mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-18 15:48:39 +08:00
feat(api): filter private metadata in OpenAI responses and improve SSE framing recovery
- Filter out internal metadata events such as moderation data for standard clients in OpenAI responses API and websocket streams. - Enhance `responsesSSEFramer` and frame accumulator to robustly handle fragmented JSON payloads, empty data fields, and recovery of split SSE fields. - Add comprehensive unit tests covering SSE metadata filtering, fragmented frame recovery, and websocket message handling.
This commit is contained in:
@@ -52,7 +52,8 @@ type imageCallResult struct {
|
||||
}
|
||||
|
||||
type sseFrameAccumulator struct {
|
||||
pending []byte
|
||||
pending []byte
|
||||
syntheticBreaks []int
|
||||
}
|
||||
|
||||
type xaiImageResult struct {
|
||||
@@ -151,33 +152,34 @@ func (a *sseFrameAccumulator) AddChunk(chunk []byte) [][]byte {
|
||||
|
||||
var frames [][]byte
|
||||
if responsesSSEStartsNewDataFrame(a.pending, chunk) {
|
||||
frames = append(frames, bytes.Clone(a.pending))
|
||||
frames = append(frames, bytes.Clone(responsesSSERecoverJSONFrame(a.pending, a.syntheticBreaks)))
|
||||
a.pending = a.pending[:0]
|
||||
a.syntheticBreaks = a.syntheticBreaks[:0]
|
||||
}
|
||||
if responsesSSENeedsLineBreak(a.pending, chunk) {
|
||||
a.pending = append(a.pending, '\n')
|
||||
}
|
||||
a.pending = append(a.pending, chunk...)
|
||||
a.pending = responsesSSEAppendChunk(a.pending, &a.syntheticBreaks, chunk)
|
||||
|
||||
for {
|
||||
frameLen := responsesSSEFrameLen(a.pending)
|
||||
if frameLen == 0 {
|
||||
break
|
||||
}
|
||||
frames = append(frames, bytes.Clone(a.pending[:frameLen]))
|
||||
frames = append(frames, bytes.Clone(responsesSSERecoverJSONFrame(a.pending[:frameLen], a.syntheticBreaks)))
|
||||
copy(a.pending, a.pending[frameLen:])
|
||||
a.pending = a.pending[:len(a.pending)-frameLen]
|
||||
a.syntheticBreaks = responsesSSEConsumeBreaks(a.syntheticBreaks, frameLen)
|
||||
}
|
||||
|
||||
if len(bytes.TrimSpace(a.pending)) == 0 {
|
||||
a.pending = a.pending[:0]
|
||||
a.syntheticBreaks = a.syntheticBreaks[:0]
|
||||
return frames
|
||||
}
|
||||
if len(a.pending) == 0 || !responsesSSECanEmitWithoutDelimiter(a.pending) {
|
||||
return frames
|
||||
}
|
||||
frames = append(frames, bytes.Clone(a.pending))
|
||||
frames = append(frames, bytes.Clone(responsesSSERecoverJSONFrame(a.pending, a.syntheticBreaks)))
|
||||
a.pending = a.pending[:0]
|
||||
a.syntheticBreaks = a.syntheticBreaks[:0]
|
||||
return frames
|
||||
}
|
||||
|
||||
@@ -192,19 +194,22 @@ func (a *sseFrameAccumulator) Flush() [][]byte {
|
||||
if frameLen == 0 {
|
||||
break
|
||||
}
|
||||
frames = append(frames, bytes.Clone(a.pending[:frameLen]))
|
||||
frames = append(frames, bytes.Clone(responsesSSERecoverJSONFrame(a.pending[:frameLen], a.syntheticBreaks)))
|
||||
copy(a.pending, a.pending[frameLen:])
|
||||
a.pending = a.pending[:len(a.pending)-frameLen]
|
||||
a.syntheticBreaks = responsesSSEConsumeBreaks(a.syntheticBreaks, frameLen)
|
||||
}
|
||||
|
||||
if len(bytes.TrimSpace(a.pending)) == 0 {
|
||||
a.pending = nil
|
||||
return frames
|
||||
}
|
||||
if responsesSSECanFlushWithoutDelimiter(a.pending) {
|
||||
frames = append(frames, bytes.Clone(a.pending))
|
||||
frame := responsesSSERecoverJSONFrame(a.pending, a.syntheticBreaks)
|
||||
if responsesSSECanFlushWithoutDelimiter(frame) {
|
||||
frames = append(frames, bytes.Clone(frame))
|
||||
}
|
||||
a.pending = nil
|
||||
a.syntheticBreaks = nil
|
||||
return frames
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,321 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/interfaces"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestResponsesSSEFramerFragmentedMetadataDataField(t *testing.T) {
|
||||
payload := `{"type":"response.metadata","metadata":{"marker":"private"}}`
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
lineEnding string
|
||||
isCodexClient bool
|
||||
}{
|
||||
{name: "standard-lf", lineEnding: "\n"},
|
||||
{name: "codex-lf", lineEnding: "\n", isCodexClient: true},
|
||||
{name: "standard-crlf", lineEnding: "\r\n"},
|
||||
{name: "codex-crlf", lineEnding: "\r\n", isCodexClient: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
prefix := "event: response.metadata" + tc.lineEnding + "data:"
|
||||
frame := []byte(prefix + " " + payload + tc.lineEnding + tc.lineEnding)
|
||||
for _, split := range []int{len(prefix), len(prefix) + 1} {
|
||||
var output bytes.Buffer
|
||||
framer := &responsesSSEFramer{isCodexClient: tc.isCodexClient}
|
||||
framer.WriteChunk(&output, frame[:split])
|
||||
framer.WriteChunk(&output, frame[split:])
|
||||
framer.WriteChunk(&output, []byte("event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n"))
|
||||
got := output.String()
|
||||
if tc.isCodexClient {
|
||||
if !strings.Contains(got, prefix+" "+payload) {
|
||||
t.Fatalf("split %d: Codex metadata missing or damaged: %q", split, got)
|
||||
}
|
||||
} else if strings.Contains(got, "response.metadata") || strings.Contains(got, "private") {
|
||||
t.Fatalf("split %d: private metadata leaked: %q", split, got)
|
||||
}
|
||||
if !strings.Contains(got, "response.completed") {
|
||||
t.Fatalf("split %d: completion missing: %q", split, got)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardResponsesStreamEmptyMetadataBeforeFragmentedCompletion(t *testing.T) {
|
||||
completed := []byte("event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n")
|
||||
for split := 1; split <= len("event:"); split++ {
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 3)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
data <- completed[:split]
|
||||
data <- completed[split:]
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "event: response.completed") {
|
||||
t.Fatalf("split %d: empty metadata consumed completion: %q", split, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardResponsesStreamEmptyMetadataBeforeLeadingSeparator(t *testing.T) {
|
||||
for _, prefix := range []string{"\n", "\r\n", ": keepalive\n", "id: event-42\n", "retry: 1000\n", "x-extra: value\n"} {
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 2)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
data <- []byte(prefix + "event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n")
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "response.completed") {
|
||||
t.Fatalf("prefix %q: empty metadata consumed completion: %q", prefix, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardResponsesStreamEmptyMetadataBeforeFragmentedFields(t *testing.T) {
|
||||
for _, prefix := range []string{"id: event-42\n", "retry: 1000\n", "x-extra: value\n"} {
|
||||
field := []byte(prefix + "event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n")
|
||||
for split := 1; split < strings.IndexByte(prefix, ':')+1; split++ {
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 3)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
data <- field[:split]
|
||||
data <- field[split:]
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "response.completed") {
|
||||
t.Fatalf("prefix %q split %d: empty metadata consumed completion: %q", prefix, split, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardResponsesStreamEmptyMetadataBeforeDataOnlyCompletion(t *testing.T) {
|
||||
completed := []byte("data: {\"type\":\"response.completed\"}\n\n")
|
||||
for split := 1; split <= len("data:"); split++ {
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 3)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
data <- completed[:split]
|
||||
data <- completed[split:]
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "response.completed") {
|
||||
t.Fatalf("split %d: empty metadata consumed data-only completion: %q", split, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardResponsesStreamEmptyMetadataBeforeDataThenEvent(t *testing.T) {
|
||||
created := []byte(`data: {"response":{"id":"resp-1","status":"completed"}}`)
|
||||
completed := []byte("event: response.completed\n\n")
|
||||
for split := 1; split <= len("event:"); split++ {
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 4)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
data <- created
|
||||
data <- completed[:split]
|
||||
data <- completed[split:]
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "event: response.completed") || !strings.Contains(got, "resp-1") {
|
||||
t.Fatalf("split %d: data-before-event completion lost: %q", split, got)
|
||||
}
|
||||
}
|
||||
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 2)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
data <- append(append(bytes.Clone(created), '\n'), completed...)
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "event: response.completed") || !strings.Contains(got, "resp-1") {
|
||||
t.Fatalf("combined data-before-event completion lost: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardResponsesStreamEmptyMetadataBeforeDataFieldThenEvent(t *testing.T) {
|
||||
for _, field := range []string{"id: event-42", "retry: 1000", ": keepalive", "x-extra: value"} {
|
||||
for split := 1; split <= len(field); split++ {
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 5)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
data <- []byte(`data: {"response":{"id":"resp-1","status":"completed"}}`)
|
||||
data <- []byte(field[:split])
|
||||
data <- []byte(field[split:])
|
||||
data <- []byte("event: response.completed\n\n")
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "event: response.completed") || !strings.Contains(got, "resp-1") {
|
||||
t.Fatalf("field %q split %d: data-before-event completion lost: %q", field, split, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardResponsesStreamEmptyMetadataBeforeDataAndFragmentedEventAfterLineBreak(t *testing.T) {
|
||||
for _, field := range []string{"", "id: event-42\n", ": keepalive\n"} {
|
||||
for split := 1; split < len("event:"); split++ {
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 5)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
data <- []byte("data: {\"response\":{\"id\":\"resp-1\",\"status\":\"completed\"}}\n")
|
||||
if field != "" {
|
||||
data <- []byte(field)
|
||||
}
|
||||
event := []byte("event: response.completed\n\n")
|
||||
data <- event[:split]
|
||||
data <- event[split:]
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "event: response.completed") || !strings.Contains(got, "resp-1") {
|
||||
t.Fatalf("field %q split %d: completion lost: %q", field, split, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardResponsesStreamEmptyMetadataBeforeDataWithFragmentedEvent(t *testing.T) {
|
||||
for _, field := range []string{"", "id: event-42\n", ": keepalive\n"} {
|
||||
for split := 1; split < len("event:"); split++ {
|
||||
h, recorder, c, flusher := newResponsesStreamTestHandler(t)
|
||||
data := make(chan []byte, 3)
|
||||
errs := make(chan *interfaces.ErrorMessage)
|
||||
data <- []byte("event: response.metadata\ndata:")
|
||||
event := []byte("event: response.completed\n\n")
|
||||
data <- append([]byte("data: {\"response\":{\"id\":\"resp-1\",\"status\":\"completed\"}}\n"+field), event[:split]...)
|
||||
data <- event[split:]
|
||||
close(data)
|
||||
close(errs)
|
||||
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil)
|
||||
got := recorder.Body.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "event: error") || !strings.Contains(got, "event: response.completed") || !strings.Contains(got, "resp-1") {
|
||||
t.Fatalf("field %q split %d: completion lost: %q", field, split, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponsesSSEFramerPreservesEmptyEvent(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
framer := &responsesSSEFramer{}
|
||||
framer.WriteChunk(&output, []byte("event: custom\ndata:"))
|
||||
if output.Len() != 0 {
|
||||
t.Fatalf("empty data field was emitted before completion: %q", output.String())
|
||||
}
|
||||
framer.WriteChunk(&output, []byte("\n\n"))
|
||||
if got, want := output.String(), "event: custom\ndata:\n\n"; got != want {
|
||||
t.Fatalf("empty data frame = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponsesSSEFramerMetadataWithFieldPrefixInJSONString(t *testing.T) {
|
||||
for _, marker := range []string{"data: private", "event: private", "id: private", "retry: private", ": private", `escaped " data: private`, `escaped \ event: private`} {
|
||||
for _, isCodexClient := range []bool{false, true} {
|
||||
encodedMarker, errMarshal := json.Marshal(marker)
|
||||
if errMarshal != nil {
|
||||
t.Fatalf("marshal marker: %v", errMarshal)
|
||||
}
|
||||
payload := `{"type":"response.metadata","metadata":{"marker":` + string(encodedMarker) + `}}`
|
||||
frame := []byte("data: " + payload + "\n\n")
|
||||
field := strings.Index(string(frame), strings.TrimPrefix(marker, `escaped " `))
|
||||
if field < 0 {
|
||||
field = strings.Index(string(frame), "event: private")
|
||||
}
|
||||
if field < 0 {
|
||||
t.Fatalf("marker %q not found in %q", marker, frame)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
framer := &responsesSSEFramer{isCodexClient: isCodexClient}
|
||||
framer.WriteChunk(&output, frame[:field])
|
||||
framer.WriteChunk(&output, frame[field:])
|
||||
framer.WriteChunk(&output, []byte("event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n"))
|
||||
got := output.String()
|
||||
if isCodexClient {
|
||||
if !strings.Contains(got, payload) {
|
||||
t.Fatalf("marker %q: Codex payload corrupted: %q", marker, got)
|
||||
}
|
||||
} else if strings.Contains(got, "response.metadata") || strings.Contains(got, "private") {
|
||||
t.Fatalf("marker %q: metadata leaked: %q", marker, got)
|
||||
}
|
||||
if !strings.Contains(got, "response.completed") {
|
||||
t.Fatalf("marker %q: completion missing: %q", marker, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponsesSSEFramerFiltersMalformedPrivatePayload(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
framer := &responsesSSEFramer{}
|
||||
framer.WriteChunk(&output, []byte("data: {\"type\":\"response.metadata\",\"metadata\":{\"marker\":\"private\"}\n\n"))
|
||||
framer.WriteChunk(&output, []byte("event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n"))
|
||||
if got := output.String(); strings.Contains(got, "response.metadata") || strings.Contains(got, "private") {
|
||||
t.Fatalf("malformed private payload leaked: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponsesSSEFramerKeepsEmptyCommentInMultilineData(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
framer := &responsesSSEFramer{}
|
||||
for _, chunk := range []string{
|
||||
`data: {"type"`,
|
||||
":",
|
||||
`data: :"response.metadata","metadata":{"marker":"private"}}` + "\n\n",
|
||||
"event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n",
|
||||
} {
|
||||
framer.WriteChunk(&output, []byte(chunk))
|
||||
}
|
||||
got := output.String()
|
||||
if strings.Contains(got, "response.metadata") || strings.Contains(got, "private") || !strings.Contains(got, "response.completed") {
|
||||
t.Fatalf("metadata with empty comment leaked: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSEFrameAccumulatorKeepsEmptyCommentInMultilineData(t *testing.T) {
|
||||
acc := &sseFrameAccumulator{}
|
||||
var frames [][]byte
|
||||
for _, chunk := range []string{
|
||||
`data: {"type"`,
|
||||
":",
|
||||
`data: :"response.completed","response":{"output":[]}}` + "\n\n",
|
||||
} {
|
||||
frames = append(frames, acc.AddChunk([]byte(chunk))...)
|
||||
}
|
||||
if len(frames) != 1 {
|
||||
t.Fatalf("frames = %q, want one", frames)
|
||||
}
|
||||
payload, ok := responsesSSEDataPayload(frames[0])
|
||||
if !ok || !json.Valid(payload) || gjson.GetBytes(payload, "type").String() != "response.completed" {
|
||||
t.Fatalf("empty comment corrupted image data: %q", frames[0])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestResponsesSSEFramerRecoversFragmentedJSON(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
chunks []string
|
||||
}{
|
||||
{
|
||||
name: "multiline-json",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`:"response.metadata",` + "\ndata: " + `"metadata":{"marker":"private"}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple-colons",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`:"response.metadata","metadata"`,
|
||||
`:{"marker":"private"}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "real-comment",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`:"response.metadata",` + "\n: keepalive\ndata: " + `"metadata":{"marker":"private"}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "split-data-field",
|
||||
chunks: []string{
|
||||
`data`,
|
||||
`: {"type":"response.metadata","metadata":{"marker":"private"}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple-colons-with-comment",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`:"response.metadata",` + "\n: keepalive\ndata: " + `"metadata"`,
|
||||
`:{"marker":"private"}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "separate-comment-with-split-key",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`: keepalive`,
|
||||
`data: :"response.metadata","metadata"`,
|
||||
`:{"marker":"private"}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "colon-before-string-field",
|
||||
chunks: []string{
|
||||
`data: {"type":"response.metadata","metadata":{"marker"`,
|
||||
`:"`,
|
||||
`data: private"}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
} {
|
||||
for _, isCodexClient := range []bool{false, true} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
framer := &responsesSSEFramer{isCodexClient: isCodexClient}
|
||||
for _, chunk := range tc.chunks {
|
||||
framer.WriteChunk(&output, []byte(chunk))
|
||||
}
|
||||
framer.WriteChunk(&output, []byte("event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n"))
|
||||
got := output.String()
|
||||
if isCodexClient {
|
||||
frames := strings.Split(got, "\n\n")
|
||||
payload, ok := responsesSSEDataPayload([]byte(frames[0]))
|
||||
wantMarker := "private"
|
||||
if tc.name == "colon-before-string-field" {
|
||||
wantMarker = "data: private"
|
||||
}
|
||||
if !ok || !json.Valid(payload) || gjson.GetBytes(payload, "type").String() != "response.metadata" || gjson.GetBytes(payload, "metadata.marker").String() != wantMarker {
|
||||
t.Fatalf("Codex metadata was corrupted: %q", got)
|
||||
}
|
||||
if strings.Contains(strings.Join(tc.chunks, ""), ": keepalive") && !strings.Contains(got, ": keepalive") {
|
||||
t.Fatalf("genuine SSE comment was removed: %q", got)
|
||||
}
|
||||
} else if strings.Contains(got, "response.metadata") || strings.Contains(got, "private") {
|
||||
t.Fatalf("private metadata leaked: %q", got)
|
||||
}
|
||||
if !strings.Contains(got, "response.completed") {
|
||||
t.Fatalf("completion missing: %q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSEFrameAccumulatorRecoversFragmentedJSON(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
chunks []string
|
||||
}{
|
||||
{
|
||||
name: "multiline-json",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`:"response.completed",` + "\ndata: " + `"response":{"output":[]}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple-colons",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`:"response.completed","response"`,
|
||||
`:{"output":[]}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "real-comment",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`:"response.completed",` + "\n: keepalive\ndata: " + `"response":{"output":[]}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "split-data-field",
|
||||
chunks: []string{
|
||||
`data`,
|
||||
`: {"type":"response.completed","response":{"output":[]}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple-colons-with-comment",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`:"response.completed",` + "\n: keepalive\ndata: " + `"response"`,
|
||||
`:{"output":[]}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "separate-comment-with-split-key",
|
||||
chunks: []string{
|
||||
`data: {"type"`,
|
||||
`: keepalive`,
|
||||
`data: :"response.completed","response"`,
|
||||
`:{"output":[]}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "colon-before-string-field",
|
||||
chunks: []string{
|
||||
`data: {"type":"response.completed","metadata":{"marker"`,
|
||||
`:"`,
|
||||
`data: private"}}` + "\n\n",
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
acc := &sseFrameAccumulator{}
|
||||
var frames [][]byte
|
||||
for _, chunk := range tc.chunks {
|
||||
frames = append(frames, acc.AddChunk([]byte(chunk))...)
|
||||
}
|
||||
if len(frames) != 1 {
|
||||
t.Fatalf("frames = %q, want one", frames)
|
||||
}
|
||||
payload, ok := responsesSSEDataPayload(frames[0])
|
||||
if !ok || !json.Valid(payload) || gjson.GetBytes(payload, "type").String() != "response.completed" {
|
||||
t.Fatalf("image response was corrupted: %q", frames[0])
|
||||
}
|
||||
if strings.Contains(strings.Join(tc.chunks, ""), ": keepalive") && !bytes.Contains(frames[0], []byte(": keepalive")) {
|
||||
t.Fatalf("genuine SSE comment was removed: %q", frames[0])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,9 @@ func flushResponsesSSE(w http.ResponseWriter) error {
|
||||
|
||||
type responsesSSEFramer struct {
|
||||
pending []byte
|
||||
nextFramePrefix []byte
|
||||
nextFieldSuffix []byte
|
||||
syntheticBreaks []int
|
||||
outputItems map[int][]byte
|
||||
outputOrder []int
|
||||
unindexedOutputItems [][]byte
|
||||
@@ -81,17 +84,102 @@ func (f *responsesSSEFramer) WriteChunk(w io.Writer, chunk []byte) {
|
||||
if len(chunk) == 0 || f.terminalEvent != "" {
|
||||
return
|
||||
}
|
||||
if len(f.nextFieldSuffix) > 0 {
|
||||
trimmed := bytes.TrimLeft(chunk, " \t\r\n")
|
||||
_, complete, _ := responsesSSEChunkField(trimmed)
|
||||
lastLine := bytes.TrimSpace(f.nextFramePrefix[bytes.LastIndexByte(f.nextFramePrefix, '\n')+1:])
|
||||
if complete && len(lastLine) > 0 && !bytes.HasPrefix(lastLine, []byte("data:")) {
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, f.nextFieldSuffix...)
|
||||
f.nextFieldSuffix = nil
|
||||
} else {
|
||||
f.nextFieldSuffix = append(f.nextFieldSuffix, chunk...)
|
||||
trimmed = bytes.TrimLeft(f.nextFieldSuffix, " \t\r\n")
|
||||
_, _, partial := responsesSSEChunkField(trimmed)
|
||||
if partial {
|
||||
return
|
||||
}
|
||||
chunk = f.nextFieldSuffix
|
||||
f.nextFieldSuffix = nil
|
||||
}
|
||||
}
|
||||
if len(f.nextFramePrefix) > 0 {
|
||||
payload, found := responsesSSEDataPayload(f.nextFramePrefix)
|
||||
if found && json.Valid(payload) && gjson.GetBytes(payload, "type").String() == "" &&
|
||||
!responsesSSEHasField(f.nextFramePrefix, []byte("event:")) && responsesSSEFrameLen(f.nextFramePrefix) == 0 {
|
||||
trimmed := bytes.TrimLeft(chunk, " \t\r\n")
|
||||
field, complete, partial := responsesSSEChunkField(trimmed)
|
||||
lastLine := bytes.TrimSpace(f.nextFramePrefix[bytes.LastIndexByte(f.nextFramePrefix, '\n')+1:])
|
||||
_, _, lastPartial := responsesSSEChunkField(lastLine)
|
||||
if complete && lastPartial {
|
||||
joined := append(bytes.Clone(lastLine), trimmed...)
|
||||
joinedField, joinedComplete, _ := responsesSSEChunkField(joined)
|
||||
if joinedComplete && (joinedField == "event" || joinedField == "data" || joinedField == "id" || joinedField == "retry" ||
|
||||
(field != "" && field != "event" && field != "data" && field != "id" && field != "retry")) {
|
||||
complete = false
|
||||
}
|
||||
}
|
||||
newFieldPrefix := bytes.HasPrefix(lastLine, []byte("data:"))
|
||||
for _, prefix := range [][]byte{[]byte("event:"), []byte("data:"), []byte("id:"), []byte("retry:")} {
|
||||
newFieldPrefix = newFieldPrefix || (len(trimmed) < len(prefix) && bytes.HasPrefix(prefix, trimmed))
|
||||
}
|
||||
if partial && newFieldPrefix {
|
||||
f.nextFieldSuffix = append(f.nextFieldSuffix, chunk...)
|
||||
return
|
||||
}
|
||||
if complete && !bytes.HasSuffix(f.nextFramePrefix, []byte("\n")) && !bytes.HasSuffix(f.nextFramePrefix, []byte("\r")) &&
|
||||
chunk[0] != '\n' && chunk[0] != '\r' {
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, '\n')
|
||||
}
|
||||
}
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, chunk...)
|
||||
chunk = f.nextFramePrefix
|
||||
f.nextFramePrefix = nil
|
||||
}
|
||||
if responsesSSEHasEmptyEventData(f.pending) {
|
||||
trimmed := bytes.TrimLeft(chunk, " \t\r\n")
|
||||
field, complete, partial := responsesSSEChunkField(trimmed)
|
||||
if partial {
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, chunk...)
|
||||
return
|
||||
}
|
||||
newFrame := complete && field != "data"
|
||||
if complete && field == "data" {
|
||||
payload, _ := responsesSSEDataPayload(trimmed)
|
||||
valid := json.Valid(payload)
|
||||
if !valid && !bytes.Equal(payload, []byte("[DONE]")) && responsesSSEFrameLen(trimmed) == 0 {
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, chunk...)
|
||||
return
|
||||
}
|
||||
hasEvent := responsesSSEHasField(trimmed, []byte("event:"))
|
||||
if valid && hasEvent && responsesSSEEventName(trimmed) == "" && responsesSSEFrameLen(trimmed) == 0 {
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, chunk...)
|
||||
return
|
||||
}
|
||||
newFrame = bytes.Equal(payload, []byte("[DONE]")) ||
|
||||
(valid && (gjson.GetBytes(payload, "type").String() != "" || hasEvent))
|
||||
if valid && !newFrame && responsesSSEFrameLen(trimmed) == 0 {
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, chunk...)
|
||||
return
|
||||
}
|
||||
}
|
||||
if newFrame {
|
||||
f.writeFrame(w, f.pending)
|
||||
f.pending = f.pending[:0]
|
||||
f.syntheticBreaks = f.syntheticBreaks[:0]
|
||||
if f.terminalEvent != "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if responsesSSEStartsNewDataFrame(f.pending, chunk) {
|
||||
f.writeFrame(w, f.pending)
|
||||
f.pending = f.pending[:0]
|
||||
f.syntheticBreaks = f.syntheticBreaks[:0]
|
||||
if f.terminalEvent != "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
if responsesSSENeedsLineBreak(f.pending, chunk) {
|
||||
f.pending = append(f.pending, '\n')
|
||||
}
|
||||
f.pending = append(f.pending, chunk...)
|
||||
f.pending = responsesSSEAppendChunk(f.pending, &f.syntheticBreaks, chunk)
|
||||
for {
|
||||
frameLen := responsesSSEFrameLen(f.pending)
|
||||
if frameLen == 0 {
|
||||
@@ -100,6 +188,7 @@ func (f *responsesSSEFramer) WriteChunk(w io.Writer, chunk []byte) {
|
||||
f.writeFrame(w, f.pending[:frameLen])
|
||||
copy(f.pending, f.pending[frameLen:])
|
||||
f.pending = f.pending[:len(f.pending)-frameLen]
|
||||
f.syntheticBreaks = responsesSSEConsumeBreaks(f.syntheticBreaks, frameLen)
|
||||
if f.terminalEvent != "" {
|
||||
f.pending = f.pending[:0]
|
||||
return
|
||||
@@ -107,16 +196,36 @@ func (f *responsesSSEFramer) WriteChunk(w io.Writer, chunk []byte) {
|
||||
}
|
||||
if len(bytes.TrimSpace(f.pending)) == 0 {
|
||||
f.pending = f.pending[:0]
|
||||
f.syntheticBreaks = f.syntheticBreaks[:0]
|
||||
return
|
||||
}
|
||||
if len(f.pending) == 0 || !responsesSSECanEmitWithoutDelimiter(f.pending) {
|
||||
if len(f.pending) == 0 || responsesSSEHasEmptyEventData(f.pending) || !responsesSSECanEmitWithoutDelimiter(f.pending) {
|
||||
return
|
||||
}
|
||||
f.writeFrame(w, f.pending)
|
||||
f.pending = f.pending[:0]
|
||||
f.syntheticBreaks = f.syntheticBreaks[:0]
|
||||
}
|
||||
|
||||
func (f *responsesSSEFramer) Flush(w io.Writer) {
|
||||
if len(f.nextFieldSuffix) > 0 {
|
||||
if responsesSSENeedsLineBreak(f.nextFramePrefix, f.nextFieldSuffix) {
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, '\n')
|
||||
}
|
||||
f.nextFramePrefix = append(f.nextFramePrefix, f.nextFieldSuffix...)
|
||||
f.nextFieldSuffix = nil
|
||||
}
|
||||
if len(f.nextFramePrefix) > 0 {
|
||||
trimmed := bytes.TrimLeft(f.nextFramePrefix, " \t\r\n")
|
||||
_, _, partial := responsesSSEChunkField(trimmed)
|
||||
if partial && !bytes.HasPrefix([]byte("data"), trimmed) {
|
||||
f.writeFrame(w, f.pending)
|
||||
f.pending = f.pending[:0]
|
||||
f.syntheticBreaks = f.syntheticBreaks[:0]
|
||||
}
|
||||
f.pending = responsesSSEAppendChunk(f.pending, &f.syntheticBreaks, f.nextFramePrefix)
|
||||
f.nextFramePrefix = nil
|
||||
}
|
||||
if len(f.pending) == 0 || f.terminalEvent != "" {
|
||||
return
|
||||
}
|
||||
@@ -124,17 +233,19 @@ func (f *responsesSSEFramer) Flush(w io.Writer) {
|
||||
f.pending = f.pending[:0]
|
||||
return
|
||||
}
|
||||
if !responsesSSECanFlushWithoutDelimiter(f.pending) {
|
||||
frame := responsesSSERecoverJSONFrame(f.pending, f.syntheticBreaks)
|
||||
if !responsesSSECanFlushWithoutDelimiter(frame) {
|
||||
f.pending = f.pending[:0]
|
||||
return
|
||||
}
|
||||
f.writeFrame(w, f.pending)
|
||||
f.writeFrame(w, frame)
|
||||
f.pending = f.pending[:0]
|
||||
f.syntheticBreaks = f.syntheticBreaks[:0]
|
||||
}
|
||||
|
||||
func (f *responsesSSEFramer) writeFrame(w io.Writer, frame []byte) {
|
||||
if f.writeErr == nil {
|
||||
f.writeErr = writeResponsesSSEChunk(w, f.repairFrame(frame))
|
||||
f.writeErr = writeResponsesSSEChunk(w, f.repairFrame(responsesSSERecoverJSONFrame(frame, f.syntheticBreaks)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,14 +298,13 @@ func (f *responsesSSEFramer) repairFrame(frame []byte) []byte {
|
||||
f.dataFrames++
|
||||
return frame
|
||||
}
|
||||
if !json.Valid(payload) {
|
||||
return frame
|
||||
}
|
||||
|
||||
payloadType := gjson.GetBytes(payload, "type").String()
|
||||
if f.shouldFilterPrivateEvent(streamEvent, payloadType) {
|
||||
return nil
|
||||
}
|
||||
if !json.Valid(payload) {
|
||||
return frame
|
||||
}
|
||||
|
||||
f.dataFrames++
|
||||
|
||||
@@ -333,6 +443,73 @@ func responsesSSEDataPayload(frame []byte) ([]byte, bool) {
|
||||
return payload, found
|
||||
}
|
||||
|
||||
func responsesSSERecoverJSONFrame(frame []byte, syntheticBreaks []int) []byte {
|
||||
if len(syntheticBreaks) == 0 {
|
||||
return frame
|
||||
}
|
||||
payload, found := responsesSSEDataPayload(frame)
|
||||
if found && json.Valid(payload) {
|
||||
return frame
|
||||
}
|
||||
|
||||
// Consider only newlines inserted at chunk boundaries when recovering invalid JSON.
|
||||
positions := make([]int, 0, len(syntheticBreaks))
|
||||
for _, pos := range syntheticBreaks {
|
||||
if pos >= 0 && pos < len(frame) && frame[pos] == '\n' {
|
||||
positions = append(positions, pos)
|
||||
}
|
||||
}
|
||||
try := func(candidate []byte) []byte {
|
||||
recovered, ok := responsesSSEDataPayload(candidate)
|
||||
if !ok || !json.Valid(recovered) {
|
||||
return nil
|
||||
}
|
||||
return responsesSSEFrameWithData(candidate, recovered)
|
||||
}
|
||||
for i, pos := range positions {
|
||||
if i == 8 {
|
||||
break
|
||||
}
|
||||
candidate := make([]byte, 0, len(frame)-1)
|
||||
candidate = append(candidate, frame[:pos]...)
|
||||
candidate = append(candidate, frame[pos+1:]...)
|
||||
if recovered := try(candidate); recovered != nil {
|
||||
return recovered
|
||||
}
|
||||
}
|
||||
if len(positions) > 1 {
|
||||
candidate := make([]byte, 0, len(frame)-len(positions))
|
||||
start := 0
|
||||
for _, pos := range positions {
|
||||
candidate = append(candidate, frame[start:pos]...)
|
||||
start = pos + 1
|
||||
}
|
||||
candidate = append(candidate, frame[start:]...)
|
||||
if recovered := try(candidate); recovered != nil {
|
||||
return recovered
|
||||
}
|
||||
}
|
||||
return frame
|
||||
}
|
||||
|
||||
func responsesSSEAppendChunk(pending []byte, syntheticBreaks *[]int, chunk []byte) []byte {
|
||||
if responsesSSENeedsLineBreak(pending, chunk) {
|
||||
*syntheticBreaks = append(*syntheticBreaks, len(pending))
|
||||
pending = append(pending, '\n')
|
||||
}
|
||||
return append(pending, chunk...)
|
||||
}
|
||||
|
||||
func responsesSSEConsumeBreaks(syntheticBreaks []int, consumed int) []int {
|
||||
remaining := syntheticBreaks[:0]
|
||||
for _, pos := range syntheticBreaks {
|
||||
if pos >= consumed {
|
||||
remaining = append(remaining, pos-consumed)
|
||||
}
|
||||
}
|
||||
return remaining
|
||||
}
|
||||
|
||||
func responsesSSEFrameWithData(frame, payload []byte) []byte {
|
||||
var out bytes.Buffer
|
||||
for _, line := range bytes.Split(frame, []byte("\n")) {
|
||||
@@ -471,6 +648,29 @@ func responsesSSECanEmitWithoutDelimiter(chunk []byte) bool {
|
||||
return responsesSSEDataLinesValid(trimmed)
|
||||
}
|
||||
|
||||
func responsesSSEChunkField(chunk []byte) (name string, complete, partial bool) {
|
||||
if len(chunk) == 0 {
|
||||
return "", false, false
|
||||
}
|
||||
for i, b := range chunk {
|
||||
if b == ':' || b == '\n' || b == '\r' {
|
||||
return string(chunk[:i]), true, false
|
||||
}
|
||||
if !(b >= 'a' && b <= 'z') && !(b >= 'A' && b <= 'Z') && !(b >= '0' && b <= '9') && b != '-' && b != '_' && b != '.' {
|
||||
return "", false, false
|
||||
}
|
||||
}
|
||||
return "", false, true
|
||||
}
|
||||
|
||||
func responsesSSEHasEmptyEventData(chunk []byte) bool {
|
||||
if !responsesSSEHasField(chunk, []byte("event:")) {
|
||||
return false
|
||||
}
|
||||
payload, found := responsesSSEDataPayload(chunk)
|
||||
return found && len(bytes.TrimSpace(payload)) == 0
|
||||
}
|
||||
|
||||
func responsesSSECanFlushWithoutDelimiter(chunk []byte) bool {
|
||||
trimmed := bytes.TrimSpace(chunk)
|
||||
return len(trimmed) > 0 && responsesSSEHasField(trimmed, []byte("data:")) && responsesSSEDataLinesValid(trimmed)
|
||||
@@ -521,12 +721,36 @@ func responsesSSENeedsLineBreak(pending, chunk []byte) bool {
|
||||
}
|
||||
for _, prefix := range [][]byte{[]byte("data:"), []byte("event:"), []byte("id:"), []byte("retry:"), []byte(":")} {
|
||||
if bytes.HasPrefix(trimmed, prefix) {
|
||||
return true
|
||||
return !responsesSSEDataEndsInsideJSONString(pending)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func responsesSSEDataEndsInsideJSONString(chunk []byte) bool {
|
||||
payload, found := responsesSSEDataPayload(chunk)
|
||||
payload = bytes.TrimSpace(payload)
|
||||
if !found || len(payload) == 0 || (payload[0] != '{' && payload[0] != '[' && payload[0] != '"') {
|
||||
return false
|
||||
}
|
||||
inString := false
|
||||
escaped := false
|
||||
for _, b := range payload {
|
||||
if escaped {
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
if inString && b == '\\' {
|
||||
escaped = true
|
||||
continue
|
||||
}
|
||||
if b == '"' {
|
||||
inString = !inString
|
||||
}
|
||||
}
|
||||
return inString
|
||||
}
|
||||
|
||||
// OpenAIResponsesAPIHandler contains the handlers for OpenAIResponses API endpoints.
|
||||
// It holds a pool of clients to interact with the backend service.
|
||||
type OpenAIResponsesAPIHandler struct {
|
||||
|
||||
@@ -178,7 +178,8 @@ func (h *OpenAIResponsesAPIHandler) forwardResponsesWebsocket(
|
||||
payloads := websocketJSONPayloadsFromChunk(chunk)
|
||||
for i := range payloads {
|
||||
eventType := gjson.GetBytes(payloads[i], "type").String()
|
||||
if eventType == "response.subscription_usage" {
|
||||
if eventType == "response.subscription_usage" ||
|
||||
(eventType == "response.metadata" && !isCodexResponsesClientRequest(c)) {
|
||||
continue
|
||||
}
|
||||
if eventType == "response.created" {
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/api/handlers"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
coreexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
sdkconfig "github.com/router-for-me/CLIProxyAPI/v8/sdk/config"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type websocketMetadataExecutor struct {
|
||||
websocketCaptureExecutor
|
||||
}
|
||||
|
||||
func (*websocketMetadataExecutor) ExecuteStream(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (*coreexecutor.StreamResult, error) {
|
||||
chunks := make(chan coreexecutor.StreamChunk, 5)
|
||||
for _, payload := range []string{
|
||||
`data: {"type":"response.created","response":{"id":"resp-1"}}`,
|
||||
`data: {"type":"codex.response.metadata","headers":{"x-turn-state":"turn-1"}}`,
|
||||
`data: {"type":"response.metadata","metadata":{"openai_chatgpt_moderation_metadata":{"prompt":{}}}}`,
|
||||
`data: {"type":"response.subscription_usage","subscription":{"tier":"private"}}`,
|
||||
`data: {"type":"response.completed","response":{"id":"resp-1","output":[]}}`,
|
||||
} {
|
||||
chunks <- coreexecutor.StreamChunk{Payload: []byte(payload)}
|
||||
}
|
||||
close(chunks)
|
||||
return &coreexecutor.StreamResult{Chunks: chunks}, nil
|
||||
}
|
||||
|
||||
func TestResponsesWebsocketFiltersModerationMetadataForStandardClients(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
headers http.Header
|
||||
wantTypes []string
|
||||
}{
|
||||
{
|
||||
name: "standard client",
|
||||
headers: http.Header{"User-Agent": []string{"standard-client/1.0"}},
|
||||
wantTypes: []string{"response.created", "codex.response.metadata", "response.completed"},
|
||||
},
|
||||
{
|
||||
name: "codex user agent",
|
||||
headers: http.Header{"User-Agent": []string{"codex_cli_rs/1.0"}},
|
||||
wantTypes: []string{"response.created", "codex.response.metadata", "response.metadata", "response.completed"},
|
||||
},
|
||||
{
|
||||
name: "codex originator",
|
||||
headers: http.Header{"Originator": []string{"codex_cli_rs"}},
|
||||
wantTypes: []string{"response.created", "codex.response.metadata", "response.metadata", "response.completed"},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
executor := &websocketMetadataExecutor{}
|
||||
manager := coreauth.NewManager(nil, nil, nil)
|
||||
manager.RegisterExecutor(executor)
|
||||
auth := &coreauth.Auth{ID: "websocket-metadata-auth", Provider: executor.Identifier(), Status: coreauth.StatusActive}
|
||||
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
||||
t.Fatalf("register auth: %v", errRegister)
|
||||
}
|
||||
registry.GetGlobalRegistry().RegisterClient(auth.ID, auth.Provider, []*registry.ModelInfo{{ID: "websocket-metadata-model"}})
|
||||
t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(auth.ID) })
|
||||
|
||||
h := NewOpenAIResponsesAPIHandler(handlers.NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, manager))
|
||||
router := gin.New()
|
||||
router.GET("/v1/responses", h.ResponsesWebsocket)
|
||||
server := httptest.NewServer(router)
|
||||
defer server.Close()
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(server.URL, "http") + "/v1/responses"
|
||||
conn, _, errDial := websocket.DefaultDialer.Dial(wsURL, tc.headers)
|
||||
if errDial != nil {
|
||||
t.Fatalf("dial websocket: %v", errDial)
|
||||
}
|
||||
defer func() {
|
||||
if errClose := conn.Close(); errClose != nil {
|
||||
t.Errorf("close websocket: %v", errClose)
|
||||
}
|
||||
}()
|
||||
request := `{"type":"response.create","model":"websocket-metadata-model","input":[]}`
|
||||
if errWrite := conn.WriteMessage(websocket.TextMessage, []byte(request)); errWrite != nil {
|
||||
t.Fatalf("write request: %v", errWrite)
|
||||
}
|
||||
|
||||
var gotTypes []string
|
||||
for {
|
||||
_, payload, errRead := conn.ReadMessage()
|
||||
if errRead != nil {
|
||||
t.Fatalf("read websocket: %v; events: %v", errRead, gotTypes)
|
||||
}
|
||||
eventType := gjson.GetBytes(payload, "type").String()
|
||||
gotTypes = append(gotTypes, eventType)
|
||||
if eventType == "error" {
|
||||
t.Fatalf("unexpected error event: %s", payload)
|
||||
}
|
||||
if eventType == "response.completed" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !slices.Equal(gotTypes, tc.wantTypes) {
|
||||
t.Fatalf("event types = %v, want %v", gotTypes, tc.wantTypes)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user