agentrun-sdk 0.1.2__py3-none-any.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 agentrun-sdk might be problematic. Click here for more details.

Files changed (115) hide show
  1. agentrun_operation_sdk/cli/__init__.py +1 -0
  2. agentrun_operation_sdk/cli/cli.py +19 -0
  3. agentrun_operation_sdk/cli/common.py +21 -0
  4. agentrun_operation_sdk/cli/runtime/__init__.py +1 -0
  5. agentrun_operation_sdk/cli/runtime/commands.py +203 -0
  6. agentrun_operation_sdk/client/client.py +75 -0
  7. agentrun_operation_sdk/operations/runtime/__init__.py +8 -0
  8. agentrun_operation_sdk/operations/runtime/configure.py +101 -0
  9. agentrun_operation_sdk/operations/runtime/launch.py +82 -0
  10. agentrun_operation_sdk/operations/runtime/models.py +31 -0
  11. agentrun_operation_sdk/services/runtime.py +152 -0
  12. agentrun_operation_sdk/utils/logging_config.py +72 -0
  13. agentrun_operation_sdk/utils/runtime/config.py +94 -0
  14. agentrun_operation_sdk/utils/runtime/container.py +280 -0
  15. agentrun_operation_sdk/utils/runtime/entrypoint.py +203 -0
  16. agentrun_operation_sdk/utils/runtime/schema.py +56 -0
  17. agentrun_sdk/__init__.py +7 -0
  18. agentrun_sdk/agent/__init__.py +25 -0
  19. agentrun_sdk/agent/agent.py +696 -0
  20. agentrun_sdk/agent/agent_result.py +46 -0
  21. agentrun_sdk/agent/conversation_manager/__init__.py +26 -0
  22. agentrun_sdk/agent/conversation_manager/conversation_manager.py +88 -0
  23. agentrun_sdk/agent/conversation_manager/null_conversation_manager.py +46 -0
  24. agentrun_sdk/agent/conversation_manager/sliding_window_conversation_manager.py +179 -0
  25. agentrun_sdk/agent/conversation_manager/summarizing_conversation_manager.py +252 -0
  26. agentrun_sdk/agent/state.py +97 -0
  27. agentrun_sdk/event_loop/__init__.py +9 -0
  28. agentrun_sdk/event_loop/event_loop.py +499 -0
  29. agentrun_sdk/event_loop/streaming.py +319 -0
  30. agentrun_sdk/experimental/__init__.py +4 -0
  31. agentrun_sdk/experimental/hooks/__init__.py +15 -0
  32. agentrun_sdk/experimental/hooks/events.py +123 -0
  33. agentrun_sdk/handlers/__init__.py +10 -0
  34. agentrun_sdk/handlers/callback_handler.py +70 -0
  35. agentrun_sdk/hooks/__init__.py +49 -0
  36. agentrun_sdk/hooks/events.py +80 -0
  37. agentrun_sdk/hooks/registry.py +247 -0
  38. agentrun_sdk/models/__init__.py +10 -0
  39. agentrun_sdk/models/anthropic.py +432 -0
  40. agentrun_sdk/models/bedrock.py +649 -0
  41. agentrun_sdk/models/litellm.py +225 -0
  42. agentrun_sdk/models/llamaapi.py +438 -0
  43. agentrun_sdk/models/mistral.py +539 -0
  44. agentrun_sdk/models/model.py +95 -0
  45. agentrun_sdk/models/ollama.py +357 -0
  46. agentrun_sdk/models/openai.py +436 -0
  47. agentrun_sdk/models/sagemaker.py +598 -0
  48. agentrun_sdk/models/writer.py +449 -0
  49. agentrun_sdk/multiagent/__init__.py +22 -0
  50. agentrun_sdk/multiagent/a2a/__init__.py +15 -0
  51. agentrun_sdk/multiagent/a2a/executor.py +148 -0
  52. agentrun_sdk/multiagent/a2a/server.py +252 -0
  53. agentrun_sdk/multiagent/base.py +92 -0
  54. agentrun_sdk/multiagent/graph.py +555 -0
  55. agentrun_sdk/multiagent/swarm.py +656 -0
  56. agentrun_sdk/py.typed +1 -0
  57. agentrun_sdk/session/__init__.py +18 -0
  58. agentrun_sdk/session/file_session_manager.py +216 -0
  59. agentrun_sdk/session/repository_session_manager.py +152 -0
  60. agentrun_sdk/session/s3_session_manager.py +272 -0
  61. agentrun_sdk/session/session_manager.py +73 -0
  62. agentrun_sdk/session/session_repository.py +51 -0
  63. agentrun_sdk/telemetry/__init__.py +21 -0
  64. agentrun_sdk/telemetry/config.py +194 -0
  65. agentrun_sdk/telemetry/metrics.py +476 -0
  66. agentrun_sdk/telemetry/metrics_constants.py +15 -0
  67. agentrun_sdk/telemetry/tracer.py +563 -0
  68. agentrun_sdk/tools/__init__.py +17 -0
  69. agentrun_sdk/tools/decorator.py +569 -0
  70. agentrun_sdk/tools/executor.py +137 -0
  71. agentrun_sdk/tools/loader.py +152 -0
  72. agentrun_sdk/tools/mcp/__init__.py +13 -0
  73. agentrun_sdk/tools/mcp/mcp_agent_tool.py +99 -0
  74. agentrun_sdk/tools/mcp/mcp_client.py +423 -0
  75. agentrun_sdk/tools/mcp/mcp_instrumentation.py +322 -0
  76. agentrun_sdk/tools/mcp/mcp_types.py +63 -0
  77. agentrun_sdk/tools/registry.py +607 -0
  78. agentrun_sdk/tools/structured_output.py +421 -0
  79. agentrun_sdk/tools/tools.py +217 -0
  80. agentrun_sdk/tools/watcher.py +136 -0
  81. agentrun_sdk/types/__init__.py +5 -0
  82. agentrun_sdk/types/collections.py +23 -0
  83. agentrun_sdk/types/content.py +188 -0
  84. agentrun_sdk/types/event_loop.py +48 -0
  85. agentrun_sdk/types/exceptions.py +81 -0
  86. agentrun_sdk/types/guardrails.py +254 -0
  87. agentrun_sdk/types/media.py +89 -0
  88. agentrun_sdk/types/session.py +152 -0
  89. agentrun_sdk/types/streaming.py +201 -0
  90. agentrun_sdk/types/tools.py +258 -0
  91. agentrun_sdk/types/traces.py +5 -0
  92. agentrun_sdk-0.1.2.dist-info/METADATA +51 -0
  93. agentrun_sdk-0.1.2.dist-info/RECORD +115 -0
  94. agentrun_sdk-0.1.2.dist-info/WHEEL +5 -0
  95. agentrun_sdk-0.1.2.dist-info/entry_points.txt +2 -0
  96. agentrun_sdk-0.1.2.dist-info/top_level.txt +3 -0
  97. agentrun_wrapper/__init__.py +11 -0
  98. agentrun_wrapper/_utils/__init__.py +6 -0
  99. agentrun_wrapper/_utils/endpoints.py +16 -0
  100. agentrun_wrapper/identity/__init__.py +5 -0
  101. agentrun_wrapper/identity/auth.py +211 -0
  102. agentrun_wrapper/memory/__init__.py +6 -0
  103. agentrun_wrapper/memory/client.py +1697 -0
  104. agentrun_wrapper/memory/constants.py +103 -0
  105. agentrun_wrapper/memory/controlplane.py +626 -0
  106. agentrun_wrapper/py.typed +1 -0
  107. agentrun_wrapper/runtime/__init__.py +13 -0
  108. agentrun_wrapper/runtime/app.py +473 -0
  109. agentrun_wrapper/runtime/context.py +34 -0
  110. agentrun_wrapper/runtime/models.py +25 -0
  111. agentrun_wrapper/services/__init__.py +1 -0
  112. agentrun_wrapper/services/identity.py +192 -0
  113. agentrun_wrapper/tools/__init__.py +6 -0
  114. agentrun_wrapper/tools/browser_client.py +325 -0
  115. agentrun_wrapper/tools/code_interpreter_client.py +186 -0
