@chendpoc/pi-memory 0.2.3 → 0.3.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 +92 -43
- package/dist/adapters/llm/standalone.js +1 -1
- package/dist/cli/init.js +20 -3
- package/dist/cli/parseArgs.d.ts +5 -2
- package/dist/cli/parseArgs.js +13 -0
- package/dist/cli/schedulerSync.d.ts +2 -0
- package/dist/cli/schedulerSync.js +26 -0
- package/dist/cli/status.d.ts +4 -40
- package/dist/cli/status.js +6 -230
- package/dist/cli/theme.d.ts +2 -0
- package/dist/cli/theme.js +8 -0
- package/dist/cli.js +5 -0
- package/dist/commands/status.js +2 -2
- package/dist/compact/parseMemoryExport.js +2 -1
- package/dist/compact/register.js +1 -1
- package/dist/compact/runSummary.js +1 -16
- package/dist/compact/subagentDelta.js +3 -5
- package/dist/consolidate/index.d.ts +1 -0
- package/dist/consolidate/index.js +1 -0
- package/dist/consolidate/mergeEntries.js +3 -3
- package/dist/consolidate/mergeMemoryEntries.d.ts +8 -0
- package/dist/consolidate/mergeMemoryEntries.js +23 -0
- package/dist/consolidate/mergePrompt.js +2 -2
- package/dist/consolidate/mergeWithLlm.js +2 -2
- package/dist/consolidate/runJob.d.ts +2 -2
- package/dist/consolidate/runJob.js +6 -12
- package/dist/consolidate/scheduler.d.ts +2 -2
- package/dist/consolidate/scheduler.js +1 -1
- package/dist/constants/env.d.ts +1 -0
- package/dist/constants/env.js +1 -0
- package/dist/constants/paths.d.ts +3 -1
- package/dist/constants/paths.js +6 -1
- package/dist/extension/createMemoryRuntime.d.ts +3 -0
- package/dist/extension/createMemoryRuntime.js +203 -0
- package/dist/extension/index.d.ts +2 -0
- package/dist/extension/index.js +1 -0
- package/dist/extension/lifecycle.d.ts +28 -0
- package/dist/extension/lifecycle.js +52 -0
- package/dist/extension/messageUtils.d.ts +4 -0
- package/dist/extension/messageUtils.js +18 -0
- package/dist/extension/types.d.ts +35 -0
- package/dist/extension/types.js +1 -0
- package/dist/index.d.ts +7 -9
- package/dist/index.js +7 -9
- package/dist/pi-extension.js +26 -236
- package/dist/preflight/episodic.js +13 -30
- package/dist/preflight/queryIntent.js +1 -1
- package/dist/preflight/render.js +1 -1
- package/dist/preflight/strip.d.ts +0 -1
- package/dist/preflight/strip.js +0 -24
- package/dist/redaction/index.d.ts +4 -0
- package/dist/redaction/index.js +3 -0
- package/dist/redaction/patterns/constants.d.ts +6 -0
- package/dist/redaction/patterns/constants.js +6 -0
- package/dist/redaction/patterns/crypto.d.ts +3 -0
- package/dist/redaction/patterns/crypto.js +17 -0
- package/dist/redaction/patterns/generic.d.ts +3 -0
- package/dist/redaction/patterns/generic.js +38 -0
- package/dist/redaction/patterns/index.d.ts +16 -0
- package/dist/redaction/patterns/index.js +23 -0
- package/dist/redaction/patterns/llm.d.ts +3 -0
- package/dist/redaction/patterns/llm.js +144 -0
- package/dist/redaction/patterns/platform.d.ts +3 -0
- package/dist/redaction/patterns/platform.js +87 -0
- package/dist/redaction/patterns/types.d.ts +18 -0
- package/dist/redaction/patterns/types.js +1 -0
- package/dist/redaction/redactText.d.ts +9 -0
- package/dist/redaction/redactText.js +31 -0
- package/dist/redaction/types.d.ts +19 -0
- package/dist/redaction/types.js +1 -0
- package/dist/redaction/utils.d.ts +28 -0
- package/dist/redaction/utils.js +106 -0
- package/dist/scheduler/index.d.ts +3 -0
- package/dist/scheduler/index.js +3 -0
- package/dist/scheduler/launchd.d.ts +14 -0
- package/dist/scheduler/launchd.js +69 -0
- package/dist/scheduler/launchdPlist.d.ts +14 -0
- package/dist/scheduler/launchdPlist.js +62 -0
- package/dist/scheduler/sync.d.ts +36 -0
- package/dist/scheduler/sync.js +79 -0
- package/dist/shutdown/enqueue.d.ts +1 -1
- package/dist/shutdown/enqueue.js +2 -5
- package/dist/shutdown/readQueue.js +1 -1
- package/dist/shutdown/runDrainJob.js +8 -37
- package/dist/shutdown/sessionReader.js +1 -14
- package/dist/sidecar/client.d.ts +6 -2
- package/dist/sidecar/client.js +49 -14
- package/dist/{preflight → sidecar}/queryCache.d.ts +1 -1
- package/dist/sidecar/reindexBridge.js +2 -2
- package/dist/sidecar/server/server.js +1 -1
- package/dist/sidecar/server/vec/chunkQuery.d.ts +4 -0
- package/dist/sidecar/server/vec/chunkQuery.js +46 -0
- package/dist/sidecar/server/vec/chunkReindex.d.ts +5 -0
- package/dist/sidecar/server/vec/chunkReindex.js +40 -0
- package/dist/sidecar/server/vec/embeddingCodec.d.ts +2 -0
- package/dist/sidecar/server/vec/embeddingCodec.js +6 -0
- package/dist/sidecar/server/vec/schema.d.ts +20 -0
- package/dist/sidecar/server/vec/schema.js +61 -0
- package/dist/sidecar/server/vec/store.d.ts +2 -13
- package/dist/sidecar/server/vec/store.js +12 -139
- package/dist/sidecar/sidecarManager.js +4 -58
- package/dist/sidecar/spawnLock.d.ts +2 -0
- package/dist/sidecar/spawnLock.js +57 -0
- package/dist/sidecar/syncIndex.d.ts +3 -0
- package/dist/sidecar/syncIndex.js +12 -0
- package/dist/sidecar/warmup.js +1 -1
- package/dist/status/copy.d.ts +2 -0
- package/dist/status/copy.js +2 -0
- package/dist/status/format.d.ts +7 -0
- package/dist/status/format.js +133 -0
- package/dist/status/gather.d.ts +2 -0
- package/dist/status/gather.js +88 -0
- package/dist/status/index.d.ts +4 -0
- package/dist/status/index.js +3 -0
- package/dist/status/types.d.ts +33 -0
- package/dist/status/types.js +1 -0
- package/dist/store/consolidatePort.d.ts +11 -0
- package/dist/store/consolidatePort.js +1 -0
- package/dist/store/index.d.ts +2 -0
- package/dist/store/index.js +1 -0
- package/dist/store/ingestEntries.d.ts +16 -0
- package/dist/store/ingestEntries.js +22 -0
- package/dist/{init/workspace.d.ts → store/initWorkspace.d.ts} +1 -1
- package/dist/{init/workspace.js → store/initWorkspace.js} +7 -5
- package/dist/store/listeners.d.ts +11 -0
- package/dist/store/listeners.js +27 -0
- package/dist/store/markdown/insert.d.ts +3 -0
- package/dist/store/markdown/insert.js +23 -0
- package/dist/store/memoryStore.d.ts +9 -22
- package/dist/store/memoryStore.js +71 -205
- package/dist/store/resolveEntries.d.ts +11 -0
- package/dist/store/resolveEntries.js +23 -0
- package/dist/store/types.d.ts +0 -1
- package/dist/store/writePath.d.ts +20 -0
- package/dist/store/writePath.js +123 -0
- package/dist/ui/memoryStatusWidget.d.ts +4 -8
- package/dist/ui/memoryStatusWidget.js +5 -17
- package/dist/utils/async.d.ts +11 -0
- package/dist/utils/async.js +24 -0
- package/dist/utils/index.d.ts +5 -1
- package/dist/utils/index.js +5 -1
- package/dist/{ipc/jsonlFramer.d.ts → utils/jsonl.d.ts} +1 -1
- package/dist/{ipc/jsonlFramer.js → utils/jsonl.js} +1 -1
- package/dist/utils/memory/index.d.ts +10 -0
- package/dist/utils/memory/index.js +43 -0
- package/dist/utils/paths.d.ts +4 -0
- package/dist/utils/paths.js +13 -3
- package/dist/utils/scheduler.d.ts +1 -1
- package/dist/utils/scheduler.js +6 -6
- package/dist/{preflight/session.d.ts → utils/session/index.d.ts} +1 -0
- package/dist/{preflight/session.js → utils/session/index.js} +5 -2
- package/doc/README-zh.md +445 -0
- package/package.json +9 -4
- package/scripts/postinstall.mjs +11 -1
- package/templates/com.pi.memory.consolidate.plist.example +41 -0
- package/templates/consolidate.cmd.example +15 -0
- package/templates/crontab.example +14 -0
- package/templates/schtasks.example.txt +34 -0
- package/dist/consolidate/entryKey.d.ts +0 -5
- package/dist/consolidate/entryKey.js +0 -4
- /package/dist/{preflight → sidecar}/queryCache.js +0 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { createLlmClient } from "../adapters/llm/index.js";
|
|
2
|
+
import { readPiMemoryEnv, resolveMemoryAgentDir } from "../config/index.js";
|
|
3
|
+
import { runEpisodicPreflight } from "../preflight/episodic.js";
|
|
4
|
+
import { queryIntentCache } from "../preflight/intentCache.js";
|
|
5
|
+
import { injectPrivateMemoryContext } from "../preflight/strip.js";
|
|
6
|
+
import { enqueueShutdownMetadata } from "../shutdown/enqueue.js";
|
|
7
|
+
import { resolveSidecarPaths } from "../sidecar/paths.js";
|
|
8
|
+
import { stopSidecar } from "../sidecar/sidecarManager.js";
|
|
9
|
+
import { isSubagentSession, readParentSession } from "../utils/session/index.js";
|
|
10
|
+
import { formatTimestamp } from "../utils/time.js";
|
|
11
|
+
import { MemoryStore } from "../store/memoryStore.js";
|
|
12
|
+
import { syncMaintenanceScheduler } from "../scheduler/sync.js";
|
|
13
|
+
import { bootstrapConsolidate, bootstrapSidecar, loadSessionMemoryCap, mergePrivateMemoryBlocks, } from "./lifecycle.js";
|
|
14
|
+
import { findLastUserMessageIndex, getUserMessageText, setUserMessageText, } from "./messageUtils.js";
|
|
15
|
+
class MemoryRuntimeImpl {
|
|
16
|
+
store;
|
|
17
|
+
sidecarPaths;
|
|
18
|
+
sessionId;
|
|
19
|
+
isSubagent;
|
|
20
|
+
llmClient = null;
|
|
21
|
+
sessionMemoryCap = null;
|
|
22
|
+
turnPreflight = null;
|
|
23
|
+
isFirstTurn = true;
|
|
24
|
+
reindexScheduler = null;
|
|
25
|
+
consolidateScheduler = null;
|
|
26
|
+
stopConsolidateInterval = null;
|
|
27
|
+
unsubSyncToSidecar = null;
|
|
28
|
+
unsubConsolidateCheck = null;
|
|
29
|
+
constructor(opts) {
|
|
30
|
+
const agentDir = opts.agentDir ?? resolveMemoryAgentDir();
|
|
31
|
+
this.store = new MemoryStore({ agentDir });
|
|
32
|
+
this.sidecarPaths = resolveSidecarPaths(agentDir);
|
|
33
|
+
this.isSubagent = isSubagentSession(opts.ctx);
|
|
34
|
+
this.sessionId = opts.ctx.sessionManager.getSessionFile() ?? null;
|
|
35
|
+
}
|
|
36
|
+
getLlm() {
|
|
37
|
+
return this.llmClient;
|
|
38
|
+
}
|
|
39
|
+
getSessionMemoryCap() {
|
|
40
|
+
return this.sessionMemoryCap;
|
|
41
|
+
}
|
|
42
|
+
getTurnPreflight() {
|
|
43
|
+
return this.turnPreflight;
|
|
44
|
+
}
|
|
45
|
+
async bootstrap(ctx, pi) {
|
|
46
|
+
this.isFirstTurn = true;
|
|
47
|
+
this.turnPreflight = null;
|
|
48
|
+
await this.refreshLlm(ctx, pi);
|
|
49
|
+
await this.store.ensureInitialized();
|
|
50
|
+
void syncMaintenanceScheduler({ agentDir: this.store.agentDir });
|
|
51
|
+
try {
|
|
52
|
+
const sidecar = await bootstrapSidecar({
|
|
53
|
+
store: this.store,
|
|
54
|
+
sidecarPaths: this.sidecarPaths,
|
|
55
|
+
reindexScheduler: this.reindexScheduler,
|
|
56
|
+
});
|
|
57
|
+
this.reindexScheduler = sidecar.reindexScheduler;
|
|
58
|
+
this.unsubSyncToSidecar = sidecar.unsubSyncToSidecar;
|
|
59
|
+
const consolidate = bootstrapConsolidate({
|
|
60
|
+
store: this.store,
|
|
61
|
+
getLlm: () => this.llmClient,
|
|
62
|
+
onComplete: () => this.reloadSessionMemoryCap(),
|
|
63
|
+
stopExistingInterval: this.stopConsolidateInterval,
|
|
64
|
+
});
|
|
65
|
+
this.consolidateScheduler = consolidate.consolidateScheduler;
|
|
66
|
+
this.stopConsolidateInterval = consolidate.stopConsolidateInterval;
|
|
67
|
+
this.unsubConsolidateCheck = consolidate.unsubConsolidateCheck;
|
|
68
|
+
await this.reloadSessionMemoryCap();
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// sidecar optional; preflight falls back to MEMORY.md
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async refreshLlm(ctx, pi) {
|
|
75
|
+
const env = readPiMemoryEnv();
|
|
76
|
+
const flagModel = pi.getFlag("memory-helper-model");
|
|
77
|
+
const modelSpec = typeof flagModel === "string" ? flagModel : env.helperModel;
|
|
78
|
+
this.llmClient = await createLlmClient({ ctx, modelSpec, env });
|
|
79
|
+
}
|
|
80
|
+
async runBeforeAgentStart(event, ctx) {
|
|
81
|
+
const userPayload = String(event.prompt ?? "").trim();
|
|
82
|
+
if (!userPayload)
|
|
83
|
+
return;
|
|
84
|
+
const forceHelper = this.isFirstTurn;
|
|
85
|
+
this.isFirstTurn = false;
|
|
86
|
+
if (this.isSubagent) {
|
|
87
|
+
this.turnPreflight = { userPayload, privateContext: this.sessionMemoryCap ?? "" };
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const workingUi = ctx.hasUI
|
|
91
|
+
? {
|
|
92
|
+
show: (msg) => ctx.ui.setWorkingMessage(msg),
|
|
93
|
+
update: (msg) => ctx.ui.setWorkingMessage(msg),
|
|
94
|
+
clear: () => ctx.ui.setWorkingMessage(),
|
|
95
|
+
}
|
|
96
|
+
: null;
|
|
97
|
+
try {
|
|
98
|
+
workingUi?.show("Recalling memory…");
|
|
99
|
+
const env = readPiMemoryEnv();
|
|
100
|
+
const result = await runEpisodicPreflight(userPayload, {
|
|
101
|
+
socketPath: this.sidecarPaths.socketPath,
|
|
102
|
+
agentDir: this.store.agentDir,
|
|
103
|
+
store: this.store,
|
|
104
|
+
llm: this.llmClient,
|
|
105
|
+
forceEpisodic: forceHelper,
|
|
106
|
+
sessionId: this.sessionId ?? undefined,
|
|
107
|
+
budgetMs: env.preflightBudgetMs,
|
|
108
|
+
signal: ctx.signal,
|
|
109
|
+
onProgress: workingUi?.update,
|
|
110
|
+
});
|
|
111
|
+
const episodicContext = result?.privateContext ?? null;
|
|
112
|
+
const combined = mergePrivateMemoryBlocks(this.sessionMemoryCap, episodicContext);
|
|
113
|
+
this.turnPreflight = { userPayload, privateContext: combined ?? "" };
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
workingUi?.clear();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async runContext(event, ctx) {
|
|
120
|
+
const userIndex = findLastUserMessageIndex(event.messages);
|
|
121
|
+
if (userIndex < 0)
|
|
122
|
+
return;
|
|
123
|
+
const scaffolded = getUserMessageText(event.messages[userIndex]);
|
|
124
|
+
if (!scaffolded?.trim())
|
|
125
|
+
return;
|
|
126
|
+
let privateContext;
|
|
127
|
+
let userPayload;
|
|
128
|
+
if (this.turnPreflight && this.turnPreflight.userPayload === scaffolded.trim()) {
|
|
129
|
+
privateContext = this.turnPreflight.privateContext || undefined;
|
|
130
|
+
userPayload = this.turnPreflight.userPayload;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
const userTurnCount = event.messages.filter((message) => message.role === "user").length;
|
|
134
|
+
const env = readPiMemoryEnv();
|
|
135
|
+
if (this.isSubagent) {
|
|
136
|
+
privateContext = this.sessionMemoryCap ?? undefined;
|
|
137
|
+
userPayload = scaffolded;
|
|
138
|
+
this.turnPreflight = { userPayload: scaffolded, privateContext: privateContext ?? "" };
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
const preflight = await runEpisodicPreflight(scaffolded, {
|
|
142
|
+
socketPath: this.sidecarPaths.socketPath,
|
|
143
|
+
agentDir: this.store.agentDir,
|
|
144
|
+
store: this.store,
|
|
145
|
+
llm: this.llmClient,
|
|
146
|
+
forceEpisodic: userTurnCount === 1,
|
|
147
|
+
sessionId: this.sessionId ?? undefined,
|
|
148
|
+
budgetMs: env.preflightBudgetMs,
|
|
149
|
+
signal: ctx.signal,
|
|
150
|
+
});
|
|
151
|
+
privateContext =
|
|
152
|
+
mergePrivateMemoryBlocks(this.sessionMemoryCap, preflight?.privateContext) || undefined;
|
|
153
|
+
userPayload = scaffolded;
|
|
154
|
+
this.turnPreflight = { userPayload: scaffolded, privateContext: privateContext ?? "" };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (!privateContext)
|
|
158
|
+
return;
|
|
159
|
+
const injectedText = injectPrivateMemoryContext(scaffolded, userPayload, privateContext);
|
|
160
|
+
const messages = [...event.messages];
|
|
161
|
+
messages[userIndex] = setUserMessageText(messages[userIndex], injectedText);
|
|
162
|
+
return { messages };
|
|
163
|
+
}
|
|
164
|
+
async shutdown(event, ctx) {
|
|
165
|
+
const header = ctx.sessionManager.getHeader();
|
|
166
|
+
void enqueueShutdownMetadata(this.store.agentDir, {
|
|
167
|
+
sessionFile: ctx.sessionManager.getSessionFile() ?? "",
|
|
168
|
+
parentSession: readParentSession(header),
|
|
169
|
+
reason: event.reason,
|
|
170
|
+
isSubagent: this.isSubagent,
|
|
171
|
+
enqueuedAt: formatTimestamp(),
|
|
172
|
+
}).catch(() => { });
|
|
173
|
+
if (this.sessionId) {
|
|
174
|
+
queryIntentCache.clearSession(this.sessionId);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async reloadSessionMemoryCap() {
|
|
178
|
+
this.sessionMemoryCap = await loadSessionMemoryCap(this.store);
|
|
179
|
+
}
|
|
180
|
+
async dispose() {
|
|
181
|
+
this.stopConsolidateInterval?.();
|
|
182
|
+
this.stopConsolidateInterval = null;
|
|
183
|
+
this.unsubSyncToSidecar?.();
|
|
184
|
+
this.unsubSyncToSidecar = null;
|
|
185
|
+
this.unsubConsolidateCheck?.();
|
|
186
|
+
this.unsubConsolidateCheck = null;
|
|
187
|
+
this.consolidateScheduler = null;
|
|
188
|
+
this.reindexScheduler = null;
|
|
189
|
+
this.llmClient = null;
|
|
190
|
+
this.sessionMemoryCap = null;
|
|
191
|
+
this.turnPreflight = null;
|
|
192
|
+
this.isFirstTurn = false;
|
|
193
|
+
try {
|
|
194
|
+
await stopSidecar();
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
// ignore shutdown errors
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
export function createMemoryRuntime(opts) {
|
|
202
|
+
return new MemoryRuntimeImpl(opts);
|
|
203
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createMemoryRuntime } from "./createMemoryRuntime.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type ConsolidateScheduler } from "../consolidate/scheduler.js";
|
|
2
|
+
import type { LlmClient } from "../adapters/llm/types.js";
|
|
3
|
+
import { mergePrivateMemoryBlocks } from "../preflight/render.js";
|
|
4
|
+
import { type ReindexScheduler } from "../sidecar/reindexBridge.js";
|
|
5
|
+
import type { SidecarPaths } from "../sidecar/paths.js";
|
|
6
|
+
import type { MemoryStore } from "../store/memoryStore.js";
|
|
7
|
+
export type SidecarBootstrapResult = {
|
|
8
|
+
reindexScheduler: ReindexScheduler;
|
|
9
|
+
unsubSyncToSidecar: () => void;
|
|
10
|
+
};
|
|
11
|
+
export declare function bootstrapSidecar(opts: {
|
|
12
|
+
store: MemoryStore;
|
|
13
|
+
sidecarPaths: SidecarPaths;
|
|
14
|
+
reindexScheduler: ReindexScheduler | null;
|
|
15
|
+
}): Promise<SidecarBootstrapResult>;
|
|
16
|
+
export type ConsolidateBootstrapResult = {
|
|
17
|
+
consolidateScheduler: ConsolidateScheduler;
|
|
18
|
+
stopConsolidateInterval: () => void;
|
|
19
|
+
unsubConsolidateCheck: () => void;
|
|
20
|
+
};
|
|
21
|
+
export declare function bootstrapConsolidate(opts: {
|
|
22
|
+
store: MemoryStore;
|
|
23
|
+
getLlm: () => LlmClient | null;
|
|
24
|
+
onComplete: () => Promise<void>;
|
|
25
|
+
stopExistingInterval: (() => void) | null;
|
|
26
|
+
}): ConsolidateBootstrapResult;
|
|
27
|
+
export declare function loadSessionMemoryCap(store: MemoryStore): Promise<string | null>;
|
|
28
|
+
export { mergePrivateMemoryBlocks };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { readPiMemoryEnv } from "../config/index.js";
|
|
2
|
+
import { readPreflightRuntimeConfig } from "../config/preflight.js";
|
|
3
|
+
import { createConsolidateScheduler, startConsolidateInterval, } from "../consolidate/scheduler.js";
|
|
4
|
+
import { mergePrivateMemoryBlocks, renderMemoryCapPrivateMemory } from "../preflight/render.js";
|
|
5
|
+
import { createReindexScheduler } from "../sidecar/reindexBridge.js";
|
|
6
|
+
import { ensureSidecarRunning } from "../sidecar/sidecarManager.js";
|
|
7
|
+
import { warmSidecar } from "../sidecar/warmup.js";
|
|
8
|
+
export async function bootstrapSidecar(opts) {
|
|
9
|
+
await ensureSidecarRunning({
|
|
10
|
+
socketPath: opts.sidecarPaths.socketPath,
|
|
11
|
+
dbPath: opts.sidecarPaths.dbPath,
|
|
12
|
+
});
|
|
13
|
+
if (readPreflightRuntimeConfig().warmSidecar) {
|
|
14
|
+
try {
|
|
15
|
+
await warmSidecar(opts.sidecarPaths.socketPath);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// warm is best-effort
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const reindexScheduler = opts.reindexScheduler ??
|
|
22
|
+
createReindexScheduler({
|
|
23
|
+
sidecar: opts.sidecarPaths,
|
|
24
|
+
agentDir: opts.store.agentDir,
|
|
25
|
+
getDocuments: () => opts.store.exportForIndex(),
|
|
26
|
+
debounceMs: readPiMemoryEnv().reindexDebounceMs,
|
|
27
|
+
});
|
|
28
|
+
const unsubSyncToSidecar = opts.store.onSyncToSidecar(() => reindexScheduler.schedule());
|
|
29
|
+
await reindexScheduler.runNow();
|
|
30
|
+
return { reindexScheduler, unsubSyncToSidecar };
|
|
31
|
+
}
|
|
32
|
+
export function bootstrapConsolidate(opts) {
|
|
33
|
+
const consolidateScheduler = createConsolidateScheduler({
|
|
34
|
+
getStore: () => opts.store,
|
|
35
|
+
getAgentDir: () => opts.store.agentDir,
|
|
36
|
+
getLlm: opts.getLlm,
|
|
37
|
+
debounceMs: readPiMemoryEnv().consolidateDebounceMs,
|
|
38
|
+
onComplete: opts.onComplete,
|
|
39
|
+
});
|
|
40
|
+
const unsubConsolidateCheck = opts.store.onConsolidateCheck(() => consolidateScheduler.schedule());
|
|
41
|
+
opts.stopExistingInterval?.();
|
|
42
|
+
const stopConsolidateInterval = startConsolidateInterval(() => {
|
|
43
|
+
void consolidateScheduler.runNow();
|
|
44
|
+
});
|
|
45
|
+
void consolidateScheduler.runNow();
|
|
46
|
+
return { consolidateScheduler, stopConsolidateInterval, unsubConsolidateCheck };
|
|
47
|
+
}
|
|
48
|
+
export async function loadSessionMemoryCap(store) {
|
|
49
|
+
const fallback = await store.readForFallback();
|
|
50
|
+
return renderMemoryCapPrivateMemory(fallback) || null;
|
|
51
|
+
}
|
|
52
|
+
export { mergePrivateMemoryBlocks };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
|
+
export declare function getUserMessageText(message: AgentMessage): string | null;
|
|
3
|
+
export declare function setUserMessageText(message: AgentMessage, text: string): AgentMessage;
|
|
4
|
+
export declare function findLastUserMessageIndex(messages: AgentMessage[]): number;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function getUserMessageText(message) {
|
|
2
|
+
if (message.role !== "user")
|
|
3
|
+
return null;
|
|
4
|
+
if (typeof message.content === "string")
|
|
5
|
+
return message.content;
|
|
6
|
+
return message.content
|
|
7
|
+
.filter((block) => block.type === "text")
|
|
8
|
+
.map((block) => block.text)
|
|
9
|
+
.join("\n");
|
|
10
|
+
}
|
|
11
|
+
export function setUserMessageText(message, text) {
|
|
12
|
+
if (message.role !== "user")
|
|
13
|
+
return message;
|
|
14
|
+
return { ...message, content: text };
|
|
15
|
+
}
|
|
16
|
+
export function findLastUserMessageIndex(messages) {
|
|
17
|
+
return messages.findLastIndex((message) => message.role === "user");
|
|
18
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import type { ExtensionAPI, ExtensionContext, SessionShutdownEvent } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { LlmClient } from "../adapters/llm/types.js";
|
|
4
|
+
import type { SidecarPaths } from "../sidecar/paths.js";
|
|
5
|
+
import type { MemoryStore } from "../store/memoryStore.js";
|
|
6
|
+
export type TurnPreflight = {
|
|
7
|
+
userPayload: string;
|
|
8
|
+
privateContext: string;
|
|
9
|
+
};
|
|
10
|
+
export type MemoryRuntime = {
|
|
11
|
+
readonly store: MemoryStore;
|
|
12
|
+
readonly sidecarPaths: SidecarPaths;
|
|
13
|
+
readonly sessionId: string | null;
|
|
14
|
+
readonly isSubagent: boolean;
|
|
15
|
+
getLlm(): LlmClient | null;
|
|
16
|
+
getSessionMemoryCap(): string | null;
|
|
17
|
+
getTurnPreflight(): TurnPreflight | null;
|
|
18
|
+
bootstrap(ctx: ExtensionContext, pi: ExtensionAPI): Promise<void>;
|
|
19
|
+
refreshLlm(ctx: ExtensionContext, pi: ExtensionAPI): Promise<void>;
|
|
20
|
+
runBeforeAgentStart(event: {
|
|
21
|
+
prompt?: unknown;
|
|
22
|
+
}, ctx: ExtensionContext): Promise<void>;
|
|
23
|
+
runContext(event: {
|
|
24
|
+
messages: AgentMessage[];
|
|
25
|
+
}, ctx: ExtensionContext): Promise<{
|
|
26
|
+
messages: AgentMessage[];
|
|
27
|
+
} | undefined>;
|
|
28
|
+
shutdown(event: SessionShutdownEvent, ctx: ExtensionContext): Promise<void>;
|
|
29
|
+
reloadSessionMemoryCap(): Promise<void>;
|
|
30
|
+
dispose(): Promise<void>;
|
|
31
|
+
};
|
|
32
|
+
export type CreateMemoryRuntimeOptions = {
|
|
33
|
+
ctx: ExtensionContext;
|
|
34
|
+
agentDir?: string;
|
|
35
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
export { initializeMemoryWorkspace, type InitMemoryWorkspaceResult } from "./
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export { readPiMemoryEnv, resolveEmbedDim, type PiMemoryEnv } from "./config/env.js";
|
|
1
|
+
export { initializeMemoryWorkspace, type InitMemoryWorkspaceResult } from "./store/initWorkspace.js";
|
|
2
|
+
export { resolveAgentDirFromEnv, resolveMemoryAgentDir, type ResolveMemoryAgentDirOptions, } from "./config/agentDir.js";
|
|
3
|
+
export { createMemoryStore, MemoryStore } from "./store/memoryStore.js";
|
|
5
4
|
export { runConsolidateJob, type RunConsolidateJobResult } from "./consolidate/runJob.js";
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export { MemoryStore } from "./store/memoryStore.js";
|
|
9
|
-
export { ensureSidecarRunning, stopSidecar } from "./sidecar/sidecarManager.js";
|
|
10
|
-
export { query, reindex, ping } from "./sidecar/client.js";
|
|
5
|
+
export { mergeMemoryEntries, scheduleMergeMemoryEntriesInBackground } from "./consolidate/mergeMemoryEntries.js";
|
|
6
|
+
export { runDrainShutdownQueueJob, type RunDrainShutdownQueueResult, } from "./shutdown/runDrainJob.js";
|
|
11
7
|
export { createLlmClient, type LlmClient } from "./adapters/llm/index.js";
|
|
8
|
+
export { DEFAULT_MEMORY_FILE, MEMORY_SECTIONS, type MemorySection, } from "./constants/memory.js";
|
|
9
|
+
export { PI_MEMORY_DATA_SUBDIR, PI_MEMORY_ENV_FILE_NAME } from "./constants/paths.js";
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
export { initializeMemoryWorkspace } from "./
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export { readPiMemoryEnv, resolveEmbedDim } from "./config/env.js";
|
|
1
|
+
export { initializeMemoryWorkspace } from "./store/initWorkspace.js";
|
|
2
|
+
export { resolveAgentDirFromEnv, resolveMemoryAgentDir, } from "./config/agentDir.js";
|
|
3
|
+
export { createMemoryStore, MemoryStore } from "./store/memoryStore.js";
|
|
5
4
|
export { runConsolidateJob } from "./consolidate/runJob.js";
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export { MemoryStore } from "./store/memoryStore.js";
|
|
9
|
-
export { ensureSidecarRunning, stopSidecar } from "./sidecar/sidecarManager.js";
|
|
10
|
-
export { query, reindex, ping } from "./sidecar/client.js";
|
|
5
|
+
export { mergeMemoryEntries, scheduleMergeMemoryEntriesInBackground } from "./consolidate/mergeMemoryEntries.js";
|
|
6
|
+
export { runDrainShutdownQueueJob, } from "./shutdown/runDrainJob.js";
|
|
11
7
|
export { createLlmClient } from "./adapters/llm/index.js";
|
|
8
|
+
export { DEFAULT_MEMORY_FILE, MEMORY_SECTIONS, } from "./constants/memory.js";
|
|
9
|
+
export { PI_MEMORY_DATA_SUBDIR, PI_MEMORY_ENV_FILE_NAME } from "./constants/paths.js";
|