f7a3a49f94
Store a chat's harness with its operator session, expose chat_backends and a backend choice in New chat, and route each session through a manager. The Codex adapter uses the App Server's Unix WebSocket for threads, turns, history, questions and interrupt; OpenCode remains the default and Claude is not advertised until its sidecar and login are wired.
68 lines
2.7 KiB
Go
68 lines
2.7 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, 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 || !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
|
|
}
|
|
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},
|
|
})
|
|
}
|