com.sky.ondeviceagent / Runtime /AgentCore /Rag /StreamingDbInstaller.cs
Sky-Kim's picture
Initial commit
2e7837a
Raw
History Blame Contribute Delete
3.26 kB
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;
}
// WriteManifest stamps an ISO timestamp so any new ingest changes the manifest text - stale installs are detected automatically.
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);
}
}
// Write manifest last so a partial copy isn't mistaken for a complete install on next launch.
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;
}
}
}