ai 5.0.0-canary.12 → 5.0.0-canary.14

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.
@@ -275,23 +275,11 @@ function convertToCoreMessages(messages, options) {
275
275
  break;
276
276
  }
277
277
  case "reasoning": {
278
- for (const detail of part.details) {
279
- switch (detail.type) {
280
- case "text":
281
- content2.push({
282
- type: "reasoning",
283
- text: detail.text,
284
- signature: detail.signature
285
- });
286
- break;
287
- case "redacted":
288
- content2.push({
289
- type: "redacted-reasoning",
290
- data: detail.data
291
- });
292
- break;
293
- }
294
- }
278
+ content2.push({
279
+ type: "reasoning",
280
+ text: part.reasoning,
281
+ providerOptions: part.providerMetadata
282
+ });
295
283
  break;
296
284
  }
297
285
  case "tool-invocation":
@@ -561,11 +549,6 @@ var reasoningPartSchema = z5.object({
561
549
  text: z5.string(),
562
550
  providerOptions: providerMetadataSchema.optional()
563
551
  });
564
- var redactedReasoningPartSchema = z5.object({
565
- type: z5.literal("redacted-reasoning"),
566
- data: z5.string(),
567
- providerOptions: providerMetadataSchema.optional()
568
- });
569
552
  var toolCallPartSchema = z5.object({
570
553
  type: z5.literal("tool-call"),
571
554
  toolCallId: z5.string(),
@@ -606,7 +589,6 @@ var coreAssistantMessageSchema = z6.object({
606
589
  textPartSchema,
607
590
  filePartSchema,
608
591
  reasoningPartSchema,
609
- redactedReasoningPartSchema,
610
592
  toolCallPartSchema
611
593
  ])
612
594
  )
@@ -626,7 +608,7 @@ var coreMessageSchema = z6.union([
626
608
  ]);
627
609
 
628
610
  // core/prompt/standardize-prompt.ts
629
- function standardizePrompt({
611
+ async function standardizePrompt({
630
612
  prompt,
631
613
  tools
632
614
  }) {
@@ -656,7 +638,6 @@ function standardizePrompt({
656
638
  });
657
639
  }
658
640
  return {
659
- type: "prompt",
660
641
  system: prompt.system,
661
642
  messages: [
662
643
  {
@@ -683,7 +664,7 @@ function standardizePrompt({
683
664
  message: "messages must not be empty"
684
665
  });
685
666
  }
686
- const validationResult = safeValidateTypes({
667
+ const validationResult = await safeValidateTypes({
687
668
  value: messages,
688
669
  schema: z7.array(coreMessageSchema)
689
670
  });
@@ -695,7 +676,6 @@ function standardizePrompt({
695
676
  });
696
677
  }
697
678
  return {
698
- type: "messages",
699
679
  messages,
700
680
  system: prompt.system
701
681
  };
@@ -706,6 +686,262 @@ function standardizePrompt({
706
686
  // core/util/index.ts
707
687
  import { generateId } from "@ai-sdk/provider-utils";
708
688
 
689
+ // core/util/data-stream-parts.ts
690
+ var textStreamPart = {
691
+ code: "0",
692
+ name: "text",
693
+ parse: (value) => {
694
+ if (typeof value !== "string") {
695
+ throw new Error('"text" parts expect a string value.');
696
+ }
697
+ return { type: "text", value };
698
+ }
699
+ };
700
+ var dataStreamPart = {
701
+ code: "2",
702
+ name: "data",
703
+ parse: (value) => {
704
+ if (!Array.isArray(value)) {
705
+ throw new Error('"data" parts expect an array value.');
706
+ }
707
+ return { type: "data", value };
708
+ }
709
+ };
710
+ var errorStreamPart = {
711
+ code: "3",
712
+ name: "error",
713
+ parse: (value) => {
714
+ if (typeof value !== "string") {
715
+ throw new Error('"error" parts expect a string value.');
716
+ }
717
+ return { type: "error", value };
718
+ }
719
+ };
720
+ var messageAnnotationsStreamPart = {
721
+ code: "8",
722
+ name: "message_annotations",
723
+ parse: (value) => {
724
+ if (!Array.isArray(value)) {
725
+ throw new Error('"message_annotations" parts expect an array value.');
726
+ }
727
+ return { type: "message_annotations", value };
728
+ }
729
+ };
730
+ var toolCallStreamPart = {
731
+ code: "9",
732
+ name: "tool_call",
733
+ parse: (value) => {
734
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
735
+ throw new Error(
736
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
737
+ );
738
+ }
739
+ return {
740
+ type: "tool_call",
741
+ value
742
+ };
743
+ }
744
+ };
745
+ var toolResultStreamPart = {
746
+ code: "a",
747
+ name: "tool_result",
748
+ parse: (value) => {
749
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("result" in value)) {
750
+ throw new Error(
751
+ '"tool_result" parts expect an object with a "toolCallId" and a "result" property.'
752
+ );
753
+ }
754
+ return {
755
+ type: "tool_result",
756
+ value
757
+ };
758
+ }
759
+ };
760
+ var toolCallStreamingStartStreamPart = {
761
+ code: "b",
762
+ name: "tool_call_streaming_start",
763
+ parse: (value) => {
764
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string") {
765
+ throw new Error(
766
+ '"tool_call_streaming_start" parts expect an object with a "toolCallId" and "toolName" property.'
767
+ );
768
+ }
769
+ return {
770
+ type: "tool_call_streaming_start",
771
+ value
772
+ };
773
+ }
774
+ };
775
+ var toolCallDeltaStreamPart = {
776
+ code: "c",
777
+ name: "tool_call_delta",
778
+ parse: (value) => {
779
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("argsTextDelta" in value) || typeof value.argsTextDelta !== "string") {
780
+ throw new Error(
781
+ '"tool_call_delta" parts expect an object with a "toolCallId" and "argsTextDelta" property.'
782
+ );
783
+ }
784
+ return {
785
+ type: "tool_call_delta",
786
+ value
787
+ };
788
+ }
789
+ };
790
+ var finishMessageStreamPart = {
791
+ code: "d",
792
+ name: "finish_message",
793
+ parse: (value) => {
794
+ if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
795
+ throw new Error(
796
+ '"finish_message" parts expect an object with a "finishReason" property.'
797
+ );
798
+ }
799
+ const result = {
800
+ finishReason: value.finishReason
801
+ };
802
+ if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
803
+ result.usage = {
804
+ promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
805
+ completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
806
+ };
807
+ }
808
+ return {
809
+ type: "finish_message",
810
+ value: result
811
+ };
812
+ }
813
+ };
814
+ var finishStepStreamPart = {
815
+ code: "e",
816
+ name: "finish_step",
817
+ parse: (value) => {
818
+ if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
819
+ throw new Error(
820
+ '"finish_step" parts expect an object with a "finishReason" property.'
821
+ );
822
+ }
823
+ const result = {
824
+ finishReason: value.finishReason,
825
+ isContinued: false
826
+ };
827
+ if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
828
+ result.usage = {
829
+ promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
830
+ completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
831
+ };
832
+ }
833
+ if ("isContinued" in value && typeof value.isContinued === "boolean") {
834
+ result.isContinued = value.isContinued;
835
+ }
836
+ return {
837
+ type: "finish_step",
838
+ value: result
839
+ };
840
+ }
841
+ };
842
+ var startStepStreamPart = {
843
+ code: "f",
844
+ name: "start_step",
845
+ parse: (value) => {
846
+ if (value == null || typeof value !== "object" || !("messageId" in value) || typeof value.messageId !== "string") {
847
+ throw new Error(
848
+ '"start_step" parts expect an object with an "id" property.'
849
+ );
850
+ }
851
+ return {
852
+ type: "start_step",
853
+ value: {
854
+ messageId: value.messageId
855
+ }
856
+ };
857
+ }
858
+ };
859
+ var reasoningStreamPart = {
860
+ code: "g",
861
+ name: "reasoning",
862
+ parse: (value) => {
863
+ if (value == null || typeof value !== "object" || !("text" in value) || typeof value.text !== "string" || "providerMetadata" in value && typeof value.providerMetadata !== "object") {
864
+ throw new Error(
865
+ '"reasoning" parts expect an object with a "text" property.'
866
+ );
867
+ }
868
+ return {
869
+ type: "reasoning",
870
+ value: {
871
+ text: value.text,
872
+ providerMetadata: value.providerMetadata
873
+ }
874
+ };
875
+ }
876
+ };
877
+ var sourcePart = {
878
+ code: "h",
879
+ name: "source",
880
+ parse: (value) => {
881
+ if (value == null || typeof value !== "object") {
882
+ throw new Error('"source" parts expect a Source object.');
883
+ }
884
+ return {
885
+ type: "source",
886
+ value
887
+ };
888
+ }
889
+ };
890
+ var fileStreamPart = {
891
+ code: "k",
892
+ name: "file",
893
+ parse: (value) => {
894
+ if (value == null || typeof value !== "object" || !("data" in value) || typeof value.data !== "string" || !("mimeType" in value) || typeof value.mimeType !== "string") {
895
+ throw new Error(
896
+ '"file" parts expect an object with a "data" and "mimeType" property.'
897
+ );
898
+ }
899
+ return { type: "file", value };
900
+ }
901
+ };
902
+ var reasoningPartFinishStreamPart = {
903
+ code: "l",
904
+ name: "reasoning_part_finish",
905
+ parse: () => {
906
+ return {
907
+ type: "reasoning_part_finish",
908
+ value: {}
909
+ };
910
+ }
911
+ };
912
+ var dataStreamParts = [
913
+ textStreamPart,
914
+ dataStreamPart,
915
+ errorStreamPart,
916
+ messageAnnotationsStreamPart,
917
+ toolCallStreamPart,
918
+ toolResultStreamPart,
919
+ toolCallStreamingStartStreamPart,
920
+ toolCallDeltaStreamPart,
921
+ finishMessageStreamPart,
922
+ finishStepStreamPart,
923
+ startStepStreamPart,
924
+ reasoningStreamPart,
925
+ sourcePart,
926
+ reasoningPartFinishStreamPart,
927
+ fileStreamPart
928
+ ];
929
+ var dataStreamPartsByCode = Object.fromEntries(
930
+ dataStreamParts.map((part) => [part.code, part])
931
+ );
932
+ var DataStreamStringPrefixes = Object.fromEntries(
933
+ dataStreamParts.map((part) => [part.name, part.code])
934
+ );
935
+ var validCodes = dataStreamParts.map((part) => part.code);
936
+ function formatDataStreamPart(type, value) {
937
+ const streamPart = dataStreamParts.find((part) => part.name === type);
938
+ if (!streamPart) {
939
+ throw new Error(`Invalid stream part type: ${type}`);
940
+ }
941
+ return `${streamPart.code}:${JSON.stringify(value)}
942
+ `;
943
+ }
944
+
709
945
  // core/util/schema.ts
