WaveCut/Bonsai-Chat-WebGPU
5
1export interface WebGpuDeviceLostDetails {2 recoverable: true;3 nextAction: 'reload-model';4 stage: 'load' | 'generate' | 'score-sequence' | 'backend-report';5 modelId: string;6 cpuFallbackAvailable: boolean;7 cause: 'memory-pressure' | 'device-loss';8 signal: {9 line: string;10 reason: number | null;11 message: string | null;12 };13}14 15export type ResourceFailureKind = 'context-capacity' | 'memory-pressure';16 17export function classifyResourceFailure(18 error: unknown,19 nativeLog: readonly string[] = [],20): ResourceFailureKind | null {21 const errorRecord = typeof error === 'object' && error !== null22 ? error as { message?: unknown; name?: unknown; type?: unknown }23 : null;24 const diagnostic = [25 typeof error === 'string' ? error : '',26 typeof errorRecord?.name === 'string' ? errorRecord.name : '',27 typeof errorRecord?.message === 'string' ? errorRecord.message : '',28 typeof errorRecord?.type === 'string' ? errorRecord.type : '',29 ...nativeLog,30 ].join('\n');31 if (32 /\bkv[_\s-]?cache(?:[_\s-]+is)?[_\s-]+full\b|running out of context capacity|context (?:window|capacity).*(?:full|exhausted)|(?:prompt|tokens?).*(?:exceed|too (?:long|large)).*(?:context|n_ctx)/iu.test(diagnostic)33 ) {34 return 'context-capacity';35 }36 if (37 /\bout[ -]of[ -](?:device[ -])?memory\b|\boom\b|GPUOutOfMemoryError|memory access out of bounds|cannot (?:enlarge|allocate).*memory|failed to (?:allocate|create).*(?:buffer|memory)|(?:buffer|memory) allocation failed/iu.test(diagnostic)38 ) {39 return 'memory-pressure';40 }41 return null;42}43 44export class EngineRuntimeError extends Error {45 readonly code: string;46 readonly details?: unknown;47 48 constructor(code: string, message: string, details?: unknown) {49 super(message);50 this.name = 'EngineRuntimeError';51 this.code = code;52 this.details = details;53 }54}55 56export function abortError(): DOMException {57 return new DOMException('The engine operation was aborted.', 'AbortError');58}59 60export function throwIfAborted(signal: AbortSignal): void {61 if (signal.aborted) {62 throw abortError();63 }64}65 