linshenkx/prompt-optimizer · error · FavoriteValidationError

Cannot delete the last prompt asset version

Error message

Cannot delete the last prompt asset version

What it means

Thrown by FavoriteManager.deleteFavoritePromptAssetVersion when the prompt asset attached to a favorite has only one version left. The library forbids removing the final version because a prompt asset must always contain at least one usable version; deleting it would leave the favorite with an empty, unusable asset.

Source

Thrown at packages/core/src/services/favorite/manager.ts:546

    try {
      await this.storageProvider.updateData(this.STORAGE_KEYS.FAVORITES, (favorites: FavoritePrompt[] | null) => {
        const favoritesList = favorites || [];
        const index = favoritesList.findIndex(f => f.id === id);
        if (index === -1) {
          throw new FavoriteNotFoundError(id);
        }

        const currentFavorite = favoritesList[index];
        const metadata = currentFavorite.metadata && typeof currentFavorite.metadata === 'object'
          ? { ...currentFavorite.metadata }
          : {};
        const promptAsset = isPromptAsset(metadata.promptAsset) ? metadata.promptAsset : null;
        if (!promptAsset) {
          throw new FavoriteValidationError('Prompt asset is not available for this favorite');
        }
        if (promptAsset.versions.length <= 1) {
          throw new FavoriteValidationError('Cannot delete the last prompt asset version');
        }
        if (promptAsset.currentVersionId === versionId) {
          throw new FavoriteValidationError('Cannot delete the current prompt asset version');
        }

        const now = Date.now();
        const nextPromptAsset = deletePromptAssetVersion(promptAsset, versionId, now);
        if (!nextPromptAsset) {
          throw new FavoriteValidationError(`Prompt asset version not found: ${versionId}`);
        }

        const nextFavorite: FavoritePrompt = {
          ...currentFavorite,
          updatedAt: now,
          metadata: {
            ...metadata,
            promptAsset: nextPromptAsset,
          },

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Check promptAsset.versions.length > 1 before exposing the delete action (disable/hide the button in the UI)
  2. If you truly want to remove the asset, delete the whole favorite or replace the asset instead of deleting its last version
  3. Add another version first (update the prompt), then delete the old one
  4. Handle FavoriteValidationError and surface a user-friendly message

Example fix

// before
await manager.deleteFavoritePromptAssetVersion(favId, versionId); // throws if last version

// after
const fav = await manager.getFavorite(favId);
const asset = fav.metadata.promptAsset;
if (asset && asset.versions.length > 1 && asset.currentVersionId !== versionId) {
  await manager.deleteFavoritePromptAssetVersion(favId, versionId);
} else {
  // hide/disable delete in UI instead
}
Defensive patterns

Strategy: validation

Validate before calling

const fav = await manager.getFavorite(favId);
const asset = fav.metadata.promptAsset;
const canDelete = !!asset && asset.versions.length > 1 && asset.currentVersionId !== versionId;

Type guard

const hasMultipleVersions = (a: unknown): a is { versions: unknown[] } =>
  typeof a === 'object' && a !== null && Array.isArray((a as any).versions) && (a as any).versions.length > 1;

Try / catch

try { await manager.deleteFavoritePromptAssetVersion(id, vId); } catch (e) { if (e instanceof FavoriteValidationError) { /* show 'cannot delete last version' */ } else throw e; }

Prevention

When it happens

Trigger: Calling deleteFavoritePromptAssetVersion(favoriteId, versionId) on a favorite whose metadata.promptAsset.versions array has length <= 1 — i.e. the asset was created with a single version and no additional versions were ever saved via updateFavoritePromptAsset or similar.

Common situations: UI showing a delete button on the only remaining version of a saved prompt; importing favorites from an older export where assets only ever had one version; test fixtures that create a one-version asset and then attempt deletion.

Related errors


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