pendra 0.13.0 → 0.13.1

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
@@ -339,7 +339,7 @@ var SseDecoder = class {
339
339
 
340
340
  // src/streaming.ts
341
341
  var Stream = class {
342
- constructor(response, controller) {
342
+ constructor(response, controller, idleTimeoutMs) {
343
343
  this.decoder = new TextDecoder();
344
344
  this.sse = new SseDecoder();
345
345
  this.done = false;
@@ -348,11 +348,15 @@ var Stream = class {
348
348
  }
349
349
  this.reader = response.body.getReader();
350
350
  this.controller = controller;
351
+ this.idleTimeoutMs = idleTimeoutMs;
351
352
  }
352
353
  async *[Symbol.asyncIterator]() {
353
354
  try {
354
355
  while (!this.done) {
355
- const { value, done: readerDone } = await this.reader.read();
356
+ const { value, done: readerDone } = await readWithIdleTimeout(
357
+ this.reader,
358
+ this.idleTimeoutMs
359
+ );
356
360
  const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
357
361
  for (const data of payloads) {
358
362
  if (data === "[DONE]") {
@@ -367,7 +371,7 @@ var Stream = class {
367
371
  }
368
372
  }
369
373
  } finally {
370
- this.reader.releaseLock();
374
+ releaseReader(this.reader, this.controller, this.done);
371
375
  }
372
376
  }
373
377
  abort() {
@@ -375,11 +379,34 @@ var Stream = class {
375
379
  this.controller.abort();
376
380
  }
377
381
  };
382
+ async function readWithIdleTimeout(reader, idleTimeoutMs) {
383
+ if (!idleTimeoutMs) return reader.read();
384
+ let timer;
385
+ const idle = new Promise((_, reject) => {
386
+ timer = setTimeout(
387
+ () => reject(new APIConnectionError(`Stream received no data for ${idleTimeoutMs}ms`)),
388
+ idleTimeoutMs
389
+ );
390
+ });
391
+ try {
392
+ return await Promise.race([reader.read(), idle]);
393
+ } finally {
394
+ clearTimeout(timer);
395
+ }
396
+ }
397
+ function releaseReader(reader, controller, finished) {
398
+ if (!finished) {
399
+ controller.abort();
400
+ reader.cancel().catch(() => {
401
+ });
402
+ }
403
+ reader.releaseLock();
404
+ }
378
405
 
379
406
  // src/_privateStreaming.ts
380
407
  var DECODER = new TextDecoder();
381
408
  var EncryptedStream = class {
382
- constructor(response, controller, decryptor) {
409
+ constructor(response, controller, decryptor, idleTimeoutMs) {
383
410
  this.decoder = new TextDecoder();
384
411
  this.sse = new SseDecoder();
385
412
  this.done = false;
@@ -389,11 +416,15 @@ var EncryptedStream = class {
389
416
  this.reader = response.body.getReader();
390
417
  this.controller = controller;
391
418
  this.decryptor = decryptor;
419
+ this.idleTimeoutMs = idleTimeoutMs;
392
420
  }
393
421
  async *[Symbol.asyncIterator]() {
394
422
  try {
395
423
  while (!this.done) {
396
- const { value, done: readerDone } = await this.reader.read();
424
+ const { value, done: readerDone } = await readWithIdleTimeout(
425
+ this.reader,
426
+ this.idleTimeoutMs
427
+ );
397
428
  const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
398
429
  for (const data of payloads) {
399
430
  if (data === "[DONE]") {
@@ -429,7 +460,7 @@ var EncryptedStream = class {
429
460
  }
430
461
  }
431
462
  } finally {
432
- this.reader.releaseLock();
463
+ releaseReader(this.reader, this.controller, this.done);
433
464
  }
434
465
  }
435
466
  abort() {
@@ -458,9 +489,9 @@ var Completions = class {
458
489
  if (params.stream) {
459
490
  const { response: response2, controller } = await this.client._fetch(
460
491
  "/api/v1/chat/completions",
461
- { method: "POST", body, stream: true, headers }
492
+ { method: "POST", body, headers }
462
493
  );
463
- return new Stream(response2, controller);
494
+ return new Stream(response2, controller, this.client.timeout);
464
495
  }
465
496
  const { response } = await this.client._fetch(
466
497
  "/api/v1/chat/completions",
@@ -481,9 +512,9 @@ var Completions = class {
481
512
  if (stream) {
482
513
  const { response: response2, controller } = await this.client._fetch(
483
514
  "/api/v1/chat/completions",
484
- { method: "POST", body: topLevel, stream: true, headers }
515
+ { method: "POST", body: topLevel, headers }
485
516
  );
486
- return new EncryptedStream(response2, controller, decryptor);
517
+ return new EncryptedStream(response2, controller, decryptor, this.client.timeout);
487
518
  }
488
519
  const { response } = await this.client._fetch(
489
520
  "/api/v1/chat/completions",
@@ -737,7 +768,7 @@ var Rerank = class {
737
768
  };
738
769
 
739
770
  // src/version.ts
740
- var VERSION = "0.13.0";
771
+ var VERSION = "0.13.1";
741
772
  var USER_AGENT = `pendra-node/${VERSION}`;
742
773
 
743
774
  // src/_hpke.ts
@@ -1428,11 +1459,8 @@ var Pendra = class {
1428
1459
  err instanceof Error ? err.message : "Failed to connect to the Pendra API"
1429
1460
  );
1430
1461
  }
1431
- if (!options.stream) {
1432
- clearTimeout(timeoutId);
1433
- }
1462
+ clearTimeout(timeoutId);
1434
1463
  if (!response.ok) {
1435
- clearTimeout(timeoutId);
1436
1464
  this.invalidateKeyCacheIfRekeyed(response);
1437
1465
  let message;
1438
1466
  try {