fix: openrouter auto load api_key
Browse files
src/services/telegram/interactiveCommands.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import type { AppStateStore } from '../../state/AppState.js'
|
| 2 |
-
import { saveGlobalConfig } from '../../utils/config.js'
|
| 3 |
import { logForDebugging } from '../../utils/debug.js'
|
| 4 |
import { isBilledAsExtraUsage } from '../../utils/extraUsage.js'
|
| 5 |
import {
|
|
@@ -29,6 +29,7 @@ import type {
|
|
| 29 |
TelegramInlineKeyboardButton,
|
| 30 |
TelegramInlineKeyboardMarkup,
|
| 31 |
} from './telegramTypes.js'
|
|
|
|
| 32 |
|
| 33 |
const COPILOT_CLIENT_ID = 'Ov23li8tweQw6odWQebz'
|
| 34 |
const COPILOT_DEVICE_CODE_URL = 'https://github.com/login/device/code'
|
|
@@ -654,10 +655,43 @@ export async function handleTelegramCallback(
|
|
| 654 |
const providerId = event.data.split(':').at(-1)
|
| 655 |
await telegramService.answerCallbackQuery(event.callbackQueryId)
|
| 656 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 657 |
const providerMessages: Record<string, string> = {
|
| 658 |
'anthropic': 'Anthropic 登录需要在本地终端执行 /login 命令,Telegram 暂不支持浏览器 OAuth 流程。',
|
| 659 |
'openai': 'OpenAI 登录需要在本地终端执行 /login 命令,Telegram 暂不支持交互式登录。',
|
| 660 |
-
'openrouter': 'OpenRouter 登录需要在本地终端执行 /login 命令,Telegram 暂不支持 API Key 输入。',
|
| 661 |
'local': '本地模型配置需要在本地终端执行 /login 命令。',
|
| 662 |
}
|
| 663 |
|
|
|
|
| 1 |
import type { AppStateStore } from '../../state/AppState.js'
|
| 2 |
+
import { saveGlobalConfig, getGlobalConfig } from '../../utils/config.js'
|
| 3 |
import { logForDebugging } from '../../utils/debug.js'
|
| 4 |
import { isBilledAsExtraUsage } from '../../utils/extraUsage.js'
|
| 5 |
import {
|
|
|
|
| 29 |
TelegramInlineKeyboardButton,
|
| 30 |
TelegramInlineKeyboardMarkup,
|
| 31 |
} from './telegramTypes.js'
|
| 32 |
+
import { getOpenRouterApiKey } from '../../utils/auth.js'
|
| 33 |
|
| 34 |
const COPILOT_CLIENT_ID = 'Ov23li8tweQw6odWQebz'
|
| 35 |
const COPILOT_DEVICE_CODE_URL = 'https://github.com/login/device/code'
|
|
|
|
| 655 |
const providerId = event.data.split(':').at(-1)
|
| 656 |
await telegramService.answerCallbackQuery(event.callbackQueryId)
|
| 657 |
|
| 658 |
+
if (providerId === 'openrouter') {
|
| 659 |
+
// 检查是否已有 OpenRouter API Key
|
| 660 |
+
const existingApiKey = getOpenRouterApiKey()
|
| 661 |
+
if (existingApiKey) {
|
| 662 |
+
try {
|
| 663 |
+
// 保存配置并切换到 OpenRouter
|
| 664 |
+
saveGlobalConfig(current => ({
|
| 665 |
+
...current,
|
| 666 |
+
authProvider: 'openrouter',
|
| 667 |
+
openRouterApiKey: existingApiKey,
|
| 668 |
+
}))
|
| 669 |
+
|
| 670 |
+
// 清除 provider 缓存
|
| 671 |
+
const { clearStoredProviderCache } = await import('../../utils/model/providers.js')
|
| 672 |
+
clearStoredProviderCache()
|
| 673 |
+
|
| 674 |
+
await telegramService.editMessage(
|
| 675 |
+
event.chatId,
|
| 676 |
+
event.messageId,
|
| 677 |
+
`✅ 已切换到 OpenRouter (使用已配置的 API Key)\n\n当前状态:已登录 OpenRouter`,
|
| 678 |
+
)
|
| 679 |
+
return true
|
| 680 |
+
} catch (error) {
|
| 681 |
+
await telegramService.editMessage(
|
| 682 |
+
event.chatId,
|
| 683 |
+
event.messageId,
|
| 684 |
+
`❌ 切换到 OpenRouter 失败: ${error instanceof Error ? error.message : String(error)}`,
|
| 685 |
+
)
|
| 686 |
+
return true
|
| 687 |
+
}
|
| 688 |
+
}
|
| 689 |
+
}
|
| 690 |
+
|
| 691 |
const providerMessages: Record<string, string> = {
|
| 692 |
'anthropic': 'Anthropic 登录需要在本地终端执行 /login 命令,Telegram 暂不支持浏览器 OAuth 流程。',
|
| 693 |
'openai': 'OpenAI 登录需要在本地终端执行 /login 命令,Telegram 暂不支持交互式登录。',
|
| 694 |
+
'openrouter': 'OpenRouter 登录需要在本地终端执行 /login 命令或配置环境变量 OPENROUTER_API_KEY,Telegram 暂不支持 API Key 输入。',
|
| 695 |
'local': '本地模型配置需要在本地终端执行 /login 命令。',
|
| 696 |
}
|
| 697 |
|