Thread Management¶
The thread management system in Bedrock Swarm handles the execution and coordination of agent tasks. It provides mechanisms for creating, managing, and monitoring threads of execution.
Class Documentation¶
A Thread manages a single conversation between a user and an agent.
The Thread is responsible for: 1. Maintaining conversation history 2. Processing messages through the agent 3. Handling tool executions 4. Recording all interactions 5. Managing runs and their states
| PARAMETER | DESCRIPTION |
|---|---|
agent
|
The agent that will process messages in this thread
TYPE:
|
Source code in src/bedrock_swarm/agency/thread.py
Attributes¶
thread_id: str
property
¶
Get the thread ID.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Thread ID
TYPE:
|
Functions¶
process_message(content: str) -> str
¶
Process a message through this thread.
This method: 1. Records the incoming message 2. Creates and tracks a new run 3. Processes it through the agent 4. Handles any tool executions 5. Records and returns the response
| PARAMETER | DESCRIPTION |
|---|---|
content
|
Message to process
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The final response text |
Source code in src/bedrock_swarm/agency/thread.py
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 | |
_execute_tools(tool_calls: List[ToolCall]) -> List[ToolOutput]
¶
Execute a list of tool calls.
Source code in src/bedrock_swarm/agency/thread.py
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 321 322 323 324 325 326 327 | |
_execute_single_tool(tool_call: ToolCall) -> ToolResult
¶
Execute a single tool call.
| PARAMETER | DESCRIPTION |
|---|---|
tool_call
|
The tool call to execute
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ToolResult
|
Result of the tool execution |
Source code in src/bedrock_swarm/agency/thread.py
_get_final_response(original_message: str, tool_outputs: List[ToolOutput]) -> Dict[str, str]
¶
Get final response after tool execution.
This method builds a comprehensive prompt that includes: 1. Recent conversation history for context 2. The original message 3. Tool execution results 4. Instructions for response formatting
Source code in src/bedrock_swarm/agency/thread.py
_record_message(role: str, content: str, metadata: Optional[Dict] = None) -> None
¶
Record a message in the thread history.
| PARAMETER | DESCRIPTION |
|---|---|
role
|
The role of the message sender (user/assistant/system)
TYPE:
|
content
|
The message content
TYPE:
|
metadata
|
Optional metadata about the message
TYPE:
|
Source code in src/bedrock_swarm/agency/thread.py
get_history() -> List[Message]
¶
Get the complete message history.
| RETURNS | DESCRIPTION |
|---|---|
List[Message]
|
List of all messages in chronological order |
get_last_message() -> Optional[Message]
¶
Get the most recent message.
| RETURNS | DESCRIPTION |
|---|---|
Optional[Message]
|
The last message or None if history is empty |
get_context_window(n: int = 5) -> List[Message]
¶
Get the n most recent messages.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Number of messages to return
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[Message]
|
List of up to n most recent messages |
Source code in src/bedrock_swarm/agency/thread.py
get_run(run_id: str) -> Optional[Run]
¶
Get a specific run by ID.
| PARAMETER | DESCRIPTION |
|---|---|
run_id
|
ID of the run to retrieve
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Optional[Run]
|
The run if found, None otherwise |
Source code in src/bedrock_swarm/agency/thread.py
get_current_run() -> Optional[Run]
¶
cancel_run(run_id: str) -> bool
¶
Cancel a specific run.
| PARAMETER | DESCRIPTION |
|---|---|
run_id
|
ID of the run to cancel
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if run was cancelled, False otherwise |
Source code in src/bedrock_swarm/agency/thread.py
Features¶
The thread system provides:
- Thread Creation:
- Dynamic thread creation
- Thread configuration
-
Resource allocation
-
Thread Management:
- Start/stop control
- Pause/resume
- Status monitoring
-
Resource cleanup
-
Thread Communication:
- Message passing
- Event handling
- State synchronization
Usage Examples¶
from bedrock_swarm.agency import Thread
# Create a thread
thread = Thread(
name="processing_thread",
target=process_function,
args=("input_data",),
kwargs={"option": "value"}
)
# Start the thread
await thread.start()
# Check thread status
status = await thread.get_status()
print(status) # Output: {"state": "running", "progress": 50}
# Send message to thread
await thread.send_message("update_config", {"param": "new_value"})
# Wait for completion
await thread.join()
Thread States¶
Threads can be in these states:
- Created: Initial state
- Running: Active execution
- Paused: Temporarily stopped
- Completed: Finished execution
- Failed: Error state
Error Handling¶
The thread system handles:
- Thread creation errors
- Execution failures
- Resource allocation errors
- Communication errors
- Cleanup failures
Implementation Details¶
The thread implementation includes:
- Async execution support
- Resource management
- Error recovery
- State persistence
- Performance monitoring
Thread Configuration¶
Threads can be configured with: