0 Votes

Wiki source code of LLMChatRenderer

Version 12.1 by Ryan C on 2025/08/04 03:30

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