Overview
The AppService (app.service.ts) is the central registry and management system for all third-party applications (TPAs) in MentraOS. It handles app discovery, authentication, webhook interactions, and maintains the app ecosystem.
Location
packages/cloud/src/services/core/app.service.ts
Key Responsibilities
1. App Registry Management
- Maintains both system apps and user-installed apps
- Provides app discovery and retrieval
- Manages app metadata and configurations
2. App Authentication
- Validates API keys for app connections
- Generates and manages app tokens
- Handles app-specific security
3. Webhook Orchestration
- Manages webhook endpoints for apps
- Handles session lifecycle webhooks (start/stop)
- Implements retry logic for failed webhooks
- Supports tool call webhooks for app interactions
4. App State Management
- Tracks app states across user sessions
- Manages app installation and permissions
- Handles pre-installed app configurations
Core Methods
getAllApps(userId?: string)
Retrieves all available apps for a user, combining:
- System apps (hardcoded for reliability)
- User-installed apps from MongoDB
- Pre-installed apps based on environment
getApp(packageName: string)
Fetches a specific app by its package name.
validateApiKey(packageName: string, apiKey: string)
Validates an app’s API key for authentication.
generateAppToken(packageName: string, userId: string, sessionId: string)
Creates a JWT token for app authentication.
Webhook System
Session Webhooks
Apps can register webhooks to be notified of session events:Start Webhook
Stop Webhook
Tool Call Webhooks
Apps can expose tools that the system can call:Pre-installed Apps
The service manages different app installation tiers:Production Apps
Debug/Development Apps
Additional apps loaded in development mode:- Testing apps
- Developer tools
- Experimental features
App State Tracking
The service maintains in-memory app states:INITIALIZING: App is starting upCONNECTED: App is readyRESURRECTING: App is reconnectingSTOPPED: App has been terminated
Integration with Other Services
With AppManager
- AppService provides app metadata
- AppManager handles runtime lifecycle
With SessionService
- Apps are associated with user sessions
- Session events trigger app webhooks
With MongoDB
- App definitions stored in
appscollection - User app installations tracked in
userscollection
Configuration
Environment Variables
SYSTEM_DASHBOARD_PACKAGE_NAME: Package name for system dashboardNODE_ENV: Determines if debug apps are loadedDEBUG_APPS: Force load debug apps in production
System Apps
Currently hardcoded for reliability, includes:- Dashboard app
- System utilities
- Core functionality apps
Security Considerations
- API Key Hashing: All API keys are hashed before storage
- Webhook Secrets: Optional endpoint secrets for webhook verification
- Token Expiration: App tokens have limited lifetime
- Permission Validation: Apps must declare required permissions
Future Improvements
- Database Migration: Move system apps to database
- Service Splitting: Separate into:
appstore.service.ts: App store functionalitydeveloper.service.ts: Developer portal featurestools.service.ts: Tool execution system
- Caching Layer: Add Redis for app metadata caching
- Webhook Queue: Implement proper webhook queue with retries
Usage Example
Best Practices
- Always validate app existence before operations
- Use try-catch for webhook calls - they can fail
- Log webhook failures but don’t crash the session
- Cache app metadata when possible
- Validate permissions before granting access