@howaboua/pi-agent-comms 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -57,7 +57,7 @@ export function createTools(
57
57
  lifecycle: NotesLifecycle,
58
58
  search: HistorySearch,
59
59
  roles: ReturnType<typeof createModelRoles>,
60
- ): ToolDefinition[] {
60
+ ) {
61
61
  const assertActive = () => {
62
62
  if (!lifecycle.active)
63
63
  throw new Error(
@@ -65,6 +65,41 @@ export function createTools(
65
65
  );
66
66
  };
67
67
  const queries = createQueryExecutor(pi, store, lifecycle, search);
68
+ const requestContext = async (
69
+ id: string,
70
+ input: unknown,
71
+ signal: AbortSignal | undefined,
72
+ ctx: ExtensionContext,
73
+ parentCallId?: string,
74
+ ) => {
75
+ assertActive();
76
+ if (signal?.aborted) throw new Error("Rollover cancelled");
77
+ const { role, resume = true } = prepareNewContextArguments(input);
78
+ const handoff =
79
+ role === undefined ? undefined : await roles.prepare(role, ctx);
80
+ if (signal?.aborted) throw new Error("Rollover cancelled");
81
+ const started = lifecycle.requestRollover(
82
+ ctx,
83
+ id,
84
+ handoff,
85
+ resume,
86
+ parentCallId,
87
+ );
88
+ if (started && resume) ctx.abort();
89
+ return {
90
+ ...result({
91
+ started,
92
+ message: started
93
+ ? !resume
94
+ ? "Window request queued without continuation. Finish your reply"
95
+ : lifecycle.newWindowRequests === "pi-limits"
96
+ ? "Window request queued"
97
+ : "New window queued"
98
+ : "Checkpoint or rollover is already pending. Save notes and finish your response",
99
+ }),
100
+ ...(started && resume ? { terminate: true } : {}),
101
+ };
102
+ };
68
103
  const tools: ToolDefinition[] = [
69
104
  {
70
105
  name: "notes",
@@ -97,27 +132,8 @@ export function createTools(
97
132
  prepareArguments: prepareNewContextArguments,
98
133
  exposure: "model-only",
99
134
  executionMode: "sequential",
100
- async execute(id, input, signal, _update, ctx) {
101
- assertActive();
102
- if (signal?.aborted) throw new Error("Rollover cancelled");
103
- const { role } = prepareNewContextArguments(input);
104
- const handoff =
105
- role === undefined ? undefined : await roles.prepare(role, ctx);
106
- if (signal?.aborted) throw new Error("Rollover cancelled");
107
- const started = lifecycle.requestRollover(ctx, id, handoff);
108
- if (started) ctx.abort();
109
- return {
110
- ...result({
111
- started,
112
- message: started
113
- ? lifecycle.newWindowRequests === "pi-limits"
114
- ? "Window request queued"
115
- : "New window queued"
116
- : "Checkpoint or rollover is already pending. Save notes and finish your response",
117
- }),
118
- ...(started ? { terminate: true } : {}),
119
- };
120
- },
135
+ execute: (id, input, signal, _update, ctx) =>
136
+ requestContext(id, input, signal, ctx),
121
137
  },
122
138
  {
123
139
  name: "get_context_remaining",
@@ -136,13 +152,19 @@ export function createTools(
136
152
  },
137
153
  ];
138
154
  const newContext = tools.find((tool) => tool.name === "new_context")!;
139
- let advertisedRoles = "[]";
140
- roles.onRoleIdsChanged((ids) => {
141
- const key = JSON.stringify(ids);
142
- if (key === advertisedRoles) return;
143
- advertisedRoles = key;
144
- newContext.parameters = newContextSchema(ids);
155
+ let roleIds: string[] = [];
156
+ let advertised = "";
157
+ const refresh = () => {
158
+ const schema = newContextSchema(roleIds, lifecycle.taskCompletionRollover);
159
+ const key = JSON.stringify(schema);
160
+ if (key === advertised) return;
161
+ advertised = key;
162
+ newContext.parameters = schema;
145
163
  pi.registerTool(newContext);
164
+ };
165
+ roles.onRoleIdsChanged((ids) => {
166
+ roleIds = ids;
167
+ refresh();
146
168
  });
147
- return tools;
169
+ return { tools, refresh, requestContext };
148
170
  }
@@ -95,19 +95,18 @@ function hideArchiveSummaries(
95
95
  hidden: ReadonlySet<string>,
96
96
  ): readonly SessionEntry[] {
97
97
  if (hidden.size === 0) return entries;
98
- return entries.map(
99
- (entry): SessionEntry =>
100
- entry.type === "branch_summary" && hidden.has(entry.id)
101
- ? // Keep entry IDs as replay cut points without sending archive summaries.
102
- {
103
- type: "custom",
104
- id: entry.id,
105
- parentId: entry.parentId,
106
- timestamp: entry.timestamp,
107
- customType: "codex-context-tree-summary",
108
- data: {},
109
- }
110
- : entry,
98
+ return entries.map((entry): SessionEntry =>
99
+ entry.type === "branch_summary" && hidden.has(entry.id)
100
+ ? // Keep entry IDs as replay cut points without sending archive summaries.
101
+ {
102
+ type: "custom",
103
+ id: entry.id,
104
+ parentId: entry.parentId,
105
+ timestamp: entry.timestamp,
106
+ customType: "codex-context-tree-summary",
107
+ data: {},
108
+ }
109
+ : entry,
111
110
  );
112
111
  }
113
112
 
@@ -256,17 +256,16 @@ export class Windows {
256
256
  if (entries[i]!.type === "compaction" && !isWindowCheckpoint(entries[i]!))
257
257
  boundary = -1;
258
258
  }
259
- return entries.slice(Math.max(0, boundary)).map(
260
- (entry): SessionEntry =>
261
- isWindowCheckpoint(entry)
262
- ? {
263
- type: "custom",
264
- id: entry.id,
265
- parentId: entry.parentId,
266
- timestamp: entry.timestamp,
267
- customType: "notes-compaction:window-checkpoint",
268
- }
269
- : entry,
259
+ return entries.slice(Math.max(0, boundary)).map((entry): SessionEntry =>
260
+ isWindowCheckpoint(entry)
261
+ ? {
262
+ type: "custom",
263
+ id: entry.id,
264
+ parentId: entry.parentId,
265
+ timestamp: entry.timestamp,
266
+ customType: "notes-compaction:window-checkpoint",
267
+ }
268
+ : entry,
270
269
  );
271
270
  }
272
271
  }