Compare commits

...

24 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
749ae42acb small changes
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 41s
2026-03-28 11:13:58 -03:00
28204d29ab lol
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 10s
2026-03-26 22:21:59 -03:00
deb3a89f56 fixing migrate
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-03-26 22:03:45 -03:00
c730a53dd0 fixing pipe
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 10s
2026-03-26 21:18:51 -03:00
1437092a42 adding movies & web search tool
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 4m0s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 12s
2026-03-26 20:35:22 -03:00
6fdfc3797a adding web search to all recs 2026-03-26 20:13:31 -03:00
f9f3d95406 feature: new changes!
All checks were successful
Recommender Build and Deploy (internal) / Build Recommender Image (push) Successful in 3m59s
Recommender Build and Deploy (internal) / Deploy Recommender (internal) (push) Successful in 10s
2026-03-25 20:09:32 -03:00
26f61077b8 changess 2026-03-25 19:46:14 -03:00
48 changed files with 2839 additions and 955 deletions

View File

@@ -7,7 +7,7 @@ COPY package.json package-lock.json ./
COPY packages/backend/package.json ./packages/backend/package.json COPY packages/backend/package.json ./packages/backend/package.json
COPY packages/frontend/package.json ./packages/frontend/package.json COPY packages/frontend/package.json ./packages/frontend/package.json
RUN npm ci --ignore-scripts RUN npm ci
# ─── Stage 2: Build the Preact frontend ────────────────────────────────────── # ─── Stage 2: Build the Preact frontend ──────────────────────────────────────
FROM deps AS build-frontend FROM deps AS build-frontend

View File

@@ -18,7 +18,7 @@ spec:
image: git.ivanch.me/ivanch/recommender:latest image: git.ivanch.me/ivanch/recommender:latest
imagePullPolicy: Always imagePullPolicy: Always
ports: ports:
- containerPort: 8080 - containerPort: 80
env: env:
- name: OPENAI_API_KEY - name: OPENAI_API_KEY
valueFrom: valueFrom:
@@ -30,6 +30,17 @@ spec:
secretKeyRef: secretKeyRef:
name: recommender-secrets name: recommender-secrets
key: DATABASE_URL 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: resources:
requests: requests:
memory: "256Mi" memory: "256Mi"
@@ -48,7 +59,7 @@ spec:
app: recommender app: recommender
ports: ports:
- port: 80 - port: 80
targetPort: 8080 targetPort: 80
--- ---
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress

View File

@@ -4,5 +4,8 @@ set -e
# Start Nginx in the background # Start Nginx in the background
nginx & nginx &
# Run migrations
node /app/node_modules/.bin/tsx /app/packages/backend/src/migrate.ts
# Start the Node.js backend # Start the Node.js backend
exec node /app/node_modules/.bin/tsx /app/packages/backend/src/index.ts exec node /app/node_modules/.bin/tsx /app/packages/backend/src/index.ts

24
package-lock.json generated
View File

