refactor(core,server): 简化配置加载,移除 workdir 参数

Provider 和 Agent 配置统一从全局目录加载,无需传递 workdir:
- ProviderRegistry.init() 不再需要 workdir 参数
- AgentRegistry.init() 不再需要 workdir 参数
- 配置文件路径统一使用 ~/.ai-terminal-assistant/
This commit is contained in:
2025-12-16 10:23:28 +08:00
parent 6a28c98789
commit a6c1e792fa
7 changed files with 32 additions and 55 deletions
+3 -3
View File
@@ -31,11 +31,11 @@ export class AgentRegistry {
* 完整初始化 - 加载用户配置
* 用户配置可以覆盖预设 Agent 的设置
*/
async init(workdir: string): Promise<void> {
async init(): Promise<void> {
if (this.initialized) return;
// 加载用户配置
this.userConfig = await loadAgentConfig(workdir);
// 加载用户配置(从 ~/.ai-terminal-assistant/agents.json
this.userConfig = await loadAgentConfig('');
if (this.userConfig) {
this.globalConfig = this.userConfig.defaults ?? null;
+9 -21
View File
@@ -7,34 +7,25 @@
import { existsSync } from 'node:fs';
import { readFile, writeFile, mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { homedir } from 'node:os';
import type { ProvidersConfigFile, CustomProviderDefinition, ProviderConfig } from './types.js';
/** 配置目录名 */
const CONFIG_DIR = '.ai-terminal-assistant';
import { getConfigDir as getGlobalConfigDir } from '../constants/paths.js';
/** 配置文件名 */
const CONFIG_FILE = 'providers.json';
/**
* 获取配置目录路径
*/
export function getConfigDir(workdir?: string): string {
return workdir ? join(workdir, CONFIG_DIR) : join(homedir(), CONFIG_DIR);
}
/**
* 获取配置文件路径
* 始终使用全局配置目录 ~/.ai-terminal-assistant/
*/
export function getConfigPath(workdir?: string): string {
return join(getConfigDir(workdir), CONFIG_FILE);
export function getConfigPath(): string {
return join(getGlobalConfigDir(), CONFIG_FILE);
}
/**
* 加载提供商配置
*/
export async function loadProvidersConfig(workdir?: string): Promise<ProvidersConfigFile> {
const configPath = getConfigPath(workdir);
export async function loadProvidersConfig(): Promise<ProvidersConfigFile> {
const configPath = getConfigPath();
if (!existsSync(configPath)) {
return { providers: {}, configs: {} };
@@ -56,12 +47,9 @@ export async function loadProvidersConfig(workdir?: string): Promise<ProvidersCo
/**
* 保存提供商配置
*/
export async function saveProvidersConfig(
config: ProvidersConfigFile,
workdir?: string
): Promise<void> {
const configDir = getConfigDir(workdir);
const configPath = getConfigPath(workdir);
export async function saveProvidersConfig(config: ProvidersConfigFile): Promise<void> {
const configDir = getGlobalConfigDir();
const configPath = getConfigPath();
// 确保目录存在
if (!existsSync(configDir)) {
-1
View File
@@ -41,7 +41,6 @@ export {
loadProvidersConfig,
saveProvidersConfig,
resolveApiKey,
getConfigDir,
getConfigPath,
} from './config.js';
+7 -12
View File
@@ -46,9 +46,6 @@ export class ProviderRegistry {
/** 是否已完全初始化(包括用户配置) */
private fullyInitialized = false;
/** 工作目录 */
private workdir?: string;
constructor() {
// 同步初始化内置提供商(不需要异步加载)
this.initBuiltinProviders();
@@ -70,13 +67,11 @@ export class ProviderRegistry {
* 完整初始化 Registry
* 加载用户配置(自定义提供商和配置)
*/
async init(workdir?: string): Promise<void> {
async init(): Promise<void> {
if (this.fullyInitialized) return;
this.workdir = workdir;
// 加载用户配置
const config = await loadProvidersConfig(workdir);
// 加载用户配置(从 ~/.ai-terminal-assistant/providers.json
const config = await loadProvidersConfig();
// 加载自定义提供商
if (config.providers) {
@@ -414,7 +409,7 @@ export class ProviderRegistry {
/**
* 保存配置到文件
*/
async saveConfig(workdir?: string): Promise<void> {
async saveConfig(): Promise<void> {
this.ensureInitialized();
const config = {
@@ -422,13 +417,13 @@ export class ProviderRegistry {
configs: Object.fromEntries(this.configs),
};
await saveProvidersConfig(config, workdir ?? this.workdir);
await saveProvidersConfig(config);
}
/**
* 重新加载配置
*/
async reloadConfig(workdir?: string): Promise<void> {
async reloadConfig(): Promise<void> {
this.fullyInitialized = false;
this.configs.clear();
this.customDefinitions.clear();
@@ -440,7 +435,7 @@ export class ProviderRegistry {
}
}
await this.init(workdir ?? this.workdir);
await this.init();
}
}