Skip to content

Fix SEO payload generation contract#10

Merged
ChrisAdamsdevelopment merged 1 commit into
mainfrom
codex/fix-seo-payload-generation-contract
May 4, 2026
Merged

Fix SEO payload generation contract#10
ChrisAdamsdevelopment merged 1 commit into
mainfrom
codex/fix-seo-payload-generation-contract

Conversation

@ChrisAdamsdevelopment
Copy link
Copy Markdown
Owner

@ChrisAdamsdevelopment ChrisAdamsdevelopment commented May 4, 2026

Motivation

  • A contract mismatch was found where the frontend called POST /api/generate-seo with structured fields (title, artist, genre, platform, etc.) while the backend only accepted { promptText }, which could produce 400 Invalid prompt and break the "Generate AI SEO Payload" flow.
  • The change aims to make the SEO contract robust and backward-compatible while preserving existing app behavior (auth, Stripe, usage limits, server/browser cleanse paths).

Description

  • Added asCleanText and buildSeoPrompt helpers and updated the backend POST /api/generate-seo to accept either a direct promptText or structured fields (title, artist, genre, platform, description, tags, lyrics, vibe) and to build a clear promptText server-side when required.
  • If promptText is present and valid it is used directly, otherwise structured fields are validated and composed into a prompt, and a 400 is returned only when neither is provided.
  • Preserved the frontend-facing Gemini response shape by returning an object with title, description, and tags, and added safe parsing that returns a JSON error (502) when Gemini returns malformed JSON instead of allowing the server to crash.
  • Updated the frontend handler in app.tsx to POST richer structured payloads, handle non-OK responses and network failures per item, surface a per-item error, and avoid clearing existing SEO fields on failure.

Testing

  • Ran npm install successfully.
  • Built the frontend with npm run build successfully (vite/tsc completed and bundles were generated).
  • Started the server locally with safe env: NODE_ENV=development JWT_SECRET=dev_jwt_secret_change_me ENABLE_MOCK_CHECKOUT=true DB_PATH=./spectra.db FRONTEND_URL=http://localhost:5173 PORT=3001 npm start and verified the server started.
  • Verified with curl that unauthenticated POST /api/generate-seo returns a JSON 401 Unauthorized (not HTML), and inspected code paths to confirm structured payloads are now accepted and invalid payloads are handled safely.
  • Confirmed that auth, Stripe/webhook, usage limiting, server cleanse, and browser cleanse behaviors were preserved and not changed beyond the narrow SEO flow contract and error-handling updates.

Codex Task

Summary by Sourcery

Align the SEO generation API contract between frontend and backend, improving robustness, error handling, and resilience to malformed Gemini responses.

New Features:

  • Allow the SEO generation endpoint to build prompts from structured song metadata when promptText is not provided.
  • Enhance the frontend SEO generation request to send richer metadata and handle per-item failures without overwriting existing SEO fields.

Bug Fixes:

  • Ensure the SEO generation endpoint rejects only truly invalid payloads and no longer fails when structured fields are sent instead of promptText.
  • Prevent server crashes by safely handling and validating JSON returned from the Gemini API.
  • Surface clear per-item error messages in the UI when SEO generation fails or returns malformed data.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 4, 2026

Reviewer's Guide

Backend SEO endpoint now accepts either a raw prompt or structured SEO fields and safely normalizes them into a prompt, improves Gemini JSON parsing, and the frontend calls it with richer structured payloads and robust per-item error handling without wiping existing SEO data.

File-Level Changes

Change Details Files
Normalize and validate SEO request payloads on the backend, allowing both promptText and structured SEO fields while returning clearer 400 errors.
  • Introduced asCleanText helper to sanitize and length-limit incoming text fields.
  • Added buildSeoPrompt to derive a valid Gemini prompt from either promptText or structured SEO metadata fields.
  • Updated /api/generate-seo handler to use buildSeoPrompt and return a more descriptive 400 error when no usable input is provided.
server.js
Harden Gemini response handling to avoid crashes and preserve a stable SEO payload contract.
  • Wrapped JSON.parse of Gemini output in try/catch and respond with 502 when the model returns malformed JSON.
  • Normalized the outgoing SEO payload to always include string title, description, and tags fields, defaulting to empty strings if missing or non-string.
server.js
Update frontend SEO generation flow to send richer payloads and handle errors without erasing existing SEO fields.
  • Changed SEO generation button handler to POST structured SEO fields (title, artist, genre, description, tags, platform) instead of just title/artist/genre/platform.
  • Added defensive JSON parsing and per-request error handling that logs failures and stores an error message on the item when the backend responds non-OK or the network fails.
  • Ensured existing SEO title/description/tags are preserved when generation fails or when the backend returns partial data, only overwriting fields with successful payload values.
app.tsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The inline onClick handler for generating SEO in app.tsx has grown quite large and imperative; consider extracting it into a named function or helper to improve readability and make error-handling logic easier to reuse and reason about.
  • Using the generic error field on activeItem for SEO-generation failures may overwrite or conflate other error states; consider introducing a dedicated seoError (or similar) field so SEO issues don’t mask unrelated item errors.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The inline `onClick` handler for generating SEO in `app.tsx` has grown quite large and imperative; consider extracting it into a named function or helper to improve readability and make error-handling logic easier to reuse and reason about.
- Using the generic `error` field on `activeItem` for SEO-generation failures may overwrite or conflate other error states; consider introducing a dedicated `seoError` (or similar) field so SEO issues don’t mask unrelated item errors.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ChrisAdamsdevelopment ChrisAdamsdevelopment merged commit 3a6644b into main May 4, 2026
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant