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.
260 lines
11 KiB
Go
260 lines
11 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// operatorDirectoryLimit bounds the chat_directories choices: the operator home
|
|
// plus recent operator-session directories, capped at this small fixed number so
|
|
// a long history is never returned. It matches the posture of the other small
|
|
// fixed list bounds.
|
|
const operatorDirectoryLimit = 8
|
|
|
|
// handleChatSessions answers chat_sessions with the operator's own sessions,
|
|
// marking any another client currently holds as Busy. The registry is the single
|
|
// source of truth for which sessions are listed and for their directory, tag,
|
|
// created/recency, so a pipeline stage session (recorded in run_steps, never in
|
|
// the operator registry) is always excluded and a session with no recorded tag
|
|
// is never listed. FAIL-CLOSED: when no registry is configured the result is
|
|
// empty, never the whole endpoint list. broker.Sessions is called at most once
|
|
// per request, and the registry once.
|
|
func handleChatSessions(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, 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
|
|
}
|
|
response := protocol.Message{Type: protocol.ChatSessions, ID: request.ID}
|
|
// No registry (no store and no injection): list nothing rather than the whole
|
|
// OpenCode machine.
|
|
if registry == nil {
|
|
_ = write(response)
|
|
return
|
|
}
|
|
operatorSessions, err := registry.OperatorSessions(ctx)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
// OperatorSessions is already most-recently-used first with a deterministic
|
|
// tie-break; keep that order and only include a registry session the broker
|
|
// still reports, so a session the operator deleted upstream is not listed.
|
|
registered := make(map[string]struct{}, len(operatorSessions))
|
|
for _, session := range operatorSessions {
|
|
if session.ID != "" {
|
|
registered[session.ID] = struct{}{}
|
|
}
|
|
}
|
|
sessions, err := broker.Sessions(ctx)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
present := make(map[string]protocol.ChatSession, len(sessions))
|
|
for _, session := range sessions {
|
|
if _, ok := registered[session.ID]; ok {
|
|
present[session.ID] = session
|
|
}
|
|
}
|
|
for _, operator := range operatorSessions {
|
|
session, ok := present[operator.ID]
|
|
if !ok {
|
|
continue
|
|
}
|
|
if operator.Title != "" {
|
|
session.Title = operator.Title
|
|
}
|
|
session.Directory = operator.Directory
|
|
session.Tag = operator.Tag
|
|
session.CreatedAt = operator.CreatedAt
|
|
session.UpdatedAt = operator.UpdatedAt
|
|
// The closed state comes from the registry, so every client sees the same
|
|
// thing; a closed session stays in the list (the app groups it as history)
|
|
// rather than disappearing.
|
|
session.Closed = operator.ClosedAt > 0
|
|
session.ClosedAt = operator.ClosedAt
|
|
session.Busy = hub.busyForOther(operator.ID, clientID)
|
|
response.ChatSessions = append(response.ChatSessions, session)
|
|
}
|
|
_ = write(response)
|
|
}
|
|
|
|
// handleChatCreate creates one operator session in a resolved directory and
|
|
// records it in the operator registry: the OpenCode session id, the exact
|
|
// resolved directory, the title and the operator tag. A blank or omitted
|
|
// directory defaults to the operator's home, never the server's working
|
|
// directory. A registry write failure answers an error naming the created id, so
|
|
// an orphaned session is visible rather than silently reported as success.
|
|
func handleChatCreate(ctx context.Context, request protocol.Message, broker chatBroker, 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
|
|
}
|
|
title := ""
|
|
directory := ""
|
|
if request.ChatCreate != nil {
|
|
title = strings.TrimSpace(request.ChatCreate.Title)
|
|
directory = strings.TrimSpace(request.ChatCreate.Directory)
|
|
}
|
|
if title == "" {
|
|
title = "New chat"
|
|
}
|
|
resolved, err := resolveChatDirectory(directory)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
session, err := broker.Create(ctx, title, resolved)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
if err := registry.RecordOperatorSession(ctx, session.ID, resolved, title, protocol.ChatSessionTagOperator); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but could not be recorded: " + err.Error()})
|
|
return
|
|
}
|
|
session.Directory = resolved
|
|
session.Tag = protocol.ChatSessionTagOperator
|
|
response := protocol.Message{Type: protocol.ChatCreate, ID: request.ID, ChatSession: &session}
|
|
// The same registry row is echoed on the list field so a client can splice it
|
|
// into chat_sessions without a re-list.
|
|
response.ChatSessions = []protocol.ChatSession{session}
|
|
_ = write(response)
|
|
}
|
|
|
|
// handleChatAdopt records a session that already exists on the endpoint in the
|
|
// operator registry, using the endpoint's own directory and title verbatim, so
|
|
// it appears in chat_sessions and can be continued. It refuses a nil broker, a
|
|
// nil registry, a nil payload and an invalid/blank id before any broker call; an
|
|
// id the endpoint does not report is an error naming the id and records nothing;
|
|
// an endpoint record with an empty directory is refused rather than substituting
|
|
// the server's working directory or the operator home. The response echoes the
|
|
// adopted session and its registry row like chat_create.
|
|
func handleChatAdopt(ctx context.Context, request protocol.Message, broker chatBroker, 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
|
|
}
|
|
if request.ChatAdopt == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat_adopt session id is required"})
|
|
return
|
|
}
|
|
id := strings.TrimSpace(request.ChatAdopt.SessionID)
|
|
if !opencode.ValidSessionID(id) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
return
|
|
}
|
|
session, found, err := broker.Session(ctx, id)
|
|
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 " + id + " was not found on the OpenCode endpoint"})
|
|
return
|
|
}
|
|
directory := strings.TrimSpace(session.Directory)
|
|
if directory == "" {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " has no directory on the OpenCode endpoint"})
|
|
return
|
|
}
|
|
if err := registry.RecordOperatorSession(ctx, session.ID, directory, session.Title, protocol.ChatSessionTagOperator); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was found but could not be recorded: " + err.Error()})
|
|
return
|
|
}
|
|
session.Directory = directory
|
|
session.Tag = protocol.ChatSessionTagOperator
|
|
response := protocol.Message{Type: protocol.ChatAdopt, ID: request.ID, ChatSession: &session}
|
|
// The same registry row is echoed on the list field so a client can splice it
|
|
// into chat_sessions without a re-list.
|
|
response.ChatSessions = []protocol.ChatSession{session}
|
|
_ = write(response)
|
|
}
|
|
|
|
// handleChatDirectories answers chat_directories with the operator's home first,
|
|
// then the directories of previous operator sessions most-recently-used first,
|
|
// deduplicated against the home directory and capped at operatorDirectoryLimit.
|
|
func handleChatDirectories(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
|
|
}
|
|
home, err := OperatorHomeDirectory()
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
directories := []string{home}
|
|
seen := map[string]struct{}{home: {}}
|
|
recent, err := registry.OperatorDirectories(ctx, operatorDirectoryLimit)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
for _, directory := range recent {
|
|
if len(directories) >= operatorDirectoryLimit {
|
|
break
|
|
}
|
|
if directory == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[directory]; ok {
|
|
continue
|
|
}
|
|
seen[directory] = struct{}{}
|
|
directories = append(directories, directory)
|
|
}
|
|
_ = write(protocol.Message{Type: protocol.ChatDirectories, ID: request.ID, ChatDirectories: directories})
|
|
}
|
|
|
|
// resolveChatDirectory resolves a chat_create directory to an absolute path: an
|
|
// omitted/empty value, "~" or a "~/..." value resolves to the operator's home
|
|
// (os.UserHomeDir) and never the server's working directory; any other value
|
|
// must be an absolute, existing directory, so a relative path cannot silently
|
|
// create a session relative to wherever the server was started.
|
|
func resolveChatDirectory(directory string) (string, error) {
|
|
directory = strings.TrimSpace(directory)
|
|
if directory == "" || directory == "~" || strings.HasPrefix(directory, "~/") {
|
|
return OperatorHomeDirectory()
|
|
}
|
|
if !filepath.IsAbs(directory) {
|
|
return "", errors.New("chat directory must be absolute or start with ~")
|
|
}
|
|
info, err := os.Stat(directory)
|
|
if err != nil {
|
|
return "", errors.New("chat directory " + directory + " is not available: " + err.Error())
|
|
}
|
|
if !info.IsDir() {
|
|
return "", errors.New("chat directory " + directory + " is not a directory")
|
|
}
|
|
return directory, nil
|
|
}
|
|
|
|
// OperatorHomeDirectory returns the operator's home directory, the default chat
|
|
// location. It is resolved through the standard library so it never falls back
|
|
// to the process working directory.
|
|
func OperatorHomeDirectory() (string, error) {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", errors.New("resolve operator home directory: " + err.Error())
|
|
}
|
|
if home == "" {
|
|
return "", errors.New("operator home directory is empty")
|
|
}
|
|
return home, nil
|
|
}
|