|
1 | | -using System.Text.Json.Nodes; |
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Nodes; |
2 | 3 | using Azure; |
3 | 4 | using Devlooped.Extensions.AI.Grok; |
4 | 5 | using Microsoft.Extensions.AI; |
@@ -319,4 +320,70 @@ public async Task GrokInvokesHostedMcp() |
319 | 320 | response.Messages.SelectMany(x => x.Contents).OfType<HostedToolCallContent>(), |
320 | 321 | x => x.ToolCall.Type == Devlooped.Grok.ToolCallType.McpTool); |
321 | 322 | } |
| 323 | + |
| 324 | + [SecretsFact("XAI_API_KEY", "GITHUB_TOKEN")] |
| 325 | + public async Task GrokStreamsUpdatesFromAllTools() |
| 326 | + { |
| 327 | + var messages = new Chat() |
| 328 | + { |
| 329 | + { "user", |
| 330 | + """ |
| 331 | + What's the oldest stable version released on the devlooped/GrokClient repo on GitHub?, |
| 332 | + what is the current price of Tesla stock, |
| 333 | + and what is the current date? Respond with the following JSON: |
| 334 | + { |
| 335 | + "today": "[get_date result]", |
| 336 | + "release": "[first stable release of devlooped/GrokClient]", |
| 337 | + "price": [$TSLA price] |
| 338 | + } |
| 339 | + """ |
| 340 | + }, |
| 341 | + }; |
| 342 | + |
| 343 | + var grok = new GrokClient(Configuration["XAI_API_KEY"]!) |
| 344 | + .AsIChatClient("grok-4-fast") |
| 345 | + .AsBuilder() |
| 346 | + .UseFunctionInvocation() |
| 347 | + .UseLogging(output.AsLoggerFactory()) |
| 348 | + .Build(); |
| 349 | + |
| 350 | + var getDateCalls = 0; |
| 351 | + var options = new ChatOptions |
| 352 | + { |
| 353 | + Tools = |
| 354 | + [ |
| 355 | + new HostedWebSearchTool(), |
| 356 | + new HostedMcpServerTool("GitHub", "https://api.githubcopilot.com/mcp/") { |
| 357 | + AuthorizationToken = Configuration["GITHUB_TOKEN"]!, |
| 358 | + AllowedTools = ["list_releases", "get_release_by_tag"], |
| 359 | + }, |
| 360 | + AIFunctionFactory.Create(() => { |
| 361 | + getDateCalls++; |
| 362 | + return DateTimeOffset.Now.ToString("O"); |
| 363 | + }, "get_date", "Gets the current date") |
| 364 | + ] |
| 365 | + }; |
| 366 | + |
| 367 | + var updates = await grok.GetStreamingResponseAsync(messages, options).ToListAsync(); |
| 368 | + var response = updates.ToChatResponse(); |
| 369 | + var typed = JsonSerializer.Deserialize<Response>(response.Text, new JsonSerializerOptions(JsonSerializerDefaults.Web)); |
| 370 | + |
| 371 | + Assert.NotNull(typed); |
| 372 | + |
| 373 | + Assert.Contains( |
| 374 | + response.Messages.SelectMany(x => x.Contents).OfType<HostedToolCallContent>(), |
| 375 | + x => x.ToolCall.Type == Devlooped.Grok.ToolCallType.McpTool); |
| 376 | + |
| 377 | + Assert.Contains( |
| 378 | + response.Messages.SelectMany(x => x.Contents).OfType<HostedToolCallContent>(), |
| 379 | + x => x.ToolCall.Type == Devlooped.Grok.ToolCallType.WebSearchTool); |
| 380 | + |
| 381 | + Assert.Equal(1, getDateCalls); |
| 382 | + |
| 383 | + Assert.Equal(DateOnly.FromDateTime(DateTime.Today), typed.Today); |
| 384 | + Assert.EndsWith("1.0.0", typed.Release); |
| 385 | + Assert.True(typed.Price > 100); |
| 386 | + } |
| 387 | + |
| 388 | + record Response(DateOnly Today, string Release, decimal Price); |
322 | 389 | } |
0 commit comments