@chendpoc/pi-memory 0.1.0

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.
Files changed (177) hide show
  1. package/README.md +180 -0
  2. package/dist/adapters/piComplete.d.ts +17 -0
  3. package/dist/adapters/piComplete.d.ts.map +1 -0
  4. package/dist/adapters/piComplete.js +169 -0
  5. package/dist/adapters/piComplete.js.map +1 -0
  6. package/dist/bundle/install.d.ts +34 -0
  7. package/dist/bundle/install.d.ts.map +1 -0
  8. package/dist/bundle/install.js +183 -0
  9. package/dist/bundle/install.js.map +1 -0
  10. package/dist/cli.d.ts +3 -0
  11. package/dist/cli.d.ts.map +1 -0
  12. package/dist/cli.js +245 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/config.d.ts +27 -0
  15. package/dist/config.d.ts.map +1 -0
  16. package/dist/config.js +49 -0
  17. package/dist/config.js.map +1 -0
  18. package/dist/errclass.d.ts +7 -0
  19. package/dist/errclass.d.ts.map +1 -0
  20. package/dist/errclass.js +32 -0
  21. package/dist/errclass.js.map +1 -0
  22. package/dist/extension.d.ts +24 -0
  23. package/dist/extension.d.ts.map +1 -0
  24. package/dist/extension.js +7 -0
  25. package/dist/extension.js.map +1 -0
  26. package/dist/fallback/index.d.ts +11 -0
  27. package/dist/fallback/index.d.ts.map +1 -0
  28. package/dist/fallback/index.js +16 -0
  29. package/dist/fallback/index.js.map +1 -0
  30. package/dist/fallback/llmRerank.d.ts +19 -0
  31. package/dist/fallback/llmRerank.d.ts.map +1 -0
  32. package/dist/fallback/llmRerank.js +60 -0
  33. package/dist/fallback/llmRerank.js.map +1 -0
  34. package/dist/fallback/memoryMd.d.ts +6 -0
  35. package/dist/fallback/memoryMd.d.ts.map +1 -0
  36. package/dist/fallback/memoryMd.js +35 -0
  37. package/dist/fallback/memoryMd.js.map +1 -0
  38. package/dist/fallback/sessionIndex.d.ts +35 -0
  39. package/dist/fallback/sessionIndex.d.ts.map +1 -0
  40. package/dist/fallback/sessionIndex.js +222 -0
  41. package/dist/fallback/sessionIndex.js.map +1 -0
  42. package/dist/fallback/sessionSearch.d.ts +18 -0
  43. package/dist/fallback/sessionSearch.d.ts.map +1 -0
  44. package/dist/fallback/sessionSearch.js +161 -0
  45. package/dist/fallback/sessionSearch.js.map +1 -0
  46. package/dist/index.d.ts +25 -0
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +24 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/paths.d.ts +7 -0
  51. package/dist/paths.d.ts.map +1 -0
  52. package/dist/paths.js +26 -0
  53. package/dist/paths.js.map +1 -0
  54. package/dist/pi-extension.d.ts +6 -0
  55. package/dist/pi-extension.d.ts.map +1 -0
  56. package/dist/pi-extension.js +224 -0
  57. package/dist/pi-extension.js.map +1 -0
  58. package/dist/preflight/detectIntents.d.ts +102 -0
  59. package/dist/preflight/detectIntents.d.ts.map +1 -0
  60. package/dist/preflight/detectIntents.js +624 -0
  61. package/dist/preflight/detectIntents.js.map +1 -0
  62. package/dist/preflight/hook.d.ts +58 -0
  63. package/dist/preflight/hook.d.ts.map +1 -0
  64. package/dist/preflight/hook.js +77 -0
  65. package/dist/preflight/hook.js.map +1 -0
  66. package/dist/preflight/render.d.ts +21 -0
  67. package/dist/preflight/render.d.ts.map +1 -0
  68. package/dist/preflight/render.js +132 -0
  69. package/dist/preflight/render.js.map +1 -0
  70. package/dist/preflight/strip.d.ts +11 -0
  71. package/dist/preflight/strip.d.ts.map +1 -0
  72. package/dist/preflight/strip.js +46 -0
  73. package/dist/preflight/strip.js.map +1 -0
  74. package/dist/service.d.ts +56 -0
  75. package/dist/service.d.ts.map +1 -0
  76. package/dist/service.js +158 -0
  77. package/dist/service.js.map +1 -0
  78. package/dist/sidecar/bundle.d.ts +19 -0
  79. package/dist/sidecar/bundle.d.ts.map +1 -0
  80. package/dist/sidecar/bundle.js +39 -0
  81. package/dist/sidecar/bundle.js.map +1 -0
  82. package/dist/sidecar/client.d.ts +17 -0
  83. package/dist/sidecar/client.d.ts.map +1 -0
  84. package/dist/sidecar/client.js +107 -0
  85. package/dist/sidecar/client.js.map +1 -0
  86. package/dist/sidecar/process.d.ts +14 -0
  87. package/dist/sidecar/process.d.ts.map +1 -0
  88. package/dist/sidecar/process.js +126 -0
  89. package/dist/sidecar/process.js.map +1 -0
  90. package/dist/tools/memoryAppend.d.ts +37 -0
  91. package/dist/tools/memoryAppend.d.ts.map +1 -0
  92. package/dist/tools/memoryAppend.js +99 -0
  93. package/dist/tools/memoryAppend.js.map +1 -0
  94. package/dist/tools/memoryRecall.d.ts +113 -0
  95. package/dist/tools/memoryRecall.d.ts.map +1 -0
  96. package/dist/tools/memoryRecall.js +325 -0
  97. package/dist/tools/memoryRecall.js.map +1 -0
  98. package/dist/trainer/bundleBuilder.d.ts +30 -0
  99. package/dist/trainer/bundleBuilder.d.ts.map +1 -0
  100. package/dist/trainer/bundleBuilder.js +106 -0
  101. package/dist/trainer/bundleBuilder.js.map +1 -0
  102. package/dist/trainer/bundleLoader.d.ts +12 -0
  103. package/dist/trainer/bundleLoader.d.ts.map +1 -0
  104. package/dist/trainer/bundleLoader.js +59 -0
  105. package/dist/trainer/bundleLoader.js.map +1 -0
  106. package/dist/trainer/deltaMerge.d.ts +38 -0
  107. package/dist/trainer/deltaMerge.d.ts.map +1 -0
  108. package/dist/trainer/deltaMerge.js +183 -0
  109. package/dist/trainer/deltaMerge.js.map +1 -0
  110. package/dist/trainer/entityResolver.d.ts +27 -0
  111. package/dist/trainer/entityResolver.d.ts.map +1 -0
  112. package/dist/trainer/entityResolver.js +92 -0
  113. package/dist/trainer/entityResolver.js.map +1 -0
  114. package/dist/trainer/extractFacts.d.ts +67 -0
  115. package/dist/trainer/extractFacts.d.ts.map +1 -0
  116. package/dist/trainer/extractFacts.js +213 -0
  117. package/dist/trainer/extractFacts.js.map +1 -0
  118. package/dist/trainer/index.d.ts +54 -0
  119. package/dist/trainer/index.d.ts.map +1 -0
  120. package/dist/trainer/index.js +82 -0
  121. package/dist/trainer/index.js.map +1 -0
  122. package/dist/trainer/llmExtractor.d.ts +16 -0
  123. package/dist/trainer/llmExtractor.d.ts.map +1 -0
  124. package/dist/trainer/llmExtractor.js +146 -0
  125. package/dist/trainer/llmExtractor.js.map +1 -0
  126. package/dist/trainer/marker.d.ts +10 -0
  127. package/dist/trainer/marker.d.ts.map +1 -0
  128. package/dist/trainer/marker.js +28 -0
  129. package/dist/trainer/marker.js.map +1 -0
  130. package/dist/trainer/scheduler.d.ts +31 -0
  131. package/dist/trainer/scheduler.d.ts.map +1 -0
  132. package/dist/trainer/scheduler.js +72 -0
  133. package/dist/trainer/scheduler.js.map +1 -0
  134. package/dist/trainer/sessionLoader.d.ts +23 -0
  135. package/dist/trainer/sessionLoader.d.ts.map +1 -0
  136. package/dist/trainer/sessionLoader.js +106 -0
  137. package/dist/trainer/sessionLoader.js.map +1 -0
  138. package/dist/types.d.ts +135 -0
  139. package/dist/types.d.ts.map +1 -0
  140. package/dist/types.js +8 -0
  141. package/dist/types.js.map +1 -0
  142. package/package.json +78 -0
  143. package/src/adapters/piComplete.ts +233 -0
  144. package/src/bundle/install.ts +206 -0
  145. package/src/cli.ts +254 -0
  146. package/src/config.ts +92 -0
  147. package/src/errclass.ts +37 -0
  148. package/src/extension.ts +23 -0
  149. package/src/fallback/index.ts +24 -0
  150. package/src/fallback/llmRerank.ts +90 -0
  151. package/src/fallback/memoryMd.ts +36 -0
  152. package/src/fallback/sessionIndex.ts +289 -0
  153. package/src/fallback/sessionSearch.ts +181 -0
  154. package/src/index.ts +213 -0
  155. package/src/paths.ts +28 -0
  156. package/src/pi-extension.ts +276 -0
  157. package/src/preflight/detectIntents.ts +654 -0
  158. package/src/preflight/hook.ts +136 -0
  159. package/src/preflight/render.ts +185 -0
  160. package/src/preflight/strip.ts +50 -0
  161. package/src/service.ts +202 -0
  162. package/src/sidecar/bundle.ts +52 -0
  163. package/src/sidecar/client.ts +166 -0
  164. package/src/sidecar/process.ts +145 -0
  165. package/src/tools/memoryAppend.ts +113 -0
  166. package/src/tools/memoryRecall.ts +364 -0
  167. package/src/trainer/bundleBuilder.ts +192 -0
  168. package/src/trainer/bundleLoader.ts +105 -0
  169. package/src/trainer/deltaMerge.ts +221 -0
  170. package/src/trainer/entityResolver.ts +140 -0
  171. package/src/trainer/extractFacts.ts +312 -0
  172. package/src/trainer/index.ts +147 -0
  173. package/src/trainer/llmExtractor.ts +206 -0
  174. package/src/trainer/marker.ts +30 -0
  175. package/src/trainer/scheduler.ts +104 -0
  176. package/src/trainer/sessionLoader.ts +139 -0
  177. package/src/types.ts +168 -0
