Overview
The search module provides full-text search capabilities powered by PostgreSQL’stsvector and a custom search_prompts database function. Search queries are tokenized and matched against prompt titles, descriptions, and content.
searchPrompts
Performs a full-text search across prompts with optional filtering. Location:src/features/search/actions.ts:15
Parameters
Search query string (1-500 characters). Searches across prompt titles, descriptions, and content.
Optional filters to narrow search results
UUID of a user to filter results by ownership. If provided, only prompts owned by this user are returned.
UUID of a collection to filter results. If provided, only prompts in this collection are returned.
Whether to include archived prompts. If
true, returns only archived prompts. If false, returns only non-archived prompts. If omitted, returns both.Returns
SearchResult Type
Each search result contains:Prompt UUID
UUID of the prompt owner
Prompt title
Prompt description (may be null)
ISO timestamp when prompt was archived, or
null if not archivedWhether the prompt is publicly accessible
ISO timestamp when prompt was created
ISO timestamp when prompt was last updated
Content from the most recent version
UUID of the latest version
Array of collection UUIDs this prompt belongs to
Relevance score from PostgreSQL full-text search. Higher values indicate better matches.
Validation
Input is validated using thesearchSchema Zod schema:
Examples
Basic search
Search with filters
Search user’s prompts
Behavior
- Empty query: Returns
{ data: [], error: null }without performing a database query - Case-insensitive: Search is case-insensitive
- Ranking: Results are ordered by relevance (PostgreSQL
ts_rank) - Database function: Delegates to the
search_promptsRPC function in PostgreSQL - Authentication: Does not require authentication, but typically used with
userIdfilter to scope results
Database Implementation
The search is powered by a PostgreSQL RPC function:to_tsquery()for query parsingtsvectorcolumn (search_tokens) for indexed full-text searchts_rank()for relevance scoring- A trigger (
update_prompt_search_tokens) that automatically updates the search index when prompts or versions change
Performance
- Search queries are fast due to GIN index on the
search_tokenscolumn - The
search_tokenstsvector is automatically maintained by a database trigger - Results are limited by the database function (typically to prevent excessive result sets)
Error Handling
Possible error scenarios:-
Validation failure: Invalid UUID format or query length
-
Database error: Connection issues or query execution failure
-
Empty query: Not treated as error
Security
- Search respects Row-Level Security (RLS) policies in Supabase
- Users can only search prompts they have access to (owned by them or public)
- API key authentication (for MCP) automatically scopes search to the key owner’s prompts
Integration with UI
Typical usage in a search component:MCP Integration
The search action is exposed via the Model Context Protocol at/api/mcp as the search_prompts tool: