xonecas.io
64875
Mc

mini-coder / commit 8ff94b0

fix(llm): prevent anthropic stream hanging & add reasoning UI

This fixes an issue where Anthropic models would hang at the end of a turn indefinitely when run in Bun. The underlying issue is a stream tee() deadlock in the @ai-sdk/anthropic provider. We bypass this by capturing the deduplicated messages from onStepFinish instead of awaiting the faulty result.response promise.

Additionally, this adds full streaming support for reasoning/thinking chunks across all supported providers (Anthropic, OpenAI, Google) using the standard ai sdk 'reasoning' chunk type, and excludes these chunks from spamming the SQLite API logs.

xonecas2026-03-09 8ff94b0e370df58f81023c7b36e4fe1a0ab99d90 parent dc69057

3 files changed

src/llm-api/providers.ts+0−1
@@ -4,7 +4,6 @@ import { createOpenAI } from "@ai-sdk/openai";
44 import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
55 import type { LanguageModel } from "ai";
66
7-
87 import { logApiEvent } from "./api-log.ts";
98 import {
109 type AvailableModelsSnapshot,
src/llm-api/turn.ts+27−8
@@ -130,6 +130,8 @@ export async function* runTurn(options: {
130130 : {}),
131131 };
132132
133+ let finalMessages: CoreMessage[] = [];
134+
133135 const streamOpts: StreamTextOptions = {
134136 model,
135137 messages,
@@ -145,6 +147,16 @@ export async function* runTurn(options: {
145147 outputTokens += step.usage?.outputTokens ?? 0;
146148 contextTokens = step.usage?.inputTokens ?? contextTokens;
147149 stepCount++;
150+
151+ // Accumulate messages from each step finish.
152+ // AI SDK's step.response.messages (or step.messages in newer versions)
153+ // contains the authoritative, deduplicated list of all messages
154+ // generated across all steps.
155+ const s = step as unknown as {
156+ response?: { messages?: CoreMessage[] };
157+ messages?: CoreMessage[];
158+ };
159+ finalMessages = s.response?.messages ?? s.messages ?? finalMessages;
148160 },
149161 // On the last allowed step, strip all tools so the model is forced to
150162 // respond with text — no more tool calls are possible.
@@ -175,7 +187,7 @@ export async function* runTurn(options: {
175187
176188 const c = chunk as StreamChunk;
177189
178- if (c.type !== "text-delta") {
190+ if (c.type !== "text-delta" && c.type !== "reasoning") {
179191 logApiEvent("stream chunk", {
180192 type: c.type,
181193 toolCallId: c.toolCallId,
@@ -202,6 +214,14 @@ export async function* runTurn(options: {
202214 };
203215 break;
204216 }
217+ case "reasoning": {
218+ const delta = typeof c.textDelta === "string" ? c.textDelta : "";
219+ yield {
220+ type: "reasoning-delta",
221+ delta,
222+ };
223+ break;
224+ }
205225
206226 case "tool-call": {
207227 yield {
@@ -244,13 +264,12 @@ export async function* runTurn(options: {
244264 }
245265 }
246266
247- // Collect the final response messages after the stream completes.
248- // Using result.response (which resolves to the final step's response)
249- // gives us the authoritative, deduplicated list of all messages generated
250- // across all steps. Accumulating from onStepFinish would cause duplicates
251- // because step.response.messages includes all prior step messages PLUS the
252- // current step's messages on each callback invocation.
253- const newMessages = (await result.response)?.messages ?? [];
267+ // We use finalMessages accumulated from onStepFinish instead of awaiting result.response.
268+ // Awaiting result.response causes the process to hang indefinitely for Anthropic models in Bun
269+ // due to a known bug in @ai-sdk/anthropic's tee() stream usage.
270+ // Since step.response.messages includes the full deduplicated list, the last invocation
271+ // gives us the exact equivalent without waiting on the broken promise.
272+ const newMessages = finalMessages;
254273
255274 logApiEvent("turn complete", {
256275 newMessagesCount: newMessages.length,
src/llm-api/types.ts+6−0
@@ -2,6 +2,11 @@ import type { CoreMessage } from "./turn.ts";
22
33 // ─── Turn events (streamed to the caller) ─────────────────────────────────────
44
5+export interface ReasoningDeltaEvent {
6+ type: "reasoning-delta";
7+ delta: string;
8+}
9+
510 export interface TextDeltaEvent {
611 type: "text-delta";
712 delta: string;
@@ -43,6 +48,7 @@ export interface TurnErrorEvent {
4348 }
4449
4550 export type TurnEvent =
51+ | ReasoningDeltaEvent
4652 | TextDeltaEvent
4753 | ToolCallStartEvent
4854 | ToolResultEvent