diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 51b528b67..8c9a5476f 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -5092,7 +5092,8 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider. body, searchFn = searchAsFunction(body) } body = forVendor(p, body, asksCompaction(r.Context())) - // Ark takes web_search in the shape its docs give (search_ark.go) + // Ark takes web_search in the shape its docs give, and only the include + // values it knows (search_ark.go) body = forArkSearch(p, body) if replay { // its reasoning goes back as text, never left out: DeepSeek diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go index 9fce9851a..d67d926fb 100644 --- a/internal/gateway/responses.go +++ b/internal/gateway/responses.go @@ -1497,7 +1497,7 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte { var include []string for _, v := range r.Include { if v == "" || slices.Contains(include, v) || (v == "reasoning.encrypted_content" && out["reasoning"] == nil) || - v == outputReplay && host != "chatgpt.com" && host != "api.openai.com" { + v == outputReplay && host != "chatgpt.com" && host != "api.openai.com" || !includeTaken(host, v) { continue } include = append(include, v) @@ -1513,8 +1513,9 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte { if r.WebSearch { tools = append(tools, map[string]any{"type": "web_search"}) // the pages it found, which a client of another protocol is - // told of (OpenAI's option; xAI's API isn't known to take it) - if host != "api.x.ai" && !slices.Contains(include, "web_search_call.action.sources") { + // told of (OpenAI's option; xAI's API isn't known to take it, + // and Ark's refuses it, arkIncludes) + if host != "api.x.ai" && includeTaken(host, "web_search_call.action.sources") && !slices.Contains(include, "web_search_call.action.sources") { include = append(include, "web_search_call.action.sources") } } diff --git a/internal/gateway/search_ark.go b/internal/gateway/search_ark.go index 1a3330a57..e2d8d2d8f 100644 --- a/internal/gateway/search_ark.go +++ b/internal/gateway/search_ark.go @@ -28,24 +28,65 @@ var arkSearchFields = map[string]bool{"type": true, "sources": true, "limit": tr // 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: ": 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. Any other -// provider's body, or one with no such tool, goes as it came. +// 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")) { + 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 } - tools, err := rawArray(q["tools"]) - 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 { @@ -90,13 +131,15 @@ func forArkSearch(p provider.Provider, body []byte) []byte { if err != nil { continue } - tools[i], changed = b, true + tools[i], toolsChanged = b, true } - if !changed { + if !changed && !toolsChanged { return body } - if q["tools"], err = marshalPlain(tools); err != nil { - return body + if toolsChanged { + if q["tools"], err = marshalPlain(tools); err != nil { + return body + } } b, err := marshalPlain(q) if err != nil { diff --git a/internal/gateway/search_ark_test.go b/internal/gateway/search_ark_test.go index 8e62c06bf..036c96377 100644 --- a/internal/gateway/search_ark_test.go +++ b/internal/gateway/search_ark_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" ) // Volcengine Ark's Responses API runs web_search itself, on each of its @@ -167,3 +168,136 @@ func TestForArkSearchScope(t *testing.T) { t.Errorf("Ark's own shape changed: %s", out) } } + +// arkRefusesInclude answers as Ark's Responses API does a request whose +// include names a value it doesn't take (asked with a real key, +// 2026-11-01): the whole request is refused. +func arkRefusesInclude(body []byte) (int, string) { + var q struct { + Include []string `json:"include"` + } + json.Unmarshal(body, &q) + for _, v := range q.Include { + if !arkIncludes[v] { + return 400, `{"error":{"code":"InvalidParameter","message":"The parameter ` + "`include`" + ` specified in the request are not valid: unknown type: ` + v + `. Request id: 021793510893623dc6263e9e52ba05a43f20bd75063a4f8b23617","param":"include","type":"BadRequest"}}` + } + } + return 0, "" +} + +const arkSearchedReply = "data: {\"type\":\"response.created\",\"response\":{\"id\":\"r1\",\"model\":\"deepseek-v4-1-flash-260910\"}}\n\n" + + "data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"web_search_call\",\"status\":\"completed\",\"action\":{\"type\":\"search\",\"query\":\"今天北京天气\"}}}\n\n" + + "data: {\"type\":\"response.output_text.delta\",\"delta\":\"北京今天晴,见 https://www.weather.com.cn/weather/101010100.shtml\"}\n\n" + + "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"r1\",\"usage\":{\"input_tokens\":40,\"output_tokens\":20}}}\n\n" + +// Every search that reaches Ark's Responses API goes without an include +// value Ark refuses (axelburks on Discord: 400 "The parameter `include` +// specified in the request are not valid: unknown type: ..."): a client's +// web search on an Ark model, translated (Claude Code's +// web_search_20250305) or relayed (OpenCode's AI SDK, which adds +// web_search_call.action.sources itself), and Settings' searcher on an +// Ark model. Sealed reasoning, which Ark takes, still goes. +func TestArkSearchSendsNoIncludeItRefuses(t *testing.T) { + searchSandbox(t) + f := &fake{t: t, reply: arkSearchedReply, refuse: arkRefusesInclude} + up := httptest.NewServer(f) + t.Cleanup(up.Close) + ark := provider.Provider{ID: "volcengine", Preset: "volcengine", Name: "Volcengine Ark", Key: "k", Models: []string{"deepseek-v4-1-flash-260910"}, Responses: "http://ark.cn-beijing.volces.com/api/v3"} + if err := provider.Save(ark); err != nil { + t.Fatal(err) + } + s := New() + addr := strings.TrimPrefix(up.URL, "http://") + s.client = &http.Client{Transport: &http.Transport{DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) { + return (&net.Dialer{}).DialContext(ctx, network, addr) + }}} + sent := func(t *testing.T) []string { + t.Helper() + var q struct { + Include []string `json:"include"` + Tools []map[string]any `json:"tools"` + } + if err := json.Unmarshal(f.got, &q); err != nil { + t.Fatalf("sent %s: %v", f.got, err) + } + if !slices.ContainsFunc(q.Tools, func(tl map[string]any) bool { return tl["type"] == "web_search" }) { + t.Fatalf("Ark wasn't sent its web_search: %s", f.got) + } + return q.Include + } + + t.Run("claude code", func(t *testing.T) { + code, reply := postTo(t, s, "/v1/messages", `{"model":"volcengine/deepseek-v4-1-flash-260910","max_tokens":3000,"stream":true,"messages":[{"role":"user","content":"今天北京天气"}],"tools":[{"type":"web_search_20250305","name":"web_search"}]}`) + if code != 200 || strings.Contains(reply, "unknown type") { + t.Fatalf("%d %s", code, reply) + } + if inc := sent(t); len(inc) != 0 { + t.Errorf("Ark was asked for include %q", inc) + } + }) + t.Run("opencode relayed", func(t *testing.T) { + code, reply := postTo(t, s, "/v1/responses", `{"model":"volcengine/deepseek-v4-1-flash-260910","stream":true,"reasoning":{"effort":"low"},"input":[{"role":"user","content":[{"type":"input_text","text":"今天北京天气"}]}],`+ + `"tools":[{"type":"web_search"}],"include":["reasoning.encrypted_content","web_search_call.action.sources"]}`) + if code != 200 || strings.Contains(reply, "unknown type") { + t.Fatalf("%d %s", code, reply) + } + if inc := sent(t); !slices.Equal(inc, []string{"reasoning.encrypted_content"}) { + t.Errorf("Ark was asked for include %q, want the sealed reasoning alone", inc) + } + }) + t.Run("settings searcher", func(t *testing.T) { + st := settings.Load() + st.Searcher = "volcengine/deepseek-v4-1-flash-260910" + if err := settings.Save(st); err != nil { + t.Fatal(err) + } + said, _, err := s.modelSearch(context.Background(), "今天北京天气") + if err != nil || !strings.Contains(said, "北京今天晴") { + t.Fatalf("search = %q %v", said, err) + } + if inc := sent(t); len(inc) != 0 { + t.Errorf("Ark was asked for include %q", inc) + } + }) +} + +// Ark's rule is Ark's: OpenAI's Responses API, and a relay, are still +// asked for the pages a search found, and a client's include goes on as +// it came; Ark keeps only what it takes, with no sources added. +func TestArkIncludeScope(t *testing.T) { + include := func(body, host string) []string { + t.Helper() + r, err := parseResponses([]byte(body)) + if err != nil { + t.Fatal(err) + } + var q struct { + Include []string `json:"include"` + } + json.Unmarshal(buildResponses(r, "m", host, false), &q) + return q.Include + } + const body = `{"model":"m","input":"hi","reasoning":{"effort":"low"},"include":["reasoning.encrypted_content","message.output_text.logprobs"],"tools":[{"type":"web_search"}]}` + for host, want := range map[string][]string{ + "api.openai.com": {"reasoning.encrypted_content", "message.output_text.logprobs", "web_search_call.action.sources"}, + "relay.example": {"reasoning.encrypted_content", "message.output_text.logprobs", "web_search_call.action.sources"}, + arkHost: {"reasoning.encrypted_content"}, + } { + if got := include(body, host); !slices.Equal(got, want) { + t.Errorf("%s: include %q, want %q", host, got, want) + } + } + // a relayed body: OpenAI's as it came, Ark's without what it refuses + in := []byte(`{"model":"m","tools":[{"type":"function","name":"f"}],"include":["reasoning.encrypted_content","web_search_call.action.sources"]}`) + if out := forArkSearch(provider.Provider{ID: "openai", Responses: "https://api.openai.com/v1"}, in); string(out) != string(in) { + t.Errorf("OpenAI's body changed: %s", out) + } + out := forArkSearch(provider.Provider{ID: "volcengine", Responses: "https://ark.cn-beijing.volces.com/api/v3"}, in) + if !strings.Contains(string(out), `"include":["reasoning.encrypted_content"]`) || !strings.Contains(string(out), `"name":"f"`) { + t.Errorf("Ark's body: %s", out) + } + only := forArkSearch(provider.Provider{ID: "volcengine", Responses: "https://ark.cn-beijing.volces.com/api/v3"}, []byte(`{"model":"m","include":["web_search_call.action.sources"]}`)) + if strings.Contains(string(only), "include") || strings.Contains(string(only), "tools") { + t.Errorf("Ark's body with nothing it takes: %s", only) + } +}