refactor: conversation into a class

This commit is contained in:
mitchell 2026-05-02 02:08:13 -04:00
parent 94895aacd5
commit f5c8bdfe99
2 changed files with 30 additions and 30 deletions

View file

@ -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<Conversation> {
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<void> {
await convoFile.write(JSON.stringify(convo));
}