refactor: conversation into a class
This commit is contained in:
parent
94895aacd5
commit
f5c8bdfe99
2 changed files with 30 additions and 30 deletions
23
index.ts
23
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 <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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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> {
|
||||
export class Conversation implements IConversation {
|
||||
messages: ModelMessage[] = [];
|
||||
|
||||
constructor(public model: string) {}
|
||||
|
||||
async load() {
|
||||
try {
|
||||
return await convoFile.json();
|
||||
const convo: IConversation = await convoFile.json();
|
||||
this.model = convo.model;
|
||||
this.messages = convo.messages;
|
||||
} catch {
|
||||
return { model: defaultModel, messages: [] };
|
||||
console.error("No conversation found, starting one.");
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveConversation(convo: Conversation): Promise<void> {
|
||||
await convoFile.write(JSON.stringify(convo));
|
||||
async save() {
|
||||
await convoFile.write(
|
||||
JSON.stringify({
|
||||
model: this.model,
|
||||
messages: this.messages,
|
||||
} satisfies IConversation),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue