Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve web search upstream errors https://github.com/siyuan-note/siyuan/issues/20196

Daniela1fbd7f1710b
diff --git a/kernel/util/websearch.go b/kernel/util/websearch.go
index 53903dfa0d..5497b70126 100644
--- a/kernel/util/websearch.go
+++ b/kernel/util/websearch.go
@@ -19,7 +19,9 @@ package util
import (
"encoding/json"
"errors"
+ "fmt"
"io"
+ "net/http"
"net/url"
"strings"
@@ -45,11 +47,18 @@ type mcpParams struct {
}
type mcpResponse struct {
- Result *mcpResult `json:"result,omitempty"`
+ Result *mcpResult `json:"result,omitempty"`
+ Error *mcpResponseError `json:"error,omitempty"`
+}
+
+type mcpResponseError struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
}
type mcpResult struct {
Content []mcpContent `json:"content"`
+ IsError bool `json:"isError,omitempty"`
}
type mcpContent struct {
@@ -82,7 +91,14 @@ func WebSearch(query, exaApiKey string) (string, error) {
if err != nil {
return "", errors.New("web search failed: " + err.Error())
}
+ return parseWebSearchResponse(resp.Response)
+}
+
+func parseWebSearchResponse(resp *http.Response) (string, error) {
defer resp.Body.Close()
+ if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
+ return "", fmt.Errorf("web search failed: HTTP %d", resp.StatusCode)
+ }
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
@@ -96,7 +112,10 @@ func WebSearch(query, exaApiKey string) (string, error) {
}
logging.LogInfof("websearch response: status=%d, len=%d, preview=%s", resp.StatusCode, len(body), preview)
- text := parseMcpResponse(body)
+ text, err := parseMcpResponse(body)
+ if err != nil {
+ return "", err
+ }
if text == "" {
return "No search results found. Please try a different query.", nil
}
@@ -104,41 +123,54 @@ func WebSearch(query, exaApiKey string) (string, error) {
return truncateRunes(text, maxWebSearchChars), nil
}
-func parseMcpResponse(body string) string {
- if text := parseMcpJSON(body); text != "" {
- return text
+func parseMcpResponse(body string) (string, error) {
+ if text, err := parseMcpJSON(body); text != "" || err != nil {
+ return text, err
}
for line := range strings.SplitSeq(body, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "data:") {
payload := strings.TrimSpace(line[5:])
- if text := parseMcpJSON(payload); text != "" {
- return text
+ if text, err := parseMcpJSON(payload); text != "" || err != nil {
+ return text, err
}
}
}
- return ""
+ return "", nil
}
-func parseMcpJSON(payload string) string {
+func parseMcpJSON(payload string) (string, error) {
payload = strings.TrimSpace(payload)
if !strings.HasPrefix(payload, "{") {
- return ""
+ return "", nil
}
var resp mcpResponse
if err := json.Unmarshal([]byte(payload), &resp); err != nil {
- return ""
+ return "", fmt.Errorf("web search invalid response: %w", err)
+ }
+ if resp.Error != nil {
+ return "", fmt.Errorf("web search RPC error %d: %s", resp.Error.Code, resp.Error.Message)
}
if resp.Result == nil {
- return ""
+ return "", nil
+ }
+ if resp.Result.IsError {
+ message := ""
+ for _, item := range resp.Result.Content {
+ if item.Text != "" {
+ message = item.Text
+ break
+ }
+ }
+ return "", fmt.Errorf("web search tool failed: %s", message)
}
for _, item := range resp.Result.Content {
if item.Text != "" {
- return item.Text
+ return item.Text, nil
}
}
- return ""
+ return "", nil
}
diff --git a/kernel/util/websearch_test.go b/kernel/util/websearch_test.go
new file mode 100644
index 0000000000..a7d9dfec26
--- /dev/null
+++ b/kernel/util/websearch_test.go
@@ -0,0 +1,50 @@
+package util
+
+import (
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+func TestWebSearchResponseErrors(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ status int
+ body string
+ errorText string
+ }{
+ {"unauthorized", 401, `{}`, "HTTP 401"},
+ {"limited", 429, `{}`, "HTTP 429"},
+ {"server error", 500, `<html>failed</html>`, "HTTP 500"},
+ {"redirect", 302, `{}`, "HTTP 302"},
+ {"RPC error", 200, `{"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"upstream failed"}}`, "RPC error -32603: upstream failed"},
+ {"SSE RPC error", 200, "event: message\ndata: {\"error\":{\"code\":-32000,\"message\":\"invalid key\"}}\n\n", "RPC error -32000: invalid key"},
+ {"tool error", 200, `{"result":{"isError":true,"content":[{"type":"text","text":"rate limited"}]}}`, "tool failed: rate limited"},
+ {"invalid JSON", 200, `{"result":`, "invalid response"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ response := httptest.NewRecorder()
+ response.WriteHeader(tc.status)
+ response.WriteString(tc.body)
+ text, err := parseWebSearchResponse(response.Result())
+ if err == nil || !strings.Contains(err.Error(), tc.errorText) || text != "" {
+ t.Fatalf("upstream failure was not preserved: text=%q err=%v", text, err)
+ }
+ })
+ }
+}
+
+func TestWebSearchResponseResults(t *testing.T) {
+ for _, tc := range []struct{ body, expected string }{
+ {`{"result":{"content":[{"type":"text","text":"search results"}]}}`, "search results"},
+ {"event: message\ndata: {\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"SSE results\"}]}}\n\n", "SSE results"},
+ {`{"result":{"content":[]}}`, "No search results found. Please try a different query."},
+ } {
+ response := httptest.NewRecorder()
+ response.WriteString(tc.body)
+ text, err := parseWebSearchResponse(response.Result())
+ if err != nil || text != tc.expected {
+ t.Fatalf("successful response changed: text=%q err=%v", text, err)
+ }
+ }
+}