diff --git a/packages/react-native-executorch/src/controllers/LLMController.ts b/packages/react-native-executorch/src/controllers/LLMController.ts index 7dcc51528..6be8d6fc9 100644 --- a/packages/react-native-executorch/src/controllers/LLMController.ts +++ b/packages/react-native-executorch/src/controllers/LLMController.ts @@ -215,7 +215,9 @@ export class LLMController { ); } this.onToken = () => {}; - this.nativeModule.unload(); + if (this.nativeModule) { + this.nativeModule.unload(); + } this.isReadyCallback(false); this.isGeneratingCallback(false); } @@ -245,10 +247,22 @@ export class LLMController { } public interrupt() { + if (!this.nativeModule) { + throw new RnExecutorchError( + RnExecutorchErrorCode.ModuleNotLoaded, + "Cannot interrupt a model that's not loaded." + ); + } this.nativeModule.interrupt(); } public getGeneratedTokenCount(): number { + if (!this.nativeModule) { + throw new RnExecutorchError( + RnExecutorchErrorCode.ModuleNotLoaded, + "Cannot get token count for a model that's not loaded." + ); + } return this.nativeModule.getGeneratedTokenCount(); } diff --git a/packages/react-native-executorch/src/controllers/OCRController.ts b/packages/react-native-executorch/src/controllers/OCRController.ts index 4cac5d891..57f1e3489 100644 --- a/packages/react-native-executorch/src/controllers/OCRController.ts +++ b/packages/react-native-executorch/src/controllers/OCRController.ts @@ -103,7 +103,9 @@ export class OCRController { 'The model is currently generating. Please wait until previous model run is complete.' ); } - this.nativeModule.unload(); + if (this.nativeModule) { + this.nativeModule.unload(); + } this.isReadyCallback(false); this.isGeneratingCallback(false); } diff --git a/packages/react-native-executorch/src/controllers/VerticalOCRController.ts b/packages/react-native-executorch/src/controllers/VerticalOCRController.ts index 73ea54429..eaf4b0849 100644 --- a/packages/react-native-executorch/src/controllers/VerticalOCRController.ts +++ b/packages/react-native-executorch/src/controllers/VerticalOCRController.ts @@ -106,7 +106,9 @@ export class VerticalOCRController { 'The model is currently generating. Please wait until previous model run is complete.' ); } - this.ocrNativeModule.unload(); + if (this.ocrNativeModule) { + this.ocrNativeModule.unload(); + } this.isReadyCallback(false); this.isGeneratingCallback(false); } diff --git a/packages/react-native-executorch/src/hooks/natural_language_processing/useLLM.ts b/packages/react-native-executorch/src/hooks/natural_language_processing/useLLM.ts index 23882e65c..852441556 100644 --- a/packages/react-native-executorch/src/hooks/natural_language_processing/useLLM.ts +++ b/packages/react-native-executorch/src/hooks/natural_language_processing/useLLM.ts @@ -68,7 +68,9 @@ export const useLLM = ({ })(); return () => { - controllerInstance.delete(); + if (controllerInstance.isReady) { + controllerInstance.delete(); + } }; }, [ controllerInstance,