From 916b110c3ccc8e95f523c3b012ea81816bd0d82d Mon Sep 17 00:00:00 2001 From: musistudio Date: Tue, 21 Oct 2025 21:37:09 +0800 Subject: [PATCH] fix ui bug and optimize session config --- src/utils/router.ts | 21 +++++++++++++++++++++ ui/src/components/Providers.tsx | 22 ++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/utils/router.ts b/src/utils/router.ts index 42ff80d..119daa7 100644 --- a/src/utils/router.ts +++ b/src/utils/router.ts @@ -9,6 +9,7 @@ import { readFile, access } from "fs/promises"; import { opendir, stat } from "fs/promises"; import { join } from "path"; import { CLAUDE_PROJECTS_DIR, HOME_DIR } from "../constants"; +import { LRUCache } from "lru-cache"; const enc = get_encoding("cl100k_base"); @@ -228,9 +229,23 @@ export const router = async (req: any, _res: any, context: any) => { return; }; +// 内存缓存,存储sessionId到项目名称的映射 +// null值表示之前已查找过但未找到项目 +// 使用LRU缓存,限制最大1000个条目 +const sessionProjectCache = new LRUCache({ + max: 1000, +}); + export const searchProjectBySession = async ( sessionId: string ): Promise => { + // 首先检查缓存 + if (sessionProjectCache.has(sessionId)) { + return sessionProjectCache.get(sessionId)!; + } + console.log('读取项目配置') + + try { const dir = await opendir(CLAUDE_PROJECTS_DIR); const folderNames: string[] = []; @@ -263,13 +278,19 @@ export const searchProjectBySession = async ( // 返回第一个存在的项目目录名称 for (const result of results) { if (result) { + // 缓存找到的结果 + sessionProjectCache.set(sessionId, result); return result; } } + // 缓存未找到的结果(null值表示之前已查找过但未找到项目) + sessionProjectCache.set(sessionId, null); return null; // 没有找到匹配的项目 } catch (error) { console.error("Error searching for project by session:", error); + // 出错时也缓存null结果,避免重复出错 + sessionProjectCache.set(sessionId, null); return null; } }; diff --git a/ui/src/components/Providers.tsx b/ui/src/components/Providers.tsx index fadf0a2..4bb51b8 100644 --- a/ui/src/components/Providers.tsx +++ b/ui/src/components/Providers.tsx @@ -106,14 +106,16 @@ export function Providers() { }; const handleEditProvider = (index: number) => { - const provider = config.Providers[index]; - setEditingProviderIndex(index); + // Find the actual index in the original providers array + const actualIndex = validProviders.indexOf(filteredProviders[index]); + const provider = config.Providers[actualIndex]; + setEditingProviderIndex(actualIndex); setEditingProviderData(JSON.parse(JSON.stringify(provider))); // 深拷贝 setIsNewProvider(false); // Reset API key visibility and error when opening edit dialog setShowApiKey(prev => ({ ...prev, - [index]: false + [actualIndex]: false })); setApiKeyError(null); setNameError(null); @@ -197,9 +199,17 @@ export function Providers() { setNameError(null); }; - const handleRemoveProvider = (index: number) => { + // Handle deletion by setting the correct index in the state + const handleSetDeletingProviderIndex = (filteredIndex: number) => { + setDeletingProviderIndex(filteredIndex); + }; + + // Handle deletion by passing the filtered index to get the actual index in the original array + const handleRemoveProvider = (filteredIndex: number) => { + // Find the actual index in the original providers array + const actualIndex = validProviders.indexOf(filteredProviders[filteredIndex]); const newProviders = [...config.Providers]; - newProviders.splice(index, 1); + newProviders.splice(actualIndex, 1); setConfig({ ...config, Providers: newProviders }); setDeletingProviderIndex(null); }; @@ -540,7 +550,7 @@ export function Providers() {