@chendpoc/pi-memory 0.1.13 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +284 -216
- package/UBIQUITOUS_LANGUAGE.md +81 -0
- package/dist/adapters/embed/factory.d.ts +7 -0
- package/dist/adapters/embed/factory.d.ts.map +1 -0
- package/dist/adapters/embed/factory.js +44 -0
- package/dist/adapters/embed/factory.js.map +1 -0
- package/dist/adapters/embed/hash.d.ts +3 -0
- package/dist/adapters/embed/hash.d.ts.map +1 -0
- package/dist/adapters/embed/hash.js +26 -0
- package/dist/adapters/embed/hash.js.map +1 -0
- package/dist/adapters/embed/normalize.d.ts +3 -0
- package/dist/adapters/embed/normalize.d.ts.map +1 -0
- package/dist/adapters/embed/normalize.js +15 -0
- package/dist/adapters/embed/normalize.js.map +1 -0
- package/dist/adapters/embed/ollama.d.ts +8 -0
- package/dist/adapters/embed/ollama.d.ts.map +1 -0
- package/dist/adapters/embed/ollama.js +31 -0
- package/dist/adapters/embed/ollama.js.map +1 -0
- package/dist/adapters/embed/openai.d.ts +8 -0
- package/dist/adapters/embed/openai.d.ts.map +1 -0
- package/dist/adapters/embed/openai.js +30 -0
- package/dist/adapters/embed/openai.js.map +1 -0
- package/dist/adapters/embed/types.d.ts +9 -0
- package/dist/adapters/embed/types.d.ts.map +1 -0
- package/dist/adapters/embed/types.js.map +1 -0
- package/dist/adapters/http.d.ts +13 -0
- package/dist/adapters/http.d.ts.map +1 -0
- package/dist/adapters/http.js +31 -0
- package/dist/adapters/http.js.map +1 -0
- package/dist/adapters/index.d.ts +4 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +4 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/llm/extractText.d.ts +5 -0
- package/dist/adapters/llm/extractText.d.ts.map +1 -0
- package/dist/adapters/llm/extractText.js +7 -0
- package/dist/adapters/llm/extractText.js.map +1 -0
- package/dist/adapters/llm/factory.d.ts +11 -0
- package/dist/adapters/llm/factory.d.ts.map +1 -0
- package/dist/adapters/llm/factory.js +48 -0
- package/dist/adapters/llm/factory.js.map +1 -0
- package/dist/adapters/llm/index.d.ts +8 -0
- package/dist/adapters/llm/index.d.ts.map +1 -0
- package/dist/adapters/llm/index.js +7 -0
- package/dist/adapters/llm/index.js.map +1 -0
- package/dist/adapters/llm/modelSpec.d.ts +10 -0
- package/dist/adapters/llm/modelSpec.d.ts.map +1 -0
- package/dist/adapters/llm/modelSpec.js +21 -0
- package/dist/adapters/llm/modelSpec.js.map +1 -0
- package/dist/adapters/llm/ollama.d.ts +4 -0
- package/dist/adapters/llm/ollama.d.ts.map +1 -0
- package/dist/adapters/llm/ollama.js +32 -0
- package/dist/adapters/llm/ollama.js.map +1 -0
- package/dist/adapters/llm/openai-compat.d.ts +4 -0
- package/dist/adapters/llm/openai-compat.d.ts.map +1 -0
- package/dist/adapters/llm/openai-compat.js +36 -0
- package/dist/adapters/llm/openai-compat.js.map +1 -0
- package/dist/adapters/llm/pi-ai.d.ts +5 -0
- package/dist/adapters/llm/pi-ai.d.ts.map +1 -0
- package/dist/adapters/llm/pi-ai.js +60 -0
- package/dist/adapters/llm/pi-ai.js.map +1 -0
- package/dist/adapters/llm/standalone.d.ts +6 -0
- package/dist/adapters/llm/standalone.d.ts.map +1 -0
- package/dist/adapters/llm/standalone.js +44 -0
- package/dist/adapters/llm/standalone.js.map +1 -0
- package/dist/adapters/llm/types.d.ts +15 -0
- package/dist/adapters/llm/types.d.ts.map +1 -0
- package/dist/adapters/llm/types.js.map +1 -0
- package/dist/cli/init.d.ts +3 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +13 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/jobs.d.ts +32 -0
- package/dist/cli/jobs.d.ts.map +1 -0
- package/dist/cli/jobs.js +100 -0
- package/dist/cli/jobs.js.map +1 -0
- package/dist/cli/log.d.ts +14 -0
- package/dist/cli/log.d.ts.map +1 -0
- package/dist/cli/log.js +32 -0
- package/dist/cli/log.js.map +1 -0
- package/dist/cli/parseArgs.d.ts +35 -0
- package/dist/cli/parseArgs.d.ts.map +1 -0
- package/dist/cli/parseArgs.js +127 -0
- package/dist/cli/parseArgs.js.map +1 -0
- package/dist/cli/status.d.ts +44 -0
- package/dist/cli/status.d.ts.map +1 -0
- package/dist/cli/status.js +238 -0
- package/dist/cli/status.js.map +1 -0
- package/dist/cli/theme.d.ts +14 -0
- package/dist/cli/theme.d.ts.map +1 -0
- package/dist/cli/theme.js +20 -0
- package/dist/cli/theme.js.map +1 -0
- package/dist/cli.js +70 -356
- package/dist/cli.js.map +1 -1
- package/dist/commands/index.d.ts +6 -0
- package/dist/commands/index.d.ts.map +1 -0
- package/dist/commands/index.js +14 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/parseMemoryStatusArgs.d.ts +3 -0
- package/dist/commands/parseMemoryStatusArgs.d.ts.map +1 -0
- package/dist/commands/parseMemoryStatusArgs.js +15 -0
- package/dist/commands/parseMemoryStatusArgs.js.map +1 -0
- package/dist/commands/parseRememberArgs.d.ts +10 -0
- package/dist/commands/parseRememberArgs.d.ts.map +1 -0
- package/dist/commands/parseRememberArgs.js +46 -0
- package/dist/commands/parseRememberArgs.js.map +1 -0
- package/dist/commands/remember.d.ts +8 -0
- package/dist/commands/remember.d.ts.map +1 -0
- package/dist/commands/remember.js +30 -0
- package/dist/commands/remember.js.map +1 -0
- package/dist/commands/status.d.ts +10 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/status.js +81 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/types.d.ts +10 -0
- package/dist/commands/types.d.ts.map +1 -0
- package/dist/commands/types.js +2 -0
- package/dist/{consolidation → commands}/types.js.map +1 -1
- package/dist/compact/index.d.ts +5 -0
- package/dist/compact/index.d.ts.map +1 -0
- package/dist/compact/index.js +5 -0
- package/dist/compact/index.js.map +1 -0
- package/dist/compact/parseMemoryExport.d.ts +6 -0
- package/dist/compact/parseMemoryExport.d.ts.map +1 -0
- package/dist/compact/parseMemoryExport.js +62 -0
- package/dist/compact/parseMemoryExport.js.map +1 -0
- package/dist/compact/register.d.ts +10 -0
- package/dist/compact/register.d.ts.map +1 -0
- package/dist/compact/register.js +47 -0
- package/dist/compact/register.js.map +1 -0
- package/dist/compact/runSummary.d.ts +6 -0
- package/dist/compact/runSummary.d.ts.map +1 -0
- package/dist/compact/runSummary.js +29 -0
- package/dist/compact/runSummary.js.map +1 -0
- package/dist/compact/subagentDelta.d.ts +6 -0
- package/dist/compact/subagentDelta.d.ts.map +1 -0
- package/dist/compact/subagentDelta.js +13 -0
- package/dist/compact/subagentDelta.js.map +1 -0
- package/dist/compact/summaryPrompt.d.ts +3 -0
- package/dist/compact/summaryPrompt.d.ts.map +1 -0
- package/dist/compact/summaryPrompt.js +41 -0
- package/dist/compact/summaryPrompt.js.map +1 -0
- package/dist/config/agentDir.d.ts +11 -0
- package/dist/config/agentDir.d.ts.map +1 -0
- package/dist/config/agentDir.js +21 -0
- package/dist/config/agentDir.js.map +1 -0
- package/dist/config/chunking.d.ts +7 -0
- package/dist/config/chunking.d.ts.map +1 -0
- package/dist/config/chunking.js +20 -0
- package/dist/config/chunking.js.map +1 -0
- package/dist/config/env.d.ts +28 -0
- package/dist/config/env.d.ts.map +1 -0
- package/dist/config/env.js +43 -0
- package/dist/config/env.js.map +1 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/loadEnv.d.ts +10 -0
- package/dist/config/loadEnv.d.ts.map +1 -0
- package/dist/config/loadEnv.js +25 -0
- package/dist/config/loadEnv.js.map +1 -0
- package/dist/config/preflight.d.ts +8 -0
- package/dist/config/preflight.d.ts.map +1 -0
- package/dist/config/preflight.js +22 -0
- package/dist/config/preflight.js.map +1 -0
- package/dist/config/preflightBudget.d.ts +9 -0
- package/dist/config/preflightBudget.d.ts.map +1 -0
- package/dist/config/preflightBudget.js +16 -0
- package/dist/config/preflightBudget.js.map +1 -0
- package/dist/config/retrieval.d.ts +10 -0
- package/dist/config/retrieval.d.ts.map +1 -0
- package/dist/config/retrieval.js +31 -0
- package/dist/config/retrieval.js.map +1 -0
- package/dist/consolidate/entryKey.d.ts +6 -0
- package/dist/consolidate/entryKey.d.ts.map +1 -0
- package/dist/consolidate/entryKey.js +5 -0
- package/dist/consolidate/entryKey.js.map +1 -0
- package/dist/consolidate/index.d.ts +5 -0
- package/dist/consolidate/index.d.ts.map +1 -0
- package/dist/consolidate/index.js +5 -0
- package/dist/consolidate/index.js.map +1 -0
- package/dist/consolidate/mergeEntries.d.ts +4 -0
- package/dist/consolidate/mergeEntries.d.ts.map +1 -0
- package/dist/consolidate/mergeEntries.js +17 -0
- package/dist/consolidate/mergeEntries.js.map +1 -0
- package/dist/consolidate/mergePrompt.d.ts +4 -0
- package/dist/consolidate/mergePrompt.d.ts.map +1 -0
- package/dist/consolidate/mergePrompt.js +40 -0
- package/dist/consolidate/mergePrompt.js.map +1 -0
- package/dist/consolidate/mergeWithLlm.d.ts +4 -0
- package/dist/consolidate/mergeWithLlm.d.ts.map +1 -0
- package/dist/consolidate/mergeWithLlm.js +27 -0
- package/dist/consolidate/mergeWithLlm.js.map +1 -0
- package/dist/consolidate/runJob.d.ts +29 -0
- package/dist/consolidate/runJob.d.ts.map +1 -0
- package/dist/consolidate/runJob.js +48 -0
- package/dist/consolidate/runJob.js.map +1 -0
- package/dist/consolidate/scheduler.d.ts +18 -0
- package/dist/consolidate/scheduler.d.ts.map +1 -0
- package/dist/consolidate/scheduler.js +32 -0
- package/dist/consolidate/scheduler.js.map +1 -0
- package/dist/constants/chunking.d.ts +7 -0
- package/dist/constants/chunking.d.ts.map +1 -0
- package/dist/constants/chunking.js +7 -0
- package/dist/constants/chunking.js.map +1 -0
- package/dist/constants/env.d.ts +47 -0
- package/dist/constants/env.d.ts.map +1 -0
- package/dist/constants/env.js +52 -0
- package/dist/constants/env.js.map +1 -0
- package/dist/constants/index.d.ts +8 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +8 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/constants/memory.d.ts +17 -0
- package/dist/constants/memory.d.ts.map +1 -0
- package/dist/constants/memory.js +16 -0
- package/dist/constants/memory.js.map +1 -0
- package/dist/constants/paths.d.ts +23 -0
- package/dist/constants/paths.d.ts.map +1 -0
- package/dist/constants/paths.js +23 -0
- package/dist/constants/paths.js.map +1 -0
- package/dist/constants/preflight.d.ts +17 -0
- package/dist/constants/preflight.d.ts.map +1 -0
- package/dist/constants/preflight.js +17 -0
- package/dist/constants/preflight.js.map +1 -0
- package/dist/constants/retrieval.d.ts +10 -0
- package/dist/constants/retrieval.d.ts.map +1 -0
- package/dist/constants/retrieval.js +10 -0
- package/dist/constants/retrieval.js.map +1 -0
- package/dist/constants/security.d.ts +5 -0
- package/dist/constants/security.d.ts.map +1 -0
- package/dist/constants/security.js +5 -0
- package/dist/constants/security.js.map +1 -0
- package/dist/constants/timing.d.ts +38 -0
- package/dist/constants/timing.d.ts.map +1 -0
- package/dist/constants/timing.js +38 -0
- package/dist/constants/timing.js.map +1 -0
- package/dist/index.d.ts +11 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -28
- package/dist/index.js.map +1 -1
- package/dist/init/workspace.d.ts +15 -0
- package/dist/init/workspace.d.ts.map +1 -0
- package/dist/init/workspace.js +32 -0
- package/dist/init/workspace.js.map +1 -0
- package/dist/ipc/jsonlFramer.d.ts +9 -0
- package/dist/ipc/jsonlFramer.d.ts.map +1 -0
- package/dist/ipc/jsonlFramer.js +26 -0
- package/dist/ipc/jsonlFramer.js.map +1 -0
- package/dist/pi-extension.d.ts +0 -2
- package/dist/pi-extension.d.ts.map +1 -1
- package/dist/pi-extension.js +175 -515
- package/dist/pi-extension.js.map +1 -1
- package/dist/preflight/episodic.d.ts +26 -0
- package/dist/preflight/episodic.d.ts.map +1 -0
- package/dist/preflight/episodic.js +125 -0
- package/dist/preflight/episodic.js.map +1 -0
- package/dist/preflight/intentCache.d.ts +13 -0
- package/dist/preflight/intentCache.d.ts.map +1 -0
- package/dist/preflight/intentCache.js +37 -0
- package/dist/preflight/intentCache.js.map +1 -0
- package/dist/preflight/queryCache.d.ts +15 -0
- package/dist/preflight/queryCache.d.ts.map +1 -0
- package/dist/preflight/queryCache.js +46 -0
- package/dist/preflight/queryCache.js.map +1 -0
- package/dist/preflight/queryIntent.d.ts +29 -0
- package/dist/preflight/queryIntent.d.ts.map +1 -0
- package/dist/preflight/queryIntent.js +91 -0
- package/dist/preflight/queryIntent.js.map +1 -0
- package/dist/preflight/render.d.ts +8 -33
- package/dist/preflight/render.d.ts.map +1 -1
- package/dist/preflight/render.js +29 -126
- package/dist/preflight/render.js.map +1 -1
- package/dist/preflight/session.d.ts +3 -0
- package/dist/preflight/session.d.ts.map +1 -0
- package/dist/preflight/session.js +6 -0
- package/dist/preflight/session.js.map +1 -0
- package/dist/preflight/strip.d.ts +0 -8
- package/dist/preflight/strip.d.ts.map +1 -1
- package/dist/preflight/strip.js +2 -11
- package/dist/preflight/strip.js.map +1 -1
- package/dist/shutdown/enqueue.d.ts +12 -0
- package/dist/shutdown/enqueue.d.ts.map +1 -0
- package/dist/shutdown/enqueue.js +14 -0
- package/dist/shutdown/enqueue.js.map +1 -0
- package/dist/shutdown/extractPrompt.d.ts +4 -0
- package/dist/shutdown/extractPrompt.d.ts.map +1 -0
- package/dist/shutdown/extractPrompt.js +29 -0
- package/dist/shutdown/extractPrompt.js.map +1 -0
- package/dist/shutdown/processed.d.ts +9 -0
- package/dist/shutdown/processed.d.ts.map +1 -0
- package/dist/shutdown/processed.js +28 -0
- package/dist/shutdown/processed.js.map +1 -0
- package/dist/shutdown/readQueue.d.ts +4 -0
- package/dist/shutdown/readQueue.d.ts.map +1 -0
- package/dist/shutdown/readQueue.js +26 -0
- package/dist/shutdown/readQueue.js.map +1 -0
- package/dist/shutdown/runDrainJob.d.ts +26 -0
- package/dist/shutdown/runDrainJob.d.ts.map +1 -0
- package/dist/shutdown/runDrainJob.js +143 -0
- package/dist/shutdown/runDrainJob.js.map +1 -0
- package/dist/shutdown/sessionReader.d.ts +6 -0
- package/dist/shutdown/sessionReader.d.ts.map +1 -0
- package/dist/shutdown/sessionReader.js +44 -0
- package/dist/shutdown/sessionReader.js.map +1 -0
- package/dist/sidecar/client.d.ts +17 -16
- package/dist/sidecar/client.d.ts.map +1 -1
- package/dist/sidecar/client.js +64 -100
- package/dist/sidecar/client.js.map +1 -1
- package/dist/sidecar/paths.d.ts +8 -0
- package/dist/sidecar/paths.d.ts.map +1 -0
- package/dist/sidecar/paths.js +23 -0
- package/dist/sidecar/paths.js.map +1 -0
- package/dist/sidecar/protocol.d.ts +54 -0
- package/dist/sidecar/protocol.d.ts.map +1 -0
- package/dist/sidecar/protocol.js +4 -0
- package/dist/sidecar/protocol.js.map +1 -0
- package/dist/sidecar/reindexBridge.d.ts +13 -0
- package/dist/sidecar/reindexBridge.d.ts.map +1 -0
- package/dist/sidecar/reindexBridge.js +37 -0
- package/dist/sidecar/reindexBridge.js.map +1 -0
- package/dist/sidecar/server/process.d.ts +2 -0
- package/dist/sidecar/server/process.d.ts.map +1 -0
- package/dist/sidecar/server/process.js +22 -0
- package/dist/sidecar/server/process.js.map +1 -0
- package/dist/sidecar/server/query.d.ts +8 -0
- package/dist/sidecar/server/query.d.ts.map +1 -0
- package/dist/sidecar/server/query.js +7 -0
- package/dist/sidecar/server/query.js.map +1 -0
- package/dist/sidecar/server/reindex.d.ts +8 -0
- package/dist/sidecar/server/reindex.d.ts.map +1 -0
- package/dist/sidecar/server/reindex.js +12 -0
- package/dist/sidecar/server/reindex.js.map +1 -0
- package/dist/sidecar/server/server.d.ts +11 -0
- package/dist/sidecar/server/server.d.ts.map +1 -0
- package/dist/sidecar/server/server.js +76 -0
- package/dist/sidecar/server/server.js.map +1 -0
- package/dist/sidecar/server/stats.d.ts +8 -0
- package/dist/sidecar/server/stats.d.ts.map +1 -0
- package/dist/sidecar/server/stats.js +18 -0
- package/dist/sidecar/server/stats.js.map +1 -0
- package/dist/sidecar/server/vec/embedder.d.ts +2 -0
- package/dist/sidecar/server/vec/embedder.d.ts.map +1 -0
- package/dist/sidecar/server/vec/embedder.js +2 -0
- package/dist/sidecar/server/vec/embedder.js.map +1 -0
- package/dist/sidecar/server/vec/mmr.d.ts +13 -0
- package/dist/sidecar/server/vec/mmr.d.ts.map +1 -0
- package/dist/sidecar/server/vec/mmr.js +36 -0
- package/dist/sidecar/server/vec/mmr.js.map +1 -0
- package/dist/sidecar/server/vec/store.d.ts +30 -0
- package/dist/sidecar/server/vec/store.d.ts.map +1 -0
- package/dist/sidecar/server/vec/store.js +179 -0
- package/dist/sidecar/server/vec/store.js.map +1 -0
- package/dist/sidecar/sidecarManager.d.ts +10 -0
- package/dist/sidecar/sidecarManager.d.ts.map +1 -0
- package/dist/sidecar/sidecarManager.js +111 -0
- package/dist/sidecar/sidecarManager.js.map +1 -0
- package/dist/sidecar/utils.d.ts +3 -0
- package/dist/sidecar/utils.d.ts.map +1 -0
- package/dist/sidecar/utils.js +27 -0
- package/dist/sidecar/utils.js.map +1 -0
- package/dist/sidecar/warmup.d.ts +6 -0
- package/dist/sidecar/warmup.d.ts.map +1 -0
- package/dist/sidecar/warmup.js +39 -0
- package/dist/sidecar/warmup.js.map +1 -0
- package/dist/store/backend.d.ts +12 -0
- package/dist/store/backend.d.ts.map +1 -0
- package/dist/store/backend.js +47 -0
- package/dist/store/backend.js.map +1 -0
- package/dist/store/index.d.ts +6 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +6 -0
- package/dist/store/index.js.map +1 -0
- package/dist/store/indexChunks.d.ts +9 -0
- package/dist/store/indexChunks.d.ts.map +1 -0
- package/dist/store/indexChunks.js +78 -0
- package/dist/store/indexChunks.js.map +1 -0
- package/dist/store/markdown/format.d.ts +5 -0
- package/dist/store/markdown/format.d.ts.map +1 -0
- package/dist/store/markdown/format.js +14 -0
- package/dist/store/markdown/format.js.map +1 -0
- package/dist/store/markdown/parse.d.ts +5 -0
- package/dist/store/markdown/parse.d.ts.map +1 -0
- package/dist/store/markdown/parse.js +57 -0
- package/dist/store/markdown/parse.js.map +1 -0
- package/dist/store/markdown/template.d.ts +3 -0
- package/dist/store/markdown/template.d.ts.map +1 -0
- package/dist/store/markdown/template.js +22 -0
- package/dist/store/markdown/template.js.map +1 -0
- package/dist/store/memoryStore.d.ts +69 -0
- package/dist/store/memoryStore.d.ts.map +1 -0
- package/dist/store/memoryStore.js +421 -0
- package/dist/store/memoryStore.js.map +1 -0
- package/dist/store/paths.d.ts +10 -0
- package/dist/store/paths.d.ts.map +1 -0
- package/dist/store/paths.js +19 -0
- package/dist/store/paths.js.map +1 -0
- package/dist/store/types.d.ts +38 -0
- package/dist/store/types.d.ts.map +1 -0
- package/dist/store/types.js +3 -0
- package/dist/store/types.js.map +1 -0
- package/dist/ui/memoryStatusWidget.d.ts +20 -0
- package/dist/ui/memoryStatusWidget.d.ts.map +1 -0
- package/dist/ui/memoryStatusWidget.js +51 -0
- package/dist/ui/memoryStatusWidget.js.map +1 -0
- package/dist/utils/debugLog.d.ts +4 -0
- package/dist/utils/debugLog.d.ts.map +1 -0
- package/dist/utils/debugLog.js +14 -0
- package/dist/utils/debugLog.js.map +1 -0
- package/dist/utils/fs.d.ts +26 -0
- package/dist/utils/fs.d.ts.map +1 -0
- package/dist/utils/fs.js +101 -0
- package/dist/utils/fs.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/paths.d.ts +22 -0
- package/dist/utils/paths.d.ts.map +1 -0
- package/dist/utils/paths.js +51 -0
- package/dist/utils/paths.js.map +1 -0
- package/dist/utils/platform.d.ts +7 -0
- package/dist/utils/platform.d.ts.map +1 -0
- package/dist/utils/platform.js +18 -0
- package/dist/utils/platform.js.map +1 -0
- package/dist/utils/scheduler.d.ts +33 -0
- package/dist/utils/scheduler.d.ts.map +1 -0
- package/dist/utils/scheduler.js +63 -0
- package/dist/utils/scheduler.js.map +1 -0
- package/dist/utils/socket.d.ts +7 -0
- package/dist/utils/socket.d.ts.map +1 -0
- package/dist/utils/socket.js +35 -0
- package/dist/utils/socket.js.map +1 -0
- package/dist/utils/time.d.ts +22 -0
- package/dist/utils/time.d.ts.map +1 -0
- package/dist/utils/time.js +42 -0
- package/dist/utils/time.js.map +1 -0
- package/doc/README-zh.md +365 -0
- package/doc/ROADMAP-zh.md +52 -0
- package/doc/ROADMAP.md +52 -0
- package/package.json +31 -10
- package/scripts/init-memory-workspace.mjs +62 -0
- package/scripts/path-utils.mjs +12 -0
- package/scripts/platform-fs.mjs +65 -0
- package/scripts/postinstall.mjs +17 -0
- package/src/adapters/embed/factory.ts +50 -0
- package/src/adapters/embed/hash.ts +29 -0
- package/src/adapters/embed/normalize.ts +13 -0
- package/src/adapters/embed/ollama.ts +49 -0
- package/src/adapters/embed/openai.ts +46 -0
- package/src/adapters/embed/types.ts +9 -0
- package/src/adapters/http.ts +42 -0
- package/src/adapters/index.ts +16 -0
- package/src/adapters/llm/extractText.ts +6 -0
- package/src/adapters/llm/factory.ts +60 -0
- package/src/adapters/llm/index.ts +7 -0
- package/src/adapters/llm/modelSpec.ts +39 -0
- package/src/adapters/llm/ollama.ts +42 -0
- package/src/adapters/llm/openai-compat.ts +46 -0
- package/src/adapters/llm/pi-ai.ts +75 -0
- package/src/adapters/llm/standalone.ts +60 -0
- package/src/adapters/llm/types.ts +16 -0
- package/src/cli/init.ts +18 -0
- package/src/cli/jobs.ts +144 -0
- package/src/cli/log.ts +46 -0
- package/src/cli/parseArgs.ts +175 -0
- package/src/cli/status.ts +323 -0
- package/src/cli/theme.ts +26 -0
- package/src/cli.ts +80 -374
- package/src/commands/index.ts +20 -0
- package/src/commands/parseMemoryStatusArgs.ts +11 -0
- package/src/commands/parseRememberArgs.ts +55 -0
- package/src/commands/remember.ts +43 -0
- package/src/commands/status.ts +112 -0
- package/src/commands/types.ts +12 -0
- package/src/compact/index.ts +4 -0
- package/src/compact/parseMemoryExport.ts +71 -0
- package/src/compact/register.ts +57 -0
- package/src/compact/runSummary.ts +42 -0
- package/src/compact/subagentDelta.ts +22 -0
- package/src/compact/summaryPrompt.ts +45 -0
- package/src/config/agentDir.ts +24 -0
- package/src/config/chunking.ts +36 -0
- package/src/config/env.ts +97 -0
- package/src/config/index.ts +7 -0
- package/src/config/loadEnv.ts +26 -0
- package/src/config/preflight.ts +34 -0
- package/src/config/preflightBudget.ts +33 -0
- package/src/config/retrieval.ts +50 -0
- package/src/consolidate/entryKey.ts +4 -0
- package/src/consolidate/index.ts +4 -0
- package/src/consolidate/mergeEntries.ts +22 -0
- package/src/consolidate/mergePrompt.ts +46 -0
- package/src/consolidate/mergeWithLlm.ts +39 -0
- package/src/consolidate/runJob.ts +81 -0
- package/src/consolidate/scheduler.ts +54 -0
- package/src/constants/chunking.ts +8 -0
- package/src/constants/env.ts +59 -0
- package/src/constants/index.ts +7 -0
- package/src/constants/memory.ts +22 -0
- package/src/constants/paths.ts +26 -0
- package/src/constants/preflight.ts +22 -0
- package/src/constants/retrieval.ts +10 -0
- package/src/constants/security.ts +5 -0
- package/src/constants/timing.ts +48 -0
- package/src/index.ts +11 -247
- package/src/init/workspace.ts +44 -0
- package/src/ipc/jsonlFramer.ts +29 -0
- package/src/pi-extension.ts +196 -605
- package/src/preflight/episodic.ts +183 -0
- package/src/preflight/intentCache.ts +44 -0
- package/src/preflight/queryCache.ts +59 -0
- package/src/preflight/queryIntent.ts +126 -0
- package/src/preflight/render.ts +34 -180
- package/src/preflight/session.ts +7 -0
- package/src/preflight/strip.ts +4 -11
- package/src/shutdown/enqueue.ts +29 -0
- package/src/shutdown/extractPrompt.ts +27 -0
- package/src/shutdown/processed.ts +45 -0
- package/src/shutdown/readQueue.ts +26 -0
- package/src/shutdown/runDrainJob.ts +202 -0
- package/src/shutdown/sessionReader.ts +61 -0
- package/src/sidecar/client.ts +98 -150
- package/src/sidecar/paths.ts +31 -0
- package/src/sidecar/protocol.ts +38 -0
- package/src/sidecar/reindexBridge.ts +52 -0
- package/src/sidecar/server/process.ts +27 -0
- package/src/sidecar/server/query.ts +16 -0
- package/src/sidecar/server/reindex.ts +21 -0
- package/src/sidecar/server/server.ts +105 -0
- package/src/sidecar/server/stats.ts +23 -0
- package/src/sidecar/server/vec/embedder.ts +1 -0
- package/src/sidecar/server/vec/mmr.ts +55 -0
- package/src/sidecar/server/vec/store.ts +232 -0
- package/src/sidecar/sidecarManager.ts +134 -0
- package/src/sidecar/utils.ts +28 -0
- package/src/sidecar/warmup.ts +50 -0
- package/src/store/backend.ts +66 -0
- package/src/store/index.ts +5 -0
- package/src/store/indexChunks.ts +89 -0
- package/src/store/markdown/format.ts +16 -0
- package/src/store/markdown/parse.ts +63 -0
- package/src/store/markdown/template.ts +24 -0
- package/src/store/memoryStore.ts +518 -0
- package/src/store/paths.ts +33 -0
- package/src/store/proper-lockfile.d.ts +3 -0
- package/src/store/types.ts +46 -0
- package/src/ui/memoryStatusWidget.ts +80 -0
- package/src/utils/debugLog.ts +22 -0
- package/src/utils/fs.ts +115 -0
- package/src/utils/index.ts +68 -0
- package/src/utils/paths.ts +67 -0
- package/src/utils/platform.ts +20 -0
- package/src/utils/scheduler.ts +98 -0
- package/src/utils/socket.ts +33 -0
- package/src/utils/time.ts +51 -0
- package/templates/MEMORY.md.example +22 -0
- package/dist/adapters/ollamaClient.d.ts +0 -11
- package/dist/adapters/ollamaClient.d.ts.map +0 -1
- package/dist/adapters/ollamaClient.js +0 -122
- package/dist/adapters/ollamaClient.js.map +0 -1
- package/dist/adapters/openaiCompatClient.d.ts +0 -11
- package/dist/adapters/openaiCompatClient.d.ts.map +0 -1
- package/dist/adapters/openaiCompatClient.js +0 -118
- package/dist/adapters/openaiCompatClient.js.map +0 -1
- package/dist/adapters/piComplete.d.ts +0 -17
- package/dist/adapters/piComplete.d.ts.map +0 -1
- package/dist/adapters/piComplete.js +0 -169
- package/dist/adapters/piComplete.js.map +0 -1
- package/dist/bundle/install.d.ts +0 -34
- package/dist/bundle/install.d.ts.map +0 -1
- package/dist/bundle/install.js +0 -183
- package/dist/bundle/install.js.map +0 -1
- package/dist/cache/memoryCaches.d.ts +0 -32
- package/dist/cache/memoryCaches.d.ts.map +0 -1
- package/dist/cache/memoryCaches.js +0 -59
- package/dist/cache/memoryCaches.js.map +0 -1
- package/dist/config.d.ts +0 -55
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -85
- package/dist/config.js.map +0 -1
- package/dist/consolidation/enqueue.d.ts +0 -33
- package/dist/consolidation/enqueue.d.ts.map +0 -1
- package/dist/consolidation/enqueue.js +0 -145
- package/dist/consolidation/enqueue.js.map +0 -1
- package/dist/consolidation/index.d.ts +0 -19
- package/dist/consolidation/index.d.ts.map +0 -1
- package/dist/consolidation/index.js +0 -15
- package/dist/consolidation/index.js.map +0 -1
- package/dist/consolidation/lock.d.ts +0 -5
- package/dist/consolidation/lock.d.ts.map +0 -1
- package/dist/consolidation/lock.js +0 -33
- package/dist/consolidation/lock.js.map +0 -1
- package/dist/consolidation/log.d.ts +0 -8
- package/dist/consolidation/log.d.ts.map +0 -1
- package/dist/consolidation/log.js +0 -32
- package/dist/consolidation/log.js.map +0 -1
- package/dist/consolidation/memoryIndex.d.ts +0 -18
- package/dist/consolidation/memoryIndex.d.ts.map +0 -1
- package/dist/consolidation/memoryIndex.js +0 -100
- package/dist/consolidation/memoryIndex.js.map +0 -1
- package/dist/consolidation/phase2/runPhase2.d.ts +0 -23
- package/dist/consolidation/phase2/runPhase2.d.ts.map +0 -1
- package/dist/consolidation/phase2/runPhase2.js +0 -196
- package/dist/consolidation/phase2/runPhase2.js.map +0 -1
- package/dist/consolidation/scheduler/launchd.d.ts +0 -5
- package/dist/consolidation/scheduler/launchd.d.ts.map +0 -1
- package/dist/consolidation/scheduler/launchd.js +0 -54
- package/dist/consolidation/scheduler/launchd.js.map +0 -1
- package/dist/consolidation/scheduler/runConsolidate.d.ts +0 -26
- package/dist/consolidation/scheduler/runConsolidate.d.ts.map +0 -1
- package/dist/consolidation/scheduler/runConsolidate.js +0 -83
- package/dist/consolidation/scheduler/runConsolidate.js.map +0 -1
- package/dist/consolidation/scheduler/setupSchedule.d.ts +0 -3
- package/dist/consolidation/scheduler/setupSchedule.d.ts.map +0 -1
- package/dist/consolidation/scheduler/setupSchedule.js +0 -79
- package/dist/consolidation/scheduler/setupSchedule.js.map +0 -1
- package/dist/consolidation/scheduler/systemd.d.ts +0 -9
- package/dist/consolidation/scheduler/systemd.d.ts.map +0 -1
- package/dist/consolidation/scheduler/systemd.js +0 -57
- package/dist/consolidation/scheduler/systemd.js.map +0 -1
- package/dist/consolidation/scheduler/types.d.ts +0 -24
- package/dist/consolidation/scheduler/types.d.ts.map +0 -1
- package/dist/consolidation/scheduler/types.js.map +0 -1
- package/dist/consolidation/scope.d.ts +0 -12
- package/dist/consolidation/scope.d.ts.map +0 -1
- package/dist/consolidation/scope.js +0 -51
- package/dist/consolidation/scope.js.map +0 -1
- package/dist/consolidation/stage1/deltaExtract.d.ts +0 -3
- package/dist/consolidation/stage1/deltaExtract.d.ts.map +0 -1
- package/dist/consolidation/stage1/deltaExtract.js +0 -19
- package/dist/consolidation/stage1/deltaExtract.js.map +0 -1
- package/dist/consolidation/stage1/drainQueue.d.ts +0 -21
- package/dist/consolidation/stage1/drainQueue.d.ts.map +0 -1
- package/dist/consolidation/stage1/drainQueue.js +0 -113
- package/dist/consolidation/stage1/drainQueue.js.map +0 -1
- package/dist/consolidation/stage1/extractSession.d.ts +0 -6
- package/dist/consolidation/stage1/extractSession.d.ts.map +0 -1
- package/dist/consolidation/stage1/extractSession.js +0 -47
- package/dist/consolidation/stage1/extractSession.js.map +0 -1
- package/dist/consolidation/stage1/store.d.ts +0 -45
- package/dist/consolidation/stage1/store.d.ts.map +0 -1
- package/dist/consolidation/stage1/store.js +0 -297
- package/dist/consolidation/stage1/store.js.map +0 -1
- package/dist/consolidation/types.d.ts +0 -49
- package/dist/consolidation/types.d.ts.map +0 -1
- package/dist/errclass.d.ts +0 -7
- package/dist/errclass.d.ts.map +0 -1
- package/dist/errclass.js +0 -32
- package/dist/errclass.js.map +0 -1
- package/dist/extension.d.ts +0 -24
- package/dist/extension.d.ts.map +0 -1
- package/dist/extension.js +0 -7
- package/dist/extension.js.map +0 -1
- package/dist/fallback/index.d.ts +0 -11
- package/dist/fallback/index.d.ts.map +0 -1
- package/dist/fallback/index.js +0 -16
- package/dist/fallback/index.js.map +0 -1
- package/dist/fallback/llmRerank.d.ts +0 -19
- package/dist/fallback/llmRerank.d.ts.map +0 -1
- package/dist/fallback/llmRerank.js +0 -68
- package/dist/fallback/llmRerank.js.map +0 -1
- package/dist/fallback/memoryMd.d.ts +0 -6
- package/dist/fallback/memoryMd.d.ts.map +0 -1
- package/dist/fallback/memoryMd.js +0 -35
- package/dist/fallback/memoryMd.js.map +0 -1
- package/dist/fallback/sessionIndex.d.ts +0 -35
- package/dist/fallback/sessionIndex.d.ts.map +0 -1
- package/dist/fallback/sessionIndex.js +0 -239
- package/dist/fallback/sessionIndex.js.map +0 -1
- package/dist/fallback/sessionSearch.d.ts +0 -18
- package/dist/fallback/sessionSearch.d.ts.map +0 -1
- package/dist/fallback/sessionSearch.js +0 -234
- package/dist/fallback/sessionSearch.js.map +0 -1
- package/dist/local/graphQuery.d.ts +0 -31
- package/dist/local/graphQuery.d.ts.map +0 -1
- package/dist/local/graphQuery.js +0 -195
- package/dist/local/graphQuery.js.map +0 -1
- package/dist/paths.d.ts +0 -7
- package/dist/paths.d.ts.map +0 -1
- package/dist/paths.js +0 -26
- package/dist/paths.js.map +0 -1
- package/dist/preflight/detectIntents.d.ts +0 -102
- package/dist/preflight/detectIntents.d.ts.map +0 -1
- package/dist/preflight/detectIntents.js +0 -631
- package/dist/preflight/detectIntents.js.map +0 -1
- package/dist/preflight/hook.d.ts +0 -76
- package/dist/preflight/hook.d.ts.map +0 -1
- package/dist/preflight/hook.js +0 -122
- package/dist/preflight/hook.js.map +0 -1
- package/dist/service.d.ts +0 -72
- package/dist/service.d.ts.map +0 -1
- package/dist/service.js +0 -257
- package/dist/service.js.map +0 -1
- package/dist/session/activeBranch.d.ts +0 -17
- package/dist/session/activeBranch.d.ts.map +0 -1
- package/dist/session/activeBranch.js +0 -251
- package/dist/session/activeBranch.js.map +0 -1
- package/dist/settings.d.ts +0 -41
- package/dist/settings.d.ts.map +0 -1
- package/dist/settings.js +0 -85
- package/dist/settings.js.map +0 -1
- package/dist/sidecar/bundle.d.ts +0 -19
- package/dist/sidecar/bundle.d.ts.map +0 -1
- package/dist/sidecar/bundle.js +0 -39
- package/dist/sidecar/bundle.js.map +0 -1
- package/dist/sidecar/process.d.ts +0 -14
- package/dist/sidecar/process.d.ts.map +0 -1
- package/dist/sidecar/process.js +0 -138
- package/dist/sidecar/process.js.map +0 -1
- package/dist/tools/memoryAppend.d.ts +0 -47
- package/dist/tools/memoryAppend.d.ts.map +0 -1
- package/dist/tools/memoryAppend.js +0 -138
- package/dist/tools/memoryAppend.js.map +0 -1
- package/dist/tools/memoryRecall.d.ts +0 -113
- package/dist/tools/memoryRecall.d.ts.map +0 -1
- package/dist/tools/memoryRecall.js +0 -351
- package/dist/tools/memoryRecall.js.map +0 -1
- package/dist/trainer/bundleBuilder.d.ts +0 -30
- package/dist/trainer/bundleBuilder.d.ts.map +0 -1
- package/dist/trainer/bundleBuilder.js +0 -106
- package/dist/trainer/bundleBuilder.js.map +0 -1
- package/dist/trainer/bundleLoader.d.ts +0 -12
- package/dist/trainer/bundleLoader.d.ts.map +0 -1
- package/dist/trainer/bundleLoader.js +0 -59
- package/dist/trainer/bundleLoader.js.map +0 -1
- package/dist/trainer/deltaMerge.d.ts +0 -38
- package/dist/trainer/deltaMerge.d.ts.map +0 -1
- package/dist/trainer/deltaMerge.js +0 -183
- package/dist/trainer/deltaMerge.js.map +0 -1
- package/dist/trainer/entityResolver.d.ts +0 -27
- package/dist/trainer/entityResolver.d.ts.map +0 -1
- package/dist/trainer/entityResolver.js +0 -92
- package/dist/trainer/entityResolver.js.map +0 -1
- package/dist/trainer/extractFacts.d.ts +0 -67
- package/dist/trainer/extractFacts.d.ts.map +0 -1
- package/dist/trainer/extractFacts.js +0 -213
- package/dist/trainer/extractFacts.js.map +0 -1
- package/dist/trainer/index.d.ts +0 -54
- package/dist/trainer/index.d.ts.map +0 -1
- package/dist/trainer/index.js +0 -82
- package/dist/trainer/index.js.map +0 -1
- package/dist/trainer/llmExtractor.d.ts +0 -16
- package/dist/trainer/llmExtractor.d.ts.map +0 -1
- package/dist/trainer/llmExtractor.js +0 -146
- package/dist/trainer/llmExtractor.js.map +0 -1
- package/dist/trainer/marker.d.ts +0 -10
- package/dist/trainer/marker.d.ts.map +0 -1
- package/dist/trainer/marker.js +0 -28
- package/dist/trainer/marker.js.map +0 -1
- package/dist/trainer/scheduler.d.ts +0 -33
- package/dist/trainer/scheduler.d.ts.map +0 -1
- package/dist/trainer/scheduler.js +0 -73
- package/dist/trainer/scheduler.js.map +0 -1
- package/dist/trainer/sessionLoader.d.ts +0 -26
- package/dist/trainer/sessionLoader.d.ts.map +0 -1
- package/dist/trainer/sessionLoader.js +0 -145
- package/dist/trainer/sessionLoader.js.map +0 -1
- package/dist/types.d.ts +0 -135
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -8
- package/dist/types.js.map +0 -1
- package/src/adapters/ollamaClient.ts +0 -179
- package/src/adapters/openaiCompatClient.ts +0 -155
- package/src/adapters/piComplete.ts +0 -233
- package/src/bundle/install.ts +0 -206
- package/src/cache/memoryCaches.ts +0 -72
- package/src/config.ts +0 -180
- package/src/consolidation/enqueue.ts +0 -205
- package/src/consolidation/index.ts +0 -90
- package/src/consolidation/lock.ts +0 -41
- package/src/consolidation/log.ts +0 -43
- package/src/consolidation/memoryIndex.ts +0 -127
- package/src/consolidation/phase2/runPhase2.ts +0 -255
- package/src/consolidation/scheduler/launchd.ts +0 -62
- package/src/consolidation/scheduler/runConsolidate.ts +0 -115
- package/src/consolidation/scheduler/setupSchedule.ts +0 -106
- package/src/consolidation/scheduler/systemd.ts +0 -78
- package/src/consolidation/scheduler/types.ts +0 -27
- package/src/consolidation/scope.ts +0 -58
- package/src/consolidation/stage1/deltaExtract.ts +0 -29
- package/src/consolidation/stage1/drainQueue.ts +0 -143
- package/src/consolidation/stage1/extractSession.ts +0 -58
- package/src/consolidation/stage1/store.ts +0 -411
- package/src/consolidation/types.ts +0 -52
- package/src/errclass.ts +0 -37
- package/src/extension.ts +0 -23
- package/src/fallback/index.ts +0 -24
- package/src/fallback/llmRerank.ts +0 -97
- package/src/fallback/memoryMd.ts +0 -36
- package/src/fallback/sessionIndex.ts +0 -290
- package/src/fallback/sessionSearch.ts +0 -261
- package/src/local/graphQuery.ts +0 -252
- package/src/paths.ts +0 -28
- package/src/preflight/detectIntents.ts +0 -660
- package/src/preflight/hook.ts +0 -213
- package/src/service.ts +0 -306
- package/src/session/activeBranch.ts +0 -294
- package/src/settings.ts +0 -161
- package/src/sidecar/bundle.ts +0 -52
- package/src/sidecar/process.ts +0 -160
- package/src/tools/memoryAppend.ts +0 -166
- package/src/tools/memoryRecall.ts +0 -388
- package/src/trainer/bundleBuilder.ts +0 -192
- package/src/trainer/bundleLoader.ts +0 -105
- package/src/trainer/deltaMerge.ts +0 -221
- package/src/trainer/entityResolver.ts +0 -140
- package/src/trainer/extractFacts.ts +0 -312
- package/src/trainer/index.ts +0 -147
- package/src/trainer/llmExtractor.ts +0 -206
- package/src/trainer/marker.ts +0 -30
- package/src/trainer/scheduler.ts +0 -107
- package/src/trainer/sessionLoader.ts +0 -176
- package/src/types.ts +0 -168
- /package/dist/{consolidation/scheduler → adapters/embed}/types.js +0 -0
- /package/dist/{consolidation → adapters/llm}/types.js +0 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
import type { Embedder } from "../../../adapters/embed/types.js";
|
|
4
|
+
import type { IndexDocument, MemoryEntry } from "../../protocol.js";
|
|
5
|
+
import { ensureDirSync, pathDirname } from "../../../utils/fs.js";
|
|
6
|
+
import { readRetrievalConfig } from "../../../config/retrieval.js";
|
|
7
|
+
import { getEmbedder } from "./embedder.js";
|
|
8
|
+
import { cosineSimilarity, distanceToRelevance, mmrSelect, type ScoredCandidate } from "./mmr.js";
|
|
9
|
+
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
|
|
12
|
+
type ChunkRow = {
|
|
13
|
+
chunk_id: string;
|
|
14
|
+
content: string;
|
|
15
|
+
source: string;
|
|
16
|
+
timestamp: string;
|
|
17
|
+
embedding: Buffer;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type StoredEmbeddingMeta = {
|
|
21
|
+
model: string;
|
|
22
|
+
provider: string;
|
|
23
|
+
dim: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type ReindexOutcome = {
|
|
27
|
+
indexed: number;
|
|
28
|
+
indexGeneration: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function embeddingToBlob(embedding: Float32Array): Buffer {
|
|
32
|
+
return Buffer.from(embedding.buffer, embedding.byteOffset, embedding.byteLength);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function blobToEmbedding(blob: Buffer): Float32Array {
|
|
36
|
+
return new Float32Array(blob.buffer, blob.byteOffset, blob.byteLength / 4);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class VecStore {
|
|
40
|
+
private db: import("better-sqlite3").Database;
|
|
41
|
+
|
|
42
|
+
constructor(dbPath: string) {
|
|
43
|
+
ensureDirSync(pathDirname(dbPath));
|
|
44
|
+
const Database = require("better-sqlite3") as typeof import("better-sqlite3");
|
|
45
|
+
this.db = new Database(dbPath);
|
|
46
|
+
this.initSchema();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private initSchema(): void {
|
|
50
|
+
this.db.exec(`
|
|
51
|
+
CREATE TABLE IF NOT EXISTS meta (
|
|
52
|
+
key TEXT PRIMARY KEY,
|
|
53
|
+
value TEXT NOT NULL
|
|
54
|
+
);
|
|
55
|
+
CREATE TABLE IF NOT EXISTS memory_chunks (
|
|
56
|
+
chunk_id TEXT PRIMARY KEY,
|
|
57
|
+
content TEXT NOT NULL,
|
|
58
|
+
source TEXT NOT NULL,
|
|
59
|
+
timestamp TEXT NOT NULL,
|
|
60
|
+
embedding BLOB NOT NULL
|
|
61
|
+
);
|
|
62
|
+
`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getStoredEmbeddingMeta(): StoredEmbeddingMeta | null {
|
|
66
|
+
const read = (key: string): string | undefined =>
|
|
67
|
+
this.db.prepare("SELECT value FROM meta WHERE key = ?").pluck().get(key) as string | undefined;
|
|
68
|
+
|
|
69
|
+
const model = read("embedding_model");
|
|
70
|
+
const provider = read("embedding_provider");
|
|
71
|
+
const dimRaw = read("embedding_dim");
|
|
72
|
+
if (!model || !provider || !dimRaw) return null;
|
|
73
|
+
|
|
74
|
+
const dim = Number.parseInt(dimRaw, 10);
|
|
75
|
+
if (!Number.isFinite(dim)) return null;
|
|
76
|
+
return { model, provider, dim };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
embeddingMetaMatches(embedder: Embedder): boolean {
|
|
80
|
+
const stored = this.getStoredEmbeddingMeta();
|
|
81
|
+
if (!stored) return true;
|
|
82
|
+
return (
|
|
83
|
+
stored.model === embedder.model &&
|
|
84
|
+
stored.provider === embedder.provider &&
|
|
85
|
+
stored.dim === embedder.dim
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getIndexGeneration(): number {
|
|
90
|
+
const raw = this.db.prepare("SELECT value FROM meta WHERE key = 'index_generation'").pluck().get();
|
|
91
|
+
return Number(raw ?? "0");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
getChunkCount(): number {
|
|
95
|
+
const row = this.db.prepare("SELECT COUNT(*) AS count FROM memory_chunks").get() as { count: number };
|
|
96
|
+
return row.count;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private clearChunksIfEmbeddingMismatch(embedder: Embedder): void {
|
|
100
|
+
if (this.embeddingMetaMatches(embedder)) return;
|
|
101
|
+
this.db.prepare("DELETE FROM memory_chunks").run();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private bumpIndexGeneration(): number {
|
|
105
|
+
const generation = this.getIndexGeneration() + 1;
|
|
106
|
+
this.db
|
|
107
|
+
.prepare("INSERT INTO meta(key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value")
|
|
108
|
+
.run("index_generation", String(generation));
|
|
109
|
+
return generation;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private writeEmbeddingMeta(embedder: Embedder): void {
|
|
113
|
+
const upsert = this.db.prepare(
|
|
114
|
+
"INSERT INTO meta(key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value",
|
|
115
|
+
);
|
|
116
|
+
upsert.run("embedding_model", embedder.model);
|
|
117
|
+
upsert.run("embedding_provider", embedder.provider);
|
|
118
|
+
upsert.run("embedding_dim", String(embedder.dim));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async reindex(documents: IndexDocument[]): Promise<ReindexOutcome> {
|
|
122
|
+
const embedder = getEmbedder();
|
|
123
|
+
this.clearChunksIfEmbeddingMismatch(embedder);
|
|
124
|
+
|
|
125
|
+
if (documents.length === 0) {
|
|
126
|
+
const indexGeneration = this.getIndexGeneration();
|
|
127
|
+
this.writeEmbeddingMeta(embedder);
|
|
128
|
+
return { indexed: 0, indexGeneration };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const embeddings = await embedder.embedBatch(documents.map((doc) => doc.content));
|
|
132
|
+
|
|
133
|
+
const sync = this.db.transaction((docs: IndexDocument[], vectors: Float32Array[]) => {
|
|
134
|
+
const incomingIds = new Set(docs.map((doc) => doc.id));
|
|
135
|
+
const existing = this.db.prepare("SELECT chunk_id FROM memory_chunks").all() as Array<{ chunk_id: string }>;
|
|
136
|
+
|
|
137
|
+
const deleteChunk = this.db.prepare("DELETE FROM memory_chunks WHERE chunk_id = ?");
|
|
138
|
+
for (const row of existing) {
|
|
139
|
+
if (!incomingIds.has(row.chunk_id)) {
|
|
140
|
+
deleteChunk.run(row.chunk_id);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const upsert = this.db.prepare(`
|
|
145
|
+
INSERT INTO memory_chunks(chunk_id, content, source, timestamp, embedding)
|
|
146
|
+
VALUES (?, ?, ?, ?, ?)
|
|
147
|
+
ON CONFLICT(chunk_id) DO UPDATE SET
|
|
148
|
+
content = excluded.content,
|
|
149
|
+
source = excluded.source,
|
|
150
|
+
timestamp = excluded.timestamp,
|
|
151
|
+
embedding = excluded.embedding
|
|
152
|
+
`);
|
|
153
|
+
|
|
154
|
+
for (let i = 0; i < docs.length; i++) {
|
|
155
|
+
const doc = docs[i]!;
|
|
156
|
+
upsert.run(doc.id, doc.content, doc.source, doc.timestamp, embeddingToBlob(vectors[i]!));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const indexGeneration = this.bumpIndexGeneration();
|
|
160
|
+
this.writeEmbeddingMeta(embedder);
|
|
161
|
+
return { indexed: docs.length, indexGeneration };
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
return sync(documents, embeddings);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async query(queryText: string, topK?: number): Promise<MemoryEntry[]> {
|
|
168
|
+
const retrieval = readRetrievalConfig();
|
|
169
|
+
const limit = topK ?? retrieval.topK;
|
|
170
|
+
const embedder = getEmbedder();
|
|
171
|
+
this.clearChunksIfEmbeddingMismatch(embedder);
|
|
172
|
+
|
|
173
|
+
const queryEmbedding = await embedder.embed(queryText);
|
|
174
|
+
|
|
175
|
+
const rows = this.db
|
|
176
|
+
.prepare("SELECT chunk_id, content, source, timestamp, embedding FROM memory_chunks")
|
|
177
|
+
.all() as ChunkRow[];
|
|
178
|
+
|
|
179
|
+
if (rows.length === 0) return [];
|
|
180
|
+
|
|
181
|
+
const candidates: ScoredCandidate[] = [];
|
|
182
|
+
for (const row of rows) {
|
|
183
|
+
const embedding = blobToEmbedding(row.embedding);
|
|
184
|
+
if (embedding.length !== queryEmbedding.length) continue;
|
|
185
|
+
const similarity = cosineSimilarity(queryEmbedding, embedding);
|
|
186
|
+
if (similarity < retrieval.minRelevance) continue;
|
|
187
|
+
candidates.push({
|
|
188
|
+
chunkId: row.chunk_id,
|
|
189
|
+
content: row.content,
|
|
190
|
+
source: row.source,
|
|
191
|
+
timestamp: row.timestamp,
|
|
192
|
+
distance: 1 - similarity,
|
|
193
|
+
embedding,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (candidates.length === 0) return [];
|
|
198
|
+
|
|
199
|
+
candidates.sort((a, b) => a.distance - b.distance);
|
|
200
|
+
const poolSize = Math.min(limit * retrieval.candidatePoolMultiplier, candidates.length);
|
|
201
|
+
const pool = candidates.slice(0, poolSize);
|
|
202
|
+
const selected = mmrSelect(queryEmbedding, pool, limit, retrieval.mmrLambda);
|
|
203
|
+
|
|
204
|
+
return selected.map((item) => ({
|
|
205
|
+
content: item.content,
|
|
206
|
+
source: item.source,
|
|
207
|
+
timestamp: item.timestamp,
|
|
208
|
+
relevance: distanceToRelevance(item.distance),
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
close(): void {
|
|
213
|
+
this.db.close();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const stores = new Map<string, VecStore>();
|
|
218
|
+
|
|
219
|
+
export function getVecStore(dbPath: string): VecStore {
|
|
220
|
+
let store = stores.get(dbPath);
|
|
221
|
+
if (!store) {
|
|
222
|
+
store = new VecStore(dbPath);
|
|
223
|
+
stores.set(dbPath, store);
|
|
224
|
+
}
|
|
225
|
+
return store;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** @internal test hook */
|
|
229
|
+
export function resetVecStoreForTests(): void {
|
|
230
|
+
for (const store of stores.values()) store.close();
|
|
231
|
+
stores.clear();
|
|
232
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Agent 侧:connect-or-create、spawn lock、execa 生命周期
|
|
2
|
+
import { readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
|
|
4
|
+
import { execa } from "execa";
|
|
5
|
+
|
|
6
|
+
import { SIDECAR_SPAWN_LOCK_FILE } from "../constants/paths.js";
|
|
7
|
+
import {
|
|
8
|
+
SIDECAR_FORCE_KILL_DELAY_MS,
|
|
9
|
+
SIDECAR_SPAWN_LOCK_STALE_MS,
|
|
10
|
+
SIDECAR_START_TIMEOUT_MS,
|
|
11
|
+
} from "../constants/timing.js";
|
|
12
|
+
import { ensureDirSync, joinPath, pathDirname, pathExists } from "../utils/fs.js";
|
|
13
|
+
import { nowMs } from "../utils/time.js";
|
|
14
|
+
import { ping } from "./client.js";
|
|
15
|
+
import { resolveSidecarEntry } from "./paths.js";
|
|
16
|
+
import { canConnect, waitUntilReady } from "./utils.js";
|
|
17
|
+
|
|
18
|
+
export { resolveSidecarEntry } from "./paths.js";
|
|
19
|
+
|
|
20
|
+
const START_TIMEOUT_MS = SIDECAR_START_TIMEOUT_MS;
|
|
21
|
+
|
|
22
|
+
let instance: SidecarManager | undefined;
|
|
23
|
+
|
|
24
|
+
export type SidecarOpts = {
|
|
25
|
+
entry?: string;
|
|
26
|
+
socketPath: string;
|
|
27
|
+
dbPath: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** 上层唯一入口:确保 sidecar 在跑(attach 或 spawn) */
|
|
31
|
+
export async function ensureSidecarRunning(opts: SidecarOpts): Promise<void> {
|
|
32
|
+
const resolved = { ...opts, entry: opts.entry ?? resolveSidecarEntry() };
|
|
33
|
+
ensureDirSync(pathDirname(resolved.socketPath));
|
|
34
|
+
|
|
35
|
+
if (await canConnect(resolved.socketPath)) return;
|
|
36
|
+
|
|
37
|
+
if (!acquireSpawnLock(resolved.socketPath)) {
|
|
38
|
+
await waitUntilReady(() => canConnect(resolved.socketPath), START_TIMEOUT_MS);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
if (await canConnect(resolved.socketPath)) return;
|
|
44
|
+
await getInstance().spawn(resolved);
|
|
45
|
+
} finally {
|
|
46
|
+
releaseSpawnLock(resolved.socketPath);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function stopSidecar(): Promise<void> {
|
|
51
|
+
await getInstance().stop();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getInstance(): SidecarManager {
|
|
55
|
+
instance ??= new SidecarManager();
|
|
56
|
+
return instance;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
class SidecarManager {
|
|
60
|
+
private child?: ReturnType<typeof execa>;
|
|
61
|
+
|
|
62
|
+
async spawn(opts: Required<SidecarOpts>): Promise<void> {
|
|
63
|
+
if (await ping(opts.socketPath)) return;
|
|
64
|
+
|
|
65
|
+
this.child = execa(
|
|
66
|
+
process.execPath,
|
|
67
|
+
[opts.entry, "--socket", opts.socketPath, "--db", opts.dbPath],
|
|
68
|
+
{
|
|
69
|
+
stdio: "ignore",
|
|
70
|
+
cleanup: true,
|
|
71
|
+
forceKillAfterDelay: SIDECAR_FORCE_KILL_DELAY_MS,
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
this.child.catch(() => {});
|
|
76
|
+
|
|
77
|
+
await waitUntilReady(() => ping(opts.socketPath), START_TIMEOUT_MS);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async stop(): Promise<void> {
|
|
81
|
+
this.child?.kill("SIGTERM");
|
|
82
|
+
await this.child;
|
|
83
|
+
this.child = undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function spawnLockPath(socketPath: string): string {
|
|
88
|
+
return joinPath(pathDirname(socketPath), SIDECAR_SPAWN_LOCK_FILE);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function acquireSpawnLock(socketPath: string): boolean {
|
|
92
|
+
const lockPath = spawnLockPath(socketPath);
|
|
93
|
+
for (let i = 0; i < 5; i++) {
|
|
94
|
+
try {
|
|
95
|
+
writeFileSync(lockPath, `${process.pid}\n${nowMs()}\n`, { flag: "wx" });
|
|
96
|
+
return true;
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
99
|
+
if (isLockStale(lockPath)) {
|
|
100
|
+
try {
|
|
101
|
+
unlinkSync(lockPath);
|
|
102
|
+
} catch {}
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isLockStale(lockPath: string): boolean {
|
|
112
|
+
if (!pathExists(lockPath)) return false;
|
|
113
|
+
try {
|
|
114
|
+
const [pidLine = "", tsLine = "0"] = readFileSync(lockPath, "utf8").trim().split("\n");
|
|
115
|
+
const pid = Number.parseInt(pidLine, 10);
|
|
116
|
+
const ts = Number.parseInt(tsLine, 10);
|
|
117
|
+
if (Number.isFinite(pid)) {
|
|
118
|
+
try {
|
|
119
|
+
process.kill(pid, 0);
|
|
120
|
+
} catch {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return !Number.isFinite(ts) || nowMs() - ts > SIDECAR_SPAWN_LOCK_STALE_MS;
|
|
125
|
+
} catch {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function releaseSpawnLock(socketPath: string): void {
|
|
131
|
+
try {
|
|
132
|
+
unlinkSync(spawnLockPath(socketPath));
|
|
133
|
+
} catch {}
|
|
134
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import net from "node:net";
|
|
2
|
+
|
|
3
|
+
export function waitUntilReady(predicate: () => Promise<boolean>, timeout: number): Promise<void> {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const timer = setTimeout(() => {
|
|
6
|
+
reject(new Error(`Timeout waiting for predicate after ${timeout}ms`));
|
|
7
|
+
}, timeout);
|
|
8
|
+
const interval = setInterval(async () => {
|
|
9
|
+
if (await predicate()) {
|
|
10
|
+
clearInterval(interval);
|
|
11
|
+
clearTimeout(timer);
|
|
12
|
+
resolve();
|
|
13
|
+
}
|
|
14
|
+
}, 100);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function canConnect(socketPath: string): Promise<boolean> {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
const socket = net.connect(socketPath);
|
|
21
|
+
socket.on("connect", () => {
|
|
22
|
+
resolve(true);
|
|
23
|
+
});
|
|
24
|
+
socket.on("error", () => {
|
|
25
|
+
resolve(false);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { fetchIndexStats, ping, query } from "./client.js";
|
|
2
|
+
import { SIDECAR_WARMUP_QUERY_TIMEOUT_MS } from "../constants/timing.js";
|
|
3
|
+
import { debugMemory } from "../utils/debugLog.js";
|
|
4
|
+
import { nowMs } from "../utils/time.js";
|
|
5
|
+
|
|
6
|
+
export type WarmSidecarOptions = {
|
|
7
|
+
queryTimeoutMs?: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/** Pre-open sidecar DB and optionally smoke-test embed + vec paths. Fail-silent. */
|
|
11
|
+
export async function warmSidecar(
|
|
12
|
+
socketPath: string,
|
|
13
|
+
options: WarmSidecarOptions = {},
|
|
14
|
+
): Promise<void> {
|
|
15
|
+
const startedAt = nowMs();
|
|
16
|
+
|
|
17
|
+
if (!(await ping(socketPath))) {
|
|
18
|
+
debugMemory("sidecar", "warm", { ok: false, reason: "ping" });
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const statsStartedAt = nowMs();
|
|
23
|
+
const statsResult = await fetchIndexStats(socketPath);
|
|
24
|
+
const statsMs = nowMs() - statsStartedAt;
|
|
25
|
+
|
|
26
|
+
if ("error" in statsResult) {
|
|
27
|
+
debugMemory("sidecar", "warm", { ok: false, stats_ms: statsMs, reason: "stats" });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let queryMs = 0;
|
|
32
|
+
if (statsResult.stats.chunk_count > 0) {
|
|
33
|
+
const queryStartedAt = nowMs();
|
|
34
|
+
const queryTimeoutMs = options.queryTimeoutMs ?? SIDECAR_WARMUP_QUERY_TIMEOUT_MS;
|
|
35
|
+
try {
|
|
36
|
+
await query(socketPath, ".", queryTimeoutMs);
|
|
37
|
+
} catch {
|
|
38
|
+
// warm query is best-effort
|
|
39
|
+
}
|
|
40
|
+
queryMs = nowMs() - queryStartedAt;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
debugMemory("sidecar", "warm", {
|
|
44
|
+
ok: true,
|
|
45
|
+
stats_ms: statsMs,
|
|
46
|
+
query_ms: queryMs,
|
|
47
|
+
chunks: statsResult.stats.chunk_count,
|
|
48
|
+
total_ms: nowMs() - startedAt,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import lockfile from "proper-lockfile";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
MEMORY_LOCK_MAX_TIMEOUT_MS,
|
|
5
|
+
MEMORY_LOCK_MIN_TIMEOUT_MS,
|
|
6
|
+
MEMORY_LOCK_RETRIES,
|
|
7
|
+
} from "../constants/timing.js";
|
|
8
|
+
import {
|
|
9
|
+
ensureDir,
|
|
10
|
+
ensureFile,
|
|
11
|
+
joinPath,
|
|
12
|
+
listDir,
|
|
13
|
+
pathDirname,
|
|
14
|
+
readText,
|
|
15
|
+
removeFile,
|
|
16
|
+
writeText,
|
|
17
|
+
} from "../utils/fs.js";
|
|
18
|
+
|
|
19
|
+
import { isAutoOverflowFile } from "./paths.js";
|
|
20
|
+
|
|
21
|
+
export class MarkdownMemoryBackend {
|
|
22
|
+
constructor(private readonly memoryFile: string) {}
|
|
23
|
+
|
|
24
|
+
async ensureAgentDir(): Promise<void> {
|
|
25
|
+
await ensureDir(pathDirname(this.memoryFile));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async readText(path: string): Promise<string> {
|
|
29
|
+
return readText(path);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async writeText(path: string, content: string): Promise<void> {
|
|
33
|
+
await writeText(path, content);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async listAutoFiles(agentDir: string): Promise<string[]> {
|
|
37
|
+
const names = await listDir(agentDir);
|
|
38
|
+
return names.filter(isAutoOverflowFile).sort();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
autoFilePath(agentDir: string, fileName: string): string {
|
|
42
|
+
return joinPath(agentDir, fileName);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async deleteAutoFile(path: string): Promise<void> {
|
|
46
|
+
await removeFile(path);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async withMemoryLock<T>(fn: () => Promise<T>): Promise<T> {
|
|
50
|
+
await this.ensureAgentDir();
|
|
51
|
+
await ensureFile(this.memoryFile);
|
|
52
|
+
|
|
53
|
+
const release = await lockfile.lock(this.memoryFile, {
|
|
54
|
+
retries: {
|
|
55
|
+
retries: MEMORY_LOCK_RETRIES,
|
|
56
|
+
minTimeout: MEMORY_LOCK_MIN_TIMEOUT_MS,
|
|
57
|
+
maxTimeout: MEMORY_LOCK_MAX_TIMEOUT_MS,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
try {
|
|
61
|
+
return await fn();
|
|
62
|
+
} finally {
|
|
63
|
+
await release();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { IndexDocument } from "../sidecar/protocol.js";
|
|
2
|
+
import type { ChunkingConfig } from "../config/chunking.js";
|
|
3
|
+
import { CHUNKING_DISABLED_MAX_CHARS } from "../constants/chunking.js";
|
|
4
|
+
import { pathBasename } from "../utils/fs.js";
|
|
5
|
+
import type { MemorySection, ParsedEntry } from "./types.js";
|
|
6
|
+
|
|
7
|
+
function formatChunkContent(section: MemorySection, body: string): string {
|
|
8
|
+
return `[${section}] ${body.trim()}`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Split long text on paragraph / line / sentence boundaries, then hard-wrap at maxChars.
|
|
13
|
+
*/
|
|
14
|
+
export function splitTextByMaxChars(text: string, maxChars: number): string[] {
|
|
15
|
+
const trimmed = text.trim();
|
|
16
|
+
if (!trimmed) return [];
|
|
17
|
+
if (maxChars <= 0 || trimmed.length <= maxChars) return [trimmed];
|
|
18
|
+
|
|
19
|
+
const parts: string[] = [];
|
|
20
|
+
let remaining = trimmed;
|
|
21
|
+
|
|
22
|
+
while (remaining.length > maxChars) {
|
|
23
|
+
let cut = maxChars;
|
|
24
|
+
const minCut = Math.floor(maxChars * 0.35);
|
|
25
|
+
|
|
26
|
+
const paragraph = remaining.lastIndexOf("\n\n", cut);
|
|
27
|
+
if (paragraph >= minCut) {
|
|
28
|
+
cut = paragraph;
|
|
29
|
+
} else {
|
|
30
|
+
const line = remaining.lastIndexOf("\n", cut);
|
|
31
|
+
if (line >= minCut) {
|
|
32
|
+
cut = line;
|
|
33
|
+
} else {
|
|
34
|
+
const sentence = remaining.lastIndexOf(". ", cut);
|
|
35
|
+
if (sentence >= minCut) {
|
|
36
|
+
cut = sentence + 1;
|
|
37
|
+
} else {
|
|
38
|
+
const space = remaining.lastIndexOf(" ", cut);
|
|
39
|
+
if (space >= minCut) {
|
|
40
|
+
cut = space;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const part = remaining.slice(0, cut).trim();
|
|
47
|
+
if (part) parts.push(part);
|
|
48
|
+
remaining = remaining.slice(cut).trim();
|
|
49
|
+
if (!part && remaining.length > 0) {
|
|
50
|
+
parts.push(remaining.slice(0, maxChars).trim());
|
|
51
|
+
remaining = remaining.slice(maxChars).trim();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (remaining) parts.push(remaining);
|
|
56
|
+
return parts.length > 0 ? parts : [trimmed];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function buildIndexDocuments(
|
|
60
|
+
entries: ParsedEntry[],
|
|
61
|
+
chunking: ChunkingConfig,
|
|
62
|
+
): IndexDocument[] {
|
|
63
|
+
const docs: IndexDocument[] = [];
|
|
64
|
+
const maxChars =
|
|
65
|
+
chunking.maxChars <= CHUNKING_DISABLED_MAX_CHARS
|
|
66
|
+
? CHUNKING_DISABLED_MAX_CHARS
|
|
67
|
+
: chunking.maxChars;
|
|
68
|
+
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
const bodies =
|
|
71
|
+
maxChars <= CHUNKING_DISABLED_MAX_CHARS
|
|
72
|
+
? [entry.content.trim()].filter(Boolean)
|
|
73
|
+
: splitTextByMaxChars(entry.content, maxChars);
|
|
74
|
+
|
|
75
|
+
if (bodies.length === 0) continue;
|
|
76
|
+
|
|
77
|
+
bodies.forEach((body, index) => {
|
|
78
|
+
const id = bodies.length === 1 ? entry.id : `${entry.id}#${index}`;
|
|
79
|
+
docs.push({
|
|
80
|
+
id,
|
|
81
|
+
content: formatChunkContent(entry.section, body),
|
|
82
|
+
source: pathBasename(entry.sourceFile),
|
|
83
|
+
timestamp: entry.timestamp,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return docs;
|
|
89
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { MemorySection, StoreMemoryEntry } from "../types.js";
|
|
2
|
+
|
|
3
|
+
export function formatEntryLine(entry: StoreMemoryEntry): string {
|
|
4
|
+
const prefix = entry.userAuthored ? "[user] " : "";
|
|
5
|
+
const meta = `<!-- id:${entry.id}${entry.userAuthored ? " user" : ""} ts:${entry.timestamp} -->`;
|
|
6
|
+
return `- ${prefix}${entry.content.trim()} ${meta}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function formatSectionHeader(section: MemorySection): string {
|
|
10
|
+
return `## ${section}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function countLines(content: string): number {
|
|
14
|
+
if (!content) return 0;
|
|
15
|
+
return content.split("\n").length;
|
|
16
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { pathBasename } from "../../utils/fs.js";
|
|
2
|
+
import { epochTimestamp } from "../../utils/time.js";
|
|
3
|
+
|
|
4
|
+
import { OVERFLOW_POINTER_RE } from "../../constants/memory.js";
|
|
5
|
+
import { MEMORY_SECTIONS, type MemorySection, type ParsedEntry } from "../types.js";
|
|
6
|
+
|
|
7
|
+
const SECTION_RE = /^##\s+(Preferences|Conventions|Findings|Todos)\s*$/;
|
|
8
|
+
const ENTRY_META_RE = /<!--\s*id:([^\s]+)(?:\s+user)?(?:\s+ts:([^\s]+))?\s*-->/;
|
|
9
|
+
const USER_PREFIX_RE = /^\[user\]\s+/;
|
|
10
|
+
|
|
11
|
+
export function parseMemoryMarkdown(content: string, sourceFile: string): ParsedEntry[] {
|
|
12
|
+
const entries: ParsedEntry[] = [];
|
|
13
|
+
let section: MemorySection | undefined;
|
|
14
|
+
const lines = content.split("\n");
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < lines.length; i++) {
|
|
17
|
+
const line = lines[i]!;
|
|
18
|
+
const sectionMatch = line.match(SECTION_RE);
|
|
19
|
+
if (sectionMatch) {
|
|
20
|
+
section = sectionMatch[1] as MemorySection;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!line.startsWith("- ") || !section) continue;
|
|
25
|
+
|
|
26
|
+
const pointerMatch = line.match(OVERFLOW_POINTER_RE);
|
|
27
|
+
if (pointerMatch) continue;
|
|
28
|
+
|
|
29
|
+
const metaMatch = line.match(ENTRY_META_RE);
|
|
30
|
+
const id = metaMatch?.[1] ?? `${pathBasename(sourceFile)}:${i + 1}`;
|
|
31
|
+
const timestamp = metaMatch?.[2] ?? epochTimestamp();
|
|
32
|
+
let body = line.slice(2).replace(ENTRY_META_RE, "").trim();
|
|
33
|
+
const userAuthored = USER_PREFIX_RE.test(body);
|
|
34
|
+
if (userAuthored) body = body.replace(USER_PREFIX_RE, "").trim();
|
|
35
|
+
|
|
36
|
+
if (!body) continue;
|
|
37
|
+
|
|
38
|
+
entries.push({
|
|
39
|
+
id,
|
|
40
|
+
section,
|
|
41
|
+
content: body,
|
|
42
|
+
userAuthored: userAuthored || undefined,
|
|
43
|
+
timestamp,
|
|
44
|
+
sourceFile,
|
|
45
|
+
line: i + 1,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return entries;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function listOverflowPointers(content: string): string[] {
|
|
53
|
+
const files: string[] = [];
|
|
54
|
+
for (const line of content.split("\n")) {
|
|
55
|
+
const match = line.match(OVERFLOW_POINTER_RE);
|
|
56
|
+
if (match?.[1]) files.push(match[1]);
|
|
57
|
+
}
|
|
58
|
+
return files;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function isKnownSection(name: string): name is MemorySection {
|
|
62
|
+
return (MEMORY_SECTIONS as readonly string[]).includes(name);
|
|
63
|
+
}
|