@oyasmi/pipiclaw 0.5.1 → 0.5.3

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 (193) hide show
  1. package/README.md +308 -209
  2. package/dist/agent/channel-runner.d.ts +47 -0
  3. package/dist/agent/channel-runner.js +441 -0
  4. package/dist/agent/index.d.ts +3 -0
  5. package/dist/agent/index.js +2 -0
  6. package/dist/agent/progress-formatter.d.ts +4 -0
  7. package/dist/agent/progress-formatter.js +52 -0
  8. package/dist/agent/run-queue.d.ts +7 -0
  9. package/dist/agent/run-queue.js +26 -0
  10. package/dist/agent/runner-factory.d.ts +3 -0
  11. package/dist/agent/runner-factory.js +10 -0
  12. package/dist/agent/session-events.d.ts +14 -0
  13. package/dist/agent/session-events.js +215 -0
  14. package/dist/agent/session-resource-gate.d.ts +10 -0
  15. package/dist/agent/session-resource-gate.js +44 -0
  16. package/dist/agent/type-guards.d.ts +22 -0
  17. package/dist/agent/type-guards.js +106 -0
  18. package/dist/agent/types.d.ts +160 -0
  19. package/dist/agent/types.js +22 -0
  20. package/dist/agent.d.ts +2 -16
  21. package/dist/agent.js +1 -782
  22. package/dist/command-extension.d.ts +0 -1
  23. package/dist/command-extension.js +0 -1
  24. package/dist/commands.d.ts +0 -1
  25. package/dist/commands.js +0 -1
  26. package/dist/config-loader.d.ts +0 -1
  27. package/dist/config-loader.js +1 -2
  28. package/dist/context.d.ts +58 -15
  29. package/dist/context.js +50 -8
  30. package/dist/index.d.ts +12 -13
  31. package/dist/index.js +12 -13
  32. package/dist/log.d.ts +0 -1
  33. package/dist/log.js +0 -1
  34. package/dist/main.d.ts +0 -1
  35. package/dist/main.js +5 -405
  36. package/dist/memory/bootstrap.d.ts +6 -0
  37. package/dist/memory/bootstrap.js +46 -0
  38. package/dist/{memory-candidates.d.ts → memory/candidates.d.ts} +1 -1
  39. package/dist/{memory-candidates.js → memory/candidates.js} +33 -21
  40. package/dist/memory/chinese-words.d.ts +1 -0
  41. package/dist/memory/chinese-words.js +273 -0
  42. package/dist/{memory-consolidation.d.ts → memory/consolidation.d.ts} +0 -1
  43. package/dist/{memory-consolidation.js → memory/consolidation.js} +26 -35
  44. package/dist/{memory-files.d.ts → memory/files.d.ts} +0 -6
  45. package/dist/{memory-files.js → memory/files.js} +11 -36
  46. package/dist/{memory-lifecycle.d.ts → memory/lifecycle.d.ts} +23 -6
  47. package/dist/memory/lifecycle.js +246 -0
  48. package/dist/{memory-recall.d.ts → memory/recall.d.ts} +2 -2
  49. package/dist/memory/recall.js +501 -0
  50. package/dist/{session-memory.d.ts → memory/session.d.ts} +1 -1
  51. package/dist/{session-memory.js → memory/session.js} +31 -62
  52. package/dist/model-utils.d.ts +0 -1
  53. package/dist/model-utils.js +0 -1
  54. package/dist/paths.d.ts +0 -1
  55. package/dist/paths.js +0 -1
  56. package/dist/prompt-builder.d.ts +0 -1
  57. package/dist/prompt-builder.js +0 -1
  58. package/dist/runtime/bootstrap.d.ts +47 -0
  59. package/dist/runtime/bootstrap.js +450 -0
  60. package/dist/{delivery.d.ts → runtime/delivery.d.ts} +0 -1
  61. package/dist/{delivery.js → runtime/delivery.js} +1 -2
  62. package/dist/{dingtalk.d.ts → runtime/dingtalk.d.ts} +10 -1
  63. package/dist/{dingtalk.js → runtime/dingtalk.js} +87 -28
  64. package/dist/{events.d.ts → runtime/events.d.ts} +0 -1
  65. package/dist/{events.js → runtime/events.js} +1 -2
  66. package/dist/{store.d.ts → runtime/store.d.ts} +5 -1
  67. package/dist/{store.js → runtime/store.js} +60 -20
  68. package/dist/sandbox.d.ts +0 -1
  69. package/dist/sandbox.js +1 -2
  70. package/dist/{llm-json.d.ts → shared/llm-json.d.ts} +0 -1
  71. package/dist/{llm-json.js → shared/llm-json.js} +0 -1
  72. package/dist/shared/markdown-sections.d.ts +6 -0
  73. package/dist/{markdown-sections.js → shared/markdown-sections.js} +10 -4
  74. package/dist/{shell-escape.d.ts → shared/shell-escape.d.ts} +0 -1
  75. package/dist/{shell-escape.js → shared/shell-escape.js} +0 -1
  76. package/dist/shared/text-utils.d.ts +9 -0
  77. package/dist/shared/text-utils.js +36 -0
  78. package/dist/shared/type-guards.d.ts +5 -0
  79. package/dist/shared/type-guards.js +12 -0
  80. package/dist/shared/types.d.ts +14 -0
  81. package/dist/shared/types.js +1 -0
  82. package/dist/sidecar-worker.d.ts +0 -1
  83. package/dist/sidecar-worker.js +1 -8
  84. package/dist/{sub-agents.d.ts → subagents/discovery.d.ts} +0 -1
  85. package/dist/{sub-agents.js → subagents/discovery.js} +2 -3
  86. package/dist/{tools/subagent.d.ts → subagents/tool.d.ts} +2 -16
  87. package/dist/{tools/subagent.js → subagents/tool.js} +16 -38
  88. package/dist/tools/attach.d.ts +0 -1
  89. package/dist/tools/attach.js +0 -1
  90. package/dist/tools/bash.d.ts +0 -1
  91. package/dist/tools/bash.js +0 -1
  92. package/dist/tools/edit.d.ts +0 -1
  93. package/dist/tools/edit.js +1 -2
  94. package/dist/tools/index.d.ts +1 -2
  95. package/dist/tools/index.js +1 -2
  96. package/dist/tools/read.d.ts +0 -1
  97. package/dist/tools/read.js +1 -2
  98. package/dist/tools/truncate.d.ts +0 -1
  99. package/dist/tools/truncate.js +0 -1
  100. package/dist/tools/write-content.d.ts +0 -1
  101. package/dist/tools/write-content.js +1 -2
  102. package/dist/tools/write.d.ts +0 -1
  103. package/dist/tools/write.js +0 -1
  104. package/package.json +9 -3
  105. package/CHANGELOG.md +0 -47
  106. package/dist/agent.d.ts.map +0 -1
  107. package/dist/agent.js.map +0 -1
  108. package/dist/command-extension.d.ts.map +0 -1
  109. package/dist/command-extension.js.map +0 -1
  110. package/dist/commands.d.ts.map +0 -1
  111. package/dist/commands.js.map +0 -1
  112. package/dist/config-loader.d.ts.map +0 -1
  113. package/dist/config-loader.js.map +0 -1
  114. package/dist/context.d.ts.map +0 -1
  115. package/dist/context.js.map +0 -1
  116. package/dist/delivery.d.ts.map +0 -1
  117. package/dist/delivery.js.map +0 -1
  118. package/dist/dingtalk.d.ts.map +0 -1
  119. package/dist/dingtalk.js.map +0 -1
  120. package/dist/events.d.ts.map +0 -1
  121. package/dist/events.js.map +0 -1
  122. package/dist/index.d.ts.map +0 -1
  123. package/dist/index.js.map +0 -1
  124. package/dist/llm-json.d.ts.map +0 -1
  125. package/dist/llm-json.js.map +0 -1
  126. package/dist/log.d.ts.map +0 -1
  127. package/dist/log.js.map +0 -1
  128. package/dist/main.d.ts.map +0 -1
  129. package/dist/main.js.map +0 -1
  130. package/dist/markdown-sections.d.ts +0 -6
  131. package/dist/markdown-sections.d.ts.map +0 -1
  132. package/dist/markdown-sections.js.map +0 -1
  133. package/dist/memory-candidates.d.ts.map +0 -1
  134. package/dist/memory-candidates.js.map +0 -1
  135. package/dist/memory-consolidation.d.ts.map +0 -1
  136. package/dist/memory-consolidation.js.map +0 -1
  137. package/dist/memory-files.d.ts.map +0 -1
  138. package/dist/memory-files.js.map +0 -1
  139. package/dist/memory-lifecycle.d.ts.map +0 -1
  140. package/dist/memory-lifecycle.js +0 -150
  141. package/dist/memory-lifecycle.js.map +0 -1
  142. package/dist/memory-recall.d.ts.map +0 -1
  143. package/dist/memory-recall.js +0 -218
  144. package/dist/memory-recall.js.map +0 -1
  145. package/dist/model-utils.d.ts.map +0 -1
  146. package/dist/model-utils.js.map +0 -1
  147. package/dist/paths.d.ts.map +0 -1
  148. package/dist/paths.js.map +0 -1
  149. package/dist/prompt-builder.d.ts.map +0 -1
  150. package/dist/prompt-builder.js.map +0 -1
  151. package/dist/sandbox.d.ts.map +0 -1
  152. package/dist/sandbox.js.map +0 -1
  153. package/dist/session-memory-files.d.ts +0 -2
  154. package/dist/session-memory-files.d.ts.map +0 -1
  155. package/dist/session-memory-files.js +0 -2
  156. package/dist/session-memory-files.js.map +0 -1
  157. package/dist/session-memory.d.ts.map +0 -1
  158. package/dist/session-memory.js.map +0 -1
  159. package/dist/shell-escape.d.ts.map +0 -1
  160. package/dist/shell-escape.js.map +0 -1
  161. package/dist/sidecar-worker.d.ts.map +0 -1
  162. package/dist/sidecar-worker.js.map +0 -1
  163. package/dist/store.d.ts.map +0 -1
  164. package/dist/store.js.map +0 -1
  165. package/dist/sub-agents.d.ts.map +0 -1
  166. package/dist/sub-agents.js.map +0 -1
  167. package/dist/tools/attach.d.ts.map +0 -1
  168. package/dist/tools/attach.js.map +0 -1
  169. package/dist/tools/bash.d.ts.map +0 -1
  170. package/dist/tools/bash.js.map +0 -1
  171. package/dist/tools/edit.d.ts.map +0 -1
  172. package/dist/tools/edit.js.map +0 -1
  173. package/dist/tools/index.d.ts.map +0 -1
  174. package/dist/tools/index.js.map +0 -1
  175. package/dist/tools/read.d.ts.map +0 -1
  176. package/dist/tools/read.js.map +0 -1
  177. package/dist/tools/subagent.d.ts.map +0 -1
  178. package/dist/tools/subagent.js.map +0 -1
  179. package/dist/tools/truncate.d.ts.map +0 -1
  180. package/dist/tools/truncate.js.map +0 -1
  181. package/dist/tools/write-content.d.ts.map +0 -1
  182. package/dist/tools/write-content.js.map +0 -1
  183. package/dist/tools/write.d.ts.map +0 -1
  184. package/dist/tools/write.js.map +0 -1
  185. package/docs/improve-memory/design.md +0 -537
  186. package/docs/improve-memory/interfaces-and-tests.md +0 -473
  187. package/docs/improve-memory/spec.md +0 -357
  188. package/docs/memory-rfc.md +0 -297
  189. package/docs/proj-review.md +0 -188
  190. package/docs/subagent/pi-subagent-analyse.txt +0 -190
  191. package/docs/subagent/pi-subagent-design.txt +0 -266
  192. package/docs/subagent/pi-subagent-phase1-plan.txt +0 -529
  193. package/docs/test-supplementation-plan.md +0 -553
