feat(filesystem): add getAgentFilesystemMounts API and integrate with LocalFilesystemBrowser for improved mount management

This commit is contained in:
Anish Sarkar 2026-04-24 05:03:23 +05:30
parent ce71897286
commit a7a758f26e
6 changed files with 77 additions and 5 deletions

View file

@ -55,6 +55,7 @@ export const IPC_CHANNELS = {
ANALYTICS_GET_CONTEXT: 'analytics:get-context', ANALYTICS_GET_CONTEXT: 'analytics:get-context',
// Agent filesystem mode // Agent filesystem mode
AGENT_FILESYSTEM_GET_SETTINGS: 'agent-filesystem:get-settings', AGENT_FILESYSTEM_GET_SETTINGS: 'agent-filesystem:get-settings',
AGENT_FILESYSTEM_GET_MOUNTS: 'agent-filesystem:get-mounts',
AGENT_FILESYSTEM_SET_SETTINGS: 'agent-filesystem:set-settings', AGENT_FILESYSTEM_SET_SETTINGS: 'agent-filesystem:set-settings',
AGENT_FILESYSTEM_PICK_ROOT: 'agent-filesystem:pick-root', AGENT_FILESYSTEM_PICK_ROOT: 'agent-filesystem:pick-root',
} as const; } as const;

View file

