Skip to content

fix: support codex alpha search relay#6263

Closed
SaikaOfficial wants to merge 1 commit into
QuantumNous:mainfrom
SaikaOfficial:fix/codex-alpha-search
Closed

fix: support codex alpha search relay#6263
SaikaOfficial wants to merge 1 commit into
QuantumNous:mainfrom
SaikaOfficial:fix/codex-alpha-search

Conversation

@SaikaOfficial

@SaikaOfficial SaikaOfficial commented Jul 17, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)
修复 Codex CLI 在 GPT-5.6 / Responses Lite 场景启用 web search 时,请求在 new-api 中无法进入 relay 链路的问题。
根据 #6114 的复现,Codex CLI 会向 provider base_url 发起独立的:POST /v1/alpha/search
这个请求不是普通 /v1/responses 请求体中的 hosted web_search tool,而是 Codex CLI 在 GPT-5.6 相关模式下使用的 standalone alpha search 端点。new-api 原本没有注册和处理该路径,请求会落到 NoRoute,最终返回 404 Invalid URL (POST /v1/alpha/search)。
主要变更:
注册 /v1/alpha/search、/alpha/search、/backend-api/codex/alpha/search。
新增 Codex alpha search 请求 DTO,用请求中的 model 参与模型映射、渠道选择和计费。
OpenAI 兼容渠道转发到 /v1/alpha/search。
Codex 渠道转发到 /backend-api/codex/alpha/search。
避免 /alpha/search 被前端 SPA fallback 捕获。
为 alpha search 成功调用补充独立的工具调用计费记录。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)
image
image

[INFO] 2026/07/17 - 17:01:39 | ... | record consume log: userId=1, params={... "model_name":"gpt-5.6-sol", ... "codex_alpha_search":true, "request_path":"/v1/alpha/search", ... "web_search_call_count":1 ...}
[GIN] 2026/07/17 - 17:01:39 | relay | | 200 | 1.892889174s | 172.18.0.1 | POST /v1/alpha/search

Summary by CodeRabbit

  • New Features
    • Added Codex Alpha Search support with new routing for /v1/alpha/search, /alpha/search, and backend-compatible endpoints.
    • Introduced Codex Alpha Search request type, validation (including required model), and token-count metadata handling.
    • Extended relay mode/format detection and response dispatch so Alpha Search requests flow end-to-end.
    • Updated request header behavior and Accept handling for Alpha Search.
    • Added Alpha Search quota consumption and billing, including extra tool billing safeguards.

@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds Codex Alpha Search request contracts, routes, relay-mode classification, upstream adaptors, response forwarding, and quota billing. Requests are validated, normalized, sent to supported upstream endpoints, and billed as one tool call.

Changes

Codex Alpha Search relay

Layer / File(s) Summary
Request contracts and format classification
types/relay_format.go, dto/codex_alpha_search.go, relay/constant/relay_mode.go, relay/common/..., relay/helper/valid_request.go
Adds the Alpha Search request DTO, relay format and mode identifiers, path detection, validation, and relay metadata generation.
HTTP route and relay dispatch
router/relay-router.go, router/web-router.go, controller/relay.go, middleware/distributor.go
Registers Alpha Search endpoints and dispatches matching requests through the relay pipeline.
Upstream request and response pipeline
relay/alpha_search_handler.go, relay/channel/codex/adaptor.go, relay/channel/openai/...
Normalizes request bodies, configures Alpha Search URLs and headers, forwards upstream responses, and handles errors.
Alpha Search quota billing
service/tool_billing.go, setting/operation_setting/tools.go
Adds one-call quota consumption, billing metadata, and the default Alpha Search tool price.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Router
  participant Relay
  participant Upstream
  participant Billing
  Client->>Router: POST /alpha/search
  Router->>Relay: Validate and dispatch request
  Relay->>Upstream: Send normalized Alpha Search request
  Upstream-->>Relay: Return JSON response
  Relay->>Billing: Consume one Alpha Search quota call
  Relay-->>Client: Forward response
Loading

Poem

I’m a rabbit routing search through the night,
With JSON tucked neatly and headers just right.
Upstream replies hop back bright and clear,
While quota logs carrot-hop near.
Codex Alpha Search bounds with delight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding Codex Alpha Search relay support.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@SaikaOfficial
SaikaOfficial force-pushed the fix/codex-alpha-search branch from df4f432 to 93dacf1 Compare July 17, 2026 09:35

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
relay/channel/codex/adaptor.go (1)

187-233: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Inline the single-use header manipulation helper.

As per coding guidelines: "Avoid package-level or module-level helpers with only one caller unless they express a stable business concept; inline single-use mechanical logic instead."

The setupCodexAlphaSearchHeaders function is only called here and performs strictly mechanical header manipulation. Please inline it directly into SetupRequestHeader to eliminate the unnecessary indirection.

