@runanywhere/core 0.17.8 → 0.18.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.
Files changed (106) hide show
  1. package/README.md +218 -2
  2. package/RunAnywhereCore.podspec +1 -0
  3. package/android/CMakeLists.txt +24 -2
  4. package/android/build.gradle +61 -9
  5. package/android/src/main/cpp/cpp-adapter.cpp +51 -3
  6. package/android/src/main/include/rac/backends/rac_vlm_llamacpp.h +216 -0
  7. package/android/src/main/include/rac/core/capabilities/rac_lifecycle.h +3 -1
  8. package/android/src/main/include/rac/core/rac_core.h +11 -0
  9. package/android/src/main/include/rac/core/rac_types.h +8 -6
  10. package/android/src/main/include/rac/features/diffusion/rac_diffusion.h +22 -0
  11. package/android/src/main/include/rac/features/diffusion/rac_diffusion_component.h +263 -0
  12. package/android/src/main/include/rac/features/diffusion/rac_diffusion_model_registry.h +358 -0
  13. package/android/src/main/include/rac/features/diffusion/rac_diffusion_service.h +187 -0
  14. package/android/src/main/include/rac/features/diffusion/rac_diffusion_tokenizer.h +167 -0
  15. package/android/src/main/include/rac/features/diffusion/rac_diffusion_types.h +454 -0
  16. package/android/src/main/include/rac/features/llm/rac_tool_calling.h +373 -0
  17. package/android/src/main/include/rac/features/platform/rac_diffusion_platform.h +305 -0
  18. package/android/src/main/include/rac/features/vad/rac_vad_energy.h +1 -1
  19. package/android/src/main/include/rac/features/vlm/rac_vlm.h +16 -0
  20. package/android/src/main/include/rac/features/vlm/rac_vlm_component.h +168 -0
  21. package/android/src/main/include/rac/features/vlm/rac_vlm_service.h +206 -0
  22. package/android/src/main/include/rac/features/vlm/rac_vlm_types.h +417 -0
  23. package/android/src/main/include/rac/infrastructure/model_management/rac_model_registry.h +15 -0
  24. package/android/src/main/include/rac/infrastructure/model_management/rac_model_types.h +3 -0
  25. package/android/src/main/include/rac/utils/rac_image_utils.h +215 -0
  26. package/android/src/main/java/com/margelo/nitro/runanywhere/PlatformAdapterBridge.kt +201 -1
  27. package/android/src/main/jniLibs/arm64-v8a/libc++_shared.so +0 -0
  28. package/android/src/main/jniLibs/arm64-v8a/libomp.so +0 -0
  29. package/android/src/main/jniLibs/arm64-v8a/librac_commons.so +0 -0
  30. package/android/src/main/jniLibs/arm64-v8a/librunanywhere_jni.so +0 -0
  31. package/android/src/main/jniLibs/x86_64/libc++_shared.so +0 -0
  32. package/android/src/main/jniLibs/x86_64/libomp.so +0 -0
  33. package/android/src/main/jniLibs/x86_64/librac_commons.so +0 -0
  34. package/android/src/main/jniLibs/x86_64/librunanywhere_jni.so +0 -0
  35. package/cpp/HybridRunAnywhereCore.cpp +263 -163
  36. package/cpp/HybridRunAnywhereCore.hpp +11 -0
  37. package/cpp/bridges/InitBridge.cpp +234 -3
  38. package/cpp/bridges/PlatformDownloadBridge.h +44 -0
  39. package/cpp/bridges/ToolCallingBridge.cpp +188 -0
  40. package/cpp/bridges/ToolCallingBridge.hpp +98 -0
  41. package/cpp/third_party/nlohmann/json.hpp +24765 -0
  42. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/RACommons.h +18 -4
  43. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_core.h +11 -0
  44. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_diffusion.h +22 -0
  45. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_diffusion_component.h +263 -0
  46. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_diffusion_model_registry.h +358 -0
  47. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_diffusion_platform.h +305 -0
  48. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_diffusion_service.h +187 -0
  49. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_diffusion_tokenizer.h +167 -0
  50. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_diffusion_types.h +454 -0
  51. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_endpoints.h +3 -17
  52. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_image_utils.h +215 -0
  53. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_lifecycle.h +3 -1
  54. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_model_assignment.h +4 -20
  55. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_model_registry.h +15 -0
  56. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_model_types.h +3 -0
  57. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_tool_calling.h +373 -0
  58. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_types.h +8 -6
  59. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_vad_energy.h +1 -1
  60. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_vlm.h +16 -0
  61. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_vlm_component.h +168 -0
  62. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_vlm_llamacpp.h +216 -0
  63. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_vlm_service.h +206 -0
  64. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/Headers/rac_vlm_types.h +417 -0
  65. package/ios/Binaries/RACommons.xcframework/ios-arm64/RACommons.framework/RACommons +0 -0
  66. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/RACommons.h +18 -4
  67. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_core.h +11 -0
  68. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_diffusion.h +22 -0
  69. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_diffusion_component.h +263 -0
  70. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_diffusion_model_registry.h +358 -0
  71. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_diffusion_platform.h +305 -0
  72. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_diffusion_service.h +187 -0
  73. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_diffusion_tokenizer.h +167 -0
  74. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_diffusion_types.h +454 -0
  75. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_endpoints.h +3 -17
  76. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_image_utils.h +215 -0
  77. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_lifecycle.h +3 -1
  78. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_model_assignment.h +4 -20
  79. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_model_registry.h +15 -0
  80. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_model_types.h +3 -0
  81. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_tool_calling.h +373 -0
  82. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_types.h +8 -6
  83. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_vad_energy.h +1 -1
  84. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_vlm.h +16 -0
  85. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_vlm_component.h +168 -0
  86. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_vlm_llamacpp.h +216 -0
  87. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_vlm_service.h +206 -0
  88. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/Headers/rac_vlm_types.h +417 -0
  89. package/ios/Binaries/RACommons.xcframework/ios-arm64_x86_64-simulator/RACommons.framework/RACommons +0 -0
  90. package/ios/PlatformAdapterBridge.h +24 -1
  91. package/ios/PlatformAdapterBridge.m +243 -0
  92. package/nitrogen/generated/shared/c++/HybridRunAnywhereCoreSpec.cpp +4 -0
  93. package/nitrogen/generated/shared/c++/HybridRunAnywhereCoreSpec.hpp +4 -0
  94. package/package.json +8 -4
  95. package/src/Foundation/Security/SecureStorageService.ts +12 -6
  96. package/src/Public/Extensions/RunAnywhere+Models.ts +5 -3
  97. package/src/Public/Extensions/RunAnywhere+STT.ts +7 -2
  98. package/src/Public/Extensions/RunAnywhere+ToolCalling.ts +472 -0
  99. package/src/Public/Extensions/index.ts +16 -0
  100. package/src/Public/RunAnywhere.ts +18 -0
  101. package/src/index.ts +0 -1
  102. package/src/services/Network/index.ts +0 -1
  103. package/src/services/index.ts +0 -1
  104. package/src/specs/RunAnywhereCore.nitro.ts +72 -0
  105. package/src/types/ToolCallingTypes.ts +198 -0
  106. package/src/types/index.ts +13 -0
