{"record":{"id":"48b1f41789028e05","repo":"linshenkx/prompt-optimizer","slug":"delete-predefined-variable","errorCode":"DELETE_PREDEFINED_VARIABLE","errorMessage":"Cannot delete predefined variable: ${name}","messagePattern":"Cannot delete predefined variable: (.+?)","errorType":"validation","errorClass":"VariableError","httpStatus":null,"severity":"error","filePath":"packages/ui/src/services/VariableManager.ts","lineNumber":148,"sourceCode":"      throw new VariableError(\n        `Variable value too long: ${value.length} > ${VARIABLE_VALIDATION.MAX_VALUE_LENGTH}`,\n        name,\n        undefined,\n        'VALUE_TOO_LONG'\n      );\n    }\n\n    this.customVariables[name] = value;\n    this.saveToStorage();\n  }\n\n  getVariable(name: string): string | undefined {\n    return this.customVariables[name];\n  }\n\n  deleteVariable(name: string): void {\n    if (this.isPredefinedVariable(name)) {\n      throw new VariableError(\n        `Cannot delete predefined variable: ${name}`,\n        name,\n        undefined,\n        'DELETE_PREDEFINED_VARIABLE'\n      );\n    }\n\n    delete this.customVariables[name];\n    this.saveToStorage();\n  }\n\n  listVariables(): Record<string, string> {\n    return { ...this.customVariables };\n  }\n\n  // 变量解析\n  resolveAllVariables(context?: Record<string, unknown>): Record<string, string> {\n    // 获取预定义变量的值","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/ui/src/services/VariableManager.ts#L130-L166","documentation":"VariableManager.deleteVariable throws VariableError with code DELETE_PREDEFINED_VARIABLE when attempting to delete a variable that is predefined. Predefined variables are system-owned and permanent; only custom variables (stored in customVariables) can be removed.","triggerScenarios":"Calling deleteVariable(name) where isPredefinedVariable(name) returns true. Commonly triggered by a 'delete all' or bulk-clear UI action that iterates all variable names without filtering.","commonSituations":"A settings screen offering delete buttons for every row including built-ins; import/sync scripts that mirror a remote variable set by deleting everything first; reset-to-defaults features.","solutions":["Filter the deletion loop to skip predefined names (use isPredefinedVariable or list only custom variables)","In UI, disable/hide the delete action for predefined variables","For 'reset' semantics, re-derive predefined values instead of deleting them"],"exampleFix":"// before\nfor (const name of allNames) variableManager.deleteVariable(name)\n\n// after\nfor (const name of allNames) {\n  if (!variableManager.isPredefinedVariable(name)) variableManager.deleteVariable(name)\n}","handlingStrategy":"validation","validationCode":"if (!variableManager.isPredefinedVariable(name)) {\n  variableManager.deleteVariable(name)\n}","typeGuard":"const isDeletable = (vm: VariableManager, name: string): boolean =>\n  !vm.isPredefinedVariable(name)","tryCatchPattern":"try {\n  vm.deleteVariable(name)\n} catch (e) {\n  if (e instanceof VariableError && e.code === 'DELETE_PREDEFINED_VARIABLE') return\n  throw e\n}","preventionTips":["Hide/disable delete UI affordances for predefined variables","Bulk deletes should iterate only custom variables"],"tags":["variables","predefined","delete"],"backgroundTag":"reserved-name-collision","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}