Skip to content

Commit 03c26ac

Browse files
committed
capture and expose download errors
1 parent dd87f85 commit 03c26ac

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

Runtime/LLM.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ public class LLM : MonoBehaviour
7979
public bool started { get; protected set; } = false;
8080
/// <summary> Boolean set to true if the server has failed to start. </summary>
8181
public bool failed { get; protected set; } = false;
82+
/// <summary> Boolean set to true if the models were not downloaded successfully. </summary>
83+
public static bool downloadFailed { get; protected set; } = false;
8284
/// <summary> Boolean set to true if the server has started and is ready to receive requests, false otherwise. </summary>
83-
public static bool modelsDownloaded { get; protected set; } = false;
85+
public static bool downloadComplete { get; protected set; } = false;
8486

8587
/// <summary> the LLM model to use.
8688
/// Models with .gguf format are allowed.</summary>
@@ -110,9 +112,10 @@ public async void Awake()
110112
{
111113
if (!enabled) return;
112114
#if !UNITY_EDITOR
113-
await LLMManager.DownloadModels();
115+
downloadFailed = !await LLMManager.DownloadModels();
114116
#endif
115-
modelsDownloaded = true;
117+
downloadComplete = true;
118+
if (downloadFailed) return;
116119
await AndroidSetup();
117120
string arguments = GetLlamaccpArguments();
118121
if (arguments == null) return;
@@ -147,10 +150,11 @@ public async Task WaitUntilReady()
147150
while (!started) await Task.Yield();
148151
}
149152

150-
public static async Task WaitUntilModelsDownloaded(Callback<float> downloadProgressCallback = null)
153+
public static async Task<bool> WaitUntilModelsDownloaded(Callback<float> downloadProgressCallback = null)
151154
{
152155
if (downloadProgressCallback != null) LLMManager.downloadProgressCallbacks.Add(downloadProgressCallback);
153-
while (!modelsDownloaded) await Task.Yield();
156+
while (!downloadComplete) await Task.Yield();
157+
return !downloadFailed;
154158
}
155159

156160
/// <summary>

Runtime/LLMManager.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class LLMManager
3232
{
3333
public static float downloadProgress = 1;
3434
public static List<Callback<float>> downloadProgressCallbacks = new List<Callback<float>>();
35-
static Task downloadModelsTask;
35+
static Task<bool> downloadModelsTask;
3636
static readonly object lockObject = new object();
3737
static long totalSize;
3838
static long currFileSize;
@@ -44,7 +44,7 @@ public static void SetDownloadProgress(float progress)
4444
foreach (Callback<float> downloadProgressCallback in downloadProgressCallbacks) downloadProgressCallback?.Invoke(downloadProgress);
4545
}
4646

47-
public static Task DownloadModels()
47+
public static Task<bool> DownloadModels()
4848
{
4949
lock (lockObject)
5050
{
@@ -53,10 +53,10 @@ public static Task DownloadModels()
5353
return downloadModelsTask;
5454
}
5555

56-
public static async Task DownloadModelsOnce()
56+
public static async Task<bool> DownloadModelsOnce()
5757
{
5858
if (Application.platform == RuntimePlatform.Android) await LLMUnitySetup.AndroidExtractFile(LLMUnitySetup.BuildFilename);
59-
if (!File.Exists(LLMUnitySetup.BuildFile)) return;
59+
if (!File.Exists(LLMUnitySetup.BuildFile)) return true;
6060

6161
List<StringPair> downloads = new List<StringPair>();
6262
using (FileStream fs = new FileStream(LLMUnitySetup.BuildFile, FileMode.Open, FileAccess.Read))
@@ -71,7 +71,7 @@ public static async Task DownloadModelsOnce()
7171
}
7272
}
7373
}
74-
if (downloads.Count == 0) return;
74+
if (downloads.Count == 0) return true;
7575

7676
try
7777
{
@@ -100,9 +100,10 @@ public static async Task DownloadModelsOnce()
100100
}
101101
catch (Exception ex)
102102
{
103-
LLMUnitySetup.LogError($"Error downloading the models");
104-
throw ex;
103+
LLMUnitySetup.LogError($"Error downloading the models: {ex.Message}");
104+
return false;
105105
}
106+
return true;
106107
}
107108

108109
#if UNITY_EDITOR

0 commit comments

Comments
 (0)