(params)
| 319 | } |
| 320 | |
| 321 | async function webSearch(params) { |
| 322 | requireApiKey(); |
| 323 | |
| 324 | const { query, model = "perplexity-fast", detailed = false } = params; |
| 325 | |
| 326 | if (!query || typeof query !== "string") { |
| 327 | throw new Error("Query is required and must be a string"); |
| 328 | } |
| 329 | |
| 330 | const searchModels = [ |
| 331 | "perplexity-fast", |
| 332 | "perplexity-reasoning", |
| 333 | "gemini-search", |
| 334 | ]; |
| 335 | if (!searchModels.includes(model)) { |
| 336 | throw new Error( |
| 337 | `Model "${model}" doesn't support web search. Use: ${searchModels.join(", ")}`, |
| 338 | ); |
| 339 | } |
| 340 | |
| 341 | const systemPrompt = detailed |
| 342 | ? "Search the web and provide a comprehensive answer with sources. Include relevant details and cite your sources." |
| 343 | : "Search the web and provide a concise, accurate answer. Include source URLs."; |
| 344 | |
| 345 | const requestBody = { |
| 346 | model, |
| 347 | messages: [ |
| 348 | { role: "system", content: systemPrompt }, |
| 349 | { role: "user", content: query }, |
| 350 | ], |
| 351 | }; |
| 352 | |
| 353 | try { |
| 354 | const response = await fetch(`${API_BASE_URL}/v1/chat/completions`, { |
| 355 | method: "POST", |
| 356 | headers: { |
| 357 | "Content-Type": "application/json", |
| 358 | ...getAuthHeaders(), |
| 359 | }, |
| 360 | body: JSON.stringify(requestBody), |
| 361 | }); |
| 362 | |
| 363 | if (!response.ok) { |
| 364 | const errorText = await response |
| 365 | .text() |
| 366 | .catch(() => "Unknown error"); |
| 367 | throw new Error(parseApiError(response.status, errorText)); |
| 368 | } |
| 369 | |
| 370 | const result = await response.json(); |
| 371 | const answer = result.choices?.[0]?.message?.content || ""; |
| 372 | |
| 373 | const responseData = { |
| 374 | answer, |
| 375 | query, |
| 376 | model: result.model || model, |
| 377 | }; |
| 378 |
nothing calls this directly
no test coverage detected