feat(vscode-ide-companion): improve authentication flow with cached auth state

优化认证流程,支持缓存认证状态避免重复认证:
- 在 qwenAgentManager 中创建会话前先进行认证
- 在 qwenConnectionHandler 中检查缓存的认证状态
- 只在没有有效缓存时触发新的认证流程
- 认证失败时清除无效缓存
- 添加详细的日志记录用于调试
This commit is contained in:
yiliang114
2025-11-24 20:32:08 +08:00
parent 4ad377b0d8
commit f11d054a47
2 changed files with 63 additions and 13 deletions

View File

@@ -3,7 +3,7 @@
* Copyright 2025 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/
import * as vscode from 'vscode';
import { AcpConnection } from '../acp/acpConnection.js';
import type {
AcpSessionUpdate,
@@ -423,11 +423,28 @@ export class QwenAgentManager {
/**
* 创建新会话
*
* 注意认证应该在connect()方法中完成,这里只创建会话
*
* @param workingDir - 工作目录
* @returns 新创建的 session ID
*/
async createNewSession(workingDir: string): Promise<string | null> {
console.log('[QwenAgentManager] Creating new session...');
// 先进行认证
console.log('[QwenAgentManager] Authenticating before creating session...');
try {
const config = vscode.workspace.getConfiguration('qwenCode');
const openaiApiKey = config.get<string>('qwen.openaiApiKey', '');
const authMethod = openaiApiKey ? 'openai' : 'qwen-oauth';
await this.connection.authenticate(authMethod);
console.log('[QwenAgentManager] Authentication successful');
} catch (authError) {
console.error('[QwenAgentManager] Authentication failed:', authError);
throw authError;
}
await this.connection.newSession(workingDir);
const newSessionId = this.connection.currentSessionId;
console.log(