Version 1.2.2. Added usage overview. Shows token used and cost in $.

This commit is contained in:
2026-04-15 10:00:39 +02:00
parent 752691fe54
commit d4c6420481
18 changed files with 1657 additions and 86 deletions

View File

@@ -1,5 +1,20 @@
"""
agents/tasks.py — Agent and agent run CRUD operations (async).
agents/tasks.py — Agent and agent run CRUD operations (async, PostgreSQL).
Agents are persistent, named, goal-oriented configurations that can be:
- Run manually (via the Agents UI or /api/agents/{id}/run)
- Scheduled with a cron expression (managed by AgentRunner + APScheduler)
- Triggered by email, Telegram, webhooks, or monitors
agent_runs records every execution — input/output tokens, status, result text.
This is the source of truth for the Agents UI run history and token totals.
Design note on allowed_tools:
- NULL in DB means "all tools" (unlimited access)
- [] (empty list) is falsy in Python — treated identically to NULL
- Non-empty list restricts the agent to exactly those tools
- This is enforced structurally: only declared schemas are sent to the model;
it is impossible for the model to call a tool it wasn't given a schema for.
"""
from __future__ import annotations
@@ -161,6 +176,8 @@ async def finish_run(
output_tokens: int = 0,
result: str | None = None,
error: str | None = None,
model: str | None = None,
cost_usd: float | None = None,
) -> dict | None:
now = _now()
pool = await get_pool()
@@ -168,10 +185,12 @@ async def finish_run(
"""
UPDATE agent_runs
SET ended_at = $1, status = $2, input_tokens = $3,
output_tokens = $4, result = $5, error = $6
WHERE id = $7
output_tokens = $4, result = $5, error = $6,
model = $7, cost_usd = $8
WHERE id = $9
""",
now, status, input_tokens, output_tokens, result, error, run_id,
now, status, input_tokens, output_tokens, result, error,
model or None, cost_usd, run_id,
)
return await get_run(run_id)