Core Functions
Prompt Storage
storePromptsForWorkspace
Stores a list of prompts for a workspace, handling deduplication and deletions automatically.The stored prompts (after deduplication and trimming)
- Trims whitespace from each prompt
- Filters out empty strings
- Deduplicates against existing prompts in workspace
- Only inserts new prompts (skips duplicates)
- Deletes prompts that exist in DB but not in the input array
- Uses ClickHouse with fallback for resilience
analytics.user_prompts table in ClickHouse
Prompt Retrieval
fetchUserPromptsForWorkspace
Retrieves all active prompts configured for a workspace.Workspace ID to query
Array of prompt objects with:
id: Unique prompt identifier (UUID)user_id: User who created the promptworkspace_id: Workspace IDprompt: The prompt textcreated_at: Creation timestamp (ClickHouse format)
analytics.user_prompts table filtered by workspace_id
Response Storage
storePromptResponses
Stores AI model responses for prompts after a job run. Handles multiple providers and sources.- Only stores responses with
status: "fulfilled" - Generates UUID for each response
- Normalizes source data (ensures all fields present)
- Converts timestamp to ClickHouse format
- Uses fallback insertion (non-fatal on individual record failures)
- Logs errors for failed records without throwing
analytics.prompt_responses table
Source Schema:
Response Retrieval
fetchPromptResponsesForWorkspace
Retrieves all prompt responses for a workspace.Workspace ID
Array of response objects with:
id: Response UUIDprompt_id: Associated prompt IDprompt: Original prompt textuser_id: User IDworkspace_id: Workspace IDmodel: Provider identifiermodel_provider: Provider nameresponse: AI-generated response textsources: Array of source objectsprompt_run_at: Execution timestampcreated_at: Storage timestampis_analysed: Boolean flag for analysis status
fetchPromptSourcesForWorkspace
Aggregates and extracts domain and source statistics from all responses.Workspace ID
Object containing:
domain_stats: Array of{ domain: string, count: number }sorted by frequencysourceStats: Array of unique source objects with title, url, domain, favicon, cited_text
- Fetches all responses via
fetchPromptResponsesForWorkspace - Calls
extractDomainStats(responses)from@oneglanse/utils - Calls
extractSourceStats(responses)from@oneglanse/utils
Scheduling
scheduleCronForPrompts
Sets up a PostgreSQL cron job to automatically run prompts on a schedule.- Removes any existing schedule for the workspace first
- Creates new
pg_cronjob namedauto_run_prompts_{workspaceId} - Job calls internal tRPC endpoint
/api/trpc/internal.runPrompts - Uses
current_setting('app.api_base_url')andcurrent_setting('app.cron_secret')from GUCs - Secrets are NOT stored in
cron.jobtable (read at runtime) - Requires
configureSchedulerSecrets()to have been called at startup
- Uses parameterized SQL via
format(%L, ...)to prevent injection - Authenticates with
Authorization: Bearer {cron_secret}header - Validates request via internal middleware
unscheduleCronForPrompts
Removes the cron schedule for a workspace.Workspace ID
- Calls
cron.unschedule('auto_run_prompts_{workspaceId}') - Gracefully handles case where schedule doesn’t exist
- Does not throw errors
configureSchedulerSecrets
Initializes PostgreSQL GUCs with API credentials for cron jobs. Must be called once at service startup.- Reads
API_BASE_URLandINTERNAL_CRON_SECRETfrom environment - Executes
ALTER ROLE CURRENT_USER SET app.api_base_url = $1 - Executes
ALTER ROLE CURRENT_USER SET app.cron_secret = $1 - Persists GUCs for all future sessions opened by the app role
- Falls back gracefully with warning if ALTER ROLE fails (insufficient permissions)
- Database role must have
ALTER ROLEprivilege on itself, OR - Must be called with a superuser role
Usage in tRPC Routers
Example fromapps/web/src/server/api/routers/prompt/prompt.ts:
ClickHouse Schema
analytics.user_prompts
analytics.prompt_responses
Error Handling
storePromptsForWorkspace
- Throws if all inserts fail (when
throwOnAllFailed: true) - Logs individual record failures to console
- Non-fatal for partial failures
storePromptResponses
- Never throws (uses
throwOnAllFailed: false) - Logs detailed error info for failed records
- Includes prompt preview and response length in error logs
Scheduling Functions
scheduleCronForPrompts: ThrowsDatabaseErrorif SQL generation failsunscheduleCronForPrompts: Never throws (catches and ignores errors)configureSchedulerSecrets: Logs warnings but doesn’t throw
Type Definitions
Source Files
packages/services/src/prompt/storePromptsForWorkspace.ts- Prompt storagepackages/services/src/prompt/storePromptResponses.ts- Response storagepackages/services/src/prompt/fetchUserPromptsForWorkspace.ts- Prompt retrievalpackages/services/src/prompt/fetchPromptResponsesForWorkspace.ts- Response retrievalpackages/services/src/prompt/fetchPromptSourcesForWorkspace.ts- Source aggregationpackages/services/src/prompt/scheduler.ts- Cron schedulingpackages/services/src/prompt/lib/insertClickHouseWithFallback.ts- Resilient insertion
packages/services/src/prompt/index.ts.