agno 2.1.3__py3-none-any.whl → 2.1.5__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 (94) hide show
  1. agno/agent/agent.py +1779 -577
  2. agno/db/async_postgres/__init__.py +3 -0
  3. agno/db/async_postgres/async_postgres.py +1668 -0
  4. agno/db/async_postgres/schemas.py +124 -0
  5. agno/db/async_postgres/utils.py +289 -0
  6. agno/db/base.py +237 -2
  7. agno/db/dynamo/dynamo.py +10 -8
  8. agno/db/dynamo/schemas.py +1 -10
  9. agno/db/dynamo/utils.py +2 -2
  10. agno/db/firestore/firestore.py +2 -2
  11. agno/db/firestore/utils.py +4 -2
  12. agno/db/gcs_json/gcs_json_db.py +2 -2
  13. agno/db/in_memory/in_memory_db.py +2 -2
  14. agno/db/json/json_db.py +2 -2
  15. agno/db/migrations/v1_to_v2.py +30 -13
  16. agno/db/mongo/mongo.py +18 -6
  17. agno/db/mysql/mysql.py +35 -13
  18. agno/db/postgres/postgres.py +29 -6
  19. agno/db/redis/redis.py +2 -2
  20. agno/db/singlestore/singlestore.py +2 -2
  21. agno/db/sqlite/sqlite.py +34 -12
  22. agno/db/sqlite/utils.py +8 -3
  23. agno/eval/accuracy.py +50 -43
  24. agno/eval/performance.py +6 -3
  25. agno/eval/reliability.py +6 -3
  26. agno/eval/utils.py +33 -16
  27. agno/exceptions.py +8 -2
  28. agno/knowledge/embedder/fastembed.py +1 -1
  29. agno/knowledge/knowledge.py +260 -46
  30. agno/knowledge/reader/pdf_reader.py +4 -6
  31. agno/knowledge/reader/reader_factory.py +2 -3
  32. agno/memory/manager.py +241 -33
  33. agno/models/anthropic/claude.py +37 -0
  34. agno/os/app.py +15 -10
  35. agno/os/interfaces/a2a/router.py +3 -5
  36. agno/os/interfaces/agui/router.py +4 -1
  37. agno/os/interfaces/agui/utils.py +33 -6
  38. agno/os/interfaces/slack/router.py +2 -4
  39. agno/os/mcp.py +98 -41
  40. agno/os/router.py +23 -0
  41. agno/os/routers/evals/evals.py +52 -20
  42. agno/os/routers/evals/utils.py +14 -14
  43. agno/os/routers/knowledge/knowledge.py +130 -9
  44. agno/os/routers/knowledge/schemas.py +57 -0
  45. agno/os/routers/memory/memory.py +116 -44
  46. agno/os/routers/metrics/metrics.py +16 -6
  47. agno/os/routers/session/session.py +65 -22
  48. agno/os/schema.py +38 -0
  49. agno/os/utils.py +69 -13
  50. agno/reasoning/anthropic.py +80 -0
  51. agno/reasoning/gemini.py +73 -0
  52. agno/reasoning/openai.py +5 -0
  53. agno/reasoning/vertexai.py +76 -0
  54. agno/session/workflow.py +69 -1
  55. agno/team/team.py +934 -241
  56. agno/tools/function.py +36 -18
  57. agno/tools/google_drive.py +270 -0
  58. agno/tools/googlesheets.py +20 -5
  59. agno/tools/mcp_toolbox.py +3 -3
  60. agno/tools/scrapegraph.py +1 -1
  61. agno/utils/models/claude.py +3 -1
  62. agno/utils/print_response/workflow.py +112 -12
  63. agno/utils/streamlit.py +1 -1
  64. agno/vectordb/base.py +22 -1
  65. agno/vectordb/cassandra/cassandra.py +9 -0
  66. agno/vectordb/chroma/chromadb.py +26 -6
  67. agno/vectordb/clickhouse/clickhousedb.py +9 -1
  68. agno/vectordb/couchbase/couchbase.py +11 -0
  69. agno/vectordb/lancedb/lance_db.py +20 -0
  70. agno/vectordb/langchaindb/langchaindb.py +11 -0
  71. agno/vectordb/lightrag/lightrag.py +9 -0
  72. agno/vectordb/llamaindex/llamaindexdb.py +15 -1
  73. agno/vectordb/milvus/milvus.py +23 -0
  74. agno/vectordb/mongodb/mongodb.py +22 -0
  75. agno/vectordb/pgvector/pgvector.py +19 -0
  76. agno/vectordb/pineconedb/pineconedb.py +35 -4
  77. agno/vectordb/qdrant/qdrant.py +24 -0
  78. agno/vectordb/singlestore/singlestore.py +25 -17
  79. agno/vectordb/surrealdb/surrealdb.py +18 -1
  80. agno/vectordb/upstashdb/upstashdb.py +26 -1
  81. agno/vectordb/weaviate/weaviate.py +18 -0
  82. agno/workflow/condition.py +29 -0
  83. agno/workflow/loop.py +29 -0
  84. agno/workflow/parallel.py +141 -113
  85. agno/workflow/router.py +29 -0
  86. agno/workflow/step.py +146 -25
  87. agno/workflow/steps.py +29 -0
  88. agno/workflow/types.py +26 -1
  89. agno/workflow/workflow.py +507 -22
  90. {agno-2.1.3.dist-info → agno-2.1.5.dist-info}/METADATA +100 -41
  91. {agno-2.1.3.dist-info → agno-2.1.5.dist-info}/RECORD +94 -86
  92. {agno-2.1.3.dist-info → agno-2.1.5.dist-info}/WHEEL +0 -0
  93. {agno-2.1.3.dist-info → agno-2.1.5.dist-info}/licenses/LICENSE +0 -0
  94. {agno-2.1.3.dist-info → agno-2.1.5.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1668 @@
1
+ import time
2
+ from datetime import date, datetime, timedelta, timezone
3
+ from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
4
+ from uuid import uuid4
5
+
6
+ from agno.db.async_postgres.schemas import get_table_schema_definition
7
+ from agno.db.async_postgres.utils import (
8
+ apply_sorting,
9
+ bulk_upsert_metrics,
10
+ calculate_date_metrics,
11
+ create_schema,
12
+ fetch_all_sessions_data,
13
+ get_dates_to_calculate_metrics_for,
14
+ is_table_available,
15
+ is_valid_table,
16
+ )
17
+ from agno.db.base import AsyncBaseDb, SessionType
18
+ from agno.db.schemas.evals import EvalFilterType, EvalRunRecord, EvalType
19
+ from agno.db.schemas.knowledge import KnowledgeRow
20
+ from agno.db.schemas.memory import UserMemory
21
+ from agno.session import AgentSession, Session, TeamSession, WorkflowSession
22
+ from agno.utils.log import log_debug, log_error, log_info, log_warning
23
+
24
+ try:
25
+ from sqlalchemy import Index, String, UniqueConstraint, func, update
26
+ from sqlalchemy.dialects import postgresql
27
+ from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine
28
+ from sqlalchemy.schema import Column, MetaData, Table
29
+ from sqlalchemy.sql.expression import select, text
30
+ except ImportError:
31
+ raise ImportError("`sqlalchemy` not installed. Please install it using `pip install sqlalchemy`")
32
+
33
+
34
+ class AsyncPostgresDb(AsyncBaseDb):
35
+ def __init__(
36
+ self,
37
+ db_id: Optional[str] = None,
38
+ db_url: Optional[str] = None,
39
+ db_engine: Optional[AsyncEngine] = None,
40
+ db_schema: Optional[str] = None,
41
+ session_table: Optional[str] = None,
42
+ memory_table: Optional[str] = None,
43
+ metrics_table: Optional[str] = None,
44
+ eval_table: Optional[str] = None,
45
+ knowledge_table: Optional[str] = None,
46
+ ):
47
+ """
48
+ Async interface for interacting with a PostgreSQL database.
49
+
50
+ The following order is used to determine the database connection:
51
+ 1. Use the db_engine if provided
52
+ 2. Use the db_url
53
+ 3. Raise an error if neither is provided
54
+
55
+ Args:
56
+ db_id (Optional[str]): The ID of the database.
57
+ db_url (Optional[str]): The database URL to connect to.
58
+ db_engine (Optional[AsyncEngine]): The SQLAlchemy async database engine to use.
59
+ db_schema (Optional[str]): The database schema to use.
60
+ session_table (Optional[str]): Name of the table to store Agent, Team and Workflow sessions.
61
+ memory_table (Optional[str]): Name of the table to store memories.
62
+ metrics_table (Optional[str]): Name of the table to store metrics.
63
+ eval_table (Optional[str]): Name of the table to store evaluation runs data.
64
+ knowledge_table (Optional[str]): Name of the table to store knowledge content.
65
+
66
+ Raises:
67
+ ValueError: If neither db_url nor db_engine is provided.
68
+ ValueError: If none of the tables are provided.
69
+ """
70
+ super().__init__(
71
+ id=db_id,
72
+ session_table=session_table,
73
+ memory_table=memory_table,
74
+ metrics_table=metrics_table,
75
+ eval_table=eval_table,
76
+ knowledge_table=knowledge_table,
77
+ )
78
+
79
+ _engine: Optional[AsyncEngine] = db_engine
80
+ if _engine is None and db_url is not None:
81
+ _engine = create_async_engine(db_url)
82
+ if _engine is None:
83
+ raise ValueError("One of db_url or db_engine must be provided")
84
+
85
+ self.db_url: Optional[str] = db_url
86
+ self.db_engine: AsyncEngine = _engine
87
+ self.db_schema: str = db_schema if db_schema is not None else "ai"
88
+ self.metadata: MetaData = MetaData()
89
+
90
+ # Initialize database session factory
91
+ self.async_session_factory = async_sessionmaker(bind=self.db_engine)
92
+
93
+ # -- DB methods --
94
+ async def _create_table(self, table_name: str, table_type: str, db_schema: str) -> Table:
95
+ """
96
+ Create a table with the appropriate schema based on the table type.
97
+
98
+ Args:
99
+ table_name (str): Name of the table to create
100
+ table_type (str): Type of table (used to get schema definition)
101
+ db_schema (str): Database schema name
102
+
103
+ Returns:
104
+ Table: SQLAlchemy Table object
105
+ """
106
+ try:
107
+ table_schema = get_table_schema_definition(table_type).copy()
108
+
109
+ columns: List[Column] = []
110
+ indexes: List[str] = []
111
+ unique_constraints: List[str] = []
112
+ schema_unique_constraints = table_schema.pop("_unique_constraints", [])
113
+
114
+ # Get the columns, indexes, and unique constraints from the table schema
115
+ for col_name, col_config in table_schema.items():
116
+ column_args = [col_name, col_config["type"]()]
117
+ column_kwargs = {}
118
+ if col_config.get("primary_key", False):
119
+ column_kwargs["primary_key"] = True
120
+ if "nullable" in col_config:
121
+ column_kwargs["nullable"] = col_config["nullable"]
122
+ if col_config.get("index", False):
123
+ indexes.append(col_name)
124
+ if col_config.get("unique", False):
125
+ column_kwargs["unique"] = True
126
+ unique_constraints.append(col_name)
127
+ columns.append(Column(*column_args, **column_kwargs)) # type: ignore
128
+
129
+ # Create the table object
130
+ table_metadata = MetaData(schema=db_schema)
131
+ table = Table(table_name, table_metadata, *columns, schema=db_schema)
132
+
133
+ # Add multi-column unique constraints with table-specific names
134
+ for constraint in schema_unique_constraints:
135
+ constraint_name = f"{table_name}_{constraint['name']}"
136
+ constraint_columns = constraint["columns"]
137
+ table.append_constraint(UniqueConstraint(*constraint_columns, name=constraint_name))
138
+
139
+ # Add indexes to the table definition
140
+ for idx_col in indexes:
141
+ idx_name = f"idx_{table_name}_{idx_col}"
142
+ table.append_constraint(Index(idx_name, idx_col))
143
+
144
+ async with self.async_session_factory() as sess, sess.begin():
145
+ await create_schema(session=sess, db_schema=db_schema)
146
+
147
+ # Create table
148
+ async with self.db_engine.begin() as conn:
149
+ await conn.run_sync(table.create, checkfirst=True)
150
+
151
+ # Create indexes
152
+ for idx in table.indexes:
153
+ try:
154
+ # Check if index already exists
155
+ async with self.async_session_factory() as sess:
156
+ exists_query = text(
157
+ "SELECT 1 FROM pg_indexes WHERE schemaname = :schema AND indexname = :index_name"
158
+ )
159
+ result = await sess.execute(exists_query, {"schema": db_schema, "index_name": idx.name})
160
+ exists = result.scalar() is not None
161
+ if exists:
162
+ log_debug(f"Index {idx.name} already exists in {db_schema}.{table_name}, skipping creation")
163
+ continue
164
+
165
+ async with self.db_engine.begin() as conn:
166
+ await conn.run_sync(idx.create)
167
+ log_debug(f"Created index: {idx.name} for table {db_schema}.{table_name}")
168
+
169
+ except Exception as e:
170
+ log_error(f"Error creating index {idx.name}: {e}")
171
+
172
+ log_info(f"Successfully created table {table_name} in schema {db_schema}")
173
+ return table
174
+
175
+ except Exception as e:
176
+ log_error(f"Could not create table {db_schema}.{table_name}: {e}")
177
+ raise
178
+
179
+ async def _get_table(self, table_type: str) -> Table:
180
+ if table_type == "sessions":
181
+ if not hasattr(self, "session_table"):
182
+ self.session_table = await self._get_or_create_table(
183
+ table_name=self.session_table_name, table_type="sessions", db_schema=self.db_schema
184
+ )
185
+ return self.session_table
186
+
187
+ if table_type == "memories":
188
+ if not hasattr(self, "memory_table"):
189
+ self.memory_table = await self._get_or_create_table(
190
+ table_name=self.memory_table_name, table_type="memories", db_schema=self.db_schema
191
+ )
192
+ return self.memory_table
193
+
194
+ if table_type == "metrics":
195
+ if not hasattr(self, "metrics_table"):
196
+ self.metrics_table = await self._get_or_create_table(
197
+ table_name=self.metrics_table_name, table_type="metrics", db_schema=self.db_schema
198
+ )
199
+ return self.metrics_table
200
+
201
+ if table_type == "evals":
202
+ if not hasattr(self, "eval_table"):
203
+ self.eval_table = await self._get_or_create_table(
204
+ table_name=self.eval_table_name, table_type="evals", db_schema=self.db_schema
205
+ )
206
+ return self.eval_table
207
+
208
+ if table_type == "knowledge":
209
+ if not hasattr(self, "knowledge_table"):
210
+ self.knowledge_table = await self._get_or_create_table(
211
+ table_name=self.knowledge_table_name, table_type="knowledge", db_schema=self.db_schema
212
+ )
213
+ return self.knowledge_table
214
+
215
+ raise ValueError(f"Unknown table type: {table_type}")
216
+
217
+ async def _get_or_create_table(self, table_name: str, table_type: str, db_schema: str) -> Table:
218
+ """
219
+ Check if the table exists and is valid, else create it.
220
+
221
+ Args:
222
+ table_name (str): Name of the table to get or create
223
+ table_type (str): Type of table (used to get schema definition)
224
+ db_schema (str): Database schema name
225
+
226
+ Returns:
227
+ Table: SQLAlchemy Table object representing the schema.
228
+ """
229
+
230
+ async with self.async_session_factory() as sess, sess.begin():
231
+ table_is_available = await is_table_available(session=sess, table_name=table_name, db_schema=db_schema)
232
+
233
+ if not table_is_available:
234
+ return await self._create_table(table_name=table_name, table_type=table_type, db_schema=db_schema)
235
+
236
+ if not await is_valid_table(
237
+ db_engine=self.db_engine,
238
+ table_name=table_name,
239
+ table_type=table_type,
240
+ db_schema=db_schema,
241
+ ):
242
+ raise ValueError(f"Table {db_schema}.{table_name} has an invalid schema")
243
+
244
+ try:
245
+ async with self.db_engine.connect() as conn:
246
+
247
+ def create_table(connection):
248
+ return Table(table_name, self.metadata, schema=db_schema, autoload_with=connection)
249
+
250
+ table = await conn.run_sync(create_table)
251
+ return table
252
+
253
+ except Exception as e:
254
+ log_error(f"Error loading existing table {db_schema}.{table_name}: {e}")
255
+ raise
256
+
257
+ # -- Session methods --
258
+ async def delete_session(self, session_id: str) -> bool:
259
+ """
260
+ Delete a session from the database.
261
+
262
+ Args:
263
+ session_id (str): ID of the session to delete
264
+
265
+ Returns:
266
+ bool: True if the session was deleted, False otherwise.
267
+
268
+ Raises:
269
+ Exception: If an error occurs during deletion.
270
+ """
271
+ try:
272
+ table = await self._get_table(table_type="sessions")
273
+
274
+ async with self.async_session_factory() as sess, sess.begin():
275
+ delete_stmt = table.delete().where(table.c.session_id == session_id)
276
+ result = await sess.execute(delete_stmt)
277
+
278
+ if result.rowcount == 0:
279
+ log_debug(f"No session found to delete with session_id: {session_id} in table {table.name}")
280
+ return False
281
+
282
+ else:
283
+ log_debug(f"Successfully deleted session with session_id: {session_id} in table {table.name}")
284
+ return True
285
+
286
+ except Exception as e:
287
+ log_error(f"Error deleting session: {e}")
288
+ return False
289
+
290
+ async def delete_sessions(self, session_ids: List[str]) -> None:
291
+ """Delete all given sessions from the database.
292
+ Can handle multiple session types in the same run.
293
+
294
+ Args:
295
+ session_ids (List[str]): The IDs of the sessions to delete.
296
+
297
+ Raises:
298
+ Exception: If an error occurs during deletion.
299
+ """
300
+ try:
301
+ table = await self._get_table(table_type="sessions")
302
+
303
+ async with self.async_session_factory() as sess, sess.begin():
304
+ delete_stmt = table.delete().where(table.c.session_id.in_(session_ids))
305
+ result = await sess.execute(delete_stmt)
306
+
307
+ log_debug(f"Successfully deleted {result.rowcount} sessions")
308
+
309
+ except Exception as e:
310
+ log_error(f"Error deleting sessions: {e}")
311
+
312
+ async def get_session(
313
+ self,
314
+ session_id: str,
315
+ session_type: SessionType,
316
+ user_id: Optional[str] = None,
317
+ deserialize: Optional[bool] = True,
318
+ ) -> Optional[Union[Session, Dict[str, Any]]]:
319
+ """
320
+ Read a session from the database.
321
+
322
+ Args:
323
+ session_id (str): ID of the session to read.
324
+ user_id (Optional[str]): User ID to filter by. Defaults to None.
325
+ session_type (Optional[SessionType]): Type of session to read. Defaults to None.
326
+ deserialize (Optional[bool]): Whether to serialize the session. Defaults to True.
327
+
328
+ Returns:
329
+ Union[Session, Dict[str, Any], None]:
330
+ - When deserialize=True: Session object
331
+ - When deserialize=False: Session dictionary
332
+
333
+ Raises:
334
+ Exception: If an error occurs during retrieval.
335
+ """
336
+ try:
337
+ table = await self._get_table(table_type="sessions")
338
+
339
+ async with self.async_session_factory() as sess:
340
+ stmt = select(table).where(table.c.session_id == session_id)
341
+
342
+ if user_id is not None:
343
+ stmt = stmt.where(table.c.user_id == user_id)
344
+ if session_type is not None:
345
+ session_type_value = session_type.value if isinstance(session_type, SessionType) else session_type
346
+ stmt = stmt.where(table.c.session_type == session_type_value)
347
+ result = await sess.execute(stmt)
348
+ row = result.fetchone()
349
+ if row is None:
350
+ return None
351
+
352
+ session = dict(row._mapping)
353
+
354
+ if not deserialize:
355
+ return session
356
+
357
+ if session_type == SessionType.AGENT:
358
+ return AgentSession.from_dict(session)
359
+ elif session_type == SessionType.TEAM:
360
+ return TeamSession.from_dict(session)
361
+ elif session_type == SessionType.WORKFLOW:
362
+ return WorkflowSession.from_dict(session)
363
+ else:
364
+ raise ValueError(f"Invalid session type: {session_type}")
365
+
366
+ except Exception as e:
367
+ log_error(f"Exception reading from session table: {e}")
368
+ return None
369
+
370
+ async def get_sessions(
371
+ self,
372
+ session_type: Optional[SessionType] = None,
373
+ user_id: Optional[str] = None,
374
+ component_id: Optional[str] = None,
375
+ session_name: Optional[str] = None,
376
+ start_timestamp: Optional[int] = None,
377
+ end_timestamp: Optional[int] = None,
378
+ limit: Optional[int] = None,
379
+ page: Optional[int] = None,
380
+ sort_by: Optional[str] = None,
381
+ sort_order: Optional[str] = None,
382
+ deserialize: Optional[bool] = True,
383
+ ) -> Union[List[Session], Tuple[List[Dict[str, Any]], int]]:
384
+ """
385
+ Get all sessions in the given table. Can filter by user_id and entity_id.
386
+
387
+ Args:
388
+ user_id (Optional[str]): The ID of the user to filter by.
389
+ component_id (Optional[str]): The ID of the agent / workflow to filter by.
390
+ start_timestamp (Optional[int]): The start timestamp to filter by.
391
+ end_timestamp (Optional[int]): The end timestamp to filter by.
392
+ session_name (Optional[str]): The name of the session to filter by.
393
+ limit (Optional[int]): The maximum number of sessions to return. Defaults to None.
394
+ page (Optional[int]): The page number to return. Defaults to None.
395
+ sort_by (Optional[str]): The field to sort by. Defaults to None.
396
+ sort_order (Optional[str]): The sort order. Defaults to None.
397
+ deserialize (Optional[bool]): Whether to serialize the sessions. Defaults to True.
398
+
399
+ Returns:
400
+ Union[List[Session], Tuple[List[Dict], int]]:
401
+ - When deserialize=True: List of Session objects
402
+ - When deserialize=False: Tuple of (session dictionaries, total count)
403
+
404
+ Raises:
405
+ Exception: If an error occurs during retrieval.
406
+ """
407
+ try:
408
+ table = await self._get_table(table_type="sessions")
409
+
410
+ async with self.async_session_factory() as sess, sess.begin():
411
+ stmt = select(table)
412
+
413
+ # Filtering
414
+ if user_id is not None:
415
+ stmt = stmt.where(table.c.user_id == user_id)
416
+ if component_id is not None:
417
+ if session_type == SessionType.AGENT:
418
+ stmt = stmt.where(table.c.agent_id == component_id)
419
+ elif session_type == SessionType.TEAM:
420
+ stmt = stmt.where(table.c.team_id == component_id)
421
+ elif session_type == SessionType.WORKFLOW:
422
+ stmt = stmt.where(table.c.workflow_id == component_id)
423
+ if start_timestamp is not None:
424
+ stmt = stmt.where(table.c.created_at >= start_timestamp)
425
+ if end_timestamp is not None:
426
+ stmt = stmt.where(table.c.created_at <= end_timestamp)
427
+ if session_name is not None:
428
+ stmt = stmt.where(
429
+ func.coalesce(func.json_extract_path_text(table.c.session_data, "session_name"), "").ilike(
430
+ f"%{session_name}%"
431
+ )
432
+ )
433
+ if session_type is not None:
434
+ session_type_value = session_type.value if isinstance(session_type, SessionType) else session_type
435
+ stmt = stmt.where(table.c.session_type == session_type_value)
436
+
437
+ count_stmt = select(func.count()).select_from(stmt.alias())
438
+ total_count = await sess.scalar(count_stmt) or 0
439
+
440
+ # Sorting
441
+ stmt = apply_sorting(stmt, table, sort_by, sort_order)
442
+
443
+ # Paginating
444
+ if limit is not None:
445
+ stmt = stmt.limit(limit)
446
+ if page is not None:
447
+ stmt = stmt.offset((page - 1) * limit)
448
+
449
+ result = await sess.execute(stmt)
450
+ records = result.fetchall()
451
+ if records is None:
452
+ return [], 0
453
+
454
+ session = [dict(record._mapping) for record in records]
455
+ if not deserialize:
456
+ return session, total_count
457
+
458
+ if session_type == SessionType.AGENT:
459
+ return [AgentSession.from_dict(record) for record in session] # type: ignore
460
+ elif session_type == SessionType.TEAM:
461
+ return [TeamSession.from_dict(record) for record in session] # type: ignore
462
+ elif session_type == SessionType.WORKFLOW:
463
+ return [WorkflowSession.from_dict(record) for record in session] # type: ignore
464
+ else:
465
+ raise ValueError(f"Invalid session type: {session_type}")
466
+
467
+ except Exception as e:
468
+ log_error(f"Exception reading from session table: {e}")
469
+ return [] if deserialize else ([], 0)
470
+
471
+ async def rename_session(
472
+ self, session_id: str, session_type: SessionType, session_name: str, deserialize: Optional[bool] = True
473
+ ) -> Optional[Union[Session, Dict[str, Any]]]:
474
+ """
475
+ Rename a session in the database.
476
+
477
+ Args:
478
+ session_id (str): The ID of the session to rename.
479
+ session_type (SessionType): The type of session to rename.
480
+ session_name (str): The new name for the session.
481
+ deserialize (Optional[bool]): Whether to serialize the session. Defaults to True.
482
+
483
+ Returns:
484
+ Optional[Union[Session, Dict[str, Any]]]:
485
+ - When deserialize=True: Session object
486
+ - When deserialize=False: Session dictionary
487
+
488
+ Raises:
489
+ Exception: If an error occurs during renaming.
490
+ """
491
+ try:
492
+ table = await self._get_table(table_type="sessions")
493
+
494
+ async with self.async_session_factory() as sess, sess.begin():
495
+ stmt = (
496
+ update(table)
497
+ .where(table.c.session_id == session_id)
498
+ .where(table.c.session_type == session_type.value)
499
+ .values(
500
+ session_data=func.cast(
501
+ func.jsonb_set(
502
+ func.cast(table.c.session_data, postgresql.JSONB),
503
+ text("'{session_name}'"),
504
+ func.to_jsonb(session_name),
505
+ ),
506
+ postgresql.JSON,
507
+ )
508
+ )
509
+ .returning(*table.c)
510
+ )
511
+ result = await sess.execute(stmt)
512
+ row = result.fetchone()
513
+ if not row:
514
+ return None
515
+
516
+ log_debug(f"Renamed session with id '{session_id}' to '{session_name}'")
517
+
518
+ session = dict(row._mapping)
519
+ if not deserialize:
520
+ return session
521
+
522
+ # Return the appropriate session type
523
+ if session_type == SessionType.AGENT:
524
+ return AgentSession.from_dict(session)
525
+ elif session_type == SessionType.TEAM:
526
+ return TeamSession.from_dict(session)
527
+ elif session_type == SessionType.WORKFLOW:
528
+ return WorkflowSession.from_dict(session)
529
+ else:
530
+ raise ValueError(f"Invalid session type: {session_type}")
531
+
532
+ except Exception as e:
533
+ log_error(f"Exception renaming session: {e}")
534
+ return None
535
+
536
+ async def upsert_session(
537
+ self, session: Session, deserialize: Optional[bool] = True
538
+ ) -> Optional[Union[Session, Dict[str, Any]]]:
539
+ """
540
+ Insert or update a session in the database.
541
+
542
+ Args:
543
+ session (Session): The session data to upsert.
544
+ deserialize (Optional[bool]): Whether to deserialize the session. Defaults to True.
545
+
546
+ Returns:
547
+ Optional[Union[Session, Dict[str, Any]]]:
548
+ - When deserialize=True: Session object
549
+ - When deserialize=False: Session dictionary
550
+
551
+ Raises:
552
+ Exception: If an error occurs during upsert.
553
+ """
554
+ try:
555
+ table = await self._get_table(table_type="sessions")
556
+ session_dict = session.to_dict()
557
+
558
+ if isinstance(session, AgentSession):
559
+ async with self.async_session_factory() as sess, sess.begin():
560
+ stmt = postgresql.insert(table).values(
561
+ session_id=session_dict.get("session_id"),
562
+ session_type=SessionType.AGENT.value,
563
+ agent_id=session_dict.get("agent_id"),
564
+ user_id=session_dict.get("user_id"),
565
+ runs=session_dict.get("runs"),
566
+ agent_data=session_dict.get("agent_data"),
567
+ session_data=session_dict.get("session_data"),
568
+ summary=session_dict.get("summary"),
569
+ metadata=session_dict.get("metadata"),
570
+ created_at=session_dict.get("created_at"),
571
+ updated_at=session_dict.get("created_at"),
572
+ )
573
+ stmt = stmt.on_conflict_do_update( # type: ignore
574
+ index_elements=["session_id"],
575
+ set_=dict(
576
+ agent_id=session_dict.get("agent_id"),
577
+ user_id=session_dict.get("user_id"),
578
+ agent_data=session_dict.get("agent_data"),
579
+ session_data=session_dict.get("session_data"),
580
+ summary=session_dict.get("summary"),
581
+ metadata=session_dict.get("metadata"),
582
+ runs=session_dict.get("runs"),
583
+ updated_at=int(time.time()),
584
+ ),
585
+ ).returning(table)
586
+ result = await sess.execute(stmt)
587
+ row = result.fetchone()
588
+ if row is None:
589
+ return None
590
+ session_dict = dict(row._mapping)
591
+
592
+ log_debug(f"Upserted agent session with id '{session_dict.get('session_id')}'")
593
+
594
+ if not deserialize:
595
+ return session_dict
596
+ return AgentSession.from_dict(session_dict)
597
+
598
+ elif isinstance(session, TeamSession):
599
+ async with self.async_session_factory() as sess, sess.begin():
600
+ stmt = postgresql.insert(table).values(
601
+ session_id=session_dict.get("session_id"),
602
+ session_type=SessionType.TEAM.value,
603
+ team_id=session_dict.get("team_id"),
604
+ user_id=session_dict.get("user_id"),
605
+ runs=session_dict.get("runs"),
606
+ team_data=session_dict.get("team_data"),
607
+ session_data=session_dict.get("session_data"),
608
+ summary=session_dict.get("summary"),
609
+ metadata=session_dict.get("metadata"),
610
+ created_at=session_dict.get("created_at"),
611
+ updated_at=session_dict.get("created_at"),
612
+ )
613
+ stmt = stmt.on_conflict_do_update( # type: ignore
614
+ index_elements=["session_id"],
615
+ set_=dict(
616
+ team_id=session_dict.get("team_id"),
617
+ user_id=session_dict.get("user_id"),
618
+ team_data=session_dict.get("team_data"),
619
+ session_data=session_dict.get("session_data"),
620
+ summary=session_dict.get("summary"),
621
+ metadata=session_dict.get("metadata"),
622
+ runs=session_dict.get("runs"),
623
+ updated_at=int(time.time()),
624
+ ),
625
+ ).returning(table)
626
+ result = await sess.execute(stmt)
627
+ row = result.fetchone()
628
+ if row is None:
629
+ return None
630
+ session_dict = dict(row._mapping)
631
+
632
+ log_debug(f"Upserted team session with id '{session_dict.get('session_id')}'")
633
+
634
+ if not deserialize:
635
+ return session_dict
636
+ return TeamSession.from_dict(session_dict)
637
+
638
+ elif isinstance(session, WorkflowSession):
639
+ async with self.async_session_factory() as sess, sess.begin():
640
+ stmt = postgresql.insert(table).values(
641
+ session_id=session_dict.get("session_id"),
642
+ session_type=SessionType.WORKFLOW.value,
643
+ workflow_id=session_dict.get("workflow_id"),
644
+ user_id=session_dict.get("user_id"),
645
+ runs=session_dict.get("runs"),
646
+ workflow_data=session_dict.get("workflow_data"),
647
+ session_data=session_dict.get("session_data"),
648
+ summary=session_dict.get("summary"),
649
+ metadata=session_dict.get("metadata"),
650
+ created_at=session_dict.get("created_at"),
651
+ updated_at=session_dict.get("created_at"),
652
+ )
653
+ stmt = stmt.on_conflict_do_update( # type: ignore
654
+ index_elements=["session_id"],
655
+ set_=dict(
656
+ workflow_id=session_dict.get("workflow_id"),
657
+ user_id=session_dict.get("user_id"),
658
+ workflow_data=session_dict.get("workflow_data"),
659
+ session_data=session_dict.get("session_data"),
660
+ summary=session_dict.get("summary"),
661
+ metadata=session_dict.get("metadata"),
662
+ runs=session_dict.get("runs"),
663
+ updated_at=int(time.time()),
664
+ ),
665
+ ).returning(table)
666
+ result = await sess.execute(stmt)
667
+ row = result.fetchone()
668
+ if row is None:
669
+ return None
670
+ session_dict = dict(row._mapping)
671
+
672
+ log_debug(f"Upserted workflow session with id '{session_dict.get('session_id')}'")
673
+
674
+ if not deserialize:
675
+ return session_dict
676
+ return WorkflowSession.from_dict(session_dict)
677
+
678
+ else:
679
+ raise ValueError(f"Invalid session type: {session.session_type}")
680
+
681
+ except Exception as e:
682
+ log_error(f"Exception upserting into sessions table: {e}")
683
+ return None
684
+
685
+ # -- Memory methods --
686
+ async def delete_user_memory(self, memory_id: str):
687
+ """Delete a user memory from the database.
688
+
689
+ Returns:
690
+ bool: True if deletion was successful, False otherwise.
691
+
692
+ Raises:
693
+ Exception: If an error occurs during deletion.
694
+ """
695
+ try:
696
+ table = await self._get_table(table_type="memories")
697
+
698
+ async with self.async_session_factory() as sess, sess.begin():
699
+ delete_stmt = table.delete().where(table.c.memory_id == memory_id)
700
+ result = await sess.execute(delete_stmt)
701
+
702
+ success = result.rowcount > 0
703
+ if success:
704
+ log_debug(f"Successfully deleted user memory id: {memory_id}")
705
+ else:
706
+ log_debug(f"No user memory found with id: {memory_id}")
707
+
708
+ except Exception as e:
709
+ log_error(f"Error deleting user memory: {e}")
710
+
711
+ async def delete_user_memories(self, memory_ids: List[str]) -> None:
712
+ """Delete user memories from the database.
713
+
714
+ Args:
715
+ memory_ids (List[str]): The IDs of the memories to delete.
716
+
717
+ Raises:
718
+ Exception: If an error occurs during deletion.
719
+ """
720
+ try:
721
+ table = await self._get_table(table_type="memories")
722
+
723
+ async with self.async_session_factory() as sess, sess.begin():
724
+ delete_stmt = table.delete().where(table.c.memory_id.in_(memory_ids))
725
+ result = await sess.execute(delete_stmt)
726
+
727
+ if result.rowcount == 0:
728
+ log_debug(f"No user memories found with ids: {memory_ids}")
729
+ else:
730
+ log_debug(f"Successfully deleted {result.rowcount} user memories")
731
+
732
+ except Exception as e:
733
+ log_error(f"Error deleting user memories: {e}")
734
+
735
+ async def get_all_memory_topics(self) -> List[str]:
736
+ """Get all memory topics from the database.
737
+
738
+ Returns:
739
+ List[str]: List of memory topics.
740
+ """
741
+ try:
742
+ table = await self._get_table(table_type="memories")
743
+
744
+ async with self.async_session_factory() as sess, sess.begin():
745
+ stmt = select(func.json_array_elements_text(table.c.topics))
746
+ result = await sess.execute(stmt)
747
+ records = result.fetchall()
748
+
749
+ return list(set([record[0] for record in records]))
750
+
751
+ except Exception as e:
752
+ log_error(f"Exception reading from memory table: {e}")
753
+ return []
754
+
755
+ async def get_user_memory(
756
+ self, memory_id: str, deserialize: Optional[bool] = True
757
+ ) -> Optional[Union[UserMemory, Dict[str, Any]]]:
758
+ """Get a memory from the database.
759
+
760
+ Args:
761
+ memory_id (str): The ID of the memory to get.
762
+ deserialize (Optional[bool]): Whether to serialize the memory. Defaults to True.
763
+
764
+ Returns:
765
+ Union[UserMemory, Dict[str, Any], None]:
766
+ - When deserialize=True: UserMemory object
767
+ - When deserialize=False: UserMemory dictionary
768
+
769
+ Raises:
770
+ Exception: If an error occurs during retrieval.
771
+ """
772
+ try:
773
+ table = await self._get_table(table_type="memories")
774
+
775
+ async with self.async_session_factory() as sess, sess.begin():
776
+ stmt = select(table).where(table.c.memory_id == memory_id)
777
+
778
+ result = await sess.execute(stmt)
779
+ row = result.fetchone()
780
+ if not row:
781
+ return None
782
+
783
+ memory_raw = dict(row._mapping)
784
+ if not deserialize:
785
+ return memory_raw
786
+
787
+ return UserMemory.from_dict(memory_raw)
788
+
789
+ except Exception as e:
790
+ log_error(f"Exception reading from memory table: {e}")
791
+ return None
792
+
793
+ async def get_user_memories(
794
+ self,
795
+ user_id: Optional[str] = None,
796
+ agent_id: Optional[str] = None,
797
+ team_id: Optional[str] = None,
798
+ topics: Optional[List[str]] = None,
799
+ search_content: Optional[str] = None,
800
+ limit: Optional[int] = None,
801
+ page: Optional[int] = None,
802
+ sort_by: Optional[str] = None,
803
+ sort_order: Optional[str] = None,
804
+ deserialize: Optional[bool] = True,
805
+ ) -> Union[List[UserMemory], Tuple[List[Dict[str, Any]], int]]:
806
+ """Get all memories from the database as UserMemory objects.
807
+
808
+ Args:
809
+ user_id (Optional[str]): The ID of the user to filter by.
810
+ agent_id (Optional[str]): The ID of the agent to filter by.
811
+ team_id (Optional[str]): The ID of the team to filter by.
812
+ topics (Optional[List[str]]): The topics to filter by.
813
+ search_content (Optional[str]): The content to search for.
814
+ limit (Optional[int]): The maximum number of memories to return.
815
+ page (Optional[int]): The page number.
816
+ sort_by (Optional[str]): The column to sort by.
817
+ sort_order (Optional[str]): The order to sort by.
818
+ deserialize (Optional[bool]): Whether to serialize the memories. Defaults to True.
819
+
820
+ Returns:
821
+ Union[List[UserMemory], Tuple[List[Dict[str, Any]], int]]:
822
+ - When deserialize=True: List of UserMemory objects
823
+ - When deserialize=False: Tuple of (memory dictionaries, total count)
824
+
825
+ Raises:
826
+ Exception: If an error occurs during retrieval.
827
+ """
828
+ try:
829
+ table = await self._get_table(table_type="memories")
830
+
831
+ async with self.async_session_factory() as sess, sess.begin():
832
+ stmt = select(table)
833
+ # Filtering
834
+ if user_id is not None:
835
+ stmt = stmt.where(table.c.user_id == user_id)
836
+ if agent_id is not None:
837
+ stmt = stmt.where(table.c.agent_id == agent_id)
838
+ if team_id is not None:
839
+ stmt = stmt.where(table.c.team_id == team_id)
840
+ if topics is not None:
841
+ for topic in topics:
842
+ stmt = stmt.where(func.cast(table.c.topics, String).like(f'%"{topic}"%'))
843
+ if search_content is not None:
844
+ stmt = stmt.where(func.cast(table.c.memory, postgresql.TEXT).ilike(f"%{search_content}%"))
845
+
846
+ # Get total count after applying filtering
847
+ count_stmt = select(func.count()).select_from(stmt.alias())
848
+ total_count = await sess.scalar(count_stmt) or 0
849
+
850
+ # Sorting
851
+ stmt = apply_sorting(stmt, table, sort_by, sort_order)
852
+
853
+ # Paginating
854
+ if limit is not None:
855
+ stmt = stmt.limit(limit)
856
+ if page is not None:
857
+ stmt = stmt.offset((page - 1) * limit)
858
+
859
+ result = await sess.execute(stmt)
860
+ records = result.fetchall()
861
+ if not records:
862
+ return [] if deserialize else ([], 0)
863
+
864
+ memories_raw = [dict(record._mapping) for record in records]
865
+ if not deserialize:
866
+ return memories_raw, total_count
867
+
868
+ return [UserMemory.from_dict(record) for record in memories_raw]
869
+
870
+ except Exception as e:
871
+ log_error(f"Exception reading from memory table: {e}")
872
+ return [] if deserialize else ([], 0)
873
+
874
+ async def clear_memories(self) -> None:
875
+ """Delete all memories from the database.
876
+
877
+ Raises:
878
+ Exception: If an error occurs during deletion.
879
+ """
880
+ try:
881
+ table = await self._get_table(table_type="memories")
882
+
883
+ async with self.async_session_factory() as sess, sess.begin():
884
+ await sess.execute(table.delete())
885
+
886
+ except Exception as e:
887
+ log_warning(f"Exception deleting all memories: {e}")
888
+
889
+ async def get_user_memory_stats(
890
+ self, limit: Optional[int] = None, page: Optional[int] = None
891
+ ) -> Tuple[List[Dict[str, Any]], int]:
892
+ """Get user memories stats.
893
+
894
+ Args:
895
+ limit (Optional[int]): The maximum number of user stats to return.
896
+ page (Optional[int]): The page number.
897
+
898
+ Returns:
899
+ Tuple[List[Dict[str, Any]], int]: A list of dictionaries containing user stats and total count.
900
+
901
+ Example:
902
+ (
903
+ [
904
+ {
905
+ "user_id": "123",
906
+ "total_memories": 10,
907
+ "last_memory_updated_at": 1714560000,
908
+ },
909
+ ],
910
+ total_count: 1,
911
+ )
912
+ """
913
+ try:
914
+ table = await self._get_table(table_type="memories")
915
+
916
+ async with self.async_session_factory() as sess, sess.begin():
917
+ stmt = (
918
+ select(
919
+ table.c.user_id,
920
+ func.count(table.c.memory_id).label("total_memories"),
921
+ func.max(table.c.updated_at).label("last_memory_updated_at"),
922
+ )
923
+ .where(table.c.user_id.is_not(None))
924
+ .group_by(table.c.user_id)
925
+ .order_by(func.max(table.c.updated_at).desc())
926
+ )
927
+
928
+ count_stmt = select(func.count()).select_from(stmt.alias())
929
+ total_count = await sess.scalar(count_stmt) or 0
930
+
931
+ # Pagination
932
+ if limit is not None:
933
+ stmt = stmt.limit(limit)
934
+ if page is not None:
935
+ stmt = stmt.offset((page - 1) * limit)
936
+
937
+ result = await sess.execute(stmt)
938
+ records = result.fetchall()
939
+ if not records:
940
+ return [], 0
941
+
942
+ return [
943
+ {
944
+ "user_id": record.user_id, # type: ignore
945
+ "total_memories": record.total_memories,
946
+ "last_memory_updated_at": record.last_memory_updated_at,
947
+ }
948
+ for record in records
949
+ ], total_count
950
+
951
+ except Exception as e:
952
+ log_error(f"Exception getting user memory stats: {e}")
953
+ return [], 0
954
+
955
+ async def upsert_user_memory(
956
+ self, memory: UserMemory, deserialize: Optional[bool] = True
957
+ ) -> Optional[Union[UserMemory, Dict[str, Any]]]:
958
+ """Upsert a user memory in the database.
959
+
960
+ Args:
961
+ memory (UserMemory): The user memory to upsert.
962
+ deserialize (Optional[bool]): Whether to serialize the memory. Defaults to True.
963
+
964
+ Returns:
965
+ Optional[Union[UserMemory, Dict[str, Any]]]:
966
+ - When deserialize=True: UserMemory object
967
+ - When deserialize=False: UserMemory dictionary
968
+
969
+ Raises:
970
+ Exception: If an error occurs during upsert.
971
+ """
972
+ try:
973
+ table = await self._get_table(table_type="memories")
974
+
975
+ async with self.async_session_factory() as sess, sess.begin():
976
+ if memory.memory_id is None:
977
+ memory.memory_id = str(uuid4())
978
+
979
+ stmt = postgresql.insert(table).values(
980
+ memory_id=memory.memory_id,
981
+ memory=memory.memory,
982
+ input=memory.input,
983
+ user_id=memory.user_id,
984
+ agent_id=memory.agent_id,
985
+ team_id=memory.team_id,
986
+ topics=memory.topics,
987
+ updated_at=int(time.time()),
988
+ )
989
+ stmt = stmt.on_conflict_do_update( # type: ignore
990
+ index_elements=["memory_id"],
991
+ set_=dict(
992
+ memory=memory.memory,
993
+ topics=memory.topics,
994
+ input=memory.input,
995
+ agent_id=memory.agent_id,
996
+ team_id=memory.team_id,
997
+ updated_at=int(time.time()),
998
+ ),
999
+ ).returning(table)
1000
+
1001
+ result = await sess.execute(stmt)
1002
+ row = result.fetchone()
1003
+ if row is None:
1004
+ return None
1005
+
1006
+ memory_raw = dict(row._mapping)
1007
+
1008
+ log_debug(f"Upserted user memory with id '{memory.memory_id}'")
1009
+
1010
+ if not memory_raw or not deserialize:
1011
+ return memory_raw
1012
+
1013
+ return UserMemory.from_dict(memory_raw)
1014
+
1015
+ except Exception as e:
1016
+ log_error(f"Exception upserting user memory: {e}")
1017
+ return None
1018
+
1019
+ # -- Metrics methods --
1020
+ async def _get_all_sessions_for_metrics_calculation(
1021
+ self, start_timestamp: Optional[int] = None, end_timestamp: Optional[int] = None
1022
+ ) -> List[Dict[str, Any]]:
1023
+ """
1024
+ Get all sessions of all types (agent, team, workflow) as raw dictionaries.
1025
+
1026
+ Args:
1027
+ start_timestamp (Optional[int]): The start timestamp to filter by. Defaults to None.
1028
+ end_timestamp (Optional[int]): The end timestamp to filter by. Defaults to None.
1029
+
1030
+ Returns:
1031
+ List[Dict[str, Any]]: List of session dictionaries with session_type field.
1032
+
1033
+ Raises:
1034
+ Exception: If an error occurs during retrieval.
1035
+ """
1036
+ try:
1037
+ table = await self._get_table(table_type="sessions")
1038
+
1039
+ stmt = select(
1040
+ table.c.user_id,
1041
+ table.c.session_data,
1042
+ table.c.runs,
1043
+ table.c.created_at,
1044
+ table.c.session_type,
1045
+ )
1046
+
1047
+ if start_timestamp is not None:
1048
+ stmt = stmt.where(table.c.created_at >= start_timestamp)
1049
+ if end_timestamp is not None:
1050
+ stmt = stmt.where(table.c.created_at <= end_timestamp)
1051
+
1052
+ async with self.async_session_factory() as sess:
1053
+ result = await sess.execute(stmt)
1054
+ records = result.fetchall()
1055
+
1056
+ return [dict(record._mapping) for record in records]
1057
+
1058
+ except Exception as e:
1059
+ log_error(f"Exception reading from sessions table: {e}")
1060
+ return []
1061
+
1062
+ async def _get_metrics_calculation_starting_date(self, table: Table) -> Optional[date]:
1063
+ """Get the first date for which metrics calculation is needed:
1064
+
1065
+ 1. If there are metrics records, return the date of the first day without a complete metrics record.
1066
+ 2. If there are no metrics records, return the date of the first recorded session.
1067
+ 3. If there are no metrics records and no sessions records, return None.
1068
+
1069
+ Args:
1070
+ table (Table): The table to get the starting date for.
1071
+
1072
+ Returns:
1073
+ Optional[date]: The starting date for which metrics calculation is needed.
1074
+ """
1075
+ async with self.async_session_factory() as sess:
1076
+ stmt = select(table).order_by(table.c.date.desc()).limit(1)
1077
+ result = await sess.execute(stmt)
1078
+ row = result.fetchone()
1079
+
1080
+ # 1. Return the date of the first day without a complete metrics record.
1081
+ if row is not None:
1082
+ if row.completed:
1083
+ return row._mapping["date"] + timedelta(days=1)
1084
+ else:
1085
+ return row._mapping["date"]
1086
+
1087
+ # 2. No metrics records. Return the date of the first recorded session.
1088
+ first_session, _ = await self.get_sessions(sort_by="created_at", sort_order="asc", limit=1, deserialize=False)
1089
+
1090
+ first_session_date = first_session[0]["created_at"] if first_session else None # type: ignore[index]
1091
+
1092
+ # 3. No metrics records and no sessions records. Return None.
1093
+ if first_session_date is None:
1094
+ return None
1095
+
1096
+ return datetime.fromtimestamp(first_session_date, tz=timezone.utc).date()
1097
+
1098
+ async def calculate_metrics(self) -> Optional[list[dict]]:
1099
+ """Calculate metrics for all dates without complete metrics.
1100
+
1101
+ Returns:
1102
+ Optional[list[dict]]: The calculated metrics.
1103
+
1104
+ Raises:
1105
+ Exception: If an error occurs during metrics calculation.
1106
+ """
1107
+ try:
1108
+ table = await self._get_table(table_type="metrics")
1109
+
1110
+ starting_date = await self._get_metrics_calculation_starting_date(table)
1111
+
1112
+ if starting_date is None:
1113
+ log_info("No session data found. Won't calculate metrics.")
1114
+ return None
1115
+
1116
+ dates_to_process = get_dates_to_calculate_metrics_for(starting_date)
1117
+ if not dates_to_process:
1118
+ log_info("Metrics already calculated for all relevant dates.")
1119
+ return None
1120
+
1121
+ start_timestamp = int(
1122
+ datetime.combine(dates_to_process[0], datetime.min.time()).replace(tzinfo=timezone.utc).timestamp()
1123
+ )
1124
+ end_timestamp = int(
1125
+ datetime.combine(dates_to_process[-1] + timedelta(days=1), datetime.min.time())
1126
+ .replace(tzinfo=timezone.utc)
1127
+ .timestamp()
1128
+ )
1129
+
1130
+ sessions = await self._get_all_sessions_for_metrics_calculation(
1131
+ start_timestamp=start_timestamp, end_timestamp=end_timestamp
1132
+ )
1133
+
1134
+ all_sessions_data = fetch_all_sessions_data(
1135
+ sessions=sessions, dates_to_process=dates_to_process, start_timestamp=start_timestamp
1136
+ )
1137
+ if not all_sessions_data:
1138
+ log_info("No new session data found. Won't calculate metrics.")
1139
+ return None
1140
+
1141
+ results = []
1142
+ metrics_records = []
1143
+
1144
+ for date_to_process in dates_to_process:
1145
+ date_key = date_to_process.isoformat()
1146
+ sessions_for_date = all_sessions_data.get(date_key, {})
1147
+
1148
+ # Skip dates with no sessions
1149
+ if not any(len(sessions) > 0 for sessions in sessions_for_date.values()):
1150
+ continue
1151
+
1152
+ metrics_record = calculate_date_metrics(date_to_process, sessions_for_date)
1153
+
1154
+ metrics_records.append(metrics_record)
1155
+
1156
+ if metrics_records:
1157
+ async with self.async_session_factory() as sess, sess.begin():
1158
+ results = await bulk_upsert_metrics(session=sess, table=table, metrics_records=metrics_records)
1159
+
1160
+ log_debug("Updated metrics calculations")
1161
+
1162
+ return results
1163
+
1164
+ except Exception as e:
1165
+ log_error(f"Exception refreshing metrics: {e}")
1166
+ return None
1167
+
1168
+ async def get_metrics(
1169
+ self, starting_date: Optional[date] = None, ending_date: Optional[date] = None
1170
+ ) -> Tuple[List[dict], Optional[int]]:
1171
+ """Get all metrics matching the given date range.
1172
+
1173
+ Args:
1174
+ starting_date (Optional[date]): The starting date to filter metrics by.
1175
+ ending_date (Optional[date]): The ending date to filter metrics by.
1176
+
1177
+ Returns:
1178
+ Tuple[List[dict], Optional[int]]: A tuple containing the metrics and the timestamp of the latest update.
1179
+
1180
+ Raises:
1181
+ Exception: If an error occurs during retrieval.
1182
+ """
1183
+ try:
1184
+ table = await self._get_table(table_type="metrics")
1185
+
1186
+ async with self.async_session_factory() as sess, sess.begin():
1187
+ stmt = select(table)
1188
+ if starting_date:
1189
+ stmt = stmt.where(table.c.date >= starting_date)
1190
+ if ending_date:
1191
+ stmt = stmt.where(table.c.date <= ending_date)
1192
+ result = await sess.execute(stmt)
1193
+ records = result.fetchall()
1194
+ if not records:
1195
+ return [], None
1196
+
1197
+ # Get the latest updated_at
1198
+ latest_stmt = select(func.max(table.c.updated_at))
1199
+ latest_result = await sess.execute(latest_stmt)
1200
+ latest_updated_at = latest_result.scalar()
1201
+
1202
+ return [dict(row._mapping) for row in records], latest_updated_at
1203
+
1204
+ except Exception as e:
1205
+ log_warning(f"Exception getting metrics: {e}")
1206
+ return [], None
1207
+
1208
+ # -- Knowledge methods --
1209
+ async def delete_knowledge_content(self, id: str):
1210
+ """Delete a knowledge row from the database.
1211
+
1212
+ Args:
1213
+ id (str): The ID of the knowledge row to delete.
1214
+ """
1215
+ table = await self._get_table(table_type="knowledge")
1216
+
1217
+ try:
1218
+ async with self.async_session_factory() as sess, sess.begin():
1219
+ stmt = table.delete().where(table.c.id == id)
1220
+ await sess.execute(stmt)
1221
+
1222
+ except Exception as e:
1223
+ log_error(f"Exception deleting knowledge content: {e}")
1224
+
1225
+ async def get_knowledge_content(self, id: str) -> Optional[KnowledgeRow]:
1226
+ """Get a knowledge row from the database.
1227
+
1228
+ Args:
1229
+ id (str): The ID of the knowledge row to get.
1230
+
1231
+ Returns:
1232
+ Optional[KnowledgeRow]: The knowledge row, or None if it doesn't exist.
1233
+ """
1234
+ table = await self._get_table(table_type="knowledge")
1235
+
1236
+ try:
1237
+ async with self.async_session_factory() as sess, sess.begin():
1238
+ stmt = select(table).where(table.c.id == id)
1239
+ result = await sess.execute(stmt)
1240
+ row = result.fetchone()
1241
+ if row is None:
1242
+ return None
1243
+
1244
+ return KnowledgeRow.model_validate(row._mapping)
1245
+
1246
+ except Exception as e:
1247
+ log_error(f"Exception getting knowledge content: {e}")
1248
+ return None
1249
+
1250
+ async def get_knowledge_contents(
1251
+ self,
1252
+ limit: Optional[int] = None,
1253
+ page: Optional[int] = None,
1254
+ sort_by: Optional[str] = None,
1255
+ sort_order: Optional[str] = None,
1256
+ ) -> Tuple[List[KnowledgeRow], int]:
1257
+ """Get all knowledge contents from the database.
1258
+
1259
+ Args:
1260
+ limit (Optional[int]): The maximum number of knowledge contents to return.
1261
+ page (Optional[int]): The page number.
1262
+ sort_by (Optional[str]): The column to sort by.
1263
+ sort_order (Optional[str]): The order to sort by.
1264
+
1265
+ Returns:
1266
+ List[KnowledgeRow]: The knowledge contents.
1267
+
1268
+ Raises:
1269
+ Exception: If an error occurs during retrieval.
1270
+ """
1271
+ table = await self._get_table(table_type="knowledge")
1272
+
1273
+ try:
1274
+ async with self.async_session_factory() as sess, sess.begin():
1275
+ stmt = select(table)
1276
+
1277
+ # Apply sorting
1278
+ if sort_by is not None:
1279
+ stmt = stmt.order_by(getattr(table.c, sort_by) * (1 if sort_order == "asc" else -1))
1280
+
1281
+ # Get total count before applying limit and pagination
1282
+ count_stmt = select(func.count()).select_from(stmt.alias())
1283
+ total_count = await sess.scalar(count_stmt) or 0
1284
+
1285
+ # Apply pagination after count
1286
+ if limit is not None:
1287
+ stmt = stmt.limit(limit)
1288
+ if page is not None:
1289
+ stmt = stmt.offset((page - 1) * limit)
1290
+
1291
+ result = await sess.execute(stmt)
1292
+ records = result.fetchall()
1293
+ return [KnowledgeRow.model_validate(record._mapping) for record in records], total_count
1294
+
1295
+ except Exception as e:
1296
+ log_error(f"Exception getting knowledge contents: {e}")
1297
+ return [], 0
1298
+
1299
+ async def upsert_knowledge_content(self, knowledge_row: KnowledgeRow):
1300
+ """Upsert knowledge content in the database.
1301
+
1302
+ Args:
1303
+ knowledge_row (KnowledgeRow): The knowledge row to upsert.
1304
+
1305
+ Returns:
1306
+ Optional[KnowledgeRow]: The upserted knowledge row, or None if the operation fails.
1307
+ """
1308
+ try:
1309
+ table = await self._get_table(table_type="knowledge")
1310
+ async with self.async_session_factory() as sess, sess.begin():
1311
+ # Get the actual table columns to avoid "unconsumed column names" error
1312
+ table_columns = set(table.columns.keys())
1313
+
1314
+ # Only include fields that exist in the table and are not None
1315
+ insert_data = {}
1316
+ update_fields = {}
1317
+
1318
+ # Map of KnowledgeRow fields to table columns
1319
+ field_mapping = {
1320
+ "id": "id",
1321
+ "name": "name",
1322
+ "description": "description",
1323
+ "metadata": "metadata",
1324
+ "type": "type",
1325
+ "size": "size",
1326
+ "linked_to": "linked_to",
1327
+ "access_count": "access_count",
1328
+ "status": "status",
1329
+ "status_message": "status_message",
1330
+ "created_at": "created_at",
1331
+ "updated_at": "updated_at",
1332
+ "external_id": "external_id",
1333
+ }
1334
+
1335
+ # Build insert and update data only for fields that exist in the table
1336
+ for model_field, table_column in field_mapping.items():
1337
+ if table_column in table_columns:
1338
+ value = getattr(knowledge_row, model_field, None)
1339
+ if value is not None:
1340
+ insert_data[table_column] = value
1341
+ # Don't include ID in update_fields since it's the primary key
1342
+ if table_column != "id":
1343
+ update_fields[table_column] = value
1344
+
1345
+ # Ensure id is always included for the insert
1346
+ if "id" in table_columns and knowledge_row.id:
1347
+ insert_data["id"] = knowledge_row.id
1348
+
1349
+ # Handle case where update_fields is empty (all fields are None or don't exist in table)
1350
+ if not update_fields:
1351
+ # If we have insert_data, just do an insert without conflict resolution
1352
+ if insert_data:
1353
+ stmt = postgresql.insert(table).values(insert_data)
1354
+ await sess.execute(stmt)
1355
+ else:
1356
+ # If we have no data at all, this is an error
1357
+ log_error("No valid fields found for knowledge row upsert")
1358
+ return None
1359
+ else:
1360
+ # Normal upsert with conflict resolution
1361
+ stmt = (
1362
+ postgresql.insert(table)
1363
+ .values(insert_data)
1364
+ .on_conflict_do_update(index_elements=["id"], set_=update_fields)
1365
+ )
1366
+ await sess.execute(stmt)
1367
+
1368
+ log_debug(f"Upserted knowledge row with id '{knowledge_row.id}'")
1369
+
1370
+ return knowledge_row
1371
+
1372
+ except Exception as e:
1373
+ log_error(f"Error upserting knowledge row: {e}")
1374
+ return None
1375
+
1376
+ # -- Eval methods --
1377
+ async def create_eval_run(self, eval_run: EvalRunRecord) -> Optional[EvalRunRecord]:
1378
+ """Create an EvalRunRecord in the database.
1379
+
1380
+ Args:
1381
+ eval_run (EvalRunRecord): The eval run to create.
1382
+
1383
+ Returns:
1384
+ Optional[EvalRunRecord]: The created eval run, or None if the operation fails.
1385
+
1386
+ Raises:
1387
+ Exception: If an error occurs during creation.
1388
+ """
1389
+ try:
1390
+ table = await self._get_table(table_type="evals")
1391
+
1392
+ async with self.async_session_factory() as sess, sess.begin():
1393
+ current_time = int(time.time())
1394
+ stmt = postgresql.insert(table).values(
1395
+ {"created_at": current_time, "updated_at": current_time, **eval_run.model_dump()}
1396
+ )
1397
+ await sess.execute(stmt)
1398
+
1399
+ log_debug(f"Created eval run with id '{eval_run.run_id}'")
1400
+
1401
+ return eval_run
1402
+
1403
+ except Exception as e:
1404
+ log_error(f"Error creating eval run: {e}")
1405
+ return None
1406
+
1407
+ async def delete_eval_run(self, eval_run_id: str) -> None:
1408
+ """Delete an eval run from the database.
1409
+
1410
+ Args:
1411
+ eval_run_id (str): The ID of the eval run to delete.
1412
+ """
1413
+ try:
1414
+ table = await self._get_table(table_type="evals")
1415
+
1416
+ async with self.async_session_factory() as sess, sess.begin():
1417
+ stmt = table.delete().where(table.c.run_id == eval_run_id)
1418
+ result = await sess.execute(stmt)
1419
+
1420
+ if result.rowcount == 0:
1421
+ log_warning(f"No eval run found with ID: {eval_run_id}")
1422
+ else:
1423
+ log_debug(f"Deleted eval run with ID: {eval_run_id}")
1424
+
1425
+ except Exception as e:
1426
+ log_error(f"Error deleting eval run {eval_run_id}: {e}")
1427
+
1428
+ async def delete_eval_runs(self, eval_run_ids: List[str]) -> None:
1429
+ """Delete multiple eval runs from the database.
1430
+
1431
+ Args:
1432
+ eval_run_ids (List[str]): List of eval run IDs to delete.
1433
+ """
1434
+ try:
1435
+ table = await self._get_table(table_type="evals")
1436
+
1437
+ async with self.async_session_factory() as sess, sess.begin():
1438
+ stmt = table.delete().where(table.c.run_id.in_(eval_run_ids))
1439
+ result = await sess.execute(stmt)
1440
+
1441
+ if result.rowcount == 0:
1442
+ log_warning(f"No eval runs found with IDs: {eval_run_ids}")
1443
+ else:
1444
+ log_debug(f"Deleted {result.rowcount} eval runs")
1445
+
1446
+ except Exception as e:
1447
+ log_error(f"Error deleting eval runs {eval_run_ids}: {e}")
1448
+
1449
+ async def get_eval_run(
1450
+ self, eval_run_id: str, deserialize: Optional[bool] = True
1451
+ ) -> Optional[Union[EvalRunRecord, Dict[str, Any]]]:
1452
+ """Get an eval run from the database.
1453
+
1454
+ Args:
1455
+ eval_run_id (str): The ID of the eval run to get.
1456
+ deserialize (Optional[bool]): Whether to serialize the eval run. Defaults to True.
1457
+
1458
+ Returns:
1459
+ Optional[Union[EvalRunRecord, Dict[str, Any]]]:
1460
+ - When deserialize=True: EvalRunRecord object
1461
+ - When deserialize=False: EvalRun dictionary
1462
+
1463
+ Raises:
1464
+ Exception: If an error occurs during retrieval.
1465
+ """
1466
+ try:
1467
+ table = await self._get_table(table_type="evals")
1468
+
1469
+ async with self.async_session_factory() as sess, sess.begin():
1470
+ stmt = select(table).where(table.c.run_id == eval_run_id)
1471
+ result = await sess.execute(stmt)
1472
+ row = result.fetchone()
1473
+ if row is None:
1474
+ return None
1475
+
1476
+ eval_run_raw = dict(row._mapping)
1477
+ if not deserialize:
1478
+ return eval_run_raw
1479
+
1480
+ return EvalRunRecord.model_validate(eval_run_raw)
1481
+
1482
+ except Exception as e:
1483
+ log_error(f"Exception getting eval run {eval_run_id}: {e}")
1484
+ return None
1485
+
1486
+ async def get_eval_runs(
1487
+ self,
1488
+ limit: Optional[int] = None,
1489
+ page: Optional[int] = None,
1490
+ sort_by: Optional[str] = None,
1491
+ sort_order: Optional[str] = None,
1492
+ agent_id: Optional[str] = None,
1493
+ team_id: Optional[str] = None,
1494
+ workflow_id: Optional[str] = None,
1495
+ model_id: Optional[str] = None,
1496
+ filter_type: Optional[EvalFilterType] = None,
1497
+ eval_type: Optional[List[EvalType]] = None,
1498
+ deserialize: Optional[bool] = True,
1499
+ ) -> Union[List[EvalRunRecord], Tuple[List[Dict[str, Any]], int]]:
1500
+ """Get all eval runs from the database.
1501
+
1502
+ Args:
1503
+ limit (Optional[int]): The maximum number of eval runs to return.
1504
+ page (Optional[int]): The page number.
1505
+ sort_by (Optional[str]): The column to sort by.
1506
+ sort_order (Optional[str]): The order to sort by.
1507
+ agent_id (Optional[str]): The ID of the agent to filter by.
1508
+ team_id (Optional[str]): The ID of the team to filter by.
1509
+ workflow_id (Optional[str]): The ID of the workflow to filter by.
1510
+ model_id (Optional[str]): The ID of the model to filter by.
1511
+ eval_type (Optional[List[EvalType]]): The type(s) of eval to filter by.
1512
+ filter_type (Optional[EvalFilterType]): Filter by component type (agent, team, workflow).
1513
+ deserialize (Optional[bool]): Whether to serialize the eval runs. Defaults to True.
1514
+
1515
+ Returns:
1516
+ Union[List[EvalRunRecord], Tuple[List[Dict[str, Any]], int]]:
1517
+ - When deserialize=True: List of EvalRunRecord objects
1518
+ - When deserialize=False: List of dictionaries
1519
+
1520
+ Raises:
1521
+ Exception: If an error occurs during retrieval.
1522
+ """
1523
+ try:
1524
+ table = await self._get_table(table_type="evals")
1525
+
1526
+ async with self.async_session_factory() as sess, sess.begin():
1527
+ stmt = select(table)
1528
+
1529
+ # Filtering
1530
+ if agent_id is not None:
1531
+ stmt = stmt.where(table.c.agent_id == agent_id)
1532
+ if team_id is not None:
1533
+ stmt = stmt.where(table.c.team_id == team_id)
1534
+ if workflow_id is not None:
1535
+ stmt = stmt.where(table.c.workflow_id == workflow_id)
1536
+ if model_id is not None:
1537
+ stmt = stmt.where(table.c.model_id == model_id)
1538
+ if eval_type is not None and len(eval_type) > 0:
1539
+ stmt = stmt.where(table.c.eval_type.in_(eval_type))
1540
+ if filter_type is not None:
1541
+ if filter_type == EvalFilterType.AGENT:
1542
+ stmt = stmt.where(table.c.agent_id.is_not(None))
1543
+ elif filter_type == EvalFilterType.TEAM:
1544
+ stmt = stmt.where(table.c.team_id.is_not(None))
1545
+ elif filter_type == EvalFilterType.WORKFLOW:
1546
+ stmt = stmt.where(table.c.workflow_id.is_not(None))
1547
+
1548
+ # Get total count after applying filtering
1549
+ count_stmt = select(func.count()).select_from(stmt.alias())
1550
+ total_count = await sess.scalar(count_stmt) or 0
1551
+
1552
+ # Sorting
1553
+ if sort_by is None:
1554
+ stmt = stmt.order_by(table.c.created_at.desc())
1555
+ else:
1556
+ stmt = apply_sorting(stmt, table, sort_by, sort_order)
1557
+
1558
+ # Paginating
1559
+ if limit is not None:
1560
+ stmt = stmt.limit(limit)
1561
+ if page is not None:
1562
+ stmt = stmt.offset((page - 1) * limit)
1563
+
1564
+ result = await sess.execute(stmt)
1565
+ records = result.fetchall()
1566
+ if not records:
1567
+ return [] if deserialize else ([], 0)
1568
+
1569
+ eval_runs_raw = [dict(row._mapping) for row in records]
1570
+ if not deserialize:
1571
+ return eval_runs_raw, total_count
1572
+
1573
+ return [EvalRunRecord.model_validate(row) for row in eval_runs_raw]
1574
+
1575
+ except Exception as e:
1576
+ log_error(f"Exception getting eval runs: {e}")
1577
+ return [] if deserialize else ([], 0)
1578
+
1579
+ async def rename_eval_run(
1580
+ self, eval_run_id: str, name: str, deserialize: Optional[bool] = True
1581
+ ) -> Optional[Union[EvalRunRecord, Dict[str, Any]]]:
1582
+ """Upsert the name of an eval run in the database, returning raw dictionary.
1583
+
1584
+ Args:
1585
+ eval_run_id (str): The ID of the eval run to update.
1586
+ name (str): The new name of the eval run.
1587
+
1588
+ Returns:
1589
+ Optional[Dict[str, Any]]: The updated eval run, or None if the operation fails.
1590
+
1591
+ Raises:
1592
+ Exception: If an error occurs during update.
1593
+ """
1594
+ try:
1595
+ table = await self._get_table(table_type="evals")
1596
+ async with self.async_session_factory() as sess, sess.begin():
1597
+ stmt = (
1598
+ table.update().where(table.c.run_id == eval_run_id).values(name=name, updated_at=int(time.time()))
1599
+ )
1600
+ await sess.execute(stmt)
1601
+
1602
+ eval_run_raw = await self.get_eval_run(eval_run_id=eval_run_id, deserialize=deserialize)
1603
+ if not eval_run_raw or not deserialize:
1604
+ return eval_run_raw
1605
+
1606
+ return EvalRunRecord.model_validate(eval_run_raw)
1607
+
1608
+ except Exception as e:
1609
+ log_error(f"Error upserting eval run name {eval_run_id}: {e}")
1610
+ return None
1611
+
1612
+ # -- Migrations --
1613
+
1614
+ async def migrate_table_from_v1_to_v2(self, v1_db_schema: str, v1_table_name: str, v1_table_type: str):
1615
+ """Migrate all content in the given table to the right v2 table"""
1616
+
1617
+ from agno.db.migrations.v1_to_v2 import (
1618
+ get_all_table_content,
1619
+ parse_agent_sessions,
1620
+ parse_memories,
1621
+ parse_team_sessions,
1622
+ parse_workflow_sessions,
1623
+ )
1624
+
1625
+ # Get all content from the old table
1626
+ old_content: list[dict[str, Any]] = get_all_table_content(
1627
+ db=self,
1628
+ db_schema=v1_db_schema,
1629
+ table_name=v1_table_name,
1630
+ )
1631
+ if not old_content:
1632
+ log_info(f"No content to migrate from table {v1_table_name}")
1633
+ return
1634
+
1635
+ # Parse the content into the new format
1636
+ memories: List[UserMemory] = []
1637
+ sessions: Sequence[Union[AgentSession, TeamSession, WorkflowSession]] = []
1638
+ if v1_table_type == "agent_sessions":
1639
+ sessions = parse_agent_sessions(old_content)
1640
+ elif v1_table_type == "team_sessions":
1641
+ sessions = parse_team_sessions(old_content)
1642
+ elif v1_table_type == "workflow_sessions":
1643
+ sessions = parse_workflow_sessions(old_content)
1644
+ elif v1_table_type == "memories":
1645
+ memories = parse_memories(old_content)
1646
+ else:
1647
+ raise ValueError(f"Invalid table type: {v1_table_type}")
1648
+
1649
+ # Insert the new content into the new table
1650
+ if v1_table_type == "agent_sessions":
1651
+ for session in sessions:
1652
+ await self.upsert_session(session)
1653
+ log_info(f"Migrated {len(sessions)} Agent sessions to table: {self.session_table}")
1654
+
1655
+ elif v1_table_type == "team_sessions":
1656
+ for session in sessions:
1657
+ await self.upsert_session(session)
1658
+ log_info(f"Migrated {len(sessions)} Team sessions to table: {self.session_table}")
1659
+
1660
+ elif v1_table_type == "workflow_sessions":
1661
+ for session in sessions:
1662
+ await self.upsert_session(session)
1663
+ log_info(f"Migrated {len(sessions)} Workflow sessions to table: {self.session_table}")
1664
+
1665
+ elif v1_table_type == "memories":
1666
+ for memory in memories:
1667
+ await self.upsert_user_memory(memory)
1668
+ log_info(f"Migrated {len(memories)} memories to table: {self.memory_table}")