From c74069ba572baeed8d4b302f4f2b008a5daa8f98 Mon Sep 17 00:00:00 2001 From: farion1231 Date: Tue, 5 Aug 2025 20:10:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=8A=B6=E6=80=81=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=8A=9F=E8=83=BD=EF=BC=9A=E6=B8=85=E7=90=86=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BF=9D=E7=95=99UI?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 主要变更 - 移除所有Claude命令调用和进程管理逻辑 - 简化检测函数,暂时返回"功能开发中"状态 - 添加单独检查状态按钮和相关UI交互 - 保留完整的供应商管理功能(添加、编辑、删除、切换) ## 技术优化 - 删除复杂的超时机制、进程监听、错误处理 - 移除axios依赖和HTTP请求相关代码 - 清理竞态条件和队列管理逻辑 - 保持清晰的UI状态管理 ## UI改进 - 添加橙色主题的单独检查按钮 - 增强检查状态的视觉反馈(🔄 进度指示器) - 保留所有状态显示逻辑等待功能重新实现 --- src/main/services.ts | 149 ++++------------------- src/renderer/App.tsx | 45 ++++--- src/renderer/components/ProviderList.css | 21 ++++ src/renderer/components/ProviderList.tsx | 25 +++- 4 files changed, 90 insertions(+), 150 deletions(-) diff --git a/src/main/services.ts b/src/main/services.ts index 1dc46a9..53651fb 100644 --- a/src/main/services.ts +++ b/src/main/services.ts @@ -1,123 +1,17 @@ -import axios from 'axios' import fs from 'fs/promises' import path from 'path' import os from 'os' import { Provider, ProviderStatus } from '../shared/types' -export async function checkProviderStatus(provider: Provider): Promise { - const startTime = Date.now() - - try { - // 方法1: 先检查 Anthropic 官方状态 (适用于 Anthropic API) - if (provider.apiUrl.includes('anthropic.com')) { - try { - const statusResponse = await axios.get('https://status.anthropic.com/api/v2/summary.json', { - timeout: 3000 - }) - - if (statusResponse.data.status?.indicator !== 'none') { - return { - isOnline: false, - responseTime: -1, - lastChecked: new Date(), - error: 'Anthropic 服务当前不可用' - } - } - } catch { - // 状态检查失败,继续尝试直接 API 调用 - } - } - - // 方法2: 轻量级 API 测试请求 - 使用最新的 API 格式 - const testPayload = { - model: provider.model || 'claude-sonnet-4-20250514', - max_tokens: 1, - messages: [ - { - role: 'user', - content: 'test' - } - ] - } - - const response = await axios.post( - `${provider.apiUrl}/v1/messages`, - testPayload, - { - headers: { - 'x-api-key': provider.apiKey, - 'anthropic-version': '2023-06-01', - 'content-type': 'application/json' - }, - timeout: 20000, // 增加超时时间到20秒 - validateStatus: (status) => { - // 200-299 为成功,400-499 通常表示 API 可用但请求有问题(如 key 无效) - return status < 500 - } - } - ) - - const responseTime = Date.now() - startTime - - // 检查响应状态 - if (response.status >= 200 && response.status < 300) { - return { - isOnline: true, - responseTime, - lastChecked: new Date() - } - } else if (response.status >= 400 && response.status < 500) { - // 客户端错误,API 可用但可能是 key 无效或其他认证问题 - return { - isOnline: true, // API 本身是可用的 - responseTime, - lastChecked: new Date(), - error: `API 可用但认证失败 (${response.status}): ${response.data?.error?.message || '请检查 API Key'}` - } - } else { - return { - isOnline: false, - responseTime, - lastChecked: new Date(), - error: `服务器错误 (${response.status})` - } - } - } catch (error) { - const responseTime = Date.now() - startTime - - if (axios.isAxiosError(error)) { - if (error.code === 'ECONNABORTED' || error.code === 'ETIMEDOUT') { - return { - isOnline: false, - responseTime, - lastChecked: new Date(), - error: '请求超时 - 服务可能不可用' - } - } else if (error.response) { - // 服务器响应了错误状态码 - return { - isOnline: false, - responseTime, - lastChecked: new Date(), - error: `HTTP ${error.response.status}: ${error.response.data?.error?.message || error.message}` - } - } else if (error.request) { - // 请求发出但没有收到响应 - return { - isOnline: false, - responseTime, - lastChecked: new Date(), - error: '网络连接失败 - 无法访问服务' - } - } - } - - return { - isOnline: false, - responseTime, - lastChecked: new Date(), - error: error instanceof Error ? error.message : '未知错误' - } +export async function checkProviderStatus( + provider: Provider +): Promise { + // 暂时返回未检查状态 + return { + isOnline: false, + responseTime: -1, + lastChecked: new Date(), + error: '功能开发中' } } @@ -125,17 +19,17 @@ export function getClaudeCodeConfig() { // Claude Code 配置文件路径 const configDir = path.join(os.homedir(), '.claude') const configPath = path.join(configDir, 'settings.json') - + return { path: configPath, dir: configDir } } export async function switchProvider(provider: Provider): Promise { try { const { path: configPath, dir: configDir } = getClaudeCodeConfig() - + // 确保目录存在 await fs.mkdir(configDir, { recursive: true }) - + // 读取现有配置 let config: any = {} try { @@ -144,18 +38,19 @@ export async function switchProvider(provider: Provider): Promise { } catch { // 文件不存在或解析失败,使用空配置 } - - // 更新配置 - config.api = { - ...config.api, - baseURL: provider.apiUrl, - apiKey: provider.apiKey, - model: provider.model + + // 确保 env 对象存在 + if (!config.env) { + config.env = {} } - + + // 更新环境变量配置 + config.env.ANTHROPIC_AUTH_TOKEN = provider.apiKey + config.env.ANTHROPIC_BASE_URL = provider.apiUrl + // 写回配置文件 await fs.writeFile(configPath, JSON.stringify(config, null, 2)) - + return true } catch (error) { console.error('切换供应商失败:', error) diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index ebac781..6f1890e 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -10,7 +10,7 @@ function App() { const [currentProviderId, setCurrentProviderId] = useState('') const [statuses, setStatuses] = useState>({}) const [isAddModalOpen, setIsAddModalOpen] = useState(false) - const [isRefreshing, setIsRefreshing] = useState(false) + const [checkingStatus, setCheckingStatus] = useState>({}) const [configPath, setConfigPath] = useState('') const [editingProviderId, setEditingProviderId] = useState(null) @@ -34,20 +34,30 @@ function App() { } const checkAllStatuses = async () => { - if (Object.keys(providers).length === 0) return + // 功能开发中 + alert('状态检查功能开发中') + } + + const checkSingleStatus = async (providerId: string) => { + const provider = providers[providerId] + if (!provider) return + + setCheckingStatus(prev => ({ ...prev, [providerId]: true })) - setIsRefreshing(true) - const newStatuses: Record = {} - - await Promise.all( - Object.values(providers).map(async (provider) => { - const status = await window.electronAPI.checkStatus(provider) - newStatuses[provider.id] = status - }) - ) - - setStatuses(newStatuses) - setIsRefreshing(false) + try { + // 暂时显示开发中状态 + const status: ProviderStatus = { + isOnline: false, + responseTime: -1, + lastChecked: new Date(), + error: '功能开发中' + } + setStatuses(prev => ({ ...prev, [providerId]: status })) + } catch (error) { + console.error('检查状态失败:', error) + } finally { + setCheckingStatus(prev => ({ ...prev, [providerId]: false })) + } } const handleAddProvider = async (provider: Omit) => { @@ -96,10 +106,9 @@ function App() {