@bendyline/gezel-capacitor 0.0.0-stage → 1.0.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.
Files changed (121) hide show
  1. package/LICENSE +21 -0
  2. package/Package.swift +26 -0
  3. package/README.md +230 -2
  4. package/android/build.gradle +30 -0
  5. package/android/src/main/AndroidManifest.xml +1 -0
  6. package/android/src/main/java/com/bendyline/gezel/capacitor/GezelKnowledgePlugin.java +153 -0
  7. package/android/src/main/java/com/bendyline/gezel/capacitor/GezelOfflineSpeechPlugin.java +216 -0
  8. package/android/src/main/java/com/bendyline/gezel/capacitor/GezelRuntimePlugin.java +84 -0
  9. package/android/src/main/java/com/bendyline/gezel/capacitor/KnowledgeFiles.java +101 -0
  10. package/android/src/main/java/com/bendyline/gezel/capacitor/SpeechModels.java +230 -0
  11. package/android/src/main/java/com/bendyline/gezel/speech/SpeechRuntime.java +38 -0
  12. package/dist/index.d.ts +561 -0
  13. package/dist/index.js +7512 -0
  14. package/dist/knowledge-worker.js +211 -0
  15. package/dist/licenses/npm/manifest.json +76 -0
  16. package/dist/licenses/npm/texts/1583809bf1155510d2961f94aa9bca6dc4b44b6f59fd773de33bd6c20860d781.txt +29 -0
  17. package/dist/licenses/npm/texts/1cd39aa4a566eba49eb206b969ef423295e79b50503df567bbdb2e8877d78892.txt +8 -0
  18. package/dist/licenses/npm/texts/3f1189b28e3866e0d979968d466b78f813f76827cfdca1fbb124cc0a5c8841f8.txt +21 -0
  19. package/dist/licenses/npm/texts/5b37224c080cdcc97c871ada971c224e9926370fe74f11b539aa1cf9f3b1aca1.txt +21 -0
  20. package/dist/licenses/npm/texts/c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4.txt +201 -0
  21. package/dist/sqlite3.wasm +0 -0
  22. package/embedding-manifest.json +210 -0
  23. package/ios/Sources/GezelCapacitor/GezelKnowledgePlugin.swift +200 -0
  24. package/ios/Sources/GezelCapacitor/GezelOfflineSpeechPlugin.swift +484 -0
  25. package/ios/Sources/GezelCapacitor/GezelRuntimePlugin.swift +102 -0
  26. package/kokoro-lexicon/LICENSE +202 -0
  27. package/kokoro-lexicon/lexicon-gb-en.txt.gz +0 -0
  28. package/kokoro-lexicon/lexicon-us-en.txt.gz +0 -0
  29. package/native/android/LICENSE-gezel.txt +21 -0
  30. package/native/android/PUBLIC-DISTRIBUTION.md +97 -0
  31. package/native/android/THIRD_PARTY_LICENSES/LICENSE-arm-optimized-routines-MIT-OR-Apache-2.0.txt +249 -0
  32. package/native/android/THIRD_PARTY_LICENSES/LICENSE-cpp-httplib-MIT.txt +22 -0
  33. package/native/android/THIRD_PARTY_LICENSES/LICENSE-fp16-MIT.txt +11 -0
  34. package/native/android/THIRD_PARTY_LICENSES/LICENSE-ggml-MIT.txt +21 -0
  35. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-llamafile-sgemm-MIT.txt +21 -0
  36. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-openvino-APACHE.txt +201 -0
  37. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-pillow-MIT-CMU.txt +30 -0
  38. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-rotate-bits-MIT.txt +21 -0
  39. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-unicode-data-Unicode-3.0.txt +39 -0
  40. package/native/android/THIRD_PARTY_LICENSES/LICENSE-nlohmann-json-MIT.txt +21 -0
  41. package/native/android/THIRD_PARTY_LICENSES/LICENSE-nvidia-cub-BSD-3-Clause.txt +24 -0
  42. package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-headers-Apache-2.0-OR-MIT.txt +33 -0
  43. package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-APACHE.txt +201 -0
  44. package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-cjson-MIT.txt +32 -0
  45. package/native/android/THIRD_PARTY_LICENSES/LICENSE-yarn-MIT.txt +21 -0
  46. package/native/android/THIRD_PARTY_LICENSES/manifest.json +21 -0
  47. package/native/android/engine-manifest.json +88 -0
  48. package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.aar +0 -0
  49. package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.aar.sha256 +1 -0
  50. package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.pom +8 -0
  51. package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.pom.sha256 +1 -0
  52. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar +0 -0
  53. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.md5 +1 -0
  54. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha1 +1 -0
  55. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha256 +1 -0
  56. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha512 +1 -0
  57. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar +0 -0
  58. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.md5 +1 -0
  59. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha1 +1 -0
  60. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha256 +1 -0
  61. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha512 +1 -0
  62. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module +118 -0
  63. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.md5 +1 -0
  64. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha1 +1 -0
  65. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha256 +1 -0
  66. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha512 +1 -0
  67. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom +51 -0
  68. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.md5 +1 -0
  69. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha1 +1 -0
  70. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha256 +1 -0
  71. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha512 +1 -0
  72. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml +13 -0
  73. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.md5 +1 -0
  74. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha1 +1 -0
  75. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha256 +1 -0
  76. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha512 +1 -0
  77. package/native/android/sdk-manifest.json +98 -0
  78. package/native/ios/GezelLlama.xcframework/Info.plist +47 -0
  79. package/native/ios/GezelLlama.xcframework/ios-arm64/Headers/gezel_llama.h +290 -0
  80. package/native/ios/GezelLlama.xcframework/ios-arm64/Headers/module.modulemap +9 -0
  81. package/native/ios/GezelLlama.xcframework/ios-arm64/libGezelLlama.a +0 -0
  82. package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/Headers/gezel_llama.h +290 -0
  83. package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/Headers/module.modulemap +9 -0
  84. package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/libGezelLlama.a +0 -0
  85. package/native/ios/LICENSE-gezel.txt +21 -0
  86. package/native/ios/LICENSE-ggml.txt +21 -0
  87. package/native/ios/LICENSE-llama-cpp.txt +21 -0
  88. package/native/ios/PUBLIC-DISTRIBUTION.md +97 -0
  89. package/native/ios/Package.swift +17 -0
  90. package/native/ios/Sources/GezelModelStorage/MobileModelStore.swift +286 -0
  91. package/native/ios/Sources/GezelModelStorage/ModelDownloadHTTP.swift +55 -0
  92. package/native/ios/Sources/GezelModelStorage/ModelDownloadSource.swift +46 -0
  93. package/native/ios/Sources/GezelModelStorage/ModelDownloads.swift +289 -0
  94. package/native/ios/Sources/GezelModelStorage/PrivacyInfo.xcprivacy +32 -0
  95. package/native/ios/Sources/GezelRuntime/AppleFoundationProvider.swift +292 -0
  96. package/native/ios/Sources/GezelRuntime/AppleNativeTools.swift +152 -0
  97. package/native/ios/Sources/GezelRuntime/GezelNativeRuntime.swift +1349 -0
  98. package/native/ios/Sources/GezelRuntime/NativeCall.swift +35 -0
  99. package/native/ios/Sources/GezelRuntime/PrivacyInfo.xcprivacy +31 -0
  100. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-arm-optimized-routines-MIT-OR-Apache-2.0.txt +249 -0
  101. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-cpp-httplib-MIT.txt +22 -0
  102. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-fp16-MIT.txt +11 -0
  103. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-ggml-MIT.txt +21 -0
  104. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-llamafile-sgemm-MIT.txt +21 -0
  105. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-openvino-APACHE.txt +201 -0
  106. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-pillow-MIT-CMU.txt +30 -0
  107. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-rotate-bits-MIT.txt +21 -0
  108. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-unicode-data-Unicode-3.0.txt +39 -0
  109. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-nlohmann-json-MIT.txt +21 -0
  110. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-nvidia-cub-BSD-3-Clause.txt +24 -0
  111. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-headers-Apache-2.0-OR-MIT.txt +33 -0
  112. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-APACHE.txt +201 -0
  113. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-cjson-MIT.txt +32 -0
  114. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-yarn-MIT.txt +21 -0
  115. package/native/ios/THIRD_PARTY_LICENSES/manifest.json +21 -0
  116. package/native/ios/engine-manifest.json +102 -0
  117. package/native/ios/sdk-manifest.json +89 -0
  118. package/package.json +94 -3
  119. package/scripts/embedding-package.d.mts +23 -0
  120. package/scripts/embedding-package.mjs +185 -0
  121. package/scripts/stage-native.mjs +168 -0
