Open source alternative to Semrush and Ahrefs
TypeScript backlink-analysisgoogle-search-console-mcpkeyword-researchmcpseoseo-agentseo-auditseo-automationseo-skillsseo-toolssite-audit
Retry website research search outages and skip competitors when search stays down (#864)
Carter Smith7955e7a67a74
diff --git a/plugins/openseo/.claude-plugin/plugin.json b/plugins/openseo/.claude-plugin/plugin.jsonindex 4fa98dcc..e77212b7 100644--- a/plugins/openseo/.claude-plugin/plugin.json+++ b/plugins/openseo/.claude-plugin/plugin.json@@ -1,7 +1,7 @@{"name": "openseo","displayName": "OpenSEO",- "version": "1.0.19",+ "version": "1.0.20","description": "Research keywords and competitors, audit sites, track rankings and AI visibility, and use connected Search Console data from your AI assistant.","author": {"name": "OpenSEO",diff --git a/plugins/openseo/.codex-plugin/plugin.json b/plugins/openseo/.codex-plugin/plugin.jsonindex 9ba1d8fa..040c7af9 100644--- a/plugins/openseo/.codex-plugin/plugin.json+++ b/plugins/openseo/.codex-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "openseo",- "version": "1.0.19",+ "version": "1.0.20","description": "Research keywords and competitors, audit sites, track rankings and AI visibility, and use connected Search Console data from your AI assistant.","author": {"name": "OpenSEO",diff --git a/plugins/openseo/.cursor-plugin/plugin.json b/plugins/openseo/.cursor-plugin/plugin.jsonindex e3ff1e32..b8b6ba44 100644--- a/plugins/openseo/.cursor-plugin/plugin.json+++ b/plugins/openseo/.cursor-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "openseo",- "version": "1.0.19",+ "version": "1.0.20","description": "Research keywords and competitors, audit sites, track rankings and AI visibility, and use connected Search Console data from your AI assistant.","author": {"name": "Every App, Inc",diff --git a/src/server/features/projects/services/websiteResearch.test.ts b/src/server/features/projects/services/websiteResearch.test.tsindex 57eab959..c9f16219 100644--- a/src/server/features/projects/services/websiteResearch.test.ts+++ b/src/server/features/projects/services/websiteResearch.test.ts@@ -44,6 +44,7 @@ vi.mock();import { researchWebsite } from "./websiteResearch";+import { AppError } from "@/server/lib/errors";import { customer } from "@/server/features/ai-visibility/services/aiVisibilityTestFixtures";const project = {@@ -176,4 +177,35 @@ describe("researchWebsite", () => {expect(result.suggestedKeywords).toEqual(valid.slice(0, 15));});++ it.each([+ { failures: 1, competitors: [competitor] },+ { failures: 4, competitors: [] },+ ])(+ "retries search outages and skips competitors when $failures attempts fail",+ async ({ failures, competitors }) => {+ vi.useFakeTimers();+ listCompetitors.mockResolvedValue([]);+ for (let n = 0; n < failures; n++)+ search.mockRejectedValueOnce(new AppError("UPSTREAM_UNAVAILABLE"));++ const research = researchWebsite("https://saved.com", project, customer);+ await vi.runAllTimersAsync();+ const result = await research;+ vi.useRealTimers();++ expect(search).toHaveBeenCalledTimes(Math.min(failures + 1, 4));+ expect(result.competitors).toEqual(competitors);+ },+ );++ it("fails instead of skipping competitors when search fails for a non-outage reason", async () => {+ listCompetitors.mockResolvedValue([]);+ search.mockRejectedValue(new AppError("DATAFORSEO_AUTH_FAILED"));++ await expect(+ researchWebsite("https://saved.com", project, customer),+ ).rejects.toThrow("Website research was incomplete");+ expect(search).toHaveBeenCalledOnce();+ });});diff --git a/src/server/features/projects/services/websiteResearch.ts b/src/server/features/projects/services/websiteResearch.tsindex e06e2165..403ed41c 100644--- a/src/server/features/projects/services/websiteResearch.ts+++ b/src/server/features/projects/services/websiteResearch.ts@@ -25,6 +25,14 @@ import { normalizeAiSuggestion } from "@/shared/ai-prompt-suggestions";import { ProjectContextRepository } from "@/server/features/project-context/repositories/ProjectContextRepository";import { AiVisibilityRepository } from "@/server/features/ai-visibility/repositories/AiVisibilityRepository";+const SEARCH_RETRIES = 3;++// DataForSEO's own search backend failing (e.g. 40101), not a problem with our+// request or account.+function isSearchOutage(error: unknown): error is AppError {+ return error instanceof AppError && error.code === "UPSTREAM_UNAVAILABLE";+}+/** Internal research for callers that have already checked admission credits. */export async function researchWebsite(website: string,@@ -79,6 +87,7 @@ export async function researchWebsite(const sources = new Set<string>();let searched = false;let searchSucceeded = false;+ let searchOutage = false;let sitesRead = 0;const outputSchema = z.object({...(missingOverview@@ -171,21 +180,34 @@ export async function researchWebsite(// Searches belong to the already-admitted research task; collect// actual cost instead of reserving credits and interrupting it.let response;- try {- response = await fetchLiveSerp({- keyword,- ...market,- depth: 10,- });- } catch (error) {- if (error instanceof DataforseoChargedTaskError)- spend.push({- provider: "dataforseo",- creditFeature: "keyword_research",- operation: "project_website_research",- costUsd: error.billing.costUsd,+ for (let attempt = 0; ; attempt++) {+ try {+ response = await fetchLiveSerp({+ keyword,+ ...market,+ depth: 10,});- throw error;+ break;+ } catch (error) {+ if (error instanceof DataforseoChargedTaskError)+ spend.push({+ provider: "dataforseo",+ creditFeature: "keyword_research",+ operation: "project_website_research",+ costUsd: error.billing.costUsd,+ });+ // DataForSEO's search engine errors (40101) usually clear+ // within seconds. Timeouts already used their whole budget.+ if (+ attempt === SEARCH_RETRIES ||+ !isSearchOutage(error) ||+ error.name === "DataForSEOTimeoutError"+ )+ throw error;+ }+ await new Promise((resolve) =>+ setTimeout(resolve, 1000 * 2 ** attempt),+ );}spend.push({provider: "dataforseo",@@ -208,6 +230,16 @@ export async function researchWebsite(searchSucceeded = results.some((searchResult) => searchResult.status === "fulfilled",);+ searchOutage = results.every(+ (searchResult) =>+ searchResult.status === "rejected" &&+ isSearchOutage(searchResult.reason),+ );+ if (searchOutage)+ return {+ error:+ "Live search is unavailable. Skip competitor research and return no competitors.",+ };return results.map((searchResult, index) =>searchResult.status === "fulfilled"? searchResult.value@@ -262,6 +294,9 @@ export async function researchWebsite(const research = websiteResearchSchema.parse({...preserved,...output,+ // Without live search, competitors can't be verified. The review starts+ // with none so the user can add them instead of losing a paid run.+ ...(searchOutage ? { competitors: [] } : {}),suggestedKeywords: suggestedKeywords.filter((keyword) =>@@ -285,7 +320,7 @@ export async function researchWebsite("INTERNAL_ERROR","Website research returned duplicate tracking suggestions. Please try again.",);- if (missingCompetitors && !searchSucceeded)+ if (missingCompetitors && !searchSucceeded && !searchOutage)throw new AppError("INTERNAL_ERROR","Website research was incomplete. Please try again.",diff --git a/src/server/mcp/tools/project-website.ts b/src/server/mcp/tools/project-website.tsindex 70cf8871..c6e04a35 100644--- a/src/server/mcp/tools/project-website.ts+++ b/src/server/mcp/tools/project-website.ts@@ -17,7 +17,7 @@ export const researchProjectWebsiteTool = {config: {title: "Research project website",description:- "Fill missing project basics, business competitors, and Prompt Research topics using saved context and verified website pages. Each field is independent: reuse existing name, domain, overview, and competitors; search for competitors only when none are saved. Requires a positive usage-credit balance to start; completes research before billing actual model and search costs, even if that makes the balance negative. Newly researched competitors have evidence links; preserveCompetitors=true means the existing list is reused and competitors is empty. Returns five suggested topics with five prompts each, plus up to fifteen suggested keywords, only when research keywords are missing. Results are drafts, not saved or tracked. Review newly researched competitors before save_project_website_setup.",+ "Fill missing project basics, business competitors, and Prompt Research topics using saved context and verified website pages. Each field is independent: reuse existing name, domain, overview, and competitors; search for competitors only when none are saved. Requires a positive usage-credit balance to start; completes research before billing actual model and search costs, even if that makes the balance negative. Newly researched competitors have evidence links, and the list is empty if live search was unavailable; preserveCompetitors=true means the existing list is reused and competitors is empty. Returns five suggested topics with five prompts each, plus up to fifteen suggested keywords, only when research keywords are missing. Results are drafts, not saved or tracked. Review newly researched competitors before save_project_website_setup.",inputSchema: researchProjectWebsiteSchema.shape,outputSchema: z.looseObject({name: z.string(),