feat: add interactive config setup and baseURL support
This commit is contained in:
parent
d9bffdec2b
commit
3c9a5e625a
4 changed files with 60 additions and 25 deletions
|
|
@ -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!"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue