fix: show published models and support multi-model key selection

This commit is contained in:
wlx committed 2026-09-22 03:09:07 +08:00
1 parent 1a57de5bf9
commit 0a44a0802b
14 files changed
+360 -60

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
<script setup lang="ts">
import { ref } from 'vue'
import UserConsole from '../src/views/UserConsole.vue'
import CatalogAdmin from '../src/components/CatalogAdmin.vue'
import '../src/styles.css'
// Deterministic UI fixture. All requests stay in memory; no credentials or upstream calls.
const view = ref('models')
const admin = ref(false)
const notice = ref('')
const providers = [{ id: 'relay-a', name: '本地渠道 A', code: 'fixture-a', protocol: 'openai-compatible', sourceType: 'relay', status: 'active' }]
const definitions = Array.from({ length: 315 }, (_, i) => ({ id: `model-${i + 1}`, publicName: `测试模型 ${String(i + 1).padStart(3, '0')}`, modelKey: `model-${i + 1}`, type: 'text', mode: 'sync', description: '仅用于模型目录与跨页授权的本地测试。', inputTypes: ['text'], outputTypes: ['text'], status: 'active' }))
const routes = definitions.slice(0, 10).flatMap((model, i) => Array.from({ length: i === 0 ? 2 : 1 }, (_, n) => ({ ...model, id: `route-${i + 1}-${n + 1}`, logicalModelId: model.id, providerId: 'relay-a', upstreamModel: model.modelKey, sourceLabel: `来源 ${n + 1}`, capabilities: {}, pricing: { input: 1, output: 2, unit: 'CNY / 1M tokens' } })))
const keys = Array.from({ length: 14 }, (_, i) => ({ id: `key-${i + 1}`, name: `本地 Key ${i + 1}`, scopeType: 'personal', scopes: ['invoke'], keyPrefix: 'test-only', modelIds: [] as string[], status: 'active', createdAt: '2026-09-22T00:00:00Z' }))
window.fetch = async (input, init) => {
const url = new URL(String(input), window.location.origin)
const path = url.pathname
let data: unknown
if (path === '/api/models') data = { logicalModels: definitions, models: routes, providers }
else if (path === '/api/admin/models') data = { logicalModels: definitions, models: routes }
else if (path === '/api/admin/providers') data = { providers }
else if (path === '/api/keys') data = { keys }
else if (/^\/api\/keys\/key-\d+\/models$/.test(path) && init?.method === 'POST') {
const key = keys.find(key => path === `/api/keys/${key.id}/models`)!
key.modelIds = JSON.parse(String(init.body)).modelIds
data = { apiKey: key }
} else if (path === '/api/projects') data = { projects: [] }
else if (path === '/api/tasks') data = { tasks: [] }
else if (path === '/api/overview') data = { generatedAt: '', callsToday: 0, callsThisMonth: 0, asyncTasks: 0, tasks: { queued: 0, running: 0, succeeded: 0, failed: 0 }, series: [], ledger: [] }
else throw new Error(`Unexpected fixture request: ${path}`)
return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } })
}
</script>
<template>
<main style="max-width: 1400px; margin: auto; padding: 24px">
<nav style="display: flex; gap: 16px; margin-bottom: 24px"><strong>本地隔离测试</strong><button class="button secondary" @click="admin = false; view = 'models'">用户模型广场</button><button class="button secondary" @click="admin = false; view = 'keys'">用户 Keys</button><button class="button secondary" @click="admin = true">管理端目录</button></nav>
<p role="status">{{ notice }}</p>
<CatalogAdmin v-if="admin" section="models" @notify="notice = $event" />
<UserConsole v-else :view="view" @navigate="view = $event" @notify="notice = $event" />
</main>
</template>
+6
View File
@@ -0,0 +1,6 @@
<!doctype html>
<html lang="zh-CN"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>本地模型目录回归测试</title></head><body><div id="app"></div><script type="module">
import { createApp } from 'vue'
import Preview from './CatalogPreview.vue'
createApp(Preview).mount('#app')
</script></body></html>
+48
View File
@@ -0,0 +1,48 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { nextTick, ref } from 'vue'
import { buildModelCatalog, setGroupSelection } from '../src/services/model-catalog.ts'
import { usePagination } from '../src/composables/usePagination.ts'
test('published models without enabled routes remain visible and duplicate routes are grouped', () => {
const definitions = Array.from({ length: 315 }, (_, i) => ({ id: `m${i}`, status: 'active' }))
const routes = [{ id: 'a', logicalModelId: 'm0', status: 'active' }, { id: 'b', logicalModelId: 'm0', status: 'active' }, { id: 'c', logicalModelId: 'm1', status: 'disabled' }]
const catalog = buildModelCatalog([...definitions, { id: 'hidden', status: 'disabled' }], routes)
assert.equal(catalog.length, 315)
assert.deepEqual(catalog[0].groups.map(group => group.id), ['a', 'b'])
assert.equal(catalog[1].groups.length, 0)
assert.equal(catalog.at(-1).definition.id, 'm314')
})
test('selecting and removing another model preserves existing exact group grants', () => {
let grants = ['model-a/relay-1', 'legacy-unavailable']
grants = setGroupSelection(grants, ['model-b/relay-2'], true)
grants = setGroupSelection(grants, ['model-a/relay-1', 'model-a/relay-2'], true)
assert.deepEqual(grants, ['model-a/relay-1', 'legacy-unavailable', 'model-b/relay-2', 'model-a/relay-2'])
grants = setGroupSelection(grants, ['model-a/relay-1', 'model-a/relay-2'], false)
assert.deepEqual(grants, ['legacy-unavailable', 'model-b/relay-2'])
})
test('pagination covers a large catalog once and clamps after removal or filtering', async () => {
const items = ref(Array.from({ length: 315 }, (_, i) => i))
const { page, pageSize, pages, pagedItems } = usePagination(items)
const all = []
for (let i = 1; i <= pages.value; i++) { page.value = i; all.push(...pagedItems.value) }
assert.deepEqual(all, items.value)
assert.equal(page.value, 27)
items.value = [10, 20]
await nextTick()
assert.equal(page.value, 1)
assert.deepEqual(pagedItems.value, [10, 20])
items.value = Array.from({ length: 48 }, (_, i) => i)
await nextTick()
page.value = 3
pageSize.value = 24
await nextTick()
assert.equal(page.value, 1)
assert.equal(pagedItems.value.length, 24)
items.value = []
await nextTick()
assert.equal(pages.value, 1)
assert.deepEqual(pagedItems.value, [])
})