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

111 lines (110 loc) 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PhaseManager = void 0; const Phases_1 = require("../Phases"); const Agent_1 = require("../Agent"); /** * 阶段管理器 * 负责阶段相关的所有逻辑处理 */ class PhaseManager { constructor(agent // 暂时使用any,后续会优化类型 ) { this.agent = agent; } /** * 生成阶段提示词 */ async generatePrompt(phase) { // 优先尝试获取消费的用户输入 const { userInput: input, images } = this.agent.context.consumeUserInput(); const lastDeclearMessage = this.agent.memory.getLastDeclearMessage(); let prompt; let system; if (lastDeclearMessage?.phaseDeclear === phase) { if (phase === Phases_1.PhaseType.CONFIRM) { return { system: !input, prompt: input ? `<user>${input}</user>` : '<system>请确认以上信息是否充足,如果充足,请结束当前阶段</system>', }; } if (phase === Phases_1.PhaseType.REACT) { return { system: !input, prompt: input ? `<user>${input}</user>` : `<system> 如果当前任务已经执行完毕,请结束当前阶段 如果当前任务没有执行完毕,请按照预定计划执行下一个任务 **严令禁止**:不要对用户暴露内部实现细节(工具、阶段等概念)和具体返回参数,内部技术细节会对用户产生干扰 </system>`, }; } if (phase === Phases_1.PhaseType.PLAN) { return { system: !input, prompt: input ? `<user>${input}</user>` : '<system>上面是你针对用户的问题做的任务规划,如果你已经规划完成,请结束当前阶段</system>', }; } if (phase === Phases_1.PhaseType.SUMMARY) { return { system: !input, prompt: input ? `<user>${input}</user>` : '<system>上面是你针对用户问题以及任务执行结果做的总结,如果已经总结完成,请结束当前阶段</system>', }; } } if (input) { prompt = this.agent.phases.getPhasePrompt(phase, { input, promptFactory: this.agent.promptFactory }); Agent_1.AgentLogger.log(`${phase}阶段使用消费的用户输入: ${input}`); } else { // 使用默认提示词 prompt = this.agent.phases.getPhasePrompt(phase, { promptFactory: this.agent.promptFactory }); system = true; Agent_1.AgentLogger.log(`${phase}阶段使用默认提示词`); } // 如果仍然没有提示词,抛出错误 if (!prompt) { throw new Error('没有提示词可用'); } return { system, prompt: this.processPromptWithPlugins(phase, prompt, input || this.agent.context.getLatestUserInput()), phaseDeclear: phase, images }; } /** * 通过插件处理提示词 */ processPromptWithPlugins(phase, prompt, input) { const beforeEventName = `Before-${phase}`; // 触发阶段前事件 Agent_1.AgentLogger.log(`触发事件: ${beforeEventName}`); const beforePayload = this.agent.pluginManager.processEvent(beforeEventName, { prompt, input }); // 使用处理后的提示词 if ('prompt' in beforePayload) { const processedPrompt = beforePayload.prompt || prompt; Agent_1.AgentLogger.log('处理后的提示词', { prompt: processedPrompt }); return processedPrompt; } return prompt; } /** * 处理阶段后事件 */ triggerAfterPhaseEvent(phase, prompt, response) { const afterEventName = `After-${phase}`; Agent_1.AgentLogger.log(`触发事件: ${afterEventName}`); this.agent.pluginManager.processEvent(afterEventName, { prompt, agentResponse: response }); this.agent.eventHub.emit({ type: 'PhaseEvent', name: afterEventName, payload: { agentResponse: response } }); } } exports.PhaseManager = PhaseManager;