LittlePlyResearch notebook

worker.mjs

javascript · 23 lines · Exact bytes

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

Recorded SHA-256 fa4f2ac12639b61f468d39e4add97c05005177cff30e09a2667d7a5fa9d4f41fRendered SHA-256 fa4f2ac12639b61f468d39e4add97c05005177cff30e09a2667d7a5fa9d4f41f

Apache-2.0 License notes. Line links are stable within this snapshot. CM-7 schema-2 state-input browser integration; FP32 selected update2232 checkpoint 29e68a8e85336cbb1f56efdad36bd76217728b6a376d1e5f74e89887d21383da. ONNX Runtime Web 1.24.3, chess.js 1.4.0. Exact integrated source; standard-game UI, fixed masked120-slot prompt,512 raw output classes. Three-ply browser helper is separate from live seven-ply search. Original UI and terminal PGN export preserved; prototype scorer/codec/worker/runner unchanged except adding PGN export utility.

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=data.startFen?new Chess(data.startFen):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,data.historyPlies??16);
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