mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-05-27 16:40:28 +08:00
154342ca00
Parse Claude Code JSONL session files (~/.claude/projects/) and Codex SQLite database (~/.codex/state_5.sqlite) to track API usage without requiring proxy interception. This enables usage statistics for users who don't use the proxy feature. Key changes: - Add session_usage.rs: incremental JSONL parser with message.id dedup - Add session_usage_codex.rs: import thread-level token data from Codex - Add data_source column to proxy_request_logs (proxy/session_log/codex_db) - Add session_log_sync table for tracking parse offsets - Background sync every 60s + manual sync via DataSourceBar UI - Schema migration v7→v8 - i18n support for zh/en/ja
130 lines
3.0 KiB
TypeScript
130 lines
3.0 KiB
TypeScript
import { invoke } from "@tauri-apps/api/core";
|
|
import type {
|
|
UsageSummary,
|
|
DailyStats,
|
|
ProviderStats,
|
|
ModelStats,
|
|
RequestLog,
|
|
LogFilters,
|
|
ModelPricing,
|
|
ProviderLimitStatus,
|
|
PaginatedLogs,
|
|
SessionSyncResult,
|
|
DataSourceSummary,
|
|
} from "@/types/usage";
|
|
import type { UsageResult } from "@/types";
|
|
import type { AppId } from "./types";
|
|
import type { TemplateType } from "@/config/constants";
|
|
|
|
export const usageApi = {
|
|
// Provider usage script methods
|
|
query: async (providerId: string, appId: AppId): Promise<UsageResult> => {
|
|
return invoke("queryProviderUsage", { providerId, app: appId });
|
|
},
|
|
|
|
testScript: async (
|
|
providerId: string,
|
|
appId: AppId,
|
|
scriptCode: string,
|
|
timeout?: number,
|
|
apiKey?: string,
|
|
baseUrl?: string,
|
|
accessToken?: string,
|
|
userId?: string,
|
|
templateType?: TemplateType,
|
|
): Promise<UsageResult> => {
|
|
return invoke("testUsageScript", {
|
|
providerId,
|
|
app: appId,
|
|
scriptCode,
|
|
timeout,
|
|
apiKey,
|
|
baseUrl,
|
|
accessToken,
|
|
userId,
|
|
templateType,
|
|
});
|
|
},
|
|
|
|
// Proxy usage statistics methods
|
|
getUsageSummary: async (
|
|
startDate?: number,
|
|
endDate?: number,
|
|
): Promise<UsageSummary> => {
|
|
return invoke("get_usage_summary", { startDate, endDate });
|
|
},
|
|
|
|
getUsageTrends: async (
|
|
startDate?: number,
|
|
endDate?: number,
|
|
): Promise<DailyStats[]> => {
|
|
return invoke("get_usage_trends", { startDate, endDate });
|
|
},
|
|
|
|
getProviderStats: async (): Promise<ProviderStats[]> => {
|
|
return invoke("get_provider_stats");
|
|
},
|
|
|
|
getModelStats: async (): Promise<ModelStats[]> => {
|
|
return invoke("get_model_stats");
|
|
},
|
|
|
|
getRequestLogs: async (
|
|
filters: LogFilters,
|
|
page: number = 0,
|
|
pageSize: number = 20,
|
|
): Promise<PaginatedLogs> => {
|
|
return invoke("get_request_logs", {
|
|
filters,
|
|
page,
|
|
pageSize,
|
|
});
|
|
},
|
|
|
|
getRequestDetail: async (requestId: string): Promise<RequestLog | null> => {
|
|
return invoke("get_request_detail", { requestId });
|
|
},
|
|
|
|
getModelPricing: async (): Promise<ModelPricing[]> => {
|
|
return invoke("get_model_pricing");
|
|
},
|
|
|
|
updateModelPricing: async (
|
|
modelId: string,
|
|
displayName: string,
|
|
inputCost: string,
|
|
outputCost: string,
|
|
cacheReadCost: string,
|
|
cacheCreationCost: string,
|
|
): Promise<void> => {
|
|
return invoke("update_model_pricing", {
|
|
modelId,
|
|
displayName,
|
|
inputCost,
|
|
outputCost,
|
|
cacheReadCost,
|
|
cacheCreationCost,
|
|
});
|
|
},
|
|
|
|
deleteModelPricing: async (modelId: string): Promise<void> => {
|
|
return invoke("delete_model_pricing", { modelId });
|
|
},
|
|
|
|
checkProviderLimits: async (
|
|
providerId: string,
|
|
appType: string,
|
|
): Promise<ProviderLimitStatus> => {
|
|
return invoke("check_provider_limits", { providerId, appType });
|
|
},
|
|
|
|
// Session usage sync
|
|
syncSessionUsage: async (): Promise<SessionSyncResult> => {
|
|
return invoke("sync_session_usage");
|
|
},
|
|
|
|
getDataSourceBreakdown: async (): Promise<DataSourceSummary[]> => {
|
|
return invoke("get_usage_data_sources");
|
|
},
|
|
};
|