Files
paseo/packages/app/src/utils/assistant-image-source.ts
Mohamed Boudra 3585c80266 Allow previews to open readable files (#1214)
* Allow previews to open readable files

* Fix Windows file preview path test

* Address file preview review comments
2026-05-29 11:35:38 +08:00

40 lines
965 B
TypeScript

import { fileUriToPath } from "@/attachments/utils";
import { resolveFilePreviewReadTarget } from "@/file-explorer/preview-target";
export type AssistantImageSourceResolution =
| { kind: "direct"; uri: string }
| { kind: "file_rpc"; cwd: string; path: string };
export function resolveAssistantImageSource(input: {
source: string;
workspaceRoot?: string;
}): AssistantImageSourceResolution | null {
const source = input.source.trim();
if (!source) {
return null;
}
if (/^(https?:|data:|blob:)/i.test(source)) {
return { kind: "direct", uri: source };
}
const sourcePath = source.startsWith("file://") ? fileUriToPath(source) : source;
if (!sourcePath) {
return null;
}
const readTarget = resolveFilePreviewReadTarget({
path: sourcePath,
workspaceRoot: input.workspaceRoot,
});
if (!readTarget) {
return null;
}
return {
kind: "file_rpc",
cwd: readTarget.cwd,
path: readTarget.path,
};
}