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.
110 lines
5.1 KiB
Go
110 lines
5.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// chatBroker is the server-side OpenCode chat seam. *opencode.Chat implements
|
|
// it; tests substitute a scripted broker. Keeping it an interface means the
|
|
// /connect handler never builds a second OpenCode transport.
|
|
type chatBroker interface {
|
|
Prompt(ctx context.Context, sessionID, text string) error
|
|
// Stream subscribes to the session's OpenCode event stream. ready is called
|
|
// once the subscription is attached, before any frame is read, so the caller
|
|
// can order the prompt after the subscription.
|
|
Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error
|
|
Interrupt(ctx context.Context, sessionID string) error
|
|
Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error)
|
|
// Sessions lists the endpoint's OpenCode sessions so the handler can
|
|
// restrict them to the operator's own recorded sessions.
|
|
Sessions(ctx context.Context) ([]protocol.ChatSession, error)
|
|
// Create creates one OpenCode session in the given directory and returns it.
|
|
Create(ctx context.Context, title, directory string) (protocol.ChatSession, error)
|
|
// Session returns the endpoint's own record of one existing session so an
|
|
// adopted session carries the endpoint's directory and title; found is false
|
|
// when the endpoint does not list sessionID.
|
|
Session(ctx context.Context, sessionID string) (protocol.ChatSession, bool, error)
|
|
// Permissions lists the session's pending OpenCode approval requests so the
|
|
// handler can push chat_permission frames.
|
|
Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error)
|
|
Reply(ctx context.Context, request protocol.ChatReplyRequest) error
|
|
}
|
|
|
|
// operatorRegistry is the server-side operator-session seam. *pipeline.Store
|
|
// satisfies it. It is a table of its own, separate from the pipeline stage
|
|
// ownership (SessionOwner/run_session_owners) that settles a run: chat_sessions
|
|
// lists operator sessions only and never consults stage ownership.
|
|
type operatorRegistry interface {
|
|
RecordOperatorSession(ctx context.Context, sessionID, directory, title, tag string) error
|
|
TouchOperatorSession(ctx context.Context, sessionID string) error
|
|
OperatorSessions(ctx context.Context) ([]pipeline.OperatorSession, error)
|
|
OperatorDirectories(ctx context.Context, limit int) ([]string, error)
|
|
// OperatorSession returns one recorded operator session by id (found false
|
|
// when the id is not an operator session), so chat_close/chat_reopen can echo
|
|
// the updated row and refuse a session that is not theirs.
|
|
OperatorSession(ctx context.Context, sessionID string) (pipeline.OperatorSession, bool, error)
|
|
// CloseOperatorSession records the closed state; ReopenOperatorSession clears
|
|
// it. Neither deletes anything, and neither touches a pipeline-owned run
|
|
// session (which has no operator_sessions row).
|
|
CloseOperatorSession(ctx context.Context, sessionID string) error
|
|
ReopenOperatorSession(ctx context.Context, sessionID string) error
|
|
}
|
|
|
|
// chatClientToken returns an unguessable per-connection identity. It attributes
|
|
// a session claim so two clients on the same server cannot silently write the
|
|
// same conversation; a fresh token per connection means a reconnected client is
|
|
// a new writer and must re-claim.
|
|
func chatClientToken() string {
|
|
var b [8]byte
|
|
if _, err := rand.Read(b[:]); err != nil {
|
|
return "conn-unknown"
|
|
}
|
|
return "conn-" + hex.EncodeToString(b[:])
|
|
}
|
|
|
|
// handleChat dispatches one chat_* request. It returns true when the request was
|
|
// a chat type and has been fully answered (the caller must not fall through to
|
|
// the generic response). Frames are pushed after the ack, each through the
|
|
// connection-scoped write function so the read loop and the stream goroutine
|
|
// never interleave partial websocket writes.
|
|
func handleChat(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) bool {
|
|
switch request.Type {
|
|
case protocol.ChatPrompt:
|
|
handleChatPrompt(ctx, request, broker, hub, registry, clientID, write)
|
|
return true
|
|
case protocol.ChatMessages:
|
|
handleChatMessages(ctx, request, broker, hub, write)
|
|
return true
|
|
case protocol.ChatInterrupt:
|
|
handleChatSimple(ctx, request, broker, hub, write)
|
|
return true
|
|
case protocol.ChatReply:
|
|
handleChatSimple(ctx, request, broker, hub, write)
|
|
return true
|
|
case protocol.ChatSessions:
|
|
handleChatSessions(ctx, request, broker, hub, registry, clientID, write)
|
|
return true
|
|
case protocol.ChatCreate:
|
|
handleChatCreate(ctx, request, broker, registry, write)
|
|
return true
|
|
case protocol.ChatAdopt:
|
|
handleChatAdopt(ctx, request, broker, registry, write)
|
|
return true
|
|
case protocol.ChatDirectories:
|
|
handleChatDirectories(ctx, request, registry, write)
|
|
return true
|
|
case protocol.ChatClose:
|
|
handleChatClose(ctx, request, broker, hub, registry, write)
|
|
return true
|
|
case protocol.ChatReopen:
|
|
handleChatReopen(ctx, request, registry, write)
|
|
return true
|
|
}
|
|
return false
|
|
}
|