repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui widget bridge

fbbfeae7849ec2b30f45abeddce37e64277fd434

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

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/ui/__tests__/widget-bridge.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import {
3 + attributionEventFromWidgetMessage,
4 + renderWidgetBridgeScript,
5 + widgetActionMessage,
6 +} from "../web/components/widget-bridge.js";
8 +describe("ui widget bridge", () => {
9 + it("creates window.pi bridge source", () => {
10 + const source = renderWidgetBridgeScript("widget-1");
11 + expect(source).toContain("window.pi");
12 + expect(source).toContain("action");
13 + expect(source).toContain("prompt");
14 + expect(source).toContain("notify");
15 + });
17 + it("builds widget action messages", () => {
18 + expect(widgetActionMessage("widget-1", "Approve", { ok: true })).toEqual({
19 + type: "widget_action",
20 + widgetId: "widget-1",
21 + label: "Approve",
22 + payload: { ok: true },
23 + });
24 + });
26 + it("turns widget prompts into visible attribution events", () => {
27 + expect(
28 + attributionEventFromWidgetMessage(
29 + { type: "widget_prompt", widgetId: "widget-1", text: "continue" },
30 + () => 50,
31 + ),
32 + ).toEqual({
33 + type: "widget_action",
34 + widgetId: "widget-1",
35 + label: "prompt",
36 + payload: { text: "continue" },
37 + at: 50,
38 + });
39 + });
40 +});

extensions/ui/web/components/widget-bridge.js.ts

Added
new file mode 100644
1 +export type WidgetBridgeMessage =
2 + | {
3 + type: "widget_action";
4 + widgetId: string;
5 + label: string;
6 + payload?: unknown;
7 + }
8 + | { type: "widget_prompt"; widgetId: string; text: string }
9 + | { type: "widget_notify"; widgetId: string; message: string }
10 + | { type: "widget_error"; widgetId: string; message: string };
12 +export type WidgetAttributionEvent = {
13 + type: "widget_action";
14 + widgetId: string;
15 + label: string;
16 + payload?: unknown;
17 + at: number;
18 +};
20 +export function widgetActionMessage(
21 + widgetId: string,
22 + label: string,
23 + payload?: unknown,
24 +): WidgetBridgeMessage {
25 + return { type: "widget_action", widgetId, label, payload };
28 +export function attributionEventFromWidgetMessage(
29 + message: WidgetBridgeMessage,
30 + now: () => number = Date.now,
31 +): WidgetAttributionEvent {
32 + if (message.type === "widget_prompt") {
33 + return {
34 + type: "widget_action",
35 + widgetId: message.widgetId,
36 + label: "prompt",
37 + payload: { text: message.text },
38 + at: now(),
39 + };
40 + }
41 + if (message.type === "widget_notify") {
42 + return {
43 + type: "widget_action",
44 + widgetId: message.widgetId,
45 + label: "notify",
46 + payload: { message: message.message },
47 + at: now(),
48 + };
49 + }
50 + if (message.type === "widget_error") {
51 + return {
52 + type: "widget_action",
53 + widgetId: message.widgetId,
54 + label: "error",
55 + payload: { message: message.message },
56 + at: now(),
57 + };
58 + }
59 + return { ...message, at: now() };
62 +export function renderWidgetBridgeScript(widgetId: string): string {
63 + return `<script>
64 +(() => {
65 + const widgetId = ${JSON.stringify(widgetId)};
66 + function send(message) { parent.postMessage({ ...message, widgetId }, '*'); }
67 + window.pi = {
68 + action(label, payload) { send({ type: 'widget_action', label, payload }); },
69 + prompt(text) { send({ type: 'widget_prompt', text }); },
70 + steer(text) { send({ type: 'widget_prompt', text }); },
71 + followUp(text) { send({ type: 'widget_prompt', text }); },
72 + notify(message) { send({ type: 'widget_notify', message }); },
73 + state() { return { widgetId }; },
74 + on() {},
75 + update(payload) { send({ type: 'widget_action', label: 'update', payload }); },
76 + requestAttachment() { send({ type: 'widget_action', label: 'attachment-request' }); },
77 + };
78 + window.addEventListener('error', event => send({ type: 'widget_error', message: event.message }));
79 +})();
80 +</script>`;