feat(frontend): 客户端损耗保护弹窗与共享密钥提示
- 30 秒轮询损耗状态;暂停时红色弹窗/横幅并提供「继续任务」 - 共享密钥黄色提示条,按类型去重,不重复刷屏 - 通知铃铛登记 cost_guard / secret_shared 场景
This commit is contained in:
@@ -0,0 +1,278 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* 代理/LLM 损耗保护常驻提示(2026-10-24 需求):
|
||||
* 异常时客户端立刻弹窗并暂停该账号的新任务;已跑完的任务与结果行不受影响。
|
||||
*
|
||||
* - 暂停:红色不可忽略横幅 + 首次弹确认框,用户排查后点「继续任务」走 resume;
|
||||
* - 共用密钥:黄色提示条,只报账号数,不阻断任务;
|
||||
* - 轮询失败保留上一轮状态(与 ProxyBalanceAlert 同口径),不把网络抖动刷成误报。
|
||||
*/
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import {
|
||||
fetchCostGuardStatus,
|
||||
resumeCostGuard,
|
||||
type CostGuardStatus,
|
||||
type SharedSecretWarning,
|
||||
} from '@/shared/api/types/modules/cost-guard'
|
||||
import { AUTH_TOKEN_KEY } from '@/shared/auth/session-keys'
|
||||
import {
|
||||
COST_GUARD_POLL_MS,
|
||||
pausedPromptKey,
|
||||
sharedWarningKey,
|
||||
usageLine,
|
||||
} from '@/shared/utils/cost-guard-alert'
|
||||
|
||||
const status = ref<CostGuardStatus | null>(null)
|
||||
const sharedWarnings = ref<SharedSecretWarning[]>([])
|
||||
const resuming = ref(false)
|
||||
let timer: number | null = null
|
||||
let inflight = false
|
||||
let disposed = false
|
||||
let promptedKey = ''
|
||||
const warnedSharedKeys = new Set<string>()
|
||||
|
||||
function hasLoginToken(): boolean {
|
||||
if (typeof window === 'undefined') return false
|
||||
return Boolean(window.localStorage.getItem(AUTH_TOKEN_KEY))
|
||||
}
|
||||
|
||||
async function refresh(): Promise<void> {
|
||||
if (disposed || inflight) return
|
||||
if (!hasLoginToken()) {
|
||||
status.value = null
|
||||
sharedWarnings.value = []
|
||||
return
|
||||
}
|
||||
inflight = true
|
||||
try {
|
||||
const next = await fetchCostGuardStatus().catch((error) => {
|
||||
console.warn('[cost-guard] 损耗状态读取失败,本轮不提示:', error)
|
||||
return null
|
||||
})
|
||||
if (next === null || disposed) return
|
||||
status.value = next
|
||||
sharedWarnings.value = next.sharedWarnings || []
|
||||
notifySharedWarnings(sharedWarnings.value)
|
||||
maybePromptPaused(next)
|
||||
} finally {
|
||||
inflight = false
|
||||
}
|
||||
}
|
||||
|
||||
function notifySharedWarnings(warnings: SharedSecretWarning[]): void {
|
||||
for (const warning of warnings) {
|
||||
const key = sharedWarningKey(warning)
|
||||
if (warnedSharedKeys.has(key)) continue
|
||||
warnedSharedKeys.add(key)
|
||||
ElMessage.warning({ message: warning.message, duration: 8000, showClose: true })
|
||||
}
|
||||
}
|
||||
|
||||
function maybePromptPaused(next: CostGuardStatus): void {
|
||||
const key = pausedPromptKey(next)
|
||||
if (!key || key === promptedKey) return
|
||||
promptedKey = key
|
||||
ElMessageBox.confirm(
|
||||
`${next.reason}\n\n${usageLine(next)}\n\n已完成的任务与数据不受影响。排查后点击「继续任务」;也可以先点「稍后处理」,处理完再从右上角横幅继续。`,
|
||||
'任务已自动暂停(损耗保护)',
|
||||
{
|
||||
confirmButtonText: '继续任务',
|
||||
cancelButtonText: '稍后处理',
|
||||
distinguishCancelAndClose: true,
|
||||
closeOnClickModal: false,
|
||||
closeOnPressEscape: false,
|
||||
type: 'error',
|
||||
customStyle: { whiteSpace: 'pre-line' },
|
||||
},
|
||||
)
|
||||
.then(() => resume())
|
||||
.catch(() => {
|
||||
/* 稍后处理/关闭:保留横幅,不重复弹 */
|
||||
})
|
||||
}
|
||||
|
||||
async function resume(): Promise<void> {
|
||||
if (resuming.value) return
|
||||
resuming.value = true
|
||||
try {
|
||||
const next = await resumeCostGuard()
|
||||
status.value = next
|
||||
sharedWarnings.value = next.sharedWarnings || []
|
||||
promptedKey = ''
|
||||
ElMessage.success('已继续任务,后续派发会从当前进度接着跑')
|
||||
} catch (error) {
|
||||
console.warn('[cost-guard] 继续任务失败:', error)
|
||||
ElMessage.error('继续任务失败,请稍后重试')
|
||||
} finally {
|
||||
resuming.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function refreshWhenVisible(): void {
|
||||
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return
|
||||
void refresh()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void refresh()
|
||||
timer = window.setInterval(() => void refresh(), COST_GUARD_POLL_MS)
|
||||
window.addEventListener('focus', refreshWhenVisible)
|
||||
document.addEventListener('visibilitychange', refreshWhenVisible)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
disposed = true
|
||||
if (timer !== null) window.clearInterval(timer)
|
||||
window.removeEventListener('focus', refreshWhenVisible)
|
||||
document.removeEventListener('visibilitychange', refreshWhenVisible)
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="status?.paused || sharedWarnings.length" class="cost-guard-stack">
|
||||
<div
|
||||
v-if="status?.paused"
|
||||
class="cost-guard-alert cost-guard-alert--paused"
|
||||
role="alert"
|
||||
aria-live="assertive"
|
||||
>
|
||||
<div class="cost-guard-alert-icon" aria-hidden="true">!</div>
|
||||
<div class="cost-guard-alert-content">
|
||||
<div class="cost-guard-alert-title">任务已自动暂停(损耗保护)</div>
|
||||
<div class="cost-guard-alert-reason">{{ status.reason }}</div>
|
||||
<div class="cost-guard-alert-metrics">{{ usageLine(status) }}</div>
|
||||
<div class="cost-guard-alert-hint">
|
||||
已完成的任务与结果行不受影响。请检查「密钥设置」与被暂停任务,排查后继续。
|
||||
</div>
|
||||
<button class="cost-guard-alert-button" :disabled="resuming" @click="resume">
|
||||
{{ resuming ? '正在恢复…' : '继续任务' }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="sharedWarnings.length"
|
||||
class="cost-guard-alert cost-guard-alert--shared"
|
||||
role="status"
|
||||
>
|
||||
<div class="cost-guard-alert-icon" aria-hidden="true">i</div>
|
||||
<div class="cost-guard-alert-content">
|
||||
<div class="cost-guard-alert-title">密钥共用提示</div>
|
||||
<div
|
||||
v-for="warning in sharedWarnings"
|
||||
:key="sharedWarningKey(warning)"
|
||||
class="cost-guard-alert-reason"
|
||||
>
|
||||
{{ warning.message }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
/* 放在代理余量警告(top:68px)下方,两个常驻提示同时出现时不互相遮挡 */
|
||||
.cost-guard-stack {
|
||||
position: fixed;
|
||||
top: 230px;
|
||||
right: 16px;
|
||||
z-index: 3000;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
width: 400px;
|
||||
max-width: calc(100vw - 32px);
|
||||
}
|
||||
|
||||
.cost-guard-alert {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 10px;
|
||||
width: 100%;
|
||||
padding: 14px 16px;
|
||||
box-sizing: border-box;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 14px 36px -18px rgba(60, 20, 20, 0.55);
|
||||
}
|
||||
|
||||
.cost-guard-alert--paused {
|
||||
border: 1px solid #d64545;
|
||||
background: #fff1f1;
|
||||
color: #7d1d1d;
|
||||
}
|
||||
|
||||
.cost-guard-alert--shared {
|
||||
border: 1px solid #e0a13c;
|
||||
background: #fff8e8;
|
||||
color: #7a4a06;
|
||||
}
|
||||
|
||||
.cost-guard-alert-icon {
|
||||
flex-shrink: 0;
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
border-radius: 50%;
|
||||
color: #ffffff;
|
||||
font-size: 14px;
|
||||
font-weight: 800;
|
||||
line-height: 22px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.cost-guard-alert--paused .cost-guard-alert-icon {
|
||||
background: #d64545;
|
||||
}
|
||||
|
||||
.cost-guard-alert--shared .cost-guard-alert-icon {
|
||||
background: #e0a13c;
|
||||
}
|
||||
|
||||
.cost-guard-alert-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 5px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.cost-guard-alert-title {
|
||||
font-size: 14px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.cost-guard-alert-reason,
|
||||
.cost-guard-alert-metrics {
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.cost-guard-alert-metrics {
|
||||
color: #9a4a4a;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.cost-guard-alert-hint {
|
||||
color: #9a4a4a;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.cost-guard-alert-button {
|
||||
align-self: flex-start;
|
||||
margin-top: 4px;
|
||||
padding: 6px 14px;
|
||||
border: 0;
|
||||
border-radius: 8px;
|
||||
background: #d64545;
|
||||
color: #ffffff;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.cost-guard-alert-button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: default;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user