Merge pull request #9152 from pangxin12345/fix/codex-browser-tabs-context

fix(openai): normalize standalone browser tabs context
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-12-02 09:25:01 +08:00
commit 7d32ba27f7
2 files changed
+53 -3

No files matched your search

@@ -16,7 +16,7 @@ var codexSelectedPageName = regexp.MustCompile(`^codex_apps_selected_page_part_(
func normalizeCodexUntrustedContext(body []byte) ([]byte, bool) {
if !mayContainCodexCallOutputCandidate(body,
"codex_apps_open_page", "codex_apps_selected_page_part_",
"browser_info", "terminal_info", "goal_objective", "untrusted_context", "untrusted_content", "ide_context", "proposed_plan",
"browser_info", "browser_tabs", "terminal_info", "goal_objective", "untrusted_context", "untrusted_content", "ide_context", "proposed_plan",
) || !hasUniqueJSONMembers(body) {
return body, false
}
@@ -89,7 +89,7 @@ func normalizeCodexUntrustedContext(body []byte) ([]byte, bool) {
func validCodexUntrustedContext(name, output string) bool {
switch name {
case "browser_info", "terminal_info", "goal_objective", "untrusted_context", "ide_context":
case "browser_info", "browser_tabs", "terminal_info", "goal_objective", "untrusted_context", "ide_context":
return strings.TrimSpace(output) != ""
case "untrusted_content":
// Codex's fallback for an unnamed context source can carry any text, including an empty value.
@@ -54,7 +54,7 @@ func TestNormalizeCodexPageContextFirstMessage(t *testing.T) {
require.Equal(t, got, again)
}
var codexTextContextNamesForTest = []string{"browser_info", "terminal_info", "goal_objective", "untrusted_context", "untrusted_content", "ide_context", "proposed_plan"}
var codexTextContextNamesForTest = []string{"browser_info", "browser_tabs", "terminal_info", "goal_objective", "untrusted_context", "untrusted_content", "ide_context", "proposed_plan"}
func TestNormalizeCodexTerminalInfoIssue8991(t *testing.T) {
body := []byte(`{"model":"deepseek-flash","input":[{"type":"function_call_output","namespace":"agent","name":"terminal_info","output":"retest context"}]}`)
@@ -373,3 +373,53 @@ func TestNormalizeCodexPageContextRejectsUnrelatedOrMalformedItems(t *testing.T)
require.Equal(t, body, string(got))
}
}
func TestNormalizeCodexBrowserTabs(t *testing.T) {
// Shape sent by the Codex desktop in-app browser: an agent-namespace output
// with an item id but no call_id, followed by the user's question.
tabs := "<in-app-browser-context source=\"ambient-ui-state\">\nTab 1 (active): https://example.com/\nTab 2: https://example.org/\n</in-app-browser-context>"
body := pageContextRequest(t,
map[string]any{"type": "function_call_output", "id": "fco_123", "namespace": "agent", "name": "browser_tabs", "output": tabs},
map[string]any{"type": "message", "role": "user", "content": []any{map[string]any{"type": "input_text", "text": "How many tabs are open?"}}})
h := &OpenAIGatewayHandler{}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
require.False(t, h.validateFunctionCallOutputRequest(c, body, zap.NewNop()))
require.Equal(t, 400, w.Code)
got, changed := normalizeCodexUntrustedContext(body)
require.True(t, changed)
require.Equal(t, "message", gjson.GetBytes(got, "input.0.type").String())
require.Equal(t, "user", gjson.GetBytes(got, "input.0.role").String())
require.Equal(t, "<external_browser_tabs>"+tabs+"</external_browser_tabs>", gjson.GetBytes(got, "input.0.content.0.text").String())
require.JSONEq(t, gjson.GetBytes(body, "input.1").Raw, gjson.GetBytes(got, "input.1").Raw)
w = httptest.NewRecorder()
c, _ = gin.CreateTestContext(w)
require.True(t, h.validateFunctionCallOutputRequest(c, got, zap.NewNop()))
require.False(t, c.Writer.Written())
}
func TestNormalizeCodexBrowserTabsBoundaries(t *testing.T) {
for _, testCase := range []struct {
name string
key string
value any
}{
{"real call id", "call_id", "call_real"},
{"other namespace", "namespace", "browser"},
{"name prefix", "name", "browser_tabs_extra"},
{"name case", "name", "Browser_tabs"},
{"empty output", "output", ""},
{"blank output", "output", " \n\t"},
{"object output", "output", map[string]any{"tabs": 2}},
} {
t.Run(testCase.name, func(t *testing.T) {
item := pageContextItem("browser_tabs", "Tab 1: https://example.com/")
item[testCase.key] = testCase.value
body := pageContextRequest(t, item)
got, changed := normalizeCodexUntrustedContext(body)
require.False(t, changed)
require.Equal(t, body, got)
})
}
}