0696ab849a
Snapshot the chat directory with internal/filediff when a turn starts and compare it when the turn ends; store changed turns (newest 100 per chat), push chat_turn_diffs summaries, attach them to chat_messages replies and serve one turn's patches through chat_turn_diff.
96 lines
3.8 KiB
Go
96 lines
3.8 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
|
|
}
|
|
page, err := broker.Messages(ctx, query.SessionID, query.Limit, query.Cursor)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
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)
|
|
_ = write(response)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|