Skip to content

Commit cc0b62a

Browse files
committed
Use convenience methods in README code examples
- Client example: add sessionUpdateConsumer for visible output - Sync/async agent examples: use InitializeResponse.ok(), context.sendMessage(), PromptResponse.endTurn() - Streaming updates: use context.sendThought(), context.sendMessage() - Agent requests: use context.readFile(), context.writeFile()
1 parent a4cdc59 commit cc0b62a

File tree

1 file changed

+35
-54
lines changed

1 file changed

+35
-54
lines changed

README.md

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,30 @@ import com.agentclientprotocol.sdk.client.transport.*;
6868
import com.agentclientprotocol.sdk.spec.AcpSchema.*;
6969
import java.util.List;
7070

71-
// Connect to an agent via stdio
71+
// Launch Gemini CLI as an ACP agent subprocess
7272
var params = AgentParameters.builder("gemini").arg("--experimental-acp").build();
7373
var transport = new StdioAcpClientTransport(params);
7474

75-
// Create client
76-
AcpSyncClient client = AcpClient.sync(transport).build();
75+
// Create client — sessionUpdateConsumer prints the agent's streamed response
76+
AcpSyncClient client = AcpClient.sync(transport)
77+
.sessionUpdateConsumer(notification -> {
78+
if (notification.update() instanceof AgentMessageChunk msg) {
79+
System.out.print(((TextContent) msg.content()).text());
80+
}
81+
})
82+
.build();
7783

78-
// Initialize, create session, send prompt
84+
// Three-phase lifecycle: initialize → session prompt
7985
client.initialize();
8086
var session = client.newSession(new NewSessionRequest("/workspace", List.of()));
8187
var response = client.prompt(new PromptRequest(
8288
session.sessionId(),
83-
List.of(new TextContent("Hello, world!"))
89+
List.of(new TextContent("What is 2+2? Reply with just the number."))
8490
));
91+
// Output: 4
92+
// Stop reason: END_TURN
8593

94+
System.out.println("\nStop reason: " + response.stopReason());
8695
client.close();
8796
```
8897

@@ -130,30 +139,22 @@ The builder API with blocking handlers and plain return values ([tutorial](https
130139
import com.agentclientprotocol.sdk.agent.*;
131140
import com.agentclientprotocol.sdk.agent.transport.*;
132141
import com.agentclientprotocol.sdk.spec.AcpSchema.*;
133-
import java.util.List;
134142
import java.util.UUID;
135143

136-
// Create stdio transport
137144
var transport = new StdioAcpAgentTransport();
138145

139-
// Build sync agent - handlers use plain return values (no Mono!)
146+
// Sync agent plain return values, no Mono
140147
AcpSyncAgent agent = AcpAgent.sync(transport)
141-
.initializeHandler(req ->
142-
new InitializeResponse(1, new AgentCapabilities(), List.of()))
148+
.initializeHandler(req -> InitializeResponse.ok())
143149
.newSessionHandler(req ->
144150
new NewSessionResponse(UUID.randomUUID().toString(), null, null))
145151
.promptHandler((req, context) -> {
146-
// Send updates using blocking void method
147-
context.sendUpdate(req.sessionId(),
148-
new AgentMessageChunk("agent_message_chunk",
149-
new TextContent("Hello from the agent!")));
150-
// Return response directly (no Mono!)
151-
return new PromptResponse(StopReason.END_TURN);
152+
context.sendMessage("Hello from the agent!"); // blocking void method
153+
return PromptResponse.endTurn();
152154
})
153155
.build();
154156

155-
// Run agent (blocks until client disconnects)
156-
agent.run();
157+
agent.run(); // Blocks until client disconnects
157158
```
158159

159160
### 4. Hello World Agent (Async)
@@ -165,24 +166,20 @@ import com.agentclientprotocol.sdk.agent.*;
165166
import com.agentclientprotocol.sdk.agent.transport.*;
166167
import com.agentclientprotocol.sdk.spec.AcpSchema.*;
167168
import reactor.core.publisher.Mono;
168-
import java.util.List;
169169
import java.util.UUID;
170170

