feat: stream chat and settle metered CNY usage atomically
This commit is contained in:
1 parent
7360cf3ebe
commit
b46ee72f2d
28 files changed
+1098
-86
No files matched your search
@@ -69,7 +69,11 @@ function selectPlaygroundModel(id: string) {
|
||||
}
|
||||
const prompt = ref('')
|
||||
const running = ref(false)
|
||||
const maxOutputTokens = ref(4096)
|
||||
const reasoningOutput = ref('')
|
||||
let streamAbort: AbortController | undefined
|
||||
const response = ref('')
|
||||
const responseError = ref('')
|
||||
const keys = ref<ApiKeyItem[]>([])
|
||||
const tasks = ref<TaskItem[]>([])
|
||||
const historyRevision = ref(0)
|
||||
@@ -139,6 +143,7 @@ onMounted(async () => {
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
streamAbort?.abort()
|
||||
taskPollers.forEach((timer) => window.clearTimeout(timer))
|
||||
taskPollers.clear()
|
||||
copyTimers.forEach((timer) => window.clearTimeout(timer))
|
||||
@@ -221,6 +226,7 @@ async function runModel() {
|
||||
if (!activeModel.value.id) return emit('notify', '当前没有可调用的模型,请先检查模型目录')
|
||||
if (!prompt.value.trim()) return emit('notify', '请先输入调用内容')
|
||||
if (running.value) return
|
||||
if (callMode.value !== '异步' && (!Number.isInteger(maxOutputTokens.value) || maxOutputTokens.value < 1 || maxOutputTokens.value > 32768)) return emit('notify', '最大输出 Token 须为 1–32768 的整数')
|
||||
if (!catalogConnected.value) return emit('notify', 'Control Plane 未连接,无法提交真实调用')
|
||||
if (!remoteApiKey.value) {
|
||||
emit('notify', '请先创建一个临时 API Key,再进行真实接口调用')
|
||||
@@ -230,6 +236,9 @@ async function runModel() {
|
||||
}
|
||||
running.value = true
|
||||
response.value = ''
|
||||
responseError.value = ''
|
||||
reasoningOutput.value = ''
|
||||
const selectedMode = callMode.value
|
||||
try {
|
||||
if (callMode.value === '异步') {
|
||||
const task = await catalogClient.createMediaTask(remoteApiKey.value, { model: activeModel.value.id, prompt: prompt.value, mediaType: activeModel.value.type === '视频' ? 'video' : '3d' })
|
||||
@@ -238,17 +247,22 @@ async function runModel() {
|
||||
activeTaskId.value = task.id
|
||||
response.value = `任务 ${task.id} 已进入队列,可前往任务中心查看进度。`
|
||||
scheduleTaskPoll(task.id, remoteApiKey.value)
|
||||
} else if (selectedMode === '流式') {
|
||||
streamAbort = new AbortController()
|
||||
await catalogClient.streamChat(remoteApiKey.value, { model: activeModel.value.id, prompt: prompt.value, maxTokens: maxOutputTokens.value }, (text, reasoning) => { response.value += text; reasoningOutput.value += reasoning }, streamAbort.signal)
|
||||
} else {
|
||||
const result = await catalogClient.chatCompletion(remoteApiKey.value, { model: activeModel.value.id, prompt: prompt.value })
|
||||
const result = await catalogClient.chatCompletion(remoteApiKey.value, { model: activeModel.value.id, prompt: prompt.value, maxTokens: maxOutputTokens.value })
|
||||
response.value = result.choices[0]?.message.content || '模型未返回内容'
|
||||
}
|
||||
emit('notify', callMode.value === '异步' ? '异步任务提交成功' : '模型调用完成')
|
||||
await refreshOverview()
|
||||
} catch (error) {
|
||||
emit('notify', error instanceof Error ? error.message : '模型调用失败')
|
||||
responseError.value = error instanceof Error && error.name === 'AbortError' ? '已停止接收,请在使用记录中确认最终状态和费用' : error instanceof Error ? error.message : '模型调用失败'
|
||||
emit('notify', responseError.value)
|
||||
} finally {
|
||||
running.value = false
|
||||
streamAbort = undefined
|
||||
historyRevision.value++
|
||||
await refreshOverview()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -482,7 +496,7 @@ async function createProject() {
|
||||
<section class="metric-grid">
|
||||
<article class="metric-card"><span class="icon-tile violet"><Activity :size="18" /></span><div><small>今日调用</small><strong>{{ overviewLoaded ? overview.callsToday.toLocaleString() : '--' }}</strong><em>来自网关调用记录</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile cyan"><Gauge :size="18" /></span><div><small>平均响应</small><strong>{{ overview.averageLatencyMs === undefined ? '--' : `${overview.averageLatencyMs}ms` }}</strong><em>{{ overview.averageLatencyMs === undefined ? '暂无延迟样本' : '按真实调用计算' }}</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile orange"><CircleDollarSign :size="18" /></span><div><small>可用额度</small><strong>{{ overview.availableCredit === undefined ? '--' : `¥ ${overview.availableCredit.toFixed(2)}` }}</strong><em>{{ overview.availableCredit === undefined ? '额度账本尚未接入' : '读取额度账本' }}</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile orange"><CircleDollarSign :size="18" /></span><div><small>可用额度</small><strong>{{ overview.availableCredit === undefined ? '--' : `¥ ${overview.availableCredit.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}` }}</strong><em>{{ overview.availableCredit === undefined ? '额度账本尚未接入' : '读取额度账本' }}</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile green"><CheckCircle2 :size="18" /></span><div><small>平台可用性</small><strong>{{ overview.availabilityPercent === undefined ? '--' : `${overview.availabilityPercent.toFixed(2)}%` }}</strong><em>{{ overview.availabilityPercent === undefined ? '监控数据尚未接入' : '来自可用性监控' }}</em></div></article>
|
||||
</section>
|
||||
|
||||
@@ -516,14 +530,18 @@ async function createProject() {
|
||||
<label>分组<select class="text-input" aria-label="调试分组" :value="activeModel.id" :disabled="running || !playgroundGroups.length" @change="selectPlaygroundGroup(($event.target as HTMLSelectElement).value)"><option v-for="group in playgroundGroups" :key="group.id" :value="group.id">{{ group.sourceLabel || '默认分组' }} · {{ group.provider }}</option></select></label>
|
||||
</div>
|
||||
<label class="form-label">调用模式</label>
|
||||
<div class="segment"><button v-for="mode in ['同步','流式','异步']" :key="mode" :disabled="running || mode !== activeModel.mode || mode === '流式'" :class="{ active: callMode === mode }" @click="callMode = mode as CallMode">{{ mode }}</button></div>
|
||||
<div class="segment"><button v-for="mode in ['同步','流式','异步']" :key="mode" :disabled="running || (activeModel.mode === '异步' ? mode !== '异步' : mode === '异步')" :class="{ active: callMode === mode }" @click="callMode = mode as CallMode">{{ mode }}</button></div>
|
||||
<label v-if="callMode !== '异步'" class="form-label token-limit">最大输出 Token<input v-model.number="maxOutputTokens" class="text-input" type="number" min="1" max="32768" step="1" :disabled="running" /></label>
|
||||
<small v-if="callMode !== '异步'">按分组人民币售价和实际 Token 用量扣费;调用前预留额度,结束后释放差额。</small>
|
||||
<label class="form-label prompt-label"><span>输入内容</span><small>{{ prompt.length }}/4000</small></label>
|
||||
<textarea v-model="prompt" maxlength="4000" :placeholder="callMode === '异步' ? activeModel.type === '视频' ? '例如:电影感产品展示视频,柔和运镜与自然光影' : '例如:一个低面数的木质机械鸟,游戏资产风格' : '描述你希望模型完成的任务'"></textarea>
|
||||
<div class="request-footer"><span><ShieldCheck :size="14" />{{ catalogConnected ? '通过 MOYI Key 调用真实网关' : '连接服务后才能调用' }}</span><button class="button primary" :disabled="running || !catalogConnected || !activeModel.id" @click="runModel"><Play v-if="!running" :size="16" /><span v-else class="spinner"></span>{{ running ? '提交中…' : callMode === '异步' ? activeModel.type === '视频' ? '生成视频' : '生成 3D' : '开始调用' }}</button></div>
|
||||
</article>
|
||||
<article class="panel response-panel">
|
||||
<header class="panel-header"><div><span>RESPONSE</span><h3>{{ callMode === '异步' ? '任务状态' : '模型输出' }}</h3></div><button class="icon-button" :class="{ copied: copySucceeded('response') }" :disabled="!response" :title="copySucceeded('response') ? '已复制' : '复制响应'" @click="copy(response, '响应内容', 'response')"><Check v-if="copySucceeded('response')" :size="16" /><Copy v-else :size="16" /></button></header>
|
||||
<div v-if="running" class="response-state"><span class="loading-orbit"><i></i></span><strong>{{ callMode === '异步' ? activeModel.type === '视频' ? '正在创建视频任务' : '正在创建 3D 任务' : '模型正在生成响应' }}</strong><p>请求通过模亦灵枢统一网关发送</p></div>
|
||||
<button v-if="running && callMode === '流式'" class="button secondary" @click="streamAbort?.abort()">停止接收</button>
|
||||
<details v-if="reasoningOutput"><summary>思考过程</summary><pre class="response-content">{{ reasoningOutput }}</pre></details>
|
||||
<div v-if="running && !response" class="response-state"><span class="loading-orbit"><i></i></span><strong>{{ callMode === '异步' ? activeModel.type === '视频' ? '正在创建视频任务' : '正在创建 3D 任务' : '模型正在生成响应' }}</strong><p>请求通过模亦灵枢统一网关发送</p></div>
|
||||
<div v-else-if="callMode === '异步' && activeTask" class="live-task-result">
|
||||
<div class="task-preview" :class="{ empty: !activeTask.previewUrl && !activeTask.videoUrl }"><video v-if="activeTask.videoUrl" :src="activeTask.videoUrl" controls></video><img v-else-if="activeTask.previewUrl" :src="activeTask.previewUrl" alt="异步任务预览" /><span v-else><Box :size="34" /><small>{{ activeTask.status === '已完成' ? '结果已生成' : '生成预览中' }}</small></span><em>{{ activeTask.provider === 'tripo' ? 'TRIPO' : activeTask.provider === 'seedance' ? 'SEEDANCE' : 'MOYI GATEWAY' }}</em></div>
|
||||
<div class="live-task-head"><span class="status-chip" :class="activeTask.status"><i></i>{{ activeTask.status }}</span><code>{{ activeTask.id }}</code></div>
|
||||
@@ -532,7 +550,7 @@ async function createProject() {
|
||||
<dl v-if="activeTask.creditsConsumed !== undefined" class="task-cost"><dt>上游任务消耗</dt><dd>{{ activeTask.creditsConsumed }} credits</dd></dl>
|
||||
<div class="live-task-actions"><button class="button secondary" @click="emit('navigate', 'tasks')"><FileClock :size="16" />任务中心</button><button v-if="activeTask.modelUrl" class="button primary" @click="openTaskAsset(activeTask.modelUrl)"><Download :size="16" />下载 GLB</button><button v-if="activeTask.videoUrl" class="button primary" @click="openTaskAsset(activeTask.videoUrl)"><ExternalLink :size="16" />查看视频</button><button v-if="activeTask.previewUrl" class="icon-button" title="打开预览" @click="openTaskAsset(activeTask.previewUrl)"><ExternalLink :size="16" /></button></div>
|
||||
</div>
|
||||
<div v-else-if="response" class="response-result"><div><span class="status-chip success"><i></i>200 OK</span><small>MOYI unified response</small></div><p>{{ response }}</p></div>
|
||||
<div v-else-if="response || responseError" class="response-result"><div><span class="status-chip" :class="responseError ? 'failed' : running ? 'muted' : 'success'"><i></i>{{ responseError ? '调用未正常完成' : running ? '正在输出' : '调用完成' }}</span><small>MOYI unified response</small></div><p>{{ response }}</p><p v-if="responseError" role="alert">{{ responseError }}</p></div>
|
||||
<div v-else class="response-state empty"><span class="response-icon"><Sparkles :size="28" /></span><strong>等待模型响应</strong><p>配置请求并点击“开始调用”</p></div>
|
||||
</article>
|
||||
</section>
|
||||
@@ -552,9 +570,11 @@ async function createProject() {
|
||||
|
||||
<template v-else-if="view === 'usage'">
|
||||
<section class="page-heading"><div><span>USAGE & CREDITS</span><h1>用量与额度</h1><p>查看调用消耗、额度余额和最近流水。</p></div><button class="button disabled" disabled><WalletCards :size="16" />充值暂未开放</button></section>
|
||||
<section class="credit-hero"><div><span>当前可用额度</span><strong>{{ overview.availableCredit === undefined ? '--' : `¥ ${overview.availableCredit.toFixed(2)}` }}</strong><p>{{ overview.availableCredit === undefined ? '额度账本尚未接入,当前不会展示预估金额。' : '额度由平台账本实时计算。' }}</p></div><div class="credit-circle"><span>{{ overview.availableCredit === undefined ? '--' : '已同步' }}</span><small>{{ overview.availableCredit === undefined ? '等待账本' : '账本状态' }}</small></div></section>
|
||||
<section class="metric-grid three"><article class="metric-card"><span class="icon-tile violet"><Activity :size="18" /></span><div><small>本月调用</small><strong>{{ overviewLoaded ? overview.callsThisMonth.toLocaleString() : '--' }}</strong><em>网关真实记录</em></div></article><article class="metric-card"><span class="icon-tile cyan"><BarChart3 :size="18" /></span><div><small>本月消耗</small><strong>--</strong><em>计费规则尚未接入</em></div></article><article class="metric-card"><span class="icon-tile orange"><Box :size="18" /></span><div><small>异步任务</small><strong>{{ overviewLoaded ? overview.asyncTasks : '--' }}</strong><em>{{ overview.asyncSuccessRate === undefined ? '暂无完成样本' : `成功率 ${overview.asyncSuccessRate.toFixed(1)}%` }}</em></div></article></section>
|
||||
<section class="two-column wide-left"><article class="panel chart-panel"><header class="panel-header"><div><span>USAGE TREND</span><h3>过去 7 天调用</h3></div><span class="select-button static">按调用次数</span></header><div v-if="usageSeries.length" class="bar-chart large"><div v-for="item in usageSeries" :key="item.date"><i :style="{ height: `${item.height}%` }" :title="`${item.date} · ${item.count} 次`"></i><span>{{ item.label }}</span></div></div><div v-else class="empty-state compact"><BarChart3 :size="22" /><strong>暂无调用趋势</strong><span>完成真实调用后会在这里形成统计</span></div></article><article class="panel ledger"><header class="panel-header"><div><span>CREDIT LEDGER</span><h3>最近额度流水</h3></div><Download :size="17" /></header><div v-if="overview.ledger.length"><p v-for="item in overview.ledger" :key="item.id"><span class="activity-dot violet"><CircleDollarSign :size="14" /></span><span><strong>{{ item.title }}</strong><small>{{ item.createdAt }}</small></span><b :class="item.amount >= 0 ? 'positive' : ''">{{ item.amount >= 0 ? '+' : '' }} ¥{{ item.amount.toFixed(2) }}</b></p></div><div v-else class="empty-state compact"><CircleDollarSign :size="22" /><strong>暂无额度流水</strong><span>管理员发放或计费完成后会显示记录</span></div></article></section>
|
||||
<section class="credit-hero"><div><span>当前可用额度</span><strong>{{ overview.availableCredit === undefined ? '--' : `¥ ${overview.availableCredit.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}` }}</strong><p>{{ overview.availableCredit === undefined ? '额度账本尚未接入,当前不会展示预估金额。' : '额度由平台账本实时计算。' }}</p></div><div class="credit-circle"><span>{{ overview.availableCredit === undefined ? '--' : '已同步' }}</span><small>{{ overview.availableCredit === undefined ? '等待账本' : '账本状态' }}</small></div></section>
|
||||
<p v-if="overview.reservedCredit" class="billing-note">进行中的调用已预留 ¥{{ overview.reservedCredit.toLocaleString('zh-CN', { maximumFractionDigits: 8 }) }};结束后按实际 Token 用量结算,剩余预留自动释放。</p>
|
||||
<p v-if="(overview.balance ?? 0) < 0" class="billing-note" role="alert">账户存在待补余额,请补足后继续调用。可在下方流水查看实际扣费。</p>
|
||||
<section class="metric-grid three"><article class="metric-card"><span class="icon-tile violet"><Activity :size="18" /></span><div><small>本月调用</small><strong>{{ overviewLoaded ? overview.callsThisMonth.toLocaleString() : '--' }}</strong><em>网关真实记录</em></div></article><article class="metric-card"><span class="icon-tile cyan"><BarChart3 :size="18" /></span><div><small>本月消耗</small><strong>{{ overview.costThisMonth === undefined ? '--' : `¥ ${overview.costThisMonth.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}` }}</strong><em>已入账的模型调用费用</em></div></article><article class="metric-card"><span class="icon-tile orange"><Box :size="18" /></span><div><small>异步任务</small><strong>{{ overviewLoaded ? overview.asyncTasks : '--' }}</strong><em>{{ overview.asyncSuccessRate === undefined ? '暂无完成样本' : `成功率 ${overview.asyncSuccessRate.toFixed(1)}%` }}</em></div></article></section>
|
||||
<section class="two-column wide-left"><article class="panel chart-panel"><header class="panel-header"><div><span>USAGE TREND</span><h3>过去 7 天调用</h3></div><span class="select-button static">按调用次数</span></header><div v-if="usageSeries.length" class="bar-chart large"><div v-for="item in usageSeries" :key="item.date"><i :style="{ height: `${item.height}%` }" :title="`${item.date} · ${item.count} 次`"></i><span>{{ item.label }}</span></div></div><div v-else class="empty-state compact"><BarChart3 :size="22" /><strong>暂无调用趋势</strong><span>完成真实调用后会在这里形成统计</span></div></article><article class="panel ledger"><header class="panel-header"><div><span>CREDIT LEDGER</span><h3>最近额度流水</h3></div><Download :size="17" /></header><div v-if="overview.ledger.length"><p v-for="item in overview.ledger" :key="item.id"><span class="activity-dot violet"><CircleDollarSign :size="14" /></span><span><strong>{{ item.title }}</strong><small>{{ item.createdAt }}</small></span><b :class="item.amount >= 0 ? 'positive' : ''">{{ item.amount >= 0 ? '+' : '' }} ¥{{ item.amount.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 }) }}</b></p></div><div v-else class="empty-state compact"><CircleDollarSign :size="22" /><strong>暂无额度流水</strong><span>管理员发放或计费完成后会显示记录</span></div></article></section>
|
||||
</template>
|
||||
|
||||
<template v-else>
|
||||
@@ -584,6 +604,10 @@ MOYI_MODEL=<model-id></code></pre></div></section><section><b>02</b><div><
|
||||
|
||||
<style scoped>
|
||||
.request-panel, .response-panel { min-width: 0; }
|
||||
.token-limit { display: grid; gap: 8px; }
|
||||
.response-result > p, .response-content { white-space: pre-wrap; overflow-wrap: anywhere; max-width: 100%; }
|
||||
.response-result .status-chip.failed { color: #b42318; background: #fee4e2; }
|
||||
.response-result p[role="alert"] { color: #b42318; }
|
||||
.playground-selectors { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
|
||||
.playground-selectors label { display: grid; gap: 8px; font-size: 13px; min-width: 0; }
|
||||
.playground-selectors select { width: 100%; min-width: 0; }
|
||||
|
||||
Reference in new issue
Block a user