evals-lab 0.16.0 → 0.17.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.
- package/CHANGELOG.md +29 -0
- package/README.md +12 -11
- package/bin/evals-lab.js +5 -1
- package/bin/update.js +65 -0
- package/lab/VERSION +1 -1
- package/lab/demo/CREDITS.md +4 -3
- package/lab/demo/datasets/recorded-replies.json +39 -0
- package/lab/demo/manifest.json +2 -2
- package/lab/demo/pipelines/recorded-replies.json +89 -0
- package/lab/demo/sources/Demo 1/basketball-hoop.jpg +0 -0
- package/lab/demo/sources/Demo 1/blue-boardwalk.jpg +0 -0
- package/lab/demo/sources/Demo 1/busy-beach.jpg +0 -0
- package/lab/demo/sources/Demo 1/butcher-sign.jpg +0 -0
- package/lab/demo/sources/Demo 1/cactus-flower.jpg +0 -0
- package/lab/demo/sources/Demo 1/corner-shop.jpg +0 -0
- package/lab/demo/sources/Demo 1/crosswalk-cyclist-panorama.jpg +0 -0
- package/lab/demo/sources/Demo 1/cyanotype-room.jpg +0 -0
- package/lab/demo/sources/Demo 1/dead-end-sign.jpg +0 -0
- package/lab/demo/sources/Demo 1/dog-in-snow.jpg +0 -0
- package/lab/demo/sources/Demo 1/empty-bedroom.jpg +0 -0
- package/lab/demo/sources/Demo 1/farmers-market-stall.jpg +0 -0
- package/lab/demo/sources/Demo 1/four-jets.jpg +0 -0
- package/lab/demo/sources/Demo 1/german-shepherd.jpg +0 -0
- package/lab/demo/sources/Demo 1/harbour-bridge-dusk.jpg +0 -0
- package/lab/demo/sources/Demo 1/harrow-on-the-hill-sign.jpg +0 -0
- package/lab/demo/sources/Demo 1/hong-kong-street.jpg +0 -0
- package/lab/demo/sources/Demo 1/house-salisbury-street.jpg +0 -0
- package/lab/demo/sources/Demo 1/infrared-orchard.jpg +0 -0
- package/lab/demo/sources/Demo 1/keyboard-desk.jpg +0 -0
- package/lab/demo/sources/Demo 1/lighthouse-dunes.jpg +0 -0
- package/lab/demo/sources/Demo 1/mountain-lake.jpg +0 -0
- package/lab/demo/sources/Demo 1/museum-skeleton.jpg +0 -0
- package/lab/demo/sources/Demo 1/music-room.jpg +0 -0
- package/lab/demo/sources/Demo 1/old-red-car.jpg +0 -0
- package/lab/demo/sources/Demo 1/parked-car-plate.jpg +0 -0
- package/lab/demo/sources/Demo 1/phone-and-wallet.png +0 -0
- package/lab/demo/sources/Demo 1/phone-box.jpg +0 -0
- package/lab/demo/sources/Demo 1/pink-flamingo.jpg +0 -0
- package/lab/demo/sources/Demo 1/postcards.jpg +0 -0
- package/lab/demo/sources/Demo 1/red-roof-church.jpg +0 -0
- package/lab/demo/sources/Demo 1/roadside-mailboxes.jpg +0 -0
- package/lab/demo/sources/Demo 1/rodeo.jpg +0 -0
- package/lab/demo/sources/Demo 1/running-tap.tif +0 -0
- package/lab/demo/sources/Demo 1/snail-on-stem.jpg +0 -0
- package/lab/demo/sources/Demo 1/two-horses-field.jpg +0 -0
- package/lab/demo/sources/Demo 1/university-sign.jpg +0 -0
- package/lab/demo/sources/Demo 1/vegetable-crates.jpg +0 -0
- package/lab/demo/sources/Demo 1/watermarked-car.jpg +0 -0
- package/lab/demo/sources/Demo 1/watermarked-pills.jpg +0 -0
- package/lab/demo/sources/Demo 1/whiteboard-delegate.jpg +0 -0
- package/lab/demo/sources/Demo 1/whiteboard-germ-layers.jpg +0 -0
- package/lab/demo/sources/Demo 2/aerial-city.jpg +0 -0
- package/lab/demo/sources/Demo 2/alligator-pen.jpg +0 -0
- package/lab/demo/sources/Demo 2/arm-tattoo.jpg +0 -0
- package/lab/demo/sources/Demo 2/bed-and-plant.jpg +0 -0
- package/lab/demo/sources/Demo 2/bright-bedroom.jpg +0 -0
- package/lab/demo/sources/Demo 2/car-headlight.jpg +0 -0
- package/lab/demo/sources/Demo 2/child-in-surf.jpg +0 -0
- package/lab/demo/sources/Demo 2/city-highway.jpg +0 -0
- package/lab/demo/sources/Demo 2/corner-bakery.jpg +0 -0
- package/lab/demo/sources/Demo 2/cyclist-yellow-jacket.jpg +0 -0
- package/lab/demo/sources/Demo 2/excavator-street-signs.jpg +0 -0
- package/lab/demo/sources/Demo 2/globe-closeup.jpg +0 -0
- package/lab/demo/sources/Demo 2/harbour-village.jpg +0 -0
- package/lab/demo/sources/Demo 2/high-street-walkers.jpg +0 -0
- package/lab/demo/sources/Demo 2/hillside-rooftops.jpg +0 -0
- package/lab/demo/sources/Demo 2/house-at-night.jpg +0 -0
- package/lab/demo/sources/Demo 2/ivy-leaves.jpg +0 -0
- package/lab/demo/sources/Demo 2/man-with-alligator.jpg +0 -0
- package/lab/demo/sources/Demo 2/orange-mailbox-hedge.jpg +0 -0
- package/lab/demo/sources/Demo 2/pedal-boats.jpg +0 -0
- package/lab/demo/sources/Demo 2/pink-trees-vignette.jpg +0 -0
- package/lab/demo/sources/Demo 2/please-leave-quietly-sign.jpg +0 -0
- package/lab/demo/sources/Demo 2/railroad-crossing-sign.jpg +0 -0
- package/lab/demo/sources/Demo 2/roadworks-sign.jpg +0 -0
- package/lab/demo/sources/Demo 2/roundabout-sign.jpg +0 -0
- package/lab/demo/sources/Demo 2/sea-cave.jpg +0 -0
- package/lab/demo/sources/Demo 2/shadow-on-sand.jpg +0 -0
- package/lab/demo/sources/Demo 2/signpost-a404.jpg +0 -0
- package/lab/demo/sources/Demo 2/street-fruit-cart.jpg +0 -0
- package/lab/demo/sources/Demo 2/street-sign-parnassusweg.jpg +0 -0
- package/lab/demo/sources/Demo 2/two-horses-close.jpg +0 -0
- package/lab/demo/sources/Demo 2/victorian-house.jpg +0 -0
- package/lab/demo/sources/Demo 2/vintage-dashboard.jpg +0 -0
- package/lab/demo/sources/Demo 2/volcano-at-dusk.jpg +0 -0
- package/lab/demo/sources/Demo 2/wall-camera.jpg +0 -0
- package/lab/demo/sources/Demo 2/watch-for-rocks-sign.jpg +0 -0
- package/lab/demo/sources/Demo 2/waterfall.jpg +0 -0
- package/lab/demo/sources/Demo 2/white-domes.jpg +0 -0
- package/lab/demo/sources/Demo 2/whiteboard-meeting-notes.jpg +0 -0
- package/lab/demo/sources/Demo 2/whiteboard-messages.jpg +0 -0
- package/lab/demo/sources/Demo 2/wooden-house-fence.jpg +0 -0
- package/lab/kinds/list.mjs +11 -3
- package/lab/server.py +19 -1
- package/lab/web/dist/assets/dist-BfJnsQb9.js +1 -0
- package/lab/web/dist/assets/gallery-Dv7iXLfa.js +5 -0
- package/lab/web/dist/assets/main-DS76udej.css +1 -0
- package/lab/web/dist/assets/main-DaObzKJz.js +18 -0
- package/lab/web/dist/assets/tokens-z9rSXnjM.js +64 -0
- package/lab/web/dist/gallery.html +2 -2
- package/lab/web/dist/index.html +3 -3
- package/package.json +3 -3
- package/lab/web/dist/assets/dist-DI3ewZYj.js +0 -1
- package/lab/web/dist/assets/gallery-ICJNZQIZ.js +0 -5
- package/lab/web/dist/assets/main-BOkQWswU.css +0 -1
- package/lab/web/dist/assets/main-rJDsPFWT.js +0 -18
- package/lab/web/dist/assets/tokens-C5ist-xG.js +0 -64
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import{$ as e,$n as t,$t as n,A as r,An as i,At as a,B as o,Bn as s,Bt as c,C as l,Cn as u,Cr as d,Ct as f,D as p,Dn as m,Dt as h,E as g,En as _,Et as v,F as y,Fn as b,Ft as x,G as S,Gn as C,Gt as w,H as T,Hn as E,Ht as D,I as O,In as k,It as A,J as j,Jn as M,Jt as N,K as P,Kn as F,Kt as I,L as ee,Ln as L,Lt as te,M as R,Mn as ne,Mt as re,N as z,Nn as B,Nt as V,O as H,On as ie,Ot as U,P as ae,Pn as oe,Pt as W,Q as se,Qn as ce,Qt as le,R as G,Rn as ue,Rt as de,S as fe,Sn as pe,Sr as me,T as he,Tn as ge,Tr as _e,Tt as ve,U as ye,Un as be,Ut as xe,V as Se,Vn as Ce,Vt as we,W as Te,Wn as Ee,Wt as De,X as Oe,Xn as ke,Xt as Ae,Y as je,Yn as Me,Yt as Ne,Z as Pe,Zn as Fe,Zt as Ie,_ as Le,_n as Re,_r as ze,_t as Be,a as Ve,an as He,ar as K,at as Ue,b as We,bn as Ge,br as Ke,bt as qe,c as Je,cn as Ye,cr as Xe,ct as Ze,d as Qe,dn as $e,dt as et,en as tt,er as nt,et as rt,f as it,fn as at,fr as ot,ft as st,g as ct,gn as lt,gr as ut,gt as dt,h as ft,hn as pt,hr as mt,ht,i as gt,in as _t,ir as q,it as vt,j as yt,jn as bt,jt as xt,k as St,kn as Ct,kt as wt,ln as Tt,lr as Et,lt as Dt,m as Ot,mn as kt,mr as At,mt as jt,nn as Mt,nr as Nt,nt as Pt,o as Ft,on as It,or as Lt,ot as Rt,p as zt,pn as Bt,pr as Vt,pt as Ht,q as Ut,qn as Wt,r as Gt,rn as Kt,rr as qt,rt as Jt,s as Yt,sn as Xt,sr as Zt,st as Qt,t as $t,tn as en,tr as tn,tt as nn,u as rn,un as an,ur as on,ut as sn,v as cn,vn as ln,vr as un,vt as dn,w as fn,wn as pn,wr as mn,wt as hn,x as gn,xn as _n,xr as vn,xt as yn,y as bn,yn as J,yr as xn,yt as Sn,z as Cn,zn as wn,zt as Tn}from"./tokens-C5ist-xG.js";var En=mn(),Y=_e(),Dn=(0,Y.createContext)(null);function X(){let e=(0,Y.useContext)(Dn);if(!e)throw Error(`useApi outside an ApiContext: main.tsx mounts one at the root`);return e}var On=`modulepreload`,kn=function(e){return`/`+e},An={},jn=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=kn(t,n),t=s(t),t in An)return;An[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`:On,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)})},Mn={},Nn=new Set,Pn=e=>`/plugins/${encodeURIComponent(e.id)}/${encodeURIComponent(e.version)}/${e.entry.split(`/`).map(encodeURIComponent).join(`/`)}`;async function Fn(e){if(!Nn.has(e.id))try{let t=await jn(()=>import(Pn(e)),[]);if(typeof t.default!=`function`)throw Error(`its entry exports no register function`);await t.default(K().pluginHost(e.id)),Nn.add(e.id),delete Mn[e.id]}catch(t){Mn[e.id]=t instanceof Error?t.message:String(t)}}async function In(e){let t=[];try{t=(await e.plugins()).plugins}catch{return}for(let e of t)await Fn(e)}var Ln=class extends Error{status;body;constructor(e,t,n=null){super(t),this.name=`ApiError`,this.status=e,this.body=n}},Rn=(e,t)=>`${e}:${t}`,zn=(e,t,n,r)=>{let i=e[Rn(t,n)]??[];return i.length===0||i.includes(`*all`)||i.includes(r)},Bn=new EventTarget,Vn=e=>/^\/api\/(sources|queue)(\/|\?|$)/.test(e);function Hn(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 Ln(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`&&Vn(t)&&Bn.dispatchEvent(new Event(`write`)),a}let a=e=>({...e,snapshot:K().upgradePipeline(e.snapshot),results:K().upgradeResults(e.results)}),o=e=>{let t=new URLSearchParams;return e.pipeline&&t.set(`pipeline`,e.pipeline),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=>K().evalsOf(e.snapshot).some(e=>K().isWholeRun(K().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}}),setDatasetPreview:(e,t)=>i(`/api/datasets/${encodeURIComponent(e)}`,{method:`PATCH`,body:{preview: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`),runGroups:e=>i(`/api/queue/${encodeURIComponent(e)}/groups`),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}}),updateSource:(e,t)=>i(`/api/sources/${encodeURIComponent(e)}`,{method:`PATCH`,body:t}),sourceCalls:e=>i(`/api/sources/${encodeURIComponent(e)}/calls`),snapshotSource:e=>i(`/api/sources/${encodeURIComponent(e)}/snapshot`,{body:{}}),sourceResponses:e=>i(`/api/sources/${encodeURIComponent(e)}/responses`),restoreResponse:(e,t,n,r)=>i(`/api/sources/${encodeURIComponent(e)}/responses/restore`,{body:{name:t,n,...r==null?{}:{drop:r}}}),removeCalls:(e,t)=>i(`/api/sources/${encodeURIComponent(e)}/calls/remove`,{body:{names:t}}),editCall:(e,t,n)=>i(`/api/sources/${encodeURIComponent(e)}/calls/edit`,{body:{name:t,...n}}),cloneCall:(e,t)=>i(`/api/sources/${encodeURIComponent(e)}/calls/clone`,{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)}`),{cache:`no-store`});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:{}}),copyMovePreflight:e=>i(`/api/copy-move/preflight`,{body:e}),copyAsset:e=>i(`/api/copy-move/copy`,{body:e}),moveAsset:e=>i(`/api/copy-move/move`,{body:e}),forWorkspace:t=>Hn(e,`/w/${encodeURIComponent(t)}`),models:e=>i(`/api/models`,{body:e}),modelsAsk:e=>i(`/api/models/ask`,{body:e})}}var Un=q(`a-large-small`,[[`path`,{d:`m15 16 2.536-7.328a1.02 1.02 1 0 1 1.928 0L22 16`,key:`xik6mr`}],[`path`,{d:`M15.697 14h5.606`,key:`1stdlc`}],[`path`,{d:`m2 16 4.039-9.69a.5.5 0 0 1 .923 0L11 16`,key:`d5nyq2`}],[`path`,{d:`M3.304 13h6.392`,key:`1q3zxz`}]]),Wn=q(`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`}]]),Gn=q(`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`}]]),Kn=q(`arrow-left-right`,[[`path`,{d:`M8 3 4 7l4 4`,key:`9rb6wj`}],[`path`,{d:`M4 7h16`,key:`6tx8e3`}],[`path`,{d:`m16 21 4-4-4-4`,key:`siv7j2`}],[`path`,{d:`M20 17H4`,key:`h6l3hr`}]]),qn=q(`ban`,[[`path`,{d:`M4.929 4.929 19.07 19.071`,key:`196cmz`}],[`circle`,{cx:`12`,cy:`12`,r:`10`,key:`1mglay`}]]),Jn=q(`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`}]]),Yn=q(`chart-line`,[[`path`,{d:`M3 3v16a2 2 0 0 0 2 2h16`,key:`c24i48`}],[`path`,{d:`m19 9-5 5-4-4-3 3`,key:`2osh9i`}]]),Xn=q(`circle-check`,[[`circle`,{cx:`12`,cy:`12`,r:`10`,key:`1mglay`}],[`path`,{d:`m9 12 2 2 4-4`,key:`dzmm74`}]]),Zn=q(`cloud-sync`,[[`path`,{d:`m17 18-1.535 1.605a5 5 0 0 1-8-1.5`,key:`adpv5j`}],[`path`,{d:`M17 22v-4h-4`,key:`ex1ofj`}],[`path`,{d:`M20.996 15.251A4.5 4.5 0 0 0 17.495 8h-1.79a7 7 0 1 0-12.709 5.607`,key:`ziqt14`}],[`path`,{d:`M7 10v4h4`,key:`1j6gx1`}],[`path`,{d:`m7 14 1.535-1.605a5 5 0 0 1 8 1.5`,key:`19q5h7`}]]),Qn=q(`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`}]]),$n=q(`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`}]]),er=q(`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`}]]),tr=q(`file-input`,[[`path`,{d:`M4 11V4a2 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 2H6a2 2 0 0 1-2-2v-1`,key:`1q9hii`}],[`path`,{d:`M14 2v5a1 1 0 0 0 1 1h5`,key:`wfsgrz`}],[`path`,{d:`M2 15h10`,key:`jfw4w8`}],[`path`,{d:`m9 18 3-3-3-3`,key:`112psh`}]]),nr=q(`file-text`,[[`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:`M10 9H8`,key:`b1mrlr`}],[`path`,{d:`M16 13H8`,key:`t4e002`}],[`path`,{d:`M16 17H8`,key:`z1uh3a`}]]),rr=q(`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`}]]),ir=q(`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`}]]),ar=q(`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`}]]),or=q(`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`}]]),sr=q(`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`}]]),cr=q(`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`}]]),lr=q(`form`,[[`path`,{d:`M4 14h6`,key:`77gv2w`}],[`path`,{d:`M4 2h10`,key:`a2b314`}],[`rect`,{x:`4`,y:`18`,width:`16`,height:`4`,rx:`1`,key:`sybzq6`}],[`rect`,{x:`4`,y:`6`,width:`16`,height:`4`,rx:`1`,key:`1osc9e`}]]),ur=q(`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`}]]),dr=q(`link-2`,[[`path`,{d:`M9 17H7A5 5 0 0 1 7 7h2`,key:`8i5ue5`}],[`path`,{d:`M15 7h2a5 5 0 1 1 0 10h-2`,key:`1b9ql8`}],[`line`,{x1:`8`,x2:`16`,y1:`12`,y2:`12`,key:`1jonct`}]]),fr=q(`list-tree`,[[`path`,{d:`M8 5h13`,key:`1pao27`}],[`path`,{d:`M13 12h8`,key:`h98zly`}],[`path`,{d:`M13 19h8`,key:`c3s6r1`}],[`path`,{d:`M3 10a2 2 0 0 0 2 2h3`,key:`1npucw`}],[`path`,{d:`M3 5v12a2 2 0 0 0 2 2h3`,key:`x1gjn2`}]]),pr=q(`list`,[[`path`,{d:`M3 5h.01`,key:`18ugdj`}],[`path`,{d:`M3 12h.01`,key:`nlz23k`}],[`path`,{d:`M3 19h.01`,key:`noohij`}],[`path`,{d:`M8 5h13`,key:`1pao27`}],[`path`,{d:`M8 12h13`,key:`1za7za`}],[`path`,{d:`M8 19h13`,key:`m83p4d`}]]),mr=q(`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`}]]),hr=q(`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`}]]),gr=q(`minus`,[[`path`,{d:`M5 12h14`,key:`1ays0h`}]]),_r=q(`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`}]]),vr=q(`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`}]]),yr=q(`printer`,[[`path`,{d:`M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2`,key:`143wyd`}],[`path`,{d:`M6 9V3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v6`,key:`1itne7`}],[`rect`,{x:`6`,y:`14`,width:`12`,height:`8`,rx:`1`,key:`1ue0tg`}]]),br=q(`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`}]]),xr=q(`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`}]]),Sr=q(`rotate-ccw`,[[`path`,{d:`M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8`,key:`1357e3`}],[`path`,{d:`M3 3v5h5`,key:`1xhq8a`}]]),Cr=q(`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`}]]),wr=q(`scan-search`,[[`path`,{d:`M3 7V5a2 2 0 0 1 2-2h2`,key:`aa7l1z`}],[`path`,{d:`M17 3h2a2 2 0 0 1 2 2v2`,key:`4qcy5o`}],[`path`,{d:`M21 17v2a2 2 0 0 1-2 2h-2`,key:`6vwrx8`}],[`path`,{d:`M7 21H5a2 2 0 0 1-2-2v-2`,key:`ioqczr`}],[`circle`,{cx:`12`,cy:`12`,r:`3`,key:`1v7zrd`}],[`path`,{d:`m16 16-1.9-1.9`,key:`1dq9hf`}]]),Tr=q(`scissors`,[[`circle`,{cx:`6`,cy:`6`,r:`3`,key:`1lh9wr`}],[`path`,{d:`M8.12 8.12 12 12`,key:`1alkpv`}],[`path`,{d:`M20 4 8.12 15.88`,key:`xgtan2`}],[`circle`,{cx:`6`,cy:`18`,r:`3`,key:`fqmcym`}],[`path`,{d:`M14.8 14.8 20 20`,key:`ptml3r`}]]),Er=q(`send`,[[`path`,{d:`M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z`,key:`1ffxy3`}],[`path`,{d:`m21.854 2.147-10.94 10.939`,key:`12cjpa`}]]),Dr=q(`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`}]]),Or=q(`square-code`,[[`path`,{d:`m10 9-3 3 3 3`,key:`1oro0q`}],[`path`,{d:`m14 15 3-3-3-3`,key:`bz13h7`}],[`rect`,{x:`3`,y:`3`,width:`18`,height:`18`,rx:`2`,key:`h1oib`}]]),kr=q(`square-terminal`,[[`path`,{d:`m7 11 2-2-2-2`,key:`1lz0vl`}],[`path`,{d:`M11 13h4`,key:`1p7l4v`}],[`rect`,{width:`18`,height:`18`,x:`3`,y:`3`,rx:`2`,ry:`2`,key:`1m3agn`}]]),Ar=q(`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`}]]),jr=q(`text-search`,[[`path`,{d:`M21 5H3`,key:`1fi0y6`}],[`path`,{d:`M10 12H3`,key:`1ulcyk`}],[`path`,{d:`M10 19H3`,key:`108z41`}],[`circle`,{cx:`17`,cy:`15`,r:`3`,key:`1upz2a`}],[`path`,{d:`m21 19-1.9-1.9`,key:`dwi7p8`}]]),Mr=q(`trash`,[[`path`,{d:`M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6`,key:`miytrc`}],[`path`,{d:`M3 6h18`,key:`d0wm0j`}],[`path`,{d:`M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2`,key:`e791ji`}]]),Nr=q(`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`}]]),Pr=(0,Y.createContext)(null);function Fr(){let e=(0,Y.useContext)(Pr);if(!e)throw Error(`useStore outside a StoreContext: the app mounts one at its root`);return e}function Z(e){let t=Fr();return[(0,Y.useSyncExternalStore)(t.subscribe,()=>t.get(e)),(0,Y.useCallback)(n=>t.set(e,n),[t,e])]}function Ir(){let e=Fr();return{sync:(0,Y.useSyncExternalStore)(e.subscribe,e.sync),resolve:e.resolve}}var Lr=e=>e,Rr=()=>({version:9,scoring:{mode:`all`,threshold:null},grader:null,every:[],run:[],cases:[]});function Q(e,t){return e instanceof Ln?e.message:t===void 0?e instanceof Error?e.message:String(e):t}var zr=e=>e?.state===`conflict`?`Data conflict: not saved — a newer version was saved in another browser.`:e?.state===`offline`?`Not saved: ${e.message}`:null,Br=(e,t)=>JSON.stringify(e)===JSON.stringify(t),Vr=({body:e,...t})=>t;function Hr({api:e,pause:t=800,idle:n}){let r=Lr(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(Vr(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,versions:t.versions,cases:n.cases.length};if(p(e,{doc:c,sync:{state:`saved`}}),h(Vr(c)),o.has(e))return v(e)}catch(t){let n=a.get(e);if(!n?.doc)return;let r=t instanceof Ln&&t.status===409?t.body?.current??null:null;if(r){if(Br(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:Q(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:Q(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:Q(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,n){let i=await r.createDataset({name:e,...t?{body:t}:{},...n?{preview:n}:{}});return _(i),i},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({...Vr(n),cases:i?.body.cases.length??n.cases})},async setPreview(e,t){let n=await r.setDatasetPreview(e,t),i=a.get(e)?.doc;i&&p(e,{doc:{...i,preview:n.preview??null}}),h({...Vr(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 Ur=3e4,Wr=(e,t)=>e.get(`promptlab.versions`)[t]??{};function Gr(e,t){return(e.get(`promptlab.versions`).dataset?.[t]??[]).map(e=>({...e,doc:K().upgradeDatasetBody(e?.doc)})).filter(e=>Kr(e.doc))}var Kr=e=>!!e&&typeof e==`object`&&Array.isArray(e.cases);function qr(e,t,n,r,i){let a=e.get(`promptlab.versions`),o=Wr(e,t),s=(o[n]??[]).slice(),c=structuredClone(r);s[0]&&Yr(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 Jr(e,t,n,r){let i=e.get(`promptlab.versions`),a=(i.dataset?.[t]??[]).slice(),o=structuredClone(n);a[0]&&Yr(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 Yr=(e,t)=>JSON.stringify(e)===JSON.stringify(t),$=Ge(),Xr=(0,Y.createContext)(null);function Zr({children:e,pause:t}){let n=X(),r=Fr(),i=(0,Y.useMemo)(()=>Hr({api:n,...t==null?{}:{pause:t},idle:{ms:Ur,cut:(e,t)=>Jr(r,e,t,`idle`)}}),[n,r,t]);return(0,Y.useEffect)(()=>{let e=()=>{i.flush()};return addEventListener(`pagehide`,e),()=>removeEventListener(`pagehide`,e)},[i]),(0,$.jsx)(Xr.Provider,{value:i,children:e})}function Qr(){let e=(0,Y.useContext)(Xr);if(!e)throw Error(`useDatasets outside a DatasetsProvider: the app mounts one at its root`);return e}function $r(){let e=Qr(),t=(0,Y.useSyncExternalStore)(e.subscribe,e.list);return(0,Y.useEffect)(()=>{e.list().list===null&&e.refresh()},[e]),t}function ei(e){let t=Qr(),n=(0,Y.useSyncExternalStore)(t.subscribe,()=>e?t.entry(e):void 0);return(0,Y.useEffect)(()=>{e&&t.load(e)},[t,e]),n}var ti=e=>e.name.trim()||e.text.trim().split(`
|
|
2
|
-
`)[0].trim()||`Untitled prompt`,ni=e=>e?.state===`conflict`?`Data conflict: not saved — a newer version was saved in another browser.`:e?.state===`offline`?`Not saved: ${e.message}`:null,ri=({versions:e,uses:t,...n})=>n;function ii({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(ri(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(ri(f)),u)return p(t)}catch(e){let n=r.get(t);if(!n?.doc)return;let a=e instanceof Ln&&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:Q(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:Q(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:Q(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(ri(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 ai=(0,Y.createContext)(null);function oi({children:e,pause:t}){let n=X(),r=(0,Y.useMemo)(()=>ii({api:n,...t==null?{}:{pause:t}}),[n,t]);return(0,Y.useEffect)(()=>{let e=()=>{r.flush()};return addEventListener(`pagehide`,e),()=>removeEventListener(`pagehide`,e)},[r]),(0,$.jsx)(ai.Provider,{value:r,children:e})}function si(){let e=(0,Y.useContext)(ai);if(!e)throw Error(`usePrompts outside a PromptsProvider: the app mounts one at its root`);return e}function ci(){let e=si(),t=(0,Y.useSyncExternalStore)(e.subscribe,e.list);return(0,Y.useEffect)(()=>{e.refresh()},[e]),t}function li(e){let t=si(),n=(0,Y.useSyncExternalStore)(t.subscribe,()=>e?t.entry(e):void 0);return(0,Y.useEffect)(()=>{e&&t.load(e)},[t,e]),n}function ui({n:e,of:t}){return(0,$.jsxs)(`span`,{className:`fig-part`,children:[e,(0,$.jsxs)(`span`,{className:`fig-of`,children:[`/`,t]})]})}var di=e=>e===`good`?` fig-good`:e===`bad`?` fig-bad`:``;function fi({fig:e}){return(0,$.jsx)(`span`,{className:`fig-value${di(e.tone)}`,children:e.value??(e.of?(0,$.jsx)(ui,{n:e.of[0],of:e.of[1]}):`—`)})}function pi({fig:e}){return(0,$.jsxs)(`div`,{className:`fig`,children:[(0,$.jsx)(`span`,{className:`fig-label`,children:e.label}),(0,$.jsx)(fi,{fig:e})]},e.key)}function mi({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)(fi,{fig:t}),t.under&&(0,$.jsx)(`span`,{className:`fig-under`,children:(0,$.jsx)(ui,{n:t.under[0],of:t.under[1]})})]})]}),!!n.length&&(0,$.jsx)(`div`,{className:`fig-rest`,children:n.map(e=>(0,$.jsx)(pi,{fig:e},e.key))})]})}var hi=[`datasets`,`filters`,`prompts`,`sources`],gi=[`connections`,`workspaces`],_i=[`connections`],vi={tab:`home`},yi={history:[`overview`,`runs`],library:hi,setup:gi},bi=e=>`evals-lab.section.${e}`;function xi(e){let t=yi[e],n=null;try{n=sessionStorage.getItem(bi(e))}catch{}return n&&t.includes(n)?n:t[0]}function Si(e){if(!(e.tab!==`history`&&e.tab!==`library`&&e.tab!==`setup`||!e.section))try{sessionStorage.setItem(bi(e.tab),e.section)}catch{}}function Ci(e){let t=e.indexOf(`?`),n=new URLSearchParams(t<0?``:e.slice(t+1)),r=(t<0?e:e.slice(0,t)).replace(/^#\/?/,``).split(`/`).map(ki),i=null;if((r[0]===`evals`||r[0]===`datasets`||r[0]===`sources`)&&(r=[`library`,r[0]===`sources`?`sources`:`datasets`,...r.slice(1)],i=`#/${r.map(encodeURIComponent).join(`/`)}`),r[0]===`setup`&&r[1]===`wizards`){let e=r.slice(2).filter(Boolean);r=e.length?[`home`,`guides`,...e]:[`home`],i=`#/${r.map(encodeURIComponent).join(`/`)}`}let a=wi(r);if(a.tab===`history`){let e=n.get(`pipeline`),t=n.get(`content`);e&&(a.pipeline=e),t&&(a.content=t)}let o=r.length===1||r.length===2&&!r[1];return a.tab===`history`&&!a.section?(a.section=n.size?`runs`:xi(`history`),i=Ti(a)):o&&(a.tab===`library`||a.tab===`setup`)&&(a.section=xi(a.tab),i=Ti(a)),{route:a,rewrite:i}}function wi([e=``,...t]){let n=t.join(`/`)||null;switch(e){case``:return vi;case`home`:{let[e=``,...n]=t;return e===`guides`?{tab:`home`,guide:n.join(`/`)||null}:vi}case`history`:return t.length?t.length===1&&(t[0]===`overview`||t[0]===`runs`)?{tab:`history`,section:t[0]}:vi:{tab:`history`};case`runs`:return{tab:`runs`,id:n};case`results`:return n?{tab:`results`,id:n}:{tab:`history`};case`library`:{let[e=``,...r]=t,i=hi.find(t=>t===e);return i?{tab:`library`,section:i,id:r.join(`/`)||null}:{tab:`library`,section:`datasets`,id:n}}case`setup`:{let[e=``,...n]=t;if(!e)return{tab:`setup`,section:`connections`,id:null};if(e===`profiles`){let e=n.join(`/`)||null;return e?{tab:`setup`,section:`connections`,id:e,kind:`profile`}:{tab:`setup`,section:`connections`,id:null}}let r=gi.find(t=>t===e),i=n.join(`/`)||null;return!r||i&&!_i.includes(r)?vi:{tab:`setup`,section:r,id:i}}default:return vi}}function Ti(e){switch(e.tab){case`home`:return e.guide?`#/home/guides/${encodeURIComponent(e.guide)}`:`#/home`;case`history`:{let t=new URLSearchParams;e.pipeline&&t.set(`pipeline`,e.pipeline),e.content&&t.set(`content`,e.content);let n=e.section?`#/history/${e.section}`:`#/history`;return t.size?`${n}?${t}`:n}case`runs`:return e.id?`#/runs/${encodeURIComponent(e.id)}`:`#/runs`;case`results`:return`#/results/${encodeURIComponent(e.id)}`;case`library`:return`#/library/${e.section}${e.id?`/${encodeURIComponent(e.id)}`:``}`;case`setup`:return e.kind===`profile`&&e.id?`#/setup/profiles/${encodeURIComponent(e.id)}`:`#/setup/${e.section}${e.id?`/${encodeURIComponent(e.id)}`:``}`}}function Ei(e){location.hash=Ti(e)}function Di(e){history.replaceState(history.state,``,Ti(e)),window.dispatchEvent(new HashChangeEvent(`hashchange`))}function Oi(){return Ci(location.hash).route}function ki(e){try{return decodeURIComponent(e)}catch{return e}}var Ai=e=>(typeof e.id==`string`?e.id.trim():``)||`item:${K().caseItem(e)}`,ji=(e,t)=>e.trim().toLowerCase()===t.trim().toLowerCase(),Mi=e=>Array.isArray(e.metrics)?e.metrics:[],Ni=e=>!e.not&&e.weight!==0,Pi=e=>e.type===`contains`&&e.not===!0&&e.weight!==0,Fi=e=>K().metricLines(e);function Ii(e,t){let n=Mi(e);switch(t.type){case`bounds`:{let r=n.findIndex(e=>e.type===`item-count`&&Ni(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=>Pi(e)&&ji(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`&&Ni(e));return i>=0&&Fi(n[i].values).some(e=>ji(e,r))?e:{...e,metrics:i>=0?n.map((e,t)=>t===i?{...e,values:[...Fi(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(Pi(e)&&ji(String(e.value??``),r))return i=!0,[];if((e.type===`contains-all`||e.type===`contains-any`)&&Ni(e)&&Fi(e.values).some(e=>ji(e,r))){i=!0;let t=Fi(e.values).filter(e=>!ji(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 Li(e,t,n){let r=[];return n.type===`delete`?{next:e.filter(e=>{let n=t.has(Ai(e));return n&&r.push(Ai(e)),!n}),changed:r}:{next:e.map(e=>{if(!t.has(Ai(e)))return e;let i=Ii(e,n);return i!==e&&r.push(Ai(e)),i}),changed:r}}function Ri(e,t,n){let r=new Map(e.map(e=>[Ai(e),e])),i=new Map(t.map(e=>[Ai(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(K().validateEvals({cases:[t]})),s=`${n.id}: `,c=K().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:K().caseItem(n),reasons:c})}return{cases:a}}var zi={expect:`Expect`,forbid:`Forbid`},Bi=e=>zi[e],Vi=e=>`${e} ${e===1?`eval case`:`eval cases`}`;function Hi(e,t){let n=Vi(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 ${zi[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 Ui={graded:`Graded`,todo:`To do`,missing:`Missing`};function Wi(e,t){return t&&!t.includes(K().caseItem(e))?`missing`:e.todo?`todo`:`graded`}function Gi(e,t){let n=new Set(e.map(e=>K().caseItem(e)));return t.filter(e=>!n.has(e))}var Ki={file:``,status:`all`,show:`all`};function qi(e,t,n,r=null){let i=t.file.trim().toLowerCase();return e.filter(e=>(!i||K().caseItem(e).toLowerCase().includes(i))&&(t.status===`all`||Wi(e,r)===t.status)&&(t.show===`all`||n.has(Ai(e))))}var Ji=(0,Y.createContext)(null);function Yi({children:e}){let[t,n]=(0,Y.useState)(null),[r,i]=(0,Y.useState)(new Set),[a,o]=(0,Y.useState)(Ki),[s,c]=(0,Y.useState)(null),[l,u]=(0,Y.useState)(null),d=ei(l?.dataset??null),f=(0,Y.useRef)(null),p=(0,Y.useCallback)(e=>{e!==f.current&&(f.current=e,n(e),i(new Set),o(Ki),c(null))},[]),m=(0,Y.useCallback)(e=>{i(e)},[]),h=(0,Y.useCallback)(e=>{o(t=>({...t,...e}))},[]),g=(0,Y.useCallback)((e,t,r)=>{f.current=e,n(e),i(new Set),o({...Ki,show:`selected`}),c(null),u({dataset:e,files:t,from:r});let a=Ti({tab:`library`,section:`datasets`,id:e});location.hash===a?window.dispatchEvent(new HashChangeEvent(`hashchange`)):location.hash=a},[]),_=d?.doc?.body??null,v=d?.sync.state===`missing`;(0,Y.useEffect)(()=>{if(!l||!_&&!v)return;let e=new Set(l.files),t=(_?.cases??[]).filter(t=>e.has(K().caseItem(t))).map(Ai);i(new Set(t)),c({from:l.from,wanted:e.size,found:t.length}),u(null)},[l,_,v]);let y=(0,Y.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)(Ji.Provider,{value:y,children:e})}function Xi(){let e=(0,Y.useContext)(Ji);if(!e)throw Error(`useCases outside an CasesProvider: the app mounts one at its root`);return e}var Zi=(0,Y.createContext)(null);function Qi({children:e}){let[t,n]=(0,Y.useState)([]),[r,i]=(0,Y.useState)(null),a=(0,Y.useRef)(t);a.current=t;let o=(0,Y.useRef)(r);o.current=r;let s=(0,Y.useRef)(0),c=(0,Y.useCallback)((e,t)=>{a.current=e,n(e),o.current=t,i(t)},[]),l=(0,Y.useCallback)((e,t={})=>{let n=++s.current,r=a.current,i=r.find(t=>t.id===e),o=t.item==null?{}:{item:t.item},l;if(i)l=r.map(r=>r.id===e?{...r,from:t.from??r.from,last:n,...o,...t.item==null?{}:{show:`all`}}:r);else if(l=[...r,{id:e,at:t.at??``,from:t.from??`runs`,show:`all`,last:n,...o}],l.length>4){let e=l.reduce((e,t)=>e.last<t.last?e:t);l=l.filter(t=>t.id!==e.id)}c(l,e)},[c]),u=(0,Y.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,Y.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,Y.useCallback)((e,t)=>{c(a.current.map(n=>n.id===e?{...n,show:t}:n),o.current)},[c]),p=(0,Y.useCallback)(()=>{c(a.current,null)},[c]),m=(0,Y.useCallback)(()=>{let e=a.current.find(e=>e.id===o.current);window.location.hash=`#/${e?.from??`runs`}`,p()},[p]),h=(0,Y.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)(Zi.Provider,{value:h,children:e})}function $i(){let e=(0,Y.useContext)(Zi);if(!e)throw Error(`useResults outside a ResultsProvider: the app mounts one at its root`);return e}var ea={type:`Connection type`,profile:`Request profile`,px:`Resolution`,format:`Image format`,quality:`Image quality`,seed:`Seed`,nPredict:`Response 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 responses`,maxTokens:`Response tokens`},ta={format:`image/jpeg`,quality:`0.92`,seed:``,nPredict:``,repeatPenalty:``,frequencyPenalty:``,presencePenalty:``,dryMultiplier:``,dryBase:``,dryAllowedLength:``,grammar:``,px:`921600`},na=[`type`,`profile`,`px`,`format`,`quality`,`seed`,`nPredict`,`maxTokens`,`repeatPenalty`,`frequencyPenalty`,`presencePenalty`,`dryMultiplier`,`dryBase`,`dryAllowedLength`,`grammar`,`imagePosition`,`cachePrompt`,`thinking`,`rejectLoops`,`url`,`model`,`temperature`],ra=[`type`,`profile`,`url`,`model`,`temperature`],ia=[`px`,`format`,`quality`,`imagePosition`];function aa(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 oa=(e,t)=>({...ta,...t?.setupProfile?.settings||{}});function sa(e){return ea[e]??e}function ca(e,t){return e===`type`?la(String(t)):e===`profile`?t===`speed`?`Speed`:`Quality`:e===`px`?K().budgetLabel(t):e===`format`?String(t).replace(`image/`,``).toUpperCase():String(t)}function la(e){return K().CONNECTION_TYPES[e]?.label??e}var ua=e=>e.content?e.content!==`text`:null,da=e=>Number(new Date(e)).toString(36).slice(-6).toUpperCase(),fa=(e,t,n=`${t}s`)=>`${e} ${e===1?t:n}`,pa=e=>{let t=e?e.timed??e.ran:0;return t?e.ms/t:null};function ma(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 ha={queued:`Queued`,running:`Running`,done:`Done`,incomplete:`Incomplete`,cancelled:`Cancelled`,failed:`Failed`,interrupted:`Interrupted`},ga=e=>ha[e]??e,_a={done:`sage`,queued:`amber`,running:`amber`,failed:`rust`,incomplete:`rust`,cancelled:`rust`,interrupted:`rust`};function va(e){return ha[e]?(0,$.jsx)(Ye,{tone:_a[e]??`neutral`,className:`rchip ${e}`,children:ha[e]}):null}var ya=e=>e.length?e.reduce((e,t)=>e+t,0)/e.length:null;function ba(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 xa(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 Sa=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(pa).filter(e=>e!=null);return{pct:r?Math.round(100*n/r):null,avgScore:i.length?Math.round(ya(i)):null,avgTime:a.length?ya(a):null}},Ca=e=>{let t=Sa(e);return{pct:t.pct==null?`—`:`${t.pct}%`,avgScore:t.avgScore==null?`—`:`${t.avgScore}%`,avgTime:t.avgTime==null?`—`:ba(t.avgTime)}},wa=e=>e.profile??`(Default)`,Ta=`(No experiment)`,Ea=`(No content)`,Da=e=>(0,$.jsx)(`span`,{className:`unset`,children:e}),Oa=(e,t)=>t?(0,$.jsx)(`a`,{className:`link-btn`,href:t,children:e}):e,ka=e=>!!e.run&&(e.status===`cancelled`||e.status===`interrupted`),Aa=e=>e.rerunOf?(0,$.jsxs)(`span`,{className:`hrerun note`,children:[`Re-run of `,e.rerunOfAt?da(e.rerunOfAt):`a cleared run`]}):null,ja=[{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:da(e.at)}),Aa(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:Be(e.at)}),e.cancelled&&(0,$.jsx)(`span`,{className:`err`,children:` cancelled`})]})},{key:`setup`,label:`Experiment`,on:!0,sort:e=>e.setup||null,get:e=>e.setup?Oa(e.setup,e.pipelineHref):(0,$.jsx)(`span`,{className:`note`,children:Ta})},{key:`state`,label:`State`,on:!0,sort:e=>ga(e.status),get:e=>va(e.status)},{key:`content`,label:`Content`,on:!0,sort:e=>e.content||null,get:e=>e.content?Oa(e.content,e.contentHref):(0,$.jsx)(`span`,{className:`note`,children:Ea})},{key:`pct`,label:`% Passed Items`,on:!0,numeric:!0,sort:e=>Sa(e).pct,get:e=>Ca(e).pct},{key:`avgscore`,label:`Avg Score`,on:!0,numeric:!0,sort:e=>Sa(e).avgScore,get:e=>Ca(e).avgScore},{key:`avgtime`,label:`Avg Time`,on:!0,numeric:!0,sort:e=>Sa(e).avgTime,get:e=>Ca(e).avgTime},{key:`duration`,label:`Duration`,on:!1,numeric:!0,sort:e=>xa(e.startedAt,e.finishedAt),get:e=>{let t=xa(e.startedAt,e.finishedAt);return t==null?`—`:ba(t)}},{key:`ran`,label:`Ran`,on:!1,numeric:!0,sort:e=>e.done,get:e=>(0,$.jsx)(ui,{n:e.done,of:e.total})},{key:`plugins`,label:`Plugins`,on:!1,get:e=>e.plugins||Da(`(No plugins)`)},{key:`comment`,label:`Comment`,on:!1,sort:e=>e.comment,get:e=>(0,$.jsx)(`button`,{type:`button`,className:`ghost 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:`——`})})}],Ma=[{key:`name`,label:`Name`,on:!0,sort:(e,t)=>e?.name||`Variant ${K().targetLetter(t)}`,get:(e,t,n)=>(0,$.jsx)(`button`,{type:`button`,className:`ghost`,"data-scenario":n,children:t?.name||`Variant ${K().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)(Xt,{pass:e.mark})]},t)):t.evals||Da(`(No evals)`)},{key:`connection`,label:`Connection`,on:!0,sort:e=>e?.connection,get:(e,t)=>t?.connection||Da(`(No connection)`)},{key:`profile`,label:`Profile`,on:!0,sort:e=>e?.setupProfile?wa(e):null,get:(e,t,n)=>t?.setupProfile?(0,$.jsx)(`button`,{type:`button`,className:`ghost`,"data-prof":n,children:wa(t)}):`—`},{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)(ui,{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=>pa(e),get:(e,t)=>{let n=pa(t);return n==null?`—`:ba(n)}},{key:`total`,label:`Total`,on:!1,numeric:!0,sort:e=>e.ms||null,get:(e,t)=>t.ms?ba(t.ms):`—`},{key:`model`,label:`Model`,on:!1,get:(e,t)=>t.model||Da(`(Not set)`)},{key:`res`,label:`Resolution`,on:!1,image:!0,get:(e,t)=>String(K().budgetLabel(oa(e,t).px))},{key:`format`,label:`Format`,on:!1,image:!0,get:(e,t)=>String(oa(e,t).format).replace(`image/`,``).toUpperCase()},{key:`quality`,label:`Quality`,on:!1,image:!0,get:(e,t)=>oa(e,t).format===`image/png`?`—`:String(oa(e,t).quality)},{key:`seed`,label:`Seed`,on:!1,get:(e,t)=>String(oa(e,t).seed||`1234`)},{key:`predict`,label:`Response tokens`,on:!1,get:(e,t)=>String(oa(e,t).nPredict||oa(e,t).maxTokens||`—`)},{key:`penalty`,label:`Repeat penalty`,on:!1,get:(e,t)=>String(oa(e,t).repeatPenalty||`—`)},{key:`decoding`,label:`Decoding`,on:!1,get:(e,t)=>aa(oa(e,t))||Da(`(Default)`)},...[[`case`,`Case`],[`tidy`,`Tidy items`],[`dedupe`,`Remove duplicates`],[`keep-first`,`Keep first`],[`drop-echoes`,`Drop prompt echoes`]].map(([e,t])=>({key:`modifier-${e}`,label:t,on:!1,get:(t,n)=>n.modifiers?.[e]||Da(`(Not set)`)}))],Na={"Run Scenarios":`Run Variants`},Pa={tests:`evals`},Fa={connection:`profile`},Ia=[[`Runs`,ja],[`Run Scenarios`,Ma]],La=e=>![`queued`,`running`].includes(e),Ra=e=>e.cancel&&!La(e.status),za=()=>({ran:0,passed:0,found:0,of:0,ms:0,timed:0,share:0,shared:0});function Ba(e){return e.of?Math.round(100*e.found/e.of):e.shared?Math.round(100*e.share/e.shared):null}var Va=(e,t)=>e.filter(e=>!e.whole).map(e=>e.items[t]).filter(e=>!!e&&!K().isSkipped(e)),Ha=e=>Array.isArray(e)?e.length:typeof e==`number`?e:0;function Ua(e,t){let n=t.map(za);return(e||[]).forEach((e,r)=>{e&&!e.unrun&&(e.scenarios||[]).forEach((e,i)=>{let a=n[i];if(!a||!e)return;let o=Va(t[i],r);if(o.length){a.ran++,o.every(e=>e.pass)&&a.passed++;for(let e of o)a.found+=Ha(e.found),a.of+=Ha(e.found)+Ha(e.missed)+Ha(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 Wa=(e,t,n,r)=>{let i=r?K().wholeRunsOf(e,t,r):[];return K().targetsOf(e).map((r,a)=>K().scenarioEvals(e,a,t,La(n)).map(e=>{let t=i[a]?.[e.id];return t?{...e,wholeRun:t}:e}))},Ga=e=>Wa(e.snapshot||{},e.results||[],e.status);function Ka(e,t){return t.map((t,n)=>(e||[]).map((e,r)=>{if(!e||e.unrun||!e.scenarios?.[n])return null;let i=Va(t,r);return i.length?i.every(e=>e.pass):null}))}function qa(e,t){let n=e.rowStep.flatMap((e,n)=>e===t?[n]:[]),r=Ka(e.results,e.outcomes);return{items:n.length,passed:e.scenarios.map((e,t)=>{let i=n.map(e=>r[t]?.[e]).filter(e=>e!=null);return{passed:i.filter(Boolean).length,ran:i.length}})}}var Ja=(e,t,n)=>(e.outcomes[n]??[]).some(e=>!e.whole&&K().failedScore(e.items[t]));function Ya({done:e,name:t,hasCase:n,caseDatasets:r}){return!e||!t?null:n?`edit`:r>=1?`add`:null}function Xa(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 Za=e=>!e.skipped&&e.whole&&e.verdict?.ran?!!e.verdict.pass:null,Qa=e=>e.map(e=>`${e.label} ${Xa(e)}`).join(`, `),$a=e=>e.map(e=>({label:e.label,value:Xa(e),mark:Za(e)})),eo=e=>{if(!e.group)return null;let t=K().casesRef(e)?.n??e.pin;return t==null?null:`v${t}`},to=(e,t,n)=>e.group?.name||e.group?.id||(e.name||``).trim()||K().evalLabel({evals:n},t);function no(e){let t=e.evals,n=t.map((n,r)=>({key:n.id,name:to(n,r,t),version:eo(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?K().groupPass(i):null,skipped:!!i?.skipped}}),whole:e.outcomes.some(e=>e[r]?.wholeRun)?e.scenarios.map((t,n)=>{let i=e.outcomes[n]?.[r]?.wholeRun;return i?.ran?i.pass:null}):null})),r=e.snapshot.pass;return{rows:n,overall:t.length?{label:r?.mode===`atLeast`?`At least ${r.count}`:`All evals pass`,pass:e.scenarios.map((t,n)=>K().overallVerdict(e.outcomes[n]??[],r)),pct:e.tallies.map(e=>e.ran?Math.round(100*e.passed/e.ran):null)}:null}}var ro=e=>e.scenarios.map((t,n)=>K().recordedTargetOf(e.snapshot,n)),io=e=>e.findIndex(e=>!e);function ao(e){return!e||e.error?null:e.transcript?.at(-1)?.got!=null||e.raw!=null?K().gradedText(e):null}function oo(e,t,n,r){let i=e.results[t]?.scenarios?.[n]?.scores?.[r];return i&&!K().isSkipped(i)?i.metrics??[]:[]}var so=e=>`${e.type}::${e.label}`;function co(e,t){let n=e.scenarios.length,r=(n,r)=>oo(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=so(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=>so(e)===t);s&&s.weight!==0&&(o++,s.pass&&a++)}),o?{passed:a,ran:o}:null})}))}var lo=e=>typeof e.n==`number`&&Number.isInteger(e.n)?`${e.id}@${e.n}`:e.id;function uo(e,t,n,r=[],i=null){let a=e.own??(e.group?n[lo(e.group)]:void 0),o=K().casesRef(e),s=o?n[lo(o)]?.cases??(o.id===i?r:[]):[],c=t?s.find(e=>!e.todo&&K().caseItem(e)===t):void 0;return[...a?.every??[],...c?.metrics??[]]}function fo(e,t){let n=e=>[typeof e.metric==`string`&&e.metric.trim()||``,K().METRICS[e.type]?.label??e.type],r=0;return e.map(e=>{let i=t.findIndex((t,i)=>i>=r&&t.type===e.type&&n(t).includes(e.label));if(i<0)return null;r=i+1;let a=t[i];return[a.not?`not`:``,K().metricCheck(a)].filter(Boolean).join(` `)||null})}function po(e,t){let n=e.snapshot||{},r=K().contentOf(n),i=!!(r&&K().CONTENT_TYPES[r.type]?.inline),a=K().lastKind(n),o=K().runItemsOf(n),s=K().itemContents(n),c=new Map(s.map(({step:e,content:t})=>[e,t.type===`source`?t.ref.id:null])),l=e.results||[],u=Wa(n,l,e.status,t);return{id:e.id,status:e.status,at:e.submittedAt,behind:e.behind,name:n.name||`Run`,error:e.error,progress:e.progress||{current:null,n:0,total:0},scenarios:K().targetsOf(n).map((e,t)=>({id:e?.id||`scenario-${t}`,name:K().targetLabel(n,t),kind:a,plain:!(a&&K().OUTPUT_KINDS[a]?.terms),asked:_o(n,t)})),items:o.map(e=>e.name),groups:s.length>1?s.map(({step:e,content:t})=>({step:e,source:c.get(e)??null,name:t.type===`source`?t.ref.name||t.ref.id:K().CONTENT_TYPES[t.type]?.label??t.type})):[],rowStep:o.map(e=>e.step),rowSource:o.map(e=>c.get(e.step)??null),content:i?{type:`text`,text:r.text??``}:{type:`source`,source:r?.ref?.id??``,sourceName:r?.ref?.name??``},evals:K().evalsOf(n),snapshot:n,results:l,outcomes:u,tallies:Ua(l,u),startedAt:e.startedAt,finishedAt:e.finishedAt}}var mo={LOWER:`lower case`,SENTENCE:`Sentence case`,TITLE:`Title Case`,UPPER:`UPPER CASE`,NONE:`No change`};function ho(e){if(!e)return{};let t={};for(let n of K().outOf(e)?.modifiers||[])t[n.type]=n.type===`case`?mo[n.mode]??String(n.mode):K().modifierSummary(n);let n=(e.steps||[]).filter(e=>e?.type===`filters`);for(let e of n.flatMap(e=>e.filterSets||[]))for(let n of e.own?.filters||[]){let e=n.op,r=n.match,i=n.type===`drop-item`||n.type===`remove`&&e?.kind===`items`;n.type===`rewrite-item`&&e?.kind===`case`||n.type===`change-case`?t.case=mo[e?.mode??``]??String(e?.mode):n.type===`rewrite-item`&&e?.kind===`tidy`||n.type===`trim`&&e?.bullets?t.tidy=`on`:n.type===`keep-first`&&(e?.unit??`items`)===`items`?t[`keep-first`]=String(e?.n??``):i&&r?.duplicate?t.dedupe=`on`:i&&r?.echoes&&(t[`drop-echoes`]=`on`)}return t}function go(e,t){let n=K().scenarioProfile(e,t),r=K().targetsOf(e)[t],i=e.profiles,a=!r?.connection||!!i&&Object.hasOwn(i,K().answerKey(r.connection.id,r.profile?.id??null));return n?a?{connection:n.name,profile:r?.profile?.name??null}:{connection:null,profile:n.name}:{connection:null,profile:null}}var _o=(e,t)=>{if(K().recordedTargetOf(e,t))return null;let{connection:n,profile:r}=go(e,t);return[n,r].filter(Boolean).join(`, `)||null};function vo(e){let t=e.snapshot||{},n=K().contentOf(t),r=e.results||[],i=K().targetsOf(t),a=K().jobsOf(t),o=Wa(t,r,e.status),s=Ua(r,o),c=i.map((e,n)=>{let r=s[n]??za(),i=(a.length?K().stagesFor(t,n).connections:[]).map(e=>String(e.model||``)),c=i[0]?new Set(i).size===1?i[0]:i.join(` → `):``,l=Ba(r),u=K().overallVerdict(o[n]??[],t.pass);return{name:K().targetLabel(t,n),id:e?.id,model:c,setupProfile:K().scenarioProfile(t,n),...go(t,n),modifiers:ho(K().jobsOf(t).at(-1)),percent:l,passed:r.passed,cases:r.ran,pass:u,detail:u==null?`not graded`:Qa(o[n]??[]),evals:Qa(o[n]??[]),evalParts:$a(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||``,pipelineId:t.id||``,comment:t.comment||``,plugins:(t.plugins||[]).map(e=>`${e.id} ${e.version}`).join(`, `),done:u,total:l.total??u,content:n&&K().CONTENT_TYPES[n.type]?.inline?`text`:n?.type===`source`&&(n.ref?.name||n.ref?.id)||``,sourceId:n?.type===`source`&&n.ref?.id||``,scenarios:c,rerunOf:e.rerunOf??null,rerunOfAt:e.rerunOfAt??null}}var yo=(e,t)=>({key:`state`,label:`State`,value:t?`Stopping`:ga(e),tone:e===`done`?`good`:[`failed`,`incomplete`,`cancelled`,`interrupted`].includes(e)?`bad`:null}),bo=e=>e.length?e.reduce((e,t)=>e+t,0)/e.length:null;function xo(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&&!K().isSkipped(r))for(let e of r.metrics||[])typeof e.asked==`number`&&(t++,n+=e.asked);return{calls:t,ms:n}}function So(e,t=!1){let n=e.tallies,r=n.reduce((e,t)=>e+t.passed,0),i=n.reduce((e,t)=>e+t.ran,0),a=n.map(Ba).filter(e=>e!=null),o=a.length?`${Math.round(bo(a))}%`:`—`,s=xa(e.startedAt,e.finishedAt),c=xo(e.results),l={key:`ran`,label:`Run Items`,of:e.progress.total?[e.progress.n,e.progress.total]:null};return{lead:i?{key:`pct`,label:`% Passed Items`,value:`${Math.round(100*r/i)}%`}:{...l,key:`lead-ran`},rest:[{key:`run`,label:`Run ID`,value:e.at?(0,Y.createElement)(`code`,null,da(e.at)):`—`},yo(e.status,t),{key:`score`,label:`Avg Score`,value:o},{key:`duration`,label:`Duration`,value:s==null?`—`:ba(s)},...c.calls?[{key:`grader`,label:`Grader`,value:`${c.calls} ${c.calls===1?`call`:`calls`}, ${ba(c.ms)}`}]:[],...i?[l]:[],{key:`start`,label:`Start Date`,value:e.at&&Be(e.at)||`—`},...e.snapshot.comment?[{key:`comment`,label:`Comment`,value:(0,Y.createElement)(`span`,{className:`fig-comment`,title:e.snapshot.comment},e.snapshot.comment)}]:[]]}}var Co=e=>e.evals.length&&La(e.status)?`(Not graded)`:`—`;function wo(e,t){let n=e.tallies[t],r={key:`time`,label:`Avg Time`,value:To(n)??`—`};return n?.ran?{lead:{key:`pct`,label:`% Passed Items`,value:`${Math.round(100*n.passed/n.ran)}%`},rest:[{key:`score`,label:`Score`,value:Ba(n)==null?`—`:`${Ba(n)}%`},r]}:e.evals.length&&La(e.status)&&Oo(e,t)==null?{lead:{key:`pct`,label:`% Passed Items`,value:Co(e)},rest:[r]}:{lead:r,rest:[]}}function To(e){return e?.timed?ba(e.ms/e.timed):null}function Eo(e,t=!1){let n=t?`Stopping`:ga(e.status);return e.status===`queued`?{state:n,detail:e.behind?`behind ${e.behind}`:`next in line`}:e.status===`running`?{state:n,detail:e.progress.current||``}:null}function Do(e,t){return e.status===`running`&&e.progress.at===t?`busy`:e.results[t]?`done`:La(e.status)?`unreached`:`waiting`}function Oo(e,t){return K().overallVerdict(e.outcomes[t]??[],e.snapshot.pass)}var ko=(e,t)=>t!=null&&e.rowSource?.[t]!==void 0?e.rowSource[t]:e.content?.type===`source`?e.content.source:null;function Ao(e,t,n){let r=ko(e,n);return r===`samples`?`/api/samples/thumbs/${encodeURIComponent(t)}`:r?`/api/sources/${encodeURIComponent(r)}/thumbs/${encodeURIComponent(t)}`:``}function jo(e,t,n){let r=ko(e,n);return r===`samples`?`/api/samples/zoom/${encodeURIComponent(t)}`:r?`/api/sources/${encodeURIComponent(r)}/zoom/${encodeURIComponent(t)}`:`/api/samples/zoom/${encodeURIComponent(t)}`}var Mo=(e,t)=>t==null||t===``?null:`${e}: ${t}`,No=e=>(e||[]).map(String).join(`, `),Po=e=>e.length?e.every(t=>t===e[0])?String(e[0]):e.join(` → `):``,Fo=e=>{let t=K().OUTPUT_KINDS[K().lastKind(e)??K().defaultKind()];return t?!t.terms:!1},Io=e=>{let t=e.metric&&String(e.metric)||K().METRICS[e.type]?.label||e.type,n=K().metricCheck(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 Lo(e,t,n,r){let i=K().readCase(t,n,Fo(e)),{stages:a,connections:o}=K().stagesFor(e,r),s=o.map(e=>K().connectionSettings(e)),c=e=>Po(s.map(e)),l=K().evalsDataset(e);return[`An eval case is failing.`,``,`## The eval case`,Mo(`item`,K().caseItem(t)),Mo(`eval case id`,t.id),Mo(`note`,t.note),...(t.metrics||[]).map((e,t)=>Mo(`metric ${t+1}`,Io(e))),``,`## What came back`,Mo(`verdict`,i.pass?`pass`:`fail`),Mo(`score`,i.score==null?`—`:`${Math.round(i.score*100)}%`),Mo(`items`,No(n?.terms)||`(none)`),Mo(`missed`,No((i.missed??[]).map(e=>Array.isArray(e)?e.join(` / `):e))),Mo(`invented`,No(i.invented)),Mo(`dropped`,No((n?.dropped||[]).map(e=>`${e.item} (${e.by})`))),Mo(`reasons`,No(i.reasons)),Mo(`error`,n?.error),Mo(`took`,n?.ms==null?null:`${n.ms} ms`),``,`## How it was asked`,Mo(`variant`,`${r+1} of ${K().targetsOf(e).length}`),Mo(`model`,c(e=>e.model)),Mo(`endpoint`,c(e=>e.url||`(the lab's own Ollama)`)),Mo(`connection type`,c(e=>la(String(e.type??``)))),Mo(`image`,a[0]?.withImage?Ro(s[0]):`no image`),Mo(`seed`,c(e=>e.seed||`1234`)),Mo(`response tokens`,c(e=>e.nPredict||e.maxTokens||`not set`)),Mo(`repeat penalty`,c(e=>e.repeatPenalty||`(profile default)`)),Mo(`other decoding`,zo(s.at(-1))),Mo(`modifiers`,(K().outOf(K().jobsOf(e).at(-1))?.modifiers||[]).map(e=>`${K().MODIFIERS[e.type]?.label??e.type}: ${K().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 eval: ${l.name||l.id} (this eval 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 eval's README`].filter(e=>e!==null).join(`
|
|
5
|
-
`)}function Ro(e){let{px:t,format:n}=K().connectionImage(e);return`${t==null?`default budget`:K().budgetLabel(t)}, ${n.replace(`image/`,``).toUpperCase()}`}function zo(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 Bo(e,t){let n=[];return!La(e.status)||!K().evalsDataset(e.snapshot)||(e.results||[]).forEach((r,i)=>{r&&!r.unrun&&(r.scenarios||[]).forEach((a,o)=>{let s=t.find(e=>K().caseItem(e)===r.name);Ja(e,i,o)&&s&&a.res&&n.push({kase:s,res:a.res,scenarioIndex:o,item:r.name??``})})}),n}var Vo=e=>{let t=Math.min(...e),n=Math.max(...e);return t===n?String(t):`${t}–${n}`};function Ho(e,t,n){let r=K().evalsDataset(e.snapshot);if(!La(e.status)||!r)return[];let i=e.outcomes[n]?.find(t=>e.evals.find(e=>e.id===t.id&&K().casesRef(e))),a=new Map(t.map(e=>[K().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||K().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?`${Vo(r)}, ${e===`over`?`max`:`min`} ${Vo(i)}`:null}))}var Uo=[{value:`pass`,label:`% Passed Items`},{value:`score`,label:`Score`},{value:`time`,label:`Avg Time`}],Wo=[10,20,50],Go=e=>e===`done`,Ko=e=>e===`queued`||e===`running`,qo=(e,t)=>t?`${e}\u0000${t}`:e;function Jo(e){let t=new Map;for(let n of e)t.set(n.work?.id||n.id,(t.get(n.work?.id||n.id)??0)+1);return e.map(e=>{let n=e.work?.id||e.id,r=(t.get(n)??0)>1;return{id:r?e.id:n,doc:n,saved:e.id,label:e.name,only:r?e.name:null}})}var Yo=(e,t)=>{let n=t.snapshot?.id;if(!n)return;let r=e.filter(e=>e.doc===n);return r.length>1?r.find(e=>e.only===t.snapshot?.name):r[0]};function Xo(e){let t=ma(vo(e),e),n=e.results||[],r=Ga(e),i=Ka(n,r),a=new Map,o=[],s=new Map(K().itemContents(e.snapshot).map(e=>[e.step,e]));n.forEach((e,t)=>{if(!e||e.unrun)return;let n=e.name||`Item ${t+1}`,r=e.getItems?s.get(e.getItems):void 0,i=r?.content.type===`source`?r.content.ref:null,c=i&&r.dir?`${i.id}\u0001${n}`:n,l=a.get(c)??0;a.set(c,l+1),o.push({key:qo(c,l),name:n,x:t,...i?{source:i.name||i.id}:{}})});let c=new Map(o.map(e=>[e.x,e.key])),l=e.prompts||[],u=new Map;return t.scenarios.forEach((e,t)=>{if(!e.id)return;let n=l.filter(t=>t.target===e.id).sort((e,t)=>e.job-t.job),a=new Map;(i[t]??[]).forEach((e,t)=>{let n=c.get(t);e!=null&&n&&a.set(n,e)}),u.set(e.id,{id:e.id,name:e.name,prompt:n.map(e=>[`v${e.version}`,e.name].filter(Boolean).join(`, `)).join(` → `),version:n.map(e=>`v${e.version}`).join(` → `),connection:e.connection??``,model:e.model,passed:e.passed,cases:e.cases,pct:e.cases?Math.round(100*e.passed/e.cases):null,score:e.percent,avgTime:pa(e),evals:(r[t]??[]).map(e=>({id:e.id,label:e.label,passed:e.passed,ran:e.ran,mark:e.whole&&!e.skipped&&e.verdict?.ran?!!e.verdict.pass:null})),verdicts:a})}),{id:e.id,at:e.submittedAt,status:e.status,finished:Go(e.status),items:o,targets:u}}function Zo(e,t){let n=e.filter(e=>!Ko(e.status)).sort((e,t)=>t.submittedAt.localeCompare(e.submittedAt)||(e.id<t.id?1:e.id>t.id?-1:0)).slice(0,t).reverse().map(Xo),r=n.filter(e=>e.finished),i=r.at(-1)??null,a=r.at(-2)??null,o=i?[...i.targets.values()]:[],s=new Map(o.map(e=>[e.id,e.name]));for(let e of[...n].reverse())for(let t of e.targets.values())s.has(t.id)||s.set(t.id,t.name);return{runs:n,latest:i,previous:a,targets:o,rows:[...s].map(([e,t])=>({id:e,name:t}))}}var Qo=(e,t)=>t===`pass`?e.pct:t===`score`?e.score:e.avgTime;function $o(e,t){return e.map((e,n)=>({t:e,i:n,r:es(e,t)})).sort((e,t)=>ts(e.r,t.r)||e.i-t.i).map(e=>e.t)}var es=(e,t)=>[t,...Uo.map(e=>e.value).filter(e=>e!==t)].map(t=>{let n=Qo(e,t);return n==null?null:t===`time`?n:-n}),ts=(e,t)=>{for(let n=0;n<e.length;n++){let r=e[n]??null,i=t[n]??null;if(r!==i)return r==null?1:i==null?-1:r-i}return 0};function ns(e,t){let n=e[0];if(!n||e.length<2||Qo(n,t)==null)return new Set;let r=es(n,t),i=e.filter(e=>ts(es(e,t),r)===0);return i.length===e.length?new Set:new Set(i.map(e=>e.id))}function rs(e){let{latest:t,previous:n}=e;if(!t||!n)return null;let r=[],i=[];for(let e of t.targets.values()){let a=n.targets.get(e.id);if(a)for(let n of t.items){let t=e.verdicts.get(n.key),o=a.verdicts.get(n.key);t!=null&&o!=null&&t!==o&&(t?i:r).push({item:n,target:e})}}return{failing:r,passing:i}}function is(e,t,n){let r=null;return e.runs.map(e=>{let i=e.targets.get(t);if(!e.finished)return{run:e,value:null,gap:`unfinished`,version:null};if(!i)return{run:e,value:null,gap:`absent`,version:null};let a=r!=null&&i.version!==r&&i.version?i.version:null;r=i.version;let o=Qo(i,n);return{run:e,value:o,gap:o==null?`ungraded`:null,version:a}})}function as(e,t){if(t!==`time`)return 100;let n=0;for(let t of e.runs)if(t.finished)for(let e of t.targets.values())n=Math.max(n,e.avgTime??0);if(!n)return 1e3;let r=10**Math.floor(Math.log10(n));return([1,2,5,10].find(e=>e*r>=n)??10)*r}function os(e,t){let n=new Map;for(let t of[...e.runs].reverse())for(let e of t.items)n.has(e.key)||n.set(e.key,e);return[...n].map(([n,{name:r,source:i}])=>{let a=t.map(t=>{let i=e.runs.map(e=>e.finished?e.targets.get(t)?.verdicts.get(n)??null:null),a=i.filter(e=>e===!0).length,o=i.filter(e=>e===!1).length;return a+o?{key:n,name:r,cells:i,passed:a,failed:o}:null});return{key:n,name:r,...i?{source:i}:{},cells:a,failed:a.reduce((e,t)=>e+(t?.failed??0),0)}}).filter(e=>e.cells.some(Boolean)).sort((e,t)=>t.failed-e.failed||e.name.localeCompare(t.name))}var ss=[{id:`home`,label:`Home`,icon:wn,built:!0},{id:`runs`,label:`Experiments`,icon:C,built:!0},{id:`history`,label:`History`,icon:s,built:!0},{id:`library`,label:`Library`,icon:ue,built:!0},{id:`setup`,label:`Setup`,icon:Ct,built:!0}],cs=e=>e.replace(/[^A-Za-z0-9._-]+/g,`-`).replace(/^[-.]+|[-.]+$/g,``);function ls(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 us(e,t,n,r,i=new Date){return`${e}-${cs(t)||e}-v${n}-${ls(i)}.${r}`}function ds(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 fs=()=>Ei({tab:`library`,section:`datasets`,id:null}),ps=e=>Ei({tab:`library`,section:`datasets`,id:e}),ms=()=>Di({tab:`library`,section:`datasets`,id:null}),hs=(e,t=location.hash)=>`/w/${encodeURIComponent(e)}${t}`,gs=(e,t,n)=>n?`No ${e} ${t} in ${n.name}`:`No ${e} ${t}`,_s=`promptlab.ws.undo`;function vs(e){try{localStorage.setItem(_s,JSON.stringify(e))}catch{}}function ys(){let e=null;try{e=localStorage.getItem(_s),localStorage.removeItem(_s)}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 bs(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 xs=(0,Y.createContext)({current:{id:`default`,slug:`default`,name:`Default`,archived:!1,isDefault:!0},requestedSlug:null,list:[],switchTo:()=>{}}),Ss=()=>(0,Y.useContext)(xs),Cs=1600,ws=(0,Y.createContext)(null);function Ts({children:e}){let[t,n]=(0,Y.useState)(null),r=(0,Y.useRef)(null);(0,Y.useEffect)(()=>()=>{r.current&&clearTimeout(r.current)},[]);let i=(0,Y.useCallback)(e=>{Pt(e).then(e=>{n(e?{label:`Copied to clipboard.`}:{label:`Couldn't copy`,bad:!0}),r.current&&clearTimeout(r.current),r.current=setTimeout(()=>n(null),Cs)})},[]);return(0,$.jsxs)(ws.Provider,{value:i,children:[e,(0,$.jsx)(Qt,{undo:null,notice:t,onUndo:()=>{},onDismiss:()=>n(null)})]})}function Es(){let e=(0,Y.useContext)(ws);return(0,Y.useCallback)(t=>{e?e(t):Pt(t)},[e])}var Ds=(e,t)=>`${location.origin}${hs(e,Ti(t))}`;function Os(){let e=Es(),t=Ss().current?.slug??null;return(0,Y.useCallback)(n=>({label:`Copy link`,icon:J.link,onSelect:()=>{t&&e(Ds(t,n))}}),[e,t])}var ks={"eval-group":{noun:`eval`,href:e=>Ti({tab:`library`,section:`datasets`,id:e})},"filter-set":{noun:`filter set`,href:e=>Ti({tab:`library`,section:`filters`,id:e})},prompt:{noun:`prompt`,href:e=>Ti({tab:`library`,section:`prompts`,id:e})},source:{noun:`Source`,href:e=>Ti({tab:`library`,section:`sources`,id:e})},pipeline:{noun:`experiment`,href:e=>Ti({tab:`runs`,id:e})}};function As(e){let t=new Set,n=new Set,r=new Set,i=new Set,a=(e,t)=>{typeof t==`string`&&t&&e.add(t)},o=Array.isArray(e?.evals)?e.evals:[];for(let e of o)a(t,e?.group?.id),a(t,e?.dataset?.id);for(let t of K().targetsOf(e))for(let e of t?.steps??[])a(n,e?.from?.id);for(let t of K().jobsOf(e))for(let e of t?.steps??[])if(e?.content?.type===`source`&&a(i,e.content.ref?.id),Array.isArray(e?.filterSets))for(let t of e.filterSets)a(r,t?.set?.id);let s=K().profileIds(e)??[];return{groups:[...t],prompts:[...n],filterSets:[...r],sources:[...i],profiles:s}}var js=(e,t)=>e?.find(e=>e.id===t)?.name||t;function Ms(e,t){if(e.kind===`pipeline`){let n=t.pipelines.find(t=>t.id===e.id);if(!n)return[];let r=As(n.work),i=[],a=(e,t,n)=>{for(let r of t)i.push({kind:e,id:r,name:js(n,r),href:ks[e].href(r)})};return a(`eval-group`,r.groups,t.groups),a(`prompt`,r.prompts,t.prompts),a(`filter-set`,r.filterSets,t.filterSets),a(`source`,r.sources,t.sources),i}let n={"eval-group":`groups`,"filter-set":`filterSets`,prompt:`prompts`,source:`sources`}[e.kind],r=t.pipelines.filter(t=>As(t.work)[n].includes(e.id)).map(e=>({kind:`pipeline`,id:e.id,name:e.name,href:ks.pipeline.href(e.id)})),i=e.kind===`source`?(t.groups??[]).filter(t=>t.preview?.id===e.id).map(e=>({kind:`eval-group`,id:e.id,name:e.name||e.id,href:ks[`eval-group`].href(e.id)})):[];return[...r,...i]}function Ns({subject:e,scan:t,undo:n,onClose:r,onChanged:i,accountShare:a}){let o=X(),s=Ss();return s.current?(0,$.jsx)(et,{api:o,subject:e,workspaces:s.list,current:s.current,dependents:Ms(e,t),undo:n,onClose:r,onChanged:i,accountShare:a}):null}function Ps({undo:e}){let t=Os(),n=Qr(),{list:r,error:i}=$r(),[a]=Z(`promptlab.workflows`),[o,s]=c(`eval-groups`,20),[l,d]=(0,Y.useState)(!1),[f,p]=(0,Y.useState)(null),[m,h]=(0,Y.useState)(null),[g,_]=(0,Y.useState)(null),v=(0,Y.useRef)(null),[,y]=(0,Y.useState)(0);(0,Y.useEffect)(()=>n.subscribe(()=>y(e=>e+1)),[n]),(0,Y.useEffect)(()=>{for(let e of r??[])n.load(e.id)},[r,n]);let b=e=>p(e instanceof Error?e.message:String(e)),S=(0,Y.useMemo)(()=>{let e=new Map;for(let t of a.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},[a.list]),C=(r??[]).map(e=>({...e,body:n.entry(e.id)?.doc?.body??null,links:S.get(e.id)??{names:[],pinned:!1}})),w=()=>new Set((n.list().list??[]).map(e=>e.name)),T=async()=>{p(null);try{ps((await n.create(D(`Untitled eval`,w()))).id)}catch(e){b(e)}},E=async t=>{p(null);try{await n.flush(t.id);let r=await n.remove(t.id);e.offer({label:`Deleted ${t.name}`,restore:()=>{n.restore(r).catch(b)}})}catch(e){b(e)}},O=async e=>{p(null);let t;try{t=await Fs(e)}catch(e){b(e);return}let r;try{r=JSON.parse(t)}catch(e){p(`Not JSON: ${e instanceof Error?e.message:String(e)}`);return}h(`import`);try{let e=await n.importFile(r);e[0]&&ps(e[0].id)}catch(e){b(e)}finally{h(null)}},k=async()=>{h(`exportAll`);try{let e=await fetch(n.exportAllUrl());if(!e.ok)throw Error(`the export answered ${e.status}`);let t=await e.json();ds(new Blob([JSON.stringify(t,null,2)+`
|
|
6
|
-
`],{type:`application/json`}),us(`evals`,`all`,t.version,`json`))}catch(e){b(e)}finally{h(null)}},A=e=>[{label:`Open`,icon:Ee,onSelect:()=>ps(e.id)},t({tab:`library`,section:`datasets`,id:e.id}),{label:`Copy or move…`,icon:J.transfer,onSelect:()=>_(e)},{label:`Delete`,icon:J.remove,destructive:!0,onSelect:()=>void E(e)}],j=e=>e.scoring.mode===`weighted`?`Points`:`All metrics pass`,M=[{key:`group`,header:`Eval`,role:`title`,sort:e=>e.name.toLowerCase(),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>ps(e.id),children:e.name})},{key:`preview`,header:`Source Coverage`,sort:e=>e.preview?.name??``,tdProps:e=>({"data-state":e.preview?void 0:`none`}),cell:e=>e.preview?.name??(0,$.jsx)(`span`,{className:`unset`,children:`(No Source)`})},{key:`cases`,header:`Eval Cases`,className:`num`,sort:e=>e.cases,cell:e=>e.cases},{key:`every`,header:`Every item metrics`,className:`num`,sort:e=>e.body?.every.length??-1,cell:e=>e.body?e.body.every.length:`…`},{key:`scoring`,header:`Item passes when`,sort:e=>e.body?j(e.body):``,cell:e=>e.body?j(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?`experiment`:`experiments`}${e.links.pinned?`, pinned`:``}`}):(0,$.jsx)(`span`,{className:`unset`,children:`(Not linked)`})},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)($e,{label:`${e.name} actions`,actions:A(e)})}],N=[{label:`Import JSON…`,icon:u,busy:m===`import`,onSelect:()=>v.current?.click()},{label:`Export all`,icon:ar,disabled:!r?.length,busy:m===`exportAll`,onSelect:()=>void k()},{label:`Page size…`,icon:J.pageSize,onSelect:()=>d(!0)}];return(0,$.jsxs)(Tt,{id:`evals`,title:`Evals`,note:r?String(r.length):void 0,actions:N,about:`Checks that experiments link to grade responses.`,children:[(0,$.jsx)(`input`,{ref:v,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&&O(t)}}),r===null?(0,$.jsx)(Bt,{label:`Evals`,error:i,onRetry:()=>void n.refresh()}):(0,$.jsx)(W,{className:`eg-groups`,cards:!0,label:`Evals`,rows:C,rowKey:e=>e.id,pageSize:o,columns:M,empty:`(No evals)`}),f&&(0,$.jsx)(pt,{tone:`error`,children:f}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>void T(),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add eval`]}),l&&(0,$.jsx)(x,{value:o,onChange:s,onClose:()=>d(!1)}),g&&(0,$.jsx)(Ns,{subject:{kind:`eval-group`,id:g.id,name:g.name},scan:{pipelines:a.list},undo:e,onClose:()=>_(null),onChanged:()=>void n.refresh()})]})}function Fs(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)})}function Is(e){return[e.every&&`Every response`,e.not&&`Not`,e.of===`said`&&`Raw response`,e.weight!=null&&`Weight ${e.weight}`].filter(Boolean).join(`, `)}var Ls=(e,t)=>({type:e,...K().METRICS[e].defaults(),...t?.weight==null?{}:{weight:t.weight},...t?.not?{not:!0}:{},...t?.metric?{metric:t.metric}:{}});function Rs({label:e,metrics:t,set:n,undoable:r,over:i=`item`,rawReply:a}){let o=K(),[s,c]=(0,Y.useState)(null),[l,u]=(0,Y.useState)(null),d=(e,r)=>n(t.map((t,n)=>n===e?r:t)),f=e=>{s===`new`?n([...t,Ls(e)]):typeof s==`number`&&d(s,Ls(e,t[s])),c(null)};return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(dn,{noun:`metric`,items:[...t],empty:`(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:Dr,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=>nn(e,t,e=>d(n,e)));return(0,$.jsx)(Tt,{id:l,title:c,level:3,actions:r,about:s?.description,children:(0,$.jsxs)(Ne,{children:[m.length>0&&(0,$.jsx)(w,{label:`${e}, metric ${n+1}`,rows:m}),(0,$.jsx)(Tt,{id:`${l}-counts`,title:`Options`,level:3,open:u,note:u?Is(t):`Defaults`,children:(0,$.jsx)(w,{label:`${e}, metric ${n+1}, options`,rows:[...i===`run`?[{key:`every`,label:`Every response`,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:`The text before Read as`}:{},control:{type:`select`,value:t.of??`reply`,options:[{value:`reply`,label:`Parsed response`},{value:`said`,label:`Raw response`}],onChange:e=>{let{of:r,...i}=t;d(n,e===`said`?{...i,of:`said`}:i)}}}]:[],{key:`weight`,label:`Weight`,width:`quarter`,control:{type:`text`,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)(sn,{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)(zs,{title:s===`new`?`Add metric`:`Change type`,current:typeof s==`number`?t[s]?.type??null:null,over:i,onPick:f,onClose:()=>c(null)})]})}function zs({title:e,current:t,over:n,onPick:r,onClose:i}){let[a,o]=(0,Y.useState)(``),s=Object.values(K().METRICS).filter(e=>n!==`run`||!(e.graded||e.perItem)).length,c=(0,Y.useMemo)(()=>{let e=new Map;for(let[t,r]of Object.entries(K().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]),l=a.trim()?c[0]?.[1][0]:void 0,u=(0,Y.useId)();return(0,$.jsxs)(Ae,{title:e,onClose:i,className:`metric-picker`,onEnter:l&&(()=>r(l.id)),dismiss:`Cancel`,children:[(0,$.jsx)(xt,{noun:`metrics`,filters:[],count:{shown:c.reduce((e,[,t])=>e+t.length,0),total:s},search:{label:`Find a metric`,placeholder:`Find a metric`,value:a,onChange:o,describedBy:l?u:void 0,onKeyDown:wt(u)}}),c.length===0&&(0,$.jsx)(`p`,{className:`unset`,children:`(No matches)`}),c.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===l?` top-match`:``}`,...e.id===t?{"aria-current":`true`}:{},...e===l?{id:u,onKeyDown:U}:{},title:e.description,onClick:()=>r(e.id),children:e.label},e.id))})]},e))]})}function Bs(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:Be(n)}:null,...e.missing?{missing:!0}:{}}})],recent:e.filter(e=>!e.missing&&t(e.id)).sort((e,n)=>t(n.id).localeCompare(t(e.id))).slice(0,5).map(e=>e.id)}}var Vs=50,Hs=null;Bn.addEventListener(`write`,()=>{Hs=null});function Us(e){return Hs?.api!==e&&(Hs={api:e,at:Promise.resolve().then(()=>e.queue({limit:Vs})).then(({runs:e})=>{let t={};for(let n of e)for(let[e,r]of Object.entries(n.snapshot?.profiles??{})){let i=K().answerConnectionId(e,r);i&&(!t[i]||t[i]<n.submittedAt)&&(t[i]=n.submittedAt)}return t},()=>({}))}),Hs.at}function Ws(){let e=X(),[t]=Z(`promptlab.connections`),[n,r]=(0,Y.useState)({});(0,Y.useEffect)(()=>{let t=!0,n=()=>{Us(e).then(e=>{t&&r(e)})};return n(),Bn.addEventListener(`write`,n),()=>{t=!1,Bn.removeEventListener(`write`,n)}},[e]);let i=e=>t.list.find(t=>t.id===e)?.model?.trim()||void 0;return(e,t=[])=>{let{options:r,recent:a}=Bs(e,e=>n[e]??null,t);return{options:r.map(e=>e.fixed?e:{...e,detail:i(e.id)}),recent:a,usedLabel:`Last used`,detailLabel:`Model`}}}var Gs=e=>(K().CONNECTION_TYPES[e.type??``]?.answers??[`prompt`]).includes(`prompt`);function Ks(){let[e]=Z(`promptlab.connections`);return(0,Y.useMemo)(()=>{let t=e.list.find(t=>t.id===e.grader);return t?{id:t.id,name:t.name}:null},[e])}function qs(){let[e]=Z(`promptlab.connections`),t=Ws(),n=(0,Y.useMemo)(()=>e.list.filter(e=>K().listedConnection(e)&&Gs(e)),[e.list]),r=Ks();return{asked:n,labGrader:r,control:(e,i)=>({type:`picker`,kind:`Connection`,title:`Choose grader connection`,value:e,...t(n,[{id:``,label:r?`Lab (${r.name})`:`(No grader)`}]),onChange:i})}}function Js({name:e,body:t,onSave:n,onClose:r}){let[i]=(0,Y.useState)(()=>JSON.stringify(t,null,2)),a=(0,Y.useId)(),[o,s]=(0,Y.useState)(i),[c,l]=(0,Y.useState)(()=>Xs(o));(0,Y.useEffect)(()=>{let e=setTimeout(()=>l(Xs(o)),400);return()=>clearTimeout(e)},[o]);let u=c.problems,d=()=>{let e=Xs(o);e.body?n(e.body):l(e)};return(0,$.jsxs)(Ae,{title:`Edit raw… ${e}`,onClose:r,wide:!0,dirty:o!==i,children:[(0,$.jsx)(`textarea`,{className:`mono-block raw-content`,"aria-label":`Eval JSON`,...u.length?{"aria-invalid":!0,"aria-describedby":a}:{},spellCheck:!1,value:o,onChange:e=>s(e.target.value)}),(0,$.jsxs)(`div`,{className:`raw-foot`,children:[u.length?(0,$.jsx)(pt,{tone:`error`,inline:!0,id:a,children:`${u.length} to fix — ${u.join(`, `)}`}):(0,$.jsx)(`span`,{className:`note`,children:`Validated before it is accepted.`}),o===i?(0,$.jsx)(`button`,{type:`button`,className:`run`,onClick:r,children:`Close`}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:r,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,className:`run`,onClick:d,disabled:u.length>0,children:`Save & Close`})]})]})]})}var Ys=[`version`,`source`,`scoring`,`grader`,`every`,`run`,`cases`];function Xs(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:[`an eval has to be a JSON object`]};let n=t,r=[];for(let e of Object.keys(n))Ys.includes(e)||r.push(`${e} is not part of an eval`);Array.isArray(n.cases)||r.push(`cases has to be a list`);let i=K().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=Rr(),o={version:i,scoring:n.scoring??a.scoring,grader:n.grader??null,every:n.every??[],run:n.run??[],cases:n.cases};return r.push(...K().validateEvals(o)),r.length?{body:null,problems:r}:{body:o,problems:[]}}function Zs(e,t){return`/api/sources/${encodeURIComponent(e)}/thumbs/${encodeURIComponent(t)}`}function Qs(e){return/\.(?:jpe?g|png|gif|webp|bmp|heic|heif|dng|tiff?)$/i.test(e)}function $s(e){let t=X(),[n,r]=(0,Y.useState)(null);return(0,Y.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}function ec({title:e,action:t,start:n,taken:r,initial:i=``,onPick:a,onClose:o}){let s=X(),[c,l]=(0,Y.useState)(null);(0,Y.useEffect)(()=>{let e=!0;return s.sources().then(t=>{e&&l(t.sources)}).catch(()=>{e&&l([])}),()=>{e=!1}},[s]);let[u,d]=(0,Y.useState)(n),f=$s(u?.id),p=(0,Y.useMemo)(()=>(f??[]).filter(e=>!r.has(e)),[f,r]),[m,h]=(0,Y.useState)(i),[g,_]=(0,Y.useState)(!!i);(0,Y.useEffect)(()=>{!g&&p[0]&&h(p[0])},[g,p]);let[y,b]=(0,Y.useState)(``),x=p.filter(e=>!y.trim()||e.toLowerCase().includes(y.trim().toLowerCase())),S=m.trim(),C=!!S&&r.has(S),T=[{key:`browse`,label:`Browse`,control:{type:`picker`,kind:`Source`,value:u?.id??``,usedLabel:`Last changed`,...Bs(c??(n?[n]:[]),e=>c?.find(t=>t.id===e)?.changed??null,[{id:``,label:`(No Source)`}]),onChange:e=>{let t=c?.find(t=>t.id===e)??(n?.id===e?n:null);d(t?{id:t.id,name:t.name}:null),_(!0),b(``)}}},{key:`item`,label:`Item`,width:`full`,control:{type:`text`,value:m,onChange:e=>{_(!0),h(e)}}}];return(0,$.jsxs)(Ae,{title:e,onClose:o,dirty:g&&S!==i.trim(),className:`item-picker`,onEnter:!S||C?void 0:()=>a(S),footer:(0,$.jsxs)(`div`,{className:`edit-actions`,children:[C&&(0,$.jsxs)(pt,{tone:`error`,inline:!0,children:[S,` has an eval case`]}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:o,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,className:`edit-done`,disabled:!S||C,onClick:()=>a(S),children:t})]}),children:[(0,$.jsx)(w,{label:e,rows:T}),u&&f&&(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(xt,{noun:`items`,filters:[],count:{shown:x.length,total:p.length},search:{label:`Find an item`,placeholder:`Find an item`,value:y,onChange:b}}),p.length?x.length?(0,$.jsx)(v,{className:`ip-items`,compact:!0,label:`${p.length} ${p.length===1?`item`:`items`} without an eval case`,legend:!0,value:S,onChange:h,options:x.map(e=>({value:e,label:e,lead:Qs(e)?(0,$.jsx)(`img`,{className:`ip-thumb`,src:Zs(u.id,e),alt:``,loading:`lazy`}):(0,$.jsx)(`span`,{className:`ip-thumb`,"aria-hidden":`true`})}))}):(0,$.jsx)(`p`,{className:`unset`,children:`(No matches)`}):(0,$.jsxs)(`p`,{className:`note`,children:[`Every item of `,u.name,` has an eval case.`]})]})]})}var tc=2e3,nc=e=>({id:``,item:e,todo:!0,note:``,metrics:[]}),rc=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 ic({item:e,cases:n,onWrite:r,problem:i,resultFor:a,scenarioCount:o,plain:s=!1,files:c,onFile:l,onClose:u,run:d,source:f=null,sourceFiles:p=null,onRemove:m,onSaved:h}){let g=n.find(t=>K().caseItem(t)===e),_=!g,[v,y]=(0,Y.useState)(()=>structuredClone(g??nc(e))),[b,x]=(0,Y.useState)(0),[S,C]=(0,Y.useState)(null),[w,T]=(0,Y.useState)(null),[E,D]=(0,Y.useState)(!1),[O,k]=(0,Y.useState)(!1),[A,j]=(0,Y.useState)(!1),M=(0,Y.useRef)(v);M.current=v;let N=(0,Y.useRef)(n);N.current=n;let P=(0,Y.useRef)(null),F=t=>N.current.filter(n=>!(t.id&&n.id===t.id)&&!(!t.id&&K().caseItem(n)===e)).some(e=>K().caseItem(e)===K().caseItem(t))?`${K().caseItem(t)} has an eval case already`:K().validateEvals({cases:[{...t,id:t.id||`new`}]})[0]?.replace(/^[^:]*: /,``)??null,I=F(v),ee=(0,Y.useCallback)(()=>{let e=M.current;if(F(e))return!1;e.id||(e={...e,id:rc(N.current)},M.current=e,y(e));let t=e;return r(e=>ac({cases:e},t)),h?.(K().caseItem(t)),!0},[r,h,e]);(0,Y.useEffect)(()=>()=>{P.current&&clearTimeout(P.current)},[]);let[L,te]=(0,Y.useState)(!1),R=e=>{y(e),te(!0)},ne=e=>{let t=M.current;R({...t,metrics:e,...t.todo&&!(t.metrics??[]).length&&e.length?{todo:!1}:{}})},re=w&&w[b]?w[b]:a(b),z=!re||v.todo||!(v.metrics??[]).length?null:K().readCase(v,re,s),B=async()=>{if(d&&!E){D(!0),C(null);try{let e=await d(K().caseItem(M.current));T(e),C(e.length?null:`No response to grade.`)}catch(e){C(e instanceof Error?e.message:String(e))}finally{D(!1)}}},V=c.indexOf(e),H=e=>{let t=V<0?null:c[V+e];if(!t)return;let n=L||_;(!n||ee())&&(n&&(k(!0),P.current&&clearTimeout(P.current),P.current=setTimeout(()=>k(!1),tc)),l(t))};(0,Y.useEffect)(()=>{let e=e=>{(e.key===`ArrowLeft`||e.key===`ArrowRight`)&&(e.target?.closest?.(`input, textarea, select, [contenteditable]`)||!at()&&le()?.classList.contains(`case-editor`)&&(e.preventDefault(),H(e.key===`ArrowRight`?1:-1)))};return document.addEventListener(`keydown`,e),()=>document.removeEventListener(`keydown`,e)});let ie=()=>{(!L&&!_||ee())&&u()},U=K().caseItem(v),ae=new Set(n.filter(t=>!(v.id&&t.id===v.id)&&K().caseItem(t)!==e).map(e=>K().caseItem(e))),oe=!!p&&!p.includes(U),W=[{key:`item`,label:`Item`,control:f?{type:`static`,text:oe?`${U||`None`} (missing)`:U}:{type:`text`,value:U,onChange:e=>R({...M.current,item:e.trim()})}},{key:`todo`,label:`To do`,control:{type:`checkbox`,checked:!!v.todo,label:`Not graded yet`,onChange:e=>R({...M.current,todo:e})}},{key:`note`,label:`Note`,control:{type:`textarea`,value:v.note??``,onChange:e=>R({...M.current,note:e})}}],se=[];o>1&&se.push({key:`scenario`,label:`Show result of`,control:{type:`select`,value:String(b),options:Array.from({length:o},(e,t)=>({value:String(t),label:`Variant ${K().targetLetter(t)}`})),onChange:e=>x(Number(e))}}),z&&(se.push({key:`verdict`,label:`Result`,control:{type:`static`,text:z.pass?`Pass`:`Fail`}}),z.watch.length&&se.push({key:`watch`,label:`Watch`,control:{type:`static`,text:`${z.watch.filter(e=>e.pass).length} of ${z.watch.length}`}}));let G=f&&U&&Qs(U)&&p?.includes(U)?{src:Zs(f.id,U),file:U,source:f.name}:null,ue=[...G?[{key:`preview`,title:`Preview`,children:(0,$.jsxs)(`div`,{className:`cf-preview`,children:[(0,$.jsx)(`img`,{src:G.src,alt:G.file,loading:`lazy`}),(0,$.jsxs)(`p`,{className:`note`,children:[G.file,` in `,G.source]})]})}]:[],{key:`case`,title:`Eval Case`,rows:W},{key:`metrics`,title:`This item's metrics`,children:(0,$.jsx)(Rs,{label:`This item's metrics`,metrics:v.metrics??[],set:ne,undoable:ne})},...se.length?[{key:`result`,title:`Result`,rows:se}]:[]],de=[{label:`Previous`,icon:t,disabled:V<=0,onSelect:()=>H(-1)},{label:`Next`,icon:ce,disabled:V<0||V>=c.length-1,onSelect:()=>H(1)},...d?[{label:`Run`,icon:br,disabled:E,costly:!0,onSelect:()=>void B()}]:[]],fe=[{label:`Change item…`,icon:xr,onSelect:()=>j(!0)},...m?[{label:`Remove`,icon:ge,destructive:!0,onSelect:()=>{m(K().caseItem(M.current)),u()}}]:[]],pe=S??i??(I?`Not saved: ${I}`:null);return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(De,{title:U||`Eval Case`,status:pe??(E?`Running…`:O?`Saved.`:void 0),...pe?{statusBad:!0}:{},sections:ue,...fe.length?{menu:fe}:{},actions:de,dirty:L,adding:_,className:`case-editor`,onSave:ie,onClose:u}),A&&(0,$.jsx)(ec,{title:`Change item`,action:`Change`,start:f,taken:ae,onPick:e=>{j(!1),R({...M.current,item:e})},onClose:()=>j(!1)})]})}function ac(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 oc(e,t){return structuredClone(e.cases).filter(e=>K().caseItem(e)!==t)}var sc=(0,Y.createContext)(null);function cc({children:e}){let t=Qr(),[n,r]=(0,Y.useState)(null),i=ei(n?.dataset??null),a=(0,Y.useCallback)((e,t)=>{r({item:e,...t})},[]),o=(0,Y.useCallback)(e=>{r(t=>t&&{...t,item:e})},[]),s=i?.doc?.preview??null,c=$s(s?.id),l=n?.dataset??``,u=(0,Y.useCallback)(e=>{t.edit(l,t=>({...t,cases:e(t.cases)}))},[t,l]),d=(0,Y.useMemo)(()=>({openCaseEditor:a}),[a]),f=i?.doc;return(0,$.jsxs)(sc.Provider,{value:d,children:[e,n&&f&&(0,$.jsx)(ic,{item:n.item,cases:f.body.cases,onWrite:u,problem:zr(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}:{},...n.onSaved?{onSaved:n.onSaved}:{},onClose:()=>r(null)},n.item)]})}function lc(){let e=(0,Y.useContext)(sc);if(!e)throw Error(`useCaseEditor outside a CaseEditorProvider: the app mounts one at its root`);return e}function uc({rows:e,shown:t,filters:n,onFilters:r,selected:i,onSelect:a,selectionActions:o,sourceFiles:s,noCase:c,pageSize:l,onEdit:u,onAdd:d,onRemove:f,loading:p,loadError:m,onRetry:h,children:g}){let _=t.map(Ai),v=_.filter(e=>i.has(e)).length,y=v>0&&v===_.length,b=t=>e.filter(e=>Wi(e,s)===t).length,x={label:`Status`,value:n.status,onChange:e=>r({status:e}),options:[{value:`all`,label:`All`},{value:`graded`,label:`Graded ${b(`graded`)}`},{value:`todo`,label:`To do ${b(`todo`)}`},...s&&c?[{value:`missing`,label:`Missing ${b(`missing`)}`},{value:`nocase`,label:`No eval case ${c.length}`}]:[]]},S=n.status===`nocase`&&c?c.filter(e=>!n.file.trim()||e.toLowerCase().includes(n.file.trim().toLowerCase())):null,C=[{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)(Bt,{label:`Eval Cases`,error:m,onRetry:h})]}):(0,$.jsxs)(`div`,{className:`ds-casebody`,children:[(e.length>0||!!c?.length)&&(0,$.jsx)(xt,{noun:`eval cases`,search:{label:`Item`,placeholder:`Search items`,value:n.file,onChange:e=>r({file:e})},filters:C,quick:x,...S?{}:{count:{shown:t.length,total:e.length}},lead:(0,$.jsx)(`label`,{className:`ev-check ev-all`,children:(0,$.jsx)(te,{all:y,some:v>0,disabled:!t.length,label:`Select all shown`,onChange:()=>a(e=>Tn(e,_))})})}),g,S?S.length===0?(0,$.jsx)(`p`,{className:`unset`,children:`(No matches)`}):(0,$.jsx)(`div`,{className:`ds-cases`,children:(0,$.jsx)(W,{className:`dstable`,cards:!0,label:`Items with no eval case`,rows:S,pageSize:l,defaultSort:null,rowKey:e=>e,columns:[{key:`item`,header:`Item`,role:`title`,sort:e=>e,cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>d(e),children:e})},{key:`status`,header:`Status`,sort:()=>`No eval case`,cell:()=>(0,$.jsx)(`span`,{className:`ds-status ds-nocase`,children:`No eval case`})}]})}):e.length===0?(0,$.jsx)(`p`,{className:`repeater-empty`,children:`(No eval cases)`}):(0,$.jsx)($.Fragment,{children:t.length===0?(0,$.jsxs)($.Fragment,{children:[i.size>0&&(0,$.jsx)(V,{count:i.size,noun:`eval case`,hidden:i.size,actions:o,onClear:()=>a(()=>new Set)}),(0,$.jsx)(`p`,{className:`unset`,children:`(No matches)`})]}):(0,$.jsx)(`div`,{className:`ds-cases`,children:(0,$.jsx)(W,{className:`dstable`,cards:!0,label:`Eval Cases`,rows:t,pageSize:l,defaultSort:null,rowKey:e=>Ai(e),select:{noun:`eval case`,selected:i,onChange:e=>a(()=>e),actions:()=>o},columns:dc(s,u,f)})})}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>d(),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add eval case`]})]})}function dc(e,t,n){let r=t=>Wi(t,e);return[{key:`item`,header:`Item`,role:`title`,sort:e=>e.item,cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>t(e.item),children:e.item})},{key:`status`,header:`Status`,sort:e=>Ui[r(e)],tdProps:e=>({"data-state":r(e)}),cell:e=>(0,$.jsx)(`span`,{className:`ds-status ds-${r(e)}`,children:Ui[r(e)]})},{key:`metrics`,header:`Metrics`,className:`num`,sort:e=>fc(e).length,cell:e=>fc(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)($e,{label:`${e.item} actions`,actions:[{label:`Edit…`,icon:J.edit,onSelect:()=>t(e.item)},{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>n(e.item)}]})}]}var fc=e=>(e.metrics??[]).filter(e=>e.weight!==0),pc={bounds:`Set count`,addTerm:`Add term`,removeTerm:`Remove term`,todo:`Mark To do`,graded:`Mark graded`},mc=[{value:`keep`,label:`Keep`},{value:`set`,label:`Set`},{value:`clear`,label:`Clear`}],hc=e=>/^\s*\d+\s*$/.test(e)?Number(e):null;function gc({kind:e,cases:t,keys:n,onApply:r,onClose:i}){let[a,o]=(0,Y.useState)(`keep`),[s,c]=(0,Y.useState)(`keep`),[l,u]=(0,Y.useState)(``),[d,f]=(0,Y.useState)(``),[p,m]=(0,Y.useState)(`expect`),[h,g]=(0,Y.useState)(``),[_,v]=(0,Y.useState)(null),y=(0,Y.useMemo)(()=>{switch(e){case`bounds`:{let e=a===`set`?hc(l):null,t=s===`set`?hc(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,Y.useMemo)(()=>{if(typeof y==`string`)return null;let{next:e,changed:r}=Li(t,n,y);return{changed:r,problems:Ri(t,e,r)}},[y,t,n]),x=_??b?.problems??null,S=!!x&&x.cases.length>0,C=b?.changed.length??0,T=(0,Y.useCallback)(()=>{if(typeof y==`string`)return;let e=r(y);e&&v(e)},[y,r]),E=e=>t=>{v(null),e(t)},D=[];return e===`bounds`&&(D.push({key:`min`,label:`Min items`,required:!0,control:{type:`select`,value:a,options:mc,onChange:E(e=>o(e))}}),a===`set`&&D.push({key:`minN`,label:`Min`,required:!0,width:`quarter`,control:{type:`text`,value:l,onChange:E(u)}}),D.push({key:`max`,label:`Max items`,required:!0,control:{type:`select`,value:s,options:mc,onChange:E(e=>c(e))}}),s===`set`&&D.push({key:`maxN`,label:`Max`,required:!0,width:`quarter`,control:{type:`text`,value:d,onChange:E(f)}})),e===`addTerm`&&D.push({key:`list`,label:`List`,required:!0,control:{type:`select`,value:p,onChange:E(e=>m(e)),options:[`expect`,`forbid`].map(e=>({value:e,label:Bi(e)}))}}),(e===`addTerm`||e===`removeTerm`)&&D.push({key:`term`,label:`Term`,required:!0,control:{type:`text`,value:h,onChange:E(g)}}),(0,$.jsxs)(Ae,{title:pc[e],onClose:i,dirty:a!==`keep`||s!==`keep`||!!l||!!d||!!h,className:`ev-bulk`,onEnter:!C||S?void 0:T,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:T,children:`Apply`})]}),children:[D.length>0&&(0,$.jsx)(w,{label:pc[e],rows:D}),(0,$.jsx)(`p`,{className:`note ev-changes`,role:`status`,children:typeof y==`string`?y:`Changes ${C} of ${n.size}`}),S&&x&&(0,$.jsx)(pt,{tone:`error`,className:`ev-refusal`,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 _c({pageSize:e=20}){let t=Fr(),n=Qr(),r=Dt(),{openCaseEditor:i}=lc(),{list:a}=$r(),{datasetId:o,selected:s,setSelected:c,filters:l,setFilters:u,handedOver:d}=Xi(),[f,p]=(0,Y.useState)(null),[m,h]=(0,Y.useState)(null),g=(0,Y.useCallback)(()=>p(null),[]),_=a?.find(e=>e.id===o)??a?.[0]??null,v=_?.id??``,y=ei(v||null),b=y?.doc?.body??null,x=b?.cases,S=(0,Y.useMemo)(()=>x?K().gradedSetFrom({cases:x}):[],[x]),C=y?.doc?.preview??_?.preview??null,w=$s(C?.id),T=(0,Y.useMemo)(()=>w?Gi(S,w):null,[S,w]),E=(0,Y.useMemo)(()=>new Set(S.map(Ai)),[S]),D=(0,Y.useMemo)(()=>new Set([...s].filter(e=>E.has(e))),[s,E]),O=(0,Y.useMemo)(()=>qi(S,l,D,w),[S,l,D,w]),k=!!y?.doc&&!C;(0,Y.useEffect)(()=>{k&&(l.status===`missing`||l.status===`nocase`)&&u({status:`all`})},[k,l.status,u]);let A=(0,Y.useCallback)(()=>n.entry(v)?.doc?.body??null,[n,v]),j=(0,Y.useCallback)(e=>{v&&n.edit(v,t=>({...t,cases:e}))},[n,v]),M=e=>{i(e,{dataset:v,files:O.map(e=>e.item),onRemove:P})},N=e=>{h(null),i(e,{dataset:v,files:[...S.map(e=>e.item),e],onRemove:P})};function P(e){let t=A()?.cases??[];j(oc({cases:t},e)),r.offer({label:`Removed ${e}`,restore:()=>j(t)})}let F=e=>{let i=A();if(!v||!i)return null;let{next:a,changed:o}=Li(i.cases,D,e);if(!o.length)return p(null),null;if(e.type!==`delete`){let e=Ri(i.cases,a,o);if(e.cases.length)return e}Jr(t,v,i,`bulk`);let s=i.cases;if(n.edit(v,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:Hi(e,o.length),restore:()=>n.edit(v,e=>({...e,cases:s}))}),p(null),null},I=D.size===0,ee=[{label:`Set count…`,icon:ie,disabled:I,onSelect:()=>p(`bounds`)},{label:`Add term…`,icon:J.add,disabled:I,onSelect:()=>p(`addTerm`)},{label:`Remove term…`,icon:gr,disabled:I,onSelect:()=>p(`removeTerm`)},{label:`Mark To do…`,icon:qn,disabled:I,onSelect:()=>p(`todo`)},{label:`Mark graded…`,icon:Xn,disabled:I,onSelect:()=>p(`graded`)},{label:`Delete`,icon:J.remove,destructive:!0,disabled:I,onSelect:()=>void F({type:`delete`})}];return(0,$.jsxs)(`div`,{className:`cases`,children:[a?.length?(0,$.jsx)(uc,{rows:S,shown:O,filters:l,onFilters:u,selected:D,onSelect:c,selectionActions:ee,sourceFiles:w,noCase:T,loading:!b,loadError:y?.sync.state===`missing`?y.sync.message:null,onRetry:()=>{v&&n.load(v)},pageSize:e,onEdit:M,onAdd:e=>h({item:e??``}),onRemove:P,children:d&&_&&(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 `,_.name,` now`]})]})}):null,m&&b&&(0,$.jsx)(ec,{title:`Add eval case`,action:`Add eval case`,start:C,initial:m.item,taken:new Set(S.map(e=>e.item)),onPick:N,onClose:()=>h(null)}),f&&b&&(0,$.jsx)(gc,{kind:f,cases:b.cases,keys:D,onApply:F,onClose:g}),(0,$.jsx)(Qt,{undo:r.undo,onUndo:r.undoNow,onDismiss:r.dismiss})]})}function vc({groupId:e,undo:t}){let r=Os(),i=X(),a=Fr(),o=Qr(),{list:l,error:d}=$r(),{pickDataset:f}=Xi(),[p,m]=(0,Y.useState)(!1),[g,_]=(0,Y.useState)(!1),[v,y]=(0,Y.useState)(null),[S,C]=(0,Y.useState)(!1),[T,E]=(0,Y.useState)(!1),[O,k]=c(`dataset-cases`,20),[A,j]=(0,Y.useState)(!1),[N,P]=(0,Y.useState)(null),[F,I]=(0,Y.useState)(!1),ee=(0,Y.useRef)(null),[L]=Z(`promptlab.versions`),[te]=Z(`promptlab.workflows`),[R,re]=(0,Y.useState)(null);(0,Y.useEffect)(()=>{let e=!0;return i.sources().then(t=>{e&&re(t.sources)}).catch(()=>{e&&re([])}),()=>{e=!1}},[i]);let z=ei(e)?.doc??null,B=z?.body??null,V=l?.find(t=>t.id===e)??null;(0,Y.useEffect)(()=>{f(e)},[e,f]);let H=Ss(),ie=(0,Y.useRef)(new Set);(0,Y.useEffect)(()=>{if(V){ie.current.add(e);return}l&&(ms(),ie.current.has(e)||t.notify({label:gs(`eval`,e,H.current)}))},[l,V,e,t.notify,H.current]);let U=(0,Y.useMemo)(()=>e?Gr(a,e):[],[a,e,L]),ae=e=>y({text:e instanceof Error?e.message:String(e),bad:!0}),oe=(0,Y.useCallback)(()=>o.entry(e)?.doc?.body??null,[o,e]),W=(0,Y.useCallback)(t=>o.edit(e,t),[o,e]),se=()=>new Set((o.list().list??[]).map(e=>e.name)),ce=async()=>{y(null);try{ps((await o.create(D(`Untitled eval`,se()))).id)}catch(e){ae(e)}},le=t=>{let n=t.trim();z&&n&&n!==z.name&&(y(null),o.rename(e,n).catch(e=>{e instanceof Ln&&e.status===409?y({text:`An eval is already called ${n}.`,bad:!0}):ae(e)}))},G=async()=>{if(z){y(null);try{await o.flush(e),ps((await o.create(D(`${z.name} copy`,se()),structuredClone(z.body),z.preview??null)).id)}catch(e){ae(e)}}},ue=async()=>{if(!z)return;y(null);let{name:n}=z;try{await o.flush(e);let r=await o.remove(e);fs(),t.offer({label:`Deleted ${n}`,restore:()=>{o.restore(r).then(e=>{ps(e.id),e.name!==n&&y({text:`Restored as ${e.name}.`,bad:!1})}).catch(ae)}})}catch(e){ae(e)}},de=async e=>{y(null);let t;try{t=await yc(e)}catch(e){ae(e);return}let n;try{n=JSON.parse(t)}catch(e){y({text:`Not JSON: ${e instanceof Error?e.message:String(e)}`,bad:!0});return}P(`import`);try{let e=await o.importFile(n);e[0]&&ps(e[0].id),y({text:`Imported ${e.map(e=>e.name).join(`, `)}.`,bad:!1})}catch(e){ae(e)}finally{P(null)}},fe=async()=>{e&&(P(`export`),await o.flush(e).catch(()=>{}),await pe(o.exportUrl(e),e=>us(`eval`,e.group?.name??e.dataset?.name??``,e.version,`json`)),P(null))},pe=async(e,t)=>{try{let n=await fetch(e);if(!n.ok)throw Error(`the export answered ${n.status}`);let r=await n.json();ds(new Blob([JSON.stringify(r,null,2)+`
|
|
7
|
-
`],{type:`application/json`}),t(r))}catch(e){ae(e)}},me=n=>{let r=oe();r&&(Jr(a,e,r,`restore`),W(()=>structuredClone(n.doc)),E(!1),t.offer({label:`Restored the eval`,restore:()=>W(()=>r)}))},he=qs(),_e=e=>{let{mode:t,threshold:n}=e.scoring;return[{key:`mode`,label:`Item passes when`,control:{type:`select`,value:t,options:[{value:`all`,label:`All metrics pass`},{value:`weighted`,label:`Points`}],onChange:e=>W(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`,width:`quarter`,control:{type:`text`,value:n==null?``:String(n),onChange:e=>W(t=>({...t,scoring:{mode:`weighted`,threshold:e.trim()?/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e:null}}))}}]:[],{key:`grader`,label:`Grader`,control:he.control(e.grader?.id??``,e=>{let t=he.asked.find(t=>t.id===e);W(e=>({...e,grader:t?{id:t.id,name:t.name}:null}))})}]},ve=z?.preview??null,ye=[{key:`preview`,label:`Source Coverage`,control:{type:`picker`,kind:`Source`,value:ve?.id??``,usedLabel:`Last changed`,...Bs([...ve&&R&&!R.some(e=>e.id===ve.id)?[{id:ve.id,name:`${ve.name} (missing)`,missing:!0}]:[],...R??[]],e=>R?.find(t=>t.id===e)?.changed??null,[{id:``,label:`(No Source)`}]),onChange:t=>{let n=R?.find(e=>e.id===t),r=n?{id:n.id,name:n.name}:ve?.id===t?ve:null;o.setPreview(e,r).catch(ae)}}}],be=e=>t=>W(n=>({...n,[e]:t})),xe=e=>(n,r)=>{let i=oe()?.[e]??[];be(e)(n),t.offer({label:r,restore:()=>W(t=>({...t,[e]:i}))})},Se=B?B.cases.filter(e=>!e.todo).length:0,Ce=[{label:`Rename…`,icon:b,disabled:!z,onSelect:()=>m(!0)},{label:`Clone`,icon:J.clone,disabled:!z,onSelect:()=>void G()},r({tab:`library`,section:`datasets`,id:e}),{label:`Copy or move…`,icon:J.transfer,disabled:!z,onSelect:()=>I(!0)},{label:`Import JSON…`,icon:u,busy:N===`import`,onSelect:()=>ee.current?.click()},{label:`Export JSON`,icon:M,disabled:!z,busy:N===`export`,onSelect:()=>void fe()},{label:`Export all`,icon:ar,disabled:!l?.length,busy:N===`exportAll`,onSelect:()=>{P(`exportAll`),pe(o.exportAllUrl(),e=>us(`evals`,`all`,e.version,`json`)).finally(()=>P(null))}},{label:`Restore…`,icon:s,disabled:!U.length||!z,onSelect:()=>E(!0)},{label:`Edit raw…`,icon:er,disabled:!z,onSelect:()=>C(!0)},{label:`Delete`,icon:ge,destructive:!0,disabled:!z,onSelect:()=>void ue()}],we=(0,$.jsx)(`input`,{ref:ee,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&&de(t)}}),Te=B?`${B.cases.length} ${B.cases.length===1?`eval case`:`eval cases`}, ${Se} graded`:void 0;return(0,$.jsxs)(`div`,{className:`datasets`,children:[(0,$.jsx)(n,{kind:`Eval`,kindId:`Eval group`,name:z?.name??V?.name??(l===null?null:`None`),onPick:l?.length?()=>_(!0):void 0,picking:g,meta:Te,primary:{label:`New eval`,icon:ne,onSelect:()=>void ce()},actions:Ce}),v&&(0,$.jsx)(pt,{tone:v.bad?`error`:`info`,children:v.text}),we,B==null?(0,$.jsx)(Bt,{label:`Eval`,error:d,onRetry:()=>void o.refresh()}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(Tt,{id:`grades`,title:`Grades`,level:2,children:(0,$.jsx)(w,{label:`Grades`,rows:_e(B)})}),(0,$.jsx)(Tt,{id:`every`,title:`Every item metrics`,level:2,note:B.every.length?String(B.every.length):void 0,children:(0,$.jsx)(Rs,{label:`Every item metrics`,metrics:B.every,over:`item`,set:be(`every`),undoable:xe(`every`)})}),(0,$.jsx)(Tt,{id:`run`,title:`Whole run metrics`,level:2,note:B.run.length?String(B.run.length):void 0,children:(0,$.jsx)(Rs,{label:`Whole run metrics`,metrics:B.run,over:`run`,set:be(`run`),undoable:xe(`run`)})}),(0,$.jsxs)(Tt,{id:`cases`,title:`Eval Cases`,level:2,note:String(B.cases.length),actions:[{label:`Page size…`,icon:J.pageSize,onSelect:()=>j(!0)}],children:[(0,$.jsx)(w,{label:`Preview`,rows:ye}),(0,$.jsx)(_c,{pageSize:O})]})]}),g&&l&&(0,$.jsx)(h,{kind:`Eval`,value:e,onClose:()=>_(!1),options:l.map(e=>({id:e.id,label:e.name})),onPick:e=>{y(null),ps(e)}}),p&&z&&(0,$.jsx)(sn,{title:`Rename eval`,value:z.name,onCommit:le,onClose:()=>m(!1)}),A&&(0,$.jsx)(x,{value:O,onChange:k,onClose:()=>j(!1)}),F&&z&&(0,$.jsx)(Ns,{subject:{kind:`eval-group`,id:e,name:z.name},scan:{pipelines:te.list},undo:t,onClose:()=>I(!1),onChanged:()=>void o.refresh()}),T&&z&&(0,$.jsx)(st,{noun:`eval`,versions:U,at:e=>e.at,reason:e=>e.reason,onRestore:me,onClose:()=>E(!1)}),S&&z&&(0,$.jsx)(Js,{name:z.name,body:z.body,onSave:e=>{W(()=>e),C(!1)},onClose:()=>C(!1)})]})}function yc(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)})}function bc({groupId:e}){let t=Dt();return(0,$.jsxs)($.Fragment,{children:[e?(0,$.jsx)(vc,{groupId:e,undo:t}):(0,$.jsx)(Ps,{undo:t}),(0,$.jsx)(Qt,{undo:t.undo,notice:t.notice,onUndo:t.undoNow,onDismiss:t.dismiss})]})}var xc=e=>e,Sc=e=>e?.state===`conflict`?`Data conflict: not saved — a newer version was saved in another browser.`:e?.state===`offline`?`Not saved: ${e.message}`:null,Cc=(e,t)=>JSON.stringify(e)===JSON.stringify(t),wc=({body:e,...t})=>t;function Tc({api:e,pause:t=800}){let n=xc(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(wc(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,versions:t.versions,filters:r.filters.length};if(d(e,{doc:c,sync:{state:`saved`}}),p(wc(c)),a.has(e))return g(e)}catch(t){let n=i.get(e);if(!n?.doc)return;let r=t instanceof Ln&&t.status===409?t.body?.current??null:null;if(r){if(Cc(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:Q(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:Q(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:Q(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({...wc(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 Ec=(0,Y.createContext)(null);function Dc({children:e,pause:t}){let n=X(),r=(0,Y.useMemo)(()=>Tc({api:n,...t==null?{}:{pause:t}}),[n,t]);return(0,Y.useEffect)(()=>{let e=()=>{r.flush()};return addEventListener(`pagehide`,e),()=>removeEventListener(`pagehide`,e)},[r]),(0,$.jsx)(Ec.Provider,{value:r,children:e})}function Oc(){let e=(0,Y.useContext)(Ec);if(!e)throw Error(`useFilterSets outside a FilterSetsProvider: the app mounts one at its root`);return e}function kc(){let e=Oc(),t=(0,Y.useSyncExternalStore)(e.subscribe,e.list);return(0,Y.useEffect)(()=>{e.list().list===null&&e.refresh()},[e]),t}function Ac(e){let t=Oc(),n=(0,Y.useSyncExternalStore)(t.subscribe,()=>e?t.entry(e):void 0);return(0,Y.useEffect)(()=>{e&&t.load(e)},[t,e]),n}function jc(e){let t=[];for(let n of K().jobsOf(e))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 Mc=()=>Ei({tab:`library`,section:`filters`,id:null}),Nc=e=>Ei({tab:`library`,section:`filters`,id:e}),Pc=()=>Di({tab:`library`,section:`filters`,id:null});function Fc({undo:e}){let t=Os(),n=Oc(),{list:r,error:i}=kc(),[a]=Z(`promptlab.workflows`),[o,s]=c(`filter-sets`,20),[l,u]=(0,Y.useState)(!1),[d,f]=(0,Y.useState)(null),[p,m]=(0,Y.useState)(null),[h,g]=(0,Y.useState)(!1),_=e=>f(e instanceof Error?e.message:String(e)),v=(0,Y.useMemo)(()=>{let e=new Map;for(let t of a.list)for(let n of jc(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},[a.list]),y=(r??[]).map(e=>({...e,links:v.get(e.id)??{names:[],pinned:!1}})),b=()=>new Set((n.list().list??[]).map(e=>e.name)),S=async e=>{f(null);try{Nc((await n.create(e,K().blankFilterSetBody())).id)}catch(e){_(e)}},C=async t=>{f(null);try{await n.flush(t.id);let r=await n.remove(t.id);e.offer({label:`Deleted ${t.name}`,restore:()=>{n.restore(r).catch(_)}})}catch(e){_(e)}},w=e=>[{label:`Open`,icon:Ee,onSelect:()=>Nc(e.id)},t({tab:`library`,section:`filters`,id:e.id}),{label:`Copy or move…`,icon:J.transfer,onSelect:()=>m(e)},{label:`Delete`,icon:J.remove,destructive:!0,onSelect:()=>void C(e)}],T=[{key:`set`,header:`Filter set`,role:`title`,sort:e=>e.name.toLowerCase(),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>Nc(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?`experiment`:`experiments`}${e.links.pinned?`, pinned`:``}`}):(0,$.jsx)(`span`,{className:`unset`,children:`(Not linked)`})},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)($e,{label:`${e.name} actions`,actions:w(e)})}],E=[{label:`Page size…`,icon:J.pageSize,onSelect:()=>u(!0)}];return(0,$.jsxs)(Tt,{id:`filter-sets`,title:`Filter sets`,note:r?String(r.length):void 0,actions:E,about:`Filters that experiments link to clean list responses.`,children:[r===null?(0,$.jsx)(Bt,{label:`Filter sets`,error:i,onRetry:()=>void n.refresh()}):(0,$.jsx)(W,{className:`eg-groups`,cards:!0,label:`Filter sets`,rows:y,rowKey:e=>e.id,pageSize:o,columns:T,empty:`(No filter sets)`}),d&&(0,$.jsx)(pt,{tone:`error`,children:d}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>g(!0),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add filter set`]}),h&&(0,$.jsx)(Ic,{taken:b(),onCreate:e=>void S(e),onClose:()=>g(!1)}),l&&(0,$.jsx)(x,{value:o,onChange:s,onClose:()=>u(!1)}),p&&(0,$.jsx)(Ns,{subject:{kind:`filter-set`,id:p.id,name:p.name},scan:{pipelines:a.list},undo:e,onClose:()=>m(null),onChanged:()=>void n.refresh()})]})}function Ic({taken:e,onCreate:t,onClose:n}){let r=D(`Untitled filter set`,e);return(0,$.jsx)(sn,{title:`New filter set`,value:``,placeholder:r,onCommit:e=>t(e||r),onClose:n})}var Lc=[{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:`duplicate`,scope:`item`,keys:[`duplicate`],label:`is already in the list`,control:`flag`},{id:`echoes`,scope:`item`,keys:[`echoes`],label:`is made only of the prompt's words`,control:`flag`},{id:`blank`,scope:`reply`,keys:[`blank`],label:`it is blank`,control:`blank`},{id:`textRegex`,scope:`reply`,keys:[`regex`],label:`it contains a pattern`,control:`regex`},{id:`fewerItems`,scope:`reply`,keys:[`itemCount`],label:`it has fewer items than`,control:`num`,dir:`below`,unit:`items`},{id:`moreItems`,scope:`reply`,keys:[`itemCount`],label:`it has more items than`,control:`num`,dir:`above`,unit:`items`},{id:`share`,scope:`reply`,keys:[`share`,`items`],label:`too many items are one of these words`,control:`share`}],Rc=(e,t)=>Lc.filter(n=>n.scope===e&&(!t||n.keys.every(e=>t.includes(e)))),zc=e=>{let t=K().FILTER_TYPES[e]?.matches;return t===`reply`?`reply`:t?`item`:null},Bc=e=>{let t=zc(e);return t?Rc(t,K().FILTER_TYPES[e].conditions):[]},Vc=e=>!!e&&typeof e==`object`&&!Array.isArray(e);function Hc(e,t,n,r){let i=Rc(e,r).filter(e=>e.keys[0]===t);return i.length===0?null:i.length===1?i[0]:(Vc(n)&&n.above!=null?i.find(e=>e.dir===`above`):i.find(e=>e.dir===`below`))??i[0]}function Uc(e,t,n){let r=[],i=new Set;for(let a of Rc(t,n)){let o=a.keys[0];if(i.has(o)||e[o]==null)continue;let s=Hc(t,o,e[o],n);s&&(r.push(s),s.keys.forEach(e=>i.add(e)))}return r}function Wc(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`flag`:n[t.keys[0]]=!0;break;case`share`:n.share={above:``},n.items={terms:[]}}return n}function Gc(e,t,n){return Rc(t,n).find(t=>t.keys.every(t=>e[t]==null))??null}function Kc(e,t,n){let r=Gc(e,t,n);return r?Wc(e,r):e}function qc(e,t){let n={...e};for(let e of t.keys)delete n[e];return n}function Jc(e,t,n){return Wc(qc(e,t),n)}function Yc(e,t,n,r){let i=n.keys[0];return Rc(t,r).filter(t=>t.keys[0]===i||t.keys.every(t=>e[t]==null))}var Xc=e=>e==null||e===``?``:String(e),Zc=e=>/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e.trim(),Qc=e=>Array.isArray(e.terms)?e.terms.map(String):[],$c=(e,t)=>({...e,terms:t}),el=e=>typeof e.regex==`string`?e.regex:``,tl=(e,t)=>({...e,regex:t}),nl=(e,t)=>Xc(Vc(e[t.keys[0]])?e[t.keys[0]][t.dir]:void 0),rl=(e,t,n)=>({...e,[t.keys[0]]:{[t.dir]:Zc(n)}}),il=e=>({pct:Xc(Vc(e.share)&&e.share.above!=null&&e.share.above!==``?Math.round(Number(e.share.above)*100):``),words:Vc(e.items)&&Array.isArray(e.items.terms)?e.items.terms.map(String):[]}),al=(e,t)=>({...e,share:{above:/^\d+(\.\d+)?$/.test(t.trim())?Number(t)/100:t.trim()}}),ol=(e,t)=>({...e,items:{terms:t}});function sl(e,t){let n=K().FILTER_TYPES[t],r=n?.matches===`limit`?void 0:Bc(t)[0],i=n?.defaultOp?.();return{id:e,type:t,match:r?Wc({},r):{},...i?{op:i}:{},options:{ignoreFailures:!1,repeat:1},tests:[]}}function cl({scope:e,prefix:t,allowed:n,match:r,onChange:i}){let a=Uc(r,e,n),[o,s]=t,c=(0,Y.useId)();return(0,$.jsxs)(`div`,{className:`cond-builder`,children:[a.map((t,a)=>(0,$.jsxs)(`div`,{className:`cond-row`,"data-control":t.control,children:[(0,$.jsxs)(`div`,{className:`cond-line`,children:[(0,$.jsx)(`span`,{className:`cond-prefix`,children:a===0?o:s}),(0,$.jsx)(`select`,{className:`cond-pick`,"aria-label":`Condition`,value:t.id,onChange:a=>{let o=Yc(r,e,t,n).find(e=>e.id===a.target.value);o&&i(Jc(r,t,o))},children:Yc(r,e,t,n).map(e=>(0,$.jsx)(`option`,{value:e.id,children:e.label},e.id))}),t.control===`num`&&(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`input`,{type:`text`,inputMode:`numeric`,className:`cond-num`,"aria-label":t.label,value:nl(r,t),onChange:e=>i(rl(r,t,e.target.value))}),t.unit&&(0,$.jsx)(`span`,{className:`cond-unit`,children:t.unit})]}),t.control===`regex`&&(0,$.jsx)(`input`,{type:`text`,className:`cond-regex`,"aria-label":`Pattern`,placeholder:`a regular expression`,spellCheck:!1,value:el(r),onChange:e=>i(tl(r,e.target.value))}),(0,$.jsx)(`button`,{type:`button`,className:`cond-x`,"aria-label":`Remove condition ${a+1}`,onClick:()=>i(qc(r,t)),children:(0,$.jsx)(_n,{size:14,"aria-hidden":`true`})})]}),(t.control===`chips`||t.control===`share`)&&(0,$.jsxs)(`span`,{id:`${c}-${a}-words`,className:`sr-only`,children:[`Condition `,a+1,` words`]}),t.control===`chips`&&(0,$.jsx)(ve,{value:Qc(r),onChange:e=>i($c(r,e)),labelledBy:`${c}-${a}-words`}),t.control===`share`&&(0,$.jsxs)(`div`,{className:`cond-share`,children:[(0,$.jsx)(`input`,{type:`text`,inputMode:`numeric`,className:`cond-num`,"aria-label":`Share, percent`,value:il(r).pct,onChange:e=>i(al(r,e.target.value))}),(0,$.jsx)(`span`,{className:`cond-unit`,children:`% or more of items are one of these words`}),(0,$.jsx)(ve,{value:il(r).words,onChange:e=>i(ol(r,e)),labelledBy:`${c}-${a}-words`})]})]},t.id)),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn cond-add`,disabled:!Gc(r,e,n),onClick:()=>i(Kc(r,e,n)),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add condition`]})]})}var ll={"a-large-small":Un,"scan-search":wr,"text-search":jr,trash:Mr,eraser:Wt,replace:xr,scissors:Tr},ul=lr;function dl(e){let t=K().FILTER_TYPES[e]?.icon;return t&&ll[t]||null}var fl=e=>K().FILTER_TYPES[e]?.label??e;function pl({type:e}){let t=dl(e);return(0,$.jsxs)(`span`,{className:`verb-label`,children:[t&&(0,$.jsx)(t,{size:14,"aria-hidden":`true`}),fl(e)]})}var ml=e=>e.replace(/(^|\s)(\p{Ll})/gu,(e,t,n)=>t+n.toUpperCase()),hl=e=>!!K().FILTER_TYPES[e.type]?.rawReply?.(e),gl={comma:`Comma`,lines:`New line`,semicolon:`Semicolon`,none:`None`},_l=[{value:`LOWER`,label:`Lower case`},{value:`UPPER`,label:`Upper case`},{value:`SENTENCE`,label:`Sentence case`},{value:`TITLE`,label:`Title case`}],vl=[`mode`,`find`,`pattern`,`replacement`,`n`,`unit`,`bullets`],yl=(e,t)=>JSON.stringify(e)===JSON.stringify(t),bl=e=>/^\d+$/.test(e.trim())?Number(e):e.trim();function xl({filter:e,isNew:t,others:n,onSave:r,onClose:i}){let a=K(),o=a.FILTER_TYPES[e.type],s=zc(e.type),[c,l]=(0,Y.useState)(t?``:e.id),[u,d]=(0,Y.useState)(e.why??``),[f,p]=(0,Y.useState)(()=>structuredClone(e.match??{})),[m,h]=(0,Y.useState)(()=>structuredClone(e.op??{})),[g,_]=(0,Y.useState)(!!e.options?.ignoreFailures),[v,y]=(0,Y.useState)(String(e.options?.repeat??1)),[b,x]=(0,Y.useState)(()=>structuredClone(e.tests??[])),[S,C]=(0,Y.useState)(!1),[T,E]=(0,Y.useState)(null),D=()=>E(null),O=e=>{h(t=>({...t,...e})),D()},k=!!o?.conditions.length&&(o.matchesWith?.(m)??!0),A=(0,Y.useMemo)(()=>({id:c.trim()||(t?e.id:``),type:e.type,...u.trim()?{why:u.trim()}:{},match:k?f:{},...o?.defaultOp?{op:m}:{},options:{ignoreFailures:g,repeat:bl(v)},tests:b}),[c,u,f,m,g,v,b,e.type,k,o]),j=a.tryFilter(A),M=j.some(e=>!e.ok),N=(e,t)=>x(n=>n.map((n,r)=>r===e?{...n,...t}:n)),P=()=>{let e=A,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`)];if(t.length)return E(t);r(e)},F=[];if(o?.kinds){let t=typeof m.kind==`string`?m.kind:o.kinds[0].kind;F.push({key:`kind`,label:`What`,hint:o.kinds.find(e=>e.kind===t)?.about,control:{type:`select`,value:t,options:o.kinds.map(e=>({value:e.kind,label:e.label})),onChange:t=>{h(o.defaultOp(t)),(o.matchesWith?.({kind:t})??!0)&&!Object.keys(f).length&&p(sl(e.id,e.type).match),D()}}})}for(let e of vl)if(e in m){if(e===`mode`){let t=m.mode===`NONE`?[..._l,{value:`NONE`,label:`As written`}]:_l;F.push({key:e,label:`Case`,control:{type:`select`,value:String(m.mode??`LOWER`),options:t,onChange:e=>O({mode:e})}})}else if(e===`find`||e===`pattern`||e===`replacement`){let t={find:`Find`,pattern:`Pattern`,replacement:`Replace with`}[e];F.push({key:e,label:t,width:`full`,control:{type:`text`,value:String(m[e]??``),onChange:t=>O({[e]:t})}})}else e===`n`?F.push({key:e,label:`Keep`,width:`quarter`,control:{type:`text`,value:String(m.n??``),onChange:e=>O({n:bl(e)})}}):e===`unit`?F.push({key:e,label:`Of`,control:{type:`select`,value:String(m.unit??``),options:o?.choices?.unit??[],onChange:e=>O({unit:e})}}):e===`bullets`&&F.push({key:e,label:`Bullets and numbering`,control:{type:`checkbox`,checked:!!m.bullets,onChange:e=>O({bullets:e})}})}let I=[{key:`filter`,rows:[{key:`id`,label:`Name`,width:`full`,required:!0,control:{type:`text`,value:c,onChange:e=>{l(e),D()},...t?{placeholder:e.id}:{}}},{key:`type`,label:`Type`,control:{type:`static`,text:o?.label??e.type,icon:dl(e.type)},...t?{}:{hint:`Set when the filter was created — delete and recreate to change it.`}},...F,{key:`why`,label:`Notes`,width:`full`,control:{type:`text`,value:u,onChange:d,placeholder:`Add a note`}}]}];if(k&&s){let e=o?.matches===`reply`?[`${o.label} the response when`,`and`]:o?.matches===`limit`?[`Only when it`,`and it`]:[`${o?.label??``} an item when it`,`and it`];I.push({key:`match`,title:`When`,children:(0,$.jsx)(cl,{scope:s,prefix:e,allowed:o.conditions,match:f,onChange:e=>{p(e),D()}})})}return I.push({key:`tests`,title:`Tests`,children:(0,$.jsxs)(`div`,{className:`filter-tests`,children:[b.map((e,t)=>{let n=j[t],r=!!n?.ok;return(0,$.jsxs)(`div`,{className:`filter-test`,children:[(0,$.jsx)(w,{label:`Test ${t+1}`,rows:[{key:`sep`,label:`Split by`,control:{type:`select`,value:e.separator,options:a.FILTER_SEPARATORS.map(e=>({value:e,label:gl[e]??e})),onChange:e=>{N(t,{separator:e}),D()}}},{key:`in`,label:`Sample input`,control:{type:`textarea`,value:e.sampleInput,onChange:e=>{N(t,{sampleInput:e}),D()}}},{key:`out`,label:`Expected output`,control:{type:`textarea`,value:e.expectedOutput,onChange:e=>{N(t,{expectedOutput:e}),D()}}}]}),(0,$.jsxs)(`div`,{className:`filter-test-result`,children:[(0,$.jsx)(`span`,{className:`filter-test-verdict ${r?`hit`:`fault`}`,role:`status`,children:Sl(!!o?.rejects,r,n?.got??``,e)}),(0,$.jsx)(`button`,{type:`button`,className:`icon-btn`,"aria-label":`Remove test ${t+1}`,onClick:()=>{x(e=>e.filter((e,n)=>n!==t)),D()},children:(0,$.jsx)(_n,{size:14,"aria-hidden":`true`})})]})]},t)}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>{let e=o?.rawReply?.(A)||o?.rejects?`none`:`comma`;x(t=>[...t,{separator:e,sampleInput:``,expectedOutput:``}]),D()},children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add test`]})]})}),I.push({key:`options`,title:`Options`,rows:[{key:`ignore`,label:`Ignore failures`,control:{type:`checkbox`,checked:g,onChange:e=>{_(e),D()}}},{key:`repeat`,label:`Repeat`,hint:`Times to run the filter`,width:`quarter`,control:{type:`text`,value:v,onChange:e=>{y(e),D()}}}]}),(0,$.jsx)(De,{title:t?`New filter`:e.id,...T?.length?{status:T.join(` `),statusBad:!0}:{},sections:I,onSave:P,canSave:!M||S,footerStart:M?(0,$.jsxs)(`label`,{className:`edit-override`,children:[(0,$.jsx)(`input`,{type:`checkbox`,checked:S,onChange:e=>C(e.target.checked)}),`Save with failing tests`]}):void 0,adding:t,dirty:!yl(A,e),focusRow:`id`,wide:!0,onClose:i})}function Sl(e,t,n,r){let i=r.separator===`none`;if(e)return`${t?`Pass`:`Fail`} — ${n===`rejected`?`rejected`:`kept`}`;if(i&&n===`rejected`)return`${t?`Pass`:`Fail`} — rejected`;if(i)return t?`Pass — reads as expected`:`Fail — left ${n?JSON.stringify(n):`nothing`}`;let a=K().splitSample(r.expectedOutput,r.separator);return t?`Pass — ${a.length} of ${a.length} items as expected`:`Fail — kept ${n||`nothing`}`}function Cl({onPick:e,onClose:t}){let n=Object.entries(K().FILTER_TYPES).sort(([,e],[,t])=>e.label.localeCompare(t.label));return(0,$.jsx)(Ae,{title:`New filter`,onClose:t,className:`new-filter`,dismiss:`Cancel`,children:(0,$.jsx)(`ul`,{className:`nf-verbs`,"aria-label":`Filter type`,children:n.map(([t,n])=>{let r=dl(t);return(0,$.jsx)(`li`,{children:(0,$.jsxs)(`button`,{type:`button`,className:`nf-verb`,onClick:()=>e(t),children:[(0,$.jsx)(`span`,{className:`nf-verb-icon`,children:r&&(0,$.jsx)(r,{size:14,"aria-hidden":`true`})}),(0,$.jsxs)(`span`,{className:`nf-verb-text`,children:[(0,$.jsx)(`span`,{className:`nf-verb-name`,children:n.label}),n.about&&(0,$.jsx)(`span`,{className:`nf-verb-about`,children:n.about})]})]})},t)})})})}function wl({label:e,filters:t,format:n,onChange:r,onRemoved:i}){let[a,o]=(0,Y.useState)(null),[s,c]=(0,Y.useState)(!1),[l,u]=(0,Y.useState)(null),d=[...t],f=n!=null&&n>=0&&n<=d.length,p=d.map((e,t)=>({kind:`filter`,filter:e,n:t}));f&&p.splice(n,0,{kind:`format`});let m=e=>{let t=e.findIndex(e=>e.kind===`format`);r(e.flatMap(e=>e.kind===`filter`?[e.filter]:[]),t<0?void 0:t)},h=(e,t)=>{let n=[...p],[r]=n.splice(e,1);n.splice(e+t,0,r),m(n)},g=e=>p.indexOf(e),_=e=>[{label:`Move up`,icon:J.moveUp,disabled:g(e)===0,onSelect:()=>h(g(e),-1)},{label:`Move down`,icon:J.moveDown,disabled:g(e)===p.length-1,onSelect:()=>h(g(e),1)}],v=[{key:`n`,header:`#`,label:``,className:`num fs-n`,sort:null,cell:e=>e.kind===`filter`?e.n+1:null},{key:`id`,header:`Filter`,role:`title`,sort:null,cell:e=>e.kind===`format`?(0,$.jsxs)(`span`,{className:`fs-format-name`,children:[(0,$.jsx)(ul,{size:14,"aria-hidden":`true`}),`Response Format`]}):(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>o(e.n),children:e.filter.id})},{key:`type`,header:`Type`,className:`fs-type`,sort:null,cell:e=>e.kind===`filter`?(0,$.jsx)(pl,{type:e.filter.type}):null},{key:`examples`,header:`Tests`,sort:null,cell:e=>{if(e.kind===`format`)return null;let t=K().tryFilter(e.filter),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=>{if(e.kind===`format`)return(0,$.jsx)($e,{label:`Response Format actions`,actions:[..._(e),{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>m(p.filter(t=>t!==e))}]});let t=e.filter,r=e.n,a=[{label:`Edit…`,icon:J.edit,onSelect:()=>o(r)},{label:`Clone`,icon:J.clone,onSelect:()=>{let n=g(e);m([...p.slice(0,n+1),{kind:`filter`,filter:{...structuredClone(t),id:we(d,`${t.id} copy`)},n:-1},...p.slice(n+1)])}},..._(e),{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>{m(p.filter(t=>t!==e)),i?.(`Removed ${t.id}`,d,n)}}];return(0,$.jsx)($e,{actions:a,label:`${t.id} actions`})}}],y=a==null?void 0:d[a];return(0,$.jsxs)(`div`,{className:`filters-table`,children:[(0,$.jsx)(W,{empty:`(No filters)`,className:`rules fs-table`,cards:!0,label:e,rows:p,rowKey:e=>e.kind===`format`?`\0format`:e.filter.id,columns:v,rowProps:e=>e.kind===`format`?{className:`fs-format-row`}:{},defaultSort:null,select:{noun:`filter`,selectable:e=>e.kind===`filter`,label:e=>e.kind===`filter`?e.filter.id:`Response Format`,actions:e=>[{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>{let t=new Set(e);m(p.filter(e=>!t.has(e)));let r=e[0]?.kind===`filter`?e[0].filter.id:``;i?.(e.length===1?`Removed ${r}`:`Removed ${e.length} filters`,d,n)}}]}}),(0,$.jsx)(`span`,{className:`add-btn fs-add`,children:(0,$.jsx)($e,{label:`Add filter`,trigger:(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(J.add,{size:14,"aria-hidden":`true`}),`Add filter`]}),actions:[{label:`New filter…`,icon:J.add,onSelect:()=>c(!0)},{label:`Response Format`,icon:ul,disabled:f,onSelect:()=>r(d,d.length)}]})}),s&&(0,$.jsx)(Cl,{onPick:e=>{c(!1),u(sl(we(d,ml(fl(e))),e))},onClose:()=>c(!1)}),(l||y)&&(0,$.jsx)(xl,{filter:l??y,isNew:!!l,others:l?d:d.filter((e,t)=>t!==a),onSave:e=>{l?m([...p,{kind:`filter`,filter:e,n:-1}]):r(d.map((t,n)=>n===a?e:t),f?n:void 0),u(null),o(null)},onClose:()=>{u(null),o(null)}},l?`new`:a)]})}function Tl({setId:e,undo:t}){let r=Os(),i=Oc(),{list:a,error:o}=kc(),[s]=Z(`promptlab.workflows`),[c,l]=(0,Y.useState)(!1),[u,d]=(0,Y.useState)(!1),[f,p]=(0,Y.useState)(!1),[m,g]=(0,Y.useState)(null),[_,v]=(0,Y.useState)(!1),y=Ac(e)?.doc??null,b=y?.body??null,x=a?.find(t=>t.id===e)??null;(0,Y.useEffect)(()=>{a&&!x&&Pc()},[a,x]);let S=e=>g({text:e instanceof Error?e.message:String(e),bad:!0}),C=(0,Y.useCallback)(t=>i.edit(e,t),[i,e]),w=()=>new Set((i.list().list??[]).map(e=>e.name)),T=async e=>{g(null);try{Nc((await i.create(e)).id)}catch(e){S(e)}},E=t=>{let n=t.trim();y&&n&&n!==y.name&&(g(null),i.rename(e,n).catch(e=>{e instanceof Ln&&e.status===409?g({text:`A filter set is already called ${n}.`,bad:!0}):S(e)}))},O=async()=>{if(y){g(null);try{await i.flush(e),Nc((await i.create(D(`${y.name} copy`,w()),structuredClone(y.body))).id)}catch(e){S(e)}}},k=async()=>{if(!y)return;g(null);let{name:n}=y;try{await i.flush(e);let r=await i.remove(e);Mc(),t.offer({label:`Deleted ${n}`,restore:()=>{i.restore(r).then(e=>{Nc(e.id),e.name!==n&&g({text:`Restored as ${e.name}.`,bad:!1})}).catch(S)}})}catch(e){S(e)}},A=[{label:`Rename…`,icon:J.rename,disabled:!y,onSelect:()=>l(!0)},{label:`Clone`,icon:J.clone,disabled:!y,onSelect:()=>void O()},r({tab:`library`,section:`filters`,id:e}),{label:`Copy or move…`,icon:J.transfer,disabled:!y,onSelect:()=>v(!0)},{label:`Delete`,icon:ge,destructive:!0,disabled:!y,onSelect:()=>void k()}],j=b?b.filters.length:0,M=b?`${j} ${j===1?`filter`:`filters`}`:void 0;return(0,$.jsxs)(`div`,{className:`filters-page`,children:[(0,$.jsx)(n,{kind:`Filter set`,name:y?.name??x?.name??(a===null?null:`None`),onPick:a?.length?()=>p(!0):void 0,picking:f,meta:M,primary:{label:`New filter set`,icon:ne,onSelect:()=>d(!0)},actions:A}),m&&(0,$.jsx)(pt,{tone:m.bad?`error`:`info`,children:m.text}),b==null?(0,$.jsx)(Bt,{label:`Filter set`,error:o,onRetry:()=>void i.refresh()}):(0,$.jsx)(Tt,{id:`filters`,title:`Filters`,level:2,note:j?String(j):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)(wl,{label:`Filters`,filters:b.filters,format:b.format,onChange:(e,t)=>C(n=>El({...n,filters:e},t)),onRemoved:(e,n,r)=>t.offer({label:e,restore:()=>C(e=>El({...e,filters:[...n]},r))})})}),f&&a&&(0,$.jsx)(h,{kind:`Filter set`,value:e,onClose:()=>p(!1),options:a.map(e=>({id:e.id,label:e.name})),onPick:e=>{g(null),Nc(e)}}),u&&(0,$.jsx)(Ic,{taken:w(),onCreate:e=>void T(e),onClose:()=>d(!1)}),c&&y&&(0,$.jsx)(sn,{title:`Rename filter set`,value:y.name,onCommit:E,onClose:()=>l(!1)}),_&&y&&(0,$.jsx)(Ns,{subject:{kind:`filter-set`,id:e,name:y.name},scan:{pipelines:s.list},undo:t,onClose:()=>v(!1),onChanged:()=>void i.refresh()})]})}function El(e,t){let{format:n,...r}=e;return t==null?r:{...r,format:t}}function Dl({setId:e}){let t=Dt();return(0,$.jsxs)($.Fragment,{children:[e?(0,$.jsx)(Tl,{setId:e,undo:t}):(0,$.jsx)(Fc,{undo:t}),(0,$.jsx)(Qt,{undo:t.undo,onUndo:t.undoNow,onDismiss:t.dismiss})]})}var Ol=[{value:`all`,label:`All`},{value:`used`,label:`Used`},{value:`unused`,label:`Unused`}],kl=[{value:`all`,label:`All`},{value:`scenario`,label:`Used by this variant`},{value:`scenarios`,label:`Used by all variants`}];function Al(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 jl(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 Ml=e=>Be(e)||e;function Nl(e){return e.id===null?(0,$.jsx)(Pl,{onClose:e.onClose}):(0,$.jsx)(Fl,{...e,id:e.id})}function Pl({onClose:e}){let t=si(),[n,r]=(0,Y.useState)(``),[i,a]=(0,Y.useState)(``),[o,s]=(0,Y.useState)(null),[c,l]=(0,Y.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)(De,{title:`New prompt`,status:o??void 0,statusBad:!!o,focusRow:`text`,wide:!0,sections:[{key:`prompt`,rows:[{key:`name`,label:`Name`,width:`full`,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,adding:!0,saving:c,onSave:()=>void u(),onClose:e})}function Fl({id:e,onClose:t,onDuplicate:n,onDelete:r}){let i=si(),a=li(e),[o,s]=(0,Y.useState)(null),[c,l]=(0,Y.useState)(!1),{openRun:u}=$i(),[d,f]=(0,Y.useState)(null),p=Os(),m=a?.doc;if(!m)return(0,$.jsx)(De,{title:`Prompt`,sections:[{key:`loading`,children:(0,$.jsx)(Bt,{label:`Prompt`,error:a?.sync.state===`missing`?a.sync.message:null,onRetry:()=>i.load(e)})}],onClose:t});let h=e=>f(e instanceof Error?e.message:String(e)),g=e=>m.versions.find(t=>t.version===e)?.text??``,_=m.uses.some(e=>e.job>0),v=[{key:`version`,header:`Version`,role:`title`,sort:e=>e.version,cell:e=>`v${e.version}${e.version===m.version?` (newest)`:``}`},{key:`uses`,header:`Uses`,className:`num`,sort:e=>e.uses,cell:e=>e.uses}],y=e=>(0,$.jsx)(`pre`,{className:`mono-block pl-words`,children:e}),b=(e,t)=>t?`Hide words`:`Show words`,x=e=>`${e.runId}:${e.scenarioId}:${e.job}`,S=[{key:`run`,header:`Run`,role:`title`,sort:e=>e.submittedAt,cell:e=>(0,$.jsx)(`a`,{className:`pl-run`,href:Ti({tab:`results`,id:e.runId}),onClick:n=>{n.preventDefault(),t(),u(e.runId,{at:e.submittedAt,from:`history`})},children:da(e.submittedAt)})},{key:`scenario`,header:`Scenario`,sort:e=>e.scenarioName,cell:e=>e.scenarioName},..._?[{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=>Ml(e.submittedAt)}],C=[{label:`Set as default`,icon:Ar,disabled:m.default,onSelect:()=>{f(null),i.setDefault(e).catch(h)}},{label:`Duplicate`,icon:J.clone,onSelect:()=>n(m)},p({tab:`library`,section:`prompts`,id:e}),{label:`Delete`,icon:J.remove,destructive:!0,disabled:m.default,onSelect:()=>r(m)}],w=o?.name??m.name,T=o?.text??m.text,E=w!==m.name||T!==m.text,D=e=>s({name:w,text:T,...e}),O=async()=>{if(!E){t();return}l(!0),i.edit(e,{name:w,text:T});try{if(await i.flush(e),i.entry(e)?.sync.state===`conflict`){s(null),l(!1);return}t()}catch(e){h(e),l(!1)}},k=d??ni(a.sync),A=a.sync.state===`conflict`;return(0,$.jsx)(De,{title:ti({...m,name:w,text:T}),status:k??void 0,statusBad:!!k,menu:C,wide:!0,actions:A?[{label:`Keep this tab's data`,onSelect:()=>void i.resolve(e,`mine`)},{label:`Lose this tab's changes`,onSelect:()=>void i.resolve(e,`theirs`)}]:[],dirty:E,saving:c,onSave:()=>void O(),onClose:t,sections:[{key:`prompt`,rows:[{key:`name`,label:`Name`,width:`full`,control:{type:`text`,value:w,placeholder:`(First line)`,onChange:e=>D({name:e})}},{key:`version`,label:`Version`,required:!0,control:{type:`static`,text:`v${m.version}${m.default?` (Default)`:``}`}},{key:`text`,label:`Prompt`,required:!0,control:{type:`textarea`,value:T,placeholder:`None`,onChange:e=>D({text:e})}}]},{key:`versions`,title:`Versions`,children:(0,$.jsx)(Ht,{label:`Versions`,versions:[...m.versions].sort((e,t)=>t.version-e.version),rowKey:e=>String(e.version),columns:v,at:e=>e.created,atLabel:`Made`,current:e=>e.version===m.version,onRestore:t=>{f(null),i.restoreVersion(e,t.version).catch(h)},expand:{label:b,detail:e=>y(e.text)}})},{key:`uses`,title:`Uses`,children:(0,$.jsx)(W,{className:`pl-uses`,cards:!0,label:`Uses`,rows:m.uses,rowKey:x,pageSize:10,empty:`(No uses)`,defaultSort:{key:`when`,dir:`desc`},columns:S,expand:{label:b,detail:e=>y(g(e.version))}})}]})}var Il=`#new`,Ll={tab:`library`,section:`prompts`,id:null},Rl=e=>({tab:`library`,section:`prompts`,id:e});function zl({promptId:e=null}){let t=si(),{list:n,error:r}=ci(),[i]=Z(`promptlab.workflows`),a=Dt(),[o,s]=c(`prompts`,20),[l,u]=(0,Y.useState)(!1),[d,f]=(0,Y.useState)(``),[p,m]=(0,Y.useState)(`all`),[h,g]=(0,Y.useState)(!1),_=h?Il:e,v=t=>{g(t===Il),t!==Il&&t!==e&&Ei(t?Rl(t):Ll)},y=Os(),[b,S]=(0,Y.useState)(null),[C,w]=(0,Y.useState)(null),T=Ss(),E=(0,Y.useRef)(new Set);(0,Y.useEffect)(()=>{if(e&&n){if(n.some(t=>t.id===e)){E.current.add(e);return}t.entry(e)?.doc||(Di(Ll),E.current.has(e)||a.notify({label:gs(`prompt`,e,T.current)}))}},[e,n,t,a.notify,T.current]);let D=n??[],O=D.filter(e=>Al(e,d,p)),k=e=>S(e instanceof Error?e.message:String(e)),A=async()=>{S(null),v(Il)},j=async e=>{S(null);try{await t.flush(e.id);let n=await t.create({name:`${ti(e)} copy`,text:t.entry(e.id)?.doc?.text??e.text});v(n.id)}catch(e){k(e)}},M=async e=>{S(null);let n=[];try{for(let r of e)n.push(await t.remove(r.id)),_===r.id&&v(null)}catch(e){k(e)}n.length&&a.offer({label:e.length===1?`Deleted ${ti(e[0])}`:`Deleted ${n.length} prompts`,restore:()=>{for(let e of n)t.restore(e).catch(k)}})},N=e=>M([e]),P=e=>{S(null),t.setDefault(e.id).catch(k)},F=e=>[{label:`Edit…`,icon:J.edit,onSelect:()=>v(e.id)},{label:`Set as default`,icon:Ar,disabled:e.default,onSelect:()=>P(e)},{label:`Duplicate`,icon:J.clone,onSelect:()=>void j(e)},y(Rl(e.id)),{label:`Copy or move…`,icon:J.transfer,onSelect:()=>w(e)},{label:`Delete`,icon:J.remove,destructive:!0,disabled:e.default,onSelect:()=>void N(e)}];return(0,$.jsxs)(Tt,{id:`prompts`,title:`Prompt library`,note:n?`${D.length} ${D.length===1?`prompt`:`prompts`}`:void 0,actions:[{label:`Page size…`,icon:J.pageSize,onSelect:()=>u(!0)}],about:`Every version kept, with the runs that used it.${D.some(e=>e.default)?` The Default is the one a new variant starts from.`:``}`,children:[n===null?(0,$.jsx)(Bt,{label:`Prompts`,error:r,onRetry:()=>void t.refresh()}):(0,$.jsxs)($.Fragment,{children:[D.length>0&&(0,$.jsx)(xt,{noun:`prompts`,search:{label:`Search prompts`,placeholder:`Search prompts`,value:d,onChange:f},filters:[{key:`show`,label:`Show`,value:p,onChange:e=>m(e),options:Ol}],count:{shown:O.length,total:D.length}}),(0,$.jsx)(W,{className:`pl-prompts`,cards:!0,label:`Prompts`,rows:O,rowKey:e=>e.id,pageSize:o,total:D.length,empty:`(No prompts)`,rowProps:e=>({"data-default":e.default||void 0}),columns:[{key:`prompt`,header:`Prompt`,role:`title`,sort:e=>ti(e).toLowerCase(),cell:e=>(0,$.jsxs)(`button`,{type:`button`,className:`link-btn pl-open`,onClick:()=>v(e.id),children:[(0,$.jsxs)(`span`,{className:`pl-title`,children:[ti(e),e.default&&(0,$.jsx)(Ye,{tone:`amber`,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?Ml(e.lastUsed):(0,$.jsx)(`span`,{className:`unset`,children:`(Never)`})},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)($e,{label:`${ti(e)} actions`,actions:F(e)})}],select:{noun:`prompt`,label:ti,actions:e=>[{label:`Delete`,icon:J.remove,destructive:!0,disabled:e.every(e=>e.default),onSelect:()=>void M(e.filter(e=>!e.default))}]}})]}),b&&(0,$.jsx)(pt,{tone:`error`,children:b}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>void A(),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add prompt`]}),_&&(0,$.jsx)(Nl,{id:_===Il?null:_,onClose:()=>v(null),onDuplicate:e=>void j(e),onDelete:e=>void N(e)}),l&&(0,$.jsx)(x,{value:o,onChange:s,onClose:()=>u(!1)}),C&&(0,$.jsx)(Ns,{subject:{kind:`prompt`,id:C.id,name:ti(C)},scan:{pipelines:i.list},undo:a,onClose:()=>w(null),onChanged:()=>void t.refresh()}),(0,$.jsx)(Qt,{undo:a.undo,notice:a.notice,onUndo:a.undoNow,onDismiss:a.dismiss})]})}var Bl=[{value:`name`,label:`Name`,dir:1},{value:`added`,label:`Newest`,dir:-1},{value:`size`,label:`Largest`,dir:-1},{value:`type`,label:`Type`,dir:1}],Vl=`evals-lab.sources`,Hl={view:`thumbs`,sort:`name`,dir:1};function Ul(){try{let e=window.localStorage.getItem(Vl);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===`type`?t.sort:`name`,dir:t.dir===-1?-1:1}}}catch{}return Hl}function Wl(e){try{window.localStorage.setItem(Vl,JSON.stringify(e))}catch{}}var Gl=e=>{let t=e.lastIndexOf(`.`);return t<0?``:e.slice(t).toLowerCase()},Kl=[`.heic`,`.heif`,`.dng`,`.tif`,`.tiff`],ql=[`.jpg`,`.jpeg`,`.png`],Jl=e=>ql.includes(Gl(e))||Kl.includes(Gl(e)),Yl=(e,t)=>`/api/sources/${encodeURIComponent(e)}/thumbs/${encodeURIComponent(t.name)}${t.v?`?v=${t.v}`:``}`,Xl=(e,t)=>Kl.includes(Gl(t.name))?`/api/sources/${encodeURIComponent(e)}/zoom/${encodeURIComponent(t.name)}`:`/api/sources/${encodeURIComponent(e)}/files/${encodeURIComponent(t.name)}`;function Zl(e,t,n){let r=[...e],i=(e,t)=>e.name.localeCompare(t.name,void 0,{numeric:!0,sensitivity:`base`});return r.sort((e,r)=>{let a;return a=t===`size`?e.bytes-r.bytes:t===`added`?(e.added??0)-(r.added??0):t===`type`&&Gl(e.name).localeCompare(Gl(r.name))||i(e,r),a*n}),r}function Ql(e,t){let n=t.trim().toLowerCase();return n?e.filter(e=>e.name.toLowerCase().includes(n)):[...e]}function $l(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]}`}var eu=e=>ht(e*1e3);function tu(e){let t=e.platform??e.config?.platform,n=e.provider??e.config?.provider;return{id:e.id,name:e.name,system:e.system,type:e.type,...t?{platform:t}:{},...n?{provider:n}:{},files:e.files.length,bytes:e.bytes}}var nu=`[redacted]`,ru=/^(?:api[-_]?key|x-api-key|authorization|ocp-apim-subscription-key|password|secret|client[-_]?secret|token|access[-_]?token|refresh[-_]?token|sig)$/i,iu=/^(?:headers|authentication)$/,au=[[/sk-ant-[A-Za-z0-9_-]+/g,nu],[/sk-[A-Za-z0-9_-]{20,}/g,nu],[/Bearer\s+[A-Za-z0-9._~+/=-]+/g,`Bearer ${nu}`],[/eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,nu],[/([?&](?:sig|code)=)[^&"\s]+/g,`$1${nu}`]];function ou(e){let t=0,n=e=>{let n=e;for(let[e,r]of au)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))iu.test(i)&&a&&typeof a==`object`&&!Array.isArray(a)?n[i]=Object.fromEntries(Object.keys(a).map(e=>(t++,[e,nu]))):iu.test(i)&&typeof a==`string`||ru.test(i)&&a!=null&&typeof a!=`object`?(t++,n[i]=nu):n[i]=r(a);return n}return typeof e==`string`?n(e):e};return{value:r(e),count:t}}var su={import:`evals-lab.flow.importing`,resync:`evals-lab.flow.resyncing`},cu=`resume`;function lu(e){try{return sessionStorage.getItem(e)}catch{return null}}function uu(e,t){try{t?sessionStorage.setItem(e,t):sessionStorage.removeItem(e)}catch{}}function du(e,t){uu(su[e],t)}function fu(e,t){let n=lu(su[e]);return n!==null&&uu(su[e],null),n===t}function pu(e){let t=new URL(e);for(let e of Object.keys(su)){let n=lu(su[e]);n&&t.searchParams.append(cu,`${e}:${n}`)}return t.href}function mu(e){for(let t of e.searchParams.getAll(cu)){let e=t.indexOf(`:`),n=t.slice(0,e);e>0&&Object.hasOwn(su,n)&&uu(su[n],t.slice(e+1))}e.searchParams.delete(cu)}var hu=[`https://service.flow.microsoft.com//.default`];function gu(e){let t=e.trim();return!t||t.toLowerCase()===`organizations`?`Any organisation`:`${t} only`}var _u=null;function vu(e){let t=`${e.clientId}/${e.tenant}`;return _u?.key!==t&&(_u={key:t,pca:jn(()=>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}),[])}),_u.pca}var yu=()=>{let e=new URLSearchParams(window.location.search);return e.has(`state`)&&(e.has(`code`)||e.has(`error`))};async function bu(e){await vu(e)}async function xu(e){let t=await vu(e);return(t.getActiveAccount()??t.getAllAccounts()[0]??null)?.username??null}var Su=`signin`;function Cu(e=window.location.href){let t=new URL(e);return t.hostname===`127.0.0.1`?(t.hostname=`localhost`,t.searchParams.set(Su,`microsoft`),t.href):null}function wu(){let e=new URL(window.location.href);return e.searchParams.get(`signin`)===`microsoft`&&(e.searchParams.delete(Su),mu(e),window.history.replaceState(window.history.state,``,e.href),!0)}async function Tu(e,t=window.location.href){let n=Cu();if(n){window.location.assign(pu(n));return}await(await vu(e)).loginRedirect({scopes:hu,prompt:`select_account`,redirectStartPage:t})}async function Eu(e){let t=await vu(e),n=t.getActiveAccount()??t.getAllAccounts()[0];n&&await t.clearCache({account:n})}async function Du(e){let t=await vu(e),n=t.getActiveAccount()??t.getAllAccounts()[0];if(!n)throw Error(`sign in to Microsoft first`);try{return(await t.acquireTokenSilent({scopes:hu,account:n})).accessToken}catch(e){let{InteractionRequiredAuthError:r}=await jn(async()=>{let{InteractionRequiredAuthError:e}=await import(`./dist-DI3ewZYj.js`);return{InteractionRequiredAuthError:e}},[]);throw e instanceof r&&await t.acquireTokenRedirect({scopes:hu,account:n,redirectStartPage:window.location.href}),e}}var Ou=new EventTarget,ku=()=>Ou.dispatchEvent(new Event(`change`));function Au(){let e=X(),[t,n]=(0,Y.useState)({cfg:void 0,editable:!1});return(0,Y.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(),Ou.addEventListener(`change`,r),()=>{t=!1,Ou.removeEventListener(`change`,r)}},[e]),t}var ju=()=>Au().cfg,Mu=e=>on(()=>Du(e));async function Nu(e,t,n){let r=await e.flow(t,n);return{name:r.name,definition:ou(r.definition).value}}async function Pu(e,t,n,r,i){let a=await e.run(t,n,r),o=ot(i),s=new Map,c=new Map,l=t=>t?(c.has(t)||c.set(t,e.content(t)),c.get(t)):Promise.resolve(null),u=i=>(s.has(i)||s.set(i,e.repetitions(t,n,r,i)),s.get(i)),d=async(e,t)=>{let n=o[e]?.loops??[];return n.length?(await u(e)).find(e=>n.every(n=>!(n in t)||e.at[n]===t[n]))??null:a.actions[e]??null},f=e=>e.status!==`Skipped`&&(e.inputsLink||e.outputsLink||e.error),p=async e=>({run:r,at:e.at,startTime:e.startTime,inputs:await l(e.inputsLink),outputs:await l(e.outputsLink),error:e.error});return{detail:a,triggerOutputs:()=>l(a.trigger?.outputsLink??null),actionInputs:async(e,t)=>l((await d(e,t))?.inputsLink??null),actionOutputs:async(e,t)=>l((await d(e,t))?.outputsLink??null),callsOf:async e=>{let t=e.loops.length?await u(e.name):[a.actions[e.name]].filter(e=>!!e);return Promise.all(t.filter(f).map(p))}}}async function Fu(e,t,n,r,i,a){if(!r.length)return[];let o=(await e.runs(t,n)).slice(0,a).map(e=>e.id);if(!o.length)return[];let s=new Set,c=[];for(let a of r){let{records:r}=await Iu(e,t,n,o,a,i,s);for(let e of r)s.add(xn(e)),c.push(e)}return c}async function Iu(e,t,n,r,i,a,o,s){let c=new Set(o),l=[],u=0,d=0;for(let o of r){let r=await Pu(e,t,n,o,a);for(let e of await r.callsOf(i)){let t=ou(await un(i,e,r,a)).value,n=xn(t);if(c.has(n)){u++;continue}c.add(n),l.push(t)}s?.(++d)}return{records:l,skipped:u}}var Lu=5;function Ru({typeRow:e,initial:t,onMade:n,bind:r}){let i=X(),a=ju(),o=(0,Y.useMemo)(()=>a?Mu(a):null,[a]),[s,c]=(0,Y.useState)(void 0),[l,u]=(0,Y.useState)(null),d=t?.config??{},[f,p]=(0,Y.useState)(``),[m,h]=(0,Y.useState)(null),[g,_]=(0,Y.useState)(``),[v,y]=(0,Y.useState)(t?.name??``),[b,x]=(0,Y.useState)(!1),[S,C]=(0,Y.useState)(null),T=e=>C(e instanceof Error?e.message:String(e)),[E,D]=(0,Y.useState)(null),[O,k]=(0,Y.useState)(null),A=e=>e instanceof Error?e.message:String(e);(0,Y.useEffect)(()=>{a&&xu(a).then(c,()=>c(null))},[a]),(0,Y.useEffect)(()=>{o&&s&&(D(null),o.environments().then(e=>{u(e),p((e.find(e=>e.id===d.environment)??e.find(e=>e.isDefault)??e[0])?.id??``)},e=>D(A(e))))},[o,s,d.environment]),(0,Y.useEffect)(()=>{o&&f&&(h(null),k(null),_(``),o.flows(f).then(e=>{if(h(e),t){_(e.find(e=>e.id===d.flow)?.id??``);return}_(e[0]?.id??``),y(e[0]?.name??``)},e=>k(A(e))))},[o,f,t,d.flow]);let j=!!o&&!!s,M=!!t&&j&&(g!==d.flow||f!==d.environment),N=!!t&&v.trim()!==t.name,P=M||N,F=!!v.trim()&&(t&&!j?!0:j&&!!g),I=(0,Y.useRef)(async()=>{});(0,Y.useEffect)(()=>{r({enabled:F&&!b,busy:b,changed:P,run:()=>void I.current()})},[r,F,b,P]),(0,Y.useEffect)(()=>()=>r(null),[r]);let ee=t?t.name:`New Source`,L={key:`name`,label:`Name`,width:`full`,control:{type:`text`,value:v,onChange:y}},te=(0,$.jsx)(w,{label:ee,rows:t?[e,L]:[e]}),R=async(e,t,n)=>{try{for(let r of await Fu(e,f,g,me(n),n,Lu))try{await i.upload(t,Ke(r),new Blob([JSON.stringify(r,null,2)],{type:`application/json`}))}catch{}}catch{}},ne=()=>{let e=l?.find(e=>e.id===f)?.name;return{platform:`power-automate`,environment:f,...e?{environmentName:e}:{},flow:g,displayName:m?.find(e=>e.id===g)?.name??g}},re=async e=>{if(!P){await n(e);return}x(!0),C(null);try{let t=M&&o?await Nu(o,f,g):null,r=await i.updateSource(e.id,{...N?{name:v.trim()}:{},...t?{config:ne()}:{}});t&&await i.saveDefinition(e.id,(await i.definition(e.id)).version,t.definition),await n(r)}catch(e){T(e instanceof Ln?Error(e.message):e)}finally{x(!1)}};return I.current=async()=>{if(!F)return;if(t){await re(t);return}if(!o)return;x(!0),C(null);let e=null;try{let t=await i.createSource(v.trim(),`workflow`,ne());e=t.id;let r=await Nu(o,f,g);await i.saveDefinition(t.id,0,r.definition),R(o,t.id,r.definition),await n(t)}catch(t){e&&await i.removeSource(e).catch(()=>{}),T(t instanceof Ln?Error(t.message):t)}finally{x(!1)}},a===void 0||a&&s===void 0?(0,$.jsxs)($.Fragment,{children:[te,(0,$.jsx)(Bt,{label:`Account`})]}):a===null?(0,$.jsxs)($.Fragment,{children:[te,(0,$.jsxs)(`p`,{className:`note`,children:[`(Microsoft sign-in not set up) `,(0,$.jsxs)(`a`,{href:`#/setup`,children:[`Open`,`\xA0`,`Setup`]})]})]}):s?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(w,{label:ee,rows:[e,{key:`account`,label:`Account`,control:{type:`static`,text:s}},{key:`env`,label:`Environment`,error:E??void 0,control:l?{type:`picker`,kind:`Environment`,value:f,options:l.map(e=>({id:e.id,label:e.name})),onChange:p}:{type:`static`,text:E?`(No environment)`:`(Loading)`,none:!0}},{key:`flow`,label:`Flow`,error:O??void 0,control:m?{type:`picker`,kind:`Flow`,value:g,placeholder:`(Not set)`,options:m.map(e=>({id:e.id,label:e.name})),onChange:e=>{_(e),t||y(m.find(t=>t.id===e)?.name??``)}}:{type:`static`,text:f&&!O?`(Loading)`:`(Not set)`,none:!0}},L]}),m&&!m.length&&(0,$.jsx)(`p`,{className:`unset`,children:`(No flows)`}),S&&(0,$.jsx)(pt,{tone:`error`,children:S})]}):(0,$.jsxs)($.Fragment,{children:[te,(0,$.jsx)(`div`,{className:`sacts`,children:(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,onClick:()=>void Tu(a).catch(T),children:[(0,$.jsx)(mr,{size:14,"aria-hidden":`true`}),` Sign in with Microsoft`]})}),S&&(0,$.jsx)(pt,{tone:`error`,children:S})]})}function zu(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}}var Bu=e=>{try{return new URL(/^https?:\/\//i.test(e)?e:`https://`+e).host}catch{return null}},Vu=e=>(K().CONNECTION_TYPES[e??``]?.answers??[`prompt`]).includes(`request`);function Hu(e,t){let n=Bu(e);return n&&t.find(e=>Vu(e.type)&&Bu(K().addressOf(e))===n)||null}function Uu(e,t){let n=K(),r=n.httpApiFor(e);if(r!==`raw`)return r;let i=Hu(e,t);return i&&n.CONNECTION_TYPES[i.type]?.speaks?.(i)||`raw`}function Wu(e,t){let n=Hu(e,t)??t.find(e=>Vu(e.type));return n?{id:n.id,name:n.name??``}:{id:`http-endpoint`,name:`HTTP endpoint`}}function Gu(e){let t=Bu(e),n=Object.values(K().CONNECTION_TYPES).filter(e=>e.picker!==!1&&Vu(e.id)),r=n.find(e=>e.url&&Bu(e.url)===t);if(r)return{type:r.id,name:r.label,url:``};let i=n.find(e=>!e.url)??n[0],a=(()=>{try{return new URL(e).origin}catch{return``}})();return{type:i.id,name:i.label,url:a}}function Ku(e,t){let n=ze(e,t.name),r=n?ut(e,n.name):[],i=K();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 qu(e,t,n,r,i){let a=K(),o=n.template,s=String(o.uri??``),c=Uu(s,r),l={type:`httpRequest`,method:String(o.method??`POST`).toUpperCase(),...zu(s),body:o.body??null,...a.HTTP_APIS[c].cellOf(o.body??null)},u=ze(t,n.name),d=Wu(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?.()??[]}),p=[{id:a.newId(),name:`Recorded`,connection:null,profile:null,steps:[{type:`recorded`,prompt:``}]},{id:a.newId(),name:`Draft`,connection:d,profile:null,steps:[structuredClone(l)]}];return{version:a.PIPELINE_VERSION,id:a.newId(),name:`${e.name}, ${n.name}`,...a.serialize([f],p),evals:[a.newLink(i)],pass:{mode:`all`}}}function Ju(e,t,n){let r=e.result;if(!r||!(`status`in r))return null;let i=me(t).find(t=>t.name===e.step);if(!i)return null;let a=String(i.template.uri??``),o={step:i.name,loop:i.loops.at(-1)??null,api:Uu(a,n),readAs:ze(t,i.name)?.expression??null};try{return K().httpReplyOf(o,{prompt:``},r.body,r.status,{scope:e.scope,now:e.at}).raw}catch{return null}}function Yu(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 Xu=e=>e.replace(/\.json$/i,``).replace(/[^A-Za-z0-9._-]/g,`_`);function Zu(e,t,n){let r=[],i=0;for(let{name:a,record:o}of e){if(Ju(o,t,n)==null){i++;continue}r.push({id:Xu(a),item:a,todo:!1,note:``,metrics:[]})}return{cases:r,skipped:i}}function Qu(e){return e.map(e=>({id:e.replace(/\.[a-z0-9]+$/i,``).replace(/[^A-Za-z0-9._-]/g,`_`),item:e,todo:!0,note:``,metrics:[]}))}function $u(e,t,n){let r=[],i=0;for(let{name:a,record:o}of e){let e=Ju(o,t,n);if(e==null){i++;continue}let s=ze(t,o.step),c=s?ut(t,s.name):[];r.push({id:a.replace(/\.json$/i,``).replace(/[^A-Za-z0-9._-]/g,`_`),item:a,todo:!0,note:``,metrics:Yu(e,c)})}return{cases:r,skipped:i}}var ed={fetched:`Fetched`,recorded:`Recorded`,uploaded:`Uploaded`,added:`Added`,edited:`Edited`,copied:`Copied`},td=e=>e?[ed[e.kind]??e.kind,e.at?Be(e.at):``].filter(Boolean).join(` `):``;function nd({status:e,title:t}){return e==null?(0,$.jsx)(`span`,{className:`unset`,children:`(n/a)`}):(0,$.jsxs)(`span`,{className:`sresult`,title:t,children:[(0,$.jsx)(Xt,{pass:e>=200&&e<300,decorative:!0}),e]})}function rd(e,t){return{key:`history`,title:`History`,children:(0,$.jsx)(`div`,{className:`sflow sresponses`,children:(0,$.jsx)(W,{className:`slist`,cards:!0,label:`History`,rows:[...e],rowKey:e=>String(e.n),defaultSort:null,columns:[{key:`updated`,header:`Updated`,role:`title`,sort:e=>e.at,cell:e=>td(e)},{key:`status`,header:`Status`,sort:e=>e.status,cell:e=>(0,$.jsx)(nd,{status:e.status})},{key:`act`,header:``,className:`s-actions`,role:`menu`,sort:null,cell:e=>e.current?(0,$.jsx)(Ye,{children:`Current`}):(0,$.jsx)(an,{actions:[{label:`Restore`,icon:Sr,onSelect:()=>t(e.n)}]})}]})})}}function id({name:e,facts:t,sections:n,changed:r,valid:i,taken:a,menu:o,onSave:s,onClose:c}){let l=e.lastIndexOf(`.`),u=l>0?e.slice(l):``,d=e.slice(0,e.length-u.length),[f,p]=(0,Y.useState)(d),[m,h]=(0,Y.useState)(),[g,_]=(0,Y.useState)(),[v,y]=(0,Y.useState)(!1),b=async()=>{let t=f.trim(),n=t+u;if(!t)return h(`An item needs a name.`);if(n!==e&&a(n))return h(`${n} is taken.`);if(!i||i()){if(n===e&&!r)return c();y(!0),_(void 0);try{await s(n),c()}catch(e){e instanceof Ln&&e.body?.field===`name`?h(Q(e)):_(Q(e)),y(!1)}}};return(0,$.jsx)(De,{title:e,menu:o,wide:!0,sections:[{key:`call`,rows:s?[{key:`name`,label:`Name`,width:`full`,control:{type:`text`,value:f,suffix:u,onChange:e=>{p(e),h(void 0)}},error:m},...t]:t},...n??[]],...s?{onSave:()=>void b(),saving:v,dirty:f!==d||!!r,refusal:g}:{},onClose:c})}function ad(e){let[t]=Z(`promptlab.workflows`);return(0,Y.useMemo)(()=>t.list.filter(t=>{let n=K().contentOf(t.work);return n?.type===`source`&&n.ref.id===e}),[t.list,e])}var od=e=>Ti({tab:`runs`,id:e});function sd(e,t){let n=Qr(),{list:r}=$r(),i=(0,Y.useMemo)(()=>[...new Set([...t.flatMap(e=>{let t=K().evalsDataset(e.work);return t?.id?[t.id]:[]}),...(r??[]).filter(t=>t.preview?.id===e).map(e=>e.id)])],[t,r,e]);(0,Y.useEffect)(()=>{for(let e of i)n.load(e)},[n,i]);let a=(0,Y.useSyncExternalStore)(n.subscribe,n.held);return(0,Y.useMemo)(()=>{let e=new Map,t=new Map;for(let n of i){let r=a.find(e=>e.id===n)?.doc;if(!r)continue;let i=new Set(r.body.cases.flatMap(e=>e.item?[e.item]:[]));e.set(n,i);for(let e of i)t.has(e)||t.set(e,{id:r.id,name:r.name})}return{caseOf:e=>t.get(e)??null,casesIn:t=>t&&e.get(t)||new Set}},[i,a])}function cd({group:e}){return e?(0,$.jsx)(`a`,{href:Ti({tab:`library`,section:`datasets`,id:e.id}),children:e.name}):(0,$.jsx)(`span`,{className:`unset`,children:`(No eval case)`})}var ld=e=>e?{type:`static`,text:e.name,href:Ti({tab:`library`,section:`datasets`,id:e.id})}:{type:`static`,text:`(No eval case)`,none:!0};function ud({pipelines:e,columns:t,itemsOf:n,casesIn:r,footer:i}){let a=e=>{let t=n(e),i=r(K().evalsDataset(e)?.id);return{n:t.filter(e=>i.has(e)).length,of:t.length}};return(0,$.jsxs)(Tt,{id:`source-pipelines`,title:`Experiments`,note:String(e.length),children:[e.length?(0,$.jsx)(`div`,{className:`sflow`,children:(0,$.jsx)(W,{className:`slist`,cards:!0,label:`Experiments`,rows:[...e],rowKey:e=>e.id,columns:[{key:`name`,header:`Experiment`,role:`title`,sort:e=>e.name,cell:e=>(0,$.jsx)(`a`,{href:od(e.id),children:e.name})},...t.map(e=>({key:e.key,header:e.header,sort:e.sort?t=>e.sort(t.work):null,cell:t=>e.cell(t.work)})),{key:`graded`,header:`Graded`,sort:e=>a(e.work).n,cell:e=>{let t=a(e.work);return`${t.n} of ${t.of}`}}]})}):(0,$.jsx)(`p`,{className:`note`,children:`(No experiments)`}),i]})}var dd=e=>e.name.toLowerCase().endsWith(`.json`),fd=e=>new Promise((t,n)=>{let r=new FileReader;r.onload=()=>t(String(r.result)),r.onerror=()=>n(r.error),r.readAsText(e)});function pd({source:e,definition:t,cfg:n}){let r=X(),i=Qr(),[a]=Z(`promptlab.connections`),o=Dt(),[c,l]=(0,Y.useState)(e.files.filter(dd)),[f,p]=(0,Y.useState)({}),[m,h]=(0,Y.useState)({}),[g,_]=(0,Y.useState)(null),[v,y]=(0,Y.useState)(()=>fu(`import`,e.id)?`import`:null),[b,x]=(0,Y.useState)(null),S=(0,Y.useMemo)(()=>me(t),[t]),[C,w]=(0,Y.useState)(!1),[T,E]=(0,Y.useState)(null),D=(0,Y.useCallback)(async()=>{let[t,n]=await Promise.all([r.source(e.id),r.sourceResponses(e.id)]),i=t.files.filter(dd);l(i);let a=await Promise.all(i.map(async t=>{try{return[t.name,d(JSON.parse(await fd(await r.file(e.id,t.name))))]}catch{return[t.name,null]}}));p(Object.fromEntries(a)),h(n.responses)},[r,e.id]),O=(0,Y.useCallback)(()=>{E(null),D().then(()=>w(!0),e=>E(Q(e)))},[D]);(0,Y.useEffect)(O,[O]);let k=e=>{let n=f[e],r=n?S.find(e=>e.name===n.step):void 0;return n&&r?mt(r,n,t):null},A=(0,Y.useMemo)(()=>new Set(c.flatMap(e=>f[e.name]?[xn(f[e.name])]:[])),[c,f]),j=async(t,n=0)=>{let i=0,a=[];for(let o of t)try{(await r.upload(e.id,Ke(o),new Blob([JSON.stringify(o,null,2)],{type:`application/json`}))).unchanged?n++:i++}catch(e){a.push(Q(e))}await D();let o=[`Added ${i}`,n?`Skipped ${n}`:``,a.length?`Refused ${a.length}: ${a[0]}`:``].filter(Boolean).join(`, `);_(a.length?Re(o):ln(o))},N=async()=>{let{cases:n,skipped:r}=$u(c.flatMap(e=>f[e.name]?[{name:e.name,record:f[e.name]}]:[]),t,a.list);if(!n.length)return _(ln(`No item holds a production response.`));try{let t=await i.create(`${e.name} · production`,{...Rr(),cases:n},{id:e.id,name:e.name});_(ln([`Made ${t.name}: ${n.length} ${n.length===1?`eval case`:`eval cases`}`,r?`Skipped ${r}`:``].filter(Boolean).join(`, `)))}catch(e){_(Re(Q(e)))}},P=async t=>{try{let{trash:n}=await r.removeFiles(e.id,t);l(e=>e.filter(e=>!t.includes(e.name))),o.offer({label:t.length===1?`Removed ${t[0]}`:`Removed ${t.length} items`,restore:()=>void r.restoreTrash(n).then(D,e=>_(Re(Q(e))))})}catch(e){_(Re(Q(e)))}},F=async t=>{let n=URL.createObjectURL(await r.file(e.id,t)),i=document.createElement(`a`);i.href=n,i.download=t,document.body.append(i),i.click(),i.remove(),URL.revokeObjectURL(n)},I=async t=>{try{let{call:n}=await r.cloneCall(e.id,t);l(e=>e.flatMap(e=>e.name===t?[e,{...e,name:n.name}]:[e])),p(e=>({...e,[n.name]:e[t]??null})),o.offer({label:`Cloned ${t}`,restore:()=>void r.removeFiles(e.id,[n.name]).then(()=>l(e=>e.filter(e=>e.name!==n.name)),e=>_(Re(Q(e))))})}catch(e){_(Re(Q(e)))}},ee=async(t,n,i)=>{let a=i?JSON.stringify(ou(i).value,null,2):void 0,{call:s}=await r.editCall(e.id,t,{...n===t?{}:{to:n},...a===void 0?{}:{content:a}});await D(),o.offer({label:a===void 0?`Renamed ${t}`:`Edited ${t}`,restore:async()=>{s.n!=null&&s.was!=null&&await r.restoreResponse(e.id,s.name,s.was,s.n),s.name!==t&&await r.editCall(e.id,s.name,{to:t}),await D()}})},L=async(t,n)=>{let i=(m[t]??[]).find(e=>e.current)?.n;try{await r.restoreResponse(e.id,t,n),await D(),x({name:t,seq:Date.now()}),i!=null&&o.offer({label:`Restored ${t}`,restore:()=>r.restoreResponse(e.id,t,i).then(D)})}catch(e){_(Re(Q(e)))}},te=(e,t)=>[...t?[]:[{label:`Edit…`,icon:J.edit,disabled:!f[e],onSelect:()=>x({name:e,seq:Date.now()})}],{label:`Clone`,icon:J.clone,onSelect:()=>{t&&x(null),I(e)}},{label:`Download`,icon:M,onSelect:()=>void F(e).catch(e=>_(Re(Q(e))))},{label:`Remove`,icon:ge,destructive:!0,onSelect:()=>{t&&x(null),P([e])}}],R=(0,Y.useCallback)(()=>y(null),[]),re=(0,Y.useCallback)(()=>x(null),[]),z=b?f[b.name]:null,B=ad(e.id),{caseOf:V,casesIn:H}=sd(e.id,B),ie=S.length<=1,U=e=>(m[e]??[]).find(e=>e.current)??null;return(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(Tt,{id:`flow-calls`,title:`Items`,note:String(c.length),actions:[{label:`Import runs…`,icon:s,disabled:!S.length||!n,onSelect:()=>y(`import`)},{label:`Upload…`,icon:u,disabled:!S.length,onSelect:()=>y(`upload`)},{label:`New eval`,icon:Me,disabled:!c.length,onSelect:()=>void N()}],children:[(0,$.jsx)(lt,{said:g}),C?c.length?(0,$.jsx)(`div`,{className:`sflow`,children:(0,$.jsx)(W,{className:`slist`,cards:!0,label:`Items`,rows:c,rowKey:e=>e.name,select:{noun:`item`,actions:e=>[{label:`Download`,icon:M,onSelect:()=>void(async()=>{for(let t of e)await F(t.name)})().catch(e=>_(Re(Q(e))))},{label:`Remove`,icon:ge,destructive:!0,onSelect:()=>void P(e.map(e=>e.name))}]},columns:[{key:`reads`,header:`Reads`,role:`title`,sort:e=>vd(f[e.name])||e.name,cell:e=>f[e.name]?(0,$.jsx)(`button`,{type:`button`,className:`link-btn sopen`,onClick:()=>x({name:e.name,seq:Date.now()}),children:vd(f[e.name])||e.name}):`Unreadable`},...ie?[]:[{key:`action`,header:`Action`,sort:e=>f[e.name]?.step??``,cell:e=>f[e.name]?.step??(0,$.jsx)(`span`,{className:`unset`,children:`(No step)`})}],{key:`updated`,header:`Updated`,sort:e=>U(e.name)?.at??``,cell:e=>td(U(e.name))||(0,$.jsx)(`span`,{className:`unset`,children:`(Never)`})},{key:`reply`,header:`Response`,sort:e=>_d(f[e.name])?.code??``,cell:e=>{let t=_d(f[e.name]);return f[e.name]?t?(0,$.jsxs)(`span`,{className:`sresult`,title:t.title,children:[(0,$.jsx)(Xt,{pass:t.ok,decorative:!0}),t.code]}):(0,$.jsx)(nd,{status:null}):(0,$.jsx)(`span`,{className:`unset`,children:`(No record)`})}},{key:`request`,header:`Request`,sort:e=>gd(f[e.name],k(e.name)),tdProps:e=>{let t=k(e.name);return t&&t.match===!1?{title:t.where}:{}},cell:e=>{let t=gd(f[e.name],k(e.name));return t===md||t===hd?(0,$.jsx)(`span`,{className:`unset`,children:t}):t}},{key:`case`,header:`Eval Case`,sort:e=>V(e.name)?.name??``,cell:e=>(0,$.jsx)(cd,{group:V(e.name)})},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(an,{label:`${e.name} actions`,actions:te(e.name,!1)})}]})}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`p`,{className:`unset`,children:`(No items)`}),!!S.length&&n&&(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,onClick:()=>y(`import`),children:[(0,$.jsx)(s,{size:14,"aria-hidden":`true`}),` Import runs…`]})]}):(0,$.jsx)(Bt,{label:`Items`,error:T,onRetry:O}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,disabled:!S.length,onClick:()=>y(`hand`),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),` Add item`]})]}),v===`import`&&n&&(0,$.jsx)(Td,{cfg:n,source:e,steps:S,definition:t,held:A,onClose:R,onRead:async(e,t)=>{y(null),await j(e,t)}}),v===`hand`&&(0,$.jsx)(Ed,{steps:S,onClose:R,onAdd:async e=>{y(null),await j([e])}}),v===`upload`&&(0,$.jsx)(Od,{steps:S,onClose:R,onRead:async e=>{y(null),await j(e)}}),(0,$.jsx)(ud,{pipelines:B,casesIn:H,itemsOf:e=>{let t=K().flowStepOf(K().jobsOf(e)[0])?.step;return c.filter(e=>!t||f[e.name]?.step===t).map(e=>e.name)},columns:[{key:`action`,header:`Action`,cell:e=>K().flowStepOf(K().jobsOf(e)[0])?.step??(0,$.jsx)(`span`,{className:`unset`,children:`(No action)`})},{key:`targets`,header:`Variants`,cell:e=>K().targetsOf(e).map((t,n)=>K().targetLabel(e,n)).join(`, `)||(0,$.jsx)(`span`,{className:`unset`,children:`(No variants)`})}]}),b&&z&&(0,$.jsx)(wd,{name:b.name,record:z,menu:te(b.name,!0),caseOf:V(b.name),responses:m[b.name]??[],onRestore:e=>void L(b.name,e),taken:e=>c.some(t=>t.name===e),onSave:(e,t)=>ee(b.name,e,t),onClose:re},b.seq),(0,$.jsx)(Qt,{undo:o.undo,onUndo:o.undoNow,onDismiss:o.dismiss})]})}var md=`(n/a)`,hd=`(Not compared)`,gd=(e,t)=>e&&!e.request?md:!t||t.match===null?hd:t.match?`Matches`:`Differs`;function _d(e){let t=e?.result;return t?`error`in t?{ok:!1,code:t.error.code||`Error`,...t.error.message?{title:t.error.message}:{}}:{ok:t.status>=200&&t.status<300,code:String(t.status)}:null}function vd(e){if(!e)return``;let t=e=>{let t=Sd(e).replace(/\s+/g,` `).trim();return t.length>60?`${t.slice(0,59)}…`:t};return xd(e).map(e=>`${e.label}: ${t(e.value)}`).join(`, `)}var yd=e=>e.map(e=>({value:e.name,label:e.name})),bd=e=>e.from===`trigger`?`Trigger body`:Vt(e);function xd(e){let t=e.scope;return[...t.trigger?[{key:`trigger`,label:bd({from:`trigger`}),value:t.trigger.body,put:(e,t)=>{e.scope.trigger={...e.scope.trigger,body:t}}}]:[],...Object.entries(t.actions??{}).map(([e,t])=>({key:`action-${e}`,label:e,value:t.body,put:(t,n)=>{t.scope.actions[e]={...t.scope.actions[e],body:n}}})),...Object.entries(t.variables??{}).map(([e,t])=>({key:`variable-${e}`,label:e,value:t,put:(t,n)=>{t.scope.variables[e]=n}})),...Object.entries(t.items??{}).map(([e,t])=>({key:`item-${e}`,label:e,value:t,put:(t,n)=>{t.scope.items[e]=n}}))]}var Sd=e=>typeof e==`string`?e:e==null?``:JSON.stringify(e,null,2),Cd=e=>{if(e===``)return null;try{return JSON.parse(e)}catch{return e}};function wd({name:e,record:t,menu:n,caseOf:r,responses:i,onRestore:a,taken:o,onSave:s,onClose:c}){let l=(0,Y.useMemo)(()=>xd(t),[t]),u=t.result,d=(0,Y.useMemo)(()=>({...Object.fromEntries(l.map(e=>[e.key,Sd(e.value)])),...u&&`status`in u?{status:String(u.status),body:Sd(u.body)}:{},...u&&`error`in u?{code:u.error.code??``,message:u.error.message??``}:{}}),[l,u]),[f,p]=(0,Y.useState)(d),[m,h]=(0,Y.useState)(),g=Object.keys(d).filter(e=>f[e]!==d[e]),_=(e,t,n=!0)=>({key:e,label:t,control:{type:`textarea`,mono:n,value:f[e]??``,onChange:t=>p(n=>({...n,[e]:t}))}}),v=async e=>{if(!g.length)return s(e,null);let n=structuredClone(t);for(let e of l)g.includes(e.key)&&e.put(n,Cd(f[e.key]));u&&`status`in u?n.result={status:Number(f.status),body:g.includes(`body`)?Cd(f.body):u.body}:u&&`error`in u&&(n.result={error:{...u.error,code:f.code,message:f.message}});let{run:r,iteration:i}=t.origin;return n.origin={kind:`edited`,...r?{run:r}:{},...i?{iteration:i}:{}},s(e,n)},y=()=>!(u&&`status`in u)||/^\d+$/.test(f.status.trim())?!0:(h(`A status is a whole number.`),!1),b=t.request?`${t.request.method} ${t.request.uri}`:null,x=[{key:`action`,label:`Action`,control:{type:`static`,text:t.step}},...b?[{key:`request`,label:`Request`,control:{type:`code`,text:b}}]:[],{key:`case`,label:`Eval case`,control:ld(r)}],S=[...l.length?[{key:`reads`,title:`Reads`,rows:l.map(e=>_(e.key,e.label))}]:[],...u&&`status`in u?[{key:`reply`,title:`Response`,rows:[{key:`status`,label:`Status`,width:`quarter`,error:m,control:{type:`text`,numeric:!0,value:f.status??``,onChange:e=>{h(void 0),p(t=>({...t,status:e}))}}},_(`body`,`Body`)]}]:[],...u&&`error`in u?[{key:`reply`,title:`Response`,rows:[{key:`code`,label:`Code`,control:{type:`text`,value:f.code??``,onChange:e=>p(t=>({...t,code:e}))}},_(`message`,`Message`,!1)]}]:[],...i.length?[rd(i,a)]:[]];return(0,$.jsx)(id,{name:e,facts:x,sections:S,changed:g.length>0,taken:o,menu:n,valid:y,onSave:v,onClose:c})}function Td({cfg:e,source:t,steps:n,definition:r,held:i,onClose:a,onRead:o}){let s=(0,Y.useMemo)(()=>Mu(e),[e]),c=t.config??{},[l,u]=(0,Y.useState)(void 0),[d,f]=(0,Y.useState)(n[0]?.name??``),[p,m]=(0,Y.useState)(null),[h,g]=(0,Y.useState)(``),[_,v]=(0,Y.useState)(new Set),[y,b]=(0,Y.useState)(``),[x,S]=(0,Y.useState)(null),[C,T]=(0,Y.useState)(null),[E,D]=(0,Y.useState)(null),[O,k]=(0,Y.useState)(0);(0,Y.useEffect)(()=>{xu(e).then(u,()=>u(null))},[e]),(0,Y.useEffect)(()=>{l&&c.environment&&c.flow&&(D(null),s.runs(c.environment,c.flow).then(m,e=>D(Q(e))))},[l,s,c.environment,c.flow,O]);let A=(0,Y.useMemo)(()=>[...new Set((p??[]).map(e=>e.status))].sort(),[p]),j=(0,Y.useMemo)(()=>(p??[]).filter(e=>!h||e.status===h),[p,h]),M=e=>v(t=>xe(t,e)),N=j.filter(e=>_.has(e.id)).map(e=>e.id),P=async()=>{let e=n.find(e=>e.name===d);if(!e||!c.environment||!c.flow)return;let t=N;T(null),S(`Reading 0 of ${t.length}`);try{let{records:n,skipped:a}=await Iu(s,c.environment,c.flow,t,e,r,i,e=>S(`Reading ${e} of ${t.length}`));await o(n,a)}catch(e){T(Re(Q(e))),S(null)}};return(0,$.jsxs)(Ae,{title:`Import runs`,onClose:a,wide:!0,dirty:N.length>0,onEnter:x||!N.length||!d?void 0:()=>void P(),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)(tt,{disabled:!!x||!N.length||!d,busy:!!x,why:d?N.length?null:`Choose runs to import`:`Choose a step`,onClick:()=>void P(),children:[x&&(0,$.jsx)(kt,{}),`Import `,N.length||``]})]}),children:[l===void 0?(0,$.jsx)(Bt,{label:`Account`}):l?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(w,{label:`Import`,rows:[{key:`step`,label:`Step`,control:{type:`select`,value:d,options:yd(n),onChange:f}},{key:`latest`,label:`Latest`,width:`quarter`,control:{type:`text`,value:y,placeholder:`N`,onChange:e=>{b(e);let t=Number.parseInt(e,10);t>0&&v(new Set(j.slice(0,t).map(e=>e.id)))}}}]}),p?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(xt,{noun:`runs`,count:{shown:j.length,total:p.length},filters:[{key:`status`,label:`Status`,value:h,onChange:g,options:[{value:``,label:`All`},...A.map(e=>({value:e,label:e}))]}]}),(0,$.jsx)(`div`,{className:`sflow`,children:(0,$.jsx)(W,{className:`slist`,label:`Runs`,rows:j,rowKey:e=>e.id,pageSize:20,total:p.length,empty:`(No runs)`,select:{noun:`run`,label:e=>`run ${e.id}`,selected:_,onChange:v},rowProps:e=>({onClick:()=>M(e.id),"data-selected":_.has(e.id)||void 0}),columns:[{key:`start`,header:`Started`,sort:e=>e.startTime??``,cell:e=>e.startTime&&ht(e.startTime)||(0,$.jsx)(`span`,{className:`unset`,children:`(Not started)`})},{key:`status`,header:`Status`,sort:e=>e.status,cell:e=>e.status}]})})]}):(0,$.jsx)(Bt,{label:`Runs`,error:E,onRetry:()=>k(e=>e+1)})]}):(0,$.jsx)(`div`,{className:`sacts`,children:(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,onClick:()=>{du(`import`,t.id),Tu(e).catch(e=>{du(`import`,null),T(Re(Q(e)))})},children:[(0,$.jsx)(mr,{size:14,"aria-hidden":`true`}),` Sign in with Microsoft`]})}),(0,$.jsx)(lt,{said:C})]})}function Ed({steps:e,onClose:t,onAdd:n}){let[r,i]=(0,Y.useState)(e[0]?.name??``),[a,o]=(0,Y.useState)({}),s=e.find(e=>e.name===r),c=(s?.reads??[]).filter(e=>Vt(e));return(0,$.jsxs)(Ae,{title:`Add item`,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=>Vt(t)===e))),[t]=vn(s,[e]).records;n({...t,origin:{kind:`hand`}})},children:`Add`})]}),children:[(0,$.jsx)(w,{label:`Item`,rows:[{key:`step`,label:`Step`,control:{type:`select`,value:r,options:yd(e),onChange:e=>{i(e),o({})}}},...c.map(e=>({key:`read-${Vt(e)}`,label:bd(e),control:{type:`textarea`,value:a[Vt(e)]??``,onChange:t=>o(n=>({...n,[Vt(e)]:t}))}}))]}),s&&!c.length&&(0,$.jsx)(`p`,{className:`unset`,children:`(No reads)`})]})}function Dd(e,t){if(e.toLowerCase().endsWith(`.csv`))return{rows:At(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.map(d)};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 Od({steps:e,onClose:t,onRead:n}){let r=(0,Y.useRef)(null),[i,a]=(0,Y.useState)(e[0]?.name??``),[o,s]=(0,Y.useState)(null),[c,l]=(0,Y.useState)(null),d=e.find(e=>e.name===i),f=o&&d&&`rows`in o.parsed?vn(d,o.parsed.rows):null,p=o?`records`in o.parsed?o.parsed.records:f?.records??[]:[],m=async e=>{if(e){l(null);try{s({name:e.name,parsed:Dd(e.name,await fd(e))})}catch(t){s(null),l(Re(`${e.name}: ${Q(t)}`))}}};return(0,$.jsxs)(Ae,{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:!p.length,onClick:()=>void n(p),children:[`Add `,p.length||``]})]}),children:[(0,$.jsx)(w,{label:`Upload`,rows:[...o&&`records`in o.parsed?[]:[{key:`step`,label:`Step`,control:{type:`select`,value:i,options:yd(e),onChange:a}}],{key:`file`,label:`File`,control:o?{type:`static`,text:o.name}:{type:`static`,text:`(No file)`,none:!0}},...o?[{key:`count`,label:`records`in o.parsed?`Items`:`Rows`,control:{type:`static`,text:String(p.length)}}]:[],...f?.unmatched.length?[{key:`unmatched`,label:`Unmatched`,control:{type:`static`,text:f.unmatched.join(`, `)}}]:[]]}),(0,$.jsxs)(`div`,{className:`sacts`,children:[(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,onClick:()=>r.current?.click(),children:[(0,$.jsx)(u,{size:14,"aria-hidden":`true`}),` Choose file`]}),(0,$.jsx)(`input`,{ref:r,type:`file`,accept:`.json,.csv`,hidden:!0,"aria-hidden":`true`,onChange:e=>{m(e.target.files?.[0]),e.target.value=``}})]}),(0,$.jsx)(lt,{said:c})]})}function kd(e){let t=Qr(),n=Dt(),[r,i]=Z(`promptlab.workflows`),[a,o]=(0,Y.useState)(!1),[s,c]=(0,Y.useState)(null);return{busy:a,say:s,create:async(a,s)=>{o(!0),c(null);let l=r;try{let{groupId:o,work:c}=await s(),u={id:c.id,name:c.name,work:c};i({...r,list:[...r.list,u],at:u.id}),e(),location.hash=`#/runs`,n.offer({label:a,restore:()=>{i(l),t.remove(o).catch(()=>{})}})}catch(e){c(Q(e)),o(!1)}}}}function Ad({test:e,ready:t,why:n,onCreate:r,onClose:i,children:a}){return(0,$.jsxs)(Ae,{title:`Create new experiment test`,onClose:i,className:`edit`,footer:(0,$.jsxs)(`div`,{className:`sacts`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:i,children:`Cancel`}),(0,$.jsxs)(tt,{why:n,disabled:e.busy||!t,busy:e.busy,onClick:r,children:[e.busy&&(0,$.jsx)(kt,{}),`Create`]})]}),children:[a,e.say&&(0,$.jsx)(pt,{tone:`error`,children:e.say})]})}var jd=e=>e.toLowerCase().endsWith(`.json`),Md=e=>K().METRICS[e.type]?.label??e.type;function Nd({source:e,definition:t,step:n,connections:r,onClose:i}){let a=X(),o=Qr(),[s,c]=Z(`promptlab.connections`),l=kd(i),u=qs(),[d,f]=(0,Y.useState)(null),[p,m]=(0,Y.useState)(null),h=(0,Y.useCallback)(()=>{m(null),(async()=>{try{let t=(await a.source(e.id)).files.filter(e=>jd(e.name)),n=await Promise.all(t.map(async t=>({name:t.name,record:JSON.parse(await fd(await a.file(e.id,t.name)))})));f(n)}catch(e){m(Q(e))}})()},[a,e.id]);(0,Y.useEffect)(h,[h]);let g=Ku(t,n),_=d?Zu(d,t,r):null,v=g.some(e=>K().METRICS[e.type]?.graded),[y,b]=(0,Y.useState)(``),x=u.asked.find(e=>e.id===y)??null,S=e=>{if(Hu(e,r))return r;let t={id:K().newId(),model:``,key:``,...Gu(e)};return c({...s,list:[...s.list,t]}),[...r,t]};return(0,$.jsx)(Ad,{test:l,ready:!!_?.cases.length,onCreate:()=>_&&void l.create(`Made a test of ${n.name}`,async()=>{let r=String(n.template.uri??``),i=await o.create(`${e.name}, ${n.name}`,{...Rr(),every:g,cases:_.cases,...x?{grader:{id:x.id,name:x.name}}:{}},{id:e.id,name:e.name}),a={id:i.id,name:i.name};return{groupId:i.id,work:qu({id:e.id,name:e.name},t,n,S(r),a)}}),onClose:i,why:_&&!_.cases.length?`No recorded responses`:void 0,children:d?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(w,{label:`Test`,rows:[{key:`pipeline`,label:`Experiment`,control:{type:`static`,text:`${e.name}, ${n.name}`}},{key:`a`,label:`Variant A`,control:{type:`static`,text:`Recorded`}},{key:`b`,label:`Variant B`,control:{type:`static`,text:`Draft`}},{key:`checks`,label:`Checks`,control:g.length?{type:`static`,text:g.map(Md).join(`, `)}:{type:`static`,text:`(No checks)`,none:!0}},...v?[{key:`grader`,label:`Grader`,control:u.control(y,b)}]:[],{key:`calls`,label:`Items graded`,control:{type:`static`,text:`${_.cases.length}${_.skipped?`, ${_.skipped} with no recorded response left out`:``}`}}]}),!_.cases.length&&(0,$.jsx)(`p`,{className:`note`,children:`(No recorded responses)`})]}):(0,$.jsx)(Bt,{label:`Items`,error:p,onRetry:h})})}function Pd({source:e}){let t=X(),n=ju(),[r,i]=(0,Y.useState)(null),[a,o]=(0,Y.useState)(null),[s,c]=(0,Y.useState)(!1),[l,u]=(0,Y.useState)(null),d=e.config??{},f=(0,Y.useCallback)(()=>{u(null),t.definition(e.id).then(i,e=>{i(null),u(e instanceof Error?e.message:String(e))})},[t,e.id,d.flow]);(0,Y.useEffect)(f,[f]);let[p,m]=(0,Y.useState)(d.environmentName??``),h=async()=>{if(p||!n||!d.environment)return;let r=(await Mu(n).environments()).find(e=>e.id===d.environment);r&&r.name!==r.id&&(m(r.name),await t.updateSource(e.id,{config:{...d,environmentName:r.name}}))};(0,Y.useEffect)(()=>{!p&&n&&xu(n).then(e=>e?h():void 0).catch(()=>{})},[n]);let g=async()=>{if(!n||!r||!d.environment||!d.flow)return;c(!0),o(null),du(`resync`,e.id);let a=!1;try{if(!await xu(n)){await Tu(n),a=!0;return}let s=await Nu(Mu(n),d.environment,d.flow);i(await t.saveDefinition(e.id,r.version,s.definition)),o(ln(`Synced.`)),h().catch(()=>{})}catch(e){e instanceof Ln&&e.status===409?(f(),o(ln(`It changed since: reloaded.`))):o(Re(e instanceof Error?e.message:String(e)))}finally{a||du(`resync`,null),c(!1)}},[_,v]=(0,Y.useState)(()=>fu(`resync`,e.id));(0,Y.useEffect)(()=>{_&&n&&r&&(v(!1),xu(n).then(e=>{e&&g()},()=>{}))},[_,n,r]);let y=(0,Y.useMemo)(()=>Fd(r?.body),[r]),b=(0,Y.useMemo)(()=>me(r?.body),[r]),[x]=Z(`promptlab.connections`),[S,T]=(0,Y.useState)(null),E=ad(e.id),D=e=>E.find(t=>K().flowStepOf(K().jobsOf(t.work)[0])?.step===e)??null,[O,k]=(0,Y.useState)(``),[A,j]=(0,Y.useState)(`testable`),[M,N]=(0,Y.useState)(``),P=e=>b.some(t=>t.name===e.name),F=(0,Y.useMemo)(()=>[...new Set(y.map(e=>e.type).filter(Boolean))].sort(),[y]),I=y.filter(e=>(A===`all`||P(e))&&(!M||e.type===M)&&e.name.toLowerCase().includes(O.trim().toLowerCase()));return(0,$.jsxs)(`div`,{className:`sources`,"data-level":`flow`,children:[(0,$.jsx)(lt,{said:a}),(0,$.jsxs)(Tt,{id:`flow-source`,title:`Flow`,actions:[{label:`Re-sync`,icon:br,busy:s,disabled:!n||!r,onSelect:()=>void g()}],children:[(0,$.jsx)(w,{label:`Flow`,rows:[...d.displayName&&d.displayName!==e.name?[{key:`flow`,label:`Flow`,control:{type:`static`,text:d.displayName}}]:[],{key:`env`,label:`Environment`,control:p||d.environment?{type:`static`,text:p||d.environment}:{type:`static`,text:`(No environment)`,none:!0}},...r?[{key:`version`,label:`Definition`,control:r.version?{type:`static`,text:`V${r.version}${r.at?`, ${ht(r.at)}`:``}`}:{type:`static`,text:`(No version)`,none:!0}}]:[]]}),!r&&(0,$.jsx)(Bt,{label:`Definition`,error:l,onRetry:f})]}),(0,$.jsx)(Tt,{id:`flow-actions`,title:`Actions`,note:r?String(y.length):void 0,children:r?y.length?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(xt,{noun:`actions`,search:{label:`Find an action`,value:O,placeholder:`Name`,onChange:k},quick:{label:`Show`,value:A,onChange:e=>j(e),options:[{value:`all`,label:`All`},{value:`testable`,label:`Can be tested`}]},filters:[{key:`type`,label:`Type`,value:M,onChange:N,options:[{value:``,label:`All`},...F.map(e=>({value:e,label:e}))]}],count:{shown:I.length,total:y.length}}),(0,$.jsx)(`div`,{className:`sflow`,children:(0,$.jsx)(W,{className:`slist`,cards:!0,label:`Actions`,rows:I,rowKey:e=>e.name,total:y.length,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:`Items from`,sort:e=>e.calls,cell:e=>e.calls||`—`},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,cell:e=>{let t=b.find(t=>t.name===e.name),n=t?D(t.name):null;return t?(0,$.jsx)(an,{label:`${e.name} actions`,actions:[n?{label:`Open test`,icon:C,onSelect:()=>{location.hash=od(n.id)}}:{label:`Create test…`,icon:C,onSelect:()=>T(t)}]}):null}}]})})]}):(0,$.jsx)(`p`,{className:`unset`,children:`(No actions)`}):(0,$.jsx)(Bt,{label:`Actions`,error:l,onRetry:f})}),(0,$.jsx)(pd,{source:e,definition:r?.body??null,cfg:n}),S&&r&&(0,$.jsx)(Nd,{source:e,definition:r.body,step:S,connections:x.list,onClose:()=>T(null)})]})}function Fd(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 Id={url:`URL`,reqHeaders:`Headers`,body:`Body`,method:`Method`,connection:`Auth connection`,path:`Path`,command:`Command`,args:`Arguments`},Ld=[`GET`,`POST`,`PUT`,`PATCH`,`DELETE`],Rd=e=>Id[e]??e.charAt(0).toUpperCase()+e.slice(1);function zd({typeRow:e,which:t,initial:n,onMade:r,bind:i}){let a=X(),[o]=Z(`promptlab.connections`),[s]=Z(`promptlab.versions`),c=Ss().current,[l,u]=(0,Y.useState)(null);(0,Y.useEffect)(()=>{let e=!0;return a.connectionShares().then(t=>{e&&u(t.shares)},()=>{e&&u({})}),()=>{e=!1}},[a]);let d=K().DYNAMIC_PROVIDERS,f=n?n.config?.provider:t,p=f&&f in d?f:n?``:Object.keys(d)[0]??``,m=d[p],h=!!m&&m.tier!==`supplied`&&!n,[g,_]=(0,Y.useState)(()=>{let{provider:e,...t}=n?.config??{};return t}),[v,y]=(0,Y.useState)(n?.name??null),b=v??Bd(g.url??``),[x,S]=(0,Y.useState)(!1),[C,T]=(0,Y.useState)(null),E=m?m.configProblems(g):[`unknown provider`],D=!!m&&!!b.trim()&&E.length===0,O={provider:p,...g},k=!!n&&!Vd(O,n.config??{}),A=!!n&&b.trim()!==n.name,j=k||A,M=(0,Y.useRef)(async()=>{});(0,Y.useEffect)(()=>{i({enabled:D&&!x,busy:x,changed:j,run:()=>void M.current()})},[i,D,x,j,p]),(0,Y.useEffect)(()=>()=>i(null),[i]),M.current=async()=>{if(!m||!D)return;if(n&&!j){await r(n);return}S(!0),T(null);let e=null;try{if(n){await r(await a.updateSource(n.id,{...A?{name:b.trim()}:{},...k?{config:O}:{}}));return}let t=await a.createSource(b.trim(),`dynamic`,O);e=t.id;let i;if(h){let e=m.noun;i=await a.snapshotSource(t.id).then(({call:t})=>t.error?`The ${e} failed: ${t.error}`:void 0,t=>`The ${e} failed: ${t instanceof Error?t.message:String(t)}`)}await r(t,i)}catch(t){e&&await a.removeSource(e).catch(()=>{}),T(t instanceof Ln||t instanceof Error?t.message:String(t))}finally{S(!1)}};let N=e=>t=>_(n=>({...n,[e]:t})),P=e=>{let t=g[e]??``;if(e===`method`)return{type:`select`,value:t||`GET`,onChange:N(e),options:Ld.map(e=>({value:e,label:e}))};if(e===`connection`){let n=o.list.filter(e=>K().listedConnection(e)&&l!==null&&(!c||zn(l,`profile`,e.id,c.id))),r=!n.some(e=>e.id===t)&&o.list.find(e=>e.id===t),i=r?r.name||r.id:``;return{type:`picker`,kind:`Connection`,value:t,onChange:N(e),usedLabel:`Last changed`,...Bs(n,e=>s.profile?.[e]?.[0]?.at??null,[{id:``,label:`No auth`}]),...r?{placeholder:l===null?i:`${i}, not shared with ${c?.name??`this workspace`}`}:{}}}return e===`reqHeaders`||e===`body`||e===`args`?{type:`textarea`,value:t,onChange:N(e),placeholder:e===`reqHeaders`?`Name: value; Name: value`:e===`args`?`One a line`:``}:{type:`text`,value:t,onChange:N(e),...e===`url`?{placeholder:`https://…`}:{}}},F=b.trim()&&E[0]?E[0].charAt(0).toUpperCase()+E[0].slice(1):null,I=m?.config??[],ee=F?I.find(e=>[e,Rd(e).replace(/s$/,``)].some(e=>F.toLowerCase().includes(e.toLowerCase())))??I[0]:void 0,L=I.map(e=>({key:e,label:Rd(e),control:P(e),...e===ee?{error:F}:{}}));return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(w,{label:n?n.name:`New Source`,rows:[e,...L,{key:`name`,label:`Name`,width:`full`,control:{type:`text`,value:b,onChange:y}}]}),m?.warning&&(0,$.jsxs)(`p`,{className:`swarn`,role:`note`,children:[(0,$.jsx)(pn,{size:14,"aria-hidden":`true`}),m.warning]}),F&&!ee&&(0,$.jsx)(pt,{tone:`error`,children:F}),C&&(0,$.jsx)(pt,{tone:`error`,children:C})]})}var Bd=e=>{try{let t=new URL(e.trim());if(t.protocol!==`http:`&&t.protocol!==`https:`)return``;let n=t.pathname.split(`/`).filter(Boolean).pop()??``;return[t.hostname,decodeURIComponent(n)].filter(Boolean).join(` `)}catch{return``}},Vd=(e,t)=>[...new Set([...Object.keys(e),...Object.keys(t)])].every(n=>(e[n]??``)===(t[n]??``)),Hd=e=>`${e.n}:${e.name}`,Ud=e=>e.status==null||e.status>=200&&e.status<300;function Wd(e){return Object.entries(e).flatMap(([e,t])=>t.map(t=>({...t,name:e}))).sort((e,t)=>(t.at??``).localeCompare(e.at??``)||t.n-e.n)}function Gd(e){let t=Wd(e).find(Ud);return t?{name:t.name,n:t.n}:null}function Kd(e,t){let n=e[t.name];return n?.find(e=>e.n===t.n)??(t.n===0?n?.find(e=>e.n===1):void 0)}function qd(e,t){let n=Kd(e,t);return n?Hd({name:t.name,n:n.n}):``}var Jd=(e,t)=>[e,td(t)].filter(Boolean).join(`, `),Yd=e=>Wd(e).map(e=>({id:Hd(e),label:Jd(e.name,e)}));function Xd(e){let t=e.indexOf(`:`);return{n:Number(e.slice(0,t)),name:e.slice(t+1)}}function Zd(e,t,n=0){let[r,i]=(0,Y.useState)(null);return(0,Y.useEffect)(()=>{if(!t)return;let n=!0;return e.sourceResponses(t).then(e=>{n&&i({id:t,responses:e.responses})},()=>{n&&i({id:t,responses:{}})}),()=>{n=!1}},[e,t,n]),t?r?.id===t?r.responses:null:{}}var Qd=`Answers the prompt using only what's in the item.`,$d=`#default`;function ef({source:e,responses:t,plural:n,onClose:r}){let i=Qr(),[a]=Z(`promptlab.connections`),[o]=Z(`promptlab.connectionProfiles`),{list:s,error:c}=ci(),l=kd(r),u=qs(),{asked:d,labGrader:f}=u,p=(0,Y.useMemo)(()=>K().answersOf(K().withSlugs(a.list),o.list).split,[a.list,o.list]),m=Ws(),[h,g]=(0,Y.useState)(d[0]?.id??``),[_,v]=(0,Y.useState)(null),[y,b]=(0,Y.useState)(null),[x,S]=(0,Y.useState)(``),[C,T]=(0,Y.useState)(Qd),[E,D]=(0,Y.useState)(()=>Gd(t)),O=_??p(h)?.id??$d,k=y??(f?``:h),A=s!==null||c!==null,j=x.trim()||(s?.find(e=>e.default)?.text??``),M=(e,t)=>{let n=e.find(e=>e.id===t);return n?{id:n.id,name:n.name}:null};return(0,$.jsx)(Ad,{test:l,onCreate:()=>{let t=M(d,h);t&&E&&l.create(`Made a test of ${e.name}`,async()=>{let r={type:`llm-rubric`,...K().METRICS[`llm-rubric`].defaults(),rubric:C.trim()},a=k?M(d,k):null,s=await i.create(`${e.name}, ${n}`,{...Rr(),every:[r],cases:Qu([E.name]).map(e=>({...e,todo:!1})),...a?{grader:a}:{}},{id:e.id,name:e.name}),c=H(e,{connection:t,profile:O===$d?null:M(o.list,O)},{id:s.id,name:s.name},j),l=K().contentOf(c);return{groupId:s.id,work:l?.type===`source`?K().withContent(c,{...l,live:!1,pin:E}):c}})},onClose:r,ready:!!h&&!!E&&A&&!!j&&!!C.trim(),why:A&&!j?`Needs a prompt`:void 0,children:(0,$.jsx)(w,{label:`Test`,rows:[{key:`pipeline`,label:`Experiment`,control:{type:`static`,text:e.name}},{key:`connection`,label:`Connection`,required:!0,control:{type:`picker`,kind:`Connection`,value:h,...m(d),onChange:e=>{g(e),v(null)}}},{key:`profile`,label:`Profile`,control:{type:`picker`,kind:`Profile`,value:O,...Bs(o.list,()=>null,[{id:$d,label:`(Default)`}]),onChange:v}},{key:`prompt`,label:`Prompt`,required:!j,control:{type:`textarea`,value:x,onChange:S,placeholder:s?.find(e=>e.default)?.text??``}},{key:`rubric`,label:`Rubric`,required:!0,control:{type:`textarea`,value:C,onChange:T}},{key:`grader`,label:`Grader`,control:u.control(k,b)},{key:`response`,label:`Response`,required:!0,width:`full`,control:{type:`picker`,kind:`Response`,ordered:!0,value:E?Hd(E):``,options:Yd(t),placeholder:`(Not set)`,onChange:e=>D(Xd(e))}}]})})}var tf={"cloud-sync":Zn,"file-input":tr,"square-terminal":kr};function nf({name:e}){let t=tf[e]??Zn;return(0,$.jsx)(t,{size:14,"aria-hidden":`true`})}var rf=e=>e.slice(0,1).toUpperCase()+e.slice(1),af=e=>/^[aeiou]/i.test(e)?`an`:`a`;function of({source:e,note:t}){let[n]=Z(`promptlab.connections`),r=e.config??{},i=K().DYNAMIC_PROVIDERS[r.provider??``],a=(i?.config??[]).filter(e=>r[e]?.trim()).map(e=>{let t=r[e],i=Rd(e);return e===`connection`?{key:e,label:i,control:{type:`static`,text:n.list.find(e=>e.id===t)?.name||t}}:{key:e,label:i,control:{type:`code`,text:t}}});return(0,$.jsxs)(`div`,{className:`sources`,"data-level":`dynamic`,children:[a.length>0&&(0,$.jsx)(Tt,{id:`dynamic-provider`,title:`Provider`,children:(0,$.jsx)(w,{label:`Provider`,rows:a})}),(0,$.jsx)(sf,{source:e,noun:i?.noun??`item`,canProduce:i?.tier!==`supplied`,produceIcon:i?.produceIcon??`cloud-sync`,takes:Object.keys(i?.uploads??{}),note:t})]})}function sf({source:e,noun:t,canProduce:n,produceIcon:r,takes:i,note:a}){let o=X(),s=Qr(),c=Dt(),[l,d]=(0,Y.useState)(null),[f,p]=(0,Y.useState)({}),[m,h]=(0,Y.useState)(null),[g,_]=(0,Y.useState)(a?Re(a):null);(0,Y.useEffect)(()=>{a&&_(Re(a))},[a]);let[v,y]=(0,Y.useState)(!1),b=(0,Y.useRef)(null),[x,S]=(0,Y.useState)(!1),[w,T]=(0,Y.useState)(null),[E,D]=(0,Y.useState)(null),[O,k]=(0,Y.useState)(!1),A=`${t}s`,j=rf(A),M=(0,Y.useMemo)(()=>(l??[]).filter(e=>!e.error).map(e=>e.name),[l]),N=ad(e.id),{caseOf:P,casesIn:F}=sd(e.id,N),I=af(t),ee=n?`Needs ${I} ${t} that succeeded`:`Needs ${I} ${t}`,L=(0,Y.useCallback)(()=>{h(null),Promise.all([o.sourceCalls(e.id),o.sourceResponses(e.id)]).then(([e,t])=>{d(e.calls),p(t.responses),D(null)},e=>h(Q(e)))},[o,e.id]);(0,Y.useEffect)(L,[L]);let te=async()=>{y(!0),_(null);try{let{call:t}=await o.snapshotSource(e.id);if(L(),t.outcome===`fetched`&&t.n!=null){let{n,was:r}=t;c.offer({label:`Fetched ${t.name}`,restore:()=>r==null?void 0:o.restoreResponse(e.id,t.name,r,n).then(L)})}else t.outcome===`added`?c.offer({label:`Added ${t.name}`,restore:()=>o.removeCalls(e.id,[t.name]).then(L)}):t.outcome===`unchanged`?c.notify({label:`${t.name} is unchanged`}):c.notify({label:`Fetch failed: ${t.status??t.error??`no answer`}`,bad:!0})}catch(e){c.notify({label:`Fetch failed: ${Q(e)}`,bad:!0})}finally{y(!1)}},R=async t=>{if(!t.length)return;_(null);let n=[],r=[];for(let i of t)try{await o.upload(e.id,i.name,i),n.push(i.name)}catch(e){r.push(`${i.name}: ${Q(e)}`)}L();let i=[...n.length===1?[`Uploaded ${n[0]}`]:n.length?[`Uploaded ${n.length} ${A}`]:[],...r].join(`; `);_(r.length?Re(i):ln(i))},re=async()=>{let n=M;if(!n.length)return _(ln(`No ${t} to grade yet.`));try{let t=await s.create(`${e.name}, ${A}`,{...Rr(),cases:Qu(n)},{id:e.id,name:e.name});_(ln(`Made ${t.name}: ${n.length} ${n.length===1?`eval case`:`eval cases`}`))}catch(e){_(Re(Q(e)))}},z=async t=>{try{let{trash:n}=await o.removeCalls(e.id,t);d(e=>(e??[]).filter(e=>!t.includes(e.name))),p(e=>Object.fromEntries(Object.entries(e).filter(([e])=>!t.includes(e))));let r=t.length===1?`Removed ${t[0]}`:`Removed ${t.length} ${A}`;n?c.offer({label:r,restore:()=>void o.restoreTrash(n).then(L,e=>_(Re(Q(e))))}):_(ln(r))}catch(e){_(Re(Q(e)))}},B=async t=>{if(t.error)return T({call:t,text:null,seq:Date.now()});try{T({call:t,text:await(await o.file(e.id,t.name)).text(),seq:Date.now()})}catch(e){_(Re(Q(e)))}},V=async(t,n,r)=>{let{call:i}=await o.editCall(e.id,t.name,{...n===t.name?{}:{to:n},...r===void 0?{}:{content:r}});L(),c.offer({label:r===void 0?`Renamed ${t.name}`:`Edited ${t.name}`,restore:async()=>{i.n!=null&&i.was!=null&&await o.restoreResponse(e.id,i.name,i.was,i.n),i.name!==t.name&&await o.editCall(e.id,i.name,{to:t.name}),L()}})},H=async(t,n)=>{let r=(f[t.name]??[]).find(e=>e.current)?.n;try{await o.restoreResponse(e.id,t.name,n),L(),await B(t),r!=null&&c.offer({label:`Restored ${t.name}`,restore:()=>o.restoreResponse(e.id,t.name,r).then(L)})}catch(e){_(Re(Q(e)))}},ie=async t=>{try{let{call:n}=await o.cloneCall(e.id,t),r={kind:`copied`,at:new Date().toISOString()};d(e=>(e??[]).flatMap(e=>e.name===t?[e,{...e,name:n.name,origin:`copy`,updated:r}]:[e])),D(n.name),c.offer({label:`Cloned ${t}`,restore:()=>void o.removeCalls(e.id,[n.name]).then(()=>d(e=>(e??[]).filter(e=>e.name!==n.name)),e=>_(Re(Q(e))))})}catch(e){_(Re(Q(e)))}},U=(t,n)=>[...n?[]:[{label:`Edit…`,icon:J.edit,disabled:!!t.error,onSelect:()=>void B(t)}],{label:`Clone`,icon:J.clone,disabled:!!t.error,onSelect:()=>{n&&T(null),ie(t.name)}},{label:`Download`,icon:J.export,disabled:!!t.error,onSelect:()=>void uf(o,e.id,t.name).catch(e=>_(Re(Q(e))))},{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>{n&&T(null),z([t.name])}}],ae=[{label:`Upload…`,icon:u,onSelect:()=>b.current?.click()},{label:`New eval`,icon:Me,disabled:!(l??[]).some(e=>!e.error),onSelect:()=>void re()}],oe=e=>e.error?(0,$.jsxs)(`span`,{className:`sresult`,title:e.error,children:[(0,$.jsx)(Xt,{pass:!1,decorative:!0}),e.status??`Failed`]}):(0,$.jsx)(nd,{status:e.origin===`copy`?null:e.status}),se=e=>e.error?null:P(e.name),ce=e=>{let t=K().contentOf(e);return t?.type===`source`&&!(t.live??!(l??[]).length)?{pin:t.pin??null}:null},le=e=>{let t=ce(e);if(!t)return`Live data`;if(!t.pin)return(0,$.jsx)(`span`,{className:`unset`,children:`(Not set)`});let n=Kd(f,t.pin);return n?Jd(t.pin.name,n):(0,$.jsx)(`span`,{className:`unset`,children:`(Removed)`})};return(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(Tt,{id:`dynamic-calls`,title:j,note:l?String(l.length):void 0,actions:ae,children:[(0,$.jsx)(lt,{said:g}),l===null?(0,$.jsx)(Bt,{label:j,error:m,onRetry:L}):l.length?(0,$.jsx)(`div`,{className:`sflow`,children:(0,$.jsx)(W,{className:`slist`,cards:!0,label:j,rows:l,rowKey:e=>e.name,select:{noun:t,actions:e=>[{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>void z(e.map(e=>e.name))}]},rowProps:e=>e.name===E?{"data-fresh":``}:{},columns:[{key:`name`,header:`Name`,role:`title`,sort:e=>e.name.replace(/\.[^.]*$/,``),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`link-btn sopen`,onClick:()=>void B(e),children:e.name})},{key:`updated`,header:`Updated`,sort:e=>e.updated?.at??e.at??``,cell:e=>td(e.updated)||(0,$.jsx)(`span`,{className:`unset`,children:`(Never)`})},...n?[{key:`status`,header:`Status`,sort:e=>String(e.status??``),cell:oe}]:[],{key:`case`,header:`Eval Case`,sort:e=>se(e)?.name??``,cell:e=>(0,$.jsx)(cd,{group:se(e)})},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(an,{label:`${e.name} actions`,actions:U(e,!1)})}]})}):(0,$.jsxs)(`p`,{className:`note`,children:[`No `,A,` yet.`,n?``:` Write or upload one, or hand them in through the CLI.`]}),n?(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,"aria-busy":v||void 0,disabled:v,onClick:()=>void te(),children:[(0,$.jsx)(nf,{name:r}),` Get response`]}):(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>S(!0),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),` New `,t,`…`]})]}),(0,$.jsx)(ud,{pipelines:N,casesIn:F,itemsOf:e=>{let t=n?ce(e):null;return t?.pin?[t.pin.name]:M},columns:n?[{key:`updates`,header:`Data updates`,cell:le}]:[],footer:(0,$.jsxs)(tt,{className:`ghost add-btn`,why:ee,disabled:!M.length,onClick:()=>k(!0),children:[(0,$.jsx)(C,{size:14,"aria-hidden":`true`}),` Create test…`]})}),(0,$.jsx)(`input`,{ref:b,type:`file`,multiple:!0,accept:`.txt,.json,.csv,.md`,hidden:!0,"aria-hidden":`true`,onChange:e=>{R([...e.target.files??[]]),e.target.value=``}}),x&&(0,$.jsx)(cf,{noun:t,takes:i,onSave:async(t,n)=>{try{await o.upload(e.id,t,new Blob([n],{type:`text/plain`}))}finally{L()}},onClose:()=>S(!1)}),O&&(0,$.jsx)(ef,{source:e,responses:f,plural:A,onClose:()=>k(!1)}),w&&(0,$.jsx)(lf,{call:w.call,text:w.text,menu:U(w.call,!0),caseOf:se(w.call),responses:f[w.call.name]??[],onRestore:e=>void H(w.call,e),taken:e=>(l??[]).some(t=>t.name===e),onSave:(e,t)=>V(w.call,e,t===(w.text??``)?void 0:t),onClose:()=>T(null)},w.seq),(0,$.jsx)(Qt,{undo:c.undo,notice:c.notice,onUndo:c.undoNow,onDismiss:c.dismiss})]})}function cf({noun:e,takes:t,onSave:n,onClose:r}){let[i,a]=(0,Y.useState)(``),[o,s]=(0,Y.useState)(``),[c,l]=(0,Y.useState)(!1),[u,d]=(0,Y.useState)(null),f=async()=>{let a=i.trim(),s=/\.[^./]+$/.exec(a)?.[0].toLowerCase(),c=s?a:`${a}.txt`,u=rf(af(e));if(!a)return d(`${u} ${e} needs a name.`);if(s&&t.length&&!t.includes(s))return d(`${u} ${e}'s name ends ${t.join(`, `)}.`);if(!o.trim())return d(`${u} ${e} needs some text.`);l(!0);try{await n(c,o),r()}catch(e){d(Q(e)),l(!1)}};return(0,$.jsx)(De,{title:`New ${e}`,focusRow:`name`,status:u??void 0,statusBad:!!u,wide:!0,sections:[{key:`item`,rows:[{key:`name`,label:`Name`,width:`full`,required:!0,control:{type:`text`,value:i,onChange:a,placeholder:`notes.txt`}},{key:`text`,label:`Text`,required:!0,control:{type:`textarea`,value:o,onChange:s}}]}],onSave:()=>void f(),saving:c,adding:!0,onClose:r})}function lf({call:e,text:t,menu:n,caseOf:r,responses:i,onRestore:a,taken:o,onSave:s,onClose:c}){let[l,u]=(0,Y.useState)(t??``),d=e.request,f=d?.url?`${d.method??`GET`} ${d.url}`:d?.path??d?.command,p=[...f?[{key:`request`,label:`Request`,control:{type:`code`,text:f}}]:[],...e.error?[{key:`error`,label:`Error`,control:{type:`static`,text:e.error}}]:[{key:`case`,label:`Eval case`,control:ld(r)}]];return t===null?(0,$.jsx)(id,{name:e.name,facts:p,taken:o,menu:n,onClose:c}):(0,$.jsx)(id,{name:e.name,facts:p,taken:o,menu:n,onClose:c,changed:l!==t,onSave:e=>s(e,l),sections:[{key:`content`,title:`Content`,children:(0,$.jsx)(`textarea`,{className:`mono call-content`,"aria-label":`Content`,spellCheck:!1,value:l,onChange:e=>u(e.target.value)})},...i.length?[rd(i,a)]:[]]})}async function uf(e,t,n){let r=URL.createObjectURL(await e.file(t,n)),i=document.createElement(`a`);i.href=r,i.download=n,document.body.append(i),i.click(),i.remove(),URL.revokeObjectURL(r)}var df={workflow:{Create:Ru,Level:Pd},dynamic:{Create:zd,Level:of}},ff=e=>(e.provider?K().DYNAMIC_PROVIDERS[e.provider]?.label??e.provider:null)??(e.platform?K().WORKFLOW_PLATFORMS[e.platform]?.label??e.platform:null)??K().SOURCE_TYPES[e.type]?.label??e.type,pf=e=>Q(e,`the request failed`);function mf({source:e,onSaved:t,onClose:n}){let r={key:`kind`,label:`Kind`,control:{type:`static`,text:ff(e)}},i=df[e.type]?.Create;return i?(0,$.jsx)(gf,{source:e,kindRow:r,Create:i,onSaved:t,onClose:n}):(0,$.jsx)(hf,{source:e,kindRow:r,onSaved:t,onClose:n})}function hf({source:e,kindRow:t,onSaved:n,onClose:r}){let i=X(),[a,o]=(0,Y.useState)(e.name),[s,c]=(0,Y.useState)(!1),[l,u]=(0,Y.useState)(null),d=async()=>{let t=a.trim();if(!t||t===e.name){r();return}c(!0);try{n(await i.updateSource(e.id,{name:t})),r()}catch(e){u(pf(e)),c(!1)}};return(0,$.jsx)(De,{title:`Edit ${e.name}`,focusRow:`name`,status:l??void 0,statusBad:!!l,sections:[{key:`source`,rows:[{key:`name`,label:`Name`,width:`full`,required:!0,control:{type:`text`,value:a,onChange:o}},t]}],onSave:()=>void d(),saving:s,onClose:r})}function gf({source:e,kindRow:t,Create:n,onSaved:r,onClose:i}){let a=X(),[o,s]=(0,Y.useState)(null),[c,l]=(0,Y.useState)(null),[u,d]=(0,Y.useState)(0),[f,p]=(0,Y.useState)(null);return(0,Y.useEffect)(()=>{let t=!0;return a.source(e.id).then(e=>{t&&s(e)},e=>{t&&l(pf(e))}),()=>{t=!1}},[a,e.id,u]),(0,$.jsx)(De,{title:`Edit ${e.name}`,dirty:!!f?.changed,sections:[{key:`source`,children:o?(0,$.jsx)(n,{typeRow:t,initial:o,bind:p,onMade:e=>{r(e),i()}}):(0,$.jsx)(Bt,{label:`Source`,error:c,onRetry:()=>{l(null),d(e=>e+1)}})}],onSave:()=>f?.run(),saving:!!f?.busy,canSave:!!f?.enabled,onClose:i})}var _f={folder:be,workflow:pe,globe:Ce,upload:u,file:nr,terminal:kr,"square-code":Or,"arrow-left-right":Kn},vf=new Set([`square-code`,`arrow-left-right`]);function yf(e,t,n){let r=t?K().WORKFLOW_PLATFORMS[t]:void 0,i=n?K().DYNAMIC_PROVIDERS[n]:void 0;return r?.icon??i?.icon??K().SOURCE_TYPES[e]?.icon}function bf({name:e,size:t=16}){let n=e&&_f[e]||be;return(0,$.jsx)(n,{className:`skind`,size:t,fill:e&&vf.has(e)?`none`:`currentColor`,"aria-hidden":`true`})}function xf({sourceId:e=null,onOpen:t,onGone:r,mode:i=`browse`,onPick:a,disabled:o}){let s=Os(),c=X(),l=Dt(),[d]=Z(`promptlab.workflows`),f=$r(),[p,m]=(0,Y.useState)(null),[g,_]=(0,Y.useState)(0),[v,y]=(0,Y.useState)(null),[x,S]=(0,Y.useState)(Ul),[C,T]=(0,Y.useState)(``),[E,D]=(0,Y.useState)(new Set),[O,k]=(0,Y.useState)(null),[A,j]=(0,Y.useState)(!1),[N,P]=(0,Y.useState)(()=>i===`browse`&&Cf()!==null),[I,ee]=(0,Y.useState)(null),[L,te]=(0,Y.useState)(null),[R,re]=(0,Y.useState)(null),[z,B]=(0,Y.useState)(void 0),[H,ie]=(0,Y.useState)(!1),[U,ae]=(0,Y.useState)(``),oe=(0,Y.useRef)(!1),[se,ce]=(0,Y.useState)(!1),le=async e=>{if(!oe.current){oe.current=!0,ce(!0);try{await e()}finally{oe.current=!1,ce(!1)}}},[G,ue]=(0,Y.useState)(()=>i===`browse`&&Cf()||K().DEFAULT_SOURCE_TYPE),[de,fe]=(0,Y.useState)(null),[pe,me]=(0,Y.useState)(null),[he,_e]=(0,Y.useState)(null);(0,Y.useEffect)(()=>{_e(t=>t&&t.id!==e?null:t)},[e]);let[ve,ye]=(0,Y.useState)(``),[be,Se]=(0,Y.useState)(``),[Ce,we]=(0,Y.useState)([]),[Te,Ee]=(0,Y.useState)(null),[De,Oe]=(0,Y.useState)([]),[je,Me]=(0,Y.useState)(!1),[Ne,Pe]=(0,Y.useState)(!1),[Fe,Ie]=(0,Y.useState)(null),[Le,Re]=(0,Y.useState)(null),[ze,Be]=(0,Y.useState)(0),[Ve,He]=(0,Y.useState)(0),Ue=(0,Y.useRef)(null),We=(0,Y.useRef)(1),{view:Ge,sort:Ke,dir:qe}=x,Je=(0,Y.useCallback)(()=>ee(null),[]),Ye=(0,Y.useCallback)(()=>{P(!1),me(null)},[]),Xe=(0,Y.useCallback)(()=>te(null),[]),Ze=(0,Y.useCallback)(()=>ie(!1),[]);(0,Y.useEffect)(()=>Wl(x),[x]),(0,Y.useEffect)(()=>{i===`browse`&&wf(N?G:null)},[i,N,G]),(0,Y.useEffect)(()=>{let e=!0;return c.config().then(t=>{e&&we(t.dynamicProviders??[])},()=>{}),()=>{e=!1}},[c]),(0,Y.useEffect)(()=>{if(!R?.platform){B(void 0);return}let e=!0;return Promise.all([c.connections(),c.connectionShares()]).then(([{connections:t},{shares:n}])=>{if(!e)return;let r=t.find(e=>e.id===`microsoft`);if(!r){B(void 0);return}let i=n[Rn(`account`,r.id)];B({name:r.label,sharedWith:e=>!i||i.includes(e)||i.includes(`*all`)})},()=>{e&&B(void 0)}),()=>{e=!1}},[R,c]);let Qe=async()=>{try{let e=await c.sources();m(e.sources),_(e.cap)}catch{}};(0,Y.useEffect)(()=>{let e=!0;return c.sources().then(t=>{e&&(m(t.sources),_(t.cap))}).catch(t=>{e&&Ie(nt(t))}),()=>{e=!1}},[c,ze]),(0,Y.useEffect)(()=>{D(new Set),Oe([]);let t=!0;return Re(null),e?c.source(e).then(e=>{t&&y(e)}).catch(e=>{t&&(y(null),e instanceof Ln&&e.status===404&&r?r():Re(nt(e)))}):y(null),()=>{t=!1}},[e,Ve]);let et=async()=>{if(!e){y(null);return}try{y(await c.source(e))}catch{y(null)}},tt=(e,t=`info`)=>{Ee({tone:t,text:e}),window.setTimeout(()=>Ee(null),6e3)},nt=e=>Q(e,`the request failed`),rt=Ql(Zl(v?.files??[],Ke,qe),C),it=e=>D(t=>xe(t,e)),at=()=>D(new Set),ot=e=>{S(t=>({...t,view:e})),e===`thumbs`&&at()},st=async e=>{let t=[];try{for(let n of e)t.push((await c.removeSource(n.id)).trash),m(e=>(e??[]).filter(e=>e.id!==n.id))}catch(e){tt(nt(e),`error`)}t.length&&l.offer({label:e.length===1?`Deleted ${e[0].name}`:`Deleted ${t.length} Sources`,restore:()=>void(async()=>{try{for(let e of t)await c.restoreTrash(e);await Qe()}catch(e){tt(nt(e),`error`)}})()})},ct=e=>st([e]),ut=async e=>{if(v)try{let{trash:t}=await c.removeFiles(v.id,e),n=new Set(e);y(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)}),D(e=>new Set([...e].filter(e=>!n.has(e)))),l.offer({label:e.length===1?`Removed ${e[0]}`:`Removed ${e.length} files`,restore:()=>void(async()=>{try{let e=await c.restoreTrash(t);y(e.source)}catch(e){tt(nt(e),`error`)}})()})}catch(e){tt(nt(e),`error`)}},dt=(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)},ft=async e=>{if(!v)return;let t=e.length===1?e[0]:null;Pe(!0);try{let n=t?await c.file(v.id,t):await c.zip(v.id,e);dt(n,t??`${v.name}.zip`)}catch(e){tt(nt(e),`error`)}finally{Pe(!1)}},mt=async e=>{if(!v||v.system)return;let t=[...e];if(!t.length)return;let n=t.map(e=>({id:We.current++,file:e,state:`uploading`}));Oe(e=>[...e,...n]);for(let e of n)try{await c.upload(v.id,e.file.name,e.file),Oe(t=>t.map(t=>t.id===e.id?{...t,state:`done`}:t))}catch(t){Oe(n=>n.map(n=>n.id===e.id?{...n,state:`error`,error:nt(t)}:n))}await et()},ht=e=>void mt([e.file]),gt=async(e,t)=>{if(v&&L)try{await c.copyFiles(t.id,v.id,L.names,e===`move`),te(null),D(new Set),await et(),tt(e===`move`?`Moved to ${t.name}.`:`Copied to ${t.name}.`)}catch(e){tt(nt(e),`error`),te(null)}},_t=()=>le(async()=>{if(v&&U.trim())try{let e=await c.createSource(U.trim(),v.type);try{await c.copyFiles(e.id,v.id,[...E],!1)}catch(t){throw await c.removeSource(e.id).catch(()=>{}),t}ie(!1),ae(``),D(new Set),await Qe(),tt(`Made a new Source.`)}catch(e){tt(nt(e),`error`)}}),q=()=>le(async()=>{if(U.trim()){me(null);try{await c.createSource(U.trim(),G),P(!1),ae(``),ue(K().DEFAULT_SOURCE_TYPE),await Qe()}catch(e){me(nt(e))}}}),vt=O&&(0,$.jsx)(mf,{source:O,onSaved:e=>{m(t=>(t??[]).map(t=>t.id===e.id?tu(e):t)),y(t=>t?.id===e.id?{...t,name:e.name,config:e.config??null}:t)},onClose:()=>k(null)}),yt=e=>s({tab:`library`,section:`sources`,id:e.id}),bt=(e,t)=>e.system?[yt(e)]:[{label:`Edit…`,icon:b,onSelect:()=>k(e)},yt(e),{label:`Copy or move…`,icon:J.transfer,onSelect:()=>re(e)},{label:`Delete`,icon:ge,destructive:!0,onSelect:()=>void ct(e).then(t)}],St=e=>[{label:`Preview`,icon:F,onSelect:()=>ee(e)},{label:`Download`,icon:M,onSelect:()=>void ft([e.name])},...v?.system?[]:[{label:`Remove`,icon:ge,destructive:!0,onSelect:()=>void ut([e.name])}]],Ct=[...v?.system?[]:[{label:`Remove`,icon:ge,destructive:!0,onSelect:()=>void ut([...E])}],{label:`Download`,icon:M,busy:Ne,onSelect:()=>void ft([...E])},...v?.system?[]:[{label:`Move to…`,icon:or,onSelect:()=>te({kind:`move`,names:[...E]})}],{label:`Copy to…`,icon:ke,onSelect:()=>te({kind:`copy`,names:[...E]})},...v?.system?[]:[{label:`New Source from selection…`,icon:sr,onSelect:()=>ie(!0)}]],wt=e=>{e.preventDefault(),Me(!1),v&&!v.system&&mt(e.dataTransfer.files)},Et=R&&(0,$.jsx)(Ns,{subject:{kind:`source`,id:R.id,name:R.name},scan:{pipelines:d.list,groups:f.list??[]},undo:l,accountShare:z,onClose:()=>{re(null),B(void 0)},onChanged:()=>{Qe(),e===R.id&&t?.(null)}}),Ot=(p??[]).filter(e=>(!be||Lf(e)===be)&&e.name.toLowerCase().includes(ve.trim().toLowerCase())),At=(0,$.jsx)(kf,{view:Ge,onChange:ot}),jt=e=>(0,$.jsx)(xt,{noun:`Sources`,search:{label:`Find a Source`,value:ve,placeholder:`Name`,onChange:ye},filters:[{key:`type`,label:`Type`,value:be,onChange:Se,options:[{value:``,label:`All`},...Pf(Ce).filter(e=>!e.disabled)]}],trail:e,count:{shown:Ot.length,total:(p??[]).length}}),Mt=()=>(0,$.jsxs)(`div`,{className:`sources`,"data-level":`sources`,children:[(0,$.jsx)(lt,{said:Te}),(0,$.jsxs)(`header`,{className:`shead`,children:[(0,$.jsx)(`h2`,{className:`shead-title`,children:`Sources`}),At]}),p!==null&&p.length>0&&jt(),p===null?(0,$.jsx)(Bt,{label:`Sources`,error:Fe,onRetry:()=>{Ie(null),Be(e=>e+1)}}):p.length===0?(0,$.jsx)(`p`,{className:`unset`,children:`(No Sources)`}):Ge===`thumbs`?(0,$.jsx)(`ul`,{className:`sfolders`,children:Ot.map(e=>(0,$.jsx)(`li`,{className:`sfolder`,children:(0,$.jsxs)(`button`,{type:`button`,className:`pane sfolder-open${o?.(e)?` off`:``}`,disabled:o?.(e),onClick:()=>a?.(e),children:[(0,$.jsx)(bf,{name:If(e)}),(0,$.jsx)(`span`,{className:`sfolder-name`,children:e.name}),(0,$.jsx)(`span`,{className:`sfolder-meta`,children:Rf(e)})]})},e.id))}):(0,$.jsx)(W,{className:`slist slist-folders`,label:`Sources`,rows:Ot,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${o?.(e)?` off`:``}`,disabled:o?.(e),onClick:()=>a?.(e),children:[(0,$.jsx)(bf,{name:If(e)}),e.name]})},{key:`type`,header:`Type`,sort:e=>Rf(e),cell:e=>Rf(e)},{key:`files`,header:`Items`,className:`num`,cell:e=>e.files},{key:`size`,header:`Size`,className:`num`,sort:e=>e.bytes,cell:e=>$l(e.bytes)}]})]});if(i===`pick`)return Mt();if(e){if(!v)return(0,$.jsx)(`div`,{className:`sources`,children:(0,$.jsx)(Bt,{label:`Files`,error:Le,onRetry:()=>He(e=>e+1)})});let e=df[v.type]?.Level,r=!e&&!v.system,i=()=>t?.(null),a=(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(n,{kind:`Source`,name:v.name,onPick:p?.length?()=>j(!0):void 0,picking:A,meta:Rf(tu(v)),primary:r?{label:`Upload…`,icon:u,onSelect:()=>Ue.current?.click()}:void 0,actions:bt(tu(v),i)}),A&&p&&(0,$.jsx)(h,{kind:`Source`,value:v.id,onPick:e=>t?.(e),onClose:()=>j(!1),options:p.map(e=>({id:e.id,label:e.name}))}),vt,Et]});return e?(0,$.jsxs)(`div`,{className:`sources`,children:[a,(0,$.jsx)(e,{source:v,onBack:i,note:he?.id===v.id?he.text:void 0}),(0,$.jsx)(Qt,{undo:l.undo,onUndo:l.undoNow,onDismiss:l.dismiss})]}):(0,$.jsxs)(`div`,{className:`sources`,"data-level":`files`,children:[a,(0,$.jsx)(lt,{said:Te}),(0,$.jsxs)(Tt,{id:`source-files`,title:`Files`,note:String(v.files.length),children:[v.files.length>0&&(0,$.jsx)(xt,{noun:`files`,search:{label:`Find a file`,value:C,placeholder:`Name`,onChange:T},filters:[{key:`sort`,label:`Sort`,value:Ke,neutral:`name`,options:Bl,onChange:e=>S(t=>({...t,sort:e,dir:Bl.find(t=>t.value===e)?.dir??1}))}],trail:At,count:{shown:rt.length,total:v.files.length}}),Ge===`list`&&E.size>0&&(0,$.jsx)(V,{count:E.size,noun:`file`,onClear:at,actions:Ct}),(0,$.jsx)(`div`,{className:`pane sfiles${je?` drag`:``}`,onDragOver:e=>{e.preventDefault(),Me(!0)},onDragLeave:()=>Me(!1),onDrop:wt,children:rt.length===0?(0,$.jsx)(`p`,{className:`unset`,children:C?`(No matches)`:`(No files)`}):Ge===`thumbs`?(0,$.jsx)(`ul`,{className:`stiles`,children:rt.map(e=>(0,$.jsxs)(`li`,{className:`stile-cell`,children:[(0,$.jsxs)(`button`,{type:`button`,className:`stile`,title:e.name,"aria-label":e.name,onClick:()=>ee(e),children:[(0,$.jsx)(Tf,{id:v.id,file:e,text:!!K().SOURCE_TYPES[v.type]?.itemText?.(e.name)}),(0,$.jsx)(`span`,{className:`stile-name`,children:e.name})]}),(0,$.jsx)(`span`,{className:`stile-menu`,children:(0,$.jsx)($e,{label:`${e.name} actions`,actions:St(e)})})]},e.name))}):(0,$.jsx)(W,{className:`slist`,label:`${v.name} files`,rows:rt,rowKey:e=>e.name,sort:{key:Ke,dir:qe===1?`asc`:`desc`},onSort:e=>S(t=>({...t,sort:e?.key??`name`,dir:e?.dir===`desc`?-1:1})),select:{noun:`file`,selected:E,onChange:D},rowProps:e=>({onClick:()=>it(e.name)}),columns:[{key:`name`,header:`Name`,className:`sfile-name`,role:`title`,sort:e=>e.name,cell:e=>e.name},{key:`type`,header:`Type`,className:`stype`,sort:e=>Af(e.name),cell:e=>Af(e.name)},{key:`size`,header:`Size`,className:`num`,sort:e=>e.bytes,cell:e=>$l(e.bytes)},{key:`added`,header:`Added`,sort:e=>e.added,cell:e=>e.added?eu(e.added):(0,$.jsx)(`span`,{className:`unset`,children:`(Unknown)`})},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,tdProps:()=>({onClick:e=>e.stopPropagation()}),cell:e=>(0,$.jsx)($e,{label:`${e.name} actions`,actions:St(e)})}]})})]}),r&&(0,$.jsxs)(`div`,{className:`supload`,children:[(0,$.jsx)(`input`,{ref:Ue,type:`file`,multiple:!0,hidden:!0,"aria-hidden":`true`,accept:K().sourceTypeOf(v)?.uploads?.join(`,`),onChange:e=>{e.target.files&&mt(e.target.files),e.target.value=``}}),De.length>0&&(0,$.jsx)(`ul`,{className:`suploads`,children:De.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)(Bt,{inline:!0,label:`Uploading…`}),e.state===`done`&&(0,$.jsx)(`span`,{className:`note`,children:`Uploaded`}),e.state===`error`&&(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(pt,{tone:`error`,inline:!0,children:e.error}),(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>ht(e),children:`Retry`})]})]},e.id))})]}),I&&(0,$.jsxs)(Ae,{title:I.name,onClose:Je,wide:!!K().documentTypeOf(I.name),dismiss:`Close`,children:[(0,$.jsx)(Of,{id:v.id,file:I}),(0,$.jsx)(`p`,{className:`note`,children:$l(I.bytes)})]}),L&&(0,$.jsx)(Ae,{title:L.kind===`move`?`Move to…`:`Copy to…`,onClose:Xe,dismiss:`Cancel`,children:(0,$.jsx)(xf,{mode:`pick`,disabled:e=>e.id===v.id||e.system||e.type!==v.type,onPick:e=>void gt(L.kind,e)})}),H&&(0,$.jsxs)(Ae,{title:`New Source from selection`,onClose:Ze,dirty:!!U.trim(),initialFocus:`input[aria-label="New Source name"]`,onEnter:U.trim()&&!se?()=>void _t():void 0,children:[(0,$.jsxs)(`label`,{className:`prompt`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:`Name`}),(0,$.jsx)(`input`,{"aria-label":`New Source name`,value:U,onChange:e=>ae(e.target.value)})]}),(0,$.jsxs)(`div`,{className:`sacts`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:()=>ie(!1),children:`Cancel`}),(0,$.jsxs)(`button`,{type:`button`,disabled:!U.trim()||se,"aria-busy":se||void 0,onClick:()=>void _t(),children:[se&&(0,$.jsx)(kt,{}),`Make`]})]})]}),(0,$.jsx)(Qt,{undo:l.undo,onUndo:l.undoNow,onDismiss:l.dismiss})]})}let Nt=df[Ff(G,Ce)]?.Create,Pt={key:`type`,label:`Type`,control:{type:`cards`,value:G,onChange:e=>{ue(e),fe(null)},options:Pf(Ce).map(e=>({...e,icon:(0,$.jsx)(bf,{name:e.icon})}))}},Ft=Nt?!!de?.enabled&&!de.busy:!!U.trim()&&!se,It=()=>{Nt?de?.enabled&&de.run():q()};return(0,$.jsxs)(`div`,{className:`sources`,"data-level":`sources`,children:[(0,$.jsx)(lt,{said:Te}),(0,$.jsxs)(Tt,{id:`library-sources`,title:`Sources`,children:[p!==null&&p.length>0&&jt(At),p===null?(0,$.jsx)(Bt,{label:`Sources`,error:Fe,onRetry:()=>{Ie(null),Be(e=>e+1)}}):p.length===0?(0,$.jsx)(`p`,{className:`unset`,children:`(No Sources)`}):Ge===`thumbs`?(0,$.jsx)(`ul`,{className:`sfolders`,children:Ot.map(e=>(0,$.jsxs)(`li`,{className:`sfolder`,children:[(0,$.jsxs)(`button`,{type:`button`,className:`pane sfolder-open`,onClick:()=>t?.(e.id),children:[(0,$.jsx)(bf,{name:If(e)}),(0,$.jsx)(`span`,{className:`sfolder-name`,children:e.name}),(0,$.jsx)(`span`,{className:`sfolder-meta`,children:Rf(e)}),Mf(e)&&(0,$.jsxs)(Nf,{source:e,cap:g,children:[(0,$.jsx)(`span`,{className:`squota-bar`,children:(0,$.jsx)(`span`,{className:`squota-fill`,style:{width:`${jf(e.bytes,g)}%`}})}),(0,$.jsx)(`span`,{className:`squota-pct`,children:`${jf(e.bytes,g)}%`})]})]}),(0,$.jsx)(`span`,{className:`sfolder-menu`,children:(0,$.jsx)($e,{label:`${e.name} actions`,actions:bt(e)})})]},e.id))}):(0,$.jsx)(`div`,{className:`pane sfiles`,children:(0,$.jsx)(W,{className:`slist slist-folders`,label:`Sources`,rows:Ot,rowKey:e=>e.id,rowProps:e=>({"data-open":e.id,onClick:n=>{n.target.closest(`button, a, input, .menu`)||t?.(e.id)}}),select:{noun:`Source`,actions:e=>[{label:`Delete`,icon:ge,destructive:!0,disabled:e.every(e=>e.system),onSelect:()=>void st(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)(bf,{name:If(e)}),(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>t?.(e.id),children:e.name})]})},{key:`type`,header:`Type`,sort:e=>Rf(e),cell:e=>Rf(e)},{key:`files`,header:`Items`,className:`num`,cell:e=>e.files},{key:`size`,header:`Size`,className:`num`,sort:e=>e.bytes,cell:e=>$l(e.bytes)},{key:`used`,header:`Used`,className:`num`,sort:e=>Mf(e)?e.bytes:null,cell:e=>Mf(e)?(0,$.jsx)(Nf,{source:e,cap:g,children:(0,$.jsx)(`span`,{className:`squota-pct`,children:`${jf(e.bytes,g)}%`})}):(0,$.jsx)(`span`,{className:`unset`,children:`(n/a)`})},{key:`menu`,header:``,className:`s-actions`,role:`menu`,sort:null,tdProps:()=>({onClick:e=>e.stopPropagation()}),cell:e=>(0,$.jsx)($e,{label:`${e.name} actions`,actions:bt(e)})}]})}),vt,(0,$.jsx)(`div`,{className:`sfoot`,children:(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>P(!0),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),` Add source`]})})]}),N&&(0,$.jsxs)(Ae,{title:`Add source`,onClose:Ye,dirty:!!U.trim(),className:`edit sadd`,initialFocus:`.prop[data-key="name"] input`,onEnter:Ft?It:void 0,footer:(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:Ye,children:`Cancel`}),(0,$.jsxs)(`button`,{type:`button`,disabled:Nt?!de?.enabled:!U.trim()||se,"aria-busy":(Nt?de?.busy:se)||void 0,onClick:It,children:[(Nt?de?.busy:se)&&(0,$.jsx)(kt,{}),`Add source`]})]}),children:[Nt?(0,$.jsx)(Nt,{typeRow:Pt,which:G,bind:fe,onMade:async(e,n)=>{P(!1),ue(K().DEFAULT_SOURCE_TYPE),await Qe(),n&&(_e({id:e.id,text:n}),t?.(e.id))}}):(0,$.jsx)(w,{label:`New Source`,rows:[Pt,{key:`name`,label:`Name`,width:`full`,control:{type:`text`,value:U,onChange:e=>{ae(e),me(null)}}}]}),!Nt&&pe&&(0,$.jsx)(pt,{tone:`error`,children:pe})]}),Et,(0,$.jsx)(Qt,{undo:l.undo,onUndo:l.undoNow,onDismiss:l.dismiss})]})}var Sf=`evals-lab.sources.adding`;function Cf(){try{return sessionStorage.getItem(Sf)}catch{return null}}function wf(e){try{e?sessionStorage.setItem(Sf,e):sessionStorage.removeItem(Sf)}catch{}}function Tf({id:e,file:t,text:n}){let[r,i]=(0,Y.useState)(!1),a=Df(n?e:null,t,Ef);return K().documentTypeOf(t.name)?(0,$.jsxs)(`span`,{className:`stile-type`,children:[(0,$.jsx)(nr,{size:32,"aria-hidden":`true`}),Af(t.name)]}):a?(0,$.jsx)(`span`,{className:`stile-text`,"aria-hidden":`true`,children:a}):!t.thumb||r?(0,$.jsx)(`span`,{className:`stile-type`,children:Af(t.name)||`FILE`}):(0,$.jsx)(`img`,{className:`stile-img`,src:Yl(e,t),alt:``,loading:`lazy`,onError:()=>i(!0)})}var Ef=600;function Df(e,t,n){let r=X(),[i,a]=(0,Y.useState)(null);return(0,Y.useEffect)(()=>{if(!e)return;let i=!0;return Promise.resolve().then(()=>r.file(e,t.name)).then(e=>(n?e.slice(0,n):e).text()).then(e=>{i&&a(e)},()=>{}),()=>{i=!1}},[r,e,t.name,t.v,n]),i}function Of({id:e,file:t}){let n=X(),r=Jl(t.name),i=r?void 0:K().documentTypeOf(t.name)?.media,[a,o]=(0,Y.useState)(null),[s,c]=(0,Y.useState)(null),[l,u]=(0,Y.useState)(0);return(0,Y.useEffect)(()=>{if(r)return;let a=null,s=!0;return o(null),c(null),n.file(e,t.name).then(async e=>{if(!s)return;if(i){a=URL.createObjectURL(new Blob([e],{type:i})),o({url:a});return}let t=await e.text();s&&o({text:t})}).catch(e=>{s&&c(Q(e,`the request failed`))}),()=>{s=!1,a&&URL.revokeObjectURL(a)}},[n,e,t.name,t.v,r,i,l]),r?(0,$.jsx)(`img`,{className:`spreview-img`,src:Xl(e,t),alt:t.name}):a&&`url`in a?(0,$.jsx)(`iframe`,{className:`spreview-doc`,src:a.url,title:t.name}):a?(0,$.jsx)(`pre`,{className:`mono-block spreview-text`,tabIndex:0,children:a.text}):(0,$.jsx)(Bt,{error:s,onRetry:()=>u(e=>e+1)})}function kf({view:e,onChange:t}){return(0,$.jsx)(re,{label:`View`,value:e,onChange:e=>t(e),options:[{value:`thumbs`,label:`Thumbnails`},{value:`list`,label:`List`}]})}function Af(e){let t=e.lastIndexOf(`.`);return t<0?``:e.slice(t+1).toUpperCase()}function jf(e,t){return t<=0?0:Math.max(0,Math.min(100,Math.round(e/t*100)))}var Mf=e=>!e.system&&!df[e.type]?.Level;function Nf({source:e,cap:t,children:n}){let r=`Quota: ${$l(e.bytes)} of ${$l(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 Pf=(e=[])=>Object.entries(K().SOURCE_TYPES).flatMap(([t,n])=>n.platforms?n.platforms.flatMap(e=>{let n=K().WORKFLOW_PLATFORMS[e];return n?[{value:e,label:n.label,icon:yf(t,e)}]:[]}):n.providers?e.map(e=>({value:e.id,label:e.label,icon:yf(t,void 0,e.id),...e.enrolled?{}:{disabled:!0,note:e.note??`set by the server`}})):[{value:t,label:n.label,icon:yf(t)}]),Ff=(e,t=[])=>{if(e in K().SOURCE_TYPES)return e;let n=Object.entries(K().SOURCE_TYPES).find(([,t])=>t.platforms?.includes(e))?.[0];if(n)return n;if(t.some(t=>t.id===e)||e in K().DYNAMIC_PROVIDERS){let e=Object.entries(K().SOURCE_TYPES).find(([,e])=>e.providers)?.[0];if(e)return e}return e},If=e=>yf(e.type,e.platform,e.provider),Lf=e=>e.provider??e.platform??e.type,Rf=e=>e.provider?K().DYNAMIC_PROVIDERS[e.provider]?.label??K().SOURCE_TYPES[e.type]?.label??e.type:e.platform?K().WORKFLOW_PLATFORMS[e.platform]?.label??K().SOURCE_TYPES[e.type]?.label??e.type:K().SOURCE_TYPES[e.type]?.label??e.type;function zf({sourceId:e}){let[t,n]=(0,Y.useState)(e);return(0,Y.useEffect)(()=>n(e),[e]),(0,$.jsx)(xf,{sourceId:t,onOpen:e=>{n(e),Ei({tab:`library`,section:`sources`,id:e})},onGone:()=>Di({tab:`library`,section:`sources`,id:null})})}var Bf=[{id:`datasets`,label:`Evals`,icon:Me},{id:`filters`,label:`Filter sets`,icon:E},{id:`prompts`,label:`Prompts`,icon:k},{id:`sources`,label:`Sources`,icon:Ee}];function Vf({section:e,id:t}){return(0,$.jsxs)(`div`,{className:`library`,children:[(0,$.jsx)(en,{label:`Library`,level:`section`,className:`lib-sections`,current:e,items:Bf.map(({id:e,label:t,icon:n})=>({key:e,label:t,href:Ti({tab:`library`,section:e,id:null}),icon:n}))}),(0,$.jsx)(`section`,{className:`lib-section`,"data-section":e,children:e===`datasets`?(0,$.jsx)(bc,{groupId:t}):e===`filters`?(0,$.jsx)(Dl,{setId:t}):e===`prompts`?(0,$.jsx)(zl,{promptId:t}):(0,$.jsx)(zf,{sourceId:t})})]})}function Hf(e,t){e.focus(t),e.setAttribute(`data-focus-ring`,``);let n=()=>{e.removeAttribute(`data-focus-ring`),e.removeEventListener(`blur`,n),document.removeEventListener(`pointerdown`,n,!0)};e.addEventListener(`blur`,n),document.addEventListener(`pointerdown`,n,!0)}var Uf=K().WIZARDS,Wf=K().wizardProgress;function Gf(e){return K().targetsOf(e).some(e=>e.steps?.some(e=>e.type===`recorded`))}K().WIZARDS[`test-workflow`]={id:`test-workflow`,title:`Test a Power Automate workflow`,blurb:`Vary a workflow action's request and grade the responses 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 items.`,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 Create test… on the action that calls the model.`,where:`#/library/sources`,done:e=>e.pipelines.some(e=>Gf(e.work))},{id:`run`,title:`Run the test`,detail:`On Experiments, change the editable variant's request and run it.`,where:`#/runs`,focus:`.action-bar-primary`,done:e=>e.runs.some(e=>Gf(e.snapshot))},{id:`compare`,title:`Compare the responses`,detail:`Open the finished run and read the two responses side by side.`,where:`#/history`,done:e=>e.runs.some(e=>e.status===`done`&&Gf(e.snapshot))}]};var Kf=(0,Y.createContext)(null);function qf({children:e}){let t=X(),[n,r]=Z(`promptlab.wizard`),[i]=Z(`promptlab.connections`),[a]=Z(`promptlab.workflows`),[o,s]=(0,Y.useState)([]),[c,l]=(0,Y.useState)([]),u=(0,Y.useRef)(!!n);u.current=!!n,(0,Y.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),Bn.addEventListener(`write`,r),()=>{e=!1,window.removeEventListener(`hashchange`,r),Bn.removeEventListener(`write`,r)}},[t,n?.id]);let d=(0,Y.useMemo)(()=>({profiles:i.list,pipelines:a.list,sources:o,runs:c}),[i.list,a.list,o,c]),f=(0,Y.useMemo)(()=>Object.values(Uf),[]),p=n?Uf[n.id]??null:null,m=(0,Y.useCallback)(e=>{Uf[e]&&r({id:e})},[r]),h=(0,Y.useCallback)(()=>r(null),[r]),g=(0,Y.useCallback)(e=>{location.hash!==e.where&&(location.hash=e.where),e.focus&&Xf(e.focus)},[]),_=(0,Y.useMemo)(()=>({entries:f,active:p,lab:d,start:m,cancel:h,goToStep:g}),[f,p,d,m,h,g]);return(0,$.jsx)(Kf.Provider,{value:_,children:e})}function Jf(){let e=(0,Y.useContext)(Kf);if(!e)throw Error(`useWizard outside a WizardProvider: the app mounts one at its root`);return e}function Yf(){let{active:e,lab:t}=Jf();return(0,Y.useMemo)(()=>e?{entry:e,...Wf(e,t)}:null,[e,t])}function Xf(e,t=20){let n=document.querySelector(e);if(n){Hf(n),n.scrollIntoView?.({block:`center`,behavior:`smooth`});return}t>0&&requestAnimationFrame(()=>Xf(e,t-1))}var Zf=e=>({tab:`home`,guide:e});function Qf(e,t){return e.some(e=>{let n=Wf(e,t);return n.total>0&&n.done===n.total})}function $f(){let{entries:e,lab:t,start:n}=Jf(),[r,i]=Z(`promptlab.guidesDismissed`);return r||e.length===0||Qf(e,t)?null:(0,$.jsxs)(`section`,{className:`home-guides`,"aria-label":`Guides`,children:[(0,$.jsxs)(`div`,{className:`home-guides-head`,children:[(0,$.jsx)(`h2`,{className:`home-guides-title`,children:`Guides`}),(0,$.jsx)(`button`,{type:`button`,className:`icon-btn home-guides-dismiss`,"aria-label":`Dismiss guides`,onClick:()=>i(!0),children:(0,$.jsx)(_n,{size:16,"aria-hidden":`true`})})]}),(0,$.jsx)(`p`,{className:`note`,children:`Step-by-step walk-throughs.`}),(0,$.jsx)(W,{className:`wiz-table`,cards:!0,label:`Guides`,rows:e,rowKey:e=>e.id,columns:[{key:`name`,header:`Guide`,role:`title`,cell:e=>(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`a`,{className:`link-btn wiz-list-name`,href:Ti(Zf(e.id)),children:e.title}),(0,$.jsx)(`span`,{className:`note`,children:e.blurb})]})},{key:`progress`,header:`Steps`,sort:e=>Wf(e,t).done,cell:e=>{let n=Wf(e,t);return`${n.done}/${n.total}`}},{key:`start`,header:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)(an,{label:`${e.title} actions`,actions:[{label:`Start`,icon:oe,onSelect:()=>{n(e.id),Ei(Zf(e.id))}}]})}]})]})}function ep({guideId:e}){let{entries:t,active:r,lab:i,start:a,cancel:o,goToStep:s}=Jf(),c=Os(),l=t.find(t=>t.id===e)??null;if((0,Y.useEffect)(()=>{l||Di(Zf(null))},[e,l]),!l)return null;let u=l.id===r?.id,{ticks:d,done:f,total:p,current:m}=Wf(l,i),h=m>=0?l.steps[m]:null,g=m<0;return(0,$.jsxs)(`div`,{className:`guide-view`,children:[(0,$.jsx)(n,{kind:`Guide`,name:l.title,meta:`${f} of ${p} complete`,actions:[c(Zf(l.id))],docked:!0}),u?(0,$.jsx)(Sn,{primary:{label:`Next step`,icon:qt,onSelect:()=>h&&s(h),disabled:g},status:g?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`b`,{children:`All steps done.`}),` Cancel to close it.`]}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(`b`,{children:[`Step `,m+1,` of `,p]}),` · `,h.title]}),secondary:{label:`Cancel guide`,icon:g?_n:_,onSelect:o}}):(0,$.jsx)(Sn,{primary:{label:`Start`,icon:oe,onSelect:()=>a(l.id)},status:g?(0,$.jsx)(`b`,{children:`All steps done.`}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(`b`,{children:[`Step `,m+1,` of `,p]}),` · `,h.title]})}),(0,$.jsx)(Tt,{id:`home-guide-steps`,title:l.title,note:l.blurb,children:(0,$.jsx)(W,{className:`wiz-table`,cards:!0,label:`${l.title} steps`,rows:l.steps.map((e,t)=>({...e,n:t,state:d[t]?`done`:t===m?`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)(an,{label:`${e.title} actions`,actions:[{label:`Go to step`,icon:L,onSelect:()=>s(e)}]})}]})})]})}var tp=`0.16.0 (2026.10.16-944)`,np=3,rp=25,ip=3e3,ap=e=>ss.find(t=>t.id===e),op=e=>Bf.find(t=>t.id===e);function sp({guide:e=null}){return e?(0,$.jsx)(ep,{guideId:e}):(0,$.jsx)(cp,{})}function cp(){let e=X(),[t]=Z(`promptlab.connections`),n=(0,Y.useMemo)(()=>t.list.filter(e=>K().listedConnection(e)),[t.list]),[r,i]=Z(`promptlab.workflows`),a=$r(),o=ci(),{pickDataset:s}=Xi(),{openRun:c}=$i(),l=Qr(),u=si(),[d,f]=(0,Y.useState)(null),[p,m]=(0,Y.useState)(null),[h,g]=(0,Y.useState)(null),[_,v]=(0,Y.useState)(null),[y,b]=(0,Y.useState)(0),[x,S]=(0,Y.useState)(0),C=e=>e instanceof Error?e.message:String(e);(0,Y.useEffect)(()=>{let t=!0;return g(null),e.sources().then(e=>{t&&f(e.sources)},e=>{t&&g(C(e))}),()=>{t=!1}},[e,y]),(0,Y.useEffect)(()=>{let t=!0,n;v(null);let r=()=>e.queue({limit:rp}).then(e=>{t&&(m(e.runs),e.runs.some(e=>e.status===`queued`||e.status===`running`)&&(n=setTimeout(()=>void r(),ip)))},e=>{t&&v(C(e))});return r(),()=>{t=!1,n&&clearTimeout(n)}},[e,x]);let w=(0,Y.useMemo)(()=>r.list.filter(e=>e.work?.version===20),[r.list]),T=(0,Y.useMemo)(()=>Jo(w),[w]),E=a.list??[],D=E.reduce((e,t)=>e+t.cases,0),O=n.length===0?`setup`:a.list===null?null:E.length===0||D===0?`datasets`:p!==null&&p.length===0?`runs`:null,k=(...e)=>e.filter(Boolean).join(`, `)||void 0,A=ap(`library`),j=A.icon;return(0,$.jsxs)(`div`,{className:`home`,children:[(0,$.jsxs)(`div`,{className:`pane home-pane`,children:[(0,$.jsx)($f,{}),(0,$.jsxs)(`ol`,{className:`home-line`,"aria-label":`Lab`,children:[(0,$.jsx)(lp,{id:`runs`,icon:ap(`runs`).icon,name:ap(`runs`).label,hub:!O||O===`runs`,start:O===`runs`,fact:k(fa(w.length,`experiment`)),total:w.length,items:w.map(e=>({key:e.id,label:e.name,title:e.name,href:Ti({tab:`runs`,id:e.id}),onPick:()=>i({...r,at:e.id})}))}),(0,$.jsx)(lp,{id:`history`,icon:ap(`history`).icon,name:ap(`history`).label,fact:p&&p.length===0?`(No runs)`:void 0,wait:p?void 0:{error:_,onRetry:()=>S(e=>e+1)},items:[],children:p&&p.length>0&&(0,$.jsx)(up,{runs:p,choices:T,onOpen:e=>c(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)(j,{size:18})}),A.label]}),(0,$.jsxs)(`ol`,{className:`home-branch`,"aria-label":A.label,children:[(0,$.jsx)(lp,{id:`datasets`,to:`library/datasets`,branch:!0,icon:op(`datasets`).icon,name:op(`datasets`).label,start:O===`datasets`,fact:a.list?k(fa(E.length,`eval`),fa(D,`eval case`)):void 0,total:E.length,wait:a.list?void 0:{error:a.error,onRetry:()=>void l.refresh()},items:E.map(e=>({key:e.id,label:e.name,title:e.name,sub:fa(e.cases,`eval case`),href:Ti({tab:`library`,section:`datasets`,id:e.id}),onPick:()=>s(e.id)}))}),(0,$.jsx)(lp,{id:`prompts`,to:`library/prompts`,branch:!0,icon:op(`prompts`).icon,name:op(`prompts`).label,fact:o.list?k(fa(o.list.length,`prompt`)):void 0,total:o.list?.length??0,wait:o.list?void 0:{error:o.error,onRetry:()=>void u.refresh()},items:(o.list??[]).map(e=>{let t=ti(e);return{key:e.id,label:t,title:t,sub:e.default?`Default`:void 0,href:Ti({tab:`library`,section:`prompts`,id:e.id})}})}),(0,$.jsx)(lp,{id:`sources`,to:`library/sources`,branch:!0,icon:op(`sources`).icon,name:op(`sources`).label,fact:d?k(fa(d.length,`source`)):void 0,total:d?.length??0,wait:d?void 0:{error:h,onRetry:()=>b(e=>e+1)},items:(d??[]).map(e=>({key:e.id,label:e.name,title:e.name,sub:fa(e.files,Et(e)),href:Ti({tab:`library`,section:`sources`,id:e.id})}))})]})]}),(0,$.jsx)(lp,{id:`setup`,icon:ap(`setup`).icon,name:ap(`setup`).label,start:O===`setup`,fact:k(fa(n.length,`connection`)),total:n.length,items:n.map(e=>({key:e.id,label:e.name||`Untitled`,title:e.name||`Untitled`,sub:e.model,href:Ti({tab:`setup`,section:`connections`,id:e.id})}))})]})]}),(0,$.jsxs)(`p`,{className:`home-version`,children:[`Evals Lab `,tp]})]})}function lp({id:e,icon:t,name:n,to:r,fact:i,items:a,total:o=a.length,hub:s,start:c,branch:l,wait:u,children:d}){let f=o-Math.min(a.length,np);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)(`a`,{className:`home-start`,href:`#/${r??e}`,children:(0,$.jsx)(Ye,{tone:`amber`,children:`Start Here`})}),u&&(0,$.jsx)(Bt,{label:n,error:u.error,onRetry:u.onRetry}),!u&&i&&(0,$.jsx)(`span`,{className:`home-fact`,children:i}),!u&&d,!u&&a.length>0&&(0,$.jsxs)(`ul`,{className:`home-items`,children:[a.slice(0,np).map(e=>(0,$.jsx)(`li`,{children:(0,$.jsxs)(`a`,{href:e.href,title:e.title,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)),f>0&&(0,$.jsx)(`li`,{children:(0,$.jsxs)(`a`,{className:`home-more`,href:`#/${r??e}`,children:[f,` more`]})})]})]})}function up({runs:e,choices:t,onOpen:n}){let r=[],i=new Set;for(let n of e){let e=Yo(t,n),a=e?e.id:n.snapshot?.id?`${n.snapshot.id}\0${n.snapshot.name??``}`:n.id;if(i.has(a))continue;i.add(a);let o=t.filter(e=>e.doc===n.snapshot?.id).length>1;r.push({row:n,to:e?.id??(o?null:n.snapshot?.id??null)})}return(0,$.jsx)(`ul`,{className:`home-compare`,children:r.slice(0,np).map(({row:e,to:t})=>{let r=vo(e),i=e.results||[],a=Ka(i,Ga(e)),o=Math.max(e.progress?.total??0,i.length),s=e.snapshot?.name||`Untitled`,c=e.status===`queued`||e.status===`running`,l=Ti({tab:`results`,id:e.id});return(0,$.jsxs)(`li`,{children:[(0,$.jsxs)(`div`,{className:`home-compare-head`,children:[t?(0,$.jsx)(`a`,{href:Ti({tab:`history`,section:`overview`,pipeline:t}),title:s,children:s}):(0,$.jsx)(`span`,{title:s,children:s}),c&&(0,$.jsx)(Ye,{tone:`amber`,children:`Running`}),(0,$.jsx)(`a`,{className:`home-compare-run`,href:l,title:`${Be(e.submittedAt)}, ${da(e.submittedAt)}`,onClick:t=>{t.preventDefault(),n(e)},children:dt(e.submittedAt)})]}),r.scenarios.map((t,r)=>{let s=Array.from({length:o},(e,t)=>{let n=i[t],o=n&&!n.unrun?a[r]?.[t]??null:null,s=n?.name||`Item ${t+1}`;return{state:o==null?`none`:o?`pass`:`fail`,title:`${s}, ${o==null?n&&!n.unrun?`not graded`:`not run yet`:o?`passed`:`failed`}`}}),c=s.filter(e=>e.state===`pass`).length,u=s.filter(e=>e.title.endsWith(`not run yet`)).length;return(0,$.jsxs)(`div`,{className:`home-compare-row`,children:[(0,$.jsx)(`span`,{className:`home-compare-name`,title:t.name,children:t.name}),(0,$.jsx)(It,{className:`sm fit`,label:`${t.name}: ${c} of ${o} passed${u?`, ${u} not run yet`:``}`,cells:s,href:l,onOpen:()=>n(e)}),(0,$.jsx)(`span`,{className:`home-compare-n`,children:(0,$.jsx)(ui,{n:c,of:o})})]},t.id??r)})]},e.id)})})}function dp({list:e,at:t,canRestore:r,onSelect:i,onNew:a,onClone:o,link:c,onCopyMove:l,onRename:d,onDelete:f,onRestore:p,onExport:m,onExportCi:g,onImport:_,used:v={}}){let[y,x]=(0,Y.useState)(!1),[S,C]=(0,Y.useState)(!1),w=e.find(e=>e.id===t),T=[{label:`Rename…`,icon:b,onSelect:()=>x(!0)},{label:`Clone`,icon:ke,onSelect:o},c,{label:`Copy or move…`,icon:J.transfer,onSelect:l},{label:`Export YAML`,icon:M,onSelect:m},{label:`Export for CI…`,icon:M,onSelect:g},{label:`Import YAML…`,icon:u,onSelect:_},{label:`Restore…`,icon:s,disabled:!r,onSelect:p},{label:`Delete`,icon:ge,destructive:!0,onSelect:f}],E=K().jobsOf(w?.work).length,D=K().targetsOf(w?.work).length;return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(n,{kind:`Experiment`,kindId:`Pipeline`,name:w?.name?.trim()||`Untitled`,onPick:()=>C(!0),picking:S,meta:`${pp(E,`request`)}, ${pp(D,`variant`)}`,primary:{label:`New experiment`,icon:ne,onSelect:a},actions:T,docked:!0}),S&&(0,$.jsx)(h,{kind:`Experiment`,value:t,onPick:i,onClose:()=>C(!1),options:e.map(e=>{let t=v[e.id];return{id:e.id,label:e.name||`Untitled`,used:t?{at:t,label:Be(t)}:null}}),recent:e.filter(e=>v[e.id]).sort((e,t)=>v[t.id].localeCompare(v[e.id])).slice(0,fp).map(e=>e.id)}),y&&(0,$.jsx)(sn,{title:`Rename experiment`,value:w?.name??``,onCommit:d,onClose:()=>x(!1)})]})}var fp=5,pp=(e,t)=>`${e} ${e===1?t:`${t}s`}`;function mp({hasItems:e,onExport:t,onClose:n}){let[r,i]=(0,Y.useState)(!1),[a,o]=(0,Y.useState)(!1),[s,c]=(0,Y.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)(De,{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:`(No files)`,none:!0}}]}],actions:[{label:`Export`,icon:M,busy:a,primary:!0,onSelect:()=>void l()}]})}function hp({problems:e,error:t,run:n,submitting:r,stopping:i=!1,onRun:a,onStop:o,onOpen:s,onShare:c}){let l=!!n&&!La(n.status),u=l&&(i||Ra(n)),d=n?.progress?.total??0,f=n?.progress?.n??0,p=l&&d?Math.round(f/d*100):null,m=n&&(0,$.jsxs)(`button`,{type:`button`,className:`runbar-open`,title:`Open this run`,onClick:s,children:[(0,$.jsx)(`span`,{className:`runbar-word`,children:u?`Stopping`:ga(n.status)}),gp(n)&&(0,$.jsx)(`span`,{className:`runbar-detail`,children:gp(n)})]}),h=l?m:e.length?(0,$.jsxs)(`span`,{className:`runbar-why`,children:[bp(e[0]),yp(e[0])]}):t?(0,$.jsxs)(`span`,{className:`runbar-why bad`,children:[bp(t),c&&(0,$.jsxs)($.Fragment,{children:[` `,(0,$.jsx)(`button`,{type:`button`,className:`link-btn runbar-fix`,onClick:c,children:`Share`})]})]}):m??(0,$.jsx)(`span`,{className:`runbar-word`,children:`Ready to run`});return(0,$.jsx)(Sn,{className:`runbar`,primary:{label:`Run`,icon:oe,onSelect:a,disabled:e.length>0,busy:r},status:h,secondary:l?{label:u?`Stopping…`:`Stop`,icon:_,onSelect:o,busy:u}:void 0,progress:p,progressLabel:`Run progress`})}function gp(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=xa(e.startedAt,e.finishedAt);return t==null?String(e.progress.total):`${e.progress.total} in ${ba(t)}`}return``}var _p=[[/^(?:Connection(?: Profile)?|Grader) .* not found$/,`#/setup`,`Open\xA0Setup`],[/^Source .* not found$/,`#/library/sources`,`Open\xA0Sources`],[/^(?:Dataset|Eval group|Eval) .* not found$/,`#/library/datasets`,`Open\xA0Evals`]],vp=e=>_p.find(([t])=>t.test(e.trim()))?.[1];function yp(e){let t=_p.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 bp=e=>{let t=e.trim();if(!t)return t;let n=t[0].toUpperCase()+t.slice(1);return/[.!?]$/.test(n)?n:`${n}.`},xp=10;function Sp({onPick:e,onClose:t,used:n}){let{list:r,error:i}=ci(),a=si(),[o,s]=(0,Y.useState)(``),[c,l]=(0,Y.useState)(`all`),[u,d]=(0,Y.useState)(0),f=r??[],p=f.filter(e=>jl(e,o,c,n??{byScenario:new Set,byAll:new Set})).sort((e,t)=>Number(t.default)-Number(e.default)),m=Math.ceil(p.length/xp),h=Math.min(u,Math.max(0,m-1)),g=p.slice(h*xp,(h+1)*xp),_=o.trim()?g[0]:void 0,v=(0,Y.useId)();return(0,$.jsxs)(Ae,{title:`Select prompt`,onClose:t,className:`sp`,initialFocus:`.sp input[type=search]`,onEnter:_&&(()=>e(_)),dismiss:`Cancel`,children:[(0,$.jsx)(xt,{noun:`prompts`,search:{label:`Search prompts`,placeholder:`Search prompts`,value:o,onChange:e=>{s(e),d(0)},describedBy:_?v:void 0,onKeyDown:wt(v)},filters:[{key:`show`,label:`Show`,value:c,onChange:e=>{l(e),d(0)},options:kl}],count:{shown:p.length,total:f.length}}),r===null?(0,$.jsx)(Bt,{label:`Prompts`,error:i,onRetry:()=>void a.refresh()}):f.length===0?(0,$.jsx)(`p`,{className:`unset`,children:`(No prompts)`}):p.length===0?(0,$.jsx)(`p`,{className:`unset`,children:`(No matches)`}):(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:`pane sp-pick${t===_?` top-match`:``}`,onClick:()=>e(t),...t===_?{onKeyDown:U}:{},children:[(0,$.jsxs)(`span`,{className:`sp-head`,children:[(0,$.jsx)(`span`,{className:`sp-title`,...t===_?{id:v}:{},children:ti(t)}),t.default&&(0,$.jsx)(Ye,{tone:`amber`,children:`Default`})]}),(0,$.jsx)(`span`,{className:`sp-text`,children:t.text.trim()||(0,$.jsx)(`span`,{className:`unset`,children:`(Empty)`})}),(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 `,Ml(t.lastUsed)]}),(0,$.jsxs)(`span`,{children:[`v`,t.version]})]})]})},t.id))}),(0,$.jsx)(A,{label:`Prompts`,page:h,size:xp,total:p.length,onPage:d})]})]})}function Cp({what:e,options:t,recent:n,taken:r,linkedHere:i,ready:a,onPick:o,onMove:s,onClose:c}){let[l,u]=(0,Y.useState)(`new`),[d,f]=(0,Y.useState)(``),[p,m]=(0,Y.useState)(``),[h,g]=(0,Y.useState)(!1),[_,y]=(0,Y.useState)(null),b=(0,Y.useId)(),x=d.trim(),S=l===`new`&&r.has(x),C=t.find(e=>e.id===p),w=!h&&(l===`new`?!!x&&!S:!!p&&a(p)),T=async()=>{if(!w)return;g(!0),y(null);let e=await s(l===`new`?{kind:l,name:x}:{kind:l,id:p});if(e){y(e),g(!1);return}c()};return(0,$.jsxs)(Ae,{title:`Move ${e} to a filter set`,onClose:c,className:`move-to-set`,dirty:!!x||!!p,initialFocus:`.mv-name`,onEnter:w?()=>void T():void 0,footer:(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost`,onClick:c,children:`Cancel`}),(0,$.jsx)(`button`,{type:`button`,disabled:!w,"aria-busy":h||void 0,onClick:()=>void T(),children:`Move`})]}),children:[(0,$.jsx)(v,{label:`Filter set`,value:l,onChange:e=>u(e),options:[{value:`new`,label:`New filter set`,children:(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`input`,{className:`mv-name`,type:`text`,"aria-label":`Name`,placeholder:`Name`,value:d,...S?{"aria-invalid":!0,"aria-describedby":b}:{},onChange:e=>f(e.target.value)}),S&&(0,$.jsxs)(pt,{tone:`error`,id:b,children:[`A filter set named `,x,` already exists.`]})]})},{value:`existing`,label:`Existing filter set`,children:e=>(0,$.jsx)(N,{kind:`Filter set`,value:p,options:t,recent:n,usedLabel:`Last changed`,placeholder:`Choose a filter set`,labelledBy:e,onChange:e=>{u(`existing`),m(e),o?.(e)}})}]}),l===`existing`&&C&&i.has(p)&&(0,$.jsxs)(`p`,{className:`note`,children:[C.label,` is already linked here, so `,e,` joins its end and runs where it runs.`]}),_&&(0,$.jsx)(pt,{tone:`error`,children:_})]})}function wp(e,t){return e==null?`Follow latest`:t!=null&&t>e?`Pinned v${e} · v${t} is latest`:`Pinned v${e}`}function Tp(e,t){if(!e||!t)return null;let n=e.cases.map(e=>K().caseItem(e));return{found:K().itemsFound(n,t),total:n.length}}function Ep({pipeline:e,edit:t,offer:n,children:r,contentFiles:i}){let a=Qr(),{list:o}=$r(),[s,c]=(0,Y.useState)(!1),[u,d]=(0,Y.useState)(null),f=e.evals,[,p]=(0,Y.useState)(0);(0,Y.useEffect)(()=>a.subscribe(()=>p(e=>e+1)),[a]),(0,Y.useEffect)(()=>{for(let e of f)e.group&&a.load(e.group.id)},[f,a]);let m=(r,i)=>{let a=e;t(r),n({label:i,restore:()=>t(a)})},h=e=>a.entry(e)?.doc?.body??null,g=new Map((o??[]).map(e=>[e.id,e])),_=new Set(f.flatMap(e=>e.group?[e.group.id]:[])),v=f.flatMap((e,t)=>e.group?[{t:e,j:t}]:[]),y=f.flatMap((e,t)=>e.own?[{t:e,j:t}]:[]),b=async(t,n,r)=>{await a.flush(n),await a.refresh();let{list:i,error:o}=a.list(),s=o?void 0:i?.find(e=>e.id===n)?.versions;s!=null&&m(Te(e,t,s),`Pinned ${r}`)},x=({t,j:n})=>{let r=t.group.id;return[{label:`Open in Library`,icon:Ee,onSelect:()=>ps(r)},t.pin==null?{label:`Pin`,icon:vr,disabled:!g.has(r),onSelect:()=>{b(n,r,t.group.name||r)}}:{label:`Follow latest`,icon:_r,onSelect:()=>m(Te(e,n,null),`${t.group.name||r} follows the latest`)},{label:`Unlink`,icon:Nr,onSelect:()=>m(R(e,n),`Unlinked ${t.group.name||r}`)}]};return(0,$.jsxs)(Tt,{id:`evals`,title:`Evals`,level:2,note:f.length?String(f.length):void 0,children:[f.length===0&&(0,$.jsx)(`p`,{className:`repeater-empty`,children:`(No evals)`}),f.length>0&&(0,$.jsx)(Op,{pass:e.pass,count:f.length,edit:n=>t(ye(e,n))}),v.length>0&&(0,$.jsx)(W,{className:`eg-links`,cards:!0,label:`Linked evals`,rows:v,rowKey:e=>e.t.id,columns:[{key:`group`,header:`Eval`,role:`title`,sort:e=>(e.t.group.name||``).toLowerCase(),cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>ps(e.t.group.id),children:e.t.group.name||e.t.group.id})},{key:`items`,header:`Eval Cases here`,sort:e=>Tp(h(e.t.group.id),i)?.found??-1,cell:e=>{let t=Tp(h(e.t.group.id),i);if(!t)return`…`;let n=`${t.found} of ${t.total}`;return t.found<t.total?(0,$.jsxs)(`span`,{className:`eval-flag`,title:`${t.total-t.found} not in this run's content`,children:[(0,$.jsx)(ir,{size:12,"aria-hidden":`true`}),n]}):n}},{key:`follows`,header:`Version`,sort:e=>e.t.pin??0,cell:e=>wp(e.t.pin,g.get(e.t.group.id)?.versions)},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>(0,$.jsx)($e,{label:`${e.t.group.name||e.t.group.id} actions`,actions:x(e)})}]}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>c(!0),children:[(0,$.jsx)(J.add,{size:14,"aria-hidden":`true`}),`Link eval`]}),r,y.map(({t:n,j:r})=>(0,$.jsx)(Dp,{ev:n,label:K().evalLabel(e,r),renaming:u===r,onRename:()=>d(r),rawReply:K().jobsOf(e).some(e=>K().readAsOf(e)!=null),casesBody:n.own.casesFrom?h(n.own.casesFrom.id):null,loadCases:()=>{n.own.casesFrom&&a.load(n.own.casesFrom.id)},set:n=>t(G(e,r,n)),undoableSet:(t,n)=>m(G(e,r,t),n),onCommitName:n=>{t(O(e,r,n)),d(null)},onCancelName:()=>d(null),onCopyCases:t=>m(ct(e,r,t),`Copied ${K().evalLabel(e,r)}'s eval cases in`),onMoveToLibrary:async()=>{let t=D((n.name||``).trim()||K().evalLabel(e,r),new Set((o??[]).map(e=>e.name))),{casesFrom:i,...s}=n.own,c=await a.create(t,s);m(fn(e,r,{id:c.id,name:c.name}),`Moved ${t} to the Library`)},onRemove:()=>m(R(e,r),`Removed ${K().evalLabel(e,r)}`)},n.id)),s&&(0,$.jsx)(kp,{list:o??[],linkedIds:_,contentFiles:i,onLink:n=>{t(l(e,n)),c(!1)},onNew:async()=>{let n=await a.create(D(`Untitled eval`,new Set((o??[]).map(e=>e.name))));t(l(e,{id:n.id,name:n.name})),c(!1)},onClose:()=>c(!1)})]})}function Dp({ev:e,label:t,renaming:n,onRename:r,rawReply:i,casesBody:a,loadCases:o,set:s,undoableSet:c,onCommitName:l,onCancelName:u,onCopyCases:d,onMoveToLibrary:f,onRemove:p}){let m=K(),h=qs(),g=e.own;(0,Y.useEffect)(()=>{g.casesFrom&&o()},[g.casesFrom,o]);let _=m.isWholeRun(m.EVAL_TYPES.group,e),v=_?g.run:g.every,y=t=>s({...e,own:{...g,...t}}),b=t=>({...e,own:{...g,..._?{run:t}:{every:t}}}),x=v.some(e=>m.METRICS[e.type]?.graded),{mode:S,threshold:C}=g.scoring,T=[{label:`Rename…`,icon:J.rename,onSelect:r},...g.casesFrom?[{label:`Copy eval cases in`,icon:Fe,disabled:!a,onSelect:()=>a&&d(a.cases)}]:[],{label:`Move to Library`,icon:cr,onSelect:()=>void f()},{label:`Remove`,icon:J.remove,destructive:!0,onSelect:p}],E=[{key:`mode`,label:`Item passes when`,control:{type:`select`,value:S,options:[{value:`all`,label:`All metrics pass`},{value:`weighted`,label:`Points`}],onChange:e=>y({scoring:{mode:e===`weighted`?`weighted`:`all`,threshold:e===`weighted`?C??1:C}})}},...S===`weighted`?[{key:`threshold`,label:`Pass at`,width:`quarter`,control:{type:`text`,value:C==null?``:String(C),onChange:e=>y({scoring:{mode:S,threshold:e.trim()?/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e:null}})}}]:[],...g.casesFrom?[]:[{key:`over`,label:`Reads`,control:{type:`select`,value:_?`run`:`item`,options:[{value:`item`,label:`Each item`},{value:`run`,label:`Whole run`}],onChange:e=>y(e===`run`?{every:[],run:[...g.every,...g.run]}:{every:[...g.every,...g.run],run:[]})}}],...g.casesFrom?[{key:`cases-from`,label:`Eval cases from`,control:{type:`static`,text:g.casesFrom.name||g.casesFrom.id}}]:[],...x||g.grader?[{key:`grader`,label:`Grader`,control:h.control(g.grader?.id??``,e=>{let t=h.asked.find(t=>t.id===e);y({grader:t?{id:t.id,name:t.name}:null})})}]:[]];return(0,$.jsx)(Tt,{id:`eval-${e.id}`,title:t,level:3,note:m.EVAL_TYPES.group?.label??`Eval`,about:`An eval of this experiment's own, defined here rather than in the Library.`,actions:T,...n?{rename:{value:e.name||t,onCommit:l,onCancel:u}}:{},children:(0,$.jsxs)(Ne,{children:[(0,$.jsx)(w,{label:t,rows:E}),(0,$.jsx)(Rs,{label:t,metrics:v,over:_?`run`:`item`,rawReply:i,set:e=>s(b(e)),undoable:(e,t)=>c(b(e),t)}),(0,$.jsx)(w,{label:`${t} on failure`,rows:[{key:`continue`,label:`Continue on failure`,control:{type:`checkbox`,checked:e.continueOnFailure,onChange:t=>s({...e,continueOnFailure:t})}}]})]})})}function Op({pass:e,count:t,edit:n}){let r=e.mode===`atLeast`?e.count:t,i=[{key:`pass`,label:`Run passes when`,control:{type:`select`,value:e.mode,options:[{value:`all`,label:`All evals 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:`Evals`,width:`quarter`,control:{type:`text`,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,{rows:i})}function kp({list:e,linkedIds:t,contentFiles:n,onLink:r,onNew:i,onClose:a}){let o=Qr(),[,s]=(0,Y.useState)(0);(0,Y.useEffect)(()=>o.subscribe(()=>s(e=>e+1)),[o]),(0,Y.useEffect)(()=>{for(let t of e)o.load(t.id)},[e,o]);let c=e=>Tp(o.entry(e)?.doc?.body,n),l=Bs(e,t=>e.find(e=>e.id===t)?.updated),u=l.options.map(e=>{let n=c(e.id);return{...e,chosen:t.has(e.id),detail:n&&n.total?`${n.found} of ${n.total} eval cases here`:void 0}});return(0,$.jsx)(h,{kind:`Eval`,title:`Link eval`,usedLabel:`Last changed`,detailLabel:`Eval Cases`,value:``,options:u,recent:l.recent,onPick:t=>{let n=e.find(e=>e.id===t);n&&r({id:n.id,name:n.name})},onClose:a,footer:(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>void i(),children:[(0,$.jsx)(J.add,{size:14,"aria-hidden":`true`}),`New eval…`]})})}var Ap=(e,t)=>e.entry(t)?.doc?.body??null,jp=e=>t=>(t.own??(t.set?Ap(e,t.set.id):null))?.format!=null;function Mp(e,t){return e.entry(t)?.doc?Promise.resolve(!0):new Promise(n=>{let r=e.subscribe(()=>{let a=e.entry(t);(a?.doc||a&&a.sync.state===`missing`)&&(r(),clearTimeout(i),n(!!a.doc))}),i=setTimeout(()=>{r(),n(!1)},1e4);e.load(t)})}function Np({pipeline:e,job:t,format:n,edit:r,offer:i,moveAll:a,onMoveAllClose:o}){let s=Oc(),{list:c}=kc(),[l,u]=(0,Y.useState)(new Set),[d,f]=(0,Y.useState)(null),[p,g]=(0,Y.useState)(null),[_,v]=(0,Y.useState)(null),[y,b]=(0,Y.useState)(null),[x,S]=(0,Y.useState)(null),[,C]=(0,Y.useState)(0);(0,Y.useEffect)(()=>s.subscribe(()=>C(e=>e+1)),[s]);let w=K().filterSetsOf(K().jobsOf(e)[t]),T=w.flatMap(e=>e.set?[e.set.id]:[]).join(` `);(0,Y.useEffect)(()=>{for(let e of T.split(` `).filter(Boolean))s.load(e)},[s,T]);let E=jp(s),D=gn(K().jobsOf(e)[t],E),O=D.flatMap(e=>e.kind===`filter`?[e.filter]:[]),k=new Map((c??[]).map(e=>[e.id,e])),A=K().formatReadsItems(K().outOf(K().jobsOf(e)[t]).kind),j=D.findIndex(e=>e.kind===`format`),M=A&&j<0?D.findIndex(e=>e.kind===`set`&&E(e.link)):-1,N=j>=0?j:M,P=e=>A&&N>=0&&e>N,F=e=>{S(null),r(e)},I=n=>F(Ut(e,t,n)),ee=(t,n)=>{let a=e;F(t),i({label:n,restore:()=>r(a)})},L=(e,t)=>{let n=[...D],[r]=n.splice(e,1);n.splice(e+t,0,r),I(n)},te=e=>[{label:`Move up`,icon:J.moveUp,disabled:e===0,onSelect:()=>L(e,-1)},{label:`Move down`,icon:J.moveDown,disabled:e===D.length-1,onSelect:()=>L(e,1)}],R=e=>e.set.name||e.set.id,ne=(n,r)=>[{label:`Edit…`,icon:J.edit,onSelect:()=>f(r)},{label:`Clone`,icon:J.clone,onSelect:()=>{let e=D[r];I([...D.slice(0,r+1),{...e,filter:{...structuredClone(n),id:we(O,`${n.id} copy`)}},...D.slice(r+1)])}},...te(r),{label:`Move to filter set…`,icon:cr,onSelect:()=>b([r])},{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>ee(Ut(e,t,D.filter((e,t)=>t!==r)),`Removed ${n.id}`)}],re=async n=>{let r=n.set.id;await s.flush(r),await s.refresh();let{list:i,error:a}=s.list(),o=a?void 0:i?.find(e=>e.id===r)?.versions;o!=null&&ee(Cn(e,t,n.id,o),`Pinned ${R(n)}`)},z=(n,r)=>[{label:`Open in Library`,icon:Ee,onSelect:()=>Nc(n.set.id)},n.pin==null?{label:`Pin`,icon:vr,disabled:!k.has(n.set.id),onSelect:()=>{re(n)}}:{label:`Follow latest`,icon:_r,onSelect:()=>ee(Cn(e,t,n.id,null),`${R(n)} follows the latest`)},...te(r),{label:`Unlink`,icon:Nr,onSelect:()=>ee(se(e,t,n.id,E),`Unlinked ${R(n)}`)}],B=(0,Y.useId)(),V=D.flatMap((e,t)=>{let n={...e,n:t+1,at:t};if(e.kind!==`set`||!l.has(e.link.id))return[n];let r=Ap(s,e.link.set.id),i=r?.filters??[],a=t===M?r?.format??null:null,o=i.map((n,r)=>({kind:`inner`,filter:n,key:`${e.link.id}/${r}`,below:a==null?P(t):r>=a,id:`${B}-${t}-${r}`}));return a!=null&&o.splice(a,0,{kind:`innerFormat`,key:`${e.link.id}/format`,id:`${B}-${t}-format`}),[n,...o]}),H=e=>u(t=>xe(t,e)),ie=e=>V.flatMap(t=>(t.kind===`inner`||t.kind===`innerFormat`)&&t.key.startsWith(`${e}/`)?[t.id]:[]).join(` `)||void 0,U=(e,t)=>t&&hl(e)?(0,$.jsx)(`span`,{className:`fs-per-item`,children:`(Per item)`}):null,ae=(0,$.jsxs)(`span`,{className:`fs-format`,children:[(0,$.jsxs)(`span`,{className:`fs-format-name`,children:[(0,$.jsx)(ul,{size:14,"aria-hidden":`true`}),`Response Format`]}),n]}),oe=[{key:`n`,header:`#`,label:``,className:`num fs-n`,sort:null,cell:e=>e.kind===`inner`||e.kind===`innerFormat`?null:e.n},{key:`filter`,header:`Filter`,role:`title`,sort:null,cell:e=>{if(e.kind===`format`)return ae;if(e.kind===`innerFormat`)return(0,$.jsx)(`span`,{className:`fs-inner`,children:ae});if(e.kind===`filter`)return(0,$.jsxs)(`span`,{className:`fs-name`,children:[(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>f(e.at),children:e.filter.id}),U(e.filter,P(e.at))]});if(e.kind===`set`){let t=l.has(e.link.id),n=t?nt:ce;return(0,$.jsxs)(`span`,{className:`fs-set`,children:[(0,$.jsx)(`button`,{type:`button`,className:`ghost fs-toggle`,"aria-expanded":t,"aria-controls":t?ie(e.link.id):void 0,"aria-label":`${t?`Hide`:`Show`} ${R(e.link)}'s filters`,onClick:()=>H(e.link.id),children:(0,$.jsx)(n,{size:14,"aria-hidden":`true`})}),(0,$.jsx)(`button`,{type:`button`,className:`link-btn`,onClick:()=>Nc(e.link.set.id),children:R(e.link)}),(0,$.jsx)(Ye,{children:e.link.pin==null?`Latest`:wp(e.link.pin,k.get(e.link.set.id)?.versions)})]})}return(0,$.jsxs)(`span`,{className:`fs-inner`,children:[e.filter.id,U(e.filter,e.below)]})}},{key:`type`,header:`Type`,className:`fs-type`,sort:null,cell:e=>e.kind===`filter`||e.kind===`inner`?(0,$.jsx)(pl,{type:e.filter.type}):e.kind===`set`?`Filter set`:null},{key:`menu`,header:``,label:``,role:`menu`,sort:null,cell:e=>e.kind===`filter`?(0,$.jsx)($e,{label:`${e.filter.id} actions`,actions:ne(e.filter,e.at)}):e.kind===`set`?(0,$.jsx)($e,{label:`${R(e.link)} actions`,actions:z(e.link,e.at)}):e.kind===`format`?(0,$.jsx)($e,{label:`Response Format actions`,actions:te(e.at)}):null}],le=d!=null&&D[d]?.kind===`filter`?D[d]:null,G=new Set(w.flatMap(e=>e.set?[e.set.id]:[])),{options:ue,recent:de}=Bs((c??[]).filter(e=>!G.has(e.id)),e=>k.get(e)?.updated),pe=async n=>{let r={id:n,name:k.get(n)?.name??``};if(!await Mp(s,n))return S(`That filter set hasn't loaded.`);let i=K().newFilterLink(r),a=w.find(E);if(A&&E(i)&&a)return S(`${r.name||`That set`} holds a Response Format, and ${a.set?R(a):`a set here`} already does.`);F(fe(e,t,r,E))},me=D.flatMap((e,t)=>e.kind===`filter`?[t]:[]),he=a?me:y,ge=()=>{b(null),o?.()},_e=e=>e.length===1?D[e[0]].filter.id:`${e.length} filters`,ve=async(n,a)=>{let o=n.map(e=>D[e].filter),c=e,l=null,u,d,f;if(a.kind===`new`){let r=j<0?0:n.filter(e=>e<j).length,i=r>0&&r<n.length,c;try{c=await s.create(a.name,{...K().blankFilterSetBody(),filters:o.map(e=>structuredClone(e)),...i?{format:r}:{}})}catch(e){return e instanceof Error?e.message:String(e)}u=c.name,l=K().newFilterLink({id:c.id,name:u}),d=Ut(e,t,St(D,n,l,i)),f=()=>{s.remove(c.id).catch(()=>{})}}else{let r=s.entry(a.id)?.doc;if(!r)return`That filter set hasn't loaded.`;s.edit(a.id,e=>zt(e,o.map(e=>structuredClone(e)))),await s.flush(a.id);let i=s.entry(a.id),c=Sc(i?.sync);if(c)return s.edit(a.id,()=>r.body),c;u=r.name||r.id;let p=w.find(e=>e.set?.id===a.id);p||(l=K().newFilterLink({id:a.id,name:r.name})),d=Ut(e,t,St(D,n,l)),p?.pin!=null&&i?.doc&&(d=Cn(d,t,p.id,i.doc.versions)),f=()=>s.edit(a.id,()=>r.body)}let p=_e(n);return ge(),F(d),i({label:`Moved ${p} to ${u}`,restore:()=>{r(c),f()}}),null},ye=Bs(c??[],e=>k.get(e)?.updated);return(0,$.jsxs)(`div`,{className:`filter-stage`,id:`job-${t}-filters`,children:[V.length>0&&(0,$.jsx)(W,{className:`fs-table`,cards:!0,label:`Filters`,rows:V,defaultSort:null,rowKey:e=>e.kind===`inner`||e.kind===`innerFormat`?e.key:e.kind===`set`?e.link.id:e.kind===`format`?`\0format`:`${e.from}/${e.filter.id}`,rowProps:e=>e.kind===`inner`?{id:e.id,className:`fs-inner-row`}:e.kind===`innerFormat`?{id:e.id,className:`fs-inner-row fs-format-row`}:e.kind===`format`?{className:`fs-format-row`}:{},columns:oe}),x&&(0,$.jsx)(pt,{tone:`error`,children:x}),(0,$.jsx)(`span`,{className:`add-btn fs-add`,children:(0,$.jsx)($e,{label:`Add filter`,trigger:(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(J.add,{size:14,"aria-hidden":`true`}),`Add filter`]}),actions:[{label:`New filter…`,icon:m,onSelect:()=>v(`type`)},{label:`Link filter set…`,icon:dr,onSelect:()=>v(`link`)}]})}),_===`type`&&(0,$.jsx)(Cl,{onPick:e=>{v(null),g(sl(we(O,ml(fl(e))),e))},onClose:()=>v(null)}),(p||le)&&(0,$.jsx)(xl,{filter:p??le.filter,isNew:!!p,others:p?O:O.filter(e=>e!==le.filter),onSave:e=>{I(p?[...D,{kind:`filter`,filter:e,from:``}]:D.map((t,n)=>n===d?{...le,filter:e}:t)),g(null),f(null)},onClose:()=>{g(null),f(null)}},p?`new`:d),he&&he.length>0&&he.every(e=>D[e]?.kind===`filter`)&&(0,$.jsx)(Cp,{what:_e(he),options:ye.options,recent:ye.recent,taken:new Set((c??[]).map(e=>e.name)),linkedHere:G,ready:e=>!!s.entry(e)?.doc,onPick:e=>s.load(e),onMove:e=>ve(he,e),onClose:ge}),_===`link`&&(0,$.jsx)(h,{kind:`Filter set`,title:`Link filter set`,usedLabel:`Last changed`,value:``,options:ue,recent:de,onPick:e=>{pe(e)},onClose:()=>v(null),footer:(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>v(`new-set`),children:[(0,$.jsx)(J.add,{size:14,"aria-hidden":`true`}),`New filter set…`]})}),_===`new-set`&&(0,$.jsx)(Ic,{taken:new Set((c??[]).map(e=>e.name)),onClose:()=>v(null),onCreate:n=>void(async()=>{let r=await s.create(n,K().blankFilterSetBody());F(fe(e,t,{id:r.id,name:r.name},E))})()})]})}var Pp=(e,t)=>K().filterSetsOf(K().jobsOf(e)[t]).some(e=>!!e.own?.filters.length),Fp=e=>e.steps??[],Ip=(e,t)=>({...e,steps:t}),Lp=e=>Fp(e).filter(e=>e.type===`request`),Rp=(e,t)=>Lp(e).findIndex(e=>e.id===t);function zp(e,t,n){let r=[...Fp(e)],i=r.findIndex(e=>e.id===t),a=i+n;return i<0||a<0||a>=r.length?e:([r[i],r[a]]=[r[a],r[i]],Ip(e,r))}var Bp=(e,t)=>Fp(e).find(e=>e.id===t)?.type!==`request`||Lp(e).length>1;function Vp(e,t){let n=Fp(e).find(e=>e.id===t);return n?n.type===`request`?z(e,Rp(e,t)):Ip(e,Fp(e).filter(e=>e.id!==t)):e}var Hp=(e,t)=>{let n=Fp(e).find(e=>e.id===t)?.type;return n===`request`||n===`getItems`};function Up(e,t){let n=K(),r=Fp(e),i=r.findIndex(e=>e.id===t),a=r[i];if(!a||!Hp(e,t))return e;if(a.type===`getItems`)return Ip(e,[...r.slice(0,i+1),{...structuredClone(a),id:n.newId()},...r.slice(i+1)]);let o=Rp(e,t),s=n.jobsOf(e),c={...structuredClone(s[o]),id:n.newId(),name:``};c.steps=c.steps.filter(e=>e.type!==`attachContent`);let l=[...s.slice(0,o+1),c,...s.slice(o+1)],u=n.targetsOf(e).map(e=>({...e,steps:[...e.steps.slice(0,o+1),structuredClone(e.steps[o]),...e.steps.slice(o+1)]})),d=n.withTargets(n.withJobs(e,l),u),f=Fp(d).filter(e=>e.id===c.id||`request`in e&&e.request===c.id),p=Fp(d).filter(e=>!f.includes(e)),m=Math.max(...p.map((e,n)=>e.id===t||`request`in e&&e.request===t?n:-1))+1;return Ip(d,[...p.slice(0,m),...f,...p.slice(m)])}function Wp(e,t){let n=new Set(Fp(e).flatMap(e=>e.type===t?[e.request]:[]));return[...Lp(e)].reverse().find(e=>!n.has(e.id))}function Gp(e,t){let n=K(),r=Fp(e);if(t===`request`){let t=Qe(e),r=n.jobsOf(t).at(-1).id,i=Fp(t).filter(e=>e.id===r||`request`in e&&e.request===r);return{next:Ip(t,[...Fp(t).filter(e=>!i.includes(e)),...i]),id:r}}if(t===`getItems`){let t=n.newId(),i=r.findIndex(e=>e.type===`request`),a=i<0?r.length:i,o={type:`prompt`};return{next:Ip(e,[...r.slice(0,a),{type:`getItems`,id:t,content:o},...r.slice(a)]),id:t}}let i=Wp(e,t);if(!i)return null;let a=n.newId(),o=t===`readResponse`?{type:`readResponse`,id:a,request:i.id}:{type:`filter`,id:a,request:i.id,filterSets:[]};return{next:Ip(e,[...r,o]),id:a}}var Kp=(e,t,n)=>Ip(e,Fp(e).map(e=>e.id===t&&e.type===`getItems`?{...e,content:n}:e));function qp({stepId:e,pipeline:t,sources:n,edit:r}){let[i,a]=(0,Y.useState)(!1),s=X(),c=K(),l=Fp(t).find(t=>t.id===e),u=l?.type===`getItems`?l.content:null,d=c.CONTENT_TYPES,f=Fp(t).find(e=>e.type===`getItems`)?.id===e,p=u?.type===`source`?n.find(e=>e.id===u.ref.id):void 0,m=p?.type===`dynamic`&&(!p?.provider||c.DYNAMIC_PROVIDERS[p.provider]?.tier!==`supplied`),h=m&&u?.type===`source`?u.live??(p?p.files===0:!1):!1,g=u?.type===`source`?u.pin??null:null,_=Zd(s,m&&u?.type===`source`?u.ref.id:null),v=(n,i)=>{let a=Kp(t,e,n);r(f&&i!==void 0?o(a,0,i):a)},y=async e=>{let t=e.type===`dynamic`&&c.DYNAMIC_PROVIDERS[e.provider??``]?.tier!==`supplied`&&e.files>0?await s.sourceResponses(e.id).then(e=>Gd(e.responses),()=>null):null;v({type:`source`,ref:Xe(e.id,e.name),first:null,loops:1,...t?{pin:t}:{}},c.sourceTypeOf(e)?.sendsImage??!1)},b=[{label:`Change content`,icon:J.rename,onSelect:()=>a(!0)}],x=[u?.type===`text`?{key:`attach`,label:c.CONTENT_TYPES.text?.label??`Text`,required:!0,actions:b,control:{type:`textarea`,value:u.text,placeholder:`Item text`,onChange:e=>v({...u,text:e})}}:{key:`attach`,label:c.CONTENT_TYPES[u?.type??``]?.label??c.STEP_TYPES.attachContent?.label??`Content`,required:!0,actions:b,control:{type:`static`,text:Xp(u,n)}},...m&&u?.type===`source`?[{key:`freshness`,label:`Data updates`,control:{type:`select`,value:h?`live`:`saved`,options:Jp,onChange:e=>v({...u,live:e===`live`,...e===`saved`&&!u.pin&&_&&Gd(_)?{pin:Gd(_)}:{}})}}]:[],...m&&u?.type===`source`&&!h?[{key:`response`,label:`Response`,required:!0,control:{type:`picker`,kind:`Response`,ordered:!0,value:g&&_?qd(_,g):``,options:_?Yd(_):[],placeholder:g?_?Kd(_,g)?g.name:`(Removed)`:g.name:`(Not set)`,onChange:e=>v({...u,pin:Xd(e)})}}]:[]];return(0,$.jsxs)(`div`,{className:`stage-row`,id:`step-${e}-items`,children:[(0,$.jsx)(w,{label:`Content`,rows:x}),i&&(0,$.jsxs)(Ae,{title:u?`Change content`:`Add content`,onClose:()=>a(!1),dismiss:`Cancel`,children:[(0,$.jsx)(xf,{mode:`pick`,onPick:e=>{a(!1),y(e)}}),(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:()=>{v({type:`text`,text:``},!1),a(!1)},children:[(0,$.jsx)(`span`,{children:d.text?.label??`Text`}),d.text?.description&&(0,$.jsx)(`span`,{className:`pick-about`,children:d.text.description})]}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>{v({type:`prompt`},!1),a(!1)},children:[(0,$.jsx)(`span`,{children:d.prompt?.label??`(No content)`}),d.prompt?.description&&(0,$.jsx)(`span`,{className:`pick-about`,children:d.prompt.description})]})]})]})]})}var Jp=[{value:`live`,label:`Live data`},{value:`saved`,label:`Saved response`}],Yp=(e,t)=>{let n=t.find(t=>t.id===e.ref.id);return n?`${n.name}, ${fa(n.files,Et(n))}`:`A Source that is gone: ${e.ref.name}`},Xp=(e,t)=>e?e.type===`source`?Yp(e,t):K().CONTENT_TYPES[e.type]?.label??e.type:`none`,Zp=`#echo`,Qp=`#default`,$p=e=>!!K().STEP_TYPES[e.type]?.local,em=()=>Object.entries(K().STEP_TYPES).filter(([,e])=>e.slot===`target`&&e.local&&!K().CONNECTION_TYPES[e.replaces??``]?.replays).map(([,e])=>({id:Zp,label:e.label??`Echo`}));function tm(e,t,n,r,i){let a=e.same!=null,o={key:`connection`,label:e.label??`Connection`,required:!a,control:{type:`picker`,kind:`Connection`,value:e.connection,...r(nm(t,e.at),a?[{id:``,label:e.same}]:e.fixed),onChange:i.connection}};if(e.echo)return[o];let s=a&&!e.own?``:e.profile?.id??`#default`,c=[...a&&e.connection===``?[{id:``,label:e.same}]:[],{id:Qp,label:`(Default)`}];return[o,{key:`profile`,label:`Profile`,control:{type:`picker`,kind:`Profile`,value:s,...Bs(nm(n,e.profile),()=>null,c),onChange:i.profile}}]}var nm=(e,t)=>[...t?.id&&!e.some(e=>e.id===t.id)?[{id:t.id,name:`${t.name||t.id} (missing)`,missing:!0}]:[],...e.map(e=>({id:e.id,name:e.name}))],rm=(e,t)=>{let n=e.find(e=>e.id===t);return n?{id:n.id,name:n.name}:null},im=[`answeredBy`,`connection`,`model`,`profile`,`settings`,`prompt`],am={answeredBy:`Answered by`,connection:`AI Connection`,model:`Model`,profile:`Profile`,settings:`Settings`,prompt:`Prompt`},om=(e,t,n)=>n===`prompt`&&sm(e,t)?`Body`:am[n],sm=(e,t)=>!!K().flowStepOf(K().jobsOf(e)[t]),cm=(e,t,n)=>K().targetEntryOf(e,t,n)?.asks===`request`;function lm(e,t){return sm(e,t)?Math.max(0,K().targetsOf(e).findIndex((n,r)=>cm(e,r,t))):0}function um(e,t){let n=K(),r=lm(e,t);if(sm(e,t)&&cm(e,r,t))return n.targetEntryOf(e,r,t)?.newStep?.(n.requestStepOf(e,r,t))}function dm(e,t,n){let r=K(),i=r.targetStepOf(e,t,n)?.type??``,a=r.STEP_TYPES[i];return a?.asks===`request`?`Request`:a?.label??i}function fm(e,t){let n=K(),r=lm(e,t),i=e=>e.asks===`request`?0:n.CONNECTION_TYPES[e.replaces??``]?.replays?1:2;return Object.entries(n.STEP_TYPES).filter(([i,a])=>a.slot===`target`&&!(t>0&&a.firstJobOnly)&&(a.asks===`request`?cm(e,r,t)&&n.targetStepOf(e,r,t)?.type===i:!!a.local)).sort(([,e],[,t])=>i(e)-i(t)).map(([e,t])=>({type:e,label:t.asks===`request`?`Request`:t.label??e}))}var pm=(e,t,n)=>{let r=K().targetEntryOf(e,t,n);return!!r&&!r.local&&r.modelFrom===`profile`},mm=(e,t,n)=>!!K().targetEntryOf(e,t,n)?.local;function hm(e,t,n,r){let i=K(),a=i.targetStepOf(e,t,n);if(!a)return``;let o=i.targetAnswerOf(e,t,n);switch(r){case`answeredBy`:return a.type;case`connection`:return mm(e,t,n)?`#${a.type}`:o?.connection.id??``;case`profile`:return mm(e,t,n)?``:o?.profile?.id??``;case`model`:return mm(e,t,n)?``:a.model??``;case`settings`:return pm(e,t,n)?JSON.stringify(Object.entries(a.settings??{}).sort()):``;case`prompt`:return JSON.stringify([a.type,a.prompt??``,a.from?.id??``])}}function gm(e,t,n){if(!sm(e,t))return hm(e,0,t,n);let r=lm(e,t),i=um(e,t);return i?n===`model`?i.model??``:n===`prompt`?JSON.stringify([i.type,i.prompt??``,``]):hm(e,r,t,n):n===`answeredBy`?``:hm(e,r,t,n)}function _m(e,t,n,r){return sm(e,n)&&r!==`answeredBy`&&mm(e,t,n)?!1:hm(e,t,n,r)!==gm(e,n,r)}var vm=(e,t,n,r)=>r!==`answeredBy`&&sm(e,n)&&mm(e,t,n);function ym(e,t,n,r){let i=K(),a=i.targetStepOf(e,t,n);return r===`answeredBy`?sm(e,n):r===`model`&&sm(e,n)&&cm(e,t,n)?!!i.HTTP_APIS[i.flowStepOf(i.jobsOf(e)[n])?.api??``]?.fields.includes(`model`):r===`model`||r===`profile`?!mm(e,t,n):r===`settings`?pm(e,t,n):r!==`prompt`||!i.CONNECTION_TYPES[i.STEP_TYPES[a?.type??``]?.replaces??``]?.replays}var bm=(e,t)=>im.filter(n=>n===`answeredBy`?sm(e,t):n!==`settings`||K().targetsOf(e).some((n,r)=>pm(e,r,t)&&!!K().targetStepOf(e,r,t)?.settings));function xm(e,t){return K().jobsOf(e).flatMap((n,r)=>bm(e,r).filter(n=>_m(e,t,r,n)).map(e=>({k:r,row:e})))}function Sm(e,t,n,r){let i=K(),a=i.STEP_TYPES[r];if(!a||i.targetStepOf(e,t,n)?.type===r)return e;let o=i.jobsOf(e).length===1;if(a.asks===`request`){let a=um(e,n);if(!a||a.type!==r)return e;let o=i.targetsOf(e)[lm(e,n)],s=i.withTarget(e,t,n,()=>a);return n===0?i.withTargets(s,i.targetsOf(s).map((e,n)=>n===t?{...e,connection:o.connection,profile:o.profile}:e)):s}let s=i.withTarget(e,t,n,()=>({type:r,prompt:``}));return o?i.withTargets(s,i.targetsOf(s).map((e,n)=>n===t?{...e,connection:null,profile:null}:e)):s}var Cm=(e,t,n,r)=>K().withTarget(e,t,n,e=>{let t={...e,model:r};return r||delete t.model,t}),wm=(e,t,n,r,i)=>K().withTarget(e,t,n,e=>{let{from:t,...n}=e;return{...n,prompt:r,...i?{from:{...i}}:{}}});function Tm(e,t,n,r,i){let a=K(),o=lm(e,n),s=um(e,n)??a.targetStepOf(e,o,n);if(!s||!_m(e,t,n,r))return e;if(r===`answeredBy`)return Sm(e,t,n,a.targetStepOf(e,o,n)?.type??``);if(r===`model`)return Cm(e,t,n,s.model??``);if(r===`settings`)return km(e,t,n,s.settings??null);if(r===`prompt`)return wm(e,t,n,s.prompt??``,s.from??null);let c=a.targetAnswerOf(e,o,n);if(r===`connection`&&mm(e,o,n))return n===0&&s.type===`echo`?je(e,t):e;let l=r===`connection`?`profile`:`connection`,u=_m(e,t,n,l),d=[...n>0?[()=>a.withTarget(e,t,n,e=>{let{connection:t,profile:n,...r}=e;return r}),()=>r===`connection`?j(e,t,n,null,i):Oe(e,t,n,rn,i)]:[],()=>r===`connection`?c?j(e,t,n,c.connection,i):e:Oe(e,t,n,c?.profile??null,i)];for(let e of d){let i=e();if(!_m(i,t,n,r)&&_m(i,t,n,l)===u)return i}return d.at(-1)()}function Em(e,t,n,r){let i=lm(e,t);return K().targetsOf(e).map((r,a)=>a===i||(typeof n==`function`?n(a):hm(e,a,t,n)===hm(e,i,t,n))).reduce((e,t,n)=>t?r(e,n):e,e)}function Dm(e){let t=K(),n=t.targetsOf(e),r=n[0];if(!r)return e;let i={...structuredClone(r),id:t.newId(),name:``},a=t.withTargets(e,[...n,i]),o=n.length;return t.jobsOf(a).reduce((e,n,r)=>sm(e,r)&&_m(e,o,r,`answeredBy`)?Sm(e,o,r,t.targetStepOf(e,lm(e,r),r)?.type??``):e,a)}var Om=e=>K().jobsOf(e).flatMap((t,n)=>pm(e,0,n)?[n]:[]),km=(e,t,n,r)=>K().withTarget(e,t,n,e=>{let{settings:t,...n}=e;return r?{...n,settings:{...r}}:n});function Am(e){let t=K(),n=(t.CONNECTION_TYPES[e.type??`llama.cpp`]?.settings??[]).filter(e=>e.scope!==`connection`&&e.key!==`model`).map(e=>e.key),r={};for(let e of n)t.DECODING_KEYS.includes(e)&&(r[e]=``);for(let[t,i]of Object.entries(e.opts))n.includes(t)&&(r[t]=String(i));return r}function jm(e,t,n){return n.reduce((e,n)=>{let r=Dm(e),i=K().targetsOf(r).length-1;return km(K().withTargets(r,K().targetsOf(r).map((e,t)=>t===i?{...e,name:n.label}:e)),i,t,Am(n))},e)}function Mm({pipeline:e,title:t,onClose:n}){let r=X(),[i,a]=(0,Y.useState)(null),[o,s]=(0,Y.useState)(null),c=K().contentOf(e),l=c?.type===`source`?c.ref.id:null,u=(0,Y.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 fd(await r.file(l,t.name)));return{name:t.name,reply:K().recordedReplyOf(e,n)}})))}catch(e){s(Q(e))}})()},[r,l,e]);return(0,Y.useEffect)(u,[u]),(0,$.jsx)(Ae,{title:t,wide:!0,className:`edit`,onClose:n,dismiss:`Close`,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`,{className:`mono-block`,children:e.reply??`(no recorded response)`})]},e.name))}):(0,$.jsx)(`p`,{className:`unset`,children:`(No items)`}):(0,$.jsx)(Bt,{label:`Responses`,error:o,onRetry:u})})}function Nm({pipeline:e,k:t,name:n,connections:r,connectionProfiles:i,resolve:a,prompts:s,joined:c,edit:l,offer:u,onConnection:d,onProfile:f,onEcho:m,onSelectPrompt:h,onSaveToLibrary:_,extra:v=[]}){let y=K(),b=Ws(),[x,C]=(0,Y.useState)(null),[E,D]=(0,Y.useState)(!1),[O,k]=(0,Y.useState)(!1),A=lm(e,t),j=Ve(e,A,t)??{type:`prompt`,prompt:``},M=Ft(j.type),N=y.STEP_TYPES[j.type],P=y.jobsOf(e)[t],I=y.targetsOf(e)[A],ee=y.tokensOf(P),L=t===0?``:`Same as ${y.stepNames(e)[y.jobsOf(e)[0].id]??`Request 1`}`,te=(n,r)=>l(Em(e,t,n,r)),R=(0,Y.useRef)(null),ne=(0,Y.useRef)(null);(0,Y.useLayoutEffect)(()=>{if(ne.current==null||!R.current)return;let e=ne.current;ne.current=null,R.current.focus(),R.current.setSelectionRange(e,e)},[j.prompt]);let re=e=>{let n=j.prompt??``,r=R.current,i=r?.selectionStart??n.length,a=r?.selectionEnd??n.length,o=n.slice(0,i)+e+n.slice(a);te(`prompt`,(e,n)=>S(e,n,t,o)),ne.current=i+e.length},z=j.from?s?.find(e=>e.id===j.from.id):void 0,B=j.prompt??``,V=j.from?`${z?ti(z):j.from.name}${z?`, version ${z.version}`:``}`:Pm(B),H=!!y.CONNECTION_TYPES[N?.replaces??``]?.replays,ie=N?.modelFrom===`profile`&&!N?.local,U=[...ee.map(e=>y.TOKEN_TYPES[e.type]?.notation(e.name)??`{${e.name}}`),...t>0?[`{reply}`]:[]],oe=[{label:`Select prompt…`,icon:ue,onSelect:h},{label:`Save to library…`,icon:bt,disabled:!B,onSelect:_},{label:`Insert token…`,icon:Nt,disabled:!U.length,onSelect:()=>D(!0)},...z?[{label:`Revert to version ${z.version}`,icon:J.revert,disabled:B===z.text,onSelect:()=>{let r=e;te(`prompt`,(e,n)=>p(e,n,t,z.text,{id:z.id,name:ti(z)})),u({label:`Reverted ${n} to version ${z.version}`,restore:()=>l(r)})}}]:[],...v],W=H?[]:tm(t===0?{connection:$p(j)?Zp:I?.connection?.id??``,at:I?.connection,profile:I?.profile,fixed:sm(e,t)?[]:em(),echo:$p(j),label:`AI Connection`}:{connection:j.connection?.id??``,at:j.connection,profile:j.profile,own:j.connection!=null||j.profile!=null,same:L,label:`AI Connection`},r,i,b,{connection:e=>e===`#echo`?m():d(e===``?null:rm(r,e)),profile:e=>f(e===``?rn:e===`#default`?null:rm(i,e))}),se=y.targetAnswerOf(e,A,t),ce=se?a(y.answerKey(se.connection.id,se.profile?.id)):null,le=ie?[{key:`model`,label:`Model`,width:`full`,control:{type:`text`,value:j.model??``,placeholder:ce?.model||`(Not set)`,onChange:e=>te(`model`,(n,r)=>Cm(n,r,t,e))}}]:[],G=y.contentOf(e),de=N?.asks===`request`||H||t===0&&y.CONTENT_TYPES[G?.type??``]?.inline?[]:[{key:`image`,label:`Send image`,control:{type:`switch`,checked:y.sendsImage(P),onChange:n=>l(o(e,t,n))}}],fe=y.flowStepOf(P),pe=fe?[{key:`flow`,label:y.STEP_TYPES.flowStep?.label??`Flow step`,locked:!0,control:{type:`static`,text:fe.step}}]:[],me=M.rows?.(j,n=>te(r=>Object.keys(n).every(n=>{let i=y.targetStepOf(e,A,t),a=y.targetStepOf(e,r,t);return i?.type===a?.type&&JSON.stringify(i?.[n])===JSON.stringify(a?.[n])}),(e,r)=>y.withTarget(e,r,t,e=>{let t={...e,...n};for(let[e,r]of Object.entries(n))r===``&&e!==`prompt`&&e!==`system`&&delete t[e];return t})))??[],he=ee.map(n=>{let r=y.TOKEN_TYPES[n.type],i=r?.fields??[];return{key:n.name,label:r?r.notation(n.name):`{${n.name}}`,code:!0,control:i.includes(`value`)?{type:`text`,value:n.value??``,placeholder:`(Not set)`,onChange:r=>l(T(e,t,n.name,{value:r}))}:{type:`static`,text:r?.label??n.type},...i.includes(`enabled`)?{enabled:{checked:!!n.enabled,onChange:r=>l(T(e,t,n.name,{enabled:r}))}}:{},actions:[{label:`Edit…`,icon:J.edit,onSelect:()=>C({mode:`edit`,name:n.name})}]}}),ge=x?.mode===`edit`?ee.find(e=>e.name===x.name):void 0,_e=n=>{let r=e;x?.mode===`add`?l(it(r,t,n.name,n.type)):ge&&(l(Le(r,t,ge.name,n)),n.type!==ge.type&&ge.value&&u({label:`Changed {${ge.name}} to ${y.TOKEN_TYPES[n.type]?.label.toLowerCase()??n.type}`,restore:()=>l(r)})),C(null)},ve=M.words?.(j),ye=M.promptLabel?.(j)??`Prompt`,be=M.request?.(j)??[],xe=H?Gt(e,t):null;return(0,$.jsxs)(Ne,{children:[H&&(0,$.jsxs)(`div`,{className:`prompt request-prompt`,children:[(0,$.jsxs)(`div`,{className:`pe-head`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:`Prompt`}),(0,$.jsx)(`span`,{className:`request-meta`,children:N?.label??j.type}),(0,$.jsx)($e,{actions:[{label:`Preview responses…`,icon:F,onSelect:()=>k(!0)}],label:`${n} prompt actions`})]}),xe!=null&&(0,$.jsx)(Yt,{className:`readonly`,words:xe,showExprs:!1})]}),O&&(0,$.jsx)(Mm,{pipeline:e,title:`${n} responses`,onClose:()=>k(!1)}),!H&&ye&&(0,$.jsxs)(`div`,{className:`prompt request-prompt`,children:[(0,$.jsxs)(`div`,{className:`pe-head`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:ye}),(0,$.jsx)(`span`,{className:`request-meta`,children:V}),(0,$.jsx)($e,{actions:oe,label:`${n} ${ye.toLowerCase()} actions`})]}),(0,$.jsx)(`textarea`,{ref:R,value:B,"aria-label":`${n}, ${ye.toLowerCase()}`,rows:4,onChange:e=>{let n=e.target.value;te(`prompt`,(e,r)=>S(e,r,t,n))}}),ve&&(0,$.jsx)(Yt,{className:`pe-words`,words:ve.text,was:ve.was,showExprs:!1}),c&&(0,$.jsx)(`span`,{className:`prop-hint`,role:`note`,children:`Content is attached after the prompt`})]}),(0,$.jsx)(w,{label:`${n} settings`,rows:[...W,...le,...me,...de,...pe]}),he.length?(0,$.jsx)(w,{label:`Tokens`,rows:he,addLabel:`Add token`,onAdd:()=>C({mode:`add`}),onRemove:n=>{let r=e;l(ae(e,t,n.key)),u({label:`Removed {${n.key}}`,restore:()=>l(r)})}}):(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>C({mode:`add`}),children:[(0,$.jsx)(J.add,{size:14,"aria-hidden":`true`}),`Add token`]}),be.length>0&&(0,$.jsx)(Tt,{id:`request-${t}-flow-request`,title:`Flow's request`,level:3,open:!1,children:(0,$.jsx)(w,{label:`Flow's request`,rows:be})}),x&&(x.mode===`add`||ge)&&(0,$.jsx)(Je,{title:ge?y.TOKEN_TYPES[ge.type]?.notation(ge.name)??`{${ge.name}}`:`New token`,initial:ge?{name:ge.name,type:ge.type}:{name:``,type:`value`},adding:!ge,problem:n=>g(e,t,n,ge?.name),onSave:_e,onClose:()=>C(null)},x.mode===`edit`?x.name:`add`),E&&(0,$.jsx)(Ae,{title:`Insert token`,onClose:()=>D(!1),dismiss:`Cancel`,children:(0,$.jsx)(`div`,{className:`pick-others`,children:U.map(e=>(0,$.jsx)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>{D(!1),re(e)},children:(0,$.jsx)(`code`,{children:e})},e))})})]})}function Pm(e){let t=e.trim().split(/\s+/).filter(Boolean);return t.length?t.length>6?`${t.slice(0,6).join(` `)}…`:t.join(` `):`(No prompt)`}function Fm({text:e,writtenFor:t,say:n,onClose:r}){let i=async()=>{n(await Pt(e)?`Copied the request.`:`Could not copy.`),r()};return(0,$.jsx)(De,{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:Fe,primary:!0,onSelect:()=>void i()}]})}function Im({pipeline:e,i:t,k:n,variant:i,request:a,label:o,prompts:s,tokens:c,onSave:l,extra:u=[],onClose:d}){K();let f=sm(e,n)?um(e,n):Ve(e,0,n),p=Ve(e,t,n)??{type:`prompt`,prompt:``},[m,h]=(0,Y.useState)(p),[g,_]=(0,Y.useState)(!1),[v,y]=(0,Y.useState)(!1),[b,x]=(0,Y.useState)(!1),S=si(),C=Ft(m.type),w=o??C.promptLabel?.(m)??`Prompt`,T=m.prompt??``,E=e=>{if(!e?.from)return Pm(e?.prompt??``);let t=s?.find(t=>t.id===e.from.id);return t?`${ti(t)}, version ${t.version}`:e.from.name},D=(e,t)=>h(n=>{let{from:r,...i}=n;return{...i,prompt:e,...t?{from:{...t}}:{}}}),O=(0,Y.useRef)(null),k=(0,Y.useRef)(null);(0,Y.useLayoutEffect)(()=>{if(k.current==null||!O.current)return;let e=k.current;k.current=null,O.current.focus(),O.current.setSelectionRange(e,e)},[m.prompt]);let A=e=>{let t=O.current,n=t?.selectionStart??T.length,r=t?.selectionEnd??T.length;h(t=>({...t,prompt:T.slice(0,n)+e+T.slice(r)})),k.current=n+e.length},j=!!f&&f.type===m.type&&(f.prompt??``)===T&&(f.from?.id??``)===(m.from?.id??``),M=[{label:`Save to library…`,icon:bt,disabled:!T,onSelect:()=>x(!0)},{label:`Clear override`,icon:J.clear,disabled:!f||j,onSelect:()=>f&&D(f.prompt??``,f.from??null)},...u],N=(C.rows?.(m,e=>h(t=>{let n={...t,...e};for(let[t,r]of Object.entries(e))r===``&&t!==`prompt`&&t!==`system`&&delete n[t];return n}))??[]).filter(e=>e.key!==`model`),P=C.words?.(m),F=C.fields?.(m)??[],I=C.request?.(m)??[],ee=`${i}, ${a} ${w.toLowerCase()}`,{api:L,...te}=m;return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(De,{title:ee,wide:!0,className:`variant-prompt`,dirty:JSON.stringify(m)!==JSON.stringify(p),onSave:()=>{l(te),d()},onClose:d,sections:[{key:`words`,rows:N,children:(0,$.jsxs)(`div`,{className:`variant-words`,children:[f&&(0,$.jsxs)(`div`,{className:`prompt`,children:[(0,$.jsxs)(`div`,{className:`pe-head`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:a}),(0,$.jsx)(`span`,{className:`request-meta`,children:E(f)})]}),(0,$.jsx)(Yt,{className:`readonly`,words:f.prompt??``,showExprs:!1})]}),(0,$.jsxs)(`div`,{className:`prompt`,children:[(0,$.jsxs)(`div`,{className:`pe-head`,children:[(0,$.jsx)(`span`,{className:`prop-name`,children:i}),(0,$.jsx)(`span`,{className:`request-meta`,children:E(m)}),(0,$.jsxs)(`span`,{className:`variant-words-actions`,children:[F.length>0&&(0,$.jsx)(Kt,{fields:F,onInsert:A,label:i}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,onClick:()=>_(!0),children:[(0,$.jsx)(ue,{size:14,"aria-hidden":`true`}),`Select prompt…`]}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost`,disabled:!c.length,onClick:()=>y(!0),children:[(0,$.jsx)(Nt,{size:14,"aria-hidden":`true`}),`Insert token…`]}),(0,$.jsx)($e,{actions:M,label:`${i} ${w.toLowerCase()} actions`})]})]}),(0,$.jsx)(`textarea`,{ref:O,value:T,"aria-label":ee,rows:6,onChange:e=>{let t=e.target.value;h(e=>({...e,prompt:t}))}}),P&&(0,$.jsx)(Yt,{className:`pe-words`,words:P.text,was:P.was,showExprs:!1})]})]})},...I.length?[{key:`request`,title:`Flow's request`,rows:I,folded:!0}]:[]]}),g&&(0,$.jsx)(Sp,{onClose:()=>_(!1),used:{byScenario:r(e,t),byAll:yt(e)},onPick:e=>{D(e.text,{id:e.id,name:ti(e)}),_(!1)}}),v&&(0,$.jsx)(Ae,{title:`Insert token`,onClose:()=>y(!1),dismiss:`Cancel`,children:(0,$.jsx)(`div`,{className:`pick-others`,children:c.map(e=>(0,$.jsx)(`button`,{type:`button`,className:`ghost pick-item`,onClick:()=>{y(!1),A(e)},children:(0,$.jsx)(`code`,{children:e})},e))})}),b&&(0,$.jsx)(sn,{title:`Save to library`,value:T.split(`
|
|
9
|
-
`)[0]?.trim()??``,onCommit:e=>void(async()=>{let t=await S.create({name:e,text:T});D(T,{id:t.id,name:ti(t)})})(),onClose:()=>x(!1)})]})}function Lm(e,t){let n=K(),r=n.jobsOf(e)[t];return[...r?n.tokensOf(r).map(e=>n.TOKEN_TYPES[e.type]?.notation(e.name)??`{${e.name}}`):[],...t>0?[`{reply}`]:[]]}var Rm=e=>`${e.pack??``}/${e.id}`;function zm({pipeline:e,free:t,names:n,resolve:r,onAdd:i,onClose:a}){let o=X(),s=K(),c=Om(e),[l,u]=(0,Y.useState)(c[0]??0),[d,f]=(0,Y.useState)(null),[p,m]=(0,Y.useState)([]),h=(0,Y.useId)();(0,Y.useEffect)(()=>{let e=!0;return o.presets().then(t=>{e&&f(t)}).catch(()=>{e&&f([])}),()=>{e=!1}},[o]);let g=s.targetAnswerOf(e,0,l),_=g?r(s.answerKey(g.connection.id,g.profile?.id))?.type:void 0,v=(d??[]).filter(e=>!_||(e.type??`llama.cpp`)===_),y=v.filter(e=>p.includes(Rm(e))),b=[...new Set(v.map(e=>e.packName??`Lab`))],x=()=>{y.length&&i(l,y)};return(0,$.jsx)(De,{title:`Add variants from presets`,className:`from-presets`,onClose:a,dirty:p.length>0,sections:[{key:`request`,rows:[{key:`request`,label:`Request`,control:{type:`select`,value:String(l),options:c.map(e=>({value:String(e),label:n(e)})),onChange:e=>u(Number(e))}}]},{key:`presets`,children:(0,$.jsxs)(`div`,{role:`group`,"aria-labelledby":h,className:`preset-pick`,onKeyDown:e=>{e.key===`Enter`&&e.target.type===`checkbox`&&(e.preventDefault(),x())},children:[(0,$.jsxs)(`div`,{className:`preset-head`,children:[(0,$.jsx)(`span`,{id:h,children:`Presets`}),(0,$.jsx)(`span`,{className:`preset-count`,children:`${y.length} of ${t}`})]}),d&&!v.length&&(0,$.jsx)(`p`,{className:`unset`,children:`(No presets)`}),b.map(e=>(0,$.jsxs)(`div`,{className:`preset-group`,role:`group`,"aria-label":e,children:[(0,$.jsx)(`h4`,{children:e}),v.filter(t=>(t.packName??`Lab`)===e).map(e=>{let n=p.includes(Rm(e));return(0,$.jsxs)(`label`,{className:`multi-opt`,children:[(0,$.jsx)(`input`,{type:`checkbox`,checked:n,disabled:!n&&y.length>=t,onChange:t=>m(n=>t.target.checked?[...n,Rm(e)]:n.filter(t=>t!==Rm(e)))}),(0,$.jsx)(`span`,{children:e.label})]},Rm(e))})]},e))]})}],actions:[{label:`Add ${y.length} ${y.length===1?`variant`:`variants`}`,primary:!0,disabled:!y.length,onSelect:x}]})}var Bm=e=>`${e.i}:${e.k}:${e.row}`,Vm=4;function Hm({pipeline:e,connections:t,connectionProfiles:n,resolve:r,answers:i,fromProfile:a,prompts:o,targetCap:s,names:c,edit:l,undoable:u,onRename:d,onCopyRequest:f,say:p}){let m=K(),h=m.targetsOf(e),g=m.jobsOf(e),_=Jm(),[v,b]=(0,Y.useState)(null),[x,S]=(0,Y.useState)(null),[C,w]=(0,Y.useState)(null),[T,E]=(0,Y.useState)(!1),[D,O]=(0,Y.useState)({}),k=Math.min(s,Vm),A=e=>c[g[e]?.id??``]??`Request ${e+1}`,M=t=>t===0&&!g.some((t,n)=>sm(e,n)),N=t=>{let n=g.some((n,r)=>!ym(e,t,r,`prompt`));return[{label:`Rename…`,icon:J.rename,onSelect:()=>d(t)},{label:`Clone`,icon:J.clone,disabled:h.length>=k,onSelect:()=>l(ft(e,t))},...n?[{label:`Preview responses…`,icon:F,onSelect:()=>w(t)}]:[],...h.length>1?[{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>u(`Removed ${Pe(e,t)}`,y(e,t))}]:[]]},P=s=>(0,$.jsx)(qm,{at:s,pipeline:e,variant:Pe(e,s.i),request:A(s.k),connections:t,connectionProfiles:n,resolve:r,prompts:o,editing:v===Bm(s),onEdit:e=>b(e?Bm(s):null),onPrompt:()=>S({i:s.i,k:s.k}),onAnsweredBy:t=>l(t?Sm(e,s.i,s.k,t):Tm(e,s.i,s.k,s.row,i)),onConnection:n=>l(n===`#echo`?je(e,s.i):j(e,s.i,s.k,rm(t,n),i,a)),onProfile:t=>l(Oe(e,s.i,s.k,t===`#default`?null:rm(n,t),i)),onModel:t=>l(Cm(e,s.i,s.k,t)),onClear:()=>l(Tm(e,s.i,s.k,s.row,i))},Bm(s)),I=t=>{let n=!!h[t]?.name?.trim();return(0,$.jsxs)(`span`,{className:`variant-head`,children:[n?Pe(e,t):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`span`,{className:`sr-only`,children:`Variant`}),` ${m.targetLetter(t)}`]}),(0,$.jsx)($e,{actions:N(t),label:`${Pe(e,t)} actions`})]})},ee=(0,$.jsx)(`div`,{className:`scrollx`,children:(0,$.jsxs)(`table`,{className:`variants`,"aria-label":`Variants`,children:[(0,$.jsx)(`thead`,{children:(0,$.jsxs)(`tr`,{children:[(0,$.jsx)(`td`,{}),h.map((e,t)=>(0,$.jsx)(`th`,{scope:`col`,children:I(t)},e.id))]})}),g.map((t,n)=>(0,$.jsxs)(`tbody`,{children:[(0,$.jsx)(`tr`,{className:`variants-request`,children:(0,$.jsx)(`th`,{scope:`rowgroup`,colSpan:h.length+1,children:A(n)})}),bm(e,n).map(t=>(0,$.jsxs)(`tr`,{children:[(0,$.jsx)(`th`,{scope:`row`,children:om(e,n,t)}),h.map((e,r)=>(0,$.jsx)(`td`,{children:P({i:r,k:n,row:t})},e.id))]},t))]},t.id))]})}),L=(0,$.jsx)(`ul`,{className:`variant-cards`,"aria-label":`Variants`,children:h.map((r,i)=>{let a=!M(i)&&!!D[r.id],s=xm(e,i);return(0,$.jsxs)(`li`,{className:`variant-card`,children:[(0,$.jsxs)(`div`,{className:`variant-card-head`,children:[M(i)?(0,$.jsx)(`span`,{className:`variant-card-name`,children:Pe(e,i)}):(0,$.jsxs)(`button`,{type:`button`,className:`variant-card-toggle`,"aria-expanded":a,onClick:()=>O(e=>({...e,[r.id]:!a})),children:[(0,$.jsx)(ce,{className:`step-caret`,size:14,"aria-hidden":`true`}),Pe(e,i)]}),(0,$.jsx)($e,{actions:N(i),label:`${Pe(e,i)} actions`})]}),a?g.map((t,n)=>(0,$.jsxs)(`div`,{className:`variant-card-request`,children:[(0,$.jsx)(`h4`,{children:A(n)}),(0,$.jsx)(`dl`,{className:`variant-pairs`,children:bm(e,n).map(t=>(0,$.jsxs)(`div`,{children:[(0,$.jsx)(`dt`,{children:om(e,n,t)}),(0,$.jsx)(`dd`,{children:P({i,k:n,row:t})})]},t))})]},t.id)):s.length?(0,$.jsx)(`dl`,{className:`variant-pairs`,children:s.map(({k:r,row:a})=>(0,$.jsxs)(`div`,{children:[(0,$.jsxs)(`dt`,{children:[A(r),` `,om(e,r,a)]}),(0,$.jsx)(`dd`,{children:Um({pipeline:e,i,k:r,row:a,connections:t,connectionProfiles:n,prompts:o})})]},`${r}:${a}`))}):(0,$.jsx)(`p`,{className:`unset`,children:`(No overrides)`})]},r.id)})});return(0,$.jsxs)(Tt,{id:`variants`,title:`Variants`,note:String(h.length),actions:[{label:`Add variants from presets…`,icon:J.preset,disabled:h.length>=k||!Om(e).length,onSelect:()=>E(!0)}],children:[_?L:ee,(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,disabled:h.length>=k,onClick:()=>l(Dm(e)),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add variant`]}),x&&(()=>{let{i:t,k:n}=x,r=Ve(e,t,n),i=(Ft(r?.type).actions?.({pipeline:e,i:t,k:n,say:p,onSelectPrompt:()=>{},onClearPrompt:()=>{},onCopyRequest:()=>f(t,n)})??[]).filter(e=>![`Select prompt…`,`Save to library…`,`Clear prompt`,`Revert to default`].includes(e.label));return(0,$.jsx)(Im,{pipeline:e,i:t,k:n,variant:Pe(e,t),request:A(n),label:sm(e,n)?om(e,n,`prompt`):void 0,prompts:o,tokens:Lm(e,n),extra:i,onSave:r=>l(m.withTarget(e,t,n,()=>r)),onClose:()=>S(null)})})(),T&&(0,$.jsx)(zm,{pipeline:e,free:k-h.length,names:A,resolve:r,onAdd:(t,n)=>{l(jm(e,t,n)),E(!1)},onClose:()=>E(!1)}),C!==null&&(0,$.jsx)(Mm,{pipeline:e,title:`${Pe(e,C)} responses`,onClose:()=>w(null)})]})}function Um({pipeline:e,i:t,k:n,row:r,connections:i,connectionProfiles:a,prompts:o}){let s=K(),c=s.targetStepOf(e,t,n),l=s.targetAnswerOf(e,t,n);if(vm(e,t,n,r))return`(Not sent)`;if(!ym(e,t,n,r))return`(None)`;switch(r){case`answeredBy`:return dm(e,t,n);case`connection`:{let e=s.STEP_TYPES[c?.type??``];return e?.local?e.label??c.type:l?Wm(l.connection,i):`(No connection)`}case`profile`:return l?.profile?Wm(l.profile,a):`(Default)`;case`model`:return c?.model||`(Not set)`;case`settings`:return Gm(c?.settings);case`prompt`:return sm(e,n)&&_m(e,t,n,r)?`Edited`:Km(c,o)}}var Wm=(e,t)=>t.some(t=>t.id===e.id)?e.name||e.id:`${e.name||e.id} (missing)`;function Gm(e){let t=K(),n=e=>Object.values(t.CONNECTION_TYPES).flatMap(e=>e.settings).find(t=>t.key===e)?.label??e,r=Object.entries(e??{}).filter(([,e])=>e.trim()).map(([e,t])=>`${n(e)} ${t}`);return r.length?r.join(`, `):`(Not set)`}function Km(e,t){if(!e?.from)return Pm(e?.prompt??``);let n=t?.find(t=>t.id===e.from.id);return n?`${ti(n)}, version ${n.version}`:e.from.name}function qm({at:e,pipeline:t,variant:n,request:r,connections:i,connectionProfiles:a,resolve:o,prompts:s,editing:c,onEdit:l,onPrompt:u,onAnsweredBy:d,onConnection:f,onProfile:p,onModel:m,onClear:h}){let g=K(),_=Ws(),v=(0,Y.useId)(),y=(0,Y.useRef)(null),b=(0,Y.useRef)(!1);(0,Y.useEffect)(()=>{!c&&b.current&&(b.current=!1,y.current?.focus())},[c]);let x=()=>{b.current=!0,l(!1)},{i:S,k:C,row:w}=e,T=`${n}, ${r} ${om(t,C,w)}`,E=_m(t,S,C,w),D=sm(t,C),O=lm(t,C);if(!D&&S===0)return(0,$.jsx)(`span`,{className:`unset`,children:`(Same)`});if(vm(t,S,C,w))return(0,$.jsx)(`span`,{className:`unset`,children:`(Not sent)`});if(!E&&!ym(t,O,C,w))return(0,$.jsx)(`span`,{className:`unset`,children:`(Same)`});if(!ym(t,S,C,w))return(0,$.jsx)(`span`,{className:`unset`,children:`(None)`});if(w===`settings`&&!E||D&&S===O&&(w===`connection`||w===`profile`))return(0,$.jsx)(`span`,{className:`unset`,children:`(Same)`});let k=Um({pipeline:t,i:S,k:C,row:w,connections:i,connectionProfiles:a,prompts:s});if(!c)return(0,$.jsx)(`button`,{ref:y,type:`button`,className:`variant-cell${E?` override`:``}`,"aria-label":`${T}: ${E?k:`(Same)`}`,onClick:()=>w===`prompt`?u():l(!0),children:E?k:`(Same)`});let A=e=>{let t=e.relatedTarget;(t?e.currentTarget.contains(t):document.querySelector(`[role=dialog]`))||l(!1)},j=e=>{e.key===`Escape`&&!e.target.closest(`[role=dialog]`)&&(e.stopPropagation(),x())},M=g.targetAnswerOf(t,S,C),P=g.targetStepOf(t,S,C),F=null;if(w===`answeredBy`)F=(0,$.jsxs)(`select`,{autoFocus:!0,"aria-labelledby":v,value:E?P?.type??``:``,onChange:e=>{d(e.target.value),x()},children:[(0,$.jsx)(`option`,{value:``,children:`(Same)`}),fm(t,C).map(e=>(0,$.jsx)(`option`,{value:e.type,children:e.label},e.type))]});else if(w===`model`){let e=M?o(g.answerKey(M.connection.id,M.profile?.id)):null;F=(0,$.jsx)(`input`,{type:`text`,autoFocus:!0,"aria-labelledby":v,value:P?.model??``,placeholder:e?.model||`(Not set)`,onChange:e=>m(e.target.value)})}else if(w===`connection`){let e=g.STEP_TYPES[P?.type??``]?.local?Zp:M?.connection.id??``;F=(0,$.jsx)(N,{kind:`Connection`,labelledBy:v,value:e,..._(nm(i,M?.connection),C===0&&!D?em():[]),onChange:f})}else w===`settings`?F=(0,$.jsx)(`span`,{className:`variant-settings`,"aria-labelledby":v,children:k}):w===`profile`&&(F=(0,$.jsx)(N,{kind:`Profile`,labelledBy:v,value:M?.profile?.id??`#default`,...Bs(nm(a,M?.profile),()=>null,[{id:Qp,label:`(Default)`}]),onChange:p}));return(0,$.jsxs)(`span`,{className:`variant-edit`,onBlur:A,onKeyDown:j,children:[(0,$.jsx)(`span`,{id:v,className:`sr-only`,children:T}),F,(0,$.jsx)(`button`,{type:`button`,className:`icon-btn`,"aria-label":`Clear override`,title:`Clear override`,disabled:!E,autoFocus:w===`settings`,onMouseDown:e=>e.preventDefault(),onClick:()=>{h(),x()},children:(0,$.jsx)(J.clear,{size:16,"aria-hidden":`true`})})]})}function Jm(){let[e]=(0,Y.useState)(()=>typeof matchMedia==`function`?matchMedia(`(max-width: 600px)`):null),[t,n]=(0,Y.useState)(!!e?.matches);return(0,Y.useEffect)(()=>{if(!e)return;let t=()=>n(e.matches);return e.addEventListener(`change`,t),()=>e.removeEventListener(`change`,t)},[e]),t}function Ym({pipeline:t,connections:n,connectionProfiles:i,resolve:a,sources:o,joined:s,targetCap:c,protectedTerms:l,edit:u,offer:d}){let f=K(),m=Fp(t),h=f.stepNames(t),g=f.stepFaults(t),_=m.find(e=>e.type===`request`)?.id,[v,y]=(0,Y.useState)({}),b=e=>v[e]??e===_,x=e=>y(t=>({...t,[e]:!b(e)})),[S,C]=(0,Y.useState)(null),[w,T]=(0,Y.useState)(null),[D,O]=(0,Y.useState)(null),[k,A]=(0,Y.useState)(null),[N,F]=(0,Y.useState)(null),[I,L]=(0,Y.useState)(null),te=n.filter(e=>f.listedConnection(e)),R=si(),{list:ne}=ci(),re=Oc(),[,z]=(0,Y.useState)(0);(0,Y.useEffect)(()=>re.subscribe(()=>z(e=>e+1)),[re]);let B=()=>{let e=f.contentOf(t),n=e?.type===`source`?e.ref.id:null,r=n?o.find(e=>e.id===n)?.platform:void 0;return r&&f.WORKFLOW_PLATFORMS[r]||null},V=(e,n)=>{let r=t;u(n),d({label:e,restore:()=>u(r)})},H=e=>{let t=a(f.answerKey(e.connection.id,e.profile?.id));return t?f.CONNECTION_TYPES[f.typeOf(t)]?.answers??[`prompt`]:void 0},ie=e=>i.find(t=>t.id===e)?.fromProfile,U=e=>{if(e.type===`getItems`)return Xp(e.content,o);let n=Rp(t,e.type===`request`?e.id:e.request),r=f.jobsOf(t)[n];if(e.type===`request`){let e=lm(t,n),r=f.targetStepOf(t,e,n),i=r?.from?ne?.find(e=>e.id===r.from.id):void 0;return[r?.from?i?ti(i):r.from.name:Pm(r?.prompt??``),f.targetAnswerOf(t,e,n)?.connection.name??(r&&f.STEP_TYPES[r.type]?.local?f.STEP_TYPES[r.type]?.label:void 0)].filter(Boolean).join(`, `)}if(!r)return``;if(e.type===`readResponse`){let e=f.outOf(r),t=Zt().find(t=>t.id===e.kind)?.label??e.kind,n=e.modifiers.map(e=>Lt().find(t=>t.id===e.type)?.label??e.type);return[...f.readAsOf(r)==null?[]:[`Read as`],t,...n].join(`, `)}let i=f.filterSetsOf(r),a=i.flatMap(e=>e.set?[e.set.name||e.set.id]:[]),s=i.reduce((e,t)=>e+(t.own?.filters.length??0),0);return[...a,...s?[`${s} ${s===1?`filter`:`filters`}`]:[]].join(`, `)||`(No filters)`},ae=(e,n)=>{let r=h[e.id]??e.type,i=e.type===`filter`?Rp(t,e.request):-1;return[{label:`Move up`,icon:J.moveUp,disabled:n===0,onSelect:()=>u(zp(t,e.id,-1))},{label:`Move down`,icon:J.moveDown,disabled:n===m.length-1,onSelect:()=>u(zp(t,e.id,1))},{label:`Clone`,icon:J.clone,disabled:!Hp(t,e.id),onSelect:()=>u(Up(t,e.id))},...e.type===`filter`?[{label:`Move all to filter set…`,icon:cr,disabled:!Pp(t,i),onSelect:()=>{y(t=>({...t,[e.id]:!0})),F(i)}}]:[],...Bp(t,e.id)?[{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>V(`Removed ${r}`,Vp(t,e.id))}]:[]]},oe=m.filter(e=>e.type===`request`).length,W=e=>{let n=Gp(t,e);n&&(u(n.next),y(e=>({...e,[n.id]:!0})))},se=[{label:`Send request`,icon:Er,description:`Adds Request ${oe+1}`,onSelect:()=>W(`request`)},{label:`Read response`,icon:fr,description:`Read as, Response Format`,disabled:!Wp(t,`readResponse`),onSelect:()=>W(`readResponse`)},{label:`Filter`,icon:E,description:`A filter set, or filters of its own`,disabled:!Wp(t,`filter`),onSelect:()=>W(`filter`)},{label:`Get items`,icon:M,description:`Adds more items, before Request 1`,onSelect:()=>W(`getItems`)}],le=n=>{if(n.type===`getItems`)return(0,$.jsx)(qp,{stepId:n.id,pipeline:t,sources:o,edit:u});let r=Rp(t,n.type===`request`?n.id:n.request),c=f.jobsOf(t)[r];if(!c)return null;let p=m.some(e=>e.type===`filter`&&e.request===c.id),g=f.formatReadsItems(f.outOf(c).kind);if(n.type===`request`){let e=h[n.id],o=lm(t,r),l=Ve(t,o,r),p=(Ft(l?.type).actions?.({pipeline:t,i:o,k:r,say:L,onSelectPrompt:()=>T({i:o,k:r}),onClearPrompt:()=>{},onCopyRequest:()=>A({i:o,k:r})})??[]).filter(e=>![`Select prompt…`,`Save to library…`,`Clear prompt`,`Revert to default`].includes(e.label));return(0,$.jsx)(Nm,{pipeline:t,k:r,name:e,connections:f.flowStepOf(c)?te:te.filter(Gs),connectionProfiles:i,resolve:a,prompts:ne,joined:s?.(o,r)??!1,edit:u,offer:d,onConnection:e=>u(Em(t,r,`connection`,(t,n)=>j(t,n,r,e,H,ie))),onProfile:e=>u(Em(t,r,`profile`,(t,n)=>Oe(t,n,r,e,H))),onEcho:()=>u(Em(t,r,`connection`,(e,t)=>je(e,t))),onSelectPrompt:()=>T({i:o,k:r}),onSaveToLibrary:()=>O({i:o,k:r}),extra:p})}return n.type===`readResponse`?(0,$.jsx)(rt,{id:`step-${n.id}-read`,bare:!0,out:f.outOf(c),readAs:f.flowStepOf(c)?f.readAsOf(c):void 0,outputKinds:Zt(),modifiers:Lt(),onFormat:e=>u(Se(t,r,e)),onSetting:(e,n)=>u(Xm(t,r,t=>({...t,[e]:n}))),onReadAs:e=>e==null?V(`Removed Read as`,P(t,r,null)):u(P(t,r,e)),...l?{protectedTerms:l}:{},offer:d,onModifier:(e,n)=>u(Zm(t,r,e,n)),onAddModifier:e=>u(Xm(t,r,t=>({...t,modifiers:[...t.modifiers,e]}))),onRemoveModifier:e=>V(`Removed a modifier`,Xm(t,r,t=>({...t,modifiers:t.modifiers.filter((t,n)=>n!==e)}))),formatInFilters:g&&p}):(0,$.jsx)(Np,{pipeline:t,job:r,edit:u,offer:d,format:g?(0,$.jsx)(e,{out:f.outOf(c),outputKinds:Zt(),onFormat:e=>u(Se(t,r,e)),onSetting:(e,n)=>u(Xm(t,r,t=>({...t,[e]:n})))}):void 0,moveAll:N===r,onMoveAllClose:()=>F(null)})};return(0,$.jsxs)(`div`,{className:`jobs`,children:[(0,$.jsxs)(Tt,{id:`steps`,title:`Steps`,note:String(m.length),children:[I&&(0,$.jsx)(`p`,{className:`note`,role:`status`,children:I}),(0,$.jsx)(`ol`,{className:`steps`,"aria-label":`Steps, in run order`,children:m.map((e,t)=>{let n=h[e.id]??e.type,r=b(e.id),i=g[e.id];return(0,$.jsxs)(`li`,{id:`step-${e.id}`,className:`step${r?` open`:``}${i?` out-of-order`:``}`,children:[(0,$.jsxs)(`div`,{className:`step-row`,children:[(0,$.jsxs)(`button`,{type:`button`,className:`step-toggle`,"aria-expanded":r,"aria-controls":`step-${e.id}-body`,onClick:()=>x(e.id),children:[(0,$.jsx)(ce,{className:`step-caret`,size:14,"aria-hidden":`true`}),(0,$.jsx)(`span`,{className:`step-num`,children:t+1}),(0,$.jsx)(`span`,{className:`step-name`,children:n}),!r&&(0,$.jsx)(`span`,{className:`step-sum`,children:U(e)})]}),(0,$.jsx)($e,{actions:ae(e,t),label:`${n} actions`})]}),i&&(0,$.jsx)(`p`,{className:`step-fault`,children:i}),r&&(0,$.jsx)(`div`,{className:`step-body`,id:`step-${e.id}-body`,children:le(e)})]},e.id)})}),(0,$.jsx)(`span`,{className:`add-btn`,children:(0,$.jsx)($e,{actions:se,label:`Add step`,trigger:(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(J.add,{size:14,"aria-hidden":`true`}),`Add step`]})})})]}),(0,$.jsx)(Hm,{pipeline:t,connections:te,connectionProfiles:i,resolve:a,answers:H,fromProfile:ie,prompts:ne,targetCap:c,names:h,edit:u,undoable:V,onRename:C,onCopyRequest:(e,t)=>A({i:e,k:t}),say:L}),S!==null&&(0,$.jsx)(sn,{title:`Rename ${Pe(t,S)}`,value:f.targetsOf(t)[S]?.name??``,onCommit:e=>u(ee(t,S,e)),onClose:()=>C(null)}),w&&(0,$.jsx)(Sp,{onClose:()=>T(null),used:{byScenario:r(t,w.i),byAll:yt(t)},onPick:e=>{let n=t,r=w.k;u(Em(n,r,`prompt`,(t,n)=>p(t,n,r,e.text,{id:e.id,name:ti(e)}))),T(null),d({label:`Picked ${ti(e)}`,restore:()=>u(n)})}}),D&&(()=>{let{i:e,k:n}=D,r=f.targetStepOf(t,e,n)?.prompt??``;return(0,$.jsx)(sn,{title:`Save to library`,value:r.split(`
|
|
10
|
-
`)[0]?.trim()??``,onCommit:e=>void(async()=>{let i=await R.create({name:e,text:r});u(Em(t,n,`prompt`,(e,t)=>p(e,t,n,r,{id:i.id,name:ti(i)}))),L(`Saved ${ti(i)} to the library.`)})(),onClose:()=>O(null)})})(),k&&(()=>{let e=Ve(t,k.i,k.k);if(!e)return null;let{text:n,writtenFor:r}=gt(e,B());return(0,$.jsx)(Fm,{text:n,writtenFor:r,say:L,onClose:()=>A(null)})})()]})}var Xm=(e,t,n)=>K().withJobs(e,K().jobsOf(e).map((e,r)=>r===t?K().withOut(e,n(K().outOf(e))):e)),Zm=(e,t,n,r)=>Xm(e,t,e=>({...e,modifiers:e.modifiers.map((e,t)=>t===n?r:e)}));function Qm({pipeline:e,sourceFiles:t,graded:n,openCaseEditor:r}){let[i,a]=(0,Y.useState)({graded:!1,todo:!0,missing:!0});if((K().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)(Tt,{id:`mapping`,title:`Mapping`,level:3,note:f(),about:`Items with an eval case are graded.`,children:o.length?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(re,{className:`mkey`,label:`Show`,options:l.filter(e=>e.files.length).map(e=>({value:e.key,label:`${e.label} ${e.files.length}`})),on:new Set(l.filter(e=>i[e.key]).map(e=>e.key)),onToggle:e=>a({...i,[e]:!i[e]})}),d.length?(0,$.jsx)(`div`,{className:`mchips`,children:d.map(({file:e,tone:t})=>(0,$.jsx)(`button`,{type:`button`,className:`badge${t===`ok`?``:` rust`} mchip ${t}`,onClick:()=>r(e),children:e},`${t}-${e}`))}):u?(0,$.jsx)(`p`,{className:`unset`,children:`(No matches)`}):null]}):(0,$.jsx)(`p`,{className:`note`,children:`(No items)`})});function f(){let r=K().contentOf(e),i=r?K().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`${fa(n.length,`eval case`)}, ${s} of ${a.length} matched`}}function $m({pipeline:e,edit:t,comment:n,setComment:r,onClear:i,onCopyFailures:a}){let o=[{label:`Clear comment`,icon:Wt,destructive:!0,onSelect:i},{label:`Copy last run's failures`,icon:ke,onSelect:a}],s=K().contentOf(e),c=s?.type===`source`?[{key:`first`,label:`Run first`,width:`quarter`,control:{type:`text`,value:s.first==null?``:String(s.first),placeholder:`all`,clearable:!0,onChange:n=>t(K().withContent(e,{...s,first:n.trim()?eh(n):null}))}},{key:`loops`,label:`Loop`,width:`quarter`,control:{type:`text`,value:String(s.loops),placeholder:`1`,onChange:n=>t(K().withContent(e,{...s,loops:Math.max(1,eh(n)??1)}))}}]:[];return(0,$.jsx)(Tt,{id:`control`,title:`Run settings`,level:2,actions:o,children:(0,$.jsx)(w,{label:`Run settings`,rows:[...c,{key:`comment`,label:`Comment`,width:`full`,control:{type:`text`,value:n,placeholder:`Add a note`,onChange:e=>r(e)}}]})})}var eh=e=>{let t=Number.parseInt(e,10);return Number.isNaN(t)?null:t};function th({pipelineId:e=null}){let t=Fr(),n=X(),r=Dt(),i=Ss(),a=Os(),{openRun:o}=$i(),{openCaseEditor:s}=lc(),[c,l]=Z(`promptlab.workflows`),[u]=Z(`promptlab.connections`),[d]=Z(`promptlab.connectionProfiles`),f=(0,Y.useMemo)(()=>K().answersOf(K().withSlugs(u.list),d.list,d.default??null),[u.list,d.list,d.default]),p=(0,Y.useRef)(f);p.current=f;let[m,h]=Z(`promptlab.comment`),[g]=Z(`promptlab.versions`),[_,v]=(0,Y.useState)([]),{list:y,error:b}=$r(),{list:x,error:S}=ci(),{list:C}=kc(),w=x?.find(e=>e.default)??null,T=Qr(),E=(0,Y.useMemo)(()=>(y??[]).map(e=>Xe(e.id,e.name)),[y]),D=(0,Y.useCallback)(()=>(y??[]).map(e=>{let t=T.entry(e.id)?.doc?.body;return{id:e.id,name:e.name,grader:t?t.grader??null:e.grader??null}}),[y,T]),[O,k]=(0,Y.useState)(4),[A,j]=(0,Y.useState)(void 0),[M,N]=(0,Y.useState)({}),[P,F]=(0,Y.useState)(null),[I,ee]=(0,Y.useState)(null),L=(0,Y.useRef)(I);L.current=I;let[te,R]=(0,Y.useState)(!1),[ne,re]=(0,Y.useState)(!1),[z,B]=(0,Y.useState)(null),V=(0,Y.useRef)(null),[H,ie]=(0,Y.useState)(null),[U,ae]=(0,Y.useState)(!1),[oe,W]=(0,Y.useState)(!1),se=(0,Y.useRef)(null);(0,Y.useEffect)(()=>{let e=!0;return n.sources().then(t=>{e&&v(t.sources)}).catch(()=>{}),n.config().then(t=>{e&&k(t.targetCap)}).catch(()=>{}),()=>{e=!1}},[n]),(0,Y.useEffect)(()=>{let e=!0,t,r=async()=>{let i=5e3;try{let t=(await n.queue({limit:1})).runs[0]??null;if(!e)return;!V.current&&t&&!La(t.status)&&(V.current=t.id),t&&t.id===V.current&&(B(t),La(t.status)||(i=1e3))}catch{}e&&(t=setTimeout(()=>void r(),i))};return r(),()=>{e=!1,t&&clearTimeout(t)}},[n]);let ce=(0,Y.useMemo)(()=>c.list.filter(e=>e.work?.version===20),[c.list]),le=e?ce.find(t=>t.id===e):void 0,G=le??ce.find(e=>e.id===c.at)??ce[0],ue=G?.work,de=G?.id??``,fe=m&&(m.pipeline??de)===de?m.text:``,pe=(0,Y.useCallback)(e=>h({text:e,pipeline:de}),[h,de]);(0,Y.useEffect)(()=>{m&&m.pipeline===void 0&&de&&h({...m,pipeline:de})},[m,de,h]);let me=!!e&&!le&&ce.length>0,ge=(0,Y.useRef)(new Set);(0,Y.useEffect)(()=>{le&&ge.current.add(le.id)},[le]),(0,Y.useEffect)(()=>{me&&e&&(Di({tab:`runs`,id:null}),ge.current.has(e)||r.notify({label:gs(`experiment`,e,i.current)}))},[me,e,r.notify,i.current]);let _e=(0,Y.useCallback)(e=>Ei({tab:`runs`,id:e}),[]),ve=ue?K().evalsDataset(ue)?.id??null:null,ye=ei(ve),be=ye?.doc?.body.cases,xe=(0,Y.useMemo)(()=>(be??[]).map(e=>({id:e.id??``,item:K().caseItem(e)})),[be]),Se=ye?.doc?.body.cases,Ce=(0,Y.useMemo)(()=>[...new Set((Se??[]).flatMap(e=>(e.metrics??[]).filter(e=>!e.not&&e.weight!==0&&(e.type===`contains-all`||e.type===`contains-any`)).flatMap(e=>K().metricLines(e.values))))],[Se]),we=(0,Y.useRef)(u.list);we.current=u.list;let Te=(0,Y.useRef)(d.list);Te.current=d.list;let Ee=(0,Y.useRef)(d.default);Ee.current=d.default;let De=ce.length?null:y?.[0]??null,Oe=y!==null||b!==null,ke=x!==null||S!==null,je=w?.text??``;(0,Y.useEffect)(()=>{if(ce.length||!Oe||!ke)return;let e=rh(we.current,p.current.split,De?{id:De.id,name:De.name}:void 0,je);l({list:[e],at:e.id})},[ce.length,l,Oe,ke,De,je]);let Me=()=>{let e=T.list().list?.[0];return e?{id:e.id,name:e.name}:void 0},Ne=(0,Y.useRef)(c);Ne.current=c;let Pe=(0,Y.useRef)(G);Pe.current=G;let Fe=(0,Y.useRef)(ue);Fe.current=ue;let Ie=(0,Y.useRef)(null),Le=(0,Y.useRef)(null),Re=(0,Y.useCallback)(()=>{Le.current&&clearTimeout(Le.current);let e=Pe.current?.id,n=Fe.current;e&&n&&(Ie.current={id:e,doc:n},Le.current=setTimeout(()=>{let e=Ie.current;e&&qr(t,`pipeline`,e.id,e.doc,`idle`)},Ur))},[t]);(0,Y.useEffect)(()=>()=>{Le.current&&clearTimeout(Le.current)},[]);let ze=(0,Y.useCallback)(e=>{let t=Ne.current,n=Pe.current.id,r={...t,list:t.list.filter(e=>e.work?.version===20).map(t=>t.id===n?{...t,work:e}:t)};Ne.current=r,l(r),Re(),F(null)},[l,Re]),Be=u.list.find(e=>K().listedConnection(e))?.id??``;(0,Y.useEffect)(()=>{let e=Fe.current,t=Pe.current?.id;if(!e||!t||!Be)return;let n=cn(e,bn(we.current,p.current.split));if(n===e)return;let r=Ne.current,i={...r,list:r.list.map(e=>e.id===t?{...e,work:n}:e)};Ne.current=i,l(i)},[Be,de,l]);let Ve=(0,Y.useCallback)((e,t,n)=>{let r=Ne.current,i=r.list.find(t=>t.id===e);if(!n?.length||!i?.work)return;let a=he(i.work,t,n);if(a===i.work)return;let o={...r,list:r.list.map(t=>t.id===e?{...t,work:a}:t)};Ne.current=o,l(o)},[l]);(0,Y.useEffect)(()=>{let e=Ie.current;e&&e.id!==de&&(qr(t,`pipeline`,e.id,e.doc,`idle`),Ie.current=null,Le.current&&=(clearTimeout(Le.current),null))},[de,t]);let He=ue?K().contentOf(ue):null,Ue=He?.type===`source`?He.ref.id:null;(0,Y.useEffect)(()=>{if(Ue===null){j(void 0);return}let e=!0;return n.source(Ue).then(t=>{e&&j(t.files.map(e=>e.name))}).catch(()=>{}),()=>{e=!1}},[Ue,n]);let We=(ue?K().itemContents(ue).slice(1):[]).flatMap(({content:e})=>e.type===`source`&&e.ref.id!==Ue?[e.ref.id]:[]),Ge=[...new Set(We)].join(`
|
|
11
|
-
`);(0,Y.useEffect)(()=>{let e=Ge?Ge.split(`
|
|
12
|
-
`):[],t=!0;return Promise.all(e.map(e=>n.source(e).then(t=>[e,t.files.map(e=>e.name)]).catch(()=>null))).then(e=>{t&&N(Object.fromEntries(e.filter(e=>e!==null)))}),()=>{t=!1}},[Ge,n]);let Ke=(0,Y.useCallback)(e=>{let t=_.find(t=>t.id===e);return t?{name:t.name,type:t.type,files:e===Ue&&A?A:M[e]??[]}:null},[_,Ue,A,M]),qe=Ks(),Je=(0,Y.useMemo)(()=>{if(!ue)return[];if(!u.list.length&&K().targetsOf(ue).length)return[`add a connection first`];let e={profiles:f.resolve,connections:f.connection,sources:Ke,grader:qe,...y?{groups:y}:{}};return K().validatePipeline(ue,e)},[ue,u.list,f,Ke,y,qe]),Ye=de?g.pipeline?.[de]??[]:[],Ze=(0,Y.useCallback)(async()=>{let e=Fe.current,r=Pe.current.id;qr(t,`pipeline`,r,e,`run`);let i=K().resolvePipeline(e,{profiles:f.resolve,grader:qe,groups:D(),...Ue!==null&&A!==void 0?{files:A}:{},filesOf:e=>e===Ue?A:M[e],comment:fe});R(!0),F(null),ee(null);try{let t=await n.submit(i);Ve(r,e,t.prompts),V.current=t.run.id,B(t.run),o(t.run.id,{at:t.run.submittedAt,from:`runs`})}catch(e){F(e instanceof Error?e.message:String(e));let t=e instanceof Ln&&e.status===403&&e.body?.unshared;ee(Array.isArray(t)?t:null),V.current=null,B(null)}finally{R(!1)}},[n,fe,Ue,o,f,A,M,t,qe,D,Ve]),Qe=(0,Y.useCallback)(async()=>{let e=L.current;if(e?.length)try{for(let t of e)await n.shareConnection(t.kind,t.id);ee(null),F(null);let t=e.map(e=>e.name).join(`, `);r.offer({label:`Shared ${t} with this workspace`,restore:()=>void(async()=>{for(let t of e)try{await n.shareConnection(t.kind,t.id,!0)}catch{}})()})}catch(e){F(e instanceof Error?e.message:String(e))}},[n,r]),[$e,et]=(0,Y.useState)(null),tt=(0,Y.useCallback)(async()=>{let e=V.current;if(e){et(e);try{B((await n.cancel(e)).run)}catch{et(null)}}},[n]);(0,Y.useEffect)(()=>{z&&La(z.status)&&et(null)},[z]);let nt=(0,Y.useCallback)(async()=>{try{let e=(await n.queue({limit:1})).runs[0];if(!e)return;let t=(e.results??[]).filter(e=>(e.scenarios??[]).some(e=>Object.values(e?.scores??{}).some(K().failedScore))).map(e=>e.name??`an item`);t.length&&await navigator.clipboard?.writeText(t.join(`
|
|
13
|
-
`))}catch{}},[n]),rt=(0,Y.useCallback)(async e=>{let r=Fe.current,i=Pe.current.id;qr(t,`pipeline`,i,r,`run`);let a=K().itemContents(r)[0]?.step,o=r.steps?{...r,steps:r.steps.filter(e=>e.type!==`getItems`||e.id===a)}:r,s=K().resolvePipeline(o,{profiles:f.resolve,grader:qe,groups:D(),files:[e],comment:fe}),c=await n.submit(s);Ve(i,r,c.prompts);let l=c.run;for(;l.status===`queued`||l.status===`running`;)await new Promise(e=>setTimeout(e,500)),l=await n.run(c.run.id);return((l.results??[]).find(t=>t.name===e)?.scenarios??[]).map(e=>e.res??null)},[n,fe,f,t,qe,D,Ve]),it=(0,Y.useCallback)(e=>{ve&&s(e,{dataset:ve,files:A??[],run:rt})},[s,A,rt,ve]),at=(0,Y.useCallback)(()=>{let e={text:fe,pipeline:de};pe(``),r.offer({label:`Cleared the comment`,restore:()=>h(e)})},[fe,pe,h,de,r]),ot=(0,Y.useCallback)(()=>{let e=K().pipelineToYaml(Fe.current,{profiles:we.current});ds(new Blob([e],{type:`text/yaml`}),us(`experiment`,Fe.current.name||`experiment`,20,`yml`))},[]),ct=(0,Y.useCallback)(async e=>{let t=Fe.current,r={};for(let e of K().evalsOf(t)){let t=K().casesRef(e)?.id??null;if(t&&!r[t]){let e=await n.dataset(t);r[t]={name:e.name,body:e.body}}}let i=K().exportBundle(t,{connections:we.current,connectionProfiles:Te.current,profileDefault:Ee.current??null,grader:qe,datasets:r});if(`error`in i)throw Error(i.error);ds(await n.bundle({files:i.files,source:Ue,items:e}),us(`ci`,t.name||`experiment`,20,`zip`))},[n,Ue,qe]),lt=(0,Y.useCallback)(async e=>{let t=await n.archivedRules().then(e=>e.rules,()=>({})),r=K().yamlToPipeline(e,{profiles:we.current,connectionProfiles:Te.current,profileType:e=>p.current.connection(e)?.type,connectionSplit:p.current.split,sources:_,groups:E,rulesFor:e=>t[e]??null});if(`error`in r){ie(r);return}let{doc:i,missing:a}=r,o={id:ih(),name:i.name||`Imported`,work:i},s={...Ne.current,at:o.id,list:[...ce,o]};Ne.current=s,l(s),_e(o.id),ie({doc:i,missing:a})},[n,ce,l,_,E,_e]);if(!G||!ue)return(0,$.jsx)(`div`,{className:`runs`,children:(0,$.jsx)(Bt,{label:`Experiments`})});let ut=e=>{let n=Pe.current.id;qr(t,`pipeline`,n,Fe.current,`restore`),ze(e.doc),re(!1)};return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(dp,{list:ce,at:de,canRestore:Ye.length>0,used:Object.fromEntries(ce.map(e=>[e.id,g.pipeline?.[e.id]?.[0]?.at??null])),onSelect:e=>{l({...Ne.current,at:e}),_e(e)},onNew:()=>{let e=nh(ce.map(e=>e.name));l({...Ne.current,list:[...ce,e],at:e.id}),_e(e.id)},onClone:()=>{let e={id:ih(),name:`${G.name} copy`,work:structuredClone(ue)};l({...Ne.current,list:[...ce,e],at:e.id}),_e(e.id)},link:a({tab:`runs`,id:de}),onCopyMove:()=>W(!0),onRename:e=>{let t=e.trim(),n=Ne.current,r=Pe.current.id,i={...n,list:n.list.filter(e=>e.work?.version===20).map(e=>e.id===r?{...e,name:t,work:{...e.work,name:t}}:e)};Ne.current=i,l(i),Re()},onDelete:()=>{let t=Ne.current,n=de,i=ce.filter(e=>e.id!==n),a=i.length?i:[rh(u.list,f.split,Me(),je)],o=a.some(e=>e.id===t.at)?t.at:a[0].id;l({...Ne.current,list:a,at:o}),e&&Di({tab:`runs`,id:null}),r.offer({label:`Deleted ${G.name||`the experiment`}`,restore:()=>{Ne.current=t,l(t),_e(n)}})},onRestore:()=>re(!0),onExport:ot,onExportCi:()=>ae(!0),onImport:()=>se.current?.click()}),(0,$.jsx)(hp,{problems:Je,error:P,run:z,submitting:te,stopping:!!z&&$e===z.id,onRun:()=>void Ze(),onStop:()=>void tt(),onOpen:()=>{z&&o(z.id,{at:z.submittedAt,from:`runs`})},...I?.length?{onShare:()=>void Qe()}:{}}),(0,$.jsxs)(`div`,{className:`runs`,onKeyDown:ah,children:[(0,$.jsx)(`input`,{ref:se,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(lt).catch(()=>ie({error:`the file could not be read`}))}}),(0,$.jsx)($m,{pipeline:ue,edit:ze,comment:fe,setComment:pe,onClear:at,onCopyFailures:()=>void nt()}),(0,$.jsx)(Ym,{pipeline:ue,connections:u.list,connectionProfiles:d.list,resolve:f.resolve,sources:_,joined:(e,t)=>(Ue===null||A!==void 0)&&K().contentJoined(ue,e,t,{sources:Ke}),targetCap:O,protectedTerms:Ce,edit:ze,offer:r.offer}),(0,$.jsx)(Ep,{contentFiles:A,pipeline:ue,edit:ze,offer:r.offer,children:(0,$.jsx)(Qm,{pipeline:ue,sourceFiles:A,graded:xe,openCaseEditor:it})}),oe&&G&&(0,$.jsx)(Ns,{subject:{kind:`pipeline`,id:G.id,name:G.name},scan:{pipelines:ce,groups:y??[],prompts:(x??[]).map(e=>({id:e.id,name:ti(e)})),filterSets:C??[],sources:_},undo:r,onClose:()=>W(!1),onChanged:()=>void t.refresh()}),(0,$.jsx)(Qt,{undo:r.undo,notice:r.notice,onUndo:r.undoNow,onDismiss:r.dismiss}),ne&&(0,$.jsx)(st,{noun:`experiment`,versions:Ye,at:e=>e.at,reason:e=>e.reason,onRestore:ut,onClose:()=>re(!1)}),U&&(0,$.jsx)(mp,{hasItems:Ue!==null,onExport:ct,onClose:()=>ae(!1)}),H&&(`doc`in H?(0,$.jsx)(Ae,{title:`Imported ${H.doc.name||`Untitled`}`,onClose:()=>ie(null),dismiss:`Close`,children:H.missing.length>0?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`p`,{className:`note`,children:`Missing in this workspace:`}),(0,$.jsx)(`ul`,{className:`import-missing`,children:H.missing.map(e=>{let t=e.replace(/ not found$/,``),n=vp(e);return(0,$.jsx)(`li`,{children:n?(0,$.jsx)(`a`,{href:n,onClick:()=>ie(null),children:t}):t},e)})})]}):(0,$.jsx)(`p`,{className:`note`,children:`Ready to run.`})}):(0,$.jsx)(Ae,{title:`Nothing imported`,onClose:()=>ie(null),dismiss:`Close`,children:(0,$.jsx)(`p`,{className:`note`,children:H.error})}))]})]})}var nh=e=>{let t=We(e),n=K().blankPipeline({name:t});return{id:ih(),name:t,work:K().withJobs(n,K().jobsOf(n).map(e=>({...e,steps:[]})))}},rh=(e,t,n,r)=>({id:ih(),name:We([]),work:Ot(bn(e,t),n,r)}),ih=()=>`w${Date.now().toString(36)}${Math.random().toString(36).slice(2,8)}`;function ah(e){if(e.key!==`Enter`||e.defaultPrevented||e.nativeEvent.isComposing)return;let t=e.target;!t.closest(`.modal`)&&Ie(t)&&(e.preventDefault(),t.blur())}var oh=3e3,sh=10,ch=10,lh=20,uh=e=>Uo.some(t=>t.value===e),dh={pass:`% passed`,score:`score`,time:`avg time`},fh=(e,t)=>e===`time`?ba(t):`${Math.round(t)}%`,ph=e=>e==null?`none`:e?`pass`:`fail`,mh=e=>e==null?`not graded`:e?`passed`:`failed`,hh=e=>e.length>sh?`${e.slice(0,sh).join(`; `)}; and ${e.length-sh} more`:e.join(`; `);function gh({pipeline:e}){let t=X(),{openRun:n}=$i(),[r]=Z(`promptlab.workflows`),i=(0,Y.useMemo)(()=>Jo(r.list.filter(e=>e.work?.version===20)),[r.list]),[a,o]=(0,Y.useState)(null),s=e||a||``,c=i.find(e=>e.id===s)??i.find(e=>e.doc===s),l=c?.id??s,u=c?.doc??s,d=c?.only??null,[f,p]=(0,Y.useState)(Wo[0]),[m,h]=(0,Y.useState)(`pass`),[g,_]=(0,Y.useState)(!1),[v,y]=(0,Y.useState)(null),[b,x]=(0,Y.useState)(null),[S,C]=(0,Y.useState)(0),w=(0,Y.useRef)(v);w.current=v,(0,Y.useEffect)(()=>{if(e)return;let n=!0;return t.queue({limit:1}).then(e=>{let t=e.runs[0],r=i.filter(e=>e.doc===t?.snapshot?.id).length>1,a=t?Yo(i,t)?.id??(r?void 0:t.snapshot?.id):void 0;n&&o(a||i[0]?.id||``)},()=>{n&&o(i[0]?.id||``)}),()=>{n=!1}},[t,e,i]),(0,Y.useEffect)(()=>{if(!u)return;let e=!0,n;y(null),x(null);let r=async()=>{try{let i=await t.queue({pipeline:u,limit:Math.min(100,f+10)});if(!e)return;y(d?i.runs.filter(e=>e.snapshot?.name===d):i.runs),x(null),i.runs.some(e=>e.status===`queued`||e.status===`running`)&&(n=setTimeout(()=>void r(),oh))}catch{e&&!w.current&&x(`Runs not loaded: server unreachable.`)}};return r(),()=>{e=!1,n&&clearTimeout(n)}},[t,u,d,f,S]);let T=(0,Y.useMemo)(()=>v?Zo(v,f):null,[v,f]),E=(0,Y.useMemo)(()=>T?$o(T.targets,m):[],[T,m]),D=(0,Y.useMemo)(()=>ns(E,m),[E,m]),O=(0,Y.useMemo)(()=>{if(!T)return[];let e=new Set(E.map(e=>e.id));return[...E.map(e=>({id:e.id,name:e.name})),...T.rows.filter(t=>!e.has(t.id))]},[T,E]),k=Ti({tab:`runs`,id:c?.saved??null}),A=(e,t)=>({href:Ti({tab:`results`,id:e.id}),onOpen:()=>n(e.id,{at:e.at,from:`history`,...t==null?{}:{item:t}})}),j=(e,t)=>{let n=A(e);return(0,$.jsx)(`a`,{className:`ov-run`,href:n.href,title:da(e.at),onClick:e=>{e.preventDefault(),n.onOpen()},children:t?`${dt(e.at)}, ${jt(e.at)}`:Be(e.at)})},M=!T&&(0,$.jsx)(Bt,{label:`Runs`,error:b,onRetry:()=>{x(null),C(e=>e+1)}});return(0,$.jsxs)(`div`,{className:`history-overview`,children:[(0,$.jsxs)(`div`,{className:`ov-controls`,children:[(0,$.jsx)(`span`,{id:`ov-pipeline`,className:`sr-only`,children:`Experiment`}),(0,$.jsx)(N,{kind:`Experiment`,labelledBy:`ov-pipeline`,value:l,placeholder:`(No experiment)`,options:i.map(e=>({id:e.id,label:e.label})),onChange:e=>{location.hash=Ti({tab:`history`,section:`overview`,pipeline:e})}}),(0,$.jsx)(`select`,{"aria-label":`Runs shown`,value:f,onChange:e=>p(Number(e.target.value)),children:Wo.map(e=>(0,$.jsxs)(`option`,{value:e,children:[`Last `,e,` runs`]},e))})]}),(0,$.jsxs)(`div`,{className:`ov-sheet`,children:[(0,$.jsxs)(`section`,{className:`ov-frame ov-latest`,"aria-labelledby":`ov-latest`,children:[(0,$.jsxs)(`h2`,{className:`ov-rebate`,id:`ov-latest`,children:[`Latest run`,T?.latest&&(0,$.jsxs)(`span`,{className:`ov-rebate-end`,children:[j(T.latest),`, `,fa(T.latest.items.length,`item`)]})]}),(0,$.jsx)(`div`,{className:`ov-body`,children:M||(T.latest?(0,$.jsx)(_h,{run:T.latest,previous:T.previous,targets:E,metric:m,onMetric:h,pipelineHref:k,runAt:A(T.latest),cellAt:e=>A(T.latest,e)}):(0,$.jsxs)(`div`,{className:`ov-empty`,children:[(0,$.jsx)(`span`,{className:`unset`,children:`(No runs)`}),(0,$.jsx)(`a`,{className:`ghost button`,href:k,children:`Open in Experiments`})]}))})]}),T?.latest&&T.previous&&(0,$.jsx)(vh,{ov:T,prev:T.previous,runLink:j,cellAt:e=>A(T.latest,e)}),T?.latest&&T.previous&&(0,$.jsxs)(`section`,{className:`ov-frame ov-time`,"aria-labelledby":`ov-time`,children:[(0,$.jsx)(`h2`,{className:`ov-rebate`,id:`ov-time`,children:`Previous performance`}),(0,$.jsxs)(`div`,{className:`ov-body`,children:[(0,$.jsx)(`div`,{className:`ov-time-head`,children:(0,$.jsx)(`select`,{"aria-label":`Previous performance shows`,value:m,onChange:e=>{uh(e.target.value)&&h(e.target.value)},children:Uo.map(e=>(0,$.jsx)(`option`,{value:e.value,children:e.label},e.value))})}),(0,$.jsx)(bh,{ov:T,rows:O,metric:m,best:D})]})]}),T?.latest&&T.previous&&(0,$.jsxs)(`section`,{className:`ov-frame ov-items`,"aria-labelledby":`ov-items`,children:[(0,$.jsx)(`h2`,{className:`ov-rebate`,id:`ov-items`,children:`Items`}),(0,$.jsx)(`div`,{className:`ov-body`,children:(0,$.jsx)(xh,{ov:T,rows:O,allPass:g,onAllPass:_})})]})]})]})}function _h({run:e,previous:t,targets:n,metric:r,onMetric:i,pipelineHref:a,runAt:o,cellAt:s}){let c=e=>[e.connection,e.model].filter(Boolean).join(`, `),l=new Set(n.map(c)).size>1,u=e=>{let n=t?.targets.get(e.id)?.version;return!!n&&!!e.version&&n!==e.version},d=n[0]?.evals??[],f=e=>r===e?`num ov-on`:`num`,p=[{key:`name`,header:`Variant`,role:`title`,sort:null,cell:e=>(0,$.jsxs)(`div`,{className:`ov-target`,children:[(0,$.jsx)(`a`,{href:a,title:e.name,children:e.name}),u(e)&&(0,$.jsxs)(`span`,{className:`note`,title:e.prompt,children:[t.targets.get(e.id).version,` → `,e.version]}),l&&c(e)&&(0,$.jsx)(`span`,{className:`note`,children:c(e)})]})},{key:`items`,header:(0,$.jsx)(`span`,{className:`sr-only`,children:`Items`}),label:``,className:`ov-strip`,sort:null,cell:t=>{let n=e.items.filter(e=>t.verdicts.get(e.key)===!1).map(e=>e.name);return(0,$.jsx)(It,{className:`fit`,label:`${t.name}: ${t.passed} of ${t.cases} passed${n.length?`; failed: ${hh(n)}`:``}`,...o,cells:e.items.map(e=>{let n=t.verdicts.get(e.key)??null;return{state:ph(n),title:`${e.name}, ${mh(n)}`,...s(e.x)}})})}},{key:`pass`,header:`Passed Items`,className:f(`pass`),sort:e=>e.pct,cell:e=>e.cases?(0,$.jsx)(ui,{n:e.passed,of:e.cases}):`—`},{key:`score`,header:`Score`,className:f(`score`),sort:e=>e.score,cell:e=>e.score==null?`—`:`${e.score}%`},...d.map((e,t)=>({key:`eval-${e.id}`,header:e.label,className:`num`,sort:null,cell:e=>{let n=e.evals[t];return n?n.mark==null?n.ran?(0,$.jsx)(`span`,{className:n.passed===n.ran?`ov-full`:void 0,title:`${n.passed} of ${n.ran}`,children:n.passed}):`—`:(0,$.jsx)(Xt,{pass:n.mark}):`—`}})),{key:`time`,header:`Avg Time`,className:f(`time`),sort:e=>e.avgTime,cell:e=>e.avgTime==null?`—`:ba(e.avgTime)}];return(0,$.jsx)(W,{className:`ov-table`,label:`Latest run`,cards:!0,rows:n,rowKey:e=>e.id,columns:p,sort:{key:r,dir:r===`time`?`asc`:`desc`},onSort:e=>{uh(e?.key)&&i(e.key)}})}function vh({ov:e,prev:t,runLink:n,cellAt:r}){let i=rs(e),[a,o]=(0,Y.useState)(new Set),s=(e,t,n)=>(0,$.jsxs)(`div`,{className:`ov-change`,children:[(0,$.jsxs)(`h3`,{children:[(0,$.jsx)(Xt,{pass:e,decorative:!0}),t,(0,$.jsx)(`span`,{className:`ov-count`,children:n.length})]}),n.length?(0,$.jsxs)(`ul`,{children:[(a.has(e)?n:n.slice(0,ch)).map(({item:e,target:t})=>{let n=r(e.x);return(0,$.jsx)(`li`,{children:(0,$.jsxs)(`a`,{href:n.href,onClick:e=>{e.preventDefault(),n.onOpen()},children:[(0,$.jsx)(`span`,{className:`ov-item`,title:e.name,children:e.name}),(0,$.jsx)(`span`,{className:`note`,children:t.name})]})},`${t.id} ${e.key}`)}),!a.has(e)&&n.length>ch&&(0,$.jsx)(`li`,{children:(0,$.jsxs)(`button`,{type:`button`,className:`ghost ov-more`,onClick:()=>o(new Set(a).add(e)),children:[n.length-ch,` more`]})})]}):(0,$.jsx)(`p`,{className:`unset`,children:`(None)`})]});return(0,$.jsxs)(`section`,{className:`ov-frame ov-changed`,"aria-labelledby":`ov-changed`,children:[(0,$.jsxs)(`h2`,{className:`ov-rebate`,id:`ov-changed`,children:[`Changed since `,n(t,!0)]}),(0,$.jsxs)(`div`,{className:`ov-body`,children:[s(!1,`New failures`,i.failing),s(!0,`New passing`,i.passing)]})]})}function yh({runs:e}){let{openRun:t}=$i(),n=e.length>12,r=Math.ceil(e.length/7),i=-1/0;return(0,$.jsx)(`div`,{className:n?`ov-heads crowded`:`ov-heads`,children:e.map((a,o)=>{let s=dt(a.at),c=(o===0||s!==dt(e[o-1].at))&&(!n||o-i>=r);c&&(i=o);let l=o===0||o===e.length-1;return(0,$.jsxs)(`a`,{className:l?`ov-head ov-end`:`ov-head`,href:Ti({tab:`results`,id:a.id}),title:`${Be(a.at)}, ${da(a.at)}`,onClick:e=>{e.preventDefault(),t(a.id,{at:a.at,from:`history`})},children:[(0,$.jsx)(`span`,{className:c?`ov-day`:`ov-day same`,children:s}),!n&&(0,$.jsx)(`span`,{className:`ov-clock`,children:jt(a.at)}),!a.finished&&va(a.status)]},a.id)})})}function bh({ov:e,rows:t,metric:n,best:r}){let i=as(e,n),a=[i,i/2,0].map(e=>fh(n,e)),o=t.map(t=>{let r=is(e,t.id,n);return{row:t,points:r,last:[...r].reverse().find(e=>e.value!=null)}});return(0,$.jsxs)(`div`,{className:`ov-grid ov-lines`,style:{"--n":e.runs.length},children:[(0,$.jsx)(`span`,{}),(0,$.jsx)(`span`,{}),(0,$.jsx)(yh,{runs:e.runs}),(0,$.jsx)(`span`,{}),o.map(({row:e,points:t,last:o})=>{let s=r.has(e.id),c=t.map(e=>`${dt(e.run.at)} ${e.value==null?e.gap===`unfinished`?e.run.status:e.gap===`absent`?`not in this run`:`not graded`:fh(n,e.value)}`);return(0,$.jsxs)(`div`,{className:`ov-line-row`,children:[(0,$.jsxs)(`span`,{className:`ov-line-name`,title:e.name,children:[(0,$.jsx)(`span`,{className:`ov-line-label`,children:e.name}),s&&(0,$.jsx)(Ye,{tone:`sage`,children:`Best`})]}),(0,$.jsx)(`span`,{className:`ov-axis`,"aria-hidden":`true`,children:a.map((e,t)=>(0,$.jsx)(`span`,{children:e},t))}),(0,$.jsx)(He,{label:`${e.name}${s?`, best`:``}, ${dh[n]} by run: ${c.join(`, `)}`,max:i,...r.size?{className:s?`best`:`quiet`}:{},points:t.map(t=>{let r=t.run.targets.get(e.id),i=n===`pass`&&r?.cases?` (${r.passed} of ${r.cases})`:``;return{value:t.value,break:t.version,title:`${Be(t.run.at)}: ${t.value==null?t.gap===`unfinished`?t.run.status:t.gap===`absent`?`not in this run`:`not graded`:`${fh(n,t.value)}${i}`}`}})}),(0,$.jsx)(`span`,{className:`ov-last`,title:o?Be(o.run.at):void 0,children:o?.value==null?`—`:fh(n,o.value)})]},e.id)})]})}function xh({ov:e,rows:t,allPass:n,onAllPass:r}){let i=(0,Y.useMemo)(()=>os(e,t.map(e=>e.id)),[e,t]),[a,o]=(0,Y.useState)(!1),s=i.filter(e=>e.failed>0),c=i.filter(e=>e.failed===0),l=[...a?s:s.slice(0,lh),...n?c:[]],u=[{key:`item`,header:`Item`,role:`title`,sort:e=>e.name,cell:e=>(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`span`,{className:`ov-item`,title:e.name,children:e.name}),e.source&&(0,$.jsx)(`span`,{className:`ov-source note`,children:e.source})]})},...t.map((t,n)=>({key:`t-${t.id}`,header:t.name,label:t.name,className:`ov-cell`,sort:e=>{let t=e.cells[n];return t?t.passed/(t.passed+t.failed):2},cell:r=>{let i=r.cells[n];return i?(0,$.jsxs)(`span`,{className:`ov-cell-in`,children:[(0,$.jsx)(It,{className:e.runs.length>25?`xs tight`:`xs`,label:`${r.name}, ${t.name}: passed ${i.passed} of ${i.passed+i.failed} runs`,cells:i.cells.map((t,n)=>({state:ph(t),title:`${Be(e.runs[n].at)}, ${t==null?`not in this run`:mh(t)}`}))}),(0,$.jsx)(`span`,{className:i.failed?`ov-n`:`ov-n ov-full`,children:(0,$.jsx)(ui,{n:i.passed,of:i.passed+i.failed})})]}):(0,$.jsx)(`span`,{className:`note`,children:`—`})}}))],d=n?nt:ce;return(0,$.jsxs)($.Fragment,{children:[l.length>0&&(0,$.jsx)(`div`,{className:`scrollx`,children:(0,$.jsx)(W,{className:`ov-matrix`,label:`Items`,cards:!0,rows:l,rowKey:e=>e.key,columns:u,defaultSort:null})}),(0,$.jsxs)(`div`,{className:`ov-items-foot`,children:[!a&&s.length>lh&&(0,$.jsxs)(`button`,{type:`button`,className:`ghost ov-more`,onClick:()=>o(!0),children:[s.length-lh,` more`]}),c.length>0&&(0,$.jsxs)(`button`,{type:`button`,className:`ghost ov-more`,"aria-expanded":n,onClick:()=>r(!n),children:[c.length,` passed every run`,(0,$.jsx)(d,{size:14,"aria-hidden":`true`})]})]})]})}var Sh=e=>!!e.id&&La(e.status),Ch=e=>`Queued ${da(e.submittedAt)}`;async function wh(e,t){try{(await e.run(t)).status===`queued`&&await e.cancel(t)}catch{}}var Th=[``,`queued`,`running`,`done`,`cancelled`,`failed`,`incomplete`,`interrupted`],Eh=e=>e.numeric?`hnum num`:void 0,Dh=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)),Oh=(e,t)=>{let n=new Set(e);return n.delete(t),n},kh=(e,t,n=t)=>!t||e.some(e=>e.value===t)?e:[...e,{value:t,label:n}],Ah=e=>e&&{before:e.submittedAt,beforeId:e.id},jh=[{id:`overview`,label:`Overview`,icon:Yn},{id:`runs`,label:`All runs`,icon:pr}];function Mh({section:e=`overview`,pipeline:t=``,content:n=``}){return(0,$.jsxs)(`div`,{className:`history`,children:[(0,$.jsx)(en,{label:`History`,level:`section`,className:`history-sections`,current:e,items:jh.map(({id:r,label:i,icon:a})=>({key:r,label:i,icon:a,href:Ti({tab:`history`,section:r,...t&&(r===e||e===`overview`)?{pipeline:t}:{},...r===`runs`&&n?{content:n}:{}})}))}),e===`overview`?(0,$.jsx)(gh,{pipeline:t}):(0,$.jsx)(Nh,{pipeline:t,content:n})]})}function Nh({pipeline:e,content:t}){let n=X(),r=Dt(),{openRun:i}=$i(),a=Os(),[o,s]=Z(`promptlab.history.columns`),[c,l]=(0,Y.useState)([]),[u,d]=(0,Y.useState)(!1),[f,p]=(0,Y.useState)(``),[m,h]=(0,Y.useState)(!1),[g,v]=(0,Y.useState)(null),[y,b]=(0,Y.useState)(null),[x,S]=(0,Y.useState)(null),[C,w]=(0,Y.useState)(null),[T,E]=(0,Y.useState)(!1),[D,O]=(0,Y.useState)(!1),[k,A]=(0,Y.useState)(!1),[j,N]=(0,Y.useState)(!1),[P,F]=(0,Y.useState)(null),[I,ee]=(0,Y.useState)(0),L=(0,Y.useRef)(j);L.current=j;let[te,R]=(0,Y.useState)(null),[ne,re]=(0,Y.useState)(0),[z,B]=(0,Y.useState)(new Set),V=(0,Y.useRef)(c);V.current=c;let[H]=Z(`promptlab.workflows`),[ie,U]=(0,Y.useState)(new Set);(0,Y.useEffect)(()=>{let e=!0;return n.sources().then(t=>{e&&U(new Set(t.sources.map(e=>e.id)))},()=>{}),()=>{e=!1}},[n]);let ae=(0,Y.useMemo)(()=>c.map(e=>{let t=ma(vo(e),e),n=t.pipelineId?H?.list.find(e=>e.id===t.pipelineId||e.work?.id===t.pipelineId):void 0;return{...t,...n?{pipelineHref:Ti({tab:`runs`,id:n.id})}:{},...t.sourceId&&ie.has(t.sourceId)?{contentHref:Ti({tab:`library`,section:`sources`,id:t.sourceId})}:{}}}),[c,H,ie]),se=(0,Y.useMemo)(()=>Ih(o),[o]),ce=(0,Y.useCallback)(async(e,t=!1)=>{try{return await n.queue({limit:25,...e,...t?{full:t}:{}})}catch{return null}},[n]),le=(0,Y.useCallback)(e=>{let t=new Set(e.runs.map(e=>e.id));l(n=>Dh([...n.filter(e=>!t.has(e.id)),...e.runs])),d(e.more)},[]);(0,Y.useEffect)(()=>{let e=!0,t,n=async()=>{let r=await ce();e&&(r?(le(r),N(!0),F(null)):L.current||F(`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)}},[ce,le,I,ne]);let G=(0,Y.useMemo)(()=>ae.filter(n=>(!f||(n.status||`done`)===f)&&(!e||n.setup===e||n.pipelineId===e)&&(!t||n.sourceId===t)),[ae,f,e,t]),ue=(e,t)=>{let n=new Map;for(let r of ae)e(r)&&!n.has(e(r))&&n.set(e(r),t(r)||e(r));return[{value:``,label:`All`},...[...n].map(([e,t])=>({value:e,label:t})).sort((e,t)=>e.label.localeCompare(t.label))]},fe=n=>{let r={pipeline:e,content:t,...n};Di({tab:`history`,section:`runs`,...r.pipeline?{pipeline:r.pipeline}:{},...r.content?{content:r.content}:{}})},pe=(e=>({Runs:ja.filter(t=>e.Runs.has(t.key)),"Run Scenarios":Ma.filter(t=>e[`Run Scenarios`].has(t.key))}))(se),me=(0,Y.useCallback)((e,t)=>{i(e,{at:t,from:`history`})},[i]),he=(0,Y.useCallback)(async e=>{B(t=>Oh(t,e));try{let t=await n.resume(e),r={runs:[t.run],more:V.current.length>25};le(r),me(e,t.run.submittedAt)}catch{}},[n,le,me]),ge=(0,Y.useCallback)(async e=>{w(null);try{let{run:t}=await n.rerunRun(e);le({runs:[t],more:V.current.length>25}),R(t.id),re(e=>e+1),r.offer({label:Ch(t),restore:()=>void wh(n,t.id).then(()=>re(e=>e+1))})}catch(e){w(`Not re-run: ${e instanceof Error?e.message:`server unreachable`}.`)}},[n,le,r]),_e=(0,Y.useCallback)(async e=>{B(t=>new Set(t).add(e));try{let{run:t}=await n.cancel(e);le({runs:[t],more:V.current.length>25})}catch{B(t=>Oh(t,e))}},[n,le]),ve=e=>{let t=e.run?V.current.find(t=>t.id===e.run):void 0,n=[];if(t&&!La(t.status)){let e=z.has(t.id)||Ra(t);n.push({label:e?`Stopping…`:`Stop`,icon:_,busy:e,onSelect:()=>void _e(t.id)})}return ka(e)&&n.push({label:`Resume`,icon:oe,onSelect:()=>void he(e.run)}),t&&Sh(t)&&n.push({label:`Re-run`,icon:Cr,onSelect:()=>void ge(t.id)}),e.run&&n.push(a({tab:`results`,id:e.run})),n},ye=(0,Y.useCallback)(async()=>{let e=V.current;A(!0);try{await n.clear()}catch{w(`Not cleared: server unreachable.`);return}finally{A(!1)}l([]),d(!1),r.offer({label:`Cleared ${fa(e.length,`run`)}`,restore:()=>{for(let t of e)n.submit(K().upgradePipeline(t.snapshot)).catch(()=>{})}})},[n,r]),be=(0,Y.useCallback)(async()=>{O(!0);let e=[],t=!1,n;for(let r=0;;r++){let i=await ce(n,!0);if(!i){t=!0;break}if(e=Dh([...e,...i.runs]),n=Ah(i.runs.at(-1)),!i.more||!n||r>100)break}if(t){let t=new Set(e.map(e=>e.id));e=Dh([...e,...V.current.filter(e=>!t.has(e.id))])}if(O(!1),!e.length){w(`Nothing to export yet.`);return}ds(new Blob([JSON.stringify({exported:new Date().toISOString(),runs:e},null,2)],{type:`application/json`}),us(`runs`,`all`,20,`json`)),w(t?`Exported ${fa(e.length,`run`)}. Server unreachable, so only those this browser had.`:`Exported ${fa(e.length,`run`)}.`)},[n,ce]),xe=[{label:`Select Columns`,icon:Qn,onSelect:()=>h(!0)},{label:`Export JSON`,icon:M,busy:D,onSelect:()=>void be()},{label:`Clear`,icon:Wt,destructive:!0,busy:k,disabled:!j,onSelect:()=>void ye()}],Se=(0,Y.useCallback)(e=>{let t=e.target.closest(`[data-openrun]`);if(t){e.stopPropagation();let n=t.closest(`tr[data-run]`),r=n?G[Number(n.getAttribute(`data-run`))]:null;r?.run&&me(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?G[Number(t.getAttribute(`data-run`))]:null;r?.run&&b({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?G[Number(t.getAttribute(`data-run`))]:null,i=n?.run?V.current.find(e=>e.id===n.run):null;i?.snapshot&&S({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?G[Number(t.getAttribute(`data-run`))]:null,r=i.getAttribute(`data-prof`);if(n&&r!=null){let e=n.scenarios[Number(r)];e&&v({name:wa(e),connection:e.connection,settings:{...e.setupProfile?.settings||{}},images:ua(n)===!0})}}},[G,me]),Ce=(0,Y.useCallback)((e,t)=>{l(n=>n.map(n=>n.id===e?{...n,snapshot:{...n.snapshot,comment:t}}:n)),b(null)},[]),we=(e,t)=>e.image&&ua(t)!==!0?(0,$.jsx)(`span`,{className:`note`,children:`—`}):e.get(t),Te=(e,t,n,r)=>n?e.image&&ua(t)!==!0?(0,$.jsx)(`span`,{className:`note`,children:`—`}):e.get(t,n,r):`—`;return(0,$.jsxs)(`div`,{className:`history-runs`,children:[(0,$.jsxs)(Tt,{id:`history`,title:`History`,actions:xe,children:[!j&&(0,$.jsx)(Bt,{label:`Runs`,error:P,onRetry:()=>{F(null),ee(e=>e+1)}}),j&&(ae.length>0||f||e||t)&&(0,$.jsx)(xt,{noun:`runs`,lead:G.length>0&&(0,$.jsx)(`p`,{className:`note hstatus`,role:`status`,children:fa(G.length,`run`)}),filters:[{key:`show`,label:`Show`,value:f,onChange:p,options:Th.map(e=>({value:e,label:e?ga(e):`All`}))},{key:`pipeline`,label:`Experiment`,value:e,onChange:e=>fe({pipeline:e}),options:kh(ue(e=>e.setup,e=>e.setup),e,ae.find(t=>t.pipelineId===e)?.setup)},{key:`content`,label:`Content`,value:t,onChange:e=>fe({content:e}),options:kh(ue(e=>e.sourceId,e=>e.content),t)}]}),C&&(0,$.jsx)(`p`,{className:`note hstatus`,role:`status`,children:C}),j?G.length?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`div`,{className:`scrollx`,onClick:Se,children:(0,$.jsx)(W,{className:`htable`,label:`Runs`,busy:T,defaultSort:null,rows:G,rowKey:e=>e.at,rowProps:(e,t)=>({className:`runrow${e.run&&e.run===te?` fresh`:``}`,"data-run":t}),columns:[...pe.Runs.map(e=>({key:e.key,header:e.label,className:[Eh(e),e.key===`runid`&&`hrunid`].filter(Boolean).join(` `)||void 0,sort:e.sort??(t=>de(e.get(t))),cell:t=>(0,$.jsx)(`span`,{className:`hval`,children:we(e,t)})})),{key:`actions`,header:(0,$.jsx)(`span`,{className:`sr-only`,children:`Actions`}),label:``,className:`hactions`,sort:null,cell:e=>{let t=ve(e);return t.length?(0,$.jsx)($e,{actions:t,label:`${da(e.at)} actions`}):null}}],expand:{label:(e,t)=>`${t?`Collapse`:`Expand`} run ${da(e.at)}`,detailProps:(e,t)=>({className:`scenrows`,"data-run":t,"data-scen":e.at}),detail:e=>(0,$.jsx)(W,{className:`scen-table`,label:`Variants`,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:pe[`Run Scenarios`].map(t=>({key:t.key,header:t.label,className:Eh(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 de(i)},cell:({sc:n,n:r})=>(0,$.jsx)(`span`,{className:`hval`,children:Te(t,e,n,r)})}))})}})}),u&&(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,disabled:T,"aria-busy":T||void 0,onClick:async()=>{E(!0);let e=await ce(Ah(V.current.at(-1)));if(E(!1),!e){w(`Server unreachable.`);return}le(e)},children:[T&&(0,$.jsx)(kt,{}),`Older`]})]}):C?(0,$.jsx)(`p`,{className:`note`,children:C}):(0,$.jsx)(`p`,{className:`unset`,children:ae.length?`(No matches)`:`(No runs)`}):null]}),(0,$.jsx)(Qt,{undo:r.undo,onUndo:r.undoNow,onDismiss:r.dismiss}),m&&(0,$.jsx)(Rh,{cols:se,onSave:e=>{s(Lh(e)),h(!1)},onClose:()=>h(!1)}),x&&(0,$.jsx)($t,{run:x.run,i:x.i,onClose:()=>S(null)}),g&&(0,$.jsx)(Ae,{title:g.name||`Settings`,onClose:()=>v(null),dismiss:`Close`,children:(0,$.jsx)(Ph,{connection:g.connection,settings:g.settings,images:g.images})}),y&&(0,$.jsx)(Fh,{runId:y.id,comment:y.comment,onSaved:e=>Ce(y.id,e),onClose:()=>b(null)})]})}function Ph({connection:e,settings:t,images:n}){let r=e=>e.filter(e=>String(t[e]??``)!==``).map(e=>({key:e,label:sa(e),control:{type:`static`,text:ca(e,t[e])}})),i=[...e?[{key:`connection`,label:`Connection`,control:{type:`static`,text:e}}]:[],...r(ra)],a=n?r(ia):[],o=r(na.filter(e=>!ra.includes(e)&&!ia.includes(e)));return!i.length&&!o.length&&!a.length?(0,$.jsx)(`p`,{className:`note`,children:`(No settings)`}):(0,$.jsxs)(Ne,{children:[i.length>0&&(0,$.jsx)(w,{label:e?`Connection`:`Connection Profile`,rows:i}),o.length>0&&(0,$.jsx)(Tt,{id:`profile-settings`,title:`Settings`,level:3,children:(0,$.jsx)(w,{label:`Settings`,rows:o})}),a.length>0&&(0,$.jsx)(Tt,{id:`profile-vision`,title:`Vision Capabilities`,level:3,children:(0,$.jsx)(w,{label:`Vision Capabilities`,rows:a})})]})}function Fh({runId:e,comment:t,onSaved:n,onClose:r}){let i=X(),[a,o]=(0,Y.useState)(t),[s,c]=(0,Y.useState)(null),[l,u]=(0,Y.useState)(!1);return(0,$.jsx)(De,{title:`Run comment`,...s?{status:s,statusBad:!0}:{},sections:[{key:`comment`,rows:[{key:`comment`,label:`Comment`,control:{type:`text`,value:a,onChange:o,placeholder:`Add a note`}}]}],onSave:async()=>{u(!0),c(null);try{await i.setComment(e,a),n(a)}catch{c(`Not saved: the server is unreachable.`),u(!1)}},saving:l,focusRow:`comment`,onClose:r})}function Ih(e){let t=()=>({Runs:new Set(ja.filter(e=>e.on).map(e=>e.key)),"Run Scenarios":new Set(Ma.filter(e=>e.on).map(e=>e.key))});if(!e)return t();let n=t();for(let t of Ia){let r=t[0],i=e[r],a=new Set(t[1].map(e=>e.key)),o=e=>Pa[e]??e,s=new Set(i?.seen?.map(o)??[...a]),c=(i?.on??[]).map(o);for(let[e,t]of Object.entries(Fa))a.has(e)&&!s.has(e)&&s.has(t)&&(s.add(e),c.includes(t)&&c.push(e));let l=[...c.filter(e=>a.has(e)),...t[1].filter(e=>e.on&&!s.has(e.key)).map(e=>e.key)];l.length&&(n[r]=new Set(l))}return n}function Lh(e){return{Runs:{on:[...e.Runs],seen:ja.map(e=>e.key)},"Run Scenarios":{on:[...e[`Run Scenarios`]],seen:Ma.map(e=>e.key)}}}function Rh({cols:e,onSave:t,onClose:n}){let[r,i]=(0,Y.useState)(()=>({Runs:new Set(e.Runs),"Run Scenarios":new Set(e[`Run Scenarios`])})),[a,o]=(0,Y.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)(De,{title:`Select Columns`,dirty:c,refusal:a??void 0,onSave:()=>{if(!r.Runs.size){o(`The Runs group needs at least one column.`);return}t(r)},onClose:n,sections:[{key:`cols`,children:(0,$.jsx)(`div`,{className:`hcols`,children:Ia.map(([e,t])=>(0,$.jsxs)(`div`,{className:`groups`,children:[(0,$.jsx)(`h3`,{children:Na[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))})}]})}var zh=e=>!!e&&e.startsWith(`•held:`),Bh={"promptlab.workflows":{list:[],at:``},"promptlab.connections":{list:[],at:``},"promptlab.connectionProfiles":{list:[]},"promptlab.comment":null,"promptlab.versions":Vh(),"promptlab.history.columns":null,"promptlab.wizard":null,"promptlab.guidesDismissed":!1,"promptlab.tokens":null};function Vh(){return{pipeline:{},profile:{},dataset:{}}}var Hh=[`promptlab.workflows`,`promptlab.connections`,`promptlab.connectionProfiles`,`promptlab.versions`,`promptlab.tokens`],Uh=[`px`,`format`,`quality`],Wh=[{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`}],Gh={"image/jpeg":`JPEG`,"image/webp":`WebP`,"image/png":`PNG`},Kh=e=>Gh[e]??e.replace(`image/`,``).toUpperCase(),qh={px:`921600`,quality:`0.92`};function Jh(e,t,{inherit:n=!1}={}){let r=K().connectionImage({}).format,i=String(e.px??``),a=String(e.format??``),o=String(e.quality??``),s=Wh.some(e=>e.value===i)||!i?Wh:[...Wh,{value:i,label:K().budgetLabel(i)}],c={value:``,label:`(From connection)`},l=n?[c,...s]:s.map(e=>e.value===qh.px?{value:``,label:e.label}:e),u=n?[c,...K().IMAGE_FORMATS.map(e=>({value:e,label:Kh(e)}))]:K().IMAGE_FORMATS.map(e=>e===r?{value:``,label:Kh(e)}:{value:e,label:Kh(e)}),d=(a||r)===`image/png`,f=n?`(From connection)`:qh.quality,p={type:`text`,value:o,onChange:t(`quality`),...d||o?{}:{placeholder:f}};return[{key:`px`,label:`Resolution`,control:{type:`select`,value:!n&&i===qh.px?``:i,options:l,onChange:t(`px`)}},{key:`format`,label:`Format`,control:{type:`select`,value:!n&&a===r?``:a,options:u,onChange:t(`format`)}},{key:`quality`,label:n?`Image quality`:`Quality`,width:`quarter`,...d?{hint:`Unused for PNG`}:{},control:p}]}function Yh(e,t){return e===`px`?Wh.find(e=>e.value===t)?.label??K().budgetLabel(t):e===`format`?Kh(t):t}function Xh(e){let{px:t,format:n,quality:r}=K().connectionImage(e),i=n===`image/png`?``:` q${r??qh.quality}`;return`${K().budgetLabel(t??Number(qh.px))}, ${Kh(n)}${i}`}var Zh=e=>({kind:`pipeline`,id:e.id,name:e.name||`Untitled experiment`}),Qh=e=>({kind:`group`,id:e.id,name:e.name||`Untitled eval`});function $h(e,t,n,r){let i=new Set(r.filter(t=>t.connection?.id===e).map(e=>e.id)),a=[];for(let n of t)(K().profileIds(n.work).includes(e)||K().answerKeys(n.work).some(e=>{let t=K().answerOfKey(e);return!!t.profile&&i.has(t.profile)}))&&a.push(Zh(n));for(let t of n)t.grader?.id===e&&a.push(Qh(t));return a}function eg(e,t,n){let r=[];for(let n of t)K().answerKeys(n.work).some(t=>{let n=K().answerOfKey(t);return n.bare?!!e.fromProfile&&n.connection===e.fromProfile:n.profile===e.id})&&r.push(Zh(n));if(e.fromProfile)for(let t of n)t.grader?.id===e.fromProfile&&r.push(Qh(t));return r}function tg(e,t,n){let r=new Set(e.map(e=>e.fromProfile).filter(Boolean)),i=[];for(let e of t)K().answerKeys(e.work).some(e=>{let t=K().answerOfKey(e);return t.bare?!r.has(t.connection):t.profile===null})&&i.push(Zh(e));for(let e of n)e.grader?.id&&!r.has(e.grader.id)&&i.push(Qh(e));return i}function ng(e){if(!e.length)return`Not used`;let t=(t,n)=>{let r=e.filter(e=>e.kind===t);return r.length===0?null:r.length===1?r[0].name:`${r.length} ${n}`};return[t(`pipeline`,`experiments`),t(`group`,`evals`)].filter(Boolean).join(`, `)}function rg({uses:e}){return e.length?(0,$.jsx)(`span`,{className:`setup-used`,children:ng(e)}):(0,$.jsx)(`span`,{className:`unset`,children:`Not used`})}function ig({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:`unset`,children:`Not used`}):(0,$.jsx)(`ul`,{className:`setup-usedby-list`,children:e.map(e=>(0,$.jsxs)(`li`,{children:[(0,$.jsx)(`a`,{href:Ti(e.kind===`group`?{tab:`library`,section:`datasets`,id:e.id}:{tab:`runs`,id:e.id}),children:e.name}),(0,$.jsx)(`span`,{className:`setup-usedby-kind`,children:e.kind===`group`?`eval`:`experiment`})]},`${e.kind}-${e.id}`))})]})}var ag={tab:`setup`,section:`connections`,id:null},og=e=>({tab:`setup`,section:`connections`,id:e,kind:`profile`}),sg=`(Default)`,cg=e=>({tab:`setup`,section:`connections`,id:e}),lg=()=>{let e=Oi();e.tab===`setup`&&e.id&&Ei(ag)},ug=[`id`,`name`,`connection`,`fromProfile`];function dg({profileId:e,connections:t,workflows:n,groups:r,presets:i,offer:a}){let o=Os(),[s,c]=Z(`promptlab.connectionProfiles`),l=(0,Y.useRef)(s);l.current=s;let[u,d]=(0,Y.useState)(null),f=(0,Y.useRef)(u);f.current=u;let[p,m]=(0,Y.useState)(void 0),[h,g]=(0,Y.useState)(`all`),_=(0,Y.useCallback)(e=>{let t={...l.current,list:e};l.current=t,c(t)},[c]),v=(0,Y.useCallback)(e=>{let{default:t,...n}=l.current,r=Object.keys(e).length?{...n,default:e}:n;l.current=r,c(r)},[c]),y=(0,Y.useCallback)((e,t)=>{let n=e===`(Default)`?Tg(l.current.default):l.current.list.find(t=>t.id===e);n&&(d({p:structuredClone(n),was:n,start:n}),m(t))},[]),b=(0,Y.useCallback)((e,t)=>{y(e,t),Ei(og(e))},[y]),x=(0,Y.useCallback)(()=>{d(null),m(void 0)},[]),S=(0,Y.useCallback)(()=>{x(),lg()},[x]);(0,Y.useEffect)(()=>{if(!e){f.current?.was&&x();return}f.current?.p.id!==e&&(e===`(Default)`||l.current.list.some(t=>t.id===e)?y(e):Di(ag))},[e,y,x]);let C=(0,Y.useCallback)(()=>{let e={id:Dg(),name:`New profile`};d({p:e,was:null,start:e}),m(`name`)},[]),w=(0,Y.useCallback)(e=>{let t=f.current;if(!t)return;let n={...t,p:e,refused:!1};f.current=n,d(n)},[]),T=(0,Y.useCallback)(()=>{let e=f.current;if(e){if(e.p.id===`(Default)`){let{id:t,name:n,...r}=Eg(e.p);v(r)}else{if(!e.p.connection?.id){d({...e,refused:!0});return}let t=Eg(e.p),n=l.current.list;_(e.was?n.map(e=>e.id===t.id?t:e):[...n,t])}x(),lg()}},[_,v,x]),E=(0,Y.useCallback)(e=>{let t=l.current.list,n=t.filter(t=>e.includes(t.id)).map(e=>{let{fromProfile:t,...n}=structuredClone(e);return{...n,id:Dg(),name:`${e.name||`Profile`} copy`}});n.length&&_([...t,...n])},[_]),D=(0,Y.useCallback)(e=>{let t=l.current.list,n=t.filter(t=>e.includes(t.id));n.length&&(_(t.filter(t=>!e.includes(t.id))),x(),lg(),a({label:n.length===1?`Deleted ${n[0].name||`the profile`}`:`Removed ${n.length} profiles`,restore:()=>_(t)}))},[_,x,a]),O=(0,Y.useMemo)(()=>{let e=new Map;for(let t of s.list)e.set(t.id,eg(t,n,r));return e},[s.list,n,r]),k=(0,Y.useMemo)(()=>tg(s.list,n,r),[s.list,n,r]),A=(0,Y.useMemo)(()=>Tg(s.default),[s.default]),j=s.list.filter(e=>h===`all`||Sg(e,t)),M=e=>e.name||`Unnamed profile`,N=e=>[{label:`Edit…`,icon:J.edit,onSelect:()=>b(e.id)},{label:`Rename…`,icon:J.rename,onSelect:()=>b(e.id,`name`)},{label:`Clone`,icon:J.clone,onSelect:()=>E([e.id])},o(og(e.id)),{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>D([e.id])}],P=e=>e===A,F=[{key:`name`,header:`Profile`,role:`title`,sort:M,cell:e=>(0,$.jsx)(`button`,{type:`button`,className:`link-btn setup-open${P(e)?` unset`:``}`,onClick:()=>b(e.id),children:M(e)})},{key:`connection`,header:`Connection`,sort:e=>xg(e,t),cell:e=>{if(P(e))return(0,$.jsx)(`span`,{className:`unset`,children:`(Any)`});let n=e.connection?.id?t.find(t=>t.id===e.connection.id):void 0;return n?(0,$.jsx)(`button`,{type:`button`,className:`link-btn setup-open setup-conn`,onClick:()=>Ei(cg(n.id)),children:xg(e,t)}):(0,$.jsx)(`span`,{className:`setup-attention`,children:xg(e,t)})}},{key:`settings`,header:`Settings`,sort:e=>wg(e,t),cell:e=>{let n=wg(e,t);return n?(0,$.jsx)(`span`,{className:`setup-sets`,children:n}):(0,$.jsx)(`span`,{className:`unset`,children:`(No settings)`})}},{key:`used`,header:`Used by`,sort:e=>ng(O.get(e.id)??[]),cell:e=>(0,$.jsx)(rg,{uses:P(e)?k:O.get(e.id)??[]})},{key:`menu`,header:``,label:``,className:`setup-menu`,role:`menu`,sort:null,cell:e=>P(e)?null:(0,$.jsx)($e,{actions:N(e),label:`${M(e)} actions`})}],I=u;return(0,$.jsxs)(Tt,{id:`setup-connection-profiles`,title:`Connection Profiles`,children:[(0,$.jsx)(`div`,{className:`setup-intro`,children:(0,$.jsx)(`p`,{className:`note`,children:`Overrides the AI Connection settings`})}),s.list.length>0&&(0,$.jsx)(xt,{noun:`profiles`,filters:[],quick:{label:`Show`,value:h,onChange:e=>g(e),options:Cg}}),h!==`all`&&j.length===0?(0,$.jsx)(`p`,{className:`unset`,children:`(No matches)`}):(0,$.jsx)(W,{className:`setup-table`,cards:!0,label:`Connection Profiles`,rows:j,rowKey:e=>e.id,select:{noun:`profile`,label:M,actions:e=>[{label:`Clone`,icon:J.clone,onSelect:()=>E(e.map(e=>e.id))},{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>D(e.map(e=>e.id))}]},rowProps:e=>({className:P(e)?`setup-row setup-default`:`setup-row`,...P(e)?{"data-default":``}:{},onClick:t=>{t.target.closest(`button, a, .menu`)||b(e.id)}}),pinnedRow:h===`all`?A:void 0,columns:F}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:C,children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add profile`]}),I&&(0,$.jsx)(fg,{profile:I.p,connections:t,presets:i,uses:I.p.id===`(Default)`?k:O.get(I.p.id)??[],refused:!!I.refused,...p?{focus:p}:{},dirty:JSON.stringify(I.p)!==JSON.stringify(I.start),adding:!I.was,...I.was?{link:o(og(I.p.id))}:{},onEdit:w,onSave:T,onClose:S})]})}function fg({profile:e,connections:t,presets:n,uses:r,refused:i,focus:a,dirty:o,adding:s,link:c,onEdit:l,onSave:u,onClose:d}){let f=e.id===sg,p=f?null:pg(e,t),m=t=>n=>{let r={...e};for(let e of t)r[e]=n;l(r)},h=e=>m([e]),g=e.connection??null,[_]=Z(`promptlab.versions`),{options:v,recent:y}=Bs(t.filter(e=>K().listedConnection(e)||e.id===g?.id).map(e=>({id:e.id,name:e.name||`Unnamed connection`})),e=>_.profile?.[e]?.[0]?.at??null),b=[...v,...g&&!t.some(e=>e.id===g.id)?[{id:g.id,label:`${g.name||g.id} (missing)`,missing:!0}]:[]],x=[{key:`name`,label:`Name`,width:`full`,required:!0,control:{type:`text`,value:e.name,onChange:h(`name`)}},{key:`connection`,label:`Connection`,required:!0,hint:`Overrides the AI Connection settings`,...i?{error:`Choose a connection`}:{},control:{type:`picker`,kind:`Connection`,value:g?.id??``,options:b,recent:y,usedLabel:`Last changed`,placeholder:`(No connection)`,onChange:n=>{let r=t.find(e=>e.id===n);r&&l({...e,connection:{id:r.id,name:r.name}})}}}],S=gg(e,p).map(t=>bg(e,t,m(t.keys))),C=p?K().CONNECTION_TYPES[p.type]:void 0,w=!C||(C.answers??[`prompt`]).includes(`prompt`)&&!C.local,T=[...f?[]:[{key:`identity`,rows:x}],...S.length?[{key:`model`,title:`Model settings`,rows:S}]:[],...w?[{key:`image`,title:`Image settings`,rows:Jh(e,h,{inherit:!0})}]:[],{key:`use`,title:`Use`,rows:[],children:(0,$.jsx)(ig,{uses:r})}],E=p?n.filter(e=>(e.type??`llama.cpp`)===p.type):[],D=t=>{let n={...e};for(let e of C?.settings??[])e.key!==`temperature`&&e.scope!==`connection`&&delete n[e.key];l({...n,...t.opts})},O=[...c?[c]:[],...E.map(e=>({label:`Apply preset: ${e.label}${e.packName?` · ${e.packName}`:``}`,icon:J.preset,onSelect:()=>D(e)}))];return(0,$.jsx)(De,{title:f?sg:e.name||`Unnamed profile`,...a?{focusRow:a}:{},sections:T,...O.length?{menu:O}:{},dirty:o,adding:s,onSave:u,onClose:d})}function pg(e,t){let n=e.connection?.id??e.fromProfile;return n&&t.find(e=>e.id===n)||null}function mg(){let e=Object.values(K().CONNECTION_TYPES).filter(e=>e.picker!==!1&&!e.local&&(e.answers??[`prompt`]).includes(`prompt`)),t=new Map;for(let n of e)for(let e of n.settings){if(e.scope===`connection`)continue;let n=t.get(e.label)??{label:e.label,keys:[],setting:e,n:0};n.keys.includes(e.key)||n.keys.push(e.key),n.n+=1,t.set(e.label,n)}return[...t.values()].filter(t=>t.n*2>e.length).sort((e,t)=>t.n-e.n).map(({n:e,...t})=>t)}function hg(){let e=mg().map(e=>e.label);for(let t of Object.values(K().CONNECTION_TYPES))for(let n of t.settings)n.scope!==`connection`&&!e.includes(n.label)&&e.push(n.label);return e}function gg(e,t){let n;if(t){let r=K().mergeAnswer(t,e);n=(K().CONNECTION_TYPES[t.type]?.settings??[]).filter(e=>e.scope!==`connection`&&(!e.appliesTo||e.appliesTo(r))).map(e=>({label:e.label,keys:[e.key],setting:e}))}else n=mg();let r=new Set(n.flatMap(e=>e.keys));for(let[t,i]of Object.entries(e)){if(ug.includes(t)||Uh.includes(t)||r.has(t)||i==null||i===``)continue;let e=yg(t),a=e?.label??vg(t);n.push({label:a,keys:[t],setting:e??{key:t,label:a,control:`text`,default:``,hint:``}})}let i=hg(),a=e=>{let t=i.indexOf(e.label);return t<0?i.length:t};return n.sort((e,t)=>a(e)-a(t)||(a(e)===i.length?e.label.localeCompare(t.label):0))}var _g={caseMode:`Case`};function vg(e){let t=_g[e]??e.replace(/([a-z\d])([A-Z])/g,`$1 $2`).replace(/[_-]+/g,` `).toLowerCase();return t.charAt(0).toUpperCase()+t.slice(1)}function yg(e){for(let t of Object.values(K().CONNECTION_TYPES)){let n=t.settings.find(t=>t.key===e);if(n)return n}}function bg(e,t,n){let r=String(t.keys.map(t=>e[t]).find(e=>e!=null&&e!==``)??``),i=t.setting;return i.control===`select`&&i.choices?{key:t.keys[0],label:t.label,control:{type:`select`,value:r,onChange:n,options:[{value:``,label:`(From connection)`},...i.choices.filter(e=>e.value!==``)]}}:i.key===`grammar`?{key:t.keys[0],label:t.label,control:{type:`textarea`,value:r,onChange:n}}:{key:t.keys[0],label:t.label,width:`quarter`,control:{type:`text`,value:r,onChange:n,...r?{}:{placeholder:`(From connection)`}}}}function xg(e,t){if(!e.connection?.id)return`(No connection)`;let n=t.find(t=>t.id===e.connection.id);return n?n.name||`Unnamed connection`:`${e.connection.name||e.connection.id} (missing)`}function Sg(e,t){return!e.connection?.id||!t.some(t=>t.id===e.connection.id)}var Cg=[{value:`all`,label:`All`},{value:`attention`,label:`Needs attention`}];function wg(e,t){let n=[],r=pg(e,t),i=new Set;for(let t of gg(e,r)){let r=t.keys.map(t=>e[t]).find(e=>e!=null&&e!==``);r==null||i.has(t.label)||(i.add(t.label),n.push(`${t.label} ${String(r)}`))}let a={px:`Resolution`,format:`Format`,quality:`Image quality`};for(let t of Uh){let r=e[t];r!=null&&r!==``&&n.push(`${a[t]} ${Yh(t,String(r))}`)}return n.join(` · `)}function Tg(e){return{...e,id:sg,name:sg}}function Eg(e){let t={id:e.id,name:e.name};for(let[n,r]of Object.entries(e))n!==`id`&&n!==`name`&&r!=null&&r!==``&&(t[n]=r);return t}var Dg=()=>`cp${Date.now().toString(36)}${Math.random().toString(36).slice(2,6)}`;function Og(e){return!e||e.length===0||e.includes(`*all`)?``:[...e].sort().join(`,`)}function kg(e){let[t,n]=(0,Y.useState)({});return(0,Y.useEffect)(()=>{let t=!0;return e.connectionShares().then(e=>{t&&n(e.shares)}).catch(()=>{}),()=>{t=!1}},[e]),{sharesFor:(0,Y.useCallback)((e,n)=>t[Rn(e,n)],[t]),setFor:(0,Y.useCallback)((t,r,i)=>{e.setConnectionShares(t,r,i).then(e=>n(e.shares)).catch(()=>{})},[e]),setForEach:(0,Y.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 Ag({offer:e}){let t=X(),n=Fr(),r=Qr(),i=si(),[a,o]=(0,Y.useState)(null),[s,c]=(0,Y.useState)(null),[l,d]=(0,Y.useState)(null),[f,p]=(0,Y.useState)(!1),m=(0,Y.useRef)(null),h=e=>c({text:e instanceof Error?e.message:String(e),bad:!0}),g=(0,Y.useCallback)(()=>{d(null),t.packs().then(e=>o(e.packs),e=>d(e instanceof Error?e.message:String(e)))},[t]);(0,Y.useEffect)(g,[g]);let _=async()=>{await Promise.all([n.refresh(),r.refresh(),i.refresh()]),g()},v=async e=>{c({text:`Installing ${e.name}…`,bad:!1}),p(!0);try{let n=await t.installPack(e);await _();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){h(e)}finally{p(!1)}},y=async n=>{let r=[];try{for(let e of n)r.push((await t.removePack(e.id)).trash)}catch(e){h(e)}r.length&&(await _(),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 _()})()}}))};return(0,$.jsxs)(Tt,{id:`setup-packs`,title:`Packs`,note:a?jg(a.length,`pack`):``,actions:[{label:`Install pack…`,icon:u,busy:f,onSelect:()=>m.current?.click()}],children:[a===null&&(0,$.jsx)(Bt,{label:`Packs`,error:l,onRetry:g}),a&&!a.length&&(0,$.jsx)(`p`,{className:`unset`,children:`(No packs)`}),a&&a.length>0&&(0,$.jsx)(W,{className:`setup-table`,cards:!0,label:`Packs`,rows:a,rowKey:e=>e.id,select:{noun:`pack`,actions:e=>[{label:`Remove`,icon:J.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=>Mg(e)},{key:`menu`,header:``,className:`ds-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(an,{label:`${e.name} actions`,actions:[{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>void y([e])}]})}]}),s&&(0,$.jsx)(pt,{tone:s.bad?`error`:`info`,children:s.text}),(0,$.jsx)(`input`,{ref:m,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 jg=(e,t)=>`${e} ${t}${e===1?``:`s`}`,Mg=e=>[[e.datasets.length,`eval`],[e.pipelines.length,`experiment`],[e.sources.length,`source`],[e.presets,`preset`]].filter(([e])=>e>0).map(([e,t])=>jg(e,t)).join(`, `);function Ng({offer:e}){let t=X(),[n,r]=(0,Y.useState)(null),[i,a]=(0,Y.useState)(null),[o,s]=(0,Y.useState)(null),[c,l]=(0,Y.useState)(!1),d=(0,Y.useRef)(null),f=(0,Y.useCallback)(()=>{s(null),t.plugins().then(e=>r(e.plugins),e=>s(e instanceof Error?e.message:String(e)))},[t]);(0,Y.useEffect)(f,[f]);let p=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 Fn(o),a({text:n.installed?Mn[n.plugin]?`Installed ${n.plugin}, and it did not load: ${Mn[n.plugin]}`:`Installed ${n.plugin}.`:`${n.plugin} is up to date.`,bad:!!Mn[n.plugin]})}catch(e){a({text:e instanceof Error?e.message:String(e),bad:!0})}finally{l(!1)}},m=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})}f(),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(f)}})};return(0,$.jsxs)(Tt,{id:`setup-plugins`,title:`Plugins`,note:n?`${n.length} ${n.length===1?`plugin`:`plugins`}`:``,actions:[{label:`Install plugin…`,icon:u,busy:c,onSelect:()=>d.current?.click()}],children:[n===null&&(0,$.jsx)(Bt,{label:`Plugins`,error:o,onRetry:f}),n&&!n.length&&(0,$.jsx)(`p`,{className:`note`,children:`(No plugins)`}),n&&n.length>0&&(0,$.jsx)(W,{className:`setup-table`,cards:!0,label:`Plugins`,rows:n,rowKey:e=>e.id,select:{noun:`plugin`,actions:e=>[{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>void m(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=>Pg(e),cell:e=>Pg(e)||(0,$.jsx)(`span`,{className:`unset`,children:`(No registrations)`})},{key:`state`,header:`State`,cell:e=>Fg(e)},{key:`menu`,header:``,className:`ds-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(an,{label:`${e.id} actions`,actions:[{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>void m([e])}]})}]}),i&&(0,$.jsx)(pt,{tone:i.bad?`error`:`info`,children:i.text}),(0,$.jsx)(`input`,{ref:d,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&&p(t)}})]})}var Pg=e=>[...e.registers.outputKinds??[],...e.registers.modifiers??[],...e.registers.evalTypes??[],...(e.registers.connectionTypes??[]).map(e=>e.id)].join(`, `),Fg=e=>Mn[e.id]?`Failed: ${Mn[e.id]}`:Nn.has(e.id)?`Loaded`:`Not Loaded`,Ig={env:`Deployment`,lab:`Own`,default:`Default`};function Lg({cfg:e,editable:t,onClose:n}){let r=X(),i=e?.from===`lab`?e:null,[a,o]=(0,Y.useState)(i?.clientId??``),[s,c]=(0,Y.useState)(i?.tenant??``),[l,u]=(0,Y.useState)(null),[d,f]=(0,Y.useState)(!1),p=a!==(i?.clientId??``)||s!==(i?.tenant??``),m=async()=>{if(!p){n();return}f(!0);try{await r.setMicrosoft(a,s),ku(),n()}catch(e){u(e instanceof Error?e.message:String(e)),f(!1)}},h=e=>t=>{u(null),e(t)},g=!t,_=new URL(`/`,Cu()??window.location.href).href,v=gu(g?e?.tenant??``:s||`organizations`),y=[g?{key:`client`,label:`Client ID`,control:e?.clientId?{type:`code`,text:e.clientId}:{type:`static`,text:`(Not set)`,none:!0}}:{key:`client`,label:`Client ID`,control:{type:`text`,value:a,onChange:h(o),placeholder:e?.from==="default"?e.clientId:`00000000-0000-0000-0000-000000000000`}},g?{key:`tenant`,label:`Tenant`,control:e?.tenant?{type:`code`,text:e.tenant}:{type:`static`,text:`(Not set)`,none:!0}}:{key:`tenant`,label:`Tenant`,control:{type:`text`,value:s,onChange:h(c),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:e?.from?{type:`static`,text:Ig[e.from]}:{type:`static`,text:`(No app)`,none:!0}}];return(0,$.jsx)(De,{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 Rg={env:`Deployment`,lab:`Own`,default:`Default`},zg=`https://console.cloud.google.com/apis/credentials`,Bg=[{value:`installed`,label:`Desktop app`},{value:`web`,label:`Web application`}],Vg={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.`},Hg=`/api/connections/google/callback`,Ug=()=>/^(localhost|127\.|\[::1\])/.test(window.location.hostname);function Wg(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 Gg({onClose:e,onSaved:t}){let n=X(),[r,i]=(0,Y.useState)(void 0),[a,o]=(0,Y.useState)(!0),[s,c]=(0,Y.useState)(``),[l,u]=(0,Y.useState)(``),[d,f]=(0,Y.useState)(``),[p,m]=(0,Y.useState)(Ug()?`installed`:`web`),[h,g]=(0,Y.useState)({clientId:``,apiKey:``,kind:``}),[_,v]=(0,Y.useState)(null),[y,b]=(0,Y.useState)(null),[x,S]=(0,Y.useState)(!1);(0,Y.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??(Ug()?`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=Wg(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:r?.clientId?{type:`code`,text:r.clientId}:{type:`static`,text:`(Not set)`,none:!0}},{key:`secret`,label:`Client secret`,control:r?.hasSecret?{type:`static`,text:`Held`}:{type:`static`,text:`(Not set)`,none:!0}},{key:`key`,label:`API key`,control:r?.apiKey?{type:`code`,text:r.apiKey}:{type:`static`,text:`(Not set)`,none:!0}},{key:`type`,label:`Type`,control:{type:`static`,text:Bg.find(e=>e.value===A).label}}]:[{key:`client`,label:`Client ID`,control:{type:`text`,value:s,onChange:T(c),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),placeholder:`AIza…`}},{key:`type`,label:`Type`,hint:Vg[p],control:{type:`select`,value:p,options:Bg,onChange:e=>T(m)(e)}}];return j.push({key:`redirect`,label:`Redirect URI`,control:A===`web`?{type:`code`,text:new URL(Hg,window.location.href).href}:{type:`static`,text:`Not Needed`}},{key:`app`,label:`App`,control:r?.from?{type:`static`,text:Rg[r.from]}:{type:`static`,text:`(No app)`,none:!0}}),(0,$.jsx)(De,{title:`Google Drive`,status:_??y??void 0,statusBad:!!_,focusRow:O?void 0:`client`,sections:[{key:`app`,rows:j}],actions:[{label:`Google Cloud`,icon:$n,onSelect:()=>{window.open(zg,`_blank`,`noopener`)}},...O?[]:[{label:`Load client file…`,icon:rr,onSelect:E}]],...O?{}:{onSave:()=>void w(),saving:x,dirty:C},onClose:e})}var Kg=`microsoft`,qg=`google`;function Jg(){let e=X(),{list:t}=Ss(),n=t.filter(e=>!e.archived).map(e=>({id:e.id,name:e.name})),{sharesFor:r,setFor:a}=kg(e),{cfg:o,editable:s}=Au(),[c,l]=(0,Y.useState)(null),[u,d]=(0,Y.useState)(null),[p,m]=(0,Y.useState)(void 0),[h,g]=(0,Y.useState)(null),[_,v]=(0,Y.useState)(null),y=(0,Y.useCallback)(()=>{v(null),e.connections().then(e=>d(e.connections),e=>{e instanceof Ln&&e.status===404?d([]):v(Q(e))})},[e]);(0,Y.useEffect)(y,[y]),(0,Y.useEffect)(()=>{if(!o){m(null);return}xu(o).then(m,()=>m(null))},[o]);let b=[...u??[],...o||s?[{id:Kg,label:`Microsoft 365`,configured:!!o,signedIn:!!p,account:p??null,problem:null}]:[]],x=async t=>{g(null);try{t.id===Kg&&o?await Tu(o):window.location.assign((await e.signIn(t.id)).url)}catch(e){g(e instanceof Error?e.message:String(e))}},S=async t=>{g(null);try{t.id===Kg&&o?(await Eu(o),m(null)):d((await e.signOut(t.id)).connections)}catch(e){g(e instanceof Error?e.message:String(e))}};if(u===null)return(0,$.jsx)(Tt,{id:`setup-connections`,title:`Accounts`,children:(0,$.jsx)(Bt,{label:`Accounts`,error:_,onRetry:y})});if(!b.length)return null;let C=b.filter(e=>e.problem);return(0,$.jsxs)(Tt,{id:`setup-connections`,title:`Accounts`,note:`${b.filter(e=>e.signedIn).length} of ${b.length}`,children:[(0,$.jsx)(W,{className:`setup-table`,cards:!0,label:`Accounts`,rows:b,rowKey:e=>e.id,columns:[{key:`label`,header:`Account`,role:`title`,cell:e=>e.label},{key:`status`,header:`Status`,cell:e=>e.id===Kg&&o&&p===void 0?(0,$.jsx)(Bt,{inline:!0,label:`Checking…`}):Yg(e)},{key:`account`,header:`Signed in as`,cell:e=>e.account||(0,$.jsx)(`span`,{className:`unset`,children:`(No one)`})},{key:`workspaces`,header:`Workspaces`,sort:e=>Og(r(`account`,e.id)),cell:e=>(0,$.jsx)(f,{label:e.label,workspaces:n,shares:r(`account`,e.id),onChange:t=>a(`account`,e.id,t)})},{key:`menu`,header:``,className:`ds-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)(an,{label:`${e.label} actions`,actions:[e.signedIn?{label:`Sign out`,icon:hr,onSelect:()=>void S(e)}:{label:`Sign in`,icon:mr,disabled:!e.configured,onSelect:()=>void x(e)},...e.id===Kg||e.id===qg?[{label:`Configure…`,icon:i,onSelect:()=>l(e.id)}]:[]]})}]}),C.map(e=>(0,$.jsxs)(pt,{tone:`error`,children:[e.label,`: `,e.problem]},e.id)),h&&(0,$.jsx)(pt,{tone:`error`,children:h}),c===Kg&&o!==void 0&&(0,$.jsx)(Lg,{cfg:o,editable:s,onClose:()=>l(null)}),c===qg&&(0,$.jsx)(Gg,{onClose:()=>l(null),onSaved:y})]})}var Yg=e=>e.configured?e.signedIn?`Signed In`:`Signed Out`:`Not Configured`,Xg={google:`Google Drive`,microsoft:`Microsoft 365`},Zg=e=>`/w/${e}`,Qg=e=>K().slugOf(e),$g=e=>{let t=Qg(e.name);return!!t&&t!==e.slug&&t!==e.slug.replace(/-\d+$/,``)};function e_(){let e=X(),t=Dt(),{current:n,switchTo:r}=Ss(),[i,a]=(0,Y.useState)(null),[o,s]=(0,Y.useState)(null),[c,l]=(0,Y.useState)(null),[u,d]=(0,Y.useState)(!1),[f,p]=(0,Y.useState)(!1),[m,h]=(0,Y.useState)(null),[g,_]=(0,Y.useState)(null),v=e=>l({text:e instanceof Error?e.message:String(e),bad:!0}),y=(0,Y.useCallback)(()=>{s(null),e.workspaces().then(e=>a(e.workspaces),e=>s(e instanceof Error?e.message:String(e)))},[e]);(0,Y.useEffect)(y,[y]);let b=(i??[]).filter(e=>!e.archived).length,x=(0,Y.useCallback)(async t=>{try{let n=await e.createWorkspace(t);if(n.workspace)return r(n.workspace.slug);a(n.workspaces),l(null)}catch(e){v(e)}},[e,r]),S=(0,Y.useCallback)(async(t,n)=>{try{a((await e.renameWorkspace(t.id,n)).workspaces)}catch(e){v(e)}},[e]),C=(0,Y.useCallback)(async t=>{try{a((await e.regenerateWorkspace(t.id)).workspaces),l(null)}catch(e){v(e)}},[e]),w=(0,Y.useCallback)(async r=>{try{let i=await e.archiveWorkspace(r.id);if(r.slug===n?.slug){let e=bs(i.workspaces);if(e){vs({id:r.id,slug:r.slug,name:r.name}),location.assign(hs(e.slug));return}}a(i.workspaces),t.offer({label:`Archived ${r.name}`,restore:()=>void(async()=>{try{a((await e.unarchiveWorkspace(r.id)).workspaces)}catch(e){v(e)}})()})}catch(e){v(e)}},[e,t,n]),T=(0,Y.useCallback)(async t=>{try{a((await e.unarchiveWorkspace(t.id)).workspaces)}catch(e){v(e)}},[e]),E=(0,Y.useCallback)(async t=>{try{a((await e.setDefaultWorkspace(t.id)).workspaces)}catch(e){v(e)}},[e]),D=(0,Y.useCallback)(async n=>{try{let{trash:r}=await e.deleteWorkspace(n.id);a(e=>(e??[]).filter(e=>e.id!==n.id)),t.offer({label:`Deleted ${n.name}`,restore:()=>void(async()=>{try{a((await e.restoreWorkspace(r)).workspaces)}catch(e){v(e)}})()})}catch(e){v(e)}},[e,t]),O=(i??[]).filter(e=>e.archived),k=(i??[]).filter(e=>u||!e.archived);return(0,$.jsxs)(Tt,{id:`setup-workspaces`,title:`Workspaces`,note:i?i_(i.length):``,children:[(0,$.jsx)(`p`,{className:`note`,children:`Each has its own experiments, runs and library.`}),i===null&&(0,$.jsx)(Bt,{label:`Workspaces`,error:o,onRetry:y}),i&&(0,$.jsxs)($.Fragment,{children:[O.length>0&&(0,$.jsx)(`div`,{className:`ws-archived`,children:(0,$.jsx)(I,{label:`Show archived`,checked:u,onChange:d})}),(0,$.jsx)(W,{className:`setup-table ws-table`,cards:!0,label:`Workspaces`,rows:k,rowKey:e=>e.id,columns:t_({onlyActive:b<=1,currentSlug:n?.slug??null,onRename:h,onRegenerate:_,onArchive:e=>void w(e),onUnarchive:e=>void T(e),onDelete:e=>void D(e),onSetDefault:e=>void E(e)})}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:()=>p(!0),children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add workspace`]})]}),c&&(0,$.jsx)(pt,{tone:c.bad?`error`:`info`,children:c.text}),(0,$.jsx)(Qt,{undo:t.undo,onUndo:t.undoNow,onDismiss:t.dismiss}),f&&(0,$.jsx)(n_,{onCreate:e=>void x(e),onClose:()=>p(!1)}),m&&(0,$.jsx)(sn,{title:`Rename ${m.name}`,value:m.name,onCommit:e=>void S(m,e),onClose:()=>h(null)}),g&&(0,$.jsx)(r_,{workspace:g,onRegenerate:()=>void C(g),onClose:()=>_(null)})]})}function t_(e){let t=t=>t.archived?[{label:`Rename…`,icon:J.rename,onSelect:()=>e.onRename(t)},{label:`Unarchive`,icon:Wn,onSelect:()=>e.onUnarchive(t)},{label:`Delete permanently`,icon:J.remove,destructive:!0,onSelect:()=>e.onDelete(t)}]:[{label:`Rename…`,icon:J.rename,onSelect:()=>e.onRename(t)},...t.isDefault?[]:[{label:`Set as default`,icon:Ar,onSelect:()=>e.onSetDefault(t)}],...e.onlyActive?[]:[{label:`Archive`,icon:Gn,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:`link-btn ws-addr`,href:Zg(t.slug),children:(0,$.jsx)(`code`,{children:Zg(t.slug)})}),!t.archived&&$g(t)&&(0,$.jsxs)(`button`,{type:`button`,className:`link-btn ws-regen`,onClick:()=>e.onRegenerate(t),children:[(0,$.jsx)(br,{size:12,"aria-hidden":`true`}),`Regenerate…`]})]})},{key:`pipelines`,header:`Experiments`,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=>e.lastUsed?o_(e.lastUsed):(0,$.jsx)(`span`,{className:`unset`,children:`(Never)`})},{key:`status`,header:`Status`,sort:t=>a_(t,e.currentSlug),cell:t=>{if(t.archived)return(0,$.jsx)(`span`,{className:`unset`,children:`Archived`});let n=[t.slug===e.currentSlug?`Current`:``,t.isDefault?`Default`:``].filter(Boolean);return n.length?(0,$.jsx)(Ye,{children:n.join(`, `)}):(0,$.jsx)(`span`,{className:`unset`,children:`—`})}},{key:`menu`,header:``,label:``,className:`setup-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)($e,{actions:t(e),label:`${e.name} actions`})}]}function n_({onCreate:e,onClose:t}){let n=X(),[r]=Z(`promptlab.connections`),i=(0,Y.useMemo)(()=>r.list.filter(e=>K().listedConnection(e)),[r.list]),[a,o]=(0,Y.useState)(``),[s,c]=(0,Y.useState)(``),[l,u]=(0,Y.useState)(`nothing`),[d,f]=(0,Y.useState)(null),[p,m]=(0,Y.useState)({});(0,Y.useEffect)(()=>{let e=!0;return n.connectionShares().then(t=>{e&&f(t.shares)}).catch(()=>{e&&f({})}),()=>{e=!1}},[n]);let h=(0,Y.useMemo)(()=>{let e=[];for(let[t,n]of Object.entries(d??{})){if(n.includes(`*all`))continue;let r=t.indexOf(`:`),a=t.slice(0,r),o=t.slice(r+1),s=a===`profile`?i.find(e=>e.id===o)?.name:Xg[o];s&&e.push({key:t,kind:a,id:o,label:s,prefill:n.includes(yn)})}return e.sort((e,t)=>e.label.localeCompare(t.label))},[d,i]),g=i.filter(e=>!d?.[`profile:${e.id}`]||d[`profile:${e.id}`].includes(`*all`)).length,_=e=>e.key in p?p[e.key]:e.prefill,v=[{key:`new`,rows:[{key:`name`,label:`Name`,width:`full`,required:!0,control:{type:`text`,value:a,onChange:o}},{key:`slug`,label:`Address`,width:`full`,control:{type:`text`,value:s,placeholder:Zg(Qg(a)||`workspace`),onChange:e=>c(e.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+/,``).slice(0,K().SLUG_MAX))}},{key:`content`,label:`Starting content`,control:{type:`select`,value:l,onChange:e=>u(e),options:[{value:`nothing`,label:`Nothing`},{value:`demo`,label:`Demo pack`}]}}]}];return h.length&&v.push({key:`connections`,title:`Also share`,rows:h.map(e=>({key:e.key,label:e.label,control:{type:`checkbox`,checked:_(e),onChange:t=>m(n=>({...n,[e.key]:t}))}})),...g?{children:(0,$.jsxs)(`p`,{className:`note`,children:[g===1?`1 connection is`:`${g} connections are`,` shared with every workspace.`]})}:{}}),d===null?null:(0,$.jsx)(De,{title:`New workspace`,focusRow:`name`,sections:v,dirty:!!a.trim()||!!s.trim()||l!==`nothing`,adding:!0,onSave:()=>{let n=h.filter(_).map(e=>({kind:e.kind,id:e.id}));e({name:a.trim(),...s.trim()?{slug:s.trim()}:{},content:l,...h.length?{connections:n}:{}}),t()},onClose:t})}function r_({workspace:e,onRegenerate:t,onClose:n}){let r=Zg(Qg(e.name)||`workspace`);return(0,$.jsx)(Ae,{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:Zg(e.slug)})})]}),(0,$.jsxs)(`div`,{children:[(0,$.jsx)(`dt`,{children:`New`}),(0,$.jsx)(`dd`,{children:(0,$.jsx)(`code`,{children:r})})]})]}),(0,$.jsx)(pt,{tone:`error`,children:`The old address stops working. Bookmarks and links to it break, and the lab will not redirect them.`})]})})}var i_=e=>e===1?`1 workspace`:`${e} workspaces`,a_=(e,t)=>e.archived?`Archived`:[e.slug===t?`Current`:``,e.isDefault?`Default`:``].filter(Boolean).join(`, `),o_=e=>ht(e)||e,s_=[{id:`connections`,label:`Connections`,icon:B},{id:`workspaces`,label:`Workspaces`,icon:Jn}];function c_({section:e=`connections`,id:t=null,kind:n}){return(0,$.jsxs)(`div`,{className:`setup`,children:[(0,$.jsx)(en,{label:`Setup`,level:`section`,className:`setup-sections`,current:e,items:s_.map(({id:e,label:t,icon:n})=>({key:e,label:t,href:Ti({tab:`setup`,section:e,id:null}),icon:n}))}),(0,$.jsx)(`section`,{className:`setup-section`,"data-section":e,children:e===`workspaces`?(0,$.jsx)(e_,{}):(0,$.jsx)(f_,{connectionId:n===`profile`?null:t,profileId:n===`profile`?t:null})})]})}var l_={tab:`setup`,section:`connections`,id:null},u_=e=>({tab:`setup`,section:`connections`,id:e}),d_=()=>{let e=Oi();e.tab===`setup`&&e.id&&Ei(l_)};function f_({connectionId:e=null,profileId:t=null}){let n=Fr(),r=Os(),i=X(),a=Dt(),{list:o}=Ss(),s=o.filter(e=>!e.archived).map(e=>({id:e.id,name:e.name})),{sharesFor:c,setFor:l,setForEach:u}=kg(i),[d,p]=Z(`promptlab.connections`),m=(0,Y.useMemo)(()=>d.list.filter(e=>K().listedConnection(e)),[d.list]),[h]=Z(`promptlab.connectionProfiles`),[g]=Z(`promptlab.versions`),[_]=Z(`promptlab.workflows`),{list:v}=$r(),y=v??[],[b,x]=(0,Y.useState)(null),[S,C]=(0,Y.useState)(null),w=(0,Y.useRef)(S);w.current=S;let[T,E]=(0,Y.useState)(null),[D,O]=(0,Y.useState)(null),[k,A]=(0,Y.useState)({}),[j,M]=(0,Y.useState)(``),[N,P]=(0,Y.useState)(`all`),[F,I]=(0,Y.useState)({key:`name`,dir:`asc`}),[ee,L]=(0,Y.useState)([]),[te,R]=(0,Y.useState)(``),[re,z]=(0,Y.useState)({}),H=(0,Y.useRef)(re);H.current=re;let[ie,U]=(0,Y.useState)(()=>new Set),ae=(0,Y.useRef)(null);(0,Y.useEffect)(()=>{let e=!0;return i.presets().then(t=>{e&&L(t)}).catch(()=>{}),i.config().then(t=>{e&&R(t.ollama??``)}).catch(()=>{}),()=>{e=!1}},[i]);let oe=(0,Y.useRef)(d);oe.current=d;let se=(0,Y.useRef)(!0);(0,Y.useEffect)(()=>()=>{se.current=!1},[]);let ce=[...ie].sort().join(`,`);(0,Y.useEffect)(()=>{ae.current=null},[ce]);let le=(0,Y.useCallback)(e=>{let t=w.current;return t?.p.id===e?t.p:oe.current.list.find(t=>t.id===e)},[]),G=(0,Y.useCallback)((e,t)=>{z(n=>({...n,[e.id]:{at:P_(e),...t}}))},[]),ue=(0,Y.useCallback)(e=>{let t=w.current;if(t?.p.id!==e.id)return;let n={...t,p:e};w.current=n,C(n)},[]),de=(0,Y.useCallback)(()=>{let e=w.current;if(!e)return;let t=oe.current,r=e.was?e:{...e,p:{...e.p,name:e.p.name.trim()?e.p.name:R_,slug:L_(e.p.name.trim()||R_,t.list)}},i=r.was?{...t,list:t.list.map(e=>e.id===r.p.id?r.p:e)}:{...t,list:[...t.list,r.p],at:r.p.id};oe.current=i,p(i),qr(n,`profile`,r.p.id,r.p,`idle`),C(null),x(null),d_()},[p,n]),fe=(0,Y.useCallback)(()=>{C(null),x(null)},[]),pe=(0,Y.useCallback)(()=>{fe(),d_()},[fe]),me=(0,Y.useCallback)(()=>{let e=z_();C({p:e,was:null,start:e}),x({id:``,focus:`name`})},[]),he=(0,Y.useCallback)(e=>{let t=oe.current.list.find(t=>t.id===e);if(!t)return;let n=`${t.name||`Connection`} copy`,r={...structuredClone(t),id:I_(),name:n,slug:L_(n,oe.current.list),key:t.key},i={...oe.current,list:[...oe.current.list,r]};oe.current=i,p(i)},[p]),ge=(0,Y.useCallback)(e=>{let t=oe.current,n=new Set(e),r=t.list.filter(e=>!n.has(e.id));if(!r.length||r.length===t.list.length)return;let i=n.has(t.at)?r[0].id:t.at,o={...t,list:r,at:i,...t.grader&&n.has(t.grader)?{grader:``}:{}};oe.current=o,p(o),x(null),C(null),d_(),U(new Set);let s=t.list.filter(e=>n.has(e.id));a.offer({label:s.length===1?`Deleted ${s[0].name||`the connection`}`:`Removed ${s.length} connections`,restore:()=>{oe.current=t,p(t)}})},[p,a]),_e=(0,Y.useCallback)(e=>ge([e]),[ge]),ve=(0,Y.useCallback)(e=>{let t=e.doc.id,r=oe.current.list.find(e=>e.id===t);r&&qr(n,`profile`,t,r,`restore`);let i=K().splitProfile(e.doc).connection,a=oe.current,o={...a,list:a.list.map(e=>e.id===t?i:e)};oe.current=o,p(o),ye()},[n,p]),ye=(0,Y.useCallback)(()=>{E(e=>(e?.back&&ke(e.id),null))},[]),be=(0,Y.useCallback)(async e=>{let t=le(e);if(!t)return;let n=K().CONNECTION_TYPES[t.type];if(n?.local){A(t=>({...t,[e]:{phase:`connected`,ms:0,lines:[`${n.label} answers each text item with its own text, and sends nothing anywhere.`]}}));return}let r=n?.keyless?``:t.key,a=O_(t,n,te)||`Ollama`,o=performance.now(),s=()=>Math.round(performance.now()-o),c=[];A(t=>({...t,[e]:{phase:`checking`,lines:[`Checking…`]}}));try{let o=((await i.models({url:K().addressOf(t),key:r,type:t.type,settings:k_(t,n)})).data||[]).map(e=>e.id).sort();if(G(t,{ids:o}),c.push(`Reached ${a}.`,`${o.length} ${o.length===1?`model`:`models`}: ${o.join(`, `)||`none`}.`),(n?.answers??[`prompt`]).includes(`prompt`)){if(t.model.trim()){let e=K().apiBase(t.url||n?.url||``),a=K().connectionRequest(t,`hi`,null,e,r),o=await i.modelsAsk({url:K().addressOf(t),key:r,type:t.type,body:a.body}),{raw:s}=K().connectionReply(t,o);c.push(``,`Asked ${t.model} “hi”; it replied:`,s||`(an empty response)`)}else c.push(``,`No model is picked, so nothing was asked.`)}A(t=>({...t,[e]:{phase:`connected`,lines:c,ms:s()}}))}catch(t){c.push(...c.length?[``]:[],t instanceof Error?t.message:String(t)),A(t=>({...t,[e]:{phase:`error`,lines:c,ms:s()}}))}},[i,le,te,G]),xe=(0,Y.useCallback)(async e=>{let t=K().CONNECTION_TYPES[e.type],n=t?.keyless?``:e.key,r=O_(e,t,te)||`Ollama`,a=performance.now(),o=()=>Math.round(performance.now()-a);try{let a=await i.models({url:K().addressOf(e),key:n,type:e.type,settings:k_(e,t),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 ${r}.`,`${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()}}}},[i,te]),Se=(0,Y.useCallback)((e,t=!1)=>{let n=le(e);if(!n)return;let r=H.current[e];if(!t&&r?.at===P_(n)&&(r.ids||r.loading))return;let i=P_(n);G(n,{loading:!0}),(async()=>{let{state:t,models:r}=await xe(n);se.current&&H.current[e]?.at===i&&G(n,r?{ids:r}:{error:t.lines[t.lines.length-1]??``})})()},[le,xe,G]),Ce=(0,Y.useCallback)(e=>{let t=new Set(e),n=oe.current.list.filter(e=>t.has(e.id)&&E_(e));if(n.length){A(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 xe(e);se.current&&(n&&G(e,{ids:n}),A(n=>({...n,[e.id]:t})))})()}},[xe,G]),we=(0,Y.useCallback)((e,t)=>{if(!e.length)return;ae.current||=new Map(e.map(e=>[e,c(`profile`,e)]));let n=ae.current;u(`profile`,e.map(e=>({id:e,workspaces:t})));let r=e.length===1?oe.current.list.find(t=>t.id===e[0])?.name||`the connection`:`${e.length} connections`;a.offer({label:`Workspaces for ${r}`,restore:()=>{u(`profile`,[...n].map(([e,t])=>({id:e,workspaces:t&&t.length?[...t]:[qe]}))),ae.current=null}})},[c,u,a]),Te=(0,Y.useCallback)((e,t)=>{let n=le(e);if(!n)return;let r={...n};for(let e of F_(n.type))r[e]=``;for(let[e,n]of Object.entries(t))r[e]=n;ue(r)},[le,ue]),Ee=(0,Y.useCallback)(e=>{A(t=>{if(!(e in t))return t;let{[e]:n,...r}=t;return r})},[]),De=(0,Y.useCallback)((e,t,n)=>{let r=le(e);if(r){if([`url`,`key`,`model`].includes(t)&&Ee(e),t===`name`){let t=oe.current.list.filter(t=>t.id!==e);if(!r.slug||r.slug===L_(r.name,t)){ue({...r,name:n,slug:L_(n,t)});return}}t===`slug`&&(n=n.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+/,``).slice(0,K().SLUG_MAX)),ue({...r,[t]:n})}},[le,ue,Ee]),Oe=(0,Y.useCallback)((e,t)=>{let n=le(e);if(!n)return;let r={...n,type:t};Ee(e);let i=K().CONNECTION_TYPES[t];i?.keyless&&(r.key=``);let a=K().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`,`type`].includes(e)||r[e]===``||F_(t).includes(e)||delete r[e];ue(r)},[le,ue,Ee]),ke=(0,Y.useCallback)((e,t)=>{let n=oe.current.list.find(t=>t.id===e);n&&(C({p:structuredClone(n),was:n,start:n}),x(t?{id:e,focus:t}:{id:e}))},[]),Ae=(0,Y.useCallback)((e,t)=>{ke(e,t),Ei(u_(e))},[ke]);(0,Y.useEffect)(()=>{if(!e){w.current?.was&&fe();return}w.current?.p.id!==e&&(oe.current.list.some(t=>t.id===e)?ke(e):Di(l_))},[e,ke,fe]);let je=(0,Y.useCallback)(e=>g.profile?.[e]??[],[g]),Me=b?S?.p??null:null,Ne=S?S.was?d.list.map(e=>e.id===S.p.id?S.p:e):[...d.list,S.p]:d.list,Pe=d.grader??null,Fe=(0,Y.useMemo)(()=>{let e=new Map;for(let t of d.list)e.set(t.id,$h(t.id,_.list,y,h.list));return e},[d.list,_.list,y,h.list]),Ie=j.trim().toLowerCase(),Le=m.filter(e=>m_(N,e,k[e.id])&&(!Ie||[e.name,e.model].some(e=>String(e??``).toLowerCase().includes(Ie)))),Re=new Set(Le.map(e=>e.id)),ze=Le.filter(e=>ie.has(e.id)),Be=[...ie].filter(e=>!Re.has(e)).length,Ve=[{label:`Test connection`,icon:B,disabled:!ze.some(E_),onSelect:()=>Ce(ze.map(e=>e.id))},{label:`Remove`,icon:J.remove,destructive:!0,disabled:!ze.length||ze.length>=m.length,onSelect:()=>ge(ze.map(e=>e.id))}],He=ie.size>0?(0,$.jsx)(V,{count:ie.size,noun:`connection`,hidden:Be,actions:Ve,onClear:()=>U(new Set),children:ze.length>0&&(0,$.jsx)(f,{label:ze.length===1?`1 connection`:`${ze.length} connections`,workspaces:s,shares:D_(ze.map(e=>c(`profile`,e.id))),onChange:e=>we(ze.map(e=>e.id),e)})}):null;return(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(Tt,{id:`setup-ai-connections`,title:`AI Connections`,children:[(0,$.jsx)(`div`,{className:`setup-intro`,children:(0,$.jsx)(`p`,{className:`note`,children:`Where the lab sends prompts.`})}),m.length>0&&(0,$.jsx)(xt,{noun:`connections`,search:{label:`Search connections`,placeholder:`Name or model`,value:j,onChange:M},filters:[],quick:{label:`Show`,value:N,onChange:e=>P(e),options:p_}}),He,(0,$.jsx)(W,{className:`setup-table`,total:m.length,empty:`(No connections)`,cards:!0,label:`AI Connections`,sort:F,onSort:I,rows:Le,rowKey:e=>e.id,select:{noun:`connection`,label:e=>e.name||`Unnamed connection`,selected:ie,onChange:U},rowProps:e=>({className:`setup-row`,onClick:t=>{t.target.closest(`button, a, .menu`)||Ae(e.id)}}),columns:h_({tests:k,only:m.length===1,grader:Pe,labOllama:te,uses:e=>Fe.get(e)??[],onGrader:e=>{let t=oe.current,n={...t,grader:t.grader===e?``:e};oe.current=n,p(n)},onOpen:Ae,onClone:he,link:e=>r(u_(e)),onRemove:_e,onTest:be,onProbe:e=>Ce([e]),onResult:e=>O({id:e,back:!1}),workspaces:s,sharesFor:e=>c(`profile`,e),onShare:(e,t)=>l(`profile`,e,t)})}),(0,$.jsxs)(`button`,{type:`button`,className:`ghost add-btn`,onClick:me,children:[(0,$.jsx)(ne,{size:14,"aria-hidden":`true`}),`Add connection`]})]}),(0,$.jsx)(dg,{profileId:t,connections:d.list,workflows:_.list,groups:y,presets:ee,offer:a.offer}),(0,$.jsx)(Jg,{}),(0,$.jsx)(Ng,{offer:a.offer}),(0,$.jsx)(Ag,{offer:a.offer}),(0,$.jsx)(Qt,{undo:a.undo,onUndo:a.undoNow,onDismiss:a.dismiss}),Me&&(0,$.jsx)(y_,{connection:Me,slug:K().withSlugs(Ne).find(e=>e.id===Me.id)?.slug??``,...b?.focus?{focus:b.focus}:{},test:k[Me.id],list:re[Me.id]?.at===P_(Me)?re[Me.id]:void 0,onList:e=>Se(Me.id,e),canRestore:!!S?.was&&!!je(Me.id).length,uses:Fe.get(Me.id)??[],isNew:!S?.was,labOllama:te,dirty:!!S&&JSON.stringify(S.p)!==JSON.stringify(S.start),onSave:de,onClose:pe,...S?.was?{link:r(u_(Me.id))}:{},onSetting:De,onType:Oe,onTest:be,onResult:()=>{x(null),O({id:Me.id,back:!0})},onRestore:()=>{fe(),E({id:Me.id,back:!0})},onApplyTemplate:e=>Te(Me.id,e)}),D&&k[D.id]&&(0,$.jsx)(A_,{test:k[D.id],onClose:()=>{D.back&&w.current?.p.id===D.id&&x({id:D.id}),O(null)}}),T&&(0,$.jsx)(st,{noun:`connection`,versions:je(T.id),at:e=>e.at,reason:e=>e.reason,onClose:ye,onRestore:ve})]})}var p_=[{value:`all`,label:`All`},{value:`connected`,label:`Connected`},{value:`attention`,label:`Needs attention`}];function m_(e,t,n){if(e===`all`)return!0;let r=T_(t,n)?.kind;return e===`connected`?r===`connected`:r===`key-needed`||r===`unreachable`}function h_({tests:e,only:t,grader:n,labOllama:r,onGrader:i,onOpen:a,onClone:o,link:s,onRemove:c,onTest:l,onProbe:u,onResult:d,uses:p,workspaces:m,sharesFor:h,onShare:g}){let _=e=>e.name||`Unnamed connection`,v=e=>K().CONNECTION_TYPES[e.type]?.label??e.type,y=r=>[{label:`Edit…`,icon:J.edit,onSelect:()=>a(r.id)},{label:`Rename…`,icon:J.rename,onSelect:()=>a(r.id,`name`)},{label:`Clone`,icon:J.clone,onSelect:()=>o(r.id)},s(r.id),{label:`Test connection`,icon:B,busy:e[r.id]?.phase===`checking`,onSelect:()=>void l(r.id)},...(K().CONNECTION_TYPES[r.type]?.answers??[`prompt`]).includes(`prompt`)&&!K().CONNECTION_TYPES[r.type]?.local?[{label:n===r.id?`Stop grading`:`Make grader`,icon:ur,onSelect:()=>i(r.id)}]:[],...t?[]:[{label:`Remove`,icon:J.remove,destructive:!0,onSelect:()=>c(r.id)}]];return[{key:`name`,header:`Connection`,role:`title`,sort:_,cell:t=>{let n=T_(t,e[t.id]);return(0,$.jsxs)(`div`,{className:`setup-profile`,children:[n&&(0,$.jsx)(`button`,{type:`button`,className:`setup-dot ${n.kind}`,title:n.title,"aria-label":`Connection status: ${n.label}`,onClick:()=>{e[t.id]?d(t.id):u(t.id)},children:(0,$.jsx)(`span`,{className:`setup-dot-mark`,"aria-hidden":`true`})}),(0,$.jsxs)(`span`,{className:`setup-profile-main`,children:[(0,$.jsx)(`span`,{className:`setup-profile-name`,children:(0,$.jsx)(`button`,{type:`button`,className:`link-btn setup-open`,onClick:()=>a(t.id),children:_(t)})}),(0,$.jsx)(`span`,{className:`setup-sub`,children:v(t)})]})]})}},{key:`endpoint`,header:`Endpoint`,label:``,sort:e=>__(e,r),cell:e=>{let t=__(e,r);return t?(0,$.jsx)(`span`,{className:`setup-endpoint`,children:v_(t)}):(0,$.jsx)(`span`,{className:`unset`,children:`—`})}},{key:`model`,header:`Model`,label:``,sort:e=>e.model.trim(),cell:e=>(0,$.jsx)(g_,{p:e})},{key:`used`,header:`Used by`,label:``,sort:e=>ng(p(e.id)),cell:e=>(0,$.jsx)(rg,{uses:p(e.id)})},{key:`workspaces`,header:`Workspaces`,sort:e=>Og(h(e.id)),cell:e=>(0,$.jsx)(f,{label:_(e),workspaces:m,shares:h(e.id),onChange:t=>g(e.id,t)})},{key:`menu`,header:``,label:``,className:`setup-menu`,role:`menu`,sort:null,cell:e=>(0,$.jsx)($e,{actions:y(e),label:`${_(e)} actions`})}]}function g_({p:e}){let t=K().CONNECTION_TYPES[e.type];return t?.local?(0,$.jsx)(`span`,{className:`unset`,children:`—`}):(t?.answers??[`prompt`]).includes(`prompt`)?e.model.trim()?(0,$.jsx)($.Fragment,{children:e.model.trim()}):(0,$.jsx)(`span`,{className:`unset`,children:`(No model)`}):(0,$.jsx)(`span`,{className:`unset`,children:`(From request)`})}function __(e,t){let n=K().CONNECTION_TYPES[e.type];return n?.local?``:O_(e,n,t).replace(/^https?:\/\//,``).replace(/\/+$/,``)}function v_(e){return e.split(/(?=[.:])|(?<=\/)/).flatMap((e,t)=>t?[(0,$.jsx)(`wbr`,{},t),e]:[e])}function y_({connection:e,slug:t,focus:n,test:r,list:i,onList:o,canRestore:c,uses:l,isNew:u,labOllama:d,dirty:f,link:p,onSave:m,onClose:h,onSetting:g,onType:_,onTest:v,onResult:y,onRestore:b,onApplyTemplate:x}){let S=K().CONNECTION_TYPES[e.type],C=(S?.answers??[`prompt`]).includes(`prompt`),w=(0,Y.useMemo)(()=>x_(e),[e]),T=t=>n=>g(e.id,t,n),E=[{key:`name`,label:`Name`,width:`full`,required:!0,control:{type:`text`,value:e.name,onChange:T(`name`),...u?{placeholder:R_}:{}}},...u?[]:[{key:`slug`,label:`Slug`,width:`full`,required:!0,...e.slug&&K().slugOf(e.slug)!==t?{hint:`Taken`}:{},control:{type:`text`,value:e.slug??``,placeholder:t,onChange:T(`slug`)}}],{key:`type`,label:`Type`,required:!0,hint:S?.description,control:{type:`select`,value:e.type,onChange:t=>_(e.id,t),options:a(Object.entries(K().CONNECTION_TYPES).filter(([t,n])=>n.picker!==!1||t===e.type).map(([e,t])=>({value:e,label:t.label})))}},...S?.local?[]:[...S?.url?[]:[{key:`url`,label:`Address`,width:`full`,control:{type:`text`,value:e.url,placeholder:d,onChange:T(`url`)}}],...S?.keyless?[]:[{key:`key`,label:`Key`,control:{type:`secret`,value:zh(e.key)?``:e.key,held:zh(e.key),onChange:T(`key`)}}],...C?[{key:`model`,label:`Model`,width:`full`,control:N_(e.model,S,i,T(`model`),o)}]:[]]],D={...e},O=(S?.settings??[]).filter(e=>e.scope===`connection`&&(!e.appliesTo||e.appliesTo(D))).map(t=>b_(e,t,T(t.key))),[k]=(0,Y.useState)(()=>({address:!!e.url.trim()})),A=!S?.local&&!S?.keyless?[{key:`ci`,label:`CI secret`,hint:`Read during CI runs`,control:{type:`code`,text:K().keyVar(e.id,t)}}]:[],j=[{key:`identity`,rows:E},...O.length?[{key:`settings`,title:`Settings`,rows:O}]:[],...u?[]:[{key:`use`,title:`Use`,rows:A,children:(0,$.jsx)(ig,{uses:l})}],...S?.url&&!S.local?[{key:`advanced`,title:`Advanced`,folded:!k.address,rows:[{key:`url`,label:`Address`,width:`full`,hint:`For a proxy or gateway`,control:{type:`text`,value:e.url,placeholder:S.url,onChange:T(`url`)}}]}]:[],...S?.local||!C?[]:[{key:`debug`,title:`Debug`,rows:w,folded:!0}]],M=[{label:`Restore…`,icon:s,disabled:!c,onSelect:b},...p?[p]:[],...(S?.templates??[]).map(e=>({label:`Apply template: ${e.label}`,icon:J.preset,onSelect:()=>x(e.settings)}))];return(0,$.jsx)(De,{title:e.name||(u?R_:`Unnamed connection`),...n?{focusRow:n}:{},sections:j,menu:M,actions:S?.tests===!1?[]:[{label:`Test connection`,icon:B,busy:r?.phase===`checking`,onSelect:()=>v(e.id),outcome:r?{label:S_[r.phase],bad:r.phase===`error`,onOpen:y}:void 0}],dirty:f,adding:u,onSave:m,onClose:h})}function b_(e,t,n){let r=e[t.key]??t.default??``;return t.control===`select`&&t.choices?{key:t.key,label:t.label,control:{type:`select`,value:r,options:t.choices,onChange:n}}:{key:t.key,label:t.label,width:`full`,control:{type:`text`,value:r,onChange:n}}}function x_(e){let t=K().apiBase(e.url),n=[];try{let{url:r,body:i}=K().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:Xh(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}var S_={checking:`Checking…`,connected:`Connected`,error:`Error`},C_={untested:`Not tested`,connected:`Connected`,"key-needed":`Key needed`,unreachable:`Unreachable`},w_=(e,t)=>!t?.keyless&&!e.key&&!t?.keyOptional?.(e);function T_(e,t){let n=K().CONNECTION_TYPES[e.type];if(n?.local||n?.tests===!1)return null;let r=(e,t)=>({kind:e,label:C_[e],title:t?`${C_[e]} — ${t}`:C_[e]});return w_(e,n)?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 E_(e){let t=K().CONNECTION_TYPES[e.type];return t?.local||t?.tests===!1?!1:!w_(e,t)}function D_(e){if(!e.length)return;let t=Og(e[0]);return e.every(e=>Og(e)===t)?e[0]:void 0}var O_=(e,t,n)=>e.url.trim()||t?.url||n;function k_(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=>j_(t.settings.url,e.url));r?.settings.testPath&&(n.testPath=r.settings.testPath)}return n}function A_({test:e,onClose:t}){return(0,$.jsx)(De,{title:`Test connection`,sections:[{key:`result`,rows:[{key:`time`,label:`Time`,required:!0,control:{type:`static`,text:e.ms==null?`—`:M_(e.ms)}},{key:`result`,label:`Result`,required:!0,control:{type:`textarea`,value:e.lines.join(`
|
|
14
|
-
`)}}]}],onClose:t})}function j_(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 M_=e=>e<1e3?`${e} ms`:`${(e/1e3).toFixed(1)}s`;function N_(e,t,n,r,i){return t?.lists===!1?{type:`text`,value:e,placeholder:`model name`,onChange:r}:{type:`picker`,kind:`Model`,value:e,onChange:r,placeholder:`Pick a model`,options:(n?.ids??[]).map(e=>({id:e,label:e})),onOpen:()=>i(),loading:!n?.ids&&!n?.error,error:n?.error,onRetry:()=>i(!0),entry:{label:`Model`,placeholder:`Model name`}}}var P_=e=>JSON.stringify([e.url,e.key,e.type]);function F_(e){return K().CONNECTION_TYPES[e]?.settings.filter(e=>e.scope===`connection`).map(e=>e.key)??[]}var I_=()=>`p${Date.now().toString(36)}${Math.random().toString(36).slice(2,6)}`,L_=(e,t)=>K().uniqueSlug(K().slugOf(e),new Set(K().withSlugs(t).map(e=>e.slug))),R_=`New connection`,z_=()=>({id:I_(),name:``,slug:``,url:``,model:``,key:``,nPredict:``,maxTokens:``,type:`ollama`}),B_=[];function V_(e,t){let n=X(),[r,i]=(0,Y.useState)(null);(0,Y.useEffect)(()=>{if(t===null)return;let r=!0,a=Lr(n);return a.runDataset(e).then(n=>{if(n){r&&i({run:e,cases:K().upgradeDatasetBody(n).cases,current:!1});return}return a.dataset(t).then(t=>{r&&i({run:e,cases:K().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:B_,current:a?.current??!1}}var H_={};function U_(e){let t=X(),[n,r]=(0,Y.useState)(null);return(0,Y.useEffect)(()=>{let n=!0;return t.runGroups(e).then(t=>{if(!n)return;let i={};for(let[e,n]of Object.entries(t??{}))i[e]=K().upgradeDatasetBody(n);r({run:e,groups:i})}).catch(()=>{}),()=>{n=!1}},[t,e]),n?.run===e?n.groups:H_}var W_={};function G_(e,t){let n=X(),[r,i]=(0,Y.useState)(null),a=(0,Y.useMemo)(()=>{let t=new Set;for(let n of e?.evals??[]){let e=K().casesRef(n);e&&t.add(e.id),n.group?.id&&t.add(n.group.id)}return[...t]},[e?.evals]),o=a.join(`,`);(0,Y.useEffect)(()=>{if(!t||!e)return;let r=!0,o=Lr(n);return Promise.all(a.map(e=>o.dataset(e).then(t=>[e,K().upgradeDatasetBody(t.body)],()=>null))).then(async t=>{if(!r)return;let n={};for(let e of t)e&&(n[e[0]]=e[1]);let a=await K().rescoreResults(e.snapshot,e.results,{group:e=>n[e.id]});r&&i({run:e.id,bodies:n,results:a})}),()=>{r=!1}},[n,t,e?.id,e?.results,o]);let s=t&&e&&r?.run===e.id?r:null;return{ready:!!s,bodies:s?.bodies??W_,results:s?.results??null}}function K_(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=>K().metricLines(e.values)),forbidden:t.filter(e=>e.not&&e.type===`contains`).map(e=>({value:String(e.value??``),except:K().metricLines(e.except)}))}}var q_=e=>e==null?`—`:ba(e);function J_(e,t){return t.flatMap((t,n)=>{let r=t.replays?[]:e?.scenarios?.[n]?.res?.transcript||[];return r.filter(e=>e.sent).map(e=>({...e,target:t.name,stages:r.length,at:n}))})}function Y_(e){let t=e.trim();if(!/^[[{]/.test(t))return null;try{return{value:JSON.parse(t)}}catch{return null}}function X_({text:e}){let t=Y_(e);if(t)return(0,$.jsx)(_t,{value:t.value});let n=/^([A-Z]+ \S+)\n\n([\s\S]*)$/.exec(e),r=n?Y_(n[2]):null;return n&&r?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`pre`,{className:`tx-line`,children:n[1]}),(0,$.jsx)(_t,{value:r.value})]}):(0,$.jsx)(`pre`,{className:`mono-block tx-text`,children:e||(0,$.jsx)(`span`,{className:`unset`,children:`(Empty)`})})}function Z_({label:e,text:t,bad:n}){return(0,$.jsxs)(`div`,{className:`tx-part`,children:[(0,$.jsx)(`span`,{className:`tx-label`,children:e}),n?(0,$.jsx)(`pre`,{className:`mono-block tx-text err`,children:t}):(0,$.jsx)(X_,{text:t})]})}function Q_({stages:e,graders:t}){if(!e.length&&!t.length)return null;let n=[...new Set([...e.map(e=>e.at),...t.map(e=>e.at)])].sort((e,t)=>e-t),r=e=>[e.target,e.stages>1?`job ${e.n}`:``,q_(e.ms)].filter(Boolean).join(`, `);return(0,$.jsx)(`div`,{className:`transcript`,children:n.flatMap(n=>[...e.filter(e=>e.at===n).map(e=>{let t=e.body??e.said??e.got;return(0,$.jsxs)(`section`,{className:`tx-block`,children:[(0,$.jsx)(`h4`,{className:`tx-head`,children:r(e)}),(0,$.jsx)(Z_,{label:`Sent`,text:e.sent??``}),t!=null&&(e.body!=null||!e.error)&&(0,$.jsx)(Z_,{label:`Returned`,text:t}),e.error&&(0,$.jsx)(Z_,{label:`Error`,text:e.error,bad:!0})]},`s-${n}-${e.n}`)}),...t.filter(e=>e.at===n).map(e=>(0,$.jsxs)(`section`,{className:`tx-block`,id:e.id,tabIndex:-1,children:[(0,$.jsx)(`h4`,{className:`tx-head`,children:e.ms==null?e.head:`${e.head}, ${q_(e.ms)}`}),e.calls.map((e,t)=>(0,$.jsxs)(`div`,{className:`tx-call`,children:[(0,$.jsx)(Z_,{label:`Sent`,text:e.sent}),e.error==null?(0,$.jsx)(Z_,{label:`Returned`,text:e.got??``}):(0,$.jsx)(Z_,{label:`Error`,text:e.error,bad:!0})]},t))]},e.id))])})}function $_({text:e,kind:t}){return(0,$.jsx)(Ye,{className:`term${t?` ${t}`:``}`,children:e})}function ev({res:e,kase:t}){let n=e?.dropped||[];if(!n.length)return null;let r=e=>t?K_(t).wanted.some(t=>K().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)($_,{text:e.item,kind:`no`}),(0,$.jsxs)(`span`,{className:`note`,children:[e.by,r(e.item)&&(0,$.jsx)(`b`,{className:`fault`,children:`, wanted`})]})]},t))]})}var tv=e=>e?.transcript?.at(-1)?.kind===`text`;function nv(e){return new Set((e?.transcript||[]).map(e=>e.n)).size>1?`Job ${e.filteredBy.job}`:null}function rv({res:e}){let t=e?.filteredBy,n=t&&nv(e);return t&&!t.rejected?(0,$.jsxs)(`div`,{className:`meta`,children:[`Emptied by `,t.filter,n?`, ${n.toLowerCase()}`:``]}):null}function iv({res:e}){let t=e?.filteredBy;if(!t?.rejected)return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`span`,{className:`err`,children:e?.error}),(0,$.jsx)(rv,{res:e})]});let n=nv(e);return(0,$.jsxs)($.Fragment,{children:[(0,$.jsxs)(`span`,{className:`err`,children:[`Discarded by `,t.filter]}),n&&(0,$.jsx)(`div`,{className:`meta`,children:n})]})}function av({res:e}){let t=e?.transcript?.at(-1);return t?.filtered?(0,$.jsxs)(`details`,{className:`dropped`,children:[(0,$.jsxs)(`summary`,{children:[`Filtered by `,t.filtered.by.join(`, `)]}),(0,$.jsx)(`pre`,{children:t.got||(0,$.jsx)(`span`,{className:`unset`,children:`(Empty)`})})]}):null}function ov({res:e}){if(e?.error)return(0,$.jsx)(iv,{res:e});if(tv(e)){let t=K().gradedText(e);return(0,$.jsxs)($.Fragment,{children:[t?(0,$.jsx)(`pre`,{children:t}):(0,$.jsx)(`div`,{className:`note`,children:`(Empty response)`}),(0,$.jsx)(sv,{res:e}),(0,$.jsx)(av,{res:e})]})}return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`div`,{className:`chips`,children:(e?.terms||[]).map((e,t)=>(0,$.jsx)($_,{text:String(e)},t))}),(0,$.jsx)(ev,{res:e,kase:null}),(0,$.jsx)(`div`,{className:`meta`,children:fa((e?.terms||[]).length,`item`)}),(0,$.jsx)(sv,{res:e}),(0,$.jsx)(av,{res:e}),(0,$.jsx)(rv,{res:e})]})}function sv({res:e}){let t=e?.transcript?.at(-1)?.stopped;if(!t)return null;let n=t.limit==null?``:`${t.limit.toLocaleString(`en-US`)} `;return(0,$.jsxs)(`div`,{className:`reply-stopped`,children:[`Stopped at the `,n,`response-token limit`]})}function cv({name:e,asked:t}){return(0,$.jsxs)(`span`,{className:`rr-target`,children:[(0,$.jsxs)(`span`,{children:[e,t&&(0,$.jsx)(`span`,{className:`sr-only`,children:`,`})]}),t&&(0,$.jsx)(`span`,{className:`rr-asked`,children:t})]})}var lv=e=>e?{"data-asked":e}:{},uv=(e,t)=>`${K().targetLetter(t)} ${e.scenarios[t]?.name??``}`.trim(),dv=e=>e?`${e.passed}/${e.ran}`:(0,$.jsx)(`span`,{className:`unset`,children:`(n/a)`});function fv({id:e,view:t,pinned:n}){let r=no(t),i=t.evals.flatMap((e,n)=>{let i=r.rows[n],a={kind:`group`,key:e.id,name:i?.name??``,verdict:(i?.cells??[]).map(e=>e.pass)},o=i?.whole?[{kind:`whole`,key:`${e.id}:whole`,verdict:i.whole.map((e,t)=>i.cells[t]?.skipped?null:e)}]:[],s=co(t,e.id).map(t=>({kind:`metric`,key:`${e.id}:${t.key}`,label:t.label,cells:t.cells}));return[a,...o,...s]});return(0,$.jsx)(`div`,{className:`scrollx`,style:{"--scenarios":t.scenarios.length},children:(0,$.jsx)(W,{className:`egtable recorded`,label:`Evals Summary`,rows:i,rowKey:e=>e.key,defaultSort:null,pinned:n,rowProps:e=>e.kind===`group`?{className:`eg-group`,"data-group":e.key}:e.kind===`whole`?{className:`eg-metric`,"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.kind===`whole`?`Whole run`:e.label})},...t.scenarios.map((e,n)=>({key:`t-${n}`,header:(0,$.jsx)(cv,{name:uv(t,n),asked:t.scenarios[n]?.asked??null}),label:uv(t,n),tdProps:()=>lv(t.scenarios[n]?.asked??null),sort:null,cell:e=>e.kind===`metric`?dv(e.cells[n]):e.verdict[n]==null?(0,$.jsx)(`span`,{className:`eg-none`,children:`—`}):(0,$.jsx)(Xt,{pass:e.verdict[n]})}))]})})}function pv({view:e,x:t,evalId:n,onClose:r}){let i=e.items[t]??e.results[t]?.name??`Item`;return(0,$.jsx)(Ae,{title:i,wide:!0,onClose:r,dismiss:`Close`,children:(0,$.jsx)(`div`,{className:`rec-compare`,style:{"--scenarios":e.scenarios.length},children:e.scenarios.map((r,i)=>{let a=ao(e.results[t]?.scenarios?.[i]?.res),o=n?oo(e,t,i,n):[];return(0,$.jsxs)(`div`,{className:`rec-side`,children:[(0,$.jsx)(`h3`,{className:`rec-side-title`,children:uv(e,i)}),(0,$.jsx)(`pre`,{className:`mono-block`,children:a??`(no response)`}),o.length>0&&(0,$.jsx)(`ul`,{className:`rec-checks`,"aria-label":`${uv(e,i)} checks`,children:o.map((e,t)=>(0,$.jsxs)(`li`,{children:[(0,$.jsx)(`span`,{className:`eval-name`,children:e.label}),(0,$.jsx)(Xt,{pass:e.pass})]},t))})]},r.id)})})})}function mv({tab:e,undo:t}){let n=X(),r=Dt(),i=t??r,{setShow:a,openRun:o,closeRun:s}=$i(),{openCaseEditor:c}=lc(),[l,u]=(0,Y.useState)(null),[d,f]=(0,Y.useState)(!1),[p,m]=(0,Y.useState)(null),[g,v]=(0,Y.useState)(new Set),[y,b]=(0,Y.useState)(null);(0,Y.useEffect)(()=>{if(!y)return;let e=document.getElementById(y);e&&(Hf(e,{preventScroll:!0}),e.scrollIntoView?.({block:`nearest`,behavior:`smooth`}),b(null))},[y,g]);let x=Es(),S=Os(),[C,w]=(0,Y.useState)(null),[T,E]=(0,Y.useState)(null),[D,O]=(0,Y.useState)(0),[k,A]=(0,Y.useState)(null),[j,M]=(0,Y.useState)(null),[N,P]=(0,Y.useState)(new Set),[F,I]=(0,Y.useState)(null),[ee,L]=(0,Y.useState)(`submitted`),te=Qr();(0,Y.useEffect)(()=>{let t=!0,r=!1,i,a=async()=>{try{let o=await n.run(e.id);if(!t)return;r=!0,u(o),E(null),La(o.status)?f(!1):i=setTimeout(()=>void a(),1e3)}catch(e){if(!t)return;r?i=setTimeout(()=>void a(),1e3):E(e instanceof Ln?e.message:`Run not loaded: server unreachable.`)}};return a(),()=>{t=!1,i&&clearTimeout(i)}},[n,e.id,D]);let R=U_(e.id),ne=(0,Y.useMemo)(()=>l?po(l,e=>R[lo(e)]??R[e.id]):null,[l,R]),re=G_(ne,ee===`latest`),z=ee===`latest`&&re.ready&&!!re.results,B=(0,Y.useMemo)(()=>z&&l&&re.results?po({...l,results:re.results},e=>re.bodies[e.id]):ne,[z,l,re.results,re.bodies,ne]),V=(0,Y.useRef)(null);(0,Y.useEffect)(()=>{if(e.item==null||!B||V.current===e.last)return;V.current=e.last;let t=`${B.items[e.item]}-${e.item}`;v(e=>e.has(t)?e:new Set(e).add(t)),requestAnimationFrame(()=>{let t=document.querySelector(`#run-${CSS.escape(e.id)}-results tr[data-item="${e.item}"]`);t&&(t.tabIndex=-1,t.scrollIntoView?.({block:`center`}),Hf(t,{preventScroll:!0}))})},[e.item,e.last,e.id,B]);let H=B?K().evalsDataset(B.snapshot)?.id??null:null,ie=(0,Y.useMemo)(()=>{let e=[],t=new Set;for(let n of B?.evals??[]){let r=K().casesRef(n);r&&!t.has(r.id)&&(t.add(r.id),e.push({id:r.id,name:r.name}))}return e},[B]),U=B?ro(B):[],ae=U.some(Boolean),se=io(U),ce=ei(ae?H:null),le=V_(e.id,H),G=(0,Y.useMemo)(()=>H&&re.bodies[H]?re.bodies[H].cases:[],[re.bodies,H]),ue=(0,Y.useMemo)(()=>{let e={};for(let t of B?.evals??[]){let n=t.group?.id?re.bodies[t.group.id]:void 0;t.group&&n&&(e[lo(t.group)]=n)}return e},[re.bodies,B]),de=z?G:le.cases,fe=z?ue:R,pe=(0,Y.useMemo)(()=>new Map(de.map(e=>[K().caseItem(e),e])),[de]),me=(0,Y.useMemo)(()=>{let e=new Set;if(!z||!ne||!B)return e;let t=Ka(ne.results,ne.outcomes),n=Ka(B.results,B.outcomes);for(let r=0;r<B.items.length;r++)B.scenarios.some((e,i)=>(t[i]?.[r]??null)!==(n[i]?.[r]??null))&&e.add(r);return e},[z,ne,B]),he=(0,Y.useCallback)(async()=>{A(null);try{let{run:t}=await n.rerunRun(e.id);o(t.id,{at:t.submittedAt,from:e.from}),i.offer({label:Ch(t),restore:()=>{wh(n,t.id),s(t.id)}})}catch(e){A(`Not re-run: ${e instanceof Error?e.message:`server unreachable`}.`)}},[n,e.id,e.from,o,s,i]),ge=(0,Y.useCallback)(async()=>{f(!0);try{u((await n.cancel(e.id)).run)}catch{f(!1)}},[n,e.id]);if(!B)return(0,$.jsx)(Tt,{id:`run-${e.id}`,title:da(e.at)||`Run`,children:(0,$.jsx)(Bt,{label:`Run`,error:T,onRetry:()=>{E(null),O(e=>e+1)}})});let _e=B.progress,ve=_e.n??_e.total??0,ye=!La(B.status),be=_e.total&&ve!=null&&ye?Math.round(ve/_e.total*100):null,xe=Bo(B,de),Se=B.scenarios.map((e,t)=>Ho(B,de,t)),Ce=ye&&(d||Ra(l)),we=Eo(B,Ce),Te=B.status===`running`&&B.progress.at!=null?B.progress.at:null,Ee=e=>`${B.items[e]}-${e}`,De=(e,t,n)=>{let r=Math.max(0,B.scenarios.findIndex((t,n)=>Ja(B,e,n))),i=(t.scenarios??[])[r]?.res;i&&x(Lo(B.snapshot,n,i,r))},Oe=()=>{let e=xe.map(e=>Lo(B.snapshot,e.kase,e.res,e.scenarioIndex)).join(`
|
|
15
|
-
|
|
16
|
-
`+`-`.repeat(60)+`
|
|
17
|
-
|
|
18
|
-
`);x(e)},Ae=[S({tab:`results`,id:e.id}),...xe.length?[{label:`Copy all failures`,icon:ke,onSelect:()=>void Oe()}]:[],...l&&Sh(l)?[{label:`Re-run`,icon:Cr,onSelect:()=>void he()}]:[],...l?[{label:`Export JSON`,icon:J.export,onSelect:()=>ds(new Blob([JSON.stringify({exported:new Date().toISOString(),runs:[l]},null,2)],{type:`application/json`}),us(`run`,da(e.at)||e.id,20,`json`))}]:[],{label:`Export PDF…`,icon:yr,onSelect:()=>window.print()}],je={label:`Show`,value:e.show,onChange:t=>a(e.id,t),options:[{value:`all`,label:`All`},{value:`failed`,label:`Failed`}]},Me=La(B.status)&&!le.current&&B.evals.some(e=>!!K().casesRef(e)||!!e.group),Ne={label:`Score`,value:ee,onChange:e=>L(e),options:[{value:`submitted`,label:`Submitted`},{value:`latest`,label:`Latest eval`}]},Pe=B.evals.find(e=>K().casesRef(e))?.id??null,Fe=!!(B.content&&K().CONTENT_TYPES[B.content.type]?.inline),Ie=B.scenarios.map((e,t)=>ae?uv(B,t):e.name),Le=Ie.map((e,t)=>({name:e,replays:!!U[t]})),Re=e=>{let t=B.results[e]?.name;return t?pe.get(t):void 0},ze=(e,t)=>{let n=B.evals.find(t=>t.id===e);return n?uo(n,B.results[t]?.name,fe,de,H):[]},Be=B.scenarios.length>1&&(0,$.jsxs)(`tr`,{className:`scenstats`,children:[(0,$.jsx)(`td`,{className:`scenstats-label`,children:`Statistics`}),B.scenarios.map((e,t)=>(0,Y.createElement)(`td`,{className:`scenstat`,"data-label":Ie[t],...lv(e.asked),key:e.id},(0,$.jsx)(mi,{dense:!0,label:`${e.name} statistics`,...wo(B,t)})))]}),Ve=(e,t)=>{let n=ce?.doc?.body;if(!H||!n||se<0)return;let{body:r,taken:a}=K().takeAsExpected(n,{[e]:ao(B.results[t]?.scenarios?.[se]?.res)});if(!a.length){A(`Not taken: ${e} has no Variant B response to take.`);return}A(null),te.edit(H,()=>r),i.offer({label:`Took ${e} as expected`,restore:()=>te.edit(H,()=>n)})},He=(t,n,r,i)=>`run-${e.id}-grader-${t}-${n}-${r}-${i}`,Ue=e=>B.scenarios.flatMap((t,n)=>(B.outcomes[n]??[]).filter(e=>!e.whole).flatMap(t=>{let r=t.items[e];if(!r||K().isSkipped(r)||!r.metrics?.length)return[];let i=fo(r.metrics,ze(t.id,e));return r.metrics.flatMap((r,a)=>r.calls?.length?[{id:He(e,n,t.id,a),at:n,head:[`${r.label}${i[a]?` ${i[a]}`:``}`,Ie[n]??``].filter(Boolean).join(`, `),ms:r.asked??null,calls:r.calls}]:[])})),We=(e,t,n,r)=>{g.has(Ee(e))||v(new Set(g).add(Ee(e))),b(He(e,t,n,r))},Ge=(e,t)=>({dataset:t,resultFor:t=>B.results[e]?.scenarios?.[t]?.res??null,scenarioCount:B.scenarios.length,files:B.results.filter(e=>e.kind!==`text`).map(e=>e.name??``),onSaved:e=>P(t=>t.has(e)?t:new Set(t).add(e))}),Ke=e=>{if(ie.length>1){I(e);return}let t=ie[0]?.id;t&&c(B.results[e]?.name??``,Ge(e,t))},qe=t=>{let r=B.results[t],i=Re(t),a=!!r&&!r.unrun&&La(B.status),o=!!i||!!r?.name&&N.has(r.name),s=Ya({done:a,name:r?.name,hasCase:o,caseDatasets:ie.length}),l=[];if(s===`edit`&&H?l.push({label:`Edit eval case`,icon:J.edit,onSelect:()=>c(r.name,Ge(t,H))}):s===`add`&&l.push({label:`Add eval case`,icon:J.add,onSelect:()=>Ke(t)}),a&&ae&&se>=0&&r?.name){let e=r.name;l.push({label:`Take B as expected`,icon:tn,onSelect:()=>Ve(e,t)})}return a&&B.id&&l.push({label:`Re-run row`,icon:oe,onSelect:()=>void n.rerun(e.id,t).then(e=>u(e.run)).catch(()=>{})}),a&&i&&hv(B,t)&&l.push({label:`Copy as AI prompt`,icon:ke,onSelect:()=>void De(t,r,i)}),l};return(0,$.jsxs)(`div`,{className:`runview`,children:[(0,$.jsxs)(Tt,{id:`run-${e.id}`,title:B.name||da(e.at),actions:Ae,children:[(0,$.jsxs)(`div`,{className:`runhead`,children:[we&&(0,$.jsxs)(`span`,{className:`note run-queue`,children:[(0,$.jsx)(`span`,{className:`run-state`,children:we.state}),we.detail&&(Te==null?(0,$.jsx)(`span`,{className:`run-item`,children:we.detail}):(0,$.jsx)(`button`,{type:`button`,className:`link-btn run-item`,onClick:()=>document.querySelector(`#run-${e.id}-results tr[data-item="${Te}"]`)?.scrollIntoView({block:`nearest`}),children:we.detail}))]}),ye&&(0,$.jsx)(`button`,{type:`button`,className:`ghost stop-btn`,disabled:Ce,"aria-busy":Ce||void 0,onClick:()=>void ge(),children:Ce?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(kt,{}),`Stopping…`]}):(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(_,{size:14,"aria-hidden":`true`}),`Stop`]})})]}),ye&&(0,$.jsx)(`div`,{className:`rrbar`,children:be==null?(0,$.jsx)(`span`,{className:`rrprogress`,children:`Waiting`}):(0,$.jsxs)(`div`,{className:`rrmeter`,role:`progressbar`,"aria-valuenow":be,"aria-valuemin":0,"aria-valuemax":100,children:[(0,$.jsx)(`div`,{className:`rrbarfill`,style:{width:`${be}%`}}),(0,$.jsxs)(`span`,{className:`rrpct`,style:{"--pct":`${be}%`},children:[be,`%`]})]})}),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:da(l.rerunOfAt)}):`a cleared run`]}),B.error&&(0,$.jsx)(pt,{tone:`error`,className:`rrstatus`,children:B.error}),k&&(0,$.jsx)(pt,{tone:`error`,className:`rrstatus`,children:k}),(0,$.jsx)(`div`,{id:`run-${e.id}-stats`,className:`runstats`,children:(0,$.jsx)(mi,{label:`Run Statistics`,...So(B,Ce)})})]}),ae?(0,$.jsx)(Tt,{id:`run-${e.id}-evals`,title:`Evals Summary`,children:(0,$.jsx)(fv,{id:`run-${e.id}-evals-t`,view:B,pinned:Be})}):(0,$.jsx)(_v,{id:`run-${e.id}-evals`,summary:no(B),scenarios:B.scenarios,pinned:Be,onScenario:w}),(0,$.jsxs)(Tt,{id:`run-${e.id}-results`,title:`Results`,children:[(0,$.jsx)(xt,{noun:`results`,filters:[],quick:Me?[je,Ne]:je}),le.current&&(0,$.jsx)(`p`,{className:`note`,children:`Graded against today's evals.`}),Me&&ee===`latest`&&!re.ready&&(0,$.jsxs)(`p`,{className:`note rrscoring`,children:[(0,$.jsx)(kt,{}),` Scoring…`]}),(0,$.jsx)(`div`,{className:`scrollx`,style:{"--scenarios":B.scenarios.length},children:(0,$.jsx)(W,{className:[ae?`recorded`:``,`rrtable`].filter(Boolean).join(` `),defaultSort:null,label:`Results`,rows:B.items.map((e,t)=>t),rowKey:Ee,pinned:Se.some(e=>e.length>0)&&H&&(0,$.jsx)(gv,{from:da(e.at)||B.name,datasetId:H,failing:Se,scenarios:B.scenarios,heads:Ie,items:B.items.length}),group:B.groups.length?{of:e=>B.rowStep[e]??``,cell:(e,t)=>{let n=B.groups.find(t=>t.step===e),r=qa(B,e);if(t.key===`item`)return(0,$.jsxs)(`span`,{className:`rrband`,children:[n?.source?(0,$.jsx)(`a`,{className:`rrband-name`,href:Ti({tab:`library`,section:`sources`,id:n.source}),children:n.name}):(0,$.jsx)(`span`,{className:`rrband-name`,children:n?.name}),(0,$.jsx)(`span`,{children:fa(r.items,`item`)})]});let i=B.scenarios.findIndex((e,n)=>t.key===`scenario-${n}`),a=r.passed[i];return a?.ran?`${a.passed} of ${a.ran} passed`:null},after:t=>e.show===`failed`&&!B.rowStep.some((e,n)=>e===t&&hv(B,n))?(0,$.jsx)(`tr`,{className:`rrband-none`,children:(0,$.jsx)(`td`,{colSpan:1+B.scenarios.length,children:`(None failed)`})}):null}:void 0,rowProps:t=>({"data-item":t,"aria-busy":t===Te||void 0,className:g.has(Ee(t))?`hasdetail`:void 0,hidden:e.show===`failed`&&!hv(B,t)}),expand:{placed:!0,open:g,onChange:v,can:e=>!!J_(B.results[e],Le).length||!!Ue(e).length,label:(e,t)=>t?`Hide details`:`Show details`,detailProps:t=>({className:`rrdetail`,"data-detail":t,hidden:e.show===`failed`&&!hv(B,t)}),detail:e=>(0,$.jsx)(`div`,{className:`pane rrdetail-panel`,children:(0,$.jsx)(Q_,{stages:J_(B.results[e],Le),graders:Ue(e)})})},columns:[{key:`item`,header:Fe?`Text`:`Item`,label:``,className:`col-item`,sort:e=>B.items[e]||``,cell:(e,t,n)=>{let r=B.results[e],i=B.items[e],a=xv(B.items,e);return(0,$.jsxs)($.Fragment,{children:[r?.name&&!Fe&&Qs(r.name)?(0,$.jsx)(`button`,{type:`button`,className:`thumb`,"aria-label":`Open ${r.name}`,onClick:()=>m({name:r.name,at:e}),children:(0,$.jsx)(`img`,{src:Ao(B,r.name,e),alt:``,loading:`lazy`,"data-name":r.name})}):null,(0,$.jsxs)(`div`,{className:`rritem`,children:[ae&&r?.name?(0,$.jsx)(`button`,{type:`button`,className:`link-btn rrcall`,title:i||void 0,onClick:()=>M(e),children:i||``}):(0,$.jsx)(`div`,{className:`meta rrname`,title:i||void 0,children:i||``}),a&&(0,$.jsx)(`span`,{className:`rrloop`,children:a}),me.has(e)&&(0,$.jsx)(`span`,{className:`rrchanged`,role:`img`,"aria-label":`Verdict changed from submitted`,title:`Verdict changed from submitted`}),n,(0,$.jsx)($e,{actions:qe(e),label:`${i||`row`} actions`})]})]})}},...B.scenarios.map((e,t)=>({key:`scenario-${t}`,header:(0,$.jsx)(cv,{name:Ie[t]??e.name,asked:e.asked}),label:Ie[t]??e.name,tdProps:()=>lv(e.asked),onHeader:()=>w(t),cell:e=>(0,$.jsx)(vv,{item:B.results[e],i:t,state:Do(B,e),readings:(B.outcomes[t]??[]).filter(e=>!e.whole).map(n=>({label:n.label,dataset:n.id===Pe,score:n.items[e]??null,metrics:ze(n.id,e),open:r=>We(e,t,n.id,r)}))})}))]})})]}),C!=null&&(0,$.jsx)($t,{run:B.snapshot,i:C,onClose:()=>w(null)}),j!=null&&(0,$.jsx)(pv,{view:B,x:j,evalId:Pe,onClose:()=>M(null)}),p&&(0,$.jsx)(Cv,{src:jo(B,p.name,p.at),name:p.name,onClose:()=>m(null)}),F!=null&&(0,$.jsx)(h,{kind:`Eval`,value:``,options:ie.map(e=>({id:e.id,label:e.name})),onPick:e=>c(B.results[F]?.name??``,Ge(F,e)),onClose:()=>I(null)})]})}var hv=(e,t)=>{let n=e.results[t];return!!n&&(!!n.unrun||(n.scenarios??[]).some((n,r)=>Ja(e,t,r)||!!n?.res?.error))};function gv({from:e,datasetId:t,failing:n,scenarios:r,heads:i,items:a}){let{showCases:o}=Xi(),{list:s}=$r(),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`}),r.map((r,s)=>{let u=r.name,d=n[s]??[];return(0,$.jsx)(`td`,{className:`reasons-cell`,"data-label":i[s]??u,children:d.length===0?(0,$.jsx)(`span`,{className:`reasons-none`,children:`No failures`}):(0,$.jsx)(`ul`,{className:`reasons-list`,"aria-label":`${u} reasons`,children:d.map(n=>{let r=n.files.length,i=(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(a,1)*100)}%`},children:c?(0,$.jsx)(`span`,{className:`reason`,children:i}):(0,$.jsx)(`a`,{className:`reason`,href:`#/library/datasets`,title:`${n.label}${n.note?` (${n.note})`:``}: open ${fa(r,`eval case`)} in the Library`,"aria-label":`${n.label}: open ${fa(r,`eval case`)} in the Library`,onClick:r=>{r.preventDefault(),o(t,n.files,`${e}${l?`, ${u}`:``}, ${n.label}`)},children:i})},n.key)})})},r.id)})]})}function _v({id:e,summary:t,scenarios:n,pinned:r,onScenario:i}){let a=[...t.rows.flatMap(e=>[{kind:`group`,group:e},...e.whole?[{kind:`whole`,group:e,pass:e.whole}]:[]]),...t.overall?[{kind:`overall`,...t.overall}]:[]],o=(e,t)=>e==null&&t==null?(0,$.jsx)(`span`,{className:`eg-none`,children:`—`}):(0,$.jsxs)(`span`,{className:`eg-cell`,children:[e!=null&&(0,$.jsx)(Xt,{pass:e}),t!=null&&(0,$.jsxs)(`span`,{className:`eg-pct`,children:[t,`%`]})]});return(0,$.jsx)(Tt,{id:e,title:`Evals Summary`,children:(0,$.jsx)(`div`,{className:`scrollx`,style:{"--scenarios":n.length},children:(0,$.jsx)(W,{className:`egtable`,label:`Evals Summary`,rows:a,rowKey:e=>e.kind===`group`?e.group.key:e.kind===`whole`?`${e.group.key}:whole`:`overall`,defaultSort:null,pinned:r,rowProps:e=>e.kind===`overall`?{className:`eg-overall`,"data-group":`overall`}:e.kind===`whole`?{className:`eg-metric`,"data-group":`${e.group.key}:whole`}:{"data-group":e.group.key},columns:[{key:`group`,header:`Eval`,label:``,role:`title`,sort:null,className:`col-eg`,cell:e=>e.kind===`group`?(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(`span`,{className:`eg-name`,children:e.group.name}),e.group.version&&(0,$.jsxs)(`span`,{className:`eg-ver`,children:[` `,e.group.version]})]}):e.kind===`whole`?(0,$.jsx)(`span`,{className:`eg-metric-name`,children:`Whole run`}):(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})]})},...n.map((e,t)=>({key:`scenario-${t}`,header:(0,$.jsx)(cv,{name:e.name,asked:e.asked}),label:e.name,tdProps:()=>lv(e.asked),onHeader:()=>i(t),cell:e=>{if(e.kind===`overall`)return o(e.pass[t]??null,e.pct[t]??null);let n=e.group.cells[t];return n?n.skipped?(0,$.jsx)(`span`,{className:`eg-none`,children:`Skipped`}):e.kind===`whole`?o(e.pass[t]??null):o(n.pass,n.pct):null}}))]})})})}function vv({item:e,i:t,state:n,readings:r}){if(n===`busy`)return(0,$.jsx)(kt,{});let i=e?.scenarios?.[t]?.res;return e?.unrun?(0,$.jsx)(`span`,{className:`err`,children:e.unrun}):i?(0,$.jsxs)($.Fragment,{children:[r.map((e,t)=>e.score&&(0,$.jsx)(yv,{reading:e,score:e.score},t)),(0,$.jsx)(ov,{res:i})]}):n===`unreached`?(0,$.jsx)(`span`,{className:`note`,children:`(Not run)`}):null}function yv({reading:e,score:t}){let n=K().isSkipped(t),r=!n&&t.metrics?.length?t:null,i=r?fo(r.metrics,e.metrics):[],a=(r?.metrics??[]).map((t,n)=>({m:t,want:i[n]??null,open:t.calls?.length?()=>e.open(n):null})),o=a.filter(({m:e})=>e.weight!==0),s=a.filter(({m:e})=>e.weight===0),c=!n&&!!t.points&&t.pass,l=n?`Skipped`:t.pass?`Pass`:`Fail`;return(0,$.jsxs)(`div`,{className:`eval-block`,children:[(0,$.jsxs)(`div`,{className:`eval-head`,children:[(0,$.jsx)(`span`,{className:`eval-group`,children:e.label}),(0,$.jsx)(`span`,{className:`verdict ${l.toLowerCase()}`,children:l}),!n&&t.score!=null&&r&&(0,$.jsx)(`span`,{className:`metric-score`,children:Sv(t.score,!!t.points)})]}),o.length>0&&(0,$.jsx)(bv,{lines:o,spared:c}),s.length>0&&(0,$.jsxs)(`div`,{className:`watched`,children:[(0,$.jsx)(`b`,{children:`Watch`}),(0,$.jsx)(bv,{lines:s,spared:!1})]})]})}function bv({lines:e,spared:t}){return(0,$.jsx)(`ul`,{children:e.map(({m:e,want:n,open:r},i)=>{let a=(0,$.jsxs)($.Fragment,{children:[e.label,n&&` ${n}`]}),o=!r&&!e.pass&&!!K().METRICS[e.type]?.graded,s=t&&!e.pass;return(0,$.jsxs)(`li`,{className:e.pass?`pass`:s?`nil`:`fail`,children:[s?(0,$.jsx)(`span`,{className:`mark nil`,role:`img`,"aria-label":`No points`,children:`–`}):(0,$.jsx)(Xt,{pass:e.pass}),(0,$.jsxs)(`span`,{className:`metric-line`,children:[r?(0,$.jsx)(`button`,{type:`button`,className:`link-btn metric-open`,onClick:r,children:a}):(0,$.jsx)(`span`,{className:`metric-words`,children:a}),(r||o)&&e.reason&&(0,$.jsx)(`span`,{className:o?`metric-reason err`:`metric-reason`,children:e.reason})]})]},i)})})}function xv(e,t){let n=e[t];if(n==null)return null;let r=0,i=0;return e.forEach((e,a)=>{e===n&&(r++,a<=t&&i++)}),r>1?`${i} of ${r}`:null}var Sv=(e,t)=>{let n=Math.round(e*100)/100;return t?`${n} ${n===1?`point`:`points`}`:`${Math.round(e*100)}%`};function Cv({src:e,name:t,onClose:n}){return(0,$.jsx)(Ae,{title:t||`Image`,onClose:n,dismiss:`Close`,children:(0,$.jsx)(`img`,{className:`lightbox-img`,src:e,alt:t})})}function wv(){let{tabs:e,focusedId:n,closeRun:r,focusRun:i,goBack:a}=$i(),o=Dt(),s=(0,Y.useId)(),c=e=>`${s}-tab-${e}`,l=`${s}-panel`,u=(0,Y.useRef)(!1);(0,Y.useEffect)(()=>{u.current&&n&&(u.current=!1,document.getElementById(c(n))?.focus())});let d=e.find(e=>e.id===n);if(!d)return null;let f=(t,n)=>{let a=e.length-1,o=t.key===`ArrowRight`?n===a?0:n+1:t.key===`ArrowLeft`?n===0?a:n-1:t.key===`Home`?0:t.key===`End`?a:null;if(o!==null){t.preventDefault();let n=e[o];i(n.id),document.getElementById(c(n.id))?.focus()}else t.key===`Delete`&&(t.preventDefault(),u.current=!0,r(e[n].id))};return(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:a,"aria-label":`Back`,children:(0,$.jsx)(t,{size:18,"aria-hidden":`true`})}),(0,$.jsx)(`code`,{className:`rrcode`,children:da(d.at)||`…`}),(0,$.jsx)(`div`,{className:`rtabs`,role:`tablist`,"aria-label":`Open runs`,children:e.map((e,t)=>{let a=e.id===n,o=da(e.at)||`…`;return(0,$.jsxs)(`div`,{role:`presentation`,className:`rtab${a?` on`:``}`,children:[(0,$.jsx)(`button`,{type:`button`,role:`tab`,id:c(e.id),"aria-selected":a,"aria-controls":l,"aria-keyshortcuts":`Delete`,tabIndex:a?0:-1,className:`rname`,title:da(e.at)||`run`,onClick:()=>i(e.id),onKeyDown:e=>f(e,t),children:o}),(0,$.jsx)(`button`,{type:`button`,className:`rx`,tabIndex:a?0:-1,"aria-label":`Close run ${o}`,onClick:()=>r(e.id),children:(0,$.jsx)(_n,{size:14,"aria-hidden":`true`})})]},e.id)})})]}),(0,$.jsx)(`div`,{role:`tabpanel`,id:l,"aria-labelledby":c(d.id),children:(0,$.jsx)(mv,{tab:d,undo:o})}),(0,$.jsx)(Qt,{undo:o.undo,onUndo:o.undoNow,onDismiss:o.dismiss})]})}function Tv(){return(0,$.jsx)(Jt,{store:Qr()})}function Ev(){return(0,$.jsx)(Jt,{store:Oc()})}var Dv=`promptlab.theme`,Ov=typeof matchMedia==`function`?matchMedia(`(prefers-color-scheme: light)`):null;function kv(){try{let e=JSON.parse(localStorage.getItem(Dv)||`null`)?.choice;return e===`light`||e===`dark`?e:null}catch{return null}}var Av=()=>kv()??`system`,jv=()=>kv()??(Ov?.matches?`light`:`dark`),Mv=()=>{let e=jv();return document.documentElement.setAttribute(`data-theme`,e),e};Mv();function Nv(e){try{e===`system`?localStorage.removeItem(Dv):localStorage.setItem(Dv,JSON.stringify({choice:e}))}catch{}Mv()}function Pv(){let[e,t]=(0,Y.useState)(Av);return(0,Y.useEffect)(()=>{let e=()=>{Mv(),t(Av())};return Ov?.addEventListener?.(`change`,e),()=>Ov?.removeEventListener?.(`change`,e)},[]),[e,e=>{Nv(e),t(e)}]}var Fv=()=>{let{route:e,rewrite:t}=Ci(location.hash);return t&&history.replaceState(null,``,t),Si(e),e};function Iv(){return(0,$.jsx)(Ze,{children:(0,$.jsx)(Zr,{children:(0,$.jsx)(Dc,{children:(0,$.jsx)(oi,{children:(0,$.jsx)(Qi,{children:(0,$.jsx)(Yi,{children:(0,$.jsx)(cc,{children:(0,$.jsx)(qf,{children:(0,$.jsx)(Ts,{children:(0,$.jsx)(Lv,{})})})})})})})})})})}function Lv(){let[e,t]=(0,Y.useState)(Fv),n=(0,Y.useRef)(e);n.current=e;let{hide:r,tabs:i,focusedId:a,openRun:o,focusRun:s}=$i(),c=(0,Y.useRef)(i);c.current=i;let l=Ss(),u=X(),d=Dt();(0,Y.useEffect)(()=>{let e=()=>{let e=Fv();t(e),e.tab!==`results`&&r()};return window.addEventListener(`hashchange`,e),()=>window.removeEventListener(`hashchange`,e)},[r]);let f=e.tab===`results`?e.id:null;if((0,Y.useEffect)(()=>{if(!f)return;if(c.current.some(e=>e.id===f)){s(f);return}let e=!0;return u.run(f).then(t=>{e&&o(f,{at:t.submittedAt,from:`history`})},t=>{e&&(t instanceof Ln&&t.status===404?(Di({tab:`history`}),d.notify({label:gs(`run`,f,l.current)})):o(f,{from:`history`}))}),()=>{e=!1}},[f,u,o,s,d.notify,l.current]),(0,Y.useEffect)(()=>{let e=n.current;a&&(e.tab!==`results`||e.id!==a)&&Ei({tab:`results`,id:a})},[a]),(0,Y.useEffect)(()=>{document.title=l.current?`${l.current.name}, Evals Lab`:`No workspace, Evals Lab`},[l.current]),(0,Y.useEffect)(()=>{let e=ys();e&&d.offer({label:`Archived ${e.name}`,restore:()=>u.unarchiveWorkspace(e.id).then(()=>{location.assign(hs(e.slug))})})},[]),!l.current)return(0,$.jsx)(Vv,{ws:l});let p=e.tab===`results`?i.find(t=>t.id===e.id)?.from??`history`:e.tab,m=ss.find(e=>e.id===p),h=l.current.archived;return(0,$.jsxs)(`div`,{className:`app`,children:[(0,$.jsxs)(`header`,{className:`app-head`,children:[(0,$.jsx)(`h1`,{children:(0,$.jsx)(`a`,{className:`app-home`,href:Ti({tab:`home`}),children:`Evals Lab`})}),(0,$.jsx)(Uv,{}),(0,$.jsx)(Tv,{}),(0,$.jsx)(Ev,{}),(0,$.jsx)(Rv,{}),(0,$.jsx)(Ue,{}),(0,$.jsx)(zv,{ws:l})]}),(0,$.jsx)(Rt,{}),(0,$.jsx)(Qt,{undo:d.undo,notice:d.notice,onUndo:d.undoNow,onDismiss:d.dismiss}),h&&(0,$.jsx)(Bv,{id:l.current.id}),(0,$.jsx)(en,{label:`Lab`,className:`app-tabs`,current:p,items:ss.map(e=>({key:e.id,label:e.label,href:`#/${e.id}`,icon:e.icon}))}),(0,$.jsx)(Hv,{tab:m,route:e,readOnly:h})]})}function Rv(){let{cancel:e}=Jf(),t=Yf();return t?(0,$.jsx)(Mt,{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:()=>Ei({tab:`home`,guide:t.entry.id}),onCancel:e}):null}function zv({ws:e}){let t=X(),[n,r]=Pv(),[i,a]=(0,Y.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(hs(n.workspace.slug))}catch{}};return(0,$.jsxs)($.Fragment,{children:[(0,$.jsx)(hn,{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:()=>Ei({tab:`setup`,section:`workspaces`,id:null}),theme:n,onTheme:r}),i&&(0,$.jsx)(n_,{onCreate:e=>void s(e),onClose:()=>a(!1)})]})}function Bv({id:e}){let t=X(),[n,r]=(0,Y.useState)(!1),i=async()=>{r(!0);try{await t.unarchiveWorkspace(e),location.reload()}catch{r(!1)}};return(0,$.jsxs)(`div`,{className:`pane 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)(Wn,{size:14,"aria-hidden":`true`}),`Unarchive`]})]})}function Vv({ws:e}){let t=bs(e.list);return(0,$.jsxs)(`div`,{className:`app`,children:[(0,$.jsxs)(`header`,{className:`app-head`,children:[(0,$.jsx)(`h1`,{children:`Evals Lab`}),(0,$.jsx)(zv,{ws:e})]}),(0,$.jsxs)(`main`,{className:`ws-gone`,children:[(0,$.jsxs)(pt,{tone:`error`,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 Hv({tab:e,route:t,readOnly:n}){let{focusedId:r}=$i(),i=r?(0,$.jsx)(wv,{}):e.id===`home`?(0,$.jsx)(sp,{guide:t.tab===`home`?t.guide??null:null}):e.id===`runs`?(0,$.jsx)(th,{pipelineId:t.tab===`runs`?t.id:null}):e.id===`history`?(0,$.jsx)(Mh,{...t.tab===`history`?{section:t.section,pipeline:t.pipeline,content:t.content}:{}}):t.tab===`library`?(0,$.jsx)(Vf,{section:t.section,id:t.id}):t.tab===`setup`?(0,$.jsx)(c_,{section:t.section,id:t.id,...t.kind?{kind:t.kind}:{}}):null,a=n&&e.id!==`setup`;return(0,$.jsx)(`main`,{"data-tab":e.id,inert:a,children:i})}function Uv(){let{sync:e,resolve:t}=Ir();return(0,$.jsx)(vt,{lines:e.state===`offline`?[{key:`offline`,text:`Not saved to the server: ${e.message}. Kept in this browser.`}]:e.state===`superseded`?[{key:`superseded`,text:`Data conflict: ${e.keys.map(Gv).join(`, `)}.`,actions:[{label:`Keep this tab's data`,onSelect:()=>void t(`mine`)}]}]:[]})}var Wv={"promptlab.connections":`Connections`,"promptlab.connectionProfiles":`Connection profiles`,"promptlab.tokens":`Tokens`,"promptlab.workflows":`Experiments`,"promptlab.versions":`Change history`},Gv=e=>Wv[e]??e,Kv=`promptlab.sync`,qv=new Set([`promptlab.workflows`,`promptlab.versions`,Kv]),Jv=e=>typeof e==`object`&&!!e&&!Array.isArray(e),Yv=(e,t)=>{let n=Bh[e];return Jv(n)&&Jv(t)?{...n,...t}:t};function Xv({api:e,storage:t,server:n,workspace:r,pause:i=800}){let a=e=>r&&qv.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(Kv)??{},p={},m=new Set,h=()=>c(Kv,{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 Hh){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)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 Ln&&e.status===409){let n=e.body?.stale??{};for(let[e,t]of Object.entries(n))p[e]=t.version;let r=Object.keys(n).filter(e=>!u(e,n[e].body));for(let e of Object.keys(n))r.includes(e)||m.delete(e);for(let e of r)C[e]=l(e),c(e,n[e].body),m.delete(e);h(),m.size&&await S(t),r.length?x({state:`superseded`,keys:r}):v.state===`saving`&&x({state:`saved`});return}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,Yv(e,l(e))),s.get(e)??Bh[e]},set(e,t){c(e,t),d&&Hh.includes(e)&&(v.state===`superseded`&&(C={},x({state:`saved`})),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!==`superseded`)return;let t=v.keys;if(e===`mine`){for(let e of t)c(e,C[e]??null),m.add(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 Hh){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 Zv=e=>[1,2,3,4,5,6,7,8,9,10].includes(e?.version)||K().fatProfileRef(e),Qv=e=>e?.version===1||e?.version===2;function $v(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(Qv).map(e=>e?.tests?.type===`graded`?e.tests.dataset?.id:void 0);return[...new Set(t.filter(e=>!!e))]}function ey(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(e=>e?.version===17).map(e=>{let t=K().contentOf(e);return t?.type===`source`&&t.live!==!0&&t.pin==null?t.ref.id:void 0});return[...new Set(t.filter(e=>!!e))]}function ty(e,t={},n){let r=K().answersOf(e.get(`promptlab.connections`).list,e.get(`promptlab.connectionProfiles`).list),i={rulesFor:e=>t[e]??null,profileType:e=>r.connection(e)?.type,connectionSplit:r.split,...n?{pinFor:n}:{}},a=e=>K().upgradePipeline(e,i),o=e=>Zv(e)||a(e)!==e,s=e.get(`promptlab.workflows`);s.list.some(e=>o(e.work))&&e.set(`promptlab.workflows`,{...s,list:s.list.map(e=>({...e,work:a(e.work)}))});let c=e.get(`promptlab.versions`),l=c.pipeline??{};Object.values(l).some(e=>e.some(e=>o(e.doc)))&&e.set(`promptlab.versions`,{...c,pipeline:Object.fromEntries(Object.entries(l).map(([e,t])=>[e,t.map(e=>({...e,doc:a(e.doc)}))]))})}function ny(e,t){let n=e=>t.list.find(t=>t.fromProfile===e)??null;return{...e,list:e.list.map(e=>K().mergeAnswer(e,n(e.id)))}}function ry(e,t){let n=K(),r=new Map(e.list.map(e=>[e.id,n.splitProfile(e)])),i=e=>{let{id:t,name:n,fromProfile:i,...a}=r.get(e)?.profile??{};return a},a=[],o=new Set;for(let n of t.list){if(!n.fromProfile){a.push(n);continue}let t=e.list.find(e=>e.id===n.fromProfile);t&&(o.add(n.fromProfile),a.push({id:n.id,name:t.name,fromProfile:n.fromProfile,...n.connection?{connection:n.connection}:{},...i(t.id)}))}for(let t of e.list){let e=r.get(t.id)?.profile;!o.has(t.id)&&e&&a.push(e)}let{list:s,split:c,...l}=e;return{connections:{...l,list:e.list.map(e=>r.get(e.id).connection)},connectionProfiles:{list:a}}}var iy=e=>ny(e.get(`promptlab.connections`),e.get(`promptlab.connectionProfiles`));function ay(e,t){let n=e.get(`promptlab.connectionProfiles`),r=ry(t,n);e.set(`promptlab.connections`,r.connections),JSON.stringify(r.connectionProfiles)!==JSON.stringify(n)&&e.set(`promptlab.connectionProfiles`,r.connectionProfiles)}function oy(e,t){let n=K();if(t)try{let r=JSON.parse(t);Array.isArray(r?.list)&&ay(e,{list:r.list.map(e=>n.pinReplyTokens(`profile`in e?n.convertProfile(e):e)),at:r.at??``,...r.grader?{grader:r.grader}:{}})}catch{}let r=iy(e),i=e=>n.splitOllama(`profile`in e?n.convertProfile(e):e);r.list.some(e=>i(e)!==e)&&ay(e,{...r,list:r.list.map(i)});let a=e.get(`promptlab.connections`),o=K().withSlugs(a.list);o!==a.list&&e.set(`promptlab.connections`,{...a,list:o})}async function sy(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 cy(){let e=location.pathname.match(/^\/w\/([^/]+)/);return e?decodeURIComponent(e[1]):null}async function ly(){if(K(),new URLSearchParams(location.search).has(`resetGuides`)){try{localStorage.removeItem(`promptlab.guidesDismissed`)}catch{}history.replaceState(history.state,``,location.pathname+location.hash)}let e=cy(),t=Hn(void 0,e?`/w/${encodeURIComponent(e)}`:``),n=()=>{},r=new Promise(e=>{n=e});if(/^#\/?history\/?$/.test(location.hash)&&t.askQueue({limit:25},r),yu())try{let e=(await t.config()).microsoft;e&&await bu(e)}catch{}if(wu())try{let e=(await t.config()).microsoft;if(e){await Tu(e);return}}catch{}await In(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(hs(e))}};document.title=s?`${s.name}, Evals Lab`:`No workspace, Evals Lab`;let l=await sy(t),u=Xv({api:t,storage:localStorage,server:l,workspace:s?.slug??a});oy(u,l||localStorage.getItem(`promptlab.connections`)?null:localStorage.getItem(`promptlab.profiles`));let d={};if($v(u).length)try{d=(await t.archivedRules()).rules}catch{}let f=new Map,p=ey(u);if(p.length)try{let{sources:e}=await t.sources();await Promise.all(e.filter(e=>p.includes(e.id)&&e.type===`dynamic`&&K().DYNAMIC_PROVIDERS[e.provider??``]?.tier!==`supplied`).map(async e=>{let{responses:n}=await t.sourceResponses(e.id);f.set(e.id,{holds:e.files>0,pin:Gd(n)})}))}catch{}ty(u,d,(e,t)=>{let n=f.get(e);return n&&(t??!n.holds)===!1?n.pin:void 0}),addEventListener(`pagehide`,()=>{u.flush(!0)}),(0,En.createRoot)(document.getElementById(`root`)).render((0,$.jsx)(Y.StrictMode,{children:(0,$.jsx)(Pr.Provider,{value:u,children:(0,$.jsx)(Dn.Provider,{value:t,children:(0,$.jsx)(xs.Provider,{value:c,children:(0,$.jsx)(Iv,{})})})})}))}ly();
|