|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.kt.a2a.android; |
| 18 | + |
| 19 | +import com.google.gson.JsonElement; |
| 20 | +import com.google.gson.JsonObject; |
| 21 | +import com.google.gson.JsonParser; |
| 22 | +import java.util.function.Consumer; |
| 23 | +import org.a2aproject.sdk.client.http.A2AHttpClient; |
| 24 | +import org.a2aproject.sdk.client.http.A2AHttpResponse; |
| 25 | +import org.a2aproject.sdk.client.transport.spi.ClientTransport; |
| 26 | +import org.a2aproject.sdk.client.transport.spi.interceptors.ClientCallContext; |
| 27 | +import org.a2aproject.sdk.jsonrpc.common.json.JsonProcessingException; |
| 28 | +import org.a2aproject.sdk.jsonrpc.common.json.JsonUtil; |
| 29 | +import org.a2aproject.sdk.jsonrpc.common.wrappers.ListTasksResult; |
| 30 | +import org.a2aproject.sdk.jsonrpc.common.wrappers.SendMessageRequest; |
| 31 | +import org.a2aproject.sdk.spec.A2AClientException; |
| 32 | +import org.a2aproject.sdk.spec.AgentCard; |
| 33 | +import org.a2aproject.sdk.spec.CancelTaskParams; |
| 34 | +import org.a2aproject.sdk.spec.DeleteTaskPushNotificationConfigParams; |
| 35 | +import org.a2aproject.sdk.spec.EventKind; |
| 36 | +import org.a2aproject.sdk.spec.GetExtendedAgentCardParams; |
| 37 | +import org.a2aproject.sdk.spec.GetTaskPushNotificationConfigParams; |
| 38 | +import org.a2aproject.sdk.spec.ListTaskPushNotificationConfigsParams; |
| 39 | +import org.a2aproject.sdk.spec.ListTaskPushNotificationConfigsResult; |
| 40 | +import org.a2aproject.sdk.spec.ListTasksParams; |
| 41 | +import org.a2aproject.sdk.spec.MessageSendParams; |
| 42 | +import org.a2aproject.sdk.spec.StreamingEventKind; |
| 43 | +import org.a2aproject.sdk.spec.Task; |
| 44 | +import org.a2aproject.sdk.spec.TaskIdParams; |
| 45 | +import org.a2aproject.sdk.spec.TaskPushNotificationConfig; |
| 46 | +import org.a2aproject.sdk.spec.TaskQueryParams; |
| 47 | +import org.jspecify.annotations.Nullable; |
| 48 | + |
| 49 | +/** |
| 50 | + * A proto-free {@link ClientTransport} that runs a real non-streaming {@code message/send} JSON-RPC |
| 51 | + * round-trip over an injected {@link A2AHttpClient}. |
| 52 | + * |
| 53 | + * <p>The SDK's {@code JSONRPCTransport} marshals through protobuf and isn't Android-buildable, so |
| 54 | + * this reuses the SDK's proto-free {@code jsonrpccommon} {@link JsonUtil} to serialize the request |
| 55 | + * and parse the response, proving a proto-free A2A transport works on Android. The remaining {@link |
| 56 | + * ClientTransport} methods throw {@link UnsupportedOperationException}. |
| 57 | + */ |
| 58 | +public final class JsonRpcHttpClientTransport implements ClientTransport { |
| 59 | + |
| 60 | + private static final String JSONRPC_VERSION = "2.0"; |
| 61 | + private static final String REQUEST_ID = "1"; |
| 62 | + private static final String A2A_VERSION_HEADER = "A2A-Version"; |
| 63 | + private static final String A2A_VERSION = "1.0"; |
| 64 | + |
| 65 | + private final A2AHttpClient httpClient; |
| 66 | + private final String url; |
| 67 | + |
| 68 | + public JsonRpcHttpClientTransport(A2AHttpClient httpClient, String url) { |
| 69 | + this.httpClient = httpClient; |
| 70 | + this.url = url; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public EventKind sendMessage(MessageSendParams request, @Nullable ClientCallContext context) { |
| 75 | + // Serialize via the SDK's proto-free JSON-RPC machinery (no hand-rolled JSON). |
| 76 | + String body; |
| 77 | + try { |
| 78 | + JsonObject envelope = |
| 79 | + JsonParser.parseString( |
| 80 | + JsonUtil.toJson(new SendMessageRequest(JSONRPC_VERSION, REQUEST_ID, request))) |
| 81 | + .getAsJsonObject(); |
| 82 | + // JsonUtil wraps params.message via the StreamingEventKind adapter; unwrap it for the wire. |
| 83 | + JsonObject params = envelope.getAsJsonObject("params"); |
| 84 | + params.add("message", params.getAsJsonObject("message").get("message")); |
| 85 | + body = envelope.toString(); |
| 86 | + } catch (JsonProcessingException e) { |
| 87 | + throw new A2AClientException("Failed to serialize A2A request", e); |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + A2AHttpResponse response = |
| 92 | + httpClient |
| 93 | + .createPost() |
| 94 | + .url(url) |
| 95 | + .addHeader(A2AHttpClient.CONTENT_TYPE, A2AHttpClient.APPLICATION_JSON) |
| 96 | + .addHeader(A2A_VERSION_HEADER, A2A_VERSION) |
| 97 | + .body(body) |
| 98 | + .post(); |
| 99 | + if (response.status() < 200 || response.status() >= 300) { |
| 100 | + throw new A2AClientException("Unexpected HTTP status: " + response.status()); |
| 101 | + } |
| 102 | + return parseSendMessageResponse(response.body()); |
| 103 | + } catch (A2AClientException e) { |
| 104 | + throw e; |
| 105 | + } catch (InterruptedException e) { |
| 106 | + Thread.currentThread().interrupt(); |
| 107 | + throw new A2AClientException("Android A2A HTTP round-trip interrupted", e); |
| 108 | + } catch (Exception e) { |
| 109 | + throw new A2AClientException("Android A2A HTTP round-trip failed", e); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** Parses a JSON-RPC {@code message/send} response body into its result {@link EventKind}. */ |
| 114 | + private static EventKind parseSendMessageResponse(String responseBody) { |
| 115 | + JsonObject envelope = JsonParser.parseString(responseBody).getAsJsonObject(); |
| 116 | + |
| 117 | + JsonElement errorNode = envelope.get("error"); |
| 118 | + if (errorNode != null && !errorNode.isJsonNull()) { |
| 119 | + throw new A2AClientException("A2A JSON-RPC error: " + errorNode); |
| 120 | + } |
| 121 | + |
| 122 | + JsonElement resultNode = envelope.get("result"); |
| 123 | + if (resultNode == null || !resultNode.isJsonObject()) { |
| 124 | + throw new A2AClientException("A2A JSON-RPC response missing 'result' object"); |
| 125 | + } |
| 126 | + |
| 127 | + try { |
| 128 | + // Result is a Task/Message; the SDK's StreamingEventKind adapter picks the concrete type. |
| 129 | + return (EventKind) JsonUtil.fromJson(resultNode.toString(), StreamingEventKind.class); |
| 130 | + } catch (JsonProcessingException e) { |
| 131 | + throw new A2AClientException("Failed to parse A2A response result", e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // --- Unused operations ------------------------------------------------------------------------- |
| 136 | + |
| 137 | + @Override |
| 138 | + public void sendMessageStreaming( |
| 139 | + MessageSendParams request, |
| 140 | + Consumer<StreamingEventKind> eventConsumer, |
| 141 | + Consumer<Throwable> errorConsumer, |
| 142 | + @Nullable ClientCallContext context) { |
| 143 | + throw new UnsupportedOperationException( |
| 144 | + "streaming not supported by JsonRpcHttpClientTransport"); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Task getTask(TaskQueryParams request, @Nullable ClientCallContext context) { |
| 149 | + throw new UnsupportedOperationException(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public Task cancelTask(CancelTaskParams request, @Nullable ClientCallContext context) { |
| 154 | + throw new UnsupportedOperationException(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public ListTasksResult listTasks(ListTasksParams request, @Nullable ClientCallContext context) { |
| 159 | + throw new UnsupportedOperationException(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public TaskPushNotificationConfig createTaskPushNotificationConfiguration( |
| 164 | + TaskPushNotificationConfig request, @Nullable ClientCallContext context) { |
| 165 | + throw new UnsupportedOperationException(); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public TaskPushNotificationConfig getTaskPushNotificationConfiguration( |
| 170 | + GetTaskPushNotificationConfigParams request, @Nullable ClientCallContext context) { |
| 171 | + throw new UnsupportedOperationException(); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public ListTaskPushNotificationConfigsResult listTaskPushNotificationConfigurations( |
| 176 | + ListTaskPushNotificationConfigsParams request, @Nullable ClientCallContext context) { |
| 177 | + throw new UnsupportedOperationException(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void deleteTaskPushNotificationConfigurations( |
| 182 | + DeleteTaskPushNotificationConfigParams request, @Nullable ClientCallContext context) { |
| 183 | + throw new UnsupportedOperationException(); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void subscribeToTask( |
| 188 | + TaskIdParams request, |
| 189 | + Consumer<StreamingEventKind> eventConsumer, |
| 190 | + Consumer<Throwable> errorConsumer, |
| 191 | + @Nullable ClientCallContext context) { |
| 192 | + throw new UnsupportedOperationException(); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public AgentCard getExtendedAgentCard( |
| 197 | + GetExtendedAgentCardParams params, @Nullable ClientCallContext context) { |
| 198 | + throw new UnsupportedOperationException(); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void close() {} |
| 203 | +} |
0 commit comments