refactor: only write reasoning to file; count line overflow better
This commit is contained in:
parent
8facbb0de2
commit
993fb143a2
2 changed files with 13 additions and 15 deletions
20
index.ts
20
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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue