ellipsis-dev 0.35.0__tar.gz → 0.37.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/PKG-INFO +2 -2
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/README.md +1 -1
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/pyproject.toml +1 -1
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/__init__.py +4 -4
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/_client.py +34 -142
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/_session_handle.py +66 -60
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/_stream.py +25 -34
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/frames.py +11 -37
- ellipsis_dev-0.37.0/src/ellipsis/lifecycle.py +71 -0
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/models.py +261 -423
- ellipsis_dev-0.35.0/src/ellipsis/lifecycle.py +0 -165
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/.gitignore +0 -0
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/_errors.py +0 -0
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/_pagination.py +0 -0
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/_transport.py +0 -0
- {ellipsis_dev-0.35.0 → ellipsis_dev-0.37.0}/src/ellipsis/collection_frames.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: ellipsis-dev
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.37.0
|
|
4
4
|
Summary: Python SDK for the Ellipsis agents platform: every /v1 operation, sync and async.
|
|
5
5
|
Project-URL: Homepage, https://www.ellipsis.dev
|
|
6
6
|
Project-URL: Documentation, https://www.ellipsis.dev/docs/api
|
|
@@ -26,7 +26,7 @@ session = client.sessions.start(
|
|
|
26
26
|
claude_code={"prompt": "Fix the flaky test in ci/"},
|
|
27
27
|
).session
|
|
28
28
|
for s in client.sessions.list(): # cursor pagination walks every page
|
|
29
|
-
print(s.id, s.
|
|
29
|
+
print(s.id, s.status)
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
Async is the same surface:
|
|
@@ -12,7 +12,7 @@ session = client.sessions.start(
|
|
|
12
12
|
claude_code={"prompt": "Fix the flaky test in ci/"},
|
|
13
13
|
).session
|
|
14
14
|
for s in client.sessions.list(): # cursor pagination walks every page
|
|
15
|
-
print(s.id, s.
|
|
15
|
+
print(s.id, s.status)
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
Async is the same surface:
|
|
@@ -28,10 +28,10 @@ from ._errors import (
|
|
|
28
28
|
)
|
|
29
29
|
from ._pagination import AsyncPage, SyncPage
|
|
30
30
|
from ._session_handle import (
|
|
31
|
-
|
|
31
|
+
SETTLED_SESSION_STATUSES,
|
|
32
32
|
AsyncSessionHandle,
|
|
33
33
|
SessionHandle,
|
|
34
|
-
|
|
34
|
+
is_session_settled,
|
|
35
35
|
)
|
|
36
36
|
from ._stream import (
|
|
37
37
|
StreamAuthError,
|
|
@@ -54,8 +54,8 @@ __all__ = [
|
|
|
54
54
|
"ServerError",
|
|
55
55
|
"SyncPage",
|
|
56
56
|
"TransportError",
|
|
57
|
-
"
|
|
58
|
-
"
|
|
57
|
+
"SETTLED_SESSION_STATUSES",
|
|
58
|
+
"is_session_settled",
|
|
59
59
|
"AsyncSessionHandle",
|
|
60
60
|
"SessionHandle",
|
|
61
61
|
"StreamAuthError",
|
|
@@ -2128,67 +2128,9 @@ class _SyncSecrets:
|
|
|
2128
2128
|
return response
|
|
2129
2129
|
|
|
2130
2130
|
|
|
2131
|
-
class _SyncSessionsTurns:
|
|
2132
|
-
def __init__(self, transport: Transport) -> None:
|
|
2133
|
-
self._transport = transport
|
|
2134
|
-
|
|
2135
|
-
def get(
|
|
2136
|
-
self,
|
|
2137
|
-
session_id: str,
|
|
2138
|
-
id: str,
|
|
2139
|
-
) -> models.SessionTurnResponse:
|
|
2140
|
-
"""Get Session Turn
|
|
2141
|
-
|
|
2142
|
-
Return one turn.
|
|
2143
|
-
|
|
2144
|
-
Poll this after sending a message: the message is answered when
|
|
2145
|
-
the turn's status is completed, failed, stopped, or cancelled.
|
|
2146
|
-
|
|
2147
|
-
Required permissions: read:sessions.
|
|
2148
|
-
API key: Supported.
|
|
2149
|
-
CLI user token: Supported.
|
|
2150
|
-
Session token: Requires the ellipsis:api scope and the required grants in the token's permissions.ellipsis configuration.
|
|
2151
|
-
Higher permission levels include lower levels: read < write < delete.
|
|
2152
|
-
Grant match patterns can further restrict access to individual resources.
|
|
2153
|
-
"""
|
|
2154
|
-
path = f"/v1/sessions/{_quote(session_id)}/turns/{_quote(id)}"
|
|
2155
|
-
response = self._transport.request(
|
|
2156
|
-
"GET", path, response_model=models.SessionTurnResponse
|
|
2157
|
-
)
|
|
2158
|
-
assert response is not None
|
|
2159
|
-
return response
|
|
2160
|
-
|
|
2161
|
-
def list(
|
|
2162
|
-
self,
|
|
2163
|
-
id: str,
|
|
2164
|
-
) -> models.SessionTurnsListResponse:
|
|
2165
|
-
"""Get Session Turns
|
|
2166
|
-
|
|
2167
|
-
Return the session's turns and messages, oldest first.
|
|
2168
|
-
|
|
2169
|
-
One turn per message: pending until the agent starts on it,
|
|
2170
|
-
running while it works, then completed, failed, stopped, or
|
|
2171
|
-
cancelled.
|
|
2172
|
-
|
|
2173
|
-
Required permissions: read:sessions.
|
|
2174
|
-
API key: Supported.
|
|
2175
|
-
CLI user token: Supported.
|
|
2176
|
-
Session token: Requires the ellipsis:api scope and the required grants in the token's permissions.ellipsis configuration.
|
|
2177
|
-
Higher permission levels include lower levels: read < write < delete.
|
|
2178
|
-
Grant match patterns can further restrict access to individual resources.
|
|
2179
|
-
"""
|
|
2180
|
-
path = f"/v1/sessions/{_quote(id)}/turns"
|
|
2181
|
-
response = self._transport.request(
|
|
2182
|
-
"GET", path, response_model=models.SessionTurnsListResponse
|
|
2183
|
-
)
|
|
2184
|
-
assert response is not None
|
|
2185
|
-
return response
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
2131
|
class _SyncSessions(SyncSessionsSugar):
|
|
2189
2132
|
def __init__(self, transport: Transport) -> None:
|
|
2190
2133
|
self._transport = transport
|
|
2191
|
-
self.turns = _SyncSessionsTurns(transport)
|
|
2192
2134
|
|
|
2193
2135
|
def analytics(
|
|
2194
2136
|
self,
|
|
@@ -2387,15 +2329,14 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2387
2329
|
cursor: Union[str, None] = None,
|
|
2388
2330
|
author_id: Union[int, None] = None,
|
|
2389
2331
|
repo: Union[str, None] = None,
|
|
2390
|
-
|
|
2332
|
+
status: Union[List[models.SessionStatus], None] = None,
|
|
2391
2333
|
) -> SyncPage[models.Session, models.SessionsListResponse]:
|
|
2392
2334
|
"""List Sessions
|
|
2393
2335
|
|
|
2394
2336
|
List cloud agent sessions, newest first.
|
|
2395
2337
|
|
|
2396
2338
|
Optionally filtered by agent (id or name), source, service, handler, time
|
|
2397
|
-
window, attributed author, repository, and
|
|
2398
|
-
still going.
|
|
2339
|
+
window, attributed author, repository, and status.
|
|
2399
2340
|
|
|
2400
2341
|
Required permissions: read:sessions.
|
|
2401
2342
|
API key: Supported.
|
|
@@ -2418,7 +2359,7 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2418
2359
|
"cursor": cursor,
|
|
2419
2360
|
"author_id": author_id,
|
|
2420
2361
|
"repo": repo,
|
|
2421
|
-
"
|
|
2362
|
+
"status": status,
|
|
2422
2363
|
}
|
|
2423
2364
|
)
|
|
2424
2365
|
response = self._transport.request(
|
|
@@ -2440,7 +2381,7 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2440
2381
|
cursor=_cursor,
|
|
2441
2382
|
author_id=author_id,
|
|
2442
2383
|
repo=repo,
|
|
2443
|
-
|
|
2384
|
+
status=status,
|
|
2444
2385
|
),
|
|
2445
2386
|
)
|
|
2446
2387
|
|
|
@@ -2475,6 +2416,7 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2475
2416
|
*,
|
|
2476
2417
|
cursor: Union[str, None] = None,
|
|
2477
2418
|
limit: Union[int, None] = None,
|
|
2419
|
+
message_id: Union[str, None] = None,
|
|
2478
2420
|
) -> SyncPage[models.SessionRecord, models.SessionRecordsListResponse]:
|
|
2479
2421
|
"""Get Session Records
|
|
2480
2422
|
|
|
@@ -2498,6 +2440,7 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2498
2440
|
{
|
|
2499
2441
|
"cursor": cursor,
|
|
2500
2442
|
"limit": limit,
|
|
2443
|
+
"message_id": message_id,
|
|
2501
2444
|
}
|
|
2502
2445
|
)
|
|
2503
2446
|
response = self._transport.request(
|
|
@@ -2507,7 +2450,9 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2507
2450
|
return SyncPage(
|
|
2508
2451
|
response,
|
|
2509
2452
|
"records",
|
|
2510
|
-
lambda _cursor: self.records(
|
|
2453
|
+
lambda _cursor: self.records(
|
|
2454
|
+
id=id, cursor=_cursor, limit=limit, message_id=message_id
|
|
2455
|
+
),
|
|
2511
2456
|
)
|
|
2512
2457
|
|
|
2513
2458
|
def search(
|
|
@@ -2522,7 +2467,7 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2522
2467
|
agent_id: Union[List[str], None] = None,
|
|
2523
2468
|
session_ids: Union[List[str], None] = None,
|
|
2524
2469
|
repo: Union[List[str], None] = None,
|
|
2525
|
-
status: Union[List[models.
|
|
2470
|
+
status: Union[List[models.SessionStatus], None] = None,
|
|
2526
2471
|
start: Union[datetime, None] = None,
|
|
2527
2472
|
end: Union[datetime, None] = None,
|
|
2528
2473
|
limit: Union[int, None] = None,
|
|
@@ -2573,8 +2518,9 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2573
2518
|
Send a message to a session.
|
|
2574
2519
|
|
|
2575
2520
|
409 for mention, non-interactive, closed, and out-of-budget
|
|
2576
|
-
sessions. Returns the created message
|
|
2577
|
-
|
|
2521
|
+
sessions. Returns the created message; a retried send with the
|
|
2522
|
+
same idempotency_key returns the original one. The message is
|
|
2523
|
+
answered once the session's status is `idle` or `closed`.
|
|
2578
2524
|
|
|
2579
2525
|
Required permissions: write:sessions.
|
|
2580
2526
|
API key: Supported.
|
|
@@ -2692,11 +2638,11 @@ class _SyncSessions(SyncSessionsSugar):
|
|
|
2692
2638
|
) -> models.SessionResponse:
|
|
2693
2639
|
"""Stop Session
|
|
2694
2640
|
|
|
2695
|
-
Stop the
|
|
2641
|
+
Stop the session's work and return it.
|
|
2696
2642
|
|
|
2697
|
-
The
|
|
2698
|
-
|
|
2699
|
-
when the
|
|
2643
|
+
The running message ends `stopped` and every queued message is
|
|
2644
|
+
cancelled, so the session goes `idle` (or `closed` when it was
|
|
2645
|
+
not interactive). 409 when the session is idle or closed already.
|
|
2700
2646
|
|
|
2701
2647
|
Required permissions: write:sessions.
|
|
2702
2648
|
API key: Supported.
|
|
@@ -5530,67 +5476,9 @@ class _AsyncSecrets:
|
|
|
5530
5476
|
return response
|
|
5531
5477
|
|
|
5532
5478
|
|
|
5533
|
-
class _AsyncSessionsTurns:
|
|
5534
|
-
def __init__(self, transport: AsyncTransport) -> None:
|
|
5535
|
-
self._transport = transport
|
|
5536
|
-
|
|
5537
|
-
async def get(
|
|
5538
|
-
self,
|
|
5539
|
-
session_id: str,
|
|
5540
|
-
id: str,
|
|
5541
|
-
) -> models.SessionTurnResponse:
|
|
5542
|
-
"""Get Session Turn
|
|
5543
|
-
|
|
5544
|
-
Return one turn.
|
|
5545
|
-
|
|
5546
|
-
Poll this after sending a message: the message is answered when
|
|
5547
|
-
the turn's status is completed, failed, stopped, or cancelled.
|
|
5548
|
-
|
|
5549
|
-
Required permissions: read:sessions.
|
|
5550
|
-
API key: Supported.
|
|
5551
|
-
CLI user token: Supported.
|
|
5552
|
-
Session token: Requires the ellipsis:api scope and the required grants in the token's permissions.ellipsis configuration.
|
|
5553
|
-
Higher permission levels include lower levels: read < write < delete.
|
|
5554
|
-
Grant match patterns can further restrict access to individual resources.
|
|
5555
|
-
"""
|
|
5556
|
-
path = f"/v1/sessions/{_quote(session_id)}/turns/{_quote(id)}"
|
|
5557
|
-
response = await self._transport.request(
|
|
5558
|
-
"GET", path, response_model=models.SessionTurnResponse
|
|
5559
|
-
)
|
|
5560
|
-
assert response is not None
|
|
5561
|
-
return response
|
|
5562
|
-
|
|
5563
|
-
async def list(
|
|
5564
|
-
self,
|
|
5565
|
-
id: str,
|
|
5566
|
-
) -> models.SessionTurnsListResponse:
|
|
5567
|
-
"""Get Session Turns
|
|
5568
|
-
|
|
5569
|
-
Return the session's turns and messages, oldest first.
|
|
5570
|
-
|
|
5571
|
-
One turn per message: pending until the agent starts on it,
|
|
5572
|
-
running while it works, then completed, failed, stopped, or
|
|
5573
|
-
cancelled.
|
|
5574
|
-
|
|
5575
|
-
Required permissions: read:sessions.
|
|
5576
|
-
API key: Supported.
|
|
5577
|
-
CLI user token: Supported.
|
|
5578
|
-
Session token: Requires the ellipsis:api scope and the required grants in the token's permissions.ellipsis configuration.
|
|
5579
|
-
Higher permission levels include lower levels: read < write < delete.
|
|
5580
|
-
Grant match patterns can further restrict access to individual resources.
|
|
5581
|
-
"""
|
|
5582
|
-
path = f"/v1/sessions/{_quote(id)}/turns"
|
|
5583
|
-
response = await self._transport.request(
|
|
5584
|
-
"GET", path, response_model=models.SessionTurnsListResponse
|
|
5585
|
-
)
|
|
5586
|
-
assert response is not None
|
|
5587
|
-
return response
|
|
5588
|
-
|
|
5589
|
-
|
|
5590
5479
|
class _AsyncSessions(AsyncSessionsSugar):
|
|
5591
5480
|
def __init__(self, transport: AsyncTransport) -> None:
|
|
5592
5481
|
self._transport = transport
|
|
5593
|
-
self.turns = _AsyncSessionsTurns(transport)
|
|
5594
5482
|
|
|
5595
5483
|
async def analytics(
|
|
5596
5484
|
self,
|
|
@@ -5789,15 +5677,14 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
5789
5677
|
cursor: Union[str, None] = None,
|
|
5790
5678
|
author_id: Union[int, None] = None,
|
|
5791
5679
|
repo: Union[str, None] = None,
|
|
5792
|
-
|
|
5680
|
+
status: Union[List[models.SessionStatus], None] = None,
|
|
5793
5681
|
) -> AsyncPage[models.Session, models.SessionsListResponse]:
|
|
5794
5682
|
"""List Sessions
|
|
5795
5683
|
|
|
5796
5684
|
List cloud agent sessions, newest first.
|
|
5797
5685
|
|
|
5798
5686
|
Optionally filtered by agent (id or name), source, service, handler, time
|
|
5799
|
-
window, attributed author, repository, and
|
|
5800
|
-
still going.
|
|
5687
|
+
window, attributed author, repository, and status.
|
|
5801
5688
|
|
|
5802
5689
|
Required permissions: read:sessions.
|
|
5803
5690
|
API key: Supported.
|
|
@@ -5820,7 +5707,7 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
5820
5707
|
"cursor": cursor,
|
|
5821
5708
|
"author_id": author_id,
|
|
5822
5709
|
"repo": repo,
|
|
5823
|
-
"
|
|
5710
|
+
"status": status,
|
|
5824
5711
|
}
|
|
5825
5712
|
)
|
|
5826
5713
|
response = await self._transport.request(
|
|
@@ -5842,7 +5729,7 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
5842
5729
|
cursor=_cursor,
|
|
5843
5730
|
author_id=author_id,
|
|
5844
5731
|
repo=repo,
|
|
5845
|
-
|
|
5732
|
+
status=status,
|
|
5846
5733
|
),
|
|
5847
5734
|
)
|
|
5848
5735
|
|
|
@@ -5877,6 +5764,7 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
5877
5764
|
*,
|
|
5878
5765
|
cursor: Union[str, None] = None,
|
|
5879
5766
|
limit: Union[int, None] = None,
|
|
5767
|
+
message_id: Union[str, None] = None,
|
|
5880
5768
|
) -> AsyncPage[models.SessionRecord, models.SessionRecordsListResponse]:
|
|
5881
5769
|
"""Get Session Records
|
|
5882
5770
|
|
|
@@ -5900,6 +5788,7 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
5900
5788
|
{
|
|
5901
5789
|
"cursor": cursor,
|
|
5902
5790
|
"limit": limit,
|
|
5791
|
+
"message_id": message_id,
|
|
5903
5792
|
}
|
|
5904
5793
|
)
|
|
5905
5794
|
response = await self._transport.request(
|
|
@@ -5909,7 +5798,9 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
5909
5798
|
return AsyncPage(
|
|
5910
5799
|
response,
|
|
5911
5800
|
"records",
|
|
5912
|
-
lambda _cursor: self.records(
|
|
5801
|
+
lambda _cursor: self.records(
|
|
5802
|
+
id=id, cursor=_cursor, limit=limit, message_id=message_id
|
|
5803
|
+
),
|
|
5913
5804
|
)
|
|
5914
5805
|
|
|
5915
5806
|
async def search(
|
|
@@ -5924,7 +5815,7 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
5924
5815
|
agent_id: Union[List[str], None] = None,
|
|
5925
5816
|
session_ids: Union[List[str], None] = None,
|
|
5926
5817
|
repo: Union[List[str], None] = None,
|
|
5927
|
-
status: Union[List[models.
|
|
5818
|
+
status: Union[List[models.SessionStatus], None] = None,
|
|
5928
5819
|
start: Union[datetime, None] = None,
|
|
5929
5820
|
end: Union[datetime, None] = None,
|
|
5930
5821
|
limit: Union[int, None] = None,
|
|
@@ -5975,8 +5866,9 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
5975
5866
|
Send a message to a session.
|
|
5976
5867
|
|
|
5977
5868
|
409 for mention, non-interactive, closed, and out-of-budget
|
|
5978
|
-
sessions. Returns the created message
|
|
5979
|
-
|
|
5869
|
+
sessions. Returns the created message; a retried send with the
|
|
5870
|
+
same idempotency_key returns the original one. The message is
|
|
5871
|
+
answered once the session's status is `idle` or `closed`.
|
|
5980
5872
|
|
|
5981
5873
|
Required permissions: write:sessions.
|
|
5982
5874
|
API key: Supported.
|
|
@@ -6094,11 +5986,11 @@ class _AsyncSessions(AsyncSessionsSugar):
|
|
|
6094
5986
|
) -> models.SessionResponse:
|
|
6095
5987
|
"""Stop Session
|
|
6096
5988
|
|
|
6097
|
-
Stop the
|
|
5989
|
+
Stop the session's work and return it.
|
|
6098
5990
|
|
|
6099
|
-
The
|
|
6100
|
-
|
|
6101
|
-
when the
|
|
5991
|
+
The running message ends `stopped` and every queued message is
|
|
5992
|
+
cancelled, so the session goes `idle` (or `closed` when it was
|
|
5993
|
+
not interactive). 409 when the session is idle or closed already.
|
|
6102
5994
|
|
|
6103
5995
|
Required permissions: write:sessions.
|
|
6104
5996
|
API key: Supported.
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
Hand-written by design (the start -> stream -> message -> stop flow is an
|
|
4
4
|
ergonomics judgment, not a projection of the spec). Everything the handle
|
|
5
5
|
does rides the generated core + the stream client; it only carries the
|
|
6
|
-
session id and the
|
|
6
|
+
session id and the message it is waiting on.
|
|
7
7
|
|
|
8
8
|
client = Ellipsis(api_key=...)
|
|
9
9
|
handle = client.sessions.run(claude_code={"prompt": "fix the flaky test"})
|
|
10
10
|
handle.send("also update the changelog")
|
|
11
|
-
|
|
11
|
+
ended = handle.wait() # the message_ended record's payload
|
|
12
12
|
|
|
13
13
|
async with AsyncEllipsis(api_key=...) as client:
|
|
14
14
|
handle = await client.sessions.run(claude_code={"prompt": "..."})
|
|
@@ -18,21 +18,35 @@ session id and the resume cursor.
|
|
|
18
18
|
import asyncio
|
|
19
19
|
import time
|
|
20
20
|
from collections.abc import Awaitable, Callable
|
|
21
|
-
from typing import TYPE_CHECKING
|
|
21
|
+
from typing import TYPE_CHECKING, Any
|
|
22
22
|
|
|
23
|
-
from . import frames, models
|
|
23
|
+
from . import frames, lifecycle, models
|
|
24
24
|
from ._stream import DEFAULT_MAX_RECONNECTS, StreamOutcome, stream_session
|
|
25
25
|
|
|
26
26
|
if TYPE_CHECKING:
|
|
27
27
|
from ._client import _AsyncSessions, _SyncSessions
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
SETTLED_SESSION_STATUSES = frozenset({"idle", "closed"})
|
|
30
30
|
|
|
31
31
|
DEFAULT_POLL_INTERVAL_SECONDS = 3.0
|
|
32
32
|
|
|
33
33
|
|
|
34
|
-
def
|
|
35
|
-
|
|
34
|
+
def is_session_settled(status: object) -> bool:
|
|
35
|
+
"""Whether every message sent to the session has been answered: the
|
|
36
|
+
session is idle or closed."""
|
|
37
|
+
return str(status) in SETTLED_SESSION_STATUSES
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _message_ended(records: Any, message_id: str | None) -> Any:
|
|
41
|
+
"""The message_ended payload among `records`: the one for `message_id`,
|
|
42
|
+
else the last one."""
|
|
43
|
+
ended = None
|
|
44
|
+
for record in records:
|
|
45
|
+
if getattr(record, "record_type", None) != "message_ended":
|
|
46
|
+
continue
|
|
47
|
+
if message_id is None or getattr(record, "message_id", None) == message_id:
|
|
48
|
+
ended = record.payload
|
|
49
|
+
return ended
|
|
36
50
|
|
|
37
51
|
|
|
38
52
|
class SessionHandle:
|
|
@@ -42,7 +56,7 @@ class SessionHandle:
|
|
|
42
56
|
self._sessions = sessions
|
|
43
57
|
self.id = session.id
|
|
44
58
|
self.session = session
|
|
45
|
-
self.
|
|
59
|
+
self.message_id: str | None = None
|
|
46
60
|
|
|
47
61
|
def refresh(self) -> models.Session:
|
|
48
62
|
self.session = self._sessions.get(self.id).session
|
|
@@ -53,21 +67,23 @@ class SessionHandle:
|
|
|
53
67
|
*,
|
|
54
68
|
timeout: float | None = None,
|
|
55
69
|
poll_interval: float = DEFAULT_POLL_INTERVAL_SECONDS,
|
|
56
|
-
) ->
|
|
57
|
-
"""Poll until the
|
|
58
|
-
|
|
59
|
-
|
|
70
|
+
) -> lifecycle.MessageEndedPayload | None:
|
|
71
|
+
"""Poll until the session is idle or closed, then return how the
|
|
72
|
+
message this handle waits on ended (the `message_ended` record's
|
|
73
|
+
payload): the last message sent through `send()`, else the session's
|
|
74
|
+
latest message. None when the session has no message."""
|
|
60
75
|
deadline = None if timeout is None else time.monotonic() + timeout
|
|
61
76
|
while True:
|
|
62
77
|
session = self.refresh()
|
|
63
|
-
if
|
|
64
|
-
|
|
78
|
+
if is_session_settled(session.status):
|
|
79
|
+
break
|
|
65
80
|
if deadline is not None and time.monotonic() >= deadline:
|
|
66
81
|
raise TimeoutError(f"session {self.id} not settled after {timeout}s")
|
|
67
82
|
time.sleep(poll_interval)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
return _message_ended(
|
|
84
|
+
self._sessions.records(self.id, message_id=self.message_id),
|
|
85
|
+
self.message_id,
|
|
86
|
+
)
|
|
71
87
|
|
|
72
88
|
def send(
|
|
73
89
|
self,
|
|
@@ -76,31 +92,38 @@ class SessionHandle:
|
|
|
76
92
|
images: list[models.ImageAttachment] | None = None,
|
|
77
93
|
idempotency_key: str | None = None,
|
|
78
94
|
) -> models.SessionMessage:
|
|
79
|
-
"""
|
|
80
|
-
|
|
81
|
-
as content blocks the model sees on that turn."""
|
|
95
|
+
"""Send a message to the session; `wait()` then waits for it to be
|
|
96
|
+
answered. `images` ride the message as content blocks the model sees."""
|
|
82
97
|
response = self._sessions.send_message(
|
|
83
98
|
self.id, message=message, images=images, idempotency_key=idempotency_key
|
|
84
99
|
)
|
|
85
|
-
self.
|
|
100
|
+
self.message_id = response.message.id
|
|
86
101
|
return response.message
|
|
87
102
|
|
|
88
103
|
def stop(self) -> models.Session:
|
|
89
104
|
self.session = self._sessions.stop(self.id).session
|
|
90
105
|
return self.session
|
|
91
106
|
|
|
92
|
-
def records(
|
|
93
|
-
|
|
107
|
+
def records(
|
|
108
|
+
self,
|
|
109
|
+
*,
|
|
110
|
+
cursor: str | None = None,
|
|
111
|
+
limit: int | None = None,
|
|
112
|
+
message_id: str | None = None,
|
|
113
|
+
):
|
|
114
|
+
return self._sessions.records(
|
|
115
|
+
self.id, cursor=cursor, limit=limit, message_id=message_id
|
|
116
|
+
)
|
|
94
117
|
|
|
95
118
|
|
|
96
119
|
class AsyncSessionHandle:
|
|
97
|
-
"""One started session, on the async client
|
|
120
|
+
"""One started session, on the async client; adds `stream()`."""
|
|
98
121
|
|
|
99
122
|
def __init__(self, sessions: "_AsyncSessions", session: models.Session) -> None:
|
|
100
123
|
self._sessions = sessions
|
|
101
124
|
self.id = session.id
|
|
102
125
|
self.session = session
|
|
103
|
-
self.
|
|
126
|
+
self.message_id: str | None = None
|
|
104
127
|
|
|
105
128
|
async def refresh(self) -> models.Session:
|
|
106
129
|
self.session = (await self._sessions.get(self.id)).session
|
|
@@ -111,23 +134,17 @@ class AsyncSessionHandle:
|
|
|
111
134
|
*,
|
|
112
135
|
timeout: float | None = None,
|
|
113
136
|
poll_interval: float = DEFAULT_POLL_INTERVAL_SECONDS,
|
|
114
|
-
) ->
|
|
137
|
+
) -> lifecycle.MessageEndedPayload | None:
|
|
115
138
|
deadline = None if timeout is None else time.monotonic() + timeout
|
|
116
139
|
while True:
|
|
117
140
|
session = await self.refresh()
|
|
118
|
-
if
|
|
119
|
-
|
|
120
|
-
):
|
|
121
|
-
status = str(
|
|
122
|
-
(await self._sessions.turns.get(self.id, self.turn_id)).turn.status
|
|
123
|
-
)
|
|
124
|
-
if is_turn_final(status):
|
|
125
|
-
return session
|
|
126
|
-
elif _settled(session, self.turn_id, None):
|
|
127
|
-
return session
|
|
141
|
+
if is_session_settled(session.status):
|
|
142
|
+
break
|
|
128
143
|
if deadline is not None and time.monotonic() >= deadline:
|
|
129
144
|
raise TimeoutError(f"session {self.id} not settled after {timeout}s")
|
|
130
145
|
await asyncio.sleep(poll_interval)
|
|
146
|
+
page = await self._sessions.records(self.id, message_id=self.message_id)
|
|
147
|
+
return _message_ended([record async for record in page], self.message_id)
|
|
131
148
|
|
|
132
149
|
async def send(
|
|
133
150
|
self,
|
|
@@ -139,15 +156,23 @@ class AsyncSessionHandle:
|
|
|
139
156
|
response = await self._sessions.send_message(
|
|
140
157
|
self.id, message=message, images=images, idempotency_key=idempotency_key
|
|
141
158
|
)
|
|
142
|
-
self.
|
|
159
|
+
self.message_id = response.message.id
|
|
143
160
|
return response.message
|
|
144
161
|
|
|
145
162
|
async def stop(self) -> models.Session:
|
|
146
163
|
self.session = (await self._sessions.stop(self.id)).session
|
|
147
164
|
return self.session
|
|
148
165
|
|
|
149
|
-
async def records(
|
|
150
|
-
|
|
166
|
+
async def records(
|
|
167
|
+
self,
|
|
168
|
+
*,
|
|
169
|
+
cursor: str | None = None,
|
|
170
|
+
limit: int | None = None,
|
|
171
|
+
message_id: str | None = None,
|
|
172
|
+
):
|
|
173
|
+
return await self._sessions.records(
|
|
174
|
+
self.id, cursor=cursor, limit=limit, message_id=message_id
|
|
175
|
+
)
|
|
151
176
|
|
|
152
177
|
async def stream(
|
|
153
178
|
self,
|
|
@@ -197,30 +222,11 @@ class AsyncSessionsSugar:
|
|
|
197
222
|
return AsyncSessionHandle(self, response.session) # type: ignore[arg-type]
|
|
198
223
|
|
|
199
224
|
|
|
200
|
-
def _settled(
|
|
201
|
-
session: models.Session,
|
|
202
|
-
turn_id: str | None,
|
|
203
|
-
turn_status: Callable[[str], str] | None,
|
|
204
|
-
) -> bool:
|
|
205
|
-
"""Whether `wait()` is done: the conversation closed, the session has no
|
|
206
|
-
turn to wait on, or the awaited turn is final. When the session's current
|
|
207
|
-
turn is a later one, `turn_status` fetches the awaited turn itself."""
|
|
208
|
-
if str(session.conversation.state) == "closed":
|
|
209
|
-
return True
|
|
210
|
-
if turn_id is None or session.turn is None:
|
|
211
|
-
return True
|
|
212
|
-
if session.turn.id == turn_id:
|
|
213
|
-
return is_turn_final(session.turn.status)
|
|
214
|
-
if turn_status is None:
|
|
215
|
-
return False
|
|
216
|
-
return is_turn_final(turn_status(turn_id))
|
|
217
|
-
|
|
218
|
-
|
|
219
225
|
__all__ = [
|
|
220
|
-
"
|
|
226
|
+
"SETTLED_SESSION_STATUSES",
|
|
221
227
|
"AsyncSessionHandle",
|
|
222
228
|
"AsyncSessionsSugar",
|
|
223
229
|
"SessionHandle",
|
|
224
230
|
"SyncSessionsSugar",
|
|
225
|
-
"
|
|
231
|
+
"is_session_settled",
|
|
226
232
|
]
|