Open History to see the last commit
1import { EventType, buildBaseUsage } from '@tanstack/ai'
2import type { StreamChunk, TokenUsage } from '@tanstack/ai'
3import type {
4 OpencodeAssistantMessage,
5 OpencodeEvent,
6 OpencodePart,
7 OpencodeStreamEvent,
8 OpencodeTokens,
9} from './sdk-types'
10
11/** Name of the CUSTOM event carrying the OpenCode session id. */
12export const SESSION_ID_EVENT = 'opencode.session-id'
13
14/** Name of the CUSTOM event carrying the harness's todo list updates. */
15export const TODO_EVENT = 'opencode.todo'
16
17/** Server name used for bridged TanStack tools. */
18export const BRIDGED_MCP_SERVER_NAME = 'tanstack'
19
20export interface TranslateContext {
21 model: string
22 runId: string
23 threadId: string
24 parentRunId?: string
25 genId: () => string
26 /**
27 * Names of bridged TanStack tools, used to surface the harness's MCP tool
28 * calls under the names the application registered.
29 */
30 bridgedToolNames?: ReadonlySet<string>
31 /** Called for each raw stream event, for logging. */
32 onStreamEvent?: (event: OpencodeStreamEvent) => void
33}
34
35/**
36 * Resolve the AG-UI tool-call name for an OpenCode tool part. OpenCode names
37 * MCP tools `<server>_<tool>`, so bridged TanStack tools arrive as
38 * `tanstack_<tool>` and are surfaced under the names the application
39 * registered; everything else (built-in `read`, `edit`, `bash`, ... and
40 * foreign MCP tools) uses the harness tool name verbatim.
41 */
42export function resolveToolName(
43 tool: string,
44 bridgedToolNames: ReadonlySet<string> | undefined,
45): string {
46 if (!bridgedToolNames || bridgedToolNames.size === 0) return tool
47 if (bridgedToolNames.has(tool)) return tool
48 if (tool.startsWith('tanstack_') && bridgedToolNames.has(tool.slice(9))) {
49 return tool.slice(9)
50 }
51 return tool
52}
53
54function buildUsage(
55 tokens: OpencodeTokens | undefined,
56): TokenUsage | undefined {
57 if (!tokens) return undefined
58 const promptTokens = tokens.input ?? 0
59 const completionTokens = tokens.output ?? 0
60 const result = buildBaseUsage({
61 promptTokens,
62 completionTokens,
63 totalTokens: promptTokens + completionTokens,
64 })
65 if (tokens.cache?.read) {
66 result.promptTokensDetails = { cachedTokens: tokens.cache.read }
67 }
68 if (tokens.reasoning) {
69 result.completionTokensDetails = { reasoningTokens: tokens.reasoning }
70 }
71 return result
72}
73
74type TextPart = Extract<OpencodePart, { type: 'text' }>
75type ReasoningPart = Extract<OpencodePart, { type: 'reasoning' }>
76type ToolPart = Extract<OpencodePart, { type: 'tool' }>
77
78const isTextPart = (part: OpencodePart): part is TextPart =>
79 part.type === 'text'
80const isReasoningPart = (part: OpencodePart): part is ReasoningPart =>
81 part.type === 'reasoning'
82const isToolPart = (part: OpencodePart): part is ToolPart =>
83 part.type === 'tool'
84
85function messageError(
86 message: OpencodeAssistantMessage,
87): { message: string } | undefined {
88 if (!message.error) return undefined
89 return { message: message.error.data?.message ?? message.error.name }
90}
91
92/**
93 * Translate an OpenCode event stream into AG-UI StreamChunk events.
94 *
95 * The harness runs its own agent loop and executes its own tools, so the
96 * translation always ends with `finishReason: 'stop'` (or `'length'` /
97 * RUN_ERROR) — never `'tool_calls'`. Harness tool activity is emitted as
98 * already-resolved TOOL_CALL_START/ARGS/END + TOOL_CALL_RESULT sequences so
99 * UIs can render it, while the TanStack engine never tries to execute them.
100 *
101 * OpenCode delivers true token-level deltas for both assistant text and
102 * reasoning via `message.part.updated` events (a `delta` string when
103 * incremental, otherwise the full part text, from which the delta is
104 * derived). The final assistant message — finish reason, token usage, and any
105 * fatal error — arrives as the terminal `done` event.
106 *
107 * Invariant: every TOOL_CALL_START is eventually paired with a
108 * TOOL_CALL_RESULT (synthesized as `{"status":"interrupted"}` when the run
109 * ends or aborts before the harness reported one) so the engine's
110 * pending-tool-call scan on the next request never force-executes them.
111 */
112export async function* translateOpencodeStream(
113 events: AsyncIterable<OpencodeStreamEvent>,
114 ctx: TranslateContext,
115): AsyncIterable<StreamChunk> {
116 const { model, runId, threadId, genId } = ctx
117 const now = () => Date.now()
118
119 let runStarted = false
120 /** Tool calls started but with no result yet, keyed by callID. */
121 const unresolvedToolCalls = new Set<string>()
122 /** Tool call ids that already emitted TOOL_CALL_START/ARGS/END. */
123 const openedToolCalls = new Set<string>()
124 /** Tool call ids that already emitted a TOOL_CALL_RESULT. */
125 const resolvedToolCalls = new Set<string>()
126
127 /** Accumulated text per text-part id, for delta derivation. */
128 const textAccumulators = new Map<string, string>()
129 let openTextId: string | null = null
130 let openReasoningId: string | null = null
131
132 function* startRun(): Generator<StreamChunk> {
133 if (runStarted) return
134 runStarted = true
135 yield {
136 type: EventType.RUN_STARTED,
137 runId,
138 threadId,
139 model,
140 timestamp: now(),
141 ...(ctx.parentRunId !== undefined && { parentRunId: ctx.parentRunId }),
142 }
143 }
144
145 function* closeText(): Generator<StreamChunk> {
146 if (openTextId !== null) {
147 yield {
148 type: EventType.TEXT_MESSAGE_END,
149 messageId: openTextId,
150 model,
151 timestamp: now(),
152 }
153 openTextId = null
154 }
155 }
156
157 function* closeReasoning(): Generator<StreamChunk> {
158 if (openReasoningId !== null) {
159 yield {
160 type: EventType.REASONING_MESSAGE_END,
161 messageId: openReasoningId,
162 model,
163 timestamp: now(),
164 }
165 yield {
166 type: EventType.REASONING_END,
167 messageId: openReasoningId,
168 model,
169 timestamp: now(),
170 }
171 openReasoningId = null
172 }
173 }
174
175 function* synthesizeUnresolvedResults(): Generator<StreamChunk> {
176 for (const toolCallId of unresolvedToolCalls) {
177 yield {
178 type: EventType.TOOL_CALL_RESULT,
179 toolCallId,
180 messageId: genId(),
181 model,
182 timestamp: now(),
183 content: JSON.stringify({ status: 'interrupted' }),
184 }
185 }
186 unresolvedToolCalls.clear()
187 }
188
189 function* handleTextPart(
190 part: Extract<OpencodePart, { type: 'text' }>,
191 delta: string | undefined,
192 ): Generator<StreamChunk> {
193 yield* closeReasoning()
194
195 const prev = textAccumulators.get(part.id) ?? ''
196 let deltaText: string
197 if (typeof delta === 'string' && delta !== '') {
198 deltaText = delta
199 textAccumulators.set(part.id, prev + delta)
200 } else {
201 const full = part.text
202 deltaText = full.startsWith(prev) ? full.slice(prev.length) : full
203 textAccumulators.set(part.id, full)
204 }
205 if (deltaText === '') return
206
207 if (openTextId !== part.id) {
208 yield* closeText()
209 openTextId = part.id
210 yield {
211 type: EventType.TEXT_MESSAGE_START,
212 messageId: part.id,
213 model,
214 timestamp: now(),
215 role: 'assistant',
216 }
217 }
218 yield {
219 type: EventType.TEXT_MESSAGE_CONTENT,
220 messageId: part.id,
221 model,
222 timestamp: now(),
223 delta: deltaText,
224 content: textAccumulators.get(part.id) ?? deltaText,
225 }
226 }
227
228 function* handleReasoningPart(
229 part: Extract<OpencodePart, { type: 'reasoning' }>,
230 delta: string | undefined,
231 ): Generator<StreamChunk> {
232 yield* closeText()
233
234 const prev = textAccumulators.get(part.id) ?? ''
235 let deltaText: string
236 if (typeof delta === 'string' && delta !== '') {
237 deltaText = delta
238 textAccumulators.set(part.id, prev + delta)
239 } else {
240 const full = part.text
241 deltaText = full.startsWith(prev) ? full.slice(prev.length) : full
242 textAccumulators.set(part.id, full)
243 }
244 if (deltaText === '') return
245
246 if (openReasoningId !== part.id) {
247 yield* closeReasoning()
248 openReasoningId = part.id
249 yield {
250 type: EventType.REASONING_START,
251 messageId: part.id,
252 model,
253 timestamp: now(),
254 }
255 yield {
256 type: EventType.REASONING_MESSAGE_START,
257 messageId: part.id,
258 role: 'reasoning' as const,
259 model,
260 timestamp: now(),
261 }
262 }
263 yield {
264 type: EventType.REASONING_MESSAGE_CONTENT,
265 messageId: part.id,
266 delta: deltaText,
267 model,
268 timestamp: now(),
269 }
270 }
271
272 function* openToolCall(
273 part: Extract<OpencodePart, { type: 'tool' }>,
274 ): Generator<StreamChunk> {
275 if (openedToolCalls.has(part.callID)) return
276 openedToolCalls.add(part.callID)
277 const toolCallName = resolveToolName(part.tool, ctx.bridgedToolNames)
278 const input = part.state.input ?? {}
279 const args = JSON.stringify(input)
280 yield {
281 type: EventType.TOOL_CALL_START,
282 toolCallId: part.callID,
283 toolCallName,
284 toolName: toolCallName,
285 model,
286 timestamp: now(),
287 }
288 yield {
289 type: EventType.TOOL_CALL_ARGS,
290 toolCallId: part.callID,
291 model,
292 timestamp: now(),
293 delta: args,
294 args,
295 }
296 yield {
297 type: EventType.TOOL_CALL_END,
298 toolCallId: part.callID,
299 toolCallName,
300 toolName: toolCallName,
301 model,
302 timestamp: now(),
303 input,
304 }
305 unresolvedToolCalls.add(part.callID)
306 }
307
308 function* handleToolPart(
309 part: Extract<OpencodePart, { type: 'tool' }>,
310 ): Generator<StreamChunk> {
311 yield* closeText()
312 yield* closeReasoning()
313 yield* openToolCall(part)
314
315 const state = part.state
316 if (state.status !== 'completed' && state.status !== 'error') return
317 if (resolvedToolCalls.has(part.callID)) return
318 resolvedToolCalls.add(part.callID)
319 unresolvedToolCalls.delete(part.callID)
320
321 const isError = state.status === 'error'
322 yield {
323 type: EventType.TOOL_CALL_RESULT,
324 toolCallId: part.callID,
325 messageId: genId(),
326 model,
327 timestamp: now(),
328 content: isError ? state.error : state.output,
329 ...(isError && { state: 'output-error' as const }),
330 }
331 }
332
333 function* handleEvent(event: OpencodeEvent): Generator<StreamChunk> {
334 if (event.type === 'message.part.updated') {
335 const { part, delta } = event.properties
336 if (isTextPart(part)) {
337 yield* handleTextPart(part, delta)
338 } else if (isReasoningPart(part)) {
339 yield* handleReasoningPart(part, delta)
340 } else if (isToolPart(part)) {
341 yield* handleToolPart(part)
342 }
343 // Other part kinds (file, step-start/finish, snapshot, ...) carry no
344 // state the chunk stream needs.
345 } else if (event.type === 'todo.updated') {
346 yield {
347 type: EventType.CUSTOM,
348 model,
349 timestamp: now(),
350 name: TODO_EVENT,
351 value: { todos: event.properties.todos },
352 }
353 }
354 // session.idle / session.status / message.updated are redundant with the
355 // terminal `done` event and are ignored.
356 }
357
358 function* finish(message: OpencodeAssistantMessage): Generator<StreamChunk> {
359 yield* startRun()
360 yield* closeText()
361 yield* closeReasoning()
362 yield* synthesizeUnresolvedResults()
363
364 const error = messageError(message)
365 if (error) {
366 yield {
367 type: EventType.RUN_ERROR,
368 model,
369 timestamp: now(),
370 message: error.message,
371 error,
372 }
373 return
374 }
375
376 const usage = buildUsage(message.tokens)
377 const finishReason = message.finish === 'length' ? 'length' : 'stop'
378 yield {
379 type: EventType.RUN_FINISHED,
380 runId,
381 threadId,
382 model,
383 timestamp: now(),
384 finishReason,
385 ...(usage !== undefined && { usage }),
386 }
387 }
388
389 try {
390 for await (const streamEvent of events) {
391 ctx.onStreamEvent?.(streamEvent)
392
393 if (streamEvent.kind === 'session') {
394 yield* startRun()
395 yield {
396 type: EventType.CUSTOM,
397 model,
398 timestamp: now(),
399 name: SESSION_ID_EVENT,
400 value: { sessionId: streamEvent.sessionId },
401 }
402 } else if (streamEvent.kind === 'event') {
403 yield* startRun()
404 yield* handleEvent(streamEvent.event)
405 } else {
406 yield* finish(streamEvent.message)
407 }
408 }
409 } catch (error) {
410 // The run is dying (abort, server exit, or connection failure). Close any
411 // open message and pair started tool calls with a synthetic result first
412 // so the next request's pending-tool-call scan doesn't try to execute
413 // them, then let the adapter surface the error as RUN_ERROR.
414 yield* closeText()
415 yield* closeReasoning()
416 yield* synthesizeUnresolvedResults()
417 throw error
418 }
419}