Skip to content

Commit f7daa04

Browse files
feat: Filter the telemetry SSE stream by event type
Stainless-Generated-From: d9acb7a1d1280429f6a722334df6386f763b66e1
1 parent 541fb3f commit f7daa04

3 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎src/kernel/resources/browsers/telemetry.py‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def stream(
157157
id_or_name: str,
158158
*,
159159
replay: str | Omit = omit,
160+
type: SequenceNotStr[str] | Omit = omit,
160161
last_event_id: str | Omit = omit,
161162
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
162163
# The extra values given here take precedence over values defined on the client or passed to this method.
@@ -171,7 +172,10 @@ def stream(
171172
stream closes when the browser session terminates. Each event frame includes an
172173
id: field containing a monotonically increasing sequence number; pass it as
173174
Last-Event-ID on reconnect to resume without gaps. The event: field is never
174-
set; all frames carry JSON in the data: field. A keepalive comment frame is sent
175+
set; event frames carry JSON in the data: field. A frame with an id: field and
176+
no data: field moves Last-Event-ID past events that were not delivered, because
177+
a type filter excluded them or they could not be decoded or exceeded the size
178+
limit; SSE clients dispatch no event for it. A keepalive comment frame is sent
175179
every 15 seconds when no events arrive. Returns 404 if the browser session does
176180
not exist. If telemetry was not enabled on the session, the stream opens but no
177181
events are delivered. Fresh connections only see new events; pass replay=all to
@@ -182,6 +186,14 @@ def stream(
182186
any other value is treated as from-now. The buffer is bounded, so the first
183187
event id may be greater than 1 if older events were evicted.
184188
189+
type: Deliver only these event types, such as captcha_solve_started or
190+
captcha_challenge_result. Repeat the parameter or pass comma-separated values
191+
for multiple types. Keepalive frames are always delivered. Filtered-out events
192+
are not sent. An id-only frame carrying the latest skipped id is sent about
193+
every 15 seconds while skipped events keep arriving, and otherwise with the next
194+
keepalive or when the stream ends, so the connection stays active and
195+
Last-Event-ID moves past them; a reconnect resumes after the skipped events.
196+
185197
extra_headers: Send extra headers
186198
187199
extra_query: Add additional query parameters to the request
@@ -201,7 +213,13 @@ def stream(
201213
extra_query=extra_query,
202214
extra_body=extra_body,
203215
timeout=timeout,
204-
query=maybe_transform({"replay": replay}, telemetry_stream_params.TelemetryStreamParams),
216+
query=maybe_transform(
217+
{
218+
"replay": replay,
219+
"type": type,
220+
},
221+
telemetry_stream_params.TelemetryStreamParams,
222+
),
205223
),
206224
cast_to=TelemetryStreamResponse,
207225
stream=True,
@@ -339,6 +357,7 @@ async def stream(
339357
id_or_name: str,
340358
*,
341359
replay: str | Omit = omit,
360+
type: SequenceNotStr[str] | Omit = omit,
342361
last_event_id: str | Omit = omit,
343362
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
344363
# The extra values given here take precedence over values defined on the client or passed to this method.
@@ -353,7 +372,10 @@ async def stream(
353372
stream closes when the browser session terminates. Each event frame includes an
354373
id: field containing a monotonically increasing sequence number; pass it as
355374
Last-Event-ID on reconnect to resume without gaps. The event: field is never
356-
set; all frames carry JSON in the data: field. A keepalive comment frame is sent
375+
set; event frames carry JSON in the data: field. A frame with an id: field and
376+
no data: field moves Last-Event-ID past events that were not delivered, because
377+
a type filter excluded them or they could not be decoded or exceeded the size
378+
limit; SSE clients dispatch no event for it. A keepalive comment frame is sent
357379
every 15 seconds when no events arrive. Returns 404 if the browser session does
358380
not exist. If telemetry was not enabled on the session, the stream opens but no
359381
events are delivered. Fresh connections only see new events; pass replay=all to
@@ -364,6 +386,14 @@ async def stream(
364386
any other value is treated as from-now. The buffer is bounded, so the first
365387
event id may be greater than 1 if older events were evicted.
366388
389+
type: Deliver only these event types, such as captcha_solve_started or
390+
captcha_challenge_result. Repeat the parameter or pass comma-separated values
391+
for multiple types. Keepalive frames are always delivered. Filtered-out events
392+
are not sent. An id-only frame carrying the latest skipped id is sent about
393+
every 15 seconds while skipped events keep arriving, and otherwise with the next
394+
keepalive or when the stream ends, so the connection stays active and
395+
Last-Event-ID moves past them; a reconnect resumes after the skipped events.
396+
367397
extra_headers: Send extra headers
368398
369399
extra_query: Add additional query parameters to the request
@@ -383,7 +413,13 @@ async def stream(
383413
extra_query=extra_query,
384414
extra_body=extra_body,
385415
timeout=timeout,
386-
query=await async_maybe_transform({"replay": replay}, telemetry_stream_params.TelemetryStreamParams),
416+
query=await async_maybe_transform(
417+
{
418+
"replay": replay,
419+
"type": type,
420+
},
421+
telemetry_stream_params.TelemetryStreamParams,
422+
),
387423
),
388424
cast_to=TelemetryStreamResponse,
389425
stream=True,

‎src/kernel/types/browsers/telemetry_stream_params.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing_extensions import Annotated, TypedDict
66

7+
from ..._types import SequenceNotStr
78
from ..._utils import PropertyInfo
89

910
__all__ = ["TelemetryStreamParams"]
@@ -17,4 +18,15 @@ class TelemetryStreamParams(TypedDict, total=False):
1718
event id may be greater than 1 if older events were evicted.
1819
"""
1920

21+
type: SequenceNotStr[str]
22+
"""
23+
Deliver only these event types, such as captcha_solve_started or
24+
captcha_challenge_result. Repeat the parameter or pass comma-separated values
25+
for multiple types. Keepalive frames are always delivered. Filtered-out events
26+
are not sent. An id-only frame carrying the latest skipped id is sent about
27+
every 15 seconds while skipped events keep arriving, and otherwise with the next
28+
keepalive or when the stream ends, so the connection stays active and
29+
Last-Event-ID moves past them; a reconnect resumes after the skipped events.
30+
"""
31+
2032
last_event_id: Annotated[str, PropertyInfo(alias="Last-Event-ID")]

‎tests/api_resources/browsers/test_telemetry.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def test_method_stream_with_all_params(self, client: Kernel) -> None:
8989
telemetry_stream = client.browsers.telemetry.stream(
9090
id_or_name="htzv5orfit78e1m2biiifpbv",
9191
replay="replay",
92+
type=["string"],
9293
last_event_id="Last-Event-ID",
9394
)
9495
telemetry_stream.response.close()
@@ -203,6 +204,7 @@ async def test_method_stream_with_all_params(self, async_client: AsyncKernel) ->
203204
telemetry_stream = await async_client.browsers.telemetry.stream(
204205
id_or_name="htzv5orfit78e1m2biiifpbv",
205206
replay="replay",
207+
type=["string"],
206208
last_event_id="Last-Event-ID",
207209
)
208210
await telemetry_stream.response.aclose()

0 commit comments

Comments
 (0)