@yandy0725/pi-memory 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +54 -48
- package/package.json +1 -2
- package/src/agent-config.ts +2 -0
- package/src/agent-runner.ts +121 -0
- package/src/config.ts +4 -6
- package/src/dream.ts +16 -74
- package/src/extract.ts +20 -37
- package/src/inject.ts +46 -120
- package/src/memory-tool.ts +4 -1
- package/src/model-resolver.ts +69 -0
- package/src/agent-types.ts +0 -44
package/index.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { readdir, readFile } from "node:fs/promises";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import { SessionManager } from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import { ensureAgentTypes } from "./src/agent-types";
|
|
6
5
|
import { loadConfig, type MemoryConfig } from "./src/config";
|
|
7
6
|
import { runDream } from "./src/dream";
|
|
8
7
|
import { runExtract } from "./src/extract";
|
|
@@ -19,9 +18,6 @@ import { readDreamMeta, shouldNudge, writeDreamMeta } from "./src/nudge";
|
|
|
19
18
|
import { resolveMemoryDir } from "./src/paths";
|
|
20
19
|
import { searchSessions } from "./src/session-search";
|
|
21
20
|
|
|
22
|
-
// Register custom agent type on load (zero setup, auto-fallback on first session).
|
|
23
|
-
ensureAgentTypes();
|
|
24
|
-
|
|
25
21
|
export default function (pi: ExtensionAPI) {
|
|
26
22
|
let memoryDir: string | null = null;
|
|
27
23
|
let config: MemoryConfig | null = null;
|
|
@@ -57,30 +53,30 @@ export default function (pi: ExtensionAPI) {
|
|
|
57
53
|
if (nudge) {
|
|
58
54
|
const ok = await ctx.ui.confirm("Memory Consolidation", `${message}\n\nConsolidate memory files now?`);
|
|
59
55
|
if (ok) {
|
|
60
|
-
// Fire-and-forget:
|
|
61
|
-
//
|
|
62
|
-
// Does not block session_start.
|
|
56
|
+
// Fire-and-forget: does not block session_start. The headless
|
|
57
|
+
// dream agent runs independently; completion notifies the user.
|
|
63
58
|
const dreamModel = config.dream.model;
|
|
64
59
|
const dreamThinkLevel = config.dream.thinkLevel;
|
|
65
60
|
const dir = memoryDir;
|
|
66
61
|
ctx.ui.setStatus("dream", "Consolidating memory...");
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
62
|
+
runDream({
|
|
63
|
+
model: dreamModel,
|
|
64
|
+
thinkLevel: dreamThinkLevel,
|
|
65
|
+
memoryDir: dir,
|
|
66
|
+
modelRegistry: ctx.modelRegistry,
|
|
67
|
+
parentModel: ctx.model,
|
|
68
|
+
})
|
|
69
|
+
.then(async (summary) => {
|
|
75
70
|
await writeDreamMeta(dir, sessions);
|
|
76
71
|
ctx.ui.notify(summary, "info");
|
|
77
|
-
|
|
78
|
-
|
|
72
|
+
})
|
|
73
|
+
// biome-ignore lint/suspicious/noExplicitAny: error catch
|
|
74
|
+
.catch((e: any) => {
|
|
79
75
|
ctx.ui.notify(`Dream failed: ${e.message}`, "error");
|
|
80
|
-
}
|
|
76
|
+
})
|
|
77
|
+
.finally(() => {
|
|
81
78
|
ctx.ui.setStatus("dream", undefined);
|
|
82
|
-
}
|
|
83
|
-
}, 0);
|
|
79
|
+
});
|
|
84
80
|
}
|
|
85
81
|
}
|
|
86
82
|
}
|
|
@@ -89,14 +85,12 @@ export default function (pi: ExtensionAPI) {
|
|
|
89
85
|
pi.on("before_agent_start", async (event, ctx) => {
|
|
90
86
|
if (!config?.enabled || !indexSnapshot || !memoryDir) return;
|
|
91
87
|
|
|
92
|
-
// Auto-surfacing: select relevant topic files via LLM side-query and inject as message.
|
|
93
|
-
// Skip for subagents: pi-subagents strips "subagent" from all children's
|
|
94
|
-
// tool sets, so its absence reliably identifies subagents. Without this guard,
|
|
95
|
-
// runSideQuery's subagent spawn → before_agent_start → re-enter here → OOM.
|
|
96
|
-
const agentTools = event.systemPromptOptions?.selectedTools;
|
|
97
|
-
const isSubagent = agentTools && !agentTools.includes("subagent");
|
|
98
|
-
|
|
99
88
|
const autoSurfacing = config.autoSurfacing;
|
|
89
|
+
// Skip auto-surfacing in subagent sessions: pi-subagents injects an
|
|
90
|
+
// <active_agent name="..."/> tag into every subagent's system prompt.
|
|
91
|
+
// Main sessions (including forks) never have this tag. Our own headless
|
|
92
|
+
// sessions use noExtensions (never bind), so they never reach here.
|
|
93
|
+
const isSubagent = event.systemPrompt?.includes("<active_agent name=\"");
|
|
100
94
|
// biome-ignore lint/suspicious/noExplicitAny: message injection result
|
|
101
95
|
let injectedMessage: any;
|
|
102
96
|
if (autoSurfacing?.enabled && event.prompt && !isSubagent) {
|
|
@@ -105,7 +99,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
105
99
|
const manifest = await scanTopics(memoryDir);
|
|
106
100
|
if (manifest.length > 0) {
|
|
107
101
|
const queryPrompt = buildSurfacingPrompt(manifest, event.prompt.slice(0, 4000), injectedTopics);
|
|
108
|
-
const selected = await runSideQuery(
|
|
102
|
+
const selected = await runSideQuery(
|
|
103
|
+
queryPrompt,
|
|
104
|
+
manifest,
|
|
105
|
+
autoSurfacing.maxFiles,
|
|
106
|
+
autoSurfacing.thinkLevel,
|
|
107
|
+
autoSurfacing.model,
|
|
108
|
+
ctx.modelRegistry,
|
|
109
|
+
ctx.model,
|
|
110
|
+
memoryDir,
|
|
111
|
+
injectedTopics,
|
|
112
|
+
);
|
|
109
113
|
if (selected.length > 0) {
|
|
110
114
|
const content = await injectSurfacedContent(
|
|
111
115
|
memoryDir,
|
|
@@ -114,9 +118,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
114
118
|
autoSurfacing.maxInjectionBytes,
|
|
115
119
|
);
|
|
116
120
|
if (content) {
|
|
117
|
-
// Track injected topics for session-level dedup
|
|
118
121
|
for (const f of selected) injectedTopics.add(f);
|
|
119
|
-
// Inject as a custom message (NOT systemPrompt)
|
|
120
122
|
injectedMessage = { customType: "memory-auto-surfacing", content, display: false };
|
|
121
123
|
}
|
|
122
124
|
}
|
|
@@ -135,16 +137,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
135
137
|
};
|
|
136
138
|
});
|
|
137
139
|
|
|
138
|
-
pi.on("agent_end", async (event) => {
|
|
140
|
+
pi.on("agent_end", async (event, ctx) => {
|
|
139
141
|
if (!config?.enabled || !memoryDir) return;
|
|
140
142
|
const extractConfig = config.extractMemories;
|
|
141
143
|
if (!extractConfig?.enabled) return;
|
|
142
144
|
if (!event.messages || event.messages.length === 0) return;
|
|
143
|
-
// Fire-and-forget: extract memories in background
|
|
144
145
|
runExtract({
|
|
145
146
|
model: extractConfig.model,
|
|
146
147
|
thinkLevel: extractConfig.thinkLevel,
|
|
147
148
|
memoryDir,
|
|
149
|
+
modelRegistry: ctx.modelRegistry,
|
|
150
|
+
parentModel: ctx.model,
|
|
148
151
|
messages: event.messages.map((m) => ({
|
|
149
152
|
// biome-ignore lint/suspicious/noExplicitAny: pi event message union type
|
|
150
153
|
role: String((m as any).role ?? ""),
|
|
@@ -202,24 +205,27 @@ export default function (pi: ExtensionAPI) {
|
|
|
202
205
|
}
|
|
203
206
|
const ok = await ctx.ui.confirm("Dream", "Consolidate all memory files? This rewrites them in-place.");
|
|
204
207
|
if (!ok) return;
|
|
208
|
+
const dir = memoryDir;
|
|
205
209
|
ctx.ui.setStatus("dream", "Consolidating memory...");
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
210
|
+
runDream({
|
|
211
|
+
model: config.dream.model,
|
|
212
|
+
thinkLevel: config.dream.thinkLevel,
|
|
213
|
+
memoryDir,
|
|
214
|
+
modelRegistry: ctx.modelRegistry,
|
|
215
|
+
parentModel: ctx.model,
|
|
216
|
+
})
|
|
217
|
+
.then(async (summary) => {
|
|
218
|
+
const sessions = (await SessionManager.list(ctx.cwd)).length;
|
|
219
|
+
await writeDreamMeta(dir, sessions);
|
|
220
|
+
ctx.ui.notify(summary, "info");
|
|
221
|
+
})
|
|
217
222
|
// biome-ignore lint/suspicious/noExplicitAny: command handler ctx
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
+
.catch((e: any) => {
|
|
224
|
+
ctx.ui.notify(`Dream failed: ${e.message}`, "error");
|
|
225
|
+
})
|
|
226
|
+
.finally(() => {
|
|
227
|
+
ctx.ui.setStatus("dream", undefined);
|
|
228
|
+
});
|
|
223
229
|
},
|
|
224
230
|
});
|
|
225
231
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.2.0",
|
|
7
7
|
"description": "File-system driven persistent memory layer for pi coding agent",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"repository": {
|
|
@@ -35,7 +35,6 @@
|
|
|
35
35
|
"@earendil-works/pi-ai": ">=0.80.2",
|
|
36
36
|
"@earendil-works/pi-coding-agent": ">=0.80.2",
|
|
37
37
|
"@earendil-works/pi-tui": ">=0.80.2",
|
|
38
|
-
"@yandy0725/pi-subagents": "*",
|
|
39
38
|
"typebox": "*"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import {
|
|
3
|
+
type AgentSession,
|
|
4
|
+
type AgentSessionEvent,
|
|
5
|
+
createAgentSession,
|
|
6
|
+
DefaultResourceLoader,
|
|
7
|
+
getAgentDir,
|
|
8
|
+
SessionManager,
|
|
9
|
+
SettingsManager,
|
|
10
|
+
} from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import { MEMORY_AGENT_TOOLS } from "./agent-config";
|
|
12
|
+
import type { ThinkLevel } from "./config";
|
|
13
|
+
import { resolveModel } from "./model-resolver";
|
|
14
|
+
|
|
15
|
+
export interface HeadlessAgentOpts {
|
|
16
|
+
task: string;
|
|
17
|
+
cwd: string;
|
|
18
|
+
modelRegistry: import("@earendil-works/pi-coding-agent").ModelRegistry;
|
|
19
|
+
model?: string;
|
|
20
|
+
parentModel?: Model<any>;
|
|
21
|
+
thinkLevel?: ThinkLevel;
|
|
22
|
+
maxTurns?: number;
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
timeoutMs?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const GRACE_TURNS = 1;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Run a headless memory-agent sub-session: create an in-memory, resource-free
|
|
31
|
+
* session, drive the turn loop, collect the assistant response text, and dispose.
|
|
32
|
+
*
|
|
33
|
+
* Does NOT call bindExtensions — no extension hooks fire in the sub-session,
|
|
34
|
+
* so pi-memory's own before_agent_start cannot recurse.
|
|
35
|
+
*/
|
|
36
|
+
export async function runHeadlessAgent(opts: HeadlessAgentOpts): Promise<string> {
|
|
37
|
+
// 1. Resolve model: undefined → parentModel; otherwise fuzzy resolve (fallback parent)
|
|
38
|
+
const resolvedModel = !opts.model
|
|
39
|
+
? opts.parentModel
|
|
40
|
+
: (resolveModel(opts.model, opts.modelRegistry) ?? opts.parentModel);
|
|
41
|
+
|
|
42
|
+
// 2. Build a pure resource loader (no extensions/skills/context files/etc.)
|
|
43
|
+
const settingsManager = SettingsManager.inMemory();
|
|
44
|
+
const loader = new DefaultResourceLoader({
|
|
45
|
+
cwd: opts.cwd,
|
|
46
|
+
agentDir: getAgentDir(),
|
|
47
|
+
settingsManager,
|
|
48
|
+
noExtensions: true,
|
|
49
|
+
noSkills: true,
|
|
50
|
+
noContextFiles: true,
|
|
51
|
+
noPromptTemplates: true,
|
|
52
|
+
noThemes: true,
|
|
53
|
+
});
|
|
54
|
+
await loader.reload();
|
|
55
|
+
|
|
56
|
+
// 3. Forward abort signal BEFORE createAgentSession (handle early abort)
|
|
57
|
+
let session: AgentSession | undefined;
|
|
58
|
+
const onAbort = (): void => {
|
|
59
|
+
void session?.abort();
|
|
60
|
+
};
|
|
61
|
+
opts.signal?.addEventListener("abort", onAbort, { once: true });
|
|
62
|
+
|
|
63
|
+
// 4. Create the in-memory session (no bindExtensions)
|
|
64
|
+
const created = await createAgentSession({
|
|
65
|
+
cwd: opts.cwd,
|
|
66
|
+
tools: [...MEMORY_AGENT_TOOLS],
|
|
67
|
+
model: resolvedModel as any,
|
|
68
|
+
thinkingLevel: opts.thinkLevel as any,
|
|
69
|
+
modelRegistry: opts.modelRegistry,
|
|
70
|
+
sessionManager: SessionManager.inMemory(opts.cwd),
|
|
71
|
+
settingsManager,
|
|
72
|
+
resourceLoader: loader,
|
|
73
|
+
});
|
|
74
|
+
session = created.session as AgentSession;
|
|
75
|
+
|
|
76
|
+
// 5. Collect response text + enforce turn limits
|
|
77
|
+
let text = "";
|
|
78
|
+
let turnCount = 0;
|
|
79
|
+
let softLimitReached = false;
|
|
80
|
+
const unsubscribe = session.subscribe((event: AgentSessionEvent) => {
|
|
81
|
+
if (event.type === "message_start") {
|
|
82
|
+
text = "";
|
|
83
|
+
} else if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
|
84
|
+
text += event.assistantMessageEvent.delta;
|
|
85
|
+
} else if (event.type === "turn_end") {
|
|
86
|
+
turnCount++;
|
|
87
|
+
if (opts.maxTurns != null) {
|
|
88
|
+
if (!softLimitReached && turnCount >= opts.maxTurns) {
|
|
89
|
+
softLimitReached = true;
|
|
90
|
+
void session.steer("You have reached your turn limit. Finish now.");
|
|
91
|
+
} else if (softLimitReached && turnCount >= opts.maxTurns + GRACE_TURNS) {
|
|
92
|
+
void session.abort();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
// 6. Drive prompt (with optional timeout)
|
|
100
|
+
const promptPromise = session.prompt(opts.task);
|
|
101
|
+
if (opts.timeoutMs != null) {
|
|
102
|
+
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
103
|
+
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
104
|
+
timeoutId = setTimeout(
|
|
105
|
+
() => reject(new Error(`headless agent timed out after ${opts.timeoutMs}ms`)),
|
|
106
|
+
opts.timeoutMs,
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
await Promise.race([promptPromise, timeoutPromise]).finally(() => {
|
|
110
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
111
|
+
});
|
|
112
|
+
} else {
|
|
113
|
+
await promptPromise;
|
|
114
|
+
}
|
|
115
|
+
return text || (session.getLastAssistantText() ?? "");
|
|
116
|
+
} finally {
|
|
117
|
+
opts.signal?.removeEventListener("abort", onAbort);
|
|
118
|
+
unsubscribe();
|
|
119
|
+
session.dispose?.();
|
|
120
|
+
}
|
|
121
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -7,7 +7,7 @@ export type ThinkLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh"
|
|
|
7
7
|
|
|
8
8
|
export interface AutoSurfacingConfig {
|
|
9
9
|
enabled: boolean;
|
|
10
|
-
model
|
|
10
|
+
model?: string;
|
|
11
11
|
thinkLevel: ThinkLevel;
|
|
12
12
|
maxFiles: number;
|
|
13
13
|
maxTopicBytes: number;
|
|
@@ -16,7 +16,7 @@ export interface AutoSurfacingConfig {
|
|
|
16
16
|
|
|
17
17
|
export interface ExtractMemoriesConfig {
|
|
18
18
|
enabled: boolean;
|
|
19
|
-
model
|
|
19
|
+
model?: string;
|
|
20
20
|
thinkLevel: ThinkLevel;
|
|
21
21
|
maxContextTokens: number;
|
|
22
22
|
}
|
|
@@ -26,7 +26,7 @@ export interface MemoryConfig {
|
|
|
26
26
|
memoryDir: string;
|
|
27
27
|
memIndexMaxLines: number;
|
|
28
28
|
memIndexMaxBytes: number;
|
|
29
|
-
dream: { nudgeAfterSessions: number; nudgeAfterHours: number; model
|
|
29
|
+
dream: { nudgeAfterSessions: number; nudgeAfterHours: number; model?: string; thinkLevel: ThinkLevel };
|
|
30
30
|
sessionSearch: { maxSessions: number; maxMatches: number };
|
|
31
31
|
autoSurfacing: AutoSurfacingConfig;
|
|
32
32
|
extractMemories: ExtractMemoriesConfig;
|
|
@@ -37,11 +37,10 @@ export const DEFAULT_CONFIG: MemoryConfig = {
|
|
|
37
37
|
memoryDir: join(homedir(), CONFIG_DIR_NAME, "memory"),
|
|
38
38
|
memIndexMaxLines: 200,
|
|
39
39
|
memIndexMaxBytes: 25600,
|
|
40
|
-
dream: { nudgeAfterSessions: 5, nudgeAfterHours: 24,
|
|
40
|
+
dream: { nudgeAfterSessions: 5, nudgeAfterHours: 24, thinkLevel: "high" },
|
|
41
41
|
sessionSearch: { maxSessions: 10, maxMatches: 5 },
|
|
42
42
|
autoSurfacing: {
|
|
43
43
|
enabled: true,
|
|
44
|
-
model: "auto",
|
|
45
44
|
thinkLevel: "off",
|
|
46
45
|
maxFiles: 5,
|
|
47
46
|
maxTopicBytes: 4096,
|
|
@@ -49,7 +48,6 @@ export const DEFAULT_CONFIG: MemoryConfig = {
|
|
|
49
48
|
},
|
|
50
49
|
extractMemories: {
|
|
51
50
|
enabled: true,
|
|
52
|
-
model: "auto",
|
|
53
51
|
thinkLevel: "high",
|
|
54
52
|
maxContextTokens: 2000,
|
|
55
53
|
},
|
package/src/dream.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { runHeadlessAgent } from "./agent-runner";
|
|
2
4
|
import type { ThinkLevel } from "./config";
|
|
3
|
-
import { getSubagentsService, type SubagentsService, type WorkspaceProvider } from "@yandy0725/pi-subagents";
|
|
4
5
|
|
|
5
|
-
/** Build dream consolidation task.
|
|
6
|
-
* Session context strategy: cwd = memoryDir (via WorkspaceProvider).
|
|
7
|
-
* Dream agent sees only the memory files, no parent conversation history.
|
|
8
|
-
* Tool scope: full file read/write/edit (needed for consolidation). */
|
|
6
|
+
/** Build dream consolidation task. (unchanged) */
|
|
9
7
|
export function buildDreamTask(memoryDir: string, maxLines: number): string {
|
|
10
8
|
return `You are a memory consolidation agent. Your job is to read all memory files
|
|
11
9
|
and consolidate them into a clean, deduplicated memory store.
|
|
@@ -53,80 +51,24 @@ CRITICAL for hooks and descriptions:
|
|
|
53
51
|
}
|
|
54
52
|
|
|
55
53
|
export interface RunDreamOpts {
|
|
56
|
-
model
|
|
54
|
+
model?: string;
|
|
57
55
|
thinkLevel: ThinkLevel;
|
|
58
56
|
memoryDir: string;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
events?: { on(channel: string, handler: (data: any) => void): () => void };
|
|
62
|
-
service?: SubagentsService;
|
|
57
|
+
modelRegistry: ModelRegistry;
|
|
58
|
+
parentModel?: Model<any>;
|
|
63
59
|
}
|
|
64
60
|
|
|
61
|
+
/** Run dream consolidation via a headless agent. Fire-and-forget (caller decides await). */
|
|
65
62
|
export async function runDream(opts: RunDreamOpts): Promise<string> {
|
|
66
|
-
const service = opts.service ?? getSubagentsService();
|
|
67
|
-
if (!service) throw new Error("pi-subagents not available — install @yandy0725/pi-subagents");
|
|
68
|
-
|
|
69
|
-
const events = opts.events;
|
|
70
|
-
if (!events) throw new Error("events required for dream — pass pi.events");
|
|
71
|
-
|
|
72
|
-
const model = opts.model === "auto" ? undefined : opts.model;
|
|
73
63
|
const task = buildDreamTask(opts.memoryDir, 200);
|
|
74
|
-
|
|
75
|
-
// Register workspace provider so the subagent runs in memoryDir
|
|
76
|
-
const provider: WorkspaceProvider = {
|
|
77
|
-
async prepare(_ctx) {
|
|
78
|
-
await access(opts.memoryDir).catch(() => {
|
|
79
|
-
throw new Error(`Memory directory not found: ${opts.memoryDir}`);
|
|
80
|
-
});
|
|
81
|
-
return {
|
|
82
|
-
cwd: opts.memoryDir,
|
|
83
|
-
dispose: () => undefined,
|
|
84
|
-
};
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
const unregister = service.registerWorkspaceProvider(provider);
|
|
88
|
-
|
|
89
|
-
// Spawn the dream subagent
|
|
90
|
-
const agentId = service.spawn(
|
|
91
|
-
"memory-agent",
|
|
64
|
+
return runHeadlessAgent({
|
|
92
65
|
task,
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const cleanup = () => {
|
|
102
|
-
if (settled) return;
|
|
103
|
-
settled = true;
|
|
104
|
-
unsubCompleted();
|
|
105
|
-
unsubFailed();
|
|
106
|
-
unregister();
|
|
107
|
-
opts.signal?.removeEventListener("abort", onAbort);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const onCompleted = (data: { id: string }) => {
|
|
111
|
-
if (data.id !== agentId) return;
|
|
112
|
-
cleanup();
|
|
113
|
-
const record = service.getRecord(agentId);
|
|
114
|
-
resolve(record?.result ?? "Dream completed.");
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const onFailed = (data: { id: string; error?: string }) => {
|
|
118
|
-
if (data.id !== agentId) return;
|
|
119
|
-
cleanup();
|
|
120
|
-
reject(new Error(data.error ?? "Dream agent failed"));
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const onAbort = () => {
|
|
124
|
-
service.abort(agentId);
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
const unsubCompleted = events.on("subagents:completed", onCompleted);
|
|
128
|
-
const unsubFailed = events.on("subagents:failed", onFailed);
|
|
129
|
-
|
|
130
|
-
opts.signal?.addEventListener("abort", onAbort, { once: true });
|
|
66
|
+
cwd: opts.memoryDir,
|
|
67
|
+
modelRegistry: opts.modelRegistry,
|
|
68
|
+
model: opts.model,
|
|
69
|
+
parentModel: opts.parentModel,
|
|
70
|
+
thinkLevel: opts.thinkLevel,
|
|
71
|
+
maxTurns: undefined,
|
|
72
|
+
timeoutMs: 600_000,
|
|
131
73
|
});
|
|
132
74
|
}
|
package/src/extract.ts
CHANGED
|
@@ -1,32 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { runHeadlessAgent } from "./agent-runner";
|
|
2
4
|
import type { ThinkLevel } from "./config";
|
|
3
|
-
import { getSubagentsService, type SubagentsService, type WorkspaceProvider } from "@yandy0725/pi-subagents";
|
|
4
5
|
|
|
5
6
|
export interface RunExtractOpts {
|
|
6
|
-
model
|
|
7
|
+
model?: string;
|
|
7
8
|
thinkLevel: ThinkLevel;
|
|
8
9
|
memoryDir: string;
|
|
9
10
|
messages: Array<{ role: string; content: string }>;
|
|
10
11
|
maxContextTokens: number;
|
|
11
|
-
|
|
12
|
+
modelRegistry: ModelRegistry;
|
|
13
|
+
parentModel?: Model<any>;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
/** Build extraction task prompt.
|
|
15
|
-
* Session context strategy: only current turn (user + assistant messages).
|
|
16
|
-
* No conversation history — extraction is turn-scoped.
|
|
17
|
-
* Tool scope: file read/write only (cwd = memoryDir). */
|
|
16
|
+
/** Build extraction task prompt. (unchanged) */
|
|
18
17
|
export function buildExtractTask(
|
|
19
18
|
memoryDir: string,
|
|
20
19
|
messages: Array<{ role: string; content: string }>,
|
|
21
20
|
maxTokens: number,
|
|
22
21
|
): string {
|
|
23
|
-
// Find user and assistant messages for context
|
|
24
22
|
const fromUser = messages.find((m) => m.role === "user");
|
|
25
23
|
const fromAssistant = messages.findLast((m) => m.role === "assistant");
|
|
26
24
|
const userText = fromUser?.content ?? "";
|
|
27
25
|
const assistantText = fromAssistant?.content ?? "";
|
|
28
26
|
|
|
29
|
-
// Truncate by maxTokens (rough estimate: ~4 chars/token)
|
|
30
27
|
const maxChars = maxTokens * 4;
|
|
31
28
|
const truncatedUser = userText.slice(0, maxChars / 2);
|
|
32
29
|
const truncatedAssistant = assistantText.slice(0, maxChars / 2);
|
|
@@ -79,35 +76,21 @@ export function buildExtractTask(
|
|
|
79
76
|
].join("\n");
|
|
80
77
|
}
|
|
81
78
|
|
|
79
|
+
/** Fire-and-forget memory extraction. Does not await the headless agent. */
|
|
82
80
|
export async function runExtract(opts: RunExtractOpts): Promise<void> {
|
|
83
81
|
if (opts.messages.length === 0) return;
|
|
84
|
-
|
|
85
|
-
const service = opts.service ?? getSubagentsService();
|
|
86
|
-
if (!service) return; // silently skip if no subagent service
|
|
87
|
-
|
|
88
|
-
const model = opts.model === "auto" ? undefined : opts.model;
|
|
89
|
-
const thinkLevel = opts.thinkLevel;
|
|
90
82
|
const task = buildExtractTask(opts.memoryDir, opts.messages, opts.maxContextTokens);
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
async prepare(_ctx) {
|
|
94
|
-
await access(opts.memoryDir).catch(() => {
|
|
95
|
-
throw new Error(`Memory directory not found: ${opts.memoryDir}`);
|
|
96
|
-
});
|
|
97
|
-
return {
|
|
98
|
-
cwd: opts.memoryDir,
|
|
99
|
-
dispose: () => undefined,
|
|
100
|
-
};
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
service.registerWorkspaceProvider(provider);
|
|
104
|
-
|
|
105
|
-
// Fire-and-forget spawn
|
|
106
|
-
service.spawn(
|
|
107
|
-
"memory-agent",
|
|
83
|
+
// fire-and-forget: runner disposes internally via finally
|
|
84
|
+
runHeadlessAgent({
|
|
108
85
|
task,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
86
|
+
cwd: opts.memoryDir,
|
|
87
|
+
modelRegistry: opts.modelRegistry,
|
|
88
|
+
model: opts.model,
|
|
89
|
+
parentModel: opts.parentModel,
|
|
90
|
+
thinkLevel: opts.thinkLevel,
|
|
91
|
+
maxTurns: 5,
|
|
92
|
+
timeoutMs: 120_000,
|
|
93
|
+
}).catch(() => {
|
|
94
|
+
/* silently ignore extract errors */
|
|
95
|
+
});
|
|
113
96
|
}
|
package/src/inject.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import {
|
|
3
|
+
import type { Model } from "@earendil-works/pi-ai";
|
|
4
|
+
import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { runHeadlessAgent } from "./agent-runner";
|
|
6
|
+
import type { ThinkLevel } from "./config";
|
|
4
7
|
import { truncateForInjection } from "./index-file";
|
|
5
8
|
import { parseFrontmatter } from "./topic-file";
|
|
6
9
|
|
|
@@ -114,30 +117,9 @@ export async function injectSurfacedContent(
|
|
|
114
117
|
return `<relevant_memories>\n${blocks.join("\n\n")}\n</relevant_memories>`;
|
|
115
118
|
}
|
|
116
119
|
|
|
117
|
-
/**
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
import type { ThinkLevel } from "./config";
|
|
121
|
-
|
|
122
|
-
export async function runSideQuery(
|
|
123
|
-
prompt: string,
|
|
124
|
-
manifest: TopicManifest[],
|
|
125
|
-
maxFiles: number,
|
|
126
|
-
thinkLevel: ThinkLevel,
|
|
127
|
-
// biome-ignore lint/suspicious/noExplicitAny: pi events API handler
|
|
128
|
-
events?: { on(channel: string, handler: (data: any) => void): () => void },
|
|
129
|
-
): Promise<string[]> {
|
|
130
|
-
// Filter to uninjected candidates only
|
|
131
|
-
const candidates = manifest.filter((t) => {
|
|
132
|
-
// already-injected topics are marked in the prompt, but we also
|
|
133
|
-
// filter here as a safety net
|
|
134
|
-
return !prompt.includes(`[already injected] ${t.filename}`);
|
|
135
|
-
});
|
|
136
|
-
if (candidates.length === 0) return [];
|
|
137
|
-
|
|
138
|
-
// Build a minimal selection prompt — only current user message + topic manifest.
|
|
139
|
-
// With prompt_mode=replace, this IS the entire system prompt (no inheritance).
|
|
140
|
-
const task = [
|
|
120
|
+
/** Build the side-query task prompt. Extracted for testability. */
|
|
121
|
+
export function buildSideQueryTask(prompt: string, maxFiles: number): string {
|
|
122
|
+
return [
|
|
141
123
|
"Respond with ONLY a JSON object.",
|
|
142
124
|
"",
|
|
143
125
|
"Below is a list of memory topic files and a user query.",
|
|
@@ -148,106 +130,50 @@ export async function runSideQuery(
|
|
|
148
130
|
"",
|
|
149
131
|
'Respond with EXACTLY: {"selected_files": [...]}',
|
|
150
132
|
].join("\n");
|
|
133
|
+
}
|
|
151
134
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
135
|
+
/** Parse selected_files JSON from headless agent response. */
|
|
136
|
+
function parseSelectedFiles(result: string, candidates: TopicManifest[], maxFiles: number): string[] {
|
|
137
|
+
try {
|
|
138
|
+
const jsonMatch = result.match(/\{[^}]*"selected_files"[^}]*\}/s);
|
|
139
|
+
if (!jsonMatch) return [];
|
|
140
|
+
const parsed = JSON.parse(jsonMatch[0]);
|
|
141
|
+
const files: string[] = parsed.selected_files ?? [];
|
|
142
|
+
return files.filter((f: string) => candidates.some((c) => c.filename === f)).slice(0, maxFiles);
|
|
143
|
+
} catch {
|
|
144
|
+
return [];
|
|
156
145
|
}
|
|
146
|
+
}
|
|
157
147
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
148
|
+
/** Run a lightweight headless side-query to select relevant topic files.
|
|
149
|
+
* Returns [] on timeout/failure — no fallback. */
|
|
150
|
+
export async function runSideQuery(
|
|
151
|
+
prompt: string,
|
|
152
|
+
manifest: TopicManifest[],
|
|
153
|
+
maxFiles: number,
|
|
154
|
+
thinkLevel: ThinkLevel,
|
|
155
|
+
model: string | undefined,
|
|
156
|
+
modelRegistry: ModelRegistry,
|
|
157
|
+
parentModel: Model<any> | undefined,
|
|
158
|
+
memoryDir: string,
|
|
159
|
+
injectedTopics: Set<string>,
|
|
160
|
+
): Promise<string[]> {
|
|
161
|
+
const candidates = manifest.filter((t) => !injectedTopics.has(t.filename));
|
|
162
|
+
if (candidates.length === 0) return [];
|
|
163
|
+
const task = buildSideQueryTask(prompt, maxFiles);
|
|
166
164
|
try {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
let settled = false;
|
|
177
|
-
const cleanup = () => {
|
|
178
|
-
if (settled) return;
|
|
179
|
-
settled = true;
|
|
180
|
-
clearTimeout(timeout);
|
|
181
|
-
unsubCompleted();
|
|
182
|
-
unsubFailed();
|
|
183
|
-
unregister();
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
const onCompleted = (data: { id: string }) => {
|
|
187
|
-
if (data.id !== agentId) return;
|
|
188
|
-
cleanup();
|
|
189
|
-
const record = service.getRecord(agentId);
|
|
190
|
-
const result = record?.result ?? "";
|
|
191
|
-
try {
|
|
192
|
-
const jsonMatch = result.match(/\{[^}]*"selected_files"[^}]*\}/s);
|
|
193
|
-
if (jsonMatch) {
|
|
194
|
-
const parsed = JSON.parse(jsonMatch[0]);
|
|
195
|
-
const files: string[] = parsed.selected_files ?? [];
|
|
196
|
-
const valid = files.filter((f: string) => candidates.some((c) => c.filename === f)).slice(0, maxFiles);
|
|
197
|
-
resolve(valid);
|
|
198
|
-
} else {
|
|
199
|
-
resolve([]);
|
|
200
|
-
}
|
|
201
|
-
} catch {
|
|
202
|
-
resolve([]);
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
const onFailed = (data: { id: string }) => {
|
|
207
|
-
if (data.id !== agentId) return;
|
|
208
|
-
cleanup();
|
|
209
|
-
resolve(keywordMatch(candidates, prompt, maxFiles));
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
const unsubCompleted = events?.on("subagents:completed", onCompleted) ?? (() => {});
|
|
213
|
-
const unsubFailed = events?.on("subagents:failed", onFailed) ?? (() => {});
|
|
214
|
-
|
|
215
|
-
if (!events) {
|
|
216
|
-
cleanup();
|
|
217
|
-
resolve(keywordMatch(candidates, prompt, maxFiles));
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// Spawn AFTER subscriptions are active
|
|
222
|
-
const agentId = service.spawn("memory-agent", task, {
|
|
223
|
-
maxTurns: 1,
|
|
224
|
-
inheritContext: false,
|
|
225
|
-
thinkingLevel: thinkLevel,
|
|
226
|
-
foreground: true,
|
|
227
|
-
bypassQueue: true,
|
|
228
|
-
});
|
|
165
|
+
const result = await runHeadlessAgent({
|
|
166
|
+
task,
|
|
167
|
+
cwd: memoryDir,
|
|
168
|
+
modelRegistry,
|
|
169
|
+
model,
|
|
170
|
+
parentModel,
|
|
171
|
+
thinkLevel,
|
|
172
|
+
maxTurns: 1,
|
|
173
|
+
timeoutMs: 30_000,
|
|
229
174
|
});
|
|
230
|
-
|
|
231
|
-
return result;
|
|
175
|
+
return parseSelectedFiles(result, candidates, maxFiles);
|
|
232
176
|
} catch {
|
|
233
|
-
|
|
234
|
-
return keywordMatch(candidates, prompt, maxFiles);
|
|
177
|
+
return [];
|
|
235
178
|
}
|
|
236
179
|
}
|
|
237
|
-
|
|
238
|
-
/** Keyword-matching fallback for topic selection (no LLM required). */
|
|
239
|
-
function keywordMatch(manifest: TopicManifest[], userPrompt: string, maxFiles: number): string[] {
|
|
240
|
-
const words = userPrompt
|
|
241
|
-
.toLowerCase()
|
|
242
|
-
.split(/\W+/)
|
|
243
|
-
.filter((w) => w.length > 2);
|
|
244
|
-
const scored = manifest
|
|
245
|
-
.map((t) => {
|
|
246
|
-
const desc = `${t.description} ${t.name}`.toLowerCase();
|
|
247
|
-
const score = words.filter((w) => desc.includes(w)).length;
|
|
248
|
-
return { filename: t.filename, score };
|
|
249
|
-
})
|
|
250
|
-
.filter((t) => t.score > 0)
|
|
251
|
-
.sort((a, b) => b.score - a.score);
|
|
252
|
-
return scored.slice(0, maxFiles).map((t) => t.filename);
|
|
253
|
-
}
|
package/src/memory-tool.ts
CHANGED
|
@@ -110,7 +110,10 @@ export async function doAdd(memoryDir: string, p: AddParams): Promise<ActionResu
|
|
|
110
110
|
|
|
111
111
|
// Build hook from all entries (comma-separated titles, trimmed to ~150 chars)
|
|
112
112
|
const allEntries = parseEntries(topicContent);
|
|
113
|
-
const hook = allEntries
|
|
113
|
+
const hook = allEntries
|
|
114
|
+
.map((e) => e.title)
|
|
115
|
+
.join("; ")
|
|
116
|
+
.slice(0, 150);
|
|
114
117
|
next = updateHook(entries, topic, hook);
|
|
115
118
|
if (!checkCapacity(next, p.maxLines, p.maxBytes)) {
|
|
116
119
|
return {
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
|
|
4
|
+
interface ModelEntry {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
provider: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Resolve a model string to a Model instance.
|
|
12
|
+
* Tries exact "provider/modelId" match (only available models), then fuzzy match.
|
|
13
|
+
* Returns the Model on success, or undefined on failure (caller falls back to parent model).
|
|
14
|
+
*/
|
|
15
|
+
export function resolveModel(input: string, registry: ModelRegistry): Model<any> | undefined {
|
|
16
|
+
if (!input) return undefined;
|
|
17
|
+
const all = (registry.getAvailable?.() ?? registry.getAll()) as ModelEntry[];
|
|
18
|
+
if (all.length === 0) return undefined;
|
|
19
|
+
const availableSet = new Set(all.map((m) => `${m.provider}/${m.id}`.toLowerCase()));
|
|
20
|
+
|
|
21
|
+
// 1. Exact match: "provider/modelId" — only if available (has auth)
|
|
22
|
+
const slashIdx = input.indexOf("/");
|
|
23
|
+
if (slashIdx !== -1) {
|
|
24
|
+
const provider = input.slice(0, slashIdx);
|
|
25
|
+
const modelId = input.slice(slashIdx + 1);
|
|
26
|
+
if (availableSet.has(input.toLowerCase())) {
|
|
27
|
+
const found = registry.find(provider, modelId);
|
|
28
|
+
if (found) return found;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// 2. Fuzzy match against available models
|
|
33
|
+
const query = input.toLowerCase();
|
|
34
|
+
let bestMatch: ModelEntry | undefined;
|
|
35
|
+
let bestScore = 0;
|
|
36
|
+
|
|
37
|
+
for (const m of all) {
|
|
38
|
+
const id = m.id.toLowerCase();
|
|
39
|
+
const name = m.name.toLowerCase();
|
|
40
|
+
const full = `${m.provider}/${m.id}`.toLowerCase();
|
|
41
|
+
|
|
42
|
+
let score = 0;
|
|
43
|
+
if (id === query || full === query) {
|
|
44
|
+
score = 100;
|
|
45
|
+
} else if (id.includes(query) || full.includes(query)) {
|
|
46
|
+
score = 60 + (query.length / id.length) * 30;
|
|
47
|
+
} else if (name.includes(query)) {
|
|
48
|
+
score = 40 + (query.length / name.length) * 20;
|
|
49
|
+
} else if (
|
|
50
|
+
query
|
|
51
|
+
.split(/[\s\-/]+/)
|
|
52
|
+
.every((part) => id.includes(part) || name.includes(part) || m.provider.toLowerCase().includes(part))
|
|
53
|
+
) {
|
|
54
|
+
score = 20;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (score > bestScore) {
|
|
58
|
+
bestScore = score;
|
|
59
|
+
bestMatch = m;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (bestMatch && bestScore >= 20) {
|
|
64
|
+
const found = registry.find(bestMatch.provider, bestMatch.id);
|
|
65
|
+
if (found) return found;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
package/src/agent-types.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* agent-types.ts — Register a single minimal agent type for pi-memory subagents.
|
|
3
|
-
*
|
|
4
|
-
* Writes `memory-agent.md` to ~/.pi/agent/agents/ so pi-subagents
|
|
5
|
-
* picks it up as a custom agent type with restricted tools and replace-mode prompt.
|
|
6
|
-
* Written during extension factory (before pi-subagents initializes its registry).
|
|
7
|
-
*
|
|
8
|
-
* One type serves three tasks:
|
|
9
|
-
* - side-query: thinking=off, maxTurns=1
|
|
10
|
-
* - extractMemories: thinking=high, maxTurns=5
|
|
11
|
-
* - dream: thinking=high (unlimited turns)
|
|
12
|
-
*
|
|
13
|
-
* On first install (before pi restart), pi-subagents auto-falls-back to
|
|
14
|
-
* general-purpose. No manual action needed.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
18
|
-
import { homedir } from "node:os";
|
|
19
|
-
import { join } from "node:path";
|
|
20
|
-
import { CONFIG_DIR_NAME } from "@earendil-works/pi-coding-agent";
|
|
21
|
-
|
|
22
|
-
const AGENTS_DIR = join(homedir(), CONFIG_DIR_NAME, "agent", "agents");
|
|
23
|
-
|
|
24
|
-
const DEFINITION = `---
|
|
25
|
-
description: Memory maintenance agent — file read/write only (no bash/websearch)
|
|
26
|
-
tools: read, write, edit, ls
|
|
27
|
-
prompt_mode: replace
|
|
28
|
-
---
|
|
29
|
-
`;
|
|
30
|
-
|
|
31
|
-
export function ensureAgentTypes(): void {
|
|
32
|
-
try {
|
|
33
|
-
mkdirSync(AGENTS_DIR, { recursive: true });
|
|
34
|
-
} catch {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const path = join(AGENTS_DIR, "memory-agent.md");
|
|
39
|
-
if (!existsSync(path)) {
|
|
40
|
-
try {
|
|
41
|
-
writeFileSync(path, DEFINITION, "utf8");
|
|
42
|
-
} catch {}
|
|
43
|
-
}
|
|
44
|
-
}
|