Expand description
Mirrors packages/agent/src/agent-loop.ts — the provider-agnostic agent loop.
Two public free functions, run_agent_loop (new prompt) and
run_agent_loop_continue (no new prompt), drive the outer follow-up loop
and inner steering+tool loop. The only LLM boundary is StreamFn
(sync return → AssistantMessageEventStream); everything else talks in
AgentMessage.
Critical invariants enforced here (plan §5):
- Tool-execution ordering: in a parallel batch,
ToolExecutionEndfires in completion order; tool-resultMessageStart/MessageEndfire later in source/ordinal order. Implemented by collecting completion signals into a queue, then walking the finalized vec by index for the result messages. - Truncate-fail:
stop_reason == Length→ every tool call in the message fails-in-place withis_error:trueand is not executed. - Late-update suppression:
on_updateafterexecuteresolves is a no-op via anaccepting_updates: Arc<AtomicBool>flipped false on settle.
The loop is fully testable without crate::Agent — run_agent_loop takes
plain AgentContext/AgentLoopConfig + an AgentEmitter.
Enums§
- Loop
Outcome - Why a run ended.
Completedis the normal exit;Aborted/Failedare set when the terminal assistant message carriedAborted/Error.
Functions§
- run_
agent_ loop - Run an agent loop starting from a new prompt. Mirrors TS
runAgentLoop. - run_
agent_ loop_ continue - Continue an agent loop from the existing context (no new prompt). Mirrors
TS
runAgentLoopContinue. Errors if the context is empty or its last message is an assistant message (the provider would reject that).
Type Aliases§
- NewMessages
- The messages a single
run_agent_loopinvocation added (prompt + assistant