feat: stream chat and settle metered CNY usage atomically

This commit is contained in:
wlx committed 2026-09-20 01:36:22 +08:00
1 parent 7360cf3ebe
commit b46ee72f2d
28 files changed
+1098 -86

No files matched your search

+2 -2
View File
@@ -35,7 +35,7 @@ const entries = ref<WalletEntry[]>([])
const ledgerLoading = ref(false)
const filteredUsers = computed(() => users.value.filter(user => `${user.account} ${user.displayName}`.toLowerCase().includes(query.value.toLowerCase())))
const totalBalance = computed(() => Object.values(balances.value).reduce((sum, balance) => sum + Number(balance), 0))
const money = (value: number | undefined) => `¥ ${Number(value || 0).toFixed(2)}`
const money = (value: number | undefined) => `¥ ${Number(value || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}`
const roleName = (value: string) => ({ user: '普通用户', admin: '管理员', super_admin: '超级管理员' })[value] || value
async function refresh() {
loading.value = true
@@ -126,7 +126,7 @@ async function copyText(value: string) {
</form>
<template #footer><button class="button secondary" :disabled="saving" @click="editor = ''">关闭</button><button v-if="!inviteCode || editor !== 'invite'" class="button primary" type="submit" form="account-editor" :disabled="saving">{{ saving ? '提交中…' : editor === 'credit' ? '确认调整' : '确认' }}</button></template>
</UiModal>
<SlidePanel v-if="ledgerUser" :title="`${ledgerUser.displayName} · 额度流水`" @close="ledgerUser = undefined"><p>币种:人民币 CNY</p><p v-if="ledgerLoading">加载中…</p><div v-for="entry in entries" :key="entry.id" class="wallet-entry"><strong>{{ Number(entry.amount) >= 0 ? '+' : '−' }} {{ money(Math.abs(Number(entry.amount))) }}</strong><small> · {{ ({ increase: '增加', decrease: '减少', set: '设置余额' })[entry.operation] }}</small><p>{{ entry.reason }}</p><small>{{ entry.actor }} · {{ new Date(entry.createdAt).toLocaleString('zh-CN') }}</small><p>{{ money(entry.balanceBefore) }} → {{ money(entry.balanceAfter) }}</p></div><p v-if="!entries.length && !ledgerLoading">暂无流水</p></SlidePanel>
<SlidePanel v-if="ledgerUser" :title="`${ledgerUser.displayName} · 额度流水`" @close="ledgerUser = undefined"><p>币种:人民币 CNY</p><p v-if="ledgerLoading">加载中…</p><div v-for="entry in entries" :key="entry.id" class="wallet-entry"><strong>{{ Number(entry.amount) >= 0 ? '+' : '−' }} {{ money(Math.abs(Number(entry.amount))) }}</strong><small> · {{ ({ increase: '增加', decrease: '减少', set: '设置余额', usage: '模型扣费' })[entry.operation] }}</small><p>{{ entry.reason }}</p><small>{{ entry.actor }} · {{ new Date(entry.createdAt).toLocaleString('zh-CN') }}</small><p>{{ money(entry.balanceBefore) }} → {{ money(entry.balanceAfter) }}</p></div><p v-if="!entries.length && !ledgerLoading">暂无流水</p></SlidePanel>
</template>
<style scoped>
+5 -3
View File
@@ -27,7 +27,7 @@ async function remove() {
}
const channel = reactive({ code: '', name: '', protocol: 'openai-compatible' as CatalogProvider['protocol'], sourceType: 'relay' as CatalogProvider['sourceType'], baseUrl: '', credentialType: 'api-key' as NonNullable<CatalogProvider['credentialType']>, credentialSecret: '', description: '', status: 'active' as CatalogProvider['status'] })
const model = reactive({ publicName: '', modelKey: '', type: 'text' as LogicalModel['type'], mode: 'sync' as LogicalModel['mode'], description: '', inputTypes: 'text', outputTypes: 'text', status: 'active' as LogicalModel['status'] })
const group = reactive({ logicalModelId: '', providerId: '', sourceLabel: '', upstreamModel: '', input: '', output: '', unit: 'CNY / 1M tokens', priority: '100', maxConcurrency: '1', status: 'active' as CatalogModel['status'] })
const group = reactive({ logicalModelId: '', providerId: '', sourceLabel: '', upstreamModel: '', input: '', output: '', cachedInput: '', unit: 'CNY / 1M tokens', priority: '100', maxConcurrency: '1', status: 'active' as CatalogModel['status'] })
const shownModels = computed(() => models.value.filter(item => `${item.publicName} ${item.modelKey || ''}`.toLowerCase().includes(query.value.toLowerCase())))
const shownChannels = computed(() => channels.value.filter(item => `${item.name} ${item.code}`.toLowerCase().includes(query.value.toLowerCase())))
const channelName = (id: string) => channels.value.find(item => item.id === id)?.name || id
@@ -59,7 +59,7 @@ function editModel(item?: LogicalModel) {
}
function editGroup(parent: LogicalModel, item?: CatalogModel) {
editingId.value = item?.id
Object.assign(group, { logicalModelId: parent.id, providerId: item?.providerId || '', sourceLabel: item?.sourceLabel || '', upstreamModel: item?.upstreamModel || '', input: item?.pricing?.input?.toString() || '', output: item?.pricing?.output?.toString() || '', unit: item?.pricing?.unit || 'CNY / 1M tokens', priority: String(item?.priority ?? 100), maxConcurrency: String(item?.maxConcurrency ?? 1), status: item?.status || 'active' })
Object.assign(group, { logicalModelId: parent.id, providerId: item?.providerId || '', sourceLabel: item?.sourceLabel || '', upstreamModel: item?.upstreamModel || '', input: item?.pricing?.input?.toString() || '', output: item?.pricing?.output?.toString() || '', cachedInput: item?.pricing?.cachedInput?.toString() || '', unit: item?.pricing?.unit || 'CNY / 1M tokens', priority: String(item?.priority ?? 100), maxConcurrency: String(item?.maxConcurrency ?? 1), status: item?.status || 'active' })
editor.value = 'group'
}
async function save() {
@@ -76,7 +76,7 @@ async function save() {
await catalogClient.saveDefinition(editingId.value, { ...model, inputTypes: model.inputTypes.split(',').map(s => s.trim()).filter(Boolean), outputTypes: model.outputTypes.split(',').map(s => s.trim()).filter(Boolean) })
} else {
if (!group.providerId || !group.sourceLabel.trim() || !group.upstreamModel.trim()) throw new Error('请选择渠道并填写分组名称、上游模型标识')
await catalogClient.saveGroup(editingId.value, { logicalModelId: group.logicalModelId, providerId: group.providerId, sourceLabel: group.sourceLabel, upstreamModel: group.upstreamModel, status: group.status, priority: Number(group.priority), maxConcurrency: Number(group.maxConcurrency), pricing: { input: group.input === '' ? undefined : Number(group.input), output: group.output === '' ? undefined : Number(group.output), unit: group.unit } })
await catalogClient.saveGroup(editingId.value, { logicalModelId: group.logicalModelId, providerId: group.providerId, sourceLabel: group.sourceLabel, upstreamModel: group.upstreamModel, status: group.status, priority: Number(group.priority), maxConcurrency: Number(group.maxConcurrency), pricing: { input: group.input === '' ? undefined : Number(group.input), output: group.output === '' ? undefined : Number(group.output), cachedInput: group.cachedInput === '' ? undefined : Number(group.cachedInput), unit: group.unit } })
}
editor.value = ''
await refresh()
@@ -141,7 +141,9 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
<label class="form-label" for="group-channel">渠道</label><select id="group-channel" v-model="group.providerId" class="text-input" required><option value="" disabled>请选择渠道</option><option v-for="item in channels" :key="item.id" :value="item.id">{{ item.name }}{{ item.status === 'disabled' ? '(已停用)' : '' }}</option></select>
<label class="form-label" for="group-upstream">上游模型标识</label><input id="group-upstream" v-model="group.upstreamModel" class="text-input" required maxlength="160" />
<div class="modal-form-grid"><label><span class="form-label">输入价格</span><input v-model="group.input" class="text-input" type="number" min="0" step="any" /></label><label><span class="form-label">输出价格</span><input v-model="group.output" class="text-input" type="number" min="0" step="any" /></label></div>
<label class="form-label">缓存命中输入价格(留空按普通输入价格)<input v-model="group.cachedInput" class="text-input" type="number" min="0" step="any" /></label>
<label class="form-label" for="group-unit">价格单位</label><input id="group-unit" v-model="group.unit" class="text-input" required />
<small>文本聊天自动扣费仅支持 CNY / 1M tokens(元/百万 Token)。输入、输出均需填写;填 0 表示免费,留空则禁止调用。请填写平台对用户的售价。</small>
<div class="modal-form-grid"><label><span class="form-label">优先级</span><input v-model="group.priority" class="text-input" type="number" min="0" step="1" required /></label><label><span class="form-label">并发上限</span><input v-model="group.maxConcurrency" class="text-input" type="number" min="1" step="1" required /></label></div>
</template>
</form>
+1 -1
View File
@@ -20,7 +20,7 @@ type Column = keyof typeof columnLabels
const columns = ref<Column[]>(['key', 'model', 'group', 'endpoint', 'mode', 'tokens', 'cost', 'latency', 'time', 'status'])
const show = (name: Column) => columns.value.includes(name)
const number = (n: unknown) => n == null ? '—' : Number(n).toLocaleString('zh-CN')
const money = (n: unknown) => n == null ? '—' : `¥ ${Number(n).toFixed(6)}`
const money = (n: unknown) => n == null ? '—' : `¥ ${Number(n).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}`
const duration = (n: unknown) => n == null ? '—' : `${(Number(n) / 1000).toFixed(2)}s`
const time = (value: string) => new Date(value).toLocaleString('zh-CN', { hour12: false })
const statusName = (v: string) => ({ succeeded: '成功', failed: '失败', queued: '排队中', running: '执行中', cancelled: '已取消' })[v] || v