From 31d8dcf23da51ac7f2a38b826c78a60b37b8f07b Mon Sep 17 00:00:00 2001 From: hex2077 Date: Sun, 27 Jul 2025 23:47:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(openai-kiro):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84=E9=85=8D=E7=BD=AE=E9=A1=B9?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E5=87=AD=E8=AF=81=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除已注释掉的未使用配置项,简化代码结构 优化凭证加载逻辑,跳过特定token文件并添加注释说明 --- src/openai/openai-kiro.js | 42 +++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/openai/openai-kiro.js b/src/openai/openai-kiro.js index d33ff7b..6e7a434 100644 --- a/src/openai/openai-kiro.js +++ b/src/openai/openai-kiro.js @@ -67,15 +67,15 @@ export class KiroApiService { this.config = config; this.credPath = config.KIRO_OAUTH_CREDS_DIR_PATH || path.join(os.homedir(), ".aws", "sso", "cache"); this.credsBase64 = config.KIRO_OAUTH_CREDS_BASE64; - this.accessToken = config.KIRO_ACCESS_TOKEN; - this.refreshToken = config.KIRO_REFRESH_TOKEN; - this.clientId = config.KIRO_CLIENT_ID; - this.clientSecret = config.KIRO_CLIENT_SECRET; - this.authMethod = KIRO_CONSTANTS.AUTH_METHOD_SOCIAL; - this.refreshUrl = KIRO_CONSTANTS.REFRESH_URL; - this.refreshIDCUrl = KIRO_CONSTANTS.REFRESH_IDC_URL; - this.baseUrl = KIRO_CONSTANTS.BASE_URL; - this.amazonQUrl = KIRO_CONSTANTS.AMAZON_Q_URL; + // this.accessToken = config.KIRO_ACCESS_TOKEN; + // this.refreshToken = config.KIRO_REFRESH_TOKEN; + // this.clientId = config.KIRO_CLIENT_ID; + // this.clientSecret = config.KIRO_CLIENT_SECRET; + // this.authMethod = KIRO_CONSTANTS.AUTH_METHOD_SOCIAL; + // this.refreshUrl = KIRO_CONSTANTS.REFRESH_URL; + // this.refreshIDCUrl = KIRO_CONSTANTS.REFRESH_IDC_URL; + // this.baseUrl = KIRO_CONSTANTS.BASE_URL; + // this.amazonQUrl = KIRO_CONSTANTS.AMAZON_Q_URL; // Add kiro-oauth-creds-base64 and kiro-oauth-creds-file to config if (config.KIRO_OAUTH_CREDS_BASE64) { @@ -186,23 +186,21 @@ async initializeAuth(forceRefresh = false) { } // Priority 3: Load from default directory if no specific file path and no token file credentials - if (Object.keys(mergedCredentials).length === 0) { - const dirPath = this.credPath; - console.debug(`[Kiro Auth] Attempting to load credentials from directory: ${dirPath}`); - const files = await fs.readdir(dirPath); - - for (const file of files) { - if (file.endsWith('.json')) { - const filePath = path.join(dirPath, file); - const credentials = await loadCredentialsFromFile(filePath); - if (credentials) { - Object.assign(mergedCredentials, credentials); - console.debug(`[Kiro Auth] Loaded credentials from ${file}`); - } + const dirPath = this.credPath; + console.debug(`[Kiro Auth] Attempting to load credentials from directory: ${dirPath}`); + const files = await fs.readdir(dirPath); + for (const file of files) { + if (file.endsWith('.json') && file !== KIRO_AUTH_TOKEN_FILE) { + const filePath = path.join(dirPath, file); + const credentials = await loadCredentialsFromFile(filePath); + if (credentials) { + Object.assign(mergedCredentials, credentials); + console.debug(`[Kiro Auth] Loaded credentials from ${file}`); } } } + // console.log('[Kiro Auth] Merged credentials:', mergedCredentials); // Apply loaded credentials, prioritizing existing values if they are not null/undefined this.accessToken = this.accessToken || mergedCredentials.accessToken; this.refreshToken = this.refreshToken || mergedCredentials.refreshToken;