feat: add interactive config setup and baseURL support

This commit is contained in:
mitchell 2026-05-03 21:10:39 -04:00
parent d9bffdec2b
commit 3c9a5e625a
4 changed files with 60 additions and 25 deletions

View file

@ -1,11 +1,55 @@
import os from "node:os";
import { autocomplete, intro, isCancel, outro, text } from "@clack/prompts";
import chalk from "chalk";
const home = os.homedir();
const configFile = Bun.file(`${home}/.lmc_config.json`);
export interface AppConfig {
model?: string;
baseURL: string;
model: string;
}
export async function initConfig(old?: AppConfig): Promise<AppConfig> {
intro(chalk.blue("Welcome to LMC. Let's make a new config!"));
const baseURL = await text({
message: "What's your base URL:",
initialValue: old?.baseURL,
defaultValue: "http://127.0.0.1:8080/v1",
placeholder: "http://127.0.0.1:8080/v1",
});
if (isCancel(baseURL)) process.exit(1);
const models = await getModels(baseURL);
const model = await autocomplete({
message: "Choose a default model:",
initialValue: old?.model,
placeholder: old?.model,
options: models,
});
if (isCancel(model)) process.exit(1);
const config = { baseURL, model } satisfies AppConfig;
await configFile.write(JSON.stringify(config, null, " "));
outro(chalk.green("Configuration saved!"));
return config;
}
type Models = {
data: {
created: number;
id: string;
object: "model";
owned_by: string;
}[];
};
async function getModels(url: string) {
const response = await fetch(`${url}/models`);
const models = (await response.json()) as Models;
return models.data.map((model) => ({ value: model.id }));
}
export async function loadConfig(): Promise<AppConfig> {
@ -13,12 +57,7 @@ export async function loadConfig(): Promise<AppConfig> {
try {
config = await configFile.json();
} catch {
config = {};
config = await initConfig();
}
return config;
}
export async function saveConfig(config: AppConfig): Promise<void> {
await configFile.write(JSON.stringify(config, null, " "));
console.log(chalk.green("Configuration saved!"));
}

View file

@ -130,6 +130,9 @@ export default class Renderer {
break;
}
case "text-start": {
if (!this.isPlain) {
spin.clear();
}
this.write("\n");
break;
}

View file

@ -112,5 +112,6 @@ export async function renderMarkdown(
html: () => "",
});
return rendered.replace(/\n\n$/, "\n");
const replaced = rendered.replace(/\n\n$/, "\n");
return Bun.wrapAnsi(replaced, columns, { trim: false });
}