The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | isAbsolutePath, |
| 4 | isWindowsAbsolutePath, |
| 5 | normalizeProjectPathForComparison, |
| 6 | workspaceRelativeFilePath, |
| 7 | } from "@t3tools/shared/path"; |
| 8 | |
| 9 | import { useRightPanelStore } from "./rightPanelStore"; |
| 10 | import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; |
| 11 | |
| 12 | interface OpenDiffFilePrimaryActionInput { |
| 13 | readonly threadRef: ScopedThreadRef | null; |
| 14 | readonly filePath: string; |
| 15 | readonly activeCwd: string | undefined; |
| 16 | readonly repositoryRoot?: string | undefined; |
| 17 | readonly openInEditor: (targetPath: string) => void; |
| 18 | } |
| 19 | |
| 20 | function normalizedRelativePathSegments( |
| 21 | filePath: string, |
| 22 | windowsRoot: boolean, |
| 23 | ): ReadonlyArray<string> | null { |
| 24 | if (filePath.startsWith("/") || isWindowsAbsolutePath(filePath) || /^[a-zA-Z]:/.test(filePath)) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | // On POSIX a backslash is part of a file name; Windows treats it as a separator |
| 29 | // (tool-call patches can name files with either), so reject ".." either way. |
| 30 | const parts = filePath.split(/[\\/]/); |
| 31 | if (parts.includes("..")) return null; |
| 32 | const segments = (windowsRoot ? parts : filePath.split("/")).filter( |
| 33 | (segment) => segment.length > 0 && segment !== ".", |
| 34 | ); |
| 35 | if (segments.length === 0) return null; |
| 36 | return segments; |
| 37 | } |
| 38 | |
| 39 | function repositoryRelativeWorkspaceSegments( |
| 40 | workspaceRoot: string | undefined, |
| 41 | repositoryRoot: string | undefined, |
| 42 | ): ReadonlyArray<string> | null { |
| 43 | if (!workspaceRoot || !repositoryRoot) return null; |
| 44 | |
| 45 | const normalizedWorkspaceRoot = normalizeProjectPathForComparison(workspaceRoot); |
| 46 | const normalizedRepositoryRoot = normalizeProjectPathForComparison(repositoryRoot); |
| 47 | if (normalizedWorkspaceRoot === normalizedRepositoryRoot) return []; |
| 48 | |
| 49 | const separator = isWindowsAbsolutePath(normalizedRepositoryRoot) ? "\\" : "/"; |
| 50 | const repositoryPrefix = normalizedRepositoryRoot.endsWith(separator) |
| 51 | ? normalizedRepositoryRoot |
| 52 | : `${normalizedRepositoryRoot}${separator}`; |
| 53 | if (!normalizedWorkspaceRoot.startsWith(repositoryPrefix)) return null; |
| 54 | |
| 55 | return normalizedWorkspaceRoot.slice(repositoryPrefix.length).split(separator).filter(Boolean); |
| 56 | } |
| 57 | |
| 58 | export function resolveDiffPathForWorkspace(input: { |
| 59 | readonly filePath: string; |
| 60 | readonly workspaceRoot: string | undefined; |
| 61 | readonly repositoryRoot: string | undefined; |
| 62 | }): string | null { |
| 63 | const windowsRoot = [input.workspaceRoot, input.repositoryRoot].some( |
| 64 | (root) => root !== undefined && isWindowsAbsolutePath(root), |
| 65 | ); |
| 66 | // Claude and ACP tool-call diffs name files by absolute path. |
| 67 | if (isAbsolutePath(input.filePath)) { |
| 68 | const relativePath = workspaceRelativeFilePath(input.filePath, input.workspaceRoot); |
| 69 | const segments = |
| 70 | relativePath === null ? null : normalizedRelativePathSegments(relativePath, windowsRoot); |
| 71 | return segments ? segments.join("/") : null; |
| 72 | } |
| 73 | |
| 74 | const fileSegments = normalizedRelativePathSegments(input.filePath, windowsRoot); |
| 75 | if (!fileSegments) return null; |
| 76 | |
| 77 | const workspaceSegments = repositoryRelativeWorkspaceSegments( |
| 78 | input.workspaceRoot, |
| 79 | input.repositoryRoot, |
| 80 | ); |
| 81 | if (!workspaceSegments || workspaceSegments.length === 0) { |
| 82 | return fileSegments.join("/"); |
| 83 | } |
| 84 | |
| 85 | const caseInsensitive = input.repositoryRoot |
| 86 | ? isWindowsAbsolutePath(input.repositoryRoot) |
| 87 | : false; |
| 88 | const belongsToWorkspace = workspaceSegments.every((segment, index) => { |
| 89 | const candidate = fileSegments[index]; |
| 90 | if (candidate === undefined) return false; |
| 91 | return caseInsensitive ? candidate.toLowerCase() === segment : candidate === segment; |
| 92 | }); |
| 93 | if (!belongsToWorkspace) return null; |
| 94 | |
| 95 | const relativeSegments = fileSegments.slice(workspaceSegments.length); |
| 96 | return relativeSegments.length > 0 ? relativeSegments.join("/") : null; |
| 97 | } |
| 98 | |
| 99 | export function openDiffFilePrimaryAction({ |
| 100 | threadRef, |
| 101 | filePath, |
| 102 | activeCwd, |
| 103 | repositoryRoot, |
| 104 | openInEditor, |
| 105 | }: OpenDiffFilePrimaryActionInput): void { |
| 106 | const workspaceFilePath = resolveDiffPathForWorkspace({ |
| 107 | filePath, |
| 108 | workspaceRoot: activeCwd, |
| 109 | repositoryRoot, |
| 110 | }); |
| 111 | if (!workspaceFilePath) return; |
| 112 | |
| 113 | if (threadRef) { |
| 114 | useRightPanelStore.getState().openFile(threadRef, workspaceFilePath); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | openInEditor(activeCwd ? resolvePathLinkTarget(workspaceFilePath, activeCwd) : workspaceFilePath); |
| 119 | } |