├── .editorconfig
├── .gitattributes
├── .github
├── actions
│ └── setup
│ │ └── action.yml
└── workflows
│ ├── check-android.yml
│ ├── check-clang.yml
│ └── ci.yml
├── .gitignore
├── .gitmodules
├── .nvmrc
├── .watchmanconfig
├── .yarn
├── plugins
│ └── @yarnpkg
│ │ ├── plugin-interactive-tools.cjs
│ │ └── plugin-workspace-tools.cjs
└── releases
│ └── yarn-3.6.1.cjs
├── .yarnrc.yml
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── android
├── .editorconfig
├── build.gradle
├── gradle.properties
└── src
│ ├── main
│ ├── AndroidManifest.xml
│ ├── AndroidManifestNew.xml
│ └── java
│ │ └── com
│ │ └── ai
│ │ ├── AiModule.kt
│ │ ├── AiPackage.kt
│ │ ├── ChatState.kt
│ │ └── ModelState.kt
│ ├── newarch
│ └── AiSpec.kt
│ └── oldarch
│ └── AiSpec.kt
├── babel.config.js
├── example
├── .watchmanconfig
├── android
│ ├── app
│ │ ├── build.gradle
│ │ ├── debug.keystore
│ │ ├── proguard-rules.pro
│ │ └── src
│ │ │ ├── debug
│ │ │ └── AndroidManifest.xml
│ │ │ └── main
│ │ │ ├── AndroidManifest.xml
│ │ │ ├── java
│ │ │ └── com
│ │ │ │ └── aiexample
│ │ │ │ ├── MainActivity.kt
│ │ │ │ └── MainApplication.kt
│ │ │ └── res
│ │ │ ├── drawable
│ │ │ └── rn_edit_text_material.xml
│ │ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ └── values
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ ├── build.gradle
│ ├── gradle.properties
│ ├── gradle
│ │ └── wrapper
│ │ │ ├── gradle-wrapper.jar
│ │ │ └── gradle-wrapper.properties
│ ├── gradlew
│ ├── gradlew.bat
│ ├── mlc-package-config.json
│ └── settings.gradle
├── app.json
├── assets
│ └── avatar.png
├── babel.config.js
├── index.js
├── ios
│ ├── .xcode.env
│ ├── AiExample-Bridging-Header.h
│ ├── AiExample.xcodeproj
│ │ ├── project.pbxproj
│ │ └── xcshareddata
│ │ │ └── xcschemes
│ │ │ └── AiExample.xcscheme
│ ├── AiExample.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ ├── AiExample
│ │ ├── AiExample.entitlements
│ │ ├── AppDelegate.h
│ │ ├── AppDelegate.mm
│ │ ├── Images.xcassets
│ │ │ ├── AppIcon.appiconset
│ │ │ │ └── Contents.json
│ │ │ └── Contents.json
│ │ ├── Info.plist
│ │ ├── LaunchScreen.storyboard
│ │ ├── PrivacyInfo.xcprivacy
│ │ └── main.m
│ ├── AiExampleTests
│ │ ├── AiExampleTests.m
│ │ └── Info.plist
│ ├── File.swift
│ ├── Podfile
│ ├── Podfile.lock
│ ├── README.md
│ └── mlc-package-config.json
├── metro.config.js
├── mlc-config.json
├── package.json
├── polyfills.js
├── react-native.config.js
└── src
│ ├── App.tsx
│ ├── ModelSelection.tsx
│ └── NetworkInfo.tsx
├── ios
├── .clang-format
├── Ai.h
├── Ai.mm
├── BackgroundWorker.h
├── BackgroundWorker.mm
├── EngineState.h
├── EngineState.mm
├── LLMEngine.h
├── LLMEngine.mm
├── MLCEngine.h
└── MLCEngine.mm
├── lefthook.yml
├── package.json
├── react-native-ai.podspec
├── scripts
├── format-android.sh
├── format-ios.sh
└── mlc-prepare.js
├── src
├── NativeAi.ts
├── __tests__
│ └── index.test.tsx
├── index.tsx
└── polyfills.ts
├── tsconfig.build.json
├── tsconfig.json
├── turbo.json
└── yarn.lock
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig helps developers define and maintain consistent
2 | # coding styles between different editors and IDEs
3 | # editorconfig.org
4 |
5 | root = true
6 |
7 | [*]
8 |
9 | indent_style = space
10 | indent_size = 2
11 |
12 | end_of_line = lf
13 | charset = utf-8
14 | trim_trailing_whitespace = true
15 | insert_final_newline = true
16 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.pbxproj -text
2 | # specific for windows script files
3 | *.bat text eol=crlf
--------------------------------------------------------------------------------
/.github/actions/setup/action.yml:
--------------------------------------------------------------------------------
1 | name: Setup
2 | description: Setup Node.js and install dependencies
3 |
4 | runs:
5 | using: composite
6 | steps:
7 | - name: Setup Node.js
8 | uses: actions/setup-node@v3
9 | with:
10 | node-version-file: .nvmrc
11 |
12 | - name: Cache dependencies
13 | id: yarn-cache
14 | uses: actions/cache@v3
15 | with:
16 | path: |
17 | **/node_modules
18 | .yarn/install-state.gz
19 | key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}-${{ hashFiles('**/package.json', '!node_modules/**') }}
20 | restore-keys: |
21 | ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
22 | ${{ runner.os }}-yarn-
23 |
24 | - name: Install dependencies
25 | if: steps.yarn-cache.outputs.cache-hit != 'true'
26 | run: yarn install --immutable
27 | shell: bash
28 |
--------------------------------------------------------------------------------
/.github/workflows/check-android.yml:
--------------------------------------------------------------------------------
1 | name: Check Android
2 |
3 | on:
4 | push:
5 | branches:
6 | - main
7 | paths:
8 | - '.github/workflows/check-android.yml'
9 | - 'android/**'
10 | pull_request:
11 | paths:
12 | - '.github/workflows/check-android.yml'
13 | - 'android/**'
14 |
15 | jobs:
16 | Kotlin-Lint:
17 | runs-on: ubuntu-latest
18 | steps:
19 | - uses: actions/checkout@v4
20 | - run: |
21 | curl -sSLO https://github.com/pinterest/ktlint/releases/download/1.0.1/ktlint && chmod a+x ktlint && sudo mv ktlint /usr/local/bin/
22 | - name: run ktlint
23 | run: |
24 | ./scripts/format-android.sh
25 | continue-on-error: true
26 | - uses: yutailang0119/action-ktlint@v3
27 | with:
28 | report-path: ./android/build/*.xml
29 | continue-on-error: false
30 | - uses: actions/upload-artifact@v4
31 | with:
32 | name: ktlint-report
33 | path: ./android/build/*.xml
34 |
--------------------------------------------------------------------------------
/.github/workflows/check-clang.yml:
--------------------------------------------------------------------------------
1 | name: Check CLang
2 |
3 | on:
4 | push:
5 | branches:
6 | - main
7 | paths:
8 | - '.github/workflows/check-clang.yml'
9 | - 'ios/**'
10 | pull_request:
11 | branches:
12 | - main
13 | paths:
14 | - '.github/workflows/check-clang.yml'
15 | - 'ios/**'
16 |
17 | jobs:
18 | CLang-Format:
19 | runs-on: macos-latest
20 | steps:
21 | - uses: actions/checkout@v4
22 | - name: Install clang-format
23 | run: brew install clang-format
24 | - name: Check ios clang formatting
25 | run: |
26 | ./scripts/format-ios.sh
27 | - name: Check for changes
28 | run: git diff --exit-code HEAD
29 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 | on:
3 | push:
4 | branches:
5 | - main
6 | pull_request:
7 | branches:
8 | - main
9 |
10 | jobs:
11 | lint:
12 | runs-on: ubuntu-latest
13 | steps:
14 | - name: Checkout
15 | uses: actions/checkout@v3
16 |
17 | - name: Setup
18 | uses: ./.github/actions/setup
19 |
20 | - name: Lint files
21 | run: yarn lint
22 |
23 | - name: Typecheck files
24 | run: yarn typecheck
25 |
26 | test:
27 | runs-on: ubuntu-latest
28 | steps:
29 | - name: Checkout
30 | uses: actions/checkout@v3
31 |
32 | - name: Setup
33 | uses: ./.github/actions/setup
34 |
35 | - name: Run unit tests
36 | run: yarn test --maxWorkers=2 --coverage
37 |
38 | build-library:
39 | runs-on: ubuntu-latest
40 | steps:
41 | - name: Checkout
42 | uses: actions/checkout@v3
43 |
44 | - name: Setup
45 | uses: ./.github/actions/setup
46 |
47 | - name: Build package
48 | run: yarn prepare
49 | # TODO: Bring it back with all the following steps with setting up the environment variables related to MLC
50 | # build-android:
51 | # runs-on: ubuntu-latest
52 | # env:
53 | # TURBO_CACHE_DIR: .turbo/android
54 | # steps:
55 | # - name: Checkout
56 | # uses: actions/checkout@v4
57 |
58 | # - name: Setup
59 | # uses: ./.github/actions/setup
60 |
61 | # - name: Cache turborepo for Android
62 | # uses: actions/cache@v4
63 | # with:
64 | # path: ${{ env.TURBO_CACHE_DIR }}
65 | # key: ${{ runner.os }}-turborepo-android-${{ hashFiles('yarn.lock') }}
66 | # restore-keys: |
67 | # ${{ runner.os }}-turborepo-android-
68 |
69 | # - name: Check turborepo cache for Android
70 | # run: |
71 | # TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:android').cache.status")
72 |
73 | # if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
74 | # echo "turbo_cache_hit=1" >> $GITHUB_ENV
75 | # fi
76 |
77 | # - name: Install JDK
78 | # if: env.turbo_cache_hit != 1
79 | # uses: actions/setup-java@v4
80 | # with:
81 | # distribution: 'zulu'
82 | # java-version: '17'
83 |
84 | # - name: Finalize Android SDK
85 | # if: env.turbo_cache_hit != 1
86 | # run: |
87 | # /bin/bash -c "yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses > /dev/null"
88 |
89 | # - name: Create local.properties
90 | # run: echo "sdk.dir=$ANDROID_HOME" > example/android/local.properties
91 |
92 | # - name: Cache Gradle
93 | # if: env.turbo_cache_hit != 1
94 | # uses: actions/cache@v4
95 | # with:
96 | # path: |
97 | # ~/.gradle/wrapper
98 | # ~/.gradle/caches
99 | # key: ${{ runner.os }}-gradle-${{ hashFiles('example/android/gradle/wrapper/gradle-wrapper.properties') }}
100 | # restore-keys: |
101 | # ${{ runner.os }}-gradle-
102 |
103 | # - name: Build example for Android
104 | # env:
105 | # JAVA_OPTS: '-XX:MaxHeapSize=6g'
106 | # run: |
107 | # yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}"
108 |
109 | # build-ios:
110 | # runs-on: macos-14
111 | # env:
112 | # TURBO_CACHE_DIR: .turbo/ios
113 | # steps:
114 | # - name: Checkout
115 | # uses: actions/checkout@v3
116 | # with:
117 | # submodules: recursive
118 |
119 | # - name: Setup
120 | # uses: ./.github/actions/setup
121 |
122 | # - name: Cache turborepo for iOS
123 | # uses: actions/cache@v3
124 | # with:
125 | # path: ${{ env.TURBO_CACHE_DIR }}
126 | # key: ${{ runner.os }}-turborepo-ios-${{ hashFiles('yarn.lock') }}
127 | # restore-keys: |
128 | # ${{ runner.os }}-turborepo-ios-
129 |
130 | # - name: Check turborepo cache for iOS
131 | # run: |
132 | # TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:ios').cache.status")
133 |
134 | # if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
135 | # echo "turbo_cache_hit=1" >> $GITHUB_ENV
136 | # fi
137 |
138 | # - name: Cache cocoapods
139 | # if: env.turbo_cache_hit != 1
140 | # id: cocoapods-cache
141 | # uses: actions/cache@v3
142 | # with:
143 | # path: |
144 | # **/ios/Pods
145 | # key: ${{ runner.os }}-cocoapods-${{ hashFiles('example/ios/Podfile.lock') }}
146 | # restore-keys: |
147 | # ${{ runner.os }}-cocoapods-
148 |
149 | # - name: Install cocoapods
150 | # if: env.turbo_cache_hit != 1 && steps.cocoapods-cache.outputs.cache-hit != 'true'
151 | # run: |
152 | # cd example/ios
153 | # pod install
154 | # env:
155 | # NO_FLIPPER: 1
156 |
157 | # - name: Build example for iOS
158 | # run: |
159 | # yarn turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}"
160 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # OSX
2 | #
3 | .DS_Store
4 |
5 | # XDE
6 | .expo/
7 |
8 | # VSCode
9 | .vscode/
10 | jsconfig.json
11 |
12 | # Xcode
13 | #
14 | build/
15 | *.pbxuser
16 | !default.pbxuser
17 | *.mode1v3
18 | !default.mode1v3
19 | *.mode2v3
20 | !default.mode2v3
21 | *.perspectivev3
22 | !default.perspectivev3
23 | xcuserdata
24 | *.xccheckout
25 | *.moved-aside
26 | DerivedData
27 | *.hmap
28 | *.ipa
29 | *.xcuserstate
30 | project.xcworkspace
31 | .xcode.env.local
32 |
33 | # Android/IJ
34 | #
35 | .classpath
36 | .cxx
37 | .gradle
38 | .idea
39 | .project
40 | .settings
41 | local.properties
42 | android.iml
43 |
44 | # Cocoapods
45 | #
46 | example/ios/Pods
47 |
48 | # Ruby
49 | example/vendor/
50 |
51 | # node.js
52 | #
53 | node_modules/
54 | npm-debug.log
55 | yarn-debug.log
56 | yarn-error.log
57 |
58 | # BUCK
59 | buck-out/
60 | \.buckd/
61 | android/app/libs
62 | android/keystores/debug.keystore
63 |
64 | # Yarn
65 | .yarn/*
66 | !.yarn/patches
67 | !.yarn/plugins
68 | !.yarn/releases
69 | !.yarn/sdks
70 | !.yarn/versions
71 |
72 | # Expo
73 | .expo/
74 |
75 | # Turborepo
76 | .turbo/
77 |
78 | # generated by bob
79 | lib/
80 |
81 | # other
82 |
83 | dist/
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "3rdparty/tvm"]
2 | path = 3rdparty/tvm
3 | url = https://github.com/mlc-ai/relax.git
4 |
--------------------------------------------------------------------------------
/.nvmrc:
--------------------------------------------------------------------------------
1 | v18
2 |
--------------------------------------------------------------------------------
/.watchmanconfig:
--------------------------------------------------------------------------------
1 | {}
--------------------------------------------------------------------------------
/.yarnrc.yml:
--------------------------------------------------------------------------------
1 | nodeLinker: node-modules
2 | nmHoistingLimits: workspaces
3 |
4 | plugins:
5 | - path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
6 | spec: "@yarnpkg/plugin-interactive-tools"
7 | - path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
8 | spec: "@yarnpkg/plugin-workspace-tools"
9 |
10 | yarnPath: .yarn/releases/yarn-3.6.1.cjs
11 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 |
2 | # Contributor Covenant Code of Conduct
3 |
4 | ## Our Pledge
5 |
6 | We as members, contributors, and leaders pledge to make participation in our
7 | community a harassment-free experience for everyone, regardless of age, body
8 | size, visible or invisible disability, ethnicity, sex characteristics, gender
9 | identity and expression, level of experience, education, socio-economic status,
10 | nationality, personal appearance, race, caste, color, religion, or sexual
11 | identity and orientation.
12 |
13 | We pledge to act and interact in ways that contribute to an open, welcoming,
14 | diverse, inclusive, and healthy community.
15 |
16 | ## Our Standards
17 |
18 | Examples of behavior that contributes to a positive environment for our
19 | community include:
20 |
21 | * Demonstrating empathy and kindness toward other people
22 | * Being respectful of differing opinions, viewpoints, and experiences
23 | * Giving and gracefully accepting constructive feedback
24 | * Accepting responsibility and apologizing to those affected by our mistakes,
25 | and learning from the experience
26 | * Focusing on what is best not just for us as individuals, but for the overall
27 | community
28 |
29 | Examples of unacceptable behavior include:
30 |
31 | * The use of sexualized language or imagery, and sexual attention or advances of
32 | any kind
33 | * Trolling, insulting or derogatory comments, and personal or political attacks
34 | * Public or private harassment
35 | * Publishing others' private information, such as a physical or email address,
36 | without their explicit permission
37 | * Other conduct which could reasonably be considered inappropriate in a
38 | professional setting
39 |
40 | ## Enforcement Responsibilities
41 |
42 | Community leaders are responsible for clarifying and enforcing our standards of
43 | acceptable behavior and will take appropriate and fair corrective action in
44 | response to any behavior that they deem inappropriate, threatening, offensive,
45 | or harmful.
46 |
47 | Community leaders have the right and responsibility to remove, edit, or reject
48 | comments, commits, code, wiki edits, issues, and other contributions that are
49 | not aligned to this Code of Conduct, and will communicate reasons for moderation
50 | decisions when appropriate.
51 |
52 | ## Scope
53 |
54 | This Code of Conduct applies within all community spaces, and also applies when
55 | an individual is officially representing the community in public spaces.
56 | Examples of representing our community include using an official e-mail address,
57 | posting via an official social media account, or acting as an appointed
58 | representative at an online or offline event.
59 |
60 | ## Enforcement
61 |
62 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
63 | reported to the community leaders responsible for enforcement at
64 | [INSERT CONTACT METHOD].
65 | All complaints will be reviewed and investigated promptly and fairly.
66 |
67 | All community leaders are obligated to respect the privacy and security of the
68 | reporter of any incident.
69 |
70 | ## Enforcement Guidelines
71 |
72 | Community leaders will follow these Community Impact Guidelines in determining
73 | the consequences for any action they deem in violation of this Code of Conduct:
74 |
75 | ### 1. Correction
76 |
77 | **Community Impact**: Use of inappropriate language or other behavior deemed
78 | unprofessional or unwelcome in the community.
79 |
80 | **Consequence**: A private, written warning from community leaders, providing
81 | clarity around the nature of the violation and an explanation of why the
82 | behavior was inappropriate. A public apology may be requested.
83 |
84 | ### 2. Warning
85 |
86 | **Community Impact**: A violation through a single incident or series of
87 | actions.
88 |
89 | **Consequence**: A warning with consequences for continued behavior. No
90 | interaction with the people involved, including unsolicited interaction with
91 | those enforcing the Code of Conduct, for a specified period of time. This
92 | includes avoiding interactions in community spaces as well as external channels
93 | like social media. Violating these terms may lead to a temporary or permanent
94 | ban.
95 |
96 | ### 3. Temporary Ban
97 |
98 | **Community Impact**: A serious violation of community standards, including
99 | sustained inappropriate behavior.
100 |
101 | **Consequence**: A temporary ban from any sort of interaction or public
102 | communication with the community for a specified period of time. No public or
103 | private interaction with the people involved, including unsolicited interaction
104 | with those enforcing the Code of Conduct, is allowed during this period.
105 | Violating these terms may lead to a permanent ban.
106 |
107 | ### 4. Permanent Ban
108 |
109 | **Community Impact**: Demonstrating a pattern of violation of community
110 | standards, including sustained inappropriate behavior, harassment of an
111 | individual, or aggression toward or disparagement of classes of individuals.
112 |
113 | **Consequence**: A permanent ban from any sort of public interaction within the
114 | community.
115 |
116 | ## Attribution
117 |
118 | This Code of Conduct is adapted from the [Contributor Covenant][homepage],
119 | version 2.1, available at
120 | [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
121 |
122 | Community Impact Guidelines were inspired by
123 | [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
124 |
125 | For answers to common questions about this code of conduct, see the FAQ at
126 | [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
127 | [https://www.contributor-covenant.org/translations][translations].
128 |
129 | [homepage]: https://www.contributor-covenant.org
130 | [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
131 | [Mozilla CoC]: https://github.com/mozilla/diversity
132 | [FAQ]: https://www.contributor-covenant.org/faq
133 | [translations]: https://www.contributor-covenant.org/translations
134 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | Contributions are always welcome, no matter how large or small!
4 |
5 | We want this community to be friendly and respectful to each other. Please follow it in all your interactions with the project. Before contributing, please read the [code of conduct](./CODE_OF_CONDUCT.md).
6 |
7 | ## Development workflow
8 |
9 | This project is a monorepo managed using [Yarn workspaces](https://yarnpkg.com/features/workspaces). It contains the following packages:
10 |
11 | - The library package in the root directory.
12 | - An example app in the `example/` directory.
13 |
14 | To get started with the project, run `yarn` in the root directory to install the required dependencies for each package:
15 |
16 | ```sh
17 | yarn
18 | ```
19 |
20 | > Since the project relies on Yarn workspaces, you cannot use [`npm`](https://github.com/npm/cli) for development.
21 |
22 | The [example app](/example/) demonstrates usage of the library. You need to run it to test any changes you make.
23 |
24 | It is configured to use the local version of the library, so any changes you make to the library's source code will be reflected in the example app. Changes to the library's JavaScript code will be reflected in the example app without a rebuild, but native code changes will require a rebuild of the example app.
25 |
26 | If you want to use Android Studio or XCode to edit the native code, you can open the `example/android` or `example/ios` directories respectively in those editors. To edit the Objective-C or Swift files, open `example/ios/AiExample.xcworkspace` in XCode and find the source files at `Pods > Development Pods > react-native-ai`.
27 |
28 | To edit the the Kotlin files, open `example/android` in Android studio and find the source files at `react-native-ai` under `Android`.
29 |
30 | You can use various commands from the root directory to work with the project.
31 |
32 | To start the packager:
33 |
34 | ```sh
35 | yarn example start
36 | ```
37 |
38 | To run the example app on Android:
39 |
40 | ```sh
41 | yarn example android
42 | ```
43 |
44 | To run the example app on iOS:
45 |
46 | ```sh
47 | yarn example ios
48 | ```
49 |
50 | By default, the example is configured to build with the old architecture. To run the example with the new architecture, you can do the following:
51 |
52 | 1. For Android, run:
53 |
54 | ```sh
55 | ORG_GRADLE_PROJECT_newArchEnabled=true yarn example android
56 | ```
57 |
58 | 2. For iOS, run:
59 |
60 | ```sh
61 | cd example/ios
62 | RCT_NEW_ARCH_ENABLED=1 pod install
63 | cd -
64 | yarn example ios
65 | ```
66 |
67 | If you are building for a different architecture than your previous build, make sure to remove the build folders first. You can run the following command to cleanup all build folders:
68 |
69 | ```sh
70 | yarn clean
71 | ```
72 |
73 | To confirm that the app is running with the new architecture, you can check the Metro logs for a message like this:
74 |
75 | ```sh
76 | Running "AiExample" with {"fabric":true,"initialProps":{"concurrentRoot":true},"rootTag":1}
77 | ```
78 |
79 | Note the `"fabric":true` and `"concurrentRoot":true` properties.
80 |
81 | Make sure your code passes TypeScript and ESLint. Run the following to verify:
82 |
83 | ```sh
84 | yarn typecheck
85 | yarn lint
86 | ```
87 |
88 | To fix formatting errors, run the following:
89 |
90 | ```sh
91 | yarn lint --fix
92 | ```
93 |
94 | Remember to add tests for your change if possible. Run the unit tests by:
95 |
96 | ```sh
97 | yarn test
98 | ```
99 |
100 | ### Commit message convention
101 |
102 | We follow the [conventional commits specification](https://www.conventionalcommits.org/en) for our commit messages:
103 |
104 | - `fix`: bug fixes, e.g. fix crash due to deprecated method.
105 | - `feat`: new features, e.g. add new method to the module.
106 | - `refactor`: code refactor, e.g. migrate from class components to hooks.
107 | - `docs`: changes into documentation, e.g. add usage example for the module..
108 | - `test`: adding or updating tests, e.g. add integration tests using detox.
109 | - `chore`: tooling changes, e.g. change CI config.
110 |
111 | Our pre-commit hooks verify that your commit message matches this format when committing.
112 |
113 | ### Linting and tests
114 |
115 | [ESLint](https://eslint.org/), [Prettier](https://prettier.io/), [TypeScript](https://www.typescriptlang.org/)
116 |
117 | We use [TypeScript](https://www.typescriptlang.org/) for type checking, [ESLint](https://eslint.org/) with [Prettier](https://prettier.io/) for linting and formatting the code, and [Jest](https://jestjs.io/) for testing.
118 |
119 | Our pre-commit hooks verify that the linter and tests pass when committing.
120 |
121 | ### Publishing to npm
122 |
123 | We use [release-it](https://github.com/release-it/release-it) to make it easier to publish new versions. It handles common tasks like bumping version based on semver, creating tags and releases etc.
124 |
125 | To publish new versions, run the following:
126 |
127 | ```sh
128 | yarn release
129 | ```
130 |
131 | ### Scripts
132 |
133 | The `package.json` file contains various scripts for common tasks:
134 |
135 | - `yarn`: setup project by installing dependencies.
136 | - `yarn typecheck`: type-check files with TypeScript.
137 | - `yarn lint`: lint files with ESLint.
138 | - `yarn test`: run unit tests with Jest.
139 | - `yarn example start`: start the Metro server for the example app.
140 | - `yarn example android`: run the example app on Android.
141 | - `yarn example ios`: run the example app on iOS.
142 |
143 | ### Sending a pull request
144 |
145 | > **Working on your first pull request?** You can learn how from this _free_ series: [How to Contribute to an Open Source Project on GitHub](https://app.egghead.io/playlists/how-to-contribute-to-an-open-source-project-on-github).
146 |
147 | When you're sending a pull request:
148 |
149 | - Prefer small pull requests focused on one change.
150 | - Verify that linters and tests are passing.
151 | - Review the documentation to make sure it looks good.
152 | - Follow the pull request template when opening a pull request.
153 | - For pull requests that change the API or implementation, discuss with maintainers first by opening an issue.
154 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 szymonrybczak
4 | Permission is hereby granted, free of charge, to any person obtaining a copy
5 | of this software and associated documentation files (the "Software"), to deal
6 | in the Software without restriction, including without limitation the rights
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | copies of the Software, and to permit persons to whom the Software is
9 | furnished to do so, subject to the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be included in all
12 | copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
react-native-ai
2 |
3 |
7 |
8 |
9 |
10 |
11 | [](https://github.com/callstackincubator/ai/blob/main/LICENSE)
12 | [](https://www.npmjs.org/package/react-native-ai)
13 | [](https://www.npmjs.org/package/react-native-ai)
14 | [](https://www.npmjs.org/package/react-native-ai)
15 |
16 |
17 |
18 |
19 |
20 |
21 | ## Installation
22 |
23 | #### 1. Install the package
24 |
25 | ```
26 | npm install react-native-ai
27 | ```
28 |
29 | #### 2. Clone MLC LLM Engine repository and set environment variable.
30 |
31 | ```
32 | git clone https://github.com/mlc-ai/mlc-llm
33 | cd mlc-llm
34 | git submodule update --init --recursive
35 | MLC_LLM_SOURCE_DIR=$(pwd) // Add this to your environment variables e.g. in .zshrc
36 | ```
37 |
38 | > [!IMPORTANT]
39 | > Ensure that `mlc_llm` works and `MLC_LLM_SOURCE_DIR` is set in your environment variables.
40 |
41 | #### 3. Install `mlc_llm` CLI:
42 |
43 | To install `mlc_llm` CLI, please follow steps described [in the official guide](https://llm.mlc.ai/docs/install/mlc_llm.html).
44 |
45 | To ensure that CLI is installed correctly, run the following command:
46 |
47 | ```
48 | mlc_llm
49 | ```
50 |
51 | If you see any output then it means that CLI is installed correctly.
52 |
53 | #### 4. Add `mlc-config.json` with models and other properties to root directory of your project:
54 |
55 | ```js
56 | {
57 | "iphone": [
58 | {
59 | "model": "MODEL_NAME",
60 | "model_id": "MODEL_ID",
61 | // "estimated_vram_bytes": 3043000000
62 | }
63 | ],
64 | "android": [
65 | {
66 | "model": "MODEL_NAME",
67 | "model_id": "MODEL_ID",
68 | // "estimated_vram_bytes": 3043000000
69 | }
70 | ]
71 | }
72 | ```
73 |
74 | Read more about configuration for [Android](https://llm.mlc.ai/docs/deploy/android.html#customize-the-app) and for [iOS](https://llm.mlc.ai/docs/deploy/ios.html#customize-the-app).
75 |
76 | You can also check out [example config](https://github.com/callstackincubator/ai/blob/main/example/mlc-config.json) in the repository.
77 |
78 | #### 4. **[Android only]**
79 |
80 | If you want to execute models also on Android you need to set `ANDROID_NDK` and `TVM_NDK_CC` environment variables. Everything is described in [MLC LLM docs](https://llm.mlc.ai/docs/deploy/android.html#id2).
81 |
82 | #### 5. **[iOS only]** If you want to execute models also on iOS you need to:
83 |
84 | - Add "Increased Memory Limit" capability inside your Xcode project inside Signing & Capabilities tab.
85 | 
86 | - Install Cocoapods:
87 | ```
88 | cd ios && pod install
89 | ```
90 |
91 | #### 6. Run the following command to prepare binaries and static libraries for the project
92 |
93 | This command prepares the necessary binaries and static libraries for your React Native project by:
94 | - Validating required dependencies
95 | - Setting up platform-specific configurations (Android/iOS)
96 | - Running `mlc_llm package` for each platform to prepare model binaries
97 | - Handling environment variables and platform-specific requirements
98 |
99 | ```
100 | npx react-native-ai mlc-prepare
101 | ```
102 |
103 | #### 7. Add missing polyfills
104 |
105 | To make the Vercel AI SDK work in your project, you should include polyfills by first installing these pacakges:
106 |
107 | ```
108 | npm install @azure/core-asynciterator-polyfill @ungap/structured-clone web-streams-polyfill text-encoding
109 | ```
110 |
111 | and creating `polyfills.ts` file which will contain following imports:
112 |
113 | ```js
114 | import '@azure/core-asynciterator-polyfill';
115 | import structuredClone from '@ungap/structured-clone';
116 | import { polyfillGlobal } from 'react-native/Libraries/Utilities/PolyfillFunctions';
117 |
118 | const webStreamPolyfills = require('web-streams-polyfill/ponyfill/es6');
119 |
120 | polyfillGlobal('TextEncoder', () => require('text-encoding').TextEncoder);
121 | polyfillGlobal('ReadableStream', () => webStreamPolyfills.ReadableStream);
122 | polyfillGlobal('TransformStream', () => webStreamPolyfills.TransformStream);
123 | polyfillGlobal('WritableStream', () => webStreamPolyfills.WritableStream);
124 | polyfillGlobal('TextEncoderStream', () => webStreamPolyfills.TextEncoderStream);
125 | polyfillGlobal('structuredClone', () => structuredClone);
126 | ```
127 |
128 | Make sure to include them inside `index.js` before registering the root component.
129 |
130 | #### 8. Build the project! 🚀
131 |
132 | ## API
133 |
134 | This library provides first-class compatibility with the [Vercel AI SDK](https://sdk.vercel.ai/docs), allowing you to use familiar functions like `streamText` and `generateText` with locally run models.
135 |
136 | ### Key Functions
137 |
138 | * **`getModels(): Promise`**
139 | Retrieves a list of available models configured in your `mlc-config.json`.
140 |
141 | ```typescript
142 | import { getModels } from 'react-native-ai';
143 |
144 | async function listModels() {
145 | const models = await getModels();
146 | console.log('Available models:', models);
147 | }
148 | ```
149 |
150 | * **`downloadModel(modelId: string, callbacks?: DownloadCallbacks): Promise`**
151 | Downloads the specified model files. It accepts optional callbacks to track the download progress.
152 |
153 | ```typescript
154 | import { downloadModel, type DownloadProgress } from 'react-native-ai';
155 |
156 | await downloadModel('Mistral-7B-Instruct-v0.2-q3f16_1', {
157 | onStart: () => console.log('Download started...'),
158 | onProgress: (progress: DownloadProgress) => {
159 | console.log(`Downloading: ${progress.percentage.toFixed(2)}%`);
160 | },
161 | onComplete: () => console.log('Download complete!'),
162 | onError: (error: Error) => console.error('Download error:', error),
163 | });
164 | ```
165 |
166 | * **`prepareModel(modelId: string): Promise`**
167 | Prepares the downloaded model for use by loading it into memory, if the model is not on the device it'll fetch it. However we recommend using `downloadModel` before calling `prepareModel`.
168 |
169 | ```typescript
170 | import { prepareModel } from 'react-native-ai';
171 |
172 | await prepareModel('Mistral-7B-Instruct-v0.2-q3f16_1');
173 | console.log('Model is ready!');
174 | ```
175 |
176 | * **`getModel(modelId: string): LanguageModelV1`**
177 | Returns a model instance compatible with the Vercel AI SDK (`LanguageModelV1` interface). You can pass this instance directly to Vercel AI SDK functions.
178 |
179 | ### Usage with Vercel AI SDK
180 |
181 | Once a model is downloaded and prepared, you can use it with the Vercel AI SDK functions.
182 |
183 | ```typescript
184 | import { getModel, prepareModel } from 'react-native-ai';
185 | import { streamText, type CoreMessage } from 'ai';
186 |
187 | async function runInference(modelId: string, messages: CoreMessage[]) {
188 | await prepareModel(modelId);
189 | const llm = getModel(modelId);
190 |
191 | const { textStream } = streamText({
192 | model: llm,
193 | messages: messages,
194 | });
195 |
196 | for await (const textPart of textStream) {
197 | console.log(textPart);
198 | }
199 | }
200 |
201 | const exampleMessages: CoreMessage[] = [
202 | { role: 'user', content: 'Hello! Tell me a short story.' },
203 | ];
204 | runInference('Mistral-7B-Instruct-v0.2-q3f16_1', exampleMessages);
205 | ```
206 |
207 | This setup allows you to leverage the power of the Vercel AI SDK's unified API while running models directly on the user's device.
208 |
209 | ## Contributing
210 |
211 | Read the [contribution guidelines](/CONTRIBUTING.md) before contributing.
212 |
213 | ## Made with ❤️ at Callstack
214 |
215 | react-native-ai is an open source project and will always remain free to use. If you think it's cool, please star it 🌟.
216 |
217 | [Callstack][callstack-readme-with-love] is a group of React and React Native geeks, contact us at [hello@callstack.com](mailto:hello@callstack.com) if you need any help with these or just want to say hi!
218 |
219 | ---
220 |
221 | Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
222 |
223 | [callstack-readme-with-love]: https://callstack.com/?utm_source=github.com&utm_medium=referral&utm_campaign=react-native-ai&utm_term=readme-with-love
224 |
--------------------------------------------------------------------------------
/android/.editorconfig:
--------------------------------------------------------------------------------
1 | [*.{kt,kts}]
2 | indent_style=space
3 | indent_size=2
4 | continuation_indent_size=2
5 | insert_final_newline=true
6 | max_line_length=160
7 | ktlint_code_style=android_studio
8 | ktlint_standard=enabled
9 | ktlint_experimental=enabled
10 | ktlint_standard_filename=disabled # dont require PascalCase filenames
11 | ktlint_standard_no-wildcard-imports=disabled # allow .* imports
12 | ktlint_function_signature_body_expression_wrapping=multiline
13 | ij_kotlin_allow_trailing_comma_on_call_site=false
14 | ij_kotlin_allow_trailing_comma=false
15 |
--------------------------------------------------------------------------------
/android/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | // Buildscript is evaluated before everything else so we can't use getExtOrDefault
3 | def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["Ai_kotlinVersion"]
4 |
5 | repositories {
6 | google()
7 | mavenCentral()
8 | }
9 |
10 | dependencies {
11 | classpath "com.android.tools.build:gradle:7.2.1"
12 | // noinspection DifferentKotlinGradleVersion
13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14 | }
15 | }
16 |
17 | def reactNativeArchitectures() {
18 | def value = rootProject.getProperties().get("reactNativeArchitectures")
19 | return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
20 | }
21 |
22 | def isNewArchitectureEnabled() {
23 | return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
24 | }
25 |
26 | apply plugin: "com.android.library"
27 | apply plugin: "kotlin-android"
28 |
29 | if (isNewArchitectureEnabled()) {
30 | apply plugin: "com.facebook.react"
31 | }
32 |
33 | def getExtOrDefault(name) {
34 | return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Ai_" + name]
35 | }
36 |
37 | def getExtOrIntegerDefault(name) {
38 | return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Ai_" + name]).toInteger()
39 | }
40 |
41 | def supportsNamespace() {
42 | def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
43 | def major = parsed[0].toInteger()
44 | def minor = parsed[1].toInteger()
45 |
46 | // Namespace support was added in 7.3.0
47 | return (major == 7 && minor >= 3) || major >= 8
48 | }
49 |
50 | android {
51 | if (supportsNamespace()) {
52 | namespace "com.ai"
53 |
54 | sourceSets {
55 | main {
56 | manifest.srcFile "src/main/AndroidManifestNew.xml"
57 | }
58 | }
59 | }
60 |
61 | compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
62 |
63 | defaultConfig {
64 | minSdkVersion getExtOrIntegerDefault("minSdkVersion")
65 | targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
66 | buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
67 |
68 | }
69 |
70 | buildFeatures {
71 | buildConfig true
72 | }
73 |
74 | buildTypes {
75 | release {
76 | minifyEnabled false
77 | }
78 | }
79 |
80 | lintOptions {
81 | disable "GradleCompatible"
82 | }
83 |
84 | compileOptions {
85 | sourceCompatibility JavaVersion.VERSION_1_8
86 | targetCompatibility JavaVersion.VERSION_1_8
87 | }
88 |
89 | sourceSets {
90 | main {
91 | if (isNewArchitectureEnabled()) {
92 | java.srcDirs += [
93 | "src/newarch",
94 | // This is needed to build Kotlin project with NewArch enabled
95 | "${project.buildDir}/generated/source/codegen/java"
96 | ]
97 | } else {
98 | java.srcDirs += ["src/oldarch"]
99 | }
100 | }
101 | }
102 | }
103 |
104 | repositories {
105 | mavenCentral()
106 | google()
107 | }
108 |
109 | def kotlin_version = getExtOrDefault("kotlinVersion")
110 |
111 | dependencies {
112 | // For < 0.71, this will be from the local maven repo
113 | // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
114 | //noinspection GradleDynamicVersion
115 | implementation "com.facebook.react:react-native:+"
116 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
117 | implementation 'com.google.code.gson:gson:2.10.1'
118 | implementation project(":mlc4j")
119 | implementation 'androidx.core:core-ktx:1.10.1'
120 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
121 | implementation 'com.github.jeziellago:compose-markdown:0.5.2'
122 | implementation 'androidx.activity:activity-compose:1.7.1'
123 | implementation platform('androidx.compose:compose-bom:2022.10.00')
124 | implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1'
125 | implementation 'androidx.compose.material3:material3:1.1.0'
126 | implementation 'androidx.compose.material:material-icons-extended'
127 | implementation 'androidx.appcompat:appcompat:1.6.1'
128 | implementation 'androidx.navigation:navigation-compose:2.5.3'
129 | implementation 'com.google.code.gson:gson:2.10.1'
130 | implementation fileTree(dir: 'src/main/libs', include: ['*.aar', '*.jar'], exclude: [])
131 | testImplementation 'junit:junit:4.13.2'
132 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
133 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
134 | androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
135 | }
136 |
137 | if (isNewArchitectureEnabled()) {
138 | react {
139 | jsRootDir = file("../src/")
140 | libraryName = "Ai"
141 | codegenJavaPackageName = "com.ai"
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/android/gradle.properties:
--------------------------------------------------------------------------------
1 | Ai_kotlinVersion=1.7.0
2 | Ai_minSdkVersion=21
3 | Ai_targetSdkVersion=31
4 | Ai_compileSdkVersion=31
5 | Ai_ndkversion=21.4.7075529
6 |
--------------------------------------------------------------------------------
/android/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
--------------------------------------------------------------------------------
/android/src/main/AndroidManifestNew.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/android/src/main/java/com/ai/AiModule.kt:
--------------------------------------------------------------------------------
1 | package com.ai
2 |
3 | import ai.mlc.mlcllm.OpenAIProtocol
4 | import ai.mlc.mlcllm.OpenAIProtocol.ChatCompletionMessage
5 | import android.os.Environment
6 | import android.util.Log
7 | import com.facebook.react.bridge.*
8 | import com.facebook.react.bridge.ReactContext.RCTDeviceEventEmitter
9 | import com.facebook.react.module.annotations.ReactModule
10 | import com.facebook.react.turbomodule.core.interfaces.TurboModule
11 | import com.google.gson.Gson
12 | import com.google.gson.annotations.SerializedName
13 | import java.io.File
14 | import java.io.FileOutputStream
15 | import java.net.URL
16 | import java.nio.channels.Channels
17 | import java.util.UUID
18 | import kotlinx.coroutines.CoroutineScope
19 | import kotlinx.coroutines.Dispatchers
20 | import kotlinx.coroutines.launch
21 | import kotlinx.coroutines.withContext
22 |
23 | @ReactModule(name = AiModule.NAME)
24 | class AiModule(reactContext: ReactApplicationContext) :
25 | ReactContextBaseJavaModule(reactContext),
26 | TurboModule {
27 |
28 | override fun getName(): String = NAME
29 |
30 | companion object {
31 | const val NAME = "Ai"
32 |
33 | const val APP_CONFIG_FILENAME = "mlc-app-config.json"
34 | const val MODEL_CONFIG_FILENAME = "mlc-chat-config.json"
35 | const val PARAMS_CONFIG_FILENAME = "ndarray-cache.json"
36 | const val MODEL_URL_SUFFIX = "/resolve/main/"
37 | }
38 |
39 | private var appConfig = AppConfig(
40 | emptyList().toMutableList(),
41 | emptyList().toMutableList()
42 | )
43 | private val gson = Gson()
44 | private lateinit var chat: Chat
45 |
46 | private fun getAppConfig(): AppConfig {
47 | val appConfigFile = File(reactApplicationContext.applicationContext.getExternalFilesDir(""), APP_CONFIG_FILENAME)
48 |
49 | val jsonString: String = if (appConfigFile.exists()) {
50 | appConfigFile.readText()
51 | } else {
52 | reactApplicationContext.applicationContext.assets.open(APP_CONFIG_FILENAME).bufferedReader().use { it.readText() }
53 | }
54 |
55 | return gson.fromJson(jsonString, AppConfig::class.java)
56 | }
57 |
58 | private suspend fun getModelConfig(modelRecord: ModelRecord): ModelConfig {
59 | downloadModelConfig(modelRecord)
60 |
61 | val modelDirFile = File(reactApplicationContext.getExternalFilesDir(""), modelRecord.modelId)
62 | val modelConfigFile = File(modelDirFile, MODEL_CONFIG_FILENAME)
63 |
64 | val jsonString: String = if (modelConfigFile.exists()) {
65 | modelConfigFile.readText()
66 | } else {
67 | throw Error("Requested model config not found")
68 | }
69 |
70 | val modelConfig = gson.fromJson(jsonString, ModelConfig::class.java)
71 |
72 | modelConfig.apply {
73 | modelId = modelRecord.modelId
74 | modelUrl = modelRecord.modelUrl
75 | modelLib = modelRecord.modelLib
76 | }
77 |
78 | return modelConfig
79 | }
80 |
81 | @ReactMethod
82 | fun getModel(name: String, promise: Promise) {
83 | appConfig = getAppConfig()
84 |
85 | val modelConfig = appConfig.modelList.find { modelRecord -> modelRecord.modelId == name }
86 |
87 | if (modelConfig == null) {
88 | promise.reject("Model not found", "Didn't find the model")
89 | return
90 | }
91 |
92 | // Return a JSON object with details
93 | val modelConfigInstance = Arguments.createMap().apply {
94 | putString("modelId", modelConfig.modelId)
95 | putString("modelLib", modelConfig.modelLib) // Add more fields if needed
96 | }
97 |
98 | promise.resolve(modelConfigInstance)
99 | }
100 |
101 | @ReactMethod
102 | fun getModels(promise: Promise) {
103 | try {
104 | appConfig = getAppConfig()
105 | appConfig.modelLibs = emptyList().toMutableList()
106 |
107 | val modelsArray = Arguments.createArray().apply {
108 | for (modelRecord in appConfig.modelList) {
109 | pushMap(Arguments.createMap().apply { putString("model_id", modelRecord.modelId) })
110 | }
111 | }
112 |
113 | promise.resolve(modelsArray)
114 | } catch (e: Exception) {
115 | promise.reject("JSON_ERROR", "Error creating JSON array", e)
116 | }
117 | }
118 |
119 | @ReactMethod
120 | fun doGenerate(instanceId: String, messages: ReadableArray, promise: Promise) {
121 | val messageList = mutableListOf()
122 |
123 | for (i in 0 until messages.size()) {
124 | val messageMap = messages.getMap(i) // Extract ReadableMap
125 |
126 | val role = if (messageMap.getString("role") == "user") OpenAIProtocol.ChatCompletionRole.user else OpenAIProtocol.ChatCompletionRole.assistant
127 | val content = messageMap.getString("content") ?: ""
128 |
129 | messageList.add(ChatCompletionMessage(role, content))
130 | }
131 |
132 | CoroutineScope(Dispatchers.Main).launch {
133 | try {
134 | chat.generateResponse(
135 | messageList,
136 | callback = object : Chat.GenerateCallback {
137 | override fun onMessageReceived(message: String) {
138 | promise.resolve(message)
139 | }
140 | }
141 | )
142 | } catch (e: Exception) {
143 | Log.e("AI", "Error generating response", e)
144 | }
145 | }
146 | }
147 |
148 | @ReactMethod
149 | fun doStream(instanceId: String, messages: ReadableArray, promise: Promise) {
150 | val messageList = mutableListOf()
151 |
152 | for (i in 0 until messages.size()) {
153 | val messageMap = messages.getMap(i) // Extract ReadableMap
154 |
155 | val role = if (messageMap.getString("role") == "user") OpenAIProtocol.ChatCompletionRole.user else OpenAIProtocol.ChatCompletionRole.assistant
156 | val content = messageMap.getString("content") ?: ""
157 |
158 | messageList.add(ChatCompletionMessage(role, content))
159 | }
160 | CoroutineScope(Dispatchers.Main).launch {
161 | chat.streamResponse(
162 | messageList,
163 | callback = object : Chat.StreamCallback {
164 | override fun onUpdate(message: String) {
165 | val event: WritableMap = Arguments.createMap().apply {
166 | putString("content", message)
167 | }
168 | sendEvent("onChatUpdate", event)
169 | }
170 |
171 | override fun onFinished(message: String) {
172 | val event: WritableMap = Arguments.createMap().apply {
173 | putString("content", message)
174 | }
175 | sendEvent("onChatComplete", event)
176 | }
177 | }
178 | )
179 | }
180 | promise.resolve(null)
181 | }
182 |
183 | @ReactMethod
184 | fun downloadModel(instanceId: String, promise: Promise) {
185 | CoroutineScope(Dispatchers.IO).launch {
186 | try {
187 | val appConfig = getAppConfig()
188 | val modelRecord = appConfig.modelList.find { modelRecord -> modelRecord.modelId == instanceId }
189 | if (modelRecord == null) {
190 | throw Error("There's no record for requested model")
191 | }
192 |
193 | val modelConfig = getModelConfig(modelRecord)
194 |
195 | val modelDir = File(reactApplicationContext.getExternalFilesDir(""), modelConfig.modelId)
196 |
197 | val modelState = ModelState(modelConfig, modelDir)
198 |
199 | modelState.initialize()
200 |
201 | sendEvent("onDownloadStart", null)
202 |
203 | CoroutineScope(Dispatchers.IO).launch {
204 | modelState.progress.collect { newValue ->
205 | val event: WritableMap = Arguments.createMap().apply {
206 | putDouble("percentage", (newValue.toDouble() / modelState.total.intValue) * 100)
207 | }
208 | sendEvent("onDownloadProgress", event)
209 | }
210 | }
211 |
212 | modelState.download()
213 |
214 | sendEvent("onDownloadComplete", null)
215 |
216 | withContext(Dispatchers.Main) { promise.resolve("Model downloaded: $instanceId") }
217 | } catch (e: Exception) {
218 | sendEvent("onDownloadError", e.message ?: "Unknown error")
219 | withContext(Dispatchers.Main) { promise.reject("MODEL_ERROR", "Error downloading model", e) }
220 | }
221 | }
222 | }
223 |
224 | private fun sendEvent(eventName: String, data: Any?) {
225 | reactApplicationContext.getJSModule(RCTDeviceEventEmitter::class.java)?.emit(eventName, data)
226 | }
227 |
228 | @ReactMethod
229 | fun prepareModel(instanceId: String, promise: Promise) {
230 | CoroutineScope(Dispatchers.IO).launch {
231 | try {
232 | val appConfig = getAppConfig()
233 |
234 | val modelRecord = appConfig.modelList.find { modelRecord -> modelRecord.modelId == instanceId }
235 |
236 | if (modelRecord == null) {
237 | throw Error("There's no record for requested model")
238 | }
239 | val modelConfig = getModelConfig(modelRecord)
240 |
241 | val modelDir = File(reactApplicationContext.getExternalFilesDir(""), modelConfig.modelId)
242 |
243 | val modelState = ModelState(modelConfig, modelDir)
244 | modelState.initialize()
245 | modelState.download()
246 |
247 | chat = Chat(modelConfig, modelDir)
248 |
249 | withContext(Dispatchers.Main) { promise.resolve("Model prepared: $instanceId") }
250 | } catch (e: Exception) {
251 | withContext(Dispatchers.Main) { promise.reject("MODEL_ERROR", "Error preparing model", e) }
252 | }
253 | }
254 | }
255 |
256 | private suspend fun downloadModelConfig(modelRecord: ModelRecord) {
257 | withContext(Dispatchers.IO) {
258 | // Don't download if config is downloaded already
259 | val modelFile = File(reactApplicationContext.getExternalFilesDir(""), modelRecord.modelId)
260 | if (modelFile.exists()) {
261 | return@withContext
262 | }
263 |
264 | // Prepare temp file for streaming
265 | val url = URL("${modelRecord.modelUrl}${MODEL_URL_SUFFIX}$MODEL_CONFIG_FILENAME")
266 | val tempId = UUID.randomUUID().toString()
267 | val tempFile = File(
268 | reactApplicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
269 | tempId
270 | )
271 |
272 | // Download
273 | url.openStream().use {
274 | Channels.newChannel(it).use { src ->
275 | FileOutputStream(tempFile).use { fileOutputStream ->
276 | fileOutputStream.channel.transferFrom(src, 0, Long.MAX_VALUE)
277 | }
278 | }
279 | }
280 | require(tempFile.exists())
281 |
282 | // Create object form config
283 | val modelConfigString = tempFile.readText()
284 | val modelConfig = gson.fromJson(modelConfigString, ModelConfig::class.java).apply {
285 | modelId = modelRecord.modelId
286 | modelLib = modelRecord.modelLib
287 | estimatedVramBytes = modelRecord.estimatedVramBytes
288 | }
289 |
290 | // Copy to config location and remove temp file
291 | val modelDirFile = File(reactApplicationContext.getExternalFilesDir(""), modelConfig.modelId)
292 | val modelConfigFile = File(modelDirFile, MODEL_CONFIG_FILENAME)
293 | tempFile.copyTo(modelConfigFile, overwrite = true)
294 | tempFile.delete()
295 |
296 | return@withContext
297 | }
298 | }
299 | }
300 |
301 | enum class ModelChatState {
302 | Generating,
303 | Resetting,
304 | Reloading,
305 | Terminating,
306 | Ready,
307 | Failed
308 | }
309 |
310 | data class MessageData(val role: String, val text: String, val id: UUID = UUID.randomUUID())
311 |
312 | data class ModelConfig(
313 | @SerializedName("model_lib") var modelLib: String,
314 | @SerializedName("model_id") var modelId: String,
315 | @SerializedName("model_url") var modelUrl: String,
316 | @SerializedName("estimated_vram_bytes") var estimatedVramBytes: Long?,
317 | @SerializedName("tokenizer_files") val tokenizerFiles: List,
318 | @SerializedName("context_window_size") val contextWindowSize: Int,
319 | @SerializedName("prefill_chunk_size") val prefillChunkSize: Int
320 | )
321 |
322 | data class AppConfig(@SerializedName("model_libs") var modelLibs: MutableList, @SerializedName("model_list") val modelList: MutableList)
323 |
324 | data class ModelRecord(
325 | @SerializedName("model_url") val modelUrl: String,
326 | @SerializedName("model_id") val modelId: String,
327 | @SerializedName("estimated_vram_bytes") val estimatedVramBytes: Long?,
328 | @SerializedName("model_lib") val modelLib: String
329 | )
330 |
331 | data class DownloadTask(val url: URL, val file: File)
332 |
333 | data class ParamsConfig(@SerializedName("records") val paramsRecords: List)
334 |
335 | data class ParamsRecord(@SerializedName("dataPath") val dataPath: String)
336 |
--------------------------------------------------------------------------------
/android/src/main/java/com/ai/AiPackage.kt:
--------------------------------------------------------------------------------
1 | package com.ai
2 |
3 | import com.facebook.react.TurboReactPackage
4 | import com.facebook.react.bridge.NativeModule
5 | import com.facebook.react.bridge.ReactApplicationContext
6 | import com.facebook.react.module.model.ReactModuleInfo
7 | import com.facebook.react.module.model.ReactModuleInfoProvider
8 | import java.util.HashMap
9 |
10 | class AiPackage : TurboReactPackage() {
11 | override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
12 | if (name == AiModule.NAME) {
13 | AiModule(reactContext)
14 | } else {
15 | null
16 | }
17 |
18 | override fun getReactModuleInfoProvider(): ReactModuleInfoProvider =
19 | ReactModuleInfoProvider {
20 | val moduleInfos: MutableMap = HashMap()
21 | val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
22 | moduleInfos[AiModule.NAME] = ReactModuleInfo(
23 | AiModule.NAME,
24 | AiModule.NAME,
25 | // canOverrideExistingModule
26 | false,
27 | // needsEagerInit
28 | false,
29 | // hasConstants
30 | true,
31 | // isCxxModule
32 | false,
33 | // isTurboModule
34 | isTurboModule
35 | )
36 |
37 | moduleInfos
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/android/src/main/java/com/ai/ChatState.kt:
--------------------------------------------------------------------------------
1 | package com.ai
2 |
3 | import ai.mlc.mlcllm.MLCEngine
4 | import ai.mlc.mlcllm.OpenAIProtocol
5 | import ai.mlc.mlcllm.OpenAIProtocol.ChatCompletionMessage
6 | import java.io.File
7 | import java.util.concurrent.Executors
8 | import kotlinx.coroutines.CoroutineScope
9 | import kotlinx.coroutines.Dispatchers
10 | import kotlinx.coroutines.Job
11 | import kotlinx.coroutines.channels.toList
12 | import kotlinx.coroutines.launch
13 |
14 | class Chat(modelConfig: ModelConfig, modelDir: File) {
15 | private val engine = MLCEngine()
16 | private val executorService = Executors.newSingleThreadExecutor()
17 | private val viewModelScope = CoroutineScope(Dispatchers.Main + Job())
18 |
19 | init {
20 | engine.unload()
21 | engine.reload(modelDir.path, modelConfig.modelLib)
22 | }
23 |
24 | fun generateResponse(messages: MutableList, callback: GenerateCallback) {
25 | executorService.submit {
26 | viewModelScope.launch {
27 | val chatResponse = engine.chat.completions.create(messages = messages)
28 | val response = chatResponse.toList().joinToString("") { it.choices.joinToString("") { it.delta.content?.text ?: "" } }
29 | callback.onMessageReceived(response)
30 | }
31 | }
32 | }
33 |
34 | fun streamResponse(messages: MutableList, callback: StreamCallback) {
35 | executorService.submit {
36 | viewModelScope.launch {
37 | val chatResponse = engine.chat.completions.create(messages = messages, stream_options = OpenAIProtocol.StreamOptions(include_usage = true))
38 |
39 | var finishReasonLength = false
40 | var streamingText = ""
41 |
42 | for (res in chatResponse) {
43 | for (choice in res.choices) {
44 | choice.delta.content?.let { content ->
45 | streamingText = content.asText()
46 | }
47 | choice.finish_reason?.let { finishReason ->
48 | if (finishReason == "length") {
49 | finishReasonLength = true
50 | }
51 | }
52 | }
53 |
54 | callback.onUpdate(streamingText)
55 | if (finishReasonLength) {
56 | streamingText = " [output truncated due to context length limit...]"
57 | callback.onUpdate(streamingText)
58 | }
59 | }
60 | callback.onFinished(streamingText)
61 | }
62 | }
63 | }
64 |
65 | interface GenerateCallback {
66 | fun onMessageReceived(message: String)
67 | }
68 |
69 | interface StreamCallback {
70 | fun onUpdate(message: String)
71 | fun onFinished(message: String)
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/android/src/main/java/com/ai/ModelState.kt:
--------------------------------------------------------------------------------
1 | package com.ai
2 |
3 | import androidx.compose.runtime.mutableIntStateOf
4 | import com.ai.AiModule.Companion.MODEL_CONFIG_FILENAME
5 | import com.ai.AiModule.Companion.MODEL_URL_SUFFIX
6 | import com.ai.AiModule.Companion.PARAMS_CONFIG_FILENAME
7 | import com.google.gson.Gson
8 | import java.io.File
9 | import java.io.FileOutputStream
10 | import java.net.URL
11 | import java.nio.channels.Channels
12 | import java.util.UUID
13 | import kotlinx.coroutines.Dispatchers
14 | import kotlinx.coroutines.flow.MutableStateFlow
15 | import kotlinx.coroutines.withContext
16 |
17 | class ModelState(private val modelConfig: ModelConfig, private val modelDir: File) {
18 | private var paramsConfig = ParamsConfig(emptyList())
19 | val progress = MutableStateFlow(0)
20 | val total = mutableIntStateOf(1)
21 | val id: UUID = UUID.randomUUID()
22 | private val remainingTasks = emptySet().toMutableSet()
23 | private val downloadingTasks = emptySet().toMutableSet()
24 | private val maxDownloadTasks = 3
25 | private val gson = Gson()
26 |
27 | suspend fun initialize() {
28 | val paramsConfigFile = File(modelDir, PARAMS_CONFIG_FILENAME)
29 | if (!paramsConfigFile.exists()) {
30 | downloadParamsConfig()
31 | }
32 |
33 | loadParamsConfig()
34 | indexModel()
35 | }
36 |
37 | private fun loadParamsConfig() {
38 | val paramsConfigFile = File(modelDir, PARAMS_CONFIG_FILENAME)
39 | require(paramsConfigFile.exists())
40 | val jsonString = paramsConfigFile.readText()
41 | paramsConfig = gson.fromJson(jsonString, ParamsConfig::class.java)
42 | }
43 |
44 | private suspend fun downloadParamsConfig() {
45 | withContext(Dispatchers.IO) {
46 | val url = URL("${modelConfig.modelUrl}$MODEL_URL_SUFFIX$PARAMS_CONFIG_FILENAME")
47 | val tempId = UUID.randomUUID().toString()
48 | val tempFile = File(modelDir, tempId)
49 | url.openStream().use {
50 | Channels.newChannel(it).use { src ->
51 | FileOutputStream(tempFile).use { fileOutputStream ->
52 | fileOutputStream.channel.transferFrom(src, 0, Long.MAX_VALUE)
53 | }
54 | }
55 | }
56 | require(tempFile.exists())
57 | val paramsConfigFile = File(modelDir, PARAMS_CONFIG_FILENAME)
58 | tempFile.renameTo(paramsConfigFile)
59 | require(paramsConfigFile.exists())
60 | }
61 | }
62 |
63 | suspend fun download() {
64 | while (remainingTasks.isNotEmpty() && downloadingTasks.size < maxDownloadTasks) {
65 | val downloadTask = remainingTasks.first()
66 | remainingTasks.remove(downloadTask)
67 | handleNewDownload(downloadTask)
68 | }
69 | }
70 |
71 | private suspend fun handleNewDownload(downloadTask: DownloadTask) {
72 | require(!downloadingTasks.contains(downloadTask))
73 | downloadingTasks.add(downloadTask)
74 |
75 | withContext(Dispatchers.IO) {
76 | val tempId = UUID.randomUUID().toString()
77 | val tempFile = File(modelDir, tempId)
78 |
79 | downloadTask.url.openStream().use {
80 | Channels.newChannel(it).use { src ->
81 | FileOutputStream(tempFile).use { fileOutputStream ->
82 | fileOutputStream.channel.transferFrom(src, 0, Long.MAX_VALUE)
83 | }
84 | }
85 | }
86 | require(tempFile.exists())
87 | tempFile.renameTo(downloadTask.file)
88 | require(downloadTask.file.exists())
89 |
90 | handleFinishDownload(downloadTask)
91 | }
92 | }
93 |
94 | private fun handleFinishDownload(downloadTask: DownloadTask) {
95 | remainingTasks.remove(downloadTask)
96 | downloadingTasks.remove(downloadTask)
97 | ++progress.value
98 | }
99 |
100 | private fun clear() {
101 | val files = modelDir.listFiles { dir, name ->
102 | !(dir == modelDir && name == MODEL_CONFIG_FILENAME)
103 | }
104 | require(files != null)
105 | for (file in files) {
106 | file.deleteRecursively()
107 | require(!file.exists())
108 | }
109 | val modelConfigFile = File(modelDir, MODEL_CONFIG_FILENAME)
110 | require(modelConfigFile.exists())
111 | indexModel()
112 | }
113 |
114 | private fun indexModel() {
115 | progress.value = 0
116 | total.intValue = modelConfig.tokenizerFiles.size + paramsConfig.paramsRecords.size
117 |
118 | // Adding Tokenizer to download tasks
119 | for (tokenizerFilename in modelConfig.tokenizerFiles) {
120 | val file = File(modelDir, tokenizerFilename)
121 | if (file.exists()) {
122 | ++progress.value
123 | } else {
124 | remainingTasks.add(
125 | DownloadTask(
126 | URL("${modelConfig.modelUrl}$MODEL_URL_SUFFIX$tokenizerFilename"),
127 | file
128 | )
129 | )
130 | }
131 | }
132 |
133 | // Adding params to download tasks
134 | for (paramsRecord in paramsConfig.paramsRecords) {
135 | val file = File(modelDir, paramsRecord.dataPath)
136 | if (file.exists()) {
137 | ++progress.value
138 | } else {
139 | remainingTasks.add(
140 | DownloadTask(
141 | URL("${modelConfig.modelUrl}$MODEL_URL_SUFFIX${paramsRecord.dataPath}"),
142 | file
143 | )
144 | )
145 | }
146 | }
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/android/src/newarch/AiSpec.kt:
--------------------------------------------------------------------------------
1 | package com.ai
2 |
3 | import com.facebook.react.bridge.ReactApplicationContext
4 |
5 | abstract class AiSpec internal constructor(context: ReactApplicationContext) : NativeAiSpec(context)
6 |
--------------------------------------------------------------------------------
/android/src/oldarch/AiSpec.kt:
--------------------------------------------------------------------------------
1 | package com.ai
2 |
3 | import com.facebook.react.bridge.Promise
4 | import com.facebook.react.bridge.ReactApplicationContext
5 | import com.facebook.react.bridge.ReactContextBaseJavaModule
6 |
7 | abstract class AiSpec internal constructor(context: ReactApplicationContext) : ReactContextBaseJavaModule(context) {
8 |
9 | abstract fun getModel(name: String, promise: Promise)
10 | abstract fun getModels(promise: Promise)
11 | abstract fun doGenerate(instanceId: String, text: String, promise: Promise)
12 | abstract fun doStream(instanceId: String, text: String, promise: Promise)
13 | }
14 |
--------------------------------------------------------------------------------
/babel.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | presets: ['module:@react-native/babel-preset'],
3 | };
4 |
--------------------------------------------------------------------------------
/example/.watchmanconfig:
--------------------------------------------------------------------------------
1 | {}
2 |
--------------------------------------------------------------------------------
/example/android/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id("com.android.application")
3 | id("org.jetbrains.kotlin.android")
4 | id("com.facebook.react")
5 | id("org.jetbrains.kotlin.plugin.serialization")
6 | }
7 |
8 | /**
9 | * This is the configuration block to customize your React Native Android app.
10 | * By default you don't need to apply any configuration, just uncomment the lines you need.
11 | */
12 | react {
13 | /* Folders */
14 | // The root of your project, i.e. where "package.json" lives. Default is '..'
15 | // root = file("../")
16 | // The folder where the react-native NPM package is. Default is ../node_modules/react-native
17 | // reactNativeDir = file("../node_modules/react-native")
18 | // The folder where the react-native Codegen package is. Default is ../node_modules/@react-native/codegen
19 | // codegenDir = file("../node_modules/@react-native/codegen")
20 | // The cli.js file which is the React Native CLI entrypoint. Default is ../node_modules/react-native/cli.js
21 | // cliFile = file("../node_modules/react-native/cli.js")
22 |
23 | /* Variants */
24 | // The list of variants to that are debuggable. For those we're going to
25 | // skip the bundling of the JS bundle and the assets. By default is just 'debug'.
26 | // If you add flavors like lite, prod, etc. you'll have to list your debuggableVariants.
27 | // debuggableVariants = ["liteDebug", "prodDebug"]
28 |
29 | /* Bundling */
30 | // A list containing the node command and its flags. Default is just 'node'.
31 | // nodeExecutableAndArgs = ["node"]
32 | //
33 | // The command to run when bundling. By default is 'bundle'
34 | // bundleCommand = "ram-bundle"
35 | //
36 | // The path to the CLI configuration file. Default is empty.
37 | // bundleConfig = file(../rn-cli.config.js)
38 | //
39 | // The name of the generated asset file containing your JS bundle
40 | // bundleAssetName = "MyApplication.android.bundle"
41 | //
42 | // The entry file for bundle generation. Default is 'index.android.js' or 'index.js'
43 | // entryFile = file("../js/MyApplication.android.js")
44 | //
45 | // A list of extra flags to pass to the 'bundle' commands.
46 | // See https://github.com/react-native-community/cli/blob/main/docs/commands.md#bundle
47 | // extraPackagerArgs = []
48 |
49 | /* Hermes Commands */
50 | // The hermes compiler command to run. By default it is 'hermesc'
51 | // hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
52 | //
53 | // The list of flags to pass to the Hermes compiler. By default is "-O", "-output-source-map"
54 | // hermesFlags = ["-O", "-output-source-map"]
55 | }
56 |
57 | /**
58 | * Set this to true to Run Proguard on Release builds to minify the Java bytecode.
59 | */
60 | def enableProguardInReleaseBuilds = false
61 |
62 | /**
63 | * The preferred build flavor of JavaScriptCore (JSC)
64 | *
65 | * For example, to use the international variant, you can use:
66 | * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
67 | *
68 | * The international variant includes ICU i18n library and necessary data
69 | * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
70 | * give correct results when using with locales other than en-US. Note that
71 | * this variant is about 6MiB larger per architecture than default.
72 | */
73 | def jscFlavor = 'org.webkit:android-jsc:+'
74 |
75 | android {
76 | ndkVersion rootProject.ext.ndkVersion
77 | buildToolsVersion rootProject.ext.buildToolsVersion
78 | compileSdk rootProject.ext.compileSdkVersion
79 |
80 | namespace "com.aiexample"
81 | defaultConfig {
82 | applicationId "com.aiexample"
83 | minSdkVersion rootProject.ext.minSdkVersion
84 | targetSdkVersion rootProject.ext.targetSdkVersion
85 | versionCode 1
86 | versionName "1.0"
87 | }
88 | signingConfigs {
89 | debug {
90 | storeFile file('debug.keystore')
91 | storePassword 'android'
92 | keyAlias 'androiddebugkey'
93 | keyPassword 'android'
94 | }
95 | }
96 | buildTypes {
97 | debug {
98 | signingConfig signingConfigs.debug
99 | }
100 | release {
101 | // Caution! In production, you need to generate your own keystore file.
102 | // see https://reactnative.dev/docs/signed-apk-android.
103 | signingConfig signingConfigs.debug
104 | minifyEnabled enableProguardInReleaseBuilds
105 | proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
106 | }
107 | }
108 | }
109 |
110 | dependencies {
111 | // The version of react-native is set by the React Native Gradle Plugin
112 | implementation("com.facebook.react:react-android")
113 | implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
114 |
115 | if (hermesEnabled.toBoolean()) {
116 | implementation("com.facebook.react:hermes-android")
117 | } else {
118 | implementation jscFlavor
119 | }
120 | }
121 |
122 | apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
123 |
--------------------------------------------------------------------------------
/example/android/app/debug.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/debug.keystore
--------------------------------------------------------------------------------
/example/android/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /usr/local/Cellar/android-sdk/24.3.3/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
--------------------------------------------------------------------------------
/example/android/app/src/debug/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
10 |
--------------------------------------------------------------------------------
/example/android/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
12 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/example/android/app/src/main/java/com/aiexample/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.aiexample
2 |
3 | import com.facebook.react.ReactActivity
4 | import com.facebook.react.ReactActivityDelegate
5 | import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
6 | import com.facebook.react.defaults.DefaultReactActivityDelegate
7 |
8 | class MainActivity : ReactActivity() {
9 | /**
10 | * Returns the name of the main component registered from JavaScript. This is used to schedule
11 | * rendering of the component.
12 | */
13 | override fun getMainComponentName(): String = "AiExample"
14 |
15 | /**
16 | * Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
17 | * which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
18 | */
19 | override fun createReactActivityDelegate(): ReactActivityDelegate = DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
20 | }
21 |
--------------------------------------------------------------------------------
/example/android/app/src/main/java/com/aiexample/MainApplication.kt:
--------------------------------------------------------------------------------
1 | package com.aiexample
2 |
3 | import android.app.Application
4 | import com.facebook.react.PackageList
5 | import com.facebook.react.ReactApplication
6 | import com.facebook.react.ReactHost
7 | import com.facebook.react.ReactNativeHost
8 | import com.facebook.react.ReactPackage
9 | import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
10 | import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
11 | import com.facebook.react.defaults.DefaultReactNativeHost
12 | import com.facebook.soloader.SoLoader
13 |
14 | class MainApplication :
15 | Application(),
16 | ReactApplication {
17 | override val reactNativeHost: ReactNativeHost =
18 | object : DefaultReactNativeHost(this) {
19 | override fun getPackages(): List =
20 | PackageList(this).packages.apply {
21 | // Packages that cannot be autolinked yet can be added manually here, for example:
22 | // add(MyReactNativePackage())
23 | }
24 |
25 | override fun getJSMainModuleName(): String = "index"
26 |
27 | override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
28 |
29 | override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
30 | override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
31 | }
32 |
33 | override val reactHost: ReactHost
34 | get() = getDefaultReactHost(applicationContext, reactNativeHost)
35 |
36 | override fun onCreate() {
37 | super.onCreate()
38 | SoLoader.init(this, false)
39 | if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
40 | // If you opted-in for the New Architecture, we load the native entry point for this app.
41 | load()
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/example/android/app/src/main/res/drawable/rn_edit_text_material.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
22 |
23 |
24 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/example/android/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AiExample
3 |
4 |
--------------------------------------------------------------------------------
/example/android/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/example/android/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext {
3 | buildToolsVersion = "34.0.0"
4 | minSdkVersion = 23
5 | compileSdkVersion = 34
6 | targetSdkVersion = 34
7 | ndkVersion = "26.1.10909125"
8 | kotlinVersion = "1.9.22"
9 | }
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | dependencies {
15 | classpath("com.android.tools.build:gradle")
16 | classpath("com.facebook.react:react-native-gradle-plugin")
17 | classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
18 | classpath("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
19 | }
20 | }
21 |
22 | plugins {
23 | id("com.facebook.react.rootproject")
24 | id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.0' apply false
25 | }
26 |
--------------------------------------------------------------------------------
/example/android/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx512m -XX:MaxMetaspaceSize=256m
13 | org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
19 |
20 | # AndroidX package structure to make it clearer which packages are bundled with the
21 | # Android operating system, and which are packaged with your app's APK
22 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
23 | android.useAndroidX=true
24 | # Automatically convert third-party libraries to use AndroidX
25 | android.enableJetifier=true
26 |
27 | # Use this property to specify which architecture you want to build.
28 | # You can also override it from the CLI using
29 | # ./gradlew -PreactNativeArchitectures=x86_64
30 | reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
31 |
32 | # Use this property to enable support to the new architecture.
33 | # This will allow you to use TurboModules and the Fabric render in
34 | # your application. You should enable this flag either if you want
35 | # to write custom TurboModules/Fabric components OR use libraries that
36 | # are providing them.
37 | newArchEnabled=false
38 |
39 | # Use this property to enable or disable the Hermes JS engine.
40 | # If set to false, you will be using JSC instead.
41 | hermesEnabled=true
42 | kotlin.jvm.target.validation.mode = IGNORE
43 |
--------------------------------------------------------------------------------
/example/android/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/android/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/example/android/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
4 | networkTimeout=10000
5 | validateDistributionUrl=true
6 | zipStoreBase=GRADLE_USER_HOME
7 | zipStorePath=wrapper/dists
8 |
--------------------------------------------------------------------------------
/example/android/gradlew:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | #
4 | # Copyright © 2015-2021 the original authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | #
21 | # Gradle start up script for POSIX generated by Gradle.
22 | #
23 | # Important for running:
24 | #
25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
26 | # noncompliant, but you have some other compliant shell such as ksh or
27 | # bash, then to run this script, type that shell name before the whole
28 | # command line, like:
29 | #
30 | # ksh Gradle
31 | #
32 | # Busybox and similar reduced shells will NOT work, because this script
33 | # requires all of these POSIX shell features:
34 | # * functions;
35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
37 | # * compound commands having a testable exit status, especially «case»;
38 | # * various built-in commands including «command», «set», and «ulimit».
39 | #
40 | # Important for patching:
41 | #
42 | # (2) This script targets any POSIX shell, so it avoids extensions provided
43 | # by Bash, Ksh, etc; in particular arrays are avoided.
44 | #
45 | # The "traditional" practice of packing multiple parameters into a
46 | # space-separated string is a well documented source of bugs and security
47 | # problems, so this is (mostly) avoided, by progressively accumulating
48 | # options in "$@", and eventually passing that to Java.
49 | #
50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
52 | # see the in-line comments for details.
53 | #
54 | # There are tweaks for specific operating systems such as AIX, CygWin,
55 | # Darwin, MinGW, and NonStop.
56 | #
57 | # (3) This script is generated from the Groovy template
58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
59 | # within the Gradle project.
60 | #
61 | # You can find Gradle at https://github.com/gradle/gradle/.
62 | #
63 | ##############################################################################
64 |
65 | # Attempt to set APP_HOME
66 |
67 | # Resolve links: $0 may be a link
68 | app_path=$0
69 |
70 | # Need this for daisy-chained symlinks.
71 | while
72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
73 | [ -h "$app_path" ]
74 | do
75 | ls=$( ls -ld "$app_path" )
76 | link=${ls#*' -> '}
77 | case $link in #(
78 | /*) app_path=$link ;; #(
79 | *) app_path=$APP_HOME$link ;;
80 | esac
81 | done
82 |
83 | # This is normally unused
84 | # shellcheck disable=SC2034
85 | APP_BASE_NAME=${0##*/}
86 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
87 | APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
88 |
89 | # Use the maximum available, or set MAX_FD != -1 to use that value.
90 | MAX_FD=maximum
91 |
92 | warn () {
93 | echo "$*"
94 | } >&2
95 |
96 | die () {
97 | echo
98 | echo "$*"
99 | echo
100 | exit 1
101 | } >&2
102 |
103 | # OS specific support (must be 'true' or 'false').
104 | cygwin=false
105 | msys=false
106 | darwin=false
107 | nonstop=false
108 | case "$( uname )" in #(
109 | CYGWIN* ) cygwin=true ;; #(
110 | Darwin* ) darwin=true ;; #(
111 | MSYS* | MINGW* ) msys=true ;; #(
112 | NONSTOP* ) nonstop=true ;;
113 | esac
114 |
115 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
116 |
117 |
118 | # Determine the Java command to use to start the JVM.
119 | if [ -n "$JAVA_HOME" ] ; then
120 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
121 | # IBM's JDK on AIX uses strange locations for the executables
122 | JAVACMD=$JAVA_HOME/jre/sh/java
123 | else
124 | JAVACMD=$JAVA_HOME/bin/java
125 | fi
126 | if [ ! -x "$JAVACMD" ] ; then
127 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
128 |
129 | Please set the JAVA_HOME variable in your environment to match the
130 | location of your Java installation."
131 | fi
132 | else
133 | JAVACMD=java
134 | if ! command -v java >/dev/null 2>&1
135 | then
136 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137 |
138 | Please set the JAVA_HOME variable in your environment to match the
139 | location of your Java installation."
140 | fi
141 | fi
142 |
143 | # Increase the maximum file descriptors if we can.
144 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
145 | case $MAX_FD in #(
146 | max*)
147 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
148 | # shellcheck disable=SC2039,SC3045
149 | MAX_FD=$( ulimit -H -n ) ||
150 | warn "Could not query maximum file descriptor limit"
151 | esac
152 | case $MAX_FD in #(
153 | '' | soft) :;; #(
154 | *)
155 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
156 | # shellcheck disable=SC2039,SC3045
157 | ulimit -n "$MAX_FD" ||
158 | warn "Could not set maximum file descriptor limit to $MAX_FD"
159 | esac
160 | fi
161 |
162 | # Collect all arguments for the java command, stacking in reverse order:
163 | # * args from the command line
164 | # * the main class name
165 | # * -classpath
166 | # * -D...appname settings
167 | # * --module-path (only if needed)
168 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
169 |
170 | # For Cygwin or MSYS, switch paths to Windows format before running java
171 | if "$cygwin" || "$msys" ; then
172 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
173 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
174 |
175 | JAVACMD=$( cygpath --unix "$JAVACMD" )
176 |
177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
178 | for arg do
179 | if
180 | case $arg in #(
181 | -*) false ;; # don't mess with options #(
182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
183 | [ -e "$t" ] ;; #(
184 | *) false ;;
185 | esac
186 | then
187 | arg=$( cygpath --path --ignore --mixed "$arg" )
188 | fi
189 | # Roll the args list around exactly as many times as the number of
190 | # args, so each arg winds up back in the position where it started, but
191 | # possibly modified.
192 | #
193 | # NB: a `for` loop captures its iteration list before it begins, so
194 | # changing the positional parameters here affects neither the number of
195 | # iterations, nor the values presented in `arg`.
196 | shift # remove old arg
197 | set -- "$@" "$arg" # push replacement arg
198 | done
199 | fi
200 |
201 |
202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
204 |
205 | # Collect all arguments for the java command:
206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
207 | # and any embedded shellness will be escaped.
208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
209 | # treated as '${Hostname}' itself on the command line.
210 |
211 | set -- \
212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \
213 | -classpath "$CLASSPATH" \
214 | org.gradle.wrapper.GradleWrapperMain \
215 | "$@"
216 |
217 | # Stop when "xargs" is not available.
218 | if ! command -v xargs >/dev/null 2>&1
219 | then
220 | die "xargs is not available"
221 | fi
222 |
223 | # Use "xargs" to parse quoted args.
224 | #
225 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
226 | #
227 | # In Bash we could simply go:
228 | #
229 | # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
230 | # set -- "${ARGS[@]}" "$@"
231 | #
232 | # but POSIX shell has neither arrays nor command substitution, so instead we
233 | # post-process each arg (as a line of input to sed) to backslash-escape any
234 | # character that might be a shell metacharacter, then use eval to reverse
235 | # that process (while maintaining the separation between arguments), and wrap
236 | # the whole thing up as a single "set" statement.
237 | #
238 | # This will of course break if any of these variables contains a newline or
239 | # an unmatched quote.
240 | #
241 |
242 | eval "set -- $(
243 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
244 | xargs -n1 |
245 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
246 | tr '\n' ' '
247 | )" '"$@"'
248 |
249 | exec "$JAVACMD" "$@"
250 |
--------------------------------------------------------------------------------
/example/android/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%"=="" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%"=="" set DIRNAME=.
29 | @rem This is normally unused
30 | set APP_BASE_NAME=%~n0
31 | set APP_HOME=%DIRNAME%
32 |
33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
35 |
36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
38 |
39 | @rem Find java.exe
40 | if defined JAVA_HOME goto findJavaFromJavaHome
41 |
42 | set JAVA_EXE=java.exe
43 | %JAVA_EXE% -version >NUL 2>&1
44 | if %ERRORLEVEL% equ 0 goto execute
45 |
46 | echo. 1>&2
47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
48 | echo. 1>&2
49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2
50 | echo location of your Java installation. 1>&2
51 |
52 | goto fail
53 |
54 | :findJavaFromJavaHome
55 | set JAVA_HOME=%JAVA_HOME:"=%
56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
57 |
58 | if exist "%JAVA_EXE%" goto execute
59 |
60 | echo. 1>&2
61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
62 | echo. 1>&2
63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2
64 | echo location of your Java installation. 1>&2
65 |
66 | goto fail
67 |
68 | :execute
69 | @rem Setup the command line
70 |
71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
72 |
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if %ERRORLEVEL% equ 0 goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | set EXIT_CODE=%ERRORLEVEL%
85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1
86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87 | exit /b %EXIT_CODE%
88 |
89 | :mainEnd
90 | if "%OS%"=="Windows_NT" endlocal
91 |
92 | :omega
93 |
--------------------------------------------------------------------------------
/example/android/mlc-package-config.json:
--------------------------------------------------------------------------------
1 | {
2 | "device": "android",
3 | "model_list": [
4 | {
5 | "model": "HF://mlc-ai/Qwen2.5-1.5B-Instruct-q4f16_1-MLC",
6 | "model_id": "Qwen2.5-1.5B-Instruct-q4f16_1-MLC",
7 | "estimated_vram_bytes": 3043000000,
8 | "bundle_weight": false
9 | }
10 | ]
11 | }
--------------------------------------------------------------------------------
/example/android/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'AiExample'
2 | apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
3 | include ':app'
4 | includeBuild('../node_modules/@react-native/gradle-plugin')
5 | include ':mlc4j'
6 | project(':mlc4j').projectDir = file('dist/lib/mlc4j')
7 |
--------------------------------------------------------------------------------
/example/app.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "AiExample",
3 | "displayName": "AiExample",
4 | "components": [
5 | {
6 | "appKey": "AiExample",
7 | "displayName": "AiExample"
8 | }
9 | ],
10 | "resources": {
11 | "android": [
12 | "dist/res",
13 | "dist/main.android.jsbundle"
14 | ],
15 | "ios": [
16 | "dist/assets",
17 | "dist/main.ios.jsbundle"
18 | ],
19 | "macos": [
20 | "dist/assets",
21 | "dist/main.macos.jsbundle"
22 | ],
23 | "visionos": [
24 | "dist/assets",
25 | "dist/main.visionos.jsbundle"
26 | ],
27 | "windows": [
28 | "dist/assets",
29 | "dist/main.windows.bundle"
30 | ]
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/example/assets/avatar.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/callstackincubator/ai/fdfdfafd834f42124650b499df05fcb859b51792/example/assets/avatar.png
--------------------------------------------------------------------------------
/example/babel.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 | const pak = require('../package.json');
3 |
4 | module.exports = {
5 | presets: ['module:@react-native/babel-preset'],
6 | plugins: [
7 | '@babel/plugin-proposal-async-generator-functions',
8 | [
9 | 'module-resolver',
10 | {
11 | extensions: ['.tsx', '.ts', '.js', '.json'],
12 | alias: {
13 | [pak.name]: path.join(__dirname, '..', pak.source),
14 | },
15 | },
16 | ],
17 | ],
18 | };
19 |
--------------------------------------------------------------------------------
/example/index.js:
--------------------------------------------------------------------------------
1 | import { AppRegistry } from 'react-native';
2 | import App from './src/App';
3 | import { name as appName } from './app.json';
4 | import '@azure/core-asynciterator-polyfill';
5 | import './polyfills';
6 |
7 | AppRegistry.registerComponent(appName, () => App);
8 |
--------------------------------------------------------------------------------
/example/ios/.xcode.env:
--------------------------------------------------------------------------------
1 | # This `.xcode.env` file is versioned and is used to source the environment
2 | # used when running script phases inside Xcode.
3 | # To customize your local environment, you can create an `.xcode.env.local`
4 | # file that is not versioned.
5 |
6 | # NODE_BINARY variable contains the PATH to the node executable.
7 | #
8 | # Customize the NODE_BINARY variable here.
9 | # For example, to use nvm with brew, add the following line
10 | # . "$(brew --prefix nvm)/nvm.sh" --no-use
11 | export NODE_BINARY=$(command -v node)
12 |
--------------------------------------------------------------------------------
/example/ios/AiExample-Bridging-Header.h:
--------------------------------------------------------------------------------
1 | //
2 | // Use this file to import your target's public headers that you would like to expose to Swift.
3 | //
4 |
--------------------------------------------------------------------------------
/example/ios/AiExample.xcodeproj/xcshareddata/xcschemes/AiExample.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
30 |
31 |
33 |
39 |
40 |
41 |
42 |
43 |
53 |
55 |
61 |
62 |
63 |
64 |
70 |
72 |
78 |
79 |
80 |
81 |
83 |
84 |
87 |
88 |
89 |
--------------------------------------------------------------------------------
/example/ios/AiExample.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/example/ios/AiExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/example/ios/AiExample/AiExample.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.developer.kernel.increased-memory-limit
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/example/ios/AiExample/AppDelegate.h:
--------------------------------------------------------------------------------
1 | #import
2 | #import
3 |
4 | @interface AppDelegate : RCTAppDelegate
5 |
6 | @end
7 |
--------------------------------------------------------------------------------
/example/ios/AiExample/AppDelegate.mm:
--------------------------------------------------------------------------------
1 | #import "AppDelegate.h"
2 |
3 | #import
4 |
5 | @implementation AppDelegate
6 |
7 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
8 | {
9 | self.moduleName = @"AiExample";
10 | // You can add your custom initial props in the dictionary below.
11 | // They will be passed down to the ViewController used by React Native.
12 | self.initialProps = @{};
13 |
14 | return [super application:application didFinishLaunchingWithOptions:launchOptions];
15 | }
16 |
17 | - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
18 | {
19 | return [self bundleURL];
20 | }
21 |
22 | - (NSURL *)bundleURL
23 | {
24 | #if DEBUG
25 | return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
26 | #else
27 | return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
28 | #endif
29 | }
30 |
31 | @end
32 |
--------------------------------------------------------------------------------
/example/ios/AiExample/Images.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "scale" : "2x",
6 | "size" : "20x20"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "scale" : "3x",
11 | "size" : "20x20"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "scale" : "2x",
16 | "size" : "29x29"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "scale" : "3x",
21 | "size" : "29x29"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "scale" : "2x",
26 | "size" : "40x40"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "scale" : "3x",
31 | "size" : "40x40"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "scale" : "2x",
36 | "size" : "60x60"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "scale" : "3x",
41 | "size" : "60x60"
42 | },
43 | {
44 | "idiom" : "ios-marketing",
45 | "scale" : "1x",
46 | "size" : "1024x1024"
47 | }
48 | ],
49 | "info" : {
50 | "author" : "xcode",
51 | "version" : 1
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/example/ios/AiExample/Images.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/example/ios/AiExample/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleDisplayName
8 | AiExample
9 | CFBundleExecutable
10 | $(EXECUTABLE_NAME)
11 | CFBundleIdentifier
12 | $(PRODUCT_BUNDLE_IDENTIFIER)
13 | CFBundleInfoDictionaryVersion
14 | 6.0
15 | CFBundleName
16 | $(PRODUCT_NAME)
17 | CFBundlePackageType
18 | APPL
19 | CFBundleShortVersionString
20 | $(MARKETING_VERSION)
21 | CFBundleSignature
22 | ????
23 | CFBundleVersion
24 | $(CURRENT_PROJECT_VERSION)
25 | LSRequiresIPhoneOS
26 |
27 | NSAppTransportSecurity
28 |
29 |
30 | NSAllowsArbitraryLoads
31 |
32 | NSAllowsLocalNetworking
33 |
34 |
35 | NSLocationWhenInUseUsageDescription
36 |
37 | UILaunchStoryboardName
38 | LaunchScreen
39 | UIRequiredDeviceCapabilities
40 |
41 | arm64
42 |
43 | UISupportedInterfaceOrientations
44 |
45 | UIInterfaceOrientationPortrait
46 | UIInterfaceOrientationLandscapeLeft
47 | UIInterfaceOrientationLandscapeRight
48 |
49 | UIViewControllerBasedStatusBarAppearance
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/example/ios/AiExample/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
24 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/example/ios/AiExample/PrivacyInfo.xcprivacy:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | NSPrivacyAccessedAPITypes
6 |
7 |
8 | NSPrivacyAccessedAPIType
9 | NSPrivacyAccessedAPICategoryFileTimestamp
10 | NSPrivacyAccessedAPITypeReasons
11 |
12 | C617.1
13 |
14 |
15 |
16 | NSPrivacyAccessedAPIType
17 | NSPrivacyAccessedAPICategoryUserDefaults
18 | NSPrivacyAccessedAPITypeReasons
19 |
20 | CA92.1
21 |
22 |
23 |
24 | NSPrivacyAccessedAPIType
25 | NSPrivacyAccessedAPICategorySystemBootTime
26 | NSPrivacyAccessedAPITypeReasons
27 |
28 | 35F9.1
29 |
30 |
31 |
32 | NSPrivacyCollectedDataTypes
33 |
34 | NSPrivacyTracking
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/example/ios/AiExample/main.m:
--------------------------------------------------------------------------------
1 | #import
2 |
3 | #import "AppDelegate.h"
4 |
5 | int main(int argc, char *argv[])
6 | {
7 | @autoreleasepool {
8 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/example/ios/AiExampleTests/AiExampleTests.m:
--------------------------------------------------------------------------------
1 | #import
2 | #import
3 |
4 | #import
5 | #import
6 |
7 | #define TIMEOUT_SECONDS 600
8 | #define TEXT_TO_LOOK_FOR @"Welcome to React"
9 |
10 | @interface AiExampleTests : XCTestCase
11 |
12 | @end
13 |
14 | @implementation AiExampleTests
15 |
16 | - (BOOL)findSubviewInView:(UIView *)view matching:(BOOL (^)(UIView *view))test
17 | {
18 | if (test(view)) {
19 | return YES;
20 | }
21 | for (UIView *subview in [view subviews]) {
22 | if ([self findSubviewInView:subview matching:test]) {
23 | return YES;
24 | }
25 | }
26 | return NO;
27 | }
28 |
29 | - (void)testRendersWelcomeScreen
30 | {
31 | UIViewController *vc = [[[RCTSharedApplication() delegate] window] rootViewController];
32 | NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
33 | BOOL foundElement = NO;
34 |
35 | __block NSString *redboxError = nil;
36 | #ifdef DEBUG
37 | RCTSetLogFunction(
38 | ^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
39 | if (level >= RCTLogLevelError) {
40 | redboxError = message;
41 | }
42 | });
43 | #endif
44 |
45 | while ([date timeIntervalSinceNow] > 0 && !foundElement && !redboxError) {
46 | [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
47 | [[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
48 |
49 | foundElement = [self findSubviewInView:vc.view
50 | matching:^BOOL(UIView *view) {
51 | if ([view.accessibilityLabel isEqualToString:TEXT_TO_LOOK_FOR]) {
52 | return YES;
53 | }
54 | return NO;
55 | }];
56 | }
57 |
58 | #ifdef DEBUG
59 | RCTSetLogFunction(RCTDefaultLogFunction);
60 | #endif
61 |
62 | XCTAssertNil(redboxError, @"RedBox error: %@", redboxError);
63 | XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS);
64 | }
65 |
66 | @end
67 |
--------------------------------------------------------------------------------
/example/ios/AiExampleTests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 |
24 |
25 |
--------------------------------------------------------------------------------
/example/ios/File.swift:
--------------------------------------------------------------------------------
1 | //
2 | // File.swift
3 | // AiExample
4 | //
5 |
6 | import Foundation
7 |
--------------------------------------------------------------------------------
/example/ios/Podfile:
--------------------------------------------------------------------------------
1 | ENV['RCT_NEW_ARCH_ENABLED'] = '0'
2 |
3 | # Resolve react_native_pods.rb with node to allow for hoisting
4 | require Pod::Executable.execute_command('node', ['-p',
5 | 'require.resolve(
6 | "react-native/scripts/react_native_pods.rb",
7 | {paths: [process.argv[1]]},
8 | )', __dir__]).strip
9 |
10 | platform :ios, min_ios_version_supported
11 | prepare_react_native_project!
12 |
13 | linkage = ENV['USE_FRAMEWORKS']
14 | if linkage != nil
15 | Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
16 | use_frameworks! :linkage => linkage.to_sym
17 | end
18 |
19 | target 'AiExample' do
20 | config = use_native_modules!
21 |
22 | use_react_native!(
23 | :path => config[:reactNativePath],
24 | # An absolute path to your application root.
25 | :app_path => "#{Pod::Config.instance.installation_root}/.."
26 | )
27 |
28 | target 'AiExampleTests' do
29 | inherit! :complete
30 | # Pods for testing
31 | end
32 |
33 | post_install do |installer|
34 | # https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/react_native_pods.rb#L197-L202
35 | react_native_post_install(
36 | installer,
37 | config[:reactNativePath],
38 | :mac_catalyst_enabled => false,
39 | # :ccache_enabled => true
40 | )
41 | end
42 | end
43 |
--------------------------------------------------------------------------------
/example/ios/README.md:
--------------------------------------------------------------------------------
1 | # Setup
2 |
3 | To compile the model you need to execute the following command:
4 |
5 | ```bash
6 | mlc_llm package
7 | ```
8 |
9 | > [!NOTE]
10 | > To setup `mlc_llm` read the [official documentation](https://llm.mlc.ai/docs/install/mlc_llm.html#install-mlc-packages)
11 |
12 | This will generate necessary binaries and model itself in the `build` & `dist` directories.
13 |
14 | Then model is added to XCode project under `bundle/` directory which then is used by the module.
15 |
16 | In the future, we will automate this process, so when the model is not found under the bundle/ directory, it will be downloaded from HuggingFace in the runtime.
17 |
--------------------------------------------------------------------------------
/example/ios/mlc-package-config.json:
--------------------------------------------------------------------------------
1 | {
2 | "device": "iphone",
3 | "model_list": [
4 | {
5 | "model": "HF://mlc-ai/Phi-3-mini-4k-instruct-q4f16_1-MLC",
6 | "model_id": "Phi-3-mini-4k-instruct-q4f16_1-MLC",
7 | "estimated_vram_bytes": 3043000000,
8 | "bundle_weight": false
9 | }
10 | ]
11 | }
--------------------------------------------------------------------------------
/example/metro.config.js:
--------------------------------------------------------------------------------
1 | const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
2 | const path = require('path');
3 | const escape = require('escape-string-regexp');
4 | const exclusionList = require('metro-config/src/defaults/exclusionList');
5 | const pak = require('../package.json');
6 |
7 | const root = path.resolve(__dirname, '..');
8 | const modules = Object.keys({ ...pak.peerDependencies });
9 |
10 | /**
11 | * Metro configuration
12 | * https://facebook.github.io/metro/docs/configuration
13 | *
14 | * @type {import('metro-config').MetroConfig}
15 | */
16 | const config = {
17 | watchFolders: [root],
18 |
19 | // We need to make sure that only one version is loaded for peerDependencies
20 | // So we block them at the root, and alias them to the versions in example's node_modules
21 | resolver: {
22 | blacklistRE: exclusionList(
23 | modules.map(
24 | (m) =>
25 | new RegExp(`^${escape(path.join(root, 'node_modules', m))}\\/.*$`)
26 | )
27 | ),
28 |
29 | extraNodeModules: modules.reduce((acc, name) => {
30 | acc[name] = path.join(__dirname, 'node_modules', name);
31 | return acc;
32 | }, {}),
33 | },
34 |
35 | transformer: {
36 | getTransformOptions: async () => ({
37 | transform: {
38 | experimentalImportSupport: false,
39 | inlineRequires: true,
40 | },
41 | }),
42 | },
43 | };
44 |
45 | module.exports = mergeConfig(getDefaultConfig(__dirname), config);
46 |
--------------------------------------------------------------------------------
/example/mlc-config.json:
--------------------------------------------------------------------------------
1 | {
2 | "iphone": [
3 | {
4 | "model": "HF://mlc-ai/Phi-3-mini-4k-instruct-q4f16_1-MLC",
5 | "model_id": "Phi-3-mini-4k-instruct-q4f16_1-MLC",
6 | "estimated_vram_bytes": 3043000000
7 | }
8 | ],
9 | "android": [
10 | {
11 | "model": "HF://mlc-ai/Qwen2.5-1.5B-Instruct-q4f16_1-MLC",
12 | "model_id": "Qwen2.5-1.5B-Instruct-q4f16_1-MLC",
13 | "estimated_vram_bytes": 3043000000
14 | }
15 | ]
16 | }
17 |
--------------------------------------------------------------------------------
/example/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-native-ai-example",
3 | "version": "0.0.1",
4 | "private": true,
5 | "scripts": {
6 | "android": "react-native run-android",
7 | "build:android": "cd android && ./gradlew assembleDebug --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a",
8 | "build:ios": "cd ios && xcodebuild -workspace AiExample.xcworkspace -configuration Debug -scheme AiExample -sdk iphoneos",
9 | "ios": "react-native run-ios",
10 | "start": "react-native start",
11 | "prestart": "node ../scripts/mlc-prepare.js --root ."
12 | },
13 | "dependencies": {
14 | "@azure/core-asynciterator-polyfill": "^1.0.2",
15 | "@babel/plugin-proposal-async-generator-functions": "^7.20.7",
16 | "@react-native-community/netinfo": "^11.3.2",
17 | "@types/uuid": "^10.0.0",
18 | "@ungap/structured-clone": "^1.3.0",
19 | "ai": "^4.1.36",
20 | "react": "18.2.0",
21 | "react-native": "0.74.2",
22 | "react-native-get-random-values": "^1.11.0",
23 | "react-native-gifted-chat": "^2.4.0",
24 | "react-native-select-dropdown": "^4.0.1",
25 | "text-encoding": "^0.7.0",
26 | "uuid": "^10.0.0",
27 | "web-streams-polyfill": "3.3.3",
28 | "zod": "^3.23.8"
29 | },
30 | "devDependencies": {
31 | "@babel/core": "^7.20.0",
32 | "@babel/preset-env": "^7.20.0",
33 | "@babel/runtime": "^7.20.0",
34 | "@react-native/babel-preset": "0.74.85",
35 | "@react-native/metro-config": "0.74.85",
36 | "@react-native/typescript-config": "0.74.85",
37 | "babel-plugin-module-resolver": "^5.0.0"
38 | },
39 | "engines": {
40 | "node": ">=18"
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/example/polyfills.js:
--------------------------------------------------------------------------------
1 | import 'react-native-get-random-values';
2 |
3 | // @ts-ignore
4 | import { polyfillGlobal } from 'react-native/Libraries/Utilities/PolyfillFunctions';
5 | import structuredClone from '@ungap/structured-clone';
6 |
7 | const webStreamPolyfills = require('web-streams-polyfill/ponyfill/es6');
8 |
9 | polyfillGlobal('TextEncoder', () => require('text-encoding').TextEncoder);
10 | polyfillGlobal('TextDecoder', () => require('text-encoding').TextDecoder);
11 | polyfillGlobal('ReadableStream', () => webStreamPolyfills.ReadableStream);
12 | polyfillGlobal('TransformStream', () => webStreamPolyfills.TransformStream);
13 | polyfillGlobal('WritableStream', () => webStreamPolyfills.WritableStream);
14 | polyfillGlobal('TextEncoderStream', () => webStreamPolyfills.TextEncoderStream);
15 | polyfillGlobal('structuredClone', () => structuredClone);
16 |
--------------------------------------------------------------------------------
/example/react-native.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 | const pak = require('../package.json');
3 |
4 | module.exports = {
5 | project: {
6 | ios: {
7 | automaticPodsInstallation: true,
8 | },
9 | },
10 | dependencies: {
11 | [pak.name]: {
12 | root: path.join(__dirname, '..'),
13 | },
14 | },
15 | };
16 |
--------------------------------------------------------------------------------
/example/src/App.tsx:
--------------------------------------------------------------------------------
1 | import React, { useCallback, useState } from 'react';
2 | import { SafeAreaView, StyleSheet, View, Text } from 'react-native';
3 | import { GiftedChat, type IMessage } from 'react-native-gifted-chat';
4 | import {
5 | getModel,
6 | type AiModelSettings,
7 | prepareModel,
8 | downloadModel,
9 | } from 'react-native-ai';
10 | import { streamText, type CoreMessage } from 'ai';
11 | import { v4 as uuid } from 'uuid';
12 | import NetworkInfo from './NetworkInfo';
13 | import { ModelSelection } from './ModelSelection';
14 |
15 | const aiBot = {
16 | _id: 2,
17 | name: 'AI Chat Bot',
18 | avatar: require('./../assets/avatar.png'),
19 | };
20 |
21 | const ProgressBar = ({ progress }: { progress: number }) => {
22 | if (progress === 100) return null;
23 |
24 | return (
25 |
26 |
27 | {progress.toFixed(1)}%
28 |
29 | );
30 | };
31 |
32 | export default function Example() {
33 | const [modelId, setModelId] = useState();
34 | const [displayedMessages, setDisplayedMessages] = useState([]);
35 | const [downloadProgress, setDownloadProgress] = useState(0);
36 |
37 | const onSendMessage = useCallback(
38 | async (messages: IMessage[]) => {
39 | if (modelId) {
40 | try {
41 | const { textStream } = streamText({
42 | model: getModel(modelId),
43 | temperature: 0.6,
44 | messages: messages
45 | .slice(0, -1)
46 | .toReversed()
47 | .map((message): CoreMessage => {
48 | return {
49 | content: message.text,
50 | role: message.user._id === 2 ? 'assistant' : 'user',
51 | };
52 | }),
53 | });
54 |
55 | let firstChunk = true;
56 | for await (const chunk of textStream) {
57 | if (firstChunk) {
58 | setDisplayedMessages((previousMessages) =>
59 | GiftedChat.append(previousMessages, {
60 | // @ts-ignore
61 | _id: uuid(),
62 | text: chunk,
63 | createdAt: new Date(),
64 | user: aiBot,
65 | })
66 | );
67 | } else {
68 | setDisplayedMessages((previousMessages) => {
69 | let newMessages = [...previousMessages];
70 | const prevMessage = newMessages.shift();
71 | return [
72 | {
73 | _id: prevMessage?._id ?? uuid(),
74 | text: prevMessage?.text ? prevMessage.text + chunk : chunk,
75 | createdAt: prevMessage?.createdAt ?? new Date(),
76 | user: aiBot,
77 | },
78 | ...newMessages,
79 | ];
80 | });
81 | }
82 | firstChunk = false;
83 | }
84 | } catch (error) {
85 | console.log('Error:', error);
86 | }
87 | }
88 | },
89 | [modelId]
90 | );
91 |
92 | const addAiBotMessage = useCallback((text: string) => {
93 | setDisplayedMessages((previousMessages) =>
94 | GiftedChat.append(previousMessages, {
95 | // @ts-ignore
96 | _id: uuid(),
97 | text,
98 | createdAt: new Date(),
99 | user: aiBot,
100 | })
101 | );
102 | }, []);
103 |
104 | const selectModel = useCallback(
105 | async (modelSettings: AiModelSettings) => {
106 | if (modelSettings.model_id) {
107 | setModelId(modelSettings.model_id);
108 |
109 | addAiBotMessage('Downloading model...');
110 | await downloadModel(modelSettings.model_id, {
111 | onStart: () => {
112 | addAiBotMessage('Starting model download...');
113 | },
114 | onProgress: (progress) => {
115 | setDownloadProgress(progress.percentage);
116 | },
117 | onComplete: () => {
118 | setDownloadProgress(100);
119 | addAiBotMessage('Model download complete!');
120 | },
121 | onError: (error) => {
122 | setDownloadProgress(0);
123 | addAiBotMessage(`Error downloading model: ${error.message}`);
124 | },
125 | });
126 |
127 | await prepareModel(modelSettings.model_id);
128 |
129 | addAiBotMessage('Model ready for conversation.');
130 | }
131 | },
132 | [addAiBotMessage]
133 | );
134 |
135 | const onSend = useCallback(
136 | (newMessage: IMessage[]) => {
137 | if (newMessage[0]) {
138 | setDisplayedMessages((previousMessages) =>
139 | GiftedChat.append(previousMessages, newMessage)
140 | );
141 |
142 | onSendMessage([newMessage[0], ...displayedMessages]);
143 | }
144 | },
145 | [onSendMessage, displayedMessages]
146 | );
147 |
148 | return (
149 |
150 |
151 |
152 | (
159 |
160 |
161 |
162 | )}
163 | />
164 |
165 | );
166 | }
167 |
168 | const styles = StyleSheet.create({
169 | container: {
170 | flex: 1,
171 | backgroundColor: '#fff',
172 | },
173 | footerContainer: {
174 | paddingHorizontal: 16,
175 | paddingBottom: 8,
176 | },
177 | progressContainer: {
178 | height: 28,
179 | backgroundColor: '#2A2A2A',
180 | borderRadius: 14,
181 | overflow: 'hidden',
182 | position: 'relative',
183 | },
184 | progressBar: {
185 | height: '100%',
186 | backgroundColor: '#34C759',
187 | borderRadius: 14,
188 | },
189 | progressText: {
190 | position: 'absolute',
191 | width: '100%',
192 | textAlign: 'center',
193 | color: '#FFFFFF',
194 | fontSize: 14,
195 | fontWeight: 'bold',
196 | lineHeight: 28,
197 | },
198 | });
199 |
--------------------------------------------------------------------------------
/example/src/ModelSelection.tsx:
--------------------------------------------------------------------------------
1 | import React, { useEffect, useState } from 'react';
2 | import { View, StyleSheet, Text } from 'react-native';
3 | import { type AiModelSettings, getModels } from 'react-native-ai';
4 | import SelectDropdown from 'react-native-select-dropdown';
5 |
6 | type ModelSelectionProps = {
7 | onModelIdSelected: (modelSettings: AiModelSettings) => void;
8 | };
9 |
10 | export const ModelSelection = ({ onModelIdSelected }: ModelSelectionProps) => {
11 | const [availableModels, setAvailableModels] = useState([]);
12 |
13 | useEffect(() => {
14 | const getAvailableModels = async () => {
15 | const models = await getModels();
16 | setAvailableModels(models);
17 | };
18 |
19 | getAvailableModels();
20 | }, []);
21 |
22 | return (
23 |
24 | {
28 | return (
29 |
30 |
31 | {(selectedItem && selectedItem.model_id) || 'Select model'}
32 |
33 |
34 | );
35 | }}
36 | renderItem={(item, isSelected) => {
37 | return (
38 |
44 | {item.model_id}
45 |
46 | );
47 | }}
48 | showsVerticalScrollIndicator={false}
49 | dropdownStyle={styles.dropdownMenuStyle}
50 | />
51 |
52 | );
53 | };
54 |
55 | const styles = StyleSheet.create({
56 | container: {
57 | width: '100%',
58 | paddingHorizontal: 16,
59 | paddingVertical: 8,
60 | },
61 | buttonStyle: {
62 | width: '100%',
63 | height: 50,
64 | backgroundColor: '#F5F5F5',
65 | borderRadius: 8,
66 | borderWidth: 1,
67 | borderColor: '#E8E8E8',
68 | },
69 | dropdownButtonStyle: {
70 | width: '100%',
71 | height: 50,
72 | backgroundColor: '#F5F5F5',
73 | borderRadius: 8,
74 | flexDirection: 'row',
75 | justifyContent: 'center',
76 | alignItems: 'center',
77 | paddingHorizontal: 12,
78 | },
79 | dropdownButtonTxtStyle: {
80 | flex: 1,
81 | fontSize: 16,
82 | fontWeight: '500',
83 | color: '#151E26',
84 | textAlign: 'center',
85 | },
86 | dropdownMenuStyle: {
87 | backgroundColor: '#F5F5F5',
88 | borderRadius: 8,
89 | borderWidth: 1,
90 | borderColor: '#E8E8E8',
91 | },
92 | dropdownItemStyle: {
93 | padding: 12,
94 | borderBottomWidth: 1,
95 | borderBottomColor: '#E8E8E8',
96 | },
97 | dropdownItemTxtStyle: {
98 | fontSize: 16,
99 | color: '#151E26',
100 | },
101 | });
102 |
--------------------------------------------------------------------------------
/example/src/NetworkInfo.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { View, Text, StyleSheet } from 'react-native';
3 | import { useNetInfo } from '@react-native-community/netinfo';
4 |
5 | const NetworkInfo = () => {
6 | const netInfo = useNetInfo();
7 |
8 | const getStatusColor = () => {
9 | if (netInfo.isConnected) return styles.connected;
10 | return styles.disconnected;
11 | };
12 |
13 | return (
14 |
15 |
16 |
17 | {netInfo.isConnected
18 | ? `Connected via ${netInfo.type} ${netInfo.isInternetReachable ? '✅' : '⚠️'}`
19 | : 'No network connection ❌'}
20 |
21 |
22 | );
23 | };
24 |
25 | const styles = StyleSheet.create({
26 | container: {
27 | flexDirection: 'row',
28 | alignItems: 'center',
29 | padding: 10,
30 | backgroundColor: '#f0f0f0',
31 | borderRadius: 8,
32 | },
33 | statusIndicator: {
34 | width: 12,
35 | height: 12,
36 | borderRadius: 6,
37 | marginRight: 10,
38 | },
39 | connected: {
40 | backgroundColor: '#4CAF50',
41 | },
42 | disconnected: {
43 | backgroundColor: '#F44336',
44 | },
45 | textContainer: {
46 | flex: 1,
47 | },
48 | text: {
49 | fontSize: 16,
50 | fontWeight: 'bold',
51 | },
52 | detailText: {
53 | fontSize: 14,
54 | color: '#666',
55 | },
56 | });
57 |
58 | export default NetworkInfo;
59 |
--------------------------------------------------------------------------------
/ios/.clang-format:
--------------------------------------------------------------------------------
1 | # Config for clang-format version 16
2 |
3 | # standard
4 | BasedOnStyle: llvm
5 | Standard: c++14
6 |
7 | # Indentation
8 | IndentWidth: 2
9 | ColumnLimit: 140
10 |
11 | # Includes
12 | SortIncludes: true
13 | SortUsingDeclarations: true
14 |
15 | # Pointer and reference alignment
16 | PointerAlignment: Left
17 | ReferenceAlignment: Left
18 | ReflowComments: true
19 |
20 | # Line breaking options
21 | BreakBeforeBraces: Attach
22 | BreakConstructorInitializers: BeforeColon
23 | AllowShortFunctionsOnASingleLine: Empty
24 | IndentCaseLabels: true
25 | NamespaceIndentation: Inner
26 |
--------------------------------------------------------------------------------
/ios/Ai.h:
--------------------------------------------------------------------------------
1 | #import
2 |
3 | #ifdef RCT_NEW_ARCH_ENABLED
4 | #import "RNAiSpec.h"
5 |
6 | @interface Ai : RCTEventEmitter
7 | #else
8 | #import
9 |
10 | @interface Ai : RCTEventEmitter
11 | #endif
12 |
13 | @end
14 |
--------------------------------------------------------------------------------
/ios/Ai.mm:
--------------------------------------------------------------------------------
1 | #import "Ai.h"
2 | #import "MLCEngine.h"
3 | #import
4 |
5 | @interface Ai ()
6 |
7 | @property(nonatomic, strong) MLCEngine* engine;
8 | @property(nonatomic, strong) NSURL* bundleURL;
9 | @property(nonatomic, strong) NSString* modelPath;
10 | @property(nonatomic, strong) NSString* modelLib;
11 | @property(nonatomic, strong) NSString* displayText;
12 |
13 | @end
14 |
15 | @implementation Ai
16 |
17 | {
18 | bool hasListeners;
19 | }
20 |
21 | RCT_EXPORT_MODULE()
22 |
23 | + (BOOL)requiresMainQueueSetup {
24 | return YES;
25 | }
26 |
27 | - (NSArray*)supportedEvents {
28 | return @[ @"onChatUpdate", @"onChatComplete", @"onDownloadStart", @"onDownloadComplete", @"onDownloadProgress" ];
29 | }
30 |
31 | - (void)startObserving {
32 | hasListeners = YES;
33 | }
34 |
35 | - (void)stopObserving {
36 | hasListeners = NO;
37 | }
38 |
39 | - (instancetype)init {
40 | self = [super init];
41 | if (self) {
42 | _engine = [[MLCEngine alloc] init];
43 |
44 | // Get the Documents directory path
45 | NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
46 | NSString* documentsDirectory = [paths firstObject];
47 | _bundleURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"bundle"]];
48 |
49 | // Create bundle directory if it doesn't exist
50 | NSError* dirError;
51 | [[NSFileManager defaultManager] createDirectoryAtPath:[_bundleURL path] withIntermediateDirectories:YES attributes:nil error:&dirError];
52 | if (dirError) {
53 | NSLog(@"Error creating bundle directory: %@", dirError);
54 | }
55 |
56 | // Copy the config file from the app bundle to Documents if it doesn't exist yet
57 | NSURL* bundleConfigURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"bundle/mlc-app-config.json"];
58 | NSURL* configURL = [_bundleURL URLByAppendingPathComponent:@"mlc-app-config.json"];
59 |
60 | NSError* copyError;
61 | [[NSFileManager defaultManager] removeItemAtURL:configURL error:nil]; // Remove existing file if it exists
62 | [[NSFileManager defaultManager] copyItemAtURL:bundleConfigURL toURL:configURL error:©Error];
63 | if (copyError) {
64 | NSLog(@"Error copying config file: %@", copyError);
65 | }
66 |
67 | // Read and parse JSON
68 | NSData* jsonData = [NSData dataWithContentsOfURL:configURL];
69 | if (jsonData) {
70 | NSError* error;
71 | NSDictionary* jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
72 |
73 | if (!error && [jsonDict isKindOfClass:[NSDictionary class]]) {
74 | NSArray* modelList = jsonDict[@"model_list"];
75 | if ([modelList isKindOfClass:[NSArray class]] && modelList.count > 0) {
76 | NSDictionary* firstModel = modelList[0];
77 | _modelPath = firstModel[@"model_path"];
78 | _modelLib = firstModel[@"model_lib"];
79 | }
80 | }
81 | }
82 | }
83 | return self;
84 | }
85 |
86 | - (NSDictionary*)parseResponseString:(NSString*)responseString {
87 | NSData* jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
88 | NSError* error;
89 | NSArray* jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
90 |
91 | if (error) {
92 | NSLog(@"Error parsing JSON: %@", error);
93 | return nil;
94 | }
95 |
96 | if (jsonArray.count > 0) {
97 | NSDictionary* responseDict = jsonArray[0];
98 | NSArray* choices = responseDict[@"choices"];
99 | if (choices.count > 0) {
100 | NSDictionary* choice = choices[0];
101 | NSDictionary* delta = choice[@"delta"];
102 | NSString* content = delta[@"content"];
103 | NSString* finishReason = choice[@"finish_reason"];
104 |
105 | BOOL isFinished = (finishReason != nil && ![finishReason isEqual:[NSNull null]]);
106 |
107 | return @{@"content" : content ?: @"", @"isFinished" : @(isFinished)};
108 | }
109 | }
110 |
111 | return nil;
112 | }
113 |
114 | RCT_EXPORT_METHOD(doGenerate : (NSString*)instanceId messages : (NSArray*)messages resolve : (RCTPromiseResolveBlock)
115 | resolve reject : (RCTPromiseRejectBlock)reject) {
116 | NSLog(@"Generating for instance ID: %@, with text: %@", instanceId, messages);
117 | _displayText = @"";
118 | __block BOOL hasResolved = NO;
119 |
120 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
121 | NSURL* modelLocalURL = [self.bundleURL URLByAppendingPathComponent:self.modelPath];
122 | NSString* modelLocalPath = [modelLocalURL path];
123 |
124 | [self.engine reloadWithModelPath:modelLocalPath modelLib:self.modelLib];
125 |
126 | [self.engine chatCompletionWithMessages:messages
127 | completion:^(id response) {
128 | if ([response isKindOfClass:[NSString class]]) {
129 | NSDictionary* parsedResponse = [self parseResponseString:response];
130 | if (parsedResponse) {
131 | NSString* content = parsedResponse[@"content"];
132 | BOOL isFinished = [parsedResponse[@"isFinished"] boolValue];
133 |
134 | if (content) {
135 | self.displayText = [self.displayText stringByAppendingString:content];
136 | }
137 |
138 | if (isFinished && !hasResolved) {
139 | hasResolved = YES;
140 | resolve(self.displayText);
141 | }
142 |
143 | } else {
144 | if (!hasResolved) {
145 | hasResolved = YES;
146 | reject(@"PARSE_ERROR", @"Failed to parse response", nil);
147 | }
148 | }
149 | } else {
150 | if (!hasResolved) {
151 | hasResolved = YES;
152 | reject(@"INVALID_RESPONSE", @"Received an invalid response type", nil);
153 | }
154 | }
155 | }];
156 | });
157 | }
158 |
159 | RCT_EXPORT_METHOD(doStream : (NSString*)instanceId messages : (NSArray*)messages resolve : (RCTPromiseResolveBlock)
160 | resolve reject : (RCTPromiseRejectBlock)reject) {
161 |
162 | NSLog(@"Streaming for instance ID: %@, with messages: %@", instanceId, messages);
163 |
164 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
165 | __block BOOL hasResolved = NO;
166 |
167 | NSURL* modelLocalURL = [self.bundleURL URLByAppendingPathComponent:self.modelPath];
168 | NSString* modelLocalPath = [modelLocalURL path];
169 |
170 | [self.engine reloadWithModelPath:modelLocalPath modelLib:self.modelLib];
171 |
172 | [self.engine chatCompletionWithMessages:messages
173 | completion:^(id response) {
174 | if ([response isKindOfClass:[NSString class]]) {
175 | NSDictionary* parsedResponse = [self parseResponseString:response];
176 | if (parsedResponse) {
177 | NSString* content = parsedResponse[@"content"];
178 | BOOL isFinished = [parsedResponse[@"isFinished"] boolValue];
179 |
180 | if (content) {
181 | self.displayText = [self.displayText stringByAppendingString:content];
182 | if (self->hasListeners) {
183 | [self sendEventWithName:@"onChatUpdate" body:@{@"content" : content}];
184 | }
185 | }
186 |
187 | if (isFinished && !hasResolved) {
188 | hasResolved = YES;
189 | if (self->hasListeners) {
190 | [self sendEventWithName:@"onChatComplete" body:nil];
191 | }
192 |
193 | resolve(@"");
194 |
195 | return;
196 | }
197 | } else {
198 | if (!hasResolved) {
199 | hasResolved = YES;
200 | reject(@"PARSE_ERROR", @"Failed to parse response", nil);
201 | }
202 | }
203 | } else {
204 | if (!hasResolved) {
205 | hasResolved = YES;
206 | reject(@"INVALID_RESPONSE", @"Received an invalid response type", nil);
207 | }
208 | }
209 | }];
210 | });
211 | }
212 |
213 | RCT_EXPORT_METHOD(getModel : (NSString*)name resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject) {
214 | // Read app config from Documents directory
215 | NSURL* configURL = [self.bundleURL URLByAppendingPathComponent:@"mlc-app-config.json"];
216 | NSData* jsonData = [NSData dataWithContentsOfURL:configURL];
217 |
218 | if (!jsonData) {
219 | reject(@"Model not found", @"Failed to read app config", nil);
220 | return;
221 | }
222 |
223 | NSError* error;
224 | NSDictionary* appConfig = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
225 |
226 | if (error) {
227 | reject(@"Model not found", @"Failed to parse app config", error);
228 | return;
229 | }
230 |
231 | // Find model record
232 | NSArray* modelList = appConfig[@"model_list"];
233 | NSDictionary* modelConfig = nil;
234 |
235 | for (NSDictionary* model in modelList) {
236 | if ([model[@"model_id"] isEqualToString:name]) {
237 | modelConfig = model;
238 | break;
239 | }
240 | }
241 |
242 | if (!modelConfig) {
243 | reject(@"Model not found", @"Didn't find the model", nil);
244 | return;
245 | }
246 |
247 | // Return a JSON object with details
248 | NSDictionary* modelInfo = @{@"modelId" : modelConfig[@"model_id"], @"modelLib" : modelConfig[@"model_lib"]};
249 |
250 | resolve(modelInfo);
251 | }
252 |
253 | RCT_EXPORT_METHOD(getModels : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject) {
254 | NSURL* configURL = [_bundleURL URLByAppendingPathComponent:@"mlc-app-config.json"];
255 |
256 | // Read and parse JSON
257 | NSData* jsonData = [NSData dataWithContentsOfURL:configURL];
258 | if (!jsonData) {
259 | reject(@"error", @"Failed to read JSON data", nil);
260 | return;
261 | }
262 |
263 | NSError* error;
264 | NSDictionary* jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
265 |
266 | if (error || ![jsonDict isKindOfClass:[NSDictionary class]]) {
267 | reject(@"error", @"Failed to parse JSON", error);
268 | return;
269 | }
270 |
271 | NSArray* modelList = jsonDict[@"model_list"];
272 | if (![modelList isKindOfClass:[NSArray class]]) {
273 | reject(@"error", @"model_list is missing or invalid", nil);
274 | return;
275 | }
276 | NSLog(@"models: %@", modelList);
277 | resolve(modelList);
278 | }
279 |
280 | RCT_EXPORT_METHOD(prepareModel : (NSString*)instanceId resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject) {
281 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
282 | @try {
283 | // Read app config
284 | NSURL* configURL = [self.bundleURL URLByAppendingPathComponent:@"mlc-app-config.json"];
285 | NSData* jsonData = [NSData dataWithContentsOfURL:configURL];
286 |
287 | if (!jsonData) {
288 | dispatch_async(dispatch_get_main_queue(), ^{
289 | reject(@"MODEL_ERROR", @"Failed to read app config", nil);
290 | });
291 | return;
292 | }
293 |
294 | NSError* error;
295 | NSDictionary* appConfig = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
296 |
297 | if (error) {
298 | dispatch_async(dispatch_get_main_queue(), ^{
299 | reject(@"MODEL_ERROR", @"Failed to parse app config", error);
300 | });
301 | return;
302 | }
303 |
304 | // Find model record
305 | NSArray* modelList = appConfig[@"model_list"];
306 | NSDictionary* modelRecord = nil;
307 |
308 | for (NSDictionary* model in modelList) {
309 | if ([model[@"model_id"] isEqualToString:instanceId]) {
310 | modelRecord = model;
311 | break;
312 | }
313 | }
314 |
315 | if (!modelRecord) {
316 | dispatch_async(dispatch_get_main_queue(), ^{
317 | reject(@"MODEL_ERROR", @"There's no record for requested model", nil);
318 | });
319 | return;
320 | }
321 |
322 | // Get model config
323 | NSError* configError;
324 | NSDictionary* modelConfig = [self getModelConfig:modelRecord error:&configError];
325 |
326 | if (configError || !modelConfig) {
327 | dispatch_async(dispatch_get_main_queue(), ^{
328 | reject(@"MODEL_ERROR", @"Failed to get model config", configError);
329 | });
330 | return;
331 | }
332 |
333 | // Update model properties - with null checks
334 | NSString* modelLib = modelRecord[@"model_lib"];
335 |
336 | if (!modelLib) {
337 | dispatch_async(dispatch_get_main_queue(), ^{
338 | reject(@"MODEL_ERROR", @"Invalid model config - missing required fields", nil);
339 | });
340 | return;
341 | }
342 |
343 | // Set model path to just use Documents directory and modelId
344 | NSString* modelId = modelRecord[@"model_id"];
345 | self.modelPath = modelId;
346 | self.modelLib = modelLib;
347 |
348 | // Initialize engine with model
349 | NSURL* modelLocalURL = [self.bundleURL URLByAppendingPathComponent:self.modelPath];
350 |
351 | if (!modelLocalURL) {
352 | dispatch_async(dispatch_get_main_queue(), ^{
353 | reject(@"MODEL_ERROR", @"Failed to construct model path", nil);
354 | });
355 | return;
356 | }
357 | NSString* modelLocalPath = [modelLocalURL path];
358 |
359 | [self.engine reloadWithModelPath:modelLocalPath modelLib:self.modelLib];
360 |
361 | dispatch_async(dispatch_get_main_queue(), ^{
362 | resolve([NSString stringWithFormat:@"Model prepared: %@", instanceId]);
363 | });
364 |
365 | } @catch (NSException* exception) {
366 | dispatch_async(dispatch_get_main_queue(), ^{
367 | reject(@"MODEL_ERROR", exception.reason, nil);
368 | });
369 | }
370 | });
371 | }
372 |
373 | - (NSDictionary*)getModelConfig:(NSDictionary*)modelRecord error:(NSError**)error {
374 | [self downloadModelConfig:modelRecord error:error];
375 | if (*error != nil) {
376 | return nil;
377 | }
378 |
379 | NSString* modelId = modelRecord[@"model_id"];
380 |
381 | // Use the same path construction as downloadModelConfig
382 | NSURL* modelDirURL = [self.bundleURL URLByAppendingPathComponent:modelId];
383 | NSURL* modelConfigURL = [modelDirURL URLByAppendingPathComponent:@"mlc-chat-config.json"];
384 |
385 | NSData* jsonData = [NSData dataWithContentsOfURL:modelConfigURL];
386 | if (!jsonData) {
387 | if (error) {
388 | *error = [NSError errorWithDomain:@"AiModule" code:1 userInfo:@{NSLocalizedDescriptionKey : @"Requested model config not found"}];
389 | }
390 | return nil;
391 | }
392 |
393 | return [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:error];
394 | }
395 |
396 | - (void)downloadModelConfig:(NSDictionary*)modelRecord error:(NSError**)error {
397 | NSString* modelId = modelRecord[@"model_id"];
398 | NSString* modelUrl = modelRecord[@"model_url"];
399 |
400 | if (!modelId || !modelUrl) {
401 | if (error) {
402 | *error = [NSError errorWithDomain:@"AiModule" code:3 userInfo:@{NSLocalizedDescriptionKey : @"Missing required model record fields"}];
403 | }
404 | return;
405 | }
406 |
407 | // Check if config already exists
408 | NSURL* modelDirURL = [self.bundleURL URLByAppendingPathComponent:modelId];
409 | NSURL* modelConfigURL = [modelDirURL URLByAppendingPathComponent:@"mlc-chat-config.json"];
410 | NSURL* ndarrayCacheURL = [modelDirURL URLByAppendingPathComponent:@"ndarray-cache.json"];
411 |
412 | if (!modelDirURL || !modelConfigURL) {
413 | if (error) {
414 | *error = [NSError errorWithDomain:@"AiModule" code:4 userInfo:@{NSLocalizedDescriptionKey : @"Failed to construct config URLs"}];
415 | }
416 | return;
417 | }
418 |
419 | // Create model directory if it doesn't exist
420 | NSError* dirError;
421 | [[NSFileManager defaultManager] createDirectoryAtPath:[modelDirURL path] withIntermediateDirectories:YES attributes:nil error:&dirError];
422 | if (dirError) {
423 | *error = dirError;
424 | return;
425 | }
426 |
427 | // Download and save model config if it doesn't exist
428 | if (![[NSFileManager defaultManager] fileExistsAtPath:[modelConfigURL path]]) {
429 | [self downloadAndSaveConfig:modelUrl configName:@"mlc-chat-config.json" toURL:modelConfigURL error:error];
430 | if (*error != nil)
431 | return;
432 | }
433 |
434 | // Download and save ndarray-cache if it doesn't exist
435 | if (![[NSFileManager defaultManager] fileExistsAtPath:[ndarrayCacheURL path]]) {
436 | [self downloadAndSaveConfig:modelUrl configName:@"ndarray-cache.json" toURL:ndarrayCacheURL error:error];
437 | if (*error != nil)
438 | return;
439 | }
440 |
441 | // Read and parse ndarray cache
442 | NSData* ndarrayCacheData = [NSData dataWithContentsOfURL:ndarrayCacheURL];
443 | if (!ndarrayCacheData) {
444 | if (error) {
445 | *error = [NSError errorWithDomain:@"AiModule" code:2 userInfo:@{NSLocalizedDescriptionKey : @"Failed to read ndarray cache"}];
446 | }
447 | return;
448 | }
449 |
450 | NSError* ndarrayCacheJsonError;
451 | NSDictionary* ndarrayCache = [NSJSONSerialization JSONObjectWithData:ndarrayCacheData options:0 error:&ndarrayCacheJsonError];
452 | if (ndarrayCacheJsonError) {
453 | *error = ndarrayCacheJsonError;
454 | return;
455 | }
456 |
457 | // Download parameter files from ndarray cache
458 | NSArray* records = ndarrayCache[@"records"];
459 | if ([records isKindOfClass:[NSArray class]]) {
460 | for (NSDictionary* record in records) {
461 | NSString* dataPath = record[@"dataPath"];
462 | if (dataPath) {
463 | NSURL* fileURL = [modelDirURL URLByAppendingPathComponent:dataPath];
464 | if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
465 | [self downloadModelFile:modelUrl filename:dataPath toURL:fileURL error:error];
466 | if (*error != nil)
467 | return;
468 | }
469 | }
470 | }
471 | }
472 |
473 | // Read and parse model config
474 | NSData* modelConfigData = [NSData dataWithContentsOfURL:modelConfigURL];
475 | if (!modelConfigData) {
476 | if (error) {
477 | *error = [NSError errorWithDomain:@"AiModule" code:2 userInfo:@{NSLocalizedDescriptionKey : @"Failed to read model config"}];
478 | }
479 | return;
480 | }
481 |
482 | NSError* modelConfigJsonError;
483 | NSDictionary* modelConfig = [NSJSONSerialization JSONObjectWithData:modelConfigData options:0 error:&modelConfigJsonError];
484 | if (modelConfigJsonError) {
485 | *error = modelConfigJsonError;
486 | return;
487 | }
488 |
489 | // Download tokenizer files
490 | NSArray* tokenizerFiles = modelConfig[@"tokenizer_files"];
491 | for (NSString* filename in tokenizerFiles) {
492 | NSURL* fileURL = [modelDirURL URLByAppendingPathComponent:filename];
493 | if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
494 | [self downloadModelFile:modelUrl filename:filename toURL:fileURL error:error];
495 | if (*error != nil)
496 | return;
497 | }
498 | }
499 |
500 | // Download model file
501 | NSString* modelPath = modelConfig[@"model_path"];
502 | if (modelPath) {
503 | NSURL* fileURL = [modelDirURL URLByAppendingPathComponent:modelPath];
504 | if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
505 | [self downloadModelFile:modelUrl filename:modelPath toURL:fileURL error:error];
506 | if (*error != nil)
507 | return;
508 | }
509 | }
510 | }
511 |
512 | - (void)downloadAndSaveConfig:(NSString*)modelUrl configName:(NSString*)configName toURL:(NSURL*)destURL error:(NSError**)error {
513 | NSString* urlString = [NSString stringWithFormat:@"%@/resolve/main/%@", modelUrl, configName];
514 | NSURL* url = [NSURL URLWithString:urlString];
515 |
516 | NSData* configData = [NSData dataWithContentsOfURL:url];
517 | if (!configData) {
518 | if (error) {
519 | *error = [NSError errorWithDomain:@"AiModule"
520 | code:2
521 | userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Failed to download %@", configName]}];
522 | }
523 | return;
524 | }
525 |
526 | if (![configData writeToURL:destURL atomically:YES]) {
527 | if (error) {
528 | *error = [NSError errorWithDomain:@"AiModule"
529 | code:6
530 | userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Failed to write %@", configName]}];
531 | }
532 | return;
533 | }
534 | }
535 |
536 | - (void)downloadModelFile:(NSString*)modelUrl filename:(NSString*)filename toURL:(NSURL*)destURL error:(NSError**)error {
537 | NSString* urlString = [NSString stringWithFormat:@"%@/resolve/main/%@", modelUrl, filename];
538 | NSURL* url = [NSURL URLWithString:urlString];
539 |
540 | NSData* fileData = [NSData dataWithContentsOfURL:url];
541 | if (!fileData) {
542 | if (error) {
543 | *error = [NSError errorWithDomain:@"AiModule"
544 | code:2
545 | userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Failed to download %@", filename]}];
546 | }
547 | return;
548 | }
549 |
550 | if (![fileData writeToURL:destURL atomically:YES]) {
551 | if (error) {
552 | *error = [NSError errorWithDomain:@"AiModule"
553 | code:6
554 | userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Failed to write %@", filename]}];
555 | }
556 | return;
557 | }
558 | }
559 |
560 | RCT_EXPORT_METHOD(downloadModel : (NSString*)instanceId resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject) {
561 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
562 | @try {
563 | // Read app config
564 | NSURL* configURL = [self.bundleURL URLByAppendingPathComponent:@"mlc-app-config.json"];
565 | NSData* jsonData = [NSData dataWithContentsOfURL:configURL];
566 |
567 | if (!jsonData) {
568 | dispatch_async(dispatch_get_main_queue(), ^{
569 | reject(@"MODEL_ERROR", @"Failed to read app config", nil);
570 | });
571 | return;
572 | }
573 |
574 | NSError* error;
575 | NSDictionary* appConfig = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
576 |
577 | if (error) {
578 | dispatch_async(dispatch_get_main_queue(), ^{
579 | reject(@"MODEL_ERROR", @"Failed to parse app config", error);
580 | });
581 | return;
582 | }
583 |
584 | // Find model record
585 | NSArray* modelList = appConfig[@"model_list"];
586 | NSDictionary* modelRecord = nil;
587 |
588 | for (NSDictionary* model in modelList) {
589 | if ([model[@"model_id"] isEqualToString:instanceId]) {
590 | modelRecord = model;
591 | break;
592 | }
593 | }
594 |
595 | if (!modelRecord) {
596 | dispatch_async(dispatch_get_main_queue(), ^{
597 | reject(@"MODEL_ERROR", @"There's no record for requested model", nil);
598 | });
599 | return;
600 | }
601 |
602 | // Send download start event
603 | if (self->hasListeners) {
604 | [self sendEventWithName:@"onDownloadStart" body:nil];
605 | }
606 |
607 | // Get model config and download files
608 | NSError* configError;
609 | NSDictionary* modelConfig = [self getModelConfig:modelRecord error:&configError];
610 |
611 | if (configError || !modelConfig) {
612 | dispatch_async(dispatch_get_main_queue(), ^{
613 | reject(@"MODEL_ERROR", @"Failed to get model config", configError);
614 | });
615 | return;
616 | }
617 |
618 | // Calculate total files to download
619 | NSInteger totalFiles = 0;
620 | __block NSInteger downloadedFiles = 0;
621 |
622 | // Count files from ndarray cache
623 | NSURL* modelDirURL = [self.bundleURL URLByAppendingPathComponent:modelRecord[@"model_id"]];
624 | NSURL* ndarrayCacheURL = [modelDirURL URLByAppendingPathComponent:@"ndarray-cache.json"];
625 | NSData* ndarrayCacheData = [NSData dataWithContentsOfURL:ndarrayCacheURL];
626 | if (ndarrayCacheData) {
627 | NSDictionary* ndarrayCache = [NSJSONSerialization JSONObjectWithData:ndarrayCacheData options:0 error:nil];
628 | NSArray* records = ndarrayCache[@"records"];
629 | if ([records isKindOfClass:[NSArray class]]) {
630 | totalFiles += records.count;
631 | }
632 | }
633 |
634 | // Count tokenizer files
635 | NSArray* tokenizerFiles = modelConfig[@"tokenizer_files"];
636 | if ([tokenizerFiles isKindOfClass:[NSArray class]]) {
637 | totalFiles += tokenizerFiles.count;
638 | }
639 |
640 | // Add model file
641 | if (modelConfig[@"model_path"]) {
642 | totalFiles += 1;
643 | }
644 |
645 | // Add config files
646 | totalFiles += 2; // mlc-chat-config.json and ndarray-cache.json
647 |
648 | // Send progress updates during download
649 | void (^updateProgress)(void) = ^{
650 | downloadedFiles++;
651 | if (self->hasListeners) {
652 | double percentage = (double)downloadedFiles / totalFiles * 100.0;
653 | [self sendEventWithName:@"onDownloadProgress" body:@{@"percentage" : @(percentage)}];
654 | }
655 | };
656 |
657 | // Download config files
658 | [self downloadAndSaveConfig:modelRecord[@"model_url"]
659 | configName:@"mlc-chat-config.json"
660 | toURL:[modelDirURL URLByAppendingPathComponent:@"mlc-chat-config.json"]
661 | error:&error];
662 | if (error) {
663 | dispatch_async(dispatch_get_main_queue(), ^{
664 | reject(@"MODEL_ERROR", @"Failed to download config files", error);
665 | });
666 | return;
667 | }
668 | updateProgress();
669 |
670 | [self downloadAndSaveConfig:modelRecord[@"model_url"] configName:@"ndarray-cache.json" toURL:ndarrayCacheURL error:&error];
671 | if (error) {
672 | dispatch_async(dispatch_get_main_queue(), ^{
673 | reject(@"MODEL_ERROR", @"Failed to download config files", error);
674 | });
675 | return;
676 | }
677 | updateProgress();
678 |
679 | // Download parameter files
680 | NSDictionary* ndarrayCache = [NSJSONSerialization JSONObjectWithData:ndarrayCacheData options:0 error:nil];
681 | NSArray* records = ndarrayCache[@"records"];
682 | if ([records isKindOfClass:[NSArray class]]) {
683 | for (NSDictionary* record in records) {
684 | NSString* dataPath = record[@"dataPath"];
685 | if (dataPath) {
686 | NSURL* fileURL = [modelDirURL URLByAppendingPathComponent:dataPath];
687 | [self downloadModelFile:modelRecord[@"model_url"] filename:dataPath toURL:fileURL error:&error];
688 | if (error) {
689 | dispatch_async(dispatch_get_main_queue(), ^{
690 | reject(@"MODEL_ERROR", @"Failed to download parameter files", error);
691 | });
692 | return;
693 | }
694 | updateProgress();
695 | }
696 | }
697 | }
698 |
699 | // Download tokenizer files
700 | for (NSString* filename in tokenizerFiles) {
701 | NSURL* fileURL = [modelDirURL URLByAppendingPathComponent:filename];
702 | [self downloadModelFile:modelRecord[@"model_url"] filename:filename toURL:fileURL error:&error];
703 | if (error) {
704 | dispatch_async(dispatch_get_main_queue(), ^{
705 | reject(@"MODEL_ERROR", @"Failed to download tokenizer files", error);
706 | });
707 | return;
708 | }
709 | updateProgress();
710 | }
711 |
712 | // Download model file
713 | NSString* modelPath = modelConfig[@"model_path"];
714 | if (modelPath) {
715 | NSURL* fileURL = [modelDirURL URLByAppendingPathComponent:modelPath];
716 | [self downloadModelFile:modelRecord[@"model_url"] filename:modelPath toURL:fileURL error:&error];
717 | if (error) {
718 | dispatch_async(dispatch_get_main_queue(), ^{
719 | reject(@"MODEL_ERROR", @"Failed to download model file", error);
720 | });
721 | return;
722 | }
723 | updateProgress();
724 | }
725 |
726 | // Send download complete event
727 | if (self->hasListeners) {
728 | [self sendEventWithName:@"onDownloadComplete" body:nil];
729 | }
730 |
731 | dispatch_async(dispatch_get_main_queue(), ^{
732 | resolve([NSString stringWithFormat:@"Model downloaded: %@", instanceId]);
733 | });
734 |
735 | } @catch (NSException* exception) {
736 | if (self->hasListeners) {
737 | [self sendEventWithName:@"onDownloadError" body:@{@"message" : exception.reason ?: @"Unknown error"}];
738 | }
739 | dispatch_async(dispatch_get_main_queue(), ^{
740 | reject(@"MODEL_ERROR", exception.reason, nil);
741 | });
742 | }
743 | });
744 | }
745 |
746 | // Don't compile this code when we build for the old architecture.
747 | #ifdef RCT_NEW_ARCH_ENABLED
748 | - (std::shared_ptr)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams&)params {
749 | return std::make_shared(params);
750 | }
751 | #endif
752 |
753 | @end
754 |
--------------------------------------------------------------------------------
/ios/BackgroundWorker.h:
--------------------------------------------------------------------------------
1 | //
2 | // BackgroundWorker.h
3 | // Pods
4 | //
5 | // Created by Szymon Rybczak on 19/07/2024.
6 | //
7 |
8 | #import
9 |
10 | NS_ASSUME_NONNULL_BEGIN
11 |
12 | @interface BackgroundWorker : NSThread
13 | - (instancetype)initWithTask:(void (^)(void))task;
14 | @end
15 |
16 | NS_ASSUME_NONNULL_END
17 |
--------------------------------------------------------------------------------
/ios/BackgroundWorker.mm:
--------------------------------------------------------------------------------
1 | //
2 | // BackgroundWorker.mm
3 | // Pods
4 | //
5 | // Created by Szymon Rybczak on 19/07/2024.
6 | //
7 |
8 | #import "BackgroundWorker.h"
9 |
10 | @implementation BackgroundWorker {
11 | void (^_task)(void);
12 | }
13 |
14 | - (instancetype)initWithTask:(void (^)(void))task {
15 | self = [super init];
16 | if (self) {
17 | _task = [task copy];
18 | }
19 | return self;
20 | }
21 |
22 | - (void)main {
23 | if (_task) {
24 | _task();
25 | }
26 | }
27 |
28 | @end
29 |
--------------------------------------------------------------------------------
/ios/EngineState.h:
--------------------------------------------------------------------------------
1 | //
2 | // MLCEngine.h
3 | // Pods
4 | //
5 | // Created by Szymon Rybczak on 19/07/2024.
6 | //
7 |
8 | #import "LLMEngine.h"
9 | #import
10 |
11 | NS_ASSUME_NONNULL_BEGIN
12 |
13 | @interface EngineState : NSObject
14 | @property(nonatomic, strong) NSMutableDictionary* requestStateMap;
15 |
16 | - (void)chatCompletionWithJSONFFIEngine:(JSONFFIEngine*)jsonFFIEngine
17 | request:(NSDictionary*)request
18 | completion:(void (^)(NSString* response))completion;
19 | - (void)streamCallbackWithResult:(NSString*)result;
20 | @end
21 | NS_ASSUME_NONNULL_END
22 |
--------------------------------------------------------------------------------
/ios/EngineState.mm:
--------------------------------------------------------------------------------
1 | //
2 | // EngineState.mm
3 | // Pods
4 | //
5 | // Created by Szymon Rybczak on 19/07/2024.
6 | //
7 |
8 | #import "EngineState.h"
9 | #import "LLMEngine.h"
10 |
11 | @implementation EngineState
12 |
13 | - (instancetype)init {
14 | self = [super init];
15 | if (self) {
16 | _requestStateMap = [NSMutableDictionary new];
17 | }
18 | return self;
19 | }
20 |
21 | - (void)chatCompletionWithJSONFFIEngine:(JSONFFIEngine*)jsonFFIEngine
22 | request:(NSDictionary*)request
23 | completion:(void (^)(NSString* response))completion {
24 | NSError* error;
25 | NSData* jsonData = [NSJSONSerialization dataWithJSONObject:request options:0 error:&error];
26 | if (error) {
27 | NSLog(@"Error encoding JSON: %@", error);
28 | return;
29 | }
30 |
31 | NSString* jsonRequest = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
32 | NSString* requestID = [[NSUUID UUID] UUIDString];
33 |
34 | // Store the completion handler in the requestStateMap
35 | self.requestStateMap[requestID] = completion;
36 |
37 | [jsonFFIEngine chatCompletion:jsonRequest requestID:requestID];
38 | }
39 |
40 | - (void)streamCallbackWithResult:(NSString*)result {
41 | NSError* error;
42 | NSArray* responses = [NSJSONSerialization JSONObjectWithData:[result dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error];
43 | if (error) {
44 | NSLog(@"Error decoding JSON: %@", error);
45 | return;
46 | }
47 |
48 | for (NSDictionary* res in responses) {
49 | NSString* requestID = res[@"id"];
50 | void (^completion)(NSString*) = self.requestStateMap[requestID];
51 | if (completion) {
52 | completion(result);
53 | if (res[@"usage"]) {
54 | [self.requestStateMap removeObjectForKey:requestID];
55 | }
56 | }
57 | }
58 | }
59 |
60 | @end
61 |
--------------------------------------------------------------------------------
/ios/LLMEngine.h:
--------------------------------------------------------------------------------
1 | //
2 | // LLMEngine.h
3 | // Pods
4 | //
5 | // Created by Szymon Rybczak on 19/07/2024.
6 | //
7 |
8 | #import
9 | #import
10 |
11 | /**
12 | * This is an internal Raw JSON FFI Engine that redirects request to internal JSON FFI Engine in C++
13 | */
14 | @interface JSONFFIEngine : NSObject
15 |
16 | - (void)initBackgroundEngine:(void (^)(NSString*))streamCallback;
17 |
18 | - (void)reload:(NSString*)engineConfig;
19 |
20 | - (void)unload;
21 |
22 | - (void)reset;
23 |
24 | - (void)chatCompletion:(NSString*)requestJSON requestID:(NSString*)requestID;
25 |
26 | - (void)abort:(NSString*)requestID;
27 |
28 | - (void)runBackgroundLoop;
29 |
30 | - (void)runBackgroundStreamBackLoop;
31 |
32 | - (void)exitBackgroundLoop;
33 |
34 | @end
35 |
--------------------------------------------------------------------------------
/ios/LLMEngine.mm:
--------------------------------------------------------------------------------
1 | //
2 | // LLMEngine.mm
3 | // Pods
4 | //
5 | // Created by Szymon Rybczak on 19/07/2024.
6 | //
7 |
8 | #import
9 | #import
10 | #include
11 |
12 | #include "LLMEngine.h"
13 |
14 | #define TVM_USE_LIBBACKTRACE 0
15 | #define DMLC_USE_LOGGING_LIBRARY
16 |
17 | #include
18 | #include
19 |
20 | using namespace tvm::runtime;
21 |
22 | @implementation JSONFFIEngine {
23 | // Internal c++ classes
24 | // internal module backed by JSON FFI
25 | Module json_ffi_engine_;
26 | // member functions
27 | PackedFunc init_background_engine_func_;
28 | PackedFunc unload_func_;
29 | PackedFunc reload_func_;
30 | PackedFunc reset_func_;
31 | PackedFunc chat_completion_func_;
32 | PackedFunc abort_func_;
33 | PackedFunc run_background_loop_func_;
34 | PackedFunc run_background_stream_back_loop_func_;
35 | PackedFunc exit_background_loop_func_;
36 | }
37 |
38 | - (instancetype)init {
39 | if (self = [super init]) {
40 | const PackedFunc* f_json_ffi_create = Registry::Get("mlc.json_ffi.CreateJSONFFIEngine");
41 | ICHECK(f_json_ffi_create) << "Cannot find mlc.json_ffi.CreateJSONFFIEngine";
42 | json_ffi_engine_ = (*f_json_ffi_create)();
43 | init_background_engine_func_ = json_ffi_engine_->GetFunction("init_background_engine");
44 | reload_func_ = json_ffi_engine_->GetFunction("reload");
45 | unload_func_ = json_ffi_engine_->GetFunction("unload");
46 | reset_func_ = json_ffi_engine_->GetFunction("reset");
47 | chat_completion_func_ = json_ffi_engine_->GetFunction("chat_completion");
48 | abort_func_ = json_ffi_engine_->GetFunction("abort");
49 | run_background_loop_func_ = json_ffi_engine_->GetFunction("run_background_loop");
50 | run_background_stream_back_loop_func_ = json_ffi_engine_->GetFunction("run_background_stream_back_loop");
51 | exit_background_loop_func_ = json_ffi_engine_->GetFunction("exit_background_loop");
52 |
53 | ICHECK(init_background_engine_func_ != nullptr);
54 | ICHECK(reload_func_ != nullptr);
55 | ICHECK(unload_func_ != nullptr);
56 | ICHECK(reset_func_ != nullptr);
57 | ICHECK(chat_completion_func_ != nullptr);
58 | ICHECK(abort_func_ != nullptr);
59 | ICHECK(run_background_loop_func_ != nullptr);
60 | ICHECK(run_background_stream_back_loop_func_ != nullptr);
61 | ICHECK(exit_background_loop_func_ != nullptr);
62 | }
63 | return self;
64 | }
65 |
66 | - (void)initBackgroundEngine:(void (^)(NSString*))streamCallback {
67 | TypedPackedFunc internal_stream_callback(
68 | [streamCallback](String value) { streamCallback([NSString stringWithUTF8String:value.c_str()]); });
69 | int device_type = kDLMetal;
70 | int device_id = 0;
71 | init_background_engine_func_(device_type, device_id, internal_stream_callback);
72 | }
73 |
74 | - (void)reload:(NSString*)engineConfigJson {
75 | std::string engine_config = engineConfigJson.UTF8String;
76 | reload_func_(engine_config);
77 | }
78 |
79 | - (void)unload {
80 | unload_func_();
81 | }
82 |
83 | - (void)reset {
84 | reset_func_();
85 | }
86 |
87 | - (void)chatCompletion:(NSString*)requestJSON requestID:(NSString*)requestID {
88 | std::string request_json = requestJSON.UTF8String;
89 | std::string request_id = requestID.UTF8String;
90 | chat_completion_func_(request_json, request_id);
91 | }
92 |
93 | - (void)abort:(NSString*)requestID {
94 | std::string request_id = requestID.UTF8String;
95 | abort_func_(request_id);
96 | }
97 |
98 | - (void)runBackgroundLoop {
99 | run_background_loop_func_();
100 | }
101 |
102 | - (void)runBackgroundStreamBackLoop {
103 | run_background_stream_back_loop_func_();
104 | }
105 |
106 | - (void)exitBackgroundLoop {
107 | exit_background_loop_func_();
108 | }
109 |
110 | @end
111 |
--------------------------------------------------------------------------------
/ios/MLCEngine.h:
--------------------------------------------------------------------------------
1 | //
2 | // MLCEngine.h
3 | // Pods
4 | //
5 | // Created by Szymon Rybczak on 19/07/2024.
6 | //
7 |
8 | #import
9 |
10 | NS_ASSUME_NONNULL_BEGIN
11 |
12 | @interface MLCEngine : NSObject
13 |
14 | - (instancetype)init;
15 |
16 | - (void)reloadWithModelPath:(NSString*)modelPath modelLib:(NSString*)modelLib;
17 | - (void)reset;
18 | - (void)unload;
19 |
20 | - (void)chatCompletionWithMessages:(NSArray*)messages completion:(void (^)(id response))completion;
21 | @end
22 |
23 | NS_ASSUME_NONNULL_END
24 |
--------------------------------------------------------------------------------
/ios/MLCEngine.mm:
--------------------------------------------------------------------------------
1 | //
2 | // MLCEngine.mm
3 | // Pods
4 | //
5 | // Created by Szymon Rybczak on 19/07/2024.
6 | //
7 |
8 | #import "MLCEngine.h"
9 | #import "BackgroundWorker.h"
10 | #import "EngineState.h"
11 | #import "LLMEngine.h"
12 |
13 | // Private class extension for MLCEngine
14 | @interface MLCEngine ()
15 | @property(nonatomic, strong) EngineState* state;
16 | @property(nonatomic, strong) JSONFFIEngine* jsonFFIEngine;
17 | @property(nonatomic, strong) NSMutableArray* threads;
18 | @end
19 |
20 | @implementation MLCEngine
21 |
22 | - (instancetype)init {
23 | self = [super init];
24 | if (self) {
25 | _state = [[EngineState alloc] init];
26 | _jsonFFIEngine = [[JSONFFIEngine alloc] init];
27 | _threads = [NSMutableArray array];
28 |
29 | [_jsonFFIEngine initBackgroundEngine:^(NSString* _Nullable result) {
30 | [self.state streamCallbackWithResult:result];
31 | }];
32 |
33 | BackgroundWorker* backgroundWorker = [[BackgroundWorker alloc] initWithTask:^{
34 | [NSThread setThreadPriority:1.0];
35 | [self.jsonFFIEngine runBackgroundLoop];
36 | }];
37 |
38 | BackgroundWorker* backgroundStreamBackWorker = [[BackgroundWorker alloc] initWithTask:^{
39 | [self.jsonFFIEngine runBackgroundStreamBackLoop];
40 | }];
41 |
42 | backgroundWorker.qualityOfService = NSQualityOfServiceUserInteractive;
43 | [_threads addObject:backgroundWorker];
44 | [_threads addObject:backgroundStreamBackWorker];
45 | [backgroundWorker start];
46 | [backgroundStreamBackWorker start];
47 | }
48 | return self;
49 | }
50 |
51 | - (void)dealloc {
52 | [self.jsonFFIEngine exitBackgroundLoop];
53 | }
54 |
55 | - (void)reloadWithModelPath:(NSString*)modelPath modelLib:(NSString*)modelLib {
56 | NSString* engineConfig =
57 | [NSString stringWithFormat:@"{\"model\": \"%@\", \"model_lib\": \"system://%@\", \"mode\": \"interactive\"}", modelPath, modelLib];
58 | [self.jsonFFIEngine reload:engineConfig];
59 | }
60 |
61 | - (void)reset {
62 | [self.jsonFFIEngine reset];
63 | }
64 |
65 | - (void)unload {
66 | [self.jsonFFIEngine unload];
67 | }
68 |
69 | - (void)chatCompletionWithMessages:(NSArray*)messages completion:(void (^)(NSString* response))completion {
70 | NSDictionary* request = @{@"messages" : messages, @"temperature" : @0.6};
71 |
72 | [self.state chatCompletionWithJSONFFIEngine:self.jsonFFIEngine request:request completion:completion];
73 | }
74 |
75 | @end
76 |
--------------------------------------------------------------------------------
/lefthook.yml:
--------------------------------------------------------------------------------
1 | pre-commit:
2 | parallel: true
3 | commands:
4 | lint:
5 | glob: '*.{js,ts,jsx,tsx}'
6 | run: npx eslint {staged_files}
7 | types:
8 | glob: '*.{js, ts, jsx, tsx}'
9 | run: npx tsc --noEmit {staged_files}
10 | commit-msg:
11 | parallel: true
12 | commands:
13 | commitlint:
14 | run: npx commitlint --edit
15 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-native-ai",
3 | "version": "0.1.0",
4 | "description": "React Native AI",
5 | "main": "lib/commonjs/index",
6 | "module": "lib/module/index",
7 | "types": "lib/typescript/src/index.d.ts",
8 | "react-native": "src/index",
9 | "source": "src/index",
10 | "bin": {
11 | "mlc-prepare": "scripts/mlc-prepare.js"
12 | },
13 | "files": [
14 | "src",
15 | "lib",
16 | "android",
17 | "ios",
18 | "cpp",
19 | "*.podspec",
20 | "./scripts/mlc-prepare.js",
21 | "!ios/build",
22 | "!android/build",
23 | "!android/gradle",
24 | "!android/gradlew",
25 | "!android/gradlew.bat",
26 | "!android/local.properties",
27 | "!**/__tests__",
28 | "!**/__fixtures__",
29 | "!**/__mocks__",
30 | "!**/.*"
31 | ],
32 | "scripts": {
33 | "example": "yarn workspace react-native-ai-example",
34 | "test": "jest",
35 | "typecheck": "tsc --noEmit",
36 | "lint": "eslint \"**/*.{js,ts,tsx}\"",
37 | "clean": "del-cli lib",
38 | "prepare": "bob build",
39 | "release": "release-it",
40 | "format:ios": "./scripts/format-ios.sh",
41 | "format:android": "./scripts/format-android.sh",
42 | "prestart": "node scripts/mlc-prepare.js"
43 | },
44 | "keywords": [
45 | "react-native",
46 | "ios",
47 | "android"
48 | ],
49 | "repository": {
50 | "type": "git",
51 | "url": "git+https://github.com/callstackincubator/ai.git"
52 | },
53 | "author": "szymonrybczak (https://github.com/szymonrybczak)",
54 | "license": "MIT",
55 | "bugs": {
56 | "url": "https://github.com/callstackincubator/ai/issues"
57 | },
58 | "homepage": "https://github.com/callstackincubator/ai#readme",
59 | "publishConfig": {
60 | "registry": "https://registry.npmjs.org/"
61 | },
62 | "devDependencies": {
63 | "@commitlint/config-conventional": "^17.0.2",
64 | "@evilmartians/lefthook": "^1.5.0",
65 | "@react-native/eslint-config": "^0.73.1",
66 | "@release-it/conventional-changelog": "^5.0.0",
67 | "@types/jest": "^29.5.5",
68 | "@types/react": "^18.2.44",
69 | "commitlint": "^17.0.2",
70 | "del-cli": "^5.1.0",
71 | "eslint": "^8.51.0",
72 | "eslint-config-prettier": "^9.0.0",
73 | "eslint-plugin-prettier": "^5.0.1",
74 | "jest": "^29.7.0",
75 | "prettier": "^3.0.3",
76 | "react": "18.2.0",
77 | "react-native": "0.74.2",
78 | "react-native-builder-bob": "^0.23.2",
79 | "release-it": "^15.0.0",
80 | "turbo": "^2.4.2",
81 | "typescript": "^5.2.2"
82 | },
83 | "resolutions": {
84 | "@types/react": "^18.2.44"
85 | },
86 | "peerDependencies": {
87 | "react": "*",
88 | "react-native": "*"
89 | },
90 | "workspaces": [
91 | "example"
92 | ],
93 | "packageManager": "yarn@3.6.1",
94 | "jest": {
95 | "preset": "react-native",
96 | "modulePathIgnorePatterns": [
97 | "/example/node_modules",
98 | "/lib/"
99 | ]
100 | },
101 | "commitlint": {
102 | "extends": [
103 | "@commitlint/config-conventional"
104 | ]
105 | },
106 | "release-it": {
107 | "git": {
108 | "commitMessage": "chore: release ${version}",
109 | "tagName": "v${version}"
110 | },
111 | "npm": {
112 | "publish": true
113 | },
114 | "github": {
115 | "release": true
116 | },
117 | "plugins": {
118 | "@release-it/conventional-changelog": {
119 | "preset": "angular"
120 | }
121 | }
122 | },
123 | "eslintConfig": {
124 | "root": true,
125 | "extends": [
126 | "@react-native",
127 | "prettier"
128 | ],
129 | "rules": {
130 | "prettier/prettier": [
131 | "error",
132 | {
133 | "quoteProps": "consistent",
134 | "singleQuote": true,
135 | "tabWidth": 2,
136 | "trailingComma": "es5",
137 | "useTabs": false
138 | }
139 | ]
140 | }
141 | },
142 | "eslintIgnore": [
143 | "node_modules/",
144 | "lib/"
145 | ],
146 | "prettier": {
147 | "quoteProps": "consistent",
148 | "singleQuote": true,
149 | "tabWidth": 2,
150 | "trailingComma": "es5",
151 | "useTabs": false
152 | },
153 | "react-native-builder-bob": {
154 | "source": "src",
155 | "output": "lib",
156 | "targets": [
157 | "commonjs",
158 | "module",
159 | [
160 | "typescript",
161 | {
162 | "project": "tsconfig.build.json"
163 | }
164 | ]
165 | ]
166 | },
167 | "codegenConfig": {
168 | "name": "RNAiSpec",
169 | "type": "modules",
170 | "jsSrcsDir": "src"
171 | },
172 | "dependencies": {
173 | "@ai-sdk/provider": "^1.0.7",
174 | "@stardazed/streams-text-encoding": "^1.0.2",
175 | "@ungap/structured-clone": "^1.3.0",
176 | "text-encoding": "^0.7.0",
177 | "web-streams-polyfill": "3.3.3"
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/react-native-ai.podspec:
--------------------------------------------------------------------------------
1 | require "json"
2 |
3 | package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4 |
5 | # Check for MLC-LLM directory
6 | mlc_source_dir = if ENV['MLC_LLM_SOURCE_DIR'] && Dir.exist?(ENV['MLC_LLM_SOURCE_DIR'])
7 | ENV['MLC_LLM_SOURCE_DIR']
8 | else
9 | raise "❌ MLC-LLM directory not found! Please set MLC_LLM_SOURCE_DIR environment variable pointing to your MLC-LLM repository"
10 | end
11 |
12 | puts "✅ Using MLC-LLM from: #{mlc_source_dir}"
13 |
14 | folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
15 |
16 | Pod::Spec.new do |s|
17 | s.name = "react-native-ai"
18 | s.version = package["version"]
19 | s.summary = package["description"]
20 | s.homepage = package["homepage"]
21 | s.license = package["license"]
22 | s.authors = package["author"]
23 |
24 | s.platforms = { :ios => min_ios_version_supported }
25 | s.source = { :git => "https://github.com/callstackincubator/ai.git", :tag => "#{s.version}" }
26 |
27 | s.source_files = "ios/**/*.{h,m,mm}"
28 |
29 | # Define and validate MLC-LLM header paths
30 | mlc_header_paths = [
31 | File.join(mlc_source_dir, '3rdparty', 'tvm', 'include'),
32 | File.join(mlc_source_dir, '3rdparty', 'tvm', '3rdparty', 'dmlc-core', 'include'),
33 | File.join(mlc_source_dir, '3rdparty', 'tvm', '3rdparty', 'dlpack', 'include')
34 | ]
35 |
36 | mlc_header_paths.each do |path|
37 | unless Dir.exist?(path)
38 | raise "❌ Required MLC-LLM header directory not found: #{path}"
39 | end
40 | end
41 |
42 | s.subspec 'MLCEngineObjC' do |ss|
43 | ss.source_files = 'ios/**/*.{h,m,mm}'
44 | ss.private_header_files = 'ios/ObjC/Private/*.h'
45 | ss.pod_target_xcconfig = {
46 | 'HEADER_SEARCH_PATHS' => mlc_header_paths
47 | }
48 | end
49 |
50 | # Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
51 | # See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
52 | if respond_to?(:install_modules_dependencies, true)
53 | install_modules_dependencies(s)
54 | else
55 | s.dependency "React-Core"
56 |
57 | # Don't install the dependencies when we run `pod install` in the old architecture.
58 | if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
59 | s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
60 | s.pod_target_xcconfig = {
61 | "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
62 | "OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
63 | "CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
64 | }
65 | s.dependency "React-Codegen"
66 | s.dependency "RCT-Folly"
67 | s.dependency "RCTRequired"
68 | s.dependency "RCTTypeSafety"
69 | s.dependency "ReactCommon/turbomodule/core"
70 | end
71 | end
72 | end
73 |
--------------------------------------------------------------------------------
/scripts/format-android.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | cd android && ktlint --reporter=checkstyle,output=build/ktlint-report.xml --relative --editorconfig=../.editorconfig $1
--------------------------------------------------------------------------------
/scripts/format-ios.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | find ios -type f \( -name "*.h" -o -name "*.cpp" -o -name "*.m" -o -name "*.mm" \) -print0 | while read -d $'\0' file; do
4 | echo "Formatting $file"
5 | clang-format -style=file:./ios/.clang-format -i "$file" $1
6 | done
--------------------------------------------------------------------------------
/scripts/mlc-prepare.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | const { execSync } = require('child_process');
4 | const path = require('path');
5 | const fs = require('fs');
6 |
7 | // Check for required dependencies
8 | function checkDependency(command, name) {
9 | try {
10 | execSync(`which ${command}`, { stdio: 'ignore' });
11 | console.log(`✅ ${name} found in PATH`);
12 | return true;
13 | } catch (error) {
14 | console.error(
15 | `❌ ${name} not found in PATH. Please install ${name} first.`
16 | );
17 | return false;
18 | }
19 | }
20 |
21 | // Validate required dependencies
22 | const hasGitLFS = checkDependency('git-lfs', 'Git LFS');
23 | const hasRustup = checkDependency('rustup', 'Rustup');
24 |
25 | if (!hasGitLFS || !hasRustup) {
26 | console.error('\n🔧 Please install the missing dependencies:');
27 | if (!hasGitLFS) {
28 | console.error('- Git LFS: https://git-lfs.com');
29 | }
30 | if (!hasRustup) {
31 | console.error('- Rustup: https://rustup.rs');
32 | }
33 | process.exit(1);
34 | }
35 |
36 | const projectRoot = process.cwd();
37 |
38 | const args = process.argv.slice(2);
39 | const rootIndex = args.findIndex((arg) => arg === '--root');
40 | const platformIndex = args.findIndex((arg) => arg === '--platform');
41 |
42 | const rootDir = rootIndex !== -1 ? args[rootIndex + 1] : projectRoot;
43 | const platformArg =
44 | platformIndex !== -1 ? args[platformIndex + 1]?.toLowerCase() : null;
45 |
46 | let platforms = ['android', 'ios'];
47 | if (platformArg) {
48 | if (platformArg !== 'android' && platformArg !== 'ios') {
49 | console.error('❌ Invalid platform. Must be either "android" or "ios"');
50 | process.exit(1);
51 | }
52 | platforms = [platformArg];
53 | }
54 |
55 | if (!process.env.MLC_LLM_SOURCE_DIR) {
56 | console.error(
57 | 'MLC LLM home is not specified. Please obtain a copy of MLC LLM source code by cloning https://github.com/mlc-ai/mlc-llm, and set environment variable "MLC_LLM_SOURCE_DIR=path/to/mlc-llm"'
58 | );
59 | process.exit(1);
60 | }
61 |
62 | const configPath = path.join(rootDir, 'mlc-config.json');
63 | const androidPath = path.join(rootDir, 'android');
64 | const iosPath = path.join(rootDir, 'ios');
65 |
66 | if (!fs.existsSync(configPath)) {
67 | console.error('❌ Config file not found in project root: mlc-config.json');
68 | process.exit(1);
69 | }
70 |
71 | const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
72 | console.log(config);
73 |
74 | if (platforms.includes('android')) {
75 | if (!process.env.ANDROID_NDK || !process.env.TVM_NDK_CC) {
76 | console.error(
77 | '❌ Missing required environment variables for Android build:'
78 | );
79 | if (!process.env.ANDROID_NDK) console.error('- ANDROID_NDK not set');
80 | if (!process.env.TVM_NDK_CC) console.error('- TVM_NDK_CC not set');
81 | console.error('\nPlease set these variables following the guide at:');
82 | console.error('https://llm.mlc.ai/docs/deploy/android.html#id2');
83 |
84 | // Remove Android from platforms to process
85 | platforms = platforms.filter((p) => p !== 'android');
86 |
87 | // Only exit if Android was the only platform
88 | if (platforms.length === 0) {
89 | process.exit(1);
90 | }
91 | }
92 |
93 | const androidConfig = JSON.stringify(
94 | {
95 | device: 'android',
96 | model_list: config.android.map((model) => ({
97 | ...model,
98 | bundle_weight: false,
99 | })),
100 | },
101 | null,
102 | 2
103 | );
104 | fs.writeFileSync(
105 | path.join(androidPath, 'mlc-package-config.json'),
106 | androidConfig
107 | );
108 | }
109 |
110 | if (platforms.includes('ios')) {
111 | const iosConfig = JSON.stringify(
112 | {
113 | device: 'iphone',
114 | model_list: config.iphone.map((model) => ({
115 | ...model,
116 | bundle_weight: false,
117 | })),
118 | },
119 | null,
120 | 2
121 | );
122 | fs.writeFileSync(path.join(iosPath, 'mlc-package-config.json'), iosConfig);
123 | }
124 |
125 | console.log('🚀 Copying config to selected platforms...');
126 |
127 | if (platforms.includes('android')) {
128 | console.log('📦 Running "mlc_llm package" for Android...');
129 | execSync('cd android && mlc_llm package', { stdio: 'inherit' });
130 | }
131 |
132 | if (platforms.includes('ios')) {
133 | console.log('📦 Running "mlc_llm package" for iOS...');
134 | execSync('cd ios && mlc_llm package', { stdio: 'inherit' });
135 | }
136 |
137 | console.log('✅ Model packaging complete!');
138 |
--------------------------------------------------------------------------------
/src/NativeAi.ts:
--------------------------------------------------------------------------------
1 | import type { TurboModule } from 'react-native';
2 | import { TurboModuleRegistry } from 'react-native';
3 | import type { AiModelSettings, Message } from './index';
4 |
5 | export interface Spec extends TurboModule {
6 | getModel(name: string): Promise; // Returns JSON string of ModelInstance
7 | getModels(): Promise; // Returns array with available models
8 | doGenerate(instanceId: string, messages: Message[]): Promise;
9 | doStream(instanceId: string, messages: Message[]): Promise;
10 | downloadModel(instanceId: string): Promise; // Ensures the model is on the device
11 | prepareModel(instanceId: string): Promise; // Prepares the model for use, if model is not downloaded it will call downloadModel
12 | }
13 |
14 | export default TurboModuleRegistry.getEnforcing('Ai');
15 |
--------------------------------------------------------------------------------
/src/__tests__/index.test.tsx:
--------------------------------------------------------------------------------
1 | it.todo('write a test');
2 |
--------------------------------------------------------------------------------
/src/index.tsx:
--------------------------------------------------------------------------------
1 | import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
2 | import {
3 | type LanguageModelV1,
4 | type LanguageModelV1CallOptions,
5 | type LanguageModelV1CallWarning,
6 | type LanguageModelV1FinishReason,
7 | type LanguageModelV1FunctionToolCall,
8 | type LanguageModelV1StreamPart,
9 | } from '@ai-sdk/provider';
10 | import './polyfills';
11 | import { LogBox, type EmitterSubscription } from 'react-native';
12 | import {
13 | ReadableStream,
14 | ReadableStreamDefaultController,
15 | } from 'web-streams-polyfill';
16 |
17 | const LINKING_ERROR =
18 | `The package 'react-native-ai' doesn't seem to be linked. Make sure: \n\n` +
19 | Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
20 | '- You rebuilt the app after installing the package\n' +
21 | '- You are not using Expo Go\n';
22 |
23 | // @ts-expect-error
24 | const isTurboModuleEnabled = global.__turboModuleProxy != null;
25 |
26 | const AiModule = isTurboModuleEnabled
27 | ? require('./NativeAi').default
28 | : NativeModules.Ai;
29 |
30 | const Ai = AiModule
31 | ? AiModule
32 | : new Proxy(
33 | {},
34 | {
35 | get() {
36 | throw new Error(LINKING_ERROR);
37 | },
38 | }
39 | );
40 |
41 | export default Ai;
42 |
43 | export interface AiModelSettings extends Record {
44 | model_id?: string;
45 | }
46 |
47 | export interface Model {
48 | modelId: string;
49 | modelLib: string;
50 | }
51 |
52 | export interface Message {
53 | role: 'assistant' | 'system' | 'tool' | 'user';
54 | content: string;
55 | }
56 |
57 | export interface DownloadProgress {
58 | percentage: number;
59 | }
60 |
61 | LogBox.ignoreLogs(['new NativeEventEmitter', 'Avatar:']); // Ignore log notification by message
62 |
63 | class AiModel implements LanguageModelV1 {
64 | readonly specificationVersion = 'v1';
65 | readonly defaultObjectGenerationMode = 'json';
66 | readonly provider = 'gemini-nano';
67 | public modelId: string;
68 | private options: AiModelSettings;
69 |
70 | constructor(modelId: string, options: AiModelSettings = {}) {
71 | this.modelId = modelId;
72 | this.options = options;
73 |
74 | console.debug('init:', this.modelId);
75 | }
76 |
77 | private model!: Model;
78 | async getModel() {
79 | this.model = await Ai.getModel(this.modelId);
80 |
81 | return this.model;
82 | }
83 |
84 | async doGenerate(options: LanguageModelV1CallOptions): Promise<{
85 | text?: string;
86 | toolCalls?: Array;
87 | finishReason: LanguageModelV1FinishReason;
88 | usage: {
89 | promptTokens: number;
90 | completionTokens: number;
91 | };
92 | rawCall: {
93 | rawPrompt: unknown;
94 | rawSettings: Record;
95 | };
96 | }> {
97 | const model = await this.getModel();
98 | const messages = options.prompt;
99 | const extractedMessages = messages.map((message): Message => {
100 | let content = '';
101 |
102 | if (Array.isArray(message.content)) {
103 | content = message.content
104 | .map((messageContent) =>
105 | messageContent.type === 'text'
106 | ? messageContent.text
107 | : messageContent
108 | )
109 | .join('');
110 | }
111 |
112 | return {
113 | role: message.role,
114 | content: content,
115 | };
116 | });
117 |
118 | let text = '';
119 |
120 | if (messages.length > 0) {
121 | text = await Ai.doGenerate(model.modelId, extractedMessages);
122 | }
123 |
124 | return {
125 | text,
126 | finishReason: 'stop',
127 | usage: {
128 | promptTokens: 0,
129 | completionTokens: 0,
130 | },
131 | rawCall: {
132 | rawPrompt: options,
133 | rawSettings: {},
134 | },
135 | };
136 | }
137 |
138 | stream: ReadableStream | null = null;
139 | controller: ReadableStreamDefaultController | null =
140 | null;
141 | streamId: string | null = null;
142 | chatUpdateListener: EmitterSubscription | null = null;
143 | chatCompleteListener: EmitterSubscription | null = null;
144 | chatErrorListener: EmitterSubscription | null = null;
145 | isStreamClosed: boolean = false;
146 |
147 | public doStream = async (
148 | options: LanguageModelV1CallOptions
149 | ): Promise<{
150 | stream: ReadableStream;
151 | rawCall: { rawPrompt: unknown; rawSettings: Record };
152 | rawResponse?: { headers?: Record };
153 | warnings?: LanguageModelV1CallWarning[];
154 | }> => {
155 | // Reset stream state
156 | this.isStreamClosed = false;
157 | const messages = options.prompt;
158 | const extractedMessages = messages.map((message): Message => {
159 | let content = '';
160 |
161 | if (Array.isArray(message.content)) {
162 | content = message.content
163 | .map((messageContent) =>
164 | messageContent.type === 'text'
165 | ? messageContent.text
166 | : messageContent
167 | )
168 | .join('');
169 | }
170 |
171 | return {
172 | role: message.role,
173 | content: content,
174 | };
175 | });
176 | const model = await this.getModel();
177 |
178 | const stream = new ReadableStream({
179 | start: (controller) => {
180 | this.controller = controller;
181 |
182 | const eventEmitter =
183 | Platform.OS === 'android'
184 | ? new NativeEventEmitter()
185 | : new NativeEventEmitter(NativeModules.Ai);
186 |
187 | this.chatCompleteListener = eventEmitter.addListener(
188 | 'onChatComplete',
189 | () => {
190 | try {
191 | if (!this.isStreamClosed && this.controller) {
192 | this.controller.enqueue({
193 | type: 'finish',
194 | finishReason: 'stop',
195 | usage: {
196 | promptTokens: 0,
197 | completionTokens: 0,
198 | },
199 | });
200 | this.isStreamClosed = true;
201 | this.controller.close();
202 | }
203 | } catch (error) {
204 | console.error('🔴 [Stream] Error in complete handler:', error);
205 | }
206 | }
207 | );
208 |
209 | this.chatErrorListener = eventEmitter.addListener(
210 | 'onChatUpdate',
211 | (data) => {
212 | console.log(
213 | '🟢 [Stream] Update data:',
214 | JSON.stringify(data, null, 2)
215 | );
216 | try {
217 | if (!this.isStreamClosed && this.controller) {
218 | if (data.error) {
219 | this.controller.enqueue({ type: 'error', error: data.error });
220 | this.isStreamClosed = true;
221 | this.controller.close();
222 | } else {
223 | this.controller.enqueue({
224 | type: 'text-delta',
225 | textDelta: data.content || '',
226 | });
227 | }
228 | } else {
229 | console.log(
230 | '🟡 [Stream] Cannot update - stream closed or no controller'
231 | );
232 | }
233 | } catch (error) {
234 | console.error('🔴 [Stream] Error in update handler:', error);
235 | }
236 | }
237 | );
238 |
239 | if (!model) {
240 | console.error('🔴 [Stream] Model not initialized');
241 | throw new Error('Model not initialized');
242 | }
243 |
244 | console.log(
245 | '🔵 [Stream] Starting native stream with model:',
246 | model.modelId
247 | );
248 | Ai.doStream(model.modelId, extractedMessages);
249 | },
250 | cancel: () => {
251 | console.log('🟡 [Stream] Stream cancelled, cleaning up');
252 | this.isStreamClosed = true;
253 | if (this.chatUpdateListener) {
254 | console.log('🟡 [Stream] Removing chat update listener');
255 | this.chatUpdateListener.remove();
256 | }
257 | if (this.chatCompleteListener) {
258 | console.log('🟡 [Stream] Removing chat complete listener');
259 | this.chatCompleteListener.remove();
260 | }
261 | if (this.chatErrorListener) {
262 | console.log('🟡 [Stream] Removing chat error listener');
263 | this.chatErrorListener.remove();
264 | }
265 | },
266 | pull: (_controller) => {
267 | console.log('🔵 [Stream] Pull called');
268 | },
269 | });
270 |
271 | return {
272 | stream,
273 | rawCall: { rawPrompt: options.prompt, rawSettings: this.options },
274 | };
275 | };
276 |
277 | // Add other methods here as needed
278 | }
279 |
280 | type ModelOptions = {};
281 |
282 | export function getModel(modelId: string, options: ModelOptions = {}): AiModel {
283 | return new AiModel(modelId, options);
284 | }
285 |
286 | export async function getModels(): Promise {
287 | return Ai.getModels();
288 | }
289 |
290 | export async function downloadModel(
291 | modelId: string,
292 | callbacks?: {
293 | onStart?: () => void;
294 | onProgress?: (progress: DownloadProgress) => void;
295 | onComplete?: () => void;
296 | onError?: (error: Error) => void;
297 | }
298 | ): Promise {
299 | const eventEmitter = new NativeEventEmitter(NativeModules.Ai);
300 |
301 | const downloadStartListener = eventEmitter.addListener(
302 | 'onDownloadStart',
303 | () => {
304 | console.log('🔵 [Download] Started downloading model:', modelId);
305 | callbacks?.onStart?.();
306 | }
307 | );
308 |
309 | const downloadProgressListener = eventEmitter.addListener(
310 | 'onDownloadProgress',
311 | (progress: DownloadProgress) => {
312 | console.log(
313 | '🟢 [Download] Progress:',
314 | progress.percentage.toFixed(2) + '%'
315 | );
316 | callbacks?.onProgress?.(progress);
317 | }
318 | );
319 |
320 | const downloadCompleteListener = eventEmitter.addListener(
321 | 'onDownloadComplete',
322 | () => {
323 | console.log('✅ [Download] Completed downloading model:', modelId);
324 | callbacks?.onComplete?.();
325 | // Cleanup listeners
326 | downloadStartListener.remove();
327 | downloadProgressListener.remove();
328 | downloadCompleteListener.remove();
329 | downloadErrorListener.remove();
330 | }
331 | );
332 |
333 | const downloadErrorListener = eventEmitter.addListener(
334 | 'onDownloadError',
335 | (error) => {
336 | console.error('🔴 [Download] Error downloading model:', error);
337 | callbacks?.onError?.(
338 | new Error(error.message || 'Unknown download error')
339 | );
340 | // Cleanup listeners
341 | downloadStartListener.remove();
342 | downloadProgressListener.remove();
343 | downloadCompleteListener.remove();
344 | downloadErrorListener.remove();
345 | }
346 | );
347 |
348 | try {
349 | await Ai.downloadModel(modelId);
350 | } catch (error) {
351 | // Cleanup listeners in case of error
352 | downloadStartListener.remove();
353 | downloadProgressListener.remove();
354 | downloadCompleteListener.remove();
355 | downloadErrorListener.remove();
356 | throw error;
357 | }
358 | }
359 |
360 | export async function prepareModel(modelId: string) {
361 | return Ai.prepareModel(modelId);
362 | }
363 |
364 | const { doGenerate, doStream } = Ai;
365 |
366 | export { doGenerate, doStream };
367 |
--------------------------------------------------------------------------------
/src/polyfills.ts:
--------------------------------------------------------------------------------
1 | // @ts-nocheck
2 | import { Platform } from 'react-native';
3 | import structuredClone from '@ungap/structured-clone';
4 | import { polyfillGlobal } from 'react-native/Libraries/Utilities/PolyfillFunctions';
5 | import {
6 | TextEncoderStream,
7 | TextDecoderStream,
8 | } from '@stardazed/streams-text-encoding';
9 |
10 | if (Platform.OS !== 'web') {
11 | const setupPolyfills = async () => {
12 | const webStreamPolyfills = require('web-streams-polyfill/ponyfill/es6');
13 |
14 | if (!('structuredClone' in global)) {
15 | polyfillGlobal('structuredClone', () => structuredClone);
16 | }
17 |
18 | polyfillGlobal('ReadableStream', () => webStreamPolyfills.ReadableStream);
19 | polyfillGlobal('TransformStream', () => webStreamPolyfills.TransformStream);
20 | polyfillGlobal('WritableStream', () => webStreamPolyfills.WritableStream);
21 |
22 | polyfillGlobal('TextDecoderStream', () => TextDecoderStream);
23 | polyfillGlobal('TextEncoderStream', () => TextEncoderStream);
24 | };
25 |
26 | setupPolyfills();
27 | }
28 |
--------------------------------------------------------------------------------
/tsconfig.build.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "./tsconfig",
3 | "exclude": ["example", "3rdparty"]
4 | }
5 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "rootDir": ".",
4 | "paths": {
5 | "react-native-ai": ["./src/index"]
6 | },
7 | "allowUnreachableCode": false,
8 | "allowUnusedLabels": false,
9 | "esModuleInterop": true,
10 | "forceConsistentCasingInFileNames": true,
11 | "jsx": "react-jsx",
12 | "lib": ["esnext"],
13 | "module": "esnext",
14 | "moduleResolution": "node",
15 | "noFallthroughCasesInSwitch": true,
16 | "noImplicitReturns": true,
17 | "noImplicitUseStrict": false,
18 | "noStrictGenericChecks": false,
19 | "noUncheckedIndexedAccess": true,
20 | "resolveJsonModule": true,
21 | "skipLibCheck": true,
22 | "strict": true,
23 | "target": "esnext",
24 | "verbatimModuleSyntax": true
25 | },
26 | "exclude": ["3rdparty/"]
27 | }
28 |
--------------------------------------------------------------------------------
/turbo.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://turbo.build/schema.json",
3 | "tasks": {
4 | "build:android": {
5 | "env": ["ORG_GRADLE_PROJECT_newArchEnabled"],
6 | "inputs": [
7 | "package.json",
8 | "android",
9 | "!android/build",
10 | "src/*.ts",
11 | "src/*.tsx",
12 | "example/package.json",
13 | "example/android",
14 | "!example/android/.gradle",
15 | "!example/android/build",
16 | "!example/android/app/build"
17 | ],
18 | "outputs": []
19 | },
20 | "build:ios": {
21 | "env": ["RCT_NEW_ARCH_ENABLED"],
22 | "inputs": [
23 | "package.json",
24 | "*.podspec",
25 | "ios",
26 | "src/*.ts",
27 | "src/*.tsx",
28 | "example/package.json",
29 | "example/ios",
30 | "!example/ios/build",
31 | "!example/ios/Pods"
32 | ],
33 | "outputs": []
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------