@assistant-ui/react-langchain 0.0.27 → 0.0.29
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/README.md +1 -1
- package/dist/convertMessages.d.ts +2 -76
- package/dist/convertMessages.d.ts.map +1 -1
- package/dist/convertMessages.js +20 -153
- package/dist/convertMessages.js.map +1 -1
- package/dist/converter.d.ts +180 -0
- package/dist/converter.d.ts.map +1 -0
- package/dist/converter.js +213 -0
- package/dist/converter.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/streamingTiming.d.ts +1 -2
- package/dist/streamingTiming.d.ts.map +1 -1
- package/dist/streamingTiming.js +2 -36
- package/dist/streamingTiming.js.map +1 -1
- package/dist/types.d.ts +2 -55
- package/dist/types.d.ts.map +1 -1
- package/dist/useStreamRuntime.d.ts +2 -8
- package/dist/useStreamRuntime.d.ts.map +1 -1
- package/dist/useStreamRuntime.js +23 -44
- package/dist/useStreamRuntime.js.map +1 -1
- package/package.json +24 -10
- package/src/convertMessages.test.ts +217 -1
- package/src/convertMessages.ts +43 -213
- package/src/converter.ts +372 -0
- package/src/streamingTiming.test.tsx +37 -0
- package/src/streamingTiming.ts +5 -68
- package/src/types.ts +1 -39
- package/src/useStreamRuntime.test.tsx +74 -1
- package/src/useStreamRuntime.ts +47 -54
package/src/convertMessages.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
+
import type { MessageTiming } from "@assistant-ui/core";
|
|
3
4
|
import type { useExternalMessageConverter } from "@assistant-ui/core/react";
|
|
4
|
-
import type {
|
|
5
|
-
AppendMessage,
|
|
6
|
-
DataMessagePart,
|
|
7
|
-
MessageTiming,
|
|
8
|
-
} from "@assistant-ui/core";
|
|
9
|
-
import { httpUrlPattern, parseDataUrl } from "@assistant-ui/core/internal";
|
|
10
5
|
import type { ReadonlyJSONObject } from "assistant-stream/utils";
|
|
6
|
+
import {
|
|
7
|
+
convertLangChainContentBlock,
|
|
8
|
+
getCustomMetadata,
|
|
9
|
+
uiMessageToDataPart,
|
|
10
|
+
withAudioTranscript,
|
|
11
|
+
} from "./converter";
|
|
11
12
|
import type {
|
|
12
13
|
LangChainBaseMessage,
|
|
13
14
|
LangChainContentBlock,
|
|
@@ -20,120 +21,52 @@ type LangChainMessageConverterMetadata =
|
|
|
20
21
|
messageTiming?: Record<string, MessageTiming>;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
const warnedMalformedMessages = new Set<string>();
|
|
25
|
+
const warnOnceInDevelopment = (message: string) => {
|
|
26
|
+
if (
|
|
27
|
+
typeof process === "undefined" ||
|
|
28
|
+
process?.env?.NODE_ENV !== "development"
|
|
29
|
+
)
|
|
30
|
+
return;
|
|
31
|
+
if (warnedMalformedMessages.has(message)) return;
|
|
32
|
+
warnedMalformedMessages.add(message);
|
|
33
|
+
console.warn(message);
|
|
34
|
+
};
|
|
28
35
|
|
|
29
36
|
export const getMessageType = (message: LangChainBaseMessage): string => {
|
|
30
37
|
if (typeof message._getType === "function") return message._getType();
|
|
31
38
|
if ("type" in message)
|
|
32
39
|
return (message as Record<string, unknown>).type as string;
|
|
33
|
-
|
|
40
|
+
warnOnceInDevelopment(
|
|
41
|
+
"Cannot determine message type; rendering the message as system text",
|
|
42
|
+
);
|
|
43
|
+
return "unknown";
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const contentBlocks = (content: unknown): readonly LangChainContentBlock[] => {
|
|
47
|
+
if (content == null) return [];
|
|
48
|
+
if (Array.isArray(content))
|
|
49
|
+
return content.filter(
|
|
50
|
+
(block) => typeof block === "object" && block !== null,
|
|
51
|
+
);
|
|
52
|
+
warnOnceInDevelopment(
|
|
53
|
+
`Ignoring message content that is neither a string nor an array: ${typeof content}`,
|
|
54
|
+
);
|
|
55
|
+
return [];
|
|
34
56
|
};
|
|
35
57
|
|
|
36
58
|
const contentToParts = (content: unknown) => {
|
|
37
59
|
if (typeof content === "string")
|
|
38
60
|
return [{ type: "text" as const, text: content }];
|
|
39
61
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
.
|
|
43
|
-
const type = part.type;
|
|
44
|
-
switch (type) {
|
|
45
|
-
case "text":
|
|
46
|
-
case "text_delta":
|
|
47
|
-
return { type: "text" as const, text: part.text };
|
|
48
|
-
case "image_url": {
|
|
49
|
-
const image =
|
|
50
|
-
typeof part.image_url === "string"
|
|
51
|
-
? part.image_url
|
|
52
|
-
: part.image_url?.url;
|
|
53
|
-
if (!image) return null;
|
|
54
|
-
return { type: "image" as const, image };
|
|
55
|
-
}
|
|
56
|
-
case "file":
|
|
57
|
-
return {
|
|
58
|
-
type: "file" as const,
|
|
59
|
-
filename: part.metadata?.filename ?? "file",
|
|
60
|
-
data:
|
|
61
|
-
part.source_type === "url"
|
|
62
|
-
? part.url
|
|
63
|
-
: part.source_type === "id"
|
|
64
|
-
? part.id
|
|
65
|
-
: part.data,
|
|
66
|
-
mimeType: part.mime_type ?? "application/octet-stream",
|
|
67
|
-
...((part.source_type === "url" || part.source_type === "id") && {
|
|
68
|
-
sourceType: part.source_type,
|
|
69
|
-
}),
|
|
70
|
-
};
|
|
71
|
-
case "audio": {
|
|
72
|
-
const mimeType = part.mime_type ?? "application/octet-stream";
|
|
73
|
-
const subtype = mimeType.startsWith("audio/")
|
|
74
|
-
? mimeType.slice("audio/".length)
|
|
75
|
-
: undefined;
|
|
76
|
-
return {
|
|
77
|
-
type: "file" as const,
|
|
78
|
-
filename: subtype ? `audio.${subtype}` : "audio",
|
|
79
|
-
data: part.data,
|
|
80
|
-
mimeType,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
case "thinking":
|
|
84
|
-
return { type: "reasoning" as const, text: part.thinking };
|
|
85
|
-
case "reasoning":
|
|
86
|
-
return {
|
|
87
|
-
type: "reasoning" as const,
|
|
88
|
-
text:
|
|
89
|
-
part.summary?.map((s) => s?.text ?? "").join("\n\n\n") ??
|
|
90
|
-
part.reasoning ??
|
|
91
|
-
"",
|
|
92
|
-
};
|
|
93
|
-
case "tool_use":
|
|
94
|
-
case "input_json_delta":
|
|
95
|
-
return null;
|
|
96
|
-
default:
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
})
|
|
100
|
-
.filter((p) => p !== null);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const hasVisibleText = (text: unknown): boolean =>
|
|
104
|
-
typeof text === "string" && text.trim() !== "";
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Audio output arrives outside the content array: providers leave `content`
|
|
108
|
-
* empty and put the spoken text in `additional_kwargs.audio.transcript`. The
|
|
109
|
-
* audio bytes stay behind because no provider reports their media type, and a
|
|
110
|
-
* streamed response carries raw PCM rather than a playable file.
|
|
111
|
-
*/
|
|
112
|
-
const withAudioTranscript = (
|
|
113
|
-
parts: ReturnType<typeof contentToParts>,
|
|
114
|
-
additionalKwargs: Record<string, unknown> | undefined,
|
|
115
|
-
): ReturnType<typeof contentToParts> => {
|
|
116
|
-
const audio = additionalKwargs?.audio as { transcript?: unknown } | undefined;
|
|
117
|
-
const transcript = audio?.transcript;
|
|
118
|
-
if (typeof transcript !== "string" || !hasVisibleText(transcript))
|
|
119
|
-
return parts;
|
|
120
|
-
if (parts.some((part) => part.type === "text" && hasVisibleText(part.text)))
|
|
121
|
-
return parts;
|
|
122
|
-
return [
|
|
123
|
-
...parts.filter((part) => part.type !== "text"),
|
|
124
|
-
{ type: "text" as const, text: transcript },
|
|
125
|
-
];
|
|
62
|
+
return contentBlocks(content)
|
|
63
|
+
.map(convertLangChainContentBlock)
|
|
64
|
+
.filter((part) => part !== null && part !== undefined);
|
|
126
65
|
};
|
|
127
66
|
|
|
128
|
-
const getCustomMetadata = (
|
|
129
|
-
additionalKwargs: Record<string, unknown> | undefined,
|
|
130
|
-
): Record<string, unknown> =>
|
|
131
|
-
(additionalKwargs?.metadata as Record<string, unknown>) ?? {};
|
|
132
|
-
|
|
133
67
|
const getStringContent = (content: unknown): string => {
|
|
134
68
|
if (typeof content === "string") return content;
|
|
135
|
-
|
|
136
|
-
return parts
|
|
69
|
+
return contentBlocks(content)
|
|
137
70
|
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
138
71
|
.map((c) => c.text)
|
|
139
72
|
.join("");
|
|
@@ -210,7 +143,9 @@ export const convertLangChainBaseMessage = (
|
|
|
210
143
|
case "tool":
|
|
211
144
|
return {
|
|
212
145
|
role: "tool",
|
|
213
|
-
|
|
146
|
+
// `joinExternalMessages` only checks the name against the tool call
|
|
147
|
+
// when it is non-null, so an empty name manufactures a mismatch.
|
|
148
|
+
toolName: message.name || undefined,
|
|
214
149
|
toolCallId: message.tool_call_id ?? "",
|
|
215
150
|
result: message.content,
|
|
216
151
|
artifact: message.artifact,
|
|
@@ -227,116 +162,11 @@ export const convertLangChainBaseMessage = (
|
|
|
227
162
|
text:
|
|
228
163
|
typeof message.content === "string"
|
|
229
164
|
? message.content
|
|
230
|
-
: JSON.stringify(message.content),
|
|
165
|
+
: (JSON.stringify(message.content) ?? ""),
|
|
231
166
|
},
|
|
232
167
|
],
|
|
233
168
|
};
|
|
234
169
|
}
|
|
235
170
|
};
|
|
236
171
|
|
|
237
|
-
|
|
238
|
-
* Audio media types that reach a provider's audio input through the LangChain
|
|
239
|
-
* `audio` block. langchain-core derives OpenAI's `input_audio.format` by
|
|
240
|
-
* splitting `mime_type` on `/`, and that format is a wav-or-mp3 enum, so
|
|
241
|
-
* `audio/mpeg` passes the converter and is rejected at the provider.
|
|
242
|
-
*/
|
|
243
|
-
const audioBlockMimeTypes = new Map<string, "audio/mp3" | "audio/wav">([
|
|
244
|
-
["audio/mp3", "audio/mp3"],
|
|
245
|
-
["audio/mpeg", "audio/mp3"],
|
|
246
|
-
["audio/wav", "audio/wav"],
|
|
247
|
-
["audio/wave", "audio/wav"],
|
|
248
|
-
["audio/x-wav", "audio/wav"],
|
|
249
|
-
]);
|
|
250
|
-
|
|
251
|
-
export const getMessageContent = (msg: AppendMessage) => {
|
|
252
|
-
const allContent = [
|
|
253
|
-
...msg.content,
|
|
254
|
-
...(msg.attachments?.flatMap((a) => a.content) ?? []),
|
|
255
|
-
];
|
|
256
|
-
|
|
257
|
-
const hasNonText = allContent.some(
|
|
258
|
-
(part) =>
|
|
259
|
-
part.type === "file" || part.type === "image" || part.type === "audio",
|
|
260
|
-
);
|
|
261
|
-
const hasText = allContent.some((part) => part.type === "text");
|
|
262
|
-
if (hasNonText && !hasText) {
|
|
263
|
-
allContent.unshift({ type: "text", text: " " });
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const content = allContent.flatMap((part) => {
|
|
267
|
-
const type = part.type;
|
|
268
|
-
switch (type) {
|
|
269
|
-
case "text":
|
|
270
|
-
return { type: "text" as const, text: part.text };
|
|
271
|
-
case "image":
|
|
272
|
-
return { type: "image_url" as const, image_url: { url: part.image } };
|
|
273
|
-
case "file": {
|
|
274
|
-
const metadata = { filename: part.filename ?? "file" };
|
|
275
|
-
if (part.sourceType === "id") {
|
|
276
|
-
return {
|
|
277
|
-
type: "file" as const,
|
|
278
|
-
id: part.data,
|
|
279
|
-
mime_type: part.mimeType,
|
|
280
|
-
filename: metadata.filename,
|
|
281
|
-
metadata,
|
|
282
|
-
source_type: "id" as const,
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
if (part.sourceType === "url" || httpUrlPattern.test(part.data)) {
|
|
286
|
-
return {
|
|
287
|
-
type: "file" as const,
|
|
288
|
-
url: part.data,
|
|
289
|
-
mime_type: part.mimeType,
|
|
290
|
-
filename: metadata.filename,
|
|
291
|
-
metadata,
|
|
292
|
-
source_type: "url" as const,
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
const parsed = parseDataUrl(part.data);
|
|
296
|
-
const audioMimeType = audioBlockMimeTypes.get(
|
|
297
|
-
(parsed?.mimeType ?? part.mimeType).toLowerCase(),
|
|
298
|
-
);
|
|
299
|
-
if (audioMimeType) {
|
|
300
|
-
return {
|
|
301
|
-
type: "audio" as const,
|
|
302
|
-
data: parsed?.data ?? part.data,
|
|
303
|
-
mime_type: audioMimeType,
|
|
304
|
-
source_type: "base64" as const,
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
return {
|
|
308
|
-
type: "file" as const,
|
|
309
|
-
data: parsed?.data ?? part.data,
|
|
310
|
-
mime_type: parsed?.mimeType ?? part.mimeType,
|
|
311
|
-
filename: metadata.filename,
|
|
312
|
-
metadata,
|
|
313
|
-
source_type: "base64" as const,
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
case "audio": {
|
|
317
|
-
const parsed = parseDataUrl(part.audio.data);
|
|
318
|
-
return {
|
|
319
|
-
type: "audio" as const,
|
|
320
|
-
data: parsed?.data ?? part.audio.data,
|
|
321
|
-
mime_type: `audio/${part.audio.format}`,
|
|
322
|
-
source_type: "base64" as const,
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
case "data":
|
|
326
|
-
return [];
|
|
327
|
-
case "tool-call":
|
|
328
|
-
throw new Error("Tool call appends are not supported.");
|
|
329
|
-
default: {
|
|
330
|
-
const _exhaustiveCheck: "reasoning" | "source" | "generative-ui" = type;
|
|
331
|
-
throw new Error(
|
|
332
|
-
`Unsupported append message part type: ${_exhaustiveCheck}`,
|
|
333
|
-
);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
if (content.length === 1 && content[0]?.type === "text") {
|
|
339
|
-
return content[0].text ?? "";
|
|
340
|
-
}
|
|
341
|
-
return content;
|
|
342
|
-
};
|
|
172
|
+
export { getMessageContent } from "./converter";
|
package/src/converter.ts
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AppendMessage,
|
|
3
|
+
DataMessagePart,
|
|
4
|
+
ThreadAssistantMessage,
|
|
5
|
+
ThreadUserMessage,
|
|
6
|
+
} from "@assistant-ui/core";
|
|
7
|
+
import {
|
|
8
|
+
parseDataUrl,
|
|
9
|
+
resolveFilePartSource,
|
|
10
|
+
} from "@assistant-ui/core/internal";
|
|
11
|
+
import type { StreamingTimingAccessors } from "@assistant-ui/core/react";
|
|
12
|
+
|
|
13
|
+
/** Known content block types from @langchain/core messages. */
|
|
14
|
+
export type LangChainContentBlock =
|
|
15
|
+
| { type: "text"; text: string }
|
|
16
|
+
| { type: "text_delta"; text: string }
|
|
17
|
+
| { type: "image_url"; image_url: string | { url?: string } }
|
|
18
|
+
| { type: "thinking"; thinking: string }
|
|
19
|
+
| {
|
|
20
|
+
type: "reasoning";
|
|
21
|
+
summary?: Array<{ type: "summary_text"; text?: string }>;
|
|
22
|
+
reasoning?: string;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
type: "file";
|
|
26
|
+
data: string;
|
|
27
|
+
mime_type: string;
|
|
28
|
+
source_type?: "base64";
|
|
29
|
+
metadata?: { filename?: string };
|
|
30
|
+
}
|
|
31
|
+
| {
|
|
32
|
+
type: "file";
|
|
33
|
+
url: string;
|
|
34
|
+
mime_type?: string;
|
|
35
|
+
source_type: "url";
|
|
36
|
+
metadata?: { filename?: string };
|
|
37
|
+
}
|
|
38
|
+
| {
|
|
39
|
+
type: "file";
|
|
40
|
+
id: string;
|
|
41
|
+
mime_type?: string;
|
|
42
|
+
source_type: "id";
|
|
43
|
+
metadata?: { filename?: string };
|
|
44
|
+
}
|
|
45
|
+
| {
|
|
46
|
+
type: "audio";
|
|
47
|
+
data: string;
|
|
48
|
+
mime_type: string;
|
|
49
|
+
source_type: "base64";
|
|
50
|
+
}
|
|
51
|
+
| { type: "tool_use" | "input_json_delta" };
|
|
52
|
+
|
|
53
|
+
type ConvertedContentPart =
|
|
54
|
+
| ThreadUserMessage["content"][number]
|
|
55
|
+
| ThreadAssistantMessage["content"][number];
|
|
56
|
+
|
|
57
|
+
export const convertLangChainContentBlock = (
|
|
58
|
+
part: LangChainContentBlock,
|
|
59
|
+
): ConvertedContentPart | null | undefined => {
|
|
60
|
+
const type = part.type;
|
|
61
|
+
switch (type) {
|
|
62
|
+
case "text":
|
|
63
|
+
case "text_delta":
|
|
64
|
+
return { type: "text" as const, text: part.text };
|
|
65
|
+
case "image_url": {
|
|
66
|
+
const image =
|
|
67
|
+
typeof part.image_url === "string"
|
|
68
|
+
? part.image_url
|
|
69
|
+
: part.image_url?.url;
|
|
70
|
+
if (!image) return null;
|
|
71
|
+
return { type: "image" as const, image };
|
|
72
|
+
}
|
|
73
|
+
case "file":
|
|
74
|
+
return {
|
|
75
|
+
type: "file" as const,
|
|
76
|
+
filename: part.metadata?.filename ?? "file",
|
|
77
|
+
data:
|
|
78
|
+
part.source_type === "url"
|
|
79
|
+
? part.url
|
|
80
|
+
: part.source_type === "id"
|
|
81
|
+
? part.id
|
|
82
|
+
: part.data,
|
|
83
|
+
mimeType: part.mime_type ?? "application/octet-stream",
|
|
84
|
+
...((part.source_type === "url" || part.source_type === "id") && {
|
|
85
|
+
sourceType: part.source_type,
|
|
86
|
+
}),
|
|
87
|
+
};
|
|
88
|
+
case "audio": {
|
|
89
|
+
const mimeType = part.mime_type ?? "application/octet-stream";
|
|
90
|
+
const subtype = mimeType.startsWith("audio/")
|
|
91
|
+
? mimeType.slice("audio/".length)
|
|
92
|
+
: undefined;
|
|
93
|
+
return {
|
|
94
|
+
type: "file" as const,
|
|
95
|
+
filename: subtype ? `audio.${subtype}` : "audio",
|
|
96
|
+
data: part.data,
|
|
97
|
+
mimeType,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
case "thinking":
|
|
101
|
+
return { type: "reasoning" as const, text: part.thinking };
|
|
102
|
+
case "reasoning":
|
|
103
|
+
return {
|
|
104
|
+
type: "reasoning" as const,
|
|
105
|
+
text:
|
|
106
|
+
part.summary && part.summary.length > 0
|
|
107
|
+
? part.summary.map((s) => s?.text ?? "").join("\n\n\n")
|
|
108
|
+
: (part.reasoning ?? ""),
|
|
109
|
+
};
|
|
110
|
+
case "tool_use":
|
|
111
|
+
case "input_json_delta":
|
|
112
|
+
return null;
|
|
113
|
+
default:
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const hasVisibleText = (text: unknown): boolean =>
|
|
119
|
+
typeof text === "string" && text.trim() !== "";
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Audio output arrives outside the content array: providers leave `content`
|
|
123
|
+
* empty and put the spoken text in `additional_kwargs.audio.transcript`. The
|
|
124
|
+
* audio bytes stay behind because no provider reports their media type, and a
|
|
125
|
+
* streamed response carries raw PCM rather than a playable file.
|
|
126
|
+
*/
|
|
127
|
+
export const withAudioTranscript = <T extends { type: string; text?: unknown }>(
|
|
128
|
+
parts: readonly T[],
|
|
129
|
+
additionalKwargs: Record<string, unknown> | undefined,
|
|
130
|
+
): readonly (T | { type: "text"; text: string })[] => {
|
|
131
|
+
const audio = additionalKwargs?.audio as { transcript?: unknown } | undefined;
|
|
132
|
+
const transcript = audio?.transcript;
|
|
133
|
+
if (typeof transcript !== "string" || !hasVisibleText(transcript))
|
|
134
|
+
return parts;
|
|
135
|
+
if (parts.some((part) => part.type === "text" && hasVisibleText(part.text)))
|
|
136
|
+
return parts;
|
|
137
|
+
return [
|
|
138
|
+
...parts.filter((part) => part.type !== "text"),
|
|
139
|
+
{ type: "text" as const, text: transcript },
|
|
140
|
+
];
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export const getCustomMetadata = (
|
|
144
|
+
additionalKwargs: Record<string, unknown> | undefined,
|
|
145
|
+
): Record<string, unknown> =>
|
|
146
|
+
(additionalKwargs?.metadata as Record<string, unknown>) ?? {};
|
|
147
|
+
|
|
148
|
+
export const uiMessageToDataPart = <
|
|
149
|
+
TUIMessage extends { name: string; props: Record<string, unknown> },
|
|
150
|
+
>(
|
|
151
|
+
ui: TUIMessage,
|
|
152
|
+
): DataMessagePart => ({
|
|
153
|
+
type: "data",
|
|
154
|
+
name: ui.name,
|
|
155
|
+
data: ui.props,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Audio media types that reach a provider's audio input through the LangChain
|
|
160
|
+
* `audio` block. langchain-core derives OpenAI's `input_audio.format` by
|
|
161
|
+
* splitting `mime_type` on `/`, and that format is a wav-or-mp3 enum, so
|
|
162
|
+
* `audio/mpeg` passes the converter and is rejected at the provider.
|
|
163
|
+
*/
|
|
164
|
+
const audioBlockMimeTypes = new Map<string, "audio/mp3" | "audio/wav">([
|
|
165
|
+
["audio/mp3", "audio/mp3"],
|
|
166
|
+
["audio/mpeg", "audio/mp3"],
|
|
167
|
+
["audio/wav", "audio/wav"],
|
|
168
|
+
["audio/wave", "audio/wav"],
|
|
169
|
+
["audio/x-wav", "audio/wav"],
|
|
170
|
+
]);
|
|
171
|
+
|
|
172
|
+
export const getMessageContent = (msg: AppendMessage) => {
|
|
173
|
+
const allContent = [
|
|
174
|
+
...msg.content,
|
|
175
|
+
...(msg.attachments?.flatMap((a) => a.content) ?? []),
|
|
176
|
+
];
|
|
177
|
+
|
|
178
|
+
const hasNonText = allContent.some(
|
|
179
|
+
(part) =>
|
|
180
|
+
part.type === "file" || part.type === "image" || part.type === "audio",
|
|
181
|
+
);
|
|
182
|
+
const hasText = allContent.some((part) => part.type === "text");
|
|
183
|
+
if (hasNonText && !hasText) {
|
|
184
|
+
allContent.unshift({ type: "text", text: " " });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const content = allContent.flatMap((part) => {
|
|
188
|
+
const type = part.type;
|
|
189
|
+
switch (type) {
|
|
190
|
+
case "text":
|
|
191
|
+
return { type: "text" as const, text: part.text };
|
|
192
|
+
case "image":
|
|
193
|
+
return { type: "image_url" as const, image_url: { url: part.image } };
|
|
194
|
+
case "file": {
|
|
195
|
+
const metadata = { filename: part.filename ?? "file" };
|
|
196
|
+
if (part.sourceType === "id") {
|
|
197
|
+
return {
|
|
198
|
+
type: "file" as const,
|
|
199
|
+
id: part.data,
|
|
200
|
+
mime_type: part.mimeType,
|
|
201
|
+
filename: metadata.filename,
|
|
202
|
+
metadata,
|
|
203
|
+
source_type: "id" as const,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
const source = resolveFilePartSource(part);
|
|
207
|
+
if (source.kind === "url") {
|
|
208
|
+
return {
|
|
209
|
+
type: "file" as const,
|
|
210
|
+
url: source.url,
|
|
211
|
+
mime_type: part.mimeType,
|
|
212
|
+
filename: metadata.filename,
|
|
213
|
+
metadata,
|
|
214
|
+
source_type: "url" as const,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const audioMimeType = audioBlockMimeTypes.get(
|
|
218
|
+
source.mimeType.toLowerCase(),
|
|
219
|
+
);
|
|
220
|
+
if (audioMimeType) {
|
|
221
|
+
return {
|
|
222
|
+
type: "audio" as const,
|
|
223
|
+
data: source.data,
|
|
224
|
+
mime_type: audioMimeType,
|
|
225
|
+
source_type: "base64" as const,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
type: "file" as const,
|
|
230
|
+
data: source.data,
|
|
231
|
+
mime_type: source.mimeType,
|
|
232
|
+
filename: metadata.filename,
|
|
233
|
+
metadata,
|
|
234
|
+
source_type: "base64" as const,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
case "audio": {
|
|
238
|
+
const parsed = parseDataUrl(part.audio.data);
|
|
239
|
+
return {
|
|
240
|
+
type: "audio" as const,
|
|
241
|
+
data: parsed?.data ?? part.audio.data,
|
|
242
|
+
mime_type: `audio/${part.audio.format}`,
|
|
243
|
+
source_type: "base64" as const,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
case "data":
|
|
247
|
+
return [];
|
|
248
|
+
case "tool-call":
|
|
249
|
+
throw new Error("Tool call appends are not supported.");
|
|
250
|
+
default: {
|
|
251
|
+
const _exhaustiveCheck: "reasoning" | "source" | "generative-ui" = type;
|
|
252
|
+
throw new Error(
|
|
253
|
+
`Unsupported append message part type: ${_exhaustiveCheck}`,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
if (content.length === 1 && content[0]?.type === "text") {
|
|
260
|
+
return content[0].text ?? "";
|
|
261
|
+
}
|
|
262
|
+
return content;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const reasoningTextLength = (part: {
|
|
266
|
+
readonly summary?: ReadonlyArray<{ readonly text?: string }>;
|
|
267
|
+
readonly reasoning?: string;
|
|
268
|
+
}): number => {
|
|
269
|
+
if (part.summary && part.summary.length > 0)
|
|
270
|
+
return part.summary.map((s) => s?.text ?? "").join("\n\n\n").length;
|
|
271
|
+
return part.reasoning?.length ?? 0;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
export const createLangChainStreamingTimingAccessors = <
|
|
275
|
+
TMessage extends {
|
|
276
|
+
id?: string | undefined;
|
|
277
|
+
content?: unknown;
|
|
278
|
+
tool_calls?: readonly unknown[] | undefined;
|
|
279
|
+
},
|
|
280
|
+
>(
|
|
281
|
+
getType: (message: TMessage) => string,
|
|
282
|
+
): StreamingTimingAccessors<TMessage> => {
|
|
283
|
+
const findAiMessage = (
|
|
284
|
+
messages: readonly TMessage[],
|
|
285
|
+
messageId: string,
|
|
286
|
+
): TMessage | undefined =>
|
|
287
|
+
messages.find(
|
|
288
|
+
(message) => getType(message) === "ai" && message.id === messageId,
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
const getTextLength = (
|
|
292
|
+
messages: readonly TMessage[],
|
|
293
|
+
messageId: string,
|
|
294
|
+
): number => {
|
|
295
|
+
const message = findAiMessage(messages, messageId);
|
|
296
|
+
if (!message) return 0;
|
|
297
|
+
const content = message.content;
|
|
298
|
+
if (typeof content === "string") return content.length;
|
|
299
|
+
if (!Array.isArray(content)) return 0;
|
|
300
|
+
let len = 0;
|
|
301
|
+
for (const part of content as readonly LangChainContentBlock[]) {
|
|
302
|
+
switch (part.type) {
|
|
303
|
+
case "text":
|
|
304
|
+
case "text_delta":
|
|
305
|
+
if (typeof part.text === "string") len += part.text.length;
|
|
306
|
+
break;
|
|
307
|
+
case "thinking":
|
|
308
|
+
if (typeof part.thinking === "string") len += part.thinking.length;
|
|
309
|
+
break;
|
|
310
|
+
case "reasoning":
|
|
311
|
+
len += reasoningTextLength(part);
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return len;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const getToolCallCount = (
|
|
319
|
+
messages: readonly TMessage[],
|
|
320
|
+
messageId: string,
|
|
321
|
+
): number => findAiMessage(messages, messageId)?.tool_calls?.length ?? 0;
|
|
322
|
+
|
|
323
|
+
const getAssistantMessageId = (
|
|
324
|
+
messages: readonly TMessage[],
|
|
325
|
+
): string | undefined => {
|
|
326
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
327
|
+
const message = messages[i];
|
|
328
|
+
if (message && getType(message) === "ai" && message.id) return message.id;
|
|
329
|
+
}
|
|
330
|
+
return undefined;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
return {
|
|
334
|
+
getAssistantMessageId,
|
|
335
|
+
getTextLength,
|
|
336
|
+
getToolCallCount,
|
|
337
|
+
};
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Resolve the assistant message a `UIMessage` belongs to: the parent id comes
|
|
342
|
+
* from `metadata.message_id` (Python SDK) or `metadata.id` (JS SDK).
|
|
343
|
+
*/
|
|
344
|
+
export const getUIMessageParentId = (ui: {
|
|
345
|
+
metadata?: { message_id?: string; id?: string } | undefined;
|
|
346
|
+
}): string | undefined => ui.metadata?.message_id ?? ui.metadata?.id;
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Group the graph's accumulated `UIMessage`s by the assistant message they
|
|
350
|
+
* belong to. Non-array state and entries without a parent link are dropped.
|
|
351
|
+
*/
|
|
352
|
+
export const groupUIMessagesByParent = <
|
|
353
|
+
T extends {
|
|
354
|
+
metadata?: { message_id?: string; id?: string } | undefined;
|
|
355
|
+
},
|
|
356
|
+
>(
|
|
357
|
+
value: unknown,
|
|
358
|
+
): Map<string, T[]> => {
|
|
359
|
+
const map = new Map<string, T[]>();
|
|
360
|
+
if (!Array.isArray(value)) return map;
|
|
361
|
+
for (const ui of value as T[]) {
|
|
362
|
+
const parentId = getUIMessageParentId(ui);
|
|
363
|
+
if (!parentId) continue;
|
|
364
|
+
const existing = map.get(parentId);
|
|
365
|
+
if (existing) {
|
|
366
|
+
existing.push(ui);
|
|
367
|
+
} else {
|
|
368
|
+
map.set(parentId, [ui]);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return map;
|
|
372
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
import { act, renderHook } from "@testing-library/react";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import type { LangChainBaseMessage } from "./types";
|
|
6
|
+
import { useLangChainStreamingTiming } from "./streamingTiming";
|
|
7
|
+
|
|
8
|
+
describe("useLangChainStreamingTiming", () => {
|
|
9
|
+
it("counts the reasoning fallback when summary is empty", () => {
|
|
10
|
+
const messages: LangChainBaseMessage[] = [
|
|
11
|
+
{
|
|
12
|
+
id: "msg-1",
|
|
13
|
+
_getType: () => "ai",
|
|
14
|
+
content: [
|
|
15
|
+
{
|
|
16
|
+
type: "reasoning",
|
|
17
|
+
summary: [],
|
|
18
|
+
reasoning: "deduced",
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
const { result, rerender } = renderHook(
|
|
25
|
+
({ msgs, running }) => useLangChainStreamingTiming(msgs, running),
|
|
26
|
+
{ initialProps: { msgs: messages, running: true } },
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
act(() => {
|
|
30
|
+
rerender({ msgs: messages, running: false });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
expect(result.current["msg-1"]?.tokenCount).toBe(
|
|
34
|
+
Math.ceil("deduced".length / 4),
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
});
|