repositories / pi-ext

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

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import {
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(
11 + "const x = 1;",
12 + );
13 + expect(renderPlainCodeBlock("ts", "const x = 1;")).toContain(
14 + 'data-language="ts"',
15 + );
16 + });
18 + it("keeps Shiki lazy and optional", () => {
19 + expect(renderCodeBlockComponentJs()).toContain("import('shiki')");
20 + expect(renderCodeBlockComponentJs()).toContain("textContent");
21 + });
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(
25 + {
26 + files: 1,
27 + additions: 1,
28 + deletions: 1,
29 + },
30 + );
31 + expect(renderDiffViewJs()).toContain("import('pierre')");
32 + });
33 +});

extensions/ui/web/app.css.ts

Modified
13 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*$'), ''));
96 + }
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

Added
new 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>`;
3 +}
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) {
18 + try {
19 + let highlighter = highlightCache.get('highlighter');
20 + if (!highlighter) {
21 + const shiki = await import('shiki');
22 + highlighter = await shiki.createHighlighter({ themes: ['github-dark'], langs: [language, 'text'] });
23 + highlightCache.set('highlighter', highlighter);
24 + }
25 + pre.innerHTML = highlighter.codeToHtml(code, { lang: language, theme: 'github-dark' });
26 + } catch (_) {
27 + /* plaintext stays visible */
28 + }
30 +`;
33 +function escapeAttr(value: string): string {
34 + return escapeHtml(value).replaceAll('"', "&quot;");
37 +function escapeHtml(value: string): string {
38 + return value
39 + .replaceAll("&", "&amp;")
40 + .replaceAll("<", "&lt;")
41 + .replaceAll(">", "&gt;");

extensions/ui/web/components/diff-view.js.ts

Added
new file mode 100644
1 +export type DiffSummary = {
2 + files: number;
3 + additions: number;
4 + deletions: number;
5 +};
7 +export function diffSummary(diff: string): DiffSummary {
8 + let files = 0;
9 + let additions = 0;
10 + let deletions = 0;
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;
15 + }
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 */ }
33 + });
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;
41 + }
42 + return { files, additions, deletions };
44 +`;