fix:Create duplicate model error#1772
Conversation
WalkthroughThe ModelSetting.vue component was refactored to introduce an Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Fix all issues with AI agents
In `@packages/plugins/model-manager/src/components/ModelSetting.vue`:
- Around line 143-145: modelBasicFormRef.value.validate() can throw if
modelBasicFormRef.value is null; update the validation call to guard against a
missing ref (either use optional chaining like
modelBasicFormRef.value?.validate() or check and early-return when
modelBasicFormRef.value is falsy) before entering the .then(...) chain so
validate() is only invoked when modelBasicFormRef.value exists; ensure related
logic that reads getLocalValue() stays consistent
(modelBasicFormRef.value?.getLocalValue()) and the form flow handles the
early-return case gracefully.
- Around line 188-204: Wrap the calls to createModel and updateModel inside a
try/catch around the block that sets emit('editCallback'), Notify(...),
closeModelSettingPanel(), selectedModel.value = null and isSaving.value = false;
specifically catch errors from createModel/updateModel (in the branch using
latestModelData.id and the else branch using updateModel(newModel.id,
newModel)), set isSaving.value = false in the catch, and call Notify({ type:
'error', message: '保存失败: ' + <error message> }) (or similar) so the user sees
the API error instead of silent failure; ensure existing emit/close/reset logic
only runs on success.
- Around line 160-176: The loop over newModel.parameters overwrites
isModelRefRelative and propertyName for each ModelRef so only the last
ModelRef's validation survives; change the logic in the parameters iteration
(where item.type === 'ModelRef') to preserve the first failure by, upon
encountering a ModelRef with no defaultValue, set isModelRefRelative = false and
propertyName = item.prop and then stop further processing for validation
purposes (either break the forEach by switching to a for-loop or track that a
failure was found and skip subsequent updates), ensuring subsequent ModelRef
fields do not overwrite the recorded first invalid result.
- Around line 220-242: The watcher assigns props directly in the else branch
causing parent mutation: when isSaving.value is true and newModel is set the
code does selectedModel.value = newModel which keeps a reference to the prop;
change this to either return early when isSaving.value is true (skip the watcher
update) or perform the same deep clone and enum deserialization used above
before assigning (use JSON.parse(JSON.stringify(newModel)) and run the
parameters Enum parsing) so selectedModel.value never holds a direct reference
to newModel; adjust the watcher logic around isSaving, selectedModel, and
newModel accordingly.
🧹 Nitpick comments (2)
packages/plugins/model-manager/src/components/ModelSetting.vue (2)
155-158: Guard against undefinedlatestModelDataor missingparameters.If
modelBasicFormRef.value?.getLocalValue()returnsundefinedor an object withoutparameters, Line 157 (latestModelData.parameters.filter(...)) will throw. Add a defensive check.Proposed guard
+ const params = latestModelData?.parameters ?? [] const newModel = { description: latestModelData.description, ... - parameters: JSON.parse(JSON.stringify(latestModelData.parameters.filter((item) => !!item.prop))) + parameters: JSON.parse(JSON.stringify(params.filter((item) => !!item.prop))) }
140-209: Mixingasync/awaitwith.then()/.catch()chains reduces readability.The function is declared
asyncbut uses.then().catch()for the validation promise. Consider usingtry/await/catchthroughout for a consistent and more readable async pattern.Sketch
const saveModel = async () => { const latestModelData = modelBasicFormRef.value?.getLocalValue() - modelBasicFormRef.value - .validate() - .then(async (valid) => { - if (valid) { - // ... save logic - } - }) - .catch(() => { - isSaving.value = false - }) + try { + const valid = await modelBasicFormRef.value?.validate() + if (!valid) return + isSaving.value = true + // ... save logic + } catch { + isSaving.value = false + } }
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
Bug Fixes & Improvements