AgentModule
AgentModule
AgentModule 是 QitOS 的策略层。你通过继承它来定义智能体的状态形状、系统提示词、决策逻辑与归约规则;真正的执行循环由 Engine(执行内核)驱动。Signature reference (not executable):钩子只要求实现
init_state 与 reduce;其余钩子全部可选。init_state(必需)
init_state(必需)
Signature reference (not executable):创建并返回本次运行的初始状态。
reduce(必需)
reduce(必需)
Signature reference (not executable):把当前观测结果(每步后智能体接收的结构化观察结果)与决策(智能体每步的结构化决策)折叠进下一步状态。
build_system_prompt
build_system_prompt
Signature reference (not executable):返回动态系统提示词;默认返回
None。prepare
prepare
Signature reference (not executable):把状态转成模型输入文本;默认是
str(state)。decide
decide
Signature reference (not executable):自定义决策钩子。返回
Decision 时跳过默认模型调用;返回 None 时继续走 Engine 的模型路径。should_stop
should_stop
Signature reference (not executable):额外停止条件;默认返回
False。.run() 方法Signature reference (not executable):Engine、执行任务,并默认返回 state.final_result;当 return_state=True 时,返回完整 EngineResult。Engine
Engine
Engine 是执行内核,负责阶段循环、工具执行、恢复、追踪与停止条件评估。Signature reference (not executable):agentbudget(预算)parserstop_criteriacritics(评估器)envhistory_policytrace_writerhooks(钩子)
EngineResult
EngineResult
示意片段(非独立程序;完整执行文件见本页链接)。其中:
state:最终强类型状态records:每步StepRecordevents:所有运行时事件step_count:执行步数task_result:结构化任务结果
AsyncEngine
AsyncEngine
AsyncEngine 提供非阻塞的智能体执行能力。它封装了同样的 Engine 循环,但把阻塞调用放到线程池中执行,适用于 asyncio 事件循环。Signature reference (not executable):Engine 构造函数(参数与 Engine.__init__ 相同)。方法Signature reference (not executable):Engine.run() 相同的 EngineResult。Signature reference (not executable):
EngineEvent 对象。流以 run_start 开始,以 run_end 结束。Signature reference (not executable):
Engine.run()。属性EngineEvent / EngineEventType / EventStream
EngineEvent / EngineEventType / EventStream
EngineEvent 是 AsyncEngine.arun_stream() 产出的结构化事件。示意片段(非独立程序;完整执行文件见本页链接)。EventStream 是一个异步兼容的事件队列,用于消费引擎事件:Signature reference (not executable):Decision
Decision
Decision 是决策阶段的规范输出(智能体每步的结构化决策)。推荐用工厂方法构造:示意片段(非独立程序;完整执行文件见本页链接)。"act":执行动作(标准化工具调用)"final":给出最终答案并结束"wait":本步不执行动作"branch":提出多个候选决策
Task / State / Action
Task / State / Action
常用基础数据结构包括:
StateSchemaTaskTaskBudgetTaskResourceActionStopReason
StopReason.CANCELLED_IMMEDIATE 会把
"cancelled_immediate" 同步写入最终 State、任务/运行结果与 END event;对应的
trace manifest 使用终态 status="stopped",而不是正常完成状态。