@ -39,6 +39,7 @@ import {
import { import {
readAgentLocalFileText, readAgentLocalFileText,
writeAgentLocalFileText, writeAgentLocalFileText,
getAgentFilesystemMounts,
getAgentFilesystemSettings, getAgentFilesystemSettings,
pickAgentFilesystemRoot, pickAgentFilesystemRoot,
setAgentFilesystemSettings, setAgentFilesystemSettings,
@ -226,6 +227,10 @@ export function registerIpcHandlers(): void {
getAgentFilesystemSettings() getAgentFilesystemSettings()
); );
ipcMain.handle(IPC_CHANNELS.AGENT_FILESYSTEM_GET_MOUNTS, () =>
getAgentFilesystemMounts()
);
ipcMain.handle( ipcMain.handle(
IPC_CHANNELS.AGENT_FILESYSTEM_SET_SETTINGS, IPC_CHANNELS.AGENT_FILESYSTEM_SET_SETTINGS,
(_event, settings: { mode?: 'cloud' | 'desktop_local_folder'; localRootPaths?: string[] | null }) => (_event, settings: { mode?: 'cloud' | 'desktop_local_folder'; localRootPaths?: string[] | null }) =>

View file

@ -122,7 +122,7 @@ function toVirtualPath(rootPath: string, absolutePath: string): string {
return `/${rel.replace(/\\/g, "/")}`; return `/${rel.replace(/\\/g, "/")}`;
} }
type LocalRootMount = { export type LocalRootMount = {
mount: string; mount: string;
rootPath: string; rootPath: string;
}; };
@ -145,6 +145,11 @@ function buildRootMounts(rootPaths: string[]): LocalRootMount[] {
return mounts; return mounts;
} }
export async function getAgentFilesystemMounts(): Promise<LocalRootMount[]> {
const rootPaths = await resolveCurrentRootPaths();
return buildRootMounts(rootPaths);
}
function parseMountedVirtualPath(virtualPath: string): { function parseMountedVirtualPath(virtualPath: string): {
mount: string; mount: string;
subPath: string; subPath: string;

View file

@ -108,6 +108,8 @@ contextBridge.exposeInMainWorld('electronAPI', {
// Agent filesystem mode // Agent filesystem mode
getAgentFilesystemSettings: () => getAgentFilesystemSettings: () =>
ipcRenderer.invoke(IPC_CHANNELS.AGENT_FILESYSTEM_GET_SETTINGS), ipcRenderer.invoke(IPC_CHANNELS.AGENT_FILESYSTEM_GET_SETTINGS),
getAgentFilesystemMounts: () =>
ipcRenderer.invoke(IPC_CHANNELS.AGENT_FILESYSTEM_GET_MOUNTS),
setAgentFilesystemSettings: (settings: { setAgentFilesystemSettings: (settings: {
mode?: "cloud" | "desktop_local_folder"; mode?: "cloud" | "desktop_local_folder";
localRootPaths?: string[] | null; localRootPaths?: string[] | null;

View file

@ -34,6 +34,11 @@ interface LocalFolderNode {
files: LocalFolderFileEntry[]; files: LocalFolderFileEntry[];
} }
type LocalRootMount = {
mount: string;
rootPath: string;
};
const getFolderDisplayName = (rootPath: string): string => const getFolderDisplayName = (rootPath: string): string =>
rootPath.split(/[\\/]/).at(-1) || rootPath; rootPath.split(/[\\/]/).at(-1) || rootPath;
@ -50,6 +55,20 @@ function getFileName(pathValue: string): string {
return pathValue.split(/[\\/]/).at(-1) || pathValue; return pathValue.split(/[\\/]/).at(-1) || pathValue;
} }
function toVirtualPath(relativePath: string): string {
const normalized = relativePath.replace(/\\/g, "/").replace(/^\/+/, "");
return `/${normalized}`;
}
function normalizeRootPathForLookup(rootPath: string, isWindows: boolean): string {
const normalized = rootPath.replace(/\\/g, "/").replace(/\/+$/, "");
return isWindows ? normalized.toLowerCase() : normalized;
}
function toMountedVirtualPath(mount: string, relativePath: string): string {
return `/${mount}${toVirtualPath(relativePath)}`;
}
export function LocalFilesystemBrowser({ export function LocalFilesystemBrowser({
rootPaths, rootPaths,
searchSpaceId, searchSpaceId,
@ -59,7 +78,9 @@ export function LocalFilesystemBrowser({
const electronAPI = useElectronAPI(); const electronAPI = useElectronAPI();
const [rootStateMap, setRootStateMap] = useState<Record<string, RootLoadState>>({}); const [rootStateMap, setRootStateMap] = useState<Record<string, RootLoadState>>({});
const [expandedFolderKeys, setExpandedFolderKeys] = useState<Set<string>>(new Set()); const [expandedFolderKeys, setExpandedFolderKeys] = useState<Set<string>>(new Set());
const [mountByRootKey, setMountByRootKey] = useState<Map<string, string>>(new Map());
const supportedExtensions = useMemo(() => Array.from(getSupportedExtensionsSet()), []); const supportedExtensions = useMemo(() => Array.from(getSupportedExtensionsSet()), []);
const isWindowsPlatform = electronAPI?.versions.platform === "win32";
useEffect(() => { useEffect(() => {
if (!electronAPI?.listFolderFiles) return; if (!electronAPI?.listFolderFiles) return;
@ -116,6 +137,31 @@ export function LocalFilesystemBrowser({
}; };
}, [electronAPI, rootPaths, searchSpaceId, supportedExtensions]); }, [electronAPI, rootPaths, searchSpaceId, supportedExtensions]);
useEffect(() => {
if (!electronAPI?.getAgentFilesystemMounts) {
setMountByRootKey(new Map());
return;
}
let cancelled = false;
void electronAPI
.getAgentFilesystemMounts()
.then((mounts: LocalRootMount[]) => {
if (cancelled) return;
const next = new Map<string, string>();
for (const entry of mounts) {
next.set(normalizeRootPathForLookup(entry.rootPath, isWindowsPlatform), entry.mount);
}
setMountByRootKey(next);
})
.catch(() => {
if (cancelled) return;
setMountByRootKey(new Map());
});
return () => {
cancelled = true;
};
}, [electronAPI, isWindowsPlatform, rootPaths]);
const treeByRoot = useMemo(() => { const treeByRoot = useMemo(() => {
const query = searchQuery?.trim().toLowerCase() ?? ""; const query = searchQuery?.trim().toLowerCase() ?? "";
const hasQuery = query.length > 0; const hasQuery = query.length > 0;
@ -160,7 +206,7 @@ export function LocalFilesystemBrowser({
}, []); }, []);
const renderFolder = useCallback( const renderFolder = useCallback(
(folder: LocalFolderNode, depth: number) => { (folder: LocalFolderNode, depth: number, mount: string) => {
const isExpanded = expandedFolderKeys.has(folder.key); const isExpanded = expandedFolderKeys.has(folder.key);
const childFolders = Array.from(folder.folders.values()).sort((a, b) => const childFolders = Array.from(folder.folders.values()).sort((a, b) =>
a.name.localeCompare(b.name) a.name.localeCompare(b.name)
@ -185,12 +231,12 @@ export function LocalFilesystemBrowser({
</button> </button>
{isExpanded && ( {isExpanded && (
<> <>
{childFolders.map((childFolder) => renderFolder(childFolder, depth + 1))} {childFolders.map((childFolder) => renderFolder(childFolder, depth + 1, mount))}
{files.map((file) => ( {files.map((file) => (
<button <button
key={file.fullPath} key={file.fullPath}
type="button" type="button"
onClick={() => onOpenFile(file.fullPath)} onClick={() => onOpenFile(toMountedVirtualPath(mount, file.relativePath))}
className="flex h-8 w-full items-center gap-1.5 rounded-md px-2 text-left text-sm transition-colors hover:bg-muted/60" className="flex h-8 w-full items-center gap-1.5 rounded-md px-2 text-left text-sm transition-colors hover:bg-muted/60"
style={{ paddingInlineStart: `${(depth + 1) * 12 + 22}px` }} style={{ paddingInlineStart: `${(depth + 1) * 12 + 22}px` }}
title={file.fullPath} title={file.fullPath}
@ -223,6 +269,8 @@ export function LocalFilesystemBrowser({
<div className="flex-1 min-h-0 overflow-y-auto px-2 py-2"> <div className="flex-1 min-h-0 overflow-y-auto px-2 py-2">
{treeByRoot.map(({ rootPath, rootNode, matchCount, totalCount }) => { {treeByRoot.map(({ rootPath, rootNode, matchCount, totalCount }) => {
const state = rootStateMap[rootPath]; const state = rootStateMap[rootPath];
const rootKey = normalizeRootPathForLookup(rootPath, isWindowsPlatform);
const mount = mountByRootKey.get(rootKey);
if (!state || state.loading) { if (!state || state.loading) {
return ( return (
<div key={rootPath} className="flex h-16 items-center gap-2 px-3 text-sm text-muted-foreground"> <div key={rootPath} className="flex h-16 items-center gap-2 px-3 text-sm text-muted-foreground">
@ -242,7 +290,12 @@ export function LocalFilesystemBrowser({
const isEmpty = totalCount === 0; const isEmpty = totalCount === 0;
return ( return (
<div key={rootPath} className="mb-1"> <div key={rootPath} className="mb-1">
{renderFolder(rootNode, 0)} {mount ? renderFolder(rootNode, 0, mount) : null}
{!mount && (
<div className="px-3 pb-2 text-xs text-muted-foreground/80">
Unable to resolve mounted root for this folder.
</div>
)}
{isEmpty && ( {isEmpty && (
<div className="px-3 pb-2 text-xs text-muted-foreground/80"> <div className="px-3 pb-2 text-xs text-muted-foreground/80">
No supported files found in this folder. No supported files found in this folder.

View file

@ -49,6 +49,11 @@ interface AgentFilesystemSettings {
updatedAt: string; updatedAt: string;
} }
interface AgentFilesystemMount {
mount: string;
rootPath: string;
}
interface LocalTextFileResult { interface LocalTextFileResult {
ok: boolean; ok: boolean;
path: string; path: string;
@ -147,6 +152,7 @@ interface ElectronAPI {
}>; }>;
// Agent filesystem mode // Agent filesystem mode
getAgentFilesystemSettings: () => Promise<AgentFilesystemSettings>; getAgentFilesystemSettings: () => Promise<AgentFilesystemSettings>;
getAgentFilesystemMounts: () => Promise<AgentFilesystemMount[]>;
setAgentFilesystemSettings: (settings: { setAgentFilesystemSettings: (settings: {
mode?: AgentFilesystemMode; mode?: AgentFilesystemMode;
localRootPaths?: string[] | null; localRootPaths?: string[] | null;