✨ problems context provider

This commit is contained in:
Nate Sesti 2024-01-14 00:14:54 -08:00
parent 014122957f
commit ce4fee701b
10 changed files with 108 additions and 5 deletions

View file

@ -56,6 +56,7 @@ const defaultConfig: SerializedContinueConfig = {
params: {},
},
{ name: "terminal", params: {} },
{ name: "problems", params: {} },
],
};

View file

@ -0,0 +1,52 @@
import { BaseContextProvider } from "..";
import {
ContextItem,
ContextProviderDescription,
ContextProviderExtras,
} from "../..";
import { ExtensionIde } from "../../ide";
import { getBasename } from "../../util";
class ProblemsContextProvider extends BaseContextProvider {
static description: ContextProviderDescription = {
title: "problems",
displayTitle: "Problems",
description: "Reference problems in the current file",
dynamic: true,
requiresQuery: false,
};
async getContextItems(
query: string,
extras: ContextProviderExtras
): Promise<ContextItem[]> {
const ide = new ExtensionIde();
const problems = await ide.getProblems();
const items = await Promise.all(
problems.map(async (problem) => {
const content = await ide.readFile(problem.filepath);
const lines = content.split("\n");
const rangeContent = lines
.slice(
Math.max(0, problem.range.start.line - 2),
problem.range.end.line + 2
)
.join("\n");
return {
description: "Problems in current file",
content: `\`\`\`${getBasename(
problem.filepath
)}\n${rangeContent}\n\`\`\`\n${problem.message}\n\n`,
name: `Warning in ${getBasename(problem.filepath)}`,
};
})
);
return items;
}
async load(): Promise<void> {}
}
export default ProblemsContextProvider;

View file

@ -7,6 +7,7 @@ import GitHubIssuesContextProvider from "./GitHubIssuesContextProvider";
import GoogleContextProvider from "./GoogleContextProvider";
import HttpContextProvider from "./HttpContextProvider";
import OpenFilesContextProvider from "./OpenFilesContextProvider";
import ProblemsContextProvider from "./ProblemsContextProvider";
import SearchContextProvider from "./SearchContextProvider";
import TerminalContextProvider from "./TerminalContextProvider";
import URLContextProvider from "./URLContextProvider";
@ -22,6 +23,7 @@ const Providers: (typeof BaseContextProvider)[] = [
HttpContextProvider,
SearchContextProvider,
CodebaseContextProvider,
ProblemsContextProvider,
];
export function contextProviderClassFromName(

View file

@ -1,5 +1,5 @@
import * as fs from "fs";
import { DiffLine, IDE, SerializedContinueConfig } from "..";
import { DiffLine, IDE, Problem, SerializedContinueConfig } from "..";
import { Chunk } from "../index";
import {
getConfigJsonPath,
@ -108,6 +108,10 @@ class FileSystemIde implements IDE {
return "";
}
async getProblems(filepath?: string | undefined): Promise<Problem[]> {
return Promise.resolve([]);
}
async subprocess(command: string): Promise<[string, string]> {
return ["", ""];
}

View file

@ -1,4 +1,4 @@
import { Chunk, DiffLine } from "..";
import { Chunk, DiffLine, Problem } from "..";
import { IDE, SerializedContinueConfig } from "../index";
import { ideRequest } from "./messaging";
@ -88,6 +88,10 @@ export class ExtensionIde implements IDE {
return r("getSearchResults", { query });
}
getProblems(filepath?: string | undefined): Promise<Problem[]> {
return r("getProblems", { filepath });
}
subprocess(command: string): Promise<[string, string]> {
return r("subprocess", { command });
}

10
core/index.d.ts vendored
View file

@ -263,6 +263,12 @@ export interface DiffLine {
line: string;
}
export class Problem {
filepath: string;
range: Range;
message: string;
}
export interface IDE {
getSerializedConfig(): Promise<SerializedContinueConfig>;
getConfigJsUrl(): Promise<string | undefined>;
@ -291,6 +297,7 @@ export interface IDE {
getOpenFiles(): Promise<string[]>;
getSearchResults(query: string): Promise<string>;
subprocess(command: string): Promise<[string, string]>;
getProblems(filepath?: string | undefined): Promise<Problem[]>;
// Embeddings
/**
@ -357,7 +364,8 @@ type ContextProviderName =
| "url"
| "tree"
| "http"
| "codebase";
| "codebase"
| "problems";
type TemplateType =
| "llama2"

View file

@ -850,7 +850,8 @@
"search",
"url",
"http",
"codebase"
"codebase",
"problems"
],
"markdownEnumDescriptions": [
"Reference the contents of the current changes as given by `git diff`",
@ -858,7 +859,9 @@
"Reference the contents of all open files.",
"Enter a search phrase and include the Google search results as context",
"Enter a URL and use the contents of the webpage as context",
"Write a custom context provider at your own HTTP endpoint. Set 'url' in the params object for the endpoint you have setup."
"Write a custom context provider at your own HTTP endpoint. Set 'url' in the params object for the endpoint you have setup.",
"Use embeddings to automatically find relevant files from throughout the codebase",
"Reference all linting errors and warnings in the currently open file"
],
"type": "string"
},

View file

@ -415,6 +415,10 @@ export function getSidebarContent(
);
break;
}
case "getProblems": {
respond(await ide.getProblems(data.message.filepath));
break;
}
case "getOpenFiles": {
respond(await ide.getOpenFiles());
break;

View file

@ -18,6 +18,7 @@ import {
ContinueConfig,
DiffLine,
IDE,
Problem,
SerializedContinueConfig,
} from "core";
import {
@ -315,6 +316,28 @@ class VsCodeIde implements IDE {
return results.join("\n\n");
}
async getProblems(filepath?: string | undefined): Promise<Problem[]> {
const uri = filepath
? vscode.Uri.file(filepath)
: vscode.window.activeTextEditor?.document.uri;
if (!uri) {
return [];
}
return vscode.languages.getDiagnostics(uri).map((d) => {
return {
filepath: uri.fsPath,
range: {
start: {
line: d.range.start.line,
character: d.range.start.character,
},
end: { line: d.range.end.line, character: d.range.end.character },
},
message: d.message,
};
});
}
async subprocess(command: string): Promise<[string, string]> {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {

View file

@ -7,6 +7,7 @@ import {
Cog6ToothIcon,
CommandLineIcon,
ExclamationCircleIcon,
ExclamationTriangleIcon,
FolderIcon,
FolderOpenIcon,
GlobeAltIcon,
@ -44,6 +45,7 @@ const ICONS_FOR_DROPDOWN: { [key: string]: any } = {
url: GlobeAltIcon,
open: FolderOpenIcon,
codebase: SparklesIcon,
problems: ExclamationTriangleIcon,
"/edit": PaintBrushIcon,
"/clear": TrashIcon,
"/test": BeakerIcon,