7d9490bba1
Two gaps in the chat's in-flight feedback. The turn indicator no longer reads a bare `Working…` for every state. A new pure derivation (TurnPhase/turnPhase/turnPhaseLabel in ui/chat/ChatFormat.kt) labels the headline from evidence already on the wire: `Thinking…` while text or reasoning deltas are still arriving (within TURN_THINKING_WINDOW_MILLIS), `Running a tool…` while calls are in flight, `Waiting…` otherwise. No percentage, ETA or invented progress. The agent's shell commands are now surfaced end to end as their own frame kind, so a background process (a dev server, a watching build) that outlives its tool call is visible and clears when it exits. Server: - protocol/chat.go adds ChatEventShell, ChatShell (id, status, command, *int exit — a pointer so exit 0 is not "absent") and ChatEventFrame.shell. - opencode/chat.go maps shell.created/shell.exited through a new exported ChatStreamState, attributing created via info.metadata.sessionID and exit via the ids the stream already surfaced; another session's or an unattributed shell is ignored, an id-less shell fails the stream. No cwd/file/credential is carried. - server/chat_hub.go retains still-running shells (chatShellLimit plus the shared byte cap) and drops them on exit; chat_stream.go replays them on chat_subscribe, the shell half of the resume (a shell is not a turn, so the message snapshot cannot carry it). App: - data/protocol/Chat.kt adds ChatShell, ChatEventFrame.shell, EVENT_SHELL, SHELL_RUNNING/SHELL_EXITED. - ui/chat/ChatStore.kt stamps ChatThread.lastDeltaAt on each delta (cleared when the turn settles) and keeps ChatThread.shells across a chat switch; a shell never marks the thread running. streamResumed drops shells so the subscribe replay re-syncs the still-running ones. - ui/chat/ChatTranscript.kt: RunningIndicator prints the phase label; a new always-visible BackgroundProcessBar names each running shell. - ui/chat/ChatConversationPane.kt shows the bar whenever a shell is running, whether or not a turn is. Tests extend the server's event mapping (opencode/chat_v2_test.go, server/chat_test.go) and the app's pure folds (new ui/chat/ChatInFlightStateTest.kt, 15 tests), pinning the shell mapping, the other-session rejection and the "background process outlives the turn" state. Checked and intentionally unchanged: - No packaged OpenCode skill reference documents either surface. internal/author/skill/references/operations.md describes the brokered chat_* protocol in prose without naming frame kinds or the app indicator, so it stays correct as-is; no reference was added, so install.go's managed inventory needs no change. - This repository has no packaged templates directory. - Shared docs are not edited here per the map: docs/notes/chat-shell-background -process.md records what is now stale in docs/cli.md and android/README.md for the docs-fold writer. Verification (this session): gofmt -l clean, go build ./... and go vet ./... OK. Android unit suite and the on-device capture were run in the preceding review stage; see the note for the live-verification limitation.
133 lines
6.1 KiB
Go
133 lines
6.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// chatPermissionPollInterval is the fixed interval between permission polls: one
|
|
// GET per poll through the shared Connection bounds, never a busy loop. The first
|
|
// poll runs immediately so a request pending when a turn starts is surfaced
|
|
// without waiting a full interval.
|
|
const chatPermissionPollInterval = 2 * time.Second
|
|
|
|
// handleChatPrompt accepts a prompt from any client. It never rejects a prompt
|
|
// because another client is watching the session: the client attaches to the
|
|
// session's shared room and the prompt either starts a turn or is queued behind
|
|
// the one in flight, with the ack saying which. The prompt is echoed live to
|
|
// every other client, attributed to the sending device's label, and the turn's
|
|
// output is fanned out to all of them.
|
|
func handleChatPrompt(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
|
|
}
|
|
prompt := request.ChatPrompt
|
|
if prompt == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat prompt payload is required"})
|
|
return
|
|
}
|
|
if !opencode.ValidSessionID(prompt.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
|
|
return
|
|
}
|
|
if strings.TrimSpace(prompt.Text) == "" {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a nonempty chat prompt is required"})
|
|
return
|
|
}
|
|
// Reject an oversized prompt before any OpenCode call; the bound matches the
|
|
// OpenCode request limit.
|
|
if len(prompt.Text) > opencode.MaxRequest {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"})
|
|
return
|
|
}
|
|
sessionID := prompt.SessionID
|
|
sub := hub.attach(sessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write)
|
|
if sub == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not attach to session " + sessionID})
|
|
return
|
|
}
|
|
// The user turn every *other* client sees, carrying the sender's label. The
|
|
// sender's own client appended it locally, so it is excluded below.
|
|
userFrame := protocol.ChatEventFrame{
|
|
SessionID: sessionID,
|
|
MessageID: hub.nextUserMessageID(),
|
|
Kind: protocol.ChatEventUser,
|
|
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
|
|
Client: sub.label,
|
|
}
|
|
queued, refused := hub.submitPrompt(sessionID, &chatQueuedPrompt{
|
|
text: prompt.Text,
|
|
requestID: request.ID,
|
|
write: write,
|
|
})
|
|
if refused {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " already has the maximum number of prompts queued"})
|
|
return
|
|
}
|
|
// Publish the user turn to every other client; the sender already has it.
|
|
hub.broadcastUser(userFrame, clientID)
|
|
if queued {
|
|
// The client that sent it is told the prompt is waiting for the in-flight
|
|
// turn, so it shows queued/steering instead of blocking or silently
|
|
// dropping the message. The turn runner acknowledges it once it runs, so
|
|
// clear the ack on the queued entry to avoid a second ack for the same id.
|
|
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatQueued: true})
|
|
}
|
|
if registry != nil {
|
|
// Recency is one UPDATE: it moves the operator session to the front of
|
|
// chat_sessions. A session id that is not an operator session updates no
|
|
// row and is not an error, so pipeline stage prompts are unaffected.
|
|
if err := registry.TouchOperatorSession(ctx, sessionID); err != nil {
|
|
// Best-effort; the turn runner also touches on delivery.
|
|
_ = err
|
|
}
|
|
}
|
|
}
|
|
|
|
// handleChatSubscribe attaches one connection to a session's shared stream so it
|
|
// receives every frame any client's turn produces, its own or another device's.
|
|
// It replays the session's still-pending approval requests, if the room holds
|
|
// any, so a device that joins late still shows the cards.
|
|
func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatHub, clientID string, write func(protocol.Message) error) {
|
|
// The wire body of chat_subscribe is the chat_subscribe field (the app's
|
|
// subscribeRequest and docs/cli.md both use it), not the chat_query body the
|
|
// other session-scoped commands carry.
|
|
query := request.ChatSubscribe
|
|
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
|
|
}
|
|
if broker == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
|
|
return
|
|
}
|
|
sub := hub.attach(query.SessionID, clientID, "", broker, write)
|
|
if sub == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not attach to session " + query.SessionID})
|
|
return
|
|
}
|
|
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID})
|
|
// Replay every still-unresolved approval request the room holds, not only the
|
|
// most recent one, so a client that attaches after two requests were raised
|
|
// still sees both cards.
|
|
for _, pending := range hub.pendingFrames(query.SessionID) {
|
|
frame := pending
|
|
sub.send(protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame})
|
|
}
|
|
// Replay the background processes still running, so a device that attaches
|
|
// after they started (a reconnect, or a chat opened from another device)
|
|
// shows them rather than nothing until they exit. The message snapshot cannot
|
|
// carry a shell — a shell outlives the turn and is not a turn — so this is the
|
|
// shell half of the resume.
|
|
for _, shell := range hub.shellSnapshot(query.SessionID) {
|
|
running := shell
|
|
frame := protocol.ChatEventFrame{SessionID: query.SessionID, Kind: protocol.ChatEventShell, Shell: &running}
|
|
sub.send(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame})
|
|
}
|
|
}
|