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
Addednew 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" });
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}` });
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]);
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" });
33 + status:
"connected",
34 + sessionPath:
"C:/tmp/ui.jsonl",
35 + commands: [{ name:
"ui", description:
"browser ui" }],
40 + status:
"connected",
41 + sessionPath:
"C:/tmp/ui.jsonl",
42 + commands: [{ name:
"ui", description:
"browser ui" }],
44 + { id:
1, at:
7, type:
"state", state:
"starting" },
45 + { id:
2, at:
7, type:
"message", role:
"user", text:
"hello" },
extensions/ui/events.ts
Addednew file mode 100644 9 +export type EventStoreOptions = {
14 +export type SnapshotOptions = {
15 + status
: UiConnectionStatus;
16 + sessionPath
?: string;
17 + commands
?: UiCommandInfo[];
20 +export class EventStore {
21 + readonly #maxEvents
: number;
22 + readonly #now: ()
=> number;
24 + #events
: UiEvent[]
= [];
26 + constructor(options
: EventStoreOptions = {}) {
27 + this.#maxEvents
= Math.
max(
1, options.maxEvents
?? 500);
28 + this.#now
= options.now
?? Date.now;
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);
40 + replayAfter(lastEventId
: number)
: UiEvent[] {
41 + return this.#events.
filter((event)
=> event.
id > lastEventId);
44 + snapshot(options
: SnapshotOptions)
: UiSnapshot {
47 + lastEventId: this.
lastEventId(),
48 + status: options.
status,
49 + sessionPath: options.sessionPath,
50 + commands: options.commands
?? [],
51 + events: [
...this.#events],
55 + lastEventId()
: number {
56 + return this.#nextId
- 1;
extensions/ui/protocol.ts
Addednew file mode 100644 1 +export type UiConnectionStatus = 11 +export type UiEventBase = {
16 +export type UiStateEvent = UiEventBase & {
18 + state
: UiConnectionStatus;
22 +export type UiLogEvent = UiEventBase & {
24 + level
: "info" | "warning" | "error";
28 +export type UiMessageEvent = UiEventBase & {
30 + role
: "user" | "assistant" | "tool" | "system";
34 +export type UiToolEvent = UiEventBase & {
36 + phase
: "start" | "update" | "end";
43 +export type UiErrorEvent = UiEventBase & {
50 +export type UiEvent = 57 +export type UiEventInput = Omit<
UiEvent,
keyof UiEventBase>;
59 +export type UiCommandInfo = {
61 + description
?: string;
64 +export type UiSnapshot = {
66 + lastEventId
: number;
67 + status
: UiConnectionStatus;
68 + sessionPath
?: string;
69 + commands
: UiCommandInfo[];