@@ -92,18 +92,42 @@ private static AIFunctionArguments ConvertToFunctionArguments(JsonElement? argum
9292 return [ ] ;
9393 }
9494
95- if ( arguments . Value . ValueKind != JsonValueKind . Object )
95+ JsonDocument ? parsedStringArguments = null ;
96+ var argumentElement = arguments . Value ;
97+
98+ if ( argumentElement . ValueKind == JsonValueKind . String )
9699 {
97- throw new InvalidOperationException (
98- $ "Inline skill scripts expect arguments as a JSON object but received a JSON element of kind '{ arguments . Value . ValueKind } '.") ;
100+ string ? rawArguments = argumentElement . GetString ( ) ;
101+ try
102+ {
103+ parsedStringArguments = JsonDocument . Parse ( rawArguments ?? string . Empty ) ;
104+ argumentElement = parsedStringArguments . RootElement ;
105+ }
106+ catch ( JsonException ex )
107+ {
108+ throw new InvalidOperationException ( "Inline skill scripts received arguments as a JSON string, but the string did not contain valid JSON." , ex ) ;
109+ }
99110 }
100111
101- var dict = new Dictionary < string , object ? > ( ) ;
102- foreach ( var property in arguments . Value . EnumerateObject ( ) )
112+ if ( argumentElement . ValueKind != JsonValueKind . Object )
103113 {
104- dict [ property . Name ] = property . Value ;
114+ throw new InvalidOperationException (
115+ $ "Inline skill scripts expect arguments as a JSON object but received a JSON element of kind '{ argumentElement . ValueKind } '.") ;
105116 }
106117
107- return new AIFunctionArguments ( dict ) ;
118+ try
119+ {
120+ var dict = new Dictionary < string , object ? > ( ) ;
121+ foreach ( var property in argumentElement . EnumerateObject ( ) )
122+ {
123+ dict [ property . Name ] = parsedStringArguments is null ? property . Value : property . Value . Clone ( ) ;
124+ }
125+
126+ return new AIFunctionArguments ( dict ) ;
127+ }
128+ finally
129+ {
130+ parsedStringArguments ? . Dispose ( ) ;
131+ }
108132 }
109133}
0 commit comments