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,169 @@
|
|
|
1
|
+
import type { JsonSchema } from "effect";
|
|
2
|
+
import { OUTPUT_FORMAT, normalizeOutputFormats, type OutputFormat } from "../config";
|
|
3
|
+
import { TOOL, type ToolName } from "./names";
|
|
4
|
+
|
|
5
|
+
export interface OutputResult {
|
|
6
|
+
readonly content: string;
|
|
7
|
+
readonly output?: unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface OutputFormatProperty {
|
|
11
|
+
readonly format?: {
|
|
12
|
+
readonly type: "string";
|
|
13
|
+
readonly enum: readonly OutputFormat[];
|
|
14
|
+
readonly description: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type ResponseFactory = (content: string, output: unknown) => OutputResult;
|
|
19
|
+
|
|
20
|
+
interface FormatHandler {
|
|
21
|
+
/** Response when this format is the only allowed one. */
|
|
22
|
+
readonly solo: ResponseFactory;
|
|
23
|
+
/** Response when this format is selected among several allowed formats. */
|
|
24
|
+
readonly combined: ResponseFactory;
|
|
25
|
+
/** Output schema when this format is the only allowed one. */
|
|
26
|
+
readonly soloSchema: (
|
|
27
|
+
schema: JsonSchema.JsonSchema,
|
|
28
|
+
) => JsonSchema.JsonSchema | undefined;
|
|
29
|
+
/** Output schema contribution when several formats are allowed. */
|
|
30
|
+
readonly combinedSchema: (schema: JsonSchema.JsonSchema) => JsonSchema.JsonSchema;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const TEXT_HANDLER: FormatHandler = {
|
|
34
|
+
solo: (content) => ({ content }),
|
|
35
|
+
combined: (content) => ({ content, output: content }),
|
|
36
|
+
soloSchema: () => undefined,
|
|
37
|
+
combinedSchema: () => ({ type: "string" }),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const JSON_HANDLER: FormatHandler = {
|
|
41
|
+
solo: (content, output) => ({ content, output }),
|
|
42
|
+
combined: (content, output) => ({ content, output }),
|
|
43
|
+
soloSchema: (schema) => schema,
|
|
44
|
+
combinedSchema: (schema) => schema,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const FORMAT_HANDLERS: Readonly<Record<OutputFormat, FormatHandler>> = {
|
|
48
|
+
[OUTPUT_FORMAT.text]: TEXT_HANDLER,
|
|
49
|
+
[OUTPUT_FORMAT.json]: JSON_HANDLER,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const FORMAT_HANDLER_MAP: ReadonlyMap<OutputFormat, FormatHandler> = new Map(
|
|
53
|
+
Object.entries(FORMAT_HANDLERS) as Array<[OutputFormat, FormatHandler]>,
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
export interface OutputStrategy {
|
|
57
|
+
/** Formats this strategy accepts, in canonical order. */
|
|
58
|
+
readonly formats: readonly OutputFormat[];
|
|
59
|
+
/** The effective default when format is omitted or invalid. */
|
|
60
|
+
readonly defaultFormat: OutputFormat;
|
|
61
|
+
/** The per-call format input property, or an empty object when there is no choice. */
|
|
62
|
+
readonly formatProperty: (description: string) => OutputFormatProperty;
|
|
63
|
+
/** The tool output schema, or undefined when output is text-only. */
|
|
64
|
+
readonly schema: (schema: JsonSchema.JsonSchema) => JsonSchema.JsonSchema | undefined;
|
|
65
|
+
/** Resolve the effective format for this call. */
|
|
66
|
+
readonly selectFormat: (requested: unknown) => OutputFormat;
|
|
67
|
+
/** Build the tool result for the selected format. */
|
|
68
|
+
readonly respond: (
|
|
69
|
+
content: string,
|
|
70
|
+
output: unknown,
|
|
71
|
+
format: OutputFormat,
|
|
72
|
+
) => OutputResult;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const TOOL_FORMAT_DEFAULTS: Readonly<Record<ToolName, OutputFormat>> = {
|
|
76
|
+
[TOOL.list]: OUTPUT_FORMAT.json,
|
|
77
|
+
[TOOL.usage]: OUTPUT_FORMAT.json,
|
|
78
|
+
[TOOL.cancelDescendants]: OUTPUT_FORMAT.json,
|
|
79
|
+
[TOOL.cancel]: OUTPUT_FORMAT.text,
|
|
80
|
+
[TOOL.steer]: OUTPUT_FORMAT.text,
|
|
81
|
+
[TOOL.queue]: OUTPUT_FORMAT.text,
|
|
82
|
+
[TOOL.context]: OUTPUT_FORMAT.text,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const TOOL_FORMAT_DEFAULT_MAP: ReadonlyMap<ToolName, OutputFormat> = new Map(
|
|
86
|
+
Object.entries(TOOL_FORMAT_DEFAULTS) as Array<[ToolName, OutputFormat]>,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const NO_FORMAT_PROPERTY = (): OutputFormatProperty => ({});
|
|
90
|
+
|
|
91
|
+
const handlerFor = (format: OutputFormat): FormatHandler => {
|
|
92
|
+
const handler = FORMAT_HANDLER_MAP.get(format);
|
|
93
|
+
if (!handler) throw new Error(`Unsupported output format: ${format}`);
|
|
94
|
+
return handler;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const resolveOutputStrategy = (
|
|
98
|
+
allowedOutputFormats: readonly OutputFormat[],
|
|
99
|
+
preferredFormat: OutputFormat,
|
|
100
|
+
): OutputStrategy => {
|
|
101
|
+
// Invalid and empty inputs fall back to the default set. The config layer
|
|
102
|
+
// uses the same normalizer, so a malformed option cannot widen the surface.
|
|
103
|
+
const formats = normalizeOutputFormats(allowedOutputFormats);
|
|
104
|
+
const handlers = formats.map(handlerFor);
|
|
105
|
+
const defaultFormat = formats.includes(preferredFormat) ? preferredFormat : formats[0];
|
|
106
|
+
|
|
107
|
+
if (formats.length === 1) {
|
|
108
|
+
const format = formats[0];
|
|
109
|
+
const handler = handlers[0];
|
|
110
|
+
return {
|
|
111
|
+
formats,
|
|
112
|
+
defaultFormat,
|
|
113
|
+
formatProperty: NO_FORMAT_PROPERTY,
|
|
114
|
+
schema: handler.soloSchema,
|
|
115
|
+
selectFormat: () => format,
|
|
116
|
+
respond: (content, output) => handler.solo(content, output),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const handlerByFormat = new Map(
|
|
121
|
+
formats.map((format, index) => [format, handlers[index]] as const),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
formats,
|
|
126
|
+
defaultFormat,
|
|
127
|
+
formatProperty: (description) => ({
|
|
128
|
+
format: {
|
|
129
|
+
type: "string",
|
|
130
|
+
enum: [...formats],
|
|
131
|
+
description,
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
schema: (schema) => ({
|
|
135
|
+
anyOf: handlers.map((handler) => handler.combinedSchema(schema)),
|
|
136
|
+
}),
|
|
137
|
+
selectFormat: (requested) =>
|
|
138
|
+
typeof requested === "string" && handlerByFormat.has(requested as OutputFormat)
|
|
139
|
+
? (requested as OutputFormat)
|
|
140
|
+
: defaultFormat,
|
|
141
|
+
respond: (content, output, format) => {
|
|
142
|
+
const handler = handlerByFormat.get(format);
|
|
143
|
+
if (!handler) throw new Error(`Unsupported output format: ${format}`);
|
|
144
|
+
return handler.combined(content, output);
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export const resolveToolOutputStrategy = (
|
|
150
|
+
allowedOutputFormats: readonly OutputFormat[],
|
|
151
|
+
tool: ToolName,
|
|
152
|
+
): OutputStrategy => {
|
|
153
|
+
const preferred = TOOL_FORMAT_DEFAULT_MAP.get(tool);
|
|
154
|
+
if (!preferred) throw new Error(`Unsupported tool: ${String(tool)}`);
|
|
155
|
+
return resolveOutputStrategy(allowedOutputFormats, preferred);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export const otherOutputFormat = (strategy: OutputStrategy): OutputFormat | undefined =>
|
|
159
|
+
strategy.formats.find((format) => format !== strategy.defaultFormat);
|
|
160
|
+
|
|
161
|
+
export const formatPropertyDescription = (
|
|
162
|
+
strategy: OutputStrategy,
|
|
163
|
+
otherHint: string,
|
|
164
|
+
): string => {
|
|
165
|
+
const other = otherOutputFormat(strategy);
|
|
166
|
+
return other
|
|
167
|
+
? `Response format. Defaults to ${strategy.defaultFormat}; use ${other} for ${otherHint}.`
|
|
168
|
+
: `Response format. Defaults to ${strategy.defaultFormat}.`;
|
|
169
|
+
};
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import type { ToolEditor } from "@opencode/plugin/promise/tool";
|
|
2
|
+
import { BACKGROUND_JOB_SCHEMA } from "../background";
|
|
3
|
+
import type { Config } from "../config";
|
|
4
|
+
import { toolError } from "../errors";
|
|
5
|
+
import {
|
|
6
|
+
CONTEXT_DELIVERY,
|
|
7
|
+
CONTEXT_METADATA_SOURCE,
|
|
8
|
+
DELIVERY,
|
|
9
|
+
SEND_KIND,
|
|
10
|
+
SendAdmissionError,
|
|
11
|
+
applySendResult,
|
|
12
|
+
type Delivery,
|
|
13
|
+
type SendMessage,
|
|
14
|
+
type SendResultFields,
|
|
15
|
+
} from "../messages";
|
|
16
|
+
import { RUN_STATUSES } from "../status";
|
|
17
|
+
import { maxLengthError, textLength } from "../text";
|
|
18
|
+
import {
|
|
19
|
+
MAX_DESCRIPTION_LENGTH,
|
|
20
|
+
readTextField,
|
|
21
|
+
readToolInput,
|
|
22
|
+
requireSessionID,
|
|
23
|
+
requireText,
|
|
24
|
+
} from "./input";
|
|
25
|
+
import { TOOL } from "./names";
|
|
26
|
+
import {
|
|
27
|
+
formatPropertyDescription,
|
|
28
|
+
resolveToolOutputStrategy,
|
|
29
|
+
type OutputStrategy,
|
|
30
|
+
} from "./output";
|
|
31
|
+
|
|
32
|
+
export interface SendOutput extends SendResultFields {
|
|
33
|
+
sessionID: string;
|
|
34
|
+
delivery: Delivery;
|
|
35
|
+
chars: number;
|
|
36
|
+
resume: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ContextOutput extends SendResultFields {
|
|
40
|
+
sessionID: string;
|
|
41
|
+
delivery: typeof CONTEXT_DELIVERY;
|
|
42
|
+
chars: number;
|
|
43
|
+
description?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const SEND_OUTPUT_PROPERTIES = {
|
|
47
|
+
sessionID: { type: "string" },
|
|
48
|
+
messageID: { type: "string" },
|
|
49
|
+
delivery: { type: "string", enum: Object.values(DELIVERY) },
|
|
50
|
+
chars: { type: "integer" },
|
|
51
|
+
resume: {
|
|
52
|
+
type: "boolean",
|
|
53
|
+
description: "Whether the message could wake an idle child.",
|
|
54
|
+
},
|
|
55
|
+
status: {
|
|
56
|
+
type: "string",
|
|
57
|
+
enum: [...RUN_STATUSES],
|
|
58
|
+
description:
|
|
59
|
+
"Status observed before the message was accepted; a wake may not be reflected yet.",
|
|
60
|
+
},
|
|
61
|
+
active: {
|
|
62
|
+
type: "boolean",
|
|
63
|
+
description:
|
|
64
|
+
"True when the target was running, unknown, or owned a background job when observed.",
|
|
65
|
+
},
|
|
66
|
+
backgroundJob: BACKGROUND_JOB_SCHEMA,
|
|
67
|
+
pending: {
|
|
68
|
+
type: "boolean",
|
|
69
|
+
description: "True when the message was accepted without waking an idle child.",
|
|
70
|
+
},
|
|
71
|
+
} as const;
|
|
72
|
+
|
|
73
|
+
const SEND_OUTPUT_SCHEMA = {
|
|
74
|
+
type: "object",
|
|
75
|
+
properties: SEND_OUTPUT_PROPERTIES,
|
|
76
|
+
required: ["sessionID", "delivery", "chars", "resume", "active"],
|
|
77
|
+
additionalProperties: false,
|
|
78
|
+
} as const;
|
|
79
|
+
|
|
80
|
+
const QUEUE_OUTPUT_SCHEMA = {
|
|
81
|
+
type: "object",
|
|
82
|
+
properties: {
|
|
83
|
+
...SEND_OUTPUT_PROPERTIES,
|
|
84
|
+
queued: {
|
|
85
|
+
type: "boolean",
|
|
86
|
+
description:
|
|
87
|
+
"True when the message was accepted but has not started yet. Inferred from observed status and resume; other queued prompts may be ahead.",
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
required: ["sessionID", "delivery", "chars", "resume", "active"],
|
|
91
|
+
additionalProperties: false,
|
|
92
|
+
} as const;
|
|
93
|
+
|
|
94
|
+
const CONTEXT_OUTPUT_SCHEMA = {
|
|
95
|
+
type: "object",
|
|
96
|
+
properties: {
|
|
97
|
+
sessionID: { type: "string" },
|
|
98
|
+
messageID: { type: "string" },
|
|
99
|
+
delivery: { type: "string", enum: [CONTEXT_DELIVERY] },
|
|
100
|
+
chars: { type: "integer" },
|
|
101
|
+
description: { type: "string" },
|
|
102
|
+
status: {
|
|
103
|
+
type: "string",
|
|
104
|
+
enum: [...RUN_STATUSES],
|
|
105
|
+
description:
|
|
106
|
+
"Status observed before the message was accepted; context does not wake the child.",
|
|
107
|
+
},
|
|
108
|
+
active: {
|
|
109
|
+
type: "boolean",
|
|
110
|
+
description:
|
|
111
|
+
"True when the target was running, unknown, or owned a background job when observed.",
|
|
112
|
+
},
|
|
113
|
+
backgroundJob: BACKGROUND_JOB_SCHEMA,
|
|
114
|
+
pending: { type: "boolean" },
|
|
115
|
+
},
|
|
116
|
+
required: ["sessionID", "delivery", "chars", "active"],
|
|
117
|
+
additionalProperties: false,
|
|
118
|
+
} as const;
|
|
119
|
+
|
|
120
|
+
export interface PromptSendConfig {
|
|
121
|
+
delivery: Delivery;
|
|
122
|
+
failureLabel: string;
|
|
123
|
+
outputStrategy: OutputStrategy;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const ADMITTED_LABEL: Record<Delivery, string> = {
|
|
127
|
+
[DELIVERY.steer]: "Steering message sent to",
|
|
128
|
+
[DELIVERY.queue]: "Message queued for",
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export interface SendDeps {
|
|
132
|
+
config: Config;
|
|
133
|
+
sendMessage: SendMessage;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Shared execute path for the two prompt-delivery tools. The requested
|
|
138
|
+
* delivery is fixed per tool; the output uses the delivery reported for the
|
|
139
|
+
* accepted message when provided.
|
|
140
|
+
*/
|
|
141
|
+
const runPromptSend = async (
|
|
142
|
+
rawInput: unknown,
|
|
143
|
+
context: Parameters<SendMessage>[1],
|
|
144
|
+
config: PromptSendConfig,
|
|
145
|
+
{ config: pluginConfig, sendMessage }: SendDeps,
|
|
146
|
+
) => {
|
|
147
|
+
const { limits } = pluginConfig;
|
|
148
|
+
const { outputStrategy } = config;
|
|
149
|
+
const { input, format } = readToolInput(rawInput, outputStrategy);
|
|
150
|
+
const textError = maxLengthError(input.text, limits.maxTextLength, "text");
|
|
151
|
+
if (textError) return toolError(textError);
|
|
152
|
+
const sessionID = requireSessionID(input.sessionID);
|
|
153
|
+
const text = requireText(input.text, limits.maxTextLength);
|
|
154
|
+
const resume = input.resume === undefined ? true : input.resume === true;
|
|
155
|
+
try {
|
|
156
|
+
const result = await sendMessage(
|
|
157
|
+
{
|
|
158
|
+
sessionID,
|
|
159
|
+
text,
|
|
160
|
+
kind: SEND_KIND.prompt,
|
|
161
|
+
delivery: config.delivery,
|
|
162
|
+
resume,
|
|
163
|
+
},
|
|
164
|
+
context,
|
|
165
|
+
config.failureLabel,
|
|
166
|
+
);
|
|
167
|
+
const delivery = result.admitted?.delivery ?? config.delivery;
|
|
168
|
+
const admittedLabel = ADMITTED_LABEL[delivery];
|
|
169
|
+
const chars = textLength(result.admitted?.text ?? text);
|
|
170
|
+
const output: SendOutput = {
|
|
171
|
+
sessionID,
|
|
172
|
+
delivery,
|
|
173
|
+
chars,
|
|
174
|
+
resume,
|
|
175
|
+
};
|
|
176
|
+
applySendResult(output, result);
|
|
177
|
+
const queuedNote = result.queued !== undefined ? `, queued=${result.queued}` : "";
|
|
178
|
+
return outputStrategy.respond(
|
|
179
|
+
`${admittedLabel} ${sessionID}` +
|
|
180
|
+
(result.admitted?.id ? ` as ${result.admitted.id}` : "") +
|
|
181
|
+
` (delivery=${delivery}, resume=${resume}${queuedNote}, ${chars} chars).`,
|
|
182
|
+
output,
|
|
183
|
+
format,
|
|
184
|
+
);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (error instanceof SendAdmissionError) return toolError(error.message);
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const sendInputSchema = (
|
|
192
|
+
outputStrategy: OutputStrategy,
|
|
193
|
+
maxTextLength: number,
|
|
194
|
+
textDescription: string,
|
|
195
|
+
resumeDescription: string,
|
|
196
|
+
) => ({
|
|
197
|
+
type: "object",
|
|
198
|
+
properties: {
|
|
199
|
+
sessionID: {
|
|
200
|
+
type: "string",
|
|
201
|
+
description: "Target session ID, e.g. ses_...",
|
|
202
|
+
},
|
|
203
|
+
text: {
|
|
204
|
+
type: "string",
|
|
205
|
+
description: `${textDescription} Maximum ${maxTextLength} characters; longer input is rejected.`,
|
|
206
|
+
},
|
|
207
|
+
resume: {
|
|
208
|
+
type: "boolean",
|
|
209
|
+
description: resumeDescription,
|
|
210
|
+
},
|
|
211
|
+
...outputStrategy.formatProperty(
|
|
212
|
+
formatPropertyDescription(outputStrategy, "structured output"),
|
|
213
|
+
),
|
|
214
|
+
},
|
|
215
|
+
required: ["sessionID", "text"],
|
|
216
|
+
additionalProperties: false,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
export const registerSteerTool = (editor: ToolEditor, deps: SendDeps) => {
|
|
220
|
+
const { allowedOutputFormats, limits } = deps.config;
|
|
221
|
+
const outputStrategy = resolveToolOutputStrategy(allowedOutputFormats, TOOL.steer);
|
|
222
|
+
editor.add({
|
|
223
|
+
name: TOOL.steer,
|
|
224
|
+
description:
|
|
225
|
+
"Send an instruction into a session at its next model request. " +
|
|
226
|
+
"Use it to correct course, add a constraint, or forbid a step. " +
|
|
227
|
+
"It does not interrupt current work: the message is delivered before the next request. " +
|
|
228
|
+
"The plugin cannot move a blocking shell to the background, so delivery may wait for that shell. " +
|
|
229
|
+
"By default an idle child is woken; set resume:false to accept the message without waking it. " +
|
|
230
|
+
"Use queue instead when the message should wait until the current execution settles.",
|
|
231
|
+
input: sendInputSchema(
|
|
232
|
+
outputStrategy,
|
|
233
|
+
limits.maxTextLength,
|
|
234
|
+
"Instruction for the child.",
|
|
235
|
+
"Default true: wake an idle child and start a new turn. Set false to accept without waking; an idle child keeps the message pending until its next run.",
|
|
236
|
+
),
|
|
237
|
+
output: outputStrategy.schema(SEND_OUTPUT_SCHEMA),
|
|
238
|
+
options: { namespace: deps.config.namespace },
|
|
239
|
+
execute: async (rawInput, context) =>
|
|
240
|
+
runPromptSend(
|
|
241
|
+
rawInput,
|
|
242
|
+
context,
|
|
243
|
+
{
|
|
244
|
+
delivery: DELIVERY.steer,
|
|
245
|
+
failureLabel: "Steer",
|
|
246
|
+
outputStrategy,
|
|
247
|
+
},
|
|
248
|
+
deps,
|
|
249
|
+
),
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
export const registerQueueTool = (editor: ToolEditor, deps: SendDeps) => {
|
|
254
|
+
const { allowedOutputFormats, limits } = deps.config;
|
|
255
|
+
const outputStrategy = resolveToolOutputStrategy(allowedOutputFormats, TOOL.queue);
|
|
256
|
+
editor.add({
|
|
257
|
+
name: TOOL.queue,
|
|
258
|
+
description:
|
|
259
|
+
"Queue a message for a session as its next independent turn. " +
|
|
260
|
+
"Unlike steer, it does not join the current run: it waits for the current execution to settle, then starts a separate turn. " +
|
|
261
|
+
"Background jobs are not waited for and continue in parallel. " +
|
|
262
|
+
"Use it for 'do this next' work. By default an idle child is woken; set resume:false to accept the message without waking it. " +
|
|
263
|
+
"Use steer instead to correct the current run.",
|
|
264
|
+
input: sendInputSchema(
|
|
265
|
+
outputStrategy,
|
|
266
|
+
limits.maxTextLength,
|
|
267
|
+
"Follow-up message.",
|
|
268
|
+
"Default true: wake an idle child and start the queued turn. The queue waits for the current execution, not background jobs. Set false to accept without waking; an idle child keeps the message pending until its next run.",
|
|
269
|
+
),
|
|
270
|
+
output: outputStrategy.schema(QUEUE_OUTPUT_SCHEMA),
|
|
271
|
+
options: { namespace: deps.config.namespace },
|
|
272
|
+
execute: async (rawInput, context) =>
|
|
273
|
+
runPromptSend(
|
|
274
|
+
rawInput,
|
|
275
|
+
context,
|
|
276
|
+
{
|
|
277
|
+
delivery: DELIVERY.queue,
|
|
278
|
+
failureLabel: "Queue",
|
|
279
|
+
outputStrategy,
|
|
280
|
+
},
|
|
281
|
+
deps,
|
|
282
|
+
),
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
export const registerContextTool = (editor: ToolEditor, deps: SendDeps) => {
|
|
287
|
+
const { allowedOutputFormats, limits } = deps.config;
|
|
288
|
+
const outputStrategy = resolveToolOutputStrategy(allowedOutputFormats, TOOL.context);
|
|
289
|
+
const { sendMessage } = deps;
|
|
290
|
+
|
|
291
|
+
editor.add({
|
|
292
|
+
name: TOOL.context,
|
|
293
|
+
description:
|
|
294
|
+
"Inject context into a session without asking for a turn: a synthetic message. " +
|
|
295
|
+
"Delivered at the child's next step boundary; an idle child keeps it pending until it runs again. " +
|
|
296
|
+
"Use it for facts and intermediate results, not for instructions. " +
|
|
297
|
+
"For an instruction without waking, use steer with resume:false; for a follow-up turn, use queue.",
|
|
298
|
+
input: {
|
|
299
|
+
type: "object",
|
|
300
|
+
properties: {
|
|
301
|
+
sessionID: {
|
|
302
|
+
type: "string",
|
|
303
|
+
description: "Target session ID, e.g. ses_...",
|
|
304
|
+
},
|
|
305
|
+
text: {
|
|
306
|
+
type: "string",
|
|
307
|
+
description: `Context text. Maximum ${limits.maxTextLength} characters; longer input is rejected.`,
|
|
308
|
+
},
|
|
309
|
+
description: {
|
|
310
|
+
type: "string",
|
|
311
|
+
description: `Short label shown in the transcript. Maximum ${MAX_DESCRIPTION_LENGTH} characters.`,
|
|
312
|
+
},
|
|
313
|
+
...outputStrategy.formatProperty(
|
|
314
|
+
formatPropertyDescription(outputStrategy, "structured output"),
|
|
315
|
+
),
|
|
316
|
+
},
|
|
317
|
+
required: ["sessionID", "text"],
|
|
318
|
+
additionalProperties: false,
|
|
319
|
+
},
|
|
320
|
+
output: outputStrategy.schema(CONTEXT_OUTPUT_SCHEMA),
|
|
321
|
+
options: { namespace: deps.config.namespace },
|
|
322
|
+
execute: async (rawInput, context) => {
|
|
323
|
+
const { input, format } = readToolInput(rawInput, outputStrategy);
|
|
324
|
+
const textError = maxLengthError(input?.text, limits.maxTextLength, "text");
|
|
325
|
+
if (textError) return toolError(textError);
|
|
326
|
+
const descriptionField = readTextField(
|
|
327
|
+
input?.description,
|
|
328
|
+
MAX_DESCRIPTION_LENGTH,
|
|
329
|
+
"description",
|
|
330
|
+
);
|
|
331
|
+
if (descriptionField.error) return toolError(descriptionField.error);
|
|
332
|
+
const sessionID = requireSessionID(input?.sessionID);
|
|
333
|
+
const text = requireText(input?.text, limits.maxTextLength);
|
|
334
|
+
const description = descriptionField.text;
|
|
335
|
+
try {
|
|
336
|
+
const result = await sendMessage(
|
|
337
|
+
{
|
|
338
|
+
sessionID,
|
|
339
|
+
text,
|
|
340
|
+
kind: SEND_KIND.synthetic,
|
|
341
|
+
resume: false,
|
|
342
|
+
description,
|
|
343
|
+
metadata: { source: CONTEXT_METADATA_SOURCE },
|
|
344
|
+
},
|
|
345
|
+
context,
|
|
346
|
+
"Context injection",
|
|
347
|
+
);
|
|
348
|
+
const chars = textLength(result.admitted?.text ?? text);
|
|
349
|
+
const output: ContextOutput = {
|
|
350
|
+
sessionID,
|
|
351
|
+
delivery: CONTEXT_DELIVERY,
|
|
352
|
+
chars,
|
|
353
|
+
};
|
|
354
|
+
if (description) output.description = description;
|
|
355
|
+
applySendResult(output, result);
|
|
356
|
+
return outputStrategy.respond(
|
|
357
|
+
`Context injected into ${sessionID}` +
|
|
358
|
+
(result.admitted?.id ? ` as ${result.admitted.id}` : "") +
|
|
359
|
+
` (${chars} chars).`,
|
|
360
|
+
output,
|
|
361
|
+
format,
|
|
362
|
+
);
|
|
363
|
+
} catch (error) {
|
|
364
|
+
if (error instanceof SendAdmissionError) return toolError(error.message);
|
|
365
|
+
throw error;
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
};
|