Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133//// `/kernel`: the session command that reports, upgrades, and restarts a//// session's python kernel, and the client bindings that reach it over HTTP.//// Every session composes it with the python tool, whatever its selection.
import albedo/harness/sdk/client_apiimport albedo/harness/sdk/commandimport gleam/dictimport gleam/http.{Get, Post}import gleam/jsonimport gleam/option.{None}import gleam/resultimport gleam/string
/// The client bindings of `/kernel`: the session's kernel status, and the/// upgrade or restart route its `{action}` names.pub fn bindings() -> List(client_api.Command) { [ client_api.Command( "/kernel", client_api.Read, [], client_api.Operation( ..client_api.operation_defaults( "getSession", Get, "/sessions/{session_id}", 200, ), path: [#("session_id", client_api.Session("/id"))], query: [#("tail", client_api.Literal(json.int(0)))], result_schema: json.object([ #("type", json.string("object")), #("required", json.array(["kernel", "cursor"], json.string)), ]), ), ), // `{action}` names the route: kernel/upgrade or kernel/restart. client_api.Command( "/kernel", client_api.Mutation, [ client_api.Field( ..client_api.choice_field("action", True, kernel_actions), description: kernel_action_description, ), ], client_api.Operation( ..client_api.operation_defaults( "kernelAction", Post, "/sessions/{session_id}/kernel/{action}", 200, ), path: [ #("session_id", client_api.Session("/id")), #("action", client_api.Form("/action")), ], result_schema: json.object([ #("type", json.string("object")), #("required", json.array(["old_kernel_id"], json.string)), ]), ), ), ]}
const kernel_actions = ["upgrade", "restart"]
const model_restart_only = "the model may only restart a child's or peer's kernel: /kernel restart <session>"
const kernel_action_description = "upgrade keeps variables on current code; restart starts an empty kernel"
/// `/kernel` shows whether the session's kernel runs older code than the/// daemon; `/kernel upgrade` swaps it now, past its live jobs, and/// `/kernel restart` replaces it with an empty one, or another session's when/// given one. The model may read the status and restart its own children's and/// peers' kernels, but not upgrade or restart its own.pub fn kernel() -> command.Command(command.Invocation) { command.Command( "/kernel", "Show whether this session's python kernel is older than the daemon's bundle or modules, upgrade it now (the namespace carries over, live jobs stop), or restart it (the running turn stops, variables and jobs end). restart <session> restarts another session's kernel; the model may show the status, and restart only its own children's and peers' kernels, e.g. a child whose kernel stopped answering.", [ command.Argument( "action", kernel_action_description <> ", or omit to show", False, kernel_actions, ), command.Argument( "session", "with restart, the session whose kernel to restart: an id, or a child's or peer's name", False, [], ), ], True, False, False, None, fn(ctx: command.Invocation, caller, args) { let action = dict.get(args, "action") |> result.unwrap("") let session = dict.get(args, "session") |> result.unwrap("") case action, session, caller { "restart", "", command.UserCall -> ctx.state(command.KernelRestart) |> result.map_error(command.state_error_text) "restart", "", command.ModelCall -> Error(model_restart_only) "restart", _, _ -> ctx.state(command.KernelRestartOf(session, caller)) |> result.map_error(command.state_error_text) "", "", _ -> ctx.state(command.KernelReport) |> result.map_error(command.state_error_text) _, _, command.ModelCall -> Error(model_restart_only) "upgrade", "", _ -> ctx.state(command.KernelUpgrade) |> result.map_error(command.state_error_text) "", _, _ | "upgrade", _, _ -> Error("only restart takes a session") other, _, _ -> Error( "unknown kernel action " <> other <> "; available: " <> string.join(kernel_actions, ", "), ) } |> result.map(command.Data) }, )}