@ai-sdk/langchain 2.0.21 → 2.0.23

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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @ai-sdk/langchain
2
2
 
3
+ ## 2.0.23
4
+
5
+ ### Patch Changes
6
+
7
+ - 78683c1: fix(langchain): add multimodal support to convertUserContent
8
+
9
+ ## 2.0.22
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [f748c46]
14
+ - ai@6.0.20
15
+
3
16
  ## 2.0.21
4
17
 
5
18
  ### Patch Changes
package/dist/index.js CHANGED
@@ -78,22 +78,171 @@ function convertAssistantContent(content) {
78
78
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0
79
79
  });
80
80
  }
81
+ function getDefaultFilename(mediaType, prefix = "file") {
82
+ const ext = mediaType.split("/")[1] || "bin";
83
+ return `${prefix}.${ext}`;
84
+ }
81
85
  function convertUserContent(content) {
86
+ var _a;
82
87
  if (typeof content === "string") {
83
88
  return new import_messages.HumanMessage({ content });
84
89
  }
85
- const textParts = content.filter(
86
- (part) => part.type === "text"
87
- ).map((part) => part.text);
88
- return new import_messages.HumanMessage({ content: textParts.join("") });
90
+ const contentBlocks = [];
91
+ for (const part of content) {
92
+ if (part.type === "text") {
93
+ contentBlocks.push({ type: "text", text: part.text });
94
+ } else if (part.type === "image") {
95
+ const imagePart = part;
96
+ if (imagePart.image instanceof URL) {
97
+ contentBlocks.push({
98
+ type: "image_url",
99
+ image_url: { url: imagePart.image.toString() }
100
+ });
101
+ } else if (typeof imagePart.image === "string") {
102
+ if (imagePart.image.startsWith("http://") || imagePart.image.startsWith("https://") || imagePart.image.startsWith("data:")) {
103
+ contentBlocks.push({
104
+ type: "image_url",
105
+ image_url: { url: imagePart.image }
106
+ });
107
+ } else {
108
+ const mimeType = imagePart.mediaType || "image/png";
109
+ contentBlocks.push({
110
+ type: "image_url",
111
+ image_url: { url: `data:${mimeType};base64,${imagePart.image}` }
112
+ });
113
+ }
114
+ } else if (
115
+ /**
116
+ * Handle Uint8Array or ArrayBuffer (binary data)
117
+ */
118
+ imagePart.image instanceof Uint8Array || imagePart.image instanceof ArrayBuffer
119
+ ) {
120
+ const bytes = imagePart.image instanceof ArrayBuffer ? new Uint8Array(imagePart.image) : imagePart.image;
121
+ const base64 = btoa(String.fromCharCode(...bytes));
122
+ const mimeType = imagePart.mediaType || "image/png";
123
+ contentBlocks.push({
124
+ type: "image_url",
125
+ image_url: { url: `data:${mimeType};base64,${base64}` }
126
+ });
127
+ }
128
+ } else if (part.type === "file") {
129
+ const filePart = part;
130
+ const isImage = (_a = filePart.mediaType) == null ? void 0 : _a.startsWith("image/");
131
+ if (isImage) {
132
+ if (filePart.data instanceof URL) {
133
+ contentBlocks.push({
134
+ type: "image_url",
135
+ image_url: { url: filePart.data.toString() }
136
+ });
137
+ } else if (typeof filePart.data === "string") {
138
+ if (filePart.data.startsWith("http://") || filePart.data.startsWith("https://") || filePart.data.startsWith("data:")) {
139
+ contentBlocks.push({
140
+ type: "image_url",
141
+ image_url: { url: filePart.data }
142
+ });
143
+ } else {
144
+ contentBlocks.push({
145
+ type: "image_url",
146
+ image_url: {
147
+ url: `data:${filePart.mediaType};base64,${filePart.data}`
148
+ }
149
+ });
150
+ }
151
+ } else if (filePart.data instanceof Uint8Array || filePart.data instanceof ArrayBuffer) {
152
+ const bytes = filePart.data instanceof ArrayBuffer ? new Uint8Array(filePart.data) : filePart.data;
153
+ const base64 = btoa(String.fromCharCode(...bytes));
154
+ contentBlocks.push({
155
+ type: "image_url",
156
+ image_url: { url: `data:${filePart.mediaType};base64,${base64}` }
157
+ });
158
+ }
159
+ } else {
160
+ const filename = filePart.filename || getDefaultFilename(filePart.mediaType, "file");
161
+ if (filePart.data instanceof URL) {
162
+ contentBlocks.push({
163
+ type: "file",
164
+ url: filePart.data.toString(),
165
+ mimeType: filePart.mediaType,
166
+ filename
167
+ });
168
+ } else if (typeof filePart.data === "string") {
169
+ if (filePart.data.startsWith("http://") || filePart.data.startsWith("https://")) {
170
+ contentBlocks.push({
171
+ type: "file",
172
+ url: filePart.data,
173
+ mimeType: filePart.mediaType,
174
+ filename
175
+ });
176
+ } else if (filePart.data.startsWith("data:")) {
177
+ const matches = filePart.data.match(/^data:([^;]+);base64,(.+)$/);
178
+ if (matches) {
179
+ contentBlocks.push({
180
+ type: "file",
181
+ data: matches[2],
182
+ mimeType: matches[1],
183
+ filename
184
+ });
185
+ } else {
186
+ contentBlocks.push({
187
+ type: "file",
188
+ url: filePart.data,
189
+ mimeType: filePart.mediaType,
190
+ filename
191
+ });
192
+ }
193
+ } else {
194
+ contentBlocks.push({
195
+ type: "file",
196
+ data: filePart.data,
197
+ mimeType: filePart.mediaType,
198
+ filename
199
+ });
200
+ }
201
+ } else if (filePart.data instanceof Uint8Array || filePart.data instanceof ArrayBuffer) {
202
+ const bytes = filePart.data instanceof ArrayBuffer ? new Uint8Array(filePart.data) : filePart.data;
203
+ const base64 = btoa(String.fromCharCode(...bytes));
204
+ contentBlocks.push({
205
+ type: "file",
206
+ data: base64,
207
+ mimeType: filePart.mediaType,
208
+ filename
209
+ });
210
+ }
211
+ }
212
+ }
213
+ }
214
+ if (contentBlocks.every((block) => block.type === "text")) {
215
+ return new import_messages.HumanMessage({
216
+ content: contentBlocks.map((block) => block.text).join("")
217
+ });
218
+ }
219
+ return new import_messages.HumanMessage({ content: contentBlocks });
89
220
  }
