Alloy.Tool.Executor (alloy v0.16.0)

Copy Markdown View Source

Executes tool calls and returns result messages.

Calls run in the order the model made them. Consecutive calls to tools that are safe to run concurrently (the default; see Alloy.Tool.concurrent?/0) run in parallel; a call to a tool that returns concurrent?: false waits for everything before it and runs alone.

:tool_concurrency limits parallel calls (defaults to the scheduler count). :tool_validation defaults to required-key checks; :schema enables optional JSON Schema validation. Each dispatched call outcome passes through :after_tool_call middleware before transcript and event publication. If the run's :timeout_ms deadline expires before a batch starts, unstarted calls (including middleware-blocked calls in that batch) still receive matching error tool_result blocks in the transcript (preserving any :before_tool_call block reason) without emitting :tool_start/:tool_end, running :after_tool_call, or appearing in result.tool_calls.

Each call runs in an unlinked task under Alloy.TaskSupervisor, bounded by the agent's :tool_timeout. A tool that raises, exits, throws or times out produces an is_error tool result instead of crashing the agent.

Summary

Functions

Executes tool_calls against tool_fns and returns a user Alloy.Message holding matching tool_result blocks in call order, or {:halted, reason} if :before_tool_call middleware halts the round.

Executes tool_calls against tool_fns and returns {:ok, result_message, tool_call_metadata} or {:halted, reason}.

Executes a single tool call through the same pipeline as execute_all/4.

Functions

execute_all(tool_calls, tool_fns, state)

@spec execute_all(
  [map()],
  %{required(String.t()) => Alloy.Tool.Registry.tool()},
  Alloy.Agent.State.t()
) ::
  Alloy.Message.t() | {:halted, String.t()}

Executes tool_calls against tool_fns and returns a user Alloy.Message holding matching tool_result blocks in call order, or {:halted, reason} if :before_tool_call middleware halts the round.

Equivalent to execute_all/4 with default options ([]), discarding the per-call execution metadata list.

execute_all(tool_calls, tool_fns, state, opts)

@spec execute_all(
  [map()],
  %{required(String.t()) => Alloy.Tool.Registry.tool()},
  Alloy.Agent.State.t(),
  keyword()
) :: {:ok, Alloy.Message.t(), [map()]} | {:halted, String.t()}

Executes tool_calls against tool_fns and returns {:ok, result_message, tool_call_metadata} or {:halted, reason}.

Consecutive calls to concurrency-safe tools (Alloy.Tool.concurrent?/0) run in parallel up to :tool_concurrency; sequential tools wait for earlier calls and run alone in call order. Accepts the same execution and event coordination options as run_tool/3 (:tool_timeout, :run_deadline, :cancel_token, :parent_call_id, :on_event, :event_subscriber?, :event_seq_ref, :event_correlation_id, :event_turn).

run_tool(tool_call, state, opts \\ [])

@spec run_tool(map(), Alloy.Agent.State.t(), keyword()) ::
  {:ok, map(), map() | nil} | {:halted, String.t()}

Executes a single tool call through the same pipeline as execute_all/4.

Runs :before_tool_call middleware, input required-key and optional JSON Schema validation, supervised worker execution bounded by :tool_timeout and :run_deadline with cooperative :cancel_token cancellation, crash/exit sanitization, incremental :tool_update marshalling back to the caller process, :after_tool_call middleware, result truncation, and :tool_start / :tool_update / :tool_end events.

tool_call is a map with :id, :name, and :input (the same shape returned by Alloy.Message.tool_calls/1).

Options

  • :tool_fns — tool registry map overriding state.tool_fns
  • :tool_timeout — positive integer ms overriding state.config.tool_timeout
  • :run_deadline — monotonic ms deadline; clamped to state.run_deadline when the outer state already carries a deadline
  • :cancel_token — Alloy.Cancel.t() or nil; combined with state.config.cancel_token so passing nil or a child token never disarms outer cancellation
  • :parent_call_id — optional non-empty UTF-8 String.t() attached as :parent_call_id on emitted :tool_start, :tool_update, and :tool_end events, tool telemetry metadata, and returned tool-call metadata
  • :on_event — (event -> any()) or nil, invoked in the caller process with raw {:tool_start | :tool_update | :tool_end, map()} tuples (not wrapped in Alloy.Events v1 envelopes and not rescued; to emit v1 envelopes inside an existing host stream, pass on_event: &Alloy.Events.emit(event_opts, turn, &1) together with :event_subscriber?, :event_seq_ref, :event_correlation_id, and :event_turn from Alloy.Events.normalize_opts/2)
  • :event_subscriber? — boolean or nil enabling :tool_update streaming (defaults to is_function(opts[:on_event], 1))
  • :event_seq_ref, :event_correlation_id, :event_turn — event envelope coordination options (nil uses defaults)

Returns

  • {:ok, result_block, tool_call_meta} — result_block is a normalized tool_result content block (%{type: "tool_result", ...}), and tool_call_meta is the execution metadata map (or nil if the call was not started because :cancel_token was already cancelled or :run_deadline had already expired)
  • {:halted, reason} — :before_tool_call middleware halted execution