Files
hermes-web/packages/client/src/stores/hermes/usage.ts
T
toor 5e1003ceb7
Build / build (push) Canceled after 0s
Playwright / e2e (push) Canceled after 0s
replace the monochrome theme with the CYBER neon-cyan skin
The project shipped a black-and-white theme that was only half converted:
the login page looked right, the message page was halfway, and the
settings page had not been started. This finishes the move to the CYBER
reference in prototype_reference/.

Colors now come from a single token source (styles/variables.scss), where
every semantic color is declared twice: once as OKLCH for the CSS, and
once as an RGB triple so translucent variants can be built as
rgba(var(--*-rgb), alpha). Cyan is the system itself, amber means the user
has to intervene, green means the machine is working, and red only ever
means something broke. The RGB triples are derived from the OKLCH values
so the two declarations cannot drift apart.

Pages adopt the .cy-* shell classes from the reference (view, panel,
kpis, tabs, grid, table, field) instead of ad-hoc markup, which is what
makes 19 of 26 views consistent. The remaining seven are deliberately
bespoke: chat streams, the kanban board, the workflow canvas, the
terminal, and the missing-agent screen.

The chart palette and the kanban column colors move into single-source
modules so they cannot drift either. A new guard test rejects raw hex
colors, named colors, and literal RGB triples anywhere in the client
source, with an explicit exemption list for the places that genuinely
cannot read a CSS variable. The existing chat semantics spec keeps the
chat chain honest.

No behavior changes: no logic, no i18n keys, no data structures, and the
/chat-run event contract is untouched.
2026-10-20 11:17:46 +08:00

208 lines
6.1 KiB
TypeScript

import { fetchUsageStats, type UsageStatsResponse } from '@/api/hermes/sessions'
import { CHART_CATEGORICAL } from '@/styles/chart-palette'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
interface DailyUsage {
date: string
input_tokens: number
output_tokens: number
cache_read_tokens: number
cache_write_tokens: number
sessions: number
errors: number
cost: number
visualTokens: number
inputPercent: number
outputPercent: number
cachePercent: number
}
interface ModelUsage {
model: string
inputTokens: number
outputTokens: number
cacheTokens: number
cacheWriteTokens: number
totalTokens: number
visualTokens: number
sessions: number
color: string
inputPercent: number
outputPercent: number
cachePercent: number
}
interface AgentUsage {
agent: string
inputTokens: number
outputTokens: number
cacheTokens: number
cacheWriteTokens: number
totalTokens: number
visualTokens: number
sessions: number
color: string
inputPercent: number
outputPercent: number
cachePercent: number
}
// 模型 / agent 配色取统一图表分类色板,见 styles/chart-palette.ts。
const MODEL_COLORS = CHART_CATEGORICAL
function normalizeModel(model: string | null | undefined): string {
const trimmed = (model || '').trim()
return trimmed || 'unknown'
}
function percent(part: number, total: number): number {
if (total <= 0) return 0
return part / total * 100
}
function getModelColor(model: string): string {
const normalized = normalizeModel(model)
let hash = 0
for (let i = 0; i < normalized.length; i += 1) {
hash = ((hash << 5) - hash) + normalized.charCodeAt(i)
hash |= 0
}
return MODEL_COLORS[Math.abs(hash) % MODEL_COLORS.length]
}
export const useUsageStore = defineStore('usage', () => {
const stats = ref<UsageStatsResponse | null>(null)
const isLoading = ref(false)
let latestRequestId = 0
async function loadSessions(days = 30) {
const requestId = ++latestRequestId
isLoading.value = true
try {
const response = await fetchUsageStats(days)
if (requestId === latestRequestId) {
stats.value = response
}
} catch (err) {
if (requestId === latestRequestId) {
console.error('Failed to load usage stats:', err)
}
} finally {
if (requestId === latestRequestId) {
isLoading.value = false
}
}
}
const hasData = computed(() => !!stats.value && stats.value.total_sessions > 0)
const totalInputTokens = computed(() => stats.value?.total_input_tokens ?? 0)
const totalOutputTokens = computed(() => stats.value?.total_output_tokens ?? 0)
const totalTokens = computed(() => totalInputTokens.value + totalOutputTokens.value)
const totalSessions = computed(() => stats.value?.total_sessions ?? 0)
const totalCacheTokens = computed(() => stats.value?.total_cache_read_tokens ?? 0)
const cacheHitRate = computed(() => {
const total = totalInputTokens.value + totalCacheTokens.value
if (total === 0) return null
return ((totalCacheTokens.value / total) * 100)
})
const estimatedCost = computed(() => stats.value?.total_cost ?? 0)
const modelUsage = computed<ModelUsage[]>(() => {
if (!stats.value) return []
return stats.value.model_usage.map(m => {
const model = normalizeModel(m.model)
const totalTokens = m.input_tokens + m.output_tokens
const visualTokens = totalTokens + m.cache_read_tokens
return {
model,
inputTokens: m.input_tokens,
outputTokens: m.output_tokens,
cacheTokens: m.cache_read_tokens,
cacheWriteTokens: m.cache_write_tokens,
totalTokens,
visualTokens,
sessions: m.sessions,
color: getModelColor(model),
inputPercent: percent(m.input_tokens, visualTokens),
outputPercent: percent(m.output_tokens, visualTokens),
cachePercent: percent(m.cache_read_tokens, visualTokens),
}
}).sort((a, b) => b.visualTokens - a.visualTokens)
})
const modelLegend = computed(() => {
const seen = new Set<string>()
return modelUsage.value.filter(m => {
if (seen.has(m.model)) return false
seen.add(m.model)
return true
}).map(m => ({ model: m.model, color: m.color }))
})
const agentUsage = computed<AgentUsage[]>(() => {
if (!stats.value) return []
return (stats.value.agent_usage ?? []).map(a => {
const agent = (a.agent || '').trim() || 'unknown'
const totalTokens = a.input_tokens + a.output_tokens
const visualTokens = totalTokens + a.cache_read_tokens
return {
agent,
inputTokens: a.input_tokens,
outputTokens: a.output_tokens,
cacheTokens: a.cache_read_tokens,
cacheWriteTokens: a.cache_write_tokens,
totalTokens,
visualTokens,
sessions: a.sessions,
color: getModelColor(`agent:${agent}`),
inputPercent: percent(a.input_tokens, visualTokens),
outputPercent: percent(a.output_tokens, visualTokens),
cachePercent: percent(a.cache_read_tokens, visualTokens),
}
}).sort((a, b) => b.visualTokens - a.visualTokens)
})
const dailyUsage = computed<DailyUsage[]>(() => (stats.value?.daily_usage ?? []).map(d => {
const visualTokens = d.input_tokens + d.output_tokens + d.cache_read_tokens
return {
...d,
visualTokens,
inputPercent: percent(d.input_tokens, visualTokens),
outputPercent: percent(d.output_tokens, visualTokens),
cachePercent: percent(d.cache_read_tokens, visualTokens),
}
}))
const avgSessionsPerDay = computed(() => {
if (!stats.value || stats.value.daily_usage.length === 0) return 0
const daysWithActivity = stats.value.daily_usage.filter(d => d.sessions > 0).length
const days = Math.max(1, daysWithActivity)
return totalSessions.value / days
})
return {
stats,
isLoading,
hasData,
loadSessions,
totalInputTokens,
totalOutputTokens,
totalTokens,
totalSessions,
totalCacheTokens,
cacheHitRate,
estimatedCost,
modelUsage,
modelLegend,
agentUsage,
dailyUsage,
avgSessionsPerDay,
getModelColor,
}
})