llama-cpp-capacitor 0.1.2 → 0.1.4

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.
@@ -150,6 +150,8 @@ struct MinjaCaps {
150
150
  // MARK: - Main Implementation
151
151
  @objc public class LlamaCpp: NSObject {
152
152
  private var contexts: [Int: LlamaContext] = [:]
153
+ private var nativeContexts: [Int64: UnsafeMutableRawPointer] = [:]
154
+ private var contextIdToNative: [Int: Int64] = [:]
153
155
  private var contextCounter: Int = 0
154
156
  private var contextLimit: Int = 10
155
157
  private var nativeLogEnabled: Bool = false
@@ -262,18 +264,16 @@ struct MinjaCaps {
262
264
 
263
265
  let nativeContextId = initFunc(modelPath, paramsJson.cString(using: .utf8)!)
264
266
  if nativeContextId > 0 {
265
- // Store the native context pointer (the ID is used as the pointer value)
266
- // Note: In a real implementation, the native function would return the actual pointer
267
- // For now, we use the contextId as the pointer identifier
268
- contexts[Int64(contextId)] = UnsafeMutableRawPointer(bitPattern: Int(nativeContextId))
267
+ // Store the LlamaContext for Swift bookkeeping
268
+ contexts[contextId] = context
269
+ // Store the native context pointer and mapping for C layer
270
+ let nativePtr = UnsafeMutableRawPointer(bitPattern: Int(nativeContextId))
271
+ nativeContexts[nativeContextId] = nativePtr
272
+ contextIdToNative[contextId] = nativeContextId
269
273
 
270
274
  // Register with embedding system if available
271
- // The C layer needs the actual llama_cap_context pointer, which should come from the native init
272
- // For now, we'll register with the contextId - the C layer will need to look it up
273
- if let registerFunc = registerEmbeddingContextFunc {
274
- // Note: The actual context pointer should come from the native initContext function
275
- // This is a placeholder - the real implementation needs to get the actual pointer
276
- registerFunc(Int64(contextId), contexts[Int64(contextId)]!)
275
+ if let registerFunc = registerEmbeddingContextFunc, let ptr = nativePtr {
276
+ registerFunc(nativeContextId, ptr)
277
277
  }
278
278
  } else {
279
279
  completion(.failure(.operationFailed("Failed to initialize native context")))
@@ -322,34 +322,40 @@ struct MinjaCaps {
322
322
  }
323
323
 
324
324
  func releaseContext(contextId: Int, completion: @escaping (LlamaResult<Void>) -> Void) {
325
- guard let contextPtr = contexts[contextId] else {
325
+ guard contexts[contextId] != nil else {
326
326
  completion(.failure(.contextNotFound))
327
327
  return
328
328
  }
329
329
 
330
+ let nativeId = contextIdToNative[contextId] ?? Int64(contextId)
331
+
330
332
  // Unregister from embedding system if available
331
333
  if let unregisterFunc = unregisterEmbeddingContextFunc {
332
- unregisterFunc(Int64(contextId))
334
+ unregisterFunc(nativeId)
333
335
  }
334
336
 
335
337
  // Call native release function
336
338
  if let releaseFunc = releaseContextFunc {
337
- releaseFunc(Int64(contextId))
339
+ releaseFunc(nativeId)
338
340
  }
339
341
 
340
342
  contexts.removeValue(forKey: contextId)
343
+ nativeContexts.removeValue(forKey: nativeId)
344
+ contextIdToNative.removeValue(forKey: contextId)
341
345
  completion(.success(()))
342
346
  }
343
347
 
344
348
  func releaseAllContexts(completion: @escaping (LlamaResult<Void>) -> Void) {
345
349
  // Unregister all contexts from embedding system
346
350
  if let unregisterFunc = unregisterEmbeddingContextFunc {
347
- for contextId in contexts.keys {
348
- unregisterFunc(Int64(contextId))
351
+ for (_, nativeId) in contextIdToNative {
352
+ unregisterFunc(nativeId)
349
353
  }
350
354
  }
351
355
 
352
356
  contexts.removeAll()
357
+ nativeContexts.removeAll()
358
+ contextIdToNative.removeAll()
353
359
  completion(.success(()))
354
360
  }
355
361
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llama-cpp-capacitor",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "A native Capacitor plugin that embeds llama.cpp directly into mobile apps, enabling offline AI inference with chat-first API design. Complete iOS and Android support: text generation, chat, multimodal, TTS, LoRA, embeddings, and more.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "type": "module",
@@ -20,6 +20,7 @@
20
20
  "ios/Frameworks",
21
21
  "Package.swift",
22
22
  "LlamaCpp.podspec",
23
+ "LlamaCppCapacitor.podspec",
23
24
  "types/"
24
25
  ],
25
26
  "author": "Yakub Mohammad",
@@ -71,6 +72,7 @@
71
72
  "build:all": "npm run build && npm run build:native",
72
73
  "build:ios": "cd ios && cmake -B build -S . && cmake --build build --config Release",
73
74
  "build:android": "cd android && gradlew.bat assembleRelease",
75
+ "prepack": "npm run build:native",
74
76
  "pack": "npm run build && npm pack --dry-run",
75
77
  "pack:full": "npm run build:all && npm pack --dry-run",
76
78
  "test": "jest",
@@ -83,7 +85,7 @@
83
85
  "clean:native": "rimraf ios/build ios/Frameworks android/build android/src/main/jniLibs",
84
86
  "clean:test": "rimraf test/output test/coverage",
85
87
  "watch": "tsc --watch",
86
- "prepublishOnly": "npm run build"
88
+ "prepublishOnly": "npm run build && npm run build:native"
87
89
  },
88
90
  "devDependencies": {
89
91
  "@capacitor/android": "^7.0.0",