All MicroEvals
import { getExaApiKey } from "@/lib/env"; import { asSummary...
Create MicroEval
Header image for import { getExaApiKey } from "@/lib/env";
import { asSummary...

import { getExaApiKey } from "@/lib/env"; import { asSummary...

Prompt

import { getExaApiKey } from "@/lib/env"; import { asSummaryText, isExaCategory, isExaSearchType, normalizeLinkList, toIsoDate } from "@/lib/filters"; import type { ExaCategory, ExaSearchType } from "@/lib/types"; const EXA_SEARCH_URL = "https://api.exa.ai/search"; const EXA_CONTENTS_URL = "https://api.exa.ai/contents"; export const REQUESTED_NUM_RESULTS = 100; const NUM_RESULT_FALLBACKS = [100, 50, 25, 10] as const; const CONTENTS_BATCH_SIZE = 100; const SEARCH_TIMEOUT_MS = 240_000; const CONTENTS_TIMEOUT_MS = 180_000; export type ExaSearchParams = { query: string; type: ExaSearchType; category?: ExaCategory; startPublishedDate?: string; endPublishedDate?: string; includeLinks?: string[]; excludeLinks?: string[]; }; export type ExaSearchHit = { searchId: string | null; url: string; title: string | null; author: string | null; publishedDate: string | null; summary: string; }; export type ExaAppliedSearch = { query: string; type: ExaSearchType; category: ExaCategory | null; requestedNumResults: number; effectiveNumResults: number; summary: true; highlights: false; structuredOutputs: false; startPublishedDate: string | null; endPublishedDate: string | null; includeLinks: string[]; excludeLinks: string[]; omittedUnsupportedFilters: string[]; }; export type ExaSearchExecution = { hits: ExaSearchHit[]; applied: ExaAppliedSearch; }; export type ExaCrawlHit = { url: string; text: string; failure: string | null; }; type ExaSearchApiResult = { id?: unknown; url?: unknown; title?: unknown; author?: unknown; publishedDate?: unknown; summary?: unknown; }; type ExaSearchApiResponse = { results?: ExaSearchApiResult[]; }; type ExaContentsApiResult = { url?: unknown; text?: unknown; }; type ExaContentsApiStatus = { id?: unknown; status?: unknown; error?: { tag?: unknown; httpStatusCode?: unknown }; }; type ExaContentsApiResponse = { results?: ExaContentsApiResult[]; statuses?: ExaContentsApiStatus[]; }; class ExaHttpError extends Error { readonly status: number; constructor(status: number, message: string) { super(message); this.name = "ExaHttpError"; this.status = status; } } function exaHeaders(apiKey: string): HeadersInit { return { "Content-Type": "application/json", "x-api-key": apiKey, }; } async function readExaError(response: Response): Promise<string> { const body = await response.text(); try { const parsed = JSON.parse(body) as { error?: unknown; message?: unknown }; const error = parsed.error; if (typeof error === "string" && error.trim()) { return error.trim(); } if (error && typeof error === "object") { const nested = (error as { message?: unknown }).message; if (typeof nested === "string" && nested.trim()) { return nested.trim(); } } if (typeof parsed.message === "string" && parsed.message.trim()) { return parsed.message.trim(); } } catch { if (body.trim()) { return body.trim().slice(0, 600); } } return `Exa request failed with status ${response.status}`; } async function exaFetch(url: string, apiKey: string, body: unknown, timeoutMs: number): Promise<unknown> { const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), timeoutMs); try { const response = await fetch(url, { method: "POST", headers: exaHeaders(apiKey), body: JSON.stringify(body), signal: controller.signal, cache: "no-store", }); if (!response.ok) { throw new ExaHttpError(response.status, await readExaError(response)); } return (await response.json()) as unknown; } catch (cause) { if (cause instanceof ExaHttpError) { throw cause; } if (cause instanceof DOMException && cause.name === "AbortError") { throw new Error(`Exa request timed out after ${Math.round(timeoutMs / 1000)}s`); } throw cause instanceof Error ? cause : new Error("Exa request failed"); } finally { clearTimeout(timer); } } function mentionsResultLimit(message: string): boolean { const lowered = message.toLowerCase(); return lowered.includes("numresults") || lowered.includes("number of results") || lowered.includes("result limit"); } export function parseSearchToolArgs(raw: unknown): ExaSearchParams { const record = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : {}; const query = typeof record.query === "string" ? record.query.trim() : ""; if (!query) { throw new Error("exa_search requires a non-empty query"); } const type = isExaSearchType(record.type) ? record.type : "auto"; const category = isExaCategory(record.category) ? record.category : undefined; return { query, type, category, startPublishedDate: toIsoDate(record.startPublishedDate, false), endPublishedDate: toIsoDate(record.endPublishedDate, true), includeLinks: normalizeLinkList(record.includeLinks), excludeLinks: normalizeLinkList(record.excludeLinks), }; } export async function exaSearch(params: ExaSearchParams): Promise<ExaSearchExecution> { const apiKey = getExaApiKey(); if (!apiKey) { throw new Error("EXA_API_KEY is not configured on the server"); } const omittedUnsupportedFilters: string[] = []; const restricted = params.category === "company" || params.category === "people"; const includeLinks = params.includeLinks ?? []; const excludeLinks = params.excludeLinks ?? []; const startPublishedDate = params.startPublishedDate; const endPublishedDate = params.endPublishedDate; const baseBody: Record<string, unknown> = { query: params.query, type: params.type, contents: { summary: true, highlights: false, }, }; if (params.category) { baseBody.category = params.category; } if (includeLinks.length > 0) { baseBody.includeDomains = includeLinks; } if (restricted) { if (startPublishedDate) { omittedUnsupportedFilters.push("startPublishedDate"); } if (endPublishedDate) { omittedUnsupportedFilters.push("endPublishedDate"); } if (excludeLinks.length > 0) { omittedUnsupportedFilters.push("excludeLinks"); } } else { if (startPublishedDate) { baseBody.startPublishedDate = startPublishedDate; } if (endPublishedDate) { baseBody.endPublishedDate = endPublishedDate; } if (excludeLinks.length > 0) { baseBody.excludeDomains = excludeLinks; } } let payload: ExaSearchApiResponse | null = null; let effectiveNumResults = REQUESTED_NUM_RESULTS; let lastError: Error | null = null; for (const candidate of NUM_RESULT_FALLBACKS) { try { payload = (await exaFetch( EXA_SEARCH_URL, apiKey, { ...baseBody, numResults: candidate }, SEARCH_TIMEOUT_MS, )) as ExaSearchApiResponse; effectiveNumResults = candidate; lastError = null; break; } catch (cause) { lastError = cause instanceof Error ? cause : new Error("Exa search failed"); const retryable = cause instanceof ExaHttpError && cause.status === 400 && mentionsResultLimit(cause.message); if (!retryable) { throw lastError; } } } if (!payload) { throw lastError ?? new Error("Exa search failed"); } const rows = Array.isArray(payload.results) ? payload.results : []; const hits: ExaSearchHit[] = []; for (const row of rows) { if (typeof row.url !== "string" || !row.url.trim()) { continue; } hits.push({ searchId: typeof row.id === "string" ? row.id : null, url: row.url.trim(), title: typeof row.title === "string" ? row.title : null, author: typeof row.author === "string" ? row.author : null, publishedDate: typeof row.publishedDate === "string" ? row.publishedDate : null, summary: asSummaryText(row.summary), }); } return { hits, applied: { query: params.query, type: params.type, category: params.category ?? null, requestedNumResults: REQUESTED_NUM_RESULTS, effectiveNumResults, summary: true, highlights: false, structuredOutputs: false, startPublishedDate: restricted ? null : startPublishedDate ?? null, endPublishedDate: restricted ? null : endPublishedDate ?? null, includeLinks, excludeLinks: restricted ? [] : excludeLinks, omittedUnsupportedFilters, }, }; } function describeCrawlFailure(status: ExaContentsApiStatus): string | null { if (typeof status.status === "string" && status.status.toLowerCase() === "success") { return null; } const tag = status.error && typeof status.error.tag === "string" ? status.error.tag : null; const code = status.error && typeof status.error.httpStatusCode === "number" ? String(status.error.httpStatusCode) : null; if (tag && code) { return `${tag} (${code})`; } return tag ?? code ?? "crawl failed"; } export async function exaCrawlFullPages(urls: string[], maxCharactersPerPage: number): Promise<ExaCrawlHit[]> { const apiKey = getExaApiKey(); if (!apiKey) { throw new Error("EXA_API_KEY is not configured on the server"); } const unique: string[] = []; const seen = new Set<string>(); for (const url of urls) { const trimmed = url.trim(); if (!trimmed || seen.has(trimmed)) { continue; } seen.add(trimmed); unique.push(trimmed); } if (unique.length === 0) { return []; } const textByUrl = new Map<string, string>(); const failureByUrl = new Map<string, string>(); for (let offset = 0; offset < unique.length; offset += CONTENTS_BATCH_SIZE) { const batch = unique.slice(offset, offset + CONTENTS_BATCH_SIZE); const payload = (await exaFetch( EXA_CONTENTS_URL, apiKey, { urls: batch, text: { maxCharacters: maxCharactersPerPage }, }, CONTENTS_TIMEOUT_MS, )) as ExaContentsApiResponse; for (const row of payload.results ?? []) { if (typeof row.url !== "string" || !row.url.trim()) { continue; } textByUrl.set(row.url.trim(), typeof row.text === "string" ? row.text : ""); } for (const status of payload.statuses ?? []) { if (typeof status.id !== "string") { continue; } const failure = describeCrawlFailure(status); if (failure) { failureByUrl.set(status.id.trim(), failure); } } } return unique.map((url) => { const text = textByUrl.get(url) ?? ""; const failure = failureByUrl.get(url) ?? (text ? null : "no text returned"); return { url, text, failure }; }); } Transpile the provided ts file to standalone, idiomatic Nim, omitting all comments. Output only the full, runnable Nim source code.

Drag to resize