@ninkch/dsh-infinite-canvas 0.20.5 → 0.21.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/index.js CHANGED
@@ -331,6 +331,7 @@ function ensureSchema(db, root) {
331
331
  rel TEXT, sha TEXT, bytes INTEGER,
332
332
  width INTEGER, height INTEGER, duration REAL,
333
333
  prompt TEXT, category TEXT, entity TEXT, entities TEXT, node_label TEXT,
334
+ tags TEXT, folder TEXT, merged_into TEXT,
334
335
  text TEXT, source TEXT, created_at INTEGER, updated_at INTEGER);
335
336
  CREATE INDEX IF NOT EXISTS idx_assets_proj ON assets(project_id, created_at DESC, id DESC);
336
337
  CREATE INDEX IF NOT EXISTS idx_assets_sha ON assets(sha);
@@ -338,6 +339,13 @@ function ensureSchema(db, root) {
338
339
  CREATE TABLE IF NOT EXISTS entities (id TEXT PRIMARY KEY, project_id TEXT, kind TEXT, name TEXT);
339
340
  CREATE TABLE IF NOT EXISTS episodes (id TEXT PRIMARY KEY, project_id TEXT, season INTEGER, number INTEGER, title TEXT);
340
341
  `);
342
+ // DAM 增量列(旧库迁移):tags = JSON 数组;folder = 用户文件夹名。
343
+ try {
344
+ const cols = db.prepare('PRAGMA table_info(assets)').all().map((c) => c.name);
345
+ if (!cols.includes('tags')) db.exec('ALTER TABLE assets ADD COLUMN tags TEXT');
346
+ if (!cols.includes('folder')) db.exec('ALTER TABLE assets ADD COLUMN folder TEXT');
347
+ if (!cols.includes('merged_into')) db.exec('ALTER TABLE assets ADD COLUMN merged_into TEXT');
348
+ } catch { /* 迁移失败不阻塞启动:旧库仍可用 */ }
341
349
  if (!metaGet(db, 'fts_tokenizer')) {
342
350
  try {
343
351
  db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS assets_fts USING fts5(id UNINDEXED, name, prompt, tokenize = 'trigram')`);
