@yandy0725/pi-memory 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +12 -0
- package/package.json +1 -1
- package/src/extract.ts +8 -2
package/index.ts
CHANGED
|
@@ -17,6 +17,15 @@ import { readDreamMeta, shouldNudge, writeDreamMeta } from "./src/nudge";
|
|
|
17
17
|
import { resolveMemoryDir } from "./src/paths";
|
|
18
18
|
import { searchSessions } from "./src/session-search";
|
|
19
19
|
|
|
20
|
+
function extractAgentsMdBlocks(systemPrompt: string): string[] {
|
|
21
|
+
const blocks: string[] = [];
|
|
22
|
+
const re = /<project_instructions\s+path="([^"]+)">\n([\s\S]*?)<\/project_instructions>/g;
|
|
23
|
+
for (const match of systemPrompt.matchAll(re)) {
|
|
24
|
+
blocks.push(match[0]);
|
|
25
|
+
}
|
|
26
|
+
return blocks;
|
|
27
|
+
}
|
|
28
|
+
|
|
20
29
|
function resolveDefault(cfg: MemoryConfig, task: "dream" | "autoSurfacing" | "extractMemories", key: "model"): string | undefined;
|
|
21
30
|
function resolveDefault(cfg: MemoryConfig, task: "dream" | "autoSurfacing" | "extractMemories", key: "sessionPersistence"): SessionPersistenceConfig | undefined;
|
|
22
31
|
function resolveDefault(cfg: MemoryConfig, task: "dream" | "autoSurfacing" | "extractMemories", key: "model" | "sessionPersistence"): string | SessionPersistenceConfig | undefined {
|
|
@@ -31,6 +40,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
31
40
|
let indexSnapshot = "";
|
|
32
41
|
let toolRegistered = false;
|
|
33
42
|
const injectedTopics = new Set<string>();
|
|
43
|
+
let lastSystemPrompt = "";
|
|
34
44
|
|
|
35
45
|
pi.on("session_start", async (_event, ctx) => {
|
|
36
46
|
config = await loadConfig(ctx);
|
|
@@ -91,6 +101,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
91
101
|
});
|
|
92
102
|
|
|
93
103
|
pi.on("before_agent_start", async (event, ctx) => {
|
|
104
|
+
lastSystemPrompt = event.systemPrompt;
|
|
94
105
|
if (!config?.enabled || !indexSnapshot || !memoryDir) return;
|
|
95
106
|
|
|
96
107
|
const autoSurfacing = config.autoSurfacing;
|
|
@@ -151,6 +162,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
151
162
|
if (!extractConfig?.enabled) return;
|
|
152
163
|
if (!event.messages || event.messages.length === 0) return;
|
|
153
164
|
runExtract({
|
|
165
|
+
agentsMdBlocks: extractAgentsMdBlocks(lastSystemPrompt),
|
|
154
166
|
model: resolveDefault(config, "extractMemories", "model"),
|
|
155
167
|
thinkLevel: extractConfig.thinkLevel,
|
|
156
168
|
memoryDir,
|
package/package.json
CHANGED
package/src/extract.ts
CHANGED
|
@@ -13,6 +13,7 @@ export interface RunExtractOpts {
|
|
|
13
13
|
parentModel?: Model<any>;
|
|
14
14
|
sessionPersistence?: SessionPersistenceConfig;
|
|
15
15
|
customTools?: ToolDefinition[];
|
|
16
|
+
agentsMdBlocks?: string[]; // 新增
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
/** Build extraction task prompt using memory tools instead of raw file I/O. */
|
|
@@ -20,6 +21,7 @@ export function buildExtractTask(
|
|
|
20
21
|
memoryDir: string,
|
|
21
22
|
messages: Array<{ role: string; content: string }>,
|
|
22
23
|
maxTokens: number,
|
|
24
|
+
agentsMdBlocks: string[] = [], // 新增
|
|
23
25
|
): string {
|
|
24
26
|
const fromUser = messages.find((m) => m.role === "user");
|
|
25
27
|
const fromAssistant = messages.findLast((m) => m.role === "assistant");
|
|
@@ -54,11 +56,12 @@ export function buildExtractTask(
|
|
|
54
56
|
"- Project conventions: architecture decisions, file organization, tech stack choices",
|
|
55
57
|
"- Discoveries: debugging workarounds, gotchas, configuration quirks, undocumented behavior",
|
|
56
58
|
"- References: external docs, APIs, or systems the user treats as important",
|
|
59
|
+
"- AGENTS.md rules that were violated in this conversation — extract for memory-level reinforcement (refer to the AGENTS.md content below)",
|
|
57
60
|
"",
|
|
58
61
|
"## What to Skip",
|
|
59
62
|
"- One-time task instructions or ephemeral details",
|
|
60
63
|
"- Code snippets or file paths derivable from the project",
|
|
61
|
-
"-
|
|
64
|
+
"- AGENTS.md rules that were followed without issue (refer to the AGENTS.md content below)",
|
|
62
65
|
"- Git history or recent changes",
|
|
63
66
|
"- Obvious or trivial observations",
|
|
64
67
|
"",
|
|
@@ -68,6 +71,9 @@ export function buildExtractTask(
|
|
|
68
71
|
"- Be concise but complete — one clear point per entry",
|
|
69
72
|
"- When in doubt, skip it",
|
|
70
73
|
"",
|
|
74
|
+
...(agentsMdBlocks.length > 0
|
|
75
|
+
? ["## AGENTS.md Rules", ...agentsMdBlocks, ""]
|
|
76
|
+
: []),
|
|
71
77
|
"=== Conversation ===",
|
|
72
78
|
`User: ${truncatedUser}`,
|
|
73
79
|
`Assistant: ${truncatedAssistant}`,
|
|
@@ -77,7 +83,7 @@ export function buildExtractTask(
|
|
|
77
83
|
/** Fire-and-forget memory extraction. Does not await the headless agent. */
|
|
78
84
|
export async function runExtract(opts: RunExtractOpts): Promise<void> {
|
|
79
85
|
if (opts.messages.length === 0) return;
|
|
80
|
-
const task = buildExtractTask(opts.memoryDir, opts.messages, opts.maxContextTokens);
|
|
86
|
+
const task = buildExtractTask(opts.memoryDir, opts.messages, opts.maxContextTokens, opts.agentsMdBlocks ?? []);
|
|
81
87
|
// fire-and-forget: runner disposes internally via finally
|
|
82
88
|
runHeadlessAgent({
|
|
83
89
|
task,
|