import type { ScenePacket, SceneRole } from '@olokaflow/shared'; import { getSceneBlueprints, getMotionRules } from '../catalog/hyperframesKnowledgeBase.js'; export interface EditIntentDecision { intentType: 'surgical_visual' | 'narration_edit' | 'style_preset_change' | 'aspect_remix' | 'regenerate_image'; targetSceneIndices: number[]; confidence: number; summary: string; } /** * Jev (TypeSafe System One) Intent Router & Catalog Ranker */ export function routeEditIntentWithJev( prompt: string, scenes: ScenePacket[] ): EditIntentDecision { const lower = (prompt || '').toLowerCase(); // Detect explicit scene numbers ("cảnh 1", "scene 2", "cảnh cuối", "mở đầu") const targetedIndices: number[] = []; const numMatches = lower.matchAll(/(?:cảnh|canh|scene|phân cảnh)\s*#?(\d+)/g); for (const m of numMatches) { const idx = parseInt(m[1], 10) - 1; if (idx >= 0 && idx < scenes.length) targetedIndices.push(idx); } if (/cảnh cuối|outro|cta|kết thúc/i.test(lower) && scenes.length > 0) { targetedIndices.push(scenes.length - 1); } if (/cảnh đầu|mở đầu|hook/i.test(lower) && scenes.length > 0) { targetedIndices.push(0); } // If no explicit number, match by keyword overlap with scene titles/narrations if (targetedIndices.length === 0 && scenes.length > 0) { const scored = scenes .map((s) => { const words = lower.split(/\s+/).filter((w) => w.length > 3); const hay = `${s.title} ${s.narrationText} ${s.role}`.toLowerCase(); const hits = words.filter((w) => hay.includes(w)).length; return { index: s.index, hits }; }) .sort((a, b) => b.hits - a.hits); if (scored[0] && scored[0].hits > 0) { targetedIndices.push(scored[0].index); } else { targetedIndices.push(0); } } const uniqueTargets = Array.from(new Set(targetedIndices)); if (/tỷ lệ|khung ngang|khung dọc|16:9|9:16|1:1|youtube|tiktok/i.test(lower)) { return { intentType: 'aspect_remix', targetSceneIndices: scenes.map((s) => s.index), confidence: 0.94, summary: 'Chuyển đổi tỷ lệ khung hình toàn dự án (Aspect Ratio Remix)', }; } if (/lời thoại|giọng đọc|câu thoại|đọc là|sửa chữ đọc|narration/i.test(lower)) { return { intentType: 'narration_edit', targetSceneIndices: uniqueTargets, confidence: 0.91, summary: `Cập nhật lời thoại & đồng bộ lại TTS cho Cảnh ${uniqueTargets.map((i) => i + 1).join(', ')}`, }; } if (/tạo lại ảnh|đổi ảnh ai|vẽ lại ảnh|sinh lại ảnh/i.test(lower)) { return { intentType: 'regenerate_image', targetSceneIndices: uniqueTargets, confidence: 0.92, summary: `Tạo lại hình ảnh tham khảo AI cho Cảnh ${uniqueTargets.map((i) => i + 1).join(', ')}`, }; } if (/đổi tông màu|đổi preset|đổi theme|phong cách toàn video/i.test(lower)) { return { intentType: 'style_preset_change', targetSceneIndices: scenes.map((s) => s.index), confidence: 0.89, summary: 'Đổi Frame Design Preset cho toàn bộ video', }; } return { intentType: 'surgical_visual', targetSceneIndices: uniqueTargets, confidence: 0.88, summary: `Chỉnh sửa cục bộ bố cục & hiệu ứng GSAP cho Cảnh ${uniqueTargets.map((i) => i + 1).join(', ')}`, }; } /** * Selects the best HyperFrames Scene Blueprint and 2-3 Atomic Motion Rules for a given role and visual direction. */ export function selectOptimalBlueprintAndRules( role: SceneRole, visualDirection: string ): { blueprintId: string; motionRuleIds: string[]; registryBlockName: string } { const lower = (visualDirection || '').toLowerCase(); const blueprints = getSceneBlueprints(); const rules = getMotionRules(); const roleDefaultMap: Record< SceneRole, { blueprintId: string; motionRuleIds: string[]; registryBlockName: string } > = { Hook: { blueprintId: 'kinetic-type-beats', motionRuleIds: ['kinetic-beat-slam', 'spring-pop-entrance', 'css-marker-patterns'], registryBlockName: 'quote-card', }, Problem: { blueprintId: 'overwhelm-surround', motionRuleIds: ['chromatic-glitch', 'waterfall-entry'], registryBlockName: '', }, Product_Intro: { blueprintId: 'device-surface-showcase', motionRuleIds: ['3d-camera-flight', 'spring-pop-entrance', 'coordinate-target-zoom'], registryBlockName: 'app-showcase', }, Key_Feature: { blueprintId: 'grid-card-assemble', motionRuleIds: ['waterfall-entry', 'spring-pop-entrance', 'cursor-click-ripple'], registryBlockName: 'flowchart', }, Benefits: { blueprintId: 'comparison-split', motionRuleIds: ['waterfall-entry', 'css-marker-patterns'], registryBlockName: 'app-showcase', }, Data_Proof: { blueprintId: 'dataviz-countup', motionRuleIds: ['counting-dynamic-scale', 'waterfall-entry'], registryBlockName: 'data-chart', }, Social_Proof: { blueprintId: 'video-text-pivot', motionRuleIds: ['spring-pop-entrance', 'coordinate-target-zoom'], registryBlockName: 'quote-card', }, CTA: { blueprintId: 'logo-assemble-lockup', motionRuleIds: ['spring-pop-entrance', 'particle-burst'], registryBlockName: 'logo-outro', }, }; const selected = { ...(roleDefaultMap[role] || roleDefaultMap.Hook) }; if (/số liệu|%|tăng trưởng|biểu đồ|doanh thu|gấp \d/i.test(lower)) { selected.blueprintId = 'dataviz-countup'; selected.motionRuleIds = ['counting-dynamic-scale', 'waterfall-entry']; selected.registryBlockName = 'data-chart'; } else if (/so sánh|trước và sau|cũ và mới/i.test(lower)) { selected.blueprintId = 'comparison-split'; } else if (/con trỏ|click|bấm nút|giao diện web/i.test(lower)) { selected.blueprintId = 'cursor-ui-demo'; selected.motionRuleIds = ['cursor-click-ripple', 'coordinate-target-zoom']; } // Ensure blueprintId & ruleIds exist in catalog if (!blueprints.some((b) => b.id === selected.blueprintId)) { selected.blueprintId = blueprints[0].id; } selected.motionRuleIds = selected.motionRuleIds.filter((id) => rules.some((r) => r.id === id) ); return selected; }