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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159package daemon
import ( "context" "io" "net/http" "time"
"albedo/cli/internal/daemon/protocol")
// TransferPreflight says whether a session can move to another daemon, and// why not when it cannot.type TransferPreflight struct { Bytes *int64 Reason string // Host is the target daemon's host as it names itself; Home is its // albedo home there, where its daemon.json is. Host, Home string Sessions []string // RunsThere says, for each of Sessions, whether it can run on the // target; empty when the target was not reached. RunsThere []RunsThere Available bool}
// RunsThere is whether one moving session can run on the target with its// own profile and model. Alternatives are the target's other profiles that// list the same model; the move must name one of them for a session that// is not OK, or it fails.type RunsThere struct { SessionID, Name, Profile, Model string // Reason says why it can't run there, when it can't. Reason string Alternatives []string OK bool}
// Move is one session's transfer to another daemon, as the source daemon// drives it.type Move struct { ID, SessionID, Target string // State is checking, exporting, copying, activating, checklist, active // or failed; Reason says why it waits, when it does. State, Reason string ErrorCode, Error string TargetSessionID string TargetHome string // Lost names what the target could not restore, e.g. "python namespace". Lost []string // Bindings are what still fires on this daemon for the moved sessions, // which finishing the move disarms. Bindings []MoveBinding BytesDone, BytesTotal int64}
// MoveBinding is one schedule, heartbeat, webhook or discord bot that a move// in checklist disarms on the daemon it leaves.type MoveBinding struct { // Kind is schedule, heartbeat, webhook or discord; Detail is the prompt // and cadence, the webhook's name, or the bot and its channels. Kind, ID, Label, Detail string // OldURL and NewURL are a webhook's address here and the one to point // its sender at instead. OldURL, NewURL string // Status is pending, disarmed or failed; Error says why it failed. Status, Error string}
// SessionMovePending marks a session whose move waits in checklist: the// target is live and this copy still runs until the move is finished.type SessionMovePending struct { TransferID, Host string}
func movePendingValue(w *protocol.SessionMovePending) *SessionMovePending { if w == nil { return nil } return &SessionMovePending{TransferID: w.TransferID, Host: w.Host}}
// SessionMoved is the record a moved session keeps on the daemon it left.type SessionMoved struct { At time.Time Host, Workspace, TransferID string}
func movedValue(w *protocol.SessionMoved) *SessionMoved { if w == nil { return nil } at, _ := time.Parse(time.RFC3339Nano, w.At) return &SessionMoved{Host: w.Host, Workspace: w.Workspace, TransferID: w.TransferID, At: at}}
func moveValue(w protocol.Move) Move { m := Move{ID: w.ID, SessionID: w.SessionID, Target: w.Target, State: w.State, Reason: value(w.Reason), BytesDone: w.Progress.BytesDone, BytesTotal: w.Progress.BytesTotal, TargetSessionID: value(w.TargetSessionID), TargetHome: value(w.TargetHome), Lost: w.Lost} if w.Error != nil { m.ErrorCode, m.Error = w.Error.Code, w.Error.Detail } for _, b := range w.Bindings { m.Bindings = append(m.Bindings, MoveBinding{Kind: b.Kind, ID: b.ID, Label: b.Label, Detail: b.Detail, OldURL: value(b.OldURL), NewURL: value(b.NewURL), Status: b.Status, Error: value(b.Error)}) } return m}
// PreflightTransfer asks whether session id can move to target// ([user@]host:/abs). The daemon answers within about ten seconds.func PreflightTransfer(ctx context.Context, conn *Connection, id, target string) (TransferPreflight, error) { var w protocol.TransferPreflight err := executeRead(ctx, conn, operation{Name: "check transfer", BuildRequest: func(base string, _ io.Reader) (*http.Request, error) { return protocol.NewGetTransferPreflightRequest(base, &protocol.GetTransferPreflightParams{SessionID: id, Target: target}) }, Policy: noRecovery, Timeout: 15 * time.Second}, func(data []byte) error { return decodeRequired(data, &w) }) if err != nil { return TransferPreflight{}, err } preflight := TransferPreflight{Available: w.Available, Reason: value(w.Reason), Host: w.TargetDaemon.Host, Home: value(w.TargetDaemon.Home), Bytes: w.Bytes, Sessions: w.Sessions} for _, r := range w.RunsThere { preflight.RunsThere = append(preflight.RunsThere, RunsThere{SessionID: r.SessionID, Name: r.Name, Profile: r.Profile, Model: r.Model, Reason: value(r.Reason), Alternatives: r.Alternatives, OK: r.Ok}) } return preflight, nil}
// NewTransferID is the id a client picks for a move, so asking again with it// joins the same move.func NewTransferID() (string, error) { return operationID() }
// MoveToDaemon starts moving session id and its descendants to the daemon on// target's host. profiles names the target profile, by session id, for each// session whose own profile the target lacks.func MoveToDaemon(ctx context.Context, conn *Connection, id, transferID, target string, profiles map[string]string) (Move, error) { var w protocol.Move err := executeMutation(ctx, conn, operation{Name: "move session", Body: protocol.MoveSessionToDaemonJSONRequestBody{TransferID: transferID, Target: target, Profiles: profiles}, BuildRequest: func(base string, body io.Reader) (*http.Request, error) { return protocol.NewMoveSessionToDaemonRequestWithBody(base, id, "application/json", body) }, Policy: noRecovery}, []int{200, 202}, func(data []byte, _ int) error { return decodeRequired(data, &w) }) return moveValue(w), err}
// GetTransfer reads a move's state.func GetTransfer(ctx context.Context, conn *Connection, transferID string) (Move, error) { var w protocol.Move err := executeRead(ctx, conn, operation{Name: "read transfer", BuildRequest: func(base string, _ io.Reader) (*http.Request, error) { return protocol.NewGetTransferRequest(base, transferID) }, Policy: readRecovery}, func(data []byte) error { return decodeRequired(data, &w) }) return moveValue(w), err}
// FinishTransfer disarms a move's bindings here and marks its sessions moved.// It returns the move after the attempt: active, or still in checklist with// each binding's status and the error when some could not be disarmed.func FinishTransfer(ctx context.Context, conn *Connection, transferID string) (Move, error) { var w protocol.Move err := executeMutation(ctx, conn, operation{Name: "finish move", BuildRequest: func(base string, _ io.Reader) (*http.Request, error) { return protocol.NewFinishTransferRequest(base, transferID) }, Policy: noRecovery}, []int{200}, func(data []byte, _ int) error { return decodeRequired(data, &w) }) return moveValue(w), err}