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/README.md CHANGED
@@ -154,6 +154,11 @@ Image generation is non-streaming — the response is returned as a single JSON
154
154
  | `baseURL` | — | `https://api.pendra.ai` |
155
155
  | `timeout` | — | `120000` (ms) |
156
156
 
157
+ For a streaming request, `timeout` covers waiting for the response to start and
158
+ then any gap between chunks, not the length of the whole stream, so long
159
+ generations are not cut off. Breaking out of a stream's `for await` loop early
160
+ cancels the request.
161
+
157
162
  ## Migrating from OpenAI
158
163
 
159
164
  ```diff
package/dist/index.d.mts CHANGED
@@ -376,6 +376,14 @@ interface ChatCompletionCreateParams {
376
376
  * chunk. The error frames used to be yielded *as* chunks (`JSON.parse` succeeds
377
377
  * on them), so a worker crash surfaced as a `TypeError` on `chunk.choices[0]`
378
378
  * rather than as something a caller could catch and read (issue #2252).
379
+ *
380
+ * `idleTimeoutMs` bounds the gap between reads, not the whole stream: the API
381
+ * sends a keep-alive comment every 15 s, so a healthy stream never goes quiet
382
+ * for long however long it runs, while a stalled one fails with an
383
+ * `APIConnectionError` rather than hanging (#2185). Leaving the loop before the
384
+ * stream ends — a `break`, or a frame that throws — aborts the request, so the
385
+ * connection is released and the worker stops generating tokens nobody will
386
+ * read (#2216).
379
387
  */
380
388
  declare class Stream implements AsyncIterable<ChatCompletionChunk> {
381
389
  private reader;
@@ -383,7 +391,8 @@ declare class Stream implements AsyncIterable<ChatCompletionChunk> {
383
391
  private sse;
384
392
  private done;
385
393
  private controller;
386
- constructor(response: Response, controller: AbortController);
394
+ private idleTimeoutMs;
395
+ constructor(response: Response, controller: AbortController, idleTimeoutMs?: number);
387
396
  [Symbol.asyncIterator](): AsyncIterator<ChatCompletionChunk>;
388
397
  abort(): void;
389
398
  }
@@ -699,7 +708,6 @@ declare class Pendra {
699
708
  _fetch(path: string, options: {
700
709
  method: string;
701
710
  body?: Record<string, unknown>;
702
- stream?: boolean;
703
711
  headers?: Record<string, string>;
704
712
  }): Promise<{
705
713
  response: Response;
package/dist/index.d.ts CHANGED
@@ -376,6 +376,14 @@ interface ChatCompletionCreateParams {
376
376
  * chunk. The error frames used to be yielded *as* chunks (`JSON.parse` succeeds
377
377
  * on them), so a worker crash surfaced as a `TypeError` on `chunk.choices[0]`
378
378
  * rather than as something a caller could catch and read (issue #2252).
379
+ *
380
+ * `idleTimeoutMs` bounds the gap between reads, not the whole stream: the API
381
+ * sends a keep-alive comment every 15 s, so a healthy stream never goes quiet
382
+ * for long however long it runs, while a stalled one fails with an
383
+ * `APIConnectionError` rather than hanging (#2185). Leaving the loop before the
384
+ * stream ends — a `break`, or a frame that throws — aborts the request, so the
385
+ * connection is released and the worker stops generating tokens nobody will
386
+ * read (#2216).
379
387
  */
380
388
  declare class Stream implements AsyncIterable<ChatCompletionChunk> {
381
389
  private reader;
@@ -383,7 +391,8 @@ declare class Stream implements AsyncIterable<ChatCompletionChunk> {
383
391
  private sse;
384
392
  private done;
385
393
  private controller;
386
- constructor(response: Response, controller: AbortController);
394
+ private idleTimeoutMs;
395
+ constructor(response: Response, controller: AbortController, idleTimeoutMs?: number);
387
396
  [Symbol.asyncIterator](): AsyncIterator<ChatCompletionChunk>;
388
397
  abort(): void;
389
398
  }
@@ -699,7 +708,6 @@ declare class Pendra {
699
708
  _fetch(path: string, options: {
700
709
  method: string;
701
710
  body?: Record<string, unknown>;
702
- stream?: boolean;
703
711
  headers?: Record<string, string>;
704
712
  }): Promise<{
705
713
  response: Response;
package/dist/index.js CHANGED
@@ -385,7 +385,7 @@ var SseDecoder = class {
385
385
 
386
386
  // src/streaming.ts
387
387
  var Stream = class {
388
- constructor(response, controller) {
388
+ constructor(response, controller, idleTimeoutMs) {
389
389
  this.decoder = new TextDecoder();
390
390
  this.sse = new SseDecoder();
391
391
  this.done = false;
@@ -394,11 +394,15 @@ var Stream = class {
394
394
  }
395
395
  this.reader = response.body.getReader();
396
396
  this.controller = controller;
397
+ this.idleTimeoutMs = idleTimeoutMs;
397
398
  }
398
399
  async *[Symbol.asyncIterator]() {
399
400
  try {
400
401
  while (!this.done) {
401
- const { value, done: readerDone } = await this.reader.read();
402
+ const { value, done: readerDone } = await readWithIdleTimeout(
403
+ this.reader,
404
+ this.idleTimeoutMs
405
+ );
402
406
  const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
403
407
  for (const data of payloads) {
404
408
  if (data === "[DONE]") {
@@ -413,7 +417,7 @@ var Stream = class {
413
417
  }
414
418
  }
415
419
  } finally {
416
- this.reader.releaseLock();
420
+ releaseReader(this.reader, this.controller, this.done);
417
421
  }
418
422
  }
419
423
  abort() {
@@ -421,11 +425,34 @@ var Stream = class {
421
425
  this.controller.abort();
422
426
  }
423
427
  };
428
+ async function readWithIdleTimeout(reader, idleTimeoutMs) {
429
+ if (!idleTimeoutMs) return reader.read();
430
+ let timer;
431
+ const idle = new Promise((_, reject) => {
432
+ timer = setTimeout(
433
+ () => reject(new APIConnectionError(`Stream received no data for ${idleTimeoutMs}ms`)),
434
+ idleTimeoutMs
435
+ );
436
+ });
437
+ try {
438
+ return await Promise.race([reader.read(), idle]);
439
+ } finally {
440
+ clearTimeout(timer);
441
+ }
442
+ }
443
+ function releaseReader(reader, controller, finished) {
444
+ if (!finished) {
445
+ controller.abort();
446
+ reader.cancel().catch(() => {
447
+ });
448
+ }
449
+ reader.releaseLock();
450
+ }
424
451
 
425
452
  // src/_privateStreaming.ts
426
453
  var DECODER = new TextDecoder();
427
454
  var EncryptedStream = class {
428
- constructor(response, controller, decryptor) {
455
+ constructor(response, controller, decryptor, idleTimeoutMs) {
429
456
  this.decoder = new TextDecoder();
430
457
  this.sse = new SseDecoder();
431
458
  this.done = false;
@@ -435,11 +462,15 @@ var EncryptedStream = class {
435
462
  this.reader = response.body.getReader();
436
463
  this.controller = controller;
437
464
  this.decryptor = decryptor;
465
+ this.idleTimeoutMs = idleTimeoutMs;
438
466
  }
439
467
  async *[Symbol.asyncIterator]() {
440
468
  try {
441
469
  while (!this.done) {
442
- const { value, done: readerDone } = await this.reader.read();
470
+ const { value, done: readerDone } = await readWithIdleTimeout(
471
+ this.reader,
472
+ this.idleTimeoutMs
473
+ );
443
474
  const payloads = readerDone ? this.sse.flush() : this.sse.push(this.decoder.decode(value, { stream: true }));
444
475
  for (const data of payloads) {
445
476
  if (data === "[DONE]") {
@@ -475,7 +506,7 @@ var EncryptedStream = class {
475
506
  }
476
507
  }
477
508
  } finally {
478
- this.reader.releaseLock();
509
+ releaseReader(this.reader, this.controller, this.done);
479
510
  }
480
511
  }
481
512
  abort() {
@@ -504,9 +535,9 @@ var Completions = class {
504
535
  if (params.stream) {
505
536
  const { response: response2, controller } = await this.client._fetch(
506
537
  "/api/v1/chat/completions",
507
- { method: "POST", body, stream: true, headers }
538
+ { method: "POST", body, headers }
508
539
  );
509
- return new Stream(response2, controller);
540
+ return new Stream(response2, controller, this.client.timeout);
510
541
  }
511
542
  const { response } = await this.client._fetch(
512
543
  "/api/v1/chat/completions",
@@ -527,9 +558,9 @@ var Completions = class {
527
558
  if (stream) {
528
559
  const { response: response2, controller } = await this.client._fetch(
529
560
  "/api/v1/chat/completions",
530
- { method: "POST", body: topLevel, stream: true, headers }
561
+ { method: "POST", body: topLevel, headers }
531
562
  );
532
- return new EncryptedStream(response2, controller, decryptor);
563
+ return new EncryptedStream(response2, controller, decryptor, this.client.timeout);
533
564
  }
534
565
  const { response } = await this.client._fetch(
535
566
  "/api/v1/chat/completions",
@@ -783,7 +814,7 @@ var Rerank = class {
783
814
  };
784
815
 
785
816
  // src/version.ts
786
- var VERSION = "0.13.0";
817
+ var VERSION = "0.13.1";
787
818
  var USER_AGENT = `pendra-node/${VERSION}`;
788
819
 
789
820
  // src/_hpke.ts
@@ -1474,11 +1505,8 @@ var Pendra = class {
1474
1505
  err instanceof Error ? err.message : "Failed to connect to the Pendra API"
1475
1506
  );
1476
1507
  }
1477
- if (!options.stream) {
1478
- clearTimeout(timeoutId);
1479
- }
1508
+ clearTimeout(timeoutId);
1480
1509
  if (!response.ok) {
1481
- clearTimeout(timeoutId);
1482
1510
  this.invalidateKeyCacheIfRekeyed(response);
1483
1511
  let message;
1484
1512
  try {