@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/README.md
CHANGED
|
@@ -17,7 +17,7 @@ npm install @assistant-ui/react @assistant-ui/react-langchain @langchain/react
|
|
|
17
17
|
```tsx
|
|
18
18
|
import { useStreamRuntime } from "@assistant-ui/react-langchain";
|
|
19
19
|
import { AssistantRuntimeProvider } from "@assistant-ui/react";
|
|
20
|
-
import { Thread } from "@/components/assistant-ui/thread";
|
|
20
|
+
import { Thread } from "@/components/assistant-ui/elements/thread.aui";
|
|
21
21
|
|
|
22
22
|
export function App() {
|
|
23
23
|
const runtime = useStreamRuntime({
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { getMessageContent } from "./converter.js";
|
|
1
2
|
import { LangChainBaseMessage, UIMessage } from "./types.js";
|
|
2
3
|
import { useExternalMessageConverter } from "@assistant-ui/core/react";
|
|
3
|
-
import {
|
|
4
|
+
import { MessageTiming } from "@assistant-ui/core";
|
|
4
5
|
//#region src/convertMessages.d.ts
|
|
5
6
|
type LangChainMessageConverterMetadata = useExternalMessageConverter.Metadata & {
|
|
6
7
|
uiMessagesByParent?: Map<string, UIMessage[]>;
|
|
@@ -8,81 +9,6 @@ type LangChainMessageConverterMetadata = useExternalMessageConverter.Metadata &
|
|
|
8
9
|
};
|
|
9
10
|
declare const getMessageType: (message: LangChainBaseMessage) => string;
|
|
10
11
|
declare const convertLangChainBaseMessage: (message: LangChainBaseMessage, metadata?: LangChainMessageConverterMetadata) => useExternalMessageConverter.Message;
|
|
11
|
-
declare const getMessageContent: (msg: AppendMessage) => string | ({
|
|
12
|
-
type: "text";
|
|
13
|
-
text: string;
|
|
14
|
-
image_url?: never;
|
|
15
|
-
id?: never;
|
|
16
|
-
mime_type?: never;
|
|
17
|
-
filename?: never;
|
|
18
|
-
metadata?: never;
|
|
19
|
-
source_type?: never;
|
|
20
|
-
url?: never;
|
|
21
|
-
data?: never;
|
|
22
|
-
} | {
|
|
23
|
-
type: "image_url";
|
|
24
|
-
image_url: {
|
|
25
|
-
url: string;
|
|
26
|
-
};
|
|
27
|
-
text?: never;
|
|
28
|
-
id?: never;
|
|
29
|
-
mime_type?: never;
|
|
30
|
-
filename?: never;
|
|
31
|
-
metadata?: never;
|
|
32
|
-
source_type?: never;
|
|
33
|
-
url?: never;
|
|
34
|
-
data?: never;
|
|
35
|
-
} | {
|
|
36
|
-
type: "file";
|
|
37
|
-
id: string;
|
|
38
|
-
mime_type: string;
|
|
39
|
-
filename: string;
|
|
40
|
-
metadata: {
|
|
41
|
-
filename: string;
|
|
42
|
-
};
|
|
43
|
-
source_type: "id";
|
|
44
|
-
text?: never;
|
|
45
|
-
image_url?: never;
|
|
46
|
-
url?: never;
|
|
47
|
-
data?: never;
|
|
48
|
-
} | {
|
|
49
|
-
type: "file";
|
|
50
|
-
url: string;
|
|
51
|
-
mime_type: string;
|
|
52
|
-
filename: string;
|
|
53
|
-
metadata: {
|
|
54
|
-
filename: string;
|
|
55
|
-
};
|
|
56
|
-
source_type: "url";
|
|
57
|
-
text?: never;
|
|
58
|
-
image_url?: never;
|
|
59
|
-
id?: never;
|
|
60
|
-
data?: never;
|
|
61
|
-
} | {
|
|
62
|
-
type: "file";
|
|
63
|
-
data: string;
|
|
64
|
-
mime_type: string;
|
|
65
|
-
filename: string;
|
|
66
|
-
metadata: {
|
|
67
|
-
filename: string;
|
|
68
|
-
};
|
|
69
|
-
source_type: "base64";
|
|
70
|
-
text?: never;
|
|
71
|
-
image_url?: never;
|
|
72
|
-
id?: never;
|
|
73
|
-
url?: never;
|
|
74
|
-
} | {
|
|
75
|
-
type: "audio";
|
|
76
|
-
data: string;
|
|
77
|
-
mime_type: string;
|
|
78
|
-
source_type: "base64";
|
|
79
|
-
text?: never;
|
|
80
|
-
image_url?: never;
|
|
81
|
-
id?: never;
|
|
82
|
-
filename?: never;
|
|
83
|
-
metadata?: never;
|
|
84
|
-
url?: never;
|
|
85
|
-
})[];
|
|
86
12
|
//#endregion
|
|
87
13
|
export { convertLangChainBaseMessage, getMessageContent, getMessageType };
|
|
88
14
|
//# sourceMappingURL=convertMessages.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convertMessages.d.ts","names":[],"sources":["../src/convertMessages.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"convertMessages.d.ts","names":[],"sources":["../src/convertMessages.ts"],"mappings":";;;;;KAiBK,oCACH,4BAA4B;EAC1B,qBAAqB,YAAY;EACjC,gBAAgB,eAAe;;cAetB,iBAAkB,SAAS;cAuC3B,8BACX,SAAS,sBACT,WAAU,sCACT,4BAA4B"}
|
package/dist/convertMessages.js
CHANGED
|
@@ -1,87 +1,35 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
2
|
+
import { convertLangChainContentBlock, getCustomMetadata, getMessageContent, uiMessageToDataPart, withAudioTranscript } from "./converter.js";
|
|
3
3
|
//#region src/convertMessages.ts
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
const warnedMalformedMessages = /* @__PURE__ */ new Set();
|
|
5
|
+
const warnOnceInDevelopment = (message) => {
|
|
6
|
+
if (typeof process === "undefined" || process?.env?.NODE_ENV !== "development") return;
|
|
7
|
+
if (warnedMalformedMessages.has(message)) return;
|
|
8
|
+
warnedMalformedMessages.add(message);
|
|
9
|
+
console.warn(message);
|
|
10
|
+
};
|
|
9
11
|
const getMessageType = (message) => {
|
|
10
12
|
if (typeof message._getType === "function") return message._getType();
|
|
11
13
|
if ("type" in message) return message.type;
|
|
12
|
-
|
|
14
|
+
warnOnceInDevelopment("Cannot determine message type; rendering the message as system text");
|
|
15
|
+
return "unknown";
|
|
16
|
+
};
|
|
17
|
+
const contentBlocks = (content) => {
|
|
18
|
+
if (content == null) return [];
|
|
19
|
+
if (Array.isArray(content)) return content.filter((block) => typeof block === "object" && block !== null);
|
|
20
|
+
warnOnceInDevelopment(`Ignoring message content that is neither a string nor an array: ${typeof content}`);
|
|
21
|
+
return [];
|
|
13
22
|
};
|
|
14
23
|
const contentToParts = (content) => {
|
|
15
24
|
if (typeof content === "string") return [{
|
|
16
25
|
type: "text",
|
|
17
26
|
text: content
|
|
18
27
|
}];
|
|
19
|
-
return content.map((part) =>
|
|
20
|
-
switch (part.type) {
|
|
21
|
-
case "text":
|
|
22
|
-
case "text_delta": return {
|
|
23
|
-
type: "text",
|
|
24
|
-
text: part.text
|
|
25
|
-
};
|
|
26
|
-
case "image_url": {
|
|
27
|
-
const image = typeof part.image_url === "string" ? part.image_url : part.image_url?.url;
|
|
28
|
-
if (!image) return null;
|
|
29
|
-
return {
|
|
30
|
-
type: "image",
|
|
31
|
-
image
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
case "file": return {
|
|
35
|
-
type: "file",
|
|
36
|
-
filename: part.metadata?.filename ?? "file",
|
|
37
|
-
data: part.source_type === "url" ? part.url : part.source_type === "id" ? part.id : part.data,
|
|
38
|
-
mimeType: part.mime_type ?? "application/octet-stream",
|
|
39
|
-
...(part.source_type === "url" || part.source_type === "id") && { sourceType: part.source_type }
|
|
40
|
-
};
|
|
41
|
-
case "audio": {
|
|
42
|
-
const mimeType = part.mime_type ?? "application/octet-stream";
|
|
43
|
-
const subtype = mimeType.startsWith("audio/") ? mimeType.slice(6) : void 0;
|
|
44
|
-
return {
|
|
45
|
-
type: "file",
|
|
46
|
-
filename: subtype ? `audio.${subtype}` : "audio",
|
|
47
|
-
data: part.data,
|
|
48
|
-
mimeType
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
case "thinking": return {
|
|
52
|
-
type: "reasoning",
|
|
53
|
-
text: part.thinking
|
|
54
|
-
};
|
|
55
|
-
case "reasoning": return {
|
|
56
|
-
type: "reasoning",
|
|
57
|
-
text: part.summary?.map((s) => s?.text ?? "").join("\n\n\n") ?? part.reasoning ?? ""
|
|
58
|
-
};
|
|
59
|
-
case "tool_use":
|
|
60
|
-
case "input_json_delta": return null;
|
|
61
|
-
default: return null;
|
|
62
|
-
}
|
|
63
|
-
}).filter((p) => p !== null);
|
|
64
|
-
};
|
|
65
|
-
const hasVisibleText = (text) => typeof text === "string" && text.trim() !== "";
|
|
66
|
-
/**
|
|
67
|
-
* Audio output arrives outside the content array: providers leave `content`
|
|
68
|
-
* empty and put the spoken text in `additional_kwargs.audio.transcript`. The
|
|
69
|
-
* audio bytes stay behind because no provider reports their media type, and a
|
|
70
|
-
* streamed response carries raw PCM rather than a playable file.
|
|
71
|
-
*/
|
|
72
|
-
const withAudioTranscript = (parts, additionalKwargs) => {
|
|
73
|
-
const transcript = (additionalKwargs?.audio)?.transcript;
|
|
74
|
-
if (typeof transcript !== "string" || !hasVisibleText(transcript)) return parts;
|
|
75
|
-
if (parts.some((part) => part.type === "text" && hasVisibleText(part.text))) return parts;
|
|
76
|
-
return [...parts.filter((part) => part.type !== "text"), {
|
|
77
|
-
type: "text",
|
|
78
|
-
text: transcript
|
|
79
|
-
}];
|
|
28
|
+
return contentBlocks(content).map(convertLangChainContentBlock).filter((part) => part !== null && part !== void 0);
|
|
80
29
|
};
|
|
81
|
-
const getCustomMetadata = (additionalKwargs) => additionalKwargs?.metadata ?? {};
|
|
82
30
|
const getStringContent = (content) => {
|
|
83
31
|
if (typeof content === "string") return content;
|
|
84
|
-
return content.filter((c) => c.type === "text").map((c) => c.text).join("");
|
|
32
|
+
return contentBlocks(content).filter((c) => c.type === "text").map((c) => c.text).join("");
|
|
85
33
|
};
|
|
86
34
|
const convertLangChainBaseMessage = (message, metadata = {}) => {
|
|
87
35
|
switch (getMessageType(message)) {
|
|
@@ -128,7 +76,7 @@ const convertLangChainBaseMessage = (message, metadata = {}) => {
|
|
|
128
76
|
}
|
|
129
77
|
case "tool": return {
|
|
130
78
|
role: "tool",
|
|
131
|
-
toolName: message.name
|
|
79
|
+
toolName: message.name || void 0,
|
|
132
80
|
toolCallId: message.tool_call_id ?? "",
|
|
133
81
|
result: message.content,
|
|
134
82
|
artifact: message.artifact,
|
|
@@ -139,92 +87,11 @@ const convertLangChainBaseMessage = (message, metadata = {}) => {
|
|
|
139
87
|
id: message.id,
|
|
140
88
|
content: [{
|
|
141
89
|
type: "text",
|
|
142
|
-
text: typeof message.content === "string" ? message.content : JSON.stringify(message.content)
|
|
90
|
+
text: typeof message.content === "string" ? message.content : JSON.stringify(message.content) ?? ""
|
|
143
91
|
}]
|
|
144
92
|
};
|
|
145
93
|
}
|
|
146
94
|
};
|
|
147
|
-
/**
|
|
148
|
-
* Audio media types that reach a provider's audio input through the LangChain
|
|
149
|
-
* `audio` block. langchain-core derives OpenAI's `input_audio.format` by
|
|
150
|
-
* splitting `mime_type` on `/`, and that format is a wav-or-mp3 enum, so
|
|
151
|
-
* `audio/mpeg` passes the converter and is rejected at the provider.
|
|
152
|
-
*/
|
|
153
|
-
const audioBlockMimeTypes = /* @__PURE__ */ new Map([
|
|
154
|
-
["audio/mp3", "audio/mp3"],
|
|
155
|
-
["audio/mpeg", "audio/mp3"],
|
|
156
|
-
["audio/wav", "audio/wav"],
|
|
157
|
-
["audio/wave", "audio/wav"],
|
|
158
|
-
["audio/x-wav", "audio/wav"]
|
|
159
|
-
]);
|
|
160
|
-
const getMessageContent = (msg) => {
|
|
161
|
-
const allContent = [...msg.content, ...msg.attachments?.flatMap((a) => a.content) ?? []];
|
|
162
|
-
const hasNonText = allContent.some((part) => part.type === "file" || part.type === "image" || part.type === "audio");
|
|
163
|
-
const hasText = allContent.some((part) => part.type === "text");
|
|
164
|
-
if (hasNonText && !hasText) allContent.unshift({
|
|
165
|
-
type: "text",
|
|
166
|
-
text: " "
|
|
167
|
-
});
|
|
168
|
-
const content = allContent.flatMap((part) => {
|
|
169
|
-
const type = part.type;
|
|
170
|
-
switch (type) {
|
|
171
|
-
case "text": return {
|
|
172
|
-
type: "text",
|
|
173
|
-
text: part.text
|
|
174
|
-
};
|
|
175
|
-
case "image": return {
|
|
176
|
-
type: "image_url",
|
|
177
|
-
image_url: { url: part.image }
|
|
178
|
-
};
|
|
179
|
-
case "file": {
|
|
180
|
-
const metadata = { filename: part.filename ?? "file" };
|
|
181
|
-
if (part.sourceType === "id") return {
|
|
182
|
-
type: "file",
|
|
183
|
-
id: part.data,
|
|
184
|
-
mime_type: part.mimeType,
|
|
185
|
-
filename: metadata.filename,
|
|
186
|
-
metadata,
|
|
187
|
-
source_type: "id"
|
|
188
|
-
};
|
|
189
|
-
if (part.sourceType === "url" || httpUrlPattern.test(part.data)) return {
|
|
190
|
-
type: "file",
|
|
191
|
-
url: part.data,
|
|
192
|
-
mime_type: part.mimeType,
|
|
193
|
-
filename: metadata.filename,
|
|
194
|
-
metadata,
|
|
195
|
-
source_type: "url"
|
|
196
|
-
};
|
|
197
|
-
const parsed = parseDataUrl(part.data);
|
|
198
|
-
const audioMimeType = audioBlockMimeTypes.get((parsed?.mimeType ?? part.mimeType).toLowerCase());
|
|
199
|
-
if (audioMimeType) return {
|
|
200
|
-
type: "audio",
|
|
201
|
-
data: parsed?.data ?? part.data,
|
|
202
|
-
mime_type: audioMimeType,
|
|
203
|
-
source_type: "base64"
|
|
204
|
-
};
|
|
205
|
-
return {
|
|
206
|
-
type: "file",
|
|
207
|
-
data: parsed?.data ?? part.data,
|
|
208
|
-
mime_type: parsed?.mimeType ?? part.mimeType,
|
|
209
|
-
filename: metadata.filename,
|
|
210
|
-
metadata,
|
|
211
|
-
source_type: "base64"
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
case "audio": return {
|
|
215
|
-
type: "audio",
|
|
216
|
-
data: parseDataUrl(part.audio.data)?.data ?? part.audio.data,
|
|
217
|
-
mime_type: `audio/${part.audio.format}`,
|
|
218
|
-
source_type: "base64"
|
|
219
|
-
};
|
|
220
|
-
case "data": return [];
|
|
221
|
-
case "tool-call": throw new Error("Tool call appends are not supported.");
|
|
222
|
-
default: throw new Error(`Unsupported append message part type: ${type}`);
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
if (content.length === 1 && content[0]?.type === "text") return content[0].text ?? "";
|
|
226
|
-
return content;
|
|
227
|
-
};
|
|
228
95
|
//#endregion
|
|
229
96
|
export { convertLangChainBaseMessage, getMessageContent, getMessageType };
|
|
230
97
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convertMessages.js","names":["parts","_exhaustiveCheck"],"sources":["../src/convertMessages.ts"],"sourcesContent":["\"use client\";\n\nimport type { useExternalMessageConverter } from \"@assistant-ui/core/react\";\nimport type {\n AppendMessage,\n DataMessagePart,\n MessageTiming,\n} from \"@assistant-ui/core\";\nimport { httpUrlPattern, parseDataUrl } from \"@assistant-ui/core/internal\";\nimport type { ReadonlyJSONObject } from \"assistant-stream/utils\";\nimport type {\n LangChainBaseMessage,\n LangChainContentBlock,\n UIMessage,\n} from \"./types\";\n\ntype LangChainMessageConverterMetadata =\n useExternalMessageConverter.Metadata & {\n uiMessagesByParent?: Map<string, UIMessage[]>;\n messageTiming?: Record<string, MessageTiming>;\n };\n\nconst uiMessageToDataPart = (ui: UIMessage): DataMessagePart => ({\n type: \"data\",\n name: ui.name,\n data: ui.props,\n});\n\nexport const getMessageType = (message: LangChainBaseMessage): string => {\n if (typeof message._getType === \"function\") return message._getType();\n if (\"type\" in message)\n return (message as Record<string, unknown>).type as string;\n throw new Error(\"Cannot determine message type\");\n};\n\nconst contentToParts = (content: unknown) => {\n if (typeof content === \"string\")\n return [{ type: \"text\" as const, text: content }];\n\n const parts = content as readonly LangChainContentBlock[];\n return parts\n .map((part) => {\n const type = part.type;\n switch (type) {\n case \"text\":\n case \"text_delta\":\n return { type: \"text\" as const, text: part.text };\n case \"image_url\": {\n const image =\n typeof part.image_url === \"string\"\n ? part.image_url\n : part.image_url?.url;\n if (!image) return null;\n return { type: \"image\" as const, image };\n }\n case \"file\":\n return {\n type: \"file\" as const,\n filename: part.metadata?.filename ?? \"file\",\n data:\n part.source_type === \"url\"\n ? part.url\n : part.source_type === \"id\"\n ? part.id\n : part.data,\n mimeType: part.mime_type ?? \"application/octet-stream\",\n ...((part.source_type === \"url\" || part.source_type === \"id\") && {\n sourceType: part.source_type,\n }),\n };\n case \"audio\": {\n const mimeType = part.mime_type ?? \"application/octet-stream\";\n const subtype = mimeType.startsWith(\"audio/\")\n ? mimeType.slice(\"audio/\".length)\n : undefined;\n return {\n type: \"file\" as const,\n filename: subtype ? `audio.${subtype}` : \"audio\",\n data: part.data,\n mimeType,\n };\n }\n case \"thinking\":\n return { type: \"reasoning\" as const, text: part.thinking };\n case \"reasoning\":\n return {\n type: \"reasoning\" as const,\n text:\n part.summary?.map((s) => s?.text ?? \"\").join(\"\\n\\n\\n\") ??\n part.reasoning ??\n \"\",\n };\n case \"tool_use\":\n case \"input_json_delta\":\n return null;\n default:\n return null;\n }\n })\n .filter((p) => p !== null);\n};\n\nconst hasVisibleText = (text: unknown): boolean =>\n typeof text === \"string\" && text.trim() !== \"\";\n\n/**\n * Audio output arrives outside the content array: providers leave `content`\n * empty and put the spoken text in `additional_kwargs.audio.transcript`. The\n * audio bytes stay behind because no provider reports their media type, and a\n * streamed response carries raw PCM rather than a playable file.\n */\nconst withAudioTranscript = (\n parts: ReturnType<typeof contentToParts>,\n additionalKwargs: Record<string, unknown> | undefined,\n): ReturnType<typeof contentToParts> => {\n const audio = additionalKwargs?.audio as { transcript?: unknown } | undefined;\n const transcript = audio?.transcript;\n if (typeof transcript !== \"string\" || !hasVisibleText(transcript))\n return parts;\n if (parts.some((part) => part.type === \"text\" && hasVisibleText(part.text)))\n return parts;\n return [\n ...parts.filter((part) => part.type !== \"text\"),\n { type: \"text\" as const, text: transcript },\n ];\n};\n\nconst getCustomMetadata = (\n additionalKwargs: Record<string, unknown> | undefined,\n): Record<string, unknown> =>\n (additionalKwargs?.metadata as Record<string, unknown>) ?? {};\n\nconst getStringContent = (content: unknown): string => {\n if (typeof content === \"string\") return content;\n const parts = content as readonly LangChainContentBlock[];\n return parts\n .filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n .map((c) => c.text)\n .join(\"\");\n};\n\nexport const convertLangChainBaseMessage = (\n message: LangChainBaseMessage,\n metadata: LangChainMessageConverterMetadata = {},\n): useExternalMessageConverter.Message => {\n const type = getMessageType(message);\n\n switch (type) {\n case \"system\":\n return {\n role: \"system\",\n id: message.id,\n content: [{ type: \"text\", text: getStringContent(message.content) }],\n metadata: {\n custom: getCustomMetadata(message.additional_kwargs),\n },\n };\n\n case \"human\":\n return {\n role: \"user\",\n id: message.id,\n content: contentToParts(message.content),\n metadata: {\n custom: getCustomMetadata(message.additional_kwargs),\n },\n };\n\n case \"ai\": {\n const toolCallParts =\n message.tool_calls?.map((tc) => ({\n type: \"tool-call\" as const,\n toolCallId: tc.id,\n toolName: tc.name,\n args: tc.args as ReadonlyJSONObject,\n argsText: JSON.stringify(tc.args),\n })) ?? [];\n\n const assistantStatus =\n typeof message.status === \"object\" ? message.status : undefined;\n\n const uiDataParts =\n (message.id\n ? metadata.uiMessagesByParent\n ?.get(message.id)\n ?.map(uiMessageToDataPart)\n : undefined) ?? [];\n\n const timing = metadata.messageTiming?.[message.id ?? \"\"];\n\n return {\n role: \"assistant\",\n id: message.id,\n content: [\n ...withAudioTranscript(\n contentToParts(message.content),\n message.additional_kwargs,\n ),\n ...toolCallParts,\n ...uiDataParts,\n ],\n metadata: {\n custom: getCustomMetadata(message.additional_kwargs),\n ...(timing && { timing }),\n },\n ...(assistantStatus && { status: assistantStatus }),\n };\n }\n\n case \"tool\":\n return {\n role: \"tool\",\n toolName: message.name ?? \"\",\n toolCallId: message.tool_call_id ?? \"\",\n result: message.content,\n artifact: message.artifact,\n isError: message.status === \"error\",\n };\n\n default:\n return {\n role: \"system\",\n id: message.id,\n content: [\n {\n type: \"text\",\n text:\n typeof message.content === \"string\"\n ? message.content\n : JSON.stringify(message.content),\n },\n ],\n };\n }\n};\n\n/**\n * Audio media types that reach a provider's audio input through the LangChain\n * `audio` block. langchain-core derives OpenAI's `input_audio.format` by\n * splitting `mime_type` on `/`, and that format is a wav-or-mp3 enum, so\n * `audio/mpeg` passes the converter and is rejected at the provider.\n */\nconst audioBlockMimeTypes = new Map<string, \"audio/mp3\" | \"audio/wav\">([\n [\"audio/mp3\", \"audio/mp3\"],\n [\"audio/mpeg\", \"audio/mp3\"],\n [\"audio/wav\", \"audio/wav\"],\n [\"audio/wave\", \"audio/wav\"],\n [\"audio/x-wav\", \"audio/wav\"],\n]);\n\nexport const getMessageContent = (msg: AppendMessage) => {\n const allContent = [\n ...msg.content,\n ...(msg.attachments?.flatMap((a) => a.content) ?? []),\n ];\n\n const hasNonText = allContent.some(\n (part) =>\n part.type === \"file\" || part.type === \"image\" || part.type === \"audio\",\n );\n const hasText = allContent.some((part) => part.type === \"text\");\n if (hasNonText && !hasText) {\n allContent.unshift({ type: \"text\", text: \" \" });\n }\n\n const content = allContent.flatMap((part) => {\n const type = part.type;\n switch (type) {\n case \"text\":\n return { type: \"text\" as const, text: part.text };\n case \"image\":\n return { type: \"image_url\" as const, image_url: { url: part.image } };\n case \"file\": {\n const metadata = { filename: part.filename ?? \"file\" };\n if (part.sourceType === \"id\") {\n return {\n type: \"file\" as const,\n id: part.data,\n mime_type: part.mimeType,\n filename: metadata.filename,\n metadata,\n source_type: \"id\" as const,\n };\n }\n if (part.sourceType === \"url\" || httpUrlPattern.test(part.data)) {\n return {\n type: \"file\" as const,\n url: part.data,\n mime_type: part.mimeType,\n filename: metadata.filename,\n metadata,\n source_type: \"url\" as const,\n };\n }\n const parsed = parseDataUrl(part.data);\n const audioMimeType = audioBlockMimeTypes.get(\n (parsed?.mimeType ?? part.mimeType).toLowerCase(),\n );\n if (audioMimeType) {\n return {\n type: \"audio\" as const,\n data: parsed?.data ?? part.data,\n mime_type: audioMimeType,\n source_type: \"base64\" as const,\n };\n }\n return {\n type: \"file\" as const,\n data: parsed?.data ?? part.data,\n mime_type: parsed?.mimeType ?? part.mimeType,\n filename: metadata.filename,\n metadata,\n source_type: \"base64\" as const,\n };\n }\n case \"audio\": {\n const parsed = parseDataUrl(part.audio.data);\n return {\n type: \"audio\" as const,\n data: parsed?.data ?? part.audio.data,\n mime_type: `audio/${part.audio.format}`,\n source_type: \"base64\" as const,\n };\n }\n case \"data\":\n return [];\n case \"tool-call\":\n throw new Error(\"Tool call appends are not supported.\");\n default: {\n const _exhaustiveCheck: \"reasoning\" | \"source\" | \"generative-ui\" = type;\n throw new Error(\n `Unsupported append message part type: ${_exhaustiveCheck}`,\n );\n }\n }\n });\n\n if (content.length === 1 && content[0]?.type === \"text\") {\n return content[0].text ?? \"\";\n }\n return content;\n};\n"],"mappings":";;;AAsBA,MAAM,uBAAuB,QAAoC;CAC/D,MAAM;CACN,MAAM,GAAG;CACT,MAAM,GAAG;AACX;AAEA,MAAa,kBAAkB,YAA0C;CACvE,IAAI,OAAO,QAAQ,aAAa,YAA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sC;GACxD,SAEE,MAAM,IAAI,MACR,yCAAyCC,MAC3C;EAEJ;CACF,CAAC;CAED,IAAI,QAAQ,WAAW,KAAK,QAAQ,EAAE,EAAE,SAAS,QAC/C,OAAO,QAAQ,EAAE,CAAC,QAAQ;CAE5B,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"convertMessages.js","names":[],"sources":["../src/convertMessages.ts"],"sourcesContent":["\"use client\";\n\nimport type { MessageTiming } from \"@assistant-ui/core\";\nimport type { useExternalMessageConverter } from \"@assistant-ui/core/react\";\nimport type { ReadonlyJSONObject } from \"assistant-stream/utils\";\nimport {\n convertLangChainContentBlock,\n getCustomMetadata,\n uiMessageToDataPart,\n withAudioTranscript,\n} from \"./converter\";\nimport type {\n LangChainBaseMessage,\n LangChainContentBlock,\n UIMessage,\n} from \"./types\";\n\ntype LangChainMessageConverterMetadata =\n useExternalMessageConverter.Metadata & {\n uiMessagesByParent?: Map<string, UIMessage[]>;\n messageTiming?: Record<string, MessageTiming>;\n };\n\nconst warnedMalformedMessages = new Set<string>();\nconst warnOnceInDevelopment = (message: string) => {\n if (\n typeof process === \"undefined\" ||\n process?.env?.NODE_ENV !== \"development\"\n )\n return;\n if (warnedMalformedMessages.has(message)) return;\n warnedMalformedMessages.add(message);\n console.warn(message);\n};\n\nexport const getMessageType = (message: LangChainBaseMessage): string => {\n if (typeof message._getType === \"function\") return message._getType();\n if (\"type\" in message)\n return (message as Record<string, unknown>).type as string;\n warnOnceInDevelopment(\n \"Cannot determine message type; rendering the message as system text\",\n );\n return \"unknown\";\n};\n\nconst contentBlocks = (content: unknown): readonly LangChainContentBlock[] => {\n if (content == null) return [];\n if (Array.isArray(content))\n return content.filter(\n (block) => typeof block === \"object\" && block !== null,\n );\n warnOnceInDevelopment(\n `Ignoring message content that is neither a string nor an array: ${typeof content}`,\n );\n return [];\n};\n\nconst contentToParts = (content: unknown) => {\n if (typeof content === \"string\")\n return [{ type: \"text\" as const, text: content }];\n\n return contentBlocks(content)\n .map(convertLangChainContentBlock)\n .filter((part) => part !== null && part !== undefined);\n};\n\nconst getStringContent = (content: unknown): string => {\n if (typeof content === \"string\") return content;\n return contentBlocks(content)\n .filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n .map((c) => c.text)\n .join(\"\");\n};\n\nexport const convertLangChainBaseMessage = (\n message: LangChainBaseMessage,\n metadata: LangChainMessageConverterMetadata = {},\n): useExternalMessageConverter.Message => {\n const type = getMessageType(message);\n\n switch (type) {\n case \"system\":\n return {\n role: \"system\",\n id: message.id,\n content: [{ type: \"text\", text: getStringContent(message.content) }],\n metadata: {\n custom: getCustomMetadata(message.additional_kwargs),\n },\n };\n\n case \"human\":\n return {\n role: \"user\",\n id: message.id,\n content: contentToParts(message.content),\n metadata: {\n custom: getCustomMetadata(message.additional_kwargs),\n },\n };\n\n case \"ai\": {\n const toolCallParts =\n message.tool_calls?.map((tc) => ({\n type: \"tool-call\" as const,\n toolCallId: tc.id,\n toolName: tc.name,\n args: tc.args as ReadonlyJSONObject,\n argsText: JSON.stringify(tc.args),\n })) ?? [];\n\n const assistantStatus =\n typeof message.status === \"object\" ? message.status : undefined;\n\n const uiDataParts =\n (message.id\n ? metadata.uiMessagesByParent\n ?.get(message.id)\n ?.map(uiMessageToDataPart)\n : undefined) ?? [];\n\n const timing = metadata.messageTiming?.[message.id ?? \"\"];\n\n return {\n role: \"assistant\",\n id: message.id,\n content: [\n ...withAudioTranscript(\n contentToParts(message.content),\n message.additional_kwargs,\n ),\n ...toolCallParts,\n ...uiDataParts,\n ],\n metadata: {\n custom: getCustomMetadata(message.additional_kwargs),\n ...(timing && { timing }),\n },\n ...(assistantStatus && { status: assistantStatus }),\n };\n }\n\n case \"tool\":\n return {\n role: \"tool\",\n // `joinExternalMessages` only checks the name against the tool call\n // when it is non-null, so an empty name manufactures a mismatch.\n toolName: message.name || undefined,\n toolCallId: message.tool_call_id ?? \"\",\n result: message.content,\n artifact: message.artifact,\n isError: message.status === \"error\",\n };\n\n default:\n return {\n role: \"system\",\n id: message.id,\n content: [\n {\n type: \"text\",\n text:\n typeof message.content === \"string\"\n ? message.content\n : (JSON.stringify(message.content) ?? \"\"),\n },\n ],\n };\n }\n};\n\nexport { getMessageContent } from \"./converter\";\n"],"mappings":";;;AAuBA,MAAM,0CAA0B,IAAI,IAAY;AAChD,MAAM,yBAAyB,YAAoB;CACjD,IACE,OAAO,YAAY,eACnB,SAAS,KAAK,aAAa,eAE3B;CACF,IAAI,wBAAwB,IAAI,OAAO,GAAG;CAC1C,wBAAwB,IAAI,OAAO;CACnC,QAAQ,KAAK,OAAO;AACtB;AAEA,MAAa,kBAAkB,YAA0C;CACvE,IAAI,OAAO,QAAQ,aAAa,YAAY,OAAO,QAAQ,SAAS;CACpE,IAAI,UAAU,SACZ,OAAQ,QAAoC;CAC9C,sBACE,qEACF;CACA,OAAO;AACT;AAEA,MAAM,iBAAiB,YAAuD;CAC5E,IAAI,WAAW,MAAM,OAAO,CAAC;CAC7B,IAAI,MAAM,QAAQ,OAAO,GACvB,OAAO,QAAQ,QACZ,UAAU,OAAO,UAAU,YAAY,UAAU,IACpD;CACF,sBACE,mEAAmE,OAAO,SAC5E;CACA,OAAO,CAAC;AACV;AAEA,MAAM,kBAAkB,YAAqB;CAC3C,IAAI,OAAO,YAAY,UACrB,OAAO,CAAC;EAAE,MAAM;EAAiB,MAAM;CAAQ,CAAC;CAElD,OAAO,cAAc,OAAO,CAAC,CAC1B,IAAI,4BAA4B,CAAC,CACjC,QAAQ,SAAS,SAAS,QAAQ,SAAS,KAAA,CAAS;AACzD;AAEA,MAAM,oBAAoB,YAA6B;CACrD,IAAI,OAAO,YAAY,UAAU,OAAO;CACxC,OAAO,cAAc,OAAO,CAAC,CAC1B,QAAQ,MAA2C,EAAE,SAAS,MAAM,CAAC,CACrE,KAAK,MAAM,EAAE,IAAI,CAAC,CAClB,KAAK,EAAE;AACZ;AAEA,MAAa,+BACX,SACA,WAA8C,CAAC,MACP;CAGxC,QAFa,eAAe,OAEjB,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AAS,CACP;IACE,MAAM;IACN,MACE,OAAO,QAAQ,YAAY,WACvB,QAAQ,UACP,KAAK,UAAU,QAAQ,OAAO,KAAK;GAC5C,CACF;EACF;CACJ;AACF"}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { StreamingTimingAccessors } from "@assistant-ui/core/react";
|
|
2
|
+
import { AppendMessage, DataMessagePart, ThreadAssistantMessage, ThreadUserMessage } from "@assistant-ui/core";
|
|
3
|
+
//#region src/converter.d.ts
|
|
4
|
+
/** Known content block types from @langchain/core messages. */
|
|
5
|
+
type LangChainContentBlock = {
|
|
6
|
+
type: "text";
|
|
7
|
+
text: string;
|
|
8
|
+
} | {
|
|
9
|
+
type: "text_delta";
|
|
10
|
+
text: string;
|
|
11
|
+
} | {
|
|
12
|
+
type: "image_url";
|
|
13
|
+
image_url: string | {
|
|
14
|
+
url?: string;
|
|
15
|
+
};
|
|
16
|
+
} | {
|
|
17
|
+
type: "thinking";
|
|
18
|
+
thinking: string;
|
|
19
|
+
} | {
|
|
20
|
+
type: "reasoning";
|
|
21
|
+
summary?: Array<{
|
|
22
|
+
type: "summary_text";
|
|
23
|
+
text?: string;
|
|
24
|
+
}>;
|
|
25
|
+
reasoning?: string;
|
|
26
|
+
} | {
|
|
27
|
+
type: "file";
|
|
28
|
+
data: string;
|
|
29
|
+
mime_type: string;
|
|
30
|
+
source_type?: "base64";
|
|
31
|
+
metadata?: {
|
|
32
|
+
filename?: string;
|
|
33
|
+
};
|
|
34
|
+
} | {
|
|
35
|
+
type: "file";
|
|
36
|
+
url: string;
|
|
37
|
+
mime_type?: string;
|
|
38
|
+
source_type: "url";
|
|
39
|
+
metadata?: {
|
|
40
|
+
filename?: string;
|
|
41
|
+
};
|
|
42
|
+
} | {
|
|
43
|
+
type: "file";
|
|
44
|
+
id: string;
|
|
45
|
+
mime_type?: string;
|
|
46
|
+
source_type: "id";
|
|
47
|
+
metadata?: {
|
|
48
|
+
filename?: string;
|
|
49
|
+
};
|
|
50
|
+
} | {
|
|
51
|
+
type: "audio";
|
|
52
|
+
data: string;
|
|
53
|
+
mime_type: string;
|
|
54
|
+
source_type: "base64";
|
|
55
|
+
} | {
|
|
56
|
+
type: "tool_use" | "input_json_delta";
|
|
57
|
+
};
|
|
58
|
+
type ConvertedContentPart = ThreadUserMessage["content"][number] | ThreadAssistantMessage["content"][number];
|
|
59
|
+
declare const convertLangChainContentBlock: (part: LangChainContentBlock) => ConvertedContentPart | null | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Audio output arrives outside the content array: providers leave `content`
|
|
62
|
+
* empty and put the spoken text in `additional_kwargs.audio.transcript`. The
|
|
63
|
+
* audio bytes stay behind because no provider reports their media type, and a
|
|
64
|
+
* streamed response carries raw PCM rather than a playable file.
|
|
65
|
+
*/
|
|
66
|
+
declare const withAudioTranscript: <T extends {
|
|
67
|
+
type: string;
|
|
68
|
+
text?: unknown;
|
|
69
|
+
}>(parts: readonly T[], additionalKwargs: Record<string, unknown> | undefined) => readonly (T | {
|
|
70
|
+
type: "text";
|
|
71
|
+
text: string;
|
|
72
|
+
})[];
|
|
73
|
+
declare const getCustomMetadata: (additionalKwargs: Record<string, unknown> | undefined) => Record<string, unknown>;
|
|
74
|
+
declare const uiMessageToDataPart: <TUIMessage extends {
|
|
75
|
+
name: string;
|
|
76
|
+
props: Record<string, unknown>;
|
|
77
|
+
}>(ui: TUIMessage) => DataMessagePart;
|
|
78
|
+
declare const getMessageContent: (msg: AppendMessage) => string | ({
|
|
79
|
+
type: "text";
|
|
80
|
+
text: string;
|
|
81
|
+
image_url?: never;
|
|
82
|
+
id?: never;
|
|
83
|
+
mime_type?: never;
|
|
84
|
+
filename?: never;
|
|
85
|
+
metadata?: never;
|
|
86
|
+
source_type?: never;
|
|
87
|
+
url?: never;
|
|
88
|
+
data?: never;
|
|
89
|
+
} | {
|
|
90
|
+
type: "image_url";
|
|
91
|
+
image_url: {
|
|
92
|
+
url: string;
|
|
93
|
+
};
|
|
94
|
+
text?: never;
|
|
95
|
+
id?: never;
|
|
96
|
+
mime_type?: never;
|
|
97
|
+
filename?: never;
|
|
98
|
+
metadata?: never;
|
|
99
|
+
source_type?: never;
|
|
100
|
+
url?: never;
|
|
101
|
+
data?: never;
|
|
102
|
+
} | {
|
|
103
|
+
type: "file";
|
|
104
|
+
id: string;
|
|
105
|
+
mime_type: string;
|
|
106
|
+
filename: string;
|
|
107
|
+
metadata: {
|
|
108
|
+
filename: string;
|
|
109
|
+
};
|
|
110
|
+
source_type: "id";
|
|
111
|
+
text?: never;
|
|
112
|
+
image_url?: never;
|
|
113
|
+
url?: never;
|
|
114
|
+
data?: never;
|
|
115
|
+
} | {
|
|
116
|
+
type: "file";
|
|
117
|
+
url: string;
|
|
118
|
+
mime_type: string;
|
|
119
|
+
filename: string;
|
|
120
|
+
metadata: {
|
|
121
|
+
filename: string;
|
|
122
|
+
};
|
|
123
|
+
source_type: "url";
|
|
124
|
+
text?: never;
|
|
125
|
+
image_url?: never;
|
|
126
|
+
id?: never;
|
|
127
|
+
data?: never;
|
|
128
|
+
} | {
|
|
129
|
+
type: "file";
|
|
130
|
+
data: string;
|
|
131
|
+
mime_type: string;
|
|
132
|
+
filename: string;
|
|
133
|
+
metadata: {
|
|
134
|
+
filename: string;
|
|
135
|
+
};
|
|
136
|
+
source_type: "base64";
|
|
137
|
+
text?: never;
|
|
138
|
+
image_url?: never;
|
|
139
|
+
id?: never;
|
|
140
|
+
url?: never;
|
|
141
|
+
} | {
|
|
142
|
+
type: "audio";
|
|
143
|
+
data: string;
|
|
144
|
+
mime_type: string;
|
|
145
|
+
source_type: "base64";
|
|
146
|
+
text?: never;
|
|
147
|
+
image_url?: never;
|
|
148
|
+
id?: never;
|
|
149
|
+
filename?: never;
|
|
150
|
+
metadata?: never;
|
|
151
|
+
url?: never;
|
|
152
|
+
})[];
|
|
153
|
+
declare const createLangChainStreamingTimingAccessors: <TMessage extends {
|
|
154
|
+
id?: string | undefined;
|
|
155
|
+
content?: unknown;
|
|
156
|
+
tool_calls?: readonly unknown[] | undefined;
|
|
157
|
+
}>(getType: (message: TMessage) => string) => StreamingTimingAccessors<TMessage>;
|
|
158
|
+
/**
|
|
159
|
+
* Resolve the assistant message a `UIMessage` belongs to: the parent id comes
|
|
160
|
+
* from `metadata.message_id` (Python SDK) or `metadata.id` (JS SDK).
|
|
161
|
+
*/
|
|
162
|
+
declare const getUIMessageParentId: (ui: {
|
|
163
|
+
metadata?: {
|
|
164
|
+
message_id?: string;
|
|
165
|
+
id?: string;
|
|
166
|
+
} | undefined;
|
|
167
|
+
}) => string | undefined;
|
|
168
|
+
/**
|
|
169
|
+
* Group the graph's accumulated `UIMessage`s by the assistant message they
|
|
170
|
+
* belong to. Non-array state and entries without a parent link are dropped.
|
|
171
|
+
*/
|
|
172
|
+
declare const groupUIMessagesByParent: <T extends {
|
|
173
|
+
metadata?: {
|
|
174
|
+
message_id?: string;
|
|
175
|
+
id?: string;
|
|
176
|
+
} | undefined;
|
|
177
|
+
}>(value: unknown) => Map<string, T[]>;
|
|
178
|
+
//#endregion
|
|
179
|
+
export { LangChainContentBlock, convertLangChainContentBlock, createLangChainStreamingTimingAccessors, getCustomMetadata, getMessageContent, getUIMessageParentId, groupUIMessagesByParent, uiMessageToDataPart, withAudioTranscript };
|
|
180
|
+
//# sourceMappingURL=converter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"converter.d.ts","names":[],"sources":["../src/converter.ts"],"mappings":";;;;KAaY;EACN;EAAc;;EACd;EAAoB;;EACpB;EAAmB;IAAsB;;;EACzC;EAAkB;;EAElB;EACA,UAAU;IAAQ;IAAsB;;EACxC;;EAGA;EACA;EACA;EACA;EACA;IAAa;;;EAGb;EACA;EACA;EACA;EACA;IAAa;;;EAGb;EACA;EACA;EACA;EACA;IAAa;;;EAGb;EACA;EACA;EACA;;EAEA;;KAED,uBACD,uCACA;cAES,+BACX,MAAM,0BACL;;;;;;;cAoEU,sBAAuB;EAAY;EAAc;GAC5D,gBAAgB,KAChB,kBAAkB,kDACP;EAAM;EAAc;;cAapB,oBACX,kBAAkB,wCACjB;cAGU,sBACX;EAAqB;EAAc,OAAO;GAE1C,IAAI,eACH;cAoBU,oBAAqB,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsG1B,0CACX;EACE;EACA;EACA;GAGF,UAAU,SAAS,wBAClB,yBAAyB;;;;;cA8Df,uBAAwB;EACnC;IAAa;IAAqB;;;;;;;cAOvB,0BACX;EACE;IAAa;IAAqB;;GAGpC,mBACC,YAAY"}
|