From e581d80c7a12e260b0ab0636033541e06dc0a163 Mon Sep 17 00:00:00 2001 From: Gab Date: Thu, 26 Mar 2026 15:34:28 +1100 Subject: [PATCH] fix: toothfairyai provider --- .../sdk/toothfairyai/toothfairyai-provider.ts | 98 ++++++++----------- 1 file changed, 42 insertions(+), 56 deletions(-) diff --git a/packages/tfcode/src/provider/sdk/toothfairyai/toothfairyai-provider.ts b/packages/tfcode/src/provider/sdk/toothfairyai/toothfairyai-provider.ts index 454112669..d2ac33354 100644 --- a/packages/tfcode/src/provider/sdk/toothfairyai/toothfairyai-provider.ts +++ b/packages/tfcode/src/provider/sdk/toothfairyai/toothfairyai-provider.ts @@ -117,71 +117,57 @@ export function createToothFairyAI(options: ToothFairyAIProviderSettings = {}): } if (res.body && res.headers.get("content-type")?.includes("text/event-stream")) { - const reader = res.body.getReader() const decoder = new TextDecoder() const encoder = new TextEncoder() + let buffer = "" - const filteredStream = new ReadableStream({ - async pull(controller) { - const { done, value } = await reader.read() - if (done) { - controller.close() - return - } + const filteredStream = res.body.pipeThrough( + new TransformStream({ + transform(chunk, controller) { + buffer += decoder.decode(chunk, { stream: true }) + const lines = buffer.split("\n") + buffer = lines.pop() || "" - const text = decoder.decode(value, { stream: true }) - const lines = text.split("\n") - - const filtered: string[] = [] - for (const line of lines) { - if (line.startsWith("data: ")) { - const json = line.slice(6).trim() - // Filter out connection status messages like {"status":"initialising"}, {"status":"connected"} - // These are internal progress indicators, not OpenAI-format chunks - if (json) { - try { - const parsed = JSON.parse(json) - if (parsed.status === "initialising" || parsed.status === "connected") { - log.debug("filtered connection status", { status: parsed.status }) - continue - } - } catch { - // Not valid JSON, keep the line + const filtered: string[] = [] + for (const line of lines) { + if (line.startsWith("data: ")) { + const json = line.slice(6).trim() + if (json) { + try { + const parsed = JSON.parse(json) + if (parsed.status === "initialising" || parsed.status === "connected") { + log.debug("filtered connection status", { status: parsed.status }) + continue + } + if (parsed.choices?.[0]?.finish_reason) { + log.info("stream finish_reason", { + finish_reason: parsed.choices[0].finish_reason, + }) + } + if (parsed.usage) { + log.info("stream usage", { + prompt_tokens: parsed.usage.prompt_tokens, + completion_tokens: parsed.usage.completion_tokens, + total_tokens: parsed.usage.total_tokens, + }) + } + } catch {} } } filtered.push(line) - // Log tool calls and finish_reason - try { - const parsed = JSON.parse(json) - if (parsed.choices?.[0]?.delta?.tool_calls) { - log.debug("stream tool_calls", { - tool_calls: parsed.choices[0].delta.tool_calls, - }) - } - if (parsed.choices?.[0]?.finish_reason) { - log.info("stream finish_reason", { - finish_reason: parsed.choices[0].finish_reason, - }) - } - if (parsed.usage) { - log.info("stream usage", { - prompt_tokens: parsed.usage.prompt_tokens, - completion_tokens: parsed.usage.completion_tokens, - total_tokens: parsed.usage.total_tokens, - }) - } - } catch {} - } else { - filtered.push(line) } - } - controller.enqueue(encoder.encode(filtered.join("\n"))) - }, - cancel() { - reader.cancel() - }, - }) + if (filtered.length > 0) { + controller.enqueue(encoder.encode(filtered.join("\n") + "\n")) + } + }, + flush(controller) { + if (buffer) { + controller.enqueue(encoder.encode(buffer)) + } + }, + }), + ) return new Response(filteredStream, { headers: res.headers,