fm-bindings 0.1.5

Rust bindings for Apple's Foundation Models framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// Language Model Session - the main API for Foundation Models

use crate::error::{Error, Result};
use crate::ffi;
use std::ffi::CString;
use std::ptr::NonNull;
use std::sync::{Arc, Condvar, Mutex};

/// A session for interacting with Apple's Foundation Models
///
/// This provides access to on-device language models via the FoundationModels framework.
/// Requires macOS 26+ or iOS 26+ with Apple Intelligence enabled.
///
/// # Session State
///
/// Each session maintains a transcript of all interactions (prompts, responses, etc.).
/// The transcript can be serialized to JSON for persistence and used to restore
/// sessions across app launches.
///
/// # Creating Sessions
///
/// - [`LanguageModelSession::new()`] - Create without instructions
/// - [`LanguageModelSession::with_instructions()`] - Create with system prompt
/// - [`LanguageModelSession::from_transcript_json()`] - Restore from saved transcript
///
/// # Getting Responses
///
/// - [`response()`](Self::response) - Blocking response (waits for completion)
/// - [`stream_response()`](Self::stream_response) - Streaming response (real-time chunks)
/// - [`cancel_stream()`](Self::cancel_stream) - Cancel ongoing stream
///
/// # Examples
///
/// See the method-level documentation for detailed examples:
/// - [`new()`](Self::new) and [`response()`](Self::response) for basic usage
/// - [`stream_response()`](Self::stream_response) for streaming
/// - [`transcript_json()`](Self::transcript_json) and [`from_transcript_json()`](Self::from_transcript_json) for persistence
pub struct LanguageModelSession {
    ptr: NonNull<std::ffi::c_void>,
}

// Safety: The Swift LanguageModelSession is thread-safe (@unchecked Sendable)
unsafe impl Send for LanguageModelSession {}
unsafe impl Sync for LanguageModelSession {}

impl LanguageModelSession {
    /// Creates a new language model session without instructions
    ///
    /// This is equivalent to calling `with_instructions(None)`.
    ///
    /// # Errors
    ///
    /// Returns `Error::ModelNotAvailable` if Apple Intelligence is not enabled
    /// or the system model is unavailable.
    pub fn new() -> Result<Self> {
        Self::with_instructions_opt(None)
    }

    /// Creates a new language model session with instructions
    ///
    /// Instructions define the model's persona, behavior, and guidelines for the
    /// entire session. They are always the first entry in the session transcript.
    ///
    /// # Arguments
    ///
    /// * `instructions` - System prompt that guides the model's behavior
    ///
    /// # Errors
    ///
    /// * `Error::ModelNotAvailable` - If Apple Intelligence is not enabled
    /// * `Error::InvalidInput` - If instructions contain a null byte
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use fm_bindings::LanguageModelSession;
    /// let session = LanguageModelSession::with_instructions(
    ///     "You are a helpful coding assistant. Provide concise answers."
    /// )?;
    /// # Ok::<(), fm_bindings::Error>(())
    /// ```
    pub fn with_instructions(instructions: &str) -> Result<Self> {
        Self::with_instructions_opt(Some(instructions))
    }

    /// Creates a new language model session with optional instructions
    ///
    /// # Arguments
    ///
    /// * `instructions` - Optional system prompt, or `None` for no instructions
    ///
    /// # Errors
    ///
    /// * `Error::ModelNotAvailable` - If Apple Intelligence is not enabled
    /// * `Error::InvalidInput` - If instructions contain a null byte
    fn with_instructions_opt(instructions: Option<&str>) -> Result<Self> {
        if !unsafe { ffi::fm_check_availability() } {
            return Err(Error::ModelNotAvailable);
        }

        let c_instructions = match instructions {
            Some(s) => Some(
                CString::new(s)
                    .map_err(|_| Error::InvalidInput("Instructions contain null byte".into()))?,
            ),
            None => None,
        };

        let ptr = unsafe {
            ffi::fm_create_session(
                c_instructions
                    .as_ref()
                    .map_or(std::ptr::null(), |s| s.as_ptr()),
            )
        };

        NonNull::new(ptr)
            .map(|ptr| Self { ptr })
            .ok_or_else(|| Error::InternalError("Failed to create session".into()))
    }

