refactor(core,server): 简化配置加载,移除 workdir 参数
Provider 和 Agent 配置统一从全局目录加载,无需传递 workdir: - ProviderRegistry.init() 不再需要 workdir 参数 - AgentRegistry.init() 不再需要 workdir 参数 - 配置文件路径统一使用 ~/.ai-terminal-assistant/
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -41,7 +41,6 @@ export {
|
||||
loadProvidersConfig,
|
||||
saveProvidersConfig,
|
||||
resolveApiKey,
|
||||
getConfigDir,
|
||||
getConfigPath,
|
||||
} from './config.js';
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -156,7 +156,7 @@ interface PermissionManager {
|
||||
* Provider Registry 接口
|
||||
*/
|
||||
interface ProviderRegistryInterface {
|
||||
init(workdir?: string): Promise<void>;
|
||||
init(): Promise<void>;
|
||||
isInitialized(): boolean;
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ interface ProviderRegistryInterface {
|
||||
* Agent Registry 接口
|
||||
*/
|
||||
interface AgentRegistryInterface {
|
||||
init(workdir: string): Promise<void>;
|
||||
init(): Promise<void>;
|
||||
isInitialized(): boolean;
|
||||
}
|
||||
|
||||
@@ -221,22 +221,18 @@ export async function initCore(): Promise<boolean> {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取服务器配置的工作目录
|
||||
const config = getConfig();
|
||||
const workdir = config.workdir;
|
||||
|
||||
// 初始化 ProviderRegistry(加载用户配置)
|
||||
const providerRegistry = core.getProviderRegistry();
|
||||
if (!providerRegistry.isInitialized()) {
|
||||
await providerRegistry.init(workdir);
|
||||
console.log('[Agent] ProviderRegistry initialized with workdir:', workdir);
|
||||
await providerRegistry.init();
|
||||
console.log('[Agent] ProviderRegistry initialized');
|
||||
}
|
||||
|
||||
// 初始化 AgentRegistry(加载用户自定义 Agent 配置)
|
||||
const agentRegistry = core.agentRegistry;
|
||||
if (!agentRegistry.isInitialized()) {
|
||||
await agentRegistry.init(workdir);
|
||||
console.log('[Agent] AgentRegistry initialized with workdir:', workdir);
|
||||
await agentRegistry.init();
|
||||
console.log('[Agent] AgentRegistry initialized');
|
||||
}
|
||||
|
||||
coreModule = core;
|
||||
|
||||
@@ -74,7 +74,7 @@ interface AgentConfigFile {
|
||||
// Core Agent 模块类型
|
||||
interface AgentModule {
|
||||
agentRegistry: {
|
||||
init: (workdir: string) => Promise<void>;
|
||||
init: () => Promise<void>;
|
||||
get: (name: string) => AgentInfo | undefined;
|
||||
list: () => AgentInfo[];
|
||||
};
|
||||
@@ -152,8 +152,7 @@ async function ensureRegistryInitialized(): Promise<boolean> {
|
||||
if (!module) return false;
|
||||
|
||||
if (!initialized) {
|
||||
const config = getConfig();
|
||||
await module.agentRegistry.init(config.workdir);
|
||||
await module.agentRegistry.init();
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -247,7 +247,7 @@ providersRouter.post('/', async (c) => {
|
||||
|
||||
const registry = core.getProviderRegistry();
|
||||
registry.registerCustom(body);
|
||||
await registry.saveConfig(getConfig().workdir);
|
||||
await registry.saveConfig();
|
||||
|
||||
return c.json({
|
||||
success: true,
|
||||
@@ -285,7 +285,7 @@ providersRouter.put('/:id', async (c) => {
|
||||
}
|
||||
|
||||
registry.setConfig(id, { ...body, id });
|
||||
await registry.saveConfig(getConfig().workdir);
|
||||
await registry.saveConfig();
|
||||
|
||||
return c.json({
|
||||
success: true,
|
||||
@@ -321,7 +321,7 @@ providersRouter.delete('/:id', async (c) => {
|
||||
return c.json({ success: false, error: `Provider not found: ${id}` }, 404);
|
||||
}
|
||||
|
||||
await registry.saveConfig(getConfig().workdir);
|
||||
await registry.saveConfig();
|
||||
|
||||
return c.json({
|
||||
success: true,
|
||||
@@ -365,7 +365,7 @@ providersRouter.post('/:id/models', async (c) => {
|
||||
|
||||
const registry = core.getProviderRegistry();
|
||||
registry.addCustomModel(providerId, body);
|
||||
await registry.saveConfig(getConfig().workdir);
|
||||
await registry.saveConfig();
|
||||
|
||||
return c.json({
|
||||
success: true,
|
||||
@@ -408,7 +408,7 @@ providersRouter.delete('/:id/models/:modelId', async (c) => {
|
||||
);
|
||||
}
|
||||
|
||||
await registry.saveConfig(getConfig().workdir);
|
||||
await registry.saveConfig();
|
||||
|
||||
return c.json({
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user