mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-21 18:55:16 +02:00
feat(filesystem): increase max local roots to 10, optimize path normalization, and implement caching for filesystem settings
This commit is contained in:
parent
95511f0915
commit
6aa172a730
1 changed files with 32 additions and 9 deletions
|
|
@ -11,7 +11,8 @@ export interface AgentFilesystemSettings {
|
||||||
}
|
}
|
||||||
|
|
||||||
const SETTINGS_FILENAME = "agent-filesystem-settings.json";
|
const SETTINGS_FILENAME = "agent-filesystem-settings.json";
|
||||||
const MAX_LOCAL_ROOTS = 5;
|
const MAX_LOCAL_ROOTS = 10;
|
||||||
|
let cachedSettings: AgentFilesystemSettings | null = null;
|
||||||
|
|
||||||
function getSettingsPath(): string {
|
function getSettingsPath(): string {
|
||||||
return join(app.getPath("userData"), SETTINGS_FILENAME);
|
return join(app.getPath("userData"), SETTINGS_FILENAME);
|
||||||
|
|
@ -34,7 +35,7 @@ async function canonicalizeRootPath(pathValue: string): Promise<string> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function normalizeLocalRootPaths(paths: unknown): Promise<string[]> {
|
function normalizeLocalRootPaths(paths: unknown): string[] {
|
||||||
if (!Array.isArray(paths)) {
|
if (!Array.isArray(paths)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
@ -43,8 +44,22 @@ async function normalizeLocalRootPaths(paths: unknown): Promise<string[]> {
|
||||||
if (typeof rawPath !== "string") continue;
|
if (typeof rawPath !== "string") continue;
|
||||||
const trimmed = rawPath.trim();
|
const trimmed = rawPath.trim();
|
||||||
if (!trimmed) continue;
|
if (!trimmed) continue;
|
||||||
const canonicalRootPath = await canonicalizeRootPath(trimmed);
|
uniquePaths.add(trimmed);
|
||||||
uniquePaths.add(canonicalRootPath);
|
if (uniquePaths.size >= MAX_LOCAL_ROOTS) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [...uniquePaths];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function normalizeLocalRootPathsCanonical(paths: unknown): Promise<string[]> {
|
||||||
|
const normalizedPaths = normalizeLocalRootPaths(paths);
|
||||||
|
const canonicalizedPaths = await Promise.all(
|
||||||
|
normalizedPaths.map((pathValue) => canonicalizeRootPath(pathValue))
|
||||||
|
);
|
||||||
|
const uniquePaths = new Set<string>();
|
||||||
|
for (const canonicalPath of canonicalizedPaths) {
|
||||||
|
uniquePaths.add(canonicalPath);
|
||||||
if (uniquePaths.size >= MAX_LOCAL_ROOTS) {
|
if (uniquePaths.size >= MAX_LOCAL_ROOTS) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -53,19 +68,26 @@ async function normalizeLocalRootPaths(paths: unknown): Promise<string[]> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getAgentFilesystemSettings(): Promise<AgentFilesystemSettings> {
|
export async function getAgentFilesystemSettings(): Promise<AgentFilesystemSettings> {
|
||||||
|
if (cachedSettings) {
|
||||||
|
return cachedSettings;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const raw = await readFile(getSettingsPath(), "utf8");
|
const raw = await readFile(getSettingsPath(), "utf8");
|
||||||
const parsed = JSON.parse(raw) as Partial<AgentFilesystemSettings>;
|
const parsed = JSON.parse(raw) as Partial<AgentFilesystemSettings>;
|
||||||
if (parsed.mode !== "cloud" && parsed.mode !== "desktop_local_folder") {
|
if (parsed.mode !== "cloud" && parsed.mode !== "desktop_local_folder") {
|
||||||
return getDefaultSettings();
|
cachedSettings = getDefaultSettings();
|
||||||
|
return cachedSettings;
|
||||||
}
|
}
|
||||||
return {
|
cachedSettings = {
|
||||||
mode: parsed.mode,
|
mode: parsed.mode,
|
||||||
localRootPaths: await normalizeLocalRootPaths(parsed.localRootPaths),
|
// Avoid filesystem I/O during reads; canonicalize paths on write.
|
||||||
|
localRootPaths: normalizeLocalRootPaths(parsed.localRootPaths),
|
||||||
updatedAt: parsed.updatedAt ?? new Date().toISOString(),
|
updatedAt: parsed.updatedAt ?? new Date().toISOString(),
|
||||||
};
|
};
|
||||||
|
return cachedSettings;
|
||||||
} catch {
|
} catch {
|
||||||
return getDefaultSettings();
|
cachedSettings = getDefaultSettings();
|
||||||
|
return cachedSettings;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,13 +107,14 @@ export async function setAgentFilesystemSettings(
|
||||||
localRootPaths:
|
localRootPaths:
|
||||||
settings.localRootPaths === undefined
|
settings.localRootPaths === undefined
|
||||||
? current.localRootPaths
|
? current.localRootPaths
|
||||||
: await normalizeLocalRootPaths(settings.localRootPaths ?? []),
|
: await normalizeLocalRootPathsCanonical(settings.localRootPaths ?? []),
|
||||||
updatedAt: new Date().toISOString(),
|
updatedAt: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const settingsPath = getSettingsPath();
|
const settingsPath = getSettingsPath();
|
||||||
await mkdir(dirname(settingsPath), { recursive: true });
|
await mkdir(dirname(settingsPath), { recursive: true });
|
||||||
await writeFile(settingsPath, JSON.stringify(next, null, 2), "utf8");
|
await writeFile(settingsPath, JSON.stringify(next, null, 2), "utf8");
|
||||||
|
cachedSettings = next;
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue