web: compose with Enter, share chat titles, reserve the star
Enter inserts a newline and Ctrl/Cmd+Enter sends; slash commands still run on Enter. Chat renames are stored in the server registry so every device sees the same title, with a one-time upload of old local titles. The star no longer covers the row status, and shell frames patch the background bar in place so the composer stays mounted while streaming.
This commit is contained in:
@@ -212,6 +212,15 @@ func (s *Store) ReopenOperatorSession(ctx context.Context, sessionID string) err
|
||||
return err
|
||||
}
|
||||
|
||||
// SetOperatorSessionTitle records the operator's shared chat title in one
|
||||
// UPDATE. It deliberately does not touch last_used_at: renaming is not use and
|
||||
// must not reorder the list. A session id that is not an operator session
|
||||
// updates no row and is not an error.
|
||||
func (s *Store) SetOperatorSessionTitle(ctx context.Context, sessionID, title string) error {
|
||||
_, err := s.db.ExecContext(ctx, "UPDATE operator_sessions SET title=? WHERE session_id=?", title, sessionID)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetOperatorSessionStar records the shared star state in one UPDATE. A session
|
||||
// id that is not an operator session updates no row and is not an error, so a
|
||||
// pipeline-owned run session is never affected.
|
||||
|
||||
@@ -51,3 +51,42 @@ func TestOperatorSessionStarIsSharedAndReversible(t *testing.T) {
|
||||
t.Fatalf("star toggling changed other session fields: %+v", plain)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOperatorSessionTitleIsShared proves the shared rename contract: the
|
||||
// registry title is updated without changing the OpenCode session id, other
|
||||
// fields or recency, and listing returns the new title for every client.
|
||||
func TestOperatorSessionTitleIsShared(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(filepath.Join(t.TempDir(), "rename.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a", "New chat", "operator"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, found, err := store.OperatorSession(ctx, "ses_a")
|
||||
if err != nil || !found {
|
||||
t.Fatalf("lookup: found=%v err=%v", found, err)
|
||||
}
|
||||
if err := store.SetOperatorSessionTitle(ctx, "ses_a", "Shared name"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
after, found, err := store.OperatorSession(ctx, "ses_a")
|
||||
if err != nil || !found {
|
||||
t.Fatalf("lookup after rename: found=%v err=%v", found, err)
|
||||
}
|
||||
if after.Title != "Shared name" {
|
||||
t.Fatalf("title = %q", after.Title)
|
||||
}
|
||||
if after.ID != before.ID || after.Directory != before.Directory || after.UpdatedAt != before.UpdatedAt {
|
||||
t.Fatalf("rename changed unrelated fields:\nbefore %+v\nafter %+v", before, after)
|
||||
}
|
||||
listed, err := store.OperatorSessions(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(listed) != 1 || listed[0].Title != "Shared name" {
|
||||
t.Fatalf("list did not return the shared title: %+v", listed)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ const (
|
||||
// only records the finished state, it never detaches a client.
|
||||
ChatClose = "chat_close"
|
||||
ChatReopen = "chat_reopen"
|
||||
// ChatRename records the operator's shared chat title on the server. Unlike
|
||||
// a local override, every browser and device then lists the same name.
|
||||
ChatRename = "chat_rename"
|
||||
// ChatStar records the operator's star on the server, so every browser and
|
||||
// device shows the same starred group. It takes a ChatQuery with SessionID
|
||||
// and Starred, and echoes the updated session like close/reopen.
|
||||
@@ -431,6 +434,13 @@ type ChatFormOption struct {
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
// ChatRenameRequest is the chat_rename body: the session id and the trimmed
|
||||
// shared title. An empty title is refused rather than clearing the name.
|
||||
type ChatRenameRequest struct {
|
||||
SessionID string `json:"session_id"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
// ChatAnswerRequest is the chat_answer body: the form id and a field-keyed
|
||||
// answer object. It is deliberately separate from ChatQuery because its payload
|
||||
// is structured answers, not a session-scoped read.
|
||||
|
||||
@@ -186,6 +186,8 @@ type Message struct {
|
||||
// ChatAnswer is the chat_answer body; the form update arrives through the
|
||||
// normal chat_event stream.
|
||||
ChatAnswer *ChatAnswerRequest `json:"chat_answer,omitempty"`
|
||||
// ChatRename is the chat_rename body; the reply echoes the updated session.
|
||||
ChatRename *ChatRenameRequest `json:"chat_rename,omitempty"`
|
||||
// ChatSubscribe attaches this connection to one session's shared stream; the
|
||||
// body names the session like ChatQuery. A chat_prompt response sets
|
||||
// ChatQueued when the prompt had to wait for an in-flight turn and was
|
||||
|
||||
@@ -91,6 +91,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
|
||||
SetOperatorSessionTitle(ctx context.Context, sessionID, title string) error
|
||||
}
|
||||
|
||||
var chatTokenSequence atomic.Uint64
|
||||
@@ -153,6 +154,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
case protocol.ChatStar:
|
||||
handleChatStar(ctx, request, registry, write)
|
||||
return true
|
||||
case protocol.ChatRename:
|
||||
handleChatRename(ctx, request, registry, write)
|
||||
return true
|
||||
case protocol.ChatShells:
|
||||
handleChatShells(ctx, request, broker, registry, write)
|
||||
return true
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// chatTitleLimit bounds a shared chat title. The UI disables Save on a blank
|
||||
// value; the server enforces the same rule so a direct client cannot erase a
|
||||
// title or store an unbounded string.
|
||||
const chatTitleLimit = 200
|
||||
|
||||
// handleChatRename records the operator's shared chat title. The operator-session
|
||||
// registry's title overrides OpenCode's own title in chat_sessions, so every
|
||||
// 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) {
|
||||
if registry == nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
|
||||
return
|
||||
}
|
||||
rename := request.ChatRename
|
||||
if rename == nil || !opencode.ValidSessionID(rename.SessionID) {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
||||
return
|
||||
}
|
||||
title := strings.TrimSpace(rename.Title)
|
||||
if title == "" {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat title with usable text is required"})
|
||||
return
|
||||
}
|
||||
if utf8.RuneCountInString(title) > chatTitleLimit {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat title must be at most 200 characters"})
|
||||
return
|
||||
}
|
||||
if _, found, err := registry.OperatorSession(ctx, rename.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 " + rename.SessionID + " is not an operator chat session"})
|
||||
return
|
||||
}
|
||||
if err := registry.SetOperatorSessionTitle(ctx, rename.SessionID, title); err != nil {
|
||||
_ = 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},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user