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 的设置
|
* 用户配置可以覆盖预设 Agent 的设置
|
||||||
*/
|
*/
|
||||||
async init(workdir: string): Promise<void> {
|
async init(): Promise<void> {
|
||||||
if (this.initialized) return;
|
if (this.initialized) return;
|
||||||
|
|
||||||
// 加载用户配置
|
// 加载用户配置(从 ~/.ai-terminal-assistant/agents.json)
|
||||||
this.userConfig = await loadAgentConfig(workdir);
|
this.userConfig = await loadAgentConfig('');
|
||||||
if (this.userConfig) {
|
if (this.userConfig) {
|
||||||
this.globalConfig = this.userConfig.defaults ?? null;
|
this.globalConfig = this.userConfig.defaults ?? null;
|
||||||
|
|
||||||
|
|||||||
@@ -7,34 +7,25 @@
|
|||||||
import { existsSync } from 'node:fs';
|
import { existsSync } from 'node:fs';
|
||||||
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { homedir } from 'node:os';
|
|
||||||
import type { ProvidersConfigFile, CustomProviderDefinition, ProviderConfig } from './types.js';
|
import type { ProvidersConfigFile, CustomProviderDefinition, ProviderConfig } from './types.js';
|
||||||
|
import { getConfigDir as getGlobalConfigDir } from '../constants/paths.js';
|
||||||
/** 配置目录名 */
|
|
||||||
const CONFIG_DIR = '.ai-terminal-assistant';
|
|
||||||
|
|
||||||
/** 配置文件名 */
|
/** 配置文件名 */
|
||||||
const CONFIG_FILE = 'providers.json';
|
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 {
|
export function getConfigPath(): string {
|
||||||
return join(getConfigDir(workdir), CONFIG_FILE);
|
return join(getGlobalConfigDir(), CONFIG_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载提供商配置
|
* 加载提供商配置
|
||||||
*/
|
*/
|
||||||
export async function loadProvidersConfig(workdir?: string): Promise<ProvidersConfigFile> {
|
export async function loadProvidersConfig(): Promise<ProvidersConfigFile> {
|
||||||
const configPath = getConfigPath(workdir);
|
const configPath = getConfigPath();
|
||||||
|
|
||||||
if (!existsSync(configPath)) {
|
if (!existsSync(configPath)) {
|
||||||
return { providers: {}, configs: {} };
|
return { providers: {}, configs: {} };
|
||||||
@@ -56,12 +47,9 @@ export async function loadProvidersConfig(workdir?: string): Promise<ProvidersCo
|
|||||||
/**
|
/**
|
||||||
* 保存提供商配置
|
* 保存提供商配置
|
||||||
*/
|
*/
|
||||||
export async function saveProvidersConfig(
|
export async function saveProvidersConfig(config: ProvidersConfigFile): Promise<void> {
|
||||||
config: ProvidersConfigFile,
|
const configDir = getGlobalConfigDir();
|
||||||
workdir?: string
|
const configPath = getConfigPath();
|
||||||
): Promise<void> {
|
|
||||||
const configDir = getConfigDir(workdir);
|
|
||||||
const configPath = getConfigPath(workdir);
|
|
||||||
|
|
||||||
// 确保目录存在
|
// 确保目录存在
|
||||||
if (!existsSync(configDir)) {
|
if (!existsSync(configDir)) {
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ export {
|
|||||||
loadProvidersConfig,
|
loadProvidersConfig,
|
||||||
saveProvidersConfig,
|
saveProvidersConfig,
|
||||||
resolveApiKey,
|
resolveApiKey,
|
||||||
getConfigDir,
|
|
||||||
getConfigPath,
|
getConfigPath,
|
||||||
} from './config.js';
|
} from './config.js';
|
||||||
|
|
||||||
|
|||||||
@@ -46,9 +46,6 @@ export class ProviderRegistry {
|
|||||||
/** 是否已完全初始化(包括用户配置) */
|
/** 是否已完全初始化(包括用户配置) */
|
||||||
private fullyInitialized = false;
|
private fullyInitialized = false;
|
||||||
|
|
||||||
/** 工作目录 */
|
|
||||||
private workdir?: string;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// 同步初始化内置提供商(不需要异步加载)
|
// 同步初始化内置提供商(不需要异步加载)
|
||||||
this.initBuiltinProviders();
|
this.initBuiltinProviders();
|
||||||
@@ -70,13 +67,11 @@ export class ProviderRegistry {
|
|||||||
* 完整初始化 Registry
|
* 完整初始化 Registry
|
||||||
* 加载用户配置(自定义提供商和配置)
|
* 加载用户配置(自定义提供商和配置)
|
||||||
*/
|
*/
|
||||||
async init(workdir?: string): Promise<void> {
|
async init(): Promise<void> {
|
||||||
if (this.fullyInitialized) return;
|
if (this.fullyInitialized) return;
|
||||||
|
|
||||||
this.workdir = workdir;
|
// 加载用户配置(从 ~/.ai-terminal-assistant/providers.json)
|
||||||
|
const config = await loadProvidersConfig();
|
||||||
// 加载用户配置
|
|
||||||
const config = await loadProvidersConfig(workdir);
|
|
||||||
|
|
||||||
// 加载自定义提供商
|
// 加载自定义提供商
|
||||||
if (config.providers) {
|
if (config.providers) {
|
||||||
@@ -414,7 +409,7 @@ export class ProviderRegistry {
|
|||||||
/**
|
/**
|
||||||
* 保存配置到文件
|
* 保存配置到文件
|
||||||
*/
|
*/
|
||||||
async saveConfig(workdir?: string): Promise<void> {
|
async saveConfig(): Promise<void> {
|
||||||
this.ensureInitialized();
|
this.ensureInitialized();
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
@@ -422,13 +417,13 @@ export class ProviderRegistry {
|
|||||||
configs: Object.fromEntries(this.configs),
|
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.fullyInitialized = false;
|
||||||
this.configs.clear();
|
this.configs.clear();
|
||||||
this.customDefinitions.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 接口
|
* Provider Registry 接口
|
||||||
*/
|
*/
|
||||||
interface ProviderRegistryInterface {
|
interface ProviderRegistryInterface {
|
||||||
init(workdir?: string): Promise<void>;
|
init(): Promise<void>;
|
||||||
isInitialized(): boolean;
|
isInitialized(): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ interface ProviderRegistryInterface {
|
|||||||
* Agent Registry 接口
|
* Agent Registry 接口
|
||||||
*/
|
*/
|
||||||
interface AgentRegistryInterface {
|
interface AgentRegistryInterface {
|
||||||
init(workdir: string): Promise<void>;
|
init(): Promise<void>;
|
||||||
isInitialized(): boolean;
|
isInitialized(): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,22 +221,18 @@ export async function initCore(): Promise<boolean> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取服务器配置的工作目录
|
|
||||||
const config = getConfig();
|
|
||||||
const workdir = config.workdir;
|
|
||||||
|
|
||||||
// 初始化 ProviderRegistry(加载用户配置)
|
// 初始化 ProviderRegistry(加载用户配置)
|
||||||
const providerRegistry = core.getProviderRegistry();
|
const providerRegistry = core.getProviderRegistry();
|
||||||
if (!providerRegistry.isInitialized()) {
|
if (!providerRegistry.isInitialized()) {
|
||||||
await providerRegistry.init(workdir);
|
await providerRegistry.init();
|
||||||
console.log('[Agent] ProviderRegistry initialized with workdir:', workdir);
|
console.log('[Agent] ProviderRegistry initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化 AgentRegistry(加载用户自定义 Agent 配置)
|
// 初始化 AgentRegistry(加载用户自定义 Agent 配置)
|
||||||
const agentRegistry = core.agentRegistry;
|
const agentRegistry = core.agentRegistry;
|
||||||
if (!agentRegistry.isInitialized()) {
|
if (!agentRegistry.isInitialized()) {
|
||||||
await agentRegistry.init(workdir);
|
await agentRegistry.init();
|
||||||
console.log('[Agent] AgentRegistry initialized with workdir:', workdir);
|
console.log('[Agent] AgentRegistry initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
coreModule = core;
|
coreModule = core;
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ interface AgentConfigFile {
|
|||||||
// Core Agent 模块类型
|
// Core Agent 模块类型
|
||||||
interface AgentModule {
|
interface AgentModule {
|
||||||
agentRegistry: {
|
agentRegistry: {
|
||||||
init: (workdir: string) => Promise<void>;
|
init: () => Promise<void>;
|
||||||
get: (name: string) => AgentInfo | undefined;
|
get: (name: string) => AgentInfo | undefined;
|
||||||
list: () => AgentInfo[];
|
list: () => AgentInfo[];
|
||||||
};
|
};
|
||||||
@@ -152,8 +152,7 @@ async function ensureRegistryInitialized(): Promise<boolean> {
|
|||||||
if (!module) return false;
|
if (!module) return false;
|
||||||
|
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
const config = getConfig();
|
await module.agentRegistry.init();
|
||||||
await module.agentRegistry.init(config.workdir);
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -247,7 +247,7 @@ providersRouter.post('/', async (c) => {
|
|||||||
|
|
||||||
const registry = core.getProviderRegistry();
|
const registry = core.getProviderRegistry();
|
||||||
registry.registerCustom(body);
|
registry.registerCustom(body);
|
||||||
await registry.saveConfig(getConfig().workdir);
|
await registry.saveConfig();
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -285,7 +285,7 @@ providersRouter.put('/:id', async (c) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
registry.setConfig(id, { ...body, id });
|
registry.setConfig(id, { ...body, id });
|
||||||
await registry.saveConfig(getConfig().workdir);
|
await registry.saveConfig();
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -321,7 +321,7 @@ providersRouter.delete('/:id', async (c) => {
|
|||||||
return c.json({ success: false, error: `Provider not found: ${id}` }, 404);
|
return c.json({ success: false, error: `Provider not found: ${id}` }, 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
await registry.saveConfig(getConfig().workdir);
|
await registry.saveConfig();
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -365,7 +365,7 @@ providersRouter.post('/:id/models', async (c) => {
|
|||||||
|
|
||||||
const registry = core.getProviderRegistry();
|
const registry = core.getProviderRegistry();
|
||||||
registry.addCustomModel(providerId, body);
|
registry.addCustomModel(providerId, body);
|
||||||
await registry.saveConfig(getConfig().workdir);
|
await registry.saveConfig();
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -408,7 +408,7 @@ providersRouter.delete('/:id/models/:modelId', async (c) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await registry.saveConfig(getConfig().workdir);
|
await registry.saveConfig();
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user