Files
pipelines/internal/server/chat_commands.go
T
alan 9a6363b80e Chat: draw agent follow-ups and automatic continues apart from the operator
The server now names a user turn's origin (followup, auto_continue,
auto_switch, scheduled) on chat messages, user frames and queued prompts,
from the prompt's operation id while it holds the prompt and from its own
text prefixes in history read later. Origin is a new field, so older
clients keep treating these as ordinary user turns.

The app draws an agent follow-up, auto-continue and account-switch prompt
as a quiet outlined card on the agent's side with an icon, label and the
schedule facts from the preamble; the operator's own scheduled message
keeps its bubble with a small Scheduled mark. Find searches the drawn text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 15:40:34 +00:00

105 lines
4.2 KiB
Go

package server
import (
"context"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func handleChatMessages(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) {
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
}
response := protocol.Message{Type: protocol.ChatMessages, ID: request.ID}
// A hub snapshot is only the live tail. It is never authoritative history:
// serving it here used to make older turns unreachable after a reconnect.
if broker == nil {
_ = write(response)
return
}
if err := addChatHistory(ctx, &response, broker, hub, *query); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(response)
}
// addChatHistory reads one history page into a reply: a chat_messages reply,
// or a chat_subscribe reply that asked for the newest page.
func addChatHistory(ctx context.Context, response *protocol.Message, broker chatBroker, hub *chatHub, query protocol.ChatQuery) error {
page, err := broker.Messages(ctx, query.SessionID, query.Limit, query.Cursor)
if err != nil {
return err
}
page.Messages = markChatOrigins(hub.reconcileHistory(query.SessionID, page.Messages))
response.ChatMessages = page.Messages // legacy peers keep working.
response.ChatHistory = &page
// The session's recorded per-turn changes ride with its history, so the
// transcript's change chips survive a reload and a server restart.
response.ChatTurnDiffList = hub.turnDiffList(ctx, query.SessionID)
return nil
}
func handleChatSimple(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, 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 request.Type == protocol.ChatInterrupt {
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
}
resetCompaction := hub.compactionReset(query.SessionID)
if err := broker.Interrupt(ctx, query.SessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
resetCompaction()
_ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID})
return
}
reply := request.ChatReply
if reply == nil || !validChatSessionID(reply.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if strings.TrimSpace(reply.RequestID) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a permission request id is required"})
return
}
switch reply.Decision {
case protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject:
default:
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid permission decision is required"})
return
}
// A pending approval is shared and exactly-once: the first answer wins and is
// recorded with the client that gave it, and a later answer is refused as
// already answered rather than reaching the agent twice.
label := sanitizeChatClientLabel(reply.Client)
if hub != nil {
if prior, ok := hub.reservePermission(reply.SessionID, reply.RequestID, label); !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: permissionRefusal(prior, reply.RequestID)})
return
}
}
if err := broker.Reply(ctx, *reply); err != nil {
if hub != nil {
hub.releasePermission(reply.SessionID, reply.RequestID)
}
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatReply, ID: request.ID})
if hub != nil {
// Push the resolution to every client watching the session so each card
// clears and shows the outcome and which device answered.
hub.confirmPermission(reply.SessionID, reply.RequestID, label, reply.Decision)
}
}