Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/prerender/guidance-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default async function handler(message, context) {
suggestions.forEach((incoming) => {
// Handle potential null/undefined elements in suggestions array
const {
url, aiSummary, valuable,
url, aiSummary, valuable, prompts,
} = incoming || {};

if (!url) {
Expand Down Expand Up @@ -177,6 +177,10 @@ export default async function handler(message, context) {
aiSummary: hasValidAiSummary ? aiSummary : (currentData.aiSummary ?? ''),
// Default to true if not provided, but respect explicit boolean from Mystique
valuable: isValuable,
// Use new prompts if provided; otherwise preserve existing
prompts: Array.isArray(prompts) && prompts.length > 0
? prompts
: (currentData.prompts ?? []),
};

warnOnInvalidSuggestionData(updatedData, opportunity.getType(), log);
Expand Down
45 changes: 43 additions & 2 deletions src/prerender/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,13 @@ async function fetchLatestScrapeJobId(siteId, context) {
* @param {Object} context - Processing context
* @returns {Promise<number>} - Number of suggestions sent to Mystique
*/
async function sendPrerenderGuidanceRequestToMystique(auditUrl, auditData, opportunity, context) {
async function sendPrerenderGuidanceRequestToMystique(
auditUrl,
auditData,
opportunity,
context,
generatePrompts = false,
) {
const {
log, sqs, env, site,
} = context;
Expand Down Expand Up @@ -540,6 +546,8 @@ async function sendPrerenderGuidanceRequestToMystique(auditUrl, auditData, oppor
url: data.url,
originalHtmlMarkdownKey,
markdownDiffKey,
// Signal whether this suggestion already has prompts so Mystique can skip re-generation
hasPrompts: Array.isArray(data.prompts) && data.prompts.length > 0,
});
});

Expand All @@ -548,6 +556,32 @@ async function sendPrerenderGuidanceRequestToMystique(auditUrl, auditData, oppor
return 0;
}

// Send LLMO site config category/topic/region names to Mystique for prompt classification.
// Mirrors how the rcv-prompts utility injects llmoCategoryNames/llmoTopicNames/llmoRegionNames.
// Gracefully degrades to empty arrays if config is unavailable.
let llmoCategories = [];
let llmoTopics = [];
let llmoRegions = [];
/* c8 ignore next 15 - LLMO config read is best-effort; tested separately */
try {
const llmoCfg = site?.getConfig?.()?.getLlmoConfig?.();
if (llmoCfg) {
llmoCategories = Object.values(llmoCfg.categories || {})
.map((c) => c.name).filter(Boolean);
const allTopics = { ...llmoCfg.topics, ...llmoCfg.aiTopics };
llmoTopics = Object.values(allTopics).map((t) => t.name).filter(Boolean);
const regionSet = new Set();
Object.values(llmoCfg.categories || {}).forEach((cat) => {
if (Array.isArray(cat.region)) cat.region.forEach((r) => regionSet.add(r));
else if (cat.region) regionSet.add(cat.region);
});
llmoRegions = [...regionSet];
log.debug(`${LOG_PREFIX} Loaded LLMO config: ${llmoCategories.length} categories, ${llmoTopics.length} topics, ${llmoRegions.length} regions. baseUrl=${baseUrl}`);
}
} catch (llmoErr) {
log.warn(`${LOG_PREFIX} Failed to read LLMO config for prompt classification (non-fatal): ${llmoErr.message}. baseUrl=${baseUrl}`);
}

const deliveryType = site?.getDeliveryType?.() || 'unknown';

const message = {
Expand All @@ -559,6 +593,10 @@ async function sendPrerenderGuidanceRequestToMystique(auditUrl, auditData, oppor
data: {
opportunityId,
suggestions: suggestionsPayload,
generatePrompts,
llmoCategories,
llmoTopics,
llmoRegions,
},
};

Expand Down Expand Up @@ -589,14 +627,16 @@ export async function handleAiOnlyMode(context) {
const siteId = site.getId();
const baseUrl = site.getBaseURL();

// Parse optional params from data field (opportunityId, scrapeJobId)
// Parse optional params from data field (opportunityId, scrapeJobId, generatePrompts)
let opportunityId = null;
let scrapeJobId = null;
let generatePrompts = false;
if (data) {
try {
const parsedData = typeof data === 'string' ? JSON.parse(data) : data;
opportunityId = parsedData.opportunityId;
scrapeJobId = parsedData.scrapeJobId;
generatePrompts = !!parsedData.generatePrompts;
} catch (e) {
// Ignore parse errors - graceful degradation for malformed JSON
}
Expand Down Expand Up @@ -679,6 +719,7 @@ export async function handleAiOnlyMode(context) {
auditData,
opportunity,
context,
generatePrompts,
);

log.info(`${LOG_PREFIX} ai-only: Successfully queued AI summary request for ${suggestionCount} suggestion(s). baseUrl=${baseUrl}, siteId=${siteId}, opportunityId=${opportunity.getId()}`);
Expand Down
Loading