Files
crawler-plugin/frontend-vue/src/shared/utils/cost-guard-alert.ts
T

35 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { CostGuardStatus, SharedSecretWarning } from '@/shared/api/types/modules/cost-guard'
/** 损耗状态轮询间隔:与后端每分钟扫描配合,30 秒足够把暂停提示送到界面。 */
export const COST_GUARD_POLL_MS = 30_000
export function formatCount(value: number | null | undefined): string {
const number = Number(value ?? 0)
if (!Number.isFinite(number)) return '0'
return number.toLocaleString('zh-CN')
}
/** 暂停弹窗/横幅里的用量行:当日累计 + 本轮增量,用户一眼看出是哪一项超了。 */
export function usageLine(status: CostGuardStatus | null): string {
if (!status) return ''
const today = status.today
const window = status.window
return `今日代理提取 ${formatCount(today.proxy)} 次 · LLM ${formatCount(today.llmCalls)} 次 / ${formatCount(today.llmTokens)} tokens;`
+ `最近 ${window.seconds} 秒:代理 ${formatCount(window.proxy)} 次 · LLM ${formatCount(window.llmCalls)} 次 / ${formatCount(window.llmTokens)} tokens`
}
/** 同一个暂停事件只弹一次:pausedAt 变化(新的自动暂停)才重新弹。 */
export function pausedPromptKey(status: CostGuardStatus | null): string {
if (!status?.paused) return ''
return status.pausedAt || status.reason || 'paused'
}
export function sharedWarningKey(warning: SharedSecretWarning): string {
return `${warning.type}|${warning.moduleLabel}|${warning.accounts}`
}
/** 代理 IP 共用弹窗文案:精简口语版,不搬运后端的技术口径。 */
export function proxySharedToastText(warning: SharedSecretWarning): string {
return `这把代理链接被 ${warning.accounts} 个账号共用,别人用多了你就不够用了。建议在「密钥设置」里配一把自己的。`
}