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

@@ -0,0 +1,60 @@
/**
* SaaS Billing Methods — Mixin
*
* Installs billing-related methods (usage increment, quota check) onto
* SaaSClient.prototype. Uses the same mixin pattern as saas-telemetry.ts.
*/
export interface UsageIncrementResult {
dimension: string;
incremented: number;
usage: {
relay_requests: number;
hand_executions: number;
pipeline_runs: number;
input_tokens: number;
output_tokens: number;
[key: string]: unknown;
};
}
export function installBillingMethods(ClientClass: { prototype: any }): void {
const proto = ClientClass.prototype;
/**
* Report a usage increment for a specific dimension.
*
* Called after hand execution, pipeline run, or other metered operations.
* Non-blocking — failures are logged but don't affect the caller.
*
* @param dimension - One of: hand_executions, pipeline_runs, relay_requests
* @param count - How many units to increment (default 1, max 100)
*/
proto.incrementUsageDimension = async function (
this: { request<T>(method: string, path: string, body?: unknown): Promise<T> },
dimension: string,
count: number = 1,
): Promise<UsageIncrementResult> {
return this.request<UsageIncrementResult>(
'POST',
'/api/v1/billing/usage/increment',
{ dimension, count },
);
};
/**
* Fire-and-forget version of incrementUsageDimension.
* Logs errors but never throws — safe to call in finally blocks.
*/
proto.reportUsageFireAndForget = function (
this: { incrementUsageDimension(dimension: string, count?: number): Promise<UsageIncrementResult> },
dimension: string,
count: number = 1,
): void {
this.incrementUsageDimension(dimension, count).catch((err: unknown) => {
// Non-fatal: billing reporting failure must never block user operations
const msg = err instanceof Error ? err.message : String(err);
console.warn(`[Billing] Failed to report ${dimension} usage (+${count}): ${msg}`);
});
};
}

View File

@@ -132,6 +132,7 @@ import { installAdminMethods } from './saas-admin';
import { installRelayMethods } from './saas-relay';
import { installPromptMethods } from './saas-prompt';
import { installTelemetryMethods } from './saas-telemetry';
import { installBillingMethods } from './saas-billing';
// === Client Implementation ===
@@ -411,6 +412,7 @@ installAdminMethods(SaaSClient);
installRelayMethods(SaaSClient);
installPromptMethods(SaaSClient);
installTelemetryMethods(SaaSClient);
installBillingMethods(SaaSClient);
// === API Method Type Declarations ===
// These methods are installed at runtime by installXxxMethods() in saas-*.ts.
@@ -516,6 +518,10 @@ export interface SaaSClient {
timestamp: string;
}>;
}): Promise<{ accepted: number; total: number }>;
// --- Billing (saas-billing.ts) ---
incrementUsageDimension(dimension: string, count?: number): Promise<import('./saas-billing').UsageIncrementResult>;
reportUsageFireAndForget(dimension: string, count?: number): void;
}
// === Singleton ===