diff --git a/index.ts b/index.ts index 484f9c6..b574bc2 100644 --- a/index.ts +++ b/index.ts @@ -1,9 +1,13 @@ -import os from "node:os"; import { parseArgs } from "node:util"; import { createOpenAICompatible } from "@ai-sdk/openai-compatible"; -import { type ModelMessage, streamText } from "ai"; -import chalk from "chalk"; +import { streamText } from "ai"; +import { loadConfig, saveConfig } from "./src/config"; +import { + type Conversation, + loadConversation, + saveConversation, +} from "./src/conversation"; import Renderer from "./src/render"; const { values: args, positionals } = parseArgs({ @@ -23,23 +27,13 @@ const { values: args, positionals } = parseArgs({ allowPositionals: true, }); -const home = os.homedir(); -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; const plainOut = args.plainOut ?? args.plain ?? !process.stdout.isTTY; const plainErr = args.plainErr ?? args.plain ?? !process.stderr.isTTY; - let saveConfig = args.save ?? args.s; + const saveConfigFlag = args.save ?? args.s; - let config: { model?: string }; - try { - config = await configFile.json(); - } catch { - config = {}; - saveConfig = true; - } + const { config } = await loadConfig(); config.model = args.model ?? args.m ?? config.model; @@ -48,9 +42,8 @@ async function main() { process.exit(1); } - if (saveConfig) { - await configFile.write(JSON.stringify(config, null, " ")); - console.log(chalk.green("Configuration saved!")); + if (saveConfigFlag) { + await saveConfig(config); return; } @@ -63,15 +56,13 @@ async function main() { includeUsage: true, // Include usage information in streaming responses }); - let convo: { model: string; messages: ModelMessage[] } = { + let convo: Conversation = { model: config.model, messages: [], }; if (!newConvo) { - try { - convo = await convoFile.json(); - } catch {} + convo = await loadConversation(config.model); if (args.model || args.m) { convo.model = args.model ?? args.m ?? config.model; @@ -91,7 +82,7 @@ async function main() { const stop = Date.now(); convo.messages.push({ role: "assistant", content: await text }); - await convoFile.write(JSON.stringify(convo)); + await saveConversation(convo); await renderer.renderStats(totalUsage, start, stop); } diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 0000000..e7188b6 --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,26 @@ +import os from "node:os"; +import chalk from "chalk"; + +const home = os.homedir(); +const configFile = Bun.file(`${home}/.aicl_config.json`); + +export interface AppConfig { + model?: string; +} + +export async function loadConfig(): Promise<{ + config: AppConfig; +}> { + let config: AppConfig; + try { + config = await configFile.json(); + } catch { + config = {}; + } + return { config }; +} + +export async function saveConfig(config: AppConfig): Promise { + await configFile.write(JSON.stringify(config, null, " ")); + console.log(chalk.green("Configuration saved!")); +} diff --git a/src/conversation/index.ts b/src/conversation/index.ts new file mode 100644 index 0000000..ee2f670 --- /dev/null +++ b/src/conversation/index.ts @@ -0,0 +1,24 @@ +import os from "node:os"; +import type { ModelMessage } from "ai"; + +const home = os.homedir(); +export const convoFile = Bun.file(`${home}/.aicl_convo.json`); + +export interface Conversation { + model: string; + messages: ModelMessage[]; +} + +export async function loadConversation( + defaultModel: string, +): Promise { + try { + return await convoFile.json(); + } catch { + return { model: defaultModel, messages: [] }; + } +} + +export async function saveConversation(convo: Conversation): Promise { + await convoFile.write(JSON.stringify(convo)); +}