2026-11-16 09:09:09 +08:00
|
|
|
package appui
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
// eventTypes are the event kinds the list and chat read. Sending the
|
|
|
|
|
// filter keeps a phone's stream small; server.connected always arrives.
|
2026-11-16 09:09:09 +08:00
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
// startStream subscribes to the server's event stream. Call it on the main
|
|
|
|
|
// thread; the stream runs on a goroutine and posts events back.
|
|
|
|
|
func (a *App) startStream() {
|
|
|
|
|
if a.client == nil {
|
|
|
|
|
return
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.stopStream()
|
2026-11-16 09:09:09 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2026-11-18 13:00:20 +08:00
|
|
|
a.streamCancel = cancel
|
2026-11-16 20:04:42 +08:00
|
|
|
wasDown := false
|
2026-11-18 13:00:20 +08:00
|
|
|
client := a.client
|
2026-11-16 09:09:09 +08:00
|
|
|
sub := ocapi.Subscription{
|
|
|
|
|
Types: eventTypes,
|
|
|
|
|
OnEvent: func(ev ocapi.Event) {
|
2026-11-18 13:00:20 +08:00
|
|
|
a.post(func() {
|
|
|
|
|
if a.client != client {
|
2026-11-16 09:09:09 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.applyEvent(ev)
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
OnState: func(st ocapi.SubscriptionState, err error) {
|
|
|
|
|
switch st {
|
|
|
|
|
case ocapi.Disconnected:
|
2026-11-16 20:04:42 +08:00
|
|
|
wasDown = true
|
2026-11-18 13:00:20 +08:00
|
|
|
a.post(func() {
|
|
|
|
|
if a.client != client {
|
2026-11-16 09:09:09 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.errorText = "连接中断,正在重连…"
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
case ocapi.Subscribed:
|
2026-11-16 20:04:42 +08:00
|
|
|
if !wasDown {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
wasDown = false
|
2026-11-18 13:00:20 +08:00
|
|
|
a.post(func() {
|
|
|
|
|
if a.client != client {
|
2026-11-16 09:09:09 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.errorText = ""
|
|
|
|
|
a.loadSessions()
|
|
|
|
|
a.loadPerms()
|
|
|
|
|
if a.chat != nil {
|
|
|
|
|
a.chat.reloadTail()
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
go client.Subscribe(ctx, sub)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
func (a *App) stopStream() {
|
|
|
|
|
if a.streamCancel != nil {
|
|
|
|
|
a.streamCancel()
|
|
|
|
|
a.streamCancel = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// applyEvent folds one server event into the list and, when it belongs to
|
|
|
|
|
// the open chat, into the chat. Main thread.
|
|
|
|
|
func (a *App) applyEvent(ev ocapi.Event) {
|
|
|
|
|
if a.chat != nil {
|
2026-11-16 09:09:09 +08:00
|
|
|
var ref struct {
|
|
|
|
|
SessionID string `json:"sessionID"`
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
if ev.UnmarshalData(&ref) == nil && ref.SessionID == a.chat.id {
|
|
|
|
|
a.chat.handleEvent(ev)
|
|
|
|
|
}
|
|
|
|
|
if a.chatWin != nil {
|
|
|
|
|
a.chatWin.Update(func() {})
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
switch ev.Type {
|
|
|
|
|
case "session.created":
|
|
|
|
|
var d sessionCreatedData
|
|
|
|
|
if ev.UnmarshalData(&d) != nil || d.SessionID == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
for _, s := range a.sessions {
|
2026-11-16 09:09:09 +08:00
|
|
|
if s.ID == d.SessionID {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
sess := ocapi.Session{
|
2026-11-16 09:09:09 +08:00
|
|
|
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},
|
2026-11-18 13:00:20 +08:00
|
|
|
}
|
|
|
|
|
a.sessions = append([]ocapi.Session{sess}, a.sessions...)
|
2026-11-16 09:09:09 +08:00
|
|
|
|
|
|
|
|
case "session.renamed":
|
|
|
|
|
var d sessionRenamedData
|
|
|
|
|
if ev.UnmarshalData(&d) != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.patchSession(d.SessionID, func(s *ocapi.Session) { s.Title = d.Title })
|
2026-11-16 09:09:09 +08:00
|
|
|
|
|
|
|
|
case "session.model.selected":
|
|
|
|
|
var d sessionModelData
|
|
|
|
|
if ev.UnmarshalData(&d) != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.patchSession(d.SessionID, func(s *ocapi.Session) { s.Model = d.Model })
|
2026-11-16 09:09:09 +08:00
|
|
|
|
|
|
|
|
case "session.agent.selected":
|
|
|
|
|
var d sessionAgentData
|
|
|
|
|
if ev.UnmarshalData(&d) != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.patchSession(d.SessionID, func(s *ocapi.Session) { s.Agent = d.Agent })
|
2026-11-16 09:09:09 +08:00
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
case "session.archived", "session.deleted":
|
2026-11-16 09:09:09 +08:00
|
|
|
var d sessionRefData
|
|
|
|
|
if ev.UnmarshalData(&d) != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.removeSession(d.SessionID)
|
2026-11-16 09:09:09 +08:00
|
|
|
|
|
|
|
|
case "session.unarchived", "session.moved":
|
|
|
|
|
// The event carries no full record; a list read is the truth.
|
2026-11-18 13:00:20 +08:00
|
|
|
a.loadSessions()
|
2026-11-16 09:09:09 +08:00
|
|
|
|
|
|
|
|
case "session.execution.started":
|
|
|
|
|
var d sessionRefData
|
|
|
|
|
if ev.UnmarshalData(&d) != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.active[d.SessionID] = true
|
2026-11-16 09:09:09 +08:00
|
|
|
|
|
|
|
|
case "session.execution.succeeded", "session.execution.failed", "session.execution.interrupted":
|
|
|
|
|
var d sessionRefData
|
|
|
|
|
if ev.UnmarshalData(&d) != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.active[d.SessionID] = false
|
2026-11-16 09:09:09 +08:00
|
|
|
|
|
|
|
|
case "permission.asked":
|
|
|
|
|
var d permissionData
|
|
|
|
|
if ev.UnmarshalData(&d) != nil || d.ID == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
for _, p := range a.perms {
|
2026-11-16 09:09:09 +08:00
|
|
|
if p.ID == d.ID {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.perms = append(a.perms, ocapi.PermissionRequest{
|
2026-11-16 09:09:09 +08:00
|
|
|
ID: d.ID, SessionID: d.SessionID, Action: d.Action, Resources: d.Resources,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
case "permission.replied":
|
|
|
|
|
var d struct {
|
|
|
|
|
RequestID string `json:"requestID"`
|
|
|
|
|
}
|
|
|
|
|
if ev.UnmarshalData(&d) != nil || d.RequestID == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
next := a.perms[:0]
|
|
|
|
|
for _, p := range a.perms {
|
2026-11-16 09:09:09 +08:00
|
|
|
if p.ID != d.RequestID {
|
|
|
|
|
next = append(next, p)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.perms = next
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
func (a *App) patchSession(id string, patch func(*ocapi.Session)) {
|
|
|
|
|
for i := range a.sessions {
|
|
|
|
|
if a.sessions[i].ID == id {
|
|
|
|
|
patch(&a.sessions[i])
|
2026-11-16 09:09:09 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
func (a *App) removeSession(id string) {
|
|
|
|
|
next := a.sessions[:0]
|
|
|
|
|
for _, s := range a.sessions {
|
2026-11-16 09:09:09 +08:00
|
|
|
if s.ID != id {
|
|
|
|
|
next = append(next, s)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.sessions = next
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|