Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT run command updates #4973

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions core/commands/slash/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const GenerateTerminalCommand: SlashCommand = {
return;
}

const gen =
llm.streamComplete(`The user has made a request to run a shell command. Their description of what it should do is:
const gen = llm.streamComplete(
`The user has made a request to run a shell command. Their description of what it should do is:

"${input}"

Please write a shell command that will do what the user requested. Your output should consist of only the command itself, without any explanation or example output. Do not use any newlines. Only output the command that when inserted into the terminal will do precisely what was requested. Here is the command:`,
new AbortController().signal
);
new AbortController().signal,
);

const lines = streamLines(gen);
let cmd = "";
Expand Down Expand Up @@ -54,7 +54,9 @@ Please write a shell command that will do what the user requested. Your output s
if (commandIsPotentiallyDangerous(cmd)) {
yield "\n\nWarning: This command may be potentially dangerous. Please double-check before pasting it in your terminal.";
} else {
await ide.runCommand(cmd);
await ide.runCommandInWorkspace(cmd, {
preferVisibleTerminal: true,
});
}
},
};
Expand Down
6 changes: 1 addition & 5 deletions core/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ declare global {

openUrl(url: string): Promise<void>;

runCommand(command: string): Promise<void>;
runCommand(command: string, options?: TerminalOptions): Promise<void>;

saveFile(filepath: string): Promise<void>;

Expand All @@ -637,10 +637,6 @@ declare global {

getPinnedFiles(): Promise<string[]>;

getSearchResults(query: string): Promise<string>;

subprocess(command: string, cwd?: string): Promise<[string, string]>;

getProblems(filepath?: string | undefined): Promise<Problem[]>;

getBranch(dir: string): Promise<string>;
Expand Down
21 changes: 11 additions & 10 deletions core/context/providers/GitLabMergeRequestContextProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ interface GitLabComment {
};
}

const trimFirstElement = (args: Array<string>): string => {
return args[0].trim();
};

const getSubprocess = async (extras: ContextProviderExtras) => {
const workingDir = await extras.ide.getWorkspaceDirs().then(trimFirstElement);

return (command: string) =>
extras.ide.subprocess(command, workingDir).then(trimFirstElement);
return async (command: string) => {
const { error, output } = await extras.ide.runCommandInWorkspace(command, {
preferVisibleTerminal: false,
});
if (error) {
throw new Error(error);
}
return output;
};
};

class GitLabMergeRequestContextProvider extends BaseContextProvider {
Expand Down Expand Up @@ -124,10 +125,10 @@ class GitLabMergeRequestContextProvider extends BaseContextProvider {
let urlMatches: RegExpExecArray | null;
if (/https?.*/.test(remoteUrl)) {
const pathname = new URL(remoteUrl).pathname;
urlMatches = /\/(?<projectPath>.*?)(?:(?=\.git)|$)/.exec(pathname)
urlMatches = /\/(?<projectPath>.*?)(?:(?=\.git)|$)/.exec(pathname);
} else {
// ssh
urlMatches = /:(?<projectPath>.*).git/.exec(remoteUrl)
urlMatches = /:(?<projectPath>.*).git/.exec(remoteUrl);
}

const project = urlMatches?.groups?.projectPath ?? null;
Expand Down
3 changes: 2 additions & 1 deletion core/context/providers/SearchContextProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ContextProviderDescription,
ContextProviderExtras,
} from "../../index.js";
import { grepSearchDirs } from "../../util/grepSearch.js";
import { BaseContextProvider } from "../index.js";

class SearchContextProvider extends BaseContextProvider {
Expand All @@ -18,7 +19,7 @@ class SearchContextProvider extends BaseContextProvider {
query: string,
extras: ContextProviderExtras,
): Promise<ContextItem[]> {
const results = await extras.ide.getSearchResults(query);
const results = await grepSearchDirs(query, extras.ide);
return [
{
description: "Search results",
Expand Down
28 changes: 20 additions & 8 deletions core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,17 @@ export interface IDE {

openUrl(url: string): Promise<void>;

runCommand(command: string, options?: TerminalOptions): Promise<void>;
runCommandLocally(
command: string,
options?: LocalTerminalOptions,
): Promise<{ error?: string; output: string }>;

runCommandInWorkspace(
command: string,
options?: WorkspaceTerminalOptions,
): Promise<{ error?: string; output: string }>;

ripgrepSearch(args: string[]): Promise<string>;

saveFile(fileUri: string): Promise<void>;

Expand All @@ -707,10 +717,6 @@ export interface IDE {

getPinnedFiles(): Promise<string[]>;

getSearchResults(query: string): Promise<string>;

subprocess(command: string, cwd?: string): Promise<[string, string]>;

getProblems(fileUri?: string | undefined): Promise<Problem[]>;

getBranch(dir: string): Promise<string>;
Expand Down Expand Up @@ -1379,7 +1385,13 @@ export type PackageDocsResult = {
| { details: PackageDetailsSuccess; error?: never }
);

export interface TerminalOptions {
reuseTerminal?: boolean;
terminalName?: string;
export interface LocalTerminalOptions {
preferVisibleTerminal?: boolean;
insertOnly?: boolean; // VS Code only, if true Jetbrains will do nothing
reuseTerminalNamed?: string; // VS Code only
}

export interface WorkspaceTerminalOptions {
preferVisibleTerminal?: boolean; // VS Code only
reuseTerminalNamed?: string; // VS Code only
}
15 changes: 11 additions & 4 deletions core/protocol/ide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import type {
IdeInfo,
IdeSettings,
IndexTag,
LocalTerminalOptions,
Location,
Problem,
Range,
RangeInFile,
TerminalOptions,
Thread,
WorkspaceTerminalOptions,
} from "../";

export interface GetGhTokenArgs {
Expand All @@ -29,9 +30,15 @@ export type ToIdeFromWebviewOrCoreProtocol = {
showVirtualFile: [{ name: string; content: string }, void];
openFile: [{ path: string }, void];
openUrl: [string, void];
runCommand: [{ command: string; options?: TerminalOptions }, void];
getSearchResults: [{ query: string }, string];
subprocess: [{ command: string; cwd?: string }, [string, string]];
runCommandLocally: [
{ command: string; options?: LocalTerminalOptions },
{ error?: string; output: string },
];
runCommandInWorkspace: [
{ command: string; options?: WorkspaceTerminalOptions },
{ error?: string; output: string },
];
ripgrepSearch: [{ args: string[] }, string];
saveFile: [{ filepath: string }, void];
fileExists: [{ filepath: string }, boolean];
readFile: [{ filepath: string }, string];
Expand Down
26 changes: 15 additions & 11 deletions core/protocol/messenger/messageIde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import type {
IdeInfo,
IdeSettings,
IndexTag,
LocalTerminalOptions,
Location,
Problem,
Range,
RangeInFile,
TerminalOptions,
Thread,
WorkspaceTerminalOptions,
} from "../..";

export class MessageIde implements IDE {
Expand Down Expand Up @@ -160,8 +161,19 @@ export class MessageIde implements IDE {
await this.request("openUrl", url);
}

async runCommand(command: string, options?: TerminalOptions): Promise<void> {
await this.request("runCommand", { command, options });
async ripgrepSearch(args: string[]) {
return await this.request("ripgrepSearch", { args });
}

async runCommandLocally(command: string, options?: LocalTerminalOptions) {
return await this.request("runCommandLocally", { command, options });
}

async runCommandInWorkspace(
command: string,
options?: WorkspaceTerminalOptions,
) {
return await this.request("runCommandInWorkspace", { command, options });
}

async saveFile(fileUri: string): Promise<void> {
Expand All @@ -183,18 +195,10 @@ export class MessageIde implements IDE {
return this.request("getPinnedFiles", undefined);
}

getSearchResults(query: string): Promise<string> {
return this.request("getSearchResults", { query });
}

getProblems(fileUri: string): Promise<Problem[]> {
return this.request("getProblems", { filepath: fileUri });
}

subprocess(command: string, cwd?: string): Promise<[string, string]> {
return this.request("subprocess", { command, cwd });
}

async getBranch(dir: string): Promise<string> {
return this.request("getBranch", { dir });
}
Expand Down
20 changes: 10 additions & 10 deletions core/protocol/messenger/reverseMessageIde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,16 @@ export class ReverseMessageIde {
return this.ide.openFile(data.path);
});

this.on("runCommand", (data) => {
return this.ide.runCommand(data.command);
this.on("ripgrepSearch", (data) => {
return this.ide.ripgrepSearch(data.args);
});

this.on("runCommandLocally", (data) => {
return this.ide.runCommandLocally(data.command, data.options);
});

this.on("runCommandInWorkspace", (data) => {
return this.ide.runCommandInWorkspace(data.command, data.options);
});

this.on("saveFile", (data) => {
Expand All @@ -162,18 +170,10 @@ export class ReverseMessageIde {
return this.ide.getPinnedFiles();
});

this.on("getSearchResults", (data) => {
return this.ide.getSearchResults(data.query);
});

this.on("getProblems", (data) => {
return this.ide.getProblems(data.filepath);
});

this.on("subprocess", (data) => {
return this.ide.subprocess(data.command, data.cwd);
});

this.on("getBranch", (data) => {
return this.ide.getBranch(data.dir);
});
Expand Down
3 changes: 2 additions & 1 deletion core/tools/builtIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ export enum BuiltInToolNames {
ReadCurrentlyOpenFile = "builtin_read_currently_open_file",
CreateNewFile = "builtin_create_new_file",
RunTerminalCommand = "builtin_run_terminal_command",
ExactSearch = "builtin_exact_search",
SearchWeb = "builtin_search_web",
ViewDiff = "builtin_view_diff",
LSTool = "builtin_ls",
GlobTool = "builtin_glob",
GrepTool = "builtin_grep",

// excluded from allTools for now
ViewRepoMap = "builtin_view_repo_map",
Expand Down
9 changes: 6 additions & 3 deletions core/tools/callTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { canParseUrl } from "../util/url";
import { BuiltInToolNames } from "./builtIn";

import { createNewFileImpl } from "./implementations/createNewFile";
import { exactSearchImpl } from "./implementations/exactSearch";
import { globToolImpl } from "./implementations/globTool";
import { grepToolImpl } from "./implementations/grepTool";
import { lsToolImpl } from "./implementations/lsTool";
import { readCurrentlyOpenFileImpl } from "./implementations/readCurrentlyOpenFile";
import { readFileImpl } from "./implementations/readFile";
Expand Down Expand Up @@ -140,8 +141,6 @@ export async function callTool(
return await readFileImpl(args, extras);
case BuiltInToolNames.CreateNewFile:
return await createNewFileImpl(args, extras);
case BuiltInToolNames.ExactSearch:
return await exactSearchImpl(args, extras);
case BuiltInToolNames.RunTerminalCommand:
return await runTerminalCommandImpl(args, extras);
case BuiltInToolNames.SearchWeb:
Expand All @@ -150,6 +149,10 @@ export async function callTool(
return await viewDiffImpl(args, extras);
case BuiltInToolNames.LSTool:
return await lsToolImpl(args, extras);
case BuiltInToolNames.GlobTool:
return await globToolImpl(args, extras);
case BuiltInToolNames.GrepTool:
return await grepToolImpl(args, extras);
case BuiltInToolNames.ReadCurrentlyOpenFile:
return await readCurrentlyOpenFileImpl(args, extras);
// case BuiltInToolNames.ViewRepoMap:
Expand Down
27 changes: 0 additions & 27 deletions core/tools/definitions/exactSearch.ts

This file was deleted.

32 changes: 32 additions & 0 deletions core/tools/definitions/globTool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Tool } from "../..";

import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn";

export const globTool: Tool = {
type: "function",
displayTitle: "Glob Tool",
wouldLikeTo: "search files and folders in {{{ dirPath }}}",
isCurrently: "searching files and folders in {{{ dirPath }}}",
hasAlready: "listed files and folders in {{{ dirPath }}}",
readonly: true,
group: BUILT_IN_GROUP_NAME,
function: {
name: BuiltInToolNames.GlobTool,
description: "Finds files based on pattern matching",
parameters: {
type: "object",
required: ["pattern", "path"],
properties: {
pattern: {
type: "string",
description: "ripgrep glob pattern",
},
path: {
type: "string",
description:
"The directory path to search relative to the root of the project. Always use forward slash paths like '/'. rather than e.g. '.'",
},
},
},
},
};
Loading