ai 5.0.0-canary.12 → 5.0.0-canary.13

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