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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191package acp
import ( "context" "slices" "strings" "time"
"albedo/cli/internal/daemon" "albedo/cli/internal/daemon/protocol"
sdk "github.com/coder/acp-go-sdk")
type resourceMask uint8
const ( titleResource resourceMask = 1 << iota commandsResource planResource allResources = titleResource | commandsResource | planResource)
// One worker per attachment reads the latest resource snapshots in sequence.// Both the dirty mask and the last emitted values are protected by session.mu.type sessionResources struct { title string commands []sdk.AvailableCommand plan []sdk.PlanEntry pending resourceMask sent resourceMask reading bool}
func (s *session) invalidateResources(event daemon.ResourceInvalidation) { base := "/sessions/" + s.id switch { case event.Session && event.URL == base: s.refreshResources(titleResource) case event.Catalog && event.URL == base+"/catalog": s.refreshResources(commandsResource) case event.Extension && event.URL == base+"/acp-plan": s.refreshResources(planResource) }}
// refreshResources requires session.mu, or exclusive ownership during setup.func (s *session) refreshResources(dirty resourceMask) { s.resources.pending |= dirty if s.client == nil || s.resources.reading || s.resources.pending == 0 || s.observation == buffering || s.observation == lost { return } s.resources.reading = true go s.readResources()}
func (s *session) readResources() { for { s.mu.Lock() pending := s.resources.pending if pending == 0 || s.observation == buffering || s.observation == lost || s.ctx.Err() != nil { s.resources.reading = false s.mu.Unlock() return } s.resources.pending = 0 s.mu.Unlock()
// Clear the dirty mask before I/O so invalidations received during a // read cause another pass. There is no second read that can overtake it. for _, resource := range []resourceMask{titleResource, commandsResource, planResource} { if pending&resource != 0 { s.readResource(resource) } } }}
func (s *session) readResource(resource resourceMask) { ctx, cancel := context.WithTimeout(s.ctx, 10*time.Second) defer cancel() var apply func() var err error switch resource { case titleResource: var info daemon.Session info, err = s.client.SessionInfo(ctx) apply = func() { s.publishTitle(info.Title) } case commandsResource: var commands []daemon.SessionCommand commands, err = s.client.SessionCommands(ctx) apply = func() { s.advertiseCommands(commands) } case planResource: var plan protocol.ACPPlan plan, err = s.client.ACPPlan(ctx) apply = func() { s.publishPlan(plan) } } s.mu.Lock() defer s.mu.Unlock() switch { case s.observation == lost || s.ctx.Err() != nil: return case s.observation == buffering: s.resources.pending |= resource case err != nil: s.adapter.note("session %s: could not refresh %s: %v", s.id, resource.name(), err) // A failed first catalog read still leaves the local command usable. // Keep the last known skill commands on subsequent failures. if resource == commandsResource && s.resources.sent&commandsResource == 0 { s.advertiseCommands(nil) } default: apply() }}
func (resource resourceMask) name() string { switch resource { case commandsResource: return "skill commands" case planResource: return "checklist" default: return "title" }}
func (s *session) publishTitle(title string) { if s.resources.sent&titleResource != 0 && s.resources.title == title { return } s.resources.title = title s.resources.sent |= titleResource update := sdk.SessionSessionInfoUpdate{SessionUpdate: string(updateSessionInfo), Title: sdk.PatchNull[string]()} if title != "" { update.Title = sdk.PatchSet(title) } s.update(update)}
func (s *session) advertiseCommands(skills []daemon.SessionCommand) { commands := []sdk.AvailableCommand{{Name: compactCommand, Description: "Compact this session's context and keep a summary"}} for _, skill := range skills { if skill.SkillName != "" { commands = append(commands, sdk.AvailableCommand{ Name: strings.TrimPrefix(skill.Name, "/"), Description: skill.Description, Input: &sdk.AvailableCommandInput{Unstructured: &sdk.UnstructuredCommandInput{Hint: "instructions"}}, }) } } // Discovery and loaded catalogs can present the same skills in different // orders. Keep the local command first and sort the skill commands. slices.SortFunc(commands[1:], func(a, b sdk.AvailableCommand) int { return strings.Compare(a.Name, b.Name) }) if slices.EqualFunc(s.resources.commands, commands, func(a, b sdk.AvailableCommand) bool { // Input is fixed for each non-compact command. return a.Name == b.Name && a.Description == b.Description }) { return } s.resources.commands = commands s.resources.sent |= commandsResource s.update(sdk.SessionAvailableCommandsUpdate{SessionUpdate: string(updateCommands), AvailableCommands: commands})}
func (s *session) publishPlan(plan protocol.ACPPlan) { entries := make([]sdk.PlanEntry, 0, len(plan.Items)) for _, item := range plan.Items { status := sdk.PlanEntryStatusPending if item.Done { status = sdk.PlanEntryStatusCompleted } entries = append(entries, sdk.PlanEntry{Content: item.Text, Priority: sdk.PlanEntryPriorityMedium, Status: status}) } if s.resources.sent&planResource != 0 && slices.EqualFunc(s.resources.plan, entries, func(a, b sdk.PlanEntry) bool { return a.Content == b.Content && a.Status == b.Status && a.Priority == b.Priority }) { return } s.resources.plan = entries s.resources.sent |= planResource if s.planUpdates { s.update(sdk.SessionPlanUpdate{SessionUpdate: "plan_update", Plan: sdk.PlanUpdateContent{Items: &sdk.PlanUpdateContentItems{PlanId: "checklist", Type: "items", Entries: entries}}}) return } s.update(sdk.SessionUpdatePlan{SessionUpdate: string(updatePlan), Entries: entries})}