|
| 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.adk.kt.annotations.FrameworkInternalApi |
| 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.transport.spi.ClientTransport |
| 25 | +import org.a2aproject.sdk.client.transport.spi.interceptors.ClientCallContext |
| 26 | +import org.a2aproject.sdk.jsonrpc.common.json.JsonProcessingException |
| 27 | +import org.a2aproject.sdk.jsonrpc.common.json.JsonUtil |
| 28 | +import org.a2aproject.sdk.jsonrpc.common.wrappers.ListTasksResult |
| 29 | +import org.a2aproject.sdk.jsonrpc.common.wrappers.SendMessageRequest |
| 30 | +import org.a2aproject.sdk.spec.A2AClientException |
| 31 | +import org.a2aproject.sdk.spec.AgentCard |
| 32 | +import org.a2aproject.sdk.spec.CancelTaskParams |
| 33 | +import org.a2aproject.sdk.spec.DeleteTaskPushNotificationConfigParams |
| 34 | +import org.a2aproject.sdk.spec.EventKind |
| 35 | +import org.a2aproject.sdk.spec.GetExtendedAgentCardParams |
| 36 | +import org.a2aproject.sdk.spec.GetTaskPushNotificationConfigParams |
| 37 | +import org.a2aproject.sdk.spec.ListTaskPushNotificationConfigsParams |
| 38 | +import org.a2aproject.sdk.spec.ListTaskPushNotificationConfigsResult |
| 39 | +import org.a2aproject.sdk.spec.ListTasksParams |
| 40 | +import org.a2aproject.sdk.spec.MessageSendParams |
| 41 | +import org.a2aproject.sdk.spec.StreamingEventKind |
| 42 | +import org.a2aproject.sdk.spec.Task |
| 43 | +import org.a2aproject.sdk.spec.TaskIdParams |
| 44 | +import org.a2aproject.sdk.spec.TaskPushNotificationConfig |
| 45 | +import org.a2aproject.sdk.spec.TaskQueryParams |
| 46 | + |
| 47 | +/** |
| 48 | + * A proto-free [ClientTransport] that runs a real non-streaming `message/send` JSON-RPC round-trip |
| 49 | + * over an injected [A2AHttpClient]. |
| 50 | + * |
| 51 | + * The SDK's `JSONRPCTransport` marshals through protobuf and isn't Android-buildable, so this |
| 52 | + * reuses the SDK's proto-free `jsonrpccommon` [JsonUtil] to serialize the request and parse the |
| 53 | + * response. The remaining [ClientTransport] methods throw [UnsupportedOperationException]. |
| 54 | + * |
| 55 | + * [FrameworkInternalApi]: an implementation detail wired up by `androidA2AClient(...)`; not part of |
| 56 | + * the public API. |
| 57 | + */ |
| 58 | +@FrameworkInternalApi |
| 59 | +class JsonRpcHttpClientTransport(private val httpClient: A2AHttpClient, private val url: String) : |
| 60 | + ClientTransport { |
| 61 | + |
| 62 | + override fun sendMessage(request: MessageSendParams, context: ClientCallContext?): EventKind { |
| 63 | + // Serialize via the SDK's proto-free JSON-RPC machinery (no hand-rolled JSON). |
| 64 | + val body: String = |
| 65 | + try { |
| 66 | + val envelope: JsonObject = |
| 67 | + JsonParser.parseString( |
| 68 | + JsonUtil.toJson(SendMessageRequest(JSONRPC_VERSION, REQUEST_ID, request)) |
| 69 | + ) |
| 70 | + .asJsonObject |
| 71 | + // JsonUtil wraps params.message via the StreamingEventKind adapter; unwrap it for the wire. |
| 72 | + val params = envelope.getAsJsonObject("params") |
| 73 | + params.add("message", params.getAsJsonObject("message").get("message")) |
| 74 | + envelope.toString() |
| 75 | + } catch (e: JsonProcessingException) { |
| 76 | + throw A2AClientException("Failed to serialize A2A request", e) |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + val response = |
| 81 | + httpClient |
| 82 | + .createPost() |
| 83 | + .url(url) |
| 84 | + .addHeader(A2AHttpClient.CONTENT_TYPE, A2AHttpClient.APPLICATION_JSON) |
| 85 | + .addHeader(A2A_VERSION_HEADER, A2A_VERSION) |
| 86 | + .body(body) |
| 87 | + .post() |
| 88 | + if (response.status() < 200 || response.status() >= 300) { |
| 89 | + throw A2AClientException("Unexpected HTTP status: ${response.status()}") |
| 90 | + } |
| 91 | + return parseSendMessageResponse(response.body()) |
| 92 | + } catch (e: A2AClientException) { |
| 93 | + throw e |
| 94 | + } catch (e: InterruptedException) { |
| 95 | + Thread.currentThread().interrupt() |
| 96 | + throw A2AClientException("Android A2A HTTP round-trip interrupted", e) |
| 97 | + } catch (e: Exception) { |
| 98 | + throw A2AClientException("Android A2A HTTP round-trip failed", e) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // --- Unused operations ------------------------------------------------------------------------- |
| 103 | + |
| 104 | + override fun sendMessageStreaming( |
| 105 | + request: MessageSendParams, |
| 106 | + eventConsumer: Consumer<StreamingEventKind>, |
| 107 | + errorConsumer: Consumer<Throwable>, |
| 108 | + context: ClientCallContext?, |
| 109 | + ): Unit = |
| 110 | + throw UnsupportedOperationException("streaming not supported by JsonRpcHttpClientTransport") |
| 111 | + |
| 112 | + override fun getTask(request: TaskQueryParams, context: ClientCallContext?): Task = |
| 113 | + throw UnsupportedOperationException() |
| 114 | + |
| 115 | + override fun cancelTask(request: CancelTaskParams, context: ClientCallContext?): Task = |
| 116 | + throw UnsupportedOperationException() |
| 117 | + |
| 118 | + override fun listTasks(request: ListTasksParams, context: ClientCallContext?): ListTasksResult = |
| 119 | + throw UnsupportedOperationException() |
| 120 | + |
| 121 | + override fun createTaskPushNotificationConfiguration( |
| 122 | + request: TaskPushNotificationConfig, |
| 123 | + context: ClientCallContext?, |
| 124 | + ): TaskPushNotificationConfig = throw UnsupportedOperationException() |
| 125 | + |
| 126 | + override fun getTaskPushNotificationConfiguration( |
| 127 | + request: GetTaskPushNotificationConfigParams, |
| 128 | + context: ClientCallContext?, |
| 129 | + ): TaskPushNotificationConfig = throw UnsupportedOperationException() |
| 130 | + |
| 131 | + override fun listTaskPushNotificationConfigurations( |
| 132 | + request: ListTaskPushNotificationConfigsParams, |
| 133 | + context: ClientCallContext?, |
| 134 | + ): ListTaskPushNotificationConfigsResult = throw UnsupportedOperationException() |
| 135 | + |
| 136 | + override fun deleteTaskPushNotificationConfigurations( |
| 137 | + request: DeleteTaskPushNotificationConfigParams, |
| 138 | + context: ClientCallContext?, |
| 139 | + ): Unit = throw UnsupportedOperationException() |
| 140 | + |
| 141 | + override fun subscribeToTask( |
| 142 | + request: TaskIdParams, |
| 143 | + eventConsumer: Consumer<StreamingEventKind>, |
| 144 | + errorConsumer: Consumer<Throwable>, |
| 145 | + context: ClientCallContext?, |
| 146 | + ): Unit = throw UnsupportedOperationException() |
| 147 | + |
| 148 | + override fun getExtendedAgentCard( |
| 149 | + params: GetExtendedAgentCardParams, |
| 150 | + context: ClientCallContext?, |
| 151 | + ): AgentCard = throw UnsupportedOperationException() |
| 152 | + |
| 153 | + override fun close() {} |
| 154 | + |
| 155 | + private companion object { |
| 156 | + const val JSONRPC_VERSION = "2.0" |
| 157 | + const val REQUEST_ID = "1" |
| 158 | + const val A2A_VERSION_HEADER = "A2A-Version" |
| 159 | + const val A2A_VERSION = "1.0" |
| 160 | + |
| 161 | + /** Parses a JSON-RPC `message/send` response body into its result [EventKind]. */ |
| 162 | + fun parseSendMessageResponse(responseBody: String): EventKind { |
| 163 | + val envelope = JsonParser.parseString(responseBody).asJsonObject |
| 164 | + |
| 165 | + val errorNode = envelope.get("error") |
| 166 | + if (errorNode != null && !errorNode.isJsonNull) { |
| 167 | + throw A2AClientException("A2A JSON-RPC error: $errorNode") |
| 168 | + } |
| 169 | + |
| 170 | + val resultNode = envelope.get("result") |
| 171 | + if (resultNode == null || !resultNode.isJsonObject) { |
| 172 | + throw A2AClientException("A2A JSON-RPC response missing 'result' object") |
| 173 | + } |
| 174 | + |
| 175 | + return try { |
| 176 | + // Result is a Task/Message; the SDK's StreamingEventKind adapter picks the concrete type. |
| 177 | + JsonUtil.fromJson(resultNode.toString(), StreamingEventKind::class.java) as EventKind |
| 178 | + } catch (e: JsonProcessingException) { |
| 179 | + throw A2AClientException("Failed to parse A2A response result", e) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments