Skip to main content

Alith Orchestrator

The Alith Orchestrator is an intelligent music generation system that combines the Alith AI framework with Google’s Lyria RealTime API to provide zero-dropout audio streaming and adaptive music generation.

Architecture Overview

The orchestrator implements a distributed architecture with multiple specialized components:

Core Components

AlithAgent

The core intelligence uses Alith’s Agent class with Gemini 2.5 Flash Lite for real-time decision making:
// Initialize Alith agent with optimized preamble
musicAgent = new Agent({
  model: "gemini-2.5-flash-lite",
  apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
  baseUrl: "generativelanguage.googleapis.com/v1beta/openai",
  preamble: enhancedPreamble,
  memory: new WindowBufferMemory(8)
});
The agent processes sensor data and generates Lyria prompts using embedded knowledge:
const interpretation = await musicAgent.run(JSON.stringify({
  sensorData: enrichedSensorData,
  sessionHistory: sessionData?.history || [],
  userProfile: sessionData?.profile || {},
  currentBaseline: this.currentBaseline
}));

Enhanced Sensor Processing

The orchestrator implements intelligent sensor interpretation with rate limiting and smoothing:

Rate Limiting and Optimization

The system implements sophisticated rate limiting to respect API quotas:
// Optimized rate limiting for baseline-driven processing
this.geminiCallCooldown = 4500; // 4.5 seconds for 15 requests/minute quota
this.serverLatency = 250; // Stability-focused latency
this.minSendInterval = 200; // Smooth server processing

Pattern Learning System

QDrant Integration

User patterns are stored using QDrant vector database for continuous learning:
userPatternStore = new QdrantStore({
  url: process.env.QDRANT_URL || 'http://localhost:6333',
  collectionName: 'user_patterns',
  apiKey: process.env.QDRANT_API_KEY
});

Memory Management

The system uses Alith’s WindowBufferMemory for context retention:
// Reduced memory window for token efficiency
memory: new WindowBufferMemory(8)

WebSocket Communication

Server Implementation

The orchestrator provides WebSocket endpoints for real-time communication:
this.wsServer.on('connection', (ws) => {
  ws.on('message', async (message) => {
    const data = JSON.parse(message);
    
    if (data.type === 'sensorUpdate') {
      await this.processSensorData(data.sensorData, ws);
    }
  });
});

Client Coordination

The frontend coordinator manages multiple connection types:

Performance Optimizations

Token Efficiency

The system optimizes token usage through embedded knowledge:
function preambleWithKnowledge() {
  const poemsContent = readFileSync(join(__dirname, 'knowledge', 'poems.txt'), 'utf8');
  const parametersContent = readFileSync(join(__dirname, 'knowledge', 'parameters.txt'), 'utf8');
  
  return `Enhanced preamble with ${poemsContent} and ${parametersContent}`;
}

Processing Queue

Agent synchronization prevents parallel token consumption:
let agentProcessingQueue = Promise.resolve();
let activeProcessingCount = 0;

// Queue processing to prevent parallel API calls
agentProcessingQueue = agentProcessingQueue.then(async () => {
  activeProcessingCount++;
  try {
    return await processRequest();
  } finally {
    activeProcessingCount--;
  }
});

Error Handling and Resilience

Fallback Systems

The orchestrator implements multiple fallback mechanisms:
createIntelligentRaveFallback(enrichedSensorData, sessionData) {
  // Energy-based fallback when AI agent is unavailable
  const energy = Math.sqrt(
    Math.pow(enrichedSensorData.x, 2) + 
    Math.pow(enrichedSensorData.y, 2) + 
    Math.pow(enrichedSensorData.z, 2)
  ) / 3;
  
  return {
    singleCoherentPrompt: this.generateEnergyBasedPrompt(energy),
    lyriaConfig: this.generateEnergyBasedConfig(energy),
    requiresCrossfade: energy > 0.6
  };
}

Connection Recovery

Automatic reconnection with exponential backoff:
async reconnectToServer() {
  const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
  
  setTimeout(async () => {
    try {
      await this.connectToInterpretationServer();
    } catch (error) {
      this.reconnectAttempts++;
      if (this.reconnectAttempts < this.maxReconnectAttempts) {
        this.reconnectToServer();
      }
    }
  }, delay);
}