f85c172dc2
The item adds chat_close/chat_reopen to the brokered chat protocol and the operator-session registry's closed state. main replaced the per-session claim model with shared chat rooms (chat_subscribe, a prompt queue, one broker stream fanned out to every watcher). This merges the two. Resolved conflicts: - internal/protocol/chat.go, internal/protocol/message.go: kept both additions - ChatClose/ChatReopen and main's ChatSubscribe/ChatQueued. - internal/server/chat_sessions.go: kept the closed fields on a listed session; dropped the `Busy` assignment, because main no longer marks a session Busy (any number of clients may watch and write one). - internal/server/chat_claims.go: main deleted the claim model, so the file goes; close's "is a turn running" check is now `chatHub.turnInFlight` in internal/server/chat_room.go, reading the room's `running` flag. That is the same question the old claim answered, against main's model. - internal/server/chat_close.go: calls turnInFlight instead of the removed `active`. - android AppViewModel.kt, ChatFormat.kt, ChatListPane.kt, Message.kt: kept both sides - main's search state, queued-prompt handling, `Busy`-free status label and row/menu shape, plus the item's close/reopen actions, closed chip and section, close confirmation and "Hide on this device" rename. Also in this merge: main's new convention (AGENTS.md) that an item does not edit a shared document; the item's stale-document record for this change lives in docs/notes/chat-close.md, and docs/cli.md, android/README.md and internal/author/skill/references/operations.md are left to the docs-fold writer. Checks after resolving: gofmt clean, go build ./... and go vet ./... clean, the Go module passes, and the app's :app:assembleDebug and :app:testDebugUnitTest pass (--offline --no-daemon).
145 lines
6.1 KiB
Go
145 lines
6.1 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. The flag comes from the session's room (a turn in flight), so an
|
|
// idle session is never interrupted.
|
|
interrupted := hub.turnInFlight(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
|
|
}
|