@runanywhere/llamacpp 0.18.0 → 0.19.13
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/README.md +85 -413
- package/RunAnywhereLlama.podspec +27 -13
- package/android/CMakeLists.txt +37 -20
- package/android/build.gradle +83 -8
- package/cpp/HybridRunAnywhereLlama.cpp +26 -269
- package/cpp/HybridRunAnywhereLlama.hpp +2 -58
- package/ios/{Frameworks → Binaries}/RABackendLLAMACPP.xcframework/Info.plist +8 -9
- package/ios/{Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/RABackendLLAMACPP → Binaries/RABackendLLAMACPP.xcframework/ios-arm64/librac_backend_llamacpp.a} +0 -0
- package/ios/{Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/RABackendLLAMACPP → Binaries/RABackendLLAMACPP.xcframework/ios-arm64-simulator/librac_backend_llamacpp.a} +0 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/runanywhere/llama/runanywherellamaOnLoad.kt +1 -1
- package/nitrogen/generated/android/runanywherellama+autolinking.cmake +2 -2
- package/nitrogen/generated/android/runanywherellama+autolinking.gradle +1 -1
- package/nitrogen/generated/android/runanywherellamaOnLoad.cpp +23 -18
- package/nitrogen/generated/android/runanywherellamaOnLoad.hpp +14 -5
- package/nitrogen/generated/ios/RunAnywhereLlama+autolinking.rb +4 -2
- package/nitrogen/generated/ios/RunAnywhereLlama-Swift-Cxx-Bridge.cpp +2 -2
- package/nitrogen/generated/ios/RunAnywhereLlama-Swift-Cxx-Bridge.hpp +2 -2
- package/nitrogen/generated/ios/RunAnywhereLlama-Swift-Cxx-Umbrella.hpp +1 -1
- package/nitrogen/generated/ios/RunAnywhereLlamaAutolinking.mm +1 -1
- package/nitrogen/generated/ios/RunAnywhereLlamaAutolinking.swift +6 -2
- package/nitrogen/generated/shared/c++/HybridRunAnywhereLlamaSpec.cpp +1 -11
- package/nitrogen/generated/shared/c++/HybridRunAnywhereLlamaSpec.hpp +2 -15
- package/package.json +13 -10
- package/react-native.config.js +1 -3
- package/src/LlamaCPP.ts +32 -160
- package/src/LlamaCppProvider.ts +32 -36
- package/src/index.ts +18 -23
- package/src/native/NativeRunAnywhereLlama.ts +19 -5
- package/src/specs/RunAnywhereLlama.nitro.ts +17 -126
- package/android/src/main/AndroidManifest.xml +0 -3
- package/android/src/main/cpp/cpp-adapter.cpp +0 -14
- package/android/src/main/include/rac/backends/rac_llm_llamacpp.h +0 -218
- package/android/src/main/include/rac/backends/rac_stt_onnx.h +0 -99
- package/android/src/main/include/rac/backends/rac_stt_whispercpp.h +0 -153
- package/android/src/main/include/rac/backends/rac_tts_onnx.h +0 -71
- package/android/src/main/include/rac/backends/rac_vad_onnx.h +0 -84
- package/android/src/main/include/rac/core/capabilities/rac_lifecycle.h +0 -290
- package/android/src/main/include/rac/core/rac_analytics_events.h +0 -610
- package/android/src/main/include/rac/core/rac_audio_utils.h +0 -88
- package/android/src/main/include/rac/core/rac_component_types.h +0 -160
- package/android/src/main/include/rac/core/rac_core.h +0 -331
- package/android/src/main/include/rac/core/rac_error.h +0 -469
- package/android/src/main/include/rac/core/rac_events.h +0 -334
- package/android/src/main/include/rac/core/rac_logger.h +0 -416
- package/android/src/main/include/rac/core/rac_platform_adapter.h +0 -340
- package/android/src/main/include/rac/core/rac_sdk_state.h +0 -292
- package/android/src/main/include/rac/core/rac_structured_error.h +0 -594
- package/android/src/main/include/rac/core/rac_types.h +0 -264
- package/android/src/main/include/rac/features/llm/rac_llm.h +0 -17
- package/android/src/main/include/rac/features/llm/rac_llm_analytics.h +0 -188
- package/android/src/main/include/rac/features/llm/rac_llm_component.h +0 -228
- package/android/src/main/include/rac/features/llm/rac_llm_events.h +0 -215
- package/android/src/main/include/rac/features/llm/rac_llm_metrics.h +0 -402
- package/android/src/main/include/rac/features/llm/rac_llm_service.h +0 -163
- package/android/src/main/include/rac/features/llm/rac_llm_structured_output.h +0 -141
- package/android/src/main/include/rac/features/llm/rac_llm_types.h +0 -384
- package/android/src/main/include/rac/features/platform/rac_llm_platform.h +0 -204
- package/android/src/main/include/rac/features/platform/rac_tts_platform.h +0 -197
- package/android/src/main/include/rac/features/stt/rac_stt.h +0 -17
- package/android/src/main/include/rac/features/stt/rac_stt_analytics.h +0 -204
- package/android/src/main/include/rac/features/stt/rac_stt_component.h +0 -162
- package/android/src/main/include/rac/features/stt/rac_stt_events.h +0 -62
- package/android/src/main/include/rac/features/stt/rac_stt_service.h +0 -154
- package/android/src/main/include/rac/features/stt/rac_stt_types.h +0 -389
- package/android/src/main/include/rac/features/tts/rac_tts.h +0 -17
- package/android/src/main/include/rac/features/tts/rac_tts_analytics.h +0 -181
- package/android/src/main/include/rac/features/tts/rac_tts_component.h +0 -158
- package/android/src/main/include/rac/features/tts/rac_tts_events.h +0 -54
- package/android/src/main/include/rac/features/tts/rac_tts_service.h +0 -162
- package/android/src/main/include/rac/features/tts/rac_tts_types.h +0 -374
- package/android/src/main/include/rac/features/vad/rac_vad.h +0 -17
- package/android/src/main/include/rac/features/vad/rac_vad_analytics.h +0 -236
- package/android/src/main/include/rac/features/vad/rac_vad_component.h +0 -185
- package/android/src/main/include/rac/features/vad/rac_vad_energy.h +0 -443
- package/android/src/main/include/rac/features/vad/rac_vad_events.h +0 -76
- package/android/src/main/include/rac/features/vad/rac_vad_service.h +0 -167
- package/android/src/main/include/rac/features/vad/rac_vad_types.h +0 -244
- package/android/src/main/include/rac/features/voice_agent/rac_voice_agent.h +0 -612
- package/android/src/main/include/rac/infrastructure/device/rac_device_manager.h +0 -176
- package/android/src/main/include/rac/infrastructure/download/rac_download.h +0 -418
- package/android/src/main/include/rac/infrastructure/events/rac_events.h +0 -177
- package/android/src/main/include/rac/infrastructure/model_management/rac_model_assignment.h +0 -169
- package/android/src/main/include/rac/infrastructure/model_management/rac_model_paths.h +0 -258
- package/android/src/main/include/rac/infrastructure/model_management/rac_model_registry.h +0 -357
- package/android/src/main/include/rac/infrastructure/model_management/rac_model_strategy.h +0 -374
- package/android/src/main/include/rac/infrastructure/model_management/rac_model_types.h +0 -613
- package/android/src/main/include/rac/infrastructure/network/rac_api_types.h +0 -335
- package/android/src/main/include/rac/infrastructure/network/rac_auth_manager.h +0 -252
- package/android/src/main/include/rac/infrastructure/network/rac_dev_config.h +0 -85
- package/android/src/main/include/rac/infrastructure/network/rac_endpoints.h +0 -102
- package/android/src/main/include/rac/infrastructure/network/rac_environment.h +0 -220
- package/android/src/main/include/rac/infrastructure/network/rac_http_client.h +0 -233
- package/android/src/main/include/rac/infrastructure/storage/rac_storage_analyzer.h +0 -286
- package/android/src/main/include/rac/infrastructure/telemetry/rac_telemetry_manager.h +0 -206
- package/android/src/main/include/rac/infrastructure/telemetry/rac_telemetry_types.h +0 -234
- package/android/src/main/java/com/margelo/nitro/runanywhere/llama/RunAnywhereLlamaPackage.kt +0 -35
- package/android/src/main/jniLibs/arm64-v8a/libomp.so +0 -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/librac_commons.so +0 -0
- package/android/src/main/jniLibs/x86_64/libomp.so +0 -0
- package/android/src/main/jniLibs/x86_64/librac_backend_llamacpp.so +0 -0
- package/android/src/main/jniLibs/x86_64/librac_backend_llamacpp_jni.so +0 -0
- package/android/src/main/jniLibs/x86_64/librac_commons.so +0 -0
- package/cpp/bridges/LLMBridge.cpp +0 -209
- package/cpp/bridges/LLMBridge.hpp +0 -109
- package/cpp/bridges/StructuredOutputBridge.cpp +0 -151
- package/cpp/bridges/StructuredOutputBridge.hpp +0 -66
- package/cpp/rac_llm_llamacpp.h +0 -34
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/Headers/RABackendLLAMACPP.h +0 -2
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/Info.plist +0 -11
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64/RABackendLLAMACPP.framework/Modules/module.modulemap +0 -5
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/Headers/RABackendLLAMACPP.h +0 -2
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/Info.plist +0 -11
- package/ios/Frameworks/RABackendLLAMACPP.xcframework/ios-arm64_x86_64-simulator/RABackendLLAMACPP.framework/Modules/module.modulemap +0 -5
- package/ios/LlamaCPPBackend.podspec +0 -127
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file rac_telemetry_types.h
|
|
3
|
-
* @brief Telemetry data structures - canonical source of truth
|
|
4
|
-
*
|
|
5
|
-
* All telemetry payloads are defined here. Platform SDKs (Swift, Kotlin, Flutter)
|
|
6
|
-
* use these types directly or create thin wrappers.
|
|
7
|
-
*
|
|
8
|
-
* Mirrors Swift's TelemetryEventPayload.swift structure.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
#ifndef RAC_TELEMETRY_TYPES_H
|
|
12
|
-
#define RAC_TELEMETRY_TYPES_H
|
|
13
|
-
|
|
14
|
-
#include "rac/core/rac_types.h"
|
|
15
|
-
#include "rac/infrastructure/network/rac_environment.h"
|
|
16
|
-
|
|
17
|
-
#ifdef __cplusplus
|
|
18
|
-
extern "C" {
|
|
19
|
-
#endif
|
|
20
|
-
|
|
21
|
-
// =============================================================================
|
|
22
|
-
// TELEMETRY EVENT PAYLOAD
|
|
23
|
-
// =============================================================================
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @brief Complete telemetry event payload
|
|
27
|
-
*
|
|
28
|
-
* Maps to backend SDKTelemetryEvent schema with all fields for:
|
|
29
|
-
* - LLM events (tokens, generation times, etc.)
|
|
30
|
-
* - STT events (audio duration, word count, etc.)
|
|
31
|
-
* - TTS events (character count, audio size, etc.)
|
|
32
|
-
* - VAD events (speech duration)
|
|
33
|
-
* - Model lifecycle events (size, archive type)
|
|
34
|
-
* - SDK lifecycle events (count)
|
|
35
|
-
* - Storage events (freed bytes)
|
|
36
|
-
* - Network events (online status)
|
|
37
|
-
*/
|
|
38
|
-
typedef struct rac_telemetry_payload {
|
|
39
|
-
// Required fields
|
|
40
|
-
const char* id; // Unique event ID (UUID)
|
|
41
|
-
const char* event_type; // Event type string
|
|
42
|
-
int64_t timestamp_ms; // Unix timestamp in milliseconds
|
|
43
|
-
int64_t created_at_ms; // When payload was created
|
|
44
|
-
|
|
45
|
-
// Event classification
|
|
46
|
-
const char* modality; // "llm", "stt", "tts", "model", "system"
|
|
47
|
-
|
|
48
|
-
// Device identification
|
|
49
|
-
const char* device_id; // Persistent device UUID
|
|
50
|
-
const char* session_id; // Optional session ID
|
|
51
|
-
|
|
52
|
-
// Model info
|
|
53
|
-
const char* model_id;
|
|
54
|
-
const char* model_name;
|
|
55
|
-
const char* framework; // "llamacpp", "onnx", "mlx", etc.
|
|
56
|
-
|
|
57
|
-
// Device info
|
|
58
|
-
const char* device; // Device model (e.g., "iPhone15,2")
|
|
59
|
-
const char* os_version; // OS version (e.g., "17.0")
|
|
60
|
-
const char* platform; // "ios", "android", "flutter"
|
|
61
|
-
const char* sdk_version; // SDK version string
|
|
62
|
-
|
|
63
|
-
// Common performance metrics
|
|
64
|
-
double processing_time_ms;
|
|
65
|
-
rac_bool_t success;
|
|
66
|
-
rac_bool_t has_success; // Whether success field is set
|
|
67
|
-
const char* error_message;
|
|
68
|
-
const char* error_code;
|
|
69
|
-
|
|
70
|
-
// LLM-specific fields
|
|
71
|
-
int32_t input_tokens;
|
|
72
|
-
int32_t output_tokens;
|
|
73
|
-
int32_t total_tokens;
|
|
74
|
-
double tokens_per_second;
|
|
75
|
-
double time_to_first_token_ms;
|
|
76
|
-
double prompt_eval_time_ms;
|
|
77
|
-
double generation_time_ms;
|
|
78
|
-
int32_t context_length;
|
|
79
|
-
double temperature;
|
|
80
|
-
int32_t max_tokens;
|
|
81
|
-
|
|
82
|
-
// STT-specific fields
|
|
83
|
-
double audio_duration_ms;
|
|
84
|
-
double real_time_factor;
|
|
85
|
-
int32_t word_count;
|
|
86
|
-
double confidence;
|
|
87
|
-
const char* language;
|
|
88
|
-
rac_bool_t is_streaming;
|
|
89
|
-
rac_bool_t has_is_streaming;
|
|
90
|
-
int32_t segment_index;
|
|
91
|
-
|
|
92
|
-
// TTS-specific fields
|
|
93
|
-
int32_t character_count;
|
|
94
|
-
double characters_per_second;
|
|
95
|
-
int32_t audio_size_bytes;
|
|
96
|
-
int32_t sample_rate;
|
|
97
|
-
const char* voice;
|
|
98
|
-
double output_duration_ms;
|
|
99
|
-
|
|
100
|
-
// Model lifecycle fields
|
|
101
|
-
int64_t model_size_bytes;
|
|
102
|
-
const char* archive_type;
|
|
103
|
-
|
|
104
|
-
// VAD fields
|
|
105
|
-
double speech_duration_ms;
|
|
106
|
-
|
|
107
|
-
// SDK lifecycle fields
|
|
108
|
-
int32_t count;
|
|
109
|
-
|
|
110
|
-
// Storage fields
|
|
111
|
-
int64_t freed_bytes;
|
|
112
|
-
|
|
113
|
-
// Network fields
|
|
114
|
-
rac_bool_t is_online;
|
|
115
|
-
rac_bool_t has_is_online;
|
|
116
|
-
} rac_telemetry_payload_t;
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* @brief Default/empty telemetry payload
|
|
120
|
-
*/
|
|
121
|
-
RAC_API rac_telemetry_payload_t rac_telemetry_payload_default(void);
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* @brief Free any allocated strings in a telemetry payload
|
|
125
|
-
*/
|
|
126
|
-
RAC_API void rac_telemetry_payload_free(rac_telemetry_payload_t* payload);
|
|
127
|
-
|
|
128
|
-
// =============================================================================
|
|
129
|
-
// TELEMETRY BATCH REQUEST
|
|
130
|
-
// =============================================================================
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* @brief Batch telemetry request for API
|
|
134
|
-
*
|
|
135
|
-
* Supports both V1 and V2 storage paths:
|
|
136
|
-
* - V1 (legacy): modality = NULL → stores in sdk_telemetry_events table
|
|
137
|
-
* - V2 (normalized): modality = "llm"/"stt"/"tts"/"model" → normalized tables
|
|
138
|
-
*/
|
|
139
|
-
typedef struct rac_telemetry_batch_request {
|
|
140
|
-
rac_telemetry_payload_t* events;
|
|
141
|
-
size_t events_count;
|
|
142
|
-
const char* device_id;
|
|
143
|
-
int64_t timestamp_ms;
|
|
144
|
-
const char* modality; // NULL for V1, "llm"/"stt"/"tts"/"model" for V2
|
|
145
|
-
} rac_telemetry_batch_request_t;
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* @brief Batch telemetry response from API
|
|
149
|
-
*/
|
|
150
|
-
typedef struct rac_telemetry_batch_response {
|
|
151
|
-
rac_bool_t success;
|
|
152
|
-
int32_t events_received;
|
|
153
|
-
int32_t events_stored;
|
|
154
|
-
int32_t events_skipped; // Duplicates skipped
|
|
155
|
-
const char** errors; // Array of error messages
|
|
156
|
-
size_t errors_count;
|
|
157
|
-
const char* storage_version; // "V1" or "V2"
|
|
158
|
-
} rac_telemetry_batch_response_t;
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* @brief Free batch response
|
|
162
|
-
*/
|
|
163
|
-
RAC_API void rac_telemetry_batch_response_free(rac_telemetry_batch_response_t* response);
|
|
164
|
-
|
|
165
|
-
// =============================================================================
|
|
166
|
-
// DEVICE REGISTRATION TYPES
|
|
167
|
-
// =============================================================================
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* @brief Device information for registration (telemetry-specific)
|
|
171
|
-
*
|
|
172
|
-
* Platform-specific values are passed in from Swift/Kotlin.
|
|
173
|
-
* Matches backend schemas/device.py DeviceInfo schema.
|
|
174
|
-
* Note: Named differently from rac_device_info_t to avoid conflict.
|
|
175
|
-
*/
|
|
176
|
-
typedef struct rac_device_registration_info {
|
|
177
|
-
// Required fields (backend schema)
|
|
178
|
-
const char* device_id; // Persistent UUID from Keychain/secure storage
|
|
179
|
-
const char* device_model; // "iPhone 16 Pro Max", "Pixel 7", etc.
|
|
180
|
-
const char* device_name; // User-assigned device name
|
|
181
|
-
const char* platform; // "ios", "android"
|
|
182
|
-
const char* os_version; // "17.0", "14"
|
|
183
|
-
const char* form_factor; // "phone", "tablet", "laptop", etc.
|
|
184
|
-
const char* architecture; // "arm64", "x86_64", etc.
|
|
185
|
-
const char* chip_name; // "A18 Pro", "Snapdragon 888", etc.
|
|
186
|
-
int64_t total_memory; // Total RAM in bytes
|
|
187
|
-
int64_t available_memory; // Available RAM in bytes
|
|
188
|
-
rac_bool_t has_neural_engine; // true if device has Neural Engine / NPU
|
|
189
|
-
int32_t neural_engine_cores; // Number of Neural Engine cores (0 if none)
|
|
190
|
-
const char* gpu_family; // "apple", "adreno", etc.
|
|
191
|
-
double battery_level; // 0.0-1.0, negative if unavailable
|
|
192
|
-
const char* battery_state; // "charging", "full", "unplugged", NULL if unavailable
|
|
193
|
-
rac_bool_t is_low_power_mode; // Low power mode enabled
|
|
194
|
-
int32_t core_count; // Total CPU cores
|
|
195
|
-
int32_t performance_cores; // Performance (P) cores
|
|
196
|
-
int32_t efficiency_cores; // Efficiency (E) cores
|
|
197
|
-
const char* device_fingerprint; // Unique device fingerprint (may be same as device_id)
|
|
198
|
-
|
|
199
|
-
// Legacy fields (for backward compatibility)
|
|
200
|
-
const char* device_type; // "smartphone", "tablet", etc. (deprecated - use form_factor)
|
|
201
|
-
const char* os_name; // "iOS", "Android" (deprecated - use platform)
|
|
202
|
-
int64_t total_disk_bytes;
|
|
203
|
-
int64_t available_disk_bytes;
|
|
204
|
-
const char* processor_info;
|
|
205
|
-
int32_t processor_count; // Deprecated - use core_count
|
|
206
|
-
rac_bool_t is_simulator;
|
|
207
|
-
const char* locale;
|
|
208
|
-
const char* timezone;
|
|
209
|
-
} rac_device_registration_info_t;
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* @brief Device registration request
|
|
213
|
-
*/
|
|
214
|
-
typedef struct rac_device_registration_request {
|
|
215
|
-
rac_device_registration_info_t device_info;
|
|
216
|
-
const char* sdk_version;
|
|
217
|
-
const char* build_token; // For development mode
|
|
218
|
-
int64_t last_seen_at_ms;
|
|
219
|
-
} rac_device_registration_request_t;
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* @brief Device registration response
|
|
223
|
-
*/
|
|
224
|
-
typedef struct rac_device_registration_response {
|
|
225
|
-
const char* device_id;
|
|
226
|
-
const char* status; // "registered" or "updated"
|
|
227
|
-
const char* sync_status; // "synced" or "pending"
|
|
228
|
-
} rac_device_registration_response_t;
|
|
229
|
-
|
|
230
|
-
#ifdef __cplusplus
|
|
231
|
-
}
|
|
232
|
-
#endif
|
|
233
|
-
|
|
234
|
-
#endif // RAC_TELEMETRY_TYPES_H
|
package/android/src/main/java/com/margelo/nitro/runanywhere/llama/RunAnywhereLlamaPackage.kt
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
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
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file LLMBridge.cpp
|
|
3
|
-
* @brief LLM capability bridge implementation
|
|
4
|
-
*
|
|
5
|
-
* NOTE: RACommons and LlamaCPP backend are REQUIRED and always linked via the build system.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
#include "LLMBridge.hpp"
|
|
9
|
-
#include <stdexcept>
|
|
10
|
-
|
|
11
|
-
namespace runanywhere {
|
|
12
|
-
namespace bridges {
|
|
13
|
-
|
|
14
|
-
LLMBridge& LLMBridge::shared() {
|
|
15
|
-
static LLMBridge instance;
|
|
16
|
-
return instance;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
LLMBridge::LLMBridge() = default;
|
|
20
|
-
|
|
21
|
-
LLMBridge::~LLMBridge() {
|
|
22
|
-
destroy();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
bool LLMBridge::isLoaded() const {
|
|
26
|
-
if (handle_) {
|
|
27
|
-
return rac_llm_component_is_loaded(handle_) == RAC_TRUE;
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
std::string LLMBridge::currentModelId() const {
|
|
33
|
-
return loadedModelId_;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
rac_result_t LLMBridge::loadModel(const std::string& modelPath,
|
|
37
|
-
const std::string& modelId,
|
|
38
|
-
const std::string& modelName) {
|
|
39
|
-
// Create component if needed
|
|
40
|
-
if (!handle_) {
|
|
41
|
-
rac_result_t result = rac_llm_component_create(&handle_);
|
|
42
|
-
if (result != RAC_SUCCESS) {
|
|
43
|
-
throw std::runtime_error("LLMBridge: Failed to create LLM component. Error: " + std::to_string(result));
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Use modelPath as modelId if not provided
|
|
48
|
-
std::string effectiveModelId = modelId.empty() ? modelPath : modelId;
|
|
49
|
-
std::string effectiveModelName = modelName.empty() ? effectiveModelId : modelName;
|
|
50
|
-
|
|
51
|
-
// Unload existing model if different
|
|
52
|
-
if (isLoaded() && loadedModelId_ != effectiveModelId) {
|
|
53
|
-
rac_llm_component_unload(handle_);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Load new model with correct 4-arg signature
|
|
57
|
-
// rac_llm_component_load_model(handle, model_path, model_id, model_name)
|
|
58
|
-
rac_result_t result = rac_llm_component_load_model(
|
|
59
|
-
handle_,
|
|
60
|
-
modelPath.c_str(),
|
|
61
|
-
effectiveModelId.c_str(),
|
|
62
|
-
effectiveModelName.c_str()
|
|
63
|
-
);
|
|
64
|
-
if (result == RAC_SUCCESS) {
|
|
65
|
-
loadedModelId_ = effectiveModelId;
|
|
66
|
-
} else {
|
|
67
|
-
throw std::runtime_error("LLMBridge: Failed to load LLM model '" + effectiveModelId + "'. Error: " + std::to_string(result));
|
|
68
|
-
}
|
|
69
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
rac_result_t LLMBridge::unload() {
|
|
73
|
-
if (handle_) {
|
|
74
|
-
rac_result_t result = rac_llm_component_unload(handle_);
|
|
75
|
-
if (result == RAC_SUCCESS) {
|
|
76
|
-
loadedModelId_.clear();
|
|
77
|
-
} else {
|
|
78
|
-
throw std::runtime_error("LLMBridge: Failed to unload LLM model. Error: " + std::to_string(result));
|
|
79
|
-
}
|
|
80
|
-
return result;
|
|
81
|
-
}
|
|
82
|
-
loadedModelId_.clear();
|
|
83
|
-
return RAC_SUCCESS;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
void LLMBridge::cleanup() {
|
|
87
|
-
if (handle_) {
|
|
88
|
-
rac_llm_component_cleanup(handle_);
|
|
89
|
-
}
|
|
90
|
-
loadedModelId_.clear();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
void LLMBridge::cancel() {
|
|
94
|
-
cancellationRequested_ = true;
|
|
95
|
-
if (handle_) {
|
|
96
|
-
rac_llm_component_cancel(handle_);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
void LLMBridge::destroy() {
|
|
101
|
-
if (handle_) {
|
|
102
|
-
rac_llm_component_destroy(handle_);
|
|
103
|
-
handle_ = nullptr;
|
|
104
|
-
}
|
|
105
|
-
loadedModelId_.clear();
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
LLMResult LLMBridge::generate(const std::string& prompt, const LLMOptions& options) {
|
|
109
|
-
LLMResult result;
|
|
110
|
-
cancellationRequested_ = false;
|
|
111
|
-
|
|
112
|
-
if (!handle_ || !isLoaded()) {
|
|
113
|
-
throw std::runtime_error("LLMBridge: LLM model not loaded. Call loadModel() first.");
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
rac_llm_options_t racOptions = {};
|
|
117
|
-
racOptions.max_tokens = options.maxTokens;
|
|
118
|
-
racOptions.temperature = static_cast<float>(options.temperature);
|
|
119
|
-
racOptions.top_p = static_cast<float>(options.topP);
|
|
120
|
-
// NOTE: top_k is not available in rac_llm_options_t, only top_p
|
|
121
|
-
|
|
122
|
-
rac_llm_result_t racResult = {};
|
|
123
|
-
rac_result_t status = rac_llm_component_generate(handle_, prompt.c_str(),
|
|
124
|
-
&racOptions, &racResult);
|
|
125
|
-
|
|
126
|
-
if (status == RAC_SUCCESS) {
|
|
127
|
-
if (racResult.text) {
|
|
128
|
-
result.text = racResult.text;
|
|
129
|
-
}
|
|
130
|
-
result.tokenCount = racResult.completion_tokens;
|
|
131
|
-
result.durationMs = static_cast<double>(racResult.total_time_ms);
|
|
132
|
-
} else {
|
|
133
|
-
throw std::runtime_error("LLMBridge: Text generation failed with error code: " + std::to_string(status));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
result.cancelled = cancellationRequested_;
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
void LLMBridge::generateStream(const std::string& prompt, const LLMOptions& options,
|
|
141
|
-
const LLMStreamCallbacks& callbacks) {
|
|
142
|
-
cancellationRequested_ = false;
|
|
143
|
-
|
|
144
|
-
if (!handle_ || !isLoaded()) {
|
|
145
|
-
if (callbacks.onError) {
|
|
146
|
-
callbacks.onError(-4, "LLM model not loaded. Call loadModel() first.");
|
|
147
|
-
}
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
rac_llm_options_t racOptions = {};
|
|
152
|
-
racOptions.max_tokens = options.maxTokens;
|
|
153
|
-
racOptions.temperature = static_cast<float>(options.temperature);
|
|
154
|
-
racOptions.top_p = static_cast<float>(options.topP);
|
|
155
|
-
// NOTE: top_k is not available in rac_llm_options_t, only top_p
|
|
156
|
-
|
|
157
|
-
// Stream context for callbacks
|
|
158
|
-
struct StreamContext {
|
|
159
|
-
const LLMStreamCallbacks* callbacks;
|
|
160
|
-
bool* cancellationRequested;
|
|
161
|
-
std::string accumulatedText;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
StreamContext ctx = { &callbacks, &cancellationRequested_, "" };
|
|
165
|
-
|
|
166
|
-
auto tokenCallback = [](const char* token, void* user_data) -> rac_bool_t {
|
|
167
|
-
auto* ctx = static_cast<StreamContext*>(user_data);
|
|
168
|
-
if (*ctx->cancellationRequested) {
|
|
169
|
-
return RAC_FALSE;
|
|
170
|
-
}
|
|
171
|
-
if (ctx->callbacks->onToken && token) {
|
|
172
|
-
ctx->accumulatedText += token;
|
|
173
|
-
return ctx->callbacks->onToken(token) ? RAC_TRUE : RAC_FALSE;
|
|
174
|
-
}
|
|
175
|
-
return RAC_TRUE;
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
auto completeCallback = [](const rac_llm_result_t* result, void* user_data) {
|
|
179
|
-
auto* ctx = static_cast<StreamContext*>(user_data);
|
|
180
|
-
if (ctx->callbacks->onComplete) {
|
|
181
|
-
ctx->callbacks->onComplete(
|
|
182
|
-
ctx->accumulatedText,
|
|
183
|
-
result ? result->completion_tokens : 0,
|
|
184
|
-
result ? static_cast<double>(result->total_time_ms) : 0.0
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
auto errorCallback = [](rac_result_t error_code, const char* error_message,
|
|
190
|
-
void* user_data) {
|
|
191
|
-
auto* ctx = static_cast<StreamContext*>(user_data);
|
|
192
|
-
if (ctx->callbacks->onError) {
|
|
193
|
-
ctx->callbacks->onError(error_code, error_message ? error_message : "Unknown error");
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
rac_llm_component_generate_stream(handle_, prompt.c_str(), &racOptions,
|
|
198
|
-
tokenCallback, completeCallback, errorCallback, &ctx);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
rac_lifecycle_state_t LLMBridge::getState() const {
|
|
202
|
-
if (handle_) {
|
|
203
|
-
return rac_llm_component_get_state(handle_);
|
|
204
|
-
}
|
|
205
|
-
return RAC_LIFECYCLE_STATE_IDLE;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
} // namespace bridges
|
|
209
|
-
} // namespace runanywhere
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file LLMBridge.hpp
|
|
3
|
-
* @brief LLM capability bridge for React Native
|
|
4
|
-
*
|
|
5
|
-
* Matches Swift's CppBridge+LLM.swift pattern, providing:
|
|
6
|
-
* - Model lifecycle (load/unload)
|
|
7
|
-
* - Text generation (sync and streaming)
|
|
8
|
-
* - Cancellation support
|
|
9
|
-
*
|
|
10
|
-
* Aligned with rac_llm_component.h and rac_llm_types.h API.
|
|
11
|
-
* RACommons is REQUIRED - no stub implementations.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
#pragma once
|
|
15
|
-
|
|
16
|
-
#include <functional>
|
|
17
|
-
#include <memory>
|
|
18
|
-
#include <string>
|
|
19
|
-
|
|
20
|
-
// RACommons LLM headers - REQUIRED (flat include paths)
|
|
21
|
-
#include "rac_llm_component.h"
|
|
22
|
-
#include "rac_llm_types.h"
|
|
23
|
-
|
|
24
|
-
namespace runanywhere {
|
|
25
|
-
namespace bridges {
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @brief LLM streaming callbacks
|
|
29
|
-
*/
|
|
30
|
-
struct LLMStreamCallbacks {
|
|
31
|
-
std::function<bool(const std::string&)> onToken;
|
|
32
|
-
std::function<void(const std::string&, int, double)> onComplete;
|
|
33
|
-
std::function<void(int, const std::string&)> onError;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @brief LLM generation options
|
|
38
|
-
*/
|
|
39
|
-
struct LLMOptions {
|
|
40
|
-
int maxTokens = 512;
|
|
41
|
-
double temperature = 0.7;
|
|
42
|
-
double topP = 0.9;
|
|
43
|
-
int topK = 40;
|
|
44
|
-
std::string systemPrompt;
|
|
45
|
-
std::string stopSequence;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @brief LLM generation result
|
|
50
|
-
*/
|
|
51
|
-
struct LLMResult {
|
|
52
|
-
std::string text;
|
|
53
|
-
int tokenCount = 0;
|
|
54
|
-
double durationMs = 0.0;
|
|
55
|
-
bool cancelled = false;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @brief LLM capability bridge singleton
|
|
60
|
-
*
|
|
61
|
-
* Matches CppBridge+LLM.swift API.
|
|
62
|
-
* NOTE: RACommons is REQUIRED. All methods will throw std::runtime_error if
|
|
63
|
-
* the underlying C API calls fail.
|
|
64
|
-
*/
|
|
65
|
-
class LLMBridge {
|
|
66
|
-
public:
|
|
67
|
-
static LLMBridge& shared();
|
|
68
|
-
|
|
69
|
-
// Lifecycle
|
|
70
|
-
bool isLoaded() const;
|
|
71
|
-
std::string currentModelId() const;
|
|
72
|
-
/**
|
|
73
|
-
* Load an LLM model
|
|
74
|
-
* @param modelPath Path to the model file (.gguf)
|
|
75
|
-
* @param modelId Model identifier for telemetry (e.g., "smollm2-360m-q8_0")
|
|
76
|
-
* @param modelName Human-readable model name (e.g., "SmolLM2 360M Q8_0")
|
|
77
|
-
* @return RAC_SUCCESS or error code
|
|
78
|
-
*/
|
|
79
|
-
rac_result_t loadModel(const std::string& modelPath,
|
|
80
|
-
const std::string& modelId = "",
|
|
81
|
-
const std::string& modelName = "");
|
|
82
|
-
rac_result_t unload();
|
|
83
|
-
void cleanup();
|
|
84
|
-
void cancel();
|
|
85
|
-
void destroy();
|
|
86
|
-
|
|
87
|
-
// Generation
|
|
88
|
-
LLMResult generate(const std::string& prompt, const LLMOptions& options);
|
|
89
|
-
void generateStream(const std::string& prompt, const LLMOptions& options,
|
|
90
|
-
const LLMStreamCallbacks& callbacks);
|
|
91
|
-
|
|
92
|
-
// State
|
|
93
|
-
rac_lifecycle_state_t getState() const;
|
|
94
|
-
|
|
95
|
-
private:
|
|
96
|
-
LLMBridge();
|
|
97
|
-
~LLMBridge();
|
|
98
|
-
|
|
99
|
-
// Disable copy/move
|
|
100
|
-
LLMBridge(const LLMBridge&) = delete;
|
|
101
|
-
LLMBridge& operator=(const LLMBridge&) = delete;
|
|
102
|
-
|
|
103
|
-
rac_handle_t handle_ = nullptr;
|
|
104
|
-
std::string loadedModelId_;
|
|
105
|
-
bool cancellationRequested_ = false;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
} // namespace bridges
|
|
109
|
-
} // namespace runanywhere
|