loadSTTModel static method

Future<void> loadSTTModel(
  1. String modelId
)

Load an STT model

Implementation

static Future<void> loadSTTModel(String modelId) async {
  if (!_isInitialized) {
    throw SDKError.notInitialized();
  }

  final logger = SDKLogger('RunAnywhere.LoadSTTModel');
  logger.info('Loading STT model: $modelId');
  final startTime = DateTime.now().millisecondsSinceEpoch;

  EventBus.shared.publish(SDKModelEvent.loadStarted(modelId: modelId));

  try {
    // Find the model
    final models = await availableModels();
    final model = models.where((m) => m.id == modelId).firstOrNull;

    if (model == null) {
      throw SDKError.modelNotFound('STT model not found: $modelId');
    }

    if (model.localPath == null) {
      throw SDKError.modelNotDownloaded(
        'STT model is not downloaded. Call downloadModel() first.',
      );
    }

    // Resolve the actual model path
    final resolvedPath =
        await DartBridge.modelPaths.resolveModelFilePath(model);
    if (resolvedPath == null) {
      throw SDKError.modelNotFound(
          'Could not resolve STT model file path for: $modelId');
    }

    // Unload any existing model first
    if (DartBridge.stt.isLoaded) {
      DartBridge.stt.unload();
    }

    // Load model directly via DartBridgeSTT (mirrors Swift CppBridge.STT pattern)
    logger.debug('Loading STT model via C++ bridge: $resolvedPath');
    await DartBridge.stt.loadModel(resolvedPath, modelId, model.name);

    if (!DartBridge.stt.isLoaded) {
      throw SDKError.sttNotAvailable(
        'STT model failed to load - model may not be compatible',
      );
    }

    final loadTimeMs = DateTime.now().millisecondsSinceEpoch - startTime;

    // Track STT model load success
    TelemetryService.shared.trackModelLoad(
      modelId: modelId,
      modelType: 'stt',
      success: true,
      loadTimeMs: loadTimeMs,
    );

    EventBus.shared.publish(SDKModelEvent.loadCompleted(modelId: modelId));
    logger.info('STT model loaded: ${model.name}');
  } catch (e) {
    logger.error('Failed to load STT model: $e');

    // Track STT model load failure
    TelemetryService.shared.trackModelLoad(
      modelId: modelId,
      modelType: 'stt',
      success: false,
    );
    TelemetryService.shared.trackError(
      errorCode: 'stt_model_load_failed',
      errorMessage: e.toString(),
      context: {'model_id': modelId},
    );

    EventBus.shared.publish(SDKModelEvent.loadFailed(
      modelId: modelId,
      error: e.toString(),
    ));
    rethrow;
  }
}