171171
var transport = new StdioAcpAgentTransport();
172172

173+
// Async agent — handlers return Mono
173174
AcpAsyncAgent agent = AcpAgent.async(transport)
174-
.initializeHandler(req -> Mono.just(
175-
new InitializeResponse(1, new AgentCapabilities(), List.of())))
175+
.initializeHandler(req -> Mono.just(InitializeResponse.ok()))
176176
.newSessionHandler(req -> Mono.just(
177177
new NewSessionResponse(UUID.randomUUID().toString(), null, null)))
178178
.promptHandler((req, context) ->
179-
context.sendUpdate(req.sessionId(),
180-
new AgentMessageChunk("agent_message_chunk",
181-
new TextContent("Hello from the agent!")))
182-
.then(Mono.just(new PromptResponse(StopReason.END_TURN))))
179+
context.sendMessage("Hello from the agent!")
180+
.then(Mono.just(PromptResponse.endTurn())))
183181
.build();
184182

185-
// Start and await termination
186183
agent.start().then(agent.awaitTermination()).block();
187184
```
188185

@@ -198,8 +195,7 @@ Send real-time updates to the client during prompt processing (tutorial: [client
198195
```java
199196
@Prompt
200197
PromptResponse prompt(PromptRequest req, SyncPromptContext ctx) {
201-
ctx.sendUpdate(req.sessionId(),
202-
new AgentThoughtChunk("agent_thought_chunk", new TextContent("Thinking...")));
198+
ctx.sendThought("Thinking...");
203199
ctx.sendMessage("Here's my response.");
204200
return PromptResponse.endTurn();
205201
}
@@ -208,27 +204,18 @@ PromptResponse prompt(PromptRequest req, SyncPromptContext ctx) {
208204
**Sync:**
209205
```java
210206
.promptHandler((req, context) -> {
211-
context.sendUpdate(req.sessionId(),
212-
new AgentThoughtChunk("agent_thought_chunk",
213-
new TextContent("Thinking...")));
214-
context.sendUpdate(req.sessionId(),
215-
new AgentMessageChunk("agent_message_chunk",
216-
new TextContent("Here's my response.")));
217-
return new PromptResponse(StopReason.END_TURN);
207+
context.sendThought("Thinking...");
208+
context.sendMessage("Here's my response.");
209+
return PromptResponse.endTurn();
218210
})
219211
```
220212

221213
**Async:**
222214
```java
223-
.promptHandler((request, context) -> {
224-
return context.sendUpdate(request.sessionId(),
225-
new AgentThoughtChunk("agent_thought_chunk",
226-
new TextContent("Thinking...")))
227-
.then(context.sendUpdate(request.sessionId(),
228-
new AgentMessageChunk("agent_message_chunk",
229-
new TextContent("Here's my response."))))
230-
.then(Mono.just(new PromptResponse(StopReason.END_TURN)));
231-
})
215+
.promptHandler((req, context) ->
216+
context.sendThought("Thinking...")
217+
.then(context.sendMessage("Here's my response."))
218+
.then(Mono.just(PromptResponse.endTurn())))
232219
```
233220

234221
**Client - receiving updates:**
@@ -251,16 +238,10 @@ Agents can request file operations from the client ([tutorial](https://github.co
251238
```java
252239
AcpSyncAgent agent = AcpAgent.sync(transport)
253240
.promptHandler((req, context) -> {
254-
// Read a file from the client's filesystem
255-
var fileResponse = context.readTextFile(
256-
new ReadTextFileRequest(req.sessionId(), "pom.xml", null, 10));
257-
String content = fileResponse.content();
258-
259-
// Write a file
260-
context.writeTextFile(
261-
new WriteTextFileRequest(req.sessionId(), "output.txt", "Hello!"));
262-
263-
return new PromptResponse(StopReason.END_TURN);
241+
// Convenience methods on SyncPromptContext
242+
String content = context.readFile("pom.xml");
243+
context.writeFile("output.txt", "Hello!");
244+
return PromptResponse.endTurn();
264245
})
265246
.build();
266247

0 commit comments

Comments
 (0)