remove listfolders and walkdirinworkspaces

This commit is contained in:
Dallin Romney 2024-12-12 15:12:09 -08:00
parent 01efdfeb6e
commit 5fbace715d
21 changed files with 89 additions and 116 deletions

View file

@ -1,6 +1,10 @@
// Fill in the middle prompts
import { CompletionOptions } from "../../index.js";
import {
getLastNUriRelativePathParts,
shortestRelativeUriPaths,
} from "../../util/uri.js";
import {
AutocompleteCodeSnippet,
AutocompleteSnippet,
@ -14,7 +18,7 @@ export interface AutocompleteTemplate {
filepath: string,
reponame: string,
snippets: AutocompleteSnippet[],
workspaceDirs: string[],
workspaceUris: string[],
) => [string, string];
template:
| string
@ -25,7 +29,7 @@ export interface AutocompleteTemplate {
reponame: string,
language: string,
snippets: AutocompleteSnippet[],
workspaceDirs: [],
workspaceUris: string[],
) => string);
completionOptions?: Partial<CompletionOptions>;
}
@ -79,21 +83,27 @@ const codestralMultifileFimTemplate: AutocompleteTemplate = {
filepath,
reponame,
snippets,
workspaceDirs,
workspaceUris,
): [string, string] => {
if (snippets.length === 0) {
if (suffix.trim().length === 0 && prefix.trim().length === 0) {
return [`+++++ ${getLastNPathParts(filepath, 2)}\n${prefix}`, suffix];
return [
`+++++ ${getLastNUriRelativePathParts(workspaceUris, filepath, 2)}\n${prefix}`,
suffix,
];
}
return [prefix, suffix];
}
const relativePaths = shortestRelativePaths([
...snippets.map((snippet) =>
"filepath" in snippet ? snippet.filepath : "Untitled.txt",
),
filepath,
]);
const relativePaths = shortestRelativeUriPaths(
[
...snippets.map((snippet) =>
"filepath" in snippet ? snippet.filepath : "Untitled.txt",
),
filepath,
],
workspaceUris,
);
const otherFiles = snippets
.map((snippet, i) => {
@ -144,7 +154,7 @@ const starcoder2FimTemplate: AutocompleteTemplate = {
reponame,
language,
snippets,
workspaceDirs,
workspaceUris,
): string => {
const otherFiles =
snippets.length === 0
@ -198,16 +208,16 @@ const codegeexFimTemplate: AutocompleteTemplate = {
reponame,
language,
allSnippets,
workspaceDirs,
workspaceUris,
): string => {
const snippets = allSnippets.filter(
(snippet) => snippet.type === AutocompleteSnippetType.Code,
) as AutocompleteCodeSnippet[];
const relativePaths = shortestRelativePaths([
...snippets.map((snippet) => snippet.filepath),
filepath,
]);
const relativePaths = shortestRelativeUriPaths(
[...snippets.map((snippet) => snippet.filepath), filepath],
workspaceUris,
);
const baseTemplate = `###PATH:${
relativePaths[relativePaths.length - 1]
}\n###LANGUAGE:${language}\n###MODE:BLOCK\n<|code_suffix|>${suffix}<|code_prefix|>${prefix}<|code_middle|>`;

View file

@ -79,6 +79,7 @@ export function renderPrompt({
helper.filepath,
reponame,
snippets,
helper.workspaceUris,
);
} else {
const formattedSnippets = formatSnippets(helper, snippets, workspaceDirs);
@ -103,6 +104,7 @@ export function renderPrompt({
reponame,
helper.lang.name,
snippets,
helper.workspaceUris,
);
const stopTokens = getStopTokens(

View file

@ -20,6 +20,7 @@ import { AutocompleteInput } from "./types";
export class HelperVars {
lang: AutocompleteLanguageInfo;
treePath: AstPath | undefined;
workspaceUris: string[] = [];
private _fileContents: string | undefined;
private _fileLines: string[] | undefined;
@ -43,6 +44,8 @@ export class HelperVars {
return;
}
this.workspaceUris = await this.ide.getWorkspaceDirs();
this._fileContents =
this.input.manuallyPassFileContents ??
(await this.ide.readFile(this.filepath));

View file

@ -607,8 +607,6 @@ declare global {
getAvailableThreads(): Promise<Thread[]>;
listFolders(): Promise<string[]>;
getWorkspaceDirs(): Promise<string[]>;
getWorkspaceConfigs(): Promise<ContinueRcJson[]>;

View file

@ -6,7 +6,7 @@ import {
ContextSubmenuItem,
LoadSubmenuItemsArgs,
} from "../../";
import { walkDir } from "../../indexing/walkDir";
import { walkDirInWorkspaces } from "../../indexing/walkDir";
import {
getUniqueUriPath,
getUriPathBasename,
@ -46,20 +46,15 @@ class FileContextProvider extends BaseContextProvider {
async loadSubmenuItems(
args: LoadSubmenuItemsArgs,
): Promise<ContextSubmenuItem[]> {
const workspaceDirs = await args.ide.getWorkspaceDirs();
const results = await Promise.all(
workspaceDirs.map((dir) => {
return walkDir(dir, args.ide);
}),
);
const results = await walkDirInWorkspaces(args.ide);
const files = results.flat().slice(-MAX_SUBMENU_ITEMS);
const fileGroups = groupByLastNPathParts(files, 2);
return files.map((file) => {
return files.map((uri) => {
return {
id: file,
title: getUriPathBasename(file),
description: getUniqueUriPath(file, fileGroups),
id: uri,
title: getUriPathBasename(uri),
description: getUniqueUriPath(uri, fileGroups),
};
});
}

View file

@ -5,12 +5,14 @@ import {
ContextSubmenuItem,
LoadSubmenuItemsArgs,
} from "../../index.js";
import { walkDirInWorkspaces } from "../../indexing/walkDir.js";
import {
getUniqueUriPath,
getUriPathBasename,
groupByLastNPathParts,
} from "../../util/uri.js";
import { BaseContextProvider } from "../index.js";
import { retrieveContextItemsFromEmbeddings } from "../retrieval/retrieval.js";
class FolderContextProvider extends BaseContextProvider {
static description: ContextProviderDescription = {
@ -25,15 +27,14 @@ class FolderContextProvider extends BaseContextProvider {
query: string,
extras: ContextProviderExtras,
): Promise<ContextItem[]> {
const { retrieveContextItemsFromEmbeddings } = await import(
"../retrieval/retrieval.js"
);
return retrieveContextItemsFromEmbeddings(extras, this.options, query);
}
async loadSubmenuItems(
args: LoadSubmenuItemsArgs,
): Promise<ContextSubmenuItem[]> {
const folders = await args.ide.listFolders();
const folders = await walkDirInWorkspaces(args.ide, {
onlyDirs: true,
});
const folderGroups = groupByLastNPathParts(folders, 2);
return folders.map((folder) => {

View file

@ -6,6 +6,7 @@ import {
ContextSubmenuItem,
LoadSubmenuItemsArgs,
} from "../../";
import { walkDirInWorkspaces } from "../../indexing/walkDir";
import generateRepoMap from "../../util/generateRepoMap";
import {
getUniqueUriPath,
@ -45,7 +46,9 @@ class RepoMapContextProvider extends BaseContextProvider {
async loadSubmenuItems(
args: LoadSubmenuItemsArgs,
): Promise<ContextSubmenuItem[]> {
const folders = await args.ide.listFolders();
const folders = await walkDirInWorkspaces(args.ide, {
onlyDirs: true,
});
const folderGroups = groupByLastNPathParts(folders, 2);
return [

2
core/index.d.ts vendored
View file

@ -605,8 +605,6 @@ export interface IDE {
getAvailableThreads(): Promise<Thread[]>;
listFolders(): Promise<string[]>;
getWorkspaceDirs(): Promise<string[]>;
getWorkspaceConfigs(): Promise<ContinueRcJson[]>;

View file

@ -6,7 +6,8 @@ import {
PackageDetails,
ParsedPackageInfo,
} from "../../..";
import { walkDir } from "../../walkDir";
import { getUriPathBasename } from "../../../util/uri";
import { walkDir, walkDirInWorkspaces } from "../../walkDir";
import { PythonPackageCrawler } from "./packageCrawlers/Python";
import { NodePackageCrawler } from "./packageCrawlers/TsJs";
@ -24,16 +25,10 @@ export interface PackageCrawler {
}
export async function getAllSuggestedDocs(ide: IDE) {
const workspaceDirs = await ide.getWorkspaceDirs();
const results = await Promise.all(
workspaceDirs.map((dir) => {
return walkDir(dir, ide);
}),
);
const allPaths = results.flat(); // TODO only get files, not dirs. Not critical for now
const allFiles = allPaths.map((path) => ({
path,
name: path.split(/[\\/]/).pop()!,
const allFileUris = await walkDirInWorkspaces(ide);
const allFiles = allFileUris.map((uri) => ({
path: uri,
name: getUriPathBasename(uri),
}));
// Build map of language -> package files

View file

@ -15,7 +15,7 @@ async function walkTestDir(
options?: WalkerOptions,
): Promise<string[] | undefined> {
return walkDir(TEST_DIR, ide, {
returnRelativeUris: true,
returnRelativeUrisPaths: true,
...options,
});
}
@ -259,7 +259,7 @@ describe("walkDir", () => {
[path.join(TEST_DIR, "a.txt"), path.join(TEST_DIR, "b", "c.txt")],
[],
{
returnRelativeUris: false,
returnRelativeUrisPaths: false,
},
);
});

View file

@ -15,7 +15,7 @@ import { joinPathsToUri } from "../util/uri.js";
export interface WalkerOptions {
ignoreFiles?: string[];
onlyDirs?: boolean;
returnRelativeUris?: boolean;
returnRelativeUrisPaths?: boolean;
additionalIgnoreRules?: string[];
}
@ -23,7 +23,7 @@ type Entry = [string, FileType];
// helper struct used for the DFS walk
type WalkableEntry = {
relPath: string;
relativeUriPath: string;
uri: string;
type: FileType;
entry: Entry;
@ -72,11 +72,19 @@ class DFSWalker {
});
if (this.options.onlyDirs) {
// when onlyDirs is enabled the walker will only return directory names
yield w.relPath + "/";
if (this.options.returnRelativeUrisPaths) {
yield w.relativeUriPath;
} else {
yield w.uri;
}
}
} else {
// Note that shouldInclude handles skipping files if options.onlyDirs is true
yield w.relPath;
if (this.options.returnRelativeUrisPaths) {
yield w.relativeUriPath;
} else {
yield w.uri;
}
}
}
}
@ -86,7 +94,7 @@ class DFSWalker {
const globalIgnoreFile = getGlobalContinueIgArray();
return {
walkableEntry: {
relPath: "",
relativeUriPath: "",
uri: this.uri,
type: 2 as FileType.Directory,
entry: ["", 2 as FileType.Directory],
@ -108,7 +116,7 @@ class DFSWalker {
): Promise<WalkableEntry[]> {
const entries = await this.ide.listDir(walkableEntry.uri);
return entries.map((e) => ({
relPath: `${walkableEntry.relPath}/${e[0]}`,
relativeUriPath: `${walkableEntry.relativeUriPath}/${e[0]}`,
uri: joinPathsToUri(walkableEntry.uri, e[0]),
type: e[1],
entry: e,
@ -126,7 +134,7 @@ class DFSWalker {
const patterns = ignoreFilesInDir.map((c) => gitIgArrayFromFile(c)).flat();
const newIgnoreContext = {
ignore: ignore().add(patterns),
dirname: curDir.walkableEntry.relPath,
dirname: curDir.walkableEntry.relativeUriPath,
};
return [...curDir.ignoreContexts, newIgnoreContext];
}
@ -151,7 +159,7 @@ class DFSWalker {
// we do not want to index it
return false;
}
let relPath = walkableEntry.relPath;
let relPath = walkableEntry.relativeUriPath;
if (this.entryIsDirectory(walkableEntry.entry)) {
relPath = `${relPath}/`;
} else {
@ -192,7 +200,7 @@ const defaultOptions: WalkerOptions = {
ignoreFiles: [".gitignore", ".continueignore"],
additionalIgnoreRules: [...DEFAULT_IGNORE_DIRS, ...DEFAULT_IGNORE_FILETYPES],
onlyDirs: false,
returnRelativeUris: false,
returnRelativeUrisPaths: false,
};
export async function walkDir(
@ -215,3 +223,14 @@ export async function* walkDirAsync(
const options = { ...defaultOptions, ..._optionOverrides };
yield* new DFSWalker(path, ide, options).walk();
}
export async function walkDirInWorkspaces(
ide: IDE,
_optionOverrides?: WalkerOptions,
): Promise<string[]> {
const workspaceDirs = await ide.getWorkspaceDirs();
const results = await Promise.all(
workspaceDirs.map((dir) => walkDir(dir, ide, _optionOverrides)),
);
return results.flat();
}

View file

@ -23,7 +23,6 @@ export type ToIdeFromWebviewOrCoreProtocol = {
// Methods from IDE type
getIdeInfo: [undefined, IdeInfo];
getWorkspaceDirs: [undefined, string[]];
listFolders: [undefined, string[]];
writeFile: [{ path: string; contents: string }, void];
showVirtualFile: [{ name: string; content: string }, void];
openFile: [{ path: string }, void];

View file

@ -134,10 +134,6 @@ export class MessageIde implements IDE {
});
}
async listFolders(): Promise<string[]> {
return await this.request("listFolders", undefined);
}
async writeFile(fileUri: string, contents: string): Promise<void> {
await this.request("writeFile", { path: fileUri, contents });
}

View file

@ -117,10 +117,6 @@ export class ReverseMessageIde {
return this.ide.showLines(data.filepath, data.startLine, data.endLine);
});
this.on("listFolders", () => {
return this.ide.listFolders();
});
this.on("getControlPlaneSessionInfo", async (msg) => {
// Not supported in testing
return undefined;

View file

@ -166,10 +166,6 @@ class FileSystemIde implements IDE {
return Promise.resolve([this.workspaceDir]);
}
listFolders(): Promise<string[]> {
return Promise.resolve([]);
}
writeFile(fileUri: string, contents: string): Promise<void> {
const filepath = fileURLToPath(fileUri);
return new Promise((resolve, reject) => {

View file

@ -82,7 +82,7 @@ export function getLastNUriRelativePathParts(
n: number,
): string {
const path = getRelativePath(uri, workspaceDirs);
return getLastN(path, n);
return getLastNPathParts(path, n);
}
export function getUriPathBasename(uri: string): string {
@ -116,7 +116,7 @@ export function getUniqueUriPath(
export function shortestRelativeUriPaths(
uris: string[],
directory: string,
workspaceUris: string[],
): string[] {
if (uris.length === 0) {
return [];

View file

@ -18,7 +18,6 @@ class MessageTypes {
"getTerminalContents",
"getWorkspaceDirs",
"showLines",
"listFolders",
"writeFile",
"fileExists",
"showVirtualFile",

View file

@ -582,21 +582,6 @@ class IdeProtocolClient(
respond(result)
}
"listFolders" -> {
val workspacePath = workspacePath ?: return@launch
val folders = mutableListOf<String>()
fun findNestedFolders(dirPath: String) {
val dir = File(dirPath)
val nestedFolders =
dir.listFiles { file -> file.isDirectory }?.map { file -> file.absolutePath }
?: emptyList()
folders.addAll(nestedFolders);
nestedFolders.forEach { folder -> findNestedFolders(folder) }
}
findNestedFolders(workspacePath)
respond(folders)
}
"getSearchResults" -> {
val query = (data as Map<String, Any>)["query"] as String
respond(search(query))

View file

@ -351,18 +351,6 @@ class VsCodeIde implements IDE {
return configs;
}
async listFolders(): Promise<string[]> {
const allDirs: string[] = [];
const workspaceDirs = await this.getWorkspaceDirs();
for (const directory of workspaceDirs) {
const dirs = await walkDir(directory, this, { onlyDirs: true });
allDirs.push(...dirs);
}
return allDirs;
}
async getWorkspaceDirs(): Promise<string[]> {
return this.ideUtils.getWorkspaceDirectories().map((uri) => uri.toString());
}

View file

@ -413,9 +413,6 @@ export class VsCodeMessenger {
this.onWebviewOrCore("getWorkspaceDirs", async (msg) => {
return ide.getWorkspaceDirs();
});
this.onWebviewOrCore("listFolders", async (msg) => {
return ide.listFolders();
});
this.onWebviewOrCore("writeFile", async (msg) => {
return ide.writeFile(msg.data.path, msg.data.contents);
});

View file

@ -1,5 +1,5 @@
import { IDE } from "core";
import { walkDir } from "core/indexing/walkDir";
import { walkDirInWorkspaces } from "core/indexing/walkDir";
// @ts-ignore
import MiniSearch from "minisearch";
import * as vscode from "vscode";
@ -24,18 +24,11 @@ export class FileSearch {
},
});
private async initializeFileSearchState() {
const workspaceDirs = await this.ide.getWorkspaceDirs();
const results = await Promise.all(
workspaceDirs.map((dir) => {
return walkDir(dir, this.ide);
}),
);
const results = await walkDirInWorkspaces(this.ide);
this.miniSearch.addAll(
results.flat().map((file) => ({
id: file,
relativePath: vscode.workspace.asRelativePath(file),
results.flat().map((uri) => ({
id: uri,
relativePath: vscode.workspace.asRelativePath(uri),
})),
);
}