♻️ Proposed refactor to inline the mechanical logic
-	if info.RelayMode == relayconstant.RelayModeCodexAlphaSearch {
-		setupCodexAlphaSearchHeaders(c, req)
-	} else if req.Get("OpenAI-Beta") == "" {
+	if info.RelayMode == relayconstant.RelayModeCodexAlphaSearch {
+		req.Del("OpenAI-Beta")
+		req.Del("Session_ID")
+		req.Del("Conversation_ID")
+		req.Del("X-Codex-Beta-Features")
+		req.Del("X-Codex-Turn-State")
+
+		if c != nil && c.Request != nil {
+			for _, name := range []string{
+				"Version",
+				"User-Agent",
+				"Session_id",
+				"X-Client-Request-Id",
+				"X-Codex-Turn-Metadata",
+			} {
+				if value := strings.TrimSpace(c.GetHeader(name)); value != "" {
+					req.Set(name, value)
+				}
+			}
+		}
+	} else if req.Get("OpenAI-Beta") == "" {
 		req.Set("OpenAI-Beta", "responses=experimental")
 	}
 	if req.Get("originator") == "" {
 		req.Set("originator", "codex_cli_rs")
 	}
 
 	// chatgpt.com/backend-api/codex/responses is strict about Content-Type.
 	// Clients may omit it or include parameters like `application/json; charset=utf-8`,
 	// which can be rejected by the upstream. Force the exact media type.
 	req.Set("Content-Type", "application/json")
 	if info.RelayMode == relayconstant.RelayModeCodexAlphaSearch {
 		req.Set("Accept", "application/json")
 	} else if info.IsStream {
 		req.Set("Accept", "text/event-stream")
 	} else if req.Get("Accept") == "" {
 		req.Set("Accept", "application/json")
 	}
 
 	return nil
 }
-
-func setupCodexAlphaSearchHeaders(c *gin.Context, req *http.Header) {
-	req.Del("OpenAI-Beta")
-	req.Del("Session_ID")
-	req.Del("Conversation_ID")
-	req.Del("X-Codex-Beta-Features")
-	req.Del("X-Codex-Turn-State")
-
-	if c == nil || c.Request == nil {
-		return
-	}
-
-	for _, name := range []string{
-		"Version",
-		"User-Agent",
-		"Session_id",
-		"X-Client-Request-Id",
-		"X-Codex-Turn-Metadata",
-	} {
-		if value := strings.TrimSpace(c.GetHeader(name)); value != "" {
-			req.Set(name, value)
-		}
-	}
-}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@relay/channel/codex/adaptor.go` around lines 187 - 233, Inline the logic from
setupCodexAlphaSearchHeaders directly into its sole caller, SetupRequestHeader,
within the RelayModeCodexAlphaSearch handling. Preserve the existing header
deletions, nil-request guards, and forwarded header values, then remove the
now-unused helper function.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@relay/alpha_search_handler.go`:
- Around line 32-33: Update the JSON parsing in the handler’s body-normalization
flow and the additional marshal/unmarshal call sites to use the repository
wrappers from common/json.go instead of the standard json package directly.
Preserve the existing validation and normalized output behavior while routing
all business-code serialization through those wrappers.

In `@service/tool_billing.go`:
- Around line 101-105: Update the billing flow around ComputeToolCallQuota to
use checked quota conversion and checked integer addition, rejecting
non-positive, NaN, or infinite group ratios and preventing negative or
overflow-derived charges. Propagate any conversion clamp into
relayInfo.QuotaClamp before calling attachQuotaSaturation, while preserving the
existing counter update flow only for valid checked results.

---

Nitpick comments:
In `@relay/channel/codex/adaptor.go`:
- Around line 187-233: Inline the logic from setupCodexAlphaSearchHeaders
directly into its sole caller, SetupRequestHeader, within the
RelayModeCodexAlphaSearch handling. Preserve the existing header deletions,
nil-request guards, and forwarded header values, then remove the now-unused
helper function.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 46fb0589-4020-4456-9123-d2b71a2668b0

📥 Commits

Reviewing files that changed from the base of the PR and between a63364d and df4f432.

📒 Files selected for processing (16)
  • controller/relay.go
  • dto/codex_alpha_search.go
  • middleware/distributor.go
  • relay/alpha_search_handler.go
  • relay/channel/codex/adaptor.go
  • relay/channel/openai/adaptor.go
  • relay/channel/openai/relay_alpha_search.go
  • relay/common/relay_info.go
  • relay/common/request_conversion.go
  • relay/constant/relay_mode.go
  • relay/helper/valid_request.go
  • router/relay-router.go
  • router/web-router.go
  • service/tool_billing.go
  • setting/operation_setting/tools.go
  • types/relay_format.go

Comment thread relay/alpha_search_handler.go Outdated
Comment thread service/tool_billing.go
@SaikaOfficial
SaikaOfficial force-pushed the fix/codex-alpha-search branch from 93dacf1 to 2690764 Compare July 17, 2026 09:39
@SaikaOfficial
SaikaOfficial force-pushed the fix/codex-alpha-search branch from 2690764 to b41e1c4 Compare July 17, 2026 10:02
@Calcium-Ion

Copy link
Copy Markdown
Member

根本无法确认上游兼容openai端点是否支持搜索,你这样打到非codex直接报错,这样的适配不可接受

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex CLI 内置联网搜索 POST /v1/alpha/search 返回 404 Invalid URL

2 participants