710
946
  import { validatorSymbol } from "@ai-sdk/provider-utils";
711
947
 
@@ -1278,14 +1514,6 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
1278
1514
  return {
1279
1515
  type: "reasoning",
1280
1516
  text: part.text,
1281
- signature: part.signature,
1282
- providerOptions
1283
- };
1284
- }
1285
- case "redacted-reasoning": {
1286
- return {
1287
- type: "redacted-reasoning",
1288
- data: part.data,
1289
1517
  providerOptions
1290
1518
  };
1291
1519
  }
@@ -1436,13 +1664,213 @@ function calculateLanguageModelUsage({
1436
1664
  };
1437
1665
  }
1438
1666
 
1667
+ // core/util/prepare-response-headers.ts
1668
+ function prepareResponseHeaders(headers, {
1669
+ contentType,
1670
+ dataStreamVersion
1671
+ }) {
1672
+ const responseHeaders = new Headers(headers != null ? headers : {});
1673
+ if (!responseHeaders.has("Content-Type")) {
1674
+ responseHeaders.set("Content-Type", contentType);
1675
+ }
1676
+ if (dataStreamVersion !== void 0) {
1677
+ responseHeaders.set("X-Vercel-AI-Data-Stream", dataStreamVersion);
1678
+ }
1679
+ return responseHeaders;
1680
+ }
1681
+
1682
+ // core/util/merge-streams.ts
1683
+ function mergeStreams(stream1, stream2) {
1684
+ const reader1 = stream1.getReader();
1685
+ const reader2 = stream2.getReader();
1686
+ let lastRead1 = void 0;
1687
+ let lastRead2 = void 0;
1688
+ let stream1Done = false;
1689
+ let stream2Done = false;
1690
+ async function readStream1(controller) {
1691
+ try {
1692
+ if (lastRead1 == null) {
1693
+ lastRead1 = reader1.read();
1694
+ }
1695
+ const result = await lastRead1;
1696
+ lastRead1 = void 0;
1697
+ if (!result.done) {
1698
+ controller.enqueue(result.value);
1699
+ } else {
1700
+ controller.close();
1701
+ }
1702
+ } catch (error) {
1703
+ controller.error(error);
1704
+ }
1705
+ }
1706
+ async function readStream2(controller) {
1707
+ try {
1708
+ if (lastRead2 == null) {
1709
+ lastRead2 = reader2.read();
1710
+ }
1711
+ const result = await lastRead2;
1712
+ lastRead2 = void 0;
1713
+ if (!result.done) {
1714
+ controller.enqueue(result.value);
1715
+ } else {
1716
+ controller.close();
1717
+ }
1718
+ } catch (error) {
1719
+ controller.error(error);
1720
+ }
1721
+ }
1722
+ return new ReadableStream({
1723
+ async pull(controller) {
1724
+ try {
1725
+ if (stream1Done) {
1726
+ await readStream2(controller);
1727
+ return;
1728
+ }
1729
+ if (stream2Done) {
1730
+ await readStream1(controller);
1731
+ return;
1732
+ }
1733
+ if (lastRead1 == null) {
1734
+ lastRead1 = reader1.read();
1735
+ }
1736
+ if (lastRead2 == null) {
1737
+ lastRead2 = reader2.read();
1738
+ }
1739
+ const { result, reader } = await Promise.race([
1740
+ lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
1741
+ lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
1742
+ ]);
1743
+ if (!result.done) {
1744
+ controller.enqueue(result.value);
1745
+ }
1746
+ if (reader === reader1) {
1747
+ lastRead1 = void 0;
1748
+ if (result.done) {
1749
+ await readStream2(controller);
1750
+ stream1Done = true;
1751
+ }
1752
+ } else {
1753
+ lastRead2 = void 0;
1754
+ if (result.done) {
1755
+ stream2Done = true;
1756
+ await readStream1(controller);
1757
+ }
1758
+ }
1759
+ } catch (error) {
1760
+ controller.error(error);
1761
+ }
1762
+ },
1763
+ cancel() {
1764
+ reader1.cancel();
1765
+ reader2.cancel();
1766
+ }
1767
+ });
1768
+ }
1769
+
1770
+ // streams/stream-callbacks.ts
1771
+ function createCallbacksTransformer(callbacks = {}) {
1772
+ const textEncoder = new TextEncoder();
1773
+ let aggregatedResponse = "";
1774
+ return new TransformStream({
1775
+ async start() {
1776
+ if (callbacks.onStart)
1777
+ await callbacks.onStart();
1778
+ },
1779
+ async transform(message, controller) {
1780
+ controller.enqueue(textEncoder.encode(message));
1781
+ aggregatedResponse += message;
1782
+ if (callbacks.onToken)
1783
+ await callbacks.onToken(message);
1784
+ if (callbacks.onText && typeof message === "string") {
1785
+ await callbacks.onText(message);
1786
+ }
1787
+ },
1788
+ async flush() {
1789
+ if (callbacks.onCompletion) {
1790
+ await callbacks.onCompletion(aggregatedResponse);
1791
+ }
1792
+ if (callbacks.onFinal) {
1793
+ await callbacks.onFinal(aggregatedResponse);
1794
+ }
1795
+ }
1796
+ });
1797
+ }
1798
+
1439
1799
  // util/constants.ts
1440
1800
  var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
1801
+
1802
+ // streams/stream-data.ts
1803
+ var StreamData = class {
1804
+ constructor() {
1805
+ this.encoder = new TextEncoder();
1806
+ this.controller = null;
1807
+ this.isClosed = false;
1808
+ this.warningTimeout = null;
1809
+ const self = this;
1810
+ this.stream = new ReadableStream({
1811
+ start: async (controller) => {
1812
+ self.controller = controller;
1813
+ if (process.env.NODE_ENV === "development") {
1814
+ self.warningTimeout = setTimeout(() => {
1815
+ console.warn(
1816
+ "The data stream is hanging. Did you forget to close it with `data.close()`?"
1817
+ );
1818
+ }, HANGING_STREAM_WARNING_TIME_MS);
1819
+ }
1820
+ },
1821
+ pull: (controller) => {
1822
+ },
1823
+ cancel: (reason) => {
1824
+ this.isClosed = true;
1825
+ }
1826
+ });
1827
+ }
1828
+ async close() {
1829
+ if (this.isClosed) {
1830
+ throw new Error("Data Stream has already been closed.");
1831
+ }
1832
+ if (!this.controller) {
1833
+ throw new Error("Stream controller is not initialized.");
1834
+ }
1835
+ this.controller.close();
1836
+ this.isClosed = true;
1837
+ if (this.warningTimeout) {
1838
+ clearTimeout(this.warningTimeout);
1839
+ }
1840
+ }
1841
+ append(value) {
1842
+ if (this.isClosed) {
1843
+ throw new Error("Data Stream has already been closed.");
1844
+ }
1845
+ if (!this.controller) {
1846
+ throw new Error("Stream controller is not initialized.");
1847
+ }
1848
+ this.controller.enqueue(
1849
+ this.encoder.encode(formatDataStreamPart("data", [value]))
1850
+ );
1851
+ }
1852
+ appendMessageAnnotation(value) {
1853
+ if (this.isClosed) {
1854
+ throw new Error("Data Stream has already been closed.");
1855
+ }
1856
+ if (!this.controller) {
1857
+ throw new Error("Stream controller is not initialized.");
1858
+ }
1859
+ this.controller.enqueue(
1860
+ this.encoder.encode(formatDataStreamPart("message_annotations", [value]))
1861
+ );
1862
+ }
1863
+ };
1441
1864
  export {
1442
1865
  HANGING_STREAM_WARNING_TIME_MS,
1866
+ StreamData,
1443
1867
  calculateLanguageModelUsage,
1444
1868
  convertToLanguageModelPrompt,
1869
+ createCallbacksTransformer,
1870
+ formatDataStreamPart,
1871
+ mergeStreams,
1445
1872
  prepareCallSettings,
1873
+ prepareResponseHeaders,
1446
1874
  prepareRetries,
1447
1875
  prepareToolsAndToolChoice,
1448
1876
  standardizePrompt