@@ -1 +0,0 @@
1
- {"version":3,"file":"memory-files.js","sourceRoot":"","sources":["../src/memory-files.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAC1D,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,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QAAQ,GAAsB,EAAE,CAAC;IACvC,IAAI,cAAc,GAAG,EAAE,CAAC;IACxB,IAAI,YAAY,GAAa,EAAE,CAAC;IAEhC,MAAM,KAAK,GAAG,GAAS,EAAE;QACxB,IAAI,CAAC,cAAc,EAAE,CAAC;YACrB,OAAO;QACR,CAAC;QACD,QAAQ,CAAC,IAAI,CAAC;YACb,OAAO,EAAE,cAAc;YACvB,OAAO,EAAE,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE;SACvC,CAAC,CAAC;IACJ,CAAC,CAAC;IAEF,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5B,KAAK,EAAE,CAAC;YACR,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;YACtC,YAAY,GAAG,EAAE,CAAC;YAClB,SAAS;QACV,CAAC;QACD,IAAI,cAAc,EAAE,CAAC;YACpB,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC;IACF,CAAC;IAED,KAAK,EAAE,CAAC;IACR,OAAO,QAAQ,CAAC;AACjB,CAAC","sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from \"fs\";\nimport { mkdir, readFile, rename, writeFile } from \"fs/promises\";\nimport { dirname, join } from \"path\";\n\nconst DEFAULT_CHANNEL_MEMORY = `# Channel Memory\n\nThis file stores durable channel-specific memory.\n\n- It is not preloaded into session context.\n- Read it on demand when prior decisions, preferences, or long-running work matter.\n- The runtime may append updates here during consolidation.\n\n## Durable Facts\n\n<!-- Stable facts, preferences, and ongoing commitments can accumulate here. -->\n`;\n\nconst DEFAULT_CHANNEL_HISTORY = `# Channel History\n\nThis file stores summarized older channel history.\n\n- It is not preloaded into session context.\n- Read it on demand when older context matters.\n- The runtime may append and fold history blocks here during consolidation.\n`;\n\nconst DEFAULT_CHANNEL_SESSION = `# Session Title\n\n<!-- A short title for the current active work in this channel. -->\n\n# Current State\n\n<!-- What is actively being worked on right now. -->\n\n# User Intent\n\n<!-- What the user is currently trying to achieve. -->\n\n# Active Files\n\n<!-- Important files or directories currently in focus. -->\n\n# Decisions\n\n<!-- Recent decisions that matter to the current work. -->\n\n# Constraints\n\n<!-- Current constraints, assumptions, or important guardrails. -->\n\n# Errors & Corrections\n\n<!-- Recent failures, corrections, and things to avoid repeating. -->\n\n# Next Steps\n\n<!-- Likely next actions if work resumes later. -->\n\n# Worklog\n\n<!-- Very terse notes about recent progress. -->\n`;\n\nexport interface MemoryUpdateBlock {\n\ttimestamp: string;\n\tentries: string[];\n}\n\nexport interface HistoryBlock {\n\ttimestamp: string;\n\tcontent: string;\n}\n\nfunction normalizeContent(content: string): string {\n\treturn content.trim().length > 0 ? `${content.trim()}\\n` : \"\";\n}\n\nasync function writeAtomically(path: string, content: string): Promise<void> {\n\tawait mkdir(dirname(path), { recursive: true });\n\tconst tempPath = `${path}.tmp`;\n\tawait writeFile(tempPath, content, \"utf-8\");\n\tawait rename(tempPath, path);\n}\n\nfunction ensureTrailingNewlines(content: string): string {\n\treturn content.trimEnd().length > 0 ? `${content.trimEnd()}\\n\\n` : \"\";\n}\n\nexport function getChannelMemoryPath(channelDir: string): string {\n\treturn join(channelDir, \"MEMORY.md\");\n}\n\nexport function getChannelHistoryPath(channelDir: string): string {\n\treturn join(channelDir, \"HISTORY.md\");\n}\n\nexport function getChannelSessionPath(channelDir: string): string {\n\treturn join(channelDir, \"SESSION.md\");\n}\n\nexport async function ensureChannelMemoryFiles(channelDir: string): Promise<void> {\n\tensureChannelMemoryFilesSync(channelDir);\n}\n\nexport function ensureChannelMemoryFilesSync(channelDir: string): void {\n\tconst memoryPath = getChannelMemoryPath(channelDir);\n\tconst historyPath = getChannelHistoryPath(channelDir);\n\tconst sessionPath = getChannelSessionPath(channelDir);\n\n\tmkdirSync(channelDir, { recursive: true });\n\n\tif (!existsSync(memoryPath)) {\n\t\twriteFileSync(memoryPath, DEFAULT_CHANNEL_MEMORY, \"utf-8\");\n\t}\n\tif (!existsSync(historyPath)) {\n\t\twriteFileSync(historyPath, DEFAULT_CHANNEL_HISTORY, \"utf-8\");\n\t}\n\tif (!existsSync(sessionPath)) {\n\t\twriteFileSync(sessionPath, DEFAULT_CHANNEL_SESSION, \"utf-8\");\n\t}\n}\n\nasync function readTextFile(path: string): Promise<string> {\n\tif (!existsSync(path)) {\n\t\treturn \"\";\n\t}\n\treturn readFile(path, \"utf-8\");\n}\n\nexport async function readChannelMemory(channelDir: string): Promise<string> {\n\treturn readTextFile(getChannelMemoryPath(channelDir));\n}\n\nexport async function readChannelHistory(channelDir: string): Promise<string> {\n\treturn readTextFile(getChannelHistoryPath(channelDir));\n}\n\nexport async function readChannelSession(channelDir: string): Promise<string> {\n\treturn readTextFile(getChannelSessionPath(channelDir));\n}\n\nexport async function rewriteChannelMemory(channelDir: string, content: string): Promise<void> {\n\tawait ensureChannelMemoryFiles(channelDir);\n\tconst nextContent = normalizeContent(content) || DEFAULT_CHANNEL_MEMORY;\n\tawait writeAtomically(getChannelMemoryPath(channelDir), nextContent);\n}\n\nexport async function rewriteChannelHistory(channelDir: string, content: string): Promise<void> {\n\tawait ensureChannelMemoryFiles(channelDir);\n\tconst nextContent = normalizeContent(content) || DEFAULT_CHANNEL_HISTORY;\n\tawait writeAtomically(getChannelHistoryPath(channelDir), nextContent);\n}\n\nexport async function rewriteChannelSession(channelDir: string, content: string): Promise<void> {\n\tawait ensureChannelMemoryFiles(channelDir);\n\tconst nextContent = normalizeContent(content) || DEFAULT_CHANNEL_SESSION;\n\tawait writeAtomically(getChannelSessionPath(channelDir), nextContent);\n}\n\nexport async function appendChannelMemoryUpdate(channelDir: string, block: MemoryUpdateBlock): Promise<void> {\n\tif (block.entries.length === 0) {\n\t\treturn;\n\t}\n\n\tawait ensureChannelMemoryFiles(channelDir);\n\tconst path = getChannelMemoryPath(channelDir);\n\tconst existing = await readTextFile(path);\n\tconst renderedBlock = [`## Update ${block.timestamp}`, ...block.entries.map((entry) => `- ${entry.trim()}`)].join(\n\t\t\"\\n\",\n\t);\n\tawait writeAtomically(path, `${ensureTrailingNewlines(existing)}${renderedBlock}\\n`);\n}\n\nexport async function appendChannelHistoryBlock(channelDir: string, block: HistoryBlock): Promise<void> {\n\tconst trimmedContent = block.content.trim();\n\tif (!trimmedContent) {\n\t\treturn;\n\t}\n\n\tawait ensureChannelMemoryFiles(channelDir);\n\tconst path = getChannelHistoryPath(channelDir);\n\tconst existing = await readTextFile(path);\n\tconst renderedBlock = [`## ${block.timestamp}`, trimmedContent].join(\"\\n\\n\");\n\tawait writeAtomically(path, `${ensureTrailingNewlines(existing)}${renderedBlock}\\n`);\n}\n\nexport interface MarkdownSection {\n\theading: string;\n\tcontent: string;\n}\n\nexport function splitMarkdownSections(content: string): MarkdownSection[] {\n\tconst normalized = content.replace(/\\r/g, \"\").trim();\n\tif (!normalized) {\n\t\treturn [];\n\t}\n\n\tconst lines = normalized.split(\"\\n\");\n\tconst sections: MarkdownSection[] = [];\n\tlet currentHeading = \"\";\n\tlet currentLines: string[] = [];\n\n\tconst flush = (): void => {\n\t\tif (!currentHeading) {\n\t\t\treturn;\n\t\t}\n\t\tsections.push({\n\t\t\theading: currentHeading,\n\t\t\tcontent: currentLines.join(\"\\n\").trim(),\n\t\t});\n\t};\n\n\tfor (const line of lines) {\n\t\tif (line.startsWith(\"## \")) {\n\t\t\tflush();\n\t\t\tcurrentHeading = line.slice(3).trim();\n\t\t\tcurrentLines = [];\n\t\t\tcontinue;\n\t\t}\n\t\tif (currentHeading) {\n\t\t\tcurrentLines.push(line);\n\t\t}\n\t}\n\n\tflush();\n\treturn sections;\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"memory-lifecycle.d.ts","sourceRoot":"","sources":["../src/memory-lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE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.js";
3
- import { updateChannelSessionMemory } from "./session-memory.js";
4
- export class MemoryLifecycle {
5
- constructor(options) {
6
- this.options = options;
7
- this.backgroundQueue = Promise.resolve();
8
- this.turnsSinceSessionUpdate = 0;
9
- this.toolCallsSinceSessionUpdate = 0;
10
- this.sessionUpdatePending = false;
11
- }
12
- buildRunOptions(messages, sessionEntries) {
13
- return {
14
- channelDir: this.options.channelDir,
15
- model: this.options.getModel(),
16
- resolveApiKey: this.options.resolveApiKey,
17
- messages: messages ?? this.options.getMessages(),
18
- sessionEntries: sessionEntries ?? this.options.getSessionEntries(),
19
- };
20
- }
21
- createExtensionFactory() {
22
- return (pi) => {
23
- pi.on("session_before_compact", async (event) => {
24
- await this.handleSessionBeforeCompact(event);
25
- });
26
- pi.on("session_compact", async (event) => {
27
- this.handleSessionCompact(event);
28
- });
29
- pi.on("session_before_switch", async (event) => {
30
- await this.handleSessionBeforeSwitch(event);
31
- });
32
- pi.on("session_switch", async (event) => {
33
- this.handleSessionSwitch(event);
34
- });
35
- };
36
- }
37
- noteToolCall() {
38
- if (!this.options.getSessionMemorySettings().enabled) {
39
- return;
40
- }
41
- this.toolCallsSinceSessionUpdate++;
42
- }
43
- noteCompletedAssistantTurn() {
44
- const settings = this.options.getSessionMemorySettings();
45
- if (!settings.enabled) {
46
- return;
47
- }
48
- this.turnsSinceSessionUpdate++;
49
- if (this.turnsSinceSessionUpdate >= settings.minTurnsBetweenUpdate ||
50
- this.toolCallsSinceSessionUpdate >= settings.minToolCallsBetweenUpdate) {
51
- this.enqueueSessionMemoryUpdate("threshold");
52
- }
53
- }
54
- async refreshSessionMemory(reason) {
55
- const settings = this.options.getSessionMemorySettings();
56
- if (!settings.enabled) {
57
- return false;
58
- }
59
- try {
60
- await updateChannelSessionMemory({
61
- channelDir: this.options.channelDir,
62
- messages: this.options.getMessages(),
63
- model: this.options.getModel(),
64
- resolveApiKey: this.options.resolveApiKey,
65
- });
66
- this.turnsSinceSessionUpdate = 0;
67
- this.toolCallsSinceSessionUpdate = 0;
68
- log.logInfo(`[${this.options.channelId}] Session memory updated (${reason})`);
69
- return true;
70
- }
71
- catch (error) {
72
- const message = error instanceof Error ? error.message : String(error);
73
- log.logWarning(`[${this.options.channelId}] Session memory update failed (${reason})`, message);
74
- return false;
75
- }
76
- }
77
- enqueueSessionMemoryUpdate(reason) {
78
- if (this.sessionUpdatePending) {
79
- return;
80
- }
81
- this.sessionUpdatePending = true;
82
- this.backgroundQueue = this.backgroundQueue
83
- .then(async () => {
84
- await this.refreshSessionMemory(reason);
85
- })
86
- .catch((error) => {
87
- const message = error instanceof Error ? error.message : String(error);
88
- log.logWarning(`[${this.options.channelId}] Session memory queue failed`, message);
89
- })
90
- .finally(() => {
91
- this.sessionUpdatePending = false;
92
- });
93
- }
94
- async consolidateBeforeContextDrop(reason, messages, sessionEntries) {
95
- const settings = this.options.getSessionMemorySettings();
96
- if ((reason === "compaction" && settings.forceRefreshBeforeCompact) ||
97
- (reason === "new-session" && settings.forceRefreshBeforeNewSession)) {
98
- await this.refreshSessionMemory(reason);
99
- }
100
- log.logInfo(`[${this.options.channelId}] Memory consolidation starting (${reason})`);
101
- try {
102
- const result = await runInlineConsolidation(this.buildRunOptions(messages, sessionEntries));
103
- log.logInfo(`[${this.options.channelId}] Memory consolidation finished (${reason}): memory entries=${result.appendedMemoryEntries}, history=${result.appendedHistoryBlock ? "yes" : "no"}`);
104
- }
105
- catch (error) {
106
- const message = error instanceof Error ? error.message : String(error);
107
- log.logWarning(`[${this.options.channelId}] Memory consolidation failed (${reason})`, message);
108
- }
109
- }
110
- async handleSessionBeforeCompact(event) {
111
- await this.consolidateBeforeContextDrop("compaction", event.preparation.messagesToSummarize);
112
- }
113
- handleSessionCompact(_event) {
114
- this.enqueueBackgroundMaintenance();
115
- }
116
- async handleSessionBeforeSwitch(event) {
117
- if (event.reason !== "new") {
118
- return;
119
- }
120
- await this.consolidateBeforeContextDrop("new-session");
121
- }
122
- handleSessionSwitch(event) {
123
- if (event.reason !== "new") {
124
- return;
125
- }
126
- this.enqueueBackgroundMaintenance();
127
- }
128
- enqueueBackgroundMaintenance() {
129
- this.backgroundQueue = this.backgroundQueue
130
- .then(async () => {
131
- const result = await runBackgroundMaintenance(this.buildRunOptions([], []));
132
- this.logBackgroundResult(result);
133
- })
134
- .catch((error) => {
135
- const message = error instanceof Error ? error.message : String(error);
136
- log.logWarning(`[${this.options.channelId}] Background memory maintenance failed`, message);
137
- });
138
- }
139
- logBackgroundResult(result) {
140
- if (!result.cleanedMemory && !result.foldedHistory) {
141
- return;
142
- }
143
- const details = [
144
- `memory cleanup=${result.cleanedMemory ? "yes" : "no"}`,
145
- `history fold=${result.foldedHistory ? "yes" : "no"}`,
146
- ].join(", ");
147
- log.logInfo(`[${this.options.channelId}] Background memory maintenance complete: ${details}`);
148
- }
149
- }
150
- //# sourceMappingURL=memory-lifecycle.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"memory-lifecycle.js","sourceRoot":"","sources":["../src/memory-lifecycle.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OAAO,EAGN,wBAAwB,EACxB,sBAAsB,GACtB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAcjE,MAAM,OAAO,eAAe;IAM3B,YAAoB,OAA+B;QAA/B,YAAO,GAAP,OAAO,CAAwB;QAL3C,oBAAe,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;QACnD,4BAAuB,GAAG,CAAC,CAAC;QAC5B,gCAA2B,GAAG,CAAC,CAAC;QAChC,yBAAoB,GAAG,KAAK,CAAC;IAEiB,CAAC;IAE/C,eAAe,CAAC,QAAyB,EAAE,cAA+B;QACjF,OAAO;YACN,UAAU,EAAE,IAAI,CAAC,OAAO,CAAC,UAAU;YACnC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;YAC9B,aAAa,EAAE,IAAI,CAAC,OAAO,C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,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;YAC5E,IAAI,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;YACzB,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACvE,GAAG,CAAC,UAAU,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,wCAAwC,EAAE,OAAO,CAAC,CAAC;QAC7F,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,mBAAmB,CAAC,MAAmC;QAC9D,IAAI,CAAC,MAAM,CAAC,aAAa,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;YACpD,OAAO;QACR,CAAC;QAED,MAAM,OAAO,GAAG;YACf,kBAAkB,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;YACvD,gBAAgB,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;SACrD,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACb,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,6CAA6C,OAAO,EAAE,CAAC,CAAC;IAC/F,CAAC;CACD","sourcesContent":["import type { AgentMessage } from \"@mariozechner/pi-agent-core\";\nimport type { Api, Model } from \"@mariozechner/pi-ai\";\nimport type {\n\tExtensionFactory,\n\tSessionBeforeCompactEvent,\n\tSessionBeforeSwitchEvent,\n\tSessionCompactEvent,\n\tSessionEntry,\n\tSessionSwitchEvent,\n} from \"@mariozechner/pi-coding-agent\";\nimport type { PipiclawSessionMemorySettings } from \"./context.js\";\nimport * as log from \"./log.js\";\nimport {\n\ttype BackgroundMaintenanceResult,\n\ttype ConsolidationRunOptions,\n\trunBackgroundMaintenance,\n\trunInlineConsolidation,\n} from \"./memory-consolidation.js\";\nimport { updateChannelSessionMemory } from \"./session-memory.js\";\n\nexport type ConsolidationReason = \"compaction\" | \"new-session\";\n\nexport interface MemoryLifecycleOptions {\n\tchannelId: string;\n\tchannelDir: string;\n\tgetMessages: () => AgentMessage[];\n\tgetSessionEntries: () => SessionEntry[];\n\tgetModel: () => Model<Api>;\n\tresolveApiKey: (model: Model<Api>) => Promise<string>;\n\tgetSessionMemorySettings: () => PipiclawSessionMemorySettings;\n}\n\nexport class MemoryLifecycle {\n\tprivate backgroundQueue: Promise<void> = Promise.resolve();\n\tprivate turnsSinceSessionUpdate = 0;\n\tprivate toolCallsSinceSessionUpdate = 0;\n\tprivate sessionUpdatePending = false;\n\n\tconstructor(private options: MemoryLifecycleOptions) {}\n\n\tprivate buildRunOptions(messages?: AgentMessage[], sessionEntries?: SessionEntry[]): ConsolidationRunOptions {\n\t\treturn {\n\t\t\tchannelDir: this.options.channelDir,\n\t\t\tmodel: this.options.getModel(),\n\t\t\tresolveApiKey: this.options.resolveApiKey,\n\t\t\tmessages: messages ?? this.options.getMessages(),\n\t\t\tsessionEntries: sessionEntries ?? this.options.getSessionEntries(),\n\t\t};\n\t}\n\n\tcreateExtensionFactory(): ExtensionFactory {\n\t\treturn (pi) => {\n\t\t\tpi.on(\"session_before_compact\", async (event: SessionBeforeCompactEvent) => {\n\t\t\t\tawait this.handleSessionBeforeCompact(event);\n\t\t\t});\n\t\t\tpi.on(\"session_compact\", async (event: SessionCompactEvent) => {\n\t\t\t\tthis.handleSessionCompact(event);\n\t\t\t});\n\t\t\tpi.on(\"session_before_switch\", async (event: SessionBeforeSwitchEvent) => {\n\t\t\t\tawait this.handleSessionBeforeSwitch(event);\n\t\t\t});\n\t\t\tpi.on(\"session_switch\", async (event: SessionSwitchEvent) => {\n\t\t\t\tthis.handleSessionSwitch(event);\n\t\t\t});\n\t\t};\n\t}\n\n\tnoteToolCall(): void {\n\t\tif (!this.options.getSessionMemorySettings().enabled) {\n\t\t\treturn;\n\t\t}\n\t\tthis.toolCallsSinceSessionUpdate++;\n\t}\n\n\tnoteCompletedAssistantTurn(): void {\n\t\tconst settings = this.options.getSessionMemorySettings();\n\t\tif (!settings.enabled) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.turnsSinceSessionUpdate++;\n\t\tif (\n\t\t\tthis.turnsSinceSessionUpdate >= settings.minTurnsBetweenUpdate ||\n\t\t\tthis.toolCallsSinceSessionUpdate >= settings.minToolCallsBetweenUpdate\n\t\t) {\n\t\t\tthis.enqueueSessionMemoryUpdate(\"threshold\");\n\t\t}\n\t}\n\n\tprivate async refreshSessionMemory(reason: \"threshold\" | ConsolidationReason): Promise<boolean> {\n\t\tconst settings = this.options.getSessionMemorySettings();\n\t\tif (!settings.enabled) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\tawait updateChannelSessionMemory({\n\t\t\t\tchannelDir: this.options.channelDir,\n\t\t\t\tmessages: this.options.getMessages(),\n\t\t\t\tmodel: this.options.getModel(),\n\t\t\t\tresolveApiKey: this.options.resolveApiKey,\n\t\t\t});\n\t\t\tthis.turnsSinceSessionUpdate = 0;\n\t\t\tthis.toolCallsSinceSessionUpdate = 0;\n\t\t\tlog.logInfo(`[${this.options.channelId}] Session memory updated (${reason})`);\n\t\t\treturn true;\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\tlog.logWarning(`[${this.options.channelId}] Session memory update failed (${reason})`, message);\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tprivate enqueueSessionMemoryUpdate(reason: \"threshold\"): void {\n\t\tif (this.sessionUpdatePending) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.sessionUpdatePending = true;\n\t\tthis.backgroundQueue = this.backgroundQueue\n\t\t\t.then(async () => {\n\t\t\t\tawait this.refreshSessionMemory(reason);\n\t\t\t})\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\t\tlog.logWarning(`[${this.options.channelId}] Session memory queue failed`, message);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tthis.sessionUpdatePending = false;\n\t\t\t});\n\t}\n\n\tprivate async consolidateBeforeContextDrop(\n\t\treason: ConsolidationReason,\n\t\tmessages?: AgentMessage[],\n\t\tsessionEntries?: SessionEntry[],\n\t): Promise<void> {\n\t\tconst settings = this.options.getSessionMemorySettings();\n\t\tif (\n\t\t\t(reason === \"compaction\" && settings.forceRefreshBeforeCompact) ||\n\t\t\t(reason === \"new-session\" && settings.forceRefreshBeforeNewSession)\n\t\t) {\n\t\t\tawait this.refreshSessionMemory(reason);\n\t\t}\n\n\t\tlog.logInfo(`[${this.options.channelId}] Memory consolidation starting (${reason})`);\n\t\ttry {\n\t\t\tconst result = await runInlineConsolidation(this.buildRunOptions(messages, sessionEntries));\n\t\t\tlog.logInfo(\n\t\t\t\t`[${this.options.channelId}] Memory consolidation finished (${reason}): memory entries=${result.appendedMemoryEntries}, history=${result.appendedHistoryBlock ? \"yes\" : \"no\"}`,\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\tlog.logWarning(`[${this.options.channelId}] Memory consolidation failed (${reason})`, message);\n\t\t}\n\t}\n\n\tprivate async handleSessionBeforeCompact(event: SessionBeforeCompactEvent): Promise<void> {\n\t\tawait this.consolidateBeforeContextDrop(\"compaction\", event.preparation.messagesToSummarize);\n\t}\n\n\tprivate handleSessionCompact(_event: SessionCompactEvent): void {\n\t\tthis.enqueueBackgroundMaintenance();\n\t}\n\n\tprivate async handleSessionBeforeSwitch(event: SessionBeforeSwitchEvent): Promise<void> {\n\t\tif (event.reason !== \"new\") {\n\t\t\treturn;\n\t\t}\n\n\t\tawait this.consolidateBeforeContextDrop(\"new-session\");\n\t}\n\n\tprivate handleSessionSwitch(event: SessionSwitchEvent): void {\n\t\tif (event.reason !== \"new\") {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.enqueueBackgroundMaintenance();\n\t}\n\n\tprivate enqueueBackgroundMaintenance(): void {\n\t\tthis.backgroundQueue = this.backgroundQueue\n\t\t\t.then(async () => {\n\t\t\t\tconst result = await runBackgroundMaintenance(this.buildRunOptions([], []));\n\t\t\t\tthis.logBackgroundResult(result);\n\t\t\t})\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\t\tlog.logWarning(`[${this.options.channelId}] Background memory maintenance failed`, message);\n\t\t\t});\n\t}\n\n\tprivate logBackgroundResult(result: BackgroundMaintenanceResult): void {\n\t\tif (!result.cleanedMemory && !result.foldedHistory) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst details = [\n\t\t\t`memory cleanup=${result.cleanedMemory ? \"yes\" : \"no\"}`,\n\t\t\t`history fold=${result.foldedHistory ? \"yes\" : \"no\"}`,\n\t\t].join(\", \");\n\t\tlog.logInfo(`[${this.options.channelId}] Background memory maintenance complete: ${details}`);\n\t}\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"memory-recall.d.ts","sourceRoot":"","sources":["../src/memory-recall.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,EAAyB,KAAK,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAGhH,MAAM,WAAW,aAAa;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC;IAC7C,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,aAAa,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD,cAAc,CAAC,EAAE,oBAAoB,CAAC;CACtC;AAED,MAAM,WAAW,cAAc;IAC9B,MAAM,EAAE,eAAe,CAAC,QAAQ,CAAC,CAAC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,YAAY;IAC5B,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;CACrB;AAsMD,wBAAsB,oBAAoB,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAkDxF"}
@@ -1,218 +0,0 @@
1
- import { parseJsonObject } from "./llm-json.js";
2
- import { buildMemoryCandidates } from "./memory-candidates.js";
3
- import { runSidecarTask } from "./sidecar-worker.js";
4
- const RERANK_SYSTEM_PROMPT = `You are selecting which memory snippets are most relevant to the current user turn.
5
-
6
- Return strict JSON only:
7
- {
8
- "selectedIds": ["candidate-id"]
9
- }
10
-
11
- Rules:
12
- - Select only snippets that are clearly useful for answering the current turn.
13
- - Prefer current work state, constraints, active files, recent corrections, and durable decisions.
14
- - If nothing is clearly useful, return an empty array.
15
- - Do not rewrite the candidates. Only return candidate ids.`;
16
- const HAN_REGEX = /\p{Script=Han}/u;
17
- const TOKEN_PART_REGEX = /[\p{Script=Han}]+|[\p{L}\p{N}_./-]+/gu;
18
- const MEMORY_RECALL_RERANK_TIMEOUT_MS = 5_000;
19
- function containsHanText(text) {
20
- return HAN_REGEX.test(text);
21
- }
22
- function tokenizeHanPart(part) {
23
- const chars = Array.from(part);
24
- const tokens = [];
25
- for (const size of [2, 3]) {
26
- if (chars.length < size) {
27
- continue;
28
- }
29
- for (let index = 0; index <= chars.length - size; index++) {
30
- tokens.push(chars.slice(index, index + size).join(""));
31
- }
32
- }
33
- return tokens;
34
- }
35
- function tokenize(text) {
36
- const parts = text.toLowerCase().match(TOKEN_PART_REGEX) ?? [];
37
- const tokens = [];
38
- for (const part of parts) {
39
- if (containsHanText(part)) {
40
- tokens.push(...tokenizeHanPart(part));
41
- continue;
42
- }
43
- if (part.length >= 2) {
44
- tokens.push(part);
45
- }
46
- }
47
- return Array.from(new Set(tokens));
48
- }
49
- function computeTokenOverlapScore(queryTokens, text, weight) {
50
- const haystack = new Set(tokenize(text));
51
- let score = 0;
52
- for (const token of queryTokens) {
53
- if (haystack.has(token)) {
54
- score += weight;
55
- }
56
- }
57
- return score;
58
- }
59
- function computeRecencyBoost(timestamp) {
60
- if (!timestamp)
61
- return 0;
62
- const timestampMs = Date.parse(timestamp);
63
- if (!Number.isFinite(timestampMs))
64
- return 0;
65
- const ageMs = Date.now() - timestampMs;
66
- const dayMs = 24 * 60 * 60 * 1000;
67
- if (ageMs <= dayMs)
68
- return 8;
69
- if (ageMs <= 7 * dayMs)
70
- return 5;
71
- if (ageMs <= 30 * dayMs)
72
- return 2;
73
- return 0;
74
- }
75
- function scoreCandidate(queryTokens, candidate) {
76
- let score = candidate.priority;
77
- score += computeTokenOverlapScore(queryTokens, candidate.title, 10);
78
- score += computeTokenOverlapScore(queryTokens, candidate.content, 3);
79
- score += computeTokenOverlapScore(queryTokens, candidate.path, 6);
80
- score += computeRecencyBoost(candidate.timestamp);
81
- return score;
82
- }
83
- function buildFallbackCandidates(request, candidates, existing) {
84
- if (!containsHanText(request.query) && existing.length > 0) {
85
- return existing;
86
- }
87
- const seen = new Set(existing.map(({ candidate }) => candidate.id));
88
- const seeded = [...existing];
89
- const limit = Math.max(request.maxCandidates, request.maxInjected);
90
- for (const candidate of candidates
91
- .filter((item) => item.source === "channel-session" || item.source === "channel-memory")
92
- .sort((a, b) => b.priority - a.priority || a.title.localeCompare(b.title))) {
93
- if (seen.has(candidate.id)) {
94
- continue;
95
- }
96
- seeded.push({ candidate, score: candidate.priority });
97
- seen.add(candidate.id);
98
- if (seeded.length >= limit) {
99
- break;
100
- }
101
- }
102
- return seeded;
103
- }
104
- async function rerankCandidates(request, candidates) {
105
- if ((!request.rerankWithModel && !request.autoRerank) || candidates.length <= request.maxInjected) {
106
- return candidates;
107
- }
108
- const renderedCandidates = candidates
109
- .map(({ candidate, score }) => {
110
- const clippedContent = candidate.content.length > 300 ? `${candidate.content.slice(0, 300)}...` : candidate.content;
111
- return [
112
- `id: ${candidate.id}`,
113
- `source: ${candidate.source}`,
114
- `title: ${candidate.title}`,
115
- `path: ${candidate.path}`,
116
- `score: ${score}`,
117
- `content: ${clippedContent}`,
118
- ].join("\n");
119
- })
120
- .join("\n\n---\n\n");
121
- try {
122
- const result = await runSidecarTask({
123
- name: "memory-recall-rerank",
124
- model: request.model,
125
- resolveApiKey: request.resolveApiKey,
126
- systemPrompt: RERANK_SYSTEM_PROMPT,
127
- prompt: `User turn:\n${request.query.trim()}\n\nCandidates:\n${renderedCandidates}`,
128
- timeoutMs: MEMORY_RECALL_RERANK_TIMEOUT_MS,
129
- parse: (text) => {
130
- const parsed = parseJsonObject(text);
131
- return Array.isArray(parsed.selectedIds)
132
- ? parsed.selectedIds.filter((id) => typeof id === "string" && id.trim().length > 0)
133
- : [];
134
- },
135
- });
136
- const selectedIds = new Set(result.output);
137
- if (selectedIds.size === 0) {
138
- return candidates;
139
- }
140
- const selected = candidates.filter(({ candidate }) => selectedIds.has(candidate.id));
141
- return selected.length > 0 ? selected : candidates;
142
- }
143
- catch {
144
- return candidates;
145
- }
146
- }
147
- function renderRecallResult(items, maxChars) {
148
- if (items.length === 0) {
149
- return "";
150
- }
151
- const lines = ["<runtime_context>", "Relevant context for this turn:"];
152
- for (const item of items) {
153
- lines.push("");
154
- lines.push(`[${item.source}/${item.title}]`);
155
- lines.push(`Path: ${item.path}`);
156
- lines.push(item.content);
157
- }
158
- lines.push("</runtime_context>");
159
- const rendered = lines.join("\n");
160
- if (rendered.length <= maxChars) {
161
- return rendered;
162
- }
163
- const clippedLines = ["<runtime_context>", "Relevant context for this turn:"];
164
- let usedChars = clippedLines.join("\n").length + "</runtime_context>".length + 2;
165
- for (const item of items) {
166
- const block = ["", `[${item.source}/${item.title}]`, `Path: ${item.path}`, item.content].join("\n");
167
- if (usedChars + block.length > maxChars) {
168
- break;
169
- }
170
- clippedLines.push("", `[${item.source}/${item.title}]`, `Path: ${item.path}`, item.content);
171
- usedChars += block.length;
172
- }
173
- clippedLines.push("</runtime_context>");
174
- return clippedLines.join("\n");
175
- }
176
- export async function recallRelevantMemory(request) {
177
- const query = request.query.trim();
178
- if (!query) {
179
- return { items: [], renderedText: "" };
180
- }
181
- const candidates = await buildMemoryCandidates({
182
- workspaceDir: request.workspaceDir,
183
- channelDir: request.channelDir,
184
- cache: request.candidateCache,
185
- });
186
- const filteredCandidates = request.allowedSources?.length
187
- ? candidates.filter((candidate) => request.allowedSources?.includes(candidate.source))
188
- : candidates;
189
- if (filteredCandidates.length === 0) {
190
- return { items: [], renderedText: "" };
191
- }
192
- const queryTokens = tokenize(query);
193
- const scored = filteredCandidates
194
- .map((candidate) => ({ candidate, score: scoreCandidate(queryTokens, candidate) }))
195
- .filter(({ score }) => score > 0)
196
- .sort((a, b) => b.score - a.score || a.candidate.title.localeCompare(b.candidate.title));
197
- const shortlist = buildFallbackCandidates(request, filteredCandidates, scored)
198
- .sort((a, b) => b.score - a.score ||
199
- b.candidate.priority - a.candidate.priority ||
200
- a.candidate.title.localeCompare(b.candidate.title))
201
- .slice(0, Math.max(request.maxCandidates, request.maxInjected));
202
- if (shortlist.length === 0) {
203
- return { items: [], renderedText: "" };
204
- }
205
- const reranked = await rerankCandidates(request, shortlist);
206
- const items = reranked.slice(0, request.maxInjected).map(({ candidate, score }) => ({
207
- source: candidate.source,
208
- path: candidate.path,
209
- title: candidate.title,
210
- content: candidate.content,
211
- score,
212
- }));
213
- return {
214
- items,
215
- renderedText: renderRecallResult(items, request.maxChars),
216
- };
217
- }
218
- //# sourceMappingURL=memory-recall.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"memory-recall.js","sourceRoot":"","sources":["../src/memory-recall.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAmD,MAAM,wBAAwB,CAAC;AAChH,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AA8BrD,MAAM,oBAAoB,GAAG;;;;;;;;;;;4DAW+B,CAAC;AAE7D,MAAM,SAAS,GAAG,iBAAiB,CAAC;AACpC,MAAM,gBAAgB,GAAG,uCAAuC,CAAC;AACjE,MAAM,+BAA+B,GAAG,KAAK,CAAC;AAE9C,SAAS,eAAe,CAAC,IAAY;IACpC,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC;AAED,SAAS,eAAe,CAAC,IAAY;IACpC,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,MAAM,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;QAC3B,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI,EAAE,CAAC;YACzB,SAAS;QACV,CAAC;QACD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;YAC3D,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;QACxD,CAAC;IACF,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAED,SAAS,QAAQ,CAAC,IAAY;IAC7B,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,K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,CAAC;IACxC,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,gBAAgB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAC5D,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,CAAC;QACnF,MAAM,EAAE,SAAS,CAAC,MAAM;QACxB,IAAI,EAAE,SAAS,CAAC,IAAI;QACpB,KAAK,EAAE,SAAS,CAAC,KAAK;QACtB,OAAO,EAAE,SAAS,CAAC,OAAO;QAC1B,KAAK;KACL,CAAC,CAAC,CAAC;IAEJ,OAAO;QACN,KAAK;QACL,YAAY,EAAE,kBAAkB,CAAC,KAAK,EAAE,OAAO,CAAC,QAAQ,CAAC;KACzD,CAAC;AACH,CAAC","sourcesContent":["import type { Api, Model } from \"@mariozechner/pi-ai\";\nimport { parseJsonObject } from \"./llm-json.js\";\nimport { buildMemoryCandidates, type MemoryCandidate, type MemoryCandidateCache } from \"./memory-candidates.js\";\nimport { runSidecarTask } from \"./sidecar-worker.js\";\n\nexport interface RecallRequest {\n\tquery: string;\n\tworkspaceDir: string;\n\tchannelDir: string;\n\tallowedSources?: MemoryCandidate[\"source\"][];\n\tmaxCandidates: number;\n\tmaxInjected: number;\n\tmaxChars: number;\n\trerankWithModel: boolean;\n\tautoRerank?: boolean;\n\tmodel: Model<Api>;\n\tresolveApiKey: (model: Model<Api>) => Promise<string>;\n\tcandidateCache?: MemoryCandidateCache;\n}\n\nexport interface RecalledMemory {\n\tsource: MemoryCandidate[\"source\"];\n\tpath: string;\n\ttitle: string;\n\tcontent: string;\n\tscore: number;\n}\n\nexport interface RecallResult {\n\titems: RecalledMemory[];\n\trenderedText: string;\n}\n\nconst RERANK_SYSTEM_PROMPT = `You are selecting which memory snippets are most relevant to the current user turn.\n\nReturn strict JSON only:\n{\n \"selectedIds\": [\"candidate-id\"]\n}\n\nRules:\n- Select only snippets that are clearly useful for answering the current turn.\n- Prefer current work state, constraints, active files, recent corrections, and durable decisions.\n- If nothing is clearly useful, return an empty array.\n- Do not rewrite the candidates. Only return candidate ids.`;\n\nconst HAN_REGEX = /\\p{Script=Han}/u;\nconst TOKEN_PART_REGEX = /[\\p{Script=Han}]+|[\\p{L}\\p{N}_./-]+/gu;\nconst MEMORY_RECALL_RERANK_TIMEOUT_MS = 5_000;\n\nfunction containsHanText(text: string): boolean {\n\treturn HAN_REGEX.test(text);\n}\n\nfunction tokenizeHanPart(part: string): string[] {\n\tconst chars = Array.from(part);\n\tconst tokens: string[] = [];\n\tfor (const size of [2, 3]) {\n\t\tif (chars.length < size) {\n\t\t\tcontinue;\n\t\t}\n\t\tfor (let index = 0; index <= chars.length - size; index++) {\n\t\t\ttokens.push(chars.slice(index, index + size).join(\"\"));\n\t\t}\n\t}\n\treturn tokens;\n}\n\nfunction tokenize(text: string): string[] {\n\tconst parts = text.toLowerCase().match(TOKEN_PART_REGEX) ?? [];\n\tconst tokens: string[] = [];\n\tfor (const part of parts) {\n\t\tif (containsHanText(part)) {\n\t\t\ttokens.push(...tokenizeHanPart(part));\n\t\t\tcontinue;\n\t\t}\n\t\tif (part.length >= 2) {\n\t\t\ttokens.push(part);\n\t\t}\n\t}\n\treturn Array.from(new Set(tokens));\n}\n\nfunction computeTokenOverlapScore(queryTokens: string[], text: string, weight: number): number {\n\tconst haystack = new Set(tokenize(text));\n\tlet score = 0;\n\tfor (const token of queryTokens) {\n\t\tif (haystack.has(token)) {\n\t\t\tscore += weight;\n\t\t}\n\t}\n\treturn score;\n}\n\nfunction computeRecencyBoost(timestamp: string | undefined): number {\n\tif (!timestamp) return 0;\n\tconst timestampMs = Date.parse(timestamp);\n\tif (!Number.isFinite(timestampMs)) return 0;\n\n\tconst ageMs = Date.now() - timestampMs;\n\tconst dayMs = 24 * 60 * 60 * 1000;\n\tif (ageMs <= dayMs) return 8;\n\tif (ageMs <= 7 * dayMs) return 5;\n\tif (ageMs <= 30 * dayMs) return 2;\n\treturn 0;\n}\n\nfunction scoreCandidate(queryTokens: string[], candidate: MemoryCandidate): number {\n\tlet score = candidate.priority;\n\tscore += computeTokenOverlapScore(queryTokens, candidate.title, 10);\n\tscore += computeTokenOverlapScore(queryTokens, candidate.content, 3);\n\tscore += computeTokenOverlapScore(queryTokens, candidate.path, 6);\n\tscore += computeRecencyBoost(candidate.timestamp);\n\treturn score;\n}\n\nfunction buildFallbackCandidates(\n\trequest: RecallRequest,\n\tcandidates: MemoryCandidate[],\n\texisting: Array<{ candidate: MemoryCandidate; score: number }>,\n): Array<{ candidate: MemoryCandidate; score: number }> {\n\tif (!containsHanText(request.query) && existing.length > 0) {\n\t\treturn existing;\n\t}\n\n\tconst seen = new Set(existing.map(({ candidate }) => candidate.id));\n\tconst seeded = [...existing];\n\tconst limit = Math.max(request.maxCandidates, request.maxInjected);\n\n\tfor (const candidate of candidates\n\t\t.filter((item) => item.source === \"channel-session\" || item.source === \"channel-memory\")\n\t\t.sort((a, b) => b.priority - a.priority || a.title.localeCompare(b.title))) {\n\t\tif (seen.has(candidate.id)) {\n\t\t\tcontinue;\n\t\t}\n\t\tseeded.push({ candidate, score: candidate.priority });\n\t\tseen.add(candidate.id);\n\t\tif (seeded.length >= limit) {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn seeded;\n}\n\nasync function rerankCandidates(\n\trequest: RecallRequest,\n\tcandidates: Array<{ candidate: MemoryCandidate; score: number }>,\n): Promise<Array<{ candidate: MemoryCandidate; score: number }>> {\n\tif ((!request.rerankWithModel && !request.autoRerank) || candidates.length <= request.maxInjected) {\n\t\treturn candidates;\n\t}\n\n\tconst renderedCandidates = candidates\n\t\t.map(({ candidate, score }) => {\n\t\t\tconst clippedContent =\n\t\t\t\tcandidate.content.length > 300 ? `${candidate.content.slice(0, 300)}...` : candidate.content;\n\t\t\treturn [\n\t\t\t\t`id: ${candidate.id}`,\n\t\t\t\t`source: ${candidate.source}`,\n\t\t\t\t`title: ${candidate.title}`,\n\t\t\t\t`path: ${candidate.path}`,\n\t\t\t\t`score: ${score}`,\n\t\t\t\t`content: ${clippedContent}`,\n\t\t\t].join(\"\\n\");\n\t\t})\n\t\t.join(\"\\n\\n---\\n\\n\");\n\n\ttry {\n\t\tconst result = await runSidecarTask({\n\t\t\tname: \"memory-recall-rerank\",\n\t\t\tmodel: request.model,\n\t\t\tresolveApiKey: request.resolveApiKey,\n\t\t\tsystemPrompt: RERANK_SYSTEM_PROMPT,\n\t\t\tprompt: `User turn:\\n${request.query.trim()}\\n\\nCandidates:\\n${renderedCandidates}`,\n\t\t\ttimeoutMs: MEMORY_RECALL_RERANK_TIMEOUT_MS,\n\t\t\tparse: (text) => {\n\t\t\t\tconst parsed = parseJsonObject(text) as { selectedIds?: unknown };\n\t\t\t\treturn Array.isArray(parsed.selectedIds)\n\t\t\t\t\t? parsed.selectedIds.filter((id): id is string => typeof id === \"string\" && id.trim().length > 0)\n\t\t\t\t\t: [];\n\t\t\t},\n\t\t});\n\n\t\tconst selectedIds = new Set(result.output);\n\t\tif (selectedIds.size === 0) {\n\t\t\treturn candidates;\n\t\t}\n\n\t\tconst selected = candidates.filter(({ candidate }) => selectedIds.has(candidate.id));\n\t\treturn selected.length > 0 ? selected : candidates;\n\t} catch {\n\t\treturn candidates;\n\t}\n}\n\nfunction renderRecallResult(items: RecalledMemory[], maxChars: number): string {\n\tif (items.length === 0) {\n\t\treturn \"\";\n\t}\n\n\tconst lines: string[] = [\"<runtime_context>\", \"Relevant context for this turn:\"];\n\tfor (const item of items) {\n\t\tlines.push(\"\");\n\t\tlines.push(`[${item.source}/${item.title}]`);\n\t\tlines.push(`Path: ${item.path}`);\n\t\tlines.push(item.content);\n\t}\n\tlines.push(\"</runtime_context>\");\n\n\tconst rendered = lines.join(\"\\n\");\n\tif (rendered.length <= maxChars) {\n\t\treturn rendered;\n\t}\n\n\tconst clippedLines = [\"<runtime_context>\", \"Relevant context for this turn:\"];\n\tlet usedChars = clippedLines.join(\"\\n\").length + \"</runtime_context>\".length + 2;\n\tfor (const item of items) {\n\t\tconst block = [\"\", `[${item.source}/${item.title}]`, `Path: ${item.path}`, item.content].join(\"\\n\");\n\t\tif (usedChars + block.length > maxChars) {\n\t\t\tbreak;\n\t\t}\n\t\tclippedLines.push(\"\", `[${item.source}/${item.title}]`, `Path: ${item.path}`, item.content);\n\t\tusedChars += block.length;\n\t}\n\tclippedLines.push(\"</runtime_context>\");\n\treturn clippedLines.join(\"\\n\");\n}\n\nexport async function recallRelevantMemory(request: RecallRequest): Promise<RecallResult> {\n\tconst query = request.query.trim();\n\tif (!query) {\n\t\treturn { items: [], renderedText: \"\" };\n\t}\n\n\tconst candidates = await buildMemoryCandidates({\n\t\tworkspaceDir: request.workspaceDir,\n\t\tchannelDir: request.channelDir,\n\t\tcache: request.candidateCache,\n\t});\n\tconst filteredCandidates = request.allowedSources?.length\n\t\t? candidates.filter((candidate) => request.allowedSources?.includes(candidate.source))\n\t\t: candidates;\n\tif (filteredCandidates.length === 0) {\n\t\treturn { items: [], renderedText: \"\" };\n\t}\n\n\tconst queryTokens = tokenize(query);\n\tconst scored = filteredCandidates\n\t\t.map((candidate) => ({ candidate, score: scoreCandidate(queryTokens, candidate) }))\n\t\t.filter(({ score }) => score > 0)\n\t\t.sort((a, b) => b.score - a.score || a.candidate.title.localeCompare(b.candidate.title));\n\n\tconst shortlist = buildFallbackCandidates(request, filteredCandidates, scored)\n\t\t.sort(\n\t\t\t(a, b) =>\n\t\t\t\tb.score - a.score ||\n\t\t\t\tb.candidate.priority - a.candidate.priority ||\n\t\t\t\ta.candidate.title.localeCompare(b.candidate.title),\n\t\t)\n\t\t.slice(0, Math.max(request.maxCandidates, request.maxInjected));\n\n\tif (shortlist.length === 0) {\n\t\treturn { items: [], renderedText: \"\" };\n\t}\n\n\tconst reranked = await rerankCandidates(request, shortlist);\n\tconst items = reranked.slice(0, request.maxInjected).map(({ candidate, score }) => ({\n\t\tsource: candidate.source,\n\t\tpath: candidate.path,\n\t\ttitle: candidate.title,\n\t\tcontent: candidate.content,\n\t\tscore,\n\t}));\n\n\treturn {\n\t\titems,\n\t\trenderedText: renderRecallResult(items, request.maxChars),\n\t};\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"model-utils.d.ts","sourceRoot":"","sources":["../src/model-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAY,KAAK,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACrE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAK5D,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,MAAM,CAE9D;AAED,wBAAgB,4BAA4B,CAC3C,cAAc,EAAE,MAAM,EACtB,eAAe,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,GAC3B;IAAE,KAAK,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,CA2C5C;AAED,wBAAgB,eAAe,CAC9B,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,EACpB,YAAY,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,EACpC,KAAK,GAAE,MAAW,GAChB,MAAM,CAkBR;AAED,wBAAgB,mBAAmB,CAClC,aAAa,EAAE,aAAa,EAC5B,eAAe,EAAE,uBAAuB,GACtC,KAAK,CAAC,GAAG,CAAC,CAmBZ"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"model-utils.js","sourceRoot":"","sources":["../src/model-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,QAAQ,EAAc,MAAM,qBAAqB,CAAC;AAIrE,sFAAsF;AACtF,MAAM,YAAY,GAAG,QAAQ,CAAC,WAAW,EAAE,mBAAmB,CAAC,CAAC;AAEhE,MAAM,UAAU,oBA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AC","sourcesContent":["import { type Api, getModel, type Model } from \"@mariozechner/pi-ai\";\nimport type { ModelRegistry } from \"@mariozechner/pi-coding-agent\";\nimport type { PipiclawSettingsManager } from \"./context.js\";\n\n// Default model - will be overridden by ModelRegistry if custom models are configured\nconst defaultModel = getModel(\"anthropic\", \"claude-sonnet-4-5\");\n\nexport function formatModelReference(model: Model<Api>): string {\n\treturn `${model.provider}/${model.id}`;\n}\n\nexport function findExactModelReferenceMatch(\n\tmodelReference: string,\n\tavailableModels: Model<Api>[],\n): { match?: Model<Api>; ambiguous: boolean } {\n\tconst trimmedReference = modelReference.trim();\n\tif (!trimmedReference) {\n\t\treturn { ambiguous: false };\n\t}\n\n\tconst normalizedReference = trimmedReference.toLowerCase();\n\n\tconst canonicalMatches = availableModels.filter(\n\t\t(model) => `${model.provider}/${model.id}`.toLowerCase() === normalizedReference,\n\t);\n\tif (canonicalMatches.length === 1) {\n\t\treturn { match: canonicalMatches[0], ambiguous: false };\n\t}\n\tif (canonicalMatches.length > 1) {\n\t\treturn { ambiguous: true };\n\t}\n\n\tconst slashIndex = trimmedReference.indexOf(\"/\");\n\tif (slashIndex !== -1) {\n\t\tconst provider = trimmedReference.substring(0, slashIndex).trim();\n\t\tconst modelId = trimmedReference.substring(slashIndex + 1).trim();\n\t\tif (provider && modelId) {\n\t\t\tconst providerMatches = availableModels.filter(\n\t\t\t\t(model) =>\n\t\t\t\t\tmodel.provider.toLowerCase() === provider.toLowerCase() &&\n\t\t\t\t\tmodel.id.toLowerCase() === modelId.toLowerCase(),\n\t\t\t);\n\t\t\tif (providerMatches.length === 1) {\n\t\t\t\treturn { match: providerMatches[0], ambiguous: false };\n\t\t\t}\n\t\t\tif (providerMatches.length > 1) {\n\t\t\t\treturn { ambiguous: true };\n\t\t\t}\n\t\t}\n\t}\n\n\tconst idMatches = availableModels.filter((model) => model.id.toLowerCase() === normalizedReference);\n\tif (idMatches.length === 1) {\n\t\treturn { match: idMatches[0], ambiguous: false };\n\t}\n\n\treturn { ambiguous: idMatches.length > 1 };\n}\n\nexport function formatModelList(\n\tmodels: Model<Api>[],\n\tcurrentModel: Model<Api> | undefined,\n\tlimit: number = 20,\n): string {\n\tconst refs = models\n\t\t.slice()\n\t\t.sort((a, b) => formatModelReference(a).localeCompare(formatModelReference(b)))\n\t\t.map((model) => {\n\t\t\tconst ref = formatModelReference(model);\n\t\t\tconst marker =\n\t\t\t\tcurrentModel && currentModel.provider === model.provider && currentModel.id === model.id\n\t\t\t\t\t? \" (current)\"\n\t\t\t\t\t: \"\";\n\t\t\treturn `- \\`${ref}\\`${marker}`;\n\t\t});\n\n\tif (refs.length <= limit) {\n\t\treturn refs.join(\"\\n\");\n\t}\n\n\treturn `${refs.slice(0, limit).join(\"\\n\")}\\n- ... and ${refs.length - limit} more`;\n}\n\nexport function resolveInitialModel(\n\tmodelRegistry: ModelRegistry,\n\tsettingsManager: PipiclawSettingsManager,\n): Model<Api> {\n\tconst savedProvider = settingsManager.getDefaultProvider();\n\tconst savedModelId = settingsManager.getDefaultModel();\n\tconst availableModels = modelRegistry.getAvailable();\n\tif (savedProvider && savedModelId) {\n\t\tconst savedModel = modelRegistry.find(savedProvider, savedModelId);\n\t\tif (\n\t\t\tsavedModel &&\n\t\t\tavailableModels.some((model) => model.provider === savedModel.provider && model.id === savedModel.id)\n\t\t) {\n\t\t\treturn savedModel;\n\t\t}\n\t}\n\n\tif (availableModels.length > 0) {\n\t\treturn availableModels[0];\n\t}\n\n\treturn defaultModel;\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,aAAa,CAAC;AACnC,eAAO,MAAM,YAAY,QAAmC,CAAC;AAC7D,eAAO,MAAM,aAAa,QAAkC,CAAC;AAC7D,eAAO,MAAM,mBAAmB,eAAe,CAAC;AAChD,eAAO,MAAM,cAAc,QAA2C,CAAC;AACvE,eAAO,MAAM,mBAAmB,QAAqC,CAAC;AACtE,eAAO,MAAM,gBAAgB,QAAkC,CAAC;AAChE,eAAO,MAAM,kBAAkB,QAAoC,CAAC;AACpE,eAAO,MAAM,oBAAoB,QAAsC,CAAC"}
package/dist/paths.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"paths.js","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAAC;AACnC,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;AAC7D,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;AAC7D,MAAM,CAAC,MAAM,mBAAmB,GAAG,YAAY,CAAC;AAChD,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC,aAAa,EAAE,mBAAmB,CAAC,CAAC;AACvE,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAC,YAAY,EAAE,cAAc,CAAC,CAAC;AACtE,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;AAChE,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;AACpE,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC","sourcesContent":["import { homedir } from \"os\";\nimport { join } from \"path\";\n\nexport const APP_NAME = \"pipiclaw\";\nexport const APP_HOME_DIR = join(homedir(), \".pi\", APP_NAME);\nexport const WORKSPACE_DIR = join(APP_HOME_DIR, \"workspace\");\nexport const SUB_AGENTS_DIR_NAME = \"sub-agents\";\nexport const SUB_AGENTS_DIR = join(WORKSPACE_DIR, SUB_AGENTS_DIR_NAME);\nexport const CHANNEL_CONFIG_PATH = join(APP_HOME_DIR, \"channel.json\");\nexport const AUTH_CONFIG_PATH = join(APP_HOME_DIR, \"auth.json\");\nexport const MODELS_CONFIG_PATH = join(APP_HOME_DIR, \"models.json\");\nexport const SETTINGS_CONFIG_PATH = join(APP_HOME_DIR, \"settings.json\");\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"prompt-builder.d.ts","sourceRoot":"","sources":["../src/prompt-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,WAAW,yBAAyB;IACzC,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,uBAAuB,CACtC,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,EACjB,aAAa,EAAE,aAAa,EAC5B,OAAO,GAAE,yBAA8B,GACrC,MAAM,CAyJR"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"prompt-builder.js","sourceRoot":"","sources":["../src/prompt-builder.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,uBAAuB,CACtC,aAAqB,EACrB,SAAiB,EACjB,aAA4B,EAC5B,UAAqC,EAAE;IAEvC,MAAM,WAAW,GAAG,GAAG,aAAa,IAAI,SAAS,EAAE,CAAC;IACpD,MAAM,aAAa,GAAG,GAAG,aAAa,aAAa,CAAC;IACpD,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,KAAK,QAAQ,CAAC;IAEjD,MAAM,cAAc,GAAG,QAAQ;QAC9B,CAAC,CAAC;;;uCAGmC;QACrC,CAAC,CAAC;4BACwB,OAAO,CAAC,GAAG,EAAE;uCACF,CAAC;IAEvC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,QAAQ,CAAC,IAAI,CAAC;;;;;;;;;;;;;EAab,cAAc;;;EAGd,aAAa;;;;;;;MAOT,SAAS;;;;;;;0DAO2C,CAAC,CAAC;IAE3D,QAAQ,CAAC,IAAI,CAAC;wHACyG,aAAa;;;;;;sCAM/F,SAAS;;;;;qCAKV,SAAS;;;;;qCAKT,SAAS,qEAAqE,IAAI,CAAC,cAAc,EAAE,CAAC,eAAe,EAAE,CAAC,QAAQ;;;;;;;6BAOtI,aAAa;;;;;;;gCAOV,CAAC,CAAC;IAEjC,QAAQ,CAAC,IAAI,CAAC;;;;sBAIO,aAAa;;4BAEP,WAAW;;oBAEnB,WAAW;;qBAEV,WAAW;;;;;;;;;;IAU5B,WAAW;IACX,WAAW;;uJAEwI,CAAC,CAAC;IAExJ,QAAQ,CAAC,IAAI,CAAC;WACJ,aAAa;;;;;;sDAM8B,CAAC,CAAC;IAEvD,QAAQ,CAAC,IAAI,CAAC;;;;;;;wDAOyC,CAAC,CAAC;IAEzD,QAAQ,CAAC,IAAI,CAAC;;;;6CAI8B,aAAa;EACxD,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,qCAAqC,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,uCAAuC;;;;;;;;;;;;;;;;;;;;;uFAqBvC,CAAC,CAAC;IAExF,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAC9B,CAAC","sourcesContent":["import type { SandboxConfig } from \"./sandbox.js\";\n\nexport interface AppendSystemPromptOptions {\n\tsubAgentList?: string;\n}\n\nexport function buildAppendSystemPrompt(\n\tworkspacePath: string,\n\tchannelId: string,\n\tsandboxConfig: SandboxConfig,\n\toptions: AppendSystemPromptOptions = {},\n): string {\n\tconst channelPath = `${workspacePath}/${channelId}`;\n\tconst subAgentsPath = `${workspacePath}/sub-agents`;\n\tconst isDocker = sandboxConfig.type === \"docker\";\n\n\tconst envDescription = isDocker\n\t\t? `You are running inside a Docker container (Alpine Linux).\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apk add <package>\n- Your changes persist across sessions`\n\t\t: `You are running directly on the host machine.\n- Bash working directory: ${process.cwd()}\n- Be careful with system modifications`;\n\n\tconst sections: string[] = [];\n\n\tsections.push(`## Pipiclaw Runtime\nYou are running inside Pipiclaw, a DingTalk-oriented runtime built on top of pi.\n\n## Context\n- For current date/time, use: date\n- You have access to the active session context for this session.\n- Raw transcript files are cold storage. Do not assume they are preloaded.\n\n## Formatting\nUse Markdown for formatting. DingTalk AI Card supports basic Markdown:\nBold: **text**, Italic: *text*, Code: \\`code\\`, Block: \\`\\`\\`code\\`\\`\\`, Links: [text](url)\n\n## Environment\n${envDescription}\n\n## Workspace Layout\n${workspacePath}/\n├── SOUL.md # Your identity/personality (read-only)\n├── AGENTS.md # Custom behavior instructions (read-only)\n├── MEMORY.md # Stable workspace memory (admin-managed, read on demand)\n├── sub-agents/ # Predefined sub-agent definitions\n├── skills/ # Global CLI tools you create\n├── events/ # Scheduled events\n└── ${channelId}/ # This channel\n ├── SESSION.md # Channel working memory (runtime-managed, read on demand)\n ├── MEMORY.md # Channel durable memory (read on demand, runtime-managed)\n ├── HISTORY.md # Channel summarized history (read on demand, runtime-managed)\n ├── log.jsonl # Raw message archive (cold storage)\n ├── context.jsonl # Raw session archive (cold storage)\n ├── scratch/ # Your working directory\n └── skills/ # Channel-specific tools`);\n\n\tsections.push(`## Events\nYou can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \\`${workspacePath}/events/\\`.\n\n### Event Types\n\n**Immediate** - Triggers as soon as harness sees the file.\n\\`\\`\\`json\n{\"type\": \"immediate\", \"channelId\": \"${channelId}\", \"text\": \"New event occurred\"}\n\\`\\`\\`\n\n**One-shot** - Triggers once at a specific time.\n\\`\\`\\`json\n{\"type\": \"one-shot\", \"channelId\": \"${channelId}\", \"text\": \"Reminder\", \"at\": \"2025-12-15T09:00:00+08:00\"}\n\\`\\`\\`\n\n**Periodic** - Triggers on a cron schedule.\n\\`\\`\\`json\n{\"type\": \"periodic\", \"channelId\": \"${channelId}\", \"text\": \"Check inbox\", \"schedule\": \"0 9 * * 1-5\", \"timezone\": \"${Intl.DateTimeFormat().resolvedOptions().timeZone}\"}\n\\`\\`\\`\n\n### Cron Format\n\\`minute hour day-of-month month day-of-week\\`\n\n### Creating Events\nCreate a JSON file under \\`${workspacePath}/events/\\` with the appropriate event payload.\nPrefer the file tools for creating or editing the event file. Use shell commands only when they are the clearest option.\n\n### Silent Completion\nFor periodic events where there's nothing to report, respond with just \\`[SILENT]\\`. This deletes the status message. Use this to avoid spam when periodic checks find nothing.\n\n### Limits\nMaximum 5 events can be queued.`);\n\n\tsections.push(`## Memory\nMemory files are not preloaded into session context. Read them explicitly when memory or history matters.\n\n### Files\n- Workspace memory: ${workspacePath}/MEMORY.md\n Stable shared background memory. Admin-managed. Read on demand.\n- Channel session memory: ${channelPath}/SESSION.md\n Current working state for this channel. Runtime-managed. Read on demand. Prefer this when current task state matters.\n- Channel memory: ${channelPath}/MEMORY.md\n Durable channel memory. Runtime-managed via consolidation. Prefer this for stable facts, decisions, preferences, and medium-horizon open loops.\n- Channel history: ${channelPath}/HISTORY.md\n Summarized older channel history. Runtime-managed. Read on demand. Do not maintain this file manually during normal work.\n\n### Runtime Behavior\n- The runtime may inject a small amount of relevant memory context from SESSION.md / MEMORY.md / HISTORY.md into a turn when it is clearly useful.\n- SESSION.md is the primary runtime-managed working-state artifact for current active work.\n- The runtime automatically consolidates channel MEMORY.md and HISTORY.md before compaction or session trimming.\n- Workspace MEMORY.md is not updated by normal runtime consolidation.\n\n### Cold Storage\n- ${channelPath}/log.jsonl is a raw archive. It is not normal memory and is not proactively loaded.\n- ${channelPath}/context.jsonl is a raw session archive. It is not normal memory and is not proactively loaded.\n\nWhen a task depends on prior decisions, preferences, or long-running work, prefer SESSION.md first for current state, then MEMORY.md, then HISTORY.md.`);\n\n\tsections.push(`## System Configuration Log\nMaintain ${workspacePath}/SYSTEM.md to log all environment modifications:\n- Installed packages (apk add, npm install, pip install)\n- Environment variables set\n- Config files modified\n- Skill dependencies installed\n\nUpdate this file whenever you modify the environment.`);\n\n\tsections.push(`## Tools\n- read: Read files\n- edit: Surgical file edits\n- write: Create or overwrite files when needed\n- bash: Run shell commands and external programs\n- subagent: Delegate a focused task to a sub-agent with its own isolated context\n\nEach tool requires a \"label\" parameter (shown to user).`);\n\n\tsections.push(`## Sub-Agents\nYou have a \\`subagent\\` tool for delegating focused work to a separate agent with an isolated context window.\n\n### Predefined Sub-Agents\nPredefined sub-agent definitions live in \\`${subAgentsPath}/\\`.\n${options.subAgentList ? `Available predefined sub-agents:\\n${options.subAgentList}` : \"Available predefined sub-agents: none\"}\n\n### Temporary Inline Sub-Agents\nIf no predefined sub-agent fits, you may define a temporary inline sub-agent directly in the \\`subagent\\` tool call by providing a focused \\`systemPrompt\\` plus optional tools, model, and budget settings.\n\nUse sub-agents when:\n- The task can be decomposed into a focused sub-problem\n- You need a fresh context for heavy file reading, shell work, or review\n- A specialized role would produce better results\n- The main conversation has grown long and you want to offload a bounded task\n\nDo not use sub-agents when:\n- The task is simple and direct\n- The task depends heavily on the full current conversation state\n- The task requires frequent user confirmation\n\nImportant rules:\n- Sub-agents cannot see your conversation history unless you include the needed context in \\`task\\`\n- The runtime injects a small fixed execution context (workspace path, channel id, sandbox), but you must still include task-specific context yourself\n- Sub-agents do not receive the \\`subagent\\` tool, so they cannot create nested agents\n- Prefer predefined sub-agents when one clearly fits\n- Use temporary inline sub-agents only when that extra flexibility is genuinely useful`);\n\n\treturn sections.join(\"\\n\\n\");\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,aAAa,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC;AAErF,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,aAAa,CAc5D;AAED,wBAAsB,eAAe,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CA4B1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,CAAC;IACrB,CAAC;CACD;AAED,SAAS,eAAe,CAAC,GAAW;IACnC,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QAClC,IAAI,CAAC;YACJ,KAAK,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE;gBACpD,KAAK,EAAE,QAAQ;gBACf,QAAQ,EAAE,IAAI;aACd,CAAC,CAAC;QACJ,CAAC;QAAC,MAAM,CAAC;YACR,gBAAgB;QACjB,CAAC;IACF,CAAC;SAAM,CAAC;QACP,IAAI,CAAC;YACJ,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACR,IAAI,CAAC;gBACJ,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAC9B,CAAC;YAAC,MAAM,CAAC;gBACR,uBAAuB;YACxB,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import { spawn } from \"child_process\";\nimport { shellEscape } from \"./shell-escape.js\";\n\nexport type SandboxConfig = { type: \"host\" } | { type: \"docker\"; container: string };\n\nexport function parseSandboxArg(value: string): SandboxConfig {\n\tif (value === \"host\") {\n\t\treturn { type: \"host\" };\n\t}\n\tif (value.startsWith(\"docker:\")) {\n\t\tconst container = value.slice(\"docker:\".length);\n\t\tif (!container) {\n\t\t\tconsole.error(\"Error: docker sandbox requires container name (e.g., docker:pipiclaw-sandbox)\");\n\t\t\tprocess.exit(1);\n\t\t}\n\t\treturn { type: \"docker\", container };\n\t}\n\tconsole.error(`Error: Invalid sandbox type '${value}'. Use 'host' or 'docker:<container-name>'`);\n\tprocess.exit(1);\n}\n\nexport async function validateSandbox(config: SandboxConfig): Promise<void> {\n\tif (config.type === \"host\") {\n\t\treturn;\n\t}\n\n\t// Check if Docker is available\n\ttry {\n\t\tawait execSimple(\"docker\", [\"--version\"]);\n\t} catch {\n\t\tconsole.error(\"Error: Docker is not installed or not in PATH\");\n\t\tprocess.exit(1);\n\t}\n\n\t// Check if container exists and is running\n\ttry {\n\t\tconst result = await execSimple(\"docker\", [\"inspect\", \"-f\", \"{{.State.Running}}\", config.container]);\n\t\tif (result.trim() !== \"true\") {\n\t\t\tconsole.error(`Error: Container '${config.container}' is not running.`);\n\t\t\tconsole.error(`Start it with: docker start ${config.container}`);\n\t\t\tprocess.exit(1);\n\t\t}\n\t} catch {\n\t\tconsole.error(`Error: Container '${config.container}' does not exist.`);\n\t\tconsole.error(\"Create it with: ./docker.sh create <data-dir>\");\n\t\tprocess.exit(1);\n\t}\n\n\tconsole.log(` Docker container '${config.container}' is running.`);\n}\n\nfunction execSimple(cmd: string, args: string[]): Promise<string> {\n\treturn new Promise((resolve, reject) => {\n\t\tconst child = spawn(cmd, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"] });\n\t\tlet stdout = \"\";\n\t\tlet stderr = \"\";\n\t\tchild.stdout?.on(\"data\", (d) => {\n\t\t\tstdout += d;\n\t\t});\n\t\tchild.stderr?.on(\"data\", (d) => {\n\t\t\tstderr += d;\n\t\t});\n\t\tchild.on(\"close\", (code) => {\n\t\t\tif (code === 0) resolve(stdout);\n\t\t\telse reject(new Error(stderr || `Exit code ${code}`));\n\t\t});\n\t});\n}\n\n/**\n * Create an executor that runs commands either on host or in Docker container\n */\nexport function createExecutor(config: SandboxConfig): Executor {\n\tif (config.type === \"host\") {\n\t\treturn new HostExecutor();\n\t}\n\treturn new DockerExecutor(config.container);\n}\n\nexport interface Executor {\n\t/**\n\t * Execute a bash command\n\t */\n\texec(command: string, options?: ExecOptions): Promise<ExecResult>;\n\n\t/**\n\t * Get the workspace path prefix for this executor\n\t * Host: returns the actual path\n\t * Docker: returns /workspace\n\t */\n\tgetWorkspacePath(hostPath: string): string;\n}\n\nexport interface ExecOptions {\n\ttimeout?: number;\n\tsignal?: AbortSignal;\n\tstdin?: string;\n}\n\nexport interface ExecResult {\n\tstdout: string;\n\tstderr: string;\n\tcode: number;\n}\n\nclass HostExecutor implements Executor {\n\tasync exec(command: string, options?: ExecOptions): Promise<ExecResult> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst shell = process.platform === \"win32\" ? \"cmd\" : \"sh\";\n\t\t\tconst shellArgs = process.platform === \"win32\" ? [\"/c\"] : [\"-c\"];\n\t\t\tconst child = (() => {\n\t\t\t\ttry {\n\t\t\t\t\treturn spawn(shell, [...shellArgs, command], {\n\t\t\t\t\t\tdetached: true,\n\t\t\t\t\t\tstdio: [\"pipe\", \"pipe\", \"pipe\"],\n\t\t\t\t\t});\n\t\t\t\t} catch (err) {\n\t\t\t\t\treject(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t})();\n\n\t\t\tif (!child) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet stdout = \"\";\n\t\t\tlet stderr = \"\";\n\t\t\tlet timedOut = false;\n\t\t\tlet settled = false;\n\n\t\t\tconst cleanup = () => {\n\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\tif (options?.signal) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst rejectOnce = (err: Error) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tcleanup();\n\t\t\t\treject(err);\n\t\t\t};\n\n\t\t\tconst resolveOnce = (result: ExecResult) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tcleanup();\n\t\t\t\tresolve(result);\n\t\t\t};\n\n\t\t\tconst timeoutHandle =\n\t\t\t\toptions?.timeout && options.timeout > 0\n\t\t\t\t\t? setTimeout(() => {\n\t\t\t\t\t\t\ttimedOut = true;\n\t\t\t\t\t\t\tkillProcessTree(child.pid!);\n\t\t\t\t\t\t}, options.timeout * 1000)\n\t\t\t\t\t: undefined;\n\n\t\t\tconst onAbort = () => {\n\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t};\n\n\t\t\tif (options?.signal) {\n\t\t\t\tif (options.signal.aborted) {\n\t\t\t\t\tonAbort();\n\t\t\t\t} else {\n\t\t\t\t\toptions.signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tchild.stdout?.on(\"data\", (data) => {\n\t\t\t\tstdout += data.toString();\n\t\t\t\tif (stdout.length > 10 * 1024 * 1024) {\n\t\t\t\t\tstdout = stdout.slice(0, 10 * 1024 * 1024);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tchild.stderr?.on(\"data\", (data) => {\n\t\t\t\tstderr += data.toString();\n\t\t\t\tif (stderr.length > 10 * 1024 * 1024) {\n\t\t\t\t\tstderr = stderr.slice(0, 10 * 1024 * 1024);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tchild.on(\"error\", (err) => {\n\t\t\t\trejectOnce(err instanceof Error ? err : new Error(String(err)));\n\t\t\t});\n\n\t\t\tchild.on(\"close\", (code) => {\n\t\t\t\tif (options?.signal?.aborted) {\n\t\t\t\t\trejectOnce(new Error(`${stdout}\\n${stderr}\\nCommand aborted`.trim()));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (timedOut) {\n\t\t\t\t\trejectOnce(\n\t\t\t\t\t\tnew Error(`${stdout}\\n${stderr}\\nCommand timed out after ${options?.timeout} seconds`.trim()),\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tresolveOnce({ stdout, stderr, code: code ?? 0 });\n\t\t\t});\n\n\t\t\tif (options?.stdin !== undefined) {\n\t\t\t\tchild.stdin?.on(\"error\", (err) => {\n\t\t\t\t\tif ((err as NodeJS.ErrnoException).code === \"EPIPE\") return;\n\t\t\t\t\trejectOnce(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t});\n\t\t\t\tchild.stdin?.end(options.stdin);\n\t\t\t} else {\n\t\t\t\tchild.stdin?.end();\n\t\t\t}\n\t\t});\n\t}\n\n\tgetWorkspacePath(hostPath: string): string {\n\t\treturn hostPath;\n\t}\n}\n\nclass DockerExecutor implements Executor {\n\tconstructor(private container: string) {}\n\n\tasync exec(command: string, options?: ExecOptions): Promise<ExecResult> {\n\t\t// Wrap command for docker exec\n\t\tconst interactive = options?.stdin !== undefined ? \"-i \" : \"\";\n\t\tconst dockerCmd = `docker exec ${interactive}${this.container} sh -c ${shellEscape(command)}`;\n\t\tconst hostExecutor = new HostExecutor();\n\t\treturn hostExecutor.exec(dockerCmd, options);\n\t}\n\n\tgetWorkspacePath(_hostPath: string): string {\n\t\t// Docker container sees /workspace\n\t\treturn \"/workspace\";\n\t}\n}\n\nfunction killProcessTree(pid: number): void {\n\tif (process.platform === \"win32\") {\n\t\ttry {\n\t\t\tspawn(\"taskkill\", [\"/F\", \"/T\", \"/PID\", String(pid)], {\n\t\t\t\tstdio: \"ignore\",\n\t\t\t\tdetached: true,\n\t\t\t});\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\t} else {\n\t\ttry {\n\t\t\tprocess.kill(-pid, \"SIGKILL\");\n\t\t} catch {\n\t\t\ttry {\n\t\t\t\tprocess.kill(pid, \"SIGKILL\");\n\t\t\t} catch {\n\t\t\t\t// Process already dead\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
@@ -1,2 +0,0 @@
1
- export { ensureChannelMemoryFiles, ensureChannelMemoryFilesSync, getChannelSessionPath, readChannelSession, rewriteChannelSession, } from "./memory-files.js";
2
- //# sourceMappingURL=session-memory-files.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-memory-files.d.ts","sourceRoot":"","sources":["../src/session-memory-files.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,wBAAwB,EACxB,4BAA4B,EAC5B,qBAAqB,EACrB,kBAAkB,EAClB,qBAAqB,GACrB,MAAM,mBAAmB,CAAC"}
@@ -1,2 +0,0 @@
1
- export { ensureChannelMemoryFiles, ensureChannelMemoryFilesSync, getChannelSessionPath, readChannelSession, rewriteChannelSession, } from "./memory-files.js";
2
- //# sourceMappingURL=session-memory-files.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-memory-files.js","sourceRoot":"","sources":["../src/session-memory-files.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,wBAAwB,EACxB,4BAA4B,EAC5B,qBAAqB,EACrB,kBAAkB,EAClB,qBAAqB,GACrB,MAAM,mBAAmB,CAAC","sourcesContent":["export {\n\tensureChannelMemoryFiles,\n\tensureChannelMemoryFilesSync,\n\tgetChannelSessionPath,\n\treadChannelSession,\n\trewriteChannelSession,\n} from \"./memory-files.js\";\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-memory.d.ts","sourceRoot":"","sources":["../src/session-memory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,GAAG,EAAW,KAAK,EAAE,MAAM,qBAAqB,CAAC;AA2C/D,MAAM,WAAW,kBAAkB;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,oBAAoB,EAAE,MAAM,EAAE,CAAC;IAC/B,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,OAAO,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,MAAM,WAAW,0BAA0B;IAC1C,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,aAAa,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CACtD;AA0LD,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,kBAAkB,GAAG,MAAM,CAyBrE;AAcD,wBAAsB,0BAA0B,CAAC,OAAO,EAAE,0BAA0B,GAAG,OAAO,CAAC,kBAAkB,CAAC,CA4BjH"}