mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 14:29:19 +08:00
feat: 公司支持多模型提供商与会话消歧选型
This commit is contained in:
@@ -9,6 +9,7 @@ export namespace AgentVO {
|
||||
threadId: string;
|
||||
title: string;
|
||||
status: string;
|
||||
llmProviderId?: number | null;
|
||||
llmModel?: string | null;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
@@ -24,20 +25,22 @@ export namespace AgentVO {
|
||||
}
|
||||
|
||||
export interface LlmModelItem {
|
||||
providerId: number;
|
||||
providerDisplayName?: string | null;
|
||||
model: string;
|
||||
label?: string | null;
|
||||
contextWindow?: number | null;
|
||||
isDefault?: boolean;
|
||||
}
|
||||
|
||||
export interface LlmSettings {
|
||||
export interface LlmProvider {
|
||||
id: number;
|
||||
configured: boolean;
|
||||
baseUrl?: string | null;
|
||||
displayName?: string | null;
|
||||
apiKeyMasked?: string | null;
|
||||
models?: LlmModelItem[];
|
||||
updateTime?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface ProbedLlmModel {
|
||||
@@ -54,23 +57,26 @@ export namespace AgentVO {
|
||||
updatedAt?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export async function listAgentSessionsApi() {
|
||||
return requestClient.get<{ items: AgentVO.Session[] }>('/agent/sessions');
|
||||
}
|
||||
|
||||
export async function createAgentSessionApi(options?: {
|
||||
title?: string;
|
||||
llmProviderId?: number;
|
||||
llmModel?: string;
|
||||
}) {
|
||||
return requestClient.post<AgentVO.Session>('/agent/sessions', {
|
||||
title: options?.title,
|
||||
llmProviderId: options?.llmProviderId,
|
||||
llmModel: options?.llmModel,
|
||||
});
|
||||
}
|
||||
|
||||
export async function patchAgentSessionApi(
|
||||
threadId: string,
|
||||
body: { title?: string; llmModel?: string },
|
||||
body: { title?: string; llmProviderId?: number; llmModel?: string },
|
||||
) {
|
||||
return requestClient.patch<AgentVO.Session>(
|
||||
`/agent/sessions/${threadId}`,
|
||||
@@ -92,26 +98,51 @@ export async function deleteAgentMemoryApi(memoryId: string) {
|
||||
return requestClient.delete<{ ok: boolean }>(`/agent/memories/${memoryId}`);
|
||||
}
|
||||
|
||||
export async function getAgentLlmSettingsApi() {
|
||||
return requestClient.get<AgentVO.LlmSettings>(
|
||||
'/company/agent-llm-settings',
|
||||
export async function listAgentLlmProvidersApi() {
|
||||
return requestClient.get<{ items: AgentVO.LlmProvider[] }>(
|
||||
'/company/agent-llm-providers',
|
||||
);
|
||||
}
|
||||
|
||||
export async function updateAgentLlmSettingsApi(body: {
|
||||
export async function createAgentLlmProviderApi(body: {
|
||||
baseUrl: string;
|
||||
apiKey?: string;
|
||||
apiKey: string;
|
||||
displayName?: string;
|
||||
}) {
|
||||
return requestClient.post<AgentVO.LlmProvider>(
|
||||
'/company/agent-llm-providers',
|
||||
body,
|
||||
);
|
||||
}
|
||||
|
||||
export async function updateAgentLlmProviderApi(
|
||||
providerId: number,
|
||||
body: { baseUrl?: string; apiKey?: string; displayName?: string },
|
||||
) {
|
||||
return requestClient.put<AgentVO.LlmProvider>(
|
||||
`/company/agent-llm-providers/${providerId}`,
|
||||
body,
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteAgentLlmProviderApi(providerId: number) {
|
||||
return requestClient.delete<void>(
|
||||
`/company/agent-llm-providers/${providerId}`,
|
||||
);
|
||||
}
|
||||
|
||||
export async function upsertAgentLlmProviderModelsApi(
|
||||
providerId: number,
|
||||
models: Array<{
|
||||
model: string;
|
||||
label?: string;
|
||||
contextWindow?: number | null;
|
||||
isDefault?: boolean;
|
||||
}>;
|
||||
}) {
|
||||
return requestClient.put<AgentVO.LlmSettings>(
|
||||
'/company/agent-llm-settings',
|
||||
body,
|
||||
}>,
|
||||
) {
|
||||
return requestClient.put<AgentVO.LlmProvider>(
|
||||
`/company/agent-llm-providers/${providerId}/models`,
|
||||
{ models },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -124,9 +155,10 @@ export async function listAgentEnabledLlmModelsApi() {
|
||||
|
||||
/** 服务端代理 GET /v1/models,不落库 */
|
||||
export async function probeAgentLlmModelsApi(body: {
|
||||
baseUrl: string;
|
||||
baseUrl?: string;
|
||||
apiKey?: string;
|
||||
useStoredKey?: boolean;
|
||||
providerId?: number;
|
||||
}) {
|
||||
return requestClient.post<{ items: AgentVO.ProbedLlmModel[] }>(
|
||||
'/company/agent-llm-settings/probe-models',
|
||||
@@ -139,7 +171,6 @@ export function streamAgentChat(options: {
|
||||
threadId: string;
|
||||
content: string;
|
||||
onToken: (text: string) => void;
|
||||
/** 工具状态(不进正文);phase=start 显示 label,end 可清除 */
|
||||
onTool?: (payload: {
|
||||
phase: string;
|
||||
name?: string;
|
||||
@@ -230,7 +261,6 @@ export function streamAgentChat(options: {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
// 统一换行,按 SSE 事件块拆分
|
||||
buffer = buffer.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||
const parts = buffer.split('\n\n');
|
||||
buffer = parts.pop() || '';
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
export type { AgentVO } from './apis';
|
||||
export {
|
||||
createAgentLlmProviderApi,
|
||||
createAgentSessionApi,
|
||||
deleteAgentLlmProviderApi,
|
||||
deleteAgentMemoryApi,
|
||||
getAgentLlmSettingsApi,
|
||||
listAgentEnabledLlmModelsApi,
|
||||
listAgentLlmProvidersApi,
|
||||
listAgentMemoriesApi,
|
||||
listAgentMessagesApi,
|
||||
listAgentSessionsApi,
|
||||
patchAgentSessionApi,
|
||||
probeAgentLlmModelsApi,
|
||||
streamAgentChat,
|
||||
updateAgentLlmSettingsApi,
|
||||
updateAgentLlmProviderApi,
|
||||
upsertAgentLlmProviderModelsApi,
|
||||
} from './apis';
|
||||
|
||||
@@ -62,8 +62,8 @@ const draft = ref('');
|
||||
/** 本条消息附带的店铺/商品/SKU,发送时写成 [引用] 块 */
|
||||
const citations = ref<CatalogCite[]>([]);
|
||||
const enabledModels = ref<AgentVO.LlmModelItem[]>([]);
|
||||
/** 当前选中模型(有会话时与 session.llmModel 同步;草稿态本地暂存) */
|
||||
const selectedModel = ref<string | undefined>(undefined);
|
||||
/** 选中值:`${providerId}::${model}`;会话态与草稿共用 */
|
||||
const selectedModelKey = ref<string | undefined>(undefined);
|
||||
const patchingModel = ref(false);
|
||||
const messagePaneRef = ref<HTMLElement | null>(null);
|
||||
let abort: AbortController | null = null;
|
||||
@@ -73,12 +73,38 @@ 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,
|
||||
})),
|
||||
);
|
||||
function modelKey(providerId: number, model: string) {
|
||||
return `${providerId}::${model}`;
|
||||
}
|
||||
|
||||
function parseModelKey(key: string | undefined): {
|
||||
providerId: number;
|
||||
model: string;
|
||||
} | null {
|
||||
if (!key) return null;
|
||||
const idx = key.indexOf('::');
|
||||
if (idx <= 0) return null;
|
||||
const providerId = Number(key.slice(0, idx));
|
||||
const model = key.slice(idx + 2);
|
||||
if (!Number.isFinite(providerId) || !model) return null;
|
||||
return { providerId, model };
|
||||
}
|
||||
|
||||
const modelOptions = computed(() => {
|
||||
const groups = new Map<string, { label: string; options: Array<{ value: string; label: string }> }>();
|
||||
for (const m of enabledModels.value) {
|
||||
const gLabel = m.providerDisplayName || `提供商 #${m.providerId}`;
|
||||
const gKey = String(m.providerId);
|
||||
if (!groups.has(gKey)) {
|
||||
groups.set(gKey, { label: gLabel, options: [] });
|
||||
}
|
||||
groups.get(gKey)!.options.push({
|
||||
value: modelKey(m.providerId, m.model),
|
||||
label: m.label || m.model,
|
||||
});
|
||||
}
|
||||
return [...groups.values()];
|
||||
});
|
||||
|
||||
const showMessageLoading = computed(
|
||||
() => loadingMessages.value && !sending.value && messages.value.length === 0,
|
||||
@@ -127,19 +153,27 @@ function scrollChatToBottom() {
|
||||
el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
|
||||
function defaultModelId() {
|
||||
function defaultModelKey() {
|
||||
const d = enabledModels.value.find((m) => m.isDefault);
|
||||
return d?.model || enabledModels.value[0]?.model;
|
||||
const pick = d || enabledModels.value[0];
|
||||
return pick ? modelKey(pick.providerId, pick.model) : undefined;
|
||||
}
|
||||
|
||||
function syncSelectedFromSession() {
|
||||
const s = activeSession.value;
|
||||
if (s?.llmModel) {
|
||||
selectedModel.value = s.llmModel;
|
||||
if (s?.llmProviderId != null && s.llmModel) {
|
||||
selectedModelKey.value = modelKey(s.llmProviderId, s.llmModel);
|
||||
return;
|
||||
}
|
||||
if (!selectedModel.value) {
|
||||
selectedModel.value = defaultModelId();
|
||||
if (s?.llmModel) {
|
||||
const matches = enabledModels.value.filter((m) => m.model === s.llmModel);
|
||||
if (matches.length === 1) {
|
||||
selectedModelKey.value = modelKey(matches[0]!.providerId, matches[0]!.model);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!selectedModelKey.value) {
|
||||
selectedModelKey.value = defaultModelKey();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,8 +207,8 @@ async function loadEnabledModels() {
|
||||
try {
|
||||
const res = await listAgentEnabledLlmModelsApi();
|
||||
enabledModels.value = res.items || [];
|
||||
if (!selectedModel.value) {
|
||||
selectedModel.value = defaultModelId();
|
||||
if (!selectedModelKey.value) {
|
||||
selectedModelKey.value = defaultModelKey();
|
||||
}
|
||||
} catch {
|
||||
enabledModels.value = [];
|
||||
@@ -216,8 +250,8 @@ watch(activeThreadId, (id) => {
|
||||
} else {
|
||||
messages.value = [];
|
||||
streamingText.value = '';
|
||||
if (!selectedModel.value) {
|
||||
selectedModel.value = defaultModelId();
|
||||
if (!selectedModelKey.value) {
|
||||
selectedModelKey.value = defaultModelKey();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -237,31 +271,38 @@ function beginNewChat() {
|
||||
streamingText.value = '';
|
||||
draft.value = '';
|
||||
citations.value = [];
|
||||
selectedModel.value = defaultModelId();
|
||||
selectedModelKey.value = defaultModelKey();
|
||||
}
|
||||
|
||||
/** 第一条消息发出时才落库 */
|
||||
async function ensureSession(): Promise<string | null> {
|
||||
if (activeThreadId.value) return activeThreadId.value;
|
||||
const parsed = parseModelKey(selectedModelKey.value);
|
||||
const s = await createAgentSessionApi({
|
||||
llmModel: selectedModel.value,
|
||||
llmProviderId: parsed?.providerId,
|
||||
llmModel: parsed?.model,
|
||||
});
|
||||
composingNew.value = false;
|
||||
sessions.value = [s, ...sessions.value];
|
||||
activeThreadId.value = s.threadId;
|
||||
if (s.llmModel) {
|
||||
selectedModel.value = s.llmModel;
|
||||
if (s.llmProviderId != null && s.llmModel) {
|
||||
selectedModelKey.value = modelKey(s.llmProviderId, s.llmModel);
|
||||
}
|
||||
return s.threadId;
|
||||
}
|
||||
|
||||
async function onModelChange(model: string) {
|
||||
selectedModel.value = model;
|
||||
async function onModelChange(key: string) {
|
||||
if (!key || !key.includes('::')) return;
|
||||
selectedModelKey.value = key;
|
||||
const parsed = parseModelKey(key);
|
||||
const threadId = activeThreadId.value;
|
||||
if (!threadId || composingNew.value) return;
|
||||
if (!threadId || composingNew.value || !parsed) return;
|
||||
patchingModel.value = true;
|
||||
try {
|
||||
const s = await patchAgentSessionApi(threadId, { llmModel: model });
|
||||
const s = await patchAgentSessionApi(threadId, {
|
||||
llmProviderId: parsed.providerId,
|
||||
llmModel: parsed.model,
|
||||
});
|
||||
const idx = sessions.value.findIndex((x) => x.threadId === threadId);
|
||||
if (idx >= 0) {
|
||||
sessions.value[idx] = { ...sessions.value[idx]!, ...s };
|
||||
@@ -470,14 +511,14 @@ void loadEnabledModels().then(() => refreshSessions());
|
||||
}}
|
||||
</span>
|
||||
<Select
|
||||
:value="selectedModel"
|
||||
:value="selectedModelKey"
|
||||
:options="modelOptions"
|
||||
:loading="patchingModel"
|
||||
:disabled="sending || modelOptions.length === 0"
|
||||
:disabled="sending || enabledModels.length === 0"
|
||||
placeholder="选择模型"
|
||||
class="w-56 shrink-0"
|
||||
class="w-64 shrink-0"
|
||||
:allow-clear="false"
|
||||
@change="(v: string) => onModelChange(v)"
|
||||
@change="(v) => onModelChange(String(v ?? ''))"
|
||||
/>
|
||||
</header>
|
||||
|
||||
|
||||
@@ -11,182 +11,223 @@ import {
|
||||
FormItem,
|
||||
Input,
|
||||
InputPassword,
|
||||
Modal,
|
||||
Popconfirm,
|
||||
Radio,
|
||||
Spin,
|
||||
message,
|
||||
} from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
getAgentLlmSettingsApi,
|
||||
createAgentLlmProviderApi,
|
||||
deleteAgentLlmProviderApi,
|
||||
listAgentLlmProvidersApi,
|
||||
probeAgentLlmModelsApi,
|
||||
type AgentVO,
|
||||
updateAgentLlmSettingsApi,
|
||||
updateAgentLlmProviderApi,
|
||||
upsertAgentLlmProviderModelsApi,
|
||||
} from '#/api/agent';
|
||||
|
||||
defineOptions({ name: 'AgentLlmSettings' });
|
||||
|
||||
const loading = ref(true);
|
||||
const saving = ref(false);
|
||||
const probingModels = ref(false);
|
||||
const probedModels = ref<AgentVO.ProbedLlmModel[]>([]);
|
||||
const manualModelId = ref('');
|
||||
|
||||
type EnabledModelRow = {
|
||||
model: string;
|
||||
contextWindow: string;
|
||||
isDefault: boolean;
|
||||
};
|
||||
|
||||
const llmForm = ref({
|
||||
type ProviderDraft = {
|
||||
id: number;
|
||||
baseUrl: string;
|
||||
apiKey: string;
|
||||
apiKeyMasked: string | null;
|
||||
displayName: string;
|
||||
enabledModels: EnabledModelRow[];
|
||||
probedModels: AgentVO.ProbedLlmModel[];
|
||||
manualModelId: string;
|
||||
probing: boolean;
|
||||
savingCreds: boolean;
|
||||
savingModels: boolean;
|
||||
};
|
||||
|
||||
const loading = ref(true);
|
||||
const providers = ref<ProviderDraft[]>([]);
|
||||
const createOpen = ref(false);
|
||||
const creating = ref(false);
|
||||
const createForm = 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 = await getAgentLlmSettingsApi();
|
||||
llmForm.value = {
|
||||
baseUrl: s.baseUrl || '',
|
||||
apiKey: '',
|
||||
apiKeyMasked: s.apiKeyMasked || null,
|
||||
configured: !!s.configured,
|
||||
displayName: s.displayName || '',
|
||||
};
|
||||
enabledModels.value = (s.models || []).map((m) => ({
|
||||
function toDraft(p: AgentVO.LlmProvider): ProviderDraft {
|
||||
return {
|
||||
id: p.id,
|
||||
baseUrl: p.baseUrl || '',
|
||||
apiKey: '',
|
||||
apiKeyMasked: p.apiKeyMasked || null,
|
||||
displayName: p.displayName || '',
|
||||
enabledModels: (p.models || []).map((m) => ({
|
||||
model: m.model,
|
||||
contextWindow:
|
||||
m.contextWindow != null && m.contextWindow > 0
|
||||
? String(m.contextWindow)
|
||||
: '',
|
||||
isDefault: !!m.isDefault,
|
||||
}));
|
||||
})),
|
||||
probedModels: [],
|
||||
manualModelId: '',
|
||||
probing: false,
|
||||
savingCreds: false,
|
||||
savingModels: false,
|
||||
};
|
||||
}
|
||||
|
||||
async function loadAll() {
|
||||
loading.value = true;
|
||||
try {
|
||||
const res = await listAgentLlmProvidersApi();
|
||||
providers.value = (res.items || []).map(toDraft);
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载模型配置失败');
|
||||
message.error(e?.message || '加载提供商失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function isModelEnabled(id: string) {
|
||||
return enabledModels.value.some((m) => m.model === id);
|
||||
function isModelEnabled(d: ProviderDraft, id: string) {
|
||||
return d.enabledModels.some((m) => m.model === id);
|
||||
}
|
||||
|
||||
function toggleProbedModel(p: AgentVO.ProbedLlmModel, checked: boolean) {
|
||||
function toggleProbedModel(
|
||||
d: ProviderDraft,
|
||||
p: AgentVO.ProbedLlmModel,
|
||||
checked: boolean,
|
||||
) {
|
||||
if (checked) {
|
||||
if (isModelEnabled(p.id)) return;
|
||||
enabledModels.value.push({
|
||||
if (isModelEnabled(d, p.id)) return;
|
||||
d.enabledModels.push({
|
||||
model: p.id,
|
||||
contextWindow:
|
||||
p.contextWindow != null && p.contextWindow > 0
|
||||
? String(p.contextWindow)
|
||||
: '',
|
||||
isDefault: enabledModels.value.length === 0,
|
||||
isDefault: false,
|
||||
});
|
||||
} 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;
|
||||
d.enabledModels = d.enabledModels.filter((m) => m.model !== p.id);
|
||||
}
|
||||
}
|
||||
|
||||
function setDefaultModel(d: ProviderDraft, modelId: string) {
|
||||
for (const draft of providers.value) {
|
||||
draft.enabledModels = draft.enabledModels.map((m) => ({
|
||||
...m,
|
||||
isDefault: draft.id === d.id && m.model === modelId,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
function removeEnabledModel(d: ProviderDraft, modelId: string) {
|
||||
const wasDefault = d.enabledModels.find((m) => m.model === modelId)?.isDefault;
|
||||
d.enabledModels = d.enabledModels.filter((m) => m.model !== modelId);
|
||||
if (!wasDefault) return;
|
||||
for (const draft of providers.value) {
|
||||
for (const m of draft.enabledModels) {
|
||||
m.isDefault = false;
|
||||
}
|
||||
}
|
||||
for (const draft of providers.value) {
|
||||
if (draft.enabledModels.length > 0) {
|
||||
draft.enabledModels[0]!.isDefault = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
function addManualModel(d: ProviderDraft) {
|
||||
const id = d.manualModelId.trim();
|
||||
if (!id) {
|
||||
message.warning('请输入模型 id');
|
||||
return;
|
||||
}
|
||||
if (isModelEnabled(id)) {
|
||||
if (isModelEnabled(d, id)) {
|
||||
message.warning('该模型已在启用列表中');
|
||||
return;
|
||||
}
|
||||
enabledModels.value.push({
|
||||
d.enabledModels.push({
|
||||
model: id,
|
||||
contextWindow: '',
|
||||
isDefault: enabledModels.value.length === 0,
|
||||
isDefault: false,
|
||||
});
|
||||
manualModelId.value = '';
|
||||
d.manualModelId = '';
|
||||
}
|
||||
|
||||
async function probeModels() {
|
||||
if (!llmForm.value.baseUrl.trim()) {
|
||||
async function probeModels(d: ProviderDraft) {
|
||||
if (!d.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()) {
|
||||
!!d.apiKeyMasked &&
|
||||
(!d.apiKey.trim() || d.apiKey.trim().startsWith('****'));
|
||||
if (!useStored && !d.apiKey.trim()) {
|
||||
message.warning('请填写 API Key,或先保存密钥后再用已存密钥探测');
|
||||
return;
|
||||
}
|
||||
probingModels.value = true;
|
||||
d.probing = true;
|
||||
try {
|
||||
const res = await probeAgentLlmModelsApi({
|
||||
baseUrl: llmForm.value.baseUrl.trim(),
|
||||
apiKey: useStored ? undefined : llmForm.value.apiKey.trim(),
|
||||
baseUrl: d.baseUrl.trim(),
|
||||
apiKey: useStored ? undefined : d.apiKey.trim(),
|
||||
useStoredKey: useStored,
|
||||
providerId: useStored ? d.id : undefined,
|
||||
});
|
||||
probedModels.value = res.items || [];
|
||||
if (probedModels.value.length === 0) {
|
||||
d.probedModels = res.items || [];
|
||||
if (d.probedModels.length === 0) {
|
||||
message.warning('未返回模型,可手动添加模型 id');
|
||||
} else {
|
||||
message.success(`已加载 ${probedModels.value.length} 个模型`);
|
||||
message.success(`已加载 ${d.probedModels.length} 个模型`);
|
||||
}
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '加载模型列表失败,可改为手动添加');
|
||||
message.error(e?.message || '加载模型列表失败');
|
||||
} finally {
|
||||
probingModels.value = false;
|
||||
d.probing = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function saveSettings() {
|
||||
if (!llmForm.value.baseUrl.trim()) {
|
||||
async function saveCredentials(d: ProviderDraft) {
|
||||
if (!d.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;
|
||||
d.savingCreds = true;
|
||||
try {
|
||||
const models = enabledModels.value.map((m) => {
|
||||
const p = await updateAgentLlmProviderApi(d.id, {
|
||||
baseUrl: d.baseUrl.trim(),
|
||||
apiKey: d.apiKey.trim() || undefined,
|
||||
displayName: d.displayName.trim(),
|
||||
});
|
||||
d.apiKeyMasked = p.apiKeyMasked || null;
|
||||
d.apiKey = '';
|
||||
d.baseUrl = p.baseUrl || d.baseUrl;
|
||||
d.displayName = p.displayName || '';
|
||||
message.success('凭证已保存');
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '保存凭证失败');
|
||||
} finally {
|
||||
d.savingCreds = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function saveModels(d: ProviderDraft) {
|
||||
if (d.enabledModels.length === 0) {
|
||||
message.warning('请至少启用一个模型');
|
||||
return;
|
||||
}
|
||||
d.savingModels = true;
|
||||
try {
|
||||
const models = d.enabledModels.map((m) => {
|
||||
const cw = m.contextWindow.trim();
|
||||
let contextWindow: number | null | undefined;
|
||||
if (cw === '') {
|
||||
@@ -204,28 +245,51 @@ async function saveSettings() {
|
||||
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('模型配置已保存');
|
||||
const p = await upsertAgentLlmProviderModelsApi(d.id, models);
|
||||
// 刷新全部,保证公司级默认只剩一条
|
||||
await loadAll();
|
||||
message.success(`「${p.displayName || p.baseUrl}」模型列表已保存`);
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '保存失败');
|
||||
message.error(e?.message || '保存模型失败');
|
||||
} finally {
|
||||
saving.value = false;
|
||||
d.savingModels = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function removeProvider(id: number) {
|
||||
try {
|
||||
await deleteAgentLlmProviderApi(id);
|
||||
providers.value = providers.value.filter((p) => p.id !== id);
|
||||
message.success('已删除提供商');
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '删除失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function submitCreate() {
|
||||
if (!createForm.value.baseUrl.trim()) {
|
||||
message.warning('请填写 Base URL');
|
||||
return;
|
||||
}
|
||||
if (!createForm.value.apiKey.trim()) {
|
||||
message.warning('请填写 API Key');
|
||||
return;
|
||||
}
|
||||
creating.value = true;
|
||||
try {
|
||||
const p = await createAgentLlmProviderApi({
|
||||
baseUrl: createForm.value.baseUrl.trim(),
|
||||
apiKey: createForm.value.apiKey.trim(),
|
||||
displayName: createForm.value.displayName.trim() || undefined,
|
||||
});
|
||||
providers.value.push(toDraft(p));
|
||||
createOpen.value = false;
|
||||
createForm.value = { baseUrl: '', apiKey: '', displayName: '' };
|
||||
message.success('提供商已创建,请继续勾选启用模型');
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '创建失败');
|
||||
} finally {
|
||||
creating.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,98 +308,122 @@ onMounted(() => {
|
||||
<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>
|
||||
<div class="flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<div class="text-base font-medium">模型提供商</div>
|
||||
<p class="text-muted-foreground mt-1 text-sm">
|
||||
可配置多个 OpenAI Compatible 提供商;跨提供商允许同名模型。公司全局仅一个默认模型。
|
||||
</p>
|
||||
</div>
|
||||
<Button type="primary" @click="createOpen = true">添加提供商</Button>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="!loading && providers.length === 0"
|
||||
class="text-muted-foreground rounded-lg border border-dashed border-border px-4 py-10 text-center text-sm"
|
||||
>
|
||||
尚未配置提供商,点击右上角添加。
|
||||
</div>
|
||||
|
||||
<Card
|
||||
v-for="d in providers"
|
||||
:key="d.id"
|
||||
:title="d.displayName || d.baseUrl || `提供商 #${d.id}`"
|
||||
:bordered="false"
|
||||
>
|
||||
<template #extra>
|
||||
<Popconfirm
|
||||
title="删除该提供商及其启用模型?"
|
||||
ok-text="删除"
|
||||
cancel-text="取消"
|
||||
@confirm="removeProvider(d.id)"
|
||||
>
|
||||
<Button type="link" danger size="small">删除</Button>
|
||||
</Popconfirm>
|
||||
</template>
|
||||
|
||||
<Form layout="vertical">
|
||||
<FormItem label="Base URL" required>
|
||||
<Input
|
||||
v-model:value="llmForm.baseUrl"
|
||||
v-model:value="d.baseUrl"
|
||||
placeholder="https://api.deepseek.com/v1"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem
|
||||
:label="
|
||||
llmForm.configured
|
||||
? `API Key(已配置 ${llmForm.apiKeyMasked || '****'},留空则用已存密钥探测/不改 Key)`
|
||||
d.apiKeyMasked
|
||||
? `API Key(已配置 ${d.apiKeyMasked},留空不改)`
|
||||
: 'API Key'
|
||||
"
|
||||
:required="!llmForm.configured"
|
||||
>
|
||||
<InputPassword
|
||||
v-model:value="llmForm.apiKey"
|
||||
placeholder="sk-..."
|
||||
/>
|
||||
<InputPassword v-model:value="d.apiKey" placeholder="sk-..." />
|
||||
</FormItem>
|
||||
<FormItem label="提供商备注(可选)">
|
||||
<Input
|
||||
v-model:value="llmForm.displayName"
|
||||
placeholder="如 DeepSeek"
|
||||
/>
|
||||
<Input v-model:value="d.displayName" placeholder="如 DeepSeek" />
|
||||
</FormItem>
|
||||
<Button
|
||||
type="default"
|
||||
class="mb-4"
|
||||
:loading="d.savingCreds"
|
||||
@click="saveCredentials(d)"
|
||||
>
|
||||
保存凭证
|
||||
</Button>
|
||||
|
||||
<FormItem label="模型列表">
|
||||
<div class="mb-2 flex flex-wrap gap-2">
|
||||
<Button
|
||||
type="primary"
|
||||
ghost
|
||||
:loading="probingModels"
|
||||
@click="probeModels"
|
||||
:loading="d.probing"
|
||||
@click="probeModels(d)"
|
||||
>
|
||||
加载模型列表
|
||||
</Button>
|
||||
</div>
|
||||
<div
|
||||
v-if="probedModels.length > 0"
|
||||
v-if="d.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"
|
||||
v-for="p in d.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)"
|
||||
:checked="isModelEnabled(d, p.id)"
|
||||
@update:checked="(v: boolean) => toggleProbedModel(d, 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"
|
||||
v-model:value="d.manualModelId"
|
||||
class="flex-1"
|
||||
placeholder="手动添加模型 id,如 deepseek-chat"
|
||||
@press-enter="addManualModel"
|
||||
placeholder="手动添加模型 id"
|
||||
@press-enter="addManualModel(d)"
|
||||
/>
|
||||
<Button @click="addManualModel">添加</Button>
|
||||
<Button @click="addManualModel(d)">添加</Button>
|
||||
</div>
|
||||
<div
|
||||
v-if="enabledModels.length === 0"
|
||||
v-if="d.enabledModels.length === 0"
|
||||
class="text-muted-foreground text-xs"
|
||||
>
|
||||
尚未启用模型。请加载列表勾选,或手动添加。
|
||||
尚未启用模型。勾选或手动添加后保存。
|
||||
</div>
|
||||
<div v-else class="space-y-2">
|
||||
<div
|
||||
v-for="m in enabledModels"
|
||||
v-for="m in d.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)"
|
||||
@change="setDefaultModel(d, m.model)"
|
||||
>
|
||||
默认
|
||||
公司默认
|
||||
</Radio>
|
||||
<span class="min-w-0 flex-1 truncate font-medium">{{
|
||||
m.model
|
||||
@@ -344,7 +432,7 @@ onMounted(() => {
|
||||
type="link"
|
||||
size="small"
|
||||
danger
|
||||
@click="removeEnabledModel(m.model)"
|
||||
@click="removeEnabledModel(d, m.model)"
|
||||
>
|
||||
移除
|
||||
</Button>
|
||||
@@ -357,11 +445,45 @@ onMounted(() => {
|
||||
</div>
|
||||
</div>
|
||||
</FormItem>
|
||||
<Button type="primary" :loading="saving" @click="saveSettings">
|
||||
保存
|
||||
<Button
|
||||
type="primary"
|
||||
:loading="d.savingModels"
|
||||
@click="saveModels(d)"
|
||||
>
|
||||
保存启用模型
|
||||
</Button>
|
||||
</Form>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<Modal
|
||||
v-model:open="createOpen"
|
||||
title="添加提供商"
|
||||
:confirm-loading="creating"
|
||||
ok-text="创建"
|
||||
destroy-on-close
|
||||
@ok="submitCreate"
|
||||
>
|
||||
<Form layout="vertical" class="mt-2">
|
||||
<FormItem label="Base URL" required>
|
||||
<Input
|
||||
v-model:value="createForm.baseUrl"
|
||||
placeholder="https://api.deepseek.com/v1"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="API Key" required>
|
||||
<InputPassword
|
||||
v-model:value="createForm.apiKey"
|
||||
placeholder="sk-..."
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="备注(可选)">
|
||||
<Input
|
||||
v-model:value="createForm.displayName"
|
||||
placeholder="如 DeepSeek"
|
||||
/>
|
||||
</FormItem>
|
||||
</Form>
|
||||
</Modal>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user