@ninkch/dsh-infinite-canvas 0.20.2 → 0.20.4

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/index.js CHANGED
@@ -4064,7 +4064,7 @@ export function apply(ctx) {
4064
4064
  const boards = [];
4065
4065
  let prevBoard = null;
4066
4066
  // 0.20.2 T5 防线②:分镜产出后的场时长校验。切段算法与 client.js 同源(shot-segments.js),
4067
- // 上限同式取分镜表节点 data.segMaxSeconds(缺省 15,不另立常量)。判据:切段段数 >1
4067
+ // 上限同式取分镜表节点 data.targetDuration(0.20.3 起下拉即总时长语义;缺省 15,不另立常量)。判据:切段段数 >1
4068
4068
  //(该场要拆多次生成才装得下)或任一单镜 duration 超上限 → 场超时长。
4069
4069
  const shotDurOf = (s) => Math.max(0.3, Math.round((Number(s && s.duration) || 3) * 10) / 10);
4070
4070
  const durCheck = (shots, cap) => {
@@ -4103,7 +4103,7 @@ export function apply(ctx) {
4103
4103
  const sbDoc = readToolDoc({ projectId: pid, canvasId: cid });
4104
4104
  const sbNode0 = sbDoc.nodes[sb.nodeId];
4105
4105
  origReq = String((sbNode0 && sbNode0.data && sbNode0.data.requirement) || '');
4106
- segMax = Number(sbNode0 && sbNode0.data && sbNode0.data.segMaxSeconds) || 15;
4106
+ segMax = Number(sbNode0 && sbNode0.data && sbNode0.data.targetDuration) || 15;
4107
4107
  } catch { /* 读不到按空处理 */ }
4108
4108
  let sbSt = await canvasExec(pid, cid, sb.nodeId, { timeoutMs: 10 * 60 * 1000 });
4109
4109
  let gate = PROD.gateStoryboard(sbSt.shots);
@@ -4229,7 +4229,7 @@ export function apply(ctx) {
4229
4229
  const sb = boards[i];
4230
4230
  run.paidPhase = 'video ' + (i + 1) + '/' + boards.length;
4231
4231
  const shots = Array.isArray(sb.data && sb.data.shots) ? sb.data.shots : [];
4232
- const segMax = Number(sb.data && sb.data.segMaxSeconds) || 15;
4232
+ const segMax = Number(sb.data && sb.data.targetDuration) || 15;
4233
4233
  const plan = planShotSegments(shots, { maxSeconds: segMax });
4234
4234
  // 客户端兜底同款(production.quote 同式):切段为空但表里有镜头 → 整表按一段
4235
4235
  const segs = plan.segments.length ? plan.segments
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ninkch/dsh-infinite-canvas",
3
- "version": "0.20.2",
3
+ "version": "0.20.4",
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
@@ -238,7 +238,7 @@ export function createProduction(deps) {
238
238
  * 角色/场景/道具 实体数),顶层 refImagesTotal;跨场共享实体只计一次(与执行侧
239
239
  * run 级 Map 同序先到先得,归属第一场)。已有参考资产(readAssetIndexNames 边界匹配)的不计。
240
240
  * 切段口径与客户端执行完全同源:同一个 planShotSegments(shot-segments.js,client.js 的逐字移植),
241
- * maxSeconds 同样取分镜表节点 data.segMaxSeconds(缺省 15,client.js L13883 同式)。
241
+ * maxSeconds 同样取分镜表节点 data.targetDuration(0.20.3 起下拉即总时长语义;缺省 15,client.js StoryboardNodeBody 同式)。
242
242
  * 单价由调用方传入(画布设置里的单价,未配置则不给金额,只给调用次数与秒数)。
243
243
  */
244
244
  function quote(pid, cid, unitPricePerSecond) {
@@ -258,7 +258,7 @@ export function createProduction(deps) {
258
258
  const secs = shots.reduce((a, s) => a + (Number(s && s.duration) || 0), 0);
259
259
  totalSeconds += secs;
260
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;
261
+ const maxSeconds = Number(n.data.targetDuration) || 15;
262
262
  // 已有视频的表不产生新调用;待生成的表按切段算法数段(与客户端 expandStoryboard 同式同默认)
263
263
  let calls = 0;
264
264
  let refImages = 0;
@@ -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.targetDuration) || 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 };