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 { streamText } from "ai";
|
||||||
|
|
||||||
import { loadConfig, saveConfig } from "./src/config";
|
import { loadConfig, saveConfig } from "./src/config";
|
||||||
import {
|
import { Conversation } from "./src/conversation";
|
||||||
type Conversation,
|
|
||||||
loadConversation,
|
|
||||||
saveConversation,
|
|
||||||
} from "./src/conversation";
|
|
||||||
import Renderer from "./src/render";
|
import Renderer from "./src/render";
|
||||||
|
|
||||||
const { values: args, positionals } = parseArgs({
|
const { values: args, positionals } = parseArgs({
|
||||||
|
|
@ -36,8 +32,6 @@ async function main() {
|
||||||
|
|
||||||
const config = await loadConfig();
|
const config = await loadConfig();
|
||||||
|
|
||||||
config.model = modelFlag ?? config.model;
|
|
||||||
|
|
||||||
if (!config.model) {
|
if (!config.model) {
|
||||||
console.error(
|
console.error(
|
||||||
"Please run 'lmc --model <model> --save' to set a default model.",
|
"Please run 'lmc --model <model> --save' to set a default model.",
|
||||||
|
|
@ -46,6 +40,7 @@ async function main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (saveConfigFlag) {
|
if (saveConfigFlag) {
|
||||||
|
config.model = modelFlag ?? config.model;
|
||||||
await saveConfig(config);
|
await saveConfig(config);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -59,17 +54,11 @@ async function main() {
|
||||||
includeUsage: true, // Include usage information in streaming responses
|
includeUsage: true, // Include usage information in streaming responses
|
||||||
});
|
});
|
||||||
|
|
||||||
let convo: Conversation = {
|
const convo = new Conversation(modelFlag ?? config.model);
|
||||||
model: config.model,
|
|
||||||
messages: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!newConvo) {
|
if (!newConvo) {
|
||||||
convo = await loadConversation(config.model);
|
await convo.load();
|
||||||
|
if (modelFlag) convo.model = modelFlag;
|
||||||
if (args.model || args.m) {
|
|
||||||
convo.model = modelFlag ?? config.model;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const prompt = positionals.slice(2).join(" ") || "Introduce yourself.";
|
const prompt = positionals.slice(2).join(" ") || "Introduce yourself.";
|
||||||
|
|
@ -85,7 +74,7 @@ async function main() {
|
||||||
const stop = Date.now();
|
const stop = Date.now();
|
||||||
|
|
||||||
convo.messages.push({ role: "assistant", content: await text });
|
convo.messages.push({ role: "assistant", content: await text });
|
||||||
await saveConversation(convo);
|
await convo.save();
|
||||||
|
|
||||||
await renderer.renderStats(totalUsage, start, stop);
|
await renderer.renderStats(totalUsage, start, stop);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,23 +2,34 @@ import os from "node:os";
|
||||||
import type { ModelMessage } from "ai";
|
import type { ModelMessage } from "ai";
|
||||||
|
|
||||||
const home = os.homedir();
|
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;
|
model: string;
|
||||||
messages: ModelMessage[];
|
messages: ModelMessage[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadConversation(
|
export class Conversation implements IConversation {
|
||||||
defaultModel: string,
|
messages: ModelMessage[] = [];
|
||||||
): Promise<Conversation> {
|
|
||||||
|
constructor(public model: string) {}
|
||||||
|
|
||||||
|
async load() {
|
||||||
try {
|
try {
|
||||||
return await convoFile.json();
|
const convo: IConversation = await convoFile.json();
|
||||||
|
this.model = convo.model;
|
||||||
|
this.messages = convo.messages;
|
||||||
} catch {
|
} catch {
|
||||||
return { model: defaultModel, messages: [] };
|
console.error("No conversation found, starting one.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function saveConversation(convo: Conversation): Promise<void> {
|
async save() {
|
||||||
await convoFile.write(JSON.stringify(convo));
|
await convoFile.write(
|
||||||
|
JSON.stringify({
|
||||||
|
model: this.model,
|
||||||
|
messages: this.messages,
|
||||||
|
} satisfies IConversation),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue