| 1 | import type { ChatAttachment, EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | import { imageMimeType } from "@t3tools/shared/image"; |
| 3 | import type { FilePreviewItem } from "../../components/FilePreviewModal.types"; |
| 4 | import { markdownImageSources } from "../../lib/markdownImageSources"; |
| 5 | import { resolveMarkdownMediaPreview } from "../../lib/markdownMedia"; |
| 6 | |
| 7 | export function messageImageGallery( |
| 8 | message: { readonly text: string; readonly attachments?: readonly ChatAttachment[] }, |
| 9 | context: { |
| 10 | environmentId: EnvironmentId; |
| 11 | threadId: ThreadId; |
| 12 | workspaceRoot: string | null | undefined; |
| 13 | }, |
| 14 | ): FilePreviewItem[] { |
| 15 | const images: FilePreviewItem[] = []; |
| 16 | for (const attachment of message.attachments ?? []) { |
| 17 | if (attachment.type !== "image" && imageMimeType(attachment) === null) continue; |
| 18 | images.push({ |
| 19 | kind: "image", |
| 20 | name: attachment.name, |
| 21 | environmentId: context.environmentId, |
| 22 | resource: { |
| 23 | _tag: "attachment", |
| 24 | attachmentId: attachment.id, |
| 25 | fileName: attachment.name, |
| 26 | mimeType: attachment.mimeType, |
| 27 | }, |
| 28 | }); |
| 29 | } |
| 30 | for (const href of markdownImageSources(message.text)) { |
| 31 | const preview = resolveMarkdownMediaPreview(href, { ...context, imageEmbed: true }); |
| 32 | if (preview?.kind === "image") images.push(preview.source); |
| 33 | } |
| 34 | return images; |
| 35 | } |