fix(oauth): extract system-role input items into instructions field
OAuth upstreams (ChatGPT) reject requests containing role:"system" in the input array with HTTP 400 "System messages are not allowed". Extract such items before forwarding and merge their content into the top-level instructions field, prepending to any existing value. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -172,6 +172,11 @@ func applyCodexOAuthTransform(reqBody map[string]any, isCodexCLI bool, isCompact
|
||||
result.PromptCacheKey = strings.TrimSpace(v)
|
||||
}
|
||||
|
||||
// 提取 input 中 role:"system" 消息至 instructions(OAuth 上游不支持 system role)。
|
||||
if extractSystemMessagesFromInput(reqBody) {
|
||||
result.Modified = true
|
||||
}
|
||||
|
||||
// instructions 处理逻辑:根据是否是 Codex CLI 分别调用不同方法
|
||||
if applyInstructions(reqBody, isCodexCLI) {
|
||||
result.Modified = true
|
||||
@@ -301,6 +306,73 @@ func getNormalizedCodexModel(modelID string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// extractTextFromContent extracts plain text from a content value that is either
|
||||
// a Go string or a []any of content-part maps with type:"text".
|
||||
func extractTextFromContent(content any) string {
|
||||
switch v := content.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []any:
|
||||
var parts []string
|
||||
for _, part := range v {
|
||||
m, ok := part.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if t, _ := m["type"].(string); t == "text" {
|
||||
if text, ok := m["text"].(string); ok {
|
||||
parts = append(parts, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "")
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// extractSystemMessagesFromInput scans the input array for items with role=="system",
|
||||
// removes them, and merges their content into reqBody["instructions"].
|
||||
// If instructions is already non-empty, extracted content is prepended with "\n\n".
|
||||
// Returns true if any system messages were extracted.
|
||||
func extractSystemMessagesFromInput(reqBody map[string]any) bool {
|
||||
input, ok := reqBody["input"].([]any)
|
||||
if !ok || len(input) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
var systemTexts []string
|
||||
remaining := make([]any, 0, len(input))
|
||||
|
||||
for _, item := range input {
|
||||
m, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
remaining = append(remaining, item)
|
||||
continue
|
||||
}
|
||||
if role, _ := m["role"].(string); role != "system" {
|
||||
remaining = append(remaining, item)
|
||||
continue
|
||||
}
|
||||
if text := extractTextFromContent(m["content"]); text != "" {
|
||||
systemTexts = append(systemTexts, text)
|
||||
}
|
||||
}
|
||||
|
||||
if len(systemTexts) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
extracted := strings.Join(systemTexts, "\n\n")
|
||||
if existing, ok := reqBody["instructions"].(string); ok && strings.TrimSpace(existing) != "" {
|
||||
reqBody["instructions"] = extracted + "\n\n" + existing
|
||||
} else {
|
||||
reqBody["instructions"] = extracted
|
||||
}
|
||||
reqBody["input"] = remaining
|
||||
return true
|
||||
}
|
||||
|
||||
// applyInstructions 处理 instructions 字段:仅在 instructions 为空时填充默认值。
|
||||
func applyInstructions(reqBody map[string]any, isCodexCLI bool) bool {
|
||||
if !isInstructionsEmpty(reqBody) {
|
||||
|
||||
Reference in New Issue
Block a user