-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, validate_tool_calls/1]). -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( " Tool execution compatibility API and batch validation.\n" "\n" " `execute_tool` delegates to the canonical dispatch in `pig/tool`; batch\n" " validation remains here because it is used by the agent runtime.\n" ). -file("src/pig/tool/execution.gleam", 15). ?DOC( " Execute a tool call through canonical registry dispatch.\n" "\n" " This compatibility API delegates to `tool.execute_tool`, which owns lookup,\n" " argument decoding, context construction, and handler invocation.\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) -> pig@tool:execute_tool(Registry, Call). -file("src/pig/tool/execution.gleam", 32). -spec validate_remaining( list(pig_protocol@message:tool_call()), gleam@dict:dict(binary(), nil), integer() ) -> {ok, nil} | {error, pig@tool:tool_call_batch_error()}. validate_remaining(Calls, Seen, Index) -> case Calls of [] -> {ok, nil}; [Call | _] when erlang:element(2, Call) =:= <<""/utf8>> -> {error, {empty_tool_call_id, Index}}; [Call@1 | Rest] -> case gleam_stdlib:map_get(Seen, erlang:element(2, Call@1)) of {ok, _} -> {error, {duplicate_tool_call_id, erlang:element(2, Call@1)}}; {error, nil} -> validate_remaining( Rest, gleam@dict:insert(Seen, erlang:element(2, Call@1), nil), Index + 1 ) end end. -file("src/pig/tool/execution.gleam", 26). ?DOC( " Validate a batch before hooks, telemetry, or tool processes are started.\n" "\n" " The first invalidity is selected left-to-right: an empty ID at its index,\n" " or the second occurrence of a duplicated ID. Empty batches are valid.\n" ). -spec validate_tool_calls(list(pig_protocol@message:tool_call())) -> {ok, nil} | {error, pig@tool:tool_call_batch_error()}. validate_tool_calls(Calls) -> validate_remaining(Calls, maps:new(), 0).