agents 0.14.1 → 0.14.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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/dist/{agent-tool-types-BAJWu8s4.d.ts → agent-tool-types-V25Z_HcX.d.ts} +258 -115
  3. package/dist/agent-tool-types.d.ts +13 -11
  4. package/dist/{agent-tools-DYrkT-Kx.js → agent-tools-3zLG7MgA.js} +4 -2
  5. package/dist/{agent-tools-DYrkT-Kx.js.map → agent-tools-3zLG7MgA.js.map} +1 -1
  6. package/dist/{agent-tools-0R6KEert.d.ts → agent-tools-C-9s151X.d.ts} +2 -2
  7. package/dist/agent-tools.d.ts +13 -11
  8. package/dist/agent-tools.js +8 -3
  9. package/dist/agent-tools.js.map +1 -1
  10. package/dist/browser/ai.js +1 -1
  11. package/dist/browser/ai.js.map +1 -1
  12. package/dist/browser/index.js +1 -1
  13. package/dist/browser/tanstack-ai.js +1 -1
  14. package/dist/browser/tanstack-ai.js.map +1 -1
  15. package/dist/chat/index.d.ts +133 -11
  16. package/dist/chat/index.js +118 -29
  17. package/dist/chat/index.js.map +1 -1
  18. package/dist/chat-sdk/index.d.ts +4 -4
  19. package/dist/chat-sdk/index.js.map +1 -1
  20. package/dist/{classPrivateFieldGet2-D_obpP6O.js → classPrivateFieldGet2-Beqsfu2Z.js} +5 -5
  21. package/dist/{classPrivateMethodInitSpec-10iTYB7F.js → classPrivateMethodInitSpec-B5ko1s2R.js} +2 -2
  22. package/dist/cli/index.js.map +1 -1
  23. package/dist/client-FUizKzj2.js.map +1 -1
  24. package/dist/client.d.ts +1 -1
  25. package/dist/compaction-helpers-iiKMr2TQ.js.map +1 -1
  26. package/dist/email.js.map +1 -1
  27. package/dist/experimental/memory/session/index.js.map +1 -1
  28. package/dist/experimental/webmcp.js.map +1 -1
  29. package/dist/{index-RJ4OxMOe.d.ts → index-CPe1OtI0.d.ts} +17 -1
  30. package/dist/index.d.ts +66 -64
  31. package/dist/index.js +263 -76
  32. package/dist/index.js.map +1 -1
  33. package/dist/mcp/client.d.ts +14 -14
  34. package/dist/mcp/do-oauth-client-provider.js.map +1 -1
  35. package/dist/mcp/index.d.ts +30 -30
  36. package/dist/mcp/index.js.map +1 -1
  37. package/dist/mcp/x402.js.map +1 -1
  38. package/dist/observability/index.d.ts +1 -1
  39. package/dist/observability/index.js.map +1 -1
  40. package/dist/react.d.ts +3 -3
  41. package/dist/react.js +1 -1
  42. package/dist/react.js.map +1 -1
  43. package/dist/schedule.js.map +1 -1
  44. package/dist/serializable.d.ts +1 -1
  45. package/dist/{shared-BIpUk4G5.js → shared-wyII629d.js} +3 -3
  46. package/dist/{shared-BIpUk4G5.js.map → shared-wyII629d.js.map} +1 -1
  47. package/dist/skills/index.js +4 -4
  48. package/dist/skills/index.js.map +1 -1
  49. package/dist/sub-routing.d.ts +6 -6
  50. package/dist/sub-routing.js.map +1 -1
  51. package/dist/tool-output-truncation-CNnnGZQ3.js.map +1 -1
  52. package/dist/utils.js.map +1 -1
  53. package/dist/vite.d.ts +15 -4
  54. package/dist/vite.js +37 -7
  55. package/dist/vite.js.map +1 -1
  56. package/dist/workflows.d.ts +10 -2
  57. package/dist/workflows.js +48 -22
  58. package/dist/workflows.js.map +1 -1
  59. package/package.json +9 -17
