How ludwigweiss/sillytavern-starmem works
1. What this repo is
- STARmem is a SillyTavern browser extension written primarily in JavaScript/ES modules.
- The runtime boundary is the SillyTavern client; Node.js is used only for development, testing, linting, Playwright E2E, and benchmarks.
- The extension’s top-level entry is index.js, with manifest.json, settings.html, and style.css providing the extension shell and UI.
2. Primary architectural boundary
- The repo sits between SillyTavern chat events and an external Child Model API.
- It captures conversation state from SillyTavern, retrieves memory/context, injects facts back into the prompt flow, and then performs asynchronous post-response consolidation.
- The main external dependency is the Child Model endpoint; the rest is local extension logic and persisted settings/storage.
3. Integration layer with SillyTavern
- src/integration/ contains the extension-facing glue: initialization, router integration, prompt injection, interceptors, health checks, setup wizard, settings UI, and debug helpers.
- This layer is the main adapter between SillyTavern’s event model and STARmem’s internal subsystems.
- It also owns user-visible operational controls like enable/disable, connection testing, and context-flow visibility.
4. Core runtime and shared state
- src/core/ centralizes shared constants, app state, validation, and error boundaries.
- This is the boundary that keeps configuration, invariants, and failure handling consistent across retrieval, memory, and integration subsystems.
- It prevents the rest of the system from depending directly on SillyTavern specifics or UI state.
5. 4-tier retrieval pipeline
- src/retrieval/ implements the main query pipeline:
- Tier 0 exact cache
- Tier 1 fuzzy cache / Jaccard-style matching
- Tier 2 BM25+ semantic retrieval
- Tier 3 Child Model reasoning over long context
- Query classification, routing, prompt templates, refinement, expansion, and output formatting all live here.
- The routing layer is the key latency-control boundary: cheap cache hits are preferred, BM25+ is the main semantic fallback, and the LLM is the last resort.
6. 4-memory architecture
- src/memory/ holds the long-lived memory model:
- Working Memory for recent turns / hot buffer
- Episodic Memory for consolidated conversation history
- Persona Memory for traits, preferences, and evidence-backed beliefs
- Graph Memory for entities and relationships
- src/memory/unified-retrieval.js fuses results across these stores into a single ranked result set.
- src/memory/fusion-scorer.js is the ranking boundary that combines base retrieval score, recency, graph signals, and memory-specific weights.
7. Knowledge lifecycle / adaptive consolidation
- src/akl/ is the subsystem for memory evolution over time: decay, drift detection, importance scoring, influence propagation, consolidation triggers, and storage integration.
- This layer is responsible for deciding what becomes durable memory and what fades.
- It also enforces the repo’s design principle that older lore can remain important via custom decay/boost logic instead of naïve recency-only eviction.
8. Persistence and structural storage
- src/storage/ contains persistence-facing structures such as semantic store integration, RAPTOR tree storage, and AKL metadata.
- The storage boundary is important because many memories are not just transient retrieval artifacts; they need to survive across sessions and be reloadable from extension settings or chat-scoped state.
- The repository appears optimized for extension-local persistence rather than a separate backend database service.
9. Model clients
- src/models/child-model.js and src/models/tertiary-model.js encapsulate external LLM interactions.
- The Child Model is the critical pre-generation retrieval/reasoning service used in Tier 3 and for fact extraction.
- The Tertiary Model supports asynchronous post-generation extraction or consolidation, so the generation path can stay responsive.
10. Utility and infrastructure support
- src/utils/ provides low-level helpers such as caches, token counting, clustering, idle detection, debug console, status reporting, and graph/context-flow helpers.
- These utilities are cross-cutting infrastructure used by retrieval, memory, and integration rather than standalone features.
- src/utils/context-flow.js and the debug/status tooling are especially relevant for understanding runtime data movement.
11. Test and validation architecture
- The repo has layered verification: unit tests, integration tests, E2E Playwright tests against a live SillyTavern instance, and a benchmark suite under tests/benchmark/.
- This mirrors the architecture: unit tests cover individual retrieval/memory algorithms, integration tests cover cross-subsystem flows, and E2E validates actual extension behavior in SillyTavern.
- The benchmark suite is a separate performance/evaluation subsystem with fixtures, scorers, and reporting.
12. How data flows through the system
- A chat turn enters from SillyTavern, is captured by the integration layer, and updates Working Memory.
- The user query is classified, routed through the retrieval tiers, and merged with unified memory results.
- A fused fact sheet is injected before the parent model generates the reply.
- After generation, async consolidation updates episodic, persona, graph, and storage layers, closing the loop for the next turn.
Main entry points: integration/index.js Integration API · memory/index.js Memory API · router.js Query router · retrieval/index.js Retrieval API
Components 31
Extension shell
SillyTavern extension entrypoints and visible UI surface
- Main entry · Top-level extension bootstrap for SillyTavern
index.js - Manifest · Browser extension metadata and permissions
manifest.json - Settings UI · Extension configuration surface
settings.html - Styles · Shell styling for the settings and controls
style.css
SillyTavern bridge
Event hookup, prompt injection, health checks, and setup flow
- Integration API · Primary SillyTavern integration surface
src/integration/index.js - Bootstrap · Initialization and lifecycle wiring for the extension
src/integration/init.js - Prompt inject · Injects facts and context into the chat flow
src/integration/prompt-injection.js - Router bridge · Connects SillyTavern events to retrieval routing
src/integration/router-integration.js
Core runtime
Shared state, constants, validation, and error boundaries
- Core API · Shared constants, state, validation, and error handling
src/core/index.js
Retrieval pipeline
Tiered query routing from cache to semantic and model-backed search
- Retrieval API · Entry to tiered retrieval and query handling
src/retrieval/index.js - Query router · Classifies queries and selects retrieval tiers
src/retrieval/router.js - Tier 0 · Exact-match cache layer
src/retrieval/tier-0-cache.js - Tier 1 · Fuzzy and Jaccard-style cache matching
src/retrieval/tier-1-cache.js - Tier 2 · BM25-backed semantic retrieval layer
src/retrieval/tier-2-bm25.js - Tier 3 · Child-model reasoning over long context
src/retrieval/tier-3-llm.js
Memory system
Working, episodic, persona, and graph memory plus fusion
- Memory API · Unified access to long-lived memory stores
src/memory/index.js - Working mem · Recent turns and active conversational state
src/memory/working-memory.js - Episodic mem · Consolidated history of prior exchanges
src/memory/episodic-memory.js - Persona mem · Traits, preferences, and evidence-backed beliefs
src/memory/persona-memory.js - Graph mem · Entities and relationships extracted from chat
src/memory/graph-memory.js - Unified retrieve · Merges results across memory stores
src/memory/unified-retrieval.js - Fusion scorer · Combines retrieval score, recency, and graph signals
src/memory/fusion-scorer.js
Knowledge lifecycle
Decay, drift, importance, consolidation, and propagation
- AKL API · Adaptive knowledge lifecycle and consolidation entrypoint
src/akl/index.js
Persistence layer
Extension-local storage for semantic and metadata structures
- Storage API · Semantic store, RAPTOR tree, and AKL metadata persistence
src/storage/index.js
Model clients
External Child and tertiary model integrations
- Model API · External model client surface
src/models/index.js - Child model · Primary external model for reasoning and extraction
src/models/child-model.js - Tertiary model · Async post-generation extraction and consolidation model
src/models/tertiary-model.js
Shared utilities
Cross-cutting caches, token logic, context flow, and diagnostics
- Utilities · Cross-cutting caches, token counting, clustering, context flow, and diagnostics
src/utils/index.js
Verification
Tests, end-to-end checks, and benchmarks
- Benchmarks · Performance and quality benchmark runners
tests/benchmark/runners/run-benchmark.js - E2E tests · Playwright validation against a live SillyTavern instance
tests/e2e/interceptor.spec.js - Unit tests · Focused tests for retrieval, memory, and scoring modules
tests/unit/router.test.js
Connections 36
- Main entry → Integration API: bootstraps · Main entry hands control to the integration layer
- Main entry → Core API: shares state · Extension bootstrap depends on core invariants
- Main entry → Manifest: declares · Browser extension metadata describes the entrypoint
- Main entry → Settings UI: renders · Settings UI is part of the extension shell
- Main entry → Styles: styles · Extension shell styling is loaded with the UI
- Integration API → Bootstrap: initializes · Coordinates lifecycle and registration
- Integration API → Router bridge: routes events · Connects chat events to the retrieval pipeline
- Integration API → Prompt inject: injects context · Adds facts before generation
- Integration API → Core API: validates · Uses shared runtime validation and error boundaries
- Router bridge → Retrieval API: queries · Forwards routed requests into retrieval
- Prompt inject → Memory API: reads memory · Pulls unified memory context for injection
- Prompt inject → Retrieval API: requests facts · Builds prompt payloads from retrieval results
- Retrieval API → Query router: dispatches · Routes each query to the cheapest viable tier
- Query router → Tier 0: exact hit · First latency-controlled retrieval path
- Query router → Tier 1: fuzzy hit · Fallback cache matching
- Query router → Tier 2: semantic hit · Main semantic retrieval fallback
- Query router → Tier 3: reasoning fallback · Last-resort Child Model reasoning path
- Tier 3 → Child model: calls · Uses the Child Model endpoint for reasoning
- Retrieval API → Model API: uses clients · Retrieval can invoke external model clients
- Memory API → Working mem: maintains · Tracks hot conversational state
- Memory API → Episodic mem: stores · Persists consolidated turn history
- Memory API → Persona mem: stores · Keeps durable persona facts
- Memory API → Graph mem: stores · Maintains entity and relation graph
- Memory API → Unified retrieve: fuses · Combines results across memory subsystems
- Unified retrieve → Fusion scorer: ranks · Applies score fusion across signals
- AKL API → Storage API: persists · Adaptive lifecycle writes metadata and structures
- AKL API → Memory API: evolves · Feeds consolidation back into memory stores
- AKL API → Utilities: signals · Uses shared helpers for decay, clustering, and diagnostics
- Model API → Child model: exposes · Primary external model client
- Model API → Tertiary model: exposes · Async consolidation model client
- Integration API → Utilities: observes · Context flow and status tooling support the adapter layer
- Retrieval API → Utilities: depends on · Uses caches, token counts, and graph helpers
- Benchmarks → Retrieval API: measures · Benchmarks exercise retrieval behavior
- E2E tests → Integration API: validates · End-to-end checks cover the SillyTavern bridge
- Unit tests → Query router: verifies · Unit tests cover routing and tier selection
- Unit tests → Fusion scorer: verifies · Unit tests cover cross-memory ranking