Skip to main content

foundation_models/ffi/
mod.rs

1//! Raw FFI declarations matching the Swift `@_cdecl` exports in
2//! `swift-bridge/Sources/FoundationModelsBridge`.
3//!
4//! These are intentionally opaque (`*mut c_void`) and unsafe; safe wrappers
5//! live in the parent modules.
6
7use core::ffi::{c_char, c_void};
8
9/// Plain-old-data carrier for [`crate::generation::GenerationOptions`].
10#[derive(Copy, Clone, Debug)]
11pub struct FFIGenerationOptions {
12    pub temperature: f64,
13    pub maximum_response_tokens: i32,
14    pub sampling_mode: i32,
15    pub top_k: i32,
16    pub top_p: f64,
17    pub random_seed: u64,
18    pub has_random_seed: bool,
19}
20
21pub type FmRespondCallback = unsafe extern "C" fn(
22    context: *mut c_void,
23    response: *mut c_char,
24    error: *mut c_char,
25    status: i32,
26);
27
28pub type FmStreamCallback =
29    unsafe extern "C" fn(context: *mut c_void, chunk: *mut c_char, done: bool, status: i32);
30
31pub type FmToolCallback = unsafe extern "C" fn(
32    context: *mut c_void,
33    tool_name: *const c_char,
34    arguments_json: *const c_char,
35    output_json_out: *mut *mut c_char,
36    error_out: *mut *mut c_char,
37) -> i32;
38
39extern "C" {
40    pub fn fm_string_dup(s: *const c_char) -> *mut c_char;
41    pub fn fm_string_free(s: *mut c_char);
42    pub fn fm_bytes_free(ptr: *mut c_void);
43    pub fn fm_object_release(ptr: *mut c_void);
44
45    pub fn fm_system_model_is_available() -> bool;
46    pub fn fm_system_model_availability_code() -> i32;
47    pub fn fm_system_model_create_default() -> *mut c_void;
48    pub fn fm_system_model_create(
49        use_case: i32,
50        guardrails: i32,
51        error_out: *mut *mut c_char,
52    ) -> *mut c_void;
53    pub fn fm_system_model_create_with_adapter(
54        adapter: *mut c_void,
55        guardrails: i32,
56        error_out: *mut *mut c_char,
57    ) -> *mut c_void;
58    pub fn fm_system_model_availability_code_for(model: *mut c_void) -> i32;
59    pub fn fm_system_model_supported_languages_json(model: *mut c_void) -> *mut c_char;
60    pub fn fm_system_model_supports_locale(
61        model: *mut c_void,
62        locale_identifier: *const c_char,
63    ) -> bool;
64
65    pub fn fm_adapter_create_from_file(
66        file_path: *const c_char,
67        error_out: *mut *mut c_char,
68    ) -> *mut c_void;
69    pub fn fm_adapter_create_from_name(
70        name: *const c_char,
71        error_out: *mut *mut c_char,
72    ) -> *mut c_void;
73    pub fn fm_adapter_compile(
74        adapter: *mut c_void,
75        context: *mut c_void,
76        callback: FmRespondCallback,
77    );
78    pub fn fm_adapter_compatible_identifiers_json(name: *const c_char) -> *mut c_char;
79    pub fn fm_adapter_remove_obsolete(error_out: *mut *mut c_char) -> i32;
80    pub fn fm_adapter_metadata_json(adapter: *mut c_void) -> *mut c_char;
81
82    pub fn fm_session_create(instructions: *const c_char) -> *mut c_void;
83    pub fn fm_session_create_ex(
84        model: *mut c_void,
85        instructions_json: *const c_char,
86        transcript_json: *const c_char,
87        tools_json: *const c_char,
88        tool_context: *mut c_void,
89        tool_callback: Option<FmToolCallback>,
90        error_out: *mut *mut c_char,
91    ) -> *mut c_void;
92
93    pub fn fm_session_respond(
94        session: *mut c_void,
95        prompt: *const c_char,
96        temperature: f64,
97        max_tokens: i32,
98        sampling_mode: i32,
99        top_k: i32,
100        top_p: f64,
101        context: *mut c_void,
102        callback: FmRespondCallback,
103    );
104    pub fn fm_session_respond_request_json(
105        session: *mut c_void,
106        request_json: *const c_char,
107        context: *mut c_void,
108        callback: FmRespondCallback,
109    );
110
111    pub fn fm_session_respond_with_schema(
112        session: *mut c_void,
113        prompt: *const c_char,
114        schema_json: *const c_char,
115        include_schema_in_prompt: bool,
116        temperature: f64,
117        max_tokens: i32,
118        sampling_mode: i32,
119        top_k: i32,
120        top_p: f64,
121        context: *mut c_void,
122        callback: FmRespondCallback,
123    );
124
125    pub fn fm_session_stream_response(
126        session: *mut c_void,
127        prompt: *const c_char,
128        temperature: f64,
129        max_tokens: i32,
130        sampling_mode: i32,
131        top_k: i32,
132        top_p: f64,
133        context: *mut c_void,
134        callback: FmStreamCallback,
135    );
136    pub fn fm_session_stream_request_json(
137        session: *mut c_void,
138        request_json: *const c_char,
139        context: *mut c_void,
140        callback: FmStreamCallback,
141    );
142
143    pub fn fm_session_prewarm(session: *mut c_void);
144    pub fn fm_session_prewarm_prompt_json(
145        session: *mut c_void,
146        prompt_json: *const c_char,
147        error_out: *mut *mut c_char,
148    ) -> i32;
149    pub fn fm_session_is_responding(session: *mut c_void) -> bool;
150    pub fn fm_session_transcript_json(session: *mut c_void) -> *mut c_char;
151    pub fn fm_session_log_feedback(
152        session: *mut c_void,
153        sentiment: i32,
154        description: *const c_char,
155    );
156    pub fn fm_session_log_feedback_attachment_json(
157        session: *mut c_void,
158        request_json: *const c_char,
159        length_out: *mut usize,
160        error_out: *mut *mut c_char,
161    ) -> *mut c_void;
162
163    pub fn fm_generation_schema_compile_json(
164        request_json: *const c_char,
165        context: *mut c_void,
166        callback: FmRespondCallback,
167    );
168    pub fn fm_generation_schema_validate_json(
169        schema_json: *const c_char,
170        error_out: *mut *mut c_char,
171    ) -> i32;
172}
173
174/// Status codes mirrored 1:1 from the `FM_*` constants in Swift.
175pub mod status {
176    pub const OK: i32 = 0;
177    pub const INVALID_ARGUMENT: i32 = -1;
178    pub const MODEL_UNAVAILABLE: i32 = -2;
179    pub const CANCELLED: i32 = -3;
180    pub const GUARDRAIL_VIOLATION: i32 = -4;
181    pub const CONTEXT_WINDOW_EXCEEDED: i32 = -5;
182    pub const UNSUPPORTED_LANGUAGE: i32 = -6;
183    pub const ASSETS_UNAVAILABLE: i32 = -7;
184    pub const RATE_LIMITED: i32 = -8;
185    pub const DECODING_FAILURE: i32 = -9;
186    pub const REFUSAL: i32 = -10;
187    pub const CONCURRENT_REQUESTS: i32 = -11;
188    pub const UNSUPPORTED_GUIDE: i32 = -12;
189    pub const TOOL_CALL_FAILED: i32 = -13;
190    pub const ADAPTER_INVALID_ASSET: i32 = -14;
191    pub const ADAPTER_INVALID_NAME: i32 = -15;
192    pub const ADAPTER_COMPATIBLE_NOT_FOUND: i32 = -16;
193    pub const UNKNOWN: i32 = -99;
194}