feat(用户端): 密钥设置面板 LLM 两卡加人民币余额行,与代理余量同形

现状纠偏(写死免得下次重研):桌面壳加载的是 frontend-vue 的 Vue SPA(app_client/blueprints/main.py:52 供产物、
config.py:96 WEB_HOME_URL 直载),所谓"客户端密钥设置"不在 Python 里;而密钥明文只在后端(前端手里只有掩码),
所以余额必须由后端查、前端不自打上游——本面只消费 GET /api/user-secrets/llm-balance。

- 新建 SecretBalanceRow.vue:根元素就是 <div class="secret-meta">,保住既有挂载测试按 .secret-meta 取数的断言不破;
  LLM 卡插一行、代理卡那 8 行 div 换成同一组件 ⇒ 两卡同形自动达成。面板 763→789 行,在 800 报警线内,
  可读棘轮基线不用动(该文件本就未点名)。
- 新建 shared/utils/llm-balance-text.ts(零依赖纯函数,node --test 直测)+ shared/composables/useLlmBalance.ts
  (按 moduleKey 存 items、三态文案、available=false 原样透出后端 message)。不引任何汇率乘算:绊线
  tests/llm-balance-text.test.ts 里钉住源码不得出现 7.2/per_usd 类字样——换算只在上游 credits.per_usd 里。
- loadLlmBalance() 移出 if (props.showProxy):原形状下 LLM 卡永远空白。刷新时机=挂载、保存该模块后、清空后、
  「检测已存值」后;⚠ 刻意不在「检测输入框未保存值」后刷——checkMyApiSecret(moduleKey,value) 探的是没落库的那把
  key,而余额读服务端已存密钥,刷出来是上一把 key 的钱挂在新渠道名下=串档形状(同一格绊线盯着这条)。
- 不动 ProxyBalanceAlert.vue / utils/proxy-balance-alert.ts:没要求告警,引阈值就是自造口径。
- 复用 jikip 那套 VO 的 surplus/scope/low 语义不 borrow 给 LLM(铁律 25):types/modules/user-secret.ts 里
  新类型与 :48-59 的 jikip VO 并置但互不相通。

