fix(web-search): wrap more provider json · openclaw/openclaw@695a4f5
vincentkoc
·
2026-05-15
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -94,6 +94,7 @@ Docs: https://docs.openclaw.ai
|
94 | 94 | - Exa: report malformed search API JSON with a provider-owned error instead of leaking raw parser failures. |
95 | 95 | - Memory host SDK: report malformed remote JSON with caller-scoped errors for POST and batch file upload responses instead of leaking raw parser failures. |
96 | 96 | - Media providers: report malformed operation-poll and audio-transcription JSON with provider-owned errors instead of leaking raw parser failures. |
| 97 | +- MiniMax, Gemini, Kimi, and Ollama web search: report malformed API JSON with provider-owned errors instead of leaking raw parser failures. |
97 | 98 | - Twilio voice-call: report malformed successful API JSON responses with provider-owned errors instead of leaking raw parser failures. |
98 | 99 | - Voice-call provider APIs: report malformed successful guarded JSON responses with provider-prefixed errors instead of leaking raw parser failures. |
99 | 100 | - Realtime transcription: report malformed provider websocket JSON frames with owned parser errors instead of leaking raw `SyntaxError` objects. |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | import { |
2 | 2 | createProviderHttpError, |
3 | 3 | formatProviderHttpErrorMessage, |
| 4 | +readProviderJsonResponse, |
4 | 5 | } from "openclaw/plugin-sdk/provider-http"; |
5 | 6 | import { |
6 | 7 | buildSearchCacheKey, |
@@ -196,13 +197,7 @@ async function runGeminiSearch(params: {
|
196 | 197 | throw new Error(error.message.replace(/key=[^&\s]+/giu, "key=***")); |
197 | 198 | } |
198 | 199 | |
199 | | -let data: GeminiGroundingResponse; |
200 | | -try { |
201 | | -data = (await res.json()) as GeminiGroundingResponse; |
202 | | -} catch (error) { |
203 | | -const safeError = String(error).replace(/key=[^&\s]+/giu, "key=***"); |
204 | | -throw new Error(`Gemini API returned invalid JSON: ${safeError}`, { cause: error }); |
205 | | -} |
| 200 | +const data = await readProviderJsonResponse<GeminiGroundingResponse>(res, "Gemini API error"); |
206 | 201 | |
207 | 202 | if (data.error) { |
208 | 203 | const rawMessage = data.error.message || data.error.status || "unknown"; |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -166,6 +166,34 @@ describe("google web search provider", () => {
|
166 | 166 | ); |
167 | 167 | }); |
168 | 168 | |
| 169 | +it("reports malformed Gemini API JSON with a stable provider error", async () => { |
| 170 | +vi.stubGlobal( |
| 171 | +"fetch", |
| 172 | +withFetchPreconnect(vi.fn(() => Promise.resolve(new Response("{ nope")))), |
| 173 | +); |
| 174 | +const provider = createGeminiWebSearchProvider(); |
| 175 | +const tool = provider.createTool({ |
| 176 | +config: { |
| 177 | +plugins: { |
| 178 | +entries: { |
| 179 | +google: { |
| 180 | +config: { |
| 181 | +webSearch: { |
| 182 | +apiKey: "AIza-plugin-test", |
| 183 | +}, |
| 184 | +}, |
| 185 | +}, |
| 186 | +}, |
| 187 | +}, |
| 188 | +}, |
| 189 | +searchConfig: { provider: "gemini" }, |
| 190 | +}); |
| 191 | + |
| 192 | +await expect(tool?.execute({ query: "OpenClaw docs" })).rejects.toThrow( |
| 193 | +"Gemini API error: malformed JSON response", |
| 194 | +); |
| 195 | +}); |
| 196 | + |
169 | 197 | it("passes provider execution abort signals into the Gemini fetch", async () => { |
170 | 198 | const mockFetch = installGeminiFetch(); |
171 | 199 | const controller = new AbortController(); |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | import { |
2 | 2 | createProviderHttpError, |
3 | 3 | formatProviderHttpErrorMessage, |
| 4 | +readProviderJsonResponse, |
4 | 5 | } from "openclaw/plugin-sdk/provider-http"; |
5 | 6 | import { |
6 | 7 | DEFAULT_SEARCH_COUNT, |
@@ -145,7 +146,10 @@ async function runMiniMaxSearch(params: {
|
145 | 146 | throw await createProviderHttpError(res, "MiniMax Search API error"); |
146 | 147 | } |
147 | 148 | |
148 | | -const data = (await res.json()) as MiniMaxSearchResponse; |
| 149 | +const data = await readProviderJsonResponse<MiniMaxSearchResponse>( |
| 150 | +res, |
| 151 | +"MiniMax Search API error", |
| 152 | +); |
149 | 153 | |
150 | 154 | if (data.base_resp?.status_code && data.base_resp.status_code !== 0) { |
151 | 155 | throw new Error( |
@@ -261,4 +265,5 @@ export const __testing = {
|
261 | 265 | resolveMiniMaxApiKey, |
262 | 266 | resolveMiniMaxEndpoint, |
263 | 267 | resolveMiniMaxRegion, |
| 268 | +readMiniMaxSearchJsonResponse: readProviderJsonResponse<MiniMaxSearchResponse>, |
264 | 269 | } as const; |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,7 @@ const {
|
7 | 7 | resolveMiniMaxApiKey, |
8 | 8 | resolveMiniMaxEndpoint, |
9 | 9 | resolveMiniMaxRegion, |
| 10 | + readMiniMaxSearchJsonResponse, |
10 | 11 | } = minimaxWebSearchTesting; |
11 | 12 | |
12 | 13 | function restoreEnvValue(key: string, value: string | undefined) { |
@@ -167,4 +168,10 @@ describe("minimax web search provider", () => {
|
167 | 168 | expect(MINIMAX_SEARCH_ENDPOINT_CN).toBe("https://api.minimaxi.com/v1/coding_plan/search"); |
168 | 169 | }); |
169 | 170 | }); |
| 171 | + |
| 172 | +it("reports malformed Search API JSON with a stable provider error", async () => { |
| 173 | +await expect( |
| 174 | +readMiniMaxSearchJsonResponse(new Response("{ nope"), "MiniMax Search API error"), |
| 175 | +).rejects.toThrow("MiniMax Search API error: malformed JSON response"); |
| 176 | +}); |
170 | 177 | }); |
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | | -import { createProviderHttpError } from "openclaw/plugin-sdk/provider-http"; |
| 1 | +import { |
| 2 | +createProviderHttpError, |
| 3 | +readProviderJsonResponse, |
| 4 | +} from "openclaw/plugin-sdk/provider-http"; |
2 | 5 | import type { OpenClawConfig } from "openclaw/plugin-sdk/provider-onboard"; |
3 | 6 | import { |
4 | 7 | buildSearchCacheKey, |
@@ -216,7 +219,7 @@ async function runKimiSearch(params: {
|
216 | 219 | throw await createProviderHttpError(res, "Kimi API error"); |
217 | 220 | } |
218 | 221 | |
219 | | -const data = (await res.json()) as KimiSearchResponse; |
| 222 | +const data = await readProviderJsonResponse<KimiSearchResponse>(res, "Kimi API error"); |
220 | 223 | if (hasKimiSearchResults(data)) { |
221 | 224 | hasGroundingEvidence = true; |
222 | 225 | } |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -158,6 +158,17 @@ describe("kimi web search provider", () => {
|
158 | 158 | }); |
159 | 159 | }); |
160 | 160 | |
| 161 | +it("reports malformed Kimi API JSON with a stable provider error", async () => { |
| 162 | +const fetchMock = vi.fn().mockResolvedValue(new Response("{ nope")); |
| 163 | +vi.stubGlobal("fetch", fetchMock); |
| 164 | + |
| 165 | +await withEnvAsync({ KIMI_API_KEY: "kimi-test-key" }, async () => { |
| 166 | +await expect(executeKimiSearch("kimi malformed response")).rejects.toThrow( |
| 167 | +"Kimi API error: malformed JSON response", |
| 168 | +); |
| 169 | +}); |
| 170 | +}); |
| 171 | + |
161 | 172 | it("accepts final responses backed by Kimi web search tool replay", async () => { |
162 | 173 | const toolArguments = JSON.stringify({ |
163 | 174 | query: "OpenClaw GitHub repository", |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -391,6 +391,20 @@ describe("ollama web search provider", () => {
|
391 | 391 | ); |
392 | 392 | }); |
393 | 393 | |
| 394 | +it("reports malformed Ollama web search JSON with a stable provider error", async () => { |
| 395 | +fetchWithSsrFGuardMock.mockResolvedValueOnce({ |
| 396 | +response: new Response("{ nope", { status: 200 }), |
| 397 | +release: vi.fn(async () => {}), |
| 398 | +}); |
| 399 | + |
| 400 | +await expect( |
| 401 | +runOllamaWebSearch({ |
| 402 | +config: createOllamaConfig(), |
| 403 | +query: "openclaw", |
| 404 | +}), |
| 405 | +).rejects.toThrow("Ollama web search returned malformed JSON"); |
| 406 | +}); |
| 407 | + |
394 | 408 | it("warns when Ollama is not reachable during setup without cancelling", async () => { |
395 | 409 | fetchWithSsrFGuardMock.mockRejectedValueOnce(new Error("connect failed")); |
396 | 410 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -65,6 +65,14 @@ type OllamaWebSearchAttempt = {
|
65 | 65 | apiKey?: string; |
66 | 66 | }; |
67 | 67 | |
| 68 | +async function readOllamaWebSearchResponse(response: Response): Promise<OllamaWebSearchResponse> { |
| 69 | +try { |
| 70 | +return (await response.json()) as OllamaWebSearchResponse; |
| 71 | +} catch (cause) { |
| 72 | +throw new Error("Ollama web search returned malformed JSON", { cause }); |
| 73 | +} |
| 74 | +} |
| 75 | + |
68 | 76 | function isOllamaCloudBaseUrl(baseUrl: string): boolean { |
69 | 77 | try { |
70 | 78 | const parsed = new URL(baseUrl); |
@@ -211,7 +219,7 @@ export async function runOllamaWebSearch(params: {
|
211 | 219 | } |
212 | 220 | throw new Error(message); |
213 | 221 | } |
214 | | -payload = (await response.json()) as OllamaWebSearchResponse; |
| 222 | +payload = await readOllamaWebSearchResponse(response); |
215 | 223 | break; |
216 | 224 | } catch (error) { |
217 | 225 | if (error instanceof Error) { |
@@ -336,5 +344,6 @@ export const __testing = {
|
336 | 344 | resolveOllamaWebSearchApiKey, |
337 | 345 | resolveOllamaWebSearchBaseUrl, |
338 | 346 | isOllamaCloudBaseUrl, |
| 347 | + readOllamaWebSearchResponse, |
339 | 348 | warnOllamaWebSearchPrereqs, |
340 | 349 | }; |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。