Repository navigation
refactor(server): consolidate intent-extraction logic into a single module - #302
Conversation
Extract intentKeywordSchema, INTENT_SYSTEM_PROMPT, AI_VOLUME_MULTIPLIER and a new extractIntentKeywords() helper into server/src/lib/intent-extraction.js as the single source of truth. Both volumes.js call sites (/analyze and /analyze-batch) now collapse to one await extractIntentKeywords(), removing the duplicated generateObject() calls and the unused ai/zod imports from the route. No behavior change to the volumes endpoints. Closes #241
There was a problem hiding this comment.
Pull request overview
This PR refactors the server “volumes” routes by extracting the duplicated intent/keyword extraction logic (schema, system prompt, multiplier, and generateObject call) into a shared library module, so /analyze and /analyze-batch cannot drift.
Changes:
- Added
server/src/lib/intent-extraction.jsas the single source of truth for intent+keyword extraction and the AI volume multiplier. - Updated
server/src/routes/volumes.jsto callextractIntentKeywords(...)in both/analyzeand/analyze-batch, removing the duplicated inlinegenerateObjectblocks and dropping unused imports.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| server/src/routes/volumes.js | Replaces duplicated inline intent extraction with a shared helper and imports the shared multiplier. |
| server/src/lib/intent-extraction.js | New module centralizing intent schema, system prompt, multiplier parsing, and the shared generateObject wrapper. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import { generateObject } from 'ai'; | ||
| import { z } from 'zod'; | ||
|
|
||
| export const AI_VOLUME_MULTIPLIER = parseFloat(process.env.AI_VOLUME_MULTIPLIER || '0.15'); |
|
|
||
| export const INTENT_SYSTEM_PROMPT = `You are a keyword research expert. Given an AI search prompt, you must: | ||
|
|
||
| 1. Determine the primary search intent (comparison, how-to, what-is, best-top, vs-review, recommendation, or problem-solving). |
| CRITICAL: Output HEAD terms, NOT long-tail variants. Google Ads only returns search volume for keywords with measurable demand — long-tail strings like "best portable motion control for travel 2026" return 0. Drop modifiers and surface the underlying noun phrases. | ||
|
|
||
| Rules: | ||
| - 1 to 3 words per keyword (strict). Never more than 4. |
parseFloat on a non-numeric or empty AI_VOLUME_MULTIPLIER yielded NaN, which would propagate into estAiVolume and persisted rows. Fall back to the 0.15 default when the parsed value is not finite. (Copilot review on #302.)
|
Thanks @copilot — addressed: 1. 2 & 3. System-prompt wording (missing |
Summary
Consolidates the duplicated intent-extraction logic in the volumes routes into a single module.
The schema, system prompt, multiplier, and the
generateObjectcall used to extract search intent + 5 Google head keywords were defined inline inserver/src/routes/volumes.js, and the samegenerateObject({ schema, system, prompt })call appeared twice — in/analyzeand/analyze-batch— with identical arguments.This PR extracts a single source of truth,
server/src/lib/intent-extraction.js, exporting:intentKeywordSchemaINTENT_SYSTEM_PROMPTAI_VOLUME_MULTIPLIERextractIntentKeywords(promptText, aiModel)— wraps the sharedgenerateObjectcall and returns{ intent, keywords }.Both call sites in
volumes.jsnow collapse to a singleawait extractIntentKeywords(...), and the now-unusedai/zodimports are dropped from the route.No behavior change: production keeps its current head-term prompt and schema.
Related issue
Closes #241
Type of change
refactor— Code change that neither fixes a bug nor adds a featureChecklist
refactor/)eslint) on changed filesprettier --checkpasses on changed filesvitest run— 93/93)