Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(content): keep open opportunities when Generate is pressed (#870)
Gökhan Geyik178971bf1f3c
diff --git a/server/src/lib/open-opportunities.js b/server/src/lib/open-opportunities.jsnew file mode 100644index 000000000..5ea21b011--- /dev/null+++ b/server/src/lib/open-opportunities.js@@ -0,0 +1,24 @@+import supabaseAdmin from '../config/supabase.js';++const PAGE = 1000;++/**+ * A brand's open (status 'new') opportunities, as { prompt_id, title }. Paged:+ * a large brand holds more than PostgREST's 1,000-row default, and a short+ * read would undercount the per-prompt cap.+ */+export async function loadOpenOpportunities(brandId) {+ const rows = [];+ for (let from = 0; ; from += PAGE) {+ const { data, error } = await supabaseAdmin+ .from('content_opportunities')+ .select('prompt_id, title')+ .eq('brand_id', brandId)+ .eq('status', 'new')+ .order('id')+ .range(from, from + PAGE - 1);+ if (error) throw new Error(error.message);+ rows.push(...(data || []));+ if (!data || data.length < PAGE) return rows;+ }+}diff --git a/server/src/lib/opportunity-generator.js b/server/src/lib/opportunity-generator.jsindex 4f935591a..995f74795 100644--- a/server/src/lib/opportunity-generator.js+++ b/server/src/lib/opportunity-generator.js@@ -11,12 +11,17 @@ import supabaseAdmin from '../config/supabase.js';import { selectInChunks } from './chunked-in.js';import { logger } from './logger.js';import {+ ALREADY_SUGGESTED_RULE,OPPORTUNITIES_PER_RUN,OPPORTUNITY_COUNT_RULE,+ alreadySuggested,belowOpenCap,openCountsByPrompt,+ openTitlesByPrompt,+ opportunityKey,relatedCandidate,} from './opportunity-limits.js';+import { loadOpenOpportunities } from './open-opportunities.js';const opportunitySchema = z.object({opportunities: z@@ -48,7 +53,7 @@ Rules:- Focus on content that will improve the brand's visibility in AI-generated answers.- Categorize as "owned" or "earned".${OPPORTUNITY_COUNT_RULE}-- Some prompts list opportunities "Already suggested" for them. Do not repeat or reword those: suggest a genuinely different piece of content for that prompt, or choose another prompt.+${ALREADY_SUGGESTED_RULE}- The bracketed [N] indexes in the prompt data exist ONLY for the relatedPromptIndex field. NEVER mention an index like "[0]" or "Prompt 3" in titles or descriptions — refer to the prompt by quoting or paraphrasing its actual text/topic instead.`;function computeScore(volume, visibility, competitorGap, intent) {@@ -134,27 +139,9 @@ export async function generateContentOpportunities(brandId) {// Open opportunities, read before choosing candidates: prompts already at// the cap are skipped, and the rest show the model what they already hold.- // Paged — a large brand holds more than PostgREST's 1,000-row default.- const existing = [];- for (let from = 0; ; from += 1000) {- const { data, error } = await supabaseAdmin- .from('content_opportunities')- .select('prompt_id, title')- .eq('brand_id', brandId)- .eq('status', 'new')- .order('id')- .range(from, from + 999);- if (error) throw new Error(error.message);- existing.push(...(data || []));- if (!data || data.length < 1000) break;- }+ const existing = await loadOpenOpportunities(brandId);const openCounts = openCountsByPrompt(existing);- const openTitles = new Map();- for (const o of existing) {- if (!o.prompt_id || !o.title) continue;- if (!openTitles.has(o.prompt_id)) openTitles.set(o.prompt_id, []);- openTitles.get(o.prompt_id).push(o.title);- }+ const openTitles = openTitlesByPrompt(existing);const ranked = testedPrompts.map((p) => {@@ -205,13 +192,8 @@ export async function generateContentOpportunities(brandId) {return;}- const promptLine = (p, i) => {- const already = openTitles.get(p.promptId) || [];- const suffix = already.length- ? ` | Already suggested: ${already.map((t) => `"${t}"`).join('; ')}`- : '';- return `[${i}] "${p.text}" | Intent: ${p.intent} | AI Vol: ${p.estAiVolume}/mo | Vis: ${p.avgVisibility}% | Gap: ${p.competitorGap}%${suffix}`;- };+ const promptLine = (p, i) =>+ `[${i}] "${p.text}" | Intent: ${p.intent} | AI Vol: ${p.estAiVolume}/mo | Vis: ${p.avgVisibility}% | Gap: ${p.competitorGap}%${alreadySuggested(openTitles.get(p.promptId))}`;const userPrompt = `Brand: ${brand.name}Industry: ${brand.industry || 'Not specified'}@@ -234,18 +216,13 @@ IMPORTANT: Write every opportunity title and description in ${langName}.`;// Exact repeats are still caught here, as the last guard behind the cap and// the already-suggested list.- const seen = new Set(- existing.map(- (opportunity) =>- `${opportunity.prompt_id}::${(opportunity.title || '').toLowerCase().trim()}`,- ),- );+ const seen = new Set(existing.map((o) => opportunityKey(o.prompt_id, o.title)));const rows = object.opportunities.map((opp) => ({ rel: relatedCandidate(scored, opp.relatedPromptIndex), opp })).filter(({ rel, opp }) => {if (!rel) return false;- const key = `${rel.promptId}::${opp.title.toLowerCase().trim()}`;+ const key = opportunityKey(rel.promptId, opp.title);if (seen.has(key)) return false;seen.add(key);return true;diff --git a/server/src/lib/opportunity-limits.js b/server/src/lib/opportunity-limits.jsindex af26a6aec..a9141870e 100644--- a/server/src/lib/opportunity-limits.js+++ b/server/src/lib/opportunity-limits.js@@ -58,3 +58,27 @@ export function relatedCandidate(candidates, index) {? candidates[index]: null;}++/** The instruction that goes with the "Already suggested" lists in the prompt data. */+export const ALREADY_SUGGESTED_RULE = `- Some prompts list opportunities "Already suggested" for them. Do not repeat or reword those: suggest a genuinely different piece of content for that prompt, or choose another prompt.`;++/** Open opportunity titles per prompt id, from rows carrying `prompt_id` and `title`. */+export function openTitlesByPrompt(rows) {+ const titles = new Map();+ for (const r of rows || []) {+ if (!r.prompt_id || !r.title) continue;+ if (!titles.has(r.prompt_id)) titles.set(r.prompt_id, []);+ titles.get(r.prompt_id).push(r.title);+ }+ return titles;+}++/** The prompt-data suffix listing what a prompt already holds, or '' if nothing. */+export function alreadySuggested(titles) {+ return titles?.length ? ` | Already suggested: ${titles.map((t) => `"${t}"`).join('; ')}` : '';+}++/** Duplicate key: the same prompt and the same title, ignoring case and outer spaces. */+export function opportunityKey(promptId, title) {+ return `${promptId}::${(title || '').toLowerCase().trim()}`;+}diff --git a/server/src/lib/opportunity-limits.test.js b/server/src/lib/opportunity-limits.test.jsindex 33fcec398..a1276e413 100644--- a/server/src/lib/opportunity-limits.test.js+++ b/server/src/lib/opportunity-limits.test.js@@ -5,8 +5,11 @@ import {MAX_OPEN_PER_PROMPT,OPPORTUNITIES_PER_RUN,OPPORTUNITY_COUNT_RULE,+ alreadySuggested,belowOpenCap,openCountsByPrompt,+ openTitlesByPrompt,+ opportunityKey,relatedCandidate,} from './opportunity-limits.js';@@ -96,9 +99,35 @@ describe('open-opportunity cap (#837)', () => {expect(relatedCandidate(candidates, undefined)).toBeNull();});- it('the nightly generator applies the cap and shows the model what is already open', () => {- const code = source('./opportunity-generator.js');- expect(code).toContain('belowOpenCap(');- expect(code).toContain('Already suggested');+ it('lists open titles per prompt and renders them for the prompt data', () => {+ const titles = openTitlesByPrompt([+ { prompt_id: 'a', title: 'Guide' },+ { prompt_id: 'a', title: 'Checklist' },+ { prompt_id: 'b', title: null },+ ]);+ expect(titles.get('a')).toEqual(['Guide', 'Checklist']);+ expect(titles.has('b')).toBe(false);+ expect(alreadySuggested(titles.get('a'))).toBe(' | Already suggested: "Guide"; "Checklist"');+ expect(alreadySuggested(undefined)).toBe('');+ });++ it('keys duplicates by prompt and title, ignoring case and outer spaces', () => {+ expect(opportunityKey('p', ' Guide ')).toBe(opportunityKey('p', 'guide'));+ expect(opportunityKey('p', 'Guide')).not.toBe(opportunityKey('q', 'Guide'));+ });++ for (const [label, path] of GENERATORS) {+ it(`the ${label} generator applies the cap and shows the model what is open`, () => {+ const code = source(path);+ expect(code).toContain('loadOpenOpportunities(');+ expect(code).toContain('belowOpenCap(');+ expect(code).toContain('${ALREADY_SUGGESTED_RULE}');+ expect(code).toContain('opportunityKey(');+ });+ }++ it('the Generate button adds to the open list instead of deleting it (#63)', () => {+ const code = source('../workers/content-worker.js');+ expect(code).not.toMatch(/\.delete\(\)/);});});diff --git a/server/src/workers/content-worker.js b/server/src/workers/content-worker.jsindex 664df3e1c..aa5ccf363 100644--- a/server/src/workers/content-worker.js+++ b/server/src/workers/content-worker.js@@ -11,10 +11,17 @@ import supabaseAdmin from '../config/supabase.js';import { selectInChunks } from '../lib/chunked-in.js';import logger from '../lib/logger.js';import {+ ALREADY_SUGGESTED_RULE,OPPORTUNITIES_PER_RUN,OPPORTUNITY_COUNT_RULE,+ alreadySuggested,+ belowOpenCap,+ openCountsByPrompt,+ openTitlesByPrompt,+ opportunityKey,relatedCandidate,} from '../lib/opportunity-limits.js';+import { loadOpenOpportunities } from '../lib/open-opportunities.js';const opportunitySchema = z.object({opportunities: z@@ -57,6 +64,7 @@ Rules:- Set impact based on: volume × (100 - current visibility) × competitor gap.${OPPORTUNITY_COUNT_RULE}- Do NOT repeat the same recommendation in different wording.+${ALREADY_SUGGESTED_RULE}- The bracketed [N] indexes in the prompt data exist ONLY for the relatedPromptIndex field. NEVER mention an index like "[0]" or "Prompt 3" in titles or descriptions — refer to the prompt by quoting or paraphrasing its actual text/topic instead.`;function computeOpportunityScore(volume, visibility, competitorGap, intent) {@@ -207,15 +215,26 @@ export async function processContentJob({ brandId, model, job }) {};});- const scoredPrompts = promptDataForLLM+ // The button adds to the open list, as the nightly run does (#63), rather+ // than replacing it: prompts at the cap are skipped and the rest show the+ // model what they already hold.+ const existing = await loadOpenOpportunities(brandId);+ const openCounts = openCountsByPrompt(existing);+ const openTitles = openTitlesByPrompt(existing);++ const ranked = promptDataForLLM.map((p) => ({...p,score: computeOpportunityScore(p.estAiVolume, p.avgVisibility, p.competitorGap, p.intent),}))- .sort((a, b) => b.score - a.score)- .slice(0, 25);+ .sort((a, b) => b.score - a.score);+ const scoredPrompts = belowOpenCap(ranked, openCounts).slice(0, 25);if (scoredPrompts.length === 0) {+ logger.info(+ { brandId, openOpportunities: existing.length },+ 'every candidate prompt is at its open cap — nothing generated',+ );return { generated: 0 };}@@ -230,7 +249,7 @@ Prompt Data (sorted by opportunity score, highest first):${scoredPrompts.map((p, i) =>- `[${i}] "${p.text}" | Category: ${p.category} | Intent: ${p.intent} | Est. AI Volume: ${p.estAiVolume}/mo | Visibility: ${p.avgVisibility}% | Competitor Gap: ${p.competitorGap > 0 ? '+' + p.competitorGap : p.competitorGap}% | Competitors Cited: ${p.competitorsCited.join(', ') || 'none'} | Keywords: ${p.keywords.join(', ')}`,+ `[${i}] "${p.text}" | Category: ${p.category} | Intent: ${p.intent} | Est. AI Volume: ${p.estAiVolume}/mo | Visibility: ${p.avgVisibility}% | Competitor Gap: ${p.competitorGap > 0 ? '+' + p.competitorGap : p.competitorGap}% | Competitors Cited: ${p.competitorsCited.join(', ') || 'none'} | Keywords: ${p.keywords.join(', ')}${alreadySuggested(openTitles.get(p.promptId))}`,).join('\n')}@@ -249,15 +268,14 @@ IMPORTANT: Write every opportunity title and description in ${langName}.`;job.progress({ phase: 'saving', message: 'Saving opportunities...' });- await supabaseAdmin- .from('content_opportunities')- .delete()- .eq('brand_id', brandId)- .in('status', ['new']);+ const seen = new Set(existing.map((o) => opportunityKey(o.prompt_id, o.title)));const rows = object.opportunities.flatMap((opp) => {const relatedPrompt = relatedCandidate(scoredPrompts, opp.relatedPromptIndex);if (!relatedPrompt) return [];+ const key = opportunityKey(relatedPrompt.promptId, opp.title);+ if (seen.has(key)) return [];+ seen.add(key);const score = computeOpportunityScore(relatedPrompt.estAiVolume,relatedPrompt.avgVisibility,