Full project file path in context (#1407)

* Full workspace file path when using @Open Files

* Full project file path for @Files and 'active file' context

* FileTreeContextProvider uses splitPath from util instead of local copy

* relative-to-root file paths for rag retrieval
This commit is contained in:
Tijs Zwinkels 2024-06-01 22:54:42 +02:00 • committed by GitHub
parent 0f20f9d84a
commit 6b200ba15b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 35 additions and 17 deletions

View file

@ -4,6 +4,7 @@ import {
ContextProviderExtras,
} from "../../index.js";
import { BaseContextProvider } from "../index.js";
import { splitPath } from "../../util/index.js";
interface Directory {
name: string;
@ -11,15 +12,6 @@ interface Directory {
directories: Directory[];
}
function splitPath(path: string, withRoot?: string): string[] {
let parts = path.includes("/") ? path.split("/") : path.split("\\");
if (withRoot !== undefined) {
const rootParts = splitPath(withRoot);
parts = parts.slice(rootParts.length - 1);
}
return parts;
}
function formatFileTree(tree: Directory, indentation = ""): string {
let result = "";
for (const file of tree.files) {

View file

@ -3,7 +3,7 @@ import {
ContextProviderDescription,
ContextProviderExtras,
} from "../../index.js";
import { getBasename } from "../../util/index.js";
import { getBasename, getRelativePath } from "../../util/index.js";
import { BaseContextProvider } from "../index.js";
class OpenFilesContextProvider extends BaseContextProvider {
@ -26,7 +26,7 @@ class OpenFilesContextProvider extends BaseContextProvider {
openFiles.map(async (filepath: string) => {
return {
description: filepath,
content: `\`\`\`${getBasename(filepath)}\n${await ide.readFile(
content: `\`\`\`${await getRelativePath(filepath, await extras.ide.getWorkspaceDirs())}\n${await ide.readFile(
filepath,
)}\n\`\`\``,
name: (filepath.split("/").pop() ?? "").split("\\").pop() ?? "",

View file

@ -6,7 +6,7 @@ import {
} from "../../index.js";
import { LanceDbIndex } from "../../indexing/LanceDbIndex.js";
import { deduplicateArray, getBasename } from "../../util/index.js";
import { deduplicateArray, getRelativePath } from "../../util/index.js";
import { RETRIEVAL_PARAMS } from "../../util/parameters.js";
import { retrieveFts } from "./fullTextSearch.js";
@ -149,7 +149,7 @@ export async function retrieveContextItemsFromEmbeddings(
return [
...results.map((r) => {
const name = `${getBasename(r.filepath)} (${r.startLine}-${r.endLine})`;
const name = `${getRelativePath(r.filepath, workspaceDirs)} (${r.startLine}-${r.endLine})`;
const description = `${r.filepath} (${r.startLine}-${r.endLine})`;
return {
name,

View file

@ -1,3 +1,7 @@
import {
ContextProviderExtras,
} from "../index.js";
export function removeQuotesAndEscapes(output: string): string {
output = output.trim();
@ -97,6 +101,26 @@ export function getLastNPathParts(filepath: string, n: number): string {
return filepath.split(/[\\/]/).slice(-n).join("/");
}
export function getRelativePath(filepath: string, workspaceDirs: string[]): string {
for (const workspaceDir of workspaceDirs) {
const filepathParts = splitPath(filepath);
const workspaceDirParts = splitPath(workspaceDir);
if (filepathParts.slice(0, workspaceDirParts.length).join('/') === workspaceDirParts.join('/')) {
return filepathParts.slice(workspaceDirParts.length).join('/');
}
}
return splitPath(filepath).pop() ?? ''; // If the file is not in any of the workspaces, return the plain filename
}
export function splitPath(path: string, withRoot?: string): string[] {
let parts = path.includes("/") ? path.split("/") : path.split("\\");
if (withRoot !== undefined) {
const rootParts = splitPath(withRoot);
parts = parts.slice(rootParts.length - 1);
}
return parts;
}
export function getMarkdownLanguageTagForFile(filepath: string): string {
const ext = filepath.split(".").pop();
switch (ext) {

View file

@ -7,7 +7,7 @@ import {
RangeInFile,
} from "core";
import { stripImages } from "core/llm/countTokens";
import { getBasename } from "core/util";
import { getBasename, getRelativePath } from "core/util";
import { IIdeMessenger } from "../../context/IdeMessenger";
interface MentionAttrs {
@ -96,8 +96,9 @@ async function resolveEditorContent(
if (item.itemType === "file") {
// This is a quick way to resolve @file references
const basename = getBasename(item.id);
const relativeFilePath = getRelativePath(item.id, await ideMessenger.ide.getWorkspaceDirs());
const rawContent = await ideMessenger.ide.readFile(item.id);
const content = `\`\`\`title="${basename}"\n${rawContent}\n\`\`\`\n`;
const content = `\`\`\`${relativeFilePath}\n${rawContent}\n\`\`\`\n`;
contextItemsText += content;
contextItems.push({
name: basename,

View file

@ -13,7 +13,7 @@ import {
} from "core";
import { constructMessages } from "core/llm/constructMessages";
import { stripImages } from "core/llm/countTokens";
import { getBasename } from "core/util";
import { getBasename, getRelativePath } from "core/util";
import { usePostHog } from "posthog-js/react";
import { useEffect, useRef } from "react";
import { useSelector } from "react-redux";
@ -179,8 +179,9 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
.join("\n");
}
contextItems.unshift({
content: `The following file is currently open. Don't reference it if it's not relevant to the user's message.\n\n\`\`\`${getBasename(
content: `The following file is currently open. Don't reference it if it's not relevant to the user's message.\n\n\`\`\`${getRelativePath(
currentFilePath,
await ideMessenger.ide.getWorkspaceDirs(),
)}\n${currentFileContents}\n\`\`\``,
name: `Active file: ${getBasename(currentFilePath)}`,
description: currentFilePath,