repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui extension request protocol

fb50ca39966a0945bac2b543a26b9f9f5c64a6ce

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

unsigned

Browse filesCompare with HEAD

Changed files (9)

extensions/ui/__tests__/extension-ui.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import {
3 + extensionUiEventFromRequest,
4 + extensionUiResponseCommand,
5 + isExtensionUiRequest,
6 +} from "../extension-ui";
8 +describe("ui extension UI protocol", () => {
9 + it("recognizes dialog requests", () => {
10 + expect(
11 + isExtensionUiRequest({
12 + type: "extension_ui_request",
13 + id: "u1",
14 + method: "select",
15 + title: "Pick",
16 + options: ["a", "b"],
17 + }),
18 + ).toBe(true);
19 + });
21 + it("normalizes select requests into stream events", () => {
22 + expect(
23 + extensionUiEventFromRequest({
24 + type: "extension_ui_request",
25 + id: "u1",
26 + method: "select",
27 + title: "Pick one",
28 + options: ["a", "b"],
29 + }),
30 + ).toEqual({
31 + type: "ui_request",
32 + requestId: "u1",
33 + method: "select",
34 + title: "Pick one",
35 + message: "select: Pick one",
36 + options: ["a", "b"],
37 + expectsResponse: true,
38 + });
39 + });
41 + it("normalizes fire-and-forget notify requests", () => {
42 + expect(
43 + extensionUiEventFromRequest({
44 + type: "extension_ui_request",
45 + id: "u2",
46 + method: "notify",
47 + message: "Blocked",
48 + notifyType: "warning",
49 + }),
50 + ).toMatchObject({
51 + type: "ui_notify",
52 + requestId: "u2",
53 + message: "Blocked",
54 + notifyType: "warning",
55 + });
56 + });
58 + it("creates response commands for values, confirmations, and cancellations", () => {
59 + expect(extensionUiResponseCommand("u1", { value: "a" })).toEqual({
60 + type: "extension_ui_response",
61 + id: "u1",
62 + value: "a",
63 + });
64 + expect(extensionUiResponseCommand("u2", { confirmed: true })).toEqual({
65 + type: "extension_ui_response",
66 + id: "u2",
67 + confirmed: true,
68 + });
69 + expect(extensionUiResponseCommand("u3", { cancelled: true })).toEqual({
70 + type: "extension_ui_response",
71 + id: "u3",
72 + cancelled: true,
73 + });
74 + });
75 +});

extensions/ui/__tests__/runtime.test.ts

