Open History to see the last commit
1import { isAbsolutePath, isWindowsAbsolutePath } from "@t3tools/shared/path";
2
3export const WORKSPACE_BASENAME_LOOKUP_LIMIT = 25;
4
5// All callers open one panel; the newest click must win across callers.
6let latestLookupSequence = 0;
7
8export function claimWorkspaceBasenameLookup(): () => boolean {
9 latestLookupSequence += 1;
10 const claimed = latestLookupSequence;
11 return () => claimed === latestLookupSequence;
12}
13
14export interface WorkspaceEntryCandidate {
15 readonly path: string;
16 readonly kind: "file" | "directory";
17}
18
19function basenameOfPath(path: string): string {
20 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
21 return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
22}
23
24export function needsWorkspaceBasenameLookup(relativePath: string): boolean {
25 const trimmed = relativePath.trim();
26 return (
27 trimmed !== "." &&
28 trimmed !== ".." &&
29 trimmed.length > 0 &&
30 !trimmed.includes("/") &&
31 !trimmed.includes("\\")
32 );
33}
34
35export function pickWorkspaceBasenameMatch(
36 basename: string,
37 entries: ReadonlyArray<WorkspaceEntryCandidate>,
38): string | null {
39 const target = basename.trim();
40 if (!target) return null;
41 const files = entries.filter((entry) => entry.kind === "file");
42 const exact = files.find((entry) => basenameOfPath(entry.path) === target);
43 if (exact) return exact.path;
44 const folded = target.toLowerCase();
45 const foldedMatches = files.filter(
46 (entry) => basenameOfPath(entry.path).toLowerCase() === folded,
47 );
48 return foldedMatches.length === 1 ? (foldedMatches[0]?.path ?? null) : null;
49}
50
51// Repository-relative paths can repeat the project's suffix in subfolder projects.
52function stripRepeatedWorkspacePrefix(relativePath: string, cwd: string): string | null {
53 if (isAbsolutePath(relativePath)) return null;
54 const pathSegments = relativePath.split(/[\\/]+/);
55 const cwdSegments = cwd.split(/[\\/]+/).filter(Boolean);
56 const fold = isWindowsAbsolutePath(cwd)
57 ? (segment: string | undefined) => segment?.toLowerCase()
58 : (segment: string | undefined) => segment;
59 for (let count = Math.min(cwdSegments.length, pathSegments.length - 1); count > 0; count--) {
60 const prefix = cwdSegments.slice(-count);
61 if (prefix.every((segment, index) => fold(segment) === fold(pathSegments[index]))) {
62 return pathSegments.slice(count).join("/") || null;
63 }
64 }
65 return null;
66}
67
68/**
69 * The project search query that may find a chat file link's real file, or null when the link
70 * names its file exactly. Agents often cite paths relative to a package (`cloud/http.ts`).
71 */
72export function workspaceLinkLookupQuery(relativePath: string, cwd: string): string | null {
73 const trimmed = relativePath.trim();
74 if (isAbsolutePath(trimmed) || /^(?:\.{1,2}|~)[\\/]/.test(trimmed) || /[\\/]$/.test(trimmed)) {
75 return null;
76 }
77 if (needsWorkspaceBasenameLookup(trimmed)) return trimmed;
78 return stripRepeatedWorkspacePrefix(trimmed, cwd) ?? trimmed;
79}
80
81/** The one file a chat link names: its basename, its path past a repeated prefix, or its suffix. */
82export function pickWorkspaceLinkMatch(
83 relativePath: string,
84 cwd: string,
85 entries: ReadonlyArray<WorkspaceEntryCandidate>,
86): string | null {
87 const trimmed = relativePath.trim();
88 if (needsWorkspaceBasenameLookup(trimmed)) return pickWorkspaceBasenameMatch(trimmed, entries);
89 const stripped = stripRepeatedWorkspacePrefix(trimmed, cwd);
90 const strippedMatch =
91 stripped === null ? null : pickStrippedWorkspaceMatch(stripped, cwd, entries);
92 if (strippedMatch !== null) return strippedMatch;
93 const suffix = trimmed.replaceAll("\\", "/");
94 const matches = entries.filter(
95 (entry) =>
96 entry.kind === "file" &&
97 (entry.path.replaceAll("\\", "/") === suffix ||
98 entry.path.replaceAll("\\", "/").endsWith(`/${suffix}`)),
99 );
100 return matches.length === 1 ? (matches[0]?.path ?? null) : null;
101}
102
103function pickStrippedWorkspaceMatch(
104 strippedPath: string,
105 cwd: string,
106 entries: ReadonlyArray<WorkspaceEntryCandidate>,
107): string | null {
108 const caseInsensitive = isWindowsAbsolutePath(cwd);
109 const key = (path: string) => {
110 const normalized = path.replaceAll("\\", "/");
111 return caseInsensitive ? normalized.toLowerCase() : normalized;
112 };
113 const files = entries.filter((entry) => entry.kind === "file");
114 return files.find((entry) => key(entry.path) === key(strippedPath))?.path ?? null;
115}