Skip to main content

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
export async function savePrompt(
  input: PromptCreateInput
): Promise<SavePromptResult>

Parameters

input
PromptCreateInput
required
The prompt data to create
input.title
string
required
Prompt title (1-100 characters)
input.description
string
Optional description (max 500 characters)
input.content
string
required
Prompt content/template (1-20000 characters)
input.version_note
string
Optional note for the initial version (max 200 characters)

Returns

SavePromptResult
object
success
boolean
required
true if the prompt was created successfully
data
object
Returned when success is true
id
string
UUID of the newly created prompt
error
string
Returned when success is false. Error message describing what went wrong

Validation

Input is validated using the promptCreateSchema Zod schema:
const promptCreateSchema = z.object({
  title: z.string().trim().min(1).max(100),
  description: z.string().trim().max(500).optional(),
  content: z.string().trim().min(1).max(20000),
  version_note: z.string().trim().max(200).optional(),
});

Example

import { savePrompt } from '@/features/prompts/actions/save-prompt';

const result = await savePrompt({
  title: 'Code Review Assistant',
  description: 'Helps review pull requests',
  content: 'Review the following code for {{language}}:\n\n{{code}}',
  version_note: 'Initial version',
});

if (result.success) {
  console.log('Created prompt:', result.data.id);
} else {
  console.error('Failed:', result.error);
}

Behavior

  • Creates both a prompts record and an initial prompt_versions record (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
export async function saveNewVersion(
  promptId: string,
  input: { content: string; version_note?: string }
): Promise<SaveNewVersionResult>

Parameters

promptId
string
required
UUID of the prompt to create a new version for
input
object
required
Version data
input.content
string
required
New prompt content (1-20000 characters)
input.version_note
string
Optional note describing the changes (max 200 characters)

Returns

SaveNewVersionResult
object
success
boolean
required
true if the version was created successfully
newVersion
number
Returned when success is true. The version number of the newly created version
error
string
Returned when success is false. Error message

Example

import { saveNewVersion } from '@/features/prompts/actions/save-new-version';

const result = await saveNewVersion('prompt-uuid', {
  content: 'Updated template with {{new_variable}}',
  version_note: 'Added support for custom variables',
});

if (result.success) {
  console.log('Created version:', result.newVersion); // e.g., 2
}

Behavior

  • Automatically increments version number based on existing versions
  • Updates the parent prompt’s updated_at timestamp
  • 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
export async function restoreVersion(
  promptId: string,
  versionId: string
): Promise<{ success?: boolean; newVersion?: number; error?: string }>

Parameters

promptId
string
required
UUID of the prompt
versionId
string
required
UUID of the historical version to restore

Returns

result
object
success
boolean
true when restoration succeeds
newVersion
number
The new version number created with the restored content
error
string
Error message if restoration fails

Example

import { restoreVersion } from '@/features/prompts/actions/restore-version';

const result = await restoreVersion(
  'prompt-uuid',
  'version-uuid-to-restore'
);

if (result.success) {
  console.log('Restored as version:', result.newVersion);
}

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_at timestamp
  • 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
export async function updatePromptMetadata(
  promptId: string,
  input: PromptMetadataInput
): Promise<PromptActionResult>

Parameters

promptId
string
required
UUID of the prompt to update
input
PromptMetadataInput
required
Metadata to update
input.title
string
required
New title (1-100 characters)
input.description
string
New description (max 500 characters)

Returns

PromptActionResult
object
success
boolean
required
true if update succeeded
error
string
Error message if update failed

Example

import { updatePromptMetadata } from '@/features/prompts/actions/manage-prompt';

const result = await updatePromptMetadata('prompt-uuid', {
  title: 'Updated Title',
  description: 'New description',
});

if (!result.success) {
  console.error(result.error);
}

Behavior

  • Validates input using promptMetadataSchema
  • Trims whitespace from title and description
  • Sets description to null if empty after trimming
  • Updates the updated_at timestamp
  • Revalidates the home page cache

archivePrompt

Archives a prompt (soft delete). Location: src/features/prompts/actions/manage-prompt.ts:24
export async function archivePrompt(
  promptId: string
): Promise<PromptActionResult>

Parameters

promptId
string
required
UUID of the prompt to archive

Returns

PromptActionResult
object
success
boolean
required
true if archival succeeded
error
string
Error message if archival failed

Example

import { archivePrompt } from '@/features/prompts/actions/manage-prompt';

const result = await archivePrompt('prompt-uuid');

if (result.success) {
  console.log('Prompt archived');
}

Behavior

  • Sets archived_at to 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
export async function restorePrompt(
  promptId: string
): Promise<PromptActionResult>

Parameters

promptId
string
required
UUID of the archived prompt to restore

Returns

PromptActionResult
object
success
boolean
required
true if restoration succeeded
error
string
Error message if restoration failed

Example

import { restorePrompt } from '@/features/prompts/actions/manage-prompt';

const result = await restorePrompt('prompt-uuid');

if (result.success) {
  console.log('Prompt restored');
}

Behavior

  • Sets archived_at to null
  • Revalidates the home page cache

deletePrompt

Permanently deletes a prompt and all its versions. Location: src/features/prompts/actions/manage-prompt.ts:59
export async function deletePrompt(
  promptId: string
): Promise<PromptActionResult>

Parameters

promptId
string
required
UUID of the prompt to delete

Returns

PromptActionResult
object
success
boolean
required
true if deletion succeeded
error
string
Error message if deletion failed

Example

import { deletePrompt } from '@/features/prompts/actions/manage-prompt';

const result = await deletePrompt('prompt-uuid');

if (result.success) {
  console.log('Prompt permanently deleted');
}
This action is irreversible. All versions and associated data will be permanently deleted.

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
export async function togglePromptPublic(
  promptId: string,
  isPublic: boolean
): Promise<PromptActionResult>

Parameters

promptId
string
required
UUID of the prompt
isPublic
boolean
required
true to make the prompt public, false to make it private

Returns

PromptActionResult
object
success
boolean
required
true if the visibility update succeeded
error
string
Error message if update failed

Example

import { togglePromptPublic } from '@/features/prompts/actions/manage-prompt';

// Make public
const result = await togglePromptPublic('prompt-uuid', true);

if (result.success) {
  console.log('Prompt is now public');
}

// Make private
const result2 = await togglePromptPublic('prompt-uuid', false);

Behavior

  • Updates the is_public flag 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
export async function duplicatePrompt(
  promptId: string
): Promise<DuplicatePromptResult>

Parameters

promptId
string
required
UUID of the prompt to duplicate

Returns

DuplicatePromptResult
object
success
boolean
required
true if duplication succeeded
data
object
Returned when success is true
id
string
UUID of the newly created duplicate prompt
error
string
Error message if duplication failed

Example

import { duplicatePrompt } from '@/features/prompts/actions/duplicate-prompt';

const result = await duplicatePrompt('prompt-uuid');

if (result.success) {
  console.log('Created duplicate:', result.data.id);
  // Redirect to edit the new prompt
  router.push(`/prompts/${result.data.id}`);
}

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 the requireUser() helper or manual authentication checks:
const { data: { user }, error } = await supabase.auth.getUser();
if (error || !user) {
  return { success: false, error: 'Unauthorized' };
}

Error Handling

Actions follow a consistent error handling pattern:
  • Return objects with success boolean flag
  • On success: include data or relevant result fields
  • On failure: include error string 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 call revalidatePath('/') to invalidate Next.js cache and ensure the UI reflects changes immediately. Some actions revalidate specific paths like /prompts/${promptId}.

Build docs developers (and LLMs) love