refactor: only write reasoning to file; count line overflow better

This commit is contained in:
mitchell 2026-04-16 04:00:33 -04:00
parent 8facbb0de2
commit 993fb143a2
2 changed files with 13 additions and 15 deletions

View file

@ -6,9 +6,6 @@ import { renderStream } from "./src/render";
const home = os.homedir(); const home = os.homedir();
const convoFile = Bun.file(`${home}/.aicl_convo.json`); 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({ const provider = createOpenAICompatible({
name: "llama.cpp", name: "llama.cpp",
@ -27,8 +24,8 @@ try {
const prompt = Bun.argv[2] ?? "Introduce yourself."; const prompt = Bun.argv[2] ?? "Introduce yourself.";
messages.push({ role: "user", content: prompt }); messages.push({ role: "user", content: prompt });
const { fullStream, textStream, reasoningText, text, totalUsage } = streamText({ const { fullStream, textStream, text, totalUsage } = streamText({
model: provider("qwen3.5-35b-a3b"), model: provider("gemma4-26b-a4b"),
messages, messages,
}); });
@ -43,20 +40,15 @@ const glow = Bun.spawn({
stdout: "inherit", stdout: "inherit",
}); });
const reasoning = await reasoningText; await streamRender;
const response = await text;
const stop = Date.now(); 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; await glow.exited;
messages.push({ role: "assistant", content: await text });
await convoFile.write(JSON.stringify(messages));
const { inputTokens, outputTokens } = await totalUsage; const { inputTokens, outputTokens } = await totalUsage;
const context = (inputTokens ?? 0) / 1000; const context = (inputTokens ?? 0) / 1000;
const output = (outputTokens ?? 0) / 1000; const output = (outputTokens ?? 0) / 1000;

View file

@ -1,5 +1,9 @@
import os from "node:os";
import type { AsyncIterableStream, TextStreamPart, ToolSet } from "ai"; import type { AsyncIterableStream, TextStreamPart, ToolSet } from "ai";
const home = os.homedir();
const reasoningWriter = Bun.file(`${home}/.aicl_reasoning.md`).writer();
let lineCount = 0; let lineCount = 0;
let charsSinceNL = 0; let charsSinceNL = 0;
@ -36,12 +40,14 @@ export async function renderStream(
write("\u001b[2m\u001b[3m\n"); write("\u001b[2m\u001b[3m\n");
} else if (part.type === "reasoning-delta") { } else if (part.type === "reasoning-delta") {
write(part.text); write(part.text);
reasoningWriter.write(part.text);
checkContainment(part.text); checkContainment(part.text);
} else if (part.type === "reasoning-end") { } else if (part.type === "reasoning-end") {
write("\n\u001b[22m\u001b[23m"); write("\n\u001b[22m\u001b[23m");
moveCursor(0, -lineCount - 2); moveCursor(0, -lineCount - 2);
clearScreenDown(); clearScreenDown();
resetCounts(); resetCounts();
reasoningWriter.end();
} else if (part.type === "text-start") { } else if (part.type === "text-start") {
write("\n"); write("\n");
} else if (part.type === "text-delta") { } else if (part.type === "text-delta") {
@ -64,7 +70,7 @@ function checkContainment(text: string) {
charsSinceNL += text.length; charsSinceNL += text.length;
if (text.includes("\n")) { if (text.includes("\n")) {
lineCount += text.match(/\n/g)?.length ?? 1; lineCount += text.match(/\n/g)?.length ?? 1;
charsSinceNL = 0; charsSinceNL = text.length - text.lastIndexOf("\n") - 1;
} else if (charsSinceNL >= columns) { } else if (charsSinceNL >= columns) {
lineCount++; lineCount++;
charsSinceNL = charsSinceNL - columns; charsSinceNL = charsSinceNL - columns;