@ai-sdk/harness-opencode 1.0.152 → 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 +10 -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/bridge/index.mjs
CHANGED
|
@@ -578,6 +578,73 @@ function serialiseError(err) {
|
|
|
578
578
|
return err;
|
|
579
579
|
}
|
|
580
580
|
//#endregion
|
|
581
|
+
//#region src/bridge/opencode-host-input-tracker.ts
|
|
582
|
+
function createOpenCodeHostInputTracker({ emit }) {
|
|
583
|
+
const unsettledToolCalls = /* @__PURE__ */ new Map();
|
|
584
|
+
const waitingToolCallIds = /* @__PURE__ */ new Set();
|
|
585
|
+
const groupWaits = /* @__PURE__ */ new Map([["host-tool", /* @__PURE__ */ new Set()], ["subagent", /* @__PURE__ */ new Set()]]);
|
|
586
|
+
const coveredIds = /* @__PURE__ */ new Set();
|
|
587
|
+
const emitEarlyFinishStepIfReady = () => {
|
|
588
|
+
if (unsettledToolCalls.size === 0) return;
|
|
589
|
+
const notWaitingByGroup = /* @__PURE__ */ new Map();
|
|
590
|
+
for (const [callID, group] of unsettledToolCalls) {
|
|
591
|
+
if (waitingToolCallIds.has(callID)) continue;
|
|
592
|
+
if (group == null) return;
|
|
593
|
+
notWaitingByGroup.set(group, (notWaitingByGroup.get(group) ?? 0) + 1);
|
|
594
|
+
}
|
|
595
|
+
for (const [group, count] of notWaitingByGroup) if (count > groupWaits.get(group).size) return;
|
|
596
|
+
const uncovered = [...waitingToolCallIds, ...[...groupWaits.values()].flatMap((ids) => [...ids])].filter((id) => !coveredIds.has(id));
|
|
597
|
+
if (uncovered.length === 0) return;
|
|
598
|
+
for (const id of uncovered) coveredIds.add(id);
|
|
599
|
+
emit({
|
|
600
|
+
type: "finish-step",
|
|
601
|
+
finishReason: {
|
|
602
|
+
unified: "tool-calls",
|
|
603
|
+
raw: "host-input"
|
|
604
|
+
},
|
|
605
|
+
usage: {
|
|
606
|
+
inputTokens: {
|
|
607
|
+
total: 0,
|
|
608
|
+
noCache: 0,
|
|
609
|
+
cacheRead: 0,
|
|
610
|
+
cacheWrite: 0
|
|
611
|
+
},
|
|
612
|
+
outputTokens: {
|
|
613
|
+
total: 0,
|
|
614
|
+
text: 0
|
|
615
|
+
}
|
|
616
|
+
},
|
|
617
|
+
harnessMetadata: { opencode: { inferredStep: true } }
|
|
618
|
+
});
|
|
619
|
+
};
|
|
620
|
+
return {
|
|
621
|
+
observeToolCall({ callID, status, group }) {
|
|
622
|
+
if (status === "started") unsettledToolCalls.set(callID, group);
|
|
623
|
+
else unsettledToolCalls.delete(callID);
|
|
624
|
+
emitEarlyFinishStepIfReady();
|
|
625
|
+
},
|
|
626
|
+
markWaiting({ toolCallId }) {
|
|
627
|
+
if (!unsettledToolCalls.has(toolCallId)) unsettledToolCalls.set(toolCallId, void 0);
|
|
628
|
+
waitingToolCallIds.add(toolCallId);
|
|
629
|
+
emitEarlyFinishStepIfReady();
|
|
630
|
+
},
|
|
631
|
+
clearWaiting({ toolCallId, settled = false }) {
|
|
632
|
+
waitingToolCallIds.delete(toolCallId);
|
|
633
|
+
if (settled) unsettledToolCalls.delete(toolCallId);
|
|
634
|
+
},
|
|
635
|
+
markGroupWaiting({ group, id }) {
|
|
636
|
+
groupWaits.get(group).add(id);
|
|
637
|
+
emitEarlyFinishStepIfReady();
|
|
638
|
+
},
|
|
639
|
+
clearGroupWaiting({ group, id }) {
|
|
640
|
+
groupWaits.get(group).delete(id);
|
|
641
|
+
},
|
|
642
|
+
observeFinishStep() {
|
|
643
|
+
coveredIds.clear();
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
//#endregion
|
|
581
648
|
//#region src/bridge/opencode-types.ts
|
|
582
649
|
function asOpenCodeObject(value) {
|
|
583
650
|
return value != null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
@@ -965,7 +1032,7 @@ function translateLegacyStepFinishPart(value) {
|
|
|
965
1032
|
}
|
|
966
1033
|
//#endregion
|
|
967
1034
|
//#region src/bridge/create-emit-stream-event.ts
|
|
968
|
-
function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireToolName, nativeNameField, getHostToolName, authorizeHostToolCall, onSubagentSession, isMcpToolName, stripWorkDir, formatError }) {
|
|
1035
|
+
function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireToolName, nativeNameField, getHostToolName, authorizeHostToolCall, onSubagentSession, onToolCallStatus, isMcpToolName, stripWorkDir, formatError }) {
|
|
969
1036
|
const compactionMessages = /* @__PURE__ */ new Set();
|
|
970
1037
|
let compactionTrigger = "auto";
|
|
971
1038
|
let compaction;
|
|
@@ -1080,6 +1147,7 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1080
1147
|
getHostToolName,
|
|
1081
1148
|
authorizeHostToolCall,
|
|
1082
1149
|
onSubagentSession,
|
|
1150
|
+
onToolCallStatus,
|
|
1083
1151
|
isMcpToolName
|
|
1084
1152
|
});
|
|
1085
1153
|
return;
|
|
@@ -1152,6 +1220,11 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1152
1220
|
const callID = String(props.callID ?? event.id);
|
|
1153
1221
|
const command = String(props.command ?? "");
|
|
1154
1222
|
state.shellCommands.set(callID, command);
|
|
1223
|
+
onToolCallStatus?.({
|
|
1224
|
+
callID,
|
|
1225
|
+
status: "started",
|
|
1226
|
+
group: void 0
|
|
1227
|
+
});
|
|
1155
1228
|
emit({
|
|
1156
1229
|
type: "tool-call",
|
|
1157
1230
|
toolCallId: callID,
|
|
@@ -1164,6 +1237,11 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1164
1237
|
}
|
|
1165
1238
|
if (type === "session.next.shell.ended") {
|
|
1166
1239
|
const callID = String(props.callID ?? event.id);
|
|
1240
|
+
onToolCallStatus?.({
|
|
1241
|
+
callID,
|
|
1242
|
+
status: "settled",
|
|
1243
|
+
group: void 0
|
|
1244
|
+
});
|
|
1167
1245
|
emit({
|
|
1168
1246
|
type: "tool-result",
|
|
1169
1247
|
toolCallId: callID,
|
|
@@ -1194,6 +1272,14 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1194
1272
|
toolName
|
|
1195
1273
|
});
|
|
1196
1274
|
const hostToolName = getHostToolName(toolName, props.tool);
|
|
1275
|
+
onToolCallStatus?.({
|
|
1276
|
+
callID,
|
|
1277
|
+
status: "started",
|
|
1278
|
+
group: toolCallGroup({
|
|
1279
|
+
hostToolName,
|
|
1280
|
+
toolName
|
|
1281
|
+
})
|
|
1282
|
+
});
|
|
1197
1283
|
if (hostToolName) {
|
|
1198
1284
|
authorizeHostToolCall({
|
|
1199
1285
|
callID,
|
|
@@ -1224,6 +1310,11 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1224
1310
|
const rawToolName = cachedTool?.rawToolName ?? String(props.tool ?? "");
|
|
1225
1311
|
if (rawToolName === "StructuredOutput" || rawToolName === "question") return;
|
|
1226
1312
|
const toolName = cachedTool?.toolName ?? toWireToolName(rawToolName || "unknown");
|
|
1313
|
+
onToolCallStatus?.({
|
|
1314
|
+
callID,
|
|
1315
|
+
status: "settled",
|
|
1316
|
+
group: void 0
|
|
1317
|
+
});
|
|
1227
1318
|
if (getHostToolName(toolName, rawToolName)) return;
|
|
1228
1319
|
emit({
|
|
1229
1320
|
type: "tool-result",
|
|
@@ -1290,6 +1381,10 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1290
1381
|
}
|
|
1291
1382
|
};
|
|
1292
1383
|
}
|
|
1384
|
+
function toolCallGroup({ hostToolName, toolName }) {
|
|
1385
|
+
if (hostToolName) return "host-tool";
|
|
1386
|
+
if (toolName === "agent") return "subagent";
|
|
1387
|
+
}
|
|
1293
1388
|
function closeLegacyOpenParts({ state, emit }) {
|
|
1294
1389
|
for (const id of state.legacyReasoningPartIds) {
|
|
1295
1390
|
emit({
|
|
@@ -1324,7 +1419,7 @@ function emitLegacyStepFinishPart({ part, state, emit }) {
|
|
|
1324
1419
|
emit(event);
|
|
1325
1420
|
return true;
|
|
1326
1421
|
}
|
|
1327
|
-
function emitLegacyToolPart({ part, state, emit, toWireToolName, nativeNameField, getHostToolName, authorizeHostToolCall, onSubagentSession, isMcpToolName }) {
|
|
1422
|
+
function emitLegacyToolPart({ part, state, emit, toWireToolName, nativeNameField, getHostToolName, authorizeHostToolCall, onSubagentSession, onToolCallStatus, isMcpToolName }) {
|
|
1328
1423
|
const toolPart = legacyToolPartFromValue(part);
|
|
1329
1424
|
if (!toolPart) return;
|
|
1330
1425
|
const status = toolPart.status;
|
|
@@ -1350,6 +1445,14 @@ function emitLegacyToolPart({ part, state, emit, toWireToolName, nativeNameField
|
|
|
1350
1445
|
toolName
|
|
1351
1446
|
});
|
|
1352
1447
|
const hostToolName = getHostToolName(toolName, rawToolName);
|
|
1448
|
+
onToolCallStatus?.({
|
|
1449
|
+
callID,
|
|
1450
|
+
status: status === "running" ? "started" : "settled",
|
|
1451
|
+
group: toolCallGroup({
|
|
1452
|
+
hostToolName,
|
|
1453
|
+
toolName
|
|
1454
|
+
})
|
|
1455
|
+
});
|
|
1353
1456
|
if (hostToolName) {
|
|
1354
1457
|
if (status === "running") authorizeHostToolCall({
|
|
1355
1458
|
callID,
|
|
@@ -1552,7 +1655,7 @@ function normalizeJsonValue(value) {
|
|
|
1552
1655
|
}
|
|
1553
1656
|
//#endregion
|
|
1554
1657
|
//#region src/bridge/tool-relay.ts
|
|
1555
|
-
async function startAuthorizedToolRelay({ tools, emit, requestToolResult, authorizer = new ToolRelayAuthorizer() }) {
|
|
1658
|
+
async function startAuthorizedToolRelay({ tools, emit, requestToolResult, onWaitingChange, authorizer = new ToolRelayAuthorizer() }) {
|
|
1556
1659
|
const toolNames = new Set(tools.map((tool) => tool.name));
|
|
1557
1660
|
const server = createServer(async (req, res) => {
|
|
1558
1661
|
try {
|
|
@@ -1585,7 +1688,14 @@ async function startAuthorizedToolRelay({ tools, emit, requestToolResult, author
|
|
|
1585
1688
|
input: JSON.stringify(input ?? {}),
|
|
1586
1689
|
providerExecuted: false
|
|
1587
1690
|
});
|
|
1588
|
-
|
|
1691
|
+
onWaitingChange?.({
|
|
1692
|
+
requestId,
|
|
1693
|
+
waiting: true
|
|
1694
|
+
});
|
|
1695
|
+
const { output, isError, toolResult } = await requestToolResult(requestId).finally(() => onWaitingChange?.({
|
|
1696
|
+
requestId,
|
|
1697
|
+
waiting: false
|
|
1698
|
+
}));
|
|
1589
1699
|
emit({
|
|
1590
1700
|
type: "tool-result",
|
|
1591
1701
|
toolCallId: requestId,
|
|
@@ -1594,7 +1704,11 @@ async function startAuthorizedToolRelay({ tools, emit, requestToolResult, author
|
|
|
1594
1704
|
isError: !!isError
|
|
1595
1705
|
});
|
|
1596
1706
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
1597
|
-
res.end(JSON.stringify({
|
|
1707
|
+
res.end(JSON.stringify({
|
|
1708
|
+
result: output,
|
|
1709
|
+
...isError != null ? { isError } : {},
|
|
1710
|
+
...toolResult != null ? { toolResult } : {}
|
|
1711
|
+
}));
|
|
1598
1712
|
} catch (error) {
|
|
1599
1713
|
res.writeHead(500, { "Content-Type": "application/json" });
|
|
1600
1714
|
res.end(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
|
|
@@ -1720,6 +1834,8 @@ const runtime = {
|
|
|
1720
1834
|
toolNames: /* @__PURE__ */ new Set(),
|
|
1721
1835
|
mcpToolPrefixes: /* @__PURE__ */ new Set()
|
|
1722
1836
|
};
|
|
1837
|
+
let currentHostInputTracker;
|
|
1838
|
+
let currentTurn;
|
|
1723
1839
|
prependOpenCodeBinToPath({
|
|
1724
1840
|
bootstrapDir,
|
|
1725
1841
|
env
|
|
@@ -1731,14 +1847,11 @@ await runBridge({
|
|
|
1731
1847
|
onStop: () => runtime.sessionId ? { openCodeSessionId: runtime.sessionId } : {}
|
|
1732
1848
|
});
|
|
1733
1849
|
async function runTurn(start, turn) {
|
|
1850
|
+
currentTurn = turn;
|
|
1734
1851
|
const emit = (msg) => turn.emit(msg);
|
|
1735
1852
|
let totalUsage;
|
|
1736
1853
|
try {
|
|
1737
|
-
await ensureRuntime({
|
|
1738
|
-
start,
|
|
1739
|
-
turn,
|
|
1740
|
-
emit
|
|
1741
|
-
});
|
|
1854
|
+
await ensureRuntime({ start });
|
|
1742
1855
|
const client = runtime.client;
|
|
1743
1856
|
if (start.skillsChanged) await client.instance.dispose({ directory: workdir });
|
|
1744
1857
|
const sessionId = await ensureSession({
|
|
@@ -1794,17 +1907,13 @@ async function switchSessionModel({ client, sessionId, start }) {
|
|
|
1794
1907
|
});
|
|
1795
1908
|
if (response.error != null) throw response.error;
|
|
1796
1909
|
}
|
|
1797
|
-
async function ensureRuntime({ start
|
|
1910
|
+
async function ensureRuntime({ start }) {
|
|
1798
1911
|
if (runtime.client && isDeepStrictEqual(runtime.openCodeConfig, start.openCodeConfig)) return;
|
|
1799
1912
|
closeRuntime();
|
|
1800
1913
|
try {
|
|
1801
1914
|
if (start.tools && start.tools.length > 0) {
|
|
1802
1915
|
runtime.toolNames = new Set(start.tools.map((tool) => tool.name));
|
|
1803
|
-
runtime.relay = await startToolRelay({
|
|
1804
|
-
tools: start.tools,
|
|
1805
|
-
emit,
|
|
1806
|
-
requestToolResult: turn.requestToolResult
|
|
1807
|
-
});
|
|
1916
|
+
runtime.relay = await startToolRelay({ tools: start.tools });
|
|
1808
1917
|
}
|
|
1809
1918
|
const toolSchemasPath = runtime.relay && start.tools ? writeHostToolSchemas(start.tools) : void 0;
|
|
1810
1919
|
const serverAuthHeaders = configureOpenCodeServerAuth({ env });
|
|
@@ -2094,6 +2203,8 @@ async function runPrompt({ client, sessionId, start, turn, emit }) {
|
|
|
2094
2203
|
let terminalError;
|
|
2095
2204
|
let submittingUserMessage = false;
|
|
2096
2205
|
const state = createTranslationState();
|
|
2206
|
+
const hostInputTracker = createOpenCodeHostInputTracker({ emit });
|
|
2207
|
+
currentHostInputTracker = hostInputTracker;
|
|
2097
2208
|
const initialSessionTokens = await readSessionTokens({
|
|
2098
2209
|
client,
|
|
2099
2210
|
sessionId
|
|
@@ -2112,9 +2223,11 @@ async function runPrompt({ client, sessionId, start, turn, emit }) {
|
|
|
2112
2223
|
builtinToolFiltering: start.builtinToolFiltering,
|
|
2113
2224
|
turn,
|
|
2114
2225
|
state,
|
|
2226
|
+
hostInputTracker,
|
|
2115
2227
|
emit: (msg) => {
|
|
2116
2228
|
if (msg.type === "text-delta" || msg.type === "reasoning-delta") sawContent = true;
|
|
2117
2229
|
if (msg.type === "finish-step") {
|
|
2230
|
+
hostInputTracker.observeFinishStep();
|
|
2118
2231
|
sawFinishStep = true;
|
|
2119
2232
|
stepUsage = addUsage({
|
|
2120
2233
|
left: stepUsage,
|
|
@@ -2339,7 +2452,7 @@ async function runCompaction({ client, sessionId, start, turn, emit }) {
|
|
|
2339
2452
|
harnessMetadata: { opencode: { missingSummary: true } }
|
|
2340
2453
|
});
|
|
2341
2454
|
}
|
|
2342
|
-
async function consumeEvents({ client, sessionId, permissionMode, builtinToolFiltering, turn, state, emit, signal, onSubscribed, onEvent }) {
|
|
2455
|
+
async function consumeEvents({ client, sessionId, permissionMode, builtinToolFiltering, turn, state, hostInputTracker, emit, signal, onSubscribed, onEvent }) {
|
|
2343
2456
|
const stream = await subscribeLegacyEvents({
|
|
2344
2457
|
client,
|
|
2345
2458
|
signal
|
|
@@ -2363,6 +2476,7 @@ async function consumeEvents({ client, sessionId, permissionMode, builtinToolFil
|
|
|
2363
2476
|
state
|
|
2364
2477
|
}),
|
|
2365
2478
|
onSubagentSession: registerSubagentSession(sessionId),
|
|
2479
|
+
onToolCallStatus: (input) => hostInputTracker?.observeToolCall(input),
|
|
2366
2480
|
isMcpToolName: (toolName) => [...runtime.mcpToolPrefixes].some((prefix) => toolName.startsWith(prefix)),
|
|
2367
2481
|
stripWorkDir,
|
|
2368
2482
|
formatError
|
|
@@ -2426,42 +2540,91 @@ async function consumeEvents({ client, sessionId, permissionMode, builtinToolFil
|
|
|
2426
2540
|
}
|
|
2427
2541
|
processEvent(event);
|
|
2428
2542
|
};
|
|
2429
|
-
|
|
2430
|
-
|
|
2543
|
+
let rejectHandlerFailure;
|
|
2544
|
+
const handlerFailure = new Promise((_, reject) => {
|
|
2545
|
+
rejectHandlerFailure = reject;
|
|
2546
|
+
});
|
|
2547
|
+
handlerFailure.catch(() => {});
|
|
2548
|
+
const runHandler = (handler) => {
|
|
2549
|
+
handler.catch((error) => rejectHandlerFailure(error));
|
|
2550
|
+
};
|
|
2551
|
+
const hostInputWait = ({ toolCallId, isDescendant, settlesToolCall }) => {
|
|
2552
|
+
if (hostInputTracker == null) return void 0;
|
|
2553
|
+
if (isDescendant) {
|
|
2554
|
+
const id = toolCallId;
|
|
2555
|
+
return {
|
|
2556
|
+
markWaiting: () => hostInputTracker.markGroupWaiting({
|
|
2557
|
+
group: "subagent",
|
|
2558
|
+
id
|
|
2559
|
+
}),
|
|
2560
|
+
clearWaiting: () => hostInputTracker.clearGroupWaiting({
|
|
2561
|
+
group: "subagent",
|
|
2562
|
+
id
|
|
2563
|
+
})
|
|
2564
|
+
};
|
|
2565
|
+
}
|
|
2566
|
+
return {
|
|
2567
|
+
markWaiting: () => hostInputTracker.markWaiting({ toolCallId }),
|
|
2568
|
+
clearWaiting: () => hostInputTracker.clearWaiting({
|
|
2569
|
+
toolCallId,
|
|
2570
|
+
settled: settlesToolCall
|
|
2571
|
+
})
|
|
2572
|
+
};
|
|
2573
|
+
};
|
|
2574
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
2575
|
+
try {
|
|
2576
|
+
for (;;) {
|
|
2577
|
+
const next = await Promise.race([iterator.next(), handlerFailure]);
|
|
2578
|
+
if (next.done) break;
|
|
2579
|
+
if (consumeEvent(next.value)) break;
|
|
2580
|
+
}
|
|
2581
|
+
} finally {
|
|
2582
|
+
iterator.return?.();
|
|
2583
|
+
}
|
|
2584
|
+
function consumeEvent(rawEvent) {
|
|
2585
|
+
if (signal.aborted || turn.abortSignal.aborted) return true;
|
|
2431
2586
|
const event = unwrapOpenCodeEvent(rawEvent);
|
|
2432
2587
|
const eventSessionId = event ? getOpenCodeEventSessionId(event) : void 0;
|
|
2433
|
-
if (!event)
|
|
2588
|
+
if (!event) return false;
|
|
2434
2589
|
const scopedSessionId = !eventSessionId || eventSessionId === sessionId ? sessionId : taskSessionIds.has(eventSessionId) ? eventSessionId : void 0;
|
|
2435
|
-
if (!scopedSessionId)
|
|
2590
|
+
if (!scopedSessionId) return false;
|
|
2436
2591
|
const isDescendant = scopedSessionId !== sessionId;
|
|
2437
|
-
|
|
2592
|
+
const waitFor = ({ toolCallId, settlesToolCall }) => hostInputWait({
|
|
2593
|
+
toolCallId: isDescendant ? `${scopedSessionId}:${toolCallId}` : toolCallId,
|
|
2594
|
+
isDescendant,
|
|
2595
|
+
settlesToolCall
|
|
2596
|
+
});
|
|
2597
|
+
if (event.type === "question.asked") runHandler(handleQuestion({
|
|
2438
2598
|
client,
|
|
2439
2599
|
turn,
|
|
2440
2600
|
emit,
|
|
2441
|
-
event
|
|
2442
|
-
|
|
2443
|
-
|
|
2601
|
+
event,
|
|
2602
|
+
waitFor
|
|
2603
|
+
}));
|
|
2604
|
+
else if (event.type === "permission.v2.asked") runHandler(handlePermissionV2({
|
|
2444
2605
|
client,
|
|
2445
2606
|
sessionId: scopedSessionId,
|
|
2446
2607
|
permissionMode,
|
|
2447
2608
|
builtinToolFiltering,
|
|
2448
2609
|
turn,
|
|
2449
2610
|
emit,
|
|
2450
|
-
event
|
|
2451
|
-
|
|
2452
|
-
|
|
2611
|
+
event,
|
|
2612
|
+
waitFor
|
|
2613
|
+
}));
|
|
2614
|
+
else if (event.type === "permission.asked") runHandler(handlePermission({
|
|
2453
2615
|
client,
|
|
2454
2616
|
sessionId: scopedSessionId,
|
|
2455
2617
|
permissionMode,
|
|
2456
2618
|
builtinToolFiltering,
|
|
2457
2619
|
turn,
|
|
2458
2620
|
emit,
|
|
2459
|
-
event
|
|
2460
|
-
|
|
2621
|
+
event,
|
|
2622
|
+
waitFor
|
|
2623
|
+
}));
|
|
2461
2624
|
else if (isDescendant) processDescendantEvent(scopedSessionId, event);
|
|
2462
2625
|
else emitStreamEvent(event);
|
|
2463
|
-
if (isDescendant)
|
|
2464
|
-
|
|
2626
|
+
if (isDescendant) return false;
|
|
2627
|
+
return onEvent?.(event) === true;
|
|
2465
2628
|
}
|
|
2466
2629
|
}
|
|
2467
2630
|
function getSubagentStepId(event) {
|
|
@@ -2473,7 +2636,7 @@ function getSubagentStepId(event) {
|
|
|
2473
2636
|
if (event?.type !== "session.next.step.ended") return void 0;
|
|
2474
2637
|
return stringValue(event.properties?.stepID) ?? event.id;
|
|
2475
2638
|
}
|
|
2476
|
-
async function handleQuestion({ client, turn, emit, event }) {
|
|
2639
|
+
async function handleQuestion({ client, turn, emit, event, waitFor }) {
|
|
2477
2640
|
const nativeRequest = event.properties;
|
|
2478
2641
|
if (nativeRequest == null || typeof nativeRequest.id !== "string" || typeof nativeRequest.sessionID !== "string" || !Array.isArray(nativeRequest.questions)) return;
|
|
2479
2642
|
const toolCallId = nativeRequest.tool?.callID ?? nativeRequest.id;
|
|
@@ -2487,6 +2650,11 @@ async function handleQuestion({ client, turn, emit, event }) {
|
|
|
2487
2650
|
providerMetadata: { opencode: { nativeRequest } }
|
|
2488
2651
|
});
|
|
2489
2652
|
const questionKey = openCodeQuestionKey(nativeRequest);
|
|
2653
|
+
const wait = waitFor?.({
|
|
2654
|
+
toolCallId,
|
|
2655
|
+
settlesToolCall: true
|
|
2656
|
+
});
|
|
2657
|
+
wait?.markWaiting();
|
|
2490
2658
|
const nativeResponse = toOpenCodeQuestionResponse({
|
|
2491
2659
|
nativeRequest,
|
|
2492
2660
|
output: (await turn.requestToolResult({
|
|
@@ -2495,7 +2663,7 @@ async function handleQuestion({ client, turn, emit, event }) {
|
|
|
2495
2663
|
const continuedRequest = candidate.toolResult?.providerOptions?.opencode?.nativeRequest;
|
|
2496
2664
|
return continuedRequest != null && openCodeQuestionKey(continuedRequest) === questionKey;
|
|
2497
2665
|
}
|
|
2498
|
-
})).output
|
|
2666
|
+
}).finally(() => wait?.clearWaiting())).output
|
|
2499
2667
|
});
|
|
2500
2668
|
const response = nativeResponse.action === "reject" ? await client.question.reject({
|
|
2501
2669
|
requestID: nativeRequest.id,
|
|
@@ -2510,7 +2678,7 @@ async function handleQuestion({ client, turn, emit, event }) {
|
|
|
2510
2678
|
function sanitizeMcpToolName(value) {
|
|
2511
2679
|
return value.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
2512
2680
|
}
|
|
2513
|
-
async function handlePermissionV2({ client, sessionId, permissionMode, builtinToolFiltering, turn, emit, event }) {
|
|
2681
|
+
async function handlePermissionV2({ client, sessionId, permissionMode, builtinToolFiltering, turn, emit, event, waitFor }) {
|
|
2514
2682
|
const props = event.properties ?? {};
|
|
2515
2683
|
const requestID = String(props.id ?? "");
|
|
2516
2684
|
if (!requestID) return;
|
|
@@ -2522,7 +2690,8 @@ async function handlePermissionV2({ client, sessionId, permissionMode, builtinTo
|
|
|
2522
2690
|
permissionMode,
|
|
2523
2691
|
builtinToolFiltering,
|
|
2524
2692
|
turn,
|
|
2525
|
-
emit
|
|
2693
|
+
emit,
|
|
2694
|
+
waitFor
|
|
2526
2695
|
});
|
|
2527
2696
|
await client.v2.session.permission.reply({
|
|
2528
2697
|
sessionID: sessionId,
|
|
@@ -2531,7 +2700,7 @@ async function handlePermissionV2({ client, sessionId, permissionMode, builtinTo
|
|
|
2531
2700
|
...reply.message ? { message: reply.message } : {}
|
|
2532
2701
|
});
|
|
2533
2702
|
}
|
|
2534
|
-
async function handlePermission({ client, sessionId, permissionMode, builtinToolFiltering, turn, emit, event }) {
|
|
2703
|
+
async function handlePermission({ client, sessionId, permissionMode, builtinToolFiltering, turn, emit, event, waitFor }) {
|
|
2535
2704
|
const props = event.properties ?? {};
|
|
2536
2705
|
const requestID = String(props.id ?? "");
|
|
2537
2706
|
if (!requestID) return;
|
|
@@ -2544,7 +2713,8 @@ async function handlePermission({ client, sessionId, permissionMode, builtinTool
|
|
|
2544
2713
|
permissionMode,
|
|
2545
2714
|
builtinToolFiltering,
|
|
2546
2715
|
turn,
|
|
2547
|
-
emit
|
|
2716
|
+
emit,
|
|
2717
|
+
waitFor
|
|
2548
2718
|
});
|
|
2549
2719
|
await client.permission.reply({
|
|
2550
2720
|
requestID,
|
|
@@ -2553,7 +2723,15 @@ async function handlePermission({ client, sessionId, permissionMode, builtinTool
|
|
|
2553
2723
|
...reply.message ? { message: reply.message } : {}
|
|
2554
2724
|
});
|
|
2555
2725
|
}
|
|
2556
|
-
async function selectPermissionReply({ action, resources, requestID, toolCallId, permissionMode, builtinToolFiltering, turn, emit }) {
|
|
2726
|
+
async function selectPermissionReply({ action, resources, requestID, toolCallId, permissionMode, builtinToolFiltering, turn, emit, waitFor }) {
|
|
2727
|
+
const requestApproval = async () => {
|
|
2728
|
+
const wait = waitFor?.({
|
|
2729
|
+
toolCallId,
|
|
2730
|
+
settlesToolCall: false
|
|
2731
|
+
});
|
|
2732
|
+
wait?.markWaiting();
|
|
2733
|
+
return turn.requestToolApproval(requestID).finally(() => wait?.clearWaiting());
|
|
2734
|
+
};
|
|
2557
2735
|
const toolName = toPermissionToolName(action);
|
|
2558
2736
|
if (isBuiltinToolInactive({
|
|
2559
2737
|
toolName,
|
|
@@ -2564,7 +2742,7 @@ async function selectPermissionReply({ action, resources, requestID, toolCallId,
|
|
|
2564
2742
|
approvalId: requestID,
|
|
2565
2743
|
toolCallId
|
|
2566
2744
|
});
|
|
2567
|
-
const decision = await
|
|
2745
|
+
const decision = await requestApproval();
|
|
2568
2746
|
return decision.approved ? { reply: "once" } : {
|
|
2569
2747
|
reply: "reject",
|
|
2570
2748
|
...decision.reason ? { message: decision.reason } : {}
|
|
@@ -2582,7 +2760,7 @@ async function selectPermissionReply({ action, resources, requestID, toolCallId,
|
|
|
2582
2760
|
approvalId: requestID,
|
|
2583
2761
|
toolCallId
|
|
2584
2762
|
});
|
|
2585
|
-
const decision = await
|
|
2763
|
+
const decision = await requestApproval();
|
|
2586
2764
|
return decision.approved ? { reply: "once" } : {
|
|
2587
2765
|
reply: "reject",
|
|
2588
2766
|
...decision.reason ? { message: decision.reason } : {}
|
|
@@ -2757,11 +2935,26 @@ function emitAssistantContentPart(part, emit) {
|
|
|
2757
2935
|
id
|
|
2758
2936
|
});
|
|
2759
2937
|
}
|
|
2760
|
-
async function startToolRelay({ tools
|
|
2938
|
+
async function startToolRelay({ tools }) {
|
|
2761
2939
|
return startAuthorizedToolRelay({
|
|
2762
2940
|
tools,
|
|
2763
|
-
emit
|
|
2764
|
-
|
|
2941
|
+
emit: (message) => {
|
|
2942
|
+
currentTurn?.emit(message);
|
|
2943
|
+
},
|
|
2944
|
+
requestToolResult: (toolCallId) => {
|
|
2945
|
+
if (!currentTurn) throw new Error("No active turn in OpenCode bridge");
|
|
2946
|
+
return currentTurn.requestToolResult(toolCallId);
|
|
2947
|
+
},
|
|
2948
|
+
onWaitingChange: ({ requestId, waiting }) => {
|
|
2949
|
+
if (waiting) currentHostInputTracker?.markGroupWaiting({
|
|
2950
|
+
group: "host-tool",
|
|
2951
|
+
id: requestId
|
|
2952
|
+
});
|
|
2953
|
+
else currentHostInputTracker?.clearGroupWaiting({
|
|
2954
|
+
group: "host-tool",
|
|
2955
|
+
id: requestId
|
|
2956
|
+
});
|
|
2957
|
+
}
|
|
2765
2958
|
});
|
|
2766
2959
|
}
|
|
2767
2960
|
function createDeferred() {
|