Current section
Files
Jump to
Current section
Files
src/pig@supervisor.erl
-module(pig@supervisor).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/pig/supervisor.gleam").
-export([start_supervised/2, start_supervised_with_session_store/3, run_with_timeout/3, run/2, run_continue_with_timeout/2, run_continue/1, stop/1]).
-export_type([supervised_agent/0, start_error/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(
" Supervised agent — wraps agent in OTP static supervisor.\n"
"\n"
" The easy path: `start_supervised(config)` gives you an agent\n"
" managed by a OneForOne supervisor. Advanced users can still\n"
" use `pig.start(config)` for standalone agents.\n"
).
-type supervised_agent() :: {supervised_agent,
gleam@erlang@process:subject(pig@agent@runtime:runtime_msg()),
gleam@erlang@process:pid_()}.
-type start_error() :: {actor_start, gleam@otp@actor:start_error()} |
{session_load, pig@session_store:session_error()}.
-file("src/pig/supervisor.gleam", 89).
-spec start_with_runtime(
list(pig@obs@consumer_spec:consumer_spec()),
fun((gleam@erlang@process:name(pig@obs@dispatcher:dispatcher_message()), gleam@erlang@process:name(pig@agent@runtime:runtime_msg())) -> gleam@otp@supervision:child_specification(nil))
) -> {ok, supervised_agent()} | {error, start_error()}.
start_with_runtime(Consumer_specs, Runtime_spec) ->
Dispatcher_name = gleam_erlang_ffi:new_name(<<"pig_event_dispatcher"/utf8>>),
Agent_name = gleam_erlang_ffi:new_name(<<"pig_agent"/utf8>>),
Event_tree = begin
_pipe = gleam@otp@static_supervisor:new(one_for_all),
_pipe@1 = gleam@otp@static_supervisor:add(
_pipe,
pig@obs@dispatcher:supervised(Dispatcher_name)
),
gleam@list:fold(
Consumer_specs,
_pipe@1,
fun(Builder, Entry) ->
gleam@otp@static_supervisor:add(
Builder,
erlang:element(2, Entry)
)
end
)
end,
App_tree = begin
_pipe@2 = gleam@otp@static_supervisor:new(one_for_one),
_pipe@3 = gleam@otp@static_supervisor:add(
_pipe@2,
gleam@otp@static_supervisor:supervised(Event_tree)
),
gleam@otp@static_supervisor:add(
_pipe@3,
Runtime_spec(Dispatcher_name, Agent_name)
)
end,
case gleam@otp@static_supervisor:start(App_tree) of
{ok, Started} ->
Dispatcher_subject = gleam@erlang@process:named_subject(
Dispatcher_name
),
Agent_subject = gleam@erlang@process:named_subject(Agent_name),
gleam@list:each(
Consumer_specs,
fun(Entry@1) ->
Consumer_subject = gleam@erlang@process:named_subject(
erlang:element(3, Entry@1)
),
gleam@erlang@process:send(
Dispatcher_subject,
{register_consumer, Consumer_subject}
)
end
),
{ok, {supervised_agent, Agent_subject, erlang:element(2, Started)}};
{error, E} ->
{error, {actor_start, E}}
end.
-file("src/pig/supervisor.gleam", 72).
-spec start_with_session(
pig@agent@state:agent_config(),
list(pig@obs@consumer_spec:consumer_spec()),
list(pig_protocol@message:message()),
pig@agent@runtime:session_state()
) -> {ok, supervised_agent()} | {error, start_error()}.
start_with_session(Agent_config, Consumer_specs, Initial_history, Session) ->
start_with_runtime(
Consumer_specs,
fun(Dispatcher_name, Name) ->
pig@agent@runtime:supervised(
Agent_config,
Dispatcher_name,
Name,
Initial_history,
Session
)
end
).
-file("src/pig/supervisor.gleam", 40).
?DOC(
" Start a supervised agent without durable session storage.\n"
"\n"
" This convenience path starts with empty history and no durable session.\n"
).
-spec start_supervised(
pig@agent@state:agent_config(),
list(pig@obs@consumer_spec:consumer_spec())
) -> {ok, supervised_agent()} | {error, start_error()}.
start_supervised(Agent_config, Consumer_specs) ->
start_with_session(Agent_config, Consumer_specs, [], session_disabled).
-file("src/pig/supervisor.gleam", 52).
?DOC(
" Preflight a durable session, then start a supervised agent.\n"
"\n"
" The preflight preserves a typed `SessionLoad` error without starting the\n"
" supervision tree. The runtime independently reloads the store every time\n"
" its worker starts, including after OTP child restarts.\n"
).
-spec start_supervised_with_session_store(
pig@agent@state:agent_config(),
list(pig@obs@consumer_spec:consumer_spec()),
pig@session_store:session_store()
) -> {ok, supervised_agent()} | {error, start_error()}.
start_supervised_with_session_store(Agent_config, Consumer_specs, Store) ->
{session_store, Load, _} = Store,
case Load() of
{error, Error} ->
{error, {session_load, Error}};
{ok, _} ->
start_with_runtime(
Consumer_specs,
fun(Dispatcher_name, Name) ->
pig@agent@runtime:supervised_with_session_store(
Agent_config,
Dispatcher_name,
Name,
Store
)
end
)
end.
-file("src/pig/supervisor.gleam", 141).
?DOC(" Run a prompt against the supervised agent with an explicit timeout.\n").
-spec run_with_timeout(supervised_agent(), binary(), integer()) -> {ok,
pig_protocol@message:message()} |
{error, pig@run_error:run_error()}.
run_with_timeout(Sup, Prompt, Timeout_ms) ->
pig@agent@runtime:run(erlang:element(2, Sup), Prompt, Timeout_ms).
-file("src/pig/supervisor.gleam", 136).
?DOC(" Run a prompt against the supervised agent with a 120-second timeout.\n").
-spec run(supervised_agent(), binary()) -> {ok, pig_protocol@message:message()} |
{error, pig@run_error:run_error()}.
run(Sup, Prompt) ->
run_with_timeout(Sup, Prompt, 120000).
-file("src/pig/supervisor.gleam", 155).
?DOC(" Resume a supervised agent's history with an explicit timeout.\n").
-spec run_continue_with_timeout(supervised_agent(), integer()) -> {ok,
pig_protocol@message:message()} |
{error, pig@run_error:run_error()}.
run_continue_with_timeout(Sup, Timeout_ms) ->
pig@agent@runtime:run_continue(erlang:element(2, Sup), Timeout_ms).
-file("src/pig/supervisor.gleam", 150).
?DOC(" Resume a supervised agent's loaded or interrupted history.\n").
-spec run_continue(supervised_agent()) -> {ok, pig_protocol@message:message()} |
{error, pig@run_error:run_error()}.
run_continue(Sup) ->
run_continue_with_timeout(Sup, 120000).
-file("src/pig/supervisor.gleam", 166).
?DOC(
" Stop the supervised agent.\n"
"\n"
" Sends an exit signal to the supervisor process. OTP cascades\n"
" shutdown to the agent child process.\n"
).
-spec stop(supervised_agent()) -> nil.
stop(Sup) ->
gleam@erlang@process:send_exit(erlang:element(3, Sup)).