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