@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,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 */
@@ -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 <RACommons/rac_error.h>
14
+ #include <RACommons/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 */
@@ -0,0 +1,167 @@
1
+ /**
2
+ * @file rac_diffusion_tokenizer.h
3
+ * @brief RunAnywhere Commons - Diffusion Tokenizer Utilities
4
+ *
5
+ * Utilities for managing diffusion model tokenizer files.
6
+ * Apple's compiled CoreML models don't include tokenizer files (vocab.json, merges.txt),
7
+ * so they must be downloaded from HuggingFace.
8
+ *
9
+ * This API provides:
10
+ * - URL resolution for predefined tokenizer sources
11
+ * - Automatic download of missing tokenizer files
12
+ * - Support for custom tokenizer URLs
13
+ */
14
+
15
+ #ifndef RAC_DIFFUSION_TOKENIZER_H
16
+ #define RAC_DIFFUSION_TOKENIZER_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
+ // TOKENIZER FILE NAMES
27
+ // =============================================================================
28
+
29
+ /** Vocabulary file name */
30
+ #define RAC_DIFFUSION_TOKENIZER_VOCAB_FILE "vocab.json"
31
+
32
+ /** Merge rules file name */
33
+ #define RAC_DIFFUSION_TOKENIZER_MERGES_FILE "merges.txt"
34
+
35
+ // =============================================================================
36
+ // URL RESOLUTION
37
+ // =============================================================================
38
+
39
+ /**
40
+ * @brief Get the base URL for a tokenizer source
41
+ *
42
+ * Returns the HuggingFace URL for the specified tokenizer source.
43
+ * For custom sources, returns the custom_base_url from config.
44
+ *
45
+ * @param source Tokenizer source preset
46
+ * @param custom_url Custom URL (only used when source == RAC_DIFFUSION_TOKENIZER_CUSTOM)
47
+ * @return Base URL string (static, do not free), or NULL if invalid
48
+ *
49
+ * @note URLs returned are HuggingFace raw file URLs (resolve/main/tokenizer)
50
+ *
51
+ * Example return values:
52
+ * - SD_1_5: "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/tokenizer"
53
+ * - SD_2_X: "https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/tokenizer"
54
+ * - SDXL: "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/tokenizer"
55
+ * - CUSTOM: Returns custom_url parameter
56
+ */
57
+ RAC_API const char* rac_diffusion_tokenizer_get_base_url(rac_diffusion_tokenizer_source_t source,
58
+ const char* custom_url);
59
+
60
+ /**
61
+ * @brief Get the full URL for a tokenizer file
62
+ *
63
+ * Constructs the full URL for downloading a specific tokenizer file.
64
+ *
65
+ * @param source Tokenizer source preset
66
+ * @param custom_url Custom URL (only used when source == RAC_DIFFUSION_TOKENIZER_CUSTOM)
67
+ * @param filename File name to append (e.g., "vocab.json" or "merges.txt")
68
+ * @param out_url Output buffer for the full URL
69
+ * @param out_url_size Size of output buffer
70
+ * @return RAC_SUCCESS or error code
71
+ *
72
+ * Example:
73
+ * @code
74
+ * char url[512];
75
+ * rac_diffusion_tokenizer_get_file_url(
76
+ * RAC_DIFFUSION_TOKENIZER_SD_1_5,
77
+ * NULL,
78
+ * "vocab.json",
79
+ * url,
80
+ * sizeof(url)
81
+ * );
82
+ * // url = "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/tokenizer/vocab.json"
83
+ * @endcode
84
+ */
85
+ RAC_API rac_result_t rac_diffusion_tokenizer_get_file_url(rac_diffusion_tokenizer_source_t source,
86
+ const char* custom_url,
87
+ const char* filename, char* out_url,
88
+ size_t out_url_size);
89
+
90
+ // =============================================================================
91
+ // FILE MANAGEMENT
92
+ // =============================================================================
93
+
94
+ /**
95
+ * @brief Check if tokenizer files exist in a directory
96
+ *
97
+ * @param model_dir Path to the model directory
98
+ * @param out_has_vocab Output: true if vocab.json exists
99
+ * @param out_has_merges Output: true if merges.txt exists
100
+ * @return RAC_SUCCESS or error code
101
+ */
102
+ RAC_API rac_result_t rac_diffusion_tokenizer_check_files(const char* model_dir,
103
+ rac_bool_t* out_has_vocab,
104
+ rac_bool_t* out_has_merges);
105
+
106
+ /**
107
+ * @brief Ensure tokenizer files exist, downloading if necessary
108
+ *
109
+ * Checks for vocab.json and merges.txt in the model directory.
110
+ * If missing and auto_download is enabled, downloads from the configured source.
111
+ *
112
+ * @param model_dir Path to the model directory
113
+ * @param config Tokenizer configuration (source, custom URL, auto_download)
114
+ * @return RAC_SUCCESS if files exist or were downloaded successfully
115
+ * RAC_ERROR_FILE_NOT_FOUND if files missing and auto_download disabled
116
+ * RAC_ERROR_NETWORK if download failed
117
+ *
118
+ * Example:
119
+ * @code
120
+ * rac_diffusion_tokenizer_config_t config = {
121
+ * .source = RAC_DIFFUSION_TOKENIZER_SD_1_5,
122
+ * .custom_base_url = NULL,
123
+ * .auto_download = RAC_TRUE
124
+ * };
125
+ * rac_result_t result = rac_diffusion_tokenizer_ensure_files("/path/to/model", &config);
126
+ * @endcode
127
+ */
128
+ RAC_API rac_result_t
129
+ rac_diffusion_tokenizer_ensure_files(const char* model_dir,
130
+ const rac_diffusion_tokenizer_config_t* config);
131
+
132
+ /**
133
+ * @brief Download a tokenizer file
134
+ *
135
+ * Downloads a specific tokenizer file from the configured source.
136
+ *
137
+ * @param source Tokenizer source preset
138
+ * @param custom_url Custom URL (only used when source == RAC_DIFFUSION_TOKENIZER_CUSTOM)
139
+ * @param filename File name to download (e.g., "vocab.json")
140
+ * @param output_path Full path where the file should be saved
141
+ * @return RAC_SUCCESS or error code
142
+ */
143
+ RAC_API rac_result_t rac_diffusion_tokenizer_download_file(rac_diffusion_tokenizer_source_t source,
144
+ const char* custom_url,
145
+ const char* filename,
146
+ const char* output_path);
147
+
148
+ // =============================================================================
149
+ // DEFAULT TOKENIZER SOURCE FOR MODEL VARIANT
150
+ // =============================================================================
151
+
152
+ /**
153
+ * @brief Get the default tokenizer source for a model variant
154
+ *
155
+ * Returns the recommended tokenizer source for a given model variant.
156
+ *
157
+ * @param model_variant Model variant (SD 1.5, SD 2.1, SDXL, etc.)
158
+ * @return Default tokenizer source
159
+ */
160
+ RAC_API rac_diffusion_tokenizer_source_t
161
+ rac_diffusion_tokenizer_default_for_variant(rac_diffusion_model_variant_t model_variant);
162
+
163
+ #ifdef __cplusplus
164
+ }
165
+ #endif
166
+
167
+ #endif /* RAC_DIFFUSION_TOKENIZER_H */