@oyasmi/pipiclaw 0.4.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -5
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +156 -57
- package/dist/agent.js.map +1 -1
- package/dist/context.d.ts +18 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +26 -0
- package/dist/context.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/llm-json.d.ts +7 -0
- package/dist/llm-json.d.ts.map +1 -0
- package/dist/llm-json.js +77 -0
- package/dist/llm-json.js.map +1 -0
- package/dist/markdown-sections.d.ts +6 -0
- package/dist/markdown-sections.d.ts.map +1 -0
- package/dist/markdown-sections.js +34 -0
- package/dist/markdown-sections.js.map +1 -0
- package/dist/memory-candidates.d.ts +21 -0
- package/dist/memory-candidates.d.ts.map +1 -0
- package/dist/memory-candidates.js +126 -0
- package/dist/memory-candidates.js.map +1 -0
- package/dist/memory-consolidation.d.ts.map +1 -1
- package/dist/memory-consolidation.js +28 -49
- package/dist/memory-consolidation.js.map +1 -1
- package/dist/memory-files.d.ts +3 -0
- package/dist/memory-files.d.ts.map +1 -1
- package/dist/memory-files.js +51 -0
- package/dist/memory-files.js.map +1 -1
- package/dist/memory-lifecycle.d.ts +9 -0
- package/dist/memory-lifecycle.d.ts.map +1 -1
- package/dist/memory-lifecycle.js +66 -0
- package/dist/memory-lifecycle.js.map +1 -1
- package/dist/memory-recall.d.ts +29 -0
- package/dist/memory-recall.d.ts.map +1 -0
- package/dist/memory-recall.js +218 -0
- package/dist/memory-recall.js.map +1 -0
- package/dist/prompt-builder.d.ts.map +1 -1
- package/dist/prompt-builder.js +7 -2
- package/dist/prompt-builder.js.map +1 -1
- package/dist/session-memory-files.d.ts +2 -0
- package/dist/session-memory-files.d.ts.map +1 -0
- package/dist/session-memory-files.js +2 -0
- package/dist/session-memory-files.js.map +1 -0
- package/dist/session-memory.d.ts +22 -0
- package/dist/session-memory.d.ts.map +1 -0
- package/dist/session-memory.js +274 -0
- package/dist/session-memory.js.map +1 -0
- package/dist/sidecar-worker.d.ts +27 -0
- package/dist/sidecar-worker.d.ts.map +1 -0
- package/dist/sidecar-worker.js +105 -0
- package/dist/sidecar-worker.js.map +1 -0
- package/dist/sub-agents.d.ts +10 -0
- package/dist/sub-agents.d.ts.map +1 -1
- package/dist/sub-agents.js +90 -0
- package/dist/sub-agents.js.map +1 -1
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +2 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/subagent.d.ts +6 -0
- package/dist/tools/subagent.d.ts.map +1 -1
- package/dist/tools/subagent.js +127 -12
- package/dist/tools/subagent.js.map +1 -1
- package/docs/improve-memory/design.md +537 -0
- package/docs/improve-memory/interfaces-and-tests.md +473 -0
- package/docs/improve-memory/spec.md +357 -0
- package/docs/memory-rfc.md +7 -1
- package/docs/proj-review.md +188 -0
- package/docs/test-supplementation-plan.md +553 -0
- package/package.json +3 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class LlmJsonParseError extends Error {
|
|
2
|
+
readonly rawText: string;
|
|
3
|
+
constructor(message: string, rawText: string);
|
|
4
|
+
}
|
|
5
|
+
export declare function extractJsonObject(text: string): string;
|
|
6
|
+
export declare function parseJsonObject(text: string): unknown;
|
|
7
|
+
//# sourceMappingURL=llm-json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-json.d.ts","sourceRoot":"","sources":["../src/llm-json.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAkB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAEb,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK5C;AA4DD,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAkBtD;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAErD"}
|
package/dist/llm-json.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export class LlmJsonParseError extends Error {
|
|
2
|
+
constructor(message, rawText) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "LlmJsonParseError";
|
|
5
|
+
this.rawText = rawText;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
function extractFromFence(text) {
|
|
9
|
+
const fenceMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
10
|
+
return fenceMatch?.[1]?.trim() || null;
|
|
11
|
+
}
|
|
12
|
+
function findBalancedJsonObject(text) {
|
|
13
|
+
let start = -1;
|
|
14
|
+
let depth = 0;
|
|
15
|
+
let inString = false;
|
|
16
|
+
let escaped = false;
|
|
17
|
+
for (let index = 0; index < text.length; index++) {
|
|
18
|
+
const char = text[index];
|
|
19
|
+
if (start === -1) {
|
|
20
|
+
if (char === "{") {
|
|
21
|
+
start = index;
|
|
22
|
+
depth = 1;
|
|
23
|
+
}
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (inString) {
|
|
27
|
+
if (escaped) {
|
|
28
|
+
escaped = false;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (char === "\\") {
|
|
32
|
+
escaped = true;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (char === '"') {
|
|
36
|
+
inString = false;
|
|
37
|
+
}
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (char === '"') {
|
|
41
|
+
inString = true;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (char === "{") {
|
|
45
|
+
depth++;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (char === "}") {
|
|
49
|
+
depth--;
|
|
50
|
+
if (depth === 0) {
|
|
51
|
+
return text.slice(start, index + 1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
export function extractJsonObject(text) {
|
|
58
|
+
const trimmed = text.trim();
|
|
59
|
+
if (!trimmed) {
|
|
60
|
+
throw new LlmJsonParseError("Model response was empty; expected a JSON object", text);
|
|
61
|
+
}
|
|
62
|
+
const candidates = [trimmed, extractFromFence(trimmed)].filter((candidate) => !!candidate);
|
|
63
|
+
for (const candidate of candidates) {
|
|
64
|
+
if (candidate.startsWith("{") && candidate.endsWith("}")) {
|
|
65
|
+
return candidate;
|
|
66
|
+
}
|
|
67
|
+
const balanced = findBalancedJsonObject(candidate);
|
|
68
|
+
if (balanced) {
|
|
69
|
+
return balanced;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
throw new LlmJsonParseError("Could not locate a balanced JSON object in the model response", text);
|
|
73
|
+
}
|
|
74
|
+
export function parseJsonObject(text) {
|
|
75
|
+
return JSON.parse(extractJsonObject(text));
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=llm-json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-json.js","sourceRoot":"","sources":["../src/llm-json.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAG3C,YAAY,OAAe,EAAE,OAAe;QAC3C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACxB,CAAC;CACD;AAED,SAAS,gBAAgB,CAAC,IAAY;IACrC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAC;IAC/D,OAAO,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,IAAI,CAAC;AACxC,CAAC;AAED,SAAS,sBAAsB,CAAC,IAA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C;AACb,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC7C,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;IAC5B,IAAI,CAAC,OAAO,EAAE,CAAC;QACd,MAAM,IAAI,iBAAiB,CAAC,kDAAkD,EAAE,IAAI,CAAC,CAAC;IACvF,CAAC;IAED,MAAM,UAAU,GAAG,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,SAAS,EAAuB,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IAChH,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACpC,IAAI,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAC1D,OAAO,SAAS,CAAC;QAClB,CAAC;QACD,MAAM,QAAQ,GAAG,sBAAsB,CAAC,SAAS,CAAC,CAAC;QACnD,IAAI,QAAQ,EAAE,CAAC;YACd,OAAO,QAAQ,CAAC;QACjB,CAAC;IACF,CAAC;IAED,MAAM,IAAI,iBAAiB,CAAC,+DAA+D,EAAE,IAAI,CAAC,CAAC;AACpG,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,IAAY;IAC3C,OAAO,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC;AAC5C,CAAC","sourcesContent":["export class LlmJsonParseError extends Error {\n\treadonly rawText: string;\n\n\tconstructor(message: string, rawText: string) {\n\t\tsuper(message);\n\t\tthis.name = \"LlmJsonParseError\";\n\t\tthis.rawText = rawText;\n\t}\n}\n\nfunction extractFromFence(text: string): string | null {\n\tconst fenceMatch = text.match(/```(?:json)?\\s*([\\s\\S]*?)```/i);\n\treturn fenceMatch?.[1]?.trim() || null;\n}\n\nfunction findBalancedJsonObject(text: string): string | null {\n\tlet start = -1;\n\tlet depth = 0;\n\tlet inString = false;\n\tlet escaped = false;\n\n\tfor (let index = 0; index < text.length; index++) {\n\t\tconst char = text[index];\n\n\t\tif (start === -1) {\n\t\t\tif (char === \"{\") {\n\t\t\t\tstart = index;\n\t\t\t\tdepth = 1;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (inString) {\n\t\t\tif (escaped) {\n\t\t\t\tescaped = false;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (char === \"\\\\\") {\n\t\t\t\tescaped = true;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (char === '\"') {\n\t\t\t\tinString = false;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (char === '\"') {\n\t\t\tinString = true;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (char === \"{\") {\n\t\t\tdepth++;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (char === \"}\") {\n\t\t\tdepth--;\n\t\t\tif (depth === 0) {\n\t\t\t\treturn text.slice(start, index + 1);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn null;\n}\n\nexport function extractJsonObject(text: string): string {\n\tconst trimmed = text.trim();\n\tif (!trimmed) {\n\t\tthrow new LlmJsonParseError(\"Model response was empty; expected a JSON object\", text);\n\t}\n\n\tconst candidates = [trimmed, extractFromFence(trimmed)].filter((candidate): candidate is string => !!candidate);\n\tfor (const candidate of candidates) {\n\t\tif (candidate.startsWith(\"{\") && candidate.endsWith(\"}\")) {\n\t\t\treturn candidate;\n\t\t}\n\t\tconst balanced = findBalancedJsonObject(candidate);\n\t\tif (balanced) {\n\t\t\treturn balanced;\n\t\t}\n\t}\n\n\tthrow new LlmJsonParseError(\"Could not locate a balanced JSON object in the model response\", text);\n}\n\nexport function parseJsonObject(text: string): unknown {\n\treturn JSON.parse(extractJsonObject(text));\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown-sections.d.ts","sourceRoot":"","sources":["../src/markdown-sections.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,uBAAuB;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,uBAAuB,EAAE,CAoChF"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function splitLevelOneSections(content) {
|
|
2
|
+
const normalized = content.replace(/\r/g, "").trim();
|
|
3
|
+
if (!normalized) {
|
|
4
|
+
return [];
|
|
5
|
+
}
|
|
6
|
+
const lines = normalized.split("\n");
|
|
7
|
+
const sections = [];
|
|
8
|
+
let currentHeading = "";
|
|
9
|
+
let currentLines = [];
|
|
10
|
+
const flush = () => {
|
|
11
|
+
if (!currentHeading) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const sectionContent = currentLines.join("\n").trim();
|
|
15
|
+
if (!sectionContent) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
sections.push({ heading: currentHeading, content: sectionContent });
|
|
19
|
+
};
|
|
20
|
+
for (const line of lines) {
|
|
21
|
+
if (line.startsWith("# ")) {
|
|
22
|
+
flush();
|
|
23
|
+
currentHeading = line.slice(2).trim();
|
|
24
|
+
currentLines = [];
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (currentHeading) {
|
|
28
|
+
currentLines.push(line);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
flush();
|
|
32
|
+
return sections;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=markdown-sections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown-sections.js","sourceRoot":"","sources":["../src/markdown-sections.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU,qBAAqB,CAAC,OAAe;IACpD,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACrD,IAAI,CAAC,UAAU,EAAE,CAAC;QACjB,OAAO,EAAE,CAAC;IACX,CAAC;IAED,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACrC,MAAM,QAAQ,GAA8B,EAAE,CAAC;IAC/C,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,MAAM,cAAc,GAAG,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;QACtD,IAAI,CAAC,cAAc,EAAE,CAAC;YACrB,OAAO;QACR,CAAC;QACD,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC,CAAC;IACrE,CAAC,CAAC;IAEF,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3B,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":["export interface MarkdownLevelOneSection {\n\theading: string;\n\tcontent: string;\n}\n\nexport function splitLevelOneSections(content: string): MarkdownLevelOneSection[] {\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: MarkdownLevelOneSection[] = [];\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\tconst sectionContent = currentLines.join(\"\\n\").trim();\n\t\tif (!sectionContent) {\n\t\t\treturn;\n\t\t}\n\t\tsections.push({ heading: currentHeading, content: sectionContent });\n\t};\n\n\tfor (const line of lines) {\n\t\tif (line.startsWith(\"# \")) {\n\t\t\tflush();\n\t\t\tcurrentHeading = line.slice(2).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"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface MemoryCandidate {
|
|
2
|
+
id: string;
|
|
3
|
+
source: "workspace-memory" | "channel-memory" | "channel-session" | "channel-history";
|
|
4
|
+
path: string;
|
|
5
|
+
title: string;
|
|
6
|
+
content: string;
|
|
7
|
+
timestamp?: string;
|
|
8
|
+
sectionKind?: string;
|
|
9
|
+
priority: number;
|
|
10
|
+
}
|
|
11
|
+
export interface BuildMemoryCandidatesOptions {
|
|
12
|
+
workspaceDir: string;
|
|
13
|
+
channelDir: string;
|
|
14
|
+
cache?: MemoryCandidateCache;
|
|
15
|
+
}
|
|
16
|
+
export interface MemoryCandidateCache {
|
|
17
|
+
entries: Map<string, Promise<MemoryCandidate[]>>;
|
|
18
|
+
}
|
|
19
|
+
export declare function createMemoryCandidateCache(): MemoryCandidateCache;
|
|
20
|
+
export declare function buildMemoryCandidates(options: BuildMemoryCandidatesOptions): Promise<MemoryCandidate[]>;
|
|
21
|
+
//# sourceMappingURL=memory-candidates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-candidates.d.ts","sourceRoot":"","sources":["../src/memory-candidates.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,eAAe;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,kBAAkB,GAAG,gBAAgB,GAAG,iBAAiB,GAAG,iBAAiB,CAAC;IACtF,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,4BAA4B;IAC5C,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,oBAAoB,CAAC;CAC7B;AAED,MAAM,WAAW,oBAAoB;IACpC,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC;CACjD;AAED,wBAAgB,0BAA0B,IAAI,oBAAoB,CAIjE;AAsHD,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,4BAA4B,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC,CAiB7G"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { readFile } from "fs/promises";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { splitLevelOneSections } from "./markdown-sections.js";
|
|
4
|
+
import { getChannelHistoryPath, getChannelMemoryPath, getChannelSessionPath, splitMarkdownSections, } from "./memory-files.js";
|
|
5
|
+
export function createMemoryCandidateCache() {
|
|
6
|
+
return {
|
|
7
|
+
entries: new Map(),
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
function normalizeContent(content) {
|
|
11
|
+
return content.replace(/\r/g, "").trim();
|
|
12
|
+
}
|
|
13
|
+
async function readOptionalFile(path) {
|
|
14
|
+
try {
|
|
15
|
+
return normalizeContent(await readFile(path, "utf-8"));
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return "";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function slugify(value) {
|
|
22
|
+
return (value
|
|
23
|
+
.toLowerCase()
|
|
24
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
25
|
+
.replace(/^-+|-+$/g, "") || "section");
|
|
26
|
+
}
|
|
27
|
+
function inferPriority(source, title) {
|
|
28
|
+
const normalizedTitle = title.trim().toLowerCase();
|
|
29
|
+
if (source === "channel-session") {
|
|
30
|
+
if (normalizedTitle === "current state")
|
|
31
|
+
return 120;
|
|
32
|
+
if (normalizedTitle === "next steps")
|
|
33
|
+
return 115;
|
|
34
|
+
if (normalizedTitle === "errors & corrections")
|
|
35
|
+
return 110;
|
|
36
|
+
if (normalizedTitle === "constraints")
|
|
37
|
+
return 108;
|
|
38
|
+
if (normalizedTitle === "user intent")
|
|
39
|
+
return 105;
|
|
40
|
+
return 100;
|
|
41
|
+
}
|
|
42
|
+
if (source === "channel-memory") {
|
|
43
|
+
if (normalizedTitle.includes("constraints"))
|
|
44
|
+
return 88;
|
|
45
|
+
if (normalizedTitle.includes("decisions"))
|
|
46
|
+
return 86;
|
|
47
|
+
if (normalizedTitle.includes("open loops"))
|
|
48
|
+
return 84;
|
|
49
|
+
return 80;
|
|
50
|
+
}
|
|
51
|
+
if (source === "workspace-memory") {
|
|
52
|
+
return 60;
|
|
53
|
+
}
|
|
54
|
+
return 40;
|
|
55
|
+
}
|
|
56
|
+
function buildCandidate(source, path, title, content, timestamp) {
|
|
57
|
+
return {
|
|
58
|
+
id: `${source}:${slugify(title)}:${timestamp ?? ""}`,
|
|
59
|
+
source,
|
|
60
|
+
path,
|
|
61
|
+
title,
|
|
62
|
+
content,
|
|
63
|
+
timestamp,
|
|
64
|
+
sectionKind: title.trim().toLowerCase(),
|
|
65
|
+
priority: inferPriority(source, title),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function buildCacheKey(options) {
|
|
69
|
+
return `${options.workspaceDir}\u0000${options.channelDir}`;
|
|
70
|
+
}
|
|
71
|
+
function buildWorkspaceOrChannelMemoryCandidates(source, path, content) {
|
|
72
|
+
const sections = splitMarkdownSections(content);
|
|
73
|
+
if (sections.length === 0 && content) {
|
|
74
|
+
return [
|
|
75
|
+
buildCandidate(source, path, source === "workspace-memory" ? "Workspace Memory" : "Channel Memory", content),
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
return sections
|
|
79
|
+
.filter((section) => section.content.trim())
|
|
80
|
+
.map((section) => buildCandidate(source, path, section.heading, section.content));
|
|
81
|
+
}
|
|
82
|
+
function buildSessionCandidates(path, content) {
|
|
83
|
+
return splitLevelOneSections(content)
|
|
84
|
+
.filter((section) => section.content.trim())
|
|
85
|
+
.map((section) => buildCandidate("channel-session", path, section.heading, section.content));
|
|
86
|
+
}
|
|
87
|
+
function buildHistoryCandidates(path, content) {
|
|
88
|
+
return splitMarkdownSections(content)
|
|
89
|
+
.filter((section) => section.content.trim())
|
|
90
|
+
.map((section) => buildCandidate("channel-history", path, section.heading, section.content, section.heading));
|
|
91
|
+
}
|
|
92
|
+
async function buildMemoryCandidatesUncached(options) {
|
|
93
|
+
const workspaceMemoryPath = join(options.workspaceDir, "MEMORY.md");
|
|
94
|
+
const channelMemoryPath = getChannelMemoryPath(options.channelDir);
|
|
95
|
+
const channelSessionPath = getChannelSessionPath(options.channelDir);
|
|
96
|
+
const channelHistoryPath = getChannelHistoryPath(options.channelDir);
|
|
97
|
+
const [workspaceMemory, channelMemory, channelSession, channelHistory] = await Promise.all([
|
|
98
|
+
readOptionalFile(workspaceMemoryPath),
|
|
99
|
+
readOptionalFile(channelMemoryPath),
|
|
100
|
+
readOptionalFile(channelSessionPath),
|
|
101
|
+
readOptionalFile(channelHistoryPath),
|
|
102
|
+
]);
|
|
103
|
+
return [
|
|
104
|
+
...buildSessionCandidates(channelSessionPath, channelSession),
|
|
105
|
+
...buildWorkspaceOrChannelMemoryCandidates("channel-memory", channelMemoryPath, channelMemory),
|
|
106
|
+
...buildWorkspaceOrChannelMemoryCandidates("workspace-memory", workspaceMemoryPath, workspaceMemory),
|
|
107
|
+
...buildHistoryCandidates(channelHistoryPath, channelHistory),
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
export async function buildMemoryCandidates(options) {
|
|
111
|
+
if (!options.cache) {
|
|
112
|
+
return buildMemoryCandidatesUncached(options);
|
|
113
|
+
}
|
|
114
|
+
const key = buildCacheKey(options);
|
|
115
|
+
const cached = options.cache.entries.get(key);
|
|
116
|
+
if (cached) {
|
|
117
|
+
return cached;
|
|
118
|
+
}
|
|
119
|
+
const pending = buildMemoryCandidatesUncached(options).catch((error) => {
|
|
120
|
+
options.cache?.entries.delete(key);
|
|
121
|
+
throw error;
|
|
122
|
+
});
|
|
123
|
+
options.cache.entries.set(key, pending);
|
|
124
|
+
return pending;
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=memory-candidates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-candidates.js","sourceRoot":"","sources":["../src/memory-candidates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EACN,qBAAqB,EACrB,oBAAoB,EACpB,qBAAqB,EACrB,qBAAqB,GACrB,MAAM,mBAAmB,CAAC;AAuB3B,MAAM,UAAU,0BAA0B;IACzC,OAAO;QACN,OAAO,EAAE,IAAI,GAAG,EAAE;KAClB,CAAC;AACH,CAAC;AAED,SAAS,gBAAgB,CAAC,OAAe;IACxC,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;AAC1C,CAAC;AAED,KAAK,UAAU,gBAAgB,CAAC,IAAY;IAC3C,IAAI,CAAC;QACJ,OAAO,gBAAgB,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IACxD,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,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pG,GAAG,sBAAsB,CAAC,kBAAkB,EAAE,cAAc,CAAC;KAC7D,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,OAAqC;IAChF,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACpB,OAAO,6BAA6B,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED,MAAM,GAAG,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IACnC,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC9C,IAAI,MAAM,EAAE,CAAC;QACZ,OAAO,MAAM,CAAC;IACf,CAAC;IAED,MAAM,OAAO,GAAG,6BAA6B,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QACtE,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACnC,MAAM,KAAK,CAAC;IACb,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACxC,OAAO,OAAO,CAAC;AAChB,CAAC","sourcesContent":["import { readFile } from \"fs/promises\";\nimport { join } from \"path\";\nimport { splitLevelOneSections } from \"./markdown-sections.js\";\nimport {\n\tgetChannelHistoryPath,\n\tgetChannelMemoryPath,\n\tgetChannelSessionPath,\n\tsplitMarkdownSections,\n} from \"./memory-files.js\";\n\nexport interface MemoryCandidate {\n\tid: string;\n\tsource: \"workspace-memory\" | \"channel-memory\" | \"channel-session\" | \"channel-history\";\n\tpath: string;\n\ttitle: string;\n\tcontent: string;\n\ttimestamp?: string;\n\tsectionKind?: string;\n\tpriority: number;\n}\n\nexport interface BuildMemoryCandidatesOptions {\n\tworkspaceDir: string;\n\tchannelDir: string;\n\tcache?: MemoryCandidateCache;\n}\n\nexport interface MemoryCandidateCache {\n\tentries: Map<string, Promise<MemoryCandidate[]>>;\n}\n\nexport function createMemoryCandidateCache(): MemoryCandidateCache {\n\treturn {\n\t\tentries: new Map(),\n\t};\n}\n\nfunction normalizeContent(content: string): string {\n\treturn content.replace(/\\r/g, \"\").trim();\n}\n\nasync function readOptionalFile(path: string): Promise<string> {\n\ttry {\n\t\treturn normalizeContent(await readFile(path, \"utf-8\"));\n\t} catch {\n\t\treturn \"\";\n\t}\n}\n\nfunction slugify(value: string): string {\n\treturn (\n\t\tvalue\n\t\t\t.toLowerCase()\n\t\t\t.replace(/[^a-z0-9]+/g, \"-\")\n\t\t\t.replace(/^-+|-+$/g, \"\") || \"section\"\n\t);\n}\n\nfunction inferPriority(source: MemoryCandidate[\"source\"], title: string): number {\n\tconst normalizedTitle = title.trim().toLowerCase();\n\tif (source === \"channel-session\") {\n\t\tif (normalizedTitle === \"current state\") return 120;\n\t\tif (normalizedTitle === \"next steps\") return 115;\n\t\tif (normalizedTitle === \"errors & corrections\") return 110;\n\t\tif (normalizedTitle === \"constraints\") return 108;\n\t\tif (normalizedTitle === \"user intent\") return 105;\n\t\treturn 100;\n\t}\n\tif (source === \"channel-memory\") {\n\t\tif (normalizedTitle.includes(\"constraints\")) return 88;\n\t\tif (normalizedTitle.includes(\"decisions\")) return 86;\n\t\tif (normalizedTitle.includes(\"open loops\")) return 84;\n\t\treturn 80;\n\t}\n\tif (source === \"workspace-memory\") {\n\t\treturn 60;\n\t}\n\treturn 40;\n}\n\nfunction buildCandidate(\n\tsource: MemoryCandidate[\"source\"],\n\tpath: string,\n\ttitle: string,\n\tcontent: string,\n\ttimestamp?: string,\n): MemoryCandidate {\n\treturn {\n\t\tid: `${source}:${slugify(title)}:${timestamp ?? \"\"}`,\n\t\tsource,\n\t\tpath,\n\t\ttitle,\n\t\tcontent,\n\t\ttimestamp,\n\t\tsectionKind: title.trim().toLowerCase(),\n\t\tpriority: inferPriority(source, title),\n\t};\n}\n\nfunction buildCacheKey(options: BuildMemoryCandidatesOptions): string {\n\treturn `${options.workspaceDir}\\u0000${options.channelDir}`;\n}\n\nfunction buildWorkspaceOrChannelMemoryCandidates(\n\tsource: \"workspace-memory\" | \"channel-memory\",\n\tpath: string,\n\tcontent: string,\n): MemoryCandidate[] {\n\tconst sections = splitMarkdownSections(content);\n\tif (sections.length === 0 && content) {\n\t\treturn [\n\t\t\tbuildCandidate(source, path, source === \"workspace-memory\" ? \"Workspace Memory\" : \"Channel Memory\", content),\n\t\t];\n\t}\n\n\treturn sections\n\t\t.filter((section) => section.content.trim())\n\t\t.map((section) => buildCandidate(source, path, section.heading, section.content));\n}\n\nfunction buildSessionCandidates(path: string, content: string): MemoryCandidate[] {\n\treturn splitLevelOneSections(content)\n\t\t.filter((section) => section.content.trim())\n\t\t.map((section) => buildCandidate(\"channel-session\", path, section.heading, section.content));\n}\n\nfunction buildHistoryCandidates(path: string, content: string): MemoryCandidate[] {\n\treturn splitMarkdownSections(content)\n\t\t.filter((section) => section.content.trim())\n\t\t.map((section) => buildCandidate(\"channel-history\", path, section.heading, section.content, section.heading));\n}\n\nasync function buildMemoryCandidatesUncached(options: BuildMemoryCandidatesOptions): Promise<MemoryCandidate[]> {\n\tconst workspaceMemoryPath = join(options.workspaceDir, \"MEMORY.md\");\n\tconst channelMemoryPath = getChannelMemoryPath(options.channelDir);\n\tconst channelSessionPath = getChannelSessionPath(options.channelDir);\n\tconst channelHistoryPath = getChannelHistoryPath(options.channelDir);\n\n\tconst [workspaceMemory, channelMemory, channelSession, channelHistory] = await Promise.all([\n\t\treadOptionalFile(workspaceMemoryPath),\n\t\treadOptionalFile(channelMemoryPath),\n\t\treadOptionalFile(channelSessionPath),\n\t\treadOptionalFile(channelHistoryPath),\n\t]);\n\n\treturn [\n\t\t...buildSessionCandidates(channelSessionPath, channelSession),\n\t\t...buildWorkspaceOrChannelMemoryCandidates(\"channel-memory\", channelMemoryPath, channelMemory),\n\t\t...buildWorkspaceOrChannelMemoryCandidates(\"workspace-memory\", workspaceMemoryPath, workspaceMemory),\n\t\t...buildHistoryCandidates(channelHistoryPath, channelHistory),\n\t];\n}\n\nexport async function buildMemoryCandidates(options: BuildMemoryCandidatesOptions): Promise<MemoryCandidate[]> {\n\tif (!options.cache) {\n\t\treturn buildMemoryCandidatesUncached(options);\n\t}\n\n\tconst key = buildCacheKey(options);\n\tconst cached = options.cache.entries.get(key);\n\tif (cached) {\n\t\treturn cached;\n\t}\n\n\tconst pending = buildMemoryCandidatesUncached(options).catch((error) => {\n\t\toptions.cache?.entries.delete(key);\n\t\tthrow error;\n\t});\n\toptions.cache.entries.set(key, pending);\n\treturn pending;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-consolidation.d.ts","sourceRoot":"","sources":["../src/memory-consolidation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"memory-consolidation.d.ts","sourceRoot":"","sources":["../src/memory-consolidation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,GAAG,EAA6B,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjF,OAAO,EAEN,KAAK,YAAY,EAGjB,MAAM,+BAA+B,CAAC;AA2EvC,MAAM,WAAW,uBAAuB;IACvC,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,aAAa,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,cAAc,CAAC,EAAE,YAAY,EAAE,CAAC;CAChC;AAED,MAAM,WAAW,yBAAyB;IACzC,OAAO,EAAE,OAAO,CAAC;IACjB,qBAAqB,EAAE,MAAM,CAAC;IAC9B,oBAAoB,EAAE,OAAO,CAAC;CAC9B;AAED,MAAM,WAAW,2BAA2B;IAC3C,aAAa,EAAE,OAAO,CAAC;IACvB,aAAa,EAAE,OAAO,CAAC;CACvB;AAkJD,wBAAsB,sBAAsB,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAkCjH;AA8DD,wBAAsB,wBAAwB,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAQrH"}
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { appendChannelHistoryBlock, appendChannelMemoryUpdate, readChannelHistory, readChannelMemory, rewriteChannelHistory, rewriteChannelMemory, splitMarkdownSections, } from "./memory-files.js";
|
|
1
|
+
import { getLatestCompactionEntry, serializeConversation, } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { parseJsonObject } from "./llm-json.js";
|
|
3
|
+
import { appendChannelHistoryBlock, appendChannelMemoryUpdate, readChannelHistory, readChannelMemory, readChannelSession, rewriteChannelHistory, rewriteChannelMemory, splitMarkdownSections, } from "./memory-files.js";
|
|
4
|
+
import { runSidecarTask } from "./sidecar-worker.js";
|
|
4
5
|
const INLINE_TRANSCRIPT_MAX_CHARS = 28_000;
|
|
5
6
|
const MEMORY_CLEANUP_LENGTH_THRESHOLD = 8_000;
|
|
6
7
|
const MEMORY_UPDATE_BLOCK_THRESHOLD = 6;
|
|
7
8
|
const HISTORY_LENGTH_THRESHOLD = 16_000;
|
|
8
9
|
const HISTORY_BLOCK_THRESHOLD = 8;
|
|
9
10
|
const HISTORY_RECENT_BLOCKS_TO_KEEP = 4;
|
|
11
|
+
const INLINE_CONSOLIDATION_TIMEOUT_MS = 20_000;
|
|
12
|
+
const MEMORY_CLEANUP_TIMEOUT_MS = 30_000;
|
|
13
|
+
const HISTORY_FOLDING_TIMEOUT_MS = 30_000;
|
|
10
14
|
const INLINE_CONSOLIDATION_SYSTEM_PROMPT = `You are a runtime memory consolidation worker for Pipiclaw.
|
|
11
15
|
|
|
12
16
|
Return strict JSON only. Do not wrap in Markdown fences.
|
|
@@ -22,6 +26,7 @@ Rules:
|
|
|
22
26
|
- Each memoryEntries item must be a standalone sentence fragment suitable for a Markdown bullet without the bullet prefix.
|
|
23
27
|
- Do not include raw transcript quotes unless essential.
|
|
24
28
|
- Do not include ephemeral chatter, obvious one-shot acknowledgements, or formatting instructions.
|
|
29
|
+
- Prefer leaving highly volatile step-by-step execution state in SESSION.md rather than promoting it into durable memory.
|
|
25
30
|
- historyBlock: concise Markdown summarizing the conversation chunk for later recovery.
|
|
26
31
|
- Prefer short bullets and short paragraphs.
|
|
27
32
|
- If there is nothing worth storing, return empty values.`;
|
|
@@ -34,6 +39,7 @@ Goals:
|
|
|
34
39
|
- Remove outdated entries, duplicates, and verbose phrasing.
|
|
35
40
|
- Organize the result with stable sections where relevant.
|
|
36
41
|
- Prefer concise bullets over prose.
|
|
42
|
+
- Remove content that is clearly transient session-state and belongs in SESSION.md instead.
|
|
37
43
|
|
|
38
44
|
Suggested sections:
|
|
39
45
|
- ## Identity / Participants
|
|
@@ -65,24 +71,8 @@ function clipTranscript(text, maxChars) {
|
|
|
65
71
|
const tailChars = maxChars - headChars;
|
|
66
72
|
return `${normalized.slice(0, headChars)}\n\n[... omitted middle section ...]\n\n${normalized.slice(-tailChars)}`;
|
|
67
73
|
}
|
|
68
|
-
function extractJsonObject(text) {
|
|
69
|
-
const trimmed = text.trim();
|
|
70
|
-
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
|
|
71
|
-
return trimmed;
|
|
72
|
-
}
|
|
73
|
-
const fenceMatch = trimmed.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
74
|
-
if (fenceMatch?.[1]) {
|
|
75
|
-
return fenceMatch[1].trim();
|
|
76
|
-
}
|
|
77
|
-
const firstBrace = trimmed.indexOf("{");
|
|
78
|
-
const lastBrace = trimmed.lastIndexOf("}");
|
|
79
|
-
if (firstBrace >= 0 && lastBrace > firstBrace) {
|
|
80
|
-
return trimmed.slice(firstBrace, lastBrace + 1);
|
|
81
|
-
}
|
|
82
|
-
return trimmed;
|
|
83
|
-
}
|
|
84
74
|
function parseConsolidationResponse(text) {
|
|
85
|
-
const parsed =
|
|
75
|
+
const parsed = parseJsonObject(text);
|
|
86
76
|
return {
|
|
87
77
|
memoryEntries: Array.isArray(parsed.memoryEntries)
|
|
88
78
|
? parsed.memoryEntries
|
|
@@ -142,38 +132,27 @@ function hasMeaningfulMessages(messages) {
|
|
|
142
132
|
function countMatchingSectionHeadings(content, prefix) {
|
|
143
133
|
return splitMarkdownSections(content).filter((section) => section.heading.startsWith(prefix)).length;
|
|
144
134
|
}
|
|
145
|
-
async function runWorkerPrompt(model, resolveApiKey, systemPrompt, prompt) {
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
convertToLlm,
|
|
155
|
-
getApiKey: async () => apiKey,
|
|
135
|
+
async function runWorkerPrompt(name, model, resolveApiKey, systemPrompt, prompt, timeoutMs) {
|
|
136
|
+
const result = await runSidecarTask({
|
|
137
|
+
name,
|
|
138
|
+
model,
|
|
139
|
+
resolveApiKey,
|
|
140
|
+
systemPrompt,
|
|
141
|
+
prompt,
|
|
142
|
+
timeoutMs,
|
|
143
|
+
parse: (text) => text.trim(),
|
|
156
144
|
});
|
|
157
|
-
|
|
158
|
-
await worker.waitForIdle();
|
|
159
|
-
const lastMessage = worker.state.messages[worker.state.messages.length - 1];
|
|
160
|
-
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
161
|
-
throw new Error("Consolidation worker returned no assistant message");
|
|
162
|
-
}
|
|
163
|
-
if (lastMessage.stopReason === "error" || lastMessage.stopReason === "aborted") {
|
|
164
|
-
throw new Error(lastMessage.errorMessage || "Consolidation worker failed");
|
|
165
|
-
}
|
|
166
|
-
return lastMessage.content
|
|
167
|
-
.filter((part) => part.type === "text")
|
|
168
|
-
.map((part) => part.text)
|
|
169
|
-
.join("\n")
|
|
170
|
-
.trim();
|
|
145
|
+
return result.output;
|
|
171
146
|
}
|
|
172
147
|
async function buildInlineConsolidationResponse(options, messages) {
|
|
173
148
|
const transcript = clipTranscript(serializeConversation(messages), INLINE_TRANSCRIPT_MAX_CHARS);
|
|
174
149
|
const currentMemory = clipTranscript(await readChannelMemory(options.channelDir), 8_000);
|
|
150
|
+
const currentSession = clipTranscript(await readChannelSession(options.channelDir), 8_000);
|
|
175
151
|
const currentHistory = clipTranscript(await readChannelHistory(options.channelDir), 8_000);
|
|
176
|
-
const prompt = `
|
|
152
|
+
const prompt = `Current SESSION.md:
|
|
153
|
+
${currentSession || "(empty)"}
|
|
154
|
+
|
|
155
|
+
Channel memory file:
|
|
177
156
|
${currentMemory || "(empty)"}
|
|
178
157
|
|
|
179
158
|
Channel history file:
|
|
@@ -181,7 +160,7 @@ ${currentHistory || "(empty)"}
|
|
|
181
160
|
|
|
182
161
|
Conversation chunk to persist:
|
|
183
162
|
${transcript || "(empty)"}`;
|
|
184
|
-
const rawResponse = await runWorkerPrompt(options.model, options.resolveApiKey, INLINE_CONSOLIDATION_SYSTEM_PROMPT, prompt);
|
|
163
|
+
const rawResponse = await runWorkerPrompt("memory-inline-consolidation", options.model, options.resolveApiKey, INLINE_CONSOLIDATION_SYSTEM_PROMPT, prompt, INLINE_CONSOLIDATION_TIMEOUT_MS);
|
|
185
164
|
return parseConsolidationResponse(rawResponse);
|
|
186
165
|
}
|
|
187
166
|
export async function runInlineConsolidation(options) {
|
|
@@ -218,7 +197,7 @@ async function cleanupChannelMemory(options, currentMemory) {
|
|
|
218
197
|
}
|
|
219
198
|
const prompt = `Current MEMORY.md:
|
|
220
199
|
${currentMemory}`;
|
|
221
|
-
const nextMemory = await runWorkerPrompt(options.model, options.resolveApiKey, MEMORY_CLEANUP_SYSTEM_PROMPT, prompt);
|
|
200
|
+
const nextMemory = await runWorkerPrompt("memory-cleanup", options.model, options.resolveApiKey, MEMORY_CLEANUP_SYSTEM_PROMPT, prompt, MEMORY_CLEANUP_TIMEOUT_MS);
|
|
222
201
|
await rewriteChannelMemory(options.channelDir, nextMemory);
|
|
223
202
|
return true;
|
|
224
203
|
}
|
|
@@ -234,7 +213,7 @@ async function foldChannelHistory(options, currentHistory) {
|
|
|
234
213
|
const recentSections = sections.slice(-HISTORY_RECENT_BLOCKS_TO_KEEP);
|
|
235
214
|
const prompt = `Older history blocks to fold:
|
|
236
215
|
${olderSections.map((section) => `## ${section.heading}\n\n${section.content}`).join("\n\n")}`;
|
|
237
|
-
const foldedSummary = await runWorkerPrompt(options.model, options.resolveApiKey, HISTORY_FOLDING_SYSTEM_PROMPT, prompt);
|
|
216
|
+
const foldedSummary = await runWorkerPrompt("history-folding", options.model, options.resolveApiKey, HISTORY_FOLDING_SYSTEM_PROMPT, prompt, HISTORY_FOLDING_TIMEOUT_MS);
|
|
238
217
|
const foldedHeading = `## Folded History Through ${olderSections[olderSections.length - 1]?.heading ?? new Date().toISOString()}`;
|
|
239
218
|
const rebuiltHistory = [
|
|
240
219
|
"# Channel History",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-consolidation.js","sourceRoot":"","sources":["../src/memory-consolidation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAEpD,OAAO,EACN,YAAY,EACZ,wBAAwB,EAGxB,qBAAqB,GACrB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACN,yBAAyB,EACzB,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,EACjB,qBAAqB,EACrB,oBAAoB,EACpB,qBAAqB,GACrB,MAAM,mBAAmB,CAAC;AAE3B,MAAM,2BAA2B,GAAG,MAAM,CAAC;AAC3C,MAAM,+BAA+B,GAAG,KAAK,CAAC;AAC9C,MAAM,6BAA6B,GAAG,CAAC,CAAC;AACxC,MAAM,wBAAwB,GAAG,MAAM,CAAC;AACxC,MAAM,uBAAuB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AC,CAAC;IAExE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AACzC,CAAC","sourcesContent":["import type { AgentMessage } from \"@mariozechner/pi-agent-core\";\nimport { Agent } from \"@mariozechner/pi-agent-core\";\nimport type { Api, AssistantMessage, Message, Model } from \"@mariozechner/pi-ai\";\nimport {\n\tconvertToLlm,\n\tgetLatestCompactionEntry,\n\ttype SessionEntry,\n\ttype SessionMessageEntry,\n\tserializeConversation,\n} from \"@mariozechner/pi-coding-agent\";\nimport {\n\tappendChannelHistoryBlock,\n\tappendChannelMemoryUpdate,\n\treadChannelHistory,\n\treadChannelMemory,\n\trewriteChannelHistory,\n\trewriteChannelMemory,\n\tsplitMarkdownSections,\n} from \"./memory-files.js\";\n\nconst INLINE_TRANSCRIPT_MAX_CHARS = 28_000;\nconst MEMORY_CLEANUP_LENGTH_THRESHOLD = 8_000;\nconst MEMORY_UPDATE_BLOCK_THRESHOLD = 6;\nconst HISTORY_LENGTH_THRESHOLD = 16_000;\nconst HISTORY_BLOCK_THRESHOLD = 8;\nconst HISTORY_RECENT_BLOCKS_TO_KEEP = 4;\n\nconst INLINE_CONSOLIDATION_SYSTEM_PROMPT = `You are a runtime memory consolidation worker for Pipiclaw.\n\nReturn strict JSON only. Do not wrap in Markdown fences.\n\nOutput schema:\n{\n \"memoryEntries\": [\"string\"],\n \"historyBlock\": \"string\"\n}\n\nRules:\n- memoryEntries: concise durable facts, decisions, preferences, constraints, current work state, or open loops that should survive compaction.\n- Each memoryEntries item must be a standalone sentence fragment suitable for a Markdown bullet without the bullet prefix.\n- Do not include raw transcript quotes unless essential.\n- Do not include ephemeral chatter, obvious one-shot acknowledgements, or formatting instructions.\n- historyBlock: concise Markdown summarizing the conversation chunk for later recovery.\n- Prefer short bullets and short paragraphs.\n- If there is nothing worth storing, return empty values.`;\n\nconst MEMORY_CLEANUP_SYSTEM_PROMPT = `You are rewriting a Pipiclaw channel MEMORY.md file.\n\nReturn Markdown only. Do not use code fences.\n\nGoals:\n- Keep only durable and useful channel memory.\n- Remove outdated entries, duplicates, and verbose phrasing.\n- Organize the result with stable sections where relevant.\n- Prefer concise bullets over prose.\n\nSuggested sections:\n- ## Identity / Participants\n- ## Preferences\n- ## Ongoing Work\n- ## Constraints\n- ## Decisions\n- ## Open Loops\n\nOmit empty sections.`;\n\nconst HISTORY_FOLDING_SYSTEM_PROMPT = `You are folding older HISTORY.md blocks for Pipiclaw.\n\nReturn Markdown only. Do not use code fences.\n\nGoals:\n- Compress older history blocks into one concise summary block.\n- Keep important decisions, milestones, and unresolved outcomes.\n- Remove redundancy and transcript-like detail.\n- Preserve a chronological narrative at a high level.`;\n\nexport interface ConsolidationRunOptions {\n\tchannelDir: string;\n\tmodel: Model<Api>;\n\tresolveApiKey: (model: Model<Api>) => Promise<string>;\n\tmessages: AgentMessage[];\n\tsessionEntries?: SessionEntry[];\n}\n\nexport interface InlineConsolidationResult {\n\tskipped: boolean;\n\tappendedMemoryEntries: number;\n\tappendedHistoryBlock: boolean;\n}\n\nexport interface BackgroundMaintenanceResult {\n\tcleanedMemory: boolean;\n\tfoldedHistory: boolean;\n}\n\ninterface ConsolidationResponse {\n\tmemoryEntries: string[];\n\thistoryBlock: string;\n}\n\nfunction normalizeText(text: string): string {\n\treturn text.replace(/\\r/g, \"\").trim();\n}\n\nfunction clipTranscript(text: string, maxChars: number): string {\n\tconst normalized = normalizeText(text);\n\tif (normalized.length <= maxChars) {\n\t\treturn normalized;\n\t}\n\n\tconst headChars = Math.floor(maxChars * 0.35);\n\tconst tailChars = maxChars - headChars;\n\treturn `${normalized.slice(0, headChars)}\\n\\n[... omitted middle section ...]\\n\\n${normalized.slice(-tailChars)}`;\n}\n\nfunction extractJsonObject(text: string): string {\n\tconst trimmed = text.trim();\n\tif (trimmed.startsWith(\"{\") && trimmed.endsWith(\"}\")) {\n\t\treturn trimmed;\n\t}\n\n\tconst fenceMatch = trimmed.match(/```(?:json)?\\s*([\\s\\S]*?)```/i);\n\tif (fenceMatch?.[1]) {\n\t\treturn fenceMatch[1].trim();\n\t}\n\n\tconst firstBrace = trimmed.indexOf(\"{\");\n\tconst lastBrace = trimmed.lastIndexOf(\"}\");\n\tif (firstBrace >= 0 && lastBrace > firstBrace) {\n\t\treturn trimmed.slice(firstBrace, lastBrace + 1);\n\t}\n\n\treturn trimmed;\n}\n\nfunction parseConsolidationResponse(text: string): ConsolidationResponse {\n\tconst parsed = JSON.parse(extractJsonObject(text)) as Partial<ConsolidationResponse>;\n\treturn {\n\t\tmemoryEntries: Array.isArray(parsed.memoryEntries)\n\t\t\t? parsed.memoryEntries\n\t\t\t\t\t.map((entry) => (typeof entry === \"string\" ? entry.trim() : \"\"))\n\t\t\t\t\t.filter((entry) => entry.length > 0)\n\t\t\t: [],\n\t\thistoryBlock: typeof parsed.historyBlock === \"string\" ? parsed.historyBlock.trim() : \"\",\n\t};\n}\n\nfunction getLatestCompactionBoundary(entries: SessionEntry[]): number {\n\tconst latestCompaction = getLatestCompactionEntry(entries);\n\tif (!latestCompaction) {\n\t\treturn 0;\n\t}\n\n\tconst boundaryIndex = entries.findIndex((entry) => entry.id === latestCompaction.firstKeptEntryId);\n\treturn boundaryIndex >= 0 ? boundaryIndex : 0;\n}\n\nfunction isMessage(entry: SessionEntry): entry is SessionMessageEntry {\n\treturn entry.type === \"message\";\n}\n\nfunction isStandardAgentMessage(message: AgentMessage): message is Message {\n\treturn (\n\t\ttypeof message === \"object\" &&\n\t\tmessage !== null &&\n\t\t\"role\" in message &&\n\t\t(message.role === \"user\" || message.role === \"assistant\" || message.role === \"toolResult\")\n\t);\n}\n\nfunction buildMessagesForConsolidation(messages: AgentMessage[]): Message[] {\n\treturn messages.filter(isStandardAgentMessage);\n}\n\nfunction extractMessagesFromSessionEntries(entries: SessionEntry[]): AgentMessage[] {\n\treturn entries.filter(isMessage).map((entry) => entry.message);\n}\n\nfunction hasMeaningfulMessages(messages: Message[]): boolean {\n\tlet meaningfulCount = 0;\n\tfor (const message of messages) {\n\t\tif (message.role === \"user\") {\n\t\t\tconst text =\n\t\t\t\ttypeof message.content === \"string\"\n\t\t\t\t\t? message.content\n\t\t\t\t\t: message.content.map((part) => (part.type === \"text\" ? part.text : \"[image]\")).join(\"\\n\");\n\t\t\tif (text.trim()) meaningfulCount++;\n\t\t} else if (message.role === \"assistant\") {\n\t\t\tconst text = message.content\n\t\t\t\t.filter(\n\t\t\t\t\t(part): part is Extract<AssistantMessage[\"content\"][number], { type: \"text\" }> => part.type === \"text\",\n\t\t\t\t)\n\t\t\t\t.map((part) => part.text)\n\t\t\t\t.join(\"\\n\");\n\t\t\tif (text.trim()) meaningfulCount++;\n\t\t}\n\t\tif (meaningfulCount >= 2) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nfunction countMatchingSectionHeadings(content: string, prefix: string): number {\n\treturn splitMarkdownSections(content).filter((section) => section.heading.startsWith(prefix)).length;\n}\n\nasync function runWorkerPrompt(\n\tmodel: Model<Api>,\n\tresolveApiKey: (model: Model<Api>) => Promise<string>,\n\tsystemPrompt: string,\n\tprompt: string,\n): Promise<string> {\n\tconst apiKey = await resolveApiKey(model);\n\tconst worker = new Agent({\n\t\tinitialState: {\n\t\t\tsystemPrompt,\n\t\t\tmodel,\n\t\t\tthinkingLevel: \"off\",\n\t\t\ttools: [],\n\t\t},\n\t\tconvertToLlm,\n\t\tgetApiKey: async () => apiKey,\n\t});\n\n\tawait worker.prompt(prompt);\n\tawait worker.waitForIdle();\n\n\tconst lastMessage = worker.state.messages[worker.state.messages.length - 1];\n\tif (!lastMessage || lastMessage.role !== \"assistant\") {\n\t\tthrow new Error(\"Consolidation worker returned no assistant message\");\n\t}\n\n\tif (lastMessage.stopReason === \"error\" || lastMessage.stopReason === \"aborted\") {\n\t\tthrow new Error(lastMessage.errorMessage || \"Consolidation worker failed\");\n\t}\n\n\treturn lastMessage.content\n\t\t.filter((part): part is Extract<AssistantMessage[\"content\"][number], { type: \"text\" }> => part.type === \"text\")\n\t\t.map((part) => part.text)\n\t\t.join(\"\\n\")\n\t\t.trim();\n}\n\nasync function buildInlineConsolidationResponse(\n\toptions: ConsolidationRunOptions,\n\tmessages: Message[],\n): Promise<ConsolidationResponse> {\n\tconst transcript = clipTranscript(serializeConversation(messages), INLINE_TRANSCRIPT_MAX_CHARS);\n\tconst currentMemory = clipTranscript(await readChannelMemory(options.channelDir), 8_000);\n\tconst currentHistory = clipTranscript(await readChannelHistory(options.channelDir), 8_000);\n\n\tconst prompt = `Channel memory file:\n${currentMemory || \"(empty)\"}\n\nChannel history file:\n${currentHistory || \"(empty)\"}\n\nConversation chunk to persist:\n${transcript || \"(empty)\"}`;\n\n\tconst rawResponse = await runWorkerPrompt(\n\t\toptions.model,\n\t\toptions.resolveApiKey,\n\t\tINLINE_CONSOLIDATION_SYSTEM_PROMPT,\n\t\tprompt,\n\t);\n\treturn parseConsolidationResponse(rawResponse);\n}\n\nexport async function runInlineConsolidation(options: ConsolidationRunOptions): Promise<InlineConsolidationResult> {\n\tconst sourceEntries = options.sessionEntries ?? [];\n\tconst relevantEntries =\n\t\tsourceEntries.length > 0 ? sourceEntries.slice(getLatestCompactionBoundary(sourceEntries)) : sourceEntries;\n\tconst relevantMessages = buildMessagesForConsolidation(\n\t\trelevantEntries.length > 0 ? extractMessagesFromSessionEntries(relevantEntries) : options.messages,\n\t);\n\n\tif (!hasMeaningfulMessages(relevantMessages)) {\n\t\treturn { skipped: true, appendedMemoryEntries: 0, appendedHistoryBlock: false };\n\t}\n\n\tconst response = await buildInlineConsolidationResponse(options, relevantMessages);\n\tconst timestamp = new Date().toISOString();\n\n\tif (response.memoryEntries.length > 0) {\n\t\tawait appendChannelMemoryUpdate(options.channelDir, {\n\t\t\ttimestamp,\n\t\t\tentries: response.memoryEntries,\n\t\t});\n\t}\n\n\tif (response.historyBlock.trim()) {\n\t\tawait appendChannelHistoryBlock(options.channelDir, {\n\t\t\ttimestamp,\n\t\t\tcontent: response.historyBlock,\n\t\t});\n\t}\n\n\treturn {\n\t\tskipped: false,\n\t\tappendedMemoryEntries: response.memoryEntries.length,\n\t\tappendedHistoryBlock: response.historyBlock.trim().length > 0,\n\t};\n}\n\nasync function cleanupChannelMemory(options: ConsolidationRunOptions, currentMemory: string): Promise<boolean> {\n\tif (\n\t\tcurrentMemory.length < MEMORY_CLEANUP_LENGTH_THRESHOLD &&\n\t\tcountMatchingSectionHeadings(currentMemory, \"Update \") < MEMORY_UPDATE_BLOCK_THRESHOLD\n\t) {\n\t\treturn false;\n\t}\n\n\tconst prompt = `Current MEMORY.md:\n${currentMemory}`;\n\tconst nextMemory = await runWorkerPrompt(options.model, options.resolveApiKey, MEMORY_CLEANUP_SYSTEM_PROMPT, prompt);\n\tawait rewriteChannelMemory(options.channelDir, nextMemory);\n\treturn true;\n}\n\nasync function foldChannelHistory(options: ConsolidationRunOptions, currentHistory: string): Promise<boolean> {\n\tconst sections = splitMarkdownSections(currentHistory);\n\tif (currentHistory.length < HISTORY_LENGTH_THRESHOLD && sections.length < HISTORY_BLOCK_THRESHOLD) {\n\t\treturn false;\n\t}\n\n\tif (sections.length <= HISTORY_RECENT_BLOCKS_TO_KEEP) {\n\t\treturn false;\n\t}\n\n\tconst olderSections = sections.slice(0, -HISTORY_RECENT_BLOCKS_TO_KEEP);\n\tconst recentSections = sections.slice(-HISTORY_RECENT_BLOCKS_TO_KEEP);\n\tconst prompt = `Older history blocks to fold:\n${olderSections.map((section) => `## ${section.heading}\\n\\n${section.content}`).join(\"\\n\\n\")}`;\n\tconst foldedSummary = await runWorkerPrompt(\n\t\toptions.model,\n\t\toptions.resolveApiKey,\n\t\tHISTORY_FOLDING_SYSTEM_PROMPT,\n\t\tprompt,\n\t);\n\n\tconst foldedHeading = `## Folded History Through ${olderSections[olderSections.length - 1]?.heading ?? new Date().toISOString()}`;\n\tconst rebuiltHistory = [\n\t\t\"# Channel History\",\n\t\t\"\",\n\t\tfoldedHeading,\n\t\t\"\",\n\t\tnormalizeText(foldedSummary),\n\t\t\"\",\n\t\t...recentSections.flatMap((section) => [`## ${section.heading}`, \"\", normalizeText(section.content), \"\"]),\n\t].join(\"\\n\");\n\n\tawait rewriteChannelHistory(options.channelDir, rebuiltHistory);\n\treturn true;\n}\n\nexport async function runBackgroundMaintenance(options: ConsolidationRunOptions): Promise<BackgroundMaintenanceResult> {\n\tconst currentMemory = await readChannelMemory(options.channelDir);\n\tconst currentHistory = await readChannelHistory(options.channelDir);\n\n\tconst cleanedMemory = await cleanupChannelMemory(options, currentMemory);\n\tconst foldedHistory = await foldChannelHistory(options, currentHistory);\n\n\treturn { cleanedMemory, foldedHistory };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"memory-consolidation.js","sourceRoot":"","sources":["../src/memory-consolidation.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,wBAAwB,EAGxB,qBAAqB,GACrB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EACN,yBAAyB,EACzB,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,qBAAqB,EACrB,oBAAoB,EACpB,qBAAqB,GACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,MAAM,2BAA2B,GAAG,MAAM,CAAC;AAC3C,MAAM,+BAA+B,GAAG,KAAK,CAAC;AAC9C,MAAM,6BAA6B,GAAG,CAAC,CAAC;AACxC,MAAM,wBAAwB,GAAG,MAAM,CAAC;AACxC,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAClC,MAAM,6BAA6B,GAAG,CAAC,CAAC;AACxC,MAAM,+BAA+B,GAAG,MAAM,CAAC;AAC/C,MAAM,yBAAyB,GAAG,MAAM,CAAC;AACzC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAE1C,MAAM,kCAAkC,GAAG;;;;;;;;;;;;;;;;;;0DAkBe,CAAC;AAE3D,MAAM,4BAA4B,GAAG;;;;;;;;;;;;;;;;;;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0BAA0B,CAC1B,CAAC;IAEF,MAAM,aAAa,GAAG,6BAA6B,aAAa,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,OAAO,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC;IAClI,MAAM,cAAc,GAAG;QACtB,mBAAmB;QACnB,EAAE;QACF,aAAa;QACb,EAAE;QACF,aAAa,CAAC,aAAa,CAAC;QAC5B,EAAE;QACF,GAAG,cAAc,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,MAAM,OAAO,CAAC,OAAO,EAAE,EAAE,EAAE,EAAE,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;KACzG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAEb,MAAM,qBAAqB,CAAC,OAAO,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IAChE,OAAO,IAAI,CAAC;AACb,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAAC,OAAgC;IAC9E,MAAM,aAAa,GAAG,MAAM,iBAAiB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAClE,MAAM,cAAc,GAAG,MAAM,kBAAkB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,MAAM,oBAAoB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;IACzE,MAAM,aAAa,GAAG,MAAM,kBAAkB,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IAExE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AACzC,CAAC","sourcesContent":["import type { AgentMessage } from \"@mariozechner/pi-agent-core\";\nimport type { Api, AssistantMessage, Message, Model } from \"@mariozechner/pi-ai\";\nimport {\n\tgetLatestCompactionEntry,\n\ttype SessionEntry,\n\ttype SessionMessageEntry,\n\tserializeConversation,\n} from \"@mariozechner/pi-coding-agent\";\nimport { parseJsonObject } from \"./llm-json.js\";\nimport {\n\tappendChannelHistoryBlock,\n\tappendChannelMemoryUpdate,\n\treadChannelHistory,\n\treadChannelMemory,\n\treadChannelSession,\n\trewriteChannelHistory,\n\trewriteChannelMemory,\n\tsplitMarkdownSections,\n} from \"./memory-files.js\";\nimport { runSidecarTask } from \"./sidecar-worker.js\";\n\nconst INLINE_TRANSCRIPT_MAX_CHARS = 28_000;\nconst MEMORY_CLEANUP_LENGTH_THRESHOLD = 8_000;\nconst MEMORY_UPDATE_BLOCK_THRESHOLD = 6;\nconst HISTORY_LENGTH_THRESHOLD = 16_000;\nconst HISTORY_BLOCK_THRESHOLD = 8;\nconst HISTORY_RECENT_BLOCKS_TO_KEEP = 4;\nconst INLINE_CONSOLIDATION_TIMEOUT_MS = 20_000;\nconst MEMORY_CLEANUP_TIMEOUT_MS = 30_000;\nconst HISTORY_FOLDING_TIMEOUT_MS = 30_000;\n\nconst INLINE_CONSOLIDATION_SYSTEM_PROMPT = `You are a runtime memory consolidation worker for Pipiclaw.\n\nReturn strict JSON only. Do not wrap in Markdown fences.\n\nOutput schema:\n{\n \"memoryEntries\": [\"string\"],\n \"historyBlock\": \"string\"\n}\n\nRules:\n- memoryEntries: concise durable facts, decisions, preferences, constraints, current work state, or open loops that should survive compaction.\n- Each memoryEntries item must be a standalone sentence fragment suitable for a Markdown bullet without the bullet prefix.\n- Do not include raw transcript quotes unless essential.\n- Do not include ephemeral chatter, obvious one-shot acknowledgements, or formatting instructions.\n- Prefer leaving highly volatile step-by-step execution state in SESSION.md rather than promoting it into durable memory.\n- historyBlock: concise Markdown summarizing the conversation chunk for later recovery.\n- Prefer short bullets and short paragraphs.\n- If there is nothing worth storing, return empty values.`;\n\nconst MEMORY_CLEANUP_SYSTEM_PROMPT = `You are rewriting a Pipiclaw channel MEMORY.md file.\n\nReturn Markdown only. Do not use code fences.\n\nGoals:\n- Keep only durable and useful channel memory.\n- Remove outdated entries, duplicates, and verbose phrasing.\n- Organize the result with stable sections where relevant.\n- Prefer concise bullets over prose.\n- Remove content that is clearly transient session-state and belongs in SESSION.md instead.\n\nSuggested sections:\n- ## Identity / Participants\n- ## Preferences\n- ## Ongoing Work\n- ## Constraints\n- ## Decisions\n- ## Open Loops\n\nOmit empty sections.`;\n\nconst HISTORY_FOLDING_SYSTEM_PROMPT = `You are folding older HISTORY.md blocks for Pipiclaw.\n\nReturn Markdown only. Do not use code fences.\n\nGoals:\n- Compress older history blocks into one concise summary block.\n- Keep important decisions, milestones, and unresolved outcomes.\n- Remove redundancy and transcript-like detail.\n- Preserve a chronological narrative at a high level.`;\n\nexport interface ConsolidationRunOptions {\n\tchannelDir: string;\n\tmodel: Model<Api>;\n\tresolveApiKey: (model: Model<Api>) => Promise<string>;\n\tmessages: AgentMessage[];\n\tsessionEntries?: SessionEntry[];\n}\n\nexport interface InlineConsolidationResult {\n\tskipped: boolean;\n\tappendedMemoryEntries: number;\n\tappendedHistoryBlock: boolean;\n}\n\nexport interface BackgroundMaintenanceResult {\n\tcleanedMemory: boolean;\n\tfoldedHistory: boolean;\n}\n\ninterface ConsolidationResponse {\n\tmemoryEntries: string[];\n\thistoryBlock: string;\n}\n\nfunction normalizeText(text: string): string {\n\treturn text.replace(/\\r/g, \"\").trim();\n}\n\nfunction clipTranscript(text: string, maxChars: number): string {\n\tconst normalized = normalizeText(text);\n\tif (normalized.length <= maxChars) {\n\t\treturn normalized;\n\t}\n\n\tconst headChars = Math.floor(maxChars * 0.35);\n\tconst tailChars = maxChars - headChars;\n\treturn `${normalized.slice(0, headChars)}\\n\\n[... omitted middle section ...]\\n\\n${normalized.slice(-tailChars)}`;\n}\n\nfunction parseConsolidationResponse(text: string): ConsolidationResponse {\n\tconst parsed = parseJsonObject(text) as Partial<ConsolidationResponse>;\n\treturn {\n\t\tmemoryEntries: Array.isArray(parsed.memoryEntries)\n\t\t\t? parsed.memoryEntries\n\t\t\t\t\t.map((entry) => (typeof entry === \"string\" ? entry.trim() : \"\"))\n\t\t\t\t\t.filter((entry) => entry.length > 0)\n\t\t\t: [],\n\t\thistoryBlock: typeof parsed.historyBlock === \"string\" ? parsed.historyBlock.trim() : \"\",\n\t};\n}\n\nfunction getLatestCompactionBoundary(entries: SessionEntry[]): number {\n\tconst latestCompaction = getLatestCompactionEntry(entries);\n\tif (!latestCompaction) {\n\t\treturn 0;\n\t}\n\n\tconst boundaryIndex = entries.findIndex((entry) => entry.id === latestCompaction.firstKeptEntryId);\n\treturn boundaryIndex >= 0 ? boundaryIndex : 0;\n}\n\nfunction isMessage(entry: SessionEntry): entry is SessionMessageEntry {\n\treturn entry.type === \"message\";\n}\n\nfunction isStandardAgentMessage(message: AgentMessage): message is Message {\n\treturn (\n\t\ttypeof message === \"object\" &&\n\t\tmessage !== null &&\n\t\t\"role\" in message &&\n\t\t(message.role === \"user\" || message.role === \"assistant\" || message.role === \"toolResult\")\n\t);\n}\n\nfunction buildMessagesForConsolidation(messages: AgentMessage[]): Message[] {\n\treturn messages.filter(isStandardAgentMessage);\n}\n\nfunction extractMessagesFromSessionEntries(entries: SessionEntry[]): AgentMessage[] {\n\treturn entries.filter(isMessage).map((entry) => entry.message);\n}\n\nfunction hasMeaningfulMessages(messages: Message[]): boolean {\n\tlet meaningfulCount = 0;\n\tfor (const message of messages) {\n\t\tif (message.role === \"user\") {\n\t\t\tconst text =\n\t\t\t\ttypeof message.content === \"string\"\n\t\t\t\t\t? message.content\n\t\t\t\t\t: message.content.map((part) => (part.type === \"text\" ? part.text : \"[image]\")).join(\"\\n\");\n\t\t\tif (text.trim()) meaningfulCount++;\n\t\t} else if (message.role === \"assistant\") {\n\t\t\tconst text = message.content\n\t\t\t\t.filter(\n\t\t\t\t\t(part): part is Extract<AssistantMessage[\"content\"][number], { type: \"text\" }> => part.type === \"text\",\n\t\t\t\t)\n\t\t\t\t.map((part) => part.text)\n\t\t\t\t.join(\"\\n\");\n\t\t\tif (text.trim()) meaningfulCount++;\n\t\t}\n\t\tif (meaningfulCount >= 2) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nfunction countMatchingSectionHeadings(content: string, prefix: string): number {\n\treturn splitMarkdownSections(content).filter((section) => section.heading.startsWith(prefix)).length;\n}\n\nasync function runWorkerPrompt(\n\tname: string,\n\tmodel: Model<Api>,\n\tresolveApiKey: (model: Model<Api>) => Promise<string>,\n\tsystemPrompt: string,\n\tprompt: string,\n\ttimeoutMs: number,\n): Promise<string> {\n\tconst result = await runSidecarTask({\n\t\tname,\n\t\tmodel,\n\t\tresolveApiKey,\n\t\tsystemPrompt,\n\t\tprompt,\n\t\ttimeoutMs,\n\t\tparse: (text) => text.trim(),\n\t});\n\treturn result.output;\n}\n\nasync function buildInlineConsolidationResponse(\n\toptions: ConsolidationRunOptions,\n\tmessages: Message[],\n): Promise<ConsolidationResponse> {\n\tconst transcript = clipTranscript(serializeConversation(messages), INLINE_TRANSCRIPT_MAX_CHARS);\n\tconst currentMemory = clipTranscript(await readChannelMemory(options.channelDir), 8_000);\n\tconst currentSession = clipTranscript(await readChannelSession(options.channelDir), 8_000);\n\tconst currentHistory = clipTranscript(await readChannelHistory(options.channelDir), 8_000);\n\n\tconst prompt = `Current SESSION.md:\n${currentSession || \"(empty)\"}\n\nChannel memory file:\n${currentMemory || \"(empty)\"}\n\nChannel history file:\n${currentHistory || \"(empty)\"}\n\nConversation chunk to persist:\n${transcript || \"(empty)\"}`;\n\n\tconst rawResponse = await runWorkerPrompt(\n\t\t\"memory-inline-consolidation\",\n\t\toptions.model,\n\t\toptions.resolveApiKey,\n\t\tINLINE_CONSOLIDATION_SYSTEM_PROMPT,\n\t\tprompt,\n\t\tINLINE_CONSOLIDATION_TIMEOUT_MS,\n\t);\n\treturn parseConsolidationResponse(rawResponse);\n}\n\nexport async function runInlineConsolidation(options: ConsolidationRunOptions): Promise<InlineConsolidationResult> {\n\tconst sourceEntries = options.sessionEntries ?? [];\n\tconst relevantEntries =\n\t\tsourceEntries.length > 0 ? sourceEntries.slice(getLatestCompactionBoundary(sourceEntries)) : sourceEntries;\n\tconst relevantMessages = buildMessagesForConsolidation(\n\t\trelevantEntries.length > 0 ? extractMessagesFromSessionEntries(relevantEntries) : options.messages,\n\t);\n\n\tif (!hasMeaningfulMessages(relevantMessages)) {\n\t\treturn { skipped: true, appendedMemoryEntries: 0, appendedHistoryBlock: false };\n\t}\n\n\tconst response = await buildInlineConsolidationResponse(options, relevantMessages);\n\tconst timestamp = new Date().toISOString();\n\n\tif (response.memoryEntries.length > 0) {\n\t\tawait appendChannelMemoryUpdate(options.channelDir, {\n\t\t\ttimestamp,\n\t\t\tentries: response.memoryEntries,\n\t\t});\n\t}\n\n\tif (response.historyBlock.trim()) {\n\t\tawait appendChannelHistoryBlock(options.channelDir, {\n\t\t\ttimestamp,\n\t\t\tcontent: response.historyBlock,\n\t\t});\n\t}\n\n\treturn {\n\t\tskipped: false,\n\t\tappendedMemoryEntries: response.memoryEntries.length,\n\t\tappendedHistoryBlock: response.historyBlock.trim().length > 0,\n\t};\n}\n\nasync function cleanupChannelMemory(options: ConsolidationRunOptions, currentMemory: string): Promise<boolean> {\n\tif (\n\t\tcurrentMemory.length < MEMORY_CLEANUP_LENGTH_THRESHOLD &&\n\t\tcountMatchingSectionHeadings(currentMemory, \"Update \") < MEMORY_UPDATE_BLOCK_THRESHOLD\n\t) {\n\t\treturn false;\n\t}\n\n\tconst prompt = `Current MEMORY.md:\n${currentMemory}`;\n\tconst nextMemory = await runWorkerPrompt(\n\t\t\"memory-cleanup\",\n\t\toptions.model,\n\t\toptions.resolveApiKey,\n\t\tMEMORY_CLEANUP_SYSTEM_PROMPT,\n\t\tprompt,\n\t\tMEMORY_CLEANUP_TIMEOUT_MS,\n\t);\n\tawait rewriteChannelMemory(options.channelDir, nextMemory);\n\treturn true;\n}\n\nasync function foldChannelHistory(options: ConsolidationRunOptions, currentHistory: string): Promise<boolean> {\n\tconst sections = splitMarkdownSections(currentHistory);\n\tif (currentHistory.length < HISTORY_LENGTH_THRESHOLD && sections.length < HISTORY_BLOCK_THRESHOLD) {\n\t\treturn false;\n\t}\n\n\tif (sections.length <= HISTORY_RECENT_BLOCKS_TO_KEEP) {\n\t\treturn false;\n\t}\n\n\tconst olderSections = sections.slice(0, -HISTORY_RECENT_BLOCKS_TO_KEEP);\n\tconst recentSections = sections.slice(-HISTORY_RECENT_BLOCKS_TO_KEEP);\n\tconst prompt = `Older history blocks to fold:\n${olderSections.map((section) => `## ${section.heading}\\n\\n${section.content}`).join(\"\\n\\n\")}`;\n\tconst foldedSummary = await runWorkerPrompt(\n\t\t\"history-folding\",\n\t\toptions.model,\n\t\toptions.resolveApiKey,\n\t\tHISTORY_FOLDING_SYSTEM_PROMPT,\n\t\tprompt,\n\t\tHISTORY_FOLDING_TIMEOUT_MS,\n\t);\n\n\tconst foldedHeading = `## Folded History Through ${olderSections[olderSections.length - 1]?.heading ?? new Date().toISOString()}`;\n\tconst rebuiltHistory = [\n\t\t\"# Channel History\",\n\t\t\"\",\n\t\tfoldedHeading,\n\t\t\"\",\n\t\tnormalizeText(foldedSummary),\n\t\t\"\",\n\t\t...recentSections.flatMap((section) => [`## ${section.heading}`, \"\", normalizeText(section.content), \"\"]),\n\t].join(\"\\n\");\n\n\tawait rewriteChannelHistory(options.channelDir, rebuiltHistory);\n\treturn true;\n}\n\nexport async function runBackgroundMaintenance(options: ConsolidationRunOptions): Promise<BackgroundMaintenanceResult> {\n\tconst currentMemory = await readChannelMemory(options.channelDir);\n\tconst currentHistory = await readChannelHistory(options.channelDir);\n\n\tconst cleanedMemory = await cleanupChannelMemory(options, currentMemory);\n\tconst foldedHistory = await foldChannelHistory(options, currentHistory);\n\n\treturn { cleanedMemory, foldedHistory };\n}\n"]}
|
package/dist/memory-files.d.ts
CHANGED
|
@@ -8,12 +8,15 @@ export interface HistoryBlock {
|
|
|
8
8
|
}
|
|
9
9
|
export declare function getChannelMemoryPath(channelDir: string): string;
|
|
10
10
|
export declare function getChannelHistoryPath(channelDir: string): string;
|
|
11
|
+
export declare function getChannelSessionPath(channelDir: string): string;
|
|
11
12
|
export declare function ensureChannelMemoryFiles(channelDir: string): Promise<void>;
|
|
12
13
|
export declare function ensureChannelMemoryFilesSync(channelDir: string): void;
|
|
13
14
|
export declare function readChannelMemory(channelDir: string): Promise<string>;
|
|
14
15
|
export declare function readChannelHistory(channelDir: string): Promise<string>;
|
|
16
|
+
export declare function readChannelSession(channelDir: string): Promise<string>;
|
|
15
17
|
export declare function rewriteChannelMemory(channelDir: string, content: string): Promise<void>;
|
|
16
18
|
export declare function rewriteChannelHistory(channelDir: string, content: string): Promise<void>;
|
|
19
|
+
export declare function rewriteChannelSession(channelDir: string, content: string): Promise<void>;
|
|
17
20
|
export declare function appendChannelMemoryUpdate(channelDir: string, block: MemoryUpdateBlock): Promise<void>;
|
|
18
21
|
export declare function appendChannelHistoryBlock(channelDir: string, block: HistoryBlock): Promise<void>;
|
|
19
22
|
export interface MarkdownSection {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-files.d.ts","sourceRoot":"","sources":["../src/memory-files.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"memory-files.d.ts","sourceRoot":"","sources":["../src/memory-files.ts"],"names":[],"mappings":"AA+DA,MAAM,WAAW,iBAAiB;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,MAAM,WAAW,YAAY;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CAChB;AAiBD,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED,wBAAsB,wBAAwB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEhF;AAED,wBAAgB,4BAA4B,CAAC,UAAU,EA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