Sync open chats across devices through the server (pipeline run work in progress)
This commit is contained in:
+12
-1
@@ -214,7 +214,11 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
handleChatDirectories(ctx, request, registry, write)
|
||||
return true
|
||||
case protocol.ChatClose:
|
||||
handleChatClose(ctx, request, broker, hub, registry, write)
|
||||
var store chatOpenWorkspaceStore
|
||||
if candidate, ok := registry.(chatOpenWorkspaceStore); ok {
|
||||
store = candidate
|
||||
}
|
||||
handleChatClose(ctx, request, broker, hub, registry, store, write)
|
||||
return true
|
||||
case protocol.ChatReopen:
|
||||
handleChatReopen(ctx, request, registry, write)
|
||||
@@ -225,6 +229,13 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
case protocol.ChatColor:
|
||||
handleChatColor(ctx, request, registry, hub.changes(), clientID, write)
|
||||
return true
|
||||
case protocol.ChatOpenWorkspace:
|
||||
var store chatOpenWorkspaceStore
|
||||
if candidate, ok := registry.(chatOpenWorkspaceStore); ok {
|
||||
store = candidate
|
||||
}
|
||||
handleChatOpenWorkspace(ctx, request, broker, registry, store, hub.changes(), clientID, write)
|
||||
return true
|
||||
case protocol.ChatRename:
|
||||
handleChatRename(ctx, request, broker, registry, hub.changes(), clientID, write)
|
||||
return true
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
// registry, not on one device. A session id that is not a recorded operator
|
||||
// session is refused before anything is written, so a pipeline-owned run session
|
||||
// is never touched.
|
||||
func handleChatClose(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, write func(protocol.Message) error) {
|
||||
func handleChatClose(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, store chatOpenWorkspaceStore, write func(protocol.Message) error) {
|
||||
if broker == nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
|
||||
return
|
||||
@@ -92,6 +92,22 @@ func handleChatClose(ctx context.Context, request protocol.Message, broker chatB
|
||||
// chat_sessions without a re-list.
|
||||
response.ChatSessions = []protocol.ChatSession{wire}
|
||||
_ = write(response)
|
||||
if store != nil {
|
||||
workspace, workspaceErr := store.ChatOpenWorkspace(ctx, chatWorkspaceScope)
|
||||
if workspaceErr == nil {
|
||||
kept := workspace.Entries[:0]
|
||||
for _, entry := range workspace.Entries {
|
||||
if entry.SessionID != sessionID {
|
||||
kept = append(kept, entry)
|
||||
}
|
||||
}
|
||||
if len(kept) != len(workspace.Entries) {
|
||||
if updated, saveErr := store.SaveChatOpenWorkspace(ctx, chatWorkspaceScope, kept, false); saveErr == nil && hub != nil {
|
||||
hub.changes().broadcastWorkspace("", updated)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleChatReopen clears the closed state of an operator chat session so it
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
const chatWorkspaceScope = "default"
|
||||
|
||||
type chatOpenWorkspaceStore interface {
|
||||
ChatOpenWorkspace(context.Context, string) (protocol.ChatOpenWorkspaceSnapshot, error)
|
||||
SaveChatOpenWorkspace(context.Context, string, []protocol.ChatOpenWorkspaceEntry, bool) (protocol.ChatOpenWorkspaceSnapshot, error)
|
||||
}
|
||||
|
||||
func reconcileChatWorkspace(ctx context.Context, snapshot protocol.ChatOpenWorkspaceSnapshot, broker chatBroker, registry operatorRegistry, store chatOpenWorkspaceStore) (protocol.ChatOpenWorkspaceSnapshot, error) {
|
||||
if broker == nil || registry == nil || store == nil {
|
||||
return snapshot, nil
|
||||
}
|
||||
valid := make([]protocol.ChatOpenWorkspaceEntry, 0, len(snapshot.Entries))
|
||||
for _, entry := range snapshot.Entries {
|
||||
row, found, err := registry.OperatorSession(ctx, entry.SessionID)
|
||||
if err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
if !found || row.ClosedAt != 0 {
|
||||
continue
|
||||
}
|
||||
if _, live, err := broker.Session(ctx, entry.SessionID); err != nil {
|
||||
return snapshot, err
|
||||
} else if live {
|
||||
valid = append(valid, entry)
|
||||
}
|
||||
}
|
||||
if len(valid) == len(snapshot.Entries) {
|
||||
return snapshot, nil
|
||||
}
|
||||
return store.SaveChatOpenWorkspace(ctx, chatWorkspaceScope, valid, false)
|
||||
}
|
||||
|
||||
func validateChatWorkspace(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry) ([]protocol.ChatOpenWorkspaceEntry, error) {
|
||||
if request.ChatOpenWorkspaceRequest == nil {
|
||||
return nil, fmt.Errorf("chat_open_workspace_request is required")
|
||||
}
|
||||
entries := request.ChatOpenWorkspaceRequest.Entries
|
||||
if len(entries) > 64 {
|
||||
return nil, fmt.Errorf("too many open chats")
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for i := range entries {
|
||||
id := strings.TrimSpace(entries[i].SessionID)
|
||||
if !validChatSessionID(id) || seen[id] {
|
||||
return nil, fmt.Errorf("invalid or duplicate open chat session %q", id)
|
||||
}
|
||||
entries[i].SessionID = id
|
||||
seen[id] = true
|
||||
if registry == nil || broker == nil {
|
||||
return nil, fmt.Errorf("chat workspace validation unavailable")
|
||||
}
|
||||
row, found, err := registry.OperatorSession(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !found || row.ClosedAt != 0 {
|
||||
continue
|
||||
}
|
||||
if _, live, err := broker.Session(ctx, id); err != nil {
|
||||
return nil, err
|
||||
} else if !live {
|
||||
continue
|
||||
}
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func handleChatOpenWorkspace(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, store chatOpenWorkspaceStore, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) {
|
||||
if store == nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat workspace persistence unavailable"})
|
||||
return
|
||||
}
|
||||
entries, err := validateChatWorkspace(ctx, request, broker, registry)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
// Invalid, closed, deleted, and replaced sessions are dropped rather than
|
||||
// returned as errors: clients can never restore them from a stale snapshot.
|
||||
valid := entries[:0]
|
||||
for _, entry := range entries {
|
||||
row, found, rowErr := registry.OperatorSession(ctx, entry.SessionID)
|
||||
if rowErr != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: rowErr.Error()})
|
||||
return
|
||||
}
|
||||
if !found || row.ClosedAt != 0 {
|
||||
continue
|
||||
}
|
||||
_, live, sessionErr := broker.Session(ctx, entry.SessionID)
|
||||
if sessionErr != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: sessionErr.Error()})
|
||||
return
|
||||
}
|
||||
if live {
|
||||
valid = append(valid, entry)
|
||||
}
|
||||
}
|
||||
snapshot, err := store.SaveChatOpenWorkspace(ctx, chatWorkspaceScope, valid, request.ChatOpenWorkspaceRequest.Seed)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatOpenWorkspace, ID: request.ID, ChatOpenWorkspace: &snapshot})
|
||||
if changes != nil {
|
||||
changes.broadcastWorkspace(clientID, snapshot)
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,22 @@ func (c *chatSessionChanges) broadcast(sender string, session protocol.ChatSessi
|
||||
}
|
||||
}
|
||||
|
||||
func (c *chatSessionChanges) broadcastWorkspace(sender string, snapshot protocol.ChatOpenWorkspaceSnapshot) {
|
||||
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 {
|
||||
copy := snapshot
|
||||
copy.Entries = append([]protocol.ChatOpenWorkspaceEntry(nil), snapshot.Entries...)
|
||||
go func() { _ = write(protocol.Message{Type: protocol.ChatOpenWorkspaceChanged, ChatOpenWorkspace: ©}) }()
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
@@ -261,6 +261,17 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
// connection the same way.
|
||||
hub.sessionChanges.attach(clientID, write)
|
||||
defer hub.sessionChanges.detach(clientID)
|
||||
if workspaceStore, ok := registry.(chatOpenWorkspaceStore); ok {
|
||||
if snapshot, err := workspaceStore.ChatOpenWorkspace(connCtx, chatWorkspaceScope); err == nil && snapshot.Revision > 0 {
|
||||
snapshot, err = reconcileChatWorkspace(connCtx, snapshot, options.Chat, registry, workspaceStore)
|
||||
if err != nil {
|
||||
log.Printf("reconcile open chat workspace: %v", err)
|
||||
}
|
||||
if err == nil {
|
||||
_ = write(protocol.Message{Type: protocol.ChatOpenWorkspaceChanged, ChatOpenWorkspace: &snapshot})
|
||||
}
|
||||
}
|
||||
}
|
||||
// Terminal lifecycle changes reach every connection; output only the
|
||||
// connections that opened or read that terminal.
|
||||
terminals.clients.attach(clientID, write)
|
||||
|
||||
Reference in New Issue
Block a user