mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
Merge pull request #8735 from Hato626/perf/oauth-input-metadata-cleanup
perf(openai): avoid repeated full-body copies during OAuth metadata cleanup
This commit is contained in:
2 files changed
+132
-15
No files matched your search
@@ -1452,9 +1452,9 @@ func normalizeOpenAIOAuthResponsesCompatibilityBody(body []byte) ([]byte, bool,
|
||||
}
|
||||
normalized := body
|
||||
changed := false
|
||||
prompt := gjson.GetBytes(normalized, "prompt")
|
||||
prompt := parseRawJSONView(normalized).Get("prompt")
|
||||
if prompt.Exists() {
|
||||
input := gjson.GetBytes(normalized, "input")
|
||||
input := parseRawJSONView(normalized).Get("input")
|
||||
if prompt.Type != gjson.Null && (!input.Exists() || input.Type == gjson.Null) {
|
||||
next, err := sjson.SetRawBytes(normalized, "input", []byte(prompt.Raw))
|
||||
if err != nil {
|
||||
@@ -1469,7 +1469,7 @@ func normalizeOpenAIOAuthResponsesCompatibilityBody(body []byte) ([]byte, bool,
|
||||
normalized = next
|
||||
changed = true
|
||||
}
|
||||
if gjson.GetBytes(normalized, "commands").Exists() {
|
||||
if parseRawJSONView(normalized).Get("commands").Exists() {
|
||||
next, err := sjson.DeleteBytes(normalized, "commands")
|
||||
if err != nil {
|
||||
return body, false, fmt.Errorf("normalize oauth responses delete commands: %w", err)
|
||||
@@ -1478,22 +1478,39 @@ func normalizeOpenAIOAuthResponsesCompatibilityBody(body []byte) ([]byte, bool,
|
||||
changed = true
|
||||
}
|
||||
// Only remove the input-item field, never same-named user content.
|
||||
input := gjson.GetBytes(normalized, "input")
|
||||
input := parseRawJSONView(normalized).Get("input")
|
||||
if !input.IsArray() {
|
||||
return normalized, changed, nil
|
||||
}
|
||||
for i, item := range input.Array() {
|
||||
if !item.IsObject() || !item.Get("internal_chat_message_metadata_passthrough").Exists() {
|
||||
continue
|
||||
}
|
||||
next, err := sjson.DeleteBytes(normalized, fmt.Sprintf("input.%d.internal_chat_message_metadata_passthrough", i))
|
||||
if err != nil {
|
||||
return body, false, fmt.Errorf("normalize oauth input metadata: %w", err)
|
||||
}
|
||||
normalized = next
|
||||
changed = true
|
||||
const field = "internal_chat_message_metadata_passthrough"
|
||||
hasMetadata := false
|
||||
input.ForEach(func(_, item gjson.Result) bool {
|
||||
hasMetadata = item.IsObject() && item.Get(field).Exists()
|
||||
return !hasMetadata
|
||||
})
|
||||
if !hasMetadata {
|
||||
return normalized, changed, nil
|
||||
}
|
||||
return normalized, changed, nil
|
||||
|
||||
// Clean each item locally, then rebuild the request once. Deleting by
|
||||
// input.N on the full body copies the entire history for every message.
|
||||
var items []string
|
||||
var normalizeErr error
|
||||
input.ForEach(func(_, item gjson.Result) bool {
|
||||
raw := item.Raw
|
||||
if item.IsObject() && item.Get(field).Exists() {
|
||||
raw, normalizeErr = sjson.Delete(raw, field)
|
||||
if normalizeErr != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
items = append(items, raw)
|
||||
return true
|
||||
})
|
||||
if normalizeErr != nil {
|
||||
return body, false, fmt.Errorf("normalize oauth input metadata: %w", normalizeErr)
|
||||
}
|
||||
return replaceOpenAIRawInput(normalized, input, items), true, nil
|
||||
}
|
||||
|
||||
func normalizeGPT6ResponsesSampling(body []byte, model string) ([]byte, bool, error) {
|
||||
|
||||
@@ -1,13 +1,113 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func oauthInputMetadataHistory(items, textBytes, metadataEvery int) []byte {
|
||||
var body strings.Builder
|
||||
_, _ = body.WriteString(`{"model":"gpt-5.5","opaque":9007199254740993,"input":[`)
|
||||
text := strings.Repeat("x", textBytes)
|
||||
for i := 0; i < items; i++ {
|
||||
if i > 0 {
|
||||
_ = body.WriteByte(',')
|
||||
}
|
||||
_, _ = body.WriteString(`{"role":"user","content":"`)
|
||||
_, _ = body.WriteString(text)
|
||||
_ = body.WriteByte('"')
|
||||
if metadataEvery > 0 && i%metadataEvery == 0 {
|
||||
_, _ = body.WriteString(`,"internal_chat_message_metadata_passthrough":{"content_item_kinds":["text"]}`)
|
||||
}
|
||||
_ = body.WriteByte('}')
|
||||
}
|
||||
_, _ = body.WriteString(`],"tail":{"keep":true}}`)
|
||||
return []byte(body.String())
|
||||
}
|
||||
|
||||
func TestOAuthInputInternalMetadataLargeHistory(t *testing.T) {
|
||||
for _, fixture := range []struct {
|
||||
name string
|
||||
metadataEvery int
|
||||
}{
|
||||
{name: "all", metadataEvery: 1},
|
||||
{name: "sparse", metadataEvery: 32},
|
||||
} {
|
||||
t.Run(fixture.name, func(t *testing.T) {
|
||||
body := oauthInputMetadataHistory(128, 16<<10, fixture.metadataEvery)
|
||||
original := bytes.Clone(body)
|
||||
out, changed, err := normalizeOpenAIOAuthResponsesCompatibilityBody(body)
|
||||
require.NoError(t, err)
|
||||
require.True(t, changed)
|
||||
require.Equal(t, original, body, "normalization must not mutate caller-owned history")
|
||||
require.True(t, gjson.ValidBytes(out))
|
||||
require.Equal(t, int64(128), gjson.GetBytes(out, "input.#").Int())
|
||||
require.NotContains(t, string(out), "internal_chat_message_metadata_passthrough")
|
||||
require.Equal(t, "9007199254740993", gjson.GetBytes(out, "opaque").Raw)
|
||||
require.True(t, gjson.GetBytes(out, "tail.keep").Bool())
|
||||
text := strings.Repeat("x", 16<<10)
|
||||
for _, item := range gjson.GetBytes(out, "input").Array() {
|
||||
require.Equal(t, "user", item.Get("role").String())
|
||||
require.Equal(t, text, item.Get("content").String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOAuthInputInternalMetadataPreservesRawContent(t *testing.T) {
|
||||
const body = ` {"opaque":9007199254740993,"inpu\u0074":[null,42,"keep",{"type":"function_call_output","call_id":"call_keep","output":[{"type":"input_image","image_url":"https://example.com/image.png","internal_chat_message_metadata_passthrough":{"keep":true}}],"internal_chat_message_metadata_passthrou\u0067h":null},{"role":"user","content":"internal_chat_message_metadata_passthrough"}],"tail" : "unchanged" } `
|
||||
out, changed, err := normalizeOpenAIOAuthResponsesCompatibilityBody([]byte(body))
|
||||
require.NoError(t, err)
|
||||
require.True(t, changed)
|
||||
require.True(t, bytes.HasPrefix(out, []byte(` {"opaque":9007199254740993,"inpu\u0074":`)))
|
||||
require.True(t, bytes.HasSuffix(out, []byte(`,"tail" : "unchanged" } `)))
|
||||
require.Equal(t, `[null,42,"keep",{"type":"function_call_output","call_id":"call_keep","output":[{"type":"input_image","image_url":"https://example.com/image.png","internal_chat_message_metadata_passthrough":{"keep":true}}]},{"role":"user","content":"internal_chat_message_metadata_passthrough"}]`, gjson.GetBytes(out, "input").Raw)
|
||||
}
|
||||
|
||||
func TestOAuthInputInternalMetadataUnchangedHistory(t *testing.T) {
|
||||
body := oauthInputMetadataHistory(128, 16<<10, 0)
|
||||
original := bytes.Clone(body)
|
||||
out, changed, err := normalizeOpenAIOAuthResponsesCompatibilityBody(body)
|
||||
require.NoError(t, err)
|
||||
require.False(t, changed)
|
||||
require.Equal(t, original, out)
|
||||
require.Same(t, &body[0], &out[0], "unchanged history should retain the caller's buffer")
|
||||
}
|
||||
|
||||
func BenchmarkOAuthInputInternalMetadataHistory(b *testing.B) {
|
||||
for _, fixture := range []struct {
|
||||
name string
|
||||
items int
|
||||
metadataEvery int
|
||||
}{
|
||||
{name: "1_item", items: 1, metadataEvery: 1},
|
||||
{name: "32_items", items: 32, metadataEvery: 1},
|
||||
{name: "128_items", items: 128, metadataEvery: 1},
|
||||
{name: "128_items_sparse", items: 128, metadataEvery: 32},
|
||||
{name: "128_items_unchanged", items: 128},
|
||||
} {
|
||||
b.Run(fixture.name, func(b *testing.B) {
|
||||
body := oauthInputMetadataHistory(fixture.items, 16<<10, fixture.metadataEvery)
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(int64(len(body)))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
out, changed, err := normalizeOpenAIOAuthResponsesCompatibilityBody(body)
|
||||
if err != nil || changed != (fixture.metadataEvery > 0) {
|
||||
b.Fatalf("changed=%v err=%v", changed, err)
|
||||
}
|
||||
runtime.KeepAlive(out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOAuthInputInternalMetadata(t *testing.T) {
|
||||
const field = "internal_chat_message_metadata_passthrough"
|
||||
// The identically named field in user content must not be recursively stripped.
|
||||
|
||||
Reference in new issue
Block a user