| using System; |
| using System.IO; |
| using UnityEngine; |
| using OnDeviceAgent.Inference; |
| namespace OnDeviceAgent.AgentCore |
| { |
|
|
| public static class StreamingDbInstaller |
| { |
| const string ManifestFile = "db_manifest.txt"; |
|
|
| public static string Install(string subdir, Action<string> log) |
| { |
| var dest = Path.Combine(Application.persistentDataPath, subdir); |
| var destManifest = Path.Combine(dest, ManifestFile); |
|
|
| string srcManifest; |
| try |
| { |
| srcManifest = StreamingAssetsModelLoader.LoadText(subdir + "/" + ManifestFile, "[RAG]"); |
| } |
| catch (Exception e) |
| { |
| log?.Invoke("[RAG] no DB manifest in StreamingAssets/" + subdir + " (" + e.Message + |
| "), run OnDeviceAgent ▸ RAG ▸ Ingest Knowledge to DB first."); |
| Directory.CreateDirectory(dest); |
| return dest; |
| } |
|
|
| |
| if (File.Exists(destManifest)) |
| { |
| try |
| { |
| var destText = File.ReadAllText(destManifest); |
| if (string.Equals(destText, srcManifest, StringComparison.Ordinal)) |
| { |
| log?.Invoke("[RAG] DB already up-to-date at " + dest + "."); |
| return dest; |
| } |
| log?.Invoke("[RAG] DB stale at " + dest + ", refreshing from StreamingAssets/" + subdir + "."); |
| } |
| catch (Exception e) |
| { |
| log?.Invoke("[RAG] stale-check failed (" + e.Message + "), refreshing."); |
| } |
|
|
| try { Directory.Delete(dest, true); } |
| catch (Exception e) { log?.Invoke("[RAG] couldn't clear stale DB: " + e.Message); } |
| } |
|
|
| Directory.CreateDirectory(dest); |
|
|
| var copied = 0; |
| foreach (var raw in srcManifest.Replace("\r", string.Empty).Split('\n')) |
| { |
| var rel = raw.Trim(); |
| if (rel.Length == 0 || rel.StartsWith("#")) continue; |
| try |
| { |
| var content = StreamingAssetsModelLoader.LoadText(subdir + "/" + rel, "[RAG]"); |
| var outPath = Path.Combine(dest, rel.Replace('/', Path.DirectorySeparatorChar)); |
| Directory.CreateDirectory(Path.GetDirectoryName(outPath)); |
| File.WriteAllText(outPath, content); |
| copied++; |
| } |
| catch (Exception e) |
| { |
| log?.Invoke("[RAG] DB copy failed for " + rel + ": " + e.Message); |
| } |
| } |
|
|
| |
| try { File.WriteAllText(destManifest, srcManifest); } catch (Exception e) { log?.Invoke("[RAG] manifest write failed: " + e.Message); } |
| log?.Invoke("[RAG] installed DB to " + dest + " (" + copied + " files)."); |
| return dest; |
| } |
| } |
| } |
|
|