Overview
The prompts module provides a comprehensive set of server actions for managing the complete lifecycle of prompts, including creation, versioning, archival, and metadata updates. All actions require authentication and operate within the context of the logged-in user.savePrompt
Creates a new prompt with an initial version. Location:src/features/prompts/actions/save-prompt.ts:11
Parameters
The prompt data to create
Prompt title (1-100 characters)
Optional description (max 500 characters)
Prompt content/template (1-20000 characters)
Optional note for the initial version (max 200 characters)
Returns
Validation
Input is validated using thepromptCreateSchema Zod schema:
Example
Behavior
- Creates both a
promptsrecord and an initialprompt_versionsrecord (version 1) - Automatically assigns the authenticated user as the owner
- If version creation fails, attempts to rollback the prompt creation
- Returns error if user is not authenticated
saveNewVersion
Creates a new version of an existing prompt. Location:src/features/prompts/actions/save-new-version.ts:17
Parameters
UUID of the prompt to create a new version for
Returns
Example
Behavior
- Automatically increments version number based on existing versions
- Updates the parent prompt’s
updated_attimestamp - Revalidates the cache for the home page
restoreVersion
Restores a historical version by creating a new version with the old content. Location:src/features/prompts/actions/restore-version.ts:7
Parameters
UUID of the prompt
UUID of the historical version to restore
Returns
Example
Behavior
- Fetches the content from the specified historical version
- Creates a new version with that content and an auto-generated note:
"Restored from v{N}" - Updates the prompt’s
updated_attimestamp - Revalidates both the prompt detail page and home page
updatePromptMetadata
Updates the title and/or description of a prompt without creating a new version. Location:src/features/prompts/actions/manage-prompt.ts:96
Parameters
UUID of the prompt to update
Returns
Example
Behavior
- Validates input using
promptMetadataSchema - Trims whitespace from title and description
- Sets description to
nullif empty after trimming - Updates the
updated_attimestamp - Revalidates the home page cache
archivePrompt
Archives a prompt (soft delete). Location:src/features/prompts/actions/manage-prompt.ts:24
Parameters
UUID of the prompt to archive
Returns
Example
Behavior
- Sets
archived_atto the current timestamp - Only archives if the prompt is not already archived
- Revalidates the home page cache
restorePrompt
Restores an archived prompt. Location:src/features/prompts/actions/manage-prompt.ts:42
Parameters
UUID of the archived prompt to restore
Returns
Example
Behavior
- Sets
archived_attonull - Revalidates the home page cache
deletePrompt
Permanently deletes a prompt and all its versions. Location:src/features/prompts/actions/manage-prompt.ts:59
Parameters
UUID of the prompt to delete
Returns
Example
Behavior
- Permanently removes the prompt record from the database
- Cascade deletes all associated versions (handled by database foreign key constraints)
- Revalidates the home page cache
togglePromptPublic
Toggles the public/private visibility of a prompt. Location:src/features/prompts/actions/manage-prompt.ts:76
Parameters
UUID of the prompt
true to make the prompt public, false to make it privateReturns
Example
Behavior
- Updates the
is_publicflag on the prompt - Public prompts are accessible via sharing links at
/p/[promptId] - Private prompts require authentication and ownership verification
- Revalidates the home page cache
duplicatePrompt
Creates a copy of an existing prompt with all content from the latest version. Location:src/features/prompts/actions/duplicate-prompt.ts:23
Parameters
UUID of the prompt to duplicate
Returns
Example
Behavior
- Fetches the source prompt’s latest version content
- Creates a new prompt with title prefixed by
"Copy of " - Copies the description unchanged
- Creates version 1 with the content from the source prompt’s latest version
- The duplicate is owned by the current user (even if duplicating someone else’s public prompt)
- If version creation fails, rolls back the prompt creation
- Revalidates the home page cache
Authentication
All actions require an authenticated user session. They use therequireUser() helper or manual authentication checks:
Error Handling
Actions follow a consistent error handling pattern:- Return objects with
successboolean flag - On success: include
dataor relevant result fields - On failure: include
errorstring with human-readable message - Database errors are caught and logged to console
- Validation errors from Zod are transformed into user-friendly messages
Cache Revalidation
Most mutating actions callrevalidatePath('/') to invalidate Next.js cache and ensure the UI reflects changes immediately. Some actions revalidate specific paths like /prompts/${promptId}.