Packages

A spec-compliant Open Responses server for Elixir and Phoenix, with pluggable provider adapters and first-class streaming.

Current section

Files

Jump to
open_responses lib open_responses tool_runner.ex
Raw

lib/open_responses/tool_runner.ex

defmodule OpenResponses.ToolRunner do
@moduledoc """
Dispatches hosted (server-side) tool calls to registered tool modules.
When the model emits a `function_call` whose name is registered in
`:hosted_tools`, the agentic loop calls `ToolRunner.run/4` instead of
yielding the call back to the client. The result is injected as a
`function_call_output` item and the loop continues automatically.
Tool modules are registered in config:
```elixir
config :open_responses, :hosted_tools, %{
"get_weather" => MyApp.Tools.Weather,
"search_docs" => MyApp.Tools.DocSearch
}
```
Each module must implement the `OpenResponses.Tool` behaviour. See that
module and the [Tool Dispatch guide](tool_dispatch.html) for details.
"""
@spec run(binary(), map(), map(), keyword()) ::
{:ok, String.t()} | {:error, term()}
def run(name, arguments, context, opts \\ []) do
registry = Keyword.get(opts, :registry, default_registry())
case Map.fetch(registry, name) do
{:ok, module} -> module.execute(arguments, context)
:error -> {:error, :not_found}
end
end
defp default_registry do
Application.get_env(:open_responses, :hosted_tools, %{})
end
end