@acosmi/sdk-ts 2.1.0 → 2.2.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/dist/index.mjs CHANGED
@@ -2728,6 +2728,60 @@ var Client = class _Client {
2728
2728
  ctl.dispose();
2729
2729
  }
2730
2730
  }
2731
+ // ===========================================================================
2732
+ // 媒体生成 (v1.3+) — 图片 / 视频生成托管模型 (与 chat 同网关)
2733
+ //
2734
+ // 仅对 capabilities.supports_image_generation / supports_video_generation 的模型有效。
2735
+ // 网关不算钱; 用量由网关上报营销系统结算。
2736
+ // ===========================================================================
2737
+ /** 解析 nexus-v4 {code,message,data} 信封, code!=0 抛 BusinessError, 返回 data。 */
2738
+ unwrapAPIResponse(result) {
2739
+ const env = JSON.parse(new TextDecoder().decode(result));
2740
+ const bizErr = apiResponseBusinessError(env);
2741
+ if (bizErr) throw bizErr;
2742
+ return env.data;
2743
+ }
2744
+ /**
2745
+ * 图片生成 (同步)。POST /managed-models/:id/images/generations
2746
+ *
2747
+ * @param modelID 图片生成托管模型 ID (capabilities.supports_image_generation=true)
2748
+ */
2749
+ async generateImage(modelID, req, signal) {
2750
+ const endpoint = `/managed-models/${encodeURIComponent(modelID)}/images/generations`;
2751
+ const { result } = await this.doJSONFullRaw(
2752
+ "POST",
2753
+ endpoint,
2754
+ req,
2755
+ signal,
2756
+ CHAT_REQUEST_TIMEOUT_MS
2757
+ );
2758
+ return this.unwrapAPIResponse(result);
2759
+ }
2760
+ /**
2761
+ * 创建视频生成任务 (异步)。POST /managed-models/:id/videos/generations
2762
+ * 返回 taskId; 用 pollVideoTask() 轮询直到 status=completed。
2763
+ * 上报真物理量 (视频秒数) 需在 pollVideoTask 时回传 req.duration。
2764
+ *
2765
+ * @param modelID 视频生成托管模型 ID (capabilities.supports_video_generation=true)
2766
+ */
2767
+ async generateVideo(modelID, req, signal) {
2768
+ const endpoint = `/managed-models/${encodeURIComponent(modelID)}/videos/generations`;
2769
+ const { result } = await this.doJSONFullRaw("POST", endpoint, req, signal);
2770
+ return this.unwrapAPIResponse(result);
2771
+ }
2772
+ /**
2773
+ * 轮询视频任务状态。GET /managed-models/:id/videos/tasks/:taskId
2774
+ *
2775
+ * @param durationSeconds 创建时的时长 (秒), 透传给网关在 completed 时上报用量。
2776
+ */
2777
+ async pollVideoTask(modelID, taskID, durationSeconds, signal) {
2778
+ let endpoint = `/managed-models/${encodeURIComponent(modelID)}/videos/tasks/${encodeURIComponent(taskID)}`;
2779
+ if (durationSeconds != null && durationSeconds > 0) {
2780
+ endpoint += `?duration=${encodeURIComponent(String(durationSeconds))}`;
2781
+ }
2782
+ const { result } = await this.doJSONFullRaw("GET", endpoint, null, signal);
2783
+ return this.unwrapAPIResponse(result);
2784
+ }
2731
2785
  /**
2732
2786
  * Anthropic 原生格式同步聊天
2733
2787
  * v0.5.0: 根据 provider 自动路由
@@ -3072,11 +3126,11 @@ var Client = class _Client {
3072
3126
  }
3073
3127
  }
3074
3128
  /** doJSONFull 的 raw bytes 变体 (chat 用, 不立即 JSON.parse) */
3075
- async doJSONFullRaw(method, path, body, signal) {
3076
- return this.doJSONFullRawInternal(method, path, body, signal, false);
3129
+ async doJSONFullRaw(method, path, body, signal, timeoutMs = 3e4) {
3130
+ return this.doJSONFullRawInternal(method, path, body, signal, false, timeoutMs);
3077
3131
  }
3078
- async doJSONFullRawInternal(method, path, body, signal, retried) {
3079
- const ctl = withRequestTimeout(3e4, signal);
3132
+ async doJSONFullRawInternal(method, path, body, signal, retried, timeoutMs = 3e4) {
3133
+ const ctl = withRequestTimeout(timeoutMs, signal);
3080
3134
  try {
3081
3135
  const token = await this.ensureToken(ctl.signal);
3082
3136
  let bodyStr = null;
@@ -3104,7 +3158,7 @@ var Client = class _Client {
3104
3158
  `unauthorized and refresh failed: ${refreshErr instanceof Error ? refreshErr.message : String(refreshErr)}`
3105
3159
  );
3106
3160
  }
3107
- return this.doJSONFullRawInternal(method, path, body, signal, true);
3161
+ return this.doJSONFullRawInternal(method, path, body, signal, true, timeoutMs);
3108
3162
  }
3109
3163
  if (resp.status < 200 || resp.status >= 300) {
3110
3164
  const bodyBytes = await readLimited(resp.body, maxErrorBodySize);