|
6 | 6 |
|
7 | 7 | import java.time.Duration; |
8 | 8 | import java.util.List; |
| 9 | +import java.util.Map; |
9 | 10 | import java.util.concurrent.CopyOnWriteArrayList; |
10 | 11 | import java.util.concurrent.atomic.AtomicReference; |
11 | 12 |
|
12 | 13 | import com.agentclientprotocol.sdk.client.AcpClient; |
13 | 14 | import com.agentclientprotocol.sdk.client.AcpSyncClient; |
| 15 | +import com.agentclientprotocol.sdk.spec.AcpAgentTransport; |
14 | 16 | import com.agentclientprotocol.sdk.spec.AcpSchema; |
15 | 17 | import com.agentclientprotocol.sdk.test.InMemoryTransportPair; |
16 | 18 | import org.junit.jupiter.api.AfterEach; |
@@ -102,6 +104,60 @@ void syncHandlerReceivesTheBuiltAgentAndItsReplayPrecedesTheAnswer() { |
102 | 104 | } |
103 | 105 | } |
104 | 106 |
|
| 107 | + /** |
| 108 | + * The example on {@link AgentAwareHandler}, as written there: the built agent is assigned to |
| 109 | + * {@code agent}, so the handler's agent parameter is {@code self}. Named {@code agent} it would |
| 110 | + * not compile ("variable agent is already defined"). |
| 111 | + */ |
| 112 | + @Test |
| 113 | + void asyncJavadocExampleAssignsTheAgentAndNamesTheParameterSelf() { |
| 114 | + List<AcpSchema.SessionConfigOption> options = List.of(); |
| 115 | + AcpAgentTransport transport = pair.agentTransport(); |
| 116 | + AcpAsyncAgent agent = AcpAgent.async(transport) |
| 117 | + .promptHandler((request, context) -> Mono.just(AcpSchema.PromptResponse.endTurn())) |
| 118 | + .setSessionConfigOptionHandler((request, self) -> self |
| 119 | + .sendSessionUpdate(request.sessionId(), new AcpSchema.ConfigOptionUpdate(options)) |
| 120 | + .thenReturn(new AcpSchema.SetSessionConfigOptionResponse(options))) |
| 121 | + .build(); |
| 122 | + agent.start().block(TIMEOUT); |
| 123 | + try (AcpSyncClient client = client()) { |
| 124 | + assertThat(client.setSessionConfigOption(AcpSchema.SetSessionConfigOptionRequest.select("s4", "model", "fast")) |
| 125 | + .configOptions()).isEmpty(); |
| 126 | + } |
| 127 | + finally { |
| 128 | + agent.close(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * The example on {@link SyncAgentAwareHandler}, as written there: the built agent is assigned |
| 134 | + * to {@code agent}, so the handler's agent parameter is {@code self}. |
| 135 | + */ |
| 136 | + @Test |
| 137 | + void syncJavadocExampleAssignsTheAgentAndNamesTheParameterSelf() { |
| 138 | + Map<String, List<AcpSchema.SessionUpdate>> history = Map.of("s5", |
| 139 | + List.of(new AcpSchema.AgentMessageChunk(new AcpSchema.TextContent("replayed")))); |
| 140 | + AcpAgentTransport transport = pair.agentTransport(); |
| 141 | + AcpSyncAgent agent = AcpAgent.sync(transport) |
| 142 | + .promptHandler((request, context) -> AcpSchema.PromptResponse.endTurn()) |
| 143 | + .loadSessionHandler((request, self) -> { |
| 144 | + for (AcpSchema.SessionUpdate update : history.get(request.sessionId())) { |
| 145 | + self.sendSessionUpdate(request.sessionId(), update); // the replay |
| 146 | + } |
| 147 | + return new AcpSchema.LoadSessionResponse(null, null); |
| 148 | + }) |
| 149 | + .build(); |
| 150 | + agent.start(); |
| 151 | + try (AcpSyncClient client = client()) { |
| 152 | + client.loadSession(new AcpSchema.LoadSessionRequest("s5", "/w", List.of())); |
| 153 | + |
| 154 | + assertThat(updates).containsExactly("s5:replayed"); |
| 155 | + } |
| 156 | + finally { |
| 157 | + agent.close(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
105 | 161 | @Test |
106 | 162 | void oneArgumentHandlersStillPickTheirOwnInterface() { |
107 | 163 | AcpSyncAgent agent = AcpAgent.sync(pair.agentTransport()) |
|
0 commit comments