@runanywhere/llamacpp 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/RunAnywhereLlama.podspec +131 -0
- package/android/CMakeLists.txt +105 -0
- package/android/build.gradle +288 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/cpp/cpp-adapter.cpp +14 -0
- package/android/src/main/java/com/margelo/nitro/runanywhere/llama/RunAnywhereLlamaPackage.kt +35 -0
- package/android/src/main/jniLibs/arm64-v8a/librac_backend_llamacpp.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librac_backend_llamacpp_jni.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librunanywhere_llamacpp.so +0 -0
- package/cpp/HybridRunAnywhereLlama.cpp +346 -0
- package/cpp/HybridRunAnywhereLlama.hpp +107 -0
- package/cpp/bridges/LLMBridge.cpp +209 -0
- package/cpp/bridges/LLMBridge.hpp +109 -0
- package/cpp/bridges/StructuredOutputBridge.cpp +151 -0
- package/cpp/bridges/StructuredOutputBridge.hpp +66 -0
- package/cpp/rac_llm_llamacpp.h +34 -0
- package/ios/.testlocal +0 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/Info.plist +44 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/Headers/RABackendLLAMACPP.h +2 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/Info.plist +11 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/Modules/module.modulemap +5 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/RABackendLLAMACPP +0 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/Headers/RABackendLLAMACPP.h +2 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/Info.plist +11 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/Modules/module.modulemap +5 -0
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/RABackendLLAMACPP +0 -0
- package/ios/LlamaCPPBackend.podspec +127 -0
- package/nitro.json +16 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/runanywhere/llama/runanywherellamaOnLoad.kt +35 -0
- package/nitrogen/generated/android/runanywherellama+autolinking.cmake +81 -0
- package/nitrogen/generated/android/runanywherellama+autolinking.gradle +27 -0
- package/nitrogen/generated/android/runanywherellamaOnLoad.cpp +44 -0
- package/nitrogen/generated/android/runanywherellamaOnLoad.hpp +25 -0
- package/nitrogen/generated/ios/RunAnywhereLlama+autolinking.rb +60 -0
- package/nitrogen/generated/ios/RunAnywhereLlama-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/RunAnywhereLlama-Swift-Cxx-Bridge.hpp +27 -0
- package/nitrogen/generated/ios/RunAnywhereLlama-Swift-Cxx-Umbrella.hpp +38 -0
- package/nitrogen/generated/ios/RunAnywhereLlamaAutolinking.mm +35 -0
- package/nitrogen/generated/ios/RunAnywhereLlamaAutolinking.swift +12 -0
- package/nitrogen/generated/shared/c++/HybridRunAnywhereLlamaSpec.cpp +33 -0
- package/nitrogen/generated/shared/c++/HybridRunAnywhereLlamaSpec.hpp +77 -0
- package/package.json +60 -0
- package/react-native.config.js +14 -0
- package/src/LlamaCPP.ts +206 -0
- package/src/LlamaCppProvider.ts +120 -0
- package/src/index.ts +59 -0
- package/src/native/NativeRunAnywhereLlama.ts +58 -0
- package/src/native/index.ts +11 -0
- package/src/specs/RunAnywhereLlama.nitro.ts +160 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
# =============================================================================
|
|
6
|
+
# Version Constants (MUST match Swift Package.swift)
|
|
7
|
+
# =============================================================================
|
|
8
|
+
LLAMACPP_VERSION = "0.1.4"
|
|
9
|
+
|
|
10
|
+
# =============================================================================
|
|
11
|
+
# Binary Source - RABackendLlamaCPP from runanywhere-sdks
|
|
12
|
+
# =============================================================================
|
|
13
|
+
LLAMACPP_GITHUB_ORG = "RunanywhereAI"
|
|
14
|
+
LLAMACPP_REPO = "runanywhere-sdks"
|
|
15
|
+
|
|
16
|
+
# =============================================================================
|
|
17
|
+
# testLocal Toggle
|
|
18
|
+
# Set RA_TEST_LOCAL=1 or create .testlocal file to use local binaries
|
|
19
|
+
# =============================================================================
|
|
20
|
+
LLAMACPP_TEST_LOCAL = ENV['RA_TEST_LOCAL'] == '1' || File.exist?(File.join(__dir__, 'ios', '.testlocal'))
|
|
21
|
+
|
|
22
|
+
Pod::Spec.new do |s|
|
|
23
|
+
s.name = "RunAnywhereLlama"
|
|
24
|
+
s.version = package["version"]
|
|
25
|
+
s.summary = package["description"]
|
|
26
|
+
s.homepage = "https://runanywhere.com"
|
|
27
|
+
s.license = package["license"]
|
|
28
|
+
s.authors = "RunAnywhere AI"
|
|
29
|
+
|
|
30
|
+
s.platforms = { :ios => "15.1" }
|
|
31
|
+
s.source = { :git => "https://github.com/RunanywhereAI/sdks.git", :tag => "#{s.version}" }
|
|
32
|
+
|
|
33
|
+
# =============================================================================
|
|
34
|
+
# Llama Backend - RABackendLlamaCPP
|
|
35
|
+
# Downloads from runanywhere-sdks (NOT runanywhere-sdks)
|
|
36
|
+
# =============================================================================
|
|
37
|
+
if LLAMACPP_TEST_LOCAL
|
|
38
|
+
puts "[RunAnywhereLlama] Using LOCAL RABackendLlamaCPP from ios/Frameworks/"
|
|
39
|
+
s.vendored_frameworks = "ios/Frameworks/RABackendLLAMACPP.xcframework"
|
|
40
|
+
else
|
|
41
|
+
s.prepare_command = <<-CMD
|
|
42
|
+
set -e
|
|
43
|
+
|
|
44
|
+
FRAMEWORK_DIR="ios/Frameworks"
|
|
45
|
+
VERSION="#{LLAMACPP_VERSION}"
|
|
46
|
+
VERSION_FILE="$FRAMEWORK_DIR/.llamacpp_version"
|
|
47
|
+
|
|
48
|
+
# Check if already downloaded with correct version
|
|
49
|
+
if [ -f "$VERSION_FILE" ] && [ -d "$FRAMEWORK_DIR/RABackendLLAMACPP.xcframework" ]; then
|
|
50
|
+
CURRENT_VERSION=$(cat "$VERSION_FILE")
|
|
51
|
+
if [ "$CURRENT_VERSION" = "$VERSION" ]; then
|
|
52
|
+
echo "ā
RABackendLLAMACPP.xcframework version $VERSION already downloaded"
|
|
53
|
+
exit 0
|
|
54
|
+
fi
|
|
55
|
+
fi
|
|
56
|
+
|
|
57
|
+
echo "š¦ Downloading RABackendLlamaCPP.xcframework version $VERSION..."
|
|
58
|
+
|
|
59
|
+
mkdir -p "$FRAMEWORK_DIR"
|
|
60
|
+
rm -rf "$FRAMEWORK_DIR/RABackendLLAMACPP.xcframework"
|
|
61
|
+
|
|
62
|
+
# Download from runanywhere-sdks
|
|
63
|
+
DOWNLOAD_URL="https://github.com/#{LLAMACPP_GITHUB_ORG}/#{LLAMACPP_REPO}/releases/download/core-v$VERSION/RABackendLlamaCPP-ios-v$VERSION.zip"
|
|
64
|
+
ZIP_FILE="/tmp/RABackendLlamaCPP.zip"
|
|
65
|
+
|
|
66
|
+
echo " URL: $DOWNLOAD_URL"
|
|
67
|
+
|
|
68
|
+
curl -L -f -o "$ZIP_FILE" "$DOWNLOAD_URL" || {
|
|
69
|
+
echo "ā Failed to download RABackendLlamaCPP from $DOWNLOAD_URL"
|
|
70
|
+
exit 1
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
echo "š Extracting RABackendLLAMACPP.xcframework..."
|
|
74
|
+
unzip -q -o "$ZIP_FILE" -d "$FRAMEWORK_DIR/"
|
|
75
|
+
rm -f "$ZIP_FILE"
|
|
76
|
+
|
|
77
|
+
echo "$VERSION" > "$VERSION_FILE"
|
|
78
|
+
|
|
79
|
+
if [ -d "$FRAMEWORK_DIR/RABackendLLAMACPP.xcframework" ]; then
|
|
80
|
+
echo "ā
RABackendLLAMACPP.xcframework installed successfully"
|
|
81
|
+
else
|
|
82
|
+
echo "ā RABackendLLAMACPP.xcframework extraction failed"
|
|
83
|
+
exit 1
|
|
84
|
+
fi
|
|
85
|
+
CMD
|
|
86
|
+
|
|
87
|
+
s.vendored_frameworks = "ios/Frameworks/RABackendLLAMACPP.xcframework"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Source files - Llama C++ implementation
|
|
91
|
+
s.source_files = [
|
|
92
|
+
"cpp/HybridRunAnywhereLlama.cpp",
|
|
93
|
+
"cpp/HybridRunAnywhereLlama.hpp",
|
|
94
|
+
"cpp/bridges/**/*.{cpp,hpp}",
|
|
95
|
+
]
|
|
96
|
+
|
|
97
|
+
# Build settings
|
|
98
|
+
s.pod_target_xcconfig = {
|
|
99
|
+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17",
|
|
100
|
+
"HEADER_SEARCH_PATHS" => [
|
|
101
|
+
"$(PODS_TARGET_SRCROOT)/cpp",
|
|
102
|
+
"$(PODS_TARGET_SRCROOT)/cpp/bridges",
|
|
103
|
+
"$(PODS_TARGET_SRCROOT)/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/Headers",
|
|
104
|
+
"$(PODS_TARGET_SRCROOT)/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/Headers",
|
|
105
|
+
# RACommons headers from sibling core package (for rac_logger.h)
|
|
106
|
+
"$(PODS_TARGET_SRCROOT)/../core/ios/Frameworks/RACommons.xcframework/ios-arm64/RACommons.framework/Headers",
|
|
107
|
+
"$(PODS_TARGET_SRCROOT)/../core/ios/Frameworks/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers",
|
|
108
|
+
"$(PODS_TARGET_SRCROOT)/../core/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers",
|
|
109
|
+
"$(PODS_TARGET_SRCROOT)/../core/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers",
|
|
110
|
+
"$(PODS_ROOT)/Headers/Public",
|
|
111
|
+
].join(" "),
|
|
112
|
+
"GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) HAS_LLAMACPP=1",
|
|
113
|
+
"DEFINES_MODULE" => "YES",
|
|
114
|
+
"SWIFT_OBJC_INTEROP_MODE" => "objcxx",
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
# Required system libraries
|
|
118
|
+
s.libraries = "c++"
|
|
119
|
+
s.frameworks = "Accelerate", "Foundation", "CoreML"
|
|
120
|
+
|
|
121
|
+
# Dependencies
|
|
122
|
+
s.dependency 'RunAnywhereCore'
|
|
123
|
+
s.dependency 'React-jsi'
|
|
124
|
+
s.dependency 'React-callinvoker'
|
|
125
|
+
|
|
126
|
+
# Load Nitrogen-generated autolinking
|
|
127
|
+
load 'nitrogen/generated/ios/RunAnywhereLlama+autolinking.rb'
|
|
128
|
+
add_nitrogen_files(s)
|
|
129
|
+
|
|
130
|
+
install_modules_dependencies(s)
|
|
131
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
project(runanywherellama)
|
|
2
|
+
cmake_minimum_required(VERSION 3.9.0)
|
|
3
|
+
|
|
4
|
+
set(PACKAGE_NAME runanywherellama)
|
|
5
|
+
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
6
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
7
|
+
|
|
8
|
+
# Path to pre-built native libraries (downloaded from runanywhere-binaries)
|
|
9
|
+
set(JNILIB_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
|
|
10
|
+
|
|
11
|
+
# =============================================================================
|
|
12
|
+
# RABackendLlamaCPP - Llama LLM backend (REQUIRED)
|
|
13
|
+
# Downloaded via Gradle downloadNativeLibs task
|
|
14
|
+
# =============================================================================
|
|
15
|
+
if(NOT EXISTS "${JNILIB_DIR}/librunanywhere_llamacpp.so")
|
|
16
|
+
message(FATAL_ERROR "[RunAnywhereLlama] RABackendLlamaCPP not found at ${JNILIB_DIR}/librunanywhere_llamacpp.so\n"
|
|
17
|
+
"Run: ./gradlew :runanywhere_llamacpp:downloadNativeLibs")
|
|
18
|
+
endif()
|
|
19
|
+
|
|
20
|
+
add_library(runanywhere_llamacpp SHARED IMPORTED)
|
|
21
|
+
set_target_properties(runanywhere_llamacpp PROPERTIES
|
|
22
|
+
IMPORTED_LOCATION "${JNILIB_DIR}/librunanywhere_llamacpp.so"
|
|
23
|
+
IMPORTED_NO_SONAME TRUE
|
|
24
|
+
)
|
|
25
|
+
message(STATUS "[RunAnywhereLlama] Found RABackendLlamaCPP at ${JNILIB_DIR}/librunanywhere_llamacpp.so")
|
|
26
|
+
|
|
27
|
+
# =============================================================================
|
|
28
|
+
# Source files - Llama bridges
|
|
29
|
+
# =============================================================================
|
|
30
|
+
file(GLOB BRIDGE_SOURCES "../cpp/bridges/*.cpp")
|
|
31
|
+
|
|
32
|
+
add_library(${PACKAGE_NAME} SHARED
|
|
33
|
+
src/main/cpp/cpp-adapter.cpp
|
|
34
|
+
../cpp/HybridRunAnywhereLlama.cpp
|
|
35
|
+
${BRIDGE_SOURCES}
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# Add Nitrogen specs (this handles all React Native linking)
|
|
39
|
+
include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/runanywherellama+autolinking.cmake)
|
|
40
|
+
|
|
41
|
+
# =============================================================================
|
|
42
|
+
# Include directories
|
|
43
|
+
# =============================================================================
|
|
44
|
+
# Get core package include dir (for rac/*.h headers)
|
|
45
|
+
get_filename_component(RN_NODE_MODULES "${CMAKE_SOURCE_DIR}/../../.." ABSOLUTE)
|
|
46
|
+
set(CORE_INCLUDE_DIR "${RN_NODE_MODULES}/@runanywhere/core/android/src/main/include")
|
|
47
|
+
set(CORE_JNILIB_DIR "${RN_NODE_MODULES}/@runanywhere/core/android/src/main/jniLibs/${ANDROID_ABI}")
|
|
48
|
+
|
|
49
|
+
include_directories(
|
|
50
|
+
"src/main/cpp"
|
|
51
|
+
"../cpp"
|
|
52
|
+
"../cpp/bridges"
|
|
53
|
+
"${CMAKE_SOURCE_DIR}/include"
|
|
54
|
+
# RAC API headers from core package (flat access for all subdirectories)
|
|
55
|
+
"${CORE_INCLUDE_DIR}"
|
|
56
|
+
"${CORE_INCLUDE_DIR}/rac"
|
|
57
|
+
"${CORE_INCLUDE_DIR}/rac/core"
|
|
58
|
+
"${CORE_INCLUDE_DIR}/rac/core/capabilities"
|
|
59
|
+
"${CORE_INCLUDE_DIR}/rac/features"
|
|
60
|
+
"${CORE_INCLUDE_DIR}/rac/features/llm"
|
|
61
|
+
"${CORE_INCLUDE_DIR}/rac/features/stt"
|
|
62
|
+
"${CORE_INCLUDE_DIR}/rac/features/tts"
|
|
63
|
+
"${CORE_INCLUDE_DIR}/rac/features/vad"
|
|
64
|
+
"${CORE_INCLUDE_DIR}/rac/features/voice_agent"
|
|
65
|
+
"${CORE_INCLUDE_DIR}/rac/features/platform"
|
|
66
|
+
"${CORE_INCLUDE_DIR}/rac/infrastructure"
|
|
67
|
+
"${CORE_INCLUDE_DIR}/rac/infrastructure/device"
|
|
68
|
+
"${CORE_INCLUDE_DIR}/rac/infrastructure/download"
|
|
69
|
+
"${CORE_INCLUDE_DIR}/rac/infrastructure/events"
|
|
70
|
+
"${CORE_INCLUDE_DIR}/rac/infrastructure/model_management"
|
|
71
|
+
"${CORE_INCLUDE_DIR}/rac/infrastructure/network"
|
|
72
|
+
"${CORE_INCLUDE_DIR}/rac/infrastructure/storage"
|
|
73
|
+
"${CORE_INCLUDE_DIR}/rac/infrastructure/telemetry"
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
# =============================================================================
|
|
77
|
+
# RACommons - Core SDK functionality (from core package)
|
|
78
|
+
# =============================================================================
|
|
79
|
+
if(NOT EXISTS "${CORE_JNILIB_DIR}/librac_commons.so")
|
|
80
|
+
message(FATAL_ERROR "[RunAnywhereLlama] RACommons not found at ${CORE_JNILIB_DIR}/librac_commons.so\n"
|
|
81
|
+
"Run: ./gradlew :runanywhere_core:downloadNativeLibs")
|
|
82
|
+
endif()
|
|
83
|
+
|
|
84
|
+
add_library(rac_commons SHARED IMPORTED)
|
|
85
|
+
set_target_properties(rac_commons PROPERTIES
|
|
86
|
+
IMPORTED_LOCATION "${CORE_JNILIB_DIR}/librac_commons.so"
|
|
87
|
+
IMPORTED_NO_SONAME TRUE
|
|
88
|
+
)
|
|
89
|
+
message(STATUS "[RunAnywhereLlama] Found RACommons at ${CORE_JNILIB_DIR}/librac_commons.so")
|
|
90
|
+
|
|
91
|
+
# =============================================================================
|
|
92
|
+
# Linking - LlamaCPP backend and RACommons are REQUIRED
|
|
93
|
+
# =============================================================================
|
|
94
|
+
find_library(LOG_LIB log)
|
|
95
|
+
|
|
96
|
+
target_link_libraries(
|
|
97
|
+
${PACKAGE_NAME}
|
|
98
|
+
${LOG_LIB}
|
|
99
|
+
android
|
|
100
|
+
rac_commons
|
|
101
|
+
runanywhere_llamacpp
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# HAS_LLAMACPP and HAS_RACOMMONS are always defined since backends are required
|
|
105
|
+
target_compile_definitions(${PACKAGE_NAME} PRIVATE HAS_LLAMACPP=1 HAS_RACOMMONS=1)
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
def getExtOrDefault(name) {
|
|
2
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['RunAnywhereLlama_' + name]
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
// Only arm64-v8a is supported
|
|
6
|
+
def reactNativeArchitectures() {
|
|
7
|
+
return ["arm64-v8a"]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
apply plugin: 'com.android.library'
|
|
11
|
+
apply plugin: 'kotlin-android'
|
|
12
|
+
apply from: '../nitrogen/generated/android/runanywherellama+autolinking.gradle'
|
|
13
|
+
apply plugin: 'com.facebook.react'
|
|
14
|
+
|
|
15
|
+
def getExtOrIntegerDefault(name) {
|
|
16
|
+
if (rootProject.ext.has(name)) {
|
|
17
|
+
return rootProject.ext.get(name)
|
|
18
|
+
} else if (project.properties.containsKey('RunAnywhereLlama_' + name)) {
|
|
19
|
+
return (project.properties['RunAnywhereLlama_' + name]).toInteger()
|
|
20
|
+
}
|
|
21
|
+
def defaults = [
|
|
22
|
+
'compileSdkVersion': 36,
|
|
23
|
+
'minSdkVersion': 24,
|
|
24
|
+
'targetSdkVersion': 36
|
|
25
|
+
]
|
|
26
|
+
return defaults[name] ?: 36
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// =============================================================================
|
|
30
|
+
// Version Constants (MUST match Swift Package.swift and iOS Podspec)
|
|
31
|
+
// RABackendLlamaCPP from runanywhere-sdks
|
|
32
|
+
// =============================================================================
|
|
33
|
+
def coreVersion = "0.1.4"
|
|
34
|
+
|
|
35
|
+
// =============================================================================
|
|
36
|
+
// Binary Source - RABackendLlamaCPP from runanywhere-sdks
|
|
37
|
+
// =============================================================================
|
|
38
|
+
def githubOrg = "RunanywhereAI"
|
|
39
|
+
def coreRepo = "runanywhere-sdks"
|
|
40
|
+
|
|
41
|
+
// =============================================================================
|
|
42
|
+
// testLocal Toggle
|
|
43
|
+
// =============================================================================
|
|
44
|
+
def useLocalBuild = project.findProperty("runanywhere.testLocal")?.toBoolean() ?:
|
|
45
|
+
System.getenv("RA_TEST_LOCAL") == "1" ?: false
|
|
46
|
+
|
|
47
|
+
// Native libraries directory
|
|
48
|
+
def jniLibsDir = file("src/main/jniLibs")
|
|
49
|
+
def downloadedLibsDir = file("build/downloaded-libs")
|
|
50
|
+
|
|
51
|
+
android {
|
|
52
|
+
namespace "com.margelo.nitro.runanywhere.llama"
|
|
53
|
+
|
|
54
|
+
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
|
|
55
|
+
|
|
56
|
+
defaultConfig {
|
|
57
|
+
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
|
|
58
|
+
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
|
|
59
|
+
|
|
60
|
+
ndk {
|
|
61
|
+
abiFilters 'arm64-v8a'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
externalNativeBuild {
|
|
65
|
+
cmake {
|
|
66
|
+
cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
|
|
67
|
+
arguments "-DANDROID_STL=c++_shared"
|
|
68
|
+
abiFilters 'arm64-v8a'
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
externalNativeBuild {
|
|
74
|
+
cmake {
|
|
75
|
+
path "CMakeLists.txt"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
packagingOptions {
|
|
80
|
+
excludes = [
|
|
81
|
+
"META-INF",
|
|
82
|
+
"META-INF/**"
|
|
83
|
+
]
|
|
84
|
+
pickFirsts = [
|
|
85
|
+
"**/libc++_shared.so",
|
|
86
|
+
"**/libjsi.so",
|
|
87
|
+
"**/libfbjni.so",
|
|
88
|
+
"**/libfolly_runtime.so"
|
|
89
|
+
]
|
|
90
|
+
jniLibs {
|
|
91
|
+
useLegacyPackaging = true
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
buildFeatures {
|
|
96
|
+
buildConfig true
|
|
97
|
+
prefab true
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
buildTypes {
|
|
101
|
+
release {
|
|
102
|
+
minifyEnabled false
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
lint {
|
|
107
|
+
disable 'GradleCompatible'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
compileOptions {
|
|
111
|
+
sourceCompatibility JavaVersion.VERSION_17
|
|
112
|
+
targetCompatibility JavaVersion.VERSION_17
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
kotlinOptions {
|
|
116
|
+
jvmTarget = "17"
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
sourceSets {
|
|
120
|
+
main {
|
|
121
|
+
java.srcDirs += [
|
|
122
|
+
"generated/java",
|
|
123
|
+
"generated/jni"
|
|
124
|
+
]
|
|
125
|
+
jniLibs.srcDirs = [jniLibsDir]
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// =============================================================================
|
|
131
|
+
// Download Native Libraries (RABackendLlamaCPP ONLY)
|
|
132
|
+
// =============================================================================
|
|
133
|
+
|
|
134
|
+
task downloadNativeLibs {
|
|
135
|
+
description = "Downloads RABackendLlamaCPP from GitHub releases"
|
|
136
|
+
group = "build setup"
|
|
137
|
+
|
|
138
|
+
def versionFile = file("${jniLibsDir}/.version")
|
|
139
|
+
def expectedVersion = coreVersion
|
|
140
|
+
|
|
141
|
+
outputs.dir(jniLibsDir)
|
|
142
|
+
outputs.upToDateWhen {
|
|
143
|
+
versionFile.exists() && versionFile.text.trim() == expectedVersion
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
doLast {
|
|
147
|
+
if (useLocalBuild) {
|
|
148
|
+
logger.lifecycle("[RunAnywhereLlama] Skipping download - using local build mode")
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
def currentVersion = versionFile.exists() ? versionFile.text.trim() : ""
|
|
153
|
+
if (currentVersion == expectedVersion) {
|
|
154
|
+
logger.lifecycle("[RunAnywhereLlama] RABackendLlamaCPP version $expectedVersion already downloaded")
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
logger.lifecycle("[RunAnywhereLlama] Downloading RABackendLlamaCPP...")
|
|
159
|
+
logger.lifecycle(" Core Version: $coreVersion")
|
|
160
|
+
|
|
161
|
+
downloadedLibsDir.mkdirs()
|
|
162
|
+
jniLibsDir.deleteDir()
|
|
163
|
+
jniLibsDir.mkdirs()
|
|
164
|
+
|
|
165
|
+
// =============================================================================
|
|
166
|
+
// Download RABackendLlamaCPP from runanywhere-sdks
|
|
167
|
+
// =============================================================================
|
|
168
|
+
def llamacppUrl = "https://github.com/${githubOrg}/${coreRepo}/releases/download/core-v${coreVersion}/RABackendLlamaCPP-android-v${coreVersion}.zip"
|
|
169
|
+
def llamacppZip = file("${downloadedLibsDir}/RABackendLlamaCPP.zip")
|
|
170
|
+
|
|
171
|
+
logger.lifecycle("\nš¦ Downloading RABackendLlamaCPP...")
|
|
172
|
+
logger.lifecycle(" URL: $llamacppUrl")
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
new URL(llamacppUrl).withInputStream { input ->
|
|
176
|
+
llamacppZip.withOutputStream { output ->
|
|
177
|
+
output << input
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
logger.lifecycle(" Downloaded: ${llamacppZip.length() / 1024}KB")
|
|
181
|
+
|
|
182
|
+
// Extract and flatten the archive structure
|
|
183
|
+
// Archive structure: RABackendLlamaCPP-android-v0.1.4/llamacpp/arm64-v8a/*.so
|
|
184
|
+
// Target structure: arm64-v8a/*.so
|
|
185
|
+
copy {
|
|
186
|
+
from zipTree(llamacppZip)
|
|
187
|
+
into jniLibsDir
|
|
188
|
+
// IMPORTANT: Exclude libc++_shared.so - React Native provides its own
|
|
189
|
+
// Using a different version causes ABI compatibility issues
|
|
190
|
+
exclude "**/libc++_shared.so"
|
|
191
|
+
eachFile { fileCopyDetails ->
|
|
192
|
+
def pathString = fileCopyDetails.relativePath.pathString
|
|
193
|
+
// Handle RABackendLlamaCPP-android-vX.Y.Z/llamacpp/ABI/*.so structure
|
|
194
|
+
def match = pathString =~ /.*\/(arm64-v8a|armeabi-v7a|x86|x86_64)\/(.+\.so)$/
|
|
195
|
+
if (match) {
|
|
196
|
+
def abi = match[0][1]
|
|
197
|
+
def filename = match[0][2]
|
|
198
|
+
fileCopyDetails.relativePath = new RelativePath(true, abi, filename)
|
|
199
|
+
} else if (pathString.endsWith(".so")) {
|
|
200
|
+
// Fallback: just use the last two segments (abi/file.so)
|
|
201
|
+
def segments = pathString.split("/")
|
|
202
|
+
if (segments.length >= 2) {
|
|
203
|
+
fileCopyDetails.relativePath = new RelativePath(true, segments[-2], segments[-1])
|
|
204
|
+
}
|
|
205
|
+
} else {
|
|
206
|
+
// Exclude non-so files
|
|
207
|
+
fileCopyDetails.exclude()
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
includeEmptyDirs = false
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
logger.lifecycle(" ā
RABackendLlamaCPP native libraries installed")
|
|
214
|
+
|
|
215
|
+
// Extract header files
|
|
216
|
+
def includeDir = file("include")
|
|
217
|
+
includeDir.deleteDir()
|
|
218
|
+
includeDir.mkdirs()
|
|
219
|
+
|
|
220
|
+
copy {
|
|
221
|
+
from zipTree(llamacppZip)
|
|
222
|
+
into includeDir
|
|
223
|
+
eachFile { fileCopyDetails ->
|
|
224
|
+
def pathString = fileCopyDetails.relativePath.pathString
|
|
225
|
+
// Handle RABackendLlamaCPP-android-vX.Y.Z/include/*.h structure
|
|
226
|
+
if (pathString.contains("/include/") && pathString.endsWith(".h")) {
|
|
227
|
+
def filename = pathString.substring(pathString.lastIndexOf("/") + 1)
|
|
228
|
+
fileCopyDetails.relativePath = new RelativePath(true, filename)
|
|
229
|
+
} else {
|
|
230
|
+
fileCopyDetails.exclude()
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
includeEmptyDirs = false
|
|
234
|
+
}
|
|
235
|
+
logger.lifecycle(" ā
RABackendLlamaCPP headers installed")
|
|
236
|
+
|
|
237
|
+
} catch (Exception e) {
|
|
238
|
+
logger.error("ā Failed to download RABackendLlamaCPP: ${e.message}")
|
|
239
|
+
throw new GradleException("Failed to download RABackendLlamaCPP", e)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// =============================================================================
|
|
243
|
+
// List installed files
|
|
244
|
+
// =============================================================================
|
|
245
|
+
logger.lifecycle("\nš Installed native libraries:")
|
|
246
|
+
jniLibsDir.listFiles()?.findAll { it.isDirectory() }?.each { abiDir ->
|
|
247
|
+
logger.lifecycle(" ${abiDir.name}/")
|
|
248
|
+
abiDir.listFiles()?.findAll { it.name.endsWith(".so") }?.sort()?.each { soFile ->
|
|
249
|
+
logger.lifecycle(" ${soFile.name} (${soFile.length() / 1024}KB)")
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
versionFile.text = expectedVersion
|
|
254
|
+
logger.lifecycle("\nā
RABackendLlamaCPP version $expectedVersion installed")
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (!useLocalBuild) {
|
|
259
|
+
preBuild.dependsOn downloadNativeLibs
|
|
260
|
+
|
|
261
|
+
afterEvaluate {
|
|
262
|
+
tasks.matching {
|
|
263
|
+
it.name.contains("generateCodegen") || it.name.contains("Codegen")
|
|
264
|
+
}.configureEach {
|
|
265
|
+
mustRunAfter downloadNativeLibs
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
task cleanNativeLibs(type: Delete) {
|
|
271
|
+
description = "Removes downloaded native libraries"
|
|
272
|
+
group = "build"
|
|
273
|
+
delete jniLibsDir
|
|
274
|
+
delete downloadedLibsDir
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
clean.dependsOn cleanNativeLibs
|
|
278
|
+
|
|
279
|
+
repositories {
|
|
280
|
+
mavenCentral()
|
|
281
|
+
google()
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
dependencies {
|
|
285
|
+
implementation "com.facebook.react:react-android"
|
|
286
|
+
implementation project(":react-native-nitro-modules")
|
|
287
|
+
implementation project(":runanywhere_core")
|
|
288
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cpp-adapter.cpp
|
|
3
|
+
*
|
|
4
|
+
* Android JNI entry point for RunAnywhereLlama native module.
|
|
5
|
+
* This file is required by React Native's CMake build system.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#include <jni.h>
|
|
9
|
+
#include "runanywherellamaOnLoad.hpp"
|
|
10
|
+
|
|
11
|
+
extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
|
|
12
|
+
// Initialize nitrogen module and register HybridObjects
|
|
13
|
+
return margelo::nitro::runanywhere::llama::initialize(vm);
|
|
14
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package com.margelo.nitro.runanywhere.llama
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
7
|
+
import com.margelo.nitro.runanywhere.SDKLogger
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* React Native package for RunAnywhere LlamaCPP backend.
|
|
11
|
+
* This class is required for React Native autolinking.
|
|
12
|
+
*/
|
|
13
|
+
class RunAnywhereLlamaPackage : BaseReactPackage() {
|
|
14
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
15
|
+
return null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
19
|
+
return ReactModuleInfoProvider { HashMap() }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
companion object {
|
|
23
|
+
private val log = SDKLogger("LLM.LlamaCpp")
|
|
24
|
+
|
|
25
|
+
init {
|
|
26
|
+
// Load the native library which registers the HybridObject factory
|
|
27
|
+
try {
|
|
28
|
+
System.loadLibrary("runanywherellama")
|
|
29
|
+
} catch (e: UnsatisfiedLinkError) {
|
|
30
|
+
// Native library may already be loaded or bundled differently
|
|
31
|
+
log.error("Failed to load runanywherellama: ${e.message}")
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
Binary file
|
|
Binary file
|