fm-rs 0.3.0

Rust bindings for Apple's FoundationModels.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
// Foundation Models 27 session APIs shared by all models.
// Selected by build.rs when the build SDK is 27.0 or newer;
// session_27_fallback.swift preserves the C ABI on older SDKs.

import CoreGraphics
import Foundation
import FoundationModels
import ImageIO

// The cross-import overlays are probed rather than assumed: the iOS
// simulator SDK ships only the CoreSpotlight overlay, not the Vision one.
// canImport only tests for the overlay; the public frameworks are imported.
#if canImport(_CoreSpotlight_FoundationModels)
    import CoreSpotlight
#endif
#if canImport(_Vision_FoundationModels)
    import Vision
#endif

private let foundationModels27Requirement =
    "This API requires macOS 27.0, iOS 27.0, visionOS 27.0, or later"

/// Maps Foundation Models 27 error types onto stable FFI codes, covering the
/// general `LanguageModelError` plus the split `SystemLanguageModel.Error`
/// and `LanguageModelSession.Error` cases.
func classifyLanguageModelError(_ error: Error) -> (FFIErrorCode, String)? {
    guard #available(iOS 27.0, macOS 27.0, visionOS 27.0, *) else {
        return nil
    }

    if let systemError = error as? SystemLanguageModel.Error {
        switch systemError {
        case .assetsUnavailable(let details):
            return (
                .assetsUnavailable,
                systemError.errorDescription ?? details.debugDescription
            )
        @unknown default:
            return (.generationFailed, systemError.localizedDescription)
        }
    }

    if let sessionError = error as? LanguageModelSession.Error {
        let message = sessionError.errorDescription ?? sessionError.debugDescription
        switch sessionError {
        case .concurrentRequests:
            return (.concurrentRequests, message)
        case .transcriptMutationWhileResponding:
            return (.invalidInput, message)
        @unknown default:
            return (.generationFailed, sessionError.localizedDescription)
        }
    }

    if let modelError = error as? LanguageModelError {
        let base = modelError.errorDescription
        switch modelError {
        case .contextSizeExceeded(let details):
            let message = base ?? details.debugDescription
            return (
                .contextSizeExceeded,
                message + " (context size \(details.contextSize) tokens, request \(details.tokenCount) tokens)"
            )
        case .rateLimited(let details):
            var message = base ?? details.debugDescription
            if let resetDate = details.resetDate {
                message += " (resets \(ISO8601DateFormatter().string(from: resetDate)))"
            }
            return (.rateLimited, message)
        case .guardrailViolation(let details):
            return (.guardrailViolation, base ?? details.debugDescription)
        case .refusal(let details):
            return (.refusal, base ?? details.debugDescription)
        case .unsupportedCapability(let details):
            return (.unsupportedCapability, base ?? details.debugDescription)
        case .unsupportedTranscriptContent(let details):
            return (.unsupportedTranscriptContent, base ?? details.debugDescription)
        case .unsupportedGenerationGuide(let details):
            return (.unsupportedGenerationGuide, base ?? details.debugDescription)
        case .unsupportedLanguageOrLocale(let details):
            return (.unsupportedLanguageOrLocale, base ?? details.debugDescription)
        case .timeout(let details):
            return (.timeout, base ?? details.debugDescription)
        @unknown default:
            return (.generationFailed, modelError.localizedDescription)
        }
    }

    return nil
}

// MARK: - Built-in System Tools

/// Instantiates Apple's built-in tools from the Vision and Core Spotlight
/// cross-import overlays. Returns nil and sets `errorOut` on failure.
func makeBuiltinTools(
    _ names: [String],
    errorOut: UnsafeMutablePointer<UnsafeMutableRawPointer?>?
) -> [any Tool]? {
    guard !names.isEmpty else { return [] }
    guard #available(iOS 27.0, macOS 27.0, visionOS 27.0, *) else {
        errorOut?.pointee = createError(
            "Built-in system tools require macOS 27.0, iOS 27.0, visionOS 27.0, or later",
            code: .unsupportedPlatform
        )
        return nil
    }

    var tools: [any Tool] = []
    for name in names {
        switch name {
        case "ocr":
            #if canImport(_Vision_FoundationModels)
                tools.append(OCRTool())
            #else
                errorOut?.pointee = createError(
                    "The OCR tool is not available in this platform SDK",
                    code: .unsupportedPlatform
                )
                return nil
            #endif
        case "barcodeReader":
            #if canImport(_Vision_FoundationModels)
                tools.append(BarcodeReaderTool())
            #else
                errorOut?.pointee = createError(
                    "The barcode reader tool is not available in this platform SDK",
                    code: .unsupportedPlatform
                )
                return nil
            #endif
        case "spotlightSearch":
            #if canImport(_CoreSpotlight_FoundationModels)
                tools.append(SpotlightSearchTool())
            #else
                errorOut?.pointee = createError(
                    "The Spotlight search tool is not available in this platform SDK",
                    code: .unsupportedPlatform
                )
                return nil
            #endif
        default:
            errorOut?.pointee = createError(
                "Unknown built-in tool '\(name)'",
                code: .invalidInput
            )
            return nil
        }
    }
    return tools
}

