@ai-sdk/harness-opencode 1.0.151 → 1.0.153
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 +21 -0
- package/dist/bridge/host-tool-mcp.mjs +87 -5
- package/dist/bridge/host-tool-mcp.mjs.map +1 -1
- package/dist/bridge/index.mjs +237 -44
- package/dist/bridge/index.mjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
- package/src/bridge/create-emit-stream-event.ts +37 -0
- package/src/bridge/host-tool-mcp.ts +29 -1
- package/src/bridge/index.ts +191 -57
- package/src/bridge/opencode-host-input-tracker.ts +106 -0
- package/src/bridge/tool-relay.ts +15 -5
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":["SandboxSession"],"sources":["../src/opencode-auth.ts","../src/opencode-harness.ts","../src/version.ts","../src/index.ts"],"mappings":";;;;;KAmMY,6BAA6B;;;KCxG7B;WACD,OAAO;;;;;;WAMP,uBAAuB;;;;;;WAMvB,iBAAiB;;;;;WAKjB,aAAa;WACb;;;;;;WAMA;WACA;;;;;WAKA,eAAe;WACf;;;;;;;WAOA,YAAY;;;;;WAKZ,kBAAkB;;cAKvB;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["SandboxSession"],"sources":["../src/opencode-auth.ts","../src/opencode-harness.ts","../src/version.ts","../src/index.ts"],"mappings":";;;;;KAmMY,6BAA6B;;;KCxG7B;WACD,OAAO;;;;;;WAMP,uBAAuB;;;;;;WAMvB,iBAAiB;;;;;WAKjB,aAAa;WACb;;;;;;WAMA;WACA;;;;;WAKA,eAAe;WACf;;;;;;;WAOA,YAAY;;;;;WAKZ,kBAAkB;;cAKvB;;;;;;;;;;;;;QAu0C0gZ;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;;;;QAAzP;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;;;;;;;;UAAzP;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;;;;;UAAzP;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;UAA4K;;;;;;UAAgJ;;;;;;;;;;;;QAAhJ;;;;;;QAAgJ;;;;;;;;;;;;QAArjB;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;QAA4K;;;;;;QAAgJ;;;;;;;;;;;;;;;;;;UAArjB;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;;;;;;;;;;;;;;;QAAzP;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;;;;QAAzP;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;;;;;;;;UAAzP;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;;;;;UAAzP;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;UAA4K;;;;;;UAAgJ;;;;;;;;;;;;QAAhJ;;;;;;QAAgJ;;;;;;;;;;;;;;;;;;;UAArjB;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA9sCzvZ,eACd,WAAU,0BACT,iBAAiB;;;qBCrQP;;;;;;;qBCIA,oCAAQ;;;;;;;;;;;;;QAO+51D;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;;;;QAAzP;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;;;;;;;;UAAzP;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;;;;;UAAzP;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;UAA4K;;;;;;UAAgJ;;;;;;;;;;;;QAAhJ;;;;;;QAAgJ;;;;;;;;;;;;QAArjB;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;QAA4K;;;;;;QAAgJ;;;;;;;;;;;;;;;;;;UAArjB;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;;;;;;;;;;;;;;;QAAzP;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;;;;QAAzP;QAAoC;;;UAAmE;UAAsC;;QAAiE;QAA2C;;;;;;;;;;;;;;;UAAzP;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;;;;;UAAzP;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C;;;;;;;;;UAA4K;;;;;;UAAgJ;;;;;;;;;;;;QAAhJ;;;;;;QAAgJ;;;;;;;;;;;;;;;;;;;UAArjB;UAAoC;;;YAAmE;YAAsC;;UAAiE;UAA2C"}
|
package/dist/index.js
CHANGED
|
@@ -713,7 +713,7 @@ const startMessageSchema = harnessV1BridgeStartBaseSchema.extend({
|
|
|
713
713
|
z.discriminatedUnion("type", [startMessageSchema, ...harnessV1BridgeInboundCommandSchemas]);
|
|
714
714
|
//#endregion
|
|
715
715
|
//#region src/version.ts
|
|
716
|
-
const VERSION = "1.0.
|
|
716
|
+
const VERSION = "1.0.153";
|
|
717
717
|
//#endregion
|
|
718
718
|
//#region src/opencode-harness.ts
|
|
719
719
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/harness-opencode",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.153",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@ai-sdk/harness": "1.0.
|
|
29
|
+
"@ai-sdk/harness": "1.0.151",
|
|
30
30
|
"@ai-sdk/provider": "4.0.27",
|
|
31
31
|
"@ai-sdk/provider-utils": "5.0.59",
|
|
32
32
|
"ws": "8.21.0"
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
mapOpenCodeFinishReason,
|
|
12
12
|
translateLegacyStepFinishPart,
|
|
13
13
|
} from './opencode-finish-step';
|
|
14
|
+
import type { OpenCodeHostInputGroup } from './opencode-host-input-tracker';
|
|
14
15
|
import { mapUsage } from './opencode-usage';
|
|
15
16
|
import {
|
|
16
17
|
asOpenCodeObject,
|
|
@@ -35,6 +36,7 @@ export function createEmitStreamEvent({
|
|
|
35
36
|
getHostToolName,
|
|
36
37
|
authorizeHostToolCall,
|
|
37
38
|
onSubagentSession,
|
|
39
|
+
onToolCallStatus,
|
|
38
40
|
isMcpToolName,
|
|
39
41
|
stripWorkDir,
|
|
40
42
|
formatError,
|
|
@@ -57,6 +59,7 @@ export function createEmitStreamEvent({
|
|
|
57
59
|
input: unknown;
|
|
58
60
|
}) => void;
|
|
59
61
|
onSubagentSession?: (session: SubagentSession) => void;
|
|
62
|
+
onToolCallStatus?: OnToolCallStatus;
|
|
60
63
|
isMcpToolName: (toolName: string) => boolean;
|
|
61
64
|
stripWorkDir: (file: string) => string;
|
|
62
65
|
formatError: (error: unknown) => string;
|
|
@@ -171,6 +174,7 @@ export function createEmitStreamEvent({
|
|
|
171
174
|
getHostToolName,
|
|
172
175
|
authorizeHostToolCall,
|
|
173
176
|
onSubagentSession,
|
|
177
|
+
onToolCallStatus,
|
|
174
178
|
isMcpToolName,
|
|
175
179
|
});
|
|
176
180
|
return;
|
|
@@ -241,6 +245,7 @@ export function createEmitStreamEvent({
|
|
|
241
245
|
const callID = String(props.callID ?? event.id);
|
|
242
246
|
const command = String(props.command ?? '');
|
|
243
247
|
state.shellCommands.set(callID, command);
|
|
248
|
+
onToolCallStatus?.({ callID, status: 'started', group: undefined });
|
|
244
249
|
emit({
|
|
245
250
|
type: 'tool-call',
|
|
246
251
|
toolCallId: callID,
|
|
@@ -253,6 +258,7 @@ export function createEmitStreamEvent({
|
|
|
253
258
|
}
|
|
254
259
|
if (type === 'session.next.shell.ended') {
|
|
255
260
|
const callID = String(props.callID ?? event.id);
|
|
261
|
+
onToolCallStatus?.({ callID, status: 'settled', group: undefined });
|
|
256
262
|
emit({
|
|
257
263
|
type: 'tool-result',
|
|
258
264
|
toolCallId: callID,
|
|
@@ -288,6 +294,11 @@ export function createEmitStreamEvent({
|
|
|
288
294
|
const toolName = toWireToolName(rawToolName);
|
|
289
295
|
state.toolNames.set(callID, { rawToolName, toolName });
|
|
290
296
|
const hostToolName = getHostToolName(toolName, props.tool);
|
|
297
|
+
onToolCallStatus?.({
|
|
298
|
+
callID,
|
|
299
|
+
status: 'started',
|
|
300
|
+
group: toolCallGroup({ hostToolName, toolName }),
|
|
301
|
+
});
|
|
291
302
|
if (hostToolName) {
|
|
292
303
|
authorizeHostToolCall({
|
|
293
304
|
callID,
|
|
@@ -325,6 +336,7 @@ export function createEmitStreamEvent({
|
|
|
325
336
|
}
|
|
326
337
|
const toolName =
|
|
327
338
|
cachedTool?.toolName ?? toWireToolName(rawToolName || 'unknown');
|
|
339
|
+
onToolCallStatus?.({ callID, status: 'settled', group: undefined });
|
|
328
340
|
if (getHostToolName(toolName, rawToolName)) return;
|
|
329
341
|
emit({
|
|
330
342
|
type: 'tool-result',
|
|
@@ -403,6 +415,24 @@ export function createEmitStreamEvent({
|
|
|
403
415
|
};
|
|
404
416
|
}
|
|
405
417
|
|
|
418
|
+
type OnToolCallStatus = (input: {
|
|
419
|
+
callID: string;
|
|
420
|
+
status: 'started' | 'settled';
|
|
421
|
+
group: OpenCodeHostInputGroup | undefined;
|
|
422
|
+
}) => void;
|
|
423
|
+
|
|
424
|
+
function toolCallGroup({
|
|
425
|
+
hostToolName,
|
|
426
|
+
toolName,
|
|
427
|
+
}: {
|
|
428
|
+
hostToolName: string | undefined;
|
|
429
|
+
toolName: string;
|
|
430
|
+
}): OpenCodeHostInputGroup | undefined {
|
|
431
|
+
if (hostToolName) return 'host-tool';
|
|
432
|
+
if (toolName === 'agent') return 'subagent';
|
|
433
|
+
return undefined;
|
|
434
|
+
}
|
|
435
|
+
|
|
406
436
|
function closeLegacyOpenParts({
|
|
407
437
|
state,
|
|
408
438
|
emit,
|
|
@@ -453,6 +483,7 @@ function emitLegacyToolPart({
|
|
|
453
483
|
getHostToolName,
|
|
454
484
|
authorizeHostToolCall,
|
|
455
485
|
onSubagentSession,
|
|
486
|
+
onToolCallStatus,
|
|
456
487
|
isMcpToolName,
|
|
457
488
|
}: {
|
|
458
489
|
part: unknown;
|
|
@@ -472,6 +503,7 @@ function emitLegacyToolPart({
|
|
|
472
503
|
input: unknown;
|
|
473
504
|
}) => void;
|
|
474
505
|
onSubagentSession?: (session: SubagentSession) => void;
|
|
506
|
+
onToolCallStatus?: OnToolCallStatus;
|
|
475
507
|
isMcpToolName: (toolName: string) => boolean;
|
|
476
508
|
}): void {
|
|
477
509
|
const toolPart = legacyToolPartFromValue(part);
|
|
@@ -497,6 +529,11 @@ function emitLegacyToolPart({
|
|
|
497
529
|
}
|
|
498
530
|
state.toolNames.set(callID, { rawToolName, toolName });
|
|
499
531
|
const hostToolName = getHostToolName(toolName, rawToolName);
|
|
532
|
+
onToolCallStatus?.({
|
|
533
|
+
callID,
|
|
534
|
+
status: status === 'running' ? 'started' : 'settled',
|
|
535
|
+
group: toolCallGroup({ hostToolName, toolName }),
|
|
536
|
+
});
|
|
500
537
|
if (hostToolName) {
|
|
501
538
|
if (status === 'running') {
|
|
502
539
|
authorizeHostToolCall({
|
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
8
8
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
9
|
+
import {
|
|
10
|
+
convertHarnessToolModelOutput,
|
|
11
|
+
type BridgeTurn,
|
|
12
|
+
} from '@ai-sdk/harness/bridge';
|
|
9
13
|
import { readHostToolSchemasFromEnvironment } from './host-tool-schemas';
|
|
10
14
|
import { jsonSchemaToZodShape } from './json-schema-to-zod';
|
|
11
15
|
|
|
@@ -43,7 +47,30 @@ for (const schema of schemas) {
|
|
|
43
47
|
`Tool relay ${schema.name} failed with ${res.status}: ${body.slice(0, 500)}`,
|
|
44
48
|
);
|
|
45
49
|
}
|
|
46
|
-
const data = (await res.json()) as {
|
|
50
|
+
const data = (await res.json()) as {
|
|
51
|
+
result?: unknown;
|
|
52
|
+
isError?: boolean;
|
|
53
|
+
toolResult?: Awaited<
|
|
54
|
+
ReturnType<BridgeTurn['requestToolResult']>
|
|
55
|
+
>['toolResult'];
|
|
56
|
+
};
|
|
57
|
+
if (data.toolResult != null) {
|
|
58
|
+
const converted = convertHarnessToolModelOutput({
|
|
59
|
+
output: data.toolResult.output,
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
content: converted.content.map(part =>
|
|
63
|
+
part.type === 'text'
|
|
64
|
+
? part
|
|
65
|
+
: {
|
|
66
|
+
type: 'image' as const,
|
|
67
|
+
data: part.data,
|
|
68
|
+
mimeType: part.mediaType,
|
|
69
|
+
},
|
|
70
|
+
),
|
|
71
|
+
isError: data.isError === true || converted.isError,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
47
74
|
return {
|
|
48
75
|
content: [
|
|
49
76
|
{
|
|
@@ -51,6 +78,7 @@ for (const schema of schemas) {
|
|
|
51
78
|
text: JSON.stringify(data.result ?? null),
|
|
52
79
|
},
|
|
53
80
|
],
|
|
81
|
+
...(data.isError === true ? { isError: true } : {}),
|
|
54
82
|
};
|
|
55
83
|
} catch (err) {
|
|
56
84
|
return {
|
package/src/bridge/index.ts
CHANGED
|
@@ -14,6 +14,10 @@ import {
|
|
|
14
14
|
createOpencodeClient,
|
|
15
15
|
createOpencodeServer,
|
|
16
16
|
} from '@opencode-ai/sdk/v2';
|
|
17
|
+
import {
|
|
18
|
+
createOpenCodeHostInputTracker,
|
|
19
|
+
type OpenCodeHostInputTracker,
|
|
20
|
+
} from './opencode-host-input-tracker';
|
|
17
21
|
import {
|
|
18
22
|
createTranslationState,
|
|
19
23
|
emitOpenCodeStreamStart,
|
|
@@ -137,6 +141,12 @@ const runtime: RuntimeState = {
|
|
|
137
141
|
toolNames: new Set(),
|
|
138
142
|
mcpToolPrefixes: new Set(),
|
|
139
143
|
};
|
|
144
|
+
/*
|
|
145
|
+
* The tool relay outlives a single turn, so it reports host tool waits to the
|
|
146
|
+
* tracker of the turn that is currently running.
|
|
147
|
+
*/
|
|
148
|
+
let currentHostInputTracker: OpenCodeHostInputTracker | undefined;
|
|
149
|
+
let currentTurn: BridgeTurn | undefined;
|
|
140
150
|
prependOpenCodeBinToPath({ bootstrapDir, env: procEnv });
|
|
141
151
|
|
|
142
152
|
await runBridge<StartMessage>({
|
|
@@ -148,10 +158,11 @@ await runBridge<StartMessage>({
|
|
|
148
158
|
});
|
|
149
159
|
|
|
150
160
|
async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
|
|
161
|
+
currentTurn = turn;
|
|
151
162
|
const emit: Emit = msg => turn.emit(msg as BridgeEvent);
|
|
152
163
|
let totalUsage: HarnessUsage | undefined;
|
|
153
164
|
try {
|
|
154
|
-
await ensureRuntime({ start
|
|
165
|
+
await ensureRuntime({ start });
|
|
155
166
|
const client = runtime.client!;
|
|
156
167
|
if (start.skillsChanged) {
|
|
157
168
|
await client.instance.dispose({ directory: workdir });
|
|
@@ -199,12 +210,8 @@ async function switchSessionModel({
|
|
|
199
210
|
|
|
200
211
|
async function ensureRuntime({
|
|
201
212
|
start,
|
|
202
|
-
turn,
|
|
203
|
-
emit,
|
|
204
213
|
}: {
|
|
205
214
|
start: StartMessage;
|
|
206
|
-
turn: BridgeTurn;
|
|
207
|
-
emit: Emit;
|
|
208
215
|
}): Promise<void> {
|
|
209
216
|
if (
|
|
210
217
|
runtime.client &&
|
|
@@ -220,8 +227,6 @@ async function ensureRuntime({
|
|
|
220
227
|
runtime.toolNames = new Set(start.tools.map(tool => tool.name));
|
|
221
228
|
runtime.relay = await startToolRelay({
|
|
222
229
|
tools: start.tools,
|
|
223
|
-
emit,
|
|
224
|
-
requestToolResult: turn.requestToolResult,
|
|
225
230
|
});
|
|
226
231
|
}
|
|
227
232
|
const toolSchemasPath =
|
|
@@ -715,6 +720,8 @@ async function runPrompt({
|
|
|
715
720
|
let terminalError: string | undefined;
|
|
716
721
|
let submittingUserMessage = false;
|
|
717
722
|
const state = createTranslationState();
|
|
723
|
+
const hostInputTracker = createOpenCodeHostInputTracker({ emit });
|
|
724
|
+
currentHostInputTracker = hostInputTracker;
|
|
718
725
|
const initialSessionTokens = await readSessionTokens({
|
|
719
726
|
client,
|
|
720
727
|
sessionId,
|
|
@@ -732,11 +739,13 @@ async function runPrompt({
|
|
|
732
739
|
builtinToolFiltering: start.builtinToolFiltering,
|
|
733
740
|
turn,
|
|
734
741
|
state,
|
|
742
|
+
hostInputTracker,
|
|
735
743
|
emit: msg => {
|
|
736
744
|
if (msg.type === 'text-delta' || msg.type === 'reasoning-delta') {
|
|
737
745
|
sawContent = true;
|
|
738
746
|
}
|
|
739
747
|
if (msg.type === 'finish-step') {
|
|
748
|
+
hostInputTracker.observeFinishStep();
|
|
740
749
|
sawFinishStep = true;
|
|
741
750
|
stepUsage = addUsage({
|
|
742
751
|
left: stepUsage,
|
|
@@ -1024,6 +1033,7 @@ async function consumeEvents({
|
|
|
1024
1033
|
builtinToolFiltering,
|
|
1025
1034
|
turn,
|
|
1026
1035
|
state,
|
|
1036
|
+
hostInputTracker,
|
|
1027
1037
|
emit,
|
|
1028
1038
|
signal,
|
|
1029
1039
|
onSubscribed,
|
|
@@ -1035,6 +1045,7 @@ async function consumeEvents({
|
|
|
1035
1045
|
builtinToolFiltering: StartMessage['builtinToolFiltering'];
|
|
1036
1046
|
turn: BridgeTurn;
|
|
1037
1047
|
state: TranslationState;
|
|
1048
|
+
hostInputTracker?: OpenCodeHostInputTracker;
|
|
1038
1049
|
emit: Emit;
|
|
1039
1050
|
signal: AbortSignal;
|
|
1040
1051
|
onSubscribed?: () => void;
|
|
@@ -1069,6 +1080,7 @@ async function consumeEvents({
|
|
|
1069
1080
|
getHostToolName,
|
|
1070
1081
|
authorizeHostToolCall: input => authorizeHostToolCall({ ...input, state }),
|
|
1071
1082
|
onSubagentSession: registerSubagentSession(sessionId),
|
|
1083
|
+
onToolCallStatus: input => hostInputTracker?.observeToolCall(input),
|
|
1072
1084
|
isMcpToolName: toolName =>
|
|
1073
1085
|
[...runtime.mcpToolPrefixes].some(prefix => toolName.startsWith(prefix)),
|
|
1074
1086
|
stripWorkDir,
|
|
@@ -1141,56 +1153,141 @@ async function consumeEvents({
|
|
|
1141
1153
|
}
|
|
1142
1154
|
processEvent(event);
|
|
1143
1155
|
};
|
|
1144
|
-
|
|
1145
|
-
|
|
1156
|
+
/*
|
|
1157
|
+
* Permission requests and questions are answered without blocking the event
|
|
1158
|
+
* loop, so every request of a step reaches the host before it pauses. A
|
|
1159
|
+
* failed handler still ends the event loop with its error.
|
|
1160
|
+
*/
|
|
1161
|
+
let rejectHandlerFailure!: (error: unknown) => void;
|
|
1162
|
+
const handlerFailure = new Promise<never>((_, reject) => {
|
|
1163
|
+
rejectHandlerFailure = reject;
|
|
1164
|
+
});
|
|
1165
|
+
handlerFailure.catch(() => {});
|
|
1166
|
+
const runHandler = (handler: Promise<void>) => {
|
|
1167
|
+
handler.catch(error => rejectHandlerFailure(error));
|
|
1168
|
+
};
|
|
1169
|
+
const hostInputWait = ({
|
|
1170
|
+
toolCallId,
|
|
1171
|
+
isDescendant,
|
|
1172
|
+
settlesToolCall,
|
|
1173
|
+
}: {
|
|
1174
|
+
toolCallId: string;
|
|
1175
|
+
isDescendant: boolean;
|
|
1176
|
+
settlesToolCall: boolean;
|
|
1177
|
+
}): HostInputWait | undefined => {
|
|
1178
|
+
if (hostInputTracker == null) return undefined;
|
|
1179
|
+
if (isDescendant) {
|
|
1180
|
+
const id = toolCallId;
|
|
1181
|
+
return {
|
|
1182
|
+
markWaiting: () =>
|
|
1183
|
+
hostInputTracker.markGroupWaiting({ group: 'subagent', id }),
|
|
1184
|
+
clearWaiting: () =>
|
|
1185
|
+
hostInputTracker.clearGroupWaiting({ group: 'subagent', id }),
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
return {
|
|
1189
|
+
markWaiting: () => hostInputTracker.markWaiting({ toolCallId }),
|
|
1190
|
+
clearWaiting: () =>
|
|
1191
|
+
hostInputTracker.clearWaiting({
|
|
1192
|
+
toolCallId,
|
|
1193
|
+
settled: settlesToolCall,
|
|
1194
|
+
}),
|
|
1195
|
+
};
|
|
1196
|
+
};
|
|
1197
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
1198
|
+
try {
|
|
1199
|
+
for (;;) {
|
|
1200
|
+
const next = await Promise.race([iterator.next(), handlerFailure]);
|
|
1201
|
+
if (next.done) break;
|
|
1202
|
+
if (consumeEvent(next.value)) break;
|
|
1203
|
+
}
|
|
1204
|
+
} finally {
|
|
1205
|
+
void iterator.return?.();
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
function consumeEvent(rawEvent: unknown): boolean {
|
|
1209
|
+
if (signal.aborted || turn.abortSignal.aborted) return true;
|
|
1146
1210
|
const event = unwrapOpenCodeEvent(rawEvent);
|
|
1147
1211
|
const eventSessionId = event ? getOpenCodeEventSessionId(event) : undefined;
|
|
1148
|
-
if (!event)
|
|
1212
|
+
if (!event) return false;
|
|
1149
1213
|
const scopedSessionId =
|
|
1150
1214
|
!eventSessionId || eventSessionId === sessionId
|
|
1151
1215
|
? sessionId
|
|
1152
1216
|
: taskSessionIds.has(eventSessionId)
|
|
1153
1217
|
? eventSessionId
|
|
1154
1218
|
: undefined;
|
|
1155
|
-
if (!scopedSessionId)
|
|
1219
|
+
if (!scopedSessionId) return false;
|
|
1156
1220
|
const isDescendant = scopedSessionId !== sessionId;
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1221
|
+
const waitFor = ({
|
|
1222
|
+
toolCallId,
|
|
1223
|
+
settlesToolCall,
|
|
1224
|
+
}: {
|
|
1225
|
+
toolCallId: string;
|
|
1226
|
+
settlesToolCall: boolean;
|
|
1227
|
+
}) =>
|
|
1228
|
+
hostInputWait({
|
|
1229
|
+
toolCallId: isDescendant
|
|
1230
|
+
? `${scopedSessionId}:${toolCallId}`
|
|
1231
|
+
: toolCallId,
|
|
1232
|
+
isDescendant,
|
|
1233
|
+
settlesToolCall,
|
|
1163
1234
|
});
|
|
1235
|
+
if (event.type === 'question.asked') {
|
|
1236
|
+
runHandler(
|
|
1237
|
+
handleQuestion({
|
|
1238
|
+
client,
|
|
1239
|
+
turn,
|
|
1240
|
+
emit,
|
|
1241
|
+
event,
|
|
1242
|
+
waitFor,
|
|
1243
|
+
}),
|
|
1244
|
+
);
|
|
1164
1245
|
} else if (event.type === 'permission.v2.asked') {
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1246
|
+
runHandler(
|
|
1247
|
+
handlePermissionV2({
|
|
1248
|
+
client,
|
|
1249
|
+
sessionId: scopedSessionId,
|
|
1250
|
+
permissionMode,
|
|
1251
|
+
builtinToolFiltering,
|
|
1252
|
+
turn,
|
|
1253
|
+
emit,
|
|
1254
|
+
event,
|
|
1255
|
+
waitFor,
|
|
1256
|
+
}),
|
|
1257
|
+
);
|
|
1174
1258
|
} else if (event.type === 'permission.asked') {
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1259
|
+
runHandler(
|
|
1260
|
+
handlePermission({
|
|
1261
|
+
client,
|
|
1262
|
+
sessionId: scopedSessionId,
|
|
1263
|
+
permissionMode,
|
|
1264
|
+
builtinToolFiltering,
|
|
1265
|
+
turn,
|
|
1266
|
+
emit,
|
|
1267
|
+
event,
|
|
1268
|
+
waitFor,
|
|
1269
|
+
}),
|
|
1270
|
+
);
|
|
1184
1271
|
} else if (isDescendant) {
|
|
1185
1272
|
processDescendantEvent(scopedSessionId, event);
|
|
1186
1273
|
} else {
|
|
1187
1274
|
emitStreamEvent(event);
|
|
1188
1275
|
}
|
|
1189
|
-
if (isDescendant)
|
|
1190
|
-
|
|
1276
|
+
if (isDescendant) return false;
|
|
1277
|
+
return onEvent?.(event) === true;
|
|
1191
1278
|
}
|
|
1192
1279
|
}
|
|
1193
1280
|
|
|
1281
|
+
type HostInputWait = {
|
|
1282
|
+
markWaiting(): void;
|
|
1283
|
+
clearWaiting(): void;
|
|
1284
|
+
};
|
|
1285
|
+
|
|
1286
|
+
type WaitForHostInput = (input: {
|
|
1287
|
+
toolCallId: string;
|
|
1288
|
+
settlesToolCall: boolean;
|
|
1289
|
+
}) => HostInputWait | undefined;
|
|
1290
|
+
|
|
1194
1291
|
function getSubagentStepId(event: OpenCodeEvent | undefined) {
|
|
1195
1292
|
if (event?.type === 'message.part.updated') {
|
|
1196
1293
|
const part = asOpenCodeObject(event.properties?.part);
|
|
@@ -1206,11 +1303,13 @@ async function handleQuestion({
|
|
|
1206
1303
|
turn,
|
|
1207
1304
|
emit,
|
|
1208
1305
|
event,
|
|
1306
|
+
waitFor,
|
|
1209
1307
|
}: {
|
|
1210
1308
|
client: OpenCodeClient;
|
|
1211
1309
|
turn: BridgeTurn;
|
|
1212
1310
|
emit: Emit;
|
|
1213
1311
|
event: OpenCodeEvent;
|
|
1312
|
+
waitFor?: WaitForHostInput;
|
|
1214
1313
|
}): Promise<void> {
|
|
1215
1314
|
const nativeRequest = event.properties as OpenCodeQuestionRequest | undefined;
|
|
1216
1315
|
if (
|
|
@@ -1238,17 +1337,21 @@ async function handleQuestion({
|
|
|
1238
1337
|
});
|
|
1239
1338
|
|
|
1240
1339
|
const questionKey = openCodeQuestionKey(nativeRequest);
|
|
1241
|
-
const
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
continuedRequest
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1340
|
+
const wait = waitFor?.({ toolCallId, settlesToolCall: true });
|
|
1341
|
+
wait?.markWaiting();
|
|
1342
|
+
const result = await turn
|
|
1343
|
+
.requestToolResult({
|
|
1344
|
+
toolCallId,
|
|
1345
|
+
matches: candidate => {
|
|
1346
|
+
const continuedRequest = candidate.toolResult?.providerOptions?.opencode
|
|
1347
|
+
?.nativeRequest as OpenCodeQuestionRequest | undefined;
|
|
1348
|
+
return (
|
|
1349
|
+
continuedRequest != null &&
|
|
1350
|
+
openCodeQuestionKey(continuedRequest) === questionKey
|
|
1351
|
+
);
|
|
1352
|
+
},
|
|
1353
|
+
})
|
|
1354
|
+
.finally(() => wait?.clearWaiting());
|
|
1252
1355
|
const nativeResponse = toOpenCodeQuestionResponse({
|
|
1253
1356
|
nativeRequest,
|
|
1254
1357
|
output: result.output as Parameters<
|
|
@@ -1286,6 +1389,7 @@ async function handlePermissionV2({
|
|
|
1286
1389
|
turn,
|
|
1287
1390
|
emit,
|
|
1288
1391
|
event,
|
|
1392
|
+
waitFor,
|
|
1289
1393
|
}: {
|
|
1290
1394
|
client: OpenCodeClient;
|
|
1291
1395
|
sessionId: string;
|
|
@@ -1294,6 +1398,7 @@ async function handlePermissionV2({
|
|
|
1294
1398
|
turn: BridgeTurn;
|
|
1295
1399
|
emit: Emit;
|
|
1296
1400
|
event: OpenCodeEvent;
|
|
1401
|
+
waitFor?: WaitForHostInput;
|
|
1297
1402
|
}): Promise<void> {
|
|
1298
1403
|
const props = event.properties ?? {};
|
|
1299
1404
|
const requestID = String(props.id ?? '');
|
|
@@ -1309,6 +1414,7 @@ async function handlePermissionV2({
|
|
|
1309
1414
|
builtinToolFiltering,
|
|
1310
1415
|
turn,
|
|
1311
1416
|
emit,
|
|
1417
|
+
waitFor,
|
|
1312
1418
|
});
|
|
1313
1419
|
await client.v2.session.permission.reply({
|
|
1314
1420
|
sessionID: sessionId,
|
|
@@ -1326,6 +1432,7 @@ async function handlePermission({
|
|
|
1326
1432
|
turn,
|
|
1327
1433
|
emit,
|
|
1328
1434
|
event,
|
|
1435
|
+
waitFor,
|
|
1329
1436
|
}: {
|
|
1330
1437
|
client: OpenCodeClient;
|
|
1331
1438
|
sessionId: string;
|
|
@@ -1334,6 +1441,7 @@ async function handlePermission({
|
|
|
1334
1441
|
turn: BridgeTurn;
|
|
1335
1442
|
emit: Emit;
|
|
1336
1443
|
event: OpenCodeEvent;
|
|
1444
|
+
waitFor?: WaitForHostInput;
|
|
1337
1445
|
}): Promise<void> {
|
|
1338
1446
|
const props = event.properties ?? {};
|
|
1339
1447
|
const requestID = String(props.id ?? '');
|
|
@@ -1348,6 +1456,7 @@ async function handlePermission({
|
|
|
1348
1456
|
builtinToolFiltering,
|
|
1349
1457
|
turn,
|
|
1350
1458
|
emit,
|
|
1459
|
+
waitFor,
|
|
1351
1460
|
});
|
|
1352
1461
|
await client.permission.reply({
|
|
1353
1462
|
requestID,
|
|
@@ -1367,6 +1476,7 @@ async function selectPermissionReply({
|
|
|
1367
1476
|
builtinToolFiltering,
|
|
1368
1477
|
turn,
|
|
1369
1478
|
emit,
|
|
1479
|
+
waitFor,
|
|
1370
1480
|
}: {
|
|
1371
1481
|
action: string;
|
|
1372
1482
|
resources: string[];
|
|
@@ -1376,7 +1486,15 @@ async function selectPermissionReply({
|
|
|
1376
1486
|
builtinToolFiltering: StartMessage['builtinToolFiltering'];
|
|
1377
1487
|
turn: BridgeTurn;
|
|
1378
1488
|
emit: Emit;
|
|
1489
|
+
waitFor?: WaitForHostInput;
|
|
1379
1490
|
}): Promise<{ reply: 'once' | 'always' | 'reject'; message?: string }> {
|
|
1491
|
+
const requestApproval = async () => {
|
|
1492
|
+
const wait = waitFor?.({ toolCallId, settlesToolCall: false });
|
|
1493
|
+
wait?.markWaiting();
|
|
1494
|
+
return turn
|
|
1495
|
+
.requestToolApproval(requestID)
|
|
1496
|
+
.finally(() => wait?.clearWaiting());
|
|
1497
|
+
};
|
|
1380
1498
|
const toolName = toPermissionToolName(action);
|
|
1381
1499
|
if (
|
|
1382
1500
|
isBuiltinToolInactive({ toolName, toolFiltering: builtinToolFiltering })
|
|
@@ -1386,7 +1504,7 @@ async function selectPermissionReply({
|
|
|
1386
1504
|
approvalId: requestID,
|
|
1387
1505
|
toolCallId,
|
|
1388
1506
|
});
|
|
1389
|
-
const decision = await
|
|
1507
|
+
const decision = await requestApproval();
|
|
1390
1508
|
return decision.approved
|
|
1391
1509
|
? { reply: 'once' }
|
|
1392
1510
|
: {
|
|
@@ -1412,7 +1530,7 @@ async function selectPermissionReply({
|
|
|
1412
1530
|
approvalId: requestID,
|
|
1413
1531
|
toolCallId,
|
|
1414
1532
|
});
|
|
1415
|
-
const decision = await
|
|
1533
|
+
const decision = await requestApproval();
|
|
1416
1534
|
return decision.approved
|
|
1417
1535
|
? { reply: 'once' }
|
|
1418
1536
|
: {
|
|
@@ -1718,16 +1836,32 @@ function emitAssistantContentPart(part: unknown, emit: Emit): void {
|
|
|
1718
1836
|
|
|
1719
1837
|
async function startToolRelay({
|
|
1720
1838
|
tools,
|
|
1721
|
-
emit,
|
|
1722
|
-
requestToolResult,
|
|
1723
1839
|
}: {
|
|
1724
1840
|
tools: ReadonlyArray<{ name: string }>;
|
|
1725
|
-
emit: Emit;
|
|
1726
|
-
requestToolResult: (
|
|
1727
|
-
toolCallId: string,
|
|
1728
|
-
) => Promise<{ output: unknown; isError?: boolean }>;
|
|
1729
1841
|
}): Promise<ToolRelay> {
|
|
1730
|
-
return startAuthorizedToolRelay({
|
|
1842
|
+
return startAuthorizedToolRelay({
|
|
1843
|
+
tools,
|
|
1844
|
+
emit: message => {
|
|
1845
|
+
currentTurn?.emit(message as BridgeEvent);
|
|
1846
|
+
},
|
|
1847
|
+
requestToolResult: toolCallId => {
|
|
1848
|
+
if (!currentTurn) throw new Error('No active turn in OpenCode bridge');
|
|
1849
|
+
return currentTurn.requestToolResult(toolCallId);
|
|
1850
|
+
},
|
|
1851
|
+
onWaitingChange: ({ requestId, waiting }) => {
|
|
1852
|
+
if (waiting) {
|
|
1853
|
+
currentHostInputTracker?.markGroupWaiting({
|
|
1854
|
+
group: 'host-tool',
|
|
1855
|
+
id: requestId,
|
|
1856
|
+
});
|
|
1857
|
+
} else {
|
|
1858
|
+
currentHostInputTracker?.clearGroupWaiting({
|
|
1859
|
+
group: 'host-tool',
|
|
1860
|
+
id: requestId,
|
|
1861
|
+
});
|
|
1862
|
+
}
|
|
1863
|
+
},
|
|
1864
|
+
});
|
|
1731
1865
|
}
|
|
1732
1866
|
|
|
1733
1867
|
function createDeferred<T>(): {
|