diff --git a/drizzle/0040_bored_venus.sql b/drizzle/0040_bored_venus.sql new file mode 100644 index 000000000..07be939e1 --- /dev/null +++ b/drizzle/0040_bored_venus.sql @@ -0,0 +1 @@ +ALTER TABLE "message_request" ADD COLUMN "ttfb_ms" integer; \ No newline at end of file diff --git a/drizzle/meta/0040_snapshot.json b/drizzle/meta/0040_snapshot.json new file mode 100644 index 000000000..f13c88541 --- /dev/null +++ b/drizzle/meta/0040_snapshot.json @@ -0,0 +1,1951 @@ +{ + "id": "1da9ba1f-bef1-4e61-ac2c-43868c526b28", + "prevId": "b6de6e35-33b4-4a1b-94c9-c6707bb17468", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.error_rules": { + "name": "error_rules", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "pattern": { + "name": "pattern", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "match_type": { + "name": "match_type", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true, + "default": "'regex'" + }, + "category": { + "name": "category", + "type": "varchar(50)", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "override_response": { + "name": "override_response", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "override_status_code": { + "name": "override_status_code", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "is_enabled": { + "name": "is_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "is_default": { + "name": "is_default", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "priority": { + "name": "priority", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": { + "idx_error_rules_enabled": { + "name": "idx_error_rules_enabled", + "columns": [ + { + "expression": "is_enabled", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "priority", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "unique_pattern": { + "name": "unique_pattern", + "columns": [ + { + "expression": "pattern", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_category": { + "name": "idx_category", + "columns": [ + { + "expression": "category", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_match_type": { + "name": "idx_match_type", + "columns": [ + { + "expression": "match_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.keys": { + "name": "keys", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "key": { + "name": "key", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "is_enabled": { + "name": "is_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "can_login_web_ui": { + "name": "can_login_web_ui", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "limit_5h_usd": { + "name": "limit_5h_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_daily_usd": { + "name": "limit_daily_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "daily_reset_mode": { + "name": "daily_reset_mode", + "type": "daily_reset_mode", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'fixed'" + }, + "daily_reset_time": { + "name": "daily_reset_time", + "type": "varchar(5)", + "primaryKey": false, + "notNull": true, + "default": "'00:00'" + }, + "limit_weekly_usd": { + "name": "limit_weekly_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_monthly_usd": { + "name": "limit_monthly_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_total_usd": { + "name": "limit_total_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_concurrent_sessions": { + "name": "limit_concurrent_sessions", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "provider_group": { + "name": "provider_group", + "type": "varchar(50)", + "primaryKey": false, + "notNull": false, + "default": "'default'" + }, + "cache_ttl_preference": { + "name": "cache_ttl_preference", + "type": "varchar(10)", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_keys_user_id": { + "name": "idx_keys_user_id", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_keys_created_at": { + "name": "idx_keys_created_at", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_keys_deleted_at": { + "name": "idx_keys_deleted_at", + "columns": [ + { + "expression": "deleted_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.message_request": { + "name": "message_request", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "provider_id": { + "name": "provider_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "key": { + "name": "key", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "model": { + "name": "model", + "type": "varchar(128)", + "primaryKey": false, + "notNull": false + }, + "duration_ms": { + "name": "duration_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "cost_usd": { + "name": "cost_usd", + "type": "numeric(21, 15)", + "primaryKey": false, + "notNull": false, + "default": "'0'" + }, + "cost_multiplier": { + "name": "cost_multiplier", + "type": "numeric(10, 4)", + "primaryKey": false, + "notNull": false + }, + "session_id": { + "name": "session_id", + "type": "varchar(64)", + "primaryKey": false, + "notNull": false + }, + "request_sequence": { + "name": "request_sequence", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 1 + }, + "provider_chain": { + "name": "provider_chain", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "status_code": { + "name": "status_code", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "api_type": { + "name": "api_type", + "type": "varchar(20)", + "primaryKey": false, + "notNull": false + }, + "endpoint": { + "name": "endpoint", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "original_model": { + "name": "original_model", + "type": "varchar(128)", + "primaryKey": false, + "notNull": false + }, + "input_tokens": { + "name": "input_tokens", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "output_tokens": { + "name": "output_tokens", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "ttfb_ms": { + "name": "ttfb_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "cache_creation_input_tokens": { + "name": "cache_creation_input_tokens", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "cache_read_input_tokens": { + "name": "cache_read_input_tokens", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "cache_creation_5m_input_tokens": { + "name": "cache_creation_5m_input_tokens", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "cache_creation_1h_input_tokens": { + "name": "cache_creation_1h_input_tokens", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "cache_ttl_applied": { + "name": "cache_ttl_applied", + "type": "varchar(10)", + "primaryKey": false, + "notNull": false + }, + "context_1m_applied": { + "name": "context_1m_applied", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "error_message": { + "name": "error_message", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "error_stack": { + "name": "error_stack", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "error_cause": { + "name": "error_cause", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "blocked_by": { + "name": "blocked_by", + "type": "varchar(50)", + "primaryKey": false, + "notNull": false + }, + "blocked_reason": { + "name": "blocked_reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_agent": { + "name": "user_agent", + "type": "varchar(512)", + "primaryKey": false, + "notNull": false + }, + "messages_count": { + "name": "messages_count", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_message_request_user_date_cost": { + "name": "idx_message_request_user_date_cost", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "cost_usd", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"message_request\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_user_query": { + "name": "idx_message_request_user_query", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"message_request\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_session_id": { + "name": "idx_message_request_session_id", + "columns": [ + { + "expression": "session_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"message_request\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_session_seq": { + "name": "idx_message_request_session_seq", + "columns": [ + { + "expression": "session_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "request_sequence", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"message_request\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_endpoint": { + "name": "idx_message_request_endpoint", + "columns": [ + { + "expression": "endpoint", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"message_request\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_provider_id": { + "name": "idx_message_request_provider_id", + "columns": [ + { + "expression": "provider_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_user_id": { + "name": "idx_message_request_user_id", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_key": { + "name": "idx_message_request_key", + "columns": [ + { + "expression": "key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_created_at": { + "name": "idx_message_request_created_at", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_message_request_deleted_at": { + "name": "idx_message_request_deleted_at", + "columns": [ + { + "expression": "deleted_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.model_prices": { + "name": "model_prices", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "model_name": { + "name": "model_name", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "price_data": { + "name": "price_data", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": { + "idx_model_prices_latest": { + "name": "idx_model_prices_latest", + "columns": [ + { + "expression": "model_name", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_model_prices_model_name": { + "name": "idx_model_prices_model_name", + "columns": [ + { + "expression": "model_name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_model_prices_created_at": { + "name": "idx_model_prices_created_at", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.notification_settings": { + "name": "notification_settings", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "enabled": { + "name": "enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "circuit_breaker_enabled": { + "name": "circuit_breaker_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "circuit_breaker_webhook": { + "name": "circuit_breaker_webhook", + "type": "varchar(512)", + "primaryKey": false, + "notNull": false + }, + "daily_leaderboard_enabled": { + "name": "daily_leaderboard_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "daily_leaderboard_webhook": { + "name": "daily_leaderboard_webhook", + "type": "varchar(512)", + "primaryKey": false, + "notNull": false + }, + "daily_leaderboard_time": { + "name": "daily_leaderboard_time", + "type": "varchar(10)", + "primaryKey": false, + "notNull": false, + "default": "'09:00'" + }, + "daily_leaderboard_top_n": { + "name": "daily_leaderboard_top_n", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 5 + }, + "cost_alert_enabled": { + "name": "cost_alert_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "cost_alert_webhook": { + "name": "cost_alert_webhook", + "type": "varchar(512)", + "primaryKey": false, + "notNull": false + }, + "cost_alert_threshold": { + "name": "cost_alert_threshold", + "type": "numeric(5, 2)", + "primaryKey": false, + "notNull": false, + "default": "'0.80'" + }, + "cost_alert_check_interval": { + "name": "cost_alert_check_interval", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 60 + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.providers": { + "name": "providers", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "url": { + "name": "url", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "key": { + "name": "key", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "is_enabled": { + "name": "is_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "weight": { + "name": "weight", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 1 + }, + "priority": { + "name": "priority", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "cost_multiplier": { + "name": "cost_multiplier", + "type": "numeric(10, 4)", + "primaryKey": false, + "notNull": false, + "default": "'1.0'" + }, + "group_tag": { + "name": "group_tag", + "type": "varchar(50)", + "primaryKey": false, + "notNull": false + }, + "provider_type": { + "name": "provider_type", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true, + "default": "'claude'" + }, + "preserve_client_ip": { + "name": "preserve_client_ip", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "model_redirects": { + "name": "model_redirects", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "allowed_models": { + "name": "allowed_models", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'null'::jsonb" + }, + "join_claude_pool": { + "name": "join_claude_pool", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "codex_instructions_strategy": { + "name": "codex_instructions_strategy", + "type": "varchar(20)", + "primaryKey": false, + "notNull": false, + "default": "'auto'" + }, + "mcp_passthrough_type": { + "name": "mcp_passthrough_type", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true, + "default": "'none'" + }, + "mcp_passthrough_url": { + "name": "mcp_passthrough_url", + "type": "varchar(512)", + "primaryKey": false, + "notNull": false + }, + "limit_5h_usd": { + "name": "limit_5h_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_daily_usd": { + "name": "limit_daily_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "daily_reset_mode": { + "name": "daily_reset_mode", + "type": "daily_reset_mode", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'fixed'" + }, + "daily_reset_time": { + "name": "daily_reset_time", + "type": "varchar(5)", + "primaryKey": false, + "notNull": true, + "default": "'00:00'" + }, + "limit_weekly_usd": { + "name": "limit_weekly_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_monthly_usd": { + "name": "limit_monthly_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_concurrent_sessions": { + "name": "limit_concurrent_sessions", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "max_retry_attempts": { + "name": "max_retry_attempts", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "circuit_breaker_failure_threshold": { + "name": "circuit_breaker_failure_threshold", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 5 + }, + "circuit_breaker_open_duration": { + "name": "circuit_breaker_open_duration", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 1800000 + }, + "circuit_breaker_half_open_success_threshold": { + "name": "circuit_breaker_half_open_success_threshold", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 2 + }, + "proxy_url": { + "name": "proxy_url", + "type": "varchar(512)", + "primaryKey": false, + "notNull": false + }, + "proxy_fallback_to_direct": { + "name": "proxy_fallback_to_direct", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "first_byte_timeout_streaming_ms": { + "name": "first_byte_timeout_streaming_ms", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "streaming_idle_timeout_ms": { + "name": "streaming_idle_timeout_ms", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "request_timeout_non_streaming_ms": { + "name": "request_timeout_non_streaming_ms", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "website_url": { + "name": "website_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "favicon_url": { + "name": "favicon_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "cache_ttl_preference": { + "name": "cache_ttl_preference", + "type": "varchar(10)", + "primaryKey": false, + "notNull": false + }, + "context_1m_preference": { + "name": "context_1m_preference", + "type": "varchar(20)", + "primaryKey": false, + "notNull": false + }, + "tpm": { + "name": "tpm", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "rpm": { + "name": "rpm", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "rpd": { + "name": "rpd", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "cc": { + "name": "cc", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_providers_enabled_priority": { + "name": "idx_providers_enabled_priority", + "columns": [ + { + "expression": "is_enabled", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "priority", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "weight", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"providers\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_providers_group": { + "name": "idx_providers_group", + "columns": [ + { + "expression": "group_tag", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"providers\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_providers_created_at": { + "name": "idx_providers_created_at", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_providers_deleted_at": { + "name": "idx_providers_deleted_at", + "columns": [ + { + "expression": "deleted_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.request_filters": { + "name": "request_filters", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(100)", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "scope": { + "name": "scope", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true + }, + "action": { + "name": "action", + "type": "varchar(30)", + "primaryKey": false, + "notNull": true + }, + "match_type": { + "name": "match_type", + "type": "varchar(20)", + "primaryKey": false, + "notNull": false + }, + "target": { + "name": "target", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "replacement": { + "name": "replacement", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "priority": { + "name": "priority", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "is_enabled": { + "name": "is_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": { + "idx_request_filters_enabled": { + "name": "idx_request_filters_enabled", + "columns": [ + { + "expression": "is_enabled", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "priority", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_request_filters_scope": { + "name": "idx_request_filters_scope", + "columns": [ + { + "expression": "scope", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_request_filters_action": { + "name": "idx_request_filters_action", + "columns": [ + { + "expression": "action", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.sensitive_words": { + "name": "sensitive_words", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "word": { + "name": "word", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "match_type": { + "name": "match_type", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true, + "default": "'contains'" + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_enabled": { + "name": "is_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": { + "idx_sensitive_words_enabled": { + "name": "idx_sensitive_words_enabled", + "columns": [ + { + "expression": "is_enabled", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "match_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_sensitive_words_created_at": { + "name": "idx_sensitive_words_created_at", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.system_settings": { + "name": "system_settings", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "site_title": { + "name": "site_title", + "type": "varchar(128)", + "primaryKey": false, + "notNull": true, + "default": "'Claude Code Hub'" + }, + "allow_global_usage_view": { + "name": "allow_global_usage_view", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "currency_display": { + "name": "currency_display", + "type": "varchar(10)", + "primaryKey": false, + "notNull": true, + "default": "'USD'" + }, + "billing_model_source": { + "name": "billing_model_source", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true, + "default": "'original'" + }, + "enable_auto_cleanup": { + "name": "enable_auto_cleanup", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "cleanup_retention_days": { + "name": "cleanup_retention_days", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 30 + }, + "cleanup_schedule": { + "name": "cleanup_schedule", + "type": "varchar(50)", + "primaryKey": false, + "notNull": false, + "default": "'0 2 * * *'" + }, + "cleanup_batch_size": { + "name": "cleanup_batch_size", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 10000 + }, + "enable_client_version_check": { + "name": "enable_client_version_check", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "verbose_provider_error": { + "name": "verbose_provider_error", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "enable_http2": { + "name": "enable_http2", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "role": { + "name": "role", + "type": "varchar", + "primaryKey": false, + "notNull": false, + "default": "'user'" + }, + "rpm_limit": { + "name": "rpm_limit", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 60 + }, + "daily_limit_usd": { + "name": "daily_limit_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false, + "default": "'100.00'" + }, + "provider_group": { + "name": "provider_group", + "type": "varchar(50)", + "primaryKey": false, + "notNull": false, + "default": "'default'" + }, + "tags": { + "name": "tags", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "limit_5h_usd": { + "name": "limit_5h_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_weekly_usd": { + "name": "limit_weekly_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_monthly_usd": { + "name": "limit_monthly_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_total_usd": { + "name": "limit_total_usd", + "type": "numeric(10, 2)", + "primaryKey": false, + "notNull": false + }, + "limit_concurrent_sessions": { + "name": "limit_concurrent_sessions", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "daily_reset_mode": { + "name": "daily_reset_mode", + "type": "daily_reset_mode", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'fixed'" + }, + "daily_reset_time": { + "name": "daily_reset_time", + "type": "varchar(5)", + "primaryKey": false, + "notNull": true, + "default": "'00:00'" + }, + "is_enabled": { + "name": "is_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "allowed_clients": { + "name": "allowed_clients", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "allowed_models": { + "name": "allowed_models", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_users_active_role_sort": { + "name": "idx_users_active_role_sort", + "columns": [ + { + "expression": "deleted_at", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "role", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"users\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_users_enabled_expires_at": { + "name": "idx_users_enabled_expires_at", + "columns": [ + { + "expression": "is_enabled", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "expires_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"users\".\"deleted_at\" IS NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_users_created_at": { + "name": "idx_users_created_at", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_users_deleted_at": { + "name": "idx_users_deleted_at", + "columns": [ + { + "expression": "deleted_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": { + "public.daily_reset_mode": { + "name": "daily_reset_mode", + "schema": "public", + "values": [ + "fixed", + "rolling" + ] + } + }, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index eb4aa1302..0bfd887ea 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -281,6 +281,13 @@ "when": 1766461982056, "tag": "0039_abnormal_marvel_apes", "breakpoints": true + }, + { + "idx": 40, + "version": "7", + "when": 1766509746306, + "tag": "0040_bored_venus", + "breakpoints": true } ] } \ No newline at end of file diff --git a/messages/en/dashboard.json b/messages/en/dashboard.json index d9ea063f4..03d520d79 100644 --- a/messages/en/dashboard.json +++ b/messages/en/dashboard.json @@ -55,6 +55,10 @@ "filters": { "user": "User", "provider": "Provider", + "searchUser": "Search users...", + "searchProvider": "Search providers...", + "noUserFound": "No matching users found", + "noProviderFound": "No matching providers found", "model": "Model", "endpoint": "Endpoint", "status": "Status", @@ -92,10 +96,13 @@ "endpoint": "Endpoint", "inputTokens": "Input", "outputTokens": "Output", + "tokens": "Tokens", "cacheWrite": "Cache Write", "cacheRead": "Cache Read", + "cache": "Cache", "cost": "Cost", "duration": "Duration", + "performance": "Perf", "status": "Status" }, "stats": { @@ -194,6 +201,12 @@ }, "billingDetails": { "title": "Billing Details" + }, + "performance": { + "title": "Performance", + "ttfb": "TTFB", + "duration": "Total Duration", + "outputRate": "Output Rate" } }, "providerChain": { @@ -274,7 +287,9 @@ "cacheCreationConsumedAmount": "Cache Creation Spend", "totalConsumedAmount": "Total Spend", "successRate": "Success Rate", - "avgResponseTime": "Avg Response Time" + "avgResponseTime": "Avg Response Time", + "avgTtfbMs": "Avg TTFB", + "avgTokensPerSecond": "Avg tok/s" }, "states": { "loading": "Loading...", @@ -356,10 +371,14 @@ "noDetailedData": "No detailed data available", "storageTip": "Tip: Set environment variable STORE_SESSION_MESSAGES=true to enable messages and response storage", "clientInfo": "Client Info", + "requestHeaders": "Request Headers", + "requestBody": "Request Body", "requestMessages": "Request Messages", "requestMessagesDescription": "Message content sent by the client", - "responseBody": "Response Body Content", + "responseHeaders": "Response Headers", + "responseBody": "Response Body", "responseBodyDescription": "Complete server response (5-minute TTL)", + "noHeaders": "No data", "noData": "No Data" }, "actions": { @@ -468,6 +487,7 @@ "usageLogs": "Usage Logs", "leaderboard": "Leaderboard", "availability": "Availability", + "myQuota": "My Quota", "quotasManagement": "Quotas", "userManagement": "Users", "providers": "Providers", @@ -837,25 +857,6 @@ "editAriaLabel": "Edit user", "deleteAriaLabel": "Delete user" }, - "users": { - "title": "User Management", - "description": "Showing {count} users", - "toolbar": { - "searchPlaceholder": "Search name, note, tags, keys...", - "groupFilter": "Filter by Group", - "allGroups": "All Groups", - "tagFilter": "Filter by Tag", - "allTags": "All Tags", - "keyGroupFilter": "Key Group", - "allKeyGroups": "All Key Groups", - "createUser": "Create User", - "createKey": "Create Key" - }, - "dialog": { - "userProviderGroup": "Your Provider Groups", - "userProviderGroupHint": "New keys can only use your existing provider groups." - } - }, "availability": { "title": "Provider Availability Monitor", "description": "Real-time monitoring of provider availability and performance metrics", @@ -968,6 +969,90 @@ "refreshFailed": "Refresh failed, please retry" } }, + "rateLimits": { + "title": "Rate Limit Event Statistics", + "description": "View and analyze statistics for rate limit events", + "loading": "Loading...", + "error": "Load Failed", + "totalEvents": "Total Events", + "avgUsage": "Avg Usage", + "affectedUsers": "Affected Users", + "noData": "No Data", + "noDataHint": "No rate limit events in the selected time range", + "filters": { + "startTime": "Start Time", + "endTime": "End Time", + "user": "User", + "provider": "Provider", + "limitType": "Limit Type", + "allUsers": "All Users", + "allProviders": "All Providers", + "allLimitTypes": "All Types", + "apply": "Apply Filters", + "reset": "Reset", + "loading": "Loading...", + "limitTypes": { + "rpm": "RPM Limit", + "usd_5h": "5h Spend Limit", + "usd_weekly": "Weekly Spend Limit", + "usd_monthly": "Monthly Spend Limit", + "concurrent_sessions": "Concurrent Session Limit", + "daily_quota": "Daily Quota Limit" + } + }, + "chart": { + "title": "Rate Limit Timeline", + "description": "Hourly trend of rate limit events", + "total": "Total", + "events": "Events" + }, + "breakdown": { + "title": "Rate Limit Type Breakdown", + "description": "Share of events by rate limit type", + "total": "Total", + "count": "Events", + "percentage": "Percentage", + "noData": "No Data", + "types": { + "rpm": "RPM", + "usd_5h": "5h Spend", + "usd_weekly": "Weekly Spend", + "usd_monthly": "Monthly Spend", + "concurrent_sessions": "Concurrent Sessions", + "daily_quota": "Daily Quota" + } + }, + "topUsers": { + "title": "Top Affected Users", + "description": "Users who triggered rate limits most frequently", + "total": "Total", + "rank": "Rank", + "username": "Username", + "eventCount": "Events", + "percentage": "Percentage", + "loading": "Loading...", + "noData": "No Data" + } + }, + "users": { + "title": "User Management", + "description": "Showing {count} users", + "toolbar": { + "searchPlaceholder": "Search name, note, tags, keys...", + "groupFilter": "Filter by Group", + "allGroups": "All Groups", + "tagFilter": "Filter by Tag", + "allTags": "All Tags", + "keyGroupFilter": "Key Group", + "allKeyGroups": "All Key Groups", + "createUser": "Create User", + "createKey": "Create Key" + }, + "dialog": { + "userProviderGroup": "Your Provider Groups", + "userProviderGroupHint": "New keys can only use your existing provider groups." + } + }, "userManagement": { "table": { "columns": { @@ -1079,6 +1164,7 @@ "keySaveFailed": "Failed to save key", "keyDeleteFailed": "Failed to delete key", "saveSuccess": "Changes saved successfully", + "atLeastOneKeyEnabled": "At least one key must be enabled", "operationFailed": "Operation failed", "userDisabled": "User has been disabled", "userEnabled": "User has been enabled", @@ -1307,6 +1393,9 @@ "keyStatus": { "enabled": "Enabled", "disabled": "Disabled", + "active": "Active", + "expired": "Expired", + "expiringSoon": "Expiring Soon", "keyEnabled": "Key enabled", "keyDisabled": "Key disabled", "toggleKeyStatus": "Toggle key status", @@ -1318,6 +1407,9 @@ "userStatus": { "enabled": "Enabled", "disabled": "Disabled", + "active": "Active", + "expired": "Expired", + "expiringSoon": "Expiring Soon", "userEnabled": "User enabled", "userDisabled": "User disabled", "toggleUserStatus": "Toggle user status", @@ -1397,7 +1489,8 @@ }, "enableStatus": { "label": "Enable Status", - "description": "Disabled keys cannot be used" + "description": "Disabled keys cannot be used", + "cannotDisableTooltip": "Cannot disable the last enabled key" }, "balanceQueryPage": { "label": "Independent Personal Usage Page", diff --git a/messages/en/provider-chain.json b/messages/en/provider-chain.json index 8f2e45312..43e2b9a8d 100644 --- a/messages/en/provider-chain.json +++ b/messages/en/provider-chain.json @@ -36,7 +36,8 @@ "requestChain": "Request Chain:", "systemError": "System Error", "concurrentLimit": "Concurrent Limit", - "http2Fallback": "HTTP/2 Fallback" + "http2Fallback": "HTTP/2 Fallback", + "clientError": "Client Error" }, "timeline": { "sessionReuse": "Session Reuse", @@ -127,6 +128,15 @@ "requestUrl": "URL", "requestHeaders": "Headers", "requestBody": "Body", - "requestBodyTruncated": "(truncated)" + "requestBodyTruncated": "(truncated)", + "clientErrorNonRetryable": "Client Error (Attempt {attempt}, non-retryable)", + "matchedRule": "Matched Error Rule", + "ruleId": "Rule ID: {id}", + "ruleCategory": "Rule Category: {category}", + "rulePattern": "Rule Pattern: {pattern}", + "ruleMatchType": "Match Type: {matchType}", + "ruleDescription": "Description: {description}", + "ruleHasOverride": "Overrides: response={response}, statusCode={statusCode}", + "clientErrorNote": "This error is caused by client input and is not retried or counted in the circuit breaker." } } diff --git a/messages/ja/dashboard.json b/messages/ja/dashboard.json index fa0bea4b4..00b64b9c4 100644 --- a/messages/ja/dashboard.json +++ b/messages/ja/dashboard.json @@ -55,6 +55,10 @@ "filters": { "user": "ユーザー", "provider": "プロバイダー", + "searchUser": "ユーザーを検索...", + "searchProvider": "プロバイダーを検索...", + "noUserFound": "一致するユーザーが見つかりません", + "noProviderFound": "一致するプロバイダーが見つかりません", "model": "モデル", "endpoint": "エンドポイント", "status": "ステータス", @@ -92,10 +96,13 @@ "endpoint": "エンドポイント", "inputTokens": "入力", "outputTokens": "出力", + "tokens": "Tokens", "cacheWrite": "キャッシュ書き込み", "cacheRead": "キャッシュ読み取り", + "cache": "Cache", "cost": "コスト", "duration": "所要時間", + "performance": "Perf", "status": "ステータス" }, "stats": { @@ -193,6 +200,12 @@ }, "billingDetails": { "title": "課金詳細" + }, + "performance": { + "title": "パフォーマンス", + "ttfb": "TTFB", + "duration": "総所要時間", + "outputRate": "出力速度" } }, "providerChain": { @@ -273,7 +286,9 @@ "cacheCreationConsumedAmount": "キャッシュ作成消費額", "totalConsumedAmount": "総消費額", "successRate": "成功率", - "avgResponseTime": "平均応答時間" + "avgResponseTime": "平均応答時間", + "avgTtfbMs": "平均TTFB", + "avgTokensPerSecond": "平均トークン/秒" }, "states": { "loading": "読み込み中...", @@ -355,10 +370,14 @@ "noDetailedData": "詳細データなし", "storageTip": "ヒント: メッセージとレスポンスの保存を有効にするには、環境変数 STORE_SESSION_MESSAGES=true を設定してください", "clientInfo": "クライアント情報", + "requestHeaders": "リクエストヘッダー", + "requestBody": "リクエストボディ", "requestMessages": "リクエスト メッセージ", "requestMessagesDescription": "クライアントが送信したメッセージ内容", - "responseBody": "レスポンスボディ内容", + "responseHeaders": "レスポンスヘッダー", + "responseBody": "レスポンスボディ", "responseBodyDescription": "サーバーからの完全なレスポンス (5 分間の TTL)", + "noHeaders": "データなし", "noData": "データなし" }, "actions": { @@ -465,6 +484,7 @@ "usageLogs": "使用ログ", "leaderboard": "ランキング", "availability": "可用性監視", + "myQuota": "自分のクォータ", "quotasManagement": "クォータ管理", "userManagement": "ユーザー", "providers": "プロバイダー管理", @@ -813,20 +833,6 @@ "editAriaLabel": "ユーザーを編集", "deleteAriaLabel": "ユーザーを削除" }, - "users": { - "title": "ユーザー管理", - "description": "{count} 人のユーザーを表示中", - "toolbar": { - "searchPlaceholder": "名前、メモ、タグ、キーで検索...", - "groupFilter": "グループでフィルター", - "allGroups": "すべてのグループ", - "tagFilter": "タグでフィルター", - "allTags": "すべてのタグ", - "keyGroupFilter": "キーグループ", - "allKeyGroups": "すべてのキーグループ", - "createUser": "ユーザーを作成" - } - }, "availability": { "title": "プロバイダー可用性モニター", "description": "プロバイダーの可用性とパフォーマンス指標をリアルタイムで監視", @@ -939,6 +945,85 @@ "refreshFailed": "更新に失敗しました。再試行してください" } }, + "rateLimits": { + "title": "レート制限イベント統計", + "description": "レート制限イベントの統計データを表示・分析", + "loading": "読み込み中...", + "error": "読み込み失敗", + "totalEvents": "総イベント数", + "avgUsage": "平均使用率", + "affectedUsers": "影響を受けたユーザー数", + "noData": "データなし", + "noDataHint": "選択した時間範囲にはレート制限イベントがありません", + "filters": { + "startTime": "開始時間", + "endTime": "終了時間", + "user": "ユーザー", + "provider": "プロバイダー", + "limitType": "制限タイプ", + "allUsers": "すべてのユーザー", + "allProviders": "すべてのプロバイダー", + "allLimitTypes": "すべてのタイプ", + "apply": "フィルターを適用", + "reset": "リセット", + "loading": "読み込み中...", + "limitTypes": { + "rpm": "RPM レート制限", + "usd_5h": "5時間の支出制限", + "usd_weekly": "週次支出制限", + "usd_monthly": "月次支出制限", + "concurrent_sessions": "同時セッション制限", + "daily_quota": "日次クォータ制限" + } + }, + "chart": { + "title": "レート制限イベントのタイムライン", + "description": "時間ごとのレート制限イベント推移", + "total": "合計", + "events": "イベント数" + }, + "breakdown": { + "title": "制限タイプ分布", + "description": "制限タイプ別イベント比率", + "total": "合計", + "count": "イベント数", + "percentage": "割合", + "noData": "データなし", + "types": { + "rpm": "RPM", + "usd_5h": "5時間の支出", + "usd_weekly": "週次支出", + "usd_monthly": "月次支出", + "concurrent_sessions": "同時セッション", + "daily_quota": "日次クォータ" + } + }, + "topUsers": { + "title": "影響ユーザーランキング", + "description": "レート制限を最も多く発生させたユーザー", + "total": "合計", + "rank": "順位", + "username": "ユーザー名", + "eventCount": "イベント数", + "percentage": "割合", + "loading": "読み込み中...", + "noData": "データなし" + } + }, + "users": { + "title": "ユーザー管理", + "description": "{count} 人のユーザーを表示中", + "toolbar": { + "searchPlaceholder": "名前、メモ、タグ、キーで検索...", + "groupFilter": "グループでフィルター", + "allGroups": "すべてのグループ", + "tagFilter": "タグでフィルター", + "allTags": "すべてのタグ", + "keyGroupFilter": "キーグループ", + "allKeyGroups": "すべてのキーグループ", + "createUser": "ユーザーを作成" + } + }, "userManagement": { "table": { "columns": { @@ -1041,6 +1126,7 @@ "keySaveFailed": "キーの保存に失敗しました", "keyDeleteFailed": "キーの削除に失敗しました", "saveSuccess": "変更が保存されました", + "atLeastOneKeyEnabled": "少なくとも1つのキーを有効にする必要があります", "operationFailed": "操作に失敗しました", "userDisabled": "ユーザーが無効化されました", "userEnabled": "ユーザーが有効化されました", @@ -1266,7 +1352,32 @@ }, "keyStatus": { "enabled": "有効", - "disabled": "無効" + "disabled": "無効", + "active": "正常", + "expired": "期限切れ", + "expiringSoon": "まもなく期限切れ", + "keyEnabled": "キーが有効になりました", + "keyDisabled": "キーが無効になりました", + "toggleKeyStatus": "キー状態を切り替える", + "clickToDisableKey": "クリックしてキーを無効化", + "clickToEnableKey": "クリックしてキーを有効化", + "operationFailed": "操作に失敗しました", + "clickToQuickRenew": "クリックして更新" + }, + "userStatus": { + "enabled": "有効", + "disabled": "無効", + "active": "正常", + "expired": "期限切れ", + "expiringSoon": "まもなく期限切れ", + "userEnabled": "ユーザーが有効になりました", + "userDisabled": "ユーザーが無効になりました", + "toggleUserStatus": "ユーザー状態を切り替える", + "clickToDisableUser": "クリックしてユーザーを無効化", + "clickToEnableUser": "クリックしてユーザーを有効化", + "operationFailed": "操作に失敗しました", + "deleteFailed": "削除に失敗しました", + "deleteSuccess": "削除しました" }, "userEditSection": { "sections": { @@ -1308,7 +1419,13 @@ "enabledDescription": "現在有効です。無効にすると、このユーザーとそのキーは使用できなくなります。", "disabledDescription": "現在無効です。有効にすると、このユーザーとそのキーが通常通り使用できるようになります。", "confirmDisable": "無効化を確認", - "confirmEnable": "有効化を確認" + "confirmEnable": "有効化を確認", + "confirmEnableTitle": "ユーザー有効化の確認", + "confirmDisableTitle": "ユーザー無効化の確認", + "confirmEnableDescription": "有効化すると、このユーザーとそのキーが通常通り使用できるようになります。", + "confirmDisableDescription": "無効化すると、このユーザーとそのキーは使用できなくなります。", + "cancel": "キャンセル", + "processing": "処理中..." } }, "presetClients": { @@ -1332,7 +1449,8 @@ }, "enableStatus": { "label": "有効状態", - "description": "無効化されたキーは使用できません" + "description": "無効化されたキーは使用できません", + "cannotDisableTooltip": "最後の有効なキーを無効にできません" }, "balanceQueryPage": { "label": "独立した個人使用量ページ", diff --git a/messages/ja/provider-chain.json b/messages/ja/provider-chain.json index d565e706b..a2e6c09e4 100644 --- a/messages/ja/provider-chain.json +++ b/messages/ja/provider-chain.json @@ -36,7 +36,8 @@ "requestChain": "リクエストチェーン:", "systemError": "システムエラー", "concurrentLimit": "同時実行制限", - "http2Fallback": "HTTP/2 フォールバック" + "http2Fallback": "HTTP/2 フォールバック", + "clientError": "クライアントエラー" }, "timeline": { "sessionReuse": "セッション再利用", @@ -127,6 +128,15 @@ "requestUrl": "URL", "requestHeaders": "ヘッダー", "requestBody": "ボディ", - "requestBodyTruncated": "(切り捨て)" + "requestBodyTruncated": "(切り捨て)", + "clientErrorNonRetryable": "クライアントエラー(試行{attempt}、再試行不可)", + "matchedRule": "一致したエラールール", + "ruleId": "ルールID: {id}", + "ruleCategory": "カテゴリ: {category}", + "rulePattern": "パターン: {pattern}", + "ruleMatchType": "一致タイプ: {matchType}", + "ruleDescription": "説明: {description}", + "ruleHasOverride": "上書き: 応答={response} ステータスコード={statusCode}", + "clientErrorNote": "このエラーはクライアント入力が原因のため再試行せず、サーキットブレーカーにもカウントされません。" } } diff --git a/messages/ru/dashboard.json b/messages/ru/dashboard.json index d0c4cb305..e046b8a16 100644 --- a/messages/ru/dashboard.json +++ b/messages/ru/dashboard.json @@ -55,6 +55,10 @@ "filters": { "user": "Пользователь", "provider": "Поставщик", + "searchUser": "Поиск пользователей...", + "searchProvider": "Поиск провайдеров...", + "noUserFound": "Пользователи не найдены", + "noProviderFound": "Провайдеры не найдены", "model": "Модель", "endpoint": "Эндпоинт", "status": "Статус", @@ -92,10 +96,13 @@ "endpoint": "Эндпоинт", "inputTokens": "Вход", "outputTokens": "Выход", + "tokens": "Tokens", "cacheWrite": "Запись в кэш", "cacheRead": "Чтение из кэша", + "cache": "Cache", "cost": "Стоимость", "duration": "Продолжительность", + "performance": "Perf", "status": "Статус" }, "stats": { @@ -193,6 +200,12 @@ }, "billingDetails": { "title": "Детали биллинга" + }, + "performance": { + "title": "Производительность", + "ttfb": "TTFB", + "duration": "Общее время", + "outputRate": "Скорость вывода" } }, "providerChain": { @@ -273,7 +286,9 @@ "cacheCreationConsumedAmount": "Расход на создание кэша", "totalConsumedAmount": "Общие расходы", "successRate": "Процент успеха", - "avgResponseTime": "Среднее время ответа" + "avgResponseTime": "Среднее время ответа", + "avgTtfbMs": "Средний TTFB", + "avgTokensPerSecond": "Средн. ток/с" }, "states": { "loading": "Загрузка...", @@ -355,10 +370,14 @@ "noDetailedData": "Подробные данные отсутствуют", "storageTip": "Подсказка: установите переменную окружения STORE_SESSION_MESSAGES=true, чтобы включить сохранение сообщений и ответов", "clientInfo": "Информация о клиенте", + "requestHeaders": "Заголовки запроса", + "requestBody": "Тело запроса", "requestMessages": "Сообщения запроса", "requestMessagesDescription": "Содержимое сообщения, отправленного клиентом", - "responseBody": "Содержимое тела ответа", + "responseHeaders": "Заголовки ответа", + "responseBody": "Тело ответа", "responseBodyDescription": "Полный ответ сервера (TTL 5 минут)", + "noHeaders": "Нет данных", "noData": "Нет данных" }, "actions": { @@ -468,6 +487,7 @@ "usageLogs": "Журналы", "leaderboard": "Лидеры", "availability": "Доступность", + "myQuota": "Моя квота", "quotasManagement": "Квоты", "userManagement": "Пользователи", "providers": "Управление поставщиками", @@ -815,25 +835,6 @@ "editAriaLabel": "Редактировать пользователя", "deleteAriaLabel": "Удалить пользователя" }, - "users": { - "title": "Управление пользователями", - "description": "Показано {count} пользователей", - "toolbar": { - "searchPlaceholder": "Поиск по имени, заметкам, тегам, ключам...", - "groupFilter": "Фильтр по группе", - "allGroups": "Все группы", - "tagFilter": "Фильтр по тегу", - "allTags": "Все теги", - "keyGroupFilter": "Группа ключей", - "allKeyGroups": "Все группы ключей", - "createUser": "Создать пользователя", - "createKey": "Создать ключ" - }, - "dialog": { - "userProviderGroup": "Ваши группы провайдеров", - "userProviderGroupHint": "Новые ключи могут использовать только ваши существующие группы провайдеров" - } - }, "availability": { "title": "Мониторинг доступности провайдеров", "description": "Мониторинг доступности и показателей производительности провайдеров в реальном времени", @@ -946,6 +947,90 @@ "refreshFailed": "Обновление не удалось, попробуйте снова" } }, + "rateLimits": { + "title": "Статистика событий лимитов", + "description": "Просмотр и анализ статистики событий лимитов", + "loading": "Загрузка...", + "error": "Ошибка загрузки", + "totalEvents": "Всего событий", + "avgUsage": "Среднее использование", + "affectedUsers": "Затронутые пользователи", + "noData": "Нет данных", + "noDataHint": "Нет событий лимитов в выбранном диапазоне времени", + "filters": { + "startTime": "Начало", + "endTime": "Конец", + "user": "Пользователь", + "provider": "Провайдер", + "limitType": "Тип лимита", + "allUsers": "Все пользователи", + "allProviders": "Все провайдеры", + "allLimitTypes": "Все типы", + "apply": "Применить фильтры", + "reset": "Сбросить", + "loading": "Загрузка...", + "limitTypes": { + "rpm": "Лимит RPM", + "usd_5h": "Лимит расходов за 5ч", + "usd_weekly": "Недельный лимит расходов", + "usd_monthly": "Месячный лимит расходов", + "concurrent_sessions": "Лимит одновременных сессий", + "daily_quota": "Дневной лимит квоты" + } + }, + "chart": { + "title": "Хронология событий лимитов", + "description": "Почасовой тренд событий лимитов", + "total": "Всего", + "events": "Событий" + }, + "breakdown": { + "title": "Распределение по типам лимитов", + "description": "Доля событий по типам лимитов", + "total": "Всего", + "count": "Событий", + "percentage": "Доля", + "noData": "Нет данных", + "types": { + "rpm": "RPM", + "usd_5h": "Расходы 5ч", + "usd_weekly": "Недельные расходы", + "usd_monthly": "Месячные расходы", + "concurrent_sessions": "Одновременные сессии", + "daily_quota": "Дневная квота" + } + }, + "topUsers": { + "title": "Топ затронутых пользователей", + "description": "Пользователи, чаще всего попадавшие под лимиты", + "total": "Всего", + "rank": "Место", + "username": "Имя пользователя", + "eventCount": "Событий", + "percentage": "Доля", + "loading": "Загрузка...", + "noData": "Нет данных" + } + }, + "users": { + "title": "Управление пользователями", + "description": "Показано {count} пользователей", + "toolbar": { + "searchPlaceholder": "Поиск по имени, заметкам, тегам, ключам...", + "groupFilter": "Фильтр по группе", + "allGroups": "Все группы", + "tagFilter": "Фильтр по тегу", + "allTags": "Все теги", + "keyGroupFilter": "Группа ключей", + "allKeyGroups": "Все группы ключей", + "createUser": "Создать пользователя", + "createKey": "Создать ключ" + }, + "dialog": { + "userProviderGroup": "Ваши группы провайдеров", + "userProviderGroupHint": "Новые ключи могут использовать только ваши существующие группы провайдеров" + } + }, "userManagement": { "table": { "columns": { @@ -1052,6 +1137,7 @@ "keySaveFailed": "Не удалось сохранить ключ", "keyDeleteFailed": "Не удалось удалить ключ", "saveSuccess": "Изменения сохранены", + "atLeastOneKeyEnabled": "Необходимо оставить хотя бы один активный ключ", "operationFailed": "Операция не удалась", "userDisabled": "Пользователь отключен", "userEnabled": "Пользователь активирован", @@ -1279,7 +1365,32 @@ }, "keyStatus": { "enabled": "Включён", - "disabled": "Отключён" + "disabled": "Отключён", + "active": "Активен", + "expired": "Истёк", + "expiringSoon": "Скоро истечёт", + "keyEnabled": "Ключ включён", + "keyDisabled": "Ключ отключён", + "toggleKeyStatus": "Переключить статус ключа", + "clickToDisableKey": "Нажмите, чтобы отключить ключ", + "clickToEnableKey": "Нажмите, чтобы включить ключ", + "operationFailed": "Операция не удалась", + "clickToQuickRenew": "Нажмите для быстрого продления" + }, + "userStatus": { + "enabled": "Включён", + "disabled": "Отключён", + "active": "Активен", + "expired": "Истёк", + "expiringSoon": "Скоро истечёт", + "userEnabled": "Пользователь включён", + "userDisabled": "Пользователь отключён", + "toggleUserStatus": "Переключить статус пользователя", + "clickToDisableUser": "Нажмите, чтобы отключить пользователя", + "clickToEnableUser": "Нажмите, чтобы включить пользователя", + "operationFailed": "Операция не удалась", + "deleteFailed": "Не удалось удалить", + "deleteSuccess": "Удаление успешно" }, "userEditSection": { "sections": { @@ -1321,7 +1432,13 @@ "enabledDescription": "Сейчас включён. При отключении пользователь и его ключи станут недоступны.", "disabledDescription": "Сейчас отключён. При включении пользователь и его ключи станут доступны.", "confirmDisable": "Подтвердить отключение", - "confirmEnable": "Подтвердить включение" + "confirmEnable": "Подтвердить включение", + "confirmEnableTitle": "Подтвердить включение пользователя", + "confirmDisableTitle": "Подтвердить отключение пользователя", + "confirmEnableDescription": "Включение позволит пользователю и его ключам работать в обычном режиме.", + "confirmDisableDescription": "Отключение сделает пользователя и его ключи недоступными.", + "cancel": "Отмена", + "processing": "Обработка..." } }, "presetClients": { @@ -1345,7 +1462,8 @@ }, "enableStatus": { "label": "Статус включения", - "description": "Отключённые ключи не могут использоваться" + "description": "Отключённые ключи не могут использоваться", + "cannotDisableTooltip": "Невозможно отключить последний активный ключ" }, "balanceQueryPage": { "label": "Независимая страница использования", diff --git a/messages/ru/provider-chain.json b/messages/ru/provider-chain.json index 892263b49..b44a317c9 100644 --- a/messages/ru/provider-chain.json +++ b/messages/ru/provider-chain.json @@ -36,7 +36,8 @@ "requestChain": "Цепочка запросов:", "systemError": "Системная ошибка", "concurrentLimit": "Лимит параллельных запросов", - "http2Fallback": "Откат HTTP/2" + "http2Fallback": "Откат HTTP/2", + "clientError": "Ошибка клиента" }, "timeline": { "sessionReuse": "Повторное использование сессии", @@ -127,6 +128,15 @@ "requestUrl": "URL", "requestHeaders": "Заголовки", "requestBody": "Тело", - "requestBodyTruncated": "(обрезано)" + "requestBodyTruncated": "(обрезано)", + "clientErrorNonRetryable": "Ошибка клиента (попытка {attempt}, без повторов)", + "matchedRule": "Совпавшее правило ошибки", + "ruleId": "ID правила: {id}", + "ruleCategory": "Категория: {category}", + "rulePattern": "Шаблон: {pattern}", + "ruleMatchType": "Тип совпадения: {matchType}", + "ruleDescription": "Описание: {description}", + "ruleHasOverride": "Переопределения: response={response}, statusCode={statusCode}", + "clientErrorNote": "Эта ошибка вызвана вводом клиента, не повторяется и не учитывается в автомате защиты." } } diff --git a/messages/zh-CN/dashboard.json b/messages/zh-CN/dashboard.json index 8a0f7f39a..5e581a36b 100644 --- a/messages/zh-CN/dashboard.json +++ b/messages/zh-CN/dashboard.json @@ -55,6 +55,10 @@ "filters": { "user": "用户", "provider": "供应商", + "searchUser": "搜索用户...", + "searchProvider": "搜索供应商...", + "noUserFound": "未找到匹配的用户", + "noProviderFound": "未找到匹配的供应商", "model": "模型", "endpoint": "端点", "status": "状态", @@ -92,10 +96,13 @@ "endpoint": "端点", "inputTokens": "输入", "outputTokens": "输出", + "tokens": "Tokens", "cacheWrite": "缓存写入", "cacheRead": "缓存读取", + "cache": "缓存", "cost": "成本", "duration": "耗时", + "performance": "性能", "status": "状态" }, "stats": { @@ -194,6 +201,12 @@ }, "billingDetails": { "title": "计费详情" + }, + "performance": { + "title": "性能数据", + "ttfb": "首字节时间(TTFB)", + "duration": "总耗时", + "outputRate": "输出速率" } }, "providerChain": { @@ -274,7 +287,9 @@ "cacheCreationConsumedAmount": "缓存创建消耗金额", "totalConsumedAmount": "总消耗金额", "successRate": "成功率", - "avgResponseTime": "平均响应时间" + "avgResponseTime": "平均响应时间", + "avgTtfbMs": "平均 TTFB", + "avgTokensPerSecond": "平均输出速率" }, "states": { "loading": "加载中...", @@ -356,10 +371,14 @@ "noDetailedData": "暂无详细数据", "storageTip": "提示:请设置环境变量 STORE_SESSION_MESSAGES=true 以启用 messages 和 response 存储", "clientInfo": "客户端信息", + "requestHeaders": "请求头", + "requestBody": "请求体", "requestMessages": "请求 Messages", "requestMessagesDescription": "客户端发送的消息内容", - "responseBody": "响应体内容", + "responseHeaders": "响应头", + "responseBody": "响应体", "responseBodyDescription": "服务器返回的完整响应(5分钟 TTL)", + "noHeaders": "无数据", "noData": "暂无数据" }, "actions": { @@ -469,6 +488,7 @@ "usageLogs": "使用记录", "leaderboard": "排行榜", "availability": "可用性监控", + "myQuota": "我的配额", "quotasManagement": "限额管理", "providers": "供应商管理", "documentation": "文档", @@ -1145,6 +1165,7 @@ "keySaveFailed": "保存密钥失败", "keyDeleteFailed": "删除密钥失败", "saveSuccess": "保存成功", + "atLeastOneKeyEnabled": "至少需要保留一个启用的密钥", "operationFailed": "操作失败", "userDisabled": "用户已禁用", "userEnabled": "用户已启用", @@ -1371,6 +1392,9 @@ "keyStatus": { "enabled": "启用", "disabled": "禁用", + "active": "正常", + "expired": "已过期", + "expiringSoon": "即将过期", "keyEnabled": "密钥已启用", "keyDisabled": "密钥已禁用", "toggleKeyStatus": "切换密钥启用状态", @@ -1382,6 +1406,9 @@ "userStatus": { "enabled": "启用", "disabled": "禁用", + "active": "正常", + "expired": "已过期", + "expiringSoon": "即将过期", "userEnabled": "用户已启用", "userDisabled": "用户已禁用", "toggleUserStatus": "切换用户启用状态", @@ -1461,7 +1488,8 @@ }, "enableStatus": { "label": "启用状态", - "description": "禁用后此密钥将无法使用。禁用后仅管理员可启用。" + "description": "禁用后此密钥将无法使用。禁用后仅管理员可启用。", + "cannotDisableTooltip": "无法禁用最后一个启用的密钥" }, "balanceQueryPage": { "label": "独立个人用量页面", diff --git a/messages/zh-CN/provider-chain.json b/messages/zh-CN/provider-chain.json index 2680ff32c..a3c96cfb3 100644 --- a/messages/zh-CN/provider-chain.json +++ b/messages/zh-CN/provider-chain.json @@ -36,7 +36,8 @@ "requestChain": "请求链路:", "systemError": "系统错误", "concurrentLimit": "并发限制", - "http2Fallback": "HTTP/2 回退" + "http2Fallback": "HTTP/2 回退", + "clientError": "客户端错误" }, "timeline": { "sessionReuse": "会话复用", @@ -127,6 +128,15 @@ "requestUrl": "请求 URL", "requestHeaders": "请求头", "requestBody": "请求体", - "requestBodyTruncated": "(已截断)" + "requestBodyTruncated": "(已截断)", + "clientErrorNonRetryable": "客户端错误(第 {attempt} 次尝试,不可重试)", + "matchedRule": "匹配的错误规则", + "ruleId": "规则 ID: {id}", + "ruleCategory": "规则类别: {category}", + "rulePattern": "匹配模式: {pattern}", + "ruleMatchType": "匹配类型: {matchType}", + "ruleDescription": "规则描述: {description}", + "ruleHasOverride": "覆写配置: 响应体={response}, 状态码={statusCode}", + "clientErrorNote": "此错误由用户输入导致,不会重试,不计入熔断器。" } } diff --git a/messages/zh-TW/dashboard.json b/messages/zh-TW/dashboard.json index ecf9e85b2..b3a6331b8 100644 --- a/messages/zh-TW/dashboard.json +++ b/messages/zh-TW/dashboard.json @@ -55,6 +55,10 @@ "filters": { "user": "使用者", "provider": "供應商", + "searchUser": "搜尋使用者...", + "searchProvider": "搜尋供應商...", + "noUserFound": "未找到匹配的使用者", + "noProviderFound": "未找到匹配的供應商", "model": "模型", "endpoint": "端點", "status": "狀態", @@ -92,10 +96,13 @@ "endpoint": "端點", "inputTokens": "輸入", "outputTokens": "輸出", + "tokens": "Tokens", "cacheWrite": "快取寫入", "cacheRead": "快取讀取", + "cache": "快取", "cost": "成本", "duration": "耗時", + "performance": "效能", "status": "狀態" }, "stats": { @@ -194,6 +201,12 @@ }, "billingDetails": { "title": "計費詳情" + }, + "performance": { + "title": "效能資料", + "ttfb": "首字節時間(TTFB)", + "duration": "總耗時", + "outputRate": "輸出速率" } }, "providerChain": { @@ -274,7 +287,9 @@ "cacheCreationConsumedAmount": "快取建立消耗金額", "totalConsumedAmount": "總消耗金額", "successRate": "成功率", - "avgResponseTime": "平均回覆時間" + "avgResponseTime": "平均回覆時間", + "avgTtfbMs": "平均 TTFB", + "avgTokensPerSecond": "平均輸出速率" }, "states": { "loading": "載入中...", @@ -356,10 +371,14 @@ "noDetailedData": "暫無詳細資料", "storageTip": "提示:請設定環境變數 STORE_SESSION_MESSAGES=true 以啟用 messages 和 response 儲存", "clientInfo": "用戶端資訊", + "requestHeaders": "請求頭", + "requestBody": "請求體", "requestMessages": "請求訊息", "requestMessagesDescription": "用戶端傳送的訊息內容", - "responseBody": "回覆主體內容", + "responseHeaders": "響應頭", + "responseBody": "響應體", "responseBodyDescription": "伺服器傳回的完整回覆(5分鐘 TTL)", + "noHeaders": "無資料", "noData": "暫無資料" }, "actions": { @@ -469,6 +488,7 @@ "usageLogs": "使用記錄", "leaderboard": "排行榜", "availability": "可用性監控", + "myQuota": "我的額度", "quotasManagement": "額度管理", "userManagement": "使用者管理", "providers": "供應商管理", @@ -816,25 +836,6 @@ "editAriaLabel": "編輯使用者", "deleteAriaLabel": "刪除使用者" }, - "users": { - "title": "使用者管理", - "description": "顯示 {count} 位使用者", - "toolbar": { - "searchPlaceholder": "搜尋使用者名稱、備註、標籤、Key...", - "groupFilter": "依群組篩選", - "allGroups": "所有群組", - "tagFilter": "依標籤篩選", - "allTags": "所有標籤", - "keyGroupFilter": "金鑰分組", - "allKeyGroups": "所有金鑰分組", - "createUser": "建立使用者", - "createKey": "建立 Key" - }, - "dialog": { - "userProviderGroup": "您的供應商分組", - "userProviderGroupHint": "新建立的 Key 只能使用您既有的分組" - } - }, "availability": { "title": "供應商可用性監控", "description": "即時監控供應商的可用性狀態和效能指標", @@ -947,6 +948,90 @@ "refreshFailed": "重新整理失敗,請重試" } }, + "rateLimits": { + "title": "限流事件統計", + "description": "查看與分析限流事件的統計資料", + "loading": "載入中...", + "error": "載入失敗", + "totalEvents": "總事件數", + "avgUsage": "平均使用率", + "affectedUsers": "受影響使用者數", + "noData": "無資料", + "noDataHint": "在選定的時間範圍內沒有限流事件", + "filters": { + "startTime": "開始時間", + "endTime": "結束時間", + "user": "使用者", + "provider": "供應商", + "limitType": "限流類型", + "allUsers": "全部使用者", + "allProviders": "全部供應商", + "allLimitTypes": "全部類型", + "apply": "套用篩選", + "reset": "重設", + "loading": "載入中...", + "limitTypes": { + "rpm": "RPM 限流", + "usd_5h": "5 小時消費限流", + "usd_weekly": "週消費限流", + "usd_monthly": "月消費限流", + "concurrent_sessions": "並發 Session 限流", + "daily_quota": "每日額度限流" + } + }, + "chart": { + "title": "限流事件時間軸", + "description": "依小時統計的限流事件趨勢", + "total": "總計", + "events": "事件數" + }, + "breakdown": { + "title": "限流類型分佈", + "description": "不同限流類型的事件占比", + "total": "總計", + "count": "事件數", + "percentage": "占比", + "noData": "無資料", + "types": { + "rpm": "RPM 限流", + "usd_5h": "5 小時消費", + "usd_weekly": "週消費", + "usd_monthly": "月消費", + "concurrent_sessions": "並發 Session", + "daily_quota": "每日額度" + } + }, + "topUsers": { + "title": "受影響使用者排行", + "description": "觸發限流最多的使用者列表", + "total": "總計", + "rank": "排名", + "username": "使用者名稱", + "eventCount": "事件數", + "percentage": "占比", + "loading": "載入中...", + "noData": "無資料" + } + }, + "users": { + "title": "使用者管理", + "description": "顯示 {count} 位使用者", + "toolbar": { + "searchPlaceholder": "搜尋使用者名稱、備註、標籤、Key...", + "groupFilter": "依群組篩選", + "allGroups": "所有群組", + "tagFilter": "依標籤篩選", + "allTags": "所有標籤", + "keyGroupFilter": "金鑰分組", + "allKeyGroups": "所有金鑰分組", + "createUser": "建立使用者", + "createKey": "建立 Key" + }, + "dialog": { + "userProviderGroup": "您的供應商分組", + "userProviderGroupHint": "新建立的 Key 只能使用您既有的分組" + } + }, "userManagement": { "table": { "columns": { @@ -1053,6 +1138,7 @@ "keySaveFailed": "儲存金鑰失敗", "keyDeleteFailed": "刪除金鑰失敗", "saveSuccess": "儲存成功", + "atLeastOneKeyEnabled": "至少需要保留一個啟用的金鑰", "operationFailed": "操作失敗", "userDisabled": "使用者已停用", "userEnabled": "使用者已啟用", @@ -1278,7 +1364,32 @@ }, "keyStatus": { "enabled": "啟用", - "disabled": "停用" + "disabled": "停用", + "active": "正常", + "expired": "已過期", + "expiringSoon": "即將過期", + "keyEnabled": "密鑰已啟用", + "keyDisabled": "密鑰已停用", + "toggleKeyStatus": "切換密鑰狀態", + "clickToDisableKey": "點擊停用密鑰", + "clickToEnableKey": "點擊啟用密鑰", + "operationFailed": "操作失敗", + "clickToQuickRenew": "點擊快速續期" + }, + "userStatus": { + "enabled": "啟用", + "disabled": "停用", + "active": "正常", + "expired": "已過期", + "expiringSoon": "即將過期", + "userEnabled": "使用者已啟用", + "userDisabled": "使用者已停用", + "toggleUserStatus": "切換使用者狀態", + "clickToDisableUser": "點擊停用使用者", + "clickToEnableUser": "點擊啟用使用者", + "operationFailed": "操作失敗", + "deleteFailed": "刪除失敗", + "deleteSuccess": "刪除成功" }, "userEditSection": { "sections": { @@ -1320,7 +1431,13 @@ "enabledDescription": "目前已啟用,停用後該使用者及其金鑰將無法繼續使用", "disabledDescription": "目前已停用,啟用後該使用者及其金鑰將恢復正常使用", "confirmDisable": "確認停用", - "confirmEnable": "確認啟用" + "confirmEnable": "確認啟用", + "confirmEnableTitle": "確認啟用使用者", + "confirmDisableTitle": "確認停用使用者", + "confirmEnableDescription": "啟用後該使用者及其金鑰將恢復正常使用", + "confirmDisableDescription": "停用後該使用者及其金鑰將無法繼續使用", + "cancel": "取消", + "processing": "處理中..." } }, "presetClients": { @@ -1344,7 +1461,8 @@ }, "enableStatus": { "label": "啟用狀態", - "description": "停用後此金鑰將無法使用。停用後僅管理員可啟用。" + "description": "停用後此金鑰將無法使用。停用後僅管理員可啟用。", + "cannotDisableTooltip": "無法停用最後一個啟用的金鑰" }, "balanceQueryPage": { "label": "獨立個人用量頁面", diff --git a/messages/zh-TW/provider-chain.json b/messages/zh-TW/provider-chain.json index 71215c76a..9ffe2561f 100644 --- a/messages/zh-TW/provider-chain.json +++ b/messages/zh-TW/provider-chain.json @@ -36,7 +36,8 @@ "requestChain": "請求鏈路:", "systemError": "系統錯誤", "concurrentLimit": "並發限制", - "http2Fallback": "HTTP/2 回退" + "http2Fallback": "HTTP/2 回退", + "clientError": "客戶端錯誤" }, "timeline": { "sessionReuse": "會話複用", @@ -127,6 +128,15 @@ "requestUrl": "請求 URL", "requestHeaders": "請求頭", "requestBody": "請求體", - "requestBodyTruncated": "(已截斷)" + "requestBodyTruncated": "(已截斷)", + "clientErrorNonRetryable": "客戶端錯誤(第 {attempt} 次嘗試,不可重試)", + "matchedRule": "匹配的錯誤規則", + "ruleId": "規則 ID: {id}", + "ruleCategory": "規則類別: {category}", + "rulePattern": "匹配模式: {pattern}", + "ruleMatchType": "匹配類型: {matchType}", + "ruleDescription": "規則描述: {description}", + "ruleHasOverride": "覆寫設定: 回應體={response}, 狀態碼={statusCode}", + "clientErrorNote": "此錯誤由使用者輸入導致,不會重試,不計入熔斷器。" } } diff --git a/src/actions/active-sessions.ts b/src/actions/active-sessions.ts index 950602ca3..f77e7e256 100644 --- a/src/actions/active-sessions.ts +++ b/src/actions/active-sessions.ts @@ -8,6 +8,7 @@ import { setSessionDetailsCache, } from "@/lib/cache/session-cache"; import { logger } from "@/lib/logger"; +import { normalizeRequestSequence } from "@/lib/utils/request-sequence"; import type { ActiveSessionInfo } from "@/types/session"; import { summarizeTerminateSessionsBatch } from "./active-sessions-utils"; import type { ActionResult } from "./types"; @@ -512,6 +513,8 @@ export async function getSessionDetails( ActionResult<{ messages: unknown | null; response: string | null; + requestHeaders: Record | null; + responseHeaders: Record | null; sessionStats: Awaited< ReturnType > | null; @@ -572,12 +575,18 @@ export async function getSessionDetails( }; } - // 5. 并行获取 messages 和 response(不缓存,因为这些数据较大) + // 5. 解析 requestSequence:未指定时默认取当前最新请求序号 const { SessionManager } = await import("@/lib/session-manager"); - const [messages, response, requestCount] = await Promise.all([ - SessionManager.getSessionMessages(sessionId, requestSequence), - SessionManager.getSessionResponse(sessionId, requestSequence), - SessionManager.getSessionRequestCount(sessionId), + const requestCount = await SessionManager.getSessionRequestCount(sessionId); + const normalizedSequence = normalizeRequestSequence(requestSequence); + const effectiveSequence = normalizedSequence ?? (requestCount > 0 ? requestCount : undefined); + + // 6. 并行获取 messages 和 response(不缓存,因为这些数据较大) + const [messages, response, requestHeaders, responseHeaders] = await Promise.all([ + SessionManager.getSessionMessages(sessionId, effectiveSequence), + SessionManager.getSessionResponse(sessionId, effectiveSequence), + SessionManager.getSessionRequestHeaders(sessionId, effectiveSequence), + SessionManager.getSessionResponseHeaders(sessionId, effectiveSequence), ]); return { @@ -585,8 +594,10 @@ export async function getSessionDetails( data: { messages, response, + requestHeaders, + responseHeaders, sessionStats, - currentSequence: requestSequence ?? (requestCount > 0 ? requestCount : null), + currentSequence: effectiveSequence ?? null, }, }; } catch (error) { diff --git a/src/actions/error-rules.ts b/src/actions/error-rules.ts index 33f0b13b0..739a99c84 100644 --- a/src/actions/error-rules.ts +++ b/src/actions/error-rules.ts @@ -520,16 +520,16 @@ export async function testErrorRuleAction(input: { message: string }): Promise< // 3. 处理 message 为空的情况(运行时会回退到原始错误消息) const overrideErrorObj = detection.overrideResponse.error as Record; - const overrideMessage = - typeof overrideErrorObj?.message === "string" && - overrideErrorObj.message.trim().length > 0 - ? overrideErrorObj.message - : rawMessage; + const isMessageEmpty = + typeof overrideErrorObj?.message !== "string" || + overrideErrorObj.message.trim().length === 0; - if (overrideMessage === rawMessage) { + if (isMessageEmpty) { warnings.push("覆写响应的 message 为空,运行时将回退到原始错误消息"); } + const overrideMessage = isMessageEmpty ? rawMessage : overrideErrorObj.message; + // 构建最终响应(与 error-handler.ts 构建逻辑一致) finalResponse = { ...responseWithoutRequestId, diff --git a/src/actions/keys.ts b/src/actions/keys.ts index 3ca4ec564..db11e2308 100644 --- a/src/actions/keys.ts +++ b/src/actions/keys.ts @@ -85,6 +85,7 @@ export async function addKey(data: { userId: number; name: string; expiresAt?: string; + isEnabled?: boolean; canLoginWebUi?: boolean; limit5hUsd?: number | null; limitDailyUsd?: number | null; @@ -245,7 +246,7 @@ export async function addKey(data: { user_id: data.userId, name: validatedData.name, key: generatedKey, - is_enabled: true, + is_enabled: data.isEnabled ?? true, expires_at: expiresAt, can_login_web_ui: validatedData.canLoginWebUi, limit_5h_usd: validatedData.limit5hUsd, @@ -475,12 +476,16 @@ export async function removeKey(keyId: number): Promise { return { ok: false, error: "无权限执行此操作" }; } - const activeKeyCount = await countActiveKeysByUser(key.userId); - if (activeKeyCount <= 1) { - return { - ok: false, - error: "该用户至少需要保留一个可用的密钥,无法删除最后一个密钥", - }; + // 只有删除启用的密钥时,才需要检查是否是最后一个启用的密钥 + // 删除禁用的密钥不会影响用户的可用密钥数量 + if (key.isEnabled) { + const activeKeyCount = await countActiveKeysByUser(key.userId); + if (activeKeyCount <= 1) { + return { + ok: false, + error: "该用户至少需要保留一个可用的密钥,无法删除最后一个密钥", + }; + } } // 非 admin 删除时的额外检查:确保删除后用户仍有分组(防止分组被清空从而绕过限制) @@ -837,7 +842,7 @@ export async function batchUpdateKeys( const currentEnabledCount = userEnabledCounts.get(userId) ?? 0; if (currentEnabledCount - disableCount < 1) { throw new BatchUpdateError( - tError("CANNOT_DISABLE_LAST_KEY") || "无法禁用最后一个可用密钥", + tError("CANNOT_DISABLE_LAST_KEY"), ERROR_CODES.OPERATION_FAILED ); } @@ -894,7 +899,7 @@ export async function batchUpdateKeys( if (Number(remainingEnabled?.count ?? 0) < 1) { throw new BatchUpdateError( - tError("CANNOT_DISABLE_LAST_KEY") || "无法禁用最后一个可用密钥", + tError("CANNOT_DISABLE_LAST_KEY"), ERROR_CODES.OPERATION_FAILED ); } diff --git a/src/app/[locale]/dashboard/_components/dashboard-header.tsx b/src/app/[locale]/dashboard/_components/dashboard-header.tsx index 2e0d29c91..55d13cbed 100644 --- a/src/app/[locale]/dashboard/_components/dashboard-header.tsx +++ b/src/app/[locale]/dashboard/_components/dashboard-header.tsx @@ -23,7 +23,9 @@ export function DashboardHeader({ session }: DashboardHeaderProps) { { href: "/dashboard/leaderboard", label: t("leaderboard") }, { href: "/dashboard/availability", label: t("availability"), adminOnly: true }, { href: "/dashboard/providers", label: t("providers"), adminOnly: true }, - { href: "/dashboard/quotas", label: t("quotasManagement") }, + ...(isAdmin + ? [{ href: "/dashboard/quotas", label: t("quotasManagement") }] + : [{ href: "/dashboard/my-quota", label: t("myQuota") }]), { href: "/dashboard/users", label: t("userManagement") }, { href: "/usage-doc", label: t("documentation") }, { href: "/settings", label: t("systemSettings"), adminOnly: true }, diff --git a/src/app/[locale]/dashboard/_components/statistics/chart.tsx b/src/app/[locale]/dashboard/_components/statistics/chart.tsx index a83337062..001be1832 100644 --- a/src/app/[locale]/dashboard/_components/statistics/chart.tsx +++ b/src/app/[locale]/dashboard/_components/statistics/chart.tsx @@ -381,7 +381,10 @@ export function UserStatisticsChart({ )} - + )} -
+
{sortedLegendUsers.map(({ user, index }) => { const color = getUserColor(index); const userTotal = userTotals[user.dataKey] ?? { @@ -552,12 +555,14 @@ export function UserStatisticsChart({ const isSelected = selectedUserIds.has(user.id); return ( -
enableUserFilter && toggleUserSelection(user.id)} + onClick={() => toggleUserSelection(user.id)} + disabled={!enableUserFilter} + aria-pressed={isSelected} className={cn( - "rounded-md px-3 py-2 text-center transition-all min-w-16", - enableUserFilter && "cursor-pointer", + "rounded-md px-3 py-2 text-center transition-all min-w-16 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-default", isSelected ? "bg-muted/50 hover:bg-muted/70 ring-1 ring-border" : "bg-muted/10 hover:bg-muted/30 opacity-50" @@ -568,6 +573,7 @@ export function UserStatisticsChart({ -
+ ); })}
diff --git a/src/app/[locale]/dashboard/_components/user/forms/edit-key-form.tsx b/src/app/[locale]/dashboard/_components/user/forms/edit-key-form.tsx index 543d90bfd..cfdb29b72 100644 --- a/src/app/[locale]/dashboard/_components/user/forms/edit-key-form.tsx +++ b/src/app/[locale]/dashboard/_components/user/forms/edit-key-form.tsx @@ -50,7 +50,7 @@ export function EditKeyForm({ keyData, user, isAdmin = false, onSuccess }: EditK const [providerGroupSuggestions, setProviderGroupSuggestions] = useState([]); const router = useRouter(); const t = useTranslations("quota.keys.editKeyForm"); - const tKeyEdit = useTranslations("userManagement.keyEditSection.fields"); + const tKeyEdit = useTranslations("dashboard.userManagement.keyEditSection.fields"); const tUI = useTranslations("ui.tagInput"); const tCommon = useTranslations("common"); const tErrors = useTranslations("errors"); diff --git a/src/app/[locale]/dashboard/_components/user/forms/key-edit-section.tsx b/src/app/[locale]/dashboard/_components/user/forms/key-edit-section.tsx index e8ac9cffe..e45b34eed 100644 --- a/src/app/[locale]/dashboard/_components/user/forms/key-edit-section.tsx +++ b/src/app/[locale]/dashboard/_components/user/forms/key-edit-section.tsx @@ -2,9 +2,9 @@ import { format } from "date-fns"; import { Calendar, Gauge, Key, Plus, Sparkles } from "lucide-react"; -import { useEffect, useMemo, useState } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { DatePickerField } from "@/components/form/date-picker-field"; -import { TextField } from "@/components/form/form-field"; +import { TagInputField, TextField } from "@/components/form/form-field"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; @@ -16,6 +16,7 @@ import { SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; +import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import { PROVIDER_GROUP } from "@/lib/constants/provider.constants"; import { cn } from "@/lib/utils"; import { type DailyResetMode, LimitRulePicker, type LimitType } from "./limit-rule-picker"; @@ -44,6 +45,8 @@ export interface KeyEditSectionProps { }; /** Admin 可自由编辑 providerGroup */ isAdmin?: boolean; + /** 是否是最后一个启用的 key (用于禁用 Switch 防止全部禁用) */ + isLastEnabledKey?: boolean; userProviderGroup?: string; onChange: { (field: string, value: any): void; @@ -74,7 +77,11 @@ export interface KeyEditSectionProps { noGroupHint?: string; }; cacheTtl: { label: string; options: Record }; - enableStatus?: { label: string; description: string }; + enableStatus?: { + label: string; + description: string; + cannotDisableTooltip?: string; + }; }; limitRules: any; quickExpire: any; @@ -127,6 +134,7 @@ const TTL_ORDER = ["inherit", "5m", "1h"] as const; export function KeyEditSection({ keyData, isAdmin = false, + isLastEnabledKey = false, userProviderGroup, onChange, scrollRef, @@ -289,13 +297,31 @@ export function KeyEditSection({ if (!normalizedKeyProviderGroup) return []; return normalizedKeyProviderGroup.split(",").filter(Boolean); }, [normalizedKeyProviderGroup]); - const extraKeyGroupOption = useMemo(() => { + const _extraKeyGroupOption = useMemo(() => { if (!normalizedKeyProviderGroup) return null; if (normalizedKeyProviderGroup === normalizedUserProviderGroup) return null; if (userGroups.includes(normalizedKeyProviderGroup)) return null; return normalizedKeyProviderGroup; }, [normalizedKeyProviderGroup, normalizedUserProviderGroup, userGroups]); + // 普通用户选择分组时,自动移除 default + const handleUserProviderGroupChange = useCallback( + (newValue: string) => { + const groups = newValue + .split(",") + .map((g) => g.trim()) + .filter(Boolean); + // 如果有多个分组且包含 default,移除 default + if (groups.length > 1 && groups.includes(PROVIDER_GROUP.DEFAULT)) { + const withoutDefault = groups.filter((g) => g !== PROVIDER_GROUP.DEFAULT); + onChange("providerGroup", withoutDefault.join(",")); + } else { + onChange("providerGroup", newValue); + } + }, + [onChange] + ); + return (
{/* 基本信息区域 */} @@ -321,11 +347,26 @@ export function KeyEditSection({ {translations.fields.enableStatus?.description || "Disabled keys cannot be used"}

- onChange("isEnabled", checked)} - /> + + +
+ onChange("isEnabled", checked)} + /> +
+
+ {isLastEnabledKey && ( + +

+ {translations.fields.enableStatus?.cannotDisableTooltip || + "Cannot disable the last enabled key"} +

+
+ )} +
@@ -425,46 +466,42 @@ export function KeyEditSection({ /> ) : userGroups.length > 0 ? (
- - -

- {keyData.id > 0 - ? translations.fields.providerGroup.editHint || "已有密钥的分组不可修改" - : translations.fields.providerGroup.selectHint || "选择此 Key 可使用的供应商分组"} -

+ )} +
+

+ {translations.fields.providerGroup.editHint || "已有密钥的分组不可修改"} +

+ + ) : ( + // 创建模式:多选 + + )} ) : keyGroupOptions.length > 0 ? (
diff --git a/src/app/[locale]/dashboard/_components/user/forms/provider-group-select.tsx b/src/app/[locale]/dashboard/_components/user/forms/provider-group-select.tsx index f52920c02..c1849ca60 100644 --- a/src/app/[locale]/dashboard/_components/user/forms/provider-group-select.tsx +++ b/src/app/[locale]/dashboard/_components/user/forms/provider-group-select.tsx @@ -1,10 +1,11 @@ "use client"; -import { useEffect, useMemo, useState } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { toast } from "sonner"; import { getProviderGroupsWithCount } from "@/actions/providers"; import { TagInputField } from "@/components/form/form-field"; import type { TagInputSuggestion } from "@/components/ui/tag-input"; +import { PROVIDER_GROUP } from "@/lib/constants/provider.constants"; export interface ProviderGroupSelectProps { /** Comma-separated group tags. */ @@ -103,6 +104,24 @@ export function ProviderGroupSelect({ return base; }, [translations, isLoading]); + // 选择新分组后自动移除 "default" + const handleChange = useCallback( + (newValue: string) => { + const groupList = newValue + .split(",") + .map((g) => g.trim()) + .filter(Boolean); + // 如果有多个分组且包含 default,移除 default + if (groupList.length > 1 && groupList.includes(PROVIDER_GROUP.DEFAULT)) { + const withoutDefault = groupList.filter((g) => g !== PROVIDER_GROUP.DEFAULT); + onChange(withoutDefault.join(",")); + } else { + onChange(newValue); + } + }, + [onChange] + ); + return ( ); } diff --git a/src/app/[locale]/dashboard/_components/user/forms/quick-renew-key-dialog.tsx b/src/app/[locale]/dashboard/_components/user/forms/quick-renew-key-dialog.tsx index 1d87e2757..c159a8dac 100644 --- a/src/app/[locale]/dashboard/_components/user/forms/quick-renew-key-dialog.tsx +++ b/src/app/[locale]/dashboard/_components/user/forms/quick-renew-key-dialog.tsx @@ -101,6 +101,8 @@ export function QuickRenewKeyDialog({ } } const newDate = addDays(baseDate, days); + // Set to end of day to ensure full day validity + newDate.setHours(23, 59, 59, 999); const shouldEnable = !keyData.status || keyData.status === "disabled" ? enableOnRenew : undefined; const result = await onConfirm(keyData.id, newDate, shouldEnable); diff --git a/src/app/[locale]/dashboard/_components/user/forms/user-edit-section.tsx b/src/app/[locale]/dashboard/_components/user/forms/user-edit-section.tsx index 6bd3b9cb5..33cdbe3a3 100644 --- a/src/app/[locale]/dashboard/_components/user/forms/user-edit-section.tsx +++ b/src/app/[locale]/dashboard/_components/user/forms/user-edit-section.tsx @@ -14,6 +14,7 @@ import { AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; +import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; @@ -395,15 +396,20 @@ export function UserEditSection({ /> {showProviderGroup && translations.fields.providerGroup && ( - - emitChange("providerGroup", val?.trim() || PROVIDER_GROUP.DEFAULT) - } - maxLength={50} - /> +
+ +
+ {(user.providerGroup || PROVIDER_GROUP.DEFAULT) + .split(",") + .map((g) => g.trim()) + .filter(Boolean) + .map((group) => ( + + {group} + + ))} +
+
)}
diff --git a/src/app/[locale]/dashboard/_components/user/key-row-item.tsx b/src/app/[locale]/dashboard/_components/user/key-row-item.tsx index 93a612460..e2ed9e18b 100644 --- a/src/app/[locale]/dashboard/_components/user/key-row-item.tsx +++ b/src/app/[locale]/dashboard/_components/user/key-row-item.tsx @@ -92,6 +92,8 @@ export interface KeyRowItemProps { }; } +const EXPIRING_SOON_MS = 72 * 60 * 60 * 1000; // 72小时 + function splitGroups(value?: string | null): string[] { return (value ?? "") .split(",") @@ -99,6 +101,32 @@ function splitGroups(value?: string | null): string[] { .filter(Boolean); } +function formatExpiry(expiresAt: string | null | undefined, locale: string): string { + if (!expiresAt) return "-"; + const date = new Date(expiresAt); + // 如果解析失败(如"永不过期"等翻译文本),直接返回原文本 + if (Number.isNaN(date.getTime())) return expiresAt; + return formatDate(date, "yyyy-MM-dd", locale); +} + +function getKeyExpiryStatus( + status: "enabled" | "disabled", + expiresAt: string | null | undefined +): { label: string; variant: "default" | "secondary" | "destructive" | "outline" } { + if (status === "disabled") return { label: "disabled", variant: "secondary" }; + if (!expiresAt) return { label: "active", variant: "default" }; + + const date = new Date(expiresAt); + if (Number.isNaN(date.getTime())) return { label: "active", variant: "default" }; + + const now = Date.now(); + const expTs = date.getTime(); + + if (expTs <= now) return { label: "expired", variant: "destructive" }; + if (expTs - now <= EXPIRING_SOON_MS) return { label: "expiringSoon", variant: "outline" }; + return { label: "active", variant: "default" }; +} + export function KeyRowItem({ keyData, userProviderGroup: _userProviderGroup, @@ -147,7 +175,10 @@ export function KeyRowItem({ const keyGroups = splitGroups(keyData.providerGroup); const effectiveGroups = keyGroups.length > 0 ? keyGroups : [translations.defaultGroup]; - const visibleGroups = effectiveGroups.slice(0, 2); + const visibleGroups = effectiveGroups.slice(0, 1); + + // 计算 key 过期状态 + const keyExpiryStatus = getKeyExpiryStatus(localStatus, localExpiresAt); const remainingGroups = Math.max(0, effectiveGroups.length - visibleGroups.length); const effectiveGroupText = effectiveGroups.join(", "); @@ -194,13 +225,6 @@ export function KeyRowItem({ } }; - const formatExpiry = (expiresAt: string | null | undefined): string => { - if (!expiresAt) return "-"; - const date = new Date(expiresAt); - if (Number.isNaN(date.getTime())) return "-"; - return formatDate(date, "yyyy-MM-dd", locale); - }; - const handleQuickRenewConfirm = async ( _keyId: number, expiresAt: Date, @@ -278,8 +302,8 @@ export function KeyRowItem({ className={cn( "grid items-center gap-3 px-3 py-2 text-sm border-b last:border-b-0 hover:bg-muted/40 transition-colors", isMultiSelectMode - ? "grid-cols-[24px_2fr_3fr_2fr_1fr_2fr_2fr_2fr_1fr]" - : "grid-cols-[2fr_3fr_2fr_1fr_2fr_2fr_2fr_1fr]", + ? "grid-cols-[24px_2fr_3fr_3fr_1fr_2fr_1.5fr_1.5fr_1.5fr]" + : "grid-cols-[2fr_3fr_2.5fr_1fr_2fr_1.5fr_1.5fr_1.5fr]", highlight && "bg-primary/10 ring-1 ring-primary/30" )} > @@ -297,11 +321,8 @@ export function KeyRowItem({
{keyData.name}
- - {localStatus === "enabled" ? translations.status.enabled : translations.status.disabled} + + {tKeyStatus(keyExpiryStatus.label)}
@@ -444,7 +465,7 @@ export function KeyRowItem({ setQuickRenewOpen(true); }} > - {formatExpiry(localExpiresAt)} + {formatExpiry(localExpiresAt, locale)} {/* 操作 */} diff --git a/src/app/[locale]/dashboard/_components/user/unified-edit-dialog.tsx b/src/app/[locale]/dashboard/_components/user/unified-edit-dialog.tsx index 67a5ee9b6..bd20cadd8 100644 --- a/src/app/[locale]/dashboard/_components/user/unified-edit-dialog.tsx +++ b/src/app/[locale]/dashboard/_components/user/unified-edit-dialog.tsx @@ -299,6 +299,15 @@ function UnifiedEditDialogInner({ onSubmit: async (data) => { startTransition(async () => { try { + // 验证: 编辑模式下,至少需要一个启用的 key(防止用户禁用所有 key) + if (mode === "edit") { + const enabledKeyCount = data.keys.filter((k) => k.isEnabled).length; + if (enabledKeyCount === 0) { + toast.error(t("editDialog.atLeastOneKeyEnabled")); + return; + } + } + if (mode === "create") { if (isKeyOnlyMode) { const targetUserId = user?.id ?? currentUser?.id; @@ -429,6 +438,7 @@ function UnifiedEditDialogInner({ userId: user.id, name: key.name, expiresAt: key.expiresAt || undefined, + isEnabled: key.isEnabled, canLoginWebUi: key.canLoginWebUi, providerGroup: normalizeProviderGroup(key.providerGroup), cacheTtlPreference: key.cacheTtlPreference, @@ -916,6 +926,9 @@ function UnifiedEditDialogInner({ const isExpanded = mode === "create" || keys.length === 1 || expandedKeyIds.has(key.id); const showCollapseButton = mode === "edit" && keys.length > 1; + // 计算当前是否是最后一个启用的 key + const enabledKeysCount = keys.filter((k) => k.isEnabled).length; + const isLastEnabledKey = key.isEnabled && enabledKeysCount === 1; return (
, value?: any) => diff --git a/src/app/[locale]/dashboard/_components/user/user-key-table-row.tsx b/src/app/[locale]/dashboard/_components/user/user-key-table-row.tsx index fb4e3423f..9c8951a93 100644 --- a/src/app/[locale]/dashboard/_components/user/user-key-table-row.tsx +++ b/src/app/[locale]/dashboard/_components/user/user-key-table-row.tsx @@ -1,5 +1,6 @@ "use client"; +import { useQueryClient } from "@tanstack/react-query"; import { ChevronDown, ChevronRight, SquarePen } from "lucide-react"; import { useLocale, useTranslations } from "next-intl"; import { useEffect, useState, useTransition } from "react"; @@ -13,6 +14,7 @@ import { Switch } from "@/components/ui/switch"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import { useRouter } from "@/i18n/routing"; import { cn } from "@/lib/utils"; +import { getContrastTextColor, getGroupColor } from "@/lib/utils/color"; import { formatDate } from "@/lib/utils/date-format"; import type { UserDisplay } from "@/types/user"; import { KeyRowItem } from "./key-row-item"; @@ -66,6 +68,30 @@ export interface UserKeyTableRowProps { } const DEFAULT_GRID_COLUMNS_CLASS = "grid-cols-[minmax(260px,1fr)_120px_repeat(6,90px)_80px]"; +const EXPIRING_SOON_MS = 72 * 60 * 60 * 1000; // 72小时 +const MAX_VISIBLE_GROUPS = 2; // 最多显示的分组数量 + +function splitGroups(value?: string | null): string[] { + return (value ?? "") + .split(",") + .map((g) => g.trim()) + .filter(Boolean); +} + +function getExpiryStatus( + isEnabled: boolean, + expiresAt: Date | null | undefined +): { label: string; variant: "default" | "secondary" | "destructive" | "outline" } { + const now = Date.now(); + const expTs = expiresAt?.getTime(); + const hasExpiry = typeof expTs === "number" && Number.isFinite(expTs); + + if (!isEnabled) return { label: "disabled", variant: "secondary" }; + if (hasExpiry && expTs <= now) return { label: "expired", variant: "destructive" }; + if (hasExpiry && expTs - now <= EXPIRING_SOON_MS) + return { label: "expiringSoon", variant: "outline" }; + return { label: "active", variant: "default" }; +} function normalizeLimitValue(value: unknown): number | null { const raw = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN; @@ -102,6 +128,7 @@ export function UserKeyTableRow({ const tBatchEdit = useTranslations("dashboard.userManagement.batchEdit"); const tUserStatus = useTranslations("dashboard.userManagement.userStatus"); const router = useRouter(); + const queryClient = useQueryClient(); const [_isPending, startTransition] = useTransition(); const [isTogglingEnabled, setIsTogglingEnabled] = useState(false); // 乐观更新:本地状态跟踪启用状态 @@ -129,6 +156,14 @@ export function UserKeyTableRow({ const expiresText = formatExpiry(localExpiresAt ?? null, locale); + // 计算用户过期状态 + const expiryStatus = getExpiryStatus(localIsEnabled, localExpiresAt ?? null); + + // 处理 Provider Group:拆分成数组 + const userGroups = splitGroups(user.providerGroup); + const visibleGroups = userGroups.slice(0, MAX_VISIBLE_GROUPS); + const remainingGroupsCount = Math.max(0, userGroups.length - MAX_VISIBLE_GROUPS); + const limit5h = normalizeLimitValue(user.limit5hUsd); const limitDaily = normalizeLimitValue(user.dailyQuota); const limitWeekly = normalizeLimitValue(user.limitWeeklyUsd); @@ -144,6 +179,8 @@ export function UserKeyTableRow({ return; } toast.success(tUserStatus("deleteSuccess")); + // 使 React Query 缓存失效,确保数据刷新 + queryClient.invalidateQueries({ queryKey: ["users"] }); router.refresh(); }); }; @@ -222,11 +259,34 @@ export function UserKeyTableRow({ {isExpanded ? translations.collapse : translations.expand} {user.name} - {!localIsEnabled && ( + + {tUserStatus(expiryStatus.label)} + + {visibleGroups.map((group) => { + const bgColor = getGroupColor(group); + return ( + + {group} + + ); + })} + {remainingGroupsCount > 0 && ( - {translations.userStatus?.disabled || "Disabled"} + +{remainingGroupsCount} )} + {user.tags && user.tags.length > 0 && ( + + [{user.tags.join(", ")}] + + )} {user.note ? ( {user.note} ) : null} diff --git a/src/app/[locale]/dashboard/_components/user/user-management-table.tsx b/src/app/[locale]/dashboard/_components/user/user-management-table.tsx index c081ef8d2..1c8484bee 100644 --- a/src/app/[locale]/dashboard/_components/user/user-management-table.tsx +++ b/src/app/[locale]/dashboard/_components/user/user-management-table.tsx @@ -1,5 +1,6 @@ "use client"; +import { useQueryClient } from "@tanstack/react-query"; import { useVirtualizer } from "@tanstack/react-virtual"; import { Loader2, Users } from "lucide-react"; import { useRouter } from "next/navigation"; @@ -118,6 +119,7 @@ export function UserManagementTable({ translations, }: UserManagementTableProps) { const router = useRouter(); + const queryClient = useQueryClient(); const tUserList = useTranslations("dashboard.userList"); const tUserMgmt = useTranslations("dashboard.userManagement"); const isAdmin = currentUser?.role === "admin"; @@ -350,6 +352,8 @@ export function UserManagementTable({ } toast.success(tUserMgmt("quickRenew.success")); // 刷新服务端数据(成功后乐观更新状态会在useEffect中被props覆盖) + // 使 React Query 缓存失效,确保数据刷新 + queryClient.invalidateQueries({ queryKey: ["users"] }); router.refresh(); return { ok: true }; } catch (error) { diff --git a/src/app/[locale]/dashboard/leaderboard/_components/leaderboard-view.tsx b/src/app/[locale]/dashboard/leaderboard/_components/leaderboard-view.tsx index 841527fda..3e116e02f 100644 --- a/src/app/[locale]/dashboard/leaderboard/_components/leaderboard-view.tsx +++ b/src/app/[locale]/dashboard/leaderboard/_components/leaderboard-view.tsx @@ -131,7 +131,7 @@ export function LeaderboardView({ isAdmin }: LeaderboardViewProps) { scope === "user" ? 5 : scope === "provider" - ? 7 + ? 8 : scope === "providerCacheHitRate" ? 8 : scope === "model" @@ -200,10 +200,20 @@ export function LeaderboardView({ isAdmin }: LeaderboardViewProps) { cell: (row) => `${(Number((row as ProviderEntry).successRate || 0) * 100).toFixed(1)}%`, }, { - header: t("columns.avgResponseTime"), + header: t("columns.avgTtfbMs"), className: "text-right", - cell: (row) => - `${Math.round((row as ProviderEntry).avgResponseTime || 0).toLocaleString()} ms`, + cell: (row) => { + const val = (row as ProviderEntry).avgTtfbMs; + return val && val > 0 ? `${Math.round(val).toLocaleString()} ms` : "-"; + }, + }, + { + header: t("columns.avgTokensPerSecond"), + className: "text-right", + cell: (row) => { + const val = (row as ProviderEntry).avgTokensPerSecond; + return val && val > 0 ? `${val.toFixed(1)} tok/s` : "-"; + }, }, ]; diff --git a/src/app/[locale]/dashboard/logs/_components/error-details-dialog.tsx b/src/app/[locale]/dashboard/logs/_components/error-details-dialog.tsx index 6fa06f6fb..fe3779b5c 100644 --- a/src/app/[locale]/dashboard/logs/_components/error-details-dialog.tsx +++ b/src/app/[locale]/dashboard/logs/_components/error-details-dialog.tsx @@ -6,6 +6,7 @@ import { CheckCircle, DollarSign, ExternalLink, + Gauge, Loader2, Monitor, } from "lucide-react"; @@ -53,6 +54,8 @@ interface ErrorDetailsDialogProps { costUsd?: string | null; costMultiplier?: string | null; context1mApplied?: boolean | null; // 1M上下文窗口是否已应用 + durationMs?: number | null; + ttfbMs?: number | null; externalOpen?: boolean; // 外部控制弹窗开关 onExternalOpenChange?: (open: boolean) => void; // 外部控制回调 scrollToRedirect?: boolean; // 是否滚动到重定向部分 @@ -81,6 +84,8 @@ export function ErrorDetailsDialog({ costUsd, costMultiplier, context1mApplied, + durationMs, + ttfbMs, externalOpen, onExternalOpenChange, scrollToRedirect, @@ -106,6 +111,24 @@ export function ErrorDetailsDialog({ const isInProgress = !statusCode; // 没有状态码表示请求进行中 const isBlocked = !!blockedBy; // 是否被拦截 + const outputTokensPerSecond = (() => { + if ( + outputTokens === null || + outputTokens === undefined || + outputTokens <= 0 || + durationMs === null || + durationMs === undefined || + ttfbMs === null || + ttfbMs === undefined || + ttfbMs >= durationMs + ) { + return null; + } + const seconds = (durationMs - ttfbMs) / 1000; + if (seconds <= 0) return null; + return outputTokens / seconds; + })(); + // 解析 blockedReason JSON let parsedBlockedReason: { word?: string; matchType?: string; matchedText?: string } | null = null; @@ -363,106 +386,167 @@ export function ErrorDetailsDialog({
)} - {/* 计费详情 */} - {costUsd && ( -
-

- - {t("logs.details.billingDetails.title")} -

-
-
-
- {t("logs.billingDetails.input")}: - {formatTokenAmount(inputTokens)} tokens -
-
- - {t("logs.billingDetails.output")}: - - {formatTokenAmount(outputTokens)} tokens -
- {(cacheCreation5mInputTokens ?? 0) > 0 && ( -
- - {t("logs.billingDetails.cacheWrite5m")}: - - - {formatTokenAmount(cacheCreation5mInputTokens)} tokens{" "} - (1.25x) - -
- )} - {(cacheCreation1hInputTokens ?? 0) > 0 && ( -
- - {t("logs.billingDetails.cacheWrite1h")}: - - - {formatTokenAmount(cacheCreation1hInputTokens)} tokens{" "} - (2x) - -
- )} - {(cacheReadInputTokens ?? 0) > 0 && ( -
- - {t("logs.billingDetails.cacheRead")}: - - - {formatTokenAmount(cacheReadInputTokens)} tokens{" "} - (0.1x) - -
- )} - {cacheTtlApplied && ( -
- - {t("logs.billingDetails.cacheTtl")}: - - - {cacheTtlApplied} - -
- )} - {context1mApplied && ( -
- - {t("logs.billingDetails.context1m")}: - -
- - 1M Context - - - ({t("logs.billingDetails.context1mPricing")}) + {/* 计费详情 + 性能数据并排布局 */} + {(() => { + const showBilling = !!costUsd; + const showPerformance = durationMs != null || ttfbMs != null || (outputTokens ?? 0) > 0; + const showBothSections = showBilling && showPerformance; + return ( +
+ {/* 计费详情 */} + {costUsd && ( +
+

+ + {t("logs.details.billingDetails.title")} +

+
+
+
+ + {t("logs.billingDetails.input")}: + + {formatTokenAmount(inputTokens)} tokens +
+
+ + {t("logs.billingDetails.output")}: + + + {formatTokenAmount(outputTokens)} tokens + +
+ {(cacheCreation5mInputTokens ?? 0) > 0 && ( +
+ + {t("logs.billingDetails.cacheWrite5m")}: + + + {formatTokenAmount(cacheCreation5mInputTokens)} tokens{" "} + (1.25x) + +
+ )} + {(cacheCreation1hInputTokens ?? 0) > 0 && ( +
+ + {t("logs.billingDetails.cacheWrite1h")}: + + + {formatTokenAmount(cacheCreation1hInputTokens)} tokens{" "} + (2x) + +
+ )} + {(cacheReadInputTokens ?? 0) > 0 && ( +
+ + {t("logs.billingDetails.cacheRead")}: + + + {formatTokenAmount(cacheReadInputTokens)} tokens{" "} + (0.1x) + +
+ )} + {cacheTtlApplied && ( +
+ + {t("logs.billingDetails.cacheTtl")}: + + + {cacheTtlApplied} + +
+ )} + {context1mApplied && ( +
+ + {t("logs.billingDetails.context1m")}: + +
+ + 1M Context + + + ({t("logs.billingDetails.context1mPricing")}) + +
+
+ )} + {costMultiplier && parseFloat(String(costMultiplier)) !== 1.0 && ( +
+ + {t("logs.billingDetails.multiplier")}: + + + {parseFloat(String(costMultiplier)).toFixed(2)}x + +
+ )} +
+
+ {t("logs.billingDetails.totalCost")}: + + {formatCurrency(costUsd, "USD", 6)}
- )} - {costMultiplier && parseFloat(String(costMultiplier)) !== 1.0 && ( -
- - {t("logs.billingDetails.multiplier")}: - - - {parseFloat(String(costMultiplier)).toFixed(2)}x - +
+ )} + + {/* 性能数据 */} + {(durationMs != null || ttfbMs != null || (outputTokens ?? 0) > 0) && ( +
+

+ + {t("logs.details.performance.title")} +

+
+
+
+ + {t("logs.details.performance.ttfb")}: + + + {ttfbMs != null ? `${Math.round(ttfbMs).toLocaleString()} ms` : "-"} + +
+
+ + {t("logs.details.performance.duration")}: + + + {durationMs != null + ? `${Math.round(durationMs).toLocaleString()} ms` + : "-"} + +
+
+ + {t("logs.details.performance.outputRate")}: + + + {outputTokensPerSecond !== null + ? `${outputTokensPerSecond.toFixed(1)} tok/s` + : "-"} + +
+
- )} -
-
- {t("logs.billingDetails.totalCost")}: - - {formatCurrency(costUsd, "USD", 6)} - -
+
+ )}
-
- )} + ); + })()} {/* 模型重定向信息 */} {originalModel && currentModel && originalModel !== currentModel && ( diff --git a/src/app/[locale]/dashboard/logs/_components/usage-logs-filters.tsx b/src/app/[locale]/dashboard/logs/_components/usage-logs-filters.tsx index 9848168b7..4a3a0ec9e 100644 --- a/src/app/[locale]/dashboard/logs/_components/usage-logs-filters.tsx +++ b/src/app/[locale]/dashboard/logs/_components/usage-logs-filters.tsx @@ -1,7 +1,7 @@ "use client"; import { addDays, format, parse } from "date-fns"; -import { Download } from "lucide-react"; +import { Check, ChevronsUpDown, Download } from "lucide-react"; import { useTranslations } from "next-intl"; import { useCallback, useEffect, useMemo, useState } from "react"; @@ -9,8 +9,17 @@ import { toast } from "sonner"; import { getKeys } from "@/actions/keys"; import { exportUsageLogs } from "@/actions/usage-logs"; import { Button } from "@/components/ui/button"; +import { + Command, + CommandEmpty, + CommandGroup, + CommandInput, + CommandItem, + CommandList, +} from "@/components/ui/command"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; +import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Select, SelectContent, @@ -96,9 +105,18 @@ export function UsageLogsFilters({ return dynamicOnly; }, [dynamicStatusCodes]); + const userMap = useMemo(() => new Map(users.map((user) => [user.id, user.name])), [users]); + + const providerMap = useMemo( + () => new Map(providers.map((provider) => [provider.id, provider.name])), + [providers] + ); + const [keys, setKeys] = useState(initialKeys); const [localFilters, setLocalFilters] = useState(filters); const [isExporting, setIsExporting] = useState(false); + const [userPopoverOpen, setUserPopoverOpen] = useState(false); + const [providerPopoverOpen, setProviderPopoverOpen] = useState(false); useEffect(() => { if (initialKeys.length > 0) { @@ -263,26 +281,71 @@ export function UsageLogsFilters({ {isAdmin && (
- + + + + + e.stopPropagation()} + onTouchMove={(e) => e.stopPropagation()} + > + + + + + {isUsersLoading ? t("logs.stats.loading") : t("logs.filters.noUserFound")} + + + { + void handleUserChange(""); + setUserPopoverOpen(false); + }} + className="cursor-pointer" + > + {t("logs.filters.allUsers")} + {!localFilters.userId && } + + {users.map((user) => ( + { + void handleUserChange(user.id.toString()); + setUserPopoverOpen(false); + }} + className="cursor-pointer" + > + {user.name} + {localFilters.userId === user.id && ( + + )} + + ))} + + + + +
)} @@ -324,31 +387,81 @@ export function UsageLogsFilters({ {isAdmin && (
- + + + + + e.stopPropagation()} + onTouchMove={(e) => e.stopPropagation()} + > + + + + + {isProvidersLoading + ? t("logs.stats.loading") + : t("logs.filters.noProviderFound")} + + + { + setLocalFilters({ + ...localFilters, + providerId: undefined, + }); + setProviderPopoverOpen(false); + }} + className="cursor-pointer" + > + {t("logs.filters.allProviders")} + {!localFilters.providerId && } + + {providers.map((provider) => ( + { + setLocalFilters({ + ...localFilters, + providerId: provider.id, + }); + setProviderPopoverOpen(false); + }} + className="cursor-pointer" + > + {provider.name} + {localFilters.providerId === provider.id && ( + + )} + + ))} + + + + +
)} diff --git a/src/app/[locale]/dashboard/logs/_components/usage-logs-table.tsx b/src/app/[locale]/dashboard/logs/_components/usage-logs-table.tsx index db79816ff..2f4c43de5 100644 --- a/src/app/[locale]/dashboard/logs/_components/usage-logs-table.tsx +++ b/src/app/[locale]/dashboard/logs/_components/usage-logs-table.tsx @@ -17,6 +17,11 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/comp import { cn, formatTokenAmount } from "@/lib/utils"; import type { CurrencyCode } from "@/lib/utils/currency"; import { formatCurrency } from "@/lib/utils/currency"; +import { + calculateOutputRate, + formatDuration, + NON_BILLING_ENDPOINT, +} from "@/lib/utils/performance-formatter"; import { formatProviderSummary } from "@/lib/utils/provider-chain-formatter"; import type { UsageLogRow } from "@/repository/usage-logs"; import type { BillingModelSource } from "@/types/system-config"; @@ -24,25 +29,6 @@ import { ErrorDetailsDialog } from "./error-details-dialog"; import { ModelDisplayWithRedirect } from "./model-display-with-redirect"; import { ProviderChainPopover } from "./provider-chain-popover"; -const NON_BILLING_ENDPOINT = "/v1/messages/count_tokens"; - -/** - * 格式化请求耗时 - * - 1000ms 以上显示为秒(如 "1.23s") - * - 1000ms 以下显示为毫秒(如 "850ms") - */ -function formatDuration(durationMs: number | null): string { - if (!durationMs) return "-"; - - // 1000ms 以上转换为秒 - if (durationMs >= 1000) { - return `${(Number(durationMs) / 1000).toFixed(2)}s`; - } - - // 1000ms 以下显示毫秒 - return `${durationMs}ms`; -} - interface UsageLogsTableProps { logs: UsageLogRow[]; total: number; @@ -87,19 +73,17 @@ export function UsageLogsTable({ {t("logs.columns.key")} {t("logs.columns.provider")} {t("logs.columns.model")} - {t("logs.columns.inputTokens")} - {t("logs.columns.outputTokens")} - {t("logs.columns.cacheWrite")} - {t("logs.columns.cacheRead")} + {t("logs.columns.tokens")} + {t("logs.columns.cache")} {t("logs.columns.cost")} - {t("logs.columns.duration")} + {t("logs.columns.performance")} {t("logs.columns.status")} {logs.length === 0 ? ( - + {t("logs.table.noData")} @@ -229,17 +213,35 @@ export function UsageLogsTable({ - {formatTokenAmount(log.inputTokens)} - - - {formatTokenAmount(log.outputTokens)} + + + + + {formatTokenAmount(log.inputTokens)} /{" "} + {formatTokenAmount(log.outputTokens)} + + + +
+ {t("logs.billingDetails.input")}: {formatTokenAmount(log.inputTokens)} +
+
+ {t("logs.billingDetails.output")}:{" "} + {formatTokenAmount(log.outputTokens)} +
+
+
+
- {formatTokenAmount(log.cacheCreationInputTokens)} + + {formatTokenAmount(log.cacheCreationInputTokens)} /{" "} + {formatTokenAmount(log.cacheReadInputTokens)} + {log.cacheTtlApplied ? ( {log.cacheTtlApplied} @@ -248,15 +250,21 @@ export function UsageLogsTable({
-
5m: {formatTokenAmount(log.cacheCreation5mInputTokens)}
-
1h: {formatTokenAmount(log.cacheCreation1hInputTokens)}
+
{t("logs.columns.cacheWrite")}
+
+ 5m: {formatTokenAmount(log.cacheCreation5mInputTokens)} +
+
+ 1h: {formatTokenAmount(log.cacheCreation1hInputTokens)} +
+
{t("logs.columns.cacheRead")}
+
+ {formatTokenAmount(log.cacheReadInputTokens)} +
- - {formatTokenAmount(log.cacheReadInputTokens)} - {isNonBilling ? ( "-" @@ -349,7 +357,56 @@ export function UsageLogsTable({ )} - {formatDuration(log.durationMs)} + {(() => { + const rate = calculateOutputRate( + log.outputTokens, + log.durationMs, + log.ttfbMs + ); + const secondLine = [ + log.ttfbMs != null && + log.ttfbMs > 0 && + `TTFB ${formatDuration(log.ttfbMs)}`, + rate !== null && `${rate.toFixed(0)} tok/s`, + ] + .filter(Boolean) + .join(" | "); + + return ( + + + +
+ {formatDuration(log.durationMs)} + {secondLine && ( + + {secondLine} + + )} +
+
+ +
+ {t("logs.details.performance.duration")}:{" "} + {formatDuration(log.durationMs)} +
+ {log.ttfbMs != null && ( +
+ {t("logs.details.performance.ttfb")}:{" "} + {formatDuration(log.ttfbMs)} +
+ )} + {rate !== null && ( +
+ {t("logs.details.performance.outputRate")}: {rate.toFixed(1)}{" "} + tok/s +
+ )} +
+
+
+ ); + })()}
{ if (!open) setDialogState({ logId: null, scrollToRedirect: false }); diff --git a/src/app/[locale]/dashboard/logs/_components/virtualized-logs-table.tsx b/src/app/[locale]/dashboard/logs/_components/virtualized-logs-table.tsx index 0a3e3e13a..952a177bb 100644 --- a/src/app/[locale]/dashboard/logs/_components/virtualized-logs-table.tsx +++ b/src/app/[locale]/dashboard/logs/_components/virtualized-logs-table.tsx @@ -13,27 +13,20 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/comp import { cn, formatTokenAmount } from "@/lib/utils"; import type { CurrencyCode } from "@/lib/utils/currency"; import { formatCurrency } from "@/lib/utils/currency"; +import { + calculateOutputRate, + formatDuration, + NON_BILLING_ENDPOINT, +} from "@/lib/utils/performance-formatter"; import { formatProviderSummary } from "@/lib/utils/provider-chain-formatter"; import type { BillingModelSource } from "@/types/system-config"; import { ErrorDetailsDialog } from "./error-details-dialog"; import { ModelDisplayWithRedirect } from "./model-display-with-redirect"; import { ProviderChainPopover } from "./provider-chain-popover"; -const NON_BILLING_ENDPOINT = "/v1/messages/count_tokens"; const BATCH_SIZE = 50; const ROW_HEIGHT = 52; // Estimated row height in pixels -/** - * Format duration - */ -function formatDuration(durationMs: number | null): string { - if (!durationMs) return "-"; - if (durationMs >= 1000) { - return `${(Number(durationMs) / 1000).toFixed(2)}s`; - } - return `${durationMs}ms`; -} - export interface VirtualizedLogsTableFilters { userId?: number; keyId?: number; @@ -177,67 +170,49 @@ export function VirtualizedLogsTable({ {/* Fixed header */}
-
+
{t("logs.columns.time")}
-
+
{t("logs.columns.user")}
-
+
{t("logs.columns.key")}
{t("logs.columns.provider")}
-
+
{t("logs.columns.model")}
- {t("logs.columns.inputTokens")} -
-
- {t("logs.columns.outputTokens")} -
-
- {t("logs.columns.cacheWrite")} + {t("logs.columns.tokens")}
- {t("logs.columns.cacheRead")} + {t("logs.columns.cache")}
{t("logs.columns.cost")}
- {t("logs.columns.duration")} + {t("logs.columns.performance")}
-
+
{t("logs.columns.status")}
@@ -294,102 +269,102 @@ export function VirtualizedLogsTable({ )} > {/* Time */} -
+
{/* User */} -
+
{log.userName}
{/* Key */}
{log.keyName}
{/* Provider */} -
+
{log.blockedBy ? ( {t("logs.table.blocked")} ) : ( -
-
+
+
{log.providerChain && log.providerChain.length > 0 ? ( - <> - - {formatProviderSummary(log.providerChain, tChain) && ( - - - - - {formatProviderSummary(log.providerChain, tChain)} - - - -

- {formatProviderSummary(log.providerChain, tChain)} -

-
-
-
- )} - + ) : ( {log.providerName || "-"} )} + {/* Cost multiplier badge */} + {(() => { + const successfulProvider = + log.providerChain && log.providerChain.length > 0 + ? [...log.providerChain] + .reverse() + .find( + (item) => + item.reason === "request_success" || + item.reason === "retry_success" + ) + : null; + const actualCostMultiplier = + successfulProvider?.costMultiplier ?? log.costMultiplier; + return actualCostMultiplier && + parseFloat(String(actualCostMultiplier)) !== 1.0 ? ( + 1.0 + ? "text-xs bg-orange-50 text-orange-700 border-orange-200 dark:bg-orange-950/30 dark:text-orange-300 dark:border-orange-800 shrink-0" + : "text-xs bg-green-50 text-green-700 border-green-200 dark:bg-green-950/30 dark:text-green-300 dark:border-green-800 shrink-0" + } + > + x{parseFloat(String(actualCostMultiplier)).toFixed(2)} + + ) : null; + })()}
- {/* Cost multiplier badge */} - {(() => { - const successfulProvider = - log.providerChain && log.providerChain.length > 0 - ? [...log.providerChain] - .reverse() - .find( - (item) => - item.reason === "request_success" || - item.reason === "retry_success" - ) - : null; - const actualCostMultiplier = - successfulProvider?.costMultiplier ?? log.costMultiplier; - return actualCostMultiplier && - parseFloat(String(actualCostMultiplier)) !== 1.0 ? ( - 1.0 - ? "text-xs bg-orange-50 text-orange-700 border-orange-200 dark:bg-orange-950/30 dark:text-orange-300 dark:border-orange-800 shrink-0" - : "text-xs bg-green-50 text-green-700 border-green-200 dark:bg-green-950/30 dark:text-green-300 dark:border-green-800 shrink-0" - } - > - x{parseFloat(String(actualCostMultiplier)).toFixed(2)} - - ) : null; - })()} + {log.providerChain && + log.providerChain.length > 0 && + formatProviderSummary(log.providerChain, tChain) && ( + + + + + {formatProviderSummary(log.providerChain, tChain)} + + + +

+ {formatProviderSummary(log.providerChain, tChain)} +

+
+
+
+ )}
)}
{/* Model */} -
+
@@ -411,23 +386,38 @@ export function VirtualizedLogsTable({
- {/* Input Tokens */} -
- {formatTokenAmount(log.inputTokens)} -
- - {/* Output Tokens */} -
- {formatTokenAmount(log.outputTokens)} + {/* Tokens */} +
+ + + + + {formatTokenAmount(log.inputTokens)} /{" "} + {formatTokenAmount(log.outputTokens)} + + + +
+ {t("logs.billingDetails.input")}: {formatTokenAmount(log.inputTokens)} +
+
+ {t("logs.billingDetails.output")}: {formatTokenAmount(log.outputTokens)} +
+
+
+
- {/* Cache Write */} -
+ {/* Cache */} +
- {formatTokenAmount(log.cacheCreationInputTokens)} + + {formatTokenAmount(log.cacheCreationInputTokens)} /{" "} + {formatTokenAmount(log.cacheReadInputTokens)} + {log.cacheTtlApplied ? ( {log.cacheTtlApplied} @@ -436,20 +426,22 @@ export function VirtualizedLogsTable({
-
5m: {formatTokenAmount(log.cacheCreation5mInputTokens)}
-
1h: {formatTokenAmount(log.cacheCreation1hInputTokens)}
+
{t("logs.columns.cacheWrite")}
+
+ 5m: {formatTokenAmount(log.cacheCreation5mInputTokens)} +
+
+ 1h: {formatTokenAmount(log.cacheCreation1hInputTokens)} +
+
{t("logs.columns.cacheRead")}
+
{formatTokenAmount(log.cacheReadInputTokens)}
- {/* Cache Read */} -
- {formatTokenAmount(log.cacheReadInputTokens)} -
- {/* Cost */} -
+
{isNonBilling ? ( "-" ) : log.costUsd ? ( @@ -490,13 +482,63 @@ export function VirtualizedLogsTable({ )}
- {/* Duration */} -
- {formatDuration(log.durationMs)} + {/* Performance */} +
+ {(() => { + const rate = calculateOutputRate( + log.outputTokens, + log.durationMs, + log.ttfbMs + ); + const ttfbLine = + log.ttfbMs != null && log.ttfbMs > 0 + ? `TTFB ${formatDuration(log.ttfbMs)}` + : null; + const rateLine = rate !== null ? `${rate.toFixed(0)} tok/s` : null; + + return ( + + + +
+ {formatDuration(log.durationMs)} + {ttfbLine && ( + + {ttfbLine} + + )} + {rateLine && ( + + {rateLine} + + )} +
+
+ +
+ {t("logs.details.performance.duration")}:{" "} + {formatDuration(log.durationMs)} +
+ {log.ttfbMs != null && ( +
+ {t("logs.details.performance.ttfb")}: {formatDuration(log.ttfbMs)} +
+ )} + {rate !== null && ( +
+ {t("logs.details.performance.outputRate")}: {rate.toFixed(1)}{" "} + tok/s +
+ )} +
+
+
+ ); + })()}
{/* Status */} -
+
{ if (!open) setDialogState({ logId: null, scrollToRedirect: false }); diff --git a/src/app/[locale]/dashboard/my-quota/page.tsx b/src/app/[locale]/dashboard/my-quota/page.tsx new file mode 100644 index 000000000..19d9d2f0d --- /dev/null +++ b/src/app/[locale]/dashboard/my-quota/page.tsx @@ -0,0 +1,46 @@ +import { AlertCircle } from "lucide-react"; +import { getTranslations } from "next-intl/server"; +import { getMyQuota } from "@/actions/my-usage"; +import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; +import { getSystemSettings } from "@/repository/system-config"; +import { QuotaCards } from "../../my-usage/_components/quota-cards"; + +export const dynamic = "force-dynamic"; + +export default async function MyQuotaPage({ params }: { params: Promise<{ locale: string }> }) { + // Await params to ensure locale is available in the async context + await params; + + const [quotaResult, systemSettings, tNav, tCommon] = await Promise.all([ + getMyQuota(), + getSystemSettings(), + getTranslations("dashboard.nav"), + getTranslations("common"), + ]); + + // Handle error state + if (!quotaResult.ok) { + return ( +
+
+

{tNav("myQuota")}

+
+ + + {tCommon("error")} + {quotaResult.error} + +
+ ); + } + + return ( +
+
+

{tNav("myQuota")}

+
+ + +
+ ); +} diff --git a/src/app/[locale]/dashboard/quotas/keys/page.tsx b/src/app/[locale]/dashboard/quotas/keys/page.tsx index 2f543ccb7..9147e67af 100644 --- a/src/app/[locale]/dashboard/quotas/keys/page.tsx +++ b/src/app/[locale]/dashboard/quotas/keys/page.tsx @@ -11,7 +11,7 @@ export default async function KeysQuotaPage({ params }: { params: Promise<{ loca // 权限检查:仅 admin 用户可访问 if (!session || session.user.role !== "admin") { - redirect({ href: session ? "/dashboard" : "/login", locale }); + redirect({ href: session ? "/dashboard/my-quota" : "/login", locale }); } redirect({ href: "/dashboard/quotas/users", locale }); diff --git a/src/app/[locale]/dashboard/quotas/page.tsx b/src/app/[locale]/dashboard/quotas/page.tsx index 2cd87d1a2..f9465cd13 100644 --- a/src/app/[locale]/dashboard/quotas/page.tsx +++ b/src/app/[locale]/dashboard/quotas/page.tsx @@ -11,7 +11,7 @@ export default async function QuotasPage({ params }: { params: Promise<{ locale: } if (session.user.role !== "admin") { - return redirect({ href: "/my-usage", locale }); + return redirect({ href: "/dashboard/my-quota", locale }); } return redirect({ href: "/dashboard/quotas/users", locale }); diff --git a/src/app/[locale]/dashboard/quotas/providers/page.tsx b/src/app/[locale]/dashboard/quotas/providers/page.tsx index c93a2c644..8086ce29e 100644 --- a/src/app/[locale]/dashboard/quotas/providers/page.tsx +++ b/src/app/[locale]/dashboard/quotas/providers/page.tsx @@ -50,7 +50,7 @@ export default async function ProvidersQuotaPage({ // 权限检查:仅 admin 用户可访问 if (!session || session.user.role !== "admin") { - redirect({ href: session ? "/dashboard" : "/login", locale }); + redirect({ href: session ? "/dashboard/my-quota" : "/login", locale }); } const t = await getTranslations("quota.providers"); diff --git a/src/app/[locale]/dashboard/quotas/users/page.tsx b/src/app/[locale]/dashboard/quotas/users/page.tsx index e7083975c..e1207465f 100644 --- a/src/app/[locale]/dashboard/quotas/users/page.tsx +++ b/src/app/[locale]/dashboard/quotas/users/page.tsx @@ -81,7 +81,7 @@ export default async function UsersQuotaPage({ params }: { params: Promise<{ loc // 权限检查:仅 admin 用户可访问 if (!session || session.user.role !== "admin") { - return redirect({ href: session ? "/dashboard" : "/login", locale }); + return redirect({ href: session ? "/dashboard/my-quota" : "/login", locale }); } const t = await getTranslations("quota.users"); diff --git a/src/app/[locale]/dashboard/sessions/[sessionId]/messages/_components/session-messages-client.tsx b/src/app/[locale]/dashboard/sessions/[sessionId]/messages/_components/session-messages-client.tsx index 84dca3bb8..2361137b4 100644 --- a/src/app/[locale]/dashboard/sessions/[sessionId]/messages/_components/session-messages-client.tsx +++ b/src/app/[locale]/dashboard/sessions/[sessionId]/messages/_components/session-messages-client.tsx @@ -21,6 +21,7 @@ import { import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { usePathname, useRouter } from "@/i18n/routing"; import { type CurrencyCode, formatCurrency } from "@/lib/utils/currency"; import { RequestListSidebar } from "./request-list-sidebar"; @@ -50,10 +51,17 @@ export function SessionMessagesClient() { // 从 URL 获取当前选中的请求序号 const seqParam = searchParams.get("seq"); - const selectedSeq = seqParam ? parseInt(seqParam, 10) : null; + const selectedSeq = (() => { + if (!seqParam) return null; + const parsed = Number.parseInt(seqParam, 10); + if (!Number.isFinite(parsed) || parsed <= 0) return null; + return parsed; + })(); const [messages, setMessages] = useState(null); const [response, setResponse] = useState(null); + const [requestHeaders, setRequestHeaders] = useState | null>(null); + const [responseHeaders, setResponseHeaders] = useState | null>(null); const [sessionStats, setSessionStats] = useState< Extract>, { ok: true }>["data"]["sessionStats"] @@ -95,6 +103,8 @@ export function SessionMessagesClient() { if (result.ok) { setMessages(result.data.messages); setResponse(result.data.response); + setRequestHeaders(result.data.requestHeaders); + setResponseHeaders(result.data.responseHeaders); setSessionStats(result.data.sessionStats); setCurrentSequence(result.data.currentSequence); } else { @@ -287,63 +297,84 @@ export function SessionMessagesClient() { )} - {/* Messages 数据 */} - {messages !== null && ( -
-
-
-                        {JSON.stringify(messages, null, 2)}
-                      
-
-
- )} + + + {t("details.requestHeaders")} + {t("details.requestBody")} + + {t("details.responseHeaders")} + + {t("details.responseBody")} + + + + + + + + {messages === null ? ( +
{t("details.noData")}
+ ) : ( +
+
+                          {JSON.stringify(messages, null, 2)}
+                        
+
+ )} +
- {/* Response Body */} - {response !== null && ( -
- {copiedResponse ? ( - <> - - {t("actions.copied")} - - ) : ( - <> - - {t("actions.copyResponse")} - - )} - - } - > -
-
-                        {formatResponse(response)}
-                      
-
-
- )} + + + + + + {response === null ? ( +
{t("details.noData")}
+ ) : ( +
+
+ +
+
+
+                            {formatResponse(response)}
+                          
+
+
+ )} +
+
{/* 无数据提示 */} - {!sessionStats?.userAgent && !messages && !response && ( -
-
- {t("details.noDetailedData")} + {!sessionStats?.userAgent && + !messages && + !response && + !requestHeaders && + !responseHeaders && ( +
+
+ {t("details.noDetailedData")} +
+

{t("details.storageTip")}

-

{t("details.storageTip")}

-
- )} + )}
{/* 右侧:信息卡片(占 1 列)*/} @@ -583,3 +614,19 @@ export function SessionMessagesClient() {
); } + +function HeadersDisplay({ headers }: { headers: Record | null }) { + const t = useTranslations("dashboard.sessions"); + if (!headers || Object.keys(headers).length === 0) { + return
{t("details.noHeaders")}
; + } + return ( +
+
+        {Object.entries(headers)
+          .map(([key, value]) => `${key}: ${value}`)
+          .join("\n")}
+      
+
+ ); +} diff --git a/src/app/v1/_lib/codex/__tests__/session-extractor.test.ts b/src/app/v1/_lib/codex/__tests__/session-extractor.test.ts new file mode 100644 index 000000000..76f7f67d4 --- /dev/null +++ b/src/app/v1/_lib/codex/__tests__/session-extractor.test.ts @@ -0,0 +1,148 @@ +import { describe, expect, test } from "vitest"; +import { extractCodexSessionId, isCodexClient } from "../session-extractor"; + +describe("Codex session extractor", () => { + test("extracts from header session_id", () => { + const headerSessionId = "sess_123456789012345678901"; + const result = extractCodexSessionId( + new Headers({ session_id: headerSessionId }), + { + metadata: { session_id: "sess_aaaaaaaaaaaaaaaaaaaaa" }, + previous_response_id: "resp_123456789012345678901", + }, + "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" + ); + + expect(result.sessionId).toBe(headerSessionId); + expect(result.source).toBe("header_session_id"); + }); + + test("extracts from header x-session-id", () => { + const headerSessionId = "sess_123456789012345678902"; + const result = extractCodexSessionId( + new Headers({ "x-session-id": headerSessionId }), + { + metadata: { session_id: "sess_aaaaaaaaaaaaaaaaaaaaa" }, + previous_response_id: "resp_123456789012345678901", + }, + "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" + ); + + expect(result.sessionId).toBe(headerSessionId); + expect(result.source).toBe("header_x_session_id"); + }); + + test("extracts from body metadata.session_id", () => { + const bodySessionId = "sess_123456789012345678903"; + const result = extractCodexSessionId( + new Headers(), + { metadata: { session_id: bodySessionId } }, + "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" + ); + + expect(result.sessionId).toBe(bodySessionId); + expect(result.source).toBe("body_metadata_session_id"); + }); + + test("falls back to previous_response_id", () => { + const previousResponseId = "resp_123456789012345678901"; + const result = extractCodexSessionId( + new Headers(), + { previous_response_id: previousResponseId }, + "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" + ); + + expect(result.sessionId).toBe(`codex_prev_${previousResponseId}`); + expect(result.source).toBe("body_previous_response_id"); + }); + + test("rejects previous_response_id that would exceed 256 after prefix", () => { + const longId = "a".repeat(250); // 250 + 11 (prefix) = 261 > 256 + const result = extractCodexSessionId(new Headers(), { previous_response_id: longId }, null); + expect(result.sessionId).toBe(null); + expect(result.source).toBe(null); + }); + + test("respects extraction priority", () => { + const sessionIdFromHeader = "sess_123456789012345678904"; + const xSessionIdFromHeader = "sess_123456789012345678905"; + const sessionIdFromBody = "sess_123456789012345678906"; + const previousResponseId = "resp_123456789012345678901"; + + const result = extractCodexSessionId( + new Headers({ + session_id: sessionIdFromHeader, + "x-session-id": xSessionIdFromHeader, + }), + { + metadata: { session_id: sessionIdFromBody }, + previous_response_id: previousResponseId, + }, + "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" + ); + + expect(result.sessionId).toBe(sessionIdFromHeader); + expect(result.source).toBe("header_session_id"); + }); + + test("detects Codex client User-Agent", () => { + expect(isCodexClient("codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)")).toBe(true); + expect(isCodexClient("codex_vscode/0.35.0 (Windows 10.0.26100; x86_64)")).toBe(true); + expect(isCodexClient("Mozilla/5.0")).toBe(false); + expect(isCodexClient(null)).toBe(false); + }); + + test("rejects session_id shorter than 21 characters", () => { + const result = extractCodexSessionId( + new Headers({ session_id: "short_id_12345" }), // 14 chars + {}, + null + ); + expect(result.sessionId).toBe(null); + expect(result.source).toBe(null); + }); + + test("accepts session_id with exactly 21 characters (minimum)", () => { + const minId = "a".repeat(21); + const result = extractCodexSessionId(new Headers({ session_id: minId }), {}, null); + expect(result.sessionId).toBe(minId); + expect(result.source).toBe("header_session_id"); + }); + + test("accepts session_id with exactly 256 characters (maximum)", () => { + const maxId = "a".repeat(256); + const result = extractCodexSessionId(new Headers({ session_id: maxId }), {}, null); + expect(result.sessionId).toBe(maxId); + expect(result.source).toBe("header_session_id"); + }); + + test("rejects session_id longer than 256 characters", () => { + const longId = "a".repeat(300); + const result = extractCodexSessionId(new Headers({ session_id: longId }), {}, null); + expect(result.sessionId).toBe(null); + expect(result.source).toBe(null); + }); + + test("rejects session_id with invalid characters", () => { + // Test with body metadata to avoid Headers normalization + const result = extractCodexSessionId( + new Headers(), + { metadata: { session_id: "sess_123456789@#$%^&*()!" } }, + null + ); + expect(result.sessionId).toBe(null); + }); + + test("accepts session_id with allowed special characters", () => { + const validId = "sess-123_456.789:abc012345"; + const result = extractCodexSessionId(new Headers({ session_id: validId }), {}, null); + expect(result.sessionId).toBe(validId); + }); + + test("returns null when no valid session_id found", () => { + const result = extractCodexSessionId(new Headers(), {}, "codex_cli_rs/0.50.0"); + expect(result.sessionId).toBe(null); + expect(result.source).toBe(null); + expect(result.isCodexClient).toBe(true); + }); +}); diff --git a/src/app/v1/_lib/codex/session-extractor.ts b/src/app/v1/_lib/codex/session-extractor.ts new file mode 100644 index 000000000..6a82d064f --- /dev/null +++ b/src/app/v1/_lib/codex/session-extractor.ts @@ -0,0 +1,112 @@ +import "server-only"; + +export type CodexSessionIdSource = + | "header_session_id" + | "header_x_session_id" + | "body_metadata_session_id" + | "body_previous_response_id" + | null; + +export interface CodexSessionExtractionResult { + sessionId: string | null; + source: CodexSessionIdSource; + isCodexClient: boolean; +} + +// Session ID validation constants +const CODEX_SESSION_ID_MIN_LENGTH = 21; // Codex session_id typically > 20 chars (UUID-like) +const CODEX_SESSION_ID_MAX_LENGTH = 256; // Prevent Redis key bloat from malicious input +const SESSION_ID_PATTERN = /^[\w\-.:]+$/; // Alphanumeric, dash, dot, colon only + +// Codex CLI User-Agent pattern (pre-compiled for performance) +const CODEX_CLI_PATTERN = /^(codex_vscode|codex_cli_rs)\/[\d.]+/i; + +export function normalizeCodexSessionId(value: unknown): string | null { + if (typeof value !== "string") return null; + + const trimmed = value.trim(); + if (!trimmed) return null; + + if (trimmed.length < CODEX_SESSION_ID_MIN_LENGTH) return null; + if (trimmed.length > CODEX_SESSION_ID_MAX_LENGTH) return null; + if (!SESSION_ID_PATTERN.test(trimmed)) return null; + + return trimmed; +} + +function parseMetadata(requestBody: Record): Record | null { + const metadata = requestBody.metadata; + if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) return null; + return metadata as Record; +} + +/** + * Detect official Codex CLI clients by User-Agent. + * + * Examples: + * - codex_vscode/0.35.0 (...) + * - codex_cli_rs/0.50.0 (...) + */ +export function isCodexClient(userAgent: string | null): boolean { + if (!userAgent) return false; + return CODEX_CLI_PATTERN.test(userAgent); +} + +/** + * Extract Codex session id from headers/body with priority: + * 1) headers["session_id"] + * 2) headers["x-session-id"] + * 3) body.metadata.session_id + * 4) body.previous_response_id (fallback, prefixed with "codex_prev_") + * + * Only accept session ids with length > 20. + */ +export function extractCodexSessionId( + headers: Headers, + requestBody: Record, + userAgent: string | null +): CodexSessionExtractionResult { + const officialClient = isCodexClient(userAgent); + + const headerSessionId = normalizeCodexSessionId(headers.get("session_id")); + if (headerSessionId) { + return { + sessionId: headerSessionId, + source: "header_session_id", + isCodexClient: officialClient, + }; + } + + const headerXSessionId = normalizeCodexSessionId(headers.get("x-session-id")); + if (headerXSessionId) { + return { + sessionId: headerXSessionId, + source: "header_x_session_id", + isCodexClient: officialClient, + }; + } + + const metadata = parseMetadata(requestBody); + const bodyMetadataSessionId = metadata ? normalizeCodexSessionId(metadata.session_id) : null; + if (bodyMetadataSessionId) { + return { + sessionId: bodyMetadataSessionId, + source: "body_metadata_session_id", + isCodexClient: officialClient, + }; + } + + const prevResponseId = normalizeCodexSessionId(requestBody.previous_response_id); + if (prevResponseId) { + const sessionId = `codex_prev_${prevResponseId}`; + if (sessionId.length <= CODEX_SESSION_ID_MAX_LENGTH) { + return { + sessionId, + source: "body_previous_response_id", + isCodexClient: officialClient, + }; + } + } + + return { sessionId: null, source: null, isCodexClient: officialClient }; +} diff --git a/src/app/v1/_lib/converters/codex-to-claude/request.ts b/src/app/v1/_lib/converters/codex-to-claude/request.ts index 3cc024215..1e28ed56e 100644 --- a/src/app/v1/_lib/converters/codex-to-claude/request.ts +++ b/src/app/v1/_lib/converters/codex-to-claude/request.ts @@ -18,6 +18,7 @@ */ import { randomBytes } from "node:crypto"; +import { normalizeCodexSessionId } from "@/app/v1/_lib/codex/session-extractor"; import { logger } from "@/lib/logger"; /** @@ -26,6 +27,7 @@ import { logger } from "@/lib/logger"; interface ResponseAPIRequest { model?: string; instructions?: string; + metadata?: Record; input?: Array<{ type?: string; role?: string; @@ -115,7 +117,12 @@ function generateToolCallID(): string { /** * 生成用户 ID(基于 account 和 session) */ -function generateUserID(): string { +function generateUserID(originalMetadata?: Record): string { + const sessionId = normalizeCodexSessionId(originalMetadata?.session_id); + if (sessionId) { + return `codex_session_${sessionId}`; + } + // 简化实现:使用随机 UUID const account = randomBytes(16).toString("hex"); const session = randomBytes(16).toString("hex"); @@ -144,7 +151,7 @@ export function transformCodexRequestToClaude( max_tokens: 32000, messages: [], metadata: { - user_id: generateUserID(), + user_id: generateUserID(req.metadata), }, stream, }; diff --git a/src/app/v1/_lib/proxy/auth-guard.ts b/src/app/v1/_lib/proxy/auth-guard.ts index dd5ebf28c..4f0645a03 100644 --- a/src/app/v1/_lib/proxy/auth-guard.ts +++ b/src/app/v1/_lib/proxy/auth-guard.ts @@ -25,7 +25,8 @@ export class ProxyAuthenticator { return null; } - return ProxyResponses.buildError(401, "令牌已过期或验证不正确"); + // 返回详细的错误信息,帮助用户快速定位问题 + return authState.errorResponse ?? ProxyResponses.buildError(401, "认证失败"); } private static async validate(headers: { @@ -52,7 +53,17 @@ export class ProxyAuthenticator { hasGeminiApiKeyHeader: !!headers.geminiApiKeyHeader, hasGeminiApiKeyQuery: !!headers.geminiApiKeyQuery, }); - return { user: null, key: null, apiKey: null, success: false }; + return { + user: null, + key: null, + apiKey: null, + success: false, + errorResponse: ProxyResponses.buildError( + 401, + "未提供认证凭据。请在 Authorization 头部、x-api-key 头部或 x-goog-api-key 头部中包含 API 密钥。", + "authentication_error" + ), + }; } const [firstKey] = providedKeys; @@ -62,7 +73,17 @@ export class ProxyAuthenticator { logger.warn("[ProxyAuthenticator] Multiple conflicting API keys provided", { keyCount: providedKeys.length, }); - return { user: null, key: null, apiKey: null, success: false }; + return { + user: null, + key: null, + apiKey: null, + success: false, + errorResponse: ProxyResponses.buildError( + 401, + "提供了多个冲突的 API 密钥。请仅使用一种认证方式。", + "authentication_error" + ), + }; } const apiKey = firstKey; @@ -74,7 +95,17 @@ export class ProxyAuthenticator { fromHeader: !!headers.authHeader || !!headers.apiKeyHeader || !!headers.geminiApiKeyHeader, fromQuery: !!headers.geminiApiKeyQuery, }); - return { user: null, key: null, apiKey, success: false }; + return { + user: null, + key: null, + apiKey, + success: false, + errorResponse: ProxyResponses.buildError( + 401, + "API 密钥无效。提供的密钥不存在或已被删除。", + "invalid_api_key" + ), + }; } // Check user status and expiration @@ -86,7 +117,17 @@ export class ProxyAuthenticator { userId: user.id, userName: user.name, }); - return { user: null, key: null, apiKey, success: false }; + return { + user: null, + key: null, + apiKey, + success: false, + errorResponse: ProxyResponses.buildError( + 401, + "用户账户已被禁用。请联系管理员。", + "user_disabled" + ), + }; } // 2. Check if user is expired (lazy expiration check) @@ -103,7 +144,17 @@ export class ProxyAuthenticator { error: error instanceof Error ? error.message : String(error), }); }); - return { user: null, key: null, apiKey, success: false }; + return { + user: null, + key: null, + apiKey, + success: false, + errorResponse: ProxyResponses.buildError( + 401, + `用户账户已于 ${user.expiresAt.toISOString().split("T")[0]} 过期。请续费订阅。`, + "user_expired" + ), + }; } logger.debug("[ProxyAuthenticator] Authentication successful", { diff --git a/src/app/v1/_lib/proxy/errors.ts b/src/app/v1/_lib/proxy/errors.ts index 0874fd736..d789f6fd3 100644 --- a/src/app/v1/_lib/proxy/errors.ts +++ b/src/app/v1/_lib/proxy/errors.ts @@ -504,6 +504,16 @@ async function detectErrorRuleOnceAsync(error: Error): Promise { + return detectErrorRuleOnceAsync(error); +} + /** * 向后兼容的同步检测入口,供尚未迁移的调用方/测试使用 * diff --git a/src/app/v1/_lib/proxy/forwarder.ts b/src/app/v1/_lib/proxy/forwarder.ts index 8c4ad9d64..66d9170dd 100644 --- a/src/app/v1/_lib/proxy/forwarder.ts +++ b/src/app/v1/_lib/proxy/forwarder.ts @@ -30,6 +30,7 @@ import { categorizeErrorAsync, EmptyResponseError, ErrorCategory, + getErrorDetectionResultAsync, isClientAbortError, isEmptyResponseError, isHttp2Error, @@ -448,6 +449,23 @@ export class ProxyForwarder { if (errorCategory === ErrorCategory.NON_RETRYABLE_CLIENT_ERROR) { const proxyError = lastError as ProxyError; const statusCode = proxyError.statusCode; + const detectionResult = await getErrorDetectionResultAsync(lastError); + const matchedRule = + detectionResult.matched && + detectionResult.ruleId !== undefined && + detectionResult.pattern !== undefined && + detectionResult.matchType !== undefined && + detectionResult.category !== undefined + ? { + ruleId: detectionResult.ruleId, + pattern: detectionResult.pattern, + matchType: detectionResult.matchType, + category: detectionResult.category, + description: detectionResult.description, + hasOverrideResponse: detectionResult.overrideResponse !== undefined, + hasOverrideStatusCode: detectionResult.overrideStatusCode !== undefined, + } + : undefined; logger.warn("ProxyForwarder: Non-retryable client error, stopping immediately", { providerId: currentProvider.id, @@ -478,6 +496,7 @@ export class ProxyForwarder { upstreamParsed: proxyError.upstreamError?.parsed, }, clientError: proxyError.getDetailedErrorMessage(), + matchedRule, request: buildRequestDetails(session), }, }); @@ -985,6 +1004,14 @@ export class ProxyForwarder { proxyUrl = buildProxyUrl(baseUrl, session.requestUrl); processedHeaders = headers; + if (session.sessionId) { + void SessionManager.storeSessionRequestHeaders( + session.sessionId, + processedHeaders, + session.requestSequence + ).catch((err) => logger.error("Failed to store request headers:", err)); + } + logger.debug("ProxyForwarder: Gemini request passthrough", { providerId: provider.id, type: provider.providerType, @@ -1118,6 +1145,14 @@ export class ProxyForwarder { processedHeaders = ProxyForwarder.buildHeaders(session, provider); + if (session.sessionId) { + void SessionManager.storeSessionRequestHeaders( + session.sessionId, + processedHeaders, + session.requestSequence + ).catch((err) => logger.error("Failed to store request headers:", err)); + } + if (process.env.NODE_ENV === "development") { logger.trace("ProxyForwarder: Final request headers", { provider: provider.name, @@ -1371,7 +1406,13 @@ export class ProxyForwarder { // 原因:undici fetch 无法关闭自动解压,上游可能无视 accept-encoding: identity 返回 gzip // 当 gzip 流被提前终止时(如连接关闭),undici Gunzip 会抛出 "TypeError: terminated" response = useErrorTolerantFetch - ? await ProxyForwarder.fetchWithoutAutoDecode(proxyUrl, init, provider.id, provider.name) + ? await ProxyForwarder.fetchWithoutAutoDecode( + proxyUrl, + init, + provider.id, + provider.name, + session + ) : await fetch(proxyUrl, init); // ⭐ fetch 成功:收到 HTTP 响应头,保留响应超时继续监控 // 注意:undici 的 fetch 在收到 HTTP 响应头后就 resolve,但实际数据(SSE 首字节 / 完整 JSON) @@ -1556,7 +1597,8 @@ export class ProxyForwarder { proxyUrl, http1FallbackInit, provider.id, - provider.name + provider.name, + session ) : await fetch(proxyUrl, http1FallbackInit); @@ -1907,7 +1949,8 @@ export class ProxyForwarder { url: string, init: RequestInit & { dispatcher?: Dispatcher }, providerId: number, - providerName: string + providerName: string, + session?: ProxySession ): Promise { logger.debug("ProxyForwarder: Using undici.request to bypass auto-decompression", { providerId, @@ -1962,6 +2005,14 @@ export class ProxyForwarder { } } + if (session?.sessionId) { + void SessionManager.storeSessionResponseHeaders( + session.sessionId, + responseHeaders, + session.requestSequence + ).catch((err) => logger.error("Failed to store response headers:", err)); + } + // 检测响应是否为 gzip 压缩 const encoding = responseHeaders.get("content-encoding")?.toLowerCase() || ""; let bodyStream: ReadableStream; diff --git a/src/app/v1/_lib/proxy/response-handler.ts b/src/app/v1/_lib/proxy/response-handler.ts index aaea5032f..f4f9a034c 100644 --- a/src/app/v1/_lib/proxy/response-handler.ts +++ b/src/app/v1/_lib/proxy/response-handler.ts @@ -234,6 +234,7 @@ export class ProxyResponseHandler { await updateMessageRequestDuration(messageContext.id, duration); await updateMessageRequestDetails(messageContext.id, { statusCode: statusCode, + ttfbMs: session.ttfbMs ?? duration, providerChain: session.getProviderChain(), model: session.getCurrentModel() ?? undefined, // ⭐ 更新重定向后的模型 providerId: session.provider?.id, // ⭐ 更新最终供应商ID(重试切换后) @@ -378,6 +379,7 @@ export class ProxyResponseHandler { statusCode: statusCode, inputTokens: usageMetrics?.input_tokens, outputTokens: usageMetrics?.output_tokens, + ttfbMs: session.ttfbMs ?? duration, cacheCreationInputTokens: usageMetrics?.cache_creation_input_tokens, cacheReadInputTokens: usageMetrics?.cache_read_input_tokens, cacheCreation5mInputTokens: usageMetrics?.cache_creation_5m_input_tokens, @@ -572,6 +574,8 @@ export class ProxyResponseHandler { }; if (sessionWithCleanup.clearResponseTimeout) { sessionWithCleanup.clearResponseTimeout(); + // ⭐ 同步记录 TTFB,与首字节超时口径一致 + session.recordTtfb(); logger.debug( "[ResponseHandler] Gemini passthrough: First byte timeout cleared on response received", { @@ -592,6 +596,7 @@ export class ProxyResponseHandler { const chunks: string[] = []; const decoder = new TextDecoder(); + let isFirstChunk = true; while (true) { if (session.clientAbortSignal?.aborted) break; @@ -599,6 +604,10 @@ export class ProxyResponseHandler { const { done, value } = await reader.read(); if (done) break; if (value) { + if (isFirstChunk) { + isFirstChunk = false; + session.recordTtfb(); + } chunks.push(decoder.decode(value, { stream: true })); } } @@ -928,6 +937,7 @@ export class ProxyResponseHandler { statusCode: statusCode, inputTokens: usageForCost?.input_tokens, outputTokens: usageForCost?.output_tokens, + ttfbMs: session.ttfbMs, cacheCreationInputTokens: usageForCost?.cache_creation_input_tokens, cacheReadInputTokens: usageForCost?.cache_read_input_tokens, cacheCreation5mInputTokens: usageForCost?.cache_creation_5m_input_tokens, @@ -972,6 +982,7 @@ export class ProxyResponseHandler { // ⭐ 流式:读到第一块数据后立即清除响应超时定时器 if (isFirstChunk) { + session.recordTtfb(); isFirstChunk = false; const sessionWithCleanup = session as typeof session & { clearResponseTimeout?: () => void; @@ -1280,8 +1291,28 @@ function extractUsageMetrics(value: unknown): UsageMetrics | null { } } + // 兼容顶层扁平格式:cache_creation_5m_input_tokens / cache_creation_1h_input_tokens + // 部分供应商/relay 直接在顶层返回细分字段,而非嵌套在 cache_creation 对象中 + // 优先级:嵌套格式 > 顶层扁平格式 > 旧 relay 格式 + if ( + result.cache_creation_5m_input_tokens === undefined && + typeof usage.cache_creation_5m_input_tokens === "number" + ) { + result.cache_creation_5m_input_tokens = usage.cache_creation_5m_input_tokens; + cacheCreationDetailedTotal += usage.cache_creation_5m_input_tokens; + hasAny = true; + } + if ( + result.cache_creation_1h_input_tokens === undefined && + typeof usage.cache_creation_1h_input_tokens === "number" + ) { + result.cache_creation_1h_input_tokens = usage.cache_creation_1h_input_tokens; + cacheCreationDetailedTotal += usage.cache_creation_1h_input_tokens; + hasAny = true; + } + // 兼容部分 relay / 旧字段命名:claude_cache_creation_5_m_tokens / claude_cache_creation_1_h_tokens - // 仅在标准字段缺失时使用,避免重复统计 + // 仅在标准字段缺失时使用,避免重复统计(优先级最低) if ( result.cache_creation_5m_input_tokens === undefined && typeof usage.claude_cache_creation_5_m_tokens === "number" @@ -1714,6 +1745,7 @@ async function finalizeRequestStats( // 即使没有 usageMetrics,也需要更新状态码和 provider chain await updateMessageRequestDetails(messageContext.id, { statusCode: statusCode, + ttfbMs: session.ttfbMs ?? duration, providerChain: session.getProviderChain(), model: session.getCurrentModel() ?? undefined, providerId: session.provider?.id, // ⭐ 更新最终供应商ID(重试切换后) @@ -1789,6 +1821,7 @@ async function finalizeRequestStats( statusCode: statusCode, inputTokens: normalizedUsage.input_tokens, outputTokens: normalizedUsage.output_tokens, + ttfbMs: session.ttfbMs ?? duration, cacheCreationInputTokens: normalizedUsage.cache_creation_input_tokens, cacheReadInputTokens: normalizedUsage.cache_read_input_tokens, cacheCreation5mInputTokens: normalizedUsage.cache_creation_5m_input_tokens, @@ -1927,6 +1960,7 @@ async function persistRequestFailure(options: { errorMessage, errorStack, errorCause, + ttfbMs: phase === "non-stream" ? (session.ttfbMs ?? duration) : session.ttfbMs, providerChain: session.getProviderChain(), model: session.getCurrentModel() ?? undefined, providerId: session.provider?.id, // ⭐ 更新最终供应商ID(重试切换后) diff --git a/src/app/v1/_lib/proxy/session-guard.ts b/src/app/v1/_lib/proxy/session-guard.ts index 0e0889cce..7acd1a795 100644 --- a/src/app/v1/_lib/proxy/session-guard.ts +++ b/src/app/v1/_lib/proxy/session-guard.ts @@ -48,8 +48,11 @@ export class ProxySessionGuard { try { // 1. 尝试从客户端提取 session_id(metadata.session_id) const clientSessionId = - SessionManager.extractClientSessionId(session.request.message) || - session.generateDeterministicSessionId(); + SessionManager.extractClientSessionId( + session.request.message, + session.headers, + session.userAgent + ) || session.generateDeterministicSessionId(); // 2. 获取 messages 数组 const messages = session.getMessages(); diff --git a/src/app/v1/_lib/proxy/session.ts b/src/app/v1/_lib/proxy/session.ts index 4df1c1254..b4349047a 100644 --- a/src/app/v1/_lib/proxy/session.ts +++ b/src/app/v1/_lib/proxy/session.ts @@ -17,6 +17,7 @@ export interface AuthState { key: Key | null; apiKey: string | null; success: boolean; + errorResponse?: Response; // 认证失败时的详细错误响应 } export interface MessageContext { @@ -59,6 +60,9 @@ export class ProxySession { provider: Provider | null; messageContext: MessageContext | null; + // Time To First Byte (ms). Streaming: first chunk. Non-stream: equals durationMs. + ttfbMs: number | null = null; + // Session ID(用于会话粘性和并发限流) sessionId: string | null; @@ -240,6 +244,22 @@ export class ProxySession { } } + /** + * Record Time To First Byte (TTFB) for streaming responses. + * + * Definition: first body chunk received. + * Non-stream responses should persist TTFB as `durationMs` at finalize time. + */ + recordTtfb(): number { + if (this.ttfbMs !== null) { + return this.ttfbMs; + } + + const value = Math.max(0, Date.now() - this.startTime); + this.ttfbMs = value; + return value; + } + /** * 设置 session ID */ diff --git a/src/components/ui/chart.tsx b/src/components/ui/chart.tsx index a93d37ae6..afdf4a1b2 100644 --- a/src/components/ui/chart.tsx +++ b/src/components/ui/chart.tsx @@ -60,7 +60,9 @@ function ChartContainer({ {...props} > - {children} + + {children} +
); diff --git a/src/components/ui/tag-input.tsx b/src/components/ui/tag-input.tsx index ffea352af..bf5e77d2c 100644 --- a/src/components/ui/tag-input.tsx +++ b/src/components/ui/tag-input.tsx @@ -114,12 +114,14 @@ export function TagInput({ ); const addTag = React.useCallback( - (tag: string) => { + (tag: string, keepOpen = false) => { const trimmedTag = tag.trim(); if (handleValidateTag(trimmedTag, value)) { onChange([...value, trimmedTag]); setInputValue(""); - setShowSuggestions(false); + if (!keepOpen) { + setShowSuggestions(false); + } setHighlightedIndex(-1); } }, @@ -174,7 +176,7 @@ export function TagInput({ } if (e.key === "Enter" && highlightedIndex >= 0) { e.preventDefault(); - addTag(filteredSuggestions[highlightedIndex].value); + addTag(filteredSuggestions[highlightedIndex].value, true); // keepOpen=true 保持下拉展开 return; } if (e.key === "Escape") { @@ -254,7 +256,7 @@ export function TagInput({ const handleSuggestionClick = React.useCallback( (suggestionValue: string) => { - addTag(suggestionValue); + addTag(suggestionValue, true); // keepOpen=true 保持下拉展开 inputRef.current?.focus(); }, [addTag] diff --git a/src/drizzle/schema.ts b/src/drizzle/schema.ts index 2ecef7a7e..9e85ab0b7 100644 --- a/src/drizzle/schema.ts +++ b/src/drizzle/schema.ts @@ -288,6 +288,7 @@ export const messageRequest = pgTable('message_request', { // Token 使用信息 inputTokens: integer('input_tokens'), outputTokens: integer('output_tokens'), + ttfbMs: integer('ttfb_ms'), cacheCreationInputTokens: integer('cache_creation_input_tokens'), cacheReadInputTokens: integer('cache_read_input_tokens'), cacheCreation5mInputTokens: integer('cache_creation_5m_input_tokens'), diff --git a/src/lib/error-rule-detector.ts b/src/lib/error-rule-detector.ts index bbeb33bda..be7ef6be3 100644 --- a/src/lib/error-rule-detector.ts +++ b/src/lib/error-rule-detector.ts @@ -21,9 +21,11 @@ import { type ErrorOverrideResponse, getActiveErrorRules } from "@/repository/er */ export interface ErrorDetectionResult { matched: boolean; + ruleId?: number; // 规则 ID category?: string; // 触发的错误分类 pattern?: string; // 匹配的规则模式 matchType?: string; // 匹配类型(regex/contains/exact) + description?: string; // 规则描述 /** 覆写响应体:如果配置了则用此响应替换原始错误响应 */ overrideResponse?: ErrorOverrideResponse; /** 覆写状态码:如果配置了则用此状态码替换原始状态码 */ @@ -34,6 +36,8 @@ export interface ErrorDetectionResult { * 缓存的正则规则 */ interface RegexPattern { + ruleId: number; + rawPattern: string; pattern: RegExp; category: string; description?: string; @@ -45,6 +49,8 @@ interface RegexPattern { * 缓存的包含规则 */ interface ContainsPattern { + ruleId: number; + pattern: string; text: string; category: string; description?: string; @@ -56,6 +62,8 @@ interface ContainsPattern { * 缓存的精确规则 */ interface ExactPattern { + ruleId: number; + pattern: string; text: string; category: string; description?: string; @@ -197,6 +205,8 @@ class ErrorRuleDetector { case "contains": { const lowerText = rule.pattern.toLowerCase(); newContainsPatterns.push({ + ruleId: rule.id, + pattern: rule.pattern, text: lowerText, category: rule.category, description: rule.description ?? undefined, @@ -209,6 +219,8 @@ class ErrorRuleDetector { case "exact": { const lowerText = rule.pattern.toLowerCase(); newExactPatterns.set(lowerText, { + ruleId: rule.id, + pattern: rule.pattern, text: lowerText, category: rule.category, description: rule.description ?? undefined, @@ -231,6 +243,8 @@ class ErrorRuleDetector { const pattern = new RegExp(rule.pattern, "i"); newRegexPatterns.push({ + ruleId: rule.id, + rawPattern: rule.pattern, pattern, category: rule.category, description: rule.description ?? undefined, @@ -323,9 +337,11 @@ class ErrorRuleDetector { if (lowerMessage.includes(pattern.text)) { return { matched: true, + ruleId: pattern.ruleId, category: pattern.category, - pattern: pattern.text, + pattern: pattern.pattern, matchType: "contains", + description: pattern.description, overrideResponse: pattern.overrideResponse, overrideStatusCode: pattern.overrideStatusCode, }; @@ -337,22 +353,34 @@ class ErrorRuleDetector { if (exactMatch) { return { matched: true, + ruleId: exactMatch.ruleId, category: exactMatch.category, - pattern: exactMatch.text, + pattern: exactMatch.pattern, matchType: "exact", + description: exactMatch.description, overrideResponse: exactMatch.overrideResponse, overrideStatusCode: exactMatch.overrideStatusCode, }; } // 3. 正则匹配(最慢,但最灵活) - for (const { pattern, category, overrideResponse, overrideStatusCode } of this.regexPatterns) { + for (const { + ruleId, + rawPattern, + pattern, + category, + description, + overrideResponse, + overrideStatusCode, + } of this.regexPatterns) { if (pattern.test(errorMessage)) { return { matched: true, + ruleId, category, - pattern: pattern.source, + pattern: rawPattern, matchType: "regex", + description, overrideResponse, overrideStatusCode, }; diff --git a/src/lib/session-manager.ts b/src/lib/session-manager.ts index acad4ee3b..a58c43bf4 100644 --- a/src/lib/session-manager.ts +++ b/src/lib/session-manager.ts @@ -1,7 +1,10 @@ import "server-only"; import crypto from "node:crypto"; +import { extractCodexSessionId } from "@/app/v1/_lib/codex/session-extractor"; +import { sanitizeHeaders } from "@/app/v1/_lib/proxy/errors"; import { logger } from "@/lib/logger"; +import { normalizeRequestSequence } from "@/lib/utils/request-sequence"; import type { ActiveSessionInfo, SessionProviderInfo, @@ -11,6 +14,49 @@ import type { import { getRedisClient } from "./redis"; import { SessionTracker } from "./session-tracker"; +function headersToSanitizedObject(headers: Headers): Record { + const sanitizedText = sanitizeHeaders(headers); + if (!sanitizedText || sanitizedText === "(empty)") { + return {}; + } + + const obj: Record = {}; + const lines = sanitizedText.split(/\r?\n/).filter(Boolean); + for (const line of lines) { + const colonIndex = line.indexOf(":"); + if (colonIndex === -1) continue; + const name = line.slice(0, colonIndex).trim(); + const value = line.slice(colonIndex + 1).trim(); + if (!name) continue; + + if (obj[name]) { + obj[name] = `${obj[name]}\n${value}`; + } else { + obj[name] = value; + } + } + + return obj; +} + +function parseHeaderRecord(value: string): Record | null { + try { + const parsed: unknown = JSON.parse(value); + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null; + + const record: Record = {}; + for (const [key, raw] of Object.entries(parsed as Record)) { + if (typeof raw === "string") { + record[key] = raw; + } + } + return record; + } catch (error) { + logger.warn("SessionManager: Failed to parse header record JSON", { error }); + return null; + } +} + /** * Session 管理器 * @@ -37,7 +83,26 @@ export class SessionManager { * 1. metadata.user_id (Claude Code 主要方式,格式: "{user}_session_{sessionId}") * 2. metadata.session_id (备选方式) */ - static extractClientSessionId(requestMessage: Record): string | null { + static extractClientSessionId( + requestMessage: Record, + headers?: Headers | null, + userAgent?: string | null + ): string | null { + // Codex 请求:优先尝试从 headers/body 提取稳定的 session_id + if (headers && Array.isArray(requestMessage.input)) { + const result = extractCodexSessionId(headers, requestMessage, userAgent ?? null); + if (result.sessionId) { + logger.trace("SessionManager: Extracted session from Codex request", { + sessionId: result.sessionId, + source: result.source, + isCodexClient: result.isCodexClient, + }); + return result.sessionId; + } + + return null; + } + const metadata = requestMessage.metadata; if (!metadata || typeof metadata !== "object") { return null; @@ -1281,6 +1346,95 @@ export class SessionManager { } } + static async storeSessionRequestHeaders( + sessionId: string, + headers: Headers, + requestSequence?: number + ): Promise { + const redis = getRedisClient(); + if (!redis || redis.status !== "ready") return; + + try { + const sequence = normalizeRequestSequence(requestSequence) ?? 1; + const key = `session:${sessionId}:req:${sequence}:reqHeaders`; + const headersJson = JSON.stringify(headersToSanitizedObject(headers)); + await redis.setex(key, SessionManager.SESSION_TTL, headersJson); + logger.trace("SessionManager: Stored session request headers", { + sessionId, + requestSequence: sequence, + key, + }); + } catch (error) { + logger.error("SessionManager: Failed to store session request headers", { error, sessionId }); + } + } + + static async storeSessionResponseHeaders( + sessionId: string, + headers: Headers, + requestSequence?: number + ): Promise { + const redis = getRedisClient(); + if (!redis || redis.status !== "ready") return; + + try { + const sequence = normalizeRequestSequence(requestSequence) ?? 1; + const key = `session:${sessionId}:req:${sequence}:resHeaders`; + const headersJson = JSON.stringify(headersToSanitizedObject(headers)); + await redis.setex(key, SessionManager.SESSION_TTL, headersJson); + logger.trace("SessionManager: Stored session response headers", { + sessionId, + requestSequence: sequence, + key, + }); + } catch (error) { + logger.error("SessionManager: Failed to store session response headers", { + error, + sessionId, + }); + } + } + + static async getSessionRequestHeaders( + sessionId: string, + requestSequence?: number + ): Promise | null> { + const redis = getRedisClient(); + if (!redis || redis.status !== "ready") return null; + + try { + const sequence = normalizeRequestSequence(requestSequence); + if (!sequence) return null; + const key = `session:${sessionId}:req:${sequence}:reqHeaders`; + const value = await redis.get(key); + if (!value) return null; + return parseHeaderRecord(value); + } catch (error) { + logger.error("SessionManager: Failed to get session request headers", { error, sessionId }); + return null; + } + } + + static async getSessionResponseHeaders( + sessionId: string, + requestSequence?: number + ): Promise | null> { + const redis = getRedisClient(); + if (!redis || redis.status !== "ready") return null; + + try { + const sequence = normalizeRequestSequence(requestSequence); + if (!sequence) return null; + const key = `session:${sessionId}:req:${sequence}:resHeaders`; + const value = await redis.get(key); + if (!value) return null; + return parseHeaderRecord(value); + } catch (error) { + logger.error("SessionManager: Failed to get session response headers", { error, sessionId }); + return null; + } + } + /** * 获取 session 响应体 * @@ -1560,3 +1714,5 @@ export class SessionManager { } } } + +export { headersToSanitizedObject, parseHeaderRecord }; diff --git a/src/lib/utils/performance-formatter.ts b/src/lib/utils/performance-formatter.ts new file mode 100644 index 000000000..53825dfef --- /dev/null +++ b/src/lib/utils/performance-formatter.ts @@ -0,0 +1,34 @@ +export const NON_BILLING_ENDPOINT = "/v1/messages/count_tokens"; + +/** + * 格式化请求耗时 + * - 1000ms 以上显示为秒(如 "1.23s") + * - 1000ms 以下显示为毫秒(如 "850ms") + */ +export function formatDuration(durationMs: number | null): string { + if (durationMs == null) return "-"; + + // 1000ms 以上转换为秒 + if (durationMs >= 1000) { + return `${(Number(durationMs) / 1000).toFixed(2)}s`; + } + + // 1000ms 以下显示毫秒 + return `${durationMs}ms`; +} + +/** + * 计算输出速率(tokens/second) + */ +export function calculateOutputRate( + outputTokens: number | null, + durationMs: number | null, + ttfbMs: number | null +): number | null { + if (outputTokens == null || outputTokens <= 0 || durationMs == null || durationMs <= 0) { + return null; + } + const generationTimeMs = ttfbMs != null ? durationMs - ttfbMs : durationMs; + if (generationTimeMs <= 0) return null; + return outputTokens / (generationTimeMs / 1000); +} diff --git a/src/lib/utils/provider-chain-formatter.ts b/src/lib/utils/provider-chain-formatter.ts index 30ac60d64..71f2301c1 100644 --- a/src/lib/utils/provider-chain-formatter.ts +++ b/src/lib/utils/provider-chain-formatter.ts @@ -13,7 +13,11 @@ function getProviderStatus(item: ProviderChainItem): "✓" | "✗" | "⚡" | " return "✓"; } // 失败标记 - if (item.reason === "retry_failed" || item.reason === "system_error") { + if ( + item.reason === "retry_failed" || + item.reason === "system_error" || + item.reason === "client_error_non_retryable" + ) { return "✗"; } // 并发限制失败 @@ -36,7 +40,13 @@ function isActualRequest(item: ProviderChainItem): boolean { if (item.reason === "concurrent_limit_failed") return true; // 失败记录 - if (item.reason === "retry_failed" || item.reason === "system_error") return true; + if ( + item.reason === "retry_failed" || + item.reason === "system_error" || + item.reason === "client_error_non_retryable" + ) { + return true; + } // HTTP/2 回退:算作一次中间事件(显示但不计入失败) if (item.reason === "http2_fallback") return true; @@ -250,6 +260,8 @@ export function formatProviderDescription( desc += ` ${t("description.concurrentLimit")}`; } else if (item.reason === "http2_fallback") { desc += ` ${t("description.http2Fallback")}`; + } else if (item.reason === "client_error_non_retryable") { + desc += ` ${t("description.clientError")}`; } desc += "\n"; @@ -504,6 +516,48 @@ export function formatProviderTimeline( continue; } + // === 不可重试的客户端错误 === + if (item.reason === "client_error_non_retryable") { + const attempt = item.attemptNumber ?? actualAttemptNumber ?? 0; + timeline += `${t("timeline.clientErrorNonRetryable", { attempt })}\n\n`; + + if (item.errorDetails?.provider) { + const p = item.errorDetails.provider; + timeline += `${t("timeline.provider", { provider: p.name })}\n`; + timeline += `${t("timeline.statusCode", { code: p.statusCode })}\n`; + timeline += `${t("timeline.error", { error: p.statusText })}\n`; + } else { + timeline += `${t("timeline.provider", { provider: item.name })}\n`; + if (item.statusCode) { + timeline += `${t("timeline.statusCode", { code: item.statusCode })}\n`; + } + timeline += `${t("timeline.error", { error: item.errorMessage || t("timeline.unknown") })}\n`; + } + + if (item.errorDetails?.matchedRule) { + const rule = item.errorDetails.matchedRule; + timeline += `\n${t("timeline.matchedRule")}:\n`; + timeline += `${t("timeline.ruleId", { id: rule.ruleId })}\n`; + timeline += `${t("timeline.ruleCategory", { category: rule.category })}\n`; + timeline += `${t("timeline.rulePattern", { pattern: rule.pattern })}\n`; + timeline += `${t("timeline.ruleMatchType", { matchType: rule.matchType })}\n`; + if (rule.description) { + timeline += `${t("timeline.ruleDescription", { description: rule.description })}\n`; + } + timeline += `${t("timeline.ruleHasOverride", { + response: rule.hasOverrideResponse ? "true" : "false", + statusCode: rule.hasOverrideStatusCode ? "true" : "false", + })}\n`; + } + + if (item.errorDetails?.request) { + timeline += formatRequestDetails(item.errorDetails.request, t); + } + + timeline += `\n${t("timeline.clientErrorNote")}`; + continue; + } + // === HTTP/2 协议回退 === if (item.reason === "http2_fallback") { timeline += `${t("timeline.http2Fallback")}\n\n`; diff --git a/src/lib/utils/request-sequence.ts b/src/lib/utils/request-sequence.ts new file mode 100644 index 000000000..efa2b8212 --- /dev/null +++ b/src/lib/utils/request-sequence.ts @@ -0,0 +1,14 @@ +/** + * Normalize request sequence number + * + * Ensures the sequence is a positive safe integer for multi-request session scenarios + * + * @param requestSequence - Raw sequence number + * @returns Normalized sequence number, or null for invalid input + */ +export function normalizeRequestSequence(requestSequence?: number): number | null { + if (typeof requestSequence !== "number") return null; + if (!Number.isSafeInteger(requestSequence)) return null; + if (requestSequence <= 0) return null; + return requestSequence; +} diff --git a/src/repository/error-rules.ts b/src/repository/error-rules.ts index 51e15181a..4e2ef5d3f 100644 --- a/src/repository/error-rules.ts +++ b/src/repository/error-rules.ts @@ -437,6 +437,23 @@ const DEFAULT_ERROR_RULES = [ }, }, }, + // Issue #432: Empty message content validation error (non-retryable) + { + pattern: "all messages must have non-empty content", + category: "validation_error", + description: "Message content is empty (client error)", + matchType: "contains" as const, + isDefault: true, + isEnabled: true, + priority: 89, + overrideResponse: { + type: "error", + error: { + type: "validation_error", + message: "消息内容不能为空,请确保所有消息都有有效内容(最后一条 assistant 消息除外)", + }, + }, + }, // Issue #366: Tool names must be unique (MCP server configuration error) { pattern: "Tool names must be unique", diff --git a/src/repository/leaderboard.ts b/src/repository/leaderboard.ts index 4981b6eb2..a1f9b44dd 100644 --- a/src/repository/leaderboard.ts +++ b/src/repository/leaderboard.ts @@ -28,7 +28,8 @@ export interface ProviderLeaderboardEntry { totalCost: number; totalTokens: number; successRate: number; // 0-1 之间的小数,UI 层负责格式化为百分比 - avgResponseTime: number; // 毫秒 + avgTtfbMs: number; // 毫秒 + avgTokensPerSecond: number; // tok/s(仅统计流式且可计算的请求) } /** @@ -314,7 +315,20 @@ async function findProviderLeaderboardWithTimezone( / NULLIF(count(*)::double precision, 0), 0::double precision )`, - avgResponseTime: sql`COALESCE(avg(${messageRequest.durationMs})::double precision, 0::double precision)`, + avgTtfbMs: sql`COALESCE(avg(${messageRequest.ttfbMs})::double precision, 0::double precision)`, + avgTokensPerSecond: sql`COALESCE( + avg( + CASE + WHEN ${messageRequest.outputTokens} > 0 + AND ${messageRequest.durationMs} IS NOT NULL + AND ${messageRequest.ttfbMs} IS NOT NULL + AND ${messageRequest.ttfbMs} < ${messageRequest.durationMs} + THEN (${messageRequest.outputTokens}::double precision) + / NULLIF((${messageRequest.durationMs} - ${messageRequest.ttfbMs}) / 1000.0, 0) + END + )::double precision, + 0::double precision + )`, }) .from(messageRequest) .innerJoin( @@ -332,7 +346,8 @@ async function findProviderLeaderboardWithTimezone( totalCost: parseFloat(entry.totalCost), totalTokens: entry.totalTokens, successRate: entry.successRate ?? 0, - avgResponseTime: entry.avgResponseTime ?? 0, + avgTtfbMs: entry.avgTtfbMs ?? 0, + avgTokensPerSecond: entry.avgTokensPerSecond ?? 0, })); } diff --git a/src/repository/message.ts b/src/repository/message.ts index f1aa9150c..a374e54ad 100644 --- a/src/repository/message.ts +++ b/src/repository/message.ts @@ -106,6 +106,7 @@ export async function updateMessageRequestDetails( statusCode?: number; inputTokens?: number; outputTokens?: number; + ttfbMs?: number | null; cacheCreationInputTokens?: number; cacheReadInputTokens?: number; cacheCreation5mInputTokens?: number; @@ -133,6 +134,9 @@ export async function updateMessageRequestDetails( if (details.outputTokens !== undefined) { updateData.outputTokens = details.outputTokens; } + if (details.ttfbMs !== undefined) { + updateData.ttfbMs = details.ttfbMs; + } if (details.cacheCreationInputTokens !== undefined) { updateData.cacheCreationInputTokens = details.cacheCreationInputTokens; } diff --git a/src/repository/usage-logs.ts b/src/repository/usage-logs.ts index 4a980d17e..d3f4d3d4d 100644 --- a/src/repository/usage-logs.ts +++ b/src/repository/usage-logs.ts @@ -47,6 +47,7 @@ export interface UsageLogRow { costUsd: string | null; costMultiplier: string | null; // 供应商倍率 durationMs: number | null; + ttfbMs: number | null; errorMessage: string | null; providerChain: ProviderChainItem[] | null; blockedBy: string | null; // 拦截类型(如 'sensitive_word') @@ -171,9 +172,8 @@ export async function findUsageLogsBatch( // Cursor-based pagination: WHERE (created_at, id) < (cursor_created_at, cursor_id) // Using row value comparison for efficient keyset pagination if (cursor) { - const cursorDate = new Date(cursor.createdAt); conditions.push( - sql`(${messageRequest.createdAt}, ${messageRequest.id}) < (${cursorDate.toISOString()}::timestamptz, ${cursor.id})` + sql`(${messageRequest.createdAt}, ${messageRequest.id}) < (${cursor.createdAt}::timestamptz, ${cursor.id})` ); } @@ -184,6 +184,7 @@ export async function findUsageLogsBatch( .select({ id: messageRequest.id, createdAt: messageRequest.createdAt, + createdAtRaw: sql`to_char(${messageRequest.createdAt} AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')`, sessionId: messageRequest.sessionId, requestSequence: messageRequest.requestSequence, userName: users.name, @@ -203,6 +204,7 @@ export async function findUsageLogsBatch( costUsd: messageRequest.costUsd, costMultiplier: messageRequest.costMultiplier, durationMs: messageRequest.durationMs, + ttfbMs: messageRequest.ttfbMs, errorMessage: messageRequest.errorMessage, providerChain: messageRequest.providerChain, blockedBy: messageRequest.blockedBy, @@ -226,9 +228,7 @@ export async function findUsageLogsBatch( // Calculate next cursor from the last record const lastLog = logsToReturn[logsToReturn.length - 1]; const nextCursor = - hasMore && lastLog?.createdAt - ? { createdAt: lastLog.createdAt.toISOString(), id: lastLog.id } - : null; + hasMore && lastLog?.createdAtRaw ? { createdAt: lastLog.createdAtRaw, id: lastLog.id } : null; const logs: UsageLogRow[] = logsToReturn.map((row) => { const totalRowTokens = @@ -412,6 +412,7 @@ export async function findUsageLogsWithDetails(filters: UsageLogFilters): Promis costUsd: messageRequest.costUsd, costMultiplier: messageRequest.costMultiplier, // 供应商倍率 durationMs: messageRequest.durationMs, + ttfbMs: messageRequest.ttfbMs, errorMessage: messageRequest.errorMessage, providerChain: messageRequest.providerChain, blockedBy: messageRequest.blockedBy, // 拦截类型 diff --git a/src/repository/user.ts b/src/repository/user.ts index 47c617958..e1963da1c 100644 --- a/src/repository/user.ts +++ b/src/repository/user.ts @@ -167,9 +167,8 @@ export async function findUserListBatch( // Cursor-based pagination: WHERE (created_at, id) > (cursor_created_at, cursor_id) if (cursor) { - const cursorDate = new Date(cursor.createdAt); conditions.push( - sql`(${users.createdAt}, ${users.id}) > (${cursorDate.toISOString()}::timestamptz, ${cursor.id})` + sql`(${users.createdAt}, ${users.id}) > (${cursor.createdAt}::timestamptz, ${cursor.id})` ); } @@ -187,6 +186,7 @@ export async function findUserListBatch( providerGroup: users.providerGroup, tags: users.tags, createdAt: users.createdAt, + createdAtRaw: sql`to_char(${users.createdAt} AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')`, updatedAt: users.updatedAt, deletedAt: users.deletedAt, limit5hUsd: users.limit5hUsd, @@ -211,8 +211,8 @@ export async function findUserListBatch( const lastUser = usersToReturn[usersToReturn.length - 1]; const nextCursor = - hasMore && lastUser?.createdAt - ? { createdAt: lastUser.createdAt.toISOString(), id: lastUser.id } + hasMore && lastUser?.createdAtRaw + ? { createdAt: lastUser.createdAtRaw, id: lastUser.id } : null; return { diff --git a/src/types/message.ts b/src/types/message.ts index 85c65a0c9..16bb098aa 100644 --- a/src/types/message.ts +++ b/src/types/message.ts @@ -89,6 +89,17 @@ export interface ProviderChainItem { // 客户端输入错误(不可重试) clientError?: string; // 详细的客户端错误消息(包含匹配的白名单模式) + // 匹配到的错误规则(用于排查不可重试的客户端错误) + matchedRule?: { + ruleId: number; + pattern: string; + matchType: "regex" | "contains" | "exact" | string; + category: string; + description?: string; + hasOverrideResponse: boolean; + hasOverrideStatusCode: boolean; + }; + // 新增:请求详情(用于问题排查) request?: { url: string; // 完整请求 URL(已脱敏查询参数中的 key) @@ -170,6 +181,7 @@ export interface MessageRequest { key: string; model?: string; durationMs?: number; + ttfbMs?: number | null; costUsd?: string; // 单次请求费用(美元),保持高精度字符串表示 // 供应商倍率(记录该请求使用的 cost_multiplier) diff --git a/tests/unit/lib/session-manager-helpers.test.ts b/tests/unit/lib/session-manager-helpers.test.ts new file mode 100644 index 000000000..084082868 --- /dev/null +++ b/tests/unit/lib/session-manager-helpers.test.ts @@ -0,0 +1,122 @@ +import { describe, expect, test, vi } from "vitest"; + +const loggerWarnMock = vi.fn(); + +vi.mock("server-only", () => ({})); + +vi.mock("@/lib/logger", () => ({ + logger: { + warn: loggerWarnMock, + trace: vi.fn(), + info: vi.fn(), + error: vi.fn(), + }, +})); + +const sanitizeHeadersMock = vi.fn(); + +vi.mock("@/app/v1/_lib/proxy/errors", () => ({ + sanitizeHeaders: sanitizeHeadersMock, +})); + +async function loadHelpers() { + const mod = await import("@/lib/session-manager"); + return { + headersToSanitizedObject: mod.headersToSanitizedObject, + parseHeaderRecord: mod.parseHeaderRecord, + }; +} + +describe("SessionManager 辅助函数", () => { + test("parseHeaderRecord:有效 JSON 对象应解析为记录", async () => { + vi.clearAllMocks(); + const { parseHeaderRecord } = await loadHelpers(); + + expect(parseHeaderRecord('{"a":"1","b":"2"}')).toEqual({ a: "1", b: "2" }); + expect(loggerWarnMock).not.toHaveBeenCalled(); + }); + + test("parseHeaderRecord:空对象应返回空记录", async () => { + vi.clearAllMocks(); + const { parseHeaderRecord } = await loadHelpers(); + + expect(parseHeaderRecord("{}")).toEqual({}); + expect(loggerWarnMock).not.toHaveBeenCalled(); + }); + + test("parseHeaderRecord:只保留字符串值", async () => { + vi.clearAllMocks(); + const { parseHeaderRecord } = await loadHelpers(); + + expect(parseHeaderRecord('{"a":"1","b":2,"c":true,"d":null,"e":{},"f":[]}')).toEqual({ + a: "1", + }); + expect(loggerWarnMock).not.toHaveBeenCalled(); + }); + + test("parseHeaderRecord:无效 JSON 应返回 null 并记录 warn", async () => { + vi.clearAllMocks(); + const { parseHeaderRecord } = await loadHelpers(); + + expect(parseHeaderRecord("{bad json")).toBe(null); + expect(loggerWarnMock).toHaveBeenCalledTimes(1); + + const [message, meta] = loggerWarnMock.mock.calls[0] ?? []; + expect(message).toBe("SessionManager: Failed to parse header record JSON"); + expect(meta).toEqual(expect.objectContaining({ error: expect.anything() })); + }); + + test("parseHeaderRecord:JSON 数组/null/原始值应返回 null", async () => { + vi.clearAllMocks(); + const { parseHeaderRecord } = await loadHelpers(); + + expect(parseHeaderRecord('["a"]')).toBe(null); + expect(parseHeaderRecord("null")).toBe(null); + expect(parseHeaderRecord("1")).toBe(null); + expect(loggerWarnMock).not.toHaveBeenCalled(); + }); + + test("headersToSanitizedObject:单个 header 应正确转换", async () => { + vi.clearAllMocks(); + const { headersToSanitizedObject } = await loadHelpers(); + + const headers = new Headers({ "x-test": "1" }); + sanitizeHeadersMock.mockReturnValueOnce("x-test: 1"); + + expect(headersToSanitizedObject(headers)).toEqual({ "x-test": "1" }); + expect(sanitizeHeadersMock).toHaveBeenCalledWith(headers); + }); + + test("headersToSanitizedObject:多个 header 应正确转换", async () => { + vi.clearAllMocks(); + const { headersToSanitizedObject } = await loadHelpers(); + + const headers = new Headers({ a: "1", b: "2" }); + sanitizeHeadersMock.mockReturnValueOnce("a: 1\nb: 2"); + + expect(headersToSanitizedObject(headers)).toEqual({ a: "1", b: "2" }); + expect(sanitizeHeadersMock).toHaveBeenCalledWith(headers); + }); + + test("headersToSanitizedObject:空 Headers 应返回空对象", async () => { + vi.clearAllMocks(); + const { headersToSanitizedObject } = await loadHelpers(); + + const headers = new Headers(); + sanitizeHeadersMock.mockReturnValueOnce("(empty)"); + + expect(headersToSanitizedObject(headers)).toEqual({}); + expect(sanitizeHeadersMock).toHaveBeenCalledWith(headers); + }); + + test("headersToSanitizedObject:值包含冒号时应保留完整值", async () => { + vi.clearAllMocks(); + const { headersToSanitizedObject } = await loadHelpers(); + + const headers = new Headers({ "x-test": "a:b:c" }); + sanitizeHeadersMock.mockReturnValueOnce("x-test: a:b:c"); + + expect(headersToSanitizedObject(headers)).toEqual({ "x-test": "a:b:c" }); + expect(sanitizeHeadersMock).toHaveBeenCalledWith(headers); + }); +}); diff --git a/tests/unit/lib/utils/request-sequence.test.ts b/tests/unit/lib/utils/request-sequence.test.ts new file mode 100644 index 000000000..0cbf2e18b --- /dev/null +++ b/tests/unit/lib/utils/request-sequence.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, test } from "vitest"; +import { normalizeRequestSequence } from "@/lib/utils/request-sequence"; + +describe("normalizeRequestSequence", () => { + test("正常情况:正整数应原样返回", () => { + expect(normalizeRequestSequence(1)).toBe(1); + expect(normalizeRequestSequence(100)).toBe(100); + expect(normalizeRequestSequence(Number.MAX_SAFE_INTEGER)).toBe(Number.MAX_SAFE_INTEGER); + }); + + test("边界情况:无效数字应返回 null", () => { + expect(normalizeRequestSequence(undefined)).toBe(null); + expect(normalizeRequestSequence(0)).toBe(null); + expect(normalizeRequestSequence(-1)).toBe(null); + expect(normalizeRequestSequence(1.1)).toBe(null); + expect(normalizeRequestSequence(Number.NaN)).toBe(null); + expect(normalizeRequestSequence(Number.POSITIVE_INFINITY)).toBe(null); + expect(normalizeRequestSequence(Number.NEGATIVE_INFINITY)).toBe(null); + expect(normalizeRequestSequence(Number.MAX_SAFE_INTEGER + 1)).toBe(null); + }); + + test("类型错误:非数字类型应返回 null", () => { + expect(normalizeRequestSequence("1" as unknown as number)).toBe(null); + expect(normalizeRequestSequence(null as unknown as number)).toBe(null); + expect(normalizeRequestSequence({} as unknown as number)).toBe(null); + expect(normalizeRequestSequence([] as unknown as number)).toBe(null); + }); +}); diff --git a/tests/unit/proxy/extract-usage-metrics.test.ts b/tests/unit/proxy/extract-usage-metrics.test.ts new file mode 100644 index 000000000..8b845cb09 --- /dev/null +++ b/tests/unit/proxy/extract-usage-metrics.test.ts @@ -0,0 +1,509 @@ +import { describe, it, expect } from "vitest"; + +// 由于 extractUsageMetrics 是内部函数,需要通过 parseUsageFromResponseText 间接测试 +// 或者将其导出用于测试 +// 这里我们通过构造 JSON 响应来测试 parseUsageFromResponseText + +import { parseUsageFromResponseText } from "@/app/v1/_lib/proxy/response-handler"; + +describe("extractUsageMetrics", () => { + describe("基本 token 提取", () => { + it("应正确提取 input_tokens 和 output_tokens", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 1000, + output_tokens: 500, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics).not.toBeNull(); + expect(result.usageMetrics?.input_tokens).toBe(1000); + expect(result.usageMetrics?.output_tokens).toBe(500); + }); + + it("空值或非对象应返回 null", () => { + expect(parseUsageFromResponseText("", "claude").usageMetrics).toBeNull(); + expect(parseUsageFromResponseText("null", "claude").usageMetrics).toBeNull(); + expect(parseUsageFromResponseText('"string"', "claude").usageMetrics).toBeNull(); + }); + }); + + describe("Claude 嵌套格式 (cache_creation.ephemeral_*)", () => { + it("应从 cache_creation 嵌套对象提取 5m 和 1h token", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 1000, + output_tokens: 500, + cache_creation_input_tokens: 800, + cache_creation: { + ephemeral_5m_input_tokens: 300, + ephemeral_1h_input_tokens: 500, + }, + cache_read_input_tokens: 200, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics).not.toBeNull(); + expect(result.usageMetrics?.cache_creation_input_tokens).toBe(800); + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(300); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(500); + expect(result.usageMetrics?.cache_read_input_tokens).toBe(200); + expect(result.usageMetrics?.cache_ttl).toBe("mixed"); + }); + + it("只有 5m 时应推断 cache_ttl 为 5m", () => { + const response = JSON.stringify({ + usage: { + cache_creation_input_tokens: 300, + cache_creation: { + ephemeral_5m_input_tokens: 300, + }, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(300); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBeUndefined(); + expect(result.usageMetrics?.cache_ttl).toBe("5m"); + }); + + it("只有 1h 时应推断 cache_ttl 为 1h", () => { + const response = JSON.stringify({ + usage: { + cache_creation_input_tokens: 500, + cache_creation: { + ephemeral_1h_input_tokens: 500, + }, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(500); + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBeUndefined(); + expect(result.usageMetrics?.cache_ttl).toBe("1h"); + }); + }); + + describe("旧 relay 格式 (claude_cache_creation_*)", () => { + it("应从旧 relay 字段提取 5m 和 1h token", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 1000, + output_tokens: 500, + cache_creation_input_tokens: 800, + claude_cache_creation_5_m_tokens: 300, + claude_cache_creation_1_h_tokens: 500, + cache_read_input_tokens: 200, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(300); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(500); + expect(result.usageMetrics?.cache_ttl).toBe("mixed"); + }); + + it("嵌套格式应优先于旧 relay 格式", () => { + const response = JSON.stringify({ + usage: { + cache_creation: { + ephemeral_5m_input_tokens: 100, + ephemeral_1h_input_tokens: 200, + }, + claude_cache_creation_5_m_tokens: 999, + claude_cache_creation_1_h_tokens: 888, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + // 嵌套格式优先 + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(100); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(200); + }); + }); + + describe("顶层扁平格式 (cache_creation_5m_input_tokens)", () => { + it("应从顶层扁平字段提取 5m 和 1h token", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 1000, + output_tokens: 500, + cache_creation_input_tokens: 800, + cache_creation_5m_input_tokens: 300, + cache_creation_1h_input_tokens: 500, + cache_read_input_tokens: 200, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.cache_creation_input_tokens).toBe(800); + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(300); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(500); + expect(result.usageMetrics?.cache_read_input_tokens).toBe(200); + expect(result.usageMetrics?.cache_ttl).toBe("mixed"); + }); + + it("只有顶层 5m 时应正确提取并推断 TTL", () => { + const response = JSON.stringify({ + usage: { + cache_creation_input_tokens: 300, + cache_creation_5m_input_tokens: 300, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(300); + expect(result.usageMetrics?.cache_ttl).toBe("5m"); + }); + + it("只有顶层 1h 时应正确提取并推断 TTL", () => { + const response = JSON.stringify({ + usage: { + cache_creation_input_tokens: 500, + cache_creation_1h_input_tokens: 500, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(500); + expect(result.usageMetrics?.cache_ttl).toBe("1h"); + }); + + it("嵌套格式应优先于顶层扁平格式", () => { + const response = JSON.stringify({ + usage: { + cache_creation: { + ephemeral_5m_input_tokens: 100, + ephemeral_1h_input_tokens: 200, + }, + cache_creation_5m_input_tokens: 999, + cache_creation_1h_input_tokens: 888, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + // 嵌套格式优先 + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(100); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(200); + }); + + it("顶层扁平格式应优先于旧 relay 格式", () => { + const response = JSON.stringify({ + usage: { + cache_creation_5m_input_tokens: 300, + cache_creation_1h_input_tokens: 500, + claude_cache_creation_5_m_tokens: 999, + claude_cache_creation_1_h_tokens: 888, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + // 顶层扁平格式优先于旧 relay 格式 + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(300); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(500); + }); + + it("三种格式同时存在时应按优先级提取", () => { + const response = JSON.stringify({ + usage: { + cache_creation: { + ephemeral_5m_input_tokens: 100, + ephemeral_1h_input_tokens: 200, + }, + cache_creation_5m_input_tokens: 300, + cache_creation_1h_input_tokens: 400, + claude_cache_creation_5_m_tokens: 500, + claude_cache_creation_1_h_tokens: 600, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + // 嵌套格式最优先 + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(100); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(200); + expect(result.usageMetrics?.cache_ttl).toBe("mixed"); + }); + }); + + describe("cache_creation_input_tokens 自动计算", () => { + it("当 cache_creation_input_tokens 缺失时应自动计算总量", () => { + const response = JSON.stringify({ + usage: { + cache_creation: { + ephemeral_5m_input_tokens: 300, + ephemeral_1h_input_tokens: 500, + }, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.cache_creation_input_tokens).toBe(800); + }); + + it("顶层扁平格式缺失 cache_creation_input_tokens 时应自动计算总量", () => { + const response = JSON.stringify({ + usage: { + cache_creation_5m_input_tokens: 400, + cache_creation_1h_input_tokens: 600, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.cache_creation_input_tokens).toBe(1000); + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(400); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(600); + }); + + it("混合回退:嵌套缺失某字段时顶层扁平补齐", () => { + const response = JSON.stringify({ + usage: { + cache_creation: { + ephemeral_5m_input_tokens: 200, + // 缺失 ephemeral_1h_input_tokens + }, + cache_creation_1h_input_tokens: 300, // 顶层扁平补齐 + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + // 5m 来自嵌套,1h 来自顶层扁平 + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(200); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(300); + expect(result.usageMetrics?.cache_creation_input_tokens).toBe(500); + expect(result.usageMetrics?.cache_ttl).toBe("mixed"); + }); + + it("当 cache_creation_input_tokens 存在时不应覆盖", () => { + const response = JSON.stringify({ + usage: { + cache_creation_input_tokens: 1000, + cache_creation: { + ephemeral_5m_input_tokens: 300, + ephemeral_1h_input_tokens: 500, + }, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + // 保留原值 + expect(result.usageMetrics?.cache_creation_input_tokens).toBe(1000); + }); + }); + + describe("Gemini 格式支持", () => { + it("应正确提取 Gemini usage 字段", () => { + const response = JSON.stringify({ + usageMetadata: { + promptTokenCount: 1000, + candidatesTokenCount: 500, + cachedContentTokenCount: 200, + }, + }); + + const result = parseUsageFromResponseText(response, "gemini"); + + expect(result.usageMetrics).not.toBeNull(); + // input_tokens = promptTokenCount - cachedContentTokenCount + expect(result.usageMetrics?.input_tokens).toBe(800); + expect(result.usageMetrics?.output_tokens).toBe(500); + expect(result.usageMetrics?.cache_read_input_tokens).toBe(200); + }); + + it("应正确处理 Gemini thoughtsTokenCount", () => { + const response = JSON.stringify({ + usageMetadata: { + promptTokenCount: 1000, + candidatesTokenCount: 500, + thoughtsTokenCount: 100, + }, + }); + + const result = parseUsageFromResponseText(response, "gemini"); + + // output_tokens = candidatesTokenCount + thoughtsTokenCount + expect(result.usageMetrics?.output_tokens).toBe(600); + }); + }); + + describe("OpenAI Response API 格式", () => { + it("应从 input_tokens_details.cached_tokens 提取缓存读取", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 1000, + output_tokens: 500, + input_tokens_details: { + cached_tokens: 200, + }, + }, + }); + + const result = parseUsageFromResponseText(response, "openai"); + + expect(result.usageMetrics?.cache_read_input_tokens).toBe(200); + }); + + it("顶层 cache_read_input_tokens 应优先于嵌套格式", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 1000, + cache_read_input_tokens: 300, + input_tokens_details: { + cached_tokens: 200, + }, + }, + }); + + const result = parseUsageFromResponseText(response, "openai"); + + // 顶层优先 + expect(result.usageMetrics?.cache_read_input_tokens).toBe(300); + }); + }); + + describe("SSE 流式响应解析", () => { + it("应正确合并 message_start 和 message_delta 的 usage", () => { + // 模拟 Claude SSE 流式响应 + const sseResponse = [ + "event: message_start", + 'data: {"type":"message_start","message":{"usage":{"input_tokens":1000,"cache_creation_input_tokens":500,"cache_creation":{"ephemeral_5m_input_tokens":200,"ephemeral_1h_input_tokens":300},"cache_read_input_tokens":100}}}', + "", + "event: message_delta", + 'data: {"type":"message_delta","usage":{"output_tokens":800}}', + "", + ].join("\n"); + + const result = parseUsageFromResponseText(sseResponse, "claude"); + + expect(result.usageMetrics).not.toBeNull(); + expect(result.usageMetrics?.input_tokens).toBe(1000); + expect(result.usageMetrics?.output_tokens).toBe(800); + expect(result.usageMetrics?.cache_creation_input_tokens).toBe(500); + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(200); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(300); + expect(result.usageMetrics?.cache_read_input_tokens).toBe(100); + }); + + it("message_delta 的值应优先于 message_start", () => { + const sseResponse = [ + "event: message_start", + 'data: {"type":"message_start","message":{"usage":{"input_tokens":100,"output_tokens":50}}}', + "", + "event: message_delta", + 'data: {"type":"message_delta","usage":{"input_tokens":1000,"output_tokens":500}}', + "", + ].join("\n"); + + const result = parseUsageFromResponseText(sseResponse, "claude"); + + // message_delta 优先 + expect(result.usageMetrics?.input_tokens).toBe(1000); + expect(result.usageMetrics?.output_tokens).toBe(500); + }); + + it("message_start 的 cache 细分应补充 message_delta 缺失的字段", () => { + const sseResponse = [ + "event: message_start", + 'data: {"type":"message_start","message":{"usage":{"cache_creation":{"ephemeral_5m_input_tokens":200,"ephemeral_1h_input_tokens":300}}}}', + "", + "event: message_delta", + 'data: {"type":"message_delta","usage":{"input_tokens":1000,"output_tokens":500,"cache_creation_input_tokens":500}}', + "", + ].join("\n"); + + const result = parseUsageFromResponseText(sseResponse, "claude"); + + // message_delta 的值 + expect(result.usageMetrics?.input_tokens).toBe(1000); + expect(result.usageMetrics?.output_tokens).toBe(500); + expect(result.usageMetrics?.cache_creation_input_tokens).toBe(500); + // message_start 补充的细分字段 + expect(result.usageMetrics?.cache_creation_5m_input_tokens).toBe(200); + expect(result.usageMetrics?.cache_creation_1h_input_tokens).toBe(300); + }); + }); + + describe("Codex provider 特殊处理", () => { + it("Codex 应从 input_tokens 中减去 cached_tokens", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 1000, + output_tokens: 500, + cache_read_input_tokens: 300, + }, + }); + + const result = parseUsageFromResponseText(response, "codex"); + + // adjustUsageForProviderType 会调整 input_tokens + expect(result.usageMetrics?.input_tokens).toBe(700); // 1000 - 300 + expect(result.usageMetrics?.cache_read_input_tokens).toBe(300); + }); + }); + + describe("边界情况", () => { + it("应处理所有值为 0 的情况", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 0, + output_tokens: 0, + cache_creation_input_tokens: 0, + cache_read_input_tokens: 0, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics).not.toBeNull(); + expect(result.usageMetrics?.input_tokens).toBe(0); + expect(result.usageMetrics?.output_tokens).toBe(0); + }); + + it("应处理部分字段缺失的情况", () => { + const response = JSON.stringify({ + usage: { + input_tokens: 1000, + }, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics?.input_tokens).toBe(1000); + expect(result.usageMetrics?.output_tokens).toBeUndefined(); + expect(result.usageMetrics?.cache_creation_input_tokens).toBeUndefined(); + }); + + it("应处理无效的 JSON", () => { + const result = parseUsageFromResponseText("invalid json", "claude"); + + expect(result.usageMetrics).toBeNull(); + }); + + it("应处理空的 usage 对象", () => { + const response = JSON.stringify({ + usage: {}, + }); + + const result = parseUsageFromResponseText(response, "claude"); + + expect(result.usageMetrics).toBeNull(); + }); + }); +});