From 94434c7092a8879b4d9f705a368d899ba8022f85 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 1 Jul 2024 11:54:28 -0500 Subject: [PATCH] fix: correct validation for create methods and update xml comments to match --- src/AnthropicClient/Models/Tool.cs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/AnthropicClient/Models/Tool.cs b/src/AnthropicClient/Models/Tool.cs index ed61992..9d667ce 100644 --- a/src/AnthropicClient/Models/Tool.cs +++ b/src/AnthropicClient/Models/Tool.cs @@ -65,12 +65,16 @@ public class Tool /// The description of the tool. /// The type that contains the method. /// The name of the method. - /// Thrown when , , , or is null. + /// Thrown when is null or empty. + /// Thrown when is null. /// Thrown when the method is not found in the type. /// The created tool as instance of . /// The name of the tool will be sanitized to conform to the Anthropic tool naming rules. public static Tool CreateFromStaticMethod(string name, string description, Type type, string methodName) { + ArgumentValidator.ThrowIfNullOrWhitespace(methodName, nameof(methodName)); + ArgumentValidator.ThrowIfNull(type, nameof(type)); + var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static); if (method is null) @@ -88,12 +92,16 @@ public class Tool /// The description of the tool. /// The instance that contains the method. /// The name of the method. - /// Thrown when , , , or is null. - /// Thrown when the method is not found in the type. + /// Thrown when is null or empty. + /// Thrown when is null. + /// Thrown when is not found in the type of . /// The created tool as instance of . /// The name of the tool will be sanitized to conform to the Anthropic tool naming rules. public static Tool CreateFromInstanceMethod(string name, string description, object instance, string methodName) { + ArgumentValidator.ThrowIfNullOrWhitespace(methodName, nameof(methodName)); + ArgumentValidator.ThrowIfNull(instance, nameof(instance)); + var method = instance.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance); if (method is null) @@ -111,11 +119,13 @@ public class Tool /// The name of the tool. /// The description of the tool. /// The function. - /// Thrown when , , or is null. + /// Thrown when is null. /// The created tool as instance of . /// The name of the tool will be sanitized to conform to the Anthropic tool naming rules. public static Tool CreateFromFunction(string name, string description, Func func) { + ArgumentValidator.ThrowIfNull(func, nameof(func)); + return new Tool(name, description, new AnthropicFunction(func.Method, func.Target)); } @@ -127,11 +137,13 @@ public class Tool /// The name of the tool. /// The description of the tool. /// The function. - /// Thrown when , , or is null. + /// Thrown when is null. /// The created tool as instance of . /// The name of the tool will be sanitized to conform to the Anthropic tool naming rules. public static Tool CreateFromFunction(string name, string description, Func func) { + ArgumentValidator.ThrowIfNull(func, nameof(func)); + return new Tool(name, description, new AnthropicFunction(func.Method, func.Target)); }