Changesets · Branches · Tags · Compare
| Changeset | b211d5ee576503976015c2b33e38e61d2c149f881addd5afe2a07cbf47cd9e57 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-07-21 15:31 +02:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-07-21 15:31 +02:00 |
| Parents | 9ea2d7361601 |
13 files changed
modified core/tools/implementations/codebaseTool.ts +4 −5
| @@ -1,16 +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 | - throw new Error("`query` parameter is required and cannot be empty."); | |
| 8 | - } | |
| 7 | + const query = getStringArg(args, "query"); | |
| 9 | 8 | |
| 10 | 9 | try { |
| 11 | 10 | const contextExtras = { |
| 12 | 11 | config: extras.config, |
| 13 | - fullInput: args.query, | |
| 12 | + fullInput: query, | |
| 14 | 13 | embeddingsProvider: extras.config.selectedModelByRole.embed, |
| 15 | 14 | reranker: extras.config.selectedModelByRole.rerank, |
| 16 | 15 | llm: extras.llm, |
| @@ -33,7 +32,7 @@ | ||
| 33 | 32 | { |
| 34 | 33 | name: "No Results", |
| 35 | 34 | description: "Codebase search", |
| 36 | - content: `No relevant code found for query: "${args.query}". This could mean: | |
| 35 | + content: `No relevant code found for query: "${query}". This could mean: | |
| 37 | 36 | - The codebase hasn't been indexed yet |
| 38 | 37 | - No code matches the search criteria |
| 39 | 38 | - Embeddings provider is not configured |
modified core/tools/implementations/createNewFile.ts +7 −11
| @@ -2,28 +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) => { |
| 7 | - if (!args?.filepath) { | |
| 8 | - throw new Error("`filepath` argument is required and cannot be empty."); | |
| 9 | - } | |
| 10 | - if (args?.contents === undefined) { | |
| 11 | - throw new Error( | |
| 12 | - "`contents` argument is required to create a new file (can be empty).", | |
| 13 | - ); | |
| 14 | - } | |
| 8 | + const filepath = getStringArg(args, "filepath"); | |
| 9 | + const contents = getStringArg(args, "contents", true); | |
| 10 | + | |
| 15 | 11 | const resolvedFileUri = await inferResolvedUriFromRelativePath( |
| 16 | - args.filepath, | |
| 12 | + filepath, | |
| 17 | 13 | extras.ide, |
| 18 | 14 | ); |
| 19 | 15 | if (resolvedFileUri) { |
| 20 | 16 | const exists = await extras.ide.fileExists(resolvedFileUri); |
| 21 | 17 | if (exists) { |
| 22 | 18 | throw new Error( |
| 23 | - `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`, | |
| 24 | 20 | ); |
| 25 | 21 | } |
| 26 | - await extras.ide.writeFile(resolvedFileUri, args.contents); | |
| 22 | + await extras.ide.writeFile(resolvedFileUri, contents); | |
| 27 | 23 | await extras.ide.openFile(resolvedFileUri); |
| 28 | 24 | await extras.ide.saveFile(resolvedFileUri); |
| 29 | 25 | if (extras.codeBaseIndexer) { |
modified core/tools/implementations/createRuleBlock.ts +21 −11
| @@ -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,16 +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 | - if (!name || !rule) { | |
| 17 | - throw new Error( | |
| 18 | - "`name` and `rule` arguments are required to create a rule.", | |
| 19 | - ); | |
| 20 | - } | |
| 21 | - 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 | + }); | |
| 22 | 32 | |
| 23 | 33 | const [localContinueDir] = await extras.ide.getWorkspaceDirs(); |
| 24 | 34 | const ruleFilePath = createRuleFilePath(localContinueDir, name); |
| @@ -29,7 +39,7 @@ | ||
| 29 | 39 | return [ |
| 30 | 40 | { |
| 31 | 41 | name: "New Rule Block", |
| 32 | - description: otherArgs.description || "", | |
| 42 | + description: description || "", | |
| 33 | 43 | uri: { |
| 34 | 44 | type: "file", |
| 35 | 45 | value: ruleFilePath, |
modified core/tools/implementations/fetchUrlContent.ts +4 −5
| @@ -1,14 +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 | - if (!args?.url) { | |
| 8 | - throw new Error("`url` argument is required and cannot be empty"); | |
| 9 | - } | |
| 8 | + const url = getStringArg(args, "url"); | |
| 10 | 9 | |
| 11 | - const contextItems = await getUrlContextItems(args.url, extras.fetch); | |
| 10 | + const contextItems = await getUrlContextItems(url, extras.fetch); | |
| 12 | 11 | |
| 13 | 12 | // Track truncated content |
| 14 | 13 | const truncatedUrls: string[] = []; |
| @@ -16,7 +15,7 @@ | ||
| 16 | 15 | // Check and truncate each context item |
| 17 | 16 | const processedItems = contextItems.map((item) => { |
| 18 | 17 | if (item.content.length > DEFAULT_FETCH_URL_CHAR_LIMIT) { |
| 19 | - truncatedUrls.push(args.url); | |
| 18 | + truncatedUrls.push(url); | |
| 20 | 19 | return { |
| 21 | 20 | ...item, |
| 22 | 21 | content: item.content.substring(0, DEFAULT_FETCH_URL_CHAR_LIMIT), |
modified core/tools/implementations/globSearch.ts +3 −6
| @@ -1,16 +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; |
| 5 | 6 | |
| 6 | 7 | export const fileGlobSearchImpl: ToolImpl = async (args, extras) => { |
| 7 | - if (!args?.pattern) { | |
| 8 | - throw new Error( | |
| 9 | - "`pattern` argument is required for a glob search, and cannot be empty", | |
| 10 | - ); | |
| 11 | - } | |
| 8 | + const pattern = getStringArg(args, "pattern"); | |
| 12 | 9 | const results = await extras.ide.getFileResults( |
| 13 | - args.pattern, | |
| 10 | + pattern, | |
| 14 | 11 | MAX_AGENT_GLOB_RESULTS, |
| 15 | 12 | ); |
| 16 | 13 | const contextItems: ContextItem[] = [ |
modified core/tools/implementations/grepSearch.ts +4 −7
| @@ -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,14 +42,10 @@ | ||
| 41 | 42 | } |
| 42 | 43 | |
| 43 | 44 | export const grepSearchImpl: ToolImpl = async (args, extras) => { |
| 44 | - if (!args?.query) { | |
| 45 | - throw new Error( | |
| 46 | - "`query` argument is required for a grep search, and cannot be empty", | |
| 47 | - ); | |
| 48 | - } | |
| 45 | + const query = getStringArg(args, "query"); | |
| 49 | 46 | |
| 50 | 47 | const results = await extras.ide.getSearchResults( |
| 51 | - args.query, | |
| 48 | + query, | |
| 52 | 49 | DEFAULT_GREP_SEARCH_RESULTS_LIMIT, |
| 53 | 50 | ); |
| 54 | 51 | const { formatted, numResults, truncated } = formatGrepSearchResults( |
| @@ -69,7 +66,7 @@ | ||
| 69 | 66 | |
| 70 | 67 | let contextItems: ContextItem[] = []; |
| 71 | 68 | |
| 72 | - const splitByFile: boolean = args.splitByFile || false; | |
| 69 | + const splitByFile: boolean = args?.splitByFile || false; | |
| 73 | 70 | if (splitByFile) { |
| 74 | 71 | contextItems = splitGrepResultsByFile(formatted); |
| 75 | 72 | } else { |
modified core/tools/implementations/readFile.ts +7 −13
| @@ -2,34 +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 | - if (!args?.filepath) { | |
| 9 | - throw new Error( | |
| 10 | - "`filepath` argument is required to read a file, and cannot be empty", | |
| 11 | - ); | |
| 12 | - } | |
| 9 | + const filepath = getStringArg(args, "filepath"); | |
| 13 | 10 | |
| 14 | - const firstUriMatch = await resolveRelativePathInDir( | |
| 15 | - args.filepath, | |
| 16 | - extras.ide, | |
| 17 | - ); | |
| 11 | + const firstUriMatch = await resolveRelativePathInDir(filepath, extras.ide); | |
| 18 | 12 | if (!firstUriMatch) { |
| 19 | - throw new Error(`Could not find file ${args.filepath}`); | |
| 13 | + throw new Error(`Could not find file ${filepath}`); | |
| 20 | 14 | } |
| 21 | 15 | const content = await extras.ide.readFile(firstUriMatch); |
| 22 | 16 | |
| 23 | 17 | await throwIfFileExceedsHalfOfContext( |
| 24 | - args.filepath, | |
| 18 | + filepath, | |
| 25 | 19 | content, |
| 26 | 20 | extras.config.selectedModelByRole.chat, |
| 27 | 21 | ); |
| 28 | 22 | |
| 29 | 23 | return [ |
| 30 | 24 | { |
| 31 | - name: getUriPathBasename(args.filepath), | |
| 32 | - description: args.filepath, | |
| 25 | + name: getUriPathBasename(firstUriMatch), | |
| 26 | + description: filepath, | |
| 33 | 27 | content, |
| 34 | 28 | uri: { |
| 35 | 29 | type: "file", |
modified core/tools/implementations/requestRule.ts +4 −9
| @@ -1,19 +1,14 @@ | ||
| 1 | 1 | import { ToolImpl } from "."; |
| 2 | +import { getStringArg } from "../parseArgs"; | |
| 2 | 3 | |
| 3 | 4 | export const requestRuleImpl: ToolImpl = async (args, extras) => { |
| 4 | - if (!args?.name) { | |
| 5 | - throw new Error( | |
| 6 | - "`name` argument is required to request a rule, and cannot be empty", | |
| 7 | - ); | |
| 8 | - } | |
| 5 | + const name = getStringArg(args, "name"); | |
| 9 | 6 | |
| 10 | 7 | // Find the rule by name in the config |
| 11 | - const rule = extras.config.rules.find((r) => r.name === args.name); | |
| 8 | + const rule = extras.config.rules.find((r) => r.name === name); | |
| 12 | 9 | |
| 13 | 10 | if (!rule || !rule.ruleFile) { |
| 14 | - throw new Error( | |
| 15 | - `Rule with name "${args.name}" not found or has no file path`, | |
| 16 | - ); | |
| 11 | + throw new Error(`Rule with name "${name}" not found or has no file path`); | |
| 17 | 12 | } |
| 18 | 13 | |
| 19 | 14 | return [ |
modified core/tools/implementations/runTerminalCommand.ts +9 −11
| @@ -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,14 +51,11 @@ | ||
| 50 | 51 | ]; |
| 51 | 52 | |
| 52 | 53 | export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { |
| 53 | - if (!args?.command) { | |
| 54 | - throw new Error( | |
| 55 | - "`command` argument is required to run a terminal command, and cannot be empty", | |
| 56 | - ); | |
| 57 | - } | |
| 58 | - | |
| 54 | + const command = getStringArg(args, "command"); | |
| 59 | 55 | // Default to waiting for completion if not specified |
| 60 | - const waitForCompletion = args.waitForCompletion !== false; | |
| 56 | + const waitForCompletion = | |
| 57 | + getBooleanArg(args, "waitForCompletion", false) ?? true; | |
| 58 | + | |
| 61 | 59 | const ideInfo = await extras.ide.getIdeInfo(); |
| 62 | 60 | const toolCallId = extras.toolCallId || ""; |
| 63 | 61 | |
| @@ -90,7 +88,7 @@ | ||
| 90 | 88 | } |
| 91 | 89 | |
| 92 | 90 | // Use spawn with color environment |
| 93 | - const childProc = childProcess.spawn(args.command, { | |
| 91 | + const childProc = childProcess.spawn(command, { | |
| 94 | 92 | cwd, |
| 95 | 93 | shell: true, |
| 96 | 94 | env: getColorEnv(), // Add enhanced environment for colors |
| @@ -235,7 +233,7 @@ | ||
| 235 | 233 | // Standard execution, waiting for completion |
| 236 | 234 | try { |
| 237 | 235 | // Use color environment for exec as well |
| 238 | - const output = await asyncExec(args.command, { | |
| 236 | + const output = await asyncExec(command, { | |
| 239 | 237 | cwd, |
| 240 | 238 | env: getColorEnv(), |
| 241 | 239 | }); |
| @@ -264,7 +262,7 @@ | ||
| 264 | 262 | // but don't attach any listeners other than error |
| 265 | 263 | try { |
| 266 | 264 | // Use spawn with color environment |
| 267 | - const childProc = childProcess.spawn(args.command, { | |
| 265 | + const childProc = childProcess.spawn(command, { | |
| 268 | 266 | cwd, |
| 269 | 267 | shell: true, |
| 270 | 268 | env: getColorEnv(), // Add color environment |
| @@ -315,7 +313,7 @@ | ||
| 315 | 313 | |
| 316 | 314 | // For remote environments, just run the command |
| 317 | 315 | // Note: waitForCompletion is not supported in remote environments yet |
| 318 | - await extras.ide.runCommand(args.command); | |
| 316 | + await extras.ide.runCommand(command); | |
| 319 | 317 | return [ |
| 320 | 318 | { |
| 321 | 319 | name: "Terminal", |
modified core/tools/implementations/searchWeb.ts +3 −6
| @@ -1,16 +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 | - if (!args?.query) { | |
| 8 | - throw new Error( | |
| 9 | - "`query` argument is required to search the web, and cannot be empty", | |
| 10 | - ); | |
| 11 | - } | |
| 8 | + const query = getStringArg(args, "query"); | |
| 12 | 9 | |
| 13 | - const webResults = await fetchSearchResults(args.query, 5, extras.fetch); | |
| 10 | + const webResults = await fetchSearchResults(query, 5, extras.fetch); | |
| 14 | 11 | |
| 15 | 12 | // Track truncated results |
| 16 | 13 | const truncatedResults: string[] = []; |
modified core/tools/implementations/viewSubdirectory.ts +2 −6
| @@ -2,14 +2,10 @@ | ||
| 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 | - if (!args?.directory_path) { | |
| 8 | - throw new Error( | |
| 9 | - "`directory_path` argument is required to view a map of a subdirectory, and cannot be empty", | |
| 10 | - ); | |
| 11 | - } | |
| 12 | - const { directory_path } = args; | |
| 8 | + const directory_path = getStringArg(args, "directory_path"); | |
| 13 | 9 | |
| 14 | 10 | const uri = await resolveRelativePathInDir(directory_path, extras.ide); |
| 15 | 11 |
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 | +}); |