-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathModelAddDialog.tsx
More file actions
1521 lines (1439 loc) · 51.3 KB
/
ModelAddDialog.tsx
File metadata and controls
1521 lines (1439 loc) · 51.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { useMemo, useState, useCallback, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Modal, Select, Input, Button, Switch, Tooltip, App } from "antd";
import { InfoCircleFilled } from "@ant-design/icons";
import {
LoaderCircle,
ChevronRight,
ChevronDown,
Settings,
} from "lucide-react";
import { useConfig } from "@/hooks/useConfig";
import { getConnectivityMeta, ConnectivityStatusType } from "@/lib/utils";
import { modelService } from "@/services/modelService";
import { ModelType, SingleModelConfig } from "@/types/modelConfig";
import { MODEL_TYPES, PROVIDER_LINKS } from "@/const/modelConfig";
import { useSiliconModelList } from "@/hooks/model/useSiliconModelList";
import { useDashscopeModelList } from "@/hooks/model/useDashscopeModelList";
import { useTokenPonyModelList } from "@/hooks/model/useTokenponyModelList";
import { useMinimaxModelList } from "@/hooks/model/useMinimaxModelList";
import log from "@/lib/logger";
import {
ModelChunkSizeSlider,
DEFAULT_EXPECTED_CHUNK_SIZE,
DEFAULT_MAXIMUM_CHUNK_SIZE,
} from "./ModelChunkSizeSilder";
const { Option } = Select;
// Define the return type after adding a model
export interface AddedModel {
name: string;
type: ModelType;
}
interface ModelAddDialogProps {
isOpen: boolean;
onClose: () => void;
onSuccess: (model?: AddedModel) => Promise<void>;
defaultProvider?: string; // Default provider to select when dialog opens
defaultIsBatchImport?: boolean;
tenantId?: string; // Optional tenant ID for manage operations
}
// Default form state for resetting
const DEFAULT_FORM_STATE = {
type: MODEL_TYPES.LLM as ModelType,
name: "",
displayName: "",
url: "",
apiKey: "",
maxTokens: "4096",
isMultimodal: false,
isBatchImport: false,
provider: "modelengine",
modelEngineUrl: "",
vectorDimension: "1024",
chunkSizeRange: [DEFAULT_EXPECTED_CHUNK_SIZE, DEFAULT_MAXIMUM_CHUNK_SIZE] as [
number,
number,
],
chunkingBatchSize: "10",
};
// Connectivity status type comes from utils
// Helper function to translate error messages from backend
const translateError = (
errorMessage: string,
t: (key: string, params?: any) => string
): string => {
if (!errorMessage) return errorMessage;
const errorLower = errorMessage.toLowerCase();
// Extract model name from patterns like "Name 'xxx' is already in use"
// Matches: "Name 'xxx' is already in use" or "Name xxx is already in use"
const nameMatch = errorMessage.match(
/Name\s+(?:['"]([^'"]+)['"]|([^\s,]+))\s+is already in use/i
);
if (nameMatch) {
const modelName = nameMatch[1] || nameMatch[2];
return t("model.dialog.error.nameAlreadyInUse", { name: modelName });
}
// Model not found pattern
if (
errorLower.includes("model not found") ||
errorLower.includes("not found")
) {
const modelNameMatch = errorMessage.match(
/(?:Model not found|not found)[:\s]+([^\s,]+)/i
);
if (modelNameMatch) {
return t("model.dialog.error.modelNotFound", { name: modelNameMatch[1] });
}
return t("model.dialog.error.modelNotFound", { name: "" });
}
// Unsupported model type
if (errorLower.includes("unsupported model type")) {
const typeMatch = errorMessage.match(
/unsupported model type[:\s]+([^\s,]+)/i
);
if (typeMatch) {
return t("model.dialog.error.unsupportedModelType", {
type: typeMatch[1],
});
}
return t("model.dialog.error.unsupportedModelType", { type: "unknown" });
}
// Connection failed patterns - extract model name and URL from backend error
if (
errorLower.includes("failed to connect") ||
errorLower.includes("connection failed") ||
errorLower.includes("connection error") ||
errorLower.includes("unable to connect")
) {
// Try to extract model name and URL from pattern: "Failed to connect to model 'xxx' at https://..."
// Match URL that may end with period before the next sentence (e.g., "https://api.example.com. Please verify...")
// Match URL pattern: http:// or https:// followed by domain (may contain dots) and optional path
// Example: "Failed to connect to model 'qwen-plus' at https://api.siliconflow.cn. Please verify..."
const connectMatch = errorMessage.match(
/Failed to connect to model\s+['"]([^'"]+)['"]\s+at\s+(https?:\/\/[^\s]+?)(?:\.\s|\.$|$)/i
);
if (connectMatch) {
// Remove trailing period if present (URL might end with period before next sentence)
let url = connectMatch[2].replace(/\.$/, "");
// Return fully translated message with model name and URL
return t("model.dialog.error.failedToConnect", {
modelName: connectMatch[1],
url: url,
});
}
// Fallback: return original error message (will be wrapped by connectivityFailed)
return errorMessage;
}
// Invalid configuration
if (errorLower.includes("invalid") && errorLower.includes("config")) {
// Extract the actual error description
const configError =
errorMessage.replace(/^.*?invalid[^:]*:?\s*/i, "").trim() || errorMessage;
return t("model.dialog.error.invalidConfiguration", { error: configError });
}
// ModelEngine specific errors
if (
errorLower.includes("authentication failed") ||
errorLower.includes("invalid api key")
) {
return t("model.dialog.error.apiConnectionFailed");
}
if (
errorLower.includes("access forbidden") ||
errorLower.includes("insufficient permissions")
) {
return t("model.dialog.error.apiConnectionFailed");
}
if (
errorLower.includes("endpoint not found") ||
errorLower.includes("url may be incorrect")
) {
return t("model.dialog.error.apiConnectionFailed");
}
if (errorLower.includes("server error") || errorLower.includes("http 5")) {
return t("model.dialog.error.serverError");
}
if (
errorLower.includes("connection failed") ||
errorLower.includes("network") ||
errorLower.includes("timeout")
) {
return t("model.dialog.error.apiConnectionFailed");
}
if (errorLower.includes("ssl certificate")) {
return t("model.dialog.error.apiConnectionFailed");
}
// Return original error if no pattern matches
return errorMessage;
};
export const ModelAddDialog = ({
isOpen,
onClose,
onSuccess,
defaultProvider,
defaultIsBatchImport,
tenantId,
}: ModelAddDialogProps) => {
const { t } = useTranslation();
const { message } = App.useApp();
const { updateModelConfig, saveConfig } = useConfig();
// Parse backend error message and return i18n key with params
const parseModelError = (
errorMessage: string
): { key: string; params?: Record<string, string> } => {
if (!errorMessage) {
return { key: "model.dialog.error.addFailed" };
}
// Check for name conflict error
const nameConflictMatch = errorMessage.match(
/Name ['"]?([^'"]+)['"]? is already in use/i
);
if (nameConflictMatch) {
return {
key: "model.dialog.error.nameConflict",
params: { name: nameConflictMatch[1] },
};
}
// For other errors, return generic error key without showing backend details
return { key: "model.dialog.error.addFailed" };
};
// Form state - initialize with default values
const [form, setForm] = useState(DEFAULT_FORM_STATE);
const [loading, setLoading] = useState(false);
const [verifyingConnectivity, setVerifyingConnectivity] = useState(false);
const [connectivityStatus, setConnectivityStatus] = useState<{
status: ConnectivityStatusType;
message: string;
}>({
status: null,
message: "",
});
const [modelList, setModelList] = useState<any[]>([]);
const [modelSearchTerm, setModelSearchTerm] = useState("");
const [selectedModelIds, setSelectedModelIds] = useState<Set<string>>(
new Set()
);
const [showModelList, setShowModelList] = useState(false);
const [loadingModelList, setLoadingModelList] = useState(false);
const persistModelConfig = useCallback(async () => {
const ok = await saveConfig();
if (!ok) {
message.error(t("setup.page.error.saveConfig"));
}
}, [saveConfig, message, t]);
// Settings modal state
const [settingsModalVisible, setSettingsModalVisible] = useState(false);
const [selectedModelForSettings, setSelectedModelForSettings] =
useState<any>(null);
const [modelMaxTokens, setModelMaxTokens] = useState("4096");
// Use the silicon model list hook
const siliconHook = useSiliconModelList({
form,
setModelList,
setSelectedModelIds,
setShowModelList,
setLoadingModelList,
tenantId,
});
const dashscopeHook = useDashscopeModelList({
form,
setModelList,
setSelectedModelIds,
setShowModelList,
setLoadingModelList,
tenantId,
});
const tokenponyHook = useTokenPonyModelList({
form,
setModelList,
setSelectedModelIds,
setShowModelList,
setLoadingModelList,
tenantId,
});
const minimaxHook = useMinimaxModelList({
form,
setModelList,
setSelectedModelIds,
setShowModelList,
setLoadingModelList,
tenantId,
});
let getModelList;
let getProviderSelectedModalList;
// 2. 根据条件赋值
if (form.provider === "silicon") {
({ getModelList, getProviderSelectedModalList } = siliconHook);
} else if (form.provider === "dashscope") {
({ getModelList, getProviderSelectedModalList } = dashscopeHook);
} else if (form.provider === "tokenpony") {
({ getModelList, getProviderSelectedModalList } = tokenponyHook);
} else if (form.provider === "minimax") {
({ getModelList, getProviderSelectedModalList } = minimaxHook);
}
// Reset form to default state
const resetForm = useCallback(() => {
setForm(DEFAULT_FORM_STATE);
setConnectivityStatus({ status: null, message: "" });
setModelList([]);
setModelSearchTerm("");
setSelectedModelIds(new Set());
setShowModelList(false);
}, []);
// Wrap onClose to reset form before closing
const handleClose = useCallback(() => {
resetForm();
onClose();
}, [onClose, resetForm]);
// When dialog opens, apply default provider and optional default batch mode
useEffect(() => {
if (!isOpen) return;
setForm((prev) => ({
...prev,
provider: defaultProvider || prev.provider,
isBatchImport:
typeof defaultIsBatchImport !== "undefined"
? Boolean(defaultIsBatchImport)
: prev.isBatchImport,
}));
}, [isOpen, defaultProvider, defaultIsBatchImport]);
const parseModelName = (name: string): string => {
if (!name) return "";
const parts = name.split("/");
if (parts.length <= 2) {
return parts[parts.length - 1];
} else {
return `${parts[0]}/${parts[parts.length - 1]}`;
}
};
const filteredModelList = useMemo(() => {
const keyword = modelSearchTerm.trim().toLowerCase();
if (!keyword) {
return modelList;
}
return modelList.filter((model: any) => {
const candidates = [
model.id,
model.model_name,
model.model_tag,
model.description,
];
return candidates.some(
(text) =>
typeof text === "string" && text.toLowerCase().includes(keyword)
);
});
}, [modelList, modelSearchTerm]);
// Handle model name change, automatically update the display name
const handleModelNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const name = e.target.value;
setForm((prev) => ({
...prev,
name,
// If the display name is the same as the parsed result of the model name, it means the user has not manually modified the display name
// At this time, the display name should be automatically updated
displayName:
prev.displayName === parseModelName(prev.name)
? parseModelName(name)
: prev.displayName,
}));
// Clear the previous verification status
setConnectivityStatus({ status: null, message: "" });
};
// Handle form change
const handleFormChange = (field: string, value: string | boolean) => {
setForm((prev) => ({
...prev,
[field]: value,
// When provider changes, clear provider-related fields
...(field === "provider"
? {
url: "",
apiKey: "",
modelEngineUrl: "",
}
: {}),
}));
// If the key configuration item changes, clear the verification status
if (
["type", "url", "apiKey", "maxTokens", "vectorDimension"].includes(
field
) ||
field === "provider"
) {
setConnectivityStatus({ status: null, message: "" });
}
// Clear model search term when model type changes
if (field === "type") {
setModelSearchTerm("");
}
// Clear model list when provider changes
if (field === "provider") {
setModelList([]);
setSelectedModelIds(new Set());
}
};
// Verify if the vector dimension is valid
const isValidVectorDimension = (value: string): boolean => {
const dimension = parseInt(value);
return !isNaN(dimension) && dimension > 0;
};
// Check if the form is valid
const isFormValid = () => {
if (form.isBatchImport) {
// If provider is ModelEngine, require the ModelEngine URL as well.
if (form.provider === "modelengine") {
return (
form.provider.trim() !== "" &&
form.apiKey.trim() !== "" &&
((form as any).modelEngineUrl || "").toString().trim() !== ""
);
}
return form.provider.trim() !== "" && form.apiKey.trim() !== "";
}
if (form.type === MODEL_TYPES.EMBEDDING) {
return (
form.name.trim() !== "" &&
form.url.trim() !== "" &&
isValidVectorDimension(form.vectorDimension)
);
}
return (
form.name.trim() !== "" &&
form.url.trim() !== "" &&
form.maxTokens.trim() !== ""
);
};
// Verify model connectivity
const handleVerifyConnectivity = async () => {
if (!isFormValid()) {
message.warning(t("model.dialog.warning.incompleteForm"));
return;
}
setVerifyingConnectivity(true);
setConnectivityStatus({
status: "checking",
message: t("model.dialog.status.verifying"),
});
try {
const modelType =
form.type === MODEL_TYPES.EMBEDDING && form.isMultimodal
? (MODEL_TYPES.MULTI_EMBEDDING as ModelType)
: form.type;
// Use manage interface if tenantId is provided
if (tenantId) {
// Call backend healthcheck API for tenant management
const result = await modelService.checkManageTenantModelConnectivity(
tenantId,
form.displayName || form.name
);
// Set connectivity status
if (result) {
setConnectivityStatus({
status: "available",
message: t("model.dialog.connectivity.status.available"),
});
} else {
setConnectivityStatus({
status: "unavailable",
message: t("model.dialog.connectivity.status.unavailable"),
});
}
} else {
// Use local config verification for non-tenant operations
const config = {
modelName: form.name,
modelType: modelType,
baseUrl: form.url,
apiKey: form.apiKey.trim() === "" ? "sk-no-api-key" : form.apiKey,
maxTokens:
form.type === MODEL_TYPES.EMBEDDING
? parseInt(form.vectorDimension)
: parseInt(form.maxTokens),
embeddingDim:
form.type === MODEL_TYPES.EMBEDDING
? parseInt(form.vectorDimension)
: undefined,
};
const result = await modelService.verifyModelConfigConnectivity(config);
// Set connectivity status
if (result.connectivity) {
setConnectivityStatus({
status: "available",
message: t("model.dialog.connectivity.status.available"),
});
} else {
// Set status to unavailable
setConnectivityStatus({
status: "unavailable",
message: t("model.dialog.connectivity.status.unavailable"),
});
// Show detailed error message using internationalized component (same as add failure)
if (result.error) {
const translatedError = translateError(result.error, t);
// Ensure translatedError is a valid string, fallback to original error if needed
const errorText =
translatedError && translatedError.length > 0
? translatedError
: result.error || "Unknown error";
message.error(
t("model.dialog.error.connectivityFailed", { error: errorText })
);
}
}
}
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
setConnectivityStatus({
status: "unavailable",
message: t("model.dialog.connectivity.status.unavailable"),
});
// Show error message using internationalized component (same as add failure)
const translatedError = translateError(
errorMessage || t("model.dialog.connectivity.status.unavailable"),
t
);
// Ensure translatedError is a valid string
const errorText = translatedError
? translatedError
: errorMessage || t("model.dialog.connectivity.status.unavailable");
message.error(
t("model.dialog.error.connectivityFailed", { error: errorText })
);
} finally {
setVerifyingConnectivity(false);
}
};
// Handle batch adding models
const handleBatchAddModel = async () => {
// Only include models whose id is in selectedModelIds (i.e., switch is ON)
const enabledModels = modelList.filter((model: any) =>
selectedModelIds.has(model.id)
);
const modelType =
form.type === MODEL_TYPES.EMBEDDING && form.isMultimodal
? (MODEL_TYPES.MULTI_EMBEDDING as ModelType)
: form.type;
try {
const isEmbeddingType =
modelType === MODEL_TYPES.EMBEDDING ||
modelType === MODEL_TYPES.MULTI_EMBEDDING;
// Prepare the model data
const modelsData = enabledModels.map((model: any) => {
// For embedding/multi_embedding models, explicitly exclude max_tokens as backend will set it via connectivity check
if (isEmbeddingType) {
const { max_tokens, ...modelWithoutMaxTokens } = model;
return {
...modelWithoutMaxTokens,
// Add chunk size range for embedding models
...(isEmbeddingModel
? {
expected_chunk_size: form.chunkSizeRange[0],
maximum_chunk_size: form.chunkSizeRange[1],
chunk_batch: parseInt(form.chunkingBatchSize) || 10,
}
: {}),
};
} else {
return {
...model,
max_tokens: model.max_tokens || parseInt(form.maxTokens) || 4096,
};
}
});
// Use manage interface if tenantId is provided (for super admin), otherwise use current tenant
if (tenantId) {
await modelService.batchCreateManageTenantModels({
tenantId,
provider: form.provider,
type: modelType,
apiKey: form.apiKey.trim() === "" ? "sk-no-api-key" : form.apiKey,
models: modelsData,
});
} else {
await modelService.addBatchCustomModel({
api_key: form.apiKey.trim() === "" ? "sk-no-api-key" : form.apiKey,
provider: form.provider,
type: modelType,
models: modelsData,
});
}
// Reset form state and close dialog on success
resetForm();
handleClose();
// Notify parent to refresh model list - batch add returns all added models
const addedModels: AddedModel[] = enabledModels.map((model: any) => ({
name: model.displayName || model.id,
type: modelType,
}));
await onSuccess(addedModels.length > 0 ? addedModels[0] : undefined);
} catch (error: any) {
const errorMessage =
error?.message || t("model.dialog.error.addFailedLog");
const translatedError = translateError(errorMessage, t);
message.error(
t("model.dialog.error.addFailed", { error: translatedError })
);
}
};
// Handle settings button click
const handleSettingsClick = (model: any) => {
setSelectedModelForSettings(model);
setModelMaxTokens(model.max_tokens?.toString() || "4096");
setSettingsModalVisible(true);
};
// Handle settings save
const handleSettingsSave = () => {
if (selectedModelForSettings) {
// Update the model in the list with new max_tokens
setModelList((prev) =>
prev.map((model) =>
model.id === selectedModelForSettings.id
? { ...model, max_tokens: parseInt(modelMaxTokens) || 4096 }
: model
)
);
}
setSettingsModalVisible(false);
setSelectedModelForSettings(null);
};
// Handle adding a model
const handleAddModel = async () => {
// Check connectivity status before adding
if (!form.isBatchImport && connectivityStatus.status !== "available") {
message.warning(t("model.dialog.error.connectivityRequired"));
return;
}
setLoading(true);
if (form.isBatchImport) {
await handleBatchAddModel();
setLoading(false);
return;
}
try {
const modelType =
form.type === MODEL_TYPES.EMBEDDING && form.isMultimodal
? (MODEL_TYPES.MULTI_EMBEDDING as ModelType)
: form.type;
// Determine the maximum tokens value
let maxTokensValue = parseInt(form.maxTokens);
if (
form.type === MODEL_TYPES.EMBEDDING ||
form.type === MODEL_TYPES.MULTI_EMBEDDING
) {
// For embedding models, use the vector dimension as maxTokens
maxTokensValue = 0;
}
// Add to the backend service - use manage interface if tenantId is provided
if (tenantId) {
await modelService.createManageTenantModel({
tenantId,
name: form.name,
type: modelType,
url: form.url,
apiKey: form.apiKey.trim() === "" ? "sk-no-api-key" : form.apiKey,
maxTokens: maxTokensValue,
displayName: form.displayName || form.name,
expectedChunkSize: isEmbeddingModel
? form.chunkSizeRange[0]
: undefined,
maximumChunkSize: isEmbeddingModel
? form.chunkSizeRange[1]
: undefined,
chunkingBatchSize: isEmbeddingModel
? parseInt(form.chunkingBatchSize) || 10
: undefined,
});
} else {
await modelService.addCustomModel({
name: form.name,
type: modelType,
url: form.url,
apiKey: form.apiKey.trim() === "" ? "sk-no-api-key" : form.apiKey,
maxTokens: maxTokensValue,
displayName: form.displayName || form.name,
// Send chunk size range for embedding models
...(isEmbeddingModel
? {
expectedChunkSize: form.chunkSizeRange[0],
maximumChunkSize: form.chunkSizeRange[1],
chunkingBatchSize: parseInt(form.chunkingBatchSize) || 10,
}
: {}),
});
}
// Create the model configuration object
const modelConfig: SingleModelConfig = {
modelName: form.name,
displayName: form.displayName || form.name,
apiConfig: {
apiKey: form.apiKey,
modelUrl: form.url,
},
};
// Add the dimension field for embedding models
if (form.type === MODEL_TYPES.EMBEDDING) {
modelConfig.dimension = parseInt(form.vectorDimension);
}
// Update the local storage according to the model type
let configUpdate: any = {};
switch (modelType) {
case MODEL_TYPES.LLM:
configUpdate = { llm: modelConfig };
break;
case MODEL_TYPES.EMBEDDING:
configUpdate = { embedding: modelConfig };
break;
case MODEL_TYPES.MULTI_EMBEDDING:
configUpdate = { multiEmbedding: modelConfig };
break;
case MODEL_TYPES.VLM:
configUpdate = { vlm: modelConfig };
break;
case MODEL_TYPES.RERANK:
configUpdate = { rerank: modelConfig };
break;
case MODEL_TYPES.TTS:
configUpdate = { tts: modelConfig };
break;
case MODEL_TYPES.STT:
configUpdate = { stt: modelConfig };
break;
}
// Save to localStorage and persist to backend
updateModelConfig(configUpdate);
await persistModelConfig();
// Create the returned model information
const addedModel: AddedModel = {
name: form.displayName,
type: modelType,
};
// Reset form state
resetForm();
// Call the success callback, pass the new added model information
await onSuccess(addedModel);
// Close the dialog
handleClose();
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
const translatedError = translateError(errorMessage, t);
message.error(
t("model.dialog.error.addFailed", { error: translatedError })
);
log.error(t("model.dialog.error.addFailedLog"), error);
} finally {
setLoading(false);
}
};
const isEmbeddingModel = form.type === MODEL_TYPES.EMBEDDING;
return (
<Modal
title={t("model.dialog.title")}
open={isOpen}
onCancel={handleClose}
footer={null}
destroyOnHidden
>
<div className="space-y-4">
{/* Batch Import Switch */}
<div>
<div className="flex justify-between items-center">
<label className="block text-sm font-medium text-gray-700">
{t("model.dialog.label.batchImport")}
</label>
<Switch
checked={form.isBatchImport}
onChange={(checked) => handleFormChange("isBatchImport", checked)}
/>
</div>
<div className="text-xs text-gray-500 mt-1">
{form.isBatchImport
? t("model.dialog.hint.batchImportEnabled")
: t("model.dialog.hint.batchImportDisabled")}
</div>
</div>
{/* Model Provider (shown only when batch import is enabled) */}
{form.isBatchImport && (
<div>
<label className="block mb-1 text-sm font-medium text-gray-700">
{t("model.dialog.label.provider")}
<span className="text-red-500">*</span>
</label>
<Select
style={{ width: "100%" }}
value={form.provider}
onChange={(value) => handleFormChange("provider", value)}
>
<Option value="modelengine">
{t("model.provider.modelengine")}
</Option>
<Option value="silicon">{t("model.provider.silicon")}</Option>
<Option value="dashscope">{t("model.provider.dashscope")}</Option>
<Option value="tokenpony">{t("model.provider.tokenpony")}</Option>
<Option value="minimax">{t("model.provider.minimax")}</Option>
</Select>
{/* ModelEngine URL input (only when provider is ModelEngine) */}
{form.provider === "modelengine" && (
<div className="mt-3">
<label className="block mb-1 text-sm font-medium text-gray-700">
ModelEngine URL
</label>
<Input
placeholder={t("model.dialog.placeholder.modelEngineUrl")}
value={(form as any).modelEngineUrl}
onChange={(e) =>
handleFormChange("modelEngineUrl", e.target.value)
}
/>
</div>
)}
</div>
)}
{/* Model Type */}
<div>
<label className="block mb-1 text-sm font-medium text-gray-700">
{t("model.dialog.label.type")}{" "}
<span className="text-red-500">*</span>
</label>
<Select
style={{ width: "100%" }}
value={form.type}
onChange={(value) => handleFormChange("type", value)}
>
<Option value={MODEL_TYPES.LLM}>{t("model.type.llm")}</Option>
<Option value={MODEL_TYPES.EMBEDDING}>
{t("model.type.embedding")}
</Option>
<Option value={MODEL_TYPES.VLM}>{t("model.type.vlm")}</Option>
<Option value={MODEL_TYPES.RERANK} disabled>
{t("model.type.rerank")}
</Option>
<Option value={MODEL_TYPES.STT} disabled>
{t("model.type.stt")}
</Option>
<Option value={MODEL_TYPES.TTS} disabled>
{t("model.type.tts")}
</Option>
</Select>
</div>
{/* Multimodal Switch */}
{isEmbeddingModel && !form.isBatchImport && (
<div>
<div className="flex justify-between items-center">
<label className="block text-sm font-medium text-gray-700">
{t("model.dialog.label.multimodal")}
</label>
<Switch
checked={form.isMultimodal}
onChange={(checked) =>
handleFormChange("isMultimodal", checked)
}
/>
</div>
<div className="text-xs text-gray-500 mt-1">
{form.isMultimodal
? t("model.dialog.hint.multimodalEnabled")
: t("model.dialog.hint.multimodalDisabled")}
</div>
</div>
)}
{/* Model Name */}
{!form.isBatchImport && (
<div>
<label
htmlFor="name"
className="block mb-1 text-sm font-medium text-gray-700"
>
{t("model.dialog.label.name")}{" "}
<span className="text-red-500">*</span>
</label>
<Input
id="name"
placeholder={t("model.dialog.placeholder.name")}
value={form.name}
onChange={handleModelNameChange}
/>
</div>
)}
{/* Display Name */}
{!form.isBatchImport && (
<div>
<label
htmlFor="displayName"
className="block mb-1 text-sm font-medium text-gray-700"
>
{t("model.dialog.label.displayName")}
</label>
<Input
id="displayName"
placeholder={t("model.dialog.placeholder.displayName")}
value={form.displayName}
onChange={(e) => handleFormChange("displayName", e.target.value)}
/>
</div>
)}
{/* Model URL */}
{!form.isBatchImport && (
<div>
<label
htmlFor="url"
className="block mb-1 text-sm font-medium text-gray-700"
>
{t("model.dialog.label.url")}{" "}
<span className="text-red-500">*</span>
</label>
<Input
id="url"
placeholder={
form.type === MODEL_TYPES.EMBEDDING
? t("model.dialog.placeholder.url.embedding")
: t("model.dialog.placeholder.url")
}
value={form.url}
onChange={(e) => handleFormChange("url", e.target.value)}
/>
</div>
)}
{/* API Key */}
<div>
<label
htmlFor="apiKey"
className="block mb-1 text-sm font-medium text-gray-700"
>
{t("model.dialog.label.apiKey")}{" "}
{form.isBatchImport && <span className="text-red-500">*</span>}
</label>
<Input.Password
id="apiKey"
placeholder={t("model.dialog.placeholder.apiKey")}
value={form.apiKey}
onChange={(e) => handleFormChange("apiKey", e.target.value)}
autoComplete="new-password"
/>
</div>
{/* Chunk Size Slider (Embedding model only) */}
{isEmbeddingModel && (
<div>
<label className="block mb-1 text-sm font-medium text-gray-700">
{t("modelConfig.slider.chunkingSize")}
</label>
<ModelChunkSizeSlider
value={form.chunkSizeRange}
onChange={(value) => {
setForm((prev) => ({
...prev,
chunkSizeRange: value,
}));
}}
/>