| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| export class ModelMappingUtils { |
| |
| |
| |
| |
| |
| static parseModelMapping(mappingValue) { |
| if (!mappingValue || typeof mappingValue !== 'string' || mappingValue.trim() === '') { |
| return null; |
| } |
|
|
| try { |
| const mapping = JSON.parse(mappingValue); |
| if (typeof mapping !== 'object' || mapping === null) { |
| return null; |
| } |
| return mapping; |
| } catch (error) { |
| console.warn('模型重定向 JSON 解析失败:', error); |
| return null; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| static applyModelMapping(mapping, currentModels) { |
| if (!mapping || typeof mapping !== 'object') { |
| return { updatedModels: currentModels, newMapping: {}, hasChanges: false }; |
| } |
|
|
| const updatedModels = new Set(currentModels); |
| const newMapping = Object.create(null); |
| let hasChanges = false; |
|
|
| |
| Object.entries(mapping).forEach(([displayName, originalName]) => { |
| const displayNameTrimmed = displayName.trim(); |
| const originalNameTrimmed = originalName?.trim(); |
|
|
| if (displayNameTrimmed && originalNameTrimmed) { |
| |
| if (!updatedModels.has(displayNameTrimmed)) { |
| updatedModels.add(displayNameTrimmed); |
| hasChanges = true; |
| } |
|
|
| |
| if (originalNameTrimmed !== displayNameTrimmed) { |
| if (updatedModels.delete(originalNameTrimmed)) { |
| hasChanges = true; |
| } |
| } |
|
|
| |
| newMapping[displayNameTrimmed] = originalNameTrimmed; |
| } |
| }); |
|
|
| return { |
| updatedModels: Array.from(updatedModels), |
| newMapping, |
| hasChanges |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| static restoreModelsToOriginal(currentModels, originalMapping) { |
| if (!currentModels || currentModels.length === 0) { |
| return []; |
| } |
|
|
| if (!originalMapping || Object.keys(originalMapping).length === 0) { |
| return currentModels; |
| } |
|
|
| |
| const safeMapping = Object.assign(Object.create(null), originalMapping); |
| const restoredModels = currentModels.map((model) => { |
| const key = typeof model === 'string' ? model.trim() : model; |
| return Object.prototype.hasOwnProperty.call(safeMapping, key) |
| ? safeMapping[key] |
| : key; |
| }); |
|
|
| |
| return Array.from(new Set(restoredModels)); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| static areModelsEqual(models1, models2) { |
| if (!Array.isArray(models1) || !Array.isArray(models2)) { |
| return false; |
| } |
|
|
| if (models1.length !== models2.length) { |
| return false; |
| } |
|
|
| const sorted1 = [...models1].sort(); |
| const sorted2 = [...models2].sort(); |
|
|
| return JSON.stringify(sorted1) === JSON.stringify(sorted2); |
| } |
|
|
| |
| |
| |
| |
| |
| static filterAndDedupeModels(models) { |
| if (!Array.isArray(models)) { |
| return []; |
| } |
|
|
| return Array.from(new Set(models.filter((m) => typeof m === 'string').map((m) => m.trim()).filter(Boolean))); |
| } |
| } |