@runanywhere/core 0.17.7 → 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 +259 -160
  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,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 */
@@ -0,0 +1,305 @@
1
+ /**
2
+ * @file rac_diffusion_platform.h
3
+ * @brief RunAnywhere Commons - Platform Diffusion Backend (Apple ml-stable-diffusion)
4
+ *
5
+ * C API for platform-native diffusion services. On Apple platforms, this uses
6
+ * ml-stable-diffusion with Core ML. The actual implementation is in Swift,
7
+ * with C++ providing the registration and callback infrastructure.
8
+ *
9
+ * This backend follows the same pattern as LlamaCPP and ONNX backends,
10
+ * but delegates to Swift via function pointer callbacks since
11
+ * ml-stable-diffusion is a Swift-only framework.
12
+ */
13
+
14
+ #ifndef RAC_DIFFUSION_PLATFORM_H
15
+ #define RAC_DIFFUSION_PLATFORM_H
16
+
17
+ #include <RACommons/rac_types.h>
18
+ #include <RACommons/rac_diffusion_types.h>
19
+
20
+ #ifdef __cplusplus
21
+ extern "C" {
22
+ #endif
23
+
24
+ // =============================================================================
25
+ // TYPES
26
+ // =============================================================================
27
+
28
+ /** Opaque handle to platform diffusion service */
29
+ typedef struct rac_diffusion_platform* rac_diffusion_platform_handle_t;
30
+
31
+ /**
32
+ * Platform diffusion configuration.
33
+ * Passed during initialization.
34
+ */
35
+ typedef struct rac_diffusion_platform_config {
36
+ /** Model variant (SD 1.5, SDXL, etc.) */
37
+ rac_diffusion_model_variant_t model_variant;
38
+
39
+ /** Enable safety checker */
40
+ rac_bool_t enable_safety_checker;
41
+
42
+ /** Reduce memory mode */
43
+ rac_bool_t reduce_memory;
44
+
45
+ /** Compute units to use (0 = auto, 1 = CPU, 2 = GPU, 3 = Neural Engine) */
46
+ int32_t compute_units;
47
+
48
+ /** Reserved for future use */
49
+ void* reserved;
50
+ } rac_diffusion_platform_config_t;
51
+
52
+ /**
53
+ * Generation options for platform diffusion.
54
+ */
55
+ typedef struct rac_diffusion_platform_options {
56
+ /** Text prompt */
57
+ const char* prompt;
58
+
59
+ /** Negative prompt */
60
+ const char* negative_prompt;
61
+
62
+ /** Output width */
63
+ int32_t width;
64
+
65
+ /** Output height */
66
+ int32_t height;
67
+
68
+ /** Number of inference steps */
69
+ int32_t steps;
70
+
71
+ /** Guidance scale */
72
+ float guidance_scale;
73
+
74
+ /** Random seed (-1 for random) */
75
+ int64_t seed;
76
+
77
+ /** Scheduler type */
78
+ rac_diffusion_scheduler_t scheduler;
79
+
80
+ /** Reserved for future options */
81
+ void* reserved;
82
+ } rac_diffusion_platform_options_t;
83
+
84
+ /**
85
+ * Platform diffusion result.
86
+ */
87
+ typedef struct rac_diffusion_platform_result {
88
+ /** Image data (RGBA format, caller must free) */
89
+ uint8_t* image_data;
90
+
91
+ /** Image data size in bytes */
92
+ size_t image_size;
93
+
94
+ /** Image width */
95
+ int32_t width;
96
+
97
+ /** Image height */
98
+ int32_t height;
99
+
100
+ /** Seed used for generation */
101
+ int64_t seed_used;
102
+
103
+ /** Whether safety check was triggered */
104
+ rac_bool_t safety_triggered;
105
+ } rac_diffusion_platform_result_t;
106
+
107
+ // =============================================================================
108
+ // SWIFT CALLBACK TYPES
109
+ // =============================================================================
110
+
111
+ /**
112
+ * Callback to check if platform diffusion can handle a model ID.
113
+ * Implemented in Swift.
114
+ *
115
+ * @param model_id Model identifier to check (can be NULL)
116
+ * @param user_data User-provided context
117
+ * @return RAC_TRUE if this backend can handle the model
118
+ */
119
+ typedef rac_bool_t (*rac_platform_diffusion_can_handle_fn)(const char* model_id, void* user_data);
120
+
121
+ /**
122
+ * Callback to create platform diffusion service.
123
+ * Implemented in Swift.
124
+ *
125
+ * @param model_path Path to model directory
126
+ * @param config Configuration options
127
+ * @param user_data User-provided context
128
+ * @return Handle to created service (Swift object pointer), or NULL on failure
129
+ */
130
+ typedef rac_handle_t (*rac_platform_diffusion_create_fn)(
131
+ const char* model_path, const rac_diffusion_platform_config_t* config, void* user_data);
132
+
133
+ /**
134
+ * Callback to generate image.
135
+ * Implemented in Swift.
136
+ *
137
+ * @param handle Service handle from create
138
+ * @param options Generation options
139
+ * @param out_result Output: Generated image result
140
+ * @param user_data User-provided context
141
+ * @return RAC_SUCCESS or error code
142
+ */
143
+ typedef rac_result_t (*rac_platform_diffusion_generate_fn)(
144
+ rac_handle_t handle, const rac_diffusion_platform_options_t* options,
145
+ rac_diffusion_platform_result_t* out_result, void* user_data);
146
+
147
+ /**
148
+ * Progress callback type for Swift.
149
+ *
150
+ * @param progress Progress value (0.0-1.0)
151
+ * @param step Current step
152
+ * @param total_steps Total steps
153
+ * @param user_data User-provided context
154
+ * @return RAC_TRUE to continue, RAC_FALSE to cancel
155
+ */
156
+ typedef rac_bool_t (*rac_platform_diffusion_progress_fn)(float progress, int32_t step,
157
+ int32_t total_steps, void* user_data);
158
+
159
+ /**
160
+ * Callback to generate image with progress.
161
+ * Implemented in Swift.
162
+ *
163
+ * @param handle Service handle from create
164
+ * @param options Generation options
165
+ * @param progress_callback Progress callback
166
+ * @param progress_user_data User data for progress callback
167
+ * @param out_result Output: Generated image result
168
+ * @param user_data User-provided context
169
+ * @return RAC_SUCCESS or error code
170
+ */
171
+ typedef rac_result_t (*rac_platform_diffusion_generate_with_progress_fn)(
172
+ rac_handle_t handle, const rac_diffusion_platform_options_t* options,
173
+ rac_platform_diffusion_progress_fn progress_callback, void* progress_user_data,
174
+ rac_diffusion_platform_result_t* out_result, void* user_data);
175
+
176
+ /**
177
+ * Callback to cancel generation.
178
+ * Implemented in Swift.
179
+ *
180
+ * @param handle Service handle
181
+ * @param user_data User-provided context
182
+ * @return RAC_SUCCESS or error code
183
+ */
184
+ typedef rac_result_t (*rac_platform_diffusion_cancel_fn)(rac_handle_t handle, void* user_data);
185
+
186
+ /**
187
+ * Callback to destroy platform diffusion service.
188
+ * Implemented in Swift.
189
+ *
190
+ * @param handle Service handle to destroy
191
+ * @param user_data User-provided context
192
+ */
193
+ typedef void (*rac_platform_diffusion_destroy_fn)(rac_handle_t handle, void* user_data);
194
+
195
+ /**
196
+ * Swift callbacks for platform diffusion operations.
197
+ */
198
+ typedef struct rac_platform_diffusion_callbacks {
199
+ rac_platform_diffusion_can_handle_fn can_handle;
200
+ rac_platform_diffusion_create_fn create;
201
+ rac_platform_diffusion_generate_fn generate;
202
+ rac_platform_diffusion_generate_with_progress_fn generate_with_progress;
203
+ rac_platform_diffusion_cancel_fn cancel;
204
+ rac_platform_diffusion_destroy_fn destroy;
205
+ void* user_data;
206
+ } rac_platform_diffusion_callbacks_t;
207
+
208
+ // =============================================================================
209
+ // CALLBACK REGISTRATION
210
+ // =============================================================================
211
+
212
+ /**
213
+ * Sets the Swift callbacks for platform diffusion operations.
214
+ * Must be called before using platform diffusion services.
215
+ *
216
+ * @param callbacks Callback functions (copied internally)
217
+ * @return RAC_SUCCESS on success
218
+ */
219
+ RAC_API rac_result_t rac_platform_diffusion_set_callbacks(
220
+ const rac_platform_diffusion_callbacks_t* callbacks);
221
+
222
+ /**
223
+ * Gets the current Swift callbacks.
224
+ *
225
+ * @return Pointer to callbacks, or NULL if not set
226
+ */
227
+ RAC_API const rac_platform_diffusion_callbacks_t* rac_platform_diffusion_get_callbacks(void);
228
+
229
+ /**
230
+ * Checks if Swift callbacks are registered.
231
+ *
232
+ * @return RAC_TRUE if callbacks are available
233
+ */
234
+ RAC_API rac_bool_t rac_platform_diffusion_is_available(void);
235
+
236
+ // =============================================================================
237
+ // SERVICE API
238
+ // =============================================================================
239
+
240
+ /**
241
+ * Creates a platform diffusion service.
242
+ *
243
+ * @param model_path Path to Core ML model directory
244
+ * @param config Configuration options (can be NULL for defaults)
245
+ * @param out_handle Output: Service handle
246
+ * @return RAC_SUCCESS on success, or error code
247
+ */
248
+ RAC_API rac_result_t rac_diffusion_platform_create(const char* model_path,
249
+ const rac_diffusion_platform_config_t* config,
250
+ rac_diffusion_platform_handle_t* out_handle);
251
+
252
+ /**
253
+ * Destroys a platform diffusion service.
254
+ *
255
+ * @param handle Service handle to destroy
256
+ */
257
+ RAC_API void rac_diffusion_platform_destroy(rac_diffusion_platform_handle_t handle);
258
+
259
+ /**
260
+ * Generates an image using platform diffusion.
261
+ *
262
+ * @param handle Service handle
263
+ * @param options Generation options
264
+ * @param out_result Output: Generated image
265
+ * @return RAC_SUCCESS on success, or error code
266
+ */
267
+ RAC_API rac_result_t rac_diffusion_platform_generate(
268
+ rac_diffusion_platform_handle_t handle, const rac_diffusion_platform_options_t* options,
269
+ rac_diffusion_platform_result_t* out_result);
270
+
271
+ /**
272
+ * Generates an image with progress reporting.
273
+ *
274
+ * @param handle Service handle
275
+ * @param options Generation options
276
+ * @param progress_callback Progress callback
277
+ * @param progress_user_data User data for progress callback
278
+ * @param out_result Output: Generated image
279
+ * @return RAC_SUCCESS on success, or error code
280
+ */
281
+ RAC_API rac_result_t rac_diffusion_platform_generate_with_progress(
282
+ rac_diffusion_platform_handle_t handle, const rac_diffusion_platform_options_t* options,
283
+ rac_platform_diffusion_progress_fn progress_callback, void* progress_user_data,
284
+ rac_diffusion_platform_result_t* out_result);
285
+
286
+ /**
287
+ * Cancels ongoing generation.
288
+ *
289
+ * @param handle Service handle
290
+ * @return RAC_SUCCESS on success, or error code
291
+ */
292
+ RAC_API rac_result_t rac_diffusion_platform_cancel(rac_diffusion_platform_handle_t handle);
293
+
294
+ /**
295
+ * Frees a platform diffusion result.
296
+ *
297
+ * @param result Result to free
298
+ */
299
+ RAC_API void rac_diffusion_platform_result_free(rac_diffusion_platform_result_t* result);
300
+
301
+ #ifdef __cplusplus
302
+ }
303
+ #endif
304
+
305
+ #endif /* RAC_DIFFUSION_PLATFORM_H */