Modified
165 165 expect(sent).toContainEqual({ type: "cycle_model" });
166 166 expect(sent).toContainEqual({ type: "abort" });
169 + it("routes extension UI responses to rpc", async () => {
170 + const sent: unknown[] = [];
171 + const runtime = await createUiRuntime({
172 + sessionLabel: "ui: test",
173 + address: "127.0.0.1",
174 + cwd: "C:/repo",
175 + openBrowser: false,
176 + sessionManager: fakeSessionManager(),
177 + startRpcBridge: ({ sessionPath }) => fakeRpc(sent, sessionPath),
178 + });
179 + runtimes.push(runtime);
180 + const url = new URL(runtime.status().url ?? "");
181 + const socketUrl = new URL("/ws", url);
182 + socketUrl.protocol = "ws:";
183 + socketUrl.searchParams.set("token", url.searchParams.get("token") ?? "");
184 + const socket = new WebSocket(socketUrl);
185 + await new Promise((resolve) =>
186 + socket.addEventListener("open", resolve, { once: true }),
187 + );
189 + socket.send(
190 + JSON.stringify({ type: "ui_response", id: "u1", value: "Allow" }),
191 + );
192 + await new Promise((resolve) => setTimeout(resolve, 20));
193 + socket.close();
195 + expect(sent).toContainEqual({
196 + type: "extension_ui_response",
197 + id: "u1",
198 + value: "Allow",
199 + });
200 + });
170 203 function fakeSessionManager() {

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

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import { dialogResponse } from "../web/components/dialogs.js";
4 +describe("ui web dialogs", () => {
5 + it("builds extension UI responses", () => {
6 + expect(dialogResponse("u1", "select", "Allow")).toEqual({
7 + type: "ui_response",
8 + id: "u1",
9 + value: "Allow",
10 + });
11 + expect(dialogResponse("u2", "confirm", true)).toEqual({
12 + type: "ui_response",
13 + id: "u2",
14 + confirmed: true,
15 + });
16 + expect(dialogResponse("u3", "input", undefined)).toEqual({
17 + type: "ui_response",
18 + id: "u3",
19 + cancelled: true,
20 + });
21 + });
22 +});

extensions/ui/extension-ui.ts

Added
new file mode 100644
1 +import type { UiEventInput } from "./protocol";
3 +export type ExtensionUiRequest = {
4 + type: "extension_ui_request";
5 + id: string;
6 + method: string;
7 + [key: string]: unknown;
8 +};
10 +export type ExtensionUiResponseInput =
11 + | { value: string }
12 + | { confirmed: boolean }
13 + | { cancelled: true };
15 +export function isExtensionUiRequest(
16 + value: unknown,
17 +): value is ExtensionUiRequest {
18 + return (
19 + !!value &&
20 + typeof value === "object" &&
21 + (value as { type?: unknown }).type === "extension_ui_request" &&
22 + typeof (value as { id?: unknown }).id === "string" &&
23 + typeof (value as { method?: unknown }).method === "string"
24 + );
27 +export function extensionUiEventFromRequest(
28 + request: ExtensionUiRequest,
29 +): UiEventInput {
30 + if (request.method === "select") {
31 + return {
32 + type: "ui_request",
33 + requestId: request.id,
34 + method: "select",
35 + title: stringField(request.title),
36 + message: `select: ${stringField(request.title)}`,
37 + options: arrayField(request.options),
38 + expectsResponse: true,
39 + };
40 + }
41 + if (request.method === "confirm") {
42 + return {
43 + type: "ui_request",
44 + requestId: request.id,
45 + method: "confirm",
46 + title: stringField(request.title),
47 + message:
48 + stringField(request.message) ||
49 + `confirm: ${stringField(request.title)}`,
50 + expectsResponse: true,
51 + };
52 + }
53 + if (request.method === "input") {
54 + return {
55 + type: "ui_request",
56 + requestId: request.id,
57 + method: "input",
58 + title: stringField(request.title),
59 + message: `input: ${stringField(request.title)}`,
60 + placeholder: stringField(request.placeholder),
61 + expectsResponse: true,
62 + };
63 + }
64 + if (request.method === "editor") {
65 + return {
66 + type: "ui_request",
67 + requestId: request.id,
68 + method: "editor",
69 + title: stringField(request.title),
70 + message: `editor: ${stringField(request.title)}`,
71 + prefill: stringField(request.prefill),
72 + expectsResponse: true,
73 + };
74 + }
75 + if (request.method === "notify") {
76 + return {
77 + type: "ui_notify",
78 + requestId: request.id,
79 + message: stringField(request.message),
80 + notifyType: notifyType(request.notifyType),
81 + };
82 + }
83 + return {
84 + type: extensionUiEventType(request.method),
85 + requestId: request.id,
86 + message: `${request.method}: ${JSON.stringify(request)}`,
87 + };
90 +export function extensionUiResponseCommand(
91 + id: string,
92 + input: ExtensionUiResponseInput,
93 +): { type: "extension_ui_response"; id: string } & ExtensionUiResponseInput {
94 + return { type: "extension_ui_response", id, ...input };
97 +function stringField(value: unknown): string {
98 + return typeof value === "string" ? value : "";
101 +function arrayField(value: unknown): string[] | undefined {
102 + return Array.isArray(value)
103 + ? value.filter((item) => typeof item === "string")
104 + : undefined;
107 +function notifyType(value: unknown): "info" | "warning" | "error" | undefined {
108 + return value === "info" || value === "warning" || value === "error"
109 + ? value
110 + : undefined;
113 +function extensionUiEventType(
114 + method: string,
115 +): "ui_status" | "ui_title" | "ui_widget" | "ui_editor_text" {
116 + if (method === "setTitle") return "ui_title";
117 + if (method === "setWidget") return "ui_widget";
118 + if (method === "set_editor_text") return "ui_editor_text";
119 + return "ui_status";

extensions/ui/protocol.ts

Modified
47 47 retryable: boolean;
50 +export type UiRequestEvent = UiEventBase & {
51 + type: "ui_request";
52 + requestId: string;
53 + method: "select" | "confirm" | "input" | "editor";
54 + title: string;
55 + message: string;
56 + options?: string[];
57 + placeholder?: string;
58 + prefill?: string;
59 + expectsResponse: true;
60 +};
62 +export type UiNotifyEvent = UiEventBase & {
63 + type: "ui_notify";
64 + requestId: string;
65 + message: string;
66 + notifyType?: "info" | "warning" | "error";
67 +};
69 +export type UiStatusEvent = UiEventBase & {
70 + type: "ui_status" | "ui_title" | "ui_widget" | "ui_editor_text";
71 + requestId: string;
72 + message: string;
73 + [key: string]: unknown;
74 +};
50 76 export type UiEvent =
51 77 | UiStateEvent
52 78 | UiLogEvent
53 79 | UiMessageEvent
54 80 | UiToolEvent
55 - | UiErrorEvent;
81 + | UiErrorEvent
82 + | UiRequestEvent
83 + | UiNotifyEvent
84 + | UiStatusEvent;
57 86 export type UiEventInput = Omit<UiEvent, keyof UiEventBase>;

extensions/ui/runtime.ts

Modified
8 8 } from "./command-adapter";
9 9 import { parseComposerInput } from "./composer";
10 10 import { EventStore } from "./events";
11 +import {
12 + extensionUiEventFromRequest,
13 + extensionUiResponseCommand,
14 + isExtensionUiRequest,
15 +} from "./extension-ui";
11 16 import { chooseLanAddress } from "./network";
12 17 import { openBrowser } from "./open";
13 18 import { renderQr } from "./qr";
186 191 publish({ type: "log", level: "info", message: "toggle expand requested" });
187 192 return;
194 + if (message.type === "ui_response") {
195 + await rpc.send(
196 + extensionUiResponseCommand(message.id, responseInput(message)),
197 + );
198 + return;
189 200 const parsed = parseComposerInput(message.text);
190 201 if (parsed.kind === "bash") {
191 202 publish({ type: "message", role: "user", text: message.text });
206 217 | { type: "abort" }
207 218 | { type: "cycle_model" }
208 219 | { type: "cycle-model" }
209 - | { type: "toggle-expand" };
220 + | { type: "toggle-expand" }
221 + | ({ type: "ui_response"; id: string } & (
222 + | { value: string }
223 + | { confirmed: boolean }
224 + | { cancelled: true }
225 + ));
211 227 function isBrowserMessage(value: unknown): value is BrowserMessage {
212 228 if (!value || typeof value !== "object") return false;
213 229 const type = (value as { type?: unknown }).type;
214 230 if (type === "prompt")
215 231 return typeof (value as { text?: unknown }).text === "string";
232 + if (type === "ui_response")
233 + return typeof (value as { id?: unknown }).id === "string";
216 234 return (
217 235 type === "abort" ||
218 236 type === "cycle_model" ||
242 +function responseInput(
243 + message: Extract<BrowserMessage, { type: "ui_response" }>,
244 +) {
245 + if ("value" in message) return { value: message.value };
246 + if ("confirmed" in message) return { confirmed: message.confirmed };
247 + return { cancelled: true as const };
224 250 function mapRpcState(state: string) {
225 251 if (state === "connected") return "rpc-connected";
226 252 if (state === "exit") return "rpc-dead";
230 256 function normalizeRpcEvent(
231 257 event: unknown,
232 258 ): Parameters<EventStore["append"]>[0] {
259 + if (isExtensionUiRequest(event)) return extensionUiEventFromRequest(event);
233 260 if (isRpcMessageUpdate(event)) {
234 261 return {
235 262 type: "message",

extensions/ui/web/app.css.ts

Modified
18 18 #ui-command-menu { position: absolute; left: 0; right: 0; bottom: 100%; border: 2px solid CanvasText; background: Canvas; max-height: 40vh; overflow: auto; }
19 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 20 .ui-command-item.is-selected { background: CanvasText; color: Canvas; }
21 +.ui-dialog-controls { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
22 +.ui-dialog-controls button { font: inherit; border: 2px solid CanvasText; background: Canvas; color: CanvasText; padding: 4px 8px; }
23 +.ui-dialog-controls textarea { min-width: min(100%, 420px); min-height: 80px; font: inherit; }
21 24 #ui-send { font: inherit; background: CanvasText; color: Canvas; border: 0; padding: 0 16px; }
22 25 #ui-send:disabled { opacity: .5; }

extensions/ui/web/app.js.ts

Modified
1 1 import { renderComposerComponentJs } from "./components/composer.js";
2 +import { renderDialogsComponentJs } from "./components/dialogs.js";
3 4 export function renderAppJs(): string {
4 5 return `const status = document.querySelector('#ui-status');
14 15 let socket;
16 17 ${renderComposerComponentJs()}
18 +${renderDialogsComponentJs()}
18 20 function authHeaders() {
19 21 const token = new URL(location.href).searchParams.get('token');
53 55 row.className = 'ui-event ui-event-' + (event.role || event.type);
54 56 row.dataset.eventId = String(event.id);
55 57 row.textContent = event.text || event.message || event.state || JSON.stringify(event);
58 + if (event.type === 'ui_request') renderUiRequest(row, event);
56 59 stream.append(row);
58 61 stream.scrollTop = stream.scrollHeight;

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

Added
new file mode 100644
1 +export type DialogResponse =
2 + | { type: "ui_response"; id: string; value: string }
3 + | { type: "ui_response"; id: string; confirmed: boolean }
4 + | { type: "ui_response"; id: string; cancelled: true };
6 +export function dialogResponse(
7 + id: string,
8 + method: string,
9 + value: string | boolean | undefined,
10 +): DialogResponse {
11 + if (value === undefined) return { type: "ui_response", id, cancelled: true };
12 + if (method === "confirm")
13 + return { type: "ui_response", id, confirmed: Boolean(value) };
14 + return { type: "ui_response", id, value: String(value) };
17 +export function renderDialogsComponentJs(): string {
18 + return `function dialogResponse(id, method, value) {
19 + if (value === undefined) return { type: 'ui_response', id, cancelled: true };
20 + if (method === 'confirm') return { type: 'ui_response', id, confirmed: Boolean(value) };
21 + return { type: 'ui_response', id, value: String(value) };
23 +function renderUiRequest(row, event) {
24 + const controls = document.createElement('div');
25 + controls.className = 'ui-dialog-controls';
26 + if (event.method === 'select') {
27 + for (const option of event.options || []) {
28 + const button = document.createElement('button');
29 + button.type = 'button';
30 + button.textContent = option;
31 + button.addEventListener('click', () => socket?.send(JSON.stringify(dialogResponse(event.requestId, event.method, option))));
32 + controls.append(button);
33 + }
34 + } else if (event.method === 'confirm') {
35 + for (const item of [['YES', true], ['NO', false]]) {
36 + const button = document.createElement('button');
37 + button.type = 'button';
38 + button.textContent = item[0];
39 + button.addEventListener('click', () => socket?.send(JSON.stringify(dialogResponse(event.requestId, event.method, item[1]))));
40 + controls.append(button);
41 + }
42 + } else {
43 + const field = document.createElement('textarea');
44 + field.value = event.prefill || '';
45 + field.placeholder = event.placeholder || '';
46 + const button = document.createElement('button');
47 + button.type = 'button';
48 + button.textContent = 'SEND';
49 + button.addEventListener('click', () => socket?.send(JSON.stringify(dialogResponse(event.requestId, event.method, field.value))));
50 + controls.append(field, button);
51 + }
52 + row.append(controls);
54 +`;