ToolCall.Runner (ToolCall v0.4.0)

Copy Markdown

Validates and dispatches a decoded argument map to the tool function.

Validation is generated from the same IR that produced the schema, so the parameter set, its names and its types cannot drift between the two.

Summary

Functions

Executes a call produced by resolve/4.

Resolves a tool call without invoking it.

Internal fast path for already prepared tools.

Functions

execute(call, opts \\ [])

@spec execute(ToolCall.Call.t(), keyword()) ::
  {:ok, term()} | {:pending, ToolCall.Approval.Request.t()} | {:error, term()}

Executes a call produced by resolve/4.

The call's timeout applies unless :timeout is given. A stored timeout policy is evaluated here, after the approval check, with the call's raw args and the effective context. :task_supervisor is passed to ToolCall.Timeout.run/3. The stored approval policy is checked unless :approval overrides it; a {:required, reason} answer returns {:pending, %ToolCall.Approval.Request{}} without invoking. The call's context (set by resolve/4) is passed to the policies unless :context overrides it.

resolve(module, tool, args, opts \\ [])

@spec resolve(module(), ToolCall.IR.Tool.t(), map(), keyword()) ::
  {:ok, ToolCall.Call.t()} | {:error, term()}

Resolves a tool call without invoking it.

Runs the key checks, coercion and positional construction and answers a ToolCall.Call that execute/2 can run later. The tool is not invoked. The module's timeout default, the caller's :timeout or an optional ToolCall.Timeout policy is stored in the call; a policy is evaluated by execute/2, after approval. :context is stored for the approval, audit and timeout policies. Module-level timeout, approval and audit policies are loaded on demand. A hand-built tool is validated first; invalid IR raises ArgumentError.

run(module, tools, name, args, opts \\ [])

@spec run(module(), [ToolCall.IR.Tool.t()], atom() | String.t(), term(), keyword()) ::
  {:ok, term()} | {:pending, ToolCall.Approval.Request.t()} | {:error, term()}

Runs a tool by name.

args uses string keys, as JSON decodes them, including nested object values; keyword(t) values also accept atom keys because a keyword list is their value domain. Invalid UTF-8 binaries in string arguments become {:invalid_argument, {name, {:invalid_utf8, value}}} errors.

Runtime tool lists are validated before dispatch. Compiled tools can use run_prepared/4 to skip that validation.

run_prepared(module, tools, name, args, opts \\ [])

@spec run_prepared(
  module(),
  [ToolCall.IR.Tool.t()],
  atom() | String.t(),
  term(),
  keyword()
) ::
  {:ok, term()} | {:pending, ToolCall.Approval.Request.t()} | {:error, term()}

Internal fast path for already prepared tools.

This skips runtime IR validation. Callers must pass tools prepared by ToolCall.Typespec.validate_tool!/1; use run/4 for arbitrary runtime tools.