initializeWithParams static method
Initialize with params
Matches Swift RunAnywhere.performCoreInit() flow:
- Phase 1: DartBridge.initialize() (sync, ~1-5ms)
- Phase 2: DartBridge.initializeServices() (async, ~100-500ms)
Implementation
static Future<void> initializeWithParams(SDKInitParams params) async {
if (_isInitialized) return;
final logger = SDKLogger('RunAnywhere.Init');
EventBus.shared.publish(SDKInitializationStarted());
try {
_currentEnvironment = params.environment;
_initParams = params;
// =========================================================================
// PHASE 1: Core Init (sync, ~1-5ms, no network)
// Matches Swift: RunAnywhere.performCoreInit() → CppBridge.initialize()
// =========================================================================
DartBridge.initialize(params.environment);
logger.debug('DartBridge initialized with platform adapter');
// =========================================================================
// PHASE 2: Services Init (async, ~100-500ms, may need network)
// Matches Swift: RunAnywhere.completeServicesInitialization()
// =========================================================================
// Step 2.1: Initialize service bridges with credentials
// Matches Swift: CppBridge.State.initialize() + CppBridge.initializeServices()
await DartBridge.initializeServices(
apiKey: params.apiKey,
baseURL: params.baseURL.toString(),
deviceId: DartBridgeDevice.cachedDeviceId,
);
logger.debug('Service bridges initialized');
// Step 2.2: Set base directory for model paths
// Matches Swift: CppBridge.ModelPaths.setBaseDirectory(documentsURL)
await DartBridge.modelPaths.setBaseDirectory();
logger.debug('Model paths base directory configured');
// Step 2.3: Setup local services (HTTP, etc.)
await serviceContainer.setupLocalServices(
apiKey: params.apiKey,
baseURL: params.baseURL,
environment: params.environment,
);
// Step 2.4: Register device with backend (REQUIRED before authentication)
// Matches Swift: CppBridge.Device.registerIfNeeded(environment:)
// The device must be registered in the backend database before auth can work
logger.debug('Registering device with backend...');
await _registerDeviceIfNeeded(params, logger);
// Step 2.5: Authenticate with backend (production/staging only)
// Matches Swift: CppBridge.Auth.authenticate(apiKey:) in setupHTTP()
// This gets access_token and refresh_token from backend for subsequent API calls
if (params.environment != SDKEnvironment.development) {
logger.debug('Authenticating with backend...');
await _authenticateWithBackend(params, logger);
}
// Step 2.6: Initialize model registry
// CRITICAL: Uses the GLOBAL C++ registry via rac_get_model_registry()
// Models must be in the global registry for rac_llm_component_load_model to find them
logger.debug('Initializing model registry...');
await DartBridgeModelRegistry.instance.initialize();
// NOTE: Discovery is NOT run here. It runs lazily on first availableModels() call.
// This matches Swift's Phase 2 behavior where discovery runs in background AFTER
// models have been registered by the app.
_isInitialized = true;
logger.info('✅ SDK initialized (${params.environment.description})');
EventBus.shared.publish(SDKInitializationCompleted());
// Track successful SDK initialization
TelemetryService.shared.trackSDKInit(
environment: params.environment.name,
success: true,
);
} catch (e) {
logger.error('❌ SDK initialization failed: $e');
_initParams = null;
_currentEnvironment = null;
_isInitialized = false;
_hasRunDiscovery = false;
EventBus.shared.publish(SDKInitializationFailed(e));
// Track failed SDK initialization
TelemetryService.shared.trackSDKInit(
environment: params.environment.name,
success: false,
);
TelemetryService.shared.trackError(
errorCode: 'sdk_init_failed',
errorMessage: e.toString(),
);
rethrow;
}
}