-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathmemory.ts
More file actions
179 lines (156 loc) · 4.96 KB
/
memory.ts
File metadata and controls
179 lines (156 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { readFile, writeFile, mkdir } from "fs/promises";
import { join, dirname } from "path";
import { execFileSync } from "child_process";
import type { AgentTool } from "@mariozechner/pi-agent-core";
import { memorySchema, DEFAULT_MEMORY_PATH } from "./shared.js";
import yaml from "js-yaml";
import type { MemoryLayerDef } from "../plugin-types.js";
interface MemoryLayer {
name: string;
path: string;
max_lines?: number;
format: "markdown" | "yaml";
}
interface MemoryConfig {
layers: MemoryLayer[];
archive_policy?: { max_entries?: number; compress_after?: string };
}
async function loadMemoryConfig(cwd: string, pluginLayers?: MemoryLayerDef[]): Promise<MemoryConfig | null> {
let config: MemoryConfig | null = null;
try {
const raw = await readFile(join(cwd, "memory", "memory.yaml"), "utf-8");
const parsed = yaml.load(raw) as MemoryConfig;
if (parsed?.layers && Array.isArray(parsed.layers)) {
config = parsed;
}
} catch {
// No config file
}
// Merge plugin memory layers
if (pluginLayers && pluginLayers.length > 0) {
if (!config) config = { layers: [] };
for (const layer of pluginLayers) {
config.layers.push({
name: layer.name,
path: layer.path,
format: "markdown",
});
}
}
return config;
}
function getWorkingLayer(config: MemoryConfig | null): { path: string; maxLines?: number } {
if (!config) {
return { path: DEFAULT_MEMORY_PATH };
}
const working = config.layers.find((l) => l.name === "working") || config.layers[0];
if (!working) {
return { path: DEFAULT_MEMORY_PATH };
}
return { path: working.path, maxLines: working.max_lines };
}
async function archiveOverflow(
cwd: string,
content: string,
maxLines: number,
): Promise<string> {
const lines = content.split("\n");
if (lines.length <= maxLines) return content;
// Keep the last maxLines, archive the rest
const overflow = lines.slice(0, lines.length - maxLines).join("\n");
const kept = lines.slice(lines.length - maxLines).join("\n");
const now = new Date();
const archiveFile = `memory/archive/${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}.md`;
const archivePath = join(cwd, archiveFile);
await mkdir(dirname(archivePath), { recursive: true });
// Append to archive
let existing = "";
try {
existing = await readFile(archivePath, "utf-8");
} catch {
// New archive file
}
const archiveEntry = `\n---\n_Archived: ${now.toISOString()}_\n\n${overflow}\n`;
await writeFile(archivePath, existing + archiveEntry, "utf-8");
// Try to git add the archive
try {
execFileSync("git", ["add", archiveFile], { cwd, stdio: "pipe" });
} catch {
// Not in git, that's fine
}
return kept;
}
export function createMemoryTool(cwd: string, pluginLayers?: MemoryLayerDef[]): AgentTool<typeof memorySchema> {
return {
name: "memory",
label: "memory",
description:
"Git-backed memory. Use 'load' to read current memory, 'save' to update memory and commit to git. Each save creates a git commit, giving you full history of what you've remembered.",
parameters: memorySchema,
execute: async (
_toolCallId: string,
{ action, content, message }: { action: string; content?: string; message?: string },
signal?: AbortSignal,
) => {
if (signal?.aborted) throw new Error("Operation aborted");
const config = await loadMemoryConfig(cwd, pluginLayers);
const { path: memoryPath, maxLines } = getWorkingLayer(config);
const memoryFile = join(cwd, memoryPath);
if (action === "load") {
try {
const text = await readFile(memoryFile, "utf-8");
const trimmed = text.trim();
if (!trimmed || trimmed === "# Memory") {
return {
content: [{ type: "text", text: "No memories yet." }],
details: undefined,
};
}
return {
content: [{ type: "text", text: trimmed }],
details: undefined,
};
} catch {
return {
content: [{ type: "text", text: "No memories yet." }],
details: undefined,
};
}
}
// action === "save"
if (!content) {
throw new Error("content is required for save action");
}
const commitMsg = message || "Update memory";
// Apply max_lines archiving if configured
let finalContent = content;
if (maxLines) {
finalContent = await archiveOverflow(cwd, content, maxLines);
}
await mkdir(dirname(memoryFile), { recursive: true });
await writeFile(memoryFile, finalContent, "utf-8");
try {
execFileSync("git", ["add", memoryPath], { cwd, stdio: "pipe" });
execFileSync("git", ["commit", "-m", commitMsg], {
cwd,
stdio: "pipe",
});
} catch (err: any) {
const stderr = err.stderr?.toString() || "";
return {
content: [
{
type: "text",
text: `Memory saved to ${memoryPath} but git commit failed: ${stderr.trim() || "unknown error"}. The file was still written.`,
},
],
details: undefined,
};
}
return {
content: [{ type: "text", text: `Memory saved and committed: "${commitMsg}"` }],
details: undefined,
};
},
};
}