@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 "rac/core/rac_types.h"
19
+ #include "rac/features/diffusion/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,187 @@
1
+ /**
2
+ * @file rac_diffusion_service.h
3
+ * @brief RunAnywhere Commons - Diffusion Service Interface
4
+ *
5
+ * Defines the generic diffusion service API and vtable for multi-backend dispatch.
6
+ * Backends (CoreML, ONNX, Platform) implement the vtable and register
7
+ * with the service registry.
8
+ */
9
+
10
+ #ifndef RAC_DIFFUSION_SERVICE_H
11
+ #define RAC_DIFFUSION_SERVICE_H
12
+
13
+ #include "rac/core/rac_error.h"
14
+ #include "rac/features/diffusion/rac_diffusion_types.h"
15
+
16
+ #ifdef __cplusplus
17
+ extern "C" {
18
+ #endif
19
+
20
+ // =============================================================================
21
+ // SERVICE VTABLE - Backend implementations provide this
22
+ // =============================================================================
23
+
24
+ /**
25
+ * Diffusion Service operations vtable.
26
+ * Each backend implements these functions and provides a static vtable.
27
+ */
28
+ typedef struct rac_diffusion_service_ops {
29
+ /** Initialize the service with a model path */
30
+ rac_result_t (*initialize)(void* impl, const char* model_path,
31
+ const rac_diffusion_config_t* config);
32
+
33
+ /** Generate image (blocking) */
34
+ rac_result_t (*generate)(void* impl, const rac_diffusion_options_t* options,
35
+ rac_diffusion_result_t* out_result);
36
+
37
+ /** Generate image with progress callback */
38
+ rac_result_t (*generate_with_progress)(void* impl, const rac_diffusion_options_t* options,
39
+ rac_diffusion_progress_callback_fn progress_callback,
40
+ void* user_data, rac_diffusion_result_t* out_result);
41
+
42
+ /** Get service info */
43
+ rac_result_t (*get_info)(void* impl, rac_diffusion_info_t* out_info);
44
+
45
+ /** Get supported capabilities as bitmask */
46
+ uint32_t (*get_capabilities)(void* impl);
47
+
48
+ /** Cancel ongoing generation */
49
+ rac_result_t (*cancel)(void* impl);
50
+
51
+ /** Cleanup/unload model (keeps service alive) */
52
+ rac_result_t (*cleanup)(void* impl);
53
+
54
+ /** Destroy the service */
55
+ void (*destroy)(void* impl);
56
+ } rac_diffusion_service_ops_t;
57
+
58
+ /**
59
+ * Diffusion Service instance.
60
+ * Contains vtable pointer and backend-specific implementation.
61
+ */
62
+ typedef struct rac_diffusion_service {
63
+ /** Vtable with backend operations */
64
+ const rac_diffusion_service_ops_t* ops;
65
+
66
+ /** Backend-specific implementation handle */
67
+ void* impl;
68
+
69
+ /** Model ID for reference */
70
+ const char* model_id;
71
+ } rac_diffusion_service_t;
72
+
73
+ // =============================================================================
74
+ // PUBLIC API - Generic service functions
75
+ // =============================================================================
76
+
77
+ /**
78
+ * @brief Create a diffusion service
79
+ *
80
+ * Routes through service registry to find appropriate backend.
81
+ *
82
+ * @param model_id Model identifier (registry ID or path to model)
83
+ * @param out_handle Output: Handle to the created service
84
+ * @return RAC_SUCCESS or error code
85
+ */
86
+ RAC_API rac_result_t rac_diffusion_create(const char* model_id, rac_handle_t* out_handle);
87
+
88
+ /**
89
+ * @brief Create a diffusion service with configuration
90
+ *
91
+ * Routes through service registry to find appropriate backend, honoring
92
+ * configuration hints such as preferred framework when provided.
93
+ *
94
+ * @param model_id Model identifier (registry ID or path to model)
95
+ * @param config Optional configuration (can be NULL)
96
+ * @param out_handle Output: Handle to the created service
97
+ * @return RAC_SUCCESS or error code
98
+ */
99
+ RAC_API rac_result_t rac_diffusion_create_with_config(const char* model_id,
100
+ const rac_diffusion_config_t* config,
101
+ rac_handle_t* out_handle);
102
+
103
+ /**
104
+ * @brief Initialize a diffusion service
105
+ *
106
+ * @param handle Service handle
107
+ * @param model_path Path to the model directory
108
+ * @param config Configuration (can be NULL for defaults)
109
+ * @return RAC_SUCCESS or error code
110
+ */
111
+ RAC_API rac_result_t rac_diffusion_initialize(rac_handle_t handle, const char* model_path,
112
+ const rac_diffusion_config_t* config);
113
+
114
+ /**
115
+ * @brief Generate an image from prompt
116
+ *
117
+ * Blocking call that generates an image.
118
+ *
119
+ * @param handle Service handle
120
+ * @param options Generation options
121
+ * @param out_result Output: Generation result (caller must free with rac_diffusion_result_free)
122
+ * @return RAC_SUCCESS or error code
123
+ */
124
+ RAC_API rac_result_t rac_diffusion_generate(rac_handle_t handle,
125
+ const rac_diffusion_options_t* options,
126
+ rac_diffusion_result_t* out_result);
127
+
128
+ /**
129
+ * @brief Generate an image with progress reporting
130
+ *
131
+ * @param handle Service handle
132
+ * @param options Generation options
133
+ * @param progress_callback Callback for progress updates
134
+ * @param user_data User context passed to callback
135
+ * @param out_result Output: Generation result (caller must free with rac_diffusion_result_free)
136
+ * @return RAC_SUCCESS or error code
137
+ */
138
+ RAC_API rac_result_t rac_diffusion_generate_with_progress(
139
+ rac_handle_t handle, const rac_diffusion_options_t* options,
140
+ rac_diffusion_progress_callback_fn progress_callback, void* user_data,
141
+ rac_diffusion_result_t* out_result);
142
+
143
+ /**
144
+ * @brief Get service information
145
+ *
146
+ * @param handle Service handle
147
+ * @param out_info Output: Service information
148
+ * @return RAC_SUCCESS or error code
149
+ */
150
+ RAC_API rac_result_t rac_diffusion_get_info(rac_handle_t handle, rac_diffusion_info_t* out_info);
151
+
152
+ /**
153
+ * @brief Get supported capabilities as bitmask
154
+ *
155
+ * @param handle Service handle
156
+ * @return Capability bitmask (RAC_DIFFUSION_CAP_* flags)
157
+ */
158
+ RAC_API uint32_t rac_diffusion_get_capabilities(rac_handle_t handle);
159
+
160
+ /**
161
+ * @brief Cancel ongoing generation
162
+ *
163
+ * @param handle Service handle
164
+ * @return RAC_SUCCESS or error code
165
+ */
166
+ RAC_API rac_result_t rac_diffusion_cancel(rac_handle_t handle);
167
+
168
+ /**
169
+ * @brief Cleanup and release model resources
170
+ *
171
+ * @param handle Service handle
172
+ * @return RAC_SUCCESS or error code
173
+ */
174
+ RAC_API rac_result_t rac_diffusion_cleanup(rac_handle_t handle);
175
+
176
+ /**
177
+ * @brief Destroy a diffusion service instance
178
+ *
179
+ * @param handle Service handle to destroy
180
+ */
181
+ RAC_API void rac_diffusion_destroy(rac_handle_t handle);
182
+
183
+ #ifdef __cplusplus
184
+ }
185
+ #endif
186
+
187
+ #endif /* RAC_DIFFUSION_SERVICE_H */