fix: harden open in folder behavior

This commit is contained in:
t3-code[bot] committed 2026-08-15 02:51:33 +00:00
1 parent 39788e86e9
commit f5965c2083
7 files changed
+246 -25

No files matched your search

+13 -6
View File
@@ -51,7 +51,11 @@ import {
resolveExternalWebLinkHost,
showExternalLinkContextMenu,
} from "./chat/externalLinkContextMenu";
import { buildFileLinkContextMenuItems } from "./chat/fileLinkContextMenu";
import {
buildFileLinkContextMenuItems,
canRevealFileLinkInManager,
resolveFileLinkEnvironmentId,
} from "./chat/fileLinkContextMenu";
import { hasSpecificPierreIconForFileName, syntheticFileNameForLanguageId } from "../pierre-icons";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
import { Button } from "./ui/button";
@@ -85,6 +89,7 @@ import { readLocalApi } from "../localApi";
import { cn } from "../lib/utils";
import { useRightPanelStore } from "../rightPanelStore";
import { useActiveEnvironmentId } from "../state/entities";
import { useEnvironment } from "../state/environments";
import { serverEnvironment } from "../state/server";
import { assetEnvironment } from "../state/assets";
import { usePreparedConnection } from "../state/session";
@@ -1394,15 +1399,17 @@ function ChatMarkdown({
reportFailure: false,
});
const activeEnvironmentId = useActiveEnvironmentId();
const environmentId = threadRef?.environmentId ?? activeEnvironmentId;
const environmentId = resolveFileLinkEnvironmentId(threadRef?.environmentId, activeEnvironmentId);
const environment = useEnvironment(environmentId);
const preparedConnection = usePreparedConnection(environmentId);
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
const availableEditors = serverConfig?.availableEditors ?? [];
const openInPreferredEditor = useOpenInPreferredEditor(environmentId, availableEditors);
const canRevealInFileManager =
preparedConnection._tag === "Some" &&
serverConfig?.environment.capabilities.fileManagerReveal === true &&
availableEditors.includes("file-manager");
const canRevealInFileManager = canRevealFileLinkInManager({
connectionPhase: environment?.connection.phase,
supportsRevealRpc: serverConfig?.environment.capabilities.fileManagerReveal === true,
availableEditors,
});
const diffThemeName = resolveDiffThemeName(resolvedTheme);
const markdownFileLinkMetaByHref = useMemo(() => {
const metaByHref = new Map<
@@ -1,6 +1,47 @@
import { EnvironmentId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import { buildFileLinkContextMenuItems } from "./fileLinkContextMenu";
import {
buildFileLinkContextMenuItems,
canRevealFileLinkInManager,
resolveFileLinkEnvironmentId,
} from "./fileLinkContextMenu";
describe("file link environment", () => {
it("routes through the thread environment before the active environment", () => {
const threadEnvironmentId = EnvironmentId.make("thread-environment");
expect(
resolveFileLinkEnvironmentId(threadEnvironmentId, EnvironmentId.make("active-environment")),
).toBe(threadEnvironmentId);
});
it("falls back to the active environment without thread context", () => {
const activeEnvironmentId = EnvironmentId.make("active-environment");
expect(resolveFileLinkEnvironmentId(undefined, activeEnvironmentId)).toBe(activeEnvironmentId);
});
it.each([
{
connectionPhase: "reconnecting",
supportsRevealRpc: true,
availableEditors: ["file-manager"],
},
{ connectionPhase: "connected", supportsRevealRpc: false, availableEditors: ["file-manager"] },
{ connectionPhase: "connected", supportsRevealRpc: true, availableEditors: [] },
] as const)("hides reveal when support is incomplete", (input) => {
expect(canRevealFileLinkInManager(input)).toBe(false);
});
it("allows reveal only when connected with rpc and file manager support", () => {
expect(
canRevealFileLinkInManager({
connectionPhase: "connected",
supportsRevealRpc: true,
availableEditors: ["file-manager"],
}),
).toBe(true);
});
});
describe("chat file link context menu", () => {
it("puts Open in folder first when the environment supports it", () => {
@@ -1,4 +1,4 @@
import type { ContextMenuItem } from "@t3tools/contracts";
import type { ContextMenuItem, EditorId, EnvironmentId } from "@t3tools/contracts";
export type FileLinkContextMenuAction =
| "open-in-folder"
@@ -7,6 +7,25 @@ export type FileLinkContextMenuAction =
| "copy-relative"
| "copy-full";
export function resolveFileLinkEnvironmentId(
threadEnvironmentId: EnvironmentId | undefined,
activeEnvironmentId: EnvironmentId | null,
): EnvironmentId | null {
return threadEnvironmentId ?? activeEnvironmentId;
}
export function canRevealFileLinkInManager(input: {
readonly connectionPhase: string | undefined;
readonly supportsRevealRpc: boolean;
readonly availableEditors: ReadonlyArray<EditorId>;
}): boolean {
return (
input.connectionPhase === "connected" &&
input.supportsRevealRpc &&
input.availableEditors.includes("file-manager")
);
}
export function buildFileLinkContextMenuItems(input: {
readonly canRevealInFileManager: boolean;
readonly canOpenInBrowser: boolean;
+21
View File
@@ -108,6 +108,27 @@ describe("resolveMarkdownFileLinkTarget", () => {
});
});
it("resolves the exact file path used by open in folder", () => {
expect(resolveMarkdownFileLinkMeta("src/file%20name.ts:12:4", "/repo/project")).toMatchObject({
filePath: "/repo/project/src/file name.ts",
targetPath: "/repo/project/src/file name.ts:12:4",
line: 12,
column: 4,
});
expect(resolveMarkdownFileLinkMeta("/tmp/report.ts:9", "/repo/project")).toMatchObject({
filePath: "/tmp/report.ts",
targetPath: "/tmp/report.ts:9",
line: 9,
});
});
it("resolves missing paths without requiring browser-side file access", () => {
expect(resolveMarkdownFileLinkMeta("src/not-created-yet.ts", "/repo/project")).toMatchObject({
filePath: "/repo/project/src/not-created-yet.ts",
workspaceRelativePath: "src/not-created-yet.ts",
});
});
it("normalizes slash-prefixed windows drive paths before resolving", () => {
expect(
resolveMarkdownFileLinkTarget(
+2 -2
View File
@@ -5,8 +5,8 @@ const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
const WINDOWS_UNC_PATH_PATTERN = /^\\\\/;
const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/;
const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
const RELATIVE_FILE_PATH_PATTERN = /^[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)+(?::\d+){0,2}$/;
const RELATIVE_FILE_NAME_PATTERN = /^[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+(?::\d+){0,2}$/;
const RELATIVE_FILE_PATH_PATTERN = /^[A-Za-z0-9._ -]+(?:\/[A-Za-z0-9._ -]+)+(?::\d+){0,2}$/;
const RELATIVE_FILE_NAME_PATTERN = /^[A-Za-z0-9._ -]+\.[A-Za-z0-9_-]+(?::\d+){0,2}$/;
const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/;
const POSITION_ONLY_PATTERN = /^\d+(?::\d+)?$/;
// Standard OS and dev-container roots; deliberately excludes app-route-ish