feat(billing): add usage increment API + wire hand/pipeline execution tracking

Server side:
- POST /api/v1/billing/usage/increment endpoint with dimension whitelist
  (hand_executions, pipeline_runs, relay_requests) and count validation (1-100)
- Returns updated usage quota after increment

Desktop side:
- New saas-billing.ts mixin with incrementUsageDimension() and
  reportUsageFireAndForget() (non-blocking, safe for finally blocks)
- handStore.triggerHand: reports hand_executions after successful run
- PipelinesPanel.handleRunComplete: reports pipeline_runs on completion
- SaaSClient type declarations for new billing methods

Billing pipeline now covers all three dimensions:
  relay_requests  → relay handler (server-side, real-time)
  hand_executions → handStore (client-side, fire-and-forget)
  pipeline_runs   → PipelinesPanel (client-side, fire-and-forget)
This commit is contained in:
iven
2026-04-02 02:02:59 +08:00
parent 11e3d37468
commit 837abec48a
6 changed files with 130 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import { create } from 'zustand';
import type { GatewayClient } from '../lib/gateway-client';
import { canAutoExecute, getAutonomyManager } from '../lib/autonomy-manager';
import type { AutonomyDecision } from '../lib/autonomy-manager';
import { saasClient } from '../lib/saas-client';
// === Re-exported Types (from gatewayStore for compatibility) ===
@@ -355,6 +356,13 @@ export const useHandStore = create<HandStore>((set, get) => ({
// Refresh hands to update status
await get().loadHands();
// Report hand execution to billing (fire-and-forget, non-blocking)
try {
if (saasClient.isAuthenticated()) {
saasClient.reportUsageFireAndForget('hand_executions');
}
} catch { /* billing reporting must never block */ }
return run;
} catch (err: unknown) {
set({ error: err instanceof Error ? err.message : String(err) });