-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasync_scenario_run.py
More file actions
241 lines (197 loc) · 8.07 KB
/
Copy pathasync_scenario_run.py
File metadata and controls
241 lines (197 loc) · 8.07 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""AsyncScenarioRun resource class for asynchronous operations."""
from __future__ import annotations
import os
from typing import Union, Optional
from typing_extensions import Unpack, override
from ..types import ScenarioRunView
from ._types import BaseRequestOptions, LongRequestOptions, PollingRequestOptions
from .._client import AsyncRunloop
from ._helpers import filter_params
from .async_devbox import AsyncDevbox
from ..types.scoring_contract_result_view import ScoringContractResultView
class AsyncScenarioRun:
"""A running scenario with devbox access (async).
Provides async methods for managing the scenario run lifecycle, accessing
the devbox, and retrieving scoring results. Obtain instances via
``scenario.run()`` or ``scenario.run_async()``.
Example:
>>> scenario = runloop.scenario.from_id("scn-xxx")
>>> run = await scenario.run_async()
>>> await run.await_env_ready()
>>> devbox = run.devbox
>>> # ... agent does work on the devbox ...
>>> await run.score_and_await()
>>> score = await run.get_score()
"""
def __init__(self, client: AsyncRunloop, run_id: str, devbox_id: str) -> None:
"""Create an AsyncScenarioRun instance.
:param client: AsyncRunloop client instance
:type client: AsyncRunloop
:param run_id: Scenario run ID
:type run_id: str
:param devbox_id: Devbox ID associated with this run
:type devbox_id: str
"""
self._client = client
self._id = run_id
self._devbox_id = devbox_id
@override
def __repr__(self) -> str:
return f"<AsyncScenarioRun id={self._id!r}>"
@property
def id(self) -> str:
"""Return the scenario run ID.
:return: Unique scenario run ID
:rtype: str
"""
return self._id
@property
def devbox_id(self) -> str:
"""Return the devbox ID associated with this run.
:return: Devbox ID
:rtype: str
"""
return self._devbox_id
async def get_devbox(self) -> AsyncDevbox:
"""Get the devbox instance for this scenario run.
Use this to interact with the devbox environment during the scenario run.
:return: AsyncDevbox instance
:rtype: AsyncDevbox
"""
devbox_view = await self._client.devboxes.retrieve(self._devbox_id)
return AsyncDevbox(self._client, devbox_view)
async def get_info(
self,
**options: Unpack[BaseRequestOptions],
) -> ScenarioRunView:
"""Retrieve current scenario run status and metadata.
:param options: See :typeddict:`~runloop_api_client.sdk._types.BaseRequestOptions` for available options
:return: Current scenario run state info
:rtype: ScenarioRunView
"""
return await self._client.scenarios.runs.retrieve(
self._id,
**options,
)
async def await_env_ready(
self,
**options: Unpack[PollingRequestOptions],
) -> ScenarioRunView:
"""Wait for the scenario environment (devbox) to be ready.
Blocks until the devbox reaches running state.
:param options: See :typeddict:`~runloop_api_client.sdk._types.PollingRequestOptions` for available options
:return: Scenario run state after environment is ready
:rtype: ScenarioRunView
"""
await self._client.devboxes.await_running(self._devbox_id, polling_config=options.get("polling_config"))
return await self.get_info(**filter_params(options, BaseRequestOptions))
async def score(
self,
**options: Unpack[LongRequestOptions],
) -> ScenarioRunView:
"""Submit the scenario run for scoring.
This triggers the scoring process using the scenario's scoring contract.
:param options: See :typeddict:`~runloop_api_client.sdk._types.LongRequestOptions` for available options
:return: Updated scenario run state
:rtype: ScenarioRunView
"""
return await self._client.scenarios.runs.score(
self._id,
**options,
)
async def await_scored(
self,
**options: Unpack[PollingRequestOptions],
) -> ScenarioRunView:
"""Wait for the scenario run to be scored.
Blocks until scoring is complete.
:param options: See :typeddict:`~runloop_api_client.sdk._types.PollingRequestOptions` for available options
:return: Scored scenario run state
:rtype: ScenarioRunView
"""
return await self._client.scenarios.runs.await_scored(
self._id,
**options,
)
async def score_and_await(
self,
**options: Unpack[PollingRequestOptions],
) -> ScenarioRunView:
"""Submit for scoring and wait for completion.
Convenience method that calls score() then await_scored().
:param options: See :typeddict:`~runloop_api_client.sdk._types.PollingRequestOptions` for available options
:return: Scored scenario run state
:rtype: ScenarioRunView
"""
return await self._client.scenarios.runs.score_and_await(
self._id,
**options,
)
async def score_and_complete(
self,
**options: Unpack[PollingRequestOptions],
) -> ScenarioRunView:
"""Score the run, wait for scoring, then complete and shutdown.
Convenience method that scores the scenario run, waits for scoring to
finish, then completes the run and shuts down the devbox.
:param options: See :typeddict:`~runloop_api_client.sdk._types.PollingRequestOptions` for available options
:return: Completed scenario run state with scoring results
:rtype: ScenarioRunView
"""
return await self._client.scenarios.runs.score_and_complete(
self._id,
**options,
)
async def complete(
self,
**options: Unpack[LongRequestOptions],
) -> ScenarioRunView:
"""Complete the scenario run and shutdown the devbox.
:param options: See :typeddict:`~runloop_api_client.sdk._types.LongRequestOptions` for available options
:return: Final scenario run state
:rtype: ScenarioRunView
"""
return await self._client.scenarios.runs.complete(
self._id,
**options,
)
async def cancel(
self,
**options: Unpack[LongRequestOptions],
) -> ScenarioRunView:
"""Cancel the scenario run and shutdown the devbox.
:param options: See :typeddict:`~runloop_api_client.sdk._types.LongRequestOptions` for available options
:return: Cancelled scenario run state
:rtype: ScenarioRunView
"""
return await self._client.scenarios.runs.cancel(
self._id,
**options,
)
async def download_logs(
self,
file: Union[str, os.PathLike[str]],
**options: Unpack[LongRequestOptions],
) -> None:
"""Download all logs for this scenario run to a zip file.
Downloads a zip archive containing all logs from the scenario run's
associated devbox.
:param file: Path where the zip file will be written
:type file: str | os.PathLike[str]
:param options: See :typeddict:`~runloop_api_client.sdk._types.LongRequestOptions` for available options
"""
response = await self._client.scenarios.runs.download_logs(self._id, **options)
await response.write_to_file(file)
async def get_score(
self,
**options: Unpack[BaseRequestOptions],
) -> Optional[ScoringContractResultView]:
"""Get the scoring result for this run.
Returns None if the run has not been scored yet. Always makes an API
call to retrieve the current scoring result.
:param options: See :typeddict:`~runloop_api_client.sdk._types.BaseRequestOptions` for available options
:return: Scoring result or None if not yet scored
:rtype: Optional[ScoringContractResultView]
"""
info = await self.get_info(**options)
return info.scoring_contract_result