Skip to content

Commit 273109d

Browse files
committed
chore: polish echo agent
1 parent 12a8407 commit 273109d

File tree

3 files changed

+77
-29
lines changed

3 files changed

+77
-29
lines changed

README.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,53 @@ make test # run tests
2525
## Minimal agent example
2626

2727
```python
28-
# agent_main.py
2928
import asyncio
30-
from acp import Agent, AgentSideConnection, Client, InitializeRequest, InitializeResponse, PromptRequest, PromptResponse, SessionNotification, stdio_streams, PROTOCOL_VERSION
31-
from acp.schema import ContentBlock1, SessionUpdate2
29+
30+
from acp import (
31+
Agent,
32+
AgentSideConnection,
33+
AuthenticateRequest,
34+
CancelNotification,
35+
InitializeRequest,
36+
InitializeResponse,
37+
LoadSessionRequest,
38+
NewSessionRequest,
39+
NewSessionResponse,
40+
PromptRequest,
41+
PromptResponse,
42+
stdio_streams,
43+
)
44+
3245

3346
class EchoAgent(Agent):
34-
def __init__(self, client: Client) -> None:
35-
self.client = client
36-
37-
async def initialize(self, _p: InitializeRequest) -> InitializeResponse:
38-
return InitializeResponse(protocolVersion=PROTOCOL_VERSION)
39-
40-
async def prompt(self, p: PromptRequest) -> PromptResponse:
41-
text = "".join([getattr(b, "text", "") for b in p.prompt if getattr(b, "type", None) == "text"]) or "(empty)"
42-
await self.client.sessionUpdate(SessionNotification(
43-
sessionId=p.sessionId,
44-
update=SessionUpdate2(sessionUpdate="agent_message_chunk", content=ContentBlock1(type="text", text=f"Echo: {text}")),
45-
))
47+
async def initialize(self, params: InitializeRequest) -> InitializeResponse:
48+
return InitializeResponse(protocolVersion=params.protocolVersion)
49+
50+
async def newSession(self, params: NewSessionRequest) -> NewSessionResponse:
51+
return NewSessionResponse(sessionId="sess-1")
52+
53+
async def loadSession(self, params: LoadSessionRequest) -> None:
54+
return None
55+
56+
async def authenticate(self, params: AuthenticateRequest) -> None:
57+
return None
58+
59+
async def prompt(self, params: PromptRequest) -> PromptResponse:
60+
# Normally you'd stream updates via sessionUpdate
4661
return PromptResponse(stopReason="end_turn")
4762

63+
async def cancel(self, params: CancelNotification) -> None:
64+
return None
65+
66+
4867
async def main() -> None:
4968
reader, writer = await stdio_streams()
50-
AgentSideConnection(lambda c: EchoAgent(c), writer, reader)
69+
# For an agent process, local writes go to client stdin (writer=stdout)
70+
AgentSideConnection(lambda _conn: EchoAgent(), writer, reader)
71+
# Keep running; in a real agent you would await tasks or add your own loop
5172
await asyncio.Event().wait()
5273

74+
5375
if __name__ == "__main__":
5476
asyncio.run(main())
5577
```

docs/quickstart.md

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,52 @@ pip install agent-client-protocol
1212

1313
```python
1414
import asyncio
15-
from acp import Agent, AgentSideConnection, Client, InitializeRequest, InitializeResponse, PromptRequest, PromptResponse, SessionNotification, stdio_streams, PROTOCOL_VERSION
16-
from acp.schema import ContentBlock1, SessionUpdate2
15+
16+
from acp import (
17+
Agent,
18+
AgentSideConnection,
19+
AuthenticateRequest,
20+
CancelNotification,
21+
InitializeRequest,
22+
InitializeResponse,
23+
LoadSessionRequest,
24+
NewSessionRequest,
25+
NewSessionResponse,
26+
PromptRequest,
27+
PromptResponse,
28+
stdio_streams,
29+
)
30+
1731

1832
class EchoAgent(Agent):
19-
def __init__(self, client: Client):
20-
self.client = client
21-
async def initialize(self, _p: InitializeRequest) -> InitializeResponse:
22-
return InitializeResponse(protocolVersion=PROTOCOL_VERSION)
23-
async def prompt(self, p: PromptRequest) -> PromptResponse:
24-
await self.client.sessionUpdate(SessionNotification(
25-
sessionId=p.sessionId,
26-
update=SessionUpdate2(sessionUpdate="agent_message_chunk", content=ContentBlock1(type="text", text="Hello from ACP")),
27-
))
33+
async def initialize(self, params: InitializeRequest) -> InitializeResponse:
34+
return InitializeResponse(protocolVersion=params.protocolVersion)
35+
36+
async def newSession(self, params: NewSessionRequest) -> NewSessionResponse:
37+
return NewSessionResponse(sessionId="sess-1")
38+
39+
async def loadSession(self, params: LoadSessionRequest) -> None:
40+
return None
41+
42+
async def authenticate(self, params: AuthenticateRequest) -> None:
43+
return None
44+
45+
async def prompt(self, params: PromptRequest) -> PromptResponse:
46+
# Normally you'd stream updates via sessionUpdate
2847
return PromptResponse(stopReason="end_turn")
2948

49+
async def cancel(self, params: CancelNotification) -> None:
50+
return None
51+
52+
3053
async def main() -> None:
3154
reader, writer = await stdio_streams()
32-
AgentSideConnection(lambda c: EchoAgent(c), writer, reader)
55+
# For an agent process, local writes go to client stdin (writer=stdout)
56+
AgentSideConnection(lambda _conn: EchoAgent(), writer, reader)
57+
# Keep running; in a real agent you would await tasks or add your own loop
3358
await asyncio.Event().wait()
3459

60+
3561
if __name__ == "__main__":
3662
asyncio.run(main())
3763
```

examples/agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class EchoAgent(Agent):
2020
async def initialize(self, params: InitializeRequest) -> InitializeResponse:
2121
# Avoid serializer warnings by omitting defaults
22-
return InitializeResponse(protocolVersion=params.protocolVersion, agentCapabilities=None, authMethods=[])
22+
return InitializeResponse(protocolVersion=params.protocolVersion)
2323

2424
async def newSession(self, params: NewSessionRequest) -> NewSessionResponse:
2525
return NewSessionResponse(sessionId="sess-1")

0 commit comments

Comments
 (0)