// MARK: - Session Usage

private struct SessionUsageDTO: Encodable {
    let inputTokens: Int
    let cachedInputTokens: Int
    let outputTokens: Int
    let reasoningTokens: Int
}

@available(iOS 27.0, macOS 27.0, visionOS 27.0, *)
private func usageJson(_ usage: LanguageModelSession.Usage) -> String? {
    let dto = SessionUsageDTO(
        inputTokens: usage.input.totalTokenCount,
        cachedInputTokens: usage.input.cachedTokenCount,
        outputTokens: usage.output.totalTokenCount,
        reasoningTokens: usage.output.reasoningTokenCount
    )
    guard let data = try? JSONEncoder().encode(dto) else { return nil }
    return String(data: data, encoding: .utf8)
}

/// Captures a response's exact token usage as JSON (Foundation Models 27).
func responseUsageJson<Content>(_ response: LanguageModelSession.Response<Content>) -> String? {
    guard #available(iOS 27.0, macOS 27.0, visionOS 27.0, *) else {
        return nil
    }
    return usageJson(response.usage)
}

/// Returns cumulative session token usage as JSON (Foundation Models 27).
@_cdecl("fm_session_usage")
public func fm_session_usage(
    _ sessionPtr: UnsafeMutableRawPointer,
    _ errorOut: UnsafeMutablePointer<UnsafeMutableRawPointer?>?
) -> UnsafeMutablePointer<CChar>? {
    guard #available(iOS 27.0, macOS 27.0, visionOS 27.0, *) else {
        errorOut?.pointee = createError(foundationModels27Requirement, code: .unsupportedPlatform)
        return nil
    }

    let state = Unmanaged<AnyObject>.fromOpaque(sessionPtr).takeUnretainedValue() as! SessionState
    guard let json = usageJson(state.session.usage) else {
        errorOut?.pointee = createError("Failed to encode session usage", code: .unknown)
        return nil
    }
    return strdup(json)
}

// MARK: - Transcript Controls

/// Replaces the session transcript (Foundation Models 27).
@_cdecl("fm_session_set_transcript")
public func fm_session_set_transcript(
    _ sessionPtr: UnsafeMutableRawPointer,
    _ transcriptJson: UnsafePointer<CChar>,
    _ errorOut: UnsafeMutablePointer<UnsafeMutableRawPointer?>?
) -> Bool {
    guard #available(iOS 27.0, macOS 27.0, visionOS 27.0, *) else {
        errorOut?.pointee = createError(foundationModels27Requirement, code: .unsupportedPlatform)
        return false
    }

    let state = Unmanaged<AnyObject>.fromOpaque(sessionPtr).takeUnretainedValue() as! SessionState
    guard !state.session.isResponding else {
        errorOut?.pointee = createError(
            "Cannot replace the transcript while the session is responding",
            code: .invalidInput
        )
        return false
    }

    let jsonString = String(cString: transcriptJson)
    guard let jsonData = jsonString.data(using: .utf8) else {
        errorOut?.pointee = createError("Transcript JSON is not valid UTF-8", code: .invalidInput)
        return false
    }

    do {
        let transcript = try JSONDecoder().decode(Transcript.self, from: jsonData)
        state.session.transcript = transcript
        return true
    } catch {
        errorOut?.pointee = createError(
            "Failed to decode transcript: \(error.localizedDescription)",
            code: .invalidInput
        )
        return false
    }
}

/// Sets or clears the transcript error handling policy (Foundation Models 27).
/// `policy`: 0 = clear, 1 = revert transcript, 2 = preserve transcript.
@_cdecl("fm_session_set_transcript_error_policy")
public func fm_session_set_transcript_error_policy(
    _ sessionPtr: UnsafeMutableRawPointer,
    _ policy: Int32,
    _ errorOut: UnsafeMutablePointer<UnsafeMutableRawPointer?>?
) -> Bool {
    guard #available(iOS 27.0, macOS 27.0, visionOS 27.0, *) else {
        errorOut?.pointee = createError(foundationModels27Requirement, code: .unsupportedPlatform)
        return false
    }

    let state = Unmanaged<AnyObject>.fromOpaque(sessionPtr).takeUnretainedValue() as! SessionState
    switch policy {
    case 0:
        state.session.transcriptErrorHandlingPolicy = nil
    case 1:
        state.session.transcriptErrorHandlingPolicy = .revertTranscript
    case 2:
        state.session.transcriptErrorHandlingPolicy = .preserveTranscript
    default:
        errorOut?.pointee = createError(
            "Transcript error policy must be 0 (clear), 1 (revert), or 2 (preserve)",
            code: .invalidInput
        )
        return false
    }
    return true
}

// MARK: - Multimodal Attachments

private struct AttachmentDTO: Decodable {
    let kind: String
    let path: String?
    let bufferIndex: Int?
    let label: String?
}

