'use strict'; // Node 20+. Read one request from stdin and print candidate JSON. const fs=require('node:fs'); const path=require('node:path'); const {decode,tokenize,spans,roleRanker}=require('./inference.js'); if(require.main===module) { try { const request=JSON.parse(fs.readFileSync(0,'utf8')); if(typeof request.text!=='string'||!request.text.trim()||Array.from(request.text).length>5000) throw Error('Provide text with 1–5,000 Unicode code points.'); const points=Array.from(request.text),tokens=tokenize(request.text); const ends=new Map(tokens.map(([, ,end],i)=>[end,i+1])); const starts=new Map(tokens.map(([,start],i)=>[start,i])); const model=decode(JSON.parse(fs.readFileSync(path.join(__dirname,'role-ranker.json'),'utf8'))); let triggers; if(request.trigger!==undefined) { const {start,end,event_type}=request.trigger; if(!Number.isInteger(start)||!Number.isInteger(end)||end<=start||!starts.has(start)||!ends.has(end)|| !Object.hasOwn(model.allowed_roles,event_type)) throw Error('Provide a supported event type and exact token-boundary offsets.'); triggers=[{start,end,type:event_type,text:points.slice(start,end).join('')}]; } else { const detector=decode(JSON.parse(fs.readFileSync(path.join(__dirname,'model.json'),'utf8'))); triggers=spans(detector,tokens,points); } const events=triggers.map((trigger,index)=>({ trigger,event_type:trigger.type,assertion_status:'unknown', participants:index>=18?[]:roleRanker(model,tokens,points,starts.get(trigger.start),ends.get(trigger.end),trigger.type) })); process.stdout.write(JSON.stringify({events,limit:18,warning:'Experimental participant proposals require source review.'},null,2)+'\n'); } catch (error) { process.stderr.write(error.message+'\n'); process.exitCode=1; } }