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
This commit is contained in:
Roo Code 2025-07-31 03:06:00 +00:00
parent 01f5320b4d
commit e7e258ad24
2 changed files with 48 additions and 3 deletions

View file

@ -9,7 +9,12 @@ export const handleUri = async (uri: vscode.Uri) => {
const query = new URLSearchParams(uri.query.replace(/\+/g, "%2B"))
const visibleProvider = ClineProvider.getVisibleInstance()
console.log(`[URI Handler] Received URI: ${uri.toString()}`)
console.log(`[URI Handler] Path: ${path}`)
console.log(`[URI Handler] Query params:`, Object.fromEntries(query.entries()))
if (!visibleProvider) {
console.error(`[URI Handler] No visible provider found`)
return
}
@ -24,7 +29,17 @@ export const handleUri = async (uri: vscode.Uri) => {
case "/openrouter": {
const code = query.get("code")
if (code) {
await visibleProvider.handleOpenRouterCallback(code)
try {
await visibleProvider.handleOpenRouterCallback(code)
} catch (error) {
console.error(`[URI Handler] Failed to handle OpenRouter callback:`, error)
// Error is already shown to user in handleOpenRouterCallback
}
} else {
console.error(`[URI Handler] OpenRouter callback received without code parameter`)
vscode.window.showErrorMessage(
"OpenRouter authorization failed: No authorization code received. Please try again.",
)
}
break
}

View file

@ -1109,6 +1109,8 @@ export class ClineProvider
// OpenRouter
async handleOpenRouterCallback(code: string) {
this.log(`[OpenRouter] Handling callback with code: ${code.substring(0, 10)}...`)
let { apiConfiguration, currentApiConfigName } = await this.getState()
let apiKey: string
@ -1116,15 +1118,24 @@ export class ClineProvider
const baseUrl = apiConfiguration.openRouterBaseUrl || "https://openrouter.ai/api/v1"
// Extract the base domain for the auth endpoint
const baseUrlDomain = baseUrl.match(/^(https?:\/\/[^\/]+)/)?.[1] || "https://openrouter.ai"
this.log(`[OpenRouter] Exchanging code for API key at: ${baseUrlDomain}/api/v1/auth/keys`)
const response = await axios.post(`${baseUrlDomain}/api/v1/auth/keys`, { code })
if (response.data && response.data.key) {
apiKey = response.data.key
this.log(`[OpenRouter] Successfully received API key: ${apiKey.substring(0, 10)}...`)
} else {
throw new Error("Invalid response from OpenRouter API")
}
} catch (error) {
this.log(
`Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
`[OpenRouter] Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
)
// Show user-friendly error message
vscode.window.showErrorMessage(
`Failed to get OpenRouter API key: ${error instanceof Error ? error.message : "Unknown error"}. Please try again.`,
)
throw error
}
@ -1136,7 +1147,26 @@ export class ClineProvider
openRouterModelId: apiConfiguration?.openRouterModelId || openRouterDefaultModelId,
}
await this.upsertProviderProfile(currentApiConfigName, newConfiguration)
try {
await this.upsertProviderProfile(currentApiConfigName, newConfiguration)
this.log(`[OpenRouter] Successfully updated provider profile with new API key`)
// Show success message to user
vscode.window.showInformationMessage("OpenRouter API key has been successfully configured!")
// Ensure the webview is updated with the new state
await this.postStateToWebview()
} catch (error) {
this.log(
`[OpenRouter] Error updating provider profile: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
)
// Show user-friendly error message
vscode.window.showErrorMessage(
`Failed to save OpenRouter API key: ${error instanceof Error ? error.message : "Unknown error"}. Please try again.`,
)
throw error
}
}
// Glama