diff --git "a/index.html" "b/index.html" --- "a/index.html" +++ "b/index.html" @@ -1,48 +1,48 @@ - + evalcull - +`:``;t=l.encode(Me(e)+n)}else t=r.lineWithEol(o);e.push(t),u+=t.length}o%ql===0&&(await new Promise(e=>setTimeout(e,0)),i&&i(o,a.lineCount))}let d=new Uint8Array(u),f=0;for(let t of e)d.set(t,f),f+=t.length;u>0&&o.push(d)}return i&&i(a.lineCount,a.lineCount),new Blob(o,{type:`application/jsonl`})}var Yl=5e3;async function Xl(e,t,n,r){let i=[],a=Ke(t);for(let o=0;osetTimeout(e,0)),r&&r(o,e.count))}return r&&r(e.count,e.count),new Blob(i,{type:`application/jsonl`})}var Zl=5e3;function Ql(e){let t={status:e?.status??`unreviewed`};return e?.compare&&(t.compare=e.compare),e?.labels&&e.labels.length>0&&(t.labels=e.labels),e?.score!==void 0&&(t.score=e.score),e?.notes!==void 0&&(t.notes=e.notes),t.edited=!!(e?.edits&&Object.keys(e.edits).length>0),t.updatedAt=e?.updatedAt??0,t}function $l(e,t,n){let r=new Map;if(e.kind!==`jsonl`||n.kind!==`candidates`&&n.kind!==`preference`)return r;for(let[n,i]of t){let t=n.indexOf(`:`);if(t===-1)continue;let a=Ge(n),o=Number(n.slice(t+1));if(a>=e.index.lineCount||o>=e.index.candidates[a])continue;let s=r.get(a)??[];s.push([o,i]),r.set(a,s)}for(let e of r.values())e.sort((e,t)=>e[0]-t[0]);return r}var eu=new Set([32,9,10,13]),tu=new TextEncoder().encode(`"_triage"`);function nu(e,t){outer:for(let n=0;n+t.length<=e.length;n++){for(let r=0;rt&&eu.has(e[n]);)n--;if(n<=t||e[n]!==125)return null;for(let t=0;t+1{let n=Ql(t),r=c.get(e);if(r){let e={};for(let[t,n]of r){let{compare:r,...i}=n;e[String(t)]=Ql(i)}n.candidates=e}return n},d=(e,n)=>{let r=t.get(We(e));if(!n.ok)return Me({_raw:n.raw,_triage:u(e,r)})??``;let i=qe(t,l,e)?.edits??{},a=n.data;for(let[e,t]of Object.entries(i))a=Yn(a,Gn(e),t);let o=u(e,r);o.edited=Object.keys(i).length>0;let s=typeof a==`object`&&!!a&&!Array.isArray(a)&&!ne(a),c;if(s){let{_triage:e,...t}=a;c={...t,_triage:o}}else c={_value:a,_triage:o};return Me(c)??``};if(e.kind===`jsonl`){let n=e.index,r=new Set(n.unparseable),c=new TextDecoder(`utf-8`,{ignoreBOM:!0});for await(let n of Kl(e)){let e=new ru(Math.ceil(n.bytes.length*1.75)+4096);for(let o=n.first;o<=n.last;o++){if(!a||a(o)){let i=n.line(o),a=Object.keys(qe(t,l,o)?.edits??{}).length>0,s=r.has(o)||a?null:q(i);if(s){let r=u(o,t.get(We(o)));r.edited=!1,e.bytes(i.subarray(0,s.close)),e.text(`${s.empty?``:`,`}"_triage":${Me(r)}}`),e.bytes(n.lineWithEol(o).subarray(i.length))}else{let t=c.decode(i),r;try{r={index:o,ok:!0,data:Fe(t)}}catch(e){r={index:o,ok:!1,error:String(e),raw:t}}e.text(d(o,r)),e.bytes(n.lineWithEol(o).subarray(i.length))}}o%Zl===0&&(await new Promise(e=>setTimeout(e,0)),i&&i(o,s))}o.push(e.result())}}else for(let e=0;esetTimeout(e,0)),i&&i(e,s));return i&&i(s,s),new Blob(o,{type:`application/jsonl`})}function au(e){let t=[];for(let n of e)t.push(Me(n)+` +`);return new Blob(t,{type:`application/jsonl`})}function ou(e,t){let n=t.roleKey,r=t.contentKey,i=String(e[n]??``),a=String(e[r]??``);return t.roles.assistant.includes(i)?i=`assistant`:t.roles.user.includes(i)?i=`user`:t.roles.system.includes(i)&&(i=`system`),{role:i,content:a}}function su(e,t){try{return Jn(e,Gn(t))}catch{return}}function cu(e,t,n,r){let i=[],a=[],o=0,s=0,c=0,l=0,u=[];for(let[t,n]of e)n.compare?.length&&u.push({ref:t,lineIdx:Ge(t),decision:n});u.sort((e,t)=>e.lineIdx-t.lineIdx);let d=e=>r&&r(e)?void 0:n(e);for(let{lineIdx:e,decision:n}of u)for(let r of n.compare){if(r.preference===`tie`){o++;continue}if(r.preference===`both_bad`){s++;continue}let n=d(e),a=r.preference===`left`?r.left:r.right,u=r.preference===`left`?r.right:r.left,f=d(Ge(a)),p=d(Ge(u));if(n===void 0||f===void 0||p===void 0){l++;continue}let m=fu(n,{ref:a,record:f},{ref:u,record:p},t);m&&(`prompt`in m||c++,i.push(m))}let f=[];for(let[t,n]of e)n.edits&&Object.keys(n.edits).length>0&&f.push({ref:t,lineIdx:Ge(t),decision:n});f.sort((e,t)=>e.lineIdx-t.lineIdx);for(let{lineIdx:e,decision:i}of f){if(r&&r(e)){l++;continue}let o=n(e);if(o===void 0){l++;continue}let s=mu(o,i.edits,t);s&&a.push(s)}return{pairs:i,editPairs:a,excludedTies:o,excludedBothBad:s,implicitPromptCount:c,skippedUnparseable:l}}function lu(e,t){for(let n=e.length-1;n>=0;n--){let r=e[n],i=String(r[t.roleKey]??``);if(t.roles.assistant.includes(i))return n}return-1}function uu(e){let t=e.indexOf(`:`);return t===-1?-1:parseInt(e.slice(t+1),10)}function du(e,t,n){switch(n.kind){case`flat`:{let e=su(t,n.responsePath);return typeof e==`string`?{ok:!0,value:e}:{ok:!1}}case`messages`:{let e=su(t,n.path);if(!Array.isArray(e))return{ok:!1};let r=lu(e,n);return r<0?{ok:!1}:{ok:!0,value:[ou(e[r],n)]}}case`candidates`:{let r=su(t,n.candidatesPath),i=uu(e);return!Array.isArray(r)||i<0||i>=r.length?{ok:!1}:{ok:!0,value:r[i]}}case`preference`:{let r=uu(e);return r===0?{ok:!0,value:su(t,n.chosenPath)}:r===1?{ok:!0,value:su(t,n.rejectedPath)}:{ok:!1}}default:return{ok:!1}}}function fu(e,t,n,r){let i=du(t.ref,t.record,r),a=du(n.ref,n.record,r);if(!i.ok||!a.ok)return null;switch(r.kind){case`messages`:{let t=su(e,r.path);if(!Array.isArray(t))return null;let n=lu(t,r);return n<0?null:{prompt:t.slice(0,n).map(e=>ou(e,r)),chosen:i.value,rejected:a.value}}case`flat`:case`candidates`:{let t=su(e,r.promptPath);return typeof t==`string`?{prompt:t,chosen:i.value,rejected:a.value}:null}case`preference`:if(r.promptPath){let t=su(e,r.promptPath);if(typeof t==`string`)return{prompt:t,chosen:i.value,rejected:a.value}}return{chosen:i.value,rejected:a.value};default:return null}}function pu(e,t,n){let r=t.length;if(e.length!==r+2)return-1;for(let n=0;n=r.length)continue;let s=r[o][n.contentKey];return{prompt:r.map(e=>ou(e,n)).slice(0,o),chosen:[{role:`assistant`,content:a}],rejected:[{role:`assistant`,content:s}]}}}else if(n.kind===`flat`){let r=su(e,n.promptPath),i=su(e,n.responsePath),a=t[n.responsePath];if(a===void 0)return null;if(typeof r==`string`)return{prompt:r,chosen:a,rejected:i}}return null}var hu=5e3;async function gu(e,t,n){let{source:r,decisions:i,format:a,record:o,opts:s={}}=t,c=s.bom??`keep`,l=s.excludeUnparseable??!1,u=new Set;if(r.kind===`jsonl`)for(let e of r.index.unparseable)u.add(e);let d=e=>u.has(e),f={name:e,byteExact:!1,lineCount:0,pairsCount:0,editPairsCount:0,tiesExcluded:0,bothBadExcluded:0,implicitPromptCount:0,editsExported:0,unparseableExcluded:0,skippedUnparseable:0},p;switch(e){case`pairs.vertex.jsonl`:throw Error(`pairs.vertex.jsonl not implemented until WP 12`);case`pairs.jsonl`:case`pairs.edits.jsonl`:{let t=new Set;for(let[e,n]of i){(n.compare?.length||n.edits&&Object.keys(n.edits).length>0)&&t.add(Ge(e));for(let e of n.compare??[])t.add(Ge(e.left)),t.add(Ge(e.right))}let r=new Map,s=Array.from(t).sort((e,t)=>e-t);for(let e=0;esetTimeout(e,0)),n&&n(e,s.length))}let c=cu(i,a,e=>r.get(e),l?d:void 0);f.tiesExcluded=c.excludedTies,f.bothBadExcluded=c.excludedBothBad,f.implicitPromptCount=c.implicitPromptCount,f.skippedUnparseable=c.skippedUnparseable,e===`pairs.jsonl`?(p=au(c.pairs),f.pairsCount=c.pairs.length,f.lineCount=c.pairs.length):(p=au(c.editPairs),f.editPairsCount=c.editPairs.length,f.lineCount=c.editPairs.length);break}case`all.decisions.jsonl`:p=await iu(r,i,o,a,n,l?e=>!d(e)||(f.unparseableExcluded++,!1):void 0),f.lineCount=(r.kind===`jsonl`?r.index.lineCount:r.count)-f.unparseableExcluded;for(let e of i.values())e.edits&&Object.keys(e.edits).length>0&&f.editsExported++;break;case`kept.jsonl`:case`rejected.jsonl`:case`skipped.jsonl`:{let t=e.replace(`.jsonl`,``),a=(e,n)=>{if(l&&d(e))return f.unparseableExcluded++,!1;let r=n?.status===t;return r&&(f.lineCount++,n?.edits&&Object.keys(n.edits).length>0&&f.editsExported++),r};r.kind===`jsonl`?(f.byteExact=!0,p=await Jl(r,i,a,{bom:c},n)):p=await Xl(r,i,a,n);break}default:throw Error(`Unknown export name: ${e}`)}return n&&n(f.lineCount,f.lineCount),{blob:p,stats:f}}function _u(e,t,n,r,i={}){let a=t.kind===`jsonl`?t.index.lineCount:t.count,o=Ke(n),s=new Set([`kept.jsonl`,`rejected.jsonl`,`skipped.jsonl`]).has(e)&&t.kind===`jsonl`,c=0,l=0,u=0,d=new Set;if(t.kind===`jsonl`)for(let e of t.index.unparseable)d.add(e);if(e===`kept.jsonl`||e===`rejected.jsonl`||e===`skipped.jsonl`){let t=e.replace(`.jsonl`,``);for(let e=0;e0&&l++)}}else if(e===`all.decisions.jsonl`){c=a,i.excludeUnparseable&&(u=d.size,c-=u);for(let e of n.values())e.edits&&Object.keys(e.edits).length>0&&l++}let f=0,p=0,m=0,h=0;if(e===`pairs.jsonl`){for(let[,e]of n)for(let t of e.compare??[])t.preference===`tie`?p++:t.preference===`both_bad`?m++:f++;r.kind===`preference`&&!r.promptPath&&(h=f)}let g=0;if(e===`pairs.edits.jsonl`)for(let[,e]of n)e.edits&&Object.keys(e.edits).length>0&&g++;return{name:e,byteExact:s,lineCount:c,pairsCount:f,editPairsCount:g,tiesExcluded:p,bothBadExcluded:m,implicitPromptCount:h,editsExported:l,unparseableExcluded:u,skippedUnparseable:0}}var vu=[`pairs.jsonl`,`pairs.edits.jsonl`,`kept.jsonl`,`rejected.jsonl`,`skipped.jsonl`,`all.decisions.jsonl`],yu=`a DPO row needs one chosen and one rejected completion for the same prompt. Strength is carried in all.decisions.jsonl only.`,bu=`Edit pairs are off-policy: the chosen side was written by a person, not sampled from the model. DPO is sensitive to that distribution shift, and human-written chosen text can teach a length or style bias. Use them knowingly.`,xu=`Browser storage is best-effort: Safari deletes it after seven days without a visit. Download the session to keep it.`;function Su(e,t,n){switch(e){case`pairs.jsonl`:{let e=[`${ol(t.pairsCount,`pair`,`pairs`)}. ${ol(t.tiesExcluded,`tie`,`ties`)} and ${ol(t.bothBadExcluded,`both-unacceptable pair`,`both-unacceptable pairs`)} were left out: ${yu}`];return t.implicitPromptCount>0&&e.push(`${ol(t.implicitPromptCount,`row has no prompt field and uses`,`rows have no prompt field and use`)} TRL's implicit-prompt format.`),e.push(`Keep and Reject decisions alone never produce a preference pair.`),e}case`pairs.edits.jsonl`:return[`${ol(t.editPairsCount,`pair`,`pairs`)} from edits.`,bu];case`all.decisions.jsonl`:return[`${ol(t.lineCount,`line`,`lines`)}.`,`Every record with its edits applied and its _triage decisions. Re-serialised; the original stays in the source file.`];default:return[`${ol(t.lineCount,`line`,`lines`)}.`,n?`Unedited lines are copied byte for byte, including line endings. Only the lines you edited are re-serialised.`:`Rows are re-serialised as JSONL.`]}}function Cu(e){let t=H(e=>e.fingerprint);return(0,U.jsx)(wu,{...e},t)}function wu({open:e,onOpenChange:t}){let n=H(e=>e.source),r=H(e=>e.decisions),i=H(e=>e.format),a=H(e=>e.session),o=H(e=>e.newerLocalCopy),s=H(e=>e.switching.kind===`opening`),[c,l]=(0,_.useState)(`keep`),[u,d]=(0,_.useState)(!1),[f,p]=(0,_.useState)({}),[m,h]=(0,_.useState)(null),[g,v]=(0,_.useState)(null),[y,b]=(0,_.useState)(null),x=y!==null&&a.kind===`refused`&&a.reason===`fingerprint-mismatch`&&a.request===y,S=(0,_.useRef)(null),C=n?.kind===`jsonl`,ee=n?.kind===`jsonl`&&n.index.hasBom,w=n?.kind===`jsonl`?n.index.unparseable.length:0,T=(0,_.useMemo)(()=>{if(!e||!n)return null;let t={};for(let e of vu)t[e]=_u(e,n,r,i,{excludeUnparseable:u});return t},[e,n,r,i,u]);(0,_.useEffect)(()=>{p({})},[r,i,c,u]);let E=async e=>{let t=H.getState();if(t.source){h(e),v(null);try{let n=await gu(e,{source:t.source,decisions:t.decisions,format:t.format,record:t.record,opts:{bom:c,excludeUnparseable:u}});Pl(n.blob,e),p(t=>({...t,[e]:n.stats}))}catch(e){v(`Export failed: ${e instanceof Error?e.message:String(e)}`)}finally{h(null)}}},D=async e=>{if(!e)return;let t=await e.text(),n={text:t,name:e.name};(0,ii.flushSync)(()=>b(n)),await H.getState().importSession(t,{name:e.name,request:n})};return(0,U.jsx)(vc,{open:e,onOpenChange:t,children:(0,U.jsxs)(xc,{className:`max-w-dialog-wide dialog-max overflow-y-auto`,children:[(0,U.jsxs)(Sc,{children:[(0,U.jsx)(wc,{children:`Export`}),(0,U.jsx)(Tc,{children:`Downloads are written by your browser; nothing is uploaded.`})]}),(0,U.jsxs)(`div`,{className:`flex flex-wrap items-center gap-4 text-chrome`,children:[ee&&(0,U.jsx)(rr,{tone:`quiet`,word:`BOM present`}),(0,U.jsxs)(`fieldset`,{className:`flex items-center gap-2`,children:[(0,U.jsx)(`legend`,{className:`sr-only`,children:`Byte order mark`}),(0,U.jsx)(`span`,{className:`text-neutral-11`,children:`BOM:`}),[`keep`,`strip`,`add`].map(e=>(0,U.jsxs)(`label`,{className:`flex items-center gap-1`,children:[(0,U.jsx)(`input`,{type:`radio`,name:`bom`,value:e,checked:c===e,onChange:()=>l(e)}),e]},e))]}),w>0&&(0,U.jsxs)(`label`,{className:`flex items-center gap-1`,children:[(0,U.jsx)(`input`,{type:`checkbox`,checked:u,onChange:e=>d(e.target.checked)}),`Exclude `,ol(w,`unparseable line`,`unparseable lines`)]})]}),g&&(0,U.jsx)(ir,{children:g}),(0,U.jsx)(`div`,{className:`flex flex-col gap-3`,children:T&&vu.map(e=>{let t=f[e]??T[e];return(0,U.jsxs)(`section`,{"aria-label":e,className:`rounded-panel border border-neutral-6 p-3`,children:[(0,U.jsxs)(`div`,{className:`flex items-center justify-between gap-2`,children:[(0,U.jsx)(`h3`,{className:`font-mono text-mono font-semibold`,children:e}),(0,U.jsx)($s,{size:`sm`,variant:`secondary`,disabled:m!==null,onClick:()=>E(e),"aria-label":`Download ${e}`,children:m===e?`Exporting…`:`Download`})]}),Su(e,t,C).map(e=>(0,U.jsx)(`p`,{className:`text-chrome text-neutral-11 mt-1`,children:e},e))]},e)})}),(0,U.jsxs)(`section`,{"aria-label":`Session`,className:`rounded-panel border border-neutral-6 p-3 flex flex-col gap-2`,children:[(0,U.jsx)(`h3`,{className:`label text-neutral-11`,children:`Session`}),(0,U.jsx)(`p`,{className:`text-chrome text-neutral-11`,children:xu}),(0,U.jsxs)(`div`,{className:`flex gap-2`,children:[(0,U.jsx)($s,{size:`sm`,variant:`secondary`,onClick:Fl,children:`Download session`}),(0,U.jsx)($s,{size:`sm`,variant:`secondary`,disabled:s,onClick:()=>S.current?.click(),children:`Load session`}),(0,U.jsx)(`input`,{ref:S,type:`file`,accept:`.json`,className:`hidden`,"aria-label":`Session file`,onChange:e=>{D(e.target.files?.[0]),e.target.value=``}})]}),a.kind===`refused`&&(0,U.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,U.jsx)(ir,{children:a.message}),x&&(0,U.jsx)($s,{size:`sm`,variant:`secondary`,disabled:s,onClick:async()=>{y!==null&&await H.getState().importSession(y.text,{force:!0,name:y.name})},children:`Load anyway`})]}),o&&(0,U.jsxs)(`div`,{className:`text-chrome text-neutral-12 flex items-center gap-2`,children:[(0,U.jsxs)(`span`,{children:[`A newer copy saved in this browser at `,new Date(o.savedAt).toLocaleString(`en-US`),` `,`differs from the session file.`]}),(0,U.jsx)($s,{size:`sm`,variant:`secondary`,disabled:s,onClick:()=>H.getState().applyNewerLocalCopy(),children:`Use the newer copy`})]})]})]})})}function Tu({open:e,onOpenChange:t,candidateRef:n,subject:r,focusField:i}){let[a,o]=(0,_.useState)([]),[s,c]=(0,_.useState)(``),[l,u]=(0,_.useState)(``),[d,f]=(0,_.useState)(``),p=(0,_.useRef)(null),m=(0,_.useRef)(null);(0,_.useEffect)(()=>{if(!e)return;let t=H.getState().decisions.get(n);o(t?.labels??[]),c(``),u(t?.score===void 0?``:String(t.score)),f(t?.notes??``)},[e,n]);let h=()=>{let e=s.trim();e&&!a.includes(e)&&o([...a,e]),c(``)},g=()=>{let e=H.getState(),r=e.decisions.get(n),i=s.trim(),o=i&&!a.includes(i)?[...a,i]:a,c=l.trim()===``?void 0:Number(l),u=d===``?void 0:d;(r?.labels??[]).join(`\0`)!==o.join(`\0`)&&e.setLabels(n,o),r?.score!==c&&!(c!==void 0&&Number.isNaN(c))&&e.setScore(n,c),r?.notes!==u&&e.setNotes(n,u),t(!1)};return(0,U.jsx)(vc,{open:e,onOpenChange:t,children:(0,U.jsxs)(xc,{className:`max-w-panel-md`,onKeyDown:e=>{e.key===`Enter`&&(e.metaKey||e.ctrlKey)&&(e.preventDefault(),g())},onOpenAutoFocus:e=>{e.preventDefault(),(i===`notes`?m.current:p.current)?.focus()},children:[(0,U.jsxs)(Sc,{children:[(0,U.jsx)(wc,{children:`Labels and notes`}),(0,U.jsxs)(Tc,{children:[`For `,r??n,`. `,Dr(`mod+enter`),` saves, Esc closes without saving.`]})]}),(0,U.jsxs)(`div`,{className:`flex flex-col gap-3 text-chrome`,children:[(0,U.jsxs)(`label`,{className:`flex flex-col gap-1`,children:[(0,U.jsx)(`span`,{className:`label text-neutral-11`,children:`Labels`}),(0,U.jsx)(`div`,{className:`flex flex-wrap gap-1`,children:a.map(e=>(0,U.jsxs)(`span`,{className:`flex items-center gap-1 rounded-control bg-neutral-3 px-2 py-1 text-neutral-12`,children:[e,(0,U.jsx)(`button`,{type:`button`,className:`rounded-control px-1`,"aria-label":`Remove label ${e}`,onClick:()=>o(a.filter(t=>t!==e)),children:`×`})]},e))}),(0,U.jsx)(`input`,{ref:p,"aria-label":`Labels`,value:s,onChange:e=>c(e.target.value),onKeyDown:e=>{e.key===`Enter`&&!e.metaKey&&!e.ctrlKey||e.key===`,`?(e.preventDefault(),h()):e.key===`Backspace`&&s===``&&a.length&&o(a.slice(0,-1))},className:`rounded-control border border-neutral-9 bg-neutral-1 px-2 py-1 text-chrome text-neutral-12`})]}),(0,U.jsxs)(`label`,{className:`flex flex-col gap-1`,children:[(0,U.jsx)(`span`,{className:`label text-neutral-11`,children:`Score`}),(0,U.jsx)(`input`,{"aria-label":`Score`,type:`number`,value:l,onChange:e=>u(e.target.value),className:`w-score rounded-control border border-neutral-9 bg-neutral-1 px-2 py-1 text-chrome text-neutral-12`,"data-numeric":!0})]}),(0,U.jsxs)(`label`,{className:`flex flex-col gap-1`,children:[(0,U.jsx)(`span`,{className:`label text-neutral-11`,children:`Notes`}),(0,U.jsx)(`textarea`,{ref:m,"aria-label":`Notes`,value:d,onChange:e=>f(e.target.value),className:`field-min-sm rounded-control border border-neutral-9 bg-neutral-1 px-2 py-1 text-content text-neutral-12`})]})]}),(0,U.jsxs)(`div`,{className:`flex justify-end gap-2`,children:[(0,U.jsx)($s,{variant:`secondary`,onClick:()=>t(!1),children:`Cancel`}),(0,U.jsx)($s,{onClick:g,children:`Save`})]})]})})}function Eu({open:e,onOpenChange:t,mode:n,total:r}){let[i,a]=(0,_.useState)(``),[o,s]=(0,_.useState)(null),c=n===`compare`?`Go to prompt`:`Go to record`,l=e=>{e||(a(``),s(null)),t(e)},u=()=>{let e=H.getState();if(i.trim()===``){e.nextUnreviewed(),l(!1);return}let t=Number(i);!Number.isInteger(t)||t<1||t>r?s(`Enter 1 to ${r.toLocaleString(`en-US`)}.`):(e.goTo(t-1),l(!1))};return(0,U.jsx)(vc,{open:e,onOpenChange:l,children:(0,U.jsxs)(xc,{className:`max-w-goto`,children:[(0,U.jsxs)(Sc,{children:[(0,U.jsx)(wc,{children:c}),(0,U.jsx)(Tc,{children:`Enter on an empty field goes to the next unreviewed.`})]}),(0,U.jsx)(`input`,{"aria-label":n===`compare`?`Prompt number`:`Record number`,type:`number`,autoFocus:!0,value:i,onChange:e=>a(e.target.value),onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),u())},className:`rounded-control border border-neutral-9 bg-neutral-1 px-2 py-1 text-chrome text-neutral-12`,"data-numeric":!0}),o&&(0,U.jsx)(ir,{children:o})]})})}var Du=5e3;async function Ou(e,t){let{index:n}=e,r=n.lineCount,i=t.query.toLowerCase();if(r===0||i===``)return{kind:`none`};let a,o;if(t.mode===`compare`){let e=t.cursor;a=n.groups.indexOf(e),a===-1&&(a=0),o=t=>n.groups[t]===e}else a=t.cursor,o=e=>e===t.cursor;let s=new TextDecoder,c=!1,l=[];a+1<=r-1&&l.push([a+1,r-1]),l.push([0,Math.min(a,r-1)]);for(let[r,a]of l)for(let l=r;l<=a;l+=Du){let r=Math.min(l+Du-1,a),u=new Uint8Array(await(await e.readRawRange(l,r)).arrayBuffer()),d=n.starts[l];for(let e=l;e<=r;e++)if(s.decode(u.subarray(n.starts[e]-d,n.ends[e]-d)).toLowerCase().includes(i)){if(o(e))c=!0;else return{kind:`match`,line:e}}if(await new Promise(e=>setTimeout(e,0)),t.isStale())return{kind:`stale`}}return c?{kind:`only-current`}:{kind:`none`}}function ku({onClose:e}){let t=H(e=>e.cursor),[n,r]=(0,_.useState)(``),[i,a]=(0,_.useState)(null),[o,s]=(0,_.useState)(!1),c=(0,_.useRef)(0);(0,_.useEffect)(()=>{c.current++},[t]),(0,_.useEffect)(()=>()=>{c.current++},[]);let l=async()=>{let{source:e,mode:t,cursor:r,goTo:i}=H.getState();if(!e||e.kind!==`jsonl`||n===``)return;let o=++c.current;s(!0),a(null);let l=await Ou(e,{query:n,mode:t,cursor:r,isStale:()=>c.current!==o});s(!1),c.current===o&&(l.kind===`match`?i(t===`compare`?e.index.groups[l.line]:l.line):l.kind===`only-current`?a(t===`compare`?`Only match is in this prompt.`:`Only match is in this record.`):l.kind===`none`&&a(`No match.`))};return(0,U.jsxs)(`div`,{role:`search`,className:`flex items-center gap-2 border-b border-neutral-6 bg-neutral-2 px-4 py-2 text-chrome text-neutral-12 animate-panel`,children:[(0,U.jsx)(`input`,{"aria-label":`Search`,autoFocus:!0,value:n,onChange:e=>r(e.target.value),onKeyDown:t=>{t.key===`Enter`?(t.preventDefault(),l()):t.key===`Escape`&&(t.preventDefault(),c.current++,e())},className:`w-panel rounded-control border border-neutral-9 bg-neutral-1 px-2 py-1 text-chrome text-neutral-12`}),(0,U.jsx)(`span`,{className:`text-neutral-11`,children:`Searches the file's raw text.`}),o&&(0,U.jsx)(`span`,{className:`text-neutral-11`,children:`Searching…`}),i&&(0,U.jsx)(`span`,{role:`status`,children:i})]})}var Au=[`shift`,`alt`,`meta`,`mod`,`ctrl`,`control`];function ju(){return typeof navigator>`u`?!1:/mac/i.test(navigator.userAgent)&&!/iphone|ipad|ipod/i.test(navigator.userAgent)}var Mu={esc:`escape`,return:`enter`,left:`arrowleft`,right:`arrowright`,up:`arrowup`,down:`arrowdown`,ShiftLeft:`shift`,ShiftRight:`shift`,AltLeft:`alt`,AltRight:`alt`,MetaLeft:`meta`,MetaRight:`meta`,OSLeft:`meta`,OSRight:`meta`,ControlLeft:`ctrl`,ControlRight:`ctrl`};function Nu(e){return(Mu[e.trim()]||e.trim()).toLowerCase().replace(/key|digit|numpad/,``)}function Pu(e){return Au.includes(e)}function Fu(e,t=`,`){return e.toLowerCase().split(t)}function Iu(e,t=`+`,n=`>`,r=!1,i,a){let o=[],s=!1;e=e.trim(),e.includes(n)?(s=!0,o=e.toLocaleLowerCase().split(n).map(e=>Nu(e))):o=e.toLocaleLowerCase().split(t).map(e=>Nu(e));let c={alt:o.includes(`alt`),ctrl:o.includes(`ctrl`)||o.includes(`control`),shift:o.includes(`shift`),meta:o.includes(`meta`),mod:o.includes(`mod`),useKey:r},l=o.filter(e=>!Au.includes(e));return{...c,keys:l,description:i,isSequence:s,hotkey:e,metadata:a}}typeof document<`u`&&(document.addEventListener(`keydown`,e=>{e.code!==void 0&&Bu([Nu(e.code)])}),document.addEventListener(`keyup`,e=>{e.code!==void 0&&Vu([Nu(e.code)])})),typeof window<`u`&&(window.addEventListener(`blur`,()=>{Lu.clear()}),window.addEventListener(`focus`,()=>{Lu.clear()}),window.addEventListener(`contextmenu`,()=>{setTimeout(()=>{Lu.clear()},0)})),typeof document<`u`&&document.addEventListener(`visibilitychange`,()=>{Lu.clear()});var Lu=new Set;function Ru(e){return Array.isArray(e)}function zu(e,t=`,`){return(Ru(e)?e:e.split(t)).every(e=>Lu.has(e.trim().toLowerCase()))}function Bu(e){let t=Array.isArray(e)?e:[e];Lu.has(`meta`)&&Lu.forEach(e=>{Pu(e)||Lu.delete(e.toLowerCase())}),t.forEach(e=>{Lu.add(e.toLowerCase())})}function Vu(e){e===`meta`?Lu.clear():(Array.isArray(e)?e:[e]).forEach(e=>{Lu.delete(e.toLowerCase())})}function Hu(e,t,n){(typeof n==`function`&&n(e,t)||n===!0)&&e.preventDefault()}function Uu(e,t,n){return typeof n==`function`?n(e,t):n===!0||n===void 0}var Wu=[`input`,`textarea`,`select`,`searchbox`,`slider`,`spinbutton`,`menuitem`,`menuitemcheckbox`,`menuitemradio`,`option`,`radio`,`textbox`];function Gu(e){return Ku(e,Wu)}function Ku(e,t=!1){let{target:n,composed:r}=e,i,a;return qu(n)&&r?(i=e.composedPath()[0]&&e.composedPath()[0].tagName,a=e.composedPath()[0]&&e.composedPath()[0].role):(i=n&&n.tagName,a=n&&n.role),Ru(t)?!!(i&&t?.some(e=>e.toLowerCase()===i.toLowerCase()||e===a)):!!(i&&t&&t)}function qu(e){return!!e.tagName&&!e.tagName.startsWith(`-`)&&e.tagName.includes(`-`)}function Ju(e,t){return e.length===0&&t?!1:!t||e.some(e=>t.includes(e))||e.includes(`*`)}var Yu=(e,t,n=!1)=>{let{alt:r,meta:i,mod:a,shift:o,ctrl:s,keys:c,useKey:l}=t,{code:u,key:d,ctrlKey:f,metaKey:p,shiftKey:m,altKey:h}=e,g=Nu(u),_=typeof e.getModifierState==`function`&&e.getModifierState(`AltGraph`);if(!l&&!c?.includes(g)&&![`ctrl`,`control`,`unknown`,`meta`,`alt`,`shift`,`os`].includes(g))return!1;if(!n){if(r!==h&&g!==`alt`||o!==m&&g!==`shift`)return!1;if(a){if(ju()?!p:!f)return!1}else if(i!==p&&g!==`meta`&&g!==`os`||s!==f&&g!==`ctrl`&&g!==`control`)return!1;if(_&&!r&&!s&&!a&&![`alt`,`ctrl`,`control`].includes(g))return!1}return l&&c?.length===1&&c.includes(d.toLowerCase())||c&&c.length===1&&c.includes(g)?!0:c&&c.length>0?c.includes(g)?zu(c):!1:!c||c.length===0},Xu=(0,_.createContext)(void 0),Zu=()=>(0,_.useContext)(Xu);function J(e,t){return e&&t&&typeof e==`object`&&typeof t==`object`?Object.keys(e).length===Object.keys(t).length&&Object.keys(e).reduce((n,r)=>n&&J(e[r],t[r]),!0):e===t}var Qu=(0,_.createContext)({hotkeys:[],activeScopes:[],toggleScope:()=>{},enableScope:()=>{},disableScope:()=>{}}),Y=()=>(0,_.useContext)(Qu);function X(e){let t=(0,_.useRef)(void 0);return J(t.current,e)||(t.current=e),t.current}var Z=e=>{e.stopPropagation(),e.preventDefault(),e.stopImmediatePropagation()},$u=typeof window<`u`?_.useLayoutEffect:_.useEffect;function ed(e){if(!e)return;let{enabled:t,preventDefault:n,ignoreEventWhen:r,...i}=e;return typeof t==`function`?i:{...i,enabled:t}}function Q(e,t,n,r){let[i,a]=(0,_.useState)(null),o=(0,_.useCallback)(e=>(a(e),()=>a(null)),[]),s=(0,_.useRef)(!1),c=Array.isArray(n)?Array.isArray(r)?void 0:r:n,l=Ru(e)?e.join(c?.delimiter):e,u=Array.isArray(n)?n:Array.isArray(r)?r:void 0,d=(0,_.useCallback)(t,u??[]),f=(0,_.useRef)(d);f.current=u?d:t;let p=X(ed(c)),m=(0,_.useRef)(c?.enabled);m.current=c?.enabled;let h=(0,_.useRef)(c?.preventDefault);h.current=c?.preventDefault;let g=(0,_.useRef)(c?.ignoreEventWhen);g.current=c?.ignoreEventWhen;let{activeScopes:v}=Y(),y=Zu();return $u(()=>{if(m.current===!1||!Ju(v,p?.scopes))return;let e=[],t,n=(n,r=!1)=>{if(!Gu(n)||Ku(n,p?.enableOnFormTags)){if(i!==null){let e=i.getRootNode();if((e instanceof Document||e instanceof ShadowRoot)&&e.activeElement!==i&&!i.contains(e.activeElement)){Z(n);return}}n.target?.isContentEditable&&!p?.enableOnContentEditable||Fu(l,p?.delimiter).forEach(i=>{if(i.includes(p?.splitKey??`+`)&&i.includes(p?.sequenceSplitKey??`>`)){console.warn(`Hotkey ${i} contains both ${p?.splitKey??`+`} and ${p?.sequenceSplitKey??`>`} which is not supported.`);return}let a=Iu(i,p?.splitKey,p?.sequenceSplitKey,p?.useKey,p?.description,p?.metadata);if(a.isSequence){t=setTimeout(()=>{e=[]},p?.sequenceTimeoutMs??1e3);let r=a.useKey?n.key:Nu(n.code);if(Pu(r.toLowerCase()))return;if(e.push(r),r!==a.keys?.[e.length-1]){e=[],t&&clearTimeout(t);return}e.length===a.keys?.length&&(f.current(n,a),t&&clearTimeout(t),e=[])}else if(Yu(n,a,p?.ignoreModifiers)||a.keys?.includes(`*`)){if(g.current?.(n)||r&&s.current||(Hu(n,a,h.current),!Uu(n,a,m.current)))return;f.current(n,a),r||(s.current=!0)}})}},r=e=>{e.code!==void 0&&(Bu(Nu(e.code)),(p?.keydown===void 0&&p?.keyup!==!0||p?.keydown)&&n(e))},a=e=>{e.code!==void 0&&(Vu(Nu(e.code)),s.current=!1,p?.keyup&&n(e,!0))},o=i||c?.document||document;return o.addEventListener(`keyup`,a,c?.eventListenerOptions),o.addEventListener(`keydown`,r,c?.eventListenerOptions),y&&Fu(l,p?.delimiter).forEach(e=>{y.addHotkey(Iu(e,p?.splitKey,p?.sequenceSplitKey,p?.useKey,p?.description,p?.metadata))}),()=>{o.removeEventListener(`keyup`,a,c?.eventListenerOptions),o.removeEventListener(`keydown`,r,c?.eventListenerOptions),y&&Fu(l,p?.delimiter).forEach(e=>{y.removeHotkey(Iu(e,p?.splitKey,p?.sequenceSplitKey,p?.useKey,p?.description,p?.metadata))}),e=[],t&&clearTimeout(t)}},[i,p,v,l]),o}function td(e){let{prefer:t,setStrength:n,decide:r,getFocusedRef:i,next:a,prev:o,toggleFocus:s,openEditor:c,saveEditor:l,cancelEditor:u,editConfirmKeep:d,editConfirmDiscard:f,isEditConfirmOpen:p,undo:m,redo:h,toggleRaw:g,toggleWhitespace:v,toggleReadingStep:y,cycleFilter:b,showCheatSheet:x,scrollUp:S,scrollDown:C,toggleMetadata:ee,openLabels:w,openNotes:T,openGoTo:E,openSearch:D,mode:O,groupSize:k,isModalOpen:A,characterKeys:j=!0,isEditorOpen:te,isEditPending:M=!1}=e,N=(0,_.useCallback)(e=>!A&&!!(e.modes.includes(`both`)||e.modes.includes(O)),[A,O]);Q(`left`,()=>{p?f():N(Dc[0])&&(k<=1||t(`left`))},{enableOnFormTags:!1}),Q(`1`,()=>{p||N(Dc[0])&&(k<=1||t(`left`))},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`right`,()=>{p?d():N(Dc[1])&&(k<=1||t(`right`))},{enableOnFormTags:!1}),Q(`2`,()=>{p||N(Dc[1])&&(k<=1||t(`right`))},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`=, 0`,()=>{N(Dc[2])&&(k<=1||t(`tie`))},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`x`,()=>{N(Dc[3])&&(k<=1||t(`both_bad`))},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`shift+1`,()=>{N(Dc[4])&&n(1)},{enabled:j,enableOnFormTags:!1}),Q(`shift+2`,()=>{N(Dc[5])&&n(2)},{enabled:j,enableOnFormTags:!1}),Q(`shift+3`,()=>{N(Dc[6])&&n(3)},{enabled:j,enableOnFormTags:!1}),Q(`a`,()=>{A||r(i(),`kept`)},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`r`,()=>{A||r(i(),`rejected`)},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`s`,()=>{A||r(i(),`skipped`)},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`tab`,e=>{A||O!==`compare`||k<=1||(e.preventDefault(),s())},{enableOnFormTags:!1}),Q(`j`,()=>{A||a()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`k`,()=>{A||o()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`up`,()=>{A||S()},{enableOnFormTags:!1}),Q(`down`,()=>{A||C()},{enableOnFormTags:!1}),Q(`e`,e=>{A||(e.preventDefault(),c())},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`mod+enter`,()=>{te&&l()},{enableOnFormTags:[`textarea`]}),Q(`escape`,()=>{(te||p||M)&&u()},{enableOnFormTags:[`textarea`]}),Q(`u`,()=>{A||m()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`shift+u`,()=>{A||h()},{enabled:j,enableOnFormTags:!1}),Q(`t`,()=>{A||g()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`w`,()=>{A||v()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`z`,()=>{A||y()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`f`,()=>{A||b()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`shift+?`,()=>{A||x()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`m`,()=>{A||ee?.()},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`l`,e=>{A||(e.preventDefault(),w?.())},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`n`,e=>{A||(e.preventDefault(),T?.())},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`g`,e=>{A||(e.preventDefault(),E?.())},{enabled:j,enableOnFormTags:!1,useKey:!0}),Q(`/`,e=>{A||(e.preventDefault(),D?.())},{enabled:j,enableOnFormTags:!1,useKey:!0})}function nd(e,t){if(t.kind!==`messages`)return[];let n=Jn(e,Gn(t.path));if(!Array.isArray(n))return[];let r=[];return n.forEach((e,n)=>{let i=e&&typeof e==`object`?e[t.roleKey]:void 0;typeof i==`string`&&t.roles.assistant.includes(i)&&r.push(n)}),r}function rd(e,t){return Xn(qn(Gn(e),e=>{let n=Jn(t,e);return Array.isArray(n)?n.length:0}))}function id(e,t){return`${e.path}[${t}].${e.contentKey}`}function ad(e,t,n){let r=e.indexOf(`:`),i=r===-1?null:parseInt(e.slice(r+1),10);switch(t.kind){case`flat`:return t.responsePath;case`messages`:{let e=nd(n,t);return e.length?id(t,e[e.length-1]):null}case`preference`:return i===1?t.rejectedPath:t.chosenPath;case`candidates`:return`${t.candidatesPath}[${i??0}]`;default:return null}}var od=[`all`,`unreviewed`,`kept`,`rejected`,`skipped`,`edited`,`unparseable`];function sd(e,t,n){return!e||!n?null:e.fingerprint===t&&e.line===Ge(n)?e:null}var cd={compare:e=>e?`Which response is better? Press ← or →, or use the buttons below. = if both are good, X if both are bad.`:`Which response is better? Press ← or →, or use the buttons below. Tie if both are good, Both bad if both are bad.`,round2:()=>`The response you preferred stays as Winner so far and meets the next one.`,single:e=>e?`Keep, reject or skip each record with A, R or S, or the buttons below.`:`Keep, reject or skip each record with the buttons below.`};function ld({onAnnounce:e}){let t=H(e=>e.source),n=H(e=>e.format),r=H(e=>e.mode),i=H(e=>e.cursor),a=H(e=>e.focus),o=H(e=>e.decisions),s=H(e=>e.filter),c=H(e=>e.prefer),l=H(e=>e.setStrength),u=H(e=>e.decide),d=H(e=>e.edit),f=H(e=>e.undo),p=H(e=>e.redo),m=H(e=>e.next),h=H(e=>e.prev),g=H(e=>e.toggleFocus),v=H(e=>e.setFilter),y=H(e=>e.setFormat),b=H(e=>e.record),x=H(e=>e.session),S=H(e=>e.past),C=H(e=>e.firstUnreviewed),[ee,w]=(0,_.useState)(!1),[T,E]=(0,_.useState)(!1),[D,O]=(0,_.useState)(!1);(0,_.useEffect)(()=>{let e=document.documentElement;return D?e.setAttribute(`data-reading-step`,``):e.removeAttribute(`data-reading-step`),()=>e.removeAttribute(`data-reading-step`)},[D]);let[k,A]=(0,_.useState)(!1),[j,te]=(0,_.useState)(Rc),M=(0,_.useCallback)(e=>{te(e),zc(e)},[]),[N,ne]=(0,_.useState)(!1),[re,ie]=(0,_.useState)(!1),ae=(0,_.useRef)(null),[oe,se]=(0,_.useState)(!1),[P,ce]=(0,_.useState)(null),[le,ue]=(0,_.useState)(null),[de,fe]=(0,_.useState)(null),[pe,me]=(0,_.useState)(null),[he,F]=(0,_.useState)(null),[I,ge]=(0,_.useState)(null),[_e,ve]=(0,_.useState)(null),ye=H(e=>e.storageState),be=H(e=>e.sourceName),L=H(e=>e.counts),[xe,Se]=(0,_.useState)(!1),[Ce,we]=(0,_.useState)(null),[Te,Ee]=(0,_.useState)(!1),[De,Oe]=(0,_.useState)(!1),[ke,Ae]=(0,_.useState)(!1),[je,Ne]=(0,_.useState)(!1),Pe=H(e=>e.switching.kind===`opening`),Le=H(e=>e.fingerprint);(0,_.useEffect)(()=>{ce(null),fe(null),ue(null),we(null),Ee(!1),Oe(!1),ne(!1)},[Le]);let ze=t?.kind===`jsonl`?t.index:null,Ve=ze?.groups,He=ze?.groupCount??0,Ue=ze?.candidates,Ke=(0,_.useMemo)(()=>Ve?Ie(Ve,He):null,[Ve,He]),qe=(0,_.useMemo)(()=>r!==`compare`||!Ke||!Ue||i>=He?null:Re(Ke,Ue,n,i),[r,Ke,Ue,He,n,i]),Je=(0,_.useMemo)(()=>qe?Be(qe.candidates,o.get(qe.promptRef)?.compare):null,[qe,o]),Ye=qe?.candidates.length??0,Xe=r===`single`||Ye<2,Ze=r===`compare`?Ye:1,R=t?r===`single`?We(i):qe?Je?.shown?.left??qe.candidates[0]??qe.promptRef:``:``,z=!Xe&&Je?.shown?Je.shown.right:``,Qe=e=>Math.max(qe?.candidates.indexOf(e)??0,0)+1,$e=e=>r===`compare`?`Response ${Qe(e)}`:`Record ${Ge(e)+1}`,et=e=>r===`compare`?`response ${Qe(e)} of prompt ${i+1}`:`record ${Ge(e)+1}`,[tt,nt]=(0,_.useState)(``),rt=(0,_.useCallback)(t=>{nt(t),e(t)},[e]),it=(0,_.useRef)(null),at=(0,_.useRef)(null);(0,_.useEffect)(()=>{if(!t||t.kind!==`jsonl`||it.current===Le)return;if(it.current=Le,x.kind!==`restored`){at.current=null,rt(``);return}let e=r===`compare`?t.index.groupCount:t.index.lineCount,n=r===`compare`?`prompt`:`record`,a=r===`compare`&&Ye>2&&Je?.shown?`, comparison ${Je.shown.pair+1} of ${Je.totalPairs}`:``;at.current=S.length,rt(`Resumed where you left off: ${n} ${i+1} of ${e}${a}. ${L().reviewed} of ${e} ${n}s done.`)},[t,Le,x.kind,r,Ye,Je,S.length,i,L,rt]),(0,_.useEffect)(()=>{at.current!==null&&S.length!==at.current&&(at.current=null,rt(``))},[S.length,rt]);let[ot,st]=(0,_.useState)([]),ct=(0,_.useCallback)(e=>{Hc(e),st(t=>t.includes(e)?t:[...t,e])},[]),lt=t?r===`single`?`single`:!Xe&&Je?.shown?Je.shown.pair===0?`compare`:`round2`:null:null,ut=lt&&!ot.includes(lt)&&!Vc(lt)?lt:null,dt=(0,_.useRef)(S.length),ft=(0,_.useRef)(null);(0,_.useEffect)(()=>{let e=ft.current;if(S.length>dt.current&&e){let t=S[S.length-1];(e!==`single`&&t?.type===`compare`||e===`single`&&t?.type===`status`)&&ct(e)}dt.current=S.length,ft.current=ut},[S,ut,ct]);let pt=null;if(r===`compare`)for(let e=S.length-1;e>=0;e--){let t=S[e];if(t.type===`compare`){pt=o.get(t.ref)?.compare?.[t.pair]?.strength;break}}let mt=sd(I,Le,R),ht=sd(_e,Le,z),B=mt?.record??null,gt=ht?.record??null,_t=mt?.text??``,vt=ht?.text??``;(0,_.useEffect)(()=>{if(!t||t.kind!==`jsonl`||!R)return;let e=!1,n=Le;return(async()=>{try{let r=Ge(R),i=await b(r),a=await t.readText(r);if(e)return;if(ge({fingerprint:n,line:r,record:i,text:a}),z){let r=Ge(z),i=await b(r),a=await t.readText(r);if(e)return;ve({fingerprint:n,line:r,record:i,text:a})}else ve(null);F(null)}catch(t){if(e)return;F(`Failed to load records: ${t instanceof Error?t.message:String(t)}`)}})(),()=>{e=!0}},[t,Le,R,z,b]),(0,_.useEffect)(()=>{t&&n.kind===`unknown`&&ae.current!==Le&&(ae.current=Le,ne(!0))},[t,n.kind,Le]),(0,_.useEffect)(()=>{N||ie(!1)},[N]),(0,_.useEffect)(()=>{w(n.kind===`unknown`)},[n.kind]);let yt=(0,_.useMemo)(()=>o.get(R)?.edits,[o,R]),bt=(0,_.useMemo)(()=>o.get(z)?.edits,[o,z]),xt=(0,_.useCallback)(()=>Xe||a===0?R:z,[Xe,a,R,z]),St=(0,_.useCallback)((e,t,n)=>{try{let r=rd(t,n),i=o.get(e)?.edits,a=i&&Object.prototype.hasOwnProperty.call(i,r)?i[r]:Jn(n,Gn(r)),s=a!==void 0&&typeof a!=`string`;ce({ref:e,path:r,initialValue:s?Me(a)??``:a??``,isJson:s,data:n}),ue(null),F(null)}catch(e){F(`Failed to open editor: ${e instanceof Error?e.message:String(e)}`)}},[o]),Ct=(0,_.useCallback)(()=>{let e=a===0||Xe?B:gt;if(!B||!e){me({fingerprint:Le,cursor:i,focus:a});return}if(!B.ok||!e.ok)return;let t=xt(),r=ad(t,n,e.data);r===null?n.kind===`messages`&&F(`No assistant turn to edit.`):St(t,r,e.data)},[B,gt,a,Xe,n,xt,St,Le,i]),wt=(0,_.useCallback)(()=>{},[P]),Tt=(0,_.useCallback)(()=>{ce(null),fe(null),ue(null),me(null)},[]),Et=N||k||Ce!==null||Te||ke||je||Pe;(0,_.useEffect)(()=>{pe&&(pe.fingerprint!==Le||pe.cursor!==i||pe.focus!==a||Et?me(null):B&&(a===0||Xe?B:gt)&&(me(null),Ct()))},[pe,Le,i,a,Et,Xe,B,gt,Ct]),(0,_.useEffect)(()=>{me(null)},[o]);let Dt=(0,_.useCallback)(e=>{if(P)try{let t=P.path,n=e;if(P.isJson)try{n=Fe(e)}catch{ue(`Not valid JSON: not saved.`);return}let r=o.get(P.ref)?.edits,i=r&&Object.prototype.hasOwnProperty.call(r,t)?{value:r[t]}:null;d(P.ref,t,n),fe({ref:P.ref,path:t,originalValue:P.initialValue,previous:i}),ce(null),ue(null),F(null)}catch(e){F(`Failed to save edit: ${e instanceof Error?e.message:String(e)}`)}},[P,d,o]),Ot=(0,_.useCallback)(()=>{fe(null)},[]),kt=(0,_.useCallback)(()=>{de&&(d(de.ref,de.path,de.previous?de.previous.value:void 0),fe(null))},[de,d]),At=e=>{let t=document.querySelector(`[role="group"][aria-current="true"] [data-card-content]`);t&&(t.scrollTop+=e)},jt=(0,_.useCallback)(()=>At(-40),[]),Mt=(0,_.useCallback)(()=>At(40),[]),Nt=(0,_.useCallback)(()=>{let e=(od.indexOf(s)+1)%od.length;v(od[e])},[s,v]),Pt=(0,_.useCallback)(()=>{A(e=>!e)},[]),Ft=(0,_.useCallback)(async e=>{se(!0);try{await y(e)}finally{se(!1)}},[y]),It=(0,_.useCallback)((e,t)=>{if(Pe)return;let n=e===R?B:e===z?gt:null;n&&n.ok&&St(e,t,n.data)},[Pe,B,gt,R,z,St]),[Lt,Rt]=(0,_.useState)([]);if((0,_.useEffect)(()=>{if(!N||!t||t.kind!==`jsonl`)return;let e=!1;return(async()=>{let n=[],r=Math.min(t.index.lineCount,1e3);for(let e=0;e{e=!0}},[N,t,b]),td({prefer:c,setStrength:l,decide:u,getFocusedRef:xt,next:m,prev:h,toggleFocus:g,openEditor:Ct,saveEditor:wt,cancelEditor:Tt,editConfirmKeep:Ot,editConfirmDiscard:kt,isEditConfirmOpen:de!==null,undo:f,redo:p,toggleRaw:()=>w(e=>!e),toggleWhitespace:()=>E(e=>!e),toggleReadingStep:()=>O(e=>!e),cycleFilter:Nt,showCheatSheet:Pt,scrollUp:jt,scrollDown:Mt,toggleMetadata:()=>Se(e=>!e),openLabels:()=>we(`labels`),openNotes:()=>we(`notes`),openGoTo:()=>Ee(!0),openSearch:()=>Oe(!0),mode:r,groupSize:Ze,characterKeys:j,isModalOpen:P!==null||de!==null||N||k||Ce!==null||Te||ke||je||Pe,isEditorOpen:P!==null,isEditPending:pe!==null}),!t)return(0,U.jsx)(jl,{loaded:!1});let zt=P||de?`Save or cancel the edit first.`:Ce===null?N&&re?`Apply or cancel the format first.`:null:`Save or cancel the labels and notes first.`,Bt=(0,U.jsx)(jl,{loaded:!0,onConfirmOpenChange:Ne,blockedReason:zt}),Vt=(0,U.jsx)(kl,{side:`top`,className:`fixed top-2 right-4 rounded-panel bg-neutral-1 px-2 py-1`,children:(0,U.jsx)(Ll,{state:ye,blockedReason:zt})}),Ht=(e,t)=>(0,U.jsxs)(U.Fragment,{children:[e,t?Vt:null,Bt]});if(t.kind!==`jsonl`)return Ht((0,U.jsxs)(`div`,{className:`min-h-screen bg-neutral-1 text-neutral-12 flex flex-col items-center justify-center gap-4`,children:[(0,U.jsx)(`h1`,{className:`text-heading font-semibold`,children:`evalcull`}),(0,U.jsxs)(`p`,{className:`text-chrome text-neutral-11`,children:[t.container,` files are not supported yet`]})]}),!0);if(oe)return Ht((0,U.jsx)(`div`,{className:`min-h-screen bg-neutral-1 text-neutral-12 flex items-center justify-center`,children:(0,U.jsx)(`p`,{role:`status`,className:`text-chrome text-neutral-11`,children:`Re-grouping records...`})}),!0);if(P){let e;if(n.kind===`messages`){let t=nd(P.data,n),r=t.findIndex(e=>rd(id(n,e),P.data)===P.path);if(r!==-1){let i=e=>{e>=0&&ei(r-1),onNext:()=>i(r+1)}}}return Ht((0,U.jsx)(`div`,{"data-screen":`editor`,className:`min-h-screen bg-neutral-1 text-neutral-12 p-8`,children:(0,U.jsxs)(`div`,{className:`max-w-column mx-auto`,children:[(0,U.jsxs)(`h2`,{className:`text-heading font-semibold mb-4`,children:[`Editing `,et(P.ref)]}),(0,U.jsxs)(`p`,{className:`text-chrome text-neutral-11 mb-4`,children:[`Field: `,(0,U.jsx)(`code`,{className:`font-mono text-mono`,children:P.path})]}),(0,U.jsx)(kr,{initialValue:P.initialValue,onSave:Dt,onCancel:Tt,error:le,turnSwitch:e},`${P.ref} ${P.path}`)]})}),!0)}if(de){let e=(o.get(de.ref)?.edits??{})[de.path];return Ht((0,U.jsx)(U.Fragment,{children:(0,U.jsx)(tc,{originalValue:de.originalValue,editedValue:Zn(e),onKeep:Ot,onDiscard:kt,onDismiss:Tt})}),!0)}return Ht((0,U.jsxs)(`div`,{"data-screen":r,className:`min-h-screen bg-neutral-1 text-neutral-12`,children:[(0,U.jsxs)(`header`,{className:`border-b border-neutral-6 px-4 py-2 text-chrome`,children:[(0,U.jsxs)(`div`,{className:`flex flex-wrap items-center gap-4`,children:[(0,U.jsx)(`h1`,{className:`font-semibold`,children:`evalcull`}),(0,U.jsx)(`span`,{className:`text-neutral-12`,children:be}),(0,U.jsx)(Nl,{counts:L(),mode:r}),(0,U.jsx)(Ll,{state:ye,blockedReason:zt}),(0,U.jsxs)(`div`,{className:`ml-auto flex flex-wrap items-center justify-end gap-2`,children:[(0,U.jsx)($s,{variant:`secondary`,size:`sm`,disabled:Pe,onClick:()=>ne(!0),children:`Configure format`}),(0,U.jsx)($s,{variant:`secondary`,size:`sm`,onClick:()=>Ae(!0),children:`Export`}),(0,U.jsxs)($s,{variant:`secondary`,size:`sm`,"aria-keyshortcuts":j?`?`:void 0,onClick:()=>A(!0),children:[`Keyboard shortcuts`,j&&(0,U.jsx)(Mc,{children:`?`})]})]})]}),he&&(0,U.jsx)(ir,{className:`mt-1`,children:he}),tt&&(0,U.jsxs)(`div`,{"data-resume-line":!0,className:`mt-1 flex flex-wrap items-center gap-2 text-neutral-11`,children:[(0,U.jsx)(`span`,{children:tt}),(0,U.jsx)($s,{variant:`secondary`,size:`sm`,onClick:C,children:`Go to first unreviewed`}),(0,U.jsx)($s,{variant:`secondary`,size:`sm`,onClick:()=>rt(``),children:`Dismiss`})]})]}),De&&(0,U.jsx)(ku,{onClose:()=>Oe(!1)}),(0,U.jsxs)(`main`,{className:`p-4`,children:[(0,U.jsxs)(`div`,{className:`max-w-page mx-auto`,children:[(0,U.jsx)(ul,{record:B,format:n,showWhitespace:T}),ut&&(0,U.jsx)(Pc,{text:cd[ut](j),onDismiss:()=>ct(ut)})]}),r===`compare`?B&&(Xe||gt)?Xe||!Je?(0,U.jsxs)(`div`,{className:`max-w-column mx-auto`,children:[(0,U.jsx)(`div`,{className:`text-center text-chrome text-neutral-11 mb-4`,children:`Only one candidate for this prompt`}),(0,U.jsx)(wr,{record:B,rawText:_t,candidateRef:R,format:n,showRaw:ee,showWhitespace:T,edits:yt,onEdit:e=>It(R,e),status:o.get(R)?.status,title:$e(R)})]}):(0,U.jsx)(Cr,{leftRecord:B,rightRecord:gt,leftRef:R,rightRef:z,leftText:_t,rightText:vt,leftEdits:yt,rightEdits:bt,focus:a,format:n,showRaw:ee,showWhitespace:T,knockout:Je,candidateCount:Ye,onEdit:It,leftStatus:o.get(R)?.status,rightStatus:o.get(z)?.status,leftTitle:$e(R),rightTitle:$e(z)}):(0,U.jsx)(`div`,{className:`text-center text-chrome text-neutral-11`,children:`Loading...`}):B?(0,U.jsx)(wr,{record:B,rawText:_t,candidateRef:R,format:n,showRaw:ee,showWhitespace:T,edits:yt,onEdit:e=>It(R,e),status:o.get(R)?.status,title:$e(R)}):(0,U.jsx)(`div`,{className:`text-center text-chrome text-neutral-11`,children:`Loading...`}),(0,U.jsx)(`div`,{className:`max-w-page mx-auto`,children:(0,U.jsx)(Nc,{mode:r,groupSize:Ze,rawToggle:n.kind!==`unknown`,characterKeys:j,strength:pt,prefer:c,setStrength:l,decide:e=>u(xt(),e),openEditor:Ct,undo:f,redo:p,next:m,prev:h,toggleRaw:()=>w(e=>!e),toggleWhitespace:()=>E(e=>!e),toggleReadingStep:()=>O(e=>!e),filter:s,cycleFilter:Nt,toggleMetadata:()=>Se(e=>!e),openLabels:()=>we(`labels`),openNotes:()=>we(`notes`),openGoTo:()=>Ee(!0),openSearch:()=>Oe(!0)})}),(0,U.jsx)(`div`,{className:`mt-4 text-center text-chrome text-neutral-11`,"data-numeric":!0,children:r===`compare`?`Prompt ${i+1} of ${t.index.groupCount}`:`Record ${i+1} of ${t.index.lineCount}`}),xe&&(0,U.jsx)(`div`,{className:`max-w-page mx-auto`,children:(0,U.jsx)(Wl,{record:!Xe&&a===1?gt:B,format:n,candidateRef:xt()})})]}),(0,U.jsx)(Cu,{open:ke,onOpenChange:Ae}),(0,U.jsx)(Tu,{open:Ce!==null,onOpenChange:e=>!e&&we(null),candidateRef:xt(),subject:et(xt()),focusField:Ce??`labels`}),(0,U.jsx)(Eu,{open:Te,onOpenChange:Ee,mode:r,total:r===`compare`?t.index.groupCount:t.index.lineCount}),(0,U.jsx)(jc,{open:k,onOpenChange:A,characterKeys:j,onCharacterKeysChange:M}),(0,U.jsx)(ll,{open:N,onOpenChange:ne,onDraftChange:()=>ie(!0),currentFormat:n,sampleRecords:Lt,onApply:Ft},Le)]}),!1)}function ud(){let[e,t]=(0,_.useState)(``);return(0,U.jsxs)(U.Fragment,{children:[(0,U.jsx)(`div`,{role:`status`,className:`sr-only`,"data-resume-status":!0,children:e}),(0,U.jsx)(ld,{onAnnounce:t})]})}(0,v.createRoot)(document.getElementById(`root`)).render((0,U.jsx)(_.StrictMode,{children:(0,U.jsx)(ud,{})}));