@@ -439,6 +447,9 @@ function assetRowFromRecord(a) {
439
447
  entity: a.entity ? String(a.entity) : null,
440
448
  entities: Array.isArray(a.entities) ? JSON.stringify(a.entities) : null,
441
449
  node_label: a.nodeLabel ? String(a.nodeLabel) : null,
450
+ tags: Array.isArray(a.tags) && a.tags.length ? JSON.stringify(a.tags.map(String).map((t) => t.trim()).filter(Boolean).slice(0, 20)) : null,
451
+ folder: a.folder ? String(a.folder).slice(0, 80) : null,
452
+ merged_into: a.mergedInto ? String(a.mergedInto) : null,
442
453
  text: (a.kind === 'text' && typeof a.text === 'string') ? a.text : null,
443
454
  source: String(a.source || ''),
444
455
  created_at: Number(a.createdAt) || Date.now(),
@@ -461,6 +472,9 @@ function rowToAsset(r) {
461
472
  if (r.entity) out.entity = r.entity;
462
473
  if (r.entities) { try { out.entities = JSON.parse(r.entities); } catch { /* 忽略 */ } }
463
474
  if (r.node_label) out.nodeLabel = r.node_label;
475
+ if (r.tags) { try { const tags = JSON.parse(r.tags); if (Array.isArray(tags) && tags.length) out.tags = tags; } catch { /* 忽略 */ } }
476
+ if (r.folder) out.folder = r.folder;
477
+ if (r.merged_into) out.mergedInto = r.merged_into;
464
478
  if (r.text != null && r.text !== '') out.text = r.text;
465
479
  return out;
466
480
  }
@@ -471,11 +485,11 @@ function syncFts(db, id, name, prompt) {
471
485
  }
472
486
 
473
487
  function upsertAssetRow(db, row) {
474
- db.prepare(`INSERT INTO assets (id, project_id, episode_id, kind, mime, name, rel, sha, bytes, width, height, duration, prompt, category, entity, entities, node_label, text, source, created_at, updated_at)
475
- VALUES (@id, @project_id, @episode_id, @kind, @mime, @name, @rel, @sha, @bytes, @width, @height, @duration, @prompt, @category, @entity, @entities, @node_label, @text, @source, @created_at, @updated_at)
488
+ db.prepare(`INSERT INTO assets (id, project_id, episode_id, kind, mime, name, rel, sha, bytes, width, height, duration, prompt, category, entity, entities, node_label, tags, folder, merged_into, text, source, created_at, updated_at)
489
+ VALUES (@id, @project_id, @episode_id, @kind, @mime, @name, @rel, @sha, @bytes, @width, @height, @duration, @prompt, @category, @entity, @entities, @node_label, @tags, @folder, @merged_into, @text, @source, @created_at, @updated_at)
476
490
  ON CONFLICT(id) DO UPDATE SET project_id=@project_id, episode_id=@episode_id, kind=@kind, mime=@mime, name=@name, rel=@rel, sha=@sha, bytes=@bytes,
477
491
  width=@width, height=@height, duration=@duration, prompt=@prompt, category=@category, entity=@entity, entities=@entities,
478
- node_label=@node_label, text=@text, source=@source, updated_at=@updated_at`).run(row);
492
+ node_label=@node_label, tags=@tags, folder=@folder, merged_into=@merged_into, text=@text, source=@source, updated_at=@updated_at`).run(row);
479
493
  syncFts(db, row.id, row.name, row.prompt);
480
494
  }
481
495
 
@@ -597,6 +611,9 @@ function dbAssetUpdate(id, patch) {
597
611
  if (patch && patch.entity !== undefined) merged.entity = patch.entity ? String(patch.entity) : undefined;
598
612
  if (patch && patch.entities !== undefined) merged.entities = Array.isArray(patch.entities) ? patch.entities : undefined;
599
613
  if (patch && patch.nodeLabel !== undefined) merged.nodeLabel = patch.nodeLabel ? String(patch.nodeLabel) : undefined;
614
+ if (patch && patch.tags !== undefined) merged.tags = Array.isArray(patch.tags) ? patch.tags.map(String).map((t) => t.trim()).filter(Boolean).slice(0, 20) : [];
615
+ if (patch && patch.folder !== undefined) merged.folder = patch.folder ? String(patch.folder).slice(0, 80) : undefined;
616
+ if (patch && patch.mergedInto !== undefined) merged.mergedInto = patch.mergedInto ? String(patch.mergedInto) : undefined;
600
617
  if (patch && patch.text !== undefined && cur.kind === 'text') merged.text = String(patch.text || '');
601
618
  upsertAssetRow(db, assetRowFromRecord({ ...merged, createdAt: cur.createdAt }));
602
619
  return { ok: true, asset: rowToAsset(db.prepare('SELECT * FROM assets WHERE id = ?').get(row.id)) };
@@ -2543,10 +2560,11 @@ function canvasCreateNode(args) {
2543
2560
  const doc = readToolDoc(args);
2544
2561
  const rawType = String((args && args.type) || 'text');
2545
2562
  const type = normalizeNodeType(rawType);
2546
- if (!['script', 'text', 'image', 'video', 'storyboard', 'finalcut'].includes(type)) throw new Error('不支持的节点类型: ' + rawType);
2563
+ if (!['script', 'text', 'image', 'video', 'audio', 'storyboard', 'finalcut'].includes(type)) throw new Error('不支持的节点类型: ' + rawType);
2547
2564
  const legacyType = rawType !== type ? rawType : '';
2548
2565
  const label = args && args.name ? String(args.name).slice(0, 40) : '';
2549
2566
  const prompt = String((args && args.prompt) || '');
2567
+ const assetId = String((args && args.assetId) || '').trim() || null;
2550
2568
  const connectFrom = String((args && args.connectFrom) || '');
2551
2569
  // G4 禁止逐镜节点:一致性靠参考图资产,不靠逐镜建点(高频走错路,这里直接封死)
2552
2570
  const g4 = PROD.gateShotNodeName(label, type);
@@ -2567,7 +2585,7 @@ function canvasCreateNode(args) {
2567
2585
  node = agentNode('text', '', pos.x, pos.y, label || '图片节点');
2568
2586
  node.type = 'image';
2569
2587
  node.w = mediaSize.w; node.h = mediaSize.h;
2570
- node.data = { ...node.data, status: 'idle', error: null, prompt, assetId: null, demo: false, size: (args && args.size) || '2K', ratio: (args && args.ratio) || '16:9', imageCount: 1 };
2588
+ node.data = { ...node.data, status: assetId ? 'done' : 'idle', error: null, prompt, assetId: assetId || null, demo: false, size: (args && args.size) || '2K', ratio: (args && args.ratio) || '16:9', imageCount: 1 };
2571
2589
  } else if (type === 'video') {
2572
2590
  node = agentNode('text', '', pos.x, pos.y, label || '视频节点');
2573
2591
  node.type = 'video';
@@ -2578,12 +2596,13 @@ function canvasCreateNode(args) {
2578
2596
  const sbWithShots = sbs.filter((n) => Array.isArray(n.data && n.data.shots) && n.data.shots.length).pop() || null;
2579
2597
  const sbEmpty = sbs[sbs.length - 1] || null; // 存在但尚未拆解镜头
2580
2598
  const hasPrompt = Boolean(String(prompt || '').trim());
2599
+ const hasAsset = Boolean(assetId);
2581
2600
  let autoConnectId = '';
2582
2601
  let forced = false;
2583
- if (sbWithShots) {
2602
+ if (!hasAsset && sbWithShots) {
2584
2603
  autoConnectId = sbWithShots.id; // 无论是否传 prompt:镜头清单只认分镜表
2585
2604
  forced = hasPrompt;
2586
- } else if (!hasPrompt && sbEmpty) {
2605
+ } else if (!hasAsset && !hasPrompt && sbEmpty) {
2587
2606
  autoConnectId = sbEmpty.id; // 分镜表存在但 shots 为空:先连线占位(生成前需先拆解镜头)
2588
2607
  }
2589
2608
  // G2/G3:建视频节点前,目标分镜表的 shots 必须过质量闸门;且一个分镜表只允许一个视频节点。
@@ -2610,8 +2629,17 @@ function canvasCreateNode(args) {
2610
2629
  }
2611
2630
  if (!g3.ok && ovOk) PROD.recordOverride(id, canvasId, 'G3', ov.quote, { node: label, storyboardId: sbTargetId });
2612
2631
  }
2613
- node.data = { ...node.data, status: 'idle', error: null, prompt: autoConnectId ? '' : prompt, assetId: null, demo: false, duration: Number(args && args.duration) || 5, resolution: (args && args.resolution) || '720P', ratio: (args && args.ratio) || '16:9' };
2632
+ node.data = { ...node.data, status: assetId ? 'done' : 'idle', error: null, prompt: hasAsset ? '' : (autoConnectId ? '' : prompt), assetId: assetId || null, demo: false, duration: Number(args && args.duration) || 5, resolution: (args && args.resolution) || '720P', ratio: (args && args.ratio) || '16:9' };
2614
2633
  autoConnected = autoConnectId ? { from: autoConnectId, ignoredPrompt: forced && hasPrompt, emptyShots: !sbWithShots && !!sbEmpty } : null;
2634
+ } else if (type === 'audio') {
2635
+ node = agentNode('text', '', pos.x, pos.y, label || '音频节点');
2636
+ node.type = 'audio';
2637
+ node.w = 360; node.h = 300;
2638
+ node.data = {
2639
+ ...node.data, status: assetId ? 'done' : 'idle', error: null, prompt: '', assetId: assetId || null,
2640
+ audioModel: '', voiceId: '', audioEmotion: 'auto', audioEmotionScale: '4', audioSpeechRate: '0', audioFormat: 'mp3',
2641
+ audioGain: 1, audioStart: 0, audioOffset: 0, audioDuration: 0, audioLoop: false, audioMute: false,
2642
+ };
2615
2643
  } else if (type === 'storyboard') {
2616
2644
  node = agentNode('text', '', pos.x, pos.y, label || '分镜表');
2617
2645
  node.type = 'storyboard';
@@ -2628,7 +2656,7 @@ function canvasCreateNode(args) {
2628
2656
  node = agentNode('text', '', pos.x, pos.y, label || '成片');
2629
2657
  node.type = 'finalcut';
2630
2658
  node.w = 360; node.h = 360;
2631
- node.data = { ...node.data, status: 'idle', error: null, resolution: (args && args.resolution) || '720P', ratio: (args && args.ratio) || '16:9', transition: 'none', progress: null, assetId: null, subs: {}, keepAudio: true };
2659
+ node.data = { ...node.data, status: assetId ? 'done' : 'idle', error: null, resolution: (args && args.resolution) || '720P', ratio: (args && args.ratio) || '16:9', transition: 'none', progress: null, assetId: assetId || null, subs: {}, keepAudio: true };
2632
2660
  } else if (type === 'script') {
2633
2661
  node = agentNode('script', prompt, pos.x, pos.y, label || defaultLabel(prompt));
2634
2662
  } else {
@@ -3157,7 +3185,7 @@ async function canvasNodeStatus(args) {
3157
3185
  };
3158
3186
  }
3159
3187
  /** 可被 canvas_update_node 修改的节点字段白名单(结构字段 id/type/x/y/w/h/data 一律不可写)。 */
3160
- const NODE_PATCH_FIELDS = ['prompt', 'idea', 'output', 'text', 'script', 'requirement', 'label', 'ratio', 'size', 'duration', 'resolution', 'frameMode', 'count', 'imageModel', 'videoModel', 'model', 'cont', 'targetDuration', 'combatMode'];
3188
+ const NODE_PATCH_FIELDS = ['prompt', 'idea', 'output', 'text', 'script', 'requirement', 'label', 'ratio', 'size', 'duration', 'resolution', 'frameMode', 'count', 'imageModel', 'videoModel', 'model', 'cont', 'targetDuration', 'combatMode', 'assetId'];
3161
3189
  /** 更新已有节点的字段:对话侧"改提示词 / 改参数"的唯一入口(此前只能删节点重建)。 */
3162
3190
  function canvasUpdateNode(args) {
3163
3191
  const pid = String((args && args.projectId) || '');
@@ -3194,10 +3222,13 @@ function canvasUpdateNode(args) {
3194
3222
  } else if (k === 'combatMode') {
3195
3223
  // 打斗判定三态覆盖:auto=按场检测(缺省)/ on=本场强制按打斗注入 / off=强制不注入
3196
3224
  v = ['auto', 'on', 'off'].includes(String(v).toLowerCase()) ? String(v).toLowerCase() : 'auto';
3225
+ } else if (k === 'assetId') {
3226
+ v = String(v == null ? '' : v).trim() || null;
3197
3227
  } else {
3198
3228
  v = String(v == null ? '' : v);
3199
3229
  }
3200
3230
  n.data[k] = v;
3231
+ if (k === 'assetId' && v) { n.data.status = 'done'; n.data.error = null; n.data.demo = false; }
3201
3232
  applied.push(k);
3202
3233
  }
3203
3234
  if (patch.shots !== undefined) {
@@ -3404,10 +3435,11 @@ const PORT_TABLE = {
3404
3435
  'storyboard': { inputs: [{ id: 'prev', kind: 'text' }, { id: 'script', kind: 'text' }], outputs: [{ id: 'out', kind: 'text' }, { id: 'tail', kind: 'text' }] },
3405
3436
  'image': { inputs: [{ id: 'prompt', kind: 'text' }, { id: 'ref', kind: 'image' }], outputs: [{ id: 'out', kind: 'image' }] },
3406
3437
  'video': { inputs: [{ id: 'prompt', kind: 'text' }, { id: 'frame', kind: 'image' }, { id: 'ref', kind: 'image' }], outputs: [{ id: 'out', kind: 'video' }, { id: 'tailpick', kind: 'image' }, { id: 'last', kind: 'image' }] },
3407
- 'finalcut': { inputs: [{ id: 'in', kind: 'video' }], outputs: [] },
3438
+ 'finalcut': { inputs: [{ id: 'in', kind: 'video' }, { id: 'audio', kind: 'audio' }], outputs: [] },
3439
+ 'audio': { inputs: [{ id: 'prompt', kind: 'text' }], outputs: [{ id: 'out', kind: 'audio' }] },
3408
3440
  'group': { inputs: [], outputs: [] },
3409
3441
  };
3410
- const MULTI_INPUT_PORTS = new Set(['ref', 'char', 'scene', 'prop']);
3442
+ const MULTI_INPUT_PORTS = new Set(['ref', 'char', 'scene', 'prop', 'audio']);
3411
3443
  /** Agent 改动了项目文档:记一次性信号,让画布在几秒内拉回合并(否则画布上看不到 Agent 的改动)。
3412
3444
  * canvasId 记录是哪个画布被改(缺省/默认画布 = null),客户端只刷新当前画布。 */
3413
3445
  function markDocChanged(projectId, canvasId) {
@@ -3445,7 +3477,7 @@ function connectEdge(doc, fromId, toId, fromPort, toPort) {
3445
3477
  else if (from.type === 'image' || from.type === 'image-asset' || from.type === 'image-gen') tp = 'ref';
3446
3478
  else tp = 'prompt';
3447
3479
  } else if (to.type === 'finalcut') {
3448
- tp = 'in';
3480
+ tp = from.type === 'audio' ? 'audio' : 'in';
3449
3481
  } else {
3450
3482
  tp = String((ins[0] && ins[0].id) || '');
3451
3483
  }
@@ -3720,6 +3752,11 @@ function resolveModel(providers, want) {
3720
3752
  return { provider: provider.id, model: model.id };
3721
3753
  }
3722
3754
 
3755
+ /** 3D-BOX stage id → 文件系统安全名(空值表示旧版单文件入口)。 */
3756
+ function safeStageId(id) {
3757
+ return String(id || '').replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 80);
3758
+ }
3759
+
3723
3760
  export function apply(ctx) {
3724
3761
  ctx.effect(() => {
3725
3762
  const llm = () => ctx.get('llm');
@@ -3781,7 +3818,35 @@ export function apply(ctx) {
3781
3818
  ...(temperature === undefined ? {} : { temperature }),
3782
3819
  ...(maxTokens === undefined ? {} : { maxTokens }),
3783
3820
  };
3784
- const userMessage = { role: 'user', content: [{ type: 'text', text: user }] };
3821
+ // 视觉输入(拉片):把 dataURL 存成宿主附件,作为 image content block 发给模型。
3822
+ // 模型不支持图片输入时适配器会报错——调用方据此提示用户换视觉模型,绝不静默丢图。
3823
+ const imageBlocks = [];
3824
+ const wantImages = Array.isArray(want.images) ? want.images.slice(0, 16) : [];
3825
+ if (wantImages.length) {
3826
+ let attachments = null;
3827
+ try { attachments = (ctx.get && ctx.get('attachments')) || ctx.attachments || null; } catch { attachments = null; }
3828
+ if (!attachments || typeof attachments.saveImage !== 'function') {
3829
+ throw new Error('当前宿主不支持图片附件(attachments),无法做视觉拉片');
3830
+ }
3831
+ for (let i = 0; i < wantImages.length; i++) {
3832
+ const im = wantImages[i] || {};
3833
+ const raw = String(im.dataUrl || '');
3834
+ const m = /^data:([^;]+);base64,([\s\S]+)$/.exec(raw);
3835
+ let data = null;
3836
+ let mediaType = String(im.mediaType || 'image/jpeg');
3837
+ if (m) {
3838
+ mediaType = m[1];
3839
+ data = Buffer.from(m[2], 'base64');
3840
+ } else if (im.data && (im.data instanceof Uint8Array || Buffer.isBuffer(im.data))) {
3841
+ data = im.data;
3842
+ }
3843
+ if (!data || !data.length) continue;
3844
+ const ref = await attachments.saveImage({ data, mediaType, name: String(im.name || ('frame-' + (i + 1) + '.jpg')) });
3845
+ if (ref) imageBlocks.push({ type: 'image', attachment: ref });
3846
+ }
3847
+ if (!imageBlocks.length) throw new Error('拉片图片解码失败(没有可用的帧)');
3848
+ }
3849
+ const userMessage = { role: 'user', content: [{ type: 'text', text: user }, ...imageBlocks] };
3785
3850
  /** 跑一次流:统计 chunk 类型,返回文本与失败原因(单次尝试抛错不致命,便于逐级回退)。 */
3786
3851
  const run = async (options, label) => {
3787
3852
  const kinds = [];
@@ -3920,6 +3985,347 @@ export function apply(ctx) {
3920
3985
  }
3921
3986
  /** needJson 失败局部重试指令:追加在 user 尾部,测试以「只输出 JSON」识别重试轮。 */
3922
3987
  const AUTO_JSON_RETRY = '\n\n【格式重试】上一轮输出无法解析为 JSON。只输出 JSON 对象本身:不要任何解释文字、不要 markdown 代码围栏、不要前后缀。';
3988
+ /** 3D-BOX 道具图元白名单:prompt、AI 输出清洗、引擎构建三处共用一份,避免拼错 preset。 */
3989
+ const DIRECTOR_PROP_PRESETS = ['box', 'sphere', 'cylinder', 'cone', 'plane', 'capsule', 'torus', 'pyramid', 'disc', 'arc', 'half_cylinder', 'ancient_house', 'ancient_stall', 'stone_step'];
3990
+ /** 3D-BOX 自然语言改景:把当前 spec + 指令交给用户已配置的模型,返回完整可构建 spec。 */
3991
+ const DIRECTOR_EDIT_SYSTEM = [
3992
+ 'You are a 3D previs scene editor. Modify the CURRENT SPEC according to the INSTRUCTION.',
3993
+ 'Return ONLY one JSON object, no prose, no markdown fences.',
3994
+ 'Keep the same schema: { title, duration, env, style, models[], characters[], props[], cameras[], shots[], shot }',
3995
+ 'Camera: { name, pos:[x,y,z], target:[x,y,z], fov, keys?[] }',
3996
+ 'Shot: { camera, start, end }',
3997
+ 'Character: { name, pos:[x,y,z], rotY, pose, color, path?, keys?, poseAnim? }',
3998
+ 'Model asset: { assetId, pos:[x,y,z], rotY, h } — only ids already present in the CURRENT SPEC.',
3999
+ 'Prop: { name, preset, pos, scale, color } or { name, pos, parts:[{preset,pos,scale,color,rotY?,rx?,rz?,array?,mirror?}] }.',
4000
+ 'Attachment: { attach:{ to:"character id or name", bone:"RightHand", offset:[x,y,z], rotY? } } on a prop/model.',
4001
+ 'Valid pose ids: none, stand, arms_up, punch, kick, flip, fly, crouch, fight, walk, run, jump, hit, fall, walk_a, walk_b, run_a, run_b.',
4002
+ 'Preserve all fields the instruction does not mention. Keep cameras/shots non-empty. Use meters and seconds.',
4003
+ 'Examples: "机位低一点" -> lower camera pos.y and target.y; "让 A 从左入画" -> move A pos.x negative; "加一张桌子" -> add a furn_* model or a parts prop; "手持" -> set shot.cameraMove="handheld".',
4004
+ ].join('\n');
4005
+ /** 3D-BOX 一句话搭景:LLM 生成完整 spec(动作/道具/机位/分镜),本地模板只做 schema 骨架与失败兜底。 */
4006
+ const DIRECTOR_BRIEF_SYSTEM = [
4007
+ 'You are a 3D previs director. Turn a one-line brief into a complete, playable previz spec.',
4008
+ 'Return ONLY one JSON object, no prose, no markdown fences.',
4009
+ 'Schema: { title, duration, env:{preset,grid,tonemap,bg?}, style?, models[], characters[], props[], cameras[], shots[], shot:{duration,cameraMove}, lights[], fog?, dof? }',
4010
+ 'Character: { name, pos:[x,y,z], rotY, color, clip, pose, path?, pathFace?, locomotion?, poseAnim? }',
4011
+ 'poseAnim: [{"t":0,"pose":"idle"},{"t":1.2,"pose":"punch"},{"t":2.4,"pose":"hit"}] — use it for fights/actions.',
4012
+ 'path: [[x,z],...] + locomotion:{source:"clip",clip:"run",stride:1.1,cycle:0.8,refSpeed:4} for chase/walk movement.',
4013
+ 'Camera: { name, pos:[x,y,z], target:[x,y,z], fov, keys?[] } — keys are [{t,pos,target,fov,ease}] for camera movement.',
4014
+ 'Shot: { camera, start, end } — shots must cover 0..duration without gaps.',
4015
+ 'Prop: { name, preset, pos:[x,y,z], scale:[x,y,z], color }',
4016
+ 'Model asset: { assetId, name?, pos:[x,y,z], rotY, h, scale?, color? } — place real local GLB assets.',
4017
+ 'Parametric prop: { name, pos:[x,y,z], rotY?, parts:[{preset,pos:[x,y,z],scale:[x,y,z],color?,rotY?,rx?,rz?}] } — build cars/weapons/signs/lanterns from 2-12 primitives when no local asset fits. part.pos is the bottom-center offset; scale is [width,height,depth].',
4018
+ 'Repeat/mirror/scatter: part or model { array:{count,offset:[dx,dy,dz],rotY?} } | { mirror:"x"|"z"|"xz" } | model/part { scatter:{count,radius,seed?} }. Use them for fences, windows, wheels, street rows and forests instead of listing every copy. Caps: 24 per part array/scatter, 40 per model array/scatter.',
4019
+ 'Bone attachment: prop/model { attach:{ to:"character id or name", bone:"...", offset:[x,y,z], rotY?, rx?, rz?, scale? } } — weapons, hats and backpacks follow the skeleton with full bone rotation (incl. pitch); do not animate their position separately. Common bones: Hips, Spine, Spine1, Spine2, Neck, Head, LeftShoulder, LeftArm, LeftForeArm, LeftHand, RightShoulder, RightArm, RightForeArm, RightHand, LeftUpLeg, LeftLeg, LeftFoot, RightUpLeg, RightLeg, RightFoot.',
4020
+ 'Valid prop presets: ' + DIRECTOR_PROP_PRESETS.join(', ') + '.',
4021
+ 'Valid pose ids: none, stand, arms_up, punch, kick, flip, fly, crouch, fight, walk, run, jump, hit, fall, walk_a, walk_b, run_a, run_b.',
4022
+ 'env.preset: studio | daylight | night. Use meters and seconds.',
4023
+ 'Rules: read the brief literally and cast 2–6 characters, 2–6 props, 2–5 cameras, 3–6 shots. Build a recognizable set using models[] from AVAILABLE LOCAL ASSETS first (buildings/furniture/trees/people); use parts props only for gaps (cars, weapons, small props).',
4024
+ 'Never invent assetId values. Street: use bld_*/tree_* plus a road plane; room: use furn_*; fight: use a ground/arena plus human models.',
4025
+ 'Action words must become poseAnim/path/locomotion, not just labels. A chase needs running paths and a moving camera; a fight needs punch/kick/hit beats; dialogue needs idle poses and over-shoulder cameras.',
4026
+ 'The BASE SKELETON is only a valid fallback shape; replace or extend its arrays with your own detailed staging. Never return empty cameras or shots.',
4027
+ ].join('\n');
4028
+ /** 本地 GLB 资产目录:brief 与 edit 共用一份。 */
4029
+ function loadDirectorCatalog() {
4030
+ try {
4031
+ const j = JSON.parse(readFileSync(new URL('./assets/director/models/index.json', import.meta.url), 'utf8'));
4032
+ return Array.isArray(j.models) ? j.models : [];
4033
+ } catch { return []; }
4034
+ }
4035
+ /** models[].assetId 只接受本地目录内的 id:编造 id 直接过滤,高度按目录补齐。 */
4036
+ function sanitizeDirectorModels(spec, catalogMap) {
4037
+ if (!spec || !Array.isArray(spec.models)) return spec;
4038
+ spec.models = spec.models.map((m) => {
4039
+ if (!m || typeof m !== 'object') return null;
4040
+ const id = String(m.assetId || '');
4041
+ const meta = catalogMap.get(id);
4042
+ if (!meta) return null;
4043
+ return { ...m, assetId: id, h: Number(m.h) > 0 ? Number(m.h) : Number(meta.height) || 1.7 };
4044
+ }).filter(Boolean);
4045
+ return spec;
4046
+ }
4047
+ /** AI 输出的 props[].parts 清洗:白名单 preset + 限制数量,坏数据不写盘、不撑爆渲染。 */
4048
+ function normalizeDirectorParts(spec) {
4049
+ if (!spec || !Array.isArray(spec.props)) return spec;
4050
+ spec.props = spec.props.filter((p) => p && typeof p === 'object').map((p) => {
4051
+ if (!Array.isArray(p.parts) || !p.parts.length) return p;
4052
+ const parts = p.parts.filter((pt) => pt && typeof pt === 'object').slice(0, 40).map((pt) => ({
4053
+ ...pt,
4054
+ preset: DIRECTOR_PROP_PRESETS.includes(String(pt.preset)) ? String(pt.preset) : 'box',
4055
+ }));
4056
+ return parts.length ? { ...p, parts } : p;
4057
+ });
4058
+ return spec;
4059
+ }
4060
+ function mergeDirectorSpec(base, patch) {
4061
+ if (Array.isArray(patch)) return patch;
4062
+ if (patch && typeof patch === 'object') {
4063
+ const out = (base && typeof base === 'object' && !Array.isArray(base)) ? { ...base } : {};
4064
+ for (const [k, v] of Object.entries(patch)) out[k] = mergeDirectorSpec(out[k], v);
4065
+ return out;
4066
+ }
4067
+ return patch === undefined ? base : patch;
4068
+ }
4069
+ /** 动作语义兜底:模型偶尔只给“站着”的角色;按 brief 的关键词补路径/姿态关键帧,
4070
+ * 保证“追车/打斗”至少能被预演出来(不替代 AI 的完整编排,只补最低可演要求)。 */
4071
+ function enrichDirectorSpec(spec, brief) {
4072
+ if (!spec || typeof spec !== 'object') return spec;
4073
+ const text = String(brief || '').toLowerCase();
4074
+ const chase = /追|跑|逃|追逐|警匪|chase|run|escape|pursuit/i.test(text);
4075
+ const fight = /打|斗|拳|踢|击|fight|punch|kick|combat|battle/i.test(text);
4076
+ const chars = Array.isArray(spec.characters) ? spec.characters : [];
4077
+ if (!Array.isArray(spec.props) || !spec.props.length) spec.props = [{ name: '地面', preset: 'plane', pos: [0, 0, 0], scale: [16, 1, 12], color: '#9aa3ad' }];
4078
+ // AI 失败兜底也要有"能演"的道具:追车类 brief 至少给一台参数化警车(AI 成功时它已给 parts/车,则跳过)
4079
+ const wantsCar = /警车|警匪|追车|追逐|car|police|chase/i.test(text);
4080
+ const hasCarProp = spec.props.some((p) => p && (Array.isArray(p.parts) || /车|car/i.test(String(p.name || ''))));
4081
+ if (wantsCar && !hasCarProp) {
4082
+ spec.props.push({
4083
+ name: '警车', pos: [2.5, 0, 1],
4084
+ parts: [
4085
+ { preset: 'box', pos: [0, 0.4, 0], scale: [4, 0.7, 1.8], color: '#15181d' },
4086
+ { preset: 'box', pos: [-0.3, 1.1, 0], scale: [2, 0.6, 1.6], color: '#2f6fed' },
4087
+ { preset: 'box', pos: [-0.3, 1.45, 0], scale: [0.7, 0.16, 0.4], color: '#ff2d2d' },
4088
+ { preset: 'cylinder', pos: [1.3, 0.3, 0.95], scale: [0.6, 0.6, 0.6], color: '#0b0b0d', mirror: 'x' },
4089
+ { preset: 'cylinder', pos: [-1.3, 0.3, -0.95], scale: [0.6, 0.6, 0.6], color: '#0b0b0d', mirror: 'x' },
4090
+ ],
4091
+ });
4092
+ }
4093
+ const duration = Math.max(0.5, Number(spec.duration) || 6);
4094
+ if (Array.isArray(spec.shots) && spec.shots.length) {
4095
+ const shots = spec.shots.slice().sort((a, b) => (Number(a.start) || 0) - (Number(b.start) || 0));
4096
+ shots[0].start = 0;
4097
+ shots[shots.length - 1].end = duration;
4098
+ spec.shots = shots;
4099
+ }
4100
+ if (chase) {
4101
+ chars.slice(0, 2).forEach((c, i) => {
4102
+ if (!c || typeof c !== 'object') return;
4103
+ if (!Array.isArray(c.path) || c.path.length < 2) c.path = [[Number(c.pos?.[0]) || (i ? 1.2 : -1.2), Number(c.pos?.[2]) || 0], [8 + i * 1.5, 0]];
4104
+ if (!c.clip || c.clip === 'idle') c.clip = 'run';
4105
+ c.pathFace = c.pathFace !== false;
4106
+ if (!c.locomotion || typeof c.locomotion !== 'object') c.locomotion = { source: 'clip', clip: 'run', stride: 1.1, cycle: 0.8, refSpeed: 4 };
4107
+ });
4108
+ }
4109
+ if (fight) {
4110
+ const hasBeat = chars.some((c) => Array.isArray(c && c.poseAnim) && c.poseAnim.length);
4111
+ if (!hasBeat) {
4112
+ chars.slice(0, 2).forEach((c, i) => {
4113
+ if (!c || typeof c !== 'object') return;
4114
+ c.clip = c.clip || 'fight';
4115
+ c.poseAnim = i === 0
4116
+ ? [{ t: 0, pose: 'idle' }, { t: 0.5, pose: 'punch' }, { t: 1.1, pose: 'hit' }, { t: 1.6, pose: 'fight' }]
4117
+ : [{ t: 0, pose: 'fight' }, { t: 0.6, pose: 'kick' }, { t: 1.2, pose: 'hit' }, { t: 1.8, pose: 'idle' }];
4118
+ });
4119
+ }
4120
+ }
4121
+ return spec;
4122
+ }
4123
+ async function briefDirectorSpec(brief, opts) {
4124
+ const text = String(brief || '').trim();
4125
+ if (!text) throw new Error('需要 brief');
4126
+ const eng = await loadDirectorEngine();
4127
+ if (!eng || typeof eng.buildStageFromSpec !== 'function' || typeof eng.specFromBrief !== 'function') throw new Error('引擎未就绪');
4128
+ const o = opts || {};
4129
+ const seed = eng.specFromBrief(text, o);
4130
+ const catalog = loadDirectorCatalog();
4131
+ const catalogMap = new Map(catalog.map((m) => [String(m.id), m]));
4132
+ const catalogText = catalog.map((m) => m.id + ' | ' + (m.label || '') + ' | ' + (m.category || '') + ' | h=' + (m.height || '')).join('\n');
4133
+ const call = (o && typeof o.llm === 'function') ? o.llm : roomLlm;
4134
+ const hints = [
4135
+ o.scene && o.scene !== 'auto' ? 'SCENE PRESET: ' + o.scene : '',
4136
+ o.style && o.style !== 'auto' ? 'STYLE PRESET: ' + o.style : '',
4137
+ o.cameraMove && o.cameraMove !== 'auto' ? 'CAMERA PRESET: ' + o.cameraMove : '',
4138
+ ].filter(Boolean).join('\n');
4139
+ const baseUser = 'BRIEF:\n' + text + (hints ? '\n\n' + hints : '')
4140
+ + '\n\nAVAILABLE LOCAL ASSETS (use models[]; ids are exact):\n' + catalogText
4141
+ + '\n\nBASE SKELETON JSON:\n' + JSON.stringify(seed);
4142
+ let lastErr = null;
4143
+ for (let attempt = 0; attempt < 2; attempt++) {
4144
+ const user = attempt === 0
4145
+ ? baseUser
4146
+ : baseUser + '\n\n【修正】上一轮返回不可用:' + String((lastErr && lastErr.message) || lastErr) + '\n只输出修正后的完整 JSON 对象。';
4147
+ const raw = await call({ system: DIRECTOR_BRIEF_SYSTEM, user });
4148
+ const parsed = roomJson(raw);
4149
+ if (!parsed || typeof parsed !== 'object') { lastErr = new Error('模型没有返回可解析的 JSON'); continue; }
4150
+ const ai = (parsed.spec && typeof parsed.spec === 'object') ? parsed.spec : parsed;
4151
+ const next = enrichDirectorSpec(mergeDirectorSpec(seed, ai), text);
4152
+ // 本地资产兜底:AI/模板都漏掉 models 时,按题材补建筑/家具/树,避免只给一块地面。
4153
+ if (!Array.isArray(next.models) || !next.models.length) {
4154
+ const t = String(text || '').toLowerCase();
4155
+ if (/街|城|追|警|巷|road|street|city|chase|alley/i.test(t) && catalogMap.has('bld_a')) {
4156
+ next.models = [
4157
+ { assetId: 'bld_a', pos: [-10, 0, -8], h: catalogMap.get('bld_a').height || 12 },
4158
+ { assetId: 'bld_b', pos: [10, 0, -8], h: catalogMap.get('bld_b').height || 15 },
4159
+ { assetId: 'tree_oak', pos: [6, 0, -4], h: catalogMap.get('tree_oak').height || 7 },
4160
+ ];
4161
+ } else if (/会|室|桌|家|office|room|meeting|indoor/i.test(t) && catalogMap.has('furn_tableCross')) {
4162
+ next.models = [
4163
+ { assetId: 'furn_tableCross', pos: [0, 0, 0], h: catalogMap.get('furn_tableCross').height || 0.75 },
4164
+ { assetId: 'furn_chair', pos: [-1.2, 0, 0.8], h: catalogMap.get('furn_chair').height || 0.95 },
4165
+ { assetId: 'furn_chair', pos: [1.2, 0, 0.8], h: catalogMap.get('furn_chair').height || 0.95 },
4166
+ ];
4167
+ }
4168
+ }
4169
+ // 资产 id 校验 + 缺省高度(模型必须来自本地目录,绝不接受编造 id)
4170
+ sanitizeDirectorModels(next, catalogMap);
4171
+ normalizeDirectorParts(next);
4172
+ const built = eng.buildStageFromSpec(next);
4173
+ if (!built || !Array.isArray(built.cameras) || !built.cameras.length || !Array.isArray(built.shots) || !built.shots.length) {
4174
+ lastErr = new Error('返回的规格缺机位/分镜,无法构建');
4175
+ continue;
4176
+ }
4177
+ return { spec: next, built, source: 'llm' };
4178
+ }
4179
+ throw lastErr || new Error('AI 搭景失败');
4180
+ }
4181
+ async function editDirectorSpec(currentSpec, instruction, opts) {
4182
+ const text = String(instruction || '').trim();
4183
+ if (!text) throw new Error('需要 instruction');
4184
+ if (!currentSpec || typeof currentSpec !== 'object') throw new Error('缺少当前场景规格');
4185
+ const eng = await loadDirectorEngine();
4186
+ if (!eng || typeof eng.buildStageFromSpec !== 'function') throw new Error('引擎未就绪');
4187
+ const catalogMap = new Map(loadDirectorCatalog().map((m) => [String(m.id), m]));
4188
+ const call = (opts && typeof opts.llm === 'function') ? opts.llm : roomLlm;
4189
+ const baseUser = 'CURRENT SPEC JSON:\n' + JSON.stringify(currentSpec) + '\n\nINSTRUCTION:\n' + text;
4190
+ let lastErr = null;
4191
+ for (let attempt = 0; attempt < 2; attempt++) {
4192
+ const user = attempt === 0
4193
+ ? baseUser
4194
+ : baseUser + '\n\n【修正】上一轮返回不可用:' + String((lastErr && lastErr.message) || lastErr) + '\n只输出修正后的完整 JSON 对象。';
4195
+ const raw = await call({ system: DIRECTOR_EDIT_SYSTEM, user });
4196
+ const parsed = roomJson(raw);
4197
+ if (!parsed || typeof parsed !== 'object') { lastErr = new Error('模型没有返回可解析的 JSON'); continue; }
4198
+ const next = (parsed.spec && typeof parsed.spec === 'object') ? parsed.spec : parsed;
4199
+ sanitizeDirectorModels(next, catalogMap);
4200
+ normalizeDirectorParts(next);
4201
+ const built = eng.buildStageFromSpec(next);
4202
+ if (!built || !Array.isArray(built.cameras) || !built.cameras.length || !Array.isArray(built.shots) || !built.shots.length) {
4203
+ lastErr = new Error('返回的规格缺机位/分镜,无法构建');
4204
+ continue;
4205
+ }
4206
+ return { ok: true, spec: next, built };
4207
+ }
4208
+ throw lastErr || new Error('改景失败');
4209
+ }
4210
+ /** AI 运镜:自然语言 → 机位关键帧 JSON,校验后写回 spec。纯函数只做校验/归一,模型调用可注入。 */
4211
+ const DIRECTOR_CAMERA_SYSTEM = [
4212
+ 'You are a 3D previs camera planner. Given the CURRENT SPEC and a CAMERA INSTRUCTION, return camera keyframes.',
4213
+ 'Return ONLY one JSON object: {"keys":[{"t":0,"pos":[x,y,z],"target":[x,y,z],"fov":42,"ease":"auto"}]}',
4214
+ 'Rules: at least 2 keys; t in seconds within the scene duration; pos/target in meters; keep the subject in frame; y between 0.3 and 6; fov between 20 and 90; use ease "auto" or "linear".',
4215
+ 'Use the current camera as the starting point. Do not change characters, props or shots; only return camera keys.',
4216
+ 'Examples: "环绕香水瓶,低机位,最后推近 logo" -> keys around the subject with y low, ending closer and narrower; "手持跟拍" -> small jitter keys; "升镜" -> pos.y increases.',
4217
+ ].join('\n');
4218
+ async function planCameraMotion(currentSpec, prompt, opts) {
4219
+ const text = String(prompt || '').trim();
4220
+ if (!text) throw new Error('需要 prompt');
4221
+ if (!currentSpec || typeof currentSpec !== 'object') throw new Error('缺少当前场景规格');
4222
+ const eng = await loadDirectorEngine();
4223
+ if (!eng || typeof eng.buildStageFromSpec !== 'function') throw new Error('引擎未就绪');
4224
+ const call = (opts && typeof opts.llm === 'function') ? opts.llm : roomLlm;
4225
+ const duration = Math.max(0.5, Number(currentSpec.duration) || 5);
4226
+ const baseUser = 'CURRENT SPEC JSON:\n' + JSON.stringify(currentSpec) + '\n\nCAMERA INSTRUCTION:\n' + text;
4227
+ const asArr = (v) => {
4228
+ if (Array.isArray(v) && v.length >= 3) return [Number(v[0]) || 0, Number(v[1]) || 0, Number(v[2]) || 0];
4229
+ if (v && typeof v === 'object') return [Number(v.x) || 0, Number(v.y) || 0, Number(v.z) || 0];
4230
+ return null;
4231
+ };
4232
+ let lastErr = null;
4233
+ for (let attempt = 0; attempt < 2; attempt++) {
4234
+ const user = attempt === 0
4235
+ ? baseUser
4236
+ : baseUser + '\n\n【修正】上一轮返回不可用:' + String((lastErr && lastErr.message) || lastErr) + '\n只输出修正后的 {"keys":[...]} JSON。';
4237
+ const raw = await call({ system: DIRECTOR_CAMERA_SYSTEM, user });
4238
+ const parsed = roomJson(raw);
4239
+ const keysRaw = parsed && Array.isArray(parsed.keys) ? parsed.keys
4240
+ : (parsed && parsed.camera && Array.isArray(parsed.camera.keys) ? parsed.camera.keys : null);
4241
+ if (!Array.isArray(keysRaw) || keysRaw.length < 2) { lastErr = new Error('机位关键帧不足 2 个'); continue; }
4242
+ const norm = [];
4243
+ let bad = false;
4244
+ for (const k of keysRaw) {
4245
+ const t = Number(k && k.t);
4246
+ const pos = asArr(k && k.pos);
4247
+ const target = asArr(k && k.target);
4248
+ if (!Number.isFinite(t) || !pos || !target) { bad = true; break; }
4249
+ norm.push({
4250
+ t: Math.max(0, Math.min(duration * 1.5, t)),
4251
+ pos: [pos[0], Math.max(0.3, Math.min(6, pos[1])), pos[2]],
4252
+ target,
4253
+ fov: Math.max(8, Math.min(140, Number(k.fov) || 42)),
4254
+ ease: String(k.ease || 'auto'),
4255
+ });
4256
+ }
4257
+ if (bad) { lastErr = new Error('关键帧字段不完整'); continue; }
4258
+ norm.sort((a, b) => a.t - b.t);
4259
+ if (norm.some((k) => Math.hypot(k.pos[0] - k.target[0], k.pos[2] - k.target[2]) > 60)) {
4260
+ lastErr = new Error('机位离主体过远');
4261
+ continue;
4262
+ }
4263
+ const out = JSON.parse(JSON.stringify(currentSpec));
4264
+ const cams = Array.isArray(out.cameras) ? out.cameras : [];
4265
+ if (!cams.length) throw new Error('当前规格没有机位');
4266
+ let cam = cams[0];
4267
+ if (opts && opts.camera) cam = cams.find((c) => c.id === opts.camera || c.name === opts.camera) || cam;
4268
+ cam.keys = norm;
4269
+ out.cameraPrompt = text;
4270
+ out.cameraMove = 'ai';
4271
+ out.shot = out.shot || {};
4272
+ out.shot.cameraMove = 'ai';
4273
+ const built = eng.buildStageFromSpec(out);
4274
+ if (!built || !Array.isArray(built.cameras) || !built.cameras.length) { lastErr = new Error('机位关键帧无法构建'); continue; }
4275
+ return { ok: true, spec: out, keys: norm };
4276
+ }
4277
+ throw lastErr || new Error('AI 运镜失败');
4278
+ }
4279
+ /** Agent 素材导入:本地绝对路径或 http(s) URL → 项目资产目录 + 资产库。 */
4280
+ const importAsset = async (args) => {
4281
+ const pid = String((args && args.projectId) || '');
4282
+ if (!pid) throw new Error('需要 projectId');
4283
+ const localPath = String((args && args.path) || '').trim();
4284
+ const url = String((args && args.url) || '').trim();
4285
+ if (!localPath && !url) throw new Error('需要 path 或 url');
4286
+ const root = dataRoot();
4287
+ if (!root) throw new Error('未设置数据根目录:请先在画布设置里配置数据根目录');
4288
+ const doc = readDocOrNull(pid);
4289
+ const projectName = (doc && doc.name) || pid;
4290
+ const folder = ensureProjectFolder(pid, projectName);
4291
+ if (!folder) throw new Error('项目目录不可用');
4292
+ let buf = null, srcName = '', respMime = '';
4293
+ if (localPath) {
4294
+ const abs = path.resolve(localPath);
4295
+ if (!existsSync(abs)) throw new Error('文件不存在: ' + abs);
4296
+ const st = statSync(abs);
4297
+ if (!st.isFile()) throw new Error('不是文件: ' + abs);
4298
+ if (st.size > 200 * 1024 * 1024) throw new Error('文件超过 200MB 上限');
4299
+ buf = readFileSync(abs);
4300
+ srcName = path.basename(abs);
4301
+ } else {
4302
+ const r = await fetchProxyForward({ url, method: 'GET', timeoutMs: 120000 });
4303
+ if (!r || r.status >= 400) throw new Error('下载失败 HTTP ' + (r && r.status));
4304
+ buf = r.buf;
4305
+ respMime = String(r.contentType || '').split(';')[0].trim();
4306
+ try { srcName = decodeURIComponent(path.basename(new URL(url).pathname)) || 'asset'; } catch { srcName = 'asset'; }
4307
+ }
4308
+ if (!buf || !buf.length) throw new Error('文件为空');
4309
+ const mime = (respMime && respMime.includes('/')) ? respMime : mimeOf(srcName);
4310
+ const kindArg = String((args && args.kind) || '').toLowerCase();
4311
+ const kind = ['image', 'video', 'audio'].includes(kindArg) ? kindArg
4312
+ : mime.startsWith('image/') ? 'image' : mime.startsWith('video/') ? 'video' : mime.startsWith('audio/') ? 'audio' : 'general';
4313
+ const extMatch = /\.([a-z0-9]{1,8})$/i.exec(srcName);
4314
+ const ext = (extMatch && extMatch[1]) || (mime.split('/')[1] || 'bin').replace(/[^a-z0-9]/gi, '').slice(0, 8) || 'bin';
4315
+ const id = 'a_' + Date.now().toString(36) + '_' + Math.random().toString(36).slice(2, 8);
4316
+ const file = id + '.' + ext.toLowerCase();
4317
+ const rel = 'projects/' + pid + '/' + kind + '/' + file;
4318
+ const phys = physicalRel(rel);
4319
+ if (!phys) throw new Error('资产路径解析失败');
4320
+ const absOut = path.join(root, phys);
4321
+ mkdirSync(path.dirname(absOut), { recursive: true });
4322
+ writeFileSync(absOut, buf);
4323
+ const name = String((args && args.name) || srcName || ('import-' + id)).slice(0, 80);
4324
+ const asset = { id, projectId: pid, kind, mime, name, rel, size: buf.length, createdAt: Date.now(), source: 'import', category: 'general' };
4325
+ const saved = await methods.assetCreate({ asset, projectName });
4326
+ if (!saved || !saved.ok) throw new Error((saved && saved.error) || '资产入库失败');
4327
+ return { ok: true, assetId: id, kind, name, mime, size: buf.length, rel };
4328
+ };
3923
4329
  async function autoRoomStep(run, role, system, user, needJson) {
3924
4330
  if (run.status !== 'running') throw new Error('aborted');
3925
4331
  const raw = await roomLlm({ system, user });
@@ -4428,6 +4834,8 @@ export function apply(ctx) {
4428
4834
  __autoApi.runs = AUTO_RUNS;
4429
4835
  __autoApi.setRoomLlm = (fn) => { roomLlm = fn; };
4430
4836
  __autoApi.setCanvasRunner = (fn) => { canvasExec = fn; };
4837
+ __autoApi.editDirectorSpec = editDirectorSpec;
4838
+ __autoApi.planCameraMotion = planCameraMotion;
4431
4839
 
4432
4840
  const methods = {
4433
4841
  async ping() { return { ok: true, version: VERSION }; },
@@ -4488,29 +4896,173 @@ export function apply(ctx) {
4488
4896
  * 客户端知道自己要删哪个项目,Host 按项目物理文件夹定位(与 projectDirGet 同一基准),
4489
4897
  * 只删 projects/<folder>/craft 这一个子目录——绝不碰项目镜像、其他项目、数据根其他内容。
4490
4898
  * 幂等:目录不存在也是成功;失败可见(removed=false + reason),不静默吞。 */
4491
- /** 导演台场景规格:AI / 脚本写入,客户端打开导演台时自动套用(存 <projectDir>/craft/director-stage.json)。 */
4899
+ /** 3D-BOX stage id:文件系统安全 + 稳定(一个 stage = 一条库记录)。 */
4492
4900
  async directorStageSave(args) {
4493
4901
  const dir = await methods.projectDirGet({ projectId: args && args.projectId });
4494
4902
  if (!dir || !dir.ok) return { ok: false, error: (dir && dir.error) || '项目目录不可用' };
4495
4903
  const craftDir = path.join(dir.abs, 'craft');
4496
- try { mkdirSync(craftDir, { recursive: true }); } catch { /* 交给写入报错 */ }
4497
- const file = path.join(craftDir, 'director-stage.json');
4904
+ const stageId = safeStageId(args && args.stageId);
4498
4905
  const spec = (args && args.spec) || null;
4499
4906
  if (!spec || typeof spec !== 'object') return { ok: false, error: '缺少 spec 对象' };
4500
- const payload = { ok: true, savedAt: Date.now(), spec };
4501
- try { writeFileSync(file, JSON.stringify(payload, null, 2), 'utf8'); }
4502
- catch (e) { return { ok: false, error: '写入失败: ' + String((e && e.message) || e) }; }
4503
- return { ok: true, projectId: String(args.projectId), file, bytes: Buffer.byteLength(JSON.stringify(payload)) };
4907
+ const payload = { ok: true, savedAt: Date.now(), stageId: stageId || null, spec };
4908
+ try {
4909
+ if (stageId) {
4910
+ const stageDir = path.join(craftDir, '3dbox');
4911
+ mkdirSync(stageDir, { recursive: true });
4912
+ // 旧版单文件先迁到 3dbox/,避免第一次生成新 stage 时把旧预演覆盖掉。
4913
+ const legacyFile = path.join(craftDir, 'director-stage.json');
4914
+ if (existsSync(legacyFile)) {
4915
+ try {
4916
+ const old = JSON.parse(readFileSync(legacyFile, 'utf8'));
4917
+ const oldSid = safeStageId(old.stageId);
4918
+ if (!oldSid || oldSid !== stageId) {
4919
+ const keepSid = oldSid || ('legacy_' + (Number(old.savedAt) || Date.now()));
4920
+ const keepFile = path.join(stageDir, safeStageId(keepSid) + '.json');
4921
+ if (!existsSync(keepFile)) writeFileSync(keepFile, JSON.stringify({ ...old, stageId: safeStageId(keepSid) }, null, 2), 'utf8');
4922
+ }
4923
+ } catch { /* 旧文件损坏则放弃迁移,不阻塞新保存 */ }
4924
+ }
4925
+ writeFileSync(path.join(stageDir, stageId + '.json'), JSON.stringify(payload, null, 2), 'utf8');
4926
+ }
4927
+ // 兼容旧入口:始终维护「最近一次」文件(列表会按 stageId 去重,不重复展示)
4928
+ mkdirSync(craftDir, { recursive: true });
4929
+ writeFileSync(path.join(craftDir, 'director-stage.json'), JSON.stringify(payload, null, 2), 'utf8');
4930
+ } catch (e) { return { ok: false, error: '写入失败: ' + String((e && e.message) || e) }; }
4931
+ return { ok: true, projectId: String(args.projectId), stageId: stageId || null, file: path.join(craftDir, stageId ? '3dbox/' + stageId + '.json' : 'director-stage.json'), bytes: Buffer.byteLength(JSON.stringify(payload)) };
4504
4932
  },
4505
4933
  async directorStageLoad(args) {
4506
4934
  const dir = await methods.projectDirGet({ projectId: args && args.projectId });
4507
4935
  if (!dir || !dir.ok) return { ok: false, error: (dir && dir.error) || '项目目录不可用' };
4508
- const file = path.join(dir.abs, 'craft', 'director-stage.json');
4509
- if (!existsSync(file)) return { ok: true, found: false, file };
4936
+ const stageId = safeStageId(args && args.stageId);
4937
+ const file = stageId
4938
+ ? path.join(dir.abs, 'craft', '3dbox', stageId + '.json')
4939
+ : path.join(dir.abs, 'craft', 'director-stage.json');
4940
+ if (!existsSync(file)) return { ok: true, found: false, file, stageId: stageId || null };
4510
4941
  try {
4511
4942
  const doc = JSON.parse(readFileSync(file, 'utf8'));
4512
- return { ok: true, found: true, file, savedAt: doc.savedAt || null, spec: doc.spec || null };
4513
- } catch (e) { return { ok: false, found: false, file, error: '读取失败: ' + String((e && e.message) || e) }; }
4943
+ return { ok: true, found: true, file, stageId: stageId || doc.stageId || null, savedAt: doc.savedAt || null, spec: doc.spec || null };
4944
+ } catch (e) { return { ok: false, found: false, file, stageId: stageId || null, error: '读取失败: ' + String((e && e.message) || e) }; }
4945
+ },
4946
+ async directorStageList() {
4947
+ let list = [];
4948
+ try { list = await projectsList(); } catch { list = []; }
4949
+ const items = [];
4950
+ const seen = new Set();
4951
+ for (const p of (Array.isArray(list) ? list : [])) {
4952
+ try {
4953
+ const dir = await methods.projectDirGet({ projectId: p.id });
4954
+ if (!dir || !dir.ok) continue;
4955
+ const craftDir = path.join(dir.abs, 'craft');
4956
+ const stageDir = path.join(craftDir, '3dbox');
4957
+ const files = [];
4958
+ try {
4959
+ for (const f of readdirSync(stageDir)) if (f.endsWith('.json')) files.push({ file: path.join(stageDir, f), stageId: f.slice(0, -5) });
4960
+ } catch { /* 没有目录 */ }
4961
+ files.push({ file: path.join(craftDir, 'director-stage.json'), stageId: null });
4962
+ for (const ent of files) {
4963
+ if (!existsSync(ent.file)) continue;
4964
+ let doc = null;
4965
+ try { doc = JSON.parse(readFileSync(ent.file, 'utf8')); } catch { continue; }
4966
+ const sid = safeStageId(ent.stageId || doc.stageId);
4967
+ const key = p.id + '::' + (sid || 'legacy');
4968
+ if (seen.has(key)) continue;
4969
+ seen.add(key);
4970
+ const spec = doc.spec || {};
4971
+ items.push({
4972
+ projectId: p.id, projectName: p.name || p.id, stageId: sid || null,
4973
+ title: String(spec.title || '3D-BOX'),
4974
+ savedAt: Number(doc.savedAt) || 0,
4975
+ cameras: Array.isArray(spec.cameras) ? spec.cameras.length : 0,
4976
+ shots: Array.isArray(spec.shots) ? spec.shots.length : 0,
4977
+ duration: Number(spec.duration) || 0,
4978
+ });
4979
+ }
4980
+ } catch { /* 单个项目读失败不影响列表 */ }
4981
+ }
4982
+ items.sort((a, b) => b.savedAt - a.savedAt);
4983
+ return { ok: true, multiStage: true, items };
4984
+ },
4985
+ async directorStageBrief(args) {
4986
+ const projectId = String((args && args.projectId) || '');
4987
+ const brief = String((args && args.brief) || '').trim();
4988
+ if (!brief) return { ok: false, error: '需要 brief' };
4989
+ const eng = await loadDirectorEngine();
4990
+ if (!eng || typeof eng.specFromBrief !== 'function') return { ok: false, error: '引擎未就绪' };
4991
+ const stageId = safeStageId(args && args.stageId) || ('box_' + Date.now().toString(36) + '_' + Math.random().toString(36).slice(2, 6));
4992
+ const scene = String((args && args.scene) || '');
4993
+ let spec = null;
4994
+ let source = 'llm';
4995
+ let warning = '';
4996
+ if (scene === 'blank') {
4997
+ spec = eng.specFromBrief(brief, { scene: 'blank', style: args && args.style, cameraMove: args && args.cameraMove, duration: args && args.duration });
4998
+ source = 'blank';
4999
+ } else {
5000
+ try {
5001
+ const made = await briefDirectorSpec(brief, {
5002
+ scene: args && args.scene, style: args && args.style,
5003
+ cameraMove: args && args.cameraMove, duration: args && args.duration,
5004
+ });
5005
+ spec = made.spec;
5006
+ } catch (e) {
5007
+ // 明确降级:AI 失败时仍可出图,但前端必须展示 warning(不静默假装 AI 成功)
5008
+ spec = enrichDirectorSpec(eng.specFromBrief(brief, {
5009
+ scene: args && args.scene, style: args && args.style,
5010
+ cameraMove: args && args.cameraMove, duration: args && args.duration,
5011
+ }), brief);
5012
+ source = 'fallback';
5013
+ warning = 'AI 搭景失败,已使用本地模板:' + String((e && e.message) || e);
5014
+ }
5015
+ }
5016
+ try {
5017
+ const saved = await methods.directorStageSave({ projectId, stageId, spec });
5018
+ if (!saved || !saved.ok) return { ok: false, error: (saved && saved.error) || '保存失败' };
5019
+ return { ok: true, projectId, stageId, briefVersion: 2, spec, source, warning, savedAt: saved.savedAt || Date.now() };
5020
+ } catch (e) {
5021
+ return { ok: false, error: String((e && e.message) || e) };
5022
+ }
5023
+ },
5024
+ async directorStageEdit(args) {
5025
+ const projectId = String((args && args.projectId) || '');
5026
+ const stageId = safeStageId(args && args.stageId);
5027
+ const instruction = String((args && args.instruction) || '').trim();
5028
+ if (!instruction) return { ok: false, error: '需要 instruction' };
5029
+ const cur = await methods.directorStageLoad({ projectId, stageId });
5030
+ if (!cur || !cur.ok) return { ok: false, error: (cur && cur.error) || '读取场景失败' };
5031
+ if (!cur.found || !cur.spec) return { ok: false, error: '当前 3D-BOX 还没有规格:先生成或导入一版' };
5032
+ try {
5033
+ const edited = await editDirectorSpec(cur.spec, instruction);
5034
+ const saved = await methods.directorStageSave({ projectId, stageId, spec: edited.spec });
5035
+ if (!saved || !saved.ok) return { ok: false, error: (saved && saved.error) || '保存失败' };
5036
+ return { ok: true, projectId, stageId: stageId || null, spec: edited.spec, savedAt: saved.savedAt || Date.now() };
5037
+ } catch (e) {
5038
+ return { ok: false, error: String((e && e.message) || e) };
5039
+ }
5040
+ },
5041
+ async directorStageCamera(args) {
5042
+ const projectId = String((args && args.projectId) || '');
5043
+ const stageId = safeStageId(args && args.stageId);
5044
+ const prompt = String((args && args.prompt) || '').trim();
5045
+ const moveId = String((args && args.cameraMove) || '').trim();
5046
+ if (!prompt && !moveId) return { ok: false, error: '需要 prompt 或 cameraMove' };
5047
+ const cur = await methods.directorStageLoad({ projectId, stageId });
5048
+ if (!cur || !cur.ok) return { ok: false, error: (cur && cur.error) || '读取场景失败' };
5049
+ if (!cur.found || !cur.spec) return { ok: false, error: '当前 3D-BOX 还没有规格:先生成或导入一版' };
5050
+ try {
5051
+ if (prompt) {
5052
+ const planned = await planCameraMotion(cur.spec, prompt, { duration: args && args.duration });
5053
+ const saved = await methods.directorStageSave({ projectId, stageId, spec: planned.spec });
5054
+ if (!saved || !saved.ok) return { ok: false, error: (saved && saved.error) || '保存失败' };
5055
+ return { ok: true, projectId, stageId: stageId || null, spec: planned.spec, keys: planned.keys.length, savedAt: saved.savedAt || Date.now() };
5056
+ }
5057
+ const eng = await loadDirectorEngine();
5058
+ if (!eng || typeof eng.applyCameraMove !== 'function') return { ok: false, error: '引擎未就绪' };
5059
+ const next = eng.applyCameraMove(cur.spec, moveId, { duration: args && args.duration });
5060
+ const saved = await methods.directorStageSave({ projectId, stageId, spec: next });
5061
+ if (!saved || !saved.ok) return { ok: false, error: (saved && saved.error) || '保存失败' };
5062
+ return { ok: true, projectId, stageId: stageId || null, spec: next, keys: (next.cameras && next.cameras[0] && next.cameras[0].keys || []).length, savedAt: saved.savedAt || Date.now() };
5063
+ } catch (e) {
5064
+ return { ok: false, error: String((e && e.message) || e) };
5065
+ }
4514
5066
  },
4515
5067
  async projectCraftRemove(args) {
4516
5068
  const root = dataRoot();
@@ -4648,7 +5200,14 @@ export function apply(ctx) {
4648
5200
  const cur = r.items.find((a) => String(a.id) === id);
4649
5201
  if (!cur) throw new Error('资产不存在:' + id);
4650
5202
  const patch = (args && args.patch) || {};
4651
- const next = { ...cur, ...(patch.name !== undefined ? { name: String(patch.name).slice(0, 80) } : {}), ...(patch.category !== undefined ? { category: String(patch.category || 'general') } : {}) };
5203
+ const next = {
5204
+ ...cur,
5205
+ ...(patch.name !== undefined ? { name: String(patch.name).slice(0, 80) } : {}),
5206
+ ...(patch.category !== undefined ? { category: String(patch.category || 'general') } : {}),
5207
+ ...(patch.tags !== undefined ? { tags: Array.isArray(patch.tags) ? patch.tags.map(String).filter(Boolean).slice(0, 20) : [] } : {}),
5208
+ ...(patch.folder !== undefined ? { folder: String(patch.folder || '').slice(0, 80) } : {}),
5209
+ ...(patch.mergedInto !== undefined ? { mergedInto: patch.mergedInto ? String(patch.mergedInto) : undefined } : {}),
5210
+ };
4652
5211
  return writeAssetIndex(String(next.projectId), '', [next]);
4653
5212
  },
4654
5213
  // CAS 对象库 GC 已随写路径退役(IC-08):见 dbObjectsGc 位置的说明。
@@ -4751,6 +5310,22 @@ export function apply(ctx) {
4751
5310
  return { ok: true, rev: 1, stageMachine: true, qualityGate: true, multiAgent, autoMode: multiAgent, version: VERSION, stages: PRODUCTION_STAGES };
4752
5311
  },
4753
5312
  /** 制作状态汇报:模型宣称"完成/可交付"前必须以 ready:true 为据;next 给当前阶段的下一步。 */
5313
+ async productionPrice() {
5314
+ const cfg = readIcConfig();
5315
+ const price = Number(cfg.production && cfg.production.videoUnitPricePerSecond);
5316
+ return { ok: true, price: Number.isFinite(price) && price > 0 ? price : 0, currency: 'CNY' };
5317
+ },
5318
+ /** 设置视频每秒单价(自动预算/批量估算共用):0 = 未设置。入口是对话工具 canvas_production_set_price。 */
5319
+ async productionSetPrice(args) {
5320
+ const price = Number(args && args.price);
5321
+ if (!Number.isFinite(price) || price < 0 || price > 10000) {
5322
+ return { ok: false, error: '单价必须是 0–10000 的数字(0 = 清除,不显示估算金额)' };
5323
+ }
5324
+ const cfg = readIcConfig();
5325
+ cfg.production = { ...(cfg.production || {}), videoUnitPricePerSecond: Math.round(price * 100) / 100 };
5326
+ writeIcConfig(cfg);
5327
+ return { ok: true, price: cfg.production.videoUnitPricePerSecond, currency: 'CNY' };
5328
+ },
4754
5329
  async productionStatus(args) {
4755
5330
  const pid = String((args && args.projectId) || '');
4756
5331
  if (!pid) return { ok: false, error: '缺少 projectId' };
@@ -4791,7 +5366,8 @@ export function apply(ctx) {
4791
5366
  const configured = Number(cfg.production && cfg.production.videoUnitPricePerSecond);
4792
5367
  const q = PROD.quote(pid, toolCanvasId(args), Number.isFinite(price) && price > 0 ? price : configured);
4793
5368
  if (q && q.ok && q.estimatedCost == null) {
4794
- q.note = (q.note ? q.note + ';' : '') + '未配置视频单价——在画布设置 → 制作 → 视频每秒单价 填入后可显示估算金额';
5369
+ // 锚点词「视频每秒单价」被 test-production 断言(T9):改措辞必须保留这四个字
5370
+ q.note = (q.note ? q.note + ';' : '') + '未设置视频每秒单价——在对话里说「设置视频单价 X 元/秒」后可显示估算金额';
4795
5371
  }
4796
5372
  return q;
4797
5373
  },
@@ -5349,7 +5925,7 @@ export function apply(ctx) {
5349
5925
  });
5350
5926
  reg({
5351
5927
  name: 'canvas_update_node',
5352
- description: '修改无限画布上**已有节点**的字段(改提示词、改比例/清晰度/时长/分辨率等参数)——"改节点"的唯一入口,改完不必删节点重建。可改字段:prompt(图片/视频节点的对话框提示词)、idea/output(剧本节点)、text(文本节点的正文)、script/requirement(分镜表的剧本来源与额外要求)、shots(分镜镜头清单,JSON 数组)、label(节点名)、ratio、size、duration、resolution、frameMode、count、imageModel、videoModel、model、cont(分镜表「分镜衔接」开关,默认 true;关掉后拆镜不写衔接字段、视频不自动续帧)、targetDuration(分镜**总时长硬约束**秒数,0/不传 = 自动档按镜头节奏;设置后所有镜头 duration 之和严格等于该值)、combatMode(打斗判定三态:auto=按本场剧本自动检测(缺省)/ on=本场强制按打斗注入战斗契约 / off=强制不注入——只影响分镜生成的注入口径)。结构字段(id/type/坐标/宽高)不可改。改完用 canvas_generate_node 触发生成。**这是迭代生成的主路径**:用户对某张图/某段视频不满意("重来/换一版/不像")时,改 prompt 后重生成即可,**绝不删掉节点再新建一个**——同一角色/场景/镜的节点永远只有一个,删了重建会丢连线、位置与历史。**前提**:先用 canvas_node_status 读到当前内容,再只传要改的字段。**注意 shots 是整表替换**:只改一镜也要先把 canvas_node_status 返回的全部 shots 取回、改完再整体传回(含每镜的 tail/cam/start/seg/timeBeats/camera/vfx/negative 与打斗字段 attack/reaction/impact/initiative/nextCombat);否则其它镜的衔接与战斗字段会被冲掉(服务端会按镜序兜底保留,但不要依赖它)。',
5928
+ description: '修改无限画布上**已有节点**的字段(改提示词、改比例/清晰度/时长/分辨率等参数)——"改节点"的唯一入口,改完不必删节点重建。可改字段:prompt(图片/视频节点的对话框提示词)、assetId(挂载已有资产 id,图片/视频节点直接呈现该素材;来自 canvas_import_asset / canvas_list_assets)、idea/output(剧本节点)、text(文本节点的正文)、script/requirement(分镜表的剧本来源与额外要求)、shots(分镜镜头清单,JSON 数组)、label(节点名)、ratio、size、duration、resolution、frameMode、count、imageModel、videoModel、model、cont(分镜表「分镜衔接」开关,默认 true;关掉后拆镜不写衔接字段、视频不自动续帧)、targetDuration(分镜**总时长硬约束**秒数,0/不传 = 自动档按镜头节奏;设置后所有镜头 duration 之和严格等于该值)、combatMode(打斗判定三态:auto=按本场剧本自动检测(缺省)/ on=本场强制按打斗注入战斗契约 / off=强制不注入——只影响分镜生成的注入口径)。结构字段(id/type/坐标/宽高)不可改。改完用 canvas_generate_node 触发生成。**这是迭代生成的主路径**:用户对某张图/某段视频不满意("重来/换一版/不像")时,改 prompt 后重生成即可,**绝不删掉节点再新建一个**——同一角色/场景/镜的节点永远只有一个,删了重建会丢连线、位置与历史。**前提**:先用 canvas_node_status 读到当前内容,再只传要改的字段。**注意 shots 是整表替换**:只改一镜也要先把 canvas_node_status 返回的全部 shots 取回、改完再整体传回(含每镜的 tail/cam/start/seg/timeBeats/camera/vfx/negative 与打斗字段 attack/reaction/impact/initiative/nextCombat);否则其它镜的衔接与战斗字段会被冲掉(服务端会按镜序兜底保留,但不要依赖它)。',
5353
5929
  parameters: {
5354
5930
  type: 'object',
5355
5931
  properties: {
@@ -5510,20 +6086,28 @@ export function apply(ctx) {
5510
6086
  });
5511
6087
  reg({
5512
6088
  name: 'canvas_director_stage',
5513
- description: '3D 导演台(预演场景)的总控工具:用一段 JSON 规格直接生成/更新整场预演,无需手动搭场景。\n' +
5514
- 'action=apply:把 spec 写入项目并落盘(客户端打开导演台时自动套用;再点「导出视频」即得白模预演片)。\n' +
6089
+ description: '3D-BOX / 3D 导演台(预演场景)的总控工具:用一段 JSON 规格或一句话直接生成/更新整场预演,无需手动搭场景。\n' +
6090
+ 'action=apply:把 spec 写入项目并落盘(客户端打开 3D-BOX 时自动套用;再点「导出视频」即得白模预演片)。\n' +
6091
+ 'action=brief:一句话搭景。可选 scene(场景预设,见 action=presets)、style(导演风格)、cameraMove(运镜预设);不传则按关键词自动选。\n' +
6092
+ 'action=camera:给当前已保存的规格施加一个运镜预设(生成可编辑的机位关键帧)。\n' +
6093
+ 'action=presets:返回场景预设 / 导演风格 / 运镜清单(Agent 先查再选,避免编造 id)。\n' +
5515
6094
  'action=get:读回当前项目的规格。\n' +
5516
6095
  'action=help:返回规格 schema 与可用姿态/预设清单。\n' +
5517
- 'spec 结构:{ title, duration, env{preset,bg,ground,grid,exposure,tonemap}, ' +
6096
+ 'spec 结构:{ title, duration, env{preset,bg,ground,grid,exposure,tonemap}, style, ' +
5518
6097
  'characters[{name,pos:[x,y,z],rotY,h,pose,color,material{roughness,metallic,specular},path:[[x,z]...],pathFace,poseAnim[{t,pose}],loop,loopPeriod,keys[{t,pos,rotY}]}], ' +
5519
- 'props[{preset,pos,scale,color}], cameras[{name,pos,target,fov,keys[{t,pos,target}]}], shots[{camera,start,end}] }。\n' +
6098
+ 'props[{preset,pos,scale,color}], cameras[{name,pos,target,fov,keys[{t,pos,target,fov}]}], shots[{camera,start,end}], shot{cameraMove,shotSize,note} }。\n' +
5520
6099
  'pose 取值见 action=help 的 poses;道具 preset 见 presets。坐标两维数组 [x,z] 自动补 y=0。',
5521
6100
  parameters: {
5522
6101
  type: 'object',
5523
6102
  properties: {
5524
- action: { type: 'string', enum: ['apply', 'get', 'help', 'brief'], description: '操作类型' },
5525
- brief: { type: 'string', description: '一句话描述(action=brief 时用,如「两人在巷子里打斗,8秒」);会套用打斗/追逐/对话模板生成规格并落盘' },
5526
- template: { type: 'string', enum: ['fight', 'chase', 'dialogue'], description: '强制指定模板(不传则按 brief 关键词自动判断)' },
6103
+ action: { type: 'string', enum: ['apply', 'get', 'help', 'brief', 'presets', 'camera', 'edit'], description: '操作类型' },
6104
+ brief: { type: 'string', description: '一句话描述(action=brief 时用,如「两人在巷子里打斗,8秒」);会套用场景/风格/运镜预设生成规格并落盘' },
6105
+ template: { type: 'string', enum: ['fight', 'chase', 'dialogue'], description: '旧版模板(保留兼容;新代码用 scene)' },
6106
+ scene: { type: 'string', description: '场景预设 id(action=brief 时用;见 action=presets 的 scenePresets,如 dialogue3 / interview / meeting / chase)' },
6107
+ style: { type: 'string', description: '导演风格 id(action=brief 时用;见 action=presets 的 directorStyles,如 animation / tvc / mv / documentary / commerce / suspense)' },
6108
+ cameraMove: { type: 'string', description: '运镜预设 id(action=brief 或 action=camera 时用;见 action=presets 的 cameraMoves,如 push_in / arc_left / crane_up / dolly_zoom)' },
6109
+ prompt: { type: 'string', description: '自然语言运镜(action=camera 时用):如「环绕香水瓶,低机位,最后推近 logo」;由用户已配置模型生成可编辑机位关键帧' },
6110
+ instruction: { type: 'string', description: '自然语言改景指令(action=edit 时用):如「机位再低一点」「让角色 A 从左入画」「加一张桌子」;由用户已配置的模型改当前 spec,失败自动重试一次' },
5527
6111
  duration: { type: 'number', description: '时长秒数(不传则从 brief 里抓「N秒」,否则用模板默认值)' },
5528
6112
  projectId: { type: 'string', description: '项目 id' },
5529
6113
  spec: { type: 'object', description: '场景规格(action=apply 时必填)' },
@@ -5535,6 +6119,19 @@ export function apply(ctx) {
5535
6119
  timeoutMs: 30000,
5536
6120
  async execute(args) {
5537
6121
  const action = String((args && args.action) || 'get');
6122
+ if (action === 'presets') {
6123
+ const eng = await loadDirectorEngine();
6124
+ return {
6125
+ ok: true,
6126
+ scenePresets: (eng && eng.SCENE_PRESETS) || [],
6127
+ directorStyles: (eng && eng.DIRECTOR_STYLES) || [],
6128
+ cameraMoves: (eng && eng.CAMERA_MOVES) || [],
6129
+ text: '✅ 3D-BOX 预设清单\n' +
6130
+ '· 场景:' + (((eng && eng.SCENE_PRESETS) || []).map((x) => x.id + '=' + x.zh).join(' / ') || '(引擎未就绪)') + '\n' +
6131
+ '· 风格:' + (((eng && eng.DIRECTOR_STYLES) || []).map((x) => x.id + '=' + x.zh).join(' / ') || '(引擎未就绪)') + '\n' +
6132
+ '· 运镜:' + (((eng && eng.CAMERA_MOVES) || []).map((x) => x.id + '=' + x.zh).join(' / ') || '(引擎未就绪)'),
6133
+ };
6134
+ }
5538
6135
  if (action === 'help') {
5539
6136
  const eng = await loadDirectorEngine();
5540
6137
  return {
@@ -5544,7 +6141,10 @@ export function apply(ctx) {
5544
6141
  '· 道具 preset:box, sphere, cylinder, cone, plane, capsule, torus, pyramid, disc, arc\n' +
5545
6142
  '· 循环动作:poseAnim 首尾 pose 相同即可循环(如 walk_a→walk_b→walk_a)\n' +
5546
6143
  '· 路径:path 为二维地面点数组,物体沿路径运动并按切线自动转向(pathFace:false 关闭)\n' +
5547
- '· 角色一律使用资产库的真实关节人偶(assetId=xbot):characters[].preset 可填 actors 列表里的体型,未填默认 m_standard',
6144
+ '· 角色一律使用资产库的真实关节人偶(assetId=xbot):characters[].preset 可填 actors 列表里的体型,未填默认 m_standard\n' +
6145
+ '· 3D-BOX 场景预设:' + (((eng && eng.SCENE_PRESETS) || []).map((x) => x.id).join(', ') || '(引擎未就绪)') + '\n' +
6146
+ '· 导演风格:' + (((eng && eng.DIRECTOR_STYLES) || []).map((x) => x.id).join(', ') || '(引擎未就绪)') + '\n' +
6147
+ '· 运镜:' + (((eng && eng.CAMERA_MOVES) || []).map((x) => x.id).join(', ') || '(引擎未就绪)'),
5548
6148
  poses: (eng && eng.BONE_POSES ? eng.BONE_POSES.map((p) => ({ id: p.id, zh: p.zh, en: p.en })) : []),
5549
6149
  presets: ['box', 'sphere', 'cylinder', 'cone', 'plane', 'capsule', 'torus', 'pyramid', 'disc', 'arc'],
5550
6150
  actors: (eng && eng.ACTOR_PRESETS ? eng.ACTOR_PRESETS.map((p) => ({ preset: p.id, zh: p.zh, en: p.en, h: p.h })) : []),
@@ -5556,12 +6156,39 @@ export function apply(ctx) {
5556
6156
  if (!brief) throw new Error('action=brief 需要 brief 一句话描述');
5557
6157
  const eng2 = await loadDirectorEngine();
5558
6158
  if (!eng2 || typeof eng2.specFromBrief !== 'function') throw new Error('引擎未就绪');
5559
- const spec2 = eng2.specFromBrief(brief, { template: args.template, duration: args.duration });
6159
+ const spec2 = eng2.specFromBrief(brief, {
6160
+ template: args.template,
6161
+ scene: args.scene,
6162
+ style: args.style,
6163
+ cameraMove: args.cameraMove,
6164
+ duration: args.duration,
6165
+ });
5560
6166
  const r4 = await methods.directorStageSave({ projectId: args.projectId, spec: spec2 });
5561
6167
  if (!r4.ok) throw new Error(r4.error || '保存失败');
5562
6168
  return {
5563
6169
  ok: true, projectId: String(args.projectId || ''), spec: spec2,
5564
- text: '✅ 已按「' + (spec2.title || brief) + '」生成预演规格并写入项目(' + (spec2.characters || []).length + ' 角色 / ' + (spec2.cameras || []).length + ' 机位 / ' + (spec2.shots || []).length + ' 分镜 / ' + spec2.duration + 's)。打开或刷新 3D 导演台即可看到;点「导出视频」出片。',
6170
+ text: '✅ 已按「' + (spec2.title || brief) + '」生成 3D-BOX 预演并写入项目(' + (spec2.characters || []).length + ' 角色 / ' + (spec2.cameras || []).length + ' 机位 / ' + (spec2.shots || []).length + ' 分镜 / ' + spec2.duration + 's' + (spec2.style ? ' / 风格 ' + spec2.style : '') + (spec2.cameraMove ? ' / 运镜 ' + spec2.cameraMove : '') + ')。打开或刷新 3D-BOX 即可看到;点「导出视频」出片。',
6171
+ };
6172
+ }
6173
+ if (action === 'camera') {
6174
+ const moveId = String((args && args.cameraMove) || '').trim();
6175
+ const prompt = String((args && args.prompt) || '').trim();
6176
+ if (!moveId && !prompt) throw new Error('action=camera 需要 cameraMove 或 prompt');
6177
+ const r5 = await methods.directorStageCamera({ projectId: args.projectId, cameraMove: moveId || undefined, prompt: prompt || undefined, duration: args.duration });
6178
+ if (!r5 || !r5.ok) throw new Error((r5 && r5.error) || '运镜失败');
6179
+ return {
6180
+ ok: true, projectId: String(args.projectId || ''), spec: r5.spec,
6181
+ text: '✅ 已施加' + (prompt ? ' AI 运镜「' + prompt.slice(0, 60) + '」' : '运镜「' + moveId + '」') + '并写入项目(' + (r5.keys || 0) + ' 个机位关键帧)。刷新 3D-BOX 即可预览。',
6182
+ };
6183
+ }
6184
+ if (action === 'edit') {
6185
+ const instruction = String((args && args.instruction) || '').trim();
6186
+ if (!instruction) throw new Error('action=edit 需要 instruction');
6187
+ const r6 = await methods.directorStageEdit({ projectId: args.projectId, instruction });
6188
+ if (!r6 || !r6.ok) throw new Error((r6 && r6.error) || '改景失败');
6189
+ return {
6190
+ ok: true, projectId: String(args.projectId || ''), spec: r6.spec,
6191
+ text: '✅ 已按「' + instruction.slice(0, 60) + '」改景并写入项目(' + ((r6.spec && r6.spec.cameras) || []).length + ' 机位 / ' + ((r6.spec && r6.spec.shots) || []).length + ' 分镜)。刷新 3D-BOX 即可预览。',
5565
6192
  };
5566
6193
  }
5567
6194
  if (action === 'get') {
@@ -5578,6 +6205,68 @@ export function apply(ctx) {
5578
6205
  },
5579
6206
  });
5580
6207
 
6208
+ reg({
6209
+ name: 'canvas_list_presets',
6210
+ description: '列出运镜/转场预设(视频节点“尝试”区、3D-BOX 预设页、Agent 共用一份)。kind=all/camera/transition/template;先查清单再选 id,避免编造。',
6211
+ parameters: {
6212
+ type: 'object',
6213
+ properties: {
6214
+ kind: { type: 'string', enum: ['all', 'camera', 'transition', 'template'], description: '可选:按类型过滤(缺省 all)' },
6215
+ },
6216
+ additionalProperties: false,
6217
+ },
6218
+ output: textOutput,
6219
+ timeoutMs: 15000,
6220
+ async execute(args) {
6221
+ const eng = await loadDirectorEngine();
6222
+ const all = (eng && eng.MOTION_PRESETS) || [];
6223
+ const kind = String((args && args.kind) || 'all');
6224
+ const list = kind === 'all' ? all : all.filter((x) => x.kind === kind);
6225
+ const lines = list.map((x) => '- ' + x.id + ' [' + x.kind + '] ' + x.zh + ':' + x.promptZh);
6226
+ return {
6227
+ ok: true,
6228
+ presets: list.map((x) => {
6229
+ const o = { id: x.id, zh: x.zh, en: x.en, kind: x.kind, category: x.category, promptZh: x.promptZh, promptEn: x.promptEn };
6230
+ if (x.cameraMove) o.cameraMove = x.cameraMove;
6231
+ return o;
6232
+ }),
6233
+ text: '✅ 运镜/转场预设(' + list.length + '):\n' + lines.join('\n'),
6234
+ };
6235
+ },
6236
+ });
6237
+ reg({
6238
+ name: 'canvas_apply_preset',
6239
+ description: '给当前项目的 3D-BOX 规格施加一个运镜/转场预设(转场会映射到基础机位运动;模板只写备注)。先用 canvas_list_presets 查 presetId。',
6240
+ parameters: {
6241
+ type: 'object',
6242
+ properties: {
6243
+ projectId: { type: 'string', description: '项目 id' },
6244
+ presetId: { type: 'string', description: '运镜/转场预设 id(见 canvas_list_presets)' },
6245
+ duration: { type: 'number', description: '可选:时长秒数(缺省用规格时长)' },
6246
+ },
6247
+ required: ['projectId', 'presetId'],
6248
+ additionalProperties: false,
6249
+ },
6250
+ output: textOutput,
6251
+ timeoutMs: 30000,
6252
+ async execute(args) {
6253
+ const pid = String((args && args.projectId) || '');
6254
+ const presetId = String((args && args.presetId) || '').trim();
6255
+ if (!presetId) throw new Error('需要 presetId');
6256
+ const eng = await loadDirectorEngine();
6257
+ if (!eng || typeof eng.applyMotionPreset !== 'function') throw new Error('引擎未就绪');
6258
+ const cur = await methods.directorStageLoad({ projectId: pid });
6259
+ if (!cur || !cur.found || !cur.spec) throw new Error('当前项目还没有 3D-BOX 规格:先用 canvas_director_stage action=brief/apply');
6260
+ const next = eng.applyMotionPreset(cur.spec, presetId, { duration: args.duration });
6261
+ const saved = await methods.directorStageSave({ projectId: pid, spec: next });
6262
+ if (!saved.ok) throw new Error(saved.error || '保存失败');
6263
+ return {
6264
+ ok: true, projectId: pid, spec: next,
6265
+ text: '✅ 已应用预设「' + presetId + '」并写入项目(' + ((next.cameras && next.cameras[0] && next.cameras[0].keys) || []).length + ' 个机位关键帧)。刷新 3D-BOX 即可预览。',
6266
+ };
6267
+ },
6268
+ });
6269
+
5581
6270
  reg({
5582
6271
  name: 'canvas_delete_project',
5583
6272
  description: '删除无限画布上的一个项目(含其全部节点)。用户明确要求删除项目、或清理误创建的空项目时使用;删除前请先用 canvas_list_projects 确认 id。',
@@ -5799,6 +6488,27 @@ export function apply(ctx) {
5799
6488
  return { ok: true, projectId: args.projectId, stage: r.to, text: '✅ 阶段 ' + r.from + ' → ' + r.to + '(已记入审计)。' };
5800
6489
  },
5801
6490
  });
6491
+ reg({
6492
+ name: 'canvas_production_set_price',
6493
+ description: '设置视频每秒单价(人民币):自动创作预算单与批量执行的估算金额都按它计算。用户说"设置视频单价 X 元/秒 / 每秒 X 块"时调用;0 = 清除(不显示金额)。单价不进预算指纹——改价不影响已批准的预算单,只改变金额显示。',
6494
+ parameters: {
6495
+ type: 'object',
6496
+ properties: {
6497
+ price: { type: 'number', description: '每秒单价(人民币元),0–10000;0 = 清除' },
6498
+ },
6499
+ required: ['price'],
6500
+ additionalProperties: false,
6501
+ },
6502
+ output: textOutput,
6503
+ timeoutMs: 15000,
6504
+ async execute(args) {
6505
+ const r = await methods.productionSetPrice(args);
6506
+ if (!r.ok) return { ok: false, text: '❌ ' + (r.error || '单价设置失败') };
6507
+ return { ok: true, price: r.price, text: r.price > 0
6508
+ ? '✅ 视频单价已设为 ' + r.price + ' 元/秒:自动创作预算单与批量执行估算将按此计算。'
6509
+ : '✅ 已清除视频单价(估算金额不再显示;批准付费不受影响)。' };
6510
+ },
6511
+ });
5802
6512
  reg({
5803
6513
  name: 'canvas_auto_status',
5804
6514
  description: '自动创作总状态:开关是否开启、宿主多智能体能力、编剧室运行进度(阶段/日志)、免费段预算单(场/镜/秒/待调用次数/估算金额)、付费段进度与成片节点。【自动模式开场先调它】它决定你走全自动分支还是引导式工作坊。',
@@ -5902,8 +6612,9 @@ export function apply(ctx) {
5902
6612
  properties: {
5903
6613
  projectId: { type: 'string', description: '项目 id' },
5904
6614
  canvasId: { type: 'string', description: '可选:画布 id(项目下的子画布;缺省 = 项目默认画布)' },
5905
- type: { type: 'string', enum: ['script', 'text', 'image', 'video', 'storyboard', 'finalcut', 'image-gen', 'image-asset', 'video-gen', 'video-asset'], description: '节点类型(image/video 为合并类型;旧名 image-gen 等仍接受并自动映射)' },
6615
+ type: { type: 'string', enum: ['script', 'text', 'image', 'video', 'audio', 'storyboard', 'finalcut', 'image-gen', 'image-asset', 'video-gen', 'video-asset'], description: '节点类型(image/video/audio 为合并/资产类型;旧名 image-gen 等仍接受并自动映射)' },
5906
6616
  name: { type: 'string', description: '节点名字(建议填写,如"角色-陈拾 参考图")' },
6617
+ assetId: { type: 'string', description: '可选:已有资产 id(canvas_import_asset / canvas_list_assets 返回)。有 assetId 时图片/视频节点直接呈现该素材,不再需要生成 prompt' },
5907
6618
  prompt: { type: 'string', description: '提示词(图片/视频用画面提示词;剧本用创作意图;文本用正文)' },
5908
6619
  size: { type: 'string', description: '可选:图片清晰度 1K/2K/4K' },
5909
6620
  ratio: { type: 'string', description: '可选:比例,如 16:9 / 9:16 / 1:1' },
@@ -5938,6 +6649,61 @@ export function apply(ctx) {
5938
6649
  };
5939
6650
  },
5940
6651
  });
6652
+ reg({
6653
+ name: 'canvas_import_asset',
6654
+ description: '把本地文件或 http(s) URL 导入当前项目资产库,返回 assetId;随后可用 canvas_create_node / canvas_update_node 的 assetId 字段挂到图片/视频/成片节点。支持 image/video/audio,单文件上限 200MB。',
6655
+ parameters: {
6656
+ type: 'object',
6657
+ properties: {
6658
+ projectId: { type: 'string', description: '项目 id' },
6659
+ path: { type: 'string', description: '可选:本地绝对路径(与 url 二选一)' },
6660
+ url: { type: 'string', description: '可选:http(s) 直链(与 path 二选一)' },
6661
+ kind: { type: 'string', enum: ['image', 'video', 'audio'], description: '可选:资产类型(缺省按 MIME 推断)' },
6662
+ name: { type: 'string', description: '可选:资产显示名(缺省用文件名)' },
6663
+ },
6664
+ required: ['projectId'],
6665
+ additionalProperties: false,
6666
+ },
6667
+ output: textOutput,
6668
+ timeoutMs: 120000,
6669
+ async execute(args) {
6670
+ const r = await importAsset(args);
6671
+ return {
6672
+ ok: true, projectId: String((args && args.projectId) || ''), assetId: r.assetId, kind: r.kind, name: r.name, rel: r.rel,
6673
+ text: '✅ 已导入素材「' + r.name + '」(' + r.kind + ' · ' + Math.round(r.size / 1024) + 'KB · assetId=' + r.assetId + ')。用 canvas_create_node/update_node 的 assetId 挂到节点。',
6674
+ };
6675
+ },
6676
+ });
6677
+ reg({
6678
+ name: 'canvas_list_assets',
6679
+ description: '列出当前项目资产库(可按 kind 过滤)。返回 assetId/name/kind/mime/大小/相对路径;用于 find-and-mount,避免编造 assetId。',
6680
+ parameters: {
6681
+ type: 'object',
6682
+ properties: {
6683
+ projectId: { type: 'string', description: '项目 id' },
6684
+ kind: { type: 'string', enum: ['image', 'video', 'audio', 'text', 'general'], description: '可选:按类型过滤' },
6685
+ limit: { type: 'number', description: '可选:返回条数(默认 50,上限 200)' },
6686
+ },
6687
+ required: ['projectId'],
6688
+ additionalProperties: false,
6689
+ },
6690
+ output: textOutput,
6691
+ timeoutMs: 15000,
6692
+ async execute(args) {
6693
+ const projectId = String((args && args.projectId) || '');
6694
+ const limit = Math.min(200, Math.max(1, Number(args && args.limit) || 50));
6695
+ const r = await methods.assetsList({ projectId, kind: args && args.kind, limit });
6696
+ if (!r || !r.ok) return { ok: false, projectId, text: '❌ 资产列表读取失败:' + ((r && r.error) || '未知错误') };
6697
+ const assets = (r.items || []).map((a) => {
6698
+ const o = { id: a.id, name: a.name, kind: a.kind, mime: a.mime, createdAt: a.createdAt };
6699
+ if (a.rel) o.rel = a.rel;
6700
+ if (a.size != null) o.size = a.size;
6701
+ return o;
6702
+ });
6703
+ const lines = assets.map((a) => '- ' + a.id + ' [' + a.kind + '] ' + a.name + (a.size ? ' · ' + Math.round(a.size / 1024) + 'KB' : ''));
6704
+ return { ok: true, projectId, assets, text: '✅ 资产(' + assets.length + '):\n' + (lines.join('\n') || '(空)') };
6705
+ },
6706
+ });
5941
6707
  reg({
5942
6708
  name: 'canvas_connect',
5943
6709
  description: '在项目画布上把两个节点连成工作流连线(剧本 → 分镜表 → 图片/视频 → 成片)。【工作流铁律】分镜表的内容来自连线的上游剧本节点,不要把剧本全文复制进分镜表节点;图片/视频节点没有上游提示词时才在 prompt 里写内容,有上游时靠连线取。端口类型必须匹配(text→text、image→image、video→video),不匹配会报错;单线输入端口(script/prompt/frame/in)重复连线会替换旧线,ref 类端口允许多条。目标端口缺省时按语义自动选:文本→分镜表 script、分镜表→分镜表 prev、文本→图片/视频 prompt、图片→图片/视频 ref、视频→视频 frame / 成片 in。',
@@ -6114,7 +6880,7 @@ export function apply(ctx) {
6114
6880
  let raw = '';
6115
6881
  for await (const chunk of req) {
6116
6882
  raw += chunk;
6117
- if (raw.length > 4e6) throw new Error('请求过大');
6883
+ if (raw.length > 16e6) throw new Error('请求过大'); // 视觉拉片可能带多帧 base64
6118
6884
  }
6119
6885
  const args = JSON.parse(raw || '{}');
6120
6886
  res.statusCode = 200;