Packages

A well-typed, idiomatic Gleam client for Anthropic's Claude API with streaming support and tool use

Current section

Files

Jump to
anthropic_gleam src anthropic@types@tool.erl
Raw

src/anthropic@types@tool.erl

-module(anthropic@types@tool).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/anthropic/types/tool.gleam").
-export([property/1, property_with_description/2, enum_property/2, array_property/2, object_property/3, property_schema_to_json/1, empty_input_schema/0, input_schema/2, input_schema_to_json/1, tool/2, tool_with_description/3, tool_to_json/1, tool_to_json_string/1, tools_to_json/1, tool_choice_to_json/1, auto_choice/0, any_choice/0, tool_name_choice/1, no_tool_choice/0, tool_call/3, tool_success/2, tool_failure/2, tool_result_id/1, is_tool_success/1]).
-export_type([property_schema/0, input_schema/0, tool/0, tool_choice/0, tool_call/0, tool_result/0]).
-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 definition types for the Anthropic Messages API\n"
"\n"
" This module defines types for tool definitions following the Anthropic schema.\n"
" Tools allow Claude to call external functions and receive results.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let weather_tool = Tool(\n"
" name: \"get_weather\",\n"
" description: Some(\"Get the current weather for a location\"),\n"
" input_schema: InputSchema(\n"
" schema_type: \"object\",\n"
" properties: Some([\n"
" #(\"location\", PropertySchema(\n"
" property_type: \"string\",\n"
" description: Some(\"City and state, e.g. 'San Francisco, CA'\"),\n"
" enum_values: None,\n"
" )),\n"
" ]),\n"
" required: Some([\"location\"]),\n"
" ),\n"
" )\n"
" ```\n"
).
-type property_schema() :: {property_schema,
binary(),
gleam@option:option(binary()),
gleam@option:option(list(binary())),
gleam@option:option(property_schema()),
gleam@option:option(list({binary(), property_schema()})),
gleam@option:option(list(binary()))}.
-type input_schema() :: {input_schema,
binary(),
gleam@option:option(list({binary(), property_schema()})),
gleam@option:option(list(binary()))}.
-type tool() :: {tool, binary(), gleam@option:option(binary()), input_schema()}.
-type tool_choice() :: auto | any | {tool_name, binary()} | no_tool.
-type tool_call() :: {tool_call, binary(), binary(), binary()}.
-type tool_result() :: {tool_success, binary(), binary()} |
{tool_failure, binary(), binary()}.
-file("src/anthropic/types/tool.gleam", 53).
?DOC(" Create a simple property schema with just a type\n").
-spec property(binary()) -> property_schema().
property(Property_type) ->
{property_schema, Property_type, none, none, none, none, none}.
-file("src/anthropic/types/tool.gleam", 65).
?DOC(" Create a property schema with type and description\n").
-spec property_with_description(binary(), binary()) -> property_schema().
property_with_description(Property_type, Description) ->
{property_schema,
Property_type,
{some, Description},
none,
none,
none,
none}.
-file("src/anthropic/types/tool.gleam", 80).
?DOC(" Create an enum property schema\n").
-spec enum_property(gleam@option:option(binary()), list(binary())) -> property_schema().
enum_property(Description, Values) ->
{property_schema,
<<"string"/utf8>>,
Description,
{some, Values},
none,
none,
none}.
-file("src/anthropic/types/tool.gleam", 95).
?DOC(" Create an array property schema\n").
-spec array_property(gleam@option:option(binary()), property_schema()) -> property_schema().
array_property(Description, Item_schema) ->
{property_schema,
<<"array"/utf8>>,
Description,
none,
{some, Item_schema},
none,
none}.
-file("src/anthropic/types/tool.gleam", 110).
?DOC(" Create an object property schema with nested properties\n").
-spec object_property(
gleam@option:option(binary()),
list({binary(), property_schema()}),
list(binary())
) -> property_schema().
object_property(Description, Properties, Required) ->
{property_schema,
<<"object"/utf8>>,
Description,
none,
none,
{some, Properties},
{some, Required}}.
-file("src/anthropic/types/tool.gleam", 126).
?DOC(" Encode a PropertySchema to JSON\n").
-spec property_schema_to_json(property_schema()) -> gleam@json:json().
property_schema_to_json(Schema) ->
Base_fields = [{<<"type"/utf8>>,
gleam@json:string(erlang:element(2, Schema))}],
With_description = case erlang:element(3, Schema) of
{some, Desc} ->
lists:append(
Base_fields,
[{<<"description"/utf8>>, gleam@json:string(Desc)}]
);
none ->
Base_fields
end,
With_enum = case erlang:element(4, Schema) of
{some, Values} ->
lists:append(
With_description,
[{<<"enum"/utf8>>,
gleam@json:array(Values, fun gleam@json:string/1)}]
);
none ->
With_description
end,
With_items = case erlang:element(5, Schema) of
{some, Item_schema} ->
lists:append(
With_enum,
[{<<"items"/utf8>>, property_schema_to_json(Item_schema)}]
);
none ->
With_enum
end,
With_properties = case erlang:element(6, Schema) of
{some, Props} ->
Props_json = begin
_pipe = Props,
gleam@list:map(
_pipe,
fun(Pair) ->
{Name, Prop_schema} = Pair,
{Name, property_schema_to_json(Prop_schema)}
end
)
end,
lists:append(
With_items,
[{<<"properties"/utf8>>, gleam@json:object(Props_json)}]
);
none ->
With_items
end,
With_required = case erlang:element(7, Schema) of
{some, Req} ->
lists:append(
With_properties,
[{<<"required"/utf8>>,
gleam@json:array(Req, fun gleam@json:string/1)}]
);
none ->
With_properties
end,
gleam@json:object(With_required).
-file("src/anthropic/types/tool.gleam", 190).
?DOC(" Create an empty input schema (for tools with no parameters)\n").
-spec empty_input_schema() -> input_schema().
empty_input_schema() ->
{input_schema, <<"object"/utf8>>, none, none}.
-file("src/anthropic/types/tool.gleam", 195).
?DOC(" Create an input schema with properties\n").
-spec input_schema(list({binary(), property_schema()}), list(binary())) -> input_schema().
input_schema(Properties, Required) ->
{input_schema, <<"object"/utf8>>, {some, Properties}, {some, Required}}.
-file("src/anthropic/types/tool.gleam", 207).
?DOC(" Encode an InputSchema to JSON\n").
-spec input_schema_to_json(input_schema()) -> gleam@json:json().
input_schema_to_json(Schema) ->
Base_fields = [{<<"type"/utf8>>,
gleam@json:string(erlang:element(2, Schema))}],
With_properties = case erlang:element(3, Schema) of
{some, Props} ->
Props_json = begin
_pipe = Props,
gleam@list:map(
_pipe,
fun(Pair) ->
{Name, Prop_schema} = Pair,
{Name, property_schema_to_json(Prop_schema)}
end
)
end,
lists:append(
Base_fields,
[{<<"properties"/utf8>>, gleam@json:object(Props_json)}]
);
none ->
Base_fields
end,
With_required = case erlang:element(4, Schema) of
{some, Req} ->
lists:append(
With_properties,
[{<<"required"/utf8>>,
gleam@json:array(Req, fun gleam@json:string/1)}]
);
none ->
With_properties
end,
gleam@json:object(With_required).
-file("src/anthropic/types/tool.gleam", 251).
?DOC(" Create a tool with no description\n").
-spec tool(binary(), input_schema()) -> tool().
tool(Name, Input_schema) ->
{tool, Name, none, Input_schema}.
-file("src/anthropic/types/tool.gleam", 256).
?DOC(" Create a tool with a description\n").
-spec tool_with_description(binary(), binary(), input_schema()) -> tool().
tool_with_description(Name, Description, Input_schema) ->
{tool, Name, {some, Description}, Input_schema}.
-file("src/anthropic/types/tool.gleam", 265).
?DOC(" Encode a Tool to JSON\n").
-spec tool_to_json(tool()) -> gleam@json:json().
tool_to_json(T) ->
Base_fields = [{<<"name"/utf8>>, gleam@json:string(erlang:element(2, T))},
{<<"input_schema"/utf8>>, input_schema_to_json(erlang:element(4, T))}],
With_description = case erlang:element(3, T) of
{some, Desc} ->
lists:append(
Base_fields,
[{<<"description"/utf8>>, gleam@json:string(Desc)}]
);
none ->
Base_fields
end,
gleam@json:object(With_description).
-file("src/anthropic/types/tool.gleam", 281).
?DOC(" Convert a tool to a JSON string\n").
-spec tool_to_json_string(tool()) -> binary().
tool_to_json_string(T) ->
_pipe = T,
_pipe@1 = tool_to_json(_pipe),
gleam@json:to_string(_pipe@1).
-file("src/anthropic/types/tool.gleam", 288).
?DOC(" Encode a list of tools to JSON\n").
-spec tools_to_json(list(tool())) -> gleam@json:json().
tools_to_json(Tools) ->
gleam@json:array(Tools, fun tool_to_json/1).
-file("src/anthropic/types/tool.gleam", 309).
?DOC(" Encode a ToolChoice to JSON\n").
-spec tool_choice_to_json(tool_choice()) -> gleam@json:json().
tool_choice_to_json(Choice) ->
case Choice of
auto ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"auto"/utf8>>)}]
);
any ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"any"/utf8>>)}]
);
{tool_name, Name} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"tool"/utf8>>)},
{<<"name"/utf8>>, gleam@json:string(Name)}]
);
no_tool ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"none"/utf8>>)}]
)
end.
-file("src/anthropic/types/tool.gleam", 320).
?DOC(" Create an Auto tool choice\n").
-spec auto_choice() -> tool_choice().
auto_choice() ->
auto.
-file("src/anthropic/types/tool.gleam", 325).
?DOC(" Create an Any tool choice\n").
-spec any_choice() -> tool_choice().
any_choice() ->
any.
-file("src/anthropic/types/tool.gleam", 330).
?DOC(" Create a specific tool choice\n").
-spec tool_name_choice(binary()) -> tool_choice().
tool_name_choice(Name) ->
{tool_name, Name}.
-file("src/anthropic/types/tool.gleam", 335).
?DOC(" Create a NoTool choice\n").
-spec no_tool_choice() -> tool_choice().
no_tool_choice() ->
no_tool.
-file("src/anthropic/types/tool.gleam", 356).
?DOC(" Create a ToolCall\n").
-spec tool_call(binary(), binary(), binary()) -> tool_call().
tool_call(Id, Name, Input) ->
{tool_call, Id, Name, Input}.
-file("src/anthropic/types/tool.gleam", 379).
?DOC(" Create a successful tool result\n").
-spec tool_success(binary(), binary()) -> tool_result().
tool_success(Tool_use_id, Content) ->
{tool_success, Tool_use_id, Content}.
-file("src/anthropic/types/tool.gleam", 384).
?DOC(" Create a failed tool result\n").
-spec tool_failure(binary(), binary()) -> tool_result().
tool_failure(Tool_use_id, Error) ->
{tool_failure, Tool_use_id, Error}.
-file("src/anthropic/types/tool.gleam", 389).
?DOC(" Get the tool_use_id from a ToolResult\n").
-spec tool_result_id(tool_result()) -> binary().
tool_result_id(Result) ->
case Result of
{tool_success, Tool_use_id, _} ->
Tool_use_id;
{tool_failure, Tool_use_id@1, _} ->
Tool_use_id@1
end.
-file("src/anthropic/types/tool.gleam", 397).
?DOC(" Check if a tool result is successful\n").
-spec is_tool_success(tool_result()) -> boolean().
is_tool_success(Result) ->
case Result of
{tool_success, _, _} ->
true;
{tool_failure, _, _} ->
false
end.