refactor: extract config and conversation handling into separate modules
This commit is contained in:
parent
f50280364d
commit
6dc55bc3f3
3 changed files with 64 additions and 23 deletions
37
index.ts
37
index.ts
|
|
@ -1,9 +1,13 @@
|
||||||
import os from "node:os";
|
|
||||||
import { parseArgs } from "node:util";
|
import { parseArgs } from "node:util";
|
||||||
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
||||||
import { type ModelMessage, streamText } from "ai";
|
import { streamText } from "ai";
|
||||||
import chalk from "chalk";
|
|
||||||
|
|
||||||
|
import { loadConfig, saveConfig } from "./src/config";
|
||||||
|
import {
|
||||||
|
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({
|
||||||
|
|
@ -23,23 +27,13 @@ const { values: args, positionals } = parseArgs({
|
||||||
allowPositionals: true,
|
allowPositionals: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const home = os.homedir();
|
|
||||||
const convoFile = Bun.file(`${home}/.aicl_convo.json`);
|
|
||||||
const configFile = Bun.file(`${home}/.aicl_config.json`);
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const newConvo = args.new ?? args.n;
|
const newConvo = args.new ?? args.n;
|
||||||
const plainOut = args.plainOut ?? args.plain ?? !process.stdout.isTTY;
|
const plainOut = args.plainOut ?? args.plain ?? !process.stdout.isTTY;
|
||||||
const plainErr = args.plainErr ?? args.plain ?? !process.stderr.isTTY;
|
const plainErr = args.plainErr ?? args.plain ?? !process.stderr.isTTY;
|
||||||
let saveConfig = args.save ?? args.s;
|
const saveConfigFlag = args.save ?? args.s;
|
||||||
|
|
||||||
let config: { model?: string };
|
const { config } = await loadConfig();
|
||||||
try {
|
|
||||||
config = await configFile.json();
|
|
||||||
} catch {
|
|
||||||
config = {};
|
|
||||||
saveConfig = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
config.model = args.model ?? args.m ?? config.model;
|
config.model = args.model ?? args.m ?? config.model;
|
||||||
|
|
||||||
|
|
@ -48,9 +42,8 @@ async function main() {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (saveConfig) {
|
if (saveConfigFlag) {
|
||||||
await configFile.write(JSON.stringify(config, null, " "));
|
await saveConfig(config);
|
||||||
console.log(chalk.green("Configuration saved!"));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,15 +56,13 @@ async function main() {
|
||||||
includeUsage: true, // Include usage information in streaming responses
|
includeUsage: true, // Include usage information in streaming responses
|
||||||
});
|
});
|
||||||
|
|
||||||
let convo: { model: string; messages: ModelMessage[] } = {
|
let convo: Conversation = {
|
||||||
model: config.model,
|
model: config.model,
|
||||||
messages: [],
|
messages: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!newConvo) {
|
if (!newConvo) {
|
||||||
try {
|
convo = await loadConversation(config.model);
|
||||||
convo = await convoFile.json();
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
if (args.model || args.m) {
|
if (args.model || args.m) {
|
||||||
convo.model = args.model ?? args.m ?? config.model;
|
convo.model = args.model ?? args.m ?? config.model;
|
||||||
|
|
@ -91,7 +82,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 convoFile.write(JSON.stringify(convo));
|
await saveConversation(convo);
|
||||||
|
|
||||||
await renderer.renderStats(totalUsage, start, stop);
|
await renderer.renderStats(totalUsage, start, stop);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
src/config/index.ts
Normal file
26
src/config/index.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
import os from "node:os";
|
||||||
|
import chalk from "chalk";
|
||||||
|
|
||||||
|
const home = os.homedir();
|
||||||
|
const configFile = Bun.file(`${home}/.aicl_config.json`);
|
||||||
|
|
||||||
|
export interface AppConfig {
|
||||||
|
model?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadConfig(): Promise<{
|
||||||
|
config: AppConfig;
|
||||||
|
}> {
|
||||||
|
let config: AppConfig;
|
||||||
|
try {
|
||||||
|
config = await configFile.json();
|
||||||
|
} catch {
|
||||||
|
config = {};
|
||||||
|
}
|
||||||
|
return { config };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveConfig(config: AppConfig): Promise<void> {
|
||||||
|
await configFile.write(JSON.stringify(config, null, " "));
|
||||||
|
console.log(chalk.green("Configuration saved!"));
|
||||||
|
}
|
||||||
24
src/conversation/index.ts
Normal file
24
src/conversation/index.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import os from "node:os";
|
||||||
|
import type { ModelMessage } from "ai";
|
||||||
|
|
||||||
|
const home = os.homedir();
|
||||||
|
export const convoFile = Bun.file(`${home}/.aicl_convo.json`);
|
||||||
|
|
||||||
|
export interface Conversation {
|
||||||
|
model: string;
|
||||||
|
messages: ModelMessage[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadConversation(
|
||||||
|
defaultModel: string,
|
||||||
|
): Promise<Conversation> {
|
||||||
|
try {
|
||||||
|
return await convoFile.json();
|
||||||
|
} catch {
|
||||||
|
return { model: defaultModel, messages: [] };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveConversation(convo: Conversation): Promise<void> {
|
||||||
|
await convoFile.write(JSON.stringify(convo));
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue