From 0e69aea035e9552af2e1025c1a1505753fe14c20 Mon Sep 17 00:00:00 2001 From: "t3-code[bot]" <269035359+t3-code[bot]@users.noreply.github.com> Date: Tue, 4 Aug 2026 21:44:29 +0000 Subject: [PATCH] feat(web): open files in containing folder --- apps/server/src/auth/RpcAuthorization.ts | 1 + .../src/process/externalLauncher.test.ts | 91 +++++++++++++++++++ apps/server/src/process/externalLauncher.ts | 42 ++++++++- apps/server/src/server.test.ts | 27 ++++++ apps/server/src/ws.ts | 6 ++ apps/web/src/components/ChatMarkdown.tsx | 75 ++++++++++++++- .../client-runtime/src/state/shellCommands.ts | 4 + packages/contracts/src/editor.ts | 5 + packages/contracts/src/rpc.ts | 13 ++- 9 files changed, 261 insertions(+), 3 deletions(-) diff --git a/apps/server/src/auth/RpcAuthorization.ts b/apps/server/src/auth/RpcAuthorization.ts index 36f348d637..610e5a78c1 100644 --- a/apps/server/src/auth/RpcAuthorization.ts +++ b/apps/server/src/auth/RpcAuthorization.ts @@ -81,6 +81,7 @@ export const RPC_REQUIRED_SCOPES = { [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, diff --git a/apps/server/src/process/externalLauncher.test.ts b/apps/server/src/process/externalLauncher.test.ts index 1ab6166e92..587477d606 100644 --- a/apps/server/src/process/externalLauncher.test.ts +++ b/apps/server/src/process/externalLauncher.test.ts @@ -94,6 +94,97 @@ it.effect("launches the default browser through the platform command", () => { ); }); +it.effect("reveals files with the linux file manager", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" }); + const commandPath = path.join(binDir, "xdg-open"); + yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n"); + yield* fileSystem.chmod(commandPath, 0o755); + + let spawned: ChildProcess.StandardCommand | undefined; + yield* Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + yield* launcher.revealInFileManager({ path: "/tmp/project/src/index.ts" }); + }).pipe( + Effect.provide( + testLayer({ + platform: "linux", + env: { PATH: binDir }, + onSpawn: (command) => { + spawned = command; + }, + }), + ), + ); + + assert.ok(spawned); + assert.equal(spawned.command, "xdg-open"); + assert.deepEqual(spawned.args, ["/tmp/project/src"]); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + +it.effect("reveals files with Finder", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" }); + const commandPath = path.join(binDir, "open"); + yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n"); + yield* fileSystem.chmod(commandPath, 0o755); + + let spawned: ChildProcess.StandardCommand | undefined; + yield* Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + yield* launcher.revealInFileManager({ path: "/tmp/project/src/index.ts" }); + }).pipe( + Effect.provide( + testLayer({ + platform: "darwin", + env: { PATH: binDir }, + onSpawn: (command) => { + spawned = command; + }, + }), + ), + ); + + assert.ok(spawned); + assert.equal(spawned.command, "open"); + assert.deepEqual(spawned.args, ["-R", "/tmp/project/src/index.ts"]); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + +it.effect("reveals files with Windows Explorer", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" }); + yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n"); + + let spawned: ChildProcess.StandardCommand | undefined; + yield* Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + yield* launcher.revealInFileManager({ path: "C:\\project\\src\\index.ts" }); + }).pipe( + Effect.provide( + testLayer({ + platform: "win32", + env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" }, + onSpawn: (command) => { + spawned = command; + }, + }), + ), + ); + + assert.ok(spawned); + assert.equal(spawned.command, "explorer"); + assert.deepEqual(spawned.args, ["/select,C:\\project\\src\\index.ts"]); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + it.effect("launches an installed editor with platform-safe arguments", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/apps/server/src/process/externalLauncher.ts b/apps/server/src/process/externalLauncher.ts index 8ec928f26f..16df2853c6 100644 --- a/apps/server/src/process/externalLauncher.ts +++ b/apps/server/src/process/externalLauncher.ts @@ -16,6 +16,7 @@ import { ExternalLauncherUnsupportedEditorError, type EditorId, type LaunchEditorInput, + type RevealInFileManagerInput, } from "@t3tools/contracts"; import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell"; @@ -45,7 +46,7 @@ export { ExternalLauncherUnsupportedEditorError, isExternalLauncherError, } from "@t3tools/contracts"; -export type { LaunchEditorInput }; +export type { LaunchEditorInput, RevealInFileManagerInput }; interface EditorLaunch { readonly editor: EditorId; readonly target: string; @@ -337,6 +338,10 @@ 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 file in the host file manager. */ + readonly revealInFileManager: ( + input: RevealInFileManagerInput, + ) => Effect.Effect; } >()("t3/process/externalLauncher") {} @@ -384,6 +389,33 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( }; }); +const resolveFileManagerRevealLaunch = Effect.fn( + "externalLauncher.resolveFileManagerRevealLaunch", +)(function* ( + input: RevealInFileManagerInput, +): Effect.fn.Return { + const platform = yield* HostProcessPlatform; + const path = yield* Path.Path; + const args = + platform === "darwin" + ? ["-R", input.path] + : platform === "win32" + ? [`/select,${input.path}`] + : [path.dirname(input.path)]; + + yield* Effect.annotateCurrentSpan({ + "externalLauncher.target": input.path, + "externalLauncher.platform": platform, + }); + + return { + editor: "file-manager", + target: input.path, + command: fileManagerCommandForPlatform(platform), + args, + }; +}); + const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* ( launch: ProcessLaunch, onError: (cause: unknown) => ExternalLauncherError, @@ -501,6 +533,14 @@ export const make = Effect.gen(function* () { ), ), ), + revealInFileManager: (input) => + provideCommandResolutionServices( + Effect.flatMap(resolveFileManagerRevealLaunch(input), (launch) => + launchEditorProcess(launch).pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ), + ), + ), }); }); diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 89f903c4f8..0e9fdb16b6 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -5097,6 +5097,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 revealedInput: { path: string } | null = null; + yield* buildAppUnderTest({ + layers: { + externalLauncher: { + revealInFileManager: (input) => + Effect.sync(() => { + revealedInput = input; + }), + }, + }, + }); + + const wsUrl = yield* getWsServerUrl("/ws"); + yield* Effect.scoped( + withWsRpcClient(wsUrl, (client) => + client[WS_METHODS.shellRevealInFileManager]({ + path: "/tmp/project/src/index.ts", + }), + ), + ); + + assert.deepEqual(revealedInput, { path: "/tmp/project/src/index.ts" }); + }).pipe(Effect.provide(NodeHttpServer.layerTest)), + ); + it.effect("routes websocket rpc shell.openInEditor errors", () => Effect.gen(function* () { const externalLauncherError = new ExternalLauncherCommandNotFoundError({ diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 56ea24a4a8..e25a052687 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -1831,6 +1831,12 @@ const makeWsRpcLayer = ( observeRpcEffect(WS_METHODS.shellOpenInEditor, externalLauncher.launchEditor(input), { "rpc.aggregate": "workspace", }), + [WS_METHODS.shellRevealInFileManager]: (input) => + observeRpcEffect( + WS_METHODS.shellRevealInFileManager, + externalLauncher.revealInFileManager(input), + { "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 53b043f3a8..4387b7098e 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -88,6 +88,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 { useOpenChangeRequestLink } from "~/lib/openPullRequestLink"; @@ -781,6 +782,7 @@ function UncachedShikiCodeBlock({ interface MarkdownFileLinkProps { href: string; + filePath: string; targetPath: string; iconPath: string; displayPath: string; @@ -791,6 +793,7 @@ interface MarkdownFileLinkProps { theme: "light" | "dark"; threadRef?: ScopedThreadRef | undefined; onOpen: (targetPath: string) => Promise>; + onRevealInFileManager: (filePath: string) => Promise>; onOpenInBrowser?: (() => Promise>) | undefined; className?: string | undefined; } @@ -1086,6 +1089,7 @@ function MarkdownExternalLinkContent({ const MarkdownFileLink = memo(function MarkdownFileLink({ href, + filePath, targetPath, iconPath, displayPath, @@ -1096,6 +1100,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ theme, threadRef, onOpen, + onRevealInFileManager, onOpenInBrowser, className, }: MarkdownFileLinkProps) { @@ -1134,6 +1139,38 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ })(); }, [onOpen, targetPath]); + const handleRevealInFileManager = useCallback(() => { + void (async () => { + try { + const result = await onRevealInFileManager(filePath); + if (result._tag === "Success" || isAtomCommandInterrupted(result)) { + return; + } + reportMarkdownActionFailure( + { operation: "open-file-in-folder", target: filePath }, + result.cause, + ); + const error = squashAtomCommandFailure(result); + toastManager.add( + stackedThreadToast({ + type: "error", + title: "Unable to open folder", + description: error instanceof Error ? error.message : "An error occurred.", + }), + ); + } catch (cause) { + reportMarkdownActionFailure({ operation: "open-file-in-folder", target: filePath }, cause); + toastManager.add( + stackedThreadToast({ + type: "error", + title: "Unable to open folder", + description: cause instanceof Error ? cause.message : "An error occurred.", + }), + ); + } + })(); + }, [filePath, onRevealInFileManager]); + const handleOpenInFilePreview = useCallback(() => { if (!threadRef || !workspaceRelativePath) { handleOpenInEditor(); @@ -1231,6 +1268,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ const clicked = await api.contextMenu.show( [ { id: "open", label: "Open in editor" }, + { id: "open-in-folder", label: "Open in folder" }, ...(onOpenInBrowser ? ([{ id: "open-in-browser", label: "Open in integrated browser" }] as const) : []), @@ -1244,6 +1282,10 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ handleOpenInEditor(); return; } + if (clicked === "open-in-folder") { + handleRevealInFileManager(); + return; + } if (clicked === "open-in-browser") { handleOpenInBrowser(); return; @@ -1262,7 +1304,15 @@ const MarkdownFileLink = memo(function MarkdownFileLink({ ); } }, - [displayPath, handleCopy, handleOpenInBrowser, handleOpenInEditor, onOpenInBrowser, targetPath], + [ + displayPath, + handleCopy, + handleOpenInBrowser, + handleOpenInEditor, + handleRevealInFileManager, + onOpenInBrowser, + targetPath, + ], ); return ( @@ -1306,6 +1356,7 @@ function areMarkdownFileLinkPropsEqual( ): boolean { return ( previous.href === next.href && + previous.filePath === next.filePath && previous.targetPath === next.targetPath && previous.iconPath === next.iconPath && previous.displayPath === next.displayPath && @@ -1316,6 +1367,7 @@ function areMarkdownFileLinkPropsEqual( previous.theme === next.theme && previous.threadRef === next.threadRef && previous.onOpen === next.onOpen && + previous.onRevealInFileManager === next.onRevealInFileManager && previous.onOpenInBrowser === next.onOpenInBrowser && previous.className === next.className ); @@ -1338,6 +1390,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)); @@ -1414,6 +1469,21 @@ function ChatMarkdown({ }, [openPreview, threadRef], ); + const revealMarkdownFileInFileManager = useCallback( + (filePath: string): Promise> => { + const targetEnvironmentId = threadRef?.environmentId ?? environmentId; + if (targetEnvironmentId === null) { + return Promise.resolve( + AsyncResult.failure(Cause.fail(new Error("No environment is selected."))), + ); + } + return revealInFileManager({ + environmentId: targetEnvironmentId, + input: { path: filePath }, + }); + }, + [environmentId, revealInFileManager, threadRef?.environmentId], + ); const openMarkdownFileInPreview = useCallback( (path: string) => { if (!threadRef || preparedConnection._tag === "None") { @@ -1460,6 +1530,7 @@ function ChatMarkdown({ return ( ( 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 d714a0e026..0610e17315 100644 --- a/packages/contracts/src/editor.ts +++ b/packages/contracts/src/editor.ts @@ -123,6 +123,11 @@ export const RemoteOpenTarget = Schema.Struct({ }); export type RemoteOpenTarget = typeof RemoteOpenTarget.Type; +export const RevealInFileManagerInput = Schema.Struct({ + path: TrimmedNonEmptyString, +}); +export type RevealInFileManagerInput = typeof RevealInFileManagerInput.Type; + export class ExternalLauncherUnknownEditorError extends Schema.TaggedErrorClass()( "ExternalLauncherUnknownEditorError", { diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index 115fc8a131..142669293e 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -2,7 +2,11 @@ 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, + RevealInFileManagerInput, +} from "./editor.ts"; import { AuthAccessStreamError, AuthAccessStreamEvent, @@ -204,6 +208,7 @@ export const WS_METHODS = { // Shell methods shellOpenInEditor: "shell.openInEditor", + shellRevealInFileManager: "shell.revealInFileManager", // Filesystem methods filesystemBrowse: "filesystem.browse", @@ -644,6 +649,11 @@ export const WsShellOpenInEditorRpc = Rpc.make(WS_METHODS.shellOpenInEditor, { error: Schema.Union([ExternalLauncherError, EnvironmentAuthorizationError]), }); +export const WsShellRevealInFileManagerRpc = Rpc.make(WS_METHODS.shellRevealInFileManager, { + payload: RevealInFileManagerInput, + error: Schema.Union([ExternalLauncherError, EnvironmentAuthorizationError]), +}); + export const WsFilesystemBrowseRpc = Rpc.make(WS_METHODS.filesystemBrowse, { payload: FilesystemBrowseInput, success: FilesystemBrowseResult, @@ -1022,6 +1032,7 @@ export const WsRpcGroup = RpcGroup.make( WsProjectsSearchEntriesRpc, WsProjectsWriteFileRpc, WsShellOpenInEditorRpc, + WsShellRevealInFileManagerRpc, WsFilesystemBrowseRpc, WsAssetsCreateUrlRpc, WsSubscribeVcsStatusRpc,