From d2313b9d1c1540fbe8a1de66788e14a2eca68cbd Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Sat, 25 Oct 2025 18:49:42 +0200 Subject: [PATCH] refactor: memoize useWebSocket return value and improve formatting consistency --- packages/server/src/server/acp/types.ts | 53 ++++++++++++++++++++----- packages/server/src/server/session.ts | 22 ++++++---- 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/packages/server/src/server/acp/types.ts b/packages/server/src/server/acp/types.ts index 56a551f3a..286f33d51 100644 --- a/packages/server/src/server/acp/types.ts +++ b/packages/server/src/server/acp/types.ts @@ -1,21 +1,43 @@ -import type { SessionNotification, RequestPermissionRequest, ClientSideConnection } from "@agentclientprotocol/sdk"; +import type { + SessionNotification, + RequestPermissionRequest, + ClientSideConnection, +} from "@agentclientprotocol/sdk"; import type { ChildProcess } from "child_process"; /** * Extended update types with messageId for proper deduplication * messageId is optional since some sources may not provide it */ -type UserMessageChunkWithId = Extract & { messageId?: string }; -type AgentMessageChunkWithId = Extract & { messageId?: string }; -type AgentThoughtChunkWithId = Extract & { messageId?: string }; +type UserMessageChunkWithId = Extract< + SessionNotification["update"], + { sessionUpdate: "user_message_chunk" } +> & { messageId?: string }; +type AgentMessageChunkWithId = Extract< + SessionNotification["update"], + { sessionUpdate: "agent_message_chunk" } +> & { messageId?: string }; +type AgentThoughtChunkWithId = Extract< + SessionNotification["update"], + { sessionUpdate: "agent_thought_chunk" } +> & { messageId?: string }; export type EnrichedSessionUpdate = | UserMessageChunkWithId | AgentMessageChunkWithId | AgentThoughtChunkWithId - | Exclude; + | Exclude< + SessionNotification["update"], + { + sessionUpdate: + | "user_message_chunk" + | "agent_message_chunk" + | "agent_thought_chunk"; + } + >; -export interface EnrichedSessionNotification extends Omit { +export interface EnrichedSessionNotification + extends Omit { update: EnrichedSessionUpdate; } @@ -23,10 +45,15 @@ export interface EnrichedSessionNotification extends Omit update.notification.type === "session" && - update.notification.notification.update.sessionUpdate === "user_message_chunk" + update.notification.notification.update.sessionUpdate === + "user_message_chunk" ); // Need at least one user message and some additional updates (3-5) for context @@ -467,14 +468,15 @@ export class Session { break; case "create_agent_request": - await this.handleCreateAgentRequest(msg.cwd, msg.initialMode, msg.requestId); + await this.handleCreateAgentRequest( + msg.cwd, + msg.initialMode, + msg.requestId + ); break; case "initialize_agent_request": - await this.handleInitializeAgentRequest( - msg.agentId, - msg.requestId - ); + await this.handleInitializeAgentRequest(msg.agentId, msg.requestId); break; case "set_agent_mode": @@ -790,8 +792,12 @@ export class Session { this.subscribeToAgent(agentId); // Auto-initialize agent immediately on explicit creation so it's ready to use - console.log(`[Session ${this.clientId}] Auto-initializing agent ${agentId}`); - const { info } = await this.agentManager.initializeAgentAndGetHistory(agentId); + console.log( + `[Session ${this.clientId}] Auto-initializing agent ${agentId}` + ); + const { info } = await this.agentManager.initializeAgentAndGetHistory( + agentId + ); // Emit agent_created message with initialized info this.emit({