@acosmi/sdk-ts 2.17.0 → 2.19.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.
@@ -2319,6 +2319,22 @@ function notifyUpstreamActivity(cb) {
2319
2319
  } catch {
2320
2320
  }
2321
2321
  }
2322
+ var GATEWAY_REQUEST_ID_HEADER = "X-Acosmi-Request-Id";
2323
+ function readGatewayRequestID(headers) {
2324
+ const raw = headers.get(GATEWAY_REQUEST_ID_HEADER);
2325
+ if (!raw) return void 0;
2326
+ const trimmed = raw.trim();
2327
+ return trimmed === "" ? void 0 : trimmed;
2328
+ }
2329
+ function notifyGatewayRequestID(cb, headers) {
2330
+ if (!cb) return;
2331
+ const id = readGatewayRequestID(headers);
2332
+ if (id === void 0) return;
2333
+ try {
2334
+ cb(id);
2335
+ } catch {
2336
+ }
2337
+ }
2322
2338
  var DEFAULT_API_TIMEOUT_MS = 6e4;
2323
2339
  function newDeferred() {
2324
2340
  let resolve;
@@ -3026,7 +3042,7 @@ var Client = class _Client {
3026
3042
  * 响应的 tokenRemaining / callRemaining 字段来自服务端 Header, 反映结算后余额
3027
3043
  * v0.5.0: 根据 provider 自动路由到 /anthropic 或 /chat 端点
3028
3044
  */
3029
- async chat(modelID, req, signal) {
3045
+ async chat(modelID, req, signal, onGatewayRequestID) {
3030
3046
  const r = { ...req, stream: false };
3031
3047
  const ctl = withRequestTimeout(CHAT_REQUEST_TIMEOUT_MS, signal);
3032
3048
  try {
@@ -3039,6 +3055,7 @@ var Client = class _Client {
3039
3055
  ctl.signal,
3040
3056
  CHAT_REQUEST_TIMEOUT_MS
3041
3057
  );
3058
+ notifyGatewayRequestID(onGatewayRequestID, headers);
3042
3059
  const resp = adapter.parseResponse(result);
3043
3060
  const v1 = headers.get("X-Token-Remaining");
3044
3061
  if (v1) {
@@ -3159,28 +3176,29 @@ var Client = class _Client {
3159
3176
  * Anthropic → chatMessagesAnthropic (现有路径, POST /anthropic)
3160
3177
  * 其他厂商 → chatMessagesOpenAI (POST /chat, 响应转换为 AnthropicResponse)
3161
3178
  */
3162
- async chatMessages(modelID, req, signal) {
3179
+ async chatMessages(modelID, req, signal, onGatewayRequestID) {
3163
3180
  const m = await this.ensureModelCached(modelID, signal);
3164
3181
  const adapter = getAdapterForModel(m);
3165
3182
  if (adapter.format() === 0 /* Anthropic */) {
3166
- return this.chatMessagesAnthropic(modelID, req, adapter, signal);
3183
+ return this.chatMessagesAnthropic(modelID, req, adapter, signal, onGatewayRequestID);
3167
3184
  }
3168
- return this.chatMessagesOpenAI(modelID, req, adapter, signal);
3185
+ return this.chatMessagesOpenAI(modelID, req, adapter, signal, onGatewayRequestID);
3169
3186
  }
3170
- async chatMessagesAnthropic(modelID, req, adapter, signal) {
3187
+ async chatMessagesAnthropic(modelID, req, adapter, signal, onGatewayRequestID) {
3171
3188
  const r = { ...req, stream: false };
3172
3189
  const ctl = withRequestTimeout(CHAT_REQUEST_TIMEOUT_MS, signal);
3173
3190
  try {
3174
3191
  const caps = this.getCachedCapabilities(modelID) ?? zeroModelCapabilities();
3175
3192
  const body = adapter.buildRequestBody(caps, r);
3176
3193
  const data = JSON.stringify(body);
3177
- const { result } = await this.doJSONFullRaw(
3194
+ const { result, headers } = await this.doJSONFullRaw(
3178
3195
  "POST",
3179
3196
  `/managed-models/${encodeURIComponent(modelID)}/anthropic`,
3180
3197
  data,
3181
3198
  ctl.signal,
3182
3199
  CHAT_REQUEST_TIMEOUT_MS
3183
3200
  );
3201
+ notifyGatewayRequestID(onGatewayRequestID, headers);
3184
3202
  const rawStr = new TextDecoder().decode(result);
3185
3203
  try {
3186
3204
  const wrapper = JSON.parse(rawStr);
@@ -3205,7 +3223,7 @@ var Client = class _Client {
3205
3223
  ctl.dispose();
3206
3224
  }
3207
3225
  }
3208
- async chatMessagesOpenAI(modelID, req, adapter, signal) {
3226
+ async chatMessagesOpenAI(modelID, req, adapter, signal, onGatewayRequestID) {
3209
3227
  const r = { ...req, stream: false };
3210
3228
  const ctl = withRequestTimeout(CHAT_REQUEST_TIMEOUT_MS, signal);
3211
3229
  try {
@@ -3213,13 +3231,14 @@ var Client = class _Client {
3213
3231
  const body = adapter.buildRequestBody(caps, r);
3214
3232
  const data = JSON.stringify(body);
3215
3233
  const endpoint = `/managed-models/${encodeURIComponent(modelID)}${adapter.endpointSuffix()}`;
3216
- const { result } = await this.doJSONFullRaw(
3234
+ const { result, headers } = await this.doJSONFullRaw(
3217
3235
  "POST",
3218
3236
  endpoint,
3219
3237
  data,
3220
3238
  ctl.signal,
3221
3239
  CHAT_REQUEST_TIMEOUT_MS
3222
3240
  );
3241
+ notifyGatewayRequestID(onGatewayRequestID, headers);
3223
3242
  const { parseOpenAIResponseToAnthropic: parseOpenAIResponseToAnthropic2 } = await Promise.resolve().then(() => (init_openai(), openai_exports));
3224
3243
  return parseOpenAIResponseToAnthropic2(result);
3225
3244
  } finally {
@@ -3231,10 +3250,11 @@ var Client = class _Client {
3231
3250
  * v0.5.0: 根据 adapter 路由端点
3232
3251
  *
3233
3252
  * @param onUpstreamActivity 见 {@link UpstreamActivityCallback}
3253
+ * @param onGatewayRequestID 见 {@link GatewayRequestIDCallback}
3234
3254
  */
3235
- chatStream(modelID, req, signal, onUpstreamActivity) {
3255
+ chatStream(modelID, req, signal, onUpstreamActivity, onGatewayRequestID) {
3236
3256
  return {
3237
- [Symbol.asyncIterator]: () => this.chatStreamGen(modelID, req, signal, false, onUpstreamActivity)
3257
+ [Symbol.asyncIterator]: () => this.chatStreamGen(modelID, req, signal, false, onUpstreamActivity, onGatewayRequestID)
3238
3258
  };
3239
3259
  }
3240
3260
  /**
@@ -3243,13 +3263,14 @@ var Client = class _Client {
3243
3263
  * 无 started/settled/failed 自定义事件, 无 data: [DONE], message_stop 为自然结束
3244
3264
  *
3245
3265
  * @param onUpstreamActivity 见 {@link UpstreamActivityCallback}
3266
+ * @param onGatewayRequestID 见 {@link GatewayRequestIDCallback}
3246
3267
  */
3247
- chatMessagesStream(modelID, req, signal, onUpstreamActivity) {
3268
+ chatMessagesStream(modelID, req, signal, onUpstreamActivity, onGatewayRequestID) {
3248
3269
  return {
3249
- [Symbol.asyncIterator]: () => this.chatMessagesStreamGen(modelID, req, signal, false, onUpstreamActivity)
3270
+ [Symbol.asyncIterator]: () => this.chatMessagesStreamGen(modelID, req, signal, false, onUpstreamActivity, onGatewayRequestID)
3250
3271
  };
3251
3272
  }
3252
- async *chatStreamGen(modelID, req, signal, retried, onUpstreamActivity) {
3273
+ async *chatStreamGen(modelID, req, signal, retried, onUpstreamActivity, onGatewayRequestID) {
3253
3274
  const r = { ...req, stream: true };
3254
3275
  const { body, adapter } = await this.buildChatRequest(modelID, r, signal);
3255
3276
  const token = await this.ensureToken(signal);
@@ -3282,9 +3303,10 @@ var Client = class _Client {
3282
3303
  `stream: unauthorized and refresh failed: ${refreshErr instanceof Error ? refreshErr.message : String(refreshErr)}`
3283
3304
  );
3284
3305
  }
3285
- yield* this.chatStreamGen(modelID, req, signal, true, onUpstreamActivity);
3306
+ yield* this.chatStreamGen(modelID, req, signal, true, onUpstreamActivity, onGatewayRequestID);
3286
3307
  return;
3287
3308
  }
3309
+ notifyGatewayRequestID(onGatewayRequestID, resp.headers);
3288
3310
  if (!resp.ok) {
3289
3311
  const bodyBytes = await readLimited(resp.body, maxErrorBodySize);
3290
3312
  throw parseHTTPErrorWithHeader(resp.status, bodyBytes, resp.headers);
@@ -3319,7 +3341,7 @@ var Client = class _Client {
3319
3341
  }
3320
3342
  }
3321
3343
  }
3322
- async *chatMessagesStreamGen(modelID, req, signal, retried, onUpstreamActivity) {
3344
+ async *chatMessagesStreamGen(modelID, req, signal, retried, onUpstreamActivity, onGatewayRequestID) {
3323
3345
  const r = { ...req, stream: true };
3324
3346
  const { body, adapter } = await this.buildChatRequest(modelID, r, signal);
3325
3347
  const token = await this.ensureToken(signal);
@@ -3352,9 +3374,10 @@ var Client = class _Client {
3352
3374
  `messages stream: unauthorized and refresh failed: ${refreshErr instanceof Error ? refreshErr.message : String(refreshErr)}`
3353
3375
  );
3354
3376
  }
3355
- yield* this.chatMessagesStreamGen(modelID, req, signal, true, onUpstreamActivity);
3377
+ yield* this.chatMessagesStreamGen(modelID, req, signal, true, onUpstreamActivity, onGatewayRequestID);
3356
3378
  return;
3357
3379
  }
3380
+ notifyGatewayRequestID(onGatewayRequestID, resp.headers);
3358
3381
  if (!resp.ok) {
3359
3382
  const bodyBytes = await readLimited(resp.body, maxErrorBodySize);
3360
3383
  throw parseHTTPErrorWithHeader(resp.status, bodyBytes, resp.headers);
@@ -7871,6 +7894,7 @@ exports.FilterStatusFallbackTkdistSkew = FilterStatusFallbackTkdistSkew;
7871
7894
  exports.FilterStatusInternalBypass = FilterStatusInternalBypass;
7872
7895
  exports.FilterStatusOK = FilterStatusOK;
7873
7896
  exports.FilterStatusUnknown = FilterStatusUnknown;
7897
+ exports.GATEWAY_REQUEST_ID_HEADER = GATEWAY_REQUEST_ID_HEADER;
7874
7898
  exports.IdempotencyKeyHeader = IdempotencyKeyHeader;
7875
7899
  exports.InMemoryTokenStore = InMemoryTokenStore;
7876
7900
  exports.LocalStorageTokenStore = LocalStorageTokenStore;