(
payload: Omit<BrowserWorkerRequest, 'id' | 'modelId'>,
callbacks: { progress?: (value: BrowserAiProgress) => void; onToken?: (chunk: string) => void } = {},
)
| 71 | failRuntime(event.message || 'Browser model worker failed.'); |
| 72 | }; |
| 73 | const request = ( |
| 74 | payload: Omit<BrowserWorkerRequest, 'id' | 'modelId'>, |
| 75 | callbacks: { progress?: (value: BrowserAiProgress) => void; onToken?: (chunk: string) => void } = {}, |
| 76 | ): Promise<BrowserWorkerResponse> => new Promise((resolve, reject) => { |
| 77 | if (disposed) { reject(fatalFailure ?? new Error('The browser model was unloaded. Load it again to continue.')); return; } |
| 78 | if (pending) { reject(new Error('The browser model is already busy.')); return; } |
| 79 | const id = ++sequence; |
| 80 | pending = { id, kind: payload.kind, resolve, reject, ...callbacks }; |
| 81 | try { worker.postMessage({ ...payload, id, modelId } satisfies BrowserWorkerRequest); } |
| 82 | catch (error) { |
| 83 | if (isGpuRuntimeFailure(error)) failRuntime(error); |
| 84 | else { pending = undefined; reject(error instanceof Error ? error : new Error(runtimeErrorDetail(error))); } |
| 85 | } |
| 86 | }); |
| 87 | return { |
| 88 | prepare: async (progress, options) => { await request({ kind: 'prepare', ...options?.cacheOnly ? { cacheOnly: true } : {} }, { progress }); }, |
| 89 | countTokens: async messages => { |
no test coverage detected