From af9677e46dae8bf81d2a40f494605e5e54f7ba1d Mon Sep 17 00:00:00 2001 From: amplace <81105770+AmPlace@users.noreply.github.com> Date: Thu, 23 Jul 2026 18:08:03 +0800 Subject: [PATCH] add code to responses failure errors --- internal/protocol/responses/responses.go | 2 +- internal/protocol/responses/responses_test.go | 3 +++ internal/protocol/responses/stream.go | 2 +- internal/protocol/responses/stream_test.go | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/protocol/responses/responses.go b/internal/protocol/responses/responses.go index ea00a052..86305c5f 100644 --- a/internal/protocol/responses/responses.go +++ b/internal/protocol/responses/responses.go @@ -80,7 +80,7 @@ func Failure(responseID, model, message, errorType string) []string { } failed := map[string]any{ "id": responseID, "object": "response", "status": "failed", "model": model, - "error": map[string]any{"type": errorType, "message": message}, + "error": map[string]any{"type": errorType, "code": errorType, "message": message}, } return []string{ seq.Event("response.created", map[string]any{"response": initial}), diff --git a/internal/protocol/responses/responses_test.go b/internal/protocol/responses/responses_test.go index 44944e05..004bafb2 100644 --- a/internal/protocol/responses/responses_test.go +++ b/internal/protocol/responses/responses_test.go @@ -11,6 +11,9 @@ func TestFailureSequenceIsMonotonicAndDone(t *testing.T) { if len(frames) != 4 || frames[3] != "data: [DONE]\n\n" { t.Fatalf("unexpected frames %#v", frames) } + if !strings.Contains(frames[2], `"code":"server_error"`) { + t.Fatalf("response.failed missing error code: %s", frames[2]) + } for index, frame := range frames[:3] { parts := strings.Split(frame, "data: ") if len(parts) != 2 { diff --git a/internal/protocol/responses/stream.go b/internal/protocol/responses/stream.go index 8b28de59..20a4b3d5 100644 --- a/internal/protocol/responses/stream.go +++ b/internal/protocol/responses/stream.go @@ -1117,7 +1117,7 @@ func (s *LiveStreamer) Fail(message, errorType string) []string { frames := s.Start() failed := map[string]any{ "id": s.responseID, "object": "response", "status": "failed", "model": s.model, - "error": map[string]any{"type": errorType, "message": message}, + "error": map[string]any{"type": errorType, "code": errorType, "message": message}, } frames = append(frames, s.sequence.Event("response.failed", map[string]any{"response": failed}), diff --git a/internal/protocol/responses/stream_test.go b/internal/protocol/responses/stream_test.go index e4f486a1..ed3fc1ba 100644 --- a/internal/protocol/responses/stream_test.go +++ b/internal/protocol/responses/stream_test.go @@ -17,6 +17,9 @@ func TestEmptyCompleteCanStillFail(t *testing.T) { if len(failed) != 2 || !strings.Contains(failed[0], "response.failed") || failed[1] != "data: [DONE]\n\n" { t.Fatalf("unexpected failure %#v", failed) } + if !strings.Contains(failed[0], `"code":"server_error"`) { + t.Fatalf("response.failed missing error code: %s", failed[0]) + } } func TestToolStreamUsesStableIDsAndMonotonicSequence(t *testing.T) {