    /// Creates a session from a serialized transcript JSON
    ///
    /// This restores a previous session state, including the original instructions
    /// and full conversation history. Use this to resume conversations across
    /// app launches.
    ///
    /// # Arguments
    ///
    /// * `transcript_json` - JSON string from `transcript_json()`
    ///
    /// # Errors
    ///
    /// * `Error::ModelNotAvailable` - If Apple Intelligence is not enabled
    /// * `Error::InvalidInput` - If JSON contains a null byte or is invalid
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use fm_bindings::LanguageModelSession;
    /// let json = std::fs::read_to_string("session.json")?;
    /// let session = LanguageModelSession::from_transcript_json(&json)?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn from_transcript_json(transcript_json: &str) -> Result<Self> {
        if !unsafe { ffi::fm_check_availability() } {
            return Err(Error::ModelNotAvailable);
        }

        let c_json = CString::new(transcript_json)
            .map_err(|_| Error::InvalidInput("Transcript JSON contains null byte".into()))?;

        let ptr = unsafe { ffi::fm_create_session_from_transcript(c_json.as_ptr()) };

        NonNull::new(ptr)
            .map(|ptr| Self { ptr })
            .ok_or_else(|| Error::InternalError("Failed to restore session from transcript".into()))
    }

    /// Gets the current session transcript as JSON
    ///
    /// The returned JSON can be persisted and later passed to `from_transcript_json()`
    /// to restore the session state.
    ///
    /// # Returns
    ///
    /// JSON string representing the full transcript (instructions, prompts, responses)
    ///
    /// # Errors
    ///
    /// * `Error::InternalError` - If transcript serialization fails
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use fm_bindings::LanguageModelSession;
    /// let session = LanguageModelSession::new()?;
    /// let _ = session.response("Hello")?;
    ///
    /// let json = session.transcript_json()?;
    /// std::fs::write("session.json", &json)?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn transcript_json(&self) -> Result<String> {
        let json_ptr = unsafe { ffi::fm_get_transcript_json(self.ptr.as_ptr()) };

        if json_ptr.is_null() {
            // Empty transcript is valid
            return Ok("[]".to_string());
        }

        let json = unsafe {
            let s = std::ffi::CStr::from_ptr(json_ptr)
                .to_string_lossy()
                .into_owned();
            ffi::fm_free_string(json_ptr);
            s
        };

        Ok(json)
    }

    /// Generates a complete response to the given prompt
    ///
    /// This method blocks until the entire response is generated and returned as a String.
    /// The prompt and response are added to the session transcript.
    ///
    /// For a better user experience with incremental updates, use `stream_response` instead.
    ///
    /// # Arguments
    ///
    /// * `prompt` - The input text to send to the model
    ///
    /// # Errors
    ///
    /// * `Error::InvalidInput` - If the prompt is empty or contains a null byte
    /// * `Error::GenerationError` - If an error occurs during generation
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use fm_bindings::LanguageModelSession;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let session = LanguageModelSession::new()?;
    /// let response = session.response("Explain Rust ownership")?;
    /// println!("Response: {}", response);
    /// # Ok(())
    /// # }
    /// ```
    pub fn response(&self, prompt: &str) -> Result<String> {
        if prompt.is_empty() {
            return Err(Error::InvalidInput("Prompt cannot be empty".into()));
        }

        let c_prompt = CString::new(prompt)
            .map_err(|_| Error::InvalidInput("Prompt contains null byte".into()))?;

        // Shared state for collecting response
        let state = Arc::new((Mutex::new(ResponseState::default()), Condvar::new()));
        let state_ptr = Box::into_raw(Box::new(Arc::clone(&state)));

        unsafe {
            ffi::fm_session_response(
                self.ptr.as_ptr(),
                c_prompt.as_ptr(),
                state_ptr as *mut _,
                response_chunk_callback,
                response_done_callback,
                response_error_callback,
            );
        }

        // Wait for completion
        let (mutex, cvar) = &*state;
        let mut response_state = mutex.lock().map_err(|_| Error::PoisonError)?;
        while !response_state.finished {
            response_state = cvar.wait(response_state).map_err(|_| Error::PoisonError)?;
        }

        // Check for errors
        if let Some(error) = &response_state.error {
            if error.contains("not available") {
                return Err(Error::ModelNotAvailable);
            }
            return Err(Error::GenerationError(error.clone()));
        }

        Ok(response_state.text.clone())
    }

    /// Generates a streaming response to the given prompt
    ///
    /// This method calls the provided callback for each chunk as it's generated,
    /// providing immediate feedback to the user. The prompt and complete response
    /// are added to the session transcript.
    ///
    /// # Arguments
    ///
    /// * `prompt` - The input text to send to the model
    /// * `on_chunk` - Callback function called for each generated chunk
    ///
    /// # Errors
    ///
    /// * `Error::InvalidInput` - If the prompt is empty or contains a null byte
    /// * `Error::GenerationError` - If an error occurs during generation
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use fm_bindings::LanguageModelSession;
    /// # use std::io::{self, Write};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let session = LanguageModelSession::new()?;
    ///
    /// session.stream_response("Tell me a story", |chunk| {
    ///     print!("{}", chunk);
    ///     let _ = io::stdout().flush();
    /// })?;
    ///
    /// println!(); // newline after stream completes
    /// # Ok(())
    /// # }
    /// ```
    pub fn stream_response<F>(&self, prompt: &str, on_chunk: F) -> Result<()>
    where
        F: FnMut(&str),
    {
        if prompt.is_empty() {
            return Err(Error::InvalidInput("Prompt cannot be empty".into()));
        }

        let c_prompt = CString::new(prompt)
            .map_err(|_| Error::InvalidInput("Prompt contains null byte".into()))?;

        // Shared state for streaming
        let state = Arc::new((Mutex::new(StreamState::default()), Condvar::new()));
        let user_data = Box::into_raw(Box::new((
            Arc::clone(&state),
            Box::new(on_chunk) as Box<dyn FnMut(&str)>,
        )));

        unsafe {
            ffi::fm_session_stream(
                self.ptr.as_ptr(),
                c_prompt.as_ptr(),
                user_data as *mut _,
                stream_chunk_callback,
                stream_done_callback,
                stream_error_callback,
            );
        }

        // Wait for completion
        let (mutex, cvar) = &*state;
        let mut stream_state = mutex.lock().map_err(|_| Error::PoisonError)?;
        while !stream_state.finished {
            stream_state = cvar.wait(stream_state).map_err(|_| Error::PoisonError)?;
        }

        // Check for errors
        if let Some(error) = &stream_state.error {
            if error.contains("not available") {
                return Err(Error::ModelNotAvailable);
            }
            return Err(Error::GenerationError(error.clone()));
        }

        Ok(())
    }

    /// Cancels the current streaming response
    ///
    /// This method immediately cancels any ongoing streaming operation.
    /// The streaming callback will stop receiving tokens and the stream
    /// will complete with the tokens received so far.
    ///
    /// # Notes
    ///
    /// * Safe to call even if no stream is active
    /// * After cancellation, `stream_response` will return normally
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use fm_bindings::LanguageModelSession;
    /// # use std::sync::Arc;
    /// # use std::thread;
    /// # use std::time::Duration;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let session = Arc::new(LanguageModelSession::new()?);
    /// let session_clone = Arc::clone(&session);
    ///
    /// // Start streaming in a thread
    /// let handle = thread::spawn(move || {
    ///     session_clone.stream_response("Write a long essay...", |chunk| {
    ///         print!("{}", chunk);
    ///     })
    /// });
    ///
    /// // Cancel after a delay
    /// thread::sleep(Duration::from_secs(2));
    /// session.cancel_stream();
    ///
    /// handle.join().unwrap()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cancel_stream(&self) {
        unsafe {
            ffi::fm_session_cancel_stream(self.ptr.as_ptr());
        }
    }
}

