LittlePlyResearch notebook

worker.mjs

javascript · 23 lines · Exact bytes

Line 1 ↗
Version & provenance · browser-local-v1

Recorded SHA-256 7937fa401d41e107638cd9f424e01fb7576f851632832367e0588e1e434e14caRendered SHA-256 7937fa401d41e107638cd9f424e01fb7576f851632832367e0588e1e434e14ca

Apache-2.0 License notes. Line links are stable within this snapshot. Browser integration v1, derived from the validated desktop WebGPU prototype. FP32 export of checkpoint 6133648b2926b38542ee3ee1251a1ba111d61da9a8ea69d1c5f0559b09933643; model ONNX SHA-256 e16f6aab5bb02f8b90014f15b55f423d1ac59d6941c9e3bebb5e6bda557499a4. 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