@bendyline/gezel-capacitor 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/Package.swift +26 -0
- package/README.md +230 -2
- package/android/build.gradle +30 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/GezelKnowledgePlugin.java +153 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/GezelOfflineSpeechPlugin.java +216 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/GezelRuntimePlugin.java +84 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/KnowledgeFiles.java +101 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/SpeechModels.java +230 -0
- package/android/src/main/java/com/bendyline/gezel/speech/SpeechRuntime.java +38 -0
- package/dist/index.d.ts +561 -0
- package/dist/index.js +7512 -0
- package/dist/knowledge-worker.js +211 -0
- package/dist/licenses/npm/manifest.json +76 -0
- package/dist/licenses/npm/texts/1583809bf1155510d2961f94aa9bca6dc4b44b6f59fd773de33bd6c20860d781.txt +29 -0
- package/dist/licenses/npm/texts/1cd39aa4a566eba49eb206b969ef423295e79b50503df567bbdb2e8877d78892.txt +8 -0
- package/dist/licenses/npm/texts/3f1189b28e3866e0d979968d466b78f813f76827cfdca1fbb124cc0a5c8841f8.txt +21 -0
- package/dist/licenses/npm/texts/5b37224c080cdcc97c871ada971c224e9926370fe74f11b539aa1cf9f3b1aca1.txt +21 -0
- package/dist/licenses/npm/texts/c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4.txt +201 -0
- package/dist/sqlite3.wasm +0 -0
- package/embedding-manifest.json +210 -0
- package/ios/Sources/GezelCapacitor/GezelKnowledgePlugin.swift +200 -0
- package/ios/Sources/GezelCapacitor/GezelOfflineSpeechPlugin.swift +484 -0
- package/ios/Sources/GezelCapacitor/GezelRuntimePlugin.swift +102 -0
- package/kokoro-lexicon/LICENSE +202 -0
- package/kokoro-lexicon/lexicon-gb-en.txt.gz +0 -0
- package/kokoro-lexicon/lexicon-us-en.txt.gz +0 -0
- package/native/android/LICENSE-gezel.txt +21 -0
- package/native/android/PUBLIC-DISTRIBUTION.md +97 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-arm-optimized-routines-MIT-OR-Apache-2.0.txt +249 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-cpp-httplib-MIT.txt +22 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-fp16-MIT.txt +11 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-ggml-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-llamafile-sgemm-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-openvino-APACHE.txt +201 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-pillow-MIT-CMU.txt +30 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-rotate-bits-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-unicode-data-Unicode-3.0.txt +39 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-nlohmann-json-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-nvidia-cub-BSD-3-Clause.txt +24 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-headers-Apache-2.0-OR-MIT.txt +33 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-APACHE.txt +201 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-cjson-MIT.txt +32 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-yarn-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/manifest.json +21 -0
- package/native/android/engine-manifest.json +88 -0
- package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.aar +0 -0
- package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.aar.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.pom +8 -0
- package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.pom.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar +0 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha512 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar +0 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha512 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module +118 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha512 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom +51 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha512 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml +13 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha512 +1 -0
- package/native/android/sdk-manifest.json +98 -0
- package/native/ios/GezelLlama.xcframework/Info.plist +47 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64/Headers/gezel_llama.h +290 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64/Headers/module.modulemap +9 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64/libGezelLlama.a +0 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/Headers/gezel_llama.h +290 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/Headers/module.modulemap +9 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/libGezelLlama.a +0 -0
- package/native/ios/LICENSE-gezel.txt +21 -0
- package/native/ios/LICENSE-ggml.txt +21 -0
- package/native/ios/LICENSE-llama-cpp.txt +21 -0
- package/native/ios/PUBLIC-DISTRIBUTION.md +97 -0
- package/native/ios/Package.swift +17 -0
- package/native/ios/Sources/GezelModelStorage/MobileModelStore.swift +286 -0
- package/native/ios/Sources/GezelModelStorage/ModelDownloadHTTP.swift +55 -0
- package/native/ios/Sources/GezelModelStorage/ModelDownloadSource.swift +46 -0
- package/native/ios/Sources/GezelModelStorage/ModelDownloads.swift +289 -0
- package/native/ios/Sources/GezelModelStorage/PrivacyInfo.xcprivacy +32 -0
- package/native/ios/Sources/GezelRuntime/AppleFoundationProvider.swift +292 -0
- package/native/ios/Sources/GezelRuntime/AppleNativeTools.swift +152 -0
- package/native/ios/Sources/GezelRuntime/GezelNativeRuntime.swift +1349 -0
- package/native/ios/Sources/GezelRuntime/NativeCall.swift +35 -0
- package/native/ios/Sources/GezelRuntime/PrivacyInfo.xcprivacy +31 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-arm-optimized-routines-MIT-OR-Apache-2.0.txt +249 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-cpp-httplib-MIT.txt +22 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-fp16-MIT.txt +11 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-ggml-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-llamafile-sgemm-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-openvino-APACHE.txt +201 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-pillow-MIT-CMU.txt +30 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-rotate-bits-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-unicode-data-Unicode-3.0.txt +39 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-nlohmann-json-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-nvidia-cub-BSD-3-Clause.txt +24 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-headers-Apache-2.0-OR-MIT.txt +33 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-APACHE.txt +201 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-cjson-MIT.txt +32 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-yarn-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/manifest.json +21 -0
- package/native/ios/engine-manifest.json +102 -0
- package/native/ios/sdk-manifest.json +89 -0
- package/package.json +94 -3
- package/scripts/embedding-package.d.mts +23 -0
- package/scripts/embedding-package.mjs +185 -0
- package/scripts/stage-native.mjs +168 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import CryptoKit
|
|
3
|
+
|
|
4
|
+
public struct MobileModelDownload: Codable, Sendable {
|
|
5
|
+
public let id: String
|
|
6
|
+
public let name: String
|
|
7
|
+
public let source: MobileModelSource
|
|
8
|
+
public var state: String
|
|
9
|
+
public var downloadedBytes: Int64
|
|
10
|
+
public var error: String?
|
|
11
|
+
public var modelId: String?
|
|
12
|
+
var etag: String?
|
|
13
|
+
public func json() throws -> [String: Any] {
|
|
14
|
+
var result = try JSONSerialization.jsonObject(with: JSONEncoder().encode(self)) as! [String:Any]
|
|
15
|
+
result.removeValue(forKey: "etag")
|
|
16
|
+
return result
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/// Foreground streaming acquisition, independent of inference and product files.
|
|
21
|
+
public final class ModelDownloads: @unchecked Sendable {
|
|
22
|
+
private let store: MobileModelStore
|
|
23
|
+
private let root: URL
|
|
24
|
+
private let configuration: URLSessionConfiguration
|
|
25
|
+
private let queue = DispatchQueue(label: "com.bendyline.gezel.model-download")
|
|
26
|
+
private let sourceQueue = DispatchQueue(label: "com.bendyline.gezel.model-source")
|
|
27
|
+
private let journalLock = NSRecursiveLock()
|
|
28
|
+
private var sourcePending = false
|
|
29
|
+
private var sourceCancelled = false
|
|
30
|
+
private var sourceTransfer: ModelDownloadHTTP?
|
|
31
|
+
private let cancellationLock = NSLock()
|
|
32
|
+
private var cancelled = false
|
|
33
|
+
private var cancelIdentity: String?
|
|
34
|
+
private var activeId: String?
|
|
35
|
+
private var transfer: ModelDownloadHTTP?
|
|
36
|
+
private var output: FileHandle?
|
|
37
|
+
private var current: MobileModelDownload?
|
|
38
|
+
private var offset: Int64 = 0
|
|
39
|
+
private var checkpoint: Int64 = 0
|
|
40
|
+
private var cancelCallbacks: [@Sendable () -> Void] = []
|
|
41
|
+
private let fm = FileManager.default
|
|
42
|
+
|
|
43
|
+
public init(store: MobileModelStore, configuration: URLSessionConfiguration = .ephemeral) throws {
|
|
44
|
+
self.store = store; self.root = try store.downloadsDirectory(); self.configuration = configuration
|
|
45
|
+
for var record in try records() where ["queued","downloading","verifying"].contains(record.state) {
|
|
46
|
+
record.state = "paused"; record.error = "Download paused when the app closed. Resume when ready."
|
|
47
|
+
record.downloadedBytes = try length(candidate(record))
|
|
48
|
+
try save(record)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
private func folder(_ id: String) throws -> URL {
|
|
52
|
+
guard UUID(uuidString: id)?.uuidString.lowercased() == id else { throw ModelDownloadError("Invalid download identity") }
|
|
53
|
+
let url = root.appendingPathComponent(id, isDirectory: true)
|
|
54
|
+
try rejectLink(url)
|
|
55
|
+
return url
|
|
56
|
+
}
|
|
57
|
+
private func rejectLink(_ file: URL) throws {
|
|
58
|
+
// attributesOfItem inspects dangling links too; fileExists follows them.
|
|
59
|
+
if let attributes = try? fm.attributesOfItem(atPath: file.path), attributes[.type] as? FileAttributeType == .typeSymbolicLink {
|
|
60
|
+
throw ModelDownloadError("Invalid model staging link")
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
private func partial(_ record: MobileModelDownload) throws -> URL {
|
|
64
|
+
let file = try folder(record.id).appendingPathComponent("model.part")
|
|
65
|
+
try rejectLink(file); return file
|
|
66
|
+
}
|
|
67
|
+
private func candidate(_ record: MobileModelDownload) throws -> URL {
|
|
68
|
+
let file = try partial(record)
|
|
69
|
+
return fm.fileExists(atPath: file.path) ? file : try store.downloadModelURL(id: record.id)
|
|
70
|
+
}
|
|
71
|
+
private func length(_ file: URL) throws -> Int64 {
|
|
72
|
+
guard fm.fileExists(atPath: file.path) else { return 0 }
|
|
73
|
+
let values = try file.resourceValues(forKeys: [.isRegularFileKey,.isSymbolicLinkKey,.fileSizeKey])
|
|
74
|
+
guard values.isRegularFile == true, values.isSymbolicLink != true else { throw ModelDownloadError("Invalid model staging file") }
|
|
75
|
+
return Int64(values.fileSize ?? 0)
|
|
76
|
+
}
|
|
77
|
+
private func read(_ id: String) throws -> MobileModelDownload {
|
|
78
|
+
let url = try folder(id).appendingPathComponent("download.json")
|
|
79
|
+
try rejectLink(url)
|
|
80
|
+
guard try length(url) <= 32 * 1024 else { throw ModelDownloadError("Invalid download record") }
|
|
81
|
+
var record = try JSONDecoder().decode(MobileModelDownload.self, from: Data(contentsOf: url))
|
|
82
|
+
guard record.id == id, !record.name.isEmpty, record.name.utf16.count <= 200,
|
|
83
|
+
["queued","downloading","paused","verifying","complete","failed"].contains(record.state) else { throw ModelDownloadError("Invalid download record") }
|
|
84
|
+
try record.source.validate(exact: true)
|
|
85
|
+
if record.state != "complete" { record.downloadedBytes = min(try length(candidate(record)), record.source.sizeBytes!) }
|
|
86
|
+
return record
|
|
87
|
+
}
|
|
88
|
+
private func records() throws -> [MobileModelDownload] {
|
|
89
|
+
journalLock.lock(); defer { journalLock.unlock() }
|
|
90
|
+
let entries = try fm.contentsOfDirectory(at: root, includingPropertiesForKeys: [.isDirectoryKey])
|
|
91
|
+
guard entries.count <= 16 else { throw ModelDownloadError("Download library exceeds its limit") }
|
|
92
|
+
return try entries.map { try read($0.lastPathComponent) }
|
|
93
|
+
}
|
|
94
|
+
private func save(_ record: MobileModelDownload) throws {
|
|
95
|
+
journalLock.lock(); defer { journalLock.unlock() }
|
|
96
|
+
let directory = try folder(record.id)
|
|
97
|
+
try fm.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
98
|
+
try JSONEncoder().encode(record).write(to: directory.appendingPathComponent("download.json"), options: .atomic)
|
|
99
|
+
}
|
|
100
|
+
private func checkCancelled() throws {
|
|
101
|
+
cancellationLock.lock(); let stopped = cancelled; cancellationLock.unlock()
|
|
102
|
+
if stopped { throw ModelDownloadError("Download paused") }
|
|
103
|
+
}
|
|
104
|
+
public func list() throws -> [MobileModelDownload] { try records() }
|
|
105
|
+
private static func exactLength(_ text: String?) throws -> Int64 {
|
|
106
|
+
guard let text, text.range(of: "^[0-9]{1,12}$", options: .regularExpression) != nil, let value = Int64(text) else {
|
|
107
|
+
throw ModelDownloadError("Model source omitted a valid exact length")
|
|
108
|
+
}
|
|
109
|
+
return value
|
|
110
|
+
}
|
|
111
|
+
private static func etag(_ response: HTTPURLResponse) -> String? {
|
|
112
|
+
guard let value = response.value(forHTTPHeaderField: "ETag"), value.utf8.count <= 512,
|
|
113
|
+
value.range(of: "^\"[^\\r\\n\"]+\"$", options: .regularExpression) != nil else { return nil }
|
|
114
|
+
return value
|
|
115
|
+
}
|
|
116
|
+
public func resolve(_ identity: MobileModelSource, completion: @escaping @Sendable (Result<MobileModelSource,Error>) -> Void) {
|
|
117
|
+
cancellationLock.lock()
|
|
118
|
+
guard !sourcePending else { cancellationLock.unlock(); completion(.failure(ModelDownloadError("A model source is already being checked"))); return }
|
|
119
|
+
sourcePending = true; sourceCancelled = false; cancellationLock.unlock()
|
|
120
|
+
sourceQueue.async {
|
|
121
|
+
let done: @Sendable (Result<MobileModelSource,Error>) -> Void = { result in
|
|
122
|
+
self.cancellationLock.lock()
|
|
123
|
+
let cancelled = self.sourceCancelled
|
|
124
|
+
self.sourceTransfer = nil; self.sourcePending = false; self.cancellationLock.unlock()
|
|
125
|
+
completion(cancelled ? .failure(ModelDownloadError("Source lookup cancelled")) : result)
|
|
126
|
+
}
|
|
127
|
+
do {
|
|
128
|
+
try identity.validate(exact: false)
|
|
129
|
+
let box = ResolvedSource(identity)
|
|
130
|
+
let request = try ModelDownloadHTTP(url: identity.url(), method: "HEAD", fields: [:], configuration: self.configuration,
|
|
131
|
+
queue: self.sourceQueue, headers: { response in
|
|
132
|
+
guard response.statusCode == 200 else { throw ModelDownloadError("Model source cannot be inspected (HTTP \(response.statusCode)). Gated models must be imported from Files.") }
|
|
133
|
+
box.source.sizeBytes = try Self.exactLength(response.value(forHTTPHeaderField: "Content-Length"))
|
|
134
|
+
try box.source.validate(exact: true)
|
|
135
|
+
}, chunk: { _ in throw ModelDownloadError("Model metadata request returned unexpected bytes") }, completion: { error in
|
|
136
|
+
if let error { done(.failure(error)) } else { done(.success(box.source)) }
|
|
137
|
+
})
|
|
138
|
+
self.cancellationLock.lock()
|
|
139
|
+
self.sourceTransfer = request
|
|
140
|
+
let cancelled = self.sourceCancelled
|
|
141
|
+
self.cancellationLock.unlock()
|
|
142
|
+
request.start(); if cancelled { request.cancel() }
|
|
143
|
+
} catch { done(.failure(error)) }
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
public func cancelSourceResolution() {
|
|
147
|
+
cancellationLock.lock(); sourceCancelled = true; let request = sourceTransfer; cancellationLock.unlock()
|
|
148
|
+
request?.cancel()
|
|
149
|
+
}
|
|
150
|
+
public func suspend() { cancelSourceResolution(); cancel() }
|
|
151
|
+
private final class ResolvedSource: @unchecked Sendable {
|
|
152
|
+
var source: MobileModelSource
|
|
153
|
+
init(_ source: MobileModelSource) { self.source = source }
|
|
154
|
+
}
|
|
155
|
+
private func space(for remaining: Int64) throws {
|
|
156
|
+
let free = try fm.attributesOfFileSystem(forPath: root.path)[.systemFreeSize] as? NSNumber
|
|
157
|
+
guard (free?.int64Value ?? 0) > remaining + 64 * 1024 * 1024 else { throw ModelDownloadError("Not enough storage for this model") }
|
|
158
|
+
}
|
|
159
|
+
public func start(source: MobileModelSource, name: String) throws -> MobileModelDownload {
|
|
160
|
+
try queue.sync {
|
|
161
|
+
guard activeId == nil else { throw ModelDownloadError("A model download is already running") }
|
|
162
|
+
try source.validate(exact: true)
|
|
163
|
+
guard !name.isEmpty, name.utf16.count <= 200, !name.contains("\0") else { throw ModelDownloadError("Invalid model name") }
|
|
164
|
+
guard try records().count < 16 else { throw ModelDownloadError("Remove a saved download before starting another") }
|
|
165
|
+
try space(for: source.sizeBytes!)
|
|
166
|
+
let record = MobileModelDownload(id: UUID().uuidString.lowercased(), name: name, source: source, state: "queued", downloadedBytes: 0)
|
|
167
|
+
try save(record); launch(record)
|
|
168
|
+
return record
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
public func resume(id: String) throws -> MobileModelDownload {
|
|
172
|
+
try queue.sync {
|
|
173
|
+
guard activeId == nil else { throw ModelDownloadError("A model download is already running") }
|
|
174
|
+
var record = try read(id)
|
|
175
|
+
if record.state == "complete" { return record }
|
|
176
|
+
try space(for: record.source.sizeBytes! - record.downloadedBytes)
|
|
177
|
+
record.state = "queued"; record.error = nil; try save(record); launch(record)
|
|
178
|
+
return record
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
private func launch(_ record: MobileModelDownload) {
|
|
182
|
+
cancellationLock.lock(); cancelled = false; cancelIdentity = record.id; cancellationLock.unlock()
|
|
183
|
+
activeId = record.id
|
|
184
|
+
queue.async { self.begin(record) }
|
|
185
|
+
}
|
|
186
|
+
public func cancel(id: String? = nil, completion: @escaping @Sendable () -> Void = {}) {
|
|
187
|
+
// Interrupt hashing too; it deliberately checks between bounded chunks.
|
|
188
|
+
cancellationLock.lock()
|
|
189
|
+
guard cancelIdentity != nil, id == nil || cancelIdentity == id else { cancellationLock.unlock(); completion(); return }
|
|
190
|
+
cancelled = true; cancellationLock.unlock()
|
|
191
|
+
queue.async {
|
|
192
|
+
if let id, self.activeId != id { completion(); return }
|
|
193
|
+
guard self.activeId != nil else { completion(); return }
|
|
194
|
+
self.cancelCallbacks.append(completion)
|
|
195
|
+
if let transfer = self.transfer { transfer.cancel() }
|
|
196
|
+
else { self.finish(ModelDownloadError("Download paused")) }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
public func remove(id: String, completion: @escaping @Sendable (Result<Void,Error>) -> Void) {
|
|
200
|
+
cancel(id: id) {
|
|
201
|
+
self.queue.async {
|
|
202
|
+
do { _ = try self.read(id); try self.fm.removeItem(at: self.folder(id)); completion(.success(())) }
|
|
203
|
+
catch { completion(.failure(error)) }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
private func begin(_ input: MobileModelDownload) {
|
|
208
|
+
current = input
|
|
209
|
+
do {
|
|
210
|
+
let record = input; let expected = record.source.sizeBytes!
|
|
211
|
+
let file = try candidate(record); offset = try length(file)
|
|
212
|
+
guard offset <= expected else { throw ModelDownloadError("Saved partial exceeds expected model length") }
|
|
213
|
+
try checkCancelled()
|
|
214
|
+
if offset == expected { try verify(file); return }
|
|
215
|
+
let part = try partial(record)
|
|
216
|
+
if offset > 0 && record.etag == nil { try fm.removeItem(at: part); offset = 0 }
|
|
217
|
+
let priorOffset = offset, priorTag = record.etag
|
|
218
|
+
let fields = offset > 0 ? ["Range":"bytes=\(offset)-", "If-Range":record.etag!] : [:]
|
|
219
|
+
transfer = try ModelDownloadHTTP(url: record.source.url(), method: "GET", fields: fields, configuration: configuration, queue: queue,
|
|
220
|
+
headers: { response in
|
|
221
|
+
try self.checkCancelled()
|
|
222
|
+
let tag = Self.etag(response)
|
|
223
|
+
if response.statusCode == 200 { self.offset = 0 }
|
|
224
|
+
else if response.statusCode == 206, priorOffset > 0 {
|
|
225
|
+
let expectedRange = "bytes \(priorOffset)-\(expected-1)/\(expected)"
|
|
226
|
+
guard let tag, tag == priorTag, response.value(forHTTPHeaderField: "Content-Range") == expectedRange else {
|
|
227
|
+
throw ModelDownloadError("Model source changed its resume identity or byte range; remove this download and try again")
|
|
228
|
+
}
|
|
229
|
+
} else { throw ModelDownloadError("Model download failed (HTTP \(response.statusCode))") }
|
|
230
|
+
guard try Self.exactLength(response.value(forHTTPHeaderField: "Content-Length")) == expected - self.offset else { throw ModelDownloadError("Model source length differs from the verified download source") }
|
|
231
|
+
if let encoding = response.value(forHTTPHeaderField: "Content-Encoding"), encoding.lowercased() != "identity" { throw ModelDownloadError("Compressed model transfers are not supported") }
|
|
232
|
+
if !self.fm.fileExists(atPath: part.path) { guard self.fm.createFile(atPath: part.path, contents: nil) else { throw ModelDownloadError("Cannot create model staging file") } }
|
|
233
|
+
self.output = try FileHandle(forWritingTo: part)
|
|
234
|
+
if self.offset == 0 { try self.output!.truncate(atOffset: 0) }
|
|
235
|
+
else { guard try self.output!.seekToEnd() == UInt64(self.offset) else { throw ModelDownloadError("Saved partial changed") } }
|
|
236
|
+
self.current!.etag = tag; self.checkpoint = self.offset
|
|
237
|
+
try self.update(state: "downloading")
|
|
238
|
+
}, chunk: { data in
|
|
239
|
+
try self.checkCancelled()
|
|
240
|
+
guard self.offset + Int64(data.count) <= expected else { throw ModelDownloadError("Model transfer exceeded its expected length") }
|
|
241
|
+
try self.output!.write(contentsOf: data); self.offset += Int64(data.count)
|
|
242
|
+
if self.offset - self.checkpoint >= 1024 * 1024 {
|
|
243
|
+
try self.output!.synchronize(); try self.update(state: "downloading"); self.checkpoint = self.offset
|
|
244
|
+
}
|
|
245
|
+
}, completion: { error in
|
|
246
|
+
do {
|
|
247
|
+
try self.output?.synchronize(); try self.output?.close(); self.output = nil; self.transfer = nil
|
|
248
|
+
if let error { throw error }
|
|
249
|
+
guard self.offset == expected else { throw ModelDownloadError("Model transfer ended early. Resume to continue.") }
|
|
250
|
+
try self.verify(part)
|
|
251
|
+
} catch { self.finish(error) }
|
|
252
|
+
})
|
|
253
|
+
transfer!.start()
|
|
254
|
+
} catch { finish(error) }
|
|
255
|
+
}
|
|
256
|
+
private func update(state: String) throws {
|
|
257
|
+
current!.state = state; current!.downloadedBytes = offset
|
|
258
|
+
try save(current!)
|
|
259
|
+
}
|
|
260
|
+
private func verify(_ file: URL) throws {
|
|
261
|
+
try checkCancelled(); try update(state: "verifying")
|
|
262
|
+
let input = try FileHandle(forReadingFrom: file); defer { try? input.close() }
|
|
263
|
+
var digest = SHA256()
|
|
264
|
+
while let chunk = try input.read(upToCount: 1024 * 1024), !chunk.isEmpty { try checkCancelled(); digest.update(data: chunk) }
|
|
265
|
+
let hash = digest.finalize().map { String(format: "%02x", $0) }.joined()
|
|
266
|
+
guard hash == current!.source.sha256 else {
|
|
267
|
+
try fm.removeItem(at: file); offset = 0; current!.etag = nil
|
|
268
|
+
throw ModelDownloadError("Model SHA-256 verification failed. No model was installed.")
|
|
269
|
+
}
|
|
270
|
+
try checkCancelled()
|
|
271
|
+
_ = try store.publishDownloadedModel(id: current!.id, name: current!.name, source: current!.source, file: file)
|
|
272
|
+
current!.modelId = current!.id; current!.error = nil; try update(state: "complete")
|
|
273
|
+
finish(nil)
|
|
274
|
+
}
|
|
275
|
+
private func finish(_ error: Error?) {
|
|
276
|
+
try? output?.synchronize(); try? output?.close(); output = nil; transfer = nil
|
|
277
|
+
if let error, var record = current {
|
|
278
|
+
cancellationLock.lock(); let paused = cancelled; cancellationLock.unlock()
|
|
279
|
+
record.state = paused ? "paused" : "failed"
|
|
280
|
+
record.error = String((paused ? "Download paused. Resume when ready." : error.localizedDescription).prefix(1000))
|
|
281
|
+
record.downloadedBytes = min((try? length(candidate(record))) ?? 0, record.source.sizeBytes!)
|
|
282
|
+
try? save(record)
|
|
283
|
+
}
|
|
284
|
+
current = nil; activeId = nil
|
|
285
|
+
cancellationLock.lock(); cancelIdentity = nil; cancellationLock.unlock()
|
|
286
|
+
let callbacks = cancelCallbacks; cancelCallbacks.removeAll()
|
|
287
|
+
callbacks.forEach { $0() }
|
|
288
|
+
}
|
|
289
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>NSPrivacyTracking</key>
|
|
6
|
+
<false/>
|
|
7
|
+
<key>NSPrivacyTrackingDomains</key>
|
|
8
|
+
<array/>
|
|
9
|
+
<key>NSPrivacyCollectedDataTypes</key>
|
|
10
|
+
<array/>
|
|
11
|
+
<key>NSPrivacyAccessedAPITypes</key>
|
|
12
|
+
<array>
|
|
13
|
+
<dict>
|
|
14
|
+
<key>NSPrivacyAccessedAPIType</key>
|
|
15
|
+
<string>NSPrivacyAccessedAPICategoryFileTimestamp</string>
|
|
16
|
+
<key>NSPrivacyAccessedAPITypeReasons</key>
|
|
17
|
+
<array>
|
|
18
|
+
<string>C617.1</string>
|
|
19
|
+
<string>3B52.1</string>
|
|
20
|
+
</array>
|
|
21
|
+
</dict>
|
|
22
|
+
<dict>
|
|
23
|
+
<key>NSPrivacyAccessedAPIType</key>
|
|
24
|
+
<string>NSPrivacyAccessedAPICategoryDiskSpace</string>
|
|
25
|
+
<key>NSPrivacyAccessedAPITypeReasons</key>
|
|
26
|
+
<array>
|
|
27
|
+
<string>E174.1</string>
|
|
28
|
+
</array>
|
|
29
|
+
</dict>
|
|
30
|
+
</array>
|
|
31
|
+
</dict>
|
|
32
|
+
</plist>
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import FoundationModels
|
|
3
|
+
|
|
4
|
+
struct MobileChatTurn: Sendable {
|
|
5
|
+
let role: String
|
|
6
|
+
let content: String
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public struct MobileInferenceError: LocalizedError {
|
|
10
|
+
public let code: String
|
|
11
|
+
public let message: String
|
|
12
|
+
public init(code: String, message: String) { self.code = code; self.message = message }
|
|
13
|
+
public var errorDescription: String? { message }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
enum AppleFoundationProvider {
|
|
17
|
+
static let maximumOutputTokens = 1024
|
|
18
|
+
|
|
19
|
+
/// Model features are diagnostic; hosts must only advertise features their
|
|
20
|
+
/// request protocol can carry. This adapter currently exposes text/tools.
|
|
21
|
+
static func modelCapabilities() -> [String: Bool] {
|
|
22
|
+
if #available(iOS 27.0, macOS 27.0, *) {
|
|
23
|
+
let model = SystemLanguageModel.default
|
|
24
|
+
return ["tools": model.capabilities.contains(.toolCalling),
|
|
25
|
+
"guidedGeneration": model.capabilities.contains(.guidedGeneration),
|
|
26
|
+
"vision": model.capabilities.contains(.vision),
|
|
27
|
+
"reasoning": model.capabilities.contains(.reasoning)]
|
|
28
|
+
}
|
|
29
|
+
return ["tools": true, "guidedGeneration": true, "vision": false, "reasoning": false]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static func availability() -> (reason: String?, contextTokens: Int) {
|
|
33
|
+
guard #available(iOS 26.0, macOS 26.0, *) else {
|
|
34
|
+
return ("Apple on-device AI requires iOS 26 or macOS 26 or later.", 4096)
|
|
35
|
+
}
|
|
36
|
+
// Explicitly choose the on-device model, including on SDKs that also
|
|
37
|
+
// expose cloud models. Never substitute a different model/provider.
|
|
38
|
+
let model = SystemLanguageModel.default
|
|
39
|
+
// Before OS 27 this is a fixed 4096; from 27 it is the model's real
|
|
40
|
+
// window (larger on newer devices), which every budget below follows.
|
|
41
|
+
let reportedContext = model.contextSize
|
|
42
|
+
// An unavailable system model can report no context at all (including
|
|
43
|
+
// on the simulator). Keep its descriptor valid for the shared provider
|
|
44
|
+
// list; the availability reason still prevents inference.
|
|
45
|
+
let context = reportedContext > maximumOutputTokens ? reportedContext : 4096
|
|
46
|
+
switch model.availability {
|
|
47
|
+
case .available:
|
|
48
|
+
guard reportedContext > maximumOutputTokens else {
|
|
49
|
+
return ("Apple's on-device model did not report a usable context window.", context)
|
|
50
|
+
}
|
|
51
|
+
return (nil, context)
|
|
52
|
+
case .unavailable(.deviceNotEligible):
|
|
53
|
+
return ("This device does not support Apple Intelligence.", context)
|
|
54
|
+
case .unavailable(.appleIntelligenceNotEnabled):
|
|
55
|
+
return ("Enable Apple Intelligence in Settings to use Apple on-device AI.", context)
|
|
56
|
+
case .unavailable(.modelNotReady):
|
|
57
|
+
// The SDK does not expose download progress or a download command.
|
|
58
|
+
return ("Apple's on-device model is not ready. The system manages its download and preparation.", context)
|
|
59
|
+
case .unavailable:
|
|
60
|
+
return ("Apple on-device AI is currently unavailable on this device.", context)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static func requireContextBudget(promptTokens: Int, maxTokens: Int, contextSize: Int, modelContext: Int) throws {
|
|
65
|
+
let limit = min(contextSize, modelContext)
|
|
66
|
+
guard contextSize >= 512, contextSize <= modelContext, (1...maximumOutputTokens).contains(maxTokens),
|
|
67
|
+
modelContext > 0, promptTokens >= 0, promptTokens <= limit - maxTokens - 256 else {
|
|
68
|
+
throw MobileInferenceError(code: "CONTEXT_LIMIT", message: "This conversation exceeds Apple on-device AI's context budget. Start a new conversation.")
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// Apple's generation errors carry no user-facing description ("error -1").
|
|
73
|
+
@available(iOS 26.0, macOS 26.0, *)
|
|
74
|
+
static func described(_ error: Error) -> Error {
|
|
75
|
+
if #available(iOS 27.0, macOS 27.0, *) {
|
|
76
|
+
if let error = error as? LanguageModelError {
|
|
77
|
+
switch error {
|
|
78
|
+
case .contextSizeExceeded:
|
|
79
|
+
return MobileInferenceError(code: "CONTEXT_LIMIT", message: "This conversation exceeds Apple on-device AI's context budget. Start a new conversation.")
|
|
80
|
+
case .guardrailViolation, .refusal:
|
|
81
|
+
return MobileInferenceError(code: "GUARDRAIL", message: "Apple on-device AI declined this request.")
|
|
82
|
+
case .rateLimited:
|
|
83
|
+
return MobileInferenceError(code: "BUSY", message: "Apple on-device AI is busy. Try again in a moment.")
|
|
84
|
+
case .timeout:
|
|
85
|
+
return MobileInferenceError(code: "TIMEOUT", message: "Apple on-device AI timed out. Try again.")
|
|
86
|
+
case .unsupportedLanguageOrLocale, .unsupportedCapability, .unsupportedGenerationGuide, .unsupportedTranscriptContent:
|
|
87
|
+
return MobileInferenceError(code: "UNSUPPORTED", message: "Apple on-device AI does not support this request.")
|
|
88
|
+
@unknown default:
|
|
89
|
+
return MobileInferenceError(code: "INFERENCE_FAILED", message: "Apple on-device AI could not complete this response.")
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if error is SystemLanguageModel.Error {
|
|
93
|
+
return MobileInferenceError(code: "UNAVAILABLE", message: "Apple's on-device model is not ready. The system manages its download and preparation.")
|
|
94
|
+
}
|
|
95
|
+
if error is LanguageModelSession.Error {
|
|
96
|
+
return MobileInferenceError(code: "BUSY", message: "Apple on-device AI is busy. Try again in a moment.")
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
guard let error = error as? LanguageModelSession.GenerationError else { return error }
|
|
100
|
+
NSLog("[GezelRuntime] Apple on-device generation failed: %@", String(describing: error))
|
|
101
|
+
switch error {
|
|
102
|
+
case .exceededContextWindowSize:
|
|
103
|
+
return MobileInferenceError(code: "CONTEXT_LIMIT", message: "This conversation exceeds Apple on-device AI's context budget. Start a new conversation.")
|
|
104
|
+
case .guardrailViolation:
|
|
105
|
+
return MobileInferenceError(code: "GUARDRAIL", message: "Apple on-device AI's safety guardrails declined this request.")
|
|
106
|
+
case .decodingFailure:
|
|
107
|
+
return MobileInferenceError(code: "INFERENCE_FAILED", message: "Apple on-device AI produced a response or tool call it could not complete.")
|
|
108
|
+
case .assetsUnavailable:
|
|
109
|
+
return MobileInferenceError(code: "UNAVAILABLE", message: "Apple's on-device model is not ready. The system manages its download and preparation.")
|
|
110
|
+
case .rateLimited, .concurrentRequests:
|
|
111
|
+
return MobileInferenceError(code: "BUSY", message: "Apple on-device AI is busy. Try again in a moment.")
|
|
112
|
+
case .unsupportedLanguageOrLocale:
|
|
113
|
+
return MobileInferenceError(code: "UNSUPPORTED", message: "Apple on-device AI does not support this language.")
|
|
114
|
+
default:
|
|
115
|
+
return MobileInferenceError(code: "INFERENCE_FAILED", message: "Apple on-device AI could not complete this response.")
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/// The request as a Foundation Models transcript: instructions (with tool
|
|
120
|
+
/// definitions) first, then the conversation; the last user turn is the prompt.
|
|
121
|
+
@available(iOS 26.0, macOS 26.0, *)
|
|
122
|
+
static func transcript(
|
|
123
|
+
turns: [MobileChatTurn], tools toolSpecs: [[String: Any]],
|
|
124
|
+
invoke: @escaping @Sendable (String, String) async throws -> NativeToolReply
|
|
125
|
+
) throws -> (entries: [Transcript.Entry], final: MobileChatTurn, tools: [AppleBridgedTool]) {
|
|
126
|
+
guard turns.count <= 64, turns.reduce(0, { $0 + $1.content.utf8.count }) <= 32_768,
|
|
127
|
+
let final = turns.last, final.role == "user" else {
|
|
128
|
+
throw MobileInferenceError(code: "CONTEXT_LIMIT", message: "This conversation is too long for Apple on-device AI. Start a new conversation.")
|
|
129
|
+
}
|
|
130
|
+
var entries: [Transcript.Entry] = []
|
|
131
|
+
var instructions: [String] = []
|
|
132
|
+
var sawConversation = false
|
|
133
|
+
for turn in turns.dropLast() {
|
|
134
|
+
let segment = Transcript.Segment.text(.init(content: turn.content))
|
|
135
|
+
switch turn.role {
|
|
136
|
+
case "system":
|
|
137
|
+
guard !sawConversation else {
|
|
138
|
+
throw MobileInferenceError(code: "INVALID_ARGUMENT", message: "System instructions must precede the conversation.")
|
|
139
|
+
}
|
|
140
|
+
instructions.append(turn.content)
|
|
141
|
+
case "user":
|
|
142
|
+
sawConversation = true
|
|
143
|
+
entries.append(.prompt(.init(segments: [segment])))
|
|
144
|
+
case "assistant":
|
|
145
|
+
sawConversation = true
|
|
146
|
+
entries.append(.response(.init(assetIDs: [], segments: [segment])))
|
|
147
|
+
default:
|
|
148
|
+
throw MobileInferenceError(code: "INVALID_ARGUMENT", message: "Unsupported conversation role.")
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
guard toolSpecs.count <= 64 else {
|
|
152
|
+
throw MobileInferenceError(code: "INVALID_ARGUMENT", message: "Too many tools for one request.")
|
|
153
|
+
}
|
|
154
|
+
let tools = try toolSpecs.map { try AppleBridgedTool($0, invoke: invoke) }
|
|
155
|
+
guard Set(tools.map(\.name)).count == tools.count else {
|
|
156
|
+
throw MobileInferenceError(code: "INVALID_ARGUMENT", message: "Tool names must be unique.")
|
|
157
|
+
}
|
|
158
|
+
if !instructions.isEmpty || !tools.isEmpty {
|
|
159
|
+
// The model reads tool definitions from the instructions entry, as
|
|
160
|
+
// LanguageModelSession(tools:instructions:) arranges them itself.
|
|
161
|
+
entries.insert(.instructions(.init(
|
|
162
|
+
segments: instructions.isEmpty ? [] : [.text(.init(content: instructions.joined(separator: "\n\n")))],
|
|
163
|
+
toolDefinitions: tools.map { Transcript.ToolDefinition(tool: $0) })), at: 0)
|
|
164
|
+
}
|
|
165
|
+
return (entries, final, tools)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/// Exact prompt size, tool definitions included, for callers budgeting a request.
|
|
169
|
+
@available(iOS 26.4, macOS 26.4, *)
|
|
170
|
+
static func countTokens(turns: [MobileChatTurn], tools: [[String: Any]]) async throws -> Int {
|
|
171
|
+
let request = try transcript(turns: turns, tools: tools) { _, _ in throw CancellationError() }
|
|
172
|
+
let final = Transcript.Entry.prompt(.init(segments: [.text(.init(content: request.final.content))]))
|
|
173
|
+
return try await SystemLanguageModel.default.tokenCount(for: request.entries + [final])
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/// `tools` run inside Apple's own tool loop and call back through `invoke`,
|
|
177
|
+
/// where the app's shared tool loop records and executes each call. A tool
|
|
178
|
+
/// ending the turn (a handoff, a question) stops generation as a normal stop.
|
|
179
|
+
/// `schema` (a `NativeToolSchema` node) constrains the reply with guided
|
|
180
|
+
/// generation; that reply is sent as one delta of JSON text at the end.
|
|
181
|
+
@available(iOS 26.0, macOS 26.0, *)
|
|
182
|
+
static func generate(
|
|
183
|
+
turns: [MobileChatTurn], maxTokens: Int, contextSize: Int,
|
|
184
|
+
tools toolSpecs: [[String: Any]] = [],
|
|
185
|
+
schema: [String: Any]? = nil,
|
|
186
|
+
invoke: @escaping @Sendable (String, String) async throws -> NativeToolReply = { _, _ in throw CancellationError() },
|
|
187
|
+
onUsage: ([String: Int]) -> Void = { _ in },
|
|
188
|
+
onDelta: (String) throws -> Void
|
|
189
|
+
) async throws -> String {
|
|
190
|
+
let readiness = availability()
|
|
191
|
+
if let reason = readiness.reason { throw MobileInferenceError(code: "UNAVAILABLE", message: reason) }
|
|
192
|
+
guard (1...maximumOutputTokens).contains(maxTokens) else {
|
|
193
|
+
throw MobileInferenceError(code: "CONTEXT_LIMIT", message: "This conversation is too long for Apple on-device AI. Start a new conversation.")
|
|
194
|
+
}
|
|
195
|
+
let (entries, final, tools) = try transcript(turns: turns, tools: toolSpecs, invoke: invoke)
|
|
196
|
+
// Built before admission, so a schema Apple cannot use fails before
|
|
197
|
+
// any prompt is spent.
|
|
198
|
+
var responseSchema: GenerationSchema?
|
|
199
|
+
if let schema {
|
|
200
|
+
do {
|
|
201
|
+
responseSchema = try GenerationSchema(root: try NativeToolNode(schema).dynamicSchema(name: "response"), dependencies: [])
|
|
202
|
+
} catch {
|
|
203
|
+
throw MobileInferenceError(code: "INVALID_ARGUMENT", message: "Apple on-device AI cannot follow this response format's schema.")
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
let model = SystemLanguageModel.default
|
|
207
|
+
let finalEntry = Transcript.Entry.prompt(.init(segments: [.text(.init(content: final.content))]))
|
|
208
|
+
let promptTokens: Int
|
|
209
|
+
if #available(iOS 26.4, macOS 26.4, *) {
|
|
210
|
+
// Counting the transcript includes the tool definitions it carries.
|
|
211
|
+
promptTokens = try await model.tokenCount(for: entries + [finalEntry])
|
|
212
|
+
} else {
|
|
213
|
+
// Older systems expose no tokenizer. UTF-8 bytes plus entry overhead
|
|
214
|
+
// conservatively bound this adapter's admission; no history is cut.
|
|
215
|
+
let toolBytes = (try? JSONSerialization.data(withJSONObject: toolSpecs).count) ?? 0
|
|
216
|
+
promptTokens = turns.reduce(toolBytes, { $0 + $1.content.utf8.count + 32 })
|
|
217
|
+
}
|
|
218
|
+
try requireContextBudget(promptTokens: promptTokens, maxTokens: maxTokens, contextSize: contextSize, modelContext: readiness.contextTokens)
|
|
219
|
+
try Task.checkCancellation()
|
|
220
|
+
// Fresh state on every turn. All prior messages come from the durable
|
|
221
|
+
// app transcript; no hidden provider session can drift after restart.
|
|
222
|
+
let session = LanguageModelSession(model: model, tools: tools, transcript: Transcript(entries: entries))
|
|
223
|
+
let options = GenerationOptions(samplingMode: .greedy, maximumResponseTokens: maxTokens)
|
|
224
|
+
var text = ""
|
|
225
|
+
var reachedTokenLimit = false
|
|
226
|
+
var guidedComplete = true
|
|
227
|
+
func awaitProviderRelease() async {
|
|
228
|
+
// A cancellation request is not a release barrier. Keep the plugin
|
|
229
|
+
// busy until the SDK reports its session is no longer responding.
|
|
230
|
+
while session.isResponding {
|
|
231
|
+
await Task.detached { try? await Task.sleep(nanoseconds: 50_000_000) }.value
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
do {
|
|
235
|
+
if let responseSchema {
|
|
236
|
+
// Partial structured content is not a prefix of the finished
|
|
237
|
+
// JSON (the SDK renders each snapshot whole), so it is not streamed.
|
|
238
|
+
for try await snapshot in session.streamResponse(to: final.content, schema: responseSchema, options: options) {
|
|
239
|
+
try Task.checkCancellation()
|
|
240
|
+
text = snapshot.content.jsonString
|
|
241
|
+
guard text.utf8.count <= 65_536 else {
|
|
242
|
+
throw MobileInferenceError(code: "RESOURCE_LIMIT", message: "Apple on-device AI exceeded this app's response size limit.")
|
|
243
|
+
}
|
|
244
|
+
guidedComplete = snapshot.content.isComplete
|
|
245
|
+
if #available(iOS 27.0, macOS 27.0, *) {
|
|
246
|
+
reachedTokenLimit = snapshot.usage.output.totalTokenCount >= maxTokens
|
|
247
|
+
onUsage(["inputTokens": snapshot.usage.input.totalTokenCount,
|
|
248
|
+
"outputTokens": snapshot.usage.output.totalTokenCount])
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if !text.isEmpty { try onDelta(text) }
|
|
252
|
+
} else {
|
|
253
|
+
for try await snapshot in session.streamResponse(to: final.content, options: options) {
|
|
254
|
+
try Task.checkCancellation()
|
|
255
|
+
let next = snapshot.content
|
|
256
|
+
guard next.utf8.count <= 65_536 else {
|
|
257
|
+
throw MobileInferenceError(code: "RESOURCE_LIMIT", message: "Apple on-device AI exceeded this app's response size limit.")
|
|
258
|
+
}
|
|
259
|
+
guard next.hasPrefix(text) else {
|
|
260
|
+
throw MobileInferenceError(code: "INFERENCE_FAILED", message: "Apple on-device AI revised text that had already been streamed.")
|
|
261
|
+
}
|
|
262
|
+
let delta = String(next.dropFirst(text.count))
|
|
263
|
+
if !delta.isEmpty { try onDelta(delta) }
|
|
264
|
+
text = next
|
|
265
|
+
if #available(iOS 27.0, macOS 27.0, *) {
|
|
266
|
+
reachedTokenLimit = snapshot.usage.output.totalTokenCount >= maxTokens
|
|
267
|
+
onUsage(["inputTokens": snapshot.usage.input.totalTokenCount,
|
|
268
|
+
"outputTokens": snapshot.usage.output.totalTokenCount])
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
} catch let error as LanguageModelSession.ToolCallError where error.underlyingError is NativeToolTurnEnded {
|
|
273
|
+
// The app ended the turn after a tool result; nothing was revised.
|
|
274
|
+
await awaitProviderRelease()
|
|
275
|
+
try Task.checkCancellation()
|
|
276
|
+
return "stop"
|
|
277
|
+
} catch {
|
|
278
|
+
withUnsafeCurrentTask { $0?.cancel() }
|
|
279
|
+
await awaitProviderRelease()
|
|
280
|
+
// Overflow while appending a tool's output surfaces as that tool's error.
|
|
281
|
+
if let error = error as? LanguageModelSession.ToolCallError { throw described(error.underlyingError) }
|
|
282
|
+
throw described(error)
|
|
283
|
+
}
|
|
284
|
+
await awaitProviderRelease()
|
|
285
|
+
try Task.checkCancellation()
|
|
286
|
+
// iOS 26 exposes no terminal stop reason/token usage for a text stream.
|
|
287
|
+
// Its normal completion is reported as stop; the SDK still enforces
|
|
288
|
+
// maximumResponseTokens, and the host enforces the byte/time budgets.
|
|
289
|
+
// A guided reply that stopped unfinished was cut off by that limit.
|
|
290
|
+
return reachedTokenLimit || !guidedComplete ? "length" : "stop"
|
|
291
|
+
}
|
|
292
|
+
}
|