Skip to content

Commit 1d12e8b

Browse files
rizalibnumsluszniakchmjkb
authored
feat!: add modular resource fetcher adapters for Expo and bare React Native (#759)
## Description This PR introduces modular resource fetcher adapters to support both Expo and bare React Native environments, replacing the previous monolithic approach with a flexible, platform-specific architecture. **Key Changes** New Adapter Packages: - @rn-executorch/expo-adapter: Resource fetcher for Expo projects using expo-file-system and expo-asset - @rn-executorch/bare-adapter: Resource fetcher for bare React Native projects using @dr.pogodin/react-native-fs and @kesha-antonov/react-native-background-downloader **Initialization Changes:** - Added initExecutorch() function that requires explicit adapter selection - Users must now choose and configure the appropriate adapter for their project type - Provides better separation of concerns and platform-specific optimizations **Documentation Updates:** - Created individual README.md files for each adapter package ### Introduces a breaking change? - [x] Yes - [ ] No **Migration Required:** Users must now explicitly initialize the library with a resource fetcher adapter: ```typescript // Before (no initialization needed) import { useLLM } from 'react-native-executorch'; // After (required initialization) import { initExecutorch, useLLM } from 'react-native-executorch'; import { ExpoResourceFetcher } from '@rn-executorch/expo-adapter'; // or BareResourceFetcher initExecutorch({ resourceFetcher: ExpoResourceFetcher, }); ``` ### Type of change - [ ] Bug fix (change which fixes an issue) - [x] New feature (change which adds functionality) - [x] Documentation update (improves or adds clarity to existing documentation) - [ ] Other (chores, tests, code style improvements etc.) ### Tested on - [x] iOS - [x] Android ### Testing instructions **For Expo projects:** - Install dependencies: `yarn add @rn-executorch/expo-adapter expo-file-system expo-asset` - Initialize: `initExecutorch({ resourceFetcher: ExpoResourceFetcher })` - Run existing LLM example app to verify model downloads work correctly **For bare React Native projects:** - Install dependencies: `yarn add @rn-executorch/bare-adapter @dr.pogodin/react-native-fs @kesha-antonov/react-native-background-downloader` - Initialize: `initExecutorch({ resourceFetcher: BareResourceFetcher })` - Run the bare React Native example app in PR #763 > Note: A separate PR will add a dedicated bare React Native example app to make this PR easier to review. The Expo example apps can be used to verify the Expo adapter functionality. ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues Closes #549 ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have updated the documentation accordingly - [x] My changes generate no new warnings ### Additional notes **Why This Change:** - Different React Native environments have different filesystem APIs and c - apabilities - Expo projects benefit from using Expo's managed filesystem APIs - Bare React Native projects can leverage native libraries with background download support - Modular architecture allows for better platform-specific optimizations - Enables future extensibility for other environments (e.g., React Native Windows, macOS) **Split Into Multiple PRs:** To make review easier, this work has been split: - This PR: Core adapter infrastructure and Expo adapter implementation - Follow-up PR: Bare React Native example app demonstrating the bare adapter usage BREAKING CHANGE: `initExecutorch()` with explicit adapter selection is now required before using any react-native-executorch hooks. Users must install and configure either `@rn-executorch/expo-adapter` or `@rn-executorch/bare-adapter` depending on their project type. --------- Co-authored-by: Mateusz Słuszniak <mateusz.sluszniak@swmansion.com> Co-authored-by: Mateusz Sluszniak <56299341+msluszniak@users.noreply.github.com> Co-authored-by: Jakub Chmura <92989966+chmjkb@users.noreply.github.com>
1 parent 19cc916 commit 1d12e8b

90 files changed

Lines changed: 3655 additions & 1398 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.cspell-wordlist.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,7 @@ sublist
108108
TTFT
109109
timestamping
110110
logprob
111+
RNFS
112+
pogodin
113+
kesha
114+
antonov

.cspell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": "0.2",
33
"language": "en",
4-
"ignorePaths": ["**/node_modules", "**/Pods"],
4+
"ignorePaths": ["**/node_modules", "**/Pods", "**/readmes/**"],
55
"dictionaryDefinitions": [
66
{
77
"name": "project-words",

.github/workflows/ci.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
run: yarn lint
2525

2626
- name: Typecheck files
27-
run: yarn typecheck
27+
run: yarn workspaces foreach --all --topological-dev run prepare && yarn typecheck
2828

2929
build-library:
3030
runs-on: ubuntu-latest
@@ -35,7 +35,5 @@ jobs:
3535
- name: Setup
3636
uses: ./.github/actions/setup
3737

38-
- name: Build package
39-
run: |
40-
cd packages/react-native-executorch
41-
yarn prepare
38+
- name: Build all packages
39+
run: yarn workspaces foreach --all --topological-dev run prepare

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v20
1+
v22

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ React Native ExecuTorch is powering [Private Mind](https://privatemind.swmansion
7676
```bash
7777
# Install the package
7878
yarn add react-native-executorch
79+
80+
# If you use expo, please add these packages for resource fetching:
81+
yarn add @react-native-executorch/expo-adapter
82+
yarn add expo-file-system expo-asset
83+
84+
#if you use bare React Native project use these packages:
85+
yarn add @react-native-executorch/bare-adapter
86+
yarn add @dr.pogodin/react-native-fs @kesha-antonov/react-native-background-downloader
87+
7988
# Depending on the platform, choose either iOS or Android
8089
yarn expo run:< ios | android >
8190
```
@@ -88,8 +97,14 @@ Add this to your component file:
8897
import {
8998
useLLM,
9099
LLAMA3_2_1B,
91-
Message
100+
Message,
101+
initExecutorch,
92102
} from 'react-native-executorch';
103+
import { ExpoResourceFetcher } from '@react-native-executorch/expo-resource-fetcher';
104+
105+
initExecutorch({
106+
resourceFetcher: ExpoResourceFetcher,
107+
});
93108

94109
function MyComponent() {
95110
// Initialize the model 🚀

apps/computer-vision/app/_layout.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { Drawer } from 'expo-router/drawer';
2+
import { initExecutorch } from 'react-native-executorch';
3+
import { ExpoResourceFetcher } from '@react-native-executorch/expo-resource-fetcher';
4+
25
import ColorPalette from '../colors';
36
import React, { useState } from 'react';
47
import { Text, StyleSheet, View } from 'react-native';
@@ -10,6 +13,10 @@ import {
1013
} from '@react-navigation/drawer';
1114
import { GeneratingContext } from '../context';
1215

16+
initExecutorch({
17+
resourceFetcher: ExpoResourceFetcher,
18+
});
19+
1320
interface CustomDrawerProps extends DrawerContentComponentProps {
1421
isGenerating: boolean;
1522
}

apps/computer-vision/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"lint": "eslint . --ext .ts,.tsx --fix"
1212
},
1313
"dependencies": {
14-
"@react-native/metro-config": "^0.76.3",
14+
"@react-native-executorch/expo-resource-fetcher": "workspace:*",
15+
"@react-native/metro-config": "^0.81.5",
1516
"@react-navigation/drawer": "^7.3.9",
1617
"@react-navigation/native": "^7.1.6",
1718
"@shopify/react-native-skia": "2.2.12",
@@ -21,7 +22,7 @@
2122
"expo-linking": "~8.0.10",
2223
"expo-router": "~6.0.17",
2324
"expo-status-bar": "~3.0.9",
24-
"metro-config": "^0.81.0",
25+
"metro-config": "^0.81.5",
2526
"react": "19.1.0",
2627
"react-native": "0.81.5",
2728
"react-native-device-info": "^14.0.4",

apps/computer-vision/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"customConditions": ["react-native"],
1010
"noEmit": true,
1111
"paths": {
12-
"react-native-executorch": ["../../packages/react-native-executorch/src"]
12+
"react-native-executorch": ["../../packages/react-native-executorch/src"],
13+
"@react-native-executorch/expo-resource-fetcher": [
14+
"../../packages/expo-resource-fetcher/src"
15+
]
1316
}
1417
}
1518
}

apps/llm/app/_layout.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import { Drawer } from 'expo-router/drawer';
2+
import { initExecutorch } from 'react-native-executorch';
3+
import { ExpoResourceFetcher } from '@react-native-executorch/expo-resource-fetcher';
24
import ColorPalette from '../colors';
35
import React, { useState } from 'react';
46
import { Text, StyleSheet, View } from 'react-native';
5-
67
import {
78
DrawerContentComponentProps,
89
DrawerContentScrollView,
910
DrawerItemList,
1011
} from '@react-navigation/drawer';
1112
import { GeneratingContext } from '../context';
1213

14+
initExecutorch({
15+
resourceFetcher: ExpoResourceFetcher,
16+
});
17+
1318
interface CustomDrawerProps extends DrawerContentComponentProps {
1419
isGenerating: boolean;
1520
}

apps/llm/app/llm/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function LLMScreen() {
3434

3535
useEffect(() => {
3636
if (llm.error) {
37-
console.log('LLM error:', llm.error);
37+
console.error('LLM error:', llm.error);
3838
}
3939
}, [llm.error]);
4040

0 commit comments

Comments
 (0)