llama-cpp-capacitor 0.0.13 → 0.0.21

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 (34) hide show
  1. package/LlamaCpp.podspec +17 -17
  2. package/Package.swift +27 -27
  3. package/README.md +717 -574
  4. package/android/build.gradle +88 -69
  5. package/android/src/main/AndroidManifest.xml +2 -2
  6. package/android/src/main/CMakeLists-arm64.txt +131 -0
  7. package/android/src/main/CMakeLists-x86_64.txt +135 -0
  8. package/android/src/main/CMakeLists.txt +35 -52
  9. package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCpp.java +956 -717
  10. package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCppPlugin.java +710 -590
  11. package/android/src/main/jni-utils.h +7 -7
  12. package/android/src/main/jni.cpp +868 -127
  13. package/cpp/{rn-completion.cpp → cap-completion.cpp} +202 -24
  14. package/cpp/{rn-completion.h → cap-completion.h} +22 -11
  15. package/cpp/{rn-llama.cpp → cap-llama.cpp} +81 -27
  16. package/cpp/{rn-llama.h → cap-llama.h} +32 -20
  17. package/cpp/{rn-mtmd.hpp → cap-mtmd.hpp} +15 -15
  18. package/cpp/{rn-tts.cpp → cap-tts.cpp} +12 -12
  19. package/cpp/{rn-tts.h → cap-tts.h} +14 -14
  20. package/cpp/ggml-cpu/ggml-cpu-impl.h +30 -0
  21. package/dist/docs.json +100 -3
  22. package/dist/esm/definitions.d.ts +45 -2
  23. package/dist/esm/definitions.js.map +1 -1
  24. package/dist/esm/index.d.ts +22 -0
  25. package/dist/esm/index.js +66 -3
  26. package/dist/esm/index.js.map +1 -1
  27. package/dist/plugin.cjs.js +71 -3
  28. package/dist/plugin.cjs.js.map +1 -1
  29. package/dist/plugin.js +71 -3
  30. package/dist/plugin.js.map +1 -1
  31. package/ios/Sources/LlamaCppPlugin/LlamaCpp.swift +596 -596
  32. package/ios/Sources/LlamaCppPlugin/LlamaCppPlugin.swift +591 -514
  33. package/ios/Tests/LlamaCppPluginTests/LlamaCppPluginTests.swift +15 -15
  34. package/package.json +111 -110
