Files
ecom-dmb/apps/web/apps/web-antd/src/views/agent/llm-settings/index.vue
T

234 lines
6.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { AgentVO } from '#/api/agent';
import { computed, ref } from 'vue';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import { Button, Popconfirm, Space, Tag, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteAgentLlmProviderApi,
listAgentLlmProvidersApi,
} from '#/api/agent';
import { PermissionGate } from '#/components/permission';
import {
syncVxeGridColumns,
useActionColumnVisible,
} from '#/composables/use-vxe-action-column';
import ProviderFormModal from './components/ProviderFormModal.vue';
import ProviderModelsModal from './components/ProviderModelsModal.vue';
defineOptions({ name: 'AgentLlmSettings' });
const PlusIcon = createIconifyIcon('lucide:plus');
const loading = ref(false);
const formModalRef = ref<InstanceType<typeof ProviderFormModal>>();
const modelsModalRef = ref<InstanceType<typeof ProviderModelsModal>>();
const actionCodes = ['agent:llm-settings'];
const showActionColumn = useActionColumnVisible(actionCodes);
type ProviderRow = AgentVO.LlmProvider & {
modelCount: number;
enabledCount: number;
defaultModelLabel: string;
};
function toRow(p: AgentVO.LlmProvider): ProviderRow {
const models = p.models || [];
const enabled = models.filter((m) => m.enabled !== false);
const def = enabled.find((m) => m.isDefault) || models.find((m) => m.isDefault);
return {
...p,
modelCount: models.length,
enabledCount: enabled.length,
defaultModelLabel: def?.model || '-',
};
}
const baseColumns: VxeGridProps<ProviderRow>['columns'] = [
{ type: 'seq', title: '序号', minWidth: 60 },
{
field: 'displayName',
title: '名称',
minWidth: 140,
formatter: ({ row }) => row.displayName || '-',
},
{
field: 'baseUrl',
title: 'Base URL',
minWidth: 220,
showOverflow: 'tooltip',
},
{
field: 'apiStyle',
title: 'API 协议',
minWidth: 120,
formatter: ({ row }) => {
const m: Record<string, string> = {
auto: '自动',
completions: 'Completions',
responses: 'Responses',
};
return m[row.apiStyle || 'auto'] || row.apiStyle || '自动';
},
},
{
field: 'apiKeyMasked',
title: 'API Key',
minWidth: 120,
formatter: ({ row }) => row.apiKeyMasked || '-',
},
{
field: 'enabledCount',
title: '模型',
minWidth: 120,
slots: { default: 'modelCount' },
},
{
field: 'defaultModelLabel',
title: '公司默认',
minWidth: 160,
showOverflow: 'tooltip',
},
{
field: 'updateTime',
title: '更新时间',
minWidth: 160,
},
];
const actionColumn: NonNullable<VxeGridProps<ProviderRow>['columns']>[number] = {
title: '操作',
width: 240,
fixed: 'right',
slots: { default: 'action' },
};
const tableColumns = computed(() =>
showActionColumn.value
? [...(baseColumns ?? []), actionColumn]
: (baseColumns ?? []),
);
const gridOptions: VxeGridProps<ProviderRow> = {
columns: tableColumns.value,
height: 'auto',
border: true,
stripe: true,
rowConfig: { isHover: true },
showOverflow: true,
size: 'small',
pagerConfig: { enabled: false },
toolbarConfig: {
custom: true,
refresh: true,
zoom: true,
},
proxyConfig: {
ajax: {
query: async () => {
loading.value = true;
try {
const res = await listAgentLlmProvidersApi();
const items = (res.items || []).map(toRow);
return { items, total: items.length };
} finally {
loading.value = false;
}
},
},
},
};
const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
syncVxeGridColumns(gridApi, tableColumns);
function openCreate() {
formModalRef.value?.open();
}
function openEdit(row: ProviderRow) {
formModalRef.value?.open(row);
}
function openModels(row: ProviderRow) {
modelsModalRef.value?.open(row);
}
async function handleDelete(row: ProviderRow) {
try {
await deleteAgentLlmProviderApi(row.id);
message.success('已删除');
gridApi.reload();
} catch (e: any) {
message.error(e?.message || '删除失败');
}
}
function onSaved() {
gridApi.reload();
}
</script>
<template>
<Page auto-content-height>
<p class="text-muted-foreground mb-3 text-sm">
可配置多个 OpenAI Compatible 提供商;跨提供商允许同名模型。公司全局仅一个默认模型。
</p>
<Grid :loading="loading">
<template #toolbar-tools>
<PermissionGate code="agent:llm-settings">
<Button type="primary" @click="openCreate">
<PlusIcon class="size-4" />
添加提供商
</Button>
</PermissionGate>
</template>
<template #modelCount="{ row }">
<span v-if="row.modelCount > 0">
<Tag color="blue">{{ row.enabledCount }} 启用</Tag>
<span
v-if="row.enabledCount < row.modelCount"
class="text-muted-foreground ml-1 text-xs"
>
/ {{ row.modelCount }}
</span>
</span>
<span v-else class="text-muted-foreground">0</span>
</template>
<template v-if="showActionColumn" #action="{ row }">
<Space>
<PermissionGate code="agent:llm-settings">
<Button type="link" size="small" @click="openModels(row)">
模型
</Button>
<Button type="link" size="small" @click="openEdit(row)">
编辑
</Button>
<Popconfirm
title="删除该提供商及其启用模型?"
ok-text="删除"
cancel-text="取消"
@confirm="handleDelete(row)"
>
<Button type="link" danger size="small">删除</Button>
</Popconfirm>
</PermissionGate>
</Space>
</template>
</Grid>
<ProviderFormModal ref="formModalRef" @success="onSaved" />
<ProviderModelsModal ref="modelsModalRef" @success="onSaved" />
</Page>
</template>