axelburks on Discord #feedback (1566310723922042991): every web search on an Ark model (ark.cn-beijing.volces.com) failed with 400 "The parameter `include` specified in the request are not valid: unknown type: web_search_call.action.sources". buildResponses adds that value to every Responses request offering web_search on any host but api.x.ai (#315,c4ae3ca77); Ark became a host that searches by itself later (a0cb8b646), and forArkSearch fitted the tool but not include. Ark's 创建 Response page (docs.volcengine.com/docs/82379/1569618) names `include` but lists no values, so they were asked of the real API with the owner's key (deepseek-v4-1-flash-260910): reasoning.encrypted_content is taken; web_search_call.action.sources, web_search_call.results, file_search_call.results, message.input_image.image_url, computer_call_output.output.image_url, code_interpreter_call.outputs and message.output_text.logprobs are each refused with the reporter's 400. - search_ark.go: arkIncludes (reasoning.encrypted_content alone) and includeTaken(host, v), Ark's host only. - buildResponses (every translated request, Claude Code's web_search_20250305 and Settings' searcher on an Ark model included): a value Ark refuses is left out of the client's include, and the sources value isn't added for Ark. Every other host is as it was. - forArkSearch (a Responses client relayed to Ark): include keeps what Ark takes, and goes when nothing is left. The AI SDK's OpenAI provider (OpenCode) adds web_search_call.action.sources itself whenever it offers web_search (@ai-sdk/openai 3.0.27, index.mjs addInclude), so that route failed the same way. Siblings in searchHosts[Responses]: api.openai.com takes the value (OpenAI's own), api.x.ai already gets none, and api.deepseek.com took it with web_search on the real API (200, completed). open.bigmodel.cn and api.z.ai: no key here, not tried; no report of a refusal. Tests: TestArkSearchSendsNoIncludeItRefuses (a fake Ark on the real host through a redirected dialer that answers the reporter's 400 for any include value Ark refuses: Claude Code's search translated, OpenCode's relayed with sealed reasoning kept, and Settings' searcher on the Ark model), TestArkIncludeScope (OpenAI's and a relay's include unchanged, sources still added; Ark's keeps only what it takes). Broken by hand: includeTaken returning true and forArkSearch's include filter off: all three subtests red with the reporter's exact 400 (searcher: "...unknown type: web_search_call.action.sources ... (the searcher picked in Settings › Web search ...)"), and TestArkIncludeScope red. Real use: the bodies the fixed gateway sends Ark on all three routes (captured from the test) sent to the real Ark API: each is accepted and streams; the old shape (with the sources value) is refused with the reporter's 400. This account hasn't activated Ark's web search, so the search itself ends ToolNotOpen; a search returning pages wasn't seen. Run (mote, mote-run, ate84870f17+ this change): - gofmt -l internal/gateway: nothing. - go vet ./internal/gateway, GOOS=linux go vet -tags nogui ./internal/gateway, GOOS=windows go vet ./internal/gateway: ok. - go test -count=1 -timeout 45m ./internal/gateway: ok (572.5s). Not run: the other packages' suites (only internal/gateway changed). Rebased onto6874b892b, which changes no internal/gateway file; gofmt, vet x3 and the Ark/include/search tests ran again there: ok.
150 lines
4.9 KiB
Go
150 lines
4.9 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}
|
|
|
|
// arkIncludes are the include values Ark's Responses API takes. Its
|
|
// 创建 Response page (docs.volcengine.com/docs/82379/1569618) names the
|
|
// parameter but no values; asked with a real key (2026-11-01, model
|
|
// deepseek-v4-1-flash-260910), it took reasoning.encrypted_content and
|
|
// refused the whole request over each of OpenAI's others with 400 "The
|
|
// parameter `include` specified in the request are not valid: unknown
|
|
// type: <value>": web_search_call.action.sources, web_search_call.results,
|
|
// file_search_call.results, message.input_image.image_url,
|
|
// computer_call_output.output.image_url, code_interpreter_call.outputs and
|
|
// message.output_text.logprobs (axelburks on Discord: every search on Ark
|
|
// failed over the sources magpie asked for).
|
|
var arkIncludes = map[string]bool{"reasoning.encrypted_content": true}
|
|
|
|
// includeTaken is whether a Responses request built for host may ask for
|
|
// the include value v: Ark's only what arkIncludes names, any other host's
|
|
// all of them.
|
|
func includeTaken(host, v string) bool {
|
|
return host != arkHost || arkIncludes[v]
|
|
}
|
|
|
|
// 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. An
|
|
// include value Ark refuses (arkIncludes) is left out too: the AI SDK's
|
|
// OpenAI provider (OpenCode's) adds web_search_call.action.sources to
|
|
// every request offering web_search. Any other provider's body, or one
|
|
// with neither, 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")) && !bytes.Contains(body, []byte(`"include"`)) {
|
|
return body
|
|
}
|
|
q, err := rawObject(body)
|
|
if err != nil {
|
|
return body
|
|
}
|
|
changed := false
|
|
if raw, ok := q["include"]; ok {
|
|
var inc []string
|
|
if json.Unmarshal(raw, &inc) == nil {
|
|
kept := make([]string, 0, len(inc))
|
|
for _, v := range inc {
|
|
if arkIncludes[v] {
|
|
kept = append(kept, v)
|
|
}
|
|
}
|
|
if len(kept) < len(inc) {
|
|
if len(kept) == 0 {
|
|
delete(q, "include")
|
|
} else if q["include"], err = marshalPlain(kept); err != nil {
|
|
return body
|
|
}
|
|
changed = true
|
|
}
|
|
}
|
|
}
|
|
tools, _ := rawArray(q["tools"])
|
|
toolsChanged := 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], toolsChanged = b, true
|
|
}
|
|
if !changed && !toolsChanged {
|
|
return body
|
|
}
|
|
if toolsChanged {
|
|
if q["tools"], err = marshalPlain(tools); err != nil {
|
|
return body
|
|
}
|
|
}
|
|
b, err := marshalPlain(q)
|
|
if err != nil {
|
|
return body
|
|
}
|
|
return b
|
|
}
|