refactor: stream text directly to glow; add style to stats
This commit is contained in:
parent
55b110a220
commit
7214fad3e7
1 changed files with 15 additions and 11 deletions
26
index.ts
26
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`,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue