repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/protocol.ts

Raw
File outline
export type UiConnectionStatus =
	| "starting"
	| "waiting"
	| "connected"
	| "inactive"
	| "rpc-starting"
	| "rpc-connected"
	| "rpc-dead"
	| "error"
	| "stopped";

export type UiEventBase = {
	id: number;
	at: number;
};

export type UiStateEvent = UiEventBase & {
	type: "state";
	state: UiConnectionStatus;
	message?: string;
};

export type UiLogEvent = UiEventBase & {
	type: "log";
	level: "info" | "warning" | "error";
	message: string;
};

export type UiMessageEvent = UiEventBase & {
	type: "message";
	role: "user" | "assistant" | "tool" | "system";
	text: string;
	streamId?: string;
};

export type UiToolEvent = UiEventBase & {
	type: "tool";
	phase: "start" | "update" | "end";
	toolCallId: string;
	name: string;
	text?: string;
	isError?: boolean;
};

export type UiErrorEvent = UiEventBase & {
	type: "error";
	message: string;
	action: string;
	retryable: boolean;
	clientId?: string;
	agentHint?: string;
	relatedId?: string;
};

export type UiRequestEvent = UiEventBase & {
	type: "ui_request";
	requestId: string;
	method: "select" | "confirm" | "input" | "editor";
	title: string;
	message: string;
	options?: string[];
	placeholder?: string;
	prefill?: string;
	expectsResponse: true;
};

export type UiNotifyEvent = UiEventBase & {
	type: "ui_notify";
	requestId: string;
	message: string;
	notifyType?: "info" | "warning" | "error";
};

export type UiStatusEvent = UiEventBase & {
	type: "ui_status" | "ui_title" | "ui_widget" | "ui_editor_text";
	requestId: string;
	message: string;
	[key: string]: unknown;
};

export type UiWidgetEvent = UiEventBase & {
	type: "widget_mount" | "widget_update" | "widget_action" | "widget_error";
	widgetId: string;
	toolCallId?: string;
	title?: string;
	fallback?: string;
	html?: string;
	status?: "mounted" | "updated" | "action" | "error";
	actionId?: string;
	label?: string;
	payload?: unknown;
	message?: string;
};

export type UiEvent =
	| UiStateEvent
	| UiLogEvent
	| UiMessageEvent
	| UiToolEvent
	| UiErrorEvent
	| UiRequestEvent
	| UiNotifyEvent
	| UiStatusEvent
	| UiWidgetEvent;

export type UiEventInput = UiEvent extends infer Event
	? Event extends UiEventBase
		? Omit<Event, keyof UiEventBase>
		: never
	: never;

export type UiCommandInfo = {
	name: string;
	description?: string;
};

export type UiSnapshot = {
	version: 1;
	lastEventId: number;
	status: UiConnectionStatus;
	sessionPath?: string;
	commands: UiCommandInfo[];
	events: UiEvent[];
};