agnt5 0.2.8a2__cp310-abi3-macosx_10_12_x86_64.whl

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.

Potentially problematic release.


This version of agnt5 might be problematic. Click here for more details.

agnt5/context.py ADDED
@@ -0,0 +1,84 @@
1
+ """Context implementation for AGNT5 SDK."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from typing import Any, Awaitable, Callable, Dict, List, Optional, TypeVar, Union
7
+
8
+ T = TypeVar("T")
9
+
10
+
11
+ class _CorrelationFilter(logging.Filter):
12
+ """Inject correlation IDs (run_id, trace_id, span_id) into every log record."""
13
+
14
+ def __init__(self, runtime_context: Any) -> None:
15
+ super().__init__()
16
+ self.runtime_context = runtime_context
17
+
18
+ def filter(self, record: logging.LogRecord) -> bool:
19
+ """Add correlation IDs as extra fields to the log record."""
20
+ record.run_id = self.runtime_context.run_id
21
+ if self.runtime_context.trace_id:
22
+ record.trace_id = self.runtime_context.trace_id
23
+ if self.runtime_context.span_id:
24
+ record.span_id = self.runtime_context.span_id
25
+ return True
26
+
27
+
28
+ class Context:
29
+ """
30
+ Base context providing common functionality.
31
+
32
+ Provides:
33
+ - Logging with correlation IDs
34
+ - Execution metadata (run_id, attempt)
35
+ - Runtime context for tracing
36
+
37
+ Extended by:
38
+ - FunctionContext: Minimal context for stateless functions
39
+ - WorkflowContext: Context for durable workflows
40
+ """
41
+
42
+ def __init__(
43
+ self,
44
+ run_id: str,
45
+ attempt: int = 0,
46
+ runtime_context: Optional[Any] = None,
47
+ ) -> None:
48
+ """
49
+ Initialize base context.
50
+
51
+ Args:
52
+ run_id: Unique execution identifier
53
+ attempt: Retry attempt number (0-indexed)
54
+ runtime_context: RuntimeContext for trace correlation
55
+ """
56
+ self._run_id = run_id
57
+ self._attempt = attempt
58
+ self._runtime_context = runtime_context
59
+
60
+ # Create logger with correlation
61
+ self._logger = logging.getLogger(f"agnt5.{run_id}")
62
+ from ._telemetry import setup_context_logger
63
+ setup_context_logger(self._logger)
64
+
65
+ if runtime_context:
66
+ self._logger.addFilter(_CorrelationFilter(runtime_context))
67
+
68
+ @property
69
+ def run_id(self) -> str:
70
+ """Unique execution identifier."""
71
+ return self._run_id
72
+
73
+ @property
74
+ def attempt(self) -> int:
75
+ """Current retry attempt (0-indexed)."""
76
+ return self._attempt
77
+
78
+ @property
79
+ def logger(self) -> logging.Logger:
80
+ """Full logger for .debug(), .warning(), .error(), etc."""
81
+ return self._logger
82
+
83
+
84
+