Skip to content

Commit e7e258a

Browse files
committed
fix: improve OpenRouter API key callback handling with better error handling and user feedback
- Add comprehensive logging to OpenRouter callback process for debugging - Show success/error messages to users when API key exchange completes - Ensure webview state updates after successful API key configuration - Add error handling for missing authorization codes in URI callback - Improve URI handler logging to help diagnose callback issues Fixes #6467
1 parent 01f5320 commit e7e258a

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/activate/handleUri.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ export const handleUri = async (uri: vscode.Uri) => {
99
const query = new URLSearchParams(uri.query.replace(/\+/g, "%2B"))
1010
const visibleProvider = ClineProvider.getVisibleInstance()
1111

12+
console.log(`[URI Handler] Received URI: ${uri.toString()}`)
13+
console.log(`[URI Handler] Path: ${path}`)
14+
console.log(`[URI Handler] Query params:`, Object.fromEntries(query.entries()))
15+
1216
if (!visibleProvider) {
17+
console.error(`[URI Handler] No visible provider found`)
1318
return
1419
}
1520

@@ -24,7 +29,17 @@ export const handleUri = async (uri: vscode.Uri) => {
2429
case "/openrouter": {
2530
const code = query.get("code")
2631
if (code) {
27-
await visibleProvider.handleOpenRouterCallback(code)
32+
try {
33+
await visibleProvider.handleOpenRouterCallback(code)
34+
} catch (error) {
35+
console.error(`[URI Handler] Failed to handle OpenRouter callback:`, error)
36+
// Error is already shown to user in handleOpenRouterCallback
37+
}
38+
} else {
39+
console.error(`[URI Handler] OpenRouter callback received without code parameter`)
40+
vscode.window.showErrorMessage(
41+
"OpenRouter authorization failed: No authorization code received. Please try again.",
42+
)
2843
}
2944
break
3045
}

src/core/webview/ClineProvider.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,22 +1109,33 @@ export class ClineProvider
11091109
// OpenRouter
11101110

11111111
async handleOpenRouterCallback(code: string) {
1112+
this.log(`[OpenRouter] Handling callback with code: ${code.substring(0, 10)}...`)
1113+
11121114
let { apiConfiguration, currentApiConfigName } = await this.getState()
11131115

11141116
let apiKey: string
11151117
try {
11161118
const baseUrl = apiConfiguration.openRouterBaseUrl || "https://openrouter.ai/api/v1"
11171119
// Extract the base domain for the auth endpoint
11181120
const baseUrlDomain = baseUrl.match(/^(https?:\/\/[^\/]+)/)?.[1] || "https://openrouter.ai"
1121+
1122+
this.log(`[OpenRouter] Exchanging code for API key at: ${baseUrlDomain}/api/v1/auth/keys`)
11191123
const response = await axios.post(`${baseUrlDomain}/api/v1/auth/keys`, { code })
1124+
11201125
if (response.data && response.data.key) {
11211126
apiKey = response.data.key
1127+
this.log(`[OpenRouter] Successfully received API key: ${apiKey.substring(0, 10)}...`)
11221128
} else {
11231129
throw new Error("Invalid response from OpenRouter API")
11241130
}
11251131
} catch (error) {
11261132
this.log(
1127-
`Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
1133+
`[OpenRouter] Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
1134+
)
1135+
1136+
// Show user-friendly error message
1137+
vscode.window.showErrorMessage(
1138+
`Failed to get OpenRouter API key: ${error instanceof Error ? error.message : "Unknown error"}. Please try again.`,
11281139
)
11291140
throw error
11301141
}
@@ -1136,7 +1147,26 @@ export class ClineProvider
11361147
openRouterModelId: apiConfiguration?.openRouterModelId || openRouterDefaultModelId,
11371148
}
11381149

1139-
await this.upsertProviderProfile(currentApiConfigName, newConfiguration)
1150+
try {
1151+
await this.upsertProviderProfile(currentApiConfigName, newConfiguration)
1152+
this.log(`[OpenRouter] Successfully updated provider profile with new API key`)
1153+
1154+
// Show success message to user
1155+
vscode.window.showInformationMessage("OpenRouter API key has been successfully configured!")
1156+
1157+
// Ensure the webview is updated with the new state
1158+
await this.postStateToWebview()
1159+
} catch (error) {
1160+
this.log(
1161+
`[OpenRouter] Error updating provider profile: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
1162+
)
1163+
1164+
// Show user-friendly error message
1165+
vscode.window.showErrorMessage(
1166+
`Failed to save OpenRouter API key: ${error instanceof Error ? error.message : "Unknown error"}. Please try again.`,
1167+
)
1168+
throw error
1169+
}
11401170
}
11411171

11421172
// Glama

0 commit comments

Comments
 (0)