Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api/index): port queryIndexAtSplitLevel to RPC (FIR-2241) (#1640)

Gergő Móriczf050b169e239
diff --git a/apps/api/src/services/index.ts b/apps/api/src/services/index.ts
index df7969dea..32a49c6a9 100644
--- a/apps/api/src/services/index.ts
+++ b/apps/api/src/services/index.ts
@@ -225,17 +225,41 @@ export async function queryIndexAtSplitLevel(url: string, limit: number): Promis
const urlSplitsHash = generateURLSplits(urlObj.href).map(x => hashURL(x));
- const { data, error } = await index_supabase_service
- .from("index")
- .select("resolved_url")
- .eq("url_split_" + (urlSplitsHash.length - 1) + "_hash", urlSplitsHash[urlSplitsHash.length - 1])
- .gte("created_at", new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString())
- .limit(limit)
-
- if (error) {
- logger.warn("Error querying index", { error, url, limit });
- return [];
- }
+ const level = urlSplitsHash.length - 1;
+
+ let links: Set<string> = new Set();
+ let iteration = 0;
+
+ while (true) {
+ // Query the index for the next set of links
+ const { data: _data, error } = await index_supabase_service
+ .rpc("query_index_at_split_level", {
+ i_level: level,
+ i_url_hash: urlSplitsHash[level],
+ i_newer_than: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString(),
+ })
+ .range(iteration * 1000, (iteration + 1) * 1000)
+
+ // If there's an error, return the links we have
+ if (error) {
+ logger.warn("Error querying index", { error, url, limit });
+ return [...links].slice(0, limit);
+ }
- return [...new Set((data ?? []).map((x) => x.resolved_url))];
+ // Add the links to the set
+ const data = _data ?? [];
+ data.forEach((x) => links.add(x.resolved_url));
+
+ // If we have enough links, return them
+ if (links.size >= limit) {
+ return [...links].slice(0, limit);
+ }
+
+ // If we get less than 1000 links from the query, we're done
+ if (data.length < 1000) {
+ return [...links].slice(0, limit);
+ }
+
+ iteration++;
+ }
}
diff --git a/apps/api/src/services/queue-worker.ts b/apps/api/src/services/queue-worker.ts
index 5545ed9ae..4f178cbf0 100644
--- a/apps/api/src/services/queue-worker.ts
+++ b/apps/api/src/services/queue-worker.ts
@@ -930,12 +930,12 @@ async function kickoffGetIndexLinks(sc: StoredCrawl, crawler: WebCrawler, url: s
const index = await queryIndexAtSplitLevel(
sc.crawlerOptions.allowBackwardCrawling ? generateURLSplits(trimmedURL.href)[0] : trimmedURL.href,
- sc.crawlerOptions.limit ?? 100,
+ sc.crawlerOptions.limit ?? 10000,
);
const validIndexLinks = crawler.filterLinks(
index.filter(x => crawler.filterURL(x, trimmedURL.href) !== null),
- sc.crawlerOptions.limit ?? 100,
+ sc.crawlerOptions.limit ?? 10000,
sc.crawlerOptions.maxDepth ?? 10,
false,
);