refactor: move render logic to it's own module; add build script
This commit is contained in:
parent
ab648271c2
commit
cfc494ba53
3 changed files with 76 additions and 65 deletions
67
src/render/index.ts
Normal file
67
src/render/index.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import type { AsyncIterableStream, TextStreamPart, ToolSet } from "ai";
|
||||
|
||||
const moveCursor = (x: number, y: number) =>
|
||||
new Promise<void>((resolve) => process.stdout.moveCursor(x, y, resolve));
|
||||
const clearScreenDown = () =>
|
||||
new Promise<void>((resolve) => process.stdout.clearScreenDown(resolve));
|
||||
|
||||
export async function renderStream(
|
||||
stream: AsyncIterableStream<TextStreamPart<ToolSet>>,
|
||||
) {
|
||||
let lineCount = 0;
|
||||
let charsSinceNL = 0;
|
||||
const [numColumns, numRows] = process.stdout.getWindowSize();
|
||||
|
||||
for await (const part of stream) {
|
||||
if (part.type === "reasoning-start") {
|
||||
process.stdout.write("\u001b[2m\n");
|
||||
} else if (part.type === "reasoning-delta") {
|
||||
process.stdout.write(part.text);
|
||||
|
||||
charsSinceNL += part.text.length;
|
||||
if (part.text.includes("\n")) {
|
||||
lineCount += part.text.match(/\n/g)?.length ?? 1;
|
||||
charsSinceNL = 0;
|
||||
} else if (charsSinceNL >= numColumns) {
|
||||
lineCount++;
|
||||
charsSinceNL = charsSinceNL - numColumns;
|
||||
}
|
||||
|
||||
if (lineCount >= numRows / 2) {
|
||||
await moveCursor(0, -lineCount);
|
||||
await clearScreenDown();
|
||||
lineCount = 0;
|
||||
charsSinceNL = 0;
|
||||
}
|
||||
} else if (part.type === "reasoning-end") {
|
||||
process.stdout.write("\n\u001b[22m");
|
||||
await moveCursor(0, -lineCount - 2);
|
||||
await clearScreenDown();
|
||||
lineCount = 0;
|
||||
charsSinceNL = 0;
|
||||
} else if (part.type === "text-start") {
|
||||
process.stdout.write("\n");
|
||||
} else if (part.type === "text-delta") {
|
||||
process.stdout.write(part.text);
|
||||
|
||||
charsSinceNL += part.text.length;
|
||||
if (part.text.includes("\n")) {
|
||||
lineCount += part.text.match(/\n/g)?.length ?? 1;
|
||||
charsSinceNL = 0;
|
||||
} else if (charsSinceNL >= numColumns) {
|
||||
lineCount++;
|
||||
charsSinceNL = charsSinceNL - numColumns;
|
||||
}
|
||||
|
||||
if (lineCount >= numRows / 2) {
|
||||
await moveCursor(0, -lineCount);
|
||||
await clearScreenDown();
|
||||
lineCount = 0;
|
||||
charsSinceNL = 0;
|
||||
}
|
||||
} else if (part.type === "text-end") {
|
||||
await moveCursor(0, -lineCount - 1);
|
||||
await clearScreenDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue