Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,14 @@ static bool IsAsyncMethod(MethodInfo method)
}
}

// There was no argument for the parameter in the dictionary.
// Does it have a default value?
if (parameter.HasDefaultValue)
// If the parameter is required and there's no argument specified for it, throw.
if (!parameter.HasDefaultValue)
{
return parameter.DefaultValue;
Throw.ArgumentException(nameof(arguments), $"Missing required parameter '{parameter.Name}' for method '{parameter.Member.Name}'.");
}

// Leave it empty.
return null;
// Otherwise, use the optional parameter's default value.
return parameter.DefaultValue;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public async Task SupportsMultipleFunctionCallsPerRequestAsync(bool concurrentIn
{
Tools =
[
AIFunctionFactory.Create((int i) => "Result 1", "Func1"),
AIFunctionFactory.Create((int? i = 42) => "Result 1", "Func1"),
AIFunctionFactory.Create((int i) => $"Result 2: {i}", "Func2"),
]
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ public async Task Parameters_DefaultValuesAreUsedButOverridable_Async()
AssertExtensions.EqualFunctionCallResults("hello hello", await func.InvokeAsync([new KeyValuePair<string, object?>("a", "hello")]));
}

[Fact]
public async Task Parameters_MissingRequiredParametersFail_Async()
{
AIFunction[] funcs =
[
AIFunctionFactory.Create((string theParam) => theParam + " " + theParam),
AIFunctionFactory.Create((string? theParam) => theParam + " " + theParam),
AIFunctionFactory.Create((int theParam) => theParam * 2),
AIFunctionFactory.Create((int? theParam) => theParam * 2),
];

foreach (AIFunction f in funcs)
{
Exception e = await Assert.ThrowsAsync<ArgumentException>(() => f.InvokeAsync());
Assert.Contains("'theParam'", e.Message);
}
}

[Fact]
public async Task Parameters_MappedByType_Async()
{
Expand Down
Loading