From 828b76082075efaaaa198c352e04c4562c12d143 Mon Sep 17 00:00:00 2001 From: yiliang114 <1204183885@qq.com> Date: Tue, 9 Dec 2025 00:15:44 +0800 Subject: [PATCH] refactor(vscode-ide-companion): clean up code and improve type safety - Remove commented out error handling code in acpMessageHandler - Simplify session update handler by removing redundant comments - Clean up chat types interface definitions - Simplify null check in MessageRouter - Improve type safety in SettingsMessageHandler with ApprovalModeValue type --- .../src/services/acpMessageHandler.ts | 18 ---------- .../src/services/qwenSessionUpdateHandler.ts | 12 ------- .../src/types/chatTypes.ts | 34 ------------------- .../src/webview/handlers/MessageRouter.ts | 8 +---- .../handlers/SettingsMessageHandler.ts | 21 +++--------- 5 files changed, 6 insertions(+), 87 deletions(-) diff --git a/packages/vscode-ide-companion/src/services/acpMessageHandler.ts b/packages/vscode-ide-companion/src/services/acpMessageHandler.ts index 2ef58dc1..db7802ce 100644 --- a/packages/vscode-ide-companion/src/services/acpMessageHandler.ts +++ b/packages/vscode-ide-companion/src/services/acpMessageHandler.ts @@ -233,21 +233,3 @@ export class AcpMessageHandler { } } } - -// [ -// { -// received: 'reject_once', -// code: 'invalid_enum_value', -// options: [ -// 'proceed_once', -// 'proceed_always', -// 'proceed_always_server', -// 'proceed_always_tool', -// 'modify_with_editor', -// 'cancel', -// ], -// path: [], -// message: -// "Invalid enum value. Expected 'proceed_once' | 'proceed_always' | 'proceed_always_server' | 'proceed_always_tool' | 'modify_with_editor' | 'cancel', received 'reject_once'", -// }, -// ]; diff --git a/packages/vscode-ide-companion/src/services/qwenSessionUpdateHandler.ts b/packages/vscode-ide-companion/src/services/qwenSessionUpdateHandler.ts index dba6180e..e27fbe67 100644 --- a/packages/vscode-ide-companion/src/services/qwenSessionUpdateHandler.ts +++ b/packages/vscode-ide-companion/src/services/qwenSessionUpdateHandler.ts @@ -47,30 +47,20 @@ export class QwenSessionUpdateHandler { switch (update.sessionUpdate) { case 'user_message_chunk': - // Handle user message chunk if (update.content?.text && this.callbacks.onStreamChunk) { this.callbacks.onStreamChunk(update.content.text); } break; case 'agent_message_chunk': - // Handle assistant message chunk if (update.content?.text && this.callbacks.onStreamChunk) { this.callbacks.onStreamChunk(update.content.text); } break; case 'agent_thought_chunk': - // Handle thought chunk - use special callback - console.log( - '[SessionUpdateHandler] 🧠 THOUGHT CHUNK:', - update.content?.text, - ); if (update.content?.text) { if (this.callbacks.onThoughtChunk) { - console.log( - '[SessionUpdateHandler] 🧠 Calling onThoughtChunk callback', - ); this.callbacks.onThoughtChunk(update.content.text); } else if (this.callbacks.onStreamChunk) { // Fallback to regular stream processing @@ -103,7 +93,6 @@ export class QwenSessionUpdateHandler { } case 'tool_call_update': { - // Handle tool call status update if (this.callbacks.onToolCall && 'toolCallId' in update) { this.callbacks.onToolCall({ toolCallId: update.toolCallId as string, @@ -123,7 +112,6 @@ export class QwenSessionUpdateHandler { } case 'plan': { - // Handle plan update if ('entries' in update) { const entries = update.entries as Array<{ content: string; diff --git a/packages/vscode-ide-companion/src/types/chatTypes.ts b/packages/vscode-ide-companion/src/types/chatTypes.ts index ae530081..90ebbb87 100644 --- a/packages/vscode-ide-companion/src/types/chatTypes.ts +++ b/packages/vscode-ide-companion/src/types/chatTypes.ts @@ -11,60 +11,30 @@ export interface ChatMessage { timestamp: number; } -/** - * Plan Entry - * Represents a single step in the AI's execution plan - */ export interface PlanEntry { - /** The detailed description of this plan step */ content: string; - /** The priority level of this plan step */ priority?: 'high' | 'medium' | 'low'; - /** The current execution status of this plan step */ status: 'pending' | 'in_progress' | 'completed'; } -/** - * Tool Call Update Data - * Contains information about a tool call execution or update - */ export interface ToolCallUpdateData { - /** Unique identifier for this tool call */ toolCallId: string; - /** The type of tool being called (e.g., 'read', 'write', 'execute') */ kind?: string; - /** Human-readable title or description of the tool call */ title?: string; - /** Current execution status of the tool call */ status?: string; - /** Raw input parameters passed to the tool */ rawInput?: unknown; - /** Content or output data from the tool execution */ content?: Array>; - /** File locations associated with this tool call */ locations?: Array<{ path: string; line?: number | null }>; } -/** - * Callback Functions Collection - * Defines all possible callback functions for the Qwen Agent - */ export interface QwenAgentCallbacks { - /** Callback for receiving chat messages from the agent */ onMessage?: (message: ChatMessage) => void; - /** Callback for receiving streamed text chunks during generation */ onStreamChunk?: (chunk: string) => void; - /** Callback for receiving thought process chunks during generation */ onThoughtChunk?: (chunk: string) => void; - /** Callback for receiving tool call updates during execution */ onToolCall?: (update: ToolCallUpdateData) => void; - /** Callback for receiving execution plan updates */ onPlan?: (entries: PlanEntry[]) => void; - /** Callback for handling permission requests from the agent */ onPermissionRequest?: (request: AcpPermissionRequest) => Promise; - /** Callback triggered when the agent reaches the end of a turn */ onEndTurn?: () => void; - /** Callback for receiving mode information after ACP initialization */ onModeInfo?: (info: { currentModeId?: ApprovalModeValue; availableModes?: Array<{ @@ -73,13 +43,9 @@ export interface QwenAgentCallbacks { description: string; }>; }) => void; - /** Callback for receiving notifications when the mode changes */ onModeChanged?: (modeId: ApprovalModeValue) => void; } -/** - * Tool call update type - */ export interface ToolCallUpdate { type: 'tool_call' | 'tool_call_update'; toolCallId: string; diff --git a/packages/vscode-ide-companion/src/webview/handlers/MessageRouter.ts b/packages/vscode-ide-companion/src/webview/handlers/MessageRouter.ts index b60086d3..adf94e29 100644 --- a/packages/vscode-ide-companion/src/webview/handlers/MessageRouter.ts +++ b/packages/vscode-ide-companion/src/webview/handlers/MessageRouter.ts @@ -150,13 +150,7 @@ export class MessageRouter { */ setLoginHandler(handler: () => Promise): void { this.authHandler.setLoginHandler(handler); - // Also set login handler for SessionMessageHandler - if ( - this.sessionHandler && - typeof this.sessionHandler.setLoginHandler === 'function' - ) { - this.sessionHandler.setLoginHandler(handler); - } + this.sessionHandler?.setLoginHandler?.(handler); } /** diff --git a/packages/vscode-ide-companion/src/webview/handlers/SettingsMessageHandler.ts b/packages/vscode-ide-companion/src/webview/handlers/SettingsMessageHandler.ts index af9003ad..7ea8e732 100644 --- a/packages/vscode-ide-companion/src/webview/handlers/SettingsMessageHandler.ts +++ b/packages/vscode-ide-companion/src/webview/handlers/SettingsMessageHandler.ts @@ -6,6 +6,7 @@ import * as vscode from 'vscode'; import { BaseMessageHandler } from './BaseMessageHandler.js'; +import type { ApprovalModeValue } from '../../types/acpTypes.js'; /** * Settings message handler @@ -31,7 +32,7 @@ export class SettingsMessageHandler extends BaseMessageHandler { case 'setApprovalMode': await this.handleSetApprovalMode( message.data as { - modeId?: 'plan' | 'default' | 'auto-edit' | 'yolo'; + modeId?: ApprovalModeValue; }, ); break; @@ -83,23 +84,11 @@ export class SettingsMessageHandler extends BaseMessageHandler { * Set approval mode via agent (ACP session/set_mode) */ private async handleSetApprovalMode(data?: { - modeId?: 'plan' | 'default' | 'auto-edit' | 'yolo'; + modeId?: ApprovalModeValue; }): Promise { try { - const modeId = (data?.modeId || 'default') as - | 'plan' - | 'default' - | 'auto-edit' - | 'yolo'; - await this.agentManager.setApprovalModeFromUi( - modeId === 'plan' - ? 'plan' - : modeId === 'auto-edit' - ? 'auto' - : modeId === 'yolo' - ? 'yolo' - : 'ask', - ); + const modeId = data?.modeId || 'default'; + await this.agentManager.setApprovalModeFromUi(modeId); // No explicit response needed; WebView listens for modeChanged } catch (error) { console.error('[SettingsMessageHandler] Failed to set mode:', error);