]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
36e7a3192bc1ac6ffed6ad81a96a14d80b26bf47
[pkg/ggml/sources/llama.cpp] /
1 import type { Root as HastRoot } from 'hast';
2 import { visit } from 'unist-util-visit';
3 import type { DatabaseMessageExtra, DatabaseMessageExtraImageFile } from '$lib/types/database';
4 import { AttachmentType, UrlProtocol } from '$lib/enums';
5
6 /**
7 * Rehype plugin to resolve attachment image sources.
8 * Converts attachment names (e.g., "mcp-attachment-xxx.png") to base64 data URLs.
9 */
10 export function rehypeResolveAttachmentImages(options: { attachments?: DatabaseMessageExtra[] }) {
11 return (tree: HastRoot) => {
12 visit(tree, 'element', (node) => {
13 if (node.tagName === 'img' && node.properties?.src) {
14 const src = String(node.properties.src);
15
16 // Skip data URLs and external URLs
17 if (src.startsWith(UrlProtocol.DATA) || src.startsWith(UrlProtocol.HTTP)) {
18 return;
19 }
20
21 // Find matching attachment
22 const attachment = options.attachments?.find(
23 (a): a is DatabaseMessageExtraImageFile =>
24 a.type === AttachmentType.IMAGE && a.name === src
25 );
26
27 // Replace with base64 URL if found
28 if (attachment?.base64Url) {
29 node.properties.src = attachment.base64Url;
30 }
31 }
32 });
33 };
34 }