UNPKG

flux-agent

Version:

FluxAgent - 一个可灵活插拔的AI Agent系统框架,基于TypeScript开发,支持流式执行、事件系统、插件系统、知识库管理等功能 (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (

78 lines (77 loc) 2.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginManager = void 0; class PluginManager { constructor(agent) { this.plugins = new Map(); this.eventProcessors = new Map(); this.disabledPhases = new Set(); this.additionalTools = []; this.agent = agent; } register(name, pluginFunction) { if (this.plugins.has(name)) { throw new Error(`Plugin ${name} 已经注册`); } this.plugins.set(name, pluginFunction); // 创建插件上下文并应用插件 const context = this.createPluginContext(); pluginFunction(context); } unregister(name) { this.plugins.delete(name); } createPluginContext() { return { tapVoid: (eventName, callback) => { if (!this.eventProcessors.has(eventName)) { this.eventProcessors.set(eventName, []); } this.eventProcessors.get(eventName).push((payload) => { callback(payload); return payload; }); }, tap: (eventName, callback) => { if (!this.eventProcessors.has(eventName)) { this.eventProcessors.set(eventName, []); } this.eventProcessors.get(eventName).push(callback); }, disconnect: (phaseName) => { this.disabledPhases.add(phaseName); }, addTools: (tools) => { tools.forEach(tool => { tool.applyAgent(this.agent); }); this.additionalTools.push(...tools); } }; } processEvent(eventName, payload) { if (!this.eventProcessors.has(eventName)) { return payload; } return this.eventProcessors.get(eventName).reduce((currentPayload, processor) => processor(currentPayload), payload); } getDisabledPhases() { return new Set(this.disabledPhases); } getAdditionalTools() { return [...this.additionalTools]; } hasPlugin(name) { return this.plugins.has(name); } getPluginNames() { return Array.from(this.plugins.keys()); } clear() { this.plugins.clear(); this.eventProcessors.clear(); this.disabledPhases.clear(); this.additionalTools = []; } } exports.PluginManager = PluginManager;