This commit is contained in:
Dallin Romney 2025-04-02 13:48:50 -07:00
parent 7b6b7070fd
commit f58469ffc9
14 changed files with 131 additions and 57 deletions

View file

@ -35,7 +35,7 @@ class FolderContextProvider extends BaseContextProvider {
const folders = await walkDirs(
args.ide,
{
onlyDirs: true,
include: "dirs",
source: "load submenu items - folder",
},
workspaceDirs,

View file

@ -54,7 +54,7 @@ class RepoMapContextProvider extends BaseContextProvider {
const folders = await walkDirs(
args.ide,
{
onlyDirs: true,
include: "dirs",
source: "load submenu items - repo map",
},
workspaceDirs,

View file

@ -97,7 +97,7 @@ describe("walkDir functions", () => {
);
});
it("should handle onlyDirs option", async () => {
it("should handle include option", async () => {
addToTestDir([
"src/",
["src/file1.ts", "content1"],
@ -109,7 +109,7 @@ describe("walkDir functions", () => {
(await testIde.getWorkspaceDirs())[0],
testIde,
{
onlyDirs: true,
include: "dirs",
},
);

View file

@ -10,10 +10,11 @@ import {
} from "./ignore";
export interface WalkerOptions {
onlyDirs?: boolean;
include?: "dirs" | "files" | "both";
returnRelativeUrisPaths?: boolean;
source?: string;
overrideDefaultIgnores?: Ignore;
recursive?: boolean;
}
type Entry = [string, FileType];
@ -195,7 +196,7 @@ class DFSWalker {
if (this.entryIsDirectory(entry)) {
relPath = `${relPath}/`;
} else {
if (this.options.onlyDirs) {
if (this.options.include === "dirs") {
continue;
}
}
@ -221,19 +222,22 @@ class DFSWalker {
}
if (this.entryIsDirectory(entry)) {
stack.push({
walkableEntry,
ignoreContexts,
});
if (this.options.onlyDirs) {
// when onlyDirs is enabled the walker will only return directory names
if (this.options.recursive) {
stack.push({
walkableEntry,
ignoreContexts,
});
}
if (this.options.include !== "files") {
// if yielding dirs or both, walker includes relative paths
const trailingSlash = this.options.include === "dirs" ? "" : "/";
if (this.options.returnRelativeUrisPaths) {
yield walkableEntry.relativeUriPath;
yield walkableEntry.relativeUriPath + trailingSlash;
} else {
yield walkableEntry.uri;
yield walkableEntry.uri + trailingSlash;
}
}
} else {
} else if (this.options.include !== "dirs") {
if (this.options.returnRelativeUrisPaths) {
yield walkableEntry.relativeUriPath;
} else {
@ -257,8 +261,9 @@ class DFSWalker {
}
const defaultOptions: WalkerOptions = {
onlyDirs: false,
include: "files",
returnRelativeUrisPaths: false,
recursive: true,
};
export async function* walkDirAsync(

View file

@ -3,10 +3,14 @@ export enum BuiltInToolNames {
ReadCurrentlyOpenFile = "builtin_read_currently_open_file",
CreateNewFile = "builtin_create_new_file",
RunTerminalCommand = "builtin_run_terminal_command",
ViewSubdirectory = "builtin_view_subdirectory",
ViewRepoMap = "builtin_view_repo_map",
ExactSearch = "builtin_exact_search",
SearchWeb = "builtin_search_web",
ViewDiff = "builtin_view_diff",
LSTool = "builtin_ls",
// excluded from allTools for now
ViewRepoMap = "builtin_view_repo_map",
ViewSubdirectory = "builtin_view_subdirectory",
}
export const BUILT_IN_GROUP_NAME = "Built-In";

View file

@ -5,13 +5,12 @@ import { BuiltInToolNames } from "./builtIn";
import { createNewFileImpl } from "./implementations/createNewFile";
import { exactSearchImpl } from "./implementations/exactSearch";
import { lsToolImpl } from "./implementations/lsTool";
import { readCurrentlyOpenFileImpl } from "./implementations/readCurrentlyOpenFile";
import { readFileImpl } from "./implementations/readFile";
import { runTerminalCommandImpl } from "./implementations/runTerminalCommand";
import { searchWebImpl } from "./implementations/searchWeb";
import { viewDiffImpl } from "./implementations/viewDiff";
import { viewRepoMapImpl } from "./implementations/viewRepoMap";
import { viewSubdirectoryImpl } from "./implementations/viewSubdirectory";
async function callHttpTool(
url: string,
@ -149,12 +148,14 @@ export async function callTool(
return await searchWebImpl(args, extras);
case BuiltInToolNames.ViewDiff:
return await viewDiffImpl(args, extras);
case BuiltInToolNames.ViewRepoMap:
return await viewRepoMapImpl(args, extras);
case BuiltInToolNames.ViewSubdirectory:
return await viewSubdirectoryImpl(args, extras);
case BuiltInToolNames.LSTool:
return await lsToolImpl(args, extras);
case BuiltInToolNames.ReadCurrentlyOpenFile:
return await readCurrentlyOpenFileImpl(args, extras);
// case BuiltInToolNames.ViewRepoMap:
// return await viewRepoMapImpl(args, extras);
// case BuiltInToolNames.ViewSubdirectory:
// return await viewSubdirectoryImpl(args, extras);
default:
return await callToolFromUri(uri, args, extras);
}

View file

@ -0,0 +1,33 @@
import { Tool } from "../..";
import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn";
export const lsTool: Tool = {
type: "function",
displayTitle: "LS Tool",
wouldLikeTo: "list files and folders in {{{ dirPath }}}",
isCurrently: "listing files and folders in {{{ dirPath }}}",
hasAlready: "listed files and folders in {{{ dirPath }}}",
readonly: true,
group: BUILT_IN_GROUP_NAME,
function: {
name: BuiltInToolNames.LSTool,
description: "List files and folders in a given directory",
parameters: {
type: "object",
required: ["dirPath", "recursive"],
properties: {
dirPath: {
type: "string",
description:
"The directory path relative to the root of the project. Always use forward slash paths like '/'. rather than e.g. '.'",
},
recursive: {
type: "boolean",
description:
"If true, lists files and folders recursively. To prevent unexpected large results, use this sparingly",
},
},
},
},
};

View file

@ -0,0 +1,31 @@
import { ToolImpl } from ".";
import { walkDir } from "../../indexing/walkDir";
import { resolveRelativePathInDir } from "../../util/ideUtils";
export const lsToolImpl: ToolImpl = async (args, extras) => {
const uri = await resolveRelativePathInDir(args.dirPath, extras.ide);
if (!uri) {
throw new Error(
`Directory ${args.dirPath} not found. Make sure to use forward-slash paths. Do not use e.g. "."`,
);
}
const entries = await walkDir(uri, extras.ide, {
returnRelativeUrisPaths: true,
include: "both",
recursive: args.recursive ?? false,
});
const content =
entries.length > 0
? entries.join("\n")
: `No files/folders found in ${args.dirPath}`;
return [
{
name: "File/folder list",
description: `Files/folders in ${args.dirPath}`,
content,
},
];
};

View file

@ -1,21 +1,24 @@
import { createNewFileTool } from "./definitions/createNewFile";
import { exactSearchTool } from "./definitions/exactSearch";
import { lsTool } from "./definitions/lsTool";
import { readCurrentlyOpenFileTool } from "./definitions/readCurrentlyOpenFile";
import { readFileTool } from "./definitions/readFile";
import { runTerminalCommandTool } from "./definitions/runTerminalCommand";
import { searchWebTool } from "./definitions/searchWeb";
import { viewDiffTool } from "./definitions/viewDiff";
import { viewRepoMapTool } from "./definitions/viewRepoMap";
import { viewSubdirectoryTool } from "./definitions/viewSubdirectory";
export const allTools = [
readFileTool,
createNewFileTool,
runTerminalCommandTool,
viewSubdirectoryTool,
viewRepoMapTool,
exactSearchTool,
searchWebTool,
viewDiffTool,
readCurrentlyOpenFileTool,
lsTool,
// replacing with ls tool for now
// viewSubdirectoryTool,
// viewRepoMapTool,
];

View file

@ -4,7 +4,7 @@ import { MCPSection } from "./MCPSection";
import { ModelsSection } from "./ModelsSection";
import { PromptsSection } from "./PromptsSection";
import { RulesSection } from "./RulesSection";
import { ToolsSection } from "./ToolsSection";
import { ToolPoliciesSection } from "./tool-policies/ToolPoliciesSection";
interface SelectedSectionProps {
selectedSection: string | null;
@ -23,7 +23,7 @@ export function SelectedSection(props: SelectedSectionProps) {
case "context":
return <ContextSection />;
case "tools":
return <ToolsSection />;
return <ToolPoliciesSection />;
case "mcp":
return <MCPSection />;
default:

View file

@ -1,7 +0,0 @@
import { ToolPermissionsDialog } from "../../InputToolbar/ToolPermissionsDialog";
interface ToolsSectionProps {}
export function ToolsSection({}: ToolsSectionProps) {
return <ToolPermissionsDialog />;
}

View file

@ -1,12 +1,12 @@
import { Tool } from "core";
import { useMemo } from "react";
import { useAppDispatch, useAppSelector } from "../../../redux/hooks";
import { toggleToolGroupSetting } from "../../../redux/slices/uiSlice";
import { fontSize } from "../../../util";
import ToggleSwitch from "../../gui/Switch";
import ToolDropdownItem from "./ToolDropdownItem";
import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks";
import { toggleToolGroupSetting } from "../../../../../redux/slices/uiSlice";
import { fontSize } from "../../../../../util";
import ToggleSwitch from "../../../../gui/Switch";
import ToolPolicyItem from "./ToolPolicyItem";
export const ToolPermissionsDialog = () => {
export const ToolPoliciesSection = () => {
const availableTools = useAppSelector((state) => state.config.config.tools);
const toolGroupSettings = useAppSelector(
(store) => store.ui.toolGroupSettings,
@ -61,7 +61,7 @@ export const ToolPermissionsDialog = () => {
</div>
<div className="relative flex flex-col p-1">
{tools.map((tool) => (
<ToolDropdownItem
<ToolPolicyItem
key={tool.uri + tool.function.name}
tool={tool}
duplicatesDetected={duplicateDetection[tool.function.name]}

View file

@ -2,10 +2,13 @@ import { InformationCircleIcon } from "@heroicons/react/24/outline";
import { Tool } from "core";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { useAppSelector } from "../../../redux/hooks";
import { addTool, toggleToolSetting } from "../../../redux/slices/uiSlice";
import { fontSize } from "../../../util";
import { ToolTip } from "../../gui/Tooltip";
import { useAppSelector } from "../../../../../redux/hooks";
import {
addTool,
toggleToolSetting,
} from "../../../../../redux/slices/uiSlice";
import { fontSize } from "../../../../../util";
import { ToolTip } from "../../../../gui/Tooltip";
interface ToolDropdownItemProps {
tool: Tool;
@ -13,19 +16,19 @@ interface ToolDropdownItemProps {
excluded: boolean;
}
function ToolDropdownItem(props: ToolDropdownItemProps) {
function ToolPolicyItem(props: ToolDropdownItemProps) {
const dispatch = useDispatch();
const settings = useAppSelector(
const policy = useAppSelector(
(state) => state.ui.toolSettings[props.tool.function.name],
);
useEffect(() => {
if (!settings) {
if (!policy) {
dispatch(addTool(props.tool));
}
}, [props.tool.function.name, settings]);
}, [props.tool.function.name, policy]);
if (!settings) {
if (!policy) {
return null;
}
@ -81,13 +84,13 @@ function ToolDropdownItem(props: ToolDropdownItemProps) {
<span className="text-lightgray">Excluded</span>
) : (
<div className="flex cursor-pointer gap-2">
{(settings === "allowedWithPermission" || settings === undefined) && (
{(policy === "allowedWithPermission" || policy === undefined) && (
<span className="text-yellow-500">Ask First</span>
)}
{settings === "allowedWithoutPermission" && (
{policy === "allowedWithoutPermission" && (
<span className="text-green-500">Automatic</span>
)}
{settings === "disabled" && (
{policy === "disabled" && (
<span className="text-lightgray">Excluded</span>
)}
</div>
@ -96,4 +99,4 @@ function ToolDropdownItem(props: ToolDropdownItemProps) {
);
}
export default ToolDropdownItem;
export default ToolPolicyItem;

View file

@ -52,6 +52,7 @@ export const uiSlice = createSlice({
[BuiltInToolNames.ExactSearch]: "allowedWithoutPermission",
[BuiltInToolNames.SearchWeb]: "allowedWithoutPermission",
[BuiltInToolNames.ViewDiff]: "allowedWithoutPermission",
[BuiltInToolNames.LSTool]: "allowedWithoutPermission",
},
toolGroupSettings: {
BUILT_IN_GROUP_NAME: "include",