Repository navigation
Expand file tree
/
Copy pathToolResultsTests.cs
More file actions
121 lines (103 loc) · 4.84 KB
/
Copy pathToolResultsTests.cs
File metadata and controls
121 lines (103 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.SDK.Test.Harness;
using Microsoft.Extensions.AI;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.SDK.Test;
public partial class ToolResultsTests(E2ETestFixture fixture, ITestOutputHelper output) : E2ETestBase(fixture, "tool_results", output)
{
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)]
[JsonSerializable(typeof(ToolResultAIContent))]
[JsonSerializable(typeof(ToolResultObject))]
[JsonSerializable(typeof(JsonElement))]
private partial class ToolResultsJsonContext : JsonSerializerContext;
[Fact]
public async Task Should_Handle_Structured_ToolResultObject_From_Custom_Tool()
{
var session = await CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(GetWeather, "get_weather", serializerOptions: ToolResultsJsonContext.Default.Options)],
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions
{
Prompt = "What's the weather in Paris?"
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.NotNull(assistantMessage);
Assert.Matches("(?i)sunny|72", assistantMessage!.Data.Content ?? string.Empty);
[Description("Gets weather for a city")]
static ToolResultAIContent GetWeather([Description("City name")] string city)
=> new(new()
{
TextResultForLlm = $"The weather in {city} is sunny and 72°F",
ResultType = "success",
});
}
[Fact]
public async Task Should_Handle_Tool_Result_With_Failure_ResultType()
{
var session = await CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(CheckStatus, "check_status", serializerOptions: ToolResultsJsonContext.Default.Options)],
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions
{
Prompt = "Check the status of the service using check_status. If it fails, say 'service is down'."
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.NotNull(assistantMessage);
Assert.Contains("service is down", assistantMessage!.Data.Content?.ToLowerInvariant() ?? string.Empty);
[Description("Checks the status of a service")]
static ToolResultAIContent CheckStatus()
=> new(new()
{
TextResultForLlm = "Service unavailable",
ResultType = "failure",
Error = "API timeout",
});
}
[Fact]
public async Task Should_Preserve_ToolTelemetry_And_Not_Stringify_Structured_Results_For_LLM()
{
var session = await CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(AnalyzeCode, "analyze_code", serializerOptions: ToolResultsJsonContext.Default.Options)],
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions
{
Prompt = "Analyze the file main.ts for issues."
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.NotNull(assistantMessage);
Assert.Contains("no issues", assistantMessage!.Data.Content?.ToLowerInvariant() ?? string.Empty);
// Verify the LLM received just textResultForLlm, not stringified JSON
var traffic = await Ctx.GetExchangesAsync();
var lastConversation = traffic[^1];
var toolResults = lastConversation.Request.Messages
.Where(m => m.Role == "tool")
.ToList();
Assert.Single(toolResults);
Assert.DoesNotContain("toolTelemetry", toolResults[0].StringContent);
Assert.DoesNotContain("resultType", toolResults[0].StringContent);
[Description("Analyzes code for issues")]
static ToolResultAIContent AnalyzeCode([Description("File to analyze")] string file)
=> new(new()
{
TextResultForLlm = $"Analysis of {file}: no issues found",
ResultType = "success",
ToolTelemetry = new Dictionary<string, object>
{
["metrics"] = new Dictionary<string, object> { ["analysisTimeMs"] = 150 },
["properties"] = new Dictionary<string, object> { ["analyzer"] = "eslint" },
},
});
}
}