mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
fix(openai): keep HTTP handler missing-call-id validation and scope handshake window_id
This commit is contained in:
1 parent
905893ca9e
commit
57a990fafa
3 files changed
+43
-17
No files matched your search
@@ -187,11 +187,8 @@ func ValidateFunctionCallOutputContextBytes(body []byte) FunctionCallOutputValid
|
||||
itemType := item.Get("type").String()
|
||||
switch {
|
||||
case isCodexToolCallOutputItemType(itemType):
|
||||
callID := strings.TrimSpace(item.Get("call_id").String())
|
||||
if isStandaloneNamedFunctionCallOutput(itemType, callID, item.Get("name").String()) {
|
||||
return true
|
||||
}
|
||||
result.HasFunctionCallOutput = true
|
||||
callID := strings.TrimSpace(item.Get("call_id").String())
|
||||
if callID == "" {
|
||||
result.HasFunctionCallOutputMissingCallID = true
|
||||
return true
|
||||
@@ -341,11 +338,6 @@ func ValidateFunctionCallOutputContext(reqBody map[string]any) FunctionCallOutpu
|
||||
itemType, _ := itemMap["type"].(string)
|
||||
switch {
|
||||
case isCodexToolCallOutputItemType(itemType):
|
||||
callID, _ := itemMap["call_id"].(string)
|
||||
name, _ := itemMap["name"].(string)
|
||||
if isStandaloneNamedFunctionCallOutput(itemType, callID, name) {
|
||||
continue
|
||||
}
|
||||
result.HasFunctionCallOutput = true
|
||||
case isCodexToolCallContextItemType(itemType):
|
||||
callID, _ := itemMap["call_id"].(string)
|
||||
@@ -374,10 +366,6 @@ func ValidateFunctionCallOutputContext(reqBody map[string]any) FunctionCallOutpu
|
||||
case isCodexToolCallOutputItemType(itemType):
|
||||
callID, _ := itemMap["call_id"].(string)
|
||||
callID = strings.TrimSpace(callID)
|
||||
name, _ := itemMap["name"].(string)
|
||||
if isStandaloneNamedFunctionCallOutput(itemType, callID, name) {
|
||||
continue
|
||||
}
|
||||
if callID == "" {
|
||||
result.HasFunctionCallOutputMissingCallID = true
|
||||
continue
|
||||
|
||||
@@ -203,10 +203,10 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient(
|
||||
}
|
||||
ingressSessionOriginalModel := ""
|
||||
activeTurnMetadata := ""
|
||||
initialHandshakeWindowID := ""
|
||||
activeWindowID := ""
|
||||
if c != nil {
|
||||
activeTurnMetadata = strings.TrimSpace(c.GetHeader(openAIWSTurnMetadataHeader))
|
||||
initialHandshakeWindowID = strings.TrimSpace(c.GetHeader("x-codex-window-id"))
|
||||
activeWindowID = strings.TrimSpace(c.GetHeader("x-codex-window-id"))
|
||||
}
|
||||
|
||||
applyPayloadMutation := func(current []byte, path string, value any) ([]byte, error) {
|
||||
@@ -225,6 +225,8 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient(
|
||||
payload[path] = value
|
||||
case "client_metadata." + openAIWSTurnMetadataHeader:
|
||||
setOpenAIWSTurnMetadata(payload, fmt.Sprintf("%v", value))
|
||||
case "client_metadata.x-codex-window-id":
|
||||
setOpenAIWSWindowIDMetadata(payload, fmt.Sprintf("%v", value))
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
@@ -328,7 +330,11 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient(
|
||||
frameWindowID := strings.TrimSpace(gjson.GetBytes(normalized, "client_metadata.x-codex-window-id").String())
|
||||
if frameTurnMetadata != "" {
|
||||
activeTurnMetadata = frameTurnMetadata
|
||||
if w := strings.TrimSpace(gjson.Get(frameTurnMetadata, "window_id").String()); w != "" {
|
||||
activeWindowID = w
|
||||
}
|
||||
} else if frameWindowID != "" {
|
||||
activeWindowID = frameWindowID
|
||||
if activeTurnMetadata != "" && strings.TrimSpace(gjson.Get(activeTurnMetadata, "window_id").String()) != frameWindowID {
|
||||
activeTurnMetadata = ""
|
||||
}
|
||||
@@ -338,6 +344,12 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient(
|
||||
return openAIWSClientPayload{}, NewOpenAIWSClientCloseError(coderws.StatusPolicyViolation, "invalid websocket request payload", setErr)
|
||||
}
|
||||
normalized = next
|
||||
} else if activeWindowID != "" {
|
||||
next, setErr := applyPayloadMutation(normalized, "client_metadata.x-codex-window-id", activeWindowID)
|
||||
if setErr != nil {
|
||||
return openAIWSClientPayload{}, NewOpenAIWSClientCloseError(coderws.StatusPolicyViolation, "invalid websocket request payload", setErr)
|
||||
}
|
||||
normalized = next
|
||||
}
|
||||
accountIdentitySourceRaw := append([]byte(nil), normalized...)
|
||||
accountScopedPayload, accountScoped, scopeErr := applyCodexAccountIdentityClientMetadataRaw(normalized, codexAccountIdentitySource(c, account), getAPIKeyIDFromContext(c), codexClientSessionID(c))
|
||||
@@ -608,7 +620,7 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient(
|
||||
}
|
||||
defer history.close()
|
||||
historyLost := false
|
||||
lastBridgeWindowID := initialHandshakeWindowID
|
||||
lastBridgeWindowID := ""
|
||||
var bridgeReplayInput []json.RawMessage
|
||||
bridgeReplayInputExists := false
|
||||
var bridgeAccountFailoverInput []json.RawMessage
|
||||
@@ -1496,7 +1508,7 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient(
|
||||
turnPrevRecoveryTried := false
|
||||
lastTurnFinishedAt := time.Time{}
|
||||
lastTurnResponseID := ""
|
||||
lastTurnWindowID := initialHandshakeWindowID
|
||||
lastTurnWindowID := ""
|
||||
lastTurnPayload := []byte(nil)
|
||||
var lastTurnStrictState *openAIWSIngressPreviousTurnStrictState
|
||||
lastTurnReplayInput := []json.RawMessage(nil)
|
||||
|
||||
@@ -247,6 +247,32 @@ func setOpenAIWSTurnMetadata(payload map[string]any, turnMetadata string) {
|
||||
}
|
||||
}
|
||||
|
||||
func setOpenAIWSWindowIDMetadata(payload map[string]any, windowID string) {
|
||||
if len(payload) == 0 {
|
||||
return
|
||||
}
|
||||
id := strings.TrimSpace(windowID)
|
||||
if id == "" {
|
||||
return
|
||||
}
|
||||
switch existing := payload["client_metadata"].(type) {
|
||||
case map[string]any:
|
||||
existing["x-codex-window-id"] = id
|
||||
payload["client_metadata"] = existing
|
||||
case map[string]string:
|
||||
next := make(map[string]any, len(existing)+1)
|
||||
for k, v := range existing {
|
||||
next[k] = v
|
||||
}
|
||||
next["x-codex-window-id"] = id
|
||||
payload["client_metadata"] = next
|
||||
default:
|
||||
payload["client_metadata"] = map[string]any{
|
||||
"x-codex-window-id": id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *OpenAIGatewayService) isOpenAIWSStoreRecoveryAllowed(account *Account) bool {
|
||||
if account != nil && account.IsOpenAIWSAllowStoreRecoveryEnabled() {
|
||||
return true
|
||||
|
||||
Reference in new issue
Block a user