inferResolvedUriFromRelativePath
This commit is contained in:
parent
7621abcdcc
commit
8113e1d1cb
7 changed files with 62 additions and 83 deletions
|
|
@ -1,6 +1,5 @@
|
|||
import { BranchAndDir, ContextItem, ContextProviderExtras } from "../../";
|
||||
import TransformersJsEmbeddingsProvider from "../../llm/llms/TransformersJsEmbeddingsProvider";
|
||||
import { resolveRelativePathInWorkspace } from "../../util/ideUtils";
|
||||
import { getUriPathBasename } from "../../util/uri";
|
||||
import { INSTRUCTIONS_BASE_ITEM } from "../providers/utils";
|
||||
|
||||
|
|
@ -70,14 +69,6 @@ export async function retrieveContextItemsFromEmbeddings(
|
|||
? RerankerRetrievalPipeline
|
||||
: NoRerankerRetrievalPipeline;
|
||||
|
||||
if (filterDirectory) {
|
||||
// Handle relative paths
|
||||
filterDirectory = await resolveRelativePathInWorkspace(
|
||||
filterDirectory,
|
||||
extras.ide,
|
||||
);
|
||||
}
|
||||
|
||||
const pipelineOptions: RetrievalPipelineOptions = {
|
||||
nFinal,
|
||||
nRetrieve,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { ContextItem, ContextProviderExtras } from "../..";
|
||||
import { contextProviderClassFromName } from "../../context/providers";
|
||||
import URLContextProvider from "../../context/providers/URLContextProvider";
|
||||
import { resolveRelativePathInWorkspace } from "../../util/ideUtils";
|
||||
import { resolveRelativePathInDir } from "../../util/ideUtils";
|
||||
import { getUriPathBasename } from "../../util/uri";
|
||||
import { getPreambleAndBody } from "./parse";
|
||||
|
||||
|
|
@ -26,10 +26,7 @@ async function resolveAttachment(
|
|||
}
|
||||
|
||||
// Files
|
||||
const resolvedFileUri = await resolveRelativePathInWorkspace(
|
||||
name,
|
||||
extras.ide,
|
||||
);
|
||||
const resolvedFileUri = await resolveRelativePathInDir(name, extras.ide);
|
||||
if (resolvedFileUri) {
|
||||
const content = `\`\`\`${name}\n${await extras.ide.readFile(resolvedFileUri)}\n\`\`\``;
|
||||
return [
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import generateRepoMap from "../../util/generateRepoMap";
|
||||
import { resolveRelativePathInWorkspace } from "../../util/ideUtils";
|
||||
import { resolveRelativePathInDir } from "../../util/ideUtils";
|
||||
|
||||
import { ToolImpl } from ".";
|
||||
|
||||
export const viewSubdirectoryImpl: ToolImpl = async (args: any, extras) => {
|
||||
const { directory_path } = args;
|
||||
const absoluteUri = await resolveRelativePathInWorkspace(
|
||||
const absoluteUri = await resolveRelativePathInDir(
|
||||
directory_path,
|
||||
extras.ide,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -40,6 +40,11 @@ class RepoMapGenerator {
|
|||
llm.contextLength * this.REPO_MAX_CONTEXT_LENGTH_RATIO;
|
||||
}
|
||||
|
||||
/*
|
||||
Note, the repo map is usually USED as relative file paths (passed to LLM)
|
||||
So for now, using URIs INPUTS to generate the map, but OUTPUTing relative paths
|
||||
The exception if requestFilesFromRepoMap, which now converts the relative paths back to URIs
|
||||
*/
|
||||
async generate(): Promise<string> {
|
||||
this.repoMapDirs = this.options.dirs ?? (await this.ide.getWorkspaceDirs());
|
||||
this.allPathsInDirs = await this.getAllPathsInDirs();
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
// Generated by continue
|
||||
|
||||
import { resolveRelativePathInWorkspace } from "./ideUtils";
|
||||
import { testIde } from "../test/fixtures";
|
||||
import { IDE } from "..";
|
||||
|
||||
describe("resolveRelativePathInWorkspace", () => {
|
||||
let mockIde: IDE;
|
||||
|
||||
beforeEach(() => {
|
||||
mockIde = testIde;
|
||||
});
|
||||
|
||||
it("should return the full path if the path is already absolute", async () => {
|
||||
const mockPath = "/absolute/path/to/file.txt";
|
||||
|
||||
const result = await resolveRelativePathInWorkspace(mockPath, mockIde);
|
||||
expect(result).toBe(mockPath);
|
||||
});
|
||||
|
||||
it("should resolve a relative path to a full path within workspace directories", async () => {
|
||||
const relativePath = "relative/path/to/file.txt";
|
||||
const workspaces = ["/workspace/one", "/workspace/two"];
|
||||
const expectedFullPath = `/workspace/one/${relativePath}`;
|
||||
|
||||
jest.spyOn(mockIde, "getWorkspaceDirs").mockResolvedValue(workspaces);
|
||||
jest
|
||||
.spyOn(mockIde, "fileExists")
|
||||
.mockImplementation(async (path) => path === expectedFullPath);
|
||||
|
||||
const result = await resolveRelativePathInWorkspace(relativePath, mockIde);
|
||||
expect(result).toBe(expectedFullPath);
|
||||
});
|
||||
|
||||
it("should return undefined if the relative path does not exist in any workspace directory", async () => {
|
||||
const relativePath = "non/existent/path.txt";
|
||||
const workspaces = ["/workspace/one", "/workspace/two"];
|
||||
|
||||
jest.spyOn(mockIde, "getWorkspaceDirs").mockResolvedValue(workspaces);
|
||||
jest.spyOn(mockIde, "fileExists").mockResolvedValue(false);
|
||||
|
||||
const result = await resolveRelativePathInWorkspace(relativePath, mockIde);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,18 +1,19 @@
|
|||
import { IDE } from "..";
|
||||
import { findUriInDirs, joinPathsToUri } from "./uri";
|
||||
import { joinPathsToUri, pathToUriPathSegment } from "./uri";
|
||||
|
||||
/*
|
||||
This function takes a relative filepath
|
||||
This function takes a relative (to workspace) filepath
|
||||
And checks each workspace for if it exists or not
|
||||
Only returns fully resolved URI if it exists
|
||||
*/
|
||||
export async function resolveRelativePathInWorkspace(
|
||||
export async function resolveRelativePathInDir(
|
||||
path: string,
|
||||
ide: IDE,
|
||||
dirUriCandidates?: string[],
|
||||
): Promise<string | undefined> {
|
||||
const workspaces = await ide.getWorkspaceDirs();
|
||||
for (const workspaceUri of workspaces) {
|
||||
const fullUri = joinPathsToUri(workspaceUri, path);
|
||||
const dirs = dirUriCandidates ?? (await ide.getWorkspaceDirs());
|
||||
for (const dirUri of dirs) {
|
||||
const fullUri = joinPathsToUri(dirUri, path);
|
||||
if (await ide.fileExists(fullUri)) {
|
||||
return fullUri;
|
||||
}
|
||||
|
|
@ -22,24 +23,55 @@ export async function resolveRelativePathInWorkspace(
|
|||
}
|
||||
|
||||
/*
|
||||
This function takes a relative filepath (which may not exist)
|
||||
And, based on which workspace has the closest matching path
|
||||
Guesses which workspace and returns resolved URI
|
||||
|
||||
Original use case of for tools trying to create new files
|
||||
If no meaninful path match just concatenates to first workspace's uri
|
||||
Same as above but in this case the relative path does not need to exist (e.g. file to be created, etc)
|
||||
Checks closes match with the dirs, path segment by segment
|
||||
and based on which workspace has the closest matching path, returns resolved URI
|
||||
If no meaninful path match just concatenates to first dir's uri
|
||||
*/
|
||||
export async function inferResolvedUriFromRelativePath(
|
||||
path: string,
|
||||
ide: IDE,
|
||||
dirCandidates?: string[],
|
||||
): Promise<string> {
|
||||
// for (const workspaceUri of workspaceDirs) {
|
||||
// if (isUriWithinDirectory(path))
|
||||
// const fullUri = joinPathsToUri(workspaceUri, path);
|
||||
// if (await ide.fileExists(fullUri)) {
|
||||
// return fullUri;
|
||||
// }
|
||||
// }
|
||||
// // console.warn("No meaninful filepath inferred from relative path " + path)
|
||||
// return `${workspaces[0]}/${cleanPath}`;
|
||||
const dirs = dirCandidates ?? (await ide.getWorkspaceDirs());
|
||||
if (dirs.length === 0) {
|
||||
throw new Error("inferResolvedUriFromRelativePath: no dirs provided");
|
||||
}
|
||||
const segments = pathToUriPathSegment(path).split("/");
|
||||
|
||||
// Generate all possible suffixes from shortest to longest
|
||||
const suffixes: string[] = [];
|
||||
for (let i = segments.length - 1; i >= 0; i--) {
|
||||
suffixes.push(segments.slice(i).join("/"));
|
||||
}
|
||||
|
||||
// For each suffix, try to find a unique matching directory
|
||||
for (const suffix of suffixes) {
|
||||
const uris = dirs.map((dir) => ({
|
||||
dir,
|
||||
partialUri: joinPathsToUri(dir, suffix),
|
||||
}));
|
||||
const existenceChecks = await Promise.all(
|
||||
uris.map(async ({ partialUri, dir }) => ({
|
||||
dir,
|
||||
partialUri,
|
||||
exists: await ide.fileExists(partialUri),
|
||||
})),
|
||||
);
|
||||
|
||||
const existingUris = existenceChecks.filter(({ exists }) => exists);
|
||||
|
||||
// If exactly one directory matches, use it
|
||||
if (existingUris.length === 1) {
|
||||
return joinPathsToUri(existingUris[0].dir, segments.join("/"));
|
||||
}
|
||||
}
|
||||
|
||||
// If no unique match found, use the first directory
|
||||
return joinPathsToUri(dirs[0], segments.join("/"));
|
||||
}
|
||||
|
||||
interface ResolveResult {
|
||||
resolvedUri: string;
|
||||
matchedDir: string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ import { stripImages } from "core/util/messageContent";
|
|||
import { IIdeMessenger } from "../../context/IdeMessenger";
|
||||
import { Dispatch } from "@reduxjs/toolkit";
|
||||
import { setIsGatheringContext } from "../../redux/slices/sessionSlice";
|
||||
import { inferResolvedUriFromRelativePath } from "core/util/ideUtils";
|
||||
import { ctxItemToRifWithContents } from "core/commands/util";
|
||||
import { getUriFileExtension } from "core/util/uri";
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue