Spaces:
Sleeping

File size: 6,397 Bytes
9713441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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;
}