@tanstack/ai-client 0.28.0 → 0.29.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.
@@ -1,5 +1,6 @@
1
1
  import { createResponseStreamTextDecoder, getResponseStreamReader } from "./response-stream.js";
2
2
  import { parseSseDataLine } from "./sse-utils.js";
3
+ import { normalizeMessagesDates } from "./message-date-normalizer.js";
3
4
  import { EventType, getChunkRunId, restoreInboundChunk, tanstackMetadata, uiMessagesToWire, withTanstackMetadata } from "@tanstack/ai/client";
4
5
  import { ByokMissingError, isByokMissingBody } from "@tanstack/ai/byok";
5
6
  //#region src/connection-adapters.ts
@@ -332,7 +333,7 @@ async function fetchThreadHydration(fetchClient, url, headers, credentials, thre
332
333
  pending: data.interrupts.pending
333
334
  } : null;
334
335
  return {
335
- messages: Array.isArray(data.messages) ? data.messages : [],
336
+ messages: Array.isArray(data.messages) ? normalizeMessagesDates(data.messages) : [],
336
337
  activeRun,
337
338
  interrupts
338
339
  };
@@ -479,6 +480,20 @@ function normalizeConnectionAdapter(connection) {
479
480
  if (waiter) waiter(chunk);
480
481
  else activeBuffer.push(chunk);
481
482
  }
483
+ async function waitUntilSubscriberIdle(abortSignal) {
484
+ const idle = () => activeBuffer.length === 0 && (activeWaiters.length > 0 || abortSignal?.aborted);
485
+ for (let i = 0; i < 16 && !abortSignal?.aborted; i++) {
486
+ if (idle()) return;
487
+ await Promise.resolve();
488
+ }
489
+ let macrotaskWaits = 0;
490
+ while (!abortSignal?.aborted) {
491
+ if (idle()) return;
492
+ await new Promise((resolve) => setTimeout(resolve, 0));
493
+ macrotaskWaits++;
494
+ if (activeWaiters.length === 0 && macrotaskWaits >= 32) return;
495
+ }
496
+ }
482
497
  return {
483
498
  subscribe(abortSignal) {
484
499
  const myBuffer = activeBuffer.splice(0);
@@ -536,6 +551,7 @@ function normalizeConnectionAdapter(connection) {
536
551
  } catch {}
537
552
  throw err;
538
553
  }
554
+ await waitUntilSubscriberIdle(abortSignal);
539
555
  },
540
556
  ...typeof connection.joinRun === "function" ? { joinRun: (runId, abortSignal) => connection.joinRun(runId, abortSignal) } : {},
541
557
  ...(() => {