repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui stream performance guards

bdc364704ca833d5b214dd1018e288f82dfbf258

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

extensions/ui/__tests__/performance.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import {
3 + longOutputModel,
4 + toggleLongOutput,
5 +} from "../web/components/long-output.js";
6 +import { virtualWindow } from "../web/components/virtual-stream.js";
8 +describe("ui stream performance guards", () => {
9 + it("truncates long output and preserves expansion state", () => {
10 + const text = "x".repeat(2_000);
11 + expect(longOutputModel("e1", text, new Set(), 120)).toMatchObject({
12 + id: "e1",
13 + truncated: true,
14 + text: "x".repeat(120),
15 + });
16 + const expanded = toggleLongOutput("e1", new Set());
17 + expect(longOutputModel("e1", text, expanded, 120)).toMatchObject({
18 + truncated: false,
19 + text,
20 + });
21 + });
23 + it("returns ordered virtual windows around selected item", () => {
24 + const events = Array.from({ length: 100 }, (_, index) => ({
25 + id: index + 1,
26 + }));
27 + expect(
28 + virtualWindow(events, { selectedId: 50, threshold: 20, radius: 3 }).map(
29 + (e) => e.id,
30 + ),
31 + ).toEqual([47, 48, 49, 50, 51, 52, 53]);
32 + expect(
33 + virtualWindow(events, { selectedId: 2, threshold: 200, radius: 3 }),
34 + ).toEqual(events);
35 + });
36 +});

extensions/ui/web/app.css.ts

Modified
15 15 .ui-visual-error-hint { margin-top: 8px; font-weight: 800; }
16 16 .ui-code, .ui-diff pre { overflow: auto; max-height: 60vh; padding: 8px; border: 2px solid var(--nugu-line); background: var(--nugu-bg); }
17 17 .ui-diff summary { cursor: pointer; font-weight: 800; }
18 +.ui-expand-output { display: block; margin-top: 8px; font: inherit; border: 2px solid var(--nugu-line); background: var(--nugu-bg); color: var(--nugu-fg); }
18 19 #ui-digest { border-top: 3px solid var(--nugu-line); padding: 6px 8px; font-size: 12px; }
19 20 #ui-composer { display: grid; grid-template-columns: 1fr auto; gap: 8px; border-top: 3px solid var(--nugu-line); padding: 8px; }
20 21 #ui-input-wrap { position: relative; min-width: 0; }

extensions/ui/web/app.js.ts

Modified
3 3 import { renderCodeBlockComponentJs } from "./components/code-block.js";
4 4 import { renderComposerComponentJs } from "./components/composer.js";
5 5 import { renderDialogsComponentJs } from "./components/dialogs.js";
6 6 import { renderDiffViewJs } from "./components/diff-view.js";
7 7 import { renderDigestIndicatorJs } from "./components/digest-indicator.js";
8 +import { renderLongOutputJs } from "./components/long-output.js";
9 +import { renderVirtualStreamJs } from "./components/virtual-stream.js";
8 10 import { renderVisualBlockComponentJs } from "./components/visual-block.js";
9 11 import { renderTelemetryWorkerJs } from "./worker.js";
26 28 ${renderDigestIndicatorJs()}
27 29 ${renderCodeBlockComponentJs()}
28 30 ${renderDiffViewJs()}
31 +${renderLongOutputJs()}
32 +${renderVirtualStreamJs()}
29 33 const telemetryWorkerSource = ${JSON.stringify(renderTelemetryWorkerJs())};
30 34 function startTelemetryWorker() {
31 35 if (!('Worker' in window)) return;
81 85 function renderEvents(events) {
86 + const fragment = document.createDocumentFragment();
82 87 for (const event of events) {
83 88 if (event.id <= lastEventId) continue;
84 89 lastEventId = event.id;
86 91 row.className = 'ui-event ui-event-' + (event.role || event.type);
87 92 row.dataset.eventId = String(event.id);
88 93 const eventText = event.text || event.message || event.state || JSON.stringify(event);
89 - row.textContent = eventText;
94 + applyLongOutput(row, String(event.id), String(eventText));
90 95 const fence = String.fromCharCode(96).repeat(3);
91 96 if (typeof eventText === 'string' && eventText.startsWith(fence)) {
92 97 row.textContent = '';
101 106 if (event.type === 'ui_request') renderUiRequest(row, event);
102 107 if (event.type && event.type.startsWith('widget_')) renderVisualBlock(row, event);
103 - stream.append(row);
108 + fragment.append(row);
110 + appendStreamRows(fragment);
111 + streamBatchChannel.port2.postMessage('flush');
105 112 stream.scrollTop = stream.scrollHeight;

extensions/ui/web/components/long-output.js.ts

Added
new file mode 100644
1 +export type LongOutputModel = {
2 + id: string;
3 + text: string;
4 + truncated: boolean;
5 +};
7 +export function longOutputModel(
8 + id: string,
9 + text: string,
10 + expanded: Set<string>,
11 + limit = 4_000,
12 +): LongOutputModel {
13 + if (expanded.has(id) || text.length <= limit)
14 + return { id, text, truncated: false };
15 + return { id, text: text.slice(0, limit), truncated: true };
18 +export function toggleLongOutput(
19 + id: string,
20 + expanded: Set<string>,
21 +): Set<string> {
22 + const next = new Set(expanded);
23 + if (next.has(id)) next.delete(id);
24 + else next.add(id);
25 + return next;
28 +export function renderLongOutputJs(): string {
29 + return `const expandedOutputs = new Set();
30 +function longOutputText(id, text, limit = 4000) {
31 + if (expandedOutputs.has(id) || text.length <= limit) return { text, truncated: false };
32 + return { text: text.slice(0, limit), truncated: true };
34 +function applyLongOutput(row, id, text) {
35 + const model = longOutputText(id, text);
36 + row.textContent = model.text;
37 + if (!model.truncated) return;
38 + const button = document.createElement('button');
39 + button.type = 'button';
40 + button.className = 'ui-expand-output';
41 + button.textContent = 'expand output';
42 + button.addEventListener('click', () => { expandedOutputs.add(id); row.textContent = text; });
43 + row.append(button);
45 +`;

extensions/ui/web/components/virtual-stream.js.ts

Added
new file mode 100644
1 +export type VirtualWindowOptions = {
2 + selectedId: number;
3 + threshold?: number;
4 + radius?: number;
5 +};
7 +export function virtualWindow<T extends { id: number }>(
8 + events: T[],
9 + options: VirtualWindowOptions,
10 +): T[] {
11 + const threshold = options.threshold ?? 200;
12 + if (events.length <= threshold) return events;
13 + const radius = options.radius ?? 30;
14 + const index = events.findIndex((event) => event.id === options.selectedId);
15 + if (index === -1) return events.slice(-threshold);
16 + return events.slice(Math.max(0, index - radius), index + radius + 1);
19 +export function renderVirtualStreamJs(): string {
20 + return `function appendStreamRows(fragment) {
21 + requestAnimationFrame(() => stream.append(fragment));
23 +function maybeVirtualizeStream() {
24 + const rows = [...stream.children];
25 + if (rows.length <= 500) return;
26 + for (const row of rows.slice(0, rows.length - 500)) row.remove();
28 +const streamBatchChannel = new MessageChannel();
29 +streamBatchChannel.port1.onmessage = maybeVirtualizeStream;
30 +`;