|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.agentclientprotocol.sdk.agent.support; |
| 6 | + |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.atomic.AtomicInteger; |
| 10 | +import java.util.function.Function; |
| 11 | + |
| 12 | +import com.agentclientprotocol.sdk.annotation.AcpAgent; |
| 13 | +import com.agentclientprotocol.sdk.annotation.LoadSession; |
| 14 | +import com.agentclientprotocol.sdk.annotation.NewSession; |
| 15 | +import com.agentclientprotocol.sdk.annotation.Prompt; |
| 16 | +import com.agentclientprotocol.sdk.client.AcpAsyncClient; |
| 17 | +import com.agentclientprotocol.sdk.client.AcpClient; |
| 18 | +import com.agentclientprotocol.sdk.spec.AcpSchema; |
| 19 | +import com.agentclientprotocol.sdk.spec.AcpSchema.InitializeResponse; |
| 20 | +import com.agentclientprotocol.sdk.spec.AcpSchema.NewSessionResponse; |
| 21 | +import com.agentclientprotocol.sdk.spec.AcpSchema.PromptRequest; |
| 22 | +import com.agentclientprotocol.sdk.spec.AcpSchema.PromptResponse; |
| 23 | +import com.agentclientprotocol.sdk.test.InMemoryTransportPair; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 28 | + |
| 29 | +/** |
| 30 | + * Handler discovery walks the class hierarchy: a bean that a framework proxies (Spring CGLIB, |
| 31 | + * Quarkus ArC or Micronaut AOP generate a subclass that carries no annotations) is still found, |
| 32 | + * and invoked through the proxy, so its interceptors run; handlers declared on a superclass are |
| 33 | + * found; an override is one handler, not two; and the bridge method a generic override compiles |
| 34 | + * to is not a handler of its own. |
| 35 | + */ |
| 36 | +class AgentDiscoveryTest { |
| 37 | + |
| 38 | + private static final Duration TIMEOUT = Duration.ofSeconds(5); |
| 39 | + |
| 40 | + @AcpAgent(name = "proxied", version = "1") |
| 41 | + static class ProxiedAgent { |
| 42 | + |
| 43 | + @NewSession |
| 44 | + public NewSessionResponse newSession() { |
| 45 | + return new NewSessionResponse("s1", null, null); |
| 46 | + } |
| 47 | + |
| 48 | + @LoadSession |
| 49 | + public AcpSchema.LoadSessionResponse load() { |
| 50 | + return new AcpSchema.LoadSessionResponse(null); |
| 51 | + } |
| 52 | + |
| 53 | + @Prompt |
| 54 | + public PromptResponse prompt() { |
| 55 | + return new PromptResponse(AcpSchema.StopReason.END_TURN); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + /** What a CGLIB/ArC/Micronaut proxy looks like: an unannotated subclass overriding public methods. */ |
| 61 | + static class ProxiedAgent$$SpringCGLIB$$0 extends ProxiedAgent { |
| 62 | + |
| 63 | + final AtomicInteger intercepted = new AtomicInteger(); |
| 64 | + |
| 65 | + @Override |
| 66 | + public PromptResponse prompt() { |
| 67 | + intercepted.incrementAndGet(); |
| 68 | + return super.prompt(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public NewSessionResponse newSession() { |
| 73 | + intercepted.incrementAndGet(); |
| 74 | + return super.newSession(); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void aProxiedBeanIsDiscoveredOnItsUserClassAndInvokedThroughTheProxy() { |
| 81 | + ProxiedAgent$$SpringCGLIB$$0 proxy = new ProxiedAgent$$SpringCGLIB$$0(); |
| 82 | + |
| 83 | + InitializeResponse init = session(AcpAgentSupport.create(proxy), client -> { |
| 84 | + assertThat(prompt(client)).isEqualTo(AcpSchema.StopReason.END_TURN); |
| 85 | + return client.initialize().block(TIMEOUT); |
| 86 | + }); |
| 87 | + |
| 88 | + assertThat(proxy.intercepted).as("newSession and prompt ran through the proxy").hasValue(2); |
| 89 | + assertThat(init.agentCapabilities().loadSession()).isTrue(); |
| 90 | + assertThat(init.agentInfo().name()).isEqualTo("proxied"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void anAnonymousSubclassIsDiscoveredToo() { |
| 95 | + AtomicInteger intercepted = new AtomicInteger(); |
| 96 | + ProxiedAgent anonymous = new ProxiedAgent() { |
| 97 | + @Override |
| 98 | + public PromptResponse prompt() { |
| 99 | + intercepted.incrementAndGet(); |
| 100 | + return super.prompt(); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + session(AcpAgentSupport.create(anonymous), AgentDiscoveryTest::prompt); |
| 105 | + |
| 106 | + assertThat(intercepted).hasValue(1); |
| 107 | + } |
| 108 | + |
| 109 | + abstract static class AbstractAgent { |
| 110 | + |
| 111 | + @NewSession |
| 112 | + NewSessionResponse newSession() { |
| 113 | + return new NewSessionResponse("s1", null, null); |
| 114 | + } |
| 115 | + |
| 116 | + @Prompt |
| 117 | + PromptResponse prompt(PromptRequest request) { |
| 118 | + return new PromptResponse(AcpSchema.StopReason.MAX_TOKENS); |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + @AcpAgent |
| 124 | + static class ConcreteAgent extends AbstractAgent { |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void handlersOnAnAbstractSuperclassAreDiscovered() { |
| 130 | + assertThat(session(AcpAgentSupport.create(new ConcreteAgent()), AgentDiscoveryTest::prompt)) |
| 131 | + .isEqualTo(AcpSchema.StopReason.MAX_TOKENS); |
| 132 | + } |
| 133 | + |
| 134 | + @AcpAgent |
| 135 | + static class OverridingAgent extends AbstractAgent { |
| 136 | + |
| 137 | + @Override |
| 138 | + @Prompt |
| 139 | + PromptResponse prompt(PromptRequest request) { |
| 140 | + return new PromptResponse(AcpSchema.StopReason.REFUSAL); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void anAnnotatedOverrideIsOneHandlerAndTheOverrideRuns() { |
| 147 | + assertThat(session(AcpAgentSupport.create(new OverridingAgent()), AgentDiscoveryTest::prompt)) |
| 148 | + .isEqualTo(AcpSchema.StopReason.REFUSAL); |
| 149 | + } |
| 150 | + |
| 151 | + abstract static class GenericAgent<R> { |
| 152 | + |
| 153 | + @Prompt |
| 154 | + abstract PromptResponse prompt(R request); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + /** The override compiles to a bridge prompt(Object) that carries the annotation too. */ |
| 159 | + @AcpAgent |
| 160 | + static class TypedAgent extends GenericAgent<PromptRequest> { |
| 161 | + |
| 162 | + @NewSession |
| 163 | + NewSessionResponse newSession() { |
| 164 | + return new NewSessionResponse("s1", null, null); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + @Prompt |
| 169 | + PromptResponse prompt(PromptRequest request) { |
| 170 | + return new PromptResponse(request.sessionId().equals("s1") ? AcpSchema.StopReason.END_TURN |
| 171 | + : AcpSchema.StopReason.REFUSAL); |
| 172 | + } |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + void aBridgeMethodIsNotAHandler() { |
| 178 | + assertThat(session(AcpAgentSupport.create(new TypedAgent()), AgentDiscoveryTest::prompt)) |
| 179 | + .isEqualTo(AcpSchema.StopReason.END_TURN); |
| 180 | + } |
| 181 | + |
| 182 | + static class NotAnAgent { |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + void aClassWithNoAcpAgentInItsHierarchyIsRejectedNamingIt() { |
| 188 | + assertThatThrownBy(() -> AcpAgentSupport.create(new NotAnAgent())) |
| 189 | + .isInstanceOf(IllegalArgumentException.class) |
| 190 | + .hasMessageContaining(NotAnAgent.class.getName()) |
| 191 | + .hasMessageContaining("@AcpAgent"); |
| 192 | + } |
| 193 | + |
| 194 | + private static AcpSchema.StopReason prompt(AcpAsyncClient client) { |
| 195 | + client.newSession(new AcpSchema.NewSessionRequest("/w", List.of())).block(TIMEOUT); |
| 196 | + return client.prompt(new PromptRequest("s1", List.of(new AcpSchema.TextContent("hi")))) |
| 197 | + .block(TIMEOUT) |
| 198 | + .stopReason(); |
| 199 | + } |
| 200 | + |
| 201 | + private static <T> T session(AcpAgentSupport.Builder builder, Function<AcpAsyncClient, T> scenario) { |
| 202 | + InMemoryTransportPair pair = InMemoryTransportPair.create(); |
| 203 | + AcpAgentSupport agent = builder.transport(pair.agentTransport()).requestTimeout(TIMEOUT).build(); |
| 204 | + agent.start(); |
| 205 | + AcpAsyncClient client = AcpClient.async(pair.clientTransport()).requestTimeout(TIMEOUT).build(); |
| 206 | + try { |
| 207 | + client.initialize().block(TIMEOUT); |
| 208 | + return scenario.apply(client); |
| 209 | + } |
| 210 | + finally { |
| 211 | + client.closeGracefully().block(TIMEOUT); |
| 212 | + agent.close(); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | +} |
0 commit comments