Packages

An LLM interaction library that makes you feel like a star

Current section

Files

Jump to
starlet src examples@utils.erl
Raw

src/examples@utils.erl

-module(examples@utils).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/utils.gleam").
-export([error_to_string/1, weather_decoder/0, get_weather/1, multiply_decoder/0, multiply/1]).
-export_type([multiply_args/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(false).
-type multiply_args() :: {multiply_args, integer(), integer()}.
-file("src/examples/utils.gleam", 8).
?DOC(false).
-spec error_to_string(starlet:starlet_error()) -> binary().
error_to_string(Err) ->
case Err of
{transport, Msg} ->
<<"Transport error: "/utf8, Msg/binary>>;
{http, Status, Body} ->
<<<<<<"HTTP "/utf8, (erlang:integer_to_binary(Status))/binary>>/binary,
": "/utf8>>/binary,
Body/binary>>;
{decode, Msg@1} ->
<<"Decode error: "/utf8, Msg@1/binary>>;
{provider, Provider, Msg@2, _} ->
<<<<Provider/binary, " error: "/utf8>>/binary, Msg@2/binary>>;
{tool, Tool_err} ->
case Tool_err of
{not_found, Name} ->
<<"Tool not found: "/utf8, Name/binary>>;
{invalid_arguments, Msg@3} ->
<<"Invalid arguments: "/utf8, Msg@3/binary>>;
{execution_failed, Msg@4} ->
<<"Tool execution failed: "/utf8, Msg@4/binary>>
end;
{rate_limited, Retry_after} ->
case Retry_after of
{some, Seconds} ->
<<<<"Rate limited, retry after "/utf8,
(erlang:integer_to_binary(Seconds))/binary>>/binary,
"s"/utf8>>;
none ->
<<"Rate limited"/utf8>>
end
end.
-file("src/examples/utils.gleam", 30).
?DOC(false).
-spec weather_decoder() -> gleam@dynamic@decode:decoder(binary()).
weather_decoder() ->
gleam@dynamic@decode:field(
<<"city"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(City) -> gleam@dynamic@decode:success(City) end
).
-file("src/examples/utils.gleam", 35).
?DOC(false).
-spec get_weather(binary()) -> {ok, gleam@json:json()} |
{error, starlet@tool:tool_error()}.
get_weather(City) ->
Weather = case City of
<<"Tokyo"/utf8>> ->
gleam@json:object(
[{<<"temperature"/utf8>>, gleam@json:int(18)},
{<<"condition"/utf8>>, gleam@json:string(<<"cloudy"/utf8>>)},
{<<"humidity"/utf8>>, gleam@json:int(65)}]
);
<<"Paris"/utf8>> ->
gleam@json:object(
[{<<"temperature"/utf8>>, gleam@json:int(22)},
{<<"condition"/utf8>>, gleam@json:string(<<"sunny"/utf8>>)},
{<<"humidity"/utf8>>, gleam@json:int(45)}]
);
<<"London"/utf8>> ->
gleam@json:object(
[{<<"temperature"/utf8>>, gleam@json:int(14)},
{<<"condition"/utf8>>, gleam@json:string(<<"rainy"/utf8>>)},
{<<"humidity"/utf8>>, gleam@json:int(80)}]
);
_ ->
gleam@json:object(
[{<<"temperature"/utf8>>, gleam@json:int(20)},
{<<"condition"/utf8>>,
gleam@json:string(<<"partly cloudy"/utf8>>)},
{<<"humidity"/utf8>>, gleam@json:int(50)}]
)
end,
{ok, Weather}.
-file("src/examples/utils.gleam", 69).
?DOC(false).
-spec multiply_decoder() -> gleam@dynamic@decode:decoder(multiply_args()).
multiply_decoder() ->
gleam@dynamic@decode:field(
<<"a"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(A) ->
gleam@dynamic@decode:field(
<<"b"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(B) ->
gleam@dynamic@decode:success({multiply_args, A, B})
end
)
end
).
-file("src/examples/utils.gleam", 75).
?DOC(false).
-spec multiply(multiply_args()) -> {ok, gleam@json:json()} |
{error, starlet@tool:tool_error()}.
multiply(Args) ->
{ok,
gleam@json:object(
[{<<"result"/utf8>>,
gleam@json:int(
erlang:element(2, Args) * erlang:element(3, Args)
)}]
)}.