sendContextUpdate method

Future<void> sendContextUpdate(
  1. Uint8List imageData
)

Send context update (Image) before audio stream

Implementation

Future<void> sendContextUpdate(Uint8List imageData) async {
  if (imageData.isEmpty) {
    debugPrint(
        '[CLIENT CONTEXT]: ❌ Empty image data, skipping context update');
    return;
  }

  try {
    debugPrint(
        '[CLIENT CONTEXT]: 📸 Starting compression for ${imageData.length} bytes...');

    final compressedImage = await compute(_compressImageInIsolate, {
      'imageData': imageData,
      'maxSizeKb': 256,
      'quality': 70,
    });

    debugPrint(
        '[CLIENT CONTEXT]: ✅ Compression done: ${compressedImage.length} bytes');

    final message = {
      "type": "context_update",
      "image_data": base64Encode(compressedImage),
      "mime_type": "image/jpeg",
      "timestamp": DateTime.now().millisecondsSinceEpoch
    };

    if (_channel == null || !isConnected) {
      debugPrint(
          '[CLIENT CONTEXT]: ⚠️ Not connected, attempting to connect...');
      await _ensureConnected();
    }

    debugPrint('[CLIENT CONTEXT]: 📤 Sending context update message...');
    _sendMessage(message);

    // Wait a bit to ensure message is sent before audio starts
    await Future.delayed(const Duration(milliseconds: 100));

    debugPrint(
        '[CLIENT CONTEXT]: ✅ Context update sent (${compressedImage.length} bytes)');
  } catch (e) {
    debugPrint('[CLIENT CONTEXT]: ❌ Failed to send context update: $e');
    rethrow;
  }
}