Skip to content
Merged
13 changes: 12 additions & 1 deletion backend/internal/service/account_excel_bps_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@ func (a *Account) excelBPSNativeFallbackReason(body []byte) string {
if a.IsExcelBPSOmitUnsupportedToolsEnabled() {
return ""
}
return basispoints.NativeFallbackReason(body)
return basispoints.NativeFallbackReasonWithSearch(body, a.IsExcelBPSSearchEnabled())
}

const ExcelBPSWebSearchKey = "openai_excel_bps_web_search"
const ExcelBPSWebSearchAutoKey = "openai_excel_bps_web_search_auto"

// Existing accounts opt in explicitly; enabling BPS alone never adds searches.
func (a *Account) IsExcelBPSSearchEnabled() bool {
return a.IsExcelBPSEnabled() && a.Extra[ExcelBPSWebSearchKey] == true
}
func (a *Account) IsExcelBPSSearchAutoEnabled() bool {
return a.IsExcelBPSSearchEnabled() && a.Extra[ExcelBPSWebSearchAutoKey] == true
}
56 changes: 56 additions & 0 deletions backend/internal/service/basispoints/WEB_SEARCH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# BPS account web search

Accounts explicitly enable `extra.openai_excel_bps_web_search=true`. Existing
accounts keep the prior native fallback/hosted-tool omission policy. The separate
`extra.openai_excel_bps_web_search_auto=true` option supplies search when a client
omits it and allows automatic tool selection. It requires web search enabled and
never overrides `none`, `required`, or a named client tool. Controlled experiments
never add search submissions.

The gateway sends authenticated search requests to
`https://chatgpt.com/backend-api/codex/alpha/search`, using the selected BPS grant,
account ID and pinned proxy. It does not forward client cookies or Codex tickets,
follow redirects, provision another account, or require a Claude router plugin.
The endpoint is not a public, stable OpenAI API; entitlement and wire changes can
cause rejection. This implementation has offline coverage, not production search
or billing acceptance.

Explicit `web_search` and preview declarations support automatic/forced text
search. `search_context_size` maps low/medium/high to short/medium/long response
length hints. Mixed text/image search declarations provide text results only;
image-only search, location constraints, unsupported fields and nonempty filters
are rejected. Domain filters are not claimed: the alpha provider has no verified
filter contract, and filtering displayed citations would not constrain retrieval.

`external_web_access=false` never enables live retrieval. Optional cache-only
search is removed with an explicit limitation notice while other tools/chat
continue; forced cache-only search fails before generation. Search history replays
only the supplied query/source metadata and labels missing result text as absent.

A private request-local tool drives the search loop. It is attached after catalog
cache publication, is not inherited by another request, and is never dispatched
to clients. Each entire tool batch and its original schema must validate before
search. Mixed client/search batches and parallel searches are rejected; a repeated
call/item ID fails without repeating the operation. Search results are untrusted
data in a same-account native continuation. Search failures do not replay, switch
accounts, move groups, disable BPS, or fabricate search results.

Ordinary text and reasoning summaries stream before terminal completion. The
bridge preserves output indexes, first response ID, content and summary identity,
and validates streamed text against terminal content. Forced-search prose waits
until search succeeds; structured final answers retain the existing JSON/schema
validation, and intermediate search prose is withheld. Hosted `web_search_call`
progress and sources are visible; citations are attached only to literal source
URLs actually present in final text. No fixed four-search limit is imposed.

The loop has a 10-minute context deadline, 90 seconds per search including its
slot wait, 16 concurrent search HTTP operations per process, 2 MiB per search
result and 32 MiB continuation/aggregate output limits. The gateway sends SSE keepalives while search waits. Client cancellation closes active response bodies and cancels searches
and continuation requests. These bounds are not an unlimited-runtime promise.

All completed BPS generation rounds contribute to final model token usage, also
on search failure after generation. The standalone search endpoint does not
provide an established billable token contract here; no synthetic token charge
is invented for it. Any provider-side search cost/entitlement needs separate
operational verification before enabling accounts. No real paid call is part of
CI or local validation.
13 changes: 11 additions & 2 deletions backend/internal/service/basispoints/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
// under the default fallback policy. This describes the bridge's capabilities,
// not whether the BPS upstream could support them through another protocol.
func NativeFallbackReason(body []byte) string {
return NativeFallbackReasonWithSearch(body, false)
}

// NativeFallbackReasonWithSearch leaves image routing unchanged.
func NativeFallbackReasonWithSearch(body []byte, searchEnabled bool) string {
if !gjson.ValidBytes(body) {
return ""
}
Expand All @@ -18,7 +23,11 @@ func NativeFallbackReason(body []byte) string {
return ""
}
switch choice.Get("type").String() {
case "web_search", "web_search_preview", "web_search_preview_2025_03_11", "web_search_2025_08_26", "image_generation":
case "web_search", "web_search_preview", "web_search_preview_2025_03_11", "web_search_2025_08_26":
if !searchEnabled {
return "tool_choice"
}
case "image_generation":
return "tool_choice"
}
if tools := gjson.GetBytes(body, "tools"); tools.IsArray() {
Expand All @@ -30,7 +39,7 @@ func NativeFallbackReason(body []byte) string {
fallback = "image_generation"
return false
case "web_search", "web_search_preview", "web_search_preview_2025_03_11", "web_search_2025_08_26":
if tool.Get("external_web_access").Bool() || tool.Get("search_context_size").String() == "high" {
if !searchEnabled && (tool.Get("external_web_access").Bool() || tool.Get("search_context_size").String() == "high") {
fallback = "web_search"
return false
}
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/service/basispoints/upstream_failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func failureIdentifierStatus(identifier string) int {
return http.StatusInternalServerError
case "overloaded_error", "service_unavailable":
return http.StatusServiceUnavailable
case "api_error", "basispoints_upstream_error", "basispoints_upstream_cancelled", "basispoints_protocol_error":
case "api_error", "basispoints_upstream_error", "basispoints_upstream_cancelled", "basispoints_protocol_error", "basispoints_search_failed":
return http.StatusBadGateway
default:
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestUpstreamFailureClassificationBoundaries(t *testing.T) {
status int
code string
}{
{"search failure", object{"type": "response.failed", "response": object{"error": object{"code": "basispoints_search_failed", "message": "PRIVATE"}}}, 502, "basispoints_search_failed"},
{"flat error", object{"type": "error", "code": "rate_limit_exceeded", "message": "PRIVATE"}, 429, "rate_limit_exceeded"},
{"nested status", object{"type": "error", "error": object{"status": 401, "code": "PRIVATE", "type": "invalid_request_error"}}, 401, "basispoints_upstream_error"},
{"explicit first", object{"type": "error", "status": 429, "error": object{"status": 400, "code": "invalid_value"}}, 429, "invalid_value"},
Expand Down
Loading
Loading