RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
feat(chat): add AnySearch web search provider (#20633)
Guna Palanivelbd9caf4e1f08
diff --git a/docs/references/http_api_reference.md b/docs/references/http_api_reference.mdindex accff398b9..b32c6efc19 100644--- a/docs/references/http_api_reference.md+++ b/docs/references/http_api_reference.md@@ -2682,7 +2682,8 @@ curl --request POST \- `"refine_multiturn"`: `boolean`- `"reasoning"`: `boolean`- `"cross_languages"`: `list[string]`- - `"web_search_provider"`: `string` The web search service to use. Supported values are `"brave"`, `"exa"`, `"firecrawl"`, `"linkup"`, `"parallel"`, `"querit"`, `"search1api"`, `"searchapi"`, `"serply"`, `"tavily"`, and `"youcom"`. If omitted, Tavily is selected only when `"tavily_api_key"` is configured; otherwise web search is disabled.+ - `"web_search_provider"`: `string` The web search service to use. Supported values are `"anysearch"`, `"brave"`, `"exa"`, `"firecrawl"`, `"linkup"`, `"parallel"`, `"querit"`, `"search1api"`, `"searchapi"`, `"serply"`, `"tavily"`, and `"youcom"`. If omitted, Tavily is selected only when `"tavily_api_key"` is configured; otherwise web search is disabled.+ - `"anysearch_api_key"`: `string` The optional [AnySearch](https://anysearch.com/console/api-keys) API key. Set `web_search_provider` to `"anysearch"` when using this field. Omit it or leave it blank for anonymous search; service limits apply.- `"tavily_api_key"`: `string`- `"brave_api_key"`: `string` The Brave Search API key. Set `web_search_provider` to `"brave"` when using this field.- `"exa_api_key"`: `string` The Exa API key. Set `web_search_provider` to `"exa"` when using this field.diff --git a/internal/service/web_search_anysearch_integration_test.go b/internal/service/web_search_anysearch_integration_test.gonew file mode 100644index 0000000000..29272b944a--- /dev/null+++ b/internal/service/web_search_anysearch_integration_test.go@@ -0,0 +1,138 @@+//go:build integration++// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package service++import (+ "bytes"+ "context"+ "encoding/json"+ "io"+ "net/http"+ "os"+ "regexp"+ "strings"+ "testing"+ "time"+)++// This checks the live provider contract, not the full ordinary Chat pipeline.+func TestAnySearchLiveProvider(t *testing.T) {+ if os.Getenv("RAGFLOW_TEST_ANYSEARCH") != "1" {+ t.Skip("set RAGFLOW_TEST_ANYSEARCH=1 to call the real AnySearch API")+ }+ cases := []struct{ name, key string }{{name: "anonymous"}}+ if key := strings.TrimSpace(os.Getenv("ANYSEARCH_TEST_API_KEY")); key != "" {+ cases = append(cases, struct{ name, key string }{name: "keyed", key: key})+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ const query = "What is the capital of France?"+ var response struct {+ Code *int `json:"code"`+ RequestID string `json:"request_id"`+ Data struct {+ Results []anySearchWebSearchResult `json:"results"`+ } `json:"data"`+ }+ requests, status := 0, 0+ client := *anySearchWebSearchHTTPClient+ client.Transport = anySearchTestTransport(func(r *http.Request) (*http.Response, error) {+ requests+++ if r.Method != http.MethodPost || r.URL.String() != anySearchWebSearchEndpoint {+ t.Error("request did not use the official search endpoint")+ }+ _, authPresent := r.Header["Authorization"]+ if authPresent != (tc.key != "") || (authPresent && r.Header.Get("Authorization") != "Bearer "+tc.key) {+ t.Error("authentication did not match the selected mode")+ }+ body, err := r.GetBody()+ if err != nil {+ return nil, err+ }+ var sent struct {+ Query string `json:"query"`+ MaxResults int `json:"max_results"`+ }+ err = json.NewDecoder(body).Decode(&sent)+ body.Close()+ if err != nil || sent.Query != query || sent.MaxResults != 6 {+ t.Error("live request query or result count differs from the contract")+ }+ res, err := http.DefaultTransport.RoundTrip(r)+ if err != nil {+ return nil, err+ }+ status = res.StatusCode+ if status < 200 || status >= 300 {+ return res, nil+ }+ data, err := io.ReadAll(io.LimitReader(res.Body, webSearchMaxResponseBytes+1))+ res.Body.Close()+ if err != nil {+ return nil, err+ }+ res.Body = io.NopCloser(bytes.NewReader(data))+ if len(data) <= webSearchMaxResponseBytes {+ _ = json.Unmarshal(data, &response)+ }+ return res, nil+ })+ original := anySearchWebSearchHTTPClient+ anySearchWebSearchHTTPClient = &client+ t.Cleanup(func() { anySearchWebSearchHTTPClient = original })+ ctx, cancel := context.WithTimeout(t.Context(), 45*time.Second)+ defer cancel()+ result, err := (&ChatPipelineService{}).retrieveWebSearch(ctx,+ resolveWebSearchProvider(map[string]interface{}{"web_search_provider": "anysearch", "anysearch_api_key": tc.key}), query)+ if err != nil {+ t.Fatalf("live provider failed: %v", err)+ }+ if requests != 1 || status < 200 || status >= 300 || response.Code == nil || *response.Code != 0 {+ t.Fatalf("requests=%d HTTP=%d successful_business_code=%t", requests, status, response.Code != nil && *response.Code == 0)+ }+ chunks, ok := result["chunks"].([]map[string]interface{})+ if !ok || len(chunks) == 0 || len(chunks) > 6 {+ t.Fatalf("live query delivered %d chunks, valid_payload=%t", len(chunks), ok)+ }+ for _, chunk := range chunks {+ matched := false+ for _, hit := range response.Data.Results {+ content := hit.Content+ if strings.TrimSpace(content) == "" {+ content = hit.Snippet+ }+ if chunk["url"] == strings.TrimSpace(hit.URL) && chunk["content_with_weight"] == strings.TrimSpace(content) && chunk["docnm_kwd"] == hit.Title {+ matched = true+ break+ }+ }+ if !matched {+ t.Error("delivered text/title/URL do not correlate with an actual upstream hit")+ }+ if id, ok := chunk["chunk_id"].(string); !ok || !strings.HasPrefix(id, "anysearch-") {+ t.Error("live citation is not scoped to AnySearch")+ }+ }+ requestID := "redacted"+ validID, _ := regexp.MatchString(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`, response.RequestID)+ if validID && (tc.key == "" || !strings.Contains(strings.ToLower(response.RequestID), strings.ToLower(tc.key))) {+ requestID = response.RequestID+ }+ t.Logf("requests=%d HTTP=%d code=0 request_id=%s upstream_hits=%d delivered_chunks=%d mode=%s", requests, status, requestID, len(response.Data.Results), len(chunks), tc.name)+ })+ }+}diff --git a/internal/service/web_search_anysearch_test.go b/internal/service/web_search_anysearch_test.gonew file mode 100644index 0000000000..b660afd0eb--- /dev/null+++ b/internal/service/web_search_anysearch_test.go@@ -0,0 +1,502 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package service++import (+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "net"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"+ "time"+)++func TestResolveWebSearchProviderSelectsAnySearch(t *testing.T) {+ for _, tc := range []struct {+ name string+ key interface{}+ want string+ }{+ {name: "missing key"},+ {name: "empty key", key: ""},+ {name: "whitespace key", key: " \t\n"},+ {name: "non-string key", key: 42},+ {name: "saved key", key: " anysearch-test ", want: "anysearch-test"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ config := map[string]interface{}{+ "web_search_provider": "anysearch",+ "tavily_api_key": "tavily-other",+ "youcom_api_key": "youcom-other",+ }+ if tc.key != nil {+ config["anysearch_api_key"] = tc.key+ }+ provider := resolveWebSearchProvider(config)+ if provider == nil || provider.Provider != "anysearch" || provider.APIKey != tc.want {+ t.Fatalf("provider = %+v, want AnySearch with key %q", provider, tc.want)+ }+ })+ }+ if provider := resolveWebSearchProvider(map[string]interface{}{"anysearch_api_key": "anysearch-test"}); provider != nil {+ t.Fatalf("unselected AnySearch key activated search: %+v", provider)+ }+}++func TestRetrieveAnySearchWebSearchSendsQueryAndOptionalBearer(t *testing.T) {+ for _, tc := range []struct {+ name string+ key string+ want string+ }{+ {name: "anonymous"},+ {name: "blank key", key: " \t\n"},+ {name: "saved key", key: " anysearch-test ", want: "Bearer anysearch-test"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ calls := 0+ query := "RAGFlow 中文? \"citations\" + diagrams"+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ calls+++ if r.Method != http.MethodPost {+ t.Errorf("method = %q, want POST", r.Method)+ }+ if got := r.Header.Get("Authorization"); got != tc.want {+ t.Errorf("Authorization = %q, want %q", got, tc.want)+ }+ if tc.want == "" {+ if _, exists := r.Header["Authorization"]; exists {+ t.Error("anonymous request has an Authorization header")+ }+ }+ if r.Header.Get("Content-Type") != "application/json" {+ t.Errorf("Content-Type = %q", r.Header.Get("Content-Type"))+ }+ var body map[string]interface{}+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {+ t.Errorf("decode request: %v", err)+ }+ if body["query"] != query || body["max_results"] != float64(6) || len(body) != 2 {+ t.Errorf("request body = %#v, want query and max_results=6", body)+ }+ _, _ = io.WriteString(w, `{"code":0,"data":{"results":[{"title":"RAGFlow","url":"https://example.com/ragflow","content":" An open-source RAG engine. ","snippet":"Ignored snippet."}]}}`)+ }))+ defer server.Close()+ result, err := retrieveAnySearchWebSearch(t.Context(), server.Client(), server.URL, tc.key, query)+ if err != nil {+ t.Fatalf("retrieve AnySearch: %v", err)+ }+ if calls != 1 {+ t.Fatalf("requests = %d, want 1", calls)+ }+ assertWebSearchChunk(t, result, "anysearch-https://example.com/ragflow", "RAGFlow", "An open-source RAG engine.")+ chunk := result["chunks"].([]map[string]interface{})[0]+ agg := result["doc_aggs"].([]interface{})[0].(map[string]interface{})+ if chunk["doc_id"] != chunk["chunk_id"] || agg["doc_id"] != chunk["doc_id"] || agg["url"] != chunk["url"] {+ t.Fatalf("citation identifiers disagree: chunk=%#v aggregate=%#v", chunk, agg)+ }+ })+ }+}++func TestDecodeAnySearchWebSearchResultsChecksEnvelope(t *testing.T) {+ for _, tc := range []struct {+ name string+ body string+ }{+ {"invalid JSON", `{`},+ {"trailing JSON", `{"code":0,"data":{"results":[]}} {}`},+ {"null response", `null`},+ {"array response", `[]`},+ {"missing code", `{"data":{"results":[]}}`},+ {"null code", `{"code":null,"data":{"results":[]}}`},+ {"string code", `{"code":"0","data":{"results":[]}}`},+ {"boolean code", `{"code":false,"data":{"results":[]}}`},+ {"decimal code", `{"code":0.0,"data":{"results":[]}}`},+ {"nonzero code", `{"code":401,"message":"upstream-secret","data":{"results":[]}}`},+ {"missing data", `{"code":0}`},+ {"null data", `{"code":0,"data":null}`},+ {"array data", `{"code":0,"data":[]}`},+ {"missing results", `{"code":0,"data":{}}`},+ {"null results", `{"code":0,"data":{"results":null}}`},+ {"object results", `{"code":0,"data":{"results":{}}}`},+ {"null result", `{"code":0,"data":{"results":[null]}}`},+ {"string result", `{"code":0,"data":{"results":["upstream-secret"]}}`},+ {"wrong title type", `{"code":0,"data":{"results":[{"title":42}]}}`},+ {"wrong url type", `{"code":0,"data":{"results":[{"url":false}]}}`},+ {"wrong content type", `{"code":0,"data":{"results":[{"content":{"secret":"upstream-secret"}}]}}`},+ {"wrong snippet type", `{"code":0,"data":{"results":[{"snippet":[]}]}}`},+ {"malformed second result", `{"code":0,"data":{"results":[{"url":"https://example.com","content":"valid"},null]}}`},+ } {+ t.Run(tc.name, func(t *testing.T) {+ results, err := decodeAnySearchWebSearchResults([]byte(tc.body))+ if err == nil || len(results) != 0 {+ t.Fatalf("results=%#v error=%v, want failure without partial results", results, err)+ }+ if strings.Contains(err.Error(), "upstream-secret") {+ t.Fatalf("error exposed upstream content: %v", err)+ }+ })+ }+ for _, body := range []string{+ `{"code":0,"data":{"results":[]}}`,+ `{"code":0,"data":{"results":[{}]}}`,+ `{"code":0,"data":{"results":[{"title":null,"url":null,"content":null,"snippet":null}]}}`,+ } {+ if _, err := decodeAnySearchWebSearchResults([]byte(body)); err != nil {+ t.Fatalf("valid envelope %s: %v", body, err)+ }+ }+}++func TestRetrieveAnySearchWebSearchFiltersInvalidURLs(t *testing.T) {+ for _, tc := range []struct {+ name string+ url string+ want string+ }{+ {name: "HTTPS", url: "https://example.com/article", want: "https://example.com/article"},+ {name: "HTTP", url: "http://example.com/article", want: "http://example.com/article"},+ {name: "trimmed query and fragment", url: " \thttps://example.com/article?q=rag%20flow#source \n", want: "https://example.com/article?q=rag%20flow#source"},+ {name: "IPv6", url: "https://[2001:db8::1]/article", want: "https://[2001:db8::1]/article"},+ {name: "javascript", url: "javascript:alert(1)"},+ {name: "data", url: "data:text/html,hello"},+ {name: "FTP", url: "ftp://example.com/article"},+ {name: "relative", url: "/relative/path"},+ {name: "scheme relative", url: "//example.com/article"},+ {name: "missing host", url: "https:///missing-host"},+ {name: "port only", url: "https://:443/article"},+ {name: "opaque HTTP", url: "http:article"},+ {name: "invalid escape", url: "https://example.com/%zz"},+ {name: "invalid port", url: "https://example.com:bad/article"},+ {name: "invalid bracket", url: "https://[example.com/article"},+ {name: "host whitespace", url: "https://bad host/article"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ body, err := json.Marshal(map[string]interface{}{"code": 0, "data": map[string]interface{}{+ "results": []map[string]string{{"title": "Source", "url": tc.url, "content": "Source content"}},+ }})+ if err != nil {+ t.Fatal(err)+ }+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(body) }))+ defer server.Close()+ payload, err := retrieveAnySearchWebSearch(t.Context(), server.Client(), server.URL, "", "query")+ if err != nil {+ t.Fatalf("retrieve AnySearch: %v", err)+ }+ chunks := payload["chunks"].([]map[string]interface{})+ cards := payload["doc_aggs"].([]interface{})+ if tc.want == "" {+ if len(chunks) != 0 || len(cards) != 0 {+ t.Fatalf("invalid URL retained: chunks=%#v cards=%#v", chunks, cards)+ }+ return+ }+ if len(chunks) != 1 || len(cards) != 1 || chunks[0]["url"] != tc.want || cards[0].(map[string]interface{})["url"] != tc.want {+ t.Fatalf("valid URL mapping changed: chunks=%#v cards=%#v", chunks, cards)+ }+ card := cards[0].(map[string]interface{})+ if chunks[0]["chunk_id"] != "anysearch-"+tc.want || chunks[0]["doc_id"] != chunks[0]["chunk_id"] ||+ chunks[0]["docnm_kwd"] != "Source" || chunks[0]["content_with_weight"] != "Source content" ||+ card["doc_id"] != chunks[0]["doc_id"] || card["doc_name"] != "Source" {+ t.Fatalf("valid citation mapping changed: chunks=%#v cards=%#v", chunks, cards)+ }+ })+ }+}++func TestRetrieveAnySearchWebSearchFiltersBeforeCapAndFallsBackToSnippet(t *testing.T) {+ results := []map[string]string{+ {"url": "https://example.com/blank", "content": " \n", "snippet": " \t"},+ {"url": " \t", "content": "orphan text"},+ }+ for i := 0; i < webSearchResultCount; i++ {+ results = append(results, map[string]string{"url": fmt.Sprintf("javascript:alert(%d)", i), "content": "invalid citation"})+ }+ for i := 0; i < 8; i++ {+ results = append(results, map[string]string{"title": fmt.Sprintf("Hit %d", i), "url": fmt.Sprintf(" https://example.com/%d ", i), "content": " \n", "snippet": fmt.Sprintf(" Snippet %d ", i)})+ }+ body, err := json.Marshal(map[string]interface{}{"code": 0, "data": map[string]interface{}{"results": results}})+ if err != nil {+ t.Fatal(err)+ }+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(body) }))+ defer server.Close()+ payload, err := retrieveAnySearchWebSearch(t.Context(), server.Client(), server.URL, "", "ragflow")+ if err != nil {+ t.Fatal(err)+ }+ chunks := payload["chunks"].([]map[string]interface{})+ if len(chunks) != 6 || len(payload["doc_aggs"].([]interface{})) != 6 {+ t.Fatalf("payload = %#v, want six usable results", payload)+ }+ for i, chunk := range chunks {+ if chunk["url"] != fmt.Sprintf("https://example.com/%d", i) || chunk["content_with_weight"] != fmt.Sprintf("Snippet %d", i) {+ t.Fatalf("chunk %d = %#v", i, chunk)+ }+ }+}++func TestRetrieveAnySearchWebSearchReturnsEmptyPayload(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ _, _ = io.WriteString(w, `{"code":0,"data":{"results":[]}}`)+ }))+ defer server.Close()+ result, err := retrieveAnySearchWebSearch(t.Context(), server.Client(), server.URL, "", "ragflow")+ if err != nil {+ t.Fatal(err)+ }+ if len(result["chunks"].([]map[string]interface{})) != 0 || len(result["doc_aggs"].([]interface{})) != 0 {+ t.Fatalf("payload = %#v, want empty chunks and aggregates", result)+ }+}++func TestRetrieveAnySearchWebSearchReturnsSafeProviderFailures(t *testing.T) {+ for _, status := range []int{401, 403, 429, 500, 503} {+ t.Run(fmt.Sprint(status), func(t *testing.T) {+ calls := 0+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ calls+++ w.WriteHeader(status)+ _, _ = io.WriteString(w, `{"message":"credential-secret query-secret"}`)+ }))+ defer server.Close()+ _, err := retrieveAnySearchWebSearch(t.Context(), server.Client(), server.URL, "credential-secret", "query-secret")+ if err == nil || !strings.Contains(err.Error(), fmt.Sprint(status)) {+ t.Fatalf("error = %v, want safe HTTP status %d", err, status)+ }+ assertAnySearchErrorSafe(t, err)+ if calls != 1 {+ t.Fatalf("requests = %d, want no retry", calls)+ }+ })+ }+ for _, code := range []int{401, 429} {+ _, err := decodeAnySearchWebSearchResults([]byte(fmt.Sprintf(`{"code":%d,"message":"credential-secret query-secret","data":{"results":[]}}`, code)))+ if err == nil || !strings.Contains(err.Error(), fmt.Sprint(code)) {+ t.Fatalf("error = %v, want business code %d", err, code)+ }+ assertAnySearchErrorSafe(t, err)+ }+}++type anySearchTestTransport func(*http.Request) (*http.Response, error)++func (transport anySearchTestTransport) RoundTrip(request *http.Request) (*http.Response, error) {+ return transport(request)+}++func assertAnySearchErrorSafe(t *testing.T, err error) {+ t.Helper()+ for _, secret := range []string{"credential-secret", "query-secret", "transport-secret"} {+ if strings.Contains(err.Error(), secret) {+ t.Fatalf("error exposed %q: %v", secret, err)+ }+ }+}++func TestRetrieveAnySearchWebSearchSanitizesTransportAndContextErrors(t *testing.T) {+ for _, tc := range []struct {+ name string+ err error+ }{+ {name: "transport", err: errors.New("transport-secret credential-secret query-secret")},+ {name: "canceled", err: context.Canceled},+ {name: "deadline", err: context.DeadlineExceeded},+ {name: "timeout", err: &net.DNSError{Err: "transport-secret", Name: "credential-secret", IsTimeout: true}},+ } {+ t.Run(tc.name, func(t *testing.T) {+ client := &http.Client{Transport: anySearchTestTransport(func(*http.Request) (*http.Response, error) { return nil, tc.err })}+ _, err := retrieveAnySearchWebSearch(t.Context(), client, "https://example.com/credential-secret", "credential-secret", "query-secret")+ if err == nil {+ t.Fatal("error is nil")+ }+ assertAnySearchErrorSafe(t, err)+ if (tc.err == context.Canceled || tc.err == context.DeadlineExceeded) && !errors.Is(err, tc.err) {+ t.Fatalf("error %v does not preserve %v", err, tc.err)+ }+ if tc.name == "timeout" && !strings.Contains(err.Error(), "timed out") {+ t.Fatalf("timeout category was lost: %v", err)+ }+ })+ }+ ctx, cancel := context.WithCancel(t.Context())+ cancel()+ _, err := retrieveAnySearchWebSearch(ctx, &http.Client{}, "http://127.0.0.1:1", "credential-secret", "query-secret")+ if !errors.Is(err, context.Canceled) {+ t.Fatalf("pre-canceled request error = %v", err)+ }+ assertAnySearchErrorSafe(t, err)+ _, err = retrieveAnySearchWebSearch(t.Context(), &http.Client{}, "http://%credential-secret", "credential-secret", "query-secret")+ if err == nil {+ t.Fatal("invalid endpoint succeeded")+ }+ assertAnySearchErrorSafe(t, err)+}++type anySearchTestErrorBody struct {+ closed bool+}++func (*anySearchTestErrorBody) Read([]byte) (int, error) {+ return 0, errors.New("transport-secret credential-secret query-secret")+}++func (body *anySearchTestErrorBody) Close() error {+ body.closed = true+ return nil+}++func TestRetrieveAnySearchWebSearchSanitizesBodyReadFailure(t *testing.T) {+ body := &anySearchTestErrorBody{}+ client := &http.Client{Transport: anySearchTestTransport(func(*http.Request) (*http.Response, error) {+ return &http.Response{StatusCode: 200, Header: make(http.Header), Body: body}, nil+ })}+ _, err := retrieveAnySearchWebSearch(t.Context(), client, "https://example.com", "credential-secret", "query-secret")+ if err == nil {+ t.Fatal("body read failure succeeded")+ }+ assertAnySearchErrorSafe(t, err)+ if !body.closed {+ t.Error("response body was not closed")+ }+}++func TestRetrieveAnySearchWebSearchHonorsDeadlineAndBodyLimit(t *testing.T) {+ t.Run("deadline", func(t *testing.T) {+ release := make(chan struct{})+ server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {+ select {+ case <-r.Context().Done():+ case <-release:+ }+ }))+ defer func() {+ close(release)+ server.Close()+ }()+ ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond)+ defer cancel()+ _, err := retrieveAnySearchWebSearch(ctx, server.Client(), server.URL, "credential-secret", "query-secret")+ if !errors.Is(err, context.DeadlineExceeded) {+ t.Fatalf("deadline error = %v", err)+ }+ assertAnySearchErrorSafe(t, err)+ })+ t.Run("body limit", func(t *testing.T) {+ body := `{"code":0,"data":{"results":[]}}`+ for _, size := range []int{webSearchMaxResponseBytes, webSearchMaxResponseBytes + 1} {+ t.Run(fmt.Sprint(size), func(t *testing.T) {+ responseBody := body + strings.Repeat(" ", size-len(body))+ client := &http.Client{Transport: anySearchTestTransport(func(*http.Request) (*http.Response, error) {+ return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(responseBody))}, nil+ })}+ _, err := retrieveAnySearchWebSearch(t.Context(), client, "https://example.com", "", "q")+ if size == webSearchMaxResponseBytes && err != nil {+ t.Fatalf("response at limit failed: %v", err)+ }+ if size > webSearchMaxResponseBytes && (err == nil || !strings.Contains(err.Error(), "exceed")) {+ t.Fatalf("oversized response error = %v", err)+ }+ })+ }+ })+}++func TestRetrieveAnySearchWebSearchRefusesRedirects(t *testing.T) {+ targetCalls := 0+ target := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { targetCalls++ }))+ defer target.Close()+ for _, status := range []int{301, 302, 307, 308} {+ t.Run(fmt.Sprint(status), func(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Redirect(w, r, target.URL+"/credential-secret", status)+ }))+ defer server.Close()+ client := *anySearchWebSearchHTTPClient+ client.Transport = server.Client().Transport+ _, err := retrieveAnySearchWebSearch(t.Context(), &client, server.URL, "credential-secret", "query-secret")+ if err == nil {+ t.Fatal("redirect succeeded")+ }+ assertAnySearchErrorSafe(t, err)+ })+ }+ if targetCalls != 0 {+ t.Fatalf("redirect destination received %d requests", targetCalls)+ }+}++func TestAnySearchWebSearchUsesNormalConsumersWithoutTavilyFallback(t *testing.T) {+ original := anySearchWebSearchHTTPClient+ t.Cleanup(func() { anySearchWebSearchHTTPClient = original })+ calls := 0+ fail := false+ anySearchWebSearchHTTPClient = &http.Client{Transport: anySearchTestTransport(func(r *http.Request) (*http.Response, error) {+ calls+++ if r.URL.String() != "https://api.anysearch.com/v1/search" || r.Header.Get("Authorization") != "Bearer anysearch-test" {+ t.Errorf("unexpected dispatched request: %s Authorization=%q", r.URL, r.Header.Get("Authorization"))+ }+ body := `{"code":0,"data":{"results":[{"url":"https://example.com/ragflow","title":"RAGFlow","content":"Native consumer text."}]}}`+ if fail {+ body = `{"code":401,"message":"credential-secret"}`+ }+ return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}, nil+ })}+ config := map[string]interface{}{"web_search_provider": "anysearch", "anysearch_api_key": "anysearch-test", "tavily_api_key": "other-test"}+ provider := resolveWebSearchProvider(config)+ service := &ChatPipelineService{}+ researcher := &DeepResearcher{}+ for _, retrieve := range []func(context.Context, *webSearchProviderConfig, string) (map[string]interface{}, error){service.retrieveWebSearch, researcher.retrieveWebSearch} {+ result, err := retrieve(t.Context(), provider, "ragflow")+ if err != nil {+ t.Fatal(err)+ }+ assertWebSearchChunk(t, result, "anysearch-https://example.com/ragflow", "RAGFlow", "Native consumer text.")+ }+ agenticCallback := service.webSearchFunc(config)+ if agenticCallback == nil {+ t.Fatal("AnySearch agentic callback is nil")+ }+ if service.webSearchFunc(map[string]interface{}{"web_search_provider": "", "anysearch_api_key": "anysearch-test"}) != nil {+ t.Fatal("cleared provider retained an agentic callback")+ }+ hits, err := agenticCallback(t.Context(), "ragflow")+ if err != nil || len(hits) != 1 || hits[0].Title != "RAGFlow" || hits[0].URL != "https://example.com/ragflow" || hits[0].Content != "Native consumer text." || calls != 3 {+ t.Fatalf("agentic result=%#v error=%v requests=%d", hits, err, calls)+ }+ fail = true+ tavilyCalls := 0+ _, err = retrieveWebSearchWithTavily(t.Context(), provider, "ragflow", func(context.Context, string, string) (map[string]interface{}, error) {+ tavilyCalls+++ return nil, errors.New("unexpected Tavily fallback")+ })+ if err == nil || tavilyCalls != 0 {+ t.Fatalf("failure error=%v Tavily requests=%d", err, tavilyCalls)+ }+ hits, err = agenticCallback(t.Context(), "ragflow")+ if err == nil || len(hits) != 0 || calls != 5 {+ t.Fatalf("agentic failure result=%#v error=%v requests=%d", hits, err, calls)+ }+ assertAnySearchErrorSafe(t, err)+}diff --git a/internal/service/web_search_provider.go b/internal/service/web_search_provider.goindex 29248f99de..80c38fc17f 100644--- a/internal/service/web_search_provider.go+++ b/internal/service/web_search_provider.go@@ -20,8 +20,10 @@ import ("bytes""context""encoding/json"+ "errors""fmt""io"+ "net""net/http""net/url""strconv"@@ -33,11 +35,12 @@ import ()// Providers, endpoints and clients are listed alphabetically by provider id-// (brave, exa, firecrawl, linkup, parallel, querit, search1api, searchapi, serply,+// (anysearch, brave, exa, firecrawl, linkup, parallel, querit, search1api, searchapi, serply,// tavily, youcom) so a new provider has exactly one obvious place in each list.// Tavily's endpoint lives with its retrieval code in chat_pipeline.go, which is// why it has no entry here.const (+ webSearchProviderAnySearch = "anysearch"webSearchProviderBrave = "brave"webSearchProviderExa = "exa"webSearchProviderFirecrawl = "firecrawl"@@ -50,8 +53,9 @@ const (webSearchProviderTavily = "tavily"webSearchProviderYouCom = "youcom"- braveWebSearchEndpoint = "https://api.search.brave.com/res/v1/web/search"- exaWebSearchEndpoint = "https://api.exa.ai/search"+ anySearchWebSearchEndpoint = "https://api.anysearch.com/v1/search"+ braveWebSearchEndpoint = "https://api.search.brave.com/res/v1/web/search"+ exaWebSearchEndpoint = "https://api.exa.ai/search"// v2 is Firecrawl's current search shape; the v1 endpoint is deprecated.firecrawlWebSearchEndpoint = "https://api.firecrawl.dev/v2/search"linkupWebSearchEndpoint = "https://api.linkup.so/v1/search"@@ -74,6 +78,12 @@ const ()var (+ anySearchWebSearchHTTPClient = &http.Client{+ Timeout: 30 * time.Second,+ CheckRedirect: func(_ *http.Request, _ []*http.Request) error {+ return http.ErrUseLastResponse+ },+ }braveWebSearchHTTPClient = &http.Client{Timeout: 30 * time.Second}exaWebSearchHTTPClient = &http.Client{Timeout: 30 * time.Second}firecrawlWebSearchHTTPClient = &http.Client{Timeout: 30 * time.Second}@@ -90,6 +100,7 @@ var (// http.Client per request on a hot path shares no connections at all.tavilyWebSearchHTTPClient = &http.Client{Timeout: 30 * time.Second}tavilyDeepResearchHTTPClient = &http.Client{Timeout: 15 * time.Second}+ errWebSearchResponseTooLarge = fmt.Errorf("response body exceeds %d bytes", webSearchMaxResponseBytes))type webSearchProviderConfig struct {@@ -112,15 +123,21 @@ func resolveWebSearchProvider(promptConfig map[string]interface{}) *webSearchPro}apiKeyField := ""- // keyOptional marks providers usable with no key. Only You.com: it serves a- // dedicated keyless endpoint (a different path, same response shape, rate- // limited per source IP, and it rejects an X-API-Key header — see- // youcom.EndpointFor). A key moves to the keyed endpoint and lifts the limit.+ // keyOptional marks providers usable with no key.+ // AnySearch accepts anonymous requests on the same search endpoint and+ // sends Authorization only when a key is saved.+ // You.com serves a dedicated keyless endpoint (a different path, same+ // response shape, rate limited per source IP, and it rejects an X-API-Key+ // header; see youcom.EndpointFor). A key moves to the keyed endpoint and+ // lifts the limit.//// Free tier != keyless: Exa's free 1,000 requests/month still requires a key// on every call, so it does not get this carve-out.keyOptional := falseswitch provider {+ case webSearchProviderAnySearch:+ apiKeyField = "anysearch_api_key"+ keyOptional = truecase webSearchProviderBrave:apiKeyField = "brave_api_key"case webSearchProviderExa:@@ -194,6 +211,14 @@ func retrieveWebSearchWithTavily(return nil, fmt.Errorf("web search provider is not configured")}switch provider.Provider {+ case webSearchProviderAnySearch:+ return retrieveAnySearchWebSearch(+ ctx,+ anySearchWebSearchHTTPClient,+ anySearchWebSearchEndpoint,+ provider.APIKey,+ question,+ )case webSearchProviderBrave:return retrieveBraveWebSearch(ctx,@@ -342,11 +367,15 @@ func webSearchPayload(idPrefix string, hits []webSearchHit) map[string]interface}}+type webSearchHTTPStatusError int++func (e webSearchHTTPStatusError) Error() string {+ return fmt.Sprintf("status %d", int(e))+}+// webSearchRequest performs one provider call and returns the response body.-// Providers differ only in method, headers and body — the status check and the-// read are identical everywhere, and a request that never reached the provider-// must be reported the same way regardless of which one was called. Callers-// wrap the error with their own provider name.+// Providers differ only in method, headers and body; the shared helper bounds+// response size and reports failures for callers to handle.func webSearchRequest(ctx context.Context,client *http.Client,@@ -369,7 +398,7 @@ func webSearchRequest(defer response.Body.Close()if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {- return nil, fmt.Errorf("status %d", response.StatusCode)+ return nil, webSearchHTTPStatusError(response.StatusCode)}// Cap the body: a misbehaving or compromised provider must not be able to make// the server allocate an unbounded buffer. webSearchMaxResponseBytes is far above@@ -381,11 +410,114 @@ func webSearchRequest(return nil, fmt.Errorf("read response: %w", err)}if len(responseBody) > webSearchMaxResponseBytes {- return nil, fmt.Errorf("response body exceeds %d bytes", webSearchMaxResponseBytes)+ return nil, errWebSearchResponseTooLarge}return responseBody, nil}+// --- AnySearch --------------------------------------------------------------++type anySearchWebSearchResult struct {+ Title string `json:"title"`+ URL string `json:"url"`+ Content string `json:"content"`+ Snippet string `json:"snippet"`+}++func retrieveAnySearchWebSearch(+ ctx context.Context,+ client *http.Client,+ endpoint string,+ apiKey string,+ query string,+) (map[string]interface{}, error) {+ requestBody, err := json.Marshal(map[string]interface{}{+ "query": query,+ "max_results": webSearchResultCount,+ })+ if err != nil {+ return nil, fmt.Errorf("anysearch: marshal request failed")+ }+ headers := map[string]string{+ "Accept": "application/json",+ "Content-Type": "application/json",+ }+ if key := strings.TrimSpace(apiKey); key != "" {+ headers["Authorization"] = "Bearer " + key+ }+ responseBody, err := webSearchRequest(ctx, client, http.MethodPost, endpoint, headers, bytes.NewReader(requestBody))+ if err != nil {+ // Transport errors may contain credentials or request data. Only safe+ // categories and codes reach the host's ordinary web-search logs.+ var status webSearchHTTPStatusError+ var networkError net.Error+ switch {+ case errors.As(err, &status):+ return nil, fmt.Errorf("anysearch: HTTP status %d", int(status))+ case errors.Is(err, context.Canceled):+ return nil, fmt.Errorf("anysearch: %w", context.Canceled)+ case errors.Is(err, context.DeadlineExceeded):+ return nil, fmt.Errorf("anysearch: %w", context.DeadlineExceeded)+ case errors.As(err, &networkError) && networkError.Timeout():+ return nil, fmt.Errorf("anysearch: request timed out")+ case errors.Is(err, errWebSearchResponseTooLarge):+ return nil, fmt.Errorf("anysearch: %w", errWebSearchResponseTooLarge)+ default:+ return nil, fmt.Errorf("anysearch: request failed")+ }+ }+ results, err := decodeAnySearchWebSearchResults(responseBody)+ if err != nil {+ return nil, err+ }+ hits := make([]webSearchHit, 0, len(results))+ for _, result := range results {+ rawURL := strings.TrimSpace(result.URL)+ parsed, err := url.ParseRequestURI(rawURL)+ if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Hostname() == "" {+ continue+ }+ content := result.Content+ if strings.TrimSpace(content) == "" {+ content = result.Snippet+ }+ hits = append(hits, webSearchHit{+ Title: result.Title, URL: rawURL, Content: content,+ })+ }+ return webSearchPayload("anysearch", hits), nil+}++func decodeAnySearchWebSearchResults(responseBody []byte) ([]anySearchWebSearchResult, error) {+ var envelope map[string]json.RawMessage+ if err := json.Unmarshal(responseBody, &envelope); err != nil || envelope == nil {+ return nil, fmt.Errorf("anysearch: response must be a JSON object")+ }+ var code *int+ if err := json.Unmarshal(envelope["code"], &code); err != nil || code == nil {+ return nil, fmt.Errorf("anysearch: response code must be an integer")+ }+ if *code != 0 {+ return nil, fmt.Errorf("anysearch: business code %d", *code)+ }+ var data map[string]json.RawMessage+ if err := json.Unmarshal(envelope["data"], &data); err != nil || data == nil {+ return nil, fmt.Errorf("anysearch: response data must be an object")+ }+ var decoded []*anySearchWebSearchResult+ if err := json.Unmarshal(data["results"], &decoded); err != nil || decoded == nil {+ return nil, fmt.Errorf("anysearch: response data.results must be an array of objects with string fields")+ }+ results := make([]anySearchWebSearchResult, 0, len(decoded))+ for _, result := range decoded {+ if result == nil {+ return nil, fmt.Errorf("anysearch: response result must be an object")+ }+ results = append(results, *result)+ }+ return results, nil+}+// --- Brave Search -----------------------------------------------------------type braveWebSearchResult struct {diff --git a/internal/service/web_search_provider_test.go b/internal/service/web_search_provider_test.goindex 847ae94eed..31b0455f0d 100644--- a/internal/service/web_search_provider_test.go+++ b/internal/service/web_search_provider_test.go@@ -388,7 +388,7 @@ func TestDecodeSerplyWebSearchResultsAcceptsMissingResults(t *testing.T) {}func TestResolveWebSearchProviderSelectsYouComWithoutAKey(t *testing.T) {- // You.com is the only provider usable with no credentials at all.+ // You.com stays usable when no key is saved.provider := resolveWebSearchProvider(map[string]interface{}{"web_search_provider": "youcom",})@@ -406,7 +406,7 @@ func TestResolveWebSearchProviderSelectsYouComWithoutAKey(t *testing.T) {// Exa has a free tier of 1,000 requests/month, but it is NOT keyless: every// REST call must carry a key, so selecting Exa without one leaves web search-// unconfigured. (Only You.com is keyless — see the carve-out below.)+// unconfigured. AnySearch and You.com are the providers that allow a blank key.func TestResolveWebSearchProviderRequiresExaKeyForFreeTier(t *testing.T) {if got := resolveWebSearchProvider(map[string]interface{}{"web_search_provider": "exa",diff --git a/web/src/assets/anysearch.webp b/web/src/assets/anysearch.webpnew file mode 100644index 0000000000..23e4df3695Binary files /dev/null and b/web/src/assets/anysearch.webp differdiff --git a/web/src/components/web-search-form-field.test.tsx b/web/src/components/web-search-form-field.test.tsxnew file mode 100644index 0000000000..662edc4641--- /dev/null+++ b/web/src/components/web-search-form-field.test.tsx@@ -0,0 +1,101 @@+import { fireEvent, render, screen, waitFor } from '@testing-library/react';+import { FormProvider, useForm } from 'react-hook-form';+import { WebSearchProvider } from '@/constants/chat';+import { TooltipProvider } from '@/components/ui/tooltip';+import { hasWebSearchProvider } from '@/pages/next-chats/chat/web-search-api-key';+import { WebSearchFormField } from './web-search-form-field';++jest.mock('@/hooks/common-hooks', () => ({+ useTranslate: () => ({+ t: (key: string, values?: { provider?: string }) =>+ key === 'webSearchApiKeyLabel' ? `${values?.provider} API key` : key,+ }),+}));+// Exercise catalog values, React Hook Form state and the real key field without+// coupling this provider regression to the searchable dropdown's popover.+jest.mock('./originui/select-with-search', () => ({+ SelectWithSearch: ({+ value,+ onChange,+ options,+ }: {+ value?: string;+ onChange: (value: string) => void;+ options: { value: string }[];+ }) => {+ function handleChange(event: React.ChangeEvent<HTMLSelectElement>) {+ onChange(event.target.value);+ }+ return (+ <select aria-label="Provider" value={value ?? ''} onChange={handleChange}>+ <option value="">Clear</option>+ {options.map((option) => (+ <option key={option.value} value={option.value}>+ {option.value}+ </option>+ ))}+ </select>+ );+ },+}));++describe('AnySearch settings field', () => {+ it('selects, saves, reloads and clears AnySearch with an optional key', async () => {+ const save = jest.fn();+ function Settings({ saved = {} }: { saved?: Record<string, string> }) {+ const form = useForm({ defaultValues: { prompt_config: saved } });+ return (+ <FormProvider {...form}>+ <form onSubmit={form.handleSubmit(save)}>+ <TooltipProvider>+ <WebSearchFormField />+ </TooltipProvider>+ <button type="submit">Save</button>+ </form>+ </FormProvider>+ );+ }+ const { unmount } = render(<Settings />);+ const select = screen.getByRole('combobox', { name: 'Provider' });+ expect(select.querySelectorAll('option')[1]).toHaveValue(+ WebSearchProvider.AnySearch,+ );+ fireEvent.change(select, {+ target: { value: WebSearchProvider.AnySearch },+ });+ const key = screen.getByLabelText('AnySearch API key');+ expect(key).not.toBeRequired();+ expect(key).toHaveAttribute('placeholder', 'anysearchApiKeyMessage');+ expect(+ screen.getByRole('link', { name: 'webSearchApiKeyHelp' }),+ ).toHaveAttribute('href', 'https://anysearch.com/console/api-keys');+ fireEvent.click(screen.getByRole('button', { name: 'Save' }));+ await waitFor(() => expect(save).toHaveBeenCalledTimes(1));+ const saved = JSON.parse(+ JSON.stringify(save.mock.calls[0][0].prompt_config),+ );+ expect(hasWebSearchProvider(saved)).toBe(true);+ unmount();++ render(<Settings saved={saved} />);+ expect(screen.getByRole('combobox', { name: 'Provider' })).toHaveValue(+ WebSearchProvider.AnySearch,+ );+ fireEvent.change(screen.getByLabelText('AnySearch API key'), {+ target: { value: 'anysearch-test' },+ });+ fireEvent.change(screen.getByRole('combobox', { name: 'Provider' }), {+ target: { value: '' },+ });+ expect(+ screen.queryByLabelText('AnySearch API key'),+ ).not.toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: 'Save' }));+ await waitFor(() => expect(save).toHaveBeenCalledTimes(2));+ const cleared = JSON.parse(+ JSON.stringify(save.mock.calls[1][0].prompt_config),+ );+ expect(cleared.anysearch_api_key).toBe('anysearch-test');+ expect(hasWebSearchProvider(cleared)).toBe(false);+ });+});diff --git a/web/src/components/web-search-form-field.tsx b/web/src/components/web-search-form-field.tsxindex 40314d274b..7d81c4fbcc 100644--- a/web/src/components/web-search-form-field.tsx+++ b/web/src/components/web-search-form-field.tsx@@ -14,6 +14,7 @@* limitations under the License.*/+import anysearchLogo from '@/assets/anysearch.webp';import braveLogo from '@/assets/svg/brave.svg';import exaLogo from '@/assets/exa.png';import firecrawlLogo from '@/assets/firecrawl.png';@@ -41,6 +42,11 @@ interface IProps {// One entry per provider. `name` is the BRAND, so it stays untranslated; it is what// the key field's label is interpolated into (chat.webSearchApiKeyLabel).const webSearchProviderCatalog = [+ {+ name: 'AnySearch',+ logo: anysearchLogo,+ value: WebSearchProvider.AnySearch,+ },{name: 'Brave Search',logo: braveLogo,@@ -124,6 +130,12 @@ const providerDisplayName = (provider?: WebSearchProvider) =>'';const providerKeyConfig = {+ [WebSearchProvider.AnySearch]: {+ name: 'prompt_config.anysearch_api_key',+ tip: 'anysearchApiKeyTip',+ placeholder: 'anysearchApiKeyMessage',+ helpUrl: 'https://anysearch.com/console/api-keys',+ },[WebSearchProvider.Brave]: {name: 'prompt_config.brave_api_key',tip: 'braveApiKeyTip',diff --git a/web/src/constants/chat.ts b/web/src/constants/chat.tsindex bc0c40ee60..4855e96c73 100644--- a/web/src/constants/chat.ts+++ b/web/src/constants/chat.ts@@ -57,6 +57,7 @@ export enum DatasetMetadata {}export enum WebSearchProvider {+ AnySearch = 'anysearch',Brave = 'brave',Exa = 'exa',Firecrawl = 'firecrawl',@@ -74,6 +75,9 @@ export enum WebSearchProvider {* Providers usable with no credentials at all — the key field is optional for* these, and the Internet switch stays available when it is left blank.*+ * AnySearch accepts anonymous requests on the same search endpoint. A saved+ * key is sent as Authorization: Bearer and is not retried anonymously.+ ** You.com serves a dedicated keyless ENDPOINT: a different path from the keyed* one, the same response shape, rate-limited per source IP. Adding a key* switches to the keyed endpoint and lifts that limit.@@ -83,5 +87,6 @@ export enum WebSearchProvider {* is deliberately absent from this list.*/export const KEYLESS_WEB_SEARCH_PROVIDERS: readonly WebSearchProvider[] = [+ WebSearchProvider.AnySearch,WebSearchProvider.YouCom,];diff --git a/web/src/interfaces/database/chat.ts b/web/src/interfaces/database/chat.tsindex 8b1a5d9a17..3ce9c65884 100644--- a/web/src/interfaces/database/chat.ts+++ b/web/src/interfaces/database/chat.ts@@ -45,6 +45,7 @@ export interface PromptConfig {reasoning?: boolean;cross_languages?: Array<string>;agentic_rag?: AgenticRagOverride | null;+ anysearch_api_key?: string;brave_api_key?: string;exa_api_key?: string;firecrawl_api_key?: string;diff --git a/web/src/locales/en.ts b/web/src/locales/en.tsindex 383b839123..2a42dd006e 100644--- a/web/src/locales/en.ts+++ b/web/src/locales/en.ts@@ -1292,9 +1292,12 @@ This auto-tagging feature enhances retrieval by adding another layer of domain-swebSearchApiKeyRequired:'An API key is required for the selected provider — without one no Internet search happens and the switch never appears.',// The key field's label. {{provider}} is the provider's BRAND name, which is- // deliberately not translated, so this single template covers all nine.+ // deliberately not translated, so this single template covers all providers.webSearchApiKeyLabel: '{{provider}} API Key',// One Tip/Message pair per provider, alphabetical by provider id.+ anysearchApiKeyTip:+ 'Optional. AnySearch works without a key; add a key to use your account quota. Service limits apply.',+ anysearchApiKeyMessage: 'Optional - leave blank for anonymous search',braveApiKeyTip:'When Brave Search is selected, its web results supplement dataset retrieval. Every Brave endpoint requires a key.',braveApiKeyMessage: 'Please enter your Brave Search API Key',diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.tsindex 2c08e8ccaa..00b99985df 100644--- a/web/src/locales/zh.ts+++ b/web/src/locales/zh.ts@@ -1179,9 +1179,12 @@ NER:使用 spaCy NER 和基于规则的关键词提取来抽取 Entities 和 RwebSearchApiKeyRequired:'所选服务必须填写 API Key —— 否则不会发起联网搜索,聊天框里也不会出现联网开关。',// 密钥输入框的标签。{{provider}} 是 provider 的品牌名,故意不翻译,- // 因此一个模板即可覆盖全部 9 个 provider。+ // 因此一个模板即可覆盖全部 provider。webSearchApiKeyLabel: '{{provider}} API Key',// 每个 provider 一组 Tip/Message,按 provider id 字典序排列。+ anysearchApiKeyTip:+ '可选。AnySearch 无需密钥即可使用;填写密钥可使用账户额度。服务限额仍然适用。',+ anysearchApiKeyMessage: '可选 - 留空则使用匿名搜索',braveApiKeyTip:'选择 Brave Search 后,将使用其搜索结果补充知识库检索。Brave 的所有端点都需要 Key。',braveApiKeyMessage: '请输入你的 Brave Search API Key',diff --git a/web/src/pages/next-chats/chat/app-settings/chat-settings.tsx b/web/src/pages/next-chats/chat/app-settings/chat-settings.tsxindex 2fea37ab5d..f39fdbc533 100644--- a/web/src/pages/next-chats/chat/app-settings/chat-settings.tsx+++ b/web/src/pages/next-chats/chat/app-settings/chat-settings.tsx@@ -74,7 +74,7 @@ export function ChatSettings({ hasSingleChatBox }: ChatSettingsProps) {const form = useForm<FormSchemaType>({resolver: zodResolver(formSchema),// shouldUnregister: false keeps every field mounted in the form state, which- // includes all nine per-provider *_api_key fields. Switching provider therefore+ // includes all per-provider *_api_key fields. Switching provider therefore// KEEPS the keys already typed for the others and saves them with the dialog —// deliberate (a user may switch back), but it is why the payload can carry more// than one provider key.diff --git a/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.test.tsx b/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.test.tsxnew file mode 100644index 0000000000..eaf7a744ba--- /dev/null+++ b/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.test.tsx@@ -0,0 +1,102 @@+import { renderHook } from '@testing-library/react';+import { WebSearchProvider } from '@/constants/chat';+import { removeUselessFieldsFromValues } from '@/utils/form';+import { hasWebSearchProvider } from '../web-search-api-key';+import { useChatSettingSchema } from './use-chat-setting-schema';++jest.mock('@/hooks/common-hooks', () => ({+ useTranslate: () => ({ t: (key: string) => key }),+}));+jest.mock('@/components/llm-setting-items/next', () => ({+ LlmSettingEnabledSchema: {},+ LlmSettingFieldSchema: {},+}));+jest.mock('@/components/metadata-filter', () => ({ MetadataFilterSchema: {} }));+jest.mock('@/components/rerank-candidates-count-item', () => ({+ rerankCandidatesCountSchema: {},+}));+jest.mock('@/components/rerank', () => ({ rerankFormSchema: {} }));+jest.mock('@/components/similarity-slider', () => ({+ similarityThresholdSchema: {},+ keywordsSimilarityWeightSchema: {},+}));+jest.mock('@/components/top-n-item', () => ({ topnSchema: {} }));++const DefaultSettings = {+ name: 'Search chat',+ icon: '',+ dataset_ids: [],+ llm_setting: {},+ prompt_config: {+ system: 'Answer using {knowledge}',+ parameters: [{ key: 'knowledge', optional: false }],+ quote: true,+ keyword: false,+ tts: false,+ refine_multiturn: false,+ web_search_provider: WebSearchProvider.AnySearch,+ },+};++describe('AnySearch settings schema', () => {+ it.each([undefined, '', ' ', 'anysearch-test'])(+ 'preserves selected provider and optional key %p through save and reload',+ (key) => {+ const { result } = renderHook(useChatSettingSchema);+ const validated = result.current.parse({+ ...DefaultSettings,+ prompt_config: {+ ...DefaultSettings.prompt_config,+ anysearch_api_key: key,+ },+ });+ const reloaded = JSON.parse(+ JSON.stringify(+ removeUselessFieldsFromValues(validated, 'llm_setting.'),+ ),+ );+ expect(reloaded.prompt_config.web_search_provider).toBe(+ WebSearchProvider.AnySearch,+ );+ expect(reloaded.prompt_config.anysearch_api_key).toBe(key);+ expect(result.current.safeParse(reloaded).success).toBe(true);+ expect(hasWebSearchProvider(reloaded.prompt_config)).toBe(true);+ },+ );++ it('preserves explicit cleared selection and hides Internet after reload', () => {+ const { result } = renderHook(useChatSettingSchema);+ const saved = result.current.parse({+ ...DefaultSettings,+ prompt_config: {+ ...DefaultSettings.prompt_config,+ web_search_provider: '',+ anysearch_api_key: 'retained-key',+ tavily_api_key: 'retained-other-key',+ },+ });+ const reloaded = JSON.parse(JSON.stringify(saved));+ expect(reloaded.prompt_config.web_search_provider).toBe('');+ expect(hasWebSearchProvider(reloaded.prompt_config)).toBe(false);+ });++ it('still rejects a required-key provider without its own key', () => {+ const { result } = renderHook(useChatSettingSchema);+ const saved = result.current.safeParse({+ ...DefaultSettings,+ prompt_config: {+ ...DefaultSettings.prompt_config,+ web_search_provider: WebSearchProvider.Search1API,+ anysearch_api_key: 'other-provider-key',+ },+ });+ expect(saved.success).toBe(false);+ if (!saved.success) {+ expect(saved.error.issues).toContainEqual(+ expect.objectContaining({+ path: ['prompt_config', 'search1api_api_key'],+ }),+ );+ }+ });+});diff --git a/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.tsx b/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.tsxindex 7b41fda552..03312a8255 100644--- a/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.tsx+++ b/web/src/pages/next-chats/chat/app-settings/use-chat-setting-schema.tsx@@ -37,6 +37,7 @@ export function buildChatSettingFormSchema(t: (key: string) => string) {}),).optional(),+ anysearch_api_key: z.string().optional(),brave_api_key: z.string().optional(),exa_api_key: z.string().optional(),firecrawl_api_key: z.string().optional(),@@ -50,6 +51,7 @@ export function buildChatSettingFormSchema(t: (key: string) => string) {youcom_api_key: z.string().optional(),web_search_provider: z.enum([+ WebSearchProvider.AnySearch,WebSearchProvider.Brave,WebSearchProvider.Exa,WebSearchProvider.Firecrawl,@@ -117,7 +119,7 @@ export function buildChatSettingFormSchema(t: (key: string) => string) {// A keyed provider selected without its key fails SILENTLY at runtime —// the Internet switch never appears in the chat box — so block the save- // here instead. Keyless providers (You.com) are exempt. The rule lives in+ // here instead. Keyless providers are exempt. The rule lives in// missingWebSearchApiKeyField so it can be unit-tested without the form.const missingKeyField = missingWebSearchApiKeyField(value?.prompt_config);if (missingKeyField) {diff --git a/web/src/pages/next-chats/chat/use-show-internet.test.ts b/web/src/pages/next-chats/chat/use-show-internet.test.tsnew file mode 100644index 0000000000..16a0f8d8bc--- /dev/null+++ b/web/src/pages/next-chats/chat/use-show-internet.test.ts@@ -0,0 +1,40 @@+import { renderHook } from '@testing-library/react';+import { WebSearchProvider } from '@/constants/chat';+import { useShowInternet } from './use-show-internet';++const mockFetchChat = jest.fn();+jest.mock('@/hooks/use-chat-request', () => ({+ useFetchChat: () => mockFetchChat(),+}));++describe('saved provider Internet visibility', () => {+ it('follows reloaded keyless AnySearch settings and a cleared selection', () => {+ mockFetchChat.mockReturnValue({+ data: {+ prompt_config: {+ web_search_provider: WebSearchProvider.AnySearch,+ anysearch_api_key: '',+ },+ },+ });+ const { result, rerender } = renderHook(useShowInternet);+ expect(result.current).toBe(true);++ mockFetchChat.mockReturnValue({+ data: {+ prompt_config: {+ web_search_provider: '',+ anysearch_api_key: 'retained-key',+ tavily_api_key: 'retained-other-key',+ },+ },+ });+ rerender();+ expect(result.current).toBe(false);+ });++ it('hides Internet until saved settings arrive', () => {+ mockFetchChat.mockReturnValue({ data: undefined });+ expect(renderHook(useShowInternet).result.current).toBe(false);+ });+});diff --git a/web/src/pages/next-chats/chat/web-search-api-key.test.ts b/web/src/pages/next-chats/chat/web-search-api-key.test.tsindex 41456c819b..ce20a71078 100644--- a/web/src/pages/next-chats/chat/web-search-api-key.test.ts+++ b/web/src/pages/next-chats/chat/web-search-api-key.test.ts@@ -9,6 +9,56 @@ import {missingWebSearchApiKeyField,} from './web-search-api-key';+describe('AnySearch configuration', () => {+ const provider = WebSearchProvider.AnySearch;++ it.each([undefined, '', ' '])(+ 'allows anonymous search with key %p',+ (key) => {+ const saved = JSON.parse(+ JSON.stringify({+ web_search_provider: provider,+ anysearch_api_key: key,+ tavily_api_key: 'other-provider-key',+ }),+ );++ expect(getWebSearchProvider(saved)).toBe(provider);+ expect(getWebSearchApiKeyField(provider)).toBe('anysearch_api_key');+ expect(isWebSearchApiKeyRequired(provider)).toBe(false);+ expect(missingWebSearchApiKeyField(saved)).toBeUndefined();+ expect(hasWebSearchProvider(saved)).toBe(true);+ expect(getWebSearchApiKey(saved)).toBe(+ key === undefined ? undefined : '',+ );+ },+ );++ it('reads only the saved AnySearch key and trims it', () => {+ const saved = JSON.parse(+ JSON.stringify({+ web_search_provider: provider,+ anysearch_api_key: ' anysearch-test ',+ tavily_api_key: 'other-provider-key',+ }),+ );+ expect(getWebSearchApiKey(saved)).toBe('anysearch-test');+ });++ it('keeps a cleared provider disabled after reload despite retained keys', () => {+ const saved = JSON.parse(+ JSON.stringify({+ web_search_provider: '',+ anysearch_api_key: 'anysearch-test',+ tavily_api_key: 'other-provider-key',+ }),+ );+ expect(getWebSearchProvider(saved)).toBeUndefined();+ expect(getWebSearchApiKey(saved)).toBeUndefined();+ expect(hasWebSearchProvider(saved)).toBe(false);+ });+});+describe('getWebSearchProvider', () => {it('does not select a provider for a new unconfigured dialog', () => {expect(getWebSearchProvider({} as PromptConfig)).toBeUndefined();diff --git a/web/src/pages/next-chats/chat/web-search-api-key.ts b/web/src/pages/next-chats/chat/web-search-api-key.tsindex 99016469f2..4ff41aca0f 100644--- a/web/src/pages/next-chats/chat/web-search-api-key.ts+++ b/web/src/pages/next-chats/chat/web-search-api-key.ts@@ -53,6 +53,7 @@ export function getWebSearchProvider(promptConfig?: WebSearchKeyConfig) {// for validation, and the reader below uses it to fetch the value — three// callers that used to each spell the mapping out again.const webSearchApiKeyFields: Record<WebSearchProvider, string> = {+ [WebSearchProvider.AnySearch]: 'anysearch_api_key',[WebSearchProvider.Brave]: 'brave_api_key',[WebSearchProvider.Exa]: 'exa_api_key',[WebSearchProvider.Firecrawl]: 'firecrawl_api_key',