From 708f9da5e154483d74e26a65818e43b71c31b0f2 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Tue, 21 Jul 2026 22:02:05 -0500 Subject: [PATCH] feat: show in finder btn for file pills --- .../src/process/externalLauncher.test.ts | 67 +++++++++++++++- apps/server/src/process/externalLauncher.ts | 66 ++++++++++++++- apps/server/src/server.test.ts | 27 +++++++ apps/server/src/ws.ts | 7 ++ apps/web/src/components/ChatMarkdown.tsx | 80 ++++++++++++++++++- .../client-runtime/src/state/shellCommands.ts | 4 + packages/contracts/src/editor.ts | 5 ++ packages/contracts/src/rpc.ts | 9 ++- 8 files changed, 258 insertions(+), 7 deletions(-) diff --git a/apps/server/src/process/externalLauncher.test.ts b/apps/server/src/process/externalLauncher.test.ts index 43ca40e9c7..fe5bf2ba92 100644 --- a/apps/server/src/process/externalLauncher.test.ts +++ b/apps/server/src/process/externalLauncher.test.ts @@ -92,11 +92,67 @@ it.effect("launches the default browser through the platform command", () => { ); }); +it.effect("reveals a file in Finder on macOS", () => { + let spawned: ChildProcess.StandardCommand | undefined; + let didUnref = false; + const target = "/tmp/project with spaces/src/odd '✨' file.ts"; + return Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + + yield* launcher.revealInFileManager(target); + + assert.ok(spawned); + assert.equal(spawned.command, "open"); + assert.deepEqual(spawned.args, ["-R", target]); + assert.equal(spawned.options.detached, true); + assert.equal(didUnref, true); + }).pipe( + Effect.provide( + testLayer({ + platform: "darwin", + onSpawn: (command) => { + spawned = command; + }, + onUnref: () => { + didUnref = true; + }, + }), + ), + ); +}); + +it.effect("reveals a file in Explorer on Windows", () => { + let spawned: ChildProcess.StandardCommand | undefined; + const target = `C:/project with spaces/src/odd '✨' file.ts`; + const windowsTarget = String.raw`C:\project with spaces\src\odd '✨' file.ts`; + return Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + + yield* launcher.revealInFileManager(target); + + assert.ok(spawned); + assert.equal(spawned.command, "explorer"); + assert.deepEqual(spawned.args, ["/select,", windowsTarget]); + assert.equal(spawned.options.detached, true); + }).pipe( + Effect.provide( + testLayer({ + platform: "win32", + onSpawn: (command) => { + spawned = command; + }, + }), + ), + ); +}); + it.effect("launches an installed editor with platform-safe arguments", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; - const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" }); + const binDir = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-editors-", + }); yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n"); let spawned: ChildProcess.StandardCommand | undefined; @@ -134,7 +190,9 @@ it.effect("discovers editors through the service API", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; - const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" }); + const binDir = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-editors-", + }); yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n"); yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n"); @@ -159,7 +217,10 @@ it.effect("rejects unknown editors through the service API", () => Effect.gen(function* () { const launcher = yield* ExternalLauncher.ExternalLauncher; const error = yield* launcher - .launchEditor({ editor: "missing-editor" as never, cwd: "/tmp/workspace" }) + .launchEditor({ + editor: "missing-editor" as never, + cwd: "/tmp/workspace", + }) .pipe(Effect.flip); assert.instanceOf(error, ExternalLauncher.ExternalLauncherUnknownEditorError); assert.equal(error.editor, "missing-editor"); diff --git a/apps/server/src/process/externalLauncher.ts b/apps/server/src/process/externalLauncher.ts index 9c2f0e417d..db968f6bb6 100644 --- a/apps/server/src/process/externalLauncher.ts +++ b/apps/server/src/process/externalLauncher.ts @@ -313,6 +313,8 @@ export class ExternalLauncher extends Context.Service< * Launches the editor as a detached process so server startup is not blocked. */ readonly launchEditor: (input: LaunchEditorInput) => Effect.Effect; + /** Reveal a workspace path in the host platform's file manager. */ + readonly revealInFileManager: (target: string) => Effect.Effect; } >()("t3/process/externalLauncher") {} @@ -332,7 +334,9 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( }); const editorDef = EDITORS.find((editor) => editor.id === input.editor); if (!editorDef) { - return yield* new ExternalLauncherUnknownEditorError({ editor: input.editor }); + return yield* new ExternalLauncherUnknownEditorError({ + editor: input.editor, + }); } if (editorDef.commands) { @@ -349,7 +353,9 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( } if (editorDef.id !== "file-manager") { - return yield* new ExternalLauncherUnsupportedEditorError({ editor: input.editor }); + return yield* new ExternalLauncherUnsupportedEditorError({ + editor: input.editor, + }); } return { @@ -430,6 +436,57 @@ const launchEditorProcess = Effect.fn("externalLauncher.launchEditorProcess")(fu ); }); +const resolveFileManagerRevealLaunch = Effect.fn("externalLauncher.resolveFileManagerRevealLaunch")( + function* (target: string): Effect.fn.Return { + const platform = yield* HostProcessPlatform; + const path = yield* Path.Path; + + switch (platform) { + case "darwin": + return { + command: "open", + args: ["-R", target], + options: DETACHED_IGNORE_STDIO_OPTIONS, + }; + case "win32": { + const windowsTarget = target.replaceAll("/", "\\"); + return { + command: "explorer", + args: ["/select,", windowsTarget], + options: DETACHED_IGNORE_STDIO_OPTIONS, + }; + } + default: + return { + command: "xdg-open", + args: [path.dirname(target)], + options: DETACHED_IGNORE_STDIO_OPTIONS, + }; + } + }, +); + +const revealInFileManager = Effect.fn("externalLauncher.revealInFileManager")(function* ( + target: string, +): Effect.fn.Return< + void, + ExternalLauncherError, + ChildProcessSpawner.ChildProcessSpawner | Path.Path +> { + const launch = yield* resolveFileManagerRevealLaunch(target); + return yield* launchAndUnref( + launch, + (cause) => + new ExternalLauncherEditorSpawnError({ + editor: "file-manager", + target, + command: launch.command, + args: launch.args, + cause, + }), + ); +}); + export const make = Effect.gen(function* () { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const fileSystem = yield* FileSystem.FileSystem; @@ -457,6 +514,11 @@ export const make = Effect.gen(function* () { ), ), ), + revealInFileManager: (target) => + revealInFileManager(target).pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.provideService(Path.Path, path), + ), }); }); diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 6122c49814..5e2c11b26d 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -4798,6 +4798,33 @@ it.layer(NodeServices.layer)("server router seam", (it) => { }).pipe(Effect.provide(NodeHttpServer.layerTest)), ); + it.effect("routes websocket rpc shell.revealInFileManager", () => + Effect.gen(function* () { + let revealedPath: string | null = null; + yield* buildAppUnderTest({ + layers: { + externalLauncher: { + revealInFileManager: (path) => + Effect.sync(() => { + revealedPath = path; + }), + }, + }, + }); + + const wsUrl = yield* getWsServerUrl("/ws"); + yield* Effect.scoped( + withWsRpcClient(wsUrl, (client) => + client[WS_METHODS.shellRevealInFileManager]({ + path: "/tmp/project/src/index.ts", + }), + ), + ); + + assert.equal(revealedPath, "/tmp/project/src/index.ts"); + }).pipe(Effect.provide(NodeHttpServer.layerTest)), + ); + it.effect("routes websocket rpc git methods", () => Effect.gen(function* () { yield* buildAppUnderTest({ diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 08b1770a0a..ce91052fb8 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -315,6 +315,7 @@ const RPC_REQUIRED_SCOPE = new Map([ [WS_METHODS.projectsSearchEntries, AuthOrchestrationReadScope], [WS_METHODS.projectsWriteFile, AuthOrchestrationOperateScope], [WS_METHODS.shellOpenInEditor, AuthOrchestrationOperateScope], + [WS_METHODS.shellRevealInFileManager, AuthOrchestrationOperateScope], [WS_METHODS.filesystemBrowse, AuthOrchestrationReadScope], [WS_METHODS.assetsCreateUrl, AuthOrchestrationReadScope], [WS_METHODS.subscribeVcsStatus, AuthOrchestrationReadScope], @@ -1675,6 +1676,12 @@ const makeWsRpcLayer = ( observeRpcEffect(WS_METHODS.shellOpenInEditor, externalLauncher.launchEditor(input), { "rpc.aggregate": "workspace", }), + [WS_METHODS.shellRevealInFileManager]: (input) => + observeRpcEffect( + WS_METHODS.shellRevealInFileManager, + externalLauncher.revealInFileManager(input.path), + { "rpc.aggregate": "workspace" }, + ), [WS_METHODS.filesystemBrowse]: (input) => observeRpcEffect( WS_METHODS.filesystemBrowse, diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index c85340c715..862eefe7bc 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -74,6 +74,7 @@ import { serverEnvironment } from "../state/server"; import { assetEnvironment } from "../state/assets"; import { usePreparedConnection } from "../state/session"; import { previewEnvironment } from "../state/preview"; +import { shellEnvironment } from "../state/shell"; import { useAtomCommand } from "../state/use-atom-command"; import { useAtomQueryRunner } from "../state/use-atom-query-runner"; import { isPreviewSupportedInRuntime } from "../previewStateStore"; @@ -743,6 +744,7 @@ interface MarkdownFileLinkProps { theme: "light" | "dark"; threadRef?: ScopedThreadRef | undefined; onOpen: (targetPath: string) => Promise>; + onShowInFinder: (targetPath: string) => Promise>; onOpenInBrowser?: (() => Promise>) | undefined; className?: string | undefined; } @@ -1022,6 +1024,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ theme, threadRef, onOpen, + onShowInFinder, onOpenInBrowser, className, }: MarkdownFileLinkProps) { @@ -1068,6 +1071,38 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ useRightPanelStore.getState().openFile(threadRef, workspaceRelativePath, line); }, [handleOpenInEditor, line, threadRef, workspaceRelativePath]); + const handleShowInFinder = useCallback(() => { + void (async () => { + try { + const result = await onShowInFinder(iconPath); + if (result._tag === "Success" || isAtomCommandInterrupted(result)) { + return; + } + reportMarkdownActionFailure( + { operation: "show-file-in-finder", target: iconPath }, + result.cause, + ); + const error = squashAtomCommandFailure(result); + toastManager.add( + stackedThreadToast({ + type: "error", + title: "Unable to show file in Finder", + description: error instanceof Error ? error.message : "An error occurred.", + }), + ); + } catch (cause) { + reportMarkdownActionFailure({ operation: "show-file-in-finder", target: iconPath }, cause); + toastManager.add( + stackedThreadToast({ + type: "error", + title: "Unable to show file in Finder", + description: cause instanceof Error ? cause.message : "An error occurred.", + }), + ); + } + })(); + }, [iconPath, onShowInFinder]); + const handleOpenInBrowser = useCallback(() => { if (!onOpenInBrowser) { return; @@ -1154,6 +1189,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ if (!api) return; try { + const platform = navigator.platform.toLowerCase(); const clicked = await api.contextMenu.show( [ { id: "open", label: "Open in editor" }, @@ -1162,6 +1198,14 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ : []), { id: "copy-relative", label: "Copy relative path" }, { id: "copy-full", label: "Copy full path" }, + { + id: "show-in-finder", + label: platform.includes("mac") + ? "Show in Finder" + : platform.includes("win") + ? "Show in File Explorer" + : "Show in Files", + }, ] as const, { x: event.clientX, y: event.clientY }, ); @@ -1180,6 +1224,10 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ } if (clicked === "copy-full") { handleCopy(targetPath, "Full path"); + return; + } + if (clicked === "show-in-finder") { + handleShowInFinder(); } } catch (cause) { reportMarkdownActionFailure( @@ -1188,7 +1236,15 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ ); } }, - [displayPath, handleCopy, handleOpenInBrowser, handleOpenInEditor, onOpenInBrowser, targetPath], + [ + displayPath, + handleCopy, + handleOpenInBrowser, + handleOpenInEditor, + handleShowInFinder, + onOpenInBrowser, + targetPath, + ], ); return ( @@ -1242,6 +1298,7 @@ function areMarkdownFileLinkPropsEqual( previous.theme === next.theme && previous.threadRef === next.threadRef && previous.onOpen === next.onOpen && + previous.onShowInFinder === next.onShowInFinder && previous.onOpenInBrowser === next.onOpenInBrowser && previous.className === next.className ); @@ -1264,6 +1321,9 @@ function ChatMarkdown({ const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false, }); + const revealInFileManager = useAtomCommand(shellEnvironment.revealInFileManager, { + reportFailure: false, + }); const preparedConnection = usePreparedConnection(threadRef?.environmentId ?? null); const environmentId = useActiveEnvironmentId(); const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId)); @@ -1271,6 +1331,22 @@ function ChatMarkdown({ environmentId, serverConfig?.availableEditors ?? [], ); + const showFileInFinder = useCallback( + (targetPath: string) => { + if (environmentId === null) { + return Promise.resolve( + AsyncResult.failure( + Cause.fail(new Error("Cannot show the file because no environment is selected.")), + ), + ); + } + return revealInFileManager({ + environmentId, + input: { path: targetPath }, + }); + }, + [environmentId, revealInFileManager], + ); const diffThemeName = resolveDiffThemeName(resolvedTheme); const markdownFileLinkMetaByHref = useMemo(() => { const metaByHref = new Map< @@ -1492,6 +1568,7 @@ function ChatMarkdown({ theme={resolvedTheme} threadRef={threadRef} onOpen={openInPreferredEditor} + onShowInFinder={showFileInFinder} onOpenInBrowser={ threadRef && isPreviewSupportedInRuntime() && @@ -1548,6 +1625,7 @@ function ChatMarkdown({ openExternalLinkInPreview, openMarkdownFileInPreview, resolvedTheme, + showFileInFinder, skills, text, threadRef, diff --git a/packages/client-runtime/src/state/shellCommands.ts b/packages/client-runtime/src/state/shellCommands.ts index 785bb83ed4..2ea04b82d7 100644 --- a/packages/client-runtime/src/state/shellCommands.ts +++ b/packages/client-runtime/src/state/shellCommands.ts @@ -12,5 +12,9 @@ export function createShellEnvironmentAtoms( label: "environment-data:shell:open-in-editor", tag: WS_METHODS.shellOpenInEditor, }), + revealInFileManager: createEnvironmentRpcCommand(runtime, { + label: "environment-data:shell:reveal-in-file-manager", + tag: WS_METHODS.shellRevealInFileManager, + }), }; } diff --git a/packages/contracts/src/editor.ts b/packages/contracts/src/editor.ts index 5948d87e1d..902b3c0c98 100644 --- a/packages/contracts/src/editor.ts +++ b/packages/contracts/src/editor.ts @@ -50,6 +50,11 @@ export const LaunchEditorInput = Schema.Struct({ }); export type LaunchEditorInput = typeof LaunchEditorInput.Type; +export const RevealPathInput = Schema.Struct({ + path: TrimmedNonEmptyString, +}); +export type RevealPathInput = typeof RevealPathInput.Type; + export class ExternalLauncherUnknownEditorError extends Schema.TaggedErrorClass()( "ExternalLauncherUnknownEditorError", { diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index 0356aa1807..a511b429e3 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -2,7 +2,7 @@ import * as Schema from "effect/Schema"; import * as Rpc from "effect/unstable/rpc/Rpc"; import * as RpcGroup from "effect/unstable/rpc/RpcGroup"; -import { ExternalLauncherError, LaunchEditorInput } from "./editor.ts"; +import { ExternalLauncherError, LaunchEditorInput, RevealPathInput } from "./editor.ts"; import { AuthAccessStreamError, AuthAccessStreamEvent, @@ -156,6 +156,7 @@ export const WS_METHODS = { // Shell methods shellOpenInEditor: "shell.openInEditor", + shellRevealInFileManager: "shell.revealInFileManager", // Filesystem methods filesystemBrowse: "filesystem.browse", @@ -390,6 +391,11 @@ export const WsShellOpenInEditorRpc = Rpc.make(WS_METHODS.shellOpenInEditor, { error: Schema.Union([ExternalLauncherError, EnvironmentAuthorizationError]), }); +export const WsShellRevealInFileManagerRpc = Rpc.make(WS_METHODS.shellRevealInFileManager, { + payload: RevealPathInput, + error: Schema.Union([ExternalLauncherError, EnvironmentAuthorizationError]), +}); + export const WsFilesystemBrowseRpc = Rpc.make(WS_METHODS.filesystemBrowse, { payload: FilesystemBrowseInput, success: FilesystemBrowseResult, @@ -712,6 +718,7 @@ export const WsRpcGroup = RpcGroup.make( WsProjectsSearchEntriesRpc, WsProjectsWriteFileRpc, WsShellOpenInEditorRpc, + WsShellRevealInFileManagerRpc, WsFilesystemBrowseRpc, WsAssetsCreateUrlRpc, WsSubscribeVcsStatusRpc,