-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathagent.py
More file actions
99 lines (83 loc) · 3.35 KB
/
agent.py
File metadata and controls
99 lines (83 loc) · 3.35 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import asyncio
from typing import Any
from acp import (
Agent,
AgentSideConnection,
AuthenticateRequest,
AuthenticateResponse,
CancelNotification,
InitializeRequest,
InitializeResponse,
NewSessionRequest,
NewSessionResponse,
PromptRequest,
PromptResponse,
SessionNotification,
SetSessionModeRequest,
SetSessionModeResponse,
stdio_streams,
PROTOCOL_VERSION,
)
from acp.schema import TextContentBlock, AgentMessageChunk
class ExampleAgent(Agent):
def __init__(self, conn: AgentSideConnection) -> None:
self._conn = conn
self._next_session_id = 0
async def initialize(self, params: InitializeRequest) -> InitializeResponse:
return InitializeResponse(protocolVersion=PROTOCOL_VERSION, agentCapabilities=None, authMethods=[])
async def authenticate(self, params: AuthenticateRequest) -> AuthenticateResponse | None: # noqa: ARG002
return {}
async def newSession(self, params: NewSessionRequest) -> NewSessionResponse: # noqa: ARG002
session_id = f"sess-{self._next_session_id}"
self._next_session_id += 1
return NewSessionResponse(sessionId=session_id)
async def loadSession(self, params): # type: ignore[override]
return None
async def setSessionMode(self, params: SetSessionModeRequest) -> SetSessionModeResponse | None: # noqa: ARG002
return {}
async def prompt(self, params: PromptRequest) -> PromptResponse:
# Stream a couple of agent message chunks, then end the turn
# 1) Prefix
await self._conn.sessionUpdate(
SessionNotification(
sessionId=params.sessionId,
update=AgentMessageChunk(
sessionUpdate="agent_message_chunk",
content=TextContentBlock(type="text", text="Client sent: "),
),
)
)
# 2) Echo text blocks
for block in params.prompt:
if isinstance(block, dict):
# tolerate raw dicts
if block.get("type") == "text":
text = str(block.get("text", ""))
else:
text = f"<{block.get('type', 'content')}>"
else:
# pydantic model TextContentBlock
text = getattr(block, "text", "<content>")
await self._conn.sessionUpdate(
SessionNotification(
sessionId=params.sessionId,
update=AgentMessageChunk(
sessionUpdate="agent_message_chunk",
content=TextContentBlock(type="text", text=text),
),
)
)
return PromptResponse(stopReason="end_turn")
async def cancel(self, params: CancelNotification) -> None: # noqa: ARG002
return None
async def extMethod(self, method: str, params: dict) -> dict: # noqa: ARG002
return {"example": "response"}
async def extNotification(self, method: str, params: dict) -> None: # noqa: ARG002
return None
async def main() -> None:
reader, writer = await stdio_streams()
# For an agent process, local writes go to client stdin (writer=stdout)
AgentSideConnection(lambda conn: ExampleAgent(conn), writer, reader)
await asyncio.Event().wait()
if __name__ == "__main__":
asyncio.run(main())