From 993fb143a25e2c710b7ed84bfb99976570692a0a Mon Sep 17 00:00:00 2001 From: mitchell Date: Thu, 16 Apr 2026 04:00:33 -0400 Subject: [PATCH] refactor: only write reasoning to file; count line overflow better --- index.ts | 20 ++++++-------------- src/render/index.ts | 8 +++++++- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/index.ts b/index.ts index f28c869..d54a6f1 100644 --- a/index.ts +++ b/index.ts @@ -6,9 +6,6 @@ import { renderStream } from "./src/render"; const home = os.homedir(); const convoFile = Bun.file(`${home}/.aicl_convo.json`); -const reasoningFile = Bun.file(`${home}/.aicl_reasoning.md`); -const responsePath = `${home}/.aicl_response.md`; -const responseFile = Bun.file(responsePath); const provider = createOpenAICompatible({ name: "llama.cpp", @@ -27,8 +24,8 @@ try { const prompt = Bun.argv[2] ?? "Introduce yourself."; messages.push({ role: "user", content: prompt }); -const { fullStream, textStream, reasoningText, text, totalUsage } = streamText({ - model: provider("qwen3.5-35b-a3b"), +const { fullStream, textStream, text, totalUsage } = streamText({ + model: provider("gemma4-26b-a4b"), messages, }); @@ -43,20 +40,15 @@ const glow = Bun.spawn({ stdout: "inherit", }); -const reasoning = await reasoningText; -const response = await text; +await streamRender; const stop = Date.now(); -messages.push({ role: "assistant", content: response }); -await convoFile.write(JSON.stringify(messages)); - -await reasoningFile.write(reasoning ?? ""); -await responseFile.write(response); - -await streamRender; await glow.exited; +messages.push({ role: "assistant", content: await text }); +await convoFile.write(JSON.stringify(messages)); + const { inputTokens, outputTokens } = await totalUsage; const context = (inputTokens ?? 0) / 1000; const output = (outputTokens ?? 0) / 1000; diff --git a/src/render/index.ts b/src/render/index.ts index eec9646..46be8e3 100644 --- a/src/render/index.ts +++ b/src/render/index.ts @@ -1,5 +1,9 @@ +import os from "node:os"; import type { AsyncIterableStream, TextStreamPart, ToolSet } from "ai"; +const home = os.homedir(); +const reasoningWriter = Bun.file(`${home}/.aicl_reasoning.md`).writer(); + let lineCount = 0; let charsSinceNL = 0; @@ -36,12 +40,14 @@ export async function renderStream( write("\u001b[2m\u001b[3m\n"); } else if (part.type === "reasoning-delta") { write(part.text); + reasoningWriter.write(part.text); checkContainment(part.text); } else if (part.type === "reasoning-end") { write("\n\u001b[22m\u001b[23m"); moveCursor(0, -lineCount - 2); clearScreenDown(); resetCounts(); + reasoningWriter.end(); } else if (part.type === "text-start") { write("\n"); } else if (part.type === "text-delta") { @@ -64,7 +70,7 @@ function checkContainment(text: string) { charsSinceNL += text.length; if (text.includes("\n")) { lineCount += text.match(/\n/g)?.length ?? 1; - charsSinceNL = 0; + charsSinceNL = text.length - text.lastIndexOf("\n") - 1; } else if (charsSinceNL >= columns) { lineCount++; charsSinceNL = charsSinceNL - columns;