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) (

194 lines (193 loc) 6.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolManager = void 0; const Phases_1 = require("./Phases"); /** * 工具管理器 - 负责处理所有工具调用相关的逻辑 */ class ToolManager { constructor(tools = [], logger, eventHub) { this.tools = new Map(); this.logger = logger; this.eventHub = eventHub; // 注册工具 tools.forEach(tool => this.registerTool(tool)); } getToolsForPhase(phase) { return Array.from(this.tools.values()).filter(tool => { if (typeof tool.phase === 'string') { return tool.phase === phase || tool.phase === Phases_1.PhaseType.ALL; } return tool.phase.includes(phase); }); } /** * 设置事件中心 */ setEventHub(eventHub) { this.eventHub = eventHub; } /** * 注册工具 */ registerTool(tool) { this.tools.set(tool.getName(), tool); } /** * 注册多个工具 */ registerTools(tools) { tools.forEach(tool => this.registerTool(tool)); } /** * 获取工具定义(用于LLM) */ getToolDefinitions() { return Array.from(this.tools.values()).map(tool => tool.getDefinition()); } /** * 获取所有工具名称 */ getToolNames() { return Array.from(this.tools.keys()); } /** * 检查工具是否存在 */ hasTool(toolName) { return this.tools.has(toolName); } /** * 获取工具数量 */ getToolCount() { return this.tools.size; } /** * 移除工具 */ removeTool(toolName) { return this.tools.delete(toolName); } /** * 清空所有工具 */ clearTools() { this.tools.clear(); } /** * 记录日志 */ log(message, data) { if (this.logger) { this.logger(message, data); } } /** * 执行工具调用 */ async executeToolCall(toolcallId, toolName, args, context, phase) { this.log(`处理工具调用: ${toolName}`, { args }); if (!this.tools.has(toolName)) { throw new Error(`工具 ${toolName} 未找到`); } const tool = this.tools.get(toolName); try { // 记录工具调用(在执行前) context.addToolCall(toolName, args); // 触发工具调用前事件 this.emitToolEvent({ timing: 'before', toolName, args, system: tool.isSystemTool, toolcallId }); const executionContext = { toolcallId, phase: phase || Phases_1.PhaseType.REACT, context, eventHub: this.eventHub, logger: this.logger }; const stopToolCallPromise = this.createStopToolCallPromise(toolcallId, tool); // 使用Promise.race来处理工具调用和停止调用 const result = await Promise.race([tool.execute(args, executionContext), stopToolCallPromise]); // 触发工具调用后事件 this.emitToolEvent({ timing: 'after', status: result.type, toolName, system: tool.isSystemTool, args, result, toolcallId }); // 记录工具调用结果到Context const toolRecordText = result.type === 'stop' ? '工具调用被用户停止,可视为用户不希望你调用该工具,可以询问用户原因' : tool.getContextRecordText(result); context.addToolResult({ toolName, toolcallId, args, result, recordText: toolRecordText }); this.log(`工具调用完成: ${toolName}`, result); return result; } catch (error) { context.addToolResult({ toolName, toolcallId, args, result: { type: 'error', error: new Error(error?.message || '工具执行失败') }, recordText: '工具执行失败' }); this.log(`工具调用错误: ${toolName}`, { error: error.message }); // 触发工具调用后事件(错误情况) this.emitToolEvent({ timing: 'after', status: 'error', toolName, system: tool.isSystemTool, args, result: { error: error.message } }); return { type: 'error', error: error }; } } /** * 创建停止运行工具调用Promise */ createStopToolCallPromise(toolcallId, tool) { return new Promise((resolve, reject) => { this.log('创建停止运行工具调用Promise', { toolcallId }); try { this.eventHub?.registerType('StopToolCallEvent', ({ payload }) => { if (payload.toolcallId === toolcallId) { resolve({ type: 'stop', }); tool.cancel(); } }); this.eventHub?.registerType('StopEvent', () => { resolve({ type: 'stop', }); tool.cancel(); }); } catch (error) { this.log('创建停止运行工具调用Promise失败', { error }); reject(error); } }); } /** * 触发工具事件 */ emitToolEvent(option) { if (!this.eventHub) { return; } const { timing, status, toolName, system, args, result, toolcallId } = option; const eventName = timing === 'before' ? `Before-${toolName}` : `After-${toolName}`; this.eventHub.emit({ type: 'ToolCallEvent', name: eventName, payload: { toolcallId, status, system, timing, toolType: 'function', toolName, arguments: args, result } }); } /** * 判断工具调用是否应该触发阶段变更 */ shouldTriggerPhaseChange(toolName) { // EndPhaseTool 总是触发阶段变更 if (toolName === 'EndPhaseTool') { return true; } // 其他工具根据阶段判断 return false; } } exports.ToolManager = ToolManager;