Files
magpie/internal/gateway/search_ark.go
T
yetone a0cb8b6466 gateway: Volcengine Ark's Responses API runs Codex's web_search itself
axelburks on Discord: Ark (火山方舟) runs the web_search tool on its
Responses API, but magpie took Codex's web_search out of the body and
gave the model magpie's web_search function instead, so the model said
the tool wasn't there. ark.cn-beijing.volces.com wasn't in searchHosts,
so searchedForIt translated every Codex turn for magpie's search.

Ark's 联网搜索工具 page (docs.volcengine.com/docs/82379/1756990) asks
POST ark.cn-beijing.volces.com/api/v3/responses with
{"type":"web_search"} (sources, limit, max_keyword, user_location), and
its Web Search page (82379/1585128) marks the tool 支持 on Responses and
不支持 on Chat. The host serves the Coding Plan (/api/coding/v3), Agent
Plan (/api/plan/v3) and pay-as-you-go (/api/v3) alike, and the page's
billing names both plans, so the host counts on Responses for all three.

- searchHosts[Responses] gains arkHost. Chat and Anthropic are unchanged.
- forArkSearch (search_ark.go), on the Responses relay after forVendor:
  Ark's web_search keeps only the members Ark's docs name. Codex's
  external_web_access, search_context_size and filters, and a
  location's timezone, are left out; web_search_preview becomes
  web_search. Ark's own shape ({"sources":["doubao"]}) goes as it came.
  Any other host, or an account, is untouched.
- BytePlus ModelArk (ark.ap-southeast.bytepluses.com) is left out: its
  docs name no web_search tool.
- Ark's Messages API (web_search_20250305) and Chat web_search_options,
  which the same page documents, are #4448.

Tests (search_ark_test.go): TestArkSearchesOnResponses (every Ark
region searches on Responses, not on Chat/Anthropic; BytePlus doesn't),
TestArkGetsCodexWebSearch (a Codex body through the real handler to a
fake Ark, Zhipu and relay over a redirected dialer: Ark gets the hosted
tool in its shape, Zhipu gets Codex's as sent, a relay still gets
magpie's function), TestForArkSearchScope.

Run (mote, mote-run):
- gofmt -l on the changed files: nothing.
- go vet -tags nogui ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build, GOOS=linux go build -tags nogui,
  GOOS=windows go build: ok.
- go test -run 'TestArk|TestForArkSearch|TestZhipuSearches|
  TestDeepSeekSearches|Search' ./internal/gateway: ok.
- go test -tags nogui -count=1 -timeout 45m ./... at fa427216a + this
  change: every package ok (exit 0; internal/gateway 729s).
- Rebased on 2ec345364 (it changed internal/gateway): vet x3 and builds
  x3 again ok, and the full suite again: every package ok except
  internal/plugin's TestGatewayHookMixedPiPackage, "starting plugins:
  context deadline exceeded" at 267s, which is #2865 (output added
  there); alone at this tree it passes (139.95s). internal/gateway ok
  (807s).
- Rebased again on acdc85ac8 (internal/provider and internal/switch
  only, nothing this change reads): gofmt, vet x3, builds x3 and
  internal/gateway + internal/provider tests again: VET-BUILD-OK,
  internal/gateway ok (652s), internal/provider ok (191s).
- Rebased on 2f65a33aa (a new tools-MCP route in internal/gateway):
  vet x3 of internal/gateway, go build, and -run
  'TestArk|TestForArkSearch|Search|ToolsMCP|Readonly' ok.

Broken by hand:
- arkHost out of searchHosts: TestArkSearchesOnResponses red for all
  three regions, and TestArkGetsCodexWebSearch red with Ark sent
  magpie's web_search function, the reporter's case.
- forArkSearch call removed: TestArkGetsCodexWebSearch red, Ark sent
  external_web_access, search_context_size and the timezone.

Not tried: a real Ark key (none here), so whether Ark turns away
Codex's external_web_access is from the docs, not a reply. Ark says
either search component must be activated in its console first, or the
call errors; a user who hasn't would now get that error on a Codex turn
offering web_search instead of magpie's search. Which models take the
tool is not listed by Ark (its page links a capability table); the
docs' example is doubao-seed-2-1-pro. With no Claude/Codex account, Ark
can now be magpie's automatic searcher for other models too.
2026-10-30 22:41:00 +08:00

107 lines
3.2 KiB
Go

package gateway
import (
"bytes"
"encoding/json"
"github.com/yetone/magpie/internal/provider"
)
// arkHost is Volcengine Ark's (火山方舟) API host. Its Responses API runs
// the web_search tool itself, on pay-as-you-go (/api/v3) and the plans
// (/api/coding/v3, /api/plan/v3) alike: Ark's 联网搜索工具 page
// (docs.volcengine.com/docs/82379/1756990) asks it on
// ark.cn-beijing.volces.com/api/v3/responses with {"type":"web_search"},
// and its Web Search page marks the tool 支持 on the Responses API and 不支持
// on the Chat API (axelburks on Discord: Codex's web_search was taken from
// its body, and the model said it had no such tool). BytePlus ModelArk's
// docs name no such tool, so its host isn't counted.
const arkHost = "ark.cn-beijing.volces.com"
// arkSearchFields are the members of a web_search tool Ark's Responses API
// documents: type, sources (["doubao"] for 豆包搜索 Custom, else
// search_engine with toutiao, douyin or moji), and the content plugin's
// limit, max_keyword and user_location.
var arkSearchFields = map[string]bool{"type": true, "sources": true, "limit": true, "max_keyword": true, "user_location": true}
// arkLocationFields are the members of Ark's user_location: the city,
// region and country, beside OpenAI's type. OpenAI's timezone isn't one.
var arkLocationFields = map[string]bool{"type": true, "country": true, "region": true, "city": true}
// forArkSearch gives Ark's Responses API the web_search tool in the shape
// its docs give: OpenAI's members Ark doesn't name (Codex's
// external_web_access, search_context_size, filters, a location's
// timezone) are left out, and web_search_preview is web_search. Any other
// provider's body, or one with no such tool, goes as it came.
func forArkSearch(p provider.Provider, body []byte) []byte {
if p.Account != nil || provider.HostOf(p.Base(provider.Responses)) != arkHost || !bytes.Contains(body, []byte("web_search")) {
return body
}
q, err := rawObject(body)
if err != nil {
return body
}
tools, err := rawArray(q["tools"])
if err != nil {
return body
}
changed := false
for i, raw := range tools {
t, err := rawObject(raw)
if err != nil {
continue
}
var typ string
if json.Unmarshal(t["type"], &typ) != nil || typ != "web_search" && typ != "web_search_preview" {
continue
}
out := map[string]json.RawMessage{"type": json.RawMessage(`"web_search"`)}
drop := typ != "web_search" // a member left out, or the type renamed
for k, v := range t {
if k == "type" {
continue
}
if !arkSearchFields[k] {
drop = true
continue
}
if k == "user_location" {
loc, err := rawObject(v)
if err != nil {
drop = true
continue
}
for lk := range loc {
if !arkLocationFields[lk] {
delete(loc, lk)
drop = true
}
}
if v, err = marshalPlain(loc); err != nil {
continue
}
}
out[k] = v
}
if !drop {
continue
}
b, err := marshalPlain(out)
if err != nil {
continue
}
tools[i], changed = b, true
}
if !changed {
return body
}
if q["tools"], err = marshalPlain(tools); err != nil {
return body
}
b, err := marshalPlain(q)
if err != nil {
return body
}
return b
}