feat: add --save flag; refactor conversation handling

This commit is contained in:
mitchell 2026-04-16 22:31:03 -04:00
parent 95bec63a59
commit 4bbe1a38d4

View file

@ -12,6 +12,8 @@ const { values: args, positionals } = parseArgs({
m: { type: "string" }, m: { type: "string" },
new: { type: "boolean" }, new: { type: "boolean" },
n: { type: "boolean" }, n: { type: "boolean" },
save: { type: "boolean" },
s: { type: "boolean" },
}, },
strict: true, strict: true,
allowPositionals: true, allowPositionals: true,
@ -22,22 +24,27 @@ const convoFile = Bun.file(`${home}/.aicl_convo.json`);
const configFile = Bun.file(`${home}/.aicl_config.json`); const configFile = Bun.file(`${home}/.aicl_config.json`);
async function main() { async function main() {
const newConvo = args.new ?? args.n;
let saveConfig = args.save ?? args.s;
let config: { model?: string }; let config: { model?: string };
try { try {
config = await configFile.json(); config = await configFile.json();
} catch { } catch {
config = {}; config = {};
saveConfig = true;
} }
config.model = args.model ?? args.m ?? config.model; config.model = args.model ?? args.m ?? config.model;
const newConvo = args.new ?? args.n;
if (!config.model) { if (!config.model) {
console.error("Please specify a model with --model/-m at least one time."); console.error("Please specify a model with --model/-m at least one time.");
process.exit(1); process.exit(1);
} }
await configFile.write(JSON.stringify(config, null, " ")); if (saveConfig) {
await configFile.write(JSON.stringify(config, null, " "));
}
const provider = createOpenAICompatible({ const provider = createOpenAICompatible({
name: "llama.cpp", name: "llama.cpp",
@ -46,21 +53,27 @@ async function main() {
includeUsage: true, // Include usage information in streaming responses includeUsage: true, // Include usage information in streaming responses
}); });
const messages: ModelMessage[] = []; let convo: { model: string; messages: ModelMessage[] } = {
model: config.model,
messages: [],
};
if (!newConvo) { if (!newConvo) {
try { try {
const convo = await convoFile.json(); convo = await convoFile.json();
messages.push(...convo);
} catch {} } catch {}
if (args.model || args.m) {
convo.model = args.model ?? args.m ?? config.model;
}
} }
const prompt = positionals[2] ?? "Introduce yourself."; const prompt = positionals[2] ?? "Introduce yourself.";
messages.push({ role: "user", content: prompt }); convo.messages.push({ role: "user", content: prompt });
const { fullStream, textStream, text, totalUsage } = streamText({ const { fullStream, textStream, text, totalUsage } = streamText({
model: provider(config.model), model: provider(convo.model),
messages, messages: convo.messages,
}); });
const start = Date.now(); const start = Date.now();
@ -80,8 +93,8 @@ async function main() {
await glow.exited; await glow.exited;
messages.push({ role: "assistant", content: await text }); convo.messages.push({ role: "assistant", content: await text });
await convoFile.write(JSON.stringify(messages)); await convoFile.write(JSON.stringify(convo));
const { inputTokens, outputTokens } = await totalUsage; const { inputTokens, outputTokens } = await totalUsage;
const context = (inputTokens ?? 0) / 1000; const context = (inputTokens ?? 0) / 1000;