forked from agntcy/workflow-srv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruns.py
320 lines (262 loc) · 10.2 KB
/
runs.py
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Copyright AGNTCY Contributors (https://github.com/agntcy)
# SPDX-License-Identifier: Apache-2.0
import asyncio
import logging
from collections import defaultdict
from datetime import datetime
from itertools import islice
from typing import Any, AsyncGenerator, AsyncIterator, Dict, List, Optional
from uuid import uuid4
from agent_workflow_server.generated.models.run_create_stateless import (
RunCreateStateless as ApiRunCreate,
)
from agent_workflow_server.generated.models.run_search_request import (
RunSearchRequest,
)
from agent_workflow_server.generated.models.run_stateless import (
RunStateless as ApiRun,
)
from agent_workflow_server.generated.models.stream_event_payload import (
StreamEventPayload,
)
from agent_workflow_server.generated.models.value_run_result_update import (
ValueRunResultUpdate,
)
from agent_workflow_server.storage.models import Run, RunInfo, RunStatus
from agent_workflow_server.storage.storage import DB
from ..utils.tools import is_valid_uuid
from .message import Message
from .thread import get_thread
logger = logging.getLogger(__name__)
condition = asyncio.Condition()
def _make_run(run_create: ApiRunCreate) -> Run:
"""
Convert a RunCreate API model to a Run DB model.
Args:
run_create (RunCreate): The API model for creating a run.
Returns:
Run: The service model representation of the run.
"""
curr_time = datetime.now()
if not is_valid_uuid(run_create.agent_id):
raise ValueError(f'agent_id "{run_create.agent_id}" is not a valid UUID')
return {
"run_id": str(uuid4()),
"agent_id": run_create.agent_id,
"thread_id": str(uuid4()), # TODO
"input": run_create.input,
"config": run_create.config.model_dump() if run_create.config else None,
"metadata": run_create.metadata,
"created_at": curr_time,
"updated_at": curr_time,
"status": "pending",
}
def _to_api_model(run: Run) -> ApiRun:
"""
Convert a Run service model to a Run API model.
Args:
run (Run): The service model representation of a run.
Returns:
Run: The API model representation of the run.
"""
return ApiRun(
creation=ApiRunCreate(
agent_id=run["agent_id"],
thread_id=run["thread_id"],
input=run["input"],
metadata=run["metadata"],
config=run["config"],
webhook=None, # TODO
),
run_id=run["run_id"],
agent_id=run["agent_id"],
thread_id=run["thread_id"],
created_at=run["created_at"],
updated_at=run["updated_at"],
status=run["status"],
)
class StreamManager:
def __init__(self):
self.queues: Dict[str, List[asyncio.Queue]] = {}
def get_queues(self, run_id: str) -> List[asyncio.Queue]:
return self.queues.get(run_id, [])
async def add_queue(self, run_id: str) -> asyncio.Queue:
queue = asyncio.Queue()
self.queues.setdefault(run_id, []).append(queue)
return queue
async def remove_queue(self, run_id: str, queue: asyncio.Queue):
self.queues[run_id].remove(queue)
async def put_message(self, run_id: str, message: Message) -> None:
queues = self.get_queues(run_id)
num = len(queues)
if not queues:
logger.warning(f"No queues found for run_id {run_id}")
await asyncio.gather(*(queue.put(message) for queue in queues))
logger.debug(f"Message put on {num} queues for run_id {run_id}")
stream_manager = StreamManager()
cvs_pending_run = defaultdict(asyncio.Condition)
RUNS_QUEUE = asyncio.Queue()
class Runs:
@staticmethod
async def put(run_create: ApiRunCreate) -> ApiRun:
new_run = _make_run(run_create)
run_info = RunInfo(
run_id=new_run["run_id"],
queued_at=datetime.now(),
attempts=0,
)
DB.create_run(new_run)
DB.create_run_info(run_info)
await RUNS_QUEUE.put(new_run["run_id"])
return _to_api_model(new_run)
@staticmethod
def get(run_id: str) -> Optional[ApiRun]:
run = DB.get_run(run_id)
if run is None:
return None
return _to_api_model(run)
@staticmethod
def delete(run_id: str):
if not DB.delete_run(run_id):
raise Exception("Run not found")
@staticmethod
def get_all() -> List[ApiRun]:
db_runs = DB.list_runs()
return [_to_api_model(run) for run in db_runs]
@staticmethod
def search_for_runs(search_request: RunSearchRequest) -> List[ApiRun]:
filters = {}
if search_request.agent_id:
filters["agent_id"] = search_request.agent_id
if search_request.status:
filters["status"] = search_request.status
runs = DB.search_run(filters)
if search_request.metadata and len(search_request.metadata) > 0:
for run in enumerate(runs):
thread = get_thread(run["thread_id"])
if thread:
for key, value in search_request.metadata.items():
if (
thread["metadata"].get(key) is not None
and thread["metadata"].get(key) != value
):
runs.pop(run)
return list(
islice(islice(runs, search_request.offset, None), search_request.limit)
)
@staticmethod
async def resume(run_id: str, user_input: Dict[str, Any]) -> ApiRun:
run = DB.get_run(run_id)
if run is None:
raise ValueError("Run not found")
if run["status"] != "interrupted":
raise ValueError("Run is not in interrupted state")
if run.get("interrupt") is None:
raise ValueError(f"No interrupt found for run {run_id}")
interrupt = run["interrupt"]
interrupt["user_data"] = user_input
DB.update_run(run_id, {"interrupt": interrupt})
DB.update_run_info(run_id, {"attempts": 0, "queued_at": datetime.now()})
updated = DB.update_run_status(run_id, "pending")
await RUNS_QUEUE.put(updated["run_id"])
return _to_api_model(updated)
@staticmethod
async def set_status(run_id: str, status: RunStatus):
run = DB.get_run(run_id)
if not run:
raise Exception("Run not found")
DB.update_run_status(run_id, status)
if status != "pending":
async with cvs_pending_run[run_id]:
cvs_pending_run[run_id].notify_all()
@staticmethod
async def wait(run_id: str):
run_stream = Runs.Stream.join(run_id)
last_chunk = None
try:
async for event_data in run_stream:
output = event_data.data
last_chunk = output
except Exception as error:
raise error
return last_chunk
@staticmethod
async def wait_for_output(run_id: str, timeout: float = None):
run = DB.get_run(run_id)
if run is None:
return None, None
if run["status"] != "pending":
# If the run is already completed, return the stored output immediately
return _to_api_model(run), DB.get_run_output(run_id)
# TODO: handle removing cvs when run is completed and there are no more subscribers
try:
async with cvs_pending_run[run_id]:
await asyncio.wait_for(
cvs_pending_run[run_id].wait_for(
lambda: DB.get_run_status(run_id) != "pending"
),
timeout=timeout,
)
run = DB.get_run(run_id)
return _to_api_model(run), DB.get_run_output(run_id)
except asyncio.TimeoutError:
logger.warning(f"Timeout reached while waiting for run {run_id}")
raise TimeoutError
return None, None
@staticmethod
async def stream_events(run_id: str) -> AsyncIterator[StreamEventPayload | None]:
async for message in Runs.Stream.join(run_id):
msg_data = message.data
if message.type == "control":
if message.data == "done":
break
elif message.data == "timeout":
yield None
continue
else:
logger.error(
f'received unknown control message "{message.data}" in stream events for run: {run_id}'
)
continue
# We need to get the latest value to return
run = DB.get_run(run_id)
if run is None:
raise ValueError(f"Run {run_id} not found")
yield StreamEventPayload(
ValueRunResultUpdate(
type="values",
run_id=run["run_id"],
status=run["status"],
values=msg_data,
)
)
class Stream:
@staticmethod
async def publish(run_id: str, message: Message) -> None:
await stream_manager.put_message(run_id, message)
@staticmethod
async def subscribe(run_id: str) -> asyncio.Queue:
queue = await stream_manager.add_queue(run_id)
logger.debug(f"Subscribed to queue for run_id {run_id}")
return queue
@staticmethod
async def join(
run_id: str,
) -> AsyncGenerator[Message, None]:
queue = await Runs.Stream.subscribe(run_id)
# Check after subscribe whether the run is completed to
# avoid race condition.
run = DB.get_run(run_id)
if run is None:
raise ValueError(f"Run {run_id} not found")
if run["status"] != "pending" and queue.empty():
return
while True:
try:
message: Message = await asyncio.wait_for(queue.get(), timeout=10)
yield message
if message.type == "control" and message.data == "done":
break
except TimeoutError as error:
logger.error(f"Timeout waiting for run {run_id}: {error}")
yield Message(type="control", data="timeout")