Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
f16910d6
Commit
f16910d6
authored
Mar 12, 2026
by
yexueduxing
Browse files
chore: codex transform fixes and feature compatibility
parent
826090e0
Changes
2
Show whitespace changes
Inline
Side-by-side
backend/internal/service/openai_codex_transform.go
View file @
f16910d6
...
...
@@ -129,6 +129,41 @@ func applyCodexOAuthTransform(reqBody map[string]any, isCodexCLI bool, isCompact
}
}
// 兼容遗留的 functions 和 function_call,转换为 tools 和 tool_choice
if
functionsRaw
,
ok
:=
reqBody
[
"functions"
];
ok
{
if
functions
,
k
:=
functionsRaw
.
([]
any
);
k
{
tools
:=
make
([]
any
,
0
,
len
(
functions
))
for
_
,
f
:=
range
functions
{
tools
=
append
(
tools
,
map
[
string
]
any
{
"type"
:
"function"
,
"function"
:
f
,
})
}
reqBody
[
"tools"
]
=
tools
}
delete
(
reqBody
,
"functions"
)
result
.
Modified
=
true
}
if
fcRaw
,
ok
:=
reqBody
[
"function_call"
];
ok
{
if
fcStr
,
ok
:=
fcRaw
.
(
string
);
ok
{
// e.g. "auto", "none"
reqBody
[
"tool_choice"
]
=
fcStr
}
else
if
fcObj
,
ok
:=
fcRaw
.
(
map
[
string
]
any
);
ok
{
// e.g. {"name": "my_func"}
if
name
,
ok
:=
fcObj
[
"name"
]
.
(
string
);
ok
&&
strings
.
TrimSpace
(
name
)
!=
""
{
reqBody
[
"tool_choice"
]
=
map
[
string
]
any
{
"type"
:
"function"
,
"function"
:
map
[
string
]
any
{
"name"
:
name
,
},
}
}
}
delete
(
reqBody
,
"function_call"
)
result
.
Modified
=
true
}
if
normalizeCodexTools
(
reqBody
)
{
result
.
Modified
=
true
}
...
...
@@ -303,6 +338,18 @@ func filterCodexInput(input []any, preserveReferences bool) []any {
continue
}
typ
,
_
:=
m
[
"type"
]
.
(
string
)
// 修复 OpenAI 上游的最新校验:"Expected an ID that begins with 'fc'"
fixIDPrefix
:=
func
(
id
string
)
string
{
if
id
==
""
||
strings
.
HasPrefix
(
id
,
"fc"
)
{
return
id
}
if
strings
.
HasPrefix
(
id
,
"call_"
)
{
return
"fc"
+
strings
.
TrimPrefix
(
id
,
"call_"
)
}
return
"fc_"
+
id
}
if
typ
==
"item_reference"
{
if
!
preserveReferences
{
continue
...
...
@@ -311,6 +358,9 @@ func filterCodexInput(input []any, preserveReferences bool) []any {
for
key
,
value
:=
range
m
{
newItem
[
key
]
=
value
}
if
id
,
ok
:=
newItem
[
"id"
]
.
(
string
);
ok
&&
id
!=
""
{
newItem
[
"id"
]
=
fixIDPrefix
(
id
)
}
filtered
=
append
(
filtered
,
newItem
)
continue
}
...
...
@@ -330,10 +380,20 @@ func filterCodexInput(input []any, preserveReferences bool) []any {
}
if
isCodexToolCallItemType
(
typ
)
{
if
callID
,
ok
:=
m
[
"call_id"
]
.
(
string
);
!
ok
||
strings
.
TrimSpace
(
callID
)
==
""
{
callID
,
ok
:=
m
[
"call_id"
]
.
(
string
)
if
!
ok
||
strings
.
TrimSpace
(
callID
)
==
""
{
if
id
,
ok
:=
m
[
"id"
]
.
(
string
);
ok
&&
strings
.
TrimSpace
(
id
)
!=
""
{
callID
=
id
ensureCopy
()
newItem
[
"call_id"
]
=
callID
}
}
if
callID
!=
""
{
fixedCallID
:=
fixIDPrefix
(
callID
)
if
fixedCallID
!=
callID
{
ensureCopy
()
newItem
[
"call_id"
]
=
id
newItem
[
"call_id"
]
=
fixedCallID
}
}
}
...
...
@@ -344,6 +404,14 @@ func filterCodexInput(input []any, preserveReferences bool) []any {
if
!
isCodexToolCallItemType
(
typ
)
{
delete
(
newItem
,
"call_id"
)
}
}
else
{
if
id
,
ok
:=
newItem
[
"id"
]
.
(
string
);
ok
&&
id
!=
""
{
fixedID
:=
fixIDPrefix
(
id
)
if
fixedID
!=
id
{
ensureCopy
()
newItem
[
"id"
]
=
fixedID
}
}
}
filtered
=
append
(
filtered
,
newItem
)
...
...
backend/internal/service/openai_codex_transform_test.go
View file @
f16910d6
...
...
@@ -33,12 +33,12 @@ func TestApplyCodexOAuthTransform_ToolContinuationPreservesInput(t *testing.T) {
first
,
ok
:=
input
[
0
]
.
(
map
[
string
]
any
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
"item_reference"
,
first
[
"type"
])
require
.
Equal
(
t
,
"ref1"
,
first
[
"id"
])
require
.
Equal
(
t
,
"
fc_
ref1"
,
first
[
"id"
])
// 校验 input[1] 为 map,确保后续字段断言安全。
second
,
ok
:=
input
[
1
]
.
(
map
[
string
]
any
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
"o1"
,
second
[
"id"
])
require
.
Equal
(
t
,
"
fc_
o1"
,
second
[
"id"
])
}
func
TestApplyCodexOAuthTransform_ExplicitStoreFalsePreserved
(
t
*
testing
.
T
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment