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.
@@ -31,9 +31,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var internal_exports = {};
32
32
  __export(internal_exports, {
33
33
  HANGING_STREAM_WARNING_TIME_MS: () => HANGING_STREAM_WARNING_TIME_MS,
34
+ StreamData: () => StreamData,
34
35
  calculateLanguageModelUsage: () => calculateLanguageModelUsage,
35
36
  convertToLanguageModelPrompt: () => convertToLanguageModelPrompt,
37
+ createCallbacksTransformer: () => createCallbacksTransformer,
38
+ formatDataStreamPart: () => formatDataStreamPart,
39
+ mergeStreams: () => mergeStreams,
36
40
  prepareCallSettings: () => prepareCallSettings,
41
+ prepareResponseHeaders: () => prepareResponseHeaders,
37
42
  prepareRetries: () => prepareRetries,
38
43
  prepareToolsAndToolChoice: () => prepareToolsAndToolChoice,
39
44
  standardizePrompt: () => standardizePrompt
@@ -314,23 +319,11 @@ function convertToCoreMessages(messages, options) {
314
319
  break;
315
320
  }
316
321
  case "reasoning": {
317
- for (const detail of part.details) {
318
- switch (detail.type) {
319
- case "text":
320
- content2.push({
321
- type: "reasoning",
322
- text: detail.text,
323
- signature: detail.signature
324
- });
325
- break;
326
- case "redacted":
327
- content2.push({
328
- type: "redacted-reasoning",
329
- data: detail.data
330
- });
331
- break;
332
- }
333
- }
322
+ content2.push({
323
+ type: "reasoning",
324
+ text: part.reasoning,
325
+ providerOptions: part.providerMetadata
326
+ });
334
327
  break;
335
328
  }
336
329
  case "tool-invocation":
@@ -600,11 +593,6 @@ var reasoningPartSchema = import_zod5.z.object({
600
593
  text: import_zod5.z.string(),
601
594
  providerOptions: providerMetadataSchema.optional()
602
595
  });
603
- var redactedReasoningPartSchema = import_zod5.z.object({
604
- type: import_zod5.z.literal("redacted-reasoning"),
605
- data: import_zod5.z.string(),
606
- providerOptions: providerMetadataSchema.optional()
607
- });
608
596
  var toolCallPartSchema = import_zod5.z.object({
609
597
  type: import_zod5.z.literal("tool-call"),
610
598
  toolCallId: import_zod5.z.string(),
@@ -645,7 +633,6 @@ var coreAssistantMessageSchema = import_zod6.z.object({
645
633
  textPartSchema,
646
634
  filePartSchema,
647
635
  reasoningPartSchema,
648
- redactedReasoningPartSchema,
649
636
  toolCallPartSchema
650
637
  ])
651
638
  )
@@ -665,7 +652,7 @@ var coreMessageSchema = import_zod6.z.union([
665
652
  ]);
666
653
 
667
654
  // core/prompt/standardize-prompt.ts
668
- function standardizePrompt({
655
+ async function standardizePrompt({
669
656
  prompt,
670
657
  tools
671
658
  }) {
@@ -695,7 +682,6 @@ function standardizePrompt({
695
682
  });
696
683
  }
697
684
  return {
698
- type: "prompt",
699
685
  system: prompt.system,
700
686
  messages: [
701
687
  {
@@ -722,7 +708,7 @@ function standardizePrompt({
722
708
  message: "messages must not be empty"
723
709
  });
724
710
  }
725
- const validationResult = (0, import_provider_utils2.safeValidateTypes)({
711
+ const validationResult = await (0, import_provider_utils2.safeValidateTypes)({
726
712
  value: messages,
727
713
  schema: import_zod7.z.array(coreMessageSchema)
728
714
  });
@@ -734,7 +720,6 @@ function standardizePrompt({
734
720
  });
735
721
  }
736
722
  return {
737
- type: "messages",
738
723
  messages,
739
724
  system: prompt.system
740
725
  };
@@ -745,6 +730,262 @@ function standardizePrompt({
745
730
  // core/util/index.ts
746
731
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
747
732
 
733
+ // core/util/data-stream-parts.ts
734
+ var textStreamPart = {
735
+ code: "0",
736
+ name: "text",
737
+ parse: (value) => {
738
+ if (typeof value !== "string") {
739
+ throw new Error('"text" parts expect a string value.');
740
+ }
741
+ return { type: "text", value };
742
+ }
743
+ };
744
+ var dataStreamPart = {
745
+ code: "2",
746
+ name: "data",
747
+ parse: (value) => {
748
+ if (!Array.isArray(value)) {
749
+ throw new Error('"data" parts expect an array value.');
750
+ }
751
+ return { type: "data", value };
752
+ }
753
+ };
754
+ var errorStreamPart = {
755
+ code: "3",
756
+ name: "error",
757
+ parse: (value) => {
758
+ if (typeof value !== "string") {
759
+ throw new Error('"error" parts expect a string value.');
760
+ }
761
+ return { type: "error", value };
762
+ }
763
+ };
764
+ var messageAnnotationsStreamPart = {
765
+ code: "8",
766
+ name: "message_annotations",
767
+ parse: (value) => {
768
+ if (!Array.isArray(value)) {
769
+ throw new Error('"message_annotations" parts expect an array value.');
770
+ }
771
+ return { type: "message_annotations", value };
772
+ }
773
+ };
774
+ var toolCallStreamPart = {
775
+ code: "9",
776
+ name: "tool_call",
777
+ parse: (value) => {
778
+ 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") {
779
+ throw new Error(
780
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
781
+ );
782
+ }
783
+ return {
784
+ type: "tool_call",
785
+ value
786
+ };
787
+ }
788
+ };
789
+ var toolResultStreamPart = {
790
+ code: "a",
791
+ name: "tool_result",
792
+ parse: (value) => {
793
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("result" in value)) {
794
+ throw new Error(
795
+ '"tool_result" parts expect an object with a "toolCallId" and a "result" property.'
796
+ );
797
+ }
798
+ return {
799
+ type: "tool_result",
800
+ value
801
+ };
802
+ }
803
+ };
804
+ var toolCallStreamingStartStreamPart = {
805
+ code: "b",
806
+ name: "tool_call_streaming_start",
807
+ parse: (value) => {
808
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string") {
809
+ throw new Error(
810
+ '"tool_call_streaming_start" parts expect an object with a "toolCallId" and "toolName" property.'
811
+ );
812
+ }
813
+ return {
814
+ type: "tool_call_streaming_start",
815
+ value
816
+ };
817
+ }
818
+ };
819
+ var toolCallDeltaStreamPart = {
820
+ code: "c",
821
+ name: "tool_call_delta",
822
+ parse: (value) => {
823
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("argsTextDelta" in value) || typeof value.argsTextDelta !== "string") {
824
+ throw new Error(
825
+ '"tool_call_delta" parts expect an object with a "toolCallId" and "argsTextDelta" property.'
826
+ );
827
+ }
828
+ return {
829
+ type: "tool_call_delta",
830
+ value
831
+ };
832
+ }
833
+ };
834
+ var finishMessageStreamPart = {
835
+ code: "d",
836
+ name: "finish_message",
837
+ parse: (value) => {
838
+ if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
839
+ throw new Error(
840
+ '"finish_message" parts expect an object with a "finishReason" property.'
841
+ );
842
+ }
843
+ const result = {
844
+ finishReason: value.finishReason
845
+ };
846
+ if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
847
+ result.usage = {
848
+ promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
849
+ completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
850
+ };
851
+ }
852
+ return {
853
+ type: "finish_message",
854
+ value: result
855
+ };
856
+ }
857
+ };
858
+ var finishStepStreamPart = {
859
+ code: "e",
860
+ name: "finish_step",
861
+ parse: (value) => {
862
+ if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
863
+ throw new Error(
864
+ '"finish_step" parts expect an object with a "finishReason" property.'
865
+ );
866
+ }
867
+ const result = {
868
+ finishReason: value.finishReason,
869
+ isContinued: false
870
+ };
871
+ if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
872
+ result.usage = {
873
+ promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
874
+ completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
875
+ };
876
+ }
877
+ if ("isContinued" in value && typeof value.isContinued === "boolean") {
878
+ result.isContinued = value.isContinued;
879
+ }
880
+ return {
881
+ type: "finish_step",
882
+ value: result
883
+ };
884
+ }
885
+ };
886
+ var startStepStreamPart = {
887
+ code: "f",
888
+ name: "start_step",
889
+ parse: (value) => {
890
+ if (value == null || typeof value !== "object" || !("messageId" in value) || typeof value.messageId !== "string") {
891
+ throw new Error(
892
+ '"start_step" parts expect an object with an "id" property.'
893
+ );
894
+ }
895
+ return {
896
+ type: "start_step",
897
+ value: {
898
+ messageId: value.messageId
899
+ }
900
+ };
901
+ }
902
+ };
903
+ var reasoningStreamPart = {
904
+ code: "g",
905
+ name: "reasoning",
906
+ parse: (value) => {
907
+ if (value == null || typeof value !== "object" || !("text" in value) || typeof value.text !== "string" || "providerMetadata" in value && typeof value.providerMetadata !== "object") {
908
+ throw new Error(
909
+ '"reasoning" parts expect an object with a "text" property.'
910
+ );
911
+ }
912
+ return {
913
+ type: "reasoning",
914
+ value: {
915
+ text: value.text,
916
+ providerMetadata: value.providerMetadata
917
+ }
918
+ };
919
+ }
920
+ };
921
+ var sourcePart = {
922
+ code: "h",
923
+ name: "source",
924
+ parse: (value) => {
925
+ if (value == null || typeof value !== "object") {
926
+ throw new Error('"source" parts expect a Source object.');
927
+ }
928
+ return {
929
+ type: "source",
930
+ value
931
+ };
932
+ }
933
+ };
934
+ var fileStreamPart = {
935
+ code: "k",
936
+ name: "file",
937
+ parse: (value) => {
938
+ if (value == null || typeof value !== "object" || !("data" in value) || typeof value.data !== "string" || !("mimeType" in value) || typeof value.mimeType !== "string") {
939
+ throw new Error(
940
+ '"file" parts expect an object with a "data" and "mimeType" property.'
941
+ );
942
+ }
943
+ return { type: "file", value };
944
+ }
945
+ };
946
+ var reasoningPartFinishStreamPart = {
947
+ code: "l",
948
+ name: "reasoning_part_finish",
949
+ parse: () => {
950
+ return {
951
+ type: "reasoning_part_finish",
952
+ value: {}
953
+ };
954
+ }
955
+ };
956
+ var dataStreamParts = [
957
+ textStreamPart,
958
+ dataStreamPart,
959
+ errorStreamPart,
960
+ messageAnnotationsStreamPart,
961
+ toolCallStreamPart,
962
+ toolResultStreamPart,
963
+ toolCallStreamingStartStreamPart,
964
+ toolCallDeltaStreamPart,
965
+ finishMessageStreamPart,
966
+ finishStepStreamPart,
967
+ startStepStreamPart,
968
+ reasoningStreamPart,
969
+ sourcePart,
970
+ reasoningPartFinishStreamPart,
971
+ fileStreamPart
972
+ ];
973
+ var dataStreamPartsByCode = Object.fromEntries(
974
+ dataStreamParts.map((part) => [part.code, part])
975
+ );
976
+ var DataStreamStringPrefixes = Object.fromEntries(
977
+ dataStreamParts.map((part) => [part.name, part.code])
978
+ );
979
+ var validCodes = dataStreamParts.map((part) => part.code);
980
+ function formatDataStreamPart(type, value) {
981
+ const streamPart = dataStreamParts.find((part) => part.name === type);
982
+ if (!streamPart) {
983
+ throw new Error(`Invalid stream part type: ${type}`);
984
+ }
985
+ return `${streamPart.code}:${JSON.stringify(value)}
986
+ `;
987
+ }
988
+
748
989
  // core/util/schema.ts
749
990
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
750
991
 
@@ -1317,14 +1558,6 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
1317
1558
  return {
1318
1559
  type: "reasoning",
1319
1560
  text: part.text,
1320
- signature: part.signature,
1321
- providerOptions
1322
- };
1323
- }
1324
- case "redacted-reasoning": {
1325
- return {
1326
- type: "redacted-reasoning",
1327
- data: part.data,
1328
1561
  providerOptions
1329
1562
  };
1330
1563
  }
@@ -1475,14 +1708,214 @@ function calculateLanguageModelUsage({
1475
1708
  };
1476
1709
  }
1477
1710
 
1711
+ // core/util/prepare-response-headers.ts
1712
+ function prepareResponseHeaders(headers, {
1713
+ contentType,
1714
+ dataStreamVersion
1715
+ }) {
1716
+ const responseHeaders = new Headers(headers != null ? headers : {});
1717
+ if (!responseHeaders.has("Content-Type")) {
1718
+ responseHeaders.set("Content-Type", contentType);
1719
+ }
1720
+ if (dataStreamVersion !== void 0) {
1721
+ responseHeaders.set("X-Vercel-AI-Data-Stream", dataStreamVersion);
1722
+ }
1723
+ return responseHeaders;
1724
+ }
1725
+
1726
+ // core/util/merge-streams.ts
1727
+ function mergeStreams(stream1, stream2) {
1728
+ const reader1 = stream1.getReader();
1729
+ const reader2 = stream2.getReader();
1730
+ let lastRead1 = void 0;
1731
+ let lastRead2 = void 0;
1732
+ let stream1Done = false;
1733
+ let stream2Done = false;
1734
+ async function readStream1(controller) {
1735
+ try {
1736
+ if (lastRead1 == null) {
1737
+ lastRead1 = reader1.read();
1738
+ }
1739
+ const result = await lastRead1;
1740
+ lastRead1 = void 0;
1741
+ if (!result.done) {
1742
+ controller.enqueue(result.value);
1743
+ } else {
1744
+ controller.close();
1745
+ }
1746
+ } catch (error) {
1747
+ controller.error(error);
1748
+ }
1749
+ }
1750
+ async function readStream2(controller) {
1751
+ try {
1752
+ if (lastRead2 == null) {
1753
+ lastRead2 = reader2.read();
1754
+ }
1755
+ const result = await lastRead2;
1756
+ lastRead2 = void 0;
1757
+ if (!result.done) {
1758
+ controller.enqueue(result.value);
1759
+ } else {
1760
+ controller.close();
1761
+ }
1762
+ } catch (error) {
1763
+ controller.error(error);
1764
+ }
1765
+ }
1766
+ return new ReadableStream({
1767
+ async pull(controller) {
1768
+ try {
1769
+ if (stream1Done) {
1770
+ await readStream2(controller);
1771
+ return;
1772
+ }
1773
+ if (stream2Done) {
1774
+ await readStream1(controller);
1775
+ return;
1776
+ }
1777
+ if (lastRead1 == null) {
1778
+ lastRead1 = reader1.read();
1779
+ }
1780
+ if (lastRead2 == null) {
1781
+ lastRead2 = reader2.read();
1782
+ }
1783
+ const { result, reader } = await Promise.race([
1784
+ lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
1785
+ lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
1786
+ ]);
1787
+ if (!result.done) {
1788
+ controller.enqueue(result.value);
1789
+ }
1790
+ if (reader === reader1) {
1791
+ lastRead1 = void 0;
1792
+ if (result.done) {
1793
+ await readStream2(controller);
1794
+ stream1Done = true;
1795
+ }
1796
+ } else {
1797
+ lastRead2 = void 0;
1798
+ if (result.done) {
1799
+ stream2Done = true;
1800
+ await readStream1(controller);
1801
+ }
1802
+ }
1803
+ } catch (error) {
1804
+ controller.error(error);
1805
+ }
1806
+ },
1807
+ cancel() {
1808
+ reader1.cancel();
1809
+ reader2.cancel();
1810
+ }
1811
+ });
1812
+ }
1813
+
1814
+ // streams/stream-callbacks.ts
1815
+ function createCallbacksTransformer(callbacks = {}) {
1816
+ const textEncoder = new TextEncoder();
1817
+ let aggregatedResponse = "";
1818
+ return new TransformStream({
1819
+ async start() {
1820
+ if (callbacks.onStart)
1821
+ await callbacks.onStart();
1822
+ },
1823
+ async transform(message, controller) {
1824
+ controller.enqueue(textEncoder.encode(message));
1825
+ aggregatedResponse += message;
1826
+ if (callbacks.onToken)
1827
+ await callbacks.onToken(message);
1828
+ if (callbacks.onText && typeof message === "string") {
1829
+ await callbacks.onText(message);
1830
+ }
1831
+ },
1832
+ async flush() {
1833
+ if (callbacks.onCompletion) {
1834
+ await callbacks.onCompletion(aggregatedResponse);
1835
+ }
1836
+ if (callbacks.onFinal) {
1837
+ await callbacks.onFinal(aggregatedResponse);
1838
+ }
1839
+ }
1840
+ });
1841
+ }
1842
+
1478
1843
  // util/constants.ts
1479
1844
  var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
1845
+
1846
+ // streams/stream-data.ts
1847
+ var StreamData = class {
1848
+ constructor() {
1849
+ this.encoder = new TextEncoder();
1850
+ this.controller = null;
1851
+ this.isClosed = false;
1852
+ this.warningTimeout = null;
1853
+ const self = this;
1854
+ this.stream = new ReadableStream({
1855
+ start: async (controller) => {
1856
+ self.controller = controller;
1857
+ if (process.env.NODE_ENV === "development") {
1858
+ self.warningTimeout = setTimeout(() => {
1859
+ console.warn(
1860
+ "The data stream is hanging. Did you forget to close it with `data.close()`?"
1861
+ );
1862
+ }, HANGING_STREAM_WARNING_TIME_MS);
1863
+ }
1864
+ },
1865
+ pull: (controller) => {
1866
+ },
1867
+ cancel: (reason) => {
1868
+ this.isClosed = true;
1869
+ }
1870
+ });
1871
+ }
1872
+ async close() {
1873
+ if (this.isClosed) {
1874
+ throw new Error("Data Stream has already been closed.");
1875
+ }
1876
+ if (!this.controller) {
1877
+ throw new Error("Stream controller is not initialized.");
1878
+ }
1879
+ this.controller.close();
1880
+ this.isClosed = true;
1881
+ if (this.warningTimeout) {
1882
+ clearTimeout(this.warningTimeout);
1883
+ }
1884
+ }
1885
+ append(value) {
1886
+ if (this.isClosed) {
1887
+ throw new Error("Data Stream has already been closed.");
1888
+ }
1889
+ if (!this.controller) {
1890
+ throw new Error("Stream controller is not initialized.");
1891
+ }
1892
+ this.controller.enqueue(
1893
+ this.encoder.encode(formatDataStreamPart("data", [value]))
1894
+ );
1895
+ }
1896
+ appendMessageAnnotation(value) {
1897
+ if (this.isClosed) {
1898
+ throw new Error("Data Stream has already been closed.");
1899
+ }
1900
+ if (!this.controller) {
1901
+ throw new Error("Stream controller is not initialized.");
1902
+ }
1903
+ this.controller.enqueue(
1904
+ this.encoder.encode(formatDataStreamPart("message_annotations", [value]))
1905
+ );
1906
+ }
1907
+ };
1480
1908
  // Annotate the CommonJS export names for ESM import in node:
1481
1909
  0 && (module.exports = {
1482
1910
  HANGING_STREAM_WARNING_TIME_MS,
1911
+ StreamData,
1483
1912
  calculateLanguageModelUsage,
1484
1913
  convertToLanguageModelPrompt,
1914
+ createCallbacksTransformer,
1915
+ formatDataStreamPart,
1916
+ mergeStreams,
1485
1917
  prepareCallSettings,
1918
+ prepareResponseHeaders,
1486
1919
  prepareRetries,
1487
1920
  prepareToolsAndToolChoice,
1488
1921
  standardizePrompt