Merge: chat colours, Ctrl+1-9, starred chats alphabetical

# Conflicts:
#	app/lib/features/chat/conversation/chat_thread_view.dart
#	app/tool/perf/perf_fixture.dart
#	internal/protocol/chat.go
#	internal/server/chat_close.go
#	internal/server/chat_sessions.go
This commit is contained in:
2026-11-03 11:22:50 +00:00
52 changed files with 1866 additions and 159 deletions
+6 -2
View File
@@ -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.ChatExport:
handleChatExport(ctx, request, broker, registry, write)
+1
View File
@@ -148,5 +148,6 @@ func chatSessionFromOperator(session pipeline.OperatorSession) protocol.ChatSess
out.Starred = true
out.StarredAt = session.StarredAt
}
out.Color = session.Color
return withChatHandoff(out)
}
+50
View File
@@ -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)
}
+140
View File
@@ -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)
}
}
+2
View File
@@ -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
+2 -17
View File
@@ -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)
}
+87
View File
@@ -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)
}
}
+1
View File
@@ -83,6 +83,7 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch
session.Starred = operator.StarredAt > 0
session.StarredAt = operator.StarredAt
session = withChatHandoff(session)
session.Color = operator.Color
response.ChatSessions = append(response.ChatSessions, session)
}
_ = write(response)
+2 -17
View File
@@ -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)
}
+4
View File
@@ -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)
}