@@ -29,6 +29,45 @@ private class TestTools
2929 public static string ConcatWithBool ( string text , bool flag ) => $ "{ text } :{ flag } ";
3030 }
3131
32+ private class TestToolsAsync
33+ {
34+ public static async Task < string > EchoAsync ( string message )
35+ {
36+ await Task . Delay ( 1 ) ; // Simulate async work
37+ return message ;
38+ }
39+
40+ public static async Task < int > AddAsync ( int a , int b )
41+ {
42+ await Task . Delay ( 1 ) ; // Simulate async work
43+ return a + b ;
44+ }
45+
46+ public static async Task < double > MultiplyAsync ( double x , double y )
47+ {
48+ await Task . Delay ( 1 ) ; // Simulate async work
49+ return x * y ;
50+ }
51+
52+ public static async Task < bool > IsGreaterThanAsync ( long value1 , long value2 )
53+ {
54+ await Task . Delay ( 1 ) ; // Simulate async work
55+ return value1 > value2 ;
56+ }
57+
58+ public static async Task < float > DivideAsync ( float numerator , float denominator )
59+ {
60+ await Task . Delay ( 1 ) ; // Simulate async work
61+ return numerator / denominator ;
62+ }
63+
64+ public static async Task < string > ConcatWithBoolAsync ( string text , bool flag )
65+ {
66+ await Task . Delay ( 1 ) ; // Simulate async work
67+ return $ "{ text } :{ flag } ";
68+ }
69+ }
70+
3271 private Mock < EmbeddingClient > mockEmbeddingClient ;
3372
3473 [ SetUp ]
@@ -52,6 +91,21 @@ public void CanAddLocalTools()
5291 Assert . That ( tools . Tools . Any ( t => t . FunctionName == "ConcatWithBool" ) ) ;
5392 }
5493
94+ [ Test ]
95+ public void CanAddAsyncLocalTools ( )
96+ {
97+ var tools = new ChatTools ( ) ;
98+ tools . AddFunctionTools ( typeof ( TestToolsAsync ) ) ;
99+
100+ Assert . That ( tools . Tools , Has . Count . EqualTo ( 6 ) ) ;
101+ Assert . That ( tools . Tools . Any ( t => t . FunctionName == "EchoAsync" ) ) ;
102+ Assert . That ( tools . Tools . Any ( t => t . FunctionName == "AddAsync" ) ) ;
103+ Assert . That ( tools . Tools . Any ( t => t . FunctionName == "MultiplyAsync" ) ) ;
104+ Assert . That ( tools . Tools . Any ( t => t . FunctionName == "IsGreaterThanAsync" ) ) ;
105+ Assert . That ( tools . Tools . Any ( t => t . FunctionName == "DivideAsync" ) ) ;
106+ Assert . That ( tools . Tools . Any ( t => t . FunctionName == "ConcatWithBoolAsync" ) ) ;
107+ }
108+
55109 [ Test ]
56110 public async Task CanCallToolsAsync ( )
57111 {
@@ -86,6 +140,40 @@ public async Task CanCallToolsAsync()
86140 Assert . That ( resultsList [ 5 ] . Content [ 0 ] . Text , Is . EqualTo ( "Test:True" ) ) ;
87141 }
88142
143+ [ Test ]
144+ public async Task CanCallAsyncToolsAsync ( )
145+ {
146+ var tools = new ChatTools ( ) ;
147+ tools . AddFunctionTools ( typeof ( TestToolsAsync ) ) ;
148+
149+ var toolCalls = new [ ]
150+ {
151+ ChatToolCall . CreateFunctionToolCall ( "call1" , "EchoAsync" , BinaryData . FromString ( @"{""message"": ""Hello""}" ) ) ,
152+ ChatToolCall . CreateFunctionToolCall ( "call2" , "AddAsync" , BinaryData . FromString ( @"{""a"": 2, ""b"": 3}" ) ) ,
153+ ChatToolCall . CreateFunctionToolCall ( "call3" , "MultiplyAsync" , BinaryData . FromString ( @"{""x"": 2.5, ""y"": 3.0}" ) ) ,
154+ ChatToolCall . CreateFunctionToolCall ( "call4" , "IsGreaterThanAsync" , BinaryData . FromString ( @"{""value1"": 100, ""value2"": 50}" ) ) ,
155+ ChatToolCall . CreateFunctionToolCall ( "call5" , "DivideAsync" , BinaryData . FromString ( @"{""numerator"": 10.0, ""denominator"": 2.0}" ) ) ,
156+ ChatToolCall . CreateFunctionToolCall ( "call6" , "ConcatWithBoolAsync" , BinaryData . FromString ( @"{""text"": ""Test"", ""flag"": true}" ) )
157+ } ;
158+
159+ var results = await tools . CallAsync ( toolCalls ) ;
160+ var resultsList = results . ToList ( ) ;
161+
162+ Assert . That ( resultsList , Has . Count . EqualTo ( 6 ) ) ;
163+ Assert . That ( resultsList [ 0 ] . ToolCallId , Is . EqualTo ( "call1" ) ) ;
164+ Assert . That ( resultsList [ 0 ] . Content [ 0 ] . Text , Is . EqualTo ( "Hello" ) ) ;
165+ Assert . That ( resultsList [ 1 ] . ToolCallId , Is . EqualTo ( "call2" ) ) ;
166+ Assert . That ( resultsList [ 1 ] . Content [ 0 ] . Text , Is . EqualTo ( "5" ) ) ;
167+ Assert . That ( resultsList [ 2 ] . ToolCallId , Is . EqualTo ( "call3" ) ) ;
168+ Assert . That ( resultsList [ 2 ] . Content [ 0 ] . Text , Is . EqualTo ( "7.5" ) ) ;
169+ Assert . That ( resultsList [ 3 ] . ToolCallId , Is . EqualTo ( "call4" ) ) ;
170+ Assert . That ( resultsList [ 3 ] . Content [ 0 ] . Text , Is . EqualTo ( "True" ) ) ;
171+ Assert . That ( resultsList [ 4 ] . ToolCallId , Is . EqualTo ( "call5" ) ) ;
172+ Assert . That ( resultsList [ 4 ] . Content [ 0 ] . Text , Is . EqualTo ( "5" ) ) ;
173+ Assert . That ( resultsList [ 5 ] . ToolCallId , Is . EqualTo ( "call6" ) ) ;
174+ Assert . That ( resultsList [ 5 ] . Content [ 0 ] . Text , Is . EqualTo ( "Test:True" ) ) ;
175+ }
176+
89177 [ Test ]
90178 public void CreatesCompletionOptionsWithTools ( )
91179 {
0 commit comments