|
| 1 | +package io.a2a.client; |
| 2 | + |
| 3 | +import io.a2a.client.config.ClientConfig; |
| 4 | +import io.a2a.client.transport.jsonrpc.JSONRPCTransport; |
| 5 | +import io.a2a.client.transport.jsonrpc.JSONRPCTransportConfig; |
| 6 | +import io.a2a.client.transport.jsonrpc.JSONRPCTransportConfigBuilder; |
| 7 | +import io.a2a.client.transport.spi.ClientTransport; |
| 8 | +import io.a2a.client.transport.spi.ClientTransportConfig; |
| 9 | +import io.a2a.client.transport.spi.ClientTransportConfigBuilder; |
| 10 | +import io.a2a.client.transport.spi.ClientTransportProvider; |
| 11 | +import io.a2a.spec.A2AClientException; |
| 12 | +import io.a2a.spec.AgentCard; |
| 13 | +import io.a2a.spec.AgentInterface; |
| 14 | +import io.a2a.spec.TransportProtocol; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.LinkedHashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.ServiceLoader; |
| 22 | +import java.util.function.BiConsumer; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author David BRASSELY (david.brassely at graviteesource.com) |
| 27 | + * @author GraviteeSource Team |
| 28 | + */ |
| 29 | +public class ClientBuilder { |
| 30 | + |
| 31 | + private static final Map<String, ClientTransportProvider<? extends ClientTransport, ? extends ClientTransportConfig<?>>> transportProviderRegistry = new HashMap<>(); |
| 32 | + private static final Map<Class<? extends ClientTransport>, String> transportProtocolMapping = new HashMap<>(); |
| 33 | + |
| 34 | + static { |
| 35 | + ServiceLoader<ClientTransportProvider> loader = ServiceLoader.load(ClientTransportProvider.class); |
| 36 | + for (ClientTransportProvider<?, ?> transport : loader) { |
| 37 | + transportProviderRegistry.put(transport.getTransportProtocol(), transport); |
| 38 | + transportProtocolMapping.put(transport.getTransportProtocolClass(), transport.getTransportProtocol()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private final AgentCard agentCard; |
| 43 | + private boolean useClientPreference; |
| 44 | + |
| 45 | + private final List<BiConsumer<ClientEvent, AgentCard>> consumers = new ArrayList<>(); |
| 46 | + private Consumer<Throwable> streamErrorHandler; |
| 47 | + private ClientConfig clientConfig; |
| 48 | + |
| 49 | + private final Map<Class<? extends ClientTransport>, ClientTransportConfig<? extends ClientTransport>> clientTransports = new LinkedHashMap<>(); |
| 50 | + |
| 51 | + ClientBuilder(AgentCard agentCard) { |
| 52 | + this.agentCard = agentCard; |
| 53 | + } |
| 54 | + |
| 55 | + public <T extends ClientTransport> ClientBuilder withTransport(Class<T> clazz, ClientTransportConfigBuilder<? extends ClientTransportConfig<T>, ?> configBuilder) { |
| 56 | + return withTransport(clazz, configBuilder.build()); |
| 57 | + } |
| 58 | + |
| 59 | + public <T extends ClientTransport> ClientBuilder withTransport(Class<T> clazz, ClientTransportConfig<T> config) { |
| 60 | + clientTransports.put(clazz, config); |
| 61 | + |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + public ClientBuilder useClientPreference(boolean useClientPreference) { |
| 66 | + this.useClientPreference = useClientPreference; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public ClientBuilder withJsonRpcTransport(JSONRPCTransportConfigBuilder configBuilder) { |
| 71 | + return withTransport(JSONRPCTransport.class, configBuilder.build()); |
| 72 | + } |
| 73 | + |
| 74 | + public ClientBuilder withJsonRpcTransport(JSONRPCTransportConfig config) { |
| 75 | + return withTransport(JSONRPCTransport.class, config); |
| 76 | + } |
| 77 | + |
| 78 | + public ClientBuilder withJsonRpcTransport() { |
| 79 | + return withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder()); |
| 80 | + } |
| 81 | + |
| 82 | + public ClientBuilder addStreamConsumer(BiConsumer<ClientEvent, AgentCard> consumer) { |
| 83 | + this.consumers.add(consumer); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public ClientBuilder addStreamConsumers(List<BiConsumer<ClientEvent, AgentCard>> consumers) { |
| 88 | + this.consumers.addAll(consumers); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public ClientBuilder streamErrorHandler(Consumer<Throwable> streamErrorHandler) { |
| 93 | + this.streamErrorHandler = streamErrorHandler; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + public ClientBuilder clientConfig(ClientConfig clientConfig) { |
| 98 | + this.clientConfig = clientConfig; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public Client build() throws A2AClientException { |
| 103 | + if (this.clientConfig == null) { |
| 104 | + this.clientConfig = new ClientConfig.Builder().build(); |
| 105 | + } |
| 106 | + |
| 107 | + ClientTransport clientTransport = buildClientTransport(); |
| 108 | + |
| 109 | + return new Client(agentCard, clientConfig, clientTransport, consumers, streamErrorHandler); |
| 110 | + } |
| 111 | + |
| 112 | + @SuppressWarnings("unchecked") |
| 113 | + private ClientTransport buildClientTransport() throws A2AClientException { |
| 114 | + // Get the preferred transport |
| 115 | + AgentInterface agentInterface = findBestClientTransport(); |
| 116 | + Class<? extends ClientTransport> transportProtocolClass = transportProviderRegistry.get(agentInterface.transport()).getTransportProtocolClass(); |
| 117 | + |
| 118 | + // Get the transport provider associated to the protocol |
| 119 | + ClientTransportProvider clientTransportProvider = transportProviderRegistry.get(agentInterface.transport()); |
| 120 | + |
| 121 | + // Retrieve the configuration associated to the preferred transport |
| 122 | + ClientTransportConfig<? extends ClientTransport> clientTransportConfig = clientTransports.get(transportProtocolClass); |
| 123 | + |
| 124 | + return clientTransportProvider.create(clientTransportConfig, agentCard, agentInterface.url()); |
| 125 | + } |
| 126 | + |
| 127 | + private Map<String, String> getServerPreferredTransports() { |
| 128 | + Map<String, String> serverPreferredTransports = new LinkedHashMap<>(); |
| 129 | + serverPreferredTransports.put(agentCard.preferredTransport(), agentCard.url()); |
| 130 | + for (AgentInterface agentInterface : agentCard.additionalInterfaces()) { |
| 131 | + serverPreferredTransports.putIfAbsent(agentInterface.transport(), agentInterface.url()); |
| 132 | + } |
| 133 | + return serverPreferredTransports; |
| 134 | + } |
| 135 | + |
| 136 | + private List<String> getClientPreferredTransports() { |
| 137 | + List<String> supportedClientTransports = new ArrayList<>(); |
| 138 | + |
| 139 | + if (clientTransports.isEmpty()) { |
| 140 | + // default to JSONRPC if not specified |
| 141 | + supportedClientTransports.add(TransportProtocol.JSONRPC.asString()); |
| 142 | + } else { |
| 143 | + clientTransports.forEach((aClass, clientTransportConfig) -> supportedClientTransports.add(transportProtocolMapping.get(aClass))); |
| 144 | + } |
| 145 | + return supportedClientTransports; |
| 146 | + } |
| 147 | + |
| 148 | + private AgentInterface findBestClientTransport() throws A2AClientException { |
| 149 | + // Retrieve transport supported by the A2A server |
| 150 | + Map<String, String> serverPreferredTransports = getServerPreferredTransports(); |
| 151 | + |
| 152 | + // Retrieve transport configured for this client (using withTransport methods) |
| 153 | + List<String> clientPreferredTransports = getClientPreferredTransports(); |
| 154 | + |
| 155 | + String transportProtocol = null; |
| 156 | + String transportUrl = null; |
| 157 | + if (useClientPreference) { |
| 158 | + for (String clientPreferredTransport : clientPreferredTransports) { |
| 159 | + if (serverPreferredTransports.containsKey(clientPreferredTransport)) { |
| 160 | + transportProtocol = clientPreferredTransport; |
| 161 | + transportUrl = serverPreferredTransports.get(transportProtocol); |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + } else { |
| 166 | + for (Map.Entry<String, String> transport : serverPreferredTransports.entrySet()) { |
| 167 | + if (clientPreferredTransports.contains(transport.getKey())) { |
| 168 | + transportProtocol = transport.getKey(); |
| 169 | + transportUrl = transport.getValue(); |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + if (transportProtocol == null || transportUrl == null) { |
| 175 | + throw new A2AClientException("No compatible transport found"); |
| 176 | + } |
| 177 | + if (! transportProviderRegistry.containsKey(transportProtocol)) { |
| 178 | + throw new A2AClientException("No client available for " + transportProtocol); |
| 179 | + } |
| 180 | + |
| 181 | + return new AgentInterface(transportProtocol, transportUrl); |
| 182 | + } |
| 183 | +} |
0 commit comments