mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 08:29:19 +08:00
feat: 公司 LLM 改为提供商凭证加多模型勾选并支持探测目录
This commit is contained in:
@@ -22,23 +22,34 @@ export namespace AgentVO {
|
||||
createdAt?: string;
|
||||
}
|
||||
|
||||
export interface LlmModelItem {
|
||||
model: string;
|
||||
label?: string | null;
|
||||
contextWindow?: number | null;
|
||||
isDefault?: boolean;
|
||||
}
|
||||
|
||||
export interface LlmSettings {
|
||||
configured: boolean;
|
||||
baseUrl?: string | null;
|
||||
model?: string | null;
|
||||
/** 上下文窗口 token;未配置则由 Agent 按模型名估计 */
|
||||
contextWindow?: number | null;
|
||||
displayName?: string | null;
|
||||
apiKeyMasked?: string | null;
|
||||
/** Server 字段;兼容旧 updatedAt */
|
||||
models?: LlmModelItem[];
|
||||
updateTime?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface ProbedLlmModel {
|
||||
id: string;
|
||||
contextWindow?: number | null;
|
||||
}
|
||||
|
||||
export interface Memory {
|
||||
id: string;
|
||||
kind: string;
|
||||
key?: string | null;
|
||||
text: string;
|
||||
summary?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
}
|
||||
@@ -72,10 +83,14 @@ export async function getAgentLlmSettingsApi() {
|
||||
|
||||
export async function updateAgentLlmSettingsApi(body: {
|
||||
baseUrl: string;
|
||||
model: string;
|
||||
apiKey?: string;
|
||||
/** 正整数写入;0 表示清空为自动估计 */
|
||||
contextWindow?: number | null;
|
||||
displayName?: string;
|
||||
models: Array<{
|
||||
model: string;
|
||||
label?: string;
|
||||
contextWindow?: number | null;
|
||||
isDefault?: boolean;
|
||||
}>;
|
||||
}) {
|
||||
return requestClient.put<AgentVO.LlmSettings>(
|
||||
'/company/agent-llm-settings',
|
||||
@@ -83,6 +98,18 @@ export async function updateAgentLlmSettingsApi(body: {
|
||||
);
|
||||
}
|
||||
|
||||
/** 服务端代理 GET /v1/models,不落库 */
|
||||
export async function probeAgentLlmModelsApi(body: {
|
||||
baseUrl: string;
|
||||
apiKey?: string;
|
||||
useStoredKey?: boolean;
|
||||
}) {
|
||||
return requestClient.post<{ items: AgentVO.ProbedLlmModel[] }>(
|
||||
'/company/agent-llm-settings/probe-models',
|
||||
body,
|
||||
);
|
||||
}
|
||||
|
||||
/** SSE 流式发消息 */
|
||||
export function streamAgentChat(options: {
|
||||
threadId: string;
|
||||
|
||||
@@ -6,6 +6,7 @@ export {
|
||||
listAgentMemoriesApi,
|
||||
listAgentMessagesApi,
|
||||
listAgentSessionsApi,
|
||||
probeAgentLlmModelsApi,
|
||||
streamAgentChat,
|
||||
updateAgentLlmSettingsApi,
|
||||
} from './apis';
|
||||
|
||||
@@ -6,12 +6,14 @@ import { PlusOutlined, SettingOutlined } from '@ant-design/icons-vue';
|
||||
|
||||
import {
|
||||
Button,
|
||||
Checkbox,
|
||||
Empty,
|
||||
Form,
|
||||
FormItem,
|
||||
Input,
|
||||
InputPassword,
|
||||
Modal,
|
||||
Radio,
|
||||
Spin,
|
||||
message,
|
||||
} from 'ant-design-vue';
|
||||
@@ -26,6 +28,7 @@ import {
|
||||
listAgentMemoriesApi,
|
||||
listAgentMessagesApi,
|
||||
listAgentSessionsApi,
|
||||
probeAgentLlmModelsApi,
|
||||
streamAgentChat,
|
||||
type AgentVO,
|
||||
updateAgentLlmSettingsApi,
|
||||
@@ -75,16 +78,24 @@ const draft = ref('');
|
||||
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: '',
|
||||
model: '',
|
||||
apiKey: '',
|
||||
apiKeyMasked: '' as string | null,
|
||||
configured: false,
|
||||
/** 空字符串=自动估计;数字字符串=显式窗口 */
|
||||
contextWindow: '' as string,
|
||||
displayName: '',
|
||||
});
|
||||
const enabledModels = ref<EnabledModelRow[]>([]);
|
||||
const messagePaneRef = ref<HTMLElement | null>(null);
|
||||
let abort: AbortController | null = null;
|
||||
let messagesLoadSeq = 0;
|
||||
@@ -230,6 +241,8 @@ async function ensureSession(): Promise<string | null> {
|
||||
|
||||
async function openSettings() {
|
||||
settingsOpen.value = true;
|
||||
probedModels.value = [];
|
||||
manualModelId.value = '';
|
||||
try {
|
||||
const [s, mem] = await Promise.all([
|
||||
getAgentLlmSettingsApi(),
|
||||
@@ -237,21 +250,119 @@ async function openSettings() {
|
||||
]);
|
||||
llmForm.value = {
|
||||
baseUrl: s.baseUrl || '',
|
||||
model: s.model || '',
|
||||
apiKey: '',
|
||||
apiKeyMasked: s.apiKeyMasked || null,
|
||||
configured: !!s.configured,
|
||||
contextWindow:
|
||||
s.contextWindow != null && s.contextWindow > 0
|
||||
? String(s.contextWindow)
|
||||
: '',
|
||||
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} 个模型`);
|
||||
}
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载模型列表失败,可改为手动添加');
|
||||
} finally {
|
||||
probingModels.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function removeMemory(id: string) {
|
||||
try {
|
||||
await deleteAgentMemoryApi(id);
|
||||
@@ -263,37 +374,58 @@ async function removeMemory(id: string) {
|
||||
}
|
||||
|
||||
async function saveSettings() {
|
||||
if (!llmForm.value.baseUrl.trim() || !llmForm.value.model.trim()) {
|
||||
message.warning('请填写 Base URL 与模型名');
|
||||
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 cwRaw = llmForm.value.contextWindow.trim();
|
||||
let contextWindow: number | undefined;
|
||||
if (cwRaw === '') {
|
||||
contextWindow = 0; // 清空为自动估计
|
||||
} else {
|
||||
const n = Number(cwRaw);
|
||||
if (!Number.isFinite(n) || n < 0) {
|
||||
message.warning('上下文窗口请填写正整数,或留空自动估计');
|
||||
return;
|
||||
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);
|
||||
}
|
||||
contextWindow = Math.floor(n);
|
||||
}
|
||||
return {
|
||||
model: m.model,
|
||||
contextWindow,
|
||||
isDefault: m.isDefault,
|
||||
};
|
||||
});
|
||||
const s = await updateAgentLlmSettingsApi({
|
||||
baseUrl: llmForm.value.baseUrl.trim(),
|
||||
model: llmForm.value.model.trim(),
|
||||
apiKey: llmForm.value.apiKey.trim() || undefined,
|
||||
contextWindow,
|
||||
displayName: llmForm.value.displayName.trim() || undefined,
|
||||
models,
|
||||
});
|
||||
llmForm.value.configured = s.configured;
|
||||
llmForm.value.apiKeyMasked = s.apiKeyMasked || null;
|
||||
llmForm.value.apiKey = '';
|
||||
llmForm.value.contextWindow =
|
||||
s.contextWindow != null && s.contextWindow > 0
|
||||
? String(s.contextWindow)
|
||||
: '';
|
||||
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) {
|
||||
@@ -588,14 +720,14 @@ void refreshSessions();
|
||||
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
|
||||
加密落库(MySQL),Agent 经内部接口/Redis 注入;界面仅显示掩码。
|
||||
上下文窗口多数厂商不会在 API 里返回,需按模型文档填写;留空则由
|
||||
Agent 按模型名估计。
|
||||
填写 Base URL 与 API Key 后加载模型列表并勾选启用;指定一个默认模型供对话使用。Key
|
||||
加密落库,界面仅显示掩码。上下文窗口多数厂商不会在 API 返回,有则预填,可改或留空由
|
||||
Agent 估计。
|
||||
</p>
|
||||
<Form layout="vertical">
|
||||
<FormItem label="Base URL" required>
|
||||
@@ -604,19 +736,10 @@ void refreshSessions();
|
||||
placeholder="https://api.deepseek.com/v1"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="Model" required>
|
||||
<Input v-model:value="llmForm.model" placeholder="deepseek-chat" />
|
||||
</FormItem>
|
||||
<FormItem label="上下文窗口(token)">
|
||||
<Input
|
||||
v-model:value="llmForm.contextWindow"
|
||||
placeholder="留空=按模型名自动估计,例如 128000"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem
|
||||
:label="
|
||||
llmForm.configured
|
||||
? `API Key(已配置 ${llmForm.apiKeyMasked || '****'},留空则不修改)`
|
||||
? `API Key(已配置 ${llmForm.apiKeyMasked || '****'},留空则用已存密钥探测/不改 Key)`
|
||||
: 'API Key'
|
||||
"
|
||||
:required="!llmForm.configured"
|
||||
@@ -626,6 +749,93 @@ void refreshSessions();
|
||||
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>
|
||||
@@ -640,7 +850,9 @@ void refreshSessions();
|
||||
: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.text }}</span>
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user