@ninkch/dsh-infinite-canvas 0.19.14 → 0.19.16

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.
Files changed (2) hide show
  1. package/client.js +112 -29
  2. package/package.json +1 -1
package/client.js CHANGED
@@ -5992,6 +5992,40 @@ function queueThumb(assetId) {
5992
5992
  ?? (j.data && (j.data.task_id ?? j.data.id ?? j.data.taskId))
5993
5993
  ?? (j.output && (j.output.task_id ?? j.output.id));
5994
5994
  }
5995
+ /** 中转站常见的一类 5xx:图片被交给内部图片代理后,代理只认 http(s) URL。
5996
+ * 文档与实测表明同一网关支持 dataURL,只是字段形态不同(input_reference / images:string[])。
5997
+ * 只识别这一类错误,鉴权/限流/内容安全等错误绝不重试。 */
5998
+ function needsSimpleMediaRetry(errText) {
5999
+ const t = String(errText || '');
6000
+ return /图片接口代理请求失败|需要\s*https?(?:\(s\))?\s*接口\s*url|image\s+(?:proxy|api).*?\burl\b/i.test(t);
6001
+ }
6002
+ /** 通用简化媒体请求体:不绑定任何厂商;首帧用 input_reference,参考图用字符串 dataURL 数组。
6003
+ * 带参考音频时统一走 media[](reference_image / reference_audio),首帧同时存在也降级为参考图,
6004
+ * 避免 first_frame 与 reference_* 互斥。 */
6005
+ function buildSimpleMediaRetryBody(base, media) {
6006
+ const b = base || {};
6007
+ const m = media || {};
6008
+ const frame = String(m.frameDataUrl || '');
6009
+ const refs = (Array.isArray(m.refDataUrls) ? m.refDataUrls : []).map(String).filter(Boolean);
6010
+ const audios = (Array.isArray(m.audioDataUrls) ? m.audioDataUrls : []).map(String).filter(Boolean);
6011
+ const out = {
6012
+ ...(b.model ? { model: b.model } : {}),
6013
+ prompt: String(b.prompt || ''),
6014
+ duration: Math.max(2, Math.min(30, Math.round(Number(b.duration) || 5))),
6015
+ resolution: String(b.resolution || '720P').toUpperCase(),
6016
+ ratio: String(b.ratio || '16:9'),
6017
+ };
6018
+ const images = (frame ? [frame] : []).concat(refs);
6019
+ if (audios.length) {
6020
+ out.media = images.map((url) => ({ type: 'reference_image', url }))
6021
+ .concat(audios.map((url) => ({ type: 'reference_audio', url })));
6022
+ } else if (frame && !refs.length) {
6023
+ out.input_reference = frame;
6024
+ } else if (images.length) {
6025
+ out.images = images;
6026
+ }
6027
+ return out;
6028
+ }
5995
6029
  async function downloadVideo(url, headers, authFallback) {
5996
6030
  // 经 Host 代理:签名下载直连同样会被 CORS 拦
5997
6031
  let r = await fetchViaProxy(url, { method: 'GET', headers: headers || {}, timeoutMs: 300000, label: '下载视频' });
@@ -6232,6 +6266,44 @@ function queueThumb(assetId) {
6232
6266
  taskUrl: (tid) => apiRoot + '/api/v1/tasks/' + encodeURIComponent(tid),
6233
6267
  };
6234
6268
  }
