dsh-creation-mode 0.0.0-stage → 3.0.0
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/LICENSE +33 -0
- package/README.md +26 -2
- package/client.js +2650 -0
- package/cordis.patch.yml +106 -0
- package/data.js +354 -0
- package/flow.js +338 -0
- package/icon.svg +13 -0
- package/index.js +234 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +50 -4
- package/stills/A1.jpg +0 -0
- package/stills/A2.jpg +0 -0
- package/stills/A3.jpg +0 -0
- package/stills/A4.jpg +0 -0
- package/stills/A5.jpg +0 -0
- package/stills/B1.jpg +0 -0
- package/stills/B2.jpg +0 -0
- package/stills/B3.jpg +0 -0
- package/stills/B4.jpg +0 -0
- package/stills/B5.jpg +0 -0
- package/stills/C1.jpg +0 -0
- package/stills/C2.jpg +0 -0
- package/stills/C3.jpg +0 -0
- package/stills/C4.jpg +0 -0
- package/stills/D1.jpg +0 -0
- package/stills/D2.jpg +0 -0
- package/stills/D3.jpg +0 -0
- package/stills/D4.jpg +0 -0
- package/stills/E1.jpg +0 -0
- package/stills/E2.jpg +0 -0
- package/stills/E3.jpg +0 -0
- package/stills/F1.jpg +0 -0
- package/stills/F2.jpg +0 -0
- package/stills/F3.jpg +0 -0
- package/stills/G1.jpg +0 -0
- package/stills/G2.jpg +0 -0
- package/stills/H1.jpg +0 -0
- package/stills/H10.jpg +0 -0
- package/stills/H11.jpg +0 -0
- package/stills/H12.jpg +0 -0
- package/stills/H2.jpg +0 -0
- package/stills/H3.jpg +0 -0
- package/stills/H4.jpg +0 -0
- package/stills/H5.jpg +0 -0
- package/stills/H6.jpg +0 -0
- package/stills/H7.jpg +0 -0
- package/stills/H8.jpg +0 -0
- package/stills/H9.jpg +0 -0
- package/stills/K1.jpg +0 -0
- package/stills/K10.jpg +0 -0
- package/stills/K11.jpg +0 -0
- package/stills/K12.jpg +0 -0
- package/stills/K13.jpg +0 -0
- package/stills/K14.jpg +0 -0
- package/stills/K15.jpg +0 -0
- package/stills/K16.jpg +0 -0
- package/stills/K17.jpg +0 -0
- package/stills/K18.jpg +0 -0
- package/stills/K19.jpg +0 -0
- package/stills/K2.jpg +0 -0
- package/stills/K20.jpg +0 -0
- package/stills/K21.jpg +0 -0
- package/stills/K22.jpg +0 -0
- package/stills/K23.jpg +0 -0
- package/stills/K24.jpg +0 -0
- package/stills/K25.jpg +0 -0
- package/stills/K26.jpg +0 -0
- package/stills/K27.jpg +0 -0
- package/stills/K28.jpg +0 -0
- package/stills/K29.jpg +0 -0
- package/stills/K3.jpg +0 -0
- package/stills/K30.jpg +0 -0
- package/stills/K31.jpg +0 -0
- package/stills/K32.jpg +0 -0
- package/stills/K33.jpg +0 -0
- package/stills/K34.jpg +0 -0
- package/stills/K35.jpg +0 -0
- package/stills/K36.jpg +0 -0
- package/stills/K37.jpg +0 -0
- package/stills/K38.jpg +0 -0
- package/stills/K4.jpg +0 -0
- package/stills/K5.jpg +0 -0
- package/stills/K6.jpg +0 -0
- package/stills/K7.jpg +0 -0
- package/stills/K8.jpg +0 -0
- package/stills/K9.jpg +0 -0
- package/stills/L1.jpg +0 -0
- package/stills/L10.jpg +0 -0
- package/stills/L11.jpg +0 -0
- package/stills/L12.jpg +0 -0
- package/stills/L13.jpg +0 -0
- package/stills/L14.jpg +0 -0
- package/stills/L15.jpg +0 -0
- package/stills/L16.jpg +0 -0
- package/stills/L17.jpg +0 -0
- package/stills/L18.jpg +0 -0
- package/stills/L19.jpg +0 -0
- package/stills/L2.jpg +0 -0
- package/stills/L20.jpg +0 -0
- package/stills/L21.jpg +0 -0
- package/stills/L22.jpg +0 -0
- package/stills/L23.jpg +0 -0
- package/stills/L24.jpg +0 -0
- package/stills/L25.jpg +0 -0
- package/stills/L26.jpg +0 -0
- package/stills/L27.jpg +0 -0
- package/stills/L28.jpg +0 -0
- package/stills/L29.jpg +0 -0
- package/stills/L3.jpg +0 -0
- package/stills/L30.jpg +0 -0
- package/stills/L31.jpg +0 -0
- package/stills/L32.jpg +0 -0
- package/stills/L33.jpg +0 -0
- package/stills/L34.jpg +0 -0
- package/stills/L35.jpg +0 -0
- package/stills/L36.jpg +0 -0
- package/stills/L37.jpg +0 -0
- package/stills/L38.jpg +0 -0
- package/stills/L39.jpg +0 -0
- package/stills/L4.jpg +0 -0
- package/stills/L40.jpg +0 -0
- package/stills/L41.jpg +0 -0
- package/stills/L42.jpg +0 -0
- package/stills/L43.jpg +0 -0
- package/stills/L44.jpg +0 -0
- package/stills/L45.jpg +0 -0
- package/stills/L46.jpg +0 -0
- package/stills/L47.jpg +0 -0
- package/stills/L48.jpg +0 -0
- package/stills/L49.jpg +0 -0
- package/stills/L5.jpg +0 -0
- package/stills/L50.jpg +0 -0
- package/stills/L51.jpg +0 -0
- package/stills/L52.jpg +0 -0
- package/stills/L53.jpg +0 -0
- package/stills/L54.jpg +0 -0
- package/stills/L55.jpg +0 -0
- package/stills/L56.jpg +0 -0
- package/stills/L57.jpg +0 -0
- package/stills/L58.jpg +0 -0
- package/stills/L59.jpg +0 -0
- package/stills/L6.jpg +0 -0
- package/stills/L7.jpg +0 -0
- package/stills/L8.jpg +0 -0
- package/stills/L9.jpg +0 -0
- package/stills/M1.jpg +0 -0
- package/stills/M10.jpg +0 -0
- package/stills/M11.jpg +0 -0
- package/stills/M12.jpg +0 -0
- package/stills/M13.jpg +0 -0
- package/stills/M14.jpg +0 -0
- package/stills/M15.jpg +0 -0
- package/stills/M16.jpg +0 -0
- package/stills/M17.jpg +0 -0
- package/stills/M18.jpg +0 -0
- package/stills/M19.jpg +0 -0
- package/stills/M2.jpg +0 -0
- package/stills/M20.jpg +0 -0
- package/stills/M21.jpg +0 -0
- package/stills/M22.jpg +0 -0
- package/stills/M23.jpg +0 -0
- package/stills/M24.jpg +0 -0
- package/stills/M25.jpg +0 -0
- package/stills/M26.jpg +0 -0
- package/stills/M27.jpg +0 -0
- package/stills/M28.jpg +0 -0
- package/stills/M29.jpg +0 -0
- package/stills/M3.jpg +0 -0
- package/stills/M30.jpg +0 -0
- package/stills/M31.jpg +0 -0
- package/stills/M32.jpg +0 -0
- package/stills/M33.jpg +0 -0
- package/stills/M34.jpg +0 -0
- package/stills/M35.jpg +0 -0
- package/stills/M36.jpg +0 -0
- package/stills/M37.jpg +0 -0
- package/stills/M38.jpg +0 -0
- package/stills/M39.jpg +0 -0
- package/stills/M4.jpg +0 -0
- package/stills/M40.jpg +0 -0
- package/stills/M5.jpg +0 -0
- package/stills/M6.jpg +0 -0
- package/stills/M7.jpg +0 -0
- package/stills/M8.jpg +0 -0
- package/stills/M9.jpg +0 -0
- package/stills/O1.jpg +0 -0
- package/stills/O10.jpg +0 -0
- package/stills/O11.jpg +0 -0
- package/stills/O12.jpg +0 -0
- package/stills/O13.jpg +0 -0
- package/stills/O14.jpg +0 -0
- package/stills/O15.jpg +0 -0
- package/stills/O16.jpg +0 -0
- package/stills/O17.jpg +0 -0
- package/stills/O18.jpg +0 -0
- package/stills/O19.jpg +0 -0
- package/stills/O2.jpg +0 -0
- package/stills/O20.jpg +0 -0
- package/stills/O21.jpg +0 -0
- package/stills/O22.jpg +0 -0
- package/stills/O23.jpg +0 -0
- package/stills/O24.jpg +0 -0
- package/stills/O25.jpg +0 -0
- package/stills/O26.jpg +0 -0
- package/stills/O27.jpg +0 -0
- package/stills/O28.jpg +0 -0
- package/stills/O29.jpg +0 -0
- package/stills/O3.jpg +0 -0
- package/stills/O30.jpg +0 -0
- package/stills/O31.jpg +0 -0
- package/stills/O32.jpg +0 -0
- package/stills/O33.jpg +0 -0
- package/stills/O34.jpg +0 -0
- package/stills/O35.jpg +0 -0
- package/stills/O36.jpg +0 -0
- package/stills/O37.jpg +0 -0
- package/stills/O38.jpg +0 -0
- package/stills/O39.jpg +0 -0
- package/stills/O4.jpg +0 -0
- package/stills/O40.jpg +0 -0
- package/stills/O41.jpg +0 -0
- package/stills/O42.jpg +0 -0
- package/stills/O43.jpg +0 -0
- package/stills/O44.jpg +0 -0
- package/stills/O45.jpg +0 -0
- package/stills/O46.jpg +0 -0
- package/stills/O47.jpg +0 -0
- package/stills/O48.jpg +0 -0
- package/stills/O49.jpg +0 -0
- package/stills/O5.jpg +0 -0
- package/stills/O50.jpg +0 -0
- package/stills/O51.jpg +0 -0
- package/stills/O52.jpg +0 -0
- package/stills/O53.jpg +0 -0
- package/stills/O54.jpg +0 -0
- package/stills/O55.jpg +0 -0
- package/stills/O56.jpg +0 -0
- package/stills/O57.jpg +0 -0
- package/stills/O58.jpg +0 -0
- package/stills/O59.jpg +0 -0
- package/stills/O6.jpg +0 -0
- package/stills/O60.jpg +0 -0
- package/stills/O61.jpg +0 -0
- package/stills/O62.jpg +0 -0
- package/stills/O63.jpg +0 -0
- package/stills/O64.jpg +0 -0
- package/stills/O65.jpg +0 -0
- package/stills/O66.jpg +0 -0
- package/stills/O67.jpg +0 -0
- package/stills/O68.jpg +0 -0
- package/stills/O69.jpg +0 -0
- package/stills/O7.jpg +0 -0
- package/stills/O70.jpg +0 -0
- package/stills/O71.jpg +0 -0
- package/stills/O72.jpg +0 -0
- package/stills/O73.jpg +0 -0
- package/stills/O74.jpg +0 -0
- package/stills/O75.jpg +0 -0
- package/stills/O76.jpg +0 -0
- package/stills/O77.jpg +0 -0
- package/stills/O78.jpg +0 -0
- package/stills/O79.jpg +0 -0
- package/stills/O8.jpg +0 -0
- package/stills/O80.jpg +0 -0
- package/stills/O81.jpg +0 -0
- package/stills/O82.jpg +0 -0
- package/stills/O83.jpg +0 -0
- package/stills/O84.jpg +0 -0
- package/stills/O85.jpg +0 -0
- package/stills/O86.jpg +0 -0
- package/stills/O87.jpg +0 -0
- package/stills/O88.jpg +0 -0
- package/stills/O89.jpg +0 -0
- package/stills/O9.jpg +0 -0
- package/stills/O90.jpg +0 -0
- package/stills/O91.jpg +0 -0
- package/stills/O92.jpg +0 -0
- package/stills/O93.jpg +0 -0
- package/stills/O94.jpg +0 -0
- package/stills/O95.jpg +0 -0
- package/stills/O96.jpg +0 -0
- package/stills/O97.jpg +0 -0
- package/stills/O98.jpg +0 -0
- package/stills/R1.jpg +0 -0
- package/stills/R2.jpg +0 -0
- package/stills/R3.jpg +0 -0
- package/stills/R4.jpg +0 -0
- package/stills/R5.jpg +0 -0
- package/stills/R6.jpg +0 -0
- package/stills/R7.jpg +0 -0
- package/stills/V1.jpg +0 -0
- package/stills/V10.jpg +0 -0
- package/stills/V11.jpg +0 -0
- package/stills/V12.jpg +0 -0
- package/stills/V13.jpg +0 -0
- package/stills/V14.jpg +0 -0
- package/stills/V15.jpg +0 -0
- package/stills/V2.jpg +0 -0
- package/stills/V3.jpg +0 -0
- package/stills/V4.jpg +0 -0
- package/stills/V5.jpg +0 -0
- package/stills/V6.jpg +0 -0
- package/stills/V7.jpg +0 -0
- package/stills/V8.jpg +0 -0
- package/stills/V9.jpg +0 -0
- package/stills/X1.jpg +0 -0
- package/stills/X10.jpg +0 -0
- package/stills/X11.jpg +0 -0
- package/stills/X12.jpg +0 -0
- package/stills/X13.jpg +0 -0
- package/stills/X14.jpg +0 -0
- package/stills/X15.jpg +0 -0
- package/stills/X16.jpg +0 -0
- package/stills/X17.jpg +0 -0
- package/stills/X18.jpg +0 -0
- package/stills/X19.jpg +0 -0
- package/stills/X2.jpg +0 -0
- package/stills/X20.jpg +0 -0
- package/stills/X21.jpg +0 -0
- package/stills/X22.jpg +0 -0
- package/stills/X23.jpg +0 -0
- package/stills/X24.jpg +0 -0
- package/stills/X25.jpg +0 -0
- package/stills/X26.jpg +0 -0
- package/stills/X27.jpg +0 -0
- package/stills/X28.jpg +0 -0
- package/stills/X29.jpg +0 -0
- package/stills/X3.jpg +0 -0
- package/stills/X30.jpg +0 -0
- package/stills/X31.jpg +0 -0
- package/stills/X4.jpg +0 -0
- package/stills/X5.jpg +0 -0
- package/stills/X6.jpg +0 -0
- package/stills/X7.jpg +0 -0
- package/stills/X8.jpg +0 -0
- package/stills/X9.jpg +0 -0
- package/stills/Z1.jpg +0 -0
- package/stills/Z10.jpg +0 -0
- package/stills/Z11.jpg +0 -0
- package/stills/Z12.jpg +0 -0
- package/stills/Z13.jpg +0 -0
- package/stills/Z14.jpg +0 -0
- package/stills/Z15.jpg +0 -0
- package/stills/Z2.jpg +0 -0
- package/stills/Z3.jpg +0 -0
- package/stills/Z4.jpg +0 -0
- package/stills/Z5.jpg +0 -0
- package/stills/Z6.jpg +0 -0
- package/stills/Z7.jpg +0 -0
- package/stills/Z8.jpg +0 -0
- package/stills/Z9.jpg +0 -0
- package/stills/approved.json +198 -0
- package/stills/index.json +1 -0
- package/styles.json +5090 -0
- package/tools.js +788 -0
package/tools.js
ADDED
|
@@ -0,0 +1,788 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creation-mode tools.
|
|
3
|
+
*
|
|
4
|
+
* The flow they support (enforced by flow.js, not by these tools):
|
|
5
|
+
* list_styles -> show_stills -> [用户拍板风格] -> get_style
|
|
6
|
+
* -> save_storyboard -> [用户拍板分镜] -> save_animation
|
|
7
|
+
*
|
|
8
|
+
* No second model call happens here: the Agent that already holds the user's
|
|
9
|
+
* context is the one that authors the animation.
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
|
|
14
|
+
import { OUTPUT_DIR, STILLS_DIR, catalog, defaultMusicFor, describe, searchStyles, styleById } from './data.js';
|
|
15
|
+
|
|
16
|
+
const MAX_HTML_BYTES = 2 * 1024 * 1024;
|
|
17
|
+
const MAX_STILLS_PER_CALL = 4;
|
|
18
|
+
|
|
19
|
+
/** Strip the wrappers a model habitually adds around a file body. */
|
|
20
|
+
function stripWrappers(text) {
|
|
21
|
+
let body = String(text || '').trim();
|
|
22
|
+
const fenced = /```(?:html|HTML)?\s*([\s\S]*?)```/.exec(body);
|
|
23
|
+
if (fenced && fenced[1].includes('<')) body = fenced[1].trim();
|
|
24
|
+
const start = body.search(/<!DOCTYPE html>|<html[\s>]/i);
|
|
25
|
+
if (start > 0) body = body.slice(start);
|
|
26
|
+
const end = body.toLowerCase().lastIndexOf('</html>');
|
|
27
|
+
if (end >= 0) body = body.slice(0, end + '</html>'.length);
|
|
28
|
+
return body.trim();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Strip wrappers from model-written score code.
|
|
33
|
+
*
|
|
34
|
+
* The body is stored verbatim and later compiled in the browser, so it must be a bare
|
|
35
|
+
* function body: no fences, no `function renderScore(...)` header, no `return`.
|
|
36
|
+
*/
|
|
37
|
+
function stripScore(text) {
|
|
38
|
+
let body = String(text || '').trim();
|
|
39
|
+
if (!body) return '';
|
|
40
|
+
const fenced = /```(?:js|javascript|JS)?\s*([\s\S]*?)```/.exec(body);
|
|
41
|
+
if (fenced) body = fenced[1].trim();
|
|
42
|
+
|
|
43
|
+
// Accept a wrapped declaration and keep only its body.
|
|
44
|
+
const wrapped = /(?:async\s+)?function\s+\w*\s*\([^)]*\)\s*\{([\s\S]*)\}\s*$/.exec(body);
|
|
45
|
+
if (wrapped) body = wrapped[1].trim();
|
|
46
|
+
|
|
47
|
+
// An arrow function with a block body.
|
|
48
|
+
const arrow = /^(?:async\s*)?\([^)]*\)\s*=>\s*\{([\s\S]*)\}\s*$/.exec(body);
|
|
49
|
+
if (arrow) body = arrow[1].trim();
|
|
50
|
+
|
|
51
|
+
return body.trim();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Validate the contract the preview and exporter both rely on. */
|
|
55
|
+
function inspectHtml(html) {
|
|
56
|
+
const problems = [];
|
|
57
|
+
if (!/<html[\s>]/i.test(html)) problems.push('缺少 <html> 根元素');
|
|
58
|
+
if (!/<canvas[\s>]/i.test(html)) problems.push('缺少 <canvas>(录制需要 canvas.captureStream)');
|
|
59
|
+
if (/window\.renderAt\s*=/.test(html) === false) problems.push('缺少 window.renderAt = function(t)');
|
|
60
|
+
if (/Math\.random\s*\(/.test(html)) problems.push('使用了 Math.random(),必须改为固定种子随机');
|
|
61
|
+
if (/requestAnimationFrame\s*\(/.test(html)) problems.push('使用了 requestAnimationFrame 自播,导出需要关闭它');
|
|
62
|
+
if (/<script[^>]+\bsrc=/i.test(html)) problems.push('引用了外部脚本');
|
|
63
|
+
if (/<img[^>]+\bsrc\s*=\s*["']https?:/i.test(html)) problems.push('引用了外部图片');
|
|
64
|
+
if (/<link[^>]+stylesheet/i.test(html)) problems.push('引用了外部样式表');
|
|
65
|
+
return problems;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Does the document paint into a canvas the exporter can capture? */
|
|
69
|
+
function hasCanvas(html) {
|
|
70
|
+
if (/<canvas[\s>]/i.test(html)) return true;
|
|
71
|
+
// A page may create its canvas from script rather than markup.
|
|
72
|
+
return /createElement\(\s*['"]canvas['"]\s*\)/i.test(html);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Reject animations the exporter can never record.
|
|
77
|
+
*
|
|
78
|
+
* A DOM/CSS animation previews fine but cannot be captured frame by frame, so shipping
|
|
79
|
+
* it only moves the failure to the export button. Failing here lets the Agent fix it in
|
|
80
|
+
* the same turn instead.
|
|
81
|
+
*/
|
|
82
|
+
function assertRecordable(html) {
|
|
83
|
+
if (!hasCanvas(html)) {
|
|
84
|
+
throw new Error([
|
|
85
|
+
'这段动画没有渲染到 <canvas>,无法逐帧录制,导出会失败。',
|
|
86
|
+
'请改成纯 Canvas 2D 实现:页面里有一个可见的 <canvas>,所有画面用 ctx 绘制,',
|
|
87
|
+
'window.renderAt(t) 负责把画面渲染到 t 时刻。',
|
|
88
|
+
'DOM / CSS 做出来的动画(div、svg、文字排版动画)不能被 captureStream 捕获。',
|
|
89
|
+
].join(''));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function slug(input) {
|
|
94
|
+
const base = String(input || 'animation')
|
|
95
|
+
.toLowerCase()
|
|
96
|
+
.replace(/[^\w-]+/g, '-')
|
|
97
|
+
.replace(/-+/g, '-')
|
|
98
|
+
.replace(/^-+|-+$/g, '')
|
|
99
|
+
.slice(0, 40);
|
|
100
|
+
return base || 'animation';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function round(value, digits = 2) {
|
|
104
|
+
const factor = 10 ** digits;
|
|
105
|
+
return Math.round(Number(value) * factor) / factor;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Phrases that make on-screen copy read as machine-written.
|
|
110
|
+
*
|
|
111
|
+
* These are warnings, never blocks: a line can quote one of them on purpose, and the
|
|
112
|
+
* model judges that. They exist because the prompt's ban list only works if the draft
|
|
113
|
+
* reports back when it was ignored.
|
|
114
|
+
*/
|
|
115
|
+
const AI_TELLS = [
|
|
116
|
+
'赋能', '闭环', '抓手', '颗粒度', '链路', '矩阵', '生态位',
|
|
117
|
+
'综上所述', '值得一提的是', '让我们一起', '前所未有',
|
|
118
|
+
'极致', '震撼', '惊艳', '颠覆', '化繁为简', '由浅入深', '层层递进',
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
/** Characters a viewer can comfortably read per second in a subtitle. */
|
|
122
|
+
const CHARS_PER_SECOND = 5.5;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Check a shot list for the mistakes that make a storyboard unusable.
|
|
126
|
+
* @returns warnings plus the total covered seconds; warnings render on the card.
|
|
127
|
+
*/
|
|
128
|
+
function inspectShots(shots, duration) {
|
|
129
|
+
const warnings = [];
|
|
130
|
+
if (!Array.isArray(shots) || shots.length === 0) {
|
|
131
|
+
return { warnings: ['分镜为空'], totalSeconds: 0 };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const ordered = [...shots].sort((a, b) => Number(a.start) - Number(b.start));
|
|
135
|
+
let cursor = 0;
|
|
136
|
+
let covered = 0;
|
|
137
|
+
|
|
138
|
+
for (const [index, shot] of ordered.entries()) {
|
|
139
|
+
const start = Number(shot.start);
|
|
140
|
+
const end = Number(shot.end);
|
|
141
|
+
const label = `第 ${index + 1} 场`;
|
|
142
|
+
|
|
143
|
+
if (!Number.isFinite(start) || !Number.isFinite(end)) {
|
|
144
|
+
warnings.push(`${label}的起止时间不是数字`);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (end <= start) warnings.push(`${label}的结束时间不大于开始时间`);
|
|
148
|
+
if (start < -0.001) warnings.push(`${label}开始时间早于 0`);
|
|
149
|
+
if (end > duration + 0.001) warnings.push(`${label}超出总时长 ${duration}s`);
|
|
150
|
+
if (start > cursor + 0.05) warnings.push(`${label}与上一场之间有 ${round(start - cursor)}s 空档`);
|
|
151
|
+
if (start < cursor - 0.05) warnings.push(`${label}与上一场时间重叠`);
|
|
152
|
+
if (!String(shot.scene || '').trim()) warnings.push(`${label}没有写画面内容`);
|
|
153
|
+
|
|
154
|
+
// On-screen copy is read once, at speed. A line that outlasts its shot is the
|
|
155
|
+
// most common reason a draft feels rushed, so it is measured, not left to taste.
|
|
156
|
+
const caption = String(shot.text || '').trim();
|
|
157
|
+
if (caption) {
|
|
158
|
+
const span = Math.max(0, end - start);
|
|
159
|
+
const limit = Math.max(4, Math.ceil(span * CHARS_PER_SECOND));
|
|
160
|
+
if (caption.length > limit) {
|
|
161
|
+
warnings.push(`${label}字幕 ${caption.length} 字,${round(span)}s 的镜头最多约 ${limit} 字,删到只剩一件事`);
|
|
162
|
+
}
|
|
163
|
+
const tells = AI_TELLS.filter((word) => caption.includes(word));
|
|
164
|
+
if (tells.length) warnings.push(`${label}字幕有套话「${tells.join('、')}」,换成具体的东西`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
cursor = Math.max(cursor, end);
|
|
168
|
+
covered += Math.max(0, end - start);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (ordered.length < 2) warnings.push('只有 1 场:分镜至少拆成 3 场才有节奏');
|
|
172
|
+
if (Math.abs(covered - duration) > 0.15) {
|
|
173
|
+
warnings.push(`各场时长合计 ${round(covered)}s,与总时长 ${duration}s 不一致`);
|
|
174
|
+
}
|
|
175
|
+
if (cursor < duration - 0.15) {
|
|
176
|
+
warnings.push(`最后一场在 ${round(cursor)}s 结束,距总时长 ${duration}s 还差 ${round(duration - cursor)}s`);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return { warnings, totalSeconds: round(covered) };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Resolve which score a clip should carry.
|
|
184
|
+
*
|
|
185
|
+
* An explicit request wins; otherwise the style implies a mood, so a Chinese-ink piece
|
|
186
|
+
* and a cyberpunk piece do not end up with the same bed.
|
|
187
|
+
* @param requested - the caller's `music` argument, if any.
|
|
188
|
+
* @param style - the chosen style, when known.
|
|
189
|
+
* @returns a preset id, or the caller's own free-form score description.
|
|
190
|
+
*/
|
|
191
|
+
function resolveMusic(requested, style) {
|
|
192
|
+
const wanted = String(requested || '').trim();
|
|
193
|
+
if (['none', 'off', '无', '无配乐', 'mute', '静音'].includes(wanted.toLowerCase())) return 'none';
|
|
194
|
+
// Allow completely unrestricted, tailored music prompt descriptions
|
|
195
|
+
if (wanted) return wanted;
|
|
196
|
+
if (style) return defaultMusicFor(style);
|
|
197
|
+
return '贴合画面风格与分镜剧情的专属卡点配乐';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Register the creation tools.
|
|
202
|
+
* @param deps - `{ tools, attachments }` from the realm that owns them. `tools` is
|
|
203
|
+
* required; `attachments` is optional and only disables inline reference stills,
|
|
204
|
+
* which then fall back to their URLs.
|
|
205
|
+
*/
|
|
206
|
+
export function registerCreationTools(deps) {
|
|
207
|
+
const { tools } = deps || {};
|
|
208
|
+
if (!tools) throw new Error('registerCreationTools needs the tools registry');
|
|
209
|
+
// A getter is preferred so an attachments service that arrives after activation is
|
|
210
|
+
// still picked up; a plain object keeps the simple call sites working.
|
|
211
|
+
const attachmentsOf = typeof deps.getAttachments === 'function'
|
|
212
|
+
? deps.getAttachments
|
|
213
|
+
: () => deps.attachments;
|
|
214
|
+
const disposers = [];
|
|
215
|
+
|
|
216
|
+
disposers.push(tools.register({
|
|
217
|
+
name: 'list_styles',
|
|
218
|
+
description: [
|
|
219
|
+
`在 ${catalog().styles.length} 种代码动画风格里检索。用户说出气质、题材或编号时先调用它。`,
|
|
220
|
+
'返回每条风格的编号、名称、系列、适用题材、是否有参考静帧,以及完整风格提示词。',
|
|
221
|
+
'检索结果只是候选清单:挑出 2–3 条之后,必须再用 show_stills 把画面给用户看。',
|
|
222
|
+
'选择规则:用户给了编号就精确匹配;只描述气质时挑 2–3 条最匹配的说明理由。',
|
|
223
|
+
'不要把多条风格的约束机械叠加:主画风、运动方式、版式各选一条,各自负责一件事。',
|
|
224
|
+
].join('\n'),
|
|
225
|
+
parameters: {
|
|
226
|
+
type: 'object',
|
|
227
|
+
properties: {
|
|
228
|
+
query: {
|
|
229
|
+
type: 'string',
|
|
230
|
+
description: '自然语言描述或编号。可以整句照搬用户说法(「赛博朋克霓虹街景」「像素小游戏开场」),会按词分别匹配并按命中词数排序;也可以直接给编号(「B3」「B3 A1」),此时只在编号里查。留空则按目录顺序浏览。',
|
|
231
|
+
},
|
|
232
|
+
series: {
|
|
233
|
+
type: 'string',
|
|
234
|
+
description: '限定系列:AG(代码样片)、M(动态风格)、K(卡通画风)、Z(中国风)、H(现成版式)、L(Lemo-Opuscar)、V(mg-styles-15)、X(code-video)、R(照参考学来的)、O(奥斯卡 98 部)。',
|
|
235
|
+
},
|
|
236
|
+
limit: { type: 'integer', description: '返回条数,默认 12,最多 50。', minimum: 1, maximum: 50 },
|
|
237
|
+
},
|
|
238
|
+
additionalProperties: false,
|
|
239
|
+
},
|
|
240
|
+
output: {
|
|
241
|
+
schema: {
|
|
242
|
+
type: 'object',
|
|
243
|
+
properties: {
|
|
244
|
+
total: { type: 'integer' },
|
|
245
|
+
styles: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
246
|
+
},
|
|
247
|
+
required: ['total', 'styles'],
|
|
248
|
+
additionalProperties: false,
|
|
249
|
+
},
|
|
250
|
+
render: (_args, value) => [{
|
|
251
|
+
type: 'text',
|
|
252
|
+
text: value.styles.length
|
|
253
|
+
? value.styles.map((s) => `${s.id} ${s.name}(${s.seriesName})|${s.description}${s.hasStill ? '|有参考静帧' : ''}`).join('\n')
|
|
254
|
+
+ '\n\n下一步:用 show_stills 把候选的画面给用户看,然后再让用户选。'
|
|
255
|
+
: '没有匹配的风格',
|
|
256
|
+
}],
|
|
257
|
+
presentationMeta: (_args, value) => ({ styles: value.styles }),
|
|
258
|
+
},
|
|
259
|
+
async execute(args) {
|
|
260
|
+
const { styles } = catalog();
|
|
261
|
+
const found = searchStyles(args?.query, { series: args?.series, limit: args?.limit });
|
|
262
|
+
return { total: styles.length, styles: found.map(describe) };
|
|
263
|
+
},
|
|
264
|
+
}));
|
|
265
|
+
|
|
266
|
+
disposers.push(tools.register({
|
|
267
|
+
name: 'show_stills',
|
|
268
|
+
description: [
|
|
269
|
+
'把选定风格的参考静帧作为图片贴进对话,让用户先看到画面再决定用哪一条。',
|
|
270
|
+
'推荐风格之后必须调用它:只靠文字描述,用户没法判断风格合不合口味。',
|
|
271
|
+
`一次最多贴 ${MAX_STILLS_PER_CALL} 张,通常给 2–3 条候选就够。没有参考静帧的风格会如实标出,此时用文字描述画面。`,
|
|
272
|
+
'贴完图就停下来等用户选,不要接着往下做。',
|
|
273
|
+
].join('\n'),
|
|
274
|
+
parameters: {
|
|
275
|
+
type: 'object',
|
|
276
|
+
properties: {
|
|
277
|
+
ids: {
|
|
278
|
+
type: 'array',
|
|
279
|
+
items: { type: 'string' },
|
|
280
|
+
description: '要展示的风格编号,如 ["B3","A1"]。',
|
|
281
|
+
},
|
|
282
|
+
note: { type: 'string', description: '一句话说明为什么给这几条,会显示在图片上方。' },
|
|
283
|
+
},
|
|
284
|
+
required: ['ids'],
|
|
285
|
+
additionalProperties: false,
|
|
286
|
+
},
|
|
287
|
+
output: {
|
|
288
|
+
schema: {
|
|
289
|
+
type: 'object',
|
|
290
|
+
properties: {
|
|
291
|
+
shown: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
292
|
+
missing: { type: 'array', items: { type: 'string' } },
|
|
293
|
+
unpublished: { type: 'array', items: { type: 'string' } },
|
|
294
|
+
note: { type: 'string' },
|
|
295
|
+
},
|
|
296
|
+
required: ['shown', 'missing'],
|
|
297
|
+
additionalProperties: false,
|
|
298
|
+
},
|
|
299
|
+
render: (_args, value) => {
|
|
300
|
+
const blocks = [];
|
|
301
|
+
const lines = [];
|
|
302
|
+
if (value.note) lines.push(value.note);
|
|
303
|
+
for (const item of value.shown) {
|
|
304
|
+
lines.push(`${item.id} ${item.name}|${item.description}`);
|
|
305
|
+
if (item.attachment) blocks.push({ type: 'image', attachment: item.attachment });
|
|
306
|
+
}
|
|
307
|
+
if (value.unpublished && value.unpublished.length) {
|
|
308
|
+
lines.push(`参考图还没上架:${value.unpublished.join('、')}(暂时不能用,等补图后再选)`);
|
|
309
|
+
}
|
|
310
|
+
if (value.missing.length) {
|
|
311
|
+
lines.push(`没有参考静帧:${value.missing.join('、')}(这几条请用文字描述画面)`);
|
|
312
|
+
}
|
|
313
|
+
return [{ type: 'text', text: lines.join('\n') }, ...blocks];
|
|
314
|
+
},
|
|
315
|
+
presentationMeta: (_args, value) => ({
|
|
316
|
+
shown: value.shown.map(({ attachment, ...rest }) => rest),
|
|
317
|
+
missing: value.missing,
|
|
318
|
+
unpublished: value.unpublished,
|
|
319
|
+
// The inline Tool view renders the reason the Agent gave for these candidates.
|
|
320
|
+
note: value.note,
|
|
321
|
+
}),
|
|
322
|
+
},
|
|
323
|
+
async execute(args) {
|
|
324
|
+
const ids = Array.isArray(args?.ids) ? args.ids.slice(0, MAX_STILLS_PER_CALL) : [];
|
|
325
|
+
if (!ids.length) throw new Error('ids 不能为空');
|
|
326
|
+
const shown = [];
|
|
327
|
+
const missing = [];
|
|
328
|
+
// A style that exists but has no published still is reported apart from an id
|
|
329
|
+
// that does not exist at all: the first is "not ready yet", the other is a typo.
|
|
330
|
+
const unpublished = [];
|
|
331
|
+
|
|
332
|
+
for (const raw of ids) {
|
|
333
|
+
const key = String(raw).trim().toUpperCase();
|
|
334
|
+
const style = styleById(raw);
|
|
335
|
+
if (!style) {
|
|
336
|
+
const known = styleById(key) || catalog().styles.find((s) => s.id === key);
|
|
337
|
+
if (!known && existsSync(join(STILLS_DIR, `${key}.jpg`))) unpublished.push(key);
|
|
338
|
+
else missing.push(String(raw));
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (!style.still) { missing.push(`${style.id} ${style.name}`); continue; }
|
|
342
|
+
|
|
343
|
+
let attachment;
|
|
344
|
+
const attachments = attachmentsOf();
|
|
345
|
+
if (attachments) {
|
|
346
|
+
try {
|
|
347
|
+
const data = readFileSync(join(STILLS_DIR, `${style.id}.jpg`));
|
|
348
|
+
attachment = await attachments.saveImage({
|
|
349
|
+
data,
|
|
350
|
+
mediaType: 'image/jpeg',
|
|
351
|
+
name: `${style.id}-${style.name}.jpg`,
|
|
352
|
+
});
|
|
353
|
+
} catch {
|
|
354
|
+
attachment = undefined; // the URL form below still tells the user which style it is
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
shown.push({
|
|
358
|
+
id: style.id,
|
|
359
|
+
name: style.name,
|
|
360
|
+
description: style.desc,
|
|
361
|
+
seriesName: style.seriesName,
|
|
362
|
+
...(attachment ? { attachment } : {}),
|
|
363
|
+
url: `/code-animation-api/still/${style.id}`,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (!shown.length) {
|
|
368
|
+
throw new Error(unpublished.length
|
|
369
|
+
? `这些风格的参考图还没上架:${unpublished.join('、')},暂时不能选;请从有图的风格里挑。`
|
|
370
|
+
: `这些编号没有可展示的静帧:${missing.join('、')}`);
|
|
371
|
+
}
|
|
372
|
+
return { shown, missing, unpublished, note: String(args?.note || '') };
|
|
373
|
+
},
|
|
374
|
+
}));
|
|
375
|
+
|
|
376
|
+
disposers.push(tools.register({
|
|
377
|
+
name: 'get_style',
|
|
378
|
+
description: '读取某一条风格的完整提示词与参考静帧信息。用户拍板风格后、出分镜之前调用它。',
|
|
379
|
+
parameters: {
|
|
380
|
+
type: 'object',
|
|
381
|
+
properties: { id: { type: 'string', description: '风格编号,如 B3、A1、O96。' } },
|
|
382
|
+
required: ['id'],
|
|
383
|
+
additionalProperties: false,
|
|
384
|
+
},
|
|
385
|
+
output: {
|
|
386
|
+
schema: {
|
|
387
|
+
type: 'object',
|
|
388
|
+
properties: {
|
|
389
|
+
id: { type: 'string' },
|
|
390
|
+
name: { type: 'string' },
|
|
391
|
+
prompt: { type: 'string' },
|
|
392
|
+
description: { type: 'string' },
|
|
393
|
+
stillUrl: { type: 'string', description: '参考静帧地址;没有静帧时是空字符串。' },
|
|
394
|
+
series: { type: 'string' },
|
|
395
|
+
seriesName: { type: 'string' },
|
|
396
|
+
// describe() 还会返回这些字段;漏声明会让模型看不到它们(如 promptChars)
|
|
397
|
+
category: { type: 'string' },
|
|
398
|
+
fit: { type: 'string' },
|
|
399
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
400
|
+
hasStill: { type: 'boolean' },
|
|
401
|
+
promptChars: { type: 'integer' },
|
|
402
|
+
music: { type: 'string', description: '该风格默认的配乐预设。' },
|
|
403
|
+
},
|
|
404
|
+
required: ['id', 'name', 'prompt'],
|
|
405
|
+
additionalProperties: true,
|
|
406
|
+
},
|
|
407
|
+
render: (_args, value) => [{
|
|
408
|
+
type: 'text',
|
|
409
|
+
text: `${value.id} ${value.name}\n\n${value.prompt || value.description || ''}\n\n下一步:按这条规范拆 3–8 个镜头,用 save_storyboard 出分镜草稿,等用户拍板。`,
|
|
410
|
+
}],
|
|
411
|
+
presentationMeta: (_args, value) => value,
|
|
412
|
+
},
|
|
413
|
+
async execute(args) {
|
|
414
|
+
const style = styleById(args?.id);
|
|
415
|
+
if (!style) {
|
|
416
|
+
const near = searchStyles(args?.id, { limit: 3 }).map((s) => s.id);
|
|
417
|
+
throw new Error(`没有编号为 ${args?.id} 的风格${near.length ? `,相近的有:${near.join('、')}` : ''}`);
|
|
418
|
+
}
|
|
419
|
+
return describe(style);
|
|
420
|
+
},
|
|
421
|
+
}));
|
|
422
|
+
|
|
423
|
+
disposers.push(tools.register({
|
|
424
|
+
name: 'save_storyboard',
|
|
425
|
+
description: [
|
|
426
|
+
'提交分镜草稿,在对话里渲染成可逐场审阅的分镜表。',
|
|
427
|
+
'每个镜头写清起止时间、画面内容、画面上的文字、运镜与转场。',
|
|
428
|
+
'分镜要覆盖开头、中段、结尾,各场时间合计等于总时长。',
|
|
429
|
+
'这一步不写动画代码。提交后停下来等用户拍板:加场、删场、改时长、改文案,改完重新提交。',
|
|
430
|
+
].join('\n'),
|
|
431
|
+
parameters: {
|
|
432
|
+
type: 'object',
|
|
433
|
+
properties: {
|
|
434
|
+
title: { type: 'string', description: '这个片子叫什么。' },
|
|
435
|
+
style_id: { type: 'string', description: '已拍板的风格编号。' },
|
|
436
|
+
duration: { type: 'number', description: '总时长(秒)。' },
|
|
437
|
+
fps: { type: 'integer', description: '帧率,默认 30。' },
|
|
438
|
+
aspect: { type: 'string', description: '画幅。', enum: ['16:9', '9:16', '1:1'] },
|
|
439
|
+
music: {
|
|
440
|
+
type: 'string',
|
|
441
|
+
description: '配乐风格与氛围定制提示词,根据画面风格与剧情转折自由生成(如:国风空灵古筝起步,2.5秒大堂鼓卡点爆发,4秒转入琵琶扫弦高潮)。没有预设限制,留空则自动按风格匹配。',
|
|
442
|
+
},
|
|
443
|
+
shots: {
|
|
444
|
+
type: 'array',
|
|
445
|
+
description: '镜头列表,按时间顺序。每个镜头的起始点会自动作为音乐切点卡点。',
|
|
446
|
+
items: {
|
|
447
|
+
type: 'object',
|
|
448
|
+
properties: {
|
|
449
|
+
start: { type: 'number', description: '开始时间(秒)。' },
|
|
450
|
+
end: { type: 'number', description: '结束时间(秒)。' },
|
|
451
|
+
scene: { type: 'string', description: '画面内容:谁、在哪、在做什么。' },
|
|
452
|
+
text: { type: 'string', description: '画面上出现的文字原文,没有就留空。' },
|
|
453
|
+
camera: { type: 'string', description: '运镜与转场,如「慢推」「硬切」「横向滑入」。' },
|
|
454
|
+
cues: {
|
|
455
|
+
type: 'array',
|
|
456
|
+
description: '该镜头内部的重点视觉动作与卡点时间(如文字砸落、数字翻转、粒子爆发瞬间)。',
|
|
457
|
+
items: {
|
|
458
|
+
type: 'object',
|
|
459
|
+
properties: {
|
|
460
|
+
time: { type: 'number', description: '卡点精确秒数' },
|
|
461
|
+
type: { type: 'string', description: '声音类型:impact(重击)/drop(跌落低音)/hit(打击)/rise(聚气扫频)/whoosh(呼啸)' },
|
|
462
|
+
label: { type: 'string', description: '动作描述,如「数字冲顶」' },
|
|
463
|
+
},
|
|
464
|
+
required: ['time'],
|
|
465
|
+
additionalProperties: false,
|
|
466
|
+
},
|
|
467
|
+
},
|
|
468
|
+
},
|
|
469
|
+
required: ['start', 'end', 'scene'],
|
|
470
|
+
additionalProperties: false,
|
|
471
|
+
},
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
required: ['title', 'duration', 'shots'],
|
|
475
|
+
additionalProperties: false,
|
|
476
|
+
},
|
|
477
|
+
output: {
|
|
478
|
+
schema: {
|
|
479
|
+
type: 'object',
|
|
480
|
+
properties: {
|
|
481
|
+
id: { type: 'string' },
|
|
482
|
+
title: { type: 'string' },
|
|
483
|
+
styleId: { type: 'string' },
|
|
484
|
+
styleName: { type: 'string' },
|
|
485
|
+
duration: { type: 'number' },
|
|
486
|
+
fps: { type: 'integer' },
|
|
487
|
+
aspect: { type: 'string' },
|
|
488
|
+
music: { type: 'string' },
|
|
489
|
+
totalSeconds: { type: 'number' },
|
|
490
|
+
shots: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
491
|
+
warnings: { type: 'array', items: { type: 'string' } },
|
|
492
|
+
},
|
|
493
|
+
required: ['id', 'title', 'duration', 'fps', 'aspect', 'music', 'shots', 'warnings'],
|
|
494
|
+
additionalProperties: false,
|
|
495
|
+
},
|
|
496
|
+
render: (_args, value) => [{
|
|
497
|
+
type: 'text',
|
|
498
|
+
text: [
|
|
499
|
+
`分镜草稿「${value.title}」${value.styleId ? `(${value.styleId} ${value.styleName || ''})` : ''}:${value.shots.length} 场 / ${value.duration}s`,
|
|
500
|
+
...value.shots.map((s, i) => `${i + 1}. ${s.start}–${s.end}s|${s.scene}${s.text ? `|字幕「${s.text}」` : ''}${s.camera ? `|${s.camera}` : ''}`),
|
|
501
|
+
value.warnings.length ? `\n需要留意:${value.warnings.join(';')}` : '',
|
|
502
|
+
`\n分镜 id:${value.id} 配乐:${value.music}`,
|
|
503
|
+
'请用户审这份分镜:要不要加场、删场、改时长、改文案。拍板之前不要开始写动画。',
|
|
504
|
+
].filter(Boolean).join('\n'),
|
|
505
|
+
}],
|
|
506
|
+
presentationMeta: (_args, value) => value,
|
|
507
|
+
},
|
|
508
|
+
async execute(args) {
|
|
509
|
+
const shots = Array.isArray(args?.shots) ? args.shots : [];
|
|
510
|
+
if (!shots.length) throw new Error('shots 不能为空');
|
|
511
|
+
const duration = Math.max(1, Math.min(Number(args?.duration) || 8, 300));
|
|
512
|
+
const fps = Math.max(1, Math.min(Number(args?.fps) || 30, 60));
|
|
513
|
+
const aspect = ['16:9', '9:16', '1:1'].includes(args?.aspect) ? args.aspect : '16:9';
|
|
514
|
+
const style = args?.style_id ? styleById(args.style_id) : undefined;
|
|
515
|
+
const { warnings, totalSeconds } = inspectShots(shots, duration);
|
|
516
|
+
const music = resolveMusic(args?.music, style);
|
|
517
|
+
|
|
518
|
+
const id = `sb-${slug(args?.title)}-${Date.now().toString(36)}`;
|
|
519
|
+
const storyboard = {
|
|
520
|
+
id,
|
|
521
|
+
title: String(args?.title || '分镜'),
|
|
522
|
+
styleId: style ? style.id : '',
|
|
523
|
+
styleName: style ? style.name : '',
|
|
524
|
+
duration,
|
|
525
|
+
fps,
|
|
526
|
+
aspect,
|
|
527
|
+
music,
|
|
528
|
+
totalSeconds,
|
|
529
|
+
shots: shots.map((s, i) => ({
|
|
530
|
+
index: i + 1,
|
|
531
|
+
start: round(Number(s.start) || 0),
|
|
532
|
+
end: round(Number(s.end) || 0),
|
|
533
|
+
scene: String(s.scene || ''),
|
|
534
|
+
text: String(s.text || ''),
|
|
535
|
+
camera: String(s.camera || ''),
|
|
536
|
+
cues: Array.isArray(s.cues) ? s.cues.map((c) => ({
|
|
537
|
+
time: round(Number(c.time) || 0),
|
|
538
|
+
type: String(c.type || 'hit'),
|
|
539
|
+
label: String(c.label || ''),
|
|
540
|
+
})) : [],
|
|
541
|
+
})),
|
|
542
|
+
warnings,
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
546
|
+
writeFileSync(join(OUTPUT_DIR, `${id}.json`), JSON.stringify(storyboard, null, 2), 'utf8');
|
|
547
|
+
return storyboard;
|
|
548
|
+
},
|
|
549
|
+
}));
|
|
550
|
+
|
|
551
|
+
disposers.push(tools.register({
|
|
552
|
+
name: 'save_animation',
|
|
553
|
+
description: [
|
|
554
|
+
'把一段动画 HTML 存到工作区,并在对话里渲染成可播放、可导出视频的动画卡片。',
|
|
555
|
+
'调用前应当已经完成:风格拍板(get_style)与分镜拍板(save_storyboard)。把分镜 id 传进 storyboard_id,卡片会带上分镜信息。',
|
|
556
|
+
'HTML 必须满足:单个完整文件、只含原生 HTML/JS/Canvas、不引用任何外部库或图片、实现 window.renderAt(t)(t 为秒,同一 t 渲染同一帧)、使用固定种子随机、不启用 requestAnimationFrame 自播。',
|
|
557
|
+
'生成后用 renderAt 检查首帧、中段、尾帧,确认不是黑屏或静止。',
|
|
558
|
+
].join('\n'),
|
|
559
|
+
parameters: {
|
|
560
|
+
type: 'object',
|
|
561
|
+
properties: {
|
|
562
|
+
title: { type: 'string', description: '这个动画叫什么,用于卡片标题和文件名。' },
|
|
563
|
+
style_id: { type: 'string', description: '采用的风格编号,如 B3。' },
|
|
564
|
+
storyboard_id: { type: 'string', description: '已拍板的分镜 id(save_storyboard 的返回值)。' },
|
|
565
|
+
music: {
|
|
566
|
+
type: 'string',
|
|
567
|
+
description: '配乐风格定制提示词(如:国风水墨意境、赛博重低音电子、史诗管弦、轻快动感等),留空则自动从分镜或风格继承,无任何固定预设限制。',
|
|
568
|
+
},
|
|
569
|
+
cues: {
|
|
570
|
+
type: 'array',
|
|
571
|
+
description: '全片精确卡点列表(秒数与事件类型),除镜头切点外,镜头内部的关键视觉重点(文字砸下、数据爆发、粒子聚散)在此标注,配乐会在该时间触发重音。',
|
|
572
|
+
items: {
|
|
573
|
+
type: 'object',
|
|
574
|
+
properties: {
|
|
575
|
+
time: { type: 'number', description: '精确时间点(秒)' },
|
|
576
|
+
type: { type: 'string', description: '声音类型:impact/drop/hit/rise/whoosh' },
|
|
577
|
+
label: { type: 'string', description: '事件名称' },
|
|
578
|
+
},
|
|
579
|
+
required: ['time'],
|
|
580
|
+
additionalProperties: false,
|
|
581
|
+
},
|
|
582
|
+
},
|
|
583
|
+
beats: {
|
|
584
|
+
type: 'array',
|
|
585
|
+
description: [
|
|
586
|
+
'从你自己写的动画代码里导出的精确节奏点,这是卡点最可靠的来源。',
|
|
587
|
+
'写动画时你就知道每个元素在第几秒出现、哪一帧爆发,把那些时刻抄到这里。',
|
|
588
|
+
'每项 { time, type, label, power }:time 是精确秒数(可以带小数,如 2.87);',
|
|
589
|
+
'type 是 impact(重击)/ rise(起势)/ drop(下坠)/ hit(轻点)/ swell(渐强);',
|
|
590
|
+
'label 写清对应画面里发生了什么,如「故障闪烁峰值」「中文标题砸下」;',
|
|
591
|
+
'power 是 0–1 的强度。',
|
|
592
|
+
'这些时间点会传给配乐代码,卡点位置不必再猜。',
|
|
593
|
+
].join(''),
|
|
594
|
+
items: {
|
|
595
|
+
type: 'object',
|
|
596
|
+
properties: {
|
|
597
|
+
time: { type: 'number', description: '精确秒数,可带小数' },
|
|
598
|
+
type: { type: 'string', description: 'impact / rise / drop / hit / swell' },
|
|
599
|
+
label: { type: 'string', description: '画面里发生了什么' },
|
|
600
|
+
power: { type: 'number', description: '强度 0–1,默认 0.8' },
|
|
601
|
+
},
|
|
602
|
+
required: ['time'],
|
|
603
|
+
additionalProperties: false,
|
|
604
|
+
},
|
|
605
|
+
},
|
|
606
|
+
duration: { type: 'number', description: '片长(秒),用于预览进度条与导出。默认 8。' },
|
|
607
|
+
fps: { type: 'integer', description: '帧率,默认 30。' },
|
|
608
|
+
html: { type: 'string', description: '完整的动画 HTML 文件正文。' },
|
|
609
|
+
score_js: {
|
|
610
|
+
type: 'string',
|
|
611
|
+
description: [
|
|
612
|
+
'你自己为这个片子写的配乐合成代码(Web Audio)。不要用任何预设音色库。',
|
|
613
|
+
'代码是一个函数体,可用的形参:ctx(OfflineAudioContext)、dest(AudioNode 出口)、',
|
|
614
|
+
'duration(秒)、cuts(分镜切点秒数数组)、cues(分镜里的镜头重点)、',
|
|
615
|
+
'beats(你在 beats 参数里给的精确节奏点,每项 {time,type,label,power})、',
|
|
616
|
+
'rand(可复现随机函数)、hz(MIDI 转频率)。',
|
|
617
|
+
'在里面用 ctx.createOscillator / createBufferSource / createBiquadFilter / createGain 等搭音色,',
|
|
618
|
+
'把声音接到 dest 上,并在绝对时间点上 schedule(用 start(t))。',
|
|
619
|
+
'所有节点必须按 duration 收尾,不要在函数里 await。',
|
|
620
|
+
'卡点用 beats 里的 time:它来自动画代码本身,比 cuts 准。',
|
|
621
|
+
'留空则退回内置合成引擎。',
|
|
622
|
+
].join(''),
|
|
623
|
+
},
|
|
624
|
+
},
|
|
625
|
+
required: ['title', 'html'],
|
|
626
|
+
additionalProperties: false,
|
|
627
|
+
},
|
|
628
|
+
output: {
|
|
629
|
+
schema: {
|
|
630
|
+
type: 'object',
|
|
631
|
+
properties: {
|
|
632
|
+
id: { type: 'string' },
|
|
633
|
+
title: { type: 'string' },
|
|
634
|
+
styleId: { type: 'string' },
|
|
635
|
+
styleName: { type: 'string' },
|
|
636
|
+
storyboardId: { type: 'string' },
|
|
637
|
+
music: { type: 'string' },
|
|
638
|
+
cuts: { type: 'array', items: { type: 'number' }, description: '切点秒数,配乐按这些点卡拍。' },
|
|
639
|
+
cues: { type: 'array', items: { type: 'object', additionalProperties: true }, description: '精确卡点列表' },
|
|
640
|
+
beats: { type: 'array', items: { type: 'object', additionalProperties: true }, description: '从动画代码导出的节奏点' },
|
|
641
|
+
scoreUrl: { type: 'string', description: '模型自写配乐的地址;空串表示使用内置引擎。' },
|
|
642
|
+
duration: { type: 'number' },
|
|
643
|
+
fps: { type: 'integer' },
|
|
644
|
+
url: { type: 'string' },
|
|
645
|
+
bytes: { type: 'integer' },
|
|
646
|
+
warnings: { type: 'array', items: { type: 'string' } },
|
|
647
|
+
},
|
|
648
|
+
required: ['id', 'title', 'url', 'duration', 'fps'],
|
|
649
|
+
additionalProperties: false,
|
|
650
|
+
},
|
|
651
|
+
render: (_args, value) => [{
|
|
652
|
+
type: 'text',
|
|
653
|
+
text: [
|
|
654
|
+
`已生成动画「${value.title}」${value.styleId ? `(${value.styleId} ${value.styleName || ''})` : ''}`,
|
|
655
|
+
`片长 ${value.duration}s · ${value.fps}fps · ${(value.bytes / 1024).toFixed(0)} KB`,
|
|
656
|
+
value.warnings.length ? `注意:${value.warnings.join(';')}` : '',
|
|
657
|
+
`地址:${value.url}`,
|
|
658
|
+
`配乐:${value.music}`,
|
|
659
|
+
`卡点对齐:${value.cuts.length} 个分镜切点${value.cues && value.cues.length ? ` + ${value.cues.length} 个镜头重点` : ''}已毫秒级精准对齐`,
|
|
660
|
+
'对话末尾已显示预览卡片:点击播放音画同步出声,点击导出直接一键下载成片。',
|
|
661
|
+
].filter(Boolean).join('\n'),
|
|
662
|
+
}],
|
|
663
|
+
presentationMeta: (_args, value) => value,
|
|
664
|
+
},
|
|
665
|
+
async execute(args) {
|
|
666
|
+
const html = stripWrappers(args?.html);
|
|
667
|
+
if (!html) throw new Error('html 为空');
|
|
668
|
+
const bytes = Buffer.byteLength(html, 'utf8');
|
|
669
|
+
if (bytes > MAX_HTML_BYTES) throw new Error(`HTML 过大(${(bytes / 1048576).toFixed(1)} MB),上限 2 MB`);
|
|
670
|
+
|
|
671
|
+
// 强制校验必须含有 <canvas>,若是 DOM/CSS 动画在此处直接报错并提示 Agent 修复
|
|
672
|
+
assertRecordable(html);
|
|
673
|
+
|
|
674
|
+
const warnings = inspectHtml(html);
|
|
675
|
+
const style = args?.style_id ? styleById(args.style_id) : undefined;
|
|
676
|
+
const storyboardId = String(args?.storyboard_id || '');
|
|
677
|
+
|
|
678
|
+
// The storyboard supplies both the score choice and the cut points; without it
|
|
679
|
+
// the score still works, it just has no edits to land on.
|
|
680
|
+
let board;
|
|
681
|
+
if (storyboardId) {
|
|
682
|
+
try {
|
|
683
|
+
board = JSON.parse(readFileSync(join(OUTPUT_DIR, `${storyboardId}.json`), 'utf8'));
|
|
684
|
+
} catch {
|
|
685
|
+
warnings.push(`找不到分镜 ${storyboardId},卡片不会带分镜信息`);
|
|
686
|
+
}
|
|
687
|
+
} else {
|
|
688
|
+
warnings.push('没有关联分镜:这版动画未经用户拍板');
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
const music = resolveMusic(args?.music || (board && board.music), style);
|
|
692
|
+
const cuts = Array.isArray(board?.shots)
|
|
693
|
+
? board.shots
|
|
694
|
+
.map((shot) => round(Number(shot.start) || 0))
|
|
695
|
+
.filter((t) => t > 0.05 && t < (Number(args?.duration) || board.duration || 8))
|
|
696
|
+
.sort((a, b) => a - b)
|
|
697
|
+
: [];
|
|
698
|
+
|
|
699
|
+
// Collect all internal visual hit cues from storyboard shots and args
|
|
700
|
+
const cues = [];
|
|
701
|
+
if (Array.isArray(board?.shots)) {
|
|
702
|
+
for (const s of board.shots) {
|
|
703
|
+
if (Array.isArray(s.cues)) {
|
|
704
|
+
for (const c of s.cues) {
|
|
705
|
+
const t = round(Number(c.time) || 0);
|
|
706
|
+
if (t > 0.05 && t < (Number(args?.duration) || board.duration || 8)) {
|
|
707
|
+
cues.push({ time: t, type: c.type || 'hit', label: c.label || '' });
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
if (Array.isArray(args?.cues)) {
|
|
714
|
+
for (const c of args.cues) {
|
|
715
|
+
const t = round(Number(c.time) || 0);
|
|
716
|
+
if (t > 0.05 && t < (Number(args?.duration) || board.duration || 8)) {
|
|
717
|
+
cues.push({ time: t, type: c.type || 'hit', label: c.label || '' });
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
const duration = Math.max(1, Math.min(Number(args?.duration) || board?.duration || 8, 120));
|
|
723
|
+
const fps = Math.max(1, Math.min(Number(args?.fps) || board?.fps || 30, 60));
|
|
724
|
+
|
|
725
|
+
// Beats come from the animation code the model just wrote, so they are the most
|
|
726
|
+
// trusted sync points: keep sub-second precision rather than rounding like cues.
|
|
727
|
+
const beats = (Array.isArray(args?.beats) ? args.beats : [])
|
|
728
|
+
.map((b) => ({
|
|
729
|
+
time: Math.round((Number(b.time) || 0) * 1000) / 1000,
|
|
730
|
+
type: String(b.type || 'hit'),
|
|
731
|
+
label: String(b.label || ''),
|
|
732
|
+
power: Number.isFinite(Number(b.power)) ? Math.max(0, Math.min(1, Number(b.power))) : 0.8,
|
|
733
|
+
}))
|
|
734
|
+
.filter((b) => b.time > 0.02 && b.time < duration)
|
|
735
|
+
.sort((a, b) => a.time - b.time);
|
|
736
|
+
|
|
737
|
+
const id = `anim-${slug(args?.title)}-${Date.now().toString(36)}`;
|
|
738
|
+
|
|
739
|
+
mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
740
|
+
writeFileSync(join(OUTPUT_DIR, `${id}.html`), html, 'utf8');
|
|
741
|
+
|
|
742
|
+
// A model-written score replaces the built-in engine entirely for this clip.
|
|
743
|
+
let scoreUrl = '';
|
|
744
|
+
const scoreJs = stripScore(args?.score_js);
|
|
745
|
+
if (scoreJs) {
|
|
746
|
+
if (!/createOscillator|createBufferSource|createGain|createBiquadFilter/.test(scoreJs)) {
|
|
747
|
+
warnings.push('自写配乐里没有创建任何音频节点,已退回内置引擎');
|
|
748
|
+
} else {
|
|
749
|
+
writeFileSync(join(OUTPUT_DIR, `${id}.score.js`), scoreJs, 'utf8');
|
|
750
|
+
scoreUrl = `/code-animation-api/score/${id}`;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// Without beats the score has to infer where the picture peaks, which is exactly
|
|
755
|
+
// the guessing the beats parameter exists to remove.
|
|
756
|
+
if (scoreUrl && !beats.length) {
|
|
757
|
+
warnings.push('配乐没有收到 beats:卡点只能靠 cuts 推,精度会差一些');
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
const value = {
|
|
761
|
+
id,
|
|
762
|
+
title: String(args?.title || '动画'),
|
|
763
|
+
styleId: style ? style.id : '',
|
|
764
|
+
styleName: style ? style.name : '',
|
|
765
|
+
music: scoreUrl ? '模型自写配乐' : music,
|
|
766
|
+
cuts,
|
|
767
|
+
cues,
|
|
768
|
+
beats,
|
|
769
|
+
scoreUrl,
|
|
770
|
+
duration,
|
|
771
|
+
fps,
|
|
772
|
+
url: `/code-animation-api/animation/${id}`,
|
|
773
|
+
bytes,
|
|
774
|
+
warnings,
|
|
775
|
+
};
|
|
776
|
+
if (storyboardId) value.storyboardId = storyboardId;
|
|
777
|
+
return value;
|
|
778
|
+
},
|
|
779
|
+
}));
|
|
780
|
+
|
|
781
|
+
return () => {
|
|
782
|
+
for (const dispose of disposers.reverse()) {
|
|
783
|
+
try { dispose(); } catch { /* one bad disposer must not block the rest */ }
|
|
784
|
+
}
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
export { inspectHtml, inspectShots, assertRecordable };
|