@@ -0,0 +1,254 @@
1
+ """Guardrail-related type definitions for the SDK.
2
+
3
+ These types are modeled after the Bedrock API.
4
+
5
+ - Bedrock docs: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Types_Amazon_Bedrock_Runtime.html
6
+ """
7
+
8
+ from typing import Dict, List, Literal, Optional
9
+
10
+ from typing_extensions import TypedDict
11
+
12
+
13
+ class GuardrailConfig(TypedDict, total=False):
14
+ """Configuration for content filtering guardrails.
15
+
16
+ Attributes:
17
+ guardrailIdentifier: Unique identifier for the guardrail.
18
+ guardrailVersion: Version of the guardrail to apply.
19
+ streamProcessingMode: Processing mode.
20
+ trace: The trace behavior for the guardrail.
21
+ """
22
+
23
+ guardrailIdentifier: str
24
+ guardrailVersion: str
25
+ streamProcessingMode: Optional[Literal["sync", "async"]]
26
+ trace: Literal["enabled", "disabled"]
27
+
28
+
29
+ class Topic(TypedDict):
30
+ """Information about a topic guardrail.
31
+
32
+ Attributes:
33
+ action: The action the guardrail should take when it intervenes on a topic.
34
+ name: The name for the guardrail.
35
+ type: The type behavior that the guardrail should perform when the model detects the topic.
36
+ """
37
+
38
+ action: Literal["BLOCKED"]
39
+ name: str
40
+ type: Literal["DENY"]
41
+
42
+
43
+ class TopicPolicy(TypedDict):
44
+ """A behavior assessment of a topic policy.
45
+
46
+ Attributes:
47
+ topics: The topics in the assessment.
48
+ """
49
+
50
+ topics: List[Topic]
51
+
52
+
53
+ class ContentFilter(TypedDict):
54
+ """The content filter for a guardrail.
55
+
56
+ Attributes:
57
+ action: Action to take when content is detected.
58
+ confidence: Confidence level of the detection.
59
+ type: The type of content to filter.
60
+ """
61
+
62
+ action: Literal["BLOCKED"]
63
+ confidence: Literal["NONE", "LOW", "MEDIUM", "HIGH"]
64
+ type: Literal["INSULTS", "HATE", "SEXUAL", "VIOLENCE", "MISCONDUCT", "PROMPT_ATTACK"]
65
+
66
+
67
+ class ContentPolicy(TypedDict):
68
+ """An assessment of a content policy for a guardrail.
69
+
70
+ Attributes:
71
+ filters: List of content filters to apply.
72
+ """
73
+
74
+ filters: List[ContentFilter]
75
+
76
+
77
+ class CustomWord(TypedDict):
78
+ """Definition of a custom word to be filtered.
79
+
80
+ Attributes:
81
+ action: Action to take when the word is detected.
82
+ match: The word or phrase to match.
83
+ """
84
+
85
+ action: Literal["BLOCKED"]
86
+ match: str
87
+
88
+
89
+ class ManagedWord(TypedDict):
90
+ """Definition of a managed word to be filtered.
91
+
92
+ Attributes:
93
+ action: Action to take when the word is detected.
94
+ match: The word or phrase to match.
95
+ type: Type of the word.
96
+ """
97
+
98
+ action: Literal["BLOCKED"]
99
+ match: str
100
+ type: Literal["PROFANITY"]
101
+
102
+
103
+ class WordPolicy(TypedDict):
104
+ """The word policy assessment.
105
+
106
+ Attributes:
107
+ customWords: List of custom words to filter.
108
+ managedWordLists: List of managed word lists to filter.
109
+ """
110
+
111
+ customWords: List[CustomWord]
112
+ managedWordLists: List[ManagedWord]
113
+
114
+
115
+ class PIIEntity(TypedDict):
116
+ """Definition of a Personally Identifiable Information (PII) entity to be filtered.
117
+
118
+ Attributes:
119
+ action: Action to take when PII is detected.
120
+ match: The specific PII instance to match.
121
+ type: The type of PII to detect.
122
+ """
123
+
124
+ action: Literal["ANONYMIZED", "BLOCKED"]
125
+ match: str
126
+ type: Literal[
127
+ "ADDRESS",
128
+ "AGE",
129
+ "AWS_ACCESS_KEY",
130
+ "AWS_SECRET_KEY",
131
+ "CA_HEALTH_NUMBER",
132
+ "CA_SOCIAL_INSURANCE_NUMBER",
133
+ "CREDIT_DEBIT_CARD_CVV",
134
+ "CREDIT_DEBIT_CARD_EXPIRY",
135
+ "CREDIT_DEBIT_CARD_NUMBER",
136
+ "DRIVER_ID",
137
+ "EMAIL",
138
+ "INTERNATIONAL_BANK_ACCOUNT_NUMBER",
139
+ "IP_ADDRESS",
140
+ "LICENSE_PLATE",
141
+ "MAC_ADDRESS",
142
+ "NAME",
143
+ "PASSWORD",
144
+ "PHONE",
145
+ "PIN",
146
+ "SWIFT_CODE",
147
+ "UK_NATIONAL_HEALTH_SERVICE_NUMBER",
148
+ "UK_NATIONAL_INSURANCE_NUMBER",
149
+ "UK_UNIQUE_TAXPAYER_REFERENCE_NUMBER",
150
+ "URL",
151
+ "USERNAME",
152
+ "US_BANK_ACCOUNT_NUMBER",
153
+ "US_BANK_ROUTING_NUMBER",
154
+ "US_INDIVIDUAL_TAX_IDENTIFICATION_NUMBER",
155
+ "US_PASSPORT_NUMBER",
156
+ "US_SOCIAL_SECURITY_NUMBER",
157
+ "VEHICLE_IDENTIFICATION_NUMBER",
158
+ ]
159
+
160
+
161
+ class Regex(TypedDict):
162
+ """Definition of a custom regex pattern for filtering sensitive information.
163
+
164
+ Attributes:
165
+ action: Action to take when the pattern is matched.
166
+ match: The regex filter match.
167
+ name: Name of the regex pattern for identification.
168
+ regex: The regex query.
169
+ """
170
+
171
+ action: Literal["ANONYMIZED", "BLOCKED"]
172
+ match: str
173
+ name: str
174
+ regex: str
175
+
176
+
177
+ class SensitiveInformationPolicy(TypedDict):
178
+ """Policy defining sensitive information filtering rules.
179
+
180
+ Attributes:
181
+ piiEntities: List of Personally Identifiable Information (PII) entities to detect and handle.
182
+ regexes: The regex queries in the assessment.
183
+ """
184
+
185
+ piiEntities: List[PIIEntity]
186
+ regexes: List[Regex]
187
+
188
+
189
+ class ContextualGroundingFilter(TypedDict):
190
+ """Filter for ensuring responses are grounded in provided context.
191
+
192
+ Attributes:
193
+ action: Action to take when the threshold is not met.
194
+ score: The score generated by contextual grounding filter (range [0, 1]).
195
+ threshold: Threshold used by contextual grounding filter to determine whether the content is grounded or not.
196
+ type: The contextual grounding filter type.
197
+ """
198
+
199
+ action: Literal["BLOCKED", "NONE"]
200
+ score: float
201
+ threshold: float
202
+ type: Literal["GROUNDING", "RELEVANCE"]
203
+
204
+
205
+ class ContextualGroundingPolicy(TypedDict):
206
+ """The policy assessment details for the guardrails contextual grounding filter.
207
+
208
+ Attributes:
209
+ filters: The filter details for the guardrails contextual grounding filter.
210
+ """
211
+
212
+ filters: List[ContextualGroundingFilter]
213
+
214
+
215
+ class GuardrailAssessment(TypedDict):
216
+ """A behavior assessment of the guardrail policies used in a call to the Converse API.
217
+
218
+ Attributes:
219
+ contentPolicy: The content policy.
220
+ contextualGroundingPolicy: The contextual grounding policy used for the guardrail assessment.
221
+ sensitiveInformationPolicy: The sensitive information policy.
222
+ topicPolicy: The topic policy.
223
+ wordPolicy: The word policy.
224
+ """
225
+
226
+ contentPolicy: ContentPolicy
227
+ contextualGroundingPolicy: ContextualGroundingPolicy
228
+ sensitiveInformationPolicy: SensitiveInformationPolicy
229
+ topicPolicy: TopicPolicy
230
+ wordPolicy: WordPolicy
231
+
232
+
233
+ class GuardrailTrace(TypedDict):
234
+ """Trace information from guardrail processing.
235
+
236
+ Attributes:
237
+ inputAssessment: Assessment of input content against guardrail policies, keyed by input identifier.
238
+ modelOutput: The original output from the model before guardrail processing.
239
+ outputAssessments: Assessments of output content against guardrail policies, keyed by output identifier.
240
+ """
241
+
242
+ inputAssessment: Dict[str, GuardrailAssessment]
243
+ modelOutput: List[str]
244
+ outputAssessments: Dict[str, List[GuardrailAssessment]]
245
+
246
+
247
+ class Trace(TypedDict):
248
+ """A Top level guardrail trace object.
249
+
250
+ Attributes:
251
+ guardrail: Trace information from guardrail processing.
252
+ """
253
+
254
+ guardrail: GuardrailTrace
@@ -0,0 +1,89 @@
1
+ """Media-related type definitions for the SDK.
2
+
3
+ These types are modeled after the Bedrock API.
4
+
5
+ - Bedrock docs: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Types_Amazon_Bedrock_Runtime.html
6
+ """
7
+
8
+ from typing import Literal
9
+
10
+ from typing_extensions import TypedDict
11
+
12
+ DocumentFormat = Literal["pdf", "csv", "doc", "docx", "xls", "xlsx", "html", "txt", "md"]
13
+ """Supported document formats."""
14
+
15
+
16
+ class DocumentSource(TypedDict):
17
+ """Contains the content of a document.
18
+
19
+ Attributes:
20
+ bytes: The binary content of the document.
21
+ """
22
+
23
+ bytes: bytes
24
+
25
+
26
+ class DocumentContent(TypedDict):
27
+ """A document to include in a message.
28
+
29
+ Attributes:
30
+ format: The format of the document (e.g., "pdf", "txt").
31
+ name: The name of the document.
32
+ source: The source containing the document's binary content.
33
+ """
34
+
35
+ format: Literal["pdf", "csv", "doc", "docx", "xls", "xlsx", "html", "txt", "md"]
36
+ name: str
37
+ source: DocumentSource
38
+
39
+
40
+ ImageFormat = Literal["png", "jpeg", "gif", "webp"]
41
+ """Supported image formats."""
42
+
43
+
44
+ class ImageSource(TypedDict):
45
+ """Contains the content of an image.
46
+
47
+ Attributes:
48
+ bytes: The binary content of the image.
49
+ """
50
+
51
+ bytes: bytes
52
+
53
+
54
+ class ImageContent(TypedDict):
55
+ """An image to include in a message.
56
+
57
+ Attributes:
58
+ format: The format of the image (e.g., "png", "jpeg").
59
+ source: The source containing the image's binary content.
60
+ """
61
+
62
+ format: ImageFormat
63
+ source: ImageSource
64
+
65
+
66
+ VideoFormat = Literal["flv", "mkv", "mov", "mpeg", "mpg", "mp4", "three_gp", "webm", "wmv"]
67
+ """Supported video formats."""
68
+
69
+
70
+ class VideoSource(TypedDict):
71
+ """Contains the content of a video.
72
+
73
+ Attributes:
74
+ bytes: The binary content of the video.
75
+ """
76
+
77
+ bytes: bytes
78
+
79
+
80
+ class VideoContent(TypedDict):
81
+ """A video to include in a message.
82
+
83
+ Attributes:
84
+ format: The format of the video (e.g., "mp4", "avi").
85
+ source: The source containing the video's binary content.
86
+ """
87
+
88
+ format: VideoFormat
89
+ source: VideoSource
@@ -0,0 +1,152 @@
1
+ """Data models for session management."""
2
+
3
+ import base64
4
+ import inspect
5
+ from dataclasses import asdict, dataclass, field
6
+ from datetime import datetime, timezone
7
+ from enum import Enum
8
+ from typing import TYPE_CHECKING, Any, Dict, Optional
9
+
10
+ from .content import Message
11
+
12
+ if TYPE_CHECKING:
13
+ from ..agent.agent import Agent
14
+
15
+
16
+ class SessionType(str, Enum):
17
+ """Enumeration of session types.
18
+
19
+ As sessions are expanded to support new usecases like multi-agent patterns,
20
+ new types will be added here.
21
+ """
22
+
23
+ AGENT = "AGENT"
24
+
25
+
26
+ def encode_bytes_values(obj: Any) -> Any:
27
+ """Recursively encode any bytes values in an object to base64.
28
+
29
+ Handles dictionaries, lists, and nested structures.
30
+ """
31
+ if isinstance(obj, bytes):
32
+ return {"__bytes_encoded__": True, "data": base64.b64encode(obj).decode()}
33
+ elif isinstance(obj, dict):
34
+ return {k: encode_bytes_values(v) for k, v in obj.items()}
35
+ elif isinstance(obj, list):
36
+ return [encode_bytes_values(item) for item in obj]
37
+ else:
38
+ return obj
39
+
40
+
41
+ def decode_bytes_values(obj: Any) -> Any:
42
+ """Recursively decode any base64-encoded bytes values in an object.
43
+
44
+ Handles dictionaries, lists, and nested structures.
45
+ """
46
+ if isinstance(obj, dict):
47
+ if obj.get("__bytes_encoded__") is True and "data" in obj:
48
+ return base64.b64decode(obj["data"])
49
+ return {k: decode_bytes_values(v) for k, v in obj.items()}
50
+ elif isinstance(obj, list):
51
+ return [decode_bytes_values(item) for item in obj]
52
+ else:
53
+ return obj
54
+
55
+
56
+ @dataclass
57
+ class SessionMessage:
58
+ """Message within a SessionAgent.
59
+
60
+ Attributes:
61
+ message: Message content
62
+ message_id: Index of the message in the conversation history
63
+ redact_message: If the original message is redacted, this is the new content to use
64
+ created_at: ISO format timestamp for when this message was created
65
+ updated_at: ISO format timestamp for when this message was last updated
66
+ """
67
+
68
+ message: Message
69
+ message_id: int
70
+ redact_message: Optional[Message] = None
71
+ created_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
72
+ updated_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
73
+
74
+ @classmethod
75
+ def from_message(cls, message: Message, index: int) -> "SessionMessage":
76
+ """Convert from a Message, base64 encoding bytes values."""
77
+ return cls(
78
+ message=message,
79
+ message_id=index,
80
+ created_at=datetime.now(timezone.utc).isoformat(),
81
+ updated_at=datetime.now(timezone.utc).isoformat(),
82
+ )
83
+
84
+ def to_message(self) -> Message:
85
+ """Convert SessionMessage back to a Message, decoding any bytes values.
86
+
87
+ If the message was redacted, return the redact content instead.
88
+ """
89
+ if self.redact_message is not None:
90
+ return self.redact_message
91
+ else:
92
+ return self.message
93
+
94
+ @classmethod
95
+ def from_dict(cls, env: dict[str, Any]) -> "SessionMessage":
96
+ """Initialize a SessionMessage from a dictionary, ignoring keys that are not class parameters."""
97
+ extracted_relevant_parameters = {k: v for k, v in env.items() if k in inspect.signature(cls).parameters}
98
+ return cls(**decode_bytes_values(extracted_relevant_parameters))
99
+
100
+ def to_dict(self) -> dict[str, Any]:
101
+ """Convert the SessionMessage to a dictionary representation."""
102
+ return encode_bytes_values(asdict(self)) # type: ignore
103
+
104
+
105
+ @dataclass
106
+ class SessionAgent:
107
+ """Agent that belongs to a Session."""
108
+
109
+ agent_id: str
110
+ state: Dict[str, Any]
111
+ conversation_manager_state: Dict[str, Any]
112
+ created_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
113
+ updated_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
114
+
115
+ @classmethod
116
+ def from_agent(cls, agent: "Agent") -> "SessionAgent":
117
+ """Convert an Agent to a SessionAgent."""
118
+ if agent.agent_id is None:
119
+ raise ValueError("agent_id needs to be defined.")
120
+ return cls(
121
+ agent_id=agent.agent_id,
122
+ conversation_manager_state=agent.conversation_manager.get_state(),
123
+ state=agent.state.get(),
124
+ )
125
+
126
+ @classmethod
127
+ def from_dict(cls, env: dict[str, Any]) -> "SessionAgent":
128
+ """Initialize a SessionAgent from a dictionary, ignoring keys that are not class parameters."""
129
+ return cls(**{k: v for k, v in env.items() if k in inspect.signature(cls).parameters})
130
+
131
+ def to_dict(self) -> dict[str, Any]:
132
+ """Convert the SessionAgent to a dictionary representation."""
133
+ return asdict(self)
134
+
135
+
136
+ @dataclass
137
+ class Session:
138
+ """Session data model."""
139
+
140
+ session_id: str
141
+ session_type: SessionType
142
+ created_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
143
+ updated_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
144
+
145
+ @classmethod
146
+ def from_dict(cls, env: dict[str, Any]) -> "Session":
147
+ """Initialize a Session from a dictionary, ignoring keys that are not class parameters."""
148
+ return cls(**{k: v for k, v in env.items() if k in inspect.signature(cls).parameters})
149
+
150
+ def to_dict(self) -> dict[str, Any]:
151
+ """Convert the Session to a dictionary representation."""
152
+ return asdict(self)
@@ -0,0 +1,201 @@
1
+ """Streaming-related type definitions for the SDK.
2
+
3
+ These types are modeled after the Bedrock API.
4
+
5
+ - Bedrock docs: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Types_Amazon_Bedrock_Runtime.html
6
+ """
7
+
8
+ from typing import Optional, Union
9
+
10
+ from typing_extensions import TypedDict
11
+
12
+ from .content import ContentBlockStart, Role
13
+ from .event_loop import Metrics, StopReason, Usage
14
+ from .guardrails import Trace
15
+
16
+
17
+ class MessageStartEvent(TypedDict):
18
+ """Event signaling the start of a message in a streaming response.
19
+
20
+ Attributes:
21
+ role: The role of the message sender (e.g., "assistant", "user").
22
+ """
23
+
24
+ role: Role
25
+
26
+
27
+ class ContentBlockStartEvent(TypedDict, total=False):
28
+ """Event signaling the start of a content block in a streaming response.
29
+
30
+ Attributes:
31
+ contentBlockIndex: Index of the content block within the message.
32
+ This is optional to accommodate different model providers.
33
+ start: Information about the content block being started.
34
+ """
35
+
36
+ contentBlockIndex: Optional[int]
37
+ start: ContentBlockStart
38
+
39
+
40
+ class ContentBlockDeltaText(TypedDict):
41
+ """Text content delta in a streaming response.
42
+
43
+ Attributes:
44
+ text: The text fragment being streamed.
45
+ """
46
+
47
+ text: str
48
+
49
+
50
+ class ContentBlockDeltaToolUse(TypedDict):
51
+ """Tool use input delta in a streaming response.
52
+
53
+ Attributes:
54
+ input: The tool input fragment being streamed.
55
+ """
56
+
57
+ input: str
58
+
59
+
60
+ class ReasoningContentBlockDelta(TypedDict, total=False):
61
+ """Delta for reasoning content block in a streaming response.
62
+
63
+ Attributes:
64
+ redactedContent: The content in the reasoning that was encrypted by the model provider for safety reasons.
65
+ signature: A token that verifies that the reasoning text was generated by the model.
66
+ text: The reasoning that the model used to return the output.
67
+ """
68
+
69
+ redactedContent: Optional[bytes]
70
+ signature: Optional[str]
71
+ text: Optional[str]
72
+
73
+
74
+ class ContentBlockDelta(TypedDict, total=False):
75
+ """A block of content in a streaming response.
76
+
77
+ Attributes:
78
+ reasoningContent: Contains content regarding the reasoning that is carried out by the model.
79
+ text: Text fragment being streamed.
80
+ toolUse: Tool use input fragment being streamed.
81
+ """
82
+
83
+ reasoningContent: ReasoningContentBlockDelta
84
+ text: str
85
+ toolUse: ContentBlockDeltaToolUse
86
+
87
+
88
+ class ContentBlockDeltaEvent(TypedDict, total=False):
89
+ """Event containing a delta update for a content block in a streaming response.
90
+
91
+ Attributes:
92
+ contentBlockIndex: Index of the content block within the message.
93
+ This is optional to accommodate different model providers.
94
+ delta: The incremental content update for the content block.
95
+ """
96
+
97
+ contentBlockIndex: Optional[int]
98
+ delta: ContentBlockDelta
99
+
100
+
101
+ class ContentBlockStopEvent(TypedDict, total=False):
102
+ """Event signaling the end of a content block in a streaming response.
103
+
104
+ Attributes:
105
+ contentBlockIndex: Index of the content block within the message.
106
+ This is optional to accommodate different model providers.
107
+ """
108
+
109
+ contentBlockIndex: Optional[int]
110
+
111
+
112
+ class MessageStopEvent(TypedDict, total=False):
113
+ """Event signaling the end of a message in a streaming response.
114
+
115
+ Attributes:
116
+ additionalModelResponseFields: Additional fields to include in model response.
117
+ stopReason: The reason why the model stopped generating content.
118
+ """
119
+
120
+ additionalModelResponseFields: Optional[Union[dict, list, int, float, str, bool, None]]
121
+ stopReason: StopReason
122
+
123
+
124
+ class MetadataEvent(TypedDict, total=False):
125
+ """Event containing metadata about the streaming response.
126
+
127
+ Attributes:
128
+ metrics: Performance metrics related to the model invocation.
129
+ trace: Trace information for debugging and monitoring.
130
+ usage: Resource usage information for the model invocation.
131
+ """
132
+
133
+ metrics: Metrics
134
+ trace: Optional[Trace]
135
+ usage: Usage
136
+
137
+
138
+ class ExceptionEvent(TypedDict):
139
+ """Base event for exceptions in a streaming response.
140
+
141
+ Attributes:
142
+ message: The error message describing what went wrong.
143
+ """
144
+
145
+ message: str
146
+
147
+
148
+ class ModelStreamErrorEvent(ExceptionEvent):
149
+ """Event for model streaming errors.
150
+
151
+ Attributes:
152
+ originalMessage: The original error message from the model provider.
153
+ originalStatusCode: The HTTP status code returned by the model provider.
154
+ """
155
+
156
+ originalMessage: str
157
+ originalStatusCode: int
158
+
159
+
160
+ class RedactContentEvent(TypedDict, total=False):
161
+ """Event for redacting content.
162
+
163
+ Attributes:
164
+ redactUserContentMessage: The string to overwrite the users input with.
165
+ redactAssistantContentMessage: The string to overwrite the assistants output with.
166
+
167
+ """
168
+
169
+ redactUserContentMessage: Optional[str]
170
+ redactAssistantContentMessage: Optional[str]
171
+
172
+
173
+ class StreamEvent(TypedDict, total=False):
174
+ """The messages output stream.
175
+
176
+ Attributes:
177
+ contentBlockDelta: Delta content for a content block.
178
+ contentBlockStart: Start of a content block.
179
+ contentBlockStop: End of a content block.
180
+ internalServerException: Internal server error information.
181
+ messageStart: Start of a message.
182
+ messageStop: End of a message.
183
+ metadata: Metadata about the streaming response.
184
+ modelStreamErrorException: Model streaming error information.
185
+ serviceUnavailableException: Service unavailable error information.
186
+ throttlingException: Throttling error information.
187
+ validationException: Validation error information.
188
+ """
189
+
190
+ contentBlockDelta: ContentBlockDeltaEvent
191
+ contentBlockStart: ContentBlockStartEvent
192
+ contentBlockStop: ContentBlockStopEvent
193
+ internalServerException: ExceptionEvent
194
+ messageStart: MessageStartEvent
195
+ messageStop: MessageStopEvent
196
+ metadata: MetadataEvent
197
+ redactContent: RedactContentEvent
198
+ modelStreamErrorException: ModelStreamErrorEvent
199
+ serviceUnavailableException: ExceptionEvent
200
+ throttlingException: ExceptionEvent
201
+ validationException: ExceptionEvent