@remnic/core 1.0.0 → 1.0.2
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 +47 -0
- package/dist/access-cli.js +10 -9
- package/dist/access-cli.js.map +1 -1
- package/dist/access-schema.d.ts +6 -6
- package/dist/calibration.js +3 -2
- package/dist/calibration.js.map +1 -1
- package/dist/causal-consolidation.js +3 -2
- package/dist/causal-consolidation.js.map +1 -1
- package/dist/{chunk-QFQVZOGA.js → chunk-6UJQNRIO.js} +2 -2
- package/dist/{chunk-CDW777AI.js → chunk-LP47L3ZX.js} +2 -2
- package/dist/{chunk-LU3GQNDQ.js → chunk-M5ZBBBJI.js} +12 -1
- package/dist/chunk-M5ZBBBJI.js.map +1 -0
- package/dist/chunk-OOSWAUYB.js +47 -0
- package/dist/chunk-OOSWAUYB.js.map +1 -0
- package/dist/{chunk-YAPUAHAY.js → chunk-OTFNI3OO.js} +32 -32
- package/dist/{chunk-TKO4HZCK.js → chunk-UYSKNO6E.js} +1 -1
- package/dist/{chunk-KT4NEUNF.js → chunk-XUHI52HK.js} +80 -22
- package/dist/chunk-XUHI52HK.js.map +1 -0
- package/dist/cli.js +1 -1
- package/dist/{engine-P26JFSVY.js → engine-2A6J4XEX.js} +2 -2
- package/dist/extraction.js +4 -3
- package/dist/fallback-llm.d.ts +22 -3
- package/dist/fallback-llm.js +3 -2
- package/dist/index.js +10 -9
- package/dist/index.js.map +1 -1
- package/dist/models-json.d.ts +17 -0
- package/dist/models-json.js +12 -0
- package/dist/models-json.js.map +1 -0
- package/dist/orchestrator.js +10 -9
- package/dist/resolve-provider-secret.d.ts +29 -1
- package/dist/resolve-provider-secret.js +3 -1
- package/dist/schemas.d.ts +18 -18
- package/dist/summarizer.js +4 -3
- package/package.json +2 -1
- package/dist/chunk-KT4NEUNF.js.map +0 -1
- package/dist/chunk-LU3GQNDQ.js.map +0 -1
- /package/dist/{chunk-QFQVZOGA.js.map → chunk-6UJQNRIO.js.map} +0 -0
- /package/dist/{chunk-CDW777AI.js.map → chunk-LP47L3ZX.js.map} +0 -0
- /package/dist/{chunk-YAPUAHAY.js.map → chunk-OTFNI3OO.js.map} +0 -0
- /package/dist/{chunk-TKO4HZCK.js.map → chunk-UYSKNO6E.js.map} +0 -0
- /package/dist/{engine-P26JFSVY.js.map → engine-2A6J4XEX.js.map} +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @remnic/core
|
|
2
|
+
|
|
3
|
+
Framework-agnostic memory engine for AI agents. Orchestration, storage, extraction, search, and trust zones -- all local, all private.
|
|
4
|
+
|
|
5
|
+
Part of [Remnic](https://github.com/joshuaswarren/remnic), the universal memory layer for AI agents.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @remnic/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## What it does
|
|
14
|
+
|
|
15
|
+
Remnic Core is the engine that powers persistent memory across AI agent sessions. It handles:
|
|
16
|
+
|
|
17
|
+
- **Memory orchestration** -- three-phase flow: recall before sessions, buffer during, extract after
|
|
18
|
+
- **Storage** -- plain markdown files with YAML frontmatter on your local filesystem
|
|
19
|
+
- **Extraction** -- GPT-5.2 or local LLM (Ollama, LM Studio) extracts durable knowledge from conversations
|
|
20
|
+
- **Search** -- hybrid BM25 + vector + reranking via QMD
|
|
21
|
+
- **Trust zones** -- namespace isolation and access control for multi-agent setups
|
|
22
|
+
- **Entity tracking** -- people, projects, tools, and their relationships
|
|
23
|
+
- **Consolidation** -- periodic merging, deduplication, and summarization
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Most users interact with Remnic through a higher-level package:
|
|
28
|
+
|
|
29
|
+
| Package | Use case |
|
|
30
|
+
|---------|----------|
|
|
31
|
+
| [`@remnic/plugin-openclaw`](https://www.npmjs.com/package/@remnic/plugin-openclaw) | OpenClaw gateway plugin |
|
|
32
|
+
| [`@remnic/cli`](https://www.npmjs.com/package/@remnic/cli) | Standalone CLI and daemon |
|
|
33
|
+
| [`@remnic/server`](https://www.npmjs.com/package/@remnic/server) | Standalone HTTP + MCP server |
|
|
34
|
+
|
|
35
|
+
Use `@remnic/core` directly when building a custom integration or embedding the memory engine in your own agent framework.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { Orchestrator } from "@remnic/core";
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Fallback LLM
|
|
42
|
+
|
|
43
|
+
The core includes a fallback LLM client that resolves providers from your gateway config or OpenClaw's built-in provider catalog. It supports OpenAI-compatible and Anthropic APIs with automatic auth resolution and provider fallback chains.
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
MIT
|
package/dist/access-cli.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Orchestrator
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-OTFNI3OO.js";
|
|
4
|
+
import "./chunk-UYSKNO6E.js";
|
|
4
5
|
import "./chunk-IFFFR3MR.js";
|
|
5
|
-
import "./chunk-TKO4HZCK.js";
|
|
6
6
|
import "./chunk-Z5AAYHUC.js";
|
|
7
7
|
import "./chunk-4A24LIM2.js";
|
|
8
8
|
import "./chunk-TPB3I2AC.js";
|
|
9
9
|
import "./chunk-UHGBNIOS.js";
|
|
10
10
|
import "./chunk-ZKYI7UVO.js";
|
|
11
|
-
import "./chunk-
|
|
11
|
+
import "./chunk-LP47L3ZX.js";
|
|
12
12
|
import "./chunk-ETOW6ACV.js";
|
|
13
|
+
import "./chunk-GPGBSNKM.js";
|
|
14
|
+
import "./chunk-V3RXWQIE.js";
|
|
13
15
|
import "./chunk-G3AG3KZN.js";
|
|
14
16
|
import "./chunk-2CJCWDMR.js";
|
|
17
|
+
import "./chunk-HG2NKWR2.js";
|
|
15
18
|
import "./chunk-X7XN6YU4.js";
|
|
16
19
|
import "./chunk-BRK4ODMI.js";
|
|
17
20
|
import "./chunk-C7VW7C3F.js";
|
|
18
|
-
import "./chunk-GPGBSNKM.js";
|
|
19
|
-
import "./chunk-V3RXWQIE.js";
|
|
20
21
|
import "./chunk-YCN4BVDK.js";
|
|
21
22
|
import "./chunk-L5RPWGFK.js";
|
|
22
|
-
import "./chunk-HG2NKWR2.js";
|
|
23
23
|
import "./chunk-GJR6D6KC.js";
|
|
24
24
|
import "./chunk-H63EDPFJ.js";
|
|
25
25
|
import "./chunk-WWIQTB2Y.js";
|
|
26
|
-
import "./chunk-
|
|
26
|
+
import "./chunk-6UJQNRIO.js";
|
|
27
27
|
import "./chunk-MWGVGUIS.js";
|
|
28
28
|
import "./chunk-763GUIOU.js";
|
|
29
29
|
import "./chunk-ONRU4L2N.js";
|
|
@@ -65,8 +65,9 @@ import "./chunk-JWPLJLDU.js";
|
|
|
65
65
|
import "./chunk-YNI4S5WT.js";
|
|
66
66
|
import "./chunk-DORBM6OB.js";
|
|
67
67
|
import "./chunk-XYIK4LF6.js";
|
|
68
|
-
import "./chunk-
|
|
69
|
-
import "./chunk-
|
|
68
|
+
import "./chunk-XUHI52HK.js";
|
|
69
|
+
import "./chunk-M5ZBBBJI.js";
|
|
70
|
+
import "./chunk-OOSWAUYB.js";
|
|
70
71
|
import "./chunk-Y27UJK6V.js";
|
|
71
72
|
import "./chunk-UZB5KHKX.js";
|
|
72
73
|
import "./chunk-M5KEYE5E.js";
|
package/dist/access-cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/access-cli.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { parseConfig } from \"./config.js\";\nimport type { PluginConfig } from \"./types.js\";\nimport { Orchestrator } from \"./orchestrator.js\";\nimport { EngramAccessService } from \"./access-service.js\";\nimport { readEnvVar, resolveHomeDir } from \"./runtime/env.js\";\n\ntype CommandName = \"browse\" | \"store\";\n\ntype ParsedArgs = {\n command: CommandName;\n options: Record<string, string[]>;\n flags: Set<string>;\n};\n\ntype Runtime = {\n config: PluginConfig;\n service: EngramAccessService;\n};\n\ntype UsageErrorKind =\n | \"unsupported-command\"\n | \"unexpected-positional\"\n | \"missing-option\"\n | \"missing-content\"\n | \"invalid-integer\"\n | \"invalid-number\";\n\nclass UsageError extends Error {\n constructor(\n readonly kind: UsageErrorKind,\n readonly optionName?: string,\n ) {\n super(\"invalid access-cli arguments\");\n }\n}\n\nfunction formatUsageError(error: UsageError): string {\n switch (error.kind) {\n case \"unsupported-command\":\n return \"unsupported command\";\n case \"unexpected-positional\":\n return \"unexpected positional argument\";\n case \"missing-option\":\n return `missing required option: --${error.optionName ?? \"unknown\"}`;\n case \"missing-content\":\n return \"missing required option: --content or --content-file\";\n case \"invalid-integer\":\n return `invalid integer for --${error.optionName ?? \"unknown\"}`;\n case \"invalid-number\":\n return `invalid number for --${error.optionName ?? \"unknown\"}`;\n }\n}\n\nfunction writeCliOutput(text: string = \"\"): void {\n process.stdout.write(`${text}\\n`);\n}\n\nfunction usage(): string {\n return [\n \"Usage:\",\n \" engram-access browse [options]\",\n \" engram-access store [options]\",\n \"\",\n \"Browse options:\",\n \" --namespace <name>\",\n \" --query <text>\",\n \" --category <name>\",\n \" --status <name>\",\n \" --sort <updated_desc|updated_asc|created_desc|created_asc>\",\n \" --limit <n>\",\n \" --offset <n>\",\n \"\",\n \"Store options:\",\n \" --namespace <name>\",\n \" --session-key <key>\",\n \" --principal <principal>\",\n \" --content <text> | --content-file <path>\",\n \" --category <name>\",\n \" --confidence <0-1>\",\n \" --tag <tag> (repeatable)\",\n \" --entity-ref <ref>\",\n \" --ttl <duration>\",\n \" --source-reason <text>\",\n \" --idempotency-key <key>\",\n \" --dry-run\",\n ].join(\"\\n\");\n}\n\nfunction parseArgs(argv: string[]): ParsedArgs {\n const [commandRaw, ...rest] = argv;\n if (commandRaw !== \"browse\" && commandRaw !== \"store\") {\n throw new UsageError(\"unsupported-command\");\n }\n\n const options: Record<string, string[]> = {};\n const flags = new Set<string>();\n\n for (let i = 0; i < rest.length; i += 1) {\n const token = rest[i];\n if (!token.startsWith(\"--\")) {\n throw new UsageError(\"unexpected-positional\");\n }\n const key = token.slice(2);\n const next = rest[i + 1];\n if (!next || next.startsWith(\"--\")) {\n flags.add(key);\n continue;\n }\n if (!options[key]) {\n options[key] = [];\n }\n options[key].push(next);\n i += 1;\n }\n\n return {\n command: commandRaw,\n options,\n flags,\n };\n}\n\nfunction getLastOption(args: ParsedArgs, name: string): string | undefined {\n const values = args.options[name];\n if (!values || values.length === 0) return undefined;\n return values[values.length - 1];\n}\n\nfunction getAllOptions(args: ParsedArgs, name: string): string[] {\n return args.options[name] ?? [];\n}\n\nfunction requireOption(args: ParsedArgs, name: string): string {\n const value = getLastOption(args, name);\n if (!value || value.trim().length === 0) {\n throw new UsageError(\"missing-option\", name);\n }\n return value;\n}\n\nfunction parseIntegerOption(args: ParsedArgs, name: string): number | undefined {\n const raw = getLastOption(args, name);\n if (!raw) return undefined;\n const value = parseInt(raw, 10);\n if (!Number.isFinite(value)) {\n throw new UsageError(\"invalid-integer\", name);\n }\n return value;\n}\n\nfunction parseFloatOption(args: ParsedArgs, name: string): number | undefined {\n const raw = getLastOption(args, name);\n if (!raw) return undefined;\n const value = Number.parseFloat(raw);\n if (!Number.isFinite(value)) {\n throw new UsageError(\"invalid-number\", name);\n }\n return value;\n}\n\nfunction loadPluginConfig(): Record<string, unknown> {\n const configPath =\n readEnvVar(\"OPENCLAW_ENGRAM_CONFIG_PATH\") ||\n readEnvVar(\"OPENCLAW_CONFIG_PATH\") ||\n path.join(resolveHomeDir(), \".openclaw\", \"openclaw.json\");\n const raw = JSON.parse(fs.readFileSync(configPath, \"utf8\"));\n return raw?.plugins?.entries?.[\"openclaw-engram\"]?.config ?? {};\n}\n\nfunction buildRuntime(): Runtime {\n const config = parseConfig(loadPluginConfig());\n return {\n config,\n service: new EngramAccessService(new Orchestrator(config)),\n };\n}\n\nasync function runBrowse(args: ParsedArgs): Promise<void> {\n const { service } = buildRuntime();\n const result = await service.memoryBrowse({\n namespace: getLastOption(args, \"namespace\"),\n query: getLastOption(args, \"query\"),\n category: getLastOption(args, \"category\"),\n status: getLastOption(args, \"status\"),\n sort: getLastOption(args, \"sort\") as \"updated_desc\" | \"updated_asc\" | \"created_desc\" | \"created_asc\" | undefined,\n limit: parseIntegerOption(args, \"limit\"),\n offset: parseIntegerOption(args, \"offset\"),\n });\n console.log(JSON.stringify(result, null, 2));\n}\n\nasync function runStore(args: ParsedArgs): Promise<void> {\n const { config, service } = buildRuntime();\n const contentFile = getLastOption(args, \"content-file\");\n const inlineContent = getLastOption(args, \"content\");\n const content = contentFile ? fs.readFileSync(contentFile, \"utf8\") : inlineContent;\n if (!content || content.trim().length === 0) {\n throw new UsageError(\"missing-content\");\n }\n\n const result = await service.memoryStore({\n namespace: getLastOption(args, \"namespace\"),\n sessionKey: getLastOption(args, \"session-key\"),\n authenticatedPrincipal: getLastOption(args, \"principal\") ?? config.agentAccessHttp.principal,\n content,\n category: requireOption(args, \"category\"),\n confidence: parseFloatOption(args, \"confidence\"),\n tags: getAllOptions(args, \"tag\"),\n entityRef: getLastOption(args, \"entity-ref\"),\n ttl: getLastOption(args, \"ttl\"),\n sourceReason: getLastOption(args, \"source-reason\"),\n idempotencyKey: getLastOption(args, \"idempotency-key\"),\n dryRun: args.flags.has(\"dry-run\"),\n });\n console.log(JSON.stringify(result, null, 2));\n}\n\nexport async function main(argv: string[] = process.argv.slice(2)): Promise<void> {\n const args = parseArgs(argv);\n if (args.command === \"browse\") {\n await runBrowse(args);\n return;\n }\n await runStore(args);\n}\n\nexport function printUsage(): void {\n writeCliOutput(usage());\n}\n\nexport async function runCli(argv: string[] = process.argv.slice(2)): Promise<void> {\n try {\n await main(argv);\n } catch (error) {\n if (error instanceof UsageError) {\n writeCliOutput(formatUsageError(error));\n writeCliOutput();\n printUsage();\n process.exit(1);\n }\n\n console.error(\"access-cli failed\");\n process.exit(1);\n }\n}\n"],"mappings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AChB;AAEA,YAAQ,MAAM,mBAAmB;AACjC,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/access-cli.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { parseConfig } from \"./config.js\";\nimport type { PluginConfig } from \"./types.js\";\nimport { Orchestrator } from \"./orchestrator.js\";\nimport { EngramAccessService } from \"./access-service.js\";\nimport { readEnvVar, resolveHomeDir } from \"./runtime/env.js\";\n\ntype CommandName = \"browse\" | \"store\";\n\ntype ParsedArgs = {\n command: CommandName;\n options: Record<string, string[]>;\n flags: Set<string>;\n};\n\ntype Runtime = {\n config: PluginConfig;\n service: EngramAccessService;\n};\n\ntype UsageErrorKind =\n | \"unsupported-command\"\n | \"unexpected-positional\"\n | \"missing-option\"\n | \"missing-content\"\n | \"invalid-integer\"\n | \"invalid-number\";\n\nclass UsageError extends Error {\n constructor(\n readonly kind: UsageErrorKind,\n readonly optionName?: string,\n ) {\n super(\"invalid access-cli arguments\");\n }\n}\n\nfunction formatUsageError(error: UsageError): string {\n switch (error.kind) {\n case \"unsupported-command\":\n return \"unsupported command\";\n case \"unexpected-positional\":\n return \"unexpected positional argument\";\n case \"missing-option\":\n return `missing required option: --${error.optionName ?? \"unknown\"}`;\n case \"missing-content\":\n return \"missing required option: --content or --content-file\";\n case \"invalid-integer\":\n return `invalid integer for --${error.optionName ?? \"unknown\"}`;\n case \"invalid-number\":\n return `invalid number for --${error.optionName ?? \"unknown\"}`;\n }\n}\n\nfunction writeCliOutput(text: string = \"\"): void {\n process.stdout.write(`${text}\\n`);\n}\n\nfunction usage(): string {\n return [\n \"Usage:\",\n \" engram-access browse [options]\",\n \" engram-access store [options]\",\n \"\",\n \"Browse options:\",\n \" --namespace <name>\",\n \" --query <text>\",\n \" --category <name>\",\n \" --status <name>\",\n \" --sort <updated_desc|updated_asc|created_desc|created_asc>\",\n \" --limit <n>\",\n \" --offset <n>\",\n \"\",\n \"Store options:\",\n \" --namespace <name>\",\n \" --session-key <key>\",\n \" --principal <principal>\",\n \" --content <text> | --content-file <path>\",\n \" --category <name>\",\n \" --confidence <0-1>\",\n \" --tag <tag> (repeatable)\",\n \" --entity-ref <ref>\",\n \" --ttl <duration>\",\n \" --source-reason <text>\",\n \" --idempotency-key <key>\",\n \" --dry-run\",\n ].join(\"\\n\");\n}\n\nfunction parseArgs(argv: string[]): ParsedArgs {\n const [commandRaw, ...rest] = argv;\n if (commandRaw !== \"browse\" && commandRaw !== \"store\") {\n throw new UsageError(\"unsupported-command\");\n }\n\n const options: Record<string, string[]> = {};\n const flags = new Set<string>();\n\n for (let i = 0; i < rest.length; i += 1) {\n const token = rest[i];\n if (!token.startsWith(\"--\")) {\n throw new UsageError(\"unexpected-positional\");\n }\n const key = token.slice(2);\n const next = rest[i + 1];\n if (!next || next.startsWith(\"--\")) {\n flags.add(key);\n continue;\n }\n if (!options[key]) {\n options[key] = [];\n }\n options[key].push(next);\n i += 1;\n }\n\n return {\n command: commandRaw,\n options,\n flags,\n };\n}\n\nfunction getLastOption(args: ParsedArgs, name: string): string | undefined {\n const values = args.options[name];\n if (!values || values.length === 0) return undefined;\n return values[values.length - 1];\n}\n\nfunction getAllOptions(args: ParsedArgs, name: string): string[] {\n return args.options[name] ?? [];\n}\n\nfunction requireOption(args: ParsedArgs, name: string): string {\n const value = getLastOption(args, name);\n if (!value || value.trim().length === 0) {\n throw new UsageError(\"missing-option\", name);\n }\n return value;\n}\n\nfunction parseIntegerOption(args: ParsedArgs, name: string): number | undefined {\n const raw = getLastOption(args, name);\n if (!raw) return undefined;\n const value = parseInt(raw, 10);\n if (!Number.isFinite(value)) {\n throw new UsageError(\"invalid-integer\", name);\n }\n return value;\n}\n\nfunction parseFloatOption(args: ParsedArgs, name: string): number | undefined {\n const raw = getLastOption(args, name);\n if (!raw) return undefined;\n const value = Number.parseFloat(raw);\n if (!Number.isFinite(value)) {\n throw new UsageError(\"invalid-number\", name);\n }\n return value;\n}\n\nfunction loadPluginConfig(): Record<string, unknown> {\n const configPath =\n readEnvVar(\"OPENCLAW_ENGRAM_CONFIG_PATH\") ||\n readEnvVar(\"OPENCLAW_CONFIG_PATH\") ||\n path.join(resolveHomeDir(), \".openclaw\", \"openclaw.json\");\n const raw = JSON.parse(fs.readFileSync(configPath, \"utf8\"));\n return raw?.plugins?.entries?.[\"openclaw-engram\"]?.config ?? {};\n}\n\nfunction buildRuntime(): Runtime {\n const config = parseConfig(loadPluginConfig());\n return {\n config,\n service: new EngramAccessService(new Orchestrator(config)),\n };\n}\n\nasync function runBrowse(args: ParsedArgs): Promise<void> {\n const { service } = buildRuntime();\n const result = await service.memoryBrowse({\n namespace: getLastOption(args, \"namespace\"),\n query: getLastOption(args, \"query\"),\n category: getLastOption(args, \"category\"),\n status: getLastOption(args, \"status\"),\n sort: getLastOption(args, \"sort\") as \"updated_desc\" | \"updated_asc\" | \"created_desc\" | \"created_asc\" | undefined,\n limit: parseIntegerOption(args, \"limit\"),\n offset: parseIntegerOption(args, \"offset\"),\n });\n console.log(JSON.stringify(result, null, 2));\n}\n\nasync function runStore(args: ParsedArgs): Promise<void> {\n const { config, service } = buildRuntime();\n const contentFile = getLastOption(args, \"content-file\");\n const inlineContent = getLastOption(args, \"content\");\n const content = contentFile ? fs.readFileSync(contentFile, \"utf8\") : inlineContent;\n if (!content || content.trim().length === 0) {\n throw new UsageError(\"missing-content\");\n }\n\n const result = await service.memoryStore({\n namespace: getLastOption(args, \"namespace\"),\n sessionKey: getLastOption(args, \"session-key\"),\n authenticatedPrincipal: getLastOption(args, \"principal\") ?? config.agentAccessHttp.principal,\n content,\n category: requireOption(args, \"category\"),\n confidence: parseFloatOption(args, \"confidence\"),\n tags: getAllOptions(args, \"tag\"),\n entityRef: getLastOption(args, \"entity-ref\"),\n ttl: getLastOption(args, \"ttl\"),\n sourceReason: getLastOption(args, \"source-reason\"),\n idempotencyKey: getLastOption(args, \"idempotency-key\"),\n dryRun: args.flags.has(\"dry-run\"),\n });\n console.log(JSON.stringify(result, null, 2));\n}\n\nexport async function main(argv: string[] = process.argv.slice(2)): Promise<void> {\n const args = parseArgs(argv);\n if (args.command === \"browse\") {\n await runBrowse(args);\n return;\n }\n await runStore(args);\n}\n\nexport function printUsage(): void {\n writeCliOutput(usage());\n}\n\nexport async function runCli(argv: string[] = process.argv.slice(2)): Promise<void> {\n try {\n await main(argv);\n } catch (error) {\n if (error instanceof UsageError) {\n writeCliOutput(formatUsageError(error));\n writeCliOutput();\n printUsage();\n process.exit(1);\n }\n\n console.error(\"access-cli failed\");\n process.exit(1);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AA4BjB,IAAM,aAAN,cAAyB,MAAM;AAAA,EAC7B,YACW,MACA,YACT;AACA,UAAM,8BAA8B;AAH3B;AACA;AAAA,EAGX;AAAA,EAJW;AAAA,EACA;AAIb;AAEA,SAAS,iBAAiB,OAA2B;AACnD,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO,8BAA8B,MAAM,cAAc,SAAS;AAAA,IACpE,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO,yBAAyB,MAAM,cAAc,SAAS;AAAA,IAC/D,KAAK;AACH,aAAO,wBAAwB,MAAM,cAAc,SAAS;AAAA,EAChE;AACF;AAEA,SAAS,eAAe,OAAe,IAAU;AAC/C,UAAQ,OAAO,MAAM,GAAG,IAAI;AAAA,CAAI;AAClC;AAEA,SAAS,QAAgB;AACvB,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;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AAI,KAAK,YAAY,UAAU;AAC7B,UAAM,UAAU,IAAI;AACpB;AAAA,EACF;AACA,QAAM,SAAS,IAAI;AACrB;AAEO,SAAS,aAAmB;AACjC,iBAAe,MAAM,CAAC;AACxB;AAEA,eAAsB,OAAO,OAAiB,QAAQ,KAAK,MAAM,CAAC,GAAkB;AAClF,MAAI;AACF,UAAM,KAAK,IAAI;AAAA,EACjB,SAAS,OAAO;AACd,QAAI,iBAAiB,YAAY;AAC/B,qBAAe,iBAAiB,KAAK,CAAC;AACtC,qBAAe;AACf,iBAAW;AACX,cAAQ,KAAK,CAAC;AAAA,IAChB;AAEA,YAAQ,MAAM,mBAAmB;AACjC,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF;","names":[]}
|
package/dist/access-schema.d.ts
CHANGED
|
@@ -90,12 +90,12 @@ declare const memoryStoreRequestSchema: z.ZodObject<{
|
|
|
90
90
|
namespace?: string | undefined;
|
|
91
91
|
category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | undefined;
|
|
92
92
|
confidence?: number | undefined;
|
|
93
|
-
ttl?: string | undefined;
|
|
94
93
|
tags?: string[] | undefined;
|
|
95
94
|
entityRef?: string | undefined;
|
|
96
95
|
schemaVersion?: number | undefined;
|
|
97
96
|
sessionKey?: string | undefined;
|
|
98
97
|
dryRun?: boolean | undefined;
|
|
98
|
+
ttl?: string | undefined;
|
|
99
99
|
sourceReason?: string | undefined;
|
|
100
100
|
idempotencyKey?: string | undefined;
|
|
101
101
|
}, {
|
|
@@ -103,12 +103,12 @@ declare const memoryStoreRequestSchema: z.ZodObject<{
|
|
|
103
103
|
namespace?: string | undefined;
|
|
104
104
|
category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | undefined;
|
|
105
105
|
confidence?: number | undefined;
|
|
106
|
-
ttl?: string | undefined;
|
|
107
106
|
tags?: string[] | undefined;
|
|
108
107
|
entityRef?: string | undefined;
|
|
109
108
|
schemaVersion?: number | undefined;
|
|
110
109
|
sessionKey?: string | undefined;
|
|
111
110
|
dryRun?: boolean | undefined;
|
|
111
|
+
ttl?: string | undefined;
|
|
112
112
|
sourceReason?: string | undefined;
|
|
113
113
|
idempotencyKey?: string | undefined;
|
|
114
114
|
}>;
|
|
@@ -130,12 +130,12 @@ declare const suggestionSubmitRequestSchema: z.ZodObject<{
|
|
|
130
130
|
namespace?: string | undefined;
|
|
131
131
|
category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | undefined;
|
|
132
132
|
confidence?: number | undefined;
|
|
133
|
-
ttl?: string | undefined;
|
|
134
133
|
tags?: string[] | undefined;
|
|
135
134
|
entityRef?: string | undefined;
|
|
136
135
|
schemaVersion?: number | undefined;
|
|
137
136
|
sessionKey?: string | undefined;
|
|
138
137
|
dryRun?: boolean | undefined;
|
|
138
|
+
ttl?: string | undefined;
|
|
139
139
|
sourceReason?: string | undefined;
|
|
140
140
|
idempotencyKey?: string | undefined;
|
|
141
141
|
}, {
|
|
@@ -143,12 +143,12 @@ declare const suggestionSubmitRequestSchema: z.ZodObject<{
|
|
|
143
143
|
namespace?: string | undefined;
|
|
144
144
|
category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | undefined;
|
|
145
145
|
confidence?: number | undefined;
|
|
146
|
-
ttl?: string | undefined;
|
|
147
146
|
tags?: string[] | undefined;
|
|
148
147
|
entityRef?: string | undefined;
|
|
149
148
|
schemaVersion?: number | undefined;
|
|
150
149
|
sessionKey?: string | undefined;
|
|
151
150
|
dryRun?: boolean | undefined;
|
|
151
|
+
ttl?: string | undefined;
|
|
152
152
|
sourceReason?: string | undefined;
|
|
153
153
|
idempotencyKey?: string | undefined;
|
|
154
154
|
}>;
|
|
@@ -217,13 +217,13 @@ declare const lcmSearchRequestSchema: z.ZodObject<{
|
|
|
217
217
|
}, "strip", z.ZodTypeAny, {
|
|
218
218
|
query: string;
|
|
219
219
|
namespace?: string | undefined;
|
|
220
|
-
limit?: number | undefined;
|
|
221
220
|
sessionKey?: string | undefined;
|
|
221
|
+
limit?: number | undefined;
|
|
222
222
|
}, {
|
|
223
223
|
query: string;
|
|
224
224
|
namespace?: string | undefined;
|
|
225
|
-
limit?: number | undefined;
|
|
226
225
|
sessionKey?: string | undefined;
|
|
226
|
+
limit?: number | undefined;
|
|
227
227
|
}>;
|
|
228
228
|
declare const daySummaryRequestSchema: z.ZodObject<{
|
|
229
229
|
memories: z.ZodOptional<z.ZodString>;
|
package/dist/calibration.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
FallbackLlmClient
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
3
|
+
} from "./chunk-XUHI52HK.js";
|
|
4
|
+
import "./chunk-M5ZBBBJI.js";
|
|
5
|
+
import "./chunk-OOSWAUYB.js";
|
|
5
6
|
import "./chunk-Y27UJK6V.js";
|
|
6
7
|
import "./chunk-UZB5KHKX.js";
|
|
7
8
|
import "./chunk-MARWOCVP.js";
|
package/dist/calibration.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/calibration.ts"],"sourcesContent":["/**\n * calibration.ts — Prediction-Error-Driven Model-User Calibration\n *\n * Analyzes patterns in user corrections to identify systematic miscalibration\n * between the model's predictions and the user's actual expectations.\n * During consolidation, replays chains of similar corrections through an LLM\n * to synthesize CalibrationRules that adjust model behavior for this specific user.\n *\n * Inspired by:\n * - Cerebellar motor calibration (prediction errors drive lasting adjustments)\n * - Temporal difference learning (dopamine signals prediction error)\n * - Tesla FSD shadow mode (divergence between prediction and reality = training signal)\n */\n\nimport { createHash } from \"node:crypto\";\nimport path from \"node:path\";\nimport { mkdir, readFile, writeFile } from \"node:fs/promises\";\nimport { FallbackLlmClient } from \"./fallback-llm.js\";\nimport type { GatewayConfig, MemoryFile } from \"./types.js\";\nimport { listJsonFiles, readJsonFile } from \"./json-store.js\";\nimport { isRecord } from \"./store-contract.js\";\nimport { log } from \"./logger.js\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface CalibrationRule {\n id: string;\n ruleType: \"model_tendency\" | \"user_expectation\" | \"scope_boundary\" | \"verification_required\";\n condition: string;\n modelTendency: string;\n userExpectation: string;\n calibration: string;\n confidence: number;\n evidenceCount: number;\n evidenceCorrectionIds: string[];\n createdAt: string;\n lastReinforcedAt: string;\n}\n\nexport interface CalibrationIndex {\n rules: CalibrationRule[];\n updatedAt: string;\n totalCorrectionsAnalyzed: number;\n}\n\n// ─── Storage ─────────────────────────────────────────────────────────────────\n\nfunction calibrationDir(memoryDir: string): string {\n return path.join(memoryDir, \"state\", \"calibration\");\n}\n\nfunction calibrationIndexPath(memoryDir: string): string {\n return path.join(calibrationDir(memoryDir), \"calibration-index.json\");\n}\n\nexport async function readCalibrationIndex(memoryDir: string): Promise<CalibrationIndex> {\n try {\n const raw = JSON.parse(await readFile(calibrationIndexPath(memoryDir), \"utf8\"));\n return {\n rules: Array.isArray(raw.rules) ? raw.rules : [],\n updatedAt: typeof raw.updatedAt === \"string\" ? raw.updatedAt : new Date().toISOString(),\n totalCorrectionsAnalyzed: typeof raw.totalCorrectionsAnalyzed === \"number\" ? raw.totalCorrectionsAnalyzed : 0,\n };\n } catch {\n return { rules: [], updatedAt: new Date().toISOString(), totalCorrectionsAnalyzed: 0 };\n }\n}\n\nasync function writeCalibrationIndex(memoryDir: string, index: CalibrationIndex): Promise<void> {\n const dir = calibrationDir(memoryDir);\n await mkdir(dir, { recursive: true });\n index.updatedAt = new Date().toISOString();\n await writeFile(calibrationIndexPath(memoryDir), JSON.stringify(index, null, 2), \"utf8\");\n}\n\n// ─── Correction Reading ──────────────────────────────────────────────────────\n\ninterface CorrectionMemory {\n id: string;\n content: string;\n created: string;\n confidence: number;\n entityRefs: string[];\n tags: string[];\n}\n\nasync function readCorrections(memoryDir: string): Promise<CorrectionMemory[]> {\n const correctionsDir = path.join(memoryDir, \"corrections\");\n const files = await listJsonFiles(correctionsDir).catch(() => {\n // Corrections might be in facts/ directories too\n return [] as string[];\n });\n\n // Also scan facts directories for correction-category files\n const factsDir = path.join(memoryDir, \"facts\");\n try {\n const { readdir } = await import(\"node:fs/promises\");\n const dayDirs = (await readdir(factsDir)).filter((d: string) => /^\\d{4}-\\d{2}-\\d{2}$/.test(d));\n for (const day of dayDirs) {\n const dayPath = path.join(factsDir, day);\n const dayFiles = (await readdir(dayPath))\n .filter((f: string) => f.startsWith(\"correction-\") && f.endsWith(\".md\"))\n .map((f: string) => path.join(dayPath, f));\n files.push(...dayFiles);\n }\n } catch {\n // facts dir might not exist\n }\n\n // Also check the dedicated corrections directory\n try {\n const { readdir } = await import(\"node:fs/promises\");\n const corrFiles = (await readdir(correctionsDir))\n .filter((f: string) => f.endsWith(\".md\"))\n .map((f: string) => path.join(correctionsDir, f));\n files.push(...corrFiles);\n } catch {\n // corrections dir might not exist\n }\n\n const corrections: CorrectionMemory[] = [];\n const seen = new Set<string>();\n\n for (const filePath of files) {\n try {\n const raw = await readFile(filePath, \"utf8\");\n\n // Parse frontmatter\n const fmMatch = raw.match(/^---\\n([\\s\\S]*?)\\n---\\n([\\s\\S]*)$/);\n if (!fmMatch) continue;\n\n const content = fmMatch[2].trim();\n if (!content || content.length < 10) continue;\n\n // Extract id from frontmatter\n const idMatch = fmMatch[1].match(/^id:\\s*(.+)$/m);\n const id = idMatch?.[1]?.trim() ?? path.basename(filePath, \".md\");\n\n if (seen.has(id)) continue;\n seen.add(id);\n\n const confMatch = fmMatch[1].match(/^confidence:\\s*(.+)$/m);\n const confidence = confMatch ? parseFloat(confMatch[1]) : 0.9;\n\n const entityMatch = fmMatch[1].match(/^entityRef:\\s*(.+)$/m);\n const entityRefs = entityMatch ? [entityMatch[1].trim()] : [];\n\n corrections.push({ id, content, created: \"\", confidence, entityRefs, tags: [] });\n } catch {\n // skip unparseable files\n }\n }\n\n return corrections;\n}\n\n// ─── LLM-Assisted Clustering and Replay ──────────────────────────────────────\n\nconst CLUSTER_PROMPT = `You are analyzing user corrections to an AI assistant. Each correction represents a moment where the assistant's prediction of what the user wanted was WRONG.\n\nYour job: Group these corrections into clusters where the SAME TYPE of misunderstanding is happening. Then for each cluster, synthesize a CalibrationRule.\n\nA CalibrationRule describes:\n- condition: When does this type of mistake happen?\n- modelTendency: What does the model tend to assume or do wrong?\n- userExpectation: What does the user actually want instead?\n- calibration: How should the model adjust its behavior?\n- ruleType: One of \"model_tendency\", \"user_expectation\", \"scope_boundary\", \"verification_required\"\n\nFocus on PATTERNS, not individual corrections. A cluster needs at least 2 corrections to be worth a rule.\n\nOutput valid JSON only:\n{\n \"rules\": [\n {\n \"ruleType\": \"model_tendency\",\n \"condition\": \"When discussing project scope or task boundaries\",\n \"modelTendency\": \"The model tends to assume broader scope than the user intends\",\n \"userExpectation\": \"The user prefers narrow, specific task definitions and wants to be asked before scope expansion\",\n \"calibration\": \"When uncertain about scope, ask for clarification rather than assuming. Default to the narrower interpretation.\",\n \"confidence\": 0.85,\n \"evidenceIds\": [\"correction-id-1\", \"correction-id-2\"]\n }\n ]\n}`;\n\nexport async function synthesizeCalibrationRules(\n corrections: CorrectionMemory[],\n llm: FallbackLlmClient,\n existingRules: CalibrationRule[],\n agentId?: string,\n): Promise<CalibrationRule[]> {\n if (corrections.length < 2) return [];\n\n // Format corrections for the LLM\n const correctionText = corrections\n .slice(0, 50) // limit to avoid huge prompts\n .map((c, i) => `[${c.id}] ${c.content}`)\n .join(\"\\n\\n\");\n\n const existingRulesText = existingRules.length > 0\n ? `\\n\\nExisting calibration rules (avoid duplicating these):\\n${existingRules.map((r) => `- ${r.condition}: ${r.calibration}`).join(\"\\n\")}`\n : \"\";\n\n const response = await llm.chatCompletion(\n [\n { role: \"system\", content: CLUSTER_PROMPT },\n { role: \"user\", content: `Here are ${corrections.length} corrections from this user:\\n\\n${correctionText}${existingRulesText}` },\n ],\n { temperature: 0.3, maxTokens: 3000, agentId },\n );\n\n if (!response?.content) return [];\n\n try {\n let jsonStr = response.content.trim();\n const fenceMatch = jsonStr.match(/```(?:json)?\\s*\\n?([\\s\\S]*?)\\n?\\s*```/);\n if (fenceMatch) jsonStr = fenceMatch[1];\n\n const parsed = JSON.parse(jsonStr);\n if (!Array.isArray(parsed.rules)) return [];\n\n const now = new Date().toISOString();\n return parsed.rules\n .filter((r: any) => r.condition && r.calibration && r.modelTendency)\n .map((r: any) => ({\n id: `cal-${createHash(\"sha256\").update(r.condition + r.calibration).digest(\"hex\").slice(0, 12)}`,\n ruleType: r.ruleType ?? \"model_tendency\",\n condition: String(r.condition),\n modelTendency: String(r.modelTendency),\n userExpectation: String(r.userExpectation ?? \"\"),\n calibration: String(r.calibration),\n confidence: typeof r.confidence === \"number\" ? r.confidence : 0.7,\n evidenceCount: Array.isArray(r.evidenceIds) ? r.evidenceIds.length : 1,\n evidenceCorrectionIds: Array.isArray(r.evidenceIds) ? r.evidenceIds : [],\n createdAt: now,\n lastReinforcedAt: now,\n }));\n } catch {\n log.warn(\"[calibration] failed to parse LLM response\");\n return [];\n }\n}\n\n// ─── Recall Section ──────────────────────────────────────────────────────────\n\n/**\n * Build a recall section from calibration rules relevant to the current query.\n * Uses the LLM to select which rules apply to the current context.\n */\nexport function buildCalibrationRecallSection(\n rules: CalibrationRule[],\n query: string,\n maxChars: number = 1200,\n): string | null {\n if (rules.length === 0) return null;\n\n // Simple relevance: include all rules (they're already filtered to this user)\n // In production, could use embedding similarity to filter\n const lines: string[] = [\n \"## Model Calibration (learned from past corrections)\",\n \"\",\n \"Adjustments for this specific user, learned from patterns in their corrections:\",\n \"\",\n ];\n\n let totalChars = lines.join(\"\\n\").length;\n\n for (const rule of rules) {\n const line = `- **${rule.condition}**: ${rule.modelTendency} → Instead: ${rule.calibration}`;\n if (totalChars + line.length + 1 > maxChars) break;\n lines.push(line);\n totalChars += line.length + 1;\n }\n\n if (lines.length <= 4) return null;\n lines.push(\"\");\n return lines.join(\"\\n\");\n}\n\n// ─── Public API ──────────────────────────────────────────────────────────────\n\n/**\n * Run the full calibration pipeline:\n * 1. Read all corrections\n * 2. Send to LLM for clustering and rule synthesis\n * 3. Merge with existing rules\n * 4. Write updated index\n */\nexport async function runCalibrationConsolidation(options: {\n memoryDir: string;\n gatewayConfig?: GatewayConfig;\n gatewayAgentId?: string;\n}): Promise<CalibrationRule[]> {\n try {\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable(options.gatewayAgentId)) {\n log.debug(\"[calibration] no LLM available — skipping consolidation\");\n return [];\n }\n\n const corrections = await readCorrections(options.memoryDir);\n if (corrections.length < 3) {\n log.debug(`[calibration] only ${corrections.length} corrections — need at least 3`);\n return [];\n }\n\n const existingIndex = await readCalibrationIndex(options.memoryDir);\n\n const newRules = await synthesizeCalibrationRules(corrections, llm, existingIndex.rules, options.gatewayAgentId);\n if (newRules.length === 0) {\n log.debug(\"[calibration] no new calibration rules synthesized\");\n return existingIndex.rules;\n }\n\n // Merge: keep existing rules, add new ones (deduplicate by id)\n const ruleMap = new Map(existingIndex.rules.map((r) => [r.id, r]));\n for (const rule of newRules) {\n if (ruleMap.has(rule.id)) {\n // Reinforce existing rule\n const existing = ruleMap.get(rule.id)!;\n existing.lastReinforcedAt = new Date().toISOString();\n existing.evidenceCount += rule.evidenceCount;\n existing.confidence = Math.min(1, existing.confidence + 0.05);\n } else {\n ruleMap.set(rule.id, rule);\n }\n }\n\n const allRules = [...ruleMap.values()];\n await writeCalibrationIndex(options.memoryDir, {\n rules: allRules,\n updatedAt: new Date().toISOString(),\n totalCorrectionsAnalyzed: corrections.length,\n });\n\n log.debug(`[calibration] synthesized ${newRules.length} new rule(s), ${allRules.length} total`);\n return allRules;\n } catch (error) {\n log.warn(`[calibration] consolidation failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return [];\n }\n}\n\n/**\n * Standalone entry point for calibration consolidation that can be called\n * independently of weekly compounding. The compounding engine's\n * `synthesizeWeekly()` is one trigger, but orchestrators or periodic\n * maintenance jobs should call this directly so calibration is not gated\n * on weekly compounding being enabled.\n */\nexport async function runCalibrationIfEnabled(options: {\n memoryDir: string;\n calibrationEnabled: boolean;\n gatewayConfig?: GatewayConfig;\n}): Promise<CalibrationRule[]> {\n if (!options.calibrationEnabled) {\n return [];\n }\n return runCalibrationConsolidation({\n memoryDir: options.memoryDir,\n gatewayConfig: options.gatewayConfig,\n });\n}\n\n/**\n * Get calibration rules for recall injection.\n * Reads the pre-computed calibration index.\n */\nexport async function getCalibrationRulesForRecall(\n memoryDir: string,\n): Promise<CalibrationRule[]> {\n const index = await readCalibrationIndex(memoryDir);\n return index.rules;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,SAAS,kBAAkB;AAC3B,OAAO,UAAU;AACjB,SAAS,OAAO,UAAU,iBAAiB;AA+B3C,SAAS,eAAe,WAA2B;AACjD,SAAO,KAAK,KAAK,WAAW,SAAS,aAAa;AACpD;AAEA,SAAS,qBAAqB,WAA2B;AACvD,SAAO,KAAK,KAAK,eAAe,SAAS,GAAG,wBAAwB;AACtE;AAEA,eAAsB,qBAAqB,WAA8C;AACvF,MAAI;AACF,UAAM,MAAM,KAAK,MAAM,MAAM,SAAS,qBAAqB,SAAS,GAAG,MAAM,CAAC;AAC9E,WAAO;AAAA,MACL,OAAO,MAAM,QAAQ,IAAI,KAAK,IAAI,IAAI,QAAQ,CAAC;AAAA,MAC/C,WAAW,OAAO,IAAI,cAAc,WAAW,IAAI,aAAY,oBAAI,KAAK,GAAE,YAAY;AAAA,MACtF,0BAA0B,OAAO,IAAI,6BAA6B,WAAW,IAAI,2BAA2B;AAAA,IAC9G;AAAA,EACF,QAAQ;AACN,WAAO,EAAE,OAAO,CAAC,GAAG,YAAW,oBAAI,KAAK,GAAE,YAAY,GAAG,0BAA0B,EAAE;AAAA,EACvF;AACF;AAEA,eAAe,sBAAsB,WAAmB,OAAwC;AAC9F,QAAM,MAAM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I,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;AACjE,eAAW,QAAQ,UAAU;AAC3B,UAAI,QAAQ,IAAI,KAAK,EAAE,GAAG;AAExB,cAAM,WAAW,QAAQ,IAAI,KAAK,EAAE;AACpC,iBAAS,oBAAmB,oBAAI,KAAK,GAAE,YAAY;AACnD,iBAAS,iBAAiB,KAAK;AAC/B,iBAAS,aAAa,KAAK,IAAI,GAAG,SAAS,aAAa,IAAI;AAAA,MAC9D,OAAO;AACL,gBAAQ,IAAI,KAAK,IAAI,IAAI;AAAA,MAC3B;AAAA,IACF;AAEA,UAAM,WAAW,CAAC,GAAG,QAAQ,OAAO,CAAC;AACrC,UAAM,sBAAsB,QAAQ,WAAW;AAAA,MAC7C,OAAO;AAAA,MACP,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,MAClC,0BAA0B,YAAY;AAAA,IACxC,CAAC;AAED,QAAI,MAAM,6BAA6B,SAAS,MAAM,iBAAiB,SAAS,MAAM,QAAQ;AAC9F,WAAO;AAAA,EACT,SAAS,OAAO;AACd,QAAI,KAAK,mDAAmD,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC,EAAE;AACpH,WAAO,CAAC;AAAA,EACV;AACF;AASA,eAAsB,wBAAwB,SAIf;AAC7B,MAAI,CAAC,QAAQ,oBAAoB;AAC/B,WAAO,CAAC;AAAA,EACV;AACA,SAAO,4BAA4B;AAAA,IACjC,WAAW,QAAQ;AAAA,IACnB,eAAe,QAAQ;AAAA,EACzB,CAAC;AACH;AAMA,eAAsB,6BACpB,WAC4B;AAC5B,QAAM,QAAQ,MAAM,qBAAqB,SAAS;AAClD,SAAO,MAAM;AACf;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/calibration.ts"],"sourcesContent":["/**\n * calibration.ts — Prediction-Error-Driven Model-User Calibration\n *\n * Analyzes patterns in user corrections to identify systematic miscalibration\n * between the model's predictions and the user's actual expectations.\n * During consolidation, replays chains of similar corrections through an LLM\n * to synthesize CalibrationRules that adjust model behavior for this specific user.\n *\n * Inspired by:\n * - Cerebellar motor calibration (prediction errors drive lasting adjustments)\n * - Temporal difference learning (dopamine signals prediction error)\n * - Tesla FSD shadow mode (divergence between prediction and reality = training signal)\n */\n\nimport { createHash } from \"node:crypto\";\nimport path from \"node:path\";\nimport { mkdir, readFile, writeFile } from \"node:fs/promises\";\nimport { FallbackLlmClient } from \"./fallback-llm.js\";\nimport type { GatewayConfig, MemoryFile } from \"./types.js\";\nimport { listJsonFiles, readJsonFile } from \"./json-store.js\";\nimport { isRecord } from \"./store-contract.js\";\nimport { log } from \"./logger.js\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface CalibrationRule {\n id: string;\n ruleType: \"model_tendency\" | \"user_expectation\" | \"scope_boundary\" | \"verification_required\";\n condition: string;\n modelTendency: string;\n userExpectation: string;\n calibration: string;\n confidence: number;\n evidenceCount: number;\n evidenceCorrectionIds: string[];\n createdAt: string;\n lastReinforcedAt: string;\n}\n\nexport interface CalibrationIndex {\n rules: CalibrationRule[];\n updatedAt: string;\n totalCorrectionsAnalyzed: number;\n}\n\n// ─── Storage ─────────────────────────────────────────────────────────────────\n\nfunction calibrationDir(memoryDir: string): string {\n return path.join(memoryDir, \"state\", \"calibration\");\n}\n\nfunction calibrationIndexPath(memoryDir: string): string {\n return path.join(calibrationDir(memoryDir), \"calibration-index.json\");\n}\n\nexport async function readCalibrationIndex(memoryDir: string): Promise<CalibrationIndex> {\n try {\n const raw = JSON.parse(await readFile(calibrationIndexPath(memoryDir), \"utf8\"));\n return {\n rules: Array.isArray(raw.rules) ? raw.rules : [],\n updatedAt: typeof raw.updatedAt === \"string\" ? raw.updatedAt : new Date().toISOString(),\n totalCorrectionsAnalyzed: typeof raw.totalCorrectionsAnalyzed === \"number\" ? raw.totalCorrectionsAnalyzed : 0,\n };\n } catch {\n return { rules: [], updatedAt: new Date().toISOString(), totalCorrectionsAnalyzed: 0 };\n }\n}\n\nasync function writeCalibrationIndex(memoryDir: string, index: CalibrationIndex): Promise<void> {\n const dir = calibrationDir(memoryDir);\n await mkdir(dir, { recursive: true });\n index.updatedAt = new Date().toISOString();\n await writeFile(calibrationIndexPath(memoryDir), JSON.stringify(index, null, 2), \"utf8\");\n}\n\n// ─── Correction Reading ──────────────────────────────────────────────────────\n\ninterface CorrectionMemory {\n id: string;\n content: string;\n created: string;\n confidence: number;\n entityRefs: string[];\n tags: string[];\n}\n\nasync function readCorrections(memoryDir: string): Promise<CorrectionMemory[]> {\n const correctionsDir = path.join(memoryDir, \"corrections\");\n const files = await listJsonFiles(correctionsDir).catch(() => {\n // Corrections might be in facts/ directories too\n return [] as string[];\n });\n\n // Also scan facts directories for correction-category files\n const factsDir = path.join(memoryDir, \"facts\");\n try {\n const { readdir } = await import(\"node:fs/promises\");\n const dayDirs = (await readdir(factsDir)).filter((d: string) => /^\\d{4}-\\d{2}-\\d{2}$/.test(d));\n for (const day of dayDirs) {\n const dayPath = path.join(factsDir, day);\n const dayFiles = (await readdir(dayPath))\n .filter((f: string) => f.startsWith(\"correction-\") && f.endsWith(\".md\"))\n .map((f: string) => path.join(dayPath, f));\n files.push(...dayFiles);\n }\n } catch {\n // facts dir might not exist\n }\n\n // Also check the dedicated corrections directory\n try {\n const { readdir } = await import(\"node:fs/promises\");\n const corrFiles = (await readdir(correctionsDir))\n .filter((f: string) => f.endsWith(\".md\"))\n .map((f: string) => path.join(correctionsDir, f));\n files.push(...corrFiles);\n } catch {\n // corrections dir might not exist\n }\n\n const corrections: CorrectionMemory[] = [];\n const seen = new Set<string>();\n\n for (const filePath of files) {\n try {\n const raw = await readFile(filePath, \"utf8\");\n\n // Parse frontmatter\n const fmMatch = raw.match(/^---\\n([\\s\\S]*?)\\n---\\n([\\s\\S]*)$/);\n if (!fmMatch) continue;\n\n const content = fmMatch[2].trim();\n if (!content || content.length < 10) continue;\n\n // Extract id from frontmatter\n const idMatch = fmMatch[1].match(/^id:\\s*(.+)$/m);\n const id = idMatch?.[1]?.trim() ?? path.basename(filePath, \".md\");\n\n if (seen.has(id)) continue;\n seen.add(id);\n\n const confMatch = fmMatch[1].match(/^confidence:\\s*(.+)$/m);\n const confidence = confMatch ? parseFloat(confMatch[1]) : 0.9;\n\n const entityMatch = fmMatch[1].match(/^entityRef:\\s*(.+)$/m);\n const entityRefs = entityMatch ? [entityMatch[1].trim()] : [];\n\n corrections.push({ id, content, created: \"\", confidence, entityRefs, tags: [] });\n } catch {\n // skip unparseable files\n }\n }\n\n return corrections;\n}\n\n// ─── LLM-Assisted Clustering and Replay ──────────────────────────────────────\n\nconst CLUSTER_PROMPT = `You are analyzing user corrections to an AI assistant. Each correction represents a moment where the assistant's prediction of what the user wanted was WRONG.\n\nYour job: Group these corrections into clusters where the SAME TYPE of misunderstanding is happening. Then for each cluster, synthesize a CalibrationRule.\n\nA CalibrationRule describes:\n- condition: When does this type of mistake happen?\n- modelTendency: What does the model tend to assume or do wrong?\n- userExpectation: What does the user actually want instead?\n- calibration: How should the model adjust its behavior?\n- ruleType: One of \"model_tendency\", \"user_expectation\", \"scope_boundary\", \"verification_required\"\n\nFocus on PATTERNS, not individual corrections. A cluster needs at least 2 corrections to be worth a rule.\n\nOutput valid JSON only:\n{\n \"rules\": [\n {\n \"ruleType\": \"model_tendency\",\n \"condition\": \"When discussing project scope or task boundaries\",\n \"modelTendency\": \"The model tends to assume broader scope than the user intends\",\n \"userExpectation\": \"The user prefers narrow, specific task definitions and wants to be asked before scope expansion\",\n \"calibration\": \"When uncertain about scope, ask for clarification rather than assuming. Default to the narrower interpretation.\",\n \"confidence\": 0.85,\n \"evidenceIds\": [\"correction-id-1\", \"correction-id-2\"]\n }\n ]\n}`;\n\nexport async function synthesizeCalibrationRules(\n corrections: CorrectionMemory[],\n llm: FallbackLlmClient,\n existingRules: CalibrationRule[],\n agentId?: string,\n): Promise<CalibrationRule[]> {\n if (corrections.length < 2) return [];\n\n // Format corrections for the LLM\n const correctionText = corrections\n .slice(0, 50) // limit to avoid huge prompts\n .map((c, i) => `[${c.id}] ${c.content}`)\n .join(\"\\n\\n\");\n\n const existingRulesText = existingRules.length > 0\n ? `\\n\\nExisting calibration rules (avoid duplicating these):\\n${existingRules.map((r) => `- ${r.condition}: ${r.calibration}`).join(\"\\n\")}`\n : \"\";\n\n const response = await llm.chatCompletion(\n [\n { role: \"system\", content: CLUSTER_PROMPT },\n { role: \"user\", content: `Here are ${corrections.length} corrections from this user:\\n\\n${correctionText}${existingRulesText}` },\n ],\n { temperature: 0.3, maxTokens: 3000, agentId },\n );\n\n if (!response?.content) return [];\n\n try {\n let jsonStr = response.content.trim();\n const fenceMatch = jsonStr.match(/```(?:json)?\\s*\\n?([\\s\\S]*?)\\n?\\s*```/);\n if (fenceMatch) jsonStr = fenceMatch[1];\n\n const parsed = JSON.parse(jsonStr);\n if (!Array.isArray(parsed.rules)) return [];\n\n const now = new Date().toISOString();\n return parsed.rules\n .filter((r: any) => r.condition && r.calibration && r.modelTendency)\n .map((r: any) => ({\n id: `cal-${createHash(\"sha256\").update(r.condition + r.calibration).digest(\"hex\").slice(0, 12)}`,\n ruleType: r.ruleType ?? \"model_tendency\",\n condition: String(r.condition),\n modelTendency: String(r.modelTendency),\n userExpectation: String(r.userExpectation ?? \"\"),\n calibration: String(r.calibration),\n confidence: typeof r.confidence === \"number\" ? r.confidence : 0.7,\n evidenceCount: Array.isArray(r.evidenceIds) ? r.evidenceIds.length : 1,\n evidenceCorrectionIds: Array.isArray(r.evidenceIds) ? r.evidenceIds : [],\n createdAt: now,\n lastReinforcedAt: now,\n }));\n } catch {\n log.warn(\"[calibration] failed to parse LLM response\");\n return [];\n }\n}\n\n// ─── Recall Section ──────────────────────────────────────────────────────────\n\n/**\n * Build a recall section from calibration rules relevant to the current query.\n * Uses the LLM to select which rules apply to the current context.\n */\nexport function buildCalibrationRecallSection(\n rules: CalibrationRule[],\n query: string,\n maxChars: number = 1200,\n): string | null {\n if (rules.length === 0) return null;\n\n // Simple relevance: include all rules (they're already filtered to this user)\n // In production, could use embedding similarity to filter\n const lines: string[] = [\n \"## Model Calibration (learned from past corrections)\",\n \"\",\n \"Adjustments for this specific user, learned from patterns in their corrections:\",\n \"\",\n ];\n\n let totalChars = lines.join(\"\\n\").length;\n\n for (const rule of rules) {\n const line = `- **${rule.condition}**: ${rule.modelTendency} → Instead: ${rule.calibration}`;\n if (totalChars + line.length + 1 > maxChars) break;\n lines.push(line);\n totalChars += line.length + 1;\n }\n\n if (lines.length <= 4) return null;\n lines.push(\"\");\n return lines.join(\"\\n\");\n}\n\n// ─── Public API ──────────────────────────────────────────────────────────────\n\n/**\n * Run the full calibration pipeline:\n * 1. Read all corrections\n * 2. Send to LLM for clustering and rule synthesis\n * 3. Merge with existing rules\n * 4. Write updated index\n */\nexport async function runCalibrationConsolidation(options: {\n memoryDir: string;\n gatewayConfig?: GatewayConfig;\n gatewayAgentId?: string;\n}): Promise<CalibrationRule[]> {\n try {\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable(options.gatewayAgentId)) {\n log.debug(\"[calibration] no LLM available — skipping consolidation\");\n return [];\n }\n\n const corrections = await readCorrections(options.memoryDir);\n if (corrections.length < 3) {\n log.debug(`[calibration] only ${corrections.length} corrections — need at least 3`);\n return [];\n }\n\n const existingIndex = await readCalibrationIndex(options.memoryDir);\n\n const newRules = await synthesizeCalibrationRules(corrections, llm, existingIndex.rules, options.gatewayAgentId);\n if (newRules.length === 0) {\n log.debug(\"[calibration] no new calibration rules synthesized\");\n return existingIndex.rules;\n }\n\n // Merge: keep existing rules, add new ones (deduplicate by id)\n const ruleMap = new Map(existingIndex.rules.map((r) => [r.id, r]));\n for (const rule of newRules) {\n if (ruleMap.has(rule.id)) {\n // Reinforce existing rule\n const existing = ruleMap.get(rule.id)!;\n existing.lastReinforcedAt = new Date().toISOString();\n existing.evidenceCount += rule.evidenceCount;\n existing.confidence = Math.min(1, existing.confidence + 0.05);\n } else {\n ruleMap.set(rule.id, rule);\n }\n }\n\n const allRules = [...ruleMap.values()];\n await writeCalibrationIndex(options.memoryDir, {\n rules: allRules,\n updatedAt: new Date().toISOString(),\n totalCorrectionsAnalyzed: corrections.length,\n });\n\n log.debug(`[calibration] synthesized ${newRules.length} new rule(s), ${allRules.length} total`);\n return allRules;\n } catch (error) {\n log.warn(`[calibration] consolidation failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return [];\n }\n}\n\n/**\n * Standalone entry point for calibration consolidation that can be called\n * independently of weekly compounding. The compounding engine's\n * `synthesizeWeekly()` is one trigger, but orchestrators or periodic\n * maintenance jobs should call this directly so calibration is not gated\n * on weekly compounding being enabled.\n */\nexport async function runCalibrationIfEnabled(options: {\n memoryDir: string;\n calibrationEnabled: boolean;\n gatewayConfig?: GatewayConfig;\n}): Promise<CalibrationRule[]> {\n if (!options.calibrationEnabled) {\n return [];\n }\n return runCalibrationConsolidation({\n memoryDir: options.memoryDir,\n gatewayConfig: options.gatewayConfig,\n });\n}\n\n/**\n * Get calibration rules for recall injection.\n * Reads the pre-computed calibration index.\n */\nexport async function getCalibrationRulesForRecall(\n memoryDir: string,\n): Promise<CalibrationRule[]> {\n const index = await readCalibrationIndex(memoryDir);\n return index.rules;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAcA,SAAS,kBAAkB;AAC3B,OAAO,UAAU;AACjB,SAAS,OAAO,UAAU,iBAAiB;AA+B3C,SAAS,eAAe,WAA2B;AACjD,SAAO,KAAK,KAAK,WAAW,SAAS,aAAa;AACpD;AAEA,SAAS,qBAAqB,WAA2B;AACvD,SAAO,KAAK,KAAK,eAAe,SAAS,GAAG,wBAAwB;AACtE;AAEA,eAAsB,qBAAqB,WAA8C;AACvF,MAAI;AACF,UAAM,MAAM,KAAK,MAAM,MAAM,SAAS,qBAAqB,SAAS,GAAG,MAAM,CAAC;AAC9E,WAAO;AAAA,MACL,OAAO,MAAM,QAAQ,IAAI,KAAK,IAAI,IAAI,QAAQ,CAAC;AAAA,MAC/C,WAAW,OAAO,IAAI,cAAc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AAA,EACzB,CAAC;AACH;AAMA,eAAsB,6BACpB,WAC4B;AAC5B,QAAM,QAAQ,MAAM,qBAAqB,SAAS;AAClD,SAAO,MAAM;AACf;","names":[]}
|
|
@@ -4,8 +4,9 @@ import {
|
|
|
4
4
|
} from "./chunk-NTTLPF7F.js";
|
|
5
5
|
import {
|
|
6
6
|
FallbackLlmClient
|
|
7
|
-
} from "./chunk-
|
|
8
|
-
import "./chunk-
|
|
7
|
+
} from "./chunk-XUHI52HK.js";
|
|
8
|
+
import "./chunk-M5ZBBBJI.js";
|
|
9
|
+
import "./chunk-OOSWAUYB.js";
|
|
9
10
|
import "./chunk-Y27UJK6V.js";
|
|
10
11
|
import "./chunk-UZB5KHKX.js";
|
|
11
12
|
import "./chunk-M5KEYE5E.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/causal-consolidation.ts"],"sourcesContent":["/**\n * causal-consolidation.ts — CMC Phase 2: LLM-Assisted Causal Consolidation\n *\n * Uses an LLM to analyze causal trajectory patterns across sessions.\n * The LLM receives the causal chain graph as context — connected trajectories\n * from different sessions — and identifies recurring behavioral patterns,\n * preference signals, and actionable rules.\n *\n * This is the core CMC innovation: the LLM gets cross-session causal context\n * that no other memory system provides. It can see that a user investigated\n * a bug in session 1, attempted a fix in session 2, and succeeded in session 3 —\n * and synthesize a rule or preference from that chain.\n */\n\nimport { createHash } from \"node:crypto\";\nimport type { CausalTrajectoryRecord } from \"./causal-trajectory.js\";\nimport { readChainIndex, resolveChainsDir, type CausalChainIndex, type CausalEdge } from \"./causal-chain.js\";\nimport { listJsonFiles, readJsonFile } from \"./json-store.js\";\nimport { isRecord } from \"./store-contract.js\";\nimport { FallbackLlmClient } from \"./fallback-llm.js\";\nimport type { GatewayConfig } from \"./types.js\";\nimport path from \"node:path\";\nimport { log } from \"./logger.js\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface CausalPatternCandidate {\n id: string;\n sourceType: \"causal-pattern\";\n subject: string;\n category: \"principle\" | \"rule\";\n content: string;\n score: number;\n rationale: string;\n outcome: null;\n provenance: string[];\n agent: string | null;\n workflow: string | null;\n}\n\nexport interface ConsolidationConfig {\n minRecurrence: number;\n minSessions: number;\n successThreshold: number;\n}\n\nexport interface LlmConsolidationResult {\n rules: Array<{\n content: string;\n category: \"rule\" | \"principle\" | \"preference\";\n confidence: number;\n evidence: string[];\n }>;\n preferences: Array<{\n statement: string;\n confidence: number;\n evidence: string[];\n }>;\n}\n\n// ─── Trajectory Reading ──────────────────────────────────────────────────────\n\nasync function readAllTrajectories(\n memoryDir: string,\n causalTrajectoryStoreDir?: string,\n): Promise<CausalTrajectoryRecord[]> {\n const root = causalTrajectoryStoreDir\n ? path.join(memoryDir, causalTrajectoryStoreDir)\n : path.join(memoryDir, \"state\", \"causal-trajectories\");\n const trajectoriesDir = path.join(root, \"trajectories\");\n\n const files = await listJsonFiles(trajectoriesDir).catch(() => [] as string[]);\n const results: CausalTrajectoryRecord[] = [];\n\n for (const filePath of files) {\n try {\n const raw = await readJsonFile(filePath);\n if (isRecord(raw) && typeof raw.trajectoryId === \"string\") {\n results.push(raw as unknown as CausalTrajectoryRecord);\n }\n } catch {\n // skip invalid\n }\n }\n\n return results;\n}\n\n// ─── Context Formatting ──────────────────────────────────────────────────────\n\n/**\n * Format trajectories and their causal connections as a readable context\n * for the LLM. Groups by session and shows chain connections.\n */\nfunction formatCausalContext(\n trajectories: CausalTrajectoryRecord[],\n chainIndex: CausalChainIndex,\n maxChars: number = 8000,\n): string {\n // Group trajectories by session\n const bySession = new Map<string, CausalTrajectoryRecord[]>();\n for (const t of trajectories) {\n const list = bySession.get(t.sessionKey) ?? [];\n list.push(t);\n bySession.set(t.sessionKey, list);\n }\n\n const lines: string[] = [];\n lines.push(`## Causal Trajectories (${trajectories.length} across ${bySession.size} sessions)`);\n lines.push(\"\");\n\n // Format each session's trajectories\n for (const [sessionKey, sessionTrajs] of bySession) {\n lines.push(`### Session: ${sessionKey}`);\n for (const t of sessionTrajs.slice(0, 5)) {\n const outcome = t.outcomeKind === \"success\" ? \"+\" : t.outcomeKind === \"failure\" ? \"-\" : \"~\";\n lines.push(`[${outcome}] Goal: ${t.goal}`);\n lines.push(` Action: ${t.actionSummary}`);\n lines.push(` Outcome: ${t.outcomeSummary}`);\n if (t.followUpSummary) lines.push(` Follow-up: ${t.followUpSummary}`);\n if (t.entityRefs?.length) lines.push(` Entities: ${t.entityRefs.join(\", \")}`);\n }\n lines.push(\"\");\n }\n\n // Format causal chain connections\n const edgeCount = Object.keys(chainIndex.edges).length;\n if (edgeCount > 0) {\n lines.push(`## Cross-Session Causal Chains (${edgeCount} connections)`);\n lines.push(\"\");\n\n const trajectoryMap = new Map(trajectories.map((t) => [t.trajectoryId, t]));\n const shown = new Set<string>();\n\n for (const [edgeId, edge] of Object.entries(chainIndex.edges)) {\n if (shown.size >= 10) break; // limit output size\n const from = trajectoryMap.get(edge.fromTrajectoryId);\n const to = trajectoryMap.get(edge.toTrajectoryId);\n if (!from || !to) continue;\n\n lines.push(`${edge.edgeType}: \"${from.goal}\" (${from.sessionKey}) → \"${to.goal}\" (${to.sessionKey})`);\n shown.add(edgeId);\n }\n lines.push(\"\");\n }\n\n const result = lines.join(\"\\n\");\n return result.length > maxChars ? result.slice(0, maxChars) + \"\\n[truncated]\" : result;\n}\n\n// ─── LLM Consolidation ──────────────────────────────────────────────────────\n\nconst CONSOLIDATION_PROMPT = `You are analyzing a user's causal trajectory history across multiple sessions. Trajectories record what the user tried to do (goal), what they did (action), and what happened (outcome).\n\nYour job is to identify:\n1. BEHAVIORAL RULES: Recurring patterns where the same approach consistently succeeds or fails. These should be actionable guidance for future sessions.\n2. PREFERENCES: What the user cares about, prefers, or consistently chooses — even if never explicitly stated. Infer preferences from what they repeatedly do, retry until successful, or always include in their workflow.\n\nIMPORTANT:\n- Look for CROSS-SESSION patterns — things that repeat across different sessions are more significant than within-session patterns.\n- A user who retries the same goal across sessions has a strong implicit preference for that outcome.\n- Consistent action choices reveal preferences even when the user never says \"I prefer X.\"\n- Frame preferences as \"The user would prefer responses that...\" when applicable.\n\nOutput valid JSON only:\n{\n \"rules\": [\n {\"content\": \"actionable rule text\", \"category\": \"rule|principle\", \"confidence\": 0.0-1.0, \"evidence\": [\"trajectory IDs\"]}\n ],\n \"preferences\": [\n {\"statement\": \"The user would prefer...\", \"confidence\": 0.0-1.0, \"evidence\": [\"trajectory IDs\"]}\n ]\n}\n\nIf no clear patterns exist, return {\"rules\": [], \"preferences\": []}.`;\n\nasync function consolidateWithLlm(\n context: string,\n llm: FallbackLlmClient,\n agentId?: string,\n): Promise<LlmConsolidationResult> {\n const response = await llm.chatCompletion(\n [\n { role: \"system\", content: CONSOLIDATION_PROMPT },\n { role: \"user\", content: context },\n ],\n { temperature: 0.2, maxTokens: 2000, agentId },\n );\n\n if (!response?.content) {\n return { rules: [], preferences: [] };\n }\n\n try {\n // Extract JSON from response (may have markdown code fences)\n let jsonStr = response.content.trim();\n const fenceMatch = jsonStr.match(/```(?:json)?\\s*\\n?([\\s\\S]*?)\\n?\\s*```/);\n if (fenceMatch) jsonStr = fenceMatch[1];\n\n const parsed = JSON.parse(jsonStr);\n return {\n rules: Array.isArray(parsed.rules) ? parsed.rules.filter(\n (r: any) => typeof r.content === \"string\" && r.content.length > 5,\n ) : [],\n preferences: Array.isArray(parsed.preferences) ? parsed.preferences.filter(\n (p: any) => typeof p.statement === \"string\" && p.statement.length > 5,\n ) : [],\n };\n } catch {\n log.warn(\"[cmc] failed to parse LLM consolidation response\");\n return { rules: [], preferences: [] };\n }\n}\n\n// ─── Candidate Generation ────────────────────────────────────────────────────\n\nfunction stablePatternId(content: string): string {\n const digest = createHash(\"sha256\")\n .update(`causal-pattern\\0${content}`)\n .digest(\"hex\")\n .slice(0, 16);\n return `causal-pattern:${digest}`;\n}\n\nfunction llmResultToCandidates(result: LlmConsolidationResult): CausalPatternCandidate[] {\n const candidates: CausalPatternCandidate[] = [];\n\n for (const rule of result.rules) {\n const category = rule.category === \"principle\" ? \"principle\" : \"rule\";\n candidates.push({\n id: stablePatternId(rule.content),\n sourceType: \"causal-pattern\",\n subject: rule.content.slice(0, 80),\n category,\n content: rule.content,\n score: Math.min(1, rule.confidence ?? 0.7),\n rationale: \"LLM-identified causal pattern from cross-session trajectory analysis\",\n outcome: null,\n provenance: (rule.evidence ?? []).slice(0, 5),\n agent: null,\n workflow: null,\n });\n }\n\n return candidates;\n}\n\n// ─── Public API ──────────────────────────────────────────────────────────────\n\n/**\n * Run LLM-assisted consolidation: read trajectories, format causal context,\n * ask LLM to identify patterns and preferences.\n */\nexport async function deriveCausalPromotionCandidates(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n config: ConsolidationConfig;\n gatewayConfig?: GatewayConfig;\n gatewayAgentId?: string;\n}): Promise<CausalPatternCandidate[]> {\n try {\n const trajectories = await readAllTrajectories(options.memoryDir, options.causalTrajectoryStoreDir);\n if (trajectories.length < options.config.minRecurrence) return [];\n\n const chainsDir = resolveChainsDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const chainIndex = await readChainIndex(chainsDir);\n\n // Format the causal context for the LLM\n const context = formatCausalContext(trajectories, chainIndex);\n\n // If no LLM available, fall back to empty (no deterministic fallback)\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable(options.gatewayAgentId)) {\n log.debug(\"[cmc] no LLM available for consolidation — skipping\");\n return [];\n }\n\n // Call LLM for pattern analysis\n const result = await consolidateWithLlm(context, llm, options.gatewayAgentId);\n const candidates = llmResultToCandidates(result);\n\n log.debug(`[cmc] LLM consolidation produced ${candidates.length} rule(s) and ${result.preferences.length} preference(s)`);\n return candidates;\n } catch (error) {\n log.warn(`[cmc] consolidation failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return [];\n }\n}\n\n/**\n * Get LLM-synthesized preferences from causal trajectory analysis.\n * Returns formatted preference statements for recall injection.\n */\nexport async function synthesizeCausalPreferencesViaLlm(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n gatewayConfig?: GatewayConfig;\n gatewayAgentId?: string;\n minTrajectories?: number;\n}): Promise<string | null> {\n try {\n const trajectories = await readAllTrajectories(options.memoryDir, options.causalTrajectoryStoreDir);\n if (trajectories.length < (options.minTrajectories ?? 2)) return null;\n\n const chainsDir = resolveChainsDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const chainIndex = await readChainIndex(chainsDir);\n const context = formatCausalContext(trajectories, chainIndex);\n\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable(options.gatewayAgentId)) return null;\n\n const result = await consolidateWithLlm(context, llm, options.gatewayAgentId);\n if (result.preferences.length === 0 && result.rules.length === 0) return null;\n\n const lines: string[] = [\"## Behavioral Insights (from Causal Chain Analysis)\", \"\"];\n\n for (const pref of result.preferences) {\n lines.push(`- ${pref.statement}`);\n }\n\n for (const rule of result.rules) {\n lines.push(`- ${rule.content}`);\n }\n\n lines.push(\"\");\n return lines.join(\"\\n\");\n } catch (error) {\n log.warn(`[cmc] preference synthesis failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAcA,SAAS,kBAAkB;AAO3B,OAAO,UAAU;AAyCjB,eAAe,oBACb,WACA,0BACmC;AACnC,QAAM,OAAO,2BACT,KAAK,KAAK,WAAW,wBAAwB,IAC7C,KAAK,KAAK,WAAW,SAAS,qBAAqB;AACvD,QAAM,kBAAkB,KAAK,KAAK,MAAM,cAAc;AAEtD,QAAM,QAAQ,MAAM,cAAc,eAAe,EAAE,MAAM,MAAM,CAAC,CAAa;AAC7E,QAAM,UAAoC,CAAC;AAE3C,aAAW,YAAY,OAAO;AAC5B,QAAI;AACF,YAAM,MAAM,MAAM,aAAa,QAAQ;AACvC,UAAI,SAAS,GAAG,KAAK,OAAO,IAAI,iBAAiB,UAAU;AACzD,gBAAQ,KAAK,GAAwC;AAAA,MACvD;AAAA,IACF,QAAQ;AAAA,IA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O,EAAE;AAAA,IAChC;AAEA,UAAM,KAAK,EAAE;AACb,WAAO,MAAM,KAAK,IAAI;AAAA,EACxB,SAAS,OAAO;AACd,QAAI,KAAK,kDAAkD,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC,EAAE;AACnH,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/causal-consolidation.ts"],"sourcesContent":["/**\n * causal-consolidation.ts — CMC Phase 2: LLM-Assisted Causal Consolidation\n *\n * Uses an LLM to analyze causal trajectory patterns across sessions.\n * The LLM receives the causal chain graph as context — connected trajectories\n * from different sessions — and identifies recurring behavioral patterns,\n * preference signals, and actionable rules.\n *\n * This is the core CMC innovation: the LLM gets cross-session causal context\n * that no other memory system provides. It can see that a user investigated\n * a bug in session 1, attempted a fix in session 2, and succeeded in session 3 —\n * and synthesize a rule or preference from that chain.\n */\n\nimport { createHash } from \"node:crypto\";\nimport type { CausalTrajectoryRecord } from \"./causal-trajectory.js\";\nimport { readChainIndex, resolveChainsDir, type CausalChainIndex, type CausalEdge } from \"./causal-chain.js\";\nimport { listJsonFiles, readJsonFile } from \"./json-store.js\";\nimport { isRecord } from \"./store-contract.js\";\nimport { FallbackLlmClient } from \"./fallback-llm.js\";\nimport type { GatewayConfig } from \"./types.js\";\nimport path from \"node:path\";\nimport { log } from \"./logger.js\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface CausalPatternCandidate {\n id: string;\n sourceType: \"causal-pattern\";\n subject: string;\n category: \"principle\" | \"rule\";\n content: string;\n score: number;\n rationale: string;\n outcome: null;\n provenance: string[];\n agent: string | null;\n workflow: string | null;\n}\n\nexport interface ConsolidationConfig {\n minRecurrence: number;\n minSessions: number;\n successThreshold: number;\n}\n\nexport interface LlmConsolidationResult {\n rules: Array<{\n content: string;\n category: \"rule\" | \"principle\" | \"preference\";\n confidence: number;\n evidence: string[];\n }>;\n preferences: Array<{\n statement: string;\n confidence: number;\n evidence: string[];\n }>;\n}\n\n// ─── Trajectory Reading ──────────────────────────────────────────────────────\n\nasync function readAllTrajectories(\n memoryDir: string,\n causalTrajectoryStoreDir?: string,\n): Promise<CausalTrajectoryRecord[]> {\n const root = causalTrajectoryStoreDir\n ? path.join(memoryDir, causalTrajectoryStoreDir)\n : path.join(memoryDir, \"state\", \"causal-trajectories\");\n const trajectoriesDir = path.join(root, \"trajectories\");\n\n const files = await listJsonFiles(trajectoriesDir).catch(() => [] as string[]);\n const results: CausalTrajectoryRecord[] = [];\n\n for (const filePath of files) {\n try {\n const raw = await readJsonFile(filePath);\n if (isRecord(raw) && typeof raw.trajectoryId === \"string\") {\n results.push(raw as unknown as CausalTrajectoryRecord);\n }\n } catch {\n // skip invalid\n }\n }\n\n return results;\n}\n\n// ─── Context Formatting ──────────────────────────────────────────────────────\n\n/**\n * Format trajectories and their causal connections as a readable context\n * for the LLM. Groups by session and shows chain connections.\n */\nfunction formatCausalContext(\n trajectories: CausalTrajectoryRecord[],\n chainIndex: CausalChainIndex,\n maxChars: number = 8000,\n): string {\n // Group trajectories by session\n const bySession = new Map<string, CausalTrajectoryRecord[]>();\n for (const t of trajectories) {\n const list = bySession.get(t.sessionKey) ?? [];\n list.push(t);\n bySession.set(t.sessionKey, list);\n }\n\n const lines: string[] = [];\n lines.push(`## Causal Trajectories (${trajectories.length} across ${bySession.size} sessions)`);\n lines.push(\"\");\n\n // Format each session's trajectories\n for (const [sessionKey, sessionTrajs] of bySession) {\n lines.push(`### Session: ${sessionKey}`);\n for (const t of sessionTrajs.slice(0, 5)) {\n const outcome = t.outcomeKind === \"success\" ? \"+\" : t.outcomeKind === \"failure\" ? \"-\" : \"~\";\n lines.push(`[${outcome}] Goal: ${t.goal}`);\n lines.push(` Action: ${t.actionSummary}`);\n lines.push(` Outcome: ${t.outcomeSummary}`);\n if (t.followUpSummary) lines.push(` Follow-up: ${t.followUpSummary}`);\n if (t.entityRefs?.length) lines.push(` Entities: ${t.entityRefs.join(\", \")}`);\n }\n lines.push(\"\");\n }\n\n // Format causal chain connections\n const edgeCount = Object.keys(chainIndex.edges).length;\n if (edgeCount > 0) {\n lines.push(`## Cross-Session Causal Chains (${edgeCount} connections)`);\n lines.push(\"\");\n\n const trajectoryMap = new Map(trajectories.map((t) => [t.trajectoryId, t]));\n const shown = new Set<string>();\n\n for (const [edgeId, edge] of Object.entries(chainIndex.edges)) {\n if (shown.size >= 10) break; // limit output size\n const from = trajectoryMap.get(edge.fromTrajectoryId);\n const to = trajectoryMap.get(edge.toTrajectoryId);\n if (!from || !to) continue;\n\n lines.push(`${edge.edgeType}: \"${from.goal}\" (${from.sessionKey}) → \"${to.goal}\" (${to.sessionKey})`);\n shown.add(edgeId);\n }\n lines.push(\"\");\n }\n\n const result = lines.join(\"\\n\");\n return result.length > maxChars ? result.slice(0, maxChars) + \"\\n[truncated]\" : result;\n}\n\n// ─── LLM Consolidation ──────────────────────────────────────────────────────\n\nconst CONSOLIDATION_PROMPT = `You are analyzing a user's causal trajectory history across multiple sessions. Trajectories record what the user tried to do (goal), what they did (action), and what happened (outcome).\n\nYour job is to identify:\n1. BEHAVIORAL RULES: Recurring patterns where the same approach consistently succeeds or fails. These should be actionable guidance for future sessions.\n2. PREFERENCES: What the user cares about, prefers, or consistently chooses — even if never explicitly stated. Infer preferences from what they repeatedly do, retry until successful, or always include in their workflow.\n\nIMPORTANT:\n- Look for CROSS-SESSION patterns — things that repeat across different sessions are more significant than within-session patterns.\n- A user who retries the same goal across sessions has a strong implicit preference for that outcome.\n- Consistent action choices reveal preferences even when the user never says \"I prefer X.\"\n- Frame preferences as \"The user would prefer responses that...\" when applicable.\n\nOutput valid JSON only:\n{\n \"rules\": [\n {\"content\": \"actionable rule text\", \"category\": \"rule|principle\", \"confidence\": 0.0-1.0, \"evidence\": [\"trajectory IDs\"]}\n ],\n \"preferences\": [\n {\"statement\": \"The user would prefer...\", \"confidence\": 0.0-1.0, \"evidence\": [\"trajectory IDs\"]}\n ]\n}\n\nIf no clear patterns exist, return {\"rules\": [], \"preferences\": []}.`;\n\nasync function consolidateWithLlm(\n context: string,\n llm: FallbackLlmClient,\n agentId?: string,\n): Promise<LlmConsolidationResult> {\n const response = await llm.chatCompletion(\n [\n { role: \"system\", content: CONSOLIDATION_PROMPT },\n { role: \"user\", content: context },\n ],\n { temperature: 0.2, maxTokens: 2000, agentId },\n );\n\n if (!response?.content) {\n return { rules: [], preferences: [] };\n }\n\n try {\n // Extract JSON from response (may have markdown code fences)\n let jsonStr = response.content.trim();\n const fenceMatch = jsonStr.match(/```(?:json)?\\s*\\n?([\\s\\S]*?)\\n?\\s*```/);\n if (fenceMatch) jsonStr = fenceMatch[1];\n\n const parsed = JSON.parse(jsonStr);\n return {\n rules: Array.isArray(parsed.rules) ? parsed.rules.filter(\n (r: any) => typeof r.content === \"string\" && r.content.length > 5,\n ) : [],\n preferences: Array.isArray(parsed.preferences) ? parsed.preferences.filter(\n (p: any) => typeof p.statement === \"string\" && p.statement.length > 5,\n ) : [],\n };\n } catch {\n log.warn(\"[cmc] failed to parse LLM consolidation response\");\n return { rules: [], preferences: [] };\n }\n}\n\n// ─── Candidate Generation ────────────────────────────────────────────────────\n\nfunction stablePatternId(content: string): string {\n const digest = createHash(\"sha256\")\n .update(`causal-pattern\\0${content}`)\n .digest(\"hex\")\n .slice(0, 16);\n return `causal-pattern:${digest}`;\n}\n\nfunction llmResultToCandidates(result: LlmConsolidationResult): CausalPatternCandidate[] {\n const candidates: CausalPatternCandidate[] = [];\n\n for (const rule of result.rules) {\n const category = rule.category === \"principle\" ? \"principle\" : \"rule\";\n candidates.push({\n id: stablePatternId(rule.content),\n sourceType: \"causal-pattern\",\n subject: rule.content.slice(0, 80),\n category,\n content: rule.content,\n score: Math.min(1, rule.confidence ?? 0.7),\n rationale: \"LLM-identified causal pattern from cross-session trajectory analysis\",\n outcome: null,\n provenance: (rule.evidence ?? []).slice(0, 5),\n agent: null,\n workflow: null,\n });\n }\n\n return candidates;\n}\n\n// ─── Public API ──────────────────────────────────────────────────────────────\n\n/**\n * Run LLM-assisted consolidation: read trajectories, format causal context,\n * ask LLM to identify patterns and preferences.\n */\nexport async function deriveCausalPromotionCandidates(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n config: ConsolidationConfig;\n gatewayConfig?: GatewayConfig;\n gatewayAgentId?: string;\n}): Promise<CausalPatternCandidate[]> {\n try {\n const trajectories = await readAllTrajectories(options.memoryDir, options.causalTrajectoryStoreDir);\n if (trajectories.length < options.config.minRecurrence) return [];\n\n const chainsDir = resolveChainsDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const chainIndex = await readChainIndex(chainsDir);\n\n // Format the causal context for the LLM\n const context = formatCausalContext(trajectories, chainIndex);\n\n // If no LLM available, fall back to empty (no deterministic fallback)\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable(options.gatewayAgentId)) {\n log.debug(\"[cmc] no LLM available for consolidation — skipping\");\n return [];\n }\n\n // Call LLM for pattern analysis\n const result = await consolidateWithLlm(context, llm, options.gatewayAgentId);\n const candidates = llmResultToCandidates(result);\n\n log.debug(`[cmc] LLM consolidation produced ${candidates.length} rule(s) and ${result.preferences.length} preference(s)`);\n return candidates;\n } catch (error) {\n log.warn(`[cmc] consolidation failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return [];\n }\n}\n\n/**\n * Get LLM-synthesized preferences from causal trajectory analysis.\n * Returns formatted preference statements for recall injection.\n */\nexport async function synthesizeCausalPreferencesViaLlm(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n gatewayConfig?: GatewayConfig;\n gatewayAgentId?: string;\n minTrajectories?: number;\n}): Promise<string | null> {\n try {\n const trajectories = await readAllTrajectories(options.memoryDir, options.causalTrajectoryStoreDir);\n if (trajectories.length < (options.minTrajectories ?? 2)) return null;\n\n const chainsDir = resolveChainsDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const chainIndex = await readChainIndex(chainsDir);\n const context = formatCausalContext(trajectories, chainIndex);\n\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable(options.gatewayAgentId)) return null;\n\n const result = await consolidateWithLlm(context, llm, options.gatewayAgentId);\n if (result.preferences.length === 0 && result.rules.length === 0) return null;\n\n const lines: string[] = [\"## Behavioral Insights (from Causal Chain Analysis)\", \"\"];\n\n for (const pref of result.preferences) {\n lines.push(`- ${pref.statement}`);\n }\n\n for (const rule of result.rules) {\n lines.push(`- ${rule.content}`);\n }\n\n lines.push(\"\");\n return lines.join(\"\\n\");\n } catch (error) {\n log.warn(`[cmc] preference synthesis failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,SAAS,kBAAkB;AAO3B,OAAO,UAAU;AAyCjB,eAAe,oBACb,WACA,0BACmC;AACnC,QAAM,OAAO,2BACT,KAAK,KAAK,WAAW,wBAAwB,IAC7C,KAAK,KAAK,WAAW,SAAS,qBAAqB;AACvD,QAAM,kBAAkB,KAAK,KAAK,MAAM,cAAc;AAEtD,QAAM,QAAQ,MAAM,cAAc,eAAe,EAAE,MAAM,MAAM,CAAC,CAAa;AAC7E,QAAM,UAAoC,CAAC;AAE3C,aAAW,YAAY,OAAO;AAC5B,QAAI;AACF,YAAM,MAAM,MAAM,aAAa,QAAQ;AACvC,UAAI,SAAS,GAAG,KAAK,OAAO,IAAI,iBAAiB,UAAU;AACzD,gBAAQ,KAAK,GAAwC;AAAA,MACvD;AAAA,IACF,QAAQ;AAAA,IAER;AAAA,EACF;AAEA,SAAO;AACT;AAQA,SAAS,oBACP,cACA,YACA,WAAmB,KACX;AAER,QAAM,YAAY,oBAAI,IAAsC;AAC5D,aAAW,KAAK,cAAc;AAC5B,UAAM,OAAO,UAAU,IAAI,EAAE,UAAU,KAAK,CAAC;AAC7C,SAAK,KAAK,CAAC;AACX,cAAU,IAAI,EAAE,YAAY,IAAI;AAAA,EAClC;AAEA,QAAM,QAAkB,CAAC;AACzB,QAAM,K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|
|
27
27
|
FallbackLlmClient
|
|
28
|
-
} from "./chunk-
|
|
28
|
+
} from "./chunk-XUHI52HK.js";
|
|
29
29
|
import {
|
|
30
30
|
buildChatCompletionTokenLimit,
|
|
31
31
|
shouldAssumeOpenAiChatCompletions
|
|
@@ -2165,4 +2165,4 @@ ${memoryList}` }
|
|
|
2165
2165
|
export {
|
|
2166
2166
|
ExtractionEngine
|
|
2167
2167
|
};
|
|
2168
|
-
//# sourceMappingURL=chunk-
|
|
2168
|
+
//# sourceMappingURL=chunk-6UJQNRIO.js.map
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
} from "./chunk-MDDAA2AO.js";
|
|
12
12
|
import {
|
|
13
13
|
FallbackLlmClient
|
|
14
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-XUHI52HK.js";
|
|
15
15
|
import {
|
|
16
16
|
extractJsonCandidates
|
|
17
17
|
} from "./chunk-UZB5KHKX.js";
|
|
@@ -618,4 +618,4 @@ ${truncatedConversation}`;
|
|
|
618
618
|
export {
|
|
619
619
|
HourlySummarizer
|
|
620
620
|
};
|
|
621
|
-
//# sourceMappingURL=chunk-
|
|
621
|
+
//# sourceMappingURL=chunk-LP47L3ZX.js.map
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import path from "path";
|
|
10
10
|
import os from "os";
|
|
11
11
|
var _resolveApiKeyForProvider = null;
|
|
12
|
+
var _getRuntimeAuthForModel = null;
|
|
12
13
|
var _resolverLoaded = false;
|
|
13
14
|
var _resolverNextRetryAt = 0;
|
|
14
15
|
var RESOLVER_RETRY_BACKOFF_MS = 6e4;
|
|
@@ -32,6 +33,10 @@ async function getGatewayResolver() {
|
|
|
32
33
|
const mod = await import(importUrl);
|
|
33
34
|
if (typeof mod.resolveApiKeyForProvider === "function") {
|
|
34
35
|
_resolveApiKeyForProvider = mod.resolveApiKeyForProvider;
|
|
36
|
+
if (typeof mod.getRuntimeAuthForModel === "function") {
|
|
37
|
+
_getRuntimeAuthForModel = mod.getRuntimeAuthForModel;
|
|
38
|
+
log.debug("loaded gateway getRuntimeAuthForModel from runtime module");
|
|
39
|
+
}
|
|
35
40
|
_resolverLoaded = true;
|
|
36
41
|
log.debug("loaded gateway resolveApiKeyForProvider from runtime module");
|
|
37
42
|
return _resolveApiKeyForProvider;
|
|
@@ -138,15 +143,21 @@ function resolveFromEnv(providerId) {
|
|
|
138
143
|
}
|
|
139
144
|
return void 0;
|
|
140
145
|
}
|
|
146
|
+
async function getGatewayRuntimeAuthForModel() {
|
|
147
|
+
await getGatewayResolver();
|
|
148
|
+
return _getRuntimeAuthForModel;
|
|
149
|
+
}
|
|
141
150
|
function clearSecretCache() {
|
|
142
151
|
resolvedCache.clear();
|
|
143
152
|
_resolveApiKeyForProvider = null;
|
|
153
|
+
_getRuntimeAuthForModel = null;
|
|
144
154
|
_resolverLoaded = false;
|
|
145
155
|
_resolverNextRetryAt = 0;
|
|
146
156
|
}
|
|
147
157
|
|
|
148
158
|
export {
|
|
149
159
|
resolveProviderApiKey,
|
|
160
|
+
getGatewayRuntimeAuthForModel,
|
|
150
161
|
clearSecretCache
|
|
151
162
|
};
|
|
152
|
-
//# sourceMappingURL=chunk-
|
|
163
|
+
//# sourceMappingURL=chunk-M5ZBBBJI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/resolve-provider-secret.ts"],"sourcesContent":["import { log } from \"./logger.js\";\nimport { readEnvVar } from \"./runtime/env.js\";\nimport path from \"node:path\";\nimport os from \"node:os\";\n\n/**\n * Resolve a provider API key using OpenClaw's own auth resolution system.\n *\n * This module delegates to the gateway's `resolveApiKeyForProvider()` function,\n * which handles all secret reference formats (SecretRef objects, auth profiles,\n * \"secretref-managed\" markers, environment variables, etc.) using the same\n * codepath the gateway uses for its own agent sessions.\n *\n * For plain-text API keys, a fast path returns them directly without\n * involving the gateway auth system.\n *\n * Results are cached per provider for the gateway process lifetime.\n */\n\ntype ResolveApiKeyFn = (params: {\n provider: string;\n cfg?: unknown;\n agentDir?: string;\n}) => Promise<{ apiKey?: string; source?: string; mode?: string } | null>;\n\n/**\n * Resolve request-ready auth for a model, including provider-owned transforms\n * (e.g., OAuth token exchange, base URL override for openai-codex).\n */\nexport type GetRuntimeAuthForModelFn = (params: {\n model: { provider: string; id: string; api?: string; baseUrl?: string };\n cfg?: unknown;\n workspaceDir?: string;\n}) => Promise<{\n apiKey?: string;\n baseUrl?: string;\n source?: string;\n mode?: string;\n profileId?: string;\n} | null>;\n\nlet _resolveApiKeyForProvider: ResolveApiKeyFn | null = null;\nlet _getRuntimeAuthForModel: GetRuntimeAuthForModelFn | null = null;\nlet _resolverLoaded = false;\nlet _resolverNextRetryAt = 0;\nconst RESOLVER_RETRY_BACKOFF_MS = 60_000; // 1 minute between retries after first failure\nconst resolvedCache = new Map<string, string | undefined>();\n\n/**\n * Lazily load the gateway's resolveApiKeyForProvider function.\n * Returns null if not available (e.g., running outside the gateway process).\n */\nasync function getGatewayResolver(): Promise<ResolveApiKeyFn | null> {\n if (_resolverLoaded) {\n return _resolveApiKeyForProvider;\n }\n // Backoff: don't re-scan filesystem on every call when module wasn't found.\n // After a failure, wait RESOLVER_RETRY_BACKOFF_MS before trying again.\n if (_resolverNextRetryAt > 0 && Date.now() < _resolverNextRetryAt) {\n return null;\n }\n\n try {\n // The gateway bundles this in a runtime chunk — import it dynamically.\n // This import path is stable across gateway versions since it's a named runtime export.\n const candidates = [\n // Try glob-matching the runtime module name (hash varies per build)\n ...await findRuntimeModules(),\n ];\n\n const { pathToFileURL } = await import(\"node:url\");\n for (const candidate of candidates) {\n try {\n // Convert native path to file:// URL for cross-platform ESM import compatibility\n const importUrl = pathToFileURL(candidate).href;\n const mod = await import(importUrl);\n if (typeof mod.resolveApiKeyForProvider === \"function\") {\n _resolveApiKeyForProvider = mod.resolveApiKeyForProvider;\n if (typeof mod.getRuntimeAuthForModel === \"function\") {\n _getRuntimeAuthForModel = mod.getRuntimeAuthForModel;\n log.debug(\"loaded gateway getRuntimeAuthForModel from runtime module\");\n }\n _resolverLoaded = true;\n log.debug(\"loaded gateway resolveApiKeyForProvider from runtime module\");\n return _resolveApiKeyForProvider;\n }\n } catch {\n // Try next candidate\n }\n }\n } catch {\n // Silent\n }\n\n // Backoff before retrying — avoid repeated fs scanning.\n // Retries after RESOLVER_RETRY_BACKOFF_MS so the resolver can\n // recover if the gateway restarts or the module becomes available.\n _resolverNextRetryAt = Date.now() + RESOLVER_RETRY_BACKOFF_MS;\n log.debug(`gateway resolveApiKeyForProvider not available — will retry after ${RESOLVER_RETRY_BACKOFF_MS / 1000}s`);\n return null;\n}\n\n/**\n * Find the gateway's model-auth runtime module by scanning the dist directory.\n * Uses require.resolve to find the openclaw package regardless of install method.\n */\nasync function findRuntimeModules(): Promise<string[]> {\n const { readdirSync } = await import(\"node:fs\");\n const { createRequire } = await import(\"node:module\");\n const candidates: string[] = [];\n\n // Discover the openclaw dist directory from the installed package,\n // regardless of how it was installed (Homebrew, npm global, local, etc.)\n const distDirs: string[] = [];\n\n try {\n // require.resolve finds the package from the current process context\n const req = createRequire(import.meta.url);\n const openclawMain = req.resolve(\"openclaw\");\n const openclawDist = path.join(path.dirname(openclawMain), \"..\", \"dist\");\n if (openclawDist) distDirs.push(path.resolve(openclawDist));\n } catch {\n // openclaw not resolvable from plugin context — try alternate paths\n }\n\n // Fallback: infer from the running process (gateway runs from its own dist/)\n // Use fs.realpathSync to resolve symlinks (e.g., /usr/local/bin/openclaw → actual path)\n try {\n const { realpathSync } = await import(\"node:fs\");\n const mainScript = process.argv[1];\n if (mainScript) {\n const realScript = realpathSync(mainScript);\n if (realScript.includes(\"openclaw\")) {\n const distDir = path.dirname(realScript);\n if (!distDirs.includes(distDir)) distDirs.push(distDir);\n }\n }\n } catch {\n // Silent\n }\n\n for (const dir of distDirs) {\n try {\n const files = readdirSync(dir);\n for (const f of files) {\n if (f.startsWith(\"runtime-model-auth.runtime-\") && f.endsWith(\".js\")) {\n candidates.push(path.join(dir, f));\n }\n }\n } catch {\n // Directory doesn't exist — skip\n }\n }\n\n return candidates;\n}\n\n/**\n * Resolve a provider API key from various OpenClaw formats.\n *\n * Resolution order:\n * 1. Plain-text string → returned immediately\n * 2. Gateway's resolveApiKeyForProvider → handles all secret ref formats\n * 3. Environment variable fallback (PROVIDER_NAME_API_KEY)\n * 4. undefined → provider is skipped in the fallback chain\n */\nexport async function resolveProviderApiKey(\n providerId: string,\n apiKeyValue: unknown,\n gatewayConfig?: unknown,\n agentDir?: string,\n): Promise<string | undefined> {\n // Check cache first\n const cacheKey = `provider:${providerId}`;\n if (resolvedCache.has(cacheKey)) {\n return resolvedCache.get(cacheKey);\n }\n\n let resolved: string | undefined;\n\n // Fast path: plain-text string that looks like an actual API key\n if (typeof apiKeyValue === \"string\" && apiKeyValue.trim().length > 0) {\n // Skip known non-API-key markers used by the gateway for auth modes\n // that don't use bearer tokens (OAuth, local endpoints, GCP credentials)\n if (\n apiKeyValue === \"secretref-managed\" ||\n apiKeyValue.endsWith(\"-oauth\") ||\n apiKeyValue.endsWith(\"-local\") ||\n apiKeyValue === \"lm-studio\" ||\n apiKeyValue.startsWith(\"gcp-\")\n ) {\n // Fall through to gateway resolver / env var fallback\n } else {\n resolved = apiKeyValue;\n resolvedCache.set(cacheKey, resolved);\n return resolved;\n }\n }\n\n // The API key is either a SecretRef object, \"secretref-managed\", or empty.\n // Try the gateway's own auth resolution system first.\n const resolver = await getGatewayResolver();\n if (resolver) {\n try {\n const resolvedAgentDir = agentDir ?? path.join(os.homedir(), \".openclaw\", \"agents\", \"main\", \"agent\");\n const auth = await resolver({ provider: providerId, cfg: gatewayConfig, agentDir: resolvedAgentDir });\n if (auth?.apiKey) {\n resolved = auth.apiKey;\n log.debug(`resolved API key for provider \"${providerId}\" via gateway auth (source: ${auth.source ?? \"unknown\"}, mode: ${auth.mode ?? \"unknown\"})`);\n resolvedCache.set(cacheKey, resolved);\n return resolved;\n }\n } catch (err) {\n log.debug(\n `gateway auth resolution failed for provider \"${providerId}\": ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n\n // Environment variable fallback\n resolved = resolveFromEnv(providerId);\n if (resolved) {\n log.debug(`resolved API key for provider \"${providerId}\" from environment variable`);\n } else {\n log.debug(`could not resolve API key for provider \"${providerId}\" — skipping`);\n }\n\n // Only cache successful resolutions — failures are retried on next call\n // so providers can recover after transient issues (e.g., 1Password agent restart)\n if (resolved) {\n resolvedCache.set(cacheKey, resolved);\n }\n return resolved;\n}\n\n/**\n * Try to resolve an API key from environment variables.\n */\nfunction resolveFromEnv(providerId: string): string | undefined {\n const normalized = providerId.toUpperCase().replace(/[^A-Z0-9]/g, \"_\");\n const candidates = [\n `${normalized}_API_KEY`,\n `${normalized}_TOKEN`,\n ];\n for (const envVar of candidates) {\n const value = readEnvVar(envVar);\n if (value && value.trim().length > 0) {\n return value.trim();\n }\n }\n return undefined;\n}\n\n/**\n * Get the gateway's getRuntimeAuthForModel function, if available.\n * This resolves request-ready auth including provider-owned transforms\n * (OAuth token exchange, base URL override for codex/copilot/etc.).\n * Must be called after at least one resolveProviderApiKey() call to\n * trigger the lazy module load.\n */\nexport async function getGatewayRuntimeAuthForModel(): Promise<GetRuntimeAuthForModelFn | null> {\n // Ensure the runtime module has been loaded\n await getGatewayResolver();\n return _getRuntimeAuthForModel;\n}\n\n/**\n * Clear the resolution cache (useful for testing or key rotation).\n */\nexport function clearSecretCache(): void {\n resolvedCache.clear();\n _resolveApiKeyForProvider = null;\n _getRuntimeAuthForModel = null;\n _resolverLoaded = false;\n _resolverNextRetryAt = 0;\n}\n"],"mappings":";;;;;;;;AAEA,OAAO,UAAU;AACjB,OAAO,QAAQ;AAsCf,IAAI,4BAAoD;AACxD,IAAI,0BAA2D;AAC/D,IAAI,kBAAkB;AACtB,IAAI,uBAAuB;AAC3B,IAAM,4BAA4B;AAClC,IAAM,gBAAgB,oBAAI,IAAgC;AAM1D,eAAe,qBAAsD;AACnE,MAAI,iBAAiB;AACnB,WAAO;AAAA,EACT;AAG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,UAAU,+BAA+B,KAAK,UAAU,SAAS,WAAW,KAAK,QAAQ,SAAS,GAAG;AACjJ,sBAAc,IAAI,UAAU,QAAQ;AACpC,eAAO;AAAA,MACT;AAAA,IACF,SAAS,KAAK;AACZ,UAAI;AAAA,QACF,gDAAgD,UAAU,MAAM,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,CAAC;AAAA,MAClH;AAAA,IACF;AAAA,EACF;AAGA,aAAW,eAAe,UAAU;AACpC,MAAI,UAAU;AACZ,QAAI,MAAM,kCAAkC,UAAU,6BAA6B;AAAA,EACrF,OAAO;AACL,QAAI,MAAM,2CAA2C,UAAU,mBAAc;AAAA,EAC/E;AAIA,MAAI,UAAU;AACZ,kBAAc,IAAI,UAAU,QAAQ;AAAA,EACtC;AACA,SAAO;AACT;AAKA,SAAS,eAAe,YAAwC;AAC9D,QAAM,aAAa,WAAW,YAAY,EAAE,QAAQ,cAAc,GAAG;AACrE,QAAM,aAAa;AAAA,IACjB,GAAG,UAAU;AAAA,IACb,GAAG,UAAU;AAAA,EACf;AACA,aAAW,UAAU,YAAY;AAC/B,UAAM,QAAQ,WAAW,MAAM;AAC/B,QAAI,SAAS,MAAM,KAAK,EAAE,SAAS,GAAG;AACpC,aAAO,MAAM,KAAK;AAAA,IACpB;AAAA,EACF;AACA,SAAO;AACT;AASA,eAAsB,gCAA0E;AAE9F,QAAM,mBAAmB;AACzB,SAAO;AACT;AAKO,SAAS,mBAAyB;AACvC,gBAAc,MAAM;AACpB,8BAA4B;AAC5B,4BAA0B;AAC1B,oBAAkB;AAClB,yBAAuB;AACzB;","names":[]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
log
|
|
3
|
+
} from "./chunk-KWBU5S5U.js";
|
|
4
|
+
|
|
5
|
+
// src/models-json.ts
|
|
6
|
+
import { readFileSync } from "fs";
|
|
7
|
+
import { join } from "path";
|
|
8
|
+
import { homedir } from "os";
|
|
9
|
+
var _cachedProviders = null;
|
|
10
|
+
var _loadAttempted = false;
|
|
11
|
+
function loadModelsJsonProviders() {
|
|
12
|
+
if (_loadAttempted) {
|
|
13
|
+
return _cachedProviders ?? {};
|
|
14
|
+
}
|
|
15
|
+
_loadAttempted = true;
|
|
16
|
+
try {
|
|
17
|
+
const modelsPath = join(homedir(), ".openclaw", "agents", "main", "agent", "models.json");
|
|
18
|
+
const raw = readFileSync(modelsPath, "utf-8");
|
|
19
|
+
const parsed = JSON.parse(raw);
|
|
20
|
+
const providers = parsed?.providers;
|
|
21
|
+
if (providers && typeof providers === "object" && !Array.isArray(providers)) {
|
|
22
|
+
_cachedProviders = providers;
|
|
23
|
+
log.debug(`loaded ${Object.keys(_cachedProviders).length} providers from models.json`);
|
|
24
|
+
return _cachedProviders;
|
|
25
|
+
}
|
|
26
|
+
} catch (err) {
|
|
27
|
+
log.debug(
|
|
28
|
+
`could not load models.json: ${err instanceof Error ? err.message : String(err)}`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
function clearModelsJsonCache() {
|
|
34
|
+
_cachedProviders = null;
|
|
35
|
+
_loadAttempted = false;
|
|
36
|
+
}
|
|
37
|
+
function __setModelsJsonForTest(providers) {
|
|
38
|
+
_cachedProviders = providers;
|
|
39
|
+
_loadAttempted = true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export {
|
|
43
|
+
loadModelsJsonProviders,
|
|
44
|
+
clearModelsJsonCache,
|
|
45
|
+
__setModelsJsonForTest
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=chunk-OOSWAUYB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/models-json.ts"],"sourcesContent":["import { readFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { homedir } from \"node:os\";\nimport { log } from \"./logger.js\";\nimport type { ModelProviderConfig } from \"./types.js\";\n\n/**\n * Read the gateway's materialized models.json to get the full provider map,\n * including built-in providers (openai-codex, google-vertex, etc.) that are\n * not declared in the user's openclaw.json but are registered by gateway\n * plugins at runtime.\n *\n * The gateway writes models.json to ~/.openclaw/agents/main/agent/models.json\n * with all providers merged: user-defined (from openclaw.json) + built-in\n * (from plugin catalogs). Each entry has the correct baseUrl, api format,\n * and auth mode for that provider.\n *\n * Results are cached for the process lifetime since models.json only changes\n * when the gateway restarts or `openclaw models` commands run.\n */\n\nlet _cachedProviders: Record<string, ModelProviderConfig> | null = null;\nlet _loadAttempted = false;\n\n/**\n * Load the full providers map from the gateway's models.json.\n * Returns an empty object if the file doesn't exist or can't be parsed.\n */\nexport function loadModelsJsonProviders(): Record<string, ModelProviderConfig> {\n if (_loadAttempted) {\n return _cachedProviders ?? {};\n }\n _loadAttempted = true;\n\n try {\n const modelsPath = join(homedir(), \".openclaw\", \"agents\", \"main\", \"agent\", \"models.json\");\n const raw = readFileSync(modelsPath, \"utf-8\");\n const parsed = JSON.parse(raw);\n const providers = parsed?.providers;\n\n if (providers && typeof providers === \"object\" && !Array.isArray(providers)) {\n _cachedProviders = providers as Record<string, ModelProviderConfig>;\n log.debug(`loaded ${Object.keys(_cachedProviders).length} providers from models.json`);\n return _cachedProviders;\n }\n } catch (err) {\n log.debug(\n `could not load models.json: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n return {};\n}\n\n/**\n * Clear the cached providers (useful for testing).\n */\nexport function clearModelsJsonCache(): void {\n _cachedProviders = null;\n _loadAttempted = false;\n}\n\n/**\n * Inject a providers map for testing, bypassing file I/O.\n */\nexport function __setModelsJsonForTest(providers: Record<string, ModelProviderConfig>): void {\n _cachedProviders = providers;\n _loadAttempted = true;\n}\n"],"mappings":";;;;;AAAA,SAAS,oBAAoB;AAC7B,SAAS,YAAY;AACrB,SAAS,eAAe;AAmBxB,IAAI,mBAA+D;AACnE,IAAI,iBAAiB;AAMd,SAAS,0BAA+D;AAC7E,MAAI,gBAAgB;AAClB,WAAO,oBAAoB,CAAC;AAAA,EAC9B;AACA,mBAAiB;AAEjB,MAAI;AACF,UAAM,aAAa,KAAK,QAAQ,GAAG,aAAa,UAAU,QAAQ,SAAS,aAAa;AACxF,UAAM,MAAM,aAAa,YAAY,OAAO;AAC5C,UAAM,SAAS,KAAK,MAAM,GAAG;AAC7B,UAAM,YAAY,QAAQ;AAE1B,QAAI,aAAa,OAAO,cAAc,YAAY,CAAC,MAAM,QAAQ,SAAS,GAAG;AAC3E,yBAAmB;AACnB,UAAI,MAAM,UAAU,OAAO,KAAK,gBAAgB,EAAE,MAAM,6BAA6B;AACrF,aAAO;AAAA,IACT;AAAA,EACF,SAAS,KAAK;AACZ,QAAI;AAAA,MACF,+BAA+B,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,CAAC;AAAA,IACjF;AAAA,EACF;AAEA,SAAO,CAAC;AACV;AAKO,SAAS,uBAA6B;AAC3C,qBAAmB;AACnB,mBAAiB;AACnB;AAKO,SAAS,uBAAuB,WAAsD;AAC3F,qBAAmB;AACnB,mBAAiB;AACnB;","names":[]}
|