Files
cline-multi-mirror/src/core/controller/state/getUsageStats.ts
T
Adam-PAN 6965212ff7 fix: resolve Base URL input garbled text and collapsed width in API settings
- Fix scrambled text when typing in Base URL fields: debounced input hook now ignores echo round-trips of its own saves and defers unexpected external updates during an edit grace period

- Serialize API configuration saves through a queue so rapid successive updates are applied in order on the backend

- Fix Base URL input collapsing to tiny width: Radix TooltipTrigger now renders a full-width block wrapper instead of the default shrink-to-fit button

- Trim Base URL values only on commit instead of every keystroke to prevent cursor jumps

- Skip redundant save on mount and flush uncommitted edits on unmount

- Bump version to 4.3.1, update CHANGELOG and README
2026-09-17 02:53:43 +08:00

42 lines
1.9 KiB
TypeScript

import { usageLog } from "@core/storage/usage-log"
import { StringRequest } from "@shared/proto/cline/common"
import { UsageStats } from "@shared/proto/cline/state"
import { aggregateUsageEntries, aggregateUsageStats, type UsageStatsResult, type UsageTimeRange } from "@/shared/usage-stats"
import type { Controller } from "../index"
const VALID_RANGES: UsageTimeRange[] = ["today", "week", "month", "all"]
/**
* Aggregates usage statistics over the durable usage log (usage-log.jsonl),
* which records every model call independently of task history — so stats
* stay stable even when tasks are deleted. Falls back to aggregating the
* full task history when the log is empty (e.g. users who haven't made any
* requests since this log was introduced), so pre-existing history still shows.
* @param controller The controller instance
* @param request The request containing the time range ("today" | "week" | "month" | "all")
* @returns UsageStats with the serialized UsageStatsResult JSON payload
*/
export async function getUsageStats(controller: Controller, request: StringRequest): Promise<UsageStats> {
const range = VALID_RANGES.includes(request.value as UsageTimeRange) ? (request.value as UsageTimeRange) : "all"
// Prefer the durable per-request log; it survives task deletion and
// aggregates are unaffected by VSCode-side history truncation.
const logEntries = usageLog.readAll()
let result: UsageStatsResult
if (logEntries.length > 0) {
result = aggregateUsageEntries(logEntries, range)
} else {
// Fallback: aggregate from task history (legacy data before the log existed).
const taskHistory = controller.stateManager.getGlobalStateKey("taskHistory")
result = aggregateUsageStats(taskHistory, range)
}
return UsageStats.create({
usageJson: JSON.stringify({
totals: result.totals,
modelStats: Array.from(result.modelStats.entries()),
trend: result.trend,
}),
})
}