impl Drop for LanguageModelSession {
    fn drop(&mut self) {
        unsafe {
            ffi::fm_destroy_session(self.ptr.as_ptr());
        }
    }
}

// Note: We intentionally don't implement Clone.
// To create a session with the same transcript, use:
//   let json = session.transcript_json()?;
//   let new_session = LanguageModelSession::from_transcript_json(&json)?;

// =============================================================================
// Internal State Types
// =============================================================================

#[derive(Default)]
struct ResponseState {
    text: String,
    finished: bool,
    error: Option<String>,
}

#[derive(Default)]
struct StreamState {
    finished: bool,
    error: Option<String>,
}

// =============================================================================
// C Callbacks for response()
// =============================================================================

extern "C" fn response_chunk_callback(
    chunk: *const std::os::raw::c_char,
    user_data: *mut std::os::raw::c_void,
) {
    if chunk.is_null() || user_data.is_null() {
        return;
    }

    unsafe {
        let state = &*(user_data as *const Arc<(Mutex<ResponseState>, Condvar)>);
        let chunk_str = std::ffi::CStr::from_ptr(chunk).to_string_lossy();

        let (mutex, _) = &**state;
        if let Ok(mut response_state) = mutex.lock() {
            response_state.text.push_str(&chunk_str);
        }
    }
}

