Packages

A library for building stateful chains of LLM interactions!

Current section

Files

Jump to
starflow src starflow@tool.erl
Raw

src/starflow@tool.erl

-module(starflow@tool).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_json/1, result/2]).
-export_type([tool/0, tool_result/0]).
-type tool() :: {tool,
binary(),
binary(),
starflow@schema:schema(),
starflow@schema:schema(),
fun((gleam@dynamic:dynamic_()) -> {ok, tool_result()} |
{error, list(gleam@dynamic:decode_error())})}.
-type tool_result() :: {string, binary()} |
{number, float()} |
{integer, integer()} |
{boolean, boolean()} |
null |
{array, list(tool_result())} |
{object, list({binary(), tool_result()})} |
{enum, binary()}.
-file("/home/ethanthoma/projects/flow/src/starflow/tool.gleam", 107).
-spec to_json(tool()) -> gleam@json:json().
to_json(Tool) ->
{schema, Schema_json} = erlang:element(4, Tool),
gleam@json:object(
[{<<"name"/utf8>>, gleam@json:string(erlang:element(2, Tool))},
{<<"description"/utf8>>, gleam@json:string(erlang:element(3, Tool))},
{<<"input_schema"/utf8>>, Schema_json}]
).
-file("/home/ethanthoma/projects/flow/src/starflow/tool.gleam", 48).
-spec decode_schema(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok,
tool_result()} |
{error, list(gleam@dynamic:decode_error())}.
decode_schema(Schema_json, From) ->
gleam@result:'try'(
begin
_pipe = Schema_json,
(gleam@dynamic:field(<<"type"/utf8>>, fun gleam@dynamic:string/1))(
_pipe
)
end,
fun(Object_type) -> case Object_type of
<<"string"/utf8>> ->
_pipe@1 = gleam@dynamic:string(From),
gleam@result:map(
_pipe@1,
fun(Field@0) -> {string, Field@0} end
);
<<"number"/utf8>> ->
_pipe@2 = gleam@dynamic:float(From),
gleam@result:map(
_pipe@2,
fun(Field@0) -> {number, Field@0} end
);
<<"integer"/utf8>> ->
_pipe@3 = gleam@dynamic:int(From),
gleam@result:map(
_pipe@3,
fun(Field@0) -> {integer, Field@0} end
);
<<"boolean"/utf8>> ->
_pipe@4 = gleam@dynamic:bool(From),
gleam@result:map(
_pipe@4,
fun(Field@0) -> {boolean, Field@0} end
);
<<"null"/utf8>> ->
{ok, null};
<<"array"/utf8>> ->
gleam@result:'try'(
begin
_pipe@5 = Schema_json,
(gleam@dynamic:field(
<<"items"/utf8>>,
fun(Field@0) -> {ok, Field@0} end
))(_pipe@5)
end,
fun(Items_schema) ->
gleam@result:map(
begin
_pipe@6 = From,
(gleam@dynamic:list(
fun(_capture) ->
decode_schema(
Items_schema,
_capture
)
end
))(_pipe@6)
end,
fun(Items) -> {array, Items} end
)
end
);
<<"object"/utf8>> ->
gleam@result:'try'(
begin
_pipe@7 = Schema_json,
(gleam@dynamic:field(
<<"properties"/utf8>>,
gleam@dynamic:dict(
fun gleam@dynamic:string/1,
fun(Field@0) -> {ok, Field@0} end
)
))(_pipe@7)
end,
fun(Dict) ->
gleam@result:map(
begin
_pipe@11 = (gleam@list:map(
begin
_pipe@8 = Dict,
maps:to_list(_pipe@8)
end,
fun(_use0) ->
{Key, Value} = _use0,
gleam@result:'try'(
begin
_pipe@9 = From,
(gleam@dynamic:field(
Key,
fun(Field@0) -> {ok, Field@0} end
))(_pipe@9)
end,
fun(Dyn) ->
gleam@result:map(
begin
_pipe@10 = Dyn,
decode_schema(
_pipe@10,
Value
)
end,
fun(Value@1) ->
{Key, Value@1}
end
)
end
)
end
)),
gleam@result:all(_pipe@11)
end,
fun(List) -> {object, List} end
)
end
);
_ ->
{error,
[{decode_error,
<<"valid schema type"/utf8>>,
Object_type,
[]}]}
end end
).
-file("/home/ethanthoma/projects/flow/src/starflow/tool.gleam", 35).
-spec result({binary(), gleam@dynamic:dynamic_()}, list(tool())) -> {ok,
tool_result()} |
{error, gleam@json:decode_error()}.
result(Tool_use, Tools) ->
{Name, Tool_res} = Tool_use,
_assert_subject = gleam@list:find(
Tools,
fun(Tool) -> Name =:= erlang:element(2, Tool) end
),
{ok, Tool@1} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"starflow/tool"/utf8>>,
function => <<"result"/utf8>>,
line => 38})
end,
{schema, Json} = erlang:element(5, Tool@1),
_pipe = Json,
_pipe@1 = gleam@json:to_string(_pipe),
gleam@json:decode(
_pipe@1,
fun(_capture) -> decode_schema(_capture, Tool_res) end
).