|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import random |
| 6 | +import uuid |
6 | 7 | from contextlib import contextmanager |
7 | | -from typing import Any, Mapping, Protocol, Type, cast |
| 8 | +from typing import Any, Mapping, Protocol, Type |
8 | 9 |
|
9 | 10 | from agents import CustomSpanData, custom_span, get_current_span, trace |
10 | 11 | from agents.tracing import ( |
11 | 12 | get_trace_provider, |
12 | 13 | ) |
13 | | -from agents.tracing.provider import DefaultTraceProvider |
14 | 14 | from agents.tracing.scope import Scope |
15 | | -from agents.tracing.spans import NoOpSpan, SpanImpl |
| 15 | +from agents.tracing.spans import NoOpSpan |
16 | 16 |
|
17 | 17 | import temporalio.activity |
18 | 18 | import temporalio.api.common.v1 |
@@ -284,15 +284,29 @@ async def execute_activity( |
284 | 284 | return await self.next.execute_activity(input) |
285 | 285 |
|
286 | 286 |
|
| 287 | +class RunIdRandom: |
| 288 | + """Random uuid generator seeded by the run id of the workflow. |
| 289 | + Doesn't currently support replay over reset correctly. |
| 290 | + """ |
| 291 | + |
| 292 | + def __init__(self): |
| 293 | + """Create a new random UUID generator.""" |
| 294 | + self._random = random.Random("OpenAIPlugin" + workflow.info().run_id) |
| 295 | + |
| 296 | + def uuid4(self) -> str: |
| 297 | + """Generate a random UUID.""" |
| 298 | + return uuid.UUID( |
| 299 | + bytes=random.getrandbits(16 * 8).to_bytes(16, "big"), version=4 |
| 300 | + ).hex[:24] |
| 301 | + |
| 302 | + |
287 | 303 | def _ensure_tracing_random() -> None: |
288 | 304 | instance = workflow.instance() |
289 | 305 | if not hasattr(instance, "__temporal_openai_tracing_random"): |
290 | | - new_random = random.Random() |
291 | | - new_random.setstate(workflow.random().getstate()) |
292 | 306 | setattr( |
293 | 307 | workflow.instance(), |
294 | 308 | "__temporal_openai_tracing_random", |
295 | | - new_random, |
| 309 | + RunIdRandom(), |
296 | 310 | ) |
297 | 311 |
|
298 | 312 |
|
@@ -376,6 +390,7 @@ def start_activity( |
376 | 390 | span = custom_span( |
377 | 391 | name="temporal:startActivity", data={"activity": input.activity} |
378 | 392 | ) |
| 393 | + span.finish() |
379 | 394 | span.start(mark_as_current=True) |
380 | 395 | set_header_from_context(input, temporalio.workflow.payload_converter()) |
381 | 396 | handle = self.next.start_activity(input) |
|
0 commit comments