pi-ext
bugabingas pi extensions
owned by bugabinga
Add ui code and diff rendering
2eba667ee068d8075c3a7b8201c822b201997d9a
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
Browse filesCompare with HEAD
Changed files (5)
extensions/ui/__tests__/rendering.test.ts
Addednew file mode 100644 1 +import { describe, expect, it }
from "vitest";
3 + renderCodeBlockComponentJs,
4 + renderPlainCodeBlock,
5 +}
from "../web/components/code-block.js";
6 +import { diffSummary, renderDiffViewJs }
from "../web/components/diff-view.js";
8 +describe(
"ui code and diff rendering", ()
=> {
9 + it(
"renders plaintext code immediately", ()
=> {
10 + expect(
renderPlainCodeBlock(
"ts",
"const x = 1;")).
toContain(
13 + expect(
renderPlainCodeBlock(
"ts",
"const x = 1;")).
toContain(
14 + 'data-language="ts"',
18 + it(
"keeps Shiki lazy and optional", ()
=> {
19 + expect(
renderCodeBlockComponentJs()).
toContain(
"import('shiki')");
20 + expect(
renderCodeBlockComponentJs()).
toContain(
"textContent");
23 + it(
"summarizes diffs and emits Pierre lazy loader", ()
=> {
24 + expect(
diffSummary(
"--- a/a.ts\n+++ b/a.ts\n+add\n-del\n context")).
toEqual(
31 + expect(
renderDiffViewJs()).
toContain(
"import('pierre')");
extensions/ui/web/app.css.ts
Modified13 13 .ui
-visual
-fallback {
color:
var(--nugu-muted); margin
-bottom: 8px; }
14 14 .ui
-visual
-block
-row iframe {
display: block;
width:
100%;
height:
min(60vh, 600px); max
-height: 600px;
border: 2px solid
var(--nugu-line);
background:
var(--nugu-bg); }
15 15 .ui
-visual
-error
-hint { margin
-top: 8px; font
-weight:
800; }
16 +.ui
-code, .ui
-diff pre {
overflow: auto; max
-height: 60vh;
padding: 8px;
border: 2px solid
var(--nugu-line);
background:
var(--nugu-bg); }
17 +.ui
-diff summary {
cursor: pointer; font
-weight:
800; }
16 18 #ui
-digest { border
-top: 3px solid
var(--nugu-line);
padding: 6px 8px; font
-size: 12px; }
17 19 #ui
-composer {
display: grid; grid
-template
-columns: 1fr auto;
gap: 8px; border
-top: 3px solid
var(--nugu-line);
padding: 8px; }
18 20 #ui
-input
-wrap {
position: relative; min
-width:
0; }
extensions/ui/web/app.js.ts
Modified 1 +import { renderCodeBlockComponentJs }
from "./components/code-block.js";
1 2 import { renderComposerComponentJs }
from "./components/composer.js";
2 3 import { renderDialogsComponentJs }
from "./components/dialogs.js";
4 +import { renderDiffViewJs }
from "./components/diff-view.js";
3 5 import { renderDigestIndicatorJs }
from "./components/digest-indicator.js";
4 6 import { renderVisualBlockComponentJs }
from "./components/visual-block.js";
5 7 import { renderTelemetryWorkerJs }
from "./worker.js";
22 24 ${
renderDialogsComponentJs()}
23 25 ${
renderVisualBlockComponentJs()}
24 26 ${
renderDigestIndicatorJs()}
27 +${
renderCodeBlockComponentJs()}
28 +${
renderDiffViewJs()}
25 29 const telemetryWorkerSource
= ${JSON.stringify(
renderTelemetryWorkerJs())};
26 30 function startTelemetryWorker() {
27 31 if (
!(
'Worker' in window))
return;
81 85 const row
= document.
createElement(
'article');
82 86 row.
className = 'ui-event ui-event-' + (
event.role
|| event.
type);
83 87 row.dataset.eventId
= String(
event.
id);
84 - row.textContent
= event.
text || event.message
|| event.state
|| JSON.
stringify(
event);
88 + const eventText
= event.
text || event.message
|| event.state
|| JSON.
stringify(
event);
89 + row.textContent
= eventText;
90 + const fence
= String.
fromCharCode(
96).
repeat(
3);
91 + if (
typeof eventText
=== 'string' && eventText.
startsWith(fence)) {
92 + row.textContent
= '';
93 + const firstNewline
= eventText.
indexOf(
'\n');
94 + const language
= eventText.
slice(
3, firstNewline).
trim()
|| 'text';
95 + renderCodeBlock(row, language, eventText.
slice(firstNewline
+ 1).
replace(
new RegExp(fence
+ '\\s*$'),
''));
97 + if (
typeof eventText
=== 'string' && eventText.
includes(
'+++ ')
&& eventText.
includes(
'--- ')) {
98 + row.textContent
= '';
99 + renderDiffView(row, eventText);
85 101 if (
event.
type === 'ui_request')
renderUiRequest(row,
event);
86 102 if (
event.
type && event.
type.
startsWith(
'widget_'))
renderVisualBlock(row,
event);
87 103 stream.
append(row);
extensions/ui/web/components/code-block.js.ts
Addednew file mode 100644 1 +export function renderPlainCodeBlock(language
: string, code
: string)
: string {
2 + return `<pre class="ui-code" data-language="${escapeAttr(language)}"><code>${escapeHtml(code)}</code></pre>`;
5 +export function renderCodeBlockComponentJs()
: string {
6 + return `const highlightCache = new Map(); 7 +function renderCodeBlock(container, language, code) { 8 + const pre = document.createElement('pre'); 9 + pre.className = 'ui-code'; 10 + pre.dataset.language = language || 'text'; 11 + const codeNode = document.createElement('code'); 12 + codeNode.textContent = code; 13 + pre.append(codeNode); 14 + container.append(pre); 15 + queueMicrotask(() => upgradeHighlight(pre, language || 'text', code)); 17 +async function upgradeHighlight(pre, language, code) { 19 + let highlighter = highlightCache.get('highlighter'); 21 + const shiki = await import('shiki'); 22 + highlighter = await shiki.createHighlighter({ themes: ['github-dark'], langs: [language, 'text'] }); 23 + highlightCache.set('highlighter', highlighter); 25 + pre.innerHTML = highlighter.codeToHtml(code, { lang: language, theme: 'github-dark' }); 27 + /* plaintext stays visible */ 33 +function escapeAttr(value
: string)
: string {
34 + return escapeHtml(value).
replaceAll(
'"',
""");
37 +function escapeHtml(value
: string)
: string {
39 + .
replaceAll(
"&",
"&")
40 + .
replaceAll(
"<",
"<")
41 + .
replaceAll(
">",
">");
extensions/ui/web/components/diff-view.js.ts
Addednew file mode 100644 1 +export type DiffSummary = {
7 +export function diffSummary(diff
: string)
: DiffSummary {
11 + for (
const line
of diff.
split(
"\n")) {
12 + if (line.
startsWith(
"+++ ")) files
+= 1;
13 + else if (line.
startsWith(
"+")
&& !line.
startsWith(
"+++")) additions
+= 1;
14 + else if (line.
startsWith(
"-")
&& !line.
startsWith(
"---")) deletions
+= 1;
16 + return { files, additions, deletions };
19 +export function renderDiffViewJs()
: string {
20 + return `function renderDiffView(container, diff) { 21 + const summary = diffSummary(diff); 22 + const details = document.createElement('details'); 23 + details.className = 'ui-diff'; 24 + const summaryNode = document.createElement('summary'); 25 + summaryNode.textContent = summary.files + ' files +' + summary.additions + ' -' + summary.deletions; 26 + details.append(summaryNode); 27 + const pre = document.createElement('pre'); 28 + pre.textContent = diff; 29 + details.append(pre); 30 + container.append(details); 31 + queueMicrotask(async () => { 32 + try { await import('pierre'); } catch (_) { /* plaintext diff stays visible */ } 35 +function diffSummary(diff) { 36 + let files = 0, additions = 0, deletions = 0; 37 + for (const line of diff.split('\\n')) { 38 + if (line.startsWith('+++ ')) files += 1; 39 + else if (line.startsWith('+') && !line.startsWith('+++')) additions += 1; 40 + else if (line.startsWith('-') && !line.startsWith('---')) deletions += 1; 42 + return { files, additions, deletions };