Skip to main content

Module agent_loop

Module agent_loop 

Source
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, ToolExecutionEnd fires in completion order; tool-result MessageStart/MessageEnd fire 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 with is_error:true and is not executed.
  • Late-update suppression: on_update after execute resolves is a no-op via an accepting_updates: Arc<AtomicBool> flipped false on settle.

The loop is fully testable without crate::Agentrun_agent_loop takes plain AgentContext/AgentLoopConfig + an AgentEmitter.

Enums§

LoopOutcome
Why a run ended. Completed is the normal exit; Aborted/Failed are set when the terminal assistant message carried Aborted/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_loop invocation added (prompt + assistant