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@request.erl
Raw

src/anthropic@request.erl

-module(anthropic@request).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/anthropic/request.gleam").
-export([stop_reason_to_string/1, stop_reason_to_json/1, stop_reason_from_string/1, usage_to_json/1, metadata_to_json/1, options/0, opt_max_tokens/2, opt_system/2, opt_temperature/2, opt_top_p/2, opt_top_k/2, opt_stop_sequences/2, opt_stream/2, opt_metadata/2, opt_user_id/2, opt_tools/2, opt_tool_choice/2, opt_tools_and_choice/3, new/3, new_with/3, get_options/1, apply_options/2, create_request/3, with_system/2, with_temperature/2, with_top_p/2, with_top_k/2, with_stop_sequences/2, with_stream/2, with_metadata/2, with_user_id/2, with_tools/2, with_tool_choice/2, with_tools_and_choice/3, request_to_json/1, request_to_json_string/1, create_response/5, create_response_with_stop_sequence/6, response_text/1, response_has_tool_use/1, response_get_tool_uses/1, needs_tool_execution/1, get_pending_tool_calls/1, response_to_json/1, response_to_json_string/1]).
-export_type([stop_reason/0, usage/0, metadata/0, request_options/0, create_message_request/0, create_message_response/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(
" API request and response types for the Anthropic Messages API\n"
"\n"
" This module defines the types for creating message requests and\n"
" parsing message responses from Claude's API.\n"
).
-type stop_reason() :: end_turn | max_tokens | stop_sequence | tool_use.
-type usage() :: {usage, integer(), integer()}.
-type metadata() :: {metadata, gleam@option:option(binary())}.
-type request_options() :: {request_options,
integer(),
gleam@option:option(binary()),
gleam@option:option(float()),
gleam@option:option(float()),
gleam@option:option(integer()),
gleam@option:option(list(binary())),
gleam@option:option(boolean()),
gleam@option:option(metadata()),
gleam@option:option(list(anthropic@tool:tool())),
gleam@option:option(anthropic@tool:tool_choice())}.
-type create_message_request() :: {create_message_request,
binary(),
list(anthropic@message:message()),
integer(),
gleam@option:option(binary()),
gleam@option:option(float()),
gleam@option:option(float()),
gleam@option:option(integer()),
gleam@option:option(list(binary())),
gleam@option:option(boolean()),
gleam@option:option(metadata()),
gleam@option:option(list(anthropic@tool:tool())),
gleam@option:option(anthropic@tool:tool_choice())}.
-type create_message_response() :: {create_message_response,
binary(),
binary(),
anthropic@message:role(),
list(anthropic@message:content_block()),
binary(),
gleam@option:option(stop_reason()),
gleam@option:option(binary()),
usage()}.
-file("src/anthropic/request.gleam", 36).
?DOC(" Convert a StopReason to its JSON string representation\n").
-spec stop_reason_to_string(stop_reason()) -> binary().
stop_reason_to_string(Reason) ->
case Reason of
end_turn ->
<<"end_turn"/utf8>>;
max_tokens ->
<<"max_tokens"/utf8>>;
stop_sequence ->
<<"stop_sequence"/utf8>>;
tool_use ->
<<"tool_use"/utf8>>
end.
-file("src/anthropic/request.gleam", 46).
?DOC(" Encode a StopReason to JSON\n").
-spec stop_reason_to_json(stop_reason()) -> gleam@json:json().
stop_reason_to_json(Reason) ->
gleam@json:string(stop_reason_to_string(Reason)).
-file("src/anthropic/request.gleam", 51).
?DOC(" Parse a string into a StopReason\n").
-spec stop_reason_from_string(binary()) -> {ok, stop_reason()} |
{error, binary()}.
stop_reason_from_string(Str) ->
case Str of
<<"end_turn"/utf8>> ->
{ok, end_turn};
<<"max_tokens"/utf8>> ->
{ok, max_tokens};
<<"stop_sequence"/utf8>> ->
{ok, stop_sequence};
<<"tool_use"/utf8>> ->
{ok, tool_use};
_ ->
{error, <<"Invalid stop reason: "/utf8, Str/binary>>}
end.
-file("src/anthropic/request.gleam", 76).
?DOC(" Encode Usage to JSON\n").
-spec usage_to_json(usage()) -> gleam@json:json().
usage_to_json(U) ->
gleam@json:object(
[{<<"input_tokens"/utf8>>, gleam@json:int(erlang:element(2, U))},
{<<"output_tokens"/utf8>>, gleam@json:int(erlang:element(3, U))}]
).
-file("src/anthropic/request.gleam", 96).
?DOC(" Encode Metadata to JSON\n").
-spec metadata_to_json(metadata()) -> gleam@json:json().
metadata_to_json(Metadata) ->
case erlang:element(2, Metadata) of
{some, User_id} ->
gleam@json:object(
[{<<"user_id"/utf8>>, gleam@json:string(User_id)}]
);
none ->
gleam@json:object([])
end.
-file("src/anthropic/request.gleam", 161).
?DOC(
" Create default request options\n"
"\n"
" Returns options with max_tokens set to 1024 and all other options as None.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let opts = request.options()\n"
" |> request.opt_temperature(0.8)\n"
" |> request.opt_system(\"Be concise\")\n"
" ```\n"
).
-spec options() -> request_options().
options() ->
{request_options,
1024,
none,
none,
none,
none,
none,
none,
none,
none,
none}.
-file("src/anthropic/request.gleam", 177).
?DOC(" Set max_tokens in options\n").
-spec opt_max_tokens(request_options(), integer()) -> request_options().
opt_max_tokens(Opts, Max_tokens) ->
{request_options,
Max_tokens,
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 182).
?DOC(" Set system prompt in options\n").
-spec opt_system(request_options(), binary()) -> request_options().
opt_system(Opts, System) ->
{request_options,
erlang:element(2, Opts),
{some, System},
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 187).
?DOC(" Set temperature in options\n").
-spec opt_temperature(request_options(), float()) -> request_options().
opt_temperature(Opts, Temperature) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
{some, Temperature},
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 195).
?DOC(" Set top_p in options\n").
-spec opt_top_p(request_options(), float()) -> request_options().
opt_top_p(Opts, Top_p) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
{some, Top_p},
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 200).
?DOC(" Set top_k in options\n").
-spec opt_top_k(request_options(), integer()) -> request_options().
opt_top_k(Opts, Top_k) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
{some, Top_k},
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 205).
?DOC(" Set stop sequences in options\n").
-spec opt_stop_sequences(request_options(), list(binary())) -> request_options().
opt_stop_sequences(Opts, Sequences) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
{some, Sequences},
erlang:element(8, Opts),
erlang:element(9, Opts),
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 213).
?DOC(" Set stream in options\n").
-spec opt_stream(request_options(), boolean()) -> request_options().
opt_stream(Opts, Stream) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
{some, Stream},
erlang:element(9, Opts),
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 218).
?DOC(" Set metadata in options\n").
-spec opt_metadata(request_options(), metadata()) -> request_options().
opt_metadata(Opts, Metadata) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
{some, Metadata},
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 223).
?DOC(" Set user_id in options (creates Metadata automatically)\n").
-spec opt_user_id(request_options(), binary()) -> request_options().
opt_user_id(Opts, User_id) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
{some, {metadata, {some, User_id}}},
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 228).
?DOC(" Set tools in options\n").
-spec opt_tools(request_options(), list(anthropic@tool:tool())) -> request_options().
opt_tools(Opts, Tools) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
{some, Tools},
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 233).
?DOC(" Set tool choice in options\n").
-spec opt_tool_choice(request_options(), anthropic@tool:tool_choice()) -> request_options().
opt_tool_choice(Opts, Choice) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
erlang:element(10, Opts),
{some, Choice}}.
-file("src/anthropic/request.gleam", 241).
?DOC(" Set tools and tool choice in options (convenience function)\n").
-spec opt_tools_and_choice(
request_options(),
list(anthropic@tool:tool()),
anthropic@tool:tool_choice()
) -> request_options().
opt_tools_and_choice(Opts, Tools, Choice) ->
{request_options,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
{some, Tools},
{some, Choice}}.
-file("src/anthropic/request.gleam", 299).
?DOC(
" Create a new message request with required fields only\n"
"\n"
" This is the idiomatic Gleam constructor for CreateMessageRequest.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import anthropic/types/request\n"
" import anthropic/types/message.{user_message}\n"
"\n"
" let req = request.new(\n"
" \"claude-sonnet-4-20250514\",\n"
" [user_message(\"Hello, Claude!\")],\n"
" 1024,\n"
" )\n"
" ```\n"
).
-spec new(binary(), list(anthropic@message:message()), integer()) -> create_message_request().
new(Model, Messages, Max_tokens) ->
{create_message_request,
Model,
Messages,
Max_tokens,
none,
none,
none,
none,
none,
none,
none,
none,
none}.
-file("src/anthropic/request.gleam", 352).
?DOC(
" Create a new message request with options\n"
"\n"
" This allows specifying multiple options at once using a RequestOptions record.\n"
" Useful for config-driven scenarios, copying options between requests, or\n"
" when you have many options to set.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import anthropic/types/request\n"
" import anthropic/types/message.{user_message}\n"
"\n"
" // Create reusable options\n"
" let creative_opts = request.options()\n"
" |> request.opt_system(\"You are a creative writer\")\n"
" |> request.opt_temperature(0.9)\n"
" |> request.opt_max_tokens(2048)\n"
"\n"
" // Use options with new_with\n"
" let req = request.new_with(\n"
" \"claude-sonnet-4-20250514\",\n"
" [user_message(\"Write a poem about stars\")],\n"
" creative_opts,\n"
" )\n"
"\n"
" // Reuse the same options for another request\n"
" let req2 = request.new_with(\n"
" \"claude-sonnet-4-20250514\",\n"
" [user_message(\"Write a poem about the ocean\")],\n"
" creative_opts,\n"
" )\n"
" ```\n"
).
-spec new_with(binary(), list(anthropic@message:message()), request_options()) -> create_message_request().
new_with(Model, Messages, Opts) ->
{create_message_request,
Model,
Messages,
erlang:element(2, Opts),
erlang:element(3, Opts),
erlang:element(4, Opts),
erlang:element(5, Opts),
erlang:element(6, Opts),
erlang:element(7, Opts),
erlang:element(8, Opts),
erlang:element(9, Opts),
erlang:element(10, Opts),
erlang:element(11, Opts)}.
-file("src/anthropic/request.gleam", 387).
?DOC(
" Extract options from an existing request\n"
"\n"
" This allows copying options from one request to use in another.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" // Extract options from an existing request\n"
" let opts = request.get_options(existing_request)\n"
"\n"
" // Modify and use for a new request\n"
" let new_opts = opts |> request.opt_temperature(0.5)\n"
" let new_req = request.new_with(\"claude-sonnet-4-20250514\", messages, new_opts)\n"
" ```\n"
).
-spec get_options(create_message_request()) -> request_options().
get_options(Req) ->
{request_options,
erlang:element(4, Req),
erlang:element(5, Req),
erlang:element(6, Req),
erlang:element(7, Req),
erlang:element(8, Req),
erlang:element(9, Req),
erlang:element(10, Req),
erlang:element(11, Req),
erlang:element(12, Req),
erlang:element(13, Req)}.
-file("src/anthropic/request.gleam", 438).
?DOC(" Helper to merge options - new value takes precedence if Some\n").
-spec merge_option(gleam@option:option(FWV), gleam@option:option(FWV)) -> gleam@option:option(FWV).
merge_option(Existing, New) ->
case New of
{some, _} ->
New;
none ->
Existing
end.
-file("src/anthropic/request.gleam", 417).
?DOC(
" Apply options to an existing request\n"
"\n"
" This merges options into an existing request, overwriting any options\n"
" that are set (not None) in the provided RequestOptions.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let req = request.new(\"claude-sonnet-4-20250514\", messages, 1024)\n"
" let opts = request.options()\n"
" |> request.opt_temperature(0.7)\n"
" |> request.opt_system(\"Be helpful\")\n"
"\n"
" let updated_req = request.apply_options(req, opts)\n"
" ```\n"
).
-spec apply_options(create_message_request(), request_options()) -> create_message_request().
apply_options(Req, Opts) ->
{create_message_request,
erlang:element(2, Req),
erlang:element(3, Req),
erlang:element(2, Opts),
merge_option(erlang:element(5, Req), erlang:element(3, Opts)),
merge_option(erlang:element(6, Req), erlang:element(4, Opts)),
merge_option(erlang:element(7, Req), erlang:element(5, Opts)),
merge_option(erlang:element(8, Req), erlang:element(6, Opts)),
merge_option(erlang:element(9, Req), erlang:element(7, Opts)),
merge_option(erlang:element(10, Req), erlang:element(8, Opts)),
merge_option(erlang:element(11, Req), erlang:element(9, Opts)),
merge_option(erlang:element(12, Req), erlang:element(10, Opts)),
merge_option(erlang:element(13, Req), erlang:element(11, Opts))}.
-file("src/anthropic/request.gleam", 449).
?DOC(
" Create a basic request with required fields only\n"
"\n"
" @deprecated Use `request.new` instead for idiomatic Gleam style\n"
).
-spec create_request(binary(), list(anthropic@message:message()), integer()) -> create_message_request().
create_request(Model, Messages, Max_tokens) ->
new(Model, Messages, Max_tokens).
-file("src/anthropic/request.gleam", 458).
?DOC(" Set the system prompt on a request\n").
-spec with_system(create_message_request(), binary()) -> create_message_request().
with_system(Request, System) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
{some, System},
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 466).
?DOC(" Set the temperature on a request\n").
-spec with_temperature(create_message_request(), float()) -> create_message_request().
with_temperature(Request, Temperature) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
{some, Temperature},
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 474).
?DOC(" Set top_p on a request\n").
-spec with_top_p(create_message_request(), float()) -> create_message_request().
with_top_p(Request, Top_p) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
{some, Top_p},
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 482).
?DOC(" Set top_k on a request\n").
-spec with_top_k(create_message_request(), integer()) -> create_message_request().
with_top_k(Request, Top_k) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
{some, Top_k},
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 490).
?DOC(" Set stop sequences on a request\n").
-spec with_stop_sequences(create_message_request(), list(binary())) -> create_message_request().
with_stop_sequences(Request, Sequences) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
{some, Sequences},
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 498).
?DOC(" Enable streaming on a request\n").
-spec with_stream(create_message_request(), boolean()) -> create_message_request().
with_stream(Request, Stream) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
{some, Stream},
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 506).
?DOC(" Set metadata on a request\n").
-spec with_metadata(create_message_request(), metadata()) -> create_message_request().
with_metadata(Request, Metadata) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
{some, Metadata},
erlang:element(12, Request),
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 514).
?DOC(" Set user_id in metadata on a request\n").
-spec with_user_id(create_message_request(), binary()) -> create_message_request().
with_user_id(Request, User_id) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
{some, {metadata, {some, User_id}}},
erlang:element(12, Request),
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 525).
?DOC(" Set tools on a request\n").
-spec with_tools(create_message_request(), list(anthropic@tool:tool())) -> create_message_request().
with_tools(Request, Tools) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
{some, Tools},
erlang:element(13, Request)}.
-file("src/anthropic/request.gleam", 533).
?DOC(" Set tool choice on a request\n").
-spec with_tool_choice(create_message_request(), anthropic@tool:tool_choice()) -> create_message_request().
with_tool_choice(Request, Choice) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
{some, Choice}}.
-file("src/anthropic/request.gleam", 541).
?DOC(" Set tools and tool choice on a request (convenience function)\n").
-spec with_tools_and_choice(
create_message_request(),
list(anthropic@tool:tool()),
anthropic@tool:tool_choice()
) -> create_message_request().
with_tools_and_choice(Request, Tools, Choice) ->
{create_message_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
{some, Tools},
{some, Choice}}.
-file("src/anthropic/request.gleam", 580).
-spec add_optional_string(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(binary())
) -> list({binary(), gleam@json:json()}).
add_optional_string(Fields, Key, Value) ->
case Value of
{some, V} ->
lists:append(Fields, [{Key, gleam@json:string(V)}]);
none ->
Fields
end.
-file("src/anthropic/request.gleam", 591).
-spec add_optional_float(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(float())
) -> list({binary(), gleam@json:json()}).
add_optional_float(Fields, Key, Value) ->
case Value of
{some, V} ->
lists:append(Fields, [{Key, gleam@json:float(V)}]);
none ->
Fields
end.
-file("src/anthropic/request.gleam", 602).
-spec add_optional_int(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(integer())
) -> list({binary(), gleam@json:json()}).
add_optional_int(Fields, Key, Value) ->
case Value of
{some, V} ->
lists:append(Fields, [{Key, gleam@json:int(V)}]);
none ->
Fields
end.
-file("src/anthropic/request.gleam", 613).
-spec add_optional_bool(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(boolean())
) -> list({binary(), gleam@json:json()}).
add_optional_bool(Fields, Key, Value) ->
case Value of
{some, V} ->
lists:append(Fields, [{Key, gleam@json:bool(V)}]);
none ->
Fields
end.
-file("src/anthropic/request.gleam", 624).
-spec add_optional_string_list(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(list(binary()))
) -> list({binary(), gleam@json:json()}).
add_optional_string_list(Fields, Key, Value) ->
case Value of
{some, V} ->
lists:append(
Fields,
[{Key, gleam@json:array(V, fun gleam@json:string/1)}]
);
none ->
Fields
end.
-file("src/anthropic/request.gleam", 635).
-spec add_optional_metadata(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(metadata())
) -> list({binary(), gleam@json:json()}).
add_optional_metadata(Fields, Key, Value) ->
case Value of
{some, M} ->
lists:append(Fields, [{Key, metadata_to_json(M)}]);
none ->
Fields
end.
-file("src/anthropic/request.gleam", 646).
-spec add_optional_tools(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(list(anthropic@tool:tool()))
) -> list({binary(), gleam@json:json()}).
add_optional_tools(Fields, Key, Value) ->
case Value of
{some, T} ->
lists:append(Fields, [{Key, anthropic@tool:tools_to_json(T)}]);
none ->
Fields
end.
-file("src/anthropic/request.gleam", 657).
-spec add_optional_tool_choice(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(anthropic@tool:tool_choice())
) -> list({binary(), gleam@json:json()}).
add_optional_tool_choice(Fields, Key, Value) ->
case Value of
{some, Tc} ->
lists:append(
Fields,
[{Key, anthropic@tool:tool_choice_to_json(Tc)}]
);
none ->
Fields
end.
-file("src/anthropic/request.gleam", 550).
?DOC(" Encode a CreateMessageRequest to JSON\n").
-spec request_to_json(create_message_request()) -> gleam@json:json().
request_to_json(Request) ->
Required_fields = [{<<"model"/utf8>>,
gleam@json:string(erlang:element(2, Request))},
{<<"messages"/utf8>>,
anthropic@message:messages_to_json(erlang:element(3, Request))},
{<<"max_tokens"/utf8>>, gleam@json:int(erlang:element(4, Request))}],
Optional_fields = begin
_pipe = [],
_pipe@1 = add_optional_string(
_pipe,
<<"system"/utf8>>,
erlang:element(5, Request)
),
_pipe@2 = add_optional_float(
_pipe@1,
<<"temperature"/utf8>>,
erlang:element(6, Request)
),
_pipe@3 = add_optional_float(
_pipe@2,
<<"top_p"/utf8>>,
erlang:element(7, Request)
),
_pipe@4 = add_optional_int(
_pipe@3,
<<"top_k"/utf8>>,
erlang:element(8, Request)
),
_pipe@5 = add_optional_string_list(
_pipe@4,
<<"stop_sequences"/utf8>>,
erlang:element(9, Request)
),
_pipe@6 = add_optional_bool(
_pipe@5,
<<"stream"/utf8>>,
erlang:element(10, Request)
),
_pipe@7 = add_optional_metadata(
_pipe@6,
<<"metadata"/utf8>>,
erlang:element(11, Request)
),
_pipe@8 = add_optional_tools(
_pipe@7,
<<"tools"/utf8>>,
erlang:element(12, Request)
),
add_optional_tool_choice(
_pipe@8,
<<"tool_choice"/utf8>>,
erlang:element(13, Request)
)
end,
gleam@json:object(lists:append(Required_fields, Optional_fields)).
-file("src/anthropic/request.gleam", 573).
?DOC(" Convert a request to a JSON string\n").
-spec request_to_json_string(create_message_request()) -> binary().
request_to_json_string(Request) ->
_pipe = Request,
_pipe@1 = request_to_json(_pipe),
gleam@json:to_string(_pipe@1).
-file("src/anthropic/request.gleam", 695).
?DOC(" Create a response (primarily for testing)\n").
-spec create_response(
binary(),
list(anthropic@message:content_block()),
binary(),
gleam@option:option(stop_reason()),
usage()
) -> create_message_response().
create_response(Id, Content, Model, Stop_reason, U) ->
{create_message_response,
Id,
<<"message"/utf8>>,
assistant,
Content,
Model,
Stop_reason,
none,
U}.
-file("src/anthropic/request.gleam", 715).
?DOC(" Create a response with a stop sequence\n").
-spec create_response_with_stop_sequence(
binary(),
list(anthropic@message:content_block()),
binary(),
stop_reason(),
binary(),
usage()
) -> create_message_response().
create_response_with_stop_sequence(
Id,
Content,
Model,
Stop_reason,
Stop_sequence,
U
) ->
{create_message_response,
Id,
<<"message"/utf8>>,
assistant,
Content,
Model,
{some, Stop_reason},
{some, Stop_sequence},
U}.
-file("src/anthropic/request.gleam", 736).
?DOC(" Get the text content from a response (concatenated)\n").
-spec response_text(create_message_response()) -> binary().
response_text(Response) ->
_pipe = erlang:element(5, Response),
_pipe@1 = gleam@list:filter_map(_pipe, fun(Block) -> case Block of
{text_block, Text} ->
{ok, Text};
_ ->
{error, nil}
end end),
gleam@string:join(_pipe@1, <<""/utf8>>).
-file("src/anthropic/request.gleam", 748).
?DOC(" Check if a response contains tool use blocks\n").
-spec response_has_tool_use(create_message_response()) -> boolean().
response_has_tool_use(Response) ->
gleam@list:any(erlang:element(5, Response), fun(Block) -> case Block of
{tool_use_block, _, _, _} ->
true;
_ ->
false
end end).
-file("src/anthropic/request.gleam", 758).
?DOC(" Get all tool use blocks from a response\n").
-spec response_get_tool_uses(create_message_response()) -> list(anthropic@message:content_block()).
response_get_tool_uses(Response) ->
gleam@list:filter(erlang:element(5, Response), fun(Block) -> case Block of
{tool_use_block, _, _, _} ->
true;
_ ->
false
end end).
-file("src/anthropic/request.gleam", 793).
?DOC(
" Check if response requires tool execution to continue\n"
"\n"
" Returns True if the model stopped because it wants to use a tool.\n"
" This is the signal to extract tool calls, execute them, and continue\n"
" the conversation with tool results.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" case request.needs_tool_execution(response) {\n"
" True -> {\n"
" let calls = request.get_pending_tool_calls(response)\n"
" // Execute tools and continue conversation\n"
" }\n"
" False -> {\n"
" // Response is complete, get the text\n"
" request.response_text(response)\n"
" }\n"
" }\n"
" ```\n"
).
-spec needs_tool_execution(create_message_response()) -> boolean().
needs_tool_execution(Response) ->
case erlang:element(7, Response) of
{some, tool_use} ->
true;
_ ->
false
end.
-file("src/anthropic/request.gleam", 817).
?DOC(
" Extract tool calls that need execution from a response\n"
"\n"
" Returns a list of structured `ToolCall` records ready for execution.\n"
" Each `ToolCall` contains the id, name, and input (as JSON string).\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let calls = request.get_pending_tool_calls(response)\n"
" let results = list.map(calls, fn(call) {\n"
" case call.name {\n"
" \"get_weather\" -> execute_weather(call)\n"
" \"search\" -> execute_search(call)\n"
" _ -> ToolFailure(call.id, \"Unknown tool\")\n"
" }\n"
" })\n"
" ```\n"
).
-spec get_pending_tool_calls(create_message_response()) -> list(anthropic@tool:tool_call()).
get_pending_tool_calls(Response) ->
_pipe = erlang:element(5, Response),
gleam@list:filter_map(_pipe, fun(Block) -> case Block of
{tool_use_block, Id, Name, Input} ->
{ok, {tool_call, Id, Name, Input}};
_ ->
{error, nil}
end end).
-file("src/anthropic/request.gleam", 829).
?DOC(" Encode a response to JSON (for testing/serialization)\n").
-spec response_to_json(create_message_response()) -> gleam@json:json().
response_to_json(Response) ->
Base_fields = [{<<"id"/utf8>>,
gleam@json:string(erlang:element(2, Response))},
{<<"type"/utf8>>, gleam@json:string(erlang:element(3, Response))},
{<<"role"/utf8>>,
gleam@json:string(
anthropic@message:role_to_string(erlang:element(4, Response))
)},
{<<"content"/utf8>>,
gleam@json:array(
erlang:element(5, Response),
fun anthropic@message:content_block_to_json/1
)},
{<<"model"/utf8>>, gleam@json:string(erlang:element(6, Response))},
{<<"usage"/utf8>>, usage_to_json(erlang:element(9, Response))}],
With_stop_reason = case erlang:element(7, Response) of
{some, Reason} ->
lists:append(
Base_fields,
[{<<"stop_reason"/utf8>>, stop_reason_to_json(Reason)}]
);
none ->
Base_fields
end,
With_stop_sequence = case erlang:element(8, Response) of
{some, Seq} ->
lists:append(
With_stop_reason,
[{<<"stop_sequence"/utf8>>, gleam@json:string(Seq)}]
);
none ->
With_stop_reason
end,
gleam@json:object(With_stop_sequence).
-file("src/anthropic/request.gleam", 857).
?DOC(" Convert a response to a JSON string\n").
-spec response_to_json_string(create_message_response()) -> binary().
response_to_json_string(Response) ->
_pipe = Response,
_pipe@1 = response_to_json(_pipe),
gleam@json:to_string(_pipe@1).