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