|
| 1 | +using System.Net.ServerSentEvents; |
| 2 | +using System.Text.Json; |
| 3 | + |
| 4 | +namespace A2A; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Extension methods for the <see cref="A2AClient"/> class making its API more |
| 8 | +/// convenient for certain use-cases. |
| 9 | +/// </summary> |
| 10 | +public static class A2AClientExtensions |
| 11 | +{ |
| 12 | + /// <inheritdoc cref="A2AClient.SendMessageAsync(MessageSendParams, CancellationToken)"/> |
| 13 | + public static Task<A2AResponse> SendMessageAsync( |
| 14 | + this A2AClient client, |
| 15 | + Message message, |
| 16 | + MessageSendConfiguration? configuration = null, |
| 17 | + Dictionary<string, JsonElement>? metadata = null, |
| 18 | + CancellationToken cancellationToken = default) |
| 19 | + { |
| 20 | + if (client is null) |
| 21 | + { |
| 22 | + throw new ArgumentNullException(nameof(client)); |
| 23 | + } |
| 24 | + |
| 25 | + return client.SendMessageAsync( |
| 26 | + new MessageSendParams |
| 27 | + { |
| 28 | + Message = message, |
| 29 | + Configuration = configuration, |
| 30 | + Metadata = metadata |
| 31 | + }, |
| 32 | + cancellationToken); |
| 33 | + } |
| 34 | + |
| 35 | + /// <inheritdoc cref="A2AClient.CancelTaskAsync(TaskIdParams, CancellationToken)"/> |
| 36 | + public static Task<AgentTask> CancelTaskAsync( |
| 37 | + this A2AClient client, |
| 38 | + string taskId, |
| 39 | + Dictionary<string, JsonElement>? metadata = null, |
| 40 | + CancellationToken cancellationToken = default) |
| 41 | + { |
| 42 | + if (client is null) |
| 43 | + { |
| 44 | + throw new ArgumentNullException(nameof(client)); |
| 45 | + } |
| 46 | + |
| 47 | + return client.CancelTaskAsync( |
| 48 | + new TaskIdParams |
| 49 | + { |
| 50 | + Id = taskId, |
| 51 | + Metadata = metadata |
| 52 | + }, |
| 53 | + cancellationToken); |
| 54 | + } |
| 55 | + |
| 56 | + /// <inheritdoc cref="A2AClient.SetPushNotificationAsync(TaskPushNotificationConfig, CancellationToken)"/> |
| 57 | + public static Task<TaskPushNotificationConfig> SetPushNotificationAsync( |
| 58 | + this A2AClient client, |
| 59 | + string taskId, |
| 60 | + string url, |
| 61 | + string? configId = null, |
| 62 | + string? token = null, |
| 63 | + PushNotificationAuthenticationInfo? authentication = null, |
| 64 | + CancellationToken cancellationToken = default) |
| 65 | + { |
| 66 | + if (client is null) |
| 67 | + { |
| 68 | + throw new ArgumentNullException(nameof(client)); |
| 69 | + } |
| 70 | + |
| 71 | + return client.SetPushNotificationAsync( |
| 72 | + new TaskPushNotificationConfig |
| 73 | + { |
| 74 | + TaskId = taskId, |
| 75 | + PushNotificationConfig = new PushNotificationConfig |
| 76 | + { |
| 77 | + Id = configId, |
| 78 | + Url = url, |
| 79 | + Token = token, |
| 80 | + Authentication = authentication |
| 81 | + } |
| 82 | + }, |
| 83 | + cancellationToken); |
| 84 | + } |
| 85 | + |
| 86 | + /// <inheritdoc cref="A2AClient.GetPushNotificationAsync(GetTaskPushNotificationConfigParams, CancellationToken)"/> |
| 87 | + public static Task<TaskPushNotificationConfig> GetPushNotificationAsync( |
| 88 | + this A2AClient client, |
| 89 | + string taskId, |
| 90 | + string configId, |
| 91 | + Dictionary<string, JsonElement>? metadata = null, |
| 92 | + CancellationToken cancellationToken = default) |
| 93 | + { |
| 94 | + if (client is null) |
| 95 | + { |
| 96 | + throw new ArgumentNullException(nameof(client)); |
| 97 | + } |
| 98 | + |
| 99 | + return client.GetPushNotificationAsync( |
| 100 | + new GetTaskPushNotificationConfigParams |
| 101 | + { |
| 102 | + Id = taskId, |
| 103 | + PushNotificationConfigId = configId, |
| 104 | + Metadata = metadata |
| 105 | + }, |
| 106 | + cancellationToken); |
| 107 | + } |
| 108 | + |
| 109 | + /// <inheritdoc cref="A2AClient.SendMessageStreamingAsync(MessageSendParams, CancellationToken)"/> |
| 110 | + public static IAsyncEnumerable<SseItem<A2AEvent>> SendMessageStreamingAsync( |
| 111 | + this A2AClient client, |
| 112 | + Message message, |
| 113 | + MessageSendConfiguration? configuration = null, |
| 114 | + Dictionary<string, JsonElement>? metadata = null, |
| 115 | + CancellationToken cancellationToken = default) |
| 116 | + |
| 117 | + { |
| 118 | + if (client is null) |
| 119 | + { |
| 120 | + throw new ArgumentNullException(nameof(client)); |
| 121 | + } |
| 122 | + |
| 123 | + return client.SendMessageStreamingAsync( |
| 124 | + new MessageSendParams |
| 125 | + { |
| 126 | + Message = message, |
| 127 | + Configuration = configuration, |
| 128 | + Metadata = metadata |
| 129 | + }, |
| 130 | + cancellationToken); |
| 131 | + } |
| 132 | +} |
0 commit comments