Skip to content

Commit 96dbfbf

Browse files
author
Mark Pollack
committed
Agent-aware handler examples name the agent parameter self
AcpSyncAgent agent = AcpAgent.sync(t).newSessionHandler((request, agent) -> ...) does not compile: the lambda parameter shadows the local the built agent is assigned to. The Javadoc and CHANGELOG examples now assign the built agent and name the parameter self, the handler Javadoc says why, and AgentAwareHandlerTest compiles and runs both examples as written.
1 parent e6c73cf commit 96dbfbf

5 files changed

Lines changed: 78 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
127127
two-argument lambda picks it. The agent is the one `build()` returned, so a `session/load`
128128
replay or a `ConfigOptionUpdate` after `session/set_config_option` no longer needs an
129129
`AtomicReference` holding the built agent:
130-
`.setSessionConfigOptionHandler((request, agent) -> { agent.sendSessionUpdate(request.sessionId(), update); return response; })`.
130+
`.setSessionConfigOptionHandler((request, self) -> { self.sendSessionUpdate(request.sessionId(), update); return response; })`.
131131
One-argument handlers are unchanged. The two interfaces are top-level types in
132132
`com.agentclientprotocol.sdk.agent`. Migration: none; a handler that read the built agent from
133133
an `AtomicReference` can take it as its second parameter instead:
134-
`.loadSessionHandler(request -> agentRef.get()...)` → `.loadSessionHandler((request, agent) -> agent...)`.
134+
`.loadSessionHandler(request -> agentRef.get()...)` → `.loadSessionHandler((request, self) -> self...)`.
135+
Name the agent parameter `self` (or anything but the variable the built agent is assigned to):
136+
`AcpSyncAgent agent = AcpAgent.sync(t).newSessionHandler((request, agent) -> ...)` does not
137+
compile, since a lambda parameter cannot shadow a local variable.
135138

136139
- **Capability builders that lead to each choice.** `ClientCapabilities.builder()` gains
137140
`readTextFile()`, `writeTextFile()`, `terminal()`, `elicitationForm()` and `elicitationUrl()`;

‎acp-core/src/main/java/com/agentclientprotocol/sdk/agent/AcpSyncAgent.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
*
2626
* <pre>{@code
2727
* AcpAgent.sync(transport)
28-
* .setSessionConfigOptionHandler((request, agent) -> {
29-
* agent.sendSessionUpdate(request.sessionId(),
28+
* .setSessionConfigOptionHandler((request, self) -> {
29+
* self.sendSessionUpdate(request.sessionId(),
3030
* new AcpSchema.ConfigOptionUpdate(options));
3131
* return new AcpSchema.SetSessionConfigOptionResponse(options);
3232
* })

‎acp-core/src/main/java/com/agentclientprotocol/sdk/agent/AgentAwareHandler.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
* interface; a two-argument lambda picks it.
1818
*
1919
* <pre>{@code
20-
* AcpAgent.async(transport)
21-
* .setSessionConfigOptionHandler((request, agent) -> agent
20+
* AcpAsyncAgent agent = AcpAgent.async(transport)
21+
* .setSessionConfigOptionHandler((request, self) -> self
2222
* .sendSessionUpdate(request.sessionId(), new AcpSchema.ConfigOptionUpdate(options))
2323
* .thenReturn(new AcpSchema.SetSessionConfigOptionResponse(options)))
24+
* .build();
2425
* }</pre>
2526
*
27+
* <p>The second parameter is the agent being built, the same instance {@code build()} returns; a
28+
* lambda parameter must not reuse the name of a local variable in scope, so call it {@code self}
29+
* (or anything but the name the built agent is assigned to), as above.
30+
*
2631
* <p>Implementations follow the rules on {@link AcpAgent.PromptHandler}: they run on the transport's
2732
* thread and must not block, return a {@code Mono} and never {@code null}, and fail with an
2833
* {@link com.agentclientprotocol.sdk.error.AcpProtocolException} to answer with a chosen error.

‎acp-core/src/main/java/com/agentclientprotocol/sdk/agent/SyncAgentAwareHandler.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@
1414
* overload that takes one; a two-argument lambda picks it.
1515
*
1616
* <pre>{@code
17-
* AcpAgent.sync(transport)
18-
* .loadSessionHandler((request, agent) -> {
17+
* AcpSyncAgent agent = AcpAgent.sync(transport)
18+
* .loadSessionHandler((request, self) -> {
1919
* for (AcpSchema.SessionUpdate update : history.get(request.sessionId())) {
20-
* agent.sendSessionUpdate(request.sessionId(), update); // the replay
20+
* self.sendSessionUpdate(request.sessionId(), update); // the replay
2121
* }
2222
* return new AcpSchema.LoadSessionResponse(null, null);
2323
* })
24+
* .build();
2425
* }</pre>
2526
*
27+
* <p>The second parameter is the agent being built, the same instance {@code build()} returns; a
28+
* lambda parameter must not reuse the name of a local variable in scope, so call it {@code self}
29+
* (or anything but the name the built agent is assigned to), as above.
30+
*
2631
* <p>Implementations run on the builder's handler executor and may block.
2732
* @param <Q> the request type
2833
* @param <R> the response type

‎acp-core/src/test/java/com/agentclientprotocol/sdk/agent/AgentAwareHandlerTest.java‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
import java.time.Duration;
88
import java.util.List;
9+
import java.util.Map;
910
import java.util.concurrent.CopyOnWriteArrayList;
1011
import java.util.concurrent.atomic.AtomicReference;
1112

1213
import com.agentclientprotocol.sdk.client.AcpClient;
1314
import com.agentclientprotocol.sdk.client.AcpSyncClient;
15+
import com.agentclientprotocol.sdk.spec.AcpAgentTransport;
1416
import com.agentclientprotocol.sdk.spec.AcpSchema;
1517
import com.agentclientprotocol.sdk.test.InMemoryTransportPair;
1618
import org.junit.jupiter.api.AfterEach;
@@ -102,6 +104,60 @@ void syncHandlerReceivesTheBuiltAgentAndItsReplayPrecedesTheAnswer() {
102104
}
103105
}
104106

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+
105161
@Test
106162
void oneArgumentHandlersStillPickTheirOwnInterface() {
107163
AcpSyncAgent agent = AcpAgent.sync(pair.agentTransport())

0 commit comments

Comments
 (0)