Files
archie/internal/webui/api_capabilities.go
sam 4728c8c253 feat(usage): report model usage per org with identity and cost (#1855)
Model usage was recorded per call and never read, so an org could not see
what it spent. Add the read side:

- model_usage carries identity_id. Task calls take it from the task row,
  chat and curator calls from the calling principal carried in the usage
  record, so an outbox replay keeps its attribution. Existing task rows are
  backfilled from tasks.
- StatsService.UsageTotals sums the caller org's rows by UTC day, source,
  provider, model and identity. The org comes from the principal, with no
  org parameter, so cross-org reads do not exist.
- GET /api/usage prices tokens at the catalog's list rate, netting cache
  hits, and reports calls to unpriced models as tokens only. The published
  catalog view now carries each model's cost.
- A new org Usage page behind a read_usage grant: admin and owner only,
  with a 7, 30 or 90 day range, totals, the daily series, and model, source
  and identity breakdowns. The org shell keeps it on the caller's own org.
2026-11-11 02:15:29 +11:00

54 lines
2.0 KiB
Go

package webui
import (
"net/http"
"github.com/samcharles93/archie-core/internal/domain/access"
)
// handleCapabilities reports which dashboard sections this process can serve
// and which the caller may use, so the browser hides the rest.
func (s *Server) handleCapabilities(w http.ResponseWriter, r *http.Request) {
sections := map[string]bool{
"chat": s.Chat != nil && s.Chat.Contract != nil,
"logs": s.LogFeed != nil,
"skills": s.Skills != nil,
"curators": s.Curators != nil,
"channels": s.Channels != nil,
"captures": s.Captures != nil,
"mappings": s.Mappings != nil,
"bindings": s.Bindings != nil,
// The workflows page draws its statistics from the task store
// and its definition list from the local registry, so it is
// worth showing with only the former.
"workflows": true,
// Configuration renders from whichever source is wired, and only
// the system org operates the instance: every other org's
// settings pages would offer saves the control plane refuses.
"settings": instanceViewer(r),
}
// A page is offered only to a caller whose grants let it operate there.
// With no access chain the single operator may use everything.
_, hasPrincipal := access.PrincipalFromContext(r.Context())
for _, p := range pageProbes() {
sections[p.section] = !hasPrincipal || s.Access == nil || s.mayCall(r, p.method, p.path)
}
writeJSON(w, map[string]any{"sections": sections})
}
type pageProbe struct{ section, method, path string }
// pageProbes names, per page, the API call that page makes; the dashboard
// hides a page whose call the caller's grants refuse.
func pageProbes() []pageProbe {
return []pageProbe{
{"org_members", http.MethodPost, "/api/orgs/_/members"},
{"org_agents", http.MethodPut, "/api/orgs/_/agents/_"},
{"org_policies", http.MethodPut, "/api/access/policies"},
{"org_denials", http.MethodGet, "/api/access/denials"},
{"org_audit", http.MethodGet, "/api/audit"},
{"org_usage", http.MethodGet, "/api/usage"},
{"org_config", http.MethodPost, "/api/control-plane/resources/_/commands/replace"},
}
}