6269
+ /** DashScope 兼容中转站的响应形态归一(tiantoken 等中转会改写响应结构,纯函数供回归断言):
6270
+ * · task_id:output.task_id → task_id → data.task_id → output.id → id;
6271
+ * · 状态:output.task_status → output.status → task_status → status,统一小写比较
6272
+ * (官方大写 SUCCEEDED / 中转小写 succeeded / 顶层 status 都能认);
6273
+ * · 成功/失败集合外的一切状态(unknown/pending/running 及未知值)= 继续轮询,绝不误杀
6274
+ * (中转在任务刚提交、上游未回报时常回 {status:'unknown'})。 */
6275
+ function wanTaskIdOf(j) {
6276
+ if (!j || typeof j !== 'object') return null;
6277
+ return (j.output && j.output.task_id) || j.task_id || (j.data && j.data.task_id)
6278
+ || (j.output && j.output.id) || j.id || null;
6279
+ }
6280
+ const WAN_STATUS_DONE = ['succeeded', 'success', 'done', 'completed', 'complete', 'finished', 'ok'];
6281
+ const WAN_STATUS_FAIL = ['failed', 'failure', 'fail', 'error', 'canceled', 'cancelled', 'expired', 'rejected', 'timeout'];
6282
+ function wanStatusOf(j) {
6283
+ if (!j || typeof j !== 'object') return '';
6284
+ const v = (j.output && (j.output.task_status ?? j.output.status)) ?? j.task_status ?? j.status;
6285
+ return String(v ?? '').trim().toLowerCase();
6286
+ }
6287
+ function wanVideoUrlOf(j) {
6288
+ if (!j || typeof j !== 'object') return '';
6289
+ const o = j.output || {};
6290
+ return [o.video_url, o.url, o.videoUrl, j.video_url, j.data && j.data.result_url]
6291
+ .find((u) => typeof u === 'string' && /^https?:/.test(u)) || '';
6292
+ }
6293
+ function wanErrorOf(j) {
6294
+ const o = (j && j.output) || {};
6295
+ const msg = o.message || (j && (j.message || (j.error && j.error.message))) || '';
6296
+ return (o.code && msg) ? (o.code + ' ' + msg) : (msg || o.code || '');
6297
+ }
6298
+ function wanPollState(j) {
6299
+ const st = wanStatusOf(j);
6300
+ if (WAN_STATUS_DONE.includes(st)) {
6301
+ const url = wanVideoUrlOf(j);
6302
+ return url ? { state: 'done', url } : { state: 'fail', error: '成功但无视频地址' };
6303
+ }
6304
+ if (WAN_STATUS_FAIL.includes(st)) return { state: 'fail', error: wanErrorOf(j) || st };
6305
+ return { state: 'wait' }; // unknown/pending/running 及一切未知状态 = 继续轮询
6306
+ }
6235
6307
  /** 读取生成视频的真实尺寸与时长(用于「厂商是否忽略了参数」的可见校验)。 */
