@ai-sdk/langchain 2.0.241 → 2.0.242

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.
package/dist/index.mjs CHANGED
@@ -65,6 +65,37 @@ function getDefaultFilename(mediaType, prefix = "file") {
65
65
  const ext = mediaType.split("/")[1] || "bin";
66
66
  return `${prefix}.${ext}`;
67
67
  }
68
+ function convertImageToContentBlock(data, mediaType = "image/png") {
69
+ if (data instanceof URL) {
70
+ if (data.protocol !== "data:") {
71
+ return { type: "image", url: data.toString() };
72
+ }
73
+ data = data.toString();
74
+ }
75
+ if (typeof data === "string") {
76
+ if (data.startsWith("http://") || data.startsWith("https://")) {
77
+ return { type: "image", url: data };
78
+ }
79
+ if (data.startsWith("data:")) {
80
+ const matches = data.match(/^data:([^;]+);base64,(.+)$/);
81
+ if (matches) {
82
+ return {
83
+ type: "image",
84
+ data: matches[2],
85
+ mimeType: matches[1]
86
+ };
87
+ }
88
+ return { type: "image", url: data };
89
+ }
90
+ return { type: "image", data, mimeType: mediaType };
91
+ }
92
+ const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
93
+ return {
94
+ type: "image",
95
+ data: btoa(String.fromCharCode(...bytes)),
96
+ mimeType: mediaType
97
+ };
98
+ }
68
99
  function convertUserContent(content) {
69
100
  var _a;
70
101
  if (typeof content === "string") {
@@ -76,69 +107,18 @@ function convertUserContent(content) {
76
107
  contentBlocks.push({ type: "text", text: part.text });
77
108
  } else if (part.type === "image") {
78
109
  const imagePart = part;
79
- if (imagePart.image instanceof URL) {
80
- contentBlocks.push({
81
- type: "image_url",
82
- image_url: { url: imagePart.image.toString() }
83
- });
84
- } else if (typeof imagePart.image === "string") {
85
- if (imagePart.image.startsWith("http://") || imagePart.image.startsWith("https://") || imagePart.image.startsWith("data:")) {
86
- contentBlocks.push({
87
- type: "image_url",
88
- image_url: { url: imagePart.image }
89
- });
90
- } else {
91
- const mimeType = imagePart.mediaType || "image/png";
92
- contentBlocks.push({
93
- type: "image_url",
94
- image_url: { url: `data:${mimeType};base64,${imagePart.image}` }
95
- });
96
- }
97
- } else if (
98
- /**
99
- * Handle Uint8Array or ArrayBuffer (binary data)
100
- */
101
- imagePart.image instanceof Uint8Array || imagePart.image instanceof ArrayBuffer
102
- ) {
103
- const bytes = imagePart.image instanceof ArrayBuffer ? new Uint8Array(imagePart.image) : imagePart.image;
104
- const base64 = btoa(String.fromCharCode(...bytes));
105
- const mimeType = imagePart.mediaType || "image/png";
106
- contentBlocks.push({
107
- type: "image_url",
108
- image_url: { url: `data:${mimeType};base64,${base64}` }
109
- });
110
+ if (imagePart.image instanceof URL || typeof imagePart.image === "string" || imagePart.image instanceof Uint8Array || imagePart.image instanceof ArrayBuffer) {
111
+ contentBlocks.push(
112
+ convertImageToContentBlock(imagePart.image, imagePart.mediaType)
113
+ );
110
114
  }
111
115
  } else if (part.type === "file") {
112
116
  const filePart = part;
113
117
  const isImage = (_a = filePart.mediaType) == null ? void 0 : _a.startsWith("image/");
114
118
  if (isImage) {
115
- if (filePart.data instanceof URL) {
116
- contentBlocks.push({
117
- type: "image_url",
118
- image_url: { url: filePart.data.toString() }
119
- });
120
- } else if (typeof filePart.data === "string") {
121
- if (filePart.data.startsWith("http://") || filePart.data.startsWith("https://") || filePart.data.startsWith("data:")) {
122
- contentBlocks.push({
123
- type: "image_url",
124
- image_url: { url: filePart.data }
125
- });
126
- } else {
127
- contentBlocks.push({
128
- type: "image_url",
129
- image_url: {
130
- url: `data:${filePart.mediaType};base64,${filePart.data}`
131
- }
132
- });
133
- }
134
- } else if (filePart.data instanceof Uint8Array || filePart.data instanceof ArrayBuffer) {
135
- const bytes = filePart.data instanceof ArrayBuffer ? new Uint8Array(filePart.data) : filePart.data;
136
- const base64 = btoa(String.fromCharCode(...bytes));
137
- contentBlocks.push({
138
- type: "image_url",
139
- image_url: { url: `data:${filePart.mediaType};base64,${base64}` }
140
- });
141
- }
119
+ contentBlocks.push(
120
+ convertImageToContentBlock(filePart.data, filePart.mediaType)
121
+ );
142
122
  } else {
143
123
  const filename = filePart.filename || getDefaultFilename(filePart.mediaType, "file");
144
124
  if (filePart.data instanceof URL) {
@@ -199,7 +179,7 @@ function convertUserContent(content) {
199
179
  content: contentBlocks.map((block) => block.text).join("")
200
180
  });
201
181
  }
202
- return new HumanMessage({ content: contentBlocks });
182
+ return new HumanMessage({ contentBlocks });
203
183
  }
204
184
  function isToolResultPart(item) {
205
185
  return item != null && typeof item === "object" && "type" in item && item.type === "tool-result";