


























@@ -139,6 +139,14 @@ function buildPerplexityRequestHeaders(apiKey: string, acceptJson = false): Reco
139139};
140140}
141141142+async function readPerplexityJsonResponse<T>(response: Response, label: string): Promise<T> {
143+try {
144+return (await response.json()) as T;
145+} catch (cause) {
146+throw new Error(`${label}: malformed JSON response`, { cause });
147+}
148+}
149+142150function resolvePerplexityTransport(perplexity?: PerplexityConfig): {
143151apiKey?: string;
144152source: "config" | "perplexity_env" | "openrouter_env" | "none";
@@ -246,7 +254,10 @@ async function runPerplexitySearchApi(params: {
246254if (!res.ok) {
247255return await throwWebSearchApiError(res, "Perplexity Search");
248256}
249-const data = (await res.json()) as PerplexitySearchApiResponse;
257+const data = await readPerplexityJsonResponse<PerplexitySearchApiResponse>(
258+res,
259+"Perplexity Search",
260+);
250261return (data.results ?? []).map((entry) => ({
251262title: entry.title ? wrapWebContent(entry.title, "web_search") : "",
252263url: entry.url ?? "",
@@ -289,7 +300,7 @@ async function runPerplexitySearch(params: {
289300if (!res.ok) {
290301return await throwWebSearchApiError(res, "Perplexity");
291302}
292-const data = (await res.json()) as PerplexitySearchResponse;
303+const data = await readPerplexityJsonResponse<PerplexitySearchResponse>(res, "Perplexity");
293304return {
294305content: data.choices?.[0]?.message?.content ?? "No response",
295306citations: extractPerplexityCitations(data),
@@ -533,6 +544,7 @@ export const __testing = {
533544 isDirectPerplexityBaseUrl,
534545 resolvePerplexityRequestModel,
535546 resolvePerplexityApiKey,
547+ readPerplexityJsonResponse,
536548 normalizeToIsoDate,
537549 isoToPerplexityDate,
538550} as const;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。