private struct PreparedImageAttachment: Sendable {
    enum Source: Sendable {
        case file(URL)
        case data(Data)
    }

    let source: Source
    let label: String?
}

private struct AttachmentInputError: Error, LocalizedError {
    let message: String
    var errorDescription: String? { message }
}

private func prepareImageAttachments(
    from dtos: [AttachmentDTO],
    buffers: UnsafePointer<UnsafePointer<UInt8>?>?,
    bufferLens: UnsafePointer<UInt>?,
    bufferCount: Int32
) throws -> [PreparedImageAttachment] {
    var inputs: [PreparedImageAttachment] = []
    for dto in dtos {
        let source: PreparedImageAttachment.Source
        switch dto.kind {
        case "file":
            guard let path = dto.path else {
                throw AttachmentInputError(message: "File attachment is missing a path")
            }
            source = .file(URL(fileURLWithPath: path))
        case "data":
            guard let index = dto.bufferIndex,
                  index >= 0,
                  index < Int(bufferCount),
                  let buffers,
                  let bufferLens,
                  let base = buffers[index]
            else {
                throw AttachmentInputError(message: "Data attachment references an invalid buffer")
            }
            let data = Data(bytes: base, count: Int(bufferLens[index]))
            guard let imageSource = CGImageSourceCreateWithData(data as CFData, nil),
                  CGImageSourceGetCount(imageSource) > 0
            else {
                throw AttachmentInputError(message: "Data attachment is not a decodable image")
            }
            source = .data(data)
        default:
            throw AttachmentInputError(message: "Attachment kind must be \"file\" or \"data\"")
        }
        inputs.append(PreparedImageAttachment(source: source, label: dto.label))
    }
    return inputs
}

@available(iOS 27.0, macOS 27.0, visionOS 27.0, *)
private func makeImageAttachments(
    from inputs: [PreparedImageAttachment]
) throws -> [Attachment<ImageAttachmentContent>] {
    var attachments: [Attachment<ImageAttachmentContent>] = []
    for input in inputs {
        var attachment: Attachment<ImageAttachmentContent>
        switch input.source {
        case .file(let url):
            attachment = Attachment(imageURL: url)
        case .data(let data):
            guard let source = CGImageSourceCreateWithData(data as CFData, nil),
                  let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil)
            else {
                throw AttachmentInputError(message: "Data attachment is not a decodable image")
            }
            attachment = Attachment(cgImage)
        }
        if let label = input.label {
            attachment = attachment.label(label)
        }
        attachments.append(attachment)
    }
    return attachments
}

/// Sends a prompt with image attachments and blocks until the response is
/// ready (Foundation Models 27 multimodal prompting).
@_cdecl("fm_session_respond_with_attachments")
public func fm_session_respond_with_attachments(
    _ sessionPtr: UnsafeMutableRawPointer,
    _ prompt: UnsafePointer<CChar>,
    _ optionsJson: UnsafePointer<CChar>?,
    _ attachmentsJson: UnsafePointer<CChar>,
    _ buffers: UnsafePointer<UnsafePointer<UInt8>?>?,
    _ bufferLens: UnsafePointer<UInt>?,
    _ bufferCount: Int32,
    _ errorOut: UnsafeMutablePointer<UnsafeMutableRawPointer?>?
) -> UnsafeMutablePointer<CChar>? {
    guard #available(iOS 27.0, macOS 27.0, visionOS 27.0, *) else {
        errorOut?.pointee = createError(foundationModels27Requirement, code: .unsupportedPlatform)
        return nil
    }

    let state = Unmanaged<AnyObject>.fromOpaque(sessionPtr).takeUnretainedValue() as! SessionState
    let promptString = String(cString: prompt)
    let options = parseGenerationOptions(optionsJson)

    let attachmentsString = String(cString: attachmentsJson)
    guard let attachmentsData = attachmentsString.data(using: .utf8),
          let dtos = try? JSONDecoder().decode([AttachmentDTO].self, from: attachmentsData)
    else {
        errorOut?.pointee = createError("Invalid attachments JSON", code: .invalidInput)
        return nil
    }

    let attachmentInputs: [PreparedImageAttachment]
    do {
        attachmentInputs = try prepareImageAttachments(
            from: dtos,
            buffers: buffers,
            bufferLens: bufferLens,
            bufferCount: bufferCount
        )
    } catch {
        errorOut?.pointee = createError(error.localizedDescription, code: .invalidInput)
        return nil
    }

    do {
        let (content, usageJson) = try AsyncWaiter.wait(session: state) {
            let attachments = try makeImageAttachments(from: attachmentInputs)
            let response = try await state.session.respond(options: options) {
                promptString
                for attachment in attachments {
                    attachment
                }
            }
            return (response.content, responseUsageJson(response))
        }
        state.setLastResponseUsage(usageJson)
        return strdup(content)
    } catch {
        errorOut?.pointee = createGenerationErrorFromException(error)
        return nil
    }
}