|
| 1 | +using System.ClientModel; |
| 2 | +using System.Text.Json; |
| 3 | +using ManagedCode.LlmTck.Configuration; |
| 4 | +using ManagedCode.LlmTck.Models; |
| 5 | +using Microsoft.Agents.AI; |
| 6 | +using Microsoft.Extensions.AI; |
| 7 | +using OpenAI; |
| 8 | + |
| 9 | +namespace ManagedCode.FileContext.Tests.LlmTck; |
| 10 | + |
| 11 | +public sealed class FileToolResultProtocolTests(Xunit.Abstractions.ITestOutputHelper output) |
| 12 | +{ |
| 13 | + [Theory] |
| 14 | + [InlineData(FileAccessProvider.ReadFileToolName, "{\"fileName\":\"empty.txt\"}", "\"\"")] |
| 15 | + [InlineData(FileAccessProvider.ReadFileToolName, "{\"fileName\":\"missing.txt\"}", "not found")] |
| 16 | + [InlineData(FileAccessProvider.LsToolName, "{\"directory\":\"empty\"}", "[]")] |
| 17 | + [InlineData(FileAccessProvider.GrepToolName, "{\"regexPattern\":\"absent\",\"directory\":\"\"}", "[]")] |
| 18 | + [InlineData(FileContextToolNames.GetInfo, "{\"path\":\"missing.txt\"}", "Error: Function failed.")] |
| 19 | + [InlineData(FileContextToolNames.ReadRange, "{\"path\":\"empty.txt\"}", "\"content\": \"\"")] |
| 20 | + [InlineData(FileContextToolNames.ReadRange, "{\"path\":\"first.txt\",\"startLine\":100}", "\"content\": \"\"")] |
| 21 | + [InlineData(FileContextToolNames.ReadRange, "{\"path\":\"missing.txt\"}", "Error: Function failed.")] |
| 22 | + [InlineData(FileContextToolNames.ReadRange, "{\"path\":\"../escape.txt\"}", "Error: Function failed.")] |
| 23 | + [InlineData(FileContextToolNames.SearchMarkdownGraph, "{\"query\":\"absent\"}", "Error: Function failed.")] |
| 24 | + public async Task EmptyOrFailedTool_StillSendsMatchingResult(string toolName, string arguments, string expectedContent) |
| 25 | + { |
| 26 | + var requests = await RunToolLoopAsync(LlmTckToolReplay.CreateResponse("call-empty", toolName, arguments)); |
| 27 | + |
| 28 | + foreach (var request in requests) |
| 29 | + { |
| 30 | + var results = LlmTckToolAssertions.AssertClosedCalls(request, "call-empty"); |
| 31 | + output.WriteLine(results[0].GetRawText()); |
| 32 | + results[0].GetProperty("content").ValueKind.ShouldBe(JsonValueKind.String); |
| 33 | + results[0].GetProperty("content").GetString()!.ShouldContain(expectedContent); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + [Theory] |
| 38 | + [InlineData(false)] |
| 39 | + [InlineData(true)] |
| 40 | + public async Task MultipleFileCalls_InOneTurn_ReturnEveryResult(bool concurrent) |
| 41 | + { |
| 42 | + var first = LlmTckToolReplay.CreateResponse("call-first", FileAccessProvider.ReadFileToolName, "{\"fileName\":\"first.txt\"}"); |
| 43 | + var second = LlmTckToolReplay.CreateResponse("call-second", FileContextToolNames.ReadRange, "{\"path\":\"second.txt\"}"); |
| 44 | + var missing = LlmTckToolReplay.CreateResponse("call-missing", FileContextToolNames.ReadRange, "{\"path\":\"missing.txt\"}"); |
| 45 | + |
| 46 | + var requests = await RunToolLoopAsync($"[{first},{second},{missing}]", concurrent); |
| 47 | + |
| 48 | + foreach (var request in requests) |
| 49 | + { |
| 50 | + var results = LlmTckToolAssertions.AssertClosedCalls(request, "call-first", "call-second", "call-missing"); |
| 51 | + results.Single(result => string.Equals(result.GetProperty("tool_call_id").GetString(), "call-first", StringComparison.Ordinal)) |
| 52 | + .GetProperty("content").GetString()!.ShouldContain("first-content"); |
| 53 | + results.Single(result => string.Equals(result.GetProperty("tool_call_id").GetString(), "call-second", StringComparison.Ordinal)) |
| 54 | + .GetProperty("content").GetString()!.ShouldContain("second-content"); |
| 55 | + results.Single(result => string.Equals(result.GetProperty("tool_call_id").GetString(), "call-missing", StringComparison.Ordinal)) |
| 56 | + .GetProperty("content").GetString().ShouldNotBeNullOrWhiteSpace(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public async Task LiteralNullAssistantText_IsNotInterpretedAsToolReplay() |
| 62 | + { |
| 63 | + var requests = await RunToolLoopAsync( |
| 64 | + LlmTckToolReplay.CreateResponse("read", FileAccessProvider.ReadFileToolName, "{\"fileName\":\"first.txt\"}"), |
| 65 | + finalResponse: "null"); |
| 66 | + |
| 67 | + foreach (var request in requests) |
| 68 | + { |
| 69 | + LlmTckToolAssertions.AssertClosedCalls(request, "read"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static async Task<string[]> RunToolLoopAsync(string replay, bool concurrent = false, string finalResponse = "Inspection completed.") |
| 74 | + { |
| 75 | + const string prompt = "Inspect the files."; |
| 76 | + LlmTckToolReplay.Reset(); |
| 77 | + var host = new LlmTckTestHost(() => new LlmTckConfigurationBuilder() |
| 78 | + .AddModel(FileContextAgentLlmTckTests.Model, LlmTckModelKind.Chat) |
| 79 | + .AddChatScenario("protocol", scenario => scenario.ForModel(FileContextAgentLlmTckTests.Model) |
| 80 | + .WhenUserContains(prompt).Responds(replay).Responds(finalResponse).Responds("Follow-up completed.")) |
| 81 | + .Build()); |
| 82 | + await using var hostLifetime = host.ConfigureAwait(false); |
| 83 | + await host.StartAsync().ConfigureAwait(false); |
| 84 | + var storage = await TestStorageScope.CreateAsync().ConfigureAwait(false); |
| 85 | + await using var storageLifetime = storage.ConfigureAwait(false); |
| 86 | + var store = new ManagedCodeStorageFileStore(storage.Storage); |
| 87 | + await store.WriteAsync("empty.txt", string.Empty).ConfigureAwait(false); |
| 88 | + await store.WriteAsync("first.txt", "first-content").ConfigureAwait(false); |
| 89 | + await store.WriteAsync("second.txt", "second-content").ConfigureAwait(false); |
| 90 | + using var provider = new FileContextProvider(store, new FileContextService(store), |
| 91 | + new FileContextOptions { RequireReadToolApproval = false }); |
| 92 | + var sdk = new OpenAIClient(new ApiKeyCredential("not-required-by-llm-tck"), |
| 93 | + new OpenAIClientOptions { Endpoint = LlmTckToolReplay.CreateRouteUri(host.Endpoint) }); |
| 94 | + using var client = sdk.GetChatClient(FileContextAgentLlmTckTests.Model).AsIChatClient().AsBuilder() |
| 95 | + .UseAIContextProviders(provider) |
| 96 | + .UseFunctionInvocation(configure: options => options.AllowConcurrentInvocation = concurrent).Build(); |
| 97 | + var agent = new ChatClientAgent(client, new ChatClientAgentOptions { UseProvidedChatClientAsIs = true }); |
| 98 | + |
| 99 | + var session = await agent.CreateSessionAsync().ConfigureAwait(false); |
| 100 | + var response = await agent.RunAsync(prompt, session).ConfigureAwait(false); |
| 101 | + response.Text.ShouldBe(finalResponse); |
| 102 | + var saved = await agent.SerializeSessionAsync(session).ConfigureAwait(false); |
| 103 | + var restored = await agent.DeserializeSessionAsync(saved).ConfigureAwait(false); |
| 104 | + var followUp = await agent.RunAsync("Continue the inspection.", restored).ConfigureAwait(false); |
| 105 | + followUp.Text.ShouldBe("Follow-up completed."); |
| 106 | + LlmTckToolReplay.RecordedRequests.Count.ShouldBe(3); |
| 107 | + var assertions = await host.GetAssertionsAsync().ConfigureAwait(false); |
| 108 | + assertions.Matched.ShouldBe(3); |
| 109 | + assertions.Unmatched.ShouldBe(0); |
| 110 | + return LlmTckToolReplay.RecordedRequests.Skip(1).ToArray(); |
| 111 | + } |
| 112 | +} |
0 commit comments