-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathroute.ts
More file actions
881 lines (766 loc) · 32.8 KB
/
route.ts
File metadata and controls
881 lines (766 loc) · 32.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
export const fetchCache = 'force-no-store';
export const maxDuration = 60;
import { type NextRequest } from 'next/server';
import { createClient, type SanityClient } from 'next-sanity';
import { apiVersion, dataset, projectId } from '@/sanity/lib/api';
import { pollResearch, parseResearchReport } from '@/lib/services/gemini-research';
import { generateInfographicsForTopic } from '@/lib/services/gemini-infographics';
import { generateWithGemini, stripCodeFences } from '@/lib/gemini';
import { getConfigValue } from '@/lib/config';
import type { ResearchPayload } from '@/lib/services/research';
import { writeClient } from '@/lib/sanity-write-client';
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface PipelineDoc {
_id: string;
title: string;
status: string;
researchInteractionId?: string;
researchNotebookId?: string;
trendScore?: number;
trendSources?: string;
script?: {
hook: string;
scenes: Array<{
_key: string;
sceneNumber: number;
sceneType: string;
narration: string;
visualDescription: string;
bRollKeywords: string[];
durationEstimate: number;
code?: { snippet: string; language: string; highlightLines?: number[] };
list?: { items: string[]; icon?: string };
comparison?: {
leftLabel: string;
rightLabel: string;
rows: { left: string; right: string }[];
};
mockup?: { deviceType: string; screenContent: string };
}>;
cta: string;
};
researchData?: string;
_updatedAt: string;
}
interface EnrichedScript {
title: string;
summary: string;
sourceUrl: string;
topics: string[];
script: {
hook: string;
scenes: Array<{
sceneNumber: number;
sceneType: string;
narration: string;
visualDescription: string;
bRollKeywords: string[];
durationEstimate: number;
code?: { snippet: string; language: string; highlightLines?: number[] };
list?: { items: string[]; icon?: string };
comparison?: {
leftLabel: string;
rightLabel: string;
rows: { left: string; right: string }[];
};
mockup?: { deviceType: string; screenContent: string };
}>;
cta: string;
};
qualityScore: number;
}
interface CriticResult {
score: number;
issues: string[];
summary: string;
}
interface StepResult {
id: string;
title: string;
step: string;
outcome: string;
error?: string;
}
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** Build stuck thresholds from config (with fallbacks) */
async function buildStuckThresholds(): Promise<Record<string, number>> {
const stuckMinutes = await getConfigValue('pipeline_config', 'stuckTimeoutMinutes', 30);
return {
researching: stuckMinutes * 60 * 1000,
infographics_generating: Math.round(stuckMinutes * 0.5) * 60 * 1000, // half the main timeout
enriching: Math.round(stuckMinutes * 0.33) * 60 * 1000, // third of main timeout
};
}
/** Max docs to process per status per run — keeps total time well under 60s */
const MAX_DOCS_PER_STATUS = 2;
// ---------------------------------------------------------------------------
// Sanity Write Client
// ---------------------------------------------------------------------------
function getSanityWriteClient(): SanityClient {
const token = process.env.SANITY_API_TOKEN || process.env.SANITY_API_WRITE_TOKEN;
if (!token) {
throw new Error('[check-research] Missing SANITY_API_TOKEN environment variable');
}
return createClient({ projectId, dataset, apiVersion, token, useCdn: false });
}
// ---------------------------------------------------------------------------
// Stuck detection — runs FIRST, no external API calls needed
// ---------------------------------------------------------------------------
async function flagStuckDocs(
docs: PipelineDoc[],
sanity: SanityClient,
stuckThresholds: Record<string, number>,
): Promise<StepResult[]> {
const results: StepResult[] = [];
const now = Date.now();
for (const doc of docs) {
const threshold = stuckThresholds[doc.status];
if (!threshold) continue;
const docAge = now - new Date(doc._updatedAt).getTime();
if (docAge > threshold) {
const ageMin = Math.round(docAge / 60_000);
console.warn(
`[check-research] Doc ${doc._id} ("${doc.title}") stuck in "${doc.status}" for ${ageMin}min — flagging`,
);
try {
await sanity
.patch(doc._id)
.set({
status: 'flagged',
flaggedReason: `Stuck in "${doc.status}" for ${ageMin} minutes (threshold: ${Math.round(threshold / 60_000)}min). May need manual intervention.`,
})
.commit();
results.push({
id: doc._id,
title: doc.title,
step: 'stuck-detection',
outcome: 'flagged',
error: `Stuck in "${doc.status}" for ${ageMin}min`,
});
} catch (err) {
console.error(`[check-research] Failed to flag stuck doc ${doc._id}:`, err);
}
}
}
return results;
}
// ---------------------------------------------------------------------------
// Step 1: researching → research_complete (Gemini Deep Research polling)
// ---------------------------------------------------------------------------
async function stepResearching(
doc: PipelineDoc,
sanity: SanityClient,
): Promise<StepResult> {
// Use researchInteractionId (new Gemini) or fall back to researchNotebookId (legacy)
const interactionId = doc.researchInteractionId;
if (!interactionId) {
// Legacy doc without interaction ID — skip to enriching with existing data
console.warn(`[check-research] No researchInteractionId for "${doc.title}" — skipping to enriching`);
await sanity.patch(doc._id).set({ status: 'enriching' }).commit();
return { id: doc._id, title: doc.title, step: 'researching', outcome: 'no_interaction_skip_to_enriching' };
}
console.log(`[check-research] Polling research for "${doc.title}" (interaction: ${interactionId})`);
const result = await pollResearch(interactionId);
if (result.status === 'in_progress') {
return { id: doc._id, title: doc.title, step: 'researching', outcome: 'still_in_progress' };
}
if (result.status === 'failed' || result.status === 'not_found') {
console.error(`[check-research] Research ${result.status} for "${doc.title}": ${result.error}`);
await sanity.patch(doc._id).set({
status: 'flagged',
flaggedReason: `Research ${result.status}: ${result.error || 'Unknown error'}`,
}).commit();
return { id: doc._id, title: doc.title, step: 'researching', outcome: result.status, error: result.error };
}
// Research completed — parse the report into structured data
const report = result.report || '';
console.log(`[check-research] Research completed for "${doc.title}" (${report.length} chars)`);
const researchPayload = await parseResearchReport(doc.title, report);
// Save research data and advance to research_complete
await sanity.patch(doc._id).set({
status: 'research_complete',
researchData: JSON.stringify(researchPayload),
}).commit();
console.log(`[check-research] "${doc.title}" → research_complete`);
return { id: doc._id, title: doc.title, step: 'researching', outcome: 'research_complete' };
}
// ---------------------------------------------------------------------------
// Step 2: research_complete → enriching (Gemini Imagen infographics)
// ---------------------------------------------------------------------------
async function stepResearchComplete(
doc: PipelineDoc,
sanity: SanityClient,
): Promise<StepResult> {
console.log(`[check-research] Generating infographics for "${doc.title}"`);
// Parse research data for briefing context
let briefing = '';
if (doc.researchData) {
try {
const data = JSON.parse(doc.researchData) as { briefing?: string };
briefing = data.briefing || '';
} catch { /* ignore */ }
}
try {
// Generate all infographics using Imagen 4 Fast
const batchResult = await generateInfographicsForTopic(doc.title, briefing);
console.log(`[check-research] Generated ${batchResult.results.length} infographics, ${batchResult.errors.length} failed`);
// Upload each generated image to Sanity
const infographicRefs: Array<{
_type: 'image';
_key: string;
alt?: string;
asset: { _type: 'reference'; _ref: string };
}> = [];
const infographicUrls: string[] = [];
for (let i = 0; i < batchResult.results.length; i++) {
const imgResult = batchResult.results[i];
try {
const buffer = Buffer.from(imgResult.imageBase64, 'base64');
const filename = `infographic-${doc._id}-${i}.png`;
const asset = await writeClient.assets.upload('image', buffer, {
filename,
contentType: imgResult.mimeType,
});
console.log(`[check-research] Uploaded infographic ${i + 1}: ${asset._id}`);
infographicRefs.push({
_type: 'image',
_key: `infographic-${i}`,
alt: `Research infographic ${i + 1} for ${doc.title}`,
asset: { _type: 'reference', _ref: asset._id },
});
// Build CDN URL for backward compat
const cdnUrl = `https://cdn.sanity.io/images/${projectId}/${dataset}/${asset._id.replace('image-', '').replace('-png', '.png').replace('-jpg', '.jpg')}`;
infographicUrls.push(cdnUrl);
} catch (err) {
console.warn(`[check-research] Failed to upload infographic ${i}:`, err instanceof Error ? err.message : err);
}
}
// Update research data with infographic URLs
let researchData: Record<string, unknown> = {};
if (doc.researchData) {
try { researchData = JSON.parse(doc.researchData); } catch { /* ignore */ }
}
researchData.infographicUrls = infographicUrls;
const patchData: Record<string, unknown> = {
status: 'enriching',
researchData: JSON.stringify(researchData),
};
if (infographicRefs.length > 0) {
patchData.infographics = infographicRefs;
}
await sanity.patch(doc._id).set(patchData).commit();
console.log(`[check-research] "${doc.title}" → enriching (${infographicRefs.length} infographics)`);
return { id: doc._id, title: doc.title, step: 'research_complete', outcome: 'enriching' };
} catch (err) {
// Infographic generation failed — skip to enriching without infographics
console.error(`[check-research] Infographic generation failed for "${doc.title}":`, err);
await sanity.patch(doc._id).set({ status: 'enriching' }).commit();
return { id: doc._id, title: doc.title, step: 'research_complete', outcome: 'enriching_no_infographics', error: err instanceof Error ? err.message : String(err) };
}
}
// ---------------------------------------------------------------------------
// Step 3: infographics_generating → enriching (legacy migration handler)
// ---------------------------------------------------------------------------
async function stepInfographicsGenerating(
doc: PipelineDoc,
sanity: SanityClient,
): Promise<StepResult> {
// Legacy migration: docs stuck in infographics_generating from old NotebookLM pipeline
// Just advance them to enriching — they may or may not have infographics
console.warn(`[check-research] Legacy doc "${doc.title}" in infographics_generating — advancing to enriching`);
await sanity.patch(doc._id).set({ status: 'enriching' }).commit();
return { id: doc._id, title: doc.title, step: 'infographics_generating', outcome: 'legacy_advance_to_enriching' };
}
// ---------------------------------------------------------------------------
// Step 4: enriching → script_ready
// ---------------------------------------------------------------------------
async function stepEnriching(
doc: PipelineDoc,
sanity: SanityClient,
): Promise<StepResult> {
console.log(`[check-research] Step 4: Enriching script for "${doc.title}"`);
// Parse research data from Sanity
let researchData: Record<string, unknown> = {};
if (doc.researchData) {
try {
researchData = JSON.parse(doc.researchData) as Record<string, unknown>;
} catch {
console.warn(`[check-research] Failed to parse researchData for "${doc.title}"`);
}
}
// Build full research payload
const researchPayload = buildResearchPayload(doc, researchData);
// Generate enriched script with Gemini
let enrichedScript: EnrichedScript | null = null;
try {
const SYSTEM_INSTRUCTION = await getConfigValue(
'content_config',
'systemInstruction',
SYSTEM_INSTRUCTION_FALLBACK,
);
const prompt = buildEnrichmentPrompt(doc, researchPayload);
const rawResponse = await generateWithGemini(prompt, SYSTEM_INSTRUCTION);
const cleaned = stripCodeFences(rawResponse);
enrichedScript = JSON.parse(cleaned) as EnrichedScript;
console.log(`[check-research] Enriched script generated: "${enrichedScript.title}"`);
} catch (err) {
console.error('[check-research] Failed to generate enriched script:', err);
}
if (enrichedScript) {
// Run critic pass
const criticResult = await claudeCritic(enrichedScript);
const criticScore = criticResult.score;
console.log(`[check-research] Critic score: ${criticScore}/100 — ${criticResult.summary}`);
const qualityThreshold = await getConfigValue('pipeline_config', 'qualityThreshold', 50);
const isFlagged = criticScore < qualityThreshold;
await sanity
.patch(doc._id)
.set({
script: {
...enrichedScript.script,
scenes: enrichedScript.script.scenes.map((scene, i) => ({
...scene,
_key: `scene-${i + 1}`,
})),
},
scriptQualityScore: criticScore,
status: isFlagged ? 'flagged' : 'script_ready',
researchData: JSON.stringify(researchPayload),
...(isFlagged && {
flaggedReason: `Quality score ${criticScore}/100. Issues: ${(criticResult.issues ?? []).join('; ') || 'Low quality score'}`,
}),
})
.commit();
console.log(`[check-research] "${doc.title}" → ${isFlagged ? 'flagged' : 'script_ready'} (score: ${criticScore})`);
return {
id: doc._id,
title: doc.title,
step: 'enriching',
outcome: isFlagged ? 'flagged' : 'script_ready',
};
}
// Fallback: no enriched script — transition with existing script
console.warn(`[check-research] No enriched script — transitioning "${doc.title}" to script_ready with existing script`);
await sanity
.patch(doc._id)
.set({
status: 'script_ready',
researchData: JSON.stringify(researchPayload),
})
.commit();
return { id: doc._id, title: doc.title, step: 'enriching', outcome: 'script_ready_fallback' };
}
// ---------------------------------------------------------------------------
// Gemini Script Enrichment
// ---------------------------------------------------------------------------
// SYSTEM_INSTRUCTION fallback — used when content_config singleton doesn't exist yet in Sanity.
// The live value is fetched from getConfigValue() inside stepEnriching().
const SYSTEM_INSTRUCTION_FALLBACK = `You are a content strategist and scriptwriter for CodingCat.dev, a web development education channel run by Alex Patterson.
Your style is inspired by Cleo Abram's "Huge If True" — you make complex technical topics feel exciting, accessible, and important. Key principles:
- Start with a BOLD claim or surprising fact that makes people stop scrolling
- Use analogies and real-world comparisons to explain technical concepts
- Build tension: "Here's the problem... here's why it matters... here's the breakthrough"
- Keep energy HIGH — short sentences, active voice, conversational tone
- End with a clear takeaway that makes the viewer feel smarter
- Target audience: developers who want to stay current but don't have time to read everything
Script format: 60-90 second explainer videos. Think TikTok/YouTube Shorts energy with real educational depth.
CodingCat.dev covers: React, Next.js, TypeScript, Svelte, web APIs, CSS, Node.js, cloud services, AI/ML for developers, and web platform updates.`;
function buildEnrichmentPrompt(
doc: PipelineDoc,
research: ResearchPayload,
): string {
const existingScript = doc.script
? JSON.stringify(doc.script, null, 2)
: 'No existing script';
let researchContext = '';
researchContext += `### Briefing\n${research.briefing}\n\n`;
if (research.talkingPoints.length > 0) {
researchContext += `### Key Talking Points\n${research.talkingPoints.map((tp, i) => `${i + 1}. ${tp}`).join('\n')}\n\n`;
}
if (research.codeExamples.length > 0) {
researchContext += `### Code Examples (use these in "code" scenes)\n`;
for (const ex of research.codeExamples.slice(0, 5)) {
researchContext += `\`\`\`${ex.language}\n${ex.snippet}\n\`\`\`\nContext: ${ex.context}\n\n`;
}
}
if (research.comparisonData && research.comparisonData.length > 0) {
researchContext += `### Comparison Data (use in "comparison" scenes)\n`;
for (const comp of research.comparisonData) {
researchContext += `${comp.leftLabel} vs ${comp.rightLabel}:\n`;
for (const row of comp.rows) {
researchContext += ` - ${row.left} | ${row.right}\n`;
}
researchContext += '\n';
}
}
if (research.sceneHints.length > 0) {
researchContext += `### Scene Type Suggestions\n`;
for (const hint of research.sceneHints) {
researchContext += `- ${hint.suggestedSceneType}: ${hint.reason}\n`;
}
}
if (research.infographicUrls && research.infographicUrls.length > 0) {
researchContext += `\n### Infographics Available (${research.infographicUrls.length})\nMultiple infographics have been generated for this topic. Use sceneType "narration" with bRollUrl pointing to an infographic for visual scenes.\n`;
}
return `You have an existing video script for "${doc.title}" and new deep research data.
Re-write the script to be MORE accurate, MORE insightful, and MORE engaging using the research.
## Existing Script
${existingScript}
## Research Data (use this to create an informed, accurate script)
${researchContext}
Re-generate the complete video script as JSON. Keep the same format but enrich it with research insights.
## Scene Types
Each scene MUST have a "sceneType" that determines its visual treatment:
- **"code"** — code snippets, API usage, config files. Provide actual code in the "code" field.
- **"list"** — enumerated content: "Top 5 features", key takeaways. Provide items in the "list" field.
- **"comparison"** — A-vs-B content. Provide structured data in the "comparison" field.
- **"mockup"** — UI, website, app screen, or terminal output. Provide device type and content in the "mockup" field.
- **"narration"** — conceptual explanations, introductions, or transitions. Default/fallback.
## JSON Schema
Return ONLY a JSON object:
{
"title": "string - catchy video title",
"summary": "string - 1-2 sentence summary",
"sourceUrl": "string - URL of the primary source",
"topics": ["string array of relevant tags"],
"script": {
"hook": "string - attention-grabbing opening line (5-10 seconds)",
"scenes": [
{
"sceneNumber": 1,
"sceneType": "code | list | comparison | mockup | narration",
"narration": "string - what the narrator says",
"visualDescription": "string - what to show on screen",
"bRollKeywords": ["keyword1", "keyword2"],
"durationEstimate": 15,
"code": { "snippet": "string", "language": "string", "highlightLines": [1, 3] },
"list": { "items": ["Item 1", "Item 2"], "icon": "🚀" },
"comparison": { "leftLabel": "A", "rightLabel": "B", "rows": [{ "left": "...", "right": "..." }] },
"mockup": { "deviceType": "browser | phone | terminal", "screenContent": "..." }
}
],
"cta": "string - call to action"
},
"qualityScore": 75
}
Requirements:
- 3-5 scenes totaling 60-90 seconds
- Use at least 2 different scene types
- Include REAL code snippets from the research where applicable
- The qualityScore should be your honest self-assessment (0-100)
- Return ONLY the JSON object, no markdown or extra text`;
}
// ---------------------------------------------------------------------------
// Claude Critic (optional — degrades gracefully without ANTHROPIC_API_KEY)
// ---------------------------------------------------------------------------
async function claudeCritic(script: EnrichedScript): Promise<CriticResult> {
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
if (!ANTHROPIC_API_KEY) {
return { score: script.qualityScore, issues: [], summary: 'No critic available' };
}
try {
const res = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01',
},
body: JSON.stringify({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [
{
role: 'user',
content: `You are a quality reviewer for short-form educational video scripts about web development.
Review this video script and provide a JSON response with:
- "score": number 0-100 (overall quality rating)
- "issues": string[] (list of specific problems, if any)
- "summary": string (brief overall assessment)
Evaluate based on:
1. Educational value — does it teach something useful?
2. Engagement — is the hook compelling? Is the pacing good?
3. Accuracy — are there any technical inaccuracies?
4. Clarity — is the narration clear and concise?
5. Visual direction — are the visual descriptions actionable?
Script to review:
${JSON.stringify(script, null, 2)}
Respond with ONLY the JSON object.`,
},
],
}),
});
if (!res.ok) {
console.warn(`[check-research] Claude critic failed: ${res.status}`);
return { score: script.qualityScore, issues: [], summary: 'Critic API error' };
}
const data = (await res.json()) as { content?: Array<{ text?: string }> };
const text = data.content?.[0]?.text ?? '{}';
const jsonMatch = text.match(/\{[\s\S]*\}/);
if (!jsonMatch) {
return { score: script.qualityScore, issues: [], summary: 'Could not parse critic response' };
}
const parsed = JSON.parse(jsonMatch[0]) as CriticResult;
return {
score: typeof parsed.score === 'number' ? parsed.score : script.qualityScore,
issues: Array.isArray(parsed.issues) ? parsed.issues : [],
summary: typeof parsed.summary === 'string' ? parsed.summary : 'No summary',
};
} catch (err) {
console.warn('[check-research] Claude critic error:', err);
return { score: script.qualityScore, issues: [], summary: 'Critic error' };
}
}
// ---------------------------------------------------------------------------
// Research payload builder
// ---------------------------------------------------------------------------
function extractTalkingPoints(text: string): string[] {
const lines = text.split('\n');
const points: string[] = [];
for (const line of lines) {
const cleaned = line.replace(/^[\s]*[-•*\d]+[.)]\s*/, '').trim();
if (cleaned.length > 20) {
points.push(cleaned);
}
}
return points.slice(0, 8);
}
function extractCodeExamples(text: string): Array<{ snippet: string; language: string; context: string }> {
const examples: Array<{ snippet: string; language: string; context: string }> = [];
const codeBlockRegex = /```(\w*)\n([\s\S]*?)```/g;
let match: RegExpExecArray | null;
while ((match = codeBlockRegex.exec(text)) !== null) {
const language = match[1] || 'typescript';
const snippet = match[2].trim();
const beforeBlock = text.slice(0, match.index);
const contextLines = beforeBlock.split('\n').filter((l) => l.trim());
const context =
contextLines.length > 0
? contextLines[contextLines.length - 1].trim()
: 'Code example';
examples.push({ snippet, language, context });
}
return examples;
}
function classifyScene(
content: string,
): 'narration' | 'code' | 'list' | 'comparison' | 'mockup' {
if (
/```[\s\S]*?```/.test(content) ||
/^\s{2,}(const|let|var|function|import|export|class|def|return)\b/m.test(content)
) {
return 'code';
}
const listMatches = content.match(/^[\s]*[-•*\d]+[.)]\s/gm);
if (listMatches && listMatches.length >= 3) {
return 'list';
}
if (
/\bvs\.?\b/i.test(content) ||
/\bcompare[ds]?\b/i.test(content) ||
/\bdifference[s]?\b/i.test(content) ||
/\bpros\s+(and|&)\s+cons\b/i.test(content)
) {
return 'comparison';
}
if (
/\b(UI|interface|dashboard|screen|layout|component|widget|button|modal)\b/i.test(content)
) {
return 'mockup';
}
return 'narration';
}
function classifySourceType(url: string): 'youtube' | 'article' | 'docs' | 'unknown' {
if (!url) return 'unknown';
const lower = url.toLowerCase();
if (lower.includes('youtube.com') || lower.includes('youtu.be')) return 'youtube';
if (lower.includes('/docs') || lower.includes('documentation') || lower.includes('developer.') || lower.includes('mdn')) return 'docs';
if (lower.includes('blog') || lower.includes('medium.com') || lower.includes('dev.to') || lower.includes('hashnode')) return 'article';
return 'unknown';
}
function buildResearchPayload(
doc: PipelineDoc,
researchData: Record<string, unknown>,
): ResearchPayload {
// If researchData already has the full ResearchPayload shape, use it directly
if (researchData.topic && researchData.talkingPoints && researchData.sceneHints) {
return researchData as unknown as ResearchPayload;
}
// Legacy format: extract from briefing + sources
const briefing = (researchData.briefing as string) ?? '';
const sources = (researchData.sources as Array<{ url: string; title: string }>) ?? [];
const infographicUrls = (researchData.infographicUrls as string[]) ?? [];
const talkingPoints = extractTalkingPoints(briefing);
const codeExamples = extractCodeExamples(briefing);
const sections = briefing
.split(/\n(?=#{1,3}\s)|\n\n/)
.filter((s) => s.trim().length > 50);
const sceneHints = sections.map((section) => ({
content: section.slice(0, 500),
suggestedSceneType: classifyScene(section),
reason: 'Classified from research content',
}));
return {
topic: doc.title,
notebookId: doc.researchNotebookId || '',
createdAt: doc._updatedAt,
completedAt: new Date().toISOString(),
sources: sources.map((s) => ({
title: s.title,
url: s.url,
type: classifySourceType(s.url),
})),
briefing,
talkingPoints,
codeExamples,
sceneHints,
infographicUrls: infographicUrls.length > 0 ? infographicUrls : undefined,
};
}
// ---------------------------------------------------------------------------
// Route Handler
// ---------------------------------------------------------------------------
export async function GET(request: NextRequest) {
// Auth: fail-closed — if CRON_SECRET is not set, reject
const cronSecret = process.env.CRON_SECRET;
if (!cronSecret) {
console.error('[check-research] CRON_SECRET not configured');
return Response.json({ error: 'Server misconfigured' }, { status: 503 });
}
const authHeader = request.headers.get('authorization');
if (authHeader !== `Bearer ${cronSecret}`) {
console.error('[check-research] Unauthorized request');
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
try {
const sanity = getSanityWriteClient();
// Single query for all active pipeline statuses
// Include both researchInteractionId (new Gemini) and researchNotebookId (legacy)
const docs = await sanity.fetch<PipelineDoc[]>(
`*[_type == "automatedVideo" && status in ["researching", "research_complete", "infographics_generating", "enriching"] && (defined(researchInteractionId) || defined(researchNotebookId))] {
_id, title, status, researchInteractionId, researchNotebookId, trendScore, trendSources,
script, researchData, _updatedAt
}`,
);
console.log(`[check-research] Found ${docs.length} docs in pipeline`);
if (docs.length === 0) {
return Response.json({ success: true, message: 'No docs to process', results: [] });
}
const results: StepResult[] = [];
// Phase 1: Stuck detection — runs FIRST, no external API calls
const stuckThresholds = await buildStuckThresholds();
const stuckResults = await flagStuckDocs(docs, sanity, stuckThresholds);
results.push(...stuckResults);
// Remove flagged docs from further processing
const stuckIds = new Set(stuckResults.map((r) => r.id));
const activeDocs = docs.filter((d) => !stuckIds.has(d._id));
// Group by status
const researching = activeDocs.filter((d) => d.status === 'researching');
const researchComplete = activeDocs.filter((d) => d.status === 'research_complete');
const infographicsGenerating = activeDocs.filter((d) => d.status === 'infographics_generating');
const enriching = activeDocs.filter((d) => d.status === 'enriching');
console.log(
`[check-research] Pipeline: ${researching.length} researching, ${researchComplete.length} research_complete, ${infographicsGenerating.length} infographics_generating, ${enriching.length} enriching`,
);
// Check enableDeepResearch toggle
const enableDeepResearch = await getConfigValue('pipeline_config', 'enableDeepResearch', false);
// Step 1: researching → research_complete
if (!enableDeepResearch) {
// Deep research disabled — skip researching docs to enriching
for (const doc of researching.slice(0, MAX_DOCS_PER_STATUS)) {
try {
await sanity.patch(doc._id).set({ status: doc.script ? 'script_ready' : 'enriching' }).commit();
results.push({ id: doc._id, title: doc.title, step: 'researching', outcome: 'deep_research_disabled_skip' });
} catch (err) {
console.error(`[check-research] Error skipping researching doc ${doc._id}:`, err);
results.push({
id: doc._id,
title: doc.title,
step: 'researching',
outcome: 'error',
error: err instanceof Error ? err.message : String(err),
});
}
}
} else {
for (const doc of researching.slice(0, MAX_DOCS_PER_STATUS)) {
try {
const result = await stepResearching(doc, sanity);
results.push(result);
} catch (err) {
console.error(`[check-research] Error in stepResearching for ${doc._id}:`, err);
results.push({
id: doc._id,
title: doc.title,
step: 'researching',
outcome: 'error',
error: err instanceof Error ? err.message : String(err),
});
}
}
}
// Step 2: research_complete → enriching (infographics generated inline)
for (const doc of researchComplete.slice(0, MAX_DOCS_PER_STATUS)) {
try {
const result = await stepResearchComplete(doc, sanity);
results.push(result);
} catch (err) {
console.error(`[check-research] Error in stepResearchComplete for ${doc._id}:`, err);
results.push({
id: doc._id,
title: doc.title,
step: 'research_complete',
outcome: 'error',
error: err instanceof Error ? err.message : String(err),
});
}
}
// Step 3: infographics_generating → enriching (legacy migration only)
for (const doc of infographicsGenerating.slice(0, MAX_DOCS_PER_STATUS)) {
try {
const result = await stepInfographicsGenerating(doc, sanity);
results.push(result);
} catch (err) {
console.error(`[check-research] Error in stepInfographicsGenerating for ${doc._id}:`, err);
results.push({
id: doc._id,
title: doc.title,
step: 'infographics_generating',
outcome: 'error',
error: err instanceof Error ? err.message : String(err),
});
}
}
// Step 4: enriching → script_ready
for (const doc of enriching.slice(0, MAX_DOCS_PER_STATUS)) {
try {
const result = await stepEnriching(doc, sanity);
results.push(result);
} catch (err) {
console.error(`[check-research] Error in stepEnriching for ${doc._id}:`, err);
results.push({
id: doc._id,
title: doc.title,
step: 'enriching',
outcome: 'error',
error: err instanceof Error ? err.message : String(err),
});
}
}
console.log(`[check-research] Run complete: ${results.length} results`);
return Response.json({ success: true, results });
} catch (err) {
console.error('[check-research] Unexpected error:', err);
return Response.json(
{
success: false,
error: err instanceof Error ? err.message : String(err),
},
{ status: 500 },
);
}
}