90
221
  function isToolResultPart(item) {
91
222
  return item != null && typeof item === "object" && "type" in item && item.type === "tool-result";
92
223
  }
93
224
  function processModelChunk(chunk, state, controller) {
225
+ if (!state.emittedImages) {
226
+ state.emittedImages = /* @__PURE__ */ new Set();
227
+ }
94
228
  if (chunk.id) {
95
229
  state.messageId = chunk.id;
96
230
  }
231
+ const chunkObj = chunk;
232
+ const additionalKwargs = chunkObj.additional_kwargs;
233
+ const imageOutputs = extractImageOutputs(additionalKwargs);
234
+ for (const imageOutput of imageOutputs) {
235
+ if (imageOutput.result && !state.emittedImages.has(imageOutput.id)) {
236
+ state.emittedImages.add(imageOutput.id);
237
+ const mediaType = `image/${imageOutput.output_format || "png"}`;
238
+ controller.enqueue({
239
+ type: "file",
240
+ mediaType,
241
+ url: `data:${mediaType};base64,${imageOutput.result}`
242
+ });
243
+ state.started = true;
244
+ }
245
+ }
97
246
  const reasoning = extractReasoningFromContentBlocks(chunk) || extractReasoningFromValuesMessage(chunk);
98
247
  if (reasoning) {
99
248
  if (!state.reasoningStarted) {