6236
6308
  function probeVideoMeta(blob) {
6237
6309
  return new Promise((resolve) => {
@@ -6393,17 +6465,14 @@ function queueThumb(assetId) {
6393
6465
  const r1 = await fetchViaProxy(ep.submitUrl, { headers, body, label: 'Wan 提交' });
6394
6466
  if (!r1.ok) throw new Error('Wan 提交 HTTP ' + r1.status + ' @ ' + ep.submitUrl + ':' + r1.text.slice(0, 300));
6395
6467
  let j1 = null; try { j1 = JSON.parse(r1.text); } catch { throw new Error('Wan 响应非 JSON:' + r1.text.slice(0, 200)); }
6396
- const tid = j1 && j1.output && j1.output.task_id;
6468
+ const tid = wanTaskIdOf(j1);
6397
6469
  if (!tid) throw new Error('Wan 响应中没有 task_id:' + r1.text.slice(0, 200));
6398
6470
  if (o.onTask) { try { o.onTask(tid, ep.taskUrl(tid)); } catch { /* 持久化失败不影响生成 */ } }
6399
6471
  const url = await vendorPoll('Wan', 8000, async () => {
6400
6472
  const r = await fetchViaProxy(ep.taskUrl(tid), { method: 'GET', headers: { Authorization: 'Bearer ' + (s.apiKey || '') }, timeoutMs: 60000, label: 'Wan 查询' });
6401
6473
  if (!r || !r.ok) return { state: 'wait' };
6402
6474
  let j = null; try { j = JSON.parse(r.text); } catch { return { state: 'wait' }; }
6403
- const st = j && j.output && j.output.task_status;
6404
- if (st === 'SUCCEEDED') return j.output.video_url ? { state: 'done', url: j.output.video_url } : { state: 'fail', error: '成功但无视频地址' };
6405
- if (st === 'FAILED' || st === 'CANCELED') return { state: 'fail', error: (j.output && j.output.message) || st };
6406
- return { state: 'wait' };
6475
+ return wanPollState(j);
6407
6476
  });
6408
6477
  return await downloadVideo(url, {});
6409
6478
  },
@@ -6759,15 +6828,7 @@ function queueThumb(assetId) {
6759
6828
  const isOpenaiVideo = /\/video\/generations$/.test(base);
6760
6829
  // moyu 类中转站:OpenAI 风格但图片字段用 input_reference / images:string[](dataURL 直接放),
6761
6830
  // 不收 [{url,role}] 对象数组;resolution 用大写枚举;无 generate_audio/output_format 参数。
6762
- const isMoyuFlavor = /moyu\.info/i.test(base);
6763
- // moyu 的图片代理接受「裸 base64」或「http(s) URL」,但【不认 data: 前缀】——
6764
- // dataURL 的前缀会让它的 URL 校验失败(0.19.13 实测 502「需要 http(s) 接口 url」)。
6765
- const moyuImagePayload = (dataUrl) => {
6766
- const t = String(dataUrl || '');
6767
- const m = /^data:[^;]+;base64,(.+)$/.exec(t);
6768
- return m ? m[1] : t;
6769
- };
6770
- const headers = { 'Content-Type': 'application/json', ...(s.apiKey ? { Authorization: 'Bearer ' + s.apiKey } : {}) };
6831
+ const headers = { 'Content-Type': 'application/json', ...(s.apiKey ? { Authorization: 'Bearer ' + s.apiKey } : {}) };
6771
6832
  // 尾帧+参考图同连(跨镜衔接)→ 尾帧降级为第一张衔接参考图(first_frame/reference_image 互斥),标注写在提示词里
6772
6833
  const vRefs = o.hardFrame ? [] : (o.refs || []).slice(0, 8);
6773
6834
  const downgrade = Boolean(frameBlob && vRefs.length);
@@ -6775,30 +6836,22 @@ function queueThumb(assetId) {
6775
6836
  const body = {
6776
6837
  ...(s.model ? { model: s.model } : {}),
6777
6838
  prompt,
6778
- duration: Math.min(30, Math.max(2, Number(o.duration) || 5)),
6839
+ duration: Number(o.duration) || 5,
6779
6840
  // 分辨率一律小写(Seedance 系为 '720p'/'1080p';'720P' 会被部分网关判为不支持)
6780
6841
  resolution: (o.resolution || '720p').toLowerCase(),
6781
6842
  ratio: o.ratio || '16:9',
6782
6843
  };
6783
- if (isMoyuFlavor) body.resolution = String(o.resolution || '1080P').toUpperCase(); // moyu 枚举 480P/720P/1080P
6784
6844
  // 首帧图生视频:构图以首帧为准——Seedance 系要求首帧模式必须 ratio=adaptive(仅纯首帧模式)
6785
6845
  if (frameBlob && !downgrade) {
6786
6846
  body.ratio = 'adaptive';
6787
- if (isMoyuFlavor) body.input_reference = moyuImagePayload(await blobToDataUrlScaled(frameBlob));
6788
- else try { body.image = await blobToDataUrlScaled(frameBlob); } catch { /* 忽略首帧编码失败 */ }
6847
+ try { body.image = await blobToDataUrlScaled(frameBlob); } catch { /* 忽略首帧编码失败 */ }
6789
6848
  } else if (vRefs.length) {
6790
6849
  const imgs = downgrade ? [frameBlob].concat(vRefs) : vRefs;
6791
- body.images = isMoyuFlavor
6792
- ? await Promise.all(imgs.map(async (b) => moyuImagePayload(await blobToDataUrlScaled(b))))
6793
- : isOpenaiVideo
6794
- ? await Promise.all(imgs.map(async (b) => ({ url: await blobToDataUrlScaled(b), role: 'reference_image' })))
6795
- : await Promise.all(imgs.map((b) => blobToDataUrlScaled(b)));
6850
+ body.images = await Promise.all(imgs.map(async (b) => ({ url: await blobToDataUrlScaled(b), role: 'reference_image' })));
6796
6851
  }
6797
6852
  // 可选参数:勾选才发送 generate_audio;output_format 留空不发送
6798
- if (!isMoyuFlavor) {
6799
- if (s.generateAudio === true) body.generate_audio = true;
6800
- if (String(s.outputFormat || '').trim()) body.output_format = String(s.outputFormat).trim();
6801
- }
6853
+ if (s.generateAudio === true) body.generate_audio = true;
6854
+ if (String(s.outputFormat || '').trim()) body.output_format = String(s.outputFormat).trim();
6802
6855
  // 角色声音参考(@引用实体名下的音频资产)→ audios[]:模型生成的对白/声音向参考音色对齐;
6803
6856
  // 带声音参考时强制开启同步音频(否则参考无意义)
6804
6857
  if (o.audioRefs && o.audioRefs.length) {
@@ -6810,7 +6863,37 @@ function queueThumb(assetId) {
6810
6863
  const extra = parseExtraParams(s.extra);
6811
6864
  if (extra) Object.assign(body, extra);
6812
6865
  } catch (e) { throw new Error('视频 API 扩展参数配置有误:' + ((e && e.message) || e)); }
6813
- const res = await fetchViaProxy(base, { headers, body, timeoutMs: Number(s.timeoutMs) > 0 ? Number(s.timeoutMs) : 120000, label: '视频 API' });
6866
+ const res0 = await fetchViaProxy(base, { headers, body, timeoutMs: Number(s.timeoutMs) > 0 ? Number(s.timeoutMs) : 120000, label: '视频 API' });
6867
+ let res = res0;
6868
+ // 通用兜底:不新增厂商。首帧/image/images 对象被中转站内部图片代理拒绝时,
6869
+ // 只把参考素材改成文档支持的简单字段再试一次:首帧→input_reference,
6870
+ // 参考图→images 字符串数组(带音频时→media[])。
6871
+ if (!res.ok && needsSimpleMediaRetry(res.text) && (frameBlob || vRefs.length)) {
6872
+ const frameDataUrl = frameBlob ? await blobToDataUrlScaled(frameBlob) : '';
6873
+ const refDataUrls = await Promise.all(vRefs.map((b) => blobToDataUrlScaled(b)));
6874
+ const audioDataUrls = await Promise.all((o.audioRefs || []).map((b) => blobToDataUrl(b)));
6875
+ const simpleBody = buildSimpleMediaRetryBody({
6876
+ model: s.model || '',
6877
+ prompt,
6878
+ duration: Number(o.duration) || 5,
6879
+ resolution: o.resolution || '720P',
6880
+ ratio: body.ratio || o.ratio || '16:9',
6881
+ }, { frameDataUrl, refDataUrls, audioDataUrls });
6882
+ // 扩展参数仍然优先,允许中转站在兜底请求上继续覆盖字段
6883
+ try {
6884
+ const extra = parseExtraParams(s.extra);
6885
+ if (extra) Object.assign(simpleBody, extra);
6886
+ } catch { /* 主请求已校验过,不因兜底阻断 */ }
6887
+ try {
6888
+ res = await fetchViaProxy(base, { headers, body: simpleBody, timeoutMs: Number(s.timeoutMs) > 0 ? Number(s.timeoutMs) : 120000, label: '视频 API(简化参考字段重试)' });
6889
+ } catch (e) {
6890
+ throw new Error('视频 API 请求失败,简化参考字段重试也失败:' + ((e && e.message) || e));
6891
+ }
6892
+ if (!res.ok) {
6893
+ throw new Error('视频 API HTTP ' + res0.status + ' @ ' + base + ':' + res0.text.slice(0, 160)
6894
+ + ';[简化参考字段重试] HTTP ' + res.status + ':' + res.text.slice(0, 200));
6895
+ }
6896
+ }
6814
6897
  if (!res.ok) {
6815
6898
  if (isOpenaiVideo) {
6816
6899
  // 降级链:① Seedance 原生 content[] 格式(RelayRouter 等,轮询 /v1/video/query)
@@ -20105,7 +20188,7 @@ const DIRECTOR_KEY_HELP = [
20105
20188
  // IC-04:参考图上传三件套的纯函数出口(供 scripts/test-image-upload.mjs 直接断言)
20106
20189
  window.__DSH_IC_IMGREF__ = { rawImageDims, sniffImageEncoding, registerReferenceAsset };
20107
20190
  // IC-03:视频厂商识别/前缀归一化的纯函数出口(供 scripts/test-vendor-detect.mjs 直接断言)
20108
- window.__DSH_IC_VENDOR__ = { detectVideoVendor, gatewayPrefixFor, wanEndpoints, vendorBase, VIDEO_REF_CAPS };
20191
+ window.__DSH_IC_VENDOR__ = { detectVideoVendor, gatewayPrefixFor, wanEndpoints, vendorBase, VIDEO_REF_CAPS, needsSimpleMediaRetry, buildSimpleMediaRetryBody, wanTaskIdOf, wanStatusOf, wanPollState };
20109
20192
  // 按镜头切段(分段生成本身就是最容易出错的地方):规划器 + 强弱模型分流 + 节点段区间解析
20110
20193
  // 供 scripts/test-segment-plan.mjs 直接断言
20111
20194
  window.__DSH_IC_SEGMENT__ = { planShotSegments, isCutPoint, segmentNeedsHardFrame, videoSegmentOf, shotContinuityPlan };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ninkch/dsh-infinite-canvas",
3
- "version": "0.19.14",
3
+ "version": "0.19.16",
4
4
  "description": "无限画布(Infinite Canvas)for DeepSeek Harness:节点式创作工作室(剧本/分镜/生成/成片),内置「媒体创作」预设模式(导演第一视角 + 引导工作坊)。Version-agnostic: reads the live catalog from the installed package at load time.",
5
5
  "keywords": [
6
6
  "dsh",