fix ui bug and optimize session config

This commit is contained in:
musistudio
2025-10-21 21:37:09 +08:00
parent ae1f0f4c31
commit 916b110c3c
2 changed files with 37 additions and 6 deletions

View File

@@ -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<string, string | null>({
max: 1000,
});
export const searchProjectBySession = async (
sessionId: string
): Promise<string | null> => {
// 首先检查缓存
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;
}
};

View File

@@ -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() {
<ProviderList
providers={filteredProviders}
onEdit={handleEditProvider}
onRemove={setDeletingProviderIndex}
onRemove={handleSetDeletingProviderIndex}
/>
</CardContent>