Spaces:
Sleeping
Sleeping
File size: 13,632 Bytes
bb3ee41 | 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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | """Shared memory for multi-agent communication and state sharing."""
from __future__ import annotations
import asyncio
import logging
from collections import defaultdict
from datetime import datetime
from typing import Any, Callable, Awaitable
from uuid import uuid4
from pydantic import BaseModel, Field
logger = logging.getLogger(__name__)
# Type alias for async callback functions
MessageCallback = Callable[[Any], Awaitable[None]]
class Message(BaseModel):
"""A message published to a channel."""
id: str = Field(default_factory=lambda: str(uuid4()))
channel: str
payload: Any
sender: str | None = None
timestamp: datetime = Field(default_factory=datetime.utcnow)
metadata: dict[str, Any] = Field(default_factory=dict)
model_config = {"arbitrary_types_allowed": True}
class Subscription(BaseModel):
"""A subscription to a channel."""
id: str = Field(default_factory=lambda: str(uuid4()))
channel: str
subscriber_id: str
created_at: datetime = Field(default_factory=datetime.utcnow)
model_config = {"arbitrary_types_allowed": True}
class Channel:
"""A named channel for pub/sub communication."""
def __init__(self, name: str, max_history: int = 100) -> None:
"""
Initialize a channel.
Args:
name: Channel name.
max_history: Maximum number of messages to retain in history.
"""
self.name = name
self.max_history = max_history
self._subscribers: dict[str, MessageCallback] = {}
self._history: list[Message] = []
self._lock = asyncio.Lock()
@property
def subscriber_count(self) -> int:
"""Get the number of subscribers."""
return len(self._subscribers)
async def publish(self, message: Message) -> int:
"""
Publish a message to all subscribers.
Args:
message: Message to publish.
Returns:
Number of subscribers that received the message.
"""
async with self._lock:
# Add to history
self._history.append(message)
if len(self._history) > self.max_history:
self._history = self._history[-self.max_history:]
# Notify subscribers
notified = 0
for sub_id, callback in list(self._subscribers.items()):
try:
await callback(message)
notified += 1
except Exception as e:
logger.error(f"Error notifying subscriber {sub_id}: {e}")
return notified
async def subscribe(
self,
subscriber_id: str,
callback: MessageCallback,
) -> Subscription:
"""
Subscribe to the channel.
Args:
subscriber_id: Unique identifier for the subscriber.
callback: Async callback function to receive messages.
Returns:
Subscription object.
"""
async with self._lock:
self._subscribers[subscriber_id] = callback
return Subscription(
channel=self.name,
subscriber_id=subscriber_id,
)
async def unsubscribe(self, subscriber_id: str) -> bool:
"""
Unsubscribe from the channel.
Args:
subscriber_id: Subscriber to remove.
Returns:
True if subscriber was found and removed.
"""
async with self._lock:
if subscriber_id in self._subscribers:
del self._subscribers[subscriber_id]
return True
return False
async def get_history(
self,
limit: int | None = None,
since: datetime | None = None,
) -> list[Message]:
"""
Get channel message history.
Args:
limit: Maximum number of messages to return.
since: Only return messages after this timestamp.
Returns:
List of historical messages.
"""
async with self._lock:
messages = self._history
if since:
messages = [m for m in messages if m.timestamp > since]
if limit:
messages = messages[-limit:]
return messages
async def clear_history(self) -> int:
"""
Clear the channel's message history.
Returns:
Number of messages cleared.
"""
async with self._lock:
count = len(self._history)
self._history.clear()
return count
class SharedMemory:
"""
Thread-safe shared memory for multi-agent coordination.
This memory layer provides pub/sub messaging and shared state storage
for coordination between multiple agents. All operations are thread-safe.
Attributes:
_channels: Dictionary of channels by name.
_state: Shared key-value state store.
"""
def __init__(self, max_channel_history: int = 100) -> None:
"""
Initialize shared memory.
Args:
max_channel_history: Maximum history per channel.
"""
self.max_channel_history = max_channel_history
self._channels: dict[str, Channel] = {}
self._state: dict[str, Any] = {}
self._state_lock = asyncio.Lock()
self._channel_lock = asyncio.Lock()
self._queues: dict[str, dict[str, asyncio.Queue]] = defaultdict(dict)
async def get_channel(self, name: str) -> Channel:
"""
Get or create a channel by name.
Args:
name: Channel name.
Returns:
The channel object.
"""
async with self._channel_lock:
if name not in self._channels:
self._channels[name] = Channel(
name=name,
max_history=self.max_channel_history,
)
return self._channels[name]
async def publish(
self,
channel: str,
payload: Any,
sender: str | None = None,
metadata: dict[str, Any] | None = None,
) -> Message:
"""
Publish a message to a channel.
Args:
channel: Channel name to publish to.
payload: Message payload.
sender: Optional sender identifier.
metadata: Optional message metadata.
Returns:
The published message.
"""
ch = await self.get_channel(channel)
message = Message(
channel=channel,
payload=payload,
sender=sender,
metadata=metadata or {},
)
await ch.publish(message)
# Also put in subscriber queues
async with self._channel_lock:
if channel in self._queues:
for queue in self._queues[channel].values():
try:
queue.put_nowait(message)
except asyncio.QueueFull:
# Remove oldest and add new
try:
queue.get_nowait()
queue.put_nowait(message)
except asyncio.QueueEmpty:
pass
return message
async def subscribe(
self,
channel: str,
subscriber_id: str,
callback: MessageCallback,
) -> Subscription:
"""
Subscribe to a channel with a callback.
Args:
channel: Channel name to subscribe to.
subscriber_id: Unique subscriber identifier.
callback: Async callback for received messages.
Returns:
Subscription object.
"""
ch = await self.get_channel(channel)
return await ch.subscribe(subscriber_id, callback)
async def subscribe_queue(
self,
channel: str,
subscriber_id: str,
max_size: int = 100,
) -> asyncio.Queue[Message]:
"""
Subscribe to a channel and receive messages via a queue.
This is an alternative to callback-based subscriptions.
Args:
channel: Channel name to subscribe to.
subscriber_id: Unique subscriber identifier.
max_size: Maximum queue size.
Returns:
Queue that will receive messages.
"""
async with self._channel_lock:
if subscriber_id not in self._queues[channel]:
self._queues[channel][subscriber_id] = asyncio.Queue(maxsize=max_size)
return self._queues[channel][subscriber_id]
async def unsubscribe(self, channel: str, subscriber_id: str) -> bool:
"""
Unsubscribe from a channel.
Args:
channel: Channel name.
subscriber_id: Subscriber to remove.
Returns:
True if subscriber was found and removed.
"""
async with self._channel_lock:
# Remove from callback subscriptions
if channel in self._channels:
await self._channels[channel].unsubscribe(subscriber_id)
# Remove from queue subscriptions
if channel in self._queues and subscriber_id in self._queues[channel]:
del self._queues[channel][subscriber_id]
return True
return False
async def set_state(self, key: str, value: Any) -> None:
"""
Set a shared state value.
Args:
key: State key.
value: Value to store.
"""
async with self._state_lock:
self._state[key] = value
async def get_state(self, key: str, default: Any = None) -> Any:
"""
Get a shared state value.
Args:
key: State key.
default: Default value if key not found.
Returns:
The stored value or default.
"""
async with self._state_lock:
return self._state.get(key, default)
async def delete_state(self, key: str) -> bool:
"""
Delete a shared state value.
Args:
key: State key to delete.
Returns:
True if key was found and deleted.
"""
async with self._state_lock:
if key in self._state:
del self._state[key]
return True
return False
async def update_state(self, key: str, updater: Callable[[Any], Any]) -> Any:
"""
Atomically update a state value.
Args:
key: State key.
updater: Function that takes current value and returns new value.
Returns:
The new value after update.
"""
async with self._state_lock:
current = self._state.get(key)
new_value = updater(current)
self._state[key] = new_value
return new_value
async def get_all_state(self) -> dict[str, Any]:
"""
Get all shared state values.
Returns:
Copy of the state dictionary.
"""
async with self._state_lock:
return dict(self._state)
async def clear_state(self) -> int:
"""
Clear all shared state.
Returns:
Number of keys cleared.
"""
async with self._state_lock:
count = len(self._state)
self._state.clear()
return count
async def list_channels(self) -> list[str]:
"""
List all active channels.
Returns:
List of channel names.
"""
async with self._channel_lock:
return list(self._channels.keys())
async def delete_channel(self, name: str) -> bool:
"""
Delete a channel and all its subscriptions.
Args:
name: Channel name to delete.
Returns:
True if channel was found and deleted.
"""
async with self._channel_lock:
if name in self._channels:
del self._channels[name]
if name in self._queues:
del self._queues[name]
return True
return False
async def clear(self) -> dict[str, int]:
"""
Clear all channels and state.
Returns:
Dictionary with counts of cleared items.
"""
async with self._channel_lock:
channel_count = len(self._channels)
self._channels.clear()
self._queues.clear()
async with self._state_lock:
state_count = len(self._state)
self._state.clear()
return {
"channels": channel_count,
"state_keys": state_count,
}
async def get_stats(self) -> dict[str, Any]:
"""
Get statistics about shared memory.
Returns:
Dictionary with memory statistics.
"""
async with self._channel_lock:
channel_stats = {}
for name, channel in self._channels.items():
channel_stats[name] = {
"subscribers": channel.subscriber_count,
"history_size": len(channel._history),
}
async with self._state_lock:
state_keys = list(self._state.keys())
return {
"channel_count": len(channel_stats),
"channels": channel_stats,
"state_key_count": len(state_keys),
"state_keys": state_keys,
"max_channel_history": self.max_channel_history,
}
|