* 验证工具参数
(
parameters: Record<string, any>,
schema: Record<string, ToolParameterSchema>,
required: string[] = []
)
| 8 | * 验证工具参数 |
| 9 | */ |
| 10 | public static validateParameters( |
| 11 | parameters: Record<string, any>, |
| 12 | schema: Record<string, ToolParameterSchema>, |
| 13 | required: string[] = [] |
| 14 | ): void { |
| 15 | // 检查必需参数 |
| 16 | for (const requiredParam of required) { |
| 17 | if (!(requiredParam in parameters) || parameters[requiredParam] === undefined) { |
| 18 | throw new ToolValidationError(`缺少必需参数: ${requiredParam}`, requiredParam); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // 验证每个参数 |
| 23 | for (const [key, value] of Object.entries(parameters)) { |
| 24 | const paramSchema = schema[key]; |
| 25 | if (!paramSchema) { |
| 26 | throw new ToolValidationError(`未知参数: ${key}`, key, value); |
| 27 | } |
| 28 | |
| 29 | this.validateValue(value, paramSchema, key); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * 验证单个值 |