@anvia/core 0.12.5 → 0.12.7
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.
|
@@ -102,6 +102,7 @@ function uiMessagesToCoreMessages(messages) {
|
|
|
102
102
|
function coreMessagesToUIMessages(messages) {
|
|
103
103
|
const uiMessages = [];
|
|
104
104
|
const toolNamesByCallKey = toolCallNameLookup(messages);
|
|
105
|
+
const toolPartsByCallKey = /* @__PURE__ */ new Map();
|
|
105
106
|
for (const message of messages) {
|
|
106
107
|
if (message.role === "system") {
|
|
107
108
|
uiMessages.push({
|
|
@@ -168,7 +169,9 @@ function coreMessagesToUIMessages(messages) {
|
|
|
168
169
|
data: content
|
|
169
170
|
});
|
|
170
171
|
}
|
|
172
|
+
const messageIndex = uiMessages.length;
|
|
171
173
|
uiMessages.push({ id: message.id ?? createId("msg"), role: "assistant", parts: parts2 });
|
|
174
|
+
registerToolPartLocations(parts2, messageIndex, toolPartsByCallKey);
|
|
172
175
|
continue;
|
|
173
176
|
}
|
|
174
177
|
const parts = [];
|
|
@@ -184,12 +187,57 @@ function coreMessagesToUIMessages(messages) {
|
|
|
184
187
|
if (content.callId !== void 0) {
|
|
185
188
|
part.callId = content.callId;
|
|
186
189
|
}
|
|
190
|
+
if (mergeToolResultPart(uiMessages, toolPartsByCallKey, part)) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
187
193
|
parts.push(part);
|
|
188
194
|
}
|
|
189
|
-
|
|
195
|
+
if (parts.length > 0) {
|
|
196
|
+
uiMessages.push({ id: createId("msg"), role: "tool", parts });
|
|
197
|
+
}
|
|
190
198
|
}
|
|
191
199
|
return uiMessages;
|
|
192
200
|
}
|
|
201
|
+
function registerToolPartLocations(parts, messageIndex, toolPartsByCallKey) {
|
|
202
|
+
for (const [partIndex, part] of parts.entries()) {
|
|
203
|
+
if (part.type !== "tool") {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const location = { messageIndex, partIndex };
|
|
207
|
+
toolPartsByCallKey.set(part.toolCallId, location);
|
|
208
|
+
if (part.callId !== void 0) {
|
|
209
|
+
toolPartsByCallKey.set(part.callId, location);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function mergeToolResultPart(uiMessages, toolPartsByCallKey, resultPart) {
|
|
214
|
+
if (resultPart.type !== "tool") {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
const location = toolPartsByCallKey.get(resultPart.toolCallId) ?? (resultPart.callId === void 0 ? void 0 : toolPartsByCallKey.get(resultPart.callId));
|
|
218
|
+
if (location === void 0) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
const message = uiMessages[location.messageIndex];
|
|
222
|
+
const currentPart = message?.parts[location.partIndex];
|
|
223
|
+
if (message === void 0 || currentPart?.type !== "tool") {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
const mergedPart = {
|
|
227
|
+
...currentPart,
|
|
228
|
+
toolName: resultPart.toolName === "tool" ? currentPart.toolName : resultPart.toolName,
|
|
229
|
+
state: "output-available",
|
|
230
|
+
...resultPart.output === void 0 ? {} : { output: resultPart.output }
|
|
231
|
+
};
|
|
232
|
+
if (currentPart.callId === void 0 && resultPart.callId !== void 0) {
|
|
233
|
+
mergedPart.callId = resultPart.callId;
|
|
234
|
+
toolPartsByCallKey.set(resultPart.callId, location);
|
|
235
|
+
}
|
|
236
|
+
const nextParts = [...message.parts];
|
|
237
|
+
nextParts[location.partIndex] = mergedPart;
|
|
238
|
+
uiMessages[location.messageIndex] = { ...message, parts: nextParts };
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
193
241
|
function toolCallNameLookup(messages) {
|
|
194
242
|
const toolNamesByCallKey = /* @__PURE__ */ new Map();
|
|
195
243
|
for (const message of messages) {
|
|
@@ -370,4 +418,4 @@ export {
|
|
|
370
418
|
uiMessagesToCoreMessages,
|
|
371
419
|
coreMessagesToUIMessages
|
|
372
420
|
};
|
|
373
|
-
//# sourceMappingURL=chunk-
|
|
421
|
+
//# sourceMappingURL=chunk-WEOG3JCT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ui/messages.ts"],"sourcesContent":["import {\n AssistantContent,\n type AssistantContent as AssistantContentType,\n type Message as CoreMessage,\n type DocumentContent,\n type ImageContent,\n type ImageDetail,\n type JsonValue,\n Message,\n reasoningDisplayText,\n serializeToolResultOutput,\n ToolContent,\n type ToolContent as ToolContentType,\n type ToolResultContent,\n UserContent,\n type UserContent as UserContentType,\n} from \"../completion/types\";\nimport type { UIAttachment, UIMessage, UIMessagePart } from \"./types\";\n\ntype UIToolPartLocation = {\n messageIndex: number;\n partIndex: number;\n};\n\ntype UIToolMessagePart = Extract<UIMessagePart, { type: \"tool\" }>;\n\nexport function uiMessagesToCoreMessages(messages: UIMessage[]): CoreMessage[] {\n const coreMessages: CoreMessage[] = [];\n\n for (const message of messages) {\n const text = textFromUIParts(message.parts);\n\n if (message.role === \"system\") {\n if (text.length > 0) {\n coreMessages.push(Message.system(text));\n }\n continue;\n }\n\n if (message.role === \"user\") {\n const content: UserContentType[] = [];\n for (const part of message.parts) {\n if (part.type === \"text\") {\n content.push(UserContent.text(part.text));\n continue;\n }\n if (part.type === \"attachment\") {\n content.push(attachmentToUserContent(part.attachment));\n continue;\n }\n if (part.type === \"data\" && isUserContent(part.data)) {\n content.push(part.data);\n continue;\n }\n throw new TypeError(\n \"User UI messages can only be converted from text, attachment, or image/document data parts.\",\n );\n }\n if (content.length > 0) {\n coreMessages.push(Message.user(content));\n }\n continue;\n }\n\n if (message.role === \"assistant\") {\n const content: AssistantContentType[] = [];\n const toolResults: ToolContentType[] = [];\n for (const part of message.parts) {\n if (part.type === \"text\" && part.text.length > 0) {\n content.push(AssistantContent.text(part.text));\n continue;\n }\n if (part.type === \"reasoning\" && part.text.length > 0) {\n content.push(AssistantContent.reasoning(part.text, part.reasoningId));\n continue;\n }\n if (\n part.type === \"tool\" &&\n (part.state === \"input-streaming\" ||\n part.state === \"input-available\" ||\n part.state === \"output-available\")\n ) {\n content.push(\n AssistantContent.toolCall(\n part.toolCallId,\n part.toolName,\n part.input ?? {},\n part.callId,\n ),\n );\n if (part.state === \"output-available\") {\n toolResults.push(\n ToolContent.toolResult(part.toolCallId, outputToToolResultContent(part.output), {\n callId: part.callId,\n toolName: part.toolName,\n }),\n );\n }\n }\n }\n if (content.length > 0) {\n coreMessages.push(Message.assistant(content, message.id));\n }\n if (toolResults.length > 0) {\n coreMessages.push(Message.tool(toolResults));\n }\n continue;\n }\n\n const toolResults: ToolContentType[] = [];\n for (const part of message.parts) {\n if (part.type !== \"tool\" || part.state !== \"output-available\") {\n continue;\n }\n toolResults.push(\n ToolContent.toolResult(part.toolCallId, outputToToolResultContent(part.output), {\n callId: part.callId,\n toolName: part.toolName,\n }),\n );\n }\n if (toolResults.length > 0) {\n coreMessages.push(Message.tool(toolResults));\n }\n }\n\n return coreMessages;\n}\n\nexport function coreMessagesToUIMessages(messages: CoreMessage[]): UIMessage[] {\n const uiMessages: UIMessage[] = [];\n const toolNamesByCallKey = toolCallNameLookup(messages);\n const toolPartsByCallKey = new Map<string, UIToolPartLocation>();\n\n for (const message of messages) {\n if (message.role === \"system\") {\n uiMessages.push({\n id: createId(\"msg\"),\n role: \"system\",\n parts: [{ id: createId(\"part\"), type: \"text\", text: message.content }],\n });\n continue;\n }\n\n if (message.role === \"user\") {\n const parts: UIMessagePart[] = [];\n for (const content of message.content) {\n if (content.type === \"text\") {\n parts.push({ id: createId(\"part\"), type: \"text\", text: content.text });\n } else {\n parts.push({\n id: createId(\"part\"),\n type: \"attachment\",\n attachment: userContentToAttachment(content),\n });\n }\n }\n uiMessages.push({ id: createId(\"msg\"), role: \"user\", parts });\n continue;\n }\n\n if (message.role === \"assistant\") {\n const parts: UIMessagePart[] = [];\n for (const content of message.content) {\n if (content.type === \"text\") {\n parts.push({ id: createId(\"part\"), type: \"text\", text: content.text });\n continue;\n }\n if (content.type === \"reasoning\") {\n const part: UIMessagePart = {\n id: createId(\"part\"),\n type: \"reasoning\",\n text: reasoningDisplayText(content),\n };\n if (content.id !== undefined) {\n part.reasoningId = content.id;\n }\n parts.push(part);\n continue;\n }\n if (content.type === \"tool_call\") {\n const part: UIMessagePart = {\n id: toolPartId(content.id),\n type: \"tool\",\n toolName: content.function.name,\n toolCallId: content.id,\n state: \"input-available\",\n input: content.function.arguments,\n };\n if (content.callId !== undefined) {\n part.callId = content.callId;\n }\n parts.push(part);\n continue;\n }\n parts.push({\n id: createId(\"part\"),\n type: \"data\",\n name: content.type,\n data: content as JsonValue,\n });\n }\n const messageIndex = uiMessages.length;\n uiMessages.push({ id: message.id ?? createId(\"msg\"), role: \"assistant\", parts });\n registerToolPartLocations(parts, messageIndex, toolPartsByCallKey);\n continue;\n }\n\n const parts: UIMessagePart[] = [];\n for (const content of message.content) {\n const part: UIToolMessagePart = {\n id: toolPartId(content.id),\n type: \"tool\",\n toolName: toolNameForResult(content, toolNamesByCallKey),\n toolCallId: content.id,\n state: \"output-available\",\n output: toolResultContentToJson(content.content),\n };\n if (content.callId !== undefined) {\n part.callId = content.callId;\n }\n if (mergeToolResultPart(uiMessages, toolPartsByCallKey, part)) {\n continue;\n }\n parts.push(part);\n }\n if (parts.length > 0) {\n uiMessages.push({ id: createId(\"msg\"), role: \"tool\", parts });\n }\n }\n\n return uiMessages;\n}\n\nfunction registerToolPartLocations(\n parts: UIMessagePart[],\n messageIndex: number,\n toolPartsByCallKey: Map<string, UIToolPartLocation>,\n): void {\n for (const [partIndex, part] of parts.entries()) {\n if (part.type !== \"tool\") {\n continue;\n }\n const location = { messageIndex, partIndex };\n toolPartsByCallKey.set(part.toolCallId, location);\n if (part.callId !== undefined) {\n toolPartsByCallKey.set(part.callId, location);\n }\n }\n}\n\nfunction mergeToolResultPart(\n uiMessages: UIMessage[],\n toolPartsByCallKey: Map<string, UIToolPartLocation>,\n resultPart: UIMessagePart,\n): boolean {\n if (resultPart.type !== \"tool\") {\n return false;\n }\n\n const location =\n toolPartsByCallKey.get(resultPart.toolCallId) ??\n (resultPart.callId === undefined ? undefined : toolPartsByCallKey.get(resultPart.callId));\n if (location === undefined) {\n return false;\n }\n\n const message = uiMessages[location.messageIndex];\n const currentPart = message?.parts[location.partIndex];\n if (message === undefined || currentPart?.type !== \"tool\") {\n return false;\n }\n\n const mergedPart: UIToolMessagePart = {\n ...currentPart,\n toolName: resultPart.toolName === \"tool\" ? currentPart.toolName : resultPart.toolName,\n state: \"output-available\",\n ...(resultPart.output === undefined ? {} : { output: resultPart.output }),\n };\n if (currentPart.callId === undefined && resultPart.callId !== undefined) {\n mergedPart.callId = resultPart.callId;\n toolPartsByCallKey.set(resultPart.callId, location);\n }\n\n const nextParts = [...message.parts];\n nextParts[location.partIndex] = mergedPart;\n uiMessages[location.messageIndex] = { ...message, parts: nextParts };\n return true;\n}\n\nfunction toolCallNameLookup(messages: CoreMessage[]): Map<string, string> {\n const toolNamesByCallKey = new Map<string, string>();\n\n for (const message of messages) {\n if (message.role !== \"assistant\") {\n continue;\n }\n for (const content of message.content) {\n if (content.type !== \"tool_call\") {\n continue;\n }\n toolNamesByCallKey.set(content.id, content.function.name);\n if (content.callId !== undefined) {\n toolNamesByCallKey.set(content.callId, content.function.name);\n }\n }\n }\n\n return toolNamesByCallKey;\n}\n\nfunction toolNameForResult(\n content: ToolContentType,\n toolNamesByCallKey: Map<string, string>,\n): string {\n return (\n content.toolName ??\n toolNamesByCallKey.get(content.id) ??\n (content.callId === undefined ? undefined : toolNamesByCallKey.get(content.callId)) ??\n \"tool\"\n );\n}\n\nexport function isUIMessage(value: unknown): value is UIMessage {\n return (\n isRecord(value) &&\n typeof value.id === \"string\" &&\n isUIMessageRole(value.role) &&\n Array.isArray(value.parts)\n );\n}\n\nexport function isUIMessageArray(value: unknown): value is UIMessage[] {\n return Array.isArray(value) && value.every(isUIMessage);\n}\n\nfunction textFromUIParts(parts: UIMessagePart[]): string {\n return parts.flatMap((part) => (part.type === \"text\" ? [part.text] : [])).join(\"\");\n}\n\nfunction outputToToolResultContent(output: JsonValue | undefined): ToolResultContent[] {\n return [{ type: \"text\", text: serializeToolResultOutput(output ?? null) }];\n}\n\nfunction attachmentToUserContent(attachment: UIAttachment): UserContentType {\n if (attachment.type === \"image\" || isImageFileAttachment(attachment)) {\n if (attachment.url !== undefined) {\n return UserContent.imageUrl(attachment.url, imageOptions(attachment));\n }\n if (attachment.data !== undefined && attachment.mediaType !== undefined) {\n return UserContent.imageBase64(\n attachment.data,\n attachment.mediaType,\n imageOptions(attachment),\n );\n }\n throw new TypeError(\"Image attachments require a url or base64 data with mediaType.\");\n }\n\n if (attachment.text !== undefined) {\n return {\n type: \"document\",\n source: {\n type: \"text\",\n text: attachment.text,\n ...(attachment.mediaType === undefined ? {} : { mediaType: attachment.mediaType }),\n ...(attachment.name === undefined ? {} : { filename: attachment.name }),\n },\n };\n }\n\n const mediaType = attachment.mediaType ?? \"application/octet-stream\";\n if (attachment.url !== undefined) {\n return UserContent.documentUrl(attachment.url, mediaType, documentOptions(attachment));\n }\n if (attachment.data !== undefined) {\n return UserContent.documentBase64(attachment.data, mediaType, documentOptions(attachment));\n }\n\n throw new TypeError(\"Document attachments require a url, base64 data, or text.\");\n}\n\nfunction userContentToAttachment(content: ImageContent | DocumentContent): UIAttachment {\n if (content.type === \"image\") {\n const attachment: UIAttachment = {\n id: createId(\"attachment\"),\n type: \"image\",\n ...(content.detail === undefined ? {} : { detail: content.detail }),\n };\n if (content.source.type === \"url\") {\n attachment.url = content.source.url;\n } else {\n attachment.data = content.source.data;\n attachment.mediaType = content.source.mediaType;\n }\n return attachment;\n }\n\n const attachment: UIAttachment = {\n id: createId(\"attachment\"),\n type: \"document\",\n };\n if (content.source.type === \"url\") {\n attachment.url = content.source.url;\n attachment.mediaType = content.source.mediaType;\n if (content.source.filename !== undefined) {\n attachment.name = content.source.filename;\n }\n } else if (content.source.type === \"base64\") {\n attachment.data = content.source.data;\n attachment.mediaType = content.source.mediaType;\n if (content.source.filename !== undefined) {\n attachment.name = content.source.filename;\n }\n } else {\n attachment.text = content.source.text;\n if (content.source.mediaType !== undefined) {\n attachment.mediaType = content.source.mediaType;\n }\n if (content.source.filename !== undefined) {\n attachment.name = content.source.filename;\n }\n }\n return attachment;\n}\n\nfunction isImageFileAttachment(attachment: UIAttachment): boolean {\n return attachment.type === \"file\" && attachment.mediaType?.startsWith(\"image/\") === true;\n}\n\nfunction imageOptions(attachment: UIAttachment): { detail?: ImageDetail } {\n return attachment.detail === undefined ? {} : { detail: attachment.detail };\n}\n\nfunction documentOptions(attachment: UIAttachment): { filename?: string } {\n return attachment.name === undefined ? {} : { filename: attachment.name };\n}\n\nfunction toolResultContentToJson(content: ToolResultContent[]): JsonValue {\n if (content.length === 1 && content[0]?.type === \"text\") {\n return content[0].text;\n }\n return content as JsonValue;\n}\n\nfunction isUserContent(value: unknown): value is UserContentType {\n if (!isRecord(value)) {\n return false;\n }\n\n if (value.type === \"text\") {\n return typeof value.text === \"string\";\n }\n\n if (value.type === \"image\") {\n return isImageContent(value);\n }\n\n if (value.type === \"document\") {\n return isDocumentContent(value);\n }\n\n return false;\n}\n\nfunction isImageContent(value: Record<string, unknown>): boolean {\n if (!isRecord(value.source)) {\n return false;\n }\n\n if (value.source.type === \"url\") {\n return typeof value.source.url === \"string\";\n }\n\n if (value.source.type === \"base64\") {\n return typeof value.source.data === \"string\" && typeof value.source.mediaType === \"string\";\n }\n\n return false;\n}\n\nfunction isDocumentContent(value: Record<string, unknown>): boolean {\n if (!isRecord(value.source)) {\n return false;\n }\n\n if (value.source.type === \"url\") {\n return typeof value.source.url === \"string\" && typeof value.source.mediaType === \"string\";\n }\n\n if (value.source.type === \"base64\") {\n return typeof value.source.data === \"string\" && typeof value.source.mediaType === \"string\";\n }\n\n if (value.source.type === \"text\") {\n return (\n typeof value.source.text === \"string\" &&\n (value.source.mediaType === undefined || typeof value.source.mediaType === \"string\")\n );\n }\n\n return false;\n}\n\nfunction toolPartId(toolCallId: string): string {\n return `tool_${toolCallId}`;\n}\n\nlet nextId = 0;\n\nfunction createId(prefix: string): string {\n const random = globalThis.crypto?.randomUUID?.();\n if (random !== undefined) {\n return `${prefix}_${random}`;\n }\n nextId += 1;\n return `${prefix}_${nextId.toString(36)}`;\n}\n\nfunction isUIMessageRole(value: unknown): value is UIMessage[\"role\"] {\n return value === \"system\" || value === \"user\" || value === \"assistant\" || value === \"tool\";\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null;\n}\n"],"mappings":";;;;;;;;;;AA0BO,SAAS,yBAAyB,UAAsC;AAC7E,QAAM,eAA8B,CAAC;AAErC,aAAW,WAAW,UAAU;AAC9B,UAAM,OAAO,gBAAgB,QAAQ,KAAK;AAE1C,QAAI,QAAQ,SAAS,UAAU;AAC7B,UAAI,KAAK,SAAS,GAAG;AACnB,qBAAa,KAAK,QAAQ,OAAO,IAAI,CAAC;AAAA,MACxC;AACA;AAAA,IACF;AAEA,QAAI,QAAQ,SAAS,QAAQ;AAC3B,YAAM,UAA6B,CAAC;AACpC,iBAAW,QAAQ,QAAQ,OAAO;AAChC,YAAI,KAAK,SAAS,QAAQ;AACxB,kBAAQ,KAAK,YAAY,KAAK,KAAK,IAAI,CAAC;AACxC;AAAA,QACF;AACA,YAAI,KAAK,SAAS,cAAc;AAC9B,kBAAQ,KAAK,wBAAwB,KAAK,UAAU,CAAC;AACrD;AAAA,QACF;AACA,YAAI,KAAK,SAAS,UAAU,cAAc,KAAK,IAAI,GAAG;AACpD,kBAAQ,KAAK,KAAK,IAAI;AACtB;AAAA,QACF;AACA,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AACA,UAAI,QAAQ,SAAS,GAAG;AACtB,qBAAa,KAAK,QAAQ,KAAK,OAAO,CAAC;AAAA,MACzC;AACA;AAAA,IACF;AAEA,QAAI,QAAQ,SAAS,aAAa;AAChC,YAAM,UAAkC,CAAC;AACzC,YAAMA,eAAiC,C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names":["toolResults","parts","attachment"]}
|
package/dist/index.js
CHANGED
package/dist/ui/index.d.ts
CHANGED
package/dist/ui/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ui/messages.ts"],"sourcesContent":["import {\n AssistantContent,\n type AssistantContent as AssistantContentType,\n type Message as CoreMessage,\n type DocumentContent,\n type ImageContent,\n type ImageDetail,\n type JsonValue,\n Message,\n reasoningDisplayText,\n serializeToolResultOutput,\n ToolContent,\n type ToolContent as ToolContentType,\n type ToolResultContent,\n UserContent,\n type UserContent as UserContentType,\n} from \"../completion/types\";\nimport type { UIAttachment, UIMessage, UIMessagePart } from \"./types\";\n\nexport function uiMessagesToCoreMessages(messages: UIMessage[]): CoreMessage[] {\n const coreMessages: CoreMessage[] = [];\n\n for (const message of messages) {\n const text = textFromUIParts(message.parts);\n\n if (message.role === \"system\") {\n if (text.length > 0) {\n coreMessages.push(Message.system(text));\n }\n continue;\n }\n\n if (message.role === \"user\") {\n const content: UserContentType[] = [];\n for (const part of message.parts) {\n if (part.type === \"text\") {\n content.push(UserContent.text(part.text));\n continue;\n }\n if (part.type === \"attachment\") {\n content.push(attachmentToUserContent(part.attachment));\n continue;\n }\n if (part.type === \"data\" && isUserContent(part.data)) {\n content.push(part.data);\n continue;\n }\n throw new TypeError(\n \"User UI messages can only be converted from text, attachment, or image/document data parts.\",\n );\n }\n if (content.length > 0) {\n coreMessages.push(Message.user(content));\n }\n continue;\n }\n\n if (message.role === \"assistant\") {\n const content: AssistantContentType[] = [];\n const toolResults: ToolContentType[] = [];\n for (const part of message.parts) {\n if (part.type === \"text\" && part.text.length > 0) {\n content.push(AssistantContent.text(part.text));\n continue;\n }\n if (part.type === \"reasoning\" && part.text.length > 0) {\n content.push(AssistantContent.reasoning(part.text, part.reasoningId));\n continue;\n }\n if (\n part.type === \"tool\" &&\n (part.state === \"input-streaming\" ||\n part.state === \"input-available\" ||\n part.state === \"output-available\")\n ) {\n content.push(\n AssistantContent.toolCall(\n part.toolCallId,\n part.toolName,\n part.input ?? {},\n part.callId,\n ),\n );\n if (part.state === \"output-available\") {\n toolResults.push(\n ToolContent.toolResult(part.toolCallId, outputToToolResultContent(part.output), {\n callId: part.callId,\n toolName: part.toolName,\n }),\n );\n }\n }\n }\n if (content.length > 0) {\n coreMessages.push(Message.assistant(content, message.id));\n }\n if (toolResults.length > 0) {\n coreMessages.push(Message.tool(toolResults));\n }\n continue;\n }\n\n const toolResults: ToolContentType[] = [];\n for (const part of message.parts) {\n if (part.type !== \"tool\" || part.state !== \"output-available\") {\n continue;\n }\n toolResults.push(\n ToolContent.toolResult(part.toolCallId, outputToToolResultContent(part.output), {\n callId: part.callId,\n toolName: part.toolName,\n }),\n );\n }\n if (toolResults.length > 0) {\n coreMessages.push(Message.tool(toolResults));\n }\n }\n\n return coreMessages;\n}\n\nexport function coreMessagesToUIMessages(messages: CoreMessage[]): UIMessage[] {\n const uiMessages: UIMessage[] = [];\n const toolNamesByCallKey = toolCallNameLookup(messages);\n\n for (const message of messages) {\n if (message.role === \"system\") {\n uiMessages.push({\n id: createId(\"msg\"),\n role: \"system\",\n parts: [{ id: createId(\"part\"), type: \"text\", text: message.content }],\n });\n continue;\n }\n\n if (message.role === \"user\") {\n const parts: UIMessagePart[] = [];\n for (const content of message.content) {\n if (content.type === \"text\") {\n parts.push({ id: createId(\"part\"), type: \"text\", text: content.text });\n } else {\n parts.push({\n id: createId(\"part\"),\n type: \"attachment\",\n attachment: userContentToAttachment(content),\n });\n }\n }\n uiMessages.push({ id: createId(\"msg\"), role: \"user\", parts });\n continue;\n }\n\n if (message.role === \"assistant\") {\n const parts: UIMessagePart[] = [];\n for (const content of message.content) {\n if (content.type === \"text\") {\n parts.push({ id: createId(\"part\"), type: \"text\", text: content.text });\n continue;\n }\n if (content.type === \"reasoning\") {\n const part: UIMessagePart = {\n id: createId(\"part\"),\n type: \"reasoning\",\n text: reasoningDisplayText(content),\n };\n if (content.id !== undefined) {\n part.reasoningId = content.id;\n }\n parts.push(part);\n continue;\n }\n if (content.type === \"tool_call\") {\n const part: UIMessagePart = {\n id: toolPartId(content.id),\n type: \"tool\",\n toolName: content.function.name,\n toolCallId: content.id,\n state: \"input-available\",\n input: content.function.arguments,\n };\n if (content.callId !== undefined) {\n part.callId = content.callId;\n }\n parts.push(part);\n continue;\n }\n parts.push({\n id: createId(\"part\"),\n type: \"data\",\n name: content.type,\n data: content as JsonValue,\n });\n }\n uiMessages.push({ id: message.id ?? createId(\"msg\"), role: \"assistant\", parts });\n continue;\n }\n\n const parts: UIMessagePart[] = [];\n for (const content of message.content) {\n const part: UIMessagePart = {\n id: toolPartId(content.id),\n type: \"tool\",\n toolName: toolNameForResult(content, toolNamesByCallKey),\n toolCallId: content.id,\n state: \"output-available\",\n output: toolResultContentToJson(content.content),\n };\n if (content.callId !== undefined) {\n part.callId = content.callId;\n }\n parts.push(part);\n }\n uiMessages.push({ id: createId(\"msg\"), role: \"tool\", parts });\n }\n\n return uiMessages;\n}\n\nfunction toolCallNameLookup(messages: CoreMessage[]): Map<string, string> {\n const toolNamesByCallKey = new Map<string, string>();\n\n for (const message of messages) {\n if (message.role !== \"assistant\") {\n continue;\n }\n for (const content of message.content) {\n if (content.type !== \"tool_call\") {\n continue;\n }\n toolNamesByCallKey.set(content.id, content.function.name);\n if (content.callId !== undefined) {\n toolNamesByCallKey.set(content.callId, content.function.name);\n }\n }\n }\n\n return toolNamesByCallKey;\n}\n\nfunction toolNameForResult(\n content: ToolContentType,\n toolNamesByCallKey: Map<string, string>,\n): string {\n return (\n content.toolName ??\n toolNamesByCallKey.get(content.id) ??\n (content.callId === undefined ? undefined : toolNamesByCallKey.get(content.callId)) ??\n \"tool\"\n );\n}\n\nexport function isUIMessage(value: unknown): value is UIMessage {\n return (\n isRecord(value) &&\n typeof value.id === \"string\" &&\n isUIMessageRole(value.role) &&\n Array.isArray(value.parts)\n );\n}\n\nexport function isUIMessageArray(value: unknown): value is UIMessage[] {\n return Array.isArray(value) && value.every(isUIMessage);\n}\n\nfunction textFromUIParts(parts: UIMessagePart[]): string {\n return parts.flatMap((part) => (part.type === \"text\" ? [part.text] : [])).join(\"\");\n}\n\nfunction outputToToolResultContent(output: JsonValue | undefined): ToolResultContent[] {\n return [{ type: \"text\", text: serializeToolResultOutput(output ?? null) }];\n}\n\nfunction attachmentToUserContent(attachment: UIAttachment): UserContentType {\n if (attachment.type === \"image\" || isImageFileAttachment(attachment)) {\n if (attachment.url !== undefined) {\n return UserContent.imageUrl(attachment.url, imageOptions(attachment));\n }\n if (attachment.data !== undefined && attachment.mediaType !== undefined) {\n return UserContent.imageBase64(\n attachment.data,\n attachment.mediaType,\n imageOptions(attachment),\n );\n }\n throw new TypeError(\"Image attachments require a url or base64 data with mediaType.\");\n }\n\n if (attachment.text !== undefined) {\n return {\n type: \"document\",\n source: {\n type: \"text\",\n text: attachment.text,\n ...(attachment.mediaType === undefined ? {} : { mediaType: attachment.mediaType }),\n ...(attachment.name === undefined ? {} : { filename: attachment.name }),\n },\n };\n }\n\n const mediaType = attachment.mediaType ?? \"application/octet-stream\";\n if (attachment.url !== undefined) {\n return UserContent.documentUrl(attachment.url, mediaType, documentOptions(attachment));\n }\n if (attachment.data !== undefined) {\n return UserContent.documentBase64(attachment.data, mediaType, documentOptions(attachment));\n }\n\n throw new TypeError(\"Document attachments require a url, base64 data, or text.\");\n}\n\nfunction userContentToAttachment(content: ImageContent | DocumentContent): UIAttachment {\n if (content.type === \"image\") {\n const attachment: UIAttachment = {\n id: createId(\"attachment\"),\n type: \"image\",\n ...(content.detail === undefined ? {} : { detail: content.detail }),\n };\n if (content.source.type === \"url\") {\n attachment.url = content.source.url;\n } else {\n attachment.data = content.source.data;\n attachment.mediaType = content.source.mediaType;\n }\n return attachment;\n }\n\n const attachment: UIAttachment = {\n id: createId(\"attachment\"),\n type: \"document\",\n };\n if (content.source.type === \"url\") {\n attachment.url = content.source.url;\n attachment.mediaType = content.source.mediaType;\n if (content.source.filename !== undefined) {\n attachment.name = content.source.filename;\n }\n } else if (content.source.type === \"base64\") {\n attachment.data = content.source.data;\n attachment.mediaType = content.source.mediaType;\n if (content.source.filename !== undefined) {\n attachment.name = content.source.filename;\n }\n } else {\n attachment.text = content.source.text;\n if (content.source.mediaType !== undefined) {\n attachment.mediaType = content.source.mediaType;\n }\n if (content.source.filename !== undefined) {\n attachment.name = content.source.filename;\n }\n }\n return attachment;\n}\n\nfunction isImageFileAttachment(attachment: UIAttachment): boolean {\n return attachment.type === \"file\" && attachment.mediaType?.startsWith(\"image/\") === true;\n}\n\nfunction imageOptions(attachment: UIAttachment): { detail?: ImageDetail } {\n return attachment.detail === undefined ? {} : { detail: attachment.detail };\n}\n\nfunction documentOptions(attachment: UIAttachment): { filename?: string } {\n return attachment.name === undefined ? {} : { filename: attachment.name };\n}\n\nfunction toolResultContentToJson(content: ToolResultContent[]): JsonValue {\n if (content.length === 1 && content[0]?.type === \"text\") {\n return content[0].text;\n }\n return content as JsonValue;\n}\n\nfunction isUserContent(value: unknown): value is UserContentType {\n if (!isRecord(value)) {\n return false;\n }\n\n if (value.type === \"text\") {\n return typeof value.text === \"string\";\n }\n\n if (value.type === \"image\") {\n return isImageContent(value);\n }\n\n if (value.type === \"document\") {\n return isDocumentContent(value);\n }\n\n return false;\n}\n\nfunction isImageContent(value: Record<string, unknown>): boolean {\n if (!isRecord(value.source)) {\n return false;\n }\n\n if (value.source.type === \"url\") {\n return typeof value.source.url === \"string\";\n }\n\n if (value.source.type === \"base64\") {\n return typeof value.source.data === \"string\" && typeof value.source.mediaType === \"string\";\n }\n\n return false;\n}\n\nfunction isDocumentContent(value: Record<string, unknown>): boolean {\n if (!isRecord(value.source)) {\n return false;\n }\n\n if (value.source.type === \"url\") {\n return typeof value.source.url === \"string\" && typeof value.source.mediaType === \"string\";\n }\n\n if (value.source.type === \"base64\") {\n return typeof value.source.data === \"string\" && typeof value.source.mediaType === \"string\";\n }\n\n if (value.source.type === \"text\") {\n return (\n typeof value.source.text === \"string\" &&\n (value.source.mediaType === undefined || typeof value.source.mediaType === \"string\")\n );\n }\n\n return false;\n}\n\nfunction toolPartId(toolCallId: string): string {\n return `tool_${toolCallId}`;\n}\n\nlet nextId = 0;\n\nfunction createId(prefix: string): string {\n const random = globalThis.crypto?.randomUUID?.();\n if (random !== undefined) {\n return `${prefix}_${random}`;\n }\n nextId += 1;\n return `${prefix}_${nextId.toString(36)}`;\n}\n\nfunction isUIMessageRole(value: unknown): value is UIMessage[\"role\"] {\n return value === \"system\" || value === \"user\" || value === \"assistant\" || value === \"tool\";\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null;\n}\n"],"mappings":";;;;;;;;;;AAmBO,SAAS,yBAAyB,UAAsC;AAC7E,QAAM,eAA8B,CAAC;AAErC,aAAW,WAAW,UAAU;AAC9B,UAAM,OAAO,gBAAgB,QAAQ,KAAK;AAE1C,QAAI,QAAQ,SAAS,UAAU;AAC7B,UAAI,KAAK,SAAS,GAAG;AACnB,qBAAa,KAAK,QAAQ,OAAO,IAAI,CAAC;AAAA,MACxC;AACA;AAAA,IACF;AAEA,QAAI,QAAQ,SAAS,QAAQ;AAC3B,YAAM,UAA6B,CAAC;AACpC,iBAAW,QAAQ,QAAQ,OAAO;AAChC,YAAI,KAAK,SAAS,QAAQ;AACxB,kBAAQ,KAAK,YAAY,KAAK,KAAK,IAAI,CAAC;AACxC;AAAA,QACF;AACA,YAAI,KAAK,SAAS,cAAc;AAC9B,kBAAQ,KAAK,wBAAwB,KAAK,UAAU,CAAC;AACrD;AAAA,QACF;AACA,YAAI,KAAK,SAAS,UAAU,cAAc,KAAK,IAAI,GAAG;AACpD,kBAAQ,KAAK,KAAK,IAAI;AACtB;AAAA,QACF;AACA,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AACA,UAAI,QAAQ,SAAS,GAAG;AACtB,qBAAa,KAAK,QAAQ,KAAK,OAAO,CAAC;AAAA,MACzC;AACA;AAAA,IACF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