低层 Agent Loop 是可复用的 provider-neutral 状态机
agentLoop 只接收 prompts、AgentContext、AgentLoopConfig 和可选 streamFn;runLoop 把 steering、tool calls、follow-up、continuation 和 abort 编排成外层/内层循环,并最终发出 agent_end。
Prime Agent 把“模型怎么流式回答、什么时候执行工具、用户插话后是否继续”抽成一个不依赖 TUI 的小内核,上层入口只负责喂配置和消费事件。
自研时可把 loop 做成纯运行时,再让 CLI、RPC、桌面 UI 共用;不要让界面组件自己复制一套 tool loop。
178 * Start an agent loop with a new prompt message.
179 * The prompt is added to the context and events are emitted for it.
180 */
181 export function agentLoop(
182 prompts: AgentMessage[],
183 context: AgentContext,
184 config: AgentLoopConfig,
185 signal?: AbortSignal,
186 streamFn?: StreamFn,
187 ): EventStream<AgentEvent, AgentMessage[]> {
188 const stream = createAgentStream();
189
190 endAgentStreamOnError(
191 stream,
192 runAgentLoop(
193 prompts,
194 context,
195 config,
196 async (event) => {
197 stream.push(event);
198 },
199 signal,
200 streamFn,
201 ),
202 );
203
204 return stream;
205 }
查看全部 3 处证据
-
实现
packages/agent/src/agent-loop.ts:178–205agentLoop 创建流并启动 runAgentLoop。 -
实现
packages/agent/src/agent-loop.ts:307–373外层/内层 turn loop、stream response、tool batch。 -
实现
packages/agent/src/agent-loop.ts:422–460follow-up/continuation poll 后发出 agent_end。