@howaboua/pi-notes-compaction 0.0.9 → 0.0.10
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/CHANGELOG.md +23 -5
- package/PCC-INTEGRATION.md +7 -98
- package/README.md +21 -13
- package/package.json +7 -3
- package/src/activation.ts +152 -0
- package/src/agent-identity.ts +119 -0
- package/src/attachment-snapshot.ts +103 -0
- package/src/binding-startup.ts +30 -0
- package/src/bridge.ts +1 -43
- package/src/compaction-projection.ts +88 -0
- package/src/context-owner.ts +103 -0
- package/src/context-sharing.ts +144 -0
- package/src/extension-hooks.ts +198 -0
- package/src/extension-sharing.ts +118 -0
- package/src/fresh-checkpoint.ts +117 -0
- package/src/fresh-notes.ts +141 -26
- package/src/history-search-protocol.ts +39 -0
- package/src/history-search-pruning.mjs +120 -0
- package/src/history-search-worker.mjs +256 -0
- package/src/history-search.ts +269 -0
- package/src/history.ts +212 -40
- package/src/idle-admission.ts +201 -0
- package/src/index.ts +71 -401
- package/src/integrations.ts +159 -0
- package/src/lifecycle.ts +63 -448
- package/src/note-save-marker.ts +112 -0
- package/src/notes-lookup.ts +112 -0
- package/src/notes.ts +24 -28
- package/src/query-executor.ts +190 -0
- package/src/remote.ts +357 -0
- package/src/rollover.ts +212 -0
- package/src/saved-session.ts +44 -7
- package/src/session-pruning-content.ts +237 -0
- package/src/session-pruning-plan.ts +243 -0
- package/src/session-pruning-policy.ts +20 -0
- package/src/session-pruning-screen.ts +130 -0
- package/src/session-pruning-source.ts +44 -0
- package/src/session-pruning-transaction.ts +141 -0
- package/src/session-pruning.ts +142 -0
- package/src/settings-command.ts +77 -0
- package/src/settings-screen.ts +84 -19
- package/src/settings.ts +143 -11
- package/src/shared-notes.ts +37 -14
- package/src/sharing-service.ts +348 -0
- package/src/store.ts +31 -0
- package/src/task-boundary.ts +6 -1
- package/src/tool-contracts.ts +279 -0
- package/src/tools.ts +19 -560
- package/src/tree-archive.ts +149 -0
- package/src/tree-checkpoint.ts +170 -0
- package/src/tree-handoff-read.ts +192 -0
- package/src/window-checkpoint.ts +105 -0
- package/src/windows.ts +25 -9
package/src/tools.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { StringEnum } from "@earendil-works/pi-ai";
|
|
2
1
|
import type {
|
|
3
2
|
AgentToolResult,
|
|
4
3
|
ExtensionAPI,
|
|
@@ -6,532 +5,27 @@ import type {
|
|
|
6
5
|
ToolDefinition,
|
|
7
6
|
} from "@earendil-works/pi-coding-agent";
|
|
8
7
|
import { Type } from "typebox";
|
|
9
|
-
import {
|
|
10
|
-
import type {
|
|
11
|
-
CachedResponse,
|
|
12
|
-
LookupQuery,
|
|
13
|
-
SerializedCachedResponse,
|
|
14
|
-
SharedNotesDetails,
|
|
15
|
-
} from "./bridge.js";
|
|
16
|
-
import { queryKey } from "./bridge.js";
|
|
17
|
-
import { useHistory } from "./history.js";
|
|
8
|
+
import type { LookupQuery, SharedNotesDetails } from "./bridge.js";
|
|
9
|
+
import type { HistorySearch } from "./history-search.js";
|
|
18
10
|
import type { NotesLifecycle } from "./lifecycle.js";
|
|
19
|
-
import {
|
|
20
|
-
CACHE_RECEIPT,
|
|
21
|
-
collectNotes,
|
|
22
|
-
normalizeNoteParams,
|
|
23
|
-
sessionRef,
|
|
24
|
-
sourceSessionIds,
|
|
25
|
-
useNotes,
|
|
26
|
-
} from "./notes.js";
|
|
27
|
-
import {
|
|
28
|
-
readSharedHistory,
|
|
29
|
-
readSharedNotes,
|
|
30
|
-
sharedNotesUnavailable,
|
|
31
|
-
} from "./shared-notes.js";
|
|
11
|
+
import { createQueryExecutor } from "./query-executor.js";
|
|
32
12
|
import type { NotesStore } from "./store.js";
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
] as const;
|
|
42
|
-
const HISTORY_ACTIONS = [
|
|
43
|
-
"help",
|
|
44
|
-
"list_windows",
|
|
45
|
-
"list_items",
|
|
46
|
-
"read_item",
|
|
47
|
-
"search_contents",
|
|
48
|
-
] as const;
|
|
49
|
-
export const NOTES_USAGE = "await tools.notes()";
|
|
50
|
-
export const HISTORY_USAGE = "await tools.history()";
|
|
51
|
-
const STRING = Type.Optional(Type.String());
|
|
52
|
-
const INTEGER = Type.Optional(Type.Integer({ minimum: 1 }));
|
|
53
|
-
const NULLABLE_STRING = Type.Optional(Type.Union([Type.String(), Type.Null()]));
|
|
54
|
-
const LINE = Type.Optional(Type.Union([Type.Integer(), Type.Null()]));
|
|
55
|
-
const NOTES_SCHEMA = Type.Object(
|
|
56
|
-
{
|
|
57
|
-
action: Type.Optional(StringEnum(NOTES_ACTIONS)),
|
|
58
|
-
path: STRING,
|
|
59
|
-
text: STRING,
|
|
60
|
-
prefix: NULLABLE_STRING,
|
|
61
|
-
query: STRING,
|
|
62
|
-
path_prefix: NULLABLE_STRING,
|
|
63
|
-
start_line: LINE,
|
|
64
|
-
stop_line: LINE,
|
|
65
|
-
max_results: INTEGER,
|
|
66
|
-
max_files: INTEGER,
|
|
67
|
-
max_matches_per_file: INTEGER,
|
|
68
|
-
file_order: Type.Optional(StringEnum(["ascending", "descending"])),
|
|
69
|
-
file_order_by: Type.Optional(
|
|
70
|
-
StringEnum(["name", "created_at", "updated_at"]),
|
|
71
|
-
),
|
|
72
|
-
recent_file_first: Type.Optional(Type.Boolean()),
|
|
73
|
-
},
|
|
74
|
-
{ additionalProperties: false },
|
|
75
|
-
);
|
|
76
|
-
const HISTORY_SCHEMA = Type.Object(
|
|
77
|
-
{
|
|
78
|
-
action: Type.Optional(StringEnum(HISTORY_ACTIONS)),
|
|
79
|
-
agent_name: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
80
|
-
item_id: STRING,
|
|
81
|
-
window_id: Type.Optional(
|
|
82
|
-
Type.Union([Type.String(), Type.Null()], {
|
|
83
|
-
description: "Required with item_id for read_item",
|
|
84
|
-
}),
|
|
85
|
-
),
|
|
86
|
-
query: STRING,
|
|
87
|
-
limit: INTEGER,
|
|
88
|
-
limit_chars: INTEGER,
|
|
89
|
-
max_chars_per_item: INTEGER,
|
|
90
|
-
offset_chars: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
91
|
-
recent_first: Type.Optional(Type.Boolean()),
|
|
92
|
-
role: Type.Optional(
|
|
93
|
-
Type.Union([
|
|
94
|
-
StringEnum(["user", "assistant", "tool", "system", "developer"]),
|
|
95
|
-
Type.Null(),
|
|
96
|
-
]),
|
|
97
|
-
),
|
|
98
|
-
tool_name: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
99
|
-
tool_namespace: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
100
|
-
},
|
|
101
|
-
{ additionalProperties: false },
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
const FIELDS: Record<string, readonly string[]> = {
|
|
105
|
-
list_files_by_prefix: [
|
|
106
|
-
"prefix",
|
|
107
|
-
"file_order",
|
|
108
|
-
"file_order_by",
|
|
109
|
-
"max_results",
|
|
110
|
-
],
|
|
111
|
-
read_file: ["path", "start_line", "stop_line"],
|
|
112
|
-
search_notes: [
|
|
113
|
-
"query",
|
|
114
|
-
"path_prefix",
|
|
115
|
-
"max_files",
|
|
116
|
-
"max_matches_per_file",
|
|
117
|
-
"recent_file_first",
|
|
118
|
-
],
|
|
119
|
-
append_to_file: ["path", "text"],
|
|
120
|
-
write_file: ["path", "text"],
|
|
121
|
-
list_windows: ["agent_name", "limit", "recent_first"],
|
|
122
|
-
list_items: [
|
|
123
|
-
"agent_name",
|
|
124
|
-
"window_id",
|
|
125
|
-
"role",
|
|
126
|
-
"tool_name",
|
|
127
|
-
"tool_namespace",
|
|
128
|
-
"limit",
|
|
129
|
-
"max_chars_per_item",
|
|
130
|
-
"recent_first",
|
|
131
|
-
],
|
|
132
|
-
read_item: [
|
|
133
|
-
"agent_name",
|
|
134
|
-
"window_id",
|
|
135
|
-
"item_id",
|
|
136
|
-
"offset_chars",
|
|
137
|
-
"limit_chars",
|
|
138
|
-
],
|
|
139
|
-
search_history: [
|
|
140
|
-
"agent_name",
|
|
141
|
-
"window_id",
|
|
142
|
-
"role",
|
|
143
|
-
"tool_name",
|
|
144
|
-
"tool_namespace",
|
|
145
|
-
"query",
|
|
146
|
-
"limit",
|
|
147
|
-
"recent_first",
|
|
148
|
-
],
|
|
149
|
-
};
|
|
150
|
-
function relevant(
|
|
151
|
-
namespace: LookupQuery["namespace"],
|
|
152
|
-
input: unknown,
|
|
153
|
-
): Record<string, unknown> {
|
|
154
|
-
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
155
|
-
throw new Error("Supply an action object");
|
|
156
|
-
const params = input as Record<string, unknown>;
|
|
157
|
-
const action = params["action"];
|
|
158
|
-
const key =
|
|
159
|
-
action === "search_contents" ? `search_${namespace}` : String(action);
|
|
160
|
-
const fields = FIELDS[key];
|
|
161
|
-
if (!fields)
|
|
162
|
-
throw new Error(
|
|
163
|
-
namespace === "notes"
|
|
164
|
-
? "notes requires a supported action"
|
|
165
|
-
: "Unknown action",
|
|
166
|
-
);
|
|
167
|
-
if (namespace === "notes") {
|
|
168
|
-
for (const field of Object.keys(params))
|
|
169
|
-
if (field !== "action" && !fields.includes(field))
|
|
170
|
-
throw new Error(`notes ${String(action)} does not accept ${field}`);
|
|
171
|
-
if (
|
|
172
|
-
["read_file", "write_file", "append_to_file"].includes(String(action)) &&
|
|
173
|
-
(typeof params["path"] !== "string" || !params["path"])
|
|
174
|
-
)
|
|
175
|
-
throw new Error(`notes ${String(action)} requires path`);
|
|
176
|
-
if (
|
|
177
|
-
action === "search_contents" &&
|
|
178
|
-
(typeof params["query"] !== "string" || !params["query"])
|
|
179
|
-
)
|
|
180
|
-
throw new Error("notes search_contents requires query");
|
|
181
|
-
if (
|
|
182
|
-
["write_file", "append_to_file"].includes(String(action)) &&
|
|
183
|
-
typeof params["text"] !== "string"
|
|
184
|
-
)
|
|
185
|
-
throw new Error(`notes ${String(action)} requires text`);
|
|
186
|
-
}
|
|
187
|
-
return Object.fromEntries(
|
|
188
|
-
Object.entries(params).filter(
|
|
189
|
-
([key]) => key === "action" || fields.includes(key),
|
|
190
|
-
),
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function result(payload: unknown): AgentToolResult<unknown> {
|
|
195
|
-
return {
|
|
196
|
-
content: [{ type: "text", text: JSON.stringify(payload) }],
|
|
197
|
-
details: {},
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function discovery(
|
|
202
|
-
namespace: LookupQuery["namespace"],
|
|
203
|
-
input: unknown,
|
|
204
|
-
): AgentToolResult<unknown> | undefined {
|
|
205
|
-
const params = input === undefined ? {} : input;
|
|
206
|
-
if (!params || typeof params !== "object" || Array.isArray(params))
|
|
207
|
-
throw new Error("Supply an action object, or omit arguments for help");
|
|
208
|
-
const entries = Object.entries(params);
|
|
209
|
-
if (
|
|
210
|
-
!entries.length ||
|
|
211
|
-
(params as Record<string, unknown>)["action"] === "help"
|
|
212
|
-
) {
|
|
213
|
-
if (entries.some(([key]) => key !== "action"))
|
|
214
|
-
throw new Error("Help accepts only action: help");
|
|
215
|
-
const actions = namespace === "notes" ? NOTES_ACTIONS : HISTORY_ACTIONS;
|
|
216
|
-
return result({
|
|
217
|
-
actions: Object.fromEntries(
|
|
218
|
-
actions.map((action) => [
|
|
219
|
-
action,
|
|
220
|
-
action === "help"
|
|
221
|
-
? ""
|
|
222
|
-
: FIELDS[
|
|
223
|
-
action === "search_contents" ? `search_${namespace}` : action
|
|
224
|
-
]!.map((field) =>
|
|
225
|
-
["path", "text", "query", "item_id"].includes(field) ||
|
|
226
|
-
(action === "read_item" && field === "window_id")
|
|
227
|
-
? field
|
|
228
|
-
: `${field}?`,
|
|
229
|
-
).join(" "),
|
|
230
|
-
]),
|
|
231
|
-
),
|
|
232
|
-
...(namespace === "notes"
|
|
233
|
-
? {
|
|
234
|
-
writes: "write_file replaces; append_to_file adds",
|
|
235
|
-
paths:
|
|
236
|
-
"Relative uses current agent; cross-agent uses <agent>/notes[/path]",
|
|
237
|
-
usage: '{ "action": "read_file", "path": "checkpoint" }',
|
|
238
|
-
ranges:
|
|
239
|
-
"start_line/stop_line inclusive, 1-based; negative counts from end; 0/null/omitted uses first/last line; out-of-range clamps; reversed range returns empty content",
|
|
240
|
-
defaults:
|
|
241
|
-
"list max_results 20; search max_files/max_matches_per_file 20; limits clamp to 1..100; list name ascending; search name ascending, recent_file_first sorts created_at descending",
|
|
242
|
-
}
|
|
243
|
-
: {
|
|
244
|
-
usage:
|
|
245
|
-
"Search earlier conversation, don't browse it. Pass returned IDs unchanged",
|
|
246
|
-
}),
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
return undefined;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
async function hybrid(
|
|
253
|
-
pi: ExtensionAPI,
|
|
254
|
-
store: NotesStore,
|
|
255
|
-
lifecycle: NotesLifecycle,
|
|
256
|
-
query: LookupQuery,
|
|
257
|
-
local: Record<string, unknown>,
|
|
258
|
-
callId: string,
|
|
259
|
-
ctx: ExtensionContext,
|
|
260
|
-
signal?: AbortSignal,
|
|
261
|
-
shared = false,
|
|
262
|
-
): Promise<AgentToolResult<unknown>> {
|
|
263
|
-
let responses: readonly CachedResponse[] = store.cached(
|
|
264
|
-
sourceSessionIds(ctx),
|
|
265
|
-
query,
|
|
266
|
-
);
|
|
267
|
-
let state = responses.length ? "cache" : "unavailable";
|
|
268
|
-
let lookupFailed = false;
|
|
269
|
-
const bridge = lifecycle.bridge;
|
|
270
|
-
if (bridge?.lookup) {
|
|
271
|
-
let fetched: readonly CachedResponse[] | undefined;
|
|
272
|
-
try {
|
|
273
|
-
fetched = await bridge.lookup(query, ctx, signal);
|
|
274
|
-
} catch (error) {
|
|
275
|
-
if (signal?.aborted) throw error;
|
|
276
|
-
lookupFailed = true;
|
|
277
|
-
}
|
|
278
|
-
if (signal?.aborted) throw new Error("Lookup cancelled");
|
|
279
|
-
if (fetched) {
|
|
280
|
-
try {
|
|
281
|
-
store.cache(sessionRef(ctx), fetched);
|
|
282
|
-
} catch {
|
|
283
|
-
throw new Error(
|
|
284
|
-
"Remote results could not be retained locally. Retry this lookup when local storage is available.",
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
if (fetched.length)
|
|
288
|
-
pi.appendEntry(CACHE_RECEIPT, {
|
|
289
|
-
protocol: 1,
|
|
290
|
-
sessionId: ctx.sessionManager.getSessionId(),
|
|
291
|
-
});
|
|
292
|
-
const exact = fetched.filter(
|
|
293
|
-
(response) => queryKey(response.query) === queryKey(query),
|
|
294
|
-
);
|
|
295
|
-
if (exact.length) {
|
|
296
|
-
responses = exact;
|
|
297
|
-
state = "live";
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
const canProject =
|
|
302
|
-
shared ||
|
|
303
|
-
(Boolean(bridge?.projectResult) && bridge?.canProject?.(ctx) !== false);
|
|
304
|
-
if (responses.length && !canProject) state = "incompatible";
|
|
305
|
-
const remote = {
|
|
306
|
-
state,
|
|
307
|
-
...(lookupFailed
|
|
308
|
-
? {
|
|
309
|
-
lookup: "failed",
|
|
310
|
-
recovery: "Retry this lookup when remote access is available",
|
|
311
|
-
}
|
|
312
|
-
: {}),
|
|
313
|
-
coverage: responses.length
|
|
314
|
-
? responses.map((response) => ({
|
|
315
|
-
source: response.source,
|
|
316
|
-
fetched_at: response.fetchedAt,
|
|
317
|
-
coverage: response.coverage,
|
|
318
|
-
}))
|
|
319
|
-
: "No cached response for this exact query",
|
|
320
|
-
...(state === "cache"
|
|
321
|
-
? { scope: "Exact cached query only, not a complete offline archive" }
|
|
322
|
-
: {}),
|
|
323
|
-
...(state === "incompatible"
|
|
324
|
-
? {
|
|
325
|
-
recovery:
|
|
326
|
-
"Cached encrypted contents are unavailable on this model. Local notes remain available",
|
|
327
|
-
}
|
|
328
|
-
: {}),
|
|
329
|
-
};
|
|
330
|
-
const output = result({ source: "local", local, remote });
|
|
331
|
-
if (shared)
|
|
332
|
-
return {
|
|
333
|
-
...output,
|
|
334
|
-
details: {
|
|
335
|
-
codexHistoryNotes: {},
|
|
336
|
-
externalNotesResponses: responses.map(
|
|
337
|
-
({ bytes, ...response }): SerializedCachedResponse => ({
|
|
338
|
-
...response,
|
|
339
|
-
bytesBase64: Buffer.from(bytes).toString("base64"),
|
|
340
|
-
}),
|
|
341
|
-
),
|
|
342
|
-
},
|
|
343
|
-
};
|
|
344
|
-
if (responses.length && canProject && bridge?.projectResult) {
|
|
345
|
-
try {
|
|
346
|
-
return await bridge.projectResult(output, responses, callId, ctx, signal);
|
|
347
|
-
} catch {
|
|
348
|
-
throw new Error(
|
|
349
|
-
"Remote contents were cached, but delivery failed. Retry this lookup on a compatible model.",
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
return output;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
function createQueryExecutor(
|
|
357
|
-
pi: ExtensionAPI,
|
|
358
|
-
store: NotesStore,
|
|
359
|
-
lifecycle: NotesLifecycle,
|
|
360
|
-
shared = false,
|
|
361
|
-
) {
|
|
362
|
-
const assertActive = () => {
|
|
363
|
-
if (!lifecycle.active)
|
|
364
|
-
throw new Error(
|
|
365
|
-
"Notes tools are inactive. Ask the user to check /notes.",
|
|
366
|
-
);
|
|
367
|
-
};
|
|
368
|
-
async function notes(
|
|
369
|
-
callId: string,
|
|
370
|
-
input: unknown,
|
|
371
|
-
signal: AbortSignal | undefined,
|
|
372
|
-
ctx: ExtensionContext,
|
|
373
|
-
): Promise<AgentToolResult<unknown>> {
|
|
374
|
-
const help = discovery("notes", input);
|
|
375
|
-
if (help) return help;
|
|
376
|
-
assertActive();
|
|
377
|
-
const raw = relevant("notes", input);
|
|
378
|
-
if (!shared) {
|
|
379
|
-
try {
|
|
380
|
-
const routed = await lifecycle.bridge?.route?.(
|
|
381
|
-
{ namespace: "notes", params: raw },
|
|
382
|
-
callId,
|
|
383
|
-
ctx,
|
|
384
|
-
signal,
|
|
385
|
-
);
|
|
386
|
-
if (routed) return routed;
|
|
387
|
-
} catch (error) {
|
|
388
|
-
signal?.throwIfAborted();
|
|
389
|
-
if (!sharedNotesUnavailable(error)) throw error;
|
|
390
|
-
const saved = await readSharedNotes(ctx, store, raw, signal);
|
|
391
|
-
if (saved) return result(saved);
|
|
392
|
-
throw error;
|
|
393
|
-
}
|
|
394
|
-
const saved = await readSharedNotes(ctx, store, raw, signal);
|
|
395
|
-
if (saved) return result(saved);
|
|
396
|
-
}
|
|
397
|
-
const params = relevant(
|
|
398
|
-
"notes",
|
|
399
|
-
normalizeNoteParams(raw, lifecycle.agent(ctx)),
|
|
400
|
-
);
|
|
401
|
-
if (signal?.aborted) throw new Error("Note operation cancelled");
|
|
402
|
-
const local = useNotes(
|
|
403
|
-
pi,
|
|
404
|
-
store,
|
|
405
|
-
params,
|
|
406
|
-
ctx,
|
|
407
|
-
lifecycle.agent(ctx),
|
|
408
|
-
lifecycle.windows.current?.currentWindowId ?? "",
|
|
409
|
-
lifecycle.runId,
|
|
410
|
-
shared,
|
|
411
|
-
);
|
|
412
|
-
if (
|
|
413
|
-
params["action"] === "write_file" ||
|
|
414
|
-
params["action"] === "append_to_file"
|
|
415
|
-
)
|
|
416
|
-
return {
|
|
417
|
-
...result(local),
|
|
418
|
-
details: {
|
|
419
|
-
...(shared
|
|
420
|
-
? { codexHistoryNotes: {}, notesCompactionRouted: true }
|
|
421
|
-
: {
|
|
422
|
-
notesCompaction: {
|
|
423
|
-
protocol: 1,
|
|
424
|
-
saved: true,
|
|
425
|
-
runId: lifecycle.runId,
|
|
426
|
-
},
|
|
427
|
-
}),
|
|
428
|
-
},
|
|
429
|
-
};
|
|
430
|
-
const notes = collectNotes(store, ctx.sessionManager.getBranch());
|
|
431
|
-
if (
|
|
432
|
-
params["action"] === "read_file" &&
|
|
433
|
-
notes.get(params["path"] as string)?.mode === "replace"
|
|
434
|
-
)
|
|
435
|
-
return result({
|
|
436
|
-
source: "local",
|
|
437
|
-
local,
|
|
438
|
-
remote: { state: "superseded_by_local_replacement" },
|
|
439
|
-
});
|
|
440
|
-
const replacements = [...notes.values()]
|
|
441
|
-
.filter((note) => note.mode === "replace")
|
|
442
|
-
.map((note) => `${lifecycle.agent(ctx)}/notes/${note.path}`);
|
|
443
|
-
const remoteParams = { ...params };
|
|
444
|
-
for (const field of ["path", "prefix", "path_prefix"])
|
|
445
|
-
if (typeof remoteParams[field] === "string")
|
|
446
|
-
remoteParams[field] =
|
|
447
|
-
`${lifecycle.agent(ctx)}/notes${remoteParams[field] ? `/${remoteParams[field]}` : ""}`;
|
|
448
|
-
return hybrid(
|
|
449
|
-
pi,
|
|
450
|
-
store,
|
|
451
|
-
lifecycle,
|
|
452
|
-
{ namespace: "notes", params: remoteParams },
|
|
453
|
-
{
|
|
454
|
-
...local,
|
|
455
|
-
...(replacements.length
|
|
456
|
-
? {
|
|
457
|
-
local_replacements_authoritative: replacements.slice(0, 100),
|
|
458
|
-
replacements_truncated: replacements.length > 100,
|
|
459
|
-
}
|
|
460
|
-
: {}),
|
|
461
|
-
},
|
|
462
|
-
callId,
|
|
463
|
-
ctx,
|
|
464
|
-
signal,
|
|
465
|
-
shared,
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
async function history(
|
|
469
|
-
callId: string,
|
|
470
|
-
input: unknown,
|
|
471
|
-
signal: AbortSignal | undefined,
|
|
472
|
-
ctx: ExtensionContext,
|
|
473
|
-
): Promise<AgentToolResult<unknown>> {
|
|
474
|
-
const help = discovery("history", input);
|
|
475
|
-
if (help) return help;
|
|
476
|
-
assertActive();
|
|
477
|
-
const params = relevant("history", input);
|
|
478
|
-
const query = { namespace: "history", params } as const;
|
|
479
|
-
if (!shared) {
|
|
480
|
-
try {
|
|
481
|
-
const routed = await lifecycle.bridge?.route?.(
|
|
482
|
-
query,
|
|
483
|
-
callId,
|
|
484
|
-
ctx,
|
|
485
|
-
signal,
|
|
486
|
-
);
|
|
487
|
-
if (routed) return routed;
|
|
488
|
-
} catch (error) {
|
|
489
|
-
signal?.throwIfAborted();
|
|
490
|
-
if (
|
|
491
|
-
!sharedNotesUnavailable(error) &&
|
|
492
|
-
!(
|
|
493
|
-
error instanceof Error &&
|
|
494
|
-
error.message ===
|
|
495
|
-
"Counterpart history requires its live attached owner; native history is unchanged"
|
|
496
|
-
)
|
|
497
|
-
)
|
|
498
|
-
throw error;
|
|
499
|
-
const saved = await readSharedHistory(ctx, store, params, signal);
|
|
500
|
-
if (saved) return result(saved);
|
|
501
|
-
throw error;
|
|
502
|
-
}
|
|
503
|
-
const saved = await readSharedHistory(ctx, store, params, signal);
|
|
504
|
-
if (saved) return result(saved);
|
|
505
|
-
}
|
|
506
|
-
if (params["agent_name"] && params["agent_name"] !== lifecycle.agent(ctx))
|
|
507
|
-
throw new Error(
|
|
508
|
-
"That agent's history is not available here. Omit agent_name to search this session.",
|
|
509
|
-
);
|
|
510
|
-
return hybrid(
|
|
511
|
-
pi,
|
|
512
|
-
store,
|
|
513
|
-
lifecycle,
|
|
514
|
-
query,
|
|
515
|
-
useHistory(
|
|
516
|
-
ctx.sessionManager.getBranch(),
|
|
517
|
-
params,
|
|
518
|
-
ctx.sessionManager.getSessionId(),
|
|
519
|
-
),
|
|
520
|
-
callId,
|
|
521
|
-
ctx,
|
|
522
|
-
signal,
|
|
523
|
-
shared,
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
return { notes, history };
|
|
527
|
-
}
|
|
13
|
+
import {
|
|
14
|
+
HISTORY_SCHEMA,
|
|
15
|
+
NEW_CONTEXT_DESCRIPTION,
|
|
16
|
+
NOTES_DESCRIPTION,
|
|
17
|
+
NOTES_SCHEMA,
|
|
18
|
+
prepareNotesArguments,
|
|
19
|
+
result,
|
|
20
|
+
} from "./tool-contracts.js";
|
|
528
21
|
|
|
529
22
|
export function createSharedExecutor(
|
|
530
23
|
pi: ExtensionAPI,
|
|
531
24
|
store: NotesStore,
|
|
532
25
|
lifecycle: NotesLifecycle,
|
|
26
|
+
search: HistorySearch,
|
|
533
27
|
) {
|
|
534
|
-
const queries = createQueryExecutor(pi, store, lifecycle, true);
|
|
28
|
+
const queries = createQueryExecutor(pi, store, lifecycle, search, true);
|
|
535
29
|
return async (
|
|
536
30
|
query: LookupQuery,
|
|
537
31
|
callId: string,
|
|
@@ -555,6 +49,7 @@ export function createTools(
|
|
|
555
49
|
pi: ExtensionAPI,
|
|
556
50
|
store: NotesStore,
|
|
557
51
|
lifecycle: NotesLifecycle,
|
|
52
|
+
search: HistorySearch,
|
|
558
53
|
): ToolDefinition[] {
|
|
559
54
|
const assertActive = () => {
|
|
560
55
|
if (!lifecycle.active)
|
|
@@ -562,50 +57,15 @@ export function createTools(
|
|
|
562
57
|
"Notes tools are inactive. Ask the user to check /notes.",
|
|
563
58
|
);
|
|
564
59
|
};
|
|
565
|
-
const queries = createQueryExecutor(pi, store, lifecycle);
|
|
60
|
+
const queries = createQueryExecutor(pi, store, lifecycle, search);
|
|
566
61
|
return [
|
|
567
62
|
{
|
|
568
63
|
name: "notes",
|
|
569
64
|
label: "notes",
|
|
570
|
-
description:
|
|
65
|
+
description: NOTES_DESCRIPTION,
|
|
571
66
|
parameters: NOTES_SCHEMA,
|
|
572
67
|
executionMode: "sequential",
|
|
573
|
-
prepareArguments
|
|
574
|
-
if (args === undefined) return {};
|
|
575
|
-
if (!args || typeof args !== "object" || Array.isArray(args))
|
|
576
|
-
throw new Error(
|
|
577
|
-
'Supply an action object, e.g. { "action": "read_file", "path": "checkpoint" }. Use {} for help',
|
|
578
|
-
);
|
|
579
|
-
const params = { ...args } as Record<string, unknown>;
|
|
580
|
-
if (
|
|
581
|
-
(params["action"] === "write_file" ||
|
|
582
|
-
params["action"] === "append_to_file") &&
|
|
583
|
-
"content" in params
|
|
584
|
-
) {
|
|
585
|
-
if (!("text" in params)) params["text"] = params["content"];
|
|
586
|
-
delete params["content"];
|
|
587
|
-
}
|
|
588
|
-
if (
|
|
589
|
-
params["action"] === "list_files_by_prefix" &&
|
|
590
|
-
params["max_files"] !== undefined
|
|
591
|
-
) {
|
|
592
|
-
if (!("max_results" in params))
|
|
593
|
-
params["max_results"] = params["max_files"];
|
|
594
|
-
delete params["max_files"];
|
|
595
|
-
}
|
|
596
|
-
if (!discovery("notes", params)) relevant("notes", params);
|
|
597
|
-
for (const [field, value] of Object.entries(params)) {
|
|
598
|
-
const schema =
|
|
599
|
-
NOTES_SCHEMA.properties[
|
|
600
|
-
field as keyof typeof NOTES_SCHEMA.properties
|
|
601
|
-
];
|
|
602
|
-
if (!schema || !Check(schema, value))
|
|
603
|
-
throw new Error(
|
|
604
|
-
`Invalid notes ${field}; use {} for exact arguments`,
|
|
605
|
-
);
|
|
606
|
-
}
|
|
607
|
-
return params;
|
|
608
|
-
},
|
|
68
|
+
prepareArguments: prepareNotesArguments,
|
|
609
69
|
execute: (callId, input, signal, _update, ctx) =>
|
|
610
70
|
queries.notes(callId, input, signal, ctx),
|
|
611
71
|
},
|
|
@@ -623,8 +83,7 @@ export function createTools(
|
|
|
623
83
|
{
|
|
624
84
|
name: "new_context",
|
|
625
85
|
label: "new_context",
|
|
626
|
-
description:
|
|
627
|
-
"Start fresh from saved notes; recover earlier conversation through history",
|
|
86
|
+
description: NEW_CONTEXT_DESCRIPTION,
|
|
628
87
|
parameters: Type.Object({}, { additionalProperties: false }),
|
|
629
88
|
exposure: "model-only",
|
|
630
89
|
executionMode: "sequential",
|
|
@@ -655,7 +114,7 @@ export function createTools(
|
|
|
655
114
|
assertActive();
|
|
656
115
|
const remaining = lifecycle.windows.remaining(ctx);
|
|
657
116
|
return result({
|
|
658
|
-
|
|
117
|
+
contextWindow: remaining.contextWindow,
|
|
659
118
|
remainingTokens: remaining.remainingTokens ?? null,
|
|
660
119
|
remainingPercent: remaining.remainingPercent ?? null,
|
|
661
120
|
});
|