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,271 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { ToolEditor } from "@opencode/plugin/promise/tool";
|
|
3
|
+
import type { Access } from "../access";
|
|
4
|
+
import {
|
|
5
|
+
BACKGROUND_JOB_SCHEMA,
|
|
6
|
+
backgroundJobLabel,
|
|
7
|
+
isSessionActive,
|
|
8
|
+
type BackgroundJobInfo,
|
|
9
|
+
type BackgroundJobStore,
|
|
10
|
+
} from "../background";
|
|
11
|
+
import type { Config } from "../config";
|
|
12
|
+
import { errorMessage, toolError } from "../errors";
|
|
13
|
+
import { INTERRUPT_OUTCOME, interruptOutcome } from "../interrupt";
|
|
14
|
+
import type { LiveStore } from "../live-state";
|
|
15
|
+
import { guardTarget, type Guard } from "../permissions";
|
|
16
|
+
import type { SessionInfoLike } from "../session";
|
|
17
|
+
import { RUN_STATUS, RUN_STATUSES, runStatusIsIdle, type RunStatus } from "../status";
|
|
18
|
+
import {
|
|
19
|
+
TOKEN_USAGE_SCHEMA,
|
|
20
|
+
normalizeTokens,
|
|
21
|
+
tokensUsed,
|
|
22
|
+
type TokenUsage,
|
|
23
|
+
} from "../tokens";
|
|
24
|
+
import {
|
|
25
|
+
MAX_REASON_LENGTH,
|
|
26
|
+
readTextField,
|
|
27
|
+
readToolInput,
|
|
28
|
+
requireSessionID,
|
|
29
|
+
} from "./input";
|
|
30
|
+
import { TOOL } from "./names";
|
|
31
|
+
import { formatPropertyDescription, resolveToolOutputStrategy } from "./output";
|
|
32
|
+
|
|
33
|
+
export const CANCEL_OUTCOME = {
|
|
34
|
+
accepted: INTERRUPT_OUTCOME.accepted,
|
|
35
|
+
rejected: INTERRUPT_OUTCOME.rejected,
|
|
36
|
+
unknown: INTERRUPT_OUTCOME.unknown,
|
|
37
|
+
} as const;
|
|
38
|
+
|
|
39
|
+
export type CancelOutcome = (typeof CANCEL_OUTCOME)[keyof typeof CANCEL_OUTCOME];
|
|
40
|
+
|
|
41
|
+
const CANCEL_PREFIX: Readonly<
|
|
42
|
+
Record<CancelOutcome, (sessionID: string, status: RunStatus) => string>
|
|
43
|
+
> = {
|
|
44
|
+
[CANCEL_OUTCOME.accepted]: (sessionID) => `Interrupt requested for ${sessionID}`,
|
|
45
|
+
[CANCEL_OUTCOME.rejected]: (sessionID, status) =>
|
|
46
|
+
runStatusIsIdle(status)
|
|
47
|
+
? `${sessionID} was not running; no interrupt was needed`
|
|
48
|
+
: `Interrupt was not accepted for ${sessionID}`,
|
|
49
|
+
[CANCEL_OUTCOME.unknown]: (sessionID) => `Interrupt outcome unknown for ${sessionID}`,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const cancelText = (
|
|
53
|
+
interrupt: CancelOutcome,
|
|
54
|
+
sessionID: string,
|
|
55
|
+
status: RunStatus,
|
|
56
|
+
backgroundJob: BackgroundJobInfo | undefined,
|
|
57
|
+
): string => {
|
|
58
|
+
if (!backgroundJob) return CANCEL_PREFIX[interrupt](sessionID, status);
|
|
59
|
+
if (backgroundJob.stateUnknown) {
|
|
60
|
+
if (interrupt === CANCEL_OUTCOME.rejected && runStatusIsIdle(status)) {
|
|
61
|
+
return `${sessionID} was not running; the execution was idle and no interrupt was needed for the execution, but background job state could not be verified`;
|
|
62
|
+
}
|
|
63
|
+
return `${CANCEL_PREFIX[interrupt](sessionID, status)}; background job state could not be verified`;
|
|
64
|
+
}
|
|
65
|
+
const label = backgroundJobLabel(backgroundJob);
|
|
66
|
+
if (interrupt === CANCEL_OUTCOME.rejected && runStatusIsIdle(status)) {
|
|
67
|
+
return `${sessionID} was not running; the execution was idle and no interrupt was needed for the execution, but this tool cannot stop the background job (${label})`;
|
|
68
|
+
}
|
|
69
|
+
return `${CANCEL_PREFIX[interrupt](sessionID, status)}; background job (${label}) may continue`;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export interface CancelOutput {
|
|
73
|
+
sessionID: string;
|
|
74
|
+
accepted: boolean;
|
|
75
|
+
interrupt: CancelOutcome;
|
|
76
|
+
status: RunStatus;
|
|
77
|
+
/** The target's own execution or background work. Descendants are excluded. */
|
|
78
|
+
active: boolean;
|
|
79
|
+
backgroundJob?: BackgroundJobInfo;
|
|
80
|
+
reason?: string;
|
|
81
|
+
readBackMayLag: boolean;
|
|
82
|
+
cancellationDoesNotCascade: boolean;
|
|
83
|
+
costUSD?: number;
|
|
84
|
+
tokens?: TokenUsage;
|
|
85
|
+
tokensUsed?: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const CANCEL_OUTPUT_SCHEMA = {
|
|
89
|
+
type: "object",
|
|
90
|
+
properties: {
|
|
91
|
+
sessionID: { type: "string" },
|
|
92
|
+
accepted: {
|
|
93
|
+
type: "boolean",
|
|
94
|
+
description: 'Alias for interrupt === "accepted".',
|
|
95
|
+
},
|
|
96
|
+
interrupt: {
|
|
97
|
+
type: "string",
|
|
98
|
+
enum: Object.values(CANCEL_OUTCOME),
|
|
99
|
+
description:
|
|
100
|
+
"accepted: server accepted; rejected: no active execution; unknown: aborted or no definitive answer.",
|
|
101
|
+
},
|
|
102
|
+
status: { type: "string", enum: [...RUN_STATUSES] },
|
|
103
|
+
active: {
|
|
104
|
+
type: "boolean",
|
|
105
|
+
description:
|
|
106
|
+
"True when the target is running, unknown, or owns a background job after the call.",
|
|
107
|
+
},
|
|
108
|
+
backgroundJob: BACKGROUND_JOB_SCHEMA,
|
|
109
|
+
reason: { type: "string" },
|
|
110
|
+
readBackMayLag: { type: "boolean" },
|
|
111
|
+
cancellationDoesNotCascade: {
|
|
112
|
+
type: "boolean",
|
|
113
|
+
description: "Always true; cancellation does not cascade.",
|
|
114
|
+
},
|
|
115
|
+
costUSD: { type: "number" },
|
|
116
|
+
tokens: TOKEN_USAGE_SCHEMA,
|
|
117
|
+
tokensUsed: { type: "integer" },
|
|
118
|
+
},
|
|
119
|
+
required: [
|
|
120
|
+
"sessionID",
|
|
121
|
+
"accepted",
|
|
122
|
+
"interrupt",
|
|
123
|
+
"status",
|
|
124
|
+
"active",
|
|
125
|
+
"readBackMayLag",
|
|
126
|
+
"cancellationDoesNotCascade",
|
|
127
|
+
],
|
|
128
|
+
additionalProperties: false,
|
|
129
|
+
} as const;
|
|
130
|
+
|
|
131
|
+
export interface CancelDeps {
|
|
132
|
+
config: Config;
|
|
133
|
+
access: Access;
|
|
134
|
+
guard: Guard;
|
|
135
|
+
live: LiveStore;
|
|
136
|
+
background: BackgroundJobStore;
|
|
137
|
+
session: Plugin.Context["session"];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export const registerCancelTool = (
|
|
141
|
+
editor: ToolEditor,
|
|
142
|
+
{ config, access, guard, live, background, session }: CancelDeps,
|
|
143
|
+
) => {
|
|
144
|
+
const { allowedOutputFormats } = config;
|
|
145
|
+
const outputStrategy = resolveToolOutputStrategy(allowedOutputFormats, TOOL.cancel);
|
|
146
|
+
|
|
147
|
+
editor.add({
|
|
148
|
+
name: TOOL.cancel,
|
|
149
|
+
description:
|
|
150
|
+
"Stop one session by its session ID. " +
|
|
151
|
+
"This is a soft interrupt: it stops only that session, does not cascade, and a long tool call may finish first. " +
|
|
152
|
+
"Jobs owned by an idle session keep running; the result reports observed background work and says when it may remain. " +
|
|
153
|
+
"OpenCode may wake the parent with a cancellation notice. " +
|
|
154
|
+
"The reason is recorded in the result and is not delivered to the child.",
|
|
155
|
+
input: {
|
|
156
|
+
type: "object",
|
|
157
|
+
properties: {
|
|
158
|
+
sessionID: {
|
|
159
|
+
type: "string",
|
|
160
|
+
description: "Target session ID, e.g. ses_...",
|
|
161
|
+
},
|
|
162
|
+
reason: {
|
|
163
|
+
type: "string",
|
|
164
|
+
description: `Short reason, recorded in the result. Maximum ${MAX_REASON_LENGTH} characters.`,
|
|
165
|
+
},
|
|
166
|
+
...outputStrategy.formatProperty(
|
|
167
|
+
formatPropertyDescription(outputStrategy, "structured output"),
|
|
168
|
+
),
|
|
169
|
+
},
|
|
170
|
+
required: ["sessionID"],
|
|
171
|
+
additionalProperties: false,
|
|
172
|
+
},
|
|
173
|
+
output: outputStrategy.schema(CANCEL_OUTPUT_SCHEMA),
|
|
174
|
+
options: { namespace: config.namespace },
|
|
175
|
+
execute: async (rawInput, context) => {
|
|
176
|
+
const { input, format } = readToolInput(rawInput, outputStrategy);
|
|
177
|
+
const reasonField = readTextField(input?.reason, MAX_REASON_LENGTH, "reason");
|
|
178
|
+
if (reasonField.error) return toolError(reasonField.error);
|
|
179
|
+
const sessionID = requireSessionID(input?.sessionID);
|
|
180
|
+
let info: SessionInfoLike | undefined;
|
|
181
|
+
try {
|
|
182
|
+
info = await access.authorize(sessionID, context);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
return toolError(errorMessage(error));
|
|
185
|
+
}
|
|
186
|
+
const reason = reasonField.text;
|
|
187
|
+
if (context?.signal?.aborted) {
|
|
188
|
+
return toolError("Aborted before the interrupt was requested.");
|
|
189
|
+
}
|
|
190
|
+
// Guard errors propagate unchanged: a denial surfaces as a tool error,
|
|
191
|
+
// and a plain rejection still ends the step.
|
|
192
|
+
await guard([guardTarget(info, sessionID)], context);
|
|
193
|
+
if (context?.signal?.aborted) {
|
|
194
|
+
return toolError("Aborted before the interrupt was requested.");
|
|
195
|
+
}
|
|
196
|
+
const beforeStatus = info
|
|
197
|
+
? live.statusFor(info, sessionID)
|
|
198
|
+
: (live.liveStatusFor(sessionID) ?? RUN_STATUS.unknown);
|
|
199
|
+
let backgroundJob = background.backgroundJobFor(sessionID);
|
|
200
|
+
let subtreeActive = false;
|
|
201
|
+
if (runStatusIsIdle(beforeStatus) && !backgroundJob) {
|
|
202
|
+
const probe = await background.probe({
|
|
203
|
+
session,
|
|
204
|
+
targetID: sessionID,
|
|
205
|
+
signal: context?.signal,
|
|
206
|
+
});
|
|
207
|
+
if (context?.signal?.aborted) {
|
|
208
|
+
return toolError("Aborted before the interrupt was requested.");
|
|
209
|
+
}
|
|
210
|
+
backgroundJob = probe.backgroundJob;
|
|
211
|
+
subtreeActive = probe.subtreeActive;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let result: { interrupted?: boolean } | undefined;
|
|
215
|
+
try {
|
|
216
|
+
result = (await session.interrupt(
|
|
217
|
+
{ sessionID, resume: false },
|
|
218
|
+
{ signal: context?.signal },
|
|
219
|
+
)) as { interrupted?: boolean } | undefined;
|
|
220
|
+
} catch (error) {
|
|
221
|
+
if (context?.signal?.aborted) {
|
|
222
|
+
const status = live.liveStatusFor(sessionID) ?? RUN_STATUS.unknown;
|
|
223
|
+
return toolError(
|
|
224
|
+
`Interrupt outcome unknown for ${sessionID}: the call was aborted while the interrupt was in flight; it may or may not have been accepted. Last observed state: ${status}. Run ${config.namespace}_${TOOL.list} to confirm.`,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
return toolError(`Interrupt failed for ${sessionID}: ${errorMessage(error)}`);
|
|
228
|
+
}
|
|
229
|
+
const { info: after } = await access.getSession(sessionID, context?.signal);
|
|
230
|
+
const status = after
|
|
231
|
+
? live.statusFor(after, sessionID)
|
|
232
|
+
: (live.liveStatusFor(sessionID) ?? RUN_STATUS.unknown);
|
|
233
|
+
const interrupt = interruptOutcome(result);
|
|
234
|
+
const accepted = interrupt === CANCEL_OUTCOME.accepted;
|
|
235
|
+
const readBackMayLag = accepted && !runStatusIsIdle(status);
|
|
236
|
+
if (backgroundJob) {
|
|
237
|
+
backgroundJob = { ...backgroundJob, mayRemain: true };
|
|
238
|
+
}
|
|
239
|
+
const active = isSessionActive(status, backgroundJob);
|
|
240
|
+
const prefix = cancelText(interrupt, sessionID, status, backgroundJob);
|
|
241
|
+
const output: CancelOutput = {
|
|
242
|
+
sessionID,
|
|
243
|
+
accepted,
|
|
244
|
+
interrupt,
|
|
245
|
+
status,
|
|
246
|
+
active,
|
|
247
|
+
readBackMayLag,
|
|
248
|
+
cancellationDoesNotCascade: true,
|
|
249
|
+
};
|
|
250
|
+
if (backgroundJob) output.backgroundJob = backgroundJob;
|
|
251
|
+
if (reason) output.reason = reason;
|
|
252
|
+
if (typeof after?.cost === "number" && Number.isFinite(after.cost))
|
|
253
|
+
output.costUSD = after.cost;
|
|
254
|
+
const afterTokens = normalizeTokens(after?.tokens);
|
|
255
|
+
if (afterTokens) {
|
|
256
|
+
output.tokens = afterTokens;
|
|
257
|
+
output.tokensUsed = tokensUsed(afterTokens);
|
|
258
|
+
}
|
|
259
|
+
const lagNote = readBackMayLag ? " Read-back may lag; re-run list to confirm." : "";
|
|
260
|
+
const subtreeNote =
|
|
261
|
+
subtreeActive && !backgroundJob
|
|
262
|
+
? " Its subtree may still be active; cancellation does not cascade."
|
|
263
|
+
: "";
|
|
264
|
+
return outputStrategy.respond(
|
|
265
|
+
`${prefix}${reason ? ` (${reason})` : ""}. Reported state now: ${status}.${lagNote}${subtreeNote}`,
|
|
266
|
+
output,
|
|
267
|
+
format,
|
|
268
|
+
);
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { ToolEditor } from "@opencode/plugin/promise/tool";
|
|
3
|
+
import type { Access } from "../access";
|
|
4
|
+
import type { BackgroundJobStore } from "../background";
|
|
5
|
+
import type { Config } from "../config";
|
|
6
|
+
import type { Discovery } from "../discovery";
|
|
7
|
+
import type { LiveStore } from "../live-state";
|
|
8
|
+
import type { SendMessage } from "../messages";
|
|
9
|
+
import type { Guard } from "../permissions";
|
|
10
|
+
import { registerCancelDescendantsTool } from "./cancel-descendants";
|
|
11
|
+
import { registerCancelTool } from "./cancel";
|
|
12
|
+
import { registerListTool } from "./list";
|
|
13
|
+
import { registerContextTool, registerQueueTool, registerSteerTool } from "./send";
|
|
14
|
+
import { registerUsageTool } from "./usage";
|
|
15
|
+
|
|
16
|
+
export interface ToolDeps {
|
|
17
|
+
config: Config;
|
|
18
|
+
access: Access;
|
|
19
|
+
discovery: Discovery;
|
|
20
|
+
live: LiveStore;
|
|
21
|
+
background: BackgroundJobStore;
|
|
22
|
+
guard: Guard;
|
|
23
|
+
sendMessage: SendMessage;
|
|
24
|
+
session: Plugin.Context["session"];
|
|
25
|
+
model: Plugin.Context["model"];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const registerTools = (editor: ToolEditor, deps: ToolDeps) => {
|
|
29
|
+
editor.namespace({
|
|
30
|
+
name: deps.config.namespace,
|
|
31
|
+
description: "Inspect, stop, steer and feed context to subagent sessions.",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
registerListTool(editor, deps);
|
|
35
|
+
registerUsageTool(editor, deps);
|
|
36
|
+
registerCancelTool(editor, deps);
|
|
37
|
+
registerCancelDescendantsTool(editor, deps);
|
|
38
|
+
registerSteerTool(editor, deps);
|
|
39
|
+
registerQueueTool(editor, deps);
|
|
40
|
+
registerContextTool(editor, deps);
|
|
41
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { OutputFormat } from "../config";
|
|
2
|
+
import { REQUIRED_SESSION_ID, REQUIRED_TEXT, toolError } from "../errors";
|
|
3
|
+
import { normalizeSessionID } from "../session";
|
|
4
|
+
import { clipText, maxLengthError } from "../text";
|
|
5
|
+
import type { OutputStrategy } from "./output";
|
|
6
|
+
|
|
7
|
+
export const MAX_REASON_LENGTH = 500;
|
|
8
|
+
export const MAX_DESCRIPTION_LENGTH = 200;
|
|
9
|
+
|
|
10
|
+
export type ResponseFormat = OutputFormat;
|
|
11
|
+
|
|
12
|
+
export interface ToolInput {
|
|
13
|
+
sessionID?: string;
|
|
14
|
+
parentID?: string;
|
|
15
|
+
text?: string;
|
|
16
|
+
reason?: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
statuses?: string[];
|
|
19
|
+
active?: boolean;
|
|
20
|
+
subtreeActive?: boolean;
|
|
21
|
+
limit?: number;
|
|
22
|
+
cursor?: string;
|
|
23
|
+
format?: ResponseFormat;
|
|
24
|
+
includeDescendants?: boolean;
|
|
25
|
+
resume?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ToolCtx {
|
|
29
|
+
sessionID?: string;
|
|
30
|
+
agent?: string;
|
|
31
|
+
messageID?: string;
|
|
32
|
+
id?: string;
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const requireSessionID = (value: unknown): string => {
|
|
37
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
38
|
+
return toolError(REQUIRED_SESSION_ID);
|
|
39
|
+
}
|
|
40
|
+
const sessionID = normalizeSessionID(value);
|
|
41
|
+
if (!sessionID) {
|
|
42
|
+
return toolError("sessionID is invalid; use the exact ID reported by the server.");
|
|
43
|
+
}
|
|
44
|
+
return sessionID;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const readOptionalSessionID = (value: unknown): string | undefined => {
|
|
48
|
+
if (value === undefined) return undefined;
|
|
49
|
+
if (typeof value !== "string" || value.trim().length === 0) return undefined;
|
|
50
|
+
return normalizeSessionID(value);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const requireText = (value: unknown, max: number): string => {
|
|
54
|
+
const text = clipText(value, max);
|
|
55
|
+
if (!text) return toolError(REQUIRED_TEXT);
|
|
56
|
+
return text;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const readTextField = (
|
|
60
|
+
value: unknown,
|
|
61
|
+
max: number,
|
|
62
|
+
field: string,
|
|
63
|
+
): { text?: string; error?: string } => {
|
|
64
|
+
const error = maxLengthError(value, max, field);
|
|
65
|
+
if (error) return { error };
|
|
66
|
+
return { text: clipText(value, max) };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const readToolInput = (
|
|
70
|
+
rawInput: unknown,
|
|
71
|
+
outputStrategy: OutputStrategy,
|
|
72
|
+
): { input: ToolInput; format: ResponseFormat } => {
|
|
73
|
+
const input = (rawInput ?? {}) as ToolInput;
|
|
74
|
+
return { input, format: outputStrategy.selectFormat(input.format) };
|
|
75
|
+
};
|