@ai-sdk/harness-opencode 1.0.152 → 1.0.154
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 +262 -72
- package/dist/bridge/index.mjs.map +1 -1
- package/dist/bridge/package.json +1 -0
- package/dist/bridge/pnpm-lock.yaml +117 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +53 -8
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/src/bridge/create-emit-stream-event.ts +37 -0
- package/src/bridge/host-tool-mcp.ts +29 -1
- package/src/bridge/index.ts +247 -108
- package/src/bridge/opencode-host-input-tracker.ts +106 -0
- package/src/bridge/opencode-path.ts +3 -1
- package/src/bridge/package.json +1 -0
- package/src/bridge/pnpm-lock.yaml +117 -0
- package/src/bridge/tool-relay.ts +15 -5
- package/src/opencode-bridge-protocol.ts +33 -11
- package/src/opencode-harness.ts +49 -3
package/dist/bridge/index.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import { WebSocketServer } from "ws";
|
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { isDeepStrictEqual } from "node:util";
|
|
8
8
|
import { createOpencodeClient, createOpencodeServer } from "@opencode-ai/sdk/v2";
|
|
9
|
+
import { rgPath } from "@vscode/ripgrep";
|
|
9
10
|
import { createServer } from "node:http";
|
|
10
11
|
//#region ../harness/dist/bridge/index.js
|
|
11
12
|
const DEBUG_LEVEL_WEIGHT = {
|
|
@@ -578,6 +579,73 @@ function serialiseError(err) {
|
|
|
578
579
|
return err;
|
|
579
580
|
}
|
|
580
581
|
//#endregion
|
|
582
|
+
//#region src/bridge/opencode-host-input-tracker.ts
|
|
583
|
+
function createOpenCodeHostInputTracker({ emit }) {
|
|
584
|
+
const unsettledToolCalls = /* @__PURE__ */ new Map();
|
|
585
|
+
const waitingToolCallIds = /* @__PURE__ */ new Set();
|
|
586
|
+
const groupWaits = /* @__PURE__ */ new Map([["host-tool", /* @__PURE__ */ new Set()], ["subagent", /* @__PURE__ */ new Set()]]);
|
|
587
|
+
const coveredIds = /* @__PURE__ */ new Set();
|
|
588
|
+
const emitEarlyFinishStepIfReady = () => {
|
|
589
|
+
if (unsettledToolCalls.size === 0) return;
|
|
590
|
+
const notWaitingByGroup = /* @__PURE__ */ new Map();
|
|
591
|
+
for (const [callID, group] of unsettledToolCalls) {
|
|
592
|
+
if (waitingToolCallIds.has(callID)) continue;
|
|
593
|
+
if (group == null) return;
|
|
594
|
+
notWaitingByGroup.set(group, (notWaitingByGroup.get(group) ?? 0) + 1);
|
|
595
|
+
}
|
|
596
|
+
for (const [group, count] of notWaitingByGroup) if (count > groupWaits.get(group).size) return;
|
|
597
|
+
const uncovered = [...waitingToolCallIds, ...[...groupWaits.values()].flatMap((ids) => [...ids])].filter((id) => !coveredIds.has(id));
|
|
598
|
+
if (uncovered.length === 0) return;
|
|
599
|
+
for (const id of uncovered) coveredIds.add(id);
|
|
600
|
+
emit({
|
|
601
|
+
type: "finish-step",
|
|
602
|
+
finishReason: {
|
|
603
|
+
unified: "tool-calls",
|
|
604
|
+
raw: "host-input"
|
|
605
|
+
},
|
|
606
|
+
usage: {
|
|
607
|
+
inputTokens: {
|
|
608
|
+
total: 0,
|
|
609
|
+
noCache: 0,
|
|
610
|
+
cacheRead: 0,
|
|
611
|
+
cacheWrite: 0
|
|
612
|
+
},
|
|
613
|
+
outputTokens: {
|
|
614
|
+
total: 0,
|
|
615
|
+
text: 0
|
|
616
|
+
}
|
|
617
|
+
},
|
|
618
|
+
harnessMetadata: { opencode: { inferredStep: true } }
|
|
619
|
+
});
|
|
620
|
+
};
|
|
621
|
+
return {
|
|
622
|
+
observeToolCall({ callID, status, group }) {
|
|
623
|
+
if (status === "started") unsettledToolCalls.set(callID, group);
|
|
624
|
+
else unsettledToolCalls.delete(callID);
|
|
625
|
+
emitEarlyFinishStepIfReady();
|
|
626
|
+
},
|
|
627
|
+
markWaiting({ toolCallId }) {
|
|
628
|
+
if (!unsettledToolCalls.has(toolCallId)) unsettledToolCalls.set(toolCallId, void 0);
|
|
629
|
+
waitingToolCallIds.add(toolCallId);
|
|
630
|
+
emitEarlyFinishStepIfReady();
|
|
631
|
+
},
|
|
632
|
+
clearWaiting({ toolCallId, settled = false }) {
|
|
633
|
+
waitingToolCallIds.delete(toolCallId);
|
|
634
|
+
if (settled) unsettledToolCalls.delete(toolCallId);
|
|
635
|
+
},
|
|
636
|
+
markGroupWaiting({ group, id }) {
|
|
637
|
+
groupWaits.get(group).add(id);
|
|
638
|
+
emitEarlyFinishStepIfReady();
|
|
639
|
+
},
|
|
640
|
+
clearGroupWaiting({ group, id }) {
|
|
641
|
+
groupWaits.get(group).delete(id);
|
|
642
|
+
},
|
|
643
|
+
observeFinishStep() {
|
|
644
|
+
coveredIds.clear();
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
//#endregion
|
|
581
649
|
//#region src/bridge/opencode-types.ts
|
|
582
650
|
function asOpenCodeObject(value) {
|
|
583
651
|
return value != null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
@@ -965,7 +1033,7 @@ function translateLegacyStepFinishPart(value) {
|
|
|
965
1033
|
}
|
|
966
1034
|
//#endregion
|
|
967
1035
|
//#region src/bridge/create-emit-stream-event.ts
|
|
968
|
-
function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireToolName, nativeNameField, getHostToolName, authorizeHostToolCall, onSubagentSession, isMcpToolName, stripWorkDir, formatError }) {
|
|
1036
|
+
function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireToolName, nativeNameField, getHostToolName, authorizeHostToolCall, onSubagentSession, onToolCallStatus, isMcpToolName, stripWorkDir, formatError }) {
|
|
969
1037
|
const compactionMessages = /* @__PURE__ */ new Set();
|
|
970
1038
|
let compactionTrigger = "auto";
|
|
971
1039
|
let compaction;
|
|
@@ -1080,6 +1148,7 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1080
1148
|
getHostToolName,
|
|
1081
1149
|
authorizeHostToolCall,
|
|
1082
1150
|
onSubagentSession,
|
|
1151
|
+
onToolCallStatus,
|
|
1083
1152
|
isMcpToolName
|
|
1084
1153
|
});
|
|
1085
1154
|
return;
|
|
@@ -1152,6 +1221,11 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1152
1221
|
const callID = String(props.callID ?? event.id);
|
|
1153
1222
|
const command = String(props.command ?? "");
|
|
1154
1223
|
state.shellCommands.set(callID, command);
|
|
1224
|
+
onToolCallStatus?.({
|
|
1225
|
+
callID,
|
|
1226
|
+
status: "started",
|
|
1227
|
+
group: void 0
|
|
1228
|
+
});
|
|
1155
1229
|
emit({
|
|
1156
1230
|
type: "tool-call",
|
|
1157
1231
|
toolCallId: callID,
|
|
@@ -1164,6 +1238,11 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1164
1238
|
}
|
|
1165
1239
|
if (type === "session.next.shell.ended") {
|
|
1166
1240
|
const callID = String(props.callID ?? event.id);
|
|
1241
|
+
onToolCallStatus?.({
|
|
1242
|
+
callID,
|
|
1243
|
+
status: "settled",
|
|
1244
|
+
group: void 0
|
|
1245
|
+
});
|
|
1167
1246
|
emit({
|
|
1168
1247
|
type: "tool-result",
|
|
1169
1248
|
toolCallId: callID,
|
|
@@ -1194,6 +1273,14 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1194
1273
|
toolName
|
|
1195
1274
|
});
|
|
1196
1275
|
const hostToolName = getHostToolName(toolName, props.tool);
|
|
1276
|
+
onToolCallStatus?.({
|
|
1277
|
+
callID,
|
|
1278
|
+
status: "started",
|
|
1279
|
+
group: toolCallGroup({
|
|
1280
|
+
hostToolName,
|
|
1281
|
+
toolName
|
|
1282
|
+
})
|
|
1283
|
+
});
|
|
1197
1284
|
if (hostToolName) {
|
|
1198
1285
|
authorizeHostToolCall({
|
|
1199
1286
|
callID,
|
|
@@ -1224,6 +1311,11 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1224
1311
|
const rawToolName = cachedTool?.rawToolName ?? String(props.tool ?? "");
|
|
1225
1312
|
if (rawToolName === "StructuredOutput" || rawToolName === "question") return;
|
|
1226
1313
|
const toolName = cachedTool?.toolName ?? toWireToolName(rawToolName || "unknown");
|
|
1314
|
+
onToolCallStatus?.({
|
|
1315
|
+
callID,
|
|
1316
|
+
status: "settled",
|
|
1317
|
+
group: void 0
|
|
1318
|
+
});
|
|
1227
1319
|
if (getHostToolName(toolName, rawToolName)) return;
|
|
1228
1320
|
emit({
|
|
1229
1321
|
type: "tool-result",
|
|
@@ -1290,6 +1382,10 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitError, toWireTool
|
|
|
1290
1382
|
}
|
|
1291
1383
|
};
|
|
1292
1384
|
}
|
|
1385
|
+
function toolCallGroup({ hostToolName, toolName }) {
|
|
1386
|
+
if (hostToolName) return "host-tool";
|
|
1387
|
+
if (toolName === "agent") return "subagent";
|
|
1388
|
+
}
|
|
1293
1389
|
function closeLegacyOpenParts({ state, emit }) {
|
|
1294
1390
|
for (const id of state.legacyReasoningPartIds) {
|
|
1295
1391
|
emit({
|
|
@@ -1324,7 +1420,7 @@ function emitLegacyStepFinishPart({ part, state, emit }) {
|
|
|
1324
1420
|
emit(event);
|
|
1325
1421
|
return true;
|
|
1326
1422
|
}
|
|
1327
|
-
function emitLegacyToolPart({ part, state, emit, toWireToolName, nativeNameField, getHostToolName, authorizeHostToolCall, onSubagentSession, isMcpToolName }) {
|
|
1423
|
+
function emitLegacyToolPart({ part, state, emit, toWireToolName, nativeNameField, getHostToolName, authorizeHostToolCall, onSubagentSession, onToolCallStatus, isMcpToolName }) {
|
|
1328
1424
|
const toolPart = legacyToolPartFromValue(part);
|
|
1329
1425
|
if (!toolPart) return;
|
|
1330
1426
|
const status = toolPart.status;
|
|
@@ -1350,6 +1446,14 @@ function emitLegacyToolPart({ part, state, emit, toWireToolName, nativeNameField
|
|
|
1350
1446
|
toolName
|
|
1351
1447
|
});
|
|
1352
1448
|
const hostToolName = getHostToolName(toolName, rawToolName);
|
|
1449
|
+
onToolCallStatus?.({
|
|
1450
|
+
callID,
|
|
1451
|
+
status: status === "running" ? "started" : "settled",
|
|
1452
|
+
group: toolCallGroup({
|
|
1453
|
+
hostToolName,
|
|
1454
|
+
toolName
|
|
1455
|
+
})
|
|
1456
|
+
});
|
|
1353
1457
|
if (hostToolName) {
|
|
1354
1458
|
if (status === "running") authorizeHostToolCall({
|
|
1355
1459
|
callID,
|
|
@@ -1461,8 +1565,12 @@ function openCodeErrorFromValue(value) {
|
|
|
1461
1565
|
//#endregion
|
|
1462
1566
|
//#region src/bridge/opencode-path.ts
|
|
1463
1567
|
const fallbackPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
|
|
1464
|
-
function
|
|
1465
|
-
env.PATH = [
|
|
1568
|
+
function configureOpenCodePath({ bootstrapDir, env }) {
|
|
1569
|
+
env.PATH = [
|
|
1570
|
+
path.join(bootstrapDir, "node_modules", ".bin"),
|
|
1571
|
+
env.PATH || fallbackPath,
|
|
1572
|
+
path.dirname(rgPath)
|
|
1573
|
+
].join(path.delimiter);
|
|
1466
1574
|
}
|
|
1467
1575
|
//#endregion
|
|
1468
1576
|
//#region src/bridge/opencode-server-auth.ts
|
|
@@ -1552,7 +1660,7 @@ function normalizeJsonValue(value) {
|
|
|
1552
1660
|
}
|
|
1553
1661
|
//#endregion
|
|
1554
1662
|
//#region src/bridge/tool-relay.ts
|
|
1555
|
-
async function startAuthorizedToolRelay({ tools, emit, requestToolResult, authorizer = new ToolRelayAuthorizer() }) {
|
|
1663
|
+
async function startAuthorizedToolRelay({ tools, emit, requestToolResult, onWaitingChange, authorizer = new ToolRelayAuthorizer() }) {
|
|
1556
1664
|
const toolNames = new Set(tools.map((tool) => tool.name));
|
|
1557
1665
|
const server = createServer(async (req, res) => {
|
|
1558
1666
|
try {
|
|
@@ -1585,7 +1693,14 @@ async function startAuthorizedToolRelay({ tools, emit, requestToolResult, author
|
|
|
1585
1693
|
input: JSON.stringify(input ?? {}),
|
|
1586
1694
|
providerExecuted: false
|
|
1587
1695
|
});
|
|
1588
|
-
|
|
1696
|
+
onWaitingChange?.({
|
|
1697
|
+
requestId,
|
|
1698
|
+
waiting: true
|
|
1699
|
+
});
|
|
1700
|
+
const { output, isError, toolResult } = await requestToolResult(requestId).finally(() => onWaitingChange?.({
|
|
1701
|
+
requestId,
|
|
1702
|
+
waiting: false
|
|
1703
|
+
}));
|
|
1589
1704
|
emit({
|
|
1590
1705
|
type: "tool-result",
|
|
1591
1706
|
toolCallId: requestId,
|
|
@@ -1594,7 +1709,11 @@ async function startAuthorizedToolRelay({ tools, emit, requestToolResult, author
|
|
|
1594
1709
|
isError: !!isError
|
|
1595
1710
|
});
|
|
1596
1711
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
1597
|
-
res.end(JSON.stringify({
|
|
1712
|
+
res.end(JSON.stringify({
|
|
1713
|
+
result: output,
|
|
1714
|
+
...isError != null ? { isError } : {},
|
|
1715
|
+
...toolResult != null ? { toolResult } : {}
|
|
1716
|
+
}));
|
|
1598
1717
|
} catch (error) {
|
|
1599
1718
|
res.writeHead(500, { "Content-Type": "application/json" });
|
|
1600
1719
|
res.end(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
|
|
@@ -1684,9 +1803,10 @@ const OPENCODE_TO_WIRE = {
|
|
|
1684
1803
|
askUserQuestions: "question",
|
|
1685
1804
|
subtask: "agent"
|
|
1686
1805
|
};
|
|
1687
|
-
const
|
|
1688
|
-
|
|
1689
|
-
|
|
1806
|
+
const BUILTIN_TOOL_PERMISSION = {
|
|
1807
|
+
askUserQuestions: "question",
|
|
1808
|
+
read: "read",
|
|
1809
|
+
write: "edit",
|
|
1690
1810
|
edit: "edit",
|
|
1691
1811
|
bash: "bash",
|
|
1692
1812
|
glob: "glob",
|
|
@@ -1695,7 +1815,7 @@ const PUBLIC_TO_NATIVE = {
|
|
|
1695
1815
|
webfetch: "webfetch",
|
|
1696
1816
|
skill: "skill",
|
|
1697
1817
|
todowrite: "todowrite",
|
|
1698
|
-
agent: "
|
|
1818
|
+
agent: "task"
|
|
1699
1819
|
};
|
|
1700
1820
|
const TOOL_KIND = {
|
|
1701
1821
|
read: "readonly",
|
|
@@ -1720,7 +1840,9 @@ const runtime = {
|
|
|
1720
1840
|
toolNames: /* @__PURE__ */ new Set(),
|
|
1721
1841
|
mcpToolPrefixes: /* @__PURE__ */ new Set()
|
|
1722
1842
|
};
|
|
1723
|
-
|
|
1843
|
+
let currentHostInputTracker;
|
|
1844
|
+
let currentTurn;
|
|
1845
|
+
configureOpenCodePath({
|
|
1724
1846
|
bootstrapDir,
|
|
1725
1847
|
env
|
|
1726
1848
|
});
|
|
@@ -1731,14 +1853,11 @@ await runBridge({
|
|
|
1731
1853
|
onStop: () => runtime.sessionId ? { openCodeSessionId: runtime.sessionId } : {}
|
|
1732
1854
|
});
|
|
1733
1855
|
async function runTurn(start, turn) {
|
|
1856
|
+
currentTurn = turn;
|
|
1734
1857
|
const emit = (msg) => turn.emit(msg);
|
|
1735
1858
|
let totalUsage;
|
|
1736
1859
|
try {
|
|
1737
|
-
await ensureRuntime({
|
|
1738
|
-
start,
|
|
1739
|
-
turn,
|
|
1740
|
-
emit
|
|
1741
|
-
});
|
|
1860
|
+
await ensureRuntime({ start });
|
|
1742
1861
|
const client = runtime.client;
|
|
1743
1862
|
if (start.skillsChanged) await client.instance.dispose({ directory: workdir });
|
|
1744
1863
|
const sessionId = await ensureSession({
|
|
@@ -1794,17 +1913,13 @@ async function switchSessionModel({ client, sessionId, start }) {
|
|
|
1794
1913
|
});
|
|
1795
1914
|
if (response.error != null) throw response.error;
|
|
1796
1915
|
}
|
|
1797
|
-
async function ensureRuntime({ start
|
|
1798
|
-
if (runtime.client && isDeepStrictEqual(runtime.openCodeConfig, start.openCodeConfig)) return;
|
|
1916
|
+
async function ensureRuntime({ start }) {
|
|
1917
|
+
if (runtime.client && isDeepStrictEqual(runtime.openCodeConfig, start.openCodeConfig) && isDeepStrictEqual(runtime.inactiveBuiltinToolNames, start.inactiveBuiltinToolNames)) return;
|
|
1799
1918
|
closeRuntime();
|
|
1800
1919
|
try {
|
|
1801
1920
|
if (start.tools && start.tools.length > 0) {
|
|
1802
1921
|
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
|
-
});
|
|
1922
|
+
runtime.relay = await startToolRelay({ tools: start.tools });
|
|
1808
1923
|
}
|
|
1809
1924
|
const toolSchemasPath = runtime.relay && start.tools ? writeHostToolSchemas(start.tools) : void 0;
|
|
1810
1925
|
const serverAuthHeaders = configureOpenCodeServerAuth({ env });
|
|
@@ -1827,6 +1942,7 @@ async function ensureRuntime({ start, turn, emit }) {
|
|
|
1827
1942
|
const mcpServers = asOpenCodeObject((await runtime.client.mcp.status()).data) ?? {};
|
|
1828
1943
|
runtime.mcpToolPrefixes = new Set(Object.entries(mcpServers).filter(([serverName, status]) => serverName !== "harness-tools" && asOpenCodeObject(status)?.status === "connected").map(([serverName]) => `${sanitizeMcpToolName(serverName)}_`));
|
|
1829
1944
|
runtime.openCodeConfig = structuredClone(start.openCodeConfig);
|
|
1945
|
+
runtime.inactiveBuiltinToolNames = structuredClone(start.inactiveBuiltinToolNames);
|
|
1830
1946
|
} catch (error) {
|
|
1831
1947
|
closeRuntime();
|
|
1832
1948
|
throw error;
|
|
@@ -1839,6 +1955,7 @@ function closeRuntime() {
|
|
|
1839
1955
|
runtime.client = void 0;
|
|
1840
1956
|
runtime.relay = void 0;
|
|
1841
1957
|
runtime.openCodeConfig = void 0;
|
|
1958
|
+
runtime.inactiveBuiltinToolNames = void 0;
|
|
1842
1959
|
runtime.toolNames = /* @__PURE__ */ new Set();
|
|
1843
1960
|
runtime.mcpToolPrefixes = /* @__PURE__ */ new Set();
|
|
1844
1961
|
}
|
|
@@ -1863,13 +1980,8 @@ function buildOpenCodeConfig({ start, relayPort, toolSchemasPath }) {
|
|
|
1863
1980
|
};
|
|
1864
1981
|
if (start.model) config.model = start.model;
|
|
1865
1982
|
if (skillsDir) config.skills = { paths: [skillsDir] };
|
|
1866
|
-
const inactiveToolNames = resolveInactiveBuiltinToolNames(start);
|
|
1867
1983
|
const permission = config.permission;
|
|
1868
|
-
for (const toolName of
|
|
1869
|
-
const permissionName = toPermissionToolName(PUBLIC_TO_NATIVE[toolName] ?? toolName);
|
|
1870
|
-
if (permissionName === "ls") permission.list = "ask";
|
|
1871
|
-
else permission[permissionName] = "ask";
|
|
1872
|
-
}
|
|
1984
|
+
for (const toolName of start.inactiveBuiltinToolNames ?? []) permission[BUILTIN_TOOL_PERMISSION[toolName]] = "deny";
|
|
1873
1985
|
const provider = buildProviderConfig(start);
|
|
1874
1986
|
if (provider) config.provider = provider;
|
|
1875
1987
|
const mcp = { ...start.mcpServers };
|
|
@@ -2094,6 +2206,8 @@ async function runPrompt({ client, sessionId, start, turn, emit }) {
|
|
|
2094
2206
|
let terminalError;
|
|
2095
2207
|
let submittingUserMessage = false;
|
|
2096
2208
|
const state = createTranslationState();
|
|
2209
|
+
const hostInputTracker = createOpenCodeHostInputTracker({ emit });
|
|
2210
|
+
currentHostInputTracker = hostInputTracker;
|
|
2097
2211
|
const initialSessionTokens = await readSessionTokens({
|
|
2098
2212
|
client,
|
|
2099
2213
|
sessionId
|
|
@@ -2109,12 +2223,14 @@ async function runPrompt({ client, sessionId, start, turn, emit }) {
|
|
|
2109
2223
|
client,
|
|
2110
2224
|
sessionId,
|
|
2111
2225
|
permissionMode: start.permissionMode,
|
|
2112
|
-
|
|
2226
|
+
inactiveBuiltinToolNames: start.inactiveBuiltinToolNames,
|
|
2113
2227
|
turn,
|
|
2114
2228
|
state,
|
|
2229
|
+
hostInputTracker,
|
|
2115
2230
|
emit: (msg) => {
|
|
2116
2231
|
if (msg.type === "text-delta" || msg.type === "reasoning-delta") sawContent = true;
|
|
2117
2232
|
if (msg.type === "finish-step") {
|
|
2233
|
+
hostInputTracker.observeFinishStep();
|
|
2118
2234
|
sawFinishStep = true;
|
|
2119
2235
|
stepUsage = addUsage({
|
|
2120
2236
|
left: stepUsage,
|
|
@@ -2290,7 +2406,7 @@ async function runCompaction({ client, sessionId, start, turn, emit }) {
|
|
|
2290
2406
|
client,
|
|
2291
2407
|
sessionId,
|
|
2292
2408
|
permissionMode: start.permissionMode,
|
|
2293
|
-
|
|
2409
|
+
inactiveBuiltinToolNames: start.inactiveBuiltinToolNames,
|
|
2294
2410
|
turn,
|
|
2295
2411
|
state: createTranslationState(),
|
|
2296
2412
|
emit: (msg) => {
|
|
@@ -2339,7 +2455,7 @@ async function runCompaction({ client, sessionId, start, turn, emit }) {
|
|
|
2339
2455
|
harnessMetadata: { opencode: { missingSummary: true } }
|
|
2340
2456
|
});
|
|
2341
2457
|
}
|
|
2342
|
-
async function consumeEvents({ client, sessionId, permissionMode,
|
|
2458
|
+
async function consumeEvents({ client, sessionId, permissionMode, inactiveBuiltinToolNames, turn, state, hostInputTracker, emit, signal, onSubscribed, onEvent }) {
|
|
2343
2459
|
const stream = await subscribeLegacyEvents({
|
|
2344
2460
|
client,
|
|
2345
2461
|
signal
|
|
@@ -2363,6 +2479,7 @@ async function consumeEvents({ client, sessionId, permissionMode, builtinToolFil
|
|
|
2363
2479
|
state
|
|
2364
2480
|
}),
|
|
2365
2481
|
onSubagentSession: registerSubagentSession(sessionId),
|
|
2482
|
+
onToolCallStatus: (input) => hostInputTracker?.observeToolCall(input),
|
|
2366
2483
|
isMcpToolName: (toolName) => [...runtime.mcpToolPrefixes].some((prefix) => toolName.startsWith(prefix)),
|
|
2367
2484
|
stripWorkDir,
|
|
2368
2485
|
formatError
|
|
@@ -2426,42 +2543,91 @@ async function consumeEvents({ client, sessionId, permissionMode, builtinToolFil
|
|
|
2426
2543
|
}
|
|
2427
2544
|
processEvent(event);
|
|
2428
2545
|
};
|
|
2429
|
-
|
|
2430
|
-
|
|
2546
|
+
let rejectHandlerFailure;
|
|
2547
|
+
const handlerFailure = new Promise((_, reject) => {
|
|
2548
|
+
rejectHandlerFailure = reject;
|
|
2549
|
+
});
|
|
2550
|
+
handlerFailure.catch(() => {});
|
|
2551
|
+
const runHandler = (handler) => {
|
|
2552
|
+
handler.catch((error) => rejectHandlerFailure(error));
|
|
2553
|
+
};
|
|
2554
|
+
const hostInputWait = ({ toolCallId, isDescendant, settlesToolCall }) => {
|
|
2555
|
+
if (hostInputTracker == null) return void 0;
|
|
2556
|
+
if (isDescendant) {
|
|
2557
|
+
const id = toolCallId;
|
|
2558
|
+
return {
|
|
2559
|
+
markWaiting: () => hostInputTracker.markGroupWaiting({
|
|
2560
|
+
group: "subagent",
|
|
2561
|
+
id
|
|
2562
|
+
}),
|
|
2563
|
+
clearWaiting: () => hostInputTracker.clearGroupWaiting({
|
|
2564
|
+
group: "subagent",
|
|
2565
|
+
id
|
|
2566
|
+
})
|
|
2567
|
+
};
|
|
2568
|
+
}
|
|
2569
|
+
return {
|
|
2570
|
+
markWaiting: () => hostInputTracker.markWaiting({ toolCallId }),
|
|
2571
|
+
clearWaiting: () => hostInputTracker.clearWaiting({
|
|
2572
|
+
toolCallId,
|
|
2573
|
+
settled: settlesToolCall
|
|
2574
|
+
})
|
|
2575
|
+
};
|
|
2576
|
+
};
|
|
2577
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
2578
|
+
try {
|
|
2579
|
+
for (;;) {
|
|
2580
|
+
const next = await Promise.race([iterator.next(), handlerFailure]);
|
|
2581
|
+
if (next.done) break;
|
|
2582
|
+
if (consumeEvent(next.value)) break;
|
|
2583
|
+
}
|
|
2584
|
+
} finally {
|
|
2585
|
+
iterator.return?.();
|
|
2586
|
+
}
|
|
2587
|
+
function consumeEvent(rawEvent) {
|
|
2588
|
+
if (signal.aborted || turn.abortSignal.aborted) return true;
|
|
2431
2589
|
const event = unwrapOpenCodeEvent(rawEvent);
|
|
2432
2590
|
const eventSessionId = event ? getOpenCodeEventSessionId(event) : void 0;
|
|
2433
|
-
if (!event)
|
|
2591
|
+
if (!event) return false;
|
|
2434
2592
|
const scopedSessionId = !eventSessionId || eventSessionId === sessionId ? sessionId : taskSessionIds.has(eventSessionId) ? eventSessionId : void 0;
|
|
2435
|
-
if (!scopedSessionId)
|
|
2593
|
+
if (!scopedSessionId) return false;
|
|
2436
2594
|
const isDescendant = scopedSessionId !== sessionId;
|
|
2437
|
-
|
|
2595
|
+
const waitFor = ({ toolCallId, settlesToolCall }) => hostInputWait({
|
|
2596
|
+
toolCallId: isDescendant ? `${scopedSessionId}:${toolCallId}` : toolCallId,
|
|
2597
|
+
isDescendant,
|
|
2598
|
+
settlesToolCall
|
|
2599
|
+
});
|
|
2600
|
+
if (event.type === "question.asked") runHandler(handleQuestion({
|
|
2438
2601
|
client,
|
|
2439
2602
|
turn,
|
|
2440
2603
|
emit,
|
|
2441
|
-
event
|
|
2442
|
-
|
|
2443
|
-
|
|
2604
|
+
event,
|
|
2605
|
+
waitFor
|
|
2606
|
+
}));
|
|
2607
|
+
else if (event.type === "permission.v2.asked") runHandler(handlePermissionV2({
|
|
2444
2608
|
client,
|
|
2445
2609
|
sessionId: scopedSessionId,
|
|
2446
2610
|
permissionMode,
|
|
2447
|
-
|
|
2611
|
+
inactiveBuiltinToolNames,
|
|
2448
2612
|
turn,
|
|
2449
2613
|
emit,
|
|
2450
|
-
event
|
|
2451
|
-
|
|
2452
|
-
|
|
2614
|
+
event,
|
|
2615
|
+
waitFor
|
|
2616
|
+
}));
|
|
2617
|
+
else if (event.type === "permission.asked") runHandler(handlePermission({
|
|
2453
2618
|
client,
|
|
2454
2619
|
sessionId: scopedSessionId,
|
|
2455
2620
|
permissionMode,
|
|
2456
|
-
|
|
2621
|
+
inactiveBuiltinToolNames,
|
|
2457
2622
|
turn,
|
|
2458
2623
|
emit,
|
|
2459
|
-
event
|
|
2460
|
-
|
|
2624
|
+
event,
|
|
2625
|
+
waitFor
|
|
2626
|
+
}));
|
|
2461
2627
|
else if (isDescendant) processDescendantEvent(scopedSessionId, event);
|
|
2462
2628
|
else emitStreamEvent(event);
|
|
2463
|
-
if (isDescendant)
|
|
2464
|
-
|
|
2629
|
+
if (isDescendant) return false;
|
|
2630
|
+
return onEvent?.(event) === true;
|
|
2465
2631
|
}
|
|
2466
2632
|
}
|
|
2467
2633
|
function getSubagentStepId(event) {
|
|
@@ -2473,7 +2639,7 @@ function getSubagentStepId(event) {
|
|
|
2473
2639
|
if (event?.type !== "session.next.step.ended") return void 0;
|
|
2474
2640
|
return stringValue(event.properties?.stepID) ?? event.id;
|
|
2475
2641
|
}
|
|
2476
|
-
async function handleQuestion({ client, turn, emit, event }) {
|
|
2642
|
+
async function handleQuestion({ client, turn, emit, event, waitFor }) {
|
|
2477
2643
|
const nativeRequest = event.properties;
|
|
2478
2644
|
if (nativeRequest == null || typeof nativeRequest.id !== "string" || typeof nativeRequest.sessionID !== "string" || !Array.isArray(nativeRequest.questions)) return;
|
|
2479
2645
|
const toolCallId = nativeRequest.tool?.callID ?? nativeRequest.id;
|
|
@@ -2487,6 +2653,11 @@ async function handleQuestion({ client, turn, emit, event }) {
|
|
|
2487
2653
|
providerMetadata: { opencode: { nativeRequest } }
|
|
2488
2654
|
});
|
|
2489
2655
|
const questionKey = openCodeQuestionKey(nativeRequest);
|
|
2656
|
+
const wait = waitFor?.({
|
|
2657
|
+
toolCallId,
|
|
2658
|
+
settlesToolCall: true
|
|
2659
|
+
});
|
|
2660
|
+
wait?.markWaiting();
|
|
2490
2661
|
const nativeResponse = toOpenCodeQuestionResponse({
|
|
2491
2662
|
nativeRequest,
|
|
2492
2663
|
output: (await turn.requestToolResult({
|
|
@@ -2495,7 +2666,7 @@ async function handleQuestion({ client, turn, emit, event }) {
|
|
|
2495
2666
|
const continuedRequest = candidate.toolResult?.providerOptions?.opencode?.nativeRequest;
|
|
2496
2667
|
return continuedRequest != null && openCodeQuestionKey(continuedRequest) === questionKey;
|
|
2497
2668
|
}
|
|
2498
|
-
})).output
|
|
2669
|
+
}).finally(() => wait?.clearWaiting())).output
|
|
2499
2670
|
});
|
|
2500
2671
|
const response = nativeResponse.action === "reject" ? await client.question.reject({
|
|
2501
2672
|
requestID: nativeRequest.id,
|
|
@@ -2510,7 +2681,7 @@ async function handleQuestion({ client, turn, emit, event }) {
|
|
|
2510
2681
|
function sanitizeMcpToolName(value) {
|
|
2511
2682
|
return value.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
2512
2683
|
}
|
|
2513
|
-
async function handlePermissionV2({ client, sessionId, permissionMode,
|
|
2684
|
+
async function handlePermissionV2({ client, sessionId, permissionMode, inactiveBuiltinToolNames, turn, emit, event, waitFor }) {
|
|
2514
2685
|
const props = event.properties ?? {};
|
|
2515
2686
|
const requestID = String(props.id ?? "");
|
|
2516
2687
|
if (!requestID) return;
|
|
@@ -2520,9 +2691,10 @@ async function handlePermissionV2({ client, sessionId, permissionMode, builtinTo
|
|
|
2520
2691
|
requestID,
|
|
2521
2692
|
toolCallId: String(props.source?.callID ?? requestID),
|
|
2522
2693
|
permissionMode,
|
|
2523
|
-
|
|
2694
|
+
inactiveBuiltinToolNames,
|
|
2524
2695
|
turn,
|
|
2525
|
-
emit
|
|
2696
|
+
emit,
|
|
2697
|
+
waitFor
|
|
2526
2698
|
});
|
|
2527
2699
|
await client.v2.session.permission.reply({
|
|
2528
2700
|
sessionID: sessionId,
|
|
@@ -2531,7 +2703,7 @@ async function handlePermissionV2({ client, sessionId, permissionMode, builtinTo
|
|
|
2531
2703
|
...reply.message ? { message: reply.message } : {}
|
|
2532
2704
|
});
|
|
2533
2705
|
}
|
|
2534
|
-
async function handlePermission({ client, sessionId, permissionMode,
|
|
2706
|
+
async function handlePermission({ client, sessionId, permissionMode, inactiveBuiltinToolNames, turn, emit, event, waitFor }) {
|
|
2535
2707
|
const props = event.properties ?? {};
|
|
2536
2708
|
const requestID = String(props.id ?? "");
|
|
2537
2709
|
if (!requestID) return;
|
|
@@ -2542,9 +2714,10 @@ async function handlePermission({ client, sessionId, permissionMode, builtinTool
|
|
|
2542
2714
|
requestID,
|
|
2543
2715
|
toolCallId: String(tool?.callID ?? requestID),
|
|
2544
2716
|
permissionMode,
|
|
2545
|
-
|
|
2717
|
+
inactiveBuiltinToolNames,
|
|
2546
2718
|
turn,
|
|
2547
|
-
emit
|
|
2719
|
+
emit,
|
|
2720
|
+
waitFor
|
|
2548
2721
|
});
|
|
2549
2722
|
await client.permission.reply({
|
|
2550
2723
|
requestID,
|
|
@@ -2553,18 +2726,26 @@ async function handlePermission({ client, sessionId, permissionMode, builtinTool
|
|
|
2553
2726
|
...reply.message ? { message: reply.message } : {}
|
|
2554
2727
|
});
|
|
2555
2728
|
}
|
|
2556
|
-
async function selectPermissionReply({ action, resources, requestID, toolCallId, permissionMode,
|
|
2729
|
+
async function selectPermissionReply({ action, resources, requestID, toolCallId, permissionMode, inactiveBuiltinToolNames, turn, emit, waitFor }) {
|
|
2730
|
+
const requestApproval = async () => {
|
|
2731
|
+
const wait = waitFor?.({
|
|
2732
|
+
toolCallId,
|
|
2733
|
+
settlesToolCall: false
|
|
2734
|
+
});
|
|
2735
|
+
wait?.markWaiting();
|
|
2736
|
+
return turn.requestToolApproval(requestID).finally(() => wait?.clearWaiting());
|
|
2737
|
+
};
|
|
2557
2738
|
const toolName = toPermissionToolName(action);
|
|
2558
2739
|
if (isBuiltinToolInactive({
|
|
2559
2740
|
toolName,
|
|
2560
|
-
|
|
2741
|
+
inactiveBuiltinToolNames
|
|
2561
2742
|
})) {
|
|
2562
2743
|
emit({
|
|
2563
2744
|
type: "tool-approval-request",
|
|
2564
2745
|
approvalId: requestID,
|
|
2565
2746
|
toolCallId
|
|
2566
2747
|
});
|
|
2567
|
-
const decision = await
|
|
2748
|
+
const decision = await requestApproval();
|
|
2568
2749
|
return decision.approved ? { reply: "once" } : {
|
|
2569
2750
|
reply: "reject",
|
|
2570
2751
|
...decision.reason ? { message: decision.reason } : {}
|
|
@@ -2582,7 +2763,7 @@ async function selectPermissionReply({ action, resources, requestID, toolCallId,
|
|
|
2582
2763
|
approvalId: requestID,
|
|
2583
2764
|
toolCallId
|
|
2584
2765
|
});
|
|
2585
|
-
const decision = await
|
|
2766
|
+
const decision = await requestApproval();
|
|
2586
2767
|
return decision.approved ? { reply: "once" } : {
|
|
2587
2768
|
reply: "reject",
|
|
2588
2769
|
...decision.reason ? { message: decision.reason } : {}
|
|
@@ -2601,14 +2782,8 @@ function toPermissionToolName(action) {
|
|
|
2601
2782
|
if (normalized.includes("read")) return "read";
|
|
2602
2783
|
return toWireToolName(normalized);
|
|
2603
2784
|
}
|
|
2604
|
-
function resolveInactiveBuiltinToolNames(start) {
|
|
2605
|
-
const toolFiltering = start.builtinToolFiltering;
|
|
2606
|
-
if (toolFiltering == null) return [];
|
|
2607
|
-
return toolFiltering.mode === "allow" ? Object.keys(PUBLIC_TO_NATIVE).filter((name) => !toolFiltering.toolNames.includes(name)) : toolFiltering.toolNames;
|
|
2608
|
-
}
|
|
2609
2785
|
function isBuiltinToolInactive(input) {
|
|
2610
|
-
|
|
2611
|
-
return input.toolFiltering.mode === "allow" ? !input.toolFiltering.toolNames.includes(input.toolName) : input.toolFiltering.toolNames.includes(input.toolName);
|
|
2786
|
+
return input.inactiveBuiltinToolNames?.some((toolName) => toolName === input.toolName) ?? false;
|
|
2612
2787
|
}
|
|
2613
2788
|
function isExternalPath(resource) {
|
|
2614
2789
|
if (!path.isAbsolute(resource)) return false;
|
|
@@ -2757,11 +2932,26 @@ function emitAssistantContentPart(part, emit) {
|
|
|
2757
2932
|
id
|
|
2758
2933
|
});
|
|
2759
2934
|
}
|
|
2760
|
-
async function startToolRelay({ tools
|
|
2935
|
+
async function startToolRelay({ tools }) {
|
|
2761
2936
|
return startAuthorizedToolRelay({
|
|
2762
2937
|
tools,
|
|
2763
|
-
emit
|
|
2764
|
-
|
|
2938
|
+
emit: (message) => {
|
|
2939
|
+
currentTurn?.emit(message);
|
|
2940
|
+
},
|
|
2941
|
+
requestToolResult: (toolCallId) => {
|
|
2942
|
+
if (!currentTurn) throw new Error("No active turn in OpenCode bridge");
|
|
2943
|
+
return currentTurn.requestToolResult(toolCallId);
|
|
2944
|
+
},
|
|
2945
|
+
onWaitingChange: ({ requestId, waiting }) => {
|
|
2946
|
+
if (waiting) currentHostInputTracker?.markGroupWaiting({
|
|
2947
|
+
group: "host-tool",
|
|
2948
|
+
id: requestId
|
|
2949
|
+
});
|
|
2950
|
+
else currentHostInputTracker?.clearGroupWaiting({
|
|
2951
|
+
group: "host-tool",
|
|
2952
|
+
id: requestId
|
|
2953
|
+
});
|
|
2954
|
+
}
|
|
2765
2955
|
});
|
|
2766
2956
|
}
|
|
2767
2957
|
function createDeferred() {
|