From f5c8bdfe994e2f655d1f07458604df288e6733ad Mon Sep 17 00:00:00 2001 From: mitchell Date: Sat, 2 May 2026 02:08:13 -0400 Subject: [PATCH] refactor: conversation into a class --- index.ts | 23 ++++++----------------- src/conversation/index.ts | 37 ++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/index.ts b/index.ts index e30be42..abe7d23 100644 --- a/index.ts +++ b/index.ts @@ -3,11 +3,7 @@ import { createOpenAICompatible } from "@ai-sdk/openai-compatible"; import { streamText } from "ai"; import { loadConfig, saveConfig } from "./src/config"; -import { - type Conversation, - loadConversation, - saveConversation, -} from "./src/conversation"; +import { Conversation } from "./src/conversation"; import Renderer from "./src/render"; const { values: args, positionals } = parseArgs({ @@ -36,8 +32,6 @@ async function main() { const config = await loadConfig(); - config.model = modelFlag ?? config.model; - if (!config.model) { console.error( "Please run 'lmc --model --save' to set a default model.", @@ -46,6 +40,7 @@ async function main() { } if (saveConfigFlag) { + config.model = modelFlag ?? config.model; await saveConfig(config); return; } @@ -59,17 +54,11 @@ async function main() { includeUsage: true, // Include usage information in streaming responses }); - let convo: Conversation = { - model: config.model, - messages: [], - }; + const convo = new Conversation(modelFlag ?? config.model); if (!newConvo) { - convo = await loadConversation(config.model); - - if (args.model || args.m) { - convo.model = modelFlag ?? config.model; - } + await convo.load(); + if (modelFlag) convo.model = modelFlag; } const prompt = positionals.slice(2).join(" ") || "Introduce yourself."; @@ -85,7 +74,7 @@ async function main() { const stop = Date.now(); convo.messages.push({ role: "assistant", content: await text }); - await saveConversation(convo); + await convo.save(); await renderer.renderStats(totalUsage, start, stop); } diff --git a/src/conversation/index.ts b/src/conversation/index.ts index 557e6b5..9fb01f4 100644 --- a/src/conversation/index.ts +++ b/src/conversation/index.ts @@ -2,23 +2,34 @@ import os from "node:os"; import type { ModelMessage } from "ai"; const home = os.homedir(); -export const convoFile = Bun.file(`${home}/.lmc_convo.json`); +const convoFile = Bun.file(`${home}/.lmc_convo.json`); -export interface Conversation { +export interface IConversation { model: string; messages: ModelMessage[]; } -export async function loadConversation( - defaultModel: string, -): Promise { - try { - return await convoFile.json(); - } catch { - return { model: defaultModel, messages: [] }; +export class Conversation implements IConversation { + messages: ModelMessage[] = []; + + constructor(public model: string) {} + + async load() { + try { + const convo: IConversation = await convoFile.json(); + this.model = convo.model; + this.messages = convo.messages; + } catch { + console.error("No conversation found, starting one."); + } + } + + async save() { + await convoFile.write( + JSON.stringify({ + model: this.model, + messages: this.messages, + } satisfies IConversation), + ); } } - -export async function saveConversation(convo: Conversation): Promise { - await convoFile.write(JSON.stringify(convo)); -}