Files
pipelines/internal/server/chat_answer.go
T
alan f7a3a49f94 chat: add the Codex backend
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.
2026-10-20 12:10:39 +01:00

48 lines
1.8 KiB
Go

package server
import (
"context"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// handleChatAnswer replies to one OpenCode form, which is how the agent's
// question tool resumes: the tool call is paused inside form.created until a
// client answers. It validates the session/form ids before calling the broker,
// so a malformed answer cannot reach another session. The resulting
// form.replied/form.cancelled event arrives on the ordinary chat_event stream
// and clears the card on every watching client.
func handleChatAnswer(ctx context.Context, request protocol.Message, broker chatBroker, 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
}
answer := request.ChatAnswer
if answer == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat_answer payload is required"})
return
}
if !validChatSessionID(answer.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if answer.FormID == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a form id is required"})
return
}
if len(answer.Answers) == 0 {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "at least one answer is required"})
return
}
answerer, ok := broker.(chatAnswerer)
if !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "this chat backend cannot answer questions"})
return
}
if err := answerer.Answer(ctx, *answer); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatAnswer, ID: request.ID})
}