Files
alan a12a7b3d66 Claude chats: pass the chat title as --name so terminal resume lists them by name
Spawns pass the session title with --name (Claude Code records it as the
transcript's custom-title). A rename tells the harness the new title for its
next spawn, and the first routing of a session after a restart hands it the
registry title.
2026-11-03 12:31:31 +00:00

58 lines
2.5 KiB
Go

package server
import (
"context"
"strings"
"unicode/utf8"
"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, broker chatBroker, 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
}
rename := request.ChatRename
if rename == nil || !validChatSessionID(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
}
// A harness that names its own sessions (Claude Code's --name) takes the
// new title for its next start; the registry row above stays authoritative.
if titler, ok := broker.(chatTitleSetter); ok {
titler.SetTitle(ctx, rename.SessionID, title)
}
echoOperatorSession(ctx, request, rename.SessionID, protocol.ChatRename, registry, changes, clientID, write)
}