( args: any, argName: string, allowEmpty = false, )
| 63 | } |
| 64 | |
| 65 | export function getStringArg( |
| 66 | args: any, |
| 67 | argName: string, |
| 68 | allowEmpty = false, |
| 69 | ): string { |
| 70 | if (!args || !(argName in args)) { |
| 71 | throw new Error( |
| 72 | `\`${argName}\` argument is required${allowEmpty ? "" : " and must not be empty or whitespace-only"}. (type string)`, |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | let value = args[argName]; |
| 77 | |
| 78 | // Handle case where JSON was parsed into an object by the tool call parser. |
| 79 | // If the arguments to the tool call are valid JSON (e.g. the model attempts to create a .json file) |
| 80 | // the earlier call to JSON.parse() will have deeply parsed the returned arguments. |
| 81 | // If that has happened, convert back to string. |
| 82 | if (typeof value === "object" && value !== null) { |
| 83 | try { |
| 84 | value = JSON.stringify(value); |
| 85 | return value; |
| 86 | } catch (e) { |
| 87 | //Swallow this, because it might be fine later. |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (typeof value !== "string") { |
| 92 | throw new Error( |
| 93 | `\`${argName}\` argument is required${allowEmpty ? "" : " and must not be empty or whitespace-only"}. (type string)`, |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | if (!allowEmpty && !value.trim()) { |
| 98 | throw new Error(`Argument ${argName} must not be empty or whitespace-only`); |
| 99 | } |
| 100 | |
| 101 | return value; |
| 102 | } |
| 103 | |
| 104 | export function getOptionalStringArg( |
| 105 | args: any, |
no outgoing calls
no test coverage detected