Compare commits

...

16 Commits

Author SHA1 Message Date
910d26add3 fixes!
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m6s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 30s
2026-04-03 11:48:17 -03:00
0944ec62bb decreasing bucket size
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m6s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 1m2s
2026-04-03 10:54:28 -03:00
a7d12acce6 fixing api calls
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m4s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 10s
2026-04-03 01:15:47 -03:00
0c704cf2f6 increasing max tokens
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m4s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 18s
2026-04-02 21:31:34 -03:00
98860835d9 adding max retries
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m3s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 9s
2026-04-02 21:11:39 -03:00
fd3ad4c77f increasing timeout
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m29s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 13s
2026-04-02 20:58:49 -03:00
d849b67f3d fixing disconnection issue
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m5s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 8s
2026-04-02 20:12:11 -03:00
ba38092784 new things
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m4s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 11s
2026-04-02 19:24:58 -03:00
91870f4046 fixes
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 5m38s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 40s
2026-04-02 13:46:14 -03:00
39edec4a7c changes and improvements
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 5m40s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 38s
2026-04-01 18:31:14 -03:00
a73bc27356 fix
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m3s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 11s
2026-03-31 20:23:54 -03:00
148755243a fixes
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m5s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 11s
2026-03-31 19:55:20 -03:00
bb8a5da45e adding stuff
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m6s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 12s
2026-03-31 17:21:50 -03:00
be2d8d70cb adding buckets!
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 5m42s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 39s
2026-03-31 16:42:16 -03:00
77757ace5e adding support for generic AI provider (OpenAI compatible)
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 5m37s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 34s
2026-03-30 19:58:14 -03:00
88c839e768 changing interpreter model 2026-03-30 19:35:09 -03:00
28 changed files with 1198 additions and 203 deletions

View File

@@ -30,6 +30,17 @@ spec:
secretKeyRef:
name: recommender-secrets
key: DATABASE_URL
- name: BEARER_TOKEN
valueFrom:
secretKeyRef:
name: recommender-secrets
key: BEARER_TOKEN
- name: PROVIDER_URL
value: "https://openrouter.ai/api/v1"
- name: MODEL_NAME
value: "openai/gpt-5.4"
- name: AI_PROVIDER
value: "GENERIC"
resources:
requests:
memory: "256Mi"

View File

@@ -3,4 +3,14 @@
# In production / Docker, supply these as environment variables.
DATABASE_URL=postgres://user:password@localhost:5432/recommender
# AI provider selection: OPENAI (default) or GENERIC
AI_PROVIDER=OPENAI
# OpenAI provider settings (used when AI_PROVIDER=OPENAI)
OPENAI_API_KEY=your-openai-api-key-here
# Generic provider settings (used when AI_PROVIDER=GENERIC)
PROVIDER_URL=https://your-provider.example.com/v1
BEARER_TOKEN=your-bearer-token
MODEL_NAME=your-model-name

View File

@@ -0,0 +1,9 @@
ALTER TABLE "recommendations" ADD COLUMN "use_validator" boolean DEFAULT false NOT NULL;
--> statement-breakpoint
ALTER TABLE "recommendations" ADD COLUMN "hard_requirements" boolean DEFAULT false NOT NULL;
--> statement-breakpoint
ALTER TABLE "recommendations" ADD COLUMN "self_expansive" boolean DEFAULT false NOT NULL;
--> statement-breakpoint
ALTER TABLE "recommendations" ADD COLUMN "expansive_passes" integer DEFAULT 1 NOT NULL;
--> statement-breakpoint
ALTER TABLE "recommendations" ADD COLUMN "expansive_mode" text DEFAULT 'soft' NOT NULL;

View File

@@ -2,20 +2,39 @@ import OpenAI from 'openai';
import * as dotenv from 'dotenv';
dotenv.config({ path: ['.env.local', '.env'] });
export const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const AI_PROVIDER = process.env.AI_PROVIDER ?? 'OPENAI';
const isGeneric = AI_PROVIDER === 'GENERIC';
export async function askAgent(prompt: string) {
try {
const response = await openai.chat.completions.create({
model: 'gpt-5.4',
service_tier: 'flex',
messages: [{ role: 'user', content: prompt }],
});
return response!.choices![0]!.message!.content;
} catch (err) {
console.error('Agent endpoint dummy error:', err instanceof Error ? err.message : err);
return 'Agent is in dummy mode or encountered an error.';
export const openai = isGeneric
? new OpenAI({
apiKey: process.env.BEARER_TOKEN,
baseURL: process.env.PROVIDER_URL,
timeout: 600000, // 10 minutes
maxRetries: 3,
})
: new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
timeout: 600000, // 10 minutes
maxRetries: 3,
});
export const defaultModel = isGeneric ? (process.env.MODEL_NAME ?? 'default') : 'gpt-5.4';
export const miniModel = isGeneric ? (process.env.MODEL_NAME ?? 'default') : 'gpt-5.4-mini';
export const serviceOptions = isGeneric ? {} : { service_tier: 'flex' as const };
export const supportsWebSearch = !isGeneric;
export async function parseWithRetry<T>(fn: () => Promise<T>, retries = 2): Promise<T> {
let lastErr: unknown;
for (let attempt = 0; attempt <= retries; attempt++) {
try {
return await fn();
} catch (err) {
if (err instanceof SyntaxError && attempt < retries) {
lastErr = err;
continue;
}
throw err;
}
}
throw lastErr;
}

View File

@@ -1,4 +1,4 @@
import { openai } from '../agent.js';
import { openai, defaultModel, serviceOptions, supportsWebSearch, parseWithRetry } from '../agent.js';
import type { InterpreterOutput, RankingOutput, CuratorOutput, MediaType } from '../types/agents.js';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
@@ -7,10 +7,15 @@ const CuratorSchema = z.object({
shows: z.array(z.object({
title: z.string(),
explanation: z.string(),
category: z.enum(["Definitely Like", "Might Like", "Questionable", "Will Not Like"])
category: z.enum(["Full Match", "Definitely Like", "Might Like", "Questionable", "Will Not Like"]),
genre: z.string(),
pros: z.array(z.string()).max(3),
cons: z.array(z.string()).max(3)
}))
});
const CHUNK_SIZE = 20;
export async function runCurator(
ranking: RankingOutput,
interpreter: InterpreterOutput,
@@ -20,6 +25,7 @@ export async function runCurator(
const mediaLabel = mediaType === 'movie' ? 'movie' : 'TV show';
const allShows = [
...(ranking.full_match ?? []).map((t) => ({ title: t, category: 'Full Match' as const })),
...ranking.definitely_like.map((t) => ({ title: t, category: 'Definitely Like' as const })),
...ranking.might_like.map((t) => ({ title: t, category: 'Might Like' as const })),
...ranking.questionable.map((t) => ({ title: t, category: 'Questionable' as const })),
@@ -28,33 +34,48 @@ export async function runCurator(
if (allShows.length === 0) return [];
const showList = allShows
.map((s) => `- "${s.title}" (${s.category})`)
.join('\n');
const canSearch = useWebSearch && supportsWebSearch;
const preferenceSummary = `User preferences summary:
Liked: ${interpreter.liked.join(', ') || '(none)'}
Themes: ${interpreter.themes.join(', ') || '(none)'}
Tone: ${interpreter.tone.join(', ') || '(none)'}
Character preferences: ${interpreter.character_preferences.join(', ') || '(none)'}
Avoid: ${interpreter.avoid.join(', ') || '(none)'}
Requirements: ${interpreter.requirements.join(', ') || '(none)'}`;
const response = await openai.responses.parse({
model: 'gpt-5.4',
temperature: 0.5,
service_tier: 'flex',
...(useWebSearch ? { tools: [{ type: 'web_search' as const }] } : {}),
text: { format: zodTextFormat(CuratorSchema, "shows") },
instructions: `You are a ${mediaLabel} recommendation curator. For each ${mediaLabel}, write a concise 1-2 sentence explanation of why it was assigned to its category based on the user's preferences.${useWebSearch ? '\n\nUse web search to verify details and enrich explanations with accurate information.' : ''}
const instructions = `You are a ${mediaLabel} recommendation curator. For each ${mediaLabel}, write a concise explanation and surface the most useful details for the user.${useWebSearch ? '\n\nUse web search to verify details and enrich explanations with accurate information.' : ''}
Rules:
- Preserve the exact title and category as given
- Keep explanations concise (1-2 sentences max)
- Reference specific user preferences in the explanation
- Be honest — explain why "Questionable" or "Will Not Like" ${mediaLabel}s got that rating`,
input: `User preferences summary:
Liked: ${JSON.stringify(interpreter.liked)}
Themes: ${JSON.stringify(interpreter.themes)}
Tone: ${JSON.stringify(interpreter.tone)}
Character preferences: ${JSON.stringify(interpreter.character_preferences)}
Avoid: ${JSON.stringify(interpreter.avoid)}
- explanation: 1-2 sentences explaining why it was assigned to its category, referencing specific user preferences
- genre: 1-3 words capturing the most prominent genre of the title (e.g. "Crime Drama", "Sci-Fi Thriller", "Romantic Comedy")
- pros: up to 3 short bullet points about what this title does well relative to the user's taste
- cons: up to 3 short bullet points about what the user might not like based on their preferences
- Be honest — explain why "Questionable" or "Will Not Like" ${mediaLabel}s got that rating`;
const chunks: typeof allShows[] = [];
for (let i = 0; i < allShows.length; i += CHUNK_SIZE) {
chunks.push(allShows.slice(i, i + CHUNK_SIZE));
}
const results: CuratorOutput[] = [];
for (const chunk of chunks) {
const showList = chunk.map((s) => `- "${s.title}" (${s.category})`).join('\n');
const response = await parseWithRetry(() => openai.responses.parse({
model: defaultModel,
temperature: 0.5,
max_completion_tokens: 16384,
...serviceOptions,
...(canSearch ? { tools: [{ type: 'web_search' as const }] } : {}),
text: { format: zodTextFormat(CuratorSchema, "shows") },
instructions,
input: `${preferenceSummary}
${mediaLabel}s to describe:
${showList}`,
});
}));
results.push(...(response.output_parsed?.shows ?? []));
}
return response.output_parsed?.shows ?? [];
return results;
}

View File

@@ -1,4 +1,4 @@
import { openai } from '../agent.js';
import { openai, defaultModel, serviceOptions, parseWithRetry } from '../agent.js';
import type { InterpreterOutput, MediaType } from '../types/agents.js';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
@@ -9,7 +9,8 @@ const InterpreterSchema = z.object({
themes: z.array(z.string()),
character_preferences: z.array(z.string()),
tone: z.array(z.string()),
avoid: z.array(z.string())
avoid: z.array(z.string()),
requirements: z.array(z.string())
});
interface InterpreterInput {
@@ -27,10 +28,10 @@ export async function runInterpreter(input: InterpreterInput): Promise<Interpret
? `\n\nUser Feedback Context (incorporate into preferences):\n${input.feedback_context}`
: '';
const response = await openai.responses.parse({
model: 'gpt-5.4-mini',
const response = await parseWithRetry(() => openai.responses.parse({
model: defaultModel,
temperature: 0.2,
service_tier: 'flex',
...serviceOptions,
text: { format: zodTextFormat(InterpreterSchema, "preferences") },
instructions: `You are a ${mediaLabel} preference interpreter. Transform raw user input into structured, normalized preferences.
@@ -39,14 +40,15 @@ Rules:
- Normalize terminology (e.g. "spy" → "espionage", "cop show" → "police procedural")
- Detect and resolve contradictions (prefer explicit over implicit)
- Do NOT assume anything not stated or clearly implied
- Be specific and concrete, not vague`,
- Be specific and concrete, not vague
- For "requirements": capture explicit hard requirements the user stated that recommendations must satisfy — things like "must be from the 2000s onward", "must have subtitles", "must feature a female lead". Leave empty if no such constraints were stated.`,
input: `Main prompt: ${input.main_prompt}
Liked ${mediaLabel}s: ${input.liked_shows || '(none)'}
Disliked ${mediaLabel}s: ${input.disliked_shows || '(none)'}
Themes and requirements: ${input.themes || '(none)'}${feedbackSection}`,
});
}));
return (response.output_parsed as InterpreterOutput) ?? {
liked: [], disliked: [], themes: [], character_preferences: [], tone: [], avoid: []
liked: [], disliked: [], themes: [], character_preferences: [], tone: [], avoid: [], requirements: []
};
}