@@ -1,4 +1,4 @@
1
- import { a as normalizeToolInput, i as isReplayChunk, n as createAgentToolEventState, r as applyChunkToParts, t as applyAgentToolEvent } from "../agent-tools-DYrkT-Kx.js";
1
+ import { a as normalizeToolInput, i as isReplayChunk, n as createAgentToolEventState, r as applyChunkToParts, t as applyAgentToolEvent } from "../agent-tools-3zLG7MgA.js";
2
2
  import { t as truncateToolOutput } from "../tool-output-truncation-CNnnGZQ3.js";
3
3
  import { jsonSchema, tool } from "ai";
4
4
  import { nanoid } from "nanoid";
@@ -536,16 +536,39 @@ const CHAT_MESSAGE_TYPES = {
536
536
  * - Stale stream cleanup
537
537
  * - Active stream restoration after agent restart
538
538
  */
539
- /** Number of chunks to buffer before flushing to SQLite */
539
+ /** Number of chunks to pack into a single SQLite row before flushing */
540
540
  const CHUNK_BUFFER_SIZE = 10;
541
541
  /** Maximum buffer size to prevent memory issues on rapid reconnections */
542
542
  const CHUNK_BUFFER_MAX_SIZE = 100;
543
+ /**
544
+ * Max accumulated raw chunk bytes packed into one row before forcing a flush.
545
+ * The SQLite row limit is 2 MB; packing serializes bodies into a JSON array,
546
+ * which re-escapes their contents (quotes/backslashes), so we keep the raw
547
+ * total well under the limit to leave generous headroom for escaping overhead.
548
+ * A chunk larger than this is flushed as its own (unwrapped) row.
549
+ */
550
+ const SEGMENT_MAX_BYTES = 512e3;
543
551
  /** Default cleanup interval for old streams (ms) - every 10 minutes */
544
552
  const CLEANUP_INTERVAL_MS = 600 * 1e3;
545
553
  /** Default age threshold for cleaning up completed streams (ms) - 24 hours */
546
554
  const CLEANUP_AGE_THRESHOLD_MS = 1440 * 60 * 1e3;
547
555
  /** Shared encoder for UTF-8 byte length measurement */
548
556
  const textEncoder = new TextEncoder();
557
+ /**
558
+ * A stored row body is either a single chunk body (a JSON object string —
559
+ * legacy per-chunk rows and single-chunk segments) or a packed segment (a JSON
560
+ * array of chunk body strings). Unpack to the individual chunk bodies in order.
561
+ *
562
+ * Stored chunk bodies are always serialized JSON *objects*, never arrays, so
563
+ * `Array.isArray` reliably distinguishes a packed segment from a single body.
564
+ */
565
+ function unpackSegmentBody(rowBody) {
566
+ try {
567
+ const parsed = JSON.parse(rowBody);
568
+ if (Array.isArray(parsed)) return parsed;
569
+ } catch {}
570
+ return [rowBody];
571
+ }
549
572
  function sendIfOpen$1(connection, message) {
550
573
  try {
551
574
  connection.send(message);
@@ -563,9 +586,10 @@ var ResumableStream = class ResumableStream {
563
586
  this.sql = sql;
564
587
  this._activeStreamId = null;
565
588
  this._activeRequestId = null;
566
- this._streamChunkIndex = 0;
589
+ this._segmentIndex = 0;
567
590
  this._isLive = false;
568
591
  this._chunkBuffer = [];
592
+ this._chunkBufferBytes = 0;
569
593
  this._isFlushingChunks = false;
570
594
  this._lastCleanupTime = 0;
571
595
  this.sql`create table if not exists cf_ai_chat_stream_chunks (
@@ -613,7 +637,7 @@ var ResumableStream = class ResumableStream {
613
637
  const streamId = nanoid();
614
638
  this._activeStreamId = streamId;
615
639
  this._activeRequestId = requestId;
616
- this._streamChunkIndex = 0;
640
+ this._segmentIndex = 0;
617
641
  this._isLive = true;
618
642
  this.sql`
619
643
  insert into cf_ai_chat_stream_metadata (id, request_id, status, created_at)
@@ -634,7 +658,7 @@ var ResumableStream = class ResumableStream {
634
658
  `;
635
659
  this._activeStreamId = null;
636
660
  this._activeRequestId = null;
637
- this._streamChunkIndex = 0;
661
+ this._segmentIndex = 0;
638
662
  this._isLive = false;
639
663
  this._maybeCleanupOldStreams();
640
664
  }
@@ -651,7 +675,7 @@ var ResumableStream = class ResumableStream {
651
675
  `;
652
676
  this._activeStreamId = null;
653
677
  this._activeRequestId = null;
654
- this._streamChunkIndex = 0;
678
+ this._segmentIndex = 0;
655
679
  this._isLive = false;
656
680
  }
657
681
  /**
@@ -669,18 +693,22 @@ var ResumableStream = class ResumableStream {
669
693
  return;
670
694
  }
671
695
  if (this._chunkBuffer.length >= CHUNK_BUFFER_MAX_SIZE) this.flushBuffer();
696
+ if (this._chunkBuffer.length > 0 && this._chunkBufferBytes + bodyBytes > SEGMENT_MAX_BYTES) this.flushBuffer();
672
697
  this._chunkBuffer.push({
673
- id: nanoid(),
674
698
  streamId,
675
- body,
676
- index: this._streamChunkIndex
699
+ body
677
700
  });
678
- this._streamChunkIndex++;
701
+ this._chunkBufferBytes += bodyBytes;
679
702
  if (this._chunkBuffer.length >= CHUNK_BUFFER_SIZE) this.flushBuffer();
680
703
  }
681
704
  /**
682
- * Flush buffered chunks to SQLite in a single batch.
705
+ * Flush the buffered chunks to SQLite as a single packed row.
683
706
  * Uses a lock to prevent concurrent flush operations.
707
+ *
708
+ * The whole buffer becomes one row: a single-chunk segment is stored
709
+ * unwrapped (legacy object format) so a large chunk avoids array-escaping
710
+ * inflation, while a multi-chunk segment stores a JSON array of bodies. This
711
+ * collapses N chunk rows into one, cutting rows written / stored / scanned.
684
712
  */
685
713
  flushBuffer() {
686
714
  if (this._isFlushingChunks || this._chunkBuffer.length === 0) return;
@@ -688,11 +716,14 @@ var ResumableStream = class ResumableStream {
688
716
  try {
689
717
  const chunks = this._chunkBuffer;
690
718
  this._chunkBuffer = [];
691
- const now = Date.now();
692
- for (const chunk of chunks) this.sql`
693
- insert into cf_ai_chat_stream_chunks (id, stream_id, body, chunk_index, created_at)
694
- values (${chunk.id}, ${chunk.streamId}, ${chunk.body}, ${chunk.index}, ${now})
695
- `;
719
+ this._chunkBufferBytes = 0;
720
+ const streamId = chunks[0].streamId;
721
+ const segmentBody = chunks.length === 1 ? chunks[0].body : JSON.stringify(chunks.map((chunk) => chunk.body));
722
+ this.sql`
723
+ insert into cf_ai_chat_stream_chunks (id, stream_id, body, chunk_index, created_at)
724
+ values (${nanoid()}, ${streamId}, ${segmentBody}, ${this._segmentIndex}, ${Date.now()})
725
+ `;
726
+ this._segmentIndex++;
696
727
  } finally {
697
728
  this._isFlushingChunks = false;
698
729
  }
@@ -727,8 +758,8 @@ var ResumableStream = class ResumableStream {
727
758
  where stream_id = ${streamId}
728
759
  order by chunk_index asc
729
760
  `;
730
- for (const chunk of chunks || []) if (!sendIfOpen$1(connection, JSON.stringify({
731
- body: chunk.body,
761
+ for (const chunk of chunks || []) for (const body of unpackSegmentBody(chunk.body)) if (!sendIfOpen$1(connection, JSON.stringify({
762
+ body,
732
763
  done: false,
733
764
  id: requestId,
734
765
  type: CHAT_MESSAGE_TYPES.USE_CHAT_RESPONSE,
@@ -780,8 +811,8 @@ var ResumableStream = class ResumableStream {
780
811
  where stream_id = ${stream.id}
781
812
  order by chunk_index asc
782
813
  `;
783
- for (const chunk of chunks || []) if (!sendIfOpen$1(connection, JSON.stringify({
784
- body: chunk.body,
814
+ for (const chunk of chunks || []) for (const body of unpackSegmentBody(chunk.body)) if (!sendIfOpen$1(connection, JSON.stringify({
815
+ body,
785
816
  done: false,
786
817
  id: requestId,
787
818
  type: CHAT_MESSAGE_TYPES.USE_CHAT_RESPONSE,
@@ -816,7 +847,7 @@ var ResumableStream = class ResumableStream {
816
847
  from cf_ai_chat_stream_chunks
817
848
  where stream_id = ${this._activeStreamId}
818
849
  `;
819
- this._streamChunkIndex = lastChunk && lastChunk[0]?.max_index != null ? lastChunk[0].max_index + 1 : 0;
850
+ this._segmentIndex = lastChunk && lastChunk[0]?.max_index != null ? lastChunk[0].max_index + 1 : 0;
820
851
  }
821
852
  }
822
853
  /**
@@ -824,11 +855,12 @@ var ResumableStream = class ResumableStream {
824
855
  */
825
856
  clearAll() {
826
857
  this._chunkBuffer = [];
858
+ this._chunkBufferBytes = 0;
827
859
  this.sql`delete from cf_ai_chat_stream_chunks`;
828
860
  this.sql`delete from cf_ai_chat_stream_metadata`;
829
861
  this._activeStreamId = null;
830
862
  this._activeRequestId = null;
831
- this._streamChunkIndex = 0;
863
+ this._segmentIndex = 0;
832
864
  }
833
865
  /**
834
866
  * Drop all stream tables (called on destroy).
@@ -868,13 +900,28 @@ var ResumableStream = class ResumableStream {
868
900
  where status = 'streaming' and created_at < ${cutoff}
869
901
  `;
870
902
  }
871
- /** @internal For testing only */
903
+ /**
904
+ * Return the stored chunks for a stream as individual chunk bodies in order,
905
+ * unpacking packed segment rows. The returned `chunk_index` is a running
906
+ * per-chunk sequence (0, 1, 2, …) — stable across calls because rows are
907
+ * append-only — so callers can use it as a monotonic chunk sequence.
908
+ */
872
909
  getStreamChunks(streamId) {
873
- return this.sql`
874
- select body, chunk_index from cf_ai_chat_stream_chunks
910
+ const rows = this.sql`
911
+ select body from cf_ai_chat_stream_chunks
875
912
  where stream_id = ${streamId}
876
913
  order by chunk_index asc
877
914
  ` || [];
915
+ const out = [];
916
+ let index = 0;
917
+ for (const row of rows) for (const body of unpackSegmentBody(row.body)) {
918
+ out.push({
919
+ body,
920
+ chunk_index: index
921
+ });
922
+ index++;
923
+ }
924
+ return out;
878
925
  }
879
926
  /** @internal For testing only */
880
927
  getStreamMetadata(streamId) {
@@ -899,6 +946,44 @@ var ResumableStream = class ResumableStream {
899
946
  };
900
947
  ResumableStream.CHUNK_MAX_BYTES = 18e5;
901
948
  //#endregion
949
+ //#region src/chat/sql-batch.ts
950
+ /**
951
+ * Helpers for building batched SQLite statements that run through the Agent's
952
+ * `sql` tagged template (which interleaves a `?` placeholder between every
953
+ * string fragment). Used to collapse per-row INSERT/DELETE loops into a small
954
+ * number of multi-row statements.
955
+ *
956
+ * SQLite (Durable Object / D1) caps bound parameters at 100 per query, so
957
+ * callers must chunk their inputs to stay within {@link MAX_BOUND_PARAMS}.
958
+ * See https://developers.cloudflare.com/d1/platform/limits/
959
+ */
960
+ /** Maximum bound parameters allowed in a single SQLite (DO / D1) query. */
961
+ const MAX_BOUND_PARAMS = 100;
962
+ /**
963
+ * Attach a self-referential `raw` property so a plain string[] satisfies the
964
+ * TemplateStringsArray shape. `sql` only reads indexed string fragments, so
965
+ * `raw` is never consumed — this just keeps the type system happy.
966
+ */
967
+ function asTemplateStringsArray(parts) {
968
+ parts.raw = parts;
969
+ return parts;
970
+ }
971
+ /**
972
+ * Build a TemplateStringsArray for a single-column `IN (...)` clause. Produces
973
+ * fragments for:
974
+ * `${prefix}(?, ?, ...)`
975
+ *
976
+ * @throws if `count` is less than 1.
977
+ */
978
+ function buildInClauseStrings(prefix, count) {
979
+ if (count < 1) throw new Error(`buildInClauseStrings requires count >= 1 (got ${count})`);
980
+ const parts = new Array(count + 1);
981
+ parts[0] = `${prefix}(`;
982
+ for (let i = 1; i < count; i++) parts[i] = ", ";
983
+ parts[count] = ")";
984
+ return asTemplateStringsArray(parts);
985
+ }
986
+ //#endregion
902
987
  //#region src/chat/client-tools.ts
903
988
  /**
904
989
  * Converts client tool schemas to AI SDK tool format.
@@ -1082,9 +1167,13 @@ var AbortRegistry = class {
1082
1167
  remove(id) {
1083
1168
  this.controllers.delete(id);
1084
1169
  }
1085
- /** Abort all pending requests and clear the registry. */
1086
- destroyAll() {
1087
- for (const controller of this.controllers.values()) controller.abort();
1170
+ /**
1171
+ * Abort all pending requests and clear the registry. Optionally propagate a
1172
+ * reason surfaces as `signal.reason` on each controller and through any
1173
+ * `AbortError` it produces downstream, exactly like {@link cancel}.
1174
+ */
1175
+ destroyAll(reason) {
1176
+ for (const controller of this.controllers.values()) controller.abort(reason);
1088
1177
  this.controllers.clear();
1089
1178
  }
1090
1179
  /** Check if a controller exists for the given ID. */
@@ -1511,6 +1600,6 @@ function unwrapChatFiberSnapshot(key, value, expectedKind) {
1511
1600
  };
1512
1601
  }
1513
1602
  //#endregion
1514
- export { AbortRegistry, CHAT_MESSAGE_TYPES, ContinuationState, ROW_MAX_BYTES, ResumableStream, StreamAccumulator, SubmitConcurrencyController, TurnQueue, applyAgentToolEvent, applyChunkToParts, applyToolUpdate, assistantContentKey, transition as broadcastTransition, byteLength, createAgentToolEventState, createChatFiberSnapshot, createToolsFromClientSchemas, crossMessageToolResultUpdate, enforceRowSizeLimit, isReplayChunk, normalizeToolInput, parseProtocolMessage, reconcileMessages, resolveToolMergeId, sanitizeMessage, toolApprovalUpdate, toolResultUpdate, unwrapChatFiberSnapshot, wrapChatFiberSnapshot };
1603
+ export { AbortRegistry, CHAT_MESSAGE_TYPES, ContinuationState, MAX_BOUND_PARAMS, ROW_MAX_BYTES, ResumableStream, StreamAccumulator, SubmitConcurrencyController, TurnQueue, applyAgentToolEvent, applyChunkToParts, applyToolUpdate, assistantContentKey, transition as broadcastTransition, buildInClauseStrings, byteLength, createAgentToolEventState, createChatFiberSnapshot, createToolsFromClientSchemas, crossMessageToolResultUpdate, enforceRowSizeLimit, isReplayChunk, normalizeToolInput, parseProtocolMessage, reconcileMessages, resolveToolMergeId, sanitizeMessage, toolApprovalUpdate, toolResultUpdate, unwrapChatFiberSnapshot, wrapChatFiberSnapshot };
1515
1604
 
1516
1605
  //# sourceMappingURL=index.js.map