|
| 1 | +/** |
| 2 | + * Exa API Search Provider |
| 3 | + * |
| 4 | + * API-based search provider that uses the Exa API for search. |
| 5 | + * Returns results formatted as markdown for consistency with browser providers. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { AriaBrowser } from "../../browser/ariaBrowser.js"; |
| 9 | +import type { SearchProvider } from "../searchProvider.js"; |
| 10 | +import { |
| 11 | + wrapExternalContentWithWarning, |
| 12 | + ExternalContentLabel, |
| 13 | +} from "../../utils/promptSecurity.js"; |
| 14 | +import { abbreviateForDebug } from "../debugPreview.js"; |
| 15 | + |
| 16 | +interface ExaSearchResult { |
| 17 | + url: string; |
| 18 | + title?: string; |
| 19 | + highlights?: string[]; |
| 20 | +} |
| 21 | + |
| 22 | +interface ExaApiResponse { |
| 23 | + results?: ExaSearchResult[]; |
| 24 | +} |
| 25 | + |
| 26 | +export class ExaSearchProvider implements SearchProvider { |
| 27 | + readonly name = "exa-api"; |
| 28 | + readonly requiresBrowser = false; |
| 29 | + |
| 30 | + constructor( |
| 31 | + private apiKey: string, |
| 32 | + private debug = false, |
| 33 | + ) {} |
| 34 | + |
| 35 | + async search(query: string, _browser?: AriaBrowser): Promise<string> { |
| 36 | + const url = "https://api.exa.ai/search"; |
| 37 | + const body = JSON.stringify({ |
| 38 | + query, |
| 39 | + // Opt into highlights, or Exa returns metadata only (no snippets). |
| 40 | + contents: { highlights: { maxCharacters: 1500 } }, |
| 41 | + }); |
| 42 | + |
| 43 | + if (this.debug) { |
| 44 | + // Log the exact outbound request body (sans API key) so the query and |
| 45 | + // contents options are observable. Matches the [X:debug] console.warn convention. |
| 46 | + console.warn(`[ExaSearch:debug] POST ${url}`, body); |
| 47 | + } |
| 48 | + |
| 49 | + const response = await fetch(url, { |
| 50 | + method: "POST", |
| 51 | + headers: { |
| 52 | + "Content-Type": "application/json", |
| 53 | + "x-api-key": this.apiKey, |
| 54 | + }, |
| 55 | + body, |
| 56 | + }); |
| 57 | + |
| 58 | + if (!response.ok) { |
| 59 | + const errorText = await response.text().catch(() => "Unknown error"); |
| 60 | + throw new Error(`Exa API error (${response.status}): ${errorText}`); |
| 61 | + } |
| 62 | + |
| 63 | + const data = (await response.json()) as ExaApiResponse; |
| 64 | + |
| 65 | + if (this.debug) { |
| 66 | + // Log the count plus an abbreviated sample of the first result so all |
| 67 | + // returned fields (including ones we don't map, like summary/score/ |
| 68 | + // publishedDate) are visible, with long strings truncated. |
| 69 | + const results = data.results ?? []; |
| 70 | + console.warn( |
| 71 | + `[ExaSearch:debug] response: ${results.length} result(s), sample:`, |
| 72 | + abbreviateForDebug(results[0]), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return this.formatAsMarkdown(query, data); |
| 77 | + } |
| 78 | + |
| 79 | + private formatAsMarkdown(query: string, data: ExaApiResponse): string { |
| 80 | + const header = `# Search Results for "${query}" (via ${this.name})`; |
| 81 | + |
| 82 | + let wrapped: string; |
| 83 | + if (!data.results || data.results.length === 0) { |
| 84 | + wrapped = wrapExternalContentWithWarning( |
| 85 | + `${header}\n\nNo results found.`, |
| 86 | + ExternalContentLabel.SearchResults, |
| 87 | + ); |
| 88 | + } else { |
| 89 | + const lines: string[] = []; |
| 90 | + |
| 91 | + data.results.forEach((result, index) => { |
| 92 | + const title = result.title || result.url; |
| 93 | + lines.push(`${index + 1}. [${title}](${result.url})`); |
| 94 | + if (result.highlights?.length) { |
| 95 | + lines.push(result.highlights.join("\n")); |
| 96 | + } |
| 97 | + lines.push(""); |
| 98 | + }); |
| 99 | + |
| 100 | + wrapped = wrapExternalContentWithWarning( |
| 101 | + `${header}\n\n${lines.join("\n").trim()}`, |
| 102 | + ExternalContentLabel.SearchResults, |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + return wrapped; |
| 107 | + } |
| 108 | +} |
0 commit comments