fix(coding-agent): disambiguate resource paths
This commit is contained in:
@@ -10,6 +10,7 @@ import { keyHint, keyText } from "../../modes/interactive/components/keybinding-
|
||||
import { getLanguageFromPath, highlightCode, type Theme } from "../../modes/interactive/theme/theme.js";
|
||||
import { formatDimensionNote, resizeImage } from "../../utils/image-resize.js";
|
||||
import { detectSupportedImageMimeTypeFromFile } from "../../utils/mime.js";
|
||||
import { formatPathRelativeToCwdOrAbsolute } from "../../utils/paths.js";
|
||||
import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";
|
||||
import { resolveReadPath } from "./path-utils.js";
|
||||
import { getTextOutput, invalidArgText, replaceTabs, shortenPath, str } from "./render-utils.js";
|
||||
@@ -133,7 +134,7 @@ function getCompactReadClassification(
|
||||
if (docsClassification) return docsClassification;
|
||||
|
||||
if (COMPACT_RESOURCE_FILE_NAMES.has(fileName)) {
|
||||
return { kind: "resource", label: fileName };
|
||||
return { kind: "resource", label: formatPathRelativeToCwdOrAbsolute(absolutePath, cwd) };
|
||||
}
|
||||
|
||||
return undefined;
|
||||
|
||||
@@ -85,6 +85,7 @@ import { getChangelogPath, getNewEntries, parseChangelog } from "../../utils/cha
|
||||
import { copyToClipboard } from "../../utils/clipboard.js";
|
||||
import { extensionForImageMimeType, readClipboardImage } from "../../utils/clipboard-image.js";
|
||||
import { parseGitUrl } from "../../utils/git.js";
|
||||
import { getCwdRelativePath } from "../../utils/paths.js";
|
||||
import { getPiUserAgent } from "../../utils/pi-user-agent.js";
|
||||
import { killTrackedDetachedChildren } from "../../utils/shell.js";
|
||||
import { ensureTool } from "../../utils/tools-manager.js";
|
||||
@@ -911,15 +912,9 @@ export class InteractiveMode {
|
||||
private formatContextPath(p: string): string {
|
||||
const cwd = path.resolve(this.sessionManager.getCwd());
|
||||
const absolutePath = path.isAbsolute(p) ? path.resolve(p) : path.resolve(cwd, p);
|
||||
const relativePath = path.relative(cwd, absolutePath);
|
||||
const isInsideCwd =
|
||||
relativePath === "" ||
|
||||
(!relativePath.startsWith("..") &&
|
||||
!relativePath.startsWith(`..${path.sep}`) &&
|
||||
!path.isAbsolute(relativePath));
|
||||
|
||||
if (isInsideCwd) {
|
||||
return relativePath || ".";
|
||||
const relativePath = getCwdRelativePath(absolutePath, cwd);
|
||||
if (relativePath !== undefined) {
|
||||
return relativePath;
|
||||
}
|
||||
|
||||
return this.formatDisplayPath(absolutePath);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { realpathSync } from "node:fs";
|
||||
import { isAbsolute, relative, resolve as resolvePath, sep } from "node:path";
|
||||
|
||||
/**
|
||||
* Resolve a path to its canonical (real) form, following symlinks.
|
||||
@@ -34,3 +35,23 @@ export function isLocalPath(value: string): boolean {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function resolveAgainstCwd(filePath: string, cwd: string): string {
|
||||
return isAbsolute(filePath) ? resolvePath(filePath) : resolvePath(cwd, filePath);
|
||||
}
|
||||
|
||||
export function getCwdRelativePath(filePath: string, cwd: string): string | undefined {
|
||||
const resolvedCwd = resolvePath(cwd);
|
||||
const resolvedPath = resolveAgainstCwd(filePath, resolvedCwd);
|
||||
const relativePath = relative(resolvedCwd, resolvedPath);
|
||||
const isInsideCwd =
|
||||
relativePath === "" ||
|
||||
(relativePath !== ".." && !relativePath.startsWith(`..${sep}`) && !isAbsolute(relativePath));
|
||||
|
||||
return isInsideCwd ? relativePath || "." : undefined;
|
||||
}
|
||||
|
||||
export function formatPathRelativeToCwdOrAbsolute(filePath: string, cwd: string): string {
|
||||
const absolutePath = resolveAgainstCwd(filePath, cwd);
|
||||
return (getCwdRelativePath(absolutePath, cwd) ?? absolutePath).split(sep).join("/");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user