refactor: type session event protocol

This commit is contained in:
Federico Jaramillo Martinez
2026-05-07 23:51:26 +02:00
parent a77012ba83
commit 91957b0fe0
6 changed files with 24 additions and 16 deletions
+3 -2
View File
@@ -1,3 +1,4 @@
import type { GlobalSessionEvent, SessionUiEvent } from "../../shared/apiTypes.js";
import type { WebSocket } from "ws";
export class SessionEventHub {
@@ -21,14 +22,14 @@ export class SessionEventHub {
socket.on("close", () => this.globalSockets.delete(socket));
}
publish(sessionId: string, event: unknown): void {
publish(sessionId: string, event: SessionUiEvent): void {
const payload = JSON.stringify(event);
for (const socket of this.socketsBySession.get(sessionId) ?? []) {
if (socket.readyState === socket.OPEN) socket.send(payload);
}
}
publishGlobal(event: unknown): void {
publishGlobal(event: GlobalSessionEvent): void {
const payload = JSON.stringify(event);
for (const socket of this.globalSockets) {
if (socket.readyState === socket.OPEN) socket.send(payload);
+4 -4
View File
@@ -9,7 +9,7 @@ import {
type AgentSession,
type CreateAgentSessionRuntimeFactory,
} from "@earendil-works/pi-coding-agent";
import type { ClientCommand, ClientCommandResult, ClientMessagePage, ClientSession, ClientSessionStatus } from "../types.js";
import type { ClientCommand, ClientCommandResult, ClientMessagePage, ClientSession, ClientSessionStatus, SessionUiEvent } from "../types.js";
import type { SessionEventHub } from "../realtime/sessionEventHub.js";
import { BUILTIN_COMMANDS } from "./builtinCommands.js";
import { SessionCommandService } from "./sessionCommandService.js";
@@ -141,10 +141,10 @@ export class PiSessionService {
this.events.publish(session.sessionId, {
type: "shell.end",
output: result.output,
exitCode: result.exitCode,
...(result.exitCode === undefined ? {} : { exitCode: result.exitCode }),
cancelled: result.cancelled,
truncated: result.truncated,
fullOutputPath: result.fullOutputPath,
...(result.fullOutputPath === undefined ? {} : { fullOutputPath: result.fullOutputPath }),
});
this.publishActivity(session, "bash complete", result.exitCode === 0 ? "idle" : "error", command);
this.publishStatus(session);
@@ -348,7 +348,7 @@ function clampInteger(value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, Math.floor(value)));
}
function toClientEvent(event: unknown): unknown {
function toClientEvent(event: unknown): SessionUiEvent {
const eventType = getString(event, "type");
const assistantMessageEvent = getProperty(event, "assistantMessageEvent");
if (eventType === "message_update" && getString(assistantMessageEvent, "type") === "text_delta") {
+1
View File
@@ -10,4 +10,5 @@ export type {
CommandResult as ClientCommandResult,
SessionActivity as ClientSessionActivity,
SessionUiEvent,
GlobalSessionEvent,
} from "../shared/apiTypes.js";