@@ -0,0 +1,200 @@
1
+ import Capacitor
2
+ import Foundation
3
+ import UIKit
4
+ import os
5
+ #if canImport(GezelSpeech)
6
+ import GezelSpeech
7
+ #endif
8
+
9
+ /// Knowledge catalog archives on the device. Each catalog is one verified
10
+ /// `.gezk` download that the web view's reader opens in place, in byte ranges
11
+ /// served by Capacitor's file handler; nothing is extracted. JavaScript chooses
12
+ /// catalogs from its pinned snapshot; this plugin only stores what it is given.
13
+ /// Each catalog's query model downloads into the same store and runs on the
14
+ /// speech framework's ONNX Runtime (`embed`).
15
+ @objc(GezelKnowledgePlugin)
16
+ public final class GezelKnowledgePlugin: CAPPlugin, CAPBridgedPlugin {
17
+ public let identifier = "GezelKnowledgePlugin"
18
+ public let jsName = "GezelKnowledge"
19
+ public let pluginMethods: [CAPPluginMethod] = [
20
+ CAPPluginMethod(name: "info", returnType: CAPPluginReturnPromise),
21
+ CAPPluginMethod(name: "install", returnType: CAPPluginReturnPromise),
22
+ CAPPluginMethod(name: "installModel", returnType: CAPPluginReturnPromise),
23
+ CAPPluginMethod(name: "embed", returnType: CAPPluginReturnPromise),
24
+ CAPPluginMethod(name: "cancelInstall", returnType: CAPPluginReturnPromise),
25
+ CAPPluginMethod(name: "remove", returnType: CAPPluginReturnPromise)
26
+ ]
27
+ /// A sanity bound; the store refuses any download the device has no room for.
28
+ private static let maxArchive: Int64 = 32 << 30
29
+ private let downloads = DispatchQueue(label: "com.bendyline.gezel.knowledge.downloads", qos: .utility)
30
+ private let encoder = DispatchQueue(label: "com.bendyline.gezel.knowledge.encoder", qos: .userInitiated)
31
+ private let lock = NSLock()
32
+ private let store = SpeechModelStore(folder: "gezel-knowledge/catalogs", maxFile: GezelKnowledgePlugin.maxArchive)
33
+ private var engine: OpaquePointer?
34
+ /// The model the encoder holds, so only a cold load checks memory.
35
+ private var warm: String?
36
+ private var downloading: String?
37
+ private var backgrounded = false, paused = false
38
+ private var observers: [NSObjectProtocol] = []
39
+
40
+ public override func load() {
41
+ let center = NotificationCenter.default
42
+ observers.append(center.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { [weak self] _ in
43
+ self?.enterBackground()
44
+ })
45
+ observers.append(center.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: nil) { [weak self] _ in
46
+ guard let self else { return }
47
+ self.locked { self.backgrounded = false }
48
+ })
49
+ }
50
+ deinit {
51
+ observers.forEach(NotificationCenter.default.removeObserver)
52
+ store.cancel()
53
+ #if canImport(GezelSpeech)
54
+ if let engine { gezel_speech_destroy(engine) }
55
+ #endif
56
+ }
57
+ private func locked<T>(_ body: () -> T) -> T { lock.lock(); defer { lock.unlock() }; return body() }
58
+
59
+ @objc public func info(_ call: CAPPluginCall) {
60
+ // Capacitor serves app files at /_capacitor_file_<absolute path>, honoring Range.
61
+ guard var base = bridge?.config.localURL.absoluteString,
62
+ let root = store.root.standardizedFileURL.path.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
63
+ call.reject("Knowledge catalogs could not be read", "failed"); return
64
+ }
65
+ if base.hasSuffix("/") { base.removeLast() }
66
+ do {
67
+ var result: [String: Any] = ["baseUrl": base + CapacitorBridge.fileStartIdentifier + root, "catalogs": try store.list()]
68
+ if let downloading = locked({ downloading }) { result["downloading"] = downloading }
69
+ call.resolve(result)
70
+ } catch { call.reject("Knowledge catalogs could not be read", "failed") }
71
+ }
72
+
73
+ @objc public func install(_ call: CAPPluginCall) {
74
+ guard let id = call.getString("id"), SpeechModelStore.valid(id: id), let version = call.getString("version"),
75
+ let url = call.getString("url"), let sha = call.getString("sha256"), let size = call.options["size"] as? NSNumber else {
76
+ call.reject("Invalid knowledge catalog", "invalid-input"); return
77
+ }
78
+ let file: [String: Any] = ["name": "\(version)/catalog.gezk", "url": url, "sha256": sha, "size": size]
79
+ download(call, id, [file]) { [store] in
80
+ // One version per catalog: a verified update replaces the archive it supersedes.
81
+ // Pruning only now keeps an interrupted download's partial file for resuming.
82
+ let folder = store.root.appendingPathComponent(id, isDirectory: true)
83
+ for entry in (try? FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: [.isDirectoryKey])) ?? []
84
+ where entry.lastPathComponent != version && (try? entry.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) == true {
85
+ try? FileManager.default.removeItem(at: entry)
86
+ }
87
+ }
88
+ }
89
+
90
+ /// A catalog profile's query model, verified file by file like a catalog.
91
+ @objc public func installModel(_ call: CAPPluginCall) {
92
+ guard let id = call.getString("id"), SpeechModelStore.valid(id: id), let files = call.getArray("files") as? [[String: Any]] else {
93
+ call.reject("Invalid query model", "invalid-input"); return
94
+ }
95
+ download(call, id, files)
96
+ }
97
+
98
+ private func download(_ call: CAPPluginCall, _ id: String, _ files: [[String: Any]], then finish: @escaping () -> Void = {}) {
99
+ let refusal: (String, String)? = locked {
100
+ if downloading != nil { return ("Another download is running", "busy") }
101
+ if backgrounded { return ("Knowledge downloads run in the foreground", "paused") }
102
+ downloading = id; paused = false
103
+ return nil
104
+ }
105
+ if let (message, code) = refusal { call.reject(message, code); return }
106
+ downloads.async { [self] in
107
+ defer { locked { downloading = nil } }
108
+ do {
109
+ try store.install(id, files) { received, total in
110
+ self.notifyListeners("knowledgeDownload", data: ["id": id, "receivedBytes": received, "totalBytes": total])
111
+ }
112
+ finish()
113
+ call.resolve(["id": id])
114
+ } catch SpeechModelStore.Failure.cancelled {
115
+ let wasPaused = locked { paused }
116
+ call.reject(wasPaused ? "Download paused when the app left the foreground" : "Download cancelled", wasPaused ? "paused" : "cancelled")
117
+ } catch let SpeechModelStore.Failure.failed(code, message) {
118
+ call.reject(message, code)
119
+ } catch {
120
+ call.reject(error.localizedDescription, "interrupted")
121
+ }
122
+ }
123
+ }
124
+
125
+ /// A query's unit vector from the installed model; token ids come from the profile's tokenizer.
126
+ @objc public func embed(_ call: CAPPluginCall) {
127
+ if let reason = GezelOfflineSpeechPlugin.unavailableReason { call.reject(reason, "unavailable"); return }
128
+ encoder.async { [self] in
129
+ #if canImport(GezelSpeech)
130
+ do {
131
+ let model = try store.file(call.getString("model"), call.getString("file"))
132
+ guard let encoded = call.getArray("ids") as? [NSNumber], (1...8192).contains(encoded.count) else {
133
+ throw SpeechModelStore.Failure.failed("invalid-input", "Invalid text for the query model")
134
+ }
135
+ let ids = encoded.map(\.int64Value)
136
+ lock.lock()
137
+ if engine == nil { engine = gezel_speech_create() }
138
+ let handle = engine, cold = warm != model.path
139
+ lock.unlock()
140
+ guard let handle else { throw SpeechModelStore.Failure.failed("resources", "Not enough memory for knowledge search") }
141
+ if cold { try checkMemory(for: model) }
142
+ gezel_speech_begin(handle)
143
+ var vector: UnsafeMutablePointer<Float>?
144
+ var dimensions = 0
145
+ let status = ids.withUnsafeBufferPointer {
146
+ gezel_speech_embed(handle, model.path, $0.baseAddress, $0.count, &vector, &dimensions)
147
+ }
148
+ defer { gezel_speech_free(vector) }
149
+ if status != 0 { throw SpeechModelStore.Failure.failed("failed", String(cString: gezel_speech_error(handle))) }
150
+ locked { warm = model.path }
151
+ call.resolve(["vector": UnsafeBufferPointer(start: vector, count: dimensions).map(Double.init)])
152
+ } catch let SpeechModelStore.Failure.failed(code, message) {
153
+ call.reject(message, code)
154
+ } catch {
155
+ call.reject(error.localizedDescription, "failed")
156
+ }
157
+ #else
158
+ call.reject(GezelOfflineSpeechPlugin.unavailableReason ?? "", "unavailable")
159
+ #endif
160
+ }
161
+ }
162
+
163
+ private func checkMemory(for model: URL) throws {
164
+ // Zero means no limit is known (the simulator); only a real budget can refuse.
165
+ let headroom = os_proc_available_memory()
166
+ let size = (try? model.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
167
+ if headroom > 0 && headroom < size + 256 * 1024 * 1024 {
168
+ throw SpeechModelStore.Failure.failed("resources", "Not enough memory for knowledge search. Close other apps and try again.")
169
+ }
170
+ }
171
+
172
+ @objc public func cancelInstall(_ call: CAPPluginCall) { store.cancel(); call.resolve() }
173
+
174
+ @objc public func remove(_ call: CAPPluginCall) {
175
+ let id = call.getString("id") ?? ""
176
+ if locked({ id == downloading }) { call.reject("Cancel the download before removing this catalog", "busy"); return }
177
+ downloads.async { [self] in
178
+ do { try store.remove(id); call.resolve() }
179
+ catch let SpeechModelStore.Failure.failed(code, message) { call.reject(message, code) }
180
+ catch { call.reject("The catalog could not be removed", "failed") }
181
+ }
182
+ }
183
+
184
+ private func enterBackground() {
185
+ locked {
186
+ backgrounded = true
187
+ if downloading != nil { paused = true }
188
+ }
189
+ store.cancel()
190
+ // The query model holds hundreds of megabytes; load it again on the next search.
191
+ encoder.async { [self] in
192
+ #if canImport(GezelSpeech)
193
+ locked {
194
+ if let engine { gezel_speech_release(engine) }
195
+ warm = nil
196
+ }
197
+ #endif
198
+ }
199
+ }
200
+ }