@howaboua/pi-agent-comms 0.0.0-stage → 0.0.1-dev.13.1.gef1ce49691d6
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/INTEGRATION.md +31 -0
- package/LICENSE +21 -0
- package/PCC-INTEGRATION.md +17 -0
- package/README.md +53 -2
- package/dist/viewer/archive.js +249 -0
- package/dist/viewer/config.js +85 -0
- package/dist/viewer/contracts.js +1 -0
- package/dist/viewer/server.js +177 -0
- package/dist/viewer/source.js +29 -0
- package/dist/viewer/web/app.js +88 -0
- package/dist/viewer/web/index.html +41 -0
- package/dist/viewer/web/navigation.js +72 -0
- package/dist/viewer/web/posts.js +105 -0
- package/dist/viewer/web/render.js +147 -0
- package/dist/viewer/web/session.js +86 -0
- package/dist/viewer/web/style.css +517 -0
- package/dist/viewer/web/updates.js +106 -0
- package/docs/BOARD-CHANGELOG.md +49 -0
- package/docs/BOARD.md +124 -0
- package/docs/NOTES-CHANGELOG.md +127 -0
- package/docs/NOTES.md +122 -0
- package/index.ts +21 -0
- package/package.json +75 -5
- package/src/board/AGENTS.md +12 -0
- package/src/board/activity.ts +274 -0
- package/src/board/archive.ts +118 -0
- package/src/board/awareness.ts +179 -0
- package/src/board/briefing.ts +25 -0
- package/src/board/config.ts +192 -0
- package/src/board/consolidation.ts +32 -0
- package/src/board/context-briefing.ts +245 -0
- package/src/board/contract.ts +254 -0
- package/src/board/delivery.ts +85 -0
- package/src/board/feature.ts +91 -0
- package/src/board/identity.ts +238 -0
- package/src/board/integration.ts +55 -0
- package/src/board/paging.ts +54 -0
- package/src/board/protocol.ts +78 -0
- package/src/board/queries.ts +168 -0
- package/src/board/response.ts +82 -0
- package/src/board/runtime/activation.ts +50 -0
- package/src/board/runtime/catchup.ts +96 -0
- package/src/board/runtime/coordinator.ts +311 -0
- package/src/board/runtime/engine.ts +40 -0
- package/src/board/runtime/index.ts +63 -0
- package/src/board/runtime/owner.ts +168 -0
- package/src/board/runtime/routing.ts +152 -0
- package/src/board/runtime/turns.ts +109 -0
- package/src/board/search.ts +95 -0
- package/src/board/settings/panel.ts +193 -0
- package/src/board/store.ts +427 -0
- package/src/board/summaries.ts +124 -0
- package/src/board/summarizer.ts +201 -0
- package/src/board/tool.ts +27 -0
- package/src/board/viewer/AGENTS.md +1 -0
- package/src/board/viewer/archive.ts +308 -0
- package/src/board/viewer/config.ts +111 -0
- package/src/board/viewer/contracts.ts +98 -0
- package/src/board/viewer/index.ts +162 -0
- package/src/board/viewer/server.ts +204 -0
- package/src/board/viewer/source.ts +33 -0
- package/src/board/viewer/web/app.js +88 -0
- package/src/board/viewer/web/index.html +41 -0
- package/src/board/viewer/web/navigation.js +72 -0
- package/src/board/viewer/web/posts.js +105 -0
- package/src/board/viewer/web/render.js +147 -0
- package/src/board/viewer/web/session.js +86 -0
- package/src/board/viewer/web/style.css +517 -0
- package/src/board/viewer/web/updates.js +106 -0
- package/src/guidance.ts +121 -0
- package/src/notes/AGENTS.md +27 -0
- package/src/notes/activation.ts +150 -0
- package/src/notes/agent-identity.ts +221 -0
- package/src/notes/attachment-sharing.ts +165 -0
- package/src/notes/attachment-snapshot.ts +113 -0
- package/src/notes/binding-startup.ts +30 -0
- package/src/notes/bridge.ts +130 -0
- package/src/notes/compaction-projection.ts +88 -0
- package/src/notes/context-owner.ts +99 -0
- package/src/notes/context-sharing.ts +178 -0
- package/src/notes/extension-hooks.ts +200 -0
- package/src/notes/extension-sharing.ts +133 -0
- package/src/notes/feature.ts +168 -0
- package/src/notes/fresh-checkpoint.ts +143 -0
- package/src/notes/fresh-notes.ts +323 -0
- package/src/notes/history-search-protocol.ts +39 -0
- package/src/notes/history-search-pruning.mjs +120 -0
- package/src/notes/history-search-worker.mjs +258 -0
- package/src/notes/history-search.ts +269 -0
- package/src/notes/history-windows.ts +221 -0
- package/src/notes/history.ts +158 -0
- package/src/notes/idle-admission.ts +201 -0
- package/src/notes/index.ts +125 -0
- package/src/notes/integrations.ts +174 -0
- package/src/notes/lifecycle.ts +308 -0
- package/src/notes/model-role-config.ts +79 -0
- package/src/notes/model-role-screen.ts +74 -0
- package/src/notes/model-roles.ts +225 -0
- package/src/notes/note-query.ts +148 -0
- package/src/notes/note-result.ts +88 -0
- package/src/notes/note-save-marker.ts +128 -0
- package/src/notes/notes-lookup.ts +115 -0
- package/src/notes/notes.ts +302 -0
- package/src/notes/query-executor.ts +242 -0
- package/src/notes/query-input.ts +126 -0
- package/src/notes/remote.ts +376 -0
- package/src/notes/rollover.ts +224 -0
- package/src/notes/saved-session-access.ts +117 -0
- package/src/notes/saved-session-index.mjs +83 -0
- package/src/notes/saved-session.mjs +241 -0
- package/src/notes/saved-session.ts +51 -0
- package/src/notes/saved-sharing-records.ts +135 -0
- package/src/notes/session-pruning-content.ts +237 -0
- package/src/notes/session-pruning-plan.ts +243 -0
- package/src/notes/session-pruning-policy.ts +20 -0
- package/src/notes/session-pruning-screen.ts +204 -0
- package/src/notes/session-pruning-source.ts +44 -0
- package/src/notes/session-pruning-transaction.ts +141 -0
- package/src/notes/session-pruning.ts +155 -0
- package/src/notes/settings-command.ts +95 -0
- package/src/notes/settings-controls.ts +199 -0
- package/src/notes/settings-screen.ts +113 -0
- package/src/notes/settings.ts +287 -0
- package/src/notes/shared-notes.ts +411 -0
- package/src/notes/shared-session-reader.ts +143 -0
- package/src/notes/sharing-service.ts +282 -0
- package/src/notes/sqlite.mjs +33 -0
- package/src/notes/store.ts +283 -0
- package/src/notes/task-boundary.ts +37 -0
- package/src/notes/tool-contracts.ts +312 -0
- package/src/notes/tools.ts +148 -0
- package/src/notes/tree-archive-manifest.mjs +27 -0
- package/src/notes/tree-archive.ts +129 -0
- package/src/notes/tree-checkpoint.ts +170 -0
- package/src/notes/tree-handoff-read.ts +192 -0
- package/src/notes/window-checkpoint.ts +105 -0
- package/src/notes/window-message.ts +185 -0
- package/src/notes/windows.ts +272 -0
- package/src/settings-navigation.ts +11 -0
- package/src/timestamps.ts +3 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { NotesActivation } from "./activation.js";
|
|
3
|
+
import type { NotesOwner } from "./bridge.js";
|
|
4
|
+
import { NESTED_NOTE_OUTCOME, type NestedNoteOutcome } from "./fresh-notes.js";
|
|
5
|
+
import { registerNotesCompactionIntegration } from "./integrations.js";
|
|
6
|
+
import type { NotesLifecycle } from "./lifecycle.js";
|
|
7
|
+
import type { registerNoteSaveMarkers } from "./note-save-marker.js";
|
|
8
|
+
import { isNoteWrite } from "./query-input.js";
|
|
9
|
+
import {
|
|
10
|
+
projectTreeCheckpointBranch,
|
|
11
|
+
projectTreeCheckpointMessages,
|
|
12
|
+
} from "./tree-checkpoint.js";
|
|
13
|
+
import { projectTreeHandoffReads } from "./tree-handoff-read.js";
|
|
14
|
+
import type { WindowCheckpoints } from "./window-checkpoint.js";
|
|
15
|
+
|
|
16
|
+
export async function registerNotesHooks(
|
|
17
|
+
pi: ExtensionAPI,
|
|
18
|
+
activation: NotesActivation,
|
|
19
|
+
lifecycle: NotesLifecycle,
|
|
20
|
+
owner: NotesOwner,
|
|
21
|
+
checkpoints: WindowCheckpoints,
|
|
22
|
+
markers: ReturnType<typeof registerNoteSaveMarkers>,
|
|
23
|
+
) {
|
|
24
|
+
pi.on("before_agent_start", (event, ctx) => {
|
|
25
|
+
activation.refreshActivation(ctx);
|
|
26
|
+
if (lifecycle.active) return lifecycle.beforeStart(ctx, event);
|
|
27
|
+
});
|
|
28
|
+
pi.on("input", async (event, ctx) => {
|
|
29
|
+
activation.refreshActivation(ctx);
|
|
30
|
+
if (activation.available && ctx.isIdle()) {
|
|
31
|
+
try {
|
|
32
|
+
await checkpoints.restore(ctx);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
ctx.ui.notify(`Context checkpoint failed: ${String(error)}`, "error");
|
|
35
|
+
return { action: "handled" };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return lifecycle.active ? lifecycle.input(event, ctx) : undefined;
|
|
39
|
+
});
|
|
40
|
+
const nestedWrites = new Map<
|
|
41
|
+
string,
|
|
42
|
+
{ runId: string; windowId: string; parentCallId: string; sessionId: string }
|
|
43
|
+
>();
|
|
44
|
+
pi.on("tool_execution_start", (event, ctx) => {
|
|
45
|
+
if (
|
|
46
|
+
!lifecycle.active ||
|
|
47
|
+
event.toolName !== "notes" ||
|
|
48
|
+
!event.parentToolCallId ||
|
|
49
|
+
!lifecycle.runId ||
|
|
50
|
+
!lifecycle.windows.current
|
|
51
|
+
)
|
|
52
|
+
return;
|
|
53
|
+
const input = event.args as { action?: unknown } | undefined;
|
|
54
|
+
if (!isNoteWrite(input)) return;
|
|
55
|
+
const write = {
|
|
56
|
+
runId: lifecycle.runId,
|
|
57
|
+
windowId: lifecycle.windows.current.currentWindowId,
|
|
58
|
+
parentCallId: event.parentToolCallId,
|
|
59
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
60
|
+
};
|
|
61
|
+
// Persist before execution: even a call omitted from Pi's bounded record
|
|
62
|
+
// must prevent rollover until its completion arrives.
|
|
63
|
+
pi.appendEntry(NESTED_NOTE_OUTCOME, {
|
|
64
|
+
protocol: 1,
|
|
65
|
+
runId: write.runId,
|
|
66
|
+
windowId: write.windowId,
|
|
67
|
+
callId: event.toolCallId,
|
|
68
|
+
parentCallId: write.parentCallId,
|
|
69
|
+
pending: true,
|
|
70
|
+
saved: false,
|
|
71
|
+
} satisfies NestedNoteOutcome);
|
|
72
|
+
nestedWrites.set(event.toolCallId, write);
|
|
73
|
+
});
|
|
74
|
+
pi.on("tool_execution_end", (event, ctx) => {
|
|
75
|
+
const write = nestedWrites.get(event.toolCallId);
|
|
76
|
+
nestedWrites.delete(event.toolCallId);
|
|
77
|
+
if (!write || write.sessionId !== ctx.sessionManager.getSessionId()) return;
|
|
78
|
+
const details = event.result?.details;
|
|
79
|
+
pi.appendEntry(NESTED_NOTE_OUTCOME, {
|
|
80
|
+
protocol: 1,
|
|
81
|
+
runId: write.runId,
|
|
82
|
+
windowId: write.windowId,
|
|
83
|
+
callId: event.toolCallId,
|
|
84
|
+
parentCallId: write.parentCallId,
|
|
85
|
+
pending: false,
|
|
86
|
+
...(details?.notesCompactionRouted === true
|
|
87
|
+
? { origin: "routed" as const }
|
|
88
|
+
: {}),
|
|
89
|
+
saved:
|
|
90
|
+
!event.isError &&
|
|
91
|
+
details?.notesCompaction?.saved === true &&
|
|
92
|
+
details.notesCompaction.runId === write.runId,
|
|
93
|
+
} satisfies NestedNoteOutcome);
|
|
94
|
+
});
|
|
95
|
+
pi.on("agent_before_settle", (event) => {
|
|
96
|
+
if (lifecycle.active) lifecycle.outcome(event.outcome);
|
|
97
|
+
});
|
|
98
|
+
pi.on("agent_settled", async (event, ctx) => {
|
|
99
|
+
if (!lifecycle.active) return;
|
|
100
|
+
try {
|
|
101
|
+
markers.record(
|
|
102
|
+
ctx,
|
|
103
|
+
lifecycle.windows.current?.currentWindowId,
|
|
104
|
+
lifecycle.runId,
|
|
105
|
+
event.aborted,
|
|
106
|
+
);
|
|
107
|
+
await lifecycle.settled(event, ctx);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
ctx.ui.notify(
|
|
110
|
+
`Context rollover failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
111
|
+
"error",
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
pi.on("turn_end", (event, ctx) => {
|
|
116
|
+
const checkpoint = event.entries.some(
|
|
117
|
+
(entry) => entry.type === "compaction",
|
|
118
|
+
)
|
|
119
|
+
? undefined
|
|
120
|
+
: checkpoints.draft(ctx.sessionManager.getBranch());
|
|
121
|
+
const entries = checkpoint ? [...event.entries, checkpoint] : event.entries;
|
|
122
|
+
if (!lifecycle.active || lifecycle.newWindowRequests === "off")
|
|
123
|
+
return checkpoint ? { entries } : undefined;
|
|
124
|
+
const reminder = lifecycle.windows.reminder(
|
|
125
|
+
ctx,
|
|
126
|
+
event.message.role === "assistant" &&
|
|
127
|
+
!ctx.signal?.aborted &&
|
|
128
|
+
!["error", "length", "aborted"].includes(event.message.stopReason) &&
|
|
129
|
+
event.toolResults.length > 0 &&
|
|
130
|
+
pi.getActiveTools().includes("new_context")
|
|
131
|
+
? lifecycle.taskBoundaryReminder
|
|
132
|
+
: 0,
|
|
133
|
+
!lifecycle.fresh(ctx, lifecycle.runId),
|
|
134
|
+
lifecycle.gptPromptMode(),
|
|
135
|
+
);
|
|
136
|
+
return reminder || checkpoint
|
|
137
|
+
? {
|
|
138
|
+
entries: [
|
|
139
|
+
...entries,
|
|
140
|
+
...(reminder
|
|
141
|
+
? [lifecycle.prepareReminder?.(reminder) ?? reminder]
|
|
142
|
+
: []),
|
|
143
|
+
],
|
|
144
|
+
}
|
|
145
|
+
: undefined;
|
|
146
|
+
});
|
|
147
|
+
pi.on("context", (event, ctx) => {
|
|
148
|
+
try {
|
|
149
|
+
if (lifecycle.active)
|
|
150
|
+
markers.bookmark(ctx, lifecycle.windows.current?.currentWindowId);
|
|
151
|
+
const branch = ctx.sessionManager.getBranch();
|
|
152
|
+
const projected = projectTreeCheckpointBranch(
|
|
153
|
+
branch,
|
|
154
|
+
ctx.sessionManager.getEntries(),
|
|
155
|
+
);
|
|
156
|
+
return {
|
|
157
|
+
messages: owner.projectMessages(
|
|
158
|
+
projectTreeHandoffReads(
|
|
159
|
+
projectTreeCheckpointMessages(branch, projected, event.messages) ??
|
|
160
|
+
event.messages,
|
|
161
|
+
projected,
|
|
162
|
+
),
|
|
163
|
+
projected,
|
|
164
|
+
),
|
|
165
|
+
};
|
|
166
|
+
} catch (error) {
|
|
167
|
+
ctx.abort();
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
const prepareProviderWindow = await registerNotesCompactionIntegration(
|
|
172
|
+
pi,
|
|
173
|
+
activation,
|
|
174
|
+
lifecycle,
|
|
175
|
+
checkpoints,
|
|
176
|
+
);
|
|
177
|
+
lifecycle.prepareWindow = async (ctx) => {
|
|
178
|
+
activation.refreshHistorySearch(ctx, true);
|
|
179
|
+
await prepareProviderWindow?.(ctx);
|
|
180
|
+
};
|
|
181
|
+
pi.on("session_compact_failed", async (event, ctx) => {
|
|
182
|
+
if (lifecycle.active) await lifecycle.compactFailed(event, ctx);
|
|
183
|
+
});
|
|
184
|
+
pi.on("session_compact", (_event, ctx) => {
|
|
185
|
+
if (lifecycle.active) {
|
|
186
|
+
lifecycle.windows.restore(ctx.sessionManager.getBranch());
|
|
187
|
+
activation.refreshHistorySearch(ctx, true);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
pi.on("session_tree", (_event, ctx) => {
|
|
191
|
+
lifecycle.reset(ctx);
|
|
192
|
+
activation.refreshHistorySearch(ctx, true);
|
|
193
|
+
});
|
|
194
|
+
pi.on("agent_start", (_event, ctx) =>
|
|
195
|
+
activation.refreshHistorySearch(ctx, true),
|
|
196
|
+
);
|
|
197
|
+
pi.on("model_select", (_event, ctx) => {
|
|
198
|
+
activation.refreshActivation(ctx);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import type { NotesActivation } from "./activation.js";
|
|
7
|
+
import {
|
|
8
|
+
contextAgentIdentity,
|
|
9
|
+
contextAgentNames,
|
|
10
|
+
contextTargetAgent,
|
|
11
|
+
} from "./agent-identity.js";
|
|
12
|
+
import { type NotesOwner, OWNER_CHANNEL, type OwnerRequest } from "./bridge.js";
|
|
13
|
+
import { optionalPccMissing } from "./integrations.js";
|
|
14
|
+
import type { NotesLifecycle } from "./lifecycle.js";
|
|
15
|
+
import { noteResult } from "./note-result.js";
|
|
16
|
+
import { modelIsActive } from "./settings.js";
|
|
17
|
+
import { registerContextSharingService } from "./sharing-service.js";
|
|
18
|
+
|
|
19
|
+
export async function registerNotesSharing(
|
|
20
|
+
pi: ExtensionAPI,
|
|
21
|
+
owner: NotesOwner,
|
|
22
|
+
activation: NotesActivation,
|
|
23
|
+
lifecycle: NotesLifecycle,
|
|
24
|
+
) {
|
|
25
|
+
const route = registerContextSharingService(pi, {
|
|
26
|
+
enabled: () => activation.available,
|
|
27
|
+
shareSubagentContext: () =>
|
|
28
|
+
lifecycle.active && activation.settings.shareSubagentContext,
|
|
29
|
+
owner: () => owner,
|
|
30
|
+
execute: (ctx, request, signal) =>
|
|
31
|
+
owner.executeShared!(
|
|
32
|
+
{ namespace: request.namespace, params: request.params },
|
|
33
|
+
randomUUID(),
|
|
34
|
+
ctx,
|
|
35
|
+
signal,
|
|
36
|
+
),
|
|
37
|
+
});
|
|
38
|
+
let remote: Pick<
|
|
39
|
+
import("./bridge.js").NotesBridge,
|
|
40
|
+
"lookup" | "canProject" | "projectResult"
|
|
41
|
+
> = {};
|
|
42
|
+
try {
|
|
43
|
+
const { createNotesRemoteService } = await import("./remote.js");
|
|
44
|
+
remote = createNotesRemoteService(pi);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (!optionalPccMissing(error)) throw error;
|
|
47
|
+
}
|
|
48
|
+
const family = {
|
|
49
|
+
...remote,
|
|
50
|
+
protocol: 1 as const,
|
|
51
|
+
loadSavedSession: route.loadSavedSession,
|
|
52
|
+
agentName: (ctx: ExtensionContext) => contextAgentIdentity(ctx).agentName,
|
|
53
|
+
route: async (
|
|
54
|
+
query: import("./bridge.js").LookupQuery,
|
|
55
|
+
callId: string,
|
|
56
|
+
ctx: ExtensionContext,
|
|
57
|
+
signal?: AbortSignal,
|
|
58
|
+
) => {
|
|
59
|
+
const identity = contextAgentIdentity(ctx);
|
|
60
|
+
const target = contextTargetAgent(
|
|
61
|
+
query.namespace,
|
|
62
|
+
query.params,
|
|
63
|
+
identity.agentName,
|
|
64
|
+
);
|
|
65
|
+
if (contextAgentNames(identity).includes(target)) return undefined;
|
|
66
|
+
const result = await route(
|
|
67
|
+
ctx,
|
|
68
|
+
{ ...query, sessionId: identity.sessionId, agentName: target },
|
|
69
|
+
signal,
|
|
70
|
+
);
|
|
71
|
+
if (
|
|
72
|
+
query.namespace === "history" &&
|
|
73
|
+
result &&
|
|
74
|
+
(result.details.externalNotesResponses?.length ||
|
|
75
|
+
"protectedOutputs" in result.details)
|
|
76
|
+
)
|
|
77
|
+
throw new Error(
|
|
78
|
+
"That agent returned encrypted history instead of Pi session history. Update and reload Notes on the other agent, or open its saved Pi JSONL session locally.",
|
|
79
|
+
);
|
|
80
|
+
if (!result?.details.externalNotesResponses?.length) return result;
|
|
81
|
+
const { externalNotesResponses, ...details } = result.details;
|
|
82
|
+
if (!remote.projectResult || remote.canProject?.(ctx) === false) {
|
|
83
|
+
const warning =
|
|
84
|
+
"Some notes cannot be read with this model. Available notes are shown; select a compatible Codex model to read the rest.";
|
|
85
|
+
const data = result.structuredContent;
|
|
86
|
+
if (!data || typeof data !== "object" || Array.isArray(data))
|
|
87
|
+
throw new Error(
|
|
88
|
+
"Shared Notes data is invalid. Update Notes on the other agent and retry.",
|
|
89
|
+
);
|
|
90
|
+
return {
|
|
91
|
+
...noteResult({
|
|
92
|
+
...data,
|
|
93
|
+
warnings: [
|
|
94
|
+
...("warnings" in data && Array.isArray(data["warnings"])
|
|
95
|
+
? data["warnings"]
|
|
96
|
+
: []),
|
|
97
|
+
warning,
|
|
98
|
+
],
|
|
99
|
+
}),
|
|
100
|
+
details,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return remote.projectResult(
|
|
104
|
+
{ ...result, details },
|
|
105
|
+
externalNotesResponses.map(({ bytesBase64, ...response }) => ({
|
|
106
|
+
...response,
|
|
107
|
+
bytes: Uint8Array.from(Buffer.from(bytesBase64, "base64")),
|
|
108
|
+
})),
|
|
109
|
+
callId,
|
|
110
|
+
ctx,
|
|
111
|
+
signal,
|
|
112
|
+
true,
|
|
113
|
+
);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
const stopOwner = pi.events.on(OWNER_CHANNEL, (data) => {
|
|
117
|
+
if (!data || typeof data !== "object") return;
|
|
118
|
+
const request = data as Partial<OwnerRequest>;
|
|
119
|
+
if (request.protocol === 1 && activation.available)
|
|
120
|
+
request.projection?.(owner);
|
|
121
|
+
if (
|
|
122
|
+
request.protocol === 1 &&
|
|
123
|
+
typeof request.reply === "function" &&
|
|
124
|
+
activation.available &&
|
|
125
|
+
(request.ctx
|
|
126
|
+
? modelIsActive(activation.settings, request.ctx.model)
|
|
127
|
+
: lifecycle.active)
|
|
128
|
+
)
|
|
129
|
+
request.reply(owner);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
return { family, stopOwner };
|
|
133
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionCommandContext,
|
|
5
|
+
ExtensionContext,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import type { CommsFeature } from "../guidance.js";
|
|
8
|
+
import type { OpenSettings } from "../settings-navigation.js";
|
|
9
|
+
import type { NotesRuntime } from "./index.js";
|
|
10
|
+
import {
|
|
11
|
+
legacyNotesConfigured,
|
|
12
|
+
loadNotesSettings,
|
|
13
|
+
modelIsActive,
|
|
14
|
+
type NotesSettings,
|
|
15
|
+
notesSettingsPath,
|
|
16
|
+
writeSettings,
|
|
17
|
+
} from "./settings.js";
|
|
18
|
+
import type { NotesSettingsOwner } from "./settings-command.js";
|
|
19
|
+
import type { NotesStore } from "./store.js";
|
|
20
|
+
|
|
21
|
+
/** Dormant sessions need only settings; saved continuity still owns its history cuts. */
|
|
22
|
+
function hasSavedContinuity(ctx: ExtensionContext): boolean {
|
|
23
|
+
return ctx.sessionManager.getEntries().some((entry) => {
|
|
24
|
+
if (entry.type === "custom" || entry.type === "custom_message")
|
|
25
|
+
return (
|
|
26
|
+
entry.customType.startsWith("notes-compaction:") ||
|
|
27
|
+
[
|
|
28
|
+
"codex-context-agent",
|
|
29
|
+
"codex-context-backend",
|
|
30
|
+
"codex-context-note",
|
|
31
|
+
"codex-context-note-snapshot",
|
|
32
|
+
"codex-context-window",
|
|
33
|
+
"codex-context-tree-archive",
|
|
34
|
+
"codex-note-save-marker",
|
|
35
|
+
].includes(entry.customType)
|
|
36
|
+
);
|
|
37
|
+
return (
|
|
38
|
+
entry.type === "compaction" &&
|
|
39
|
+
entry.details !== null &&
|
|
40
|
+
typeof entry.details === "object" &&
|
|
41
|
+
"pi-notes-compaction/window-checkpoint" in entry.details
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createNotesFeature(
|
|
47
|
+
pi: ExtensionAPI,
|
|
48
|
+
openSettings?: OpenSettings,
|
|
49
|
+
): CommsFeature {
|
|
50
|
+
let settings: NotesSettings | undefined;
|
|
51
|
+
let runtime: NotesRuntime | undefined;
|
|
52
|
+
let loading: Promise<NotesRuntime> | undefined;
|
|
53
|
+
let store: NotesStore | undefined;
|
|
54
|
+
let storage: Promise<NotesStore> | undefined;
|
|
55
|
+
let closed = false;
|
|
56
|
+
const ensureStore = () =>
|
|
57
|
+
(storage ??= (async () => {
|
|
58
|
+
const { NotesStore } = await import("./store.js");
|
|
59
|
+
if (closed) throw new Error("Session changed; reopen Notes settings");
|
|
60
|
+
return (store = new NotesStore(
|
|
61
|
+
join(dirname(notesSettingsPath()), "notes.sqlite"),
|
|
62
|
+
));
|
|
63
|
+
})());
|
|
64
|
+
const ensure = (ctx: ExtensionContext): Promise<NotesRuntime> => {
|
|
65
|
+
if (loading) return loading;
|
|
66
|
+
loading = (async () => {
|
|
67
|
+
const { createNotesRuntime } = await import("./index.js");
|
|
68
|
+
if (closed) throw new Error("Session changed; reopen Notes settings");
|
|
69
|
+
runtime = await createNotesRuntime(pi, await ensureStore());
|
|
70
|
+
if (closed) throw new Error("Session changed; reopen Notes settings");
|
|
71
|
+
await runtime.start(ctx, settings);
|
|
72
|
+
return runtime;
|
|
73
|
+
})();
|
|
74
|
+
return loading;
|
|
75
|
+
};
|
|
76
|
+
const owner: NotesSettingsOwner = {
|
|
77
|
+
get settings() {
|
|
78
|
+
if (!settings)
|
|
79
|
+
throw new Error(
|
|
80
|
+
"Notes settings are unavailable; fix the settings file and reload Pi",
|
|
81
|
+
);
|
|
82
|
+
return settings;
|
|
83
|
+
},
|
|
84
|
+
get runtime() {
|
|
85
|
+
return runtime;
|
|
86
|
+
},
|
|
87
|
+
async save(value, ctx) {
|
|
88
|
+
if (runtime) runtime.activation.save(value, ctx);
|
|
89
|
+
else writeSettings(notesSettingsPath(), value);
|
|
90
|
+
settings = value;
|
|
91
|
+
if (!runtime && modelIsActive(value, ctx.model)) await ensure(ctx);
|
|
92
|
+
},
|
|
93
|
+
start: ensure,
|
|
94
|
+
};
|
|
95
|
+
const open = async (
|
|
96
|
+
ctx: ExtensionCommandContext,
|
|
97
|
+
initial: "notes" | "roles" = "notes",
|
|
98
|
+
) => {
|
|
99
|
+
const { createNotesSettings } = await import("./settings-command.js");
|
|
100
|
+
return createNotesSettings(
|
|
101
|
+
owner,
|
|
102
|
+
await ensureStore(),
|
|
103
|
+
notesSettingsPath(),
|
|
104
|
+
Boolean(openSettings),
|
|
105
|
+
)(ctx, initial);
|
|
106
|
+
};
|
|
107
|
+
const routeSettings: OpenSettings =
|
|
108
|
+
openSettings ??
|
|
109
|
+
(async (ctx) => {
|
|
110
|
+
await open(ctx);
|
|
111
|
+
});
|
|
112
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
113
|
+
try {
|
|
114
|
+
settings = loadNotesSettings(ctx);
|
|
115
|
+
writeSettings(notesSettingsPath(), settings);
|
|
116
|
+
if (
|
|
117
|
+
modelIsActive(settings, ctx.model) ||
|
|
118
|
+
hasSavedContinuity(ctx) ||
|
|
119
|
+
legacyNotesConfigured(ctx)
|
|
120
|
+
)
|
|
121
|
+
await ensure(ctx);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
ctx.ui.notify(
|
|
124
|
+
`Notes continuity inactive: ${error instanceof Error ? error.message : String(error)}`,
|
|
125
|
+
"warning",
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
pi.on("model_select", async (_event, ctx) => {
|
|
130
|
+
if (!runtime && settings && modelIsActive(settings, ctx.model))
|
|
131
|
+
await ensure(ctx);
|
|
132
|
+
});
|
|
133
|
+
pi.on("session_shutdown", () => {
|
|
134
|
+
closed = true;
|
|
135
|
+
store?.close();
|
|
136
|
+
});
|
|
137
|
+
pi.registerCommand("notes", {
|
|
138
|
+
description: "Manage local notes and context rollover",
|
|
139
|
+
handler: async (args, ctx) => {
|
|
140
|
+
if (args.trim()) {
|
|
141
|
+
ctx.ui.notify("Use /notes to open settings.", "info");
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
await routeSettings(ctx, "notes");
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
pi.registerCommand("roles", {
|
|
148
|
+
description: "View model roles and JSON configuration guidance",
|
|
149
|
+
handler: async (args, ctx) => {
|
|
150
|
+
if (args.trim()) {
|
|
151
|
+
ctx.ui.notify(
|
|
152
|
+
"Use /roles to view roles and JSON configuration guidance.",
|
|
153
|
+
"info",
|
|
154
|
+
);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const next = await open(ctx, "roles");
|
|
158
|
+
if (next) await routeSettings(ctx, next);
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
return {
|
|
162
|
+
enabled: () => runtime?.lifecycle.active ?? false,
|
|
163
|
+
gptPromptMode: () => runtime?.lifecycle.gptPromptMode() ?? false,
|
|
164
|
+
async openSettings(ctx) {
|
|
165
|
+
return open(ctx);
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type {
|
|
3
|
+
AgentSettledEvent,
|
|
4
|
+
BeforeAgentStartEvent,
|
|
5
|
+
CustomMessageEntryDraft,
|
|
6
|
+
ExtensionAPI,
|
|
7
|
+
ExtensionContext,
|
|
8
|
+
} from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
10
|
+
import type { NotesLifecycle } from "./lifecycle.js";
|
|
11
|
+
|
|
12
|
+
const CHECKPOINT_MESSAGE = "notes-compaction:checkpoint:v1";
|
|
13
|
+
|
|
14
|
+
export function registerCheckpointRenderer(pi: ExtensionAPI): void {
|
|
15
|
+
pi.registerMessageRenderer<{ instructions?: string }>(
|
|
16
|
+
CHECKPOINT_MESSAGE,
|
|
17
|
+
(message, _options, theme) => {
|
|
18
|
+
const instructions =
|
|
19
|
+
message.details?.instructions ??
|
|
20
|
+
(typeof message.content === "string"
|
|
21
|
+
? message.content
|
|
22
|
+
.split("\n\nCheckpoint guidance:\n")
|
|
23
|
+
.slice(1)
|
|
24
|
+
.join("\n\nCheckpoint guidance:\n")
|
|
25
|
+
: "");
|
|
26
|
+
return new Text(
|
|
27
|
+
theme.fg("customMessageLabel", "Context checkpoint") +
|
|
28
|
+
(instructions ? `\n${instructions}` : ""),
|
|
29
|
+
0,
|
|
30
|
+
0,
|
|
31
|
+
);
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Checkpoint {
|
|
37
|
+
sessionId: string;
|
|
38
|
+
windowId: string;
|
|
39
|
+
requestId: string;
|
|
40
|
+
runId: string | undefined;
|
|
41
|
+
outcome: "completed" | "aborted" | "error" | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Owns checkpoint request/run association and completed-write eligibility.
|
|
45
|
+
export class FreshCheckpoint {
|
|
46
|
+
private checkpoint: Checkpoint | undefined;
|
|
47
|
+
private readonly host: Pick<NotesLifecycle, "windows" | "fresh">;
|
|
48
|
+
private readonly sendMessage: (
|
|
49
|
+
message: Omit<CustomMessageEntryDraft, "type">,
|
|
50
|
+
options: { triggerTurn: boolean },
|
|
51
|
+
) => void;
|
|
52
|
+
constructor(
|
|
53
|
+
host: FreshCheckpoint["host"],
|
|
54
|
+
sendMessage: FreshCheckpoint["sendMessage"],
|
|
55
|
+
) {
|
|
56
|
+
this.host = host;
|
|
57
|
+
this.sendMessage = sendMessage;
|
|
58
|
+
}
|
|
59
|
+
get pending(): boolean {
|
|
60
|
+
return Boolean(this.checkpoint);
|
|
61
|
+
}
|
|
62
|
+
reset(): void {
|
|
63
|
+
this.checkpoint = undefined;
|
|
64
|
+
}
|
|
65
|
+
take(): Checkpoint | undefined {
|
|
66
|
+
const checkpoint = this.checkpoint;
|
|
67
|
+
this.checkpoint = undefined;
|
|
68
|
+
return checkpoint;
|
|
69
|
+
}
|
|
70
|
+
bind(event: BeforeAgentStartEvent, runId: string): void {
|
|
71
|
+
if (
|
|
72
|
+
this.checkpoint &&
|
|
73
|
+
event.messageType === "custom" &&
|
|
74
|
+
event.message.customType === CHECKPOINT_MESSAGE &&
|
|
75
|
+
(event.message.details as { requestId?: string } | undefined)
|
|
76
|
+
?.requestId === this.checkpoint.requestId
|
|
77
|
+
)
|
|
78
|
+
this.checkpoint.runId = runId;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
outcome(outcome: "completed" | "aborted" | "error"): void {
|
|
82
|
+
if (this.checkpoint) this.checkpoint.outcome = outcome;
|
|
83
|
+
}
|
|
84
|
+
start(ctx: ExtensionContext, instructions?: string): void {
|
|
85
|
+
if (!ctx.isIdle() || !this.host.windows.current || this.checkpoint)
|
|
86
|
+
throw new Error("A checkpoint cannot start until the session settles");
|
|
87
|
+
const requestId = randomUUID();
|
|
88
|
+
this.checkpoint = {
|
|
89
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
90
|
+
windowId: this.host.windows.current.currentWindowId,
|
|
91
|
+
requestId,
|
|
92
|
+
runId: undefined,
|
|
93
|
+
outcome: undefined,
|
|
94
|
+
};
|
|
95
|
+
try {
|
|
96
|
+
this.sendMessage(
|
|
97
|
+
{
|
|
98
|
+
customType: CHECKPOINT_MESSAGE,
|
|
99
|
+
details: { requestId, instructions: instructions?.trim() },
|
|
100
|
+
content: `<context_window_reminder>\nSave task state in notes, then finish reply. New window opens automatically after reply; do not call new_context.\n</context_window_reminder>${instructions?.trim() ? `\n\nCheckpoint guidance:\n${instructions}` : ""}`,
|
|
101
|
+
display: true,
|
|
102
|
+
},
|
|
103
|
+
{ triggerTurn: true },
|
|
104
|
+
);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
this.checkpoint = undefined;
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
permitsRollover(
|
|
112
|
+
checkpoint: Checkpoint,
|
|
113
|
+
event: AgentSettledEvent,
|
|
114
|
+
ctx: ExtensionContext,
|
|
115
|
+
): boolean {
|
|
116
|
+
const valid =
|
|
117
|
+
checkpoint.sessionId === ctx.sessionManager.getSessionId() &&
|
|
118
|
+
checkpoint.windowId === this.host.windows.current?.currentWindowId;
|
|
119
|
+
const aborted =
|
|
120
|
+
event.aborted ||
|
|
121
|
+
checkpoint.outcome === "aborted" ||
|
|
122
|
+
checkpoint.outcome === undefined;
|
|
123
|
+
if (!valid || aborted) {
|
|
124
|
+
ctx.ui.notify(
|
|
125
|
+
"Checkpoint cancelled. The old context remains.",
|
|
126
|
+
"warning",
|
|
127
|
+
);
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (
|
|
131
|
+
checkpoint.outcome !== "completed" ||
|
|
132
|
+
!checkpoint.runId ||
|
|
133
|
+
!this.host.fresh(ctx, checkpoint.runId)
|
|
134
|
+
) {
|
|
135
|
+
ctx.ui.notify(
|
|
136
|
+
"Rollover did not start. No fresh note was saved in the completed checkpoint. The old context remains.",
|
|
137
|
+
"warning",
|
|
138
|
+
);
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
}
|