From 7214fad3e75cd1ab7842cef8824b9f918f8f69dc Mon Sep 17 00:00:00 2001 From: mitchell Date: Wed, 15 Apr 2026 22:35:19 -0400 Subject: [PATCH] refactor: stream text directly to glow; add style to stats --- index.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/index.ts b/index.ts index cbd1503..f28c869 100644 --- a/index.ts +++ b/index.ts @@ -27,36 +27,40 @@ try { const prompt = Bun.argv[2] ?? "Introduce yourself."; messages.push({ role: "user", content: prompt }); -const { fullStream, reasoningText, text, totalUsage } = streamText({ +const { fullStream, textStream, reasoningText, text, totalUsage } = streamText({ model: provider("qwen3.5-35b-a3b"), messages, }); const start = Date.now(); -renderStream(fullStream); +const streamRender = renderStream(fullStream); + +const [width] = process.stdout.getWindowSize(); +const glow = Bun.spawn({ + cmd: ["glow", "-w", `${width}`, "-"], + stdin: textStream, + stdout: "inherit", +}); const reasoning = await reasoningText; const response = await text; const stop = Date.now(); -await reasoningFile.write(reasoning ?? ""); -await responseFile.write(response); - messages.push({ role: "assistant", content: response }); await convoFile.write(JSON.stringify(messages)); -const [numColumns] = process.stdout.getWindowSize(); -Bun.spawnSync({ - cmd: ["glow", "-w", `${numColumns}`, responsePath], - stdio: ["inherit", "inherit", "inherit"], -}); +await reasoningFile.write(reasoning ?? ""); +await responseFile.write(response); + +await streamRender; +await glow.exited; const { inputTokens, outputTokens } = await totalUsage; const context = (inputTokens ?? 0) / 1000; const output = (outputTokens ?? 0) / 1000; const time = (stop - start) / 1000; process.stderr.write( - `Context: ${context.toFixed(1)}k, Output: ${output.toFixed(1)}k, Time: ${time.toFixed(2)}s\n`, + `\u001b[2mContext: ${context.toFixed(1)}k, Output: ${output.toFixed(1)}k, Time: ${time.toFixed(2)}s\u001b[22m\n`, );