-module(anthropic@types@request). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/anthropic/types/request.gleam"). -export([stop_reason_to_string/1, stop_reason_to_json/1, stop_reason_from_string/1, usage/2, usage_to_json/1, metadata_to_json/1, 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, response_to_json/1, response_to_json_string/1]). -export_type([stop_reason/0, usage/0, metadata/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 create_message_request() :: {create_message_request, binary(), list(anthropic@types@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@types@tool:tool())), gleam@option:option(anthropic@types@tool:tool_choice())}. -type create_message_response() :: {create_message_response, binary(), binary(), anthropic@types@message:role(), list(anthropic@types@message:content_block()), binary(), gleam@option:option(stop_reason()), gleam@option:option(binary()), usage()}. -file("src/anthropic/types/request.gleam", 35). ?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/types/request.gleam", 45). ?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/types/request.gleam", 50). ?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/types/request.gleam", 75). ?DOC(" Create a Usage from input and output token counts\n"). -spec usage(integer(), integer()) -> usage(). usage(Input_tokens, Output_tokens) -> {usage, Input_tokens, Output_tokens}. -file("src/anthropic/types/request.gleam", 80). ?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/types/request.gleam", 100). ?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/types/request.gleam", 142). ?DOC(" Create a basic request with required fields only\n"). -spec create_request( binary(), list(anthropic@types@message:message()), integer() ) -> create_message_request(). create_request(Model, Messages, Max_tokens) -> {create_message_request, Model, Messages, Max_tokens, none, none, none, none, none, none, none, none, none}. -file("src/anthropic/types/request.gleam", 164). ?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/types/request.gleam", 172). ?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/types/request.gleam", 180). ?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/types/request.gleam", 188). ?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/types/request.gleam", 196). ?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/types/request.gleam", 204). ?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/types/request.gleam", 212). ?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/types/request.gleam", 220). ?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/types/request.gleam", 231). ?DOC(" Set tools on a request\n"). -spec with_tools(create_message_request(), list(anthropic@types@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/types/request.gleam", 239). ?DOC(" Set tool choice on a request\n"). -spec with_tool_choice( create_message_request(), anthropic@types@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/types/request.gleam", 247). ?DOC(" Set tools and tool choice on a request (convenience function)\n"). -spec with_tools_and_choice( create_message_request(), list(anthropic@types@tool:tool()), anthropic@types@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/types/request.gleam", 286). -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/types/request.gleam", 297). -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/types/request.gleam", 308). -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/types/request.gleam", 319). -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/types/request.gleam", 330). -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/types/request.gleam", 341). -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/types/request.gleam", 352). -spec add_optional_tools( list({binary(), gleam@json:json()}), binary(), gleam@option:option(list(anthropic@types@tool:tool())) ) -> list({binary(), gleam@json:json()}). add_optional_tools(Fields, Key, Value) -> case Value of {some, T} -> lists:append(Fields, [{Key, anthropic@types@tool:tools_to_json(T)}]); none -> Fields end. -file("src/anthropic/types/request.gleam", 363). -spec add_optional_tool_choice( list({binary(), gleam@json:json()}), binary(), gleam@option:option(anthropic@types@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@types@tool:tool_choice_to_json(Tc)}] ); none -> Fields end. -file("src/anthropic/types/request.gleam", 256). ?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@types@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/types/request.gleam", 279). ?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/types/request.gleam", 401). ?DOC(" Create a response (primarily for testing)\n"). -spec create_response( binary(), list(anthropic@types@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/types/request.gleam", 421). ?DOC(" Create a response with a stop sequence\n"). -spec create_response_with_stop_sequence( binary(), list(anthropic@types@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/types/request.gleam", 442). ?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/types/request.gleam", 454). ?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/types/request.gleam", 464). ?DOC(" Get all tool use blocks from a response\n"). -spec response_get_tool_uses(create_message_response()) -> list(anthropic@types@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/types/request.gleam", 476). ?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@types@message:role_to_string( erlang:element(4, Response) ) )}, {<<"content"/utf8>>, gleam@json:array( erlang:element(5, Response), fun anthropic@types@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/types/request.gleam", 504). ?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).