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
+5 -1
View File
@@ -30,7 +30,11 @@ func handleChatAnswer(ctx context.Context, request protocol.Message, broker chat
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a form id is required"})
return
}
if len(answer.Answers) == 0 {
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
}
+35 -5
View File
@@ -26,6 +26,7 @@ type browserForms struct {
mu sync.Mutex
pending []protocol.ChatForm
answered int
canceled int
}
func (b browserModelFixture) Forms(context.Context, string) ([]protocol.ChatForm, error) {
@@ -37,13 +38,25 @@ func (b browserModelFixture) Forms(context.Context, string) ([]protocol.ChatForm
func (b browserModelFixture) Answer(_ context.Context, answer protocol.ChatAnswerRequest) error {
b.forms.mu.Lock()
defer b.forms.mu.Unlock()
if answer.Answers["answer"] != "Browser answer" {
if answer.Action != "cancel" && answer.Answers["answer"] != "Browser answer" {
return fmt.Errorf("unexpected answer")
}
if answer.FormID == "form_second" {
if answer.Answers["enabled"] != false || answer.Answers["count"] != float64(0) {
return fmt.Errorf("typed answers were lost")
}
if _, ok := answer.Answers["conditional"]; ok {
return fmt.Errorf("hidden conditional answer leaked")
}
}
for i, form := range b.forms.pending {
if form.ID == answer.FormID {
b.forms.pending = append(b.forms.pending[:i], b.forms.pending[i+1:]...)
b.forms.answered++
if answer.Action == "cancel" {
b.forms.canceled++
} else {
b.forms.answered++
}
return nil
}
}
@@ -109,6 +122,13 @@ func TestChatBrowserLifecycleAndModelDialog(t *testing.T) {
handler := HandlerWithOptions(ctx, Options{Chat: browserModelFixture{fake, forms}}, store)
defer func() { cancel(); WaitRuns(handler) }()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/fixture-cancel" && req.Method == http.MethodPost {
forms.mu.Lock()
forms.pending = append(forms.pending, protocol.ChatForm{ID: "form_cancel", SessionID: session.ID, Title: "Cancel this question", Actions: []string{"accept", "cancel"}, Fields: []protocol.ChatFormField{{Key: "answer", Type: "string", Required: true}}})
forms.mu.Unlock()
w.WriteHeader(http.StatusNoContent)
return
}
if req.URL.Path == "/fixture-attention" && req.Method == http.MethodPost {
fake.mu.Lock()
fake.permissions = []protocol.ChatPermissionFrame{
@@ -118,7 +138,16 @@ func TestChatBrowserLifecycleAndModelDialog(t *testing.T) {
fake.mu.Unlock()
forms.mu.Lock()
for _, id := range []string{"form_first", "form_second"} {
forms.pending = append(forms.pending, protocol.ChatForm{ID: id, SessionID: session.ID, Title: "Recovered question", Fields: []protocol.ChatFormField{{Key: "answer", Type: "text", Title: "Your answer", Required: true}}})
fields := []protocol.ChatFormField{{Key: "answer", Type: "string", Title: "Your answer", Required: true}}
if id == "form_second" {
fields = append(fields,
protocol.ChatFormField{Key: "enabled", Type: "boolean", Title: "Enabled", Required: true},
protocol.ChatFormField{Key: "count", Type: "integer", Title: "Count", Minimum: 0, Maximum: 5, Required: true},
protocol.ChatFormField{Key: "secret", Type: "string", Title: "Secret", Secret: true},
protocol.ChatFormField{Key: "conditional", Type: "string", Title: "Only when enabled", Required: true, When: []protocol.ChatFormWhen{{Key: "enabled", Op: "eq", Value: true}}},
)
}
forms.pending = append(forms.pending, protocol.ChatForm{ID: id, SessionID: session.ID, Title: "Recovered question", Fields: fields})
}
forms.mu.Unlock()
w.WriteHeader(http.StatusNoContent)
@@ -138,8 +167,9 @@ func TestChatBrowserLifecycleAndModelDialog(t *testing.T) {
fake.mu.Unlock()
forms.mu.Lock()
answers := forms.answered
canceled := forms.canceled
forms.mu.Unlock()
if replies != 2 || answers != 2 {
t.Fatalf("browser did not deliver all interactions: approvals=%d answers=%d", replies, answers)
if replies != 2 || answers != 2 || canceled != 1 {
t.Fatalf("browser did not deliver all interactions: approvals=%d answers=%d canceled=%d", replies, answers, canceled)
}
}