|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.Text; |
| 5 | +using System.Text.Json; |
| 6 | +using Amazon; |
| 7 | +using Amazon.BedrockAgentCore; |
| 8 | +using Amazon.BedrockAgentCore.Model; |
| 9 | + |
| 10 | +namespace AWS.AgentCore.IntegrationTests.Infrastructure; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Invokes AgentCore Runtime agents and parses responses. |
| 14 | +/// Supports both standard (JSON) and streaming (SSE) invocations. |
| 15 | +/// </summary> |
| 16 | +public sealed class AgentCoreInvoker : IDisposable |
| 17 | +{ |
| 18 | + private readonly AmazonBedrockAgentCoreClient _client; |
| 19 | + |
| 20 | + public AgentCoreInvoker(string region) |
| 21 | + { |
| 22 | + _client = new AmazonBedrockAgentCoreClient(RegionEndpoint.GetBySystemName(region)); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Invokes a non-streaming agent and returns the parsed message from the JSON response. |
| 27 | + /// </summary> |
| 28 | + public async Task<InvocationResult> InvokeAsync(string runtimeArn, string prompt, CancellationToken ct = default) |
| 29 | + { |
| 30 | + var payload = JsonSerializer.Serialize(new { prompt }); |
| 31 | + |
| 32 | + var request = new InvokeAgentRuntimeRequest |
| 33 | + { |
| 34 | + AgentRuntimeArn = runtimeArn, |
| 35 | + Payload = new MemoryStream(Encoding.UTF8.GetBytes(payload)), |
| 36 | + ContentType = "application/json", |
| 37 | + Accept = "application/json", |
| 38 | + }; |
| 39 | + |
| 40 | + var response = await _client.InvokeAgentRuntimeAsync(request, ct); |
| 41 | + |
| 42 | + using var reader = new StreamReader(response.Response); |
| 43 | + var responseBody = await reader.ReadToEndAsync(ct); |
| 44 | + |
| 45 | + string? message = null; |
| 46 | + try |
| 47 | + { |
| 48 | + using var doc = JsonDocument.Parse(responseBody); |
| 49 | + if (doc.RootElement.TryGetProperty("message", out var messageProp)) |
| 50 | + message = messageProp.GetString(); |
| 51 | + } |
| 52 | + catch (JsonException) |
| 53 | + { |
| 54 | + // Not JSON — use raw body |
| 55 | + } |
| 56 | + |
| 57 | + return new InvocationResult |
| 58 | + { |
| 59 | + RawBody = responseBody, |
| 60 | + Message = message ?? responseBody, |
| 61 | + HttpStatusCode = (int)response.HttpStatusCode, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Invokes a streaming agent and collects all SSE chunks into a result. |
| 67 | + /// </summary> |
| 68 | + public async Task<StreamingInvocationResult> InvokeStreamingAsync( |
| 69 | + string runtimeArn, string prompt, CancellationToken ct = default) |
| 70 | + { |
| 71 | + var payload = JsonSerializer.Serialize(new { prompt }); |
| 72 | + |
| 73 | + var request = new InvokeAgentRuntimeRequest |
| 74 | + { |
| 75 | + AgentRuntimeArn = runtimeArn, |
| 76 | + Payload = new MemoryStream(Encoding.UTF8.GetBytes(payload)), |
| 77 | + ContentType = "application/json", |
| 78 | + Accept = "text/event-stream", |
| 79 | + }; |
| 80 | + |
| 81 | + var response = await _client.InvokeAgentRuntimeAsync(request, ct); |
| 82 | + |
| 83 | + var chunks = new List<string>(); |
| 84 | + string? finalMessage = null; |
| 85 | + |
| 86 | + using var reader = new StreamReader(response.Response); |
| 87 | + while (true) |
| 88 | + { |
| 89 | + ct.ThrowIfCancellationRequested(); |
| 90 | + |
| 91 | + var line = await reader.ReadLineAsync(ct); |
| 92 | + if (line is null) break; |
| 93 | + if (!line.StartsWith("data: ")) continue; |
| 94 | + |
| 95 | + var json = line["data: ".Length..]; |
| 96 | + try |
| 97 | + { |
| 98 | + using var doc = JsonDocument.Parse(json); |
| 99 | + |
| 100 | + if (doc.RootElement.TryGetProperty("done", out var doneProp) && doneProp.GetBoolean()) |
| 101 | + { |
| 102 | + if (doc.RootElement.TryGetProperty("message", out var msgProp)) |
| 103 | + finalMessage = msgProp.GetString(); |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + if (doc.RootElement.TryGetProperty("chunk", out var chunkProp)) |
| 108 | + { |
| 109 | + var chunk = chunkProp.GetString(); |
| 110 | + if (!string.IsNullOrEmpty(chunk)) |
| 111 | + chunks.Add(chunk); |
| 112 | + } |
| 113 | + } |
| 114 | + catch (JsonException) |
| 115 | + { |
| 116 | + // Skip malformed SSE events |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return new StreamingInvocationResult |
| 121 | + { |
| 122 | + Chunks = chunks, |
| 123 | + FinalMessage = finalMessage ?? string.Concat(chunks), |
| 124 | + HttpStatusCode = (int)response.HttpStatusCode, |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + public void Dispose() => _client.Dispose(); |
| 129 | +} |
| 130 | + |
| 131 | +public class InvocationResult |
| 132 | +{ |
| 133 | + public string RawBody { get; set; } = ""; |
| 134 | + public string Message { get; set; } = ""; |
| 135 | + public int HttpStatusCode { get; set; } |
| 136 | +} |
| 137 | + |
| 138 | +public class StreamingInvocationResult |
| 139 | +{ |
| 140 | + public List<string> Chunks { get; set; } = new(); |
| 141 | + public string FinalMessage { get; set; } = ""; |
| 142 | + public int HttpStatusCode { get; set; } |
| 143 | +} |
0 commit comments