// Unload LLM when not in useawait RunAnywhere.unloadModel()// Unload STT when not neededawait RunAnywhere.unloadSTTModel()// Unload TTS when doneawait RunAnywhere.unloadTTSModel()
async function generateSafely(prompt: string): Promise<string> { try { const result = await RunAnywhere.generate(prompt) return result.text } catch (error) { if (isSDKError(error)) { switch (error.code) { case SDKErrorCode.modelNotLoaded: // Try to load model and retry await loadDefaultModel() return generateSafely(prompt) case SDKErrorCode.insufficientMemory: // Use a smaller model return 'I need to use a smaller model. Please try again.' case SDKErrorCode.generationCancelled: return '' // Expected, don't show error default: return 'Sorry, I encountered an error. Please try again.' } } return 'An unexpected error occurred.' }}
async function getAIResponse(prompt: string): Promise<string> { // Try on-device first if (await RunAnywhere.isModelLoaded()) { try { return (await RunAnywhere.generate(prompt)).text } catch { // Fall through to fallback } } // Fallback to simpler response return "I'm still setting up. Please try again in a moment."}
// Telemetry is only in Production mode// Use Development mode to disable all telemetryawait RunAnywhere.initialize({ environment: SDKEnvironment.Development, // No telemetry})
// In tests, use the smallest available modelconst testModel = 'smollm2-360m' // Fast for CI/CDbeforeAll(async () => { await RunAnywhere.initialize({ environment: SDKEnvironment.Development }) LlamaCPP.register() // Download and load small model for tests})
On iOS with New Architecture in RN 0.83+, react-native-screens crashes with errors like -[RCTView setColor:]. Use @react-navigation/stack (JS-based) instead of @react-navigation/native-stack, and mock react-native-screens in your Metro config:
New Architecture should be enabled on Android (newArchEnabled=true in gradle.properties) but disabled on iOS (set new_arch_enabled: false in Podfile). This avoids crashes with several native modules on iOS.
Third-party audio recording libraries like react-native-audio-recorder-player crash on iOS New Architecture. Build a custom native audio module using AVAudioRecorder (iOS/Swift) and AudioRecord (Android/Kotlin) that returns base64-encoded WAV data directly.
Audio is passed as base64-encoded strings between JavaScript and native code. RunAnywhere.transcribe() accepts base64-encoded WAV audio input. RunAnywhere.synthesize() returns base64-encoded float32 PCM audio. Use RunAnywhere.Audio.createWavFromPCMFloat32() to convert to a playable WAV file.
Add pickFirsts for libc++_shared.so, libjsi.so, libfbjni.so, libfolly_runtime.so, and libreactnative.so in your app’s build.gradlepackagingOptions to resolve conflicts.
Android Studio doesn’t inherit terminal PATH. Add explicit Node.js binary search paths in your root build.gradle to fix node not found errors during Gradle builds.