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
This commit is contained in:
yiliang114
2025-12-09 00:15:44 +08:00
parent ef3d7b92d0
commit 828b760820
5 changed files with 6 additions and 87 deletions

View File

@@ -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'",
// },
// ];

View File

@@ -47,30 +47,20 @@ export class QwenSessionUpdateHandler {
switch (update.sessionUpdate) { switch (update.sessionUpdate) {
case 'user_message_chunk': case 'user_message_chunk':
// Handle user message chunk
if (update.content?.text && this.callbacks.onStreamChunk) { if (update.content?.text && this.callbacks.onStreamChunk) {
this.callbacks.onStreamChunk(update.content.text); this.callbacks.onStreamChunk(update.content.text);
} }
break; break;
case 'agent_message_chunk': case 'agent_message_chunk':
// Handle assistant message chunk
if (update.content?.text && this.callbacks.onStreamChunk) { if (update.content?.text && this.callbacks.onStreamChunk) {
this.callbacks.onStreamChunk(update.content.text); this.callbacks.onStreamChunk(update.content.text);
} }
break; break;
case 'agent_thought_chunk': case 'agent_thought_chunk':
// Handle thought chunk - use special callback
console.log(
'[SessionUpdateHandler] 🧠 THOUGHT CHUNK:',
update.content?.text,
);
if (update.content?.text) { if (update.content?.text) {
if (this.callbacks.onThoughtChunk) { if (this.callbacks.onThoughtChunk) {
console.log(
'[SessionUpdateHandler] 🧠 Calling onThoughtChunk callback',
);
this.callbacks.onThoughtChunk(update.content.text); this.callbacks.onThoughtChunk(update.content.text);
} else if (this.callbacks.onStreamChunk) { } else if (this.callbacks.onStreamChunk) {
// Fallback to regular stream processing // Fallback to regular stream processing
@@ -103,7 +93,6 @@ export class QwenSessionUpdateHandler {
} }
case 'tool_call_update': { case 'tool_call_update': {
// Handle tool call status update
if (this.callbacks.onToolCall && 'toolCallId' in update) { if (this.callbacks.onToolCall && 'toolCallId' in update) {
this.callbacks.onToolCall({ this.callbacks.onToolCall({
toolCallId: update.toolCallId as string, toolCallId: update.toolCallId as string,
@@ -123,7 +112,6 @@ export class QwenSessionUpdateHandler {
} }
case 'plan': { case 'plan': {
// Handle plan update
if ('entries' in update) { if ('entries' in update) {
const entries = update.entries as Array<{ const entries = update.entries as Array<{
content: string; content: string;

View File

@@ -11,60 +11,30 @@ export interface ChatMessage {
timestamp: number; timestamp: number;
} }
/**
* Plan Entry
* Represents a single step in the AI's execution plan
*/
export interface PlanEntry { export interface PlanEntry {
/** The detailed description of this plan step */
content: string; content: string;
/** The priority level of this plan step */
priority?: 'high' | 'medium' | 'low'; priority?: 'high' | 'medium' | 'low';
/** The current execution status of this plan step */
status: 'pending' | 'in_progress' | 'completed'; status: 'pending' | 'in_progress' | 'completed';
} }
/**
* Tool Call Update Data
* Contains information about a tool call execution or update
*/
export interface ToolCallUpdateData { export interface ToolCallUpdateData {
/** Unique identifier for this tool call */
toolCallId: string; toolCallId: string;
/** The type of tool being called (e.g., 'read', 'write', 'execute') */
kind?: string; kind?: string;
/** Human-readable title or description of the tool call */
title?: string; title?: string;
/** Current execution status of the tool call */
status?: string; status?: string;
/** Raw input parameters passed to the tool */
rawInput?: unknown; rawInput?: unknown;
/** Content or output data from the tool execution */
content?: Array<Record<string, unknown>>; content?: Array<Record<string, unknown>>;
/** File locations associated with this tool call */
locations?: Array<{ path: string; line?: number | null }>; locations?: Array<{ path: string; line?: number | null }>;
} }
/**
* Callback Functions Collection
* Defines all possible callback functions for the Qwen Agent
*/
export interface QwenAgentCallbacks { export interface QwenAgentCallbacks {
/** Callback for receiving chat messages from the agent */
onMessage?: (message: ChatMessage) => void; onMessage?: (message: ChatMessage) => void;
/** Callback for receiving streamed text chunks during generation */
onStreamChunk?: (chunk: string) => void; onStreamChunk?: (chunk: string) => void;
/** Callback for receiving thought process chunks during generation */
onThoughtChunk?: (chunk: string) => void; onThoughtChunk?: (chunk: string) => void;
/** Callback for receiving tool call updates during execution */
onToolCall?: (update: ToolCallUpdateData) => void; onToolCall?: (update: ToolCallUpdateData) => void;
/** Callback for receiving execution plan updates */
onPlan?: (entries: PlanEntry[]) => void; onPlan?: (entries: PlanEntry[]) => void;
/** Callback for handling permission requests from the agent */
onPermissionRequest?: (request: AcpPermissionRequest) => Promise<string>; onPermissionRequest?: (request: AcpPermissionRequest) => Promise<string>;
/** Callback triggered when the agent reaches the end of a turn */
onEndTurn?: () => void; onEndTurn?: () => void;
/** Callback for receiving mode information after ACP initialization */
onModeInfo?: (info: { onModeInfo?: (info: {
currentModeId?: ApprovalModeValue; currentModeId?: ApprovalModeValue;
availableModes?: Array<{ availableModes?: Array<{
@@ -73,13 +43,9 @@ export interface QwenAgentCallbacks {
description: string; description: string;
}>; }>;
}) => void; }) => void;
/** Callback for receiving notifications when the mode changes */
onModeChanged?: (modeId: ApprovalModeValue) => void; onModeChanged?: (modeId: ApprovalModeValue) => void;
} }
/**
* Tool call update type
*/
export interface ToolCallUpdate { export interface ToolCallUpdate {
type: 'tool_call' | 'tool_call_update'; type: 'tool_call' | 'tool_call_update';
toolCallId: string; toolCallId: string;

View File

@@ -150,13 +150,7 @@ export class MessageRouter {
*/ */
setLoginHandler(handler: () => Promise<void>): void { setLoginHandler(handler: () => Promise<void>): void {
this.authHandler.setLoginHandler(handler); this.authHandler.setLoginHandler(handler);
// Also set login handler for SessionMessageHandler this.sessionHandler?.setLoginHandler?.(handler);
if (
this.sessionHandler &&
typeof this.sessionHandler.setLoginHandler === 'function'
) {
this.sessionHandler.setLoginHandler(handler);
}
} }
/** /**

View File

@@ -6,6 +6,7 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { BaseMessageHandler } from './BaseMessageHandler.js'; import { BaseMessageHandler } from './BaseMessageHandler.js';
import type { ApprovalModeValue } from '../../types/acpTypes.js';
/** /**
* Settings message handler * Settings message handler
@@ -31,7 +32,7 @@ export class SettingsMessageHandler extends BaseMessageHandler {
case 'setApprovalMode': case 'setApprovalMode':
await this.handleSetApprovalMode( await this.handleSetApprovalMode(
message.data as { message.data as {
modeId?: 'plan' | 'default' | 'auto-edit' | 'yolo'; modeId?: ApprovalModeValue;
}, },
); );
break; break;
@@ -83,23 +84,11 @@ export class SettingsMessageHandler extends BaseMessageHandler {
* Set approval mode via agent (ACP session/set_mode) * Set approval mode via agent (ACP session/set_mode)
*/ */
private async handleSetApprovalMode(data?: { private async handleSetApprovalMode(data?: {
modeId?: 'plan' | 'default' | 'auto-edit' | 'yolo'; modeId?: ApprovalModeValue;
}): Promise<void> { }): Promise<void> {
try { try {
const modeId = (data?.modeId || 'default') as const modeId = data?.modeId || 'default';
| 'plan' await this.agentManager.setApprovalModeFromUi(modeId);
| 'default'
| 'auto-edit'
| 'yolo';
await this.agentManager.setApprovalModeFromUi(
modeId === 'plan'
? 'plan'
: modeId === 'auto-edit'
? 'auto'
: modeId === 'yolo'
? 'yolo'
: 'ask',
);
// No explicit response needed; WebView listens for modeChanged // No explicit response needed; WebView listens for modeChanged
} catch (error) { } catch (error) {
console.error('[SettingsMessageHandler] Failed to set mode:', error); console.error('[SettingsMessageHandler] Failed to set mode:', error);