first commit
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import type { CardType } from "./cards";
|
||||
import { getCachedCard, setCachedCard } from "./cache";
|
||||
import { generateStructuredResponse } from "./llm";
|
||||
import {
|
||||
historicalAccuracySchema,
|
||||
internationalRatingsSchema,
|
||||
productionFactsSchema,
|
||||
ratingExplainedSchema,
|
||||
sensitivityGuideSchema,
|
||||
skipGuideSchema,
|
||||
} from "./schemas";
|
||||
import { formatMovieContext, getMovieDetails } from "./tmdb";
|
||||
|
||||
const PROMPTS: Record<CardType, string> = {
|
||||
rating_explained: `Analyze why this film received its age rating.
|
||||
You MUST return ONLY valid JSON matching this exact structure (no extra keys, no markdown):
|
||||
{
|
||||
"officialRating": string,
|
||||
"region": string,
|
||||
"summary": string,
|
||||
"factors": array of {
|
||||
"category": one of exactly ["violence", "language", "sexuality", "substances", "frightening", "other"] (use ONLY these strings, no synonyms like "gore" or "nudity" — map to closest),
|
||||
"intensity": one of exactly ["mild", "moderate", "strong"],
|
||||
"description": string,
|
||||
"approximateTiming": string (optional, e.g. "12:45"),
|
||||
"confidence": one of ["high", "medium", "low"] (optional)
|
||||
},
|
||||
"borderlineNote": string (optional)
|
||||
}
|
||||
Focus on specific scenes or content patterns that drove the rating. Be precise with the allowed category and intensity values.`,
|
||||
|
||||
skip_guide: `Create a skip guide for families who want to watch this film with a younger audience.
|
||||
You MUST return ONLY valid JSON matching this exact structure:
|
||||
{
|
||||
"originalRating": string,
|
||||
"suggestedAudienceAge": number (integer 0-21),
|
||||
"totalSkippedMinutes": number (integer),
|
||||
"segments": array of {
|
||||
"start": string (e.g. "0:05" or "12m"),
|
||||
"end": string,
|
||||
"reason": string,
|
||||
"intensity": one of exactly ["mild", "moderate", "strong"],
|
||||
"plotImpact": one of exactly ["none", "minor", "significant"],
|
||||
"skipRecommendation": one of exactly ["recommended", "optional", "do_not_skip"],
|
||||
"resumeHint": string
|
||||
},
|
||||
"disclaimer": string
|
||||
}
|
||||
Only include genuinely skippable segments. Mark plot-critical scenes as skipRecommendation "do_not_skip".
|
||||
Timestamps should be approximate for the standard theatrical cut. Use only the exact enum strings listed.`,
|
||||
|
||||
sensitivity_guide: `Create a sensitivity guide grouping potentially upsetting content by theme.
|
||||
You MUST return ONLY valid JSON:
|
||||
{
|
||||
"summary": string,
|
||||
"themes": array of {
|
||||
"theme": string,
|
||||
"severity": one of exactly ["mild", "moderate", "strong"],
|
||||
"description": string,
|
||||
"approximateTiming": string (optional)
|
||||
}
|
||||
}
|
||||
Cover violence, sexuality, substances, self-harm, abuse, animal harm, discrimination, and other difficult themes where relevant. Use only the exact "severity" values.`,
|
||||
|
||||
international_ratings: `Summarize how this film is rated across countries using the certification data provided.
|
||||
Return JSON with: ratings (array of {country, countryCode, rating, descriptors?}), discrepancyNote, strictestCountry (optional), mostLenientCountry (optional).
|
||||
Include at least US, GB, ES, DE, FR when data exists. Explain notable differences.`,
|
||||
|
||||
production_facts: `Curate interesting behind-the-scenes production facts for cinephiles.
|
||||
Return JSON with: facts (array of at least 5 {category, fact}), budget (optional string), boxOffice (optional string), filmingLocations (optional array of strings).
|
||||
Prefer specific, verifiable-sounding details; note uncertainty in the fact text when needed.`,
|
||||
|
||||
historical_accuracy: `Evaluate historical accuracy for this biographical or historical film.
|
||||
You MUST return ONLY valid JSON:
|
||||
{
|
||||
"accuracyScore": number (0-100),
|
||||
"summary": string,
|
||||
"accurateElements": string[],
|
||||
"inventions": array of { "claim": string, "reality": string },
|
||||
"confidence": one of exactly ["high", "medium", "low"]
|
||||
}
|
||||
If the film is not historical/biographical, still assess any real-world basis it claims. Use only the exact confidence value.`,
|
||||
};
|
||||
|
||||
const SCHEMAS = {
|
||||
rating_explained: ratingExplainedSchema,
|
||||
skip_guide: skipGuideSchema,
|
||||
sensitivity_guide: sensitivityGuideSchema,
|
||||
international_ratings: internationalRatingsSchema,
|
||||
production_facts: productionFactsSchema,
|
||||
historical_accuracy: historicalAccuracySchema,
|
||||
} as const;
|
||||
|
||||
export async function generateCard(movieId: number, cardType: CardType) {
|
||||
const cached = await getCachedCard(movieId, cardType);
|
||||
if (cached) {
|
||||
const schema = SCHEMAS[cardType];
|
||||
if (schema.safeParse(cached).success) {
|
||||
return { data: cached, cached: true };
|
||||
}
|
||||
// stale/invalid cached data (e.g. from before prompt/schema tightening) — regenerate
|
||||
}
|
||||
|
||||
const movie = await getMovieDetails(movieId);
|
||||
const context = formatMovieContext(movie);
|
||||
const prompt = `${PROMPTS[cardType]}
|
||||
|
||||
Movie context:
|
||||
${context}`;
|
||||
|
||||
const schema = SCHEMAS[cardType];
|
||||
const data = await generateStructuredResponse(schema, prompt);
|
||||
await setCachedCard(movieId, cardType, data);
|
||||
|
||||
return { data, cached: false };
|
||||
}
|
||||
Reference in New Issue
Block a user