-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathindex.ts
More file actions
265 lines (244 loc) · 8.57 KB
/
index.ts
File metadata and controls
265 lines (244 loc) · 8.57 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { Type } from "typebox";
import path from "node:path";
import crypto from "node:crypto";
type TextBlock = { type?: string; text?: string };
type AssistantMessage = { role?: string; content?: unknown };
type SmartSearchResult = {
title?: string;
narrative?: string;
type?: string;
combinedScore?: number;
score?: number;
observation?: {
title?: string;
narrative?: string;
type?: string;
};
};
type HealthResponse = {
status?: string;
service?: string;
version?: string;
health?: {
status?: string;
notes?: string[];
};
};
const DEFAULT_URL = process.env.AGENTMEMORY_URL || "http://localhost:3111";
const TOOL_GUIDANCE = [
"agentmemory is available for cross-session memory.",
"Use memory_search to recall prior decisions, preferences, bugs, and workflows.",
"Use memory_save when you discover durable facts worth remembering beyond this session.",
].join(" ");
function normalizeBaseUrl(url: string): string {
return url.replace(/\/+$/, "");
}
function getText(content: unknown): string {
if (typeof content === "string") return content;
if (!Array.isArray(content)) return "";
return content
.flatMap((part) => {
if (!part || typeof part !== "object") return [] as string[];
const block = part as TextBlock;
if (block.type === "text" && typeof block.text === "string") return [block.text];
return [] as string[];
})
.join("\n")
.trim();
}
function getLastAssistantText(messages: unknown[]): string {
for (const msg of [...messages].reverse()) {
if (!msg || typeof msg !== "object") continue;
const assistant = msg as AssistantMessage;
if (assistant.role !== "assistant") continue;
const text = getText(assistant.content);
if (text) return text;
}
return "";
}
function formatSearchResults(results: SmartSearchResult[]): string {
if (!results.length) return "No relevant memories found.";
return results
.slice(0, 5)
.map((result, index) => {
const obs = result.observation ?? result;
const title = obs.title?.trim() || `Memory ${index + 1}`;
const narrative = obs.narrative?.trim() || "";
const type = obs.type?.trim() || "memory";
const score = result.combinedScore ?? result.score;
const scoreText = typeof score === "number" ? ` [score=${score.toFixed(3)}]` : "";
return `- ${title} (${type})${scoreText}${narrative ? `: ${narrative}` : ""}`;
})
.join("\n");
}
async function callAgentMemory<T>(
pathname: string,
options?: {
method?: "GET" | "POST";
body?: unknown;
baseUrl?: string;
},
): Promise<T | null> {
const baseUrl = normalizeBaseUrl(options?.baseUrl || process.env.AGENTMEMORY_URL || DEFAULT_URL);
const method = options?.method || "POST";
const url = `${baseUrl}/agentmemory/${pathname.replace(/^\/+/, "")}`;
const headers: Record<string, string> = {};
if (options?.body !== undefined) headers["Content-Type"] = "application/json";
if (process.env.AGENTMEMORY_SECRET) headers.Authorization = `Bearer ${process.env.AGENTMEMORY_SECRET}`;
try {
const response = await fetch(url, {
method,
headers,
body: options?.body !== undefined ? JSON.stringify(options.body) : undefined,
});
if (!response.ok) return null;
return (await response.json()) as T;
} catch {
return null;
}
}
export default function agentmemoryExtension(pi: ExtensionAPI) {
let sessionId = `ephemeral-${crypto.randomUUID().slice(0, 8)}`;
let currentProject = process.cwd();
let lastPrompt = "";
let lastHealthOk = false;
async function getHealth() {
return await callAgentMemory<HealthResponse>("health", { method: "GET" });
}
async function refreshStatus(ctx: { ui: { setStatus: (key: string, text: string) => void } }) {
const health = await getHealth();
lastHealthOk = !!health && (health.status === "healthy" || health.health?.status === "healthy");
ctx.ui.setStatus("agentmemory", lastHealthOk ? "🧠 agentmemory" : "🧠 agentmemory off");
}
pi.registerCommand("agentmemory-status", {
description: "Check local agentmemory server health",
handler: async (_args, ctx) => {
const health = await getHealth();
if (!health) {
ctx.ui.notify("agentmemory is unreachable at http://localhost:3111", "warning");
return;
}
ctx.ui.notify(
`agentmemory ${health.status || health.health?.status || "unknown"}${health.version ? ` v${health.version}` : ""}`,
"info",
);
},
});
pi.registerTool({
name: "memory_health",
label: "Memory Health",
description: "Check whether the local agentmemory server is reachable and healthy",
parameters: Type.Object({}),
async execute() {
const health = await getHealth();
if (!health) {
return {
content: [{ type: "text", text: "agentmemory is unreachable at http://localhost:3111" }],
details: { ok: false },
};
}
return {
content: [
{
type: "text",
text: `agentmemory status: ${health.status || health.health?.status || "unknown"}${health.version ? ` (v${health.version})` : ""}`,
},
],
details: health,
};
},
});
pi.registerTool({
name: "memory_search",
label: "Memory Search",
description: "Search agentmemory for cross-session project memory, prior decisions, bugs, and user preferences",
parameters: Type.Object({
query: Type.String({ description: "What to search for in memory" }),
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 10, default: 5, description: "Maximum results" })),
}),
async execute(_toolCallId, params) {
const result = await callAgentMemory<{ results?: SmartSearchResult[] }>("smart-search", {
body: { query: params.query, limit: params.limit ?? 5 },
});
const results = result?.results || [];
return {
content: [{ type: "text", text: formatSearchResults(results) }],
details: { query: params.query, results },
};
},
});
pi.registerTool({
name: "memory_save",
label: "Memory Save",
description: "Save a durable fact, convention, workflow, preference, or bug fix into agentmemory",
parameters: Type.Object({
content: Type.String({ description: "What should be remembered" }),
type: Type.Optional(
Type.String({
description: "Memory type",
default: "fact",
}),
),
}),
async execute(_toolCallId, params) {
const result = await callAgentMemory<Record<string, unknown>>("remember", {
body: { content: params.content, type: params.type || "fact" },
});
if (!result) {
return {
content: [{ type: "text", text: "Failed to save memory to agentmemory." }],
details: { ok: false },
};
}
return {
content: [{ type: "text", text: `Saved memory (${params.type || "fact"}): ${params.content}` }],
details: result,
};
},
});
pi.on("session_start", async (_event, ctx) => {
const sessionFile = ctx.sessionManager.getSessionFile();
sessionId = sessionFile ? path.basename(sessionFile).replace(/\.[^.]+$/, "") : `ephemeral-${crypto.randomUUID().slice(0, 8)}`;
currentProject = process.cwd();
await refreshStatus(ctx);
});
pi.on("before_agent_start", async (event, ctx) => {
currentProject = event.systemPromptOptions.cwd || process.cwd();
lastPrompt = event.prompt?.trim() || "";
if (!lastPrompt) return;
const result = await callAgentMemory<{ results?: SmartSearchResult[] }>("smart-search", {
body: { query: lastPrompt, limit: 5 },
});
const results = result?.results || [];
const recallBlock = results.length
? [
"Relevant long-term memory from agentmemory:",
formatSearchResults(results),
].join("\n")
: "";
await refreshStatus(ctx);
return {
systemPrompt: [event.systemPrompt, TOOL_GUIDANCE, recallBlock].filter(Boolean).join("\n\n"),
};
});
pi.on("agent_end", async (event) => {
if (!lastHealthOk || !lastPrompt) return;
const assistantText = getLastAssistantText(event.messages as unknown[]);
if (!assistantText) return;
void callAgentMemory("observe", {
body: {
hookType: "post_tool_use",
sessionId,
project: currentProject,
cwd: currentProject,
timestamp: new Date().toISOString(),
data: {
tool_name: "conversation",
input: lastPrompt.slice(0, 500),
output: assistantText.slice(0, 4000),
},
},
});
});
}