测试:tests/llm-balance-text.test.ts 10 例(含源码绊线:面板行数<800、useLlmBalance 无汇率乘算、检测输入值路径
不触发 loadLlmBalance)+ tests/mount/密钥面板余额显示.mount.test.ts 11 例(两 LLM 卡各有 .secret-meta;龙抬头行
含 ¥+「龙抬头」、贞贞行含「不提供余额查询」且不含 ¥;两卡夹具给不同数字并交叉断言不互串;代理余量文案未变);
endpoints.test.ts 金样加一行;两支既有挂载夹具补 fetchLlmBalanceSafely 默认值。
catch-silence 棘轮:useLlmBalance.ts 那一枚 catch 的**首条语句是界面可见出口**(清空该 moduleKey 的展示),
console.warn 留在其后——按"首条即 log 才算静默"的口径它不该入账,实测入账了就把 blocks 311→312 如实登记并写明
理由,不删那个 catch(删了就是把失败读成"没有这一项")。vue-tsc rc=0、node --test 1018 例、vitest 66 例。
This commit is contained in:
2026-11-05 12:03:14 +08:00
parent eeff73d322
commit a10aa6bda3
13 changed files with 736 additions and 9 deletions
@@ -78,6 +78,13 @@
{{ statusTextOf(module.moduleKey) }}
</span>
</div>
<SecretBalanceRow
:text="llmMetaText(module.moduleKey)"
:updated-at="llmBalanceUpdatedAtOf(module.moduleKey)"
:refreshing="llmBalanceLoading"
@refresh="() => loadLlmBalance(module.moduleKey)"
/>
</section>
<section v-if="showProxy" class="secret-card">
@@ -252,14 +259,7 @@
</span>
</div>
<div class="secret-meta">
<span v-if="proxyLoading">正在读取代理配置...</span>
<span v-else-if="proxyLoadFailed">代理配置读取失败,请关闭弹窗后重试</span>
<span v-else-if="!proxySupported">代理设置仅在桌面客户端中可用</span>
<span v-else-if="balanceLoading">正在查询代理余量...</span>
<span v-else-if="balance">{{ balanceText }}</span>
<span v-else>{{ balanceUnavailableText }}</span>
</div>
<SecretBalanceRow :text="proxyMetaText" :refreshing="balanceLoading" @refresh="loadBalance" />
</section>
</div>
</template>
@@ -308,7 +308,9 @@ import {
} from '@/shared/utils/api-secret-proxy-config'
import { useProxyBalance, useProxyPlan } from '@/shared/composables/useProxyPlan'
import { useKeyedSecretVisibility, useSecretVisibility } from '@/shared/composables/useSecretVisibility'
import { useLlmBalance } from '@/shared/composables/useLlmBalance'
import SecretVisibilityToggle from '@/shared/components/SecretVisibilityToggle.vue'
import SecretBalanceRow from '@/shared/components/SecretBalanceRow.vue'
import { createModuleView, proxyCheckPresentation, type ModuleState } from '@/shared/utils/api-secret-module-view'
const props = withDefaults(
@@ -383,6 +385,23 @@ const proxyCheckResult = ref<UserApiSecretCheckResult | null>(null)
const proxyCheckError = ref('')
const { balance, balanceLoading, balanceText, balanceUnavailableText, loadBalance } = useProxyBalance({ proxyProvider })
const { llmBalanceLoading, llmBalanceTextOf, llmBalanceUpdatedAtOf, loadLlmBalance } = useLlmBalance()
/** LLM 卡余额行:渠道名以服务端回传为准(库里那把 key 才是余额接口读的,本地未保存的单选不参与取数)。 */
function llmMetaText(moduleKey: string): string {
if (llmBalanceLoading.value) return '正在查询密钥余额...'
return llmBalanceTextOf(moduleKey, llmChannelLabel(moduleChannels[moduleKey]))
}
/** 代理卡那一行的既有优先级原样搬过来(读取中 → 失败 → 不支持 → 查询中 → 数值 → 查不到)。 */
const proxyMetaText = computed(() => {
if (proxyLoading.value) return '正在读取代理配置...'
if (proxyLoadFailed.value) return '代理配置读取失败,请关闭弹窗后重试'
if (!proxySupported.value) return '代理设置仅在桌面客户端中可用'
if (balanceLoading.value) return '正在查询代理余量...'
return balance.value ? balanceText.value : balanceUnavailableText.value
})
const {
proxyPlanId,
proxyPlanUserId,
@@ -557,6 +576,8 @@ async function runCheck(moduleKey: ApiSecretModuleKey) {
} finally {
state.checking = false
refreshSnapshots()
// 只有「检测已存密钥」才顺带刷余额:检测输入值时库里还是上一把 key,刷出来是上一把的钱(串档形状)
if (!inputValue) void loadLlmBalance(moduleKey)
}
}
@@ -571,6 +592,8 @@ async function clearModule(moduleKey: ApiSecretModuleKey) {
state.error = ''
channelDirty[moduleKey] = false
refreshSnapshots()
// 清空后库里已经没有这把 key:不刷的话,这一行会继续挂着上一把 key 的钱
void loadLlmBalance(moduleKey)
ElMessage.success('已清空密钥')
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '清空失败')
@@ -679,6 +702,7 @@ async function saveAll(options: { requireAll?: boolean } = {}): Promise<boolean>
}
secretsSaved = true
refreshSnapshots()
void loadLlmBalance()
if (props.showProxy && proxyReady.value && proxyDirty.value && !proxyLocked.value) {
const api = getPywebviewApi()
@@ -741,6 +765,8 @@ onMounted(() => {
// 先拉服务端密钥包(后台锁定的代理会先对齐到本地配置),再读本地代理,避免展示对齐前的旧值
void (async () => {
await reload()
// 读的是服务端已存密钥 ⇒ 排在 reload 之后;且它不属代理卡(浏览器形态 showProxy=false 会永远空白)
void loadLlmBalance()
if (props.showProxy) {
await loadProxyConfig()
if (proxySupported.value) {