@ai-sdk/harness-opencode 1.0.170 → 1.0.173

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/dist/index.js CHANGED
@@ -729,7 +729,7 @@ const startMessageSchema = harnessV1BridgeStartBaseSchema.omit({ builtinToolFilt
729
729
  z.discriminatedUnion("type", [startMessageSchema, ...harnessV1BridgeInboundCommandSchemas]);
730
730
  //#endregion
731
731
  //#region src/version.ts
732
- const VERSION = "1.0.170";
732
+ const VERSION = "1.0.173";
733
733
  //#endregion
734
734
  //#region src/opencode-harness.ts
735
735
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/harness-opencode",
3
- "version": "1.0.170",
3
+ "version": "1.0.173",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -26,9 +26,9 @@
26
26
  }
27
27
  },
28
28
  "dependencies": {
29
- "@ai-sdk/harness": "1.0.168",
30
- "@ai-sdk/provider": "4.0.34",
31
- "@ai-sdk/provider-utils": "5.0.71",
29
+ "@ai-sdk/harness": "1.0.170",
30
+ "@ai-sdk/provider": "4.0.35",
31
+ "@ai-sdk/provider-utils": "5.0.72",
32
32
  "ws": "8.21.0"
33
33
  },
34
34
  "peerDependencies": {
@@ -62,6 +62,10 @@ import {
62
62
  import { toOpenCodePromptParts } from './opencode-prompt';
63
63
 
64
64
  type Emit = (msg: Record<string, unknown>) => void;
65
+ /* Resolves once the approval request has been sent to the host. */
66
+ type EmitApprovalRequest = (
67
+ msg: Record<string, unknown>,
68
+ ) => void | Promise<void>;
65
69
 
66
70
  type OpenCodeClient = ReturnType<typeof createOpencodeClient>;
67
71
  type OpenCodeServer = Awaited<ReturnType<typeof createOpencodeServer>>;
@@ -148,6 +152,13 @@ const TOOL_KIND: Readonly<Record<string, 'readonly' | 'edit' | 'bash'>> = {
148
152
  skill: 'edit',
149
153
  todowrite: 'edit',
150
154
  };
155
+ /*
156
+ * How long an approval request waits for its tool call to reach the host.
157
+ * OpenCode reports the call right after asking, so this only bounds a missing
158
+ * report; after it the request is sent as before.
159
+ */
160
+ const APPROVAL_TOOL_CALL_WAIT_MS = 5_000;
161
+
151
162
  const HARNESS_CLIENT_APP = procEnv.AI_SDK_HARNESS_CLIENT_APP;
152
163
 
153
164
  const args = parseArgs(argv.slice(2));
@@ -1090,9 +1101,39 @@ async function consumeEvents({
1090
1101
  taskSessionIds.add(subagentSessionId);
1091
1102
  }
1092
1103
  };
1104
+ /*
1105
+ * OpenCode asks for permission while a tool call is still pending and
1106
+ * reports the call as running only afterwards, but the host must receive
1107
+ * the call before its approval request. An approval request for a call that
1108
+ * has not been sent yet waits for it, at most APPROVAL_TOOL_CALL_WAIT_MS.
1109
+ * The bridge only starts waiting for the host's answer once the request is
1110
+ * sent, so the host is not told the step is paused before it has the call.
1111
+ */
1112
+ const sentToolCallIds = new Set<string>();
1113
+ const approvalsAwaitingToolCall = new Map<string, () => void>();
1114
+ const emitToHost: Emit = message => {
1115
+ emit(message);
1116
+ if (message.type !== 'tool-call') return;
1117
+ const toolCallId = String(message.toolCallId);
1118
+ sentToolCallIds.add(toolCallId);
1119
+ approvalsAwaitingToolCall.get(toolCallId)?.();
1120
+ };
1121
+ const emitApprovalRequest: EmitApprovalRequest = async message => {
1122
+ const toolCallId = String(message.toolCallId);
1123
+ if (!sentToolCallIds.has(toolCallId)) {
1124
+ let timeout: ReturnType<typeof setTimeout> | undefined;
1125
+ await new Promise<void>(resolve => {
1126
+ timeout = setTimeout(resolve, APPROVAL_TOOL_CALL_WAIT_MS);
1127
+ approvalsAwaitingToolCall.set(toolCallId, resolve);
1128
+ });
1129
+ clearTimeout(timeout);
1130
+ approvalsAwaitingToolCall.delete(toolCallId);
1131
+ }
1132
+ emit(message);
1133
+ };
1093
1134
  const emitStreamEvent = createEmitStreamEvent({
1094
1135
  state,
1095
- emit,
1136
+ emit: emitToHost,
1096
1137
  emitWarning: turn.emitWarning,
1097
1138
  emitError: turn.emitError,
1098
1139
  toWireToolName,
@@ -1222,6 +1263,7 @@ async function consumeEvents({
1222
1263
  if (consumeEvent(next.value)) break;
1223
1264
  }
1224
1265
  } finally {
1266
+ for (const release of approvalsAwaitingToolCall.values()) release();
1225
1267
  void iterator.return?.();
1226
1268
  }
1227
1269
 
@@ -1270,7 +1312,7 @@ async function consumeEvents({
1270
1312
  permissionMode,
1271
1313
  inactiveBuiltinToolNames,
1272
1314
  turn,
1273
- emit,
1315
+ emit: isDescendant ? emit : emitApprovalRequest,
1274
1316
  event,
1275
1317
  waitFor,
1276
1318
  }),
@@ -1283,7 +1325,7 @@ async function consumeEvents({
1283
1325
  permissionMode,
1284
1326
  inactiveBuiltinToolNames,
1285
1327
  turn,
1286
- emit,
1328
+ emit: isDescendant ? emit : emitApprovalRequest,
1287
1329
  event,
1288
1330
  waitFor,
1289
1331
  }),
@@ -1416,7 +1458,7 @@ async function handlePermissionV2({
1416
1458
  permissionMode: StartMessage['permissionMode'];
1417
1459
  inactiveBuiltinToolNames: StartMessage['inactiveBuiltinToolNames'];
1418
1460
  turn: BridgeTurn;
1419
- emit: Emit;
1461
+ emit: EmitApprovalRequest;
1420
1462
  event: OpenCodeEvent;
1421
1463
  waitFor?: WaitForHostInput;
1422
1464
  }): Promise<void> {
@@ -1459,7 +1501,7 @@ async function handlePermission({
1459
1501
  permissionMode: StartMessage['permissionMode'];
1460
1502
  inactiveBuiltinToolNames: StartMessage['inactiveBuiltinToolNames'];
1461
1503
  turn: BridgeTurn;
1462
- emit: Emit;
1504
+ emit: EmitApprovalRequest;
1463
1505
  event: OpenCodeEvent;
1464
1506
  waitFor?: WaitForHostInput;
1465
1507
  }): Promise<void> {
@@ -1505,7 +1547,7 @@ async function selectPermissionReply({
1505
1547
  permissionMode: StartMessage['permissionMode'];
1506
1548
  inactiveBuiltinToolNames: StartMessage['inactiveBuiltinToolNames'];
1507
1549
  turn: BridgeTurn;
1508
- emit: Emit;
1550
+ emit: EmitApprovalRequest;
1509
1551
  waitFor?: WaitForHostInput;
1510
1552
  }): Promise<{ reply: 'once' | 'always' | 'reject'; message?: string }> {
1511
1553
  const requestApproval = async () => {
@@ -1517,7 +1559,7 @@ async function selectPermissionReply({
1517
1559
  };
1518
1560
  const toolName = toPermissionToolName(action);
1519
1561
  if (isBuiltinToolInactive({ toolName, inactiveBuiltinToolNames })) {
1520
- emit({
1562
+ await emit({
1521
1563
  type: 'tool-approval-request',
1522
1564
  approvalId: requestID,
1523
1565
  toolCallId,
@@ -1543,7 +1585,7 @@ async function selectPermissionReply({
1543
1585
  : kind === 'readonly';
1544
1586
  if (allowed) return { reply: 'always' };
1545
1587
 
1546
- emit({
1588
+ await emit({
1547
1589
  type: 'tool-approval-request',
1548
1590
  approvalId: requestID,
1549
1591
  toolCallId,