fix: show cache loading progress
This commit is contained in:
@@ -13,6 +13,42 @@ export interface CachedModelStreamResult {
|
||||
cacheWritePromise: Promise<void> | null;
|
||||
}
|
||||
|
||||
const createProgressReader = (
|
||||
file: File,
|
||||
onProgress?: (progress: ModelDownloadProgress & { fromCache: boolean }) => void,
|
||||
): ReadableStreamDefaultReader<Uint8Array> => {
|
||||
const sourceReader = file.stream().getReader();
|
||||
let receivedBytes = 0;
|
||||
|
||||
const monitoredStream = new ReadableStream<Uint8Array>({
|
||||
async pull(controller) {
|
||||
const { done, value } = await sourceReader.read();
|
||||
if (done) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
|
||||
receivedBytes += value.byteLength;
|
||||
onProgress?.({
|
||||
receivedBytes,
|
||||
totalBytes: file.size,
|
||||
percent: file.size > 0 ? (receivedBytes / file.size) * 100 : 0,
|
||||
fromCache: true,
|
||||
});
|
||||
controller.enqueue(value);
|
||||
},
|
||||
async cancel(reason) {
|
||||
await sourceReader.cancel(reason);
|
||||
},
|
||||
});
|
||||
|
||||
return monitoredStream.getReader();
|
||||
};
|
||||
|
||||
export class LocalLLMModelCache {
|
||||
public static async exists(filename: string = LOCAL_LLM_MODEL_FILENAME): Promise<boolean> {
|
||||
return cache.exists(filename);
|
||||
@@ -41,14 +77,8 @@ export class LocalLLMModelCache {
|
||||
|
||||
if (await this.exists(filename)) {
|
||||
const file = await this.getFile(filename);
|
||||
onProgress?.({
|
||||
receivedBytes: file.size,
|
||||
totalBytes: file.size,
|
||||
percent: 100,
|
||||
fromCache: true,
|
||||
});
|
||||
return {
|
||||
reader: file.stream().getReader(),
|
||||
reader: createProgressReader(file, onProgress),
|
||||
totalBytes: file.size,
|
||||
fromCache: true,
|
||||
cacheWritePromise: null,
|
||||
|
||||
@@ -116,8 +116,8 @@ export class LocalLLMModelManager {
|
||||
|
||||
public static notifyLoadStart(fromCache: boolean): void {
|
||||
this.setState({
|
||||
isDownloading: !fromCache,
|
||||
progressPercent: fromCache ? 100 : 0,
|
||||
isDownloading: true,
|
||||
progressPercent: 0,
|
||||
progressText: fromCache ? 'Loading local language model from browser cache...' : 'Downloading local language model...',
|
||||
error: '',
|
||||
});
|
||||
@@ -127,10 +127,12 @@ export class LocalLLMModelManager {
|
||||
const receivedMb = (receivedBytes / (1024 * 1024)).toFixed(1);
|
||||
const totalMb = totalBytes ? (totalBytes / (1024 * 1024)).toFixed(1) : null;
|
||||
this.setState({
|
||||
isDownloading: !fromCache,
|
||||
isDownloading: true,
|
||||
progressPercent: totalBytes ? (receivedBytes / totalBytes) * 100 : 0,
|
||||
progressText: fromCache
|
||||
? 'Loading local language model from browser cache...'
|
||||
? totalMb
|
||||
? `Loading local language model from browser cache... ${receivedMb} / ${totalMb} MB`
|
||||
: `Loading local language model from browser cache... ${receivedMb} MB`
|
||||
: totalMb
|
||||
? `Downloading local language model... ${receivedMb} / ${totalMb} MB`
|
||||
: `Downloading local language model... ${receivedMb} MB`,
|
||||
@@ -143,7 +145,7 @@ export class LocalLLMModelManager {
|
||||
isCached: true,
|
||||
isDownloading: false,
|
||||
progressPercent: 100,
|
||||
progressText: 'Local language model is ready.',
|
||||
progressText: 'Using the local browser model. No external API requests are being sent.',
|
||||
error: '',
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user