@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.
@@ -0,0 +1,106 @@
1
+ type Emit = (message: Record<string, unknown>) => void;
2
+
3
+ /*
4
+ * Host input that cannot be attributed to an OpenCode call id. Host tool
5
+ * relay requests carry their own ids, and permission requests or questions
6
+ * raised inside a subagent belong to the parent's `agent` tool call. Calls of
7
+ * such a group are matched to the group's waits by count.
8
+ */
9
+ export type OpenCodeHostInputGroup = 'host-tool' | 'subagent';
10
+
11
+ export type OpenCodeHostInputTracker = {
12
+ observeToolCall(input: {
13
+ callID: string;
14
+ status: 'started' | 'settled';
15
+ group: OpenCodeHostInputGroup | undefined;
16
+ }): void;
17
+ markWaiting(input: { toolCallId: string }): void;
18
+ clearWaiting(input: { toolCallId: string; settled?: boolean }): void;
19
+ markGroupWaiting(input: { group: OpenCodeHostInputGroup; id: string }): void;
20
+ clearGroupWaiting(input: { group: OpenCodeHostInputGroup; id: string }): void;
21
+ observeFinishStep(): void;
22
+ };
23
+
24
+ /*
25
+ * Tracks whether OpenCode can only make progress after host input: every tool
26
+ * call that started and has not settled either waits on a permission request
27
+ * or question, or belongs to a group with enough waits. In that case an early
28
+ * `finish-step` lets the host pause the turn; the OpenCode step stays open
29
+ * until its regular `finish-step`.
30
+ */
31
+ export function createOpenCodeHostInputTracker({
32
+ emit,
33
+ }: {
34
+ emit: Emit;
35
+ }): OpenCodeHostInputTracker {
36
+ const unsettledToolCalls = new Map<
37
+ string,
38
+ OpenCodeHostInputGroup | undefined
39
+ >();
40
+ const waitingToolCallIds = new Set<string>();
41
+ const groupWaits = new Map<OpenCodeHostInputGroup, Set<string>>([
42
+ ['host-tool', new Set()],
43
+ ['subagent', new Set()],
44
+ ]);
45
+ const coveredIds = new Set<string>();
46
+
47
+ const emitEarlyFinishStepIfReady = (): void => {
48
+ if (unsettledToolCalls.size === 0) return;
49
+ const notWaitingByGroup = new Map<OpenCodeHostInputGroup, number>();
50
+ for (const [callID, group] of unsettledToolCalls) {
51
+ if (waitingToolCallIds.has(callID)) continue;
52
+ if (group == null) return;
53
+ notWaitingByGroup.set(group, (notWaitingByGroup.get(group) ?? 0) + 1);
54
+ }
55
+ for (const [group, count] of notWaitingByGroup) {
56
+ if (count > groupWaits.get(group)!.size) return;
57
+ }
58
+ const uncovered = [
59
+ ...waitingToolCallIds,
60
+ ...[...groupWaits.values()].flatMap(ids => [...ids]),
61
+ ].filter(id => !coveredIds.has(id));
62
+ if (uncovered.length === 0) return;
63
+ for (const id of uncovered) coveredIds.add(id);
64
+ emit({
65
+ type: 'finish-step',
66
+ finishReason: { unified: 'tool-calls', raw: 'host-input' },
67
+ usage: {
68
+ inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
69
+ outputTokens: { total: 0, text: 0 },
70
+ },
71
+ harnessMetadata: { opencode: { inferredStep: true } },
72
+ });
73
+ };
74
+
75
+ return {
76
+ observeToolCall({ callID, status, group }) {
77
+ if (status === 'started') {
78
+ unsettledToolCalls.set(callID, group);
79
+ } else {
80
+ unsettledToolCalls.delete(callID);
81
+ }
82
+ emitEarlyFinishStepIfReady();
83
+ },
84
+ markWaiting({ toolCallId }) {
85
+ if (!unsettledToolCalls.has(toolCallId)) {
86
+ unsettledToolCalls.set(toolCallId, undefined);
87
+ }
88
+ waitingToolCallIds.add(toolCallId);
89
+ emitEarlyFinishStepIfReady();
90
+ },
91
+ clearWaiting({ toolCallId, settled = false }) {
92
+ waitingToolCallIds.delete(toolCallId);
93
+ if (settled) unsettledToolCalls.delete(toolCallId);
94
+ },
95
+ markGroupWaiting({ group, id }) {
96
+ groupWaits.get(group)!.add(id);
97
+ emitEarlyFinishStepIfReady();
98
+ },
99
+ clearGroupWaiting({ group, id }) {
100
+ groupWaits.get(group)!.delete(id);
101
+ },
102
+ observeFinishStep() {
103
+ coveredIds.clear();
104
+ },
105
+ };
106
+ }
@@ -1,4 +1,5 @@
1
1
  import { createServer, type Server } from 'node:http';
2
+ import type { BridgeTurn } from '@ai-sdk/harness/bridge';
2
3
  import { ToolRelayAuthorizer, type ToolRelayCall } from './tool-relay-auth';
3
4
 
4
5
  export type ToolRelay = {
@@ -11,13 +12,13 @@ export async function startAuthorizedToolRelay({
11
12
  tools,
12
13
  emit,
13
14
  requestToolResult,
15
+ onWaitingChange,
14
16
  authorizer = new ToolRelayAuthorizer(),
15
17
  }: {
16
18
  tools: ReadonlyArray<{ name: string }>;
17
19
  emit: (message: Record<string, unknown>) => void;
18
- requestToolResult: (
19
- toolCallId: string,
20
- ) => Promise<{ output: unknown; isError?: boolean }>;
20
+ requestToolResult: BridgeTurn['requestToolResult'];
21
+ onWaitingChange?: (input: { requestId: string; waiting: boolean }) => void;
21
22
  authorizer?: ToolRelayAuthorizer;
22
23
  }): Promise<ToolRelay> {
23
24
  const toolNames = new Set(tools.map(tool => tool.name));
@@ -62,7 +63,10 @@ export async function startAuthorizedToolRelay({
62
63
  providerExecuted: false,
63
64
  });
64
65
 
65
- const { output, isError } = await requestToolResult(requestId);
66
+ onWaitingChange?.({ requestId, waiting: true });
67
+ const { output, isError, toolResult } = await requestToolResult(
68
+ requestId,
69
+ ).finally(() => onWaitingChange?.({ requestId, waiting: false }));
66
70
  emit({
67
71
  type: 'tool-result',
68
72
  toolCallId: requestId,
@@ -72,7 +76,13 @@ export async function startAuthorizedToolRelay({
72
76
  });
73
77
 
74
78
  res.writeHead(200, { 'Content-Type': 'application/json' });
75
- res.end(JSON.stringify({ result: output }));
79
+ res.end(
80
+ JSON.stringify({
81
+ result: output,
82
+ ...(isError != null ? { isError } : {}),
83
+ ...(toolResult != null ? { toolResult } : {}),
84
+ }),
85
+ );
76
86
  } catch (error) {
77
87
  res.writeHead(500, { 'Content-Type': 'application/json' });
78
88
  res.end(