View File

@@ -1,9 +1,10 @@
import { openai } from '../agent.js';
import { openai, defaultModel, serviceOptions, parseWithRetry } from '../agent.js';
import type { InterpreterOutput, RetrievalOutput, RankingOutput, MediaType } from '../types/agents.js';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
const RankingSchema = z.object({
full_match: z.array(z.string()),
definitely_like: z.array(z.string()),
might_like: z.array(z.string()),
questionable: z.array(z.string()),
@@ -14,6 +15,7 @@ export async function runRanking(
interpreter: InterpreterOutput,
retrieval: RetrievalOutput,
mediaType: MediaType = 'tv_show',
hardRequirements = false,
): Promise<RankingOutput> {
const mediaLabel = mediaType === 'movie' ? 'movie' : 'TV show';
@@ -31,7 +33,8 @@ export async function runRanking(
chunks.push(filtered.slice(i, i + CHUNK_SIZE));
}
const allBuckets: RankingOutput = {
const allTags: RankingOutput = {
full_match: [],
definitely_like: [],
might_like: [],
questionable: [],
@@ -41,38 +44,42 @@ export async function runRanking(
for (const chunk of chunks) {
const chunkTitles = chunk.map((c) => `- ${c.title}: ${c.reason}`).join('\n');
const response = await openai.responses.parse({
model: 'gpt-5.4',
const response = await parseWithRetry(() => openai.responses.parse({
model: defaultModel,
temperature: 0.2,
service_tier: 'flex',
max_completion_tokens: 16384,
...serviceOptions,
text: { format: zodTextFormat(RankingSchema, "ranking") },
instructions: `You are a ${mediaLabel} ranking critic. Assign each ${mediaLabel} to exactly one of four confidence buckets based on how well it matches the user's preferences.
instructions: `You are a ${mediaLabel} ranking critic. Assign each ${mediaLabel} to exactly one of five confidence tags based on how well it matches the user's preferences.
Buckets:
- "definitely_like": Near-perfect match to all preferences
Tags:
- "full_match": 100% match — perfectly satisfies every stated preference, requirement, and avoidance criteria with no compromises
- "definitely_like": Near-perfect match to all preferences with only minor caveats
- "might_like": Strong match to most preferences
- "questionable": Partial alignment, some aspects don't match
- "will_not_like": Likely mismatch, conflicts with preferences or avoidance criteria
Every ${mediaLabel} in the input must appear in exactly one bucket. Use the title exactly as given.`,
Every ${mediaLabel} in the input must appear in exactly one tag. Use the title exactly as given.${hardRequirements ? '\n\nHARD REQUIREMENTS MODE: Any candidate that does not satisfy every stated requirement must be placed in "will_not_like", regardless of other qualities.' : ''}`,
input: `User preferences:
Liked ${mediaLabel}s: ${JSON.stringify(interpreter.liked)}
Themes: ${JSON.stringify(interpreter.themes)}
Character preferences: ${JSON.stringify(interpreter.character_preferences)}
Tone: ${JSON.stringify(interpreter.tone)}
Avoid: ${JSON.stringify(interpreter.avoid)}
Liked ${mediaLabel}s: ${interpreter.liked.join(', ') || '(none)'}
Themes: ${interpreter.themes.join(', ') || '(none)'}
Character preferences: ${interpreter.character_preferences.join(', ') || '(none)'}
Tone: ${interpreter.tone.join(', ') || '(none)'}
Avoid: ${interpreter.avoid.join(', ') || '(none)'}
Requirements: ${interpreter.requirements.join(', ') || '(none)'}
Rank these ${mediaLabel}s:
${chunkTitles}`,
});
}));
const chunkResult = (response.output_parsed as Partial<RankingOutput>) ?? {};
allBuckets.definitely_like.push(...(chunkResult.definitely_like ?? []));
allBuckets.might_like.push(...(chunkResult.might_like ?? []));
allBuckets.questionable.push(...(chunkResult.questionable ?? []));
allBuckets.will_not_like.push(...(chunkResult.will_not_like ?? []));
allTags.full_match.push(...(chunkResult.full_match ?? []));
allTags.definitely_like.push(...(chunkResult.definitely_like ?? []));
allTags.might_like.push(...(chunkResult.might_like ?? []));
allTags.questionable.push(...(chunkResult.questionable ?? []));
allTags.will_not_like.push(...(chunkResult.will_not_like ?? []));
}
return allBuckets;
return allTags;
}

View File

@@ -1,4 +1,4 @@
import { openai } from '../agent.js';
import { openai, defaultModel, serviceOptions, supportsWebSearch, parseWithRetry } from '../agent.js';
import type { InterpreterOutput, RetrievalOutput, MediaType } from '../types/agents.js';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
@@ -15,15 +15,19 @@ export async function runRetrieval(
brainstormCount = 100,
mediaType: MediaType = 'tv_show',
useWebSearch = false,
hardRequirements = false,
previousFullMatches: string[] = [],
): Promise<RetrievalOutput> {
const mediaLabel = mediaType === 'movie' ? 'movie' : 'TV show';
const mediaLabelPlural = mediaType === 'movie' ? 'movies' : 'TV shows';
const response = await openai.responses.parse({
model: 'gpt-5.4',
const canSearch = useWebSearch && supportsWebSearch;
const response = await parseWithRetry(() => openai.responses.parse({
model: defaultModel,
temperature: 0.9,
service_tier: 'flex',
...(useWebSearch ? { tools: [{ type: 'web_search' as const }] } : {}),
max_completion_tokens: 16384,
...serviceOptions,
...(canSearch ? { tools: [{ type: 'web_search' as const }] } : {}),
text: { format: zodTextFormat(RetrievalSchema, "candidates") },
instructions: `You are a ${mediaLabel} candidate generator. Your goal is to brainstorm a LARGE, DIVERSE pool of ${brainstormCount} ${mediaLabel} candidates that match the user's structured preferences.${useWebSearch ? '\n\nUse web search to find recent and accurate titles, including newer releases.' : ''}
@@ -33,17 +37,19 @@ Rules:
- Each "reason" should briefly explain why the ${mediaLabel} matches the preferences
- Avoid duplicates
- Include ${mediaLabelPlural} from different decades, countries${mediaType === 'tv_show' ? ', and networks' : ', and directors'}
- Aim for ${brainstormCount} candidates minimum`,
- The "title" field must contain ONLY the exact title name — no years, descriptions, network names, episode counts, or parenthetical notes. ✗ Bad: "Breaking Bad (20082013, AMC)" ✓ Good: "Breaking Bad"
- Aim for ${brainstormCount} candidates minimum${previousFullMatches.length > 0 ? '\n- Do NOT suggest titles already in the Previous Full Matches list — generate NEW candidates inspired by what made those successful' : ''}${hardRequirements ? '\n\nIMPORTANT: Strictly follow ALL requirements. Exclude any candidate that does not meet every stated requirement.' : ''}`,
input: `Structured preferences:
Liked ${mediaLabelPlural}: ${JSON.stringify(input.liked)}
Disliked ${mediaLabelPlural}: ${JSON.stringify(input.disliked)}
Themes: ${JSON.stringify(input.themes)}
Character preferences: ${JSON.stringify(input.character_preferences)}
Tone: ${JSON.stringify(input.tone)}
Avoid: ${JSON.stringify(input.avoid)}
Liked ${mediaLabelPlural}: ${input.liked.join(', ') || '(none)'}
Disliked ${mediaLabelPlural}: ${input.disliked.join(', ') || '(none)'}
Themes: ${input.themes.join(', ') || '(none)'}
Character preferences: ${input.character_preferences.join(', ') || '(none)'}
Tone: ${input.tone.join(', ') || '(none)'}
Avoid: ${input.avoid.join(', ') || '(none)'}
Requirements: ${input.requirements.join(', ') || '(none)'}${previousFullMatches.length > 0 ? `\n\nPrevious Full Match titles (DO NOT repeat these; use them as inspiration for NEW candidates with similar qualities): ${previousFullMatches.join(', ')}` : ''}
Generate a large, diverse pool of ${mediaLabel} candidates.`,
});
}));
return (response.output_parsed as RetrievalOutput) ?? { candidates: [] };
}

View File

@@ -1,13 +1,13 @@
import { openai } from '../agent.js';
import { openai, miniModel, serviceOptions } from '../agent.js';
import type { InterpreterOutput, MediaType } from '../types/agents.js';
export async function generateTitle(interpreter: InterpreterOutput, mediaType: MediaType = 'tv_show'): Promise<string> {
const mediaLabel = mediaType === 'movie' ? 'movie' : 'TV show';
const response = await openai.responses.create({
model: 'gpt-5.4-mini',
model: miniModel,
temperature: 0.7,
service_tier: 'flex',
...serviceOptions,
instructions: `Generate a concise 5-8 word title for a ${mediaLabel} recommendation session.
Capture the essence of the user's taste — genre, tone, key themes.
Respond with ONLY the title. No quotes, no trailing punctuation.

View File

@@ -0,0 +1,68 @@
import { openai, defaultModel, serviceOptions, supportsWebSearch, parseWithRetry } from '../agent.js';
import type { RetrievalCandidate, ValidatorOutput, MediaType } from '../types/agents.js';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
const ValidatorSchema = z.object({
candidates: z.array(z.object({
title: z.string(),
reason: z.string(),
isTrash: z.boolean(),
})),
});
const CHUNK_SIZE = 30;
function splitIntoChunks<T>(items: T[], size: number): T[][] {
const chunks: T[][] = [];
for (let i = 0; i < items.length; i += size) {
chunks.push(items.slice(i, i + size));
}
return chunks;
}
async function runValidatorChunk(
candidates: RetrievalCandidate[],
mediaLabel: string,
): Promise<ValidatorOutput> {
const list = candidates.map((c) => `- ${c.title}: ${c.reason}`).join('\n');
const response = await parseWithRetry(() => openai.responses.parse({
model: defaultModel,
temperature: 0.1,
max_completion_tokens: 16384,
...serviceOptions,
...(supportsWebSearch ? { tools: [{ type: 'web_search' as const }] } : {}),
text: { format: zodTextFormat(ValidatorSchema, 'validation') },
instructions: `You are a ${mediaLabel} metadata validator. For each candidate in the list, use web search to verify:
1. The title actually exists as a real, produced ${mediaLabel} (not a made-up or hallucinated title)
2. Correct the "reason" field with accurate metadata (actual genres, tone, year) if it contains errors
Set isTrash: true for entries that:
- Do not exist as a real ${mediaLabel}
- Are clearly hallucinated or fictional titles
- Are so incorrect that no real match can be identified
Set isTrash: false for real, verifiable ${mediaLabel}s, even if minor metadata corrections are needed.
Return every candidate — do not drop any entries from the output.`,
input: `Validate these ${mediaLabel} candidates:\n${list}`,
}));
return (response.output_parsed as ValidatorOutput) ?? { candidates: [] };
}
export async function runValidator(
candidates: RetrievalCandidate[],
mediaType: MediaType = 'tv_show',
): Promise<ValidatorOutput> {
const mediaLabel = mediaType === 'movie' ? 'movie' : 'TV show';
const chunks = splitIntoChunks(candidates, CHUNK_SIZE);
const chunkResults = await Promise.all(
chunks.map((chunk) => runValidatorChunk(chunk, mediaLabel))
);
return {
candidates: chunkResults.flatMap((r) => r.candidates),
};
}

View File

@@ -11,6 +11,11 @@ export const recommendations = pgTable('recommendations', {
brainstorm_count: integer('brainstorm_count').notNull().default(100),
media_type: text('media_type').notNull().default('tv_show'),
use_web_search: boolean('use_web_search').notNull().default(false),
use_validator: boolean('use_validator').notNull().default(false),
hard_requirements: boolean('hard_requirements').notNull().default(false),
self_expansive: boolean('self_expansive').notNull().default(false),
expansive_passes: integer('expansive_passes').notNull().default(1),
expansive_mode: text('expansive_mode').notNull().default('soft'),
recommendations: jsonb('recommendations').$type<CuratorOutput[]>(),
status: text('status').notNull().default('pending'),
created_at: timestamp('created_at').defaultNow().notNull(),

View File

@@ -4,6 +4,7 @@ import postgres from 'postgres';
import * as dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs/promises';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -25,6 +26,10 @@ const runMigrations = async () => {
console.log('Running database migrations...');
try {
const folder = path.join(__dirname, '../drizzle');
// print all migrations
const migrations = await fs.readdir(folder);
console.log('Migrations:', JSON.stringify(migrations));
await migrate(db, { migrationsFolder: folder });
console.log('Migrations completed successfully.');
} catch (err) {

View File

@@ -3,20 +3,47 @@ import { db } from '../db.js';
import { recommendations } from '../db/schema.js';
import { runInterpreter } from '../agents/interpreter.js';
import { runRetrieval } from '../agents/retrieval.js';
import { runValidator } from '../agents/validator.js';
import { runRanking } from '../agents/ranking.js';
import { runCurator } from '../agents/curator.js';
import type { CuratorOutput, MediaType, SSEEvent } from '../types/agents.js';
import type { CuratorOutput, InterpreterOutput, MediaType, RankingOutput, RetrievalCandidate, SSEEvent } from '../types/agents.js';
import { generateTitle } from '../agents/titleGenerator.js';
/* -- Agent pipeline --
[1] Interpreter -> gets user input, transforms into structured data
[2] Retrieval -> gets candidates from OpenAI (high temperature)
[2.5] Validator (optional) -> verifies candidates exist, removes trash
[3] Ranking -> ranks candidates based on user input
[4] Curator -> curates candidates based on user input
*/
type RecommendationRecord = typeof recommendations.$inferSelect;
function getBucketCount(count: number): number {
return Math.ceil(count / 15);
}
function deduplicateCandidates(candidates: RetrievalCandidate[], seenTitles?: Set<string>): RetrievalCandidate[] {
const seen = seenTitles ?? new Set<string>();
return candidates.filter((c) => {
const key = c.title.toLowerCase();
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
function splitIntoBuckets<T>(items: T[], n: number): T[][] {
const size = Math.ceil(items.length / n);
return Array.from({ length: n }, (_, i) => items.slice(i * size, (i + 1) * size))
.filter((b) => b.length > 0);
}
function mergeCuratorOutputs(a: CuratorOutput[], b: CuratorOutput[]): CuratorOutput[] {
const seen = new Set(a.map((x) => x.title.toLowerCase()));
return [...a, ...b.filter((x) => !seen.has(x.title.toLowerCase()))];
}
function log(recId: string, msg: string, data?: unknown) {
const ts = new Date().toISOString();
if (data !== undefined) {
@@ -26,6 +53,124 @@ function log(recId: string, msg: string, data?: unknown) {
}
}
interface SubPipelineCtx {
recId: string;
interpreterOutput: InterpreterOutput;
mediaType: MediaType;
useWebSearch: boolean;
useValidator: boolean;
useHardRequirements: boolean;
brainstormCount: number;
previousFullMatches: string[];
allSeenTitles: Set<string>;
stagePrefix: string;
sseWrite: (event: SSEEvent) => void;
}
async function runSubPipeline(ctx: SubPipelineCtx): Promise<CuratorOutput[]> {
const {
recId, interpreterOutput, mediaType, useWebSearch, useValidator,
useHardRequirements, brainstormCount, previousFullMatches,
allSeenTitles, stagePrefix, sseWrite,
} = ctx;
const p = (stage: string) => (stagePrefix + stage) as SSEEvent['stage'];
// --- Retrieval (bucketed) ---
log(recId, `${stagePrefix}Retrieval: start`);
sseWrite({ stage: p('retrieval'), status: 'start' });
const t1 = Date.now();
const retrievalBucketCount = getBucketCount(brainstormCount);
const perBucketCount = Math.ceil(brainstormCount / retrievalBucketCount);
const retrievalBuckets = await Promise.all(
Array.from({ length: retrievalBucketCount }, () =>
runRetrieval(interpreterOutput, perBucketCount, mediaType, useWebSearch, useHardRequirements, previousFullMatches)
)
);
const allCandidates = retrievalBuckets.flatMap((r) => r.candidates);
const dedupedCandidates = deduplicateCandidates(allCandidates, allSeenTitles);
log(recId, `${stagePrefix}Retrieval: done (${Date.now() - t1}ms) — ${dedupedCandidates.length} candidates (${retrievalBucketCount} buckets, ${allCandidates.length} before dedup)`, {
titles: dedupedCandidates.map((c) => c.title),
});
sseWrite({ stage: p('retrieval'), status: 'done', data: { candidates: dedupedCandidates } });
// --- Validator (optional) ---
let candidatesForRanking = dedupedCandidates;
if (useValidator) {
log(recId, `${stagePrefix}Validator: start`);
sseWrite({ stage: p('validator'), status: 'start' });
const tV = Date.now();
const validatorOutput = await runValidator(dedupedCandidates, mediaType);
const verified = validatorOutput.candidates.filter((c) => !c.isTrash);
const trashCount = validatorOutput.candidates.length - verified.length;
candidatesForRanking = verified.map(({ title, reason }) => ({ title, reason }));
log(recId, `${stagePrefix}Validator: done (${Date.now() - tV}ms) — removed ${trashCount} trash entries`);
sseWrite({ stage: p('validator'), status: 'done', data: { removed: trashCount } });
} else {
sseWrite({ stage: p('validator'), status: 'done', data: { skipped: true } });
}
// --- Ranking (bucketed) ---
log(recId, `${stagePrefix}Ranking: start`);
sseWrite({ stage: p('ranking'), status: 'start' });
const t2 = Date.now();
const rankBucketCount = getBucketCount(candidatesForRanking.length);
const candidateBuckets = splitIntoBuckets(candidatesForRanking, rankBucketCount);
const rankingBuckets = await Promise.all(
candidateBuckets.map((bucket) =>
runRanking(interpreterOutput, { candidates: bucket }, mediaType, useHardRequirements)
)
);
const dedupTitles = (titles: string[]) => [...new Map(titles.map((t) => [t.toLowerCase(), t])).values()];
const rankingOutput: RankingOutput = {
full_match: dedupTitles(rankingBuckets.flatMap((r) => r.full_match)),
definitely_like: dedupTitles(rankingBuckets.flatMap((r) => r.definitely_like)),
might_like: dedupTitles(rankingBuckets.flatMap((r) => r.might_like)),
questionable: dedupTitles(rankingBuckets.flatMap((r) => r.questionable)),
will_not_like: dedupTitles(rankingBuckets.flatMap((r) => r.will_not_like)),
};
log(recId, `${stagePrefix}Ranking: done (${Date.now() - t2}ms) — ${rankBucketCount} buckets`, {
full_match: rankingOutput.full_match.length,
definitely_like: rankingOutput.definitely_like.length,
might_like: rankingOutput.might_like.length,
questionable: rankingOutput.questionable.length,
will_not_like: rankingOutput.will_not_like.length,
});
sseWrite({ stage: p('ranking'), status: 'done', data: rankingOutput });
// --- Curator (bucketed) ---
log(recId, `${stagePrefix}Curator: start`);
sseWrite({ stage: p('curator'), status: 'start' });
const t3 = Date.now();
type CategorizedItem = { title: string; category: keyof RankingOutput };
const categorizedItems: CategorizedItem[] = [
...rankingOutput.full_match.map((t) => ({ title: t, category: 'full_match' as const })),
...rankingOutput.definitely_like.map((t) => ({ title: t, category: 'definitely_like' as const })),
...rankingOutput.might_like.map((t) => ({ title: t, category: 'might_like' as const })),
...rankingOutput.questionable.map((t) => ({ title: t, category: 'questionable' as const })),
...rankingOutput.will_not_like.map((t) => ({ title: t, category: 'will_not_like' as const })),
];
const curatorBucketCount = getBucketCount(categorizedItems.length);
const curatorItemBuckets = splitIntoBuckets(categorizedItems, curatorBucketCount);
const curatorBucketRankings: RankingOutput[] = curatorItemBuckets.map((bucket) => ({
full_match: bucket.filter((i) => i.category === 'full_match').map((i) => i.title),
definitely_like: bucket.filter((i) => i.category === 'definitely_like').map((i) => i.title),
might_like: bucket.filter((i) => i.category === 'might_like').map((i) => i.title),
questionable: bucket.filter((i) => i.category === 'questionable').map((i) => i.title),
will_not_like: bucket.filter((i) => i.category === 'will_not_like').map((i) => i.title),
}));
const curatorBucketOutputs = await Promise.all(
curatorBucketRankings.map((ranking) =>
runCurator(ranking, interpreterOutput, mediaType, useWebSearch)
)
);
const curatorOutput = curatorBucketOutputs.reduce((acc, bucket) => mergeCuratorOutputs(acc, bucket), [] as CuratorOutput[]);
log(recId, `${stagePrefix}Curator: done (${Date.now() - t3}ms) — ${curatorOutput.length} items curated (${curatorBucketCount} buckets)`);
sseWrite({ stage: p('curator'), status: 'done', data: curatorOutput });
return curatorOutput;
}
export async function runPipeline(
rec: RecommendationRecord,
sseWrite: (event: SSEEvent) => void,
@@ -35,8 +180,11 @@ export async function runPipeline(
const startTime = Date.now();
const mediaType = (rec.media_type ?? 'tv_show') as MediaType;
const useWebSearch = rec.use_web_search ?? false;
const useValidator = rec.use_validator ?? false;
const useHardRequirements = rec.hard_requirements ?? false;
const selfExpansive = rec.self_expansive ?? false;
log(rec.id, `Starting pipeline for "${rec.title}" [${mediaType}${useWebSearch ? ', web_search' : ''}]${feedbackContext ? ' (with feedback context)' : ''}`);
log(rec.id, `Starting pipeline for "${rec.title}" [${mediaType}${useWebSearch ? ', web_search' : ''}${useValidator ? ', validator' : ''}${useHardRequirements ? ', hard_req' : ''}${selfExpansive ? `, expansive×${rec.expansive_passes}(${rec.expansive_mode})` : ''}]${feedbackContext ? ' (with feedback context)' : ''}`);
try {
// Set status to running
@@ -68,39 +216,69 @@ export async function runPipeline(
});
sseWrite({ stage: 'interpreter', status: 'done', data: interpreterOutput });
// --- Retrieval ---
// --- Pass 1: Retrieval → [Validator?] → Ranking → Curator ---
currentStage = 'retrieval';
log(rec.id, 'Retrieval: start');
sseWrite({ stage: 'retrieval', status: 'start' });
const t1 = Date.now();
const retrievalOutput = await runRetrieval(interpreterOutput, rec.brainstorm_count, mediaType, useWebSearch);
log(rec.id, `Retrieval: done (${Date.now() - t1}ms) — ${retrievalOutput.candidates.length} candidates`, {
titles: retrievalOutput.candidates.map((c) => c.title),
const allSeenTitles = new Set<string>();
const pass1Output = await runSubPipeline({
recId: rec.id,
interpreterOutput,
mediaType,
useWebSearch,
useValidator,
useHardRequirements,
brainstormCount: rec.brainstorm_count,
previousFullMatches: [],
allSeenTitles,
stagePrefix: '',
sseWrite: (event) => {
currentStage = event.stage;
sseWrite(event);
},
});
sseWrite({ stage: 'retrieval', status: 'done', data: retrievalOutput });
// --- Ranking ---
currentStage = 'ranking';
log(rec.id, 'Ranking: start');
sseWrite({ stage: 'ranking', status: 'start' });
const t2 = Date.now();
const rankingOutput = await runRanking(interpreterOutput, retrievalOutput, mediaType);
log(rec.id, `Ranking: done (${Date.now() - t2}ms)`, {
definitely_like: rankingOutput.definitely_like.length,
might_like: rankingOutput.might_like.length,
questionable: rankingOutput.questionable.length,
will_not_like: rankingOutput.will_not_like.length,
});
sseWrite({ stage: 'ranking', status: 'done', data: rankingOutput });
let mergedOutput = pass1Output;
// --- Curator ---
currentStage = 'curator';
log(rec.id, 'Curator: start');
sseWrite({ stage: 'curator', status: 'start' });
const t3 = Date.now();
const curatorOutput = await runCurator(rankingOutput, interpreterOutput, mediaType, useWebSearch);
log(rec.id, `Curator: done (${Date.now() - t3}ms) — ${curatorOutput.length} items curated`);
sseWrite({ stage: 'curator', status: 'done', data: curatorOutput });
// --- Self Expansive: extra passes ---
if (selfExpansive && rec.expansive_passes > 0) {
const allFullMatches = pass1Output
.filter((c) => c.category === 'Full Match')
.map((c) => c.title);
for (let i = 0; i < rec.expansive_passes; i++) {
const passNum = i + 2;
const passCount = rec.expansive_mode === 'extreme' ? rec.brainstorm_count : 60;
const passPrefix = `pass${passNum}:` as const;
log(rec.id, `Self Expansive Pass ${passNum}: start (${passCount} candidates, ${allFullMatches.length} full matches as context)`);
currentStage = `${passPrefix}retrieval` as SSEEvent['stage'];
const passOutput = await runSubPipeline({
recId: rec.id,
interpreterOutput,
mediaType,
useWebSearch,
useValidator,
useHardRequirements,
brainstormCount: passCount,
previousFullMatches: [...allFullMatches],
allSeenTitles,
stagePrefix: passPrefix,
sseWrite: (event) => {
currentStage = event.stage;
sseWrite(event);
},
});
mergedOutput = mergeCuratorOutputs(mergedOutput, passOutput);
const newFullMatches = passOutput
.filter((c) => c.category === 'Full Match')
.map((c) => c.title);
allFullMatches.push(...newFullMatches);
log(rec.id, `Self Expansive Pass ${passNum}: done — ${passOutput.length} new items, ${mergedOutput.length} total`);
}
}
// Generate AI title
let aiTitle: string = rec.title;
@@ -112,17 +290,27 @@ export async function runPipeline(
log(rec.id, `Title generation failed, keeping initial title: ${String(err)}`);
}
// Sort by category order before saving
const CATEGORY_ORDER: Record<string, number> = {
'Full Match': 0,
'Definitely Like': 1,
'Might Like': 2,
'Questionable': 3,
'Will Not Like': 4,
};
mergedOutput.sort((a, b) => (CATEGORY_ORDER[a.category] ?? 99) - (CATEGORY_ORDER[b.category] ?? 99));
// Save results to DB
log(rec.id, 'Saving results to DB');
await db
.update(recommendations)
.set({ recommendations: curatorOutput, status: 'done', title: aiTitle })
.set({ recommendations: mergedOutput, status: 'done', title: aiTitle })
.where(eq(recommendations.id, rec.id));
sseWrite({ stage: 'complete', status: 'done' });
sseWrite({ stage: 'complete', status: 'done', data: { title: aiTitle } });
log(rec.id, `Pipeline complete (total: ${Date.now() - startTime}ms)`);
return curatorOutput;
return mergedOutput;
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
log(rec.id, `Pipeline error at stage "${currentStage}": ${message}`);

View File

@@ -4,6 +4,7 @@ import { db } from '../db.js';
import { recommendations, feedback } from '../db/schema.js';
import { runPipeline } from '../pipelines/recommendation.js';
import type { MediaType, SSEEvent } from '../types/agents.js';
import { supportsWebSearch } from '../agent.js';
export default async function recommendationsRoute(fastify: FastifyInstance) {
// POST /recommendations — create record, return { id }
@@ -16,6 +17,11 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
brainstorm_count?: number;
media_type?: string;
use_web_search?: boolean;
use_validator?: boolean;
hard_requirements?: boolean;
self_expansive?: boolean;
expansive_passes?: number;
expansive_mode?: string;
};
const title = (body.main_prompt ?? '')
@@ -28,6 +34,12 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
const brainstorm_count = Number.isFinite(rawCount) ? Math.min(200, Math.max(50, rawCount)) : 100;
const media_type: MediaType = body.media_type === 'movie' ? 'movie' : 'tv_show';
const use_web_search = body.use_web_search === true;
const use_validator = body.use_validator === true && supportsWebSearch;
const hard_requirements = body.hard_requirements === true;
const self_expansive = body.self_expansive === true;
const rawPasses = Number(body.expansive_passes ?? 2);
const expansive_passes = Number.isFinite(rawPasses) ? Math.min(5, Math.max(1, rawPasses)) : 2;
const expansive_mode = body.expansive_mode === 'extreme' ? 'extreme' : 'soft';
const [rec] = await db
.insert(recommendations)
@@ -40,6 +52,11 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
brainstorm_count,
media_type,
use_web_search,
use_validator,
hard_requirements,
self_expansive,
expansive_passes,
expansive_mode,
status: 'pending',
})
.returning({ id: recommendations.id });
@@ -84,20 +101,7 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
if (!rec) return reply.code(404).send({ error: 'Not found' });
// Load all feedback to potentially inject as context
const feedbackRows = await db.select().from(feedback);
const mediaLabel = rec.media_type === 'movie' ? 'Movie' : 'Show';
const feedbackContext =
feedbackRows.length > 0
? feedbackRows
.map(
(f) =>
`${mediaLabel}: "${f.item_name}" — Rating: ${f.stars}/3 stars${f.feedback ? ` — Comment: ${f.feedback}` : ''}`,
)
.join('\n')
: undefined;
// Set SSE headers and hijack
// Set SSE headers and hijack before any branching
reply.raw.setHeader('Content-Type', 'text/event-stream');
reply.raw.setHeader('Cache-Control', 'no-cache');
reply.raw.setHeader('Connection', 'keep-alive');
@@ -105,17 +109,93 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
reply.raw.flushHeaders();
reply.hijack();
// Resilient write — swallows errors so a disconnected client never crashes
// an in-flight pipeline that is still running server-side.
const sseWrite = (event: SSEEvent) => {
reply.raw.write(`data: ${JSON.stringify(event)}\n\n`);
try {
reply.raw.write(`data: ${JSON.stringify(event)}\n\n`);
} catch {
// Client disconnected — pipeline continues, writes are silently dropped
}
};
try {
// Already finished — send a synthetic completion event and close immediately.
if (rec.status === 'done') {
sseWrite({ stage: 'complete', status: 'done', data: { title: rec.title } });
return;
}
// Already errored — send a synthetic error event and close immediately.
if (rec.status === 'error') {
sseWrite({ stage: 'curator', status: 'error', data: { message: 'Pipeline failed' } });
return;
}
// Already running — the pipeline is executing on a previous connection.
// Poll the DB until it reaches a terminal state, then report the result.
// This prevents starting a duplicate pipeline run on page reload.
if (rec.status === 'running') {
const POLL_INTERVAL_MS = 2000;
const TIMEOUT_MS = 20 * 60 * 1000; // 20 minutes hard ceiling
const start = Date.now();
while (Date.now() - start < TIMEOUT_MS) {
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
const [current] = await db
.select({ status: recommendations.status, title: recommendations.title })
.from(recommendations)
.where(eq(recommendations.id, id));
if (!current || current.status === 'done') {
sseWrite({ stage: 'complete', status: 'done', data: { title: current?.title ?? rec.title } });
return;
}
if (current.status === 'error') {
sseWrite({ stage: 'curator', status: 'error', data: { message: 'Pipeline failed' } });
return;
}
// Still running — keep polling
}
// Timed out waiting — report as error
sseWrite({ stage: 'curator', status: 'error', data: { message: 'Pipeline timed out' } });
return;
}
// status === 'pending' — start the pipeline normally.
// Load all feedback to potentially inject as context
const feedbackRows = await db.select().from(feedback);
const mediaLabel = rec.media_type === 'movie' ? 'Movie' : 'Show';
const feedbackContext =
feedbackRows.length > 0
? feedbackRows
.map(
(f) =>
`${mediaLabel}: "${f.item_name}" — Rating: ${f.stars}/3 stars${f.feedback ? ` — Comment: ${f.feedback}` : ''}`,
)
.join('\n')
: undefined;
await runPipeline(rec, sseWrite, feedbackContext);
} finally {
reply.raw.end();
}
});
// DELETE /recommendations/:id — delete a recommendation
fastify.delete('/recommendations/:id', async (request, reply) => {
const { id } = request.params as { id: string };
const [rec] = await db
.select({ id: recommendations.id })
.from(recommendations)
.where(eq(recommendations.id, id));
if (!rec) return reply.code(404).send({ error: 'Not found' });
await db.delete(recommendations).where(eq(recommendations.id, id));
return reply.send({ ok: true });
});
// POST /recommendations/:id/rerank — reset status so client can re-open SSE stream
fastify.post('/recommendations/:id/rerank', async (request, reply) => {
const { id } = request.params as { id: string };

View File

@@ -7,6 +7,7 @@ export interface InterpreterOutput {
character_preferences: string[];
tone: string[];
avoid: string[];
requirements: string[];
}
export interface RetrievalCandidate {
@@ -18,22 +19,47 @@ export interface RetrievalOutput {
candidates: RetrievalCandidate[];
}
export interface ValidatorCandidate {
title: string;
reason: string;
isTrash: boolean;
}
export interface ValidatorOutput {
candidates: ValidatorCandidate[];
}
export interface RankingOutput {
full_match: string[];
definitely_like: string[];
might_like: string[];
questionable: string[];
will_not_like: string[];
}
export type CuratorCategory = 'Definitely Like' | 'Might Like' | 'Questionable' | 'Will Not Like';
export type CuratorCategory = 'Full Match' | 'Definitely Like' | 'Might Like' | 'Questionable' | 'Will Not Like';
export interface CuratorOutput {
title: string;
explanation: string;
category: CuratorCategory;
genre: string;
pros: string[];
cons: string[];
}
export type PipelineStage = 'interpreter' | 'retrieval' | 'ranking' | 'curator' | 'complete';
export type PipelineStage =
| 'interpreter'
| 'retrieval'
| 'validator'
| 'ranking'
| 'curator'
| 'complete'
| `pass${number}:retrieval`
| `pass${number}:validator`
| `pass${number}:ranking`
| `pass${number}:curator`;
export type SSEStatus = 'start' | 'done' | 'error';
export interface SSEEvent {

View File

@@ -4,7 +4,9 @@ const BASE = '/api';
async function request<T>(path: string, options?: RequestInit): Promise<T> {
const res = await fetch(`${BASE}${path}`, {
headers: { 'Content-Type': 'application/json' },
headers: {
...(options?.body ? { 'Content-Type': 'application/json' } : {}),
},
...options,
});
if (!res.ok) {
@@ -22,6 +24,11 @@ export function createRecommendation(body: {
brainstorm_count?: number;
media_type: MediaType;
use_web_search?: boolean;
use_validator?: boolean;
hard_requirements?: boolean;
self_expansive?: boolean;
expansive_passes?: number;
expansive_mode?: 'soft' | 'extreme';
}): Promise<{ id: string }> {
return request('/recommendations', {
method: 'POST',
@@ -55,3 +62,7 @@ export function submitFeedback(body: {
export function getFeedback(): Promise<FeedbackEntry[]> {
return request('/feedback');
}
export function deleteRecommendation(id: string): Promise<{ ok: boolean }> {
return request(`/recommendations/${id}`, { method: 'DELETE' });
}

View File

@@ -57,6 +57,23 @@
color: #f87171;
}
.badge-magenta {
background: rgba(217, 70, 239, 0.15);
color: #e879f9;
}
.badge-verified {
background: rgba(34, 197, 94, 0.15);
color: #4ade80;
}
.card-badges {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-bottom: 10px;
}
.card-title {
font-size: 16px;
font-weight: 600;
@@ -67,7 +84,41 @@
font-size: 13px;
color: var(--text-muted);
line-height: 1.5;
margin-bottom: 12px;
margin-bottom: 8px;
}
.genre-badge {
font-size: 10px;
font-weight: 500;
background: rgba(148, 163, 184, 0.12);
color: var(--text-dim);
display: inline-block;
text-transform: none;
letter-spacing: 0;
}
.pros-cons-table {
display: flex;
gap: 16px;
margin-bottom: 10px;
font-size: 12px;
line-height: 1.5;
}
.pros-col,
.cons-col {
flex: 1;
display: flex;
flex-direction: column;
gap: 2px;
}
.pro-item {
color: #4ade80;
}
.con-item {
color: #f87171;
}
.card-feedback {
@@ -125,6 +176,8 @@
padding: 16px 0 32px;
display: flex;
justify-content: center;
gap: 12px;
flex-wrap: wrap;
}
.btn-rerank {

View File

@@ -244,4 +244,64 @@
.toggle-switch.on .toggle-knob {
transform: translateX(18px);
}
/* ── Disabled toggle ─────────────────────────────────────── */
.toggle-disabled {
opacity: 0.45;
cursor: not-allowed;
}
.toggle-switch-disabled {
cursor: not-allowed;
}
/* ── Self Expansive options ──────────────────────────────── */
.expansive-options {
padding: 12px 14px;
background: var(--bg-surface-2);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
display: flex;
flex-direction: column;
gap: 14px;
margin-top: -8px;
}
.mode-buttons {
display: flex;
gap: 8px;
}
.mode-btn {
flex: 1;
padding: 8px 0;
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
color: var(--text-muted);
font-size: 13px;
font-weight: 500;
cursor: pointer;
transition: background 0.15s, border-color 0.15s, color 0.15s;
font-family: inherit;
}
.mode-btn--active {
background: var(--accent-dim);
border-color: var(--accent);
color: var(--accent);
}
.mode-btn:hover:not(.mode-btn--active) {
background: var(--bg-surface-3);
border-color: var(--text-dim);
color: var(--text);
}
.mode-desc {
display: block;
margin-top: 4px;
}

View File

@@ -12,6 +12,11 @@ interface NewRecommendationModalProps {
brainstorm_count?: number;
media_type: MediaType;
use_web_search?: boolean;
use_validator?: boolean;
hard_requirements?: boolean;
self_expansive?: boolean;
expansive_passes?: number;
expansive_mode?: 'soft' | 'extreme';
}) => Promise<void>;
}
@@ -24,6 +29,11 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
const [themes, setThemes] = useState('');
const [brainstormCount, setBrainstormCount] = useState(100);
const [useWebSearch, setUseWebSearch] = useState(false);
const [useValidator, setUseValidator] = useState(false);
const [useHardRequirements, setUseHardRequirements] = useState(false);
const [selfExpansive, setSelfExpansive] = useState(false);
const [expansivePasses, setExpansivePasses] = useState(2);
const [expansiveMode, setExpansiveMode] = useState<'soft' | 'extreme'>('soft');
const [loading, setLoading] = useState(false);
const mediaLabel = mediaType === 'movie' ? 'Movie' : 'TV Show';
@@ -34,6 +44,12 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
setStep('form');
};
const handleWebSearchToggle = () => {
const next = !useWebSearch;
setUseWebSearch(next);
if (!next) setUseValidator(false);
};
const handleSubmit = async (e: Event) => {
e.preventDefault();
if (!mainPrompt.trim()) return;
@@ -47,6 +63,11 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
brainstorm_count: brainstormCount,
media_type: mediaType,
use_web_search: useWebSearch,
use_validator: useValidator,
hard_requirements: useHardRequirements,
self_expansive: selfExpansive,
expansive_passes: selfExpansive ? expansivePasses : 1,
expansive_mode: expansiveMode,
});
onClose();
} finally {
@@ -165,12 +186,96 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
<span class="toggle-title">Web Search</span>
<span class="toggle-desc">Use real-time web search for more accurate and up-to-date {mediaPluralLabel}</span>
</div>
<div class={`toggle-switch${useWebSearch ? ' on' : ''}`} onClick={() => setUseWebSearch((v) => !v)}>
<div class={`toggle-switch${useWebSearch ? ' on' : ''}`} onClick={handleWebSearchToggle}>
<div class="toggle-knob" />
</div>
</label>
</div>
<div class="form-group-toggle">
<label class={`toggle-label${!useWebSearch ? ' toggle-disabled' : ''}`}>
<div class="toggle-text">
<span class="toggle-title">Validator Agent</span>
<span class="toggle-desc">
Verify candidates against real {mediaPluralLabel} metadata using web search
{!useWebSearch && ' (requires Web Search)'}
</span>
</div>
<div
class={`toggle-switch${useValidator ? ' on' : ''}${!useWebSearch ? ' toggle-switch-disabled' : ''}`}
onClick={() => useWebSearch && setUseValidator((v) => !v)}
>
<div class="toggle-knob" />
</div>
</label>
</div>
<div class="form-group-toggle">
<label class="toggle-label">
<div class="toggle-text">
<span class="toggle-title">Hard Requirements</span>
<span class="toggle-desc">Strictly enforce all specified requirements when generating and ranking</span>
</div>
<div class={`toggle-switch${useHardRequirements ? ' on' : ''}`} onClick={() => setUseHardRequirements((v) => !v)}>
<div class="toggle-knob" />
</div>
</label>
</div>
<div class="form-group-toggle">
<label class="toggle-label">
<div class="toggle-text">
<span class="toggle-title">Self Expansive Mode</span>
<span class="toggle-desc">Re-run the pipeline using Full Match results to discover more great {mediaPluralLabel}</span>
</div>
<div class={`toggle-switch${selfExpansive ? ' on' : ''}`} onClick={() => setSelfExpansive((v) => !v)}>
<div class="toggle-knob" />
</div>
</label>
</div>
{selfExpansive && (
<div class="expansive-options">
<div class="form-group">
<label for="expansive-passes">Extra passes ({expansivePasses})</label>
<input
id="expansive-passes"
type="range"
class="form-input"
min={1}
max={5}
step={1}
value={expansivePasses}
onInput={(e) => setExpansivePasses(Number((e.target as HTMLInputElement).value))}
/>
</div>
<div class="form-group">
<label>Mode</label>
<div class="mode-buttons">
<button
type="button"
class={`mode-btn${expansiveMode === 'soft' ? ' mode-btn--active' : ''}`}
onClick={() => setExpansiveMode('soft')}
>
Soft
</button>
<button
type="button"
class={`mode-btn${expansiveMode === 'extreme' ? ' mode-btn--active' : ''}`}
onClick={() => setExpansiveMode('extreme')}
>
Extreme
</button>
</div>
<span class="toggle-desc mode-desc">
{expansiveMode === 'soft'
? 'Each extra pass brainstorms 60 new candidates in 2 buckets'
: `Each extra pass brainstorms ${brainstormCount} new candidates (same as main pass)`}
</span>
</div>
</div>
)}
<div class="modal-actions">
<button type="button" class="btn-secondary" onClick={onClose} disabled={loading}>
Cancel

View File

@@ -84,3 +84,30 @@
.pipeline-step-label {
font-weight: 500;
}
.pipeline-retry {
margin-top: 20px;
}
.pipeline-step--skipped {
border-color: var(--border);
background: var(--bg-surface);
opacity: 0.45;
}
.stage-skipped {
color: var(--text-dim);
}
.pipeline-pass-group--extra {
margin-top: 20px;
}
.pipeline-pass-label {
font-size: 11px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.06em;
color: var(--text-dim);
margin-bottom: 8px;
}

View File

@@ -1,15 +1,20 @@
import './PipelineProgress.css';
import type { StageMap, StageStatus } from '../types/index.js';
const STAGES: { key: keyof StageMap; label: string }[] = [
{ key: 'interpreter', label: 'Interpreting Preferences' },
{ key: 'retrieval', label: 'Generating Candidates' },
{ key: 'ranking', label: 'Ranking Candidates' },
{ key: 'curator', label: 'Curating Results' },
];
export interface StageEntry {
key: string;
label: string;
}
export interface StageGroup {
label: string;
stages: StageEntry[];
}
interface PipelineProgressProps {
stageGroups: StageGroup[];
stages: StageMap;
onRetry?: () => void;
}
function StageIcon({ status }: { status: StageStatus }) {
@@ -20,23 +25,40 @@ function StageIcon({ status }: { status: StageStatus }) {
return <span class="stage-icon stage-error"></span>;
case 'running':
return <span class="stage-icon stage-running spinner"></span>;
case 'skipped':
return <span class="stage-icon stage-skipped"></span>;
default:
return <span class="stage-icon stage-pending"></span>;
}
}
export function PipelineProgress({ stages }: PipelineProgressProps) {
export function PipelineProgress({ stageGroups, stages, onRetry }: PipelineProgressProps) {
const hasError = Object.values(stages).some((s) => s === 'error');
return (
<div class="pipeline-progress">
<h3 class="pipeline-title">Generating Recommendations</h3>
<ul class="pipeline-steps">
{STAGES.map(({ key, label }) => (
<li key={key} class={`pipeline-step pipeline-step--${stages[key]}`}>
<StageIcon status={stages[key]} />
<span class="pipeline-step-label">{label}</span>
</li>
))}
</ul>
<h3 class="pipeline-title">{hasError ? 'Pipeline Failed' : 'Generating Recommendations…'}</h3>
{stageGroups.map((group, gi) => (
<div key={gi} class={`pipeline-pass-group${gi > 0 ? ' pipeline-pass-group--extra' : ''}`}>
{group.label && <div class="pipeline-pass-label">{group.label}</div>}
<ul class="pipeline-steps">
{group.stages.map(({ key, label }) => {
const status: StageStatus = stages[key] ?? 'pending';
return (
<li key={key} class={`pipeline-step pipeline-step--${status}`}>
<StageIcon status={status} />
<span class="pipeline-step-label">{label}</span>
</li>
);
})}
</ul>
</div>
))}
{hasError && onRetry && (
<div class="pipeline-retry">
<button class="btn-primary" onClick={onRetry}>Re-run Pipeline</button>
</div>
)}
</div>
);
}

View File

@@ -4,19 +4,22 @@ import type { CuratorOutput, CuratorCategory } from '../types/index.js';
interface RecommendationCardProps {
show: CuratorOutput;
verified?: boolean;
existingFeedback?: { stars: number; feedback: string };
onFeedback: (item_name: string, stars: number, feedback: string) => Promise<void>;
}
const CATEGORY_COLORS: Record<CuratorCategory, string> = {
'Full Match': 'badge-magenta',
'Definitely Like': 'badge-green',
'Might Like': 'badge-blue',
'Questionable': 'badge-yellow',
'Will Not Like': 'badge-red',
};
export function RecommendationCard({ show, existingFeedback, onFeedback }: RecommendationCardProps) {
export function RecommendationCard({ show, verified, existingFeedback, onFeedback }: RecommendationCardProps) {
const [selectedStars, setSelectedStars] = useState(existingFeedback?.stars ?? 0);
const [hoverStar, setHoverStar] = useState(0);
const [comment, setComment] = useState(existingFeedback?.feedback ?? '');
const [showComment, setShowComment] = useState(false);
const [submitted, setSubmitted] = useState(!!existingFeedback);
@@ -47,18 +50,39 @@ export function RecommendationCard({ show, existingFeedback, onFeedback }: Recom
</div>
<p class="card-explanation">{show.explanation}</p>
<div class="card-badges">
{show.genre && <span class="badge genre-badge">{show.genre}</span>}
{verified && <span class="badge badge-verified">Verified</span>}
</div>
{(show.pros?.length > 0 || show.cons?.length > 0) && (
<div class="pros-cons-table">
<div class="pros-col">
{show.pros?.map((p) => <span class="pro-item">+ {p}</span>)}
</div>
<div class="cons-col">
{show.cons?.map((c) => <span class="con-item">- {c}</span>)}
</div>
</div>
)}
<div class="card-feedback">
<div class="star-rating">
{[1, 2, 3].map((star) => (
<button
key={star}
class={`star-btn${selectedStars >= star ? ' star-active' : ''}`}
onClick={() => handleStarClick(star)}
aria-label={`Rate ${star} star${star > 1 ? 's' : ''}`}
>
{selectedStars >= star ? '★' : '☆'}
</button>
))}
{[1, 2, 3].map((star) => {
const effective = hoverStar || selectedStars;
return (
<button
key={star}
class={`star-btn${effective >= star ? ' star-active' : ''}`}
onClick={() => handleStarClick(star)}
onMouseEnter={() => setHoverStar(star)}
onMouseLeave={() => setHoverStar(0)}
aria-label={`Rate ${star} star${star > 1 ? 's' : ''}`}
>
{effective >= star ? '★' : '☆'}
</button>
);
})}
{submitted && <span class="feedback-saved">Saved</span>}
</div>

View File

@@ -142,4 +142,25 @@
.sidebar-type-movie {
background: rgba(245, 158, 11, 0.15);
color: #fbbf24;
}
.sidebar-item-delete {
flex-shrink: 0;
background: none;
border: none;
color: var(--text-dim);
cursor: pointer;
font-size: 14px;
padding: 0 2px;
line-height: 1;
opacity: 0;
transition: opacity 0.15s, color 0.15s;
}
.sidebar-item:hover .sidebar-item-delete {
opacity: 1;
}
.sidebar-item-delete:hover {
color: var(--red);
}

View File

@@ -6,6 +6,7 @@ import {
rerankRecommendation,
submitFeedback,
getFeedback,
deleteRecommendation,
} from '../api/client.js';
export function useRecommendations() {
@@ -37,6 +38,11 @@ export function useRecommendations() {
brainstorm_count?: number;
media_type: MediaType;
use_web_search?: boolean;
use_validator?: boolean;
hard_requirements?: boolean;
self_expansive?: boolean;
expansive_passes?: number;
expansive_mode?: 'soft' | 'extreme';
}) => {
const { id } = await createRecommendation(body);
await refreshList();
@@ -71,6 +77,15 @@ export function useRecommendations() {
[],
);
const updateTitle = useCallback((id: string, title: string) => {
setList((prev) => prev.map((r) => (r.id === id ? { ...r, title } : r)));
}, []);
const deleteRec = useCallback(async (id: string) => {
await deleteRecommendation(id);
setList((prev) => prev.filter((r) => r.id !== id));
}, []);
return {
list,
selectedId,
@@ -80,6 +95,8 @@ export function useRecommendations() {
rerank,
submitFeedback: handleSubmitFeedback,
updateStatus,
updateTitle,
refreshList,
deleteRec,
};
}

View File

@@ -4,10 +4,13 @@ import type { SSEEvent } from '../types/index.js';
export function useSSE(
url: string | null,
onEvent: (event: SSEEvent) => void,
onClose?: () => void,
): { close: () => void } {
const esRef = useRef<EventSource | null>(null);
const onEventRef = useRef(onEvent);
onEventRef.current = onEvent;
const onCloseRef = useRef(onClose);
onCloseRef.current = onClose;
useEffect(() => {
if (!url) return;
@@ -32,6 +35,7 @@ export function useSSE(
es.onerror = () => {
es.close();
esRef.current = null;
onCloseRef.current?.();
};
return () => {

View File

@@ -95,6 +95,46 @@
color: var(--accent);
}
.rec-info-delete-row {
padding-top: 4px;
align-items: center;
}
/* Shared sizing for the two action buttons in the info panel */
.btn-danger,
.rec-info-delete-row .btn-rerun {
padding: 5px 14px;
font-size: 12px;
font-weight: 600;
border-radius: var(--radius);
border: 1px solid;
background: none;
cursor: pointer;
transition: background 0.15s, border-color 0.15s;
}
.btn-danger {
border-color: rgba(239, 68, 68, 0.4);
color: #f87171;
}
.btn-danger:hover {
background: rgba(239, 68, 68, 0.12);
border-color: #f87171;
}
/* Re-run button — same shape as Delete, neutral tone */
.rec-info-delete-row .btn-rerun {
border-color: var(--border);
color: var(--text-muted);
}
.rec-info-delete-row .btn-rerun:hover {
background: var(--bg-surface-2);
border-color: var(--text-dim);
color: var(--text);
}
.error-state {
color: var(--text-muted);
}

View File

@@ -2,47 +2,92 @@ import { useState, useCallback, useEffect } from 'preact/hooks';
import './Recom.css';
import { route } from 'preact-router';
import { PipelineProgress } from '../components/PipelineProgress.js';
import type { StageGroup } from '../components/PipelineProgress.js';
import { RecommendationCard } from '../components/RecommendationCard.js';
import { Sidebar } from '../components/Sidebar.js';
import { NewRecommendationModal } from '../components/NewRecommendationModal.js';
import { useRecommendationsContext } from '../context/RecommendationsContext.js';
import { useSSE } from '../hooks/useSSE.js';
import { getRecommendation } from '../api/client.js';
import type { Recommendation, SSEEvent, StageMap, PipelineStage } from '../types/index.js';
import type { Recommendation, SSEEvent, StageMap, StageStatus } from '../types/index.js';
interface RecomProps {
id: string;
path?: string;
}
const DEFAULT_STAGES: StageMap = {
interpreter: 'pending',
retrieval: 'pending',
ranking: 'pending',
curator: 'pending',
};
function buildDefaultStages(rec: Recommendation | null): StageMap {
const map: StageMap = {
interpreter: 'pending',
retrieval: 'pending',
validator: 'pending',
ranking: 'pending',
curator: 'pending',
};
if (rec?.self_expansive && rec.expansive_passes > 0) {
for (let i = 0; i < rec.expansive_passes; i++) {
const p = i + 2;
map[`pass${p}:retrieval`] = 'pending';
if (rec.use_validator) map[`pass${p}:validator`] = 'pending';
map[`pass${p}:ranking`] = 'pending';
map[`pass${p}:curator`] = 'pending';
}
}
return map;
}
const STAGE_ORDER: (keyof StageMap)[] = ['interpreter', 'retrieval', 'ranking', 'curator'];
function buildStageGroups(rec: Recommendation | null): StageGroup[] {
const baseStages = [
{ key: 'interpreter', label: 'Interpreting Preferences' },
{ key: 'retrieval', label: 'Generating Candidates' },
{ key: 'validator', label: 'Validating Candidates' },
{ key: 'ranking', label: 'Ranking Candidates' },
{ key: 'curator', label: 'Curating Results' },
];
const groups: StageGroup[] = [{ label: '', stages: baseStages }];
if (rec?.self_expansive && rec.expansive_passes > 0) {
for (let i = 0; i < rec.expansive_passes; i++) {
const p = i + 2;
groups.push({
label: `Pass ${p}`,
stages: [
{ key: `pass${p}:retrieval`, label: 'Generating Candidates' },
...(rec.use_validator ? [{ key: `pass${p}:validator`, label: 'Validating Candidates' }] : []),
{ key: `pass${p}:ranking`, label: 'Ranking Candidates' },
{ key: `pass${p}:curator`, label: 'Curating Results' },
],
});
}
}
return groups;
}
export function Recom({ id }: RecomProps) {
const { list, feedback, submitFeedback, rerank, updateStatus, refreshList, createNew } = useRecommendationsContext();
const { list, feedback, submitFeedback, rerank, updateStatus, updateTitle, refreshList, createNew, deleteRec } = useRecommendationsContext();
const [rec, setRec] = useState<Recommendation | null>(null);
const [stages, setStages] = useState<StageMap>(DEFAULT_STAGES);
const [sseUrl, setSseUrl] = useState<string | null>(null);
const [stages, setStages] = useState<StageMap>(buildDefaultStages(null));
// sseKey drives the SSE connection. null = inactive; a number = active.
// Using a timestamp nonce ensures the URL is always unique on (re)connect,
// so useSSE's useEffect always re-runs even if the base path hasn't changed.
const [sseKey, setSseKey] = useState<number | null>(null);
const [showModal, setShowModal] = useState(false);
const [infoExpanded, setInfoExpanded] = useState(true);
// Derive the actual URL from the key; query param is ignored by the server
// but makes the string unique so React/Preact state always treats it as changed.
const sseUrl = sseKey !== null ? `/api/recommendations/${id}/stream?_k=${sseKey}` : null;
useEffect(() => {
setRec(null);
setStages(DEFAULT_STAGES);
setSseUrl(null);
setStages(buildDefaultStages(null));
setSseKey(null);
getRecommendation(id)
.then((data) => {
setRec(data);
if (data.status === 'running' || data.status === 'pending') {
setStages(DEFAULT_STAGES);
setSseUrl(`/api/recommendations/${id}/stream`);
setStages(buildDefaultStages(data));
setSseKey(Date.now());
}
})
.catch(() => route('/'));
@@ -51,41 +96,89 @@ export function Recom({ id }: RecomProps) {
const handleSSEEvent = useCallback(
(event: SSEEvent) => {
if (event.stage !== 'complete') {
const stageKey = event.stage as keyof StageMap;
if (STAGE_ORDER.includes(stageKey)) {
setStages((prev) => ({
...prev,
[stageKey]: event.status === 'start' ? 'running' : event.status === 'done' ? 'done' : 'error',
}));
}
const stageKey = event.stage as string;
setStages((prev) => {
if (!(stageKey in prev)) return prev;
const eventData = event.data as { skipped?: boolean } | undefined;
let newStatus: StageStatus;
if (event.status === 'start') {
newStatus = 'running';
} else if (event.status === 'done') {
newStatus = eventData?.skipped ? 'skipped' : 'done';
} else {
newStatus = 'error';
}
return { ...prev, [stageKey]: newStatus };
});
}
if (event.stage === 'complete' && event.status === 'done') {
setSseUrl(null);
const incoming = event.data as { title?: string } | undefined;
if (incoming?.title) {
updateTitle(id, incoming.title);
setRec((prev) => (prev ? { ...prev, title: incoming.title! } : prev));
}
setSseKey(null);
updateStatus(id, 'done');
void getRecommendation(id).then(setRec);
void getRecommendation(id)
.then(setRec)
.catch(() => {
// Fetch failed after completion — poll once more via handleSSEClose logic
updateStatus(id, 'done');
});
void refreshList();
}
if (event.status === 'error') {
setSseUrl(null);
setSseKey(null);
updateStatus(id, 'error');
const stageKey = event.stage as PipelineStage;
setRec((prev) => (prev ? { ...prev, status: 'error' as const } : null));
const stageKey = event.stage as string;
if (stageKey !== 'complete') {
setStages((prev) => ({ ...prev, [stageKey as keyof StageMap]: 'error' }));
setStages((prev) => (stageKey in prev ? { ...prev, [stageKey]: 'error' } : prev));
}
}
},
[id, updateStatus, refreshList],
[id, updateStatus, updateTitle, refreshList],
);
useSSE(sseUrl, handleSSEEvent);
const handleSSEClose = useCallback(() => {
void getRecommendation(id).then((data) => {
if (data.status === 'done') {
setSseKey(null);
setRec(data);
const allDone = Object.fromEntries(
Object.keys(buildDefaultStages(data)).map((k) => [k, 'done' as StageStatus])
);
setStages(allDone);
updateStatus(id, 'done');
void refreshList();
} else if (data.status === 'error') {
setSseKey(null);
setRec(data);
updateStatus(id, 'error');
} else {
// Pipeline still running — reconnect with a new unique key so that
// useSSE's useEffect always re-fires even if the base URL is the same.
setSseKey(Date.now());
}
});
}, [id, updateStatus, refreshList]);
useSSE(sseUrl, handleSSEEvent, handleSSEClose);
const handleRetry = async () => {
await rerank(id);
setStages(buildDefaultStages(rec));
setSseKey(Date.now());
setRec((prev) => (prev ? { ...prev, status: 'pending' as const } : null));
};
const handleRerank = async () => {
await rerank(id);
setStages(DEFAULT_STAGES);
setSseUrl(`/api/recommendations/${id}/stream`);
setRec((prev) => (prev ? { ...prev, status: 'pending' } : null));
setStages(buildDefaultStages(rec));
setSseKey(Date.now());
setRec((prev) => (prev ? { ...prev, status: 'pending' as const } : null));
};
const handleCreateNew = async (body: {
@@ -96,13 +189,19 @@ export function Recom({ id }: RecomProps) {
brainstorm_count?: number;
media_type: import('../types/index.js').MediaType;
use_web_search?: boolean;
use_validator?: boolean;
hard_requirements?: boolean;
self_expansive?: boolean;
expansive_passes?: number;
expansive_mode?: 'soft' | 'extreme';
}) => {
const newId = await createNew(body);
route(`/recom/${newId}`);
};
const isRunning = rec?.status === 'running' || rec?.status === 'pending' || !!sseUrl;
const isRunning = rec?.status === 'running' || rec?.status === 'pending' || sseKey !== null;
const feedbackMap = new Map(feedback.map((f) => [f.item_name, f]));
const stageGroups = buildStageGroups(rec);
return (
<div class="layout">
@@ -158,6 +257,41 @@ export function Recom({ id }: RecomProps) {
<span class="rec-info-badge">enabled</span>
</div>
)}
{rec.use_validator && (
<div class="rec-info-row">
<span class="rec-info-label">Validator</span>
<span class="rec-info-badge">enabled</span>
</div>
)}
{rec.hard_requirements && (
<div class="rec-info-row">
<span class="rec-info-label">Hard Req.</span>
<span class="rec-info-badge">enabled</span>
</div>
)}
{rec.self_expansive && (
<div class="rec-info-row">
<span class="rec-info-label">Self Expansive</span>
<span class="rec-info-badge">{rec.expansive_passes} pass{rec.expansive_passes !== 1 ? 'es' : ''} · {rec.expansive_mode}</span>
</div>
)}
<div class="rec-info-row rec-info-delete-row">
{!isRunning && (
<button class="btn-rerun btn-rerank" onClick={handleRetry}>
Re-run Pipeline
</button>
)}
<button
class="btn-danger"
onClick={() => {
if (confirm('Delete this recommendation?')) {
void deleteRec(id).then(() => route('/'));
}
}}
>
Delete
</button>
</div>
</div>
)}
</div>
@@ -166,7 +300,7 @@ export function Recom({ id }: RecomProps) {
{isRunning && (
<div class="content-area">
<PipelineProgress stages={stages} />
<PipelineProgress stageGroups={stageGroups} stages={stages} onRetry={handleRetry} />
</div>
)}
@@ -177,6 +311,7 @@ export function Recom({ id }: RecomProps) {
<RecommendationCard
key={show.title}
show={show}
verified={rec.use_validator}
existingFeedback={feedbackMap.get(show.title)}
onFeedback={async (name, stars, comment) => {
await submitFeedback({ item_name: name, stars, feedback: comment });
@@ -200,9 +335,9 @@ export function Recom({ id }: RecomProps) {
{!isRunning && rec?.status === 'error' && (
<div class="content-area error-state">
<h2>Something went wrong</h2>
<p>The pipeline encountered an error. You can try again by clicking Re-rank.</p>
<button class="btn-primary" onClick={handleRerank}>
Try Again
<p>The pipeline encountered an error. You can try again by clicking the button below.</p>
<button class="btn-primary" onClick={handleRetry}>
Re-run Pipeline
</button>
</div>
)}

View File

@@ -1,11 +1,14 @@
export type MediaType = 'tv_show' | 'movie';
export type CuratorCategory = 'Definitely Like' | 'Might Like' | 'Questionable' | 'Will Not Like';
export type CuratorCategory = 'Full Match' | 'Definitely Like' | 'Might Like' | 'Questionable' | 'Will Not Like';
export interface CuratorOutput {
title: string;
explanation: string;
category: CuratorCategory;
genre: string;
pros: string[];
cons: string[];
}
export type RecommendationStatus = 'pending' | 'running' | 'done' | 'error';
@@ -19,6 +22,11 @@ export interface Recommendation {
themes: string;
media_type: MediaType;
use_web_search: boolean;
use_validator: boolean;
hard_requirements: boolean;
self_expansive: boolean;
expansive_passes: number;
expansive_mode: 'soft' | 'extreme';
recommendations: CuratorOutput[] | null;
status: RecommendationStatus;
created_at: string;
@@ -40,7 +48,18 @@ export interface FeedbackEntry {
created_at: string;
}
export type PipelineStage = 'interpreter' | 'retrieval' | 'ranking' | 'curator' | 'complete';
export type PipelineStage =
| 'interpreter'
| 'retrieval'
| 'validator'
| 'ranking'
| 'curator'
| 'complete'
| `pass${number}:retrieval`
| `pass${number}:validator`
| `pass${number}:ranking`
| `pass${number}:curator`;
export type SSEStatus = 'start' | 'done' | 'error';
export interface SSEEvent {
@@ -49,6 +68,6 @@ export interface SSEEvent {
data?: unknown;
}
export type StageStatus = 'pending' | 'running' | 'done' | 'error';
export type StageStatus = 'pending' | 'running' | 'done' | 'error' | 'skipped';
export type StageMap = Record<Exclude<PipelineStage, 'complete'>, StageStatus>;
export type StageMap = Record<string, StageStatus>;