Skip to content
Open
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 @@ -92,18 +92,50 @@ private static AIFunctionArguments ConvertToFunctionArguments(JsonElement? argum
return [];
}

if (arguments.Value.ValueKind != JsonValueKind.Object)
var argumentElement = arguments.Value;
using var parsedStringArguments = ParseStringArguments(argumentElement);
if (parsedStringArguments is not null)
{
argumentElement = parsedStringArguments.RootElement;
}

if (argumentElement.ValueKind != JsonValueKind.Object)
{
throw new InvalidOperationException(
$"Inline skill scripts expect arguments as a JSON object but received a JSON element of kind '{arguments.Value.ValueKind}'.");
$"Inline skill scripts expect arguments as a JSON object but received a JSON element of kind '{argumentElement.ValueKind}'.");
}

var dict = new Dictionary<string, object?>();
foreach (var property in arguments.Value.EnumerateObject())
foreach (var property in argumentElement.EnumerateObject())
{
dict[property.Name] = property.Value;
dict[property.Name] = parsedStringArguments is null ? property.Value : property.Value.Clone();
}
Comment on lines +95 to 112

return new AIFunctionArguments(dict);
}

private static JsonDocument? ParseStringArguments(JsonElement arguments)
{
if (arguments.ValueKind != JsonValueKind.String)
{
return null;
}

var json = arguments.GetString();
if (string.IsNullOrWhiteSpace(json))
{
return null;
}

try
{
return JsonDocument.Parse(json);
}
catch (JsonException ex)
{
throw new InvalidOperationException(
"Inline skill scripts received arguments as a JSON string, but the string did not contain valid JSON.",
ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ public async Task RunAsync_WithParameters_PassesArgumentsAsync()
Assert.Equal(10, int.Parse(result?.ToString()!));
}

[Fact]
public async Task RunAsync_WithStringEncodedObjectArguments_PassesArgumentsAsync()
{
// Arrange
var script = new AgentInlineSkillScript("add", (int a, int b) => a + b);
var skill = new AgentInlineSkill("calc-skill", "Calc.", "Instructions.");
using var argsDoc = JsonDocument.Parse("\"{\\\"a\\\":3,\\\"b\\\":7}\"");
var args = argsDoc.RootElement;

// Act
var result = await script.RunAsync(skill, args, null, CancellationToken.None);

// Assert
Assert.Equal(10, int.Parse(result?.ToString()!));
}
Comment on lines +46 to +60

[Fact]
public void ParametersSchema_NoParameters_ReturnsSchema()
{
Expand Down