Open source alternative to Semrush and Ahrefs
TypeScript backlink-analysisgoogle-search-console-mcpkeyword-researchmcpseoseo-agentseo-auditseo-automationseo-skillsseo-toolssite-audit
Keep AI prompt research working when SERP is down (EVE-410) (#901)
Carter Smithf82044483beb
diff --git a/src/server/features/ai-visibility/services/aiPromptResearch.test.ts b/src/server/features/ai-visibility/services/aiPromptResearch.test.tsindex 56a585a7..db8e3370 100644--- a/src/server/features/ai-visibility/services/aiPromptResearch.test.ts+++ b/src/server/features/ai-visibility/services/aiPromptResearch.test.ts@@ -39,6 +39,7 @@ vi.mock("../repositories/AiVisibilityRepository", () => ({AiVisibilityRepository: { listResearchKeywords },}));+import { AppError } from "@/server/lib/errors";import { listAiResearchKeywords, researchAiPrompts } from "./aiPromptResearch";import { customer } from "./aiVisibilityTestFixtures";@@ -118,6 +119,18 @@ describe("researchAiPrompts", () => {]);});+ it("still returns prompts that ask the keyword when the SERP is down", async () => {+ serpLive.mockRejectedValue(new AppError("UPSTREAM_UNAVAILABLE"));++ const result = await researchAiPrompts(input, customer);++ // Only the SERP put ahrefs.com on topic, so that prompt drops out.+ expect(result.prompts.map((prompt) => prompt.text)).toEqual([+ "What is the most accurate SEO tool in 2026?",+ "Is moz a good seo tool?",+ ]);+ });+it("refuses research without the paid plan before buying any data", async () => {assertPaidPlan.mockRejectedValue(new Error("PAYMENT_REQUIRED"));diff --git a/src/server/features/ai-visibility/services/aiPromptResearch.ts b/src/server/features/ai-visibility/services/aiPromptResearch.tsindex 48627813..9f398588 100644--- a/src/server/features/ai-visibility/services/aiPromptResearch.ts+++ b/src/server/features/ai-visibility/services/aiPromptResearch.ts@@ -9,6 +9,7 @@ import {CHATGPT_LOCATION_CODE,createDataforseoClient,} from "@/server/lib/dataforseo";+import { AppError } from "@/server/lib/errors";import { buildCacheKey, getCached, setCached } from "@/server/lib/r2-cache";import { normalizeAiSuggestion } from "@/shared/ai-prompt-suggestions";import {@@ -23,6 +24,8 @@ import { getTracker } from "./aiVisibilityState";/** The provider refreshes prompt data monthly; one paid call per day is enough. */const RESEARCH_TTL_SECONDS = 24 * 60 * 60;+/** Research filtered without the SERP is weaker; buy the full set again soon. */+const RESEARCH_WITHOUT_SERP_TTL_SECONDS = 60 * 60;const RESEARCH_LIMIT = 100;// Results for niche keywords shift between calls; at 20 the on-topic sites// left after generic ones could swing from a dozen to none. 50 holds steady.@@ -183,12 +186,24 @@ async function loadPrompts(limit: RESEARCH_LIMIT,orderBy: ["ai_search_volume,desc"],}),- client.serp.live({- keyword: request.topic,- locationCode: request.locationCode,- languageCode: request.languageCode,- depth: RESEARCH_SERP_DEPTH,- }),+ // The SERP only sharpens the on-topic filter. When DataForSEO's search is+ // down, keep the paid prompt data and filter by phrase and brand domains.+ client.serp+ .live({+ keyword: request.topic,+ locationCode: request.locationCode,+ languageCode: request.languageCode,+ depth: RESEARCH_SERP_DEPTH,+ })+ .catch((error: unknown) => {+ if (+ error instanceof AppError &&+ error.code === "UPSTREAM_UNAVAILABLE"+ ) {+ return null;+ }+ throw error;+ }),]);const prompts = data.flatMap((item): CachedPrompt[] =>item.question?.trim()@@ -224,14 +239,18 @@ async function loadPrompts(prompts,serpDomains: [...new Set(- serp.flatMap((item) =>+ (serp ?? []).flatMap((item) =>item.type === "organic" ? (registrableDomain(item.domain) ?? []) : [],),),].filter((domain) => !GENERIC_DOMAINS.has(domain)),};waitUntil(- setCached(cacheKey, research, RESEARCH_TTL_SECONDS).catch((error) => {+ setCached(+ cacheKey,+ research,+ serp ? RESEARCH_TTL_SECONDS : RESEARCH_WITHOUT_SERP_TTL_SECONDS,+ ).catch((error) => {console.error("ai-visibility.prompt-research.cache-write-failed", error);}),);