mirror of
https://github.com/hanzoai/functions.git
synced 2026-10-30 15:04:37 +00:00
Reference wiring of the DRY usage-metering hook into a non-LLM product. Functions now bills per invocation through commerce (the billing source of truth) — same balance gate + usage record the LLM/cloud path uses — via the shared github.com/hanzoai/commerce/metering client. No bespoke billing logic. (universe task #28) - pkg/usagemeter: thin env-gated adapter over commerce/metering. Builds the client from FromEnv() and exposes a gorilla/mux-compatible middleware: balance gate (fail-closed) + post-invocation usage record, identity from gateway-minted X-User-Id/X-Org-Id, skips /router-healthz + /metrics, flat per-call price from FUNCTIONS_PRICE_PER_CALL_CENTS. - router.go: single mux.Use(usagemeter.Middleware(logger)) line next to the existing metrics middleware. Transparent pass-through until COMMERCE_URL + COMMERCE_SERVICE_TOKEN (KMS-backed) are set, so safe to wire now. - go.mod: add the metering require + local replace ONLY — no go-version bump, no indirect churn, no go.sum change, because commerce/metering is a standalone stdlib-only module (its own go.mod, go 1.24, zero deps). Pinned to the local checkout until hanzoai/commerce#41 merges + tags. - 4 tests: pass-through when disabled, gate+record on success, 402 on empty balance, infra-path skip. All green; go vet + gofmt clean.