feat: add args parsing; refactor scripts

This commit is contained in:
mitchell 2026-04-16 16:04:08 -04:00
parent b3cef4dd03
commit bcd9771cd5
3 changed files with 59 additions and 41 deletions

View file

@ -1,58 +1,78 @@
import os from "node:os"; import os from "node:os";
import { parseArgs } from "node:util";
import { createOpenAICompatible } from "@ai-sdk/openai-compatible"; import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { type ModelMessage, streamText } from "ai"; import { type ModelMessage, streamText } from "ai";
import { renderStream } from "./src/render"; import { renderStream } from "./src/render";
const home = os.homedir(); const home = os.homedir();
const convoFile = Bun.file(`${home}/.aicl_convo.json`); const convoFile = Bun.file(`${home}/.aicl_convo.json`);
const provider = createOpenAICompatible({ const { values: args, positionals } = parseArgs({
args: Bun.argv,
options: {
model: {
type: "string",
},
new: {
type: "boolean",
},
},
strict: true,
allowPositionals: true,
});
async function main() {
const provider = createOpenAICompatible({
name: "llama.cpp", name: "llama.cpp",
apiKey: "local", apiKey: "local",
baseURL: "http://127.0.0.1:8080/v1", baseURL: "http://127.0.0.1:8080/v1",
includeUsage: true, // Include usage information in streaming responses includeUsage: true, // Include usage information in streaming responses
}); });
const messages: ModelMessage[] = []; const messages: ModelMessage[] = [];
try { if (!args.new) {
try {
const convo = await convoFile.json(); const convo = await convoFile.json();
messages.push(...convo); messages.push(...convo);
} catch {} } catch {}
}
const prompt = Bun.argv[2] ?? "Introduce yourself."; const prompt = positionals[2] ?? "Introduce yourself.";
messages.push({ role: "user", content: prompt }); messages.push({ role: "user", content: prompt });
const { fullStream, textStream, text, totalUsage } = streamText({ const { fullStream, textStream, text, totalUsage } = streamText({
model: provider("gemma4-26b-a4b"), model: provider(args.model ?? "gemma4-26b-a4b"),
messages, messages,
}); });
const start = Date.now(); const start = Date.now();
const streamRender = renderStream(fullStream); const streamRender = renderStream(fullStream);
const [width] = process.stdout.getWindowSize(); const [width] = process.stdout.getWindowSize();
const glow = Bun.spawn({ const glow = Bun.spawn({
cmd: ["glow", "-w", `${width}`, "-"], cmd: ["glow", "-w", `${width}`, "-"],
stdin: textStream, stdin: textStream,
stdout: "inherit", stdout: "inherit",
}); });
await streamRender; await streamRender;
const stop = Date.now(); const stop = Date.now();
await glow.exited; await glow.exited;
messages.push({ role: "assistant", content: await text }); messages.push({ role: "assistant", content: await text });
await convoFile.write(JSON.stringify(messages)); await convoFile.write(JSON.stringify(messages));
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(
`\u001b[2mContext: ${context.toFixed(1)}k, Output: ${output.toFixed(1)}k, Time: ${time.toFixed(2)}s\u001b[22m\n`, `\u001b[2mContext: ${context.toFixed(1)}k, Output: ${output.toFixed(1)}k, Time: ${time.toFixed(2)}s\u001b[22m\n`,
); );
}
await main();

View file

@ -10,7 +10,7 @@
"dev": "bun run index.ts", "dev": "bun run index.ts",
"prod": "bun run out/index.js", "prod": "bun run out/index.js",
"check": "bunx --bun @biomejs/biome check --write", "check": "bunx --bun @biomejs/biome check --write",
"build": "bun run check && bun run build:bundle && bun run shebang", "build:js": "bun run check && bun run build:bundle && bun run shebang",
"build:bundle": "bun build --target=bun --production --sourcemap --outdir=out index.ts", "build:bundle": "bun build --target=bun --production --sourcemap --outdir=out index.ts",
"build:bin": "bun build --compile --outfile=out/aicl index.ts", "build:bin": "bun build --compile --outfile=out/aicl index.ts",
"shebang": "sed -i '1i#!\\/usr\\/bin\\/env bun' out/index.js" "shebang": "sed -i '1i#!\\/usr\\/bin\\/env bun' out/index.js"

View file

@ -59,9 +59,7 @@ export async function renderStream(
resetCounts(); resetCounts();
} }
while (!memOk) { while (!memOk) await Bun.sleep(10);
await new Promise((resolve) => setTimeout(resolve, 10));
}
} }
} }