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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue