fix: show published models and support multi-model key selection
This commit is contained in:
1 parent
1a57de5bf9
commit
0a44a0802b
14 files changed
+360
-60
No files matched your search
@@ -3,6 +3,8 @@ import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { Plus, Pencil, RefreshCcw, Search, Trash2 } from 'lucide-vue-next'
|
||||
import { catalogClient, type CatalogProvider, type CatalogModel, type LogicalModel } from '../services/catalog'
|
||||
import UiModal from './UiModal.vue'
|
||||
import ListPagination from './ListPagination.vue'
|
||||
import { usePagination } from '../composables/usePagination'
|
||||
|
||||
const props = defineProps<{ section: 'models' | 'channels' }>()
|
||||
const emit = defineEmits<{ notify: [message: string] }>()
|
||||
@@ -30,6 +32,9 @@ const model = reactive({ publicName: '', modelKey: '', type: 'text' as LogicalMo
|
||||
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 { page: modelPage, pageSize: modelPageSize, pagedItems: pagedModels } = usePagination(shownModels)
|
||||
const { page: channelPage, pageSize: channelPageSize, pagedItems: pagedChannels } = usePagination(shownChannels)
|
||||
watch(query, () => { modelPage.value = 1; channelPage.value = 1 })
|
||||
const channelName = (id: string) => channels.value.find(item => item.id === id)?.name || id
|
||||
const modelGroups = (id: string) => groups.value.filter(item => item.logicalModelId === id)
|
||||
|
||||
@@ -103,15 +108,18 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
|
||||
<section class="page-heading"><h1>{{ props.section === 'channels' ? '渠道管理' : '模型与分组' }}</h1><div class="heading-actions"><button class="icon-button" title="刷新" :disabled="loading" @click="refresh"><RefreshCcw :size="18" /></button><button class="button primary" @click="props.section === 'channels' ? editChannel() : editModel()"><Plus :size="16" />{{ props.section === 'channels' ? '添加渠道' : '添加模型' }}</button></div></section>
|
||||
<label class="search-input catalog-search"><Search :size="16" /><input v-model="query" :placeholder="props.section === 'channels' ? '搜索渠道名称或 Code' : '搜索模型名称或标识'" /></label>
|
||||
<p v-if="error" role="alert">{{ error }}</p>
|
||||
<p v-if="props.section === 'models'" class="catalog-secondary">启用模型后会显示在用户端模型广场。分组与所属渠道也启用后,用户才能为 API Key 选择该分组;调用前还需完成凭据和计费配置。</p>
|
||||
<p v-if="loading">加载中…</p>
|
||||
<div v-if="props.section === 'channels'" class="responsive-table"><table><thead><tr><th>渠道</th><th>来源 / 协议</th><th>Base URL</th><th>凭据</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="item in shownChannels" :key="item.id"><td><strong>{{ item.name }}</strong><small class="catalog-secondary">{{ item.code }}</small></td><td>{{ item.sourceType }}<small class="catalog-secondary">{{ item.protocol }}</small></td><td class="catalog-url">{{ item.baseUrl || '未配置' }}</td><td>{{ item.credentialMasked || '未配置' }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.name} 启用`" @change="toggle('channel', item)" />{{ item.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑渠道 ${item.name}`" @click="editChannel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除渠道 ${item.name}`" :disabled="saving" @click="confirmDelete('channel', item.id, item.name)"><Trash2 :size="16" /></button></td></tr><tr v-if="!shownChannels.length && !loading"><td colspan="6">暂无渠道</td></tr></tbody></table></div>
|
||||
<template v-else>
|
||||
<section v-for="item in shownModels" :key="item.id" class="model-definition-section">
|
||||
<div v-if="props.section === 'channels'" class="responsive-table"><table><thead><tr><th>渠道</th><th>来源 / 协议</th><th>Base URL</th><th>凭据</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="item in pagedChannels" :key="item.id"><td><strong>{{ item.name }}</strong><small class="catalog-secondary">{{ item.code }}</small></td><td>{{ item.sourceType }}<small class="catalog-secondary">{{ item.protocol }}</small></td><td class="catalog-url">{{ item.baseUrl || '未配置' }}</td><td>{{ item.credentialMasked || '未配置' }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.name} 启用`" @change="toggle('channel', item)" />{{ item.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑渠道 ${item.name}`" @click="editChannel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除渠道 ${item.name}`" :disabled="saving" @click="confirmDelete('channel', item.id, item.name)"><Trash2 :size="16" /></button></td></tr><tr v-if="!shownChannels.length && !loading"><td colspan="6">暂无渠道</td></tr></tbody></table></div>
|
||||
<ListPagination v-if="props.section === 'channels'" v-model:page="channelPage" v-model:page-size="channelPageSize" :total="shownChannels.length" label="渠道分页" />
|
||||
<template v-if="props.section === 'models'">
|
||||
<section v-for="item in pagedModels" :key="item.id" class="model-definition-section">
|
||||
<header class="panel-header"><div><h3>{{ item.publicName }}</h3><small>{{ item.modelKey || item.id }} · {{ item.type }} · {{ item.mode }}</small></div><div class="heading-actions"><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.publicName} 启用`" @change="toggle('model', item)" />启用</label><button class="icon-button" :title="`编辑模型 ${item.publicName}`" @click="editModel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除模型 ${item.publicName}`" :disabled="saving" @click="confirmDelete('model', item.id, item.publicName)"><Trash2 :size="16" /></button><button class="button secondary small" @click="editGroup(item)"><Plus :size="15" />添加分组</button></div></header>
|
||||
<p v-if="item.description" class="catalog-secondary">{{ item.description }}</p>
|
||||
<div class="responsive-table"><table><thead><tr><th>分组名称</th><th>渠道</th><th>上游模型</th><th>输入 / 输出价格</th><th>并发</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="route in modelGroups(item.id)" :key="route.id"><td>{{ route.sourceLabel }}</td><td>{{ channelName(route.providerId) }}</td><td><code>{{ route.upstreamModel }}</code></td><td>{{ route.pricing?.input ?? '—' }} / {{ route.pricing?.output ?? '—' }}<small class="catalog-secondary">{{ route.pricing?.unit }}</small></td><td>{{ route.maxConcurrency ?? '默认' }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="route.status === 'active'" :disabled="saving" :aria-label="`${route.sourceLabel} 启用`" @change="toggle('group', route)" />{{ route.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑分组 ${route.sourceLabel}`" @click="editGroup(item, route)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除分组 ${route.sourceLabel}`" :disabled="saving" @click="confirmDelete('group', route.id, route.sourceLabel || '默认分组')"><Trash2 :size="16" /></button></td></tr><tr v-if="!modelGroups(item.id).length"><td colspan="7">暂无分组</td></tr></tbody></table></div>
|
||||
</section>
|
||||
<p v-if="!shownModels.length && !loading">暂无模型</p>
|
||||
<ListPagination v-model:page="modelPage" v-model:page-size="modelPageSize" :total="shownModels.length" label="管理端模型分页" />
|
||||
</template>
|
||||
<UiModal v-if="deleting" title="确认删除" @close="!saving && (deleting = undefined)"><p>确认删除「{{ deleting.name }}」?</p><p>{{ deleting.kind === 'model' ? '该模型及全部分组将从目录移除,相关 API Key 将不能再调用它们。' : deleting.kind === 'channel' ? '请先将关联分组删除或改为其他渠道,再删除渠道。' : '分组将从目录移除,相关 API Key 将不能再调用它。' }}</p><p>调用历史和已有任务会保留。删除后无法从页面恢复。</p><p v-if="deleteError" role="alert" class="delete-error">{{ deleteError }}</p><template #footer><button class="button secondary" :disabled="saving" @click="deleting = undefined">取消</button><button class="button danger" :disabled="saving" @click="remove">{{ saving ? '删除中…' : '确认删除' }}</button></template></UiModal>
|
||||
<UiModal v-if="editor" :title="`${editingId ? '编辑' : '添加'}${editor === 'channel' ? '渠道' : editor === 'model' ? '模型' : '分组'}`" @close="!saving && (editor = '')">
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
const props = withDefaults(defineProps<{ page: number; pageSize: number; total: number; sizes?: number[]; label?: string; disabled?: boolean }>(), { sizes: () => [12, 24, 48], label: '列表分页' })
|
||||
const emit = defineEmits<{ 'update:page': [page: number]; 'update:pageSize': [size: number] }>()
|
||||
const pages = computed(() => Math.max(1, Math.ceil(props.total / props.pageSize)))
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<nav class="list-pagination" :aria-label="label">
|
||||
<span>共 {{ total }} 项<span v-if="total"> · 第 {{ (page - 1) * pageSize + 1 }}–{{ Math.min(page * pageSize, total) }} 项</span></span>
|
||||
<label>每页 <select :value="pageSize" :disabled="disabled" :aria-label="`${label}每页条数`" @change="emit('update:pageSize', Number(($event.target as HTMLSelectElement).value))"><option v-for="size in sizes" :key="size" :value="size">{{ size }}</option></select> 项</label>
|
||||
<div class="page-actions">
|
||||
<button type="button" :disabled="disabled || page <= 1" @click="emit('update:page', 1)">首页</button>
|
||||
<button type="button" :disabled="disabled || page <= 1" @click="emit('update:page', page - 1)">上一页</button>
|
||||
<span aria-live="polite">{{ page }} / {{ pages }}</span>
|
||||
<button type="button" :disabled="disabled || page >= pages" @click="emit('update:page', page + 1)">下一页</button>
|
||||
<button type="button" :disabled="disabled || page >= pages" @click="emit('update:page', pages)">末页</button>
|
||||
</div>
|
||||
</nav>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.list-pagination { display: flex; align-items: center; flex-wrap: wrap; justify-content: space-between; gap: 12px; padding: 16px 0; color: #687378; font-size: 12px; }
|
||||
.page-actions { display: flex; align-items: center; gap: 8px; }
|
||||
button, select { background: #fff; color: #36454f; border: 1px solid #dce3e5; border-radius: 6px; padding: 6px 8px; font: inherit; }
|
||||
button { cursor: pointer; }
|
||||
button:disabled { opacity: .4; cursor: default; }
|
||||
button:focus-visible, select:focus-visible { outline: 2px solid #7467dc; outline-offset: 2px; }
|
||||
</style>
|
||||
@@ -1,48 +1,85 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import type { ModelItem } from '../types'
|
||||
import { setGroupSelection } from '../services/model-catalog'
|
||||
import { usePagination } from '../composables/usePagination'
|
||||
import ListPagination from './ListPagination.vue'
|
||||
|
||||
const props = defineProps<{ groups: ModelItem[]; modelValue: string[]; disabled?: boolean }>()
|
||||
const emit = defineEmits<{ 'update:modelValue': [ids: string[]] }>()
|
||||
const selectedModel = ref('')
|
||||
const models = computed(() => [...new Map(props.groups.map(group => [group.logicalModelId || group.id, group])).values()])
|
||||
const visibleGroups = computed(() => props.groups.filter(group => (group.logicalModelId || group.id) === selectedModel.value))
|
||||
const query = ref('')
|
||||
const onlySelected = ref(false)
|
||||
const expanded = ref<string[]>([])
|
||||
const models = computed(() => [...new Map(props.groups.map(group => [group.logicalModelId || group.id, group])).entries()].map(([id, model]) => ({
|
||||
id, name: model.name, modelKey: model.modelKey,
|
||||
groups: props.groups.filter(group => (group.logicalModelId || group.id) === id),
|
||||
})))
|
||||
const selectedGroups = computed(() => props.groups.filter(group => props.modelValue.includes(group.id)))
|
||||
const selectedCount = computed(() => models.value.filter(model => model.groups.some(group => props.modelValue.includes(group.id))).length)
|
||||
const unavailable = computed(() => props.modelValue.filter(id => !props.groups.some(group => group.id === id)))
|
||||
function toggle(id: string, checked: boolean) {
|
||||
emit('update:modelValue', checked ? [...new Set([...props.modelValue, id])] : props.modelValue.filter(value => value !== id))
|
||||
const filtered = computed(() => models.value.filter(model => {
|
||||
const text = `${model.name} ${model.modelKey || ''} ${model.groups.map(group => `${group.provider} ${group.sourceLabel}`).join(' ')}`.toLowerCase()
|
||||
return text.includes(query.value.trim().toLowerCase()) && (!onlySelected.value || model.groups.some(group => props.modelValue.includes(group.id)))
|
||||
}))
|
||||
const { page, pageSize, pagedItems } = usePagination(filtered, 8)
|
||||
watch([query, onlySelected], () => { page.value = 1 })
|
||||
function toggle(ids: string[], checked: boolean) { emit('update:modelValue', setGroupSelection(props.modelValue, ids, checked)) }
|
||||
function modelChecked(id: string) { return expanded.value.includes(id) || models.value.find(model => model.id === id)?.groups.some(group => props.modelValue.includes(group.id)) }
|
||||
function selectModel(id: string, checked: boolean) {
|
||||
expanded.value = setGroupSelection(expanded.value, [id], checked)
|
||||
const groups = models.value.find(model => model.id === id)?.groups || []
|
||||
if (!checked) toggle(groups.map(group => group.id), false)
|
||||
else if (groups.length === 1) toggle([groups[0]!.id], true)
|
||||
}
|
||||
function count(id: string) { return props.groups.filter(group => (group.logicalModelId || group.id) === id && props.modelValue.includes(group.id)).length }
|
||||
function clear() { expanded.value = []; emit('update:modelValue', []) }
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="group-permissions">
|
||||
<label for="permission-model">模型</label>
|
||||
<select id="permission-model" v-model="selectedModel" class="text-input" :disabled="disabled">
|
||||
<option value="" disabled>请选择模型</option>
|
||||
<option v-for="model in models" :key="model.logicalModelId || model.id" :value="model.logicalModelId || model.id">{{ model.name }} (已选 {{ count(model.logicalModelId || model.id) }} 组)</option>
|
||||
</select>
|
||||
<fieldset :disabled="disabled"><legend>可调用分组</legend>
|
||||
<label v-for="group in visibleGroups" :key="group.id" class="group-option">
|
||||
<input type="checkbox" :checked="modelValue.includes(group.id)" @change="toggle(group.id, ($event.target as HTMLInputElement).checked)" />
|
||||
<span><strong>{{ group.sourceLabel || '默认分组' }}</strong><small>{{ group.provider }} · {{ group.mode }}</small><code>{{ group.id }}</code></span>
|
||||
</label>
|
||||
<p v-if="!selectedModel">尚未选择模型</p>
|
||||
<p v-else-if="!visibleGroups.length">该模型暂无可用分组</p>
|
||||
</fieldset>
|
||||
<p>可同时选择多个模型,再勾选各模型可调用的分组。搜索或翻页会保留已选权限。</p>
|
||||
<section v-if="modelValue.length" class="selected-summary" aria-label="已选模型与分组">
|
||||
<strong>已选 {{ selectedCount }} 个模型 · {{ selectedGroups.length }} 个可用分组<span v-if="unavailable.length"> · {{ unavailable.length }} 个暂不可用授权</span></strong>
|
||||
<div class="selected-tags"><button v-for="group in selectedGroups" :key="group.id" type="button" :disabled="disabled" :aria-label="`移除 ${group.name} / ${group.sourceLabel || '默认分组'}`" @click="toggle([group.id], false)">{{ group.name }} / {{ group.sourceLabel || '默认分组' }} ×</button></div>
|
||||
</section>
|
||||
<label class="permission-search">搜索模型或分组<input v-model="query" class="text-input" placeholder="模型名称、标识、渠道或分组" :disabled="disabled" /></label>
|
||||
<label class="selected-filter"><input v-model="onlySelected" type="checkbox" :disabled="disabled" />仅看已授权模型</label>
|
||||
<div class="permission-list">
|
||||
<section v-for="model in pagedItems" :key="model.id" class="permission-model">
|
||||
<label class="model-choice"><input type="checkbox" :checked="modelChecked(model.id)" :disabled="disabled" @change="selectModel(model.id, ($event.target as HTMLInputElement).checked)" /><span><strong>{{ model.name }}</strong><small>{{ model.groups.filter(group => modelValue.includes(group.id)).length }} / {{ model.groups.length }} 个分组已选</small></span></label>
|
||||
<fieldset v-if="modelChecked(model.id)" :disabled="disabled"><legend>{{ model.name }} 的可调用分组</legend>
|
||||
<p v-if="!model.groups.some(group => modelValue.includes(group.id))">请选择至少一个分组以授权该模型。</p>
|
||||
<label v-for="group in model.groups" :key="group.id" class="group-option">
|
||||
<input type="checkbox" :checked="modelValue.includes(group.id)" @change="toggle([group.id], ($event.target as HTMLInputElement).checked)" />
|
||||
<span><strong>{{ group.sourceLabel || '默认分组' }}</strong><small>{{ group.provider }} · {{ group.mode }}</small></span>
|
||||
</label>
|
||||
</fieldset>
|
||||
</section>
|
||||
<p v-if="!filtered.length">{{ groups.length ? '没有匹配模型,请调整搜索或筛选条件。' : '暂无可授权分组,请等待管理员上线模型分组。' }}</p>
|
||||
</div>
|
||||
<ListPagination v-model:page="page" v-model:page-size="pageSize" :total="filtered.length" :sizes="[8, 16, 32]" label="权限模型分页" :disabled="disabled" />
|
||||
<fieldset v-if="unavailable.length" :disabled="disabled"><legend>暂不可用的既有授权</legend>
|
||||
<label v-for="id in unavailable" :key="id" class="group-option"><input type="checkbox" checked @change="toggle(id, false)" /><code>{{ id }}</code></label>
|
||||
<label v-for="id in unavailable" :key="id" class="group-option"><input type="checkbox" checked @change="toggle([id], false)" /><code>{{ id }}</code></label>
|
||||
</fieldset>
|
||||
<footer><span>已选 {{ modelValue.length }} 个分组</span><button class="text-link" :disabled="disabled || !modelValue.length" @click="emit('update:modelValue', [])">清空授权</button></footer>
|
||||
<footer><span>共 {{ modelValue.length }} 个分组授权</span><button type="button" class="text-link" :disabled="disabled || !modelValue.length" @click="clear">清空授权</button></footer>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.group-permissions { display: grid; gap: 12px; min-width: 0; }
|
||||
fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 8px; }
|
||||
legend { margin-bottom: 8px; font-weight: 600; font-size: 13px; }
|
||||
.group-option { display: flex; align-items: center; gap: 12px; border-bottom: 1px solid #e5e7eb; padding: 12px 0; cursor: pointer; }
|
||||
.group-option span { display: grid; gap: 5px; min-width: 0; }
|
||||
.group-option small, p { color: #6b7280; font-size: 12px; }
|
||||
.group-permissions, .permission-search { display: grid; gap: 12px; min-width: 0; }
|
||||
.permission-search, .selected-filter { font-size: 13px; }
|
||||
.permission-list { display: grid; gap: 8px; align-items: start; }
|
||||
.permission-model { border: 1px solid #e5e7eb; border-radius: 8px; padding: 12px; }
|
||||
.model-choice, .group-option { display: flex; align-items: center; gap: 12px; cursor: pointer; }
|
||||
.model-choice span, .group-option span { display: grid; gap: 5px; min-width: 0; overflow-wrap: anywhere; }
|
||||
.model-choice strong { font-size: 14px; }
|
||||
fieldset { border: 0; padding: 12px 0 0 26px; margin: 0; display: grid; gap: 8px; min-width: 0; }
|
||||
legend { padding-top: 12px; font-size: 12px; color: #687378; }
|
||||
.group-option { padding: 8px 0; font-size: 13px; }
|
||||
small, p { color: #687378; font-size: 12px; line-height: 1.6; margin: 0; }
|
||||
code { font-size: 11px; overflow-wrap: anywhere; }
|
||||
.selected-summary { padding: 12px; background: #f5f4ff; border-radius: 8px; font-size: 12px; }
|
||||
.selected-tags { display: flex; flex-wrap: wrap; gap: 6px; max-height: 140px; overflow: auto; margin-top: 8px; }
|
||||
.selected-tags button { font: inherit; color: #534a8b; background: white; border: 1px solid #d9d5f0; border-radius: 4px; padding: 5px 8px; text-align: left; overflow-wrap: anywhere; cursor: pointer; }
|
||||
footer { display: flex; justify-content: space-between; align-items: center; font-size: 13px; }
|
||||
@media (min-width: 700px) { .permission-list { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
|
||||
</style>
|
||||
@@ -1,16 +1,22 @@
|
||||
<script setup lang="ts">
|
||||
import { X } from 'lucide-vue-next'
|
||||
|
||||
defineProps<{ title: string; eyebrow?: string }>()
|
||||
defineProps<{ title: string; eyebrow?: string; wide?: boolean }>()
|
||||
defineEmits<{ close: [] }>()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="modal-overlay" @click.self="$emit('close')">
|
||||
<section class="modal" role="dialog" aria-modal="true" :aria-label="title">
|
||||
<section class="modal" :class="{ 'modal-wide': wide }" role="dialog" aria-modal="true" :aria-label="title">
|
||||
<header><div><span>{{ eyebrow ?? 'MOYI CONSOLE' }}</span><h2>{{ title }}</h2></div><button class="icon-button" aria-label="关闭弹窗" title="关闭" @click="$emit('close')"><X :size="18" /></button></header>
|
||||
<div class="modal-body"><slot /></div>
|
||||
<footer><slot name="footer" /></footer>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.modal-wide { width: min(800px, 100%); display: flex; flex-direction: column; overflow: hidden; }
|
||||
.modal-wide .modal-body { overflow-y: auto; min-height: 0; padding-right: 4px; }
|
||||
.modal-wide > header, .modal-wide > footer { flex-shrink: 0; }
|
||||
</style>
|
||||
Reference in new issue
Block a user