From f07aed6c97f801353e8393858c5d1a95dd128346 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 12:21:00 +0000 Subject: [PATCH] Use shared app and session schemas in renderer RPC parsing Co-authored-by: Theo Browne --- apps/renderer/src/wsNativeApi.ts | 108 ++----------------------------- 1 file changed, 6 insertions(+), 102 deletions(-) diff --git a/apps/renderer/src/wsNativeApi.ts b/apps/renderer/src/wsNativeApi.ts index 0ab755e22e..caedbdcbb1 100644 --- a/apps/renderer/src/wsNativeApi.ts +++ b/apps/renderer/src/wsNativeApi.ts @@ -1,6 +1,4 @@ import type { - AppBootstrapResult, - AppHealthResult, AgentExit, NativeApi, OutputChunk, @@ -15,7 +13,10 @@ import { WS_EVENT_CHANNELS, agentSessionIdSchema, agentExitSchema, + appBootstrapResultSchema, + appHealthResultSchema, outputChunkSchema, + providerSessionListSchema, providerSessionSchema, providerTurnStartResultSchema, providerEventSchema, @@ -171,10 +172,6 @@ function normalizeCloseCode(value: unknown) { return value; } -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null; -} - class WsNativeApiClient { private socket: WebSocket | null = null; private connectPromise: Promise | null = null; @@ -361,99 +358,6 @@ class WsNativeApiClient { } } - private parseAppHealthResult(method: string, value: unknown): AppHealthResult { - if (!isRecord(value)) { - throw new Error(`Runtime method '${method}' returned invalid response payload.`); - } - const launchCwd = value.launchCwd; - const sessionCount = value.sessionCount; - const activeClientConnected = value.activeClientConnected; - if ( - value.status !== "ok" || - typeof launchCwd !== "string" || - launchCwd.length === 0 || - typeof sessionCount !== "number" || - !Number.isInteger(sessionCount) || - sessionCount < 0 || - typeof activeClientConnected !== "boolean" - ) { - throw new Error(`Runtime method '${method}' returned invalid response payload.`); - } - const normalizedSessionCount = sessionCount as number; - - return { - status: "ok", - launchCwd, - sessionCount: normalizedSessionCount, - activeClientConnected, - }; - } - - private parseAppBootstrapResult(method: string, value: unknown): AppBootstrapResult { - if (!isRecord(value)) { - throw new Error(`Runtime method '${method}' returned invalid response payload.`); - } - - const launchCwd = value.launchCwd; - const projectName = value.projectName; - const provider = value.provider; - const model = value.model; - const bootstrapError = value.bootstrapError; - const parsedSession = providerSessionSchema.safeParse(value.session); - if ( - !parsedSession.success || - typeof launchCwd !== "string" || - launchCwd.length === 0 || - typeof projectName !== "string" || - projectName.length === 0 || - typeof model !== "string" || - model.length === 0 || - (bootstrapError !== undefined && - (typeof bootstrapError !== "string" || bootstrapError.length === 0)) - ) { - throw new Error(`Runtime method '${method}' returned invalid response payload.`); - } - if (provider !== "codex" && provider !== "claudeCode") { - throw new Error(`Runtime method '${method}' returned invalid response payload.`); - } - const normalizedProvider = provider as AppBootstrapResult["provider"]; - - const baseResult = { - launchCwd, - projectName, - provider: normalizedProvider, - model, - session: parsedSession.data, - }; - if (bootstrapError !== undefined) { - return { - ...baseResult, - bootstrapError, - }; - } - - return baseResult; - } - - private parseProviderSessionList( - method: string, - value: unknown, - ): Awaited> { - if (!Array.isArray(value)) { - throw new Error(`Runtime method '${method}' returned invalid response payload.`); - } - - const parsedSessions: Awaited> = []; - for (const session of value) { - const parsedSession = providerSessionSchema.safeParse(session); - if (!parsedSession.success) { - throw new Error(`Runtime method '${method}' returned invalid response payload.`); - } - parsedSessions.push(parsedSession.data); - } - return parsedSessions; - } - private handleResponse(message: WsResponseMessage) { const pending = this.pending.get(message.id); if (!pending) { @@ -560,8 +464,8 @@ class WsNativeApiClient { asNativeApi(): NativeApi { return { app: { - bootstrap: async () => this.parseAppBootstrapResult("app.bootstrap", await this.request("app.bootstrap")), - health: async () => this.parseAppHealthResult("app.health", await this.request("app.health")), + bootstrap: async () => this.requestParsed("app.bootstrap", appBootstrapResultSchema), + health: async () => this.requestParsed("app.health", appHealthResultSchema), }, todos: { list: async () => this.requestParsed("todos.list", todoListSchema), @@ -607,7 +511,7 @@ class WsNativeApiClient { respondToRequest: async (input) => this.requestNullResult("providers.respondToRequest", input), stopSession: async (input) => this.requestNullResult("providers.stopSession", input), listSessions: async () => - this.parseProviderSessionList("providers.listSessions", await this.request("providers.listSessions")), + this.requestParsed("providers.listSessions", providerSessionListSchema), onEvent: (callback) => { this.providerEventListeners.add(callback); return () => {