/** * EventBus — 统一事件系统 * * 功能: * 1. 类型安全的事件发布/订阅 * 2. 通配符监听(*) * 3. 一次性监听(once) * 4. 事件历史回放 * 5. 中间件/拦截器 * 6. WebSocket 广播适配(给 Dashboard 推送实时状态) * * 所有模块(Safety/Quality/Analytics/Scheduler/Orchestrator/Workflow) * 通过 EventBus 通信,实现松耦合。 */ /** 所有系统事件 */ export interface AgentEvents { "agent:ready": { timestamp: string; }; "agent:shutdown": { reason?: string; }; "skill:loaded": { name: string; version: string; }; "skill:unloaded": { name: string; }; "skill:error": { name: string; error: string; }; "tool:start": { tool: string; params: Record; }; "tool:success": { tool: string; result: unknown; durationMs: number; }; "tool:error": { tool: string; error: string; durationMs: number; }; "publish:start": { platform: string; title: string; }; "publish:success": { platform: string; title: string; url?: string; }; "publish:failed": { platform: string; title: string; error: string; }; "publish:error": { platform: string; title: string; error: string; }; "task:completed": { taskId: string; result?: string; }; "task:failed": { taskId: string; error: string; }; "safety:blocked": { platform: string; reason: string; riskScore: number; }; "safety:warning": { type: string; description: string; }; "quality:scored": { title: string; score: number; grade: string; }; "quality:optimized": { title: string; before: number; after: number; }; "analytics:tracked": { platform: string; contentId: string; }; "analytics:report": { timeRange: string; totalViews: number; }; "cron:created": { taskId: string; description: string; cron: string; }; "cron:executed": { taskId: string; result: string; }; "cron:failed": { taskId: string; error: string; }; "orchestrator:start": { taskId: string; strategy: string; }; "orchestrator:phase": { taskId: string; phase: string; status: string; }; "orchestrator:complete": { taskId: string; result: string; }; "workflow:start": { pipelineId: string; runId: string; }; "workflow:node": { pipelineId: string; nodeId: string; status: string; }; "workflow:complete": { pipelineId: string; runId: string; status: string; }; "mcp:connected": { server: string; }; "mcp:disconnected": { server: string; reason?: string; }; "mcp:tool_called": { server: string; tool: string; }; "log:info": { message: string; source?: string; }; "log:warn": { message: string; source?: string; }; "log:error": { message: string; source?: string; stack?: string; }; } /** 事件名称 */ export type AgentEventName = keyof AgentEvents; /** 事件数据 */ export type AgentEventData = AgentEvents[K]; /** 监听器 */ export type EventListener = (data: AgentEventData, meta: EventMeta) => void | Promise; /** 事件元数据 */ export interface EventMeta { eventName: string; timestamp: string; source?: string; } /** 中间件 */ export type EventMiddleware = (eventName: string, data: unknown, meta: EventMeta, next: () => void) => void | Promise; /** 事件历史记录 */ export interface EventRecord { eventName: string; data: unknown; meta: EventMeta; } export declare class EventBus { private listeners; private middlewares; private history; private maxHistory; /** WebSocket 广播回调(由 Dashboard 设置) */ private broadcastFn?; constructor(options?: { maxHistory?: number; }); /** * 订阅事件 */ on(event: K, listener: EventListener): () => void; /** * 一次性订阅 */ once(event: K, listener: EventListener): () => void; /** * 订阅所有事件(通配符) */ onAny(listener: (eventName: string, data: unknown, meta: EventMeta) => void): () => void; /** * 取消订阅 */ off(event: K, listener: EventListener): void; /** * 发布事件 */ emit(event: K, data: AgentEventData, source?: string): Promise; /** * 添加中间件(拦截器) * 调用 next() 继续传播,不调用则拦截 */ use(middleware: EventMiddleware): void; /** * 获取事件历史 */ getHistory(filter?: { eventName?: string; limit?: number; since?: string; }): EventRecord[]; /** 清空历史 */ clearHistory(): void; /** * 设置 WebSocket 广播函数 * Dashboard 启动时调用此方法,将事件推送给所有连接的客户端 */ setBroadcast(fn: (event: EventRecord) => void): void; getStatus(): { listenerCount: number; eventTypes: number; middlewareCount: number; historySize: number; }; /** 移除所有监听器 */ removeAllListeners(): void; private addListener; } /** 获取全局 EventBus 单例 */ export declare function getEventBus(): EventBus; /** 重置全局 EventBus(测试用) */ export declare function resetEventBus(): void; //# sourceMappingURL=bus.d.ts.map