@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,373 @@
1
+ /**
2
+ * @file rac_tool_calling.h
3
+ * @brief RunAnywhere Commons - Tool Calling API
4
+ *
5
+ * *** SINGLE SOURCE OF TRUTH FOR ALL TOOL CALLING LOGIC ***
6
+ *
7
+ * This header provides ALL tool calling functionality. Platform SDKs should
8
+ * ONLY call these functions - no fallback implementations allowed.
9
+ *
10
+ * Architecture:
11
+ * - C++ handles: ALL parsing, prompt formatting, JSON handling, follow-up prompts
12
+ * - Platform SDKs handle ONLY: tool registry (closures), tool execution (needs platform APIs)
13
+ *
14
+ * Supported Tool Calling Formats:
15
+ * - DEFAULT: <tool_call>{"tool":"name","arguments":{}}</tool_call> (Most general models)
16
+ * - LFM2: <|tool_call_start|>[func(arg="val")]<|tool_call_end|> (Liquid AI models)
17
+ *
18
+ * Ported from:
19
+ * - Swift: ToolCallParser.swift
20
+ * - React Native: ToolCallingBridge.cpp
21
+ */
22
+
23
+ #ifndef RAC_TOOL_CALLING_H
24
+ #define RAC_TOOL_CALLING_H
25
+
26
+ #include "rac/core/rac_error.h"
27
+ #include "rac/core/rac_types.h"
28
+
29
+ #ifdef __cplusplus
30
+ extern "C" {
31
+ #endif
32
+
33
+ // =============================================================================
34
+ // TOOL CALLING FORMATS - Different models use different formats
35
+ // =============================================================================
36
+
37
+ /**
38
+ * @brief Tool calling format identifiers
39
+ *
40
+ * Different LLM models use different tool calling formats. This enum allows
41
+ * specifying which format to use for parsing and prompt generation.
42
+ */
43
+ typedef enum rac_tool_call_format {
44
+ /**
45
+ * @brief SDK Default format: <tool_call>JSON</tool_call>
46
+ *
47
+ * Format: <tool_call>{"tool": "name", "arguments": {...}}</tool_call>
48
+ * Used by: Most general-purpose models (Llama, Qwen, Mistral, etc.)
49
+ */
50
+ RAC_TOOL_FORMAT_DEFAULT = 0,
51
+
52
+ /**
53
+ * @brief Liquid AI LFM2-Tool format
54
+ *
55
+ * Format: <|tool_call_start|>[func_name(arg1="val1", arg2="val2")]<|tool_call_end|>
56
+ * Used by: LiquidAI/LFM2-1.2B-Tool, LiquidAI/LFM2-350M-Tool
57
+ * Note: Uses Pythonic function call syntax
58
+ */
59
+ RAC_TOOL_FORMAT_LFM2 = 1,
60
+
61
+ /** Number of formats (for iteration) */
62
+ RAC_TOOL_FORMAT_COUNT
63
+ } rac_tool_call_format_t;
64
+
65
+ // =============================================================================
66
+ // TYPES - Canonical definitions used by all SDKs
67
+ // =============================================================================
68
+
69
+ /**
70
+ * @brief Parameter types for tool arguments
71
+ */
72
+ typedef enum rac_tool_param_type {
73
+ RAC_TOOL_PARAM_STRING = 0,
74
+ RAC_TOOL_PARAM_NUMBER = 1,
75
+ RAC_TOOL_PARAM_BOOLEAN = 2,
76
+ RAC_TOOL_PARAM_OBJECT = 3,
77
+ RAC_TOOL_PARAM_ARRAY = 4
78
+ } rac_tool_param_type_t;
79
+
80
+ /**
81
+ * @brief Tool parameter definition
82
+ */
83
+ typedef struct rac_tool_parameter {
84
+ const char* name; /**< Parameter name */
85
+ rac_tool_param_type_t type; /**< Data type */
86
+ const char* description; /**< Human-readable description */
87
+ rac_bool_t required; /**< Whether required */
88
+ const char* enum_values; /**< JSON array of allowed values (can be NULL) */
89
+ } rac_tool_parameter_t;
90
+
91
+ /**
92
+ * @brief Tool definition
93
+ */
94
+ typedef struct rac_tool_definition {
95
+ const char* name; /**< Unique tool name (e.g., "get_weather") */
96
+ const char* description; /**< What the tool does */
97
+ const rac_tool_parameter_t* parameters; /**< Array of parameters */
98
+ size_t num_parameters; /**< Number of parameters */
99
+ const char* category; /**< Optional category (can be NULL) */
100
+ } rac_tool_definition_t;
101
+
102
+ /**
103
+ * @brief Parsed tool call from LLM output
104
+ */
105
+ typedef struct rac_tool_call {
106
+ rac_bool_t has_tool_call; /**< Whether a tool call was found */
107
+ char* tool_name; /**< Name of tool to execute (owned, must free) */
108
+ char* arguments_json; /**< Arguments as JSON string (owned, must free) */
109
+ char* clean_text; /**< Text without tool call tags (owned, must free) */
110
+ int64_t call_id; /**< Unique call ID for tracking */
111
+ rac_tool_call_format_t format; /**< Format that was detected/used for parsing */
112
+ } rac_tool_call_t;
113
+
114
+ /**
115
+ * @brief Tool calling options
116
+ */
117
+ typedef struct rac_tool_calling_options {
118
+ int32_t max_tool_calls; /**< Max tool calls per turn (default: 5) */
119
+ rac_bool_t auto_execute; /**< Auto-execute tools (default: true) */
120
+ float temperature; /**< Generation temperature */
121
+ int32_t max_tokens; /**< Max tokens to generate */
122
+ const char* system_prompt; /**< Optional system prompt */
123
+ rac_bool_t replace_system_prompt; /**< Replace vs append tool instructions */
124
+ rac_bool_t keep_tools_available; /**< Keep tools after first call */
125
+ rac_tool_call_format_t format; /**< Tool calling format (default: AUTO) */
126
+ } rac_tool_calling_options_t;
127
+
128
+ /**
129
+ * @brief Default tool calling options
130
+ */
131
+ #define RAC_TOOL_CALLING_OPTIONS_DEFAULT \
132
+ { \
133
+ 5, /* max_tool_calls */ \
134
+ 1, /* auto_execute = true */ \
135
+ 0.7f, /* temperature */ \
136
+ 1024, /* max_tokens */ \
137
+ RAC_NULL, /* system_prompt */ \
138
+ 0, /* replace_system_prompt = false */ \
139
+ 0, /* keep_tools_available = false */ \
140
+ RAC_TOOL_FORMAT_DEFAULT /* format */ \
141
+ }
142
+
143
+ // =============================================================================
144
+ // PARSING API - Single Source of Truth (NO FALLBACKS)
145
+ // =============================================================================
146
+
147
+ /**
148
+ * @brief Parse LLM output for tool calls (auto-detect format)
149
+ *
150
+ * *** THIS IS THE ONLY PARSING IMPLEMENTATION - ALL SDKS MUST USE THIS ***
151
+ *
152
+ * Auto-detects the tool calling format by checking for format-specific tags.
153
+ * Handles ALL edge cases for each format.
154
+ *
155
+ * @param llm_output Raw LLM output text
156
+ * @param out_result Output: Parsed result (caller must free with rac_tool_call_free)
157
+ * @return RAC_SUCCESS on success, error code otherwise
158
+ */
159
+ RAC_API rac_result_t rac_tool_call_parse(const char* llm_output, rac_tool_call_t* out_result);
160
+
161
+ /**
162
+ * @brief Parse LLM output for tool calls with specified format
163
+ *
164
+ * Parses using a specific format.
165
+ *
166
+ * Supported formats:
167
+ * - RAC_TOOL_FORMAT_DEFAULT: <tool_call>JSON</tool_call>
168
+ * - RAC_TOOL_FORMAT_LFM2: <|tool_call_start|>[func(args)]<|tool_call_end|>
169
+ *
170
+ * @param llm_output Raw LLM output text
171
+ * @param format Tool calling format to use
172
+ * @param out_result Output: Parsed result (caller must free with rac_tool_call_free)
173
+ * @return RAC_SUCCESS on success, error code otherwise
174
+ */
175
+ RAC_API rac_result_t rac_tool_call_parse_with_format(const char* llm_output,
176
+ rac_tool_call_format_t format,
177
+ rac_tool_call_t* out_result);
178
+
179
+ /**
180
+ * @brief Free tool call result
181
+ * @param result Result to free
182
+ */
183
+ RAC_API void rac_tool_call_free(rac_tool_call_t* result);
184
+
185
+ /**
186
+ * @brief Get the human-readable name of a tool calling format
187
+ *
188
+ * @param format The format to get the name for
189
+ * @return Static string with the format name (do not free)
190
+ */
191
+ RAC_API const char* rac_tool_call_format_name(rac_tool_call_format_t format);
192
+
193
+ /**
194
+ * @brief Detect which format is present in LLM output
195
+ *
196
+ * Checks for format-specific markers without fully parsing.
197
+ * Returns RAC_TOOL_FORMAT_DEFAULT if no recognizable format is found.
198
+ *
199
+ * @param llm_output Raw LLM output text
200
+ * @return Detected format, or RAC_TOOL_FORMAT_DEFAULT if none detected
201
+ */
202
+ RAC_API rac_tool_call_format_t rac_tool_call_detect_format(const char* llm_output);
203
+
204
+ /**
205
+ * @brief Convert format name string to format enum
206
+ *
207
+ * This is the SINGLE SOURCE OF TRUTH for valid format names.
208
+ * SDKs should pass strings and let C++ handle the conversion.
209
+ *
210
+ * Valid names (case-insensitive): "default", "lfm2"
211
+ *
212
+ * @param name Format name string
213
+ * @return Corresponding format enum, or RAC_TOOL_FORMAT_DEFAULT if unknown
214
+ */
215
+ RAC_API rac_tool_call_format_t rac_tool_call_format_from_name(const char* name);
216
+
217
+ // =============================================================================
218
+ // PROMPT FORMATTING API - All prompt building happens here
219
+ // =============================================================================
220
+
221
+ /**
222
+ * @brief Format tool definitions into system prompt (default format)
223
+ *
224
+ * Creates instruction text describing available tools and expected output format.
225
+ * Uses RAC_TOOL_FORMAT_DEFAULT (<tool_call>JSON</tool_call>).
226
+ *
227
+ * @param definitions Array of tool definitions
228
+ * @param num_definitions Number of definitions
229
+ * @param out_prompt Output: Allocated prompt string (caller must free with rac_free)
230
+ * @return RAC_SUCCESS on success, error code otherwise
231
+ */
232
+ RAC_API rac_result_t rac_tool_call_format_prompt(const rac_tool_definition_t* definitions,
233
+ size_t num_definitions, char** out_prompt);
234
+
235
+ /**
236
+ * @brief Format tool definitions with specified format
237
+ *
238
+ * Creates instruction text using the specified tool calling format.
239
+ * Each format has different tag patterns and syntax instructions.
240
+ *
241
+ * @param definitions Array of tool definitions
242
+ * @param num_definitions Number of definitions
243
+ * @param format Tool calling format to use for instructions
244
+ * @param out_prompt Output: Allocated prompt string (caller must free with rac_free)
245
+ * @return RAC_SUCCESS on success, error code otherwise
246
+ */
247
+ RAC_API rac_result_t rac_tool_call_format_prompt_with_format(const rac_tool_definition_t* definitions,
248
+ size_t num_definitions,
249
+ rac_tool_call_format_t format,
250
+ char** out_prompt);
251
+
252
+ /**
253
+ * @brief Format tools from JSON array string (default format)
254
+ *
255
+ * Convenience function when tools are provided as JSON.
256
+ *
257
+ * @param tools_json JSON array of tool definitions
258
+ * @param out_prompt Output: Allocated prompt string (caller must free with rac_free)
259
+ * @return RAC_SUCCESS on success, error code otherwise
260
+ */
261
+ RAC_API rac_result_t rac_tool_call_format_prompt_json(const char* tools_json, char** out_prompt);
262
+
263
+ /**
264
+ * @brief Format tools from JSON array string with specified format
265
+ *
266
+ * @param tools_json JSON array of tool definitions
267
+ * @param format Tool calling format to use for instructions
268
+ * @param out_prompt Output: Allocated prompt string (caller must free with rac_free)
269
+ * @return RAC_SUCCESS on success, error code otherwise
270
+ */
271
+ RAC_API rac_result_t rac_tool_call_format_prompt_json_with_format(const char* tools_json,
272
+ rac_tool_call_format_t format,
273
+ char** out_prompt);
274
+
275
+ /**
276
+ * @brief Format tools from JSON array string with format specified by name
277
+ *
278
+ * *** PREFERRED API FOR SDKS - Uses string format name ***
279
+ *
280
+ * Valid format names (case-insensitive): "default", "lfm2"
281
+ * Unknown names default to "default" format.
282
+ *
283
+ * @param tools_json JSON array of tool definitions
284
+ * @param format_name Format name string (e.g., "lfm2", "default")
285
+ * @param out_prompt Output: Allocated prompt string (caller must free with rac_free)
286
+ * @return RAC_SUCCESS on success, error code otherwise
287
+ */
288
+ RAC_API rac_result_t rac_tool_call_format_prompt_json_with_format_name(const char* tools_json,
289
+ const char* format_name,
290
+ char** out_prompt);
291
+
292
+ /**
293
+ * @brief Build the initial prompt with tools and user query
294
+ *
295
+ * Combines system prompt, tool instructions, and user prompt.
296
+ *
297
+ * @param user_prompt The user's question/request
298
+ * @param tools_json JSON array of tool definitions
299
+ * @param options Tool calling options (can be NULL for defaults)
300
+ * @param out_prompt Output: Complete formatted prompt (caller must free with rac_free)
301
+ * @return RAC_SUCCESS on success, error code otherwise
302
+ */
303
+ RAC_API rac_result_t rac_tool_call_build_initial_prompt(const char* user_prompt,
304
+ const char* tools_json,
305
+ const rac_tool_calling_options_t* options,
306
+ char** out_prompt);
307
+
308
+ /**
309
+ * @brief Build follow-up prompt after tool execution
310
+ *
311
+ * Creates the prompt to continue generation after a tool was executed.
312
+ * Handles both keepToolsAvailable=true and keepToolsAvailable=false cases.
313
+ *
314
+ * @param original_user_prompt The original user prompt
315
+ * @param tools_prompt The formatted tools prompt (can be NULL if not keeping tools)
316
+ * @param tool_name Name of the tool that was executed
317
+ * @param tool_result_json JSON string of the tool result
318
+ * @param keep_tools_available Whether to include tool definitions in follow-up
319
+ * @param out_prompt Output: Follow-up prompt (caller must free with rac_free)
320
+ * @return RAC_SUCCESS on success, error code otherwise
321
+ */
322
+ RAC_API rac_result_t rac_tool_call_build_followup_prompt(const char* original_user_prompt,
323
+ const char* tools_prompt,
324
+ const char* tool_name,
325
+ const char* tool_result_json,
326
+ rac_bool_t keep_tools_available,
327
+ char** out_prompt);
328
+
329
+ // =============================================================================
330
+ // JSON UTILITY API - All JSON handling happens here
331
+ // =============================================================================
332
+
333
+ /**
334
+ * @brief Normalize JSON by adding quotes around unquoted keys
335
+ *
336
+ * Handles common LLM output patterns: {tool: "name"} → {"tool": "name"}
337
+ *
338
+ * @param json_str Input JSON string
339
+ * @param out_normalized Output: Normalized JSON (caller must free with rac_free)
340
+ * @return RAC_SUCCESS on success, error code otherwise
341
+ */
342
+ RAC_API rac_result_t rac_tool_call_normalize_json(const char* json_str, char** out_normalized);
343
+
344
+ /**
345
+ * @brief Serialize tool definitions to JSON array
346
+ *
347
+ * @param definitions Array of tool definitions
348
+ * @param num_definitions Number of definitions
349
+ * @param out_json Output: JSON array string (caller must free with rac_free)
350
+ * @return RAC_SUCCESS on success, error code otherwise
351
+ */
352
+ RAC_API rac_result_t rac_tool_call_definitions_to_json(const rac_tool_definition_t* definitions,
353
+ size_t num_definitions, char** out_json);
354
+
355
+ /**
356
+ * @brief Serialize a tool result to JSON
357
+ *
358
+ * @param tool_name Name of the tool
359
+ * @param success Whether execution succeeded
360
+ * @param result_json Result data as JSON (can be NULL)
361
+ * @param error_message Error message if failed (can be NULL)
362
+ * @param out_json Output: JSON string (caller must free with rac_free)
363
+ * @return RAC_SUCCESS on success, error code otherwise
364
+ */
365
+ RAC_API rac_result_t rac_tool_call_result_to_json(const char* tool_name, rac_bool_t success,
366
+ const char* result_json,
367
+ const char* error_message, char** out_json);
368
+
369
+ #ifdef __cplusplus
370
+ }
371
+ #endif
372
+
373
+ #endif /* RAC_TOOL_CALLING_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 "rac/core/rac_types.h"
18
+ #include "rac/features/diffusion/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 */
@@ -232,7 +232,7 @@ RAC_API rac_result_t rac_energy_vad_process_audio(rac_energy_vad_handle_t handle
232
232
  * @param sample_count Number of samples
233
233
  * @return RMS energy value, or 0.0 if empty
234
234
  */
235
- RAC_API float rac_energy_vad_calculate_rms(const float* audio_data, size_t sample_count);
235
+ RAC_API float rac_energy_vad_calculate_rms(const float* __restrict audio_data,size_t sample_count);
236
236
 
237
237
  // =============================================================================
238
238
  // PAUSE/RESUME API