extern "C" fn response_done_callback(user_data: *mut std::os::raw::c_void) {
    if user_data.is_null() {
        return;
    }

    unsafe {
        // Take ownership back from the raw pointer
        let state = Box::from_raw(user_data as *mut Arc<(Mutex<ResponseState>, Condvar)>);

        let (mutex, cvar) = &**state;
        if let Ok(mut response_state) = mutex.lock() {
            response_state.finished = true;
            cvar.notify_all();
        }
    }
}

extern "C" fn response_error_callback(
    error: *const std::os::raw::c_char,
    user_data: *mut std::os::raw::c_void,
) {
    if user_data.is_null() {
        return;
    }

    unsafe {
        // Take ownership back from the raw pointer
        let state = Box::from_raw(user_data as *mut Arc<(Mutex<ResponseState>, Condvar)>);

        let (mutex, cvar) = &**state;
        if let Ok(mut response_state) = mutex.lock() {
            if !error.is_null() {
                let error_str = std::ffi::CStr::from_ptr(error)
                    .to_string_lossy()
                    .into_owned();
                response_state.error = Some(error_str);
            }
            response_state.finished = true;
            cvar.notify_all();
        }
    }
}

// =============================================================================
// C Callbacks for stream_response()
// =============================================================================

type StreamCallback = Box<dyn FnMut(&str)>;
type StreamUserData = (Arc<(Mutex<StreamState>, Condvar)>, StreamCallback);

extern "C" fn stream_chunk_callback(
    chunk: *const std::os::raw::c_char,
    user_data: *mut std::os::raw::c_void,
) {
    if chunk.is_null() || user_data.is_null() {
        return;
    }

    unsafe {
        let data = &mut *(user_data as *mut StreamUserData);
        let chunk_str = std::ffi::CStr::from_ptr(chunk).to_string_lossy();
        (data.1)(&chunk_str);
    }
}

extern "C" fn stream_done_callback(user_data: *mut std::os::raw::c_void) {
    if user_data.is_null() {
        return;
    }

    unsafe {
        // Take ownership back from the raw pointer
        let data = Box::from_raw(user_data as *mut StreamUserData);

        let (mutex, cvar) = &*data.0;
        if let Ok(mut stream_state) = mutex.lock() {
            stream_state.finished = true;
            cvar.notify_all();
        }
    }
}

extern "C" fn stream_error_callback(
    error: *const std::os::raw::c_char,
    user_data: *mut std::os::raw::c_void,
) {
    if user_data.is_null() {
        return;
    }

    unsafe {
        // Take ownership back from the raw pointer
        let data = Box::from_raw(user_data as *mut StreamUserData);

        let (mutex, cvar) = &*data.0;
        if let Ok(mut stream_state) = mutex.lock() {
            if !error.is_null() {
                let error_str = std::ffi::CStr::from_ptr(error)
                    .to_string_lossy()
                    .into_owned();
                stream_state.error = Some(error_str);
            }
            stream_state.finished = true;
            cvar.notify_all();
        }
    }
}