feat(cloud): attribute planner token usage

This commit is contained in:
2026-07-13 23:10:21 +08:00
parent b4803f90e6
commit 40efa53411
16 changed files with 259 additions and 17 deletions
@@ -2,7 +2,7 @@ from __future__ import annotations
from uuid import uuid4
from fastapi import APIRouter, HTTPException, Request, status
from fastapi import APIRouter, HTTPException, Query, Request, status
from cloud.auth import AuthProvider, GOVERNANCE_ADMIN_SCOPE, GOVERNANCE_READ_SCOPE
from cloud.governance import GovernancePolicyConflictError
@@ -11,6 +11,7 @@ from cloud.sdk.models import (
HostGovernancePolicyRequest,
HostGovernancePolicyResponse,
HostTokenUsageSummaryResponse,
TokenUsageEventResponse,
UserSubmissionPolicyRequest,
UserSubmissionPolicyResponse,
)
@@ -137,6 +138,36 @@ def create_governance_router(*, repository, auth_provider: AuthProvider) -> APIR
remaining_tokens=summary.remaining_tokens,
)
@router.get(
"/hosts/{host_id}/token-usage-events",
response_model=list[TokenUsageEventResponse],
)
def list_host_token_usage_events(
host_id: str,
request: Request,
limit: int = Query(default=50, ge=1, le=100),
offset: int = Query(default=0, ge=0),
) -> list[TokenUsageEventResponse]:
authorize(request, GOVERNANCE_READ_SCOPE)
return [
TokenUsageEventResponse(
id=event.id,
host_id=event.host_id,
usage_day=event.usage_day,
task_id=event.task_id,
attempt=event.attempt,
provider=event.provider,
model=event.model,
input_tokens=event.input_tokens,
output_tokens=event.output_tokens,
total_tokens=event.total_tokens,
occurred_at=event.occurred_at,
)
for event in repository.list_host_token_usage_events(
host_id=host_id, limit=limit, offset=offset
)
]
@router.put(
"/hosts/{host_id}/governance-policy",
response_model=HostGovernancePolicyResponse,