Repository Public
1// SP Search orchestration: validation → cache → daily budget → provider batches → pages.2import type { Config } from "../config.ts";3import type { DB } from "../db.ts";4import { HttpError, OpsLog, utcDay } from "../util.ts";5import { cacheKey } from "./normalize.ts";6import { tavilyWebBatch } from "./tavily.ts";7import { imageBatch } from "./images.ts";8import { videoBatch, videoProviderName } from "./video.ts";9import { ProviderError, type Batch, type SearchRequest, type SearchResponse } from "./types.ts";1011const PAGE_SIZE = { web: 10, images: 40, videos: 20 } as const;12const TTL_MS = { web: 20 * 60_000, images: 6 * 3600_000, videos: 3 * 3600_000 } as const;13const MAX_BATCHES = 5;14const MAX_CACHE_ENTRIES = 600;1516/** Price of one Tavily credit on the pay-as-you-go plan (USD), used only for the cost estimate shown to the admin. */17export const TAVILY_USD_PER_CREDIT = 0.008;1819export class SearchService {20 private cache = new Map<string, { at: number; batch: Batch<any> }>();21 private inflight = new Map<string, Promise<Batch<any>>>();22 private config: Config;23 private db: DB;24 private log: OpsLog;2526 constructor(config: Config, db: DB, log: OpsLog) {27 this.config = config;28 this.db = db;29 this.log = log;30 }3132 creditsToday(provider = "tavily"): number {33 const row = this.db.prepare("SELECT COALESCE(SUM(credits),0) AS c FROM usage_daily WHERE day = ? AND provider = ?").get(utcDay(), provider) as { c: number };34 return row.c;35 }3637 private record(provider: string, kind: string, fields: { requests?: number; credits?: number; cacheHits?: number; errors?: number; latency?: number }): void {38 this.db.prepare(`39 INSERT INTO usage_daily(day, provider, kind, requests, credits, cache_hits, errors, latency_ms_total) VALUES(?,?,?,?,?,?,?,?)40 ON CONFLICT(day, provider, kind) DO UPDATE SET requests = requests + excluded.requests, credits = credits + excluded.credits,41 cache_hits = cache_hits + excluded.cache_hits, errors = errors + excluded.errors, latency_ms_total = latency_ms_total + excluded.latency_ms_total42 `).run(utcDay(), provider, kind, fields.requests ?? 0, fields.credits ?? 0, fields.cacheHits ?? 0, fields.errors ?? 0, Math.round(fields.latency ?? 0));43 }4445 private async batch(request: SearchRequest, index: number, previous: Batch<any>[]): Promise<{ batch: Batch<any>; cached: boolean }> {46 const key = cacheKey(request, index);47 const provider = request.type === "videos" ? videoProviderName(this.config, request) : "tavily";48 const hit = this.cache.get(key);49 if (hit && Date.now() - hit.at < TTL_MS[request.type]) {50 this.cache.delete(key); this.cache.set(key, hit); // LRU touch51 this.record(provider, request.type, { cacheHits: 1 });52 return { batch: hit.batch, cached: true };53 }54 // Identical concurrent requests share one provider call.55 const pending = this.inflight.get(key);56 if (pending) return { batch: await pending, cached: true };57 if (provider === "tavily" && this.creditsToday("tavily") >= this.config.dailyCreditBudget) {58 this.log.add("warn", "search", "daily credit budget reached; request refused");59 throw new HttpError(503, "budget_exhausted", "The daily search budget is used up. Try again tomorrow.");60 }61 const started = Date.now();62 const excluded = [...new Set(previous.flatMap((b) => b.domains))];63 const task: Promise<Batch<any>> =64 request.type === "web" ? tavilyWebBatch(this.config, request, excluded, request.summary && index === 0)65 : request.type === "images" ? imageBatch(this.config, request, excluded, index)66 : videoBatch(this.config, request, previous);67 this.inflight.set(key, task);68 try {69 const batch = await task;70 this.record(batch.provider.startsWith("tavily") ? "tavily" : batch.provider, request.type, { requests: 1, credits: batch.credits, latency: Date.now() - started });71 if (batch.volatile) return { batch, cached: false };72 this.cache.set(key, { at: Date.now(), batch });73 if (this.cache.size > MAX_CACHE_ENTRIES) this.cache.delete(this.cache.keys().next().value!);74 return { batch, cached: false };75 } catch (error) {76 this.record(provider, request.type, { errors: 1, latency: Date.now() - started });77 if (error instanceof ProviderError) {78 this.log.add(error.status >= 500 ? "error" : "warn", "search", `${request.type} provider failure: ${error.code}`);79 throw new HttpError(error.status, error.code, error.message, error.retryAfter ? { retryAfter: error.retryAfter } : undefined);80 }81 this.log.add("error", "search", `${request.type} unexpected failure`);82 throw new HttpError(502, "provider_error", "The search provider failed");83 } finally {84 this.inflight.delete(key);85 }86 }8788 async search(request: SearchRequest): Promise<SearchResponse> {89 const started = Date.now();90 const size = PAGE_SIZE[request.type];91 const from = (request.page - 1) * size, to = from + size;92 const batches: Batch<any>[] = [];93 let allCached = true;94 let count = 0;95 while (count < to && batches.length < MAX_BATCHES) {96 if (batches.length && batches[batches.length - 1].exhausted) break;97 const { batch, cached } = await this.batch(request, batches.length, batches);98 allCached &&= cached;99 batches.push(batch);100 count += batch.items.length;101 // A batch that adds nothing new means the provider has no more to give.102 if (batch.items.length === 0) break;103 }104 // Batches are deduplicated against each other (the same page can come back under another domain filter).105 const seen = new Set<string>();106 const all = batches.flatMap((b) => b.items).filter((item) => (seen.has(item.id) ? false : (seen.add(item.id), true)));107 const last = batches[batches.length - 1];108 const canFetchMore = !!last && !last.exhausted && last.items.length > 0 && batches.length < MAX_BATCHES;109 return {110 query: request.query, type: request.type, page: request.page, pageSize: size,111 hasMore: all.length > to || (canFetchMore && request.page < 10),112 provider: batches[0]?.provider ?? "tavily", cached: allCached, tookMs: Date.now() - started,113 answer: request.type === "web" && request.page === 1 && batches[0]?.answer ? { text: batches[0].answer, generatedBy: "tavily" } : null,114 results: all.slice(from, to),115 notices: [...new Set(batches.flatMap((b) => b.notices))],116 };117 }118119 cacheStats() {120 return { entries: this.cache.size, maxEntries: MAX_CACHE_ENTRIES };121 }122}123