|
14 | 14 | package com.runanywhere.sdk.foundation.bridge.extensions |
15 | 15 |
|
16 | 16 | import com.runanywhere.sdk.native.bridge.RunAnywhereBridge |
| 17 | +import java.io.File |
17 | 18 |
|
18 | 19 | /** |
19 | 20 | * Model registry bridge that provides direct access to the C++ model registry. |
@@ -215,7 +216,7 @@ object CppBridgeModelRegistry { |
215 | 216 | } |
216 | 217 |
|
217 | 218 | /** |
218 | | - * Update download status in C++ registry. |
| 219 | + * Update download status in C++ registry (in-memory only). |
219 | 220 | * |
220 | 221 | * @param modelId The model ID |
221 | 222 | * @param localPath The local path (or null to clear download) |
@@ -259,71 +260,43 @@ object CppBridgeModelRegistry { |
259 | 260 | } |
260 | 261 |
|
261 | 262 | /** |
262 | | - * Scan filesystem and restore downloaded models. |
| 263 | + * Scan filesystem and restore downloaded models whose filename matches their model ID. |
| 264 | + * This handles single-file models (GGUF, ONNX) and archive models that extracted into |
| 265 | + * a named directory matching the model ID. |
263 | 266 | * |
264 | | - * This is called during SDK initialization to detect previously |
265 | | - * downloaded models and update their status in the C++ registry. |
| 267 | + * For archive models with flat extraction (e.g. Genie), see |
| 268 | + * [RunAnywhere.restorePersistedDownloadPaths] in RunAnywhere+ModelManagement.jvmAndroid.kt. |
266 | 269 | */ |
267 | 270 | fun scanAndRestoreDownloadedModels() { |
268 | | - log(LogLevel.DEBUG, "Scanning for previously downloaded models...") |
269 | | - |
270 | 271 | val baseDir = CppBridgeModelPaths.getBaseDirectory() |
271 | | - val modelsDir = java.io.File(baseDir, "models") |
| 272 | + val modelsDir = File(baseDir, "models") |
272 | 273 |
|
273 | 274 | if (!modelsDir.exists()) { |
274 | 275 | log(LogLevel.DEBUG, "Models directory does not exist: ${modelsDir.absolutePath}") |
275 | 276 | return |
276 | 277 | } |
277 | 278 |
|
278 | | - val typeDirectories = |
279 | | - mapOf( |
280 | | - "llm" to ModelCategory.LANGUAGE, |
281 | | - "stt" to ModelCategory.SPEECH_RECOGNITION, |
282 | | - "tts" to ModelCategory.SPEECH_SYNTHESIS, |
283 | | - "vad" to ModelCategory.AUDIO, |
284 | | - |
285 | | - // RAG |
286 | | - "embedding" to ModelType.EMBEDDING, |
287 | | - |
288 | | - // Vision / VLM |
289 | | - "vision" to ModelCategory.VISION, |
290 | | - "multimodal" to ModelCategory.MULTIMODAL, |
291 | | - |
292 | | - // Backward compatibility |
293 | | - "other" to -1, |
294 | | - ) |
295 | | - |
| 279 | + log(LogLevel.DEBUG, "Scanning for previously downloaded models...") |
296 | 280 | var restoredCount = 0 |
297 | 281 |
|
298 | | - for ((dirName, _) in typeDirectories) { |
299 | | - val typeDir = java.io.File(modelsDir, dirName) |
| 282 | + val typeDirectories = listOf("llm", "stt", "tts", "vad", "embedding", "vision", "multimodal", "other") |
| 283 | + for (dirName in typeDirectories) { |
| 284 | + val typeDir = File(modelsDir, dirName) |
300 | 285 | if (!typeDir.exists() || !typeDir.isDirectory) continue |
301 | 286 |
|
302 | | - log(LogLevel.DEBUG, "Scanning type directory: ${typeDir.absolutePath}") |
303 | | - |
304 | | - // Scan each model file or folder in this type directory |
305 | 287 | typeDir.listFiles()?.forEach { modelPath -> |
306 | | - // Model can be stored as: |
307 | | - // 1. A directory containing the model (e.g., models/llm/model-name/) |
308 | | - // 2. A file directly (e.g., models/llm/model-name) |
309 | 288 | val modelId = modelPath.name |
310 | | - log(LogLevel.DEBUG, "Found: $modelId (isDir=${modelPath.isDirectory}, isFile=${modelPath.isFile})") |
311 | | - |
312 | | - // Check if this model exists in registry |
313 | 289 | val existingModel = get(modelId) |
314 | | - if (existingModel != null) { |
315 | | - // Update with local path |
| 290 | + if (existingModel != null && existingModel.localPath == null) { |
316 | 291 | if (updateDownloadStatus(modelId, modelPath.absolutePath)) { |
317 | 292 | restoredCount++ |
318 | | - log(LogLevel.DEBUG, "Restored downloaded model: $modelId at ${modelPath.absolutePath}") |
| 293 | + log(LogLevel.DEBUG, "Restored $modelId at ${modelPath.absolutePath}") |
319 | 294 | } |
320 | | - } else { |
321 | | - log(LogLevel.DEBUG, "Model $modelId not found in registry, skipping") |
322 | 295 | } |
323 | 296 | } |
324 | 297 | } |
325 | 298 |
|
326 | | - log(LogLevel.INFO, "Scan complete: Restored $restoredCount previously downloaded models") |
| 299 | + log(LogLevel.INFO, "Filesystem scan complete: restored $restoredCount models") |
327 | 300 | } |
328 | 301 |
|
329 | 302 | // ======================================================================== |
|
0 commit comments