release v1.0.27 to fix some bugs and enhance tools
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import fs from "node:fs/promises";
|
||||
import readline from "node:readline";
|
||||
import JSON5 from "json5";
|
||||
import {
|
||||
CONFIG_FILE,
|
||||
DEFAULT_CONFIG,
|
||||
@@ -45,33 +46,50 @@ const confirm = async (query: string): Promise<boolean> => {
|
||||
export const readConfigFile = async () => {
|
||||
try {
|
||||
const config = await fs.readFile(CONFIG_FILE, "utf-8");
|
||||
return JSON.parse(config);
|
||||
} catch {
|
||||
const name = await question("Enter Provider Name: ");
|
||||
const APIKEY = await question("Enter Provider API KEY: ");
|
||||
const baseUrl = await question("Enter Provider URL: ");
|
||||
const model = await question("Enter MODEL Name: ");
|
||||
const config = Object.assign({}, DEFAULT_CONFIG, {
|
||||
Providers: [
|
||||
{
|
||||
name,
|
||||
api_base_url: baseUrl,
|
||||
api_key: APIKEY,
|
||||
models: [model],
|
||||
try {
|
||||
// Try to parse with JSON5 first (which also supports standard JSON)
|
||||
return JSON5.parse(config);
|
||||
} catch (parseError) {
|
||||
console.error(`Failed to parse config file at ${CONFIG_FILE}`);
|
||||
console.error("Error details:", (parseError as Error).message);
|
||||
console.error("Please check your config file syntax.");
|
||||
process.exit(1);
|
||||
}
|
||||
} catch (readError: any) {
|
||||
if (readError.code === "ENOENT") {
|
||||
// Config file doesn't exist, prompt user for initial setup
|
||||
const name = await question("Enter Provider Name: ");
|
||||
const APIKEY = await question("Enter Provider API KEY: ");
|
||||
const baseUrl = await question("Enter Provider URL: ");
|
||||
const model = await question("Enter MODEL Name: ");
|
||||
const config = Object.assign({}, DEFAULT_CONFIG, {
|
||||
Providers: [
|
||||
{
|
||||
name,
|
||||
api_base_url: baseUrl,
|
||||
api_key: APIKEY,
|
||||
models: [model],
|
||||
},
|
||||
],
|
||||
Router: {
|
||||
default: `${name},${model}`,
|
||||
},
|
||||
],
|
||||
Router: {
|
||||
default: `${name},${model}`,
|
||||
},
|
||||
});
|
||||
await writeConfigFile(config);
|
||||
return config;
|
||||
});
|
||||
await writeConfigFile(config);
|
||||
return config;
|
||||
} else {
|
||||
console.error(`Failed to read config file at ${CONFIG_FILE}`);
|
||||
console.error("Error details:", readError.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const writeConfigFile = async (config: any) => {
|
||||
await ensureDir(HOME_DIR);
|
||||
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));
|
||||
// Add a comment to indicate JSON5 support
|
||||
const configWithComment = `// This config file supports JSON5 format (comments, trailing commas, etc.)\n${JSON5.stringify(config, null, 2)}`;
|
||||
await fs.writeFile(CONFIG_FILE, configWithComment);
|
||||
};
|
||||
|
||||
export const initConfig = async () => {
|
||||
|
||||
Reference in New Issue
Block a user