fix: use telegram /login local to change to local model
Browse files
src/services/telegram/interactiveCommands.ts
CHANGED
|
@@ -29,7 +29,11 @@ import type {
|
|
| 29 |
TelegramInlineKeyboardButton,
|
| 30 |
TelegramInlineKeyboardMarkup,
|
| 31 |
} from './telegramTypes.js'
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
const COPILOT_CLIENT_ID = 'Ov23li8tweQw6odWQebz'
|
| 35 |
const COPILOT_DEVICE_CODE_URL = 'https://github.com/login/device/code'
|
|
@@ -657,7 +661,11 @@ export async function handleTelegramCallback(
|
|
| 657 |
|
| 658 |
if (providerId === 'openrouter') {
|
| 659 |
// 检查是否已有 OpenRouter API Key
|
| 660 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
if (existingApiKey) {
|
| 662 |
try {
|
| 663 |
// 保存配置并切换到 OpenRouter
|
|
@@ -688,11 +696,50 @@ export async function handleTelegramCallback(
|
|
| 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 |
|
| 698 |
await telegramService.editMessage(
|
|
|
|
| 29 |
TelegramInlineKeyboardButton,
|
| 30 |
TelegramInlineKeyboardMarkup,
|
| 31 |
} from './telegramTypes.js'
|
| 32 |
+
|
| 33 |
+
// 辅助函数:记录 Telegram 相关日志
|
| 34 |
+
function logTelegramDebug(message: string, level: 'debug' | 'error' | 'info' = 'debug'): void {
|
| 35 |
+
logForDebugging(`[telegram] ${message}`, { level })
|
| 36 |
+
}
|
| 37 |
|
| 38 |
const COPILOT_CLIENT_ID = 'Ov23li8tweQw6odWQebz'
|
| 39 |
const COPILOT_DEVICE_CODE_URL = 'https://github.com/login/device/code'
|
|
|
|
| 661 |
|
| 662 |
if (providerId === 'openrouter') {
|
| 663 |
// 检查是否已有 OpenRouter API Key
|
| 664 |
+
const config = getGlobalConfig()
|
| 665 |
+
const existingApiKey = config.openRouterApiKey || process.env.OPENROUTER_API_KEY
|
| 666 |
+
|
| 667 |
+
logTelegramDebug(`[telegram] checking openrouter config: hasApiKeyInConfig=${!!config.openRouterApiKey}, hasApiKeyInEnv=${!!process.env.OPENROUTER_API_KEY}`)
|
| 668 |
+
|
| 669 |
if (existingApiKey) {
|
| 670 |
try {
|
| 671 |
// 保存配置并切换到 OpenRouter
|
|
|
|
| 696 |
}
|
| 697 |
}
|
| 698 |
|
| 699 |
+
if (providerId === 'local') {
|
| 700 |
+
// 检查是否已有本地模型配置
|
| 701 |
+
const config = getGlobalConfig()
|
| 702 |
+
const localBaseUrl = config.localBaseUrl
|
| 703 |
+
const localModelName = config.localModelName
|
| 704 |
+
|
| 705 |
+
logTelegramDebug(`[telegram] checking local config: baseUrl=${localBaseUrl}, modelName=${localModelName}, fullConfig=${JSON.stringify(config)}`)
|
| 706 |
+
|
| 707 |
+
if (localBaseUrl && localModelName) {
|
| 708 |
+
try {
|
| 709 |
+
// 保存配置并切换到本地模型
|
| 710 |
+
saveGlobalConfig(current => ({
|
| 711 |
+
...current,
|
| 712 |
+
authProvider: 'local',
|
| 713 |
+
localBaseUrl: localBaseUrl,
|
| 714 |
+
localModelName: localModelName,
|
| 715 |
+
}))
|
| 716 |
+
|
| 717 |
+
// 清除 provider 缓存
|
| 718 |
+
const { clearStoredProviderCache } = await import('../../utils/model/providers.js')
|
| 719 |
+
clearStoredProviderCache()
|
| 720 |
+
|
| 721 |
+
await telegramService.editMessage(
|
| 722 |
+
event.chatId,
|
| 723 |
+
event.messageId,
|
| 724 |
+
`✅ 已切换到本地模型\n\nBase URL: ${localBaseUrl}\n模型名称: ${localModelName}`,
|
| 725 |
+
)
|
| 726 |
+
return true
|
| 727 |
+
} catch (error) {
|
| 728 |
+
await telegramService.editMessage(
|
| 729 |
+
event.chatId,
|
| 730 |
+
event.messageId,
|
| 731 |
+
`❌ 切换到本地模型失败: ${error instanceof Error ? error.message : String(error)}`,
|
| 732 |
+
)
|
| 733 |
+
return true
|
| 734 |
+
}
|
| 735 |
+
}
|
| 736 |
+
}
|
| 737 |
+
|
| 738 |
const providerMessages: Record<string, string> = {
|
| 739 |
'anthropic': 'Anthropic 登录需要在本地终端执行 /login 命令,Telegram 暂不支持浏览器 OAuth 流程。',
|
| 740 |
'openai': 'OpenAI 登录需要在本地终端执行 /login 命令,Telegram 暂不支持交互式登录。',
|
| 741 |
'openrouter': 'OpenRouter 登录需要在本地终端执行 /login 命令或配置环境变量 OPENROUTER_API_KEY,Telegram 暂不支持 API Key 输入。',
|
| 742 |
+
'local': '本地模型配置需要在本地终端执行 /login 命令,配置 Base URL 和模型名称。',
|
| 743 |
}
|
| 744 |
|
| 745 |
await telegramService.editMessage(
|