The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Update debug-partner-search.ts
Rafi682bd6b676ba
diff --git a/apps/web/scripts/dev/debug-partner-search.ts b/apps/web/scripts/dev/debug-partner-search.tsindex 495850fb10..a619c8f640 100644--- a/apps/web/scripts/dev/debug-partner-search.ts+++ b/apps/web/scripts/dev/debug-partner-search.ts@@ -1,7 +1,6 @@import {getPartnerSearchableValues,getPartnerSearchProvider,- getPartnerSearchProviderName,normalizePartnerSearchQuery,PARTNER_SEARCH_CANDIDATE_LIMIT,partnerSearchDocumentSelect,@@ -12,6 +11,7 @@ import {import { prisma } from "@/lib/prisma";import { parsePositiveInteger } from "@/scripts/utils/parse-cli-number";import { ProgramEnrollmentStatus } from "@prisma/client";+import { Turbopuffer } from "@turbopuffer/turbopuffer";import "dotenv-flow/config";const DEFAULT_LIMIT = 10;@@ -22,6 +22,7 @@ interface DebugArguments {query: string;limit: number;status?: ProgramEnrollmentStatus;+ searchOnly: boolean;}function parseArguments(args: string[]): DebugArguments {@@ -29,6 +30,7 @@ function parseArguments(args: string[]): DebugArguments {let query: string | undefined;let limit = DEFAULT_LIMIT;let status: ProgramEnrollmentStatus | undefined;+ let searchOnly = false;for (const arg of args) {if (arg.startsWith("--programId=")) {@@ -45,6 +47,8 @@ function parseArguments(args: string[]): DebugArguments {);}status = value;+ } else if (arg === "--searchOnly") {+ searchOnly = true;} else {throw new Error(`Unknown argument: ${arg}`);}@@ -59,8 +63,12 @@ function parseArguments(args: string[]): DebugArguments {if (limit > MAX_LIMIT) {throw new Error(`--limit cannot exceed ${MAX_LIMIT}.`);}+ // Status lives on the enrollment row, so filtering by it needs the database.+ if (searchOnly && status) {+ throw new Error("--searchOnly cannot be combined with --status.");+ }- return { programId, query, limit, status };+ return { programId, query, limit, status, searchOnly };}async function getDatabaseDocuments(@@ -95,6 +103,138 @@ function containsLiteralQuery(: false;}+const NAMESPACE = "partner-search-v2";+const EMAIL_PATTERN = /^[^@\s]+@[^@\s]+\.[^@\s]+$/;++/**+ * Reads the indexed `searchText` back out of turbopuffer for the hits.+ *+ * Debug-only: `searchCandidates` asks for no attributes, because the API only+ * ever needs IDs. This exists so --searchOnly can show what was indexed when the+ * rows are not in the local database — a production index against a seeded one.+ */+async function fetchIndexedText(ids: string[]): Promise<Map<string, string>> {+ const apiKey = process.env.TURBOPUFFER_API_KEY;++ if (!apiKey || ids.length === 0) {+ return new Map();+ }++ const namespace = new Turbopuffer({+ apiKey,+ region: "aws-us-east-1",+ }).namespace(NAMESPACE);+ const { rows } = await namespace.query({+ rank_by: ["id", "asc"],+ top_k: ids.length,+ filters: ["id", "In", ids],+ include_attributes: ["searchText"],+ });++ return new Map(+ (rows ?? []).map((row) => [String(row.id), String(row.searchText ?? "")]),+ );+}++const PLATFORM_TYPES = [+ "website",+ "youtube",+ "twitter",+ "linkedin",+ "instagram",+ "tiktok",+];++/**+ * `searchText` is every searchable value lowercased and space-joined in a fixed+ * order: partner ID, name, email, company, description, platform types, handles,+ * then links. Three of those boundaries are recoverable — the ID is prefixed,+ * the email is address-shaped, and the platform types are a known enum — so the+ * blob splits into name / profile / platforms-and-links. Company and description+ * cannot be told apart once joined, so they stay together as `profile`.+ */+function parseIndexedText(searchText: string) {+ const tokens = searchText.split(" ").filter(Boolean);+ const hasPartnerId = tokens[0]?.startsWith("pn_") ?? false;+ const emailIndex = tokens.findIndex((token) => EMAIL_PATTERN.test(token));+ const platformIndex = tokens.findIndex(+ (token, index) => index > emailIndex && PLATFORM_TYPES.includes(token),+ );++ return {+ tokens,+ name: tokens+ .slice(hasPartnerId ? 1 : 0, emailIndex === -1 ? 4 : emailIndex)+ .join(" "),+ email: emailIndex === -1 ? "" : tokens[emailIndex],+ profile: tokens+ .slice(+ emailIndex + 1,+ platformIndex === -1 ? emailIndex + 1 : platformIndex,+ )+ .join(" "),+ platformsAndLinks:+ platformIndex === -1 ? "" : tokens.slice(platformIndex).join(" "),+ };+}++/**+ * How often each query term occurs, and how long the document is. Both drive the+ * BM25 score: term frequency raises it, and length normalization (b=0.75) pushes+ * it back down, so a partner with many links can rank below a shorter document+ * that matched fewer of the query's terms.+ */+function summarizeTermMatches(tokens: string[], normalizedQuery: string) {+ const queryTerms = normalizedQuery.split(/\s+/u).filter(Boolean);++ return queryTerms+ .map((term) => {+ const count = tokens.filter((token) => token.startsWith(term)).length;+ return `${term}×${count}`;+ })+ .join(" ");+}++/**+ * Everything the provider returns on its own: an ID and a score. Names, emails,+ * and the literal-match check all come from the database, so --searchOnly trades+ * them for a run that needs no DATABASE_URL.+ */+function reportProviderHits(+ hits: PartnerSearchHit[],+ indexedText: Map<string, string>,+ normalizedQuery: string,+) {+ console.table(+ hits.map((hit, index) => {+ const parsed = parseIndexedText(indexedText.get(hit.id) ?? "");++ return {+ rank: index + 1,+ score: hit.score?.toFixed(5),+ name: parsed.name,+ email: parsed.email,+ docTokens: parsed.tokens.length,+ queryTerms: summarizeTermMatches(parsed.tokens, normalizedQuery),+ };+ }),+ );++ if (indexedText.size === 0) {+ return;+ }++ console.log("\nIndexed document per hit");+ hits.forEach((hit, index) => {+ const parsed = parseIndexedText(indexedText.get(hit.id) ?? "");+ console.log(`\n${index + 1}. ${hit.id}`);+ console.log(` profile ${parsed.profile.slice(0, 150) || "—"}`);+ console.log(+ ` platforms ${parsed.platformsAndLinks.slice(0, 150) || "—"}`,+ );+ });+}+function reportResults({label,hits,@@ -129,15 +269,14 @@ function reportResults({}async function main() {- const { programId, query, limit, status } = parseArguments(+ const { programId, query, limit, status, searchOnly } = parseArguments(process.argv.slice(2),);const searchProvider = getPartnerSearchProvider();if (!searchProvider) {- throw new Error("PARTNER_SEARCH_PROVIDER is not configured.");+ throw new Error("TURBOPUFFER_API_KEY is not configured.");}- const providerName = getPartnerSearchProviderName();const startedAt = performance.now();// Step 1: Fetch the same relevance candidates used by the website@@ -147,6 +286,28 @@ async function main() {limit: PARTNER_SEARCH_CANDIDATE_LIMIT,});+ const normalizedQuery = normalizePartnerSearchQuery(query);++ if (searchOnly) {+ console.log("Partner search debug summary");+ console.table({+ programId,+ query,+ normalizedQuery,+ candidates: relevanceResult.hits.length,+ elapsedMs: (performance.now() - startedAt).toFixed(1),+ });++ const pageHits = relevanceResult.hits.slice(0, limit);+ const indexedText = await fetchIndexedText(pageHits.map(({ id }) => id));++ console.log(+ "\nProvider relevance order (provider only, no database reads)",+ );+ reportProviderHits(pageHits, indexedText, normalizedQuery);+ return;+ }+// Step 2: Apply the requested status while loading canonical database documentsconst databaseDocuments = await getDatabaseDocuments(relevanceResult.hits,@@ -159,10 +320,9 @@ async function main() {console.log("Partner search debug summary");console.table({- provider: providerName,programId,query,- normalizedQuery: normalizePartnerSearchQuery(query),+ normalizedQuery,status: status ?? "all",candidates: relevanceResult.hits.length,databaseMatches: databaseMatchedHits.length,@@ -176,7 +336,7 @@ async function main() {label: "Provider relevance order",hits: filteredHits,databaseDocuments,- normalizedQuery: normalizePartnerSearchQuery(query),+ normalizedQuery,});}