Files
sub2api/backend/internal/service/openai_responses_item_id.go
T
李建琦 6d655c9903
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
2026-08-21 18:30:13 +08:00

82 lines
2.0 KiB
Go

package service
import (
"fmt"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// Invalid replayed IDs are removed rather than rewritten because a fabricated
// msg/fc ID may point at a different upstream object.
func shouldStripOpenAIResponsesInputItemID(itemType, id string) bool {
if id == "" {
return false
}
if itemType == "message" {
return !strings.HasPrefix(id, "msg")
}
if itemType == "reasoning" {
return !strings.HasPrefix(id, "rs")
}
if isCodexToolCallInputType(itemType) {
return !strings.HasPrefix(id, "fc")
}
return false
}
func sanitizeOpenAIResponsesInputItemIDs(body []byte) ([]byte, bool, error) {
input := gjson.GetBytes(body, "input")
if !input.IsArray() {
return body, false, nil
}
items := make([][]byte, 0)
changed := false
var sanitizeErr error
index := 0
input.ForEach(func(_, item gjson.Result) bool {
currentIndex := index
index++
itemBody := []byte(item.Raw)
if item.IsObject() {
itemType := item.Get("type")
id := item.Get("id")
if itemType.Type == gjson.String && id.Type == gjson.String &&
shouldStripOpenAIResponsesInputItemID(itemType.String(), id.String()) {
itemBody, sanitizeErr = sjson.DeleteBytes(itemBody, "id")
if sanitizeErr != nil {
sanitizeErr = fmt.Errorf("delete input.%d.id: %w", currentIndex, sanitizeErr)
return false
}
changed = true
}
}
items = append(items, itemBody)
return true
})
if sanitizeErr != nil {
return nil, false, sanitizeErr
}
if !changed {
return body, false, nil
}
rebuiltInput := make([]byte, 0, len(input.Raw))
rebuiltInput = append(rebuiltInput, '[')
for i, item := range items {
if i > 0 {
rebuiltInput = append(rebuiltInput, ',')
}
rebuiltInput = append(rebuiltInput, item...)
}
rebuiltInput = append(rebuiltInput, ']')
sanitized, err := sjson.SetRawBytes(body, "input", rebuiltInput)
if err != nil {
return nil, false, fmt.Errorf("replace sanitized input: %w", err)
}
return sanitized, true, nil
}