| | | 1 | | using System.Text.Json; |
| | | 2 | | |
| | | 3 | | using Microsoft.Extensions.AI; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.AgentFramework.Evaluation; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Serialization-friendly projection of a <see cref="ChatResponse"/> used by |
| | | 9 | | /// <see cref="FileEvaluationCaptureStore"/>. Captures textual content, function |
| | | 10 | | /// call payloads, and function result payloads in their original message order. |
| | | 11 | | /// </summary> |
| | | 12 | | internal sealed class CapturedChatResponsePayload |
| | | 13 | | { |
| | 12 | 14 | | public string? ResponseId { get; set; } |
| | 12 | 15 | | public string? ModelId { get; set; } |
| | 20 | 16 | | public List<CapturedMessage> Messages { get; set; } = new(); |
| | | 17 | | |
| | | 18 | | public static CapturedChatResponsePayload FromChatResponse(ChatResponse response) |
| | | 19 | | { |
| | 4 | 20 | | var messages = new List<CapturedMessage>(response.Messages.Count); |
| | 18 | 21 | | foreach (var message in response.Messages) |
| | | 22 | | { |
| | 5 | 23 | | var captured = new CapturedMessage |
| | 5 | 24 | | { |
| | 5 | 25 | | Role = message.Role.Value, |
| | 5 | 26 | | Text = message.Text, |
| | 5 | 27 | | }; |
| | | 28 | | |
| | 5 | 29 | | if (message.Contents.Count > 0) |
| | | 30 | | { |
| | 5 | 31 | | var contents = new List<CapturedContent>(); |
| | 20 | 32 | | foreach (var content in message.Contents) |
| | | 33 | | { |
| | | 34 | | switch (content) |
| | | 35 | | { |
| | | 36 | | case FunctionCallContent fc: |
| | 0 | 37 | | contents.Add(new CapturedContent |
| | 0 | 38 | | { |
| | 0 | 39 | | Kind = CapturedContentKind.FunctionCall, |
| | 0 | 40 | | CallId = fc.CallId, |
| | 0 | 41 | | Name = fc.Name, |
| | 0 | 42 | | Arguments = fc.Arguments is not null |
| | 0 | 43 | | ? JsonSerializer.Serialize(fc.Arguments) |
| | 0 | 44 | | : null, |
| | 0 | 45 | | }); |
| | 0 | 46 | | break; |
| | | 47 | | case FunctionResultContent fr: |
| | 0 | 48 | | contents.Add(new CapturedContent |
| | 0 | 49 | | { |
| | 0 | 50 | | Kind = CapturedContentKind.FunctionResult, |
| | 0 | 51 | | CallId = fr.CallId, |
| | 0 | 52 | | Result = fr.Result is not null |
| | 0 | 53 | | ? JsonSerializer.Serialize(fr.Result) |
| | 0 | 54 | | : null, |
| | 0 | 55 | | }); |
| | 0 | 56 | | break; |
| | | 57 | | case TextContent tc: |
| | 5 | 58 | | contents.Add(new CapturedContent |
| | 5 | 59 | | { |
| | 5 | 60 | | Kind = CapturedContentKind.Text, |
| | 5 | 61 | | Text = tc.Text, |
| | 5 | 62 | | }); |
| | | 63 | | break; |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | 5 | 67 | | if (contents.Count > 0) |
| | | 68 | | { |
| | 5 | 69 | | captured.Contents = contents; |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 5 | 73 | | messages.Add(captured); |
| | | 74 | | } |
| | | 75 | | |
| | 4 | 76 | | return new CapturedChatResponsePayload |
| | 4 | 77 | | { |
| | 4 | 78 | | ResponseId = response.ResponseId, |
| | 4 | 79 | | ModelId = response.ModelId, |
| | 4 | 80 | | Messages = messages, |
| | 4 | 81 | | }; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public ChatResponse ToChatResponse() |
| | | 85 | | { |
| | 2 | 86 | | var messages = new List<ChatMessage>(Messages.Count); |
| | 10 | 87 | | foreach (var captured in Messages) |
| | | 88 | | { |
| | 3 | 89 | | var role = string.IsNullOrEmpty(captured.Role) |
| | 3 | 90 | | ? ChatRole.Assistant |
| | 3 | 91 | | : new ChatRole(captured.Role); |
| | | 92 | | |
| | 3 | 93 | | if (captured.Contents is { Count: > 0 }) |
| | | 94 | | { |
| | 3 | 95 | | var contentItems = new List<AIContent>(captured.Contents.Count); |
| | 12 | 96 | | foreach (var c in captured.Contents) |
| | | 97 | | { |
| | 3 | 98 | | switch (c.Kind) |
| | | 99 | | { |
| | | 100 | | case CapturedContentKind.Text: |
| | 3 | 101 | | contentItems.Add(new TextContent(c.Text ?? string.Empty)); |
| | 3 | 102 | | break; |
| | | 103 | | case CapturedContentKind.FunctionCall: |
| | 0 | 104 | | IDictionary<string, object?>? args = null; |
| | 0 | 105 | | if (c.Arguments is not null) |
| | | 106 | | { |
| | 0 | 107 | | args = JsonSerializer |
| | 0 | 108 | | .Deserialize<Dictionary<string, object?>>(c.Arguments); |
| | | 109 | | } |
| | 0 | 110 | | contentItems.Add(new FunctionCallContent( |
| | 0 | 111 | | c.CallId ?? string.Empty, |
| | 0 | 112 | | c.Name ?? string.Empty, |
| | 0 | 113 | | args)); |
| | 0 | 114 | | break; |
| | | 115 | | case CapturedContentKind.FunctionResult: |
| | 0 | 116 | | object? result = null; |
| | 0 | 117 | | if (c.Result is not null) |
| | | 118 | | { |
| | 0 | 119 | | result = JsonSerializer.Deserialize<JsonElement>(c.Result); |
| | | 120 | | } |
| | 0 | 121 | | contentItems.Add(new FunctionResultContent( |
| | 0 | 122 | | c.CallId ?? string.Empty, |
| | 0 | 123 | | result)); |
| | | 124 | | break; |
| | | 125 | | } |
| | | 126 | | } |
| | 3 | 127 | | messages.Add(new ChatMessage(role, contentItems)); |
| | | 128 | | } |
| | | 129 | | else |
| | | 130 | | { |
| | 0 | 131 | | messages.Add(new ChatMessage(role, captured.Text ?? string.Empty)); |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | 2 | 135 | | return new ChatResponse(messages) |
| | 2 | 136 | | { |
| | 2 | 137 | | ResponseId = ResponseId, |
| | 2 | 138 | | ModelId = ModelId, |
| | 2 | 139 | | }; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | internal sealed class CapturedMessage |
| | | 143 | | { |
| | 19 | 144 | | public string? Role { get; set; } |
| | 13 | 145 | | public string? Text { get; set; } |
| | 22 | 146 | | public List<CapturedContent>? Contents { get; set; } |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | internal sealed class CapturedContent |
| | | 150 | | { |
| | 16 | 151 | | public CapturedContentKind Kind { get; set; } |
| | 16 | 152 | | public string? Text { get; set; } |
| | 8 | 153 | | public string? CallId { get; set; } |
| | 8 | 154 | | public string? Name { get; set; } |
| | 8 | 155 | | public string? Arguments { get; set; } |
| | 8 | 156 | | public string? Result { get; set; } |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | internal enum CapturedContentKind |
| | | 160 | | { |
| | | 161 | | Text, |
| | | 162 | | FunctionCall, |
| | | 163 | | FunctionResult, |
| | | 164 | | } |
| | | 165 | | } |