Wiki source code of LLMChatRenderer
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{velocity}} | ||
2 | #if ($xcontext.action != 'jsx') | ||
3 | $xwiki.parseContent(" | ||
4 | {{html}} | ||
5 | <script> | ||
6 | console.log('✅ Renderer script is running'); | ||
7 | |||
8 | document.addEventListener('DOMContentLoaded', function () { | ||
9 | function processAssistantMessages() { | ||
10 | const messages = document.querySelectorAll('.assistantMessage'); | ||
11 | |||
12 | messages.forEach(msg => { | ||
13 | const html = msg.innerHTML; | ||
14 | |||
15 | const chunkMap = {}; | ||
16 | const chunkRegex = /\\[(\\d+)\\]\\s*\\((https:\\/\\/[^)]+)\\)/g; | ||
17 | let match; | ||
18 | while ((match = chunkRegex.exec(html)) !== null) { | ||
19 | const index = match[1]; | ||
20 | const url = match[2]; | ||
21 | const path = extractDownloadPath(url); | ||
22 | if (path) chunkMap[index] = path; | ||
23 | } | ||
24 | |||
25 | let currentIndex = null; | ||
26 | const processed = html.replace(/(\\[\\d+\\])\\s*\\((https:\\/\\/[^)]+)\\)/g, (full, idx) => { | ||
27 | currentIndex = idx.replace(/\\D/g, ''); | ||
28 | return full; | ||
29 | }).replace(/\\[\\[(image|doc_)?([^\\]]+?)\\]\\]/g, function (_, type, filename) { | ||
30 | if (!currentIndex || !chunkMap[currentIndex]) return ''; | ||
31 | const path = chunkMap[currentIndex]; | ||
32 | const downloadURL = `/bin/download/${path}/${filename}`; | ||
33 | |||
34 | if (type === 'image') { | ||
35 | return `<img src='\${downloadURL}' style='max-width:300px; display:block; margin:1em 0;' />`; | ||
36 | } else { | ||
37 | return `<a href='\${downloadURL}' target='_blank'>📄 Download \${filename}</a>`; | ||
38 | } | ||
39 | }); | ||
40 | |||
41 | msg.innerHTML = processed; | ||
42 | }); | ||
43 | } | ||
44 | |||
45 | function extractDownloadPath(viewURL) { | ||
46 | const match = viewURL.match(/\\/bin\\/view\\/([^?#]+)/); | ||
47 | if (!match) return null; | ||
48 | return decodeURIComponent(match[1]); | ||
49 | } | ||
50 | |||
51 | const observer = new MutationObserver(processAssistantMessages); | ||
52 | observer.observe(document.body, { childList: true, subtree: true }); | ||
53 | }); | ||
54 | </script> | ||
55 | {{/html}} | ||
56 | ") | ||
57 | #end | ||
58 | {{/velocity}} |