test(vscode-ide-companion): 尝试通过 session/load 旧会话

- 修改了 WebViewProvider 中的逻辑,先尝试通过 ACP 加载旧会话
- 如果加载失败,则创建新会话作为回退方案
- 在 AcpConnection 中添加了初始化响应的日志输出
- 在 QwenAgentManager 中添加了新的 loadSessionViaAcp 方法,用于测试 ACP 的 session/load 功能
This commit is contained in:
yiliang114
2025-11-19 17:08:25 +08:00
parent 454cbfdde4
commit bc2b503e8d
3 changed files with 66 additions and 23 deletions

View File

@@ -754,34 +754,53 @@ export class WebViewProvider {
console.log('[WebViewProvider] Could not get session details:', err);
}
// IMPORTANT: CLI doesn't support loading old sessions
// So we always create a NEW ACP session for continuation
// TESTING: Try to load session via ACP first, fallback to creating new session
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
const workingDir = workspaceFolder?.uri.fsPath || process.cwd();
try {
const newAcpSessionId =
await this.agentManager.createNewSession(workingDir);
console.log('[WebViewProvider] Testing session/load via ACP...');
const loadResponse =
await this.agentManager.loadSessionViaAcp(sessionId);
console.log('[WebViewProvider] session/load succeeded:', loadResponse);
// If load succeeded, use the loaded session
this.currentConversationId = sessionId;
console.log(
'[WebViewProvider] Created new ACP session for conversation:',
newAcpSessionId,
'[WebViewProvider] Set currentConversationId to loaded session:',
sessionId,
);
} catch (_loadError) {
console.log(
'[WebViewProvider] session/load not supported, creating new session',
);
// Use the NEW ACP session ID for sending messages to CLI
this.currentConversationId = newAcpSessionId;
console.log(
'[WebViewProvider] Set currentConversationId (ACP) to:',
newAcpSessionId,
);
} catch (createError) {
console.error(
'[WebViewProvider] Failed to create new ACP session:',
createError,
);
vscode.window.showWarningMessage(
'Could not switch to session. Created new session instead.',
);
throw createError;
// Fallback: CLI doesn't support loading old sessions
// So we create a NEW ACP session for continuation
try {
const newAcpSessionId =
await this.agentManager.createNewSession(workingDir);
console.log(
'[WebViewProvider] Created new ACP session for conversation:',
newAcpSessionId,
);
// Use the NEW ACP session ID for sending messages to CLI
this.currentConversationId = newAcpSessionId;
console.log(
'[WebViewProvider] Set currentConversationId (ACP) to:',
newAcpSessionId,
);
} catch (createError) {
console.error(
'[WebViewProvider] Failed to create new ACP session:',
createError,
);
vscode.window.showWarningMessage(
'Could not switch to session. Created new session instead.',
);
throw createError;
}
}
// Send messages and session details to WebView

View File

@@ -193,11 +193,13 @@ export class AcpConnection {
});
// 初始化协议
await this.sessionManager.initialize(
const res = await this.sessionManager.initialize(
this.child,
this.pendingRequests,
this.nextRequestId,
);
console.log('[ACP] Initialization response:', res);
}
/**

View File

@@ -127,7 +127,7 @@ export class QwenAgentManager {
}
/**
* 获取会话消息
* 获取会话消息(从磁盘读取)
*
* @param sessionId - 会话ID
* @returns 消息列表
@@ -159,6 +159,28 @@ export class QwenAgentManager {
}
}
/**
* 尝试通过 ACP session/load 方法加载会话
* 这是一个测试方法,用于验证 CLI 是否支持 session/load
*
* @param sessionId - 会话ID
* @returns 加载响应或错误
*/
async loadSessionViaAcp(sessionId: string): Promise<unknown> {
try {
console.log(
'[QwenAgentManager] Testing session/load via ACP for:',
sessionId,
);
const response = await this.connection.loadSession(sessionId);
console.log('[QwenAgentManager] Session load response:', response);
return response;
} catch (error) {
console.error('[QwenAgentManager] Session load via ACP failed:', error);
throw error;
}
}
/**
* 创建新会话
*