渠道: 模型名称映射(网关生效 + 渠道侧管理)
- 候选渠道携带 upstream_model, prepareUpstream 改写请求体 model 字段为上游名 - 新增渠道视角绑定 CRUD: GET/POST/PATCH/DELETE /admin/channels/:id/models - 前端渠道页新增"模型映射": 列出绑定、内联改上游名、解除、添加 - rewriteModel 三种协议通用(model 均在顶层) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -123,6 +123,82 @@ async function importModels(ch: Channel) {
|
||||
}
|
||||
}
|
||||
|
||||
// --- 渠道模型映射 ---
|
||||
const modelsOpen = ref(false)
|
||||
const mappingChan = ref<Channel | null>(null)
|
||||
const mappings = ref<ChannelModelMapping[]>([])
|
||||
const availableModels = ref<string[]>([])
|
||||
const mappingForm = reactive({ model_name: '', upstream_model: '', weight: 1 })
|
||||
|
||||
async function openModels(ch: Channel) {
|
||||
mappingChan.value = ch
|
||||
mappingForm.model_name = ''
|
||||
mappingForm.upstream_model = ''
|
||||
mappingForm.weight = 1
|
||||
modelsOpen.value = true
|
||||
await Promise.all([loadMappings(), loadAvailableModels()])
|
||||
}
|
||||
|
||||
async function loadMappings() {
|
||||
if (!mappingChan.value) return
|
||||
try {
|
||||
const { data } = await http.get(`/admin/channels/${mappingChan.value.id}/models`)
|
||||
mappings.value = data.data.items
|
||||
} catch (e) {
|
||||
toast.err(errMsg(e))
|
||||
}
|
||||
}
|
||||
|
||||
async function loadAvailableModels() {
|
||||
try {
|
||||
const { data } = await http.get('/admin/models')
|
||||
availableModels.value = (data.data.items as { name: string }[]).map((m) => m.name)
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
}
|
||||
|
||||
async function addMapping() {
|
||||
if (!mappingChan.value || !mappingForm.model_name) return
|
||||
try {
|
||||
await http.post(`/admin/channels/${mappingChan.value.id}/models`, {
|
||||
model_name: mappingForm.model_name,
|
||||
upstream_model: mappingForm.upstream_model,
|
||||
weight: Number(mappingForm.weight) || 1,
|
||||
})
|
||||
toast.ok('已绑定')
|
||||
mappingForm.upstream_model = ''
|
||||
await loadMappings()
|
||||
} catch (e) {
|
||||
toast.err(errMsg(e))
|
||||
}
|
||||
}
|
||||
|
||||
async function saveMapping(b: ChannelModelMapping) {
|
||||
if (!mappingChan.value) return
|
||||
try {
|
||||
await http.patch(`/admin/channels/${mappingChan.value.id}/models/${b.id}`, {
|
||||
upstream_model: b.upstream_model,
|
||||
})
|
||||
toast.ok('已更新')
|
||||
await loadMappings()
|
||||
} catch (e) {
|
||||
toast.err(errMsg(e))
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteMapping(b: ChannelModelMapping) {
|
||||
if (!mappingChan.value) return
|
||||
if (!confirm(`解除模型 ${b.model_name} 的绑定?`)) return
|
||||
try {
|
||||
await http.delete(`/admin/channels/${mappingChan.value.id}/models/${b.id}`)
|
||||
toast.ok('已解除')
|
||||
await loadMappings()
|
||||
} catch (e) {
|
||||
toast.err(errMsg(e))
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(load)
|
||||
</script>
|
||||
|
||||
@@ -176,6 +252,7 @@ onMounted(load)
|
||||
{{ busyId === ch.id ? '测试中…' : '测试' }}
|
||||
</button>
|
||||
<button class="text-xs text-muted hover:text-accent" @click="importModels(ch)">导入模型</button>
|
||||
<button class="text-xs text-muted hover:text-accent" @click="openModels(ch)">模型映射</button>
|
||||
<button class="text-xs text-muted hover:text-ink" @click="openEdit(ch)">编辑</button>
|
||||
<button class="text-xs text-muted hover:text-err" @click="remove(ch)">删除</button>
|
||||
</div>
|
||||
@@ -235,5 +312,57 @@ onMounted(load)
|
||||
<Button :loading="saving" @click="save">{{ editing ? '保存' : '创建' }}</Button>
|
||||
</template>
|
||||
</Modal>
|
||||
|
||||
<!-- 模型名称映射 -->
|
||||
<Modal :open="modelsOpen" :title="`模型映射 · ${mappingChan?.name}`" @close="modelsOpen = false">
|
||||
<div class="mb-3 text-xs text-muted">客户端调用「模型名」时,网关转发为右侧「上游名称」。</div>
|
||||
<table class="w-full text-sm">
|
||||
<thead>
|
||||
<tr class="border-b border-edge text-left text-xs text-muted">
|
||||
<th scope="col" class="px-3 py-2 font-medium">模型</th>
|
||||
<th scope="col" class="px-3 py-2 font-medium">上游名称</th>
|
||||
<th scope="col" class="px-3 py-2 font-medium">权重</th>
|
||||
<th scope="col" class="px-3 py-2" />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="b in mappings" :key="b.id" class="border-b border-edge last:border-0">
|
||||
<td class="px-3 py-2 font-mono text-xs text-ink">{{ b.model_name }}</td>
|
||||
<td class="px-3 py-2">
|
||||
<input
|
||||
v-model="b.upstream_model"
|
||||
class="h-8 w-full min-w-36 rounded-md border border-edge2 bg-surface px-2 font-mono text-xs text-ink outline-none focus:border-accent"
|
||||
/>
|
||||
</td>
|
||||
<td class="px-3 py-2 mono-num text-xs text-muted">{{ b.weight }}</td>
|
||||
<td class="px-3 py-2 text-right">
|
||||
<div class="flex justify-end gap-2.5">
|
||||
<button class="text-xs text-muted hover:text-ink" @click="saveMapping(b)">保存</button>
|
||||
<button class="text-xs text-muted hover:text-err" @click="deleteMapping(b)">解除</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr v-if="mappings.length === 0">
|
||||
<td colspan="4" class="px-3 py-6 text-center text-xs text-muted">尚未绑定模型,可在下方添加</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="mt-3 flex items-center gap-2 border-t border-edge pt-3">
|
||||
<select
|
||||
v-model="mappingForm.model_name"
|
||||
class="h-9 flex-1 rounded-md border border-edge2 bg-surface px-2 text-xs text-ink outline-none focus:border-accent"
|
||||
>
|
||||
<option value="" disabled>选择全局模型</option>
|
||||
<option v-for="m in availableModels" :key="m" :value="m">{{ m }}</option>
|
||||
</select>
|
||||
<input
|
||||
v-model="mappingForm.upstream_model"
|
||||
placeholder="上游名称(默认同名)"
|
||||
class="h-9 w-44 rounded-md border border-edge2 bg-surface px-2 font-mono text-xs outline-none focus:border-accent"
|
||||
@keyup.enter="addMapping"
|
||||
/>
|
||||
<Button size="sm" @click="addMapping">绑定</Button>
|
||||
</div>
|
||||
</Modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user