@@ -1,514 +1,591 @@
1
- import Foundation
2
- import Capacitor
3
-
4
- /**
5
- * Please read the Capacitor iOS Plugin Development Guide
6
- * here: https://capacitorjs.com/docs/plugins/ios
7
- */
8
- @objc(LlamaCppPlugin)
9
- public class LlamaCppPlugin: CAPPlugin, CAPBridgedPlugin {
10
- public let identifier = "LlamaCppPlugin"
11
- public let jsName = "LlamaCpp"
12
- public let pluginMethods: [CAPPluginMethod] = [
13
- // Core initialization and management
14
- CAPPluginMethod(name: "toggleNativeLog", returnType: CAPPluginReturnPromise),
15
- CAPPluginMethod(name: "setContextLimit", returnType: CAPPluginReturnPromise),
16
- CAPPluginMethod(name: "modelInfo", returnType: CAPPluginReturnPromise),
17
- CAPPluginMethod(name: "initContext", returnType: CAPPluginReturnPromise),
18
- CAPPluginMethod(name: "releaseContext", returnType: CAPPluginReturnPromise),
19
- CAPPluginMethod(name: "releaseAllContexts", returnType: CAPPluginReturnPromise),
20
-
21
- // Chat and completion
22
- CAPPluginMethod(name: "getFormattedChat", returnType: CAPPluginReturnPromise),
23
- CAPPluginMethod(name: "completion", returnType: CAPPluginReturnPromise),
24
- CAPPluginMethod(name: "stopCompletion", returnType: CAPPluginReturnPromise),
25
-
26
- // Session management
27
- CAPPluginMethod(name: "loadSession", returnType: CAPPluginReturnPromise),
28
- CAPPluginMethod(name: "saveSession", returnType: CAPPluginReturnPromise),
29
-
30
- // Tokenization
31
- CAPPluginMethod(name: "tokenize", returnType: CAPPluginReturnPromise),
32
- CAPPluginMethod(name: "detokenize", returnType: CAPPluginReturnPromise),
33
-
34
- // Embeddings and reranking
35
- CAPPluginMethod(name: "embedding", returnType: CAPPluginReturnPromise),
36
- CAPPluginMethod(name: "rerank", returnType: CAPPluginReturnPromise),
37
-
38
- // Benchmarking
39
- CAPPluginMethod(name: "bench", returnType: CAPPluginReturnPromise),
40
-
41
- // LoRA adapters
42
- CAPPluginMethod(name: "applyLoraAdapters", returnType: CAPPluginReturnPromise),
43
- CAPPluginMethod(name: "removeLoraAdapters", returnType: CAPPluginReturnPromise),
44
- CAPPluginMethod(name: "getLoadedLoraAdapters", returnType: CAPPluginReturnPromise),
45
-
46
- // Multimodal methods
47
- CAPPluginMethod(name: "initMultimodal", returnType: CAPPluginReturnPromise),
48
- CAPPluginMethod(name: "isMultimodalEnabled", returnType: CAPPluginReturnPromise),
49
- CAPPluginMethod(name: "getMultimodalSupport", returnType: CAPPluginReturnPromise),
50
- CAPPluginMethod(name: "releaseMultimodal", returnType: CAPPluginReturnPromise),
51
-
52
- // TTS methods
53
- CAPPluginMethod(name: "initVocoder", returnType: CAPPluginReturnPromise),
54
- CAPPluginMethod(name: "isVocoderEnabled", returnType: CAPPluginReturnPromise),
55
- CAPPluginMethod(name: "getFormattedAudioCompletion", returnType: CAPPluginReturnPromise),
56
- CAPPluginMethod(name: "getAudioCompletionGuideTokens", returnType: CAPPluginReturnPromise),
57
- CAPPluginMethod(name: "decodeAudioTokens", returnType: CAPPluginReturnPromise),
58
- CAPPluginMethod(name: "releaseVocoder", returnType: CAPPluginReturnPromise),
59
-
60
- // Events
61
- CAPPluginMethod(name: "addListener", returnType: CAPPluginReturnPromise),
62
- CAPPluginMethod(name: "removeAllListeners", returnType: CAPPluginReturnPromise)
63
- ]
64
-
65
- private let implementation = LlamaCpp()
66
-
67
- // MARK: - Core initialization and management
68
-
69
- @objc func toggleNativeLog(_ call: CAPPluginCall) {
70
- let enabled = call.getBool("enabled") ?? false
71
- implementation.toggleNativeLog(enabled: enabled) { result in
72
- switch result {
73
- case .success:
74
- call.resolve()
75
- case .failure(let error):
76
- call.reject(error.localizedDescription)
77
- }
78
- }
79
- }
80
-
81
- @objc func setContextLimit(_ call: CAPPluginCall) {
82
- let limit = call.getInt("limit") ?? 0
83
- implementation.setContextLimit(limit: limit) { result in
84
- switch result {
85
- case .success:
86
- call.resolve()
87
- case .failure(let error):
88
- call.reject(error.localizedDescription)
89
- }
90
- }
91
- }
92
-
93
- @objc func modelInfo(_ call: CAPPluginCall) {
94
- let path = call.getString("path") ?? ""
95
- let skip = call.getArray("skip", String.self) ?? []
96
-
97
- implementation.modelInfo(path: path, skip: skip) { result in
98
- switch result {
99
- case .success(let modelInfo):
100
- call.resolve(modelInfo)
101
- case .failure(let error):
102
- call.reject(error.localizedDescription)
103
- }
104
- }
105
- }
106
-
107
- @objc func initContext(_ call: CAPPluginCall) {
108
- let contextId = call.getInt("contextId") ?? 0
109
- let params = call.getObject("params") ?? [:]
110
-
111
- implementation.initContext(contextId: contextId, params: params) { result in
112
- switch result {
113
- case .success(let context):
114
- call.resolve(context)
115
- case .failure(let error):
116
- call.reject(error.localizedDescription)
117
- }
118
- }
119
- }
120
-
121
- @objc func releaseContext(_ call: CAPPluginCall) {
122
- let contextId = call.getInt("contextId") ?? 0
123
-
124
- implementation.releaseContext(contextId: contextId) { result in
125
- switch result {
126
- case .success:
127
- call.resolve()
128
- case .failure(let error):
129
- call.reject(error.localizedDescription)
130
- }
131
- }
132
- }
133
-
134
- @objc func releaseAllContexts(_ call: CAPPluginCall) {
135
- implementation.releaseAllContexts { result in
136
- switch result {
137
- case .success:
138
- call.resolve()
139
- case .failure(let error):
140
- call.reject(error.localizedDescription)
141
- }
142
- }
143
- }
144
-
145
- // MARK: - Chat and completion
146
-
147
- @objc func getFormattedChat(_ call: CAPPluginCall) {
148
- let contextId = call.getInt("contextId") ?? 0
149
- let messages = call.getString("messages") ?? ""
150
- let chatTemplate = call.getString("chatTemplate")
151
- let params = call.getObject("params")
152
-
153
- implementation.getFormattedChat(
154
- contextId: contextId,
155
- messages: messages,
156
- chatTemplate: chatTemplate,
157
- params: params
158
- ) { result in
159
- switch result {
160
- case .success(let formattedChat):
161
- call.resolve(formattedChat)
162
- case .failure(let error):
163
- call.reject(error.localizedDescription)
164
- }
165
- }
166
- }
167
-
168
- @objc func completion(_ call: CAPPluginCall) {
169
- let contextId = call.getInt("contextId") ?? 0
170
- let params = call.getObject("params") ?? [:]
171
-
172
- implementation.completion(contextId: contextId, params: params) { result in
173
- switch result {
174
- case .success(let completionResult):
175
- call.resolve(completionResult)
176
- case .failure(let error):
177
- call.reject(error.localizedDescription)
178
- }
179
- }
180
- }
181
-
182
- @objc func stopCompletion(_ call: CAPPluginCall) {
183
- let contextId = call.getInt("contextId") ?? 0
184
-
185
- implementation.stopCompletion(contextId: contextId) { result in
186
- switch result {
187
- case .success:
188
- call.resolve()
189
- case .failure(let error):
190
- call.reject(error.localizedDescription)
191
- }
192
- }
193
- }
194
-
195
- // MARK: - Session management
196
-
197
- @objc func loadSession(_ call: CAPPluginCall) {
198
- let contextId = call.getInt("contextId") ?? 0
199
- let filepath = call.getString("filepath") ?? ""
200
-
201
- implementation.loadSession(contextId: contextId, filepath: filepath) { result in
202
- switch result {
203
- case .success(let sessionResult):
204
- call.resolve(sessionResult)
205
- case .failure(let error):
206
- call.reject(error.localizedDescription)
207
- }
208
- }
209
- }
210
-
211
- @objc func saveSession(_ call: CAPPluginCall) {
212
- let contextId = call.getInt("contextId") ?? 0
213
- let filepath = call.getString("filepath") ?? ""
214
- let size = call.getInt("size") ?? -1
215
-
216
- implementation.saveSession(contextId: contextId, filepath: filepath, size: size) { result in
217
- switch result {
218
- case .success(let tokensSaved):
219
- call.resolve(["tokensSaved": tokensSaved])
220
- case .failure(let error):
221
- call.reject(error.localizedDescription)
222
- }
223
- }
224
- }
225
-
226
- // MARK: - Tokenization
227
-
228
- @objc func tokenize(_ call: CAPPluginCall) {
229
- let contextId = call.getInt("contextId") ?? 0
230
- let text = call.getString("text") ?? ""
231
- let imagePaths = call.getArray("imagePaths", String.self) ?? []
232
-
233
- implementation.tokenize(contextId: contextId, text: text, imagePaths: imagePaths) { result in
234
- switch result {
235
- case .success(let tokenizeResult):
236
- call.resolve(tokenizeResult)
237
- case .failure(let error):
238
- call.reject(error.localizedDescription)
239
- }
240
- }
241
- }
242
-
243
- @objc func detokenize(_ call: CAPPluginCall) {
244
- let contextId = call.getInt("contextId") ?? 0
245
- let tokens = call.getArray("tokens", Int.self) ?? []
246
-
247
- implementation.detokenize(contextId: contextId, tokens: tokens) { result in
248
- switch result {
249
- case .success(let text):
250
- call.resolve(["text": text])
251
- case .failure(let error):
252
- call.reject(error.localizedDescription)
253
- }
254
- }
255
- }
256
-
257
- // MARK: - Embeddings and reranking
258
-
259
- @objc func embedding(_ call: CAPPluginCall) {
260
- let contextId = call.getInt("contextId") ?? 0
261
- let text = call.getString("text") ?? ""
262
- let params = call.getObject("params") ?? [:]
263
-
264
- implementation.embedding(contextId: contextId, text: text, params: params) { result in
265
- switch result {
266
- case .success(let embeddingResult):
267
- call.resolve(embeddingResult)
268
- case .failure(let error):
269
- call.reject(error.localizedDescription)
270
- }
271
- }
272
- }
273
-
274
- @objc func rerank(_ call: CAPPluginCall) {
275
- let contextId = call.getInt("contextId") ?? 0
276
- let query = call.getString("query") ?? ""
277
- let documents = call.getArray("documents", String.self) ?? []
278
- let params = call.getObject("params")
279
-
280
- implementation.rerank(contextId: contextId, query: query, documents: documents, params: params) { result in
281
- switch result {
282
- case .success(let rerankResults):
283
- call.resolve(["results": rerankResults])
284
- case .failure(let error):
285
- call.reject(error.localizedDescription)
286
- }
287
- }
288
- }
289
-
290
- // MARK: - Benchmarking
291
-
292
- @objc func bench(_ call: CAPPluginCall) {
293
- let contextId = call.getInt("contextId") ?? 0
294
- let pp = call.getInt("pp") ?? 0
295
- let tg = call.getInt("tg") ?? 0
296
- let pl = call.getInt("pl") ?? 0
297
- let nr = call.getInt("nr") ?? 0
298
-
299
- implementation.bench(contextId: contextId, pp: pp, tg: tg, pl: pl, nr: nr) { result in
300
- switch result {
301
- case .success(let benchResult):
302
- call.resolve(["result": benchResult])
303
- case .failure(let error):
304
- call.reject(error.localizedDescription)
305
- }
306
- }
307
- }
308
-
309
- // MARK: - LoRA adapters
310
-
311
- @objc func applyLoraAdapters(_ call: CAPPluginCall) {
312
- let contextId = call.getInt("contextId") ?? 0
313
- let loraAdapters = call.getArray("loraAdapters", [String: Any].self) ?? []
314
-
315
- implementation.applyLoraAdapters(contextId: contextId, loraAdapters: loraAdapters) { result in
316
- switch result {
317
- case .success:
318
- call.resolve()
319
- case .failure(let error):
320
- call.reject(error.localizedDescription)
321
- }
322
- }
323
- }
324
-
325
- @objc func removeLoraAdapters(_ call: CAPPluginCall) {
326
- let contextId = call.getInt("contextId") ?? 0
327
-
328
- implementation.removeLoraAdapters(contextId: contextId) { result in
329
- switch result {
330
- case .success:
331
- call.resolve()
332
- case .failure(let error):
333
- call.reject(error.localizedDescription)
334
- }
335
- }
336
- }
337
-
338
- @objc func getLoadedLoraAdapters(_ call: CAPPluginCall) {
339
- let contextId = call.getInt("contextId") ?? 0
340
-
341
- implementation.getLoadedLoraAdapters(contextId: contextId) { result in
342
- switch result {
343
- case .success(let adapters):
344
- call.resolve(["adapters": adapters])
345
- case .failure(let error):
346
- call.reject(error.localizedDescription)
347
- }
348
- }
349
- }
350
-
351
- // MARK: - Multimodal methods
352
-
353
- @objc func initMultimodal(_ call: CAPPluginCall) {
354
- let contextId = call.getInt("contextId") ?? 0
355
- let params = call.getObject("params") ?? [:]
356
- let path = params["path"] as? String ?? ""
357
- let useGpu = params["use_gpu"] as? Bool ?? true
358
-
359
- implementation.initMultimodal(contextId: contextId, path: path, useGpu: useGpu) { result in
360
- switch result {
361
- case .success(let success):
362
- call.resolve(["success": success])
363
- case .failure(let error):
364
- call.reject(error.localizedDescription)
365
- }
366
- }
367
- }
368
-
369
- @objc func isMultimodalEnabled(_ call: CAPPluginCall) {
370
- let contextId = call.getInt("contextId") ?? 0
371
-
372
- implementation.isMultimodalEnabled(contextId: contextId) { result in
373
- switch result {
374
- case .success(let enabled):
375
- call.resolve(["enabled": enabled])
376
- case .failure(let error):
377
- call.reject(error.localizedDescription)
378
- }
379
- }
380
- }
381
-
382
- @objc func getMultimodalSupport(_ call: CAPPluginCall) {
383
- let contextId = call.getInt("contextId") ?? 0
384
-
385
- implementation.getMultimodalSupport(contextId: contextId) { result in
386
- switch result {
387
- case .success(let support):
388
- call.resolve(support)
389
- case .failure(let error):
390
- call.reject(error.localizedDescription)
391
- }
392
- }
393
- }
394
-
395
- @objc func releaseMultimodal(_ call: CAPPluginCall) {
396
- let contextId = call.getInt("contextId") ?? 0
397
-
398
- implementation.releaseMultimodal(contextId: contextId) { result in
399
- switch result {
400
- case .success:
401
- call.resolve()
402
- case .failure(let error):
403
- call.reject(error.localizedDescription)
404
- }
405
- }
406
- }
407
-
408
- // MARK: - TTS methods
409
-
410
- @objc func initVocoder(_ call: CAPPluginCall) {
411
- let contextId = call.getInt("contextId") ?? 0
412
- let params = call.getObject("params") ?? [:]
413
- let path = params["path"] as? String ?? ""
414
- let nBatch = params["n_batch"] as? Int
415
-
416
- implementation.initVocoder(contextId: contextId, path: path, nBatch: nBatch) { result in
417
- switch result {
418
- case .success(let success):
419
- call.resolve(["success": success])
420
- case .failure(let error):
421
- call.reject(error.localizedDescription)
422
- }
423
- }
424
- }
425
-
426
- @objc func isVocoderEnabled(_ call: CAPPluginCall) {
427
- let contextId = call.getInt("contextId") ?? 0
428
-
429
- implementation.isVocoderEnabled(contextId: contextId) { result in
430
- switch result {
431
- case .success(let enabled):
432
- call.resolve(["enabled": enabled])
433
- case .failure(let error):
434
- call.reject(error.localizedDescription)
435
- }
436
- }
437
- }
438
-
439
- @objc func getFormattedAudioCompletion(_ call: CAPPluginCall) {
440
- let contextId = call.getInt("contextId") ?? 0
441
- let speakerJsonStr = call.getString("speakerJsonStr") ?? ""
442
- let textToSpeak = call.getString("textToSpeak") ?? ""
443
-
444
- implementation.getFormattedAudioCompletion(
445
- contextId: contextId,
446
- speakerJsonStr: speakerJsonStr,
447
- textToSpeak: textToSpeak
448
- ) { result in
449
- switch result {
450
- case .success(let audioCompletion):
451
- call.resolve(audioCompletion)
452
- case .failure(let error):
453
- call.reject(error.localizedDescription)
454
- }
455
- }
456
- }
457
-
458
- @objc func getAudioCompletionGuideTokens(_ call: CAPPluginCall) {
459
- let contextId = call.getInt("contextId") ?? 0
460
- let textToSpeak = call.getString("textToSpeak") ?? ""
461
-
462
- implementation.getAudioCompletionGuideTokens(contextId: contextId, textToSpeak: textToSpeak) { result in
463
- switch result {
464
- case .success(let tokens):
465
- call.resolve(["tokens": tokens])
466
- case .failure(let error):
467
- call.reject(error.localizedDescription)
468
- }
469
- }
470
- }
471
-
472
- @objc func decodeAudioTokens(_ call: CAPPluginCall) {
473
- let contextId = call.getInt("contextId") ?? 0
474
- let tokens = call.getArray("tokens", Int.self) ?? []
475
-
476
- implementation.decodeAudioTokens(contextId: contextId, tokens: tokens) { result in
477
- switch result {
478
- case .success(let decodedTokens):
479
- call.resolve(["decodedTokens": decodedTokens])
480
- case .failure(let error):
481
- call.reject(error.localizedDescription)
482
- }
483
- }
484
- }
485
-
486
- @objc func releaseVocoder(_ call: CAPPluginCall) {
487
- let contextId = call.getInt("contextId") ?? 0
488
-
489
- implementation.releaseVocoder(contextId: contextId) { result in
490
- switch result {
491
- case .success:
492
- call.resolve()
493
- case .failure(let error):
494
- call.reject(error.localizedDescription)
495
- }
496
- }
497
- }
498
-
499
- // MARK: - Events
500
-
501
- @objc func addListener(_ call: CAPPluginCall) {
502
- let eventName = call.getString("eventName") ?? ""
503
- // Note: In Capacitor, event listeners are typically handled differently
504
- // This is a placeholder for the event system
505
- call.resolve()
506
- }
507
-
508
- @objc func removeAllListeners(_ call: CAPPluginCall) {
509
- let eventName = call.getString("eventName") ?? ""
510
- // Note: In Capacitor, event listeners are typically handled differently
511
- // This is a placeholder for the event system
512
- call.resolve()
513
- }
514
- }
1
+ import Foundation
2
+ import Capacitor
3
+
4
+ /**
5
+ * Please read the Capacitor iOS Plugin Development Guide
6
+ * here: https://capacitorjs.com/docs/plugins/ios
7
+ */
8
+ @objc(LlamaCppPlugin)
9
+ public class LlamaCppPlugin: CAPPlugin, CAPBridgedPlugin {
10
+ public let identifier = "LlamaCppPlugin"
11
+ public let jsName = "LlamaCpp"
12
+ public let pluginMethods: [CAPPluginMethod] = [
13
+ // Core initialization and management
14
+ CAPPluginMethod(name: "toggleNativeLog", returnType: CAPPluginReturnPromise),
15
+ CAPPluginMethod(name: "setContextLimit", returnType: CAPPluginReturnPromise),
16
+ CAPPluginMethod(name: "modelInfo", returnType: CAPPluginReturnPromise),
17
+ CAPPluginMethod(name: "initContext", returnType: CAPPluginReturnPromise),
18
+ CAPPluginMethod(name: "releaseContext", returnType: CAPPluginReturnPromise),
19
+ CAPPluginMethod(name: "releaseAllContexts", returnType: CAPPluginReturnPromise),
20
+
21
+ // Chat and completion
22
+ CAPPluginMethod(name: "getFormattedChat", returnType: CAPPluginReturnPromise),
23
+ CAPPluginMethod(name: "completion", returnType: CAPPluginReturnPromise),
24
+ CAPPluginMethod(name: "stopCompletion", returnType: CAPPluginReturnPromise),
25
+
26
+ // Session management
27
+ CAPPluginMethod(name: "loadSession", returnType: CAPPluginReturnPromise),
28
+ CAPPluginMethod(name: "saveSession", returnType: CAPPluginReturnPromise),
29
+
30
+ // Tokenization
31
+ CAPPluginMethod(name: "tokenize", returnType: CAPPluginReturnPromise),
32
+ CAPPluginMethod(name: "detokenize", returnType: CAPPluginReturnPromise),
33
+
34
+ // Embeddings and reranking
35
+ CAPPluginMethod(name: "embedding", returnType: CAPPluginReturnPromise),
36
+ CAPPluginMethod(name: "rerank", returnType: CAPPluginReturnPromise),
37
+
38
+ // Benchmarking
39
+ CAPPluginMethod(name: "bench", returnType: CAPPluginReturnPromise),
40
+
41
+ // LoRA adapters
42
+ CAPPluginMethod(name: "applyLoraAdapters", returnType: CAPPluginReturnPromise),
43
+ CAPPluginMethod(name: "removeLoraAdapters", returnType: CAPPluginReturnPromise),
44
+ CAPPluginMethod(name: "getLoadedLoraAdapters", returnType: CAPPluginReturnPromise),
45
+
46
+ // Multimodal methods
47
+ CAPPluginMethod(name: "initMultimodal", returnType: CAPPluginReturnPromise),
48
+ CAPPluginMethod(name: "isMultimodalEnabled", returnType: CAPPluginReturnPromise),
49
+ CAPPluginMethod(name: "getMultimodalSupport", returnType: CAPPluginReturnPromise),
50
+ CAPPluginMethod(name: "releaseMultimodal", returnType: CAPPluginReturnPromise),
51
+
52
+ // TTS methods
53
+ CAPPluginMethod(name: "initVocoder", returnType: CAPPluginReturnPromise),
54
+ CAPPluginMethod(name: "isVocoderEnabled", returnType: CAPPluginReturnPromise),
55
+ CAPPluginMethod(name: "getFormattedAudioCompletion", returnType: CAPPluginReturnPromise),
56
+ CAPPluginMethod(name: "getAudioCompletionGuideTokens", returnType: CAPPluginReturnPromise),
57
+ CAPPluginMethod(name: "decodeAudioTokens", returnType: CAPPluginReturnPromise),
58
+ CAPPluginMethod(name: "releaseVocoder", returnType: CAPPluginReturnPromise),
59
+
60
+ // Model download and management
61
+ CAPPluginMethod(name: "downloadModel", returnType: CAPPluginReturnPromise),
62
+ CAPPluginMethod(name: "getDownloadProgress", returnType: CAPPluginReturnPromise),
63
+ CAPPluginMethod(name: "cancelDownload", returnType: CAPPluginReturnPromise),
64
+ CAPPluginMethod(name: "getAvailableModels", returnType: CAPPluginReturnPromise),
65
+
66
+ // Grammar utilities
67
+ CAPPluginMethod(name: "convertJsonSchemaToGrammar", returnType: CAPPluginReturnPromise),
68
+
69
+ // Events
70
+ CAPPluginMethod(name: "addListener", returnType: CAPPluginReturnPromise),
71
+ CAPPluginMethod(name: "removeAllListeners", returnType: CAPPluginReturnPromise)
72
+ ]
73
+
74
+ private let implementation = LlamaCpp()
75
+
76
+ // MARK: - Core initialization and management
77
+
78
+ @objc func toggleNativeLog(_ call: CAPPluginCall) {
79
+ let enabled = call.getBool("enabled") ?? false
80
+ implementation.toggleNativeLog(enabled: enabled) { result in
81
+ switch result {
82
+ case .success:
83
+ call.resolve()
84
+ case .failure(let error):
85
+ call.reject(error.localizedDescription)
86
+ }
87
+ }
88
+ }
89
+
90
+ @objc func setContextLimit(_ call: CAPPluginCall) {
91
+ let limit = call.getInt("limit") ?? 0
92
+ implementation.setContextLimit(limit: limit) { result in
93
+ switch result {
94
+ case .success:
95
+ call.resolve()
96
+ case .failure(let error):
97
+ call.reject(error.localizedDescription)
98
+ }
99
+ }
100
+ }
101
+
102
+ @objc func modelInfo(_ call: CAPPluginCall) {
103
+ let path = call.getString("path") ?? ""
104
+ let skip = call.getArray("skip", String.self) ?? []
105
+
106
+ implementation.modelInfo(path: path, skip: skip) { result in
107
+ switch result {
108
+ case .success(let modelInfo):
109
+ call.resolve(modelInfo)
110
+ case .failure(let error):
111
+ call.reject(error.localizedDescription)
112
+ }
113
+ }
114
+ }
115
+
116
+ @objc func initContext(_ call: CAPPluginCall) {
117
+ let contextId = call.getInt("contextId") ?? 0
118
+ let params = call.getObject("params") ?? [:]
119
+
120
+ implementation.initContext(contextId: contextId, params: params) { result in
121
+ switch result {
122
+ case .success(let context):
123
+ call.resolve(context)
124
+ case .failure(let error):
125
+ call.reject(error.localizedDescription)
126
+ }
127
+ }
128
+ }
129
+
130
+ @objc func releaseContext(_ call: CAPPluginCall) {
131
+ let contextId = call.getInt("contextId") ?? 0
132
+
133
+ implementation.releaseContext(contextId: contextId) { result in
134
+ switch result {
135
+ case .success:
136
+ call.resolve()
137
+ case .failure(let error):
138
+ call.reject(error.localizedDescription)
139
+ }
140
+ }
141
+ }
142
+
143
+ @objc func releaseAllContexts(_ call: CAPPluginCall) {
144
+ implementation.releaseAllContexts { result in
145
+ switch result {
146
+ case .success:
147
+ call.resolve()
148
+ case .failure(let error):
149
+ call.reject(error.localizedDescription)
150
+ }
151
+ }
152
+ }
153
+
154
+ // MARK: - Chat and completion
155
+
156
+ @objc func getFormattedChat(_ call: CAPPluginCall) {
157
+ let contextId = call.getInt("contextId") ?? 0
158
+ let messages = call.getString("messages") ?? ""
159
+ let chatTemplate = call.getString("chatTemplate")
160
+ let params = call.getObject("params")
161
+
162
+ implementation.getFormattedChat(
163
+ contextId: contextId,
164
+ messages: messages,
165
+ chatTemplate: chatTemplate,
166
+ params: params
167
+ ) { result in
168
+ switch result {
169
+ case .success(let formattedChat):
170
+ call.resolve(formattedChat)
171
+ case .failure(let error):
172
+ call.reject(error.localizedDescription)
173
+ }
174
+ }
175
+ }
176
+
177
+ @objc func completion(_ call: CAPPluginCall) {
178
+ let contextId = call.getInt("contextId") ?? 0
179
+ let params = call.getObject("params") ?? [:]
180
+
181
+ implementation.completion(contextId: contextId, params: params) { result in
182
+ switch result {
183
+ case .success(let completionResult):
184
+ call.resolve(completionResult)
185
+ case .failure(let error):
186
+ call.reject(error.localizedDescription)
187
+ }
188
+ }
189
+ }
190
+
191
+ @objc func stopCompletion(_ call: CAPPluginCall) {
192
+ let contextId = call.getInt("contextId") ?? 0
193
+
194
+ implementation.stopCompletion(contextId: contextId) { result in
195
+ switch result {
196
+ case .success:
197
+ call.resolve()
198
+ case .failure(let error):
199
+ call.reject(error.localizedDescription)
200
+ }
201
+ }
202
+ }
203
+
204
+ // MARK: - Session management
205
+
206
+ @objc func loadSession(_ call: CAPPluginCall) {
207
+ let contextId = call.getInt("contextId") ?? 0
208
+ let filepath = call.getString("filepath") ?? ""
209
+
210
+ implementation.loadSession(contextId: contextId, filepath: filepath) { result in
211
+ switch result {
212
+ case .success(let sessionResult):
213
+ call.resolve(sessionResult)
214
+ case .failure(let error):
215
+ call.reject(error.localizedDescription)
216
+ }
217
+ }
218
+ }
219
+
220
+ @objc func saveSession(_ call: CAPPluginCall) {
221
+ let contextId = call.getInt("contextId") ?? 0
222
+ let filepath = call.getString("filepath") ?? ""
223
+ let size = call.getInt("size") ?? -1
224
+
225
+ implementation.saveSession(contextId: contextId, filepath: filepath, size: size) { result in
226
+ switch result {
227
+ case .success(let tokensSaved):
228
+ call.resolve(["tokensSaved": tokensSaved])
229
+ case .failure(let error):
230
+ call.reject(error.localizedDescription)
231
+ }
232
+ }
233
+ }
234
+
235
+ // MARK: - Tokenization
236
+
237
+ @objc func tokenize(_ call: CAPPluginCall) {
238
+ let contextId = call.getInt("contextId") ?? 0
239
+ let text = call.getString("text") ?? ""
240
+ let imagePaths = call.getArray("imagePaths", String.self) ?? []
241
+
242
+ implementation.tokenize(contextId: contextId, text: text, imagePaths: imagePaths) { result in
243
+ switch result {
244
+ case .success(let tokenizeResult):
245
+ call.resolve(tokenizeResult)
246
+ case .failure(let error):
247
+ call.reject(error.localizedDescription)
248
+ }
249
+ }
250
+ }
251
+
252
+ @objc func detokenize(_ call: CAPPluginCall) {
253
+ let contextId = call.getInt("contextId") ?? 0
254
+ let tokens = call.getArray("tokens", Int.self) ?? []
255
+
256
+ implementation.detokenize(contextId: contextId, tokens: tokens) { result in
257
+ switch result {
258
+ case .success(let text):
259
+ call.resolve(["text": text])
260
+ case .failure(let error):
261
+ call.reject(error.localizedDescription)
262
+ }
263
+ }
264
+ }
265
+
266
+ // MARK: - Embeddings and reranking
267
+
268
+ @objc func embedding(_ call: CAPPluginCall) {
269
+ let contextId = call.getInt("contextId") ?? 0
270
+ let text = call.getString("text") ?? ""
271
+ let params = call.getObject("params") ?? [:]
272
+
273
+ implementation.embedding(contextId: contextId, text: text, params: params) { result in
274
+ switch result {
275
+ case .success(let embeddingResult):
276
+ call.resolve(embeddingResult)
277
+ case .failure(let error):
278
+ call.reject(error.localizedDescription)
279
+ }
280
+ }
281
+ }
282
+
283
+ @objc func rerank(_ call: CAPPluginCall) {
284
+ let contextId = call.getInt("contextId") ?? 0
285
+ let query = call.getString("query") ?? ""
286
+ let documents = call.getArray("documents", String.self) ?? []
287
+ let params = call.getObject("params")
288
+
289
+ implementation.rerank(contextId: contextId, query: query, documents: documents, params: params) { result in
290
+ switch result {
291
+ case .success(let rerankResults):
292
+ call.resolve(["results": rerankResults])
293
+ case .failure(let error):
294
+ call.reject(error.localizedDescription)
295
+ }
296
+ }
297
+ }
298
+
299
+ // MARK: - Benchmarking
300
+
301
+ @objc func bench(_ call: CAPPluginCall) {
302
+ let contextId = call.getInt("contextId") ?? 0
303
+ let pp = call.getInt("pp") ?? 0
304
+ let tg = call.getInt("tg") ?? 0
305
+ let pl = call.getInt("pl") ?? 0
306
+ let nr = call.getInt("nr") ?? 0
307
+
308
+ implementation.bench(contextId: contextId, pp: pp, tg: tg, pl: pl, nr: nr) { result in
309
+ switch result {
310
+ case .success(let benchResult):
311
+ call.resolve(["result": benchResult])
312
+ case .failure(let error):
313
+ call.reject(error.localizedDescription)
314
+ }
315
+ }
316
+ }
317
+
318
+ // MARK: - LoRA adapters
319
+
320
+ @objc func applyLoraAdapters(_ call: CAPPluginCall) {
321
+ let contextId = call.getInt("contextId") ?? 0
322
+ let loraAdapters = call.getArray("loraAdapters", [String: Any].self) ?? []
323
+
324
+ implementation.applyLoraAdapters(contextId: contextId, loraAdapters: loraAdapters) { result in
325
+ switch result {
326
+ case .success:
327
+ call.resolve()
328
+ case .failure(let error):
329
+ call.reject(error.localizedDescription)
330
+ }
331
+ }
332
+ }
333
+
334
+ @objc func removeLoraAdapters(_ call: CAPPluginCall) {
335
+ let contextId = call.getInt("contextId") ?? 0
336
+
337
+ implementation.removeLoraAdapters(contextId: contextId) { result in
338
+ switch result {
339
+ case .success:
340
+ call.resolve()
341
+ case .failure(let error):
342
+ call.reject(error.localizedDescription)
343
+ }
344
+ }
345
+ }
346
+
347
+ @objc func getLoadedLoraAdapters(_ call: CAPPluginCall) {
348
+ let contextId = call.getInt("contextId") ?? 0
349
+
350
+ implementation.getLoadedLoraAdapters(contextId: contextId) { result in
351
+ switch result {
352
+ case .success(let adapters):
353
+ call.resolve(["adapters": adapters])
354
+ case .failure(let error):
355
+ call.reject(error.localizedDescription)
356
+ }
357
+ }
358
+ }
359
+
360
+ // MARK: - Multimodal methods
361
+
362
+ @objc func initMultimodal(_ call: CAPPluginCall) {
363
+ let contextId = call.getInt("contextId") ?? 0
364
+ let params = call.getObject("params") ?? [:]
365
+ let path = params["path"] as? String ?? ""
366
+ let useGpu = params["use_gpu"] as? Bool ?? true
367
+
368
+ implementation.initMultimodal(contextId: contextId, path: path, useGpu: useGpu) { result in
369
+ switch result {
370
+ case .success(let success):
371
+ call.resolve(["success": success])
372
+ case .failure(let error):
373
+ call.reject(error.localizedDescription)
374
+ }
375
+ }
376
+ }
377
+
378
+ @objc func isMultimodalEnabled(_ call: CAPPluginCall) {
379
+ let contextId = call.getInt("contextId") ?? 0
380
+
381
+ implementation.isMultimodalEnabled(contextId: contextId) { result in
382
+ switch result {
383
+ case .success(let enabled):
384
+ call.resolve(["enabled": enabled])
385
+ case .failure(let error):
386
+ call.reject(error.localizedDescription)
387
+ }
388
+ }
389
+ }
390
+
391
+ @objc func getMultimodalSupport(_ call: CAPPluginCall) {
392
+ let contextId = call.getInt("contextId") ?? 0
393
+
394
+ implementation.getMultimodalSupport(contextId: contextId) { result in
395
+ switch result {
396
+ case .success(let support):
397
+ call.resolve(support)
398
+ case .failure(let error):
399
+ call.reject(error.localizedDescription)
400
+ }
401
+ }
402
+ }
403
+
404
+ @objc func releaseMultimodal(_ call: CAPPluginCall) {
405
+ let contextId = call.getInt("contextId") ?? 0
406
+
407
+ implementation.releaseMultimodal(contextId: contextId) { result in
408
+ switch result {
409
+ case .success:
410
+ call.resolve()
411
+ case .failure(let error):
412
+ call.reject(error.localizedDescription)
413
+ }
414
+ }
415
+ }
416
+
417
+ // MARK: - TTS methods
418
+
419
+ @objc func initVocoder(_ call: CAPPluginCall) {
420
+ let contextId = call.getInt("contextId") ?? 0
421
+ let params = call.getObject("params") ?? [:]
422
+ let path = params["path"] as? String ?? ""
423
+ let nBatch = params["n_batch"] as? Int
424
+
425
+ implementation.initVocoder(contextId: contextId, path: path, nBatch: nBatch) { result in
426
+ switch result {
427
+ case .success(let success):
428
+ call.resolve(["success": success])
429
+ case .failure(let error):
430
+ call.reject(error.localizedDescription)
431
+ }
432
+ }
433
+ }
434
+
435
+ @objc func isVocoderEnabled(_ call: CAPPluginCall) {
436
+ let contextId = call.getInt("contextId") ?? 0
437
+
438
+ implementation.isVocoderEnabled(contextId: contextId) { result in
439
+ switch result {
440
+ case .success(let enabled):
441
+ call.resolve(["enabled": enabled])
442
+ case .failure(let error):
443
+ call.reject(error.localizedDescription)
444
+ }
445
+ }
446
+ }
447
+
448
+ @objc func getFormattedAudioCompletion(_ call: CAPPluginCall) {
449
+ let contextId = call.getInt("contextId") ?? 0
450
+ let speakerJsonStr = call.getString("speakerJsonStr") ?? ""
451
+ let textToSpeak = call.getString("textToSpeak") ?? ""
452
+
453
+ implementation.getFormattedAudioCompletion(
454
+ contextId: contextId,
455
+ speakerJsonStr: speakerJsonStr,
456
+ textToSpeak: textToSpeak
457
+ ) { result in
458
+ switch result {
459
+ case .success(let audioCompletion):
460
+ call.resolve(audioCompletion)
461
+ case .failure(let error):
462
+ call.reject(error.localizedDescription)
463
+ }
464
+ }
465
+ }
466
+
467
+ @objc func getAudioCompletionGuideTokens(_ call: CAPPluginCall) {
468
+ let contextId = call.getInt("contextId") ?? 0
469
+ let textToSpeak = call.getString("textToSpeak") ?? ""
470
+
471
+ implementation.getAudioCompletionGuideTokens(contextId: contextId, textToSpeak: textToSpeak) { result in
472
+ switch result {
473
+ case .success(let tokens):
474
+ call.resolve(["tokens": tokens])
475
+ case .failure(let error):
476
+ call.reject(error.localizedDescription)
477
+ }
478
+ }
479
+ }
480
+
481
+ @objc func decodeAudioTokens(_ call: CAPPluginCall) {
482
+ let contextId = call.getInt("contextId") ?? 0
483
+ let tokens = call.getArray("tokens", Int.self) ?? []
484
+
485
+ implementation.decodeAudioTokens(contextId: contextId, tokens: tokens) { result in
486
+ switch result {
487
+ case .success(let decodedTokens):
488
+ call.resolve(["decodedTokens": decodedTokens])
489
+ case .failure(let error):
490
+ call.reject(error.localizedDescription)
491
+ }
492
+ }
493
+ }
494
+
495
+ @objc func releaseVocoder(_ call: CAPPluginCall) {
496
+ let contextId = call.getInt("contextId") ?? 0
497
+
498
+ implementation.releaseVocoder(contextId: contextId) { result in
499
+ switch result {
500
+ case .success:
501
+ call.resolve()
502
+ case .failure(let error):
503
+ call.reject(error.localizedDescription)
504
+ }
505
+ }
506
+ }
507
+
508
+ // MARK: - Events
509
+
510
+ @objc func addListener(_ call: CAPPluginCall) {
511
+ let eventName = call.getString("eventName") ?? ""
512
+ // Note: In Capacitor, event listeners are typically handled differently
513
+ // This is a placeholder for the event system
514
+ call.resolve()
515
+ }
516
+
517
+ @objc func removeAllListeners(_ call: CAPPluginCall) {
518
+ let eventName = call.getString("eventName") ?? ""
519
+ // Note: In Capacitor, event listeners are typically handled differently
520
+ // This is a placeholder for the event system
521
+ call.resolve()
522
+ }
523
+
524
+ // MARK: - Model download and management
525
+
526
+ @objc func downloadModel(_ call: CAPPluginCall) {
527
+ let url = call.getString("url") ?? ""
528
+ let filename = call.getString("filename") ?? ""
529
+
530
+ implementation.downloadModel(url: url, filename: filename) { result in
531
+ switch result {
532
+ case .success(let path):
533
+ call.resolve(["path": path])
534
+ case .failure(let error):
535
+ call.reject(error.localizedDescription)
536
+ }
537
+ }
538
+ }
539
+
540
+ @objc func getDownloadProgress(_ call: CAPPluginCall) {
541
+ let url = call.getString("url") ?? ""
542
+
543
+ implementation.getDownloadProgress(url: url) { result in
544
+ switch result {
545
+ case .success(let progress):
546
+ call.resolve(progress)
547
+ case .failure(let error):
548
+ call.reject(error.localizedDescription)
549
+ }
550
+ }
551
+ }
552
+
553
+ @objc func cancelDownload(_ call: CAPPluginCall) {
554
+ let url = call.getString("url") ?? ""
555
+
556
+ implementation.cancelDownload(url: url) { result in
557
+ switch result {
558
+ case .success(let cancelled):
559
+ call.resolve(["cancelled": cancelled])
560
+ case .failure(let error):
561
+ call.reject(error.localizedDescription)
562
+ }
563
+ }
564
+ }
565
+
566
+ @objc func getAvailableModels(_ call: CAPPluginCall) {
567
+ implementation.getAvailableModels { result in
568
+ switch result {
569
+ case .success(let models):
570
+ call.resolve(["models": models])
571
+ case .failure(let error):
572
+ call.reject(error.localizedDescription)
573
+ }
574
+ }
575
+ }
576
+
577
+ // MARK: - Grammar utilities
578
+
579
+ @objc func convertJsonSchemaToGrammar(_ call: CAPPluginCall) {
580
+ let schema = call.getString("schema") ?? ""
581
+
582
+ implementation.convertJsonSchemaToGrammar(schema: schema) { result in
583
+ switch result {
584
+ case .success(let grammar):
585
+ call.resolve(["grammar": grammar])
586
+ case .failure(let error):
587
+ call.reject(error.localizedDescription)
588
+ }
589
+ }
590
+ }
591
+ }