agno 2.3.8__py3-none-any.whl → 2.3.9__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.
Files changed (62) hide show
  1. agno/agent/agent.py +134 -82
  2. agno/db/mysql/__init__.py +2 -1
  3. agno/db/mysql/async_mysql.py +2888 -0
  4. agno/db/mysql/mysql.py +17 -8
  5. agno/db/mysql/utils.py +139 -6
  6. agno/db/postgres/async_postgres.py +10 -5
  7. agno/db/postgres/postgres.py +7 -2
  8. agno/db/schemas/evals.py +1 -0
  9. agno/db/singlestore/singlestore.py +5 -1
  10. agno/db/sqlite/async_sqlite.py +2 -2
  11. agno/eval/__init__.py +10 -0
  12. agno/eval/agent_as_judge.py +860 -0
  13. agno/eval/base.py +29 -0
  14. agno/eval/utils.py +2 -1
  15. agno/exceptions.py +7 -0
  16. agno/knowledge/embedder/openai.py +8 -8
  17. agno/knowledge/knowledge.py +1142 -176
  18. agno/media.py +22 -6
  19. agno/models/aws/claude.py +8 -7
  20. agno/models/base.py +27 -1
  21. agno/models/deepseek/deepseek.py +67 -0
  22. agno/models/google/gemini.py +65 -11
  23. agno/models/google/utils.py +22 -0
  24. agno/models/message.py +2 -0
  25. agno/models/openai/chat.py +4 -0
  26. agno/os/app.py +64 -74
  27. agno/os/interfaces/a2a/router.py +3 -4
  28. agno/os/interfaces/agui/router.py +2 -0
  29. agno/os/router.py +3 -1607
  30. agno/os/routers/agents/__init__.py +3 -0
  31. agno/os/routers/agents/router.py +581 -0
  32. agno/os/routers/agents/schema.py +261 -0
  33. agno/os/routers/evals/evals.py +26 -6
  34. agno/os/routers/evals/schemas.py +34 -2
  35. agno/os/routers/evals/utils.py +101 -20
  36. agno/os/routers/knowledge/knowledge.py +1 -1
  37. agno/os/routers/teams/__init__.py +3 -0
  38. agno/os/routers/teams/router.py +496 -0
  39. agno/os/routers/teams/schema.py +257 -0
  40. agno/os/routers/workflows/__init__.py +3 -0
  41. agno/os/routers/workflows/router.py +545 -0
  42. agno/os/routers/workflows/schema.py +75 -0
  43. agno/os/schema.py +1 -559
  44. agno/os/utils.py +139 -2
  45. agno/team/team.py +73 -16
  46. agno/tools/file_generation.py +12 -6
  47. agno/tools/firecrawl.py +15 -7
  48. agno/utils/hooks.py +64 -5
  49. agno/utils/http.py +2 -2
  50. agno/utils/media.py +11 -1
  51. agno/utils/print_response/agent.py +8 -0
  52. agno/utils/print_response/team.py +8 -0
  53. agno/vectordb/pgvector/pgvector.py +88 -51
  54. agno/workflow/parallel.py +3 -3
  55. agno/workflow/step.py +14 -2
  56. agno/workflow/types.py +38 -2
  57. agno/workflow/workflow.py +12 -4
  58. {agno-2.3.8.dist-info → agno-2.3.9.dist-info}/METADATA +7 -2
  59. {agno-2.3.8.dist-info → agno-2.3.9.dist-info}/RECORD +62 -49
  60. {agno-2.3.8.dist-info → agno-2.3.9.dist-info}/WHEEL +0 -0
  61. {agno-2.3.8.dist-info → agno-2.3.9.dist-info}/licenses/LICENSE +0 -0
  62. {agno-2.3.8.dist-info → agno-2.3.9.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,2888 @@
1
+ import time
2
+ from datetime import date, datetime, timedelta, timezone
3
+ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
4
+ from uuid import uuid4
5
+
6
+ if TYPE_CHECKING:
7
+ from agno.tracing.schemas import Span, Trace
8
+
9
+ from agno.db.base import AsyncBaseDb, SessionType
10
+ from agno.db.migrations.manager import MigrationManager
11
+ from agno.db.mysql.schemas import get_table_schema_definition
12
+ from agno.db.mysql.utils import (
13
+ abulk_upsert_metrics,
14
+ acreate_schema,
15
+ ais_table_available,
16
+ ais_valid_table,
17
+ apply_sorting,
18
+ calculate_date_metrics,
19
+ deserialize_cultural_knowledge_from_db,
20
+ fetch_all_sessions_data,
21
+ get_dates_to_calculate_metrics_for,
22
+ serialize_cultural_knowledge_for_db,
23
+ )
24
+ from agno.db.schemas.culture import CulturalKnowledge
25
+ from agno.db.schemas.evals import EvalFilterType, EvalRunRecord, EvalType
26
+ from agno.db.schemas.knowledge import KnowledgeRow
27
+ from agno.db.schemas.memory import UserMemory
28
+ from agno.session import AgentSession, Session, TeamSession, WorkflowSession
29
+ from agno.utils.log import log_debug, log_error, log_info, log_warning
30
+ from agno.utils.string import generate_id
31
+
32
+ try:
33
+ from sqlalchemy import TEXT, ForeignKey, Index, UniqueConstraint, and_, cast, func, update
34
+ from sqlalchemy.dialects import mysql
35
+ from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine
36
+ from sqlalchemy.schema import Column, MetaData, Table
37
+ from sqlalchemy.sql.expression import select, text
38
+ except ImportError:
39
+ raise ImportError("`sqlalchemy` not installed. Please install it using `pip install sqlalchemy`")
40
+
41
+
42
+ class AsyncMySQLDb(AsyncBaseDb):
43
+ def __init__(
44
+ self,
45
+ id: Optional[str] = None,
46
+ db_url: Optional[str] = None,
47
+ db_engine: Optional[AsyncEngine] = None,
48
+ db_schema: Optional[str] = None,
49
+ session_table: Optional[str] = None,
50
+ memory_table: Optional[str] = None,
51
+ metrics_table: Optional[str] = None,
52
+ eval_table: Optional[str] = None,
53
+ knowledge_table: Optional[str] = None,
54
+ culture_table: Optional[str] = None,
55
+ traces_table: Optional[str] = None,
56
+ spans_table: Optional[str] = None,
57
+ versions_table: Optional[str] = None,
58
+ create_schema: bool = True,
59
+ ):
60
+ """
61
+ Async interface for interacting with a MySQL database.
62
+
63
+ The following order is used to determine the database connection:
64
+ 1. Use the db_engine if provided
65
+ 2. Use the db_url
66
+ 3. Raise an error if neither is provided
67
+
68
+ Args:
69
+ id (Optional[str]): The ID of the database.
70
+ db_url (Optional[str]): The database URL to connect to. Should use asyncmy driver (e.g. mysql+asyncmy://...)
71
+ db_engine (Optional[AsyncEngine]): The SQLAlchemy async database engine to use.
72
+ db_schema (Optional[str]): The database schema to use.
73
+ session_table (Optional[str]): Name of the table to store Agent, Team and Workflow sessions.
74
+ memory_table (Optional[str]): Name of the table to store memories.
75
+ metrics_table (Optional[str]): Name of the table to store metrics.
76
+ eval_table (Optional[str]): Name of the table to store evaluation runs data.
77
+ knowledge_table (Optional[str]): Name of the table to store knowledge content.
78
+ culture_table (Optional[str]): Name of the table to store cultural knowledge.
79
+ traces_table (Optional[str]): Name of the table to store run traces.
80
+ spans_table (Optional[str]): Name of the table to store span events.
81
+ versions_table (Optional[str]): Name of the table to store schema versions.
82
+ create_schema (bool): Whether to automatically create the database schema if it doesn't exist.
83
+ Set to False if schema is managed externally (e.g., via migrations). Defaults to True.
84
+
85
+ Raises:
86
+ ValueError: If neither db_url nor db_engine is provided.
87
+ ValueError: If none of the tables are provided.
88
+ """
89
+ if id is None:
90
+ base_seed = db_url or str(db_engine.url) if db_engine else "" # type: ignore
91
+ schema_suffix = db_schema if db_schema is not None else "ai"
92
+ seed = f"{base_seed}#{schema_suffix}"
93
+ id = generate_id(seed)
94
+
95
+ super().__init__(
96
+ id=id,
97
+ session_table=session_table,
98
+ memory_table=memory_table,
99
+ metrics_table=metrics_table,
100
+ eval_table=eval_table,
101
+ knowledge_table=knowledge_table,
102
+ culture_table=culture_table,
103
+ traces_table=traces_table,
104
+ spans_table=spans_table,
105
+ versions_table=versions_table,
106
+ )
107
+
108
+ _engine: Optional[AsyncEngine] = db_engine
109
+ if _engine is None and db_url is not None:
110
+ _engine = create_async_engine(db_url)
111
+ if _engine is None:
112
+ raise ValueError("One of db_url or db_engine must be provided")
113
+
114
+ self.db_url: Optional[str] = db_url
115
+ self.db_engine: AsyncEngine = _engine
116
+ self.db_schema: str = db_schema if db_schema is not None else "ai"
117
+ self.metadata: MetaData = MetaData(schema=self.db_schema)
118
+ self.create_schema: bool = create_schema
119
+
120
+ # Initialize database session factory
121
+ self.async_session_factory = async_sessionmaker(
122
+ bind=self.db_engine,
123
+ expire_on_commit=False,
124
+ )
125
+
126
+ # -- DB methods --
127
+ async def table_exists(self, table_name: str) -> bool:
128
+ """Check if a table with the given name exists in the MySQL database.
129
+
130
+ Args:
131
+ table_name: Name of the table to check
132
+
133
+ Returns:
134
+ bool: True if the table exists in the database, False otherwise
135
+ """
136
+ async with self.async_session_factory() as sess:
137
+ return await ais_table_available(session=sess, table_name=table_name, db_schema=self.db_schema)
138
+
139
+ async def _create_table(self, table_name: str, table_type: str) -> Table:
140
+ """
141
+ Create a table with the appropriate schema based on the table type.
142
+
143
+ Args:
144
+ table_name (str): Name of the table to create
145
+ table_type (str): Type of table (used to get schema definition)
146
+ db_schema (str): Database schema name
147
+
148
+ Returns:
149
+ Table: SQLAlchemy Table object
150
+ """
151
+ try:
152
+ table_schema = get_table_schema_definition(table_type).copy()
153
+
154
+ log_debug(f"Creating table {self.db_schema}.{table_name} with schema: {table_schema}")
155
+
156
+ columns: List[Column] = []
157
+ indexes: List[str] = []
158
+ unique_constraints: List[str] = []
159
+ schema_unique_constraints = table_schema.pop("_unique_constraints", [])
160
+
161
+ # Get the columns, indexes, and unique constraints from the table schema
162
+ for col_name, col_config in table_schema.items():
163
+ column_args = [col_name, col_config["type"]()]
164
+ column_kwargs = {}
165
+ if col_config.get("primary_key", False):
166
+ column_kwargs["primary_key"] = True
167
+ if "nullable" in col_config:
168
+ column_kwargs["nullable"] = col_config["nullable"]
169
+ if col_config.get("index", False):
170
+ indexes.append(col_name)
171
+ if col_config.get("unique", False):
172
+ column_kwargs["unique"] = True
173
+ unique_constraints.append(col_name)
174
+
175
+ # Handle foreign key constraint
176
+ if "foreign_key" in col_config:
177
+ fk_ref = col_config["foreign_key"]
178
+ # For spans table, dynamically replace the traces table reference
179
+ # with the actual trace table name configured for this db instance
180
+ if table_type == "spans" and "trace_id" in fk_ref:
181
+ fk_ref = f"{self.db_schema}.{self.trace_table_name}.trace_id"
182
+ column_args.append(ForeignKey(fk_ref))
183
+
184
+ columns.append(Column(*column_args, **column_kwargs)) # type: ignore
185
+
186
+ # Create the table object - use self.metadata to maintain FK references
187
+ table = Table(table_name, self.metadata, *columns, schema=self.db_schema)
188
+
189
+ # Add multi-column unique constraints with table-specific names
190
+ for constraint in schema_unique_constraints:
191
+ constraint_name = f"{table_name}_{constraint['name']}"
192
+ constraint_columns = constraint["columns"]
193
+ table.append_constraint(UniqueConstraint(*constraint_columns, name=constraint_name))
194
+
195
+ # Add indexes to the table definition
196
+ for idx_col in indexes:
197
+ idx_name = f"idx_{table_name}_{idx_col}"
198
+ table.append_constraint(Index(idx_name, idx_col))
199
+
200
+ # Create schema if not exists
201
+ if self.create_schema:
202
+ async with self.async_session_factory() as sess, sess.begin():
203
+ await acreate_schema(session=sess, db_schema=self.db_schema)
204
+
205
+ # Create table
206
+ table_created = False
207
+ if not await self.table_exists(table_name):
208
+ async with self.db_engine.begin() as conn:
209
+ await conn.run_sync(table.create, checkfirst=True)
210
+ log_debug(f"Successfully created table '{table_name}'")
211
+ table_created = True
212
+ else:
213
+ log_debug(f"Table {self.db_schema}.{table_name} already exists, skipping creation")
214
+
215
+ # Create indexes
216
+ for idx in table.indexes:
217
+ try:
218
+ # Check if index already exists
219
+ async with self.async_session_factory() as sess:
220
+ exists_query = text(
221
+ "SELECT 1 FROM information_schema.statistics WHERE table_schema = :schema "
222
+ "AND table_name = :table_name AND index_name = :index_name"
223
+ )
224
+ result = await sess.execute(
225
+ exists_query, {"schema": self.db_schema, "table_name": table_name, "index_name": idx.name}
226
+ )
227
+ exists = result.scalar() is not None
228
+ if exists:
229
+ log_debug(
230
+ f"Index {idx.name} already exists in {self.db_schema}.{table_name}, skipping creation"
231
+ )
232
+ continue
233
+
234
+ async with self.db_engine.begin() as conn:
235
+ await conn.run_sync(idx.create)
236
+ log_debug(f"Created index: {idx.name} for table {self.db_schema}.{table_name}")
237
+
238
+ except Exception as e:
239
+ log_error(f"Error creating index {idx.name}: {e}")
240
+
241
+ log_debug(f"Successfully created table {table_name} in schema {self.db_schema}")
242
+
243
+ # Store the schema version for the created table
244
+ if table_name != self.versions_table_name and table_created:
245
+ latest_schema_version = MigrationManager(self).latest_schema_version
246
+ await self.upsert_schema_version(table_name=table_name, version=latest_schema_version.public)
247
+ log_info(
248
+ f"Successfully stored version {latest_schema_version.public} in database for table {table_name}"
249
+ )
250
+
251
+ return table
252
+
253
+ except Exception as e:
254
+ log_error(f"Could not create table {self.db_schema}.{table_name}: {e}")
255
+ raise
256
+
257
+ async def _create_all_tables(self):
258
+ """Create all tables for the database."""
259
+ tables_to_create = [
260
+ (self.session_table_name, "sessions"),
261
+ (self.memory_table_name, "memories"),
262
+ (self.metrics_table_name, "metrics"),
263
+ (self.eval_table_name, "evals"),
264
+ (self.knowledge_table_name, "knowledge"),
265
+ (self.culture_table_name, "culture"),
266
+ (self.trace_table_name, "traces"),
267
+ (self.span_table_name, "spans"),
268
+ (self.versions_table_name, "versions"),
269
+ ]
270
+
271
+ for table_name, table_type in tables_to_create:
272
+ await self._get_or_create_table(
273
+ table_name=table_name, table_type=table_type, create_table_if_not_found=True
274
+ )
275
+
276
+ async def _get_table(self, table_type: str, create_table_if_not_found: Optional[bool] = False) -> Table:
277
+ if table_type == "sessions":
278
+ if not hasattr(self, "session_table"):
279
+ self.session_table = await self._get_or_create_table(
280
+ table_name=self.session_table_name,
281
+ table_type="sessions",
282
+ create_table_if_not_found=create_table_if_not_found,
283
+ )
284
+ return self.session_table
285
+
286
+ if table_type == "memories":
287
+ if not hasattr(self, "memory_table"):
288
+ self.memory_table = await self._get_or_create_table(
289
+ table_name=self.memory_table_name,
290
+ table_type="memories",
291
+ create_table_if_not_found=create_table_if_not_found,
292
+ )
293
+ return self.memory_table
294
+
295
+ if table_type == "metrics":
296
+ if not hasattr(self, "metrics_table"):
297
+ self.metrics_table = await self._get_or_create_table(
298
+ table_name=self.metrics_table_name,
299
+ table_type="metrics",
300
+ create_table_if_not_found=create_table_if_not_found,
301
+ )
302
+ return self.metrics_table
303
+
304
+ if table_type == "evals":
305
+ if not hasattr(self, "eval_table"):
306
+ self.eval_table = await self._get_or_create_table(
307
+ table_name=self.eval_table_name,
308
+ table_type="evals",
309
+ create_table_if_not_found=create_table_if_not_found,
310
+ )
311
+ return self.eval_table
312
+
313
+ if table_type == "knowledge":
314
+ if not hasattr(self, "knowledge_table"):
315
+ self.knowledge_table = await self._get_or_create_table(
316
+ table_name=self.knowledge_table_name,
317
+ table_type="knowledge",
318
+ create_table_if_not_found=create_table_if_not_found,
319
+ )
320
+ return self.knowledge_table
321
+
322
+ if table_type == "culture":
323
+ if not hasattr(self, "culture_table"):
324
+ self.culture_table = await self._get_or_create_table(
325
+ table_name=self.culture_table_name,
326
+ table_type="culture",
327
+ create_table_if_not_found=create_table_if_not_found,
328
+ )
329
+ return self.culture_table
330
+
331
+ if table_type == "versions":
332
+ if not hasattr(self, "versions_table"):
333
+ self.versions_table = await self._get_or_create_table(
334
+ table_name=self.versions_table_name,
335
+ table_type="versions",
336
+ create_table_if_not_found=create_table_if_not_found,
337
+ )
338
+ return self.versions_table
339
+
340
+ if table_type == "traces":
341
+ if not hasattr(self, "traces_table"):
342
+ self.traces_table = await self._get_or_create_table(
343
+ table_name=self.trace_table_name,
344
+ table_type="traces",
345
+ create_table_if_not_found=create_table_if_not_found,
346
+ )
347
+ return self.traces_table
348
+
349
+ if table_type == "spans":
350
+ if not hasattr(self, "spans_table"):
351
+ # Ensure traces table exists first (spans has FK to traces)
352
+ await self._get_table(table_type="traces", create_table_if_not_found=True)
353
+ self.spans_table = await self._get_or_create_table(
354
+ table_name=self.span_table_name,
355
+ table_type="spans",
356
+ create_table_if_not_found=create_table_if_not_found,
357
+ )
358
+ return self.spans_table
359
+
360
+ raise ValueError(f"Unknown table type: {table_type}")
361
+
362
+ async def _get_or_create_table(
363
+ self, table_name: str, table_type: str, create_table_if_not_found: Optional[bool] = False
364
+ ) -> Table:
365
+ """
366
+ Check if the table exists and is valid, else create it.
367
+
368
+ Args:
369
+ table_name (str): Name of the table to get or create
370
+ table_type (str): Type of table (used to get schema definition)
371
+
372
+ Returns:
373
+ Table: SQLAlchemy Table object representing the schema.
374
+ """
375
+
376
+ async with self.async_session_factory() as sess, sess.begin():
377
+ table_is_available = await ais_table_available(
378
+ session=sess, table_name=table_name, db_schema=self.db_schema
379
+ )
380
+
381
+ if (not table_is_available) and create_table_if_not_found:
382
+ return await self._create_table(table_name=table_name, table_type=table_type)
383
+
384
+ if not await ais_valid_table(
385
+ db_engine=self.db_engine,
386
+ table_name=table_name,
387
+ table_type=table_type,
388
+ db_schema=self.db_schema,
389
+ ):
390
+ raise ValueError(f"Table {self.db_schema}.{table_name} has an invalid schema")
391
+
392
+ try:
393
+ async with self.db_engine.connect() as conn:
394
+
395
+ def create_table(connection):
396
+ return Table(table_name, self.metadata, schema=self.db_schema, autoload_with=connection)
397
+
398
+ table = await conn.run_sync(create_table)
399
+ return table
400
+
401
+ except Exception as e:
402
+ log_error(f"Error loading existing table {self.db_schema}.{table_name}: {e}")
403
+ raise
404
+
405
+ async def get_latest_schema_version(self, table_name: str) -> str:
406
+ """Get the latest version of the database schema."""
407
+ table = await self._get_table(table_type="versions", create_table_if_not_found=True)
408
+ async with self.async_session_factory() as sess:
409
+ # Latest version for the given table
410
+ stmt = select(table).where(table.c.table_name == table_name).order_by(table.c.version.desc()).limit(1) # type: ignore
411
+ result = await sess.execute(stmt)
412
+ row = result.fetchone()
413
+ if row is None:
414
+ return "2.0.0"
415
+ version_dict = dict(row._mapping)
416
+ return version_dict.get("version") or "2.0.0"
417
+
418
+ async def upsert_schema_version(self, table_name: str, version: str) -> None:
419
+ """Upsert the schema version into the database."""
420
+ table = await self._get_table(table_type="versions", create_table_if_not_found=True)
421
+ current_datetime = datetime.now().isoformat()
422
+ async with self.async_session_factory() as sess, sess.begin():
423
+ stmt = mysql.insert(table).values( # type: ignore
424
+ table_name=table_name,
425
+ version=version,
426
+ created_at=current_datetime, # Store as ISO format string
427
+ updated_at=current_datetime,
428
+ )
429
+ # Update version if table_name already exists
430
+ stmt = stmt.on_duplicate_key_update(
431
+ version=version,
432
+ created_at=current_datetime,
433
+ updated_at=current_datetime,
434
+ )
435
+ await sess.execute(stmt)
436
+
437
+ # -- Session methods --
438
+ async def delete_session(self, session_id: str) -> bool:
439
+ """
440
+ Delete a session from the database.
441
+
442
+ Args:
443
+ session_id (str): ID of the session to delete
444
+
445
+ Returns:
446
+ bool: True if the session was deleted, False otherwise.
447
+
448
+ Raises:
449
+ Exception: If an error occurs during deletion.
450
+ """
451
+ try:
452
+ table = await self._get_table(table_type="sessions")
453
+
454
+ async with self.async_session_factory() as sess, sess.begin():
455
+ delete_stmt = table.delete().where(table.c.session_id == session_id)
456
+ result = await sess.execute(delete_stmt)
457
+
458
+ if result.rowcount == 0: # type: ignore
459
+ log_debug(f"No session found to delete with session_id: {session_id} in table {table.name}")
460
+ return False
461
+
462
+ else:
463
+ log_debug(f"Successfully deleted session with session_id: {session_id} in table {table.name}")
464
+ return True
465
+
466
+ except Exception as e:
467
+ log_error(f"Error deleting session: {e}")
468
+ return False
469
+
470
+ async def delete_sessions(self, session_ids: List[str]) -> None:
471
+ """Delete all given sessions from the database.
472
+ Can handle multiple session types in the same run.
473
+
474
+ Args:
475
+ session_ids (List[str]): The IDs of the sessions to delete.
476
+
477
+ Raises:
478
+ Exception: If an error occurs during deletion.
479
+ """
480
+ try:
481
+ table = await self._get_table(table_type="sessions")
482
+
483
+ async with self.async_session_factory() as sess, sess.begin():
484
+ delete_stmt = table.delete().where(table.c.session_id.in_(session_ids))
485
+ result = await sess.execute(delete_stmt)
486
+
487
+ log_debug(f"Successfully deleted {result.rowcount} sessions") # type: ignore
488
+
489
+ except Exception as e:
490
+ log_error(f"Error deleting sessions: {e}")
491
+
492
+ async def get_session(
493
+ self,
494
+ session_id: str,
495
+ session_type: SessionType,
496
+ user_id: Optional[str] = None,
497
+ deserialize: Optional[bool] = True,
498
+ ) -> Optional[Union[Session, Dict[str, Any]]]:
499
+ """
500
+ Read a session from the database.
501
+
502
+ Args:
503
+ session_id (str): ID of the session to read.
504
+ user_id (Optional[str]): User ID to filter by. Defaults to None.
505
+ session_type (Optional[SessionType]): Type of session to read. Defaults to None.
506
+ deserialize (Optional[bool]): Whether to serialize the session. Defaults to True.
507
+
508
+ Returns:
509
+ Union[Session, Dict[str, Any], None]:
510
+ - When deserialize=True: Session object
511
+ - When deserialize=False: Session dictionary
512
+
513
+ Raises:
514
+ Exception: If an error occurs during retrieval.
515
+ """
516
+ try:
517
+ table = await self._get_table(table_type="sessions")
518
+
519
+ async with self.async_session_factory() as sess:
520
+ stmt = select(table).where(table.c.session_id == session_id)
521
+
522
+ if user_id is not None:
523
+ stmt = stmt.where(table.c.user_id == user_id)
524
+ result = await sess.execute(stmt)
525
+ row = result.fetchone()
526
+ if row is None:
527
+ return None
528
+
529
+ session = dict(row._mapping)
530
+
531
+ if not deserialize:
532
+ return session
533
+
534
+ if session_type == SessionType.AGENT:
535
+ return AgentSession.from_dict(session)
536
+ elif session_type == SessionType.TEAM:
537
+ return TeamSession.from_dict(session)
538
+ elif session_type == SessionType.WORKFLOW:
539
+ return WorkflowSession.from_dict(session)
540
+ else:
541
+ raise ValueError(f"Invalid session type: {session_type}")
542
+
543
+ except Exception as e:
544
+ log_error(f"Exception reading from session table: {e}")
545
+ return None
546
+
547
+ async def get_sessions(
548
+ self,
549
+ session_type: Optional[SessionType] = None,
550
+ user_id: Optional[str] = None,
551
+ component_id: Optional[str] = None,
552
+ session_name: Optional[str] = None,
553
+ start_timestamp: Optional[int] = None,
554
+ end_timestamp: Optional[int] = None,
555
+ limit: Optional[int] = None,
556
+ page: Optional[int] = None,
557
+ sort_by: Optional[str] = None,
558
+ sort_order: Optional[str] = None,
559
+ deserialize: Optional[bool] = True,
560
+ ) -> Union[List[Session], Tuple[List[Dict[str, Any]], int]]:
561
+ """
562
+ Get all sessions in the given table. Can filter by user_id and entity_id.
563
+
564
+ Args:
565
+ user_id (Optional[str]): The ID of the user to filter by.
566
+ component_id (Optional[str]): The ID of the agent / workflow to filter by.
567
+ start_timestamp (Optional[int]): The start timestamp to filter by.
568
+ end_timestamp (Optional[int]): The end timestamp to filter by.
569
+ session_name (Optional[str]): The name of the session to filter by.
570
+ limit (Optional[int]): The maximum number of sessions to return. Defaults to None.
571
+ page (Optional[int]): The page number to return. Defaults to None.
572
+ sort_by (Optional[str]): The field to sort by. Defaults to None.
573
+ sort_order (Optional[str]): The sort order. Defaults to None.
574
+ deserialize (Optional[bool]): Whether to serialize the sessions. Defaults to True.
575
+
576
+ Returns:
577
+ Union[List[Session], Tuple[List[Dict], int]]:
578
+ - When deserialize=True: List of Session objects
579
+ - When deserialize=False: Tuple of (session dictionaries, total count)
580
+
581
+ Raises:
582
+ Exception: If an error occurs during retrieval.
583
+ """
584
+ try:
585
+ table = await self._get_table(table_type="sessions")
586
+
587
+ async with self.async_session_factory() as sess, sess.begin():
588
+ stmt = select(table)
589
+
590
+ # Filtering
591
+ if user_id is not None:
592
+ stmt = stmt.where(table.c.user_id == user_id)
593
+ if component_id is not None:
594
+ if session_type == SessionType.AGENT:
595
+ stmt = stmt.where(table.c.agent_id == component_id)
596
+ elif session_type == SessionType.TEAM:
597
+ stmt = stmt.where(table.c.team_id == component_id)
598
+ elif session_type == SessionType.WORKFLOW:
599
+ stmt = stmt.where(table.c.workflow_id == component_id)
600
+ if start_timestamp is not None:
601
+ stmt = stmt.where(table.c.created_at >= start_timestamp)
602
+ if end_timestamp is not None:
603
+ stmt = stmt.where(table.c.created_at <= end_timestamp)
604
+ if session_name is not None:
605
+ # MySQL JSON extraction syntax
606
+ stmt = stmt.where(
607
+ func.coalesce(
608
+ func.json_unquote(func.json_extract(table.c.session_data, "$.session_name")), ""
609
+ ).ilike(f"%{session_name}%")
610
+ )
611
+ if session_type is not None:
612
+ session_type_value = session_type.value if isinstance(session_type, SessionType) else session_type
613
+ stmt = stmt.where(table.c.session_type == session_type_value)
614
+
615
+ count_stmt = select(func.count()).select_from(stmt.alias())
616
+ total_count = await sess.scalar(count_stmt) or 0
617
+
618
+ # Sorting
619
+ stmt = apply_sorting(stmt, table, sort_by, sort_order)
620
+
621
+ # Paginating
622
+ if limit is not None:
623
+ stmt = stmt.limit(limit)
624
+ if page is not None:
625
+ stmt = stmt.offset((page - 1) * limit)
626
+
627
+ result = await sess.execute(stmt)
628
+ records = result.fetchall()
629
+ if records is None:
630
+ return [], 0
631
+
632
+ session = [dict(record._mapping) for record in records]
633
+ if not deserialize:
634
+ return session, total_count
635
+
636
+ if session_type == SessionType.AGENT:
637
+ return [AgentSession.from_dict(record) for record in session] # type: ignore
638
+ elif session_type == SessionType.TEAM:
639
+ return [TeamSession.from_dict(record) for record in session] # type: ignore
640
+ elif session_type == SessionType.WORKFLOW:
641
+ return [WorkflowSession.from_dict(record) for record in session] # type: ignore
642
+ else:
643
+ raise ValueError(f"Invalid session type: {session_type}")
644
+
645
+ except Exception as e:
646
+ log_error(f"Exception reading from session table: {e}")
647
+ return [] if deserialize else ([], 0)
648
+
649
+ async def rename_session(
650
+ self, session_id: str, session_type: SessionType, session_name: str, deserialize: Optional[bool] = True
651
+ ) -> Optional[Union[Session, Dict[str, Any]]]:
652
+ """
653
+ Rename a session in the database.
654
+
655
+ Args:
656
+ session_id (str): The ID of the session to rename.
657
+ session_type (SessionType): The type of session to rename.
658
+ session_name (str): The new name for the session.
659
+ deserialize (Optional[bool]): Whether to serialize the session. Defaults to True.
660
+
661
+ Returns:
662
+ Optional[Union[Session, Dict[str, Any]]]:
663
+ - When deserialize=True: Session object
664
+ - When deserialize=False: Session dictionary
665
+
666
+ Raises:
667
+ Exception: If an error occurs during renaming.
668
+ """
669
+ try:
670
+ table = await self._get_table(table_type="sessions")
671
+
672
+ async with self.async_session_factory() as sess, sess.begin():
673
+ # MySQL JSON_SET syntax
674
+ stmt = (
675
+ update(table)
676
+ .where(table.c.session_id == session_id)
677
+ .where(table.c.session_type == session_type.value)
678
+ .values(session_data=func.json_set(table.c.session_data, "$.session_name", session_name))
679
+ )
680
+ await sess.execute(stmt)
681
+
682
+ # Fetch the updated row
683
+ select_stmt = select(table).where(table.c.session_id == session_id)
684
+ result = await sess.execute(select_stmt)
685
+ row = result.fetchone()
686
+ if not row:
687
+ return None
688
+
689
+ log_debug(f"Renamed session with id '{session_id}' to '{session_name}'")
690
+
691
+ session = dict(row._mapping)
692
+ if not deserialize:
693
+ return session
694
+
695
+ # Return the appropriate session type
696
+ if session_type == SessionType.AGENT:
697
+ return AgentSession.from_dict(session)
698
+ elif session_type == SessionType.TEAM:
699
+ return TeamSession.from_dict(session)
700
+ elif session_type == SessionType.WORKFLOW:
701
+ return WorkflowSession.from_dict(session)
702
+ else:
703
+ raise ValueError(f"Invalid session type: {session_type}")
704
+
705
+ except Exception as e:
706
+ log_error(f"Exception renaming session: {e}")
707
+ return None
708
+
709
+ async def upsert_session(
710
+ self, session: Session, deserialize: Optional[bool] = True
711
+ ) -> Optional[Union[Session, Dict[str, Any]]]:
712
+ """
713
+ Insert or update a session in the database.
714
+
715
+ Args:
716
+ session (Session): The session data to upsert.
717
+ deserialize (Optional[bool]): Whether to deserialize the session. Defaults to True.
718
+
719
+ Returns:
720
+ Optional[Union[Session, Dict[str, Any]]]:
721
+ - When deserialize=True: Session object
722
+ - When deserialize=False: Session dictionary
723
+
724
+ Raises:
725
+ Exception: If an error occurs during upsert.
726
+ """
727
+ try:
728
+ table = await self._get_table(table_type="sessions", create_table_if_not_found=True)
729
+ session_dict = session.to_dict()
730
+
731
+ if isinstance(session, AgentSession):
732
+ async with self.async_session_factory() as sess, sess.begin():
733
+ current_time = int(time.time())
734
+ stmt = mysql.insert(table).values(
735
+ session_id=session_dict.get("session_id"),
736
+ session_type=SessionType.AGENT.value,
737
+ agent_id=session_dict.get("agent_id"),
738
+ user_id=session_dict.get("user_id"),
739
+ runs=session_dict.get("runs"),
740
+ agent_data=session_dict.get("agent_data"),
741
+ session_data=session_dict.get("session_data"),
742
+ summary=session_dict.get("summary"),
743
+ metadata=session_dict.get("metadata"),
744
+ created_at=session_dict.get("created_at") or current_time,
745
+ updated_at=session_dict.get("updated_at") or current_time,
746
+ )
747
+ stmt = stmt.on_duplicate_key_update(
748
+ agent_id=session_dict.get("agent_id"),
749
+ user_id=session_dict.get("user_id"),
750
+ agent_data=session_dict.get("agent_data"),
751
+ session_data=session_dict.get("session_data"),
752
+ summary=session_dict.get("summary"),
753
+ metadata=session_dict.get("metadata"),
754
+ runs=session_dict.get("runs"),
755
+ updated_at=int(time.time()),
756
+ )
757
+ await sess.execute(stmt)
758
+
759
+ # Fetch the row
760
+ select_stmt = select(table).where(table.c.session_id == session_dict.get("session_id"))
761
+ result = await sess.execute(select_stmt)
762
+ row = result.fetchone()
763
+ if row is None:
764
+ return None
765
+ session_dict = dict(row._mapping)
766
+
767
+ log_debug(f"Upserted agent session with id '{session_dict.get('session_id')}'")
768
+
769
+ if not deserialize:
770
+ return session_dict
771
+ return AgentSession.from_dict(session_dict)
772
+
773
+ elif isinstance(session, TeamSession):
774
+ async with self.async_session_factory() as sess, sess.begin():
775
+ current_time = int(time.time())
776
+ stmt = mysql.insert(table).values(
777
+ session_id=session_dict.get("session_id"),
778
+ session_type=SessionType.TEAM.value,
779
+ team_id=session_dict.get("team_id"),
780
+ user_id=session_dict.get("user_id"),
781
+ runs=session_dict.get("runs"),
782
+ team_data=session_dict.get("team_data"),
783
+ session_data=session_dict.get("session_data"),
784
+ summary=session_dict.get("summary"),
785
+ metadata=session_dict.get("metadata"),
786
+ created_at=session_dict.get("created_at") or current_time,
787
+ updated_at=session_dict.get("updated_at") or current_time,
788
+ )
789
+ stmt = stmt.on_duplicate_key_update(
790
+ team_id=session_dict.get("team_id"),
791
+ user_id=session_dict.get("user_id"),
792
+ team_data=session_dict.get("team_data"),
793
+ session_data=session_dict.get("session_data"),
794
+ summary=session_dict.get("summary"),
795
+ metadata=session_dict.get("metadata"),
796
+ runs=session_dict.get("runs"),
797
+ updated_at=int(time.time()),
798
+ )
799
+ await sess.execute(stmt)
800
+
801
+ # Fetch the row
802
+ select_stmt = select(table).where(table.c.session_id == session_dict.get("session_id"))
803
+ result = await sess.execute(select_stmt)
804
+ row = result.fetchone()
805
+ if row is None:
806
+ return None
807
+ session_dict = dict(row._mapping)
808
+
809
+ log_debug(f"Upserted team session with id '{session_dict.get('session_id')}'")
810
+
811
+ if not deserialize:
812
+ return session_dict
813
+ return TeamSession.from_dict(session_dict)
814
+
815
+ elif isinstance(session, WorkflowSession):
816
+ async with self.async_session_factory() as sess, sess.begin():
817
+ current_time = int(time.time())
818
+ stmt = mysql.insert(table).values(
819
+ session_id=session_dict.get("session_id"),
820
+ session_type=SessionType.WORKFLOW.value,
821
+ workflow_id=session_dict.get("workflow_id"),
822
+ user_id=session_dict.get("user_id"),
823
+ runs=session_dict.get("runs"),
824
+ workflow_data=session_dict.get("workflow_data"),
825
+ session_data=session_dict.get("session_data"),
826
+ summary=session_dict.get("summary"),
827
+ metadata=session_dict.get("metadata"),
828
+ created_at=session_dict.get("created_at") or current_time,
829
+ updated_at=session_dict.get("updated_at") or current_time,
830
+ )
831
+ stmt = stmt.on_duplicate_key_update(
832
+ workflow_id=session_dict.get("workflow_id"),
833
+ user_id=session_dict.get("user_id"),
834
+ workflow_data=session_dict.get("workflow_data"),
835
+ session_data=session_dict.get("session_data"),
836
+ summary=session_dict.get("summary"),
837
+ metadata=session_dict.get("metadata"),
838
+ runs=session_dict.get("runs"),
839
+ updated_at=int(time.time()),
840
+ )
841
+ await sess.execute(stmt)
842
+
843
+ # Fetch the row
844
+ select_stmt = select(table).where(table.c.session_id == session_dict.get("session_id"))
845
+ result = await sess.execute(select_stmt)
846
+ row = result.fetchone()
847
+ if row is None:
848
+ return None
849
+ session_dict = dict(row._mapping)
850
+
851
+ log_debug(f"Upserted workflow session with id '{session_dict.get('session_id')}'")
852
+
853
+ if not deserialize:
854
+ return session_dict
855
+ return WorkflowSession.from_dict(session_dict)
856
+
857
+ else:
858
+ raise ValueError(f"Invalid session type: {session.session_type}")
859
+
860
+ except Exception as e:
861
+ log_error(f"Exception upserting into sessions table: {e}")
862
+ return None
863
+
864
+ async def upsert_sessions(
865
+ self, sessions: List[Session], deserialize: Optional[bool] = True, preserve_updated_at: bool = False
866
+ ) -> List[Union[Session, Dict[str, Any]]]:
867
+ """
868
+ Bulk upsert multiple sessions for improved performance on large datasets.
869
+
870
+ Args:
871
+ sessions (List[Session]): List of sessions to upsert.
872
+ deserialize (Optional[bool]): Whether to deserialize the sessions. Defaults to True.
873
+ preserve_updated_at (bool): If True, preserve the updated_at from the session object.
874
+
875
+ Returns:
876
+ List[Union[Session, Dict[str, Any]]]: List of upserted sessions.
877
+
878
+ Raises:
879
+ Exception: If an error occurs during bulk upsert.
880
+ """
881
+ if not sessions:
882
+ return []
883
+
884
+ try:
885
+ table = await self._get_table(table_type="sessions")
886
+
887
+ # Group sessions by type for batch processing
888
+ agent_sessions = []
889
+ team_sessions = []
890
+ workflow_sessions = []
891
+
892
+ for session in sessions:
893
+ if isinstance(session, AgentSession):
894
+ agent_sessions.append(session)
895
+ elif isinstance(session, TeamSession):
896
+ team_sessions.append(session)
897
+ elif isinstance(session, WorkflowSession):
898
+ workflow_sessions.append(session)
899
+
900
+ results: List[Union[Session, Dict[str, Any]]] = []
901
+
902
+ # Process each session type in bulk
903
+ async with self.async_session_factory() as sess, sess.begin():
904
+ # Bulk upsert agent sessions
905
+ if agent_sessions:
906
+ agent_data = []
907
+ for session in agent_sessions:
908
+ session_dict = session.to_dict()
909
+ # Use preserved updated_at if flag is set and value exists, otherwise use current time
910
+ updated_at = session_dict.get("updated_at") if preserve_updated_at else int(time.time())
911
+ agent_data.append(
912
+ {
913
+ "session_id": session_dict.get("session_id"),
914
+ "session_type": SessionType.AGENT.value,
915
+ "agent_id": session_dict.get("agent_id"),
916
+ "user_id": session_dict.get("user_id"),
917
+ "runs": session_dict.get("runs"),
918
+ "agent_data": session_dict.get("agent_data"),
919
+ "session_data": session_dict.get("session_data"),
920
+ "summary": session_dict.get("summary"),
921
+ "metadata": session_dict.get("metadata"),
922
+ "created_at": session_dict.get("created_at"),
923
+ "updated_at": updated_at,
924
+ }
925
+ )
926
+
927
+ if agent_data:
928
+ stmt = mysql.insert(table)
929
+ stmt = stmt.on_duplicate_key_update(
930
+ agent_id=stmt.inserted.agent_id,
931
+ user_id=stmt.inserted.user_id,
932
+ agent_data=stmt.inserted.agent_data,
933
+ session_data=stmt.inserted.session_data,
934
+ summary=stmt.inserted.summary,
935
+ metadata=stmt.inserted.metadata,
936
+ runs=stmt.inserted.runs,
937
+ updated_at=stmt.inserted.updated_at,
938
+ )
939
+ await sess.execute(stmt, agent_data)
940
+
941
+ # Fetch the results for agent sessions
942
+ agent_ids = [session.session_id for session in agent_sessions]
943
+ select_stmt = select(table).where(table.c.session_id.in_(agent_ids))
944
+ result = await sess.execute(select_stmt)
945
+ fetched_rows = result.fetchall()
946
+
947
+ for row in fetched_rows:
948
+ session_dict = dict(row._mapping)
949
+ if deserialize:
950
+ deserialized_agent_session = AgentSession.from_dict(session_dict)
951
+ if deserialized_agent_session is None:
952
+ continue
953
+ results.append(deserialized_agent_session)
954
+ else:
955
+ results.append(session_dict)
956
+
957
+ # Bulk upsert team sessions
958
+ if team_sessions:
959
+ team_data = []
960
+ for session in team_sessions:
961
+ session_dict = session.to_dict()
962
+ # Use preserved updated_at if flag is set and value exists, otherwise use current time
963
+ updated_at = session_dict.get("updated_at") if preserve_updated_at else int(time.time())
964
+ team_data.append(
965
+ {
966
+ "session_id": session_dict.get("session_id"),
967
+ "session_type": SessionType.TEAM.value,
968
+ "team_id": session_dict.get("team_id"),
969
+ "user_id": session_dict.get("user_id"),
970
+ "runs": session_dict.get("runs"),
971
+ "team_data": session_dict.get("team_data"),
972
+ "session_data": session_dict.get("session_data"),
973
+ "summary": session_dict.get("summary"),
974
+ "metadata": session_dict.get("metadata"),
975
+ "created_at": session_dict.get("created_at"),
976
+ "updated_at": updated_at,
977
+ }
978
+ )
979
+
980
+ if team_data:
981
+ stmt = mysql.insert(table)
982
+ stmt = stmt.on_duplicate_key_update(
983
+ team_id=stmt.inserted.team_id,
984
+ user_id=stmt.inserted.user_id,
985
+ team_data=stmt.inserted.team_data,
986
+ session_data=stmt.inserted.session_data,
987
+ summary=stmt.inserted.summary,
988
+ metadata=stmt.inserted.metadata,
989
+ runs=stmt.inserted.runs,
990
+ updated_at=stmt.inserted.updated_at,
991
+ )
992
+ await sess.execute(stmt, team_data)
993
+
994
+ # Fetch the results for team sessions
995
+ team_ids = [session.session_id for session in team_sessions]
996
+ select_stmt = select(table).where(table.c.session_id.in_(team_ids))
997
+ result = await sess.execute(select_stmt)
998
+ fetched_rows = result.fetchall()
999
+
1000
+ for row in fetched_rows:
1001
+ session_dict = dict(row._mapping)
1002
+ if deserialize:
1003
+ deserialized_team_session = TeamSession.from_dict(session_dict)
1004
+ if deserialized_team_session is None:
1005
+ continue
1006
+ results.append(deserialized_team_session)
1007
+ else:
1008
+ results.append(session_dict)
1009
+
1010
+ # Bulk upsert workflow sessions
1011
+ if workflow_sessions:
1012
+ workflow_data = []
1013
+ for session in workflow_sessions:
1014
+ session_dict = session.to_dict()
1015
+ # Use preserved updated_at if flag is set and value exists, otherwise use current time
1016
+ updated_at = session_dict.get("updated_at") if preserve_updated_at else int(time.time())
1017
+ workflow_data.append(
1018
+ {
1019
+ "session_id": session_dict.get("session_id"),
1020
+ "session_type": SessionType.WORKFLOW.value,
1021
+ "workflow_id": session_dict.get("workflow_id"),
1022
+ "user_id": session_dict.get("user_id"),
1023
+ "runs": session_dict.get("runs"),
1024
+ "workflow_data": session_dict.get("workflow_data"),
1025
+ "session_data": session_dict.get("session_data"),
1026
+ "summary": session_dict.get("summary"),
1027
+ "metadata": session_dict.get("metadata"),
1028
+ "created_at": session_dict.get("created_at"),
1029
+ "updated_at": updated_at,
1030
+ }
1031
+ )
1032
+
1033
+ if workflow_data:
1034
+ stmt = mysql.insert(table)
1035
+ stmt = stmt.on_duplicate_key_update(
1036
+ workflow_id=stmt.inserted.workflow_id,
1037
+ user_id=stmt.inserted.user_id,
1038
+ workflow_data=stmt.inserted.workflow_data,
1039
+ session_data=stmt.inserted.session_data,
1040
+ summary=stmt.inserted.summary,
1041
+ metadata=stmt.inserted.metadata,
1042
+ runs=stmt.inserted.runs,
1043
+ updated_at=stmt.inserted.updated_at,
1044
+ )
1045
+ await sess.execute(stmt, workflow_data)
1046
+
1047
+ # Fetch the results for workflow sessions
1048
+ workflow_ids = [session.session_id for session in workflow_sessions]
1049
+ select_stmt = select(table).where(table.c.session_id.in_(workflow_ids))
1050
+ result = await sess.execute(select_stmt)
1051
+ fetched_rows = result.fetchall()
1052
+
1053
+ for row in fetched_rows:
1054
+ session_dict = dict(row._mapping)
1055
+ if deserialize:
1056
+ deserialized_workflow_session = WorkflowSession.from_dict(session_dict)
1057
+ if deserialized_workflow_session is None:
1058
+ continue
1059
+ results.append(deserialized_workflow_session)
1060
+ else:
1061
+ results.append(session_dict)
1062
+
1063
+ return results
1064
+
1065
+ except Exception as e:
1066
+ log_error(f"Exception during bulk session upsert, falling back to individual upserts: {e}")
1067
+ # Fallback to individual upserts
1068
+ return [
1069
+ result
1070
+ for session in sessions
1071
+ if session is not None
1072
+ for result in [await self.upsert_session(session, deserialize=deserialize)]
1073
+ if result is not None
1074
+ ]
1075
+
1076
+ # -- Memory methods --
1077
+ async def delete_user_memory(self, memory_id: str, user_id: Optional[str] = None) -> None:
1078
+ """Delete a user memory from the database.
1079
+
1080
+ Returns:
1081
+ bool: True if deletion was successful, False otherwise.
1082
+
1083
+ Raises:
1084
+ Exception: If an error occurs during deletion.
1085
+ """
1086
+ try:
1087
+ table = await self._get_table(table_type="memories")
1088
+
1089
+ async with self.async_session_factory() as sess, sess.begin():
1090
+ delete_stmt = table.delete().where(table.c.memory_id == memory_id)
1091
+ if user_id is not None:
1092
+ delete_stmt = delete_stmt.where(table.c.user_id == user_id)
1093
+ result = await sess.execute(delete_stmt)
1094
+
1095
+ success = result.rowcount > 0 # type: ignore
1096
+ if success:
1097
+ log_debug(f"Successfully deleted user memory id: {memory_id}")
1098
+ else:
1099
+ log_debug(f"No user memory found with id: {memory_id}")
1100
+
1101
+ except Exception as e:
1102
+ log_error(f"Error deleting user memory: {e}")
1103
+
1104
+ async def delete_user_memories(self, memory_ids: List[str], user_id: Optional[str] = None) -> None:
1105
+ """Delete user memories from the database.
1106
+
1107
+ Args:
1108
+ memory_ids (List[str]): The IDs of the memories to delete.
1109
+ user_id (Optional[str]): Optional user ID to filter deletions.
1110
+
1111
+ Raises:
1112
+ Exception: If an error occurs during deletion.
1113
+ """
1114
+ try:
1115
+ table = await self._get_table(table_type="memories")
1116
+
1117
+ async with self.async_session_factory() as sess, sess.begin():
1118
+ delete_stmt = table.delete().where(table.c.memory_id.in_(memory_ids))
1119
+ if user_id is not None:
1120
+ delete_stmt = delete_stmt.where(table.c.user_id == user_id)
1121
+ result = await sess.execute(delete_stmt)
1122
+
1123
+ if result.rowcount == 0: # type: ignore
1124
+ log_debug(f"No user memories found with ids: {memory_ids}")
1125
+ else:
1126
+ log_debug(f"Successfully deleted {result.rowcount} user memories") # type: ignore
1127
+
1128
+ except Exception as e:
1129
+ log_error(f"Error deleting user memories: {e}")
1130
+
1131
+ async def get_all_memory_topics(self, user_id: Optional[str] = None) -> List[str]:
1132
+ """Get all memory topics from the database.
1133
+
1134
+ Args:
1135
+ user_id (Optional[str]): Optional user ID to filter topics.
1136
+
1137
+ Returns:
1138
+ List[str]: List of memory topics.
1139
+ """
1140
+ try:
1141
+ table = await self._get_table(table_type="memories")
1142
+
1143
+ async with self.async_session_factory() as sess, sess.begin():
1144
+ # MySQL approach: extract JSON array elements differently
1145
+ stmt = select(table.c.topics)
1146
+ result = await sess.execute(stmt)
1147
+ records = result.fetchall()
1148
+
1149
+ topics_set = set()
1150
+ for row in records:
1151
+ if row[0]:
1152
+ # Parse JSON array and add topics to set
1153
+ import json
1154
+
1155
+ try:
1156
+ topics = json.loads(row[0]) if isinstance(row[0], str) else row[0]
1157
+ if isinstance(topics, list):
1158
+ topics_set.update(topics)
1159
+ except Exception:
1160
+ pass
1161
+
1162
+ return list(topics_set)
1163
+
1164
+ except Exception as e:
1165
+ log_error(f"Exception reading from memory table: {e}")
1166
+ return []
1167
+
1168
+ async def get_user_memory(
1169
+ self, memory_id: str, deserialize: Optional[bool] = True, user_id: Optional[str] = None
1170
+ ) -> Optional[Union[UserMemory, Dict[str, Any]]]:
1171
+ """Get a memory from the database.
1172
+
1173
+ Args:
1174
+ memory_id (str): The ID of the memory to get.
1175
+ deserialize (Optional[bool]): Whether to serialize the memory. Defaults to True.
1176
+
1177
+ Returns:
1178
+ Union[UserMemory, Dict[str, Any], None]:
1179
+ - When deserialize=True: UserMemory object
1180
+ - When deserialize=False: UserMemory dictionary
1181
+
1182
+ Raises:
1183
+ Exception: If an error occurs during retrieval.
1184
+ """
1185
+ try:
1186
+ table = await self._get_table(table_type="memories")
1187
+
1188
+ async with self.async_session_factory() as sess, sess.begin():
1189
+ stmt = select(table).where(table.c.memory_id == memory_id)
1190
+ if user_id is not None:
1191
+ stmt = stmt.where(table.c.user_id == user_id)
1192
+
1193
+ result = await sess.execute(stmt)
1194
+ row = result.fetchone()
1195
+ if not row:
1196
+ return None
1197
+
1198
+ memory_raw = dict(row._mapping)
1199
+ if not deserialize:
1200
+ return memory_raw
1201
+
1202
+ return UserMemory.from_dict(memory_raw)
1203
+
1204
+ except Exception as e:
1205
+ log_error(f"Exception reading from memory table: {e}")
1206
+ return None
1207
+
1208
+ async def get_user_memories(
1209
+ self,
1210
+ user_id: Optional[str] = None,
1211
+ agent_id: Optional[str] = None,
1212
+ team_id: Optional[str] = None,
1213
+ topics: Optional[List[str]] = None,
1214
+ search_content: Optional[str] = None,
1215
+ limit: Optional[int] = None,
1216
+ page: Optional[int] = None,
1217
+ sort_by: Optional[str] = None,
1218
+ sort_order: Optional[str] = None,
1219
+ deserialize: Optional[bool] = True,
1220
+ ) -> Union[List[UserMemory], Tuple[List[Dict[str, Any]], int]]:
1221
+ """Get all memories from the database as UserMemory objects.
1222
+
1223
+ Args:
1224
+ user_id (Optional[str]): The ID of the user to filter by.
1225
+ agent_id (Optional[str]): The ID of the agent to filter by.
1226
+ team_id (Optional[str]): The ID of the team to filter by.
1227
+ topics (Optional[List[str]]): The topics to filter by.
1228
+ search_content (Optional[str]): The content to search for.
1229
+ limit (Optional[int]): The maximum number of memories to return.
1230
+ page (Optional[int]): The page number.
1231
+ sort_by (Optional[str]): The column to sort by.
1232
+ sort_order (Optional[str]): The order to sort by.
1233
+ deserialize (Optional[bool]): Whether to serialize the memories. Defaults to True.
1234
+
1235
+ Returns:
1236
+ Union[List[UserMemory], Tuple[List[Dict[str, Any]], int]]:
1237
+ - When deserialize=True: List of UserMemory objects
1238
+ - When deserialize=False: Tuple of (memory dictionaries, total count)
1239
+
1240
+ Raises:
1241
+ Exception: If an error occurs during retrieval.
1242
+ """
1243
+ try:
1244
+ table = await self._get_table(table_type="memories")
1245
+
1246
+ async with self.async_session_factory() as sess, sess.begin():
1247
+ stmt = select(table)
1248
+ # Filtering
1249
+ if user_id is not None:
1250
+ stmt = stmt.where(table.c.user_id == user_id)
1251
+ if agent_id is not None:
1252
+ stmt = stmt.where(table.c.agent_id == agent_id)
1253
+ if team_id is not None:
1254
+ stmt = stmt.where(table.c.team_id == team_id)
1255
+ if topics is not None:
1256
+ # MySQL JSON contains syntax
1257
+ topic_conditions = []
1258
+ for topic in topics:
1259
+ topic_conditions.append(func.json_contains(table.c.topics, f'"{topic}"'))
1260
+ stmt = stmt.where(and_(*topic_conditions))
1261
+ if search_content is not None:
1262
+ stmt = stmt.where(cast(table.c.memory, TEXT).ilike(f"%{search_content}%"))
1263
+
1264
+ # Get total count after applying filtering
1265
+ count_stmt = select(func.count()).select_from(stmt.alias())
1266
+ total_count = await sess.scalar(count_stmt) or 0
1267
+
1268
+ # Sorting
1269
+ stmt = apply_sorting(stmt, table, sort_by, sort_order)
1270
+
1271
+ # Paginating
1272
+ if limit is not None:
1273
+ stmt = stmt.limit(limit)
1274
+ if page is not None:
1275
+ stmt = stmt.offset((page - 1) * limit)
1276
+
1277
+ result = await sess.execute(stmt)
1278
+ records = result.fetchall()
1279
+ if not records:
1280
+ return [] if deserialize else ([], 0)
1281
+
1282
+ memories_raw = [dict(record._mapping) for record in records]
1283
+ if not deserialize:
1284
+ return memories_raw, total_count
1285
+
1286
+ return [UserMemory.from_dict(record) for record in memories_raw]
1287
+
1288
+ except Exception as e:
1289
+ log_error(f"Exception reading from memory table: {e}")
1290
+ return [] if deserialize else ([], 0)
1291
+
1292
+ async def clear_memories(self) -> None:
1293
+ """Delete all memories from the database.
1294
+
1295
+ Raises:
1296
+ Exception: If an error occurs during deletion.
1297
+ """
1298
+ try:
1299
+ table = await self._get_table(table_type="memories")
1300
+
1301
+ async with self.async_session_factory() as sess, sess.begin():
1302
+ await sess.execute(table.delete())
1303
+
1304
+ except Exception as e:
1305
+ log_warning(f"Exception deleting all memories: {e}")
1306
+
1307
+ # -- Cultural Knowledge methods --
1308
+ async def clear_cultural_knowledge(self) -> None:
1309
+ """Delete all cultural knowledge from the database.
1310
+
1311
+ Raises:
1312
+ Exception: If an error occurs during deletion.
1313
+ """
1314
+ try:
1315
+ table = await self._get_table(table_type="culture")
1316
+
1317
+ async with self.async_session_factory() as sess, sess.begin():
1318
+ await sess.execute(table.delete())
1319
+
1320
+ except Exception as e:
1321
+ log_warning(f"Exception deleting all cultural knowledge: {e}")
1322
+
1323
+ async def delete_cultural_knowledge(self, id: str) -> None:
1324
+ """Delete cultural knowledge by ID.
1325
+
1326
+ Args:
1327
+ id (str): The ID of the cultural knowledge to delete.
1328
+
1329
+ Raises:
1330
+ Exception: If an error occurs during deletion.
1331
+ """
1332
+ try:
1333
+ table = await self._get_table(table_type="culture")
1334
+
1335
+ async with self.async_session_factory() as sess, sess.begin():
1336
+ stmt = table.delete().where(table.c.id == id)
1337
+ await sess.execute(stmt)
1338
+
1339
+ except Exception as e:
1340
+ log_warning(f"Exception deleting cultural knowledge: {e}")
1341
+ raise e
1342
+
1343
+ async def get_cultural_knowledge(
1344
+ self, id: str, deserialize: Optional[bool] = True
1345
+ ) -> Optional[Union[CulturalKnowledge, Dict[str, Any]]]:
1346
+ """Get cultural knowledge by ID.
1347
+
1348
+ Args:
1349
+ id (str): The ID of the cultural knowledge to retrieve.
1350
+ deserialize (Optional[bool]): Whether to deserialize to CulturalKnowledge object. Defaults to True.
1351
+
1352
+ Returns:
1353
+ Optional[Union[CulturalKnowledge, Dict[str, Any]]]: The cultural knowledge if found, None otherwise.
1354
+
1355
+ Raises:
1356
+ Exception: If an error occurs during retrieval.
1357
+ """
1358
+ try:
1359
+ table = await self._get_table(table_type="culture")
1360
+
1361
+ async with self.async_session_factory() as sess:
1362
+ stmt = select(table).where(table.c.id == id)
1363
+ result = await sess.execute(stmt)
1364
+ row = result.fetchone()
1365
+
1366
+ if row is None:
1367
+ return None
1368
+
1369
+ db_row = dict(row._mapping)
1370
+
1371
+ if not deserialize:
1372
+ return db_row
1373
+
1374
+ return deserialize_cultural_knowledge_from_db(db_row)
1375
+
1376
+ except Exception as e:
1377
+ log_warning(f"Exception reading cultural knowledge: {e}")
1378
+ raise e
1379
+
1380
+ async def get_all_cultural_knowledge(
1381
+ self,
1382
+ agent_id: Optional[str] = None,
1383
+ team_id: Optional[str] = None,
1384
+ name: Optional[str] = None,
1385
+ limit: Optional[int] = None,
1386
+ page: Optional[int] = None,
1387
+ sort_by: Optional[str] = None,
1388
+ sort_order: Optional[str] = None,
1389
+ deserialize: Optional[bool] = True,
1390
+ ) -> Union[List[CulturalKnowledge], Tuple[List[Dict[str, Any]], int]]:
1391
+ """Get all cultural knowledge with filtering and pagination.
1392
+
1393
+ Args:
1394
+ agent_id (Optional[str]): Filter by agent ID.
1395
+ team_id (Optional[str]): Filter by team ID.
1396
+ name (Optional[str]): Filter by name (case-insensitive partial match).
1397
+ limit (Optional[int]): Maximum number of results to return.
1398
+ page (Optional[int]): Page number for pagination.
1399
+ sort_by (Optional[str]): Field to sort by.
1400
+ sort_order (Optional[str]): Sort order ('asc' or 'desc').
1401
+ deserialize (Optional[bool]): Whether to deserialize to CulturalKnowledge objects. Defaults to True.
1402
+
1403
+ Returns:
1404
+ Union[List[CulturalKnowledge], Tuple[List[Dict[str, Any]], int]]:
1405
+ - When deserialize=True: List of CulturalKnowledge objects
1406
+ - When deserialize=False: Tuple with list of dictionaries and total count
1407
+
1408
+ Raises:
1409
+ Exception: If an error occurs during retrieval.
1410
+ """
1411
+ try:
1412
+ table = await self._get_table(table_type="culture")
1413
+
1414
+ async with self.async_session_factory() as sess:
1415
+ # Build query with filters
1416
+ stmt = select(table)
1417
+ if agent_id is not None:
1418
+ stmt = stmt.where(table.c.agent_id == agent_id)
1419
+ if team_id is not None:
1420
+ stmt = stmt.where(table.c.team_id == team_id)
1421
+ if name is not None:
1422
+ stmt = stmt.where(table.c.name.ilike(f"%{name}%"))
1423
+
1424
+ # Get total count
1425
+ count_stmt = select(func.count()).select_from(stmt.alias())
1426
+ total_count_result = await sess.execute(count_stmt)
1427
+ total_count = total_count_result.scalar() or 0
1428
+
1429
+ # Apply sorting
1430
+ stmt = apply_sorting(stmt, table, sort_by, sort_order)
1431
+
1432
+ # Apply pagination
1433
+ if limit is not None:
1434
+ stmt = stmt.limit(limit)
1435
+ if page is not None:
1436
+ stmt = stmt.offset((page - 1) * limit)
1437
+
1438
+ # Execute query
1439
+ result = await sess.execute(stmt)
1440
+ rows = result.fetchall()
1441
+
1442
+ db_rows = [dict(row._mapping) for row in rows]
1443
+
1444
+ if not deserialize:
1445
+ return db_rows, total_count
1446
+
1447
+ return [deserialize_cultural_knowledge_from_db(row) for row in db_rows]
1448
+
1449
+ except Exception as e:
1450
+ log_warning(f"Exception reading all cultural knowledge: {e}")
1451
+ raise e
1452
+
1453
+ async def upsert_cultural_knowledge(
1454
+ self, cultural_knowledge: CulturalKnowledge, deserialize: Optional[bool] = True
1455
+ ) -> Optional[Union[CulturalKnowledge, Dict[str, Any]]]:
1456
+ """Upsert cultural knowledge in the database.
1457
+
1458
+ Args:
1459
+ cultural_knowledge (CulturalKnowledge): The cultural knowledge to upsert.
1460
+ deserialize (Optional[bool]): Whether to deserialize the result. Defaults to True.
1461
+
1462
+ Returns:
1463
+ Optional[Union[CulturalKnowledge, Dict[str, Any]]]: The upserted cultural knowledge.
1464
+
1465
+ Raises:
1466
+ Exception: If an error occurs during upsert.
1467
+ """
1468
+ try:
1469
+ table = await self._get_table(table_type="culture")
1470
+
1471
+ # Generate ID if not present
1472
+ if cultural_knowledge.id is None:
1473
+ cultural_knowledge.id = str(uuid4())
1474
+
1475
+ # Serialize content, categories, and notes into a JSON dict for DB storage
1476
+ content_dict = serialize_cultural_knowledge_for_db(cultural_knowledge)
1477
+
1478
+ async with self.async_session_factory() as sess, sess.begin():
1479
+ # Use MySQL-specific insert with on_duplicate_key_update
1480
+ insert_stmt = mysql.insert(table).values(
1481
+ id=cultural_knowledge.id,
1482
+ name=cultural_knowledge.name,
1483
+ summary=cultural_knowledge.summary,
1484
+ content=content_dict if content_dict else None,
1485
+ metadata=cultural_knowledge.metadata,
1486
+ input=cultural_knowledge.input,
1487
+ created_at=cultural_knowledge.created_at,
1488
+ updated_at=int(time.time()),
1489
+ agent_id=cultural_knowledge.agent_id,
1490
+ team_id=cultural_knowledge.team_id,
1491
+ )
1492
+
1493
+ # Update all fields except id on conflict
1494
+ upsert_stmt = insert_stmt.on_duplicate_key_update(
1495
+ name=cultural_knowledge.name,
1496
+ summary=cultural_knowledge.summary,
1497
+ content=content_dict if content_dict else None,
1498
+ metadata=cultural_knowledge.metadata,
1499
+ input=cultural_knowledge.input,
1500
+ updated_at=int(time.time()),
1501
+ agent_id=cultural_knowledge.agent_id,
1502
+ team_id=cultural_knowledge.team_id,
1503
+ )
1504
+
1505
+ await sess.execute(upsert_stmt)
1506
+
1507
+ # Fetch the inserted/updated row
1508
+ select_stmt = select(table).where(table.c.id == cultural_knowledge.id)
1509
+ result = await sess.execute(select_stmt)
1510
+ row = result.fetchone()
1511
+
1512
+ if row is None:
1513
+ return None
1514
+
1515
+ db_row = dict(row._mapping)
1516
+
1517
+ if not deserialize:
1518
+ return db_row
1519
+
1520
+ # Deserialize from DB format to model format
1521
+ return deserialize_cultural_knowledge_from_db(db_row)
1522
+
1523
+ except Exception as e:
1524
+ log_warning(f"Exception upserting cultural knowledge: {e}")
1525
+ raise e
1526
+
1527
+ async def get_user_memory_stats(
1528
+ self, limit: Optional[int] = None, page: Optional[int] = None, user_id: Optional[str] = None
1529
+ ) -> Tuple[List[Dict[str, Any]], int]:
1530
+ """Get user memories stats.
1531
+
1532
+ Args:
1533
+ limit (Optional[int]): The maximum number of user stats to return.
1534
+ page (Optional[int]): The page number.
1535
+
1536
+ Returns:
1537
+ Tuple[List[Dict[str, Any]], int]: A list of dictionaries containing user stats and total count.
1538
+
1539
+ Example:
1540
+ (
1541
+ [
1542
+ {
1543
+ "user_id": "123",
1544
+ "total_memories": 10,
1545
+ "last_memory_updated_at": 1714560000,
1546
+ },
1547
+ ],
1548
+ total_count: 1,
1549
+ )
1550
+ """
1551
+ try:
1552
+ table = await self._get_table(table_type="memories")
1553
+
1554
+ async with self.async_session_factory() as sess, sess.begin():
1555
+ stmt = select(
1556
+ table.c.user_id,
1557
+ func.count(table.c.memory_id).label("total_memories"),
1558
+ func.max(table.c.updated_at).label("last_memory_updated_at"),
1559
+ )
1560
+
1561
+ if user_id is not None:
1562
+ stmt = stmt.where(table.c.user_id == user_id)
1563
+ else:
1564
+ stmt = stmt.where(table.c.user_id.is_not(None))
1565
+
1566
+ stmt = stmt.group_by(table.c.user_id)
1567
+ stmt = stmt.order_by(func.max(table.c.updated_at).desc())
1568
+
1569
+ count_stmt = select(func.count()).select_from(stmt.alias())
1570
+ total_count = await sess.scalar(count_stmt) or 0
1571
+
1572
+ # Pagination
1573
+ if limit is not None:
1574
+ stmt = stmt.limit(limit)
1575
+ if page is not None:
1576
+ stmt = stmt.offset((page - 1) * limit)
1577
+
1578
+ result = await sess.execute(stmt)
1579
+ records = result.fetchall()
1580
+ if not records:
1581
+ return [], 0
1582
+
1583
+ return [
1584
+ {
1585
+ "user_id": record.user_id, # type: ignore
1586
+ "total_memories": record.total_memories,
1587
+ "last_memory_updated_at": record.last_memory_updated_at,
1588
+ }
1589
+ for record in records
1590
+ ], total_count
1591
+
1592
+ except Exception as e:
1593
+ log_error(f"Exception getting user memory stats: {e}")
1594
+ return [], 0
1595
+
1596
+ async def upsert_user_memory(
1597
+ self, memory: UserMemory, deserialize: Optional[bool] = True
1598
+ ) -> Optional[Union[UserMemory, Dict[str, Any]]]:
1599
+ """Upsert a user memory in the database.
1600
+
1601
+ Args:
1602
+ memory (UserMemory): The user memory to upsert.
1603
+ deserialize (Optional[bool]): Whether to serialize the memory. Defaults to True.
1604
+
1605
+ Returns:
1606
+ Optional[Union[UserMemory, Dict[str, Any]]]:
1607
+ - When deserialize=True: UserMemory object
1608
+ - When deserialize=False: UserMemory dictionary
1609
+
1610
+ Raises:
1611
+ Exception: If an error occurs during upsert.
1612
+ """
1613
+ try:
1614
+ table = await self._get_table(table_type="memories")
1615
+
1616
+ async with self.async_session_factory() as sess, sess.begin():
1617
+ if memory.memory_id is None:
1618
+ memory.memory_id = str(uuid4())
1619
+
1620
+ current_time = int(time.time())
1621
+
1622
+ stmt = mysql.insert(table).values(
1623
+ memory_id=memory.memory_id,
1624
+ memory=memory.memory,
1625
+ input=memory.input,
1626
+ user_id=memory.user_id,
1627
+ agent_id=memory.agent_id,
1628
+ team_id=memory.team_id,
1629
+ topics=memory.topics,
1630
+ feedback=memory.feedback,
1631
+ created_at=memory.created_at,
1632
+ updated_at=memory.created_at,
1633
+ )
1634
+ stmt = stmt.on_duplicate_key_update(
1635
+ memory=memory.memory,
1636
+ topics=memory.topics,
1637
+ input=memory.input,
1638
+ agent_id=memory.agent_id,
1639
+ team_id=memory.team_id,
1640
+ feedback=memory.feedback,
1641
+ updated_at=current_time,
1642
+ # Preserve created_at on update - don't overwrite existing value
1643
+ created_at=table.c.created_at,
1644
+ )
1645
+ await sess.execute(stmt)
1646
+
1647
+ # Fetch the row
1648
+ select_stmt = select(table).where(table.c.memory_id == memory.memory_id)
1649
+ result = await sess.execute(select_stmt)
1650
+ row = result.fetchone()
1651
+ if row is None:
1652
+ return None
1653
+
1654
+ memory_raw = dict(row._mapping)
1655
+
1656
+ log_debug(f"Upserted user memory with id '{memory.memory_id}'")
1657
+
1658
+ if not memory_raw or not deserialize:
1659
+ return memory_raw
1660
+
1661
+ return UserMemory.from_dict(memory_raw)
1662
+
1663
+ except Exception as e:
1664
+ log_error(f"Exception upserting user memory: {e}")
1665
+ return None
1666
+
1667
+ async def upsert_memories(
1668
+ self, memories: List[UserMemory], deserialize: Optional[bool] = True, preserve_updated_at: bool = False
1669
+ ) -> List[Union[UserMemory, Dict[str, Any]]]:
1670
+ """
1671
+ Bulk upsert multiple user memories for improved performance on large datasets.
1672
+
1673
+ Args:
1674
+ memories (List[UserMemory]): List of memories to upsert.
1675
+ deserialize (Optional[bool]): Whether to deserialize the memories. Defaults to True.
1676
+ preserve_updated_at (bool): If True, preserve the updated_at from the memory object.
1677
+
1678
+ Returns:
1679
+ List[Union[UserMemory, Dict[str, Any]]]: List of upserted memories.
1680
+
1681
+ Raises:
1682
+ Exception: If an error occurs during bulk upsert.
1683
+ """
1684
+ if not memories:
1685
+ return []
1686
+
1687
+ try:
1688
+ table = await self._get_table(table_type="memories")
1689
+
1690
+ # Prepare bulk data
1691
+ bulk_data = []
1692
+ current_time = int(time.time())
1693
+ for memory in memories:
1694
+ if memory.memory_id is None:
1695
+ memory.memory_id = str(uuid4())
1696
+
1697
+ # Use preserved updated_at if flag is set and value exists, otherwise use current time
1698
+ updated_at = memory.updated_at if preserve_updated_at else current_time
1699
+ bulk_data.append(
1700
+ {
1701
+ "memory_id": memory.memory_id,
1702
+ "memory": memory.memory,
1703
+ "input": memory.input,
1704
+ "user_id": memory.user_id,
1705
+ "agent_id": memory.agent_id,
1706
+ "team_id": memory.team_id,
1707
+ "topics": memory.topics,
1708
+ "feedback": memory.feedback,
1709
+ "created_at": memory.created_at,
1710
+ "updated_at": updated_at,
1711
+ }
1712
+ )
1713
+
1714
+ results: List[Union[UserMemory, Dict[str, Any]]] = []
1715
+
1716
+ async with self.async_session_factory() as sess, sess.begin():
1717
+ # Bulk upsert memories using MySQL ON DUPLICATE KEY UPDATE
1718
+ stmt = mysql.insert(table)
1719
+ stmt = stmt.on_duplicate_key_update(
1720
+ memory=stmt.inserted.memory,
1721
+ topics=stmt.inserted.topics,
1722
+ input=stmt.inserted.input,
1723
+ agent_id=stmt.inserted.agent_id,
1724
+ team_id=stmt.inserted.team_id,
1725
+ feedback=stmt.inserted.feedback,
1726
+ updated_at=stmt.inserted.updated_at,
1727
+ # Preserve created_at on update
1728
+ created_at=table.c.created_at,
1729
+ )
1730
+ await sess.execute(stmt, bulk_data)
1731
+
1732
+ # Fetch results
1733
+ memory_ids = [memory.memory_id for memory in memories if memory.memory_id]
1734
+ select_stmt = select(table).where(table.c.memory_id.in_(memory_ids))
1735
+ result = await sess.execute(select_stmt)
1736
+ fetched_rows = result.fetchall()
1737
+
1738
+ for row in fetched_rows:
1739
+ memory_dict = dict(row._mapping)
1740
+ if deserialize:
1741
+ results.append(UserMemory.from_dict(memory_dict))
1742
+ else:
1743
+ results.append(memory_dict)
1744
+
1745
+ return results
1746
+
1747
+ except Exception as e:
1748
+ log_error(f"Exception during bulk memory upsert, falling back to individual upserts: {e}")
1749
+ # Fallback to individual upserts
1750
+ return [
1751
+ result
1752
+ for memory in memories
1753
+ if memory is not None
1754
+ for result in [await self.upsert_user_memory(memory, deserialize=deserialize)]
1755
+ if result is not None
1756
+ ]
1757
+
1758
+ # -- Metrics methods --
1759
+ async def _get_all_sessions_for_metrics_calculation(
1760
+ self, start_timestamp: Optional[int] = None, end_timestamp: Optional[int] = None
1761
+ ) -> List[Dict[str, Any]]:
1762
+ """
1763
+ Get all sessions of all types (agent, team, workflow) as raw dictionaries.
1764
+
1765
+ Args:
1766
+ start_timestamp (Optional[int]): The start timestamp to filter by. Defaults to None.
1767
+ end_timestamp (Optional[int]): The end timestamp to filter by. Defaults to None.
1768
+
1769
+ Returns:
1770
+ List[Dict[str, Any]]: List of session dictionaries with session_type field.
1771
+
1772
+ Raises:
1773
+ Exception: If an error occurs during retrieval.
1774
+ """
1775
+ try:
1776
+ table = await self._get_table(table_type="sessions")
1777
+
1778
+ stmt = select(
1779
+ table.c.user_id,
1780
+ table.c.session_data,
1781
+ table.c.runs,
1782
+ table.c.created_at,
1783
+ table.c.session_type,
1784
+ )
1785
+
1786
+ if start_timestamp is not None:
1787
+ stmt = stmt.where(table.c.created_at >= start_timestamp)
1788
+ if end_timestamp is not None:
1789
+ stmt = stmt.where(table.c.created_at <= end_timestamp)
1790
+
1791
+ async with self.async_session_factory() as sess:
1792
+ result = await sess.execute(stmt)
1793
+ records = result.fetchall()
1794
+
1795
+ return [dict(record._mapping) for record in records]
1796
+
1797
+ except Exception as e:
1798
+ log_error(f"Exception reading from sessions table: {e}")
1799
+ return []
1800
+
1801
+ async def _get_metrics_calculation_starting_date(self, table: Table) -> Optional[date]:
1802
+ """Get the first date for which metrics calculation is needed:
1803
+
1804
+ 1. If there are metrics records, return the date of the first day without a complete metrics record.
1805
+ 2. If there are no metrics records, return the date of the first recorded session.
1806
+ 3. If there are no metrics records and no sessions records, return None.
1807
+
1808
+ Args:
1809
+ table (Table): The table to get the starting date for.
1810
+
1811
+ Returns:
1812
+ Optional[date]: The starting date for which metrics calculation is needed.
1813
+ """
1814
+ async with self.async_session_factory() as sess:
1815
+ stmt = select(table).order_by(table.c.date.desc()).limit(1)
1816
+ result = await sess.execute(stmt)
1817
+ row = result.fetchone()
1818
+
1819
+ # 1. Return the date of the first day without a complete metrics record.
1820
+ if row is not None:
1821
+ if row.completed:
1822
+ return row._mapping["date"] + timedelta(days=1)
1823
+ else:
1824
+ return row._mapping["date"]
1825
+
1826
+ # 2. No metrics records. Return the date of the first recorded session.
1827
+ first_session, _ = await self.get_sessions(sort_by="created_at", sort_order="asc", limit=1, deserialize=False)
1828
+
1829
+ first_session_date = first_session[0]["created_at"] if first_session else None # type: ignore[index]
1830
+
1831
+ # 3. No metrics records and no sessions records. Return None.
1832
+ if first_session_date is None:
1833
+ return None
1834
+
1835
+ return datetime.fromtimestamp(first_session_date, tz=timezone.utc).date()
1836
+
1837
+ async def calculate_metrics(self) -> Optional[list[dict]]:
1838
+ """Calculate metrics for all dates without complete metrics.
1839
+
1840
+ Returns:
1841
+ Optional[list[dict]]: The calculated metrics.
1842
+
1843
+ Raises:
1844
+ Exception: If an error occurs during metrics calculation.
1845
+ """
1846
+ try:
1847
+ table = await self._get_table(table_type="metrics")
1848
+
1849
+ starting_date = await self._get_metrics_calculation_starting_date(table)
1850
+
1851
+ if starting_date is None:
1852
+ log_info("No session data found. Won't calculate metrics.")
1853
+ return None
1854
+
1855
+ dates_to_process = get_dates_to_calculate_metrics_for(starting_date)
1856
+ if not dates_to_process:
1857
+ log_info("Metrics already calculated for all relevant dates.")
1858
+ return None
1859
+
1860
+ start_timestamp = int(
1861
+ datetime.combine(dates_to_process[0], datetime.min.time()).replace(tzinfo=timezone.utc).timestamp()
1862
+ )
1863
+ end_timestamp = int(
1864
+ datetime.combine(dates_to_process[-1] + timedelta(days=1), datetime.min.time())
1865
+ .replace(tzinfo=timezone.utc)
1866
+ .timestamp()
1867
+ )
1868
+
1869
+ sessions = await self._get_all_sessions_for_metrics_calculation(
1870
+ start_timestamp=start_timestamp, end_timestamp=end_timestamp
1871
+ )
1872
+
1873
+ all_sessions_data = fetch_all_sessions_data(
1874
+ sessions=sessions, dates_to_process=dates_to_process, start_timestamp=start_timestamp
1875
+ )
1876
+ if not all_sessions_data:
1877
+ log_info("No new session data found. Won't calculate metrics.")
1878
+ return None
1879
+
1880
+ results = []
1881
+ metrics_records = []
1882
+
1883
+ for date_to_process in dates_to_process:
1884
+ date_key = date_to_process.isoformat()
1885
+ sessions_for_date = all_sessions_data.get(date_key, {})
1886
+
1887
+ # Skip dates with no sessions
1888
+ if not any(len(sessions) > 0 for sessions in sessions_for_date.values()):
1889
+ continue
1890
+
1891
+ metrics_record = calculate_date_metrics(date_to_process, sessions_for_date)
1892
+
1893
+ metrics_records.append(metrics_record)
1894
+
1895
+ if metrics_records:
1896
+ async with self.async_session_factory() as sess, sess.begin():
1897
+ results = await abulk_upsert_metrics(session=sess, table=table, metrics_records=metrics_records)
1898
+
1899
+ log_debug("Updated metrics calculations")
1900
+
1901
+ return results
1902
+
1903
+ except Exception as e:
1904
+ log_error(f"Exception refreshing metrics: {e}")
1905
+ return None
1906
+
1907
+ async def get_metrics(
1908
+ self, starting_date: Optional[date] = None, ending_date: Optional[date] = None
1909
+ ) -> Tuple[List[dict], Optional[int]]:
1910
+ """Get all metrics matching the given date range.
1911
+
1912
+ Args:
1913
+ starting_date (Optional[date]): The starting date to filter metrics by.
1914
+ ending_date (Optional[date]): The ending date to filter metrics by.
1915
+
1916
+ Returns:
1917
+ Tuple[List[dict], Optional[int]]: A tuple containing the metrics and the timestamp of the latest update.
1918
+
1919
+ Raises:
1920
+ Exception: If an error occurs during retrieval.
1921
+ """
1922
+ try:
1923
+ table = await self._get_table(table_type="metrics")
1924
+
1925
+ async with self.async_session_factory() as sess, sess.begin():
1926
+ stmt = select(table)
1927
+ if starting_date:
1928
+ stmt = stmt.where(table.c.date >= starting_date)
1929
+ if ending_date:
1930
+ stmt = stmt.where(table.c.date <= ending_date)
1931
+ result = await sess.execute(stmt)
1932
+ records = result.fetchall()
1933
+ if not records:
1934
+ return [], None
1935
+
1936
+ # Get the latest updated_at
1937
+ latest_stmt = select(func.max(table.c.updated_at))
1938
+ latest_result = await sess.execute(latest_stmt)
1939
+ latest_updated_at = latest_result.scalar()
1940
+
1941
+ return [dict(row._mapping) for row in records], latest_updated_at
1942
+
1943
+ except Exception as e:
1944
+ log_warning(f"Exception getting metrics: {e}")
1945
+ return [], None
1946
+
1947
+ # -- Knowledge methods --
1948
+ async def delete_knowledge_content(self, id: str):
1949
+ """Delete a knowledge row from the database.
1950
+
1951
+ Args:
1952
+ id (str): The ID of the knowledge row to delete.
1953
+ """
1954
+ table = await self._get_table(table_type="knowledge")
1955
+
1956
+ try:
1957
+ async with self.async_session_factory() as sess, sess.begin():
1958
+ stmt = table.delete().where(table.c.id == id)
1959
+ await sess.execute(stmt)
1960
+
1961
+ except Exception as e:
1962
+ log_error(f"Exception deleting knowledge content: {e}")
1963
+
1964
+ async def get_knowledge_content(self, id: str) -> Optional[KnowledgeRow]:
1965
+ """Get a knowledge row from the database.
1966
+
1967
+ Args:
1968
+ id (str): The ID of the knowledge row to get.
1969
+
1970
+ Returns:
1971
+ Optional[KnowledgeRow]: The knowledge row, or None if it doesn't exist.
1972
+ """
1973
+ table = await self._get_table(table_type="knowledge")
1974
+
1975
+ try:
1976
+ async with self.async_session_factory() as sess, sess.begin():
1977
+ stmt = select(table).where(table.c.id == id)
1978
+ result = await sess.execute(stmt)
1979
+ row = result.fetchone()
1980
+ if row is None:
1981
+ return None
1982
+
1983
+ return KnowledgeRow.model_validate(row._mapping)
1984
+
1985
+ except Exception as e:
1986
+ log_error(f"Exception getting knowledge content: {e}")
1987
+ return None
1988
+
1989
+ async def get_knowledge_contents(
1990
+ self,
1991
+ limit: Optional[int] = None,
1992
+ page: Optional[int] = None,
1993
+ sort_by: Optional[str] = None,
1994
+ sort_order: Optional[str] = None,
1995
+ ) -> Tuple[List[KnowledgeRow], int]:
1996
+ """Get all knowledge contents from the database.
1997
+
1998
+ Args:
1999
+ limit (Optional[int]): The maximum number of knowledge contents to return.
2000
+ page (Optional[int]): The page number.
2001
+ sort_by (Optional[str]): The column to sort by.
2002
+ sort_order (Optional[str]): The order to sort by.
2003
+
2004
+ Returns:
2005
+ List[KnowledgeRow]: The knowledge contents.
2006
+
2007
+ Raises:
2008
+ Exception: If an error occurs during retrieval.
2009
+ """
2010
+ table = await self._get_table(table_type="knowledge")
2011
+
2012
+ try:
2013
+ async with self.async_session_factory() as sess, sess.begin():
2014
+ stmt = select(table)
2015
+
2016
+ # Apply sorting
2017
+ if sort_by is not None:
2018
+ stmt = stmt.order_by(getattr(table.c, sort_by) * (1 if sort_order == "asc" else -1))
2019
+
2020
+ # Get total count before applying limit and pagination
2021
+ count_stmt = select(func.count()).select_from(stmt.alias())
2022
+ total_count = await sess.scalar(count_stmt) or 0
2023
+
2024
+ # Apply pagination after count
2025
+ if limit is not None:
2026
+ stmt = stmt.limit(limit)
2027
+ if page is not None:
2028
+ stmt = stmt.offset((page - 1) * limit)
2029
+
2030
+ result = await sess.execute(stmt)
2031
+ records = result.fetchall()
2032
+ return [KnowledgeRow.model_validate(record._mapping) for record in records], total_count
2033
+
2034
+ except Exception as e:
2035
+ log_error(f"Exception getting knowledge contents: {e}")
2036
+ return [], 0
2037
+
2038
+ async def upsert_knowledge_content(self, knowledge_row: KnowledgeRow):
2039
+ """Upsert knowledge content in the database.
2040
+
2041
+ Args:
2042
+ knowledge_row (KnowledgeRow): The knowledge row to upsert.
2043
+
2044
+ Returns:
2045
+ Optional[KnowledgeRow]: The upserted knowledge row, or None if the operation fails.
2046
+ """
2047
+ try:
2048
+ table = await self._get_table(table_type="knowledge")
2049
+ async with self.async_session_factory() as sess, sess.begin():
2050
+ # Get the actual table columns to avoid "unconsumed column names" error
2051
+ table_columns = set(table.columns.keys())
2052
+
2053
+ # Only include fields that exist in the table and are not None
2054
+ insert_data = {}
2055
+ update_fields = {}
2056
+
2057
+ # Map of KnowledgeRow fields to table columns
2058
+ field_mapping = {
2059
+ "id": "id",
2060
+ "name": "name",
2061
+ "description": "description",
2062
+ "metadata": "metadata",
2063
+ "type": "type",
2064
+ "size": "size",
2065
+ "linked_to": "linked_to",
2066
+ "access_count": "access_count",
2067
+ "status": "status",
2068
+ "status_message": "status_message",
2069
+ "created_at": "created_at",
2070
+ "updated_at": "updated_at",
2071
+ "external_id": "external_id",
2072
+ }
2073
+
2074
+ # Build insert and update data only for fields that exist in the table
2075
+ for model_field, table_column in field_mapping.items():
2076
+ if table_column in table_columns:
2077
+ value = getattr(knowledge_row, model_field, None)
2078
+ if value is not None:
2079
+ insert_data[table_column] = value
2080
+ # Don't include ID in update_fields since it's the primary key
2081
+ if table_column != "id":
2082
+ update_fields[table_column] = value
2083
+
2084
+ # Ensure id is always included for the insert
2085
+ if "id" in table_columns and knowledge_row.id:
2086
+ insert_data["id"] = knowledge_row.id
2087
+
2088
+ # Handle case where update_fields is empty (all fields are None or don't exist in table)
2089
+ if not update_fields:
2090
+ # If we have insert_data, just do an insert without conflict resolution
2091
+ if insert_data:
2092
+ stmt = mysql.insert(table).values(insert_data)
2093
+ await sess.execute(stmt)
2094
+ else:
2095
+ # If we have no data at all, this is an error
2096
+ log_error("No valid fields found for knowledge row upsert")
2097
+ return None
2098
+ else:
2099
+ # Normal upsert with conflict resolution
2100
+ stmt = mysql.insert(table).values(insert_data).on_duplicate_key_update(**update_fields)
2101
+ await sess.execute(stmt)
2102
+
2103
+ log_debug(f"Upserted knowledge row with id '{knowledge_row.id}'")
2104
+
2105
+ return knowledge_row
2106
+
2107
+ except Exception as e:
2108
+ log_error(f"Error upserting knowledge row: {e}")
2109
+ return None
2110
+
2111
+ # -- Eval methods --
2112
+ async def create_eval_run(self, eval_run: EvalRunRecord) -> Optional[EvalRunRecord]:
2113
+ """Create an EvalRunRecord in the database.
2114
+
2115
+ Args:
2116
+ eval_run (EvalRunRecord): The eval run to create.
2117
+
2118
+ Returns:
2119
+ Optional[EvalRunRecord]: The created eval run, or None if the operation fails.
2120
+
2121
+ Raises:
2122
+ Exception: If an error occurs during creation.
2123
+ """
2124
+ try:
2125
+ table = await self._get_table(table_type="evals")
2126
+
2127
+ async with self.async_session_factory() as sess, sess.begin():
2128
+ current_time = int(time.time())
2129
+ stmt = mysql.insert(table).values(
2130
+ {"created_at": current_time, "updated_at": current_time, **eval_run.model_dump()}
2131
+ )
2132
+ await sess.execute(stmt)
2133
+
2134
+ log_debug(f"Created eval run with id '{eval_run.run_id}'")
2135
+
2136
+ return eval_run
2137
+
2138
+ except Exception as e:
2139
+ log_error(f"Error creating eval run: {e}")
2140
+ return None
2141
+
2142
+ async def delete_eval_run(self, eval_run_id: str) -> None:
2143
+ """Delete an eval run from the database.
2144
+
2145
+ Args:
2146
+ eval_run_id (str): The ID of the eval run to delete.
2147
+ """
2148
+ try:
2149
+ table = await self._get_table(table_type="evals")
2150
+
2151
+ async with self.async_session_factory() as sess, sess.begin():
2152
+ stmt = table.delete().where(table.c.run_id == eval_run_id)
2153
+ result = await sess.execute(stmt)
2154
+
2155
+ if result.rowcount == 0: # type: ignore
2156
+ log_warning(f"No eval run found with ID: {eval_run_id}")
2157
+ else:
2158
+ log_debug(f"Deleted eval run with ID: {eval_run_id}")
2159
+
2160
+ except Exception as e:
2161
+ log_error(f"Error deleting eval run {eval_run_id}: {e}")
2162
+
2163
+ async def delete_eval_runs(self, eval_run_ids: List[str]) -> None:
2164
+ """Delete multiple eval runs from the database.
2165
+
2166
+ Args:
2167
+ eval_run_ids (List[str]): List of eval run IDs to delete.
2168
+ """
2169
+ try:
2170
+ table = await self._get_table(table_type="evals")
2171
+
2172
+ async with self.async_session_factory() as sess, sess.begin():
2173
+ stmt = table.delete().where(table.c.run_id.in_(eval_run_ids))
2174
+ result = await sess.execute(stmt)
2175
+
2176
+ if result.rowcount == 0: # type: ignore
2177
+ log_warning(f"No eval runs found with IDs: {eval_run_ids}")
2178
+ else:
2179
+ log_debug(f"Deleted {result.rowcount} eval runs") # type: ignore
2180
+
2181
+ except Exception as e:
2182
+ log_error(f"Error deleting eval runs {eval_run_ids}: {e}")
2183
+
2184
+ async def get_eval_run(
2185
+ self, eval_run_id: str, deserialize: Optional[bool] = True
2186
+ ) -> Optional[Union[EvalRunRecord, Dict[str, Any]]]:
2187
+ """Get an eval run from the database.
2188
+
2189
+ Args:
2190
+ eval_run_id (str): The ID of the eval run to get.
2191
+ deserialize (Optional[bool]): Whether to serialize the eval run. Defaults to True.
2192
+
2193
+ Returns:
2194
+ Optional[Union[EvalRunRecord, Dict[str, Any]]]:
2195
+ - When deserialize=True: EvalRunRecord object
2196
+ - When deserialize=False: EvalRun dictionary
2197
+
2198
+ Raises:
2199
+ Exception: If an error occurs during retrieval.
2200
+ """
2201
+ try:
2202
+ table = await self._get_table(table_type="evals")
2203
+
2204
+ async with self.async_session_factory() as sess, sess.begin():
2205
+ stmt = select(table).where(table.c.run_id == eval_run_id)
2206
+ result = await sess.execute(stmt)
2207
+ row = result.fetchone()
2208
+ if row is None:
2209
+ return None
2210
+
2211
+ eval_run_raw = dict(row._mapping)
2212
+ if not deserialize:
2213
+ return eval_run_raw
2214
+
2215
+ return EvalRunRecord.model_validate(eval_run_raw)
2216
+
2217
+ except Exception as e:
2218
+ log_error(f"Exception getting eval run {eval_run_id}: {e}")
2219
+ return None
2220
+
2221
+ async def get_eval_runs(
2222
+ self,
2223
+ limit: Optional[int] = None,
2224
+ page: Optional[int] = None,
2225
+ sort_by: Optional[str] = None,
2226
+ sort_order: Optional[str] = None,
2227
+ agent_id: Optional[str] = None,
2228
+ team_id: Optional[str] = None,
2229
+ workflow_id: Optional[str] = None,
2230
+ model_id: Optional[str] = None,
2231
+ filter_type: Optional[EvalFilterType] = None,
2232
+ eval_type: Optional[List[EvalType]] = None,
2233
+ deserialize: Optional[bool] = True,
2234
+ ) -> Union[List[EvalRunRecord], Tuple[List[Dict[str, Any]], int]]:
2235
+ """Get all eval runs from the database.
2236
+
2237
+ Args:
2238
+ limit (Optional[int]): The maximum number of eval runs to return.
2239
+ page (Optional[int]): The page number.
2240
+ sort_by (Optional[str]): The column to sort by.
2241
+ sort_order (Optional[str]): The order to sort by.
2242
+ agent_id (Optional[str]): The ID of the agent to filter by.
2243
+ team_id (Optional[str]): The ID of the team to filter by.
2244
+ workflow_id (Optional[str]): The ID of the workflow to filter by.
2245
+ model_id (Optional[str]): The ID of the model to filter by.
2246
+ eval_type (Optional[List[EvalType]]): The type(s) of eval to filter by.
2247
+ filter_type (Optional[EvalFilterType]): Filter by component type (agent, team, workflow).
2248
+ deserialize (Optional[bool]): Whether to serialize the eval runs. Defaults to True.
2249
+
2250
+ Returns:
2251
+ Union[List[EvalRunRecord], Tuple[List[Dict[str, Any]], int]]:
2252
+ - When deserialize=True: List of EvalRunRecord objects
2253
+ - When deserialize=False: List of dictionaries
2254
+
2255
+ Raises:
2256
+ Exception: If an error occurs during retrieval.
2257
+ """
2258
+ try:
2259
+ table = await self._get_table(table_type="evals")
2260
+
2261
+ async with self.async_session_factory() as sess, sess.begin():
2262
+ stmt = select(table)
2263
+
2264
+ # Filtering
2265
+ if agent_id is not None:
2266
+ stmt = stmt.where(table.c.agent_id == agent_id)
2267
+ if team_id is not None:
2268
+ stmt = stmt.where(table.c.team_id == team_id)
2269
+ if workflow_id is not None:
2270
+ stmt = stmt.where(table.c.workflow_id == workflow_id)
2271
+ if model_id is not None:
2272
+ stmt = stmt.where(table.c.model_id == model_id)
2273
+ if eval_type is not None and len(eval_type) > 0:
2274
+ stmt = stmt.where(table.c.eval_type.in_(eval_type))
2275
+ if filter_type is not None:
2276
+ if filter_type == EvalFilterType.AGENT:
2277
+ stmt = stmt.where(table.c.agent_id.is_not(None))
2278
+ elif filter_type == EvalFilterType.TEAM:
2279
+ stmt = stmt.where(table.c.team_id.is_not(None))
2280
+ elif filter_type == EvalFilterType.WORKFLOW:
2281
+ stmt = stmt.where(table.c.workflow_id.is_not(None))
2282
+
2283
+ # Get total count after applying filtering
2284
+ count_stmt = select(func.count()).select_from(stmt.alias())
2285
+ total_count = await sess.scalar(count_stmt) or 0
2286
+
2287
+ # Sorting
2288
+ if sort_by is None:
2289
+ stmt = stmt.order_by(table.c.created_at.desc())
2290
+ else:
2291
+ stmt = apply_sorting(stmt, table, sort_by, sort_order)
2292
+
2293
+ # Paginating
2294
+ if limit is not None:
2295
+ stmt = stmt.limit(limit)
2296
+ if page is not None:
2297
+ stmt = stmt.offset((page - 1) * limit)
2298
+
2299
+ result = await sess.execute(stmt)
2300
+ records = result.fetchall()
2301
+ if not records:
2302
+ return [] if deserialize else ([], 0)
2303
+
2304
+ eval_runs_raw = [dict(row._mapping) for row in records]
2305
+ if not deserialize:
2306
+ return eval_runs_raw, total_count
2307
+
2308
+ return [EvalRunRecord.model_validate(row) for row in eval_runs_raw]
2309
+
2310
+ except Exception as e:
2311
+ log_error(f"Exception getting eval runs: {e}")
2312
+ return [] if deserialize else ([], 0)
2313
+
2314
+ async def rename_eval_run(
2315
+ self, eval_run_id: str, name: str, deserialize: Optional[bool] = True
2316
+ ) -> Optional[Union[EvalRunRecord, Dict[str, Any]]]:
2317
+ """Upsert the name of an eval run in the database, returning raw dictionary.
2318
+
2319
+ Args:
2320
+ eval_run_id (str): The ID of the eval run to update.
2321
+ name (str): The new name of the eval run.
2322
+
2323
+ Returns:
2324
+ Optional[Dict[str, Any]]: The updated eval run, or None if the operation fails.
2325
+
2326
+ Raises:
2327
+ Exception: If an error occurs during update.
2328
+ """
2329
+ try:
2330
+ table = await self._get_table(table_type="evals")
2331
+ async with self.async_session_factory() as sess, sess.begin():
2332
+ stmt = (
2333
+ table.update().where(table.c.run_id == eval_run_id).values(name=name, updated_at=int(time.time()))
2334
+ )
2335
+ await sess.execute(stmt)
2336
+
2337
+ eval_run_raw = await self.get_eval_run(eval_run_id=eval_run_id, deserialize=deserialize)
2338
+ if not eval_run_raw or not deserialize:
2339
+ return eval_run_raw
2340
+
2341
+ return EvalRunRecord.model_validate(eval_run_raw)
2342
+
2343
+ except Exception as e:
2344
+ log_error(f"Error upserting eval run name {eval_run_id}: {e}")
2345
+ return None
2346
+
2347
+ # -- Migrations --
2348
+
2349
+ async def migrate_table_from_v1_to_v2(self, v1_db_schema: str, v1_table_name: str, v1_table_type: str):
2350
+ """Migrate all content in the given table to the right v2 table"""
2351
+
2352
+ from typing import Sequence
2353
+
2354
+ from agno.db.migrations.v1_to_v2 import (
2355
+ get_all_table_content,
2356
+ parse_agent_sessions,
2357
+ parse_memories,
2358
+ parse_team_sessions,
2359
+ parse_workflow_sessions,
2360
+ )
2361
+
2362
+ # Get all content from the old table
2363
+ old_content: list[dict[str, Any]] = get_all_table_content(
2364
+ db=self,
2365
+ db_schema=v1_db_schema,
2366
+ table_name=v1_table_name,
2367
+ )
2368
+ if not old_content:
2369
+ log_info(f"No content to migrate from table {v1_table_name}")
2370
+ return
2371
+
2372
+ # Parse the content into the new format
2373
+ memories: List[UserMemory] = []
2374
+ sessions: Sequence[Union[AgentSession, TeamSession, WorkflowSession]] = []
2375
+ if v1_table_type == "agent_sessions":
2376
+ sessions = parse_agent_sessions(old_content)
2377
+ elif v1_table_type == "team_sessions":
2378
+ sessions = parse_team_sessions(old_content)
2379
+ elif v1_table_type == "workflow_sessions":
2380
+ sessions = parse_workflow_sessions(old_content)
2381
+ elif v1_table_type == "memories":
2382
+ memories = parse_memories(old_content)
2383
+ else:
2384
+ raise ValueError(f"Invalid table type: {v1_table_type}")
2385
+
2386
+ # Insert the new content into the new table
2387
+ if v1_table_type == "agent_sessions":
2388
+ for session in sessions:
2389
+ await self.upsert_session(session)
2390
+ log_info(f"Migrated {len(sessions)} Agent sessions to table: {self.session_table_name}")
2391
+
2392
+ elif v1_table_type == "team_sessions":
2393
+ for session in sessions:
2394
+ await self.upsert_session(session)
2395
+ log_info(f"Migrated {len(sessions)} Team sessions to table: {self.session_table_name}")
2396
+
2397
+ elif v1_table_type == "workflow_sessions":
2398
+ for session in sessions:
2399
+ await self.upsert_session(session)
2400
+ log_info(f"Migrated {len(sessions)} Workflow sessions to table: {self.session_table_name}")
2401
+
2402
+ elif v1_table_type == "memories":
2403
+ for memory in memories:
2404
+ await self.upsert_user_memory(memory)
2405
+ log_info(f"Migrated {len(memories)} memories to table: {self.memory_table}")
2406
+
2407
+ # --- Traces ---
2408
+ def _get_traces_base_query(self, table: Table, spans_table: Optional[Table] = None):
2409
+ """Build base query for traces with aggregated span counts.
2410
+
2411
+ Args:
2412
+ table: The traces table.
2413
+ spans_table: The spans table (optional).
2414
+
2415
+ Returns:
2416
+ SQLAlchemy select statement with total_spans and error_count calculated dynamically.
2417
+ """
2418
+ from sqlalchemy import case, literal
2419
+
2420
+ if spans_table is not None:
2421
+ # JOIN with spans table to calculate total_spans and error_count
2422
+ return (
2423
+ select(
2424
+ table,
2425
+ func.coalesce(func.count(spans_table.c.span_id), 0).label("total_spans"),
2426
+ func.coalesce(func.sum(case((spans_table.c.status_code == "ERROR", 1), else_=0)), 0).label(
2427
+ "error_count"
2428
+ ),
2429
+ )
2430
+ .select_from(table.outerjoin(spans_table, table.c.trace_id == spans_table.c.trace_id))
2431
+ .group_by(table.c.trace_id)
2432
+ )
2433
+ else:
2434
+ # Fallback if spans table doesn't exist
2435
+ return select(table, literal(0).label("total_spans"), literal(0).label("error_count"))
2436
+
2437
+ async def create_trace(self, trace: "Trace") -> None:
2438
+ """Create a single trace record in the database.
2439
+
2440
+ Args:
2441
+ trace: The Trace object to store (one per trace_id).
2442
+ """
2443
+ try:
2444
+ table = await self._get_table(table_type="traces", create_table_if_not_found=True)
2445
+ if table is None:
2446
+ return
2447
+
2448
+ async with self.async_session_factory() as sess, sess.begin():
2449
+ # Check if trace exists
2450
+ result = await sess.execute(select(table).where(table.c.trace_id == trace.trace_id))
2451
+ existing = result.fetchone()
2452
+
2453
+ if existing:
2454
+ # workflow (level 3) > team (level 2) > agent (level 1) > child/unknown (level 0)
2455
+
2456
+ def get_component_level(workflow_id, team_id, agent_id, name):
2457
+ # Check if name indicates a root span
2458
+ is_root_name = ".run" in name or ".arun" in name
2459
+
2460
+ if not is_root_name:
2461
+ return 0 # Child span (not a root)
2462
+ elif workflow_id:
2463
+ return 3 # Workflow root
2464
+ elif team_id:
2465
+ return 2 # Team root
2466
+ elif agent_id:
2467
+ return 1 # Agent root
2468
+ else:
2469
+ return 0 # Unknown
2470
+
2471
+ existing_level = get_component_level(
2472
+ existing.workflow_id, existing.team_id, existing.agent_id, existing.name
2473
+ )
2474
+ new_level = get_component_level(trace.workflow_id, trace.team_id, trace.agent_id, trace.name)
2475
+
2476
+ # Only update name if new trace is from a higher or equal level
2477
+ should_update_name = new_level > existing_level
2478
+
2479
+ # Parse existing start_time to calculate correct duration
2480
+ existing_start_time_str = existing.start_time
2481
+ if isinstance(existing_start_time_str, str):
2482
+ existing_start_time = datetime.fromisoformat(existing_start_time_str.replace("Z", "+00:00"))
2483
+ else:
2484
+ existing_start_time = trace.start_time
2485
+
2486
+ recalculated_duration_ms = int((trace.end_time - existing_start_time).total_seconds() * 1000)
2487
+
2488
+ update_values = {
2489
+ "end_time": trace.end_time.isoformat(),
2490
+ "duration_ms": recalculated_duration_ms,
2491
+ "status": trace.status,
2492
+ "name": trace.name if should_update_name else existing.name,
2493
+ }
2494
+
2495
+ # Update context fields ONLY if new value is not None (preserve non-null values)
2496
+ if trace.run_id is not None:
2497
+ update_values["run_id"] = trace.run_id
2498
+ if trace.session_id is not None:
2499
+ update_values["session_id"] = trace.session_id
2500
+ if trace.user_id is not None:
2501
+ update_values["user_id"] = trace.user_id
2502
+ if trace.agent_id is not None:
2503
+ update_values["agent_id"] = trace.agent_id
2504
+ if trace.team_id is not None:
2505
+ update_values["team_id"] = trace.team_id
2506
+ if trace.workflow_id is not None:
2507
+ update_values["workflow_id"] = trace.workflow_id
2508
+
2509
+ stmt = update(table).where(table.c.trace_id == trace.trace_id).values(**update_values)
2510
+ await sess.execute(stmt)
2511
+ else:
2512
+ trace_dict = trace.to_dict()
2513
+ trace_dict.pop("total_spans", None)
2514
+ trace_dict.pop("error_count", None)
2515
+ stmt = mysql.insert(table).values(trace_dict)
2516
+ await sess.execute(stmt)
2517
+
2518
+ except Exception as e:
2519
+ log_error(f"Error creating trace: {e}")
2520
+ # Don't raise - tracing should not break the main application flow
2521
+
2522
+ async def get_trace(
2523
+ self,
2524
+ trace_id: Optional[str] = None,
2525
+ run_id: Optional[str] = None,
2526
+ ):
2527
+ """Get a single trace by trace_id or other filters.
2528
+
2529
+ Args:
2530
+ trace_id: The unique trace identifier.
2531
+ run_id: Filter by run ID (returns first match).
2532
+
2533
+ Returns:
2534
+ Optional[Trace]: The trace if found, None otherwise.
2535
+
2536
+ Note:
2537
+ If multiple filters are provided, trace_id takes precedence.
2538
+ For other filters, the most recent trace is returned.
2539
+ """
2540
+ try:
2541
+ from agno.tracing.schemas import Trace
2542
+
2543
+ table = await self._get_table(table_type="traces")
2544
+ if table is None:
2545
+ return None
2546
+
2547
+ # Get spans table for JOIN
2548
+ spans_table = await self._get_table(table_type="spans")
2549
+
2550
+ async with self.async_session_factory() as sess:
2551
+ # Build query with aggregated span counts
2552
+ stmt = self._get_traces_base_query(table, spans_table)
2553
+
2554
+ if trace_id:
2555
+ stmt = stmt.where(table.c.trace_id == trace_id)
2556
+ elif run_id:
2557
+ stmt = stmt.where(table.c.run_id == run_id)
2558
+ else:
2559
+ log_debug("get_trace called without any filter parameters")
2560
+ return None
2561
+
2562
+ # Order by most recent and get first result
2563
+ stmt = stmt.order_by(table.c.start_time.desc()).limit(1)
2564
+ result = await sess.execute(stmt)
2565
+ row = result.fetchone()
2566
+
2567
+ if row:
2568
+ return Trace.from_dict(dict(row._mapping))
2569
+ return None
2570
+
2571
+ except Exception as e:
2572
+ log_error(f"Error getting trace: {e}")
2573
+ return None
2574
+
2575
+ async def get_traces(
2576
+ self,
2577
+ run_id: Optional[str] = None,
2578
+ session_id: Optional[str] = None,
2579
+ user_id: Optional[str] = None,
2580
+ agent_id: Optional[str] = None,
2581
+ team_id: Optional[str] = None,
2582
+ workflow_id: Optional[str] = None,
2583
+ status: Optional[str] = None,
2584
+ start_time: Optional[datetime] = None,
2585
+ end_time: Optional[datetime] = None,
2586
+ limit: Optional[int] = 20,
2587
+ page: Optional[int] = 1,
2588
+ ) -> tuple[List, int]:
2589
+ """Get traces matching the provided filters with pagination.
2590
+
2591
+ Args:
2592
+ run_id: Filter by run ID.
2593
+ session_id: Filter by session ID.
2594
+ user_id: Filter by user ID.
2595
+ agent_id: Filter by agent ID.
2596
+ team_id: Filter by team ID.
2597
+ workflow_id: Filter by workflow ID.
2598
+ status: Filter by status (OK, ERROR, UNSET).
2599
+ start_time: Filter traces starting after this datetime.
2600
+ end_time: Filter traces ending before this datetime.
2601
+ limit: Maximum number of traces to return per page.
2602
+ page: Page number (1-indexed).
2603
+
2604
+ Returns:
2605
+ tuple[List[Trace], int]: Tuple of (list of matching traces, total count).
2606
+ """
2607
+ try:
2608
+ from agno.tracing.schemas import Trace
2609
+
2610
+ log_debug(
2611
+ f"get_traces called with filters: run_id={run_id}, session_id={session_id}, user_id={user_id}, agent_id={agent_id}, page={page}, limit={limit}"
2612
+ )
2613
+
2614
+ table = await self._get_table(table_type="traces")
2615
+ if table is None:
2616
+ log_debug("Traces table not found")
2617
+ return [], 0
2618
+
2619
+ # Get spans table for JOIN
2620
+ spans_table = await self._get_table(table_type="spans")
2621
+
2622
+ async with self.async_session_factory() as sess:
2623
+ # Build base query with aggregated span counts
2624
+ base_stmt = self._get_traces_base_query(table, spans_table)
2625
+
2626
+ # Apply filters
2627
+ if run_id:
2628
+ base_stmt = base_stmt.where(table.c.run_id == run_id)
2629
+ if session_id:
2630
+ base_stmt = base_stmt.where(table.c.session_id == session_id)
2631
+ if user_id:
2632
+ base_stmt = base_stmt.where(table.c.user_id == user_id)
2633
+ if agent_id:
2634
+ base_stmt = base_stmt.where(table.c.agent_id == agent_id)
2635
+ if team_id:
2636
+ base_stmt = base_stmt.where(table.c.team_id == team_id)
2637
+ if workflow_id:
2638
+ base_stmt = base_stmt.where(table.c.workflow_id == workflow_id)
2639
+ if status:
2640
+ base_stmt = base_stmt.where(table.c.status == status)
2641
+ if start_time:
2642
+ # Convert datetime to ISO string for comparison
2643
+ base_stmt = base_stmt.where(table.c.start_time >= start_time.isoformat())
2644
+ if end_time:
2645
+ # Convert datetime to ISO string for comparison
2646
+ base_stmt = base_stmt.where(table.c.end_time <= end_time.isoformat())
2647
+
2648
+ # Get total count
2649
+ count_stmt = select(func.count()).select_from(base_stmt.alias())
2650
+ count_result = await sess.execute(count_stmt)
2651
+ total_count = count_result.scalar() or 0
2652
+
2653
+ # Apply pagination
2654
+ offset = (page - 1) * limit if page and limit else 0
2655
+ paginated_stmt = base_stmt.order_by(table.c.start_time.desc()).limit(limit).offset(offset)
2656
+
2657
+ result = await sess.execute(paginated_stmt)
2658
+ results = result.fetchall()
2659
+
2660
+ traces = [Trace.from_dict(dict(row._mapping)) for row in results]
2661
+ return traces, total_count
2662
+
2663
+ except Exception as e:
2664
+ log_error(f"Error getting traces: {e}")
2665
+ return [], 0
2666
+
2667
+ async def get_trace_stats(
2668
+ self,
2669
+ user_id: Optional[str] = None,
2670
+ agent_id: Optional[str] = None,
2671
+ team_id: Optional[str] = None,
2672
+ workflow_id: Optional[str] = None,
2673
+ start_time: Optional[datetime] = None,
2674
+ end_time: Optional[datetime] = None,
2675
+ limit: Optional[int] = 20,
2676
+ page: Optional[int] = 1,
2677
+ ) -> tuple[List[Dict[str, Any]], int]:
2678
+ """Get trace statistics grouped by session.
2679
+
2680
+ Args:
2681
+ user_id: Filter by user ID.
2682
+ agent_id: Filter by agent ID.
2683
+ team_id: Filter by team ID.
2684
+ workflow_id: Filter by workflow ID.
2685
+ start_time: Filter sessions with traces created after this datetime.
2686
+ end_time: Filter sessions with traces created before this datetime.
2687
+ limit: Maximum number of sessions to return per page.
2688
+ page: Page number (1-indexed).
2689
+
2690
+ Returns:
2691
+ tuple[List[Dict], int]: Tuple of (list of session stats dicts, total count).
2692
+ Each dict contains: session_id, user_id, agent_id, team_id, total_traces,
2693
+ workflow_id, first_trace_at, last_trace_at.
2694
+ """
2695
+ try:
2696
+ table = await self._get_table(table_type="traces")
2697
+ if table is None:
2698
+ log_debug("Traces table not found")
2699
+ return [], 0
2700
+
2701
+ async with self.async_session_factory() as sess:
2702
+ # Build base query grouped by session_id
2703
+ base_stmt = (
2704
+ select(
2705
+ table.c.session_id,
2706
+ table.c.user_id,
2707
+ table.c.agent_id,
2708
+ table.c.team_id,
2709
+ table.c.workflow_id,
2710
+ func.count(table.c.trace_id).label("total_traces"),
2711
+ func.min(table.c.created_at).label("first_trace_at"),
2712
+ func.max(table.c.created_at).label("last_trace_at"),
2713
+ )
2714
+ .where(table.c.session_id.isnot(None)) # Only sessions with session_id
2715
+ .group_by(
2716
+ table.c.session_id, table.c.user_id, table.c.agent_id, table.c.team_id, table.c.workflow_id
2717
+ )
2718
+ )
2719
+
2720
+ # Apply filters
2721
+ if user_id:
2722
+ base_stmt = base_stmt.where(table.c.user_id == user_id)
2723
+ if workflow_id:
2724
+ base_stmt = base_stmt.where(table.c.workflow_id == workflow_id)
2725
+ if team_id:
2726
+ base_stmt = base_stmt.where(table.c.team_id == team_id)
2727
+ if agent_id:
2728
+ base_stmt = base_stmt.where(table.c.agent_id == agent_id)
2729
+ if start_time:
2730
+ # Convert datetime to ISO string for comparison
2731
+ base_stmt = base_stmt.where(table.c.created_at >= start_time.isoformat())
2732
+ if end_time:
2733
+ # Convert datetime to ISO string for comparison
2734
+ base_stmt = base_stmt.where(table.c.created_at <= end_time.isoformat())
2735
+
2736
+ # Get total count of sessions
2737
+ count_stmt = select(func.count()).select_from(base_stmt.alias())
2738
+ count_result = await sess.execute(count_stmt)
2739
+ total_count = count_result.scalar() or 0
2740
+
2741
+ # Apply pagination and ordering
2742
+ offset = (page - 1) * limit if page and limit else 0
2743
+ paginated_stmt = base_stmt.order_by(func.max(table.c.created_at).desc()).limit(limit).offset(offset)
2744
+
2745
+ result = await sess.execute(paginated_stmt)
2746
+ results = result.fetchall()
2747
+
2748
+ # Convert to list of dicts with datetime objects
2749
+ stats_list = []
2750
+ for row in results:
2751
+ # Convert ISO strings to datetime objects
2752
+ first_trace_at_str = row.first_trace_at
2753
+ last_trace_at_str = row.last_trace_at
2754
+
2755
+ # Parse ISO format strings to datetime objects
2756
+ first_trace_at = datetime.fromisoformat(first_trace_at_str.replace("Z", "+00:00"))
2757
+ last_trace_at = datetime.fromisoformat(last_trace_at_str.replace("Z", "+00:00"))
2758
+
2759
+ stats_list.append(
2760
+ {
2761
+ "session_id": row.session_id,
2762
+ "user_id": row.user_id,
2763
+ "agent_id": row.agent_id,
2764
+ "team_id": row.team_id,
2765
+ "workflow_id": row.workflow_id,
2766
+ "total_traces": row.total_traces,
2767
+ "first_trace_at": first_trace_at,
2768
+ "last_trace_at": last_trace_at,
2769
+ }
2770
+ )
2771
+
2772
+ return stats_list, total_count
2773
+
2774
+ except Exception as e:
2775
+ log_error(f"Error getting trace stats: {e}")
2776
+ return [], 0
2777
+
2778
+ # --- Spans ---
2779
+ async def create_span(self, span: "Span") -> None:
2780
+ """Create a single span in the database.
2781
+
2782
+ Args:
2783
+ span: The Span object to store.
2784
+ """
2785
+ try:
2786
+ table = await self._get_table(table_type="spans", create_table_if_not_found=True)
2787
+ if table is None:
2788
+ return
2789
+
2790
+ async with self.async_session_factory() as sess, sess.begin():
2791
+ stmt = mysql.insert(table).values(span.to_dict())
2792
+ await sess.execute(stmt)
2793
+
2794
+ except Exception as e:
2795
+ log_error(f"Error creating span: {e}")
2796
+
2797
+ async def create_spans(self, spans: List) -> None:
2798
+ """Create multiple spans in the database as a batch.
2799
+
2800
+ Args:
2801
+ spans: List of Span objects to store.
2802
+ """
2803
+ if not spans:
2804
+ return
2805
+
2806
+ try:
2807
+ table = await self._get_table(table_type="spans", create_table_if_not_found=True)
2808
+ if table is None:
2809
+ return
2810
+
2811
+ async with self.async_session_factory() as sess, sess.begin():
2812
+ for span in spans:
2813
+ stmt = mysql.insert(table).values(span.to_dict())
2814
+ await sess.execute(stmt)
2815
+
2816
+ except Exception as e:
2817
+ log_error(f"Error creating spans batch: {e}")
2818
+
2819
+ async def get_span(self, span_id: str):
2820
+ """Get a single span by its span_id.
2821
+
2822
+ Args:
2823
+ span_id: The unique span identifier.
2824
+
2825
+ Returns:
2826
+ Optional[Span]: The span if found, None otherwise.
2827
+ """
2828
+ try:
2829
+ from agno.tracing.schemas import Span
2830
+
2831
+ table = await self._get_table(table_type="spans")
2832
+ if table is None:
2833
+ return None
2834
+
2835
+ async with self.async_session_factory() as sess:
2836
+ stmt = select(table).where(table.c.span_id == span_id)
2837
+ result = await sess.execute(stmt)
2838
+ row = result.fetchone()
2839
+ if row:
2840
+ return Span.from_dict(dict(row._mapping))
2841
+ return None
2842
+
2843
+ except Exception as e:
2844
+ log_error(f"Error getting span: {e}")
2845
+ return None
2846
+
2847
+ async def get_spans(
2848
+ self,
2849
+ trace_id: Optional[str] = None,
2850
+ parent_span_id: Optional[str] = None,
2851
+ limit: Optional[int] = 1000,
2852
+ ) -> List:
2853
+ """Get spans matching the provided filters.
2854
+
2855
+ Args:
2856
+ trace_id: Filter by trace ID.
2857
+ parent_span_id: Filter by parent span ID.
2858
+ limit: Maximum number of spans to return.
2859
+
2860
+ Returns:
2861
+ List[Span]: List of matching spans.
2862
+ """
2863
+ try:
2864
+ from agno.tracing.schemas import Span
2865
+
2866
+ table = await self._get_table(table_type="spans")
2867
+ if table is None:
2868
+ return []
2869
+
2870
+ async with self.async_session_factory() as sess:
2871
+ stmt = select(table)
2872
+
2873
+ # Apply filters
2874
+ if trace_id:
2875
+ stmt = stmt.where(table.c.trace_id == trace_id)
2876
+ if parent_span_id:
2877
+ stmt = stmt.where(table.c.parent_span_id == parent_span_id)
2878
+
2879
+ if limit:
2880
+ stmt = stmt.limit(limit)
2881
+
2882
+ result = await sess.execute(stmt)
2883
+ results = result.fetchall()
2884
+ return [Span.from_dict(dict(row._mapping)) for row in results]
2885
+
2886
+ except Exception as e:
2887
+ log_error(f"Error getting spans: {e}")
2888
+ return []