forked from agentclientprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_agent.py
More file actions
51 lines (39 loc) · 1.48 KB
/
echo_agent.py
File metadata and controls
51 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import asyncio
from uuid import uuid4
from acp import (
Agent,
AgentSideConnection,
InitializeRequest,
InitializeResponse,
NewSessionRequest,
NewSessionResponse,
PromptRequest,
PromptResponse,
session_notification,
stdio_streams,
text_block,
update_agent_message,
)
class EchoAgent(Agent):
def __init__(self, conn):
self._conn = conn
async def initialize(self, params: InitializeRequest) -> InitializeResponse:
return InitializeResponse(protocol_version=params.protocol_version)
async def newSession(self, params: NewSessionRequest) -> NewSessionResponse:
return NewSessionResponse(session_id=uuid4().hex)
async def prompt(self, params: PromptRequest) -> PromptResponse:
for block in params.prompt:
text = block.get("text", "") if isinstance(block, dict) else getattr(block, "text", "")
chunk = update_agent_message(text_block(text))
chunk.field_meta = {"echo": True}
chunk.content.field_meta = {"echo": True}
notification = session_notification(params.session_id, chunk)
notification.field_meta = {"source": "echo_agent"}
await self._conn.sessionUpdate(notification)
return PromptResponse(stop_reason="end_turn")
async def main() -> None:
reader, writer = await stdio_streams()
AgentSideConnection(EchoAgent, writer, reader)
await asyncio.Event().wait()
if __name__ == "__main__":
asyncio.run(main())