import { NextResponse } from "next/server"; import { webSearch } from "yt-dlp-transcript-common/lib/web/search"; import { ops, optPositiveInt, reqString } from "../_lib"; import { webFailure } from "../_web"; export const dynamic = "force-dynamic"; // POST { query, n? } -> { ok: true, query, results: [{ title, url, snippet }] } // // A web search (DuckDuckGo's HTML page, release 27 D7), read only, through // the process's web pacer (common/lib/web): one request at a time, 3 s apart, // backing off on a 429 or 503. `n` results (default 6, at most 20). Nothing // is written. A generation run's agent tool `web_search` is the same read. export async function POST(request: Request) { return ops(request, ["query", "n"], async (body) => { const query = reqString(body, "query"); const n = optPositiveInt(body, "n"); try { const results = await webSearch(query, n !== undefined ? { n } : {}); return NextResponse.json({ ok: true, query, results }); } catch (e) { return webFailure(e); } }); }