agno 2.3.4__py3-none-any.whl → 2.3.6__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 +184 -45
- agno/culture/manager.py +2 -2
- agno/db/base.py +330 -8
- agno/db/dynamo/dynamo.py +722 -2
- agno/db/dynamo/schemas.py +127 -0
- agno/db/firestore/firestore.py +573 -1
- agno/db/firestore/schemas.py +40 -0
- agno/db/gcs_json/gcs_json_db.py +446 -1
- agno/db/in_memory/in_memory_db.py +143 -1
- agno/db/json/json_db.py +438 -1
- agno/db/mongo/async_mongo.py +522 -0
- agno/db/mongo/mongo.py +523 -1
- agno/db/mongo/schemas.py +29 -0
- agno/db/mysql/mysql.py +536 -3
- agno/db/mysql/schemas.py +38 -0
- agno/db/postgres/async_postgres.py +553 -15
- agno/db/postgres/postgres.py +544 -5
- agno/db/postgres/schemas.py +38 -0
- agno/db/redis/redis.py +468 -1
- agno/db/redis/schemas.py +32 -0
- agno/db/singlestore/schemas.py +38 -0
- agno/db/singlestore/singlestore.py +523 -1
- agno/db/sqlite/async_sqlite.py +549 -10
- agno/db/sqlite/schemas.py +38 -0
- agno/db/sqlite/sqlite.py +540 -9
- agno/db/sqlite/utils.py +6 -8
- agno/db/surrealdb/models.py +25 -0
- agno/db/surrealdb/surrealdb.py +548 -1
- agno/eval/accuracy.py +18 -8
- agno/eval/performance.py +10 -4
- agno/eval/reliability.py +22 -13
- agno/exceptions.py +11 -0
- agno/hooks/__init__.py +3 -0
- agno/hooks/decorator.py +164 -0
- agno/integrations/discord/client.py +1 -1
- agno/knowledge/chunking/semantic.py +2 -2
- agno/models/aimlapi/aimlapi.py +2 -3
- agno/models/anthropic/claude.py +18 -13
- agno/models/aws/bedrock.py +3 -4
- agno/models/aws/claude.py +5 -1
- agno/models/azure/ai_foundry.py +2 -2
- agno/models/azure/openai_chat.py +8 -0
- agno/models/cerebras/cerebras.py +62 -11
- agno/models/cerebras/cerebras_openai.py +2 -3
- agno/models/cohere/chat.py +1 -5
- agno/models/cometapi/cometapi.py +2 -3
- agno/models/dashscope/dashscope.py +2 -3
- agno/models/deepinfra/deepinfra.py +2 -3
- agno/models/deepseek/deepseek.py +2 -3
- agno/models/fireworks/fireworks.py +2 -3
- agno/models/google/gemini.py +9 -7
- agno/models/groq/groq.py +2 -3
- agno/models/huggingface/huggingface.py +1 -5
- agno/models/ibm/watsonx.py +1 -5
- agno/models/internlm/internlm.py +2 -3
- agno/models/langdb/langdb.py +6 -4
- agno/models/litellm/chat.py +2 -2
- agno/models/litellm/litellm_openai.py +2 -3
- agno/models/meta/llama.py +1 -5
- agno/models/meta/llama_openai.py +4 -5
- agno/models/mistral/mistral.py +1 -5
- agno/models/nebius/nebius.py +2 -3
- agno/models/nvidia/nvidia.py +4 -5
- agno/models/openai/chat.py +14 -3
- agno/models/openai/responses.py +14 -3
- agno/models/openrouter/openrouter.py +4 -5
- agno/models/perplexity/perplexity.py +2 -3
- agno/models/portkey/portkey.py +7 -6
- agno/models/requesty/requesty.py +4 -5
- agno/models/response.py +2 -1
- agno/models/sambanova/sambanova.py +4 -5
- agno/models/siliconflow/siliconflow.py +3 -4
- agno/models/together/together.py +4 -5
- agno/models/vercel/v0.py +4 -5
- agno/models/vllm/vllm.py +19 -14
- agno/models/xai/xai.py +4 -5
- agno/os/app.py +104 -0
- agno/os/config.py +13 -0
- agno/os/interfaces/whatsapp/router.py +0 -1
- agno/os/interfaces/whatsapp/security.py +3 -1
- agno/os/mcp.py +1 -0
- agno/os/router.py +31 -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/schema.py +12 -2
- agno/os/utils.py +57 -0
- agno/run/agent.py +1 -0
- agno/run/base.py +17 -0
- agno/run/team.py +4 -0
- agno/table.py +10 -0
- agno/team/team.py +221 -69
- agno/tools/function.py +10 -8
- agno/tools/google_drive.py +4 -3
- agno/tools/nano_banana.py +1 -1
- agno/tools/spotify.py +922 -0
- 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 +6 -6
- agno/utils/hooks.py +56 -1
- agno/utils/mcp.py +1 -1
- agno/vectordb/qdrant/qdrant.py +22 -22
- agno/workflow/condition.py +8 -0
- agno/workflow/loop.py +8 -0
- agno/workflow/parallel.py +8 -0
- agno/workflow/router.py +8 -0
- agno/workflow/step.py +20 -0
- agno/workflow/steps.py +8 -0
- agno/workflow/workflow.py +88 -19
- {agno-2.3.4.dist-info → agno-2.3.6.dist-info}/METADATA +38 -33
- {agno-2.3.4.dist-info → agno-2.3.6.dist-info}/RECORD +116 -105
- {agno-2.3.4.dist-info → agno-2.3.6.dist-info}/WHEEL +0 -0
- {agno-2.3.4.dist-info → agno-2.3.6.dist-info}/licenses/LICENSE +0 -0
- {agno-2.3.4.dist-info → agno-2.3.6.dist-info}/top_level.txt +0 -0
agno/db/json/json_db.py
CHANGED
|
@@ -3,9 +3,12 @@ import os
|
|
|
3
3
|
import time
|
|
4
4
|
from datetime import date, datetime, timedelta, timezone
|
|
5
5
|
from pathlib import Path
|
|
6
|
-
from typing import Any, Dict, List, Optional, Tuple, Union
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
|
|
7
7
|
from uuid import uuid4
|
|
8
8
|
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from agno.tracing.schemas import Span, Trace
|
|
11
|
+
|
|
9
12
|
from agno.db.base import BaseDb, SessionType
|
|
10
13
|
from agno.db.json.utils import (
|
|
11
14
|
apply_sorting,
|
|
@@ -34,6 +37,8 @@ class JsonDb(BaseDb):
|
|
|
34
37
|
metrics_table: Optional[str] = None,
|
|
35
38
|
eval_table: Optional[str] = None,
|
|
36
39
|
knowledge_table: Optional[str] = None,
|
|
40
|
+
traces_table: Optional[str] = None,
|
|
41
|
+
spans_table: Optional[str] = None,
|
|
37
42
|
id: Optional[str] = None,
|
|
38
43
|
):
|
|
39
44
|
"""
|
|
@@ -47,6 +52,8 @@ class JsonDb(BaseDb):
|
|
|
47
52
|
metrics_table (Optional[str]): Name of the JSON file to store metrics.
|
|
48
53
|
eval_table (Optional[str]): Name of the JSON file to store evaluation runs.
|
|
49
54
|
knowledge_table (Optional[str]): Name of the JSON file to store knowledge content.
|
|
55
|
+
traces_table (Optional[str]): Name of the JSON file to store run traces.
|
|
56
|
+
spans_table (Optional[str]): Name of the JSON file to store span events.
|
|
50
57
|
id (Optional[str]): ID of the database.
|
|
51
58
|
"""
|
|
52
59
|
if id is None:
|
|
@@ -61,6 +68,8 @@ class JsonDb(BaseDb):
|
|
|
61
68
|
metrics_table=metrics_table,
|
|
62
69
|
eval_table=eval_table,
|
|
63
70
|
knowledge_table=knowledge_table,
|
|
71
|
+
traces_table=traces_table,
|
|
72
|
+
spans_table=spans_table,
|
|
64
73
|
)
|
|
65
74
|
|
|
66
75
|
# Create the directory where the JSON files will be stored, if it doesn't exist
|
|
@@ -1338,3 +1347,431 @@ class JsonDb(BaseDb):
|
|
|
1338
1347
|
except Exception as e:
|
|
1339
1348
|
log_error(f"Error upserting cultural knowledge: {e}")
|
|
1340
1349
|
raise e
|
|
1350
|
+
|
|
1351
|
+
# --- Traces ---
|
|
1352
|
+
def create_trace(self, trace: "Trace") -> None:
|
|
1353
|
+
"""Create a single trace record in the database.
|
|
1354
|
+
|
|
1355
|
+
Args:
|
|
1356
|
+
trace: The Trace object to store (one per trace_id).
|
|
1357
|
+
"""
|
|
1358
|
+
try:
|
|
1359
|
+
traces = self._read_json_file(self.trace_table_name, create_table_if_not_found=True)
|
|
1360
|
+
|
|
1361
|
+
# Check if trace exists
|
|
1362
|
+
existing_idx = None
|
|
1363
|
+
for i, existing in enumerate(traces):
|
|
1364
|
+
if existing.get("trace_id") == trace.trace_id:
|
|
1365
|
+
existing_idx = i
|
|
1366
|
+
break
|
|
1367
|
+
|
|
1368
|
+
if existing_idx is not None:
|
|
1369
|
+
existing = traces[existing_idx]
|
|
1370
|
+
|
|
1371
|
+
# workflow (level 3) > team (level 2) > agent (level 1) > child/unknown (level 0)
|
|
1372
|
+
def get_component_level(workflow_id, team_id, agent_id, name):
|
|
1373
|
+
is_root_name = ".run" in name or ".arun" in name
|
|
1374
|
+
if not is_root_name:
|
|
1375
|
+
return 0
|
|
1376
|
+
elif workflow_id:
|
|
1377
|
+
return 3
|
|
1378
|
+
elif team_id:
|
|
1379
|
+
return 2
|
|
1380
|
+
elif agent_id:
|
|
1381
|
+
return 1
|
|
1382
|
+
else:
|
|
1383
|
+
return 0
|
|
1384
|
+
|
|
1385
|
+
existing_level = get_component_level(
|
|
1386
|
+
existing.get("workflow_id"),
|
|
1387
|
+
existing.get("team_id"),
|
|
1388
|
+
existing.get("agent_id"),
|
|
1389
|
+
existing.get("name", ""),
|
|
1390
|
+
)
|
|
1391
|
+
new_level = get_component_level(trace.workflow_id, trace.team_id, trace.agent_id, trace.name)
|
|
1392
|
+
should_update_name = new_level > existing_level
|
|
1393
|
+
|
|
1394
|
+
# Parse existing start_time to calculate correct duration
|
|
1395
|
+
existing_start_time_str = existing.get("start_time")
|
|
1396
|
+
if isinstance(existing_start_time_str, str):
|
|
1397
|
+
existing_start_time = datetime.fromisoformat(existing_start_time_str.replace("Z", "+00:00"))
|
|
1398
|
+
else:
|
|
1399
|
+
existing_start_time = trace.start_time
|
|
1400
|
+
|
|
1401
|
+
recalculated_duration_ms = int((trace.end_time - existing_start_time).total_seconds() * 1000)
|
|
1402
|
+
|
|
1403
|
+
# Update existing trace
|
|
1404
|
+
existing["end_time"] = trace.end_time.isoformat()
|
|
1405
|
+
existing["duration_ms"] = recalculated_duration_ms
|
|
1406
|
+
existing["status"] = trace.status
|
|
1407
|
+
if should_update_name:
|
|
1408
|
+
existing["name"] = trace.name
|
|
1409
|
+
|
|
1410
|
+
# Update context fields only if new value is not None
|
|
1411
|
+
if trace.run_id is not None:
|
|
1412
|
+
existing["run_id"] = trace.run_id
|
|
1413
|
+
if trace.session_id is not None:
|
|
1414
|
+
existing["session_id"] = trace.session_id
|
|
1415
|
+
if trace.user_id is not None:
|
|
1416
|
+
existing["user_id"] = trace.user_id
|
|
1417
|
+
if trace.agent_id is not None:
|
|
1418
|
+
existing["agent_id"] = trace.agent_id
|
|
1419
|
+
if trace.team_id is not None:
|
|
1420
|
+
existing["team_id"] = trace.team_id
|
|
1421
|
+
if trace.workflow_id is not None:
|
|
1422
|
+
existing["workflow_id"] = trace.workflow_id
|
|
1423
|
+
|
|
1424
|
+
traces[existing_idx] = existing
|
|
1425
|
+
else:
|
|
1426
|
+
# Add new trace
|
|
1427
|
+
trace_dict = trace.to_dict()
|
|
1428
|
+
trace_dict.pop("total_spans", None)
|
|
1429
|
+
trace_dict.pop("error_count", None)
|
|
1430
|
+
traces.append(trace_dict)
|
|
1431
|
+
|
|
1432
|
+
self._write_json_file(self.trace_table_name, traces)
|
|
1433
|
+
|
|
1434
|
+
except Exception as e:
|
|
1435
|
+
log_error(f"Error creating trace: {e}")
|
|
1436
|
+
|
|
1437
|
+
def get_trace(
|
|
1438
|
+
self,
|
|
1439
|
+
trace_id: Optional[str] = None,
|
|
1440
|
+
run_id: Optional[str] = None,
|
|
1441
|
+
):
|
|
1442
|
+
"""Get a single trace by trace_id or other filters.
|
|
1443
|
+
|
|
1444
|
+
Args:
|
|
1445
|
+
trace_id: The unique trace identifier.
|
|
1446
|
+
run_id: Filter by run ID (returns first match).
|
|
1447
|
+
|
|
1448
|
+
Returns:
|
|
1449
|
+
Optional[Trace]: The trace if found, None otherwise.
|
|
1450
|
+
"""
|
|
1451
|
+
try:
|
|
1452
|
+
from agno.tracing.schemas import Trace
|
|
1453
|
+
|
|
1454
|
+
traces = self._read_json_file(self.trace_table_name, create_table_if_not_found=False)
|
|
1455
|
+
if not traces:
|
|
1456
|
+
return None
|
|
1457
|
+
|
|
1458
|
+
# Get spans for calculating total_spans and error_count
|
|
1459
|
+
spans = self._read_json_file(self.span_table_name, create_table_if_not_found=False)
|
|
1460
|
+
|
|
1461
|
+
# Filter traces
|
|
1462
|
+
filtered = []
|
|
1463
|
+
for t in traces:
|
|
1464
|
+
if trace_id and t.get("trace_id") == trace_id:
|
|
1465
|
+
filtered.append(t)
|
|
1466
|
+
break
|
|
1467
|
+
elif run_id and t.get("run_id") == run_id:
|
|
1468
|
+
filtered.append(t)
|
|
1469
|
+
|
|
1470
|
+
if not filtered:
|
|
1471
|
+
return None
|
|
1472
|
+
|
|
1473
|
+
# Sort by start_time desc and get first
|
|
1474
|
+
filtered.sort(key=lambda x: x.get("start_time", ""), reverse=True)
|
|
1475
|
+
trace_data = filtered[0]
|
|
1476
|
+
|
|
1477
|
+
# Calculate total_spans and error_count
|
|
1478
|
+
trace_spans = [s for s in spans if s.get("trace_id") == trace_data.get("trace_id")]
|
|
1479
|
+
trace_data["total_spans"] = len(trace_spans)
|
|
1480
|
+
trace_data["error_count"] = sum(1 for s in trace_spans if s.get("status_code") == "ERROR")
|
|
1481
|
+
|
|
1482
|
+
return Trace.from_dict(trace_data)
|
|
1483
|
+
|
|
1484
|
+
except Exception as e:
|
|
1485
|
+
log_error(f"Error getting trace: {e}")
|
|
1486
|
+
return None
|
|
1487
|
+
|
|
1488
|
+
def get_traces(
|
|
1489
|
+
self,
|
|
1490
|
+
run_id: Optional[str] = None,
|
|
1491
|
+
session_id: Optional[str] = None,
|
|
1492
|
+
user_id: Optional[str] = None,
|
|
1493
|
+
agent_id: Optional[str] = None,
|
|
1494
|
+
team_id: Optional[str] = None,
|
|
1495
|
+
workflow_id: Optional[str] = None,
|
|
1496
|
+
status: Optional[str] = None,
|
|
1497
|
+
start_time: Optional[datetime] = None,
|
|
1498
|
+
end_time: Optional[datetime] = None,
|
|
1499
|
+
limit: Optional[int] = 20,
|
|
1500
|
+
page: Optional[int] = 1,
|
|
1501
|
+
) -> tuple[List, int]:
|
|
1502
|
+
"""Get traces matching the provided filters with pagination.
|
|
1503
|
+
|
|
1504
|
+
Args:
|
|
1505
|
+
run_id: Filter by run ID.
|
|
1506
|
+
session_id: Filter by session ID.
|
|
1507
|
+
user_id: Filter by user ID.
|
|
1508
|
+
agent_id: Filter by agent ID.
|
|
1509
|
+
team_id: Filter by team ID.
|
|
1510
|
+
workflow_id: Filter by workflow ID.
|
|
1511
|
+
status: Filter by status (OK, ERROR, UNSET).
|
|
1512
|
+
start_time: Filter traces starting after this datetime.
|
|
1513
|
+
end_time: Filter traces ending before this datetime.
|
|
1514
|
+
limit: Maximum number of traces to return per page.
|
|
1515
|
+
page: Page number (1-indexed).
|
|
1516
|
+
|
|
1517
|
+
Returns:
|
|
1518
|
+
tuple[List[Trace], int]: Tuple of (list of matching traces, total count).
|
|
1519
|
+
"""
|
|
1520
|
+
try:
|
|
1521
|
+
from agno.tracing.schemas import Trace
|
|
1522
|
+
|
|
1523
|
+
traces = self._read_json_file(self.trace_table_name, create_table_if_not_found=False)
|
|
1524
|
+
if not traces:
|
|
1525
|
+
return [], 0
|
|
1526
|
+
|
|
1527
|
+
# Get spans for calculating total_spans and error_count
|
|
1528
|
+
spans = self._read_json_file(self.span_table_name, create_table_if_not_found=False)
|
|
1529
|
+
|
|
1530
|
+
# Apply filters
|
|
1531
|
+
filtered = []
|
|
1532
|
+
for t in traces:
|
|
1533
|
+
if run_id and t.get("run_id") != run_id:
|
|
1534
|
+
continue
|
|
1535
|
+
if session_id and t.get("session_id") != session_id:
|
|
1536
|
+
continue
|
|
1537
|
+
if user_id and t.get("user_id") != user_id:
|
|
1538
|
+
continue
|
|
1539
|
+
if agent_id and t.get("agent_id") != agent_id:
|
|
1540
|
+
continue
|
|
1541
|
+
if team_id and t.get("team_id") != team_id:
|
|
1542
|
+
continue
|
|
1543
|
+
if workflow_id and t.get("workflow_id") != workflow_id:
|
|
1544
|
+
continue
|
|
1545
|
+
if status and t.get("status") != status:
|
|
1546
|
+
continue
|
|
1547
|
+
if start_time:
|
|
1548
|
+
trace_start = t.get("start_time", "")
|
|
1549
|
+
if trace_start < start_time.isoformat():
|
|
1550
|
+
continue
|
|
1551
|
+
if end_time:
|
|
1552
|
+
trace_end = t.get("end_time", "")
|
|
1553
|
+
if trace_end > end_time.isoformat():
|
|
1554
|
+
continue
|
|
1555
|
+
filtered.append(t)
|
|
1556
|
+
|
|
1557
|
+
total_count = len(filtered)
|
|
1558
|
+
|
|
1559
|
+
# Sort by start_time desc
|
|
1560
|
+
filtered.sort(key=lambda x: x.get("start_time", ""), reverse=True)
|
|
1561
|
+
|
|
1562
|
+
# Apply pagination
|
|
1563
|
+
if limit and page:
|
|
1564
|
+
start_idx = (page - 1) * limit
|
|
1565
|
+
filtered = filtered[start_idx : start_idx + limit]
|
|
1566
|
+
|
|
1567
|
+
# Add total_spans and error_count to each trace
|
|
1568
|
+
result_traces = []
|
|
1569
|
+
for t in filtered:
|
|
1570
|
+
trace_spans = [s for s in spans if s.get("trace_id") == t.get("trace_id")]
|
|
1571
|
+
t["total_spans"] = len(trace_spans)
|
|
1572
|
+
t["error_count"] = sum(1 for s in trace_spans if s.get("status_code") == "ERROR")
|
|
1573
|
+
result_traces.append(Trace.from_dict(t))
|
|
1574
|
+
|
|
1575
|
+
return result_traces, total_count
|
|
1576
|
+
|
|
1577
|
+
except Exception as e:
|
|
1578
|
+
log_error(f"Error getting traces: {e}")
|
|
1579
|
+
return [], 0
|
|
1580
|
+
|
|
1581
|
+
def get_trace_stats(
|
|
1582
|
+
self,
|
|
1583
|
+
user_id: Optional[str] = None,
|
|
1584
|
+
agent_id: Optional[str] = None,
|
|
1585
|
+
team_id: Optional[str] = None,
|
|
1586
|
+
workflow_id: Optional[str] = None,
|
|
1587
|
+
start_time: Optional[datetime] = None,
|
|
1588
|
+
end_time: Optional[datetime] = None,
|
|
1589
|
+
limit: Optional[int] = 20,
|
|
1590
|
+
page: Optional[int] = 1,
|
|
1591
|
+
) -> tuple[List[Dict[str, Any]], int]:
|
|
1592
|
+
"""Get trace statistics grouped by session.
|
|
1593
|
+
|
|
1594
|
+
Args:
|
|
1595
|
+
user_id: Filter by user ID.
|
|
1596
|
+
agent_id: Filter by agent ID.
|
|
1597
|
+
team_id: Filter by team ID.
|
|
1598
|
+
workflow_id: Filter by workflow ID.
|
|
1599
|
+
start_time: Filter sessions with traces created after this datetime.
|
|
1600
|
+
end_time: Filter sessions with traces created before this datetime.
|
|
1601
|
+
limit: Maximum number of sessions to return per page.
|
|
1602
|
+
page: Page number (1-indexed).
|
|
1603
|
+
|
|
1604
|
+
Returns:
|
|
1605
|
+
tuple[List[Dict], int]: Tuple of (list of session stats dicts, total count).
|
|
1606
|
+
"""
|
|
1607
|
+
try:
|
|
1608
|
+
traces = self._read_json_file(self.trace_table_name, create_table_if_not_found=False)
|
|
1609
|
+
if not traces:
|
|
1610
|
+
return [], 0
|
|
1611
|
+
|
|
1612
|
+
# Group by session_id
|
|
1613
|
+
session_stats: Dict[str, Dict[str, Any]] = {}
|
|
1614
|
+
|
|
1615
|
+
for t in traces:
|
|
1616
|
+
session_id = t.get("session_id")
|
|
1617
|
+
if not session_id:
|
|
1618
|
+
continue
|
|
1619
|
+
|
|
1620
|
+
# Apply filters
|
|
1621
|
+
if user_id and t.get("user_id") != user_id:
|
|
1622
|
+
continue
|
|
1623
|
+
if agent_id and t.get("agent_id") != agent_id:
|
|
1624
|
+
continue
|
|
1625
|
+
if team_id and t.get("team_id") != team_id:
|
|
1626
|
+
continue
|
|
1627
|
+
if workflow_id and t.get("workflow_id") != workflow_id:
|
|
1628
|
+
continue
|
|
1629
|
+
|
|
1630
|
+
created_at = t.get("created_at", "")
|
|
1631
|
+
if start_time and created_at < start_time.isoformat():
|
|
1632
|
+
continue
|
|
1633
|
+
if end_time and created_at > end_time.isoformat():
|
|
1634
|
+
continue
|
|
1635
|
+
|
|
1636
|
+
if session_id not in session_stats:
|
|
1637
|
+
session_stats[session_id] = {
|
|
1638
|
+
"session_id": session_id,
|
|
1639
|
+
"user_id": t.get("user_id"),
|
|
1640
|
+
"agent_id": t.get("agent_id"),
|
|
1641
|
+
"team_id": t.get("team_id"),
|
|
1642
|
+
"workflow_id": t.get("workflow_id"),
|
|
1643
|
+
"total_traces": 0,
|
|
1644
|
+
"first_trace_at": created_at,
|
|
1645
|
+
"last_trace_at": created_at,
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
session_stats[session_id]["total_traces"] += 1
|
|
1649
|
+
if created_at < session_stats[session_id]["first_trace_at"]:
|
|
1650
|
+
session_stats[session_id]["first_trace_at"] = created_at
|
|
1651
|
+
if created_at > session_stats[session_id]["last_trace_at"]:
|
|
1652
|
+
session_stats[session_id]["last_trace_at"] = created_at
|
|
1653
|
+
|
|
1654
|
+
stats_list = list(session_stats.values())
|
|
1655
|
+
total_count = len(stats_list)
|
|
1656
|
+
|
|
1657
|
+
# Sort by last_trace_at desc
|
|
1658
|
+
stats_list.sort(key=lambda x: x.get("last_trace_at", ""), reverse=True)
|
|
1659
|
+
|
|
1660
|
+
# Apply pagination
|
|
1661
|
+
if limit and page:
|
|
1662
|
+
start_idx = (page - 1) * limit
|
|
1663
|
+
stats_list = stats_list[start_idx : start_idx + limit]
|
|
1664
|
+
|
|
1665
|
+
# Convert ISO strings to datetime objects
|
|
1666
|
+
for stat in stats_list:
|
|
1667
|
+
first_at = stat.get("first_trace_at", "")
|
|
1668
|
+
last_at = stat.get("last_trace_at", "")
|
|
1669
|
+
if first_at:
|
|
1670
|
+
stat["first_trace_at"] = datetime.fromisoformat(first_at.replace("Z", "+00:00"))
|
|
1671
|
+
if last_at:
|
|
1672
|
+
stat["last_trace_at"] = datetime.fromisoformat(last_at.replace("Z", "+00:00"))
|
|
1673
|
+
|
|
1674
|
+
return stats_list, total_count
|
|
1675
|
+
|
|
1676
|
+
except Exception as e:
|
|
1677
|
+
log_error(f"Error getting trace stats: {e}")
|
|
1678
|
+
return [], 0
|
|
1679
|
+
|
|
1680
|
+
# --- Spans ---
|
|
1681
|
+
def create_span(self, span: "Span") -> None:
|
|
1682
|
+
"""Create a single span in the database.
|
|
1683
|
+
|
|
1684
|
+
Args:
|
|
1685
|
+
span: The Span object to store.
|
|
1686
|
+
"""
|
|
1687
|
+
try:
|
|
1688
|
+
spans = self._read_json_file(self.span_table_name, create_table_if_not_found=True)
|
|
1689
|
+
spans.append(span.to_dict())
|
|
1690
|
+
self._write_json_file(self.span_table_name, spans)
|
|
1691
|
+
|
|
1692
|
+
except Exception as e:
|
|
1693
|
+
log_error(f"Error creating span: {e}")
|
|
1694
|
+
|
|
1695
|
+
def create_spans(self, spans: List) -> None:
|
|
1696
|
+
"""Create multiple spans in the database as a batch.
|
|
1697
|
+
|
|
1698
|
+
Args:
|
|
1699
|
+
spans: List of Span objects to store.
|
|
1700
|
+
"""
|
|
1701
|
+
if not spans:
|
|
1702
|
+
return
|
|
1703
|
+
|
|
1704
|
+
try:
|
|
1705
|
+
existing_spans = self._read_json_file(self.span_table_name, create_table_if_not_found=True)
|
|
1706
|
+
for span in spans:
|
|
1707
|
+
existing_spans.append(span.to_dict())
|
|
1708
|
+
self._write_json_file(self.span_table_name, existing_spans)
|
|
1709
|
+
|
|
1710
|
+
except Exception as e:
|
|
1711
|
+
log_error(f"Error creating spans batch: {e}")
|
|
1712
|
+
|
|
1713
|
+
def get_span(self, span_id: str):
|
|
1714
|
+
"""Get a single span by its span_id.
|
|
1715
|
+
|
|
1716
|
+
Args:
|
|
1717
|
+
span_id: The unique span identifier.
|
|
1718
|
+
|
|
1719
|
+
Returns:
|
|
1720
|
+
Optional[Span]: The span if found, None otherwise.
|
|
1721
|
+
"""
|
|
1722
|
+
try:
|
|
1723
|
+
from agno.tracing.schemas import Span
|
|
1724
|
+
|
|
1725
|
+
spans = self._read_json_file(self.span_table_name, create_table_if_not_found=False)
|
|
1726
|
+
|
|
1727
|
+
for s in spans:
|
|
1728
|
+
if s.get("span_id") == span_id:
|
|
1729
|
+
return Span.from_dict(s)
|
|
1730
|
+
|
|
1731
|
+
return None
|
|
1732
|
+
|
|
1733
|
+
except Exception as e:
|
|
1734
|
+
log_error(f"Error getting span: {e}")
|
|
1735
|
+
return None
|
|
1736
|
+
|
|
1737
|
+
def get_spans(
|
|
1738
|
+
self,
|
|
1739
|
+
trace_id: Optional[str] = None,
|
|
1740
|
+
parent_span_id: Optional[str] = None,
|
|
1741
|
+
limit: Optional[int] = 1000,
|
|
1742
|
+
) -> List:
|
|
1743
|
+
"""Get spans matching the provided filters.
|
|
1744
|
+
|
|
1745
|
+
Args:
|
|
1746
|
+
trace_id: Filter by trace ID.
|
|
1747
|
+
parent_span_id: Filter by parent span ID.
|
|
1748
|
+
limit: Maximum number of spans to return.
|
|
1749
|
+
|
|
1750
|
+
Returns:
|
|
1751
|
+
List[Span]: List of matching spans.
|
|
1752
|
+
"""
|
|
1753
|
+
try:
|
|
1754
|
+
from agno.tracing.schemas import Span
|
|
1755
|
+
|
|
1756
|
+
spans = self._read_json_file(self.span_table_name, create_table_if_not_found=False)
|
|
1757
|
+
if not spans:
|
|
1758
|
+
return []
|
|
1759
|
+
|
|
1760
|
+
# Apply filters
|
|
1761
|
+
filtered = []
|
|
1762
|
+
for s in spans:
|
|
1763
|
+
if trace_id and s.get("trace_id") != trace_id:
|
|
1764
|
+
continue
|
|
1765
|
+
if parent_span_id and s.get("parent_span_id") != parent_span_id:
|
|
1766
|
+
continue
|
|
1767
|
+
filtered.append(s)
|
|
1768
|
+
|
|
1769
|
+
# Apply limit
|
|
1770
|
+
if limit:
|
|
1771
|
+
filtered = filtered[:limit]
|
|
1772
|
+
|
|
1773
|
+
return [Span.from_dict(s) for s in filtered]
|
|
1774
|
+
|
|
1775
|
+
except Exception as e:
|
|
1776
|
+
log_error(f"Error getting spans: {e}")
|
|
1777
|
+
return []
|