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.";
|
const prompt = Bun.argv[2] ?? "Introduce yourself.";
|
||||||
messages.push({ role: "user", content: prompt });
|
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"),
|
model: provider("qwen3.5-35b-a3b"),
|
||||||
messages,
|
messages,
|
||||||
});
|
});
|
||||||
|
|
||||||
const start = Date.now();
|
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 reasoning = await reasoningText;
|
||||||
const response = await text;
|
const response = await text;
|
||||||
|
|
||||||
const stop = Date.now();
|
const stop = Date.now();
|
||||||
|
|
||||||
await reasoningFile.write(reasoning ?? "");
|
|
||||||
await responseFile.write(response);
|
|
||||||
|
|
||||||
messages.push({ role: "assistant", content: response });
|
messages.push({ role: "assistant", content: response });
|
||||||
await convoFile.write(JSON.stringify(messages));
|
await convoFile.write(JSON.stringify(messages));
|
||||||
|
|
||||||
const [numColumns] = process.stdout.getWindowSize();
|
await reasoningFile.write(reasoning ?? "");
|
||||||
Bun.spawnSync({
|
await responseFile.write(response);
|
||||||
cmd: ["glow", "-w", `${numColumns}`, responsePath],
|
|
||||||
stdio: ["inherit", "inherit", "inherit"],
|
await streamRender;
|
||||||
});
|
await glow.exited;
|
||||||
|
|
||||||
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;
|
||||||
const time = (stop - start) / 1000;
|
const time = (stop - start) / 1000;
|
||||||
process.stderr.write(
|
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