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

105 lines (104 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Phases = exports.PhaseType = void 0; const prompts_1 = require("../prompts"); var PhaseType; (function (PhaseType) { PhaseType["CONFIRM"] = "confirm"; PhaseType["THINKPLAN"] = "thinkplan"; PhaseType["PLAN"] = "plan"; PhaseType["REACT"] = "react"; PhaseType["SUMMARY"] = "summary"; PhaseType["ALL"] = "all"; })(PhaseType || (exports.PhaseType = PhaseType = {})); /** * Phases 类 - Agent 阶段管理器 * * 设计理念:基于 LLM 上下文的自主任务判断 * * 核心原则: * 1. 相信 LLM 基于上下文做出正确判断的能力 * 2. 对话历史和工具调用结果就是任务状态的唯一真相源 * 3. 简化状态管理,让代码更易维护 */ class Phases { constructor(logger, promptFactory) { this.phases = [ PhaseType.CONFIRM, PhaseType.THINKPLAN, PhaseType.PLAN, PhaseType.REACT, PhaseType.SUMMARY ]; this.currentPhaseIndex = 0; this.disabledPhases = new Set(); this.phaseState = { reactCycleCount: 0 }; this.logger = logger || (() => { }); this.promptFactory = promptFactory || {}; } getCurrentPhase() { return this.phases[this.currentPhaseIndex]; } nextPhase() { let nextIndex = (this.currentPhaseIndex + 1) % this.phases.length; while (nextIndex < this.phases.length) { const nextPhase = this.phases[nextIndex]; if (!this.disabledPhases.has(nextPhase)) { this.currentPhaseIndex = nextIndex; // 重置阶段状态 if ([PhaseType.CONFIRM, PhaseType.THINKPLAN, PhaseType.PLAN].includes(nextPhase)) { this.phaseState.reactCycleCount = 0; } return nextPhase; } nextIndex++; } return null; // 没有下一个阶段了 } getNextPhase() { let nextIndex = (this.currentPhaseIndex + 1) % this.phases.length; while (nextIndex < this.phases.length) { const nextPhase = this.phases[nextIndex]; if (!this.disabledPhases.has(nextPhase)) { return nextPhase; } nextIndex++; } return null; } getPhaseState() { return { ...this.phaseState }; } resetPhase() { this.currentPhaseIndex = 0; this.phaseState = { reactCycleCount: 0 }; } disablePhase(phase) { this.disabledPhases.add(phase); } getPhasePrompt(phase, options = {}) { // 如果提供了自定义提示词,直接使用 if (options.customPrompt) { return options.customPrompt; } const promptFactory = this.promptFactory?.[phase]; // 从markdown文件加载提示词 const defaultPrompt = prompts_1.PhasePrompts[phase] || ''; const markdownPrompt = promptFactory ? promptFactory(defaultPrompt) : defaultPrompt; return `${options.input ? `<user> ${options.input} </user>` : ''} <system> ${markdownPrompt} </system> `; } } exports.Phases = Phases; Phases.llmCallCache = new Map(); Phases.cacheExpiry = new Map(); Phases.CACHE_TTL = 30000; // 30秒缓存