Current section
Files
Jump to
Current section
Files
src/pig@skill@librarian.erl
-module(pig@skill@librarian).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/pig/skill/librarian.gleam").
-export([librarian_tool/1]).
-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(
" Librarian tool for skill content reading.\n"
"\n"
" Creates a `Tool` that lets the agent read skill content on demand\n"
" by skill name. The tool handler reads SKILL.md from disk and\n"
" returns it as a JSON string.\n"
).
-file("src/pig/skill/librarian.gleam", 71).
-spec available_names(list(pig@skill:skill())) -> binary().
available_names(Skills) ->
_pipe = Skills,
_pipe@1 = gleam@list:map(_pipe, fun(S) -> erlang:element(2, S) end),
gleam@string:join(_pipe@1, <<", "/utf8>>).
-file("src/pig/skill/librarian.gleam", 62).
-spec read_skill_content(pig@skill:skill()) -> {ok, gleam@json:json()} |
{error, pig@tool:tool_error()}.
read_skill_content(S) ->
Path = <<(erlang:element(4, S))/binary, "/SKILL.md"/utf8>>,
case simplifile:read(Path) of
{ok, Content} ->
{ok, gleam@json:string(Content)};
{error, _} ->
{error,
{tool_error,
<<"Failed to read skill file: "/utf8, Path/binary>>}}
end.
-file("src/pig/skill/librarian.gleam", 55).
-spec find_skill(list(pig@skill:skill()), binary()) -> {ok, pig@skill:skill()} |
{error, nil}.
find_skill(Skills, Name) ->
gleam@list:find(Skills, fun(S) -> erlang:element(2, S) =:= Name end).
-file("src/pig/skill/librarian.gleam", 22).
?DOC(
" Create a librarian tool from a list of loaded skills.\n"
"\n"
" The tool is named `read_skill` and accepts `{\"name\": \"<skill-name>\"}`.\n"
" It reads the SKILL.md content from disk and returns it as JSON.\n"
).
-spec librarian_tool(list(pig@skill:skill())) -> pig@tool:tool().
librarian_tool(Skills) ->
{tool,
{tool_definition,
<<"read_skill"/utf8>>,
<<"Read the full content of a skill by name. "/utf8,
"Returns the SKILL.md content for the requested skill."/utf8>>,
jscheam@schema:object(
[jscheam@schema:prop(<<"name"/utf8>>, jscheam@schema:string())]
)},
fun(Args) ->
case gleam@dynamic@decode:run(
Args,
gleam@dynamic@decode:field(
<<"name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun gleam@dynamic@decode:success/1
)
) of
{ok, Name} ->
case find_skill(Skills, Name) of
{ok, S} ->
read_skill_content(S);
{error, nil} ->
{error,
{tool_error,
<<<<<<"Unknown skill \""/utf8, Name/binary>>/binary,
"\". Available: "/utf8>>/binary,
(available_names(Skills))/binary>>}}
end;
{error, _} ->
{error,
{tool_error,
<<"Invalid arguments: expected {\"name\": \"<skill-name>\"}"/utf8>>}}
end
end}.