agno 2.1.2__py3-none-any.whl → 2.3.13__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.
- agno/agent/agent.py +5540 -2273
- agno/api/api.py +2 -0
- agno/api/os.py +1 -1
- agno/compression/__init__.py +3 -0
- agno/compression/manager.py +247 -0
- agno/culture/__init__.py +3 -0
- agno/culture/manager.py +956 -0
- agno/db/async_postgres/__init__.py +3 -0
- agno/db/base.py +689 -6
- agno/db/dynamo/dynamo.py +933 -37
- agno/db/dynamo/schemas.py +174 -10
- agno/db/dynamo/utils.py +63 -4
- agno/db/firestore/firestore.py +831 -9
- agno/db/firestore/schemas.py +51 -0
- agno/db/firestore/utils.py +102 -4
- agno/db/gcs_json/gcs_json_db.py +660 -12
- agno/db/gcs_json/utils.py +60 -26
- agno/db/in_memory/in_memory_db.py +287 -14
- agno/db/in_memory/utils.py +60 -2
- agno/db/json/json_db.py +590 -14
- agno/db/json/utils.py +60 -26
- agno/db/migrations/manager.py +199 -0
- agno/db/migrations/v1_to_v2.py +43 -13
- agno/db/migrations/versions/__init__.py +0 -0
- agno/db/migrations/versions/v2_3_0.py +938 -0
- agno/db/mongo/__init__.py +15 -1
- agno/db/mongo/async_mongo.py +2760 -0
- agno/db/mongo/mongo.py +879 -11
- agno/db/mongo/schemas.py +42 -0
- agno/db/mongo/utils.py +80 -8
- agno/db/mysql/__init__.py +2 -1
- agno/db/mysql/async_mysql.py +2912 -0
- agno/db/mysql/mysql.py +946 -68
- agno/db/mysql/schemas.py +72 -10
- agno/db/mysql/utils.py +198 -7
- agno/db/postgres/__init__.py +2 -1
- agno/db/postgres/async_postgres.py +2579 -0
- agno/db/postgres/postgres.py +942 -57
- agno/db/postgres/schemas.py +81 -18
- agno/db/postgres/utils.py +164 -2
- agno/db/redis/redis.py +671 -7
- agno/db/redis/schemas.py +50 -0
- agno/db/redis/utils.py +65 -7
- agno/db/schemas/__init__.py +2 -1
- agno/db/schemas/culture.py +120 -0
- agno/db/schemas/evals.py +1 -0
- agno/db/schemas/memory.py +17 -2
- agno/db/singlestore/schemas.py +63 -0
- agno/db/singlestore/singlestore.py +949 -83
- agno/db/singlestore/utils.py +60 -2
- agno/db/sqlite/__init__.py +2 -1
- agno/db/sqlite/async_sqlite.py +2911 -0
- agno/db/sqlite/schemas.py +62 -0
- agno/db/sqlite/sqlite.py +965 -46
- agno/db/sqlite/utils.py +169 -8
- agno/db/surrealdb/__init__.py +3 -0
- agno/db/surrealdb/metrics.py +292 -0
- agno/db/surrealdb/models.py +334 -0
- agno/db/surrealdb/queries.py +71 -0
- agno/db/surrealdb/surrealdb.py +1908 -0
- agno/db/surrealdb/utils.py +147 -0
- agno/db/utils.py +2 -0
- agno/eval/__init__.py +10 -0
- agno/eval/accuracy.py +75 -55
- agno/eval/agent_as_judge.py +861 -0
- agno/eval/base.py +29 -0
- agno/eval/performance.py +16 -7
- agno/eval/reliability.py +28 -16
- agno/eval/utils.py +35 -17
- agno/exceptions.py +27 -2
- agno/filters.py +354 -0
- agno/guardrails/prompt_injection.py +1 -0
- agno/hooks/__init__.py +3 -0
- agno/hooks/decorator.py +164 -0
- agno/integrations/discord/client.py +1 -1
- agno/knowledge/chunking/agentic.py +13 -10
- agno/knowledge/chunking/fixed.py +4 -1
- agno/knowledge/chunking/semantic.py +9 -4
- agno/knowledge/chunking/strategy.py +59 -15
- agno/knowledge/embedder/fastembed.py +1 -1
- agno/knowledge/embedder/nebius.py +1 -1
- agno/knowledge/embedder/ollama.py +8 -0
- agno/knowledge/embedder/openai.py +8 -8
- agno/knowledge/embedder/sentence_transformer.py +6 -2
- agno/knowledge/embedder/vllm.py +262 -0
- agno/knowledge/knowledge.py +1618 -318
- agno/knowledge/reader/base.py +6 -2
- agno/knowledge/reader/csv_reader.py +8 -10
- agno/knowledge/reader/docx_reader.py +5 -6
- agno/knowledge/reader/field_labeled_csv_reader.py +16 -20
- agno/knowledge/reader/json_reader.py +5 -4
- agno/knowledge/reader/markdown_reader.py +8 -8
- agno/knowledge/reader/pdf_reader.py +17 -19
- agno/knowledge/reader/pptx_reader.py +101 -0
- agno/knowledge/reader/reader_factory.py +32 -3
- agno/knowledge/reader/s3_reader.py +3 -3
- agno/knowledge/reader/tavily_reader.py +193 -0
- agno/knowledge/reader/text_reader.py +22 -10
- agno/knowledge/reader/web_search_reader.py +1 -48
- agno/knowledge/reader/website_reader.py +10 -10
- agno/knowledge/reader/wikipedia_reader.py +33 -1
- agno/knowledge/types.py +1 -0
- agno/knowledge/utils.py +72 -7
- agno/media.py +22 -6
- agno/memory/__init__.py +14 -1
- agno/memory/manager.py +544 -83
- agno/memory/strategies/__init__.py +15 -0
- agno/memory/strategies/base.py +66 -0
- agno/memory/strategies/summarize.py +196 -0
- agno/memory/strategies/types.py +37 -0
- agno/models/aimlapi/aimlapi.py +17 -0
- agno/models/anthropic/claude.py +515 -40
- agno/models/aws/bedrock.py +102 -21
- agno/models/aws/claude.py +131 -274
- agno/models/azure/ai_foundry.py +41 -19
- agno/models/azure/openai_chat.py +39 -8
- agno/models/base.py +1249 -525
- agno/models/cerebras/cerebras.py +91 -21
- agno/models/cerebras/cerebras_openai.py +21 -2
- agno/models/cohere/chat.py +40 -6
- agno/models/cometapi/cometapi.py +18 -1
- agno/models/dashscope/dashscope.py +2 -3
- agno/models/deepinfra/deepinfra.py +18 -1
- agno/models/deepseek/deepseek.py +69 -3
- agno/models/fireworks/fireworks.py +18 -1
- agno/models/google/gemini.py +877 -80
- agno/models/google/utils.py +22 -0
- agno/models/groq/groq.py +51 -18
- agno/models/huggingface/huggingface.py +17 -6
- agno/models/ibm/watsonx.py +16 -6
- agno/models/internlm/internlm.py +18 -1
- agno/models/langdb/langdb.py +13 -1
- agno/models/litellm/chat.py +44 -9
- agno/models/litellm/litellm_openai.py +18 -1
- agno/models/message.py +28 -5
- agno/models/meta/llama.py +47 -14
- agno/models/meta/llama_openai.py +22 -17
- agno/models/mistral/mistral.py +8 -4
- agno/models/nebius/nebius.py +6 -7
- agno/models/nvidia/nvidia.py +20 -3
- agno/models/ollama/chat.py +24 -8
- agno/models/openai/chat.py +104 -29
- agno/models/openai/responses.py +101 -81
- agno/models/openrouter/openrouter.py +60 -3
- agno/models/perplexity/perplexity.py +17 -1
- agno/models/portkey/portkey.py +7 -6
- agno/models/requesty/requesty.py +24 -4
- agno/models/response.py +73 -2
- agno/models/sambanova/sambanova.py +20 -3
- agno/models/siliconflow/siliconflow.py +19 -2
- agno/models/together/together.py +20 -3
- agno/models/utils.py +254 -8
- agno/models/vercel/v0.py +20 -3
- agno/models/vertexai/__init__.py +0 -0
- agno/models/vertexai/claude.py +190 -0
- agno/models/vllm/vllm.py +19 -14
- agno/models/xai/xai.py +19 -2
- agno/os/app.py +549 -152
- agno/os/auth.py +190 -3
- agno/os/config.py +23 -0
- agno/os/interfaces/a2a/router.py +8 -11
- agno/os/interfaces/a2a/utils.py +1 -1
- agno/os/interfaces/agui/router.py +18 -3
- agno/os/interfaces/agui/utils.py +152 -39
- agno/os/interfaces/slack/router.py +55 -37
- agno/os/interfaces/slack/slack.py +9 -1
- agno/os/interfaces/whatsapp/router.py +0 -1
- agno/os/interfaces/whatsapp/security.py +3 -1
- agno/os/mcp.py +110 -52
- agno/os/middleware/__init__.py +2 -0
- agno/os/middleware/jwt.py +676 -112
- agno/os/router.py +40 -1478
- agno/os/routers/agents/__init__.py +3 -0
- agno/os/routers/agents/router.py +599 -0
- agno/os/routers/agents/schema.py +261 -0
- agno/os/routers/evals/evals.py +96 -39
- agno/os/routers/evals/schemas.py +65 -33
- agno/os/routers/evals/utils.py +80 -10
- agno/os/routers/health.py +10 -4
- agno/os/routers/knowledge/knowledge.py +196 -38
- agno/os/routers/knowledge/schemas.py +82 -22
- agno/os/routers/memory/memory.py +279 -52
- agno/os/routers/memory/schemas.py +46 -17
- agno/os/routers/metrics/metrics.py +20 -8
- agno/os/routers/metrics/schemas.py +16 -16
- agno/os/routers/session/session.py +462 -34
- agno/os/routers/teams/__init__.py +3 -0
- agno/os/routers/teams/router.py +512 -0
- agno/os/routers/teams/schema.py +257 -0
- agno/os/routers/traces/__init__.py +3 -0
- agno/os/routers/traces/schemas.py +414 -0
- agno/os/routers/traces/traces.py +499 -0
- agno/os/routers/workflows/__init__.py +3 -0
- agno/os/routers/workflows/router.py +624 -0
- agno/os/routers/workflows/schema.py +75 -0
- agno/os/schema.py +256 -693
- agno/os/scopes.py +469 -0
- agno/os/utils.py +514 -36
- agno/reasoning/anthropic.py +80 -0
- agno/reasoning/gemini.py +73 -0
- agno/reasoning/openai.py +5 -0
- agno/reasoning/vertexai.py +76 -0
- agno/run/__init__.py +6 -0
- agno/run/agent.py +155 -32
- agno/run/base.py +55 -3
- agno/run/requirement.py +181 -0
- agno/run/team.py +125 -38
- agno/run/workflow.py +72 -18
- agno/session/agent.py +102 -89
- agno/session/summary.py +56 -15
- agno/session/team.py +164 -90
- agno/session/workflow.py +405 -40
- agno/table.py +10 -0
- agno/team/team.py +3974 -1903
- agno/tools/dalle.py +2 -4
- agno/tools/eleven_labs.py +23 -25
- agno/tools/exa.py +21 -16
- agno/tools/file.py +153 -23
- agno/tools/file_generation.py +16 -10
- agno/tools/firecrawl.py +15 -7
- agno/tools/function.py +193 -38
- agno/tools/gmail.py +238 -14
- agno/tools/google_drive.py +271 -0
- agno/tools/googlecalendar.py +36 -8
- agno/tools/googlesheets.py +20 -5
- agno/tools/jira.py +20 -0
- agno/tools/mcp/__init__.py +10 -0
- agno/tools/mcp/mcp.py +331 -0
- agno/tools/mcp/multi_mcp.py +347 -0
- agno/tools/mcp/params.py +24 -0
- agno/tools/mcp_toolbox.py +3 -3
- agno/tools/models/nebius.py +5 -5
- agno/tools/models_labs.py +20 -10
- agno/tools/nano_banana.py +151 -0
- agno/tools/notion.py +204 -0
- agno/tools/parallel.py +314 -0
- agno/tools/postgres.py +76 -36
- agno/tools/redshift.py +406 -0
- agno/tools/scrapegraph.py +1 -1
- agno/tools/shopify.py +1519 -0
- agno/tools/slack.py +18 -3
- agno/tools/spotify.py +919 -0
- agno/tools/tavily.py +146 -0
- agno/tools/toolkit.py +25 -0
- agno/tools/workflow.py +8 -1
- agno/tools/yfinance.py +12 -11
- agno/tracing/__init__.py +12 -0
- agno/tracing/exporter.py +157 -0
- agno/tracing/schemas.py +276 -0
- agno/tracing/setup.py +111 -0
- agno/utils/agent.py +938 -0
- agno/utils/cryptography.py +22 -0
- agno/utils/dttm.py +33 -0
- agno/utils/events.py +151 -3
- agno/utils/gemini.py +15 -5
- agno/utils/hooks.py +118 -4
- agno/utils/http.py +113 -2
- agno/utils/knowledge.py +12 -5
- agno/utils/log.py +1 -0
- agno/utils/mcp.py +92 -2
- agno/utils/media.py +187 -1
- agno/utils/merge_dict.py +3 -3
- agno/utils/message.py +60 -0
- agno/utils/models/ai_foundry.py +9 -2
- agno/utils/models/claude.py +49 -14
- agno/utils/models/cohere.py +9 -2
- agno/utils/models/llama.py +9 -2
- agno/utils/models/mistral.py +4 -2
- agno/utils/print_response/agent.py +109 -16
- agno/utils/print_response/team.py +223 -30
- agno/utils/print_response/workflow.py +251 -34
- agno/utils/streamlit.py +1 -1
- agno/utils/team.py +98 -9
- agno/utils/tokens.py +657 -0
- agno/vectordb/base.py +39 -7
- agno/vectordb/cassandra/cassandra.py +21 -5
- agno/vectordb/chroma/chromadb.py +43 -12
- agno/vectordb/clickhouse/clickhousedb.py +21 -5
- agno/vectordb/couchbase/couchbase.py +29 -5
- agno/vectordb/lancedb/lance_db.py +92 -181
- agno/vectordb/langchaindb/langchaindb.py +24 -4
- agno/vectordb/lightrag/lightrag.py +17 -3
- agno/vectordb/llamaindex/llamaindexdb.py +25 -5
- agno/vectordb/milvus/milvus.py +50 -37
- agno/vectordb/mongodb/__init__.py +7 -1
- agno/vectordb/mongodb/mongodb.py +36 -30
- agno/vectordb/pgvector/pgvector.py +201 -77
- agno/vectordb/pineconedb/pineconedb.py +41 -23
- agno/vectordb/qdrant/qdrant.py +67 -54
- agno/vectordb/redis/__init__.py +9 -0
- agno/vectordb/redis/redisdb.py +682 -0
- agno/vectordb/singlestore/singlestore.py +50 -29
- agno/vectordb/surrealdb/surrealdb.py +31 -41
- agno/vectordb/upstashdb/upstashdb.py +34 -6
- agno/vectordb/weaviate/weaviate.py +53 -14
- agno/workflow/__init__.py +2 -0
- agno/workflow/agent.py +299 -0
- agno/workflow/condition.py +120 -18
- agno/workflow/loop.py +77 -10
- agno/workflow/parallel.py +231 -143
- agno/workflow/router.py +118 -17
- agno/workflow/step.py +609 -170
- agno/workflow/steps.py +73 -6
- agno/workflow/types.py +96 -21
- agno/workflow/workflow.py +2039 -262
- {agno-2.1.2.dist-info → agno-2.3.13.dist-info}/METADATA +201 -66
- agno-2.3.13.dist-info/RECORD +613 -0
- agno/tools/googlesearch.py +0 -98
- agno/tools/mcp.py +0 -679
- agno/tools/memori.py +0 -339
- agno-2.1.2.dist-info/RECORD +0 -543
- {agno-2.1.2.dist-info → agno-2.3.13.dist-info}/WHEEL +0 -0
- {agno-2.1.2.dist-info → agno-2.3.13.dist-info}/licenses/LICENSE +0 -0
- {agno-2.1.2.dist-info → agno-2.3.13.dist-info}/top_level.txt +0 -0
agno/db/sqlite/utils.py
CHANGED
|
@@ -4,6 +4,9 @@ from datetime import date, datetime, timedelta, timezone
|
|
|
4
4
|
from typing import Any, Dict, List, Optional
|
|
5
5
|
from uuid import uuid4
|
|
6
6
|
|
|
7
|
+
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
|
|
8
|
+
|
|
9
|
+
from agno.db.schemas.culture import CulturalKnowledge
|
|
7
10
|
from agno.db.sqlite.schemas import get_table_schema_definition
|
|
8
11
|
from agno.utils.log import log_debug, log_error, log_warning
|
|
9
12
|
|
|
@@ -49,6 +52,7 @@ def is_table_available(session: Session, table_name: str, db_schema: Optional[st
|
|
|
49
52
|
"""
|
|
50
53
|
Check if a table with the given name exists.
|
|
51
54
|
Note: db_schema parameter is ignored in SQLite but kept for API compatibility.
|
|
55
|
+
|
|
52
56
|
Returns:
|
|
53
57
|
bool: True if the table exists, False otherwise.
|
|
54
58
|
"""
|
|
@@ -64,15 +68,34 @@ def is_table_available(session: Session, table_name: str, db_schema: Optional[st
|
|
|
64
68
|
return False
|
|
65
69
|
|
|
66
70
|
|
|
67
|
-
def
|
|
71
|
+
async def ais_table_available(session: AsyncSession, table_name: str, db_schema: Optional[str] = None) -> bool:
|
|
68
72
|
"""
|
|
69
|
-
Check if
|
|
73
|
+
Check if a table with the given name exists.
|
|
70
74
|
Note: db_schema parameter is ignored in SQLite but kept for API compatibility.
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
bool: True if the table exists, False otherwise.
|
|
78
|
+
"""
|
|
79
|
+
try:
|
|
80
|
+
exists_query = text("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = :table")
|
|
81
|
+
exists = (await session.execute(exists_query, {"table": table_name})).scalar() is not None
|
|
82
|
+
if not exists:
|
|
83
|
+
log_debug(f"Table {table_name} {'exists' if exists else 'does not exist'}")
|
|
84
|
+
return exists
|
|
85
|
+
except Exception as e:
|
|
86
|
+
log_error(f"Error checking if table exists: {e}")
|
|
87
|
+
return False
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def is_valid_table(db_engine: Engine, table_name: str, table_type: str) -> bool:
|
|
91
|
+
"""
|
|
92
|
+
Check if the existing table has the expected column names.
|
|
93
|
+
|
|
71
94
|
Args:
|
|
72
95
|
db_engine (Engine): Database engine
|
|
73
96
|
table_name (str): Name of the table to validate
|
|
74
97
|
table_type (str): Type of table to get expected schema
|
|
75
|
-
|
|
98
|
+
|
|
76
99
|
Returns:
|
|
77
100
|
bool: True if table has all expected columns, False otherwise
|
|
78
101
|
"""
|
|
@@ -97,6 +120,45 @@ def is_valid_table(db_engine: Engine, table_name: str, table_type: str, db_schem
|
|
|
97
120
|
return False
|
|
98
121
|
|
|
99
122
|
|
|
123
|
+
async def ais_valid_table(db_engine: AsyncEngine, table_name: str, table_type: str) -> bool:
|
|
124
|
+
"""
|
|
125
|
+
Check if the existing table has the expected column names.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
db_engine (Engine): Database engine
|
|
129
|
+
table_name (str): Name of the table to validate
|
|
130
|
+
table_type (str): Type of table to get expected schema
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
bool: True if table has all expected columns, False otherwise
|
|
134
|
+
"""
|
|
135
|
+
try:
|
|
136
|
+
expected_table_schema = get_table_schema_definition(table_type)
|
|
137
|
+
expected_columns = {col_name for col_name in expected_table_schema.keys() if not col_name.startswith("_")}
|
|
138
|
+
|
|
139
|
+
# Get existing columns from the async engine
|
|
140
|
+
async with db_engine.connect() as conn:
|
|
141
|
+
existing_columns = await conn.run_sync(_get_table_columns, table_name)
|
|
142
|
+
|
|
143
|
+
missing_columns = expected_columns - existing_columns
|
|
144
|
+
if missing_columns:
|
|
145
|
+
log_warning(f"Missing columns {missing_columns} in table {table_name}")
|
|
146
|
+
return False
|
|
147
|
+
|
|
148
|
+
return True
|
|
149
|
+
|
|
150
|
+
except Exception as e:
|
|
151
|
+
log_error(f"Error validating table schema for {table_name}: {e}")
|
|
152
|
+
return False
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _get_table_columns(conn, table_name: str) -> set[str]:
|
|
156
|
+
"""Helper function to get table columns using sync inspector."""
|
|
157
|
+
inspector = inspect(conn)
|
|
158
|
+
columns_info = inspector.get_columns(table_name)
|
|
159
|
+
return {col["name"] for col in columns_info}
|
|
160
|
+
|
|
161
|
+
|
|
100
162
|
# -- Metrics util methods --
|
|
101
163
|
|
|
102
164
|
|
|
@@ -133,6 +195,39 @@ def bulk_upsert_metrics(session: Session, table: Table, metrics_records: list[di
|
|
|
133
195
|
return results # type: ignore
|
|
134
196
|
|
|
135
197
|
|
|
198
|
+
async def abulk_upsert_metrics(session: AsyncSession, table: Table, metrics_records: list[dict]) -> list[dict]:
|
|
199
|
+
"""Bulk upsert metrics into the database.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
table (Table): The table to upsert into.
|
|
203
|
+
metrics_records (list[dict]): The metrics records to upsert.
|
|
204
|
+
|
|
205
|
+
Returns:
|
|
206
|
+
list[dict]: The upserted metrics records.
|
|
207
|
+
"""
|
|
208
|
+
if not metrics_records:
|
|
209
|
+
return []
|
|
210
|
+
|
|
211
|
+
results = []
|
|
212
|
+
stmt = sqlite.insert(table)
|
|
213
|
+
|
|
214
|
+
# Columns to update in case of conflict
|
|
215
|
+
update_columns = {
|
|
216
|
+
col.name: stmt.excluded[col.name]
|
|
217
|
+
for col in table.columns
|
|
218
|
+
if col.name not in ["id", "date", "created_at", "aggregation_period"]
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
stmt = stmt.on_conflict_do_update(index_elements=["date", "aggregation_period"], set_=update_columns).returning( # type: ignore
|
|
222
|
+
table
|
|
223
|
+
)
|
|
224
|
+
result = await session.execute(stmt, metrics_records)
|
|
225
|
+
results = [dict(row._mapping) for row in result.fetchall()]
|
|
226
|
+
await session.commit()
|
|
227
|
+
|
|
228
|
+
return results # type: ignore
|
|
229
|
+
|
|
230
|
+
|
|
136
231
|
def calculate_date_metrics(date_to_process: date, sessions_data: dict) -> dict:
|
|
137
232
|
"""Calculate metrics for the given single date.
|
|
138
233
|
|
|
@@ -173,15 +268,17 @@ def calculate_date_metrics(date_to_process: date, sessions_data: dict) -> dict:
|
|
|
173
268
|
all_user_ids = set()
|
|
174
269
|
|
|
175
270
|
for session_type, sessions_count_key, runs_count_key in session_types:
|
|
176
|
-
sessions = sessions_data.get(session_type, [])
|
|
271
|
+
sessions = sessions_data.get(session_type, []) or []
|
|
177
272
|
metrics[sessions_count_key] = len(sessions)
|
|
178
273
|
|
|
179
274
|
for session in sessions:
|
|
180
275
|
if session.get("user_id"):
|
|
181
276
|
all_user_ids.add(session["user_id"])
|
|
182
|
-
|
|
277
|
+
|
|
278
|
+
# Parse runs from JSON string
|
|
183
279
|
if runs := session.get("runs", []):
|
|
184
|
-
runs = json.loads(runs)
|
|
280
|
+
runs = json.loads(runs) if isinstance(runs, str) else runs
|
|
281
|
+
metrics[runs_count_key] += len(runs)
|
|
185
282
|
for run in runs:
|
|
186
283
|
if model_id := run.get("model"):
|
|
187
284
|
model_provider = run.get("model_provider", "")
|
|
@@ -189,14 +286,17 @@ def calculate_date_metrics(date_to_process: date, sessions_data: dict) -> dict:
|
|
|
189
286
|
model_counts.get(f"{model_id}:{model_provider}", 0) + 1
|
|
190
287
|
)
|
|
191
288
|
|
|
192
|
-
|
|
289
|
+
# Parse session_data from JSON string
|
|
290
|
+
session_data = session.get("session_data", {})
|
|
291
|
+
if isinstance(session_data, str):
|
|
292
|
+
session_data = json.loads(session_data)
|
|
193
293
|
session_metrics = session_data.get("session_metrics", {})
|
|
194
294
|
for field in token_metrics:
|
|
195
295
|
token_metrics[field] += session_metrics.get(field, 0)
|
|
196
296
|
|
|
197
297
|
model_metrics = []
|
|
198
298
|
for model, count in model_counts.items():
|
|
199
|
-
model_id, model_provider = model.
|
|
299
|
+
model_id, model_provider = model.rsplit(":", 1)
|
|
200
300
|
model_metrics.append({"model_id": model_id, "model_provider": model_provider, "count": count})
|
|
201
301
|
|
|
202
302
|
metrics["users_count"] = len(all_user_ids)
|
|
@@ -266,3 +366,64 @@ def get_dates_to_calculate_metrics_for(starting_date: date) -> list[date]:
|
|
|
266
366
|
if days_diff <= 0:
|
|
267
367
|
return []
|
|
268
368
|
return [starting_date + timedelta(days=x) for x in range(days_diff)]
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
# -- Cultural Knowledge util methods --
|
|
372
|
+
def serialize_cultural_knowledge_for_db(cultural_knowledge: CulturalKnowledge) -> str:
|
|
373
|
+
"""Serialize a CulturalKnowledge object for database storage.
|
|
374
|
+
|
|
375
|
+
Converts the model's separate content, categories, and notes fields
|
|
376
|
+
into a single JSON string for the database content column.
|
|
377
|
+
SQLite requires JSON to be stored as strings.
|
|
378
|
+
|
|
379
|
+
Args:
|
|
380
|
+
cultural_knowledge (CulturalKnowledge): The cultural knowledge object to serialize.
|
|
381
|
+
|
|
382
|
+
Returns:
|
|
383
|
+
str: A JSON string containing content, categories, and notes.
|
|
384
|
+
"""
|
|
385
|
+
content_dict: Dict[str, Any] = {}
|
|
386
|
+
if cultural_knowledge.content is not None:
|
|
387
|
+
content_dict["content"] = cultural_knowledge.content
|
|
388
|
+
if cultural_knowledge.categories is not None:
|
|
389
|
+
content_dict["categories"] = cultural_knowledge.categories
|
|
390
|
+
if cultural_knowledge.notes is not None:
|
|
391
|
+
content_dict["notes"] = cultural_knowledge.notes
|
|
392
|
+
|
|
393
|
+
return json.dumps(content_dict) if content_dict else None # type: ignore
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def deserialize_cultural_knowledge_from_db(db_row: Dict[str, Any]) -> CulturalKnowledge:
|
|
397
|
+
"""Deserialize a database row to a CulturalKnowledge object.
|
|
398
|
+
|
|
399
|
+
The database stores content as a JSON dict containing content, categories, and notes.
|
|
400
|
+
This method extracts those fields and converts them back to the model format.
|
|
401
|
+
|
|
402
|
+
Args:
|
|
403
|
+
db_row (Dict[str, Any]): The database row as a dictionary.
|
|
404
|
+
|
|
405
|
+
Returns:
|
|
406
|
+
CulturalKnowledge: The cultural knowledge object.
|
|
407
|
+
"""
|
|
408
|
+
# Extract content, categories, and notes from the JSON content field
|
|
409
|
+
content_json = db_row.get("content", {}) or {}
|
|
410
|
+
|
|
411
|
+
if isinstance(content_json, str):
|
|
412
|
+
content_json = json.loads(content_json) if content_json else {}
|
|
413
|
+
|
|
414
|
+
return CulturalKnowledge.from_dict(
|
|
415
|
+
{
|
|
416
|
+
"id": db_row.get("id"),
|
|
417
|
+
"name": db_row.get("name"),
|
|
418
|
+
"summary": db_row.get("summary"),
|
|
419
|
+
"content": content_json.get("content"),
|
|
420
|
+
"categories": content_json.get("categories"),
|
|
421
|
+
"notes": content_json.get("notes"),
|
|
422
|
+
"metadata": db_row.get("metadata"),
|
|
423
|
+
"input": db_row.get("input"),
|
|
424
|
+
"created_at": db_row.get("created_at"),
|
|
425
|
+
"updated_at": db_row.get("updated_at"),
|
|
426
|
+
"agent_id": db_row.get("agent_id"),
|
|
427
|
+
"team_id": db_row.get("team_id"),
|
|
428
|
+
}
|
|
429
|
+
)
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
from datetime import date, datetime, timedelta, timezone
|
|
2
|
+
from textwrap import dedent
|
|
3
|
+
from typing import Any, Callable, Dict, List, Optional, Union
|
|
4
|
+
|
|
5
|
+
from surrealdb import BlockingHttpSurrealConnection, BlockingWsSurrealConnection, RecordID
|
|
6
|
+
|
|
7
|
+
from agno.db.base import SessionType
|
|
8
|
+
from agno.db.surrealdb import utils
|
|
9
|
+
from agno.db.surrealdb.models import desurrealize_session, surrealize_dates
|
|
10
|
+
from agno.db.surrealdb.queries import WhereClause
|
|
11
|
+
from agno.utils.log import log_error
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_all_sessions_for_metrics_calculation(
|
|
15
|
+
client: Union[BlockingWsSurrealConnection, BlockingHttpSurrealConnection],
|
|
16
|
+
table: str,
|
|
17
|
+
start_timestamp: Optional[datetime] = None,
|
|
18
|
+
end_timestamp: Optional[datetime] = None,
|
|
19
|
+
) -> List[Dict[str, Any]]:
|
|
20
|
+
"""
|
|
21
|
+
Get all sessions of all types (agent, team, workflow) as raw dictionaries.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
start_timestamp (Optional[int]): The start timestamp to filter by. Defaults to None.
|
|
25
|
+
end_timestamp (Optional[int]): The end timestamp to filter by. Defaults to None.
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
List[Dict[str, Any]]: List of session dictionaries with session_type field.
|
|
29
|
+
|
|
30
|
+
Raises:
|
|
31
|
+
Exception: If an error occurs during retrieval.
|
|
32
|
+
"""
|
|
33
|
+
where = WhereClause()
|
|
34
|
+
|
|
35
|
+
# starting_date
|
|
36
|
+
if start_timestamp is not None:
|
|
37
|
+
where = where.and_("created_at", start_timestamp, ">=")
|
|
38
|
+
|
|
39
|
+
# ending_date
|
|
40
|
+
if end_timestamp is not None:
|
|
41
|
+
where = where.and_("created_at", end_timestamp, "<=")
|
|
42
|
+
|
|
43
|
+
where_clause, where_vars = where.build()
|
|
44
|
+
|
|
45
|
+
# Query
|
|
46
|
+
query = dedent(f"""
|
|
47
|
+
SELECT *
|
|
48
|
+
FROM {table}
|
|
49
|
+
{where_clause}
|
|
50
|
+
""")
|
|
51
|
+
|
|
52
|
+
results = utils.query(client, query, where_vars, dict)
|
|
53
|
+
return [desurrealize_session(x) for x in results]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def get_metrics_calculation_starting_date(
|
|
57
|
+
client: Union[BlockingWsSurrealConnection, BlockingHttpSurrealConnection], table: str, get_sessions: Callable
|
|
58
|
+
) -> Optional[date]:
|
|
59
|
+
"""Get the first date for which metrics calculation is needed:
|
|
60
|
+
|
|
61
|
+
1. If there are metrics records, return the date of the first day without a complete metrics record.
|
|
62
|
+
2. If there are no metrics records, return the date of the first recorded session.
|
|
63
|
+
3. If there are no metrics records and no sessions records, return None.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
table (Table): The table to get the starting date for.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
Optional[date]: The starting date for which metrics calculation is needed.
|
|
70
|
+
"""
|
|
71
|
+
query = dedent(f"""
|
|
72
|
+
SELECT * FROM ONLY {table}
|
|
73
|
+
ORDER BY date DESC
|
|
74
|
+
LIMIT 1
|
|
75
|
+
""")
|
|
76
|
+
result = utils.query_one(client, query, {}, dict)
|
|
77
|
+
if result:
|
|
78
|
+
# 1. Return the date of the first day without a complete metrics record
|
|
79
|
+
result_date = result["date"]
|
|
80
|
+
assert isinstance(result_date, datetime)
|
|
81
|
+
result_date = result_date.date()
|
|
82
|
+
|
|
83
|
+
if result.get("completed"):
|
|
84
|
+
return result_date + timedelta(days=1)
|
|
85
|
+
else:
|
|
86
|
+
return result_date
|
|
87
|
+
|
|
88
|
+
# 2. No metrics records. Return the date of the first recorded session
|
|
89
|
+
first_session, _ = get_sessions(
|
|
90
|
+
session_type=SessionType.AGENT, # this is ignored because of component_id=None and deserialize=False
|
|
91
|
+
sort_by="created_at",
|
|
92
|
+
sort_order="asc",
|
|
93
|
+
limit=1,
|
|
94
|
+
component_id=None,
|
|
95
|
+
deserialize=False,
|
|
96
|
+
)
|
|
97
|
+
assert isinstance(first_session, list)
|
|
98
|
+
|
|
99
|
+
first_session_date = first_session[0]["created_at"] if first_session else None
|
|
100
|
+
|
|
101
|
+
# 3. No metrics records and no sessions records. Return None
|
|
102
|
+
if first_session_date is None:
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
# Handle different types for created_at
|
|
106
|
+
if isinstance(first_session_date, datetime):
|
|
107
|
+
return first_session_date.date()
|
|
108
|
+
elif isinstance(first_session_date, int):
|
|
109
|
+
# Assume it's a Unix timestamp
|
|
110
|
+
return datetime.fromtimestamp(first_session_date, tz=timezone.utc).date()
|
|
111
|
+
elif isinstance(first_session_date, str):
|
|
112
|
+
# Try parsing as ISO format
|
|
113
|
+
return datetime.fromisoformat(first_session_date.replace("Z", "+00:00")).date()
|
|
114
|
+
else:
|
|
115
|
+
# If it's already a date object
|
|
116
|
+
if isinstance(first_session_date, date):
|
|
117
|
+
return first_session_date
|
|
118
|
+
raise ValueError(f"Unexpected type for created_at: {type(first_session_date)}")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def bulk_upsert_metrics(
|
|
122
|
+
client: Union[BlockingWsSurrealConnection, BlockingHttpSurrealConnection],
|
|
123
|
+
table: str,
|
|
124
|
+
metrics_records: List[Dict[str, Any]],
|
|
125
|
+
) -> List[Dict[str, Any]]:
|
|
126
|
+
"""Bulk upsert metrics into the database.
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
table (Table): The table to upsert into.
|
|
130
|
+
metrics_records (List[Dict[str, Any]]): The list of metrics records to upsert.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
list[dict]: The upserted metrics records.
|
|
134
|
+
"""
|
|
135
|
+
if not metrics_records:
|
|
136
|
+
return []
|
|
137
|
+
|
|
138
|
+
metrics_records = [surrealize_dates(x) for x in metrics_records]
|
|
139
|
+
|
|
140
|
+
try:
|
|
141
|
+
results = []
|
|
142
|
+
from agno.utils.log import log_debug
|
|
143
|
+
|
|
144
|
+
for metric in metrics_records:
|
|
145
|
+
log_debug(f"Upserting metric: {metric}") # Add this
|
|
146
|
+
result = utils.query_one(
|
|
147
|
+
client,
|
|
148
|
+
"UPSERT $record CONTENT $content",
|
|
149
|
+
{"record": RecordID(table, metric["id"]), "content": metric},
|
|
150
|
+
dict,
|
|
151
|
+
)
|
|
152
|
+
if result:
|
|
153
|
+
results.append(result)
|
|
154
|
+
return results
|
|
155
|
+
|
|
156
|
+
except Exception as e:
|
|
157
|
+
import traceback
|
|
158
|
+
|
|
159
|
+
log_error(traceback.format_exc())
|
|
160
|
+
log_error(f"Error upserting metrics: {e}")
|
|
161
|
+
|
|
162
|
+
return []
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def fetch_all_sessions_data(
|
|
166
|
+
sessions: List[Dict[str, Any]], dates_to_process: list[date], start_timestamp: int
|
|
167
|
+
) -> Optional[dict]:
|
|
168
|
+
"""Return all session data for the given dates, for all session types.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
sessions (List[Dict[str, Any]]): The sessions to process.
|
|
172
|
+
dates_to_process (list[date]): The dates to fetch session data for.
|
|
173
|
+
start_timestamp (int): The start timestamp (fallback if created_at is missing).
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
dict: A dictionary with dates as keys and session data as values, for all session types.
|
|
177
|
+
|
|
178
|
+
Example:
|
|
179
|
+
{
|
|
180
|
+
"2000-01-01": {
|
|
181
|
+
"agent": [<session1>, <session2>, ...],
|
|
182
|
+
"team": [...],
|
|
183
|
+
"workflow": [...],
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
"""
|
|
187
|
+
if not dates_to_process:
|
|
188
|
+
return None
|
|
189
|
+
|
|
190
|
+
all_sessions_data: Dict[str, Dict[str, List[Dict[str, Any]]]] = {
|
|
191
|
+
date_to_process.isoformat(): {"agent": [], "team": [], "workflow": []} for date_to_process in dates_to_process
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
for session in sessions:
|
|
195
|
+
created_at = session.get("created_at", start_timestamp)
|
|
196
|
+
|
|
197
|
+
# Handle different types for created_at
|
|
198
|
+
if isinstance(created_at, datetime):
|
|
199
|
+
session_date = created_at.date().isoformat()
|
|
200
|
+
elif isinstance(created_at, int):
|
|
201
|
+
session_date = datetime.fromtimestamp(created_at, tz=timezone.utc).date().isoformat()
|
|
202
|
+
elif isinstance(created_at, date):
|
|
203
|
+
session_date = created_at.isoformat()
|
|
204
|
+
else:
|
|
205
|
+
# Fallback to start_timestamp if type is unexpected
|
|
206
|
+
session_date = datetime.fromtimestamp(start_timestamp, tz=timezone.utc).date().isoformat()
|
|
207
|
+
|
|
208
|
+
if session_date in all_sessions_data:
|
|
209
|
+
session_type = session.get("session_type", "agent") # Default to agent if missing
|
|
210
|
+
all_sessions_data[session_date][session_type].append(session)
|
|
211
|
+
|
|
212
|
+
return all_sessions_data
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def calculate_date_metrics(date_to_process: date, sessions_data: dict) -> dict:
|
|
216
|
+
"""Calculate metrics for the given single date.
|
|
217
|
+
|
|
218
|
+
Args:
|
|
219
|
+
date_to_process (date): The date to calculate metrics for.
|
|
220
|
+
sessions_data (dict): The sessions data to calculate metrics for.
|
|
221
|
+
|
|
222
|
+
Returns:
|
|
223
|
+
dict: The calculated metrics.
|
|
224
|
+
"""
|
|
225
|
+
metrics = {
|
|
226
|
+
"users_count": 0,
|
|
227
|
+
"agent_sessions_count": 0,
|
|
228
|
+
"team_sessions_count": 0,
|
|
229
|
+
"workflow_sessions_count": 0,
|
|
230
|
+
"agent_runs_count": 0,
|
|
231
|
+
"team_runs_count": 0,
|
|
232
|
+
"workflow_runs_count": 0,
|
|
233
|
+
}
|
|
234
|
+
token_metrics = {
|
|
235
|
+
"input_tokens": 0,
|
|
236
|
+
"output_tokens": 0,
|
|
237
|
+
"total_tokens": 0,
|
|
238
|
+
"audio_total_tokens": 0,
|
|
239
|
+
"audio_input_tokens": 0,
|
|
240
|
+
"audio_output_tokens": 0,
|
|
241
|
+
"cache_read_tokens": 0,
|
|
242
|
+
"cache_write_tokens": 0,
|
|
243
|
+
"reasoning_tokens": 0,
|
|
244
|
+
}
|
|
245
|
+
model_counts: Dict[str, int] = {}
|
|
246
|
+
|
|
247
|
+
session_types = [
|
|
248
|
+
("agent", "agent_sessions_count", "agent_runs_count"),
|
|
249
|
+
("team", "team_sessions_count", "team_runs_count"),
|
|
250
|
+
("workflow", "workflow_sessions_count", "workflow_runs_count"),
|
|
251
|
+
]
|
|
252
|
+
all_user_ids = set()
|
|
253
|
+
|
|
254
|
+
for session_type, sessions_count_key, runs_count_key in session_types:
|
|
255
|
+
sessions = sessions_data.get(session_type, [])
|
|
256
|
+
metrics[sessions_count_key] = len(sessions)
|
|
257
|
+
|
|
258
|
+
for session in sessions:
|
|
259
|
+
if session.get("user_id"):
|
|
260
|
+
all_user_ids.add(session["user_id"])
|
|
261
|
+
metrics[runs_count_key] += len(session.get("runs", []))
|
|
262
|
+
if runs := session.get("runs", []):
|
|
263
|
+
for run in runs:
|
|
264
|
+
if model_id := run.get("model"):
|
|
265
|
+
model_provider = run.get("model_provider", "")
|
|
266
|
+
model_counts[f"{model_id}:{model_provider}"] = (
|
|
267
|
+
model_counts.get(f"{model_id}:{model_provider}", 0) + 1
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
session_metrics = session.get("session_data", {}).get("session_metrics", {})
|
|
271
|
+
for field in token_metrics:
|
|
272
|
+
token_metrics[field] += session_metrics.get(field, 0)
|
|
273
|
+
|
|
274
|
+
model_metrics = []
|
|
275
|
+
for model, count in model_counts.items():
|
|
276
|
+
model_id, model_provider = model.split(":")
|
|
277
|
+
model_metrics.append({"model_id": model_id, "model_provider": model_provider, "count": count})
|
|
278
|
+
|
|
279
|
+
metrics["users_count"] = len(all_user_ids)
|
|
280
|
+
current_time = datetime.now(timezone.utc)
|
|
281
|
+
|
|
282
|
+
return {
|
|
283
|
+
"id": date_to_process.isoformat(), # Changed: Use date as ID (e.g., "2025-10-16")
|
|
284
|
+
"date": current_time.replace(hour=0, minute=0, second=0, microsecond=0), # Date at midnight UTC
|
|
285
|
+
"completed": date_to_process < datetime.now(timezone.utc).date(),
|
|
286
|
+
"token_metrics": token_metrics,
|
|
287
|
+
"model_metrics": model_metrics,
|
|
288
|
+
"created_at": current_time,
|
|
289
|
+
"updated_at": current_time,
|
|
290
|
+
"aggregation_period": "daily",
|
|
291
|
+
**metrics,
|
|
292
|
+
}
|