Allow previews to open readable files (#1214)

* Allow previews to open readable files

* Fix Windows file preview path test

* Address file preview review comments
This commit is contained in:
Mohamed Boudra
2026-05-29 11:35:38 +08:00
committed by GitHub
parent 2e1b907c63
commit 3585c80266
11 changed files with 361 additions and 132 deletions
@@ -53,6 +53,19 @@ describe("resolveAssistantImageSource", () => {
});
});
it("uses the same home-root target as file previews for tilde paths", () => {
expect(
resolveAssistantImageSource({
source: "~/.paseo/screenshots/output.png",
workspaceRoot: "/Users/test/project",
}),
).toEqual({
kind: "file_rpc",
cwd: "~",
path: "~/.paseo/screenshots/output.png",
});
});
it("normalizes file URIs into file RPC requests", () => {
expect(
resolveAssistantImageSource({
@@ -1,58 +1,10 @@
import { fileUriToPath } from "@/attachments/utils";
import { isAbsolutePath } from "@/utils/path";
import { resolveFilePreviewReadTarget } from "@/file-explorer/preview-target";
export type AssistantImageSourceResolution =
| { kind: "direct"; uri: string }
| { kind: "file_rpc"; cwd: string; path: string };
function trimTrailingSeparators(value: string): string {
if (value === "/" || /^[A-Za-z]:[\\/]?$/.test(value)) {
return value.replace(/\\/g, "/");
}
return value.replace(/[\\/]+$/, "");
}
function normalizeForPathComparison(value: string): string {
const normalized = trimTrailingSeparators(value.replace(/\\/g, "/"));
if (/^[A-Za-z]:\//.test(normalized)) {
return `${normalized.slice(0, 1).toUpperCase()}${normalized.slice(1)}`;
}
return normalized;
}
function isPathWithinRoot(candidatePath: string, rootPath: string): boolean {
const candidate = normalizeForPathComparison(candidatePath);
const root = normalizeForPathComparison(rootPath);
if (!candidate || !root) {
return false;
}
if (root === "/") {
return candidate.startsWith("/");
}
if (candidate === root) {
return true;
}
return candidate.startsWith(`${root}/`);
}
function deriveFallbackRootFromAbsolutePath(value: string): string | null {
if (value.startsWith("/")) {
return "/";
}
const driveMatch = /^([A-Za-z]:)[\\/]/.exec(value);
if (driveMatch?.[1]) {
return `${driveMatch[1]}/`;
}
const uncMatch = /^(\\\\[^\\]+\\[^\\]+)/.exec(value);
if (uncMatch?.[1]) {
return uncMatch[1];
}
return null;
}
export function resolveAssistantImageSource(input: {
source: string;
workspaceRoot?: string;
@@ -71,39 +23,17 @@ export function resolveAssistantImageSource(input: {
return null;
}
if (!isAbsolutePath(sourcePath)) {
const workspaceRoot = input.workspaceRoot?.trim();
if (!workspaceRoot || !isAbsolutePath(workspaceRoot)) {
return null;
}
return {
kind: "file_rpc",
cwd: workspaceRoot,
path: sourcePath,
};
}
const workspaceRoot = input.workspaceRoot?.trim();
if (
workspaceRoot &&
isAbsolutePath(workspaceRoot) &&
isPathWithinRoot(sourcePath, workspaceRoot)
) {
return {
kind: "file_rpc",
cwd: workspaceRoot,
path: sourcePath,
};
}
const fallbackRoot = deriveFallbackRootFromAbsolutePath(sourcePath);
if (!fallbackRoot) {
const readTarget = resolveFilePreviewReadTarget({
path: sourcePath,
workspaceRoot: input.workspaceRoot,
});
if (!readTarget) {
return null;
}
return {
kind: "file_rpc",
cwd: fallbackRoot,
path: sourcePath,
cwd: readTarget.cwd,
path: readTarget.path,
};
}