Repository navigation
Expand file tree
/
Copy pathapple_intelligence.swift
More file actions
334 lines (309 loc) · 11.2 KB
/
Copy pathapple_intelligence.swift
File metadata and controls
334 lines (309 loc) · 11.2 KB
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
import Foundation
#if canImport(FoundationModels)
import FoundationModels
#endif
private struct BridgeNotes: Codable {
let title: String
let summary: String
}
private struct BridgeError: LocalizedError {
let message: String
init(_ message: String) {
self.message = message
}
var errorDescription: String? { message }
}
private struct BridgeChapter: Codable {
let title: String
let summary: String
let same_topic: Bool
}
private struct BridgeChapterLines: Codable {
let lines: [Int]
}
/// Distinguishes an oversized prompt so Rust can shorten the excerpt once.
private let contextWindowMarker = "context-window:"
/// A refused or guardrail-blocked transcript, which retrying won't change.
private let refusalMarker = "refused:"
#if canImport(FoundationModels)
@available(macOS 26.0, *)
@Generable
private struct GeneratedNotes {
@Guide(description: "A short, specific meeting title of at most eight words, without dates or the word meeting")
let title: String
@Guide(description: "At most two short sentences on what the meeting covered and concluded")
let summary: String
}
@available(macOS 26.0, *)
@Generable
private struct GeneratedTitle {
@Guide(description: "A short, specific meeting title of at most eight words, without dates or the word meeting")
let title: String
}
/// Field order is generation order: the new lines are named on their own
/// before the model compares them with the current chapter.
@available(macOS 26.0, *)
@Generable
private struct GeneratedChapter {
@Guide(description: "A short name of two to five words for what the new lines are about, lowercase except names, without the word meeting")
let title: String
@Guide(description: "One short sentence on what the new lines cover")
let summary: String
@Guide(description: "true when the new lines are still about the current chapter, false when the conversation moved to a different topic")
let sameTopic: Bool
}
@available(macOS 26.0, *)
@Generable
private struct GeneratedChapterLines {
@Guide(description: "The L numbers of every line about the chapter after, in order")
let lines: [Int]
}
@available(macOS 26.0, *)
private func respond<Content: Generable>(
instructions: String,
prompt: String,
generating type: Content.Type,
maximumResponseTokens: Int
) async throws -> Content {
let model = SystemLanguageModel(
useCase: .general,
guardrails: .permissiveContentTransformations
)
let session = LanguageModelSession(model: model, instructions: instructions)
do {
return try await session.respond(
to: prompt,
generating: type,
options: GenerationOptions(temperature: 0.2, maximumResponseTokens: maximumResponseTokens)
).content
} catch LanguageModelSession.GenerationError.exceededContextWindowSize(let context) {
throw BridgeError("\(contextWindowMarker) \(context.debugDescription)")
} catch LanguageModelSession.GenerationError.refusal(_, let context) {
throw BridgeError("\(refusalMarker) \(context.debugDescription)")
} catch LanguageModelSession.GenerationError.guardrailViolation(let context) {
throw BridgeError("\(refusalMarker) \(context.debugDescription)")
}
}
@available(macOS 26.0, *)
private func generate(instructions: String, prompt: String, titleOnly: Bool) async throws -> BridgeNotes {
let model = SystemLanguageModel(
useCase: .general,
guardrails: .permissiveContentTransformations
)
let session = LanguageModelSession(model: model, instructions: instructions)
do {
if titleOnly {
let response = try await session.respond(
to: prompt,
generating: GeneratedTitle.self,
options: GenerationOptions(temperature: 0.2, maximumResponseTokens: 60)
)
return BridgeNotes(title: response.content.title, summary: "")
}
let response = try await session.respond(
to: prompt,
generating: GeneratedNotes.self,
options: GenerationOptions(temperature: 0.2, maximumResponseTokens: 220)
)
return BridgeNotes(title: response.content.title, summary: response.content.summary)
} catch LanguageModelSession.GenerationError.exceededContextWindowSize(let context) {
throw BridgeError("\(contextWindowMarker) \(context.debugDescription)")
}
}
#endif
private final class BlockingResult<Value: Sendable>: @unchecked Sendable {
private let lock = NSLock()
private var value: Result<Value, Error>?
func store(_ value: Result<Value, Error>) {
lock.withLock { self.value = value }
}
func take() -> Result<Value, Error> {
lock.withLock { value! }
}
}
private func blocking<Value: Sendable>(
timeout: TimeInterval,
_ operation: @escaping @Sendable () async throws -> Value
) -> Result<Value, Error> {
let semaphore = DispatchSemaphore(value: 0)
let result = BlockingResult<Value>()
let task = Task.detached(priority: .utility) {
do {
result.store(.success(try await operation()))
} catch {
result.store(.failure(error))
}
semaphore.signal()
}
guard semaphore.wait(timeout: .now() + timeout) == .success else {
task.cancel()
return .failure(BridgeError("Apple Intelligence timed out."))
}
return result.take()
}
private func writeError(
_ error: Error,
to destination: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?
) {
destination?.pointee = strdup(
(error as? LocalizedError)?.errorDescription ?? error.localizedDescription
)
}
/// 0 available, 1 device not eligible, 2 Apple Intelligence off,
/// 3 model not ready, 4 unsupported macOS, 5 unknown reason.
@_cdecl("hex_apple_intelligence_availability")
public func hexAppleIntelligenceAvailability() -> Int32 {
#if canImport(FoundationModels)
guard #available(macOS 26.0, *) else { return 4 }
switch SystemLanguageModel.default.availability {
case .available:
return 0
case .unavailable(.deviceNotEligible):
return 1
case .unavailable(.appleIntelligenceNotEnabled):
return 2
case .unavailable(.modelNotReady):
return 3
case .unavailable:
return 5
}
#else
return 4
#endif
}
@_cdecl("hex_apple_intelligence_context_size")
public func hexAppleIntelligenceContextSize() -> Int {
#if canImport(FoundationModels)
guard #available(macOS 26.0, *) else { return 0 }
return SystemLanguageModel.default.contextSize
#else
return 0
#endif
}
@_cdecl("hex_apple_intelligence_notes")
public func hexAppleIntelligenceNotes(
_ instructions: UnsafePointer<CChar>?,
_ prompt: UnsafePointer<CChar>?,
_ titleOnly: Bool,
_ timeoutMs: UInt64,
_ errorOutput: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?
) -> UnsafeMutablePointer<CChar>? {
#if canImport(FoundationModels)
guard #available(macOS 26.0, *), let instructions, let prompt else {
writeError(BridgeError("Apple Intelligence requires macOS 26."), to: errorOutput)
return nil
}
let instructionText = String(cString: instructions)
let promptText = String(cString: prompt)
let result = blocking(timeout: TimeInterval(timeoutMs) / 1_000) {
try await generate(instructions: instructionText, prompt: promptText, titleOnly: titleOnly)
}
switch result {
case .success(let notes):
do {
let data = try JSONEncoder().encode(notes)
return strdup(String(decoding: data, as: UTF8.self))
} catch {
writeError(error, to: errorOutput)
return nil
}
case .failure(let failure):
writeError(failure, to: errorOutput)
return nil
}
#else
writeError(BridgeError("Apple Intelligence requires macOS 26."), to: errorOutput)
return nil
#endif
}
/// Runs one guided generation and returns its JSON, or nil with `errorOutput` set.
private func bridgeCall<Value: Encodable & Sendable>(
_ instructions: UnsafePointer<CChar>?,
_ prompt: UnsafePointer<CChar>?,
_ timeoutMs: UInt64,
_ errorOutput: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,
_ operation: @escaping @Sendable (String, String) async throws -> Value
) -> UnsafeMutablePointer<CChar>? {
guard let instructions, let prompt else {
writeError(BridgeError("Apple Intelligence requires macOS 26."), to: errorOutput)
return nil
}
let instructionText = String(cString: instructions)
let promptText = String(cString: prompt)
let result = blocking(timeout: TimeInterval(timeoutMs) / 1_000) {
try await operation(instructionText, promptText)
}
switch result {
case .success(let value):
do {
let data = try JSONEncoder().encode(value)
return strdup(String(decoding: data, as: UTF8.self))
} catch {
writeError(error, to: errorOutput)
return nil
}
case .failure(let failure):
writeError(failure, to: errorOutput)
return nil
}
}
@_cdecl("hex_apple_intelligence_chapter")
public func hexAppleIntelligenceChapter(
_ instructions: UnsafePointer<CChar>?,
_ prompt: UnsafePointer<CChar>?,
_ timeoutMs: UInt64,
_ errorOutput: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?
) -> UnsafeMutablePointer<CChar>? {
#if canImport(FoundationModels)
guard #available(macOS 26.0, *) else {
writeError(BridgeError("Apple Intelligence requires macOS 26."), to: errorOutput)
return nil
}
return bridgeCall(instructions, prompt, timeoutMs, errorOutput) { instructions, prompt in
let chapter = try await respond(
instructions: instructions,
prompt: prompt,
generating: GeneratedChapter.self,
maximumResponseTokens: 120
)
return BridgeChapter(
title: chapter.title,
summary: chapter.summary,
same_topic: chapter.sameTopic
)
}
#else
writeError(BridgeError("Apple Intelligence requires macOS 26."), to: errorOutput)
return nil
#endif
}
@_cdecl("hex_apple_intelligence_chapter_lines")
public func hexAppleIntelligenceChapterLines(
_ instructions: UnsafePointer<CChar>?,
_ prompt: UnsafePointer<CChar>?,
_ timeoutMs: UInt64,
_ errorOutput: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?
) -> UnsafeMutablePointer<CChar>? {
#if canImport(FoundationModels)
guard #available(macOS 26.0, *) else {
writeError(BridgeError("Apple Intelligence requires macOS 26."), to: errorOutput)
return nil
}
return bridgeCall(instructions, prompt, timeoutMs, errorOutput) { instructions, prompt in
let lines = try await respond(
instructions: instructions,
prompt: prompt,
generating: GeneratedChapterLines.self,
maximumResponseTokens: 240
)
return BridgeChapterLines(lines: lines.lines)
}
#else
writeError(BridgeError("Apple Intelligence requires macOS 26."), to: errorOutput)
return nil
#endif
}
@_cdecl("hex_apple_intelligence_free_string")
public func hexAppleIntelligenceFreeString(_ string: UnsafeMutablePointer<CChar>?) {
free(string)
}