Skip to content

Latest commit

 

History

History
313 lines (251 loc) · 9.06 KB

File metadata and controls

313 lines (251 loc) · 9.06 KB

DreamTracer Backend Progress Tracker

Overview

Backend implementation for AI-powered dream analysis with Inngest workflows, permanent sandbox IDs, and comprehensive pattern recognition.

Core Requirements

1. Individual Dream Analysis (On Save)

Trigger: When user hits "Save" button after journal entry Process:

  • Create permanent sandbox ID
  • OpenAI agent analyzes single dream
  • Generate personalized response (funny remarks, jokes, recommendations)
  • Store analysis results in database

2. Pattern Analysis (Manual Trigger)

Trigger: Button click on patterns page Process:

  • Create permanent sandbox ID
  • OpenAI agent analyzes ALL journal entries
  • Find patterns across dreams
  • Generate comprehensive insights and recommendations
  • Store pattern analysis in database

Implementation Progress

Phase 1: Inngest Setup & Configuration ✅

  • Setup Inngest Development Server

    • Install Inngest CLI
    • Configure inngest.json
    • Set up permanent sandbox environment
    • Test webhook connections
  • Database Schema Updates

    • Add sandbox_id field to dreams table
    • Add analysis_type field (individual vs pattern)
    • Add processing_status field
    • Create indexes for performance

Phase 2: Individual Dream Analysis Workflow ✅

  • API Endpoint Enhancement

    • Update /api/dreams POST to trigger Inngest workflow
    • Add sandbox ID generation
    • Implement immediate response + background processing
  • Inngest Job: Single Dream Analysis

    • Create dream-individual-analysis job
    • Implement OpenAI integration with humor prompts
    • Add error handling and retries
    • Store results in dream_analyses table
  • AI Prompt Engineering

    • Create base system prompt for individual analysis
    • Add humor detection and response generation
    • Include recommendation engine based on dream content
    • Test with various dream types (funny, scary, emotional)

Phase 3: Pattern Analysis Workflow ✅

  • Frontend Pattern Trigger

    • Add "Generate New AI Analysis" button to patterns page
    • Implement API call to trigger pattern analysis
    • Show processing status and progress
  • API Endpoint: Pattern Analysis

    • Create /api/patterns/analyze POST endpoint
    • Fetch all user dreams from database
    • Trigger Inngest pattern analysis job
    • Return job ID for status tracking
  • Inngest Job: Pattern Analysis

    • Create dream-pattern-analysis job
    • Fetch all user dreams efficiently
    • Implement batch processing for large datasets
    • Generate comprehensive pattern insights
    • Store in user_patterns table

Phase 4: Database Optimization 🔄

  • Performance Improvements

    • Add database indexes for dream queries
    • Implement pagination for large dream collections
    • Add caching for frequently accessed data
    • Optimize SQL queries for pattern analysis
  • Data Integrity

    • Add foreign key constraints
    • Implement soft deletes
    • Add audit logging
    • Create backup procedures

Phase 5: AI Enhancement & Personalization 🔄

  • Advanced AI Features

    • Implement user-specific prompts based on history
    • Add emotional intelligence to responses
    • Create joke database for humor enhancement
    • Implement learning from user feedback
  • Response Quality

    • A/B testing for different prompt styles
    • User rating system for AI responses
    • Continuous prompt improvement
    • Quality assurance checks

Phase 6: Error Handling & Monitoring 🔄

  • Robust Error Handling

    • Implement retry mechanisms
    • Add fallback responses
    • Handle OpenAI rate limits
    • Database connection error handling
  • Monitoring & Logging

    • Add comprehensive logging
    • Implement health checks
    • Monitor processing times
    • Alert system for failures

Technical Implementation Details

Inngest Job Definitions

1. Individual Dream Analysis Job

// File: src/lib/inngest-jobs/dream-analysis.ts
export const dreamAnalysisJob = inngest.createFunction(
  { id: "dream-individual-analysis" },
  { event: "dream.created" },
  async ({ event, step }) => {
    // Implementation details
  }
);

2. Pattern Analysis Job

// File: src/lib/inngest-jobs/pattern-analysis.ts
export const patternAnalysisJob = inngest.createFunction(
  { id: "dream-pattern-analysis" },
  { event: "pattern.analysis.requested" },
  async ({ event, step }) => {
    // Implementation details
  }
);

