f7a3a49f94
Store a chat's harness with its operator session, expose chat_backends and a backend choice in New chat, and route each session through a manager. The Codex adapter uses the App Server's Unix WebSocket for threads, turns, history, questions and interrupt; OpenCode remains the default and Claude is not advertised until its sidecar and login are wired.
91 lines
3.5 KiB
Go
91 lines
3.5 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
|
|
_ = 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
|
|
}
|
|
if err := broker.Interrupt(ctx, query.SessionID); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
_ = 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)
|
|
}
|
|
}
|