Files
pipelines/internal/server/chat_close.go
T
alan 1de74f76bb Chat info: Session section with native id, resume command and export
The chat information sheet gains a Session section: the harness's native
session id (copy), harness, directory, a verified resume command (copy)
and Export conversation (Markdown), Export JSON and, for Claude Code,
Export native session (the JSONL transcript).

Server: chat_sessions rows carry resume_command and native_export; new
chat_export command builds Markdown/JSON from the same paged history
chat_messages serves (bounded at 8 MiB), refuses non-operator sessions.
App: exports go through the artifact file bridge (web download, share
sheet on phones).
2026-11-03 10:59:41 +00:00

153 lines
6.3 KiB
Go

package server
import (
"context"
"strings"
"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 || !validChatSessionID(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 {
resetCompaction := hub.compactionReset(sessionID)
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
}
resetCompaction()
}
if err := registry.CloseOperatorSession(ctx, sessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// A closed chat takes no more turns: prompts it still held are dropped.
hub.clearQueue(ctx, sessionID)
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 || !validChatSessionID(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,
Backend: session.Backend,
}
if session.ClosedAt > 0 {
out.Closed = true
out.ClosedAt = session.ClosedAt
}
if session.StarredAt > 0 {
out.Starred = true
out.StarredAt = session.StarredAt
}
return withChatHandoff(out)
}