@@ -0,0 +1,263 @@
1
+ /**
2
+ * @file rac_diffusion_component.h
3
+ * @brief RunAnywhere Commons - Diffusion Capability Component
4
+ *
5
+ * Actor-based diffusion capability that owns model lifecycle and generation.
6
+ * Uses lifecycle manager for unified lifecycle + analytics handling.
7
+ *
8
+ * Supports:
9
+ * - Text-to-image generation
10
+ * - Image-to-image transformation
11
+ * - Inpainting with mask
12
+ * - Progress reporting with optional intermediate images
13
+ */
14
+
15
+ #ifndef RAC_DIFFUSION_COMPONENT_H
16
+ #define RAC_DIFFUSION_COMPONENT_H
17
+
18
+ #include <RACommons/rac_lifecycle.h>
19
+ #include <RACommons/rac_error.h>
20
+ #include <RACommons/rac_diffusion_types.h>
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ // =============================================================================
27
+ // DIFFUSION COMPONENT API - Component lifecycle and generation
28
+ // =============================================================================
29
+
30
+ /**
31
+ * @brief Create a diffusion capability component
32
+ *
33
+ * @param out_handle Output: Handle to the component
34
+ * @return RAC_SUCCESS or error code
35
+ */
36
+ RAC_API rac_result_t rac_diffusion_component_create(rac_handle_t* out_handle);
37
+
38
+ /**
39
+ * @brief Configure the diffusion component
40
+ *
41
+ * @param handle Component handle
42
+ * @param config Configuration
43
+ * @return RAC_SUCCESS or error code
44
+ */
45
+ RAC_API rac_result_t rac_diffusion_component_configure(rac_handle_t handle,
46
+ const rac_diffusion_config_t* config);
47
+
48
+ /**
49
+ * @brief Check if model is loaded
50
+ *
51
+ * @param handle Component handle
52
+ * @return RAC_TRUE if loaded, RAC_FALSE otherwise
53
+ */
54
+ RAC_API rac_bool_t rac_diffusion_component_is_loaded(rac_handle_t handle);
55
+
56
+ /**
57
+ * @brief Get current model ID
58
+ *
59
+ * @param handle Component handle
60
+ * @return Current model ID (NULL if not loaded)
61
+ */
62
+ RAC_API const char* rac_diffusion_component_get_model_id(rac_handle_t handle);
63
+
64
+ /**
65
+ * @brief Load a diffusion model
66
+ *
67
+ * @param handle Component handle
68
+ * @param model_path Path to the model directory
69
+ * @param model_id Model identifier for telemetry
70
+ * @param model_name Human-readable model name
71
+ * @return RAC_SUCCESS or error code
72
+ */
73
+ RAC_API rac_result_t rac_diffusion_component_load_model(rac_handle_t handle, const char* model_path,
74
+ const char* model_id,
75
+ const char* model_name);
76
+
77
+ /**
78
+ * @brief Unload the current model
79
+ *
80
+ * @param handle Component handle
81
+ * @return RAC_SUCCESS or error code
82
+ */
83
+ RAC_API rac_result_t rac_diffusion_component_unload(rac_handle_t handle);
84
+
85
+ /**
86
+ * @brief Cleanup and reset the component
87
+ *
88
+ * @param handle Component handle
89
+ * @return RAC_SUCCESS or error code
90
+ */
91
+ RAC_API rac_result_t rac_diffusion_component_cleanup(rac_handle_t handle);
92
+
93
+ /**
94
+ * @brief Cancel ongoing generation
95
+ *
96
+ * Best-effort cancellation.
97
+ *
98
+ * @param handle Component handle
99
+ * @return RAC_SUCCESS or error code
100
+ */
101
+ RAC_API rac_result_t rac_diffusion_component_cancel(rac_handle_t handle);
102
+
103
+ /**
104
+ * @brief Generate an image (non-streaming)
105
+ *
106
+ * Blocking call that generates an image from the prompt.
107
+ *
108
+ * @param handle Component handle
109
+ * @param options Generation options
110
+ * @param out_result Output: Generation result
111
+ * @return RAC_SUCCESS or error code
112
+ */
113
+ RAC_API rac_result_t rac_diffusion_component_generate(rac_handle_t handle,
114
+ const rac_diffusion_options_t* options,
115
+ rac_diffusion_result_t* out_result);
116
+
117
+ /**
118
+ * @brief Generate an image with progress callbacks
119
+ *
120
+ * Non-blocking call with progress reporting via callbacks.
121
+ *
122
+ * @param handle Component handle
123
+ * @param options Generation options
124
+ * @param progress_callback Called for each progress update
125
+ * @param complete_callback Called when generation completes
126
+ * @param error_callback Called on error
127
+ * @param user_data User context passed to callbacks
128
+ * @return RAC_SUCCESS or error code
129
+ */
130
+ RAC_API rac_result_t rac_diffusion_component_generate_with_callbacks(
131
+ rac_handle_t handle, const rac_diffusion_options_t* options,
132
+ rac_diffusion_progress_callback_fn progress_callback,
133
+ rac_diffusion_complete_callback_fn complete_callback,
134
+ rac_diffusion_error_callback_fn error_callback, void* user_data);
135
+
136
+ // =============================================================================
137
+ // JSON CONVENIENCE HELPERS
138
+ // =============================================================================
139
+
140
+ /**
141
+ * @brief Configure diffusion component from JSON
142
+ *
143
+ * JSON schema (flat object):
144
+ * {
145
+ * "model_id": "optional-model-id",
146
+ * "model_variant": 0 | "sd15" | "sd21" | "sdxl" | "sdxl_turbo" | "sdxs" | "lcm",
147
+ * "enable_safety_checker": true/false,
148
+ * "reduce_memory": true/false,
149
+ * "tokenizer_source": 0 | 1 | 2 | 99,
150
+ * "tokenizer_custom_url": "https://..."
151
+ * }
152
+ *
153
+ * @param handle Component handle
154
+ * @param config_json JSON string
155
+ * @return RAC_SUCCESS or error code
156
+ */
157
+ RAC_API rac_result_t rac_diffusion_component_configure_json(rac_handle_t handle,
158
+ const char* config_json);
159
+
160
+ /**
161
+ * @brief Generate image from JSON options
162
+ *
163
+ * JSON schema (flat object):
164
+ * {
165
+ * "prompt": "text prompt",
166
+ * "negative_prompt": "optional",
167
+ * "width": 512,
168
+ * "height": 512,
169
+ * "steps": 28,
170
+ * "guidance_scale": 7.5,
171
+ * "seed": -1,
172
+ * "scheduler": 0 | "dpm++_2m_karras" | "dpm++_2m" | "dpm++_2m_sde" | "ddim" | "euler" | "euler_a" | "pndm" | "lms",
173
+ * "mode": 0 | "txt2img" | "img2img" | "inpainting",
174
+ * "denoise_strength": 0.75,
175
+ * "report_intermediate_images": false,
176
+ * "progress_stride": 1
177
+ * }
178
+ *
179
+ * @param handle Component handle
180
+ * @param options_json JSON string
181
+ * @param input_image_data Optional input image bytes (PNG/JPEG or RGBA)
182
+ * @param input_image_size Size of input image bytes
183
+ * @param mask_data Optional mask image bytes (PNG/JPEG or grayscale)
184
+ * @param mask_size Size of mask bytes
185
+ * @param out_json Output JSON (caller must free with rac_free)
186
+ * @return RAC_SUCCESS or error code
187
+ */
188
+ RAC_API rac_result_t rac_diffusion_component_generate_json(
189
+ rac_handle_t handle, const char* options_json, const uint8_t* input_image_data,
190
+ size_t input_image_size, const uint8_t* mask_data, size_t mask_size, char** out_json);
191
+
192
+ /**
193
+ * @brief Get diffusion info as JSON
194
+ *
195
+ * Output schema:
196
+ * {
197
+ * "is_ready": true/false,
198
+ * "current_model": "id",
199
+ * "model_variant": 0,
200
+ * "supports_text_to_image": true/false,
201
+ * "supports_image_to_image": true/false,
202
+ * "supports_inpainting": true/false,
203
+ * "safety_checker_enabled": true/false,
204
+ * "max_width": 512,
205
+ * "max_height": 512
206
+ * }
207
+ *
208
+ * @param handle Component handle
209
+ * @param out_json Output JSON (caller must free with rac_free)
210
+ * @return RAC_SUCCESS or error code
211
+ */
212
+ RAC_API rac_result_t rac_diffusion_component_get_info_json(rac_handle_t handle, char** out_json);
213
+
214
+ /**
215
+ * @brief Get supported capabilities
216
+ *
217
+ * Returns a bitmask of supported capabilities.
218
+ *
219
+ * @param handle Component handle
220
+ * @return Capability bitmask (RAC_DIFFUSION_CAP_* flags)
221
+ */
222
+ RAC_API uint32_t rac_diffusion_component_get_capabilities(rac_handle_t handle);
223
+
224
+ /**
225
+ * @brief Get service information
226
+ *
227
+ * @param handle Component handle
228
+ * @param out_info Output: Service information
229
+ * @return RAC_SUCCESS or error code
230
+ */
231
+ RAC_API rac_result_t rac_diffusion_component_get_info(rac_handle_t handle,
232
+ rac_diffusion_info_t* out_info);
233
+
234
+ /**
235
+ * @brief Get lifecycle state
236
+ *
237
+ * @param handle Component handle
238
+ * @return Current lifecycle state
239
+ */
240
+ RAC_API rac_lifecycle_state_t rac_diffusion_component_get_state(rac_handle_t handle);
241
+
242
+ /**
243
+ * @brief Get lifecycle metrics
244
+ *
245
+ * @param handle Component handle
246
+ * @param out_metrics Output: Lifecycle metrics
247
+ * @return RAC_SUCCESS or error code
248
+ */
249
+ RAC_API rac_result_t rac_diffusion_component_get_metrics(rac_handle_t handle,
250
+ rac_lifecycle_metrics_t* out_metrics);
251
+
252
+ /**
253
+ * @brief Destroy the diffusion component
254
+ *
255
+ * @param handle Component handle
256
+ */
257
+ RAC_API void rac_diffusion_component_destroy(rac_handle_t handle);
258
+
259
+ #ifdef __cplusplus
260
+ }
261
+ #endif
262
+
263
+ #endif /* RAC_DIFFUSION_COMPONENT_H */
@@ -0,0 +1,358 @@
1
+ /**
2
+ * @file rac_diffusion_model_registry.h
3
+ * @brief Diffusion Model Registry - CoreML-based model definitions for iOS/macOS
4
+ *
5
+ * Provides a registry for diffusion models. Currently supports CoreML backend only
6
+ * (iOS/macOS with Apple Neural Engine acceleration).
7
+ *
8
+ * Features:
9
+ * - Type-safe model definitions (no magic strings)
10
+ * - CoreML backend with ANE → GPU → CPU automatic fallback
11
+ * - Strategy pattern for extensibility
12
+ * - Tokenizer source configuration (SD 1.5, SD 2.x, SDXL)
13
+ */
14
+
15
+ #ifndef RAC_DIFFUSION_MODEL_REGISTRY_H
16
+ #define RAC_DIFFUSION_MODEL_REGISTRY_H
17
+
18
+ #include <RACommons/rac_types.h>
19
+ #include <RACommons/rac_diffusion_types.h>
20
+
21
+ #ifdef __cplusplus
22
+ extern "C" {
23
+ #endif
24
+
25
+ // =============================================================================
26
+ // BACKEND AND PLATFORM TYPES
27
+ // =============================================================================
28
+
29
+ /**
30
+ * @brief Supported inference backends for diffusion models
31
+ *
32
+ * Currently only CoreML is implemented for iOS/macOS.
33
+ * Other backends are reserved for future expansion.
34
+ */
35
+ typedef enum rac_diffusion_backend {
36
+ RAC_DIFFUSION_BACKEND_ONNX = 0, /**< ONNX Runtime (reserved for future) */
37
+ RAC_DIFFUSION_BACKEND_COREML = 1, /**< CoreML (iOS/macOS - currently supported) */
38
+ RAC_DIFFUSION_BACKEND_TFLITE = 2, /**< TensorFlow Lite (reserved for future) */
39
+ RAC_DIFFUSION_BACKEND_AUTO = 99 /**< Auto-select (defaults to CoreML on Apple) */
40
+ } rac_diffusion_backend_t;
41
+
42
+ /**
43
+ * @brief Platform availability flags (bitmask)
44
+ *
45
+ * Used to specify which platforms a model supports.
46
+ */
47
+ typedef enum rac_diffusion_platform_flags {
48
+ RAC_DIFFUSION_PLATFORM_NONE = 0,
49
+ RAC_DIFFUSION_PLATFORM_IOS = (1 << 0),
50
+ RAC_DIFFUSION_PLATFORM_ANDROID = (1 << 1),
51
+ RAC_DIFFUSION_PLATFORM_MACOS = (1 << 2),
52
+ RAC_DIFFUSION_PLATFORM_WINDOWS = (1 << 3),
53
+ RAC_DIFFUSION_PLATFORM_LINUX = (1 << 4),
54
+ RAC_DIFFUSION_PLATFORM_ALL = 0xFFFF
55
+ } rac_diffusion_platform_flags_t;
56
+
57
+ /**
58
+ * @brief Hardware acceleration capabilities
59
+ *
60
+ * Describes what hardware the model can utilize.
61
+ */
62
+ typedef enum rac_diffusion_hardware {
63
+ RAC_DIFFUSION_HW_CPU = (1 << 0), /**< CPU (always available) */
64
+ RAC_DIFFUSION_HW_GPU = (1 << 1), /**< GPU acceleration */
65
+ RAC_DIFFUSION_HW_ANE = (1 << 2), /**< Apple Neural Engine */
66
+ RAC_DIFFUSION_HW_NPU = (1 << 3), /**< Android NPU (Hexagon, etc.) */
67
+ RAC_DIFFUSION_HW_DSP = (1 << 4), /**< Android DSP */
68
+ } rac_diffusion_hardware_t;
69
+
70
+ // =============================================================================
71
+ // MODEL DEFINITION STRUCTURE
72
+ // =============================================================================
73
+
74
+ /**
75
+ * @brief Default generation parameters for a model
76
+ */
77
+ typedef struct rac_diffusion_model_defaults {
78
+ int32_t width; /**< Default output width */
79
+ int32_t height; /**< Default output height */
80
+ int32_t steps; /**< Recommended inference steps */
81
+ float guidance_scale; /**< CFG scale (0.0 for CFG-free models) */
82
+ rac_diffusion_scheduler_t scheduler; /**< Recommended scheduler */
83
+ rac_bool_t requires_cfg; /**< True if model needs CFG (false for SDXS/Turbo) */
84
+ } rac_diffusion_model_defaults_t;
85
+
86
+ /**
87
+ * @brief Download information for a model
88
+ */
89
+ typedef struct rac_diffusion_model_download {
90
+ const char* base_url; /**< HuggingFace URL or CDN */
91
+ const char* onnx_path; /**< Path to ONNX files within repo */
92
+ const char* coreml_path; /**< Path to CoreML files (if available) */
93
+ uint64_t size_bytes; /**< Approximate download size */
94
+ const char* checksum; /**< SHA256 checksum (optional) */
95
+ } rac_diffusion_model_download_t;
96
+
97
+ /**
98
+ * @brief Tokenizer information for a model
99
+ */
100
+ typedef struct rac_diffusion_model_tokenizer {
101
+ rac_diffusion_tokenizer_source_t source; /**< Tokenizer type */
102
+ const char* custom_url; /**< For custom tokenizers */
103
+ } rac_diffusion_model_tokenizer_t;
104
+
105
+ /**
106
+ * @brief Complete diffusion model definition
107
+ *
108
+ * Contains all metadata needed to download, load, and use a model.
109
+ * This structure is shared across all SDKs via the C++ commons layer.
110
+ *
111
+ * ## Adding a New Model
112
+ *
113
+ * To add a new diffusion model:
114
+ * 1. Add a new `rac_diffusion_model_def_t` in `diffusion_model_registry.cpp`
115
+ * 2. Include it in the `BUILTIN_MODELS` array
116
+ * 3. Set the appropriate tokenizer source (SD15, SD2, SDXL, or CUSTOM)
117
+ *
118
+ * Example:
119
+ * @code
120
+ * static const rac_diffusion_model_def_t MY_MODEL = {
121
+ * .model_id = "my-model-onnx",
122
+ * .display_name = "My Custom Model",
123
+ * .description = "Description here",
124
+ * .variant = RAC_DIFFUSION_MODEL_SD_1_5,
125
+ * .backend = RAC_DIFFUSION_BACKEND_ONNX,
126
+ * .platforms = RAC_DIFFUSION_PLATFORM_ALL,
127
+ * .hardware = RAC_DIFFUSION_HW_CPU | RAC_DIFFUSION_HW_GPU,
128
+ * .defaults = { .width = 512, .height = 512, .steps = 20, ... },
129
+ * .download = {
130
+ * .base_url = "https://huggingface.co/my-org/my-model",
131
+ * .onnx_path = "onnx",
132
+ * .size_bytes = 2000000000ULL,
133
+ * },
134
+ * .tokenizer = {
135
+ * .source = RAC_DIFFUSION_TOKENIZER_SD_1_5, // Reuse existing tokenizer
136
+ * },
137
+ * };
138
+ * @endcode
139
+ */
140
+ typedef struct rac_diffusion_model_def {
141
+ /** Unique model identifier (e.g., "sdxs-512-0.9-onnx") */
142
+ const char* model_id;
143
+
144
+ /** Human-readable name */
145
+ const char* display_name;
146
+
147
+ /** Description */
148
+ const char* description;
149
+
150
+ /** Model variant (SD 1.5, SDXL, SDXS, LCM, etc.) */
151
+ rac_diffusion_model_variant_t variant;
152
+
153
+ /** Preferred backend for this model */
154
+ rac_diffusion_backend_t backend;
155
+
156
+ /** Platform availability (bitmask of rac_diffusion_platform_t) */
157
+ uint32_t platforms;
158
+
159
+ /** Hardware capabilities (bitmask of rac_diffusion_hardware_t) */
160
+ uint32_t hardware;
161
+
162
+ /** Default generation parameters */
163
+ rac_diffusion_model_defaults_t defaults;
164
+
165
+ /** Download information */
166
+ rac_diffusion_model_download_t download;
167
+
168
+ /** Tokenizer information */
169
+ rac_diffusion_model_tokenizer_t tokenizer;
170
+
171
+ /** Model-specific flags */
172
+ rac_bool_t is_recommended; /**< Show as recommended in UI */
173
+ rac_bool_t supports_img2img; /**< Supports image-to-image */
174
+ rac_bool_t supports_inpainting; /**< Supports inpainting */
175
+
176
+ } rac_diffusion_model_def_t;
177
+
178
+ // =============================================================================
179
+ // MODEL STRATEGY INTERFACE
180
+ // =============================================================================
181
+
182
+ /**
183
+ * @brief Model strategy - allows custom model handling
184
+ *
185
+ * Contributors implement this interface to add support for new model types
186
+ * without modifying core SDK code.
187
+ *
188
+ * Example:
189
+ * @code
190
+ * static rac_bool_t my_can_handle(const char* model_id, void* user_data) {
191
+ * return strcmp(model_id, "my-custom-model") == 0 ? RAC_TRUE : RAC_FALSE;
192
+ * }
193
+ *
194
+ * static rac_result_t my_get_model_def(const char* model_id,
195
+ * rac_diffusion_model_def_t* out_def,
196
+ * void* user_data) {
197
+ * if (strcmp(model_id, "my-custom-model") == 0) {
198
+ * *out_def = MY_CUSTOM_MODEL_DEF;
199
+ * return RAC_SUCCESS;
200
+ * }
201
+ * return RAC_ERROR_NOT_FOUND;
202
+ * }
203
+ *
204
+ * void register_my_models(void) {
205
+ * static rac_diffusion_model_strategy_t strategy = {
206
+ * .name = "MyModels",
207
+ * .can_handle = my_can_handle,
208
+ * .get_model_def = my_get_model_def,
209
+ * // ...
210
+ * };
211
+ * rac_diffusion_model_registry_register(&strategy);
212
+ * }
213
+ * @endcode
214
+ */
215
+ typedef struct rac_diffusion_model_strategy {
216
+ /** Strategy name (e.g., "SDXS", "LCM", "CustomModel") */
217
+ const char* name;
218
+
219
+ /** Check if this strategy can handle a model ID */
220
+ rac_bool_t (*can_handle)(const char* model_id, void* user_data);
221
+
222
+ /** Get model definition for a model ID */
223
+ rac_result_t (*get_model_def)(const char* model_id,
224
+ rac_diffusion_model_def_t* out_def,
225
+ void* user_data);
226
+
227
+ /** Get all models supported by this strategy */
228
+ rac_result_t (*list_models)(rac_diffusion_model_def_t** out_models,
229
+ size_t* out_count,
230
+ void* user_data);
231
+
232
+ /** Select best backend for current platform */
233
+ rac_diffusion_backend_t (*select_backend)(const rac_diffusion_model_def_t* model,
234
+ void* user_data);
235
+
236
+ /** Optional: Custom model loading (if default isn't suitable) */
237
+ rac_result_t (*load_model)(const char* model_path,
238
+ const rac_diffusion_model_def_t* model_def,
239
+ rac_handle_t* out_service,
240
+ void* user_data);
241
+
242
+ /** User data passed to callbacks */
243
+ void* user_data;
244
+
245
+ } rac_diffusion_model_strategy_t;
246
+
247
+ // =============================================================================
248
+ // REGISTRY API
249
+ // =============================================================================
250
+
251
+ /**
252
+ * @brief Initialize the diffusion model registry
253
+ *
254
+ * Registers built-in model strategies (SD 1.5, SDXS, LCM, etc.)
255
+ * Must be called during SDK initialization.
256
+ */
257
+ RAC_API void rac_diffusion_model_registry_init(void);
258
+
259
+ /**
260
+ * @brief Cleanup the diffusion model registry
261
+ */
262
+ RAC_API void rac_diffusion_model_registry_cleanup(void);
263
+
264
+ /**
265
+ * @brief Register a model strategy
266
+ *
267
+ * @param strategy Strategy to register (caller retains ownership)
268
+ * @return RAC_SUCCESS on success, RAC_ERROR_ALREADY_EXISTS if name taken
269
+ */
270
+ RAC_API rac_result_t rac_diffusion_model_registry_register(
271
+ const rac_diffusion_model_strategy_t* strategy);
272
+
273
+ /**
274
+ * @brief Unregister a model strategy
275
+ *
276
+ * @param name Strategy name to unregister
277
+ * @return RAC_SUCCESS on success, RAC_ERROR_NOT_FOUND if not registered
278
+ */
279
+ RAC_API rac_result_t rac_diffusion_model_registry_unregister(const char* name);
280
+
281
+ /**
282
+ * @brief Get model definition by ID
283
+ *
284
+ * @param model_id Model identifier
285
+ * @param out_def Output model definition (filled on success)
286
+ * @return RAC_SUCCESS if found, RAC_ERROR_NOT_FOUND otherwise
287
+ */
288
+ RAC_API rac_result_t rac_diffusion_model_registry_get(
289
+ const char* model_id,
290
+ rac_diffusion_model_def_t* out_def);
291
+
292
+ /**
293
+ * @brief List all available models for current platform
294
+ *
295
+ * @param out_models Output array (caller must free with free())
296
+ * @param out_count Number of models
297
+ * @return RAC_SUCCESS on success
298
+ */
299
+ RAC_API rac_result_t rac_diffusion_model_registry_list(
300
+ rac_diffusion_model_def_t** out_models,
301
+ size_t* out_count);
302
+
303
+ /**
304
+ * @brief Select best backend for a model on current platform
305
+ *
306
+ * This function implements the fallback chain:
307
+ * - iOS/macOS: CoreML (ANE → GPU → CPU automatic via CoreML)
308
+ * - Android: ONNX with NNAPI EP (NPU → DSP → GPU → CPU automatic via NNAPI)
309
+ * - Desktop: ONNX with CPU EP
310
+ *
311
+ * @param model_id Model identifier
312
+ * @return Best backend, or RAC_DIFFUSION_BACKEND_ONNX as fallback
313
+ */
314
+ RAC_API rac_diffusion_backend_t rac_diffusion_model_registry_select_backend(
315
+ const char* model_id);
316
+
317
+ /**
318
+ * @brief Check if a model is available on current platform
319
+ *
320
+ * @param model_id Model identifier
321
+ * @return RAC_TRUE if available, RAC_FALSE otherwise
322
+ */
323
+ RAC_API rac_bool_t rac_diffusion_model_registry_is_available(const char* model_id);
324
+
325
+ /**
326
+ * @brief Get recommended model for current platform
327
+ *
328
+ * Returns the model marked as is_recommended=true that's available on
329
+ * the current platform.
330
+ *
331
+ * @param out_def Output model definition (filled on success)
332
+ * @return RAC_SUCCESS if found, RAC_ERROR_NOT_FOUND if no recommendation
333
+ */
334
+ RAC_API rac_result_t rac_diffusion_model_registry_get_recommended(
335
+ rac_diffusion_model_def_t* out_def);
336
+
337
+ /**
338
+ * @brief Get current platform flags
339
+ *
340
+ * @return Bitmask of current platform (e.g., RAC_DIFFUSION_PLATFORM_IOS)
341
+ */
342
+ RAC_API uint32_t rac_diffusion_model_registry_get_current_platform(void);
343
+
344
+ /**
345
+ * @brief Check if model variant requires CFG (classifier-free guidance)
346
+ *
347
+ * SDXS, SDXL Turbo, and similar distilled models don't need CFG.
348
+ *
349
+ * @param variant Model variant
350
+ * @return RAC_TRUE if CFG is required, RAC_FALSE for CFG-free models
351
+ */
352
+ RAC_API rac_bool_t rac_diffusion_model_requires_cfg(rac_diffusion_model_variant_t variant);
353
+
354
+ #ifdef __cplusplus
355
+ }
356
+ #endif
357
+
358
+ #endif /* RAC_DIFFUSION_MODEL_REGISTRY_H */