resolve relative paths in read_file tool

This commit is contained in:
Nate 2024-12-18 22:47:46 -08:00
parent 114b7f1bf6
commit f65704f6ad
2 changed files with 24 additions and 2 deletions

View file

@ -1,8 +1,13 @@
import { firstAbsUriMatch, getUriPathBasename } from "../../util/uri";
import { ToolImpl } from ".";
import { getUriPathBasename } from "../../util/uri";
export const readFileImpl: ToolImpl = async (args, extras) => {
const content = await extras.ide.readFile(args.filepath);
const firstUriMatch = await firstAbsUriMatch(
args.filepath,
await extras.ide.getWorkspaceDirs(),
);
const content = await extras.ide.readFile(firstUriMatch);
return [
{
name: getUriPathBasename(args.filepath),

View file

@ -188,3 +188,20 @@ export function getLastNPathParts(filepath: string, n: number): string {
}
return filepath.split(/[\\/]/).slice(-n).join("/");
}
export function firstAbsUriMatch(
relativePath: string,
workspaceDirUris: string[],
): string {
const cleanRelativePath = pathToUriPathSegment(relativePath);
return workspaceDirUris
.map((dirUri) => {
try {
return joinPathsToUri(dirUri, cleanRelativePath);
} catch {
return null;
}
})
.filter((uri): uri is string => uri !== null)[0];
}