Files
pipelines/internal/server/chat_color.go
T
alan a4c191a416 Chats: a shared highlight colour per chat, pushed live with star and rename
chat_color {session_id, color|null} stores a palette name (red, orange,
amber, green, teal, blue, purple, pink) on the operator-session row; rows
and replies carry it as color. Colour, star and rename changes are pushed
to every other connection as chat_session_changed.
2026-11-03 10:53:38 +00:00

51 lines
2.1 KiB
Go

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)
}