fix: show cache loading progress
This commit is contained in:
@@ -66,11 +66,7 @@ export class LocalBrowserLLMProvider implements LLMProvider {
|
|||||||
LOCAL_LLM_MODEL_URL,
|
LOCAL_LLM_MODEL_URL,
|
||||||
LOCAL_LLM_MODEL_FILENAME,
|
LOCAL_LLM_MODEL_FILENAME,
|
||||||
progress => {
|
progress => {
|
||||||
if (progress.fromCache) {
|
LocalLLMModelManager.notifyLoadProgress(progress.receivedBytes, progress.totalBytes, progress.fromCache);
|
||||||
LocalLLMModelManager.notifyLoadStart(true);
|
|
||||||
} else {
|
|
||||||
LocalLLMModelManager.notifyLoadProgress(progress.receivedBytes, progress.totalBytes, false);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -13,6 +13,42 @@ export interface CachedModelStreamResult {
|
|||||||
cacheWritePromise: Promise<void> | null;
|
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 {
|
export class LocalLLMModelCache {
|
||||||
public static async exists(filename: string = LOCAL_LLM_MODEL_FILENAME): Promise<boolean> {
|
public static async exists(filename: string = LOCAL_LLM_MODEL_FILENAME): Promise<boolean> {
|
||||||
return cache.exists(filename);
|
return cache.exists(filename);
|
||||||
@@ -41,14 +77,8 @@ export class LocalLLMModelCache {
|
|||||||
|
|
||||||
if (await this.exists(filename)) {
|
if (await this.exists(filename)) {
|
||||||
const file = await this.getFile(filename);
|
const file = await this.getFile(filename);
|
||||||
onProgress?.({
|
|
||||||
receivedBytes: file.size,
|
|
||||||
totalBytes: file.size,
|
|
||||||
percent: 100,
|
|
||||||
fromCache: true,
|
|
||||||
});
|
|
||||||
return {
|
return {
|
||||||
reader: file.stream().getReader(),
|
reader: createProgressReader(file, onProgress),
|
||||||
totalBytes: file.size,
|
totalBytes: file.size,
|
||||||
fromCache: true,
|
fromCache: true,
|
||||||
cacheWritePromise: null,
|
cacheWritePromise: null,
|
||||||
|
|||||||
@@ -116,8 +116,8 @@ export class LocalLLMModelManager {
|
|||||||
|
|
||||||
public static notifyLoadStart(fromCache: boolean): void {
|
public static notifyLoadStart(fromCache: boolean): void {
|
||||||
this.setState({
|
this.setState({
|
||||||
isDownloading: !fromCache,
|
isDownloading: true,
|
||||||
progressPercent: fromCache ? 100 : 0,
|
progressPercent: 0,
|
||||||
progressText: fromCache ? 'Loading local language model from browser cache...' : 'Downloading local language model...',
|
progressText: fromCache ? 'Loading local language model from browser cache...' : 'Downloading local language model...',
|
||||||
error: '',
|
error: '',
|
||||||
});
|
});
|
||||||
@@ -127,10 +127,12 @@ export class LocalLLMModelManager {
|
|||||||
const receivedMb = (receivedBytes / (1024 * 1024)).toFixed(1);
|
const receivedMb = (receivedBytes / (1024 * 1024)).toFixed(1);
|
||||||
const totalMb = totalBytes ? (totalBytes / (1024 * 1024)).toFixed(1) : null;
|
const totalMb = totalBytes ? (totalBytes / (1024 * 1024)).toFixed(1) : null;
|
||||||
this.setState({
|
this.setState({
|
||||||
isDownloading: !fromCache,
|
isDownloading: true,
|
||||||
progressPercent: totalBytes ? (receivedBytes / totalBytes) * 100 : 0,
|
progressPercent: totalBytes ? (receivedBytes / totalBytes) * 100 : 0,
|
||||||
progressText: fromCache
|
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
|
: totalMb
|
||||||
? `Downloading local language model... ${receivedMb} / ${totalMb} MB`
|
? `Downloading local language model... ${receivedMb} / ${totalMb} MB`
|
||||||
: `Downloading local language model... ${receivedMb} MB`,
|
: `Downloading local language model... ${receivedMb} MB`,
|
||||||
@@ -143,7 +145,7 @@ export class LocalLLMModelManager {
|
|||||||
isCached: true,
|
isCached: true,
|
||||||
isDownloading: false,
|
isDownloading: false,
|
||||||
progressPercent: 100,
|
progressPercent: 100,
|
||||||
progressText: 'Local language model is ready.',
|
progressText: 'Using the local browser model. No external API requests are being sent.',
|
||||||
error: '',
|
error: '',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user