worker.mjs
javascript · 23 lines · Exact bytes
Version & provenance · browser-local-cm5-v1
Recorded SHA-256 7937fa401d41e107638cd9f424e01fb7576f851632832367e0588e1e434e14caRendered SHA-256 7937fa401d41e107638cd9f424e01fb7576f851632832367e0588e1e434e14ca
Apache-2.0 License notes. Line links are stable within this snapshot. CM-5 browser integration v1; browser helper remains three plies, not the live seven-ply helper, derived from the validated desktop WebGPU prototype. FP32 export of checkpoint c0a3f67ac4b364276028950b2010c5644e03efb3ca0f1228b32c36b8b5340238; model ONNX SHA-256 f453044633683745fb519bd28d7f5174990f05c3329e072c17906938d827e13d. These are the exact integrated original frontend/policy/worker/runner files; third-party dependencies are separately licensed. ONNX Runtime Web 1.24.3 and chess.js 1.4.0. Website bundling changes paths and presentation but preserves model-scoring code. This source subset omits the exporter, which remains part of the recorded prototype validation, and is not a standalone build bundle. Device-dependent mate-helper budget may change overrides. No GPU memory or universal performance guarantee.
1import {Chess} from 'chess.js';
2import {Runner} from './runner.mjs';
3import {score,forcedMate,outcome} from './policy.mjs';
4let runner=null, busy=false;
5self.onmessage=async ({data})=> {
6 const {id,type}=data;
7 if(busy) {postMessage({id,type:'error',message:'A request is already running.'});return;}
8 busy=true;
9 try {
10 if(type==='load') {runner=await Runner.create(data.manifest,progress=>postMessage({id,type:'progress',progress}));postMessage({id,type:'ready',deviceInfo:runner.deviceInfo});}
11 else if(type==='move' || type==='validate') {
12 if(!runner)throw Error('Load the model first');
13 const board=new Chess();for(const move of data.moves)board.move({from:move.slice(0,2),to:move.slice(2,4),promotion:move[4]});
14 const start=performance.now();
15 const tactical=type==='validate'?{status:'disabled'}:forcedMate(board);
16 if(type==='move' && outcome(board))throw Error('The game has ended');
17 const result=tactical.status==='proven'?{move:tactical.move}:await score(board,runner);
18 postMessage({id,type:'result',...result,tactical,ms:performance.now()-start,peakOwnedCacheBytes:runner.peakOwnedCacheBytes});
19 }
20 } catch(e) {postMessage({id,type:'error',message:e.message});}
21 finally {busy=false;}
22};
23