@prestyj/ai 4.12.0 → 4.13.3

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.cjs CHANGED
@@ -336,7 +336,7 @@ var EventStream = class {
336
336
  }
337
337
  }
338
338
  };
339
- var StreamResult = class {
339
+ var StreamResult = class _StreamResult {
340
340
  response;
341
341
  buffer = [];
342
342
  done = false;
@@ -344,6 +344,18 @@ var StreamResult = class {
344
344
  resolveResponse;
345
345
  rejectResponse;
346
346
  resolveWait = null;
347
+ /**
348
+ * High-water mark: when the buffer exceeds this many unconsumed events,
349
+ * the pump pauses until the consumer drains below the low-water mark.
350
+ * Prevents unbounded memory growth when a consumer is slow.
351
+ * Only active when someone IS iterating — if nobody iterates (the `then()`
352
+ * path), backpressure is skipped so the pump can complete and resolve.
353
+ */
354
+ static HIGH_WATER = 5e3;
355
+ static LOW_WATER = 1e3;
356
+ iterating = false;
357
+ paused = false;
358
+ resolveDrain = null;
347
359
  constructor(generator, signal) {
348
360
  this.response = new Promise((resolve, reject) => {
349
361
  this.resolveResponse = resolve;
@@ -358,6 +370,13 @@ var StreamResult = class {
358
370
  this.buffer.push(next.value);
359
371
  this.resolveWait?.();
360
372
  this.resolveWait = null;
373
+ if (this.iterating && this.buffer.length > _StreamResult.HIGH_WATER) {
374
+ this.paused = true;
375
+ await new Promise((r) => {
376
+ this.resolveDrain = r;
377
+ });
378
+ this.paused = false;
379
+ }
361
380
  next = await this._nextWithAbort(generator, signal);
362
381
  }
363
382
  this.done = true;
@@ -396,11 +415,20 @@ var StreamResult = class {
396
415
  }
397
416
  }
398
417
  async *[Symbol.asyncIterator]() {
418
+ this.iterating = true;
399
419
  let index = 0;
400
420
  while (true) {
401
421
  while (index < this.buffer.length) {
402
422
  yield this.buffer[index++];
403
423
  }
424
+ if (this.paused && index > _StreamResult.LOW_WATER) {
425
+ this.resolveDrain?.();
426
+ this.resolveDrain = null;
427
+ }
428
+ if (index > 0 && !this.paused) {
429
+ this.buffer.splice(0, index);
430
+ index = 0;
431
+ }
404
432
  if (this.error) throw this.error;
405
433
  if (this.done) return;
406
434
  await new Promise((r) => {
@@ -413,6 +441,11 @@ var StreamResult = class {
413
441
  }
414
442
  }
415
443
  then(onfulfilled, onrejected) {
444
+ if (this.paused) {
445
+ this.paused = false;
446
+ this.resolveDrain?.();
447
+ this.resolveDrain = null;
448
+ }
416
449
  return this.response.then(onfulfilled, onrejected);
417
450
  }
418
451
  };
@@ -1035,6 +1068,7 @@ function parseToolArguments(argsJson) {
1035
1068
  }
1036
1069
 
1037
1070
  // src/providers/anthropic.ts
1071
+ var NON_STREAMING_TIMEOUT_MS = 60 * 60 * 1e3;
1038
1072
  var anthropicClientCache = /* @__PURE__ */ new Map();
1039
1073
  function createClient(options) {
1040
1074
  const isOAuth = options.apiKey?.startsWith("sk-ant-oat");
@@ -1201,8 +1235,9 @@ async function* runStream(options) {
1201
1235
  ...betaHeaders.length ? { headers: { "anthropic-beta": betaHeaders.join(",") } } : {}
1202
1236
  };
1203
1237
  if (!useStreaming) {
1238
+ const nonStreamingClient = client.withOptions({ timeout: NON_STREAMING_TIMEOUT_MS });
1204
1239
  try {
1205
- const message = await client.messages.create(
1240
+ const message = await nonStreamingClient.messages.create(
1206
1241
  { ...params, stream: false },
1207
1242
  requestOptions
1208
1243
  );