@@ -0,0 +1,135 @@
1
+ /** Wire types mirroring Kocoro internal/memory/types.go (TLM sidecar contract). */
2
+ export type QueryMode = "direct_relation" | "path_query" | "typed_neighborhood";
3
+ export interface QueryIntent {
4
+ mode: QueryMode;
5
+ anchor_mentions: string[];
6
+ relation_constraints?: string[];
7
+ candidate_type?: string | null;
8
+ scope_filter?: string[];
9
+ target_slot?: "" | "head" | "tail";
10
+ time_window?: string | null;
11
+ evidence_budget?: number;
12
+ result_limit?: number;
13
+ }
14
+ export interface QueryRequest {
15
+ intent: QueryIntent;
16
+ user_id?: string | null;
17
+ request_id?: string | null;
18
+ }
19
+ export interface HopRecord {
20
+ from_entity_id: string;
21
+ from_label: string;
22
+ relation: string;
23
+ direction: string;
24
+ to_entity_id: string;
25
+ to_label: string;
26
+ supporting_event_ids: string[];
27
+ }
28
+ export interface QueryCandidate {
29
+ value: string;
30
+ score: number;
31
+ evidence: string;
32
+ supporting_event_ids: string[];
33
+ support_count?: number | null;
34
+ distinct_session_count?: number | null;
35
+ entity_id?: string | null;
36
+ scope?: string | null;
37
+ observed_path?: HopRecord[];
38
+ path_collision_count?: number;
39
+ }
40
+ export interface MemoryCandidateGroup {
41
+ value: string;
42
+ score: number;
43
+ evidence: string;
44
+ support_count: number;
45
+ supporting_event_ids: string[];
46
+ entity_ids: string[];
47
+ scopes: string[];
48
+ via_relations: string[];
49
+ via_anchor_entity_ids: string[];
50
+ observed_path: HopRecord[];
51
+ path_collision_count: number;
52
+ }
53
+ export interface MemoryBlock {
54
+ groups: MemoryCandidateGroup[];
55
+ no_data_reason?: string | null;
56
+ notes: string[];
57
+ }
58
+ export interface Warning {
59
+ code: string;
60
+ message: string;
61
+ }
62
+ export interface ErrorObject {
63
+ code: string;
64
+ message: string;
65
+ details?: Record<string, unknown>;
66
+ }
67
+ export declare function errorSubCode(err: ErrorObject | null | undefined): string;
68
+ export interface ResponseEnvelope {
69
+ protocol_version: number;
70
+ bundle_version?: string;
71
+ bundle_created_at?: string | null;
72
+ bundle_dir?: string;
73
+ request_id: string;
74
+ candidates: QueryCandidate[];
75
+ memory_block?: MemoryBlock | null;
76
+ warnings: Warning[];
77
+ reason: string;
78
+ error?: ErrorObject | null;
79
+ latency_ms: number;
80
+ }
81
+ export interface ReloadResponse {
82
+ protocol_version: number;
83
+ request_id: string;
84
+ swapped: boolean;
85
+ trigger: string;
86
+ reason: string;
87
+ previous_bundle_dir?: string | null;
88
+ current_bundle_dir?: string | null;
89
+ reload_duration_ms: number;
90
+ warnings: Warning[];
91
+ error?: ErrorObject | null;
92
+ }
93
+ export interface HealthPayload {
94
+ ready: boolean;
95
+ compatibility: string;
96
+ bundle_version?: string;
97
+ bundle_created_at?: string | null;
98
+ bundle_dir?: string;
99
+ last_reload_age_secs?: number | null;
100
+ last_reload_trigger?: string | null;
101
+ protocol_version: number;
102
+ uptime_secs: number;
103
+ error?: ErrorObject | null;
104
+ status_message?: string;
105
+ }
106
+ export type ServiceStatus = "disabled" | "initializing" | "ready" | "degraded" | "unavailable";
107
+ export interface MemoryRecallArgs {
108
+ mode?: string;
109
+ anchor_mentions: string[];
110
+ relation_constraints?: string[];
111
+ candidate_type?: string | null;
112
+ scope_filter?: string[];
113
+ target_slot?: string;
114
+ time_window?: string | null;
115
+ evidence_budget?: number;
116
+ result_limit?: number;
117
+ }
118
+ export interface ToolResult {
119
+ content: string;
120
+ isError?: boolean;
121
+ }
122
+ export type ErrorClass = "ok" | "retryable" | "permanent" | "unavailable";
123
+ export interface MemoryQuerier {
124
+ status(): ServiceStatus;
125
+ query(intent: QueryIntent, signal?: AbortSignal): Promise<{
126
+ env: ResponseEnvelope | null;
127
+ errorClass: ErrorClass;
128
+ transportError?: Error;
129
+ }>;
130
+ }
131
+ export interface FallbackQuery {
132
+ sessionKeyword(query: string, limit: number): Promise<unknown[]>;
133
+ memoryFileSnippet(query: string): Promise<string>;
134
+ }
135
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,mFAAmF;AAEnF,MAAM,MAAM,SAAS,GACjB,iBAAiB,GACjB,YAAY,GACZ,oBAAoB,CAAC;AAEzB,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,SAAS,CAAC;IAChB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;IAChC,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,WAAW,CAAC,EAAE,EAAE,GAAG,MAAM,GAAG,MAAM,CAAC;IACnC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,WAAW,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B;AAED,MAAM,WAAW,SAAS;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAA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AAc,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;IACjE,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACnD"}
package/dist/types.js ADDED
@@ -0,0 +1,8 @@
1
+ /** Wire types mirroring Kocoro internal/memory/types.go (TLM sidecar contract). */
2
+ export function errorSubCode(err) {
3
+ if (!err?.details)
4
+ return "";
5
+ const v = err.details.sub_code;
6
+ return typeof v === "string" ? v : "";
7
+ }
8
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,mFAAmF;AA+EnF,MAAM,UAAU,YAAY,CAAC,GAAmC;IAC9D,IAAI,CAAC,GAAG,EAAE,OAAO;QAAE,OAAO,EAAE,CAAC;IAC7B,MAAM,CAAC,GAAG,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC;IAC/B,OAAO,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACxC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@chendpoc/pi-memory",
3
+ "version": "0.1.0",
4
+ "description": "Local episodic memory for Pi agent — TLM sidecar + memory_recall (mode B)",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./extension": {
14
+ "types": "./dist/pi-extension.d.ts",
15
+ "import": "./dist/pi-extension.js"
16
+ }
17
+ },
18
+ "bin": {
19
+ "pi-memory": "./dist/cli.js"
20
+ },
21
+ "scripts": {
22
+ "build": "tsc",
23
+ "test": "vitest run",
24
+ "test:watch": "vitest",
25
+ "typecheck": "tsc --noEmit"
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "src/**/*.ts",
30
+ "!src/**/*.test.ts",
31
+ "README.md"
32
+ ],
33
+ "engines": {
34
+ "node": ">=20"
35
+ },
36
+ "keywords": [
37
+ "pi-package",
38
+ "pi-extension",
39
+ "pi-agent",
40
+ "tlm",
41
+ "episodic-memory",
42
+ "sidecar"
43
+ ],
44
+ "license": "MIT",
45
+ "author": "chendpoc",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/chendpoc/pi-memory.git"
49
+ },
50
+ "homepage": "https://github.com/chendpoc/pi-memory#readme",
51
+ "pi": {
52
+ "extensions": [
53
+ "./src/pi-extension.ts"
54
+ ]
55
+ },
56
+ "peerDependencies": {
57
+ "@earendil-works/pi-agent-core": "*",
58
+ "@earendil-works/pi-ai": "*",
59
+ "@earendil-works/pi-coding-agent": "*",
60
+ "typebox": "*"
61
+ },
62
+ "optionalDependencies": {
63
+ "better-sqlite3": "^12.10.0"
64
+ },
65
+ "devDependencies": {
66
+ "@earendil-works/pi-agent-core": "*",
67
+ "@earendil-works/pi-ai": "*",
68
+ "@earendil-works/pi-coding-agent": "*",
69
+ "@types/better-sqlite3": "^7.6.0",
70
+ "@types/node": "^22.10.0",
71
+ "typebox": "*",
72
+ "typescript": "^5.7.2",
73
+ "vitest": "^3.0.0"
74
+ },
75
+ "allowScripts": {
76
+ "better-sqlite3@12.11.1": true
77
+ }
78
+ }
@@ -0,0 +1,233 @@
1
+ import { complete, getEnvApiKey, getModels } from "@earendil-works/pi-ai/compat";
2
+ import type { Api, Model, ProviderEnv, ToolCall } from "@earendil-works/pi-ai";
3
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
4
+ import { Type } from "typebox";
5
+
6
+ import {
7
+ COMPILE_MEMORY_INTENTS_PARAMETERS,
8
+ MEMORY_HELPER_TOOL_NAME,
9
+ type CompileMemoryIntentsResult,
10
+ type MemoryHelperLLM,
11
+ } from "../preflight/detectIntents.js";
12
+ import type { LLMClient } from "../trainer/llmExtractor.js";
13
+
14
+ export const DEFAULT_HELPER_PROVIDER = "deepseek";
15
+ export const DEFAULT_HELPER_MODEL = "deepseek-v4-flash";
16
+
17
+ const CompileMemoryIntentsParams = Type.Unsafe(COMPILE_MEMORY_INTENTS_PARAMETERS);
18
+
19
+ export function parseModelSpec(
20
+ spec: string | boolean | undefined,
21
+ defaultProvider = DEFAULT_HELPER_PROVIDER,
22
+ defaultModelId = DEFAULT_HELPER_MODEL,
23
+ ): { provider: string; modelId: string } {
24
+ if (typeof spec !== "string" || !spec.trim()) {
25
+ return { provider: defaultProvider, modelId: defaultModelId };
26
+ }
27
+ const trimmed = spec.trim();
28
+ const slash = trimmed.indexOf("/");
29
+ if (slash === -1) {
30
+ return { provider: defaultProvider, modelId: trimmed };
31
+ }
32
+ return {
33
+ provider: trimmed.slice(0, slash),
34
+ modelId: trimmed.slice(slash + 1),
35
+ };
36
+ }
37
+
38
+ async function resolveModelAuth(
39
+ ctx: ExtensionContext,
40
+ provider: string,
41
+ modelId: string,
42
+ ): Promise<{ model: Model<Api>; apiKey: string; headers?: Record<string, string>; env?: ProviderEnv } | null> {
43
+ const model = ctx.modelRegistry.find(provider, modelId);
44
+ if (!model) return null;
45
+
46
+ const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
47
+ if (!auth.ok || !auth.apiKey) return null;
48
+
49
+ return {
50
+ model,
51
+ apiKey: auth.apiKey,
52
+ headers: auth.headers,
53
+ env: auth.env,
54
+ };
55
+ }
56
+
57
+ function extractTextFromResponse(content: Array<{ type: string; text?: string }>): string {
58
+ return content
59
+ .filter((block): block is { type: "text"; text: string } => block.type === "text")
60
+ .map((block) => block.text)
61
+ .join("\n");
62
+ }
63
+
64
+ function findToolCall(
65
+ content: Array<{ type: string; name?: string; arguments?: Record<string, unknown> }>,
66
+ toolName: string,
67
+ ): ToolCall | null {
68
+ for (const block of content) {
69
+ if (block.type === "toolCall" && block.name === toolName && block.arguments) {
70
+ return block as ToolCall;
71
+ }
72
+ }
73
+ return null;
74
+ }
75
+
76
+ function buildMemoryHelperLLM(
77
+ resolved: NonNullable<Awaited<ReturnType<typeof resolveModelAuth>>>,
78
+ ): MemoryHelperLLM {
79
+ return {
80
+ async compileIntents(text: string, signal?: AbortSignal): Promise<CompileMemoryIntentsResult> {
81
+ const response = await complete(
82
+ resolved.model,
83
+ {
84
+ messages: [
85
+ {
86
+ role: "user",
87
+ content: [
88
+ {
89
+ type: "text",
90
+ text: `Analyze whether the user message requires recalling private episodic memory.\n\n<message>\n${text}\n</message>`,
91
+ },
92
+ ],
93
+ timestamp: Date.now(),
94
+ },
95
+ ],
96
+ tools: [
97
+ {
98
+ name: MEMORY_HELPER_TOOL_NAME,
99
+ description:
100
+ "Decide whether to recall private episodic memory and compile structured query intents.",
101
+ parameters: CompileMemoryIntentsParams,
102
+ },
103
+ ],
104
+ },
105
+ {
106
+ apiKey: resolved.apiKey,
107
+ headers: resolved.headers,
108
+ env: resolved.env,
109
+ signal,
110
+ toolChoice: { type: "tool", name: MEMORY_HELPER_TOOL_NAME },
111
+ },
112
+ );
113
+
114
+ const toolCall = findToolCall(response.content, MEMORY_HELPER_TOOL_NAME);
115
+ if (toolCall?.arguments) {
116
+ return toolCall.arguments as CompileMemoryIntentsResult;
117
+ }
118
+
119
+ const raw = extractTextFromResponse(response.content);
120
+ if (!raw.trim()) {
121
+ return { should_recall: false, intents: [] };
122
+ }
123
+ return JSON.parse(raw) as CompileMemoryIntentsResult;
124
+ },
125
+ };
126
+ }
127
+
128
+ /** Resolve helper LLM when model + auth are available; otherwise null (regex-only preflight). */
129
+ export async function resolveMemoryHelperLLM(
130
+ ctx: ExtensionContext,
131
+ modelSpec: string | boolean | undefined,
132
+ ): Promise<MemoryHelperLLM | null> {
133
+ const { provider, modelId } = parseModelSpec(modelSpec);
134
+ const resolved = await resolveModelAuth(ctx, provider, modelId);
135
+ if (!resolved) return null;
136
+ return buildMemoryHelperLLM(resolved);
137
+ }
138
+
139
+ /** @alias resolveMemoryHelperLLM */
140
+ export const createMemoryHelperLLM = resolveMemoryHelperLLM;
141
+
142
+ export function createPiLLMClient(
143
+ ctx: ExtensionContext,
144
+ modelSpec: string | boolean | undefined,
145
+ ): LLMClient | null {
146
+ const { provider, modelId } = parseModelSpec(modelSpec);
147
+
148
+ return {
149
+ async complete(prompt: string): Promise<string> {
150
+ const resolved = await resolveModelAuth(ctx, provider, modelId);
151
+ if (!resolved) {
152
+ throw new Error(`LLM model not available: ${provider}/${modelId}`);
153
+ }
154
+
155
+ const response = await complete(
156
+ resolved.model,
157
+ {
158
+ messages: [
159
+ {
160
+ role: "user",
161
+ content: [{ type: "text", text: prompt }],
162
+ timestamp: Date.now(),
163
+ },
164
+ ],
165
+ },
166
+ {
167
+ apiKey: resolved.apiKey,
168
+ headers: resolved.headers,
169
+ env: resolved.env,
170
+ maxTokens: 8192,
171
+ },
172
+ );
173
+
174
+ const text = extractTextFromResponse(response.content);
175
+ if (!text.trim()) {
176
+ throw new Error("LLM response was empty");
177
+ }
178
+ return text;
179
+ },
180
+ };
181
+ }
182
+
183
+ /** Standalone LLM client for CLI usage without ExtensionContext. */
184
+ export function createStandaloneLLMClient(
185
+ modelSpec?: string,
186
+ env: NodeJS.ProcessEnv = process.env,
187
+ ): LLMClient {
188
+ const { provider, modelId } = parseModelSpec(modelSpec);
189
+ const providerEnv = toProviderEnv(env);
190
+ const model = getModels(provider as Parameters<typeof getModels>[0]).find((m) => m.id === modelId);
191
+ if (!model) {
192
+ throw new Error(`Model not found: ${provider}/${modelId}`);
193
+ }
194
+
195
+ const apiKey = getEnvApiKey(provider, providerEnv);
196
+ if (!apiKey) {
197
+ throw new Error(`No API key for ${provider} — set the provider env var or use regex extractor`);
198
+ }
199
+
200
+ return {
201
+ async complete(prompt: string): Promise<string> {
202
+ const response = await complete(
203
+ model,
204
+ {
205
+ messages: [
206
+ {
207
+ role: "user",
208
+ content: [{ type: "text", text: prompt }],
209
+ timestamp: Date.now(),
210
+ },
211
+ ],
212
+ },
213
+ {
214
+ apiKey,
215
+ maxTokens: 8192,
216
+ env: providerEnv,
217
+ },
218
+ );
219
+
220
+ const text = extractTextFromResponse(response.content);
221
+ if (!text.trim()) {
222
+ throw new Error("LLM response was empty");
223
+ }
224
+ return text;
225
+ },
226
+ };
227
+ }
228
+
229
+ function toProviderEnv(env: NodeJS.ProcessEnv): ProviderEnv {
230
+ return Object.fromEntries(
231
+ Object.entries(env).filter((entry): entry is [string, string] => entry[1] !== undefined),
232
+ );
233
+ }
@@ -0,0 +1,206 @@
1
+ import { createHash } from "node:crypto";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+
5
+ import type { BundleManifest } from "../sidecar/bundle.js";
6
+
7
+ export interface InstallBundleOptions {
8
+ bundleRoot: string;
9
+ sourceDir: string;
10
+ /** Number of old bundles to keep after install (default 3). */
11
+ retain?: number;
12
+ }
13
+
14
+ export interface InstallBundleResult {
15
+ bundle_ts: string;
16
+ bundle_version: string;
17
+ installed_dir: string;
18
+ files_copied: number;
19
+ }
20
+
21
+ /**
22
+ * Enforces [0.4.0, 0.7.0) — producers guarantee additive minor bumps in this
23
+ * range; breaking schema changes move to 0.7.0+ to trip this gate.
24
+ * Ported from Kocoro internal/memory/bundle.go versionInRange.
25
+ */
26
+ export function versionInRange(v: string): boolean {
27
+ const parts = v.split(".");
28
+ if (parts.length !== 3) return false;
29
+ const [maj, min, pat] = parts.map(Number);
30
+ if (maj !== 0 || isNaN(min!) || isNaN(pat!)) return false;
31
+ if (min! < 4 || min! >= 7) return false;
32
+ return pat! >= 0;
33
+ }
34
+
35
+ /**
36
+ * Keep the newest `keep` bundle dirs plus the current symlink target.
37
+ * Best-effort — failures are silently ignored.
38
+ * Ported from Kocoro internal/memory/bundle.go Puller.retain.
39
+ */
40
+ export async function retainBundles(bundleRoot: string, keep: number): Promise<void> {
41
+ const bundlesDir = path.join(bundleRoot, "bundles");
42
+ let names: string[];
43
+ try {
44
+ names = await fs.readdir(bundlesDir);
45
+ } catch {
46
+ return;
47
+ }
48
+ const dirs: string[] = [];
49
+ for (const name of names) {
50
+ try {
51
+ const st = await fs.stat(path.join(bundlesDir, name));
52
+ if (st.isDirectory()) dirs.push(name);
53
+ } catch { /* skip unreadable */ }
54
+ }
55
+ dirs.sort().reverse();
56
+ if (dirs.length <= keep) return;
57
+
58
+ let currentTarget = "";
59
+ try {
60
+ const target = await fs.readlink(path.join(bundleRoot, "current"));
61
+ currentTarget = path.basename(target);
62
+ } catch { /* no current pointer */ }
63
+
64
+ const keepSet = new Set<string>();
65
+ for (let i = 0; i < Math.min(keep, dirs.length); i++) {
66
+ keepSet.add(dirs[i]!);
67
+ }
68
+ if (currentTarget) keepSet.add(currentTarget);
69
+
70
+ for (const d of dirs) {
71
+ if (!keepSet.has(d)) {
72
+ await fs.rm(path.join(bundlesDir, d), { recursive: true, force: true }).catch(() => {});
73
+ }
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Path sandboxing rules from Kocoro bundle.go validateManifestPath (§4.2).
79
+ */
80
+ export function validateManifestPath(rel: string, stagingDir: string): string | null {
81
+ if (!rel) return "empty path";
82
+ if (rel.includes("\0")) return "null byte in path";
83
+ if (path.isAbsolute(rel)) return "absolute path";
84
+ const cleaned = path.normalize(rel);
85
+ if (
86
+ cleaned === ".." ||
87
+ cleaned.startsWith(`..${path.sep}`) ||
88
+ cleaned.includes(`${path.sep}..`)
89
+ ) {
90
+ return "contains parent traversal";
91
+ }
92
+ const abs = path.join(stagingDir, cleaned);
93
+ const cleanedAbs = path.normalize(abs);
94
+ const prefix = path.normalize(stagingDir) + path.sep;
95
+ if (!cleanedAbs.startsWith(prefix)) {
96
+ return "escapes staging dir";
97
+ }
98
+ return null;
99
+ }
100
+
101
+ async function sha256File(filePath: string): Promise<string> {
102
+ const data = await fs.readFile(filePath);
103
+ return createHash("sha256").update(data).digest("hex");
104
+ }
105
+
106
+ /**
107
+ * Points <bundleRoot>/current at finalDir. On Windows uses a directory junction
108
+ * (unprivileged, no Developer Mode needed — os.Symlink would fail with
109
+ * ERROR_PRIVILEGE_NOT_HELD on stock hosts). On POSIX uses atomic tmp-symlink +
110
+ * rename. Ported from Kocoro bundle_link_{unix,windows}.go.
111
+ */
112
+ async function swapCurrent(bundleRoot: string, finalDir: string): Promise<void> {
113
+ const currentLink = path.join(bundleRoot, "current");
114
+
115
+ if (process.platform === "win32") {
116
+ try { await fs.rm(currentLink, { recursive: true, force: true }); } catch { /* absent */ }
117
+ await fs.symlink(finalDir, currentLink, "junction");
118
+ return;
119
+ }
120
+
121
+ const tmpLink = path.join(bundleRoot, "current.tmp");
122
+ try { await fs.unlink(tmpLink); } catch { /* absent */ }
123
+ await fs.symlink(finalDir, tmpLink);
124
+ await fs.rename(tmpLink, currentLink);
125
+ }
126
+
127
+ /**
128
+ * Install a local bundle directory into bundleRoot (staging → bundles/<ts> → current).
129
+ * Verifies per-file sha256 when manifest lists hashes.
130
+ */
131
+ export async function installBundle(
132
+ opts: InstallBundleOptions,
133
+ ): Promise<InstallBundleResult> {
134
+ const sourceDir = path.resolve(opts.sourceDir);
135
+ const bundleRoot = path.resolve(opts.bundleRoot);
136
+
137
+ const manifestPath = path.join(sourceDir, "manifest.json");
138
+ const raw = await fs.readFile(manifestPath, "utf8");
139
+ const manifest = JSON.parse(raw) as BundleManifest;
140
+
141
+ if (!manifest.bundle_ts?.trim()) {
142
+ throw new Error("manifest missing bundle_ts");
143
+ }
144
+ if (!versionInRange(manifest.bundle_version ?? "")) {
145
+ throw new Error(
146
+ `bundle_version "${manifest.bundle_version}" outside supported range [0.4.0, 0.7.0)`,
147
+ );
148
+ }
149
+
150
+ await fs.mkdir(bundleRoot, { recursive: true, mode: 0o700 });
151
+
152
+ const staging = path.join(bundleRoot, "staging", manifest.bundle_ts);
153
+ await fs.rm(staging, { recursive: true, force: true });
154
+ await fs.mkdir(staging, { recursive: true, mode: 0o700 });
155
+
156
+ let filesCopied = 0;
157
+ for (const f of manifest.files ?? []) {
158
+ const err = validateManifestPath(f.path, staging);
159
+ if (err) {
160
+ await fs.rm(staging, { recursive: true, force: true });
161
+ throw new Error(`unsafe manifest path ${f.path}: ${err}`);
162
+ }
163
+ const src = path.join(sourceDir, path.normalize(f.path));
164
+ const dest = path.join(staging, path.normalize(f.path));
165
+ await fs.mkdir(path.dirname(dest), { recursive: true, mode: 0o700 });
166
+ await fs.copyFile(src, dest, fs.constants.COPYFILE_EXCL).catch(async () => {
167
+ await fs.copyFile(src, dest);
168
+ });
169
+
170
+ if (f.sha256) {
171
+ const got = await sha256File(dest);
172
+ if (got !== f.sha256) {
173
+ await fs.rm(staging, { recursive: true, force: true });
174
+ throw new Error(`sha256 mismatch on ${f.path}: got ${got} want ${f.sha256}`);
175
+ }
176
+ }
177
+ filesCopied++;
178
+ }
179
+
180
+ // Copy manifest itself if not listed in files[]
181
+ const manifestDest = path.join(staging, "manifest.json");
182
+ try {
183
+ await fs.access(manifestDest);
184
+ } catch {
185
+ await fs.copyFile(manifestPath, manifestDest);
186
+ }
187
+
188
+ const bundlesDir = path.join(bundleRoot, "bundles");
189
+ await fs.mkdir(bundlesDir, { recursive: true, mode: 0o700 });
190
+ const finalDir = path.join(bundlesDir, manifest.bundle_ts);
191
+ await fs.rm(finalDir, { recursive: true, force: true });
192
+ await fs.rename(staging, finalDir);
193
+ await swapCurrent(bundleRoot, finalDir);
194
+
195
+ const keep = opts.retain ?? 3;
196
+ if (keep > 0) {
197
+ await retainBundles(bundleRoot, keep);
198
+ }
199
+
200
+ return {
201
+ bundle_ts: manifest.bundle_ts,
202
+ bundle_version: manifest.bundle_version,
203
+ installed_dir: finalDir,
204
+ files_copied: filesCopied,
205
+ };
206
+ }