@howaboua/pi-notes-compaction 0.0.11 → 0.0.12
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 +7 -0
- package/README.md +28 -0
- package/package.json +3 -3
- package/src/attachment-sharing.ts +134 -0
- package/src/context-owner.ts +15 -19
- package/src/extension-hooks.ts +2 -2
- package/src/fresh-notes.ts +5 -11
- package/src/history-search.ts +1 -1
- package/src/history-windows.ts +223 -0
- package/src/history.ts +4 -222
- package/src/index.ts +27 -2
- package/src/integrations.ts +3 -1
- package/src/model-role-config.ts +99 -0
- package/src/model-role-editor.ts +40 -0
- package/src/model-role-screen.ts +58 -0
- package/src/model-roles.ts +363 -0
- package/src/note-query.ts +150 -0
- package/src/notes-lookup.ts +1 -1
- package/src/notes.ts +3 -145
- package/src/query-executor.ts +10 -2
- package/src/query-input.ts +115 -0
- package/src/saved-sharing-records.ts +103 -0
- package/src/settings-command.ts +39 -8
- package/src/settings-controls.ts +209 -0
- package/src/settings-screen.ts +23 -211
- package/src/shared-notes.ts +20 -203
- package/src/shared-session-reader.ts +132 -0
- package/src/sharing-service.ts +2 -116
- package/src/tool-contracts.ts +121 -80
- package/src/tools.ts +46 -6
- package/src/window-message.ts +179 -0
- package/src/windows.ts +13 -172
package/src/settings-screen.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
1
2
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import {
|
|
6
|
-
SettingsList,
|
|
3
|
+
Key,
|
|
4
|
+
matchesKey,
|
|
7
5
|
truncateToWidth,
|
|
8
6
|
wrapTextWithAnsi,
|
|
9
7
|
} from "@earendil-works/pi-tui";
|
|
@@ -11,12 +9,10 @@ import {
|
|
|
11
9
|
openSessionPruningSettings,
|
|
12
10
|
type SessionPruningSettings,
|
|
13
11
|
} from "./session-pruning-screen.js";
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
} from "./settings.js";
|
|
19
|
-
import { TASK_BOUNDARY_PRESETS, taskBoundaryLabel } from "./task-boundary.js";
|
|
12
|
+
import type { NotesSettings } from "./settings.js";
|
|
13
|
+
import { createNotesSettingsControls } from "./settings-controls.js";
|
|
14
|
+
|
|
15
|
+
type ScreenAction = "close" | "prune" | "sessionPruning" | "roles";
|
|
20
16
|
|
|
21
17
|
export async function openNotesSettings(
|
|
22
18
|
ctx: ExtensionContext,
|
|
@@ -28,204 +24,12 @@ export async function openNotesSettings(
|
|
|
28
24
|
prune: () => Promise<void>;
|
|
29
25
|
sessionPruning: SessionPruningSettings;
|
|
30
26
|
},
|
|
31
|
-
): Promise<
|
|
27
|
+
): Promise<"roles" | undefined> {
|
|
32
28
|
while (true) {
|
|
33
|
-
const action = await ctx.ui.custom<
|
|
29
|
+
const action = await ctx.ui.custom<ScreenAction>(
|
|
34
30
|
(tui, theme, _kb, done) => {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
minutes === 0
|
|
38
|
-
? "Off"
|
|
39
|
-
: minutes < 60
|
|
40
|
-
? `${minutes}m`
|
|
41
|
-
: `${Math.ceil(minutes / 60)}h`;
|
|
42
|
-
const list = new SettingsList(
|
|
43
|
-
[
|
|
44
|
-
{
|
|
45
|
-
id: "sessionPruning",
|
|
46
|
-
label: "Session auto-pruning",
|
|
47
|
-
currentValue: "select…",
|
|
48
|
-
values: ["select…", "settings…"],
|
|
49
|
-
description:
|
|
50
|
-
"Per-session history limits and Run now. Memory savings require restarting Pi.",
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
id: "active",
|
|
54
|
-
label: "Active",
|
|
55
|
-
description: `Whitelist matches exact model IDs from any provider. Edit modelWhitelist in ${options.settingsPath}, then reload Pi. Example: ["gpt-6-luna", "gpt-6.1-sol"].`,
|
|
56
|
-
currentValue:
|
|
57
|
-
settings.active === "all" ? "All models" : "Whitelist",
|
|
58
|
-
values: ["All models", "Whitelist"],
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
id: "sharing",
|
|
62
|
-
label: "Share subagent context",
|
|
63
|
-
description:
|
|
64
|
-
"Let new child agents share notes and history. Existing families remain connected when off.",
|
|
65
|
-
currentValue: settings.shareSubagentContext ? "on" : "off",
|
|
66
|
-
values: ["off", "on"],
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
id: "rankedHistorySearch",
|
|
70
|
-
label: "Ranked history search",
|
|
71
|
-
description:
|
|
72
|
-
"BM25 for earlier user messages and assistant replies. Skips current context and tool activity.",
|
|
73
|
-
currentValue: settings.rankedHistorySearch ? "on" : "off",
|
|
74
|
-
values: ["off", "on"],
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
id: "noteSaveMarkers",
|
|
78
|
-
label: "Note-save markers",
|
|
79
|
-
currentValue: settings.noteSaveMarkers ? "on" : "off",
|
|
80
|
-
values: ["off", "on"],
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
id: "compact",
|
|
84
|
-
label: "Normal compaction",
|
|
85
|
-
description:
|
|
86
|
-
"Use Pi/PCC compaction for /compact and new_context. Off uses notes-only rollover; /compact then waits for your next prompt.",
|
|
87
|
-
currentValue: settings.normalCompaction ? "on" : "off",
|
|
88
|
-
values: ["off", "on"],
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
id: "newWindowRequests",
|
|
92
|
-
label: "New window requests",
|
|
93
|
-
description:
|
|
94
|
-
"Off disables new_context. Pi-limits keeps the current window when Pi finds too little history to compact. Unrestricted accepts requests at any size. Normal compaction still chooses how windows roll over. Manual /compact and idle rollover are separate.",
|
|
95
|
-
currentValue: settings.newWindowRequests,
|
|
96
|
-
values: [...NEW_WINDOW_REQUESTS],
|
|
97
|
-
},
|
|
98
|
-
{
|
|
99
|
-
id: "taskBoundaryReminder",
|
|
100
|
-
label: "Task-boundary reminder",
|
|
101
|
-
description:
|
|
102
|
-
"Suggest a separate-task rollover after this many used tokens, only with active capacity above 272k. Custom: edit taskBoundaryReminder in the notes settings JSON (whole tokens); 0 is off.",
|
|
103
|
-
currentValue: taskBoundaryLabel(settings.taskBoundaryReminder),
|
|
104
|
-
values: [
|
|
105
|
-
...TASK_BOUNDARY_PRESETS.map(taskBoundaryLabel),
|
|
106
|
-
"custom (edit JSON)",
|
|
107
|
-
],
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
id: "idle",
|
|
111
|
-
label: "Idle rollover",
|
|
112
|
-
description:
|
|
113
|
-
"Roll over before the next input only with fresh notes from a completed turn. Not a cache-expiry guarantee.",
|
|
114
|
-
currentValue: idleValue(settings.idleMinutes),
|
|
115
|
-
values: IDLE_MINUTES.map(idleValue),
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
id: "prune",
|
|
119
|
-
label: "Prune missing sessions",
|
|
120
|
-
description:
|
|
121
|
-
"Remove notes, cached responses and history index entries for missing session files. Keeps the active session, unknown paths and inaccessible files. Confirmation required.",
|
|
122
|
-
currentValue: "select…",
|
|
123
|
-
values: ["select…", "prune…"],
|
|
124
|
-
},
|
|
125
|
-
],
|
|
126
|
-
5,
|
|
127
|
-
getSettingsListTheme(),
|
|
128
|
-
(id, value) => {
|
|
129
|
-
if (id === "sessionPruning") {
|
|
130
|
-
done("sessionPruning");
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
if (id === "prune") {
|
|
134
|
-
done("prune");
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const current = options.settings();
|
|
138
|
-
if (
|
|
139
|
-
id === "taskBoundaryReminder" &&
|
|
140
|
-
value === "custom (edit JSON)"
|
|
141
|
-
) {
|
|
142
|
-
ctx.ui.notify(
|
|
143
|
-
`Set taskBoundaryReminder to a positive whole token count in ${options.settingsPath}, then reload Pi. 0 is off.`,
|
|
144
|
-
"info",
|
|
145
|
-
);
|
|
146
|
-
list.updateValue(
|
|
147
|
-
id,
|
|
148
|
-
taskBoundaryLabel(current.taskBoundaryReminder),
|
|
149
|
-
);
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
try {
|
|
153
|
-
if (id === "active")
|
|
154
|
-
options.save({
|
|
155
|
-
...current,
|
|
156
|
-
active: value === "All models" ? "all" : "whitelist",
|
|
157
|
-
});
|
|
158
|
-
else if (id === "sharing")
|
|
159
|
-
options.save({
|
|
160
|
-
...current,
|
|
161
|
-
shareSubagentContext: value === "on",
|
|
162
|
-
});
|
|
163
|
-
else if (id === "noteSaveMarkers")
|
|
164
|
-
options.save({ ...current, noteSaveMarkers: value === "on" });
|
|
165
|
-
else if (id === "rankedHistorySearch")
|
|
166
|
-
options.save({
|
|
167
|
-
...current,
|
|
168
|
-
rankedHistorySearch: value === "on",
|
|
169
|
-
});
|
|
170
|
-
else if (id === "compact")
|
|
171
|
-
options.save({ ...current, normalCompaction: value === "on" });
|
|
172
|
-
else if (id === "newWindowRequests") {
|
|
173
|
-
const newWindowRequests = NEW_WINDOW_REQUESTS.find(
|
|
174
|
-
(mode) => mode === value,
|
|
175
|
-
);
|
|
176
|
-
if (newWindowRequests)
|
|
177
|
-
options.save({ ...current, newWindowRequests });
|
|
178
|
-
} else if (id === "taskBoundaryReminder")
|
|
179
|
-
options.save({
|
|
180
|
-
...current,
|
|
181
|
-
taskBoundaryReminder:
|
|
182
|
-
TASK_BOUNDARY_PRESETS.find(
|
|
183
|
-
(tokens) => taskBoundaryLabel(tokens) === value,
|
|
184
|
-
) ?? current.taskBoundaryReminder,
|
|
185
|
-
});
|
|
186
|
-
else {
|
|
187
|
-
const idleMinutes = IDLE_MINUTES.find(
|
|
188
|
-
(minutes) => idleValue(minutes) === value,
|
|
189
|
-
);
|
|
190
|
-
if (idleMinutes !== undefined)
|
|
191
|
-
options.save({ ...current, idleMinutes });
|
|
192
|
-
}
|
|
193
|
-
} catch (error) {
|
|
194
|
-
if (id === "rankedHistorySearch") {
|
|
195
|
-
list.updateValue(
|
|
196
|
-
id,
|
|
197
|
-
current.rankedHistorySearch ? "on" : "off",
|
|
198
|
-
);
|
|
199
|
-
} else
|
|
200
|
-
list.updateValue(
|
|
201
|
-
id,
|
|
202
|
-
id === "active"
|
|
203
|
-
? current.active === "all"
|
|
204
|
-
? "All models"
|
|
205
|
-
: "Whitelist"
|
|
206
|
-
: id === "taskBoundaryReminder"
|
|
207
|
-
? taskBoundaryLabel(current.taskBoundaryReminder)
|
|
208
|
-
: id === "newWindowRequests"
|
|
209
|
-
? current.newWindowRequests
|
|
210
|
-
: id === "noteSaveMarkers"
|
|
211
|
-
? current.noteSaveMarkers
|
|
212
|
-
? "on"
|
|
213
|
-
: "off"
|
|
214
|
-
: id === "compact"
|
|
215
|
-
? current.normalCompaction
|
|
216
|
-
? "on"
|
|
217
|
-
: "off"
|
|
218
|
-
: idleValue(current.idleMinutes),
|
|
219
|
-
);
|
|
220
|
-
ctx.ui.notify(
|
|
221
|
-
`Could not save notes settings: ${String(error)}`,
|
|
222
|
-
"error",
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
tui.requestRender();
|
|
226
|
-
},
|
|
227
|
-
() => done("close"),
|
|
228
|
-
{ enableSearch: false },
|
|
31
|
+
const list = createNotesSettingsControls(ctx, options, done, () =>
|
|
32
|
+
tui.requestRender(),
|
|
229
33
|
);
|
|
230
34
|
return {
|
|
231
35
|
render: (width) => {
|
|
@@ -236,7 +40,7 @@ export async function openNotesSettings(
|
|
|
236
40
|
const rule = theme.fg("accent", "─".repeat(Math.max(0, width)));
|
|
237
41
|
return [
|
|
238
42
|
rule,
|
|
239
|
-
` ${theme.bold("Notes
|
|
43
|
+
` ${theme.bold("Notes")} / ${theme.fg("dim", "Roles")}`,
|
|
240
44
|
"",
|
|
241
45
|
...options
|
|
242
46
|
.status()
|
|
@@ -250,9 +54,9 @@ export async function openNotesSettings(
|
|
|
250
54
|
"",
|
|
251
55
|
...list.render(width).slice(0, -2),
|
|
252
56
|
"",
|
|
253
|
-
...wrap(
|
|
254
|
-
|
|
255
|
-
),
|
|
57
|
+
...wrap(
|
|
58
|
+
"Tab switch tab · ↑/↓ select · Enter/Space change · Esc close",
|
|
59
|
+
).map((line) => theme.fg("dim", line)),
|
|
256
60
|
...wrap("Settings saved immediately").map((line) =>
|
|
257
61
|
theme.fg("dim", line),
|
|
258
62
|
),
|
|
@@ -261,6 +65,13 @@ export async function openNotesSettings(
|
|
|
261
65
|
},
|
|
262
66
|
invalidate: () => list.invalidate(),
|
|
263
67
|
handleInput: (data) => {
|
|
68
|
+
if (
|
|
69
|
+
matchesKey(data, Key.tab) ||
|
|
70
|
+
matchesKey(data, Key.shift(Key.tab))
|
|
71
|
+
) {
|
|
72
|
+
done("roles");
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
264
75
|
list.handleInput(data);
|
|
265
76
|
tui.requestRender();
|
|
266
77
|
},
|
|
@@ -268,6 +79,7 @@ export async function openNotesSettings(
|
|
|
268
79
|
},
|
|
269
80
|
);
|
|
270
81
|
if (action === "close") return;
|
|
82
|
+
if (action === "roles") return "roles";
|
|
271
83
|
if (action === "sessionPruning") {
|
|
272
84
|
await openSessionPruningSettings(ctx, options.sessionPruning);
|
|
273
85
|
continue;
|
package/src/shared-notes.ts
CHANGED
|
@@ -1,86 +1,25 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from "node:util";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
SessionEntry,
|
|
5
|
-
} from "@earendil-works/pi-coding-agent";
|
|
6
|
-
import { type Static, Type } from "typebox";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { Static } from "typebox";
|
|
7
4
|
import { Check } from "typebox/value";
|
|
8
|
-
import { useHistory } from "./history.js";
|
|
9
5
|
import type { HistorySearch } from "./history-search.js";
|
|
10
|
-
import { collectNotes, normalizeNoteParams, readNotes } from "./notes.js";
|
|
11
6
|
import {
|
|
12
7
|
readSavedNotesSession,
|
|
13
8
|
type SavedNotesSession,
|
|
14
9
|
} from "./saved-session.js";
|
|
10
|
+
import {
|
|
11
|
+
Detachment,
|
|
12
|
+
Member,
|
|
13
|
+
Parent,
|
|
14
|
+
Plan,
|
|
15
|
+
Route,
|
|
16
|
+
sameOwner,
|
|
17
|
+
savedNotesIdentity,
|
|
18
|
+
} from "./saved-sharing-records.js";
|
|
19
|
+
import { createSharedSessionReader } from "./shared-session-reader.js";
|
|
15
20
|
import type { NotesStore } from "./store.js";
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
const Identity = Type.Object({
|
|
19
|
-
protocol: Type.Literal(1),
|
|
20
|
-
sessionId: Type.String(),
|
|
21
|
-
threadId: Type.String(),
|
|
22
|
-
agentName: Agent,
|
|
23
|
-
storage: Type.Optional(
|
|
24
|
-
Type.Union([Type.Literal("session"), Type.Literal("remote")]),
|
|
25
|
-
),
|
|
26
|
-
routing: Type.Optional(Type.Unknown()),
|
|
27
|
-
});
|
|
28
|
-
const Member = Type.Object({
|
|
29
|
-
threadId: Type.String(),
|
|
30
|
-
sessionId: Type.String(),
|
|
31
|
-
agentName: Agent,
|
|
32
|
-
machine: Type.String(),
|
|
33
|
-
sessionFile: Type.String(),
|
|
34
|
-
});
|
|
35
|
-
const Plan = Type.Object({
|
|
36
|
-
controllerSessionId: Type.String(),
|
|
37
|
-
targetSessionId: Type.String(),
|
|
38
|
-
context: Type.Optional(
|
|
39
|
-
Type.Object({
|
|
40
|
-
controller: Identity,
|
|
41
|
-
target: Identity,
|
|
42
|
-
alias: Agent,
|
|
43
|
-
controllerAlias: Agent,
|
|
44
|
-
controllerFile: Type.Optional(Type.String()),
|
|
45
|
-
controllerNotes: Type.Unknown(),
|
|
46
|
-
targetNotes: Type.Unknown(),
|
|
47
|
-
}),
|
|
48
|
-
),
|
|
49
|
-
});
|
|
50
|
-
const Route = Type.Object({
|
|
51
|
-
plan: Plan,
|
|
52
|
-
machine: Type.String(),
|
|
53
|
-
sessionFile: Type.String(),
|
|
54
|
-
phase: Type.Union([Type.Literal("pending"), Type.Literal("ready")]),
|
|
55
|
-
detached: Type.Optional(
|
|
56
|
-
Type.Object({ context: Type.Optional(Type.Unknown()) }),
|
|
57
|
-
),
|
|
58
|
-
});
|
|
59
|
-
const Detachment = Type.Object({
|
|
60
|
-
plan: Plan,
|
|
61
|
-
context: Type.Optional(Type.Unknown()),
|
|
62
|
-
});
|
|
63
|
-
const Snapshot = Type.Object({
|
|
64
|
-
protocol: Type.Literal(1),
|
|
65
|
-
timestamp: Type.Number(),
|
|
66
|
-
files: Type.Array(
|
|
67
|
-
Type.Object({
|
|
68
|
-
path: Type.String(),
|
|
69
|
-
text: Type.String(),
|
|
70
|
-
createdAt: Type.Number(),
|
|
71
|
-
updatedAt: Type.Number(),
|
|
72
|
-
operation: Type.Union([
|
|
73
|
-
Type.Literal("local_append_overlay"),
|
|
74
|
-
Type.Literal("local_replacement"),
|
|
75
|
-
]),
|
|
76
|
-
}),
|
|
77
|
-
),
|
|
78
|
-
});
|
|
79
|
-
const Parent = Type.Object({
|
|
80
|
-
transport: Type.Literal("shepherdr"),
|
|
81
|
-
parent: Type.String(),
|
|
82
|
-
});
|
|
83
|
-
type Identity = Static<typeof Identity>;
|
|
22
|
+
export { savedNotesIdentity } from "./saved-sharing-records.js";
|
|
84
23
|
|
|
85
24
|
function records(session: SavedNotesSession, kind: string): unknown[] {
|
|
86
25
|
return session.entries.flatMap((entry) =>
|
|
@@ -88,37 +27,6 @@ function records(session: SavedNotesSession, kind: string): unknown[] {
|
|
|
88
27
|
);
|
|
89
28
|
}
|
|
90
29
|
|
|
91
|
-
/** Persisted sharing is session-scoped. Forks never inherit the source agent's address. */
|
|
92
|
-
export function savedNotesIdentity(
|
|
93
|
-
session: Pick<SavedNotesSession, "threadId" | "entries">,
|
|
94
|
-
): Identity {
|
|
95
|
-
let found: Identity | undefined;
|
|
96
|
-
for (const entry of session.entries) {
|
|
97
|
-
if (entry.type !== "custom" || entry.customType !== "codex-context-agent")
|
|
98
|
-
continue;
|
|
99
|
-
if (!Check(Identity, entry.data))
|
|
100
|
-
throw new Error("Invalid saved notes identity");
|
|
101
|
-
if (entry.data.threadId !== session.threadId) continue;
|
|
102
|
-
if (found) throw new Error("Conflicting saved notes identities");
|
|
103
|
-
found = entry.data;
|
|
104
|
-
}
|
|
105
|
-
return (
|
|
106
|
-
found ?? {
|
|
107
|
-
protocol: 1,
|
|
108
|
-
threadId: session.threadId,
|
|
109
|
-
sessionId: session.threadId,
|
|
110
|
-
agentName: "/root",
|
|
111
|
-
}
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function sameOwner(a: Identity, b: Identity): boolean {
|
|
116
|
-
return (
|
|
117
|
-
a.threadId === b.threadId &&
|
|
118
|
-
a.sessionId === b.sessionId &&
|
|
119
|
-
a.agentName === b.agentName
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
30
|
function inside(path: string, root: string): boolean {
|
|
123
31
|
return path === root || path.startsWith(`${root}/`);
|
|
124
32
|
}
|
|
@@ -212,104 +120,13 @@ async function readSharedSession(
|
|
|
212
120
|
if (savedNotesIdentity(current).agentName === target) return;
|
|
213
121
|
const visited = new Set<string>();
|
|
214
122
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
? await useHistory(
|
|
223
|
-
session.branch,
|
|
224
|
-
query,
|
|
225
|
-
session.threadId,
|
|
226
|
-
access.search,
|
|
227
|
-
session.entries,
|
|
228
|
-
signal,
|
|
229
|
-
)
|
|
230
|
-
: readNotes(
|
|
231
|
-
collectNotes(store, session.branch),
|
|
232
|
-
normalizeNoteParams(query, native),
|
|
233
|
-
outputAgent,
|
|
234
|
-
)),
|
|
235
|
-
...(namespace === "notes"
|
|
236
|
-
? {
|
|
237
|
-
read_only: true,
|
|
238
|
-
warning:
|
|
239
|
-
"Only saved notes are included; other notes may be missing",
|
|
240
|
-
}
|
|
241
|
-
: {}),
|
|
242
|
-
...(session.savedAt === undefined
|
|
243
|
-
? {}
|
|
244
|
-
: { saved_at: new Date(session.savedAt).toISOString() }),
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
async function reference(
|
|
248
|
-
identity: Identity,
|
|
249
|
-
destination: string,
|
|
250
|
-
query: Record<string, unknown>,
|
|
251
|
-
file: string | undefined,
|
|
252
|
-
retained?: unknown,
|
|
253
|
-
detached = false,
|
|
254
|
-
): Promise<Record<string, unknown> | undefined> {
|
|
255
|
-
if (file && (!detached || namespace === "history")) {
|
|
256
|
-
let saved: SavedNotesSession | undefined;
|
|
257
|
-
try {
|
|
258
|
-
saved = await readSavedNotesSession(
|
|
259
|
-
file,
|
|
260
|
-
signal,
|
|
261
|
-
namespace === "history",
|
|
262
|
-
);
|
|
263
|
-
} catch (error) {
|
|
264
|
-
signal?.throwIfAborted();
|
|
265
|
-
if (
|
|
266
|
-
!(
|
|
267
|
-
error instanceof Error &&
|
|
268
|
-
"code" in error &&
|
|
269
|
-
error.code === "ENOENT" &&
|
|
270
|
-
retained !== undefined &&
|
|
271
|
-
namespace === "notes"
|
|
272
|
-
)
|
|
273
|
-
)
|
|
274
|
-
throw error;
|
|
275
|
-
}
|
|
276
|
-
if (saved) {
|
|
277
|
-
if (!sameOwner(savedNotesIdentity(saved), identity))
|
|
278
|
-
throw new Error("Saved shared notes belong to a different session");
|
|
279
|
-
return resolve(saved, destination, query);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
if (
|
|
283
|
-
namespace === "history" ||
|
|
284
|
-
destination !== identity.agentName ||
|
|
285
|
-
!Check(Snapshot, retained)
|
|
286
|
-
)
|
|
287
|
-
return;
|
|
288
|
-
const entry: SessionEntry = {
|
|
289
|
-
type: "custom",
|
|
290
|
-
id: "retained",
|
|
291
|
-
parentId: null,
|
|
292
|
-
timestamp: new Date(retained.timestamp).toISOString(),
|
|
293
|
-
customType: "codex-context-note-snapshot",
|
|
294
|
-
data: {
|
|
295
|
-
...retained,
|
|
296
|
-
files: retained.files.filter((file) =>
|
|
297
|
-
file.path.startsWith(`${identity.agentName}/notes/`),
|
|
298
|
-
),
|
|
299
|
-
},
|
|
300
|
-
};
|
|
301
|
-
return {
|
|
302
|
-
...(await read(
|
|
303
|
-
{ threadId: identity.threadId, entries: [], branch: [entry] },
|
|
304
|
-
query,
|
|
305
|
-
identity.agentName,
|
|
306
|
-
)),
|
|
307
|
-
captured_at: new Date(retained.timestamp).toISOString(),
|
|
308
|
-
warning: detached
|
|
309
|
-
? "Read-only notes captured when sharing ended; later changes are not included"
|
|
310
|
-
: "The latest notes are unavailable; this read-only copy may be outdated",
|
|
311
|
-
};
|
|
312
|
-
}
|
|
123
|
+
const { read, reference } = createSharedSessionReader(
|
|
124
|
+
store,
|
|
125
|
+
access,
|
|
126
|
+
outputAgent,
|
|
127
|
+
signal,
|
|
128
|
+
resolve,
|
|
129
|
+
);
|
|
313
130
|
async function resolve(
|
|
314
131
|
session: SavedNotesSession,
|
|
315
132
|
destination: string,
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Check } from "typebox/value";
|
|
3
|
+
import { useHistory } from "./history.js";
|
|
4
|
+
import type { HistorySearch } from "./history-search.js";
|
|
5
|
+
import { normalizeNoteParams, readNotes } from "./note-query.js";
|
|
6
|
+
import { collectNotes } from "./notes.js";
|
|
7
|
+
import {
|
|
8
|
+
readSavedNotesSession,
|
|
9
|
+
type SavedNotesSession,
|
|
10
|
+
} from "./saved-session.js";
|
|
11
|
+
import {
|
|
12
|
+
type Identity,
|
|
13
|
+
Snapshot,
|
|
14
|
+
sameOwner,
|
|
15
|
+
savedNotesIdentity,
|
|
16
|
+
} from "./saved-sharing-records.js";
|
|
17
|
+
import type { NotesStore } from "./store.js";
|
|
18
|
+
|
|
19
|
+
export function createSharedSessionReader(
|
|
20
|
+
store: Pick<NotesStore, "read">,
|
|
21
|
+
access:
|
|
22
|
+
| { namespace: "notes" }
|
|
23
|
+
| { namespace: "history"; search: HistorySearch },
|
|
24
|
+
outputAgent: string,
|
|
25
|
+
signal: AbortSignal | undefined,
|
|
26
|
+
resolve: (
|
|
27
|
+
session: SavedNotesSession,
|
|
28
|
+
destination: string,
|
|
29
|
+
query: Record<string, unknown>,
|
|
30
|
+
) => Promise<Record<string, unknown> | undefined>,
|
|
31
|
+
) {
|
|
32
|
+
const { namespace } = access;
|
|
33
|
+
async function read(
|
|
34
|
+
session: SavedNotesSession,
|
|
35
|
+
query: Record<string, unknown>,
|
|
36
|
+
native: string,
|
|
37
|
+
) {
|
|
38
|
+
return {
|
|
39
|
+
...(access.namespace === "history"
|
|
40
|
+
? await useHistory(
|
|
41
|
+
session.branch,
|
|
42
|
+
query,
|
|
43
|
+
session.threadId,
|
|
44
|
+
access.search,
|
|
45
|
+
session.entries,
|
|
46
|
+
signal,
|
|
47
|
+
)
|
|
48
|
+
: readNotes(
|
|
49
|
+
collectNotes(store, session.branch),
|
|
50
|
+
normalizeNoteParams(query, native),
|
|
51
|
+
outputAgent,
|
|
52
|
+
)),
|
|
53
|
+
...(namespace === "notes"
|
|
54
|
+
? {
|
|
55
|
+
read_only: true,
|
|
56
|
+
warning:
|
|
57
|
+
"Only saved notes are included; other notes may be missing",
|
|
58
|
+
}
|
|
59
|
+
: {}),
|
|
60
|
+
...(session.savedAt === undefined
|
|
61
|
+
? {}
|
|
62
|
+
: { saved_at: new Date(session.savedAt).toISOString() }),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function reference(
|
|
66
|
+
identity: Identity,
|
|
67
|
+
destination: string,
|
|
68
|
+
query: Record<string, unknown>,
|
|
69
|
+
file: string | undefined,
|
|
70
|
+
retained?: unknown,
|
|
71
|
+
detached = false,
|
|
72
|
+
): Promise<Record<string, unknown> | undefined> {
|
|
73
|
+
if (file && (!detached || namespace === "history")) {
|
|
74
|
+
let saved: SavedNotesSession | undefined;
|
|
75
|
+
try {
|
|
76
|
+
saved = await readSavedNotesSession(
|
|
77
|
+
file,
|
|
78
|
+
signal,
|
|
79
|
+
namespace === "history",
|
|
80
|
+
);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
signal?.throwIfAborted();
|
|
83
|
+
if (
|
|
84
|
+
!(
|
|
85
|
+
error instanceof Error &&
|
|
86
|
+
"code" in error &&
|
|
87
|
+
error.code === "ENOENT" &&
|
|
88
|
+
retained !== undefined &&
|
|
89
|
+
namespace === "notes"
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
if (saved) {
|
|
95
|
+
if (!sameOwner(savedNotesIdentity(saved), identity))
|
|
96
|
+
throw new Error("Saved shared notes belong to a different session");
|
|
97
|
+
return resolve(saved, destination, query);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (
|
|
101
|
+
namespace === "history" ||
|
|
102
|
+
destination !== identity.agentName ||
|
|
103
|
+
!Check(Snapshot, retained)
|
|
104
|
+
)
|
|
105
|
+
return;
|
|
106
|
+
const entry: SessionEntry = {
|
|
107
|
+
type: "custom",
|
|
108
|
+
id: "retained",
|
|
109
|
+
parentId: null,
|
|
110
|
+
timestamp: new Date(retained.timestamp).toISOString(),
|
|
111
|
+
customType: "codex-context-note-snapshot",
|
|
112
|
+
data: {
|
|
113
|
+
...retained,
|
|
114
|
+
files: retained.files.filter((file) =>
|
|
115
|
+
file.path.startsWith(`${identity.agentName}/notes/`),
|
|
116
|
+
),
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
...(await read(
|
|
121
|
+
{ threadId: identity.threadId, entries: [], branch: [entry] },
|
|
122
|
+
query,
|
|
123
|
+
identity.agentName,
|
|
124
|
+
)),
|
|
125
|
+
captured_at: new Date(retained.timestamp).toISOString(),
|
|
126
|
+
warning: detached
|
|
127
|
+
? "Read-only notes captured when sharing ended; later changes are not included"
|
|
128
|
+
: "The latest notes are unavailable; this read-only copy may be outdated",
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return { read, reference };
|
|
132
|
+
}
|