@@ -3822,6 +3822,15 @@
"url": "https://opencollective.com/preact" "url": "https://opencollective.com/preact"
} }
}, },
"node_modules/preact-router": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/preact-router/-/preact-router-4.1.2.tgz",
"integrity": "sha512-uICUaUFYh+XQ+6vZtQn1q+X6rSqwq+zorWOCLWPF5FAsQh3EJ+RsDQ9Ee+fjk545YWQHfUxhrBAaemfxEnMOUg==",
"license": "MIT",
"peerDependencies": {
"preact": ">=10"
}
},
"node_modules/process-warning": { "node_modules/process-warning": {
"version": "5.0.0", "version": "5.0.0",
"resolved": "https://registry.npmjs.org/process-warning/-/process-warning-5.0.0.tgz", "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-5.0.0.tgz",
@@ -4446,6 +4455,15 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/zod": {
"version": "4.3.6",
"resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz",
"integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
},
"packages/backend": { "packages/backend": {
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
@@ -4454,7 +4472,8 @@
"drizzle-orm": "^0.45.1", "drizzle-orm": "^0.45.1",
"fastify": "^5.8.4", "fastify": "^5.8.4",
"openai": "^6.32.0", "openai": "^6.32.0",
"postgres": "^3.4.8" "postgres": "^3.4.8",
"zod": "^4.3.6"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^24.12.0", "@types/node": "^24.12.0",
@@ -4466,7 +4485,8 @@
"packages/frontend": { "packages/frontend": {
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"preact": "^10.29.0" "preact": "^10.29.0",
"preact-router": "^4.1.2"
}, },
"devDependencies": { "devDependencies": {
"@preact/preset-vite": "^2.10.4", "@preact/preset-vite": "^2.10.4",

View File

@@ -3,4 +3,14 @@
# In production / Docker, supply these as environment variables. # In production / Docker, supply these as environment variables.
DATABASE_URL=postgres://user:password@localhost:5432/recommender 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 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

@@ -1,7 +1,7 @@
import { defineConfig } from 'drizzle-kit'; import { defineConfig } from 'drizzle-kit';
import * as dotenv from 'dotenv'; import * as dotenv from 'dotenv';
dotenv.config(); dotenv.config({ path: ['.env.local', '.env'] });
export default defineConfig({ export default defineConfig({
schema: './src/db/schema.ts', schema: './src/db/schema.ts',

View File

@@ -0,0 +1,22 @@
CREATE TABLE IF NOT EXISTS "feedback" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"tv_show_name" text NOT NULL,
"stars" integer NOT NULL,
"feedback" text DEFAULT '' NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "recommendations" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"title" text NOT NULL,
"main_prompt" text NOT NULL,
"liked_shows" text DEFAULT '' NOT NULL,
"disliked_shows" text DEFAULT '' NOT NULL,
"themes" text DEFAULT '' NOT NULL,
"brainstorm_count" integer DEFAULT 100 NOT NULL,
"recommendations" jsonb,
"status" text DEFAULT 'pending' NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "feedback_tv_show_name_idx" ON "feedback" USING btree ("tv_show_name");

View File

@@ -0,0 +1,9 @@
ALTER TABLE "recommendations" ADD COLUMN "media_type" text DEFAULT 'tv_show' NOT NULL;
--> statement-breakpoint
ALTER TABLE "recommendations" ADD COLUMN "use_web_search" boolean DEFAULT false NOT NULL;
--> statement-breakpoint
ALTER TABLE "feedback" RENAME COLUMN "tv_show_name" TO "item_name";
--> statement-breakpoint
DROP INDEX "feedback_tv_show_name_idx";
--> statement-breakpoint
CREATE UNIQUE INDEX "feedback_item_name_idx" ON "feedback" USING btree ("item_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

@@ -0,0 +1,161 @@
{
"id": "4ca81e42-26b0-46f5-988e-957360067861",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.feedback": {
"name": "feedback",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"tv_show_name": {
"name": "tv_show_name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"stars": {
"name": "stars",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"feedback": {
"name": "feedback",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "''"
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"feedback_tv_show_name_idx": {
"name": "feedback_tv_show_name_idx",
"columns": [
{
"expression": "tv_show_name",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.recommendations": {
"name": "recommendations",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": true
},
"main_prompt": {
"name": "main_prompt",
"type": "text",
"primaryKey": false,
"notNull": true
},
"liked_shows": {
"name": "liked_shows",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "''"
},
"disliked_shows": {
"name": "disliked_shows",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "''"
},
"themes": {
"name": "themes",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "''"
},
"brainstorm_count": {
"name": "brainstorm_count",
"type": "integer",
"primaryKey": false,
"notNull": true,
"default": 100
},
"recommendations": {
"name": "recommendations",
"type": "jsonb",
"primaryKey": false,
"notNull": false
},
"status": {
"name": "status",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "'pending'"
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,20 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1774479321371,
"tag": "0000_wild_joseph",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1774900000000,
"tag": "0001_add_media_type_web_search",
"breakpoints": true
}
]
}

View File

@@ -5,7 +5,8 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"dev": "tsx watch src/index.ts" "dev": "tsx watch src/index.ts",
"migrate": "tsx src/migrate.ts"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
@@ -16,7 +17,8 @@
"drizzle-orm": "^0.45.1", "drizzle-orm": "^0.45.1",
"fastify": "^5.8.4", "fastify": "^5.8.4",
"openai": "^6.32.0", "openai": "^6.32.0",
"postgres": "^3.4.8" "postgres": "^3.4.8",
"zod": "^4.3.6"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^24.12.0", "@types/node": "^24.12.0",

View File

@@ -1,21 +1,40 @@
import OpenAI from 'openai'; import OpenAI from 'openai';
import * as dotenv from 'dotenv'; import * as dotenv from 'dotenv';
dotenv.config(); dotenv.config({ path: ['.env.local', '.env'] });
export const openai = new OpenAI({ const AI_PROVIDER = process.env.AI_PROVIDER ?? 'OPENAI';
const isGeneric = AI_PROVIDER === 'GENERIC';
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, apiKey: process.env.OPENAI_API_KEY,
}); timeout: 600000, // 10 minutes
maxRetries: 3,
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;
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) { } catch (err) {
console.error('Agent endpoint dummy error:', err instanceof Error ? err.message : err); if (err instanceof SyntaxError && attempt < retries) {
return 'Agent is in dummy mode or encountered an error.'; lastErr = err;
continue;
} }
throw err;
}
}
throw lastErr;
} }

View File

@@ -1,11 +1,31 @@
import { openai } from '../agent.js'; import { openai, defaultModel, serviceOptions, supportsWebSearch, parseWithRetry } from '../agent.js';
import type { InterpreterOutput, RankingOutput, CuratorOutput } from '../types/agents.js'; import type { InterpreterOutput, RankingOutput, CuratorOutput, MediaType } from '../types/agents.js';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
const CuratorSchema = z.object({
shows: z.array(z.object({
title: z.string(),
explanation: z.string(),
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( export async function runCurator(
ranking: RankingOutput, ranking: RankingOutput,
interpreter: InterpreterOutput, interpreter: InterpreterOutput,
mediaType: MediaType = 'tv_show',
useWebSearch = false,
): Promise<CuratorOutput[]> { ): Promise<CuratorOutput[]> {
const mediaLabel = mediaType === 'movie' ? 'movie' : 'TV show';
const allShows = [ 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.definitely_like.map((t) => ({ title: t, category: 'Definitely Like' as const })),
...ranking.might_like.map((t) => ({ title: t, category: 'Might 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 })), ...ranking.questionable.map((t) => ({ title: t, category: 'Questionable' as const })),
@@ -14,53 +34,48 @@ export async function runCurator(
if (allShows.length === 0) return []; if (allShows.length === 0) return [];
const showList = allShows const canSearch = useWebSearch && supportsWebSearch;
.map((s) => `- "${s.title}" (${s.category})`) const preferenceSummary = `User preferences summary:
.join('\n'); 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.chat.completions.create({ 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.' : ''}
model: 'gpt-5.4-mini',
temperature: 0.5,
service_tier: 'flex',
response_format: { type: 'json_object' },
messages: [
{
role: 'system',
content: `You are a TV show recommendation curator. For each show, write a concise 1-2 sentence explanation of why it was assigned to its category based on the user's preferences.
Your output MUST be valid JSON:
{
"shows": [
{
"title": string,
"explanation": string,
"category": "Definitely Like" | "Might Like" | "Questionable" | "Will Not Like"
}
]
}
Rules: Rules:
- Preserve the exact title and category as given - Preserve the exact title and category as given
- Keep explanations concise (1-2 sentences max) - explanation: 1-2 sentences explaining why it was assigned to its category, referencing specific user preferences
- Reference specific user preferences in the explanation - genre: 1-3 words capturing the most prominent genre of the title (e.g. "Crime Drama", "Sci-Fi Thriller", "Romantic Comedy")
- Be honest — explain why "Questionable" or "Will Not Like" shows got that rating`, - 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`;
role: 'user',
content: `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)}
Shows to describe: 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}`, ${showList}`,
}, }));
], results.push(...(response.output_parsed?.shows ?? []));
}); }
const content = response.choices[0]?.message?.content ?? '{"shows":[]}'; return results;
const result = JSON.parse(content) as { shows: CuratorOutput[] };
return result.shows ?? [];
} }

View File

@@ -1,56 +1,54 @@
import { openai } from '../agent.js'; import { openai, defaultModel, serviceOptions, parseWithRetry } from '../agent.js';
import type { InterpreterOutput } from '../types/agents.js'; import type { InterpreterOutput, MediaType } from '../types/agents.js';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
const InterpreterSchema = z.object({
liked: z.array(z.string()),
disliked: z.array(z.string()),
themes: z.array(z.string()),
character_preferences: z.array(z.string()),
tone: z.array(z.string()),
avoid: z.array(z.string()),
requirements: z.array(z.string())
});
interface InterpreterInput { interface InterpreterInput {
main_prompt: string; main_prompt: string;
liked_shows: string; liked_shows: string;
disliked_shows: string; disliked_shows: string;
themes: string; themes: string;
media_type: MediaType;
feedback_context?: string; feedback_context?: string;
} }
export async function runInterpreter(input: InterpreterInput): Promise<InterpreterOutput> { export async function runInterpreter(input: InterpreterInput): Promise<InterpreterOutput> {
const mediaLabel = input.media_type === 'movie' ? 'movie' : 'TV show';
const feedbackSection = input.feedback_context const feedbackSection = input.feedback_context
? `\n\nUser Feedback Context (incorporate into preferences):\n${input.feedback_context}` ? `\n\nUser Feedback Context (incorporate into preferences):\n${input.feedback_context}`
: ''; : '';
const response = await openai.chat.completions.create({ const response = await parseWithRetry(() => openai.responses.parse({
model: 'gpt-5.4-mini', model: defaultModel,
temperature: 0.2, temperature: 0.2,
service_tier: 'flex', ...serviceOptions,
response_format: { type: 'json_object' }, text: { format: zodTextFormat(InterpreterSchema, "preferences") },
messages: [ instructions: `You are a ${mediaLabel} preference interpreter. Transform raw user input into structured, normalized preferences.
{
role: 'system',
content: `You are a TV show preference interpreter. Transform raw user input into structured, normalized preferences.
Your output MUST be valid JSON matching this schema:
{
"liked": string[], // shows the user likes
"disliked": string[], // shows the user dislikes
"themes": string[], // normalized themes (e.g. "spy" -> "espionage")
"character_preferences": string[], // character types they prefer
"tone": string[], // tone descriptors (e.g. "serious", "grounded", "dark")
"avoid": string[] // things to explicitly avoid
}
Rules: Rules:
- Extract implicit preferences from the main prompt - Extract implicit preferences from the main prompt
- Normalize terminology (e.g. "spy" → "espionage", "cop show" → "police procedural") - Normalize terminology (e.g. "spy" → "espionage", "cop show" → "police procedural")
- Detect and resolve contradictions (prefer explicit over implicit) - Detect and resolve contradictions (prefer explicit over implicit)
- Do NOT assume anything not stated or clearly implied - 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}
role: 'user', Liked ${mediaLabel}s: ${input.liked_shows || '(none)'}
content: `Main prompt: ${input.main_prompt} Disliked ${mediaLabel}s: ${input.disliked_shows || '(none)'}
Liked shows: ${input.liked_shows || '(none)'}
Disliked shows: ${input.disliked_shows || '(none)'}
Themes and requirements: ${input.themes || '(none)'}${feedbackSection}`, Themes and requirements: ${input.themes || '(none)'}${feedbackSection}`,
}, }));
],
});
const content = response.choices[0]?.message?.content ?? '{}'; return (response.output_parsed as InterpreterOutput) ?? {
return JSON.parse(content) as InterpreterOutput; liked: [], disliked: [], themes: [], character_preferences: [], tone: [], avoid: [], requirements: []
};
} }

View File

@@ -1,10 +1,24 @@
import { openai } from '../agent.js'; import { openai, defaultModel, serviceOptions, parseWithRetry } from '../agent.js';
import type { InterpreterOutput, RetrievalOutput, RankingOutput } from '../types/agents.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()),
will_not_like: z.array(z.string())
});
export async function runRanking( export async function runRanking(
interpreter: InterpreterOutput, interpreter: InterpreterOutput,
retrieval: RetrievalOutput, retrieval: RetrievalOutput,
mediaType: MediaType = 'tv_show',
hardRequirements = false,
): Promise<RankingOutput> { ): Promise<RankingOutput> {
const mediaLabel = mediaType === 'movie' ? 'movie' : 'TV show';
// Phase 1: Pre-filter — remove avoidance violations // Phase 1: Pre-filter — remove avoidance violations
const avoidList = interpreter.avoid.map((a) => a.toLowerCase()); const avoidList = interpreter.avoid.map((a) => a.toLowerCase());
const filtered = retrieval.candidates.filter((c) => { const filtered = retrieval.candidates.filter((c) => {
@@ -19,7 +33,8 @@ export async function runRanking(
chunks.push(filtered.slice(i, i + CHUNK_SIZE)); chunks.push(filtered.slice(i, i + CHUNK_SIZE));
} }
const allBuckets: RankingOutput = { const allTags: RankingOutput = {
full_match: [],
definitely_like: [], definitely_like: [],
might_like: [], might_like: [],
questionable: [], questionable: [],
@@ -29,55 +44,42 @@ export async function runRanking(
for (const chunk of chunks) { for (const chunk of chunks) {
const chunkTitles = chunk.map((c) => `- ${c.title}: ${c.reason}`).join('\n'); const chunkTitles = chunk.map((c) => `- ${c.title}: ${c.reason}`).join('\n');
const response = await openai.chat.completions.create({ const response = await parseWithRetry(() => openai.responses.parse({
model: 'gpt-5.4', model: defaultModel,
temperature: 0.2, temperature: 0.2,
service_tier: 'flex', max_completion_tokens: 16384,
response_format: { type: 'json_object' }, ...serviceOptions,
messages: [ text: { format: zodTextFormat(RankingSchema, "ranking") },
{ 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.
role: 'system',
content: `You are a TV show ranking critic. Assign each show to exactly one of four confidence buckets based on how well it matches the user's preferences.
Buckets: Tags:
- "definitely_like": Near-perfect match to all preferences - "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 - "might_like": Strong match to most preferences
- "questionable": Partial alignment, some aspects don't match - "questionable": Partial alignment, some aspects don't match
- "will_not_like": Likely mismatch, conflicts with preferences or avoidance criteria - "will_not_like": Likely mismatch, conflicts with preferences or avoidance criteria
Your output MUST be valid JSON: 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:
"definitely_like": string[], Liked ${mediaLabel}s: ${interpreter.liked.join(', ') || '(none)'}
"might_like": string[], Themes: ${interpreter.themes.join(', ') || '(none)'}
"questionable": string[], Character preferences: ${interpreter.character_preferences.join(', ') || '(none)'}
"will_not_like": string[] Tone: ${interpreter.tone.join(', ') || '(none)'}
} Avoid: ${interpreter.avoid.join(', ') || '(none)'}
Requirements: ${interpreter.requirements.join(', ') || '(none)'}
Every show in the input must appear in exactly one bucket. Use the title exactly as given.`, Rank these ${mediaLabel}s:
},
{
role: 'user',
content: `User preferences:
Liked shows: ${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)}
Rank these shows:
${chunkTitles}`, ${chunkTitles}`,
}, }));
],
});
const content = response.choices[0]?.message?.content ?? '{}'; const chunkResult = (response.output_parsed as Partial<RankingOutput>) ?? {};
const chunkResult = JSON.parse(content) as Partial<RankingOutput>;
allBuckets.definitely_like.push(...(chunkResult.definitely_like ?? [])); allTags.full_match.push(...(chunkResult.full_match ?? []));
allBuckets.might_like.push(...(chunkResult.might_like ?? [])); allTags.definitely_like.push(...(chunkResult.definitely_like ?? []));
allBuckets.questionable.push(...(chunkResult.questionable ?? [])); allTags.might_like.push(...(chunkResult.might_like ?? []));
allBuckets.will_not_like.push(...(chunkResult.will_not_like ?? [])); allTags.questionable.push(...(chunkResult.questionable ?? []));
allTags.will_not_like.push(...(chunkResult.will_not_like ?? []));
} }
return allBuckets; return allTags;
} }

View File

@@ -1,47 +1,55 @@
import { openai } from '../agent.js'; import { openai, defaultModel, serviceOptions, supportsWebSearch, parseWithRetry } from '../agent.js';
import type { InterpreterOutput, RetrievalOutput } from '../types/agents.js'; import type { InterpreterOutput, RetrievalOutput, MediaType } from '../types/agents.js';
import { z } from 'zod';
import { zodTextFormat } from 'openai/helpers/zod';
export async function runRetrieval(input: InterpreterOutput): Promise<RetrievalOutput> { const RetrievalSchema = z.object({
const response = await openai.chat.completions.create({ candidates: z.array(z.object({
model: 'gpt-5.4', title: z.string(),
reason: z.string()
}))
});
export async function runRetrieval(
input: InterpreterOutput,
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 canSearch = useWebSearch && supportsWebSearch;
const response = await parseWithRetry(() => openai.responses.parse({
model: defaultModel,
temperature: 0.9, temperature: 0.9,
service_tier: 'flex', max_completion_tokens: 16384,
response_format: { type: 'json_object' }, ...serviceOptions,
messages: [ ...(canSearch ? { tools: [{ type: 'web_search' as const }] } : {}),
{ text: { format: zodTextFormat(RetrievalSchema, "candidates") },
role: 'system', 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.' : ''}
content: `You are a TV show candidate generator. Your goal is to brainstorm a LARGE, DIVERSE pool of 6080 TV show candidates that match the user's structured preferences.
Your output MUST be valid JSON matching this schema:
{
"candidates": [
{ "title": string, "reason": string }
]
}
Rules: Rules:
- Include both well-known and obscure shows - Include both well-known and obscure ${mediaLabelPlural}
- Prioritize RECALL over precision — it's better to include too many than too few - Prioritize RECALL over precision — it's better to include too many than too few
- Each "reason" should briefly explain why the show matches the preferences - Each "reason" should briefly explain why the ${mediaLabel} matches the preferences
- Avoid duplicates - Avoid duplicates
- Include shows from different decades, countries, and networks - Include ${mediaLabelPlural} from different decades, countries${mediaType === 'tv_show' ? ', and networks' : ', and directors'}
- Aim for 6080 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:
role: 'user', Liked ${mediaLabelPlural}: ${input.liked.join(', ') || '(none)'}
content: `Structured preferences: Disliked ${mediaLabelPlural}: ${input.disliked.join(', ') || '(none)'}
Liked shows: ${JSON.stringify(input.liked)} Themes: ${input.themes.join(', ') || '(none)'}
Disliked shows: ${JSON.stringify(input.disliked)} Character preferences: ${input.character_preferences.join(', ') || '(none)'}
Themes: ${JSON.stringify(input.themes)} Tone: ${input.tone.join(', ') || '(none)'}
Character preferences: ${JSON.stringify(input.character_preferences)} Avoid: ${input.avoid.join(', ') || '(none)'}
Tone: ${JSON.stringify(input.tone)} 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(', ')}` : ''}
Avoid: ${JSON.stringify(input.avoid)}
Generate a large, diverse pool of TV show candidates.`, Generate a large, diverse pool of ${mediaLabel} candidates.`,
}, }));
],
});
const content = response.choices[0]?.message?.content ?? '{"candidates":[]}'; return (response.output_parsed as RetrievalOutput) ?? { candidates: [] };
return JSON.parse(content) as RetrievalOutput;
} }

View File

@@ -0,0 +1,22 @@
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: miniModel,
temperature: 0.7,
...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.
Examples: "Dark Crime Dramas With Moral Ambiguity", "Cozy British Mysteries With Quirky Detectives"`,
input: `Liked: ${JSON.stringify(interpreter.liked)}
Themes: ${JSON.stringify(interpreter.themes)}
Tone: ${JSON.stringify(interpreter.tone)}
Character preferences: ${JSON.stringify(interpreter.character_preferences)}`,
});
return (response.output_text ?? '').trim() || 'My Recommendation Session';
}

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

@@ -2,7 +2,7 @@ import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres'; import postgres from 'postgres';
import * as dotenv from 'dotenv'; import * as dotenv from 'dotenv';
dotenv.config(); dotenv.config({ path: ['.env.local', '.env'] });
const connectionString = process.env.DATABASE_URL || 'postgres://user:password@iris.haven:5432/recommender'; const connectionString = process.env.DATABASE_URL || 'postgres://user:password@iris.haven:5432/recommender';
export const client = postgres(connectionString); export const client = postgres(connectionString);

View File

@@ -1,4 +1,4 @@
import { pgTable, uuid, text, jsonb, timestamp, integer, uniqueIndex } from 'drizzle-orm/pg-core'; import { pgTable, uuid, text, jsonb, timestamp, integer, uniqueIndex, boolean } from 'drizzle-orm/pg-core';
import type { CuratorOutput } from '../types/agents.js'; import type { CuratorOutput } from '../types/agents.js';
export const recommendations = pgTable('recommendations', { export const recommendations = pgTable('recommendations', {
@@ -8,6 +8,14 @@ export const recommendations = pgTable('recommendations', {
liked_shows: text('liked_shows').notNull().default(''), liked_shows: text('liked_shows').notNull().default(''),
disliked_shows: text('disliked_shows').notNull().default(''), disliked_shows: text('disliked_shows').notNull().default(''),
themes: text('themes').notNull().default(''), themes: text('themes').notNull().default(''),
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[]>(), recommendations: jsonb('recommendations').$type<CuratorOutput[]>(),
status: text('status').notNull().default('pending'), status: text('status').notNull().default('pending'),
created_at: timestamp('created_at').defaultNow().notNull(), created_at: timestamp('created_at').defaultNow().notNull(),
@@ -17,10 +25,10 @@ export const feedback = pgTable(
'feedback', 'feedback',
{ {
id: uuid('id').defaultRandom().primaryKey(), id: uuid('id').defaultRandom().primaryKey(),
tv_show_name: text('tv_show_name').notNull(), item_name: text('item_name').notNull(),
stars: integer('stars').notNull(), stars: integer('stars').notNull(),
feedback: text('feedback').notNull().default(''), feedback: text('feedback').notNull().default(''),
created_at: timestamp('created_at').defaultNow().notNull(), created_at: timestamp('created_at').defaultNow().notNull(),
}, },
(table) => [uniqueIndex('feedback_tv_show_name_idx').on(table.tv_show_name)], (table) => [uniqueIndex('feedback_item_name_idx').on(table.item_name)],
); );

View File

@@ -3,10 +3,10 @@ import * as dotenv from 'dotenv';
import recommendationsRoute from './routes/recommendations.js'; import recommendationsRoute from './routes/recommendations.js';
import feedbackRoute from './routes/feedback.js'; import feedbackRoute from './routes/feedback.js';
// Load .env first, then .env.local // Load .env.local first, then .env.
// env vars set on the container take precedence over both files // dotenv 17+ supports array of paths, where the first path has the highest priority.
dotenv.config(); // Env vars set on the container (system) will take precedence over both.
dotenv.config({ path: '.env.local', override: true }); dotenv.config({ path: ['.env.local', '.env'] });
const fastify = Fastify({ logger: true }); const fastify = Fastify({ logger: true });

View File

@@ -0,0 +1,43 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
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);
dotenv.config({ path: ['.env.local', '.env'] });
const connectionString = process.env.DATABASE_URL;
if (!connectionString) {
console.error('DATABASE_URL is not set');
process.exit(1);
}
// Using max: 1 connection since it's only for migration
const migrationClient = postgres(connectionString, { max: 1 });
const db = drizzle(migrationClient);
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) {
console.error('Error running migrations:', err);
process.exit(1);
} finally {
await migrationClient.end();
}
};
runMigrations();

View File

@@ -3,12 +3,47 @@ import { db } from '../db.js';
import { recommendations } from '../db/schema.js'; import { recommendations } from '../db/schema.js';
import { runInterpreter } from '../agents/interpreter.js'; import { runInterpreter } from '../agents/interpreter.js';
import { runRetrieval } from '../agents/retrieval.js'; import { runRetrieval } from '../agents/retrieval.js';
import { runValidator } from '../agents/validator.js';
import { runRanking } from '../agents/ranking.js'; import { runRanking } from '../agents/ranking.js';
import { runCurator } from '../agents/curator.js'; import { runCurator } from '../agents/curator.js';
import type { CuratorOutput, 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; 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) { function log(recId: string, msg: string, data?: unknown) {
const ts = new Date().toISOString(); const ts = new Date().toISOString();
if (data !== undefined) { if (data !== undefined) {
@@ -18,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( export async function runPipeline(
rec: RecommendationRecord, rec: RecommendationRecord,
sseWrite: (event: SSEEvent) => void, sseWrite: (event: SSEEvent) => void,
@@ -25,8 +178,13 @@ export async function runPipeline(
): Promise<CuratorOutput[]> { ): Promise<CuratorOutput[]> {
let currentStage: SSEEvent['stage'] = 'interpreter'; let currentStage: SSEEvent['stage'] = 'interpreter';
const startTime = Date.now(); 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}"${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 { try {
// Set status to running // Set status to running
@@ -46,6 +204,7 @@ export async function runPipeline(
liked_shows: rec.liked_shows, liked_shows: rec.liked_shows,
disliked_shows: rec.disliked_shows, disliked_shows: rec.disliked_shows,
themes: rec.themes, themes: rec.themes,
media_type: mediaType,
...(feedbackContext !== undefined ? { feedback_context: feedbackContext } : {}), ...(feedbackContext !== undefined ? { feedback_context: feedbackContext } : {}),
}); });
log(rec.id, `Interpreter: done (${Date.now() - t0}ms)`, { log(rec.id, `Interpreter: done (${Date.now() - t0}ms)`, {
@@ -57,51 +216,101 @@ export async function runPipeline(
}); });
sseWrite({ stage: 'interpreter', status: 'done', data: interpreterOutput }); sseWrite({ stage: 'interpreter', status: 'done', data: interpreterOutput });
// --- Retrieval --- // --- Pass 1: Retrieval → [Validator?] → Ranking → Curator ---
currentStage = 'retrieval'; currentStage = 'retrieval';
log(rec.id, 'Retrieval: start'); const allSeenTitles = new Set<string>();
sseWrite({ stage: 'retrieval', status: 'start' }); const pass1Output = await runSubPipeline({
const t1 = Date.now(); recId: rec.id,
const retrievalOutput = await runRetrieval(interpreterOutput); interpreterOutput,
log(rec.id, `Retrieval: done (${Date.now() - t1}ms) — ${retrievalOutput.candidates.length} candidates`, { mediaType,
titles: retrievalOutput.candidates.map((c) => c.title), 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 --- let mergedOutput = pass1Output;
currentStage = 'ranking';
log(rec.id, 'Ranking: start'); // --- Self Expansive: extra passes ---
sseWrite({ stage: 'ranking', status: 'start' }); if (selfExpansive && rec.expansive_passes > 0) {
const t2 = Date.now(); const allFullMatches = pass1Output
const rankingOutput = await runRanking(interpreterOutput, retrievalOutput); .filter((c) => c.category === 'Full Match')
log(rec.id, `Ranking: done (${Date.now() - t2}ms)`, { .map((c) => c.title);
definitely_like: rankingOutput.definitely_like.length,
might_like: rankingOutput.might_like.length, for (let i = 0; i < rec.expansive_passes; i++) {
questionable: rankingOutput.questionable.length, const passNum = i + 2;
will_not_like: rankingOutput.will_not_like.length, 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);
},
}); });
sseWrite({ stage: 'ranking', status: 'done', data: rankingOutput });
// --- Curator --- mergedOutput = mergeCuratorOutputs(mergedOutput, passOutput);
currentStage = 'curator';
log(rec.id, 'Curator: start'); const newFullMatches = passOutput
sseWrite({ stage: 'curator', status: 'start' }); .filter((c) => c.category === 'Full Match')
const t3 = Date.now(); .map((c) => c.title);
const curatorOutput = await runCurator(rankingOutput, interpreterOutput); allFullMatches.push(...newFullMatches);
log(rec.id, `Curator: done (${Date.now() - t3}ms) — ${curatorOutput.length} shows curated`);
sseWrite({ stage: 'curator', status: 'done', data: curatorOutput }); log(rec.id, `Self Expansive Pass ${passNum}: done — ${passOutput.length} new items, ${mergedOutput.length} total`);
}
}
// Generate AI title
let aiTitle: string = rec.title;
try {
log(rec.id, 'Title generation: start');
aiTitle = await generateTitle(interpreterOutput, mediaType);
log(rec.id, `Title generation: done — "${aiTitle}"`);
} catch (err) {
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 // Save results to DB
log(rec.id, 'Saving results to DB'); log(rec.id, 'Saving results to DB');
await db await db
.update(recommendations) .update(recommendations)
.set({ recommendations: curatorOutput, status: 'done' }) .set({ recommendations: mergedOutput, status: 'done', title: aiTitle })
.where(eq(recommendations.id, rec.id)); .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)`); log(rec.id, `Pipeline complete (total: ${Date.now() - startTime}ms)`);
return curatorOutput; return mergedOutput;
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : String(err); const message = err instanceof Error ? err.message : String(err);
log(rec.id, `Pipeline error at stage "${currentStage}": ${message}`); log(rec.id, `Pipeline error at stage "${currentStage}": ${message}`);

View File

@@ -1,13 +1,12 @@
import type { FastifyInstance } from 'fastify'; import type { FastifyInstance } from 'fastify';
import { eq } from 'drizzle-orm';
import { db } from '../db.js'; import { db } from '../db.js';
import { feedback } from '../db/schema.js'; import { feedback } from '../db/schema.js';
export default async function feedbackRoute(fastify: FastifyInstance) { export default async function feedbackRoute(fastify: FastifyInstance) {
// POST /feedback — upsert by tv_show_name // POST /feedback — upsert by item_name
fastify.post('/feedback', async (request, reply) => { fastify.post('/feedback', async (request, reply) => {
const body = request.body as { const body = request.body as {
tv_show_name: string; item_name: string;
stars: number; stars: number;
feedback?: string; feedback?: string;
}; };
@@ -15,12 +14,12 @@ export default async function feedbackRoute(fastify: FastifyInstance) {
await db await db
.insert(feedback) .insert(feedback)
.values({ .values({
tv_show_name: body.tv_show_name, item_name: body.item_name,
stars: body.stars, stars: body.stars,
feedback: body.feedback ?? '', feedback: body.feedback ?? '',
}) })
.onConflictDoUpdate({ .onConflictDoUpdate({
target: feedback.tv_show_name, target: feedback.item_name,
set: { set: {
stars: body.stars, stars: body.stars,
feedback: body.feedback ?? '', feedback: body.feedback ?? '',

View File

@@ -3,7 +3,8 @@ import { eq, desc } from 'drizzle-orm';
import { db } from '../db.js'; import { db } from '../db.js';
import { recommendations, feedback } from '../db/schema.js'; import { recommendations, feedback } from '../db/schema.js';
import { runPipeline } from '../pipelines/recommendation.js'; import { runPipeline } from '../pipelines/recommendation.js';
import type { SSEEvent } from '../types/agents.js'; import type { MediaType, SSEEvent } from '../types/agents.js';
import { supportsWebSearch } from '../agent.js';
export default async function recommendationsRoute(fastify: FastifyInstance) { export default async function recommendationsRoute(fastify: FastifyInstance) {
// POST /recommendations — create record, return { id } // POST /recommendations — create record, return { id }
@@ -13,6 +14,14 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
liked_shows?: string; liked_shows?: string;
disliked_shows?: string; disliked_shows?: string;
themes?: string; themes?: string;
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 ?? '') const title = (body.main_prompt ?? '')
@@ -21,6 +30,17 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
.slice(0, 5) .slice(0, 5)
.join(' '); .join(' ');
const rawCount = Number(body.brainstorm_count ?? 100);
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 const [rec] = await db
.insert(recommendations) .insert(recommendations)
.values({ .values({
@@ -29,6 +49,14 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
liked_shows: body.liked_shows ?? '', liked_shows: body.liked_shows ?? '',
disliked_shows: body.disliked_shows ?? '', disliked_shows: body.disliked_shows ?? '',
themes: body.themes ?? '', themes: body.themes ?? '',
brainstorm_count,
media_type,
use_web_search,
use_validator,
hard_requirements,
self_expansive,
expansive_passes,
expansive_mode,
status: 'pending', status: 'pending',
}) })
.returning({ id: recommendations.id }); .returning({ id: recommendations.id });
@@ -43,6 +71,7 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
id: recommendations.id, id: recommendations.id,
title: recommendations.title, title: recommendations.title,
status: recommendations.status, status: recommendations.status,
media_type: recommendations.media_type,
created_at: recommendations.created_at, created_at: recommendations.created_at,
}) })
.from(recommendations) .from(recommendations)
@@ -63,7 +92,6 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
}); });
// GET /recommendations/:id/stream — SSE pipeline stream // GET /recommendations/:id/stream — SSE pipeline stream
// Always fetches all current feedback and injects if present (supports rerank flow)
fastify.get('/recommendations/:id/stream', async (request, reply) => { fastify.get('/recommendations/:id/stream', async (request, reply) => {
const { id } = request.params as { id: string }; const { id } = request.params as { id: string };
const [rec] = await db const [rec] = await db
@@ -73,19 +101,7 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
if (!rec) return reply.code(404).send({ error: 'Not found' }); if (!rec) return reply.code(404).send({ error: 'Not found' });
// Load all feedback to potentially inject as context // Set SSE headers and hijack before any branching
const feedbackRows = await db.select().from(feedback);
const feedbackContext =
feedbackRows.length > 0
? feedbackRows
.map(
(f) =>
`Show: "${f.tv_show_name}" — Rating: ${f.stars}/3 stars${f.feedback ? ` — Comment: ${f.feedback}` : ''}`,
)
.join('\n')
: undefined;
// Set SSE headers and hijack
reply.raw.setHeader('Content-Type', 'text/event-stream'); reply.raw.setHeader('Content-Type', 'text/event-stream');
reply.raw.setHeader('Cache-Control', 'no-cache'); reply.raw.setHeader('Cache-Control', 'no-cache');
reply.raw.setHeader('Connection', 'keep-alive'); reply.raw.setHeader('Connection', 'keep-alive');
@@ -93,17 +109,93 @@ export default async function recommendationsRoute(fastify: FastifyInstance) {
reply.raw.flushHeaders(); reply.raw.flushHeaders();
reply.hijack(); 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) => { const sseWrite = (event: SSEEvent) => {
try {
reply.raw.write(`data: ${JSON.stringify(event)}\n\n`); reply.raw.write(`data: ${JSON.stringify(event)}\n\n`);
} catch {
// Client disconnected — pipeline continues, writes are silently dropped
}
}; };
try { 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); await runPipeline(rec, sseWrite, feedbackContext);
} finally { } finally {
reply.raw.end(); 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 // POST /recommendations/:id/rerank — reset status so client can re-open SSE stream
fastify.post('/recommendations/:id/rerank', async (request, reply) => { fastify.post('/recommendations/:id/rerank', async (request, reply) => {
const { id } = request.params as { id: string }; const { id } = request.params as { id: string };

View File

@@ -1,3 +1,5 @@
export type MediaType = 'tv_show' | 'movie';
export interface InterpreterOutput { export interface InterpreterOutput {
liked: string[]; liked: string[];
disliked: string[]; disliked: string[];
@@ -5,6 +7,7 @@ export interface InterpreterOutput {
character_preferences: string[]; character_preferences: string[];
tone: string[]; tone: string[];
avoid: string[]; avoid: string[];
requirements: string[];
} }
export interface RetrievalCandidate { export interface RetrievalCandidate {
@@ -16,22 +19,47 @@ export interface RetrievalOutput {
candidates: RetrievalCandidate[]; candidates: RetrievalCandidate[];
} }
export interface ValidatorCandidate {
title: string;
reason: string;
isTrash: boolean;
}
export interface ValidatorOutput {
candidates: ValidatorCandidate[];
}
export interface RankingOutput { export interface RankingOutput {
full_match: string[];
definitely_like: string[]; definitely_like: string[];
might_like: string[]; might_like: string[];
questionable: string[]; questionable: string[];
will_not_like: 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 { export interface CuratorOutput {
title: string; title: string;
explanation: string; explanation: string;
category: CuratorCategory; 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 type SSEStatus = 'start' | 'done' | 'error';
export interface SSEEvent { export interface SSEEvent {

View File

@@ -9,7 +9,8 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"preact": "^10.29.0" "preact": "^10.29.0",
"preact-router": "^4.1.2"
}, },
"devDependencies": { "devDependencies": {
"@preact/preset-vite": "^2.10.4", "@preact/preset-vite": "^2.10.4",

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

View File

@@ -1,10 +1,12 @@
import type { Recommendation, RecommendationSummary, FeedbackEntry } from '../types/index.js'; import type { MediaType, Recommendation, RecommendationSummary, FeedbackEntry } from '../types/index.js';
const BASE = '/api'; const BASE = '/api';
async function request<T>(path: string, options?: RequestInit): Promise<T> { async function request<T>(path: string, options?: RequestInit): Promise<T> {
const res = await fetch(`${BASE}${path}`, { const res = await fetch(`${BASE}${path}`, {
headers: { 'Content-Type': 'application/json' }, headers: {
...(options?.body ? { 'Content-Type': 'application/json' } : {}),
},
...options, ...options,
}); });
if (!res.ok) { if (!res.ok) {
@@ -19,6 +21,14 @@ export function createRecommendation(body: {
liked_shows: string; liked_shows: string;
disliked_shows: string; disliked_shows: string;
themes: string; themes: string;
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 }> { }): Promise<{ id: string }> {
return request('/recommendations', { return request('/recommendations', {
method: 'POST', method: 'POST',
@@ -39,7 +49,7 @@ export function rerankRecommendation(id: string): Promise<{ ok: boolean }> {
} }
export function submitFeedback(body: { export function submitFeedback(body: {
tv_show_name: string; item_name: string;
stars: number; stars: number;
feedback?: string; feedback?: string;
}): Promise<{ ok: boolean }> { }): Promise<{ ok: boolean }> {
@@ -52,3 +62,7 @@ export function submitFeedback(body: {
export function getFeedback(): Promise<FeedbackEntry[]> { export function getFeedback(): Promise<FeedbackEntry[]> {
return request('/feedback'); return request('/feedback');
} }
export function deleteRecommendation(id: string): Promise<{ ok: boolean }> {
return request(`/recommendations/${id}`, { method: 'DELETE' });
}

View File

@@ -1,5 +1,15 @@
import { Router, Route } from 'preact-router';
import { Home } from './pages/Home.js'; import { Home } from './pages/Home.js';
import { Recom } from './pages/Recom.js';
import { RecommendationsProvider } from './context/RecommendationsContext.js';
export function App() { export function App() {
return <Home />; return (
<RecommendationsProvider>
<Router>
<Route path="/" component={Home} />
<Route path="/recom/:id" component={Recom} />
</Router>
</RecommendationsProvider>
);
} }

View File

@@ -0,0 +1,204 @@
/* ── Cards ─────────────────────────────────────────────── */
.cards-grid {
display: flex;
flex-direction: column;
gap: 12px;
margin-bottom: 32px;
}
.card {
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 16px;
transition: border-color 0.15s;
}
.card:hover {
border-color: var(--bg-surface-3);
}
.card-header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
flex-wrap: wrap;
}
.badge {
font-size: 11px;
font-weight: 600;
padding: 2px 8px;
border-radius: 100px;
text-transform: uppercase;
letter-spacing: 0.04em;
flex-shrink: 0;
}
.badge-green {
background: rgba(34, 197, 94, 0.15);
color: #4ade80;
}
.badge-blue {
background: rgba(59, 130, 246, 0.15);
color: #60a5fa;
}
.badge-yellow {
background: rgba(234, 179, 8, 0.15);
color: #facc15;
}
.badge-red {
background: rgba(239, 68, 68, 0.15);
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;
color: var(--text);
}
.card-explanation {
font-size: 13px;
color: var(--text-muted);
line-height: 1.5;
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 {
border-top: 1px solid var(--border);
padding-top: 10px;
}
.star-rating {
display: flex;
align-items: center;
gap: 2px;
}
.star-btn {
background: none;
border: none;
cursor: pointer;
font-size: 18px;
color: var(--text-dim);
padding: 0 2px;
transition: color 0.1s, transform 0.1s;
}
.star-btn:hover,
.star-btn.star-active {
color: var(--yellow);
}
.star-btn:hover {
transform: scale(1.1);
}
.feedback-saved {
margin-left: 8px;
font-size: 12px;
color: var(--green);
font-weight: 500;
}
.comment-area {
margin-top: 8px;
display: flex;
flex-direction: column;
gap: 6px;
}
.comment-input {
font-size: 13px;
resize: vertical;
}
/* ── Rerank Button ──────────────────────────────────────── */
.rerank-section {
padding: 16px 0 32px;
display: flex;
justify-content: center;
gap: 12px;
flex-wrap: wrap;
}
.btn-rerank {
padding: 12px 28px;
background: var(--bg-surface-2);
color: var(--text);
border: 1px solid var(--border);
border-radius: var(--radius);
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: background 0.15s, border-color 0.15s;
}
.btn-rerank:hover:not(:disabled) {
background: var(--bg-surface-3);
border-color: var(--accent);
color: var(--accent);
}
.btn-rerank:disabled {
opacity: 0.4;
cursor: not-allowed;
}

View File

@@ -0,0 +1,307 @@
/* ── Modal ──────────────────────────────────────────────── */
.modal-backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.6);
display: flex;
align-items: center;
justify-content: center;
z-index: 100;
backdrop-filter: blur(2px);
}
.modal {
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: 12px;
width: 650px;
max-width: 96vw;
max-height: 90vh;
overflow-y: auto;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px 20px 0;
}
.modal-header h2 {
font-size: 18px;
font-weight: 700;
}
.modal-close {
background: none;
border: none;
font-size: 22px;
color: var(--text-muted);
cursor: pointer;
line-height: 1;
padding: 0;
}
.modal-close:hover {
color: var(--text);
}
.modal-form {
padding: 20px;
display: flex;
flex-direction: column;
gap: 16px;
}
.form-group {
display: flex;
flex-direction: column;
gap: 6px;
flex: 1;
}
.form-group label {
font-size: 13px;
font-weight: 500;
color: var(--text-muted);
}
.form-input,
.form-textarea {
background: var(--bg-surface-2);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
color: var(--text);
font-size: 14px;
padding: 10px 12px;
outline: none;
transition: border-color 0.15s;
font-family: inherit;
width: 100%;
}
.form-input[type="range"] {
padding: 0;
cursor: pointer;
}
.form-input:focus,
.form-textarea:focus {
border-color: var(--accent);
}
.form-textarea {
resize: vertical;
min-height: 80px;
}
.modal-actions {
display: flex;
justify-content: flex-end;
gap: 10px;
padding-top: 4px;
}
/* ── Type selection step ─────────────────────────────────── */
.modal-type-select {
padding: 16px 20px 28px;
display: flex;
flex-direction: column;
gap: 20px;
}
.modal-type-hint {
font-size: 14px;
color: var(--text-muted);
margin: 0;
}
.modal-type-cards {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
}
.type-card {
display: flex;
flex-direction: column;
align-items: center;
gap: 8px;
padding: 4rem 2rem;
background: var(--bg-surface-2);
border: 1px solid var(--border);
border-radius: 10px;
cursor: pointer;
transition: border-color 0.15s, background 0.15s;
text-align: center;
}
.type-card:hover {
border-color: var(--accent);
background: var(--bg-surface);
}
.type-card-icon {
font-size: 32px;
line-height: 1;
}
.type-card-label {
font-size: 16px;
font-weight: 700;
color: var(--text);
}
.type-card-desc {
font-size: 12px;
color: var(--text-muted);
}
/* ── Modal header back button ────────────────────────────── */
.modal-header-left {
display: flex;
align-items: center;
gap: 8px;
}
.modal-back {
background: none;
border: none;
font-size: 18px;
color: var(--text-muted);
cursor: pointer;
line-height: 1;
padding: 0 4px 0 0;
}
.modal-back:hover {
color: var(--text);
}
/* ── Web search toggle ───────────────────────────────────── */
.form-group-toggle {
padding: 12px 0 4px;
border-top: 1px solid var(--border);
}
.toggle-label {
display: flex;
align-items: center;
justify-content: space-between;
cursor: pointer;
gap: 16px;
}
.toggle-text {
display: flex;
flex-direction: column;
gap: 3px;
}
.toggle-title {
font-size: 14px;
font-weight: 500;
color: var(--text);
}
.toggle-desc {
font-size: 12px;
color: var(--text-muted);
}
.toggle-switch {
flex-shrink: 0;
width: 40px;
height: 22px;
background: var(--bg-surface-2);
border: 1px solid var(--border);
border-radius: 11px;
position: relative;
cursor: pointer;
transition: background 0.2s, border-color 0.2s;
}
.toggle-switch.on {
background: var(--accent);
border-color: var(--accent);
}
.toggle-knob {
position: absolute;
top: 2px;
left: 2px;
width: 16px;
height: 16px;
background: #fff;
border-radius: 50%;
transition: transform 0.2s;
}
.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

@@ -1,4 +1,6 @@
import { useState } from 'preact/hooks'; import { useState } from 'preact/hooks';
import type { MediaType } from '../types/index.js';
import './Modal.css';
interface NewRecommendationModalProps { interface NewRecommendationModalProps {
onClose: () => void; onClose: () => void;
@@ -7,16 +9,47 @@ interface NewRecommendationModalProps {
liked_shows: string; liked_shows: string;
disliked_shows: string; disliked_shows: string;
themes: string; themes: string;
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>; }) => Promise<void>;
} }
export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationModalProps) { export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationModalProps) {
const [step, setStep] = useState<'type' | 'form'>('type');
const [mediaType, setMediaType] = useState<MediaType>('tv_show');
const [mainPrompt, setMainPrompt] = useState(''); const [mainPrompt, setMainPrompt] = useState('');
const [likedShows, setLikedShows] = useState(''); const [likedShows, setLikedShows] = useState('');
const [dislikedShows, setDislikedShows] = useState(''); const [dislikedShows, setDislikedShows] = useState('');
const [themes, setThemes] = useState(''); 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 [loading, setLoading] = useState(false);
const mediaLabel = mediaType === 'movie' ? 'Movie' : 'TV Show';
const mediaPluralLabel = mediaType === 'movie' ? 'movies' : 'shows';
const handleSelectType = (type: MediaType) => {
setMediaType(type);
setStep('form');
};
const handleWebSearchToggle = () => {
const next = !useWebSearch;
setUseWebSearch(next);
if (!next) setUseValidator(false);
};
const handleSubmit = async (e: Event) => { const handleSubmit = async (e: Event) => {
e.preventDefault(); e.preventDefault();
if (!mainPrompt.trim()) return; if (!mainPrompt.trim()) return;
@@ -27,6 +60,14 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
liked_shows: likedShows.trim(), liked_shows: likedShows.trim(),
disliked_shows: dislikedShows.trim(), disliked_shows: dislikedShows.trim(),
themes: themes.trim(), themes: themes.trim(),
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(); onClose();
} finally { } finally {
@@ -43,10 +84,37 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
return ( return (
<div class="modal-backdrop" onClick={handleBackdropClick}> <div class="modal-backdrop" onClick={handleBackdropClick}>
<div class="modal"> <div class="modal">
{step === 'type' ? (
<>
<div class="modal-header"> <div class="modal-header">
<h2>New Recommendation</h2> <h2>New Recommendation</h2>
<button class="modal-close" onClick={onClose} aria-label="Close">×</button> <button class="modal-close" onClick={onClose} aria-label="Close">×</button>
</div> </div>
<div class="modal-type-select">
<p class="modal-type-hint">What would you like recommendations for?</p>
<div class="modal-type-cards">
<button class="type-card" onClick={() => handleSelectType('tv_show')}>
<span class="type-card-icon">📺</span>
<span class="type-card-label">TV Shows</span>
<span class="type-card-desc">Series &amp; episodic content</span>
</button>
<button class="type-card" onClick={() => handleSelectType('movie')}>
<span class="type-card-icon">🎬</span>
<span class="type-card-label">Movies</span>
<span class="type-card-desc">Feature films &amp; cinema</span>
</button>
</div>
</div>
</>
) : (
<>
<div class="modal-header">
<div class="modal-header-left">
<button class="modal-back" onClick={() => setStep('type')} aria-label="Back"></button>
<h2>New {mediaLabel} Recommendation</h2>
</div>
<button class="modal-close" onClick={onClose} aria-label="Close">×</button>
</div>
<form class="modal-form" onSubmit={handleSubmit}> <form class="modal-form" onSubmit={handleSubmit}>
<div class="form-group"> <div class="form-group">
@@ -54,7 +122,7 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
<textarea <textarea
id="main-prompt" id="main-prompt"
class="form-textarea" class="form-textarea"
placeholder="Describe what you want to watch. Be as specific as you like — mood, themes, setting, what you've enjoyed before..." placeholder={`Describe what you want to watch. Be as specific as you like — mood, themes, setting, what you've enjoyed before...`}
value={mainPrompt} value={mainPrompt}
onInput={(e) => setMainPrompt((e.target as HTMLTextAreaElement).value)} onInput={(e) => setMainPrompt((e.target as HTMLTextAreaElement).value)}
rows={5} rows={5}
@@ -62,31 +130,29 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
/> />
</div> </div>
<div class="form-row">
<div class="form-group"> <div class="form-group">
<label for="liked-shows">Shows you liked</label> <label for="liked-shows">{mediaLabel}s you liked</label>
<input <input
id="liked-shows" id="liked-shows"
type="text" type="text"
class="form-input" class="form-input"
placeholder="e.g. Breaking Bad, The Wire" placeholder={mediaType === 'movie' ? 'e.g. Inception, The Godfather' : 'e.g. Breaking Bad, The Wire'}
value={likedShows} value={likedShows}
onInput={(e) => setLikedShows((e.target as HTMLInputElement).value)} onInput={(e) => setLikedShows((e.target as HTMLInputElement).value)}
/> />
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="disliked-shows">Shows you disliked</label> <label for="disliked-shows">{mediaLabel}s you disliked</label>
<input <input
id="disliked-shows" id="disliked-shows"
type="text" type="text"
class="form-input" class="form-input"
placeholder="e.g. Game of Thrones" placeholder={mediaType === 'movie' ? 'e.g. Transformers' : 'e.g. Game of Thrones'}
value={dislikedShows} value={dislikedShows}
onInput={(e) => setDislikedShows((e.target as HTMLInputElement).value)} onInput={(e) => setDislikedShows((e.target as HTMLInputElement).value)}
/> />
</div> </div>
</div>
<div class="form-group"> <div class="form-group">
<label for="themes">Themes and requirements</label> <label for="themes">Themes and requirements</label>
@@ -100,6 +166,116 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
/> />
</div> </div>
<div class="form-group">
<label for="brainstorm-count">{mediaLabel}s to brainstorm ({brainstormCount})</label>
<input
id="brainstorm-count"
type="range"
class="form-input"
min={50}
max={200}
step={10}
value={brainstormCount}
onInput={(e) => setBrainstormCount(Number((e.target as HTMLInputElement).value))}
/>
</div>
<div class="form-group-toggle">
<label class="toggle-label" for="web-search">
<div class="toggle-text">
<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={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"> <div class="modal-actions">
<button type="button" class="btn-secondary" onClick={onClose} disabled={loading}> <button type="button" class="btn-secondary" onClick={onClose} disabled={loading}>
Cancel Cancel
@@ -109,6 +285,8 @@ export function NewRecommendationModal({ onClose, onSubmit }: NewRecommendationM
</button> </button>
</div> </div>
</form> </form>
</>
)}
</div> </div>
</div> </div>
); );

View File

@@ -0,0 +1,113 @@
/* ── Pipeline Progress ──────────────────────────────────── */
.pipeline-progress {
padding: 40px 0;
}
.pipeline-title {
font-size: 18px;
font-weight: 600;
color: var(--text);
margin-bottom: 24px;
}
.pipeline-steps {
list-style: none;
display: flex;
flex-direction: column;
gap: 12px;
}
.pipeline-step {
display: flex;
align-items: center;
gap: 12px;
padding: 12px 16px;
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius);
font-size: 14px;
}
.pipeline-step--running {
border-color: var(--accent);
background: var(--accent-dim);
}
.pipeline-step--done {
border-color: rgba(34, 197, 94, 0.3);
background: rgba(34, 197, 94, 0.05);
}
.pipeline-step--error {
border-color: rgba(239, 68, 68, 0.3);
background: rgba(239, 68, 68, 0.05);
}
.stage-icon {
font-size: 16px;
width: 20px;
text-align: center;
}
.stage-done {
color: var(--green);
}
.stage-error {
color: var(--red);
}
.stage-running {
color: var(--accent);
}
.stage-pending {
color: var(--text-dim);
}
.spinner {
display: inline-block;
animation: spin 1s linear infinite;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.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,14 +1,20 @@
import './PipelineProgress.css';
import type { StageMap, StageStatus } from '../types/index.js'; import type { StageMap, StageStatus } from '../types/index.js';
const STAGES: { key: keyof StageMap; label: string }[] = [ export interface StageEntry {
{ key: 'interpreter', label: 'Interpreting Preferences' }, key: string;
{ key: 'retrieval', label: 'Generating Candidates' }, label: string;
{ key: 'ranking', label: 'Ranking Shows' }, }
{ key: 'curator', label: 'Curating Results' },
]; export interface StageGroup {
label: string;
stages: StageEntry[];
}
interface PipelineProgressProps { interface PipelineProgressProps {
stageGroups: StageGroup[];
stages: StageMap; stages: StageMap;
onRetry?: () => void;
} }
function StageIcon({ status }: { status: StageStatus }) { function StageIcon({ status }: { status: StageStatus }) {
@@ -19,23 +25,40 @@ function StageIcon({ status }: { status: StageStatus }) {
return <span class="stage-icon stage-error"></span>; return <span class="stage-icon stage-error"></span>;
case 'running': case 'running':
return <span class="stage-icon stage-running spinner"></span>; return <span class="stage-icon stage-running spinner"></span>;
case 'skipped':
return <span class="stage-icon stage-skipped"></span>;
default: default:
return <span class="stage-icon stage-pending"></span>; 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 ( return (
<div class="pipeline-progress"> <div class="pipeline-progress">
<h3 class="pipeline-title">Generating Recommendations</h3> <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"> <ul class="pipeline-steps">
{STAGES.map(({ key, label }) => ( {group.stages.map(({ key, label }) => {
<li key={key} class={`pipeline-step pipeline-step--${stages[key]}`}> const status: StageStatus = stages[key] ?? 'pending';
<StageIcon status={stages[key]} /> return (
<li key={key} class={`pipeline-step pipeline-step--${status}`}>
<StageIcon status={status} />
<span class="pipeline-step-label">{label}</span> <span class="pipeline-step-label">{label}</span>
</li> </li>
))} );
})}
</ul> </ul>
</div> </div>
))}
{hasError && onRetry && (
<div class="pipeline-retry">
<button class="btn-primary" onClick={onRetry}>Re-run Pipeline</button>
</div>
)}
</div>
); );
} }

View File

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

View File

@@ -0,0 +1,166 @@
/* ── Sidebar ────────────────────────────────────────────── */
.sidebar {
width: var(--sidebar-width);
background: var(--bg-surface);
border-right: 1px solid var(--border);
display: flex;
flex-direction: column;
overflow: hidden;
flex-shrink: 0;
}
.sidebar-header {
padding: 20px 16px 12px;
border-bottom: 1px solid var(--border);
text-align: center;
}
.sidebar-title {
font-size: 24px;
font-weight: 700;
color: var(--text);
letter-spacing: -0.3px;
text-decoration: none;
display: inline-block;
transition: color 0.15s ease;
padding-bottom: 10px;
}
.sidebar-title:hover {
color: var(--accent);
}
.btn-new {
margin: 12px;
padding: 10px 14px;
background: var(--accent);
color: #fff;
border: none;
border-radius: var(--radius);
font-size: 13px;
font-weight: 600;
cursor: pointer;
transition: background 0.15s;
text-align: left;
}
.btn-new:hover {
background: var(--accent-hover);
}
.sidebar-section-label {
padding: 8px 16px 4px;
font-size: 11px;
font-weight: 600;
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--text-dim);
}
.sidebar-list {
list-style: none;
overflow-y: auto;
flex: 1;
padding: 4px 8px 8px;
}
.sidebar-empty {
padding: 8px;
color: var(--text-dim);
font-size: 13px;
}
.sidebar-item {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 10px;
border-radius: var(--radius-sm);
cursor: pointer;
color: var(--text-muted);
font-size: 13px;
transition: background 0.1s, color 0.1s;
user-select: none;
}
.sidebar-item:hover {
background: var(--bg-surface-2);
color: var(--text);
}
.sidebar-item.selected {
background: var(--accent-dim);
color: var(--text);
}
.sidebar-icon {
font-size: 12px;
width: 16px;
text-align: center;
flex-shrink: 0;
}
.status-done {
color: var(--green);
}
.status-error {
color: var(--red);
}
.status-running {
color: var(--accent);
}
.status-pending {
color: var(--text-dim);
}
.sidebar-item-title {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
flex: 1;
}
.sidebar-type-badge {
flex-shrink: 0;
font-size: 10px;
font-weight: 600;
padding: 2px 5px;
border-radius: 4px;
text-transform: uppercase;
letter-spacing: 0.04em;
}
.sidebar-type-tv_show {
background: rgba(99, 102, 241, 0.15);
color: #818cf8;
}
.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

@@ -1,3 +1,4 @@
import './Sidebar.css';
import type { RecommendationSummary } from '../types/index.js'; import type { RecommendationSummary } from '../types/index.js';
interface SidebarProps { interface SidebarProps {
@@ -29,7 +30,7 @@ export function Sidebar({ list, selectedId, onSelect, onNewClick }: SidebarProps
return ( return (
<aside class="sidebar"> <aside class="sidebar">
<div class="sidebar-header"> <div class="sidebar-header">
<span class="sidebar-title">Recommender</span> <a href="/" class="sidebar-title">Recommender</a>
</div> </div>
<button class="btn-new" onClick={onNewClick}> <button class="btn-new" onClick={onNewClick}>
@@ -52,6 +53,9 @@ export function Sidebar({ list, selectedId, onSelect, onNewClick }: SidebarProps
{statusIcon(item.status)} {statusIcon(item.status)}
</span> </span>
<span class="sidebar-item-title">{item.title}</span> <span class="sidebar-item-title">{item.title}</span>
<span class={`sidebar-type-badge sidebar-type-${item.media_type}`}>
{item.media_type === 'movie' ? 'Film' : 'TV'}
</span>
</li> </li>
))} ))}
</ul> </ul>

View File

@@ -0,0 +1,18 @@
import { createContext } from 'preact';
import { useContext } from 'preact/hooks';
import { useRecommendations } from '../hooks/useRecommendations.js';
import type { ComponentChildren } from 'preact';
type Value = ReturnType<typeof useRecommendations>;
const Ctx = createContext<Value | null>(null);
export function RecommendationsProvider({ children }: { children: ComponentChildren }) {
return <Ctx.Provider value={useRecommendations()}>{children}</Ctx.Provider>;
}
export function useRecommendationsContext(): Value {
const ctx = useContext(Ctx);
if (!ctx) throw new Error('Must be inside RecommendationsProvider');
return ctx;
}

View File

@@ -1,11 +1,12 @@
import { useState, useEffect, useCallback } from 'preact/hooks'; import { useState, useEffect, useCallback } from 'preact/hooks';
import type { RecommendationSummary, FeedbackEntry } from '../types/index.js'; import type { MediaType, RecommendationSummary, FeedbackEntry } from '../types/index.js';
import { import {
listRecommendations, listRecommendations,
createRecommendation, createRecommendation,
rerankRecommendation, rerankRecommendation,
submitFeedback, submitFeedback,
getFeedback, getFeedback,
deleteRecommendation,
} from '../api/client.js'; } from '../api/client.js';
export function useRecommendations() { export function useRecommendations() {
@@ -34,6 +35,14 @@ export function useRecommendations() {
liked_shows: string; liked_shows: string;
disliked_shows: string; disliked_shows: string;
themes: string; themes: string;
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); const { id } = await createRecommendation(body);
await refreshList(); await refreshList();
@@ -46,7 +55,6 @@ export function useRecommendations() {
const rerank = useCallback( const rerank = useCallback(
async (id: string) => { async (id: string) => {
await rerankRecommendation(id); await rerankRecommendation(id);
// Update local list to show pending status
setList((prev) => setList((prev) =>
prev.map((r) => (r.id === id ? { ...r, status: 'pending' as const } : r)), prev.map((r) => (r.id === id ? { ...r, status: 'pending' as const } : r)),
); );
@@ -55,7 +63,7 @@ export function useRecommendations() {
); );
const handleSubmitFeedback = useCallback( const handleSubmitFeedback = useCallback(
async (body: { tv_show_name: string; stars: number; feedback?: string }) => { async (body: { item_name: string; stars: number; feedback?: string }) => {
await submitFeedback(body); await submitFeedback(body);
await refreshFeedback(); await refreshFeedback();
}, },
@@ -69,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 { return {
list, list,
selectedId, selectedId,
@@ -78,6 +95,8 @@ export function useRecommendations() {
rerank, rerank,
submitFeedback: handleSubmitFeedback, submitFeedback: handleSubmitFeedback,
updateStatus, updateStatus,
updateTitle,
refreshList, refreshList,
deleteRec,
}; };
} }

View File

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

View File

@@ -1,4 +1,6 @@
*, *::before, *::after { *,
*::before,
*::after {
box-sizing: border-box; box-sizing: border-box;
margin: 0; margin: 0;
padding: 0; padding: 0;
@@ -30,7 +32,9 @@
-webkit-font-smoothing: antialiased; -webkit-font-smoothing: antialiased;
} }
html, body, #app { html,
body,
#app {
height: 100%; height: 100%;
width: 100%; width: 100%;
overflow: hidden; overflow: hidden;
@@ -50,112 +54,7 @@ html, body, #app {
padding: 32px; padding: 32px;
} }
/* ── Sidebar ────────────────────────────────────────────── */ /* ── Shared content areas ───────────────────────────────── */
.sidebar {
width: var(--sidebar-width);
background: var(--bg-surface);
border-right: 1px solid var(--border);
display: flex;
flex-direction: column;
overflow: hidden;
flex-shrink: 0;
}
.sidebar-header {
padding: 20px 16px 12px;
border-bottom: 1px solid var(--border);
}
.sidebar-title {
font-size: 16px;
font-weight: 700;
color: var(--text);
letter-spacing: -0.3px;
}
.btn-new {
margin: 12px;
padding: 10px 14px;
background: var(--accent);
color: #fff;
border: none;
border-radius: var(--radius);
font-size: 13px;
font-weight: 600;
cursor: pointer;
transition: background 0.15s;
text-align: left;
}
.btn-new:hover {
background: var(--accent-hover);
}
.sidebar-section-label {
padding: 8px 16px 4px;
font-size: 11px;
font-weight: 600;
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--text-dim);
}
.sidebar-list {
list-style: none;
overflow-y: auto;
flex: 1;
padding: 4px 8px 8px;
}
.sidebar-empty {
padding: 8px;
color: var(--text-dim);
font-size: 13px;
}
.sidebar-item {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 10px;
border-radius: var(--radius-sm);
cursor: pointer;
color: var(--text-muted);
font-size: 13px;
transition: background 0.1s, color 0.1s;
user-select: none;
}
.sidebar-item:hover {
background: var(--bg-surface-2);
color: var(--text);
}
.sidebar-item.selected {
background: var(--accent-dim);
color: var(--text);
}
.sidebar-icon {
font-size: 12px;
width: 16px;
text-align: center;
flex-shrink: 0;
}
.status-done { color: var(--green); }
.status-error { color: var(--red); }
.status-running { color: var(--accent); }
.status-pending { color: var(--text-dim); }
.sidebar-item-title {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* ── Main content areas ─────────────────────────────────── */
.empty-state { .empty-state {
display: flex; display: flex;
@@ -174,239 +73,6 @@ html, body, #app {
color: var(--text); color: var(--text);
} }
.content-area {
max-width: 860px;
margin: 0 auto;
}
.rec-title {
font-size: 20px;
font-weight: 700;
color: var(--text);
margin-bottom: 24px;
}
.error-state {
color: var(--text-muted);
}
.error-state h2 {
color: var(--red);
margin-bottom: 8px;
}
.error-state p {
margin-bottom: 16px;
}
/* ── Pipeline Progress ──────────────────────────────────── */
.pipeline-progress {
padding: 40px 0;
}
.pipeline-title {
font-size: 18px;
font-weight: 600;
color: var(--text);
margin-bottom: 24px;
}
.pipeline-steps {
list-style: none;
display: flex;
flex-direction: column;
gap: 12px;
}
.pipeline-step {
display: flex;
align-items: center;
gap: 12px;
padding: 12px 16px;
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius);
font-size: 14px;
}
.pipeline-step--running {
border-color: var(--accent);
background: var(--accent-dim);
}
.pipeline-step--done {
border-color: rgba(34, 197, 94, 0.3);
background: rgba(34, 197, 94, 0.05);
}
.pipeline-step--error {
border-color: rgba(239, 68, 68, 0.3);
background: rgba(239, 68, 68, 0.05);
}
.stage-icon {
font-size: 16px;
width: 20px;
text-align: center;
}
.stage-done { color: var(--green); }
.stage-error { color: var(--red); }
.stage-running { color: var(--accent); }
.stage-pending { color: var(--text-dim); }
.spinner {
display: inline-block;
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.pipeline-step-label {
font-weight: 500;
}
/* ── Cards ─────────────────────────────────────────────── */
.cards-grid {
display: flex;
flex-direction: column;
gap: 12px;
margin-bottom: 32px;
}
.card {
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 16px;
transition: border-color 0.15s;
}
.card:hover {
border-color: var(--bg-surface-3);
}
.card-header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
flex-wrap: wrap;
}
.badge {
font-size: 11px;
font-weight: 600;
padding: 2px 8px;
border-radius: 100px;
text-transform: uppercase;
letter-spacing: 0.04em;
flex-shrink: 0;
}
.badge-green { background: rgba(34,197,94,0.15); color: #4ade80; }
.badge-blue { background: rgba(59,130,246,0.15); color: #60a5fa; }
.badge-yellow{ background: rgba(234,179,8,0.15); color: #facc15; }
.badge-red { background: rgba(239,68,68,0.15); color: #f87171; }
.card-title {
font-size: 16px;
font-weight: 600;
color: var(--text);
}
.card-explanation {
font-size: 13px;
color: var(--text-muted);
line-height: 1.5;
margin-bottom: 12px;
}
.card-feedback {
border-top: 1px solid var(--border);
padding-top: 10px;
}
.star-rating {
display: flex;
align-items: center;
gap: 2px;
}
.star-btn {
background: none;
border: none;
cursor: pointer;
font-size: 18px;
color: var(--text-dim);
padding: 0 2px;
transition: color 0.1s, transform 0.1s;
}
.star-btn:hover,
.star-btn.star-active {
color: var(--yellow);
}
.star-btn:hover {
transform: scale(1.1);
}
.feedback-saved {
margin-left: 8px;
font-size: 12px;
color: var(--green);
font-weight: 500;
}
.comment-area {
margin-top: 8px;
display: flex;
flex-direction: column;
gap: 6px;
}
.comment-input {
font-size: 13px;
resize: vertical;
}
/* ── Rerank Button ──────────────────────────────────────── */
.rerank-section {
padding: 16px 0 32px;
display: flex;
justify-content: center;
}
.btn-rerank {
padding: 12px 28px;
background: var(--bg-surface-2);
color: var(--text);
border: 1px solid var(--border);
border-radius: var(--radius);
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: background 0.15s, border-color 0.15s;
}
.btn-rerank:hover:not(:disabled) {
background: var(--bg-surface-3);
border-color: var(--accent);
color: var(--accent);
}
.btn-rerank:disabled {
opacity: 0.4;
cursor: not-allowed;
}
/* ── Buttons ────────────────────────────────────────────── */ /* ── Buttons ────────────────────────────────────────────── */
.btn-primary { .btn-primary {
@@ -450,109 +116,3 @@ html, body, #app {
.btn-secondary:hover:not(:disabled) { .btn-secondary:hover:not(:disabled) {
background: var(--bg-surface-3); background: var(--bg-surface-3);
} }
/* ── Modal ──────────────────────────────────────────────── */
.modal-backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.6);
display: flex;
align-items: center;
justify-content: center;
z-index: 100;
backdrop-filter: blur(2px);
}
.modal {
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: 12px;
width: 540px;
max-width: 96vw;
max-height: 90vh;
overflow-y: auto;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px 20px 0;
}
.modal-header h2 {
font-size: 18px;
font-weight: 700;
}
.modal-close {
background: none;
border: none;
font-size: 22px;
color: var(--text-muted);
cursor: pointer;
line-height: 1;
padding: 0;
}
.modal-close:hover {
color: var(--text);
}
.modal-form {
padding: 20px;
display: flex;
flex-direction: column;
gap: 16px;
}
.form-group {
display: flex;
flex-direction: column;
gap: 6px;
flex: 1;
}
.form-group label {
font-size: 13px;
font-weight: 500;
color: var(--text-muted);
}
.form-row {
display: flex;
gap: 12px;
}
.form-input,
.form-textarea {
background: var(--bg-surface-2);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
color: var(--text);
font-size: 14px;
padding: 10px 12px;
outline: none;
transition: border-color 0.15s;
font-family: inherit;
width: 100%;
}
.form-input:focus,
.form-textarea:focus {
border-color: var(--accent);
}
.form-textarea {
resize: vertical;
min-height: 80px;
}
.modal-actions {
display: flex;
justify-content: flex-end;
gap: 10px;
padding-top: 4px;
}

View File

@@ -0,0 +1,73 @@
/* ── Landing page ───────────────────────────────────────── */
.landing-layout {
display: flex;
height: 100vh;
position: relative;
overflow: hidden;
}
.landing-bg {
position: absolute;
inset: -6px;
/* overflow slightly to hide blur edges */
background: url('/wallpaper.png') center / cover no-repeat;
filter: blur(3px) brightness(0.35);
z-index: 0;
}
.landing-layout .sidebar {
position: relative;
z-index: 1;
background: rgba(26, 29, 39, 0.85);
backdrop-filter: blur(8px);
}
.landing-main {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 28px;
text-align: center;
position: relative;
z-index: 1;
padding: 40px;
}
.landing-title {
font-size: 5rem;
font-weight: 800;
color: #fff;
letter-spacing: -2px;
line-height: 1;
}
.landing-tagline {
font-size: 1.4rem;
color: rgba(255, 255, 255, 0.6);
font-weight: 400;
}
.btn-gradient {
padding: 16px 42px;
font-size: 1.20rem;
font-weight: 700;
color: rgba(255, 255, 255, 0.9);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
cursor: pointer;
background: linear-gradient(135deg, rgba(113, 88, 226, 0.8), rgba(61, 59, 243, 0.8));
backdrop-filter: blur(6px);
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2), inset 0 1px 1px rgba(255, 255, 255, 0.1);
transition: all 0.6s cubic-bezier(0.25, 0.8, 0.25, 1);
letter-spacing: 0.02em;
}
.btn-gradient:hover {
transform: translateY(-2px);
color: #fff;
background: linear-gradient(135deg, rgba(113, 88, 226, 0.95), rgba(41, 121, 255, 0.95));
box-shadow: 0 8px 24px rgba(113, 88, 226, 0.3), inset 0 1px 2px rgba(255, 255, 255, 0.2);
}

View File

@@ -1,186 +1,45 @@
import { useState, useCallback, useEffect } from 'preact/hooks'; import { useState } from 'preact/hooks';
import './Home.css';
import { route } from 'preact-router';
import { Sidebar } from '../components/Sidebar.js'; import { Sidebar } from '../components/Sidebar.js';
import { NewRecommendationModal } from '../components/NewRecommendationModal.js'; import { NewRecommendationModal } from '../components/NewRecommendationModal.js';
import { PipelineProgress } from '../components/PipelineProgress.js'; import { useRecommendationsContext } from '../context/RecommendationsContext.js';
import { RecommendationCard } from '../components/RecommendationCard.js';
import { useRecommendations } from '../hooks/useRecommendations.js';
import { useSSE } from '../hooks/useSSE.js';
import { getRecommendation } from '../api/client.js';
import type { Recommendation, SSEEvent, StageMap, PipelineStage } from '../types/index.js';
const DEFAULT_STAGES: StageMap = {
interpreter: 'pending',
retrieval: 'pending',
ranking: 'pending',
curator: 'pending',
};
const STAGE_ORDER: (keyof StageMap)[] = ['interpreter', 'retrieval', 'ranking', 'curator'];
export function Home() { export function Home() {
const { const { list, createNew } = useRecommendationsContext();
list,
selectedId,
feedback,
setSelectedId,
createNew,
rerank,
submitFeedback,
updateStatus,
refreshList,
} = useRecommendations();
const [showModal, setShowModal] = useState(false); const [showModal, setShowModal] = useState(false);
const [selectedRec, setSelectedRec] = useState<Recommendation | null>(null);
const [stages, setStages] = useState<StageMap>(DEFAULT_STAGES);
const [sseUrl, setSseUrl] = useState<string | null>(null);
// Load full recommendation when selected const handleSelect = (id: string) => route(`/recom/${id}`);
useEffect(() => {
if (!selectedId) {
setSelectedRec(null);
return;
}
void getRecommendation(selectedId).then((rec) => {
setSelectedRec(rec);
// If already running or pending, open SSE
if (rec.status === 'running' || rec.status === 'pending') {
setStages(DEFAULT_STAGES);
setSseUrl(`/api/recommendations/${selectedId}/stream`);
}
});
}, [selectedId]);
const handleSSEEvent = useCallback(
(event: SSEEvent) => {
if (!selectedId) return;
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',
}));
}
}
if (event.stage === 'complete' && event.status === 'done') {
setSseUrl(null);
updateStatus(selectedId, 'done');
// Reload full recommendation to get results
void getRecommendation(selectedId).then(setSelectedRec);
void refreshList();
}
if (event.status === 'error') {
setSseUrl(null);
updateStatus(selectedId, 'error');
const stageKey = event.stage as PipelineStage;
if (stageKey !== 'complete') {
setStages((prev) => ({ ...prev, [stageKey as keyof StageMap]: 'error' }));
}
}
},
[selectedId, updateStatus, refreshList],
);
useSSE(sseUrl, handleSSEEvent);
const handleSelect = (id: string) => {
setSseUrl(null);
setStages(DEFAULT_STAGES);
setSelectedId(id);
};
const handleCreateNew = async (body: { const handleCreateNew = async (body: {
main_prompt: string; main_prompt: string;
liked_shows: string; liked_shows: string;
disliked_shows: string; disliked_shows: string;
themes: string; themes: string;
brainstorm_count?: number;
media_type: import('../types/index.js').MediaType;
use_web_search?: boolean;
}) => { }) => {
const id = await createNew(body); const id = await createNew(body);
setStages(DEFAULT_STAGES); route(`/recom/${id}`);
setSseUrl(`/api/recommendations/${id}/stream`);
}; };
const handleRerank = async () => {
if (!selectedId) return;
await rerank(selectedId);
setStages(DEFAULT_STAGES);
setSseUrl(`/api/recommendations/${selectedId}/stream`);
setSelectedRec((prev) => (prev ? { ...prev, status: 'pending' } : null));
};
const isRunning =
selectedRec?.status === 'running' || selectedRec?.status === 'pending' || !!sseUrl;
const feedbackMap = new Map(feedback.map((f) => [f.tv_show_name, f]));
return ( return (
<div class="layout"> <div class="landing-layout">
<div class="landing-bg" />
<Sidebar <Sidebar
list={list} list={list}
selectedId={selectedId} selectedId={null}
onSelect={handleSelect} onSelect={handleSelect}
onNewClick={() => setShowModal(true)} onNewClick={() => setShowModal(true)}
/> />
<main class="landing-main">
<main class="main-content"> <h1 class="landing-title">Recommender</h1>
{!selectedId && ( <p class="landing-tagline">Discover your next favorite show or movie, powered by AI.</p>
<div class="empty-state"> <button class="btn-gradient" onClick={() => setShowModal(true)}>
<h2>TV Show Recommender</h2> Get Started
<p>Click <strong>+ New Recommendation</strong> to get started.</p>
</div>
)}
{selectedId && isRunning && (
<div class="content-area">
<PipelineProgress stages={stages} />
</div>
)}
{selectedId && !isRunning && selectedRec?.status === 'done' && selectedRec.recommendations && (
<div class="content-area">
<h2 class="rec-title">{selectedRec.title}</h2>
<div class="cards-grid">
{selectedRec.recommendations.map((show) => (
<RecommendationCard
key={show.title}
show={show}
existingFeedback={feedbackMap.get(show.title)}
onFeedback={async (name, stars, comment) => {
await submitFeedback({ tv_show_name: name, stars, feedback: comment });
}}
/>
))}
</div>
<div class="rerank-section">
<button
class="btn-rerank"
onClick={handleRerank}
disabled={feedback.length === 0}
title={feedback.length === 0 ? 'Rate at least one show to enable re-ranking' : 'Re-rank based on your feedback'}
>
Re-rank with Feedback {feedback.length > 0 ? `(${feedback.length} rated)` : ''}
</button> </button>
</div>
</div>
)}
{selectedId && !isRunning && selectedRec?.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
</button>
</div>
)}
</main> </main>
{showModal && ( {showModal && (
<NewRecommendationModal <NewRecommendationModal
onClose={() => setShowModal(false)} onClose={() => setShowModal(false)}

View File

@@ -0,0 +1,149 @@
/* ── Recom page ─────────────────────────────────────────── */
.recom-content {
min-height: 100%;
}
.content-area {
max-width: 860px;
margin: 0 auto;
}
.rec-title {
font-size: 20px;
font-weight: 700;
color: var(--text);
margin-bottom: 24px;
}
/* ── Request Info Panel ─────────────────────────────────── */
.rec-info-panel {
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius);
margin-bottom: 24px;
overflow: hidden;
}
.rec-info-toggle {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
padding: 14px 16px;
background: none;
border: none;
cursor: pointer;
text-align: left;
gap: 8px;
}
.rec-info-toggle:hover {
background: var(--bg-surface-2);
}
.rec-info-title {
font-size: 15px;
font-weight: 700;
color: var(--text);
}
.rec-info-chevron {
font-size: 10px;
color: var(--text-dim);
flex-shrink: 0;
}
.rec-info-body {
padding: 0 16px 14px;
display: flex;
flex-direction: column;
gap: 10px;
border-top: 1px solid var(--border);
padding-top: 14px;
}
.rec-info-row {
display: flex;
gap: 12px;
align-items: baseline;
}
.rec-info-label {
font-size: 11px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.06em;
color: var(--text-dim);
flex-shrink: 0;
width: 72px;
}
.rec-info-value {
font-size: 13px;
color: var(--text-muted);
line-height: 1.5;
}
.rec-info-badge {
font-size: 11px;
font-weight: 600;
padding: 2px 8px;
border-radius: 100px;
background: rgba(99, 102, 241, 0.15);
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);
}
.error-state h2 {
color: var(--red);
margin-bottom: 8px;
}
.error-state p {
margin-bottom: 16px;
}

View File

@@ -0,0 +1,355 @@
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, StageStatus } from '../types/index.js';
interface RecomProps {
id: string;
path?: string;
}
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;
}
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, updateTitle, refreshList, createNew, deleteRec } = useRecommendationsContext();
const [rec, setRec] = useState<Recommendation | 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(buildDefaultStages(null));
setSseKey(null);
getRecommendation(id)
.then((data) => {
setRec(data);
if (data.status === 'running' || data.status === 'pending') {
setStages(buildDefaultStages(data));
setSseKey(Date.now());
}
})
.catch(() => route('/'));
}, [id]);
const handleSSEEvent = useCallback(
(event: SSEEvent) => {
if (event.stage !== 'complete') {
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') {
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)
.catch(() => {
// Fetch failed after completion — poll once more via handleSSEClose logic
updateStatus(id, 'done');
});
void refreshList();
}
if (event.status === 'error') {
setSseKey(null);
updateStatus(id, 'error');
setRec((prev) => (prev ? { ...prev, status: 'error' as const } : null));
const stageKey = event.stage as string;
if (stageKey !== 'complete') {
setStages((prev) => (stageKey in prev ? { ...prev, [stageKey]: 'error' } : prev));
}
}
},
[id, updateStatus, updateTitle, refreshList],
);
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(buildDefaultStages(rec));
setSseKey(Date.now());
setRec((prev) => (prev ? { ...prev, status: 'pending' as const } : null));
};
const handleCreateNew = async (body: {
main_prompt: string;
liked_shows: string;
disliked_shows: string;
themes: string;
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' || sseKey !== null;
const feedbackMap = new Map(feedback.map((f) => [f.item_name, f]));
const stageGroups = buildStageGroups(rec);
return (
<div class="layout">
<Sidebar
list={list}
selectedId={id}
onSelect={(sid) => route(`/recom/${sid}`)}
onNewClick={() => setShowModal(true)}
/>
<main class="main-content">
<div class="recom-content">
{rec && (
<div class="content-area">
<div class="rec-info-panel">
<button class="rec-info-toggle" onClick={() => setInfoExpanded((e) => !e)}>
<span class="rec-info-title">{rec.title}</span>
<span class="rec-info-chevron">{infoExpanded ? '▲' : '▼'}</span>
</button>
{infoExpanded && (
<div class="rec-info-body">
{rec.main_prompt && (
<div class="rec-info-row">
<span class="rec-info-label">Description</span>
<span class="rec-info-value">{rec.main_prompt}</span>
</div>
)}
{rec.liked_shows && (
<div class="rec-info-row">
<span class="rec-info-label">Liked</span>
<span class="rec-info-value">{rec.liked_shows}</span>
</div>
)}
{rec.disliked_shows && (
<div class="rec-info-row">
<span class="rec-info-label">Disliked</span>
<span class="rec-info-value">{rec.disliked_shows}</span>
</div>
)}
{rec.themes && (
<div class="rec-info-row">
<span class="rec-info-label">Themes</span>
<span class="rec-info-value">{rec.themes}</span>
</div>
)}
<div class="rec-info-row">
<span class="rec-info-label">Media</span>
<span class="rec-info-value">{rec.media_type === 'tv_show' ? 'TV Shows' : 'Movies'}</span>
</div>
{rec.use_web_search && (
<div class="rec-info-row">
<span class="rec-info-label">Web Search</span>
<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>
</div>
)}
{isRunning && (
<div class="content-area">
<PipelineProgress stageGroups={stageGroups} stages={stages} onRetry={handleRetry} />
</div>
)}
{!isRunning && rec?.status === 'done' && rec.recommendations && (
<div class="content-area">
<div class="cards-grid">
{rec.recommendations.map((show) => (
<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 });
}}
/>
))}
</div>
<div class="rerank-section">
<button
class="btn-rerank"
onClick={handleRerank}
disabled={feedback.length === 0}
title={feedback.length === 0 ? 'Rate at least one show to enable re-ranking' : 'Re-rank based on your feedback'}
>
Re-rank with Feedback {feedback.length > 0 ? `(${feedback.length} rated)` : ''}
</button>
</div>
</div>
)}
{!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 the button below.</p>
<button class="btn-primary" onClick={handleRetry}>
Re-run Pipeline
</button>
</div>
)}
</div>
</main>
{showModal && (
<NewRecommendationModal
onClose={() => setShowModal(false)}
onSubmit={handleCreateNew}
/>
)}
</div>
);
}

View File

@@ -1,9 +1,14 @@
export type CuratorCategory = 'Definitely Like' | 'Might Like' | 'Questionable' | 'Will Not Like'; export type MediaType = 'tv_show' | 'movie';
export type CuratorCategory = 'Full Match' | 'Definitely Like' | 'Might Like' | 'Questionable' | 'Will Not Like';
export interface CuratorOutput { export interface CuratorOutput {
title: string; title: string;
explanation: string; explanation: string;
category: CuratorCategory; category: CuratorCategory;
genre: string;
pros: string[];
cons: string[];
} }
export type RecommendationStatus = 'pending' | 'running' | 'done' | 'error'; export type RecommendationStatus = 'pending' | 'running' | 'done' | 'error';
@@ -15,6 +20,13 @@ export interface Recommendation {
liked_shows: string; liked_shows: string;
disliked_shows: string; disliked_shows: string;
themes: string; 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; recommendations: CuratorOutput[] | null;
status: RecommendationStatus; status: RecommendationStatus;
created_at: string; created_at: string;
@@ -24,18 +36,30 @@ export interface RecommendationSummary {
id: string; id: string;
title: string; title: string;
status: RecommendationStatus; status: RecommendationStatus;
media_type: MediaType;
created_at: string; created_at: string;
} }
export interface FeedbackEntry { export interface FeedbackEntry {
id: string; id: string;
tv_show_name: string; item_name: string;
stars: number; stars: number;
feedback: string; feedback: string;
created_at: string; 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 type SSEStatus = 'start' | 'done' | 'error';
export interface SSEEvent { export interface SSEEvent {
@@ -44,6 +68,6 @@ export interface SSEEvent {
data?: unknown; 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>;