feat(chat): broker Codex approvals and complete typed tool interactions

This commit is contained in:
2026-10-20 17:41:35 +01:00
parent f31806cfcb
commit 35329e4022
21 changed files with 879 additions and 84 deletions
+32 -8
View File
@@ -258,11 +258,18 @@ func (c *Chat) Answer(ctx context.Context, request protocol.ChatAnswerRequest) e
if strings.TrimSpace(request.FormID) == "" {
return fmt.Errorf("a form id is required")
}
if request.Action == "cancel" {
_, err := c.call(ctx, "delete", "/api/session/"+request.SessionID+"/form/"+url.PathEscape(request.FormID), nil)
return err
}
if request.Action != "" && request.Action != "accept" {
return fmt.Errorf("unsupported OpenCode form action")
}
body := map[string]any{"answer": request.Answers}
if request.Answers == nil {
body["answer"] = map[string]any{}
}
if _, err := c.call(ctx, "post", "/api/session/"+request.SessionID+"/form/"+request.FormID+"/reply", body); err != nil {
if _, err := c.call(ctx, "post", "/api/session/"+request.SessionID+"/form/"+url.PathEscape(request.FormID)+"/reply", body); err != nil {
return err
}
return nil
@@ -906,13 +913,26 @@ func (c *Chat) Forms(ctx context.Context, id string) ([]protocol.ChatForm, error
}
type chatFormField struct {
Key string `json:"key"`
Title string `json:"title"`
Description string `json:"description"`
Type string `json:"type"`
Options []chatFormOption `json:"options"`
Custom bool `json:"custom"`
Required bool `json:"required"`
Key string `json:"key"`
Title string `json:"title"`
Description string `json:"description"`
Type string `json:"type"`
Options []chatFormOption `json:"options"`
Custom bool `json:"custom"`
Required bool `json:"required"`
URL string `json:"url"`
Default any `json:"default"`
Minimum any `json:"minimum"`
Maximum any `json:"maximum"`
MinLength *int `json:"minLength"`
MaxLength *int `json:"maxLength"`
MinItems *int `json:"minItems"`
MaxItems *int `json:"maxItems"`
Format string `json:"format"`
Pattern string `json:"pattern"`
Placeholder string `json:"placeholder"`
Hidden bool `json:"hidden"`
When []protocol.ChatFormWhen `json:"when"`
}
type chatFormOption struct {
@@ -1195,6 +1215,7 @@ func normalizeFormEvent(eventType string, data chatEvent, sessionID string, stre
SessionID: sessionID,
Title: form.Title,
Kind: kind,
Actions: []string{"accept", "cancel"},
}
for _, field := range form.Fields {
out := protocol.ChatFormField{
@@ -1204,6 +1225,9 @@ func normalizeFormEvent(eventType string, data chatEvent, sessionID string, stre
Type: field.Type,
Custom: field.Custom,
Required: field.Required,
URL: field.URL, Default: field.Default, Minimum: field.Minimum, Maximum: field.Maximum,
MinLength: field.MinLength, MaxLength: field.MaxLength, MinItems: field.MinItems, MaxItems: field.MaxItems,
Format: field.Format, Pattern: field.Pattern, Placeholder: field.Placeholder, Hidden: field.Hidden, When: field.When,
}
for _, option := range field.Options {
value := option.Value
@@ -5,6 +5,8 @@ import (
"net/http"
"net/http/httptest"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatFormsReadbackUsesSessionOwnership(t *testing.T) {
@@ -22,3 +24,22 @@ func TestChatFormsReadbackUsesSessionOwnership(t *testing.T) {
t.Fatalf("forms=%+v err=%v", forms, err)
}
}
func TestChatQuestionCancellationDoesNotSendAnAnswer(t *testing.T) {
calls := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
if r.Method != "DELETE" || r.URL.Path != "/api/session/ses_test/form/form_test" {
t.Errorf("unexpected cancellation: %s %s", r.Method, r.URL.Path)
}
w.WriteHeader(http.StatusNoContent)
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
if err := chat.Answer(context.Background(), protocol.ChatAnswerRequest{SessionID: "ses_test", FormID: "form_test", Action: "cancel"}); err != nil {
t.Fatal(err)
}
if calls != 1 {
t.Fatalf("calls=%d", calls)
}
}