mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 00:29:19 +08:00
feat: 模型管理独立页与会话级选模型
This commit is contained in:
@@ -9,6 +9,7 @@ export namespace AgentVO {
|
||||
threadId: string;
|
||||
title: string;
|
||||
status: string;
|
||||
llmModel?: string | null;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
lastMessageAt?: string | null;
|
||||
@@ -57,8 +58,24 @@ export async function listAgentSessionsApi() {
|
||||
return requestClient.get<{ items: AgentVO.Session[] }>('/agent/sessions');
|
||||
}
|
||||
|
||||
export async function createAgentSessionApi(title?: string) {
|
||||
return requestClient.post<AgentVO.Session>('/agent/sessions', { title });
|
||||
export async function createAgentSessionApi(options?: {
|
||||
title?: string;
|
||||
llmModel?: string;
|
||||
}) {
|
||||
return requestClient.post<AgentVO.Session>('/agent/sessions', {
|
||||
title: options?.title,
|
||||
llmModel: options?.llmModel,
|
||||
});
|
||||
}
|
||||
|
||||
export async function patchAgentSessionApi(
|
||||
threadId: string,
|
||||
body: { title?: string; llmModel?: string },
|
||||
) {
|
||||
return requestClient.patch<AgentVO.Session>(
|
||||
`/agent/sessions/${threadId}`,
|
||||
body,
|
||||
);
|
||||
}
|
||||
|
||||
export async function listAgentMessagesApi(threadId: string) {
|
||||
@@ -98,6 +115,13 @@ export async function updateAgentLlmSettingsApi(body: {
|
||||
);
|
||||
}
|
||||
|
||||
/** 聊天选模型:已启用列表,不含 Key */
|
||||
export async function listAgentEnabledLlmModelsApi() {
|
||||
return requestClient.get<{ items: AgentVO.LlmModelItem[] }>(
|
||||
'/company/agent-llm-models',
|
||||
);
|
||||
}
|
||||
|
||||
/** 服务端代理 GET /v1/models,不落库 */
|
||||
export async function probeAgentLlmModelsApi(body: {
|
||||
baseUrl: string;
|
||||
|
||||
@@ -3,9 +3,11 @@ export {
|
||||
createAgentSessionApi,
|
||||
deleteAgentMemoryApi,
|
||||
getAgentLlmSettingsApi,
|
||||
listAgentEnabledLlmModelsApi,
|
||||
listAgentMemoriesApi,
|
||||
listAgentMessagesApi,
|
||||
listAgentSessionsApi,
|
||||
patchAgentSessionApi,
|
||||
probeAgentLlmModelsApi,
|
||||
streamAgentChat,
|
||||
updateAgentLlmSettingsApi,
|
||||
|
||||
@@ -2,36 +2,21 @@
|
||||
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { PlusOutlined, SettingOutlined } from '@ant-design/icons-vue';
|
||||
import { PlusOutlined } from '@ant-design/icons-vue';
|
||||
|
||||
import {
|
||||
Button,
|
||||
Checkbox,
|
||||
Empty,
|
||||
Form,
|
||||
FormItem,
|
||||
Input,
|
||||
InputPassword,
|
||||
Modal,
|
||||
Radio,
|
||||
Spin,
|
||||
message,
|
||||
} from 'ant-design-vue';
|
||||
import { Button, Empty, Select, Spin, message } from 'ant-design-vue';
|
||||
import { ChatItem } from '@tdesign-vue-next/chat';
|
||||
import 'tdesign-vue-next/es/style/index.css';
|
||||
import '@tdesign-vue-next/chat/es/style/index.css';
|
||||
|
||||
import {
|
||||
createAgentSessionApi,
|
||||
deleteAgentMemoryApi,
|
||||
getAgentLlmSettingsApi,
|
||||
listAgentMemoriesApi,
|
||||
listAgentEnabledLlmModelsApi,
|
||||
listAgentMessagesApi,
|
||||
listAgentSessionsApi,
|
||||
probeAgentLlmModelsApi,
|
||||
patchAgentSessionApi,
|
||||
streamAgentChat,
|
||||
type AgentVO,
|
||||
updateAgentLlmSettingsApi,
|
||||
} from '#/api/agent';
|
||||
|
||||
import CatalogCiteSender from './catalog-cite-sender.vue';
|
||||
@@ -76,26 +61,10 @@ const toolStatusActive = ref(false);
|
||||
const draft = ref('');
|
||||
/** 本条消息附带的店铺/商品/SKU,发送时写成 [引用] 块 */
|
||||
const citations = ref<CatalogCite[]>([]);
|
||||
const settingsOpen = ref(false);
|
||||
const savingSettings = ref(false);
|
||||
const probingModels = ref(false);
|
||||
const memories = ref<AgentVO.Memory[]>([]);
|
||||
/** 探测到的候选模型(勾选后进 enabledModels) */
|
||||
const probedModels = ref<AgentVO.ProbedLlmModel[]>([]);
|
||||
const manualModelId = ref('');
|
||||
type EnabledModelRow = {
|
||||
model: string;
|
||||
contextWindow: string;
|
||||
isDefault: boolean;
|
||||
};
|
||||
const llmForm = ref({
|
||||
baseUrl: '',
|
||||
apiKey: '',
|
||||
apiKeyMasked: '' as string | null,
|
||||
configured: false,
|
||||
displayName: '',
|
||||
});
|
||||
const enabledModels = ref<EnabledModelRow[]>([]);
|
||||
const enabledModels = ref<AgentVO.LlmModelItem[]>([]);
|
||||
/** 当前选中模型(有会话时与 session.llmModel 同步;草稿态本地暂存) */
|
||||
const selectedModel = ref<string | undefined>(undefined);
|
||||
const patchingModel = ref(false);
|
||||
const messagePaneRef = ref<HTMLElement | null>(null);
|
||||
let abort: AbortController | null = null;
|
||||
let messagesLoadSeq = 0;
|
||||
@@ -104,6 +73,13 @@ const activeSession = computed(() =>
|
||||
sessions.value.find((s) => s.threadId === activeThreadId.value),
|
||||
);
|
||||
|
||||
const modelOptions = computed(() =>
|
||||
enabledModels.value.map((m) => ({
|
||||
value: m.model,
|
||||
label: m.label || m.model,
|
||||
})),
|
||||
);
|
||||
|
||||
const showMessageLoading = computed(
|
||||
() => loadingMessages.value && !sending.value && messages.value.length === 0,
|
||||
);
|
||||
@@ -151,6 +127,22 @@ function scrollChatToBottom() {
|
||||
el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
|
||||
function defaultModelId() {
|
||||
const d = enabledModels.value.find((m) => m.isDefault);
|
||||
return d?.model || enabledModels.value[0]?.model;
|
||||
}
|
||||
|
||||
function syncSelectedFromSession() {
|
||||
const s = activeSession.value;
|
||||
if (s?.llmModel) {
|
||||
selectedModel.value = s.llmModel;
|
||||
return;
|
||||
}
|
||||
if (!selectedModel.value) {
|
||||
selectedModel.value = defaultModelId();
|
||||
}
|
||||
}
|
||||
|
||||
function onSessionClick(threadId: string) {
|
||||
if (sending.value) {
|
||||
message.warning('回复生成中,请稍候再切换会话');
|
||||
@@ -169,6 +161,7 @@ async function refreshSessions() {
|
||||
if (!composingNew.value && !activeThreadId.value && sessions.value[0]) {
|
||||
activeThreadId.value = sessions.value[0].threadId;
|
||||
}
|
||||
syncSelectedFromSession();
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载会话失败');
|
||||
} finally {
|
||||
@@ -176,6 +169,18 @@ async function refreshSessions() {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadEnabledModels() {
|
||||
try {
|
||||
const res = await listAgentEnabledLlmModelsApi();
|
||||
enabledModels.value = res.items || [];
|
||||
if (!selectedModel.value) {
|
||||
selectedModel.value = defaultModelId();
|
||||
}
|
||||
} catch {
|
||||
enabledModels.value = [];
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMessages(threadId: string) {
|
||||
const seq = ++messagesLoadSeq;
|
||||
loadingMessages.value = true;
|
||||
@@ -190,6 +195,7 @@ async function loadMessages(threadId: string) {
|
||||
}
|
||||
messages.value = res.items || [];
|
||||
streamingText.value = '';
|
||||
syncSelectedFromSession();
|
||||
await nextTick();
|
||||
scrollChatToBottom();
|
||||
} catch (e: any) {
|
||||
@@ -205,10 +211,14 @@ async function loadMessages(threadId: string) {
|
||||
|
||||
watch(activeThreadId, (id) => {
|
||||
if (sending.value) return;
|
||||
if (id) void loadMessages(id);
|
||||
else {
|
||||
if (id) {
|
||||
void loadMessages(id);
|
||||
} else {
|
||||
messages.value = [];
|
||||
streamingText.value = '';
|
||||
if (!selectedModel.value) {
|
||||
selectedModel.value = defaultModelId();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -227,211 +237,40 @@ function beginNewChat() {
|
||||
streamingText.value = '';
|
||||
draft.value = '';
|
||||
citations.value = [];
|
||||
selectedModel.value = defaultModelId();
|
||||
}
|
||||
|
||||
/** 第一条消息发出时才落库 */
|
||||
async function ensureSession(): Promise<string | null> {
|
||||
if (activeThreadId.value) return activeThreadId.value;
|
||||
const s = await createAgentSessionApi();
|
||||
const s = await createAgentSessionApi({
|
||||
llmModel: selectedModel.value,
|
||||
});
|
||||
composingNew.value = false;
|
||||
sessions.value = [s, ...sessions.value];
|
||||
activeThreadId.value = s.threadId;
|
||||
if (s.llmModel) {
|
||||
selectedModel.value = s.llmModel;
|
||||
}
|
||||
return s.threadId;
|
||||
}
|
||||
|
||||
async function openSettings() {
|
||||
settingsOpen.value = true;
|
||||
probedModels.value = [];
|
||||
manualModelId.value = '';
|
||||
async function onModelChange(model: string) {
|
||||
selectedModel.value = model;
|
||||
const threadId = activeThreadId.value;
|
||||
if (!threadId || composingNew.value) return;
|
||||
patchingModel.value = true;
|
||||
try {
|
||||
const [s, mem] = await Promise.all([
|
||||
getAgentLlmSettingsApi(),
|
||||
listAgentMemoriesApi().catch(() => ({ items: [] as AgentVO.Memory[] })),
|
||||
]);
|
||||
llmForm.value = {
|
||||
baseUrl: s.baseUrl || '',
|
||||
apiKey: '',
|
||||
apiKeyMasked: s.apiKeyMasked || null,
|
||||
configured: !!s.configured,
|
||||
displayName: s.displayName || '',
|
||||
};
|
||||
enabledModels.value = (s.models || []).map((m) => ({
|
||||
model: m.model,
|
||||
contextWindow:
|
||||
m.contextWindow != null && m.contextWindow > 0
|
||||
? String(m.contextWindow)
|
||||
: '',
|
||||
isDefault: !!m.isDefault,
|
||||
}));
|
||||
memories.value = mem.items || [];
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载模型配置失败');
|
||||
}
|
||||
}
|
||||
|
||||
function isModelEnabled(id: string) {
|
||||
return enabledModels.value.some((m) => m.model === id);
|
||||
}
|
||||
|
||||
function toggleProbedModel(p: AgentVO.ProbedLlmModel, checked: boolean) {
|
||||
if (checked) {
|
||||
if (isModelEnabled(p.id)) return;
|
||||
enabledModels.value.push({
|
||||
model: p.id,
|
||||
contextWindow:
|
||||
p.contextWindow != null && p.contextWindow > 0
|
||||
? String(p.contextWindow)
|
||||
: '',
|
||||
isDefault: enabledModels.value.length === 0,
|
||||
});
|
||||
} else {
|
||||
const wasDefault = enabledModels.value.find(
|
||||
(m) => m.model === p.id,
|
||||
)?.isDefault;
|
||||
enabledModels.value = enabledModels.value.filter((m) => m.model !== p.id);
|
||||
if (wasDefault && enabledModels.value.length > 0) {
|
||||
enabledModels.value[0]!.isDefault = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setDefaultModel(modelId: string) {
|
||||
enabledModels.value = enabledModels.value.map((m) => ({
|
||||
...m,
|
||||
isDefault: m.model === modelId,
|
||||
}));
|
||||
}
|
||||
|
||||
function removeEnabledModel(modelId: string) {
|
||||
const wasDefault = enabledModels.value.find(
|
||||
(m) => m.model === modelId,
|
||||
)?.isDefault;
|
||||
enabledModels.value = enabledModels.value.filter((m) => m.model !== modelId);
|
||||
if (wasDefault && enabledModels.value.length > 0) {
|
||||
enabledModels.value[0]!.isDefault = true;
|
||||
}
|
||||
}
|
||||
|
||||
function addManualModel() {
|
||||
const id = manualModelId.value.trim();
|
||||
if (!id) {
|
||||
message.warning('请输入模型 id');
|
||||
return;
|
||||
}
|
||||
if (isModelEnabled(id)) {
|
||||
message.warning('该模型已在启用列表中');
|
||||
return;
|
||||
}
|
||||
enabledModels.value.push({
|
||||
model: id,
|
||||
contextWindow: '',
|
||||
isDefault: enabledModels.value.length === 0,
|
||||
});
|
||||
manualModelId.value = '';
|
||||
}
|
||||
|
||||
async function probeModels() {
|
||||
if (!llmForm.value.baseUrl.trim()) {
|
||||
message.warning('请先填写 Base URL');
|
||||
return;
|
||||
}
|
||||
const useStored =
|
||||
!!llmForm.value.configured &&
|
||||
(!llmForm.value.apiKey.trim() ||
|
||||
llmForm.value.apiKey.trim().startsWith('****'));
|
||||
if (!useStored && !llmForm.value.apiKey.trim()) {
|
||||
message.warning('请填写 API Key,或先保存密钥后再用已存密钥探测');
|
||||
return;
|
||||
}
|
||||
probingModels.value = true;
|
||||
try {
|
||||
const res = await probeAgentLlmModelsApi({
|
||||
baseUrl: llmForm.value.baseUrl.trim(),
|
||||
apiKey: useStored ? undefined : llmForm.value.apiKey.trim(),
|
||||
useStoredKey: useStored,
|
||||
});
|
||||
probedModels.value = res.items || [];
|
||||
if (probedModels.value.length === 0) {
|
||||
message.warning('未返回模型,可手动添加模型 id');
|
||||
} else {
|
||||
message.success(`已加载 ${probedModels.value.length} 个模型`);
|
||||
const s = await patchAgentSessionApi(threadId, { llmModel: model });
|
||||
const idx = sessions.value.findIndex((x) => x.threadId === threadId);
|
||||
if (idx >= 0) {
|
||||
sessions.value[idx] = { ...sessions.value[idx]!, ...s };
|
||||
}
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载模型列表失败,可改为手动添加');
|
||||
message.error(e?.message || '切换模型失败');
|
||||
syncSelectedFromSession();
|
||||
} finally {
|
||||
probingModels.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function removeMemory(id: string) {
|
||||
try {
|
||||
await deleteAgentMemoryApi(id);
|
||||
memories.value = memories.value.filter((m) => m.id !== id);
|
||||
message.success('已删除记忆');
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '删除失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function saveSettings() {
|
||||
if (!llmForm.value.baseUrl.trim()) {
|
||||
message.warning('请填写 Base URL');
|
||||
return;
|
||||
}
|
||||
if (enabledModels.value.length === 0) {
|
||||
message.warning('请至少启用一个模型(加载列表勾选或手动添加)');
|
||||
return;
|
||||
}
|
||||
if (!enabledModels.value.some((m) => m.isDefault)) {
|
||||
enabledModels.value[0]!.isDefault = true;
|
||||
}
|
||||
if (!llmForm.value.configured && !llmForm.value.apiKey.trim()) {
|
||||
message.warning('首次配置必须填写 API Key');
|
||||
return;
|
||||
}
|
||||
savingSettings.value = true;
|
||||
try {
|
||||
const models = enabledModels.value.map((m) => {
|
||||
const cw = m.contextWindow.trim();
|
||||
let contextWindow: number | null | undefined;
|
||||
if (cw === '') {
|
||||
contextWindow = 0;
|
||||
} else {
|
||||
const n = Number(cw);
|
||||
if (!Number.isFinite(n) || n < 0) {
|
||||
throw new Error(`模型 ${m.model} 的上下文窗口无效`);
|
||||
}
|
||||
contextWindow = Math.floor(n);
|
||||
}
|
||||
return {
|
||||
model: m.model,
|
||||
contextWindow,
|
||||
isDefault: m.isDefault,
|
||||
};
|
||||
});
|
||||
const s = await updateAgentLlmSettingsApi({
|
||||
baseUrl: llmForm.value.baseUrl.trim(),
|
||||
apiKey: llmForm.value.apiKey.trim() || undefined,
|
||||
displayName: llmForm.value.displayName.trim() || undefined,
|
||||
models,
|
||||
});
|
||||
llmForm.value.configured = s.configured;
|
||||
llmForm.value.apiKeyMasked = s.apiKeyMasked || null;
|
||||
llmForm.value.apiKey = '';
|
||||
enabledModels.value = (s.models || []).map((m) => ({
|
||||
model: m.model,
|
||||
contextWindow:
|
||||
m.contextWindow != null && m.contextWindow > 0
|
||||
? String(m.contextWindow)
|
||||
: '',
|
||||
isDefault: !!m.isDefault,
|
||||
}));
|
||||
message.success('模型配置已保存');
|
||||
settingsOpen.value = false;
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '保存失败');
|
||||
} finally {
|
||||
savingSettings.value = false;
|
||||
patchingModel.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -459,7 +298,6 @@ function onStreamTool(payload: {
|
||||
toolStatusLabel.value = payload.label;
|
||||
void nextTick().then(scrollChatToBottom);
|
||||
} else if (payload.phase === 'end') {
|
||||
// 同轮可能多个工具;最后一个 end 后仍保持到有 token / done 再清也可
|
||||
toolStatusLabel.value = payload.label;
|
||||
}
|
||||
}
|
||||
@@ -558,7 +396,7 @@ function onSenderStop() {
|
||||
|
||||
onBeforeUnmount(() => abort?.abort());
|
||||
|
||||
void refreshSessions();
|
||||
void loadEnabledModels().then(() => refreshSessions());
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -618,25 +456,29 @@ void refreshSessions();
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="border-t border-border p-3">
|
||||
<Button block @click="openSettings">
|
||||
<template #icon><SettingOutlined /></template>
|
||||
模型 API 设置
|
||||
</Button>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<section class="agent-chat-main flex min-h-0 min-w-0 flex-1 flex-col">
|
||||
<header
|
||||
class="flex h-12 shrink-0 items-center border-b border-border px-4"
|
||||
class="flex h-12 shrink-0 items-center gap-3 border-b border-border px-4"
|
||||
>
|
||||
<span class="truncate font-medium">
|
||||
<span class="min-w-0 flex-1 truncate font-medium">
|
||||
{{
|
||||
composingNew
|
||||
? '新对话'
|
||||
: activeSession?.title || '智能助手'
|
||||
}}
|
||||
</span>
|
||||
<Select
|
||||
:value="selectedModel"
|
||||
:options="modelOptions"
|
||||
:loading="patchingModel"
|
||||
:disabled="sending || modelOptions.length === 0"
|
||||
placeholder="选择模型"
|
||||
class="w-56 shrink-0"
|
||||
:allow-clear="false"
|
||||
@change="(v: string) => onModelChange(v)"
|
||||
/>
|
||||
</header>
|
||||
|
||||
<div class="relative flex min-h-0 flex-1 flex-col">
|
||||
@@ -649,7 +491,7 @@ void refreshSessions();
|
||||
<Empty
|
||||
v-if="!showStarter && !showMessageLoading && messages.length === 0"
|
||||
class="absolute inset-0 flex items-center justify-center"
|
||||
description="开始提问吧(需先配置公司模型 API)"
|
||||
description="开始提问吧(需先在「模型管理」配置 API)"
|
||||
/>
|
||||
<div
|
||||
ref="messagePaneRef"
|
||||
@@ -714,151 +556,6 @@ void refreshSessions();
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<Modal
|
||||
v-model:open="settingsOpen"
|
||||
title="公司模型 API(OpenAI Compatible)"
|
||||
:confirm-loading="savingSettings"
|
||||
ok-text="保存"
|
||||
width="640px"
|
||||
destroy-on-close
|
||||
@ok="saveSettings"
|
||||
>
|
||||
<p class="text-muted-foreground mb-3 text-sm">
|
||||
填写 Base URL 与 API Key 后加载模型列表并勾选启用;指定一个默认模型供对话使用。Key
|
||||
加密落库,界面仅显示掩码。上下文窗口多数厂商不会在 API 返回,有则预填,可改或留空由
|
||||
Agent 估计。
|
||||
</p>
|
||||
<Form layout="vertical">
|
||||
<FormItem label="Base URL" required>
|
||||
<Input
|
||||
v-model:value="llmForm.baseUrl"
|
||||
placeholder="https://api.deepseek.com/v1"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem
|
||||
:label="
|
||||
llmForm.configured
|
||||
? `API Key(已配置 ${llmForm.apiKeyMasked || '****'},留空则用已存密钥探测/不改 Key)`
|
||||
: 'API Key'
|
||||
"
|
||||
:required="!llmForm.configured"
|
||||
>
|
||||
<InputPassword
|
||||
v-model:value="llmForm.apiKey"
|
||||
placeholder="sk-..."
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="提供商备注(可选)">
|
||||
<Input
|
||||
v-model:value="llmForm.displayName"
|
||||
placeholder="如 DeepSeek"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="模型列表">
|
||||
<div class="mb-2 flex flex-wrap gap-2">
|
||||
<Button
|
||||
type="primary"
|
||||
ghost
|
||||
:loading="probingModels"
|
||||
@click="probeModels"
|
||||
>
|
||||
加载模型列表
|
||||
</Button>
|
||||
</div>
|
||||
<div
|
||||
v-if="probedModels.length > 0"
|
||||
class="mb-3 max-h-40 space-y-1 overflow-y-auto rounded-md border border-border p-2"
|
||||
>
|
||||
<label
|
||||
v-for="p in probedModels"
|
||||
:key="p.id"
|
||||
class="flex cursor-pointer items-center gap-2 text-sm"
|
||||
>
|
||||
<Checkbox
|
||||
:checked="isModelEnabled(p.id)"
|
||||
@update:checked="(v: boolean) => toggleProbedModel(p, v)"
|
||||
/>
|
||||
<span class="min-w-0 flex-1 truncate">{{ p.id }}</span>
|
||||
<span
|
||||
v-if="p.contextWindow"
|
||||
class="text-muted-foreground shrink-0 text-xs"
|
||||
>
|
||||
{{ p.contextWindow }}
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="mb-2 flex gap-2">
|
||||
<Input
|
||||
v-model:value="manualModelId"
|
||||
class="flex-1"
|
||||
placeholder="手动添加模型 id,如 deepseek-chat"
|
||||
@press-enter="addManualModel"
|
||||
/>
|
||||
<Button @click="addManualModel">添加</Button>
|
||||
</div>
|
||||
<div
|
||||
v-if="enabledModels.length === 0"
|
||||
class="text-muted-foreground text-xs"
|
||||
>
|
||||
尚未启用模型。请加载列表勾选,或手动添加。
|
||||
</div>
|
||||
<div v-else class="space-y-2">
|
||||
<div
|
||||
v-for="m in enabledModels"
|
||||
:key="m.model"
|
||||
class="rounded-md border border-border px-2 py-2 text-sm"
|
||||
>
|
||||
<div class="mb-1 flex items-center gap-2">
|
||||
<Radio
|
||||
:checked="m.isDefault"
|
||||
@change="setDefaultModel(m.model)"
|
||||
>
|
||||
默认
|
||||
</Radio>
|
||||
<span class="min-w-0 flex-1 truncate font-medium">{{
|
||||
m.model
|
||||
}}</span>
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
danger
|
||||
@click="removeEnabledModel(m.model)"
|
||||
>
|
||||
移除
|
||||
</Button>
|
||||
</div>
|
||||
<Input
|
||||
v-model:value="m.contextWindow"
|
||||
size="small"
|
||||
placeholder="上下文窗口 token,留空=自动估计"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</FormItem>
|
||||
</Form>
|
||||
<div class="mt-4 border-t border-border pt-3">
|
||||
<div class="mb-2 text-sm font-medium">我的记忆</div>
|
||||
<p class="text-muted-foreground mb-2 text-xs">
|
||||
对话中发送「记住:…」可写入;助手按需检索,不会默认注入全部记忆。
|
||||
</p>
|
||||
<div v-if="memories.length === 0" class="text-muted-foreground text-xs">
|
||||
暂无记忆
|
||||
</div>
|
||||
<div
|
||||
v-for="m in memories"
|
||||
:key="m.id"
|
||||
class="mb-1 flex items-start gap-2 rounded-md bg-muted/50 px-2 py-1.5 text-xs"
|
||||
>
|
||||
<span class="min-w-0 flex-1 break-words">{{
|
||||
m.summary || m.text
|
||||
}}</span>
|
||||
<Button type="link" size="small" danger @click="removeMemory(m.id)">
|
||||
删
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -0,0 +1,405 @@
|
||||
<script lang="ts" setup>
|
||||
import { onMounted, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
|
||||
import {
|
||||
Button,
|
||||
Card,
|
||||
Checkbox,
|
||||
Form,
|
||||
FormItem,
|
||||
Input,
|
||||
InputPassword,
|
||||
Radio,
|
||||
Spin,
|
||||
message,
|
||||
} from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
deleteAgentMemoryApi,
|
||||
getAgentLlmSettingsApi,
|
||||
listAgentMemoriesApi,
|
||||
probeAgentLlmModelsApi,
|
||||
type AgentVO,
|
||||
updateAgentLlmSettingsApi,
|
||||
} from '#/api/agent';
|
||||
|
||||
defineOptions({ name: 'AgentLlmSettings' });
|
||||
|
||||
const loading = ref(true);
|
||||
const saving = ref(false);
|
||||
const probingModels = ref(false);
|
||||
const memories = ref<AgentVO.Memory[]>([]);
|
||||
const probedModels = ref<AgentVO.ProbedLlmModel[]>([]);
|
||||
const manualModelId = ref('');
|
||||
|
||||
type EnabledModelRow = {
|
||||
model: string;
|
||||
contextWindow: string;
|
||||
isDefault: boolean;
|
||||
};
|
||||
|
||||
const llmForm = ref({
|
||||
baseUrl: '',
|
||||
apiKey: '',
|
||||
apiKeyMasked: '' as string | null,
|
||||
configured: false,
|
||||
displayName: '',
|
||||
});
|
||||
const enabledModels = ref<EnabledModelRow[]>([]);
|
||||
|
||||
async function loadAll() {
|
||||
loading.value = true;
|
||||
probedModels.value = [];
|
||||
manualModelId.value = '';
|
||||
try {
|
||||
const [s, mem] = await Promise.all([
|
||||
getAgentLlmSettingsApi(),
|
||||
listAgentMemoriesApi().catch(() => ({ items: [] as AgentVO.Memory[] })),
|
||||
]);
|
||||
llmForm.value = {
|
||||
baseUrl: s.baseUrl || '',
|
||||
apiKey: '',
|
||||
apiKeyMasked: s.apiKeyMasked || null,
|
||||
configured: !!s.configured,
|
||||
displayName: s.displayName || '',
|
||||
};
|
||||
enabledModels.value = (s.models || []).map((m) => ({
|
||||
model: m.model,
|
||||
contextWindow:
|
||||
m.contextWindow != null && m.contextWindow > 0
|
||||
? String(m.contextWindow)
|
||||
: '',
|
||||
isDefault: !!m.isDefault,
|
||||
}));
|
||||
memories.value = mem.items || [];
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载模型配置失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function isModelEnabled(id: string) {
|
||||
return enabledModels.value.some((m) => m.model === id);
|
||||
}
|
||||
|
||||
function toggleProbedModel(p: AgentVO.ProbedLlmModel, checked: boolean) {
|
||||
if (checked) {
|
||||
if (isModelEnabled(p.id)) return;
|
||||
enabledModels.value.push({
|
||||
model: p.id,
|
||||
contextWindow:
|
||||
p.contextWindow != null && p.contextWindow > 0
|
||||
? String(p.contextWindow)
|
||||
: '',
|
||||
isDefault: enabledModels.value.length === 0,
|
||||
});
|
||||
} else {
|
||||
const wasDefault = enabledModels.value.find(
|
||||
(m) => m.model === p.id,
|
||||
)?.isDefault;
|
||||
enabledModels.value = enabledModels.value.filter((m) => m.model !== p.id);
|
||||
if (wasDefault && enabledModels.value.length > 0) {
|
||||
enabledModels.value[0]!.isDefault = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setDefaultModel(modelId: string) {
|
||||
enabledModels.value = enabledModels.value.map((m) => ({
|
||||
...m,
|
||||
isDefault: m.model === modelId,
|
||||
}));
|
||||
}
|
||||
|
||||
function removeEnabledModel(modelId: string) {
|
||||
const wasDefault = enabledModels.value.find(
|
||||
(m) => m.model === modelId,
|
||||
)?.isDefault;
|
||||
enabledModels.value = enabledModels.value.filter((m) => m.model !== modelId);
|
||||
if (wasDefault && enabledModels.value.length > 0) {
|
||||
enabledModels.value[0]!.isDefault = true;
|
||||
}
|
||||
}
|
||||
|
||||
function addManualModel() {
|
||||
const id = manualModelId.value.trim();
|
||||
if (!id) {
|
||||
message.warning('请输入模型 id');
|
||||
return;
|
||||
}
|
||||
if (isModelEnabled(id)) {
|
||||
message.warning('该模型已在启用列表中');
|
||||
return;
|
||||
}
|
||||
enabledModels.value.push({
|
||||
model: id,
|
||||
contextWindow: '',
|
||||
isDefault: enabledModels.value.length === 0,
|
||||
});
|
||||
manualModelId.value = '';
|
||||
}
|
||||
|
||||
async function probeModels() {
|
||||
if (!llmForm.value.baseUrl.trim()) {
|
||||
message.warning('请先填写 Base URL');
|
||||
return;
|
||||
}
|
||||
const useStored =
|
||||
!!llmForm.value.configured &&
|
||||
(!llmForm.value.apiKey.trim() ||
|
||||
llmForm.value.apiKey.trim().startsWith('****'));
|
||||
if (!useStored && !llmForm.value.apiKey.trim()) {
|
||||
message.warning('请填写 API Key,或先保存密钥后再用已存密钥探测');
|
||||
return;
|
||||
}
|
||||
probingModels.value = true;
|
||||
try {
|
||||
const res = await probeAgentLlmModelsApi({
|
||||
baseUrl: llmForm.value.baseUrl.trim(),
|
||||
apiKey: useStored ? undefined : llmForm.value.apiKey.trim(),
|
||||
useStoredKey: useStored,
|
||||
});
|
||||
probedModels.value = res.items || [];
|
||||
if (probedModels.value.length === 0) {
|
||||
message.warning('未返回模型,可手动添加模型 id');
|
||||
} else {
|
||||
message.success(`已加载 ${probedModels.value.length} 个模型`);
|
||||
}
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载模型列表失败,可改为手动添加');
|
||||
} finally {
|
||||
probingModels.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function removeMemory(id: string) {
|
||||
try {
|
||||
await deleteAgentMemoryApi(id);
|
||||
memories.value = memories.value.filter((m) => m.id !== id);
|
||||
message.success('已删除记忆');
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '删除失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function saveSettings() {
|
||||
if (!llmForm.value.baseUrl.trim()) {
|
||||
message.warning('请填写 Base URL');
|
||||
return;
|
||||
}
|
||||
if (enabledModels.value.length === 0) {
|
||||
message.warning('请至少启用一个模型(加载列表勾选或手动添加)');
|
||||
return;
|
||||
}
|
||||
if (!enabledModels.value.some((m) => m.isDefault)) {
|
||||
enabledModels.value[0]!.isDefault = true;
|
||||
}
|
||||
if (!llmForm.value.configured && !llmForm.value.apiKey.trim()) {
|
||||
message.warning('首次配置必须填写 API Key');
|
||||
return;
|
||||
}
|
||||
saving.value = true;
|
||||
try {
|
||||
const models = enabledModels.value.map((m) => {
|
||||
const cw = m.contextWindow.trim();
|
||||
let contextWindow: number | null | undefined;
|
||||
if (cw === '') {
|
||||
contextWindow = 0;
|
||||
} else {
|
||||
const n = Number(cw);
|
||||
if (!Number.isFinite(n) || n < 0) {
|
||||
throw new Error(`模型 ${m.model} 的上下文窗口无效`);
|
||||
}
|
||||
contextWindow = Math.floor(n);
|
||||
}
|
||||
return {
|
||||
model: m.model,
|
||||
contextWindow,
|
||||
isDefault: m.isDefault,
|
||||
};
|
||||
});
|
||||
const s = await updateAgentLlmSettingsApi({
|
||||
baseUrl: llmForm.value.baseUrl.trim(),
|
||||
apiKey: llmForm.value.apiKey.trim() || undefined,
|
||||
displayName: llmForm.value.displayName.trim() || undefined,
|
||||
models,
|
||||
});
|
||||
llmForm.value.configured = s.configured;
|
||||
llmForm.value.apiKeyMasked = s.apiKeyMasked || null;
|
||||
llmForm.value.apiKey = '';
|
||||
enabledModels.value = (s.models || []).map((m) => ({
|
||||
model: m.model,
|
||||
contextWindow:
|
||||
m.contextWindow != null && m.contextWindow > 0
|
||||
? String(m.contextWindow)
|
||||
: '',
|
||||
isDefault: !!m.isDefault,
|
||||
}));
|
||||
message.success('模型配置已保存');
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '保存失败');
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void loadAll();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page auto-content-height>
|
||||
<div class="relative mx-auto max-w-3xl space-y-4 pb-8">
|
||||
<div
|
||||
v-if="loading"
|
||||
class="absolute inset-0 z-10 flex items-center justify-center bg-background/50"
|
||||
>
|
||||
<Spin />
|
||||
</div>
|
||||
|
||||
<Card title="公司模型 API(OpenAI Compatible)" :bordered="false">
|
||||
<p class="text-muted-foreground mb-4 text-sm">
|
||||
填写 Base URL 与 API Key 后加载模型列表并勾选启用;指定一个默认模型供新对话使用。Key
|
||||
加密落库,界面仅显示掩码。上下文窗口多数厂商不会在 API
|
||||
返回,有则预填,可改或留空由 Agent 估计。
|
||||
</p>
|
||||
<Form layout="vertical">
|
||||
<FormItem label="Base URL" required>
|
||||
<Input
|
||||
v-model:value="llmForm.baseUrl"
|
||||
placeholder="https://api.deepseek.com/v1"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem
|
||||
:label="
|
||||
llmForm.configured
|
||||
? `API Key(已配置 ${llmForm.apiKeyMasked || '****'},留空则用已存密钥探测/不改 Key)`
|
||||
: 'API Key'
|
||||
"
|
||||
:required="!llmForm.configured"
|
||||
>
|
||||
<InputPassword
|
||||
v-model:value="llmForm.apiKey"
|
||||
placeholder="sk-..."
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="提供商备注(可选)">
|
||||
<Input
|
||||
v-model:value="llmForm.displayName"
|
||||
placeholder="如 DeepSeek"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="模型列表">
|
||||
<div class="mb-2 flex flex-wrap gap-2">
|
||||
<Button
|
||||
type="primary"
|
||||
ghost
|
||||
:loading="probingModels"
|
||||
@click="probeModels"
|
||||
>
|
||||
加载模型列表
|
||||
</Button>
|
||||
</div>
|
||||
<div
|
||||
v-if="probedModels.length > 0"
|
||||
class="mb-3 max-h-40 space-y-1 overflow-y-auto rounded-md border border-border p-2"
|
||||
>
|
||||
<label
|
||||
v-for="p in probedModels"
|
||||
:key="p.id"
|
||||
class="flex cursor-pointer items-center gap-2 text-sm"
|
||||
>
|
||||
<Checkbox
|
||||
:checked="isModelEnabled(p.id)"
|
||||
@update:checked="(v: boolean) => toggleProbedModel(p, v)"
|
||||
/>
|
||||
<span class="min-w-0 flex-1 truncate">{{ p.id }}</span>
|
||||
<span
|
||||
v-if="p.contextWindow"
|
||||
class="text-muted-foreground shrink-0 text-xs"
|
||||
>
|
||||
{{ p.contextWindow }}
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="mb-2 flex gap-2">
|
||||
<Input
|
||||
v-model:value="manualModelId"
|
||||
class="flex-1"
|
||||
placeholder="手动添加模型 id,如 deepseek-chat"
|
||||
@press-enter="addManualModel"
|
||||
/>
|
||||
<Button @click="addManualModel">添加</Button>
|
||||
</div>
|
||||
<div
|
||||
v-if="enabledModels.length === 0"
|
||||
class="text-muted-foreground text-xs"
|
||||
>
|
||||
尚未启用模型。请加载列表勾选,或手动添加。
|
||||
</div>
|
||||
<div v-else class="space-y-2">
|
||||
<div
|
||||
v-for="m in enabledModels"
|
||||
:key="m.model"
|
||||
class="rounded-md border border-border px-2 py-2 text-sm"
|
||||
>
|
||||
<div class="mb-1 flex items-center gap-2">
|
||||
<Radio
|
||||
:checked="m.isDefault"
|
||||
@change="setDefaultModel(m.model)"
|
||||
>
|
||||
默认
|
||||
</Radio>
|
||||
<span class="min-w-0 flex-1 truncate font-medium">{{
|
||||
m.model
|
||||
}}</span>
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
danger
|
||||
@click="removeEnabledModel(m.model)"
|
||||
>
|
||||
移除
|
||||
</Button>
|
||||
</div>
|
||||
<Input
|
||||
v-model:value="m.contextWindow"
|
||||
size="small"
|
||||
placeholder="上下文窗口 token,留空=自动估计"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</FormItem>
|
||||
<Button type="primary" :loading="saving" @click="saveSettings">
|
||||
保存
|
||||
</Button>
|
||||
</Form>
|
||||
</Card>
|
||||
|
||||
<Card title="我的记忆" :bordered="false">
|
||||
<p class="text-muted-foreground mb-3 text-xs">
|
||||
对话中发送「记住:…」可写入;助手按需检索,不会默认注入全部记忆。
|
||||
</p>
|
||||
<div v-if="memories.length === 0" class="text-muted-foreground text-sm">
|
||||
暂无记忆
|
||||
</div>
|
||||
<div
|
||||
v-for="m in memories"
|
||||
:key="m.id"
|
||||
class="mb-2 flex items-start gap-2 rounded-md bg-muted/50 px-3 py-2 text-sm"
|
||||
>
|
||||
<span class="min-w-0 flex-1 break-words">{{
|
||||
m.summary || m.text
|
||||
}}</span>
|
||||
<Button type="link" size="small" danger @click="removeMemory(m.id)">
|
||||
删
|
||||
</Button>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
</Page>
|
||||
</template>
|
||||
@@ -41,6 +41,14 @@ describe('requestClient', () => {
|
||||
expect(response.data).toEqual(mockData);
|
||||
});
|
||||
|
||||
it('should successfully make a PATCH request', async () => {
|
||||
const patchData = { key: 'patchedValue' };
|
||||
const mockData = { data: 'patched response' };
|
||||
mock.onPatch('/test/patch', patchData).reply(200, mockData);
|
||||
const response = await requestClient.patch('/test/patch', patchData);
|
||||
expect(response.data).toEqual(mockData);
|
||||
});
|
||||
|
||||
it('should successfully make a DELETE request', async () => {
|
||||
const mockData = { data: 'delete response' };
|
||||
mock.onDelete('/test/delete').reply(200, mockData);
|
||||
@@ -94,6 +102,6 @@ describe('requestClient', () => {
|
||||
|
||||
const res = await requestClient.download('/test/download');
|
||||
|
||||
expect(res.data).toBeInstanceOf(Blob);
|
||||
expect(res).toBeInstanceOf(Blob);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -125,6 +125,17 @@ class RequestClient {
|
||||
return this.request<T>(url, { ...config, data, method: 'PUT' });
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH请求方法
|
||||
*/
|
||||
public patch<T = any>(
|
||||
url: string,
|
||||
data?: any,
|
||||
config?: RequestClientConfig,
|
||||
): Promise<T> {
|
||||
return this.request<T>(url, { ...config, data, method: 'PATCH' });
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用的请求方法
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user