evals-lab 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ import{$ as e,$t as t,A as n,An as r,At as i,B as a,Bt as o,C as s,Cn as c,D as l,Dn as u,Dt as d,E as f,En as p,Et as m,F as h,Fn as g,Ft as _,G as v,Gt as y,H as b,Ht as x,I as S,It as C,J as w,Jt as T,K as E,Kt as D,L as O,Lt as k,M as A,Mn as j,Mt as M,N,Nn as P,Nt as F,O as I,On as L,P as R,Pn as z,Pt as B,Q as ee,Qt as te,R as V,Rt as H,S as U,St as W,T as G,Tn as ne,Tt as re,U as ie,Ut as ae,V as oe,Vt as se,W as ce,Wt as le,X as ue,Xt as de,Y as fe,Yt as pe,Z as K,Zt as me,_ as he,_n as ge,_t as q,a as _e,an as ve,at as ye,b as be,bn as xe,bt as Se,c as Ce,cn as we,ct as Te,d as Ee,dn as De,dt as Oe,en as ke,et as Ae,f as je,fn as Me,ft as Ne,g as Pe,gn as J,gt as Fe,h as Ie,hn as Y,ht as Le,i as Re,in as ze,it as Be,j as Ve,jn as He,jt as Ue,k as We,kn as Ge,kt as X,ln as Ke,lt as qe,m as Je,mn as Ye,mt as Xe,n as Ze,nn as Qe,nt as $e,o as et,on as tt,ot as nt,p as rt,pn as it,pt as at,q as ot,qt as st,r as ct,rn as lt,rt as ut,s as dt,sn as ft,st as pt,t as mt,tn as ht,tt as gt,u as _t,un as vt,ut as yt,v as bt,vn as xt,vt as St,w as Ct,wn as wt,wt as Tt,x as Et,xn as Dt,xt as Ot,y as kt,yn as At,yt as jt,z as Mt,zt as Nt}from"./tokens-DzZqM5IZ.js";var Pt=z(),Z=g(),Ft=(0,Z.createContext)(null);function Q(){let e=(0,Z.useContext)(Ft);if(!e)throw Error(`useApi outside an ApiContext: main.tsx mounts one at the root`);return e}var It=`modulepreload`,Lt=function(e){return`/`+e},Rt={},zt=function(e,t,n){let r=Promise.resolve();if(t&&t.length>0){let e=document.getElementsByTagName(`link`),i=document.querySelector(`meta[property=csp-nonce]`),a=i?.nonce||i?.getAttribute(`nonce`);function o(e){return Promise.all(e.map(e=>Promise.resolve(e).then(e=>({status:`fulfilled`,value:e}),e=>({status:`rejected`,reason:e}))))}function s(e){return import.meta.resolve?import.meta.resolve(e):new URL(e,import.meta.url).href}r=o(t.map(t=>{if(t=Lt(t,n),t=s(t),t in Rt)return;Rt[t]=!0;let r=t.endsWith(`.css`);for(let n=e.length-1;n>=0;n--){let i=e[n];if(i.href===t&&(!r||i.rel===`stylesheet`))return}let i=document.createElement(`link`);if(i.rel=r?`stylesheet`:It,r||(i.as=`script`),i.crossOrigin=``,i.href=t,a&&i.setAttribute(`nonce`,a),document.head.appendChild(i),r)return new Promise((e,n)=>{i.addEventListener(`load`,e),i.addEventListener(`error`,()=>n(Error(`Unable to preload CSS for ${t}`)))})}).filter(e=>e!==void 0))}function i(e){let t=new Event(`vite:preloadError`,{cancelable:!0});if(t.payload=e,window.dispatchEvent(t),!t.defaultPrevented)throw e}return r.then(t=>{for(let e of t||[])e.status===`rejected`&&i(e.reason);return e().catch(i)})},Bt={},Vt=new Set,Ht=e=>`/plugins/${encodeURIComponent(e.id)}/${encodeURIComponent(e.version)}/${e.entry.split(`/`).map(encodeURIComponent).join(`/`)}`;async function Ut(e){if(!Vt.has(e.id))try{let t=await zt(()=>import(Ht(e)),[]);if(typeof t.default!=`function`)throw Error(`its entry exports no register function`);await t.default(J().pluginHost(e.id)),Vt.add(e.id),delete Bt[e.id]}catch(t){Bt[e.id]=t instanceof Error?t.message:String(t)}}async function Wt(e){let t=[];try{t=(await e.plugins()).plugins}catch{return}for(let e of t)await Ut(e)}var Gt=class extends Error{status;body;constructor(e,t,n=null){super(t),this.name=`ApiError`,this.status=e,this.body=n}},Kt=(e,t)=>`${e}:${t}`,qt=new EventTarget,Jt=e=>/^\/api\/(sources|queue)(\/|\?|$)/.test(e);function Yt(e=(...e)=>fetch(...e),t=``){let n=e=>t+e;async function r(e){let t=await e.text(),n=t;try{n=t?JSON.parse(t):null}catch{}if(!e.ok){let t=n&&typeof n==`object`&&`error`in n?n.error:null,r=typeof t==`string`?t:t&&typeof t==`object`&&typeof t.message==`string`?`${t.type?`${t.type}: `:``}${t.message}`:t==null?``:JSON.stringify(t);throw new Gt(e.status,r||`HTTP ${e.status}`,n)}return n}async function i(t,i){let a=await r(await e(n(t),i?.body===void 0?{method:i?.method??`GET`}:{method:i.method??`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(i.body),...i.keepalive?{keepalive:!0}:{}}));return(i?.method??(i?.body===void 0?`GET`:`POST`))!==`GET`&&Jt(t)&&qt.dispatchEvent(new Event(`write`)),a}let a=e=>({...e,snapshot:J().upgradePipeline(e.snapshot),results:J().upgradeResults(e.results)}),o=e=>{let t=new URLSearchParams;return e.limit&&t.set(`limit`,String(e.limit)),e.before&&t.set(`before`,e.before),e.beforeId&&t.set(`beforeId`,e.beforeId),e.full&&t.set(`full`,`1`),`/api/queue${t.size?`?${t}`:``}`},s=new Map,c=async e=>({...e,runs:await Promise.all(e.runs.map(e=>f(a(e))))}),l=async e=>{let t=await e;return{...t,run:a(t.run)}},u=new Map,d=e=>J().evalsOf(e.snapshot).some(e=>J().isWholeRun(J().EVAL_TYPES[e.type],e)),f=e=>{if(!e.brief||!d(e))return Promise.resolve(e);let t=`${e.status} ${e.progress?.n??0} ${e.finishedAt??``}`,n=u.get(e.id);if(n?.at===t)return n.row;let r=i(`/api/queue/${encodeURIComponent(e.id)}`).then(a,()=>(u.delete(e.id),e));return u.set(e.id,{at:t,row:r}),r};return{config:()=>i(`/api/config`),google:()=>i(`/api/google`),setGoogle:e=>i(`/api/google`,{method:`PUT`,body:e}),setMicrosoft:(e,t)=>i(`/api/microsoft`,{method:`PUT`,body:{clientId:e,tenant:t}}),state:()=>i(`/api/state`),writeState:(e,t=!1)=>i(`/api/state`,{body:{docs:e},keepalive:t}),queue:(e={})=>{let t=o(e),n=s.get(t);return s.delete(t),n??i(t).then(c)},askQueue:(e,t)=>{let n=o(e),r=i(n),a=t.then(()=>r).then(c);a.catch(()=>{}),s.set(n,a)},run:e=>i(`/api/queue/${encodeURIComponent(e)}`).then(a),setComment:(e,t)=>i(`/api/queue/${encodeURIComponent(e)}`,{method:`PATCH`,body:{comment:t}}).then(a),submit:e=>l(i(`/api/queue`,{body:e})),cancel:e=>l(i(`/api/queue/${encodeURIComponent(e)}/cancel`,{body:{}})),resume:e=>l(i(`/api/queue/${encodeURIComponent(e)}/resume`,{body:{}})),rerunRun:e=>l(i(`/api/queue/${encodeURIComponent(e)}/rerun`,{body:{}})),rerun:(e,t)=>l(i(`/api/queue/${encodeURIComponent(e)}/items/${t}`,{body:{}})),rescore:(e,t)=>l(i(`/api/queue/${encodeURIComponent(e)}/rescore/${t}`,{body:{}})),clear:()=>i(`/api/queue`,{method:`DELETE`}),sources:()=>i(`/api/sources`),source:e=>i(`/api/sources/${encodeURIComponent(e)}`),datasets:()=>i(`/api/datasets`),dataset:e=>i(`/api/datasets/${encodeURIComponent(e)}`),archivedRules:()=>i(`/api/datasets/archived-rules`),createDataset:e=>i(`/api/datasets`,{body:e}),renameDataset:(e,t)=>i(`/api/datasets/${encodeURIComponent(e)}`,{method:`PATCH`,body:{name:t}}),saveDataset:(e,t,n)=>i(`/api/datasets/${encodeURIComponent(e)}`,{method:`PUT`,body:{version:t,body:n}}),deleteDataset:e=>i(`/api/datasets/${encodeURIComponent(e)}`,{method:`DELETE`}),restoreDataset:e=>i(`/api/datasets/trash/${encodeURIComponent(e)}/restore`,{body:{}}),exportDatasetUrl:e=>n(`/api/datasets/${encodeURIComponent(e)}/export`),exportAllDatasetsUrl:()=>n(`/api/datasets/export`),importDatasets:e=>i(`/api/datasets/import`,{body:e}),runDataset:e=>i(`/api/queue/${encodeURIComponent(e)}/dataset`),filterSets:()=>i(`/api/filter-sets`),filterSet:e=>i(`/api/filter-sets/${encodeURIComponent(e)}`),createFilterSet:e=>i(`/api/filter-sets`,{body:e}),renameFilterSet:(e,t)=>i(`/api/filter-sets/${encodeURIComponent(e)}`,{method:`PATCH`,body:{name:t}}),saveFilterSet:(e,t,n)=>i(`/api/filter-sets/${encodeURIComponent(e)}`,{method:`PUT`,body:{version:t,body:n}}),deleteFilterSet:e=>i(`/api/filter-sets/${encodeURIComponent(e)}`,{method:`DELETE`}),restoreFilterSet:e=>i(`/api/filter-sets/trash/${encodeURIComponent(e)}/restore`,{body:{}}),prompts:()=>i(`/api/prompts`),prompt:e=>i(`/api/prompts/${encodeURIComponent(e)}`),createPrompt:e=>i(`/api/prompts`,{body:e}),editPrompt:(e,t,n)=>i(`/api/prompts/${encodeURIComponent(e)}`,{method:`PUT`,body:{base:t,...n}}),setDefaultPrompt:e=>i(`/api/prompts/${encodeURIComponent(e)}/default`,{body:{}}),restorePromptVersion:(e,t)=>i(`/api/prompts/${encodeURIComponent(e)}/versions/${t}/restore`,{body:{}}),deletePrompt:e=>i(`/api/prompts/${encodeURIComponent(e)}`,{method:`DELETE`}),restorePrompt:e=>i(`/api/prompts/trash/${encodeURIComponent(e)}/restore`,{body:{}}),createSource:(e,t,n)=>i(`/api/sources`,{body:{name:e,type:t,...n?{config:n}:{}}}),definition:e=>i(`/api/sources/${encodeURIComponent(e)}/definition`),saveDefinition:(e,t,n)=>i(`/api/sources/${encodeURIComponent(e)}/definition`,{method:`PUT`,body:{version:t,body:n}}),renameSource:(e,t)=>i(`/api/sources/${encodeURIComponent(e)}`,{method:`PATCH`,body:{name:t}}),removeSource:e=>i(`/api/sources/${encodeURIComponent(e)}`,{method:`DELETE`}),removeFiles:(e,t)=>i(`/api/sources/${encodeURIComponent(e)}/remove`,{body:{names:t}}),restoreTrash:e=>i(`/api/sources/trash/${encodeURIComponent(e)}/restore`,{body:{}}),copyFiles:(e,t,n,r=!1)=>i(`/api/sources/${encodeURIComponent(e)}/copy`,{body:{from:t,names:n,move:r}}),upload:async(t,i,a)=>r(await e(n(`/api/sources/${encodeURIComponent(t)}`),{method:`POST`,headers:{"X-File-Name":i},body:a})),zip:async(t,i)=>{let a=await e(n(`/api/sources/${encodeURIComponent(t)}/zip`),{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({names:i})});return a.ok||await r(a),a.blob()},bundle:async t=>{let i=await e(n(`/api/bundle`),{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t)});return i.ok||await r(i),i.blob()},file:async(t,i)=>{let a=await e(n(`/api/sources/${encodeURIComponent(t)}/files/${encodeURIComponent(i)}`));return a.ok||await r(a),a.blob()},samples:()=>i(`/api/samples`),presets:()=>i(`/api/presets`),plugins:()=>i(`/api/plugins`),installPlugin:async t=>r(await e(n(`/api/plugins`),{method:`POST`,headers:{"Content-Type":`application/zip`},body:t})),removePlugin:e=>i(`/api/plugins/${encodeURIComponent(e)}`,{method:`DELETE`}),restorePlugin:e=>i(`/api/plugins/trash/${encodeURIComponent(e)}/restore`,{body:{}}),connections:()=>i(`/api/connections`),signIn:e=>i(`/api/connections/${encodeURIComponent(e)}/start`,{body:{}}),signOut:e=>i(`/api/connections/${encodeURIComponent(e)}`,{method:`DELETE`}),connectionShares:()=>i(`/api/connections/shares`),setConnectionShares:(e,t,n)=>i(`/api/connections/shares`,{method:`PUT`,body:{kind:e,id:t,workspaces:n}}),shareConnection:(e,t,n=!1)=>i(`/api/connections/share`,{body:{kind:e,id:t,...n?{undo:!0}:{}}}),packs:()=>i(`/api/packs`),installPack:async t=>r(await e(n(`/api/packs`),{method:`POST`,headers:{"Content-Type":`application/zip`},body:t})),removePack:e=>i(`/api/packs/${encodeURIComponent(e)}`,{method:`DELETE`}),restorePack:e=>i(`/api/packs/trash/${encodeURIComponent(e)}/restore`,{body:{}}),workspaces:()=>i(`/api/workspaces`),createWorkspace:e=>i(`/api/workspaces`,{body:e}),renameWorkspace:(e,t)=>i(`/api/workspaces/${encodeURIComponent(e)}`,{method:`PATCH`,body:{name:t}}),regenerateWorkspace:e=>i(`/api/workspaces/${encodeURIComponent(e)}/regenerate`,{body:{}}),archiveWorkspace:e=>i(`/api/workspaces/${encodeURIComponent(e)}/archive`,{body:{}}),unarchiveWorkspace:e=>i(`/api/workspaces/${encodeURIComponent(e)}/unarchive`,{body:{}}),setDefaultWorkspace:e=>i(`/api/workspaces/${encodeURIComponent(e)}/default`,{body:{}}),deleteWorkspace:e=>i(`/api/workspaces/${encodeURIComponent(e)}`,{method:`DELETE`}),restoreWorkspace:e=>i(`/api/workspaces/trash/${encodeURIComponent(e)}/restore`,{body:{}}),models:e=>i(`/api/models`,{body:e}),modelsAsk:e=>i(`/api/models/ask`,{body:e})}}var Xt=Y(`archive-restore`,[[`rect`,{width:`20`,height:`5`,x:`2`,y:`3`,rx:`1`,key:`1wp1u1`}],[`path`,{d:`M4 8v11a2 2 0 0 0 2 2h2`,key:`tvwodi`}],[`path`,{d:`M20 8v11a2 2 0 0 1-2 2h-2`,key:`1gkqxj`}],[`path`,{d:`m9 15 3-3 3 3`,key:`1pd0qc`}],[`path`,{d:`M12 12v9`,key:`192myk`}]]),Zt=Y(`archive`,[[`rect`,{width:`20`,height:`5`,x:`2`,y:`3`,rx:`1`,key:`1wp1u1`}],[`path`,{d:`M4 8v11a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8`,key:`1s80jp`}],[`path`,{d:`M10 12h4`,key:`a56b0p`}]]),Qt=Y(`ban`,[[`path`,{d:`M4.929 4.929 19.07 19.071`,key:`196cmz`}],[`circle`,{cx:`12`,cy:`12`,r:`10`,key:`1mglay`}]]),$t=Y(`boxes`,[[`path`,{d:`M2.97 12.92A2 2 0 0 0 2 14.63v3.24a2 2 0 0 0 .97 1.71l3 1.8a2 2 0 0 0 2.06 0L12 19v-5.5l-5-3-4.03 2.42Z`,key:`lc1i9w`}],[`path`,{d:`m7 16.5-4.74-2.85`,key:`1o9zyk`}],[`path`,{d:`m7 16.5 5-3`,key:`va8pkn`}],[`path`,{d:`M7 16.5v5.17`,key:`jnp8gn`}],[`path`,{d:`M12 13.5V19l3.97 2.38a2 2 0 0 0 2.06 0l3-1.8a2 2 0 0 0 .97-1.71v-3.24a2 2 0 0 0-.97-1.71L17 10.5l-5 3Z`,key:`8zsnat`}],[`path`,{d:`m17 16.5-5-3`,key:`8arw3v`}],[`path`,{d:`m17 16.5 4.74-2.85`,key:`8rfmw`}],[`path`,{d:`M17 16.5v5.17`,key:`k6z78m`}],[`path`,{d:`M7.97 4.42A2 2 0 0 0 7 6.13v4.37l5 3 5-3V6.13a2 2 0 0 0-.97-1.71l-3-1.8a2 2 0 0 0-2.06 0l-3 1.8Z`,key:`1xygjf`}],[`path`,{d:`M12 8 7.26 5.15`,key:`1vbdud`}],[`path`,{d:`m12 8 4.74-2.85`,key:`3rx089`}],[`path`,{d:`M12 13.5V8`,key:`1io7kd`}]]),en=Y(`check-check`,[[`path`,{d:`M18 6 7 17l-5-5`,key:`116fxf`}],[`path`,{d:`m22 10-7.5 7.5L13 16`,key:`ke71qq`}]]),tn=Y(`circle-check`,[[`circle`,{cx:`12`,cy:`12`,r:`10`,key:`1mglay`}],[`path`,{d:`m9 12 2 2 4-4`,key:`dzmm74`}]]),nn=Y(`columns-3`,[[`rect`,{width:`18`,height:`18`,x:`3`,y:`3`,rx:`2`,key:`afitv7`}],[`path`,{d:`M9 3v18`,key:`fh3hqa`}],[`path`,{d:`M15 3v18`,key:`14nvp0`}]]),rn=Y(`external-link`,[[`path`,{d:`M15 3h6v6`,key:`1q9fwt`}],[`path`,{d:`M10 14 21 3`,key:`gplh6r`}],[`path`,{d:`M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6`,key:`a6xqqp`}]]),an=Y(`file-code-corner`,[[`path`,{d:`M4 12.15V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.706.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2h-3.35`,key:`1wthlu`}],[`path`,{d:`M14 2v5a1 1 0 0 0 1 1h5`,key:`wfsgrz`}],[`path`,{d:`m5 16-3 3 3 3`,key:`331omg`}],[`path`,{d:`m9 22 3-3-3-3`,key:`lsp7cz`}]]),on=Y(`file-up`,[[`path`,{d:`M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z`,key:`1oefj6`}],[`path`,{d:`M14 2v5a1 1 0 0 0 1 1h5`,key:`wfsgrz`}],[`path`,{d:`M12 12v6`,key:`3ahymv`}],[`path`,{d:`m15 15-3-3-3 3`,key:`15xj92`}]]),sn=Y(`flag`,[[`path`,{d:`M4 22V4a1 1 0 0 1 .4-.8A6 6 0 0 1 8 2c3 0 5 2 7.333 2q2 0 3.067-.8A1 1 0 0 1 20 4v10a1 1 0 0 1-.4.8A6 6 0 0 1 16 16c-3 0-5-2-8-2a6 6 0 0 0-4 1.528`,key:`1jaruq`}]]),cn=Y(`folder-down`,[[`path`,{d:`M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z`,key:`1kt360`}],[`path`,{d:`M12 10v6`,key:`1bos4e`}],[`path`,{d:`m15 13-3 3-3-3`,key:`6j2sf0`}]]),ln=Y(`folder-input`,[[`path`,{d:`M2 9V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H20a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2v-1`,key:`fm4g5t`}],[`path`,{d:`M2 13h10`,key:`pgb2dq`}],[`path`,{d:`m9 16 3-3-3-3`,key:`6m91ic`}]]),un=Y(`folder-plus`,[[`path`,{d:`M12 10v6`,key:`1bos4e`}],[`path`,{d:`M9 13h6`,key:`1uhe8q`}],[`path`,{d:`M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z`,key:`1kt360`}]]),dn=Y(`folder-up`,[[`path`,{d:`M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z`,key:`1kt360`}],[`path`,{d:`M12 10v6`,key:`1bos4e`}],[`path`,{d:`m9 13 3-3 3 3`,key:`1pxg3c`}]]),fn=Y(`folder`,[[`path`,{d:`M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z`,key:`1kt360`}]]),pn=Y(`funnel`,[[`path`,{d:`M10 20a1 1 0 0 0 .553.895l2 1A1 1 0 0 0 14 21v-7a2 2 0 0 1 .517-1.341L21.74 4.67A1 1 0 0 0 21 3H3a1 1 0 0 0-.742 1.67l7.225 7.989A2 2 0 0 1 10 14z`,key:`sc7q7i`}]]),mn=Y(`gavel`,[[`path`,{d:`m14 13-8.381 8.38a1 1 0 0 1-3.001-3l8.384-8.381`,key:`pgg06f`}],[`path`,{d:`m16 16 6-6`,key:`vzrcl6`}],[`path`,{d:`m21.5 10.5-8-8`,key:`a17d9x`}],[`path`,{d:`m8 8 6-6`,key:`18bi4p`}],[`path`,{d:`m8.5 7.5 8 8`,key:`1oyaui`}]]),hn=Y(`log-in`,[[`path`,{d:`m10 17 5-5-5-5`,key:`1bsop3`}],[`path`,{d:`M15 12H3`,key:`6jk70r`}],[`path`,{d:`M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4`,key:`u53s6r`}]]),gn=Y(`log-out`,[[`path`,{d:`m16 17 5-5-5-5`,key:`1bji2h`}],[`path`,{d:`M21 12H9`,key:`dn1m92`}],[`path`,{d:`M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4`,key:`1uf3rs`}]]),_n=Y(`minus`,[[`path`,{d:`M5 12h14`,key:`1ays0h`}]]),vn=Y(`pin-off`,[[`path`,{d:`M12 17v5`,key:`bb1du9`}],[`path`,{d:`M15 9.34V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H7.89`,key:`znwnzq`}],[`path`,{d:`m2 2 20 20`,key:`1ooewy`}],[`path`,{d:`M9 9v1.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h11`,key:`c9qhm2`}]]),yn=Y(`pin`,[[`path`,{d:`M12 17v5`,key:`bb1du9`}],[`path`,{d:`M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z`,key:`1nkz8b`}]]),bn=Y(`refresh-cw`,[[`path`,{d:`M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8`,key:`v9h5vc`}],[`path`,{d:`M21 3v5h-5`,key:`1q7to0`}],[`path`,{d:`M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16`,key:`3uifl3`}],[`path`,{d:`M8 16H3v5`,key:`1cv678`}]]),xn=Y(`replace`,[[`path`,{d:`M14 4a1 1 0 0 1 1-1`,key:`dhj8ez`}],[`path`,{d:`M15 10a1 1 0 0 1-1-1`,key:`1mnyi5`}],[`path`,{d:`M21 4a1 1 0 0 0-1-1`,key:`sfs9ap`}],[`path`,{d:`M21 9a1 1 0 0 1-1 1`,key:`mp6qeo`}],[`path`,{d:`m3 7 3 3 3-3`,key:`x25e72`}],[`path`,{d:`M6 10V5a2 2 0 0 1 2-2h2`,key:`15xut4`}],[`rect`,{x:`3`,y:`14`,width:`7`,height:`7`,rx:`1`,key:`1bkyp8`}]]),Sn=Y(`rotate-cw`,[[`path`,{d:`M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8`,key:`1p45f6`}],[`path`,{d:`M21 3v5h-5`,key:`1q7to0`}]]),Cn=Y(`shapes`,[[`path`,{d:`M8.3 10a.7.7 0 0 1-.626-1.079L11.4 3a.7.7 0 0 1 1.198-.043L16.3 8.9a.7.7 0 0 1-.572 1.1Z`,key:`1bo67w`}],[`rect`,{x:`3`,y:`14`,width:`7`,height:`7`,rx:`1`,key:`1bkyp8`}],[`circle`,{cx:`17.5`,cy:`17.5`,r:`3.5`,key:`w3z12y`}]]),wn=Y(`star`,[[`path`,{d:`M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z`,key:`r04s7s`}]]),Tn=Y(`unlink`,[[`path`,{d:`m18.84 12.25 1.72-1.71h-.02a5.004 5.004 0 0 0-.12-7.07 5.006 5.006 0 0 0-6.95 0l-1.72 1.71`,key:`yqzxt4`}],[`path`,{d:`m5.17 11.75-1.71 1.71a5.004 5.004 0 0 0 .12 7.07 5.006 5.006 0 0 0 6.95 0l1.71-1.71`,key:`4qinb0`}],[`line`,{x1:`8`,x2:`8`,y1:`2`,y2:`5`,key:`1041cp`}],[`line`,{x1:`2`,x2:`5`,y1:`8`,y2:`8`,key:`14m1p5`}],[`line`,{x1:`16`,x2:`16`,y1:`19`,y2:`22`,key:`rzdirn`}],[`line`,{x1:`19`,x2:`22`,y1:`16`,y2:`16`,key:`ox905f`}]]),En=(0,Z.createContext)(null);function Dn(){let e=(0,Z.useContext)(En);if(!e)throw Error(`useStore outside a StoreContext: the app mounts one at its root`);return e}function On(e){let t=Dn();return[(0,Z.useSyncExternalStore)(t.subscribe,()=>t.get(e)),(0,Z.useCallback)(n=>t.set(e,n),[t,e])]}function kn(){let e=Dn();return{sync:(0,Z.useSyncExternalStore)(e.subscribe,e.sync),resolve:e.resolve}}var An=e=>e,jn=()=>({version:8,source:null,scoring:{mode:`all`,threshold:null},grader:null,every:[],run:[],cases:[]}),Mn=e=>e?.state===`conflict`?`Not saved: changed in another browser.`:e?.state===`offline`?`Not saved: ${e.message}`:null,Nn=(e,t)=>JSON.stringify(e)===JSON.stringify(t),Pn=e=>e instanceof Error?e.message:String(e),Fn=({body:e,...t})=>t;function In({api:e,pause:t=800,idle:n}){let r=An(e),i={list:null,error:null},a=new Map,o=new Set,s=new Map,c=new Map,l=new Map,u=new Set,d=null,f=()=>{d=null,u.forEach(e=>e())},p=(e,t)=>{let n=a.get(e)??{id:e,doc:null,sync:{state:`loading`}};a.set(e,{...n,...t,id:e}),f()},m=e=>{i={list:e,error:null},f()},h=e=>{if(!i.list)return;let t=i.list.findIndex(t=>t.id===e.id);m(t<0?[...i.list,e]:i.list.map((n,r)=>r===t?e:n))},g=e=>{for(let t of[s,c]){let n=t.get(e);n&&clearTimeout(n),t.delete(e)}o.delete(e),a.delete(e),i.list?m(i.list.filter(t=>t.id!==e)):f()},_=e=>{p(e.id,{doc:e,sync:{state:`saved`}}),h(Fn(e))};async function v(e){let t=a.get(e);if(!t?.doc||!o.has(e)||t.sync.state===`conflict`)return;let{body:n,version:i}=t.doc;p(e,{sync:{state:`saving`}});try{let t=await r.saveDataset(e,i,n),s=a.get(e);if(!s?.doc)return;s.doc.body===n&&o.delete(e);let c={...s.doc,version:t.version,cases:n.cases.length};if(p(e,{doc:c,sync:{state:`saved`}}),h(Fn(c)),o.has(e))return v(e)}catch(t){let n=a.get(e);if(!n?.doc)return;let r=t instanceof Gt&&t.status===409?t.body?.current??null:null;if(r){if(Nn(r.body,n.doc.body)){o.delete(e),p(e,{doc:{...n.doc,version:r.version},sync:{state:`saved`}});return}p(e,{sync:{state:`conflict`,theirs:r}});return}p(e,{sync:{state:`offline`,message:Pn(t)}})}}function y(e){let t=s.get(e);t&&(clearTimeout(t),s.delete(e));let n=l.get(e);return n||(n=v(e).finally(()=>l.delete(e)),l.set(e,n)),n}return{list:()=>i,entry:e=>a.get(e),held:()=>d??=[...a.values()],async refresh(){i.error&&(i={list:i.list,error:null},f());try{let e=(await r.datasets()).datasets;m(e);for(let t of e){let e=a.get(t.id);e?.doc&&!o.has(t.id)&&t.version>e.doc.version&&r.dataset(t.id).then(e=>{a.has(t.id)&&!o.has(t.id)&&_(e)},()=>{})}}catch(e){i={list:i.list,error:Pn(e)},f()}},load(e){a.has(e)&&a.get(e).sync.state!==`missing`||(p(e,{sync:{state:`loading`}}),r.dataset(e).then(t=>{a.has(e)&&p(e,{doc:t,sync:{state:`saved`}})},t=>{a.has(e)&&p(e,{sync:{state:`missing`,message:Pn(t)}})}))},edit(e,r){let i=a.get(e);if(!i?.doc)return;let l=r(i.doc.body);if(l===i.doc.body)return;p(e,{doc:{...i.doc,body:l,cases:l.cases.length}}),o.add(e);let u=s.get(e);if(u&&clearTimeout(u),s.set(e,setTimeout(()=>{s.delete(e),y(e)},t)),n){let t=c.get(e);t&&clearTimeout(t),c.set(e,setTimeout(()=>{c.delete(e);let t=a.get(e)?.doc;t&&n.cut(e,t.body)},n.ms))}},async flush(e){await Promise.all((e?[e]:[...o]).map(y))},async resolve(e,t){let n=a.get(e);if(!n?.doc||n.sync.state!==`conflict`)return;let r=n.sync.theirs;if(t===`theirs`){o.delete(e),_(r);return}p(e,{doc:{...n.doc,version:r.version},sync:{state:`saved`}}),o.add(e),await y(e)},async create(e,t){let n=await r.createDataset(t?{name:e,body:t}:{name:e});return _(n),n},async rename(e,t){let n=await r.renameDataset(e,t),i=a.get(e)?.doc;i&&p(e,{doc:{...i,name:n.name,version:n.version,updated:n.updated}}),h({...Fn(n),cases:i?.body.cases.length??n.cases})},async remove(e){let{trash:t}=await r.deleteDataset(e);return g(e),t},async restore(e){let t=await r.restoreDataset(e);return _(t),t},async importFile(e){let{created:t}=await r.importDatasets(e);for(let e of t)h(e);return t},exportUrl:e=>r.exportDatasetUrl(e),exportAllUrl:()=>r.exportAllDatasetsUrl(),subscribe(e){return u.add(e),()=>{u.delete(e)}}}}var Ln=3e4,Rn=(e,t)=>e.get(`promptlab.versions`)[t]??{};function zn(e,t){return(e.get(`promptlab.versions`).dataset?.[t]??[]).map(e=>({...e,doc:J().upgradeDatasetBody(e?.doc)})).filter(e=>Bn(e.doc))}var Bn=e=>!!e&&typeof e==`object`&&Array.isArray(e.cases);function Vn(e,t,n,r,i){let a=e.get(`promptlab.versions`),o=Rn(e,t),s=(o[n]??[]).slice(),c=structuredClone(r);s[0]&&Un(s[0].doc,c)||(s.unshift({at:new Date().toISOString(),doc:c,reason:i}),s.length>10&&(s.length=10),e.set(`promptlab.versions`,{...a,[t]:{...o,[n]:s}}))}function Hn(e,t,n,r){let i=e.get(`promptlab.versions`),a=(i.dataset?.[t]??[]).slice(),o=structuredClone(n);a[0]&&Un(a[0].doc,o)||(a.unshift({at:new Date().toISOString(),doc:o,reason:r}),a.length>10&&(a.length=10),e.set(`promptlab.versions`,{...i,dataset:{...i.dataset??{},[t]:a}}))}var Un=(e,t)=>JSON.stringify(e)===JSON.stringify(t),$=C(),Wn=(0,Z.createContext)(null);function Gn({children:e,pause:t}){let n=Q(),r=Dn(),i=(0,Z.useMemo)(()=>In({api:n,...t==null?{}:{pause:t},idle:{ms:Ln,cut:(e,t)=>Hn(r,e,t,`idle`)}}),[n,r,t]);return(0,Z.useEffect)(()=>{let e=()=>{i.flush()};return addEventListener(`pagehide`,e),()=>removeEventListener(`pagehide`,e)},[i]),(0,$.jsx)(Wn.Provider,{value:i,children:e})}function Kn(){let e=(0,Z.useContext)(Wn);if(!e)throw Error(`useDatasets outside a DatasetsProvider: the app mounts one at its root`);return e}function qn(){let e=Kn(),t=(0,Z.useSyncExternalStore)(e.subscribe,e.list);return(0,Z.useEffect)(()=>{e.list().list===null&&e.refresh()},[e]),t}function Jn(e){let t=Kn(),n=(0,Z.useSyncExternalStore)(t.subscribe,()=>e?t.entry(e):void 0);return(0,Z.useEffect)(()=>{e&&t.load(e)},[t,e]),n}var Yn=e=>e.name.trim()||e.text.trim().split(`
2
+ `)[0].trim()||`Untitled prompt`,Xn=e=>e?.state===`conflict`?`Not saved: changed in another browser.`:e?.state===`offline`?`Not saved: ${e.message}`:null,Zn=e=>e instanceof Error?e.message:String(e),Qn=({versions:e,uses:t,...n})=>n;function $n({api:e,pause:t=800}){let n={list:null,error:null},r=new Map,i=new Set,a=new Map,o=new Map,s=new Set,c=()=>s.forEach(e=>e()),l=(e,t)=>{let n=r.get(e)??{id:e,doc:null,sync:{state:`loading`}};r.set(e,{...n,...t,id:e}),c()},u=e=>{n={list:e,error:null},c()},d=e=>{if(!n.list)return;let t=n.list.findIndex(t=>t.id===e.id),r=e.default?n.list.map(t=>t.default&&t.id!==e.id?{...t,default:!1}:t):n.list;u(t<0?[e,...r]:r.map((n,r)=>r===t?e:n))},f=e=>{l(e.id,{doc:e,sync:{state:`saved`}}),d(Qn(e))};async function p(t){let n=r.get(t);if(!n?.doc||!i.has(t)||n.sync.state===`conflict`)return;let{name:a,text:o,version:s}=n.doc;l(t,{sync:{state:`saving`}});try{let n=await e.editPrompt(t,s,{name:a,text:o}),c=r.get(t);if(!c?.doc)return;let u=c.doc.name!==a||c.doc.text!==o;u||i.delete(t);let f=u?{...n,name:c.doc.name,text:c.doc.text}:n;if(l(t,{doc:f,sync:{state:u?`saving`:`saved`}}),d(Qn(f)),u)return p(t)}catch(e){let n=r.get(t);if(!n?.doc)return;let a=e instanceof Gt&&e.status===409?e.body?.current??null:null;if(a){if(a.text===n.doc.text&&a.name===n.doc.name){i.delete(t),f(a);return}l(t,{sync:{state:`conflict`,theirs:a}});return}l(t,{sync:{state:`offline`,message:Zn(e)}})}}function m(e){let t=a.get(e);t&&(clearTimeout(t),a.delete(e));let n=o.get(e);return n||(n=p(e).finally(()=>o.delete(e)),o.set(e,n)),n}return{list:()=>n,entry:e=>r.get(e),async refresh(){n.error&&(n={list:n.list,error:null},c());try{u((await e.prompts()).prompts)}catch(e){n={list:n.list,error:Zn(e)},c()}},load(t){i.has(t)||(r.has(t)||l(t,{sync:{state:`loading`}}),e.prompt(t).then(e=>{r.has(t)&&!i.has(t)&&f(e)},e=>{r.has(t)&&l(t,{sync:{state:`missing`,message:Zn(e)}})}))},edit(e,n){let o=r.get(e);if(!o?.doc)return;let s={...o.doc,...n};if(s.name===o.doc.name&&s.text===o.doc.text)return;l(e,{doc:s}),d(Qn(s)),i.add(e);let c=a.get(e);c&&clearTimeout(c),a.set(e,setTimeout(()=>{a.delete(e),m(e)},t))},async flush(e){await Promise.all((e?[e]:[...i]).map(m))},async resolve(e,t){let n=r.get(e);if(!n?.doc||n.sync.state!==`conflict`)return;let a=n.sync.theirs;if(t===`theirs`){i.delete(e),f(a);return}l(e,{doc:{...n.doc,version:a.version},sync:{state:`saved`}}),i.add(e),await m(e)},async create(t={}){let n=await e.createPrompt(t);return f(n),n},async setDefault(t){await m(t),f(await e.setDefaultPrompt(t))},async restoreVersion(t,n){await m(t),f(await e.restorePromptVersion(t,n))},async remove(t){await m(t);let{trash:o}=await e.deletePrompt(t),s=a.get(t);return s&&clearTimeout(s),a.delete(t),i.delete(t),r.delete(t),n.list?u(n.list.filter(e=>e.id!==t)):c(),o},async restore(t){let n=await e.restorePrompt(t);return f(n),n},subscribe(e){return s.add(e),()=>{s.delete(e)}}}}var er=(0,Z.createContext)(null);function tr({children:e,pause:t}){let n=Q(),r=(0,Z.useMemo)(()=>$n({api:n,...t==null?{}:{pause:t}}),[n,t]);return(0,Z.useEffect)(()=>{let e=()=>{r.flush()};return addEventListener(`pagehide`,e),()=>removeEventListener(`pagehide`,e)},[r]),(0,$.jsx)(er.Provider,{value:r,children:e})}function nr(){let e=(0,Z.useContext)(er);if(!e)throw Error(`usePrompts outside a PromptsProvider: the app mounts one at its root`);return e}function rr(){let e=nr(),t=(0,Z.useSyncExternalStore)(e.subscribe,e.list);return(0,Z.useEffect)(()=>{e.refresh()},[e]),t}function ir(e){let t=nr(),n=(0,Z.useSyncExternalStore)(t.subscribe,()=>e?t.entry(e):void 0);return(0,Z.useEffect)(()=>{e&&t.load(e)},[t,e]),n}var ar=e=>(typeof e.id==`string`?e.id.trim():``)||`item:${J().caseItem(e)}`,or=(e,t)=>e.trim().toLowerCase()===t.trim().toLowerCase(),sr=e=>Array.isArray(e.metrics)?e.metrics:[],cr=e=>!e.not&&e.weight!==0,lr=e=>e.type===`contains`&&e.not===!0&&e.weight!==0,ur=e=>J().metricLines(e);function dr(e,t){let n=sr(e);switch(t.type){case`bounds`:{let r=n.findIndex(e=>e.type===`item-count`&&cr(e)),i=r>=0?n[r]:null,a=e=>e==null||e===``?null:Number(e),o=(e,t)=>e.kind===`keep`?t:e.kind===`clear`?null:e.n,s=o(t.min,a(i?.min)),c=o(t.max,a(i?.max));if(s===a(i?.min)&&c===a(i?.max))return e;let l=s==null&&c==null?null:{...i??{type:`item-count`},min:s,max:c};return{...e,metrics:r>=0?n.flatMap((e,t)=>t===r?l?[l]:[]:[e]):[...n,...l?[l]:[]]}}case`addTerm`:{let r=t.term.trim();if(!r)return e;if(t.list===`forbid`)return n.some(e=>lr(e)&&or(String(e.value??``),r))?e:{...e,metrics:[...n,{type:`contains`,value:r,not:!0,ignoreCase:!0}]};let i=n.findIndex(e=>e.type===`contains-all`&&cr(e));return i>=0&&ur(n[i].values).some(e=>or(e,r))?e:{...e,metrics:i>=0?n.map((e,t)=>t===i?{...e,values:[...ur(e.values),r].join(`
3
+ `)}:e):[...n,{type:`contains-all`,values:r,ignoreCase:!0}]}}case`removeTerm`:{let r=t.term.trim();if(!r)return e;let i=!1,a=n.flatMap(e=>{if(lr(e)&&or(String(e.value??``),r))return i=!0,[];if((e.type===`contains-all`||e.type===`contains-any`)&&cr(e)&&ur(e.values).some(e=>or(e,r))){i=!0;let t=ur(e.values).filter(e=>!or(e,r));return t.length?[{...e,values:t.join(`
4
+ `)}]:[]}return[e]});return i?{...e,metrics:a}:e}case`status`:return!!e.todo===t.todo?e:{...e,todo:t.todo}}}function fr(e,t,n){let r=[];return n.type===`delete`?{next:e.filter(e=>{let n=t.has(ar(e));return n&&r.push(ar(e)),!n}),changed:r}:{next:e.map(e=>{if(!t.has(ar(e)))return e;let i=dr(e,n);return i!==e&&r.push(ar(e)),i}),changed:r}}function pr(e,t,n){let r=new Map(e.map(e=>[ar(e),e])),i=new Map(t.map(e=>[ar(e),e])),a=[];for(let e of n){let t=r.get(e),n=i.get(e);if(!t||!n)continue;let o=new Set(J().validateEvals({cases:[t]})),s=`${n.id}: `,c=J().validateEvals({cases:[n]}).filter(e=>!o.has(e)).map(e=>e.startsWith(s)?e.slice(s.length):e);c.length&&a.push({key:e,file:J().caseItem(n),reasons:c})}return{cases:a}}var mr={expect:`Expect`,forbid:`Forbid`},hr=e=>mr[e],gr=e=>`${e} ${e===1?`case`:`cases`}`;function _r(e,t){let n=gr(t);switch(e.type){case`bounds`:{let t=[];return e.min.kind!==`keep`&&t.push({verb:e.min.kind===`set`?`Set`:`Cleared`,what:`Min`}),e.max.kind!==`keep`&&t.push({verb:e.max.kind===`set`?`Set`:`Cleared`,what:`Max`}),t.length===2&&t[0].verb===t[1].verb?`${t[0].verb} Min and Max items on ${n}`:t.length===2?`${t[0].verb} Min items and ${t[1].verb.toLowerCase()} Max items on ${n}`:t[0]?`${t[0].verb} ${t[0].what} items on ${n}`:`Changed ${n}`}case`addTerm`:return`Added ${e.term.trim()} to ${mr[e.list]} on ${n}`;case`removeTerm`:return`Removed ${e.term.trim()} from ${n}`;case`status`:return`Marked ${n} ${e.todo?`To do`:`graded`}`;case`delete`:return`Deleted ${n}`}}var vr={graded:`Graded`,todo:`To do`,missing:`Missing`};function yr(e,t){return t&&!t.includes(J().caseItem(e))?`missing`:e.todo?`todo`:`graded`}var br={file:``,status:`all`,show:`all`};function xr(e,t,n,r=null){let i=t.file.trim().toLowerCase();return e.filter(e=>(!i||J().caseItem(e).toLowerCase().includes(i))&&(t.status===`all`||yr(e,r)===t.status)&&(t.show===`all`||n.has(ar(e))))}var Sr=(0,Z.createContext)(null);function Cr({children:e}){let[t,n]=(0,Z.useState)(null),[r,i]=(0,Z.useState)(new Set),[a,o]=(0,Z.useState)(br),[s,c]=(0,Z.useState)(null),[l,u]=(0,Z.useState)(null),d=Jn(l?.dataset??null),f=(0,Z.useRef)(null),p=(0,Z.useCallback)(e=>{e!==f.current&&(f.current=e,n(e),i(new Set),o(br),c(null))},[]),m=(0,Z.useCallback)(e=>{i(e)},[]),h=(0,Z.useCallback)(e=>{o(t=>({...t,...e}))},[]),g=(0,Z.useCallback)((e,t,r)=>{f.current=e,n(e),i(new Set),o({...br,show:`selected`}),c(null),u({dataset:e,files:t,from:r});let a=`#/library/datasets/${encodeURIComponent(e)}`;location.hash===a?window.dispatchEvent(new HashChangeEvent(`hashchange`)):location.hash=a},[]),_=d?.doc?.body??null,v=d?.sync.state===`missing`;(0,Z.useEffect)(()=>{if(!l||!_&&!v)return;let e=new Set(l.files),t=(_?.cases??[]).filter(t=>e.has(J().caseItem(t))).map(ar);i(new Set(t)),c({from:l.from,wanted:e.size,found:t.length}),u(null)},[l,_,v]);let y=(0,Z.useMemo)(()=>({datasetId:t,pickDataset:p,selected:r,setSelected:m,filters:a,setFilters:h,handedOver:s,showCases:g}),[t,p,r,m,a,h,s,g]);return(0,$.jsx)(Sr.Provider,{value:y,children:e})}function wr(){let e=(0,Z.useContext)(Sr);if(!e)throw Error(`useCases outside an CasesProvider: the app mounts one at its root`);return e}var Tr=(0,Z.createContext)(null);function Er({children:e}){let[t,n]=(0,Z.useState)([]),[r,i]=(0,Z.useState)(null),a=(0,Z.useRef)(t);a.current=t;let o=(0,Z.useRef)(r);o.current=r;let s=(0,Z.useRef)(0),c=(0,Z.useCallback)((e,t)=>{a.current=e,n(e),o.current=t,i(t)},[]),l=(0,Z.useCallback)((e,t={})=>{let n=++s.current,r=a.current,i=r.find(t=>t.id===e),o;if(i)o=r.map(r=>r.id===e?{...r,from:t.from??r.from,last:n}:r);else if(o=[...r,{id:e,at:t.at??``,from:t.from??`runs`,show:`all`,last:n}],o.length>4){let e=o.reduce((e,t)=>e.last<t.last?e:t);o=o.filter(t=>t.id!==e.id)}c(o,e)},[c]),u=(0,Z.useCallback)(e=>{let t=a.current,n=t.findIndex(t=>t.id===e);if(n<0)return;let r=t[n].from,i=t.filter(t=>t.id!==e);if(i.length===0){c(i,null),window.location.hash=`#/${r}`;return}c(i,o.current===e?i[Math.min(n,i.length-1)].id:o.current)},[c]),d=(0,Z.useCallback)((e,t)=>{let n=++s.current;c(a.current.map(r=>r.id===e?{...r,from:t??r.from,last:n}:r),e)},[c]),f=(0,Z.useCallback)((e,t)=>{c(a.current.map(n=>n.id===e?{...n,show:t}:n),o.current)},[c]),p=(0,Z.useCallback)(()=>{c(a.current,null)},[c]),m=(0,Z.useCallback)(()=>{let e=a.current.find(e=>e.id===o.current);window.location.hash=`#/${e?.from??`runs`}`,p()},[p]),h=(0,Z.useMemo)(()=>({tabs:t,focusedId:r,openRun:l,closeRun:u,focusRun:d,setShow:f,goBack:m,hide:p}),[t,r,l,u,d,f,m,p]);return(0,$.jsx)(Tr.Provider,{value:h,children:e})}function Dr(){let e=(0,Z.useContext)(Tr);if(!e)throw Error(`useResults outside a ResultsProvider: the app mounts one at its root`);return e}function Or({n:e,of:t}){return(0,$.jsxs)(`span`,{className:`fig-part`,children:[e,(0,$.jsxs)(`span`,{className:`fig-of`,children:[`/`,t]})]})}var kr=e=>e===`good`?` fig-good`:e===`bad`?` fig-bad`:``;function Ar({fig:e}){return(0,$.jsx)(`span`,{className:`fig-value${kr(e.tone)}`,children:e.value??(e.of?(0,$.jsx)(Or,{n:e.of[0],of:e.of[1]}):`—`)})}function jr({fig:e}){return(0,$.jsxs)(`div`,{className:`fig`,children:[(0,$.jsx)(`span`,{className:`fig-label`,children:e.label}),(0,$.jsx)(Ar,{fig:e})]},e.key)}function Mr({label:e,lead:t,rest:n,dense:r}){return(0,$.jsxs)(`div`,{className:`figures${r?` dense`:``}`,role:`group`,"aria-label":e,children:[(0,$.jsxs)(`div`,{className:`fig fig-lead`,children:[(0,$.jsx)(`span`,{className:`fig-label`,children:t.label}),(0,$.jsxs)(`div`,{className:`fig-headline`,children:[(0,$.jsx)(Ar,{fig:t}),t.under&&(0,$.jsx)(`span`,{className:`fig-under`,children:(0,$.jsx)(Or,{n:t.under[0],of:t.under[1]})})]})]}),!!n.length&&(0,$.jsx)(`div`,{className:`fig-rest`,children:n.map(e=>(0,$.jsx)(jr,{fig:e},e.key))})]})}var Nr={type:`Connection type`,profile:`Request profile`,px:`Resolution`,format:`Image format`,quality:`Image quality`,seed:`Seed`,nPredict:`Reply tokens`,repeatPenalty:`Repeat penalty`,frequencyPenalty:`Frequency penalty`,presencePenalty:`Presence penalty`,dryMultiplier:`DRY multiplier`,dryBase:`DRY base`,dryAllowedLength:`DRY allowed length`,grammar:`Grammar`,url:`Address`,model:`Model`,temperature:`Temperature`,imagePosition:`Image position`,cachePrompt:`Cache prompt`,thinking:`Thinking`,rejectLoops:`Reject looping replies`,maxTokens:`Reply tokens`},Pr={format:`image/jpeg`,quality:`0.92`,seed:``,nPredict:``,repeatPenalty:``,frequencyPenalty:``,presencePenalty:``,dryMultiplier:``,dryBase:``,dryAllowedLength:``,grammar:``,px:`921600`},Fr=[`type`,`profile`,`px`,`format`,`quality`,`seed`,`nPredict`,`repeatPenalty`,`frequencyPenalty`,`presencePenalty`,`dryMultiplier`,`dryBase`,`dryAllowedLength`,`grammar`,`imagePosition`,`cachePrompt`,`thinking`,`rejectLoops`,`url`,`model`,`temperature`];function Ir(e){let t=(t,n)=>String(e[t]??``).trim()?[`${n} ${e[t]}`]:[];return[...t(`frequencyPenalty`,`frequency`),...t(`presencePenalty`,`presence`),...String(e.dryMultiplier??``).trim()?[`DRY ${e.dryMultiplier} / ${e.dryBase||`–`} / ${e.dryAllowedLength||`–`}`]:[],...String(e.grammar??``).trim()?[`grammar`]:[]].join(`, `)}var Lr=(e,t)=>({...Pr,...t?.setupProfile?.settings||{}});function Rr(e){return Nr[e]??e}function zr(e,t){return e===`type`?Br(String(t)):e===`profile`?t===`speed`?`Speed`:`Quality`:e===`px`?J().budgetLabel(t):e===`format`?String(t).replace(`image/`,``).toUpperCase():String(t)}function Br(e){return J().CONNECTION_TYPES[e]?.label??e}var Vr=e=>e.content?e.content!==`text`:null;function Hr({pass:e}){return(0,$.jsx)(`span`,{className:`mark ${e?`met`:`not`}`,role:`img`,"aria-label":e?`Passed`:`Failed`,children:(0,$.jsx)(e?De:k,{size:14,strokeWidth:2.5,"aria-hidden":`true`})})}var Ur=e=>Number(new Date(e)).toString(36).slice(-6).toUpperCase(),Wr=(e,t,n=`${t}s`)=>`${e} ${e===1?t:n}`,Gr=e=>{let t=e?e.timed??e.ran:0;return t?e.ms/t:null};function Kr(e,t){let n=e.scenarios.map(()=>0);for(let e of t.results||[])e&&!e.unrun&&(e.scenarios||[]).forEach((e,t)=>{t<n.length&&typeof e?.res?.ms==`number`&&n[t]++});return{...e,scenarios:e.scenarios.map((e,t)=>({...e,timed:n[t]??0}))}}var qr=e=>new Date(e).toLocaleString(void 0,{dateStyle:`medium`,timeStyle:`short`}),Jr={queued:`Queued`,running:`Running`,done:`Done`,incomplete:`Incomplete`,cancelled:`Cancelled`,failed:`Failed`,interrupted:`Interrupted`},Yr=e=>Jr[e]??e;function Xr(e){return Jr[e]?(0,$.jsx)(`span`,{className:`rchip ${e}`,children:Jr[e]}):null}var Zr=e=>e.length?e.reduce((e,t)=>e+t,0)/e.length:null;function Qr(e){if(Math.round(e)<1e3)return`${Math.max(0,Math.round(e))} ms`;let t=Math.round(e/100);if(t<600)return`${(t/10).toFixed(1)}s`;let n=Math.round(e/1e3),r=Math.floor(n/60),i=n%60;if(r<60)return i?`${r}m ${i}s`:`${r}m`;let a=Math.floor(r/60),o=r%60;return o?`${a}hr ${o}`:`${a}hr`}function $r(e,t){if(!e)return null;let n=new Date(e).getTime();if(Number.isNaN(n))return null;let r=t?new Date(t).getTime():Date.now();return Number.isNaN(r)?null:Math.max(0,r-n)}var ei=e=>{let t=(e.scenarios||[]).filter(Boolean),n=t.reduce((e,t)=>e+(t.passed||0),0),r=t.reduce((e,t)=>e+(t.cases||0),0),i=t.map(e=>e.percent).filter(e=>e!=null),a=t.map(Gr).filter(e=>e!=null);return{pct:r?Math.round(100*n/r):null,avgScore:i.length?Math.round(Zr(i)):null,avgTime:a.length?Zr(a):null}},ti=e=>{let t=ei(e);return{pct:t.pct==null?`—`:`${t.pct}%`,avgScore:t.avgScore==null?`—`:`${t.avgScore}%`,avgTime:t.avgTime==null?`—`:Qr(t.avgTime)}},ni=e=>!!e.run&&(e.status===`cancelled`||e.status===`interrupted`),ri=e=>e.rerunOf?(0,$.jsxs)(`span`,{className:`hrerun note`,children:[`Re-run of `,e.rerunOfAt?Ur(e.rerunOfAt):`a cleared run`]}):null,ii=[{key:`runid`,label:`Run ID`,on:!0,sort:e=>e.at,get:e=>(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`code`,{className:`runlink`,"data-openrun":e.run??void 0,children:Ur(e.at)}),ri(e)]})},{key:`date`,label:`Date`,on:!0,sort:e=>e.at,get:e=>(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`time`,{dateTime:e.at,title:new Date(e.at).toLocaleString(),children:qr(e.at)}),e.cancelled&&(0,$.jsx)(`span`,{className:`err`,children:` cancelled`})]})},{key:`state`,label:`State`,on:!0,sort:e=>Yr(e.status),get:e=>Xr(e.status)},{key:`content`,label:`Content`,on:!0,get:e=>e.content||`—`},{key:`pct`,label:`% Passed Items`,on:!0,numeric:!0,sort:e=>ei(e).pct,get:e=>ti(e).pct},{key:`avgscore`,label:`Avg Score`,on:!0,numeric:!0,sort:e=>ei(e).avgScore,get:e=>ti(e).avgScore},{key:`avgtime`,label:`Avg Time`,on:!0,numeric:!0,sort:e=>ei(e).avgTime,get:e=>ti(e).avgTime},{key:`duration`,label:`Duration`,on:!1,numeric:!0,sort:e=>$r(e.startedAt,e.finishedAt),get:e=>{let t=$r(e.startedAt,e.finishedAt);return t==null?`—`:Qr(t)}},{key:`ran`,label:`Ran`,on:!1,numeric:!0,sort:e=>e.done,get:e=>(0,$.jsx)(Or,{n:e.done,of:e.total})},{key:`setup`,label:`Pipeline`,on:!1,get:e=>e.setup||`—`},{key:`plugins`,label:`Plugins`,on:!1,get:e=>e.plugins||`—`},{key:`comment`,label:`Comment`,on:!1,sort:e=>e.comment,get:e=>(0,$.jsx)(`button`,{type:`button`,className:`ghost linkish hcomment-btn`,"data-editcomment":e.run??void 0,"aria-label":e.comment?void 0:`Add a comment`,children:e.comment?(0,$.jsx)(`span`,{className:`hcomment`,title:e.comment,children:e.comment}):(0,$.jsx)(`span`,{className:`hcomment empty`,children:`——`})})}],ai=[{key:`name`,label:`Name`,on:!0,sort:(e,t)=>e?.name||`Target ${J().targetLetter(t)}`,get:(e,t,n)=>(0,$.jsx)(`button`,{type:`button`,className:`ghost linkish`,"data-scenario":n,children:t?.name||`Target ${J().targetLetter(n)}`})},{key:`outcome`,label:`Outcome`,on:!0,sort:e=>e.pass,get:(e,t)=>t.pass==null?(0,$.jsx)(`span`,{className:`note`,children:`—`}):(0,$.jsx)(`b`,{className:t.pass?`hit`:`fault`,children:t.pass?`Pass`:`Fail`})},{key:`evals`,label:`Evals`,on:!0,sort:e=>e.evals||null,get:(e,t)=>t.evalParts?.length?t.evalParts.map((e,t)=>(0,$.jsxs)(`span`,{children:[t>0&&`, `,e.label,` `,e.mark==null?e.value:(0,$.jsx)(Hr,{pass:e.mark})]},t)):t.evals||`—`},{key:`profile`,label:`Target Profile`,on:!0,sort:e=>e?.setupProfile?.name,get:(e,t,n)=>t?.setupProfile?.name?(0,$.jsx)(`button`,{type:`button`,className:`ghost linkish`,"data-prof":n,children:t.setupProfile.name}):`—`},{key:`passed`,label:`Passed Items`,on:!0,numeric:!0,sort:e=>e.cases?e.passed/e.cases:null,get:(e,t)=>t.cases?(0,$.jsx)(Or,{n:t.passed,of:t.cases}):`—`},{key:`score`,label:`Score`,on:!0,numeric:!0,sort:e=>e.percent,get:(e,t)=>t.percent==null?`—`:`${t.percent}%`},{key:`avgtime`,label:`Avg Time`,on:!0,numeric:!0,sort:e=>Gr(e),get:(e,t)=>{let n=Gr(t);return n==null?`—`:Qr(n)}},{key:`total`,label:`Total`,on:!1,numeric:!0,sort:e=>e.ms||null,get:(e,t)=>t.ms?Qr(t.ms):`—`},{key:`model`,label:`Model`,on:!1,get:(e,t)=>t.model||`—`},{key:`res`,label:`Resolution`,on:!1,image:!0,get:(e,t)=>String(J().budgetLabel(Lr(e,t).px))},{key:`format`,label:`Format`,on:!1,image:!0,get:(e,t)=>String(Lr(e,t).format).replace(`image/`,``).toUpperCase()},{key:`quality`,label:`Quality`,on:!1,image:!0,get:(e,t)=>Lr(e,t).format===`image/png`?`—`:String(Lr(e,t).quality)},{key:`seed`,label:`Seed`,on:!1,get:(e,t)=>String(Lr(e,t).seed||`1234`)},{key:`predict`,label:`Reply tokens`,on:!1,get:(e,t)=>String(Lr(e,t).nPredict||Lr(e,t).maxTokens||`—`)},{key:`penalty`,label:`Repeat penalty`,on:!1,get:(e,t)=>String(Lr(e,t).repeatPenalty||`—`)},{key:`decoding`,label:`Decoding`,on:!1,get:(e,t)=>Ir(Lr(e,t))||`—`},...Object.entries(J().MODIFIERS).map(([e,t])=>({key:`modifier-${e}`,label:t.label||e,on:!1,get:(t,n)=>n.modifiers?.[e]||`—`}))],oi={"Run Scenarios":`Run Targets`},si={tests:`evals`},ci=[[`Runs`,ii],[`Run Scenarios`,ai]],li=e=>![`queued`,`running`].includes(e),ui=()=>({ran:0,passed:0,found:0,of:0,ms:0,timed:0,share:0,shared:0});function di(e){return e.of?Math.round(100*e.found/e.of):e.shared?Math.round(100*e.share/e.shared):null}var fi=(e,t)=>e.filter(e=>!e.whole).map(e=>e.items[t]).filter(e=>!!e&&!J().isSkipped(e)),pi=e=>Array.isArray(e)?e.length:typeof e==`number`?e:0;function mi(e,t){let n=t.map(ui);return(e||[]).forEach((e,r)=>{e&&!e.unrun&&(e.scenarios||[]).forEach((e,i)=>{let a=n[i];if(!a||!e)return;let o=fi(t[i],r);if(o.length){a.ran++,o.every(e=>e.pass)&&a.passed++;for(let e of o)a.found+=pi(e.found),a.of+=pi(e.found)+pi(e.missed)+pi(e.invented),e.found==null&&e.missed==null&&typeof e.score==`number`&&!e.points&&(a.share+=e.score,a.shared++)}e.res&&(a.timed++,a.ms+=e.res.ms||0)})}),n}var hi=(e,t,n)=>J().targetsOf(e).map((r,i)=>J().scenarioEvals(e,i,t,li(n))),gi=(e,t,n)=>(e.outcomes[n]??[]).some(e=>!e.whole&&J().failedScore(e.items[t]));function _i(e){return e.skipped?`Skipped`:e.whole?e.verdict?.ran?e.verdict.pass?`Passed`:`Failed`:`—`:e.ran?`${e.passed}/${e.ran}`:e.skippedItems?`Skipped`:`—`}var vi=e=>!e.skipped&&e.whole&&e.verdict?.ran?!!e.verdict.pass:null,yi=e=>e.map(e=>`${e.label} ${_i(e)}`).join(`, `),bi=e=>e.map(e=>({label:e.label,value:_i(e),mark:vi(e)})),xi=e=>{if(!e.group)return null;let t=J().casesRef(e)?.n??e.pin;return t==null?null:`v${t}`},Si=(e,t,n)=>e.group?.name||e.group?.id||(e.name||``).trim()||J().evalLabel({evals:n},t);function Ci(e){let t=e.evals,n=t.map((n,r)=>({key:n.id,name:Si(n,r,t),version:xi(n),cells:e.scenarios.map((t,n)=>{let i=e.outcomes[n]?.[r];return{pct:i&&!i.whole&&i.ran?Math.round(100*i.passed/i.ran):null,pass:i?J().groupPass(i):null}})})),r=e.snapshot.pass;return{rows:n,overall:t.length?{label:r?.mode===`atLeast`?`At least ${r.count}`:`All groups pass`,pass:e.scenarios.map((t,n)=>J().overallVerdict(e.outcomes[n]??[],r))}:null}}var wi=e=>e.scenarios.map((t,n)=>J().recordedTargetOf(e.snapshot,n)),Ti=e=>e.findIndex(e=>!e);function Ei(e){return!e||e.error?null:e.transcript?.at(-1)?.got??e.raw??null}function Di(e,t,n,r){let i=e.results[t]?.scenarios?.[n]?.scores?.[r];return i&&!J().isSkipped(i)?i.metrics??[]:[]}var Oi=e=>`${e.type}::${e.label}`;function ki(e,t){let n=e.scenarios.length,r=(n,r)=>Di(e,n,r,t),i=[],a=new Set,o=[];e.results.forEach((e,t)=>{for(let e=0;e<n;e++){let n=r(t,e);n.length>o.length&&(o=n)}});let s=e=>{let t=Oi(e);a.has(t)||(a.add(t),i.push({key:t,label:e.label}))};return o.forEach(s),e.results.forEach((e,t)=>{for(let e=0;e<n;e++)r(t,e).forEach(s)}),i.map(({key:t,label:i})=>({key:t,label:i,cells:Array.from({length:n},(n,i)=>{let a=0,o=0;return e.results.forEach((e,n)=>{let s=r(n,i).find(e=>Oi(e)===t);s&&s.weight!==0&&(o++,s.pass&&a++)}),o?{passed:a,ran:o}:null})}))}function Ai(e){let t=e.snapshot||{},n=J().contentOf(t),r=!!(n&&J().CONTENT_TYPES[n.type]?.inline),i=J().lastKind(t),a=e.results||[],o=hi(t,a,e.status);return{id:e.id,status:e.status,at:e.submittedAt,behind:e.behind,name:t.name||`Run`,error:e.error,progress:e.progress||{current:null,n:0,total:0},scenarios:J().targetsOf(t).map((e,n)=>({id:e?.id||`scenario-${n}`,name:J().targetLabel(t,n),kind:i,plain:!(i&&J().OUTPUT_KINDS[i]?.terms)})),items:r?[null]:n?.type===`source`&&n.files||[],content:r?{type:`text`,text:n.text??``}:{type:`source`,source:n?.ref?.id??``,sourceName:n?.ref?.name??``},evals:J().evalsOf(t),snapshot:t,results:a,outcomes:o,tallies:mi(a,o),startedAt:e.startedAt,finishedAt:e.finishedAt}}function ji(e){let t=e.snapshot||{},n=J().contentOf(t),r=e.results||[],i=J().targetsOf(t),a=t.jobs||[],o=hi(t,r,e.status),s=mi(r,o),c=i.map((e,n)=>{let r=s[n]??ui(),i=(a.length?J().stagesFor(t,n).connections:[]).map(e=>String(e.model||``)),c=i[0]?new Set(i).size===1?i[0]:i.join(` → `):``,l=di(r),u=J().overallVerdict(o[n]??[],t.pass);return{name:J().targetLabel(t,n),id:e?.id,model:c,setupProfile:J().scenarioProfile(t,n),modifiers:Object.fromEntries((J().outOf(t.jobs?.at(-1))?.modifiers||[]).map(e=>[e.type,J().modifierSummary(e)])),percent:l,passed:r.passed,cases:r.ran,pass:u,detail:u==null?`not graded`:yi(o[n]??[]),evals:yi(o[n]??[]),evalParts:bi(o[n]??[]),ms:r.ms,ran:r.ran}}),l=e.progress||{},u=l.n??l.total??0;return{at:e.submittedAt,run:e.id,status:e.status,cancelled:e.status===`cancelled`,startedAt:e.startedAt,finishedAt:e.finishedAt,setup:t.name||``,comment:t.comment||``,plugins:(t.plugins||[]).map(e=>`${e.id} ${e.version}`).join(`, `),done:u,total:l.total??u,content:n&&J().CONTENT_TYPES[n.type]?.inline?`text`:n?.type===`source`&&(n.ref?.name||n.ref?.id)||``,scenarios:c,rerunOf:e.rerunOf??null,rerunOfAt:e.rerunOfAt??null}}var Mi=e=>({key:`state`,label:`State`,value:Yr(e),tone:e===`done`?`good`:[`failed`,`incomplete`,`cancelled`,`interrupted`].includes(e)?`bad`:null}),Ni=e=>e.length?e.reduce((e,t)=>e+t,0)/e.length:null;function Pi(e){let t=0,n=0;for(let r of e||[])for(let e of r?.scenarios||[])for(let r of Object.values(e?.scores||{}))if(r&&!J().isSkipped(r))for(let e of r.metrics||[])typeof e.asked==`number`&&(t++,n+=e.asked);return{calls:t,ms:n}}function Fi(e){let t=e.tallies,n=t.reduce((e,t)=>e+t.passed,0),r=t.reduce((e,t)=>e+t.ran,0),i=t.map(di).filter(e=>e!=null),a=i.length?`${Math.round(Ni(i))}%`:`—`,o=$r(e.startedAt,e.finishedAt),s=Pi(e.results),c={key:`ran`,label:`Run Items`,of:e.progress.total?[e.progress.n,e.progress.total]:null};return{lead:r?{key:`pct`,label:`% Passed Items`,value:`${Math.round(100*n/r)}%`,under:[n,r]}:{...c,key:`lead-ran`},rest:[{key:`run`,label:`Run ID`,value:e.at?(0,Z.createElement)(`code`,null,Ur(e.at)):`—`},Mi(e.status),{key:`score`,label:`Avg Score`,value:a},{key:`duration`,label:`Duration`,value:o==null?`—`:Qr(o)},...s.calls?[{key:`grader`,label:`Grader`,value:`${s.calls} ${s.calls===1?`call`:`calls`}, ${Qr(s.ms)}`}]:[],...r?[c]:[],{key:`start`,label:`Start Date`,value:e.at?zi(e.at):`—`},{key:`comment`,label:`Comment`,value:e.snapshot.comment?(0,Z.createElement)(`span`,{className:`fig-comment`,title:e.snapshot.comment},e.snapshot.comment):`—`}]}}var Ii=e=>e.evals.length&&li(e.status)?`No assertions`:`—`;function Li(e,t){let n=e.tallies[t],r=Vi(e,t),i={key:`state`,label:`State`,value:r==null?Ii(e):r?`Pass`:`Fail`,tone:r==null?null:r?`good`:`bad`},a={key:`time`,label:`Avg Time`,value:Ri(n)??`—`},o=e.outcomes[t]??[],s=o.length>1?o.map(e=>({key:`eval-${e.id}`,label:e.label,value:vi(e)==null?_i(e):(0,Z.createElement)(Hr,{pass:vi(e)}),tone:e.skipped?null:e.whole?e.verdict?.ran?e.verdict.pass?`good`:`bad`:null:e.ran?e.passed===e.ran?`good`:`bad`:null})):[];return n?.ran?{lead:{key:`pct`,label:`% Passed Items`,value:`${Math.round(100*n.passed/n.ran)}%`,under:[n.passed,n.ran]},rest:[i,...s,{key:`score`,label:`Score`,value:di(n)==null?`—`:`${di(n)}%`},a]}:{lead:i,rest:[...s,a]}}function Ri(e){return e?.timed?Qr(e.ms/e.timed):null}function zi(e){let t=new Date(e);if(Number.isNaN(t.getTime()))return`—`;let n=e=>String(e).padStart(2,`0`);return`${t.getFullYear()}-${n(t.getMonth()+1)}-${n(t.getDate())} ${n(t.getHours())}:${n(t.getMinutes())}`}function Bi(e){let t=Yr(e.status);return e.status===`queued`?{state:t,detail:e.behind?`behind ${e.behind}`:`next in line`}:e.status===`running`?{state:t,detail:e.progress.current||``}:null}function Vi(e,t){return J().overallVerdict(e.outcomes[t]??[],e.snapshot.pass)}function Hi(e,t){let n=e.content?.type===`source`?e.content.source:null;return n===`samples`?`/api/samples/thumbs/${encodeURIComponent(t)}`:n?`/api/sources/${encodeURIComponent(n)}/thumbs/${encodeURIComponent(t)}`:``}function Ui(e,t){let n=e.content?.type===`source`?e.content.source:null;return n===`samples`?`/api/samples/zoom/${encodeURIComponent(t)}`:n?`/api/sources/${encodeURIComponent(n)}/zoom/${encodeURIComponent(t)}`:`/api/samples/zoom/${encodeURIComponent(t)}`}var Wi=(e,t)=>t==null||t===``?null:`${e}: ${t}`,Gi=e=>(e||[]).map(String).join(`, `),Ki=e=>e.length?e.every(t=>t===e[0])?String(e[0]):e.join(` → `):``,qi=e=>{let t=J().OUTPUT_KINDS[J().lastKind(e)??J().defaultKind()];return t?!t.terms:!1},Ji=e=>{let t=e.metric&&String(e.metric)||J().METRICS[e.type]?.label||e.type,n=J().metricSummary(e);return[e.not?`not`:``,`${t}${n?` ${n}`:``}`,e.weight===0?`(watched, not scored)`:typeof e.weight==`number`&&e.weight!==1?`×${e.weight}`:``].filter(Boolean).join(` `)};function Yi(e,t,n,r){let i=J().readCase(t,n,qi(e)),{stages:a,connections:o}=J().stagesFor(e,r),s=o.map(e=>J().connectionSettings(e)),c=e=>Ki(s.map(e)),l=J().evalsDataset(e);return[`An eval case is failing.`,``,`## The case`,Wi(`item`,J().caseItem(t)),Wi(`case id`,t.id),Wi(`note`,t.note),...(t.metrics||[]).map((e,t)=>Wi(`metric ${t+1}`,Ji(e))),``,`## What came back`,Wi(`verdict`,i.pass?`pass`:`fail`),Wi(`score`,i.score==null?`—`:`${Math.round(i.score*100)}%`),Wi(`items`,Gi(n?.terms)||`(none)`),Wi(`missed`,Gi((i.missed??[]).map(e=>Array.isArray(e)?e.join(` / `):e))),Wi(`invented`,Gi(i.invented)),Wi(`dropped`,Gi((n?.dropped||[]).map(e=>`${e.item} (${e.by})`))),Wi(`reasons`,Gi(i.reasons)),Wi(`error`,n?.error),Wi(`took`,n?.ms==null?null:`${n.ms} ms`),``,`## How it was asked`,Wi(`scenario`,`${r+1} of ${J().targetsOf(e).length}`),Wi(`model`,c(e=>e.model)),Wi(`endpoint`,c(e=>e.url||`(the lab's own Ollama)`)),Wi(`connection type`,c(e=>Br(String(e.type??``)))),Wi(`image`,a[0]?.withImage?Xi(s[0]):`no image`),Wi(`seed`,c(e=>e.seed||`1234`)),Wi(`reply tokens`,c(e=>e.nPredict||e.maxTokens||`not set`)),Wi(`repeat penalty`,c(e=>e.repeatPenalty||`(profile default)`)),Wi(`other decoding`,Zi(s.at(-1))),Wi(`modifiers`,(J().outOf(e.jobs.at(-1))?.modifiers||[]).map(e=>`${J().MODIFIERS[e.type]?.label??e.type}: ${J().modifierSummary(e)}`).join(`; `)||`none`),``,`## The prompt`,...a.flatMap((e,t)=>[a.length>1?`job ${t+1}${e.withImage?` (with image)`:``}, answering as ${e.kind}, on ${String(o[t]?.name||`unnamed`)}:`:null,e.text]),``,`## Where things live`,l?`the graded set: ${l.name||l.id} (this case is id "${t.id}")`:null,`the parser and scorer, shared by the lab, the CLI and CI: evals-core.ts`,`what the grading means: the dataset's README`].filter(e=>e!==null).join(`
5
+ `)}function Xi(e){let{px:t,format:n}=J().connectionImage(e);return`${t==null?`default budget`:J().budgetLabel(t)}, ${n.replace(`image/`,``).toUpperCase()}`}function Zi(e){let t=(t,n)=>String(e?.[t]??``).trim()?[`${n} ${e?.[t]}`]:[];return[...t(`frequencyPenalty`,`frequency`),...t(`presencePenalty`,`presence`),...String(e?.dryMultiplier??``).trim()?[`DRY`]:[],...String(e?.grammar??``).trim()?[`grammar`]:[]].join(`, `)}function Qi(e,t){let n=[];return!li(e.status)||!J().evalsDataset(e.snapshot)||(e.results||[]).forEach((r,i)=>{r&&!r.unrun&&(r.scenarios||[]).forEach((a,o)=>{let s=t.find(e=>J().caseItem(e)===r.name);gi(e,i,o)&&s&&a.res&&n.push({kase:s,res:a.res,scenarioIndex:o,item:r.name??``})})}),n}var $i=e=>{let t=Math.min(...e),n=Math.max(...e);return t===n?String(t):`${t}–${n}`};function ea(e,t,n){let r=J().evalsDataset(e.snapshot);if(!li(e.status)||!r)return[];let i=e.outcomes[n]?.find(t=>e.evals.find(e=>e.id===t.id&&J().casesRef(e))),a=new Map(t.map(e=>[J().caseItem(e),e])),o=new Map,s=(e,t,n,r,i,a)=>{let s=o.get(e);s||(s={key:e,label:n,note:null,files:[],rank:t,counts:[],limits:[]},o.set(e,s)),s.files.includes(r)||s.files.push(r),i!=null&&s.counts.push(i),a!=null&&s.limits.push(a)};return e.results.forEach((e,t)=>{if(!e||e.unrun||!e.name)return;let r=a.get(e.name),o=e.scenarios?.[n]?.res,c=i?.items[t];if(!r||r.todo||!o||!c||J().isSkipped(c)||c.pass)return;let l=e.name;if(o.error){s(`error`,6,`Error`,l);return}let u=(o.terms||[]).length,d=(r.metrics||[]).filter(e=>e.type===`item-count`&&e.weight!==0),f=(c.metrics??[]).filter(e=>e.type===`item-count`&&e.weight!==0);d.forEach((e,t)=>{let n=f[t];if(e.not||!n||n.pass)return;let r=e.max==null||e.max===``?null:Number(e.max),i=e.min==null||e.min===``?null:Number(e.min);r!=null&&u>r?s(`over`,0,`Too many items`,l,u,r):i!=null&&s(`under`,1,`Too few items`,l,u,i)});for(let e of c.missed??[])Array.isArray(e)?s(`none:${e.join(`/`)}`,3,`None of ${e.join(` / `)}`,l):s(`missed:${e}`,2,`Missed ${e}`,l);for(let e of c.invented??[])s(`invented:${e}`,4,`Invented ${e}`,l)}),[...o.values()].sort((e,t)=>e.rank-t.rank||t.files.length-e.files.length||e.label.localeCompare(t.label)).map(({key:e,label:t,files:n,counts:r,limits:i})=>({key:e,label:t,files:n,note:r.length&&i.length?`${$i(r)}, ${e===`over`?`max`:`min`} ${$i(i)}`:null}))}var ta=[{id:`home`,label:`Home`,icon:t,built:!0},{id:`runs`,label:`Runs`,icon:Qe,built:!0},{id:`history`,label:`History`,icon:ke,built:!0},{id:`library`,label:`Library`,icon:te,built:!0},{id:`setup`,label:`Setup`,icon:ae,built:!0}],na=e=>e.replace(/[^A-Za-z0-9._-]+/g,`-`).replace(/^[-.]+|[-.]+$/g,``);function ra(e=new Date){let t=e=>String(e).padStart(2,`0`);return`${e.getFullYear()}${t(e.getMonth()+1)}${t(e.getDate())}-${t(e.getHours())}${t(e.getMinutes())}`}function ia(e,t,n,r,i=new Date){return`${e}-${na(t)||e}-v${n}-${ra(i)}.${r}`}function aa(e,t){let n=URL.createObjectURL(e),r=document.createElement(`a`);r.href=n,r.download=t,document.body.appendChild(r),r.click(),r.remove(),setTimeout(()=>URL.revokeObjectURL(n),0)}var oa=`#/library/datasets`,sa=()=>{location.hash=oa},ca=e=>{location.hash=`${oa}/${encodeURIComponent(e)}`};function la({undo:e}){let t=Kn(),{list:n,error:r}=qn(),[i]=On(`promptlab.workflows`),[a,o]=Se(`eval-groups`,20),[s,c]=(0,Z.useState)(!1),[l,u]=(0,Z.useState)(null),[d,f]=(0,Z.useState)(null),p=(0,Z.useRef)(null),[,m]=(0,Z.useState)(0);(0,Z.useEffect)(()=>t.subscribe(()=>m(e=>e+1)),[t]),(0,Z.useEffect)(()=>{for(let e of n??[])t.load(e.id)},[n,t]);let h=e=>u(e instanceof Error?e.message:String(e)),g=(0,Z.useMemo)(()=>{let e=new Map;for(let t of i.list)for(let n of t.work.evals??[]){if(n.type!==`group`)continue;let r=n,i=r.group?.id??r.own?.casesFrom?.id;if(!i)continue;let a=e.get(i)??{names:[],pinned:!1};a.names.includes(t.name)||a.names.push(t.name),typeof r.pin==`number`&&(a.pinned=!0),e.set(i,a)}return e},[i.list]),v=(n??[]).map(e=>({...e,body:t.entry(e.id)?.doc?.body??null,links:g.get(e.id)??{names:[],pinned:!1}})),y=()=>new Set((t.list().list??[]).map(e=>e.name)),b=async()=>{u(null);try{ca((await t.create(da(`Untitled eval group`,y()))).id)}catch(e){h(e)}},x=async n=>{u(null);try{await t.flush(n.id);let r=await t.remove(n.id);e.offer({label:`Deleted ${n.name}`,restore:()=>{t.restore(r).catch(h)}})}catch(e){h(e)}},S=async e=>{u(null);let n;try{n=await ua(e)}catch(e){h(e);return}let r;try{r=JSON.parse(n)}catch(e){u(`Not JSON: ${e instanceof Error?e.message:String(e)}`);return}f(`import`);try{let e=await t.importFile(r);e[0]&&ca(e[0].id)}catch(e){h(e)}finally{f(null)}},C=async()=>{f(`exportAll`);try{let e=await fetch(t.exportAllUrl());if(!e.ok)throw Error(`the export answered ${e.status}`);let n=await e.json();aa(new Blob([JSON.stringify(n,null,2)+`
6
+ `],{type:`application/json`}),ia(`eval-groups`,`all`,n.version,`json`))}catch(e){h(e)}finally{f(null)}},w=e=>[{label:`Open`,icon:ht,onSelect:()=>ca(e.id)},{label:`Delete`,icon:_.remove,destructive:!0,onSelect:()=>void x(e)}],T=e=>e.scoring.mode===`weighted`?`Points`:`All`,E=[{key:`group`,header:`Eval group`,role:`title`,sort:e=>e.name.toLowerCase(),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`eg-open`,onClick:()=>ca(e.id),children:e.name})},{key:`source`,header:`Source`,sort:e=>e.body?.source?.name??``,tdProps:e=>({"data-state":e.body?.source?void 0:`none`}),cell:e=>e.body?e.body.source?.name??`Any`:`…`},{key:`cases`,header:`Cases`,className:`num`,sort:e=>e.cases,cell:e=>e.cases},{key:`every`,header:`Every item`,className:`num`,sort:e=>e.body?.every.length??-1,cell:e=>e.body?e.body.every.length:`…`},{key:`scoring`,header:`Scoring`,sort:e=>e.body?T(e.body):``,cell:e=>e.body?T(e.body):`…`},{key:`linked`,header:`Linked by`,sort:e=>e.links.names.length,tdProps:e=>({"data-state":e.links.names.length?void 0:`none`}),cell:e=>e.links.names.length?(0,$.jsx)(`span`,{title:e.links.names.join(`, `),children:`${e.links.names.length} ${e.links.names.length===1?`pipeline`:`pipelines`}${e.links.pinned?`, pinned`:``}`}):`—`},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${e.name} actions`,actions:w(e)})}],O=[{label:`Import JSON…`,icon:H,busy:d===`import`,onSelect:()=>p.current?.click()},{label:`Export all`,icon:cn,disabled:!n?.length,busy:d===`exportAll`,onSelect:()=>void C()},{label:`Page size…`,icon:_.pageSize,onSelect:()=>c(!0)}];return(0,$.jsxs)(X,{id:`evals`,title:`Evals`,note:n?String(n.length):void 0,actions:O,about:`Checks of a reply, defined once and linked by the pipelines they grade.`,children:[(0,$.jsx)(`input`,{ref:p,type:`file`,accept:`.json,application/json`,hidden:!0,"aria-label":`Import JSON file`,onChange:e=>{let t=e.target.files?.[0];e.target.value=``,t&&S(t)}}),n===null?(0,$.jsx)(M,{label:`Eval groups`,error:r,onRetry:()=>void t.refresh()}):n.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No eval groups yet.`}):(0,$.jsx)(q,{className:`eg-groups`,cards:!0,label:`Eval groups`,rows:v,rowKey:e=>e.id,pageSize:a,columns:E}),l&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:l}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost eg-add`,onClick:()=>void b(),children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),`Add eval group`]}),s&&(0,$.jsx)(St,{value:a,onChange:o,onClose:()=>c(!1)})]})}function ua(e){return new Promise((t,n)=>{let r=new FileReader;r.onload=()=>t(String(r.result??``)),r.onerror=()=>n(r.error??Error(`The file could not be read.`)),r.readAsText(e)})}var da=(e,t)=>{if(!t.has(e))return e;for(let n=2;;n++)if(!t.has(`${e} ${n}`))return`${e} ${n}`};function fa(e){return[e.every&&`Every reply`,e.not&&`Not`,e.of===`said`&&`Raw reply`,e.weight!=null&&`Weight ${e.weight}`].filter(Boolean).join(`, `)}var pa=(e,t)=>({type:e,...J().METRICS[e].defaults(),...t?.weight==null?{}:{weight:t.weight},...t?.not?{not:!0}:{},...t?.metric?{metric:t.metric}:{}});function ma({label:e,metrics:t,set:n,undoable:r,over:i=`item`,rawReply:a}){let o=J(),[s,c]=(0,Z.useState)(null),[l,u]=(0,Z.useState)(null),d=(e,r)=>n(t.map((t,n)=>n===e?r:t)),f=e=>{s===`new`?n([...t,pa(e)]):typeof s==`number`&&d(s,pa(e,t[s])),c(null)};return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(pt,{noun:`metric`,items:[...t],empty:(0,$.jsx)(`p`,{className:`note`,children:`No metrics.`}),onAdd:()=>c(`new`),onRemove:e=>r(t.filter((t,n)=>n!==e),`Removed a metric`),onRename:e=>u(e),extra:(e,t)=>[{label:`Change type…`,icon:Cn,onSelect:()=>c(t)}],renderItem:(t,n,r)=>{let s=o.METRICS[t.type],c=(t.metric||s?.label||t.type)+(t.not?` (not)`:``)+(t.weight===0?` (watch)`:``),l=`${e.replace(/\W+/g,`-`).toLowerCase()}-metric-${n}`,u=!!(t.every||t.not||t.of||t.weight!=null),f=!s?.parsedOnly&&(a!==!1||t.of===`said`),p=!s?.expects||!!t.not,m=(s?.options??[]).map(e=>Ae(e,t,e=>d(n,e)));return(0,$.jsxs)(X,{id:l,title:c,level:3,actions:r,about:s?.description,children:[m.length>0&&(0,$.jsx)(W,{label:`${e}, metric ${n+1}`,rows:m}),(0,$.jsx)(X,{id:`${l}-counts`,title:`Options`,level:3,open:u,note:u?fa(t):`Defaults`,children:(0,$.jsx)(W,{label:`${e}, metric ${n+1}, options`,rows:[...i===`run`?[{key:`every`,label:`Every reply`,control:{type:`checkbox`,checked:!!t.every,onChange:e=>{let{every:r,...i}=t;d(n,e?{...i,every:!0}:i)}}}]:[],...p?[{key:`not`,label:`Not`,control:{type:`checkbox`,checked:!!t.not,onChange:e=>{let{not:r,...i}=t;d(n,e?{...i,not:!0}:i)}}}]:[],...f?[{key:`of`,label:`Reads`,...a==null?{hint:`Raw reply: the API's text before Read as`}:{},control:{type:`select`,value:t.of??`reply`,options:[{value:`reply`,label:`Parsed reply`},{value:`said`,label:`Raw reply`}],onChange:e=>{let{of:r,...i}=t;d(n,e===`said`?{...i,of:`said`}:i)}}}]:[],{key:`weight`,label:`Weight`,control:{type:`text`,size:`num`,value:t.weight==null?``:String(t.weight),placeholder:`1`,onChange:e=>{let{weight:r,...i}=t;d(n,e.trim()?{...i,weight:/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e}:i)}}}]})})]},n)}}),l!==null&&t[l]&&(0,$.jsx)(nt,{title:`Rename metric`,value:t[l].metric||o.METRICS[t[l].type]?.label||t[l].type,onCommit:e=>{let{metric:n,...r}=t[l];d(l,e&&e!==o.METRICS[r.type]?.label?{...r,metric:e}:r)},onClose:()=>u(null)}),s!==null&&(0,$.jsx)(ha,{title:s===`new`?`Add metric`:`Change type`,current:typeof s==`number`?t[s]?.type??null:null,over:i,onPick:f,onClose:()=>c(null)})]})}function ha({title:e,current:t,over:n,onPick:r,onClose:i}){let[a,o]=(0,Z.useState)(``),s=(0,Z.useMemo)(()=>{let e=new Map;for(let[t,r]of Object.entries(J().METRICS)){if(n===`run`&&(r.graded||r.perItem))continue;let i=`${r.label} ${r.description??``}`.toLowerCase();if(a.trim()&&!i.includes(a.trim().toLowerCase()))continue;let o=r.family??`Other`;e.set(o,[...e.get(o)??[],{id:t,label:r.label,description:r.description??``}])}return[...e]},[a,n]);return(0,$.jsxs)(Tt,{title:e,onClose:i,className:`metric-picker`,children:[(0,$.jsx)(`input`,{type:`search`,className:`mp-search`,"aria-label":`Find a metric`,placeholder:`Find a metric`,value:a,onChange:e=>o(e.target.value),autoComplete:`off`}),s.length===0&&(0,$.jsx)(`p`,{className:`note`,children:`No metric matches.`}),s.map(([e,n])=>(0,$.jsxs)(`fieldset`,{className:`mp-family`,children:[(0,$.jsx)(`legend`,{children:e}),(0,$.jsx)(`div`,{className:`mp-grid`,children:n.map(e=>(0,$.jsx)(`button`,{type:`button`,className:`mp-type${e.id===t?` on`:``}`,...e.id===t?{"aria-current":`true`}:{},title:e.description,onClick:()=>r(e.id),children:e.label},e.id))})]},e))]})}function ga(e,t,n=[]){return{options:[...n.map(e=>({...e,fixed:!0})),...e.map(e=>{let n=t(e.id);return{id:e.id,label:e.name||`Untitled`,used:n?{at:n,label:qr(n)}:null}})],recent:e.filter(e=>t(e.id)).sort((e,n)=>t(n.id).localeCompare(t(e.id))).slice(0,5).map(e=>e.id)}}function _a({name:e,body:t,onSave:n,onClose:r}){let[i]=(0,Z.useState)(()=>JSON.stringify(t,null,2)),[a,o]=(0,Z.useState)(i),[s,c]=(0,Z.useState)(()=>ya(a));(0,Z.useEffect)(()=>{let e=setTimeout(()=>c(ya(a)),400);return()=>clearTimeout(e)},[a]);let l=s.problems;return(0,$.jsxs)(Tt,{title:`Edit raw… ${e}`,onClose:r,wide:!0,dirty:a!==i,children:[(0,$.jsx)(`textarea`,{className:`raw-content`,"aria-label":`Dataset JSON`,spellCheck:!1,value:a,onChange:e=>o(e.target.value)}),(0,$.jsxs)(`div`,{className:`raw-foot`,children:[(0,$.jsx)(`span`,{className:`note ${l.length?` bad`:``}`,role:l.length?`alert`:void 0,children:l.length?`${l.length} to fix — ${l.join(`, `)}`:`Validated before it is accepted.`}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:r,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,className:`run`,onClick:()=>{let e=ya(a);e.body?n(e.body):c(e)},disabled:l.length>0,children:`Save & Close`})]})]})}var va=[`version`,`source`,`scoring`,`grader`,`every`,`run`,`cases`];function ya(e){let t;try{t=JSON.parse(e)}catch(e){return{body:null,problems:[`Not JSON: ${e instanceof Error?e.message:String(e)}`]}}if(!t||typeof t!=`object`||Array.isArray(t))return{body:null,problems:[`a dataset has to be a JSON object`]};let n=t,r=[];for(let e of Object.keys(n))va.includes(e)||r.push(`${e} is not part of a dataset`);Array.isArray(n.cases)||r.push(`cases has to be a list`);let i=J().DATASET_BODY_VERSION;if(`version`in n&&n.version!==i&&r.push(`version has to be ${i}`),r.length)return{body:null,problems:r};let a=jn(),o={version:i,source:n.source??null,scoring:n.scoring??a.scoring,grader:n.grader??null,every:n.every??[],run:n.run??[],cases:n.cases};return r.push(...J().validateEvals(o)),r.length?{body:null,problems:r}:{body:o,problems:[]}}function ba(e,t){return`/api/sources/${encodeURIComponent(e)}/thumbs/${encodeURIComponent(t)}`}function xa(e){return/\.(?:jpe?g|png|gif|webp|bmp|heic|heif|dng|tiff?)$/i.test(e)}function Sa({title:e,action:t,source:n,files:r,taken:i,onPick:a,onClose:o}){let s=(0,Z.useMemo)(()=>r.filter(e=>!i.has(e)),[r,i]),[c,l]=(0,Z.useState)(``),[u,d]=(0,Z.useState)(s[0]??null),f=s.filter(e=>!c.trim()||e.toLowerCase().includes(c.trim().toLowerCase()));return(0,$.jsxs)(Tt,{title:e,onClose:o,className:`item-picker`,footer:(0,$.jsxs)(`div`,{className:`edit-actions`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:o,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,className:`edit-done`,disabled:!u||!s.includes(u),onClick:()=>{u&&a(u)},children:t})]}),children:[(0,$.jsxs)(`div`,{className:`prop ip-source`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:`Source`}),(0,$.jsx)(`span`,{className:`prop-value`,children:n.name})]}),(0,$.jsx)(`input`,{type:`search`,className:`ip-search`,"aria-label":`Find an item`,placeholder:`Find an item`,value:c,onChange:e=>l(e.target.value),autoComplete:`off`}),(0,$.jsxs)(`fieldset`,{className:`ip-items`,children:[(0,$.jsxs)(`legend`,{className:`note`,children:[s.length,` `,s.length===1?`item`:`items`,` without a case`]}),s.length?f.length?f.map(e=>(0,$.jsxs)(`label`,{className:`ip-item${e===u?` on`:``}`,children:[(0,$.jsx)(`input`,{type:`radio`,name:`item`,checked:e===u,onChange:()=>d(e)}),xa(e)?(0,$.jsx)(`img`,{src:ba(n.id,e),alt:``,loading:`lazy`}):(0,$.jsx)(`span`,{className:`ip-thumb`,"aria-hidden":`true`}),(0,$.jsx)(`span`,{className:`ip-name`,children:e})]},e)):(0,$.jsx)(`p`,{className:`note`,children:`No item matches.`}):(0,$.jsxs)(`p`,{className:`note`,children:[`Every item of `,n.name,` has a case.`]})]})]})}var Ca=2e3,wa=e=>({id:``,item:e,todo:!0,note:``,metrics:[]}),Ta=e=>{let t=e.map(e=>/^sample-(\d+)$/.exec(String(e?.id??``))?.[1]??0);return`sample-${String(Math.max(0,...t.map(Number))+1).padStart(3,`0`)}`};function Ea({item:e,cases:t,onWrite:n,problem:r,resultFor:i,scenarioCount:a,plain:s=!1,files:c,onFile:l,onClose:u,run:d,source:f=null,sourceFiles:p=null,onRemove:m}){let h=t.find(t=>J().caseItem(t)===e),g=!h,[_,v]=(0,Z.useState)(()=>structuredClone(h??wa(e))),[y,b]=(0,Z.useState)(0),[x,S]=(0,Z.useState)(null),[C,w]=(0,Z.useState)(null),[T,E]=(0,Z.useState)(!1),[D,O]=(0,Z.useState)(!1),[k,A]=(0,Z.useState)(!1),j=(0,Z.useRef)(_);j.current=_;let M=(0,Z.useRef)(t);M.current=t;let N=(0,Z.useRef)(null),P=t=>M.current.filter(n=>!(t.id&&n.id===t.id)&&!(!t.id&&J().caseItem(n)===e)).some(e=>J().caseItem(e)===J().caseItem(t))?`${J().caseItem(t)} has a case already`:J().validateEvals({cases:[{...t,id:t.id||`new`}]})[0]?.replace(/^[^:]*: /,``)??null,F=P(_),I=(0,Z.useCallback)(()=>{let e=j.current;if(P(e))return!1;e.id||(e={...e,id:Ta(M.current)},j.current=e,v(e));let t=e;return n(e=>Da({cases:e},t)),!0},[n,e]);(0,Z.useEffect)(()=>()=>{N.current&&clearTimeout(N.current)},[]);let[L,R]=(0,Z.useState)(!1),z=e=>{v(e),R(!0)},B=e=>{let t=j.current;z({...t,metrics:e,...t.todo&&!(t.metrics??[]).length&&e.length?{todo:!1}:{}})},ee=C&&C[y]?C[y]:i(y),te=!ee||_.todo||!(_.metrics??[]).length?null:J().readCase(_,ee,s),V=async()=>{if(d&&!T){E(!0),S(null);try{let e=await d(J().caseItem(j.current));w(e),S(e.length?null:`No reply to grade.`)}catch(e){S(e instanceof Error?e.message:String(e))}finally{E(!1)}}},H=c.indexOf(e),U=e=>{let t=H<0?null:c[H+e];if(!t)return;let n=L||g;(!n||I())&&(n&&(O(!0),N.current&&clearTimeout(N.current),N.current=setTimeout(()=>O(!1),Ca)),l(t))};(0,Z.useEffect)(()=>{let e=e=>{(e.key===`ArrowLeft`||e.key===`ArrowRight`)&&(e.target?.closest?.(`input, textarea, select, [contenteditable]`)||(e.preventDefault(),U(e.key===`ArrowRight`?1:-1)))};return document.addEventListener(`keydown`,e),()=>document.removeEventListener(`keydown`,e)});let W=()=>{(!L&&!g||I())&&u()},G=J().caseItem(_),ne=new Set(t.filter(t=>!(_.id&&t.id===_.id)&&J().caseItem(t)!==e).map(e=>J().caseItem(e))),re=!!p&&!p.includes(G),ie=[{key:`item`,label:`Item`,control:f?{type:`static`,text:re?`${G||`None`} (missing)`:G}:{type:`text`,value:G,onChange:e=>z({...j.current,item:e.trim()})}},{key:`todo`,label:`To do`,control:{type:`checkbox`,checked:!!_.todo,label:`Not graded yet`,onChange:e=>z({...j.current,todo:e})}},{key:`note`,label:`Note`,control:{type:`textarea`,value:_.note??``,onChange:e=>z({...j.current,note:e})}}],ae=[];a>1&&ae.push({key:`scenario`,label:`Show result of`,control:{type:`select`,value:String(y),options:Array.from({length:a},(e,t)=>({value:String(t),label:`Target ${J().targetLetter(t)}`})),onChange:e=>b(Number(e))}}),te&&(ae.push({key:`verdict`,label:`Result`,control:{type:`static`,text:te.pass?`Pass`:`Fail`}}),te.watch.length&&ae.push({key:`watch`,label:`Watch`,control:{type:`static`,text:`${te.watch.filter(e=>e.pass).length} of ${te.watch.length}`}}));let oe=f&&G&&xa(G)&&p?.includes(G)?{src:ba(f.id,G),file:G,source:f.name}:null,se=[...oe?[{key:`preview`,title:`Preview`,children:(0,$.jsxs)(`div`,{className:`cf-preview`,children:[(0,$.jsx)(`img`,{src:oe.src,alt:oe.file,loading:`lazy`}),(0,$.jsxs)(`p`,{className:`note`,children:[oe.file,` in `,oe.source]})]})}]:[],{key:`case`,title:`Case`,rows:ie},{key:`metrics`,title:`Metrics`,children:(0,$.jsx)(ma,{label:`${G||`Case`} metrics`,metrics:_.metrics??[],set:B,undoable:B})},...ae.length?[{key:`result`,title:`Result`,rows:ae}]:[]],ce=[{label:`Previous`,icon:vt,disabled:H<=0,onSelect:()=>U(-1)},{label:`Next`,icon:Ke,disabled:H<0||H>=c.length-1,onSelect:()=>U(1)},...d?[{label:`Run`,icon:bn,disabled:T,onSelect:()=>void V()}]:[]],le=[...f?[{label:`Change item…`,icon:xn,disabled:!p,onSelect:()=>A(!0)}]:[],...m?[{label:`Remove`,icon:o,destructive:!0,onSelect:()=>{m(J().caseItem(j.current)),u()}}]:[]],ue=x??r??(F?`Not saved: ${F}`:null);return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(Ot,{title:G||`Case`,status:ue??(T?`Running…`:D?`Saved.`:void 0),...ue?{statusBad:!0}:{},sections:se,...le.length?{menu:le}:{},actions:ce,dirty:L,onSave:W,onClose:u}),k&&f&&p&&(0,$.jsx)(Sa,{title:`Change item`,action:`Change`,source:f,files:p,taken:new Set([...ne,G]),onPick:e=>{A(!1),z({...j.current,item:e})},onClose:()=>A(!1)})]})}function Da(e,t){let n=e.cases.findIndex(e=>e.id===t.id),r=structuredClone(e.cases);return n>=0?r[n]=structuredClone(t):r.push(structuredClone(t)),r}function Oa(e,t){return structuredClone(e.cases).filter(e=>J().caseItem(e)!==t)}function ka(e){let t=Q(),[n,r]=(0,Z.useState)(null);return(0,Z.useEffect)(()=>{if(!e)return;let n=!0;return t.source(e).then(t=>{n&&r({id:e,files:t.files.map(e=>e.name)})}).catch(()=>{n&&r(null)}),()=>{n=!1}},[t,e]),e&&n?.id===e?n.files:null}var Aa=(0,Z.createContext)(null);function ja({children:e}){let t=Kn(),[n,r]=(0,Z.useState)(null),i=Jn(n?.dataset??null),a=(0,Z.useCallback)((e,t)=>{r({item:e,...t})},[]),o=(0,Z.useCallback)(e=>{r(t=>t&&{...t,item:e})},[]),s=i?.doc?.body.source??null,c=ka(s?.id),l=n?.dataset??``,u=(0,Z.useCallback)(e=>{t.edit(l,t=>({...t,cases:e(t.cases)}))},[t,l]),d=(0,Z.useMemo)(()=>({openCaseEditor:a}),[a]),f=i?.doc;return(0,$.jsxs)(Aa.Provider,{value:d,children:[e,n&&f&&(0,$.jsx)(Ea,{item:n.item,cases:f.body.cases,onWrite:u,problem:Mn(i.sync),resultFor:n.resultFor??(()=>null),scenarioCount:n.scenarioCount??0,files:n.files??[],onFile:o,...n.run?{run:n.run}:{},...n.plain?{plain:!0}:{},source:s,sourceFiles:c,...n.onRemove?{onRemove:n.onRemove}:{},onClose:()=>r(null)},n.item)]})}function Ma(){let e=(0,Z.useContext)(Aa);if(!e)throw Error(`useCaseEditor outside a CaseEditorProvider: the app mounts one at its root`);return e}function Na({rows:e,shown:t,filters:n,onFilters:r,selected:i,onSelect:a,selectionActions:o,sourceFiles:s,hasSource:c,pageSize:l,onEdit:u,onAdd:d,onRemove:f,loading:p,loadError:m,onRetry:h,children:g}){let _=t.map(ar),v=_.filter(e=>i.has(e)).length,y=v>0&&v===_.length,b=[{key:`status`,label:`Status`,value:n.status,onChange:e=>r({status:e}),options:[{value:`all`,label:`All`},{value:`graded`,label:`Graded`},{value:`todo`,label:`To do`},...s?[{value:`missing`,label:`Missing`}]:[]]},{key:`show`,label:`Show`,value:n.show,onChange:e=>r({show:e}),options:[{value:`all`,label:`All`},{value:`selected`,label:`Selected`}]}];return p?(0,$.jsxs)(`div`,{className:`ds-casebody`,children:[g,(0,$.jsx)(M,{label:`Cases`,error:m,onRetry:h})]}):(0,$.jsxs)(`div`,{className:`ds-casebody`,children:[e.length>0&&(0,$.jsx)(Le,{noun:`cases`,search:{label:`Item`,placeholder:`Search items`,value:n.file,onChange:e=>r({file:e})},filters:b,count:{shown:t.length,total:e.length},lead:(0,$.jsx)(`label`,{className:`ev-check ev-all`,children:(0,$.jsx)(Pa,{all:y,some:v>0,disabled:!t.length,onChange:()=>a(e=>{let t=new Set(e);for(let e of _)y?t.delete(e):t.add(e);return t})})})}),g,e.length===0?(0,$.jsx)(`p`,{className:`ds-none`,children:c?`No cases.`:`No Source yet.`}):(0,$.jsx)($.Fragment,{children:t.length===0?(0,$.jsxs)($.Fragment,{children:[i.size>0&&(0,$.jsx)(Fe,{count:i.size,noun:`case`,hidden:i.size,actions:o,onClear:()=>a(()=>new Set)}),(0,$.jsx)(`p`,{className:`note`,children:`No cases match these filters.`})]}):(0,$.jsx)(`div`,{className:`ds-cases`,children:(0,$.jsx)(q,{className:`dstable`,cards:!0,label:`Cases`,rows:t,pageSize:l,defaultSort:null,rowKey:e=>ar(e),select:{noun:`case`,selected:i,onChange:e=>a(()=>e),actions:()=>o},columns:Fa(s,u,f)})})}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost ds-add`,onClick:d,disabled:!c||!s,children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),`Add case`]})]})}function Pa({all:e,some:t,disabled:n,onChange:r}){let i=(0,Z.useRef)(null);return(0,Z.useEffect)(()=>{i.current&&(i.current.indeterminate=t&&!e)},[t,e]),(0,$.jsx)(`input`,{ref:i,type:`checkbox`,"aria-label":`Select all shown`,checked:e,disabled:n,onChange:r})}function Fa(e,t,n){let r=t=>yr(t,e);return[{key:`item`,header:`Item`,role:`title`,sort:e=>e.item,cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`ds-file`,onClick:()=>t(e.item),children:e.item})},{key:`status`,header:`Status`,sort:e=>vr[r(e)],tdProps:e=>({"data-state":r(e)}),cell:e=>(0,$.jsx)(`span`,{className:`ds-status ds-${r(e)}`,children:vr[r(e)]})},{key:`metrics`,header:`Metrics`,className:`num`,sort:e=>Ia(e).length,cell:e=>Ia(e).length},{key:`note`,header:`Note`,sort:e=>e.note??``,tdProps:e=>({"data-state":e.note?.trim()?void 0:`none`}),cell:e=>(0,$.jsx)(`span`,{className:`ds-note`,children:e.note})},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${e.item} actions`,actions:[{label:`Edit…`,icon:_.edit,onSelect:()=>t(e.item)},{label:`Remove`,icon:_.remove,destructive:!0,onSelect:()=>n(e.item)}]})}]}var Ia=e=>(e.metrics??[]).filter(e=>e.weight!==0),La={bounds:`Set count`,addTerm:`Add term`,removeTerm:`Remove term`,todo:`Mark To do`,graded:`Mark graded`},Ra=[{value:`keep`,label:`Keep`},{value:`set`,label:`Set`},{value:`clear`,label:`Clear`}],za=e=>/^\s*\d+\s*$/.test(e)?Number(e):null;function Ba({kind:e,cases:t,keys:n,onApply:r,onClose:i}){let[a,o]=(0,Z.useState)(`keep`),[s,c]=(0,Z.useState)(`keep`),[l,u]=(0,Z.useState)(``),[d,f]=(0,Z.useState)(``),[p,m]=(0,Z.useState)(`expect`),[h,g]=(0,Z.useState)(``),[_,v]=(0,Z.useState)(null),y=(0,Z.useMemo)(()=>{switch(e){case`bounds`:{let e=a===`set`?za(l):null,t=s===`set`?za(d):null;return a===`set`&&e==null?`Min items needs a whole number.`:s===`set`&&t==null?`Max items needs a whole number.`:{type:`bounds`,min:a===`set`?{kind:`set`,n:e}:{kind:a},max:s===`set`?{kind:`set`,n:t}:{kind:s}}}case`addTerm`:return h.trim()?{type:`addTerm`,list:p,term:h}:`Name the term.`;case`removeTerm`:return h.trim()?{type:`removeTerm`,term:h}:`Name the term.`;case`todo`:return{type:`status`,todo:!0};case`graded`:return{type:`status`,todo:!1}}},[e,a,s,l,d,p,h]),b=(0,Z.useMemo)(()=>{if(typeof y==`string`)return null;let{next:e,changed:r}=fr(t,n,y);return{changed:r,problems:pr(t,e,r)}},[y,t,n]),x=_??b?.problems??null,S=!!x&&x.cases.length>0,C=b?.changed.length??0,w=(0,Z.useCallback)(()=>{if(typeof y==`string`)return;let e=r(y);e&&v(e)},[y,r]),T=e=>t=>{v(null),e(t)},E=[];return e===`bounds`&&(E.push({key:`min`,label:`Min items`,required:!0,control:{type:`select`,value:a,options:Ra,onChange:T(e=>o(e))}}),a===`set`&&E.push({key:`minN`,label:`Min`,required:!0,control:{type:`text`,value:l,size:`num`,onChange:T(u)}}),E.push({key:`max`,label:`Max items`,required:!0,control:{type:`select`,value:s,options:Ra,onChange:T(e=>c(e))}}),s===`set`&&E.push({key:`maxN`,label:`Max`,required:!0,control:{type:`text`,value:d,size:`num`,onChange:T(f)}})),e===`addTerm`&&E.push({key:`list`,label:`List`,required:!0,control:{type:`select`,value:p,onChange:T(e=>m(e)),options:[`expect`,`forbid`].map(e=>({value:e,label:hr(e)}))}}),(e===`addTerm`||e===`removeTerm`)&&E.push({key:`term`,label:`Term`,required:!0,control:{type:`text`,value:h,onChange:T(g)}}),(0,$.jsxs)(Tt,{title:La[e],onClose:i,dirty:a!==`keep`||s!==`keep`||!!l||!!d||!!h,className:`ev-bulk`,footer:(0,$.jsxs)(`div`,{className:`edit-actions`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:i,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,className:`edit-done`,disabled:!C||S,onClick:w,children:`Apply`})]}),children:[E.length>0&&(0,$.jsx)(W,{label:La[e],rows:E}),(0,$.jsx)(`p`,{className:`note ev-changes`,role:`status`,children:typeof y==`string`?y:`Changes ${C} of ${n.size}`}),S&&x&&(0,$.jsx)(`div`,{className:`ev-refusal`,role:`alert`,children:x.cases.length>0&&(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(`p`,{className:`ev-refusal-head`,children:[x.cases.length,` of `,n.size,` would break`]}),(0,$.jsx)(`ul`,{children:x.cases.map(e=>(0,$.jsxs)(`li`,{children:[(0,$.jsx)(`b`,{children:e.file}),` `,e.reasons.join(`; `)]},e.key))})]})})]})}function Va({pageSize:e=20}){let t=Dn(),n=Kn(),r=ye(),{openCaseEditor:i}=Ma(),{list:a}=qn(),{datasetId:o,selected:s,setSelected:c,filters:l,setFilters:u,handedOver:d}=wr(),[f,p]=(0,Z.useState)(null),[m,h]=(0,Z.useState)(!1),g=(0,Z.useCallback)(()=>p(null),[]),v=a?.find(e=>e.id===o)??a?.[0]??null,y=v?.id??``,b=Jn(y||null),S=b?.doc?.body??null,C=S?.cases,w=(0,Z.useMemo)(()=>C?J().gradedSetFrom({cases:C}):[],[C]),T=ka(S?.source?.id),E=(0,Z.useMemo)(()=>new Set(w.map(ar)),[w]),D=(0,Z.useMemo)(()=>new Set([...s].filter(e=>E.has(e))),[s,E]),O=(0,Z.useMemo)(()=>xr(w,l,D,T),[w,l,D,T]),k=!!S&&!S.source;(0,Z.useEffect)(()=>{k&&l.status===`missing`&&u({status:`all`})},[k,l.status,u]);let A=(0,Z.useCallback)(()=>n.entry(y)?.doc?.body??null,[n,y]),j=(0,Z.useCallback)(e=>{y&&n.edit(y,t=>({...t,cases:e}))},[n,y]),M=e=>{i(e,{dataset:y,files:O.map(e=>e.item),onRemove:P})},N=e=>{h(!1),i(e,{dataset:y,files:[...w.map(e=>e.item),e],onRemove:P})};function P(e){let t=A()?.cases??[];j(Oa({cases:t},e)),r.offer({label:`Removed ${e}`,restore:()=>j(t)})}let F=e=>{let i=A();if(!y||!i)return null;let{next:a,changed:o}=fr(i.cases,D,e);if(!o.length)return p(null),null;if(e.type!==`delete`){let e=pr(i.cases,a,o);if(e.cases.length)return e}Hn(t,y,i,`bulk`);let s=i.cases;if(n.edit(y,e=>({...e,cases:a})),e.type===`delete`){let e=new Set(o);c(t=>new Set([...t].filter(t=>!e.has(t))))}return r.offer({label:_r(e,o.length),restore:()=>n.edit(y,e=>({...e,cases:s}))}),p(null),null},I=D.size===0,L=[{label:`Set count…`,icon:x,disabled:I,onSelect:()=>p(`bounds`)},{label:`Add term…`,icon:_.add,disabled:I,onSelect:()=>p(`addTerm`)},{label:`Remove term…`,icon:_n,disabled:I,onSelect:()=>p(`removeTerm`)},{label:`Mark To do…`,icon:Qt,disabled:I,onSelect:()=>p(`todo`)},{label:`Mark graded…`,icon:tn,disabled:I,onSelect:()=>p(`graded`)},{label:`Delete`,icon:_.remove,destructive:!0,disabled:I,onSelect:()=>void F({type:`delete`})}];return(0,$.jsxs)(`div`,{className:`cases`,children:[a?.length?(0,$.jsx)(Na,{rows:w,shown:O,filters:l,onFilters:u,selected:D,onSelect:c,selectionActions:L,sourceFiles:T,hasSource:!!S?.source,loading:!S,loadError:b?.sync.state===`missing`?b.sync.message:null,onRetry:()=>{y&&n.load(y)},pageSize:e,onEdit:M,onAdd:()=>h(!0),onRemove:P,children:d&&v&&(0,$.jsxs)(`p`,{className:`note ev-from`,role:`status`,children:[(0,$.jsxs)(`span`,{children:[`From `,d.from]}),d.found<d.wanted&&(0,$.jsxs)(`span`,{children:[d.found,` of `,d.wanted,` are in `,v.name,` now`]})]})}):null,m&&S?.source&&T&&(0,$.jsx)(Sa,{title:`Add case`,action:`Add case`,source:S.source,files:T,taken:new Set(w.map(e=>e.item)),onPick:N,onClose:()=>h(!1)}),f&&S&&(0,$.jsx)(Ba,{kind:f,cases:S.cases,keys:D,onApply:F,onClose:g}),(0,$.jsx)(ut,{undo:r.undo,onUndo:r.undoNow,onDismiss:r.dismiss})]})}function Ha({groupId:e,undo:t}){let n=Q(),r=Dn(),i=Kn(),{list:a,error:s}=qn(),{pickDataset:c}=wr(),[l,u]=(0,Z.useState)(!1),[d,f]=(0,Z.useState)(!1),[p,m]=(0,Z.useState)(null),[h,g]=(0,Z.useState)(!1),[v,y]=(0,Z.useState)(!1),[b,x]=Se(`dataset-cases`,20),[S,C]=(0,Z.useState)(!1),[w,T]=(0,Z.useState)(null),E=(0,Z.useRef)(null),[O]=On(`promptlab.versions`),[k]=On(`promptlab.profiles`),A=e=>O.profile?.[e]?.[0]?.at??null,[j,N]=(0,Z.useState)(null);(0,Z.useEffect)(()=>{let e=!0;return n.sources().then(t=>{e&&N(t.sources)}).catch(()=>{e&&N([])}),()=>{e=!1}},[n]);let P=Jn(e)?.doc??null,F=P?.body??null,I=a?.find(t=>t.id===e)??null;(0,Z.useEffect)(()=>{c(e)},[e,c]),(0,Z.useEffect)(()=>{a&&!I&&sa()},[a,I]);let L=(0,Z.useMemo)(()=>e?zn(r,e):[],[r,e,O]),R=e=>m({text:e instanceof Error?e.message:String(e),bad:!0}),z=(0,Z.useCallback)(()=>i.entry(e)?.doc?.body??null,[i,e]),B=(0,Z.useCallback)(t=>i.edit(e,t),[i,e]),ee=()=>new Set((i.list().list??[]).map(e=>e.name)),te=async()=>{m(null);try{ca((await i.create(Wa(`Untitled eval group`,ee()))).id)}catch(e){R(e)}},V=t=>{let n=t.trim();P&&n&&n!==P.name&&(m(null),i.rename(e,n).catch(e=>{e instanceof Gt&&e.status===409?m({text:`An eval group is already called ${n}.`,bad:!0}):R(e)}))},U=async()=>{if(P){m(null);try{await i.flush(e),ca((await i.create(Wa(`${P.name} copy`,ee()),structuredClone(P.body))).id)}catch(e){R(e)}}},G=async()=>{if(!P)return;m(null);let{name:n}=P;try{await i.flush(e);let r=await i.remove(e);sa(),t.offer({label:`Deleted ${n}`,restore:()=>{i.restore(r).then(e=>{ca(e.id),e.name!==n&&m({text:`Restored as ${e.name}.`,bad:!1})}).catch(R)}})}catch(e){R(e)}},ne=async e=>{m(null);let t;try{t=await Ua(e)}catch(e){R(e);return}let n;try{n=JSON.parse(t)}catch(e){m({text:`Not JSON: ${e instanceof Error?e.message:String(e)}`,bad:!0});return}T(`import`);try{let e=await i.importFile(n);e[0]&&ca(e[0].id),m({text:`Imported ${e.map(e=>e.name).join(`, `)}.`,bad:!1})}catch(e){R(e)}finally{T(null)}},ie=async()=>{e&&(T(`export`),await i.flush(e).catch(()=>{}),await ae(i.exportUrl(e),e=>ia(`eval-group`,e.group?.name??e.dataset?.name??``,e.version,`json`)),T(null))},ae=async(e,t)=>{try{let n=await fetch(e);if(!n.ok)throw Error(`the export answered ${n.status}`);let r=await n.json();aa(new Blob([JSON.stringify(r,null,2)+`
7
+ `],{type:`application/json`}),t(r))}catch(e){R(e)}},oe=n=>{let i=z();i&&(Hn(r,e,i,`restore`),B(()=>structuredClone(n.doc)),y(!1),t.offer({label:`Restored the eval group`,restore:()=>B(()=>i)}))},se=(0,Z.useMemo)(()=>{let e=k.list.find(e=>e.id===k.grader);return e?{id:e.id,name:e.name}:null},[k]),ce=(0,Z.useMemo)(()=>k.list.filter(e=>(J().CONNECTION_TYPES[e.type??``]?.answers??[`prompt`]).includes(`prompt`)),[k.list]),le=e=>{let{mode:t,threshold:n}=e.scoring,r=e.source&&!(j??[]).some(t=>t.id===e.source.id)?e.source:null;return[{key:`source`,label:`Source`,control:{type:`picker`,kind:`Source`,value:e.source?.id??``,usedLabel:`Last changed`,...ga([...r?[{id:r.id,name:`${r.name} (missing)`}]:[],...j??[]],e=>j?.find(t=>t.id===e)?.changed??null,[{id:``,label:`Any`}]),onChange:e=>{let t=j?.find(t=>t.id===e);B(n=>({...n,source:t?{id:t.id,name:t.name}:r&&r.id===e?r:null}))}}},{key:`mode`,label:`Scoring`,control:{type:`select`,value:t,options:[{value:`all`,label:`All must pass`},{value:`weighted`,label:`Weighted points`}],onChange:e=>B(t=>({...t,scoring:{mode:e===`weighted`?`weighted`:`all`,threshold:e===`weighted`?t.scoring.threshold??1:t.scoring.threshold}}))}},...t===`weighted`?[{key:`threshold`,label:`Pass at`,control:{type:`text`,size:`num`,value:n==null?``:String(n),onChange:e=>B(t=>({...t,scoring:{mode:`weighted`,threshold:e.trim()?/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e:null}}))}}]:[],{key:`grader`,label:`Grader`,control:{type:`picker`,kind:`Profile`,value:e.grader?.id??``,usedLabel:`Last changed`,...ga(ce,A,[{id:``,label:se?`Lab (${se.name})`:`None`}]),onChange:e=>{let t=ce.find(t=>t.id===e);B(e=>({...e,grader:t?{id:t.id,name:t.name}:null}))}}}]},ue=e=>t=>B(n=>({...n,[e]:t})),de=e=>(n,r)=>{let i=z()?.[e]??[];ue(e)(n),t.offer({label:r,restore:()=>B(t=>({...t,[e]:i}))})},fe=F?F.cases.filter(e=>!e.todo).length:0,K=[{label:`Rename…`,icon:pe,disabled:!P,onSelect:()=>u(!0)},{label:`Clone`,icon:_.clone,disabled:!P,onSelect:()=>void U()},{label:`Import JSON…`,icon:H,busy:w===`import`,onSelect:()=>E.current?.click()},{label:`Export JSON`,icon:ve,disabled:!P,busy:w===`export`,onSelect:()=>void ie()},{label:`Export all`,icon:cn,disabled:!a?.length,busy:w===`exportAll`,onSelect:()=>{T(`exportAll`),ae(i.exportAllUrl(),e=>ia(`eval-groups`,`all`,e.version,`json`)).finally(()=>T(null))}},{label:`Restore…`,icon:ke,disabled:!L.length||!P,onSelect:()=>y(!0)},{label:`Edit raw…`,icon:an,disabled:!P,onSelect:()=>g(!0)},{label:`Delete`,icon:o,destructive:!0,disabled:!P,onSelect:()=>void G()}],me=(0,$.jsx)(`input`,{ref:E,type:`file`,accept:`.json,application/json`,hidden:!0,"aria-label":`Import JSON file`,onChange:e=>{let t=e.target.files?.[0];e.target.value=``,t&&ne(t)}}),he=F?`${F.cases.length} cases, ${fe} graded`:void 0;return(0,$.jsxs)(`div`,{className:`datasets`,children:[(0,$.jsx)(re,{kind:`Eval group`,name:P?.name??I?.name??(a===null?`Loading…`:`None`),onPick:a?.length?()=>f(!0):void 0,meta:he,primary:{label:`New eval group`,icon:D,onSelect:()=>void te()},actions:K}),p&&(0,$.jsx)(`p`,{className:`note${p.bad?` bad`:``}`,role:p.bad?`alert`:`status`,children:p.text}),me,F==null?(0,$.jsx)(M,{label:`Eval group`,error:s,onRetry:()=>void i.refresh()}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(X,{id:`grades`,title:`Grades`,level:2,children:(0,$.jsx)(W,{label:`Grades`,rows:le(F)})}),(0,$.jsx)(X,{id:`every`,title:`Every item`,level:2,note:F.every.length?String(F.every.length):void 0,children:(0,$.jsx)(ma,{label:`Every item`,metrics:F.every,over:`item`,set:ue(`every`),undoable:de(`every`)})}),(0,$.jsx)(X,{id:`run`,title:`Whole run`,level:2,note:F.run.length?String(F.run.length):void 0,children:(0,$.jsx)(ma,{label:`Whole run`,metrics:F.run,over:`run`,set:ue(`run`),undoable:de(`run`)})}),(0,$.jsx)(X,{id:`cases`,title:`Cases`,level:2,note:String(F.cases.length),actions:[{label:`Page size…`,icon:_.pageSize,onSelect:()=>C(!0)}],children:(0,$.jsx)(Va,{pageSize:b})})]}),d&&a&&(0,$.jsx)(Xe,{kind:`Eval group`,value:e,onClose:()=>f(!1),options:a.map(e=>({id:e.id,label:e.name})),onPick:e=>{m(null),ca(e)}}),l&&P&&(0,$.jsx)(nt,{title:`Rename eval group`,value:P.name,onCommit:V,onClose:()=>u(!1)}),S&&(0,$.jsx)(St,{value:b,onChange:x,onClose:()=>C(!1)}),v&&P&&(0,$.jsx)(Tt,{title:`Restore eval group`,onClose:()=>y(!1),children:(0,$.jsxs)(`div`,{className:`version-list`,children:[L.length===0&&(0,$.jsx)(`p`,{className:`note`,children:`No versions of this eval group yet.`}),L.map((e,t)=>(0,$.jsxs)(`div`,{className:`version-row`,children:[(0,$.jsxs)(`span`,{className:`version-when`,children:[Ka(e.at),`, `,Ga(e.reason)]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>oe(e),children:`Restore`})]},`${e.at}-${t}`))]})}),h&&P&&(0,$.jsx)(_a,{name:P.name,body:P.body,onSave:e=>{B(()=>e),g(!1)},onClose:()=>g(!1)})]})}function Ua(e){return new Promise((t,n)=>{let r=new FileReader;r.onload=()=>t(String(r.result??``)),r.onerror=()=>n(r.error??Error(`The file could not be read.`)),r.readAsText(e)})}var Wa=(e,t)=>{if(!t.has(e))return e;for(let n=2;;n++)if(!t.has(`${e} ${n}`))return`${e} ${n}`},Ga=e=>e===`idle`?`after editing`:e===`restore`?`before a restore`:e===`bulk`?`before a bulk edit`:e===`pack`?`before a pack update`:e,Ka=e=>{let t=new Date(e);return Number.isNaN(t.getTime())?e:t.toLocaleString()};function qa({groupId:e}){let t=ye();return(0,$.jsxs)($.Fragment,{children:[e?(0,$.jsx)(Ha,{groupId:e,undo:t}):(0,$.jsx)(la,{undo:t}),(0,$.jsx)(ut,{undo:t.undo,onUndo:t.undoNow,onDismiss:t.dismiss})]})}var Ja=e=>e,Ya=(e,t)=>JSON.stringify(e)===JSON.stringify(t),Xa=e=>e instanceof Error?e.message:String(e),Za=({body:e,...t})=>t;function Qa({api:e,pause:t=800}){let n=Ja(e),r={list:null,error:null},i=new Map,a=new Set,o=new Map,s=new Map,c=new Set,l=null,u=()=>{l=null,c.forEach(e=>e())},d=(e,t)=>{let n=i.get(e)??{id:e,doc:null,sync:{state:`loading`}};i.set(e,{...n,...t,id:e}),u()},f=e=>{r={list:e,error:null},u()},p=e=>{if(!r.list)return;let t=r.list.findIndex(t=>t.id===e.id);f(t<0?[...r.list,e]:r.list.map((n,r)=>r===t?e:n))},m=e=>{let t=o.get(e);t&&clearTimeout(t),o.delete(e),a.delete(e),i.delete(e),r.list?f(r.list.filter(t=>t.id!==e)):u()},h=e=>{d(e.id,{doc:e,sync:{state:`saved`}}),p(Za(e))};async function g(e){let t=i.get(e);if(!t?.doc||!a.has(e)||t.sync.state===`conflict`)return;let{body:r,version:o}=t.doc;d(e,{sync:{state:`saving`}});try{let t=await n.saveFilterSet(e,o,r),s=i.get(e);if(!s?.doc)return;s.doc.body===r&&a.delete(e);let c={...s.doc,version:t.version,filters:r.filters.length};if(d(e,{doc:c,sync:{state:`saved`}}),p(Za(c)),a.has(e))return g(e)}catch(t){let n=i.get(e);if(!n?.doc)return;let r=t instanceof Gt&&t.status===409?t.body?.current??null:null;if(r){if(Ya(r.body,n.doc.body)){a.delete(e),d(e,{doc:{...n.doc,version:r.version},sync:{state:`saved`}});return}d(e,{sync:{state:`conflict`,theirs:r}});return}d(e,{sync:{state:`offline`,message:Xa(t)}})}}function _(e){let t=o.get(e);t&&(clearTimeout(t),o.delete(e));let n=s.get(e);return n||(n=g(e).finally(()=>s.delete(e)),s.set(e,n)),n}return{list:()=>r,entry:e=>i.get(e),held:()=>l??=[...i.values()],async refresh(){r.error&&(r={list:r.list,error:null},u());try{let e=(await n.filterSets()).filterSets;f(e);for(let t of e){let e=i.get(t.id);e?.doc&&!a.has(t.id)&&t.version>e.doc.version&&n.filterSet(t.id).then(e=>{i.has(t.id)&&!a.has(t.id)&&h(e)},()=>{})}}catch(e){r={list:r.list,error:Xa(e)},u()}},load(e){i.has(e)&&i.get(e).sync.state!==`missing`||(d(e,{sync:{state:`loading`}}),n.filterSet(e).then(t=>{i.has(e)&&d(e,{doc:t,sync:{state:`saved`}})},t=>{i.has(e)&&d(e,{sync:{state:`missing`,message:Xa(t)}})}))},edit(e,n){let r=i.get(e);if(!r?.doc)return;let s=n(r.doc.body);if(s===r.doc.body)return;d(e,{doc:{...r.doc,body:s,filters:s.filters.length}}),a.add(e);let c=o.get(e);c&&clearTimeout(c),o.set(e,setTimeout(()=>{o.delete(e),_(e)},t))},async flush(e){await Promise.all((e?[e]:[...a]).map(_))},async resolve(e,t){let n=i.get(e);if(!n?.doc||n.sync.state!==`conflict`)return;let r=n.sync.theirs;if(t===`theirs`){a.delete(e),h(r);return}d(e,{doc:{...n.doc,version:r.version},sync:{state:`saved`}}),a.add(e),await _(e)},async create(e,t){let r=await n.createFilterSet(t?{name:e,body:t}:{name:e});return h(r),r},async rename(e,t){let r=await n.renameFilterSet(e,t),a=i.get(e)?.doc;a&&d(e,{doc:{...a,name:r.name,version:r.version,updated:r.updated}}),p({...Za(r),filters:a?.body.filters.length??r.filters})},async remove(e){let{trash:t}=await n.deleteFilterSet(e);return m(e),t},async restore(e){let t=await n.restoreFilterSet(e);return h(t),t},subscribe(e){return c.add(e),()=>{c.delete(e)}}}}var $a=(0,Z.createContext)(null);function eo({children:e,pause:t}){let n=Q(),r=(0,Z.useMemo)(()=>Qa({api:n,...t==null?{}:{pause:t}}),[n,t]);return(0,Z.useEffect)(()=>{let e=()=>{r.flush()};return addEventListener(`pagehide`,e),()=>removeEventListener(`pagehide`,e)},[r]),(0,$.jsx)($a.Provider,{value:r,children:e})}function to(){let e=(0,Z.useContext)($a);if(!e)throw Error(`useFilterSets outside a FilterSetsProvider: the app mounts one at its root`);return e}function no(){let e=to(),t=(0,Z.useSyncExternalStore)(e.subscribe,e.list);return(0,Z.useEffect)(()=>{e.list().list===null&&e.refresh()},[e]),t}function ro(e){let t=to(),n=(0,Z.useSyncExternalStore)(t.subscribe,()=>e?t.entry(e):void 0);return(0,Z.useEffect)(()=>{e&&t.load(e)},[t,e]),n}function io(e){let t=[];for(let n of e.jobs??[])for(let e of n.steps??[]){let n=e.filterSets;if(Array.isArray(n))for(let e of n)e&&typeof e==`object`&&t.push(e)}return t}var ao=`#/library/filters`,oo=()=>{location.hash=ao},so=e=>{location.hash=`${ao}/${encodeURIComponent(e)}`};function co({undo:e}){let t=to(),{list:n,error:r}=no(),[i]=On(`promptlab.workflows`),[a,o]=Se(`filter-sets`,20),[s,c]=(0,Z.useState)(!1),[l,u]=(0,Z.useState)(null),d=e=>u(e instanceof Error?e.message:String(e)),f=(0,Z.useMemo)(()=>{let e=new Map;for(let t of i.list)for(let n of io(t.work)){let r=n.set?.id;if(!r)continue;let i=e.get(r)??{names:[],pinned:!1};i.names.includes(t.name)||i.names.push(t.name),typeof n.pin==`number`&&(i.pinned=!0),e.set(r,i)}return e},[i.list]),p=(n??[]).map(e=>({...e,links:f.get(e.id)??{names:[],pinned:!1}})),m=()=>new Set((t.list().list??[]).map(e=>e.name)),h=async()=>{u(null);try{so((await t.create(lo(`Untitled filter set`,m()))).id)}catch(e){d(e)}},g=async n=>{u(null);try{await t.flush(n.id);let r=await t.remove(n.id);e.offer({label:`Deleted ${n.name}`,restore:()=>{t.restore(r).catch(d)}})}catch(e){d(e)}},v=e=>[{label:`Open`,icon:ht,onSelect:()=>so(e.id)},{label:`Delete`,icon:_.remove,destructive:!0,onSelect:()=>void g(e)}],y=[{key:`set`,header:`Filter set`,role:`title`,sort:e=>e.name.toLowerCase(),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`eg-open`,onClick:()=>so(e.id),children:e.name})},{key:`filters`,header:`Filters`,className:`num`,sort:e=>e.filters,cell:e=>e.filters},{key:`linked`,header:`Linked by`,sort:e=>e.links.names.length,tdProps:e=>({"data-state":e.links.names.length?void 0:`none`}),cell:e=>e.links.names.length?(0,$.jsx)(`span`,{title:e.links.names.join(`, `),children:`${e.links.names.length} ${e.links.names.length===1?`pipeline`:`pipelines`}${e.links.pinned?`, pinned`:``}`}):`—`},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${e.name} actions`,actions:v(e)})}],b=[{label:`Page size…`,icon:_.pageSize,onSelect:()=>c(!0)}];return(0,$.jsxs)(X,{id:`filter-sets`,title:`Filter sets`,note:n?String(n.length):void 0,actions:b,about:`Item filters defined once and linked by the pipelines whose list replies they clean.`,children:[n===null?(0,$.jsx)(M,{label:`Filter sets`,error:r,onRetry:()=>void t.refresh()}):n.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No filter sets yet.`}):(0,$.jsx)(q,{className:`eg-groups`,cards:!0,label:`Filter sets`,rows:p,rowKey:e=>e.id,pageSize:a,columns:y}),l&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:l}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost eg-add`,onClick:()=>void h(),children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),`Add filter set`]}),s&&(0,$.jsx)(St,{value:a,onChange:o,onClose:()=>c(!1)})]})}var lo=(e,t)=>{if(!t.has(e))return e;for(let n=2;;n++)if(!t.has(`${e} ${n}`))return`${e} ${n}`},uo=[{id:`terms`,scope:`item`,keys:[`terms`],label:`is one of these words`,control:`chips`},{id:`regex`,scope:`item`,keys:[`regex`],label:`matches a pattern`,control:`regex`},{id:`shorter`,scope:`item`,keys:[`length`],label:`is shorter than`,control:`num`,dir:`below`,unit:`characters`},{id:`longer`,scope:`item`,keys:[`length`],label:`is longer than`,control:`num`,dir:`above`,unit:`characters`},{id:`fewerWords`,scope:`item`,keys:[`words`],label:`has fewer words than`,control:`num`,dir:`below`,unit:`words`},{id:`moreWords`,scope:`item`,keys:[`words`],label:`has more words than`,control:`num`,dir:`above`,unit:`words`},{id:`blank`,scope:`answer`,keys:[`blank`],label:`it is blank`,control:`blank`},{id:`fewerItems`,scope:`answer`,keys:[`itemCount`],label:`it has fewer items than`,control:`num`,dir:`below`,unit:`items`},{id:`moreItems`,scope:`answer`,keys:[`itemCount`],label:`it has more items than`,control:`num`,dir:`above`,unit:`items`},{id:`share`,scope:`answer`,keys:[`share`,`items`],label:`too many items are one of these words`,control:`share`}],fo=e=>uo.filter(t=>t.scope===e),po=e=>!!e&&typeof e==`object`&&!Array.isArray(e);function mo(e,t,n){let r=fo(e).filter(e=>e.keys[0]===t);return r.length===0?null:r.length===1?r[0]:(po(n)&&n.above!=null?r.find(e=>e.dir===`above`):r.find(e=>e.dir===`below`))??r[0]}function ho(e,t){let n=[],r=new Set;for(let i of fo(t)){let a=i.keys[0];if(r.has(a)||e[a]==null)continue;let o=mo(t,a,e[a]);o&&(n.push(o),o.keys.forEach(e=>r.add(e)))}return n}function go(e,t){let n={...e};for(let e of t.keys)delete n[e];switch(t.control){case`chips`:n.terms=[];break;case`regex`:n.regex=``;break;case`num`:n[t.keys[0]]={[t.dir]:``};break;case`blank`:n.blank=!0;break;case`share`:n.share={above:``},n.items={terms:[]}}return n}function _o(e,t){return fo(t).find(t=>t.keys.every(t=>e[t]==null))??null}function vo(e,t){let n=_o(e,t);return n?go(e,n):e}function yo(e,t){let n={...e};for(let e of t.keys)delete n[e];return n}function bo(e,t,n){return go(yo(e,t),n)}function xo(e,t,n){let r=n.keys[0];return fo(t).filter(t=>t.keys[0]===r||t.keys.every(t=>e[t]==null))}var So=e=>e==null||e===``?``:String(e),Co=e=>/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e.trim(),wo=e=>Array.isArray(e.terms)?e.terms.map(String):[],To=(e,t)=>({...e,terms:t}),Eo=e=>typeof e.regex==`string`?e.regex:``,Do=(e,t)=>({...e,regex:t}),Oo=(e,t)=>So(po(e[t.keys[0]])?e[t.keys[0]][t.dir]:void 0),ko=(e,t,n)=>({...e,[t.keys[0]]:{[t.dir]:Co(n)}}),Ao=e=>({pct:So(po(e.share)&&e.share.above!=null&&e.share.above!==``?Math.round(Number(e.share.above)*100):``),words:po(e.items)&&Array.isArray(e.items.terms)?e.items.terms.map(String):[]}),jo=(e,t)=>({...e,share:{above:/^\d+(\.\d+)?$/.test(t.trim())?Number(t)/100:t.trim()}}),Mo=(e,t)=>({...e,items:{terms:t}});function No(e,t){let n=fo(t===`reject-reply`?`answer`:`item`)[0];return{id:e,type:t,match:go({},n),options:{ignoreFailures:!1,repeat:1},tests:[]}}function Po({scope:e,match:t,onChange:n}){let r=ho(t,e),i=e===`item`?`Drop an item when it`:`Reject the whole reply when`,a=e===`item`?`and it`:`and`;return(0,$.jsxs)(`div`,{className:`cond-builder`,children:[r.map((r,o)=>(0,$.jsxs)(`div`,{className:`cond-row`,"data-control":r.control,children:[(0,$.jsxs)(`div`,{className:`cond-line`,children:[(0,$.jsx)(`span`,{className:`cond-prefix`,children:o===0?i:a}),(0,$.jsx)(`select`,{className:`cond-pick`,"aria-label":`Condition`,value:r.id,onChange:i=>{let a=xo(t,e,r).find(e=>e.id===i.target.value);a&&n(bo(t,r,a))},children:xo(t,e,r).map(e=>(0,$.jsx)(`option`,{value:e.id,children:e.label},e.id))}),r.control===`num`&&(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`input`,{type:`text`,inputMode:`numeric`,className:`cond-num`,"aria-label":r.label,value:Oo(t,r),onChange:e=>n(ko(t,r,e.target.value))}),r.unit&&(0,$.jsx)(`span`,{className:`cond-unit`,children:r.unit})]}),r.control===`regex`&&(0,$.jsx)(`input`,{type:`text`,className:`cond-regex`,"aria-label":`Pattern`,placeholder:`a regular expression`,spellCheck:!1,value:Eo(t),onChange:e=>n(Do(t,e.target.value))}),(0,$.jsx)(`button`,{type:`button`,className:`cond-x`,"aria-label":`Remove condition ${o+1}`,onClick:()=>n(yo(t,r)),children:(0,$.jsx)(k,{size:14,"aria-hidden":`true`})})]}),r.control===`chips`&&(0,$.jsx)(at,{value:wo(t),onChange:e=>n(To(t,e))}),r.control===`share`&&(0,$.jsxs)(`div`,{className:`cond-share`,children:[(0,$.jsx)(`input`,{type:`text`,inputMode:`numeric`,className:`cond-num`,"aria-label":`Share, percent`,value:Ao(t).pct,onChange:e=>n(jo(t,e.target.value))}),(0,$.jsx)(`span`,{className:`cond-unit`,children:`% or more of items are one of these words`}),(0,$.jsx)(at,{value:Ao(t).words,onChange:e=>n(Mo(t,e))})]})]},r.id)),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn cond-add`,disabled:!_o(t,e),onClick:()=>n(vo(t,e)),children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),`Add condition`]})]})}var Fo={comma:`Comma`,lines:`New line`,semicolon:`Semicolon`,none:`None (whole reply)`},Io=(e,t)=>JSON.stringify(e)===JSON.stringify(t),Lo=e=>/^\d+$/.test(e.trim())?Number(e):e.trim();function Ro({filter:e,isNew:t,others:n,onSave:r,onClose:i}){let a=J(),o=a.FILTER_TYPES[e.type],s=o?.scope??`item`,c=o?.label??e.type,[l,u]=(0,Z.useState)(e.id),[d,f]=(0,Z.useState)(e.why??``),[p,m]=(0,Z.useState)(()=>structuredClone(e.match??{})),[h,g]=(0,Z.useState)(!!e.options?.ignoreFailures),[_,v]=(0,Z.useState)(String(e.options?.repeat??1)),[y,b]=(0,Z.useState)(()=>structuredClone(e.tests??[])),[x,S]=(0,Z.useState)(null),C=()=>S(null),w=(0,Z.useMemo)(()=>({id:l.trim(),type:e.type,...d.trim()?{why:d.trim()}:{},match:p,options:{ignoreFailures:h,repeat:Lo(_)},tests:y}),[l,d,p,h,_,y,e.type]),T=a.tryFilter(w),E=(e,t)=>b(n=>n.map((n,r)=>r===e?{...n,...t}:n)),O=()=>{let e=w,t=[...e.id?[]:[`A filter needs a name.`],...n.some(t=>t.id===e.id)?[`A filter is already called ${e.id}.`]:[],...a.filterProblems([e],`This filter`),...T.flatMap((e,t)=>e.ok?[]:[`Test ${t+1} does not come out as expected.`])];if(t.length)return S(t);r(e)},A=[{key:`filter`,rows:[{key:`id`,label:`Name`,required:!0,control:{type:`text`,value:l,onChange:e=>{u(e),C()}}},{key:`type`,label:`Type`,control:{type:`static`,text:c},hint:`Set when the filter was created — delete and recreate to change it.`},{key:`why`,label:`Comments`,control:{type:`textarea`,value:d,onChange:f}}]},{key:`match`,title:`Match`,children:(0,$.jsx)(Po,{scope:s,match:p,onChange:e=>{m(e),C()}})},{key:`options`,title:`Options`,rows:[{key:`ignore`,label:`Ignore failures`,control:{type:`checkbox`,checked:h,label:`Keep going if this filter errors`,onChange:e=>{g(e),C()}}},{key:`repeat`,label:`Repeat`,hint:`Passes over the reply.`,control:{type:`text`,size:`num`,value:_,onChange:e=>{v(e),C()}}}]},{key:`tests`,title:`Tests`,children:(0,$.jsxs)(`div`,{className:`filter-tests`,children:[(0,$.jsx)(`p`,{className:`note`,children:`A sample reply, and what the filter should leave.`}),y.map((e,t)=>{let n=T[t],r=!!n?.ok;return(0,$.jsxs)(`div`,{className:`filter-test`,children:[(0,$.jsx)(W,{label:`Test ${t+1}`,rows:[{key:`sep`,label:`Separator`,hint:`Splits a sample into items.`,control:{type:`select`,value:e.separator,options:a.FILTER_SEPARATORS.map(e=>({value:e,label:Fo[e]??e})),onChange:e=>{E(t,{separator:e}),C()}}},{key:`in`,label:`Sample input`,control:{type:`textarea`,value:e.sampleInput,onChange:e=>{E(t,{sampleInput:e}),C()}}},{key:`out`,label:`Expected output`,control:{type:`textarea`,value:e.expectedOutput,onChange:e=>{E(t,{expectedOutput:e}),C()}}}]}),(0,$.jsxs)(`div`,{className:`filter-test-result`,children:[(0,$.jsx)(`span`,{className:`filter-test-verdict ${r?`hit`:`fault`}`,role:`status`,children:zo(s,r,n?.got??``,e)}),(0,$.jsx)(`button`,{type:`button`,className:`icon-btn`,"aria-label":`Remove test ${t+1}`,onClick:()=>{b(e=>e.filter((e,n)=>n!==t)),C()},children:(0,$.jsx)(k,{size:14,"aria-hidden":`true`})})]})]},t)}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>{b(e=>[...e,{separator:`comma`,sampleInput:``,expectedOutput:``}]),C()},children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),`Add test`]})]})}];return(0,$.jsx)(Ot,{title:t?`New filter`:e.id,...x?.length?{status:x.join(` `),statusBad:!0}:{},sections:A,onSave:O,dirty:!Io(w,e),focusRow:`id`,wide:!0,onClose:i})}function zo(e,t,n,r){if(e===`answer`)return`${t?`Pass`:`Fail`} — ${n===`rejected`?`rejected`:`kept`}`;let i=J().splitSample(r.expectedOutput,r.separator);return t?`Pass — ${i.length} of ${i.length} items as expected`:`Fail — kept ${n||`nothing`}`}var Bo=e=>e===`item`?`Remove items from a list reply that match the conditions.`:`Reject the whole reply when it matches the conditions.`;function Vo({onPick:e,onClose:t}){let n=Object.entries(J().FILTER_TYPES),[r,i]=(0,Z.useState)(n[0]?.[0]??``);return(0,$.jsxs)(Tt,{title:`New filter`,onClose:t,className:`new-filter`,footer:(0,$.jsxs)(`div`,{className:`edit-actions`,children:[(0,$.jsx)(`button`,{type:`button`,className:`edit-done ghost`,onClick:t,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,className:`edit-save`,disabled:!r,onClick:()=>r&&e(r),children:`Continue`})]}),children:[(0,$.jsx)(`p`,{className:`note`,children:`Pick a type. It is fixed once created — delete and recreate to change it.`}),(0,$.jsxs)(`fieldset`,{className:`nf-types`,children:[(0,$.jsx)(`legend`,{className:`sr-only`,children:`Filter type`}),n.map(([e,t])=>(0,$.jsxs)(`label`,{className:`nf-type${e===r?` on`:``}`,children:[(0,$.jsx)(`input`,{type:`radio`,name:`filter-type`,value:e,checked:e===r,onChange:()=>i(e)}),(0,$.jsxs)(`span`,{className:`nf-type-text`,children:[(0,$.jsx)(`span`,{className:`nf-type-name`,children:t.label}),(0,$.jsx)(`span`,{className:`nf-type-about`,children:Bo(t.scope)})]})]},e))]})]})}function Ho({label:e,filters:t,onChange:n,onRemoved:r}){let[i,a]=(0,Z.useState)(null),[o,s]=(0,Z.useState)(!1),[c,l]=(0,Z.useState)(null),u=[...t],d=(e,t)=>{let r=[...u],[i]=r.splice(e,1);r.splice(e+t,0,i),n(r)},f=[{key:`id`,header:`Filter`,role:`title`,sort:e=>e.id.toLowerCase(),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`ds-file`,onClick:()=>a(u.indexOf(e)),children:e.id})},{key:`examples`,header:`Examples`,sort:e=>{let t=J().tryFilter(e);return t.length?t.filter(e=>e.ok).length/t.length:null},cell:e=>{let t=J().tryFilter(e),n=t.filter(e=>e.ok).length;return t.length?(0,$.jsxs)(`span`,{className:n<t.length?`ds-nomatch`:void 0,children:[n,` of `,t.length,` pass`]}):(0,$.jsx)(`span`,{className:`ds-nomatch`,children:`None`})}},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>{let t=u.indexOf(e),i=[{label:`Edit…`,icon:_.edit,onSelect:()=>a(t)},{label:`Clone`,icon:_.clone,onSelect:()=>n([...u.slice(0,t+1),{...structuredClone(e),id:Uo(u,`${e.id} copy`)},...u.slice(t+1)])},{label:`Move up`,icon:_.moveUp,disabled:t===0,onSelect:()=>d(t,-1)},{label:`Move down`,icon:_.moveDown,disabled:t===u.length-1,onSelect:()=>d(t,1)},{label:`Remove`,icon:_.remove,destructive:!0,onSelect:()=>{n(u.filter((e,n)=>n!==t)),r?.(`Removed ${e.id}`,u)}}];return(0,$.jsx)(Ue,{actions:i,label:`${e.id} actions`})}}],p=i==null?void 0:u[i];return(0,$.jsxs)(`div`,{className:`filters-table`,children:[u.length?(0,$.jsx)(q,{className:`rules`,cards:!0,label:e,rows:u,rowKey:e=>e.id,columns:f,defaultSort:null,select:{noun:`filter`,actions:e=>[{label:`Remove`,icon:_.remove,destructive:!0,onSelect:()=>{let t=new Set(e);n(u.filter(e=>!t.has(e))),r?.(e.length===1?`Removed ${e[0].id}`:`Removed ${e.length} filters`,u)}}]}}):(0,$.jsx)(`p`,{className:`note`,children:`No filters.`}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>s(!0),children:[(0,$.jsx)(_.add,{size:14,"aria-hidden":`true`}),`Add filter`]}),o&&(0,$.jsx)(Vo,{onPick:e=>{s(!1),l(No(Uo(u,`New filter`),e))},onClose:()=>s(!1)}),(c||p)&&(0,$.jsx)(Ro,{filter:c??p,isNew:!!c,others:c?u:u.filter((e,t)=>t!==i),onSave:e=>{n(c?[...u,e]:u.map((t,n)=>n===i?e:t)),l(null),a(null)},onClose:()=>{l(null),a(null)}},c?`new`:i)]})}function Uo(e,t){let n=t,r=2;for(;e.some(e=>e.id===n);)n=`${t} ${r++}`;return n}function Wo({setId:e,undo:t}){let n=to(),{list:r,error:i}=no(),[a,s]=(0,Z.useState)(!1),[c,l]=(0,Z.useState)(!1),[u,d]=(0,Z.useState)(null),f=ro(e)?.doc??null,p=f?.body??null,m=r?.find(t=>t.id===e)??null;(0,Z.useEffect)(()=>{r&&!m&&oo()},[r,m]);let h=e=>d({text:e instanceof Error?e.message:String(e),bad:!0}),g=(0,Z.useCallback)(t=>n.edit(e,t),[n,e]),v=()=>new Set((n.list().list??[]).map(e=>e.name)),y=async()=>{d(null);try{so((await n.create(Go(`Untitled filter set`,v()))).id)}catch(e){h(e)}},b=t=>{let r=t.trim();f&&r&&r!==f.name&&(d(null),n.rename(e,r).catch(e=>{e instanceof Gt&&e.status===409?d({text:`A filter set is already called ${r}.`,bad:!0}):h(e)}))},x=async()=>{if(f){d(null);try{await n.flush(e),so((await n.create(Go(`${f.name} copy`,v()),structuredClone(f.body))).id)}catch(e){h(e)}}},S=async()=>{if(!f)return;d(null);let{name:r}=f;try{await n.flush(e);let i=await n.remove(e);oo(),t.offer({label:`Deleted ${r}`,restore:()=>{n.restore(i).then(e=>{so(e.id),e.name!==r&&d({text:`Restored as ${e.name}.`,bad:!1})}).catch(h)}})}catch(e){h(e)}},C=[{label:`Rename…`,icon:_.rename,disabled:!f,onSelect:()=>s(!0)},{label:`Clone`,icon:_.clone,disabled:!f,onSelect:()=>void x()},{label:`Delete`,icon:o,destructive:!0,disabled:!f,onSelect:()=>void S()}],w=p?p.filters.length:0,T=p?`${w} ${w===1?`filter`:`filters`}`:void 0;return(0,$.jsxs)(`div`,{className:`filters-page`,children:[(0,$.jsx)(re,{kind:`Filter set`,name:f?.name??m?.name??(r===null?`Loading…`:`None`),onPick:r?.length?()=>l(!0):void 0,meta:T,primary:{label:`New filter set`,icon:D,onSelect:()=>void y()},actions:C}),u&&(0,$.jsx)(`p`,{className:`note${u.bad?` bad`:``}`,role:u.bad?`alert`:`status`,children:u.text}),p==null?(0,$.jsx)(M,{label:`Filter set`,error:i,onRetry:()=>void n.refresh()}):(0,$.jsx)(X,{id:`filters`,title:`Filters`,level:2,note:w?String(w):void 0,about:`Change a filter here; every run that follows latest grades against it. Filters run top to bottom; the first that matches wins.`,children:(0,$.jsx)(Ho,{label:`Filters`,filters:p.filters,onChange:e=>g(t=>({...t,filters:e})),onRemoved:(e,n)=>t.offer({label:e,restore:()=>g(e=>({...e,filters:[...n]}))})})}),c&&r&&(0,$.jsx)(Xe,{kind:`Filter set`,value:e,onClose:()=>l(!1),options:r.map(e=>({id:e.id,label:e.name})),onPick:e=>{d(null),so(e)}}),a&&f&&(0,$.jsx)(nt,{title:`Rename filter set`,value:f.name,onCommit:b,onClose:()=>s(!1)})]})}var Go=(e,t)=>{if(!t.has(e))return e;for(let n=2;;n++)if(!t.has(`${e} ${n}`))return`${e} ${n}`};function Ko({setId:e}){let t=ye();return(0,$.jsxs)($.Fragment,{children:[e?(0,$.jsx)(Wo,{setId:e,undo:t}):(0,$.jsx)(co,{undo:t}),(0,$.jsx)(ut,{undo:t.undo,onUndo:t.undoNow,onDismiss:t.dismiss})]})}var qo=[{value:`all`,label:`All`},{value:`used`,label:`Used`},{value:`unused`,label:`Unused`}],Jo=[{value:`all`,label:`All`},{value:`scenario`,label:`Used by this target`},{value:`scenarios`,label:`Used by all targets`}];function Yo(e,t,n){let r=t.trim().toLowerCase();return r&&!e.name.toLowerCase().includes(r)&&!e.text.toLowerCase().includes(r)?!1:n===`all`||n===`used`==e.useCount>0}function Xo(e,t,n,r){let i=t.trim().toLowerCase();return i&&!e.name.toLowerCase().includes(i)&&!e.text.toLowerCase().includes(i)?!1:n===`scenario`?r.byScenario.has(e.id):n!==`scenarios`||r.byAll.has(e.id)}var Zo=e=>{let t=new Date(e);return Number.isNaN(t.getTime())?e:t.toLocaleString()};function Qo(e){return e.id===null?(0,$.jsx)($o,{onClose:e.onClose}):(0,$.jsx)(es,{...e,id:e.id})}function $o({onClose:e}){let t=nr(),[n,r]=(0,Z.useState)(``),[i,a]=(0,Z.useState)(``),[o,s]=(0,Z.useState)(null),[c,l]=(0,Z.useState)(!1),u=async()=>{l(!0);try{await t.create({name:n,text:i}),e()}catch(e){s(e instanceof Error?e.message:String(e)),l(!1)}};return(0,$.jsx)(Ot,{title:`New prompt`,status:o??void 0,statusBad:!!o,focusRow:`text`,wide:!0,sections:[{key:`prompt`,rows:[{key:`name`,label:`Name`,control:{type:`text`,value:n,placeholder:`First line`,onChange:r}},{key:`text`,label:`Prompt`,required:!0,control:{type:`textarea`,value:i,placeholder:`None`,onChange:a}}]}],dirty:!!n||!!i,saving:c,onSave:()=>void u(),onClose:e})}function es({id:e,onClose:t,onDuplicate:n,onDelete:r}){let i=nr(),a=ir(e),[o,s]=(0,Z.useState)(null),[c,l]=(0,Z.useState)(!1),{openRun:u}=Dr(),[d,f]=(0,Z.useState)(new Set),[p,m]=(0,Z.useState)(new Set),[h,g]=(0,Z.useState)(null),v=a?.doc;if(!v)return(0,$.jsx)(Ot,{title:`Prompt`,sections:[{key:`loading`,children:(0,$.jsx)(M,{label:`Prompt`,error:a?.sync.state===`missing`?a.sync.message:null,onRetry:()=>i.load(e)})}],onClose:t});let y=(e,t,n)=>{let r=new Set(e);r.has(t)?r.delete(t):r.add(t),n(r)},b=e=>g(e instanceof Error?e.message:String(e)),x=e=>v.versions.find(t=>t.version===e)?.text??``,S=v.uses.some(e=>e.job>0),C=[{key:`version`,header:`Version`,sort:e=>e.version,cell:e=>`v${e.version}${e.version===v.version?` (newest)`:``}`},{key:`created`,header:`Made`,sort:e=>e.created,cell:e=>Zo(e.created)},{key:`uses`,header:`Uses`,className:`num`,sort:e=>e.uses,cell:e=>e.uses},{key:`menu`,header:``,label:``,className:`pl-menu`,role:`menu`,sort:null,cell:t=>(0,$.jsx)(Ue,{label:`v${t.version} actions`,actions:[{label:d.has(t.version)?`Hide words`:`Show words`,icon:lt,onSelect:()=>y(d,t.version,f)},{label:`Restore`,icon:Nt,disabled:t.version===v.version,onSelect:()=>{g(null),i.restoreVersion(e,t.version).catch(b)}}]})}],w=e=>`${e.runId}:${e.scenarioId}:${e.job}`,T=[{key:`run`,header:`Run`,sort:e=>e.submittedAt,cell:e=>(0,$.jsx)(`a`,{className:`pl-run`,href:`#/history`,onClick:n=>{n.preventDefault(),t(),u(e.runId,{at:e.submittedAt,from:`history`})},children:Ur(e.submittedAt)})},{key:`scenario`,header:`Scenario`,sort:e=>e.scenarioName,cell:e=>e.scenarioName},...S?[{key:`job`,header:`Job`,className:`num`,sort:e=>e.job,cell:e=>e.job+1}]:[],{key:`version`,header:`Version`,sort:e=>e.version,cell:e=>`v${e.version}`},{key:`when`,header:`When`,sort:e=>e.submittedAt,cell:e=>Zo(e.submittedAt)},{key:`menu`,header:``,label:``,className:`pl-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${Ur(e.submittedAt)} ${e.scenarioName} actions`,actions:[{label:p.has(w(e))?`Hide words`:`Show words`,icon:lt,onSelect:()=>y(p,w(e),m)}]})}],E=[{label:`Set as default`,icon:wn,disabled:v.default,onSelect:()=>{g(null),i.setDefault(e).catch(b)}},{label:`Duplicate`,icon:_.clone,onSelect:()=>n(v)},{label:`Delete`,icon:_.remove,destructive:!0,disabled:v.default,onSelect:()=>r(v)}],D=o?.name??v.name,O=o?.text??v.text,k=D!==v.name||O!==v.text,A=e=>s({name:D,text:O,...e}),j=async()=>{if(!k){t();return}l(!0),i.edit(e,{name:D,text:O});try{if(await i.flush(e),i.entry(e)?.sync.state===`conflict`){s(null),l(!1);return}t()}catch(e){b(e),l(!1)}},N=h??Xn(a.sync),P=a.sync.state===`conflict`;return(0,$.jsx)(Ot,{title:Yn({...v,name:D,text:O}),status:N??void 0,statusBad:!!N,menu:E,wide:!0,actions:P?[{label:`Take theirs`,onSelect:()=>void i.resolve(e,`theirs`)},{label:`Keep mine`,onSelect:()=>void i.resolve(e,`mine`)}]:[],dirty:k,saving:c,onSave:()=>void j(),onClose:t,sections:[{key:`prompt`,rows:[{key:`name`,label:`Name`,control:{type:`text`,value:D,placeholder:`First line`,onChange:e=>A({name:e})}},{key:`version`,label:`Version`,required:!0,control:{type:`static`,text:`v${v.version}${v.default?` (Default)`:``}`}},{key:`text`,label:`Prompt`,required:!0,control:{type:`textarea`,value:O,placeholder:`None`,onChange:e=>A({text:e})}}]},{key:`versions`,title:`Versions`,children:(0,$.jsx)(q,{className:`pltable pl-versions`,label:`Versions`,rows:v.versions,rowKey:e=>String(e.version),defaultSort:{key:`version`,dir:`desc`},columns:C,after:e=>d.has(e.version)&&(0,$.jsx)(`tr`,{className:`pl-words-row`,children:(0,$.jsx)(`td`,{colSpan:C.length,children:(0,$.jsx)(`pre`,{className:`pl-words`,children:e.text})})})})},{key:`uses`,title:`Uses`,children:v.uses.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No uses yet.`}):(0,$.jsx)(q,{className:`pltable pl-uses`,label:`Uses`,rows:v.uses,rowKey:w,pageSize:10,defaultSort:{key:`when`,dir:`desc`},columns:T,after:e=>p.has(w(e))&&(0,$.jsx)(`tr`,{className:`pl-words-row`,children:(0,$.jsx)(`td`,{colSpan:T.length,children:(0,$.jsx)(`pre`,{className:`pl-words`,children:x(e.version)})})})})}]})}var ts=`#new`;function ns(){let e=nr(),{list:t,error:n}=rr(),r=ye(),[i,a]=Se(`prompts`,20),[o,s]=(0,Z.useState)(!1),[c,l]=(0,Z.useState)(``),[u,d]=(0,Z.useState)(`all`),[f,p]=(0,Z.useState)(null),[m,h]=(0,Z.useState)(null),g=t??[],v=g.filter(e=>Yo(e,c,u)),y=e=>h(e instanceof Error?e.message:String(e)),b=async()=>{h(null),p(ts)},x=async t=>{h(null);try{await e.flush(t.id);let n=await e.create({name:`${Yn(t)} copy`,text:e.entry(t.id)?.doc?.text??t.text});p(n.id)}catch(e){y(e)}},S=async t=>{h(null);let n=[];try{for(let r of t)n.push(await e.remove(r.id)),f===r.id&&p(null)}catch(e){y(e)}n.length&&r.offer({label:t.length===1?`Deleted ${Yn(t[0])}`:`Deleted ${n.length} prompts`,restore:()=>{for(let t of n)e.restore(t).catch(y)}})},C=e=>S([e]),w=t=>{h(null),e.setDefault(t.id).catch(y)},T=e=>[{label:`Edit…`,icon:_.edit,onSelect:()=>p(e.id)},{label:`Set as default`,icon:wn,disabled:e.default,onSelect:()=>w(e)},{label:`Duplicate`,icon:_.clone,onSelect:()=>void x(e)},{label:`Delete`,icon:_.remove,destructive:!0,disabled:e.default,onSelect:()=>void C(e)}];return(0,$.jsxs)(X,{id:`prompts`,title:`Prompt library`,note:t?`${g.length} ${g.length===1?`prompt`:`prompts`}`:void 0,actions:[{label:`Page size…`,icon:_.pageSize,onSelect:()=>s(!0)}],about:`Wordings kept with every version and the runs that used them. The Default is the one a new target starts from.`,children:[t===null?(0,$.jsx)(M,{label:`Prompts`,error:n,onRetry:()=>void e.refresh()}):g.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No prompts yet.`}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(Le,{noun:`prompts`,search:{label:`Search prompts`,placeholder:`Search prompts`,value:c,onChange:l},filters:[{key:`show`,label:`Show`,value:u,onChange:e=>d(e),options:qo}],count:{shown:v.length,total:g.length}}),v.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No prompts match these filters.`}):(0,$.jsx)(q,{className:`pl-prompts`,cards:!0,label:`Prompts`,rows:v,rowKey:e=>e.id,pageSize:i,rowProps:e=>({"data-default":e.default||void 0}),columns:[{key:`prompt`,header:`Prompt`,role:`title`,sort:e=>Yn(e).toLowerCase(),cell:e=>(0,$.jsxs)(`button`,{type:`button`,className:`pl-open`,onClick:()=>p(e.id),children:[(0,$.jsxs)(`span`,{className:`pl-title`,children:[Yn(e),e.default&&(0,$.jsx)(`span`,{className:`pl-default`,children:`Default`})]}),e.name.trim()&&e.text.trim()&&(0,$.jsx)(`span`,{className:`pl-preview`,children:e.text.trim()})]})},{key:`version`,header:`Version`,sort:e=>e.version,cell:e=>`v${e.version}`},{key:`uses`,header:`Uses`,className:`num`,sort:e=>e.useCount,cell:e=>e.useCount},{key:`last`,header:`Last used`,sort:e=>e.lastUsed??null,tdProps:e=>({"data-state":e.lastUsed?void 0:`none`}),cell:e=>e.lastUsed?Zo(e.lastUsed):`—`},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${Yn(e)} actions`,actions:T(e)})}],select:{noun:`prompt`,label:Yn,actions:e=>[{label:`Delete`,icon:_.remove,destructive:!0,disabled:e.every(e=>e.default),onSelect:()=>void S(e.filter(e=>!e.default))}]}})]}),m&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:m}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost pl-add`,onClick:()=>void b(),children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),`Add prompt`]}),f&&(0,$.jsx)(Qo,{id:f===ts?null:f,onClose:()=>p(null),onDuplicate:e=>void x(e),onDelete:e=>void C(e)}),o&&(0,$.jsx)(St,{value:i,onChange:a,onClose:()=>s(!1)}),(0,$.jsx)(ut,{undo:r.undo,onUndo:r.undoNow,onDismiss:r.dismiss})]})}var rs=`evals-lab.sources`,is={view:`thumbs`,sort:`name`,dir:1};function as(){try{let e=window.localStorage.getItem(rs);if(e){let t=JSON.parse(e);if(t.view===`thumbs`||t.view===`list`)return{view:t.view,sort:t.sort===`added`||t.sort===`size`?t.sort:`name`,dir:t.dir===-1?-1:1}}}catch{}return is}function os(e){try{window.localStorage.setItem(rs,JSON.stringify(e))}catch{}}var ss=e=>{let t=e.lastIndexOf(`.`);return t<0?``:e.slice(t).toLowerCase()},cs=[`.heic`,`.heif`,`.dng`,`.tif`,`.tiff`],ls=[`.jpg`,`.jpeg`,`.png`],us=e=>ls.includes(ss(e))||cs.includes(ss(e)),ds=(e,t)=>`/api/sources/${encodeURIComponent(e)}/thumbs/${encodeURIComponent(t.name)}${t.v?`?v=${t.v}`:``}`,fs=(e,t)=>cs.includes(ss(t.name))?`/api/sources/${encodeURIComponent(e)}/zoom/${encodeURIComponent(t.name)}`:`/api/sources/${encodeURIComponent(e)}/files/${encodeURIComponent(t.name)}`;function ps(e,t,n){let r=[...e];return r.sort((e,r)=>{let i;return i=t===`size`?e.bytes-r.bytes:t===`added`?(e.added??0)-(r.added??0):e.name.localeCompare(r.name,void 0,{numeric:!0,sensitivity:`base`}),i*n}),r}function ms(e,t){let n=t.trim().toLowerCase();return n?e.filter(e=>e.name.toLowerCase().includes(n)):[...e]}function hs(e){if(e<1024)return`${e} B`;let t=[`KB`,`MB`,`GB`,`TB`],n=e,r=-1;do n/=1024,r+=1;while(n>=1024&&r<t.length-1);let i=Math.round(n*10)/10;return`${Number.isInteger(i)?i:i.toFixed(1)} ${t[r]}`}function gs(e){return new Date(e*1e3).toLocaleDateString(void 0,{year:`numeric`,month:`short`,day:`numeric`})}function _s(e){let t=e.platform??e.config?.platform;return{id:e.id,name:e.name,system:e.system,type:e.type,...t?{platform:t}:{},files:e.files.length,bytes:e.bytes}}var vs=`[redacted]`,ys=/^(?:api[-_]?key|x-api-key|authorization|ocp-apim-subscription-key|password|secret|client[-_]?secret|token|access[-_]?token|refresh[-_]?token|sig)$/i,bs=/^(?:headers|authentication)$/,xs=[[/sk-ant-[A-Za-z0-9_-]+/g,vs],[/sk-[A-Za-z0-9_-]{20,}/g,vs],[/Bearer\s+[A-Za-z0-9._~+/=-]+/g,`Bearer ${vs}`],[/eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,vs],[/([?&](?:sig|code)=)[^&"\s]+/g,`$1${vs}`]];function Ss(e){let t=0,n=e=>{let n=e;for(let[e,r]of xs)n=n.replace(e,(...e)=>(t++,r.replace(`$1`,e[1]??``)));return n},r=e=>{if(Array.isArray(e))return e.map(r);if(e&&typeof e==`object`){let n={};for(let[i,a]of Object.entries(e))bs.test(i)&&a&&typeof a==`object`&&!Array.isArray(a)?n[i]=Object.fromEntries(Object.keys(a).map(e=>(t++,[e,vs]))):bs.test(i)&&typeof a==`string`||ys.test(i)&&a!=null&&typeof a!=`object`?(t++,n[i]=vs):n[i]=r(a);return n}return typeof e==`string`?n(e):e};return{value:r(e),count:t}}var Cs=[`https://service.flow.microsoft.com//.default`];function ws(e){let t=e.trim();return!t||t.toLowerCase()===`organizations`?`Any organisation`:`${t} only`}var Ts=null;function Es(e){let t=`${e.clientId}/${e.tenant}`;return Ts?.key!==t&&(Ts={key:t,pca:zt(()=>import(`./dist-DI3ewZYj.js`).then(async t=>{let n=await t.createStandardPublicClientApplication({auth:{clientId:e.clientId,authority:`https://login.microsoftonline.com/${e.tenant}`,redirectUri:`${window.location.origin}/`,OIDCOptions:{responseMode:t.ResponseMode.QUERY}},cache:{cacheLocation:`sessionStorage`}}),r=await n.handleRedirectPromise();return r?.account&&n.setActiveAccount(r.account),n}),[])}),Ts.pca}var Ds=()=>{let e=new URLSearchParams(window.location.search);return e.has(`state`)&&(e.has(`code`)||e.has(`error`))};async function Os(e){await Es(e)}async function ks(e){let t=await Es(e);return(t.getActiveAccount()??t.getAllAccounts()[0]??null)?.username??null}var As=`signin`;function js(e=window.location.href){let t=new URL(e);return t.hostname===`127.0.0.1`?(t.hostname=`localhost`,t.searchParams.set(As,`microsoft`),t.href):null}function Ms(){let e=new URL(window.location.href);return e.searchParams.get(`signin`)===`microsoft`&&(e.searchParams.delete(As),window.history.replaceState(window.history.state,``,e.href),!0)}async function Ns(e,t=window.location.href){let n=js();if(n){window.location.assign(n);return}await(await Es(e)).loginRedirect({scopes:Cs,prompt:`select_account`,redirectStartPage:t})}async function Ps(e){let t=await Es(e),n=t.getActiveAccount()??t.getAllAccounts()[0];n&&await t.clearCache({account:n})}async function Fs(e){let t=await Es(e),n=t.getActiveAccount()??t.getAllAccounts()[0];if(!n)throw Error(`sign in to Microsoft first`);try{return(await t.acquireTokenSilent({scopes:Cs,account:n})).accessToken}catch(e){let{InteractionRequiredAuthError:r}=await zt(async()=>{let{InteractionRequiredAuthError:e}=await import(`./dist-DI3ewZYj.js`);return{InteractionRequiredAuthError:e}},[]);throw e instanceof r&&await t.acquireTokenRedirect({scopes:Cs,account:n,redirectStartPage:window.location.href}),e}}var Is=new EventTarget,Ls=()=>Is.dispatchEvent(new Event(`change`));function Rs(){let e=Q(),[t,n]=(0,Z.useState)({cfg:void 0,editable:!1});return(0,Z.useEffect)(()=>{let t=!0,r=()=>e.config().then(e=>{t&&n({cfg:e.microsoft??null,editable:!!e.microsoftEditable})},()=>{t&&n({cfg:null,editable:!1})});return r(),Is.addEventListener(`change`,r),()=>{t=!1,Is.removeEventListener(`change`,r)}},[e]),t}var zs=()=>Rs().cfg,Bs=e=>Dt(()=>Fs(e));async function Vs(e,t,n){let r=await e.flow(t,n);return{name:r.name,definition:Ss(r.definition).value}}async function Hs(e,t,n,r,i){let a=await e.run(t,n,r),o=c(i),s=new Map,l=new Map,u=t=>t?(l.has(t)||l.set(t,e.content(t)),l.get(t)):Promise.resolve(null),d=i=>(s.has(i)||s.set(i,e.repetitions(t,n,r,i)),s.get(i)),f=async(e,t)=>{let n=o[e]?.loops??[];return n.length?(await d(e)).find(e=>n.every(n=>!(n in t)||e.at[n]===t[n]))??null:a.actions[e]??null},p=e=>e.status!==`Skipped`&&(e.inputsLink||e.outputsLink||e.error),m=async e=>({run:r,at:e.at,startTime:e.startTime,inputs:await u(e.inputsLink),outputs:await u(e.outputsLink),error:e.error});return{detail:a,triggerOutputs:()=>u(a.trigger?.outputsLink??null),actionInputs:async(e,t)=>u((await f(e,t))?.inputsLink??null),actionOutputs:async(e,t)=>u((await f(e,t))?.outputsLink??null),callsOf:async e=>{let t=e.loops.length?await d(e.name):[a.actions[e.name]].filter(e=>!!e);return Promise.all(t.filter(p).map(m))}}}async function Us(e,t,n,i,a,o){if(!i.length)return[];let s=(await e.runs(t,n)).slice(0,o).map(e=>e.id);if(!s.length)return[];let c=new Set,l=[];for(let o of i){let{records:i}=await Ws(e,t,n,s,o,a,c);for(let e of i)c.add(r(e)),l.push(e)}return l}async function Ws(e,t,n,i,a,o,s,c){let l=new Set(s),u=[],d=0,f=0;for(let s of i){let i=await Hs(e,t,n,s,o);for(let e of await i.callsOf(a)){let t=Ss(await Ge(a,e,i,o)).value,n=r(t);if(l.has(n)){d++;continue}l.add(n),u.push(t)}c?.(++f)}return{records:u,skipped:d}}var Gs=5;function Ks({onMade:e}){let t=Q(),n=zs(),r=(0,Z.useMemo)(()=>n?Bs(n):null,[n]),[i,a]=(0,Z.useState)(void 0),[o,s]=(0,Z.useState)(null),[c,l]=(0,Z.useState)(``),[u,d]=(0,Z.useState)(null),[f,p]=(0,Z.useState)(``),[m,h]=(0,Z.useState)(``),[g,_]=(0,Z.useState)(!1),[v,y]=(0,Z.useState)(null),b=e=>y(e instanceof Error?e.message:String(e));if((0,Z.useEffect)(()=>{n&&ks(n).then(a,()=>a(null))},[n]),(0,Z.useEffect)(()=>{r&&i&&r.environments().then(e=>{s(e),l((e.find(e=>e.isDefault)??e[0])?.id??``)},b)},[r,i]),(0,Z.useEffect)(()=>{r&&c&&(d(null),p(``),r.flows(c).then(e=>{d(e),p(e[0]?.id??``),h(e[0]?.name??``)},b))},[r,c]),n===void 0||n&&i===void 0)return(0,$.jsx)(M,{label:`Account`});if(n===null)return(0,$.jsx)(`p`,{className:`note`,children:`Microsoft: Not Configured`});if(!i)return(0,$.jsxs)(`div`,{className:`sacts`,children:[(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,onClick:()=>void Ns(n).catch(b),children:[(0,$.jsx)(hn,{size:14,"aria-hidden":`true`}),` Sign in with Microsoft`]}),v&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:v})]});let x=async(e,n,r)=>{try{for(let i of await Us(e,c,f,P(r),r,Gs))try{await t.upload(n,He(i),new Blob([JSON.stringify(i,null,2)],{type:`application/json`}))}catch{}}catch{}},S=async()=>{if(!r||!f||!m.trim())return;_(!0),y(null);let n=null;try{let i=u?.find(e=>e.id===f),a=await t.createSource(m.trim(),`workflow`,{platform:`power-automate`,environment:c,flow:f,displayName:i?.name??f});n=a.id;let o=await Vs(r,c,f);await t.saveDefinition(a.id,0,o.definition),x(r,a.id,o.definition),await e()}catch(e){n&&await t.removeSource(n).catch(()=>{}),b(e instanceof Gt?Error(e.message):e)}finally{_(!1)}};return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(W,{label:`Flow`,rows:[{key:`account`,label:`Account`,control:{type:`static`,text:i}},{key:`env`,label:`Environment`,control:o?{type:`select`,value:c,options:o.map(e=>({value:e.id,label:e.name})),onChange:l}:{type:`static`,text:`Loading…`}},{key:`flow`,label:`Flow`,control:u?{type:`select`,value:f,options:u.map(e=>({value:e.id,label:e.name})),onChange:e=>{p(e),h(u.find(t=>t.id===e)?.name??``)}}:{type:`static`,text:c?`Loading…`:`—`}},{key:`name`,label:`Name`,control:{type:`text`,value:m,onChange:h}}]}),u&&!u.length&&(0,$.jsx)(`p`,{className:`note`,children:`No flows in this environment.`}),v&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:v}),(0,$.jsx)(`div`,{className:`sacts`,children:(0,$.jsx)(`button`,{type:`button`,disabled:g||!f||!m.trim(),onClick:()=>void S(),children:`Add`})})]})}function qs(e,t){let n=e.result;if(!n||!(`status`in n))return null;let r=P(t).find(t=>t.name===e.step);if(!r)return null;let i=String(r.template.uri??``),a={step:r.name,loop:r.loops.at(-1)??null,api:J().httpApiFor(i),readAs:L(t,r.name)?.expression??null};try{return J().httpReplyOf(a,{prompt:``},n.body,n.status,{scope:e.scope,now:e.at}).raw}catch{return null}}function Js(e,t){let n;try{n=JSON.parse(e)}catch{return[{type:`equals`,value:e,json:!1}]}if(!n||typeof n!=`object`||Array.isArray(n))return[{type:`equals`,value:e,json:!0}];let r=n,i=t.length?t.filter(e=>e in r):Object.keys(r).filter(e=>r[e]==null||typeof r[e]!=`object`);return i.length?i.map(e=>({type:`json-field`,path:e,op:`equals`,value:typeof r[e]==`string`?r[e]:JSON.stringify(r[e])})):[{type:`equals`,value:e,json:!0}]}var Ys=e=>e.replace(/\.json$/i,``).replace(/[^A-Za-z0-9._-]/g,`_`);function Xs(e,t){let n=[],r=0;for(let{name:i,record:a}of e){if(qs(a,t)==null){r++;continue}n.push({id:Ys(i),item:i,todo:!1,note:``,metrics:[]})}return{cases:n,skipped:r}}function Zs(e,t){let n=[],r=0;for(let{name:i,record:a}of e){let e=qs(a,t);if(e==null){r++;continue}let o=L(t,a.step),s=o?u(t,o.name):[];n.push({id:i.replace(/\.json$/i,``).replace(/[^A-Za-z0-9._-]/g,`_`),item:i,todo:!0,note:``,metrics:Js(e,s)})}return{cases:n,skipped:r}}var Qs=e=>e.name.toLowerCase().endsWith(`.json`),$s=e=>new Promise((t,n)=>{let r=new FileReader;r.onload=()=>t(String(r.result)),r.onerror=()=>n(r.error),r.readAsText(e)}),ec=`evals-lab.flow.importing`;function tc(e){try{return sessionStorage.getItem(e)}catch{return null}}function nc(e,t){try{t?sessionStorage.setItem(e,t):sessionStorage.removeItem(e)}catch{}}var rc=e=>e instanceof Error?e.message:String(e);function ic({source:e,definition:t,cfg:n}){let a=Q(),s=Kn(),c=ye(),[l,u]=(0,Z.useState)(e.files.filter(Qs)),[d,f]=(0,Z.useState)({}),[m,h]=(0,Z.useState)(null),[g,_]=(0,Z.useState)(()=>{let t=tc(ec)===e.id;return t&&nc(ec,null),t?`import`:null}),[v,y]=(0,Z.useState)(null),b=(0,Z.useMemo)(()=>P(t),[t]),[x,S]=(0,Z.useState)(!1),[C,w]=(0,Z.useState)(null),T=(0,Z.useCallback)(async()=>{let t=(await a.source(e.id)).files.filter(Qs);u(t);let n=await Promise.all(t.map(async t=>{try{return[t.name,JSON.parse(await $s(await a.file(e.id,t.name)))]}catch{return[t.name,null]}}));f(Object.fromEntries(n))},[a,e.id]),E=(0,Z.useCallback)(()=>{w(null),T().then(()=>S(!0),e=>w(rc(e)))},[T]);(0,Z.useEffect)(E,[E]);let O=e=>{let n=d[e],r=n?b.find(e=>e.name===n.step):void 0;return n&&r?p(r,n,t):null},k=(0,Z.useMemo)(()=>new Set(l.flatMap(e=>d[e.name]?[r(d[e.name])]:[])),[l,d]),A=async(t,n=0)=>{let r=0,i=[];for(let n of t)try{await a.upload(e.id,He(n),new Blob([JSON.stringify(n,null,2)],{type:`application/json`})),r++}catch(e){i.push(e instanceof Gt?e.message:rc(e))}await T(),h([`Added ${r}`,n?`Skipped ${n}`:``,i.length?`Refused ${i.length}: ${i[0]}`:``].filter(Boolean).join(`, `))},j=async()=>{let{cases:n,skipped:r}=Zs(l.flatMap(e=>d[e.name]?[{name:e.name,record:d[e.name]}]:[]),t);if(!n.length)return h(`No call holds a production reply.`);try{let t=await s.create(`${e.name} · production`,{...jn(),source:{id:e.id,name:e.name},cases:n});h([`Made ${t.name}: ${n.length} ${n.length===1?`case`:`cases`}`,r?`Skipped ${r}`:``].filter(Boolean).join(`, `))}catch(e){h(rc(e))}},N=async t=>{try{let{trash:n}=await a.removeFiles(e.id,t);u(e=>e.filter(e=>!t.includes(e.name))),c.offer({label:t.length===1?`Removed ${t[0]}`:`Removed ${t.length} calls`,restore:()=>void a.restoreTrash(n).then(T,e=>h(rc(e)))})}catch(e){h(rc(e))}},F=async t=>{let n=URL.createObjectURL(await a.file(e.id,t)),r=document.createElement(`a`);r.href=n,r.download=t,document.body.append(r),r.click(),r.remove(),URL.revokeObjectURL(n)},I=e=>[{label:`View…`,icon:lt,onSelect:()=>y(e.name)},{label:`Download`,icon:ve,onSelect:()=>void F(e.name).catch(e=>h(rc(e)))},{label:`Remove`,icon:o,destructive:!0,onSelect:()=>void N([e.name])}],L=(0,Z.useCallback)(()=>_(null),[]),R=(0,Z.useCallback)(()=>y(null),[]),z=v?d[v]:null;return(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(X,{id:`flow-calls`,title:`Calls`,note:String(l.length),about:`Calls are Power Automate steps that have been run previously.`,actions:[{label:`Import runs`,icon:ke,disabled:!b.length||!n,onSelect:()=>_(`import`)},{label:`Upload…`,icon:H,disabled:!b.length,onSelect:()=>_(`upload`)},{label:`New dataset`,icon:tt,disabled:!l.length,onSelect:()=>void j()}],children:[m&&(0,$.jsx)(`p`,{className:`note`,role:`status`,children:m}),x?l.length?(0,$.jsx)(`div`,{className:`sflow`,children:(0,$.jsx)(q,{className:`slist`,label:`Calls`,rows:l,rowKey:e=>e.name,select:{noun:`call`,actions:e=>[{label:`Download`,icon:ve,onSelect:()=>void(async()=>{for(let t of e)await F(t.name)})().catch(e=>h(rc(e)))},{label:`Remove`,icon:o,destructive:!0,onSelect:()=>void N(e.map(e=>e.name))}]},columns:[{key:`name`,header:`Name`,role:`title`,sort:e=>e.name,cell:e=>(0,$.jsxs)($.Fragment,{children:[e.name,(0,$.jsx)(`span`,{className:`sflow-in`,children:sc(d[e.name])})]})},{key:`result`,header:`Result`,sort:e=>cc(d[e.name]),cell:e=>cc(d[e.name])},{key:`request`,header:`Request`,sort:e=>ac(O(e.name)),tdProps:e=>{let t=O(e.name);return t&&t.match===!1?{title:t.where}:{}},cell:e=>ac(O(e.name))},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(i,{label:`${e.name} actions`,actions:I(e)})}]})}):(0,$.jsx)(`p`,{className:`note`,children:`No calls yet. Import runs, in this group's menu, reads them from the flow's run history.`}):(0,$.jsx)(M,{label:`Calls`,error:C,onRetry:E}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,disabled:!b.length,onClick:()=>_(`hand`),children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),` Add call`]})]}),g===`import`&&n&&(0,$.jsx)(dc,{cfg:n,source:e,steps:b,definition:t,held:k,onClose:L,onRead:async(e,t)=>{_(null),await A(e,t)}}),g===`hand`&&(0,$.jsx)(fc,{steps:b,onClose:L,onAdd:async e=>{_(null),await A([e])}}),g===`upload`&&(0,$.jsx)(mc,{steps:b,onClose:L,onRead:async e=>{_(null),await A(e)}}),v&&(0,$.jsx)(Tt,{title:v,onClose:R,wide:!0,children:(0,$.jsx)(`pre`,{className:`pl-words`,children:z?JSON.stringify(z,null,2):`Unreadable.`})}),(0,$.jsx)(ut,{undo:c.undo,onUndo:c.undoNow,onDismiss:c.dismiss})]})}var ac=e=>!e||e.match===null?`—`:e.match?`In sync`:`Out of sync`,oc={run:`Run`,hand:`Hand`,upload:`Upload`},sc=e=>e?[e.step,oc[e.origin.kind]??e.origin.kind,e.at?gs(Date.parse(e.at)/1e3):``].filter(Boolean).join(`, `):`Unreadable`;function cc(e){let t=e?.result;return t?`error`in t?t.error.code||`Error`:String(t.status):`—`}var lc=e=>e.map(e=>({value:e.name,label:e.name})),uc=e=>e.from===`trigger`?`Trigger body`:wt(e);function dc({cfg:e,source:t,steps:n,definition:r,held:i,onClose:a,onRead:o}){let s=(0,Z.useMemo)(()=>Bs(e),[e]),c=t.config??{},[l,u]=(0,Z.useState)(void 0),[d,f]=(0,Z.useState)(n[0]?.name??``),[p,m]=(0,Z.useState)(null),[h,g]=(0,Z.useState)(``),[_,v]=(0,Z.useState)(new Set),[y,b]=(0,Z.useState)(``),[x,S]=(0,Z.useState)(null),[C,w]=(0,Z.useState)(null),[T,E]=(0,Z.useState)(null),[D,O]=(0,Z.useState)(0);(0,Z.useEffect)(()=>{ks(e).then(u,()=>u(null))},[e]),(0,Z.useEffect)(()=>{l&&c.environment&&c.flow&&(E(null),s.runs(c.environment,c.flow).then(m,e=>E(rc(e))))},[l,s,c.environment,c.flow,D]);let k=(0,Z.useMemo)(()=>[...new Set((p??[]).map(e=>e.status))].sort(),[p]),A=(0,Z.useMemo)(()=>(p??[]).filter(e=>!h||e.status===h),[p,h]),j=e=>v(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n}),N=A.length>0&&A.every(e=>_.has(e.id)),P=A.filter(e=>_.has(e.id)).map(e=>e.id),I=async()=>{let e=n.find(e=>e.name===d);if(!e||!c.environment||!c.flow)return;let t=P;w(null),S(`Reading 0 of ${t.length}`);try{let{records:n,skipped:a}=await Ws(s,c.environment,c.flow,t,e,r,i,e=>S(`Reading ${e} of ${t.length}`));await o(n,a)}catch(e){w(rc(e)),S(null)}};return(0,$.jsxs)(Tt,{title:`Import runs`,onClose:a,wide:!0,dirty:P.length>0,footer:(0,$.jsxs)(`div`,{className:`sacts`,children:[x&&(0,$.jsx)(`span`,{className:`note`,role:`status`,children:x}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:a,children:`Cancel`}),(0,$.jsxs)(`button`,{type:`button`,disabled:!!x||!P.length||!d,"aria-busy":!!x||void 0,onClick:()=>void I(),children:[x&&(0,$.jsx)(F,{}),`Import `,P.length||``]})]}),children:[l===void 0?(0,$.jsx)(M,{label:`Account`}):l?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(W,{label:`Import`,rows:[{key:`step`,label:`Step`,control:{type:`select`,value:d,options:lc(n),onChange:f}},{key:`latest`,label:`Latest`,control:{type:`text`,value:y,size:`num`,placeholder:`N`,onChange:e=>{b(e);let t=Number.parseInt(e,10);t>0&&v(new Set(A.slice(0,t).map(e=>e.id)))}}}]}),p?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(Le,{noun:`runs`,count:{shown:A.length,total:p.length},lead:(0,$.jsx)(`input`,{type:`checkbox`,"aria-label":`Select all shown`,checked:N,disabled:!A.length,onChange:()=>v(N?new Set:new Set(A.map(e=>e.id)))}),filters:[{key:`status`,label:`Status`,value:h,onChange:g,options:[{value:``,label:`All`},...k.map(e=>({value:e,label:e}))]}]}),A.length?(0,$.jsx)(`div`,{className:`sflow`,children:(0,$.jsx)(q,{className:`slist`,label:`Runs`,rows:A,rowKey:e=>e.id,pageSize:20,rowProps:e=>({onClick:()=>j(e.id),"data-selected":_.has(e.id)||void 0}),columns:[{key:`pick`,header:``,sort:null,cell:e=>(0,$.jsx)(`input`,{type:`checkbox`,"aria-label":`Run ${e.id}`,checked:_.has(e.id),onChange:()=>j(e.id),onClick:e=>e.stopPropagation()})},{key:`start`,header:`Started`,sort:e=>e.startTime??``,cell:e=>e.startTime?gs(Date.parse(e.startTime)/1e3):`—`},{key:`status`,header:`Status`,sort:e=>e.status,cell:e=>e.status}]})}):(0,$.jsx)(`p`,{className:`note`,children:`No runs.`})]}):(0,$.jsx)(M,{label:`Runs`,error:T,onRetry:()=>O(e=>e+1)})]}):(0,$.jsx)(`div`,{className:`sacts`,children:(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,onClick:()=>{nc(ec,t.id),Ns(e).catch(e=>{nc(ec,null),w(rc(e))})},children:[(0,$.jsx)(hn,{size:14,"aria-hidden":`true`}),` Sign in with Microsoft`]})}),C&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:C})]})}function fc({steps:e,onClose:t,onAdd:n}){let[r,i]=(0,Z.useState)(e[0]?.name??``),[a,o]=(0,Z.useState)({}),s=e.find(e=>e.name===r),c=(s?.reads??[]).filter(e=>wt(e));return(0,$.jsxs)(Tt,{title:`Add call`,onClose:t,wide:!0,dirty:Object.values(a).some(Boolean),footer:(0,$.jsxs)(`div`,{className:`sacts`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:t,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,disabled:!s,onClick:()=>{if(!s)return;let e=Object.fromEntries(Object.entries(a).filter(([e,t])=>t!==``&&c.some(t=>wt(t)===e))),[t]=j(s,[e]).records;n({...t,origin:{kind:`hand`}})},children:`Add`})]}),children:[(0,$.jsx)(W,{label:`Call`,rows:[{key:`step`,label:`Step`,control:{type:`select`,value:r,options:lc(e),onChange:e=>{i(e),o({})}}},...c.map(e=>({key:`read-${wt(e)}`,label:uc(e),control:{type:`textarea`,value:a[wt(e)]??``,onChange:t=>o(n=>({...n,[wt(e)]:t}))}}))]}),s&&!c.length&&(0,$.jsx)(`p`,{className:`note`,children:`Reads nothing.`})]})}function pc(e,t){if(e.toLowerCase().endsWith(`.csv`))return{rows:ne(t)};let n=JSON.parse(t),r=Array.isArray(n)?n:[n];if(r.length&&r.every(e=>!!e&&typeof e==`object`&&e.format===`evals-lab/record`))return{records:r};if(r.every(e=>e&&typeof e==`object`&&!Array.isArray(e)))return{rows:r};throw Error(`a file of records, or rows of objects`)}function mc({steps:e,onClose:t,onRead:n}){let r=(0,Z.useRef)(null),[i,a]=(0,Z.useState)(e[0]?.name??``),[o,s]=(0,Z.useState)(null),[c,l]=(0,Z.useState)(null),u=e.find(e=>e.name===i),d=o&&u&&`rows`in o.parsed?j(u,o.parsed.rows):null,f=o?`records`in o.parsed?o.parsed.records:d?.records??[]:[],p=async e=>{if(e){l(null);try{s({name:e.name,parsed:pc(e.name,await $s(e))})}catch(t){s(null),l(`${e.name}: ${rc(t)}`)}}};return(0,$.jsxs)(Tt,{title:`Upload`,onClose:t,wide:!0,dirty:!!o,footer:(0,$.jsxs)(`div`,{className:`sacts`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:t,children:`Cancel`}),(0,$.jsxs)(`button`,{type:`button`,disabled:!f.length,onClick:()=>void n(f),children:[`Add `,f.length||``]})]}),children:[(0,$.jsx)(W,{label:`Upload`,rows:[...o&&`records`in o.parsed?[]:[{key:`step`,label:`Step`,control:{type:`select`,value:i,options:lc(e),onChange:a}}],{key:`file`,label:`File`,control:{type:`static`,text:o?.name??`—`}},...o?[{key:`count`,label:`records`in o.parsed?`Calls`:`Rows`,control:{type:`static`,text:String(f.length)}}]:[],...d?.unmatched.length?[{key:`unmatched`,label:`Unmatched`,control:{type:`static`,text:d.unmatched.join(`, `)}}]:[]]}),(0,$.jsxs)(`div`,{className:`sacts`,children:[(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,onClick:()=>r.current?.click(),children:[(0,$.jsx)(H,{size:14,"aria-hidden":`true`}),` Choose file`]}),(0,$.jsx)(`input`,{ref:r,type:`file`,accept:`.json,.csv`,hidden:!0,"aria-hidden":`true`,onChange:e=>{p(e.target.files?.[0]),e.target.value=``}})]}),c&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:c})]})}function hc(e){let t=/^https?:\/\/[^/?#]+(\/[^?#]*)?(?:\?([^#]*))?/i.exec(e);if(!t)return{path:e,query:{}};let n={};for(let e of(t[2]??``).split(`&`).filter(Boolean)){let t=e.indexOf(`=`);n[t<0?e:e.slice(0,t)]=t<0?``:e.slice(t+1)}return{path:t[1]||`/`,query:n}}function gc(e,t){let n=J(),r=t.filter(e=>(n.CONNECTION_TYPES[e.type??``]?.answers??[`prompt`]).includes(`request`)),i=(()=>{try{return new URL(e).host}catch{return null}})(),a=r.find(e=>{try{return new URL(n.connectionBase(e)).host===i}catch{return!1}})??r[0];return a?{id:a.id,name:a.name??``}:{id:`http-endpoint`,name:`HTTP endpoint`}}function _c(e,t){let n=L(e,t.name),r=n?u(e,n.name):[],i=J();return[...n?[{type:`is-json`,schema:n.schema?JSON.stringify(n.schema,null,2):``}]:[],r.length?{type:`fields-equal-recorded`,fields:r.join(`
8
+ `)}:n?{type:`same-as-recorded`}:{type:`judge-vs-production`,...i.METRICS[`judge-vs-production`].defaults()}]}function vc(e,t,n,r,i){let a=J(),o=n.template,s=String(o.uri??``),c=a.httpApiFor(s),l={type:`httpRequest`,api:c,method:String(o.method??`POST`).toUpperCase(),...hc(s),body:o.body??null,...a.HTTP_APIS[c].cellOf(o.body??null)},u=L(t,n.name),d=gc(s,r),f=a.withOut({type:`job`,id:a.newId(),name:n.name,steps:[{type:`attachContent`,content:{type:`source`,ref:e,first:null,loops:1}},{type:`flowStep`,step:n.name,loop:n.loops.at(-1)??null,api:c},...u?.expression?[{type:`readAs`,readAs:u.expression}]:[]]},{kind:`text`,...a.OUTPUT_KINDS.text?.settingDefaults?.()??{},modifiers:a.OUTPUT_KINDS.text?.modifiers?.()??[]});return{version:a.PIPELINE_VERSION,id:a.newId(),name:`${e.name} · ${n.name}`,jobs:[f],targets:[{id:a.newId(),name:`Recorded`,profile:null,steps:[{type:`recorded`,prompt:``}]},{id:a.newId(),name:`Edited`,profile:d,steps:[structuredClone(l)]}],evals:[a.newLink(i)],pass:{mode:`all`}}}var yc=e=>e instanceof Error?e.message:String(e),bc=e=>e.toLowerCase().endsWith(`.json`),xc=e=>J().METRICS[e.type]?.label??e.type;function Sc({source:e,definition:t,step:n,profiles:r,onClose:i}){let a=Q(),o=Kn(),s=ye(),[c,l]=On(`promptlab.profiles`),[u,d]=On(`promptlab.workflows`),[f]=On(`promptlab.versions`),[p,m]=(0,Z.useState)(null),[h,g]=(0,Z.useState)(null),[_,v]=(0,Z.useState)(!1),[y,b]=(0,Z.useState)(null),x=(0,Z.useCallback)(()=>{g(null),(async()=>{try{let t=(await a.source(e.id)).files.filter(e=>bc(e.name)),n=await Promise.all(t.map(async t=>({name:t.name,record:JSON.parse(await $s(await a.file(e.id,t.name)))})));m(n)}catch(e){g(yc(e))}})()},[a,e.id]);(0,Z.useEffect)(x,[x]);let S=_c(t,n),C=p?Xs(p,t):null,w=S.some(e=>J().METRICS[e.type]?.graded),T=(0,Z.useMemo)(()=>r.filter(e=>(J().CONNECTION_TYPES[e.type??``]?.answers??[`prompt`]).includes(`prompt`)),[r]),E=r.find(e=>e.id===c.grader)??null,[D,O]=(0,Z.useState)(``),k=T.find(e=>e.id===D)??null,A=e=>{if(gc(e,r).id!==`http-endpoint`)return r;let t=(()=>{try{return new URL(e).origin}catch{return``}})(),n={id:J().newId(),name:`HTTP endpoint`,url:t,model:``,key:``,temperature:``,type:`http`};return l({...c,list:[...c.list,n]}),[...r,n]},j=async()=>{if(!C)return;v(!0),b(null);let r=String(n.template.uri??``),a=u;try{let c=await o.create(`${e.name} · ${n.name}`,{...jn(),source:{id:e.id,name:e.name},every:S,cases:C.cases,...k?{grader:{id:k.id,name:k.name}}:{}}),l={id:c.id,name:c.name},f=vc({id:e.id,name:e.name},t,n,A(r),l),p={id:f.id,name:f.name,work:f};d({...u,list:[...u.list,p],at:p.id}),i(),location.hash=`#/runs`,s.offer({label:`Made a test of ${n.name}`,restore:()=>{d(a),o.remove(c.id).catch(()=>{})}})}catch(e){b(yc(e)),v(!1)}};return(0,$.jsxs)(Tt,{title:`Create new pipeline test`,onClose:i,className:`edit`,footer:(0,$.jsxs)(`div`,{className:`sacts`,children:[_&&(0,$.jsx)(`span`,{className:`note`,role:`status`,children:`Making the test…`}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:i,children:`Cancel`}),(0,$.jsxs)(`button`,{type:`button`,disabled:_||!C?.cases.length,"aria-busy":_||void 0,onClick:()=>void j(),children:[_&&(0,$.jsx)(F,{}),`Create`]})]}),children:[p?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(W,{label:`Test`,rows:[{key:`pipeline`,label:`Pipeline`,control:{type:`static`,text:`${e.name} · ${n.name}`}},{key:`a`,label:`Target A`,control:{type:`static`,text:`Recorded — the reply each call got when the flow ran`}},{key:`b`,label:`Target B`,control:{type:`static`,text:`Edited — the step's own request, to change`}},{key:`checks`,label:`Checks`,control:{type:`static`,text:S.map(xc).join(`, `)||`—`}},...w?[{key:`grader`,label:`Grader`,control:{type:`picker`,kind:`Profile`,value:D,usedLabel:`Last changed`,...ga(T,e=>f.profile?.[e]?.[0]?.at??null,[{id:``,label:E?`Lab (${E.name})`:`None — add one later`}]),onChange:O}}]:[],{key:`calls`,label:`Calls graded`,control:{type:`static`,text:`${C.cases.length}${C.skipped?`, ${C.skipped} with no recorded reply left out`:``}`}}]}),!C.cases.length&&(0,$.jsx)(`p`,{className:`note`,children:`No call holds a recorded reply to grade against.`})]}):(0,$.jsx)(M,{label:`Calls`,error:h,onRetry:x}),y&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:y})]})}function Cc({source:e}){let t=Q(),n=zs(),[r,a]=(0,Z.useState)(null),[o,s]=(0,Z.useState)(null),[c,l]=(0,Z.useState)(!1),[u,d]=(0,Z.useState)(null),f=e.config??{},p=(0,Z.useCallback)(()=>{d(null),t.definition(e.id).then(a,e=>{a(null),d(e instanceof Error?e.message:String(e))})},[t,e.id]);(0,Z.useEffect)(p,[p]);let m=async()=>{if(n&&r&&f.environment&&f.flow){l(!0),s(null);try{if(!await ks(n)){await Ns(n);return}let i=await Vs(Bs(n),f.environment,f.flow);a(await t.saveDefinition(e.id,r.version,i.definition)),s(`Synced.`)}catch(e){e instanceof Gt&&e.status===409?(p(),s(`It changed since: reloaded.`)):s(e instanceof Error?e.message:String(e))}finally{l(!1)}}},h=(0,Z.useMemo)(()=>wc(r?.body),[r]),g=(0,Z.useMemo)(()=>P(r?.body),[r]),[_]=On(`promptlab.profiles`),[v,y]=(0,Z.useState)(null);return(0,$.jsxs)(`div`,{className:`sources`,"data-level":`flow`,children:[o&&(0,$.jsx)(`p`,{className:`note`,role:`status`,children:o}),(0,$.jsxs)(X,{id:`flow-source`,title:`Flow`,note:f.displayName,actions:[{label:`Re-sync`,icon:bn,busy:c,disabled:!n||!r,onSelect:()=>void m()}],children:[(0,$.jsx)(W,{label:`Flow`,rows:[{key:`flow`,label:`Flow`,control:{type:`static`,text:f.displayName||f.flow||`—`}},{key:`env`,label:`Environment`,control:{type:`static`,text:f.environment||`—`}},...r?[{key:`version`,label:`Definition`,control:{type:`static`,text:r.version?`Version ${r.version}${r.at?`, ${gs(Date.parse(r.at)/1e3)}`:``}`:`None`}}]:[]]}),!r&&(0,$.jsx)(M,{label:`Definition`,error:u,onRetry:p})]}),(0,$.jsx)(X,{id:`flow-actions`,title:`Actions`,note:r?String(h.length):void 0,children:r?h.length?(0,$.jsx)(`div`,{className:`sflow`,children:(0,$.jsx)(q,{className:`slist`,label:`Actions`,rows:h,rowKey:e=>e.name,defaultSort:null,columns:[{key:`name`,header:`Name`,role:`title`,sort:e=>e.name,cell:e=>(0,$.jsxs)($.Fragment,{children:[e.name,e.in&&(0,$.jsxs)(`span`,{className:`sflow-in`,children:[`in `,e.in]})]})},{key:`type`,header:`Type`,sort:e=>e.type,cell:e=>e.type},{key:`calls`,header:`Calls`,sort:e=>e.calls,cell:e=>e.calls||`—`},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,cell:e=>{let t=g.find(t=>t.name===e.name);return t?(0,$.jsx)(i,{label:`${e.name} actions`,actions:[{label:`Test…`,icon:Qe,onSelect:()=>y(t)}]}):null}}]})}):(0,$.jsx)(`p`,{className:`note`,children:`No actions.`}):(0,$.jsx)(M,{label:`Actions`,error:u,onRetry:p})}),(0,$.jsx)(ic,{source:e,definition:r?.body??null,cfg:n}),v&&r&&(0,$.jsx)(Sc,{source:e,definition:r.body,step:v,profiles:_.list,onClose:()=>y(null)})]})}function wc(e){let t=[],n=(e,i)=>{for(let[a,o]of Object.entries(e??{})){t.push({name:a,type:o.type??``,in:i,calls:r(o)}),n(o.actions,a),n(o.else?.actions,`${a} (else)`);for(let[e,t]of Object.entries(o.cases??{}))n(t.actions,`${a} (${e})`);n(o.default?.actions,`${a} (default)`)}},r=e=>{if(e.type===`Http`&&typeof e.inputs?.uri==`string`)try{return new URL(e.inputs.uri).host}catch{return e.inputs.uri.startsWith(`@`)?`(expression)`:``}let t=e.inputs?.host?.apiId??e.inputs?.host?.connectionName;return t?t.split(`/`).pop()??``:``};return n(e?.actions,``),t}var Tc={workflow:{Create:Ks,Level:Cc}};function Ec({sourceId:e=null,onOpen:t,mode:n=`browse`,onPick:r,disabled:i}){let a=Q(),s=ye(),[c,l]=(0,Z.useState)(null),[u,d]=(0,Z.useState)(0),[f,p]=(0,Z.useState)(0),[m,h]=(0,Z.useState)(null),[g,_]=(0,Z.useState)(as),[v,b]=(0,Z.useState)(``),[x,S]=(0,Z.useState)(new Set),[C,w]=(0,Z.useState)(null),[T,E]=(0,Z.useState)(!1),[O,k]=(0,Z.useState)(!1),[A,j]=(0,Z.useState)(()=>n===`browse`&&Oc()!==null),[N,P]=(0,Z.useState)(null),[F,I]=(0,Z.useState)(null),[L,R]=(0,Z.useState)(!1),[z,B]=(0,Z.useState)(``),[ee,te]=(0,Z.useState)(()=>n===`browse`&&Oc()||J().DEFAULT_SOURCE_TYPE),[V,U]=(0,Z.useState)(``),[G,ne]=(0,Z.useState)(``),[ie,ae]=(0,Z.useState)(null),[oe,se]=(0,Z.useState)([]),[ce,le]=(0,Z.useState)(!1),[ue,de]=(0,Z.useState)(!1),[fe,K]=(0,Z.useState)(null),[me,he]=(0,Z.useState)(null),[ge,_e]=(0,Z.useState)(0),[be,xe]=(0,Z.useState)(0),Se=(0,Z.useRef)(null),Ce=(0,Z.useRef)(1),{view:we,sort:Te,dir:Ee}=g,Oe=(0,Z.useCallback)(()=>P(null),[]),ke=(0,Z.useCallback)(()=>j(!1),[]),Ae=(0,Z.useCallback)(()=>I(null),[]),je=(0,Z.useCallback)(()=>R(!1),[]);(0,Z.useEffect)(()=>os(g),[g]),(0,Z.useEffect)(()=>{n===`browse`&&kc(A?ee:null)},[n,A,ee]);let Me=async()=>{try{let e=await a.sources();l(e.sources),d(e.cap),p(e.used)}catch{}};(0,Z.useEffect)(()=>{let e=!0;return a.sources().then(t=>{e&&(l(t.sources),d(t.cap),p(t.used))}).catch(t=>{e&&K(Ie(t))}),()=>{e=!1}},[a,ge]),(0,Z.useEffect)(()=>{S(new Set),se([]);let t=!0;return he(null),e?a.source(e).then(e=>{t&&h(e)}).catch(e=>{t&&(h(null),he(Ie(e)))}):h(null),()=>{t=!1}},[e,be]);let Ne=async()=>{if(!e){h(null);return}try{h(await a.source(e))}catch{h(null)}},Pe=e=>{ae(e),e&&window.setTimeout(()=>ae(null),6e3)},Ie=e=>e instanceof Gt?e.message:`the request failed`,Y=ms(ps(m?.files??[],Te,Ee),v),Re=e=>{S(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},ze=()=>S(new Set(Y.map(e=>e.name))),Be=()=>S(new Set),Ve=async e=>{let t=[];try{for(let n of e)t.push((await a.removeSource(n.id)).trash),l(e=>(e??[]).filter(e=>e.id!==n.id))}catch(e){Pe(Ie(e))}t.length&&s.offer({label:e.length===1?`Deleted ${e[0].name}`:`Deleted ${t.length} Sources`,restore:()=>void(async()=>{try{for(let e of t)await a.restoreTrash(e);await Me()}catch(e){Pe(Ie(e))}})()})},He=e=>Ve([e]),We=async e=>{if(m)try{let{trash:t}=await a.removeFiles(m.id,e),n=new Set(e);h(t=>t&&{...t,files:t.files.filter(e=>!n.has(e.name)),bytes:t.bytes-e.reduce((e,n)=>e+(t.files.find(e=>e.name===n)?.bytes??0),0)}),S(e=>new Set([...e].filter(e=>!n.has(e)))),s.offer({label:e.length===1?`Removed ${e[0]}`:`Removed ${e.length} files`,restore:()=>void(async()=>{try{let e=await a.restoreTrash(t);h(e.source)}catch(e){Pe(Ie(e))}})()})}catch(e){Pe(Ie(e))}},Ge=(e,t)=>{let n=URL.createObjectURL(e),r=document.createElement(`a`);r.href=n,r.download=t,document.body.append(r),r.click(),r.remove(),URL.revokeObjectURL(n)},X=async e=>{if(!m)return;let t=e.length===1?e[0]:null;de(!0);try{let n=t?await a.file(m.id,t):await a.zip(m.id,e);Ge(n,t??`${m.name}.zip`)}catch(e){Pe(Ie(e))}finally{de(!1)}},Ke=async e=>{if(!m||m.system)return;let t=[...e];if(!t.length)return;let n=t.map(e=>({id:Ce.current++,file:e,state:`uploading`}));se(e=>[...e,...n]);for(let e of n)try{await a.upload(m.id,e.file.name,e.file),se(t=>t.map(t=>t.id===e.id?{...t,state:`done`}:t))}catch(t){se(n=>n.map(n=>n.id===e.id?{...n,state:`error`,error:Ie(t)}:n))}await Ne()},qe=e=>void Ke([e.file]),Je=async(e,t)=>{if(m&&F)try{await a.copyFiles(t.id,m.id,F.names,e===`move`),I(null),S(new Set),await Ne(),Pe(e===`move`?`Moved to ${t.name}.`:`Copied to ${t.name}.`)}catch(e){Pe(Ie(e)),I(null)}},Ye=async()=>{if(m&&z.trim())try{let e=await a.createSource(z.trim(),m.type);try{await a.copyFiles(e.id,m.id,[...x],!1)}catch(t){throw await a.removeSource(e.id).catch(()=>{}),t}R(!1),B(``),S(new Set),await Me(),Pe(`Made a new Source.`)}catch(e){Pe(Ie(e))}},Ze=async()=>{if(z.trim())try{await a.createSource(z.trim(),ee),j(!1),B(``),te(J().DEFAULT_SOURCE_TYPE),await Me()}catch(e){Pe(Ie(e))}},Qe=async(e,t)=>{try{let n=await a.renameSource(e.id,t);l(t=>(t??[]).map(t=>t.id===e.id?_s(n):t)),h(e=>e?.id===n.id?{...e,name:n.name}:e)}catch(e){Pe(Ie(e))}},$e=(e,t=()=>w(e.id),n)=>e.system?[]:[{label:`Rename…`,icon:pe,onSelect:t},{label:`Delete`,icon:o,destructive:!0,onSelect:()=>void He(e).then(n)}],et=e=>[{label:`Preview`,icon:lt,disabled:!us(e.name),onSelect:()=>P(e)},{label:`Download`,icon:ve,onSelect:()=>void X([e.name])},...m?.system?[]:[{label:`Remove`,icon:o,destructive:!0,onSelect:()=>void We([e.name])}]],tt=[...m?.system?[]:[{label:`Remove`,icon:o,destructive:!0,onSelect:()=>void We([...x])}],{label:`Download`,icon:ve,busy:ue,onSelect:()=>void X([...x])},...m?.system?[]:[{label:`Move to…`,icon:ln,onSelect:()=>I({kind:`move`,names:[...x]})}],{label:`Copy to…`,icon:ft,onSelect:()=>I({kind:`copy`,names:[...x]})},...m?.system?[]:[{label:`New Source from selection…`,icon:un,onSelect:()=>R(!0)}]],rt=e=>{e.preventDefault(),le(!1),m&&!m.system&&Ke(e.dataTransfer.files)},it=(c??[]).filter(e=>(!G||Bc(e)===G)&&e.name.toLowerCase().includes(V.trim().toLowerCase())),at=(0,$.jsx)(Le,{noun:`Sources`,search:{label:`Find a Source`,value:V,placeholder:`Name`,onChange:U},filters:[{key:`type`,label:`Type`,value:G,onChange:ne,options:[{value:``,label:`All`},...Rc()]}],count:{shown:it.length,total:(c??[]).length}}),ot=()=>(0,$.jsxs)(`div`,{className:`sources`,"data-level":`sources`,children:[ie&&(0,$.jsx)(`p`,{className:`note`,role:`status`,children:ie}),(0,$.jsxs)(`header`,{className:`shead`,children:[(0,$.jsx)(`h2`,{className:`shead-title`,children:`Sources`}),(0,$.jsx)(jc,{view:we,onChange:e=>_(t=>({...t,view:e}))})]}),c!==null&&c.length>0&&at,c===null?(0,$.jsx)(M,{label:`Sources`,error:fe,onRetry:()=>{K(null),_e(e=>e+1)}}):c.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No Sources on the server yet.`}):we===`thumbs`?(0,$.jsx)(`ul`,{className:`sfolders`,children:it.map(e=>(0,$.jsx)(`li`,{className:`sfolder`,children:(0,$.jsxs)(`button`,{type:`button`,className:`sfolder-open${i?.(e)?` off`:``}`,disabled:i?.(e),onClick:()=>r?.(e),children:[(0,$.jsx)(Pc,{}),(0,$.jsx)(`span`,{className:`sfolder-name`,children:e.name}),(0,$.jsxs)(`span`,{className:`sfolder-meta`,children:[Vc(e),`. `,Hc(e)]})]})},e.id))}):(0,$.jsx)(q,{className:`slist slist-folders`,label:`Sources`,rows:it,rowKey:e=>e.id,columns:[{key:`name`,header:`Name`,className:`sfile-name`,role:`title`,sort:e=>e.name,cell:e=>(0,$.jsxs)(`button`,{type:`button`,className:`srow-pick${i?.(e)?` off`:``}`,disabled:i?.(e),onClick:()=>r?.(e),children:[(0,$.jsx)(Pc,{}),` `,e.name]})},{key:`type`,header:`Type`,sort:e=>Vc(e),cell:e=>Vc(e)},{key:`files`,header:`Items`,className:`num`,cell:e=>e.files},{key:`size`,header:`Size`,className:`num`,sort:e=>e.bytes,cell:e=>hs(e.bytes)}]})]});if(n===`pick`)return ot();if(e){if(!m)return(0,$.jsx)(`div`,{className:`sources`,children:(0,$.jsx)(M,{label:`Files`,error:me,onRetry:()=>xe(e=>e+1)})});let e=Tc[m.type]?.Level,n=!e&&!m.system,r=()=>t?.(null),i=(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(re,{kind:`Source`,name:m.name,onPick:c?.length?()=>k(!0):void 0,meta:`${Vc(_s(m))}. ${[Hc(_s(m)),hs(m.bytes)].join(`, `)}`,primary:n?{label:`Upload…`,icon:H,onSelect:()=>Se.current?.click()}:void 0,actions:$e(_s(m),()=>E(!0),r)}),O&&c&&(0,$.jsx)(Xe,{kind:`Source`,value:m.id,onPick:e=>t?.(e),onClose:()=>k(!1),options:c.map(e=>({id:e.id,label:e.name}))}),T&&(0,$.jsx)(nt,{title:`Rename Source`,value:m.name,onCommit:e=>{e&&e!==m.name&&Qe(m,e)},onClose:()=>E(!1)})]});return e?(0,$.jsxs)(`div`,{className:`sources`,children:[i,(0,$.jsx)(e,{source:m,onBack:r}),(0,$.jsx)(ut,{undo:s.undo,onUndo:s.undoNow,onDismiss:s.dismiss})]}):(0,$.jsxs)(`div`,{className:`sources`,"data-level":`files`,children:[i,ie&&(0,$.jsx)(`p`,{className:`note`,role:`status`,children:ie}),(0,$.jsxs)(`header`,{className:`shead`,children:[(0,$.jsx)(jc,{view:we,onChange:e=>_(t=>({...t,view:e}))}),(0,$.jsx)(Mc,{sort:Te,dir:Ee,onChange:(e,t)=>_(n=>({...n,sort:e,dir:t}))}),(0,$.jsxs)(`label`,{className:`sfilter`,children:[(0,$.jsx)(y,{size:14,"aria-hidden":`true`}),(0,$.jsx)(`input`,{type:`search`,"aria-label":`Filter by name`,placeholder:`Filter`,value:v,onChange:e=>b(e.target.value)})]})]}),x.size>0&&(0,$.jsx)(Fe,{count:x.size,noun:`file`,onClear:Be,actions:we===`thumbs`?[{label:`Select all`,icon:en,disabled:x.size===Y.length,onSelect:ze},...tt]:tt}),(0,$.jsx)(`div`,{className:`sfiles${ce?` drag`:``}`,onDragOver:e=>{e.preventDefault(),le(!0)},onDragLeave:()=>le(!1),onDrop:rt,children:Y.length===0?(0,$.jsx)(`p`,{className:`note`,children:v?`No files match that name.`:`No files in this Source yet.`}):we===`thumbs`?(0,$.jsx)(`ul`,{className:`stiles`,children:Y.map(e=>(0,$.jsx)(`li`,{children:(0,$.jsxs)(`button`,{type:`button`,className:`stile${x.has(e.name)?` sel`:``}`,"aria-pressed":x.has(e.name),"aria-label":e.name,onClick:()=>Re(e.name),children:[(0,$.jsx)(Ac,{id:m.id,file:e}),(0,$.jsx)(`span`,{className:`stile-name`,children:e.name}),x.has(e.name)&&(0,$.jsx)(`span`,{className:`stile-check`,children:(0,$.jsx)(De,{size:12,"aria-hidden":`true`})})]})},e.name))}):(0,$.jsx)(q,{className:`slist`,label:`${m.name} files`,rows:Y,rowKey:e=>e.name,defaultSort:null,select:{noun:`file`,selected:x,onChange:S},rowProps:e=>({onClick:()=>Re(e.name)}),columns:[{key:`name`,header:`Name`,className:`sfile-name`,role:`title`,cell:e=>e.name},{key:`type`,header:`Type`,className:`stype`,cell:e=>Nc(e.name)},{key:`size`,header:`Size`,className:`num`,sort:e=>e.bytes,cell:e=>hs(e.bytes)},{key:`added`,header:`Added`,sort:e=>e.added,cell:e=>e.added?gs(e.added):`—`},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,tdProps:()=>({onClick:e=>e.stopPropagation()}),cell:e=>(0,$.jsx)(Ue,{label:`${e.name} actions`,actions:et(e)})}]})}),n&&(0,$.jsxs)(`div`,{className:`supload`,children:[(0,$.jsx)(`input`,{ref:Se,type:`file`,multiple:!0,hidden:!0,"aria-hidden":`true`,accept:J().sourceTypeOf(m)?.uploads?.join(`,`),onChange:e=>{e.target.files&&Ke(e.target.files),e.target.value=``}}),oe.length>0&&(0,$.jsx)(`ul`,{className:`suploads`,children:oe.map(e=>(0,$.jsxs)(`li`,{className:`supload ${e.state}`,children:[(0,$.jsx)(`span`,{className:`supload-name`,children:e.file.name}),e.state===`uploading`&&(0,$.jsx)(`span`,{className:`supload-bar`,role:`status`,children:`Uploading…`}),e.state===`done`&&(0,$.jsx)(`span`,{className:`note`,children:`Uploaded`}),e.state===`error`&&(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`span`,{className:`note bad`,children:e.error}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>qe(e),children:`Retry`})]})]},e.id))})]}),N&&(0,$.jsxs)(Tt,{title:N.name,onClose:Oe,children:[us(N.name)?(0,$.jsx)(`img`,{className:`spreview-img`,src:fs(m.id,N),alt:N.name}):null,(0,$.jsx)(`p`,{className:`note`,children:hs(N.bytes)})]}),F&&(0,$.jsx)(Tt,{title:F.kind===`move`?`Move to…`:`Copy to…`,onClose:Ae,children:(0,$.jsx)(Ec,{mode:`pick`,disabled:e=>e.id===m.id||e.system||e.type!==m.type,onPick:e=>void Je(F.kind,e)})}),L&&(0,$.jsxs)(Tt,{title:`New Source from selection`,onClose:je,dirty:!!z.trim(),children:[(0,$.jsxs)(`label`,{className:`prompt`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:`Name`}),(0,$.jsx)(`input`,{"aria-label":`New Source name`,value:z,autoFocus:!0,onChange:e=>B(e.target.value),onKeyDown:e=>{e.key===`Enter`&&Ye()}})]}),(0,$.jsxs)(`div`,{className:`sacts`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>R(!1),children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,disabled:!z.trim(),onClick:()=>void Ye(),children:`Make`})]})]}),(0,$.jsx)(ut,{undo:s.undo,onUndo:s.undoNow,onDismiss:s.dismiss})]})}return(0,$.jsxs)(`div`,{className:`sources`,"data-level":`sources`,children:[ie&&(0,$.jsx)(`p`,{className:`note`,role:`status`,children:ie}),(0,$.jsxs)(`header`,{className:`shead`,children:[(0,$.jsx)(`h2`,{className:`shead-title`,children:`Sources`}),(0,$.jsxs)(`p`,{className:`note suste`,children:[`Used `,hs(f),` of `,hs(u)]}),(0,$.jsx)(jc,{view:we,onChange:e=>_(t=>({...t,view:e}))})]}),c!==null&&c.length>0&&at,c===null?(0,$.jsx)(M,{label:`Sources`,error:fe,onRetry:()=>{K(null),_e(e=>e+1)}}):c.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No Sources yet — Add source below.`}):we===`thumbs`?(0,$.jsx)(`ul`,{className:`sfolders`,children:it.map(e=>(0,$.jsxs)(`li`,{className:`sfolder`,children:[(0,$.jsxs)(`button`,{type:`button`,className:`sfolder-open`,onClick:()=>t?.(e.id),children:[(0,$.jsx)(Pc,{}),(0,$.jsx)(`span`,{className:`sfolder-name`,children:e.name}),(0,$.jsxs)(`span`,{className:`sfolder-meta`,children:[Vc(e),`. `,Hc(e)]}),(0,$.jsx)(Lc,{source:e,cap:u,children:e.system?(0,$.jsx)(`span`,{className:`squota-none`,children:`—`}):(0,$.jsx)(`span`,{className:`squota-bar`,children:(0,$.jsx)(`span`,{className:`squota-fill`,style:{width:`${Fc(e.bytes,u)}%`}})})})]}),(0,$.jsx)(`span`,{className:`sfolder-menu`,children:(0,$.jsx)(Ue,{label:`${e.name} actions`,actions:$e(e)})})]},e.id))}):(0,$.jsx)(`div`,{className:`sfiles`,children:(0,$.jsx)(q,{className:`slist slist-folders`,label:`Sources`,rows:it,rowKey:e=>e.id,rowProps:e=>({"data-open":e.id,onClick:()=>t?.(e.id)}),select:{noun:`Source`,actions:e=>[{label:`Delete`,icon:o,destructive:!0,disabled:e.every(e=>e.system),onSelect:()=>void Ve(e.filter(e=>!e.system))}]},columns:[{key:`name`,header:`Name`,className:`sfile-name`,role:`title`,sort:e=>e.name,cell:e=>(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(Pc,{}),` `,e.name]})},{key:`type`,header:`Type`,sort:e=>Vc(e),cell:e=>Vc(e)},{key:`files`,header:`Items`,className:`num`,cell:e=>e.files},{key:`size`,header:`Size`,className:`num`,sort:e=>e.bytes,cell:e=>hs(e.bytes)},{key:`used`,header:`Used`,className:`num`,sort:e=>e.system?null:e.bytes,cell:e=>(0,$.jsx)(Lc,{source:e,cap:u,children:e.system?`—`:`${Fc(e.bytes,u)}%`})},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,tdProps:()=>({onClick:e=>e.stopPropagation()}),cell:e=>(0,$.jsx)(Ue,{label:`${e.name} actions`,actions:$e(e)})}]})}),C&&c&&(()=>{let e=c.find(e=>e.id===C);return e?(0,$.jsxs)(`label`,{className:`srename`,children:[(0,$.jsxs)(`span`,{className:`prop-name`,children:[`Rename `,e.name]}),(0,$.jsx)(`input`,{"aria-label":`Rename ${e.name}`,defaultValue:e.name,autoFocus:!0,onKeyDown:t=>{if(t.key===`Enter`){let n=t.target.value.trim();n&&n!==e.name&&Qe(e,n),w(null)}t.key===`Escape`&&w(null)},onBlur:t=>{let n=t.target.value.trim();n&&n!==e.name&&Qe(e,n),w(null)}})]}):null})(),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>j(!0),children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),` Add source`]}),A&&(0,$.jsxs)(Tt,{title:`Add source`,onClose:ke,dirty:!!z.trim(),children:[(0,$.jsx)(W,{label:`New Source`,rows:[{key:`type`,label:`Type`,hint:J().SOURCE_TYPES[zc(ee)]?.description,control:{type:`select`,value:ee,options:Rc(),onChange:te}}]}),(()=>{let e=Tc[zc(ee)]?.Create,t=(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>j(!1),children:`Cancel`});return e?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(e,{onMade:async()=>{j(!1),te(J().DEFAULT_SOURCE_TYPE),await Me()}}),(0,$.jsx)(`div`,{className:`sacts`,children:t})]}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(`label`,{className:`prompt`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:`Name`}),(0,$.jsx)(`input`,{"aria-label":`Source name`,value:z,autoFocus:!0,onChange:e=>B(e.target.value),onKeyDown:e=>{e.key===`Enter`&&Ze()}})]}),(0,$.jsxs)(`div`,{className:`sacts`,children:[t,(0,$.jsx)(`button`,{type:`button`,disabled:!z.trim(),onClick:()=>void Ze(),children:`Add`})]})]})})()]}),(0,$.jsx)(ut,{undo:s.undo,onUndo:s.undoNow,onDismiss:s.dismiss})]})}var Dc=`evals-lab.sources.adding`;function Oc(){try{return sessionStorage.getItem(Dc)}catch{return null}}function kc(e){try{e?sessionStorage.setItem(Dc,e):sessionStorage.removeItem(Dc)}catch{}}function Ac({id:e,file:t}){let[n,r]=(0,Z.useState)(!1);return!t.thumb||n?(0,$.jsx)(`span`,{className:`stile-type`,children:Nc(t.name)||`FILE`}):(0,$.jsx)(`img`,{className:`stile-img`,src:ds(e,t),alt:``,loading:`lazy`,onError:()=>r(!0)})}function jc({view:e,onChange:t}){return(0,$.jsxs)(`div`,{className:`sview`,role:`group`,"aria-label":`View`,children:[(0,$.jsx)(`button`,{type:`button`,className:e===`thumbs`?`on`:void 0,"aria-pressed":e===`thumbs`,onClick:()=>t(`thumbs`),children:`Thumbnails`}),(0,$.jsx)(`button`,{type:`button`,className:e===`list`?`on`:void 0,"aria-pressed":e===`list`,onClick:()=>t(`list`),children:`List`})]})}function Mc({sort:e,dir:t,onChange:n}){return(0,$.jsxs)(`div`,{className:`ssort`,role:`group`,"aria-label":`Sort`,children:[(0,$.jsxs)(`select`,{"aria-label":`Sort by`,value:e,onChange:e=>n(e.target.value,t),children:[(0,$.jsx)(`option`,{value:`name`,children:`Name`}),(0,$.jsx)(`option`,{value:`added`,children:`Date added`}),(0,$.jsx)(`option`,{value:`size`,children:`Size`})]}),(0,$.jsx)(`button`,{type:`button`,className:`icon-btn`,"aria-label":`Sort ${t===1?`ascending`:`descending`}`,onClick:()=>n(e,t===1?-1:1),children:(0,$.jsx)(t===1?Me:Ye,{size:14,"aria-hidden":`true`})})]})}function Nc(e){let t=e.lastIndexOf(`.`);return t<0?``:e.slice(t+1).toUpperCase()}function Pc(){return(0,$.jsx)(fn,{size:16,"aria-hidden":`true`})}function Fc(e,t){return t<=0?0:Math.max(0,Math.min(100,Math.round(e/t*100)))}function Ic(e,t){return e.system?`Quota: not counted`:`Quota: ${hs(e.bytes)} of ${hs(t)}`}function Lc({source:e,cap:t,children:n}){let r=Ic(e,t);return(0,$.jsxs)(`span`,{className:`squota`,role:`img`,"aria-label":r,children:[n,(0,$.jsx)(`span`,{className:`squota-pop`,"aria-hidden":`true`,children:r})]})}var Rc=()=>Object.entries(J().SOURCE_TYPES).flatMap(([e,t])=>t.platforms?t.platforms.flatMap(e=>{let t=J().WORKFLOW_PLATFORMS[e];return t?[{value:e,label:t.label}]:[]}):[{value:e,label:t.label}]),zc=e=>e in J().SOURCE_TYPES?e:Object.entries(J().SOURCE_TYPES).find(([,t])=>t.platforms?.includes(e))?.[0]??e,Bc=e=>e.platform??e.type,Vc=e=>Rc().find(t=>t.value===Bc(e))?.label??J().SOURCE_TYPES[e.type]?.label??e.type,Hc=e=>{let t=J().sourceTypeOf(e)?.noun??`item`;return`${e.files} ${e.files===1?t:`${t}s`}`};function Uc({sourceId:e}){let[t,n]=(0,Z.useState)(e);return(0,Z.useEffect)(()=>n(e),[e]),(0,$.jsx)(Ec,{sourceId:t,onOpen:e=>{n(e),e?location.hash=`/library/sources/${encodeURIComponent(e)}`:location.hash=`#/library/sources`}})}var Wc=[{id:`datasets`,label:`Evals`,icon:tt},{id:`filters`,label:`Filter sets`,icon:pn},{id:`prompts`,label:`Prompts`,icon:de},{id:`sources`,label:`Sources`,icon:ht}];function Gc({section:e,sourceId:t,groupId:n,setId:r}){return(0,$.jsxs)(`div`,{className:`library`,children:[(0,$.jsx)(m,{label:`Library`,level:`section`,className:`lib-sections`,current:e,items:Wc.map(({id:e,label:t,icon:n})=>({key:e,label:t,href:`#/library/${e}`,icon:n}))}),(0,$.jsx)(`section`,{className:`lib-section`,"data-section":e,children:e===`datasets`?(0,$.jsx)(qa,{groupId:n}):e===`filters`?(0,$.jsx)(Ko,{setId:r}):e===`prompts`?(0,$.jsx)(ns,{}):(0,$.jsx)(Uc,{sourceId:t})})]})}var Kc=`0.9.0 (2026.10.07-516)`,qc=3,Jc=3,Yc=e=>ta.find(t=>t.id===e),Xc=e=>Wc.find(t=>t.id===e),Zc=e=>new Date(e).toLocaleString(void 0,{month:`short`,day:`numeric`,hour:`numeric`,minute:`2-digit`});function Qc(){let e=Q(),[t]=On(`promptlab.profiles`),[n,r]=On(`promptlab.workflows`),i=qn(),a=rr(),{pickDataset:o}=wr(),{openRun:s}=Dr(),c=Kn(),l=nr(),[u,d]=(0,Z.useState)(null),[f,p]=(0,Z.useState)(null),[m,h]=(0,Z.useState)(null),[g,_]=(0,Z.useState)(null),[v,y]=(0,Z.useState)(0),[b,x]=(0,Z.useState)(0),S=e=>e instanceof Error?e.message:String(e);(0,Z.useEffect)(()=>{let t=!0;return h(null),e.sources().then(e=>{t&&d(e.sources)},e=>{t&&h(S(e))}),()=>{t=!1}},[e,v]),(0,Z.useEffect)(()=>{let t=!0;return _(null),e.queue({limit:Jc}).then(e=>{t&&p(e.runs)},e=>{t&&_(S(e))}),()=>{t=!1}},[e,b]);let C=(0,Z.useMemo)(()=>n.list.filter(e=>e.work?.version===14),[n.list]),w=i.list??[],T=w.reduce((e,t)=>e+t.cases,0),E=(f??[]).filter(e=>e.status===`queued`||e.status===`running`).length,D=t.list.length===0?`setup`:i.list===null?null:w.length===0||T===0?`datasets`:f!==null&&f.length===0?`runs`:null,O=(...e)=>e.filter(Boolean).join(`, `)||void 0,k=Yc(`library`),A=k.icon;return(0,$.jsxs)(`div`,{className:`home`,children:[(0,$.jsx)(`div`,{className:`home-pane`,children:(0,$.jsxs)(`ol`,{className:`home-line`,"aria-label":`Lab`,children:[(0,$.jsx)($c,{id:`runs`,icon:Yc(`runs`).icon,name:Yc(`runs`).label,hub:!0,start:D===`runs`,fact:O(Wr(C.length,`pipeline`),E>0&&`${E} running`),total:C.length,items:C.map(e=>({key:e.id,label:e.name,href:`#/runs`,onPick:()=>r({...n,at:e.id})}))}),(0,$.jsx)($c,{id:`history`,icon:Yc(`history`).icon,name:Yc(`history`).label,fact:f&&f.length===0?`No runs yet`:void 0,wait:f?void 0:{error:g,onRetry:()=>x(e=>e+1)},items:(f??[]).map(e=>{let t=ji(e).scenarios.map(e=>e.percent==null?`–`:`${e.percent}%`).join(`, `);return{key:e.id,label:(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`code`,{children:Ur(e.submittedAt)}),e.status!==`done`&&Xr(e.status)]}),sub:O(Zc(e.submittedAt),!!t&&t),href:`#/history`,onOpen:()=>s(e.id,{at:e.submittedAt,from:`home`})}})}),(0,$.jsxs)(`li`,{className:`home-stop home-fork`,"data-stop":`library`,children:[(0,$.jsxs)(`a`,{className:`home-name`,href:`#/library`,children:[(0,$.jsx)(`span`,{className:`home-mark`,"aria-hidden":`true`,children:(0,$.jsx)(A,{size:18})}),k.label]}),(0,$.jsxs)(`ol`,{className:`home-branch`,"aria-label":k.label,children:[(0,$.jsx)($c,{id:`datasets`,to:`library/datasets`,branch:!0,icon:Xc(`datasets`).icon,name:Xc(`datasets`).label,start:D===`datasets`,fact:i.list?O(Wr(w.length,`eval group`),Wr(T,`case`)):void 0,total:w.length,wait:i.list?void 0:{error:i.error,onRetry:()=>void c.refresh()},items:w.map(e=>({key:e.id,label:e.name,sub:Wr(e.cases,`case`),href:`#/library/datasets/${encodeURIComponent(e.id)}`,onPick:()=>o(e.id)}))}),(0,$.jsx)($c,{id:`prompts`,to:`library/prompts`,branch:!0,icon:Xc(`prompts`).icon,name:Xc(`prompts`).label,fact:a.list?O(Wr(a.list.length,`prompt`)):void 0,total:a.list?.length??0,wait:a.list?void 0:{error:a.error,onRetry:()=>void l.refresh()},items:(a.list??[]).map(e=>({key:e.id,label:e.name||e.text.split(`
9
+ `)[0]||`Untitled`,sub:e.default?`Default`:void 0,href:`#/library/prompts`}))}),(0,$.jsx)($c,{id:`sources`,to:`library/sources`,branch:!0,icon:Xc(`sources`).icon,name:Xc(`sources`).label,fact:u?O(Wr(u.length,`source`)):void 0,total:u?.length??0,wait:u?void 0:{error:m,onRetry:()=>y(e=>e+1)},items:(u??[]).map(e=>({key:e.id,label:e.name,sub:Wr(e.files,`file`),href:`#/library/sources/${encodeURIComponent(e.id)}`}))})]})]}),(0,$.jsx)($c,{id:`setup`,icon:Yc(`setup`).icon,name:Yc(`setup`).label,start:D===`setup`,fact:O(Wr(t.list.length,`profile`)),total:t.list.length,items:t.list.map(e=>({key:e.id,label:e.name||`Untitled`,sub:e.model,href:`#/setup`}))})]})}),(0,$.jsxs)(`p`,{className:`home-version`,children:[`Evals Lab `,Kc]})]})}function $c({id:e,icon:t,name:n,to:r,fact:i,items:a,total:o=a.length,hub:s,start:c,branch:l,wait:u}){let d=o-Math.min(a.length,qc);return(0,$.jsxs)(`li`,{className:[`home-stop`,s&&`hub`,c&&`start`,l&&`branch`].filter(Boolean).join(` `),"data-stop":e,children:[(0,$.jsxs)(`a`,{className:`home-name`,href:`#/${r??e}`,children:[(0,$.jsx)(`span`,{className:`home-mark`,"aria-hidden":`true`,children:(0,$.jsx)(t,{size:l?15:18})}),n]}),c&&(0,$.jsx)(`span`,{className:`home-start`,children:`Start Here`}),u&&(0,$.jsx)(M,{label:n,error:u.error,onRetry:u.onRetry}),!u&&i&&(0,$.jsx)(`span`,{className:`home-fact`,children:i}),!u&&a.length>0&&(0,$.jsxs)(`ul`,{className:`home-items`,children:[a.slice(0,qc).map(e=>(0,$.jsx)(`li`,{children:(0,$.jsxs)(`a`,{href:e.href,onClick:t=>{e.onOpen?(t.preventDefault(),e.onOpen()):e.onPick?.()},children:[(0,$.jsx)(`span`,{className:`home-item`,children:e.label}),e.sub&&(0,$.jsx)(`span`,{className:`home-sub`,children:e.sub})]})},e.key)),d>0&&(0,$.jsx)(`li`,{children:(0,$.jsxs)(`a`,{className:`home-more`,href:`#/${r??e}`,children:[d,` more`]})})]})]})}function el({list:e,at:t,canRestore:n,onSelect:r,onNew:i,onClone:a,onRename:s,onDelete:c,onRestore:l,onExport:u,onExportCi:d,onImport:f,used:p={}}){let[m,h]=(0,Z.useState)(!1),[g,_]=(0,Z.useState)(!1),v=e.find(e=>e.id===t),y=[{label:`Rename…`,icon:pe,onSelect:()=>h(!0)},{label:`Clone`,icon:ft,onSelect:a},{label:`Export YAML`,icon:ve,onSelect:u},{label:`Export for CI…`,icon:ve,onSelect:d},{label:`Import YAML…`,icon:H,onSelect:f},{label:`Restore…`,icon:ke,disabled:!n,onSelect:l},{label:`Delete`,icon:o,destructive:!0,onSelect:c}],b=v?.work.jobs?.length??0,x=v?.work.targets?.length??0;return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(re,{kind:`Pipeline`,name:v?.name?.trim()||`Untitled`,onPick:()=>_(!0),meta:`${nl(b,`job`)}, ${nl(x,`target`)}`,primary:{label:`New pipeline`,icon:D,onSelect:i},actions:y,docked:!0}),g&&(0,$.jsx)(Xe,{kind:`Pipeline`,value:t,onPick:r,onClose:()=>_(!1),options:e.map(e=>{let t=p[e.id];return{id:e.id,label:e.name||`Untitled`,used:t?{at:t,label:qr(t)}:null}}),recent:e.filter(e=>p[e.id]).sort((e,t)=>p[t.id].localeCompare(p[e.id])).slice(0,tl).map(e=>e.id)}),m&&(0,$.jsx)(nt,{title:`Rename pipeline`,value:v?.name??``,onCommit:s,onClose:()=>h(!1)})]})}var tl=5,nl=(e,t)=>`${e} ${e===1?t:`${t}s`}`;function rl({hasItems:e,onExport:t,onClose:n}){let[r,i]=(0,Z.useState)(!1),[a,o]=(0,Z.useState)(!1),[s,c]=(0,Z.useState)(null),l=async()=>{o(!0),c(null);try{await t(e&&r),n()}catch(e){c(e instanceof Error?e.message:String(e))}finally{o(!1)}};return(0,$.jsx)(Ot,{title:`Export for CI`,status:s??void 0,statusBad:!!s,onClose:n,sections:[{key:`bundle`,rows:[{key:`items`,label:`Include files`,...e?{hint:`Copy the Source's files into the zip. Off, evals-lab run needs --items`}:{},control:e?{type:`checkbox`,checked:r,onChange:i}:{type:`static`,text:`None`}}]}],actions:[{label:`Export`,icon:ve,busy:a,primary:!0,onSelect:()=>void l()}]})}function il({problems:e,error:t,run:n,submitting:r,onRun:i,onStop:a,onOpen:o,onShare:s}){let c=!!n&&!li(n.status),l=n?.progress?.total??0,u=n?.progress?.n??0,d=c&&l?Math.round(u/l*100):null,f=n&&(0,$.jsxs)(`button`,{type:`button`,className:`runbar-open`,title:`Open this run`,onClick:o,children:[(0,$.jsx)(`span`,{className:`runbar-word`,children:Yr(n.status)}),al(n)&&(0,$.jsx)(`span`,{className:`runbar-detail`,children:al(n)})]}),p=c?f:e.length?(0,$.jsxs)(`span`,{className:`runbar-why`,children:[cl(e[0]),sl(e[0])]}):t?(0,$.jsxs)(`span`,{className:`runbar-why bad`,children:[cl(t),s&&(0,$.jsxs)($.Fragment,{children:[` `,(0,$.jsx)(`button`,{type:`button`,className:`runbar-fix`,onClick:s,children:`Share`})]})]}):f??(0,$.jsx)(`span`,{className:`runbar-word`,children:`Ready to run`});return(0,$.jsx)(Te,{className:`runbar`,primary:{label:`Run`,icon:T,onSelect:i,disabled:e.length>0,busy:r},status:p,secondary:c?{label:`Stop`,icon:se,onSelect:a}:void 0,progress:d,progressLabel:`Run progress`})}function al(e){if(e.status===`queued`)return e.behind?`behind ${e.behind}`:`next in line`;if(e.status===`running`)return e.progress?.total?`${e.progress.n} of ${e.progress.total}`:``;if(e.status===`done`&&e.progress?.total){let t=$r(e.startedAt,e.finishedAt);return t==null?String(e.progress.total):`${e.progress.total} in ${Qr(t)}`}return``}var ol=[[/^Target profile .* not found$/,`#/setup`,`Open\xA0Setup`],[/^Source .* not found$/,`#/library/sources`,`Open\xA0Sources`],[/^Dataset .* not found$/,`#/library/datasets`,`Open\xA0Evals`]];function sl(e){let t=ol.find(([t])=>t.test(e.trim()));return t?(0,$.jsxs)($.Fragment,{children:[` `,(0,$.jsx)(`a`,{className:`runbar-fix`,href:t[1],children:t[2]})]}):null}var cl=e=>{let t=e.trim();if(!t)return t;let n=t[0].toUpperCase()+t.slice(1);return/[.!?]$/.test(n)?n:`${n}.`},ll=10;function ul({onPick:e,onClose:t,used:n}){let{list:r,error:i}=rr(),a=nr(),[o,s]=(0,Z.useState)(``),[c,l]=(0,Z.useState)(`all`),[u,d]=(0,Z.useState)(0),f=r??[],p=f.filter(e=>Xo(e,o,c,n??{byScenario:new Set,byAll:new Set})).sort((e,t)=>Number(t.default)-Number(e.default)),m=Math.ceil(p.length/ll),h=Math.min(u,Math.max(0,m-1)),g=p.slice(h*ll,(h+1)*ll);return(0,$.jsxs)(Tt,{title:`Select prompt`,onClose:t,className:`sp`,initialFocus:`.sp input[type=search]`,children:[(0,$.jsx)(Le,{noun:`prompts`,search:{label:`Search prompts`,placeholder:`Search prompts`,value:o,onChange:e=>{s(e),d(0)}},filters:[{key:`show`,label:`Show`,value:c,onChange:e=>{l(e),d(0)},options:Jo}],count:{shown:p.length,total:f.length}}),r===null?(0,$.jsx)(M,{label:`Prompts`,error:i,onRetry:()=>void a.refresh()}):f.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No prompts yet.`}):p.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No prompts match these filters.`}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`ul`,{className:`sp-list`,"aria-label":`Prompts`,children:g.map(t=>(0,$.jsx)(`li`,{children:(0,$.jsxs)(`button`,{type:`button`,className:`sp-pick`,onClick:()=>e(t),children:[(0,$.jsxs)(`span`,{className:`sp-head`,children:[(0,$.jsx)(`span`,{className:`sp-title`,children:Yn(t)}),t.default&&(0,$.jsx)(`span`,{className:`pl-default`,children:`Default`})]}),(0,$.jsx)(`span`,{className:`sp-text`,children:t.text.trim()||`—`}),(0,$.jsxs)(`span`,{className:`sp-meta`,children:[(0,$.jsx)(`span`,{children:t.useCount===1?`1 use`:`${t.useCount} uses`}),t.lastUsed&&(0,$.jsxs)(`span`,{children:[`last `,Zo(t.lastUsed)]}),(0,$.jsxs)(`span`,{children:[`v`,t.version]})]})]})},t.id))}),m>1&&(0,$.jsxs)(`div`,{className:`sp-pager`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,disabled:h===0,onClick:()=>d(h-1),children:`Back`}),(0,$.jsxs)(`span`,{className:`sp-page`,children:[h+1,` of `,m]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,disabled:h>=m-1,onClick:()=>d(h+1),children:`Forward`})]})]})]})}function dl({pipeline:e,job:t,edit:n,offer:r}){let i=to(),{list:o}=no(),[c,l]=(0,Z.useState)(!1),u=J().filterSetsOf(e.jobs[t]),d=new Set(u.flatMap(e=>e.set?[e.set.id]:[])),f=new Map((o??[]).map(e=>[e.id,e])),p=(t,i)=>{let a=e;n(t),r({label:i,restore:()=>n(a)})},m=u.flatMap(e=>e.set?[e]:[]),h=u.flatMap(e=>e.own?[e]:[]),g=n=>{let r=n.set.id,i=f.get(r)?.versions;return[{label:`Open in Library`,icon:ht,onSelect:()=>so(r)},n.pin==null?{label:`Pin`,icon:yn,disabled:i==null,onSelect:()=>p(oe(e,t,n.id,i),`Pinned ${n.set.name||r}`)}:{label:`Follow latest`,icon:vn,onSelect:()=>p(oe(e,t,n.id,null),`${n.set.name||r} follows the latest`)},{label:`Unlink`,icon:Tn,onSelect:()=>p(ee(e,t,n.id),`Unlinked ${n.set.name||r}`)}]};return(0,$.jsxs)(`div`,{className:`filter-sets`,id:`job-${t}-filters`,children:[m.length>0&&(0,$.jsx)(q,{className:`eg-links`,cards:!0,label:`Linked filter sets`,rows:m,rowKey:e=>e.id,columns:[{key:`set`,header:`Filter set`,role:`title`,sort:e=>(e.set.name||``).toLowerCase(),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`eg-open`,onClick:()=>so(e.set.id),children:e.set.name||e.set.id})},{key:`follows`,header:`Version`,sort:e=>e.pin??0,cell:e=>e.pin==null?`Follow latest`:`Pinned v${e.pin}`},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${e.set.name||e.set.id} actions`,actions:g(e)})}]}),h.map((c,l)=>(0,$.jsx)(fl,{id:`job-${t}-private-${c.id}`,label:h.length>1?`Private filter set ${l+1}`:`Private filter set`,body:c.own,set:r=>n(a(e,t,c.id,r)),onRemove:()=>p(ee(e,t,c.id),`Removed the private filter set`),onRemovedFilter:(i,o)=>r({label:i,restore:()=>n(a(e,t,c.id,{...c.own,filters:[...o]}))}),onMoveToLibrary:async()=>{let n=ml(`Filters`,new Set((o??[]).map(e=>e.name))),r=await i.create(n,c.own);p(s(e,t,c.id,{id:r.id,name:r.name}),`Moved ${n} to the Library`)}},c.id)),(0,$.jsxs)(`button`,{type:`button`,className:`ghost eg-add`,onClick:()=>l(!0),children:[(0,$.jsx)(_.add,{size:14,"aria-hidden":`true`}),`Link filter set`]}),c&&(0,$.jsx)(pl,{list:o??[],linkedIds:d,onLink:r=>{n(Ct(e,t,r)),l(!1)},onNew:async()=>{let r=await i.create(ml(`Untitled filter set`,new Set((o??[]).map(e=>e.name))));n(Ct(e,t,{id:r.id,name:r.name})),l(!1)},onPrivate:()=>{n(rt(e,t)),l(!1)},onClose:()=>l(!1)})]})}function fl({id:e,label:t,body:n,set:r,onRemove:i,onRemovedFilter:a,onMoveToLibrary:o}){let s=[{label:`Move to Library`,icon:dn,onSelect:()=>void o()},{label:`Remove`,icon:_.remove,destructive:!0,onSelect:i}];return(0,$.jsx)(X,{id:e,title:t,level:3,note:`${n.filters.length} ${n.filters.length===1?`filter`:`filters`}`,about:`A filter set of this pipeline's own, defined here rather than in the Library.`,actions:s,children:(0,$.jsx)(Ho,{label:t,filters:n.filters,onChange:e=>r({...n,filters:e}),onRemoved:a})})}function pl({list:e,linkedIds:t,onLink:n,onNew:r,onPrivate:i,onClose:a}){let o=[...e].sort((e,t)=>(t.updated||``).localeCompare(e.updated||``));return(0,$.jsxs)(Tt,{title:`Link filter set`,onClose:a,children:[o.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No filter sets yet.`}):(0,$.jsx)(`ul`,{className:`pick-list`,"aria-label":`Filter sets`,children:o.map(e=>{let r=t.has(e.id);return(0,$.jsx)(`li`,{children:(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,disabled:r,onClick:()=>n({id:e.id,name:e.name}),children:[(0,$.jsx)(`span`,{children:e.name}),r&&(0,$.jsx)(`span`,{className:`pick-about`,children:`Linked`})]})},e.id)})}),(0,$.jsxs)(`div`,{className:`pick-others`,children:[(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>void r(),children:[(0,$.jsx)(_.add,{size:14,"aria-hidden":`true`}),`New filter set…`]}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,onClick:i,children:[(0,$.jsx)(_.add,{size:14,"aria-hidden":`true`}),`New private filter set`]})]})]})}function ml(e,t){if(!t.has(e))return e;for(let n=2;;n++)if(!t.has(`${e} ${n}`))return`${e} ${n}`}function hl({k:e,pipeline:t,sources:n,edit:r,offer:i,open:a,onToggle:o}){let[s,c]=(0,Z.useState)(!1),[u,d]=(0,Z.useState)(null),f=J(),p=t.jobs[e],m=e===0?f.contentOf(t):null,h=f.flowStepOf(p),g=f.sendsImage(p),v=f.tokensOf(p),y=f.CONTENT_TYPES,x=f.targetsOf(t).some((n,r)=>f.STEP_TYPES[f.targetStepOf(t,r,e)?.type??``]?.asks===`request`)||e===0&&!!y[m?.type??``]?.inline,S=[...e>0?[{kind:`reply`}]:[],...m?[{kind:`attach`}]:[],...h?[{kind:`flow`}]:[],...g?[{kind:`image`}]:[],...v.map(e=>({kind:`mapping`,name:e.name}))],C=(e,n)=>{let a=t;r(n),i({label:e,restore:()=>r(a)})},w=(e,n)=>r(b(f.withContent(t,e),0,n)),T=[...e===0&&!m?[{label:`Source…`,icon:_.add,onSelect:()=>c(!0)},{label:y.text?.label??`Text`,icon:_.add,onSelect:()=>w({type:`text`,text:``},!1)},{label:y.prompt?.label??`Prompt only`,icon:_.add,onSelect:()=>w({type:`prompt`},!1)}]:[],...!g&&!x?[{label:f.STEP_TYPES.attachImage?.label??`Image`,icon:_.add,onSelect:()=>r(b(t,e,!0))}]:[],{label:`Token mapping`,icon:_.add,onSelect:()=>d({mode:`add`})}],E=n=>{n.kind===`attach`?C(`Removed the content`,f.withContent(t,null)):n.kind===`image`?C(`Stopped sending the image`,b(t,e,!1)):n.kind===`mapping`&&C(`Removed {${n.name}}`,R(t,e,n.name))},D=u?.mode===`edit`?v.find(e=>e.name===u.name):void 0,O=n=>{let a=t;u?.mode===`add`?r(je(a,e,n.name,n.type)):D&&(r(kt(a,e,D.name,n)),n.type!==D.type&&D.value&&i({label:`Changed {${D.name}} to ${f.TOKEN_TYPES[n.type]?.label.toLowerCase()??n.type}`,restore:()=>r(a)})),d(null)},k=(i,a)=>{switch(i.kind){case`reply`:return[{key:`reply`,label:`${f.jobLabel(t,e-1)} reply`,locked:!0,control:{type:`static`,text:`Each target's ${At(f.outOf(t.jobs[e-1]).kind)}`}}];case`attach`:return[m?.type===`text`?{key:`attach`,label:f.STEP_TYPES.attachContent?.label??`Attach Content`,required:!0,actions:a,control:{type:`textarea`,value:m.text,placeholder:`The item every target is sent`,onChange:e=>r(f.withContent(t,{...m,text:e}))}}:{key:`attach`,label:f.STEP_TYPES.attachContent?.label??`Attach Content`,required:!0,actions:a,control:{type:`static`,text:vl(m,n)}}];case`flow`:return[{key:`flow`,label:f.STEP_TYPES.flowStep?.label??`Flow step`,required:!0,actions:a,control:{type:`static`,text:h?.step??``}},{key:`api`,label:`API`,required:!0,control:{type:`select`,value:h?.api??``,onChange:n=>r({...t,jobs:t.jobs.map((t,r)=>r===e?{...t,steps:t.steps.map(e=>e.type===`flowStep`?{...e,api:n}:e)}:t)}),options:Object.entries(f.HTTP_APIS).map(([e,t])=>({value:e,label:t.label}))}}];case`image`:return[{key:`image`,label:f.STEP_TYPES.attachImage?.label??`Attach Image`,required:!0,actions:a,control:{type:`static`,text:`Each item`}}];case`mapping`:{let n=v.find(e=>e.name===i.name),o=f.TOKEN_TYPES[n.type],s=o?.fields??[];return[{key:`mapping-${n.name}`,label:o?o.notation(n.name):`{${n.name}}`,code:!0,required:!0,control:s.includes(`value`)?{type:`text`,value:n.value??``,onChange:i=>r(ce(t,e,n.name,{value:i}))}:{type:`static`,text:o?.label??n.type},...s.includes(`enabled`)?{enabled:{checked:!!n.enabled,onChange:i=>r(ce(t,e,n.name,{enabled:i}))}}:{},actions:[{label:`Edit…`,icon:_.edit,onSelect:()=>d({mode:`edit`,name:n.name})},...a]}]}}},A=t=>t.kind===`reply`?`Job ${e+1} reply`:t.kind===`flow`?`Job ${e+1} flow step`:t.kind===`mapping`?`{${t.name}}`:t.kind===`attach`?`Content`:`Image`,j=S.map(r=>gl[r.kind](t,e,n)).join(`, `)||`none`;return(0,$.jsxs)(X,{id:`job-${e}-content`,title:`Content`,level:3,note:j,open:a,onToggle:o,children:[(0,$.jsx)(pt,{noun:`content`,items:S,onAdd:T,onRemove:e=>E(S[e]),locked:e=>e.kind===`reply`||e.kind===`flow`,extra:e=>e.kind===`attach`?[{label:`Change content`,icon:_.rename,onSelect:()=>c(!0)}]:[],empty:(0,$.jsx)(`p`,{className:`note`,children:`No content`}),renderItem:(t,n,r)=>(0,$.jsx)(`div`,{className:`stage-row`,id:t.kind===`mapping`?void 0:`job-${e}-${t.kind}`,children:(0,$.jsx)(W,{label:A(t),rows:k(t,r)})},t.kind===`mapping`?`m-${t.name}`:t.kind)}),u&&(u.mode===`add`||D)&&(0,$.jsx)(Ce,{title:D?f.TOKEN_TYPES[D.type]?.notation(D.name)??`{${D.name}}`:`New mapping`,initial:D?{name:D.name,type:D.type}:{name:``,type:`value`},problem:n=>l(t,e,n,D?.name),onSave:O,onClose:()=>d(null)},u.mode===`edit`?u.name:`add`),s&&(0,$.jsxs)(Tt,{title:m?`Change content`:`Add content`,onClose:()=>c(!1),children:[(0,$.jsx)(Ec,{mode:`pick`,onPick:e=>{w({type:`source`,ref:xe(e.id,e.name),first:null,loops:1},f.sourceTypeOf(e)?.sendsImage??!1),c(!1)}}),(0,$.jsxs)(`div`,{className:`pick-others`,children:[(0,$.jsx)(`h2`,{className:`shead-title`,children:`Without a Source`}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>{w({type:`text`,text:``},!1),c(!1)},children:[(0,$.jsx)(`span`,{children:y.text?.label??`Text`}),y.text?.description&&(0,$.jsx)(`span`,{className:`pick-about`,children:y.text.description})]}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>{w({type:`prompt`},!1),c(!1)},children:[(0,$.jsx)(`span`,{children:y.prompt?.label??`Prompt only`}),y.prompt?.description&&(0,$.jsx)(`span`,{className:`pick-about`,children:y.prompt.description})]})]})]})]})}var gl={reply:(e,t)=>`${J().jobLabel(e,t-1)}, ${At(J().outOf(e.jobs[t-1]).kind)}`,attach:(e,t,n)=>vl(J().contentOf(e),n),flow:(e,t)=>J().flowStepOf(e.jobs[t])?.step??``,image:()=>`image`,mapping:()=>`token mapping`},_l=(e,t)=>{let n=t.find(t=>t.id===e.ref.id);return n?`${n.name}, ${n.files} ${n.files===1?`file`:`files`}`:`A Source that is gone: ${e.ref.name}`},vl=(e,t)=>e?e.type===`source`?_l(e,t):J().CONTENT_TYPES[e.type]?.label??e.type:`none`;function yl({index:e}){return(0,$.jsx)(`span`,{className:`target-badge`,"aria-hidden":`true`,children:J().targetLetter(e)})}var bl=e=>e instanceof Error?e.message:String(e);function xl({pipeline:e,title:t,onClose:n}){let r=Q(),[i,a]=(0,Z.useState)(null),[o,s]=(0,Z.useState)(null),c=J().contentOf(e),l=c?.type===`source`?c.ref.id:null,u=(0,Z.useCallback)(()=>{if(s(null),!l){a([]);return}(async()=>{try{let t=(await r.source(l)).files.filter(e=>e.name.toLowerCase().endsWith(`.json`));a(await Promise.all(t.map(async t=>{let n=JSON.parse(await $s(await r.file(l,t.name)));return{name:t.name,reply:J().recordedReplyOf(e,n)}})))}catch(e){s(bl(e))}})()},[r,l,e]);return(0,Z.useEffect)(u,[u]),(0,$.jsx)(Tt,{title:t,wide:!0,className:`edit`,onClose:n,children:i?i.length?(0,$.jsx)(`div`,{className:`recorded-replies`,children:i.map(e=>(0,$.jsxs)(`section`,{"aria-label":e.name,children:[(0,$.jsx)(`h3`,{children:e.name}),(0,$.jsx)(`pre`,{children:e.reply??`(no recorded reply)`})]},e.name))}):(0,$.jsx)(`p`,{className:`note`,children:`No calls.`}):(0,$.jsx)(M,{label:`Replies`,error:o,onRetry:u})})}function Sl({k:e,pipeline:t,profiles:n,targetCap:r,onRequestRename:i,onAdd:a,onClone:o,onRemove:s,onProfile:c,onEcho:l,onPrompt:u,onStep:d,onSelectPrompt:f,onClearPrompt:p,onSaveToLibrary:m,onCopyRequest:h,defaultPrompt:g,onRevert:_,say:v=()=>{}}){let y=J(),[b]=On(`promptlab.versions`),x=e=>b.profile?.[e]?.[0]?.at??null,S=y.targetsOf(t),C=S.length,[w,T]=(0,Z.useState)(null);return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(X,{id:`job-${e}-targets`,title:`Targets`,level:3,note:`${C} ${C===1?`target`:`targets`}`,children:(0,$.jsx)(pt,{noun:`target`,items:S,min:1,max:r,onAdd:a,onClone:o,onRemove:s,onRename:i,extra:(n,r)=>_e(y.targetStepOf(t,r,e)?.type).actions?.({pipeline:t,i:r,k:e,say:v,onSelectPrompt:()=>f(r),onClearPrompt:()=>p(r),onSaveToLibrary:()=>m(r),onCopyRequest:()=>h(r),onPreviewReplies:()=>T(r),defaultPrompt:g,onRevert:_&&(()=>_(r))})??[],empty:(0,$.jsx)(`p`,{className:`note`,children:`No targets`}),renderItem:(r,i,a)=>{let o=K(t,i),s=y.targetStepOf(t,i,e)??{type:`prompt`,prompt:``},f=_e(s.type),p=y.STEP_TYPES[s.type],m=e>0&&!!p?.firstJobOnly,h=!!y.CONNECTION_TYPES[p?.replaces??``]?.replays,g=!m&&h?ct(t,e):null;return(0,$.jsxs)(`div`,{className:`target`,id:`job-${e}-target-${i}`,children:[(0,$.jsxs)(`div`,{className:`target-head`,children:[(0,$.jsx)(yl,{index:i}),(0,$.jsx)(`h4`,{className:`target-name`,children:o}),m&&(0,$.jsx)(`span`,{className:`target-idle`,children:`Job 1 only`}),(0,$.jsx)(Ue,{actions:a,label:`${o} actions`})]}),!m&&h&&(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`p`,{className:`note`,children:p?.description??`Replays the recorded reply. Sends nothing.`}),g!=null&&(0,$.jsxs)(`div`,{className:`prompt`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:`Prompt`}),(0,$.jsx)(et,{words:g,showExprs:!1})]})]}),!m&&!h&&(0,$.jsx)(Ze,{label:o,profile:e===0?{type:`picker`,kind:`Profile`,value:wl(s)?Cl:r.profile?.id??``,usedLabel:`Last changed`,...ga(El(n,r.profile),x,Tl()),onChange:e=>e===Cl?l(i):c(i,Dl(n,e))}:{type:`picker`,kind:`Profile`,value:s.profile?.id??``,usedLabel:`Last changed`,...ga(El(n,s.profile),x,[{id:``,label:`Same as ${y.jobLabel(t,0)}`}]),onChange:e=>c(i,e?Dl(n,e):null)},prompt:s.prompt,onPrompt:e=>u(i,e),rows:f.rows?.(s,e=>d(i,e))??[],request:f.request?.(s)??[],requestId:`job-${e}-target-${i}-request`,promptLabel:f.promptLabel?.(s)??`Prompt`,words:f.words?.(s),fields:f.fields?.(s)})]},`${i}-${e}`)}})}),w!==null&&(0,$.jsx)(xl,{pipeline:t,title:`${K(t,w)} replies`,onClose:()=>T(null)})]})}var Cl=`#echo`,wl=e=>!!J().STEP_TYPES[e.type]?.local,Tl=()=>Object.entries(J().STEP_TYPES).filter(([,e])=>e.slot===`target`&&e.local&&!J().CONNECTION_TYPES[e.replaces??``]?.replays).map(([,e])=>({id:Cl,label:e.label??`Echo`})),El=(e,t)=>[...t?.id&&!e.some(e=>e.id===t.id)?[{id:t.id,name:`${t.name||t.id} (missing)`}]:[],...e.map(e=>({id:e.id,name:e.name}))],Dl=(e,t)=>{let n=e.find(e=>e.id===t);return n?{id:n.id,name:n.name}:null};function Ol({text:e,writtenFor:t,say:n,onClose:r}){let i=async()=>{n(await dt(e)?`Copied the request.`:`Could not copy.`),r()};return(0,$.jsx)(Ot,{title:`Copy request`,wide:!0,onClose:r,sections:[{key:`request`,rows:[{key:`for`,label:`Written for`,control:{type:`static`,text:t}},{key:`body`,label:`Request`,control:{type:`code`,text:e}}]}],actions:[{label:`Copy`,icon:we,primary:!0,onSelect:()=>void i()}]})}function kl({pipeline:t,profiles:r,sources:i,targetCap:a,prompt:o=``,defaultPrompt:s,protectedTerms:c,edit:l,offer:u}){let[d,f]=(0,Z.useState)(null),[p,m]=(0,Z.useState)(null),[g,_]=(0,Z.useState)(null),[v,y]=(0,Z.useState)(null),[b,x]=(0,Z.useState)(null),[S,C]=(0,Z.useState)({}),T=e=>S[e]??!1,E=e=>t=>C(n=>({...n,[e]:t})),D=nr(),k=()=>{let e=J().contentOf(t),n=e?.type===`source`?e.ref.id:null,r=n?i.find(e=>e.id===n)?.platform:void 0;return r&&J().WORKFLOW_PLATFORMS[r]||null},j=(e,n)=>{let r=t;l(n),u({label:e,restore:()=>l(r)})},M=e=>{let t=r.find(t=>t.id===e);return t?J().CONNECTION_TYPES[J().typeOf(t)]?.answers??[`prompt`]:void 0},P=!J().targetsOf(t).length;return(0,$.jsxs)(`div`,{className:`jobs`,children:[b&&(0,$.jsx)(`p`,{className:`note`,role:`status`,children:b}),(0,$.jsx)(pt,{noun:`job`,items:t.jobs,min:1,addDisabled:P,onAdd:()=>l(Ee(t)),onClone:e=>l(Pe(t,e)),onRemove:e=>j(`Removed ${J().jobLabel(t,e)}`,N(t,e)),onRename:e=>f({job:e}),renderItem:(n,d,p)=>{let g=`Job ${d+1}${n.name?.trim()?` - ${n.name.trim()}`:``}`,v=J().flowStepOf(n)?J().readAsOf(n):void 0;return(0,$.jsxs)(Z.Fragment,{children:[d>0&&(0,$.jsxs)(`div`,{className:`job-reply`,"aria-hidden":`true`,children:[(0,$.jsx)(Ye,{size:16}),(0,$.jsx)(`span`,{children:`Reply`})]}),(0,$.jsx)(X,{id:`job-${d}`,title:g,level:2,actions:p,note:Al(t,d),children:(0,$.jsxs)(`div`,{className:`stages`,children:[(0,$.jsx)(hl,{k:d,pipeline:t,sources:i,edit:l,offer:u,open:T(`job-${d}-content`),onToggle:E(`job-${d}-content`)}),(0,$.jsx)(Sl,{k:d,pipeline:t,profiles:r,targetCap:a,onRequestRename:e=>f({job:d,scenario:e}),onAdd:()=>l(Je(t,Et(r),o)),onClone:e=>l(he(t,e)),onRemove:e=>j(`Removed ${K(t,e)}`,h(t,e)),onProfile:(e,n)=>l(ue(t,e,d,n,M)),onEcho:e=>l(fe(t,e)),onPrompt:(e,n)=>l(ot(t,e,d,n)),onStep:(e,n)=>l(jl(t,e,d,n)),say:x,onSelectPrompt:e=>m({i:e,k:d}),onClearPrompt:e=>j(`Cleared ${K(t,e)}'s prompt`,ot(t,e,d,``)),onSaveToLibrary:e=>_({i:e,k:d}),onCopyRequest:e=>y({i:e,k:d}),...d===0&&s?{defaultPrompt:s.text,onRevert:e=>j(`Reverted ${K(t,e)} to the default`,I(t,e,0,s.text,{id:s.id,name:Yn(s)}))}:{}}),(0,$.jsx)(e,{id:`job-${d}-responses`,open:T(`job-${d}-responses`),onToggle:E(`job-${d}-responses`),out:J().outOf(n),format:!!J().replyOf(n),readAs:v,outputKinds:xt(),modifiers:ge(),onKind:e=>l(ie(t,d,e)),onSetting:(e,n)=>l(Ml(t,d,t=>({...t,[e]:n}))),onAddFormat:()=>l(_t(t,d)),onRemoveFormat:()=>j(`Removed the format validation`,A(t,d)),onReadAs:e=>e==null?j(`Removed Read as`,w(t,d,null)):l(w(t,d,e)),...c?{protectedTerms:c}:{},offer:u,onModifier:(e,n)=>l(Nl(t,d,e,n)),onAddModifier:e=>l(Ml(t,d,t=>({...t,modifiers:[...t.modifiers,e]}))),onRemoveModifier:e=>j(`Removed a modifier`,Ml(t,d,t=>({...t,modifiers:t.modifiers.filter((t,n)=>n!==e)}))),children:(0,$.jsx)(dl,{pipeline:t,job:d,edit:l,offer:u})})]})})]},n.id||d)}}),P&&(0,$.jsx)(`p`,{className:`note add-why`,children:`Add a target first.`}),d&&`scenario`in d?(0,$.jsx)(nt,{title:`Rename ${K(t,d.scenario)}`,value:J().targetsOf(t)[d.scenario]?.name??``,onCommit:e=>l(V(t,d.scenario,e)),onClose:()=>f(null)}):d?(0,$.jsx)(nt,{title:`Rename ${J().jobLabel(t,d.job)}`,value:t.jobs[d.job]?.name??``,onCommit:e=>l(O(t,d.job,e)),onClose:()=>f(null)}):null,p&&(0,$.jsx)(ul,{onClose:()=>m(null),used:{byScenario:We(t,p.i),byAll:n(t)},onPick:e=>{let n=t;l(I(n,p.i,p.k,e.text,{id:e.id,name:Yn(e)})),m(null),u({label:`Picked ${Yn(e)}`,restore:()=>l(n)})}}),g&&(()=>{let{i:e,k:n}=g,r=J().targetStepOf(t,e,n)?.prompt??``;return(0,$.jsx)(nt,{title:`Save to library`,value:r.split(`
10
+ `)[0]?.trim()??``,onCommit:i=>void(async()=>{let a=await D.create({name:i,text:r});l(I(t,e,n,r,{id:a.id,name:Yn(a)})),x(`Saved ${Yn(a)} to the library.`)})(),onClose:()=>_(null)})})(),v&&(()=>{let e=J().targetStepOf(t,v.i,v.k);if(!e)return null;let{text:n,writtenFor:r}=Re(e,k());return(0,$.jsx)(Ol,{text:n,writtenFor:r,say:x,onClose:()=>y(null)})})()]})}function Al(e,t){let n=J(),r=r=>e.jobs[t].steps.filter(e=>n.STEP_TYPES[e.type]?.slot===r).length,i=e.jobs[t],a=i.steps.filter(e=>e.type!==`tokenMappings`&&n.STEP_TYPES[e.type]?.slot===`content`).length+n.tokensOf(i).length;return`Content ${t>0&&!a?`Reply`:a} → Targets ${n.targetsOf(e).length} → Responses ${r(`responses`)}`}var jl=(e,t,n,r)=>J().withTarget(e,t,n,e=>{let t={...e,...r};for(let[e,n]of Object.entries(r))n===``&&e!==`prompt`&&e!==`system`&&delete t[e];return t}),Ml=(e,t,n)=>({...e,jobs:e.jobs.map((e,r)=>r===t?J().withOut(e,n(J().outOf(e))):e)}),Nl=(e,t,n,r)=>Ml(e,t,e=>({...e,modifiers:e.modifiers.map((e,t)=>t===n?r:e)}));function Pl({pipeline:e,edit:t,offer:n,children:r,profiles:i=[],labGrader:a=null}){let o=Kn(),{list:s}=qn(),[c,l]=(0,Z.useState)(!1),[u,d]=(0,Z.useState)(null),p=e.evals,m=J().contentOf(e),h=m?.type===`source`?m.ref.id:null,[,g]=(0,Z.useState)(0);(0,Z.useEffect)(()=>o.subscribe(()=>g(e=>e+1)),[o]),(0,Z.useEffect)(()=>{for(let e of p)e.group&&o.load(e.group.id)},[p,o]);let y=(r,i)=>{let a=e;t(r),n({label:i,restore:()=>t(a)})},b=e=>o.entry(e)?.doc?.body??null,x=e=>!!e?.source&&e.source.id!==h,C=new Map((s??[]).map(e=>[e.id,e])),w=new Set(p.flatMap(e=>e.group?[e.group.id]:[])),T=p.flatMap((e,t)=>e.group?[{t:e,j:t}]:[]),D=p.flatMap((e,t)=>e.own?[{t:e,j:t}]:[]),O=({t,j:n})=>{let r=t.group.id,i=C.get(r)?.versions;return[{label:`Open in Library`,icon:ht,onSelect:()=>ca(r)},t.pin==null?{label:`Pin`,icon:yn,disabled:i==null,onSelect:()=>y(E(e,n,i),`Pinned ${t.group.name||r}`)}:{label:`Follow latest`,icon:vn,onSelect:()=>y(E(e,n,null),`${t.group.name||r} follows the latest`)},{label:`Unlink`,icon:Tn,onSelect:()=>y(Ve(e,n),`Unlinked ${t.group.name||r}`)}]};return(0,$.jsxs)(X,{id:`evals`,title:`Evals`,level:2,note:p.length?String(p.length):`None`,children:[p.length===0&&(0,$.jsx)(`p`,{className:`note evals-empty`,children:`No evals — the run is unscored.`}),p.length>0&&(0,$.jsx)(Il,{pass:e.pass,count:p.length,edit:n=>t(v(e,n))}),T.length>0&&(0,$.jsx)(q,{className:`eg-links`,cards:!0,label:`Linked eval groups`,rows:T,rowKey:e=>e.t.id,columns:[{key:`group`,header:`Eval group`,role:`title`,sort:e=>(e.t.group.name||``).toLowerCase(),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`eg-open`,onClick:()=>ca(e.t.group.id),children:e.t.group.name||e.t.group.id})},{key:`source`,header:`Source`,sort:e=>b(e.t.group.id)?.source?.name??``,tdProps:e=>({"data-state":b(e.t.group.id)?.source?void 0:`none`}),cell:e=>{let t=b(e.t.group.id);return t?t.source?x(t)?(0,$.jsxs)(`span`,{className:`eval-flag`,title:`Grades ${t.source.name}, not this run's content`,children:[(0,$.jsx)(sn,{size:12,"aria-hidden":`true`}),t.source.name]}):t.source.name:`Any`:`…`}},{key:`follows`,header:`Version`,sort:e=>e.t.pin??0,cell:e=>e.t.pin==null?`Follow latest`:`Pinned v${e.t.pin}`},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${e.t.group.name||e.t.group.id} actions`,actions:O(e)})}]}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost eg-add`,onClick:()=>l(!0),children:[(0,$.jsx)(_.add,{size:14,"aria-hidden":`true`}),`Link eval group`]}),r,D.map(({t:n,j:r})=>(0,$.jsx)(Fl,{ev:n,label:J().evalLabel(e,r),renaming:u===r,onRename:()=>d(r),profiles:i,labGrader:a,rawReply:e.jobs.some(e=>J().readAsOf(e)!=null),casesBody:n.own.casesFrom?b(n.own.casesFrom.id):null,loadCases:()=>{n.own.casesFrom&&o.load(n.own.casesFrom.id)},set:n=>t(Mt(e,r,n)),undoableSet:(t,n)=>y(Mt(e,r,t),n),onCommitName:n=>{t(S(e,r,n)),d(null)},onCancelName:()=>d(null),onCopyCases:t=>y(bt(e,r,t),`Copied ${J().evalLabel(e,r)}'s cases in`),onMoveToLibrary:async()=>{let t=Rl((n.name||``).trim()||J().evalLabel(e,r),new Set((s??[]).map(e=>e.name))),{casesFrom:i,...a}=n.own,c=await o.create(t,a);y(f(e,r,{id:c.id,name:c.name}),`Moved ${t} to the Library`)},onRemove:()=>y(Ve(e,r),`Removed ${J().evalLabel(e,r)}`)},n.id)),c&&(0,$.jsx)(Ll,{list:s??[],linkedIds:w,contentSourceId:h,onLink:n=>{t(G(e,n)),l(!1)},onNew:async()=>{let n=await o.create(Rl(`Untitled eval group`,new Set((s??[]).map(e=>e.name))));t(G(e,{id:n.id,name:n.name})),l(!1)},onClose:()=>l(!1)})]})}function Fl({ev:e,label:t,renaming:n,onRename:r,profiles:i,labGrader:a,rawReply:o,casesBody:s,loadCases:c,set:l,undoableSet:u,onCommitName:d,onCancelName:f,onCopyCases:p,onMoveToLibrary:m,onRemove:h}){let g=J(),[v]=On(`promptlab.versions`),y=e.own;(0,Z.useEffect)(()=>{y.casesFrom&&c()},[y.casesFrom,c]);let b=g.isWholeRun(g.EVAL_TYPES.group,e),x=b?y.run:y.every,S=t=>l({...e,own:{...y,...t}}),C=t=>({...e,own:{...y,...b?{run:t}:{every:t}}}),w=x.some(e=>g.METRICS[e.type]?.graded),T=i.filter(e=>(g.CONNECTION_TYPES[e.type??``]?.answers??[`prompt`]).includes(`prompt`)),{mode:E,threshold:D}=y.scoring,O=[{label:`Rename…`,icon:_.rename,onSelect:r},...y.casesFrom?[{label:`Copy cases in`,icon:we,disabled:!s,onSelect:()=>s&&p(s.cases)}]:[],{label:`Move to Library`,icon:dn,onSelect:()=>void m()},{label:`Remove`,icon:_.remove,destructive:!0,onSelect:h}],k=[{key:`mode`,label:`Scored`,control:{type:`select`,value:E,options:[{value:`all`,label:`All must pass`},{value:`weighted`,label:`Weighted points`}],onChange:e=>S({scoring:{mode:e===`weighted`?`weighted`:`all`,threshold:e===`weighted`?D??1:D}})}},...E===`weighted`?[{key:`threshold`,label:`Pass at`,control:{type:`text`,size:`num`,value:D==null?``:String(D),onChange:e=>S({scoring:{mode:E,threshold:e.trim()?/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e:null}})}}]:[],...y.casesFrom?[]:[{key:`over`,label:`Reads`,control:{type:`select`,value:b?`run`:`item`,options:[{value:`item`,label:`Each item`},{value:`run`,label:`Whole run`}],onChange:e=>S(e===`run`?{every:[],run:[...y.every,...y.run]}:{every:[...y.every,...y.run],run:[]})}}],...y.casesFrom?[{key:`cases-from`,label:`Cases from`,control:{type:`static`,text:y.casesFrom.name||y.casesFrom.id}}]:[],...w||y.grader?[{key:`grader`,label:`Grader`,control:{type:`picker`,kind:`Profile`,value:y.grader?.id??``,usedLabel:`Last changed`,...ga(T,e=>v.profile?.[e]?.[0]?.at??null,[{id:``,label:a?`Lab (${a.name})`:`None`}]),onChange:e=>{let t=i.find(t=>t.id===e);S({grader:t?{id:t.id,name:t.name}:null})}}}]:[]];return(0,$.jsxs)(X,{id:`eval-${e.id}`,title:t,level:3,note:g.EVAL_TYPES.group?.label??`Eval group`,about:`A group of this pipeline's own, defined here rather than in the Library.`,actions:O,...n?{rename:{value:e.name||t,onCommit:d,onCancel:f}}:{},children:[(0,$.jsx)(W,{label:t,rows:k}),(0,$.jsx)(ma,{label:t,metrics:x,over:b?`run`:`item`,rawReply:o,set:e=>l(C(e)),undoable:(e,t)=>u(C(e),t)}),(0,$.jsx)(W,{label:`${t} on failure`,rows:[{key:`continue`,label:`Continue on failure`,control:{type:`checkbox`,checked:e.continueOnFailure,onChange:t=>l({...e,continueOnFailure:t})}}]})]})}function Il({pass:e,count:t,edit:n}){let r=e.mode===`atLeast`?e.count:t,i=[{key:`pass`,label:`Pass when`,control:{type:`select`,value:e.mode,options:[{value:`all`,label:`All groups pass`},{value:`atLeast`,label:`At least`}],onChange:e=>n(e===`atLeast`?{mode:`atLeast`,count:Math.min(Math.max(r,1),t)}:{mode:`all`})}},...e.mode===`atLeast`?[{key:`count`,label:`Groups`,control:{type:`text`,size:`num`,value:String(e.count),onChange:e=>{let t=Number(e);n({mode:`atLeast`,count:Number.isInteger(t)&&t>0?t:e.trim()})}}}]:[]];return(0,$.jsx)(W,{label:`Pass rule`,rows:i})}function Ll({list:e,linkedIds:t,contentSourceId:n,onLink:r,onNew:i,onClose:a}){let o=Kn(),[,s]=(0,Z.useState)(0);(0,Z.useEffect)(()=>o.subscribe(()=>s(e=>e+1)),[o]),(0,Z.useEffect)(()=>{for(let t of e)o.load(t.id)},[e,o]);let c=e=>{let t=o.entry(e)?.doc?.body;return t?!t.source||t.source.id===n:null};return(0,$.jsxs)(Tt,{title:`Link eval group`,onClose:a,children:[e.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No eval groups yet.`}):(0,$.jsx)(`ul`,{className:`pick-list`,"aria-label":`Eval groups`,children:e.map(e=>{let n=t.has(e.id),i=c(e.id),a=n?`Linked`:i===!1?`Another Source`:i?`Grades this content`:``;return(0,$.jsx)(`li`,{children:(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,disabled:n,onClick:()=>r({id:e.id,name:e.name}),children:[(0,$.jsx)(`span`,{children:e.name}),a&&(0,$.jsx)(`span`,{className:`pick-about`,children:a})]})},e.id)})}),(0,$.jsx)(`div`,{className:`pick-others`,children:(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>void i(),children:[(0,$.jsx)(_.add,{size:14,"aria-hidden":`true`}),`New eval group…`]})})]})}function Rl(e,t){if(!t.has(e))return e;for(let n=2;;n++)if(!t.has(`${e} ${n}`))return`${e} ${n}`}function zl({pipeline:e,sourceFiles:t,graded:n,openCaseEditor:r}){let[i,a]=(0,Z.useState)({graded:!1,todo:!0,missing:!0});if((J().evalsDataset(e)?.id??null)===null)return null;let o=t??[],s=new Set(n.map(e=>e.item)),c=new Set(o),l=[{key:`graded`,label:`Graded`,tone:`ok`,files:o.filter(e=>s.has(e))},{key:`todo`,label:`To do`,tone:`todo`,files:o.filter(e=>!s.has(e))},{key:`missing`,label:`Missing`,tone:`missing`,files:n.filter(e=>!c.has(e.item)).map(e=>e.item)}];l[0].files.length;let u=l[1].files.length+l[2].files.length,d=l.filter(e=>i[e.key]).flatMap(e=>e.files.map(t=>({file:t,tone:e.tone})));return(0,$.jsx)(X,{id:`mapping`,title:`Mapping`,level:3,note:f(),about:`Which of the content's items the dataset has a case for, and so which are graded.`,children:o.length?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`div`,{className:`pills mkey`,role:`group`,"aria-label":`Show`,children:l.filter(e=>e.files.length).map(e=>(0,$.jsxs)(`label`,{className:`pill`,children:[(0,$.jsx)(`input`,{type:`checkbox`,checked:i[e.key],onChange:()=>a({...i,[e.key]:!i[e.key]})}),(0,$.jsxs)(`span`,{children:[e.label,` `,e.files.length]})]},e.key))}),d.length?(0,$.jsx)(`div`,{className:`mchips`,children:d.map(({file:e,tone:t})=>(0,$.jsx)(`button`,{type:`button`,className:`mchip ${t}`,onClick:()=>r(e),children:e},`${t}-${e}`))}):(0,$.jsx)(`p`,{className:`note`,children:u?`Nothing shown.`:`Every content item is graded.`})]}):(0,$.jsx)(`p`,{className:`note`,children:`No items in the content to map against.`})});function f(){let r=J().contentOf(e),i=r?J().CONTENT_TYPES[r.type]:void 0;if(i?.inline)return i.label;let a=t??[],o=new Set(n.map(e=>e.item)),s=a.filter(e=>o.has(e)).length;return`${Wr(n.length,`case`)}, ${s} of ${a.length} matched`}}function Bl({pipeline:e,edit:t,comment:n,setComment:r,onClear:i,onCopyFailures:a}){let o=[{label:`Clear comment`,icon:ze,destructive:!0,onSelect:i},{label:`Copy last run's failures`,icon:ft,onSelect:a}],s=J().contentOf(e),c=s?.type===`source`?[{key:`first`,label:`Run first`,control:{type:`text`,value:s.first==null?``:String(s.first),placeholder:`all`,size:`num`,clearable:!0,onChange:n=>t(J().withContent(e,{...s,first:n.trim()?Vl(n):null}))}},{key:`loops`,label:`Loop`,control:{type:`text`,value:String(s.loops),placeholder:`1`,onChange:n=>t(J().withContent(e,{...s,loops:Math.max(1,Vl(n)??1)}))}}]:[];return(0,$.jsx)(X,{id:`control`,title:`Run settings`,level:2,actions:o,children:(0,$.jsx)(W,{label:`Run settings`,rows:[...c,{key:`comment`,label:`Comment`,control:{type:`text`,value:n,placeholder:`A note to find this run by in History`,onChange:e=>r(e)}}]})})}var Vl=e=>{let t=Number.parseInt(e,10);return Number.isNaN(t)?null:t};function Hl(){let e=Dn(),t=Q(),n=ye(),{openRun:r}=Dr(),{openCaseEditor:i}=Ma(),[a,o]=On(`promptlab.workflows`),[s]=On(`promptlab.profiles`),[c,l]=On(`promptlab.comment`),[u]=On(`promptlab.versions`),[d,f]=(0,Z.useState)([]),{list:p,error:m}=qn(),{list:h,error:g}=rr(),_=h?.find(e=>e.default)??null,v=Kn(),y=(0,Z.useMemo)(()=>(p??[]).map(e=>xe(e.id,e.name)),[p]),b=(0,Z.useCallback)(()=>(p??[]).map(e=>{let t=v.entry(e.id)?.doc?.body;return{id:e.id,name:e.name,grader:t?t.grader??null:e.grader??null}}),[p,v]),[x,S]=(0,Z.useState)(4),[C,w]=(0,Z.useState)(void 0),[T,E]=(0,Z.useState)(null),[D,O]=(0,Z.useState)(null),k=(0,Z.useRef)(D);k.current=D;let[A,j]=(0,Z.useState)(!1),[N,P]=(0,Z.useState)(!1),[F,I]=(0,Z.useState)(null),L=(0,Z.useRef)(null),[R,z]=(0,Z.useState)(null),[B,ee]=(0,Z.useState)(!1),te=(0,Z.useRef)(null);(0,Z.useEffect)(()=>{let e=!0;return t.sources().then(t=>{e&&f(t.sources)}).catch(()=>{}),t.config().then(t=>{e&&S(t.targetCap)}).catch(()=>{}),()=>{e=!1}},[t]),(0,Z.useEffect)(()=>{let e=!0,n,r=async()=>{let i=5e3;try{let n=(await t.queue({limit:1})).runs[0]??null;if(!e)return;!L.current&&n&&!li(n.status)&&(L.current=n.id),n&&n.id===L.current&&(I(n),li(n.status)||(i=1e3))}catch{}e&&(n=setTimeout(()=>void r(),i))};return r(),()=>{e=!1,n&&clearTimeout(n)}},[t]);let V=(0,Z.useMemo)(()=>a.list.filter(e=>e.work?.version===14),[a.list]),H=V.find(e=>e.id===a.at)??V[0],U=H?.work,W=H?.id??``,G=U?J().evalsDataset(U)?.id??null:null,ne=Jn(G),re=ne?.doc?.body.cases,ie=(0,Z.useMemo)(()=>(re??[]).map(e=>({id:e.id??``,item:J().caseItem(e)})),[re]),ae=ne?.doc?.body.cases,oe=(0,Z.useMemo)(()=>[...new Set((ae??[]).flatMap(e=>(e.metrics??[]).filter(e=>!e.not&&e.weight!==0&&(e.type===`contains-all`||e.type===`contains-any`)).flatMap(e=>J().metricLines(e.values))))],[ae]),se=_?.text||U&&J().targetStepOf(U,J().targetsOf(U).length-1,0)?.prompt||``,ce=(0,Z.useRef)(s.list);ce.current=s.list;let le=V.length?null:p?.[0]??null,ue=p!==null||m!==null,de=h!==null||g!==null,fe=_?.text??``;(0,Z.useEffect)(()=>{if(V.length||!ue||!de)return;let e=Wl(ce.current,le?{id:le.id,name:le.name}:void 0,fe);o({list:[e],at:e.id})},[V.length,o,ue,de,le,fe]);let pe=()=>{let e=v.list().list?.[0];return e?{id:e.id,name:e.name}:void 0},K=(0,Z.useRef)(a);K.current=a;let me=(0,Z.useRef)(H);me.current=H;let he=(0,Z.useRef)(U);he.current=U;let ge=(0,Z.useRef)(null),q=(0,Z.useRef)(null),_e=(0,Z.useCallback)(()=>{q.current&&clearTimeout(q.current);let t=me.current?.id,n=he.current;t&&n&&(ge.current={id:t,doc:n},q.current=setTimeout(()=>{let t=ge.current;t&&Vn(e,`pipeline`,t.id,t.doc,`idle`)},Ln))},[e]);(0,Z.useEffect)(()=>()=>{q.current&&clearTimeout(q.current)},[]);let ve=(0,Z.useCallback)(e=>{let t=K.current,n=me.current.id,r={...t,at:n,list:t.list.filter(e=>e.work?.version===14).map(t=>t.id===n?{...t,work:e}:t)};K.current=r,o(r),_e(),E(null)},[o,_e]),Se=s.list[0]?.id??``;(0,Z.useEffect)(()=>{let e=he.current,t=me.current?.id;if(!e||!t||!Se)return;let n=be(e,Et(ce.current));if(n===e)return;let r=K.current,i={...r,list:r.list.map(e=>e.id===t?{...e,work:n}:e)};K.current=i,o(i)},[Se,W,o]),(0,Z.useEffect)(()=>{let t=ge.current;t&&t.id!==W&&(Vn(e,`pipeline`,t.id,t.doc,`idle`),ge.current=null,q.current&&=(clearTimeout(q.current),null))},[W,e]);let Ce=U?J().contentOf(U):null,we=Ce?.type===`source`?Ce.ref.id:null;(0,Z.useEffect)(()=>{if(we===null){w(void 0);return}let e=!0;return t.source(we).then(t=>{e&&w(t.files.map(e=>e.name))}).catch(()=>{}),()=>{e=!1}},[we,t]);let Te=(0,Z.useCallback)(e=>{let t=d.find(t=>t.id===e);return t?{name:t.name,type:t.type,files:e===we&&C?C:[]}:null},[d,we,C]),Ee=(0,Z.useMemo)(()=>{let e=s.list.find(e=>e.id===s.grader);return e?{id:e.id,name:e.name}:null},[s]),De=(0,Z.useMemo)(()=>{if(!U)return[];if(!s.list.length&&J().targetsOf(U).length)return[`add a Target profile first`];let e={profiles:e=>s.list.find(t=>t.id===e)??null,sources:Te,grader:Ee,...p?{groups:p}:{}};return J().validatePipeline(U,e)},[U,s.list,Te,p,Ee]),Oe=W?u.pipeline?.[W]??[]:[],ke=(0,Z.useCallback)(async()=>{let n=he.current,i=me.current.id;Vn(e,`pipeline`,i,n,`run`);let a=J().resolvePipeline(n,{profiles:e=>J().withSlugs(s.list).find(t=>t.id===e)??null,grader:Ee,groups:b(),...we!==null&&C!==void 0?{files:C}:{},comment:c?.text??``});j(!0),E(null),O(null);try{let e=await t.submit(a);L.current=e.run.id,I(e.run),r(e.run.id,{at:e.run.submittedAt,from:`runs`})}catch(e){E(e instanceof Error?e.message:String(e));let t=e instanceof Gt&&e.status===403&&e.body?.unshared;O(Array.isArray(t)?t:null),L.current=null,I(null)}finally{j(!1)}},[t,c,we,r,s.list,C,e,Ee,b]),Ae=(0,Z.useCallback)(async()=>{let e=k.current;if(e?.length)try{for(let n of e)await t.shareConnection(n.kind,n.id);O(null),E(null);let r=e.map(e=>e.name).join(`, `);n.offer({label:`Shared ${r} with this workspace`,restore:()=>void(async()=>{for(let n of e)try{await t.shareConnection(n.kind,n.id,!0)}catch{}})()})}catch(e){E(e instanceof Error?e.message:String(e))}},[t,n]),je=(0,Z.useCallback)(async()=>{let e=L.current;if(e)try{I((await t.cancel(e)).run)}catch{}},[t]),Me=(0,Z.useCallback)(async()=>{try{let e=(await t.queue({limit:1})).runs[0];if(!e)return;let n=(e.results??[]).filter(e=>(e.scenarios??[]).some(e=>Object.values(e?.scores??{}).some(J().failedScore))).map(e=>e.name??`an item`);n.length&&await navigator.clipboard?.writeText(n.join(`
11
+ `))}catch{}},[t]),Ne=(0,Z.useCallback)(async n=>{let r=he.current,i=me.current.id;Vn(e,`pipeline`,i,r,`run`);let a=J().resolvePipeline(r,{profiles:e=>J().withSlugs(s.list).find(t=>t.id===e)??null,grader:Ee,groups:b(),files:[n],comment:c?.text??``}),o=await t.submit(a),l=o.run;for(;l.status===`queued`||l.status===`running`;)await new Promise(e=>setTimeout(e,500)),l=await t.run(o.run.id);return((l.results??[]).find(e=>e.name===n)?.scenarios??[]).map(e=>e.res??null)},[t,c,s.list,e,Ee,b]),Pe=(0,Z.useCallback)(e=>{G&&i(e,{dataset:G,files:C??[],run:Ne})},[i,C,Ne,G]),Fe=(0,Z.useCallback)(()=>{let e=c?.text??``;l({text:``}),n.offer({label:`Cleared the comment`,restore:()=>l({text:e})})},[c,l,n]),Ie=(0,Z.useCallback)(()=>{let e=J().pipelineToYaml(he.current,{profiles:ce.current});aa(new Blob([e],{type:`text/yaml`}),ia(`pipeline`,he.current.name||`pipeline`,14,`yml`))},[]),Y=(0,Z.useCallback)(async e=>{let n=he.current,r={};for(let e of J().evalsOf(n)){let n=J().casesRef(e)?.id??null;if(n&&!r[n]){let e=await t.dataset(n);r[n]={name:e.name,body:e.body}}}let i=J().exportBundle(n,{profiles:ce.current,grader:Ee,datasets:r});if(`error`in i)throw Error(i.error);aa(await t.bundle({files:i.files,source:we,items:e}),ia(`ci`,n.name||`pipeline`,14,`zip`))},[t,we,Ee]),Le=(0,Z.useCallback)(async e=>{let n=await t.archivedRules().then(e=>e.rules,()=>({})),r=J().yamlToPipeline(e,{profiles:ce.current,profileType:e=>ce.current.find(t=>t.id===e)?.type,sources:d,groups:y,rulesFor:e=>n[e]??null});if(`error`in r){z(r);return}let{doc:i,missing:a}=r,s={id:Gl(),name:i.name||`Imported`,work:i},c={...K.current,at:s.id,list:[...V,s]};K.current=c,o(c),z({doc:i,missing:a})},[t,V,o,d,y]);if(!H||!U)return(0,$.jsx)(`div`,{className:`runs`,children:(0,$.jsx)(M,{label:`Pipelines`})});let Re=t=>{let n=me.current.id;Vn(e,`pipeline`,n,he.current,`restore`),ve(t.doc),P(!1)};return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(el,{list:V,at:W,canRestore:Oe.length>0,used:Object.fromEntries(V.map(e=>[e.id,u.pipeline?.[e.id]?.[0]?.at??null])),onSelect:e=>o({...K.current,at:e}),onNew:()=>{let e=Ul(V.map(e=>e.name));if(o({...K.current,list:[...V,e],at:e.id}),c?.text){let e=c.text;l({text:``}),n.offer({label:`Cleared the comment`,restore:()=>l({text:e})})}},onClone:()=>{let e={id:Gl(),name:`${H.name} copy`,work:structuredClone(U)};o({...K.current,list:[...V,e],at:e.id})},onRename:e=>{let t=e.trim(),n=K.current,r=me.current.id,i={...n,at:r,list:n.list.filter(e=>e.work?.version===14).map(e=>e.id===r?{...e,name:t,work:{...e.work,name:t}}:e)};K.current=i,o(i),_e()},onDelete:()=>{let e=K.current,t=V.filter(e=>e.id!==W),r=t.length?t:[Wl(s.list,pe(),fe)],i=t.length?t[0].id:r[0].id;o({...K.current,list:r,at:i}),n.offer({label:`Deleted ${H.name||`the pipeline`}`,restore:()=>{K.current=e,o(e)}})},onRestore:()=>P(!0),onExport:Ie,onExportCi:()=>ee(!0),onImport:()=>te.current?.click()}),(0,$.jsx)(il,{problems:De,error:T,run:F,submitting:A,onRun:()=>void ke(),onStop:()=>void je(),onOpen:()=>{F&&r(F.id,{at:F.submittedAt,from:`runs`})},...D?.length?{onShare:()=>void Ae()}:{}}),(0,$.jsxs)(`div`,{className:`runs`,children:[(0,$.jsx)(`input`,{ref:te,type:`file`,accept:`.yml,.yaml`,hidden:!0,"aria-hidden":`true`,onChange:e=>{let t=e.target.files?.[0];e.target.value=``,t&&t.text().then(Le).catch(()=>z({error:`the file could not be read`}))}}),(0,$.jsx)(Bl,{pipeline:U,edit:ve,comment:c?.text??``,setComment:e=>l({text:e}),onClear:Fe,onCopyFailures:()=>void Me()}),(0,$.jsx)(kl,{pipeline:U,profiles:s.list,sources:d,targetCap:x,prompt:se,..._?{defaultPrompt:_}:{},protectedTerms:oe,edit:ve,offer:n.offer}),(0,$.jsx)(Pl,{profiles:s.list,labGrader:Ee,pipeline:U,edit:ve,offer:n.offer,children:(0,$.jsx)(zl,{pipeline:U,sourceFiles:C,graded:ie,openCaseEditor:Pe})}),(0,$.jsx)(ut,{undo:n.undo,onUndo:n.undoNow,onDismiss:n.dismiss}),N&&(0,$.jsx)(Tt,{title:`Restore pipeline`,onClose:()=>P(!1),children:(0,$.jsxs)(`div`,{className:`version-list`,children:[Oe.length===0&&(0,$.jsx)(`p`,{className:`note`,children:`No versions of this pipeline yet.`}),Oe.map((e,t)=>(0,$.jsxs)(`div`,{className:`version-row`,children:[(0,$.jsxs)(`span`,{className:`version-when`,children:[ql(e.at),`, `,Kl(e.reason)]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>Re(e),children:`Restore`})]},`${e.at}-${t}`))]})}),B&&(0,$.jsx)(rl,{hasItems:we!==null,onExport:Y,onClose:()=>ee(!1)}),R&&(`doc`in R?(0,$.jsx)(Tt,{title:`Imported ${R.doc.name||`Untitled`}`,onClose:()=>z(null),children:R.missing.length>0?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`p`,{className:`note`,children:`A run is blocked until each of these exists here — create it, or pick a replacement in the Content and Target profile rows:`}),(0,$.jsx)(`ul`,{className:`import-missing`,children:R.missing.map(e=>(0,$.jsx)(`li`,{children:e},e))})]}):(0,$.jsx)(`p`,{className:`note`,children:`Ready to run.`})}):(0,$.jsx)(Tt,{title:`Nothing imported`,onClose:()=>z(null),children:(0,$.jsx)(`p`,{className:`note`,children:R.error})}))]})]})}var Ul=e=>{let t=U(e),n=J().blankPipeline({name:t});return{id:Gl(),name:t,work:{...n,jobs:n.jobs.map(e=>({...e,steps:[]}))}}},Wl=(e,t,n)=>({id:Gl(),name:U([]),work:Ie(Et(e),t,n)}),Gl=()=>`w${Date.now().toString(36)}${Math.random().toString(36).slice(2,8)}`,Kl=e=>e===`run`?`before a run`:e===`restore`?`before a restore`:e===`pack`?`before a pack update`:`after editing`,ql=e=>{let t=new Date(e);return Number.isNaN(t.getTime())?e:t.toLocaleString()},Jl=e=>!!e.id&&li(e.status),Yl=e=>`Queued ${Ur(e.submittedAt)}`;async function Xl(e,t){try{(await e.run(t)).status===`queued`&&await e.cancel(t)}catch{}}var Zl=[``,`queued`,`running`,`done`,`cancelled`,`failed`,`incomplete`,`interrupted`],Ql=e=>e.numeric?`hnum num`:void 0,$l=e=>[...new Map(e.map(e=>[e.id,e])).values()].sort((e,t)=>String(t.submittedAt).localeCompare(String(e.submittedAt))||(e.id<t.id?1:e.id>t.id?-1:0)),eu=e=>e&&{before:e.submittedAt,beforeId:e.id};function tu(){let e=Q(),t=ye(),{openRun:n}=Dr(),[r,i]=On(`promptlab.history.columns`),[a,o]=(0,Z.useState)([]),[s,c]=(0,Z.useState)(!1),[l,u]=(0,Z.useState)(``),[d,f]=(0,Z.useState)(new Set),[p,m]=(0,Z.useState)(!1),[h,g]=(0,Z.useState)(null),[_,v]=(0,Z.useState)(null),[y,b]=(0,Z.useState)(null),[x,S]=(0,Z.useState)(null),[C,w]=(0,Z.useState)(!1),[E,D]=(0,Z.useState)(!1),[O,k]=(0,Z.useState)(!1),[A,j]=(0,Z.useState)(!1),[N,P]=(0,Z.useState)(null),[I,L]=(0,Z.useState)(0),R=(0,Z.useRef)(A);R.current=A;let[z,B]=(0,Z.useState)(null),[ee,te]=(0,Z.useState)(0),V=(0,Z.useRef)(a);V.current=a;let H=(0,Z.useMemo)(()=>a.map(e=>Kr(ji(e),e)),[a]),U=(0,Z.useMemo)(()=>iu(r),[r]),W=(0,Z.useCallback)(async(t,n=!1)=>{try{return await e.queue({limit:25,...t,...n?{full:n}:{}})}catch{return null}},[e]),G=(0,Z.useCallback)(e=>{let t=new Set(e.runs.map(e=>e.id));o(n=>$l([...n.filter(e=>!t.has(e.id)),...e.runs])),c(e.more)},[]);(0,Z.useEffect)(()=>{let e=!0,t,n=async()=>{let r=await W();e&&(r?(G(r),j(!0),P(null)):R.current||P(`Runs not loaded: server unreachable.`),V.current.some(e=>[`queued`,`running`].includes(e.status))&&(t=setTimeout(()=>void n(),3e3)))};return n(),()=>{e=!1,t&&clearTimeout(t)}},[W,G,I,ee]);let ne=(0,Z.useMemo)(()=>H.filter(e=>!l||(e.status||`done`)===l),[H,l]),re=(e=>({Runs:ii.filter(t=>e.Runs.has(t.key)),"Run Scenarios":ai.filter(t=>e[`Run Scenarios`].has(t.key))}))(U),ie=(0,Z.useCallback)(e=>{f(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[]),ae=(0,Z.useCallback)((e,t)=>{n(e,{at:t,from:`history`})},[n]),oe=(0,Z.useCallback)(async t=>{try{let n=await e.resume(t),r={runs:[n.run],more:V.current.length>25};G(r),ae(t,n.run.submittedAt)}catch{}},[e,G,ae]),se=(0,Z.useCallback)(async n=>{S(null);try{let{run:r}=await e.rerunRun(n);G({runs:[r],more:V.current.length>25}),B(r.id),te(e=>e+1),t.offer({label:Yl(r),restore:()=>void Xl(e,r.id).then(()=>te(e=>e+1))})}catch(e){S(`Not re-run: ${e instanceof Error?e.message:`server unreachable`}.`)}},[e,G,t]),ce=e=>{let t=e.run?V.current.find(t=>t.id===e.run):void 0,n=[];return ni(e)&&n.push({label:`Resume`,icon:T,onSelect:()=>void oe(e.run)}),t&&Jl(t)&&n.push({label:`Re-run`,icon:Sn,onSelect:()=>void se(t.id)}),n},le=(0,Z.useCallback)(async()=>{let n=V.current;k(!0);try{await e.clear()}catch{S(`Not cleared: server unreachable.`);return}finally{k(!1)}o([]),c(!1),t.offer({label:`Cleared ${Wr(n.length,`run`)}`,restore:()=>{for(let t of n)e.submit(J().upgradePipeline(t.snapshot)).catch(()=>{})}})},[e,t]),ue=(0,Z.useCallback)(async()=>{D(!0);let e=[],t=!1,n;for(let r=0;;r++){let i=await W(n,!0);if(!i){t=!0;break}if(e=$l([...e,...i.runs]),n=eu(i.runs.at(-1)),!i.more||!n||r>100)break}if(t){let t=new Set(e.map(e=>e.id));e=$l([...e,...V.current.filter(e=>!t.has(e.id))])}if(D(!1),!e.length){S(`Nothing to export yet.`);return}aa(new Blob([JSON.stringify({exported:new Date().toISOString(),runs:e},null,2)],{type:`application/json`}),ia(`runs`,`all`,14,`json`)),S(t?`Exported ${Wr(e.length,`run`)}. Server unreachable, so only those this browser had.`:`Exported ${Wr(e.length,`run`)}.`)},[e,W]),de=[{label:`Select Columns`,icon:nn,onSelect:()=>m(!0)},{label:`Export JSON`,icon:ve,busy:E,onSelect:()=>void ue()},{label:`Clear`,icon:ze,destructive:!0,busy:O,disabled:!A,onSelect:()=>void le()}],fe=(0,Z.useCallback)(e=>{let t=e.target.closest(`[data-openrun]`);if(t){e.stopPropagation();let n=t.closest(`tr[data-run]`),r=n?ne[Number(n.getAttribute(`data-run`))]:null;r?.run&&ae(r.run,r.at);return}let n=e.target.closest(`[data-editcomment]`);if(n){e.stopPropagation();let t=n.closest(`tr[data-run]`),r=t?ne[Number(t.getAttribute(`data-run`))]:null;r?.run&&v({id:r.run,comment:r.comment});return}let r=e.target.closest(`[data-scenario]`);if(r){e.stopPropagation();let t=r.closest(`tr[data-scen]`)?.previousElementSibling,n=t?ne[Number(t.getAttribute(`data-run`))]:null,i=n?.run?V.current.find(e=>e.id===n.run):null;i?.snapshot&&b({run:i.snapshot,i:Number(r.getAttribute(`data-scenario`))});return}let i=e.target.closest(`[data-prof]`);if(i){e.stopPropagation();let t=i.closest(`tr[data-scen]`)?.previousElementSibling,n=t?ne[Number(t.getAttribute(`data-run`))]:null,r=i.getAttribute(`data-prof`);if(n&&r!=null){let e=n.scenarios[Number(r)];g({name:e?.setupProfile?.name??``,settings:Lr(n,e??{})})}}},[ne,ae]),pe=(0,Z.useCallback)((e,t)=>{o(n=>n.map(n=>n.id===e?{...n,snapshot:{...n.snapshot,comment:t}}:n)),v(null)},[]),K=(e,t)=>e.image&&Vr(t)!==!0?(0,$.jsx)(`span`,{className:`note`,children:`—`}):e.get(t),me=(e,t,n,r)=>n?e.image&&Vr(t)!==!0?(0,$.jsx)(`span`,{className:`note`,children:`—`}):e.get(t,n,r):`—`;return(0,$.jsxs)(`div`,{className:`history`,children:[(0,$.jsxs)(X,{id:`history`,title:`History`,actions:de,children:[!A&&(0,$.jsx)(M,{label:`Runs`,error:N,onRetry:()=>{P(null),L(e=>e+1)}}),A&&(H.length>0||l)&&(0,$.jsx)(Le,{noun:`runs`,lead:ne.length>0&&(0,$.jsx)(`p`,{className:`note hstatus`,role:`status`,children:Wr(ne.length,`run`)}),filters:[{key:`show`,label:`Show`,value:l,onChange:u,options:Zl.map(e=>({value:e,label:e?Yr(e):`All`}))}]}),x&&(0,$.jsx)(`p`,{className:`note hstatus`,role:`status`,children:x}),A?ne.length?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`div`,{className:`scrollx`,onClick:fe,children:(0,$.jsx)(q,{className:`htable`,label:`Runs`,defaultSort:null,rows:ne,rowKey:e=>e.at,rowProps:(e,t)=>({className:`runrow${d.has(e.at)?` open`:``}${e.run&&e.run===z?` fresh`:``}`,"data-run":t}),columns:[{key:`expand`,header:(0,$.jsx)(`span`,{className:`hvh`,children:`Targets`}),label:``,className:`hexpand`,sort:null,cell:(e,t)=>{let n=d.has(e.at);return(0,$.jsx)(`button`,{type:`button`,className:`icon-btn`,"aria-label":n?`Collapse`:`Expand`,"aria-expanded":n,"aria-controls":n?`hscen-${t}`:void 0,onClick:()=>ie(e.at),children:(0,$.jsx)(Ke,{size:16,"aria-hidden":`true`})})}},...re.Runs.map(e=>({key:e.key,header:e.label,className:Ql(e),sort:e.sort??(t=>jt(e.get(t))),cell:t=>(0,$.jsx)(`span`,{className:`hval`,children:K(e,t)})})),{key:`actions`,header:(0,$.jsx)(`span`,{className:`hvh`,children:`Actions`}),label:``,className:`hactions`,sort:null,cell:e=>{let t=ce(e);return t.length?(0,$.jsx)(Ue,{actions:t,label:`${Ur(e.at)} actions`}):null}}],after:(e,t)=>d.has(e.at)&&(0,$.jsx)(`tr`,{className:`scenrows`,id:`hscen-${t}`,"data-run":t,"data-scen":e.at,children:(0,$.jsx)(`td`,{colSpan:re.Runs.length+2,children:(0,$.jsx)(q,{className:`scen-table`,label:`Targets`,defaultSort:null,rows:((e.scenarios||[]).length?e.scenarios:[null]).map((e,t)=>({sc:e,n:t})),rowKey:({sc:e,n:t})=>e?.id??String(t),columns:re[`Run Scenarios`].map(t=>({key:t.key,header:t.label,className:Ql(t),sort:({sc:n,n:r})=>{if(!n)return null;if(t.sort)return t.sort(n,r);let i=t.get(e,n,r);return jt(i)},cell:({sc:n,n:r})=>(0,$.jsx)(`span`,{className:`hval`,children:me(t,e,n,r)})}))})})})})}),s&&(0,$.jsxs)(`button`,{type:`button`,className:`ghost older-btn`,disabled:C,"aria-busy":C||void 0,onClick:async()=>{w(!0);let e=await W(eu(V.current.at(-1)));if(w(!1),!e){S(`Server unreachable.`);return}G(e)},children:[C&&(0,$.jsx)(F,{}),`Older`]})]}):(0,$.jsx)(`p`,{className:`note`,children:x??(H.length?`No runs match this filter.`:(0,$.jsxs)($.Fragment,{children:[`No runs yet. `,(0,$.jsx)(`a`,{href:`#/runs`,children:`Start one in Runs`})]}))}):null]}),(0,$.jsx)(ut,{undo:t.undo,onUndo:t.undoNow,onDismiss:t.dismiss}),p&&(0,$.jsx)(ou,{cols:U,onSave:e=>{i(au(e)),m(!1)},onClose:()=>m(!1)}),y&&(0,$.jsx)(mt,{run:y.run,i:y.i,onClose:()=>b(null)}),h&&(0,$.jsx)(Tt,{title:h.name||`Settings`,onClose:()=>g(null),children:(0,$.jsx)(nu,{settings:h.settings})}),_&&(0,$.jsx)(ru,{runId:_.id,comment:_.comment,onSaved:e=>pe(_.id,e),onClose:()=>v(null)})]})}function nu({settings:e}){let t=Fr.filter(t=>String(e[t]??``)!==``).map(t=>({key:t,label:Rr(t),control:{type:`static`,text:zr(t,e[t])}}));return t.length?(0,$.jsx)(W,{label:`Target profile`,rows:t}):(0,$.jsx)(`p`,{className:`note`,children:`No settings recorded.`})}function ru({runId:e,comment:t,onSaved:n,onClose:r}){let i=Q(),[a,o]=(0,Z.useState)(t),[s,c]=(0,Z.useState)(null),[l,u]=(0,Z.useState)(!1);return(0,$.jsx)(Ot,{title:`Run comment`,...s?{status:s,statusBad:!0}:{},sections:[{key:`comment`,rows:[{key:`comment`,label:`Comment`,control:{type:`text`,value:a,onChange:o,placeholder:`optional`}}]}],actions:[{label:`Save & Close`,icon:De,busy:l,primary:!0,onSelect:async()=>{u(!0),c(null);try{await i.setComment(e,a),n(a)}catch{c(`Not saved: the server is unreachable.`),u(!1)}}}],focusRow:`comment`,dirty:a!==t,onClose:r})}function iu(e){let t=()=>({Runs:new Set(ii.filter(e=>e.on).map(e=>e.key)),"Run Scenarios":new Set(ai.filter(e=>e.on).map(e=>e.key))});if(!e)return t();let n=t();for(let t of ci){let r=t[0],i=e[r],a=new Set(t[1].map(e=>e.key)),o=e=>si[e]??e,s=new Set(i?.seen?.map(o)??[...a]),c=[...(i?.on??[]).map(o).filter(e=>a.has(e)),...t[1].filter(e=>e.on&&!s.has(e.key)).map(e=>e.key)];c.length&&(n[r]=new Set(c))}return n}function au(e){return{Runs:{on:[...e.Runs],seen:ii.map(e=>e.key)},"Run Scenarios":{on:[...e[`Run Scenarios`]],seen:ai.map(e=>e.key)}}}function ou({cols:e,onSave:t,onClose:n}){let[r,i]=(0,Z.useState)(()=>({Runs:new Set(e.Runs),"Run Scenarios":new Set(e[`Run Scenarios`])})),[a,o]=(0,Z.useState)(null),s=(e,t)=>e.size===t.size&&[...e].every(e=>t.has(e)),c=!s(r.Runs,e.Runs)||!s(r[`Run Scenarios`],e[`Run Scenarios`]);return(0,$.jsx)(Tt,{title:`Select Columns`,onClose:n,dirty:c,children:(0,$.jsxs)(`div`,{className:`hcols`,children:[ci.map(([e,t])=>(0,$.jsxs)(`div`,{className:`groups`,children:[(0,$.jsx)(`h3`,{children:oi[e]??e}),t.map(t=>(0,$.jsxs)(`label`,{children:[(0,$.jsx)(`input`,{type:`checkbox`,checked:r[e].has(t.key),onChange:n=>{let a=new Set(r[e]);n.target.checked?a.add(t.key):a.delete(t.key),i({...r,[e]:a}),o(null)}}),` `,t.label]},t.key))]},e)),a&&(0,$.jsx)(`p`,{className:`note bad`,children:a}),(0,$.jsxs)(`div`,{className:`hcols-actions`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:n,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,className:`run`,onClick:()=>{if(!r.Runs.size){o(`the Runs group needs at least one column`);return}t(r)},children:`Save & Close`})]})]})})}var su=e=>!!e&&e.startsWith(`•held:`),cu={"promptlab.workflows":{list:[],at:``},"promptlab.profiles":{list:[],at:``},"promptlab.comment":null,"promptlab.versions":lu(),"promptlab.history.columns":null,"promptlab.wizard":null,"promptlab.tokens":null};function lu(){return{pipeline:{},profile:{},dataset:{}}}var uu=[`promptlab.workflows`,`promptlab.profiles`,`promptlab.versions`,`promptlab.tokens`],du=e=>`/w/${encodeURIComponent(e)}${location.hash}`,fu=`promptlab.ws.undo`;function pu(e){try{localStorage.setItem(fu,JSON.stringify(e))}catch{}}function mu(){let e=null;try{e=localStorage.getItem(fu),localStorage.removeItem(fu)}catch{return null}if(!e)return null;try{let t=JSON.parse(e);return t&&t.id&&t.slug&&t.name?{id:t.id,slug:t.slug,name:t.name}:null}catch{return null}}function hu(e){let t=e.filter(e=>!e.archived);return t.length?[...t].sort((e,t)=>(t.lastUsed??``).localeCompare(e.lastUsed??``)||Number(t.isDefault)-Number(e.isDefault))[0]??null:null}var gu=(0,Z.createContext)({current:{id:`default`,slug:`default`,name:`Default`,archived:!1,isDefault:!0},requestedSlug:null,list:[],switchTo:()=>{}}),_u=()=>(0,Z.useContext)(gu),vu=[`px`,`format`,`quality`],yu=[{value:`200704`,label:`448 square`},{value:`edge448`,label:`448 edge`},{value:`409920`,label:`480p`},{value:`518400`,label:`540p`},{value:`921600`,label:`720p`},{value:`1440000`,label:`900p`},{value:`2073600`,label:`1080p`},{value:`3686400`,label:`1440p`}],bu={"image/jpeg":`JPEG`,"image/webp":`WebP`,"image/png":`PNG`},xu=e=>bu[e]??e.replace(`image/`,``).toUpperCase(),Su={px:`921600`,quality:`0.92`};function Cu(e,t){let n=J().connectionImage({}).format,r=String(e.px??``),i=String(e.format??``),a=String(e.quality??``),o=(yu.some(e=>e.value===r)||!r?yu:[...yu,{value:r,label:J().budgetLabel(r)}]).map(e=>e.value===Su.px?{value:``,label:e.label}:e),s=J().IMAGE_FORMATS.map(e=>e===n?{value:``,label:xu(e)}:{value:e,label:xu(e)}),c=(i||n)===`image/png`,l={type:`text`,value:a,onChange:t(`quality`),size:`num`,...c||a?{}:{placeholder:Su.quality}};return[{key:`px`,label:`Resolution`,control:{type:`select`,value:r===Su.px?``:r,options:o,onChange:t(`px`)}},{key:`format`,label:`Format`,control:{type:`select`,value:i===n?``:i,options:s,onChange:t(`format`)}},{key:`quality`,label:`Quality`,...c?{hint:`Unused for PNG`}:{},control:l}]}function wu(e){let{px:t,format:n,quality:r}=J().connectionImage(e),i=n===`image/png`?``:` q${r??Su.quality}`;return`${J().budgetLabel(t??Number(Su.px))}, ${xu(n)}${i}`}function Tu(e){return!e||e.length===0||e.includes(`*all`)?``:[...e].sort().join(`,`)}function Eu(e){let[t,n]=(0,Z.useState)({});return(0,Z.useEffect)(()=>{let t=!0;return e.connectionShares().then(e=>{t&&n(e.shares)}).catch(()=>{}),()=>{t=!1}},[e]),{sharesFor:(0,Z.useCallback)((e,n)=>t[Kt(e,n)],[t]),setFor:(0,Z.useCallback)((t,r,i)=>{e.setConnectionShares(t,r,i).then(e=>n(e.shares)).catch(()=>{})},[e]),setForEach:(0,Z.useCallback)(async(t,r)=>{let i=null;for(let{id:n,workspaces:a}of r)try{i=(await e.setConnectionShares(t,n,a)).shares}catch{}i&&n(i)},[e])}}function Du({offer:e}){let t=Q(),n=Dn(),r=Kn(),i=nr(),[a,o]=(0,Z.useState)(null),[s,c]=(0,Z.useState)(null),[l,u]=(0,Z.useState)(null),[d,f]=(0,Z.useState)(!1),p=(0,Z.useRef)(null),m=e=>c({text:e instanceof Error?e.message:String(e),bad:!0}),h=(0,Z.useCallback)(()=>{u(null),t.packs().then(e=>o(e.packs),e=>u(e instanceof Error?e.message:String(e)))},[t]);(0,Z.useEffect)(h,[h]);let g=async()=>{await Promise.all([n.refresh(),r.refresh(),i.refresh()]),h()},v=async e=>{c({text:`Installing ${e.name}…`,bad:!1}),f(!0);try{let n=await t.installPack(e);await g();let r=(await t.packs()).packs.find(e=>e.id===n.pack),i=r?`${r.name} ${r.version}`:n.pack;c({text:n.installed?`Installed ${i}.`:`${i} is up to date.`,bad:!1})}catch(e){m(e)}finally{f(!1)}},y=async n=>{let r=[];try{for(let e of n)r.push((await t.removePack(e.id)).trash)}catch(e){m(e)}r.length&&(await g(),e({label:n.length===1?`Removed ${n[0].name}`:`Removed ${r.length} packs`,restore:()=>{(async()=>{for(let e of r)await t.restorePack(e);await g()})()}}))};return(0,$.jsxs)(X,{id:`setup-packs`,title:`Packs`,note:a?Ou(a.length,`pack`):``,actions:[{label:`Install pack…`,icon:H,busy:d,onSelect:()=>p.current?.click()}],children:[a===null&&(0,$.jsx)(M,{label:`Packs`,error:l,onRetry:h}),a&&!a.length&&(0,$.jsx)(`p`,{className:`note`,children:`No packs installed.`}),a&&a.length>0&&(0,$.jsx)(q,{className:`setup-table`,cards:!0,label:`Packs`,rows:a,rowKey:e=>e.id,select:{noun:`pack`,actions:e=>[{label:`Remove`,icon:_.remove,destructive:!0,onSelect:()=>void y(e)}]},columns:[{key:`name`,header:`Pack`,role:`title`,cell:e=>e.name},{key:`version`,header:`Version`,cell:e=>e.version},{key:`made`,header:`Made`,sort:e=>e.datasets.length+e.pipelines.length+e.sources.length+e.presets,cell:e=>ku(e)},{key:`menu`,header:``,className:`ds-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${e.name} actions`,actions:[{label:`Remove`,icon:_.remove,destructive:!0,onSelect:()=>void y([e])}]})}]}),s&&(0,$.jsx)(`p`,{className:`note${s.bad?` bad`:``}`,role:s.bad?`alert`:`status`,children:s.text}),(0,$.jsx)(`input`,{ref:p,type:`file`,accept:`.labpack,.zip,application/zip`,hidden:!0,"aria-label":`Install pack file`,onChange:e=>{let t=e.target.files?.[0];e.target.value=``,t&&v(t)}})]})}var Ou=(e,t)=>`${e} ${t}${e===1?``:`s`}`,ku=e=>[Ou(e.datasets.length,`dataset`),Ou(e.pipelines.length,`pipeline`),Ou(e.sources.length,`source`),Ou(e.presets,`preset`)].join(`, `);function Au({offer:e}){let t=Q(),[n,r]=(0,Z.useState)(null),[i,a]=(0,Z.useState)(null),[o,s]=(0,Z.useState)(null),[c,l]=(0,Z.useState)(!1),u=(0,Z.useRef)(null),d=(0,Z.useCallback)(()=>{s(null),t.plugins().then(e=>r(e.plugins),e=>s(e instanceof Error?e.message:String(e)))},[t]);(0,Z.useEffect)(d,[d]);let f=async e=>{a(null),l(!0);try{let n=await t.installPlugin(e),i=(await t.plugins()).plugins;r(i);let o=i.find(e=>e.id===n.plugin);o&&n.installed&&await Ut(o),a({text:n.installed?Bt[n.plugin]?`Installed ${n.plugin}, and it did not load: ${Bt[n.plugin]}`:`Installed ${n.plugin}.`:`${n.plugin} is up to date.`,bad:!!Bt[n.plugin]})}catch(e){a({text:e instanceof Error?e.message:String(e),bad:!0})}finally{l(!1)}},p=async n=>{let r=[];try{for(let e of n)r.push((await t.removePlugin(e.id)).trash)}catch(e){a({text:e instanceof Error?e.message:String(e),bad:!0})}d(),r.length&&e({label:n.length===1?`Removed ${n[0].id}`:`Removed ${r.length} plugins`,restore:()=>{Promise.all(r.map(e=>t.restorePlugin(e))).then(d)}})};return(0,$.jsxs)(X,{id:`setup-plugins`,title:`Plugins`,note:n?`${n.length} ${n.length===1?`plugin`:`plugins`}`:``,actions:[{label:`Install plugin…`,icon:H,busy:c,onSelect:()=>u.current?.click()}],children:[n===null&&(0,$.jsx)(M,{label:`Plugins`,error:o,onRetry:d}),n&&!n.length&&(0,$.jsx)(`p`,{className:`note`,children:`No plugins installed.`}),n&&n.length>0&&(0,$.jsx)(q,{className:`setup-table`,cards:!0,label:`Plugins`,rows:n,rowKey:e=>e.id,select:{noun:`plugin`,actions:e=>[{label:`Remove`,icon:_.remove,destructive:!0,onSelect:()=>void p(e)}]},columns:[{key:`id`,header:`Plugin`,role:`title`,cell:e=>e.id},{key:`version`,header:`Version`,cell:e=>e.version},{key:`sha`,header:`SHA-256`,sort:e=>e.sha256,cell:e=>(0,$.jsx)(`code`,{title:e.sha256,children:e.sha256.slice(0,12)})},{key:`registers`,header:`Registers`,sort:e=>ju(e),cell:e=>ju(e)||`—`},{key:`state`,header:`State`,cell:e=>Mu(e)},{key:`menu`,header:``,className:`ds-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${e.id} actions`,actions:[{label:`Remove`,icon:_.remove,destructive:!0,onSelect:()=>void p([e])}]})}]}),i&&(0,$.jsx)(`p`,{className:`note${i.bad?` bad`:``}`,role:i.bad?`alert`:`status`,children:i.text}),(0,$.jsx)(`input`,{ref:u,type:`file`,accept:`.labplugin,.zip,application/zip`,hidden:!0,"aria-label":`Install plugin file`,onChange:e=>{let t=e.target.files?.[0];e.target.value=``,t&&f(t)}})]})}var ju=e=>[...e.registers.outputKinds??[],...e.registers.modifiers??[],...e.registers.evalTypes??[],...(e.registers.connectionTypes??[]).map(e=>e.id)].join(`, `),Mu=e=>Bt[e.id]?`Failed: ${Bt[e.id]}`:Vt.has(e.id)?`Loaded`:`Not Loaded`,Nu={env:`Deployment`,lab:`Own`,default:`Default`};function Pu({cfg:e,editable:t,onClose:n}){let r=Q(),i=e?.from===`lab`?e:null,[a,o]=(0,Z.useState)(i?.clientId??``),[s,c]=(0,Z.useState)(i?.tenant??``),[l,u]=(0,Z.useState)(null),[d,f]=(0,Z.useState)(!1),p=a!==(i?.clientId??``)||s!==(i?.tenant??``),m=async()=>{if(!p){n();return}f(!0);try{await r.setMicrosoft(a,s),Ls(),n()}catch(e){u(e instanceof Error?e.message:String(e)),f(!1)}},h=e=>t=>{u(null),e(t)},g=!t,_=new URL(`/`,js()??window.location.href).href,v=ws(g?e?.tenant??``:s||`organizations`),y=[g?{key:`client`,label:`Client ID`,control:{type:`code`,text:e?.clientId??`—`}}:{key:`client`,label:`Client ID`,control:{type:`text`,value:a,onChange:h(o),size:`wide`,placeholder:e?.from==="default"?e.clientId:`00000000-0000-0000-0000-000000000000`}},g?{key:`tenant`,label:`Tenant`,control:{type:`code`,text:e?.tenant??`—`}}:{key:`tenant`,label:`Tenant`,control:{type:`text`,value:s,onChange:h(c),size:`wide`,placeholder:`00000000-0000-0000-0000-000000000000`}},{key:`signin`,label:`Sign-in`,control:{type:`static`,text:v}},{key:`redirect`,label:`Redirect URI`,control:{type:`code`,text:_}},{key:`app`,label:`App`,control:{type:`static`,text:e?.from?Nu[e.from]:`None`}}];return(0,$.jsx)(Ot,{title:`Microsoft 365`,status:l??void 0,statusBad:!!l,focusRow:g?void 0:`client`,sections:[{key:`app`,rows:y}],...g?{}:{onSave:()=>void m(),saving:d,dirty:p},onClose:n})}var Fu={env:`Deployment`,lab:`Own`,default:`Default`},Iu=`https://console.cloud.google.com/apis/credentials`,Lu=[{value:`installed`,label:`Desktop app`},{value:`web`,label:`Web application`}],Ru={installed:`For a lab on this machine; Google needs no redirect registered.`,web:`For a lab at its own address; register the redirect below with the client.`},zu=`/api/connections/google/callback`,Bu=()=>/^(localhost|127\.|\[::1\])/.test(window.location.hostname);function Vu(e){let t;try{t=JSON.parse(e)}catch{return`that file is not JSON`}let n=t??{},r=n.installed?`installed`:n.web?`web`:null,i=r?n[r]:void 0;return!r||typeof i?.client_id!=`string`||typeof i.client_secret!=`string`?`that is not a client file from Google Cloud`:{clientId:i.client_id,clientSecret:i.client_secret,apiKey:``,clientType:r}}function Hu({onClose:e,onSaved:t}){let n=Q(),[r,i]=(0,Z.useState)(void 0),[a,o]=(0,Z.useState)(!0),[s,c]=(0,Z.useState)(``),[l,u]=(0,Z.useState)(``),[d,f]=(0,Z.useState)(``),[p,m]=(0,Z.useState)(Bu()?`installed`:`web`),[h,g]=(0,Z.useState)({clientId:``,apiKey:``,kind:``}),[_,v]=(0,Z.useState)(null),[y,b]=(0,Z.useState)(null),[x,S]=(0,Z.useState)(!1);(0,Z.useEffect)(()=>{n.google().then(e=>{i(e.google),o(e.editable);let t=e.google?.from===`lab`?e.google:null;t&&(c(t.clientId),f(t.apiKey??``),m(t.clientType)),g({clientId:t?.clientId??``,apiKey:t?.apiKey??``,kind:t?.clientType??(Bu()?`installed`:`web`)})},e=>{i(null),v(e instanceof Error?e.message:String(e))})},[n]);let C=s!==h.clientId||d!==h.apiKey||p!==h.kind||!!l,w=async()=>{if(!C){e();return}S(!0);try{await n.setGoogle({clientId:s,apiKey:d,clientType:p,...l?{clientSecret:l}:{}}),t(),e()}catch(e){v(e instanceof Error?e.message:String(e)),S(!1)}},T=e=>t=>{v(null),b(null),e(t)},E=()=>{let e=document.createElement(`input`);e.type=`file`,e.accept=`.json,application/json`,e.onchange=()=>{let t=e.files?.[0];t&&D(t)},e.click()},D=async e=>{let t=Vu(await e.text());if(typeof t==`string`){v(t);return}c(t.clientId),m(t.clientType),u(t.clientSecret??``),b(`Loaded the client file`)};if(r===void 0)return null;let O=!a,k=!!r?.hasSecret&&r.from!=="default",A=O?r?.clientType??`web`:p,j=O?[{key:`client`,label:`Client ID`,control:{type:`code`,text:r?.clientId??`—`}},{key:`secret`,label:`Client secret`,control:{type:`static`,text:r?.hasSecret?`Held`:`None`}},{key:`key`,label:`API key`,control:{type:`code`,text:r?.apiKey??`—`}},{key:`type`,label:`Type`,control:{type:`static`,text:Lu.find(e=>e.value===A).label}}]:[{key:`client`,label:`Client ID`,control:{type:`text`,value:s,onChange:T(c),size:`wide`,placeholder:r?.from==="default"?r.clientId:`000000000000-….apps.googleusercontent.com`}},{key:`secret`,label:`Client secret`,control:{type:`secret`,value:l,onChange:T(e=>u(e??``)),...k?{placeholder:`Saved`}:{}}},{key:`key`,label:`API key`,control:{type:`text`,value:d,onChange:T(f),size:`wide`,placeholder:`AIza…`}},{key:`type`,label:`Type`,hint:Ru[p],control:{type:`select`,value:p,options:Lu,onChange:e=>T(m)(e)}}];return j.push({key:`redirect`,label:`Redirect URI`,control:A===`web`?{type:`code`,text:new URL(zu,window.location.href).href}:{type:`static`,text:`Not Needed`}},{key:`app`,label:`App`,control:{type:`static`,text:r?.from?Fu[r.from]:`None`}}),(0,$.jsx)(Ot,{title:`Google Drive`,status:_??y??void 0,statusBad:!!_,focusRow:O?void 0:`client`,sections:[{key:`app`,rows:j}],actions:[{label:`Google Cloud`,icon:rn,onSelect:()=>{window.open(Iu,`_blank`,`noopener`)}},...O?[]:[{label:`Load client file…`,icon:on,onSelect:E}]],...O?{}:{onSave:()=>void w(),saving:x,dirty:C},onClose:e})}var Uu=`microsoft`,Wu=`google`;function Gu(){let e=Q(),{list:t}=_u(),n=t.filter(e=>!e.archived).map(e=>({id:e.id,name:e.name})),{sharesFor:r,setFor:i}=Eu(e),{cfg:a,editable:o}=Rs(),[s,c]=(0,Z.useState)(null),[l,u]=(0,Z.useState)(null),[d,f]=(0,Z.useState)(void 0),[p,m]=(0,Z.useState)(null),h=(0,Z.useCallback)(()=>{e.connections().then(e=>u(e.connections),()=>u([]))},[e]);(0,Z.useEffect)(h,[h]),(0,Z.useEffect)(()=>{if(!a){f(null);return}ks(a).then(f,()=>f(null))},[a]);let g=[...l??[],...a||o?[{id:Uu,label:`Microsoft 365`,configured:!!a,signedIn:!!d,account:d??null,problem:null}]:[]],_=async t=>{m(null);try{t.id===Uu&&a?await Ns(a):window.location.assign((await e.signIn(t.id)).url)}catch(e){m(e instanceof Error?e.message:String(e))}},v=async t=>{m(null);try{t.id===Uu&&a?(await Ps(a),f(null)):u((await e.signOut(t.id)).connections)}catch(e){m(e instanceof Error?e.message:String(e))}};if(l===null)return(0,$.jsx)(X,{id:`setup-connections`,title:`Accounts`,children:(0,$.jsx)(M,{label:`Accounts`})});if(!g.length)return null;let y=g.filter(e=>e.problem);return(0,$.jsxs)(X,{id:`setup-connections`,title:`Accounts`,note:`${g.filter(e=>e.signedIn).length} of ${g.length}`,children:[(0,$.jsx)(q,{className:`setup-table`,cards:!0,label:`Accounts`,rows:g,rowKey:e=>e.id,columns:[{key:`label`,header:`Account`,role:`title`,cell:e=>e.label},{key:`status`,header:`Status`,cell:e=>e.id===Uu&&a&&d===void 0?(0,$.jsxs)(`span`,{className:`loading-status`,children:[(0,$.jsx)(F,{}),`Checking`]}):Ku(e)},{key:`account`,header:`Account`,cell:e=>e.account||`—`},{key:`workspaces`,header:`Workspaces`,sort:e=>Tu(r(`account`,e.id)),cell:e=>(0,$.jsx)(Oe,{label:e.label,workspaces:n,shares:r(`account`,e.id),onChange:t=>i(`account`,e.id,t)})},{key:`menu`,header:``,className:`ds-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{label:`${e.label} actions`,actions:[e.signedIn?{label:`Sign out`,icon:gn,onSelect:()=>void v(e)}:{label:`Sign in`,icon:hn,disabled:!e.configured,onSelect:()=>void _(e)},...e.id===Uu||e.id===Wu?[{label:`Configure…`,icon:le,onSelect:()=>c(e.id)}]:[]]})}]}),y.map(e=>(0,$.jsxs)(`p`,{className:`note bad`,role:`alert`,children:[e.label,`: `,e.problem]},e.id)),p&&(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:p}),s===Uu&&a!==void 0&&(0,$.jsx)(Pu,{cfg:a,editable:o,onClose:()=>c(null)}),s===Wu&&(0,$.jsx)(Hu,{onClose:()=>c(null),onSaved:h})]})}var Ku=e=>e.configured?e.signedIn?`Signed In`:`Signed Out`:`Not Configured`,qu=J().WIZARDS,Ju=J().wizardProgress;function Yu(e){return!!e?.targets?.some(e=>e.steps?.some(e=>e.type===`recorded`))}J().WIZARDS[`test-workflow`]={id:`test-workflow`,title:`Test a Power Automate workflow`,blurb:`Vary a workflow action's request and grade the replies against what it recorded.`,steps:[{id:`source`,title:`Add a workflow Source`,detail:`In the Library, add the workflow and let it import its recent calls.`,where:`#/library/sources`,focus:`.add-btn`,done:e=>e.sources.some(e=>!!e.platform)},{id:`test`,title:`Test an action`,detail:`Open the Source and Test… the action that calls the model.`,where:`#/library/sources`,done:e=>e.pipelines.some(e=>Yu(e.work))},{id:`run`,title:`Run the test`,detail:`On Runs, change the editable target's request and run it.`,where:`#/runs`,focus:`.action-bar-primary`,done:e=>e.runs.some(e=>Yu(e.snapshot))},{id:`compare`,title:`Compare the replies`,detail:`Open the finished run and read the two replies side by side.`,where:`#/history`,done:e=>e.runs.some(e=>e.status===`done`&&Yu(e.snapshot))}]};var Xu=(0,Z.createContext)(null);function Zu({children:e}){let t=Q(),[n,r]=On(`promptlab.wizard`),[i]=On(`promptlab.profiles`),[a]=On(`promptlab.workflows`),[o,s]=(0,Z.useState)([]),[c,l]=(0,Z.useState)([]),u=(0,Z.useRef)(!!n);u.current=!!n,(0,Z.useEffect)(()=>{let e=!0,n=()=>{t.sources().then(t=>{e&&s(t.sources)},()=>{}),t.queue({limit:20}).then(t=>{e&&l(t.runs)},()=>{})};n();let r=()=>{u.current&&n()};return window.addEventListener(`hashchange`,r),qt.addEventListener(`write`,r),()=>{e=!1,window.removeEventListener(`hashchange`,r),qt.removeEventListener(`write`,r)}},[t,n?.id]);let d=(0,Z.useMemo)(()=>({profiles:i.list,pipelines:a.list,sources:o,runs:c}),[i.list,a.list,o,c]),f=(0,Z.useMemo)(()=>Object.values(qu),[]),p=n?qu[n.id]??null:null,m=(0,Z.useCallback)(e=>{qu[e]&&r({id:e})},[r]),h=(0,Z.useCallback)(()=>r(null),[r]),g=(0,Z.useCallback)(e=>{location.hash!==e.where&&(location.hash=e.where),e.focus&&ed(e.focus)},[]),_=(0,Z.useMemo)(()=>({entries:f,active:p,lab:d,start:m,cancel:h,goToStep:g}),[f,p,d,m,h,g]);return(0,$.jsx)(Xu.Provider,{value:_,children:e})}function Qu(){let e=(0,Z.useContext)(Xu);if(!e)throw Error(`useWizard outside a WizardProvider: the app mounts one at its root`);return e}function $u(){let{active:e,lab:t}=Qu();return(0,Z.useMemo)(()=>e?{entry:e,...Ju(e,t)}:null,[e,t])}function ed(e,t=20){let n=document.querySelector(e);if(n){n.focus({preventScroll:!1});try{n.focus({focusVisible:!0})}catch{}n.scrollIntoView?.({block:`center`,behavior:`smooth`});return}t>0&&requestAnimationFrame(()=>ed(e,t-1))}function td(){let{entries:e,active:t,lab:n,start:r,cancel:a,goToStep:o}=Qu();if(!t)return(0,$.jsxs)(X,{id:`setup-wizards`,title:`Wizards`,note:`${e.length} available`,children:[(0,$.jsx)(`p`,{className:`note`,children:`Get started quickly with a wizard to guide you through the steps.`}),(0,$.jsx)(q,{className:`setup-table wiz-table`,cards:!0,label:`Wizards`,rows:e,rowKey:e=>e.id,columns:[{key:`name`,header:`Wizard`,role:`title`,cell:e=>(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`span`,{className:`wiz-list-name`,children:e.title}),(0,$.jsx)(`span`,{className:`note`,children:e.blurb})]})},{key:`progress`,header:`Steps`,sort:e=>Ju(e,n).done,cell:e=>{let t=Ju(e,n);return`${t.done}/${t.total}`}},{key:`start`,header:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(i,{label:`${e.title} actions`,actions:[{label:`Start`,icon:T,onSelect:()=>r(e.id)}]})}]})]});let{ticks:s,done:c,total:l,current:u}=Ju(t,n),d=u>=0?t.steps[u]:null,f=u<0;return(0,$.jsxs)(`div`,{className:`setup-wizard`,children:[(0,$.jsx)(re,{kind:`Wizard`,name:t.title,meta:`${c} of ${l} complete`,docked:!0}),(0,$.jsx)(Te,{primary:{label:`Next step`,icon:it,onSelect:()=>d&&o(d),disabled:f},status:f?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`b`,{children:`All steps done.`}),` Cancel to close it.`]}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(`b`,{children:[`Step `,u+1,` of `,l]}),` · `,d.title]}),secondary:{label:`Cancel wizard`,icon:f?k:se,onSelect:a}}),(0,$.jsx)(X,{id:`setup-wizard-steps`,title:t.title,note:t.blurb,children:(0,$.jsx)(q,{className:`setup-table wiz-table`,cards:!0,label:`${t.title} steps`,rows:t.steps.map((e,t)=>({...e,n:t,state:s[t]?`done`:t===u?`now`:`todo`})),rowKey:e=>e.id,columns:[{key:`step`,header:`Step`,role:`title`,cell:e=>(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(`span`,{className:`wiz-step-title${e.state===`now`?` current`:``}`,children:[e.n+1,`. `,e.title]}),(0,$.jsx)(`span`,{className:`note`,children:e.detail})]})},{key:`state`,header:`Status`,cell:e=>(0,$.jsx)(`span`,{className:`wiz-row-state ${e.state}`,children:e.state===`done`?`Done`:e.state===`now`?`Next`:`To do`})},{key:`go`,header:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(i,{label:`${e.title} actions`,actions:[{label:`Go to step`,icon:me,onSelect:()=>o(e)}]})}]})})]})}var nd={google:`Google Drive`,microsoft:`Microsoft 365`},rd=e=>`/w/${e}`,id=e=>J().slugOf(e),ad=e=>{let t=id(e.name);return!!t&&t!==e.slug&&t!==e.slug.replace(/-\d+$/,``)};function od(){let e=Q(),t=ye(),{current:n}=_u(),[r,i]=(0,Z.useState)(null),[a,o]=(0,Z.useState)(null),[s,c]=(0,Z.useState)(null),[l,u]=(0,Z.useState)(!1),[d,f]=(0,Z.useState)(!1),[p,m]=(0,Z.useState)(null),[h,g]=(0,Z.useState)(null),_=e=>c({text:e instanceof Error?e.message:String(e),bad:!0}),v=(0,Z.useCallback)(()=>{o(null),e.workspaces().then(e=>i(e.workspaces),e=>o(e instanceof Error?e.message:String(e)))},[e]);(0,Z.useEffect)(v,[v]);let y=(r??[]).filter(e=>!e.archived).length,b=(0,Z.useCallback)(async t=>{try{let n=await e.createWorkspace(t);i(n.workspaces),c(null)}catch(e){_(e)}},[e]),x=(0,Z.useCallback)(async(t,n)=>{try{i((await e.renameWorkspace(t.id,n)).workspaces)}catch(e){_(e)}},[e]),S=(0,Z.useCallback)(async t=>{try{i((await e.regenerateWorkspace(t.id)).workspaces),c(null)}catch(e){_(e)}},[e]),C=(0,Z.useCallback)(async r=>{try{let a=await e.archiveWorkspace(r.id);if(r.slug===n?.slug){let e=hu(a.workspaces);if(e){pu({id:r.id,slug:r.slug,name:r.name}),location.assign(du(e.slug));return}}i(a.workspaces),t.offer({label:`Archived ${r.name}`,restore:()=>void(async()=>{try{i((await e.unarchiveWorkspace(r.id)).workspaces)}catch(e){_(e)}})()})}catch(e){_(e)}},[e,t,n]),w=(0,Z.useCallback)(async t=>{try{i((await e.unarchiveWorkspace(t.id)).workspaces)}catch(e){_(e)}},[e]),T=(0,Z.useCallback)(async t=>{try{i((await e.setDefaultWorkspace(t.id)).workspaces)}catch(e){_(e)}},[e]),E=(0,Z.useCallback)(async n=>{try{let{trash:r}=await e.deleteWorkspace(n.id);i(e=>(e??[]).filter(e=>e.id!==n.id)),t.offer({label:`Deleted ${n.name}`,restore:()=>void(async()=>{try{i((await e.restoreWorkspace(r)).workspaces)}catch(e){_(e)}})()})}catch(e){_(e)}},[e,t]),O=(r??[]).filter(e=>e.archived),k=(r??[]).filter(e=>l||!e.archived);return(0,$.jsxs)(X,{id:`setup-workspaces`,title:`Workspaces`,note:r?ud(r.length):``,children:[(0,$.jsx)(`p`,{className:`note`,children:`Workspaces contain pipelines, runs, library content, and packs. Connections can be shared.`}),r===null&&(0,$.jsx)(M,{label:`Workspaces`,error:a,onRetry:v}),r&&(0,$.jsxs)($.Fragment,{children:[O.length>0&&(0,$.jsx)(`div`,{className:`pills ws-archived`,role:`group`,"aria-label":`Show`,children:(0,$.jsxs)(`label`,{className:`pill`,children:[(0,$.jsx)(`input`,{type:`checkbox`,checked:l,onChange:e=>u(e.target.checked)}),(0,$.jsx)(`span`,{children:`Archived`})]})}),(0,$.jsx)(q,{className:`setup-table ws-table`,cards:!0,label:`Workspaces`,rows:k,rowKey:e=>e.id,columns:sd({onlyActive:y<=1,currentSlug:n?.slug??null,onRename:m,onRegenerate:g,onArchive:e=>void C(e),onUnarchive:e=>void w(e),onDelete:e=>void E(e),onSetDefault:e=>void T(e)})}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>f(!0),children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),`Add workspace`]})]}),s&&(0,$.jsx)(`p`,{className:`note${s.bad?` bad`:``}`,role:s.bad?`alert`:`status`,children:s.text}),(0,$.jsx)(ut,{undo:t.undo,onUndo:t.undoNow,onDismiss:t.dismiss}),d&&(0,$.jsx)(cd,{onCreate:e=>void b(e),onClose:()=>f(!1)}),p&&(0,$.jsx)(nt,{title:`Rename ${p.name}`,value:p.name,onCommit:e=>void x(p,e),onClose:()=>m(null)}),h&&(0,$.jsx)(ld,{workspace:h,onRegenerate:()=>void S(h),onClose:()=>g(null)})]})}function sd(e){let t=t=>t.archived?[{label:`Rename…`,icon:_.rename,onSelect:()=>e.onRename(t)},{label:`Unarchive`,icon:Xt,onSelect:()=>e.onUnarchive(t)},{label:`Delete permanently`,icon:_.remove,destructive:!0,onSelect:()=>e.onDelete(t)}]:[{label:`Rename…`,icon:_.rename,onSelect:()=>e.onRename(t)},...t.isDefault?[]:[{label:`Set as default`,icon:wn,onSelect:()=>e.onSetDefault(t)}],...e.onlyActive?[]:[{label:`Archive`,icon:Zt,onSelect:()=>e.onArchive(t)}]];return[{key:`name`,header:`Workspace`,role:`title`,sort:e=>e.name,cell:e=>e.name},{key:`slug`,header:`Address`,sort:e=>e.slug,cell:t=>(0,$.jsxs)(`span`,{className:`ws-address`,children:[(0,$.jsx)(`a`,{className:`ws-addr`,href:rd(t.slug),children:(0,$.jsx)(`code`,{children:rd(t.slug)})}),!t.archived&&ad(t)&&(0,$.jsxs)(`button`,{type:`button`,className:`ws-regen`,onClick:()=>e.onRegenerate(t),children:[(0,$.jsx)(bn,{size:12,"aria-hidden":`true`}),`Regenerate…`]})]})},{key:`pipelines`,header:`Pipelines`,sort:e=>e.pipelines,cell:e=>e.pipelines},{key:`runs`,header:`Runs`,sort:e=>e.runs,cell:e=>e.runs},{key:`lastUsed`,header:`Last used`,sort:e=>e.lastUsed??``,cell:e=>pd(e.lastUsed)},{key:`status`,header:`Status`,sort:t=>dd(t,e.currentSlug),cell:t=>{if(t.archived)return(0,$.jsx)(`span`,{className:`setup-unset`,children:`Archived`});let n=[t.slug===e.currentSlug?`Current`:``,t.isDefault?`Default`:``].filter(Boolean);return n.length?(0,$.jsx)(`span`,{className:t.slug===e.currentSlug?`ws-current`:`setup-unset`,children:n.join(`, `)}):(0,$.jsx)(`span`,{className:`setup-unset`,children:`—`})}},{key:`menu`,header:``,label:``,className:`setup-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{actions:t(e),label:`${e.name} actions`})}]}function cd({onCreate:e,onClose:t}){let n=Q(),[r]=On(`promptlab.profiles`),[i,a]=(0,Z.useState)(``),[o,s]=(0,Z.useState)(``),[c,l]=(0,Z.useState)(`nothing`),[u,d]=(0,Z.useState)({}),[f,p]=(0,Z.useState)({});(0,Z.useEffect)(()=>{let e=!0;return n.connectionShares().then(t=>{e&&d(t.shares)}).catch(()=>{}),()=>{e=!1}},[n]);let m=(0,Z.useMemo)(()=>{let e=[];for(let[t,n]of Object.entries(u)){if(n.includes(`*all`))continue;let i=t.indexOf(`:`),a=t.slice(0,i),o=t.slice(i+1),s=a===`profile`?r.list.find(e=>e.id===o)?.name:nd[o];s&&e.push({key:t,kind:a,id:o,label:s,prefill:n.includes(yt)})}return e.sort((e,t)=>e.label.localeCompare(t.label))},[u,r.list]),h=e=>e.key in f?f[e.key]:e.prefill,g=[{key:`new`,rows:[{key:`name`,label:`Name`,required:!0,control:{type:`text`,value:i,size:`wide`,onChange:a}},{key:`slug`,label:`Address`,control:{type:`text`,value:o,placeholder:rd(id(i)||`workspace`),onChange:e=>s(e.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+/,``).slice(0,J().SLUG_MAX))}},{key:`content`,label:`Starting content`,control:{type:`select`,value:c,onChange:e=>l(e),options:[{value:`nothing`,label:`Nothing`},{value:`demo`,label:`Demo pack`}]}}]}];return m.length&&g.push({key:`connections`,title:`Connections`,rows:m.map(e=>({key:e.key,label:e.label,control:{type:`checkbox`,checked:h(e),onChange:t=>p(n=>({...n,[e.key]:t}))}}))}),(0,$.jsx)(Ot,{title:`New workspace`,focusRow:`name`,sections:g,dirty:!!i.trim()||!!o.trim()||c!==`nothing`,onSave:()=>{let n=m.filter(h).map(e=>({kind:e.kind,id:e.id}));e({name:i.trim(),...o.trim()?{slug:o.trim()}:{},content:c,...m.length?{connections:n}:{}}),t()},onClose:t})}function ld({workspace:e,onRegenerate:t,onClose:n}){let r=rd(id(e.name)||`workspace`);return(0,$.jsx)(Tt,{title:`Regenerate address`,onClose:n,footer:(0,$.jsxs)(`div`,{className:`edit-actions`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:n,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,onClick:()=>{t(),n()},children:`Regenerate`})]}),children:(0,$.jsxs)(`div`,{className:`ws-regen-dialog`,children:[(0,$.jsxs)(`dl`,{className:`ws-regen-rows`,children:[(0,$.jsxs)(`div`,{children:[(0,$.jsx)(`dt`,{children:`Now`}),(0,$.jsx)(`dd`,{children:(0,$.jsx)(`code`,{children:rd(e.slug)})})]}),(0,$.jsxs)(`div`,{children:[(0,$.jsx)(`dt`,{children:`New`}),(0,$.jsx)(`dd`,{children:(0,$.jsx)(`code`,{children:r})})]})]}),(0,$.jsx)(`p`,{className:`note bad`,role:`alert`,children:`The old address stops working. Bookmarks and links to it break, and the lab will not redirect them.`})]})})}var ud=e=>e===1?`1 workspace`:`${e} workspaces`,dd=(e,t)=>e.archived?`Archived`:[e.slug===t?`Current`:``,e.isDefault?`Default`:``].filter(Boolean).join(`, `),fd=[`Jan`,`Feb`,`Mar`,`Apr`,`May`,`Jun`,`Jul`,`Aug`,`Sep`,`Oct`,`Nov`,`Dec`],pd=e=>{if(!e)return`—`;let t=new Date(e);return Number.isNaN(t.getTime())?e:`${t.getFullYear()}-${fd[t.getMonth()]}-${String(t.getDate()).padStart(2,`0`)}`},md=[{id:`connections`,label:`Connections`,icon:st},{id:`wizards`,label:`Wizards`,icon:me},{id:`workspaces`,label:`Workspaces`,icon:$t}];function hd({section:e=`connections`}){return(0,$.jsxs)(`div`,{className:`setup`,children:[(0,$.jsx)(m,{label:`Setup`,level:`section`,className:`setup-sections`,current:e,items:md.map(({id:e,label:t,icon:n})=>({key:e,label:t,href:`#/setup/${e}`,icon:n}))}),(0,$.jsx)(`section`,{className:`setup-section`,"data-section":e,children:e===`wizards`?(0,$.jsx)(td,{}):e===`workspaces`?(0,$.jsx)(od,{}):(0,$.jsx)(gd,{})})]})}function gd(){let e=Dn(),t=Q(),n=ye(),{list:r}=_u(),i=r.filter(e=>!e.archived).map(e=>({id:e.id,name:e.name})),{sharesFor:a,setFor:o,setForEach:s}=Eu(t),[c,l]=On(`promptlab.profiles`),[u]=On(`promptlab.versions`),[d]=On(`promptlab.workflows`),{list:f}=qn(),p=f??[],[m,h]=(0,Z.useState)(null),[g,v]=(0,Z.useState)(null),y=(0,Z.useRef)(g);y.current=g;let[b,x]=(0,Z.useState)(null),[S,C]=(0,Z.useState)(null),[w,T]=(0,Z.useState)({}),[E,O]=(0,Z.useState)(``),[k,A]=(0,Z.useState)(`all`),[j,M]=(0,Z.useState)({key:`name`,dir:`asc`}),[N,P]=(0,Z.useState)([]),[F,I]=(0,Z.useState)({}),[L,R]=(0,Z.useState)(()=>new Set),z=(0,Z.useRef)(null);(0,Z.useEffect)(()=>{let e=!0;return t.presets().then(t=>{e&&P(t)}).catch(()=>{}),()=>{e=!1}},[t]);let B=(0,Z.useRef)(c);B.current=c;let ee=(0,Z.useRef)(!0);(0,Z.useEffect)(()=>()=>{ee.current=!1},[]);let te=[...L].sort().join(`,`);(0,Z.useEffect)(()=>{z.current=null},[te]);let V=(0,Z.useCallback)(e=>{let t=y.current;return t?.p.id===e?t.p:B.current.list.find(t=>t.id===e)},[]),H=(0,Z.useCallback)(e=>{let t=y.current;if(t?.p.id!==e.id)return;let n={...t,p:e};y.current=n,v(n)},[]),U=(0,Z.useCallback)(()=>{let t=y.current;if(!t)return;let n=B.current,r=t.was?{...n,list:n.list.map(e=>e.id===t.p.id?t.p:e)}:{...n,list:[...n.list,t.p],at:t.p.id};B.current=r,l(r),Vn(e,`profile`,t.p.id,t.p,`idle`),v(null),h(null)},[l,e]),W=(0,Z.useCallback)(()=>{v(null),h(null)},[]),G=(0,Z.useCallback)(()=>{let e=qd(B.current.list);v({p:e,was:null,start:e}),h({id:``,focus:`name`})},[]),ne=(0,Z.useCallback)(e=>{let t=B.current.list.find(t=>t.id===e);if(!t)return;let n=`${t.name||`Profile`} copy`,r={...structuredClone(t),id:Gd(),name:n,slug:Kd(n,B.current.list),key:t.key},i={...B.current,list:[...B.current.list,r]};B.current=i,l(i)},[l]),re=(0,Z.useCallback)(e=>{let t=B.current,r=new Set(e),i=t.list.filter(e=>!r.has(e.id));if(!i.length||i.length===t.list.length)return;let a=r.has(t.at)?i[0].id:t.at,o={...t,list:i,at:a,...t.grader&&r.has(t.grader)?{grader:``}:{}};B.current=o,l(o),h(null),v(null),R(new Set);let s=t.list.filter(e=>r.has(e.id));n.offer({label:s.length===1?`Deleted ${s[0].name||`the profile`}`:`Removed ${s.length} profiles`,restore:()=>{B.current=t,l(t)}})},[l,n]),ie=(0,Z.useCallback)(e=>re([e]),[re]),ae=(0,Z.useCallback)(t=>{let n=t.doc.id,r=B.current.list.find(e=>e.id===n);r&&Vn(e,`profile`,n,r,`restore`);let i=B.current,a={...i,list:i.list.map(e=>e.id===n?t.doc:e)};B.current=a,l(a),oe()},[e,l]),oe=(0,Z.useCallback)(()=>{x(e=>(e?.back&&xe(e.id),null))},[]),se=(0,Z.useCallback)(async e=>{let n=V(e);if(!n)return;let r=J().CONNECTION_TYPES[n.type];if(r?.local){T(t=>({...t,[e]:{phase:`connected`,ms:0,lines:[`${r.label} answers each text item with its own text, and sends nothing anywhere.`]}}));return}let i=r?.keyless?``:n.key,a=Id(n,r),o=performance.now(),s=()=>Math.round(performance.now()-o),c=[];T(t=>({...t,[e]:{phase:`checking`,lines:[`Checking…`]}}));try{let o=((await t.models({url:J().addressOf(n),key:i,type:n.type,settings:Ld(n,r)})).data||[]).map(e=>e.id).sort();if(I(t=>({...t,[e]:o})),c.push(`Reached ${a}.`,`${o.length} ${o.length===1?`model`:`models`}: ${o.join(`, `)||`none`}.`),(r?.answers??[`prompt`]).includes(`prompt`)){if(n.model.trim()){let e=J().apiBase(n.url||r?.url||``),a=J().connectionRequest(n,`hi`,null,e,i),o=await t.modelsAsk({url:J().addressOf(n),key:i,type:n.type,body:a.body}),{raw:s}=J().connectionReply(n,o);c.push(``,`Asked ${n.model} “hi”; it replied:`,s||`(an empty reply)`)}else c.push(``,`No model is picked, so nothing was asked.`)}T(t=>({...t,[e]:{phase:`connected`,lines:c,ms:s()}}))}catch(t){c.push(...c.length?[``]:[],t instanceof Error?t.message:String(t)),T(t=>({...t,[e]:{phase:`error`,lines:c,ms:s()}}))}},[t,V]),ce=(0,Z.useCallback)(async e=>{let n=J().CONNECTION_TYPES[e.type],r=n?.keyless?``:e.key,i=Id(e,n),a=performance.now(),o=()=>Math.round(performance.now()-a);try{let a=await t.models({url:J().addressOf(e),key:r,type:e.type,settings:Ld(e,n),quiet:!0});if(a.error)return{state:{phase:`error`,lines:[a.error],ms:o()}};let s=(a.data||[]).map(e=>e.id).sort();return{models:s,state:{phase:`connected`,ms:o(),lines:[`Reached ${i}.`,`${s.length} ${s.length===1?`model`:`models`}: ${s.join(`, `)||`none`}.`]}}}catch(e){return{state:{phase:`error`,lines:[e instanceof Error?e.message:String(e)],ms:o()}}}},[t]),le=(0,Z.useCallback)(async e=>{let{state:t,models:n}=await ce(e);ee.current&&(n&&I(t=>({...t,[e.id]:n})),T(n=>e.id in n?n:{...n,[e.id]:t}))},[ce]),ue=(0,Z.useCallback)(e=>{let t=new Set(e),n=B.current.list.filter(e=>t.has(e.id)&&Pd(e));if(n.length){T(e=>{let t={...e};for(let e of n)t[e.id]={phase:`checking`,lines:[`Checking…`]};return t});for(let e of n)(async()=>{let{state:t,models:n}=await ce(e);ee.current&&(n&&I(t=>({...t,[e.id]:n})),T(n=>({...n,[e.id]:t})))})()}},[ce]),de=(0,Z.useCallback)((e,t)=>{if(!e.length)return;z.current||=new Map(e.map(e=>[e,a(`profile`,e)]));let r=z.current;s(`profile`,e.map(e=>({id:e,workspaces:t})));let i=e.length===1?B.current.list.find(t=>t.id===e[0])?.name||`the profile`:`${e.length} profiles`;n.offer({label:`Workspaces for ${i}`,restore:()=>{s(`profile`,[...r].map(([e,t])=>({id:e,workspaces:t&&t.length?[...t]:[qe]}))),z.current=null}})},[a,s,n]),fe=(0,Z.useRef)(!1),[pe,K]=(0,Z.useState)(!1);(0,Z.useEffect)(()=>{if(fe.current)return;let e=m?.id,t=t=>c.list.find(n=>n.id!==e&&!(n.id in w)&&!t.has(n.id)&&Pd(n));if(!t(new Set))return;fe.current=!0,K(!0);let n=new Set;(async()=>{for(let e=t(n);e;e=t(n))n.add(e.id),await le(e);fe.current=!1,ee.current&&K(!1)})()},[w,c,m,le]);let me=m?g?.p:void 0,he=me?JSON.stringify([me.id,me.url,me.type,me.key]):``;(0,Z.useEffect)(()=>{if(!me)return;let e=me;if(J().CONNECTION_TYPES[e.type]?.local||J().CONNECTION_TYPES[e.type]?.lists===!1)return;let n=J().CONNECTION_TYPES[e.type]?.keyless,r=!0,i=setTimeout(()=>{t.models({url:J().addressOf(e),key:n?``:e.key,type:e.type,quiet:!0}).then(t=>{r&&I(n=>({...n,[e.id]:(t.data||[]).map(e=>e.id).sort()}))}).catch(()=>{r&&I(t=>({...t,[e.id]:[]}))})},400);return()=>{r=!1,clearTimeout(i)}},[t,he]);let ge=(0,Z.useCallback)((e,t)=>{let n=V(e);if(!n)return;let r={...n};for(let e of Ud(n.type))r[e]=``;for(let[e,n]of Object.entries(t.opts))r[e]=n;H(r)},[V,H]),_e=(0,Z.useCallback)(e=>{T(t=>{if(!(e in t))return t;let{[e]:n,...r}=t;return r})},[]),ve=(0,Z.useCallback)((e,t,n)=>{let r=V(e);if(r){if([`url`,`key`,`model`].includes(t)&&_e(e),t===`name`){let t=B.current.list.filter(t=>t.id!==e);if(!r.slug||r.slug===Kd(r.name,t)){H({...r,name:n,slug:Kd(n,t)});return}}t===`slug`&&(n=n.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+/,``).slice(0,J().SLUG_MAX)),H({...r,[t]:n})}},[V,H,_e]),be=(0,Z.useCallback)((e,t)=>{let n=V(e);if(!n)return;let r={...n,type:t};_e(e),I(t=>({...t,[e]:[]}));let i=J().CONNECTION_TYPES[t];i?.keyless&&(r.key=``);let a=J().CONNECTION_TYPES[n.type]?.url;(i?.url||a&&r.url.trim()===a)&&(r.url=``);for(let e of Object.keys(r))[`id`,`name`,`slug`,`url`,`model`,`key`,`temperature`,`type`,...vu].includes(e)||Wd(t).includes(e)||delete r[e];H(r)},[V,H,_e]),xe=(0,Z.useCallback)((e,t)=>{let n=B.current.list.find(t=>t.id===e);n&&(v({p:structuredClone(n),was:n,start:n}),h(t?{id:e,focus:t}:{id:e}))},[]),Se=(0,Z.useCallback)(e=>u.profile?.[e]??[],[u]),Ce=m?g?.p??null:null,we=g?g.was?c.list.map(e=>e.id===g.p.id?g.p:e):[...c.list,g.p]:c.list,Te=c.grader??null,Ee=(0,Z.useMemo)(()=>{let e=new Map;for(let t of c.list)e.set(t.id,_d(t.id,d.list,p));return e},[c.list,d.list,p]),De=E.trim().toLowerCase(),ke=c.list.filter(e=>xd(k,e,w[e.id],Te)&&(!De||[e.name,e.model].some(e=>String(e??``).toLowerCase().includes(De)))),Ae=new Set(ke.map(e=>e.id)),je=ke.filter(e=>L.has(e.id)),Me=[...L].filter(e=>!Ae.has(e)).length,Ne=[{label:`Test connection`,icon:st,disabled:!je.some(Pd),onSelect:()=>ue(je.map(e=>e.id))},{label:`Remove`,icon:_.remove,destructive:!0,disabled:!je.length||je.length>=c.list.length,onSelect:()=>re(je.map(e=>e.id))}],Pe=L.size>0?(0,$.jsx)(Fe,{count:L.size,noun:`profile`,hidden:Me,actions:Ne,onClear:()=>R(new Set),children:je.length>0&&(0,$.jsx)(Oe,{label:je.length===1?`1 profile`:`${je.length} profiles`,workspaces:i,shares:Fd(je.map(e=>a(`profile`,e.id))),onChange:e=>de(je.map(e=>e.id),e)})}):null;return(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(X,{id:`setup-profiles`,title:`Target profiles`,children:[(0,$.jsxs)(`div`,{className:`setup-intro`,children:[(0,$.jsx)(`p`,{className:`note`,children:`A profile is how the lab reaches a model or an API: its type, address, key and settings. A target picks one.`}),pe&&(0,$.jsx)(Rd,{})]}),c.list.length>0&&(0,$.jsx)(Le,{noun:`profiles`,search:{label:`Search profiles`,placeholder:`Name or model`,value:E,onChange:O},filters:[],quick:{label:`Show`,value:k,onChange:e=>A(e),options:bd}}),c.list.length===0?(0,$.jsx)(`p`,{className:`note`,children:`No profiles yet.`}):ke.length===0?(0,$.jsxs)($.Fragment,{children:[Pe,(0,$.jsx)(`p`,{className:`note`,children:`No profiles match these filters.`})]}):(0,$.jsxs)($.Fragment,{children:[Pe,(0,$.jsx)(q,{className:`setup-table`,cards:!0,label:`Target profiles`,sort:j,onSort:M,rows:ke,rowKey:e=>e.id,select:{noun:`profile`,label:e=>e.name||`Unnamed profile`,selected:L,onChange:R},rowProps:e=>({className:`setup-row`,onClick:t=>{t.target.closest(`button, a, .menu`)||xe(e.id)}}),columns:Sd({tests:w,only:c.list.length===1,grader:Te,uses:e=>Ee.get(e)??[],onGrader:e=>{let t=B.current,n={...t,grader:t.grader===e?``:e};B.current=n,l(n)},onOpen:xe,onClone:ne,onRemove:ie,onTest:se,onResult:e=>C({id:e,back:!1}),workspaces:i,sharesFor:e=>a(`profile`,e),onShare:(e,t)=>o(`profile`,e,t)})})]}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:G,children:[(0,$.jsx)(D,{size:14,"aria-hidden":`true`}),`Add profile`]})]}),(0,$.jsx)(Gu,{}),(0,$.jsx)(Au,{offer:n.offer}),(0,$.jsx)(Du,{offer:n.offer}),(0,$.jsx)(ut,{undo:n.undo,onUndo:n.undoNow,onDismiss:n.dismiss}),Ce&&(0,$.jsx)(wd,{profile:Ce,slug:J().withSlugs(we).find(e=>e.id===Ce.id)?.slug??``,...m?.focus?{focus:m.focus}:{},test:w[Ce.id],models:F[Ce.id]??[],presets:N.filter(e=>(e.type??`llama.cpp`)===Ce.type),canRestore:!!g?.was&&!!Se(Ce.id).length,uses:Ee.get(Ce.id)??[],dirty:!!g&&JSON.stringify(g.p)!==JSON.stringify(g.start),onSave:U,onClose:W,onSetting:ve,onType:be,onTest:se,onResult:()=>{h(null),C({id:Ce.id,back:!0})},onRestore:()=>{W(),x({id:Ce.id,back:!0})},onApplyPreset:e=>ge(Ce.id,e)}),S&&w[S.id]&&(0,$.jsx)(zd,{test:w[S.id],onClose:()=>{S.back&&y.current?.p.id===S.id&&h({id:S.id}),C(null)}}),b&&(0,$.jsx)(Ad,{versions:Se(b.id),onClose:oe,onRestore:ae})]})}function _d(e,t,n){let r=[];for(let n of t)J().profileIds(n.work).includes(e)&&r.push({kind:`pipeline`,id:n.id,name:n.name||`Untitled pipeline`});for(let t of n)t.grader?.id===e&&r.push({kind:`group`,id:t.id,name:t.name||`Untitled eval group`});return r}function vd(e){if(!e.length)return`Not used`;let t=(t,n,r)=>{let i=e.filter(e=>e.kind===t);return i.length===0?null:i.length===1?i[0].name:`${i.length} ${r}`};return[t(`pipeline`,`pipeline`,`pipelines`),t(`group`,`eval group`,`eval groups`)].filter(Boolean).join(`, `)}function yd({uses:e}){return(0,$.jsxs)(`div`,{className:`setup-usedby`,children:[(0,$.jsx)(`span`,{className:`setup-usedby-label`,children:`Used by`}),e.length===0?(0,$.jsx)(`span`,{className:`setup-unset`,children:`Not used`}):(0,$.jsx)(`ul`,{className:`setup-usedby-list`,children:e.map(e=>(0,$.jsxs)(`li`,{children:[(0,$.jsx)(`a`,{href:e.kind===`group`?`#/library/datasets/${encodeURIComponent(e.id)}`:`#/runs`,children:e.name}),(0,$.jsx)(`span`,{className:`setup-usedby-kind`,children:e.kind===`group`?`eval group`:`pipeline`})]},`${e.kind}-${e.id}`))})]})}var bd=[{value:`all`,label:`All`},{value:`connected`,label:`Connected`},{value:`attention`,label:`Needs attention`},{value:`grader`,label:`Grader`}];function xd(e,t,n,r){if(e===`all`)return!0;if(e===`grader`)return r===t.id;let i=Nd(t,n)?.kind;return e===`connected`?i===`connected`:i===`key-needed`||i===`unreachable`}function Sd({tests:e,only:t,grader:n,onGrader:r,onOpen:i,onClone:a,onRemove:o,onTest:s,onResult:c,uses:l,workspaces:u,sharesFor:d,onShare:f}){let p=e=>e.name||`Unnamed profile`,m=e=>J().CONNECTION_TYPES[e.type]?.label??e.type,h=c=>[{label:`Edit…`,icon:_.edit,onSelect:()=>i(c.id)},{label:`Rename…`,icon:_.rename,onSelect:()=>i(c.id,`name`)},{label:`Clone`,icon:_.clone,onSelect:()=>a(c.id)},{label:`Test connection`,icon:st,busy:e[c.id]?.phase===`checking`,onSelect:()=>void s(c.id)},...(J().CONNECTION_TYPES[c.type]?.answers??[`prompt`]).includes(`prompt`)&&!J().CONNECTION_TYPES[c.type]?.local?[{label:n===c.id?`Stop grading`:`Make grader`,icon:mn,onSelect:()=>r(c.id)}]:[],...t?[]:[{label:`Remove`,icon:_.remove,destructive:!0,onSelect:()=>o(c.id)}]];return[{key:`name`,header:`Profile`,role:`title`,sort:p,cell:t=>{let r=Nd(t,e[t.id]);return(0,$.jsxs)(`div`,{className:`setup-profile`,children:[r&&(0,$.jsx)(`button`,{type:`button`,className:`setup-dot ${r.kind}`,title:r.title,"aria-label":`Connection status: ${r.label}`,onClick:()=>{e[t.id]?c(t.id):s(t.id)},children:(0,$.jsx)(`span`,{className:`setup-dot-mark`,"aria-hidden":`true`})}),(0,$.jsxs)(`span`,{className:`setup-profile-main`,children:[(0,$.jsxs)(`span`,{className:`setup-profile-name`,children:[(0,$.jsx)(`button`,{type:`button`,className:`setup-open`,onClick:()=>i(t.id),children:p(t)}),n===t.id&&(0,$.jsx)(`span`,{className:`setup-badge`,children:`Grader`})]}),(0,$.jsx)(`span`,{className:`setup-sub`,children:m(t)})]})]})}},{key:`model`,header:`Model`,label:``,sort:e=>e.model.trim(),cell:e=>(0,$.jsx)(Cd,{p:e})},{key:`used`,header:`Used by`,label:``,sort:e=>vd(l(e.id)),cell:e=>{let t=l(e.id);return t.length?(0,$.jsx)(`span`,{className:`setup-used`,children:vd(t)}):(0,$.jsx)(`span`,{className:`setup-unset`,children:`Not used`})}},{key:`workspaces`,header:`Workspaces`,sort:e=>Tu(d(e.id)),cell:e=>(0,$.jsx)(Oe,{label:p(e),workspaces:u,shares:d(e.id),onChange:t=>f(e.id,t)})},{key:`menu`,header:``,label:``,className:`setup-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(Ue,{actions:h(e),label:`${p(e)} actions`})}]}function Cd({p:e}){let t=J().CONNECTION_TYPES[e.type];return t?.local?(0,$.jsx)(`span`,{className:`setup-unset`,children:`—`}):(t?.answers??[`prompt`]).includes(`prompt`)?e.model.trim()?(0,$.jsx)($.Fragment,{children:e.model.trim()}):(0,$.jsx)(`span`,{className:`setup-unset`,children:`Not set`}):(0,$.jsx)(`span`,{className:`setup-unset`,children:`from the request`})}function wd({profile:e,slug:t,focus:n,test:r,models:i,presets:a,canRestore:o,uses:s,dirty:c,onSave:l,onClose:u,onSetting:d,onType:f,onTest:p,onResult:m,onRestore:h,onApplyPreset:g}){let v=J().CONNECTION_TYPES[e.type],y=(v?.answers??[`prompt`]).includes(`prompt`),b=(0,Z.useMemo)(()=>kd(e),[e]),x=t=>n=>d(e.id,t,n),S=[{key:`name`,label:`Name`,required:!0,control:{type:`text`,value:e.name,onChange:x(`name`)}},{key:`slug`,label:`Slug`,required:!0,...e.slug&&J().slugOf(e.slug)!==t?{hint:`Taken`}:{},control:{type:`text`,value:e.slug??``,placeholder:t,onChange:x(`slug`)}},{key:`type`,label:`Type`,required:!0,hint:v?.description,control:{type:`select`,value:e.type,onChange:t=>f(e.id,t),options:Object.entries(J().CONNECTION_TYPES).filter(([t,n])=>n.picker!==!1||t===e.type).map(([e,t])=>({value:e,label:t.label}))}},...v?.local?[]:[...v?.url?[]:[{key:`url`,label:`Address`,control:{type:`text`,value:e.url,placeholder:`the lab's Ollama`,onChange:x(`url`)}}],...v?.keyless?[]:[{key:`key`,label:`Key`,control:{type:`secret`,value:su(e.key)?``:e.key,held:su(e.key),onChange:x(`key`)}}],...y?[{key:`model`,label:`Model`,control:Hd(e.model,i,x(`model`))}]:[]]],C={...e},w=(v?.settings??[]).filter(e=>!e.appliesTo||e.appliesTo(C)).map(t=>Td(e,t,x(t.key))),[T]=(0,Z.useState)(()=>({vision:vu.some(t=>String(e[t]??``)!==``),address:!!e.url.trim()})),E=!v?.local&&!v?.keyless?[{key:`ci`,label:`CI secret`,hint:`The run reads this profile's key from this variable`,control:{type:`code`,text:J().keyVar(e.id,t)}}]:[],D=[{key:`identity`,rows:S},...w.length?[{key:`settings`,title:`Settings`,rows:w}]:[],{key:`use`,title:`Use`,rows:E,children:(0,$.jsx)(yd,{uses:s})},...v?.url&&!v.local?[{key:`advanced`,title:`Advanced`,folded:!T.address,rows:[{key:`url`,label:`Address`,hint:`Only for a proxy or a gateway`,control:{type:`text`,value:e.url,placeholder:v.url,onChange:x(`url`)}}]}]:[],...v?.local||!y?[]:[{key:`image`,title:`Vision Capabilities`,folded:!T.vision,rows:Cu(e,x)},{key:`debug`,title:`Debug`,rows:b,folded:!0}]],O=[{label:`Restore…`,icon:ke,disabled:!o,onSelect:h},...a.map(e=>({label:`Apply preset: ${e.label}`,icon:_.preset,onSelect:()=>g(e)})),...(v?.templates??[]).map(t=>({label:`Apply template: ${t.label}`,icon:_.preset,onSelect:()=>g({id:t.id,label:t.label,type:e.type,opts:t.settings})}))];return(0,$.jsx)(Ot,{title:e.name||`Unnamed profile`,...n?{focusRow:n}:{},sections:D,menu:O,actions:v?.tests===!1?[]:[{label:`Test connection`,icon:st,busy:r?.phase===`checking`,onSelect:()=>p(e.id),outcome:r?{label:jd[r.phase],bad:r.phase===`error`,onOpen:m}:void 0}],dirty:c,onSave:l,onClose:u})}function Td(e,t,n){let r=e[t.key]??t.default??``;if(t.control===`select`&&t.choices)return{key:t.key,label:t.label,control:{type:`select`,value:r,options:t.choices,onChange:n}};if(t.key===`grammar`)return{key:t.key,label:t.label,control:{type:`textarea`,value:r,onChange:n}};let i=r?null:Dd(e,t.key),a={type:`text`,value:r,onChange:n,...i?.placeholder?{placeholder:i.placeholder}:{},...i?.numeric?{size:`num`}:{}};return{key:t.key,label:t.label,control:a}}var Ed=`424242`;function Dd(e,t){let n=J().apiBase(e.url),r,i;try{r=Od(J().connectionRequest({...e,[t]:``},`(prompt)`,null,n).body),i=Od(J().connectionRequest({...e,[t]:Ed},`(prompt)`,null,n).body)}catch{return null}let a=Object.keys(i).find(e=>i[e]===Ed||i[e]===JSON.stringify(Ed));if(a===void 0)return null;let o=i[a]===Ed,s=a in r?JSON.parse(r[a]):null;return s===null?{numeric:o}:{placeholder:String(s),numeric:o}}function Od(e,t=``,n={}){if(e&&typeof e==`object`&&!Array.isArray(e))for(let[r,i]of Object.entries(e))Od(i,t?`${t}.${r}`:r,n);else n[t]=JSON.stringify(e)??`null`;return n}function kd(e){let t=J().apiBase(e.url),n=[];try{let{url:r,body:i}=J().connectionRequest(e,`(prompt)`,null,t);n.push({key:`endpoint`,label:`Endpoint`,required:!0,control:{type:`code`,text:r}});let a=i,o=a.messages,s=Array.isArray(o?.[0]?.content)?o[0].content.find(e=>e.type===`text`)?.text??``:o?.[0]?.content??``;n.push({key:`prompt`,label:`Prompt`,required:!0,control:{type:`static`,text:String(s).slice(0,40)}}),n.push({key:`image`,label:`Image`,required:!0,control:{type:`code`,text:wu(e)}});for(let[e,t]of Object.entries(a))e!==`messages`&&n.push({key:e,label:e,control:{type:`code`,text:typeof t==`object`?JSON.stringify(t):String(t)}})}catch(e){n.push({key:`error`,label:`Error`,required:!0,control:{type:`static`,text:e instanceof Error?e.message:String(e)}})}return n}function Ad({versions:e,onClose:t,onRestore:n}){return(0,$.jsx)(Tt,{title:`Restore profile`,onClose:t,children:(0,$.jsxs)(`div`,{className:`version-list`,children:[e.length===0&&(0,$.jsx)(`p`,{className:`note`,children:`No versions of this profile yet.`}),e.map((e,t)=>(0,$.jsxs)(`div`,{className:`version-row`,children:[(0,$.jsxs)(`span`,{className:`version-when`,children:[Yd(e.at),`, `,Jd(e.reason)]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>n(e),children:`Restore`})]},`${e.at}-${t}`))]})})}var jd={checking:`Checking…`,connected:`Connected`,error:`Error`},Md={untested:`Not tested`,connected:`Connected`,"key-needed":`Key needed`,unreachable:`Unreachable`};function Nd(e,t){let n=J().CONNECTION_TYPES[e.type];if(n?.local||n?.tests===!1)return null;let r=(e,t)=>({kind:e,label:Md[e],title:t?`${Md[e]} — ${t}`:Md[e]});return!n?.keyless&&!e.key?r(`key-needed`,`no key is set`):!t||t.phase===`checking`?r(`untested`):t.phase===`error`?r(`unreachable`,t.lines[t.lines.length-1]):r(`connected`)}function Pd(e){let t=J().CONNECTION_TYPES[e.type];return t?.local||t?.tests===!1?!1:t?.keyless?!0:!!e.key}function Fd(e){if(!e.length)return;let t=Tu(e[0]);return e.every(e=>Tu(e)===t)?e[0]:void 0}var Id=(e,t)=>e.url.trim()||t?.url||`the lab's Ollama`;function Ld(e,t){let n=Object.fromEntries((t?.settings??[]).map(t=>[t.key,String(e[t.key]??``)]));if(`testPath`in n&&!n.testPath.trim()){let r=t?.templates?.find(t=>Bd(t.settings.url,e.url));r?.settings.testPath&&(n.testPath=r.settings.testPath)}return n}function Rd(){return B(!0)?(0,$.jsxs)(`span`,{className:`loading-status`,role:`status`,children:[(0,$.jsx)(F,{}),`Checking…`]}):null}function zd({test:e,onClose:t}){return(0,$.jsx)(Ot,{title:`Test connection`,sections:[{key:`result`,rows:[{key:`time`,label:`Time`,required:!0,control:{type:`static`,text:e.ms==null?`—`:Vd(e.ms)}},{key:`result`,label:`Result`,required:!0,control:{type:`textarea`,value:e.lines.join(`
12
+ `)}}]}],onClose:t})}function Bd(e,t){let n=e=>{let t=String(e??``).trim();if(!t)return``;try{return new URL(/^https?:\/\//.test(t)?t:`https://${t}`).origin}catch{return``}},r=n(e);return!!r&&r===n(t)}var Vd=e=>e<1e3?`${e} ms`:`${(e/1e3).toFixed(1)}s`;function Hd(e,t,n){if(!t.length)return{type:`text`,value:e,placeholder:`model name`,onChange:n};let r=e&&!t.includes(e)?[e,...t]:[...t];return{type:`select`,value:e,onChange:n,options:[...e?[]:[{value:``,label:`Pick a model`}],...r.map(e=>({value:e,label:e}))]}}function Ud(e){return J().CONNECTION_TYPES[e]?.settings.map(e=>e.key).filter(e=>e!==`temperature`)??[]}function Wd(e){return J().CONNECTION_TYPES[e]?.settings.map(e=>e.key)??[]}var Gd=()=>`p${Date.now().toString(36)}${Math.random().toString(36).slice(2,6)}`,Kd=(e,t)=>J().uniqueSlug(J().slugOf(e),new Set(J().withSlugs(t).map(e=>e.slug))),qd=e=>({id:Gd(),name:`New profile`,slug:Kd(`New profile`,e),url:``,model:``,key:``,temperature:``,nPredict:``,maxTokens:``,type:`ollama`}),Jd=e=>e===`run`?`before a run`:e===`restore`?`before a restore`:`after editing`,Yd=e=>{let t=new Date(e);return Number.isNaN(t.getTime())?e:t.toLocaleString()},Xd=[];function Zd(e,t){let n=Q(),[r,i]=(0,Z.useState)(null);(0,Z.useEffect)(()=>{if(t===null)return;let r=!0,a=An(n);return a.runDataset(e).then(n=>{if(n){r&&i({run:e,cases:J().upgradeDatasetBody(n).cases,current:!1});return}return a.dataset(t).then(t=>{r&&i({run:e,cases:J().upgradeDatasetBody(t.body).cases,current:!0})}).catch(()=>{})}).catch(()=>{}),()=>{r=!1}},[n,e,t]);let a=t!==null&&r?.run===e?r:null;return{cases:a?a.cases:Xd,current:a?.current??!1}}var Qd=(e,t)=>`${J().targetLetter(t)} ${e.scenarios[t]?.name??``}`.trim(),$d=e=>e?`${e.passed}/${e.ran}`:(0,$.jsx)(`span`,{className:`note`,children:`n/a`});function ef({id:e,view:t}){let n=Ci(t),r=t.evals.flatMap((e,r)=>{let i=n.rows[r];return[{kind:`group`,key:e.id,name:i?.name??``,verdict:(i?.cells??[]).map(e=>e.pass)},...ki(t,e.id).map(t=>({kind:`metric`,key:`${e.id}:${t.key}`,label:t.label,cells:t.cells}))]});return(0,$.jsx)(`div`,{className:`scrollx`,style:{"--scenarios":t.scenarios.length},children:(0,$.jsx)(q,{className:`egtable recorded`,label:`Evals by target`,rows:r,rowKey:e=>e.key,defaultSort:null,rowProps:e=>e.kind===`group`?{className:`eg-group`,"data-group":e.key}:{className:`eg-metric`},columns:[{key:`eval`,header:`Eval`,label:``,role:`title`,sort:null,className:`col-eg`,cell:e=>e.kind===`group`?(0,$.jsx)(`span`,{className:`eg-name`,children:e.name}):(0,$.jsx)(`span`,{className:`eg-metric-name`,children:e.label})},...t.scenarios.map((e,n)=>({key:`t-${n}`,header:Qd(t,n),label:Qd(t,n),sort:null,className:`num`,cell:e=>e.kind===`group`?e.verdict[n]==null?(0,$.jsx)(`span`,{className:`eg-none`,children:`—`}):(0,$.jsx)(Hr,{pass:e.verdict[n]}):$d(e.cells[n])}))]})})}function tf({view:e,x:t,evalId:n,onClose:r}){let i=e.items[t]??e.results[t]?.name??`Call`;return(0,$.jsx)(Tt,{title:i,wide:!0,onClose:r,children:(0,$.jsx)(`div`,{className:`rec-compare`,style:{"--scenarios":e.scenarios.length},children:e.scenarios.map((r,i)=>{let a=Ei(e.results[t]?.scenarios?.[i]?.res),o=n?Di(e,t,i,n):[];return(0,$.jsxs)(`div`,{className:`rec-side`,children:[(0,$.jsx)(`h3`,{className:`rec-side-title`,children:Qd(e,i)}),(0,$.jsx)(`pre`,{className:`raw`,children:a??`(no reply)`}),o.length>0&&(0,$.jsx)(`ul`,{className:`rec-checks`,"aria-label":`${Qd(e,i)} checks`,children:o.map((e,t)=>(0,$.jsxs)(`li`,{children:[(0,$.jsx)(`span`,{className:`eval-name`,children:e.label}),(0,$.jsx)(Hr,{pass:e.pass})]},t))})]},r.id)})})})}function nf(e){let t=(e.metrics??[]).filter(e=>e.weight!==0);return{wanted:t.filter(e=>!e.not&&(e.type===`contains-all`||e.type===`contains-any`)).flatMap(e=>J().metricLines(e.values)),forbidden:t.filter(e=>e.not&&e.type===`contains`).map(e=>({value:String(e.value??``),except:J().metricLines(e.except)}))}}var rf=e=>e==null?`—`:Qr(e);function af(e){return e?.transcript||[]}function of(e,t){return t.flatMap((n,r)=>{let i=af(e?.scenarios?.[r]?.res);return t.length>1?i.map(e=>({...e,scenario:n.name})):i})}function sf({stages:e}){if(!e.length)return null;let t=!e.some(e=>e.scenario)&&e.length>1?`, ${e.map(e=>rf(e.ms)).join(` + `)}`:``;return(0,$.jsxs)(`div`,{className:`transcript`,children:[(0,$.jsxs)(`div`,{className:`meta`,children:[`Transcript`,t]}),e.map((e,t)=>(0,$.jsxs)(`pre`,{children:[(0,$.jsxs)(`b`,{children:[e.scenario?`${e.scenario}, `:``,`job `,e.n,` `,e.withImage?`(with image)`:`(text only)`,e.kind?`, ${e.kind}`:``,e.conn?`, ${e.conn}`:``,`, `,rf(e.ms),` — sent`]}),`
13
+ `,e.sent,`
14
+ `,(0,$.jsx)(`b`,{children:`— returned`}),`
15
+ `,e.got||(e.error?`(nothing)`:``),e.error?`\n<b className="fault">Error - ${e.error}</b>`:``]},t))]})}function cf({text:e,kind:t}){return(0,$.jsx)(`span`,{className:`term${t?` ${t}`:``}`,children:e})}function lf({res:e,kase:t}){let n=e?.dropped||[];if(!n.length)return null;let r=e=>t?nf(t).wanted.some(t=>J().termIn([e],t)):!1;return(0,$.jsxs)(`details`,{className:`dropped`,children:[(0,$.jsxs)(`summary`,{children:[`Dropped `,n.length]}),n.map((e,t)=>(0,$.jsxs)(`div`,{className:`droppedrow`,children:[(0,$.jsx)(cf,{text:e.item,kind:`no`}),(0,$.jsxs)(`span`,{className:`note`,children:[e.by,r(e.item)&&(0,$.jsx)(`b`,{className:`fault`,children:`, wanted`})]})]},t))]})}function uf({kase:e}){return(0,$.jsx)($.Fragment,{children:(e.metrics??[]).map((e,t)=>(0,$.jsx)(`div`,{className:`wants${e.weight===0?` watched`:``}`,children:Ji(e)},t))})}var df=e=>e?.transcript?.at(-1)?.kind===`text`;function ff({res:e}){return e?.error?(0,$.jsx)(`span`,{className:`err`,children:e.error}):df(e)?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`pre`,{children:e?.transcript?.at(-1)?.got||`(empty)`}),(0,$.jsx)(`div`,{className:`meta`,children:rf(e?.ms)})]}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`div`,{className:`chips`,children:(e?.terms||[]).map((e,t)=>(0,$.jsx)(cf,{text:String(e)},t))}),(0,$.jsx)(lf,{res:e,kase:null}),(0,$.jsxs)(`div`,{className:`meta`,children:[(e?.terms||[]).length,` items, `,rf(e?.ms)]})]})}function pf({res:e}){if(e?.error)return(0,$.jsx)(`span`,{className:`err`,children:e.error});let t=e?.transcript?.at(-1)?.got??e?.raw??``;return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`pre`,{children:t||`(empty)`}),(0,$.jsxs)(`div`,{className:`meta`,children:[rf(e?.ms),`, `,df(e)?`answered as text`:`read as ${(e?.terms||[]).length} items`]})]})}function mf({tab:e,undo:t}){let n=Q(),r=ye(),i=t??r,{setShow:a,openRun:o,closeRun:s}=Dr(),{openCaseEditor:c}=Ma(),[l,u]=(0,Z.useState)(null),[d,f]=(0,Z.useState)(!1),[p,m]=(0,Z.useState)(null),[h,g]=(0,Z.useState)(new Set),[v,y]=(0,Z.useState)(!1),[b,x]=(0,Z.useState)(null),[S,C]=(0,Z.useState)(null),[w,E]=(0,Z.useState)(0),[D,O]=(0,Z.useState)(null),[k,A]=(0,Z.useState)(null),j=Kn();(0,Z.useEffect)(()=>{let t=!0,r=!1,i,a=async()=>{try{let o=await n.run(e.id);if(!t)return;r=!0,u(o),C(null),li(o.status)||(i=setTimeout(()=>void a(),1e3))}catch(e){if(!t)return;r?i=setTimeout(()=>void a(),1e3):C(e instanceof Gt?e.message:`Run not loaded: server unreachable.`)}};return a(),()=>{t=!1,i&&clearTimeout(i)}},[n,e.id,w]);let N=(0,Z.useMemo)(()=>l?Ai(l):null,[l]),P=N?J().evalsDataset(N.snapshot)?.id??null:null,I=N?wi(N):[],L=I.some(Boolean),R=Ti(I),z=Jn(L?P:null),B=Zd(e.id,P),ee=(0,Z.useMemo)(()=>new Map(B.cases.map(e=>[J().caseItem(e),e])),[B.cases]),te=(0,Z.useCallback)(async()=>{O(null);try{let{run:t}=await n.rerunRun(e.id);o(t.id,{at:t.submittedAt,from:e.from}),i.offer({label:Yl(t),restore:()=>{Xl(n,t.id),s(t.id)}})}catch(e){O(`Not re-run: ${e instanceof Error?e.message:`server unreachable`}.`)}},[n,e.id,e.from,o,s,i]),V=(0,Z.useCallback)(async()=>{f(!0);try{await n.cancel(e.id)}catch{}f(!1)},[n,e.id]);if(!N)return(0,$.jsx)(X,{id:`run-${e.id}`,title:Ur(e.at)||`Run`,children:(0,$.jsx)(M,{label:`Run`,error:S,onRetry:()=>{C(null),E(e=>e+1)}})});let H=N.progress,U=H.n??H.total??0,W=!li(N.status),G=H.total&&U!=null&&W?Math.round(U/H.total*100):null,ne=Qi(N,B.cases),re=N.scenarios.map((e,t)=>ea(N,B.cases,t)),ie=Bi(N),ae=(e,t,n)=>{let r=new Set(e);r.has(t)?r.delete(t):r.add(t),n(r)},oe=async(e,t,n)=>{let r=Math.max(0,N.scenarios.findIndex((t,n)=>gi(N,e,n))),i=(t.scenarios??[])[r]?.res;i&&await dt(Yi(N.snapshot,n,i,r))&&(y(!0),setTimeout(()=>y(!1),1600))},ce=async()=>{let e=ne.map(e=>Yi(N.snapshot,e.kase,e.res,e.scenarioIndex)).join(`
16
+
17
+ `+`-`.repeat(60)+`
18
+
19
+ `);await dt(e)&&(y(!0),setTimeout(()=>y(!1),1600))},le=[...ne.length?[{label:`Copy all failures`,icon:ft,onSelect:()=>void ce()}]:[],...l&&Jl(l)?[{label:`Re-run`,icon:Sn,onSelect:()=>void te()}]:[]],ue={key:`show`,label:`Show`,value:e.show,onChange:t=>a(e.id,t),options:[{value:`all`,label:`All`},{value:`failed`,label:`Failed`}]},de=!!P,fe=N.evals.find(e=>J().casesRef(e))?.id??null,pe=!!(N.content&&J().CONTENT_TYPES[N.content.type]?.inline),K=(e,t)=>{let n=z?.doc?.body;if(!P||!n||R<0)return;let{body:r,taken:a}=J().takeAsExpected(n,{[e]:Ei(N.results[t]?.scenarios?.[R]?.res)});if(!a.length){O(`Not taken: ${e} has no Target B reply to take.`);return}O(null),j.edit(P,()=>r),i.offer({label:`Took ${e} as expected`,restore:()=>j.edit(P,()=>n)})},me=e=>{let t=N.results[e]?.name;return t?ee.get(t):void 0},he=t=>{let r=N.results[t],i=me(t),a=!!r&&!r.unrun&&li(N.status),o=(r?.scenarios??[]).some(e=>e?.res?.transcript?.length),s=[];if(o&&s.push({label:`Show details`,icon:lt,onSelect:()=>ae(h,t,g)}),a&&r?.name&&i&&s.push({label:`Edit case`,icon:_.edit,onSelect:()=>P&&c(r.name,{dataset:P,resultFor:e=>N.results[t]?.scenarios?.[e]?.res??null,scenarioCount:N.scenarios.length,files:N.results.filter(e=>e.kind!==`text`).map(e=>e.name??``)})}),a&&L&&R>=0&&r?.name){let e=r.name;s.push({label:`Take B as expected`,icon:De,onSelect:()=>K(e,t)})}return a&&N.id&&s.push({label:`Re-run row`,icon:T,onSelect:()=>void n.rerun(e.id,t).then(e=>u(e.run)).catch(()=>{})}),a&&i&&hf(N,t)&&s.push({label:`Copy as AI prompt`,icon:ft,onSelect:()=>void oe(t,r,i)}),s};return(0,$.jsxs)(`div`,{className:`runview`,children:[(0,$.jsxs)(X,{id:`run-${e.id}`,title:N.name||Ur(e.at),actions:le,children:[(0,$.jsxs)(`div`,{className:`runhead`,children:[ie&&(0,$.jsxs)(`span`,{className:`note run-queue`,children:[(0,$.jsx)(`span`,{className:`run-state`,children:ie.state}),ie.detail&&(0,$.jsx)(`span`,{className:`run-item`,children:ie.detail})]}),W&&(0,$.jsxs)(`button`,{type:`button`,className:`ghost stop-btn`,disabled:d,onClick:()=>void V(),children:[d?(0,$.jsx)(F,{}):(0,$.jsx)(se,{size:14,"aria-hidden":`true`}),`Stop`]})]}),W&&(0,$.jsx)(`div`,{className:`rrbar`,children:G==null?(0,$.jsx)(`span`,{className:`rrprogress`,children:`Waiting`}):(0,$.jsxs)(`div`,{className:`rrmeter`,role:`progressbar`,"aria-valuenow":G,"aria-valuemin":0,"aria-valuemax":100,children:[(0,$.jsx)(`div`,{className:`rrbarfill`,style:{width:`${G}%`}}),(0,$.jsxs)(`span`,{className:`rrpct`,style:{"--pct":`${G}%`},children:[G,`%`]})]})}),l?.rerunOf&&(0,$.jsxs)(`p`,{className:`note rrorigin`,children:[`Re-run of`,` `,l.rerunOfAt?(0,$.jsx)(`a`,{href:`#`,onClick:t=>{t.preventDefault(),o(l.rerunOf,{at:l.rerunOfAt,from:e.from})},children:Ur(l.rerunOfAt)}):`a cleared run`]}),N.error&&(0,$.jsx)(`p`,{className:`note bad rrstatus`,role:`status`,children:N.error}),D&&(0,$.jsx)(`p`,{className:`note bad rrstatus`,role:`status`,children:D}),(0,$.jsx)(`div`,{id:`run-${e.id}-stats`,className:`runstats`,children:(0,$.jsx)(Mr,{label:`Run Statistics`,...Fi(N)})}),v&&(0,$.jsx)(`p`,{className:`note`,role:`status`,children:`Copied to clipboard.`})]}),N.evals.length>0&&(L?(0,$.jsx)(X,{id:`run-${e.id}-evals`,title:`Evals`,children:(0,$.jsx)(ef,{id:`run-${e.id}-evals-t`,view:N})}):(0,$.jsx)(_f,{id:`run-${e.id}-evals`,summary:Ci(N),scenarios:N.scenarios,onScenario:x})),(0,$.jsxs)(X,{id:`run-${e.id}-results`,title:`Results`,children:[(0,$.jsx)(Le,{noun:`results`,filters:[ue],lead:(0,$.jsx)(`p`,{className:`note`,children:Wr(N.items.length,`item`)}),count:{total:N.items.length,shown:e.show===`failed`?N.items.filter((e,t)=>hf(N,t)).length:N.items.length}}),B.current&&(0,$.jsx)(`p`,{className:`note`,children:`Graded before the lab kept a copy of its dataset: the expectations are the dataset as it is now.`}),(0,$.jsx)(`div`,{className:`scrollx`,style:{"--scenarios":N.scenarios.length},children:(0,$.jsx)(q,{className:[de&&!L?`graded`:``,L?`recorded`:``,`rrtable`].filter(Boolean).join(` `),defaultSort:null,label:`Results`,rows:N.items.map((e,t)=>t),rowKey:e=>`${N.items[e]}-${e}`,pinned:(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(`tr`,{className:`scenstats`,children:[(0,$.jsx)(`td`,{className:`scenstats-label`,children:`Statistics`}),de&&!L&&(0,$.jsx)(`td`,{className:`scenstats-blank`}),N.scenarios.map((e,t)=>(0,$.jsx)(`td`,{className:`scenstat`,"data-label":L?Qd(N,t):e.name,children:(0,$.jsx)(Mr,{dense:!0,label:`${e.name} statistics`,...Li(N,t)})},e.id))]}),re.some(e=>e.length>0)&&P&&(0,$.jsx)(gf,{from:Ur(e.at)||N.name,datasetId:P,failing:re,scenarios:N.scenarios,items:N.items.length,graded:de&&!L})]}),rowProps:t=>({"data-item":t,className:h.has(t)?`hasdetail`:void 0,hidden:e.show===`failed`&&!hf(N,t)}),after:t=>h.has(t)&&(0,$.jsx)(`tr`,{className:`rrdetail`,"data-detail":t,hidden:e.show===`failed`&&!hf(N,t),children:(0,$.jsx)(`td`,{colSpan:+!!L+1+(de&&!L?1:0)+N.scenarios.length,children:(0,$.jsx)(sf,{stages:of(N.results[t],N.scenarios)})})}),columns:[{key:`item`,header:pe?`Text`:`Item`,label:``,className:`col-item`,sort:e=>N.items[e]||``,cell:e=>{let t=N.results[e],n=N.items[e];return(0,$.jsxs)($.Fragment,{children:[t?.name&&!pe&&xa(t.name)?(0,$.jsx)(`button`,{type:`button`,className:`thumb`,"aria-label":`Open ${t.name}`,onClick:()=>m(t.name),children:(0,$.jsx)(`img`,{src:Hi(N,t.name),alt:``,loading:`lazy`,"data-name":t.name})}):null,(0,$.jsxs)(`div`,{className:`rritem`,children:[L&&t?.name?(0,$.jsx)(`button`,{type:`button`,className:`rrcall`,onClick:()=>A(e),children:n||``}):(0,$.jsx)(`div`,{className:`meta`,children:n||``}),(0,$.jsx)(Ue,{actions:he(e),label:`${n||`row`} actions`})]})]})}},...de&&!L?[{key:`expected`,header:`Expected`,className:`col-expected`,sort:e=>{let t=me(e);return t&&!t.todo?t.metrics?.length??0:null},cell:e=>{let t=me(e);return N.results[e]?t&&!t.todo?(0,$.jsx)(uf,{kase:t}):(0,$.jsx)(`span`,{className:`note`,children:`n/a`}):``}}]:[],...N.scenarios.map((e,t)=>({key:`scenario-${t}`,header:L?Qd(N,t):e.name,label:L?Qd(N,t):e.name,onHeader:()=>x(t),cell:e=>(0,$.jsx)(vf,{item:N.results[e],i:t,readings:(N.outcomes[t]??[]).filter(e=>!e.whole).map(t=>({label:t.label,dataset:t.id===fe,score:t.items[e]??null}))})}))]})})]}),b!=null&&(0,$.jsx)(mt,{run:N.snapshot,i:b,onClose:()=>x(null)}),k!=null&&(0,$.jsx)(tf,{view:N,x:k,evalId:fe,onClose:()=>A(null)}),p&&(0,$.jsx)(Sf,{src:Ui(N,p),name:p,onClose:()=>m(null)})]})}var hf=(e,t)=>{let n=e.results[t];return!!n&&(!!n.unrun||(n.scenarios??[]).some((n,r)=>gi(e,t,r)||!!n?.res?.error))};function gf({from:e,datasetId:t,failing:n,scenarios:r,items:i,graded:a}){let{showCases:o}=wr(),{list:s}=qn(),c=s!==null&&!s.some(e=>e.id===t),l=r.length>1;return(0,$.jsxs)(`tr`,{className:`reasons`,children:[(0,$.jsx)(`td`,{className:`scenstats-label`,children:`Reasons`}),a&&(0,$.jsx)(`td`,{className:`scenstats-blank`}),r.map((r,a)=>{let s=r.name,u=n[a]??[];return(0,$.jsx)(`td`,{className:`reasons-cell`,"data-label":s,children:u.length===0?(0,$.jsx)(`span`,{className:`reasons-none`,children:`No failures`}):(0,$.jsx)(`ul`,{className:`reasons-list`,"aria-label":`${s} reasons`,children:u.map(n=>{let r=n.files.length,a=(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`span`,{className:`reason-n`,children:r}),(0,$.jsxs)(`span`,{className:`reason-t`,children:[n.label,n.note&&(0,$.jsxs)(`span`,{className:`reason-note`,children:[` `,n.note]})]})]});return(0,$.jsx)(`li`,{"data-group":n.key,style:{"--share":`${Math.round(r/Math.max(i,1)*100)}%`},children:c?(0,$.jsx)(`span`,{className:`reason`,children:a}):(0,$.jsx)(`a`,{className:`reason`,href:`#/library/datasets`,title:`${n.label}${n.note?` (${n.note})`:``}: open ${Wr(r,`case`)} in the Library`,"aria-label":`${n.label}: open ${Wr(r,`case`)} in the Library`,onClick:r=>{r.preventDefault(),o(t,n.files,`${e}${l?`, ${s}`:``}, ${n.label}`)},children:a})},n.key)})})},r.id)})]})}function _f({id:e,summary:t,scenarios:n,onScenario:r}){let i=[...t.rows.map(e=>({kind:`group`,group:e})),...t.overall?[{kind:`overall`,...t.overall}]:[]],a=(e,t)=>e==null&&t==null?(0,$.jsx)(`span`,{className:`eg-none`,children:`—`}):(0,$.jsxs)(`span`,{className:`eg-cell`,children:[e!=null&&(0,$.jsx)(Hr,{pass:e}),t!=null&&(0,$.jsxs)(`span`,{className:`eg-pct`,children:[t,`%`]})]}),o=t.rows.some(e=>e.version!=null);return(0,$.jsx)(X,{id:e,title:`Evals`,children:(0,$.jsx)(`div`,{className:`scrollx`,style:{"--scenarios":n.length},children:(0,$.jsx)(q,{className:o?`egtable`:`egtable unversioned`,label:`Evals`,rows:i,rowKey:e=>e.kind===`group`?e.group.key:`overall`,defaultSort:null,rowProps:e=>e.kind===`overall`?{className:`eg-overall`,"data-group":`overall`}:{"data-group":e.group.key},columns:[{key:`group`,header:`Eval group`,label:``,role:`title`,sort:null,className:`col-eg`,cell:e=>e.kind===`group`?(0,$.jsx)(`span`,{className:`eg-name`,children:e.group.name}):(0,$.jsxs)(`span`,{className:`eg-overall-name`,children:[(0,$.jsx)(`span`,{className:`eg-name`,children:`Overall`}),` `,(0,$.jsx)(`span`,{className:`eg-rule`,children:e.label})]})},...o?[{key:`version`,header:`Version`,label:`Version`,sort:null,className:`col-egver`,cell:e=>e.kind===`group`?e.group.version??``:``}]:[],...n.map((e,t)=>({key:`scenario-${t}`,header:e.name,label:e.name,onHeader:()=>r(t),cell:e=>{if(e.kind===`overall`)return a(e.pass[t]??null);let n=e.group.cells[t];return n?a(n.pass,n.pct):null}}))]})})})}function vf({item:e,i:t,readings:n}){let r=e?.scenarios?.[t]?.res;if(e?.unrun)return(0,$.jsx)(`span`,{className:`err`,children:e.unrun});if(!r)return(0,$.jsx)(`span`,{className:`spin`,"aria-hidden":`true`});let i=n.some(e=>e.score&&!J().isSkipped(e.score)&&e.score.metrics?.length),a=n.length===1&&J().isSkipped(n[0].score),o=(0,$.jsxs)($.Fragment,{children:[(n.length>1||!i&&n.some(e=>e.score))&&(0,$.jsx)(xf,{readings:n}),a&&n.length===1&&(0,$.jsx)(`span`,{className:`verdict skipped`,children:`Skipped`}),(0,$.jsx)(yf,{readings:n})]});return(0,$.jsxs)($.Fragment,{children:[o,e?.kind===`text`?(0,$.jsx)(pf,{res:r}):(0,$.jsx)(ff,{res:r})]})}function yf({readings:e}){let t=e.flatMap(e=>e.score&&!J().isSkipped(e.score)&&e.score.metrics?.length?[{...e,score:e.score}]:[]);return t.length?(0,$.jsx)($.Fragment,{children:t.map((e,t)=>{let n=e.score.metrics.filter(e=>e.weight!==0),r=e.score.metrics.filter(e=>e.weight===0);return(0,$.jsxs)(`div`,{className:`metric-readings`,children:[(0,$.jsx)(`span`,{className:`verdict ${e.score.pass?`pass`:`fail`}`,children:e.score.pass?`Pass`:`Fail`}),e.score.score!=null&&(0,$.jsx)(`span`,{className:`metric-score`,children:bf(e.score.score,!!e.score.points)}),n.length>0&&(0,$.jsx)(`ul`,{children:n.map((e,t)=>(0,$.jsxs)(`li`,{className:e.pass?`pass`:`fail`,children:[(0,$.jsx)(`span`,{className:`eval-name`,children:e.label}),!e.pass&&(0,$.jsxs)($.Fragment,{children:[` `,(0,$.jsx)(`span`,{className:`metric-reason`,children:e.reason})]})]},t))}),r.length>0&&(0,$.jsxs)(`div`,{className:`watched`,children:[(0,$.jsx)(`b`,{children:`Watch`}),(0,$.jsx)(`ul`,{children:r.map((e,t)=>(0,$.jsxs)(`li`,{className:e.pass?`met`:`unmet`,children:[(0,$.jsx)(`span`,{className:`eval-name`,children:e.label}),` `,(0,$.jsx)(`span`,{className:`metric-reason`,children:e.reason})]},t))})]})]},t)})}):null}var bf=(e,t)=>t?`${Math.round(e*100)/100} points`:`${Math.round(e*100)}%`;function xf({readings:e}){return(0,$.jsx)(`ul`,{className:`eval-verdicts`,children:e.map((e,t)=>{let n=e.score?J().isSkipped(e.score)?`Skipped`:e.score.pass?`Pass`:`Fail`:null;return n&&(0,$.jsxs)(`li`,{children:[(0,$.jsx)(`span`,{className:`eval-name`,children:e.label}),(0,$.jsx)(`span`,{className:`verdict ${n.toLowerCase()}`,children:n})]},t)})})}function Sf({src:e,name:t,onClose:n}){return(0,$.jsx)(Tt,{title:t||`Image`,onClose:n,children:(0,$.jsx)(`img`,{className:`lightbox-img`,src:e,alt:t})})}function Cf(){let{tabs:e,focusedId:t,closeRun:n,focusRun:r,goBack:i}=Dr(),a=ye(),o=e.find(e=>e.id===t);return o?(0,$.jsxs)(`div`,{className:`results`,role:`region`,"aria-label":`Open runs`,children:[(0,$.jsxs)(`div`,{className:`results-bar`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost icon-btn rrback`,onClick:i,"aria-label":`Back`,children:(0,$.jsx)(vt,{size:18,"aria-hidden":`true`})}),(0,$.jsx)(`code`,{className:`rrcode`,children:Ur(o.at)||`…`}),(0,$.jsx)(`div`,{className:`rtabs`,role:`tablist`,"aria-label":`Open runs`,children:e.map(e=>(0,$.jsxs)(`div`,{className:`rtab${e.id===t?` on`:``}`,children:[(0,$.jsx)(`button`,{type:`button`,role:`tab`,"aria-selected":e.id===t,className:`rname`,title:Ur(e.at)||`run`,onClick:()=>r(e.id),children:Ur(e.at)||`…`}),(0,$.jsx)(`button`,{type:`button`,className:`rx`,"aria-label":`Close`,onClick:()=>n(e.id),children:(0,$.jsx)(k,{size:14,"aria-hidden":`true`})})]},e.id))})]}),(0,$.jsx)(mf,{tab:o,undo:a}),(0,$.jsx)(ut,{undo:a.undo,onUndo:a.undoNow,onDismiss:a.dismiss})]}):null}function wf(){let e=Kn(),t=(0,Z.useSyncExternalStore)(e.subscribe,e.held),n=t.filter(e=>e.sync.state===`conflict`),r=t.filter(e=>e.sync.state===`offline`);return!n.length&&!r.length?null:(0,$.jsxs)($.Fragment,{children:[n.map(t=>(0,$.jsxs)(`div`,{className:`sync bad`,role:`alert`,children:[(0,$.jsxs)(`span`,{children:[t.doc?.name??t.id,` changed in another browser.`]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>void e.resolve(t.id,`theirs`),children:`Use theirs`}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>void e.resolve(t.id,`mine`),children:`Keep mine`})]},t.id)),r.map(t=>(0,$.jsxs)(`div`,{className:`sync bad`,role:`status`,children:[(0,$.jsxs)(`span`,{children:[t.doc?.name??t.id,` not saved: `,t.sync.state===`offline`?t.sync.message.replace(/\.$/,``):``,`.`]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>void e.flush(t.id),children:`Retry`})]},t.id))]})}function Tf(){let e=to(),t=(0,Z.useSyncExternalStore)(e.subscribe,e.held),n=t.filter(e=>e.sync.state===`conflict`),r=t.filter(e=>e.sync.state===`offline`);return!n.length&&!r.length?null:(0,$.jsxs)($.Fragment,{children:[n.map(t=>(0,$.jsxs)(`div`,{className:`sync bad`,role:`alert`,children:[(0,$.jsxs)(`span`,{children:[t.doc?.name??t.id,` changed in another browser.`]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>void e.resolve(t.id,`theirs`),children:`Use theirs`}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>void e.resolve(t.id,`mine`),children:`Keep mine`})]},t.id)),r.map(t=>(0,$.jsxs)(`div`,{className:`sync bad`,role:`status`,children:[(0,$.jsxs)(`span`,{children:[t.doc?.name??t.id,` not saved: `,t.sync.state===`offline`?t.sync.message.replace(/\.$/,``):``,`.`]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>void e.flush(t.id),children:`Retry`})]},t.id))]})}var Ef=`promptlab.theme`,Df=typeof matchMedia==`function`?matchMedia(`(prefers-color-scheme: light)`):null;function Of(){try{let e=JSON.parse(localStorage.getItem(Ef)||`null`)?.choice;return e===`light`||e===`dark`?e:null}catch{return null}}var kf=()=>Of()??`system`,Af=()=>Of()??(Df?.matches?`light`:`dark`),jf=()=>{let e=Af();return document.documentElement.setAttribute(`data-theme`,e),e};jf();function Mf(e){try{e===`system`?localStorage.removeItem(Ef):localStorage.setItem(Ef,JSON.stringify({choice:e}))}catch{}jf()}function Nf(){let[e,t]=(0,Z.useState)(kf);return(0,Z.useEffect)(()=>{let e=()=>{jf(),t(kf())};return Df?.addEventListener?.(`change`,e),()=>Df?.removeEventListener?.(`change`,e)},[]),[e,e=>{Mf(e),t(e)}]}var Pf=()=>{let e=decodeURIComponent(location.hash.replace(/^#\/?/,``)),t=e.match(/^(evals|datasets|sources)(\/.*)?$/);t&&(e=`library/${t[1]===`sources`?`sources`:`datasets`}${t[2]??``}`,history.replaceState(null,``,`#/${e.split(`/`).map(encodeURIComponent).join(`/`)}`));let n=e.match(/^library(?:\/(datasets|filters|prompts|sources))?(?:\/(.+))?$/);if(n){let e=n[1]??`datasets`,t=n[2]??null;return{tab:`library`,section:e,setupSection:`connections`,src:e===`sources`?t:null,groupId:e===`datasets`?t:null,setId:e===`filters`?t:null}}let r=e.match(/^setup(?:\/(connections|wizards|workspaces))?$/);return r?{tab:`setup`,section:`datasets`,setupSection:r[1]??`connections`,src:null,groupId:null,setId:null}:{tab:ta.find(t=>t.id===e)?.id??`home`,section:`datasets`,setupSection:`connections`,src:null,groupId:null,setId:null}};function Ff(){return(0,$.jsx)(Be,{children:(0,$.jsx)(Gn,{children:(0,$.jsx)(eo,{children:(0,$.jsx)(tr,{children:(0,$.jsx)(Er,{children:(0,$.jsx)(Cr,{children:(0,$.jsx)(ja,{children:(0,$.jsx)(Zu,{children:(0,$.jsx)(If,{})})})})})})})})})}function If(){let[e,t]=(0,Z.useState)(Pf),{hide:n}=Dr(),r=_u(),i=Q(),a=ye();if((0,Z.useEffect)(()=>{let e=()=>{t(Pf()),n()};return window.addEventListener(`hashchange`,e),()=>window.removeEventListener(`hashchange`,e)},[n]),(0,Z.useEffect)(()=>{document.title=r.current?`${r.current.name}, Evals Lab`:`No workspace, Evals Lab`},[r.current]),(0,Z.useEffect)(()=>{let e=mu();e&&a.offer({label:`Archived ${e.name}`,restore:()=>i.unarchiveWorkspace(e.id).then(()=>{location.assign(du(e.slug))})})},[]),!r.current)return(0,$.jsx)(Bf,{ws:r});let o=ta.find(t=>t.id===e.tab),s=r.current.archived;return(0,$.jsxs)(`div`,{className:`app`,children:[(0,$.jsxs)(`header`,{className:`app-head`,children:[(0,$.jsx)(`h1`,{children:`Evals Lab`}),(0,$.jsx)(Hf,{}),(0,$.jsx)(wf,{}),(0,$.jsx)(Tf,{}),(0,$.jsx)(Lf,{}),(0,$.jsx)(gt,{}),(0,$.jsx)(Rf,{ws:r})]}),(0,$.jsx)($e,{}),(0,$.jsx)(ut,{undo:a.undo,onUndo:a.undoNow,onDismiss:a.dismiss}),s&&(0,$.jsx)(zf,{id:r.current.id}),(0,$.jsx)(m,{label:`Lab`,className:`app-tabs`,tablist:!0,current:e.tab,items:ta.map(e=>({key:e.id,label:e.label,href:`#/${e.id}`,icon:e.icon,id:`tab-${e.id}`,controls:`panel-${e.id}`}))}),(0,$.jsx)(Vf,{tab:o,section:e.section,setupSection:e.setupSection,src:e.src,groupId:e.groupId,setId:e.setId,readOnly:s})]})}function Lf(){let{cancel:e}=Qu(),t=$u();return t?(0,$.jsx)(d,{name:t.entry.title,done:t.done,total:t.total,current:t.current,steps:t.entry.steps.map((e,n)=>({title:e.title,done:t.ticks[n]})),onOpen:()=>{location.hash=`#/setup/wizards`},onCancel:e}):null}function Rf({ws:e}){let t=Q(),[n,r]=Nf(),[i,a]=(0,Z.useState)(!1),o=e.list.filter(e=>!e.archived).map(e=>({id:e.id,slug:e.slug,name:e.name})),s=async e=>{try{let n=await t.createWorkspace(e);n.workspace&&location.assign(du(n.workspace.slug))}catch{}};return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(Ne,{current:e.current?{id:e.current.id,slug:e.current.slug,name:e.current.name}:null,workspaces:o,onSwitch:e.switchTo,onNew:()=>a(!0),onManage:()=>{location.hash=`#/setup/workspaces`},theme:n,onTheme:r}),i&&(0,$.jsx)(cd,{onCreate:e=>void s(e),onClose:()=>a(!1)})]})}function zf({id:e}){let t=Q(),[n,r]=(0,Z.useState)(!1),i=async()=>{r(!0);try{await t.unarchiveWorkspace(e),location.reload()}catch{r(!1)}};return(0,$.jsxs)(`div`,{className:`ws-archived-bar`,role:`status`,children:[(0,$.jsx)(`span`,{children:`This workspace is archived — read-only until you unarchive it.`}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,disabled:n,onClick:()=>void i(),children:[(0,$.jsx)(Xt,{size:14,"aria-hidden":`true`}),`Unarchive`]})]})}function Bf({ws:e}){let t=hu(e.list);return(0,$.jsxs)(`div`,{className:`app`,children:[(0,$.jsxs)(`header`,{className:`app-head`,children:[(0,$.jsx)(`h1`,{children:`Evals Lab`}),(0,$.jsx)(Rf,{ws:e})]}),(0,$.jsxs)(`main`,{className:`ws-gone`,children:[(0,$.jsxs)(`p`,{role:`alert`,children:[`No workspace at `,(0,$.jsxs)(`code`,{children:[`/w/`,e.requestedSlug]}),`.`]}),t&&(0,$.jsxs)(`button`,{type:`button`,onClick:()=>e.switchTo(t.slug),children:[`Open `,t.name]})]})]})}function Vf({tab:e,section:t,setupSection:n,src:r,groupId:i,setId:a,readOnly:o}){let{focusedId:s}=Dr(),c=s?(0,$.jsx)(Cf,{}):e.id===`home`?(0,$.jsx)(Qc,{}):e.id===`runs`?(0,$.jsx)(Hl,{}):e.id===`history`?(0,$.jsx)(tu,{}):e.id===`library`?(0,$.jsx)(Gc,{section:t,sourceId:r,groupId:i,setId:a}):(0,$.jsx)(hd,{section:n}),l=o&&e.id!==`setup`;return(0,$.jsx)(`main`,{role:`tabpanel`,id:`panel-${e.id}`,"aria-labelledby":`tab-${e.id}`,"data-tab":e.id,inert:l,children:c})}function Hf(){let{sync:e,resolve:t}=kn();return e.state===`offline`?(0,$.jsxs)(`p`,{className:`sync bad`,role:`status`,children:[`Not saved to the server: `,e.message,`. Kept in this browser.`]}):e.state===`conflict`?(0,$.jsxs)(`div`,{className:`sync bad`,role:`alert`,children:[(0,$.jsx)(`span`,{children:`Changed on another device.`}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>void t(`theirs`),children:`Use theirs`}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>void t(`mine`),children:`Keep mine`})]}):null}var Uf=`promptlab.sync`,Wf=new Set([`promptlab.workflows`,`promptlab.versions`,Uf]),Gf=e=>typeof e==`object`&&!!e&&!Array.isArray(e),Kf=(e,t)=>{let n=cu[e];return Gf(n)&&Gf(t)?{...n,...t}:t};function qf({api:e,storage:t,server:n,workspace:r,pause:i=800}){let a=e=>r&&Wf.has(e)?`${e}@${r}`:e,o=e=>{try{return t.getItem(a(e))}catch{return null}},s=new Map,c=(e,n)=>{s.delete(e);try{n==null?t.removeItem(a(e)):t.setItem(a(e),JSON.stringify(n))}catch{}s.set(e,n??null)},l=e=>{try{return JSON.parse(o(e)??`null`)}catch{return null}},u=(e,t)=>o(e)===(t==null?null:JSON.stringify(t)),d=n!==null,f=l(Uf)??{},p={},m=new Set,h=()=>c(Uf,{versions:p,dirty:[...m]}),g=new Set,_=()=>g.forEach(e=>e()),v=d?{state:`saved`}:{state:`local`},y=null,b=null,x=e=>{v=e,_()};if(n){for(let e of uu){let t=n[e];t&&u(e,t.body)?p[e]=t.version:f.dirty?.includes(e)?(p[e]=f.versions?.[e]??0,m.add(e)):t?(p[e]=t.version,c(e,t.body)):(p[e]=0,o(e)!=null&&m.add(e))}h()}async function S(t=!1){if(!d||v.state===`conflict`)return;if(!m.size){v.state===`saving`&&x({state:`saved`});return}let n=[...m],r=Object.fromEntries(n.map(e=>[e,{version:p[e]??0,body:l(e)}]));x({state:`saving`});try{let{versions:i}=await e.writeState(r,t);Object.assign(p,i);for(let e of n)u(e,r[e].body)&&m.delete(e);if(h(),x({state:`saved`}),m.size)return S(t)}catch(e){if(e instanceof Gt&&e.status===409){let t=e.body?.stale??{};for(let[e,n]of Object.entries(t))p[e]=n.version;let n=Object.keys(t).filter(e=>!u(e,t[e].body));for(let e of Object.keys(t))n.includes(e)||m.delete(e);return h(),n.length?(C=t,x({state:`conflict`,keys:n})):S()}x({state:`offline`,message:e instanceof Error?e.message:String(e)})}}let C={};function w(e=!1){return y&&=(clearTimeout(y),null),b??=S(e).finally(()=>{b=null}),b}return m.size&&(y=setTimeout(()=>{w()},0)),{get(e){return s.has(e)||s.set(e,Kf(e,l(e))),s.get(e)??cu[e]},set(e,t){c(e,t),d&&uu.includes(e)&&(m.add(e),h(),y&&clearTimeout(y),y=setTimeout(()=>{w()},i)),_()},subscribe(e){return g.add(e),()=>{g.delete(e)}},sync:()=>v,async resolve(e){if(v.state!==`conflict`)return;let t=v.keys;if(e===`theirs`){for(let e of t)c(e,C[e]?.body??null),m.delete(e);h()}C={},x({state:`saved`}),await w()},flush:w,async refresh(){if(!d)return;await w();let{docs:t}=await e.state(),n=!1;for(let e of uu){let r=t[e];!r||m.has(e)||r.version<=(p[e]??0)||(p[e]=r.version,c(e,r.body),n=!0)}n&&(h(),_())}}}var Jf=e=>[1,2,3,4,5,6,7,8,9,10].includes(e?.version)||J().fatProfileRef(e),Yf=e=>e?.version===1||e?.version===2;function Xf(e){let t=[...e.get(`promptlab.workflows`).list.map(e=>e.work),...Object.values(e.get(`promptlab.versions`).pipeline??{}).flat().map(e=>e.doc)].filter(Yf).map(e=>e?.tests?.type===`graded`?e.tests.dataset?.id:void 0);return[...new Set(t.filter(e=>!!e))]}function Zf(e,t={}){let n=new Map(e.get(`promptlab.profiles`).list.map(e=>[e.id,e.type])),r={rulesFor:e=>t[e]??null,profileType:e=>n.get(e)},i=e=>J().upgradePipeline(e,r),a=e=>Jf(e)||i(e)!==e,o=e.get(`promptlab.workflows`);o.list.some(e=>a(e.work))&&e.set(`promptlab.workflows`,{...o,list:o.list.map(e=>({...e,work:i(e.work)}))});let s=e.get(`promptlab.versions`),c=s.pipeline??{};Object.values(c).some(e=>e.some(e=>a(e.doc)))&&e.set(`promptlab.versions`,{...s,pipeline:Object.fromEntries(Object.entries(c).map(([e,t])=>[e,t.map(e=>({...e,doc:i(e.doc)}))]))})}async function Qf(e){let t=document.getElementById(`labstate`)?.textContent;if(t)try{return JSON.parse(t).docs??null}catch{return null}try{return(await e.state()).docs}catch{return null}}function $f(){let e=location.pathname.match(/^\/w\/([^/]+)/);return e?decodeURIComponent(e[1]):null}async function ep(){J();let e=$f(),t=Yt(void 0,e?`/w/${encodeURIComponent(e)}`:``),n=()=>{},r=new Promise(e=>{n=e});if(/^#\/?history\/?$/.test(location.hash)&&t.askQueue({limit:25},r),Ds())try{let e=(await t.config()).microsoft;e&&await Os(e)}catch{}if(Ms())try{let e=(await t.config()).microsoft;if(e){await Ns(e);return}}catch{}await Wt(t),n();let i=await t.workspaces().then(e=>e.workspaces,()=>[]),a=i.find(e=>e.isDefault)?.slug??`default`,o=e?i.find(t=>t.slug===e):i.find(e=>e.isDefault),s=o?{id:o.id,slug:o.slug,name:o.name,archived:o.archived,isDefault:o.isDefault}:null,c={current:s,requestedSlug:e,list:i,switchTo:e=>{location.assign(du(e))}};document.title=s?`${s.name}, Evals Lab`:`No workspace, Evals Lab`;let l=qf({api:t,storage:localStorage,server:await Qf(t),workspace:s?.slug??a}),u=l.get(`promptlab.profiles`);u.list.some(e=>`profile`in e)&&l.set(`promptlab.profiles`,{...u,list:u.list.map(e=>J().convertProfile(e))});let d=l.get(`promptlab.profiles`),f=e=>J().pinReplyTokens(J().splitOllama(e));d.list.some(e=>f(e)!==e)&&l.set(`promptlab.profiles`,{...d,list:d.list.map(e=>f(e))});let p=l.get(`promptlab.profiles`),m=J().withSlugs(p.list);m!==p.list&&l.set(`promptlab.profiles`,{...p,list:m});let h={};if(Xf(l).length)try{h=(await t.archivedRules()).rules}catch{}Zf(l,h),addEventListener(`pagehide`,()=>{l.flush(!0)}),(0,Pt.createRoot)(document.getElementById(`root`)).render((0,$.jsx)(Z.StrictMode,{children:(0,$.jsx)(En.Provider,{value:l,children:(0,$.jsx)(Ft.Provider,{value:t,children:(0,$.jsx)(gu.Provider,{value:c,children:(0,$.jsx)(Ff,{})})})})}))}ep();