50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import os from "node:os";
|
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
import { type ModelMessage, streamText } from "ai";
|
|
|
|
import { renderStream } from "./src/render";
|
|
|
|
const home = os.homedir();
|
|
const convoFile = Bun.file(`${home}/.aicl_convo.json`);
|
|
const reasoningFile = Bun.file(`${home}/.aicl_reasoning.md`);
|
|
const responsePath = `${home}/.aicl_response.md`;
|
|
const responseFile = Bun.file(responsePath);
|
|
|
|
const provider = createOpenAICompatible({
|
|
name: "llama.cpp",
|
|
apiKey: "local",
|
|
baseURL: "http://127.0.0.1:8080/v1",
|
|
includeUsage: true, // Include usage information in streaming responses
|
|
});
|
|
|
|
const messages: ModelMessage[] = [];
|
|
|
|
try {
|
|
const convo = await convoFile.json();
|
|
messages.push(...convo);
|
|
} catch {}
|
|
|
|
const prompt = Bun.argv[2] ?? "Introduce yourself.";
|
|
messages.push({ role: "user", content: prompt });
|
|
|
|
const { fullStream, reasoningText, text } = streamText({
|
|
model: provider("qwen3.5-35b-a3b"),
|
|
messages,
|
|
});
|
|
|
|
renderStream(fullStream);
|
|
|
|
const reasoning = await reasoningText;
|
|
await reasoningFile.write(reasoning ?? "");
|
|
|
|
const response = await text;
|
|
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"],
|
|
});
|