Elffuss BraidHub

kikocis/continue public

59947ce7bb53 · 1.7 KiBRawPermalink
1import { ToolCallDelta } from "..";
2
3export function safeParseToolCallArgs(
4 toolCall: ToolCallDelta,
5): Record<string, any> {
6 try {
7 return JSON.parse(toolCall.function?.arguments?.trim() || "{}");
8 } catch (e) {
9 //console.error(
10 // `Failed to parse tool call arguments:\nTool call: ${toolCall.function?.name + " " + toolCall.id}\nArgs:${toolCall.function?.arguments}\n`,
11 //);
12 return {};
13 }
14}
15
16export 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 or whitespace-only"}. (type string)`,
24 );
25 }
26 if (!allowEmpty && !args[argName].trim()) {
27 throw new Error(`Argument ${argName} must not be empty or whitespace-only`);
28 }
29 return args[argName];
30}
31
32export 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
43export 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}