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) (
243 lines (242 loc) • 8.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecutionSession = void 0;
const Phases_1 = require("../Phases");
const types_1 = require("./types");
const State_1 = require("../State");
const Agent_1 = require("../Agent");
/**
* Agent 执行会话
* 管理单次执行过程中的状态和上下文
*/
class ExecutionSession {
constructor(agent, // 暂时使用any,后续会优化类型
initialPhase) {
this.agent = agent;
this.response = {
content: null
};
this.phaseJumpHistory = []; // 记录阶段跳转历史
this.justJumped = false; // 新增:标记是否刚刚进行了阶段跳转
this.currentPhase = initialPhase;
this.loopControl = new types_1.LoopControlImpl(() => this.agent.state.getState() === State_1.AgentState.RUNNING, () => this.agent.waitingForUserInput);
}
/**
* 获取当前阶段
*/
getCurrentPhase() {
return this.currentPhase;
}
/**
* 设置当前阶段
*/
setCurrentPhase(phase) {
this.currentPhase = phase;
this.agent.context.setCurrentPhase(phase);
}
/**
* 获取当前响应
*/
getResponse() {
return this.response;
}
/**
* 设置响应
*/
setResponse(response) {
this.response = response;
}
/**
* 获取循环控制器
*/
getLoopControl() {
return this.loopControl;
}
/**
* 检查是否应该继续执行
*/
shouldContinue() {
return this.loopControl.shouldContinue();
}
/**
* 检查是否刚刚进行了阶段跳转
*/
hasJustJumped() {
return this.justJumped;
}
/**
* 重置跳转标记
*/
resetJumpFlag() {
this.justJumped = false;
}
/**
* 处理阶段结果
*/
async handlePhaseResult(result) {
switch (result.type) {
case 'skip':
await this.handlePhaseSkip(result);
break;
case 'complete':
await this.handlePhaseComplete(result);
break;
case 'pause':
await this.handlePause(result);
break;
case 'stop':
await this.handleStop();
break;
case 'error':
await this.handleError(result);
break;
}
}
/**
* 处理阶段跳过
*/
async handlePhaseSkip(result) {
Agent_1.AgentLogger.log(`跳过 ${this.currentPhase} 阶段: ${result.reason}`);
// 进入下一个标准阶段
const nextPhase = this.agent.phases.nextPhase();
if (nextPhase) {
this.setCurrentPhase(nextPhase);
this.loopControl.resetPhaseLoop();
// 设置跳转标记
this.justJumped = true;
Agent_1.AgentLogger.log(`跳过当前阶段,进入下一阶段: ${this.currentPhase}`);
}
else {
// 没有更多阶段,结束执行
Agent_1.AgentLogger.log('所有后续阶段都被跳过,结束执行');
this.agent.state.setState(State_1.AgentState.STOP);
this.agent.emitStateChangeEvent(State_1.AgentState.STOP);
}
}
/**
* 处理阶段完成
*/
async handlePhaseComplete(result) {
if (result.response) {
this.setResponse(result.response);
this.agent.context.addAgentResponse(result.response.content || result.response);
}
// 阶段完成后,推进到下一个阶段
const nextPhase = this.agent.phases.nextPhase();
if (nextPhase) {
const oldPhase = this.currentPhase;
this.setCurrentPhase(nextPhase);
this.loopControl.resetPhaseLoop();
// 正常的阶段推进不设置跳转标记,仍然需要进行必要性判断
// justJumped 标记只在智能跳转(handlePhaseSkip)时使用
Agent_1.AgentLogger.log(`阶段完成,推进到下一阶段: ${oldPhase} -> ${this.currentPhase}`);
}
else {
// 没有更多阶段,结束执行
Agent_1.AgentLogger.log('所有阶段已完成,结束执行');
this.agent.state.setState(State_1.AgentState.STOP);
this.agent.emitStateChangeEvent(State_1.AgentState.STOP);
}
}
/**
* 处理暂停(等待用户输入)
*/
async handlePause(result) {
if (result.response) {
this.setResponse(result.response);
}
this.agent.waitingForUserInput = true;
Agent_1.AgentLogger.log('等待用户输入');
}
/**
* 处理停止
*/
async handleStop() {
this.agent.phases.resetPhase();
this.setCurrentPhase(this.agent.phases.getCurrentPhase());
this.loopControl.resetPhaseLoop();
this.agent.state.setState(State_1.AgentState.IDLE);
this.agent.emitStateChangeEvent(State_1.AgentState.IDLE);
this.agent.waitingForUserInput = true;
}
/**
* 处理错误
*/
async handleError(result) {
Agent_1.AgentLogger.log('执行过程中发生错误', { error: result.error?.message });
this.agent.state.setState(State_1.AgentState.STOP);
this.agent.emitStateChangeEvent(State_1.AgentState.STOP);
this.setResponse({
content: result.error?.message || '执行出现错误'
});
}
/**
* 处理循环控制
*/
checkLoopLimits() {
this.loopControl.incrementPhaseLoop();
this.loopControl.incrementTotalLoop();
// 检查总循环限制
if (this.loopControl.isTotalLoopExceeded()) {
Agent_1.AgentLogger.log(`总循环次数过多(${this.loopControl.getTotalLoopCount()}),强制终止Agent`);
this.agent.state.setState(State_1.AgentState.STOP);
this.agent.emitStateChangeEvent(State_1.AgentState.STOP);
return {
type: 'pause',
response: {
content: '抱歉,任务处理时间过长,已自动停止。'
}
};
}
// 检查阶段循环限制
if (this.loopControl.isPhaseLoopExceeded()) {
Agent_1.AgentLogger.log(`阶段${this.currentPhase}循环次数过多(${this.loopControl.getPhaseLoopCount()}),强制推进到下一阶段`);
if (this.currentPhase === Phases_1.PhaseType.SUMMARY) {
// 最后阶段,强制终止
this.agent.state.setState(State_1.AgentState.STOP);
this.agent.emitStateChangeEvent(State_1.AgentState.STOP);
return {
type: 'pause',
response: {
content: '任务已完成。'
}
};
}
else {
// 强制进入下一阶段
const nextPhase = this.agent.phases.nextPhase();
if (nextPhase) {
const oldPhase = this.currentPhase;
this.setCurrentPhase(nextPhase);
this.loopControl.resetPhaseLoop();
Agent_1.AgentLogger.log(`强制阶段切换: ${oldPhase} -> ${this.currentPhase}`);
return null; // 继续执行
}
else {
// 没有下一阶段,强制终止
this.agent.state.setState(State_1.AgentState.STOP);
this.agent.emitStateChangeEvent(State_1.AgentState.STOP);
return {
type: 'pause',
response: {
content: '任务已完成。'
}
};
}
}
}
return null; // 继续正常执行
}
/**
* 获取执行统计信息
*/
getExecutionStats() {
return {
currentPhase: this.currentPhase,
phaseLoopCount: this.loopControl.getPhaseLoopCount(),
totalLoopCount: this.loopControl.getTotalLoopCount(),
response: this.response
};
}
}
exports.ExecutionSession = ExecutionSession;