fix(init): 确保 Registry 在服务启动时正确初始化
- AgentRegistry: 在构造函数中同步初始化预设 Agent - AgentRegistry: 添加 isInitialized() 方法 - initCore(): 添加 ProviderRegistry.init() 调用 - initCore(): 添加 AgentRegistry.init() 调用 修复用户配置(Provider、Agent)重启后丢失的问题
This commit is contained in:
@@ -13,23 +13,33 @@ export class AgentRegistry {
|
|||||||
private userConfig: AgentConfigFile | null = null;
|
private userConfig: AgentConfigFile | null = null;
|
||||||
private initialized = false;
|
private initialized = false;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
// 同步初始化预设 Agent(无需等待 init())
|
||||||
|
this.initPresetAgents();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化 - 加载预设和用户配置
|
* 同步初始化预设 Agent
|
||||||
|
*/
|
||||||
|
private initPresetAgents(): void {
|
||||||
|
for (const [name, agentConfig] of Object.entries(presetAgents)) {
|
||||||
|
this.agents.set(name, { ...agentConfig, name });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 完整初始化 - 加载用户配置
|
||||||
|
* 用户配置可以覆盖预设 Agent 的设置
|
||||||
*/
|
*/
|
||||||
async init(workdir: string): Promise<void> {
|
async init(workdir: string): Promise<void> {
|
||||||
if (this.initialized) return;
|
if (this.initialized) return;
|
||||||
|
|
||||||
// 1. 注册预设 Agent
|
// 加载用户配置
|
||||||
for (const [name, agentConfig] of Object.entries(presetAgents)) {
|
|
||||||
this.agents.set(name, { ...agentConfig, name });
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 加载用户配置
|
|
||||||
this.userConfig = await loadAgentConfig(workdir);
|
this.userConfig = await loadAgentConfig(workdir);
|
||||||
if (this.userConfig) {
|
if (this.userConfig) {
|
||||||
this.globalConfig = this.userConfig.defaults ?? null;
|
this.globalConfig = this.userConfig.defaults ?? null;
|
||||||
|
|
||||||
// 注册用户自定义 Agent
|
// 注册用户自定义 Agent(可以覆盖预设)
|
||||||
if (this.userConfig.agents) {
|
if (this.userConfig.agents) {
|
||||||
for (const [name, config] of Object.entries(this.userConfig.agents)) {
|
for (const [name, config] of Object.entries(this.userConfig.agents)) {
|
||||||
this.agents.set(name, { ...config, name });
|
this.agents.set(name, { ...config, name });
|
||||||
@@ -40,6 +50,13 @@ export class AgentRegistry {
|
|||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否已完整初始化(包括用户配置)
|
||||||
|
*/
|
||||||
|
isInitialized(): boolean {
|
||||||
|
return this.initialized;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 Agent(应用权限合并)
|
* 获取 Agent(应用权限合并)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -76,6 +76,22 @@ interface PermissionManager {
|
|||||||
setAskCallback(callback: (ctx: unknown) => Promise<{ allow: boolean; remember?: boolean }>): void;
|
setAskCallback(callback: (ctx: unknown) => Promise<{ allow: boolean; remember?: boolean }>): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provider Registry 接口
|
||||||
|
*/
|
||||||
|
interface ProviderRegistryInterface {
|
||||||
|
init(workdir?: string): Promise<void>;
|
||||||
|
isInitialized(): boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Agent Registry 接口
|
||||||
|
*/
|
||||||
|
interface AgentRegistryInterface {
|
||||||
|
init(workdir: string): Promise<void>;
|
||||||
|
isInitialized(): boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Core 模块接口
|
* Core 模块接口
|
||||||
*/
|
*/
|
||||||
@@ -85,6 +101,8 @@ interface CoreModule {
|
|||||||
loadConfig: () => unknown;
|
loadConfig: () => unknown;
|
||||||
saveConfig: (config: Record<string, unknown>) => void;
|
saveConfig: (config: Record<string, unknown>) => void;
|
||||||
getPermissionManager: (projectRoot?: string) => PermissionManager;
|
getPermissionManager: (projectRoot?: string) => PermissionManager;
|
||||||
|
getProviderRegistry: () => ProviderRegistryInterface;
|
||||||
|
agentRegistry: AgentRegistryInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -120,6 +138,20 @@ export async function initCore(): Promise<boolean> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 初始化 ProviderRegistry(加载用户配置)
|
||||||
|
const providerRegistry = core.getProviderRegistry();
|
||||||
|
if (!providerRegistry.isInitialized()) {
|
||||||
|
await providerRegistry.init();
|
||||||
|
console.log('[Agent] ProviderRegistry initialized');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化 AgentRegistry(加载用户自定义 Agent 配置)
|
||||||
|
const agentRegistry = core.agentRegistry;
|
||||||
|
if (!agentRegistry.isInitialized()) {
|
||||||
|
await agentRegistry.init(process.cwd());
|
||||||
|
console.log('[Agent] AgentRegistry initialized');
|
||||||
|
}
|
||||||
|
|
||||||
coreModule = core;
|
coreModule = core;
|
||||||
console.log('[Agent] Core module loaded');
|
console.log('[Agent] Core module loaded');
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user