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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118package daemon
import ( "cmp" "context" "maps" "slices"
"albedo/cli/internal/config" "albedo/cli/internal/daemon/protocol")
// The rotation modes a service's accounts take. A profile stores one of// them beside its account_ids.const ( RotationFailover = "failover" RotationSpread = "spread" RotationFirst = "first")
// ServiceRotation is how a service's sign-in accounts are used, read from// its profiles the way every client reads it: the lead profile is the// default when it runs this extension, else the first by name with an// account list, else the first by name. The order is the lead's list (ids// that still exist) followed by the service's other accounts in the order// given; the mode is the lead's rotation when it has a list. A lead with one// account, pinned or automatic, never switches, which reads as first.func ServiceRotation(extension string, profiles config.Profiles, accounts []string) (order []string, mode string) { lead, ok := leadProfile(extension, profiles) mode = RotationFailover if ok { for _, id := range lead.AccountIDs { if slices.Contains(accounts, id) && !slices.Contains(order, id) { order = append(order, id) } } switch { case len(lead.AccountIDs) > 0: mode = cmp.Or(lead.Rotation, RotationFailover) case len(accounts) > 1: mode = RotationFirst } } for _, id := range accounts { if !slices.Contains(order, id) { order = append(order, id) } } return order, mode}
func leadProfile(extension string, profiles config.Profiles) (config.Settings, bool) { if s, ok := profiles.Providers[profiles.Active]; ok && profileExtension(s) == extension { return s, true } names := slices.Sorted(maps.Keys(profiles.Providers)) var first *config.Settings for _, name := range names { s := profiles.Providers[name] if profileExtension(s) != extension { continue } if len(s.AccountIDs) > 0 { return s, true } if first == nil { first = &s } } if first == nil { return config.Settings{}, false } return *first, true}
func profileExtension(s config.Settings) string { return cmp.Or(s.Extension, "openai") }
// RotationTargets are the profiles a service's rotation is written to: every// profile of the extension except those pinned to one account, an override// kept under advanced setups, and those holding a key of their own, which// do not sign in.func RotationTargets(extension string, profiles config.Profiles) []string { var names []string for _, name := range slices.Sorted(maps.Keys(profiles.Providers)) { s := profiles.Providers[name] if profileExtension(s) == extension && s.AccountID == nil && !s.HasKey { names = append(names, name) } } return names}
// RotationPatch is the providers settings patch that gives a service the// order and mode: every target profile takes the list and the mode, and// drops any pin. It is nil when no profile would change.func RotationPatch(extension string, profiles config.Profiles, order []string, mode string) *protocol.ProviderSettingsPatch { targets := RotationTargets(extension, profiles) if len(targets) == 0 { return nil } patch := map[string]*protocol.ProviderProfilePatch{} for _, name := range targets { patch[name] = &protocol.ProviderProfilePatch{AccountIDs: protocol.SetNullablePatch(slices.Clone(order)), Rotation: protocol.SetPatch(mode), AccountID: protocol.ClearPatch[string]()} } return &protocol.ProviderSettingsPatch{Profiles: protocol.SetPatch(patch)}}
// SaveServiceRotation writes a service's order and mode in one patch. It// reports false when no profile runs the service, so nothing was written.func SaveServiceRotation(ctx context.Context, conn *Connection, extension string, profiles config.Profiles, order []string, mode string) (bool, error) { body := RotationPatch(extension, profiles, order, mode) if body == nil { return false, nil } var change protocol.ProviderSettingsChange return true, patchSettingsGroup(ctx, conn, "providers", profiles.ETag, body, &change)}