Changesets · Branches · Tags · Compare
| Changeset | 7cf5e158954e453ba11f099dd0d6429ef970bf6c1d7c9f4142574d18e97e9c27 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-07-22 00:24 +02:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-22 00:24 +02:00 |
| Parents | 22e692d80113, b211d5ee5765 |
17 files changed
modified core/tools/definitions/createRuleBlock.ts +1 −1
| @@ -16,7 +16,7 @@ | ||
| 16 | 16 | 'Creates a "rule" that can be referenced in future conversations. This should be used whenever you want to establish code standards / preferences that should be applied consistently, or when you want to avoid making a mistake again. To modify existing rules, use the edit tool instead.\n\nRule Types:\n- Always: Include only "rule" (always included in model context)\n- Auto Attached: Include "rule", "globs", and/or "regex" (included when files match patterns)\n- Agent Requested: Include "rule" and "description" (AI decides when to apply based on description)\n- Manual: Include only "rule" (only included when explicitly mentioned using @ruleName)', |
| 17 | 17 | parameters: { |
| 18 | 18 | type: "object", |
| 19 | - required: ["name", "rule", "description"], | |
| 19 | + required: ["name", "rule"], | |
| 20 | 20 | properties: { |
| 21 | 21 | name: { |
| 22 | 22 | type: "string", |
modified core/tools/definitions/ls.ts +1 −2
| @@ -16,12 +16,11 @@ | ||
| 16 | 16 | description: "List files and folders in a given directory", |
| 17 | 17 | parameters: { |
| 18 | 18 | type: "object", |
| 19 | - required: ["dirPath", "recursive"], | |
| 20 | 19 | properties: { |
| 21 | 20 | dirPath: { |
| 22 | 21 | type: "string", |
| 23 | 22 | description: |
| 24 | - "The directory path relative to the root of the project. Always use forward slash paths like '/'. rather than e.g. '.'", | |
| 23 | + "The directory path relative to the root of the project. Use forward slash paths like '/'. rather than e.g. '.'", | |
| 25 | 24 | }, |
| 26 | 25 | recursive: { |
| 27 | 26 | type: "boolean", |
modified core/tools/implementations/codebaseTool.ts +4 −11
| @@ -1,22 +1,15 @@ | ||
| 1 | 1 | import { ToolImpl } from "."; |
| 2 | 2 | import { RangeInFile } from "../.."; |
| 3 | 3 | import { retrieveContextItemsFromEmbeddings } from "../../context/retrieval/retrieval"; |
| 4 | +import { getStringArg } from "../parseArgs"; | |
| 4 | 5 | |
| 5 | 6 | export const codebaseToolImpl: ToolImpl = async (args, extras) => { |
| 6 | - if (!args.query || args.query.trim() === "") { | |
| 7 | - return [ | |
| 8 | - { | |
| 9 | - name: "Error", | |
| 10 | - description: "Codebase search error", | |
| 11 | - content: "Query parameter is required and cannot be empty.", | |
| 12 | - }, | |
| 13 | - ]; | |
| 14 | - } | |
| 7 | + const query = getStringArg(args, "query"); | |
| 15 | 8 | |
| 16 | 9 | try { |
| 17 | 10 | const contextExtras = { |
| 18 | 11 | config: extras.config, |
| 19 | - fullInput: args.query, | |
| 12 | + fullInput: query, | |
| 20 | 13 | embeddingsProvider: extras.config.selectedModelByRole.embed, |
| 21 | 14 | reranker: extras.config.selectedModelByRole.rerank, |
| 22 | 15 | llm: extras.llm, |
| @@ -39,7 +32,7 @@ | ||
| 39 | 32 | { |
| 40 | 33 | name: "No Results", |
| 41 | 34 | description: "Codebase search", |
| 42 | - content: `No relevant code found for query: "${args.query}". This could mean: | |
| 35 | + content: `No relevant code found for query: "${query}". This could mean: | |
| 43 | 36 | - The codebase hasn't been indexed yet |
| 44 | 37 | - No code matches the search criteria |
| 45 | 38 | - Embeddings provider is not configured |
modified core/tools/implementations/createNewFile.ts +7 −3
| @@ -2,20 +2,24 @@ | ||
| 2 | 2 | |
| 3 | 3 | import { ToolImpl } from "."; |
| 4 | 4 | import { getCleanUriPath, getUriPathBasename } from "../../util/uri"; |
| 5 | +import { getStringArg } from "../parseArgs"; | |
| 5 | 6 | |
| 6 | 7 | export const createNewFileImpl: ToolImpl = async (args, extras) => { |
| 8 | + const filepath = getStringArg(args, "filepath"); | |
| 9 | + const contents = getStringArg(args, "contents", true); | |
| 10 | + | |
| 7 | 11 | const resolvedFileUri = await inferResolvedUriFromRelativePath( |
| 8 | - args.filepath, | |
| 12 | + filepath, | |
| 9 | 13 | extras.ide, |
| 10 | 14 | ); |
| 11 | 15 | if (resolvedFileUri) { |
| 12 | 16 | const exists = await extras.ide.fileExists(resolvedFileUri); |
| 13 | 17 | if (exists) { |
| 14 | 18 | throw new Error( |
| 15 | - `File ${args.filepath} already exists. Use the edit tool to edit this file`, | |
| 19 | + `File ${filepath} already exists. Use the edit tool to edit this file`, | |
| 16 | 20 | ); |
| 17 | 21 | } |
| 18 | - await extras.ide.writeFile(resolvedFileUri, args.contents); | |
| 22 | + await extras.ide.writeFile(resolvedFileUri, contents); | |
| 19 | 23 | await extras.ide.openFile(resolvedFileUri); |
| 20 | 24 | await extras.ide.saveFile(resolvedFileUri); |
| 21 | 25 | if (extras.codeBaseIndexer) { |
modified core/tools/implementations/createRuleBlock.ts +21 −6
| @@ -2,6 +2,11 @@ | ||
| 2 | 2 | import { ToolImpl } from "."; |
| 3 | 3 | import { RuleWithSource } from "../.."; |
| 4 | 4 | import { createRuleFilePath } from "../../config/markdown/utils"; |
| 5 | +import { | |
| 6 | + getBooleanArg, | |
| 7 | + getOptionalStringArg, | |
| 8 | + getStringArg, | |
| 9 | +} from "../parseArgs"; | |
| 5 | 10 | |
| 6 | 11 | export type CreateRuleBlockArgs = Pick< |
| 7 | 12 | Required<RuleWithSource>, |
| @@ -9,11 +14,21 @@ | ||
| 9 | 14 | > & |
| 10 | 15 | Pick<RuleWithSource, "globs" | "regex" | "description" | "alwaysApply">; |
| 11 | 16 | |
| 12 | -export const createRuleBlockImpl: ToolImpl = async ( | |
| 13 | - { name, rule, ...otherArgs }: CreateRuleBlockArgs, | |
| 14 | - extras, | |
| 15 | -) => { | |
| 16 | - const fileContent = createRuleMarkdown(name, rule, otherArgs); | |
| 17 | +export const createRuleBlockImpl: ToolImpl = async (args, extras) => { | |
| 18 | + const name = getStringArg(args, "name"); | |
| 19 | + const rule = getStringArg(args, "rule"); | |
| 20 | + | |
| 21 | + const description = getOptionalStringArg(args, "description"); | |
| 22 | + const regex = getOptionalStringArg(args, "regex"); | |
| 23 | + const globs = getOptionalStringArg(args, "globs"); | |
| 24 | + const alwaysApply = getBooleanArg(args, "alwaysApply", false); | |
| 25 | + | |
| 26 | + const fileContent = createRuleMarkdown(name, rule, { | |
| 27 | + alwaysApply, | |
| 28 | + description, | |
| 29 | + globs, | |
| 30 | + regex, | |
| 31 | + }); | |
| 17 | 32 | |
| 18 | 33 | const [localContinueDir] = await extras.ide.getWorkspaceDirs(); |
| 19 | 34 | const ruleFilePath = createRuleFilePath(localContinueDir, name); |
| @@ -24,7 +39,7 @@ | ||
| 24 | 39 | return [ |
| 25 | 40 | { |
| 26 | 41 | name: "New Rule Block", |
| 27 | - description: otherArgs.description || "", | |
| 42 | + description: description || "", | |
| 28 | 43 | uri: { |
| 29 | 44 | type: "file", |
| 30 | 45 | value: ruleFilePath, |
modified core/tools/implementations/fetchUrlContent.ts +5 −2
| @@ -1,10 +1,13 @@ | ||
| 1 | 1 | import { ToolImpl } from "."; |
| 2 | 2 | import { getUrlContextItems } from "../../context/providers/URLContextProvider"; |
| 3 | +import { getStringArg } from "../parseArgs"; | |
| 3 | 4 | |
| 4 | 5 | const DEFAULT_FETCH_URL_CHAR_LIMIT = 20000; |
| 5 | 6 | |
| 6 | 7 | export const fetchUrlContentImpl: ToolImpl = async (args, extras) => { |
| 7 | - const contextItems = await getUrlContextItems(args.url, extras.fetch); | |
| 8 | + const url = getStringArg(args, "url"); | |
| 9 | + | |
| 10 | + const contextItems = await getUrlContextItems(url, extras.fetch); | |
| 8 | 11 | |
| 9 | 12 | // Track truncated content |
| 10 | 13 | const truncatedUrls: string[] = []; |
| @@ -12,7 +15,7 @@ | ||
| 12 | 15 | // Check and truncate each context item |
| 13 | 16 | const processedItems = contextItems.map((item) => { |
| 14 | 17 | if (item.content.length > DEFAULT_FETCH_URL_CHAR_LIMIT) { |
| 15 | - truncatedUrls.push(args.url); | |
| 18 | + truncatedUrls.push(url); | |
| 16 | 19 | return { |
| 17 | 20 | ...item, |
| 18 | 21 | content: item.content.substring(0, DEFAULT_FETCH_URL_CHAR_LIMIT), |
modified core/tools/implementations/globSearch.ts +4 −1
| @@ -1,10 +1,13 @@ | ||
| 1 | 1 | import { ToolImpl } from "."; |
| 2 | 2 | import { ContextItem } from "../.."; |
| 3 | +import { getStringArg } from "../parseArgs"; | |
| 3 | 4 | |
| 4 | 5 | const MAX_AGENT_GLOB_RESULTS = 100; |
| 6 | + | |
| 5 | 7 | export const fileGlobSearchImpl: ToolImpl = async (args, extras) => { |
| 8 | + const pattern = getStringArg(args, "pattern"); | |
| 6 | 9 | const results = await extras.ide.getFileResults( |
| 7 | - args.pattern, | |
| 10 | + pattern, | |
| 8 | 11 | MAX_AGENT_GLOB_RESULTS, |
| 9 | 12 | ); |
| 10 | 13 | const contextItems: ContextItem[] = [ |
modified core/tools/implementations/grepSearch.ts +5 −2
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | import { ToolImpl } from "."; |
| 2 | 2 | import { ContextItem } from "../.."; |
| 3 | 3 | import { formatGrepSearchResults } from "../../util/grepSearch"; |
| 4 | +import { getStringArg } from "../parseArgs"; | |
| 4 | 5 | |
| 5 | 6 | const DEFAULT_GREP_SEARCH_RESULTS_LIMIT = 100; |
| 6 | 7 | const DEFAULT_GREP_SEARCH_CHAR_LIMIT = 5000; // ~1000 tokens, will keep truncation simply for now |
| @@ -41,8 +42,10 @@ | ||
| 41 | 42 | } |
| 42 | 43 | |
| 43 | 44 | export const grepSearchImpl: ToolImpl = async (args, extras) => { |
| 45 | + const query = getStringArg(args, "query"); | |
| 46 | + | |
| 44 | 47 | const results = await extras.ide.getSearchResults( |
| 45 | - args.query, | |
| 48 | + query, | |
| 46 | 49 | DEFAULT_GREP_SEARCH_RESULTS_LIMIT, |
| 47 | 50 | ); |
| 48 | 51 | const { formatted, numResults, truncated } = formatGrepSearchResults( |
| @@ -63,7 +66,7 @@ | ||
| 63 | 66 | |
| 64 | 67 | let contextItems: ContextItem[] = []; |
| 65 | 68 | |
| 66 | - const splitByFile: boolean = args.splitByFile || false; | |
| 69 | + const splitByFile: boolean = args?.splitByFile || false; | |
| 67 | 70 | if (splitByFile) { |
| 68 | 71 | contextItems = splitGrepResultsByFile(formatted); |
| 69 | 72 | } else { |
modified core/tools/implementations/readFile.ts +8 −8
| @@ -2,28 +2,28 @@ | ||
| 2 | 2 | import { getUriPathBasename } from "../../util/uri"; |
| 3 | 3 | |
| 4 | 4 | import { ToolImpl } from "."; |
| 5 | +import { getStringArg } from "../parseArgs"; | |
| 5 | 6 | import { throwIfFileExceedsHalfOfContext } from "./readFileLimit"; |
| 6 | 7 | |
| 7 | 8 | export const readFileImpl: ToolImpl = async (args, extras) => { |
| 8 | - const firstUriMatch = await resolveRelativePathInDir( | |
| 9 | - args.filepath, | |
| 10 | - extras.ide, | |
| 11 | - ); | |
| 9 | + const filepath = getStringArg(args, "filepath"); | |
| 10 | + | |
| 11 | + const firstUriMatch = await resolveRelativePathInDir(filepath, extras.ide); | |
| 12 | 12 | if (!firstUriMatch) { |
| 13 | - throw new Error(`Could not find file ${args.filepath}`); | |
| 13 | + throw new Error(`Could not find file ${filepath}`); | |
| 14 | 14 | } |
| 15 | 15 | const content = await extras.ide.readFile(firstUriMatch); |
| 16 | 16 | |
| 17 | 17 | await throwIfFileExceedsHalfOfContext( |
| 18 | - args.filepath, | |
| 18 | + filepath, | |
| 19 | 19 | content, |
| 20 | 20 | extras.config.selectedModelByRole.chat, |
| 21 | 21 | ); |
| 22 | 22 | |
| 23 | 23 | return [ |
| 24 | 24 | { |
| 25 | - name: getUriPathBasename(args.filepath), | |
| 26 | - description: args.filepath, | |
| 25 | + name: getUriPathBasename(firstUriMatch), | |
| 26 | + description: filepath, | |
| 27 | 27 | content, |
| 28 | 28 | uri: { |
| 29 | 29 | type: "file", |
modified core/tools/implementations/requestRule.ts +5 −4
| @@ -1,13 +1,14 @@ | ||
| 1 | 1 | import { ToolImpl } from "."; |
| 2 | +import { getStringArg } from "../parseArgs"; | |
| 2 | 3 | |
| 3 | 4 | export const requestRuleImpl: ToolImpl = async (args, extras) => { |
| 5 | + const name = getStringArg(args, "name"); | |
| 6 | + | |
| 4 | 7 | // Find the rule by name in the config |
| 5 | - const rule = extras.config.rules.find((r) => r.name === args.name); | |
| 8 | + const rule = extras.config.rules.find((r) => r.name === name); | |
| 6 | 9 | |
| 7 | 10 | if (!rule || !rule.ruleFile) { |
| 8 | - throw new Error( | |
| 9 | - `Rule with name "${args.name}" not found or has no file path`, | |
| 10 | - ); | |
| 11 | + throw new Error(`Rule with name "${name}" not found or has no file path`); | |
| 11 | 12 | } |
| 12 | 13 | |
| 13 | 14 | return [ |
modified core/tools/implementations/runTerminalCommand.ts +9 −5
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | isProcessBackgrounded, |
| 25 | 25 | removeBackgroundedProcess, |
| 26 | 26 | } from "../../util/processTerminalBackgroundStates"; |
| 27 | +import { getBooleanArg, getStringArg } from "../parseArgs"; | |
| 27 | 28 | |
| 28 | 29 | const asyncExec = util.promisify(childProcess.exec); |
| 29 | 30 | |
| @@ -50,8 +51,11 @@ | ||
| 50 | 51 | ]; |
| 51 | 52 | |
| 52 | 53 | export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { |
| 54 | + const command = getStringArg(args, "command"); | |
| 53 | 55 | // Default to waiting for completion if not specified |
| 54 | - const waitForCompletion = args.waitForCompletion !== false; | |
| 56 | + const waitForCompletion = | |
| 57 | + getBooleanArg(args, "waitForCompletion", false) ?? true; | |
| 58 | + | |
| 55 | 59 | const ideInfo = await extras.ide.getIdeInfo(); |
| 56 | 60 | const toolCallId = extras.toolCallId || ""; |
| 57 | 61 | |
| @@ -84,7 +88,7 @@ | ||
| 84 | 88 | } |
| 85 | 89 | |
| 86 | 90 | // Use spawn with color environment |
| 87 | - const childProc = childProcess.spawn(args.command, { | |
| 91 | + const childProc = childProcess.spawn(command, { | |
| 88 | 92 | cwd, |
| 89 | 93 | shell: true, |
| 90 | 94 | env: getColorEnv(), // Add enhanced environment for colors |
| @@ -229,7 +233,7 @@ | ||
| 229 | 233 | // Standard execution, waiting for completion |
| 230 | 234 | try { |
| 231 | 235 | // Use color environment for exec as well |
| 232 | - const output = await asyncExec(args.command, { | |
| 236 | + const output = await asyncExec(command, { | |
| 233 | 237 | cwd, |
| 234 | 238 | env: getColorEnv(), |
| 235 | 239 | }); |
| @@ -258,7 +262,7 @@ | ||
| 258 | 262 | // but don't attach any listeners other than error |
| 259 | 263 | try { |
| 260 | 264 | // Use spawn with color environment |
| 261 | - const childProc = childProcess.spawn(args.command, { | |
| 265 | + const childProc = childProcess.spawn(command, { | |
| 262 | 266 | cwd, |
| 263 | 267 | shell: true, |
| 264 | 268 | env: getColorEnv(), // Add color environment |
| @@ -309,7 +313,7 @@ | ||
| 309 | 313 | |
| 310 | 314 | // For remote environments, just run the command |
| 311 | 315 | // Note: waitForCompletion is not supported in remote environments yet |
| 312 | - await extras.ide.runCommand(args.command); | |
| 316 | + await extras.ide.runCommand(command); | |
| 313 | 317 | return [ |
| 314 | 318 | { |
| 315 | 319 | name: "Terminal", |
modified core/tools/implementations/searchWeb.ts +4 −1
| @@ -1,10 +1,13 @@ | ||
| 1 | 1 | import { ToolImpl } from "."; |
| 2 | 2 | import { fetchSearchResults } from "../../context/providers/WebContextProvider"; |
| 3 | +import { getStringArg } from "../parseArgs"; | |
| 3 | 4 | |
| 4 | 5 | const DEFAULT_WEB_SEARCH_CHAR_LIMIT = 8000; |
| 5 | 6 | |
| 6 | 7 | export const searchWebImpl: ToolImpl = async (args, extras) => { |
| 7 | - const webResults = await fetchSearchResults(args.query, 5, extras.fetch); | |
| 8 | + const query = getStringArg(args, "query"); | |
| 9 | + | |
| 10 | + const webResults = await fetchSearchResults(query, 5, extras.fetch); | |
| 8 | 11 | |
| 9 | 12 | // Track truncated results |
| 10 | 13 | const truncatedResults: string[] = []; |
modified core/tools/implementations/viewSubdirectory.ts +3 −1
| @@ -2,9 +2,11 @@ | ||
| 2 | 2 | import { resolveRelativePathInDir } from "../../util/ideUtils"; |
| 3 | 3 | |
| 4 | 4 | import { ToolImpl } from "."; |
| 5 | +import { getStringArg } from "../parseArgs"; | |
| 5 | 6 | |
| 6 | 7 | export const viewSubdirectoryImpl: ToolImpl = async (args: any, extras) => { |
| 7 | - const { directory_path } = args; | |
| 8 | + const directory_path = getStringArg(args, "directory_path"); | |
| 9 | + | |
| 8 | 10 | const uri = await resolveRelativePathInDir(directory_path, extras.ide); |
| 9 | 11 | |
| 10 | 12 | if (!uri) { |
modified core/tools/parseArgs.ts +49 −0
| @@ -12,3 +12,52 @@ | ||
| 12 | 12 | return {}; |
| 13 | 13 | } |
| 14 | 14 | } |
| 15 | + | |
| 16 | +export function getStringArg( | |
| 17 | + args: any, | |
| 18 | + argName: string, | |
| 19 | + allowEmpty = false, | |
| 20 | +): string { | |
| 21 | + if (!args || !(argName in args) || typeof args[argName] !== "string") { | |
| 22 | + throw new Error( | |
| 23 | + `\`${argName}\` argument is required${allowEmpty ? "" : " and must not be empty"}. (type string)`, | |
| 24 | + ); | |
| 25 | + } | |
| 26 | + if (!allowEmpty && !args[argName].trim()) { | |
| 27 | + throw new Error(`Argument ${argName} must not be empty`); | |
| 28 | + } | |
| 29 | + return args[argName]; | |
| 30 | +} | |
| 31 | + | |
| 32 | +export function getOptionalStringArg( | |
| 33 | + args: any, | |
| 34 | + argName: string, | |
| 35 | + allowEmpty = false, | |
| 36 | +) { | |
| 37 | + if (typeof args?.[argName] === "undefined") { | |
| 38 | + return undefined; | |
| 39 | + } | |
| 40 | + return getStringArg(args, argName, allowEmpty); | |
| 41 | +} | |
| 42 | + | |
| 43 | +export function getBooleanArg(args: any, argName: string, required = false) { | |
| 44 | + if (!args || !(argName in args)) { | |
| 45 | + if (required) { | |
| 46 | + throw new Error(`Argument \`${argName}\` is required (type boolean)`); | |
| 47 | + } else { | |
| 48 | + return undefined; | |
| 49 | + } | |
| 50 | + } | |
| 51 | + if (typeof args[argName] === "string") { | |
| 52 | + if (args[argName].toLowerCase() === "false") { | |
| 53 | + return false; | |
| 54 | + } | |
| 55 | + if (args[argName].toLowerCase() === "true") { | |
| 56 | + return true; | |
| 57 | + } | |
| 58 | + } | |
| 59 | + if (typeof args[argName] !== "boolean") { | |
| 60 | + throw new Error(`Argument \`${argName}\` must be a boolean true or false`); | |
| 61 | + } | |
| 62 | + return args[argName]; | |
| 63 | +} |
added core/tools/parseArgs.vitest.ts +248 −0
| @@ -0,0 +1,248 @@ | ||
| 1 | +import { describe, expect, it } from "vitest"; | |
| 2 | +import { ToolCallDelta } from ".."; | |
| 3 | +import { | |
| 4 | + getBooleanArg, | |
| 5 | + getOptionalStringArg, | |
| 6 | + getStringArg, | |
| 7 | + safeParseToolCallArgs, | |
| 8 | +} from "./parseArgs"; | |
| 9 | + | |
| 10 | +describe("safeParseToolCallArgs", () => { | |
| 11 | + it("should parse valid JSON arguments", () => { | |
| 12 | + const toolCall: ToolCallDelta = { | |
| 13 | + id: "1", | |
| 14 | + function: { | |
| 15 | + name: "testFunction", | |
| 16 | + arguments: '{"name": "test", "value": 123}', | |
| 17 | + }, | |
| 18 | + }; | |
| 19 | + | |
| 20 | + const result = safeParseToolCallArgs(toolCall); | |
| 21 | + expect(result).toEqual({ name: "test", value: 123 }); | |
| 22 | + }); | |
| 23 | + | |
| 24 | + it("should parse valid JSON arguments with whitespace", () => { | |
| 25 | + const toolCall: ToolCallDelta = { | |
| 26 | + id: "1", | |
| 27 | + function: { | |
| 28 | + name: "testFunction", | |
| 29 | + arguments: ' {"name": "test", "value": 123} ', | |
| 30 | + }, | |
| 31 | + }; | |
| 32 | + | |
| 33 | + const result = safeParseToolCallArgs(toolCall); | |
| 34 | + expect(result).toEqual({ name: "test", value: 123 }); | |
| 35 | + }); | |
| 36 | + | |
| 37 | + it("should return empty object for invalid JSON", () => { | |
| 38 | + const toolCall: ToolCallDelta = { | |
| 39 | + id: "1", | |
| 40 | + function: { | |
| 41 | + name: "testFunction", | |
| 42 | + arguments: '{"name": "test", value: 123', // Invalid JSON | |
| 43 | + }, | |
| 44 | + }; | |
| 45 | + | |
| 46 | + const result = safeParseToolCallArgs(toolCall); | |
| 47 | + expect(result).toEqual({}); | |
| 48 | + }); | |
| 49 | + | |
| 50 | + it("should return empty object when arguments is empty", () => { | |
| 51 | + const toolCall: ToolCallDelta = { | |
| 52 | + id: "1", | |
| 53 | + function: { | |
| 54 | + name: "testFunction", | |
| 55 | + arguments: "", | |
| 56 | + }, | |
| 57 | + }; | |
| 58 | + | |
| 59 | + const result = safeParseToolCallArgs(toolCall); | |
| 60 | + expect(result).toEqual({}); | |
| 61 | + }); | |
| 62 | + | |
| 63 | + it("should return empty object when arguments is null or undefined", () => { | |
| 64 | + const toolCall1: ToolCallDelta = { | |
| 65 | + id: "1", | |
| 66 | + function: { | |
| 67 | + name: "testFunction", | |
| 68 | + arguments: null as any, | |
| 69 | + }, | |
| 70 | + }; | |
| 71 | + | |
| 72 | + const toolCall2: ToolCallDelta = { | |
| 73 | + id: "1", | |
| 74 | + function: { | |
| 75 | + name: "testFunction", | |
| 76 | + arguments: undefined, | |
| 77 | + }, | |
| 78 | + }; | |
| 79 | + | |
| 80 | + expect(safeParseToolCallArgs(toolCall1)).toEqual({}); | |
| 81 | + expect(safeParseToolCallArgs(toolCall2)).toEqual({}); | |
| 82 | + }); | |
| 83 | + | |
| 84 | + it("should handle case when function is undefined", () => { | |
| 85 | + const toolCall: ToolCallDelta = { | |
| 86 | + id: "1", | |
| 87 | + function: undefined, | |
| 88 | + }; | |
| 89 | + | |
| 90 | + const result = safeParseToolCallArgs(toolCall); | |
| 91 | + expect(result).toEqual({}); | |
| 92 | + }); | |
| 93 | +}); | |
| 94 | + | |
| 95 | +describe("getStringArg", () => { | |
| 96 | + it("should return string argument when valid", () => { | |
| 97 | + const args = { name: "test" }; | |
| 98 | + const result = getStringArg(args, "name"); | |
| 99 | + expect(result).toBe("test"); | |
| 100 | + }); | |
| 101 | + | |
| 102 | + it("should throw error when argument is missing", () => { | |
| 103 | + const args = { otherArg: "test" }; | |
| 104 | + expect(() => getStringArg(args, "name")).toThrowError( | |
| 105 | + "`name` argument is required and must not be empty. (type string)", | |
| 106 | + ); | |
| 107 | + }); | |
| 108 | + | |
| 109 | + it("should throw error when argument is not a string", () => { | |
| 110 | + const args = { name: 123 }; | |
| 111 | + expect(() => getStringArg(args, "name")).toThrowError( | |
| 112 | + "`name` argument is required and must not be empty. (type string)", | |
| 113 | + ); | |
| 114 | + }); | |
| 115 | + | |
| 116 | + it("should throw error when argument is empty and empty not allowed", () => { | |
| 117 | + const args = { name: "" }; | |
| 118 | + expect(() => getStringArg(args, "name")).toThrowError( | |
| 119 | + "Argument name must not be empty", | |
| 120 | + ); | |
| 121 | + | |
| 122 | + const argsWithSpace = { name: " " }; | |
| 123 | + expect(() => getStringArg(argsWithSpace, "name")).toThrowError( | |
| 124 | + "Argument name must not be empty", | |
| 125 | + ); | |
| 126 | + }); | |
| 127 | + | |
| 128 | + it("should accept empty string when allowEmpty is true", () => { | |
| 129 | + const args = { name: "" }; | |
| 130 | + const result = getStringArg(args, "name", true); | |
| 131 | + expect(result).toBe(""); | |
| 132 | + | |
| 133 | + const argsWithSpace = { name: " " }; | |
| 134 | + const resultWithSpace = getStringArg(argsWithSpace, "name", true); | |
| 135 | + expect(resultWithSpace).toBe(" "); | |
| 136 | + }); | |
| 137 | + | |
| 138 | + it("should handle null or undefined args", () => { | |
| 139 | + expect(() => getStringArg(null, "name")).toThrowError( | |
| 140 | + "`name` argument is required and must not be empty. (type string)", | |
| 141 | + ); | |
| 142 | + expect(() => getStringArg(undefined, "name")).toThrowError( | |
| 143 | + "`name` argument is required and must not be empty. (type string)", | |
| 144 | + ); | |
| 145 | + }); | |
| 146 | +}); | |
| 147 | + | |
| 148 | +describe("getOptionalStringArg", () => { | |
| 149 | + it("should return undefined when argument is not provided", () => { | |
| 150 | + const args = { otherArg: "test" }; | |
| 151 | + const result = getOptionalStringArg(args, "name"); | |
| 152 | + expect(result).toBeUndefined(); | |
| 153 | + }); | |
| 154 | + | |
| 155 | + it("should return string argument when valid", () => { | |
| 156 | + const args = { name: "test" }; | |
| 157 | + const result = getOptionalStringArg(args, "name"); | |
| 158 | + expect(result).toBe("test"); | |
| 159 | + }); | |
| 160 | + | |
| 161 | + it("should throw error when argument is not a string", () => { | |
| 162 | + const args = { name: 123 }; | |
| 163 | + expect(() => getOptionalStringArg(args, "name")).toThrowError( | |
| 164 | + "`name` argument is required and must not be empty. (type string)", | |
| 165 | + ); | |
| 166 | + }); | |
| 167 | + | |
| 168 | + it("should throw error when argument is empty and empty not allowed", () => { | |
| 169 | + const args = { name: "" }; | |
| 170 | + expect(() => getOptionalStringArg(args, "name")).toThrowError( | |
| 171 | + "Argument name must not be empty", | |
| 172 | + ); | |
| 173 | + }); | |
| 174 | + | |
| 175 | + it("should accept empty string when allowEmpty is true", () => { | |
| 176 | + const args = { name: "" }; | |
| 177 | + const result = getOptionalStringArg(args, "name", true); | |
| 178 | + expect(result).toBe(""); | |
| 179 | + }); | |
| 180 | + | |
| 181 | + it("should handle null or undefined args", () => { | |
| 182 | + expect(getOptionalStringArg(null, "name")).toBeUndefined(); | |
| 183 | + expect(getOptionalStringArg(undefined, "name")).toBeUndefined(); | |
| 184 | + }); | |
| 185 | +}); | |
| 186 | + | |
| 187 | +describe("getBooleanArg", () => { | |
| 188 | + it("should return boolean argument when valid", () => { | |
| 189 | + const argsTrue = { flag: true }; | |
| 190 | + const argsFalse = { flag: false }; | |
| 191 | + | |
| 192 | + expect(getBooleanArg(argsTrue, "flag")).toBe(true); | |
| 193 | + expect(getBooleanArg(argsFalse, "flag")).toBe(false); | |
| 194 | + }); | |
| 195 | + | |
| 196 | + it("should return undefined when argument is not provided and not required", () => { | |
| 197 | + const args = { otherArg: true }; | |
| 198 | + const result = getBooleanArg(args, "flag"); | |
| 199 | + expect(result).toBeUndefined(); | |
| 200 | + }); | |
| 201 | + | |
| 202 | + it("should throw error when argument is not provided and required", () => { | |
| 203 | + const args = { otherArg: true }; | |
| 204 | + expect(() => getBooleanArg(args, "flag", true)).toThrowError( | |
| 205 | + "Argument `flag` is required (type boolean)", | |
| 206 | + ); | |
| 207 | + }); | |
| 208 | + | |
| 209 | + it('should handle string "true" and "false" values', () => { | |
| 210 | + const argsStringTrue = { flag: "true" }; | |
| 211 | + const argsStringFalse = { flag: "false" }; | |
| 212 | + const argsStringTrueUppercase = { flag: "TRUE" }; | |
| 213 | + const argsStringFalseUppercase = { flag: "FALSE" }; | |
| 214 | + | |
| 215 | + expect(getBooleanArg(argsStringTrue, "flag")).toBe(true); | |
| 216 | + expect(getBooleanArg(argsStringFalse, "flag")).toBe(false); | |
| 217 | + expect(getBooleanArg(argsStringTrueUppercase, "flag")).toBe(true); | |
| 218 | + expect(getBooleanArg(argsStringFalseUppercase, "flag")).toBe(false); | |
| 219 | + }); | |
| 220 | + | |
| 221 | + it("should throw error when argument is not a boolean or valid boolean string", () => { | |
| 222 | + const argsNumber = { flag: 1 }; | |
| 223 | + const argsInvalidString = { flag: "yes" }; | |
| 224 | + const argsObject = { flag: {} }; | |
| 225 | + | |
| 226 | + expect(() => getBooleanArg(argsNumber, "flag")).toThrowError( | |
| 227 | + "Argument `flag` must be a boolean true or false", | |
| 228 | + ); | |
| 229 | + expect(() => getBooleanArg(argsInvalidString, "flag")).toThrowError( | |
| 230 | + "Argument `flag` must be a boolean true or false", | |
| 231 | + ); | |
| 232 | + expect(() => getBooleanArg(argsObject, "flag")).toThrowError( | |
| 233 | + "Argument `flag` must be a boolean true or false", | |
| 234 | + ); | |
| 235 | + }); | |
| 236 | + | |
| 237 | + it("should handle null or undefined args", () => { | |
| 238 | + expect(getBooleanArg(null, "flag")).toBeUndefined(); | |
| 239 | + expect(getBooleanArg(undefined, "flag")).toBeUndefined(); | |
| 240 | + | |
| 241 | + expect(() => getBooleanArg(null, "flag", true)).toThrowError( | |
| 242 | + "Argument `flag` is required (type boolean)", | |
| 243 | + ); | |
| 244 | + expect(() => getBooleanArg(undefined, "flag", true)).toThrowError( | |
| 245 | + "Argument `flag` is required (type boolean)", | |
| 246 | + ); | |
| 247 | + }); | |
| 248 | +}); |
modified gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx +12 −7
| @@ -18,25 +18,30 @@ | ||
| 18 | 18 | case BuiltInToolNames.CreateNewFile: |
| 19 | 19 | return ( |
| 20 | 20 | <CreateFile |
| 21 | - relativeFilepath={args.filepath} | |
| 22 | - fileContents={args.contents} | |
| 21 | + relativeFilepath={args?.filepath ?? ""} | |
| 22 | + fileContents={args?.contents ?? ""} | |
| 23 | 23 | historyIndex={historyIndex} |
| 24 | 24 | /> |
| 25 | 25 | ); |
| 26 | 26 | case BuiltInToolNames.EditExistingFile: |
| 27 | 27 | return ( |
| 28 | 28 | <EditFile |
| 29 | - relativeFilePath={args.filepath ?? ""} | |
| 30 | - changes={args.changes ?? ""} | |
| 29 | + relativeFilePath={args?.filepath ?? ""} | |
| 30 | + changes={args?.changes ?? ""} | |
| 31 | 31 | toolCallId={toolCall.id} |
| 32 | 32 | historyIndex={historyIndex} |
| 33 | 33 | /> |
| 34 | 34 | ); |
| 35 | 35 | case BuiltInToolNames.SearchAndReplaceInFile: |
| 36 | + const changes = args.diffs | |
| 37 | + ? Array.isArray(args.diffs) | |
| 38 | + ? args.diffs.join("\n\n---\n\n") | |
| 39 | + : args.diffs | |
| 40 | + : ""; | |
| 36 | 41 | return ( |
| 37 | 42 | <EditFile |
| 38 | - relativeFilePath={args.filepath ?? ""} | |
| 39 | - changes={args.diffs ? args.diffs.join("\n\n---\n\n") : ""} | |
| 43 | + relativeFilePath={args?.filepath ?? ""} | |
| 44 | + changes={changes} | |
| 40 | 45 | toolCallId={toolCall.id} |
| 41 | 46 | historyIndex={historyIndex} |
| 42 | 47 | /> |
| @@ -44,7 +49,7 @@ | ||
| 44 | 49 | case BuiltInToolNames.RunTerminalCommand: |
| 45 | 50 | return ( |
| 46 | 51 | <RunTerminalCommand |
| 47 | - command={args.command} | |
| 52 | + command={args?.command ?? ""} | |
| 48 | 53 | toolCallState={toolCallState} |
| 49 | 54 | toolCallId={toolCall.id} |
| 50 | 55 | /> |
modified gui/src/util/clientTools/editImpl.ts +5 −0
| @@ -6,6 +6,11 @@ | ||
| 6 | 6 | toolCallId, |
| 7 | 7 | extras, |
| 8 | 8 | ) => { |
| 9 | + if (!args.filepath || !args.changes) { | |
| 10 | + throw new Error( | |
| 11 | + "`filepath` and `changes` arguments are required to edit an existing file.", | |
| 12 | + ); | |
| 13 | + } | |
| 9 | 14 | const firstUriMatch = await resolveRelativePathInDir( |
| 10 | 15 | args.filepath, |
| 11 | 16 | extras.ideMessenger.ide, |