#![cfg_attr(not(foundation_models_bridge), allow(dead_code, unused_imports))]
use std::pin::Pin;
use std::task::{Context as StdContext, Poll};
use futures_core::Stream;
#[cfg(foundation_models_bridge)]
use std::ffi::{CStr, CString, c_char, c_void};
#[cfg(foundation_models_bridge)]
use std::ptr::null;
#[cfg(foundation_models_bridge)]
use std::sync::Arc;
use futures_channel::mpsc;
#[cfg(foundation_models_bridge)]
use futures_channel::oneshot;
#[cfg(foundation_models_bridge)]
unsafe extern "C" {
fn fm_availability_reason() -> i32;
fn fm_session_create(instructions: *const c_char) -> *mut c_void;
fn fm_session_create_with_tools(
instructions: *const c_char,
tools_json: *const c_char,
tool_ctx: *mut c_void,
tool_dispatch: extern "C" fn(
*mut c_void,
*const c_char,
*const c_char,
*mut c_void,
extern "C" fn(*mut c_void, *const c_char, *const c_char),
),
) -> *mut c_void;
fn fm_session_destroy(handle: *mut c_void);
fn fm_session_respond(
handle: *mut c_void,
prompt: *const c_char,
temperature: f64,
max_tokens: i64,
ctx: *mut c_void,
callback: extern "C" fn(*mut c_void, *const c_char, *const c_char),
);
fn fm_session_respond_structured(
handle: *mut c_void,
prompt: *const c_char,
schema_json: *const c_char,
temperature: f64,
max_tokens: i64,
ctx: *mut c_void,
callback: extern "C" fn(*mut c_void, *const c_char, *const c_char),
);
fn fm_session_stream(
handle: *mut c_void,
prompt: *const c_char,
temperature: f64,
max_tokens: i64,
ctx: *mut c_void,
on_token: extern "C" fn(*mut c_void, *const c_char),
on_done: extern "C" fn(*mut c_void, *const c_char),
);
fn fm_session_update_profile(handle: *mut c_void, instructions: *const c_char);
fn fm_session_respond_with_attachment(
handle: *mut c_void,
prompt: *const c_char,
image_bytes: *const u8,
image_len: usize,
image_mime: *const c_char,
temperature: f64,
max_tokens: i64,
ctx: *mut c_void,
callback: extern "C" fn(*mut c_void, *const c_char, *const c_char),
);
fn fm_session_stream_structured(
handle: *mut c_void,
prompt: *const c_char,
schema_json: *const c_char,
temperature: f64,
max_tokens: i64,
ctx: *mut c_void,
on_token: extern "C" fn(*mut c_void, *const c_char),
on_done: extern "C" fn(*mut c_void, *const c_char),
);
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum UnavailabilityReason {
#[error("device is not eligible (requires Apple Silicon M1 or later)")]
DeviceNotEligible,
#[error("Apple Intelligence is not enabled in System Settings")]
NotEnabled,
#[error("the on-device model is not ready yet")]
ModelNotReady,
#[error("unknown availability state")]
Unknown,
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Apple Intelligence unavailable: {0}")]
Unavailable(#[source] UnavailabilityReason),
#[error("generation error: {0}")]
Generation(String),
#[error("argument contains a null byte: {0}")]
NullByte(#[from] std::ffi::NulError),
#[error("temperature {0} is out of range; expected 0.0 – 2.0")]
InvalidTemperature(f64),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("tool '{name}' failed: {message}")]
ToolError { name: String, message: String },
}
#[derive(Debug, Default, Clone)]
pub struct GenerationOptions {
pub temperature: Option<f64>,
pub max_tokens: Option<usize>,
}
impl GenerationOptions {
pub fn validate(&self) -> Result<(), Error> {
if let Some(t) = self.temperature {
if !(0.0..=2.0).contains(&t) {
return Err(Error::InvalidTemperature(t));
}
}
Ok(())
}
fn ffi_temperature(&self) -> f64 {
self.temperature.unwrap_or(-1.0)
}
fn ffi_max_tokens(&self) -> i64 {
self.max_tokens.map(|n| n as i64).unwrap_or(-1)
}
}
#[derive(Debug, Clone)]
pub struct Attachment {
pub data: Vec<u8>,
pub mime: String,
}
impl Attachment {
pub fn image(data: impl Into<Vec<u8>>, mime: impl Into<String>) -> Self {
Self { data: data.into(), mime: mime.into() }
}
}
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum SchemaPropertyType {
String,
Integer,
Double,
Bool,
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct SchemaProperty {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "type")]
pub property_type: SchemaPropertyType,
#[serde(default)]
pub optional: bool,
}
impl SchemaProperty {
pub fn new(name: impl Into<String>, property_type: SchemaPropertyType) -> Self {
Self {
name: name.into(),
description: None,
property_type,
optional: false,
}
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn optional(mut self) -> Self {
self.optional = true;
self
}
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct Schema {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub properties: Vec<SchemaProperty>,
}
impl Schema {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
description: None,
properties: Vec::new(),
}
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn property(mut self, property: SchemaProperty) -> Self {
self.properties.push(property);
self
}
}
pub struct ToolDefinition {
pub name: String,
pub description: String,
pub parameters: Schema,
pub(crate) handler: Box<dyn Fn(serde_json::Value) -> Result<String, String> + Send + Sync>,
}
impl ToolDefinition {
pub fn new(
name: impl Into<String>,
description: impl Into<String>,
parameters: Schema,
handler: impl Fn(serde_json::Value) -> Result<String, String> + Send + Sync + 'static,
) -> Self {
Self {
name: name.into(),
description: description.into(),
parameters,
handler: Box::new(handler),
}
}
}
impl std::fmt::Debug for ToolDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ToolDefinition")
.field("name", &self.name)
.field("description", &self.description)
.finish_non_exhaustive()
}
}
#[cfg(foundation_models_bridge)]
#[allow(clippy::type_complexity)]
struct ToolsContext {
tools: Vec<(String, Box<dyn Fn(serde_json::Value) -> Result<String, String> + Send + Sync>)>,
}
const FM_AVAILABLE: i32 = 0;
const FM_DEVICE_NOT_ELIGIBLE: i32 = 1;
const FM_NOT_ENABLED: i32 = 2;
const FM_MODEL_NOT_READY: i32 = 3;
pub fn is_available() -> bool {
availability().is_ok()
}
pub fn availability() -> Result<(), UnavailabilityReason> {
#[cfg(foundation_models_bridge)]
{
let code = unsafe { fm_availability_reason() };
match code {
FM_AVAILABLE => Ok(()),
FM_DEVICE_NOT_ELIGIBLE => Err(UnavailabilityReason::DeviceNotEligible),
FM_NOT_ENABLED => Err(UnavailabilityReason::NotEnabled),
FM_MODEL_NOT_READY => Err(UnavailabilityReason::ModelNotReady),
_ => Err(UnavailabilityReason::Unknown),
}
}
#[cfg(not(foundation_models_bridge))]
Err(UnavailabilityReason::DeviceNotEligible)
}
pub async fn respond(prompt: &str) -> Result<String, Error> {
respond_with_options(prompt, &GenerationOptions::default()).await
}
pub async fn respond_with_options(
prompt: &str,
options: &GenerationOptions,
) -> Result<String, Error> {
let session = Session::new()?;
session.respond_with_options(prompt, options).await
}
pub struct Session {
#[cfg(foundation_models_bridge)]
handle: *mut c_void,
#[cfg(foundation_models_bridge)]
_tools: Option<Arc<ToolsContext>>,
}
unsafe impl Send for Session {}
unsafe impl Sync for Session {}
impl std::fmt::Debug for Session {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Session").finish_non_exhaustive()
}
}
impl Session {
pub fn new() -> Result<Self, Error> {
Self::with_instructions("")
}
pub fn with_instructions(instructions: &str) -> Result<Self, Error> {
availability().map_err(Error::Unavailable)?;
#[cfg(foundation_models_bridge)]
{
let c_instructions = CString::new(instructions)?;
let handle = unsafe { fm_session_create(c_instructions.as_ptr()) };
if handle.is_null() {
return Err(Error::Unavailable(UnavailabilityReason::Unknown));
}
Ok(Self { handle, _tools: None })
}
#[cfg(not(foundation_models_bridge))]
{
let _ = instructions;
Err(Error::Unavailable(UnavailabilityReason::DeviceNotEligible))
}
}
pub fn with_tools(instructions: &str, tools: Vec<ToolDefinition>) -> Result<Self, Error> {
availability().map_err(Error::Unavailable)?;
#[cfg(foundation_models_bridge)]
{
let tool_descs: Vec<serde_json::Value> = tools
.iter()
.map(|t| {
serde_json::json!({
"name": t.name,
"description": t.description,
"properties": t.parameters.properties,
})
})
.collect();
let tools_json = serde_json::to_string(&tool_descs)?;
let tools_ctx = Arc::new(ToolsContext {
tools: tools
.into_iter()
.map(|t| (t.name, t.handler))
.collect(),
});
let tool_ctx_ptr = Arc::as_ptr(&tools_ctx) as *mut c_void;
let c_instructions = CString::new(instructions)?;
let c_tools_json = CString::new(tools_json)?;
let handle = unsafe {
fm_session_create_with_tools(
c_instructions.as_ptr(),
c_tools_json.as_ptr(),
tool_ctx_ptr,
tool_dispatch,
)
};
if handle.is_null() {
return Err(Error::Unavailable(UnavailabilityReason::Unknown));
}
Ok(Self { handle, _tools: Some(tools_ctx) })
}
#[cfg(not(foundation_models_bridge))]
{
let _ = (instructions, tools);
Err(Error::Unavailable(UnavailabilityReason::DeviceNotEligible))
}
}
pub fn update_profile(&self, instructions: &str) -> Result<(), Error> {
#[cfg(foundation_models_bridge)]
{
let c_instructions = CString::new(instructions)?;
unsafe {
fm_session_update_profile(self.handle, c_instructions.as_ptr());
}
Ok(())
}
#[cfg(not(foundation_models_bridge))]
{
let _ = instructions;
Err(Error::Unavailable(UnavailabilityReason::DeviceNotEligible))
}
}
pub async fn respond(&self, prompt: &str) -> Result<String, Error> {
self.respond_with_options(prompt, &GenerationOptions::default())
.await
}
pub async fn respond_with_options(
&self,
prompt: &str,
options: &GenerationOptions,
) -> Result<String, Error> {
options.validate()?;
#[cfg(foundation_models_bridge)]
{
let (tx, rx) = oneshot::channel::<Result<String, String>>();
let ctx = Box::into_raw(Box::new(tx)) as *mut c_void;
let c_prompt = CString::new(prompt)?;
unsafe {
fm_session_respond(
self.handle,
c_prompt.as_ptr(),
options.ffi_temperature(),
options.ffi_max_tokens(),
ctx,
respond_callback,
);
}
rx.await
.map_err(|_| Error::Generation("session was dropped before responding".into()))?
.map_err(Error::Generation)
}
#[cfg(not(foundation_models_bridge))]
{
let _ = (prompt, options);
Err(Error::Unavailable(UnavailabilityReason::DeviceNotEligible))
}
}
pub async fn respond_as<T: serde::de::DeserializeOwned>(
&self,
prompt: &str,
schema: &Schema,
) -> Result<T, Error> {
self.respond_as_with_options(prompt, schema, &GenerationOptions::default())
.await
}
pub async fn respond_as_with_options<T: serde::de::DeserializeOwned>(
&self,
prompt: &str,
schema: &Schema,
options: &GenerationOptions,
) -> Result<T, Error> {
options.validate()?;
#[cfg(foundation_models_bridge)]
{
let (tx, rx) = oneshot::channel::<Result<String, String>>();
let ctx = Box::into_raw(Box::new(tx)) as *mut c_void;
let c_prompt = CString::new(prompt)?;
let schema_json = serde_json::to_string(schema)?;
let c_schema_json = CString::new(schema_json)?;
unsafe {
fm_session_respond_structured(
self.handle,
c_prompt.as_ptr(),
c_schema_json.as_ptr(),
options.ffi_temperature(),
options.ffi_max_tokens(),
ctx,
respond_callback,
);
}
let json = rx
.await
.map_err(|_| Error::Generation("session was dropped before responding".into()))?
.map_err(Error::Generation)?;
Ok(serde_json::from_str(&json)?)
}
#[cfg(not(foundation_models_bridge))]
{
let _ = (prompt, schema, options);
Err(Error::Unavailable(UnavailabilityReason::DeviceNotEligible))
}
}
pub async fn respond_with_attachment(
&self,
prompt: &str,
attachment: &Attachment,
options: &GenerationOptions,
) -> Result<String, Error> {
options.validate()?;
#[cfg(foundation_models_bridge)]
{
let (tx, rx) = oneshot::channel::<Result<String, String>>();
let ctx = Box::into_raw(Box::new(tx)) as *mut c_void;
let c_prompt = CString::new(prompt)?;
let c_mime = CString::new(&*attachment.mime)?;
unsafe {
fm_session_respond_with_attachment(
self.handle,
c_prompt.as_ptr(),
attachment.data.as_ptr(),
attachment.data.len(),
c_mime.as_ptr(),
options.ffi_temperature(),
options.ffi_max_tokens(),
ctx,
respond_callback,
);
}
rx.await
.map_err(|_| Error::Generation("session was dropped before responding".into()))?
.map_err(Error::Generation)
}
#[cfg(not(foundation_models_bridge))]
{
let _ = (prompt, attachment, options);
Err(Error::Unavailable(UnavailabilityReason::DeviceNotEligible))
}
}
pub fn stream(&self, prompt: &str) -> Result<ResponseStream, Error> {
self.stream_with_options(prompt, &GenerationOptions::default())
}
pub fn stream_with_options(
&self,
prompt: &str,
options: &GenerationOptions,
) -> Result<ResponseStream, Error> {
options.validate()?;
#[cfg(foundation_models_bridge)]
{
let (tx, rx) = mpsc::unbounded::<Result<String, String>>();
let ctx = Box::into_raw(Box::new(StreamContext { tx })) as *mut c_void;
let c_prompt = CString::new(prompt)?;
unsafe {
fm_session_stream(
self.handle,
c_prompt.as_ptr(),
options.ffi_temperature(),
options.ffi_max_tokens(),
ctx,
stream_token_callback,
stream_done_callback,
);
}
Ok(ResponseStream { rx })
}
#[cfg(not(foundation_models_bridge))]
{
let _ = (prompt, options);
Err(Error::Unavailable(UnavailabilityReason::DeviceNotEligible))
}
}
pub fn stream_structured(
&self,
prompt: &str,
schema: &Schema,
options: &GenerationOptions,
) -> Result<ResponseStructuredStream, Error> {
options.validate()?;
#[cfg(foundation_models_bridge)]
{
let (tx, rx) = mpsc::unbounded::<Result<String, String>>();
let ctx = Box::into_raw(Box::new(StreamContext { tx })) as *mut c_void;
let c_prompt = CString::new(prompt)?;
let schema_json = serde_json::to_string(schema)?;
let c_schema_json = CString::new(schema_json)?;
unsafe {
fm_session_stream_structured(
self.handle,
c_prompt.as_ptr(),
c_schema_json.as_ptr(),
options.ffi_temperature(),
options.ffi_max_tokens(),
ctx,
stream_token_callback,
stream_done_callback,
);
}
Ok(ResponseStructuredStream { rx })
}
#[cfg(not(foundation_models_bridge))]
{
let _ = (prompt, schema, options);
Err(Error::Unavailable(UnavailabilityReason::DeviceNotEligible))
}
}
}
impl Drop for Session {
fn drop(&mut self) {
#[cfg(foundation_models_bridge)]
unsafe {
fm_session_destroy(self.handle);
}
}
}
pub struct ResponseStream {
rx: mpsc::UnboundedReceiver<Result<String, String>>,
}
impl Stream for ResponseStream {
type Item = Result<String, Error>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut StdContext<'_>,
) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.rx)
.poll_next(cx)
.map(|opt| opt.map(|r| r.map_err(Error::Generation)))
}
}
pub struct ResponseStructuredStream {
rx: mpsc::UnboundedReceiver<Result<String, String>>,
}
impl Stream for ResponseStructuredStream {
type Item = Result<String, Error>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut StdContext<'_>,
) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.rx)
.poll_next(cx)
.map(|opt| opt.map(|r| r.map_err(Error::Generation)))
}
}
#[cfg(foundation_models_bridge)]
extern "C" fn respond_callback(
ctx: *mut c_void,
result: *const c_char,
error: *const c_char,
) {
let tx = unsafe { Box::from_raw(ctx as *mut oneshot::Sender<Result<String, String>>) };
if !error.is_null() {
let msg = unsafe { CStr::from_ptr(error).to_string_lossy().into_owned() };
tx.send(Err(msg)).ok();
} else if !result.is_null() {
let text = unsafe { CStr::from_ptr(result).to_string_lossy().into_owned() };
tx.send(Ok(text)).ok();
}
}
#[cfg(foundation_models_bridge)]
struct StreamContext {
tx: mpsc::UnboundedSender<Result<String, String>>,
}
#[cfg(foundation_models_bridge)]
extern "C" fn stream_token_callback(ctx: *mut c_void, token: *const c_char) {
let stream_ctx = unsafe { &*(ctx as *const StreamContext) };
let text = unsafe { CStr::from_ptr(token).to_string_lossy().into_owned() };
stream_ctx.tx.unbounded_send(Ok(text)).ok();
}
#[cfg(foundation_models_bridge)]
extern "C" fn stream_done_callback(ctx: *mut c_void, error: *const c_char) {
let stream_ctx = unsafe { Box::from_raw(ctx as *mut StreamContext) };
if !error.is_null() {
let msg = unsafe { CStr::from_ptr(error).to_string_lossy().into_owned() };
stream_ctx.tx.unbounded_send(Err(msg)).ok();
}
}
#[cfg(foundation_models_bridge)]
extern "C" fn tool_dispatch(
ctx: *mut c_void,
name_ptr: *const c_char,
args_ptr: *const c_char,
result_ctx: *mut c_void,
result_cb: extern "C" fn(*mut c_void, *const c_char, *const c_char),
) {
let tools = unsafe { &*(ctx as *const ToolsContext) };
let name = unsafe { CStr::from_ptr(name_ptr).to_string_lossy() };
let args_str = unsafe { CStr::from_ptr(args_ptr).to_string_lossy() };
let args: serde_json::Value = match serde_json::from_str(&args_str) {
Ok(v) => v,
Err(e) => {
let msg = format!("invalid tool args JSON: {e}");
if let Ok(c) = CString::new(msg) {
result_cb(result_ctx, null(), c.as_ptr());
}
return;
}
};
match tools.tools.iter().find(|(n, _)| n == name.as_ref()) {
Some((_, handler)) => match handler(args) {
Ok(result) => {
if let Ok(c) = CString::new(result) {
result_cb(result_ctx, c.as_ptr(), null());
}
}
Err(err) => {
if let Ok(c) = CString::new(err) {
result_cb(result_ctx, null(), c.as_ptr());
}
}
},
None => {
let msg = format!("unknown tool: {name}");
if let Ok(c) = CString::new(msg) {
result_cb(result_ctx, null(), c.as_ptr());
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_available_returns_without_panic() {
let _ = is_available();
}
#[test]
fn test_availability_result_is_consistent() {
let avail = availability();
assert_eq!(is_available(), avail.is_ok());
}
#[test]
fn test_options_default_is_valid() {
let opts = GenerationOptions::default();
assert!(opts.validate().is_ok());
assert_eq!(opts.ffi_temperature(), -1.0);
assert_eq!(opts.ffi_max_tokens(), -1);
}
#[test]
fn test_options_valid_temperature() {
for temp in [0.0_f64, 1.0, 2.0] {
let opts = GenerationOptions { temperature: Some(temp), ..Default::default() };
assert!(opts.validate().is_ok(), "temperature {temp} should be valid");
}
}
#[test]
fn test_options_invalid_temperature() {
for temp in [-0.1_f64, 2.001, f64::INFINITY, f64::NAN] {
let opts = GenerationOptions { temperature: Some(temp), ..Default::default() };
assert!(opts.validate().is_err(), "temperature {temp} should be invalid");
}
}
#[test]
fn test_session_creation_fails_gracefully_when_unavailable() {
if is_available() {
return; }
let err = Session::new().unwrap_err();
assert!(matches!(err, Error::Unavailable(_)));
}
#[test]
fn test_null_byte_in_prompt_returns_error() {
if !is_available() {
return;
}
let result = futures_executor::block_on(respond("hello\0world"));
assert!(matches!(result, Err(Error::NullByte(_))));
}
#[test]
fn test_schema_builder() {
let schema = Schema::new("Point")
.description("A 2D point")
.property(SchemaProperty::new("x", SchemaPropertyType::Double).description("X axis"))
.property(SchemaProperty::new("y", SchemaPropertyType::Double));
assert_eq!(schema.name, "Point");
assert_eq!(schema.properties.len(), 2);
let json = serde_json::to_string(&schema).unwrap();
assert!(json.contains("\"x\""));
assert!(json.contains("\"double\""));
}
#[test]
fn test_tool_definition_builder() {
let tool = ToolDefinition::new(
"add",
"Add two numbers",
Schema::new("AddArgs")
.property(SchemaProperty::new("a", SchemaPropertyType::Double))
.property(SchemaProperty::new("b", SchemaPropertyType::Double)),
|args| {
let a = args["a"].as_f64().unwrap_or(0.0);
let b = args["b"].as_f64().unwrap_or(0.0);
Ok(format!("{}", a + b))
},
);
assert_eq!(tool.name, "add");
let result = (tool.handler)(serde_json::json!({"a": 3.0, "b": 4.0}));
assert_eq!(result.unwrap(), "7");
}
#[test]
#[ignore = "requires Apple Intelligence (macOS 26+, Apple Silicon, AI enabled)"]
fn test_simple_respond() {
let response = futures_executor::block_on(respond(
"Reply with only the number: what is 2 + 2?",
))
.expect("respond failed");
assert!(response.contains('4'), "expected '4' in: {response:?}");
}
#[test]
#[ignore = "requires Apple Intelligence"]
fn test_respond_with_low_temperature() {
let opts = GenerationOptions {
temperature: Some(0.0),
..Default::default()
};
let r = futures_executor::block_on(respond_with_options(
"Reply with only the word: capital of France?",
&opts,
))
.expect("respond_with_options failed");
assert!(r.to_lowercase().contains("paris"), "expected Paris in: {r:?}");
}
#[test]
#[ignore = "requires Apple Intelligence"]
fn test_multi_turn_session() {
let session =
Session::with_instructions("Reply to every message with exactly one word.")
.expect("Session::with_instructions failed");
let r1 = futures_executor::block_on(session.respond("Say hello."))
.expect("first respond failed");
let r2 = futures_executor::block_on(session.respond("Say goodbye."))
.expect("second respond failed");
assert!(!r1.is_empty(), "first response was empty");
assert!(!r2.is_empty(), "second response was empty");
}
#[test]
#[ignore = "requires Apple Intelligence"]
fn test_streaming_yields_chunks() {
let session = Session::new().expect("Session::new failed");
let stream = session.stream("Count: one two three").expect("stream failed");
let chunks: Vec<String> = futures_executor::block_on_stream(stream)
.map(|r| r.expect("stream item was error"))
.collect();
assert!(!chunks.is_empty(), "stream produced no chunks");
let full = chunks.join("");
assert!(!full.is_empty(), "concatenated response was empty");
}
#[test]
#[ignore = "requires Apple Intelligence"]
fn test_structured_generation() {
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct MathAnswer {
value: f64,
explanation: String,
}
let session = Session::new().expect("Session::new failed");
let schema = Schema::new("MathAnswer")
.description("A numeric answer with a brief explanation")
.property(
SchemaProperty::new("value", SchemaPropertyType::Double)
.description("The numeric result"),
)
.property(
SchemaProperty::new("explanation", SchemaPropertyType::String)
.description("One-sentence explanation"),
);
let answer: MathAnswer =
futures_executor::block_on(session.respond_as("What is 6 × 7?", &schema))
.expect("respond_as failed");
assert!(
(answer.value - 42.0).abs() < 0.5,
"expected 42, got {}",
answer.value
);
assert!(!answer.explanation.is_empty(), "explanation was empty");
}
#[test]
#[ignore = "requires Apple Intelligence"]
fn test_tool_calling() {
let tool = ToolDefinition::new(
"add_numbers",
"Add two numbers together and return the sum",
Schema::new("AddArgs")
.property(
SchemaProperty::new("a", SchemaPropertyType::Double)
.description("First number"),
)
.property(
SchemaProperty::new("b", SchemaPropertyType::Double)
.description("Second number"),
),
|args| {
let a = args["a"].as_f64().unwrap_or(0.0);
let b = args["b"].as_f64().unwrap_or(0.0);
Ok(format!("{}", a + b))
},
);
let session = Session::with_tools(
"You are a calculator. Use the add_numbers tool when asked to add.",
vec![tool],
)
.expect("Session::with_tools failed");
let response = futures_executor::block_on(session.respond("What is 15 + 27?"))
.expect("respond failed");
assert!(
response.contains("42"),
"expected 42 in response: {response:?}"
);
}
}