feat: add token usage tracking and leaderboard

- Add token_usage table with composite unique index for claw_id + date
- Add API endpoints: POST /token, GET /token/leaderboard, GET /token/stats
- Add TokenLeaderboard component with daily/total period toggle
- Add CLI commands: `token` and `stats` for reporting and viewing usage
- Add Redis caching for leaderboard with 1-minute TTL
- Add shared utilities: authenticateRequest, getTodayDateString
- Use UPSERT pattern for atomic token updates
- Add i18n translations (en/zh) for new UI elements
This commit is contained in:
richarjiang
2026-03-15 15:17:10 +08:00
parent 8d094ad5cc
commit 36f10954cf
22 changed files with 3140 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
import { z } from "zod";
const MAX_TOKENS = 1_000_000_000_000; // 1 trillion
export const registerSchema = z.object({
name: z.string().min(1).max(100),
platform: z.string().optional(),
@@ -19,6 +21,13 @@ export const taskSchema = z.object({
toolsUsed: z.array(z.string()).optional(),
});
export const tokenSchema = z.object({
inputTokens: z.number().int().nonnegative().max(MAX_TOKENS),
outputTokens: z.number().int().nonnegative().max(MAX_TOKENS),
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional(),
});
export type RegisterInput = z.infer<typeof registerSchema>;
export type HeartbeatInput = z.infer<typeof heartbeatSchema>;
export type TaskInput = z.infer<typeof taskSchema>;
export type TokenInput = z.infer<typeof tokenSchema>;