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

@@ -470,7 +470,7 @@ class Agent:
confirmed = False
# Confirmation flow (interactive sessions only)
if tool.requires_confirmation and task_id is None:
if await tool.should_confirm(**tc.arguments) and task_id is None:
description = tool.confirmation_description(**tc.arguments)
yield ConfirmationRequiredEvent(
call_id=tc.id,
@@ -697,12 +697,18 @@ class Agent:
# Update in-memory history for multi-turn
self._session_history[session_id] = messages
# Persist conversation to DB
# Persist conversation to DB (with accumulated usage for cost tracking)
from ..providers.models import compute_cost_usd as _compute_cost
_model_str = model or "" # full "provider:model" string for pricing lookup
_cost = _compute_cost(_model_str, total_usage.input_tokens, total_usage.output_tokens) if _model_str else None
await _save_conversation(
session_id=session_id,
messages=messages,
task_id=task_id,
model=response.model or run_model or model or "",
input_tokens=total_usage.input_tokens,
output_tokens=total_usage.output_tokens,
cost_usd=_cost,
)
yield DoneEvent(
@@ -735,6 +741,9 @@ async def _save_conversation(
messages: list[dict],
task_id: str | None,
model: str = "",
input_tokens: int = 0,
output_tokens: int = 0,
cost_usd: float | None = None,
) -> None:
from ..context_vars import current_user as _cu
user_id = _cu.get().id if _cu.get() else None
@@ -745,26 +754,41 @@ async def _save_conversation(
"SELECT id, title FROM conversations WHERE id = $1", session_id
)
if existing:
# Only update title if still unset (don't overwrite a user-renamed title)
# Accumulate tokens across turns; only update title if still unset
if not existing["title"]:
title = _derive_title(messages)
await pool.execute(
"UPDATE conversations SET messages = $1, ended_at = $2, title = $3, model = $4 WHERE id = $5",
messages, now, title, model or None, session_id,
"""UPDATE conversations
SET messages = $1, ended_at = $2, title = $3, model = $4,
input_tokens = COALESCE(input_tokens, 0) + $5,
output_tokens = COALESCE(output_tokens, 0) + $6,
cost_usd = COALESCE(cost_usd, 0) + COALESCE($7, 0)
WHERE id = $8""",
messages, now, title, model or None,
input_tokens, output_tokens, cost_usd, session_id,
)
else:
await pool.execute(
"UPDATE conversations SET messages = $1, ended_at = $2, model = $3 WHERE id = $4",
messages, now, model or None, session_id,
"""UPDATE conversations
SET messages = $1, ended_at = $2, model = $3,
input_tokens = COALESCE(input_tokens, 0) + $4,
output_tokens = COALESCE(output_tokens, 0) + $5,
cost_usd = COALESCE(cost_usd, 0) + COALESCE($6, 0)
WHERE id = $7""",
messages, now, model or None,
input_tokens, output_tokens, cost_usd, session_id,
)
else:
title = _derive_title(messages)
await pool.execute(
"""
INSERT INTO conversations (id, started_at, ended_at, messages, task_id, user_id, title, model)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
INSERT INTO conversations
(id, started_at, ended_at, messages, task_id, user_id, title, model,
input_tokens, output_tokens, cost_usd)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
""",
session_id, now, now, messages, task_id, user_id, title, model or None,
input_tokens, output_tokens, cost_usd,
)
except Exception as e:
logger.error(f"Failed to save conversation {session_id}: {e}")