AI Content Generation (Enhance With AI)

Prev Next

AI Content Generation (Enhance With AI)

This page documents the generation flow that is currently shipped for Enhance With AI.

End-to-End Sequence

Image

Request and Response Contracts

Generate request (GenerateInput)

Field Type Notes
topic string Comes from description input in Enhance With AI form
contentType ContentTypeEnum Sent as route path segment and body value
fields Record<keyof GeneratedContent, boolean> Selects generated output keys
controls EnhanceControls Optional tone, mood, audience, optimization

Generate response (GeneratedContent)

Field Type Used by
title string Review + apply to content form
description string Review + apply to content form
categories string[] Converted to { title }[] before apply
tags string[] Converted to { title }[] before apply
articleBody string Mapped to articleContent in review/apply

Field Mapping Bridge

useContentEnhanceWithAi maps UI field names to domain keys before API call.

Form field API/domain field Apply target key
title title title
description description description
tags tags tags
category categories categories
articleContent articleBody articleContent

Review/apply key definitions are centralized in src/config/content/content-tools/enhance-ai/reviewFields.ts.

Controls and Prompting

Creative controls passed from UI:

  • tone
  • mood
  • audience
  • optimization

The domain prompt builder (prompt.ts) translates enums into explicit instruction strings and enforces a strict output contract:

  • JSON only
  • no extra keys
  • requested fields only

Validation and Failure Behavior

Server-side generation path (domain/content/generate.ts + schema.ts):

  1. Reject empty field selection (NO_FIELDS_REQUESTED).
  2. Parse provider output as JSON (INVALID_JSON_RESPONSE if parse fails).
  3. Validate against Zod schema for requested subset.
  4. Throw MISSING_REQUIRED_FIELD:<field> when requested key is absent.

Route behavior (route.ts):

  • Returns 400 for AIError.
  • Returns 500 for unexpected errors.
  • Writes success/failure generation audit payloads to console.debug.

Gating

Enhance With AI is currently gated by:

  • Content type allowlist in content-tools registry (vod, live, series, article).
  • Auth cookies required by AI route (siteId, id, site).
  • Proxy matcher includes /api/ai/content/:path*.

There is no feature-flag gate for this feature today.

Apply and Persist Path

Apply flow in useContentEnhanceWithAi:

  1. User selects output fields in Review (or applies all available fields).
  2. Hook writes selected values into content detail form with shouldDirty.
  3. Array fields are normalized through ContentEnhanceWithAiHelpers.toTitleOnlyEntries.
  4. Hook calls existing save(contentType) from content details context.
  5. Save persists through ContentDetailsService.update(...) with existing content mutation registry.

This keeps AI stateless: AI generates suggestions, while persistence remains in the existing content save pipeline.

API Surface

Intent Endpoint Status
generate POST /api/ai/content/:contentType/generate Active
enhance POST /api/ai/content/:contentType/enhance Returns 501 ENHANCE_NOT_IMPLEMENTED