Database Schema Updates

Dreams Table Additions

ALTER TABLE dreams ADD COLUMN sandbox_id UUID;
ALTER TABLE dreams ADD COLUMN processing_status VARCHAR(50) DEFAULT 'pending';
CREATE INDEX idx_dreams_sandbox_id ON dreams(sandbox_id);
CREATE INDEX idx_dreams_processing_status ON dreams(processing_status);

Analysis Tracking Table

CREATE TABLE analysis_jobs (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id VARCHAR(255) NOT NULL,
  job_type VARCHAR(50) NOT NULL, -- 'individual' or 'pattern'
  sandbox_id UUID NOT NULL,
  status VARCHAR(50) DEFAULT 'running',
  created_at TIMESTAMP DEFAULT NOW(),
  completed_at TIMESTAMP,
  error_message TEXT
);

API Endpoints

Enhanced Dreams API

  • POST /api/dreams - Save dream + trigger analysis
  • GET /api/dreams/[id]/analysis-status - Check processing status

Pattern Analysis API

  • POST /api/patterns/analyze - Trigger pattern analysis
  • GET /api/patterns/analysis-status/[jobId] - Check status

AI Prompt Templates

Individual Dream Analysis Prompt

const INDIVIDUAL_ANALYSIS_PROMPT = `
You are a friendly AI dream analyst with a sense of humor. 
Analyze this dream and provide:
1. Light-hearted observations or funny remarks if appropriate
2. Personalized recommendations based on dream content
3. Emotional insights
4. Symbolic interpretations

Dream: {dreamContent}
Context: {userContext}
`;

Pattern Analysis Prompt

const PATTERN_ANALYSIS_PROMPT = `
You are an expert dream pattern analyst. 
Analyze all dreams and provide:
1. Recurring themes and their significance
2. Emotional patterns over time
3. Sleep quality correlations
4. Wellness recommendations
5. Behavioral insights

Dreams: {allDreams}
User Profile: {userProfile}
`;

Testing Strategy

Unit Tests

  • Test individual job functions
  • Test AI prompt generation
  • Test database operations
  • Test error handling

Integration Tests

  • Test full workflow end-to-end
  • Test Inngest job execution
  • Test database transactions
  • Test API endpoints

Performance Tests

  • Test with large dream datasets
  • Test concurrent job processing
  • Test OpenAI API rate limits
  • Test database query performance

Deployment Checklist

Development Environment

  • Inngest dev server running
  • Database migrations applied
  • Environment variables configured
  • OpenAI API key configured

Production Readiness

  • Inngest production setup
  • Database backup procedures
  • Error monitoring configured
  • Performance monitoring setup

Success Metrics

Functional Requirements

  • Dreams analyzed within 30 seconds of saving
  • Pattern analysis completes within 2 minutes
  • 99% job success rate
  • Meaningful and humorous AI responses

Performance Requirements

  • Handle 100+ concurrent dream analyses
  • Process 1000+ dreams for pattern analysis
  • Sub-second API response times
  • Efficient database queries

Next Steps

  1. Immediate: Set up Inngest development environment
  2. Week 1: Implement individual dream analysis workflow
  3. Week 2: Implement pattern analysis workflow
  4. Week 3: AI prompt optimization and testing
  5. Week 4: Performance optimization and monitoring

Notes

  • All sandbox IDs must be permanent and persistent
  • AI responses should be personalized and contextual
  • Error handling must be comprehensive
  • Performance must scale with user growth
  • User experience should be seamless and engaging

Status: Phase 1 & 2 & 3 Complete - Ready for Database Migration and Testing Last Updated: July 7, 2025

🚨 NEXT ACTION REQUIRED: Database Migration

Before testing, you must run the database migration manually in Supabase. See: DATABASE_MIGRATION_INSTRUCTIONS.md for detailed steps.

✅ What's Working Now:

  1. Individual Dream Analysis: When you save a dream, it will:

    • Generate a permanent sandbox ID
    • Trigger background AI analysis with Dr. DreamBot
    • Provide humorous observations and recommendations
    • Store results in the database
  2. Pattern Analysis: When you click "New AI Analysis":

    • Analyze ALL your dreams for patterns
    • Generate comprehensive wellness insights
    • Provide personalized recommendations
    • Track analysis progress
  3. Inngest Integration: Both servers running: