Merge pull request #3893 from continuedev/dallin/no-uris-to-model
Don't send uris to model, fix jetbrains current file
This commit is contained in:
commit
1f573260d7
10 changed files with 141 additions and 72 deletions
|
|
@ -24,8 +24,12 @@ class CodeContextProvider extends BaseContextProvider {
|
|||
extras: ContextProviderExtras,
|
||||
): Promise<ContextItem[]> {
|
||||
// Assume the query is the id as returned by loadSubmenuItems
|
||||
const workspaceDirs = await extras.ide.getWorkspaceDirs();
|
||||
return [
|
||||
await CodeSnippetsCodebaseIndex.getForId(Number.parseInt(query, 10)),
|
||||
await CodeSnippetsCodebaseIndex.getForId(
|
||||
Number.parseInt(query, 10),
|
||||
workspaceDirs,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import {
|
|||
ContextProviderDescription,
|
||||
ContextProviderExtras,
|
||||
} from "../../";
|
||||
import { getUriPathBasename } from "../../util/uri";
|
||||
import { getUriDescription } from "../../util/uri";
|
||||
|
||||
class CurrentFileContextProvider extends BaseContextProvider {
|
||||
static description: ContextProviderDescription = {
|
||||
|
|
@ -23,12 +23,27 @@ class CurrentFileContextProvider extends BaseContextProvider {
|
|||
if (!currentFile) {
|
||||
return [];
|
||||
}
|
||||
const baseName = getUriPathBasename(currentFile.path);
|
||||
|
||||
const { relativePathOrBasename, last2Parts, baseName } = getUriDescription(
|
||||
currentFile.path,
|
||||
await extras.ide.getWorkspaceDirs(),
|
||||
);
|
||||
|
||||
let prefix = "This is the currently open file:";
|
||||
let name = baseName;
|
||||
|
||||
// This allows frontend to retrieve when using alt + enter or default context with slightly different copy
|
||||
if (query === "non-mention-usage") {
|
||||
prefix =
|
||||
"The following file is currently open. Don't reference it if it's not relevant to the user's message:";
|
||||
name = "Active file: " + baseName;
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
description: currentFile.path,
|
||||
content: `This is the currently open file:\n\n\`\`\`${baseName}\n${currentFile.contents}\n\`\`\``,
|
||||
name: baseName,
|
||||
description: last2Parts,
|
||||
content: `${prefix}\n\n\`\`\`${relativePathOrBasename}\n${currentFile.contents}\n\`\`\``,
|
||||
name,
|
||||
uri: {
|
||||
type: "file",
|
||||
value: currentFile.path,
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import { walkDirs } from "../../indexing/walkDir";
|
|||
import {
|
||||
getUriPathBasename,
|
||||
getShortestUniqueRelativeUriPaths,
|
||||
getUriDescription,
|
||||
} from "../../util/uri";
|
||||
|
||||
const MAX_SUBMENU_ITEMS = 10_000;
|
||||
|
|
@ -27,16 +28,22 @@ class FileContextProvider extends BaseContextProvider {
|
|||
extras: ContextProviderExtras,
|
||||
): Promise<ContextItem[]> {
|
||||
// Assume the query is a filepath
|
||||
query = query.trim();
|
||||
const content = await extras.ide.readFile(query);
|
||||
const fileUri = query.trim();
|
||||
const content = await extras.ide.readFile(fileUri);
|
||||
|
||||
const { relativePathOrBasename, last2Parts, baseName } = getUriDescription(
|
||||
fileUri,
|
||||
await extras.ide.getWorkspaceDirs(),
|
||||
);
|
||||
|
||||
return [
|
||||
{
|
||||
name: query.split(/[\\/]/).pop() ?? query,
|
||||
description: query,
|
||||
content: `\`\`\`${query}\n${content}\n\`\`\``,
|
||||
name: baseName,
|
||||
description: last2Parts,
|
||||
content: `\`\`\`${relativePathOrBasename}\n${content}\n\`\`\``,
|
||||
uri: {
|
||||
type: "file",
|
||||
value: query,
|
||||
value: fileUri,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import {
|
|||
ContextProviderDescription,
|
||||
ContextProviderExtras,
|
||||
} from "../../index.js";
|
||||
import { findUriInDirs, getUriPathBasename } from "../../util/uri.js";
|
||||
import { getUriDescription } from "../../util/uri.js";
|
||||
import { BaseContextProvider } from "../index.js";
|
||||
|
||||
class OpenFilesContextProvider extends BaseContextProvider {
|
||||
|
|
@ -24,14 +24,17 @@ class OpenFilesContextProvider extends BaseContextProvider {
|
|||
? await ide.getPinnedFiles()
|
||||
: await ide.getOpenFiles();
|
||||
const workspaceDirs = await extras.ide.getWorkspaceDirs();
|
||||
|
||||
return await Promise.all(
|
||||
openFiles.map(async (filepath: string) => {
|
||||
const content = await ide.readFile(filepath);
|
||||
const { relativePathOrBasename, last2Parts, baseName } =
|
||||
getUriDescription(filepath, workspaceDirs);
|
||||
|
||||
return {
|
||||
description: filepath,
|
||||
content: `\`\`\`${
|
||||
findUriInDirs(filepath, workspaceDirs).relativePathOrBasename
|
||||
}\n${await ide.readFile(filepath)}\n\`\`\``,
|
||||
name: getUriPathBasename(filepath),
|
||||
description: last2Parts,
|
||||
content: `\`\`\`${relativePathOrBasename}\n${content}\n\`\`\``,
|
||||
name: baseName,
|
||||
uri: {
|
||||
type: "file",
|
||||
value: filepath,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import {
|
|||
ContextProviderDescription,
|
||||
ContextProviderExtras,
|
||||
} from "../../index.js";
|
||||
import { getUriPathBasename } from "../../util/uri.js";
|
||||
import { getUriDescription } from "../../util/uri.js";
|
||||
import { BaseContextProvider } from "../index.js";
|
||||
|
||||
class ProblemsContextProvider extends BaseContextProvider {
|
||||
|
|
@ -20,10 +20,14 @@ class ProblemsContextProvider extends BaseContextProvider {
|
|||
): Promise<ContextItem[]> {
|
||||
const ide = extras.ide;
|
||||
const problems = await ide.getProblems();
|
||||
const workspaceDirs = await ide.getWorkspaceDirs();
|
||||
|
||||
const items = await Promise.all(
|
||||
problems.map(async (problem) => {
|
||||
const fileName = getUriPathBasename(problem.filepath);
|
||||
const { relativePathOrBasename, baseName } = getUriDescription(
|
||||
problem.filepath,
|
||||
workspaceDirs,
|
||||
);
|
||||
const content = await ide.readFile(problem.filepath);
|
||||
const lines = content.split("\n");
|
||||
const rangeContent = lines
|
||||
|
|
@ -35,8 +39,8 @@ class ProblemsContextProvider extends BaseContextProvider {
|
|||
|
||||
return {
|
||||
description: "Problems in current file",
|
||||
content: `\`\`\`${fileName}\n${rangeContent}\n\`\`\`\n${problem.message}\n\n`,
|
||||
name: `Warning in ${fileName}`,
|
||||
content: `\`\`\`${relativePathOrBasename}\n${rangeContent}\n\`\`\`\n${problem.message}\n\n`,
|
||||
name: `Warning in ${baseName}`,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { BranchAndDir, ContextItem, ContextProviderExtras } from "../../";
|
||||
import TransformersJsEmbeddingsProvider from "../../llm/llms/TransformersJsEmbeddingsProvider";
|
||||
import { getUriPathBasename } from "../../util/uri";
|
||||
import { getUriDescription } from "../../util/uri";
|
||||
import { INSTRUCTIONS_BASE_ITEM } from "../providers/utils";
|
||||
|
||||
import { RetrievalPipelineOptions } from "./pipelines/BaseRetrievalPipeline";
|
||||
|
|
@ -111,18 +111,17 @@ export async function retrieveContextItemsFromEmbeddings(
|
|||
...results
|
||||
.sort((a, b) => a.filepath.localeCompare(b.filepath))
|
||||
.map((r) => {
|
||||
const basename = getUriPathBasename(r.filepath);
|
||||
const name = `${basename} (${r.startLine}-${r.endLine})`;
|
||||
const description = `${r.filepath}`;
|
||||
const { relativePathOrBasename, last2Parts, baseName } =
|
||||
getUriDescription(r.filepath, workspaceDirs);
|
||||
|
||||
if (basename === "package.json") {
|
||||
if (baseName === "package.json") {
|
||||
console.warn("Retrieval pipeline: package.json detected");
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
description,
|
||||
content: `\`\`\`${name}\n${r.content}\n\`\`\``,
|
||||
name: `${baseName} (${r.startLine}-${r.endLine})`,
|
||||
description: last2Parts,
|
||||
content: `\`\`\`${relativePathOrBasename}\n${r.content}\n\`\`\``,
|
||||
uri: {
|
||||
type: "file" as const,
|
||||
value: r.filepath,
|
||||
|
|
|
|||
|
|
@ -28,7 +28,12 @@ import type {
|
|||
IndexTag,
|
||||
IndexingProgressUpdate,
|
||||
} from "../";
|
||||
import { getLastNPathParts, getUriPathBasename } from "../util/uri";
|
||||
import {
|
||||
findUriInDirs,
|
||||
getLastNPathParts,
|
||||
getLastNUriRelativePathParts,
|
||||
getUriPathBasename,
|
||||
} from "../util/uri";
|
||||
|
||||
type SnippetChunk = ChunkWithoutID & { title: string; signature: string };
|
||||
|
||||
|
|
@ -347,14 +352,19 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
|
|||
}
|
||||
}
|
||||
|
||||
static async getForId(id: number): Promise<ContextItem> {
|
||||
static async getForId(
|
||||
id: number,
|
||||
workspaceDirs: string[],
|
||||
): Promise<ContextItem> {
|
||||
const db = await SqliteDb.get();
|
||||
const row = await db.get("SELECT * FROM code_snippets WHERE id = ?", [id]);
|
||||
|
||||
const last2Parts = getLastNUriRelativePathParts(workspaceDirs, row.path, 2);
|
||||
const { relativePathOrBasename } = findUriInDirs(row.path, workspaceDirs);
|
||||
return {
|
||||
name: row.title,
|
||||
description: getLastNPathParts(row.path, 2),
|
||||
content: `\`\`\`${getUriPathBasename(row.path)}\n${row.content}\n\`\`\``,
|
||||
description: last2Parts,
|
||||
content: `\`\`\`${relativePathOrBasename}\n${row.content}\n\`\`\``,
|
||||
uri: {
|
||||
type: "file",
|
||||
value: row.path,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { getUriPathBasename } from "../../util/uri";
|
||||
import { getUriDescription } from "../../util/uri";
|
||||
|
||||
import { ToolImpl } from ".";
|
||||
|
||||
|
|
@ -9,13 +9,16 @@ export const readCurrentlyOpenFileImpl: ToolImpl = async (args, extras) => {
|
|||
return [];
|
||||
}
|
||||
|
||||
const basename = getUriPathBasename(result.path);
|
||||
const { relativePathOrBasename, last2Parts, baseName } = getUriDescription(
|
||||
result.path,
|
||||
await extras.ide.getWorkspaceDirs(),
|
||||
);
|
||||
|
||||
return [
|
||||
{
|
||||
name: "Current file",
|
||||
description: basename,
|
||||
content: `\`\`\`${basename}\n${result.contents}\n\`\`\``,
|
||||
name: `Current file: ${baseName}`,
|
||||
description: last2Parts,
|
||||
content: `\`\`\`${relativePathOrBasename}\n${result.contents}\n\`\`\``,
|
||||
uri: {
|
||||
type: "file",
|
||||
value: result.path,
|
||||
|
|
|
|||
|
|
@ -177,3 +177,19 @@ export function getLastNPathParts(filepath: string, n: number): string {
|
|||
}
|
||||
return filepath.split(/[\\/]/).slice(-n).join("/");
|
||||
}
|
||||
|
||||
export function getUriDescription(uri: string, dirUriCandidates: string[]) {
|
||||
const { relativePathOrBasename, foundInDir } = findUriInDirs(
|
||||
uri,
|
||||
dirUriCandidates,
|
||||
);
|
||||
const baseName = getUriPathBasename(uri);
|
||||
const last2Parts = getLastNUriRelativePathParts(dirUriCandidates, uri, 2);
|
||||
return {
|
||||
uri,
|
||||
relativePathOrBasename,
|
||||
foundInDir,
|
||||
last2Parts,
|
||||
baseName,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ import resolveEditorContent from "../../components/mainInput/resolveInput";
|
|||
import { ThunkApiType } from "../store";
|
||||
import { selectDefaultModel } from "../slices/configSlice";
|
||||
import { findUriInDirs, getUriPathBasename } from "core/util/uri";
|
||||
import * as URI from "uri-js";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export const gatherContext = createAsyncThunk<
|
||||
{
|
||||
|
|
@ -34,7 +36,7 @@ export const gatherContext = createAsyncThunk<
|
|||
const defaultContextProviders =
|
||||
state.config.config.experimental?.defaultContext ?? [];
|
||||
|
||||
if (!state.config.defaultModelTitle) {
|
||||
if (!defaultModel) {
|
||||
console.error(
|
||||
"gatherContext thunk: Cannot gather context, no model selected",
|
||||
);
|
||||
|
|
@ -49,46 +51,52 @@ export const gatherContext = createAsyncThunk<
|
|||
ideMessenger: extra.ideMessenger,
|
||||
defaultContextProviders,
|
||||
dispatch,
|
||||
selectedModelTitle: state.config.defaultModelTitle,
|
||||
selectedModelTitle: defaultModel.title,
|
||||
});
|
||||
|
||||
// Automatically use currently open file
|
||||
if (!modifiers.noContext) {
|
||||
const usingFreeTrial = defaultModel?.provider === "free-trial";
|
||||
|
||||
const currentFile = await extra.ideMessenger.ide.getCurrentFile();
|
||||
if (currentFile) {
|
||||
let currentFileContents = currentFile.contents;
|
||||
if (usingFreeTrial) {
|
||||
currentFileContents = currentFile.contents
|
||||
.split("\n")
|
||||
.slice(0, 1000)
|
||||
.join("\n");
|
||||
}
|
||||
if (
|
||||
!selectedContextItems.find(
|
||||
(item) => item.uri?.value === currentFile.path,
|
||||
)
|
||||
) {
|
||||
const currentFileResponse = await extra.ideMessenger.request(
|
||||
"context/getContextItems",
|
||||
{
|
||||
name: "currentFile",
|
||||
query: "non-mention-usage",
|
||||
fullInput: "",
|
||||
selectedCode: [],
|
||||
selectedModelTitle: defaultModel.title,
|
||||
},
|
||||
);
|
||||
if (currentFileResponse.status === "success") {
|
||||
const items = currentFileResponse.content;
|
||||
if (items.length > 0) {
|
||||
const currentFile = items[0];
|
||||
const uri = currentFile.uri?.value;
|
||||
|
||||
// don't add the file if it's already in the context items
|
||||
selectedContextItems.unshift({
|
||||
content: `The following file is currently open. Don't reference it if it's not relevant to the user's message.\n\n\`\`\`${
|
||||
findUriInDirs(
|
||||
currentFile.path,
|
||||
await extra.ideMessenger.ide.getWorkspaceDirs(),
|
||||
).relativePathOrBasename
|
||||
}\n${currentFileContents}\n\`\`\``,
|
||||
name: `Active file: ${getUriPathBasename(currentFile.path)}`,
|
||||
description: currentFile.path,
|
||||
id: {
|
||||
itemId: currentFile.path,
|
||||
if (
|
||||
uri &&
|
||||
!selectedContextItems.find(
|
||||
(item) => item.uri?.value && URI.equal(item.uri.value, uri),
|
||||
)
|
||||
) {
|
||||
// Limit to 1000 lines if using free trial
|
||||
if (usingFreeTrial) {
|
||||
currentFile.content = currentFile.content
|
||||
.split("\n")
|
||||
.slice(0, 1000)
|
||||
.join("\n");
|
||||
if (!currentFile.content.endsWith("```")) {
|
||||
currentFile.content += "\n```";
|
||||
}
|
||||
}
|
||||
currentFile.id = {
|
||||
providerTitle: "file",
|
||||
},
|
||||
uri: {
|
||||
type: "file",
|
||||
value: currentFile.path,
|
||||
},
|
||||
});
|
||||
itemId: uri,
|
||||
};
|
||||
selectedContextItems.unshift(currentFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue