linshenkx/prompt-optimizer · error · VariableError

DELETE_PREDEFINED_VARIABLE

DELETE_PREDEFINED_VARIABLE

Error message

Cannot delete predefined variable: ${name}

What it means

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.

Source

Thrown at packages/ui/src/services/VariableManager.ts:148

      throw new VariableError(
        `Variable value too long: ${value.length} > ${VARIABLE_VALIDATION.MAX_VALUE_LENGTH}`,
        name,
        undefined,
        'VALUE_TOO_LONG'
      );
    }

    this.customVariables[name] = value;
    this.saveToStorage();
  }

  getVariable(name: string): string | undefined {
    return this.customVariables[name];
  }

  deleteVariable(name: string): void {
    if (this.isPredefinedVariable(name)) {
      throw new VariableError(
        `Cannot delete predefined variable: ${name}`,
        name,
        undefined,
        'DELETE_PREDEFINED_VARIABLE'
      );
    }

    delete this.customVariables[name];
    this.saveToStorage();
  }

  listVariables(): Record<string, string> {
    return { ...this.customVariables };
  }

  // 变量解析
  resolveAllVariables(context?: Record<string, unknown>): Record<string, string> {
    // 获取预定义变量的值

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Filter the deletion loop to skip predefined names (use isPredefinedVariable or list only custom variables)
  2. In UI, disable/hide the delete action for predefined variables
  3. For 'reset' semantics, re-derive predefined values instead of deleting them

Example fix

// before
for (const name of allNames) variableManager.deleteVariable(name)

// after
for (const name of allNames) {
  if (!variableManager.isPredefinedVariable(name)) variableManager.deleteVariable(name)
}
Defensive patterns

Strategy: validation

Validate before calling

if (!variableManager.isPredefinedVariable(name)) {
  variableManager.deleteVariable(name)
}

Type guard

const isDeletable = (vm: VariableManager, name: string): boolean =>
  !vm.isPredefinedVariable(name)

Try / catch

try {
  vm.deleteVariable(name)
} catch (e) {
  if (e instanceof VariableError && e.code === 'DELETE_PREDEFINED_VARIABLE') return
  throw e
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/48b1f41789028e05. Report an issue: GitHub.