repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui slash autocomplete

c01d037e19417bc966b0fdefe4ff0a0c8794d605

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

unsigned

Browse filesCompare with HEAD

Changed files (7)

extensions/ui/__tests__/composer.test.ts

Modified
33 33 it("extracts at-references from prompts", () => {
34 - expect(parseComposerInput("read @README.md and @docs/spec.md please")).toEqual({
34 + expect(
35 + parseComposerInput("read @README.md and @docs/spec.md please"),
36 + ).toEqual({
35 37 kind: "prompt",
36 38 text: "read @README.md and @docs/spec.md please",
37 39 refs: ["README.md", "docs/spec.md"],

extensions/ui/__tests__/web-composer.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import {
3 + filterCommandItems,
4 + moveMenuIndex,
5 +} from "../web/components/composer.js";
7 +describe("ui web composer autocomplete", () => {
8 + const commands = [
9 + { name: "mode", description: "Switch mode" },
10 + { name: "skill:brutal", description: "Brutal style" },
11 + { name: "skill:wiki", description: "Wiki research" },
12 + { name: "ui", description: "Browser UI" },
13 + ];
15 + it("filters slash command items", () => {
16 + expect(filterCommandItems(commands, "/skill:b")).toEqual([
17 + { name: "skill:brutal", description: "Brutal style" },
18 + ]);
19 + });
21 + it("returns extension, skill, and prompt command items for empty slash prefix", () => {
22 + expect(filterCommandItems(commands, "/").map((item) => item.name)).toEqual([
23 + "mode",
24 + "skill:brutal",
25 + "skill:wiki",
26 + "ui",
27 + ]);
28 + });
30 + it("wraps keyboard menu selection", () => {
31 + expect(moveMenuIndex(0, 1, 3)).toBe(1);
32 + expect(moveMenuIndex(2, 1, 3)).toBe(0);
33 + expect(moveMenuIndex(0, -1, 3)).toBe(2);
34 + expect(moveMenuIndex(0, 1, 0)).toBe(0);
35 + });
36 +});

extensions/ui/server.ts

Modified
8 8 import { type WebSocket, WebSocketServer } from "ws";
9 9 import type { EventStore } from "./events";
10 10 import { listenHostFor, uiUrl } from "./network";
11 -import type { UiConnectionStatus, UiEvent } from "./protocol";
11 +import type { UiCommandInfo, UiConnectionStatus, UiEvent } from "./protocol";
12 12 import { renderAppCss } from "./web/app.css";
13 13 import { renderAppHtml } from "./web/app.html";
14 14 import { renderAppJs } from "./web/app.js";
34 34 events?: EventStore;
35 35 snapshotStatus?: UiConnectionStatus;
36 36 onBrowserMessage?: (message: unknown) => void | Promise<void>;
37 + commands?: UiCommandInfo[];
39 40 const COOKIE = "pi_ui_token";
54 55 token,
55 56 events: options.events,
56 57 snapshotStatus: options.snapshotStatus,
58 + commands: options.commands,
57 59 state: () => currentState,
58 60 clients: () => clientCount,
59 61 setState(next) {
136 138 token: string;
137 139 events?: EventStore;
138 140 snapshotStatus?: UiConnectionStatus;
141 + commands?: UiCommandInfo[];
139 142 state(): UiServerState;
140 143 clients(): number;
141 144 setState(state: UiServerState): void;
173 176 ctx.events?.snapshot({
174 177 status: ctx.snapshotStatus ?? ctx.state(),
175 - commands: [],
178 + commands: ctx.commands,
176 179 }) ?? {
177 180 version: 1,
178 181 lastEventId: 0,
179 182 status: ctx.snapshotStatus ?? ctx.state(),
180 - commands: [],
183 + commands: ctx.commands ?? [],
181 184 events: [],

extensions/ui/web/app.css.ts

Modified
13 13 .ui-event { border: 2px solid color-mix(in srgb, CanvasText 50%, transparent); padding: 8px; margin: 0 0 8px; white-space: pre-wrap; }
14 14 .ui-event-user { border-width: 4px; }
15 15 #ui-composer { display: grid; grid-template-columns: 1fr auto; gap: 8px; border-top: 3px solid CanvasText; padding: 8px; }
16 -#ui-input { min-height: 42px; resize: vertical; font: inherit; background: Canvas; color: CanvasText; border: 2px solid CanvasText; padding: 8px; }
16 +#ui-input-wrap { position: relative; min-width: 0; }
17 +#ui-input { width: 100%; min-height: 42px; resize: vertical; font: inherit; background: Canvas; color: CanvasText; border: 2px solid CanvasText; padding: 8px; }
18 +#ui-command-menu { position: absolute; left: 0; right: 0; bottom: 100%; border: 2px solid CanvasText; background: Canvas; max-height: 40vh; overflow: auto; }
19 +.ui-command-item { display: block; width: 100%; padding: 6px 8px; border: 0; border-bottom: 1px solid color-mix(in srgb, CanvasText 35%, transparent); background: Canvas; color: CanvasText; font: inherit; text-align: left; }
20 +.ui-command-item.is-selected { background: CanvasText; color: Canvas; }
17 21 #ui-send { font: inherit; background: CanvasText; color: Canvas; border: 0; padding: 0 16px; }
18 22 #ui-send:disabled { opacity: .5; }

extensions/ui/web/app.html.ts

Modified
13 13 <header id="ui-status">starting</header>
14 14 <main id="ui-stream" aria-live="polite"><h1>Pi UI shell</h1></main>
15 15 <form id="ui-composer">
16 +<div id="ui-input-wrap">
16 17 <textarea id="ui-input" name="prompt" placeholder="Message Pi"></textarea>
18 +<div id="ui-command-menu" hidden></div>
19 +</div>
17 20 <button id="ui-send" type="submit">SEND</button>
18 21 </form>
19 22 </div>

extensions/ui/web/app.js.ts

Modified
1 +import { renderComposerComponentJs } from "./components/composer.js";
1 3 export function renderAppJs(): string {
2 4 return `const status = document.querySelector('#ui-status');
3 5 const stream = document.querySelector('#ui-stream');
4 6 const form = document.querySelector('#ui-composer');
5 7 const input = document.querySelector('#ui-input');
6 8 const send = document.querySelector('#ui-send');
9 +const menu = document.querySelector('#ui-command-menu');
10 +let commands = [];
11 +let visibleCommands = [];
12 +let selectedCommandIndex = 0;
7 13 let lastEventId = 0;
8 14 let socket;
16 +${renderComposerComponentJs()}
10 18 function authHeaders() {
11 19 const token = new URL(location.href).searchParams.get('token');
12 20 return token ? { authorization: 'Bearer ' + token } : {};
17 25 if (!response.ok) throw new Error('snapshot failed: ' + response.status);
18 26 const snapshot = await response.json();
19 27 status.textContent = snapshot.status;
28 + commands = snapshot.commands || [];
20 29 renderEvents(snapshot.events || []);
21 30 lastEventId = snapshot.lastEventId || 0;
49 58 stream.scrollTop = stream.scrollHeight;
61 +input.addEventListener('input', renderCommandMenu);
62 +input.addEventListener('keydown', event => {
63 + if (menu.hidden) return;
64 + if (event.key === 'ArrowDown') {
65 + event.preventDefault();
66 + selectedCommandIndex = moveMenuIndex(selectedCommandIndex, 1, visibleCommands.length);
67 + renderCommandMenu();
68 + }
69 + if (event.key === 'ArrowUp') {
70 + event.preventDefault();
71 + selectedCommandIndex = moveMenuIndex(selectedCommandIndex, -1, visibleCommands.length);
72 + renderCommandMenu();
73 + }
74 + if (event.key === 'Tab') {
75 + event.preventDefault();
76 + selectCommand(selectedCommandIndex);
77 + }
78 +});
52 80 form.addEventListener('submit', event => {
53 81 event.preventDefault();
54 82 const text = input.value.trim();

extensions/ui/web/components/composer.js.ts

Added
new file mode 100644
1 +import type { UiCommandInfo } from "../../protocol";
3 +export function filterCommandItems(
4 + commands: UiCommandInfo[],
5 + input: string,
6 +): UiCommandInfo[] {
7 + const prefix = input.startsWith("/") ? input.slice(1).toLowerCase() : "";
8 + return commands.filter((item) => item.name.toLowerCase().startsWith(prefix));
9 +}
11 +export function moveMenuIndex(
12 + current: number,
13 + delta: number,
14 + count: number,
15 +): number {
16 + if (count <= 0) return 0;
17 + return (current + delta + count) % count;
20 +export function renderComposerComponentJs(): string {
21 + return `function filterCommandItems(commands, input) {
22 + const prefix = input.startsWith('/') ? input.slice(1).toLowerCase() : '';
23 + return commands.filter(item => item.name.toLowerCase().startsWith(prefix));
25 +function moveMenuIndex(current, delta, count) {
26 + if (count <= 0) return 0;
27 + return (current + delta + count) % count;
29 +function renderCommandMenu() {
30 + menu.innerHTML = '';
31 + if (!input.value.startsWith('/')) {
32 + menu.hidden = true;
33 + selectedCommandIndex = 0;
34 + return;
35 + }
36 + visibleCommands = filterCommandItems(commands, input.value);
37 + menu.hidden = visibleCommands.length === 0;
38 + for (const [index, command] of visibleCommands.entries()) {
39 + const item = document.createElement('button');
40 + item.type = 'button';
41 + item.className = 'ui-command-item' + (index === selectedCommandIndex ? ' is-selected' : '');
42 + item.textContent = '/' + command.name + (command.description ? ' — ' + command.description : '');
43 + item.addEventListener('click', () => selectCommand(index));
44 + menu.append(item);
45 + }
47 +function selectCommand(index) {
48 + const command = visibleCommands[index];
49 + if (!command) return;
50 + input.value = '/' + command.name + ' ';
51 + input.focus();
52 + menu.hidden = true;
54 +`;