Comprehensive Project Documentation: Moodi App & n8n Workflow Integration
Project Overview
This project consists of two interconnected systems:
- Moodi iOS/macOS App - A SwiftUI-based mood tracking and reflection application
- n8n Cloud Workflows - Three automated workflows for content generation and processing
π Moodi iOS/macOS Application
Architecture & Tech Stack
Core Framework: SwiftUI with The Composable Architecture (TCA) by Point-Free
- Language: Swift 6.2+
- Platforms: iOS 17+, macOS 15+
- Architecture Pattern: Unidirectional data flow with reducer-based state management
Point-Free Ecosystem Dependencies
-
swift-composable-architecture (v1.22.3)
- Primary state management framework
- Reducer-based architecture with side effect management
- Time-travel debugging capabilities
-
swift-dependencies (v1.10.0)
- Dependency injection framework
@DependencyClientmacro for protocol-based testing- Mock implementations for unit testing
-
swift-tagged (v0.10.0)
- Type-safe identifiers and wrapper types
- Prevents ID mixing across different entity types
-
swift-identified-collections (v1.1.1)
- Collections indexed by identifiable elements
- Efficient lookups, updates, and diffing by ID
-
swift-concurrency-extras (v1.3.2)
- Swift concurrency utilities and extensions
Project Structure
Moodi.xcworkspace/
βββ Moodi/ # Main app target
β βββ MoodiApp.swift # SwiftUI app entry point
β βββ ContentView.swift # Root view integration
βββ MoodiPackage/ # Swift Package with modular features
βββ Sources/
β βββ AppFeature/ # Root app coordinator
β βββ NetworkClient/ # API communication protocol
β βββ AudioRecorderClient/ # Audio recording protocol
βββ Tests/
βββ AppFeatureTests/ # Swift Testing framework tests
Feature Architecture Pattern
Two-Layer Separation:
- Client Module (Protocol Layer) - Defines interfaces using
@DependencyClient - Feature Module (UI + Logic Layer) - SwiftUI views and TCA reducers
Key Features Implemented
1. AppFeature (Root Coordinator)
- State Management: Mood tracking, reflection cards, recording state
- User Interface: Gradient backgrounds, mood selector, reflection carousel
- Navigation: No tab bar - single-flow experience
- Lifecycle Management: App initialization and feature coordination
2. Audio Recording System
- AVFoundation Integration: High-quality M4A recording with AAC compression
- Real-time Updates: Power level monitoring and duration tracking
- Actor-based Architecture: Thread-safe recording management
- Temporary File Management: Automatic cleanup of recording files
3. Network Integration
- API Clients: n8n webhook integration for prompt enhancement
- Slang API: Speech-to-text transcription using Deepgram Nova-2
- Multipart Upload: Binary audio file transmission
- Error Handling: Comprehensive HTTP status code management
4. UI Components
- Reflection Cards Carousel: 3D rotation effects with drag gestures
- Mood Selector: Interactive emoji-based mood tracking
- Recording Interface: Three-state microphone button (idle/recording/transcribing)
- Gradient Design System: Warm, calming color palette
Testing Framework
- Swift Testing (not XCTest) using
@Testattributes and#expect()assertions - TCA TestStore: Deterministic state testing with action verification
- Mock Dependencies: Protocol-based dependency injection for testing
π n8n Cloud Workflow System
Infrastructure
- Platform: n8n Cloud (Pro plan)
- Base URL:
https://dyrtydata.app.n8n.cloud - API Access: JWT-based authentication with personal access tokens
- Execution Model: Webhook-triggered workflows with asynchronous processing
Workflow 1: "Moodi - carousel"
Purpose: Dynamic content generation for reflection cards
- Trigger: GET webhook endpoint
- Output: JSON array of reflection questions
- Integration: Feeds AppFeature's reflection card carousel
Workflow 2: "Enhance Prompt - Generate Media"
Purpose: AI-powered content generation pipeline with safety filtering
Tech Stack & Nodes:
-
Content Safety Pipeline:
- Text Classifier (LangChain): NSFW content detection with 6 categories
- OpenRouter Chat Model: GPT-4o-mini for classification
- Switch Logic: Routes safe content vs. blocks unsafe content
-
AI Enhancement Layer:
- OpenRouter Integration: GPT-4o for prompt enhancement
- LangChain AI Agents: Specialized for image and video prompt generation
- Sarcastic Tone: Custom system messages for elevated mood
-
Media Generation Pipeline:
- Runware AI: Image generation using Flux model (runware:108@1)
- 1024x1024 JPEG output
- FlowMatchEulerDiscreteScheduler
- 18 steps, CFG Scale 1
- ElevenLabs: Text-to-speech using voice ID
JBFqnCBsd6RMkjVDRZzb- Model: eleven_multilingual_v2
- Output: MP3 44.1kHz 128kbps
- Runware AI: Image generation using Flux model (runware:108@1)
-
File Management Solution:
- file.io Integration: Temporary URL generation for audio files
- 1-day Expiry: Automatic cleanup of uploaded content
- Binary-to-URL Conversion: Solves Veed API requirement issue
-
Video Synthesis:
- Veed API: Final video generation using Fabric-1.0 model
- 720p Resolution: Standard definition output
- Webhook Callback: Asynchronous processing notification
Workflow 3: "My workflow"
Purpose: General utility workflow (details not specified in current implementation)
π Integration Architecture
API Communication Flow
-
App β n8n:
// NetworkClient.swift let url = URL(string: "https://dyrtydata.app.n8n.cloud/webhook-test/enhance_prompt")! let body = EnhancePromptRequest(prompt: prompt, type: type.rawValue) -
n8n β External APIs:
- Runware: Image generation
- ElevenLabs: Audio synthesis
- file.io: Temporary file hosting
- Veed: Video composition
-
External β App:
- Slang API: Audio transcription using Deepgram Nova-2
let url = URL(string: "https://api.slng.ai/v1/stt/deepgram/nova-2")! // Multipart form data upload with M4A audio
Data Flow Architecture
User Input (Voice/Text)
β
[Moodi App] Audio Recording (AVFoundation)
β
[Slang API] Speech-to-Text Transcription
β
[n8n Workflow] Content Safety Check
β
[OpenRouter] AI Prompt Enhancement
β
[Runware + ElevenLabs] Parallel Media Generation
β
[file.io] URL Generation for Audio
β
[Code Node] Synchronization Logic
β
[Veed API] Final Video Synthesis
β
[Webhook] Completion Notification
Content Safety
- Multi-layer Filtering: Text classification before processing
- Category-based Blocking: 5 NSFW categories + safe category
- Automatic Rejection: Unsafe content gets "I cannot help with:" response
π Performance & Scalability
Async Processing
- n8n Webhooks: Non-blocking workflow execution
- Swift Concurrency: Actor-based audio recording
- TCA Effects: Side effect management with
.runeffects - Streaming Updates: Real-time recording feedback
Error Handling
- Network Resilience: HTTP status code validation
- Graceful Degradation: Fallback UI states for failed operations
- Comprehensive Logging: Console output for debugging
- User Feedback: Loading states and error messages
This comprehensive system demonstrates modern iOS development practices integrated with powerful cloud-based AI workflows, creating a seamless user experience for mood tracking and content generation.