Debian Codegraph @

tests/api_resources/beta/sessions/test_events.py

raw
1 from __future__ import annotations
2
3 import os
4 from typing import Any, cast
5
6 import pytest
7
8 from anthropic import Anthropic, AsyncAnthropic
9 from tests.utils import assert_matches_type
10 from anthropic._utils import parse_datetime
11 from anthropic.pagination import SyncPageCursor, AsyncPageCursor
12 from anthropic.types.beta.sessions import (
13 BetaManagedAgentsSessionEvent,
14 BetaManagedAgentsSendSessionEvents,
15 )
16
17 base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
18
19
20 class TestEvents:
21 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
22
23 @pytest.mark.skip(reason="buildURL drops path-level query params")
24 @parametrize
25 def test_method_list(self, client: Anthropic) -> None:
26 event = client.beta.sessions.events.list(
27 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
28 )
29 assert_matches_type(SyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"])
30
31 @pytest.mark.skip(reason="buildURL drops path-level query params")
32 @parametrize
33 def test_method_list_with_all_params(self, client: Anthropic) -> None:
34 event = client.beta.sessions.events.list(
35 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
36 created_at_gt=parse_datetime("2019-12-27T18:11:19.117Z"),
37 created_at_gte=parse_datetime("2019-12-27T18:11:19.117Z"),
38 created_at_lt=parse_datetime("2019-12-27T18:11:19.117Z"),
39 created_at_lte=parse_datetime("2019-12-27T18:11:19.117Z"),
40 limit=0,
41 order="asc",
42 page="page",
43 types=["string"],
44 betas=["message-batches-2024-09-24"],
45 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
46 )
47 assert_matches_type(SyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"])
48
49 @pytest.mark.skip(reason="buildURL drops path-level query params")
50 @parametrize
51 def test_raw_response_list(self, client: Anthropic) -> None:
52 response = client.beta.sessions.events.with_raw_response.list(
53 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
54 )
55
56 assert response.is_closed is True
57 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
58 event = response.parse()
59 assert_matches_type(SyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"])
60
61 @pytest.mark.skip(reason="buildURL drops path-level query params")
62 @parametrize
63 def test_streaming_response_list(self, client: Anthropic) -> None:
64 with client.beta.sessions.events.with_streaming_response.list(
65 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
66 ) as response:
67 assert not response.is_closed
68 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
69
70 event = response.parse()
71 assert_matches_type(SyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"])
72
73 assert cast(Any, response.is_closed) is True
74
75 @pytest.mark.skip(reason="buildURL drops path-level query params")
76 @parametrize
77 def test_path_params_list(self, client: Anthropic) -> None:
78 with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"):
79 client.beta.sessions.events.with_raw_response.list(
80 session_id="",
81 )
82
83 @parametrize
84 def test_method_send(self, client: Anthropic) -> None:
85 event = client.beta.sessions.events.send(
86 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
87 events=[
88 {
89 "content": [
90 {
91 "text": "Where is my order #1234?",
92 "type": "text",
93 }
94 ],
95 "type": "user.message",
96 }
97 ],
98 )
99 assert_matches_type(BetaManagedAgentsSendSessionEvents, event, path=["response"])
100
101 @parametrize
102 def test_method_send_with_all_params(self, client: Anthropic) -> None:
103 event = client.beta.sessions.events.send(
104 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
105 events=[
106 {
107 "content": [
108 {
109 "text": "Where is my order #1234?",
110 "type": "text",
111 }
112 ],
113 "type": "user.message",
114 }
115 ],
116 betas=["message-batches-2024-09-24"],
117 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
118 )
119 assert_matches_type(BetaManagedAgentsSendSessionEvents, event, path=["response"])
120
121 @parametrize
122 def test_raw_response_send(self, client: Anthropic) -> None:
123 response = client.beta.sessions.events.with_raw_response.send(
124 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
125 events=[
126 {
127 "content": [
128 {
129 "text": "Where is my order #1234?",
130 "type": "text",
131 }
132 ],
133 "type": "user.message",
134 }
135 ],
136 )
137
138 assert response.is_closed is True
139 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
140 event = response.parse()
141 assert_matches_type(BetaManagedAgentsSendSessionEvents, event, path=["response"])
142
143 @parametrize
144 def test_streaming_response_send(self, client: Anthropic) -> None:
145 with client.beta.sessions.events.with_streaming_response.send(
146 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
147 events=[
148 {
149 "content": [
150 {
151 "text": "Where is my order #1234?",
152 "type": "text",
153 }
154 ],
155 "type": "user.message",
156 }
157 ],
158 ) as response:
159 assert not response.is_closed
160 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
161
162 event = response.parse()
163 assert_matches_type(BetaManagedAgentsSendSessionEvents, event, path=["response"])
164
165 assert cast(Any, response.is_closed) is True
166
167 @parametrize
168 def test_path_params_send(self, client: Anthropic) -> None:
169 with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"):
170 client.beta.sessions.events.with_raw_response.send(
171 session_id="",
172 events=[
173 {
174 "content": [
175 {
176 "text": "Where is my order #1234?",
177 "type": "text",
178 }
179 ],
180 "type": "user.message",
181 }
182 ],
183 )
184
185 @parametrize
186 def test_method_stream(self, client: Anthropic) -> None:
187 event_stream = client.beta.sessions.events.stream(
188 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
189 )
190 event_stream.response.close()
191
192 @parametrize
193 def test_method_stream_with_all_params(self, client: Anthropic) -> None:
194 event_stream = client.beta.sessions.events.stream(
195 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
196 event_deltas=["agent.message"],
197 betas=["message-batches-2024-09-24"],
198 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
199 )
200 event_stream.response.close()
201
202 @parametrize
203 def test_raw_response_stream(self, client: Anthropic) -> None:
204 response = client.beta.sessions.events.with_raw_response.stream(
205 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
206 )
207
208 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
209 stream = response.parse()
210 stream.close()
211
212 @parametrize
213 def test_streaming_response_stream(self, client: Anthropic) -> None:
214 with client.beta.sessions.events.with_streaming_response.stream(
215 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
216 ) as response:
217 assert not response.is_closed
218 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
219
220 stream = response.parse()
221 stream.close()
222
223 assert cast(Any, response.is_closed) is True
224
225 @parametrize
226 def test_path_params_stream(self, client: Anthropic) -> None:
227 with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"):
228 client.beta.sessions.events.with_raw_response.stream(
229 session_id="",
230 )
231
232
233 class TestAsyncEvents:
234 parametrize = pytest.mark.parametrize(
235 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
236 )
237
238 @pytest.mark.skip(reason="buildURL drops path-level query params")
239 @parametrize
240 async def test_method_list(self, async_client: AsyncAnthropic) -> None:
241 event = await async_client.beta.sessions.events.list(
242 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
243 )
244 assert_matches_type(AsyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"])
245
246 @pytest.mark.skip(reason="buildURL drops path-level query params")
247 @parametrize
248 async def test_method_list_with_all_params(self, async_client: AsyncAnthropic) -> None:
249 event = await async_client.beta.sessions.events.list(
250 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
251 created_at_gt=parse_datetime("2019-12-27T18:11:19.117Z"),
252 created_at_gte=parse_datetime("2019-12-27T18:11:19.117Z"),
253 created_at_lt=parse_datetime("2019-12-27T18:11:19.117Z"),
254 created_at_lte=parse_datetime("2019-12-27T18:11:19.117Z"),
255 limit=0,
256 order="asc",
257 page="page",
258 types=["string"],
259 betas=["message-batches-2024-09-24"],
260 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
261 )
262 assert_matches_type(AsyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"])
263
264 @pytest.mark.skip(reason="buildURL drops path-level query params")
265 @parametrize
266 async def test_raw_response_list(self, async_client: AsyncAnthropic) -> None:
267 response = await async_client.beta.sessions.events.with_raw_response.list(
268 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
269 )
270
271 assert response.is_closed is True
272 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
273 event = await response.parse()
274 assert_matches_type(AsyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"])
275
276 @pytest.mark.skip(reason="buildURL drops path-level query params")
277 @parametrize
278 async def test_streaming_response_list(self, async_client: AsyncAnthropic) -> None:
279 async with async_client.beta.sessions.events.with_streaming_response.list(
280 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
281 ) as response:
282 assert not response.is_closed
283 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
284
285 event = await response.parse()
286 assert_matches_type(AsyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"])
287
288 assert cast(Any, response.is_closed) is True
289
290 @pytest.mark.skip(reason="buildURL drops path-level query params")
291 @parametrize
292 async def test_path_params_list(self, async_client: AsyncAnthropic) -> None:
293 with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"):
294 await async_client.beta.sessions.events.with_raw_response.list(
295 session_id="",
296 )
297
298 @parametrize
299 async def test_method_send(self, async_client: AsyncAnthropic) -> None:
300 event = await async_client.beta.sessions.events.send(
301 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
302 events=[
303 {
304 "content": [
305 {
306 "text": "Where is my order #1234?",
307 "type": "text",
308 }
309 ],
310 "type": "user.message",
311 }
312 ],
313 )
314 assert_matches_type(BetaManagedAgentsSendSessionEvents, event, path=["response"])
315
316 @parametrize
317 async def test_method_send_with_all_params(self, async_client: AsyncAnthropic) -> None:
318 event = await async_client.beta.sessions.events.send(
319 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
320 events=[
321 {
322 "content": [
323 {
324 "text": "Where is my order #1234?",
325 "type": "text",
326 }
327 ],
328 "type": "user.message",
329 }
330 ],
331 betas=["message-batches-2024-09-24"],
332 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
333 )
334 assert_matches_type(BetaManagedAgentsSendSessionEvents, event, path=["response"])
335
336 @parametrize
337 async def test_raw_response_send(self, async_client: AsyncAnthropic) -> None:
338 response = await async_client.beta.sessions.events.with_raw_response.send(
339 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
340 events=[
341 {
342 "content": [
343 {
344 "text": "Where is my order #1234?",
345 "type": "text",
346 }
347 ],
348 "type": "user.message",
349 }
350 ],
351 )
352
353 assert response.is_closed is True
354 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
355 event = await response.parse()
356 assert_matches_type(BetaManagedAgentsSendSessionEvents, event, path=["response"])
357
358 @parametrize
359 async def test_streaming_response_send(self, async_client: AsyncAnthropic) -> None:
360 async with async_client.beta.sessions.events.with_streaming_response.send(
361 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
362 events=[
363 {
364 "content": [
365 {
366 "text": "Where is my order #1234?",
367 "type": "text",
368 }
369 ],
370 "type": "user.message",
371 }
372 ],
373 ) as response:
374 assert not response.is_closed
375 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
376
377 event = await response.parse()
378 assert_matches_type(BetaManagedAgentsSendSessionEvents, event, path=["response"])
379
380 assert cast(Any, response.is_closed) is True
381
382 @parametrize
383 async def test_path_params_send(self, async_client: AsyncAnthropic) -> None:
384 with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"):
385 await async_client.beta.sessions.events.with_raw_response.send(
386 session_id="",
387 events=[
388 {
389 "content": [
390 {
391 "text": "Where is my order #1234?",
392 "type": "text",
393 }
394 ],
395 "type": "user.message",
396 }
397 ],
398 )
399
400 @parametrize
401 async def test_method_stream(self, async_client: AsyncAnthropic) -> None:
402 event_stream = await async_client.beta.sessions.events.stream(
403 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
404 )
405 await event_stream.response.aclose()
406
407 @parametrize
408 async def test_method_stream_with_all_params(self, async_client: AsyncAnthropic) -> None:
409 event_stream = await async_client.beta.sessions.events.stream(
410 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
411 event_deltas=["agent.message"],
412 betas=["message-batches-2024-09-24"],
413 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
414 )
415 await event_stream.response.aclose()
416
417 @parametrize
418 async def test_raw_response_stream(self, async_client: AsyncAnthropic) -> None:
419 response = await async_client.beta.sessions.events.with_raw_response.stream(
420 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
421 )
422
423 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
424 stream = await response.parse()
425 await stream.close()
426
427 @parametrize
428 async def test_streaming_response_stream(self, async_client: AsyncAnthropic) -> None:
429 async with async_client.beta.sessions.events.with_streaming_response.stream(
430 session_id="sesn_011CZkZAtmR3yMPDzynEDxu7",
431 ) as response:
432 assert not response.is_closed
433 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
434
435 stream = await response.parse()
436 await stream.close()
437
438 assert cast(Any, response.is_closed) is True
439
440 @parametrize
441 async def test_path_params_stream(self, async_client: AsyncAnthropic) -> None:
442 with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"):
443 await async_client.beta.sessions.events.with_raw_response.stream(
444 session_id="",
445 )