repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui event protocol

47408681d1b4932200bd891cd3c45e4e620066d1

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

unsigned

Browse filesCompare with HEAD

Changed files (3)

extensions/ui/__tests__/events.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import { EventStore } from "../events";
4 +describe("ui event store", () => {
5 + it("assigns monotonic ids and timestamps", () => {
6 + const store = new EventStore({ maxEvents: 10, now: () => 1000 });
8 + const first = store.append({ type: "state", state: "starting" });
9 + const second = store.append({ type: "log", level: "info", message: "ready" });
11 + expect(first).toMatchObject({ id: 1, at: 1000, type: "state" });
12 + expect(second).toMatchObject({ id: 2, at: 1000, type: "log" });
13 + });
15 + it("keeps a bounded replay buffer", () => {
16 + const store = new EventStore({ maxEvents: 3, now: () => 1 });
18 + for (let i = 0; i < 5; i++) {
19 + store.append({ type: "log", level: "info", message: `event ${i}` });
20 + }
22 + expect(store.replayAfter(0).map((event) => event.id)).toEqual([3, 4, 5]);
23 + expect(store.replayAfter(3).map((event) => event.id)).toEqual([4, 5]);
24 + });
26 + it("builds deterministic snapshots", () => {
27 + const store = new EventStore({ maxEvents: 10, now: () => 7 });
28 + store.append({ type: "state", state: "starting" });
29 + store.append({ type: "message", role: "user", text: "hello" });
31 + expect(
32 + store.snapshot({
33 + status: "connected",
34 + sessionPath: "C:/tmp/ui.jsonl",
35 + commands: [{ name: "ui", description: "browser ui" }],
36 + }),
37 + ).toEqual({
38 + version: 1,
39 + lastEventId: 2,
40 + status: "connected",
41 + sessionPath: "C:/tmp/ui.jsonl",
42 + commands: [{ name: "ui", description: "browser ui" }],
43 + events: [
44 + { id: 1, at: 7, type: "state", state: "starting" },
45 + { id: 2, at: 7, type: "message", role: "user", text: "hello" },
46 + ],
47 + });
48 + });
49 +});

extensions/ui/events.ts

Added
new file mode 100644
1 +import type {
2 + UiCommandInfo,
3 + UiConnectionStatus,
4 + UiEvent,
5 + UiEventInput,
6 + UiSnapshot,
7 +} from "./protocol";
9 +export type EventStoreOptions = {
10 + maxEvents?: number;
11 + now?: () => number;
12 +};
14 +export type SnapshotOptions = {
15 + status: UiConnectionStatus;
16 + sessionPath?: string;
17 + commands?: UiCommandInfo[];
18 +};
20 +export class EventStore {
21 + readonly #maxEvents: number;
22 + readonly #now: () => number;
23 + #nextId = 1;
24 + #events: UiEvent[] = [];
26 + constructor(options: EventStoreOptions = {}) {
27 + this.#maxEvents = Math.max(1, options.maxEvents ?? 500);
28 + this.#now = options.now ?? Date.now;
29 + }
31 + append(input: UiEventInput): UiEvent {
32 + const event = { id: this.#nextId++, at: this.#now(), ...input } as UiEvent;
33 + this.#events.push(event);
34 + if (this.#events.length > this.#maxEvents) {
35 + this.#events.splice(0, this.#events.length - this.#maxEvents);
36 + }
37 + return event;
38 + }
40 + replayAfter(lastEventId: number): UiEvent[] {
41 + return this.#events.filter((event) => event.id > lastEventId);
42 + }
44 + snapshot(options: SnapshotOptions): UiSnapshot {
45 + return {
46 + version: 1,
47 + lastEventId: this.lastEventId(),
48 + status: options.status,
49 + sessionPath: options.sessionPath,
50 + commands: options.commands ?? [],
51 + events: [...this.#events],
52 + };
53 + }
55 + lastEventId(): number {
56 + return this.#nextId - 1;
57 + }

extensions/ui/protocol.ts

Added
new file mode 100644
1 +export type UiConnectionStatus =
2 + | "starting"
3 + | "waiting"
4 + | "connected"
5 + | "rpc-starting"
6 + | "rpc-connected"
7 + | "rpc-dead"
8 + | "error"
9 + | "stopped";
11 +export type UiEventBase = {
12 + id: number;
13 + at: number;
14 +};
16 +export type UiStateEvent = UiEventBase & {
17 + type: "state";
18 + state: UiConnectionStatus;
19 + message?: string;
20 +};
22 +export type UiLogEvent = UiEventBase & {
23 + type: "log";
24 + level: "info" | "warning" | "error";
25 + message: string;
26 +};
28 +export type UiMessageEvent = UiEventBase & {
29 + type: "message";
30 + role: "user" | "assistant" | "tool" | "system";
31 + text: string;
32 +};
34 +export type UiToolEvent = UiEventBase & {
35 + type: "tool";
36 + phase: "start" | "update" | "end";
37 + toolCallId: string;
38 + name: string;
39 + text?: string;
40 + isError?: boolean;
41 +};
43 +export type UiErrorEvent = UiEventBase & {
44 + type: "error";
45 + message: string;
46 + action: string;
47 + retryable: boolean;
48 +};
50 +export type UiEvent =
51 + | UiStateEvent
52 + | UiLogEvent
53 + | UiMessageEvent
54 + | UiToolEvent
55 + | UiErrorEvent;
57 +export type UiEventInput = Omit<UiEvent, keyof UiEventBase>;
59 +export type UiCommandInfo = {
60 + name: string;
61 + description?: string;
62 +};
64 +export type UiSnapshot = {
65 + version: 1;
66 + lastEventId: number;
67 + status: UiConnectionStatus;
68 + sessionPath?: string;
69 + commands: UiCommandInfo[];
70 + events: UiEvent[];
71 +};