Skip to content

refactor(server): consolidate intent-extraction logic into a single module - #302

Merged
gkhngyk merged 2 commits into
mainfrom
refactor/consolidate-intent-extraction
Jun 26, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
refactor/consolidate-intent-extraction

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Consolidates the duplicated intent-extraction logic in the volumes routes into a single module.

The schema, system prompt, multiplier, and the generateObject call used to extract search intent + 5 Google head keywords were defined inline in server/src/routes/volumes.js, and the same generateObject({ schema, system, prompt }) call appeared twice — in /analyze and /analyze-batch — with identical arguments.

This PR extracts a single source of truth, server/src/lib/intent-extraction.js, exporting:

  • intentKeywordSchema
  • INTENT_SYSTEM_PROMPT
  • AI_VOLUME_MULTIPLIER
  • extractIntentKeywords(promptText, aiModel) — wraps the shared generateObject call and returns { intent, keywords }.

Both call sites in volumes.js now collapse to a single await extractIntentKeywords(...), and the now-unused ai / zod imports are dropped from the route.

Note: the issue also mentioned cross-file drift with server/src/test-volumes.js. That smoke script was already deleted in #256, so the cross-file duplication no longer exists — this PR covers the in-route dedup + the lib extraction, which stands on its own.

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 feature

Checklist

  • Branch follows the naming convention (refactor/)
  • Commits follow Conventional Commits
  • Server lint passes (eslint) on changed files
  • Server prettier --check passes on changed files
  • Server test suite passes (vitest run — 93/93)
  • Changes are focused — one concern per PR

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.js as the single source of truth for intent+keyword extraction and the AI volume multiplier.
  • Updated server/src/routes/volumes.js to call extractIntentKeywords(...) in both /analyze and /analyze-batch, removing the duplicated inline generateObject blocks 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.

Comment thread server/src/lib/intent-extraction.js Outdated
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.)
@gkhngyk

gkhngyk commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @copilot — addressed:

1. AI_VOLUME_MULTIPLIER NaN guard ✅ Fixed (c2f813c). Now parses then falls back to 0.15 when the value isn't finite, so a non-numeric/empty env var can't poison estAiVolume or persisted rows. Verified: abc → 0.15, 0.2 → 0.2, unset → 0.15.

2 & 3. System-prompt wording (missing other; "1–3 words strict" vs "never more than 4") — intentionally not changed in this PR. This prompt was moved verbatim from the existing production code; both quirks are pre-existing, not introduced here. This PR is a pure refactor whose stated contract is no behavior change to the volumes endpoints — production keeps its current prompt and schema. Editing the prompt text would change the LLM's output, which is out of scope. They're fair observations and can be picked up as a separate prompt-tuning change if the team wants to refine extraction behavior.

Copilot AI commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

@gkhngyk I've opened a new pull request, #304, to work on those changes. Once the pull request is ready, I'll request review from you.

@gkhngyk
gkhngyk merged commit 7f8c0e7 into main Jun 26, 2026
3 checks passed
@gkhngyk
gkhngyk deleted the refactor/consolidate-intent-extraction branch June 26, 2026 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(server): consolidate intent-extraction logic into a single module

3 participants