opencode-subagent-tools 0.0.0-stage → 0.2.0
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 +49 -0
- package/LICENSE +21 -0
- package/README.md +118 -2
- package/SECURITY.md +36 -0
- package/SKILL.md +345 -0
- package/docs/configuration.md +238 -0
- package/docs/development.md +82 -0
- package/docs/installation.md +131 -0
- package/docs/limitations.md +155 -0
- package/docs/reference.md +398 -0
- package/docs/security-model.md +79 -0
- package/package.json +74 -4
- package/src/access.ts +236 -0
- package/src/background.ts +591 -0
- package/src/config.ts +143 -0
- package/src/cursor.ts +114 -0
- package/src/discovery.ts +159 -0
- package/src/errors.ts +18 -0
- package/src/events.ts +236 -0
- package/src/format.ts +34 -0
- package/src/index.ts +129 -0
- package/src/interrupt.ts +20 -0
- package/src/live-state.ts +113 -0
- package/src/messages.ts +246 -0
- package/src/permissions.ts +62 -0
- package/src/session-row.ts +116 -0
- package/src/session.ts +141 -0
- package/src/skill.ts +57 -0
- package/src/status.ts +119 -0
- package/src/text.ts +42 -0
- package/src/tokens.ts +171 -0
- package/src/tools/cancel-descendants.ts +660 -0
- package/src/tools/cancel.ts +271 -0
- package/src/tools/index.ts +41 -0
- package/src/tools/input.ts +75 -0
- package/src/tools/list.ts +608 -0
- package/src/tools/names.ts +11 -0
- package/src/tools/output.ts +169 -0
- package/src/tools/send.ts +369 -0
- package/src/tools/usage.ts +434 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import type { ToolEditor } from "@opencode/plugin/promise/tool";
|
|
2
|
+
import type { Access } from "../access";
|
|
3
|
+
import {
|
|
4
|
+
BACKGROUND_JOB_SCHEMA,
|
|
5
|
+
backgroundJobLabel,
|
|
6
|
+
type BackgroundJobInfo,
|
|
7
|
+
} from "../background";
|
|
8
|
+
import { limitInteger, type Config } from "../config";
|
|
9
|
+
import { encodeCursor, isAfterCursor, readCursor } from "../cursor";
|
|
10
|
+
import type { Discovery } from "../discovery";
|
|
11
|
+
import { toolError } from "../errors";
|
|
12
|
+
import { age, ageSeconds, compactNumber, formatCost } from "../format";
|
|
13
|
+
import { MAX_PROMPT_PREVIEW_LENGTH } from "../session";
|
|
14
|
+
import type { SessionRow } from "../session-row";
|
|
15
|
+
import { RUN_STATUS, RUN_STATUSES, isRunStatus, type RunStatus } from "../status";
|
|
16
|
+
import { clip, isUnsafeControlCode } from "../text";
|
|
17
|
+
import { TOKEN_USAGE_SCHEMA, tokensUsed, type TokenUsage } from "../tokens";
|
|
18
|
+
import { readOptionalSessionID, readToolInput } from "./input";
|
|
19
|
+
import { TOOL } from "./names";
|
|
20
|
+
import { formatPropertyDescription, resolveToolOutputStrategy } from "./output";
|
|
21
|
+
|
|
22
|
+
const RUN_STATUS_COLUMN_WIDTH = Math.max(...RUN_STATUSES.map((status) => status.length));
|
|
23
|
+
const DISCOVERY_WARNING = "discovery was incomplete; some subagents may be missing.";
|
|
24
|
+
const DISCOVERY_WARNING_TEXT = `\nWarning: ${DISCOVERY_WARNING}`;
|
|
25
|
+
|
|
26
|
+
export interface ListRowUsage {
|
|
27
|
+
costUSD?: number;
|
|
28
|
+
tokens?: TokenUsage;
|
|
29
|
+
tokensUsed?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ListRowOutput {
|
|
33
|
+
id: string;
|
|
34
|
+
parentID?: string;
|
|
35
|
+
agent?: string;
|
|
36
|
+
title?: string;
|
|
37
|
+
titleTruncated?: boolean;
|
|
38
|
+
status: RunStatus;
|
|
39
|
+
/** The session's own execution or background work; descendants are excluded. */
|
|
40
|
+
active: boolean;
|
|
41
|
+
/** The session or any discovered descendant is active. */
|
|
42
|
+
subtreeActive: boolean;
|
|
43
|
+
activeDescendants?: boolean;
|
|
44
|
+
backgroundJob?: BackgroundJobInfo;
|
|
45
|
+
ageSeconds?: number;
|
|
46
|
+
launchedAt: number;
|
|
47
|
+
/** Usage is as of the last persisted step; active or unknown children may lag. */
|
|
48
|
+
usageMayLag?: boolean;
|
|
49
|
+
usage?: ListRowUsage;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ListOutput {
|
|
53
|
+
root: string;
|
|
54
|
+
statuses: RunStatus[];
|
|
55
|
+
discoveryComplete: boolean;
|
|
56
|
+
warning?: string;
|
|
57
|
+
known: number;
|
|
58
|
+
activeCount: number;
|
|
59
|
+
subtreeActiveCount: number;
|
|
60
|
+
matching: number;
|
|
61
|
+
window: number;
|
|
62
|
+
remaining: number;
|
|
63
|
+
remainingPages: number;
|
|
64
|
+
cursor?: string;
|
|
65
|
+
counts: Record<RunStatus, number>;
|
|
66
|
+
rows: ListRowOutput[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const LIST_ROW_USAGE_SCHEMA = {
|
|
70
|
+
type: "object",
|
|
71
|
+
properties: {
|
|
72
|
+
costUSD: { type: "number" },
|
|
73
|
+
tokens: TOKEN_USAGE_SCHEMA,
|
|
74
|
+
tokensUsed: { type: "integer" },
|
|
75
|
+
},
|
|
76
|
+
additionalProperties: false,
|
|
77
|
+
} as const;
|
|
78
|
+
|
|
79
|
+
const LIST_OUTPUT_SCHEMA = {
|
|
80
|
+
type: "object",
|
|
81
|
+
properties: {
|
|
82
|
+
root: { type: "string" },
|
|
83
|
+
statuses: {
|
|
84
|
+
type: "array",
|
|
85
|
+
items: { type: "string", enum: [...RUN_STATUSES] },
|
|
86
|
+
},
|
|
87
|
+
discoveryComplete: {
|
|
88
|
+
type: "boolean",
|
|
89
|
+
description:
|
|
90
|
+
"Whether discovery walked the whole subtree, not whether all rows fit on this page.",
|
|
91
|
+
},
|
|
92
|
+
warning: { type: "string" },
|
|
93
|
+
known: {
|
|
94
|
+
type: "integer",
|
|
95
|
+
description:
|
|
96
|
+
"All discovered sessions before the status and activity filters and cursor.",
|
|
97
|
+
},
|
|
98
|
+
activeCount: {
|
|
99
|
+
type: "integer",
|
|
100
|
+
description:
|
|
101
|
+
"All discovered sessions with their own active execution or background job, before filters.",
|
|
102
|
+
},
|
|
103
|
+
subtreeActiveCount: {
|
|
104
|
+
type: "integer",
|
|
105
|
+
description:
|
|
106
|
+
"All discovered sessions that are active or have an active descendant, before filters.",
|
|
107
|
+
},
|
|
108
|
+
matching: {
|
|
109
|
+
type: "integer",
|
|
110
|
+
description:
|
|
111
|
+
"Matching sessions in the current discovery snapshot, before the cursor.",
|
|
112
|
+
},
|
|
113
|
+
window: {
|
|
114
|
+
type: "integer",
|
|
115
|
+
description:
|
|
116
|
+
"Matching sessions after the cursor; equals matching on the first page.",
|
|
117
|
+
},
|
|
118
|
+
remaining: {
|
|
119
|
+
type: "integer",
|
|
120
|
+
description: "Matching sessions in the window not returned on this page.",
|
|
121
|
+
},
|
|
122
|
+
remainingPages: {
|
|
123
|
+
type: "integer",
|
|
124
|
+
description: "Pages left in this cursor walk.",
|
|
125
|
+
},
|
|
126
|
+
cursor: { type: "string" },
|
|
127
|
+
counts: {
|
|
128
|
+
type: "object",
|
|
129
|
+
description:
|
|
130
|
+
"Counts for all discovered sessions, before the status and activity filters and cursor.",
|
|
131
|
+
properties: Object.fromEntries(
|
|
132
|
+
RUN_STATUSES.map((status) => [status, { type: "integer" }]),
|
|
133
|
+
),
|
|
134
|
+
required: [...RUN_STATUSES],
|
|
135
|
+
additionalProperties: false,
|
|
136
|
+
},
|
|
137
|
+
rows: {
|
|
138
|
+
type: "array",
|
|
139
|
+
items: {
|
|
140
|
+
type: "object",
|
|
141
|
+
properties: {
|
|
142
|
+
id: { type: "string" },
|
|
143
|
+
parentID: { type: "string" },
|
|
144
|
+
agent: { type: "string" },
|
|
145
|
+
title: { type: "string" },
|
|
146
|
+
titleTruncated: { type: "boolean" },
|
|
147
|
+
status: { type: "string", enum: [...RUN_STATUSES] },
|
|
148
|
+
active: {
|
|
149
|
+
type: "boolean",
|
|
150
|
+
description:
|
|
151
|
+
"True when the session itself is running, unknown, or owns a background job.",
|
|
152
|
+
},
|
|
153
|
+
subtreeActive: {
|
|
154
|
+
type: "boolean",
|
|
155
|
+
description: "True when the session or any discovered descendant is active.",
|
|
156
|
+
},
|
|
157
|
+
activeDescendants: {
|
|
158
|
+
type: "boolean",
|
|
159
|
+
description: "True only when a discovered descendant is active.",
|
|
160
|
+
},
|
|
161
|
+
backgroundJob: BACKGROUND_JOB_SCHEMA,
|
|
162
|
+
ageSeconds: { type: "integer" },
|
|
163
|
+
launchedAt: { type: "number" },
|
|
164
|
+
usageMayLag: {
|
|
165
|
+
type: "boolean",
|
|
166
|
+
description: "True when the session is active or unknown; usage may lag.",
|
|
167
|
+
},
|
|
168
|
+
usage: LIST_ROW_USAGE_SCHEMA,
|
|
169
|
+
},
|
|
170
|
+
required: ["id", "status", "active", "subtreeActive", "launchedAt"],
|
|
171
|
+
additionalProperties: false,
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
required: [
|
|
176
|
+
"root",
|
|
177
|
+
"statuses",
|
|
178
|
+
"discoveryComplete",
|
|
179
|
+
"known",
|
|
180
|
+
"activeCount",
|
|
181
|
+
"subtreeActiveCount",
|
|
182
|
+
"matching",
|
|
183
|
+
"window",
|
|
184
|
+
"remaining",
|
|
185
|
+
"remainingPages",
|
|
186
|
+
"counts",
|
|
187
|
+
"rows",
|
|
188
|
+
],
|
|
189
|
+
additionalProperties: false,
|
|
190
|
+
} as const;
|
|
191
|
+
|
|
192
|
+
const requestedStatuses = (
|
|
193
|
+
value: unknown,
|
|
194
|
+
): { statuses: Set<RunStatus>; invalid: string[]; error?: string } => {
|
|
195
|
+
if (value === undefined) {
|
|
196
|
+
return { statuses: new Set(RUN_STATUSES), invalid: [] };
|
|
197
|
+
}
|
|
198
|
+
if (!Array.isArray(value)) {
|
|
199
|
+
return {
|
|
200
|
+
statuses: new Set<RunStatus>(),
|
|
201
|
+
invalid: [],
|
|
202
|
+
error: "statuses must be an array of statuses; omit it to include all.",
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const valid: RunStatus[] = [];
|
|
206
|
+
const invalid: string[] = [];
|
|
207
|
+
for (const raw of value) {
|
|
208
|
+
if (typeof raw !== "string") {
|
|
209
|
+
invalid.push(String(raw));
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const status = raw.trim().toLowerCase();
|
|
213
|
+
if (isRunStatus(status)) valid.push(status);
|
|
214
|
+
else if (status.length > 0) invalid.push(raw);
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
statuses: new Set(valid),
|
|
218
|
+
invalid,
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const statusesLabel = (statuses: Set<RunStatus>): string =>
|
|
223
|
+
statuses.size === RUN_STATUSES.length ? "all" : [...statuses].join(", ");
|
|
224
|
+
|
|
225
|
+
const readOptionalBoolean = (
|
|
226
|
+
value: unknown,
|
|
227
|
+
field: string,
|
|
228
|
+
): { value?: boolean; error?: string } => {
|
|
229
|
+
if (value === undefined) return {};
|
|
230
|
+
if (typeof value === "boolean") return { value };
|
|
231
|
+
return { error: `${field} must be a boolean; omit it to include all rows.` };
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const filterDescription = (
|
|
235
|
+
statuses: Set<RunStatus>,
|
|
236
|
+
active: boolean | undefined,
|
|
237
|
+
subtreeActive: boolean | undefined,
|
|
238
|
+
): string => {
|
|
239
|
+
const parts: string[] = [];
|
|
240
|
+
if (statuses.size !== RUN_STATUSES.length) {
|
|
241
|
+
parts.push(`statuses: ${statusesLabel(statuses)}`);
|
|
242
|
+
}
|
|
243
|
+
if (active !== undefined) parts.push(`active: ${active}`);
|
|
244
|
+
if (subtreeActive !== undefined) parts.push(`subtreeActive: ${subtreeActive}`);
|
|
245
|
+
return parts.length > 0 ? parts.join("; ") : "the current filters";
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const escapeText = (value: string): string =>
|
|
249
|
+
Array.from(
|
|
250
|
+
value
|
|
251
|
+
.replace(/\\/g, "\\\\")
|
|
252
|
+
.replace(/"/g, '\\"')
|
|
253
|
+
.replace(/[\r\n\t]/g, " "),
|
|
254
|
+
)
|
|
255
|
+
.map((char) => (isUnsafeControlCode(char.codePointAt(0) ?? 0) ? "?" : char))
|
|
256
|
+
.join("");
|
|
257
|
+
|
|
258
|
+
const rowText = (row: SessionRow): string => {
|
|
259
|
+
const usageMayLag = row.active || row.status === RUN_STATUS.unknown;
|
|
260
|
+
const title = clip(row.title, MAX_PROMPT_PREVIEW_LENGTH);
|
|
261
|
+
const activity = row.active ? "active" : row.subtreeActive ? "subtree" : "";
|
|
262
|
+
const background = row.backgroundJob
|
|
263
|
+
? `background=${backgroundJobLabel(row.backgroundJob)}`
|
|
264
|
+
: "";
|
|
265
|
+
return [
|
|
266
|
+
escapeText(row.id),
|
|
267
|
+
row.status.padEnd(RUN_STATUS_COLUMN_WIDTH),
|
|
268
|
+
activity,
|
|
269
|
+
background,
|
|
270
|
+
row.activeDescendants ? "descendants=active" : "",
|
|
271
|
+
`agent=${escapeText(row.agent ?? "?")}`,
|
|
272
|
+
`age=${row.launchedAt > 0 ? age(row.launchedAt) : "?"}`,
|
|
273
|
+
row.cost !== undefined ? `cost=${formatCost(row.cost)}` : "",
|
|
274
|
+
row.tokens ? `used=${compactNumber(tokensUsed(row.tokens))}` : "",
|
|
275
|
+
usageMayLag ? "usage=lagging" : "",
|
|
276
|
+
row.parentID ? `parent=${escapeText(row.parentID)}` : "parent=?",
|
|
277
|
+
title ? `title="${escapeText(title)}"` : "",
|
|
278
|
+
]
|
|
279
|
+
.filter(Boolean)
|
|
280
|
+
.join(" ");
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const countStatuses = (rows: readonly SessionRow[]): Record<RunStatus, number> => {
|
|
284
|
+
const counts = Object.fromEntries(RUN_STATUSES.map((status) => [status, 0])) as Record<
|
|
285
|
+
RunStatus,
|
|
286
|
+
number
|
|
287
|
+
>;
|
|
288
|
+
for (const row of rows) counts[row.status]++;
|
|
289
|
+
return counts;
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const rowOutput = (row: SessionRow): ListRowOutput => {
|
|
293
|
+
const output: ListRowOutput = {
|
|
294
|
+
id: row.id,
|
|
295
|
+
status: row.status,
|
|
296
|
+
active: row.active,
|
|
297
|
+
subtreeActive: row.subtreeActive,
|
|
298
|
+
launchedAt: row.launchedAt,
|
|
299
|
+
};
|
|
300
|
+
if (row.parentID !== undefined) output.parentID = row.parentID;
|
|
301
|
+
if (row.agent !== undefined) output.agent = row.agent;
|
|
302
|
+
if (row.title !== undefined) output.title = row.title;
|
|
303
|
+
if (row.titleTruncated) output.titleTruncated = true;
|
|
304
|
+
if (row.activeDescendants) output.activeDescendants = true;
|
|
305
|
+
if (row.backgroundJob) output.backgroundJob = row.backgroundJob;
|
|
306
|
+
if (row.launchedAt > 0) output.ageSeconds = ageSeconds(row.launchedAt);
|
|
307
|
+
if (row.active || row.status === RUN_STATUS.unknown) {
|
|
308
|
+
output.usageMayLag = true;
|
|
309
|
+
}
|
|
310
|
+
if (row.cost !== undefined || row.tokens) {
|
|
311
|
+
const usage: ListRowUsage = {};
|
|
312
|
+
if (row.cost !== undefined) usage.costUSD = row.cost;
|
|
313
|
+
if (row.tokens) {
|
|
314
|
+
usage.tokens = row.tokens;
|
|
315
|
+
usage.tokensUsed = tokensUsed(row.tokens);
|
|
316
|
+
}
|
|
317
|
+
output.usage = usage;
|
|
318
|
+
}
|
|
319
|
+
return output;
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
interface PaginationSummary {
|
|
323
|
+
shown: number;
|
|
324
|
+
afterCursor: number;
|
|
325
|
+
allMatches: number;
|
|
326
|
+
limit: number;
|
|
327
|
+
remainingPages: number;
|
|
328
|
+
nextCursor?: string;
|
|
329
|
+
hasCursor: boolean;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const paginationSummary = ({
|
|
333
|
+
shown,
|
|
334
|
+
afterCursor,
|
|
335
|
+
allMatches,
|
|
336
|
+
limit,
|
|
337
|
+
remainingPages,
|
|
338
|
+
nextCursor,
|
|
339
|
+
hasCursor,
|
|
340
|
+
}: PaginationSummary): string => {
|
|
341
|
+
const pageLabel =
|
|
342
|
+
remainingPages === 0
|
|
343
|
+
? "last page"
|
|
344
|
+
: `${remainingPages} ${remainingPages === 1 ? "page" : "pages"} remaining`;
|
|
345
|
+
if (nextCursor) {
|
|
346
|
+
if (hasCursor) {
|
|
347
|
+
return `\nShowing ${shown} of ${afterCursor} in this cursor window (${allMatches} matching total); ${pageLabel}. Use cursor ${nextCursor} to continue.`;
|
|
348
|
+
}
|
|
349
|
+
return `\nShowing ${shown} of ${allMatches} matches (limit ${limit}); ${pageLabel}. Use cursor ${nextCursor} to continue.`;
|
|
350
|
+
}
|
|
351
|
+
if (hasCursor) {
|
|
352
|
+
return `\nShowing ${shown} of ${afterCursor} in this cursor window (${allMatches} matching total); ${pageLabel}.`;
|
|
353
|
+
}
|
|
354
|
+
return "";
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
export interface ListDeps {
|
|
358
|
+
config: Config;
|
|
359
|
+
access: Access;
|
|
360
|
+
discovery: Discovery;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export const registerListTool = (
|
|
364
|
+
editor: ToolEditor,
|
|
365
|
+
{ config, access, discovery }: ListDeps,
|
|
366
|
+
) => {
|
|
367
|
+
const { allowedOutputFormats, limits } = config;
|
|
368
|
+
const outputStrategy = resolveToolOutputStrategy(allowedOutputFormats, TOOL.list);
|
|
369
|
+
|
|
370
|
+
editor.add({
|
|
371
|
+
name: TOOL.list,
|
|
372
|
+
description:
|
|
373
|
+
"List subagent sessions from the server's session tree, newest launch first, with observed status and activity. " +
|
|
374
|
+
"Defaults to the caller's subtree; pass parentID for any subtree. " +
|
|
375
|
+
"`status` describes the last or current execution; use `active` and `subtreeActive` to find work that is still in progress. " +
|
|
376
|
+
"By default all statuses are included; pass statuses, active, or subtreeActive to filter, and use limit/cursor to page through large sets. " +
|
|
377
|
+
"Rows include status, activity, background jobs, age, cost, and cumulative token usage when available. " +
|
|
378
|
+
"Discovery is limited, so results can be incomplete; a warning is included when that is possible.",
|
|
379
|
+
input: {
|
|
380
|
+
type: "object",
|
|
381
|
+
properties: {
|
|
382
|
+
parentID: {
|
|
383
|
+
type: "string",
|
|
384
|
+
description:
|
|
385
|
+
"Parent session ID. Defaults to the calling session; any parent is accepted.",
|
|
386
|
+
},
|
|
387
|
+
statuses: {
|
|
388
|
+
type: "array",
|
|
389
|
+
minItems: 1,
|
|
390
|
+
maxItems: RUN_STATUSES.length,
|
|
391
|
+
items: { type: "string", enum: [...RUN_STATUSES] },
|
|
392
|
+
description:
|
|
393
|
+
"Execution statuses to include. `finished` means the last execution succeeded, not that the delegated task is complete. Omit to include all statuses; an empty array is rejected.",
|
|
394
|
+
},
|
|
395
|
+
active: {
|
|
396
|
+
type: "boolean",
|
|
397
|
+
description:
|
|
398
|
+
"Filter on the session's own activity: running, unknown, or owning a background job. Omit to include both active and inactive rows.",
|
|
399
|
+
},
|
|
400
|
+
subtreeActive: {
|
|
401
|
+
type: "boolean",
|
|
402
|
+
description:
|
|
403
|
+
"Filter on the session or any discovered descendant. Omit to include both active and inactive subtrees.",
|
|
404
|
+
},
|
|
405
|
+
...outputStrategy.formatProperty(
|
|
406
|
+
formatPropertyDescription(outputStrategy, "the human-readable table"),
|
|
407
|
+
),
|
|
408
|
+
limit: {
|
|
409
|
+
type: "integer",
|
|
410
|
+
minimum: 1,
|
|
411
|
+
maximum: limits.maxListRows,
|
|
412
|
+
description: `Maximum rows to return. Defaults to ${limits.maxListRows}; values outside 1 to ${limits.maxListRows} are rejected.`,
|
|
413
|
+
},
|
|
414
|
+
cursor: {
|
|
415
|
+
type: "string",
|
|
416
|
+
description: "Cursor from a previous list call; pass it back unchanged.",
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
additionalProperties: false,
|
|
420
|
+
},
|
|
421
|
+
output: outputStrategy.schema(LIST_OUTPUT_SCHEMA),
|
|
422
|
+
options: { namespace: config.namespace },
|
|
423
|
+
execute: async (rawInput, context) => {
|
|
424
|
+
const { input, format } = readToolInput(rawInput, outputStrategy);
|
|
425
|
+
const requested = readOptionalSessionID(input?.parentID);
|
|
426
|
+
if (input?.parentID !== undefined && requested === undefined) {
|
|
427
|
+
return toolError("parentID is invalid; use the exact ID reported by the server.");
|
|
428
|
+
}
|
|
429
|
+
const caller = context?.sessionID;
|
|
430
|
+
const statusFilter = requestedStatuses(input?.statuses);
|
|
431
|
+
if (statusFilter.error) {
|
|
432
|
+
return toolError(statusFilter.error);
|
|
433
|
+
}
|
|
434
|
+
if (statusFilter.invalid.length > 0) {
|
|
435
|
+
const shown = statusFilter.invalid.slice(0, 5).join(", ");
|
|
436
|
+
const more =
|
|
437
|
+
statusFilter.invalid.length > 5
|
|
438
|
+
? `, and ${statusFilter.invalid.length - 5} more`
|
|
439
|
+
: "";
|
|
440
|
+
return toolError(
|
|
441
|
+
`Unknown status(es): ${shown}${more}. Use one or more of: ${RUN_STATUSES.join(", ")}.`,
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
const statuses = statusFilter.statuses;
|
|
445
|
+
if (statuses.size === 0) {
|
|
446
|
+
return toolError(
|
|
447
|
+
`statuses must contain at least one status. Omit statuses to include all, or pass one or more of: ${RUN_STATUSES.join(", ")}.`,
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
const activeField = readOptionalBoolean(input?.active, "active");
|
|
451
|
+
if (activeField.error) return toolError(activeField.error);
|
|
452
|
+
const subtreeActiveField = readOptionalBoolean(
|
|
453
|
+
input?.subtreeActive,
|
|
454
|
+
"subtreeActive",
|
|
455
|
+
);
|
|
456
|
+
if (subtreeActiveField.error) return toolError(subtreeActiveField.error);
|
|
457
|
+
const activeFilter = activeField.value;
|
|
458
|
+
const subtreeActiveFilter = subtreeActiveField.value;
|
|
459
|
+
const limit = limitInteger(input?.limit, limits.maxListRows, 1, limits.maxListRows);
|
|
460
|
+
|
|
461
|
+
if (requested && requested !== caller) {
|
|
462
|
+
const parent = await access.getSession(requested, context?.signal);
|
|
463
|
+
if (context?.signal?.aborted) {
|
|
464
|
+
return toolError("Aborted before the parent could be read.");
|
|
465
|
+
}
|
|
466
|
+
if (!parent.ok || !parent.info) {
|
|
467
|
+
return toolError(`Parent session ${requested} could not be read.`);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const root = requested ?? caller;
|
|
472
|
+
if (!root) {
|
|
473
|
+
return toolError(
|
|
474
|
+
"Refusing: the calling session is unknown; pass parentID or call from a session.",
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
const filterKey = [
|
|
478
|
+
[...statuses].toSorted().join(","),
|
|
479
|
+
activeFilter === undefined ? "" : `active=${activeFilter}`,
|
|
480
|
+
subtreeActiveFilter === undefined ? "" : `subtreeActive=${subtreeActiveFilter}`,
|
|
481
|
+
]
|
|
482
|
+
.filter(Boolean)
|
|
483
|
+
.join("|");
|
|
484
|
+
const cursorResult = readCursor(input?.cursor, {
|
|
485
|
+
tool: TOOL.list,
|
|
486
|
+
root,
|
|
487
|
+
filter: filterKey,
|
|
488
|
+
});
|
|
489
|
+
if (cursorResult.error) {
|
|
490
|
+
return toolError(cursorResult.error);
|
|
491
|
+
}
|
|
492
|
+
const cursor = cursorResult.cursor;
|
|
493
|
+
if (context?.signal?.aborted) {
|
|
494
|
+
return toolError("Aborted before discovery.");
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const found = await discovery.discoverRows(root, context?.signal);
|
|
498
|
+
if (context?.signal?.aborted) {
|
|
499
|
+
return toolError("Aborted during discovery.");
|
|
500
|
+
}
|
|
501
|
+
const warning = found.complete ? "" : DISCOVERY_WARNING_TEXT;
|
|
502
|
+
const allMatches = found.rows.filter(
|
|
503
|
+
(row) =>
|
|
504
|
+
statuses.has(row.status) &&
|
|
505
|
+
(activeFilter === undefined || row.active === activeFilter) &&
|
|
506
|
+
(subtreeActiveFilter === undefined ||
|
|
507
|
+
row.subtreeActive === subtreeActiveFilter),
|
|
508
|
+
);
|
|
509
|
+
const activeCount = found.rows.filter((row) => row.active).length;
|
|
510
|
+
const subtreeActiveCount = found.rows.filter((row) => row.subtreeActive).length;
|
|
511
|
+
const afterCursor = cursor
|
|
512
|
+
? allMatches.filter((row) => isAfterCursor(row, cursor))
|
|
513
|
+
: allMatches;
|
|
514
|
+
|
|
515
|
+
const outputFor = (
|
|
516
|
+
page: readonly SessionRow[],
|
|
517
|
+
matching: number,
|
|
518
|
+
window: number,
|
|
519
|
+
remaining: number,
|
|
520
|
+
remainingPages: number,
|
|
521
|
+
nextCursor?: string,
|
|
522
|
+
): ListOutput => {
|
|
523
|
+
const output: ListOutput = {
|
|
524
|
+
root,
|
|
525
|
+
statuses: [...statuses],
|
|
526
|
+
discoveryComplete: found.complete,
|
|
527
|
+
known: found.rows.length,
|
|
528
|
+
activeCount,
|
|
529
|
+
subtreeActiveCount,
|
|
530
|
+
matching,
|
|
531
|
+
window,
|
|
532
|
+
remaining,
|
|
533
|
+
remainingPages,
|
|
534
|
+
counts: countStatuses(found.rows),
|
|
535
|
+
rows: page.map(rowOutput),
|
|
536
|
+
};
|
|
537
|
+
if (!found.complete) {
|
|
538
|
+
output.warning = DISCOVERY_WARNING;
|
|
539
|
+
}
|
|
540
|
+
if (nextCursor) output.cursor = nextCursor;
|
|
541
|
+
return output;
|
|
542
|
+
};
|
|
543
|
+
const respond = (content: string, output: ListOutput) =>
|
|
544
|
+
outputStrategy.respond(content, output, format);
|
|
545
|
+
|
|
546
|
+
if (found.rows.length === 0) {
|
|
547
|
+
return respond(
|
|
548
|
+
`No subagents discovered for ${root}.` + warning,
|
|
549
|
+
outputFor([], 0, 0, 0, 0),
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (allMatches.length === 0) {
|
|
554
|
+
const filters = filterDescription(statuses, activeFilter, subtreeActiveFilter);
|
|
555
|
+
return respond(
|
|
556
|
+
`${found.rows.length} known subagent(s) for ${root}, none match ${filters}.` +
|
|
557
|
+
warning,
|
|
558
|
+
outputFor([], 0, 0, 0, 0),
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (afterCursor.length === 0) {
|
|
563
|
+
return respond(
|
|
564
|
+
`Cursor is past the end: ${found.rows.length} known subagent(s) for ${root}.` +
|
|
565
|
+
warning,
|
|
566
|
+
outputFor([], allMatches.length, 0, 0, 0),
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const shown = afterCursor.slice(0, limit);
|
|
571
|
+
const nextCursor =
|
|
572
|
+
afterCursor.length > shown.length
|
|
573
|
+
? encodeCursor(shown[shown.length - 1], {
|
|
574
|
+
tool: TOOL.list,
|
|
575
|
+
root,
|
|
576
|
+
filter: filterKey,
|
|
577
|
+
})
|
|
578
|
+
: undefined;
|
|
579
|
+
const remaining = Math.max(0, afterCursor.length - shown.length);
|
|
580
|
+
const remainingPages = Math.ceil(remaining / limit);
|
|
581
|
+
const beforeCursor = allMatches.length - afterCursor.length;
|
|
582
|
+
const summary = paginationSummary({
|
|
583
|
+
shown: shown.length,
|
|
584
|
+
afterCursor: afterCursor.length,
|
|
585
|
+
allMatches: allMatches.length,
|
|
586
|
+
limit,
|
|
587
|
+
remainingPages,
|
|
588
|
+
nextCursor,
|
|
589
|
+
hasCursor: cursor !== undefined,
|
|
590
|
+
});
|
|
591
|
+
const beforeCursorNote =
|
|
592
|
+
cursor && beforeCursor > 0
|
|
593
|
+
? `\nNote: ${beforeCursor} matching row(s) are before this cursor window (previous pages or new children); re-run without a cursor to include new children.`
|
|
594
|
+
: "";
|
|
595
|
+
return respond(
|
|
596
|
+
shown.map(rowText).join("\n") + summary + beforeCursorNote + warning,
|
|
597
|
+
outputFor(
|
|
598
|
+
shown,
|
|
599
|
+
allMatches.length,
|
|
600
|
+
afterCursor.length,
|
|
601
|
+
remaining,
|
|
602
|
+
remainingPages,
|
|
603
|
+
nextCursor,
|
|
604
|
+
),
|
|
605
|
+
);
|
|
606
|
+
},
|
|
607
|
+
});
|
|
608
|
+
};
|