forked from agentclientprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_compatibility.py
More file actions
207 lines (164 loc) · 7.83 KB
/
Copy pathtest_compatibility.py
File metadata and controls
207 lines (164 loc) · 7.83 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import pytest
from acp import (
AuthenticateResponse,
InitializeResponse,
LoadSessionResponse,
NewSessionResponse,
PromptRequest,
PromptResponse,
ReadTextFileResponse,
RequestError,
RequestPermissionResponse,
SessionNotification,
SetSessionConfigOptionResponse,
SetSessionModeResponse,
WriteTextFileResponse,
)
from acp.schema import (
AllowedOutcome,
AuthenticateRequest,
CancelNotification,
DeniedOutcome,
InitializeRequest,
LoadSessionRequest,
NewSessionRequest,
ReadTextFileRequest,
RequestPermissionRequest,
SetSessionConfigOptionBooleanRequest,
SetSessionConfigOptionSelectRequest,
SetSessionModeRequest,
WriteTextFileRequest,
)
class LegacyAgent:
def __init__(self) -> None:
self.prompts: list[PromptRequest] = []
self.config_option_requests: list[
SetSessionConfigOptionBooleanRequest | SetSessionConfigOptionSelectRequest
] = []
self.cancellations: list[str] = []
self.ext_calls: list[tuple[str, dict]] = []
self.ext_notes: list[tuple[str, dict]] = []
async def initialize(self, params: InitializeRequest) -> InitializeResponse:
# Avoid serializer warnings by omitting defaults
return InitializeResponse(protocol_version=params.protocol_version, agent_capabilities=None, auth_methods=[])
async def newSession(self, params: NewSessionRequest) -> NewSessionResponse:
return NewSessionResponse(session_id="test-session-123")
async def loadSession(self, params: LoadSessionRequest) -> LoadSessionResponse | None:
return LoadSessionResponse()
async def authenticate(self, params: AuthenticateRequest) -> AuthenticateResponse | None:
return AuthenticateResponse()
async def prompt(self, params: PromptRequest) -> PromptResponse:
self.prompts.append(params)
return PromptResponse(stop_reason="end_turn")
async def cancel(self, params: CancelNotification) -> None:
self.cancellations.append(params.session_id)
async def setSessionMode(self, params: SetSessionModeRequest) -> SetSessionModeResponse | None:
return SetSessionModeResponse()
async def setConfigOption(
self, params: SetSessionConfigOptionBooleanRequest | SetSessionConfigOptionSelectRequest
) -> SetSessionConfigOptionResponse | None:
self.config_option_requests.append(params)
return SetSessionConfigOptionResponse(config_options=[])
async def extMethod(self, method: str, params: dict) -> dict:
self.ext_calls.append((method, params))
if method == "example.com/echo":
return {"echo": params}
raise RequestError.method_not_found(method)
async def extNotification(self, method: str, params: dict) -> None:
self.ext_notes.append((method, params))
class LegacyClient:
__test__ = False # prevent pytest from collecting this class
def __init__(self) -> None:
self.permission_outcomes: list[RequestPermissionResponse] = []
self.files: dict[str, str] = {}
self.notifications: list[SessionNotification] = []
self.ext_calls: list[tuple[str, dict]] = []
self.ext_notes: list[tuple[str, dict]] = []
def queue_permission_cancelled(self) -> None:
self.permission_outcomes.append(RequestPermissionResponse(outcome=DeniedOutcome(outcome="cancelled")))
def queue_permission_selected(self, option_id: str) -> None:
self.permission_outcomes.append(
RequestPermissionResponse(outcome=AllowedOutcome(option_id=option_id, outcome="selected"))
)
async def requestPermission(self, params: RequestPermissionRequest) -> RequestPermissionResponse:
if self.permission_outcomes:
return self.permission_outcomes.pop()
return RequestPermissionResponse(outcome=DeniedOutcome(outcome="cancelled"))
async def writeTextFile(self, params: WriteTextFileRequest) -> WriteTextFileResponse:
self.files[str(params.path)] = params.content
return WriteTextFileResponse()
async def readTextFile(self, params: ReadTextFileRequest) -> ReadTextFileResponse:
content = self.files.get(str(params.path), "default content")
return ReadTextFileResponse(content=content)
async def sessionUpdate(self, params: SessionNotification) -> None:
self.notifications.append(params)
# Optional terminal methods (not implemented in this test client)
async def createTerminal(self, params): # pragma: no cover - placeholder
raise NotImplementedError
async def terminalOutput(self, params): # pragma: no cover - placeholder
raise NotImplementedError
async def releaseTerminal(self, params): # pragma: no cover - placeholder
raise NotImplementedError
async def waitForTerminalExit(self, params): # pragma: no cover - placeholder
raise NotImplementedError
async def killTerminal(self, params): # pragma: no cover - placeholder
raise NotImplementedError
async def extMethod(self, method: str, params: dict) -> dict:
self.ext_calls.append((method, params))
if method == "example.com/ping":
return {"response": "pong", "params": params}
raise RequestError.method_not_found(method)
async def extNotification(self, method: str, params: dict) -> None:
self.ext_notes.append((method, params))
@pytest.mark.asyncio
@pytest.mark.parametrize("agent,client", [(LegacyAgent(), LegacyClient())])
async def test_initialize_and_new_session_compat(connect, client):
client_conn, agent_conn = connect()
with pytest.warns(DeprecationWarning) as record:
resp = await agent_conn.newSession(NewSessionRequest(cwd="/home/tmp", mcp_servers=[]))
assert len(record) == 2
assert "Calling new_session with NewSessionRequest parameter is deprecated" in str(record[0].message)
assert "The old style method LegacyAgent.newSession is deprecated" in str(record[1].message)
assert isinstance(resp, NewSessionResponse)
assert resp.session_id == "test-session-123"
with pytest.warns(DeprecationWarning) as record:
resp = await agent_conn.new_session(cwd="/home/tmp", mcp_servers=[])
assert len(record) == 1
assert "The old style method LegacyAgent.newSession is deprecated" in str(record[0].message)
with pytest.warns(DeprecationWarning) as record:
await client_conn.writeTextFile(
WriteTextFileRequest(path="test.txt", content="Hello, World!", session_id="test-session-123")
)
assert len(record) == 2
assert client.files["test.txt"] == "Hello, World!"
with pytest.warns(DeprecationWarning) as record:
resp = await client_conn.read_text_file(path="test.txt", session_id="test-session-123")
assert len(record) == 1
assert resp.content == "Hello, World!"
@pytest.mark.asyncio
@pytest.mark.parametrize("agent,client", [(LegacyAgent(), LegacyClient())])
async def test_set_config_option_boolean_compat(connect, agent):
_, agent_conn = connect()
with pytest.warns(DeprecationWarning) as record:
resp = await agent_conn.setConfigOption(
SetSessionConfigOptionBooleanRequest(
config_id="brave_mode",
session_id="test-session-123",
type="boolean",
value=True,
)
)
assert len(record) == 2
assert "SetSessionConfigOptionBooleanRequest | SetSessionConfigOptionSelectRequest parameter is deprecated" in str(
record[0].message
)
assert "The old style method LegacyAgent.setConfigOption is deprecated" in str(record[1].message)
assert isinstance(resp, SetSessionConfigOptionResponse)
assert agent.config_option_requests == [
SetSessionConfigOptionBooleanRequest(
config_id="brave_mode",
session_id="test-session-123",
type="boolean",
value=True,
)
]