-module(pig@tool@execution). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/pig/tool/execution.gleam"). -export([execute_tool/2]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " Pure tool execution.\n" "\n" " Looks up a tool in the registry, parses the tool call's JSON arguments\n" " into `dynamic.Dynamic`, and calls the handler. Returns structured errors\n" " for unknown tools, malformed JSON, or handler failures. Never crashes.\n" ). -file("src/pig/tool/execution.gleam", 18). ?DOC( " Execute a tool call against the registry.\n" "\n" " Parses `arguments_json` into `dynamic.Dynamic` before passing to the handler.\n" " Returns:\n" " - `Ok(json.Json)` on successful execution\n" " - `Error(ToolError)` for unknown tools, malformed args, or handler errors\n" ). -spec execute_tool(pig@tool:tool_registry(), pig_protocol@message:tool_call()) -> {ok, gleam@json:json()} | {error, pig@tool:tool_error()}. execute_tool(Registry, Call) -> case pig@tool:lookup(Registry, erlang:element(3, Call)) of {ok, T} -> case gleam@json:parse( erlang:element(4, Call), {decoder, fun gleam@dynamic@decode:decode_dynamic/1} ) of {ok, Args} -> (erlang:element(3, T))(Args); {error, _} -> {error, {tool_error, <<<<"invalid JSON arguments for tool \""/utf8, (erlang:element(3, Call))/binary>>/binary, "\""/utf8>>}} end; {error, nil} -> {error, {tool_error, <<<<"unknown tool \""/utf8, (erlang:element(3, Call))/binary>>/binary, "\""/utf8>>}} end.