diff --git a/backend/internal/pkg/apicompat/responses_to_chatcompletions.go b/backend/internal/pkg/apicompat/responses_to_chatcompletions.go index f33d4792b1..e1a7b88c77 100644 --- a/backend/internal/pkg/apicompat/responses_to_chatcompletions.go +++ b/backend/internal/pkg/apicompat/responses_to_chatcompletions.go @@ -503,6 +503,7 @@ type bufferedFuncCall struct { Name string Namespace string EncryptedFunctionArgs json.RawMessage + EncryptedContent string Args strings.Builder } @@ -547,6 +548,7 @@ func (a *BufferedResponseAccumulator) ProcessEvent(event *ResponsesStreamEvent) Name: event.Item.Name, Namespace: event.Item.Namespace, EncryptedFunctionArgs: event.Item.EncryptedFunctionArgs, + EncryptedContent: event.Item.EncryptedContent, }) } case "response.function_call_arguments.delta", "response.custom_tool_call_input.delta": @@ -616,6 +618,7 @@ func (a *BufferedResponseAccumulator) BuildOutput() []ResponsesOutput { Name: a.funcCalls[i].Name, Namespace: a.funcCalls[i].Namespace, EncryptedFunctionArgs: a.funcCalls[i].EncryptedFunctionArgs, + EncryptedContent: a.funcCalls[i].EncryptedContent, Arguments: a.funcCalls[i].Args.String(), }) } diff --git a/backend/internal/service/openai_responses_sealed_call_test.go b/backend/internal/service/openai_responses_sealed_call_test.go index 31d1406c2d..2235990873 100644 --- a/backend/internal/service/openai_responses_sealed_call_test.go +++ b/backend/internal/service/openai_responses_sealed_call_test.go @@ -8,6 +8,7 @@ import ( "net/http/httptest" "strings" "testing" + "time" "github.com/gin-gonic/gin" "github.com/stretchr/testify/require" @@ -18,21 +19,26 @@ func TestOpenAIResponsesSealedFunctionCallRoundTrip(t *testing.T) { gin.SetMode(gin.TestMode) const arguments = `{"message":"synthetic-sealed-message"}` for _, marker := range []struct { - name string - raw string + name string + raw string + encryptedContent string }{ - {name: "encrypted", raw: `["message"]`}, + {name: "encrypted", raw: `["message"]`, encryptedContent: "synthetic-call-ciphertext"}, {name: "plaintext", raw: `[]`}, {name: "null", raw: `null`}, - {name: "absent"}, + {name: "absent", encryptedContent: "synthetic-call-ciphertext"}, } { for _, shape := range []string{"terminal_output", "item_done", "delta_fallback"} { - for _, passthrough := range []bool{false, true} { - mode := "normal" - if passthrough { - mode = "passthrough" - } - t.Run(marker.name+"/"+shape+"/"+mode, func(t *testing.T) { + for _, mode := range []struct { + name string + passthrough bool + streaming bool + }{ + {name: "normal"}, + {name: "passthrough", passthrough: true}, + {name: "streaming", streaming: true}, + } { + t.Run(marker.name+"/"+shape+"/"+mode.name, func(t *testing.T) { item := map[string]any{ "type": "function_call", "call_id": "call_sealed", "name": "send_message", "namespace": "collaboration", "arguments": arguments, @@ -40,6 +46,9 @@ func TestOpenAIResponsesSealedFunctionCallRoundTrip(t *testing.T) { if marker.raw != "" { item["encrypted_function_args"] = json.RawMessage(marker.raw) } + if marker.encryptedContent != "" { + item["encrypted_content"] = marker.encryptedContent + } output := []any{} var frames []string addFrame := func(event map[string]any) { @@ -75,7 +84,11 @@ func TestOpenAIResponsesSealedFunctionCallRoundTrip(t *testing.T) { } account := &Account{ID: 1, Platform: PlatformOpenAI, Type: AccountTypeOAuth} svc := newCompactBridgeTestService() - if passthrough { + if mode.streaming { + result, err := svc.handleStreamingResponse(context.Background(), resp, c, account, time.Now(), "test-model", "test-model") + require.NoError(t, err) + require.Equal(t, 7, result.usage.InputTokens) + } else if mode.passthrough { result, err := svc.handleNonStreamingResponsePassthrough(context.Background(), resp, c, account, "test-model", "test-model") require.NoError(t, err) require.Equal(t, 7, result.usage.InputTokens) @@ -85,8 +98,15 @@ func TestOpenAIResponsesSealedFunctionCallRoundTrip(t *testing.T) { require.Equal(t, 7, result.usage.InputTokens) } require.Equal(t, http.StatusOK, rec.Code) - require.Contains(t, rec.Header().Get("Content-Type"), "application/json") body := rec.Body.Bytes() + if mode.streaming { + require.Contains(t, rec.Header().Get("Content-Type"), "text/event-stream") + _, terminal, ok := extractOpenAISSETerminalEvent(string(body)) + require.True(t, ok) + body = []byte(gjson.GetBytes(terminal, "response").Raw) + } else { + require.Contains(t, rec.Header().Get("Content-Type"), "application/json") + } require.Len(t, gjson.GetBytes(body, "output").Array(), 1) call := gjson.GetBytes(body, "output.0") require.Equal(t, "function_call", call.Get("type").String()) @@ -98,6 +118,11 @@ func TestOpenAIResponsesSealedFunctionCallRoundTrip(t *testing.T) { } else { require.Equal(t, marker.raw, call.Get("encrypted_function_args").Raw) } + if marker.encryptedContent == "" { + require.False(t, call.Get("encrypted_content").Exists()) + } else { + require.Equal(t, marker.encryptedContent, call.Get("encrypted_content").String()) + } require.Equal(t, "collaboration", call.Get("namespace").String()) }) }