686690f7b4
Reading a history page parsed the whole session file (tens of MB) on every chat_messages. A per-file index now records which lines build each shown message; a page re-reads and replays only its own messages' lines, and the index follows appends by reading only the new bytes. Results match the whole-file read (compared page by page on synthetic transcripts); shapes the index cannot replay exactly fall back to the whole-file read. chat_subscribe may ask (history: true) for the newest page in its reply, read once the subscription is attached, saving the client a round trip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
4.2 KiB
Go
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 = 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)
|
|
}
|
|
}
|