feat: initial project setup with gai tool

This commit is contained in:
2026-06-09 16:41:02 +08:00
commit a058881b53
17 changed files with 952 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
import { existsSync, mkdirSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import type { Config } from "./types";
const DEFAULT_CONFIG: Config = {
apiKey: "",
apiBase: "https://api.openai.com/v1",
model: "gpt-4o",
maxTokens: 500,
temperature: 0.7,
};
const CONFIG_DIR = join(homedir(), ".config", "gai");
const CONFIG_PATH = join(CONFIG_DIR, "config.json");
export async function loadConfig(): Promise<Config> {
let config = { ...DEFAULT_CONFIG };
if (existsSync(CONFIG_PATH)) {
try {
const file = Bun.file(CONFIG_PATH);
const json = (await file.json()) as Partial<Config>;
config = { ...config, ...json };
} catch {}
}
if (process.env.GAI_API_KEY) config.apiKey = process.env.GAI_API_KEY;
if (process.env.GAI_API_BASE) config.apiBase = process.env.GAI_API_BASE;
if (process.env.GAI_MODEL) config.model = process.env.GAI_MODEL;
if (process.env.GAI_MAX_TOKENS)
config.maxTokens = parseInt(process.env.GAI_MAX_TOKENS);
if (process.env.GAI_TEMPERATURE)
config.temperature = parseFloat(process.env.GAI_TEMPERATURE);
return config;
}
export async function saveConfig(partial: Partial<Config>): Promise<void> {
if (!existsSync(CONFIG_DIR)) {
mkdirSync(CONFIG_DIR, { recursive: true });
}
const current = await loadConfig();
const merged = { ...current, ...partial };
await Bun.write(CONFIG_PATH, JSON.stringify(merged, null, 2));
}