@ninkch/dsh-infinite-canvas 0.20.1 → 0.20.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ninkch/dsh-infinite-canvas",
3
- "version": "0.20.1",
3
+ "version": "0.20.3",
4
4
  "description": "无限画布(Infinite Canvas)for DeepSeek Harness:节点式创作工作室(剧本/分镜/生成/成片),内置「媒体创作」预设模式(导演第一视角 + 引导工作坊)。Version-agnostic: reads the live catalog from the installed package at load time.",
5
5
  "keywords": [
6
6
  "dsh",
@@ -40,6 +40,7 @@
40
40
  "index.js",
41
41
  "client.js",
42
42
  "production.js",
43
+ "shot-segments.js",
43
44
  "icon.svg",
44
45
  "cordis.patch.yml",
45
46
  "locale/*.json",
package/production.js CHANGED
@@ -20,6 +20,8 @@
20
20
 
21
21
  import { existsSync, readFileSync, mkdirSync, copyFileSync, writeFileSync, renameSync } from 'node:fs';
22
22
  import path from 'node:path';
23
+ // 按镜头切段(client.js planShotSegments 的逐字移植):预算单的 videoCalls 口径与客户端执行同源
24
+ import { planShotSegments } from './shot-segments.js';
23
25
 
24
26
  /** 阶段枚举(顺序即优先级,index 越大越靠后)。 */
25
27
  export const PRODUCTION_STAGES = ['brief', 'bible', 'assets', 'script', 'storyboard', 'shots_ready', 'generating', 'qc', 'post', 'delivered'];
@@ -190,8 +192,53 @@ export function createProduction(deps) {
190
192
  return 'q' + h.toString(36) + '-' + s.length.toString(36);
191
193
  }
192
194
 
195
+ // ── T8(0.20.2 #6/#7):参考图实体收集与「已有参考资产」判定 ──────────────────────────
196
+
197
+ /** 从 shots 收集 角色/场景/道具 实体(trim、去重、保序)。
198
+ * 预算单行项与付费段执行(index.js autoPaid)共用这一份口径——两边各写一份必然漂移,
199
+ * 「预算 N 张 / 实际 M 张」的对照关系靠同构保证。 */
200
+ function collectShotEntities(shots) {
201
+ const out = [];
202
+ const seen = new Set();
203
+ const add = (kind, name) => {
204
+ const nm = String(name || '').trim();
205
+ if (!nm) return;
206
+ const key = kind + '|' + nm;
207
+ if (seen.has(key)) return;
208
+ seen.add(key);
209
+ out.push({ kind, name: nm, key });
210
+ };
211
+ for (const sh of (Array.isArray(shots) ? shots : [])) {
212
+ const chars = Array.isArray(sh && sh.characters) ? sh.characters : [];
213
+ const props = Array.isArray(sh && sh.props) ? sh.props : [];
214
+ for (const nm of chars) add('角色', nm);
215
+ add('场景', sh && sh.scene);
216
+ for (const nm of props) add('道具', nm);
217
+ }
218
+ return out;
219
+ }
220
+
221
+ /** 实体是否已有参考资产:资产名 = 「<品类>-<名>…」且名称后必须是边界(client.js
222
+ * assetBelongsToEntity 同款语义——rest 为空或首字符不是字母/数字,防「张山/张山峰」混淆:
223
+ * 「角色-陈拾 多视图」「角色-陈拾 设定图」算,「角色-陈拾设定图」「角色-陈拾峰」不算)。 */
224
+ function hasRefAssetName(assetNames, kindLabel, name) {
225
+ const pref = kindLabel + '-' + String(name || '').trim();
226
+ return (Array.isArray(assetNames) ? assetNames : []).some((raw) => {
227
+ const nm = String(raw || '').trim();
228
+ if (!nm.startsWith(pref)) return false;
229
+ const rest = nm.slice(pref.length);
230
+ return rest === '' || !/[\p{L}\p{N}]/u.test(rest[0]);
231
+ });
232
+ }
233
+
193
234
  /**
194
- * 预算单(免费段终点):只统计"将要花钱"的东西——每个分镜表一次视频调用、总秒数、场/镜数。
235
+ * 预算单(免费段终点):只统计"将要花钱"的东西——每个分镜表按真实镜头边界切成生成段,
236
+ * 每段一次视频调用(一个分镜表 ≠ 一次生成);总秒数、场/镜数照旧。
237
+ * 0.20.2 T8(#6/#7):每个待生成分镜表加参考图行项 refImages(该场缺失参考资产的
238
+ * 角色/场景/道具 实体数),顶层 refImagesTotal;跨场共享实体只计一次(与执行侧
239
+ * run 级 Map 同序先到先得,归属第一场)。已有参考资产(readAssetIndexNames 边界匹配)的不计。
240
+ * 切段口径与客户端执行完全同源:同一个 planShotSegments(shot-segments.js,client.js 的逐字移植),
241
+ * maxSeconds 同样取分镜表节点 data.segMaxSeconds(缺省 15,client.js L13883 同式)。
195
242
  * 单价由调用方传入(画布设置里的单价,未配置则不给金额,只给调用次数与秒数)。
196
243
  */
197
244
  function quote(pid, cid, unitPricePerSecond) {
@@ -200,33 +247,62 @@ export function createProduction(deps) {
200
247
  const nodes = Object.values(doc.nodes || {});
201
248
  const sbs = nodes.filter((n) => n.type === 'storyboard' && Array.isArray(n.data && n.data.shots) && n.data.shots.length);
202
249
  const videoNodes = nodes.filter((n) => n.type === 'video');
250
+ // 项目资产名(SQL/JSON 双模由宿主 readAssetIndexNames 决定);读失败视为无资产 → 全部计缺(诚实从宽)
251
+ let assetNames = [];
252
+ try { assetNames = (deps.readAssetIndexNames ? (deps.readAssetIndexNames(pid) || []) : []).map((n) => String(n || '').trim()).filter(Boolean); } catch { assetNames = []; }
253
+ const claimed = new Set();
254
+ let refImagesTotal = 0;
203
255
  let totalSeconds = 0;
204
256
  const scenes = sbs.map((n, i) => {
205
257
  const shots = n.data.shots || [];
206
258
  const secs = shots.reduce((a, s) => a + (Number(s && s.duration) || 0), 0);
207
259
  totalSeconds += secs;
260
+ const hasVideo = videoNodes.some((v) => (doc.edges || []).some((e) => e.fromNode === n.id && e.toNode === v.id));
261
+ const maxSeconds = Number(n.data.segMaxSeconds) || 15;
262
+ // 已有视频的表不产生新调用;待生成的表按切段算法数段(与客户端 expandStoryboard 同式同默认)
263
+ let calls = 0;
264
+ let refImages = 0;
265
+ if (!hasVideo) {
266
+ const plan = planShotSegments(shots, { maxSeconds });
267
+ // 客户端兜底同款(L13884):切段结果为空但表里有镜头时,客户端会整表按一段生成——估算按 1 次算
268
+ calls = plan.segments.length ? plan.segments.length : 1;
269
+ // T8 参考图行项:该场缺失参考资产的实体数(跨场共享只计一次)
270
+ for (const ent of collectShotEntities(shots)) {
271
+ if (claimed.has(ent.key)) continue;
272
+ claimed.add(ent.key);
273
+ if (!hasRefAssetName(assetNames, ent.kind, ent.name)) refImages++;
274
+ }
275
+ }
276
+ refImagesTotal += refImages;
208
277
  return {
209
278
  scene: i + 1,
210
279
  storyboardId: n.id,
211
280
  name: String((n.data && n.data.label) || n.id),
212
281
  shots: shots.length,
213
282
  seconds: Math.round(secs * 10) / 10,
214
- hasVideo: videoNodes.some((v) => (doc.edges || []).some((e) => e.fromNode === n.id && e.toNode === v.id)),
283
+ hasVideo,
284
+ maxSeconds,
285
+ videoCalls: calls,
286
+ refImages,
215
287
  };
216
288
  });
217
- const pending = scenes.filter((s) => !s.hasVideo).length;
289
+ const videoCalls = scenes.reduce((a, s) => a + s.videoCalls, 0);
218
290
  const price = Number(unitPricePerSecond);
219
- const payload = { pid, cid: keyOf(cid), scenes: scenes.map((s) => [s.storyboardId, s.shots, s.seconds]), pending };
291
+ // 指纹吃切段结果与每场上限:段数/上限变了 → 旧批准自动失效 → 用户重新复核预算(预期行为,无兼容垫片)
292
+ // T8:参考图行项一并进指纹——缺失实体集合变了(设定集/资产中途变化)同样要求重新复核
293
+ const payload = { pid, cid: keyOf(cid), scenes: scenes.map((s) => [s.storyboardId, s.shots, s.seconds, s.videoCalls, s.maxSeconds, s.refImages]), pending: videoCalls, refImages: refImagesTotal };
294
+ const baseNote = videoCalls === 0 ? '全部分镜表都已有视频节点:本次只做重生成,不产生新调用' : '批准后按镜头切段逐段调用视频接口(按秒计费)';
220
295
  return {
221
296
  ok: true, quoteId: fingerprint(payload),
222
297
  projectId: pid, canvasId: keyOf(cid) === 'default' ? null : keyOf(cid),
223
298
  stage: st.stage,
224
299
  scenes, sceneCount: scenes.length, shotCount: scenes.reduce((a, s) => a + s.shots, 0),
225
300
  totalSeconds: Math.round(totalSeconds * 10) / 10,
226
- videoCalls: pending,
301
+ videoCalls,
302
+ refImagesTotal,
227
303
  unitPricePerSecond: Number.isFinite(price) && price > 0 ? price : null,
228
304
  estimatedCost: Number.isFinite(price) && price > 0 ? Math.round(price * totalSeconds * 100) / 100 : null,
229
- note: pending === 0 ? '全部分镜表都已有视频节点:本次只做重生成,不产生新调用' : '批准后只按差分镜表各调用一次视频接口(按秒计费)',
305
+ note: refImagesTotal > 0 ? baseNote + ';含参考图 ' + refImagesTotal + ' 张(未登记/未生成参考资产的实体)' : baseNote,
230
306
  };
231
307
  }
232
308
 
@@ -422,6 +498,7 @@ export function createProduction(deps) {
422
498
  readState, writeState, inferStage, stageOf, noteStage, advance, recordOverride, status,
423
499
  gateBible, gateStoryboard, gateStage, gateShotNodeName, gateOneVideoPerStoryboard,
424
500
  quote, approve, approvalOf,
501
+ collectShotEntities, hasRefAssetName,
425
502
  stageIndex: (name) => STAGE_INDEX[name], stageName: (idx) => PRODUCTION_STAGES[idx] || null,
426
503
  };
427
504
  }
@@ -0,0 +1,233 @@
1
+ /**
2
+ * @ninkch/dsh-infinite-canvas — 按镜头切段(宿主侧权威估算器,0.20.2)
3
+ *
4
+ * 本模块是 client.js 里 planShotSegments 的**逐字移植**(零 npm 依赖的纯函数 ES module):
5
+ * - shotVisualText ← client.js L4189-4192
6
+ * - shotContinuityPlan ← client.js L4201-4249
7
+ * - isCutPoint ← client.js L4368-4370
8
+ * - planShotSegments ← client.js L4389-4514
9
+ *
10
+ * 为什么必须逐字:真实视频调用次数由客户端切段决定(client.js expandStoryboard /
11
+ * StoryboardNodeBody 都以 `planShotSegments(shots, { maxSeconds: Number(n.data.segMaxSeconds) || 15 })`
12
+ * 的结果为准——一个分镜表 ≠ 一次生成)。宿主预算单(production.js quote)按这里的输出
13
+ * 估算 videoCalls;任何语义简化都会让预算再次失真(#8:显示 4 次、实际 14 次)。
14
+ *
15
+ * 同步契约:改动 client.js 的切段算法时必须同步本文件,反之亦然。
16
+ * 漂移守卫:scripts/test-quote-segments.mjs 用 vm 加载真实 client.js,把
17
+ * window.__DSH_IC_SEGMENT__.planShotSegments 与本模块在同一组 fixtures 上断言输出一致——
18
+ * 宿主估算与客户端执行漂移 = 预算单再次失真,测试红了就必须人工对齐两端。
19
+ */
20
+
21
+ /** 镜头画面文本(visual + motion),用于衔接描述与相似度兜底。 */
22
+ function shotVisualText(sh) {
23
+ if (!sh) return '';
24
+ return [String(sh.visual || '').trim(), String(sh.motion || '').trim()].filter(Boolean).join(';');
25
+ }
26
+ /**
27
+ * 镜头衔接规划:把「尾部状态 / 起始状态 / 运镜延续 / 段落类型 / 接缝档位」统一解出来。
28
+ * 分镜字段可手写(tail/start/cam),缺失时用 visual 兜底——**绝不编造剧情**,只做"能确认的推断"。
29
+ * seg: 'opening'(首段,交代)| 'continuation'(续帧,默认)| 'new_character'(新角色入场,参考图模式)
30
+ * | 'scene_change'(换场,不接尾帧)| 'closeup'(特写/细节插入,接缝不被画面盯着看 → 标准放宽)
31
+ * join: 'strict'(同步长硬接)| 'loose'(跨场/换景,允许软接)| 'cut'(特写插入,跳切合理,不接力)
32
+ * @returns {{seg,name,join,fromTail,fromShot,tail,start,cam,chain,note}}
33
+ */
34
+ function shotContinuityPlan(sh, prev, idx) {
35
+ const s = sh || {};
36
+ const i = Number.isFinite(Number(idx)) ? Number(idx) : 0;
37
+ const prevShot = i > 0 ? (prev || null) : null;
38
+ const segRaw = String(s.seg || '').trim().toLowerCase();
39
+ const SEG_ALIAS = {
40
+ opening: 'opening', open: 'opening', '首段': 'opening', '开场': 'opening',
41
+ continuation: 'continuation', cont: 'continuation', '续': 'continuation', '续帧': 'continuation', '承接': 'continuation',
42
+ new_character: 'new_character', newchar: 'new_character', '新角色': 'new_character', '入场': 'new_character',
43
+ scene_change: 'scene_change', scene: 'scene_change', '换场': 'scene_change', '转场': 'scene_change',
44
+ closeup: 'closeup', insert: 'closeup', '特写': 'closeup', '插入': 'closeup',
45
+ // 同机位状态跳变:切前不在/切后在(灯闪现身、影子凭空在场、影化实)——奇幻特效叙事的合法段落
46
+ state_jump: 'state_jump', jump: 'state_jump', '跳变': 'state_jump', '状态跳变': 'state_jump',
47
+ };
48
+ let seg = SEG_ALIAS[segRaw] || '';
49
+ const boxed = (k) => String(s[k] || '').trim();
50
+ if (!seg) {
51
+ if (!prevShot) seg = 'opening';
52
+ else if (/换场|场景切换|转场|另一处|另一场景|切至|切到|次日|翌日|与此同时|数日后|[Cc]ut to/.test(shotVisualText(s))) seg = 'scene_change';
53
+ else if (/新角色|首次出场|走进来|入镜|入场|冒出|推门进|[Ee]nters?/.test(shotVisualText(s))) seg = 'new_character';
54
+ else if (Number(s.duration) > 0 && Number(s.duration) < 1.5 && !(s.dialogue || []).length) seg = 'closeup';
55
+ else seg = 'continuation';
56
+ }
57
+ if (seg === 'opening' && prevShot) seg = 'continuation'; // 首段只能在第 1 镜;后面标"开场"视为续帧
58
+ // 接缝档位:换场/新角色入场都放宽(新角色段本就不承接上段画面),特写插入与状态跳变允许跳切
59
+ const join = (seg === 'scene_change' || seg === 'new_character') ? 'loose' : (seg === 'closeup' || seg === 'state_jump') ? 'cut' : 'strict';
60
+ // 只有真正需要画面承接的段落才接力尾帧;新角色入场首帧若来自上一段,新角色反而无从进入
61
+ const chain = seg === 'continuation' && !!prevShot;
62
+ // ownTail = 本镜的接点尾态(留给**下一镜**);cam = 本镜运镜(同样留给下一镜延续)
63
+ const tail = boxed('tail');
64
+ const cam = boxed('cam');
65
+ // prevTail = **上一镜**的接点尾态 = 本镜的起始态(衔接块要引用的是它,不是本镜自己的 tail)。
66
+ // 旧分镜没写 tail 时退到上一镜的「最后动作」——诚实标注来源,而不是拿本镜画面冒充。
67
+ let prevTail = '';
68
+ let prevTailSource = '';
69
+ if (prevShot) {
70
+ const pt = String(prevShot.tail || '').trim();
71
+ const pm = String(prevShot.motion || '').trim();
72
+ if (pt) { prevTail = pt; prevTailSource = 'tail'; }
73
+ else if (pm) { prevTail = pm; prevTailSource = 'motion'; }
74
+ }
75
+ const start = boxed('start');
76
+ return {
77
+ idx: i, seg, join, fromTail: i > 0, fromShot: i > 0, chain,
78
+ name: String(s.title || '').trim(),
79
+ tail, cam, start, prevTail, prevTailSource,
80
+ note: boxed('note'),
81
+ };
82
+ }
83
+ /** 该 seg 是否构成一个"可以安全开始新生成段"的切点。opening 只在全表第 1 镜成立。
84
+ * state_jump(同机位状态跳变)也是合法切点:跳变本身就是"切"——前后镜只差状态、不接动作。 */
85
+ function isCutPoint(seg) {
86
+ return seg === 'scene_change' || seg === 'closeup' || seg === 'new_character' || seg === 'opening' || seg === 'state_jump';
87
+ }
88
+ /**
89
+ * 把一张分镜表的镜头按「真实镜头边界」切成若干生成段。
90
+ *
91
+ * @param shots 分镜镜头数组(data.shots)
92
+ * @param opts { maxSeconds(单次生成上限,默认 15), minSeconds(单段下限,默认 2),
93
+ * hardMax(厂商物理上限,超了必须拆,默认同 maxSeconds) }
94
+ * @returns {{ segments: Array, total: number, max: number, cutMode: string, warnings: string[] }}
95
+ * segments[i] = {
96
+ * index, shotIds, shots, startIndex, endIndex,
97
+ * duration, // 本段总时长(秒,一位小数)
98
+ * firstSeg, lastSeg, // 本段首/末镜的段落类型
99
+ * opensOnCut, // 本段是否从一个合法切点开始(第 1 段恒真)
100
+ * closesOnCut, // 本段末镜是否本身就是"换镜头"(=下一段天然切点,无需接力)
101
+ * seamRisk, // 'none' | 'chain'(靠尾帧接力)| 'forced'(找不到切点被硬切)
102
+ * needsTailChain, // 是否需要把上一段尾帧接到本段首帧
103
+ * reason, // 人类可读的切段依据(面板上显示)
104
+ * }
105
+ */
106
+ function planShotSegments(shots, opts) {
107
+ const o = opts || {};
108
+ const list = (Array.isArray(shots) ? shots : []).filter((s) => s && (String(s.visual || '').trim() || String(s.motion || '').trim() || (Array.isArray(s.dialogue) && s.dialogue.length)));
109
+ const maxSeconds = Math.max(1, Number(o.maxSeconds) || 15);
110
+ const minSeconds = Math.max(0, Math.min(Number(o.minSeconds) || 2, maxSeconds));
111
+ const warnings = [];
112
+ const durOf = (s) => Math.max(0.3, Math.round((Number(s && s.duration) || 3) * 10) / 10);
113
+ // 预解每镜的段落类型(与 shotContinuityPlan 同一套推断,保证分段与提示词口径一致)
114
+ const plans = list.map((s, i) => shotContinuityPlan(s, list[i - 1] || null, i));
115
+ const total = Math.round(list.reduce((a, s) => a + durOf(s), 0) * 10) / 10;
116
+ if (!list.length) return { segments: [], total: 0, max: maxSeconds, cutMode: 'empty', warnings: ['分镜表里没有可用镜头'] };
117
+
118
+ /** 从 from 开始贪心找到一个"可以在 to 前断开"的位置:
119
+ * 返回该段的结束下标(含);-1 表示从 from 开始连第一镜都放不下。 */
120
+ const findBreak = (from) => {
121
+ let acc = 0;
122
+ let bestCut = -1; // 最近一次合法切点之后的位置(即"下一镜是新镜头"的下标)
123
+ for (let i = from; i < list.length; i++) {
124
+ const d = durOf(list[i]);
125
+ if (acc + d > maxSeconds + 1e-6) {
126
+ // 放不下了。若当前累计还没超过下限,也要至少吃一镜(单镜超长的情况由下面兜底)
127
+ break;
128
+ }
129
+ acc += d;
130
+ // 判断"i 之后可以断开":下一镜是切点,或本镜本身就是换镜头(切在它之后)
131
+ const nextIsCut = i + 1 < list.length && isCutPoint(plans[i + 1] && plans[i + 1].seg);
132
+ const thisIsCut = isCutPoint(plans[i] && plans[i].seg);
133
+ // 切点镜的正确归属:**下一段的首镜**。
134
+ // 旧写法 thisIsCut → bestCut = i,把换场/特写镜留在本段末尾,下一段只能从它后面那一镜(多半是
135
+ // continuation)开始 → 必然报"从镜中开始"的硬切。修好后:i>from 时断在 i-1,让切点镜去开新段。
136
+ if (nextIsCut) bestCut = i;
137
+ else if (thisIsCut) bestCut = i > from ? i - 1 : i;
138
+ if (i === list.length - 1) bestCut = i;
139
+ }
140
+ if (bestCut < from) {
141
+ // 连一镜都塞不进上限:退化为"只取这一镜",交给下面标记 forced
142
+ return { end: from, forced: true, acc: durOf(list[from]) };
143
+ }
144
+ return { end: bestCut, forced: false, acc };
145
+ };
146
+
147
+ const segments = [];
148
+ let i = 0;
149
+ let warnMark = warnings.length;
150
+ while (i < list.length && segments.length < 128) {
151
+ // 段首必须是切点;若不是(说明上一段是被 forced 断开的),本镜保持原 seg
152
+ warnMark = warnings.length;
153
+ const brk = findBreak(i);
154
+ let end = brk.end;
155
+ let seamRisk = 'none';
156
+ let reason = '';
157
+ const firstSeg = (plans[i] && plans[i].seg) || 'continuation';
158
+ const opensOnCut = segments.length === 0 || isCutPoint(firstSeg);
159
+ if (!opensOnCut) {
160
+ seamRisk = 'forced';
161
+ reason = '上一段超长被迫硬切,本段从镜中断点继续,动作相位可能不连续';
162
+ warnings.push(`第 ${i + 1} 镜起的一段是硬切(上一段超长且找不到换镜头切点):这一段与上一段的接缝只能靠尾帧+提示词硬接`);
163
+ }
164
+ // 段内若含"必须承接"的 continuation,且不是首段、且首镜也不是切点 → 需要尾帧接力
165
+ const needsTailChain = segments.length > 0 && !isCutPoint(firstSeg);
166
+ if (needsTailChain) {
167
+ seamRisk = seamRisk === 'forced' ? 'forced' : 'chain';
168
+ reason = reason || '本段首镜是续帧镜头,需用上一段尾帧接力';
169
+ }
170
+ const segShots = list.slice(i, end + 1);
171
+ const duration = Math.round(segShots.reduce((a, s) => a + durOf(s), 0) * 10) / 10;
172
+ const lastSeg = (plans[end] && plans[end].seg) || 'continuation';
173
+ const closesOnCut = isCutPoint(lastSeg) || end === list.length - 1;
174
+ if (!reason) {
175
+ reason = closesOnCut
176
+ ? '本段以换镜头收尾,下一段天然切换、无需接力'
177
+ : '本段在换镜头切点处开始';
178
+ }
179
+ segments.push({
180
+ index: segments.length,
181
+ shotIds: segShots.map((s, k) => i + k),
182
+ shots: segShots,
183
+ startIndex: i,
184
+ endIndex: end,
185
+ duration,
186
+ firstSeg,
187
+ lastSeg,
188
+ opensOnCut,
189
+ closesOnCut,
190
+ seamRisk,
191
+ needsTailChain,
192
+ reason,
193
+ warnMark,
194
+ });
195
+ if (end === i && brk.forced) {
196
+ warnings.push(`第 ${i + 1} 镜单镜时长 ${durOf(list[i])}s 已超单次生成上限 ${maxSeconds}s:这一段只能单独生成,并期望厂商钳制或报错`);
197
+ }
198
+ i = end + 1;
199
+ }
200
+ // 尾部碎段并回上一段:把 20.8s 切成「19.8 + 1.0」既没意义又必然误报硬切(1 秒那段一定从镜中开始)。
201
+ // 只有"末段很短且并回去只超上限一点点"才合并;中段碎块不动,避免掩盖真正的切点问题。
202
+ if (segments.length > 1) {
203
+ const last = segments[segments.length - 1];
204
+ const prev = segments[segments.length - 2];
205
+ const tol = Math.max(0.6, maxSeconds * 0.1);
206
+ const tinyTail = last.duration <= Math.max(2.5, maxSeconds * 0.2);
207
+ const fits = (prev.duration + last.duration) <= maxSeconds + tol;
208
+ if (tinyTail && fits) {
209
+ warnings.length = last.warnMark; // 丢弃为这一段产生的告警(它已不存在)
210
+ prev.shots = prev.shots.concat(last.shots);
211
+ prev.endIndex = last.endIndex;
212
+ prev.shotIds = prev.shots.map((x, k) => prev.startIndex + k);
213
+ prev.duration = Math.round((prev.duration + last.duration) * 10) / 10;
214
+ prev.lastSeg = last.lastSeg;
215
+ prev.closesOnCut = last.closesOnCut;
216
+ segments.pop();
217
+ warnings.push(`末段只有 ${last.duration}s,已并回上一段(合并后 ${prev.duration}s,超出单次上限 ${Math.round((prev.duration - maxSeconds) * 10) / 10}s 以内):比切出一个 1 秒的碎片更好生成`);
218
+ }
219
+ }
220
+
221
+ // cutMode:整表能不能被整齐切成"每次都是换镜头"的段——决定要不要提示用户重拆分镜
222
+ const cleanCuts = segments.every((s) => s.opensOnCut);
223
+ const cutMode = cleanCuts ? 'clean' : 'forced';
224
+ if (cutMode === 'forced') {
225
+ warnings.push('本次切段存在硬切点:建议把超长的续帧段在分镜里拆成两镜(第二镜标换场/特写插入),让切点落在真实镜头上');
226
+ }
227
+ if (segments.length > 1 && !cleanCuts) {
228
+ warnings.push(`共 ${segments.length} 段,其中 ${segments.filter((s) => !s.opensOnCut).length} 段从镜中断点开始——弱衔接模型(如 MiniMax)对这些段会明显吃力`);
229
+ }
230
+ return { segments, total, max: maxSeconds, cutMode, warnings };
231
+ }
232
+
233
+ export { planShotSegments, isCutPoint, shotContinuityPlan, shotVisualText };