diff --git a/internal/pipeline/operator_sessions.go b/internal/pipeline/operator_sessions.go index a1a0e49c..83beecc0 100644 --- a/internal/pipeline/operator_sessions.go +++ b/internal/pipeline/operator_sessions.go @@ -37,6 +37,10 @@ type OperatorSession struct { // or 0 while unstarred. It lives on the server so the starred group is shared // by every browser and device. StarredAt int64 + // Color is the operator's shared highlight for this chat: one of + // protocol.ChatColors by name, or "" for none. The name, not a shade, is + // stored so every client renders its own theme-tuned colour. + Color string } // ChatCreateBootstrap is the durable progress record for one chat_create @@ -66,6 +70,7 @@ func (s *Store) initOperatorSessions() error { session_id TEXT PRIMARY KEY, directory TEXT NOT NULL, title TEXT NOT NULL DEFAULT '', tag TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, last_used_at INTEGER NOT NULL, closed_at INTEGER NOT NULL DEFAULT 0, starred_at INTEGER NOT NULL DEFAULT 0, +color TEXT NOT NULL DEFAULT '', backend TEXT NOT NULL DEFAULT 'opencode', ephemeral INTEGER NOT NULL DEFAULT 0); CREATE INDEX IF NOT EXISTS operator_sessions_recent ON operator_sessions(last_used_at DESC);`) if err != nil { @@ -152,6 +157,10 @@ func (s *Store) migrateOperatorSessions() error { if err := s.addColumnIfMissing("operator_sessions", "starred_at", "INTEGER NOT NULL DEFAULT 0"); err != nil { return err } + // color records the shared highlight name. Existing sessions have none. + if err := s.addColumnIfMissing("operator_sessions", "color", "TEXT NOT NULL DEFAULT ''"); err != nil { + return err + } // backend records which harness owns the session so a restart can route a // chat without guessing from its id. Existing rows are OpenCode. if err := s.addColumnIfMissing("operator_sessions", "backend", "TEXT NOT NULL DEFAULT 'opencode'"); err != nil { @@ -310,14 +319,22 @@ func (s *Store) SetOperatorSessionStar(ctx context.Context, sessionID string, st return err } +// SetOperatorSessionColor records the shared highlight name in one UPDATE; "" +// clears it. The caller validates the name. Like the star, it never touches +// last_used_at, and a session id that is not an operator session updates no row. +func (s *Store) SetOperatorSessionColor(ctx context.Context, sessionID, color string) error { + _, err := s.db.ExecContext(ctx, "UPDATE operator_sessions SET color=? WHERE session_id=?", color, sessionID) + return err +} + // OperatorSession returns one operator session by id. found is false when the id // is not a recorded operator session (so a pipeline-owned run session is never // returned). It is one indexed lookup and never mutates the row. func (s *Store) OperatorSession(ctx context.Context, sessionID string) (OperatorSession, bool, error) { var session OperatorSession - row := s.db.QueryRowContext(ctx, `SELECT session_id,directory,title,tag,backend,created_at,last_used_at,closed_at,starred_at + row := s.db.QueryRowContext(ctx, `SELECT session_id,directory,title,tag,backend,created_at,last_used_at,closed_at,starred_at,color FROM operator_sessions WHERE session_id=? AND tag <> ''`, sessionID) - if err := row.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.Backend, &session.CreatedAt, &session.UpdatedAt, &session.ClosedAt, &session.StarredAt); err != nil { + if err := row.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.Backend, &session.CreatedAt, &session.UpdatedAt, &session.ClosedAt, &session.StarredAt, &session.Color); err != nil { if errors.Is(err, sql.ErrNoRows) { return OperatorSession{}, false, nil } @@ -336,7 +353,7 @@ FROM operator_sessions WHERE session_id=? AND tag <> ''`, sessionID) // operator can still see and reopen what they finished. It is one bounded // statement; recency is never mutated here. func (s *Store) OperatorSessions(ctx context.Context) ([]OperatorSession, error) { - rows, err := s.db.QueryContext(ctx, `SELECT session_id,directory,title,tag,backend,created_at,last_used_at,closed_at,starred_at + rows, err := s.db.QueryContext(ctx, `SELECT session_id,directory,title,tag,backend,created_at,last_used_at,closed_at,starred_at,color FROM operator_sessions WHERE tag <> '' AND ephemeral = 0 ORDER BY last_used_at DESC, session_id LIMIT ?`, operatorSessionListLimit) if err != nil { return nil, err @@ -345,7 +362,7 @@ FROM operator_sessions WHERE tag <> '' AND ephemeral = 0 ORDER BY last_used_at D sessions := []OperatorSession{} for rows.Next() { var session OperatorSession - if err := rows.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.Backend, &session.CreatedAt, &session.UpdatedAt, &session.ClosedAt, &session.StarredAt); err != nil { + if err := rows.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.Backend, &session.CreatedAt, &session.UpdatedAt, &session.ClosedAt, &session.StarredAt, &session.Color); err != nil { return nil, err } sessions = append(sessions, session) diff --git a/internal/pipeline/operator_sessions_color_test.go b/internal/pipeline/operator_sessions_color_test.go new file mode 100644 index 00000000..e84cb73d --- /dev/null +++ b/internal/pipeline/operator_sessions_color_test.go @@ -0,0 +1,56 @@ +package pipeline + +import ( + "context" + "path/filepath" + "testing" +) + +// TestOperatorSessionColorPersistsAcrossReopen proves the shared colour is a +// durable registry field: it survives closing and reopening the database, is +// listed, does not reorder recency, and clears back to none. +func TestOperatorSessionColorPersistsAcrossReopen(t *testing.T) { + ctx := context.Background() + path := filepath.Join(t.TempDir(), "color.db") + store, err := Open(path) + if err != nil { + t.Fatal(err) + } + if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a", "A", "operator"); err != nil { + t.Fatal(err) + } + before, _, err := store.OperatorSession(ctx, "ses_a") + if err != nil { + t.Fatal(err) + } + if before.Color != "" { + t.Fatalf("a new chat has colour %q", before.Color) + } + if err := store.SetOperatorSessionColor(ctx, "ses_a", "purple"); err != nil { + t.Fatal(err) + } + store.Close() + + store, err = Open(path) + if err != nil { + t.Fatal(err) + } + defer store.Close() + listed, err := store.OperatorSessions(ctx) + if err != nil { + t.Fatal(err) + } + if len(listed) != 1 || listed[0].Color != "purple" { + t.Fatalf("listed = %+v", listed) + } + if listed[0].UpdatedAt != before.UpdatedAt { + t.Fatalf("colouring changed recency: %d -> %d", before.UpdatedAt, listed[0].UpdatedAt) + } + if err := store.SetOperatorSessionColor(ctx, "ses_a", ""); err != nil { + t.Fatal(err) + } + cleared, _, err := store.OperatorSession(ctx, "ses_a") + if err != nil || cleared.Color != "" { + t.Fatalf("cleared = %+v err=%v", cleared, err) + } +} diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index 4a0e3e70..f48e7428 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -62,6 +62,14 @@ const ( // device shows the same starred group. It takes a ChatQuery with SessionID // and Starred, and echoes the updated session like close/reopen. ChatStar = "chat_star" + // ChatColor records the operator's shared highlight colour for one chat. It + // takes a ChatQuery with SessionID and Color (a ChatColors name; null or "" + // clears it) and echoes the updated session like chat_star. + ChatColor = "chat_color" + // ChatSessionChanged is pushed to every other open connection when a chat's + // shared list metadata (colour, star, title) changes, carrying the updated + // registry row in ChatSession, so each device's list updates live. + ChatSessionChanged = "chat_session_changed" // ChatSubscribe attaches a connection to one session's shared live stream so // it receives every frame any client's turn produces (its own and the other // devices'), not only the frames of a turn it prompted itself. Any number of @@ -280,6 +288,22 @@ type ChatSession struct { // epoch-millisecond time it was set, 0 while unstarred. Starred bool `json:"starred,omitempty"` StarredAt int64 `json:"starred_at,omitempty"` + // Color is the operator's shared highlight, a ChatColors name; omitted when + // none is set. Clients map the name to their own theme-tuned shade. + Color string `json:"color,omitempty"` +} + +// ChatColors is the fixed palette of chat highlight names, in picker order. +var ChatColors = []string{"red", "orange", "amber", "green", "teal", "blue", "purple", "pink"} + +// ValidChatColor reports whether name is one of ChatColors. +func ValidChatColor(name string) bool { + for _, color := range ChatColors { + if color == name { + return true + } + } + return false } // ChatCloseResult reports the outcome of a chat_close: the session id, the @@ -339,7 +363,9 @@ type ChatQuery struct { SessionID string `json:"session_id"` // Starred is set by chat_star; nil means the command did not name a state. Starred *bool `json:"starred,omitempty"` - Limit int `json:"limit,omitempty"` + // Color is set by chat_color: a ChatColors name, or nil/"" to clear. + Color *string `json:"color,omitempty"` + Limit int `json:"limit,omitempty"` // Cursor is the opaque continuation returned by OpenCode's descending // message listing. It is deliberately not interpreted by the broker. Cursor string `json:"cursor,omitempty"` diff --git a/internal/server/chat.go b/internal/server/chat.go index a042e88f..00e83bce 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -127,6 +127,7 @@ type operatorRegistry interface { CloseOperatorSession(ctx context.Context, sessionID string) error ReopenOperatorSession(ctx context.Context, sessionID string) error SetOperatorSessionStar(ctx context.Context, sessionID string, starred bool) error + SetOperatorSessionColor(ctx context.Context, sessionID, color string) error SetOperatorSessionTitle(ctx context.Context, sessionID, title string) error SetOperatorSessionBackend(ctx context.Context, sessionID, backend string) error } @@ -201,10 +202,13 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker handleChatReopen(ctx, request, registry, write) return true case protocol.ChatStar: - handleChatStar(ctx, request, registry, write) + handleChatStar(ctx, request, registry, hub.changes(), clientID, write) + return true + case protocol.ChatColor: + handleChatColor(ctx, request, registry, hub.changes(), clientID, write) return true case protocol.ChatRename: - handleChatRename(ctx, request, registry, write) + handleChatRename(ctx, request, registry, hub.changes(), clientID, write) return true case protocol.ChatBackends: handleChatBackends(request, broker, write) diff --git a/internal/server/chat_close.go b/internal/server/chat_close.go index 53202481..95524fbf 100644 --- a/internal/server/chat_close.go +++ b/internal/server/chat_close.go @@ -148,5 +148,6 @@ func chatSessionFromOperator(session pipeline.OperatorSession) protocol.ChatSess out.Starred = true out.StarredAt = session.StarredAt } + out.Color = session.Color return out } diff --git a/internal/server/chat_color.go b/internal/server/chat_color.go new file mode 100644 index 00000000..c14100e0 --- /dev/null +++ b/internal/server/chat_color.go @@ -0,0 +1,50 @@ +package server + +import ( + "context" + "strings" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// handleChatColor records the operator's shared highlight colour for one +// operator chat. The colour is a palette name (protocol.ChatColors), never a +// shade, so every client renders its own theme-tuned colour; null or "" clears +// it. An unknown name or a session that is not an operator chat is refused +// before anything is written. The reply echoes the updated session and every +// other connection receives it as chat_session_changed. +func handleChatColor(ctx context.Context, request protocol.Message, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) { + if registry == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"}) + return + } + query := request.ChatQuery + if query == nil || !validChatSessionID(query.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + color := "" + if query.Color != nil { + color = *query.Color + } + if color != "" && !protocol.ValidChatColor(color) { + if len(color) > 40 { + color = color[:40] + "…" + } + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "unknown chat colour \"" + color + "\"; choose one of " + strings.Join(protocol.ChatColors, ", ") + " or none"}) + return + } + sessionID := query.SessionID + if _, found, err := registry.OperatorSession(ctx, sessionID); err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } else if !found { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is not an operator chat session"}) + return + } + if err := registry.SetOperatorSessionColor(ctx, sessionID, color); err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + echoOperatorSession(ctx, request, sessionID, protocol.ChatColor, registry, changes, clientID, write) +} diff --git a/internal/server/chat_color_test.go b/internal/server/chat_color_test.go new file mode 100644 index 00000000..2c177808 --- /dev/null +++ b/internal/server/chat_color_test.go @@ -0,0 +1,140 @@ +package server + +import ( + "context" + "strings" + "testing" + + "github.com/coder/websocket" + "github.com/coder/websocket/wsjson" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +func chatColorRequest(id, session string, color *string) protocol.Message { + return protocol.Message{Type: protocol.ChatColor, ID: id, ChatQuery: &protocol.ChatQuery{SessionID: session, Color: color}} +} + +func readMessage(t *testing.T, ctx context.Context, conn *websocket.Conn) protocol.Message { + t.Helper() + var message protocol.Message + if err := wsjson.Read(ctx, conn, &message); err != nil { + t.Fatal(err) + } + return message +} + +// TestChatColorSetsListsBroadcastsAndClears is the shared-colour evidence: a +// chat_color names a palette colour, the reply echoes it, the registry keeps +// it, chat_sessions lists it, every other connection receives the row as +// chat_session_changed, and a null colour clears it again. +func TestChatColorSetsListsBroadcastsAndClears(t *testing.T) { + store := newOperatorStore(t) + broker := scriptedChat() + broker.sessions = []protocol.ChatSession{{ID: "ses_1", Title: "A", Directory: "/work/a"}} + ctx, _, conns := dialChatConns(t, Options{Chat: broker, OperatorRegistry: store}, 2) + if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil { + t.Fatal(err) + } + // A round trip on the watcher proves its connection is attached before the + // change is made. + if err := wsjson.Write(ctx, conns[1], protocol.Message{Type: protocol.Ping, ID: "p1"}); err != nil { + t.Fatal(err) + } + if pong := readMessage(t, ctx, conns[1]); pong.ID != "p1" { + t.Fatalf("ping reply = %+v", pong) + } + + teal := "teal" + if err := wsjson.Write(ctx, conns[0], chatColorRequest("c1", "ses_1", &teal)); err != nil { + t.Fatal(err) + } + reply := readMessage(t, ctx, conns[0]) + if reply.Type != protocol.ChatColor || reply.ID != "c1" || reply.ChatSession == nil || reply.ChatSession.Color != "teal" { + t.Fatalf("chat_color reply = %+v", reply) + } + pushed := readMessage(t, ctx, conns[1]) + if pushed.Type != protocol.ChatSessionChanged || pushed.ChatSession == nil || pushed.ChatSession.ID != "ses_1" || pushed.ChatSession.Color != "teal" { + t.Fatalf("watcher push = %+v", pushed) + } + row, found, err := store.OperatorSession(ctx, "ses_1") + if err != nil || !found || row.Color != "teal" { + t.Fatalf("registry row = %+v found=%v err=%v", row, found, err) + } + + if err := wsjson.Write(ctx, conns[0], protocol.Message{Type: protocol.ChatSessions, ID: "l1"}); err != nil { + t.Fatal(err) + } + listed := readMessage(t, ctx, conns[0]) + if len(listed.ChatSessions) != 1 || listed.ChatSessions[0].Color != "teal" { + t.Fatalf("chat_sessions = %+v", listed.ChatSessions) + } + + if err := wsjson.Write(ctx, conns[0], chatColorRequest("c2", "ses_1", nil)); err != nil { + t.Fatal(err) + } + cleared := readMessage(t, ctx, conns[0]) + if cleared.Type != protocol.ChatColor || cleared.ChatSession == nil || cleared.ChatSession.Color != "" { + t.Fatalf("clear reply = %+v", cleared) + } + if pushed := readMessage(t, ctx, conns[1]); pushed.Type != protocol.ChatSessionChanged || pushed.ChatSession.Color != "" { + t.Fatalf("clear push = %+v", pushed) + } + if row, _, _ := store.OperatorSession(ctx, "ses_1"); row.Color != "" { + t.Fatalf("clear not persisted: %+v", row) + } +} + +// TestChatColorRefusesUnknownColourAndForeignSession proves nothing is written +// for a colour outside the palette or a session that is not an operator chat. +func TestChatColorRefusesUnknownColourAndForeignSession(t *testing.T) { + store := newOperatorStore(t) + ctx, _, conn := dialChatOptions(t, Options{Chat: scriptedChat(), OperatorRegistry: store}) + if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil { + t.Fatal(err) + } + for _, color := range []string{"#ff0000", "Teal", "magenta"} { + value := color + if err := wsjson.Write(ctx, conn, chatColorRequest("bad", "ses_1", &value)); err != nil { + t.Fatal(err) + } + reply := readMessage(t, ctx, conn) + if reply.Type != protocol.Error || !strings.Contains(reply.Error, "unknown chat colour") { + t.Fatalf("colour %q reply = %+v", color, reply) + } + } + if row, _, _ := store.OperatorSession(ctx, "ses_1"); row.Color != "" { + t.Fatalf("a refused colour was written: %+v", row) + } + blue := "blue" + if err := wsjson.Write(ctx, conn, chatColorRequest("x", "ses_unknown", &blue)); err != nil { + t.Fatal(err) + } + if reply := readMessage(t, ctx, conn); reply.Type != protocol.Error || !strings.Contains(reply.Error, "not an operator chat") { + t.Fatalf("foreign session reply = %+v", reply) + } +} + +// TestChatStarBroadcastsToOtherConnections proves the star, like the colour, +// reaches the other devices' lists live. +func TestChatStarBroadcastsToOtherConnections(t *testing.T) { + store := newOperatorStore(t) + ctx, _, conns := dialChatConns(t, Options{Chat: scriptedChat(), OperatorRegistry: store}, 2) + if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil { + t.Fatal(err) + } + if err := wsjson.Write(ctx, conns[1], protocol.Message{Type: protocol.Ping, ID: "p1"}); err != nil { + t.Fatal(err) + } + readMessage(t, ctx, conns[1]) + starred := true + if err := wsjson.Write(ctx, conns[0], protocol.Message{Type: protocol.ChatStar, ID: "s1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1", Starred: &starred}}); err != nil { + t.Fatal(err) + } + if reply := readMessage(t, ctx, conns[0]); reply.Type != protocol.ChatStar || !reply.ChatSession.Starred { + t.Fatalf("chat_star reply = %+v", reply) + } + if pushed := readMessage(t, ctx, conns[1]); pushed.Type != protocol.ChatSessionChanged || !pushed.ChatSession.Starred { + t.Fatalf("watcher push = %+v", pushed) + } +} diff --git a/internal/server/chat_hub.go b/internal/server/chat_hub.go index ab9394d8..d68feabd 100644 --- a/internal/server/chat_hub.go +++ b/internal/server/chat_hub.go @@ -61,6 +61,8 @@ type chatHub struct { // unattended holds the unattended-observation timings (chat_unattended.go), // fixed when the hub is built. unattended chatUnattendedTimings + // sessionChanges pushes shared list metadata changes to every connection. + sessionChanges chatSessionChanges } // chatPart is the retained form of one part. Text and reasoning deltas append to diff --git a/internal/server/chat_rename.go b/internal/server/chat_rename.go index 54a8a3c0..4fa5fc78 100644 --- a/internal/server/chat_rename.go +++ b/internal/server/chat_rename.go @@ -18,7 +18,7 @@ const chatTitleLimit = 200 // browser and device lists the same name. The OpenCode session, its transcript // and its generated title are untouched, and a session that is not an operator // chat is refused before anything is written. -func handleChatRename(ctx context.Context, request protocol.Message, registry operatorRegistry, write func(protocol.Message) error) { +func handleChatRename(ctx context.Context, request protocol.Message, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) { if registry == nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"}) return @@ -48,20 +48,5 @@ func handleChatRename(ctx context.Context, request protocol.Message, registry op _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } - session, found, err := registry.OperatorSession(ctx, rename.SessionID) - if err != nil { - _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) - return - } - if !found { - _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + rename.SessionID + " is no longer an operator session"}) - return - } - wire := chatSessionFromOperator(session) - write(protocol.Message{ - Type: protocol.ChatRename, - ID: request.ID, - ChatSession: &wire, - ChatSessions: []protocol.ChatSession{wire}, - }) + echoOperatorSession(ctx, request, rename.SessionID, protocol.ChatRename, registry, changes, clientID, write) } diff --git a/internal/server/chat_session_changes.go b/internal/server/chat_session_changes.go new file mode 100644 index 00000000..114c7dcb --- /dev/null +++ b/internal/server/chat_session_changes.go @@ -0,0 +1,87 @@ +package server + +import ( + "context" + "sync" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// chatSessionChanges fans a chat's shared list metadata (colour, star, title) +// out to every open connection, so a change on one device updates the chat +// list on the others live. Unlike a chat room it is not keyed by session: every +// connection shows the list. It holds each connection's serialized write +// function, never the socket. The zero value is ready to use. +type chatSessionChanges struct { + mu sync.Mutex + clients map[string]func(protocol.Message) error +} + +func (c *chatSessionChanges) attach(clientID string, write func(protocol.Message) error) { + c.mu.Lock() + defer c.mu.Unlock() + if c.clients == nil { + c.clients = map[string]func(protocol.Message) error{} + } + c.clients[clientID] = write +} + +func (c *chatSessionChanges) detach(clientID string) { + c.mu.Lock() + defer c.mu.Unlock() + delete(c.clients, clientID) +} + +// changes returns the hub's connection fan-out; nil for a nil hub, which +// broadcasts nothing. +func (h *chatHub) changes() *chatSessionChanges { + if h == nil { + return nil + } + return &h.sessionChanges +} + +// broadcast pushes the updated row to every connection except the sender, +// which already has it in its reply. Each write runs on its own goroutine +// (bounded by the connection's write timeout), so a slow client cannot delay +// the sender or the others. +func (c *chatSessionChanges) broadcast(sender string, session protocol.ChatSession) { + c.mu.Lock() + targets := make([]func(protocol.Message) error, 0, len(c.clients)) + for id, write := range c.clients { + if id != sender { + targets = append(targets, write) + } + } + c.mu.Unlock() + for _, write := range targets { + row := session + go func() { + _ = write(protocol.Message{Type: protocol.ChatSessionChanged, ChatSession: &row}) + }() + } +} + +// echoOperatorSession answers a metadata change with the updated registry row +// and pushes the same row to every other connection. +func echoOperatorSession(ctx context.Context, request protocol.Message, sessionID, reply string, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) { + session, found, err := registry.OperatorSession(ctx, sessionID) + if err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + if !found { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is no longer an operator session"}) + return + } + wire := chatSessionFromOperator(session) + _ = write(protocol.Message{ + Type: reply, + ID: request.ID, + ChatSession: &wire, + ChatSessions: []protocol.ChatSession{wire}, + }) + if changes != nil { + changes.broadcast(clientID, wire) + } +} diff --git a/internal/server/chat_sessions.go b/internal/server/chat_sessions.go index 282b730d..a44100a2 100644 --- a/internal/server/chat_sessions.go +++ b/internal/server/chat_sessions.go @@ -82,6 +82,7 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch session.ClosedAt = operator.ClosedAt session.Starred = operator.StarredAt > 0 session.StarredAt = operator.StarredAt + session.Color = operator.Color response.ChatSessions = append(response.ChatSessions, session) } _ = write(response) diff --git a/internal/server/chat_star.go b/internal/server/chat_star.go index a38ff6d6..1011c3b0 100644 --- a/internal/server/chat_star.go +++ b/internal/server/chat_star.go @@ -13,7 +13,7 @@ import ( // session is refused before anything is written, so a pipeline-owned run session // is never touched. The request body is a ChatQuery carrying SessionID and the // desired Starred value; the reply echoes the updated session. -func handleChatStar(ctx context.Context, request protocol.Message, registry operatorRegistry, write func(protocol.Message) error) { +func handleChatStar(ctx context.Context, request protocol.Message, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) { if registry == nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"}) return @@ -39,20 +39,5 @@ func handleChatStar(ctx context.Context, request protocol.Message, registry oper _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } - session, found, err := registry.OperatorSession(ctx, sessionID) - if err != nil { - _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) - return - } - if !found { - _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is no longer an operator session"}) - return - } - wire := chatSessionFromOperator(session) - write(protocol.Message{ - Type: protocol.ChatStar, - ID: request.ID, - ChatSession: &wire, - ChatSessions: []protocol.ChatSession{wire}, - }) + echoOperatorSession(ctx, request, sessionID, protocol.ChatStar, registry, changes, clientID, write) } diff --git a/internal/server/websocket.go b/internal/server/websocket.go index abd42103..1f6da9bf 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -210,6 +210,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin // the welcome frame so a push never precedes it. acks.attach(clientID, write) defer acks.detach(clientID) + // Shared chat-list metadata (colour, star, title) reaches every open + // connection the same way. + hub.sessionChanges.attach(clientID, write) + defer hub.sessionChanges.detach(clientID) if notes != nil { defer notes.detach(clientID) }