hit-cxf
2026-04-09 940e6ea80926b243ee34cebe2df498c558ebd73e
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
/**
 * 阶段数据工具函数
 * - 路径转 URL
 * - 剧本/分镜结构化文本解析与重建
 */
 
/** 将后端本地文件路径转换为浏览器可访问的 URL */
export function assetUrl(path: string): string {
  if (!path) return '';
  if (path.startsWith('http') || path.startsWith('/') || path.startsWith('blob:') || path.startsWith('data:')) return path;
  return '/' + path;
}
 
/* ─── 剧本 / 分镜 结构化文本解析 ─── */
 
export interface ParsedCharacter {
  name: string;
  description: string;
}
 
export interface ParsedSetting {
  name: string;
  description: string;
}
 
export interface ParsedScene {
  id: string;
  characters: string[];
  settings: string[];
  description: string;
  raw: string;
}
 
export interface ParsedScript {
  characters: ParsedCharacter[];
  settings: ParsedSetting[];
  scenes: ParsedScene[];
  isZh: boolean;
}
 
/** 解析结构化剧本/分镜文本 → 角色 + 场景 + 故事线 */
export function parseScriptText(text: string): ParsedScript {
  if (!text) return { characters: [], settings: [], scenes: [], isZh: false };
 
  const normalized = text.replace(/\\n/g, '\n');
  const isZh = /角色[::]|场景设置[::]|视频片段/.test(normalized);
 
  const charHeader = isZh ? /角色[::]/ : /Characters[::]/;
  const settingHeader = isZh ? /场景设置[::]/ : /Settings[::]/;
  const sceneHeader = isZh ? /视频片段[::]/ : /Scenes[::]/;
 
  function findMatch(src: string, pat: RegExp) {
    const m = src.match(pat);
    return m ? { index: src.indexOf(m[0]), length: m[0].length } : null;
  }
 
  function extractBlock(src: string, startPat: RegExp, endPat: RegExp | null): string {
    const sm = findMatch(src, startPat);
    if (!sm) return '';
    const sp = sm.index + sm.length;
    if (endPat) {
      const em = findMatch(src.slice(sp), endPat);
      return em ? src.slice(sp, sp + em.index) : src.slice(sp);
    }
    return src.slice(sp);
  }
 
  const charBlock = extractBlock(normalized, charHeader, settingHeader).trim();
  const settingBlock = extractBlock(normalized, settingHeader, sceneHeader).trim();
  const sceneBlock = extractBlock(normalized, sceneHeader, null).trim();
 
  const characters: ParsedCharacter[] = [];
  for (const line of charBlock.split('\n')) {
    const t = line.trim();
    if (!t) continue;
    const ci = t.search(/[::]/);
    if (ci > 0) {
      characters.push({
        name: t.slice(0, ci).trim(),
        description: t.slice(ci + 1).trim().replace(/\.\s*$/, ''),
      });
    }
  }
 
  const settings: ParsedSetting[] = [];
  for (const line of settingBlock.split('\n')) {
    const t = line.trim();
    if (!t) continue;
    const ci = t.search(/[::]/);
    if (ci > 0) {
      settings.push({
        name: t.slice(0, ci).trim(),
        description: t.slice(ci + 1).trim().replace(/\.\s*$/, ''),
      });
    }
  }
 
  const scenes: ParsedScene[] = [];
  const scenePat = isZh
    ? /^视频片段\s*(\d+)\s*[::]\s*(.*)/
    : /^Scene\s+(\d+)\s*[::]\s*(.*)/i;
 
  for (const line of sceneBlock.split('\n')) {
    const m = line.trim().match(scenePat);
    if (m) {
      const raw = m[2];
      const cm = raw.match(/\[(?:Characters|角色)\s*[::]\s*([^\]]+)\]/i);
      const sm2 = raw.match(/\[(?:Settings|场景(?:设置)?)\s*[::]\s*([^\]]+)\]/i);
      const chars = cm ? cm[1].split(/[,,]/).map(s => s.trim()).filter(Boolean) : [];
      const sets = sm2 ? sm2[1].split(/[,,]/).map(s => s.trim()).filter(Boolean) : [];
      const desc = raw.replace(/\[.*?\]/g, '').trim();
      scenes.push({ id: m[1], characters: chars, settings: sets, description: desc, raw });
    }
  }
 
  return { characters, settings, scenes, isZh };
}
 
/** 将解析后的结构重建为标准文本格式 */
export function reconstructScriptText(parsed: ParsedScript): string {
  const { isZh, characters, settings, scenes } = parsed;
  const charH = isZh ? '角色:' : 'Characters:';
  const settH = isZh ? '场景设置:' : 'Settings:';
  const sceneH = isZh ? '视频片段:' : 'Scenes:';
  const scenePrefix = isZh ? '视频片段' : 'Scene';
  const charLabel = isZh ? '角色' : 'Characters';
  const settLabel = isZh ? '场景' : 'Settings';
 
  const lines: string[] = [];
  lines.push(charH);
  for (const c of characters) {
    lines.push(`${c.name}: ${c.description}`);
  }
  lines.push(settH);
  for (const s of settings) {
    lines.push(`${s.name}: ${s.description}`);
  }
  lines.push(sceneH);
  for (const sc of scenes) {
    const cp = sc.characters.length ? `[${charLabel}: ${sc.characters.join(', ')}]` : '';
    const sp = sc.settings.length ? `[${settLabel}: ${sc.settings.join(', ')}]` : '';
    const parts = [`${scenePrefix} ${sc.id}:`, cp, sp, sc.description].filter(Boolean);
    lines.push(parts.join(' '));
  }
  return lines.join('\n');
}