971e555ceb
Add a server-side close for operator chats, so finishing with a chat takes it out of the active list for every client without deleting anything. The brokered protocol gains chat_close and chat_reopen; chat_close stops a running turn by reusing the existing broker interrupt (its result carries an interrupted flag, so a stream is never left running into nothing) and then sets closed_at on the session's operator_sessions row. The session, its transcript and its history survive, and because the closed state lives in the registry chat_sessions reports it to every client, not just the one that asked. An id that is not a recorded operator session, including a pipeline-owned run session, is refused before anything is written. chat_reopen clears the state again, and simply prompting a closed session also clears it (the store clears closed_at on touch). The registry gains closed_at for a fresh database and an idempotent migration for one created before it existed. The app renders closed sessions under a labelled Closed section with a Closed chip, offers a Close action with a confirmation that says it stops a running turn, marks the chat closed on every device and deletes nothing, and a Reopen action on each closed row. The device-local hide is renamed Hide on this device, with a snackbar saying the chat stays on the server, so it is never confused with the real close. No new dependency, the installer and Docker files are untouched, and no run is started from the app. Checked before committing: the packaged templates carry no chat surface and need no update, and internal/author/install.go's managed inventory needs none either (this adds no reference file). The packaged authoring skill reference internal/author/skill/references/operations.md and the shared docs/cli.md and android/README.md are stale because of this change, but an item no longer edits a shared document: what is stale and what should change there is recorded in docs/notes/chat-close.md for the docs-fold writer, which is committed here with the change. Checks: gofmt clean, go build ./... and go vet ./... clean, and go test ./internal/protocol/... ./internal/pipeline/... ./internal/server/... ./internal/author/... pass. The app side is verified with :app:assembleDebug and :app:testDebugUnitTest (--offline --no-daemon, BUILD SUCCESSFUL); the on-device close, second-client and reopen check is deferred to visual review. Not deployed; deploying is the operator's step.
144 lines
6.0 KiB
Go
144 lines
6.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// handleChatClose finishes an operator chat session and records it as closed on
|
|
// the server. It stops a turn that is running by reusing the existing interrupt
|
|
// (so a stream is never left running into nothing, and the result records that a
|
|
// turn was stopped), then sets the closed state in the operator-session
|
|
// registry. Nothing is deleted: the session, its transcript and its history
|
|
// survive, and every client sees the closed state because it lives in the
|
|
// 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) {
|
|
if broker == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
|
|
return
|
|
}
|
|
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 || !opencode.ValidSessionID(query.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
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
|
|
}
|
|
// Stop a running turn first. A failed interrupt is surfaced and the close is
|
|
// not recorded, so the operator is never told a live turn was stopped when it
|
|
// was not.
|
|
interrupted := hub.active(sessionID)
|
|
if interrupted {
|
|
if err := broker.Interrupt(ctx, sessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " could not be stopped: " + err.Error()})
|
|
return
|
|
}
|
|
}
|
|
if err := registry.CloseOperatorSession(ctx, sessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
session, found, err := registry.OperatorSession(ctx, 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 " + sessionID + " was closed but is no longer an operator session"})
|
|
return
|
|
}
|
|
wire := chatSessionFromOperator(session)
|
|
response := protocol.Message{
|
|
Type: protocol.ChatClose,
|
|
ID: request.ID,
|
|
ChatSession: &wire,
|
|
ChatClose: &protocol.ChatCloseResult{SessionID: sessionID, ClosedAt: session.ClosedAt, Interrupted: interrupted},
|
|
}
|
|
// Echo the registry row on the list field so a client can splice it into
|
|
// chat_sessions without a re-list.
|
|
response.ChatSessions = []protocol.ChatSession{wire}
|
|
_ = write(response)
|
|
}
|
|
|
|
// handleChatReopen clears the closed state of an operator chat session so it
|
|
// returns to the active list. Nothing is created or deleted; it is the inverse of
|
|
// chat_close. Prompting a closed session also reopens it (the store clears the
|
|
// closed state on touch), so both an explicit reopen and simply sending another
|
|
// message work. A session id that is not a recorded operator session is refused.
|
|
func handleChatReopen(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
|
|
}
|
|
query := request.ChatQuery
|
|
if query == nil || !opencode.ValidSessionID(query.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
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.ReopenOperatorSession(ctx, sessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
session, found, err := registry.OperatorSession(ctx, 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 " + sessionID + " is no longer an operator session"})
|
|
return
|
|
}
|
|
wire := chatSessionFromOperator(session)
|
|
response := protocol.Message{
|
|
Type: protocol.ChatReopen,
|
|
ID: request.ID,
|
|
ChatSession: &wire,
|
|
}
|
|
response.ChatSessions = []protocol.ChatSession{wire}
|
|
_ = write(response)
|
|
}
|
|
|
|
// chatSessionFromOperator projects one registry row to the wire ChatSession the
|
|
// app lists, so close/reopen and chat_sessions agree on the closed fields. The
|
|
// id is always set; title/directory/tag are copied verbatim, and the closed
|
|
// state comes from closed_at.
|
|
func chatSessionFromOperator(session pipeline.OperatorSession) protocol.ChatSession {
|
|
out := protocol.ChatSession{
|
|
ID: session.ID,
|
|
Title: strings.TrimSpace(session.Title),
|
|
Directory: session.Directory,
|
|
Tag: session.Tag,
|
|
CreatedAt: session.CreatedAt,
|
|
UpdatedAt: session.UpdatedAt,
|
|
}
|
|
if session.ClosedAt > 0 {
|
|
out.Closed = true
|
|
out.ClosedAt = session.ClosedAt
|
|
}
|
|
return out
|
|
}
|