2026-01-07 12:08:53 +02:00
|
|
|
import type {
|
|
|
|
|
CommandExecResult,
|
|
|
|
|
DirectoryListResult,
|
|
|
|
|
FileSearchQuery,
|
|
|
|
|
FileSearchResult,
|
|
|
|
|
FilesAPI,
|
|
|
|
|
} from '@openchamber/ui/lib/api/types';
|
2025-12-13 16:34:17 +02:00
|
|
|
|
2026-01-07 12:08:53 +02:00
|
|
|
import { sendBridgeMessage, sendBridgeMessageWithOptions } from './bridge';
|
|
|
|
|
|
|
|
|
|
const normalizePath = (value: string): string => value.replace(/\\/g, '/');
|
2025-12-13 16:34:17 +02:00
|
|
|
|
|
|
|
|
export const createVSCodeFilesAPI = (): FilesAPI => ({
|
2026-01-20 20:20:17 +07:00
|
|
|
async listDirectory(path: string, options?: { respectGitignore?: boolean }): Promise<DirectoryListResult> {
|
2025-12-13 16:34:17 +02:00
|
|
|
const target = normalizePath(path);
|
2026-01-07 12:08:53 +02:00
|
|
|
const data = await sendBridgeMessage<{
|
|
|
|
|
directory?: string;
|
|
|
|
|
path?: string;
|
|
|
|
|
entries: Array<{ name: string; path: string; isDirectory: boolean }>;
|
2026-01-20 20:20:17 +07:00
|
|
|
}>('api:fs:list', {
|
|
|
|
|
path: target,
|
|
|
|
|
respectGitignore: options?.respectGitignore,
|
|
|
|
|
});
|
2025-12-13 16:34:17 +02:00
|
|
|
|
2026-01-07 12:08:53 +02:00
|
|
|
const directory = normalizePath(data?.directory || data?.path || target);
|
|
|
|
|
const entries = Array.isArray(data?.entries) ? data.entries : [];
|
|
|
|
|
return {
|
|
|
|
|
directory,
|
|
|
|
|
entries: entries.map((entry) => ({
|
|
|
|
|
name: entry.name,
|
|
|
|
|
path: normalizePath(entry.path),
|
|
|
|
|
isDirectory: Boolean(entry.isDirectory),
|
|
|
|
|
})),
|
|
|
|
|
};
|
2025-12-13 16:34:17 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async search(payload: FileSearchQuery): Promise<FileSearchResult[]> {
|
2026-03-04 01:41:01 +02:00
|
|
|
const directory = normalizePath(payload.directory);
|
2026-06-02 00:43:05 +03:00
|
|
|
const data = await sendBridgeMessage<{
|
|
|
|
|
files?: Array<{ path?: string; relativePath?: string }>;
|
|
|
|
|
}>('api:fs:search', {
|
|
|
|
|
directory,
|
|
|
|
|
query: payload.query,
|
|
|
|
|
limit: payload.maxResults,
|
|
|
|
|
includeHidden: false,
|
|
|
|
|
respectGitignore: true,
|
|
|
|
|
});
|
2026-03-04 01:41:01 +02:00
|
|
|
|
2026-06-02 00:43:05 +03:00
|
|
|
const files = Array.isArray(data?.files) ? data.files : [];
|
2025-12-13 16:34:17 +02:00
|
|
|
|
2026-06-02 00:43:05 +03:00
|
|
|
return files.map((file) => {
|
|
|
|
|
const relativePath = typeof file.relativePath === 'string'
|
|
|
|
|
? normalizePath(file.relativePath)
|
|
|
|
|
: normalizePath(file.path || '');
|
|
|
|
|
const absolutePath = typeof file.path === 'string'
|
|
|
|
|
? normalizePath(file.path)
|
|
|
|
|
: normalizePath(`${directory}/${relativePath}`);
|
|
|
|
|
return {
|
|
|
|
|
path: absolutePath,
|
|
|
|
|
preview: [relativePath || absolutePath],
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-12-13 16:34:17 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async createDirectory(path: string): Promise<{ success: boolean; path: string }> {
|
|
|
|
|
const target = normalizePath(path);
|
2026-01-07 12:08:53 +02:00
|
|
|
const data = await sendBridgeMessage<{ success: boolean; path: string }>('api:fs:mkdir', { path: target });
|
|
|
|
|
return {
|
|
|
|
|
success: Boolean(data?.success),
|
|
|
|
|
path: typeof data?.path === 'string' ? normalizePath(data.path) : target,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-21 23:04:24 +08:00
|
|
|
async statFile(path: string): Promise<{ path: string; isFile: boolean; size: number; mtimeMs?: number }> {
|
2026-04-01 15:18:05 +08:00
|
|
|
const target = normalizePath(path);
|
2026-04-21 23:04:24 +08:00
|
|
|
const data = await sendBridgeMessage<{ path?: string; isFile?: boolean; size?: number; mtimeMs?: number }>('api:fs:stat', { path: target });
|
2026-04-01 15:18:05 +08:00
|
|
|
return {
|
|
|
|
|
path: typeof data?.path === 'string' ? normalizePath(data.path) : target,
|
|
|
|
|
isFile: Boolean(data?.isFile),
|
|
|
|
|
size: typeof data?.size === 'number' ? data.size : 0,
|
2026-04-21 23:04:24 +08:00
|
|
|
mtimeMs: typeof data?.mtimeMs === 'number' ? data.mtimeMs : undefined,
|
2026-04-01 15:18:05 +08:00
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2026-01-22 12:21:19 +02:00
|
|
|
async delete(path: string): Promise<{ success: boolean }> {
|
|
|
|
|
const target = normalizePath(path);
|
|
|
|
|
const data = await sendBridgeMessage<{ success: boolean }>('api:fs:delete', { path: target });
|
|
|
|
|
return { success: Boolean(data?.success) };
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async rename(oldPath: string, newPath: string): Promise<{ success: boolean; path: string }> {
|
|
|
|
|
const data = await sendBridgeMessage<{ success: boolean; path: string }>('api:fs:rename', { oldPath, newPath });
|
|
|
|
|
return {
|
|
|
|
|
success: Boolean(data?.success),
|
|
|
|
|
path: typeof data?.path === 'string' ? normalizePath(data.path) : newPath,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2026-01-07 12:08:53 +02:00
|
|
|
async readFile(path: string): Promise<{ content: string; path: string }> {
|
|
|
|
|
const target = normalizePath(path);
|
|
|
|
|
const data = await sendBridgeMessage<{ content: string; path: string }>('api:fs:read', { path: target });
|
|
|
|
|
return {
|
|
|
|
|
content: typeof data?.content === 'string' ? data.content : '',
|
|
|
|
|
path: typeof data?.path === 'string' ? normalizePath(data.path) : target,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async writeFile(path: string, content: string): Promise<{ success: boolean; path: string }> {
|
|
|
|
|
const target = normalizePath(path);
|
|
|
|
|
const data = await sendBridgeMessage<{ success: boolean; path: string }>('api:fs:write', { path: target, content });
|
|
|
|
|
return {
|
|
|
|
|
success: Boolean(data?.success),
|
|
|
|
|
path: typeof data?.path === 'string' ? normalizePath(data.path) : target,
|
|
|
|
|
};
|
|
|
|
|
},
|
2025-12-13 16:34:17 +02:00
|
|
|
|
2026-04-17 18:46:13 +03:00
|
|
|
async revealPath(path: string): Promise<{ success: boolean }> {
|
|
|
|
|
const target = normalizePath(path);
|
|
|
|
|
const data = await sendBridgeMessage<{ success?: boolean }>('api:fs:reveal', { path: target });
|
|
|
|
|
return { success: Boolean(data?.success) };
|
|
|
|
|
},
|
|
|
|
|
|
2026-01-07 12:08:53 +02:00
|
|
|
async execCommands(commands: string[], cwd: string): Promise<{ success: boolean; results: CommandExecResult[] }> {
|
|
|
|
|
const targetCwd = normalizePath(cwd);
|
|
|
|
|
const data = await sendBridgeMessageWithOptions<{ success: boolean; results?: CommandExecResult[] }>('api:fs:exec', {
|
|
|
|
|
commands,
|
|
|
|
|
cwd: targetCwd,
|
|
|
|
|
}, { timeoutMs: 300000 });
|
2025-12-13 16:34:17 +02:00
|
|
|
|
|
|
|
|
return {
|
2026-01-07 12:08:53 +02:00
|
|
|
success: Boolean(data?.success),
|
|
|
|
|
results: Array.isArray(data?.results) ? data.results : [],
|
2025-12-13 16:34:17 +02:00
|
|
|
};
|
|
|
|
|
},
|
2026-04-16 14:33:55 -06:00
|
|
|
|
|
|
|
|
async downloadFile(path: string): Promise<void> {
|
|
|
|
|
const target = normalizePath(path);
|
|
|
|
|
const url = `/api/fs/raw?path=${encodeURIComponent(target)}&download=true`;
|
|
|
|
|
const a = document.createElement('a');
|
|
|
|
|
a.href = url;
|
|
|
|
|
a.download = target.split('/').pop() || 'file';
|
|
|
|
|
document.body.appendChild(a);
|
|
|
|
|
a.click();
|
|
|
|
|
document.body.removeChild(a);
|
|
|
|
|
},
|
2025-12-13 16:34:17 +02:00
|
|
|
});
|