Spaces:
Sleeping
Sleeping
File size: 17,389 Bytes
942216e |
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 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
"""
MCP Core - State Persistence Layer
This module provides functionality for persisting context state across sessions,
allowing for long-term storage and retrieval of context data.
"""
import json
import os
import sqlite3
from datetime import datetime
from typing import Dict, List, Optional, Any, Union
from abc import ABC, abstractmethod
from .context import Context
class PersistenceProvider(ABC):
"""
Abstract base class for persistence providers.
Persistence providers handle the storage and retrieval of context data,
allowing contexts to be persisted across sessions.
"""
@abstractmethod
async def save_context(self, context: Context) -> bool:
"""
Save a context to persistent storage.
Args:
context: The Context object to save.
Returns:
True if the save was successful, False otherwise.
"""
pass
@abstractmethod
async def load_context(self, context_id: str) -> Optional[Context]:
"""
Load a context from persistent storage.
Args:
context_id: The ID of the context to load.
Returns:
The loaded Context object if found, None otherwise.
"""
pass
@abstractmethod
async def delete_context(self, context_id: str) -> bool:
"""
Delete a context from persistent storage.
Args:
context_id: The ID of the context to delete.
Returns:
True if the deletion was successful, False otherwise.
"""
pass
@abstractmethod
async def list_contexts(self, filter_criteria: Optional[Dict[str, Any]] = None) -> List[str]:
"""
List context IDs in persistent storage, optionally filtered.
Args:
filter_criteria: Optional criteria to filter contexts by.
Returns:
List of context IDs matching the filter criteria.
"""
pass
class FileSystemProvider(PersistenceProvider):
"""
File system-based persistence provider.
This provider stores contexts as JSON files in the file system.
"""
def __init__(self, storage_dir: str = "./storage/contexts"):
"""
Initialize a new FileSystemProvider.
Args:
storage_dir: Directory to store context files in.
"""
self.storage_dir = storage_dir
os.makedirs(storage_dir, exist_ok=True)
def _get_context_path(self, context_id: str) -> str:
"""
Get the file path for a context.
Args:
context_id: The ID of the context.
Returns:
The file path for the context.
"""
return os.path.join(self.storage_dir, f"{context_id}.json")
async def save_context(self, context: Context) -> bool:
"""
Save a context to a JSON file.
Args:
context: The Context object to save.
Returns:
True if the save was successful, False otherwise.
"""
try:
with open(self._get_context_path(context.context_id), 'w') as f:
json.dump(context.to_dict(), f, indent=2)
return True
except Exception as e:
print(f"Error saving context: {e}")
return False
async def load_context(self, context_id: str) -> Optional[Context]:
"""
Load a context from a JSON file.
Args:
context_id: The ID of the context to load.
Returns:
The loaded Context object if found, None otherwise.
"""
try:
path = self._get_context_path(context_id)
if not os.path.exists(path):
return None
with open(path, 'r') as f:
data = json.load(f)
return Context.from_dict(data)
except Exception as e:
print(f"Error loading context: {e}")
return None
async def delete_context(self, context_id: str) -> bool:
"""
Delete a context JSON file.
Args:
context_id: The ID of the context to delete.
Returns:
True if the deletion was successful, False otherwise.
"""
try:
path = self._get_context_path(context_id)
if os.path.exists(path):
os.remove(path)
return True
return False
except Exception as e:
print(f"Error deleting context: {e}")
return False
async def list_contexts(self, filter_criteria: Optional[Dict[str, Any]] = None) -> List[str]:
"""
List context IDs in the storage directory, optionally filtered.
Args:
filter_criteria: Optional criteria to filter contexts by.
Returns:
List of context IDs matching the filter criteria.
"""
try:
context_ids = []
for filename in os.listdir(self.storage_dir):
if filename.endswith('.json'):
context_id = filename[:-5] # Remove .json extension
# Apply filtering if criteria provided
if filter_criteria:
try:
with open(self._get_context_path(context_id), 'r') as f:
data = json.load(f)
# Check if context matches all filter criteria
matches = True
for key, value in filter_criteria.items():
# Handle nested keys with dot notation
if '.' in key:
parts = key.split('.')
current = data
for part in parts:
if part not in current:
matches = False
break
current = current[part]
if matches and current != value:
matches = False
elif key not in data or data[key] != value:
matches = False
if matches:
context_ids.append(context_id)
except:
# Skip contexts that can't be loaded or don't match
continue
else:
context_ids.append(context_id)
return context_ids
except Exception as e:
print(f"Error listing contexts: {e}")
return []
class SQLiteProvider(PersistenceProvider):
"""
SQLite-based persistence provider.
This provider stores contexts in an SQLite database.
"""
def __init__(self, db_path: str = "./storage/contexts.db"):
"""
Initialize a new SQLiteProvider.
Args:
db_path: Path to the SQLite database file.
"""
os.makedirs(os.path.dirname(db_path), exist_ok=True)
self.db_path = db_path
self._init_db()
def _init_db(self):
"""Initialize the database schema if it doesn't exist."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Create contexts table
cursor.execute('''
CREATE TABLE IF NOT EXISTS contexts (
context_id TEXT PRIMARY KEY,
metadata TEXT,
state TEXT,
history TEXT,
created_at TEXT,
updated_at TEXT
)
''')
conn.commit()
conn.close()
async def save_context(self, context: Context) -> bool:
"""
Save a context to the SQLite database.
Args:
context: The Context object to save.
Returns:
True if the save was successful, False otherwise.
"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Convert complex objects to JSON strings
context_data = context.to_dict()
metadata = json.dumps(context_data.get("metadata", {}))
state = json.dumps(context_data.get("state", {}))
history = json.dumps(context_data.get("history", []))
# Check if context already exists
cursor.execute("SELECT 1 FROM contexts WHERE context_id = ?", (context.context_id,))
exists = cursor.fetchone() is not None
if exists:
# Update existing context
cursor.execute('''
UPDATE contexts
SET metadata = ?, state = ?, history = ?, updated_at = ?
WHERE context_id = ?
''', (metadata, state, history, context.updated_at.isoformat(), context.context_id))
else:
# Insert new context
cursor.execute('''
INSERT INTO contexts (context_id, metadata, state, history, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
''', (
context.context_id,
metadata,
state,
history,
context.created_at.isoformat(),
context.updated_at.isoformat()
))
conn.commit()
conn.close()
return True
except Exception as e:
print(f"Error saving context to SQLite: {e}")
return False
async def load_context(self, context_id: str) -> Optional[Context]:
"""
Load a context from the SQLite database.
Args:
context_id: The ID of the context to load.
Returns:
The loaded Context object if found, None otherwise.
"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT context_id, metadata, state, history, created_at, updated_at
FROM contexts
WHERE context_id = ?
''', (context_id,))
row = cursor.fetchone()
conn.close()
if not row:
return None
# Reconstruct context from database row
context_data = {
"context_id": row[0],
"metadata": json.loads(row[1]),
"state": json.loads(row[2]),
"history": json.loads(row[3]),
"created_at": row[4],
"updated_at": row[5]
}
return Context.from_dict(context_data)
except Exception as e:
print(f"Error loading context from SQLite: {e}")
return None
async def delete_context(self, context_id: str) -> bool:
"""
Delete a context from the SQLite database.
Args:
context_id: The ID of the context to delete.
Returns:
True if the deletion was successful, False otherwise.
"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("DELETE FROM contexts WHERE context_id = ?", (context_id,))
deleted = cursor.rowcount > 0
conn.commit()
conn.close()
return deleted
except Exception as e:
print(f"Error deleting context from SQLite: {e}")
return False
async def list_contexts(self, filter_criteria: Optional[Dict[str, Any]] = None) -> List[str]:
"""
List context IDs in the SQLite database, optionally filtered.
Args:
filter_criteria: Optional criteria to filter contexts by.
Returns:
List of context IDs matching the filter criteria.
"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
if not filter_criteria:
# Simple case: no filtering
cursor.execute("SELECT context_id FROM contexts")
context_ids = [row[0] for row in cursor.fetchall()]
else:
# Complex case: need to load and filter each context
cursor.execute("SELECT context_id, metadata, state FROM contexts")
rows = cursor.fetchall()
context_ids = []
for row in rows:
context_id = row[0]
metadata = json.loads(row[1])
state = json.loads(row[2])
# Check if context matches all filter criteria
matches = True
for key, value in filter_criteria.items():
if key.startswith("metadata."):
# Filter on metadata field
field = key[9:] # Remove "metadata." prefix
if field not in metadata or metadata[field] != value:
matches = False
break
elif key.startswith("state."):
# Filter on state field
field = key[6:] # Remove "state." prefix
if field not in state or state[field] != value:
matches = False
break
if matches:
context_ids.append(context_id)
conn.close()
return context_ids
except Exception as e:
print(f"Error listing contexts from SQLite: {e}")
return []
class PersistenceManager:
"""
Manages context persistence operations.
This class provides a unified interface for working with different
persistence providers.
"""
def __init__(self, provider: PersistenceProvider):
"""
Initialize a new PersistenceManager.
Args:
provider: The persistence provider to use.
"""
self.provider = provider
async def save_context(self, context: Context) -> bool:
"""
Save a context using the configured provider.
Args:
context: The Context object to save.
Returns:
True if the save was successful, False otherwise.
"""
return await self.provider.save_context(context)
async def load_context(self, context_id: str) -> Optional[Context]:
"""
Load a context using the configured provider.
Args:
context_id: The ID of the context to load.
Returns:
The loaded Context object if found, None otherwise.
"""
return await self.provider.load_context(context_id)
async def delete_context(self, context_id: str) -> bool:
"""
Delete a context using the configured provider.
Args:
context_id: The ID of the context to delete.
Returns:
True if the deletion was successful, False otherwise.
"""
return await self.provider.delete_context(context_id)
async def list_contexts(self, filter_criteria: Optional[Dict[str, Any]] = None) -> List[str]:
"""
List context IDs using the configured provider, optionally filtered.
Args:
filter_criteria: Optional criteria to filter contexts by.
Returns:
List of context IDs matching the filter criteria.
"""
return await self.provider.list_contexts(filter_criteria)
def change_provider(self, provider: PersistenceProvider) -> None:
"""
Change the persistence provider.
Args:
provider: The new persistence provider to use.
"""
self.provider = provider
|