52 lines
2.0 KiB
Go
52 lines
2.0 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 answer.Action != "" && answer.Action != "accept" && answer.Action != "decline" && answer.Action != "cancel" {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "invalid form action"})
|
|
return
|
|
}
|
|
if len(answer.Answers) == 0 && answer.Action == "" {
|
|
_ = 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})
|
|
}
|