package appui import ( "context" "encoding/json" "github.com/egoist/asuka/ui" "github.com/openchamber/openchamber-native/internal/ocapi" ) // eventTypes are the event kinds the list and chat read. Sending the filter // keeps a phone's stream small; server.connected always arrives. var eventTypes = []string{ "session.created", "session.renamed", "session.archived", "session.unarchived", "session.deleted", "session.moved", "session.model.selected", "session.agent.selected", "session.metadata.updated", "session.execution.started", "session.execution.succeeded", "session.execution.failed", "session.execution.interrupted", "session.step.started", "session.step.ended", "session.step.failed", "session.text.started", "session.text.delta", "session.text.ended", "session.reasoning.started", "session.reasoning.delta", "session.reasoning.ended", "session.tool.called", "session.tool.success", "session.tool.failed", "permission.asked", "permission.replied", } type sessionCreatedData struct { SessionID string `json:"sessionID"` ProjectID string `json:"projectID"` Title string `json:"title"` Agent string `json:"agent"` Model *ocapi.ModelRef `json:"model"` Location *ocapi.Location `json:"location"` Subpath string `json:"subpath"` Metadata json.RawMessage `json:"metadata"` } type sessionRenamedData struct { SessionID string `json:"sessionID"` Title string `json:"title"` } type sessionRefData struct { SessionID string `json:"sessionID"` } type sessionModelData struct { SessionID string `json:"sessionID"` Model *ocapi.ModelRef `json:"model"` } type sessionAgentData struct { SessionID string `json:"sessionID"` Agent string `json:"agent"` } type permissionData struct { ID string `json:"id"` SessionID string `json:"sessionID"` Action string `json:"action"` Resources []string `json:"resources"` } // startEvents subscribes to the server's event stream for the given client. // It must run on the main thread (it writes the cancel function). func (c *controller) startEvents(client *ocapi.Client) { if c.cancelSSE != nil { c.cancelSSE() } ctx, cancel := context.WithCancel(context.Background()) c.cancelSSE = cancel sub := ocapi.Subscription{ Types: eventTypes, OnEvent: func(ev ocapi.Event) { ui.Post(func() { if c.client != client { return } c.applyEvent(ev) }) }, OnState: func(st ocapi.SubscriptionState, err error) { switch st { case ocapi.Disconnected: ui.Post(func() { if c.client != client { return } c.status.Set("连接中断,正在重连…") }) case ocapi.Subscribed: // Events missed while the stream was down are gone; // re-read the list and the open chat's tail. ui.Post(func() { if c.client != client { return } c.refresh() if c.chat != nil { c.chat.reloadTail() } }) } }, } go client.Subscribe(ctx, sub) } // applyEvent folds one server event into the list state. Main thread. func (c *controller) applyEvent(ev ocapi.Event) { if c.chat != nil { var ref struct { SessionID string `json:"sessionID"` } if ev.UnmarshalData(&ref) == nil && ref.SessionID == c.chat.id { c.chat.handleEvent(ev) } } switch ev.Type { case "session.created": var d sessionCreatedData if ev.UnmarshalData(&d) != nil || d.SessionID == "" { return } list := c.sessions.Peek() for _, s := range list { if s.ID == d.SessionID { return } } next := make([]ocapi.Session, 0, len(list)+1) next = append(next, ocapi.Session{ ID: d.SessionID, ProjectID: d.ProjectID, Title: d.Title, Agent: d.Agent, Model: d.Model, Location: d.Location, Time: ocapi.SessionTime{Created: ev.Created, Updated: ev.Created}, }) c.sessions.Set(append(next, list...)) case "session.renamed": var d sessionRenamedData if ev.UnmarshalData(&d) != nil { return } c.patchSession(d.SessionID, func(s *ocapi.Session) { s.Title = d.Title }) case "session.model.selected": var d sessionModelData if ev.UnmarshalData(&d) != nil { return } c.patchSession(d.SessionID, func(s *ocapi.Session) { s.Model = d.Model }) case "session.agent.selected": var d sessionAgentData if ev.UnmarshalData(&d) != nil { return } c.patchSession(d.SessionID, func(s *ocapi.Session) { s.Agent = d.Agent }) case "session.archived": var d sessionRefData if ev.UnmarshalData(&d) != nil { return } c.removeSession(d.SessionID) case "session.deleted": var d sessionRefData if ev.UnmarshalData(&d) != nil { return } c.removeSession(d.SessionID) case "session.unarchived", "session.moved": // The event carries no full record; a list read is the truth. c.refresh() case "session.execution.started": var d sessionRefData if ev.UnmarshalData(&d) != nil { return } c.setActive(d.SessionID, true) case "session.execution.succeeded", "session.execution.failed", "session.execution.interrupted": var d sessionRefData if ev.UnmarshalData(&d) != nil { return } c.setActive(d.SessionID, false) case "permission.asked": var d permissionData if ev.UnmarshalData(&d) != nil || d.ID == "" { return } list := c.perms.Peek() for _, p := range list { if p.ID == d.ID { return } } next := make([]ocapi.PermissionRequest, len(list), len(list)+1) copy(next, list) next = append(next, ocapi.PermissionRequest{ ID: d.ID, SessionID: d.SessionID, Action: d.Action, Resources: d.Resources, }) c.perms.Set(next) if c.chat != nil { c.chat.syncPerms() } case "permission.replied": var d struct { RequestID string `json:"requestID"` } if ev.UnmarshalData(&d) != nil || d.RequestID == "" { return } list := c.perms.Peek() next := make([]ocapi.PermissionRequest, 0, len(list)) for _, p := range list { if p.ID != d.RequestID { next = append(next, p) } } if len(next) != len(list) { c.perms.Set(next) if c.chat != nil { c.chat.syncPerms() } } } } func (c *controller) patchSession(id string, patch func(*ocapi.Session)) { list := c.sessions.Peek() next := make([]ocapi.Session, len(list)) copy(next, list) for i := range next { if next[i].ID == id { patch(&next[i]) c.sessions.Set(next) return } } } func (c *controller) removeSession(id string) { list := c.sessions.Peek() next := make([]ocapi.Session, 0, len(list)) for _, s := range list { if s.ID != id { next = append(next, s) } } if len(next) != len(list) { c.sessions.Set(next) } } func (c *controller) setActive(id string, on bool) { cur := c.active.Peek() if cur[id] == on { return } next := make(map[string]bool, len(cur)+1) for k, v := range cur { next[k] = v } if on { next[id] = true } else { delete(next, id) } c.active.Set(next) }