pico-engine 1.9.0 → 1.9.1
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/README.md +2 -2
- package/krl/io.picolabs.pico-engine-ui.krl +2 -0
- package/krl/io.picolabs.wrangler.krl +7 -1
- package/krl/io.picolabs.xmlui.krl +13 -3
- package/mcp/devResources.js +17 -7
- package/mcp/docs/CLAUDE.md +85 -0
- package/{public → mcp}/docs/pico-engine.md +8 -8
- package/package.json +5 -4
- package/public/pico-engine-ui.js +2 -2
- package/public/.DS_Store +0 -0
- package/public/docs/README.md +0 -9
- package/public/docs/_media/logo.png +0 -0
- package/public/docs/_sidebar.md +0 -7
- package/public/docs/contributing.md +0 -64
- package/public/docs/favicon.ico +0 -0
- package/public/docs/index.html +0 -29
- package/public/docs/krl/README.md +0 -3
- package/public/docs/krl/_sidebar.md +0 -10
- package/public/docs/krl/discovery.md +0 -144
- package/public/docs/krl/modules/ctx.md +0 -151
- package/public/docs/krl/modules/event.md +0 -70
- package/public/docs/krl/modules/http.md +0 -42
- package/public/docs/krl/modules/math.md +0 -5
- package/public/docs/krl/modules/schedule.md +0 -5
- package/public/docs/krl/modules.md +0 -11
- package/public/docs/krl/rulesets.md +0 -81
- package/public/docs/krl/stdlib.md +0 -3
- package/public/docs/llms-full.txt +0 -630
- package/public/docs/llms.txt +0 -38
- package/public/docs/openapi.yaml +0 -78
- package/public/docs/pico-views-xmlui.md +0 -5
- package/public/docs/pico-views.md +0 -140
- package/public/docs/pico.md +0 -5
- package/public/docs/rulesets/README.md +0 -26
- package/public/docs/rulesets/io.picolabs.oauth.md +0 -71
- package/public/docs/rulesets/io.picolabs.pds.md +0 -327
- package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +0 -65
- package/public/docs/rulesets/io.picolabs.portable_subtree.md +0 -94
- package/public/docs/rulesets/io.picolabs.subscription.md +0 -398
- package/public/docs/rulesets/io.picolabs.wrangler.md +0 -471
- /package/{public → mcp}/docs/pico-overview.md +0 -0
package/public/pico-engine-ui.js
CHANGED
|
@@ -73,7 +73,7 @@ Error generating stack: `+i.message+`
|
|
|
73
73
|
`)}function zu(e){return e.join(", ")}const qf=({error:e,line:t})=>{const n=(e+"").replace(/^\w*Error:/i,"").trim();return s.jsxs("div",{children:[s.jsx("span",{className:"text-danger",children:n}),s.jsx("span",{className:"text-muted ml-1 mr-1",children:"Line:"}),s.jsx("span",{className:"text-mono",children:t})]})},pa=({src:e})=>s.jsx("div",{children:e.split(`
|
|
74
74
|
`).map((t,n)=>{try{const r=Vf(t);if(r)return s.jsx(so,{rule:r.rule,isAllow:r.kind==="allow"},n)}catch(r){return s.jsx(qf,{error:r,line:t},n)}})}),ga=({src:e})=>s.jsx("div",{children:e.split(`
|
|
75
75
|
`).map((t,n)=>{try{const r=Hf(t);if(r)return s.jsx(io,{rule:r.rule,isAllow:r.kind==="allow"},n)}catch(r){return s.jsx(qf,{error:r,line:t},n)}})}),Kf=({policy:e})=>s.jsxs("div",{children:[e.allow.map((t,n)=>s.jsx(so,{rule:t,isAllow:!0},n)),e.deny.map((t,n)=>s.jsx(so,{rule:t},n))]}),Qf=({policy:e})=>s.jsxs("div",{children:[e.allow.map((t,n)=>s.jsx(io,{rule:t,isAllow:!0},n)),e.deny.map((t,n)=>s.jsx(io,{rule:t},n))]}),so=({isAllow:e,rule:t})=>s.jsxs("div",{className:"text-mono",children:[e?s.jsx("span",{className:"badge badge-success mr-2",children:"allow"}):s.jsx("span",{className:"badge badge-danger mr-2",children:"deny"}),t.domain,s.jsx("b",{children:" : "}),t.name]}),io=({isAllow:e,rule:t})=>s.jsxs("div",{className:"text-mono",children:[e?s.jsx("span",{className:"badge badge-success mr-2",children:"allow"}):s.jsx("span",{className:"badge badge-danger mr-2",children:"deny"}),t.rid,s.jsx("b",{children:" / "}),t.name]}),Ue=e=>e.error?s.jsx("span",{className:"text-danger",children:e.error}):s.jsx("span",{}),oo=12,wy=/^[a-zA-Z0-9_-]+$/;function Bu(){return crypto.randomUUID().replace(/-/g,"")}function Wu(e){const t=e.trim();return t.length===0?"Channel id cannot be empty.":t.length<oo?`Channel id must be at least ${oo} characters.`:wy.test(t)?null:"Channel id may only contain letters, digits, hyphen, and underscore."}async function At(e){const t=await e.text();return t===""?null:JSON.parse(t)}async function jy(){const e=await fetch("/api/oauth/mesh-enabled",{credentials:"include"});if(!e.ok)return!1;const t=await At(e);return!!(t&&t.meshOAuthEnabled)}async function Ny(e){const t=await fetch(`/oauth/channels/${encodeURIComponent(e)}/status`,{credentials:"include"}),n=await At(t);if(!t.ok)throw new Error(n&&n.error||t.statusText);return n}async function Sy(e){const t=await fetch(`/oauth/channels/${encodeURIComponent(e)}/credentials`,{method:"POST",credentials:"include"}),n=await At(t);if(!t.ok)throw new Error(n&&(n.error_description||n.error)||t.statusText);return n}async function ky(e){const t=await fetch(`/oauth/channels/${encodeURIComponent(e)}/credentials`,{method:"DELETE",credentials:"include"}),n=await At(t);if(!t.ok)throw new Error(n&&(n.error_description||n.error)||t.statusText)}async function by(e){const t=await fetch(`/oauth/channels/${encodeURIComponent(e)}/tokens`,{method:"DELETE",credentials:"include"}),n=await At(t);if(!t.ok)throw new Error(n&&(n.error_description||n.error)||t.statusText)}async function Ey(e,t,n){const r={grant_type:"client_credentials",client_id:e,client_secret:t};n!==void 0&&(r.expires_in=n);const l=await fetch("/oauth/token",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)}),i=await At(l);if(!l.ok)throw new Error(i&&(i.error_description||i.error)||l.statusText);return i}async function Cy(){const e=await fetch("/oauth/apps",{credentials:"include"}),t=await At(e);if(!e.ok)throw new Error(t&&(t.error_description||t.error)||e.statusText);return t&&t.apps||[]}async function _y(e){const t=await fetch("/oauth/apps",{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),n=await At(t);if(!t.ok)throw new Error(n&&(n.error_description||n.error)||t.statusText);return n}async function Ry(e){const t=await fetch(`/oauth/apps/${encodeURIComponent(e)}`,{method:"DELETE",credentials:"include"}),n=await At(t);if(!t.ok)throw new Error(n&&(n.error_description||n.error)||t.statusText)}const Vu="mesh-oauth-exempt",Py=[{label:"5 minutes",seconds:5*60},{label:"1 hour",seconds:60*60},{label:"6 hours",seconds:6*60*60},{label:"24 hours",seconds:24*60*60},{label:"7 days",seconds:7*24*60*60},{label:"30 days",seconds:30*24*60*60},{label:"90 days",seconds:90*24*60*60},{label:"Never",seconds:0}],Ty=24*60*60,Jf=43,Oy=Jf+5,Ly=4+Jf,$y=Ly+3;function Iy(e){return e===void 0?"never":e<60*60?`${Math.round(e/60)}m`:e<24*60*60?`${Math.round(e/(60*60))}h`:`${Math.round(e/(24*60*60))}d`}function Ay({channelId:e}){const[t,n]=p.useState(null),[r,l]=p.useState(!0),[i,o]=p.useState(!1),[a,u]=p.useState(null),[c,m]=p.useState(null),[d,g]=p.useState(""),[N,x]=p.useState(Ty),[v,k]=p.useState(null),[y,f]=p.useState(!1),[h,w]=p.useState(null),b=p.useCallback(async()=>{l(!0),u(null);try{n(await Ny(e))}catch(U){u(U+"")}finally{l(!1)}},[e]);p.useEffect(()=>{b()},[b]),p.useEffect(()=>{c!=null&&c.client_secret&&(g(c.client_secret),k(null))},[c]);async function j(){o(!0),u(null);try{const U=await Sy(e);m(U),await b()}catch(U){u(U+"")}finally{o(!1)}}async function E(){if(window.confirm("Revoke webhook credentials for this channel?")){o(!0),u(null);try{await ky(e),m(null),g(""),k(null),await b()}catch(U){u(U+"")}finally{o(!1)}}}async function _(U){U.preventDefault();const te=d.trim();if(te){f(!0),w(null);try{k(await Ey((t==null?void 0:t.clientId)||e,te,N))}catch(I){k(null),w(I+"")}finally{f(!1)}}}async function O(){if(v!=null&&v.access_token)try{await navigator.clipboard.writeText(v.access_token)}catch{}}async function L(){o(!0),u(null);try{await by(e),k(null),await b()}catch(U){u(U+"")}finally{o(!1)}}return r?s.jsx("div",{className:"text-muted small mt-2",children:"Loading webhook auth…"}):t!=null&&t.eligible?s.jsxs("div",{className:"border rounded p-2 mt-2 mb-2",children:[s.jsx("div",{className:"font-weight-bold small",children:"Webhook OAuth"}),s.jsxs("p",{className:"text-muted small mb-2",children:["This channel requires a bearer token on every request. Create credentials, then mint an access token below (or call ",s.jsx("code",{children:"POST /oauth/token"}),")."]}),t.hasSecret?"":s.jsx("div",{className:"alert alert-warning py-2 small mb-2",children:"No credentials yet — external access is blocked until you create them."}),s.jsxs("div",{className:"small text-mono mb-1",children:["client_id: ",t.clientId||e]}),t.hasSecret?s.jsxs("div",{className:"text-muted small mb-2",children:["Credentials active",t.createdAt?` (since ${new Date(t.createdAt).toLocaleString()})`:""]}):s.jsx("div",{className:"text-muted small mb-2",children:"No credentials yet."}),c?s.jsxs("div",{className:"alert alert-warning py-2 small",children:[s.jsxs("div",{children:[s.jsx("strong",{children:"client_secret"})," (copy now — shown once):"]}),s.jsx("div",{className:"text-mono",children:c.client_secret})]}):"",s.jsxs("div",{className:"btn-group btn-group-sm",children:[s.jsx("button",{type:"button",className:"btn btn-outline-primary",disabled:i,onClick:j,children:t.hasSecret?"Rotate credentials":"Create credentials"}),t.hasSecret?s.jsxs(s.Fragment,{children:[s.jsx("button",{type:"button",className:"btn btn-outline-secondary",disabled:i,onClick:L,children:"Revoke tokens"}),s.jsx("button",{type:"button",className:"btn btn-outline-danger",disabled:i,onClick:E,children:"Revoke credentials"})]}):""]}),t.hasSecret?s.jsxs("form",{className:"mt-3",onSubmit:_,children:[s.jsx("div",{className:"font-weight-bold small mb-1",children:"Get access token"}),s.jsx("div",{className:"form-group mb-2",children:s.jsxs("div",{className:"d-flex align-items-end flex-wrap",children:[s.jsxs("div",{className:"mr-3",children:[s.jsx("label",{htmlFor:`oauth-secret-${e}`,className:"small mb-1",children:"client_secret"}),s.jsx("input",{id:`oauth-secret-${e}`,type:"password",className:"form-control form-control-sm text-mono",style:{width:`${Oy}ch`,maxWidth:"100%"},value:d,onChange:U=>{g(U.target.value),k(null)},placeholder:"Paste your client secret",disabled:y||i,autoComplete:"off"})]}),s.jsxs("div",{className:"flex-shrink-0",children:[s.jsx("label",{htmlFor:`oauth-expiry-${e}`,className:"small mb-1 text-nowrap",children:"Token lifetime"}),s.jsx("select",{id:`oauth-expiry-${e}`,className:"form-control form-control-sm w-auto",value:N,onChange:U=>{x(Number(U.target.value)),k(null)},disabled:y||i,children:Py.map(U=>s.jsx("option",{value:U.seconds,children:U.label},U.seconds))})]})]})}),s.jsx("button",{type:"submit",className:"btn btn-sm btn-outline-primary",disabled:y||i||!d.trim(),children:y?"Minting…":"Get token"}),v?s.jsxs("div",{className:"mt-2",children:[s.jsxs("label",{htmlFor:`oauth-token-${e}`,className:"small mb-1",children:["access_token (",v.token_type,v.expires_in===void 0?", never expires":`, expires in ${Iy(v.expires_in)}`,")"]}),s.jsxs("div",{className:"input-group input-group-sm",style:{width:"fit-content",maxWidth:"100%"},children:[s.jsx("input",{id:`oauth-token-${e}`,className:"form-control text-mono",style:{width:`${$y}ch`,flex:"0 0 auto"},value:v.access_token,readOnly:!0}),s.jsx("div",{className:"input-group-append",children:s.jsx("button",{type:"button",className:"btn btn-outline-secondary",onClick:O,children:"Copy"})})]})]}):"",h?s.jsx("div",{className:"text-danger small mt-2",children:h}):""]}):"",a?s.jsx("div",{className:"text-danger small mt-2",children:a}):""]}):t!=null&&t.reason?s.jsx("div",{className:"text-muted small mt-2",children:t.reason}):null}function Dy({channel:e,disabled:t,onSave:n}){const[r,l]=p.useState(()=>zu(e.tags)),[i,o]=p.useState(()=>ro(e.eventPolicy)),[a,u]=p.useState(()=>lo(e.queryPolicy));p.useEffect(()=>{l(zu(e.tags)),o(ro(e.eventPolicy)),u(lo(e.queryPolicy))},[e]);function c(x){const v=r.split(",").map(k=>k.trim()).filter(Boolean);v.some(k=>k.toLowerCase()===x.toLowerCase())||l(v.length===0?x:`${v.join(", ")}, ${x}`)}function m(){return{eci:e.id,tags:r.split(",").map(x=>x.trim()).filter(Boolean),eventPolicy:ma(i),queryPolicy:ha(a)}}function d(){try{return m(),!0}catch{return!1}}const g=r.split(",").map(x=>x.trim()).filter(Boolean),N=e.tags.includes("oauth-webhook")||g.some(x=>x.toLowerCase()==="oauth-webhook");return s.jsxs("div",{className:"mb-3",children:[s.jsx("h5",{className:"h6",children:"Edit channel"}),s.jsxs("form",{onSubmit:x=>{x.preventDefault(),d()&&n(m())},children:[s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:`edit-chann-tags-${e.id}`,children:"Tags"}),s.jsxs("div",{className:"text-muted small mb-1",children:["Add"," ",s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 align-baseline text-mono",onClick:()=>c("oauth-webhook"),disabled:t,children:"oauth-webhook"})," ","for webhook Client Credentials."]}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("input",{id:`edit-chann-tags-${e.id}`,type:"text",className:"form-control form-control-sm",value:r,onChange:x=>l(x.target.value),disabled:t})}),s.jsx("div",{className:"col",children:g.map((x,v)=>s.jsx("span",{className:"badge badge-secondary ml-1",children:x},v))})]})]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:`edit-chann-event-${e.id}`,children:"Event Policy"}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("textarea",{id:`edit-chann-event-${e.id}`,rows:3,className:"form-control form-control-sm",value:i,onChange:x=>o(x.target.value),disabled:t})}),s.jsx("div",{className:"col",children:s.jsx(pa,{src:i})})]})]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:`edit-chann-query-${e.id}`,children:"Query Policy"}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("textarea",{id:`edit-chann-query-${e.id}`,rows:3,className:"form-control form-control-sm",value:a,onChange:x=>u(x.target.value),disabled:t})}),s.jsx("div",{className:"col",children:s.jsx(ga,{src:a})})]})]}),s.jsx("button",{type:"submit",className:"btn btn-sm btn-outline-primary",disabled:t||!d(),children:"Save changes"})]}),N?s.jsx("div",{className:"mt-3",children:s.jsx(Ay,{channelId:e.id})}):""]})}const My=({pico:e})=>{const[t]=ca(),n=t.get("focus")||"",[r,l]=p.useState({}),[i,o]=p.useState(""),[a,u]=p.useState(()=>Bu()),[c,m]=p.useState("allow *:*"),[d,g]=p.useState("allow */*");function N(){const O=Wu(a);if(O)throw new Error(O);return{id:a.trim(),tags:i.split(","),eventPolicy:ma(c),queryPolicy:ha(d)}}function x(){try{N()}catch{return!1}return!0}const v=nn(null,()=>ye(`/c/${e.eci}/query/io.picolabs.pico-engine-ui/pico`)),[k,y]=p.useState(!1);p.useEffect(()=>{jy().then(y)},[e.eci]);const f=Rt(({eci:O,data:L})=>q(`/c/${O}/event/engine_ui/new_channel/query/io.picolabs.pico-engine-ui/pico`,L).then(U=>{v.setData(U),u(Bu())})),h=Rt(O=>q(`/c/${e.eci}/event/engine_ui/del_channel/query/io.picolabs.pico-engine-ui/pico`,{eci:O}).then(L=>v.setData(L))),w=Rt(O=>q(`/c/${e.eci}/event/engine_ui/update_channel/query/io.picolabs.pico-engine-ui/pico`,O).then(L=>v.setData(L)));p.useEffect(()=>{v.load()},[e.eci]),p.useEffect(()=>{n&&l({[n]:!0})},[n,e.eci]);const b=v.waiting||h.waiting||f.waiting||w.waiting,j=v.data&&v.data.channels||[],E=O=>{O.preventDefault();const L=document.createRange();L.selectNodeContents(O.target);const U=window.getSelection();U&&(U.removeAllRanges(),U.addRange(L))};function _(O){const L=i.split(",").map(U=>U.trim()).filter(Boolean);L.some(U=>U.toLowerCase()===O.toLowerCase())||o(L.length===0?O:`${L.join(", ")}, ${O}`)}return s.jsxs("div",{children:[s.jsx(nr,{title:"Channels",waiting:b,onRefresh:()=>v.load()}),s.jsx(Ue,{error:v.error}),s.jsx(Ue,{error:h.error}),s.jsx(Ue,{error:w.error}),j.length===0?s.jsx("div",{className:"text-muted",children:"- no channels -"}):j.map(O=>{const L=!!r[O.id],U=!O.familyChannelPicoID&&!O.tags.includes("system"),te=U;return s.jsxs("div",{children:[s.jsx("div",{children:s.jsxs("div",{className:"form-check",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:`chann-${O.id}`,onChange:I=>{const ie=O.id,Z=Object.assign({},r);I.target.checked?Z[ie]=!0:delete Z[ie],l(Z)},checked:L}),s.jsx("label",{className:"form-check-label",htmlFor:`chann-${O.id}`,children:s.jsx("span",{className:"text-mono",onDoubleClick:I=>E(I),onClick:I=>{I.preventDefault()},children:O.id})}),O.tags.map((I,ie)=>s.jsx("span",{className:"badge badge-secondary ml-1",children:I},ie)),U&&s.jsx("button",{className:"btn btn-link btn-sm",type:"button",onClick:I=>{I.preventDefault(),h.act(O.id)},disabled:b,children:"delete"})]})}),L?s.jsx("div",{className:"ml-3 mb-3",children:O.familyChannelPicoID?s.jsx("div",{className:"text-muted",children:"This is a family channel."}):te?s.jsx(Dy,{channel:O,disabled:b,onSave:I=>w.act(I)}):s.jsxs("div",{className:"row",children:[s.jsxs("div",{className:"col",children:["Event Policy",s.jsx(Kf,{policy:O.eventPolicy})]}),s.jsxs("div",{className:"col",children:["Query Policy",s.jsx(Qf,{policy:O.queryPolicy})]})]})}):""]},O.id)}),s.jsx("hr",{}),s.jsx("h4",{children:"New Channel"}),s.jsxs("form",{onSubmit:O=>{if(O.preventDefault(),!x())return;const L=N();f.act({eci:e.eci,data:L})},children:[s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:"new-chann-id",children:"Channel id (ECI)"}),s.jsxs("div",{className:"text-muted small mb-1",children:["Choose once at creation; cannot be changed later. At least"," ",oo," characters; letters, digits, hyphen, and underscore only. Must be unique across this engine."]}),s.jsx("input",{id:"new-chann-id",type:"text",className:"form-control text-mono",value:a,onChange:O=>u(O.target.value),autoComplete:"off",spellCheck:!1}),(()=>{const O=Wu(a);return O?s.jsx("div",{className:"text-danger small mt-1",children:O}):null})()]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:"new-chann-tags",children:"Tags"}),s.jsxs("div",{className:"text-muted small mb-1",children:["Add"," ",s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 align-baseline text-mono",onClick:()=>_("oauth-webhook"),children:"oauth-webhook"})," ","for webhook Client Credentials.",k?s.jsxs(s.Fragment,{children:[" ","Add"," ",s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 align-baseline text-mono",onClick:()=>_(Vu),children:Vu})," ","to allow unauthenticated ",s.jsx("code",{children:"/sky/*"})," on this channel (e.g. LoRa ingress) while mesh OAuth is enabled."]}):""]}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("input",{id:"new-chann-tags",type:"text",className:"form-control",value:i,onChange:O=>o(O.target.value)})}),s.jsx("div",{className:"col",children:i.split(",").map((O,L)=>s.jsx("span",{className:"badge badge-secondary ml-1",children:O.trim()},L))})]})]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:"new-chann-event-policy",children:"Event Policy"}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("textarea",{id:"new-chann-event-policy",rows:3,className:"form-control",value:c,onChange:O=>m(O.target.value)})}),s.jsx("div",{className:"col",children:s.jsx(pa,{src:c})})]})]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:"new-chann-query-policy",children:"Query Policy"}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("textarea",{id:"new-chann-query-policy",rows:3,className:"form-control",value:d,onChange:O=>g(O.target.value)})}),s.jsx("div",{className:"col",children:s.jsx(ga,{src:d})})]})]}),s.jsx("button",{type:"submit",className:"btn btn-outline-primary",disabled:b||!x(),children:"Add"}),s.jsx(Ue,{error:f.error})]})]})},br=32,Hu=80;function Fy(e,t,n,r){if(!e)return null;try{const l=localStorage.getItem(`${e}-ratio`);if(l){const o=Number(l);if(Number.isFinite(o)&&o>0&&o<1&&t>0){const a=t-br;return Math.max(n,Math.min(a-n,a*o))}}const i=localStorage.getItem(e);if(i){const o=Number(i);if(Number.isFinite(o)&&o>0)return o}}catch{}if(t>0){const l=t-br;return Math.max(n,Math.min(l-n,l*r))}return null}const Yf=({left:e,right:t,storageKey:n,defaultLeftRatio:r=.5,minPanePx:l=180})=>{const i=p.useRef(null),[o,a]=p.useState(null),[u,c]=p.useState(!1),m=p.useRef(o);p.useEffect(()=>{m.current=o},[o]),p.useEffect(()=>{if(!u)return;const N=v=>{const k=i.current;if(!k)return;const y=k.getBoundingClientRect(),f=y.width-br-l,h=Math.min(f,Math.max(l,v.clientX-y.left));m.current=h,a(h)},x=()=>{if(c(!1),n&&m.current!=null)try{localStorage.setItem(n,String(Math.round(m.current)));const v=i.current;if(v){const k=v.getBoundingClientRect().width-br;k>0&&localStorage.setItem(`${n}-ratio`,String(m.current/k))}}catch{}};return document.addEventListener("mousemove",N),document.addEventListener("mouseup",x),()=>{document.removeEventListener("mousemove",N),document.removeEventListener("mouseup",x)}},[u,l,n]),p.useEffect(()=>{const N=i.current;if(!N||o==null)return;const x=()=>{const k=N.getBoundingClientRect();if(k.width<Hu)return;const y=k.width-br-l;if(m.current!=null&&m.current>y){const f=Math.max(l,y);m.current=f,a(f)}},v=new ResizeObserver(x);return v.observe(N),()=>v.disconnect()},[o,l]),p.useEffect(()=>{if(o!=null||!i.current)return;const N=i.current,x=()=>{const k=N.getBoundingClientRect().width;if(k<Hu)return;const y=Fy(n,k,l,r);y!=null&&a(y)};x();const v=new ResizeObserver(x);return v.observe(N),()=>v.disconnect()},[r,o,l,n]);const d=o!=null?{width:o,flexShrink:0}:{flex:1,minWidth:l},g=o!=null?{flex:1,minWidth:l}:{flex:1,minWidth:l};return s.jsxs("div",{ref:i,className:"split-pane d-flex align-items-stretch"+(u?" split-pane-dragging":""),children:[s.jsx("div",{className:"split-pane-left overflow-auto",style:d,children:e}),s.jsx("div",{className:"split-pane-handle",role:"separator","aria-orientation":"vertical","aria-label":"Resize columns",onMouseDown:N=>{N.preventDefault(),c(!0)}}),s.jsx("div",{className:"split-pane-right overflow-auto",style:g,children:t})]})},ao=[{rid:"io.picolabs.wrangler",label:"Wrangler"},{rid:"io.picolabs.pds",label:"PDS"},{rid:"io.picolabs.subscription",label:"Subscription"},{rid:"io.picolabs.pico-engine-ui",label:"Developer UI"}],Uy={text:"",errors:!1,klog:!1,live:!0,ruleset:"",event:"",showTesting:!1,showRulesets:{}},zy=["testing_history_record","testing_history_clear","testing_saved_upsert","testing_saved_delete","testing_eci"];async function By(e){return ye(`/c/${e}/query/io.picolabs.pico-engine-ui/logs`)}function Xf(e,t){return!t||!ao.some(n=>n.rid===t)?!0:!!e.showRulesets[t]}function ya(e,t){var n;return e.type==="in"||e.type==="done"||e.type==="error"?!0:Xf(t,e.rid||((n=e.query)==null?void 0:n.rid))}function Gf(e){var n;const t=(n=Pt(e))==null?void 0:n.query;return e.kind==="query"&&(t==null?void 0:t.rid)==="io.picolabs.pico-engine-ui"&&t.name==="logs"}function Wy(e){if(!e)return"";const t=new Date(e);return Number.isNaN(t.getTime())?e:t.toLocaleTimeString([],{hour12:!1,hour:"2-digit",minute:"2-digit",second:"2-digit"})}function Pt(e){return(e.steps||[]).find(t=>t.type==="in")}function wt(e){return e?`${e.domain}:${e.name}`:""}function va(e){return e?`${e.rid||""}/${e.name||""}`:""}function Zf(e){if(!e)return"";const t=e.rule,n=t==="default"?"default":t==="family"?"family channel":t==="engine"?"engine policy":t&&typeof t=="object"?t.domain?`${t.domain}:${t.name}`:`${t.rid}/${t.name}`:"";return n?e.allowed?`Allowed by ${n}`:`Denied by ${n}`:""}function uo(e){return e==="not_fired"?"not fired":e==="not_selected"?"not selected":e||""}function em(e){switch(e.type){case"in":return e.event?`EVENT ${wt(e.event)}`:e.query?`QUERY ${va(e.query)}`:"in";case"rule":return e.rule||e.rid||"rule";case"raise":return`raise ${wt(e.event)}`;case"query_out":return`query ${e.rid||""}/${e.name||""}`;case"klog":return e.msg||"klog";case"ent":return`ent:${e.name||""}`;case"send":return`send ${wt(e.event)}`;case"log":return`${(e.level||"log").toUpperCase()} ${e.msg||""}`.trim();case"error":return e.error||"error";case"done":return`done ${e.outcome||""}`.trim();default:return e.type}}function Vy(e){var n,r;const t=[e.type,e.rid,e.name,e.rule,e.result,e.msg,e.error,e.level,wt(e.event),va(e.query),e.eci,e.did,e.host,e.transport,e.childTxn];return(n=e.event)!=null&&n.attrs&&t.push(JSON.stringify(e.event.attrs)),(r=e.query)!=null&&r.args&&t.push(JSON.stringify(e.query.args)),e.val!==void 0&&t.push(JSON.stringify(e.val)),e.before!==void 0&&t.push(JSON.stringify(e.before)),e.after!==void 0&&t.push(JSON.stringify(e.after)),e.directives!==void 0&&t.push(JSON.stringify(e.directives)),t.filter(Boolean).join(" ").toLowerCase()}function Hy(e){var r;const t=Pt(e),n=t==null?void 0:t.event;return(n==null?void 0:n.domain)==="engine_ui"&&zy.includes(n.name)?!0:((r=t==null?void 0:t.query)==null?void 0:r.name)==="__testing"}function qy(e,t){var a,u;const n=(e.steps||[]).filter(c=>ya(c,t));if(Gf(e)||!t.showTesting&&Hy(e))return!1;const r=(u=(a=Pt(e))==null?void 0:a.query)==null?void 0:u.rid;if(r&&!Xf(t,r)||t.errors&&e.outcome!=="error"&&!n.some(c=>c.type==="error")||t.klog&&!n.some(c=>c.type==="klog"))return!1;const l=t.ruleset.trim().toLowerCase();if(l&&!n.some(c=>{var m;return(c.rid||((m=c.query)==null?void 0:m.rid)||"").toLowerCase().includes(l)}))return!1;const i=t.event.trim().toLowerCase();if(i){const c=Pt(e);if(![wt(c==null?void 0:c.event),va(c==null?void 0:c.query),...n.map(d=>wt(d.event))].join(" ").toLowerCase().includes(i))return!1}const o=t.text.trim().toLowerCase();return!(o&&![...n.map(Vy),e.result!==void 0?JSON.stringify(e.result):"",Zf(e.policy)].join(" ").toLowerCase().includes(o))}function qu(e){var t;return(t=(e.steps||[]).find(n=>n.type==="done"))==null?void 0:t.directives}function Ky({value:e}){return Array.isArray(e)?s.jsx("ul",{className:"mb-0 pl-3",children:e.map((t,n)=>{const r=t&&typeof t=="object"?t:{};return s.jsxs("li",{className:"mb-2",children:[s.jsx("div",{className:"text-mono",children:r.name||"directive"}),r.rid?s.jsxs("div",{className:"small text-muted",children:[r.rid,r.rule?` · ${r.rule}`:""]}):null,s.jsx(Xe,{value:r.options??{}})]},n)})}):s.jsx(tm,{value:e})}function tm({value:e}){if(e&&typeof e=="object"&&!Array.isArray(e)&&e._truncated===!0){const t=e;return s.jsxs("div",{children:[s.jsxs("div",{className:"text-muted small mb-1",children:["Result truncated",typeof t.bytes=="number"?` (${t.bytes} bytes)`:"","."]}),t.preview?s.jsx("pre",{className:"small mb-0",children:t.preview}):null]})}return s.jsx(Xe,{value:e})}function Xe({value:e}){if(e==null||typeof e!="object")return s.jsx("span",{className:"text-mono",children:JSON.stringify(e)});if(Array.isArray(e))return e.length===0?s.jsx("span",{className:"text-muted",children:"[]"}):s.jsx("ul",{className:"log-json mb-0",children:e.map((n,r)=>s.jsx("li",{children:s.jsx(Xe,{value:n})},r))});const t=Object.entries(e);return t.length===0?s.jsx("span",{className:"text-muted",children:"{}"}):s.jsx("ul",{className:"log-json mb-0",children:t.map(([n,r])=>s.jsxs("li",{children:[s.jsx("span",{className:"text-mono",children:n}),r!=null&&typeof r=="object"?s.jsx(Xe,{value:r}):s.jsxs(s.Fragment,{children:[": ",s.jsx("span",{className:"text-mono",children:JSON.stringify(r)})]})]},n))})}function xe({title:e,children:t}){return s.jsxs("section",{className:"mb-3",children:[s.jsx("h4",{className:"h6 text-muted mb-1",children:e}),t]})}const Qy=({txn:e,step:t,nest:n=0,selected:r,onSelect:l})=>{var c,m,d;const i=t.depth||0,o=t.type==="error"||t.type==="done"&&(t.outcome==="error"||e.outcome==="error"),a=t.type==="rule"?uo(t.result):"",u=t.type==="in"&&((c=t.event)!=null&&c.attrs)||t.type==="raise"&&((m=t.event)!=null&&m.attrs)||t.type==="send"&&((d=t.event)!=null&&d.attrs)?Object.keys(t.event.attrs).slice(0,4):[];return s.jsxs("button",{type:"button",className:"log-row"+(r?" log-row-selected":"")+(o?" log-row-error":""),style:{paddingLeft:8+n*16+Math.max(0,i)*14},"aria-pressed":r,onClick:l,children:[s.jsx("span",{className:"log-row-time text-muted",children:Wy(t.t||e.start)}),s.jsxs("span",{className:"log-row-label text-mono",children:[em(t),t.type==="rule"&&t.rid?s.jsx("span",{className:"log-chip",children:t.rid}):null,t.type==="send"&&t.transport?s.jsx("span",{className:"log-chip",children:t.transport}):null,u.map(g=>s.jsx("span",{className:"log-chip",children:g},g))]}),s.jsxs("span",{className:"log-row-aside",children:[a?s.jsx("span",{className:a==="fired"?"text-success":"text-muted",children:a}):null,t.type==="ent"&&t.deleted?s.jsx("span",{className:"text-muted",children:"deleted"}):null,t.type==="done"?s.jsxs("span",{children:[e.kind==="query"?"":`${t.fired??0} fired`,e.kind!=="query"&&Array.isArray(t.directives)&&t.directives.length?` ${t.directives.length} directives`:"",typeof e.durationMs=="number"?`${e.kind==="query"?"":" "}${e.durationMs}ms`:""]}):null]})]})};function Jy({pico:e,txn:t,step:n,channelTags:r,onSelectStep:l}){var x,v,k,y,f,h,w,b;const i=Pt(t),o=t.steps||[],a=n.type==="rule"?o.map((j,E)=>({item:j,index:E})).filter(({item:j})=>j.type==="send"&&j.rid===n.rid&&j.rule===n.rule):[],u=n.type==="rule"?o.map((j,E)=>({item:j,index:E})).filter(({item:j})=>j.type==="raise"&&j.rid===n.rid&&j.rule===n.rule):[],c=n.type==="rule"?o.map((j,E)=>({item:j,index:E})).filter(({item:j})=>j.type==="klog"&&j.rid===n.rid&&j.rule===n.rule):[],m=o.map((j,E)=>({item:j,index:E})).filter(({item:j})=>j.type!=="ent"?!1:n.type==="rule"?j.rid===n.rid&&j.rule===n.rule:n.type==="in"||n.type==="done"),d=o.map((j,E)=>({item:j,index:E})).filter(({item:j})=>j.type==="rule"),g=n.rid&&n.rule?`/pico/${e.eci}/testing?rid=${encodeURIComponent(n.rid)}&rule=${encodeURIComponent(n.rule)}`:(x=n.query)!=null&&x.rid&&n.query.name?`/pico/${e.eci}/testing?rid=${encodeURIComponent(n.query.rid)}&query=${encodeURIComponent(n.query.name)}`:(v=i==null?void 0:i.query)!=null&&v.rid&&i.query.name?`/pico/${e.eci}/testing?rid=${encodeURIComponent(i.query.rid)}&query=${encodeURIComponent(i.query.name)}`:"",N=n.rid||((k=i==null?void 0:i.query)==null?void 0:k.rid)||"";return s.jsxs("div",{className:"log-inspector",children:[s.jsx("h3",{className:"h5 mb-1",children:em(n)}),s.jsxs("p",{className:"small text-muted mb-3",children:[t.kind||"transaction"," ",t.txnId,typeof t.durationMs=="number"?` · ${t.durationMs}ms`:"",t.outcome?` · ${t.outcome}`:"",t.traceId&&(t.parentTxnId||(t.steps||[]).some(j=>j.type==="query_out"))?` · trace ${t.traceId}`:""]}),i!=null&&i.eci||(y=i==null?void 0:i.event)!=null&&y.eci||(f=i==null?void 0:i.query)!=null&&f.eci||t.policy?s.jsxs(xe,{title:"Channel",children:[s.jsx("div",{className:"text-mono small",children:(i==null?void 0:i.eci)||((h=i==null?void 0:i.event)==null?void 0:h.eci)||((w=i==null?void 0:i.query)==null?void 0:w.eci)||((b=t.policy)==null?void 0:b.eci)}),r.length?s.jsx("div",{className:"mt-1",children:r.map(j=>s.jsx("span",{className:"log-chip",children:j},j))}):null,t.policy?s.jsx("div",{className:t.policy.allowed?"mt-1":"mt-1 text-danger",children:Zf(t.policy)}):null]}):null,n.type==="rule"?s.jsxs(xe,{title:"Rule",children:[s.jsxs("div",{children:[n.rid," · ",n.rule," ·"," ",uo(n.result)||"selected"]}),n.select?s.jsx("pre",{className:"mt-2 mb-0 small",children:n.select}):null]}):null,(n.type==="in"||n.type==="done")&&(i!=null&&i.query)?s.jsxs(xe,{title:"Query",children:[s.jsxs("div",{className:"text-mono mb-2",children:[i.query.rid,s.jsx("span",{className:"text-muted",children:" / "}),i.query.name]}),s.jsx("div",{className:"text-muted small mb-1",children:"Arguments"}),Object.keys(i.query.args||{}).length===0?s.jsx("div",{className:"text-muted mb-2",children:"No arguments."}):s.jsx("div",{className:"mb-2",children:s.jsx(Xe,{value:i.query.args})}),t.result!==void 0?s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"text-muted small mb-1",children:"Result"}),s.jsx(tm,{value:t.result})]}):null]}):null,(n.type==="in"||n.type==="done")&&!(i!=null&&i.query)&&qu(t)!==void 0?s.jsx(xe,{title:"Output",children:s.jsx(Ky,{value:qu(t)})}):null,(n.type==="in"||n.type==="done")&&!(i!=null&&i.query)?s.jsx(xe,{title:"Rules",children:d.length===0?s.jsx("div",{className:"text-muted",children:"No rules selected."}):s.jsx("ul",{className:"mb-0 pl-3",children:d.map(({item:j,index:E})=>s.jsxs("li",{children:[s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 align-baseline",onClick:()=>l(E),children:j.rule||j.rid})," ",s.jsx("span",{className:"text-muted",children:uo(j.result)})]},E))})}):null,(n.type==="in"||n.type==="rule"||n.type==="done")&&(i!=null&&i.event)?s.jsx(xe,{title:"Input",children:s.jsx(Xe,{value:i.event.attrs||{}})}):null,n.type==="query_out"?s.jsxs(xe,{title:"Called",children:[s.jsxs("div",{className:"text-mono",children:[n.rid,"/",n.name]}),n.eci?s.jsx("div",{className:"small text-muted",children:n.eci}):null,t.traceId?s.jsxs("div",{className:"small text-mono text-muted",children:["trace ",t.traceId]}):null]}):null,n.type==="send"&&n.event?s.jsxs(xe,{title:"Sent",children:[s.jsx("div",{className:"mb-1 text-mono",children:wt(n.event)}),s.jsxs("div",{className:"small text-muted mb-2",children:[n.transport||"send",n.eci?` · ${n.eci}`:"",n.did?` · ${n.did}`:"",n.host?` · ${n.host}`:""]}),n.childTxn?s.jsxs("div",{className:"small text-mono text-muted mb-2",children:["txn ",n.childTxn]}):null,s.jsx(Xe,{value:n.event.attrs||{}})]}):null,n.type==="raise"&&n.event?s.jsxs(xe,{title:"Raised event",children:[s.jsx("div",{className:"mb-1 text-mono",children:wt(n.event)}),s.jsx(Xe,{value:n.event.attrs||{}})]}):null,n.type==="ent"?s.jsxs(xe,{title:"State",children:[s.jsxs("div",{className:"text-mono mb-2",children:["ent:",n.name]}),s.jsx("div",{className:"text-muted small mb-1",children:"Before"}),s.jsx(Xe,{value:n.before}),s.jsx("div",{className:"text-muted small mt-2 mb-1",children:n.deleted?"Deleted":"After"}),n.deleted?null:s.jsx(Xe,{value:n.after})]}):null,m.length?s.jsx(xe,{title:"State",children:s.jsx("ul",{className:"mb-0 pl-3",children:m.map(({item:j,index:E})=>s.jsxs("li",{children:[s.jsxs("button",{type:"button",className:"btn btn-link btn-sm p-0 align-baseline",onClick:()=>l(E),children:["ent:",j.name]}),j.deleted?s.jsx("span",{className:"text-muted",children:" deleted"}):null]},E))})}):null,n.type==="klog"?s.jsxs(xe,{title:"klog",children:[s.jsx("div",{className:"mb-1",children:n.msg}),s.jsx(Xe,{value:n.val})]}):null,c.length?s.jsx(xe,{title:"klog",children:c.map(({item:j,index:E})=>s.jsxs("div",{className:"mb-2",children:[s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 align-baseline",onClick:()=>l(E),children:j.msg||"klog"}),s.jsx(Xe,{value:j.val})]},E))}):null,n.type==="log"?s.jsx(xe,{title:"Log",children:s.jsxs("div",{children:[(n.level||"log").toUpperCase()," ",n.msg]})}):null,n.type==="error"?s.jsxs(xe,{title:"Error",children:[s.jsx("div",{className:"text-danger",children:n.error}),n.line!=null?s.jsxs("div",{className:"small text-muted",children:["line ",n.line,n.column!=null?`:${n.column}`:"",n.filename?` ${n.filename}`:""]}):null]}):null,a.length?s.jsx(xe,{title:"Sent",children:s.jsx("ul",{className:"mb-0 pl-3",children:a.map(({item:j,index:E})=>s.jsxs("li",{children:[s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 align-baseline",onClick:()=>l(E),children:wt(j.event)}),j.transport?s.jsxs("span",{className:"text-muted",children:[" ",j.transport]}):null]},E))})}):null,u.length?s.jsx(xe,{title:"Raised",children:s.jsx("ul",{className:"mb-0 pl-3",children:u.map(({item:j,index:E})=>s.jsx("li",{children:s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 align-baseline",onClick:()=>l(E),children:wt(j.event)})},E))})}):null,g||N?s.jsxs(xe,{title:"Actions",children:[g?s.jsx(ft,{className:"btn btn-outline-primary btn-sm mr-2",to:g,children:"Replay"}):null,N?s.jsx(ft,{className:"btn btn-outline-secondary btn-sm",to:`/pico/${e.eci}/rulesets?rid=${encodeURIComponent(N)}`,children:"Open ruleset"}):null]}):null]})}function nm(e,t){var l;let n=e.parentTxnId;const r=new Set;for(;n&&t.has(n);){if(n===e.txnId||r.has(n))return!1;r.add(n),n=(l=t.get(n))==null?void 0:l.parentTxnId}return!!e.parentTxnId&&t.has(e.parentTxnId)}function Yy(e){const t=new Map(e.map(r=>[r.txnId,r])),n=new Map;for(const r of e){if(!nm(r,t)||!r.parentTxnId)continue;const l=n.get(r.parentTxnId)||[];l.push(r),n.set(r.parentTxnId,l)}for(const r of n.values())r.sort((l,i)=>(l.start||"").localeCompare(i.start||""));return n}function Xy(e){const t=new Map(e.map(n=>[n.txnId,n]));return e.filter(n=>!nm(n,t))}const co=({txn:e,grouped:t,filters:n,nest:r=0,selected:l,onSelect:i,seen:o})=>{if(o!=null&&o.has(e.txnId))return null;const a=new Set(o);a.add(e.txnId);const u=[...t.get(e.txnId)||[]],c=[];(e.steps||[]).forEach((m,d)=>{if(ya(m,n)){if(m.type==="query_out"){const g=`${m.rid||""}/${m.name||""}`,N=u.findIndex(x=>{var k;const v=(k=Pt(x))==null?void 0:k.query;return!!v&&`${v.rid||""}/${v.name||""}`===g});if(N>=0){const[x]=u.splice(N,1);c.push(s.jsx(co,{txn:x,grouped:t,filters:n,nest:r+1,selected:l,onSelect:i,seen:a},x.txnId));return}}c.push(s.jsx(Qy,{txn:e,step:m,nest:r,selected:(l==null?void 0:l.txnId)===e.txnId&&l.index===d,onSelect:()=>i(e.txnId,d)},`${e.txnId}:${d}`))}});for(const m of u)c.push(s.jsx(co,{txn:m,grouped:t,filters:n,nest:r+1,selected:l,onSelect:i,seen:a},m.txnId));return s.jsx("div",{className:"log-txn"+(r>0?" log-txn-nested":""),children:c})};function Gy({pico:e,isTabActive:t=!0}){var b,j,E,_,O,L,U,te;const n=nn([],()=>By(e.eci)),r=nn(null,()=>ye(`/c/${e.eci}/query/io.picolabs.pico-engine-ui/pico`)),[l,i]=p.useState(Uy),[o,a]=p.useState(!1),[u,c]=p.useState([]),[m,d]=p.useState(null);p.useEffect(()=>{c([])},[e.eci]),p.useEffect(()=>{t&&(n.load(),r.load())},[e.eci,t]),p.useEffect(()=>{if(!t||!l.live)return;const I=new EventSource(`/api/picos/${encodeURIComponent(e.eci)}/logs/live`);return I.onmessage=ie=>{let Z;try{Z=JSON.parse(ie.data)}catch{return}!Z||typeof Z.txnId!="string"||Gf(Z)||c(G=>{const Ce=G.filter(D=>D.txnId!==Z.txnId);return[Z,...Ce].slice(0,200)})},()=>I.close()},[e.eci,t,l.live]);const g=[],N=new Set;for(const I of[...u,...n.data])N.has(I.txnId)||(N.add(I.txnId),g.push(I));const x=g.filter(I=>qy(I,l)),v=m?g.find(I=>I.txnId===m.txnId):void 0,k=(b=v==null?void 0:v.steps)==null?void 0:b[(m==null?void 0:m.index)??-1],y=k&&ya(k,l)?k:void 0,f=ao.filter(I=>l.showRulesets[I.rid]),h=((j=Pt(v||{}))==null?void 0:j.eci)||((_=(E=Pt(v||{}))==null?void 0:E.event)==null?void 0:_.eci)||((L=(O=Pt(v||{}))==null?void 0:O.query)==null?void 0:L.eci)||"",w=((te=(U=r.data)==null?void 0:U.channels.find(I=>I.id===h))==null?void 0:te.tags)||[];return s.jsxs("div",{className:"pico-logging-tab",children:[s.jsx(nr,{title:"Logging",waiting:n.waiting,onRefresh:()=>n.load(),hint:"Select a step to inspect it. Filters chooses what is hidden. Refresh reloads the list."}),s.jsx(Ue,{error:n.error}),s.jsxs("div",{className:"log-filters form-row align-items-center mb-2",children:[s.jsx("div",{className:"col-md-6 mb-1",children:s.jsx("input",{className:"form-control form-control-sm",placeholder:"Search",value:l.text,onChange:I=>i({...l,text:I.target.value})})}),s.jsx("div",{className:"col-auto mb-1",children:s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-secondary",onClick:()=>a(!0),children:"Filters"})}),s.jsxs("div",{className:"col mb-1 small text-muted",children:[f.length?`Showing ${f.map(I=>I.label).join(", ")}`:"Wrangler, PDS, subscription, and developer UI are hidden",l.showTesting?" · testing":"",l.errors?" · errors":"",l.klog?" · klog":"",l.live?"":" · live off"]})]}),o?s.jsx("div",{className:"settings-modal-backdrop",onClick:()=>a(!1),children:s.jsxs("div",{className:"settings-modal card shadow",role:"dialog","aria-modal":"true","aria-labelledby":"log-filters-title",onClick:I=>I.stopPropagation(),children:[s.jsxs("div",{className:"card-header d-flex align-items-center justify-content-between",children:[s.jsx("h2",{id:"log-filters-title",className:"h5 mb-0",children:"Filters"}),s.jsx("button",{type:"button",className:"close","aria-label":"Close",onClick:()=>a(!1),children:s.jsx("span",{"aria-hidden":"true",children:"×"})})]}),s.jsxs("div",{className:"card-body",children:[s.jsxs("label",{className:"d-block mb-2",children:[s.jsx("input",{type:"checkbox",className:"mr-2",checked:l.live,onChange:I=>i({...l,live:I.target.checked})}),"Live"]}),s.jsxs("label",{className:"d-block mb-2",children:[s.jsx("input",{type:"checkbox",className:"mr-2",checked:l.errors,onChange:I=>i({...l,errors:I.target.checked})}),"Errors only"]}),s.jsxs("label",{className:"d-block mb-3",children:[s.jsx("input",{type:"checkbox",className:"mr-2",checked:l.klog,onChange:I=>i({...l,klog:I.target.checked})}),"klog only"]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{className:"small text-muted mb-1",children:"Ruleset contains"}),s.jsx("input",{className:"form-control form-control-sm",value:l.ruleset,onChange:I=>i({...l,ruleset:I.target.value})})]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{className:"small text-muted mb-1",children:"domain:type"}),s.jsx("input",{className:"form-control form-control-sm",value:l.event,onChange:I=>i({...l,event:I.target.value})})]}),s.jsx("h3",{className:"h6 mt-3",children:"Show engine rulesets"}),s.jsx("p",{className:"small text-muted",children:"Hidden rulesets stay out of the log until you check them. Their rules inside an event are hidden too."}),ao.map(I=>s.jsxs("label",{className:"d-block mb-2",children:[s.jsx("input",{type:"checkbox",className:"mr-2",checked:!!l.showRulesets[I.rid],onChange:ie=>i({...l,showRulesets:{...l.showRulesets,[I.rid]:ie.target.checked}})}),I.label,s.jsxs("span",{className:"text-muted small",children:[" ",I.rid]})]},I.rid)),s.jsxs("label",{className:"d-block mb-0 mt-2",children:[s.jsx("input",{type:"checkbox",className:"mr-2",checked:l.showTesting,onChange:I=>i({...l,showTesting:I.target.checked})}),"Testing bookkeeping"]})]})]})}):null,s.jsx(Yf,{storageKey:"pico-engine-ui-logging-split",defaultLeftRatio:.58,left:s.jsxs("div",{children:[x.length===0&&!n.waiting?s.jsx("p",{className:"text-muted mb-0",children:"No matching transactions."}):null,Xy(x).map(I=>Array.isArray(I.entries)&&!I.steps?s.jsxs("div",{className:"mb-2",children:[s.jsxs("div",{className:"text-mono small",children:[I.time," ",I.header]}),I.entries.map((Z,G)=>s.jsx("pre",{children:Z},G))]},I.txnId):s.jsx(co,{txn:I,grouped:Yy(x),filters:l,selected:m,onSelect:(Z,G)=>d({txnId:Z,index:G})},I.txnId))]}),right:v&&y?s.jsx(Jy,{pico:e,txn:v,step:y,channelTags:w,onSelectStep:I=>d({txnId:v.txnId,index:I})}):s.jsx("p",{className:"text-muted mb-0",children:"Select a step."})})]})}function ht(e){return e&&typeof e=="object"?e:null}function rm(e){return!Array.isArray(e)||e.length===0?"Install or flush failed":e.map(t=>{const n=ht(t);if(!n||typeof n.message!="string")return String(t);const r=n.line,l=n.column;return r!=null&&l!=null?`${n.message} (${r}:${l})`:n.message}).join("; ")}function xa(e,t){var r,l;const n=ht(e);if(!n)return null;if(n.domain==="wrangler"&&n.name===t){const i=ht(n.data);return ht(i==null?void 0:i.attrs)}for(const i of["responses","directives","events"]){const o=n[i];if(Array.isArray(o))for(const a of o){const u=xa(a,t);if(u)return u;const c=ht(a),m=((r=ht(c==null?void 0:c.options))==null?void 0:r.event)??(c==null?void 0:c.event);if(m){const d=ht(m);if((d==null?void 0:d.domain)==="wrangler"&&(d==null?void 0:d.name)===t)return ht((l=ht(d.data))==null?void 0:l.attrs)??ht(d.attrs)}}}return null}function Zy(e){const t=xa(e,"ruleset_install_failed");return t?rm(t.errors):null}function ev(e){const t=xa(e,"ruleset_flush_failed");return t?rm(t.errors):null}function tv(e,t){const n=e.via;let r;n==="send"?(r=`may send ${e.domain&&e.type?`${e.domain}:${e.type}`:"event"}`,e.replyTo&&(r+=` (replyTo ${String(e.replyTo)})`)):n==="schedule"?r=`may schedule ${e.domain&&e.type?`${e.domain}:${e.type}`:"event"}`:n==="action"?r=`may call ${String(e.name||e.source||"action")}`:r=`may raise ${e.domain&&e.type?`${e.domain}:${e.type}`:"internal event"}`;const l=e.when;if(l&&l.length){const i=l.map(o=>o.condition?String(o.condition):o.branch?String(o.branch):"").filter(Boolean);i.length&&(r+=` — when ${i.join(", ")}`)}else t&&(r+=` — when ${t}`);return r}function lm(e){if(e.call)return String(e.call);const t=String(e.fn||"?"),n=e.args;if(!n||!Object.keys(n).length)return`${t}()`;const r=Object.entries(n).map(([l,i])=>`${l}: ${i!=null&&i.attr?`attr ${i.attr}`:"?"}`).join("; ");return`${t}(${Object.keys(n).join(", ")}) (${r})`}function nv(e){const t=e.startsWith("?")?e.slice(1):e,n=new URLSearchParams(t),r=n.get("rid");return r?{rid:r,rule:n.get("rule")||void 0,query:n.get("query")||void 0,example:n.get("example")||void 0}:null}function rs(e){const t=new URLSearchParams;return t.set("rid",e.rid),e.rule&&t.set("rule",e.rule),e.query&&t.set("query",e.query),e.example&&t.set("example",e.example),"?"+t.toString()}function xt(e){return e.replace(/[^a-zA-Z0-9_-]/g,"_")}function Ku(e,t){return`try-op-${xt(e)}-${xt(t)}`}function Qu(e,t,n){const r=["try-q",xt(e),xt(t)].join("-");return n?`${r}-${xt(n)}`:r}function Ju(e){const t=["try",xt(e.rid),xt(e.rule),xt(e.domain),xt(e.name)].join("-");return e.exampleName?`${t}-${xt(e.exampleName)}`:t}function rv(e){return String(e).split(/\n\s*\n/).map(t=>t.split(`
|
|
76
|
-
`).map(n=>n.trim()).filter(Boolean).join(" ")).filter(Boolean)}function ks(e){const t=(e.entry||[]).filter(n=>n.domain&&n.name).map(n=>`${n.domain}:${n.name}`);return t.length?t.join(" · "):e.rule}function lv(e){const t=new Map;for(const n of e){const r=ks(n);t.set(r,(t.get(r)||0)+1)}return t}function sm(e,t){return(t.get(ks(e))||0)>1}function sv(e){if(e.via!=="action")return!0;const t=String(e.name||e.source||"");return t.includes(":")&&!/^ctx:/.test(t)}function im(e){return(e||[]).map(t=>t.required!==!1?t.name:`${t.name}?`).join(", ")}const iv={fired:"when it acts",else:"when it doesn't act",notfired:"when it doesn't act",always:"always",finally:"always, afterwards",action:"when it acts"};function ov(e,t){const n=[];for(const r of e||[])r.condition!==void 0?r.condition!==t&&n.push(`if ${String(r.condition)}`):r.branch!==void 0?n.push(iv[String(r.branch)]||String(r.branch)):r.if!==void 0?n.push(`if ${String(r.if)}`):r.choose!==void 0?n.push(`when ${String(r.choose)} is ${String(r.key)}`):r.sample?n.push("one chosen at random"):r.foreach!==void 0?n.push(`for each of ${String(r.foreach)}`):r.onFinal&&n.push("after the last iteration");return n.join(", ")}function av(e,t){const n=e.domain&&e.type?`${e.domain}:${e.type}`:String(e.source||e.name||"?");let r;e.via==="send"?(r=`sends ${n}`,e.replyTo?r+=` to the ${String(e.replyTo)} you supplied`:e.target&&(r+=` to ${String(e.target)}`)):e.via==="schedule"?r=`schedules ${n} on this pico`:e.via==="action"?r=`calls module action ${n}`:r=`raises ${n} on this pico`;const l=ov(e.when,t);return r+(l?` — ${l}`:"")}const om=({columns:e,rows:t})=>t.length?s.jsx("div",{className:"table-responsive mb-2",children:s.jsxs("table",{className:"table table-sm table-bordered mb-0",children:[s.jsx("thead",{className:"thead-light",children:s.jsxs("tr",{children:[s.jsx("th",{children:e==="attrs"?"Attribute":"Parameter"}),s.jsx("th",{children:"Type"}),s.jsx("th",{children:"Required"}),e==="params"?s.jsx("th",{children:"Default"}):null,s.jsx("th",{children:"Description"})]})}),s.jsx("tbody",{children:t.map(n=>s.jsxs("tr",{children:[s.jsx("td",{children:s.jsx("code",{children:n.name})}),s.jsx("td",{children:n.type||""}),s.jsx("td",{children:n.required!==!1?"yes":"no"}),e==="params"?s.jsx("td",{className:"text-mono small",children:n.default!==void 0?JSON.stringify(n.default):n.defaultExpr||""}):null,s.jsx("td",{className:"small",children:n.description||""})]},n.name))})]})}):s.jsx("p",{className:"text-muted small mb-2",children:"No attributes."});function Yu(e){var t;(t=document.getElementById(e))==null||t.scrollIntoView({behavior:"smooth",block:"start"})}function uv({ops:e,queries:t,dupCounts:n}){if(e.length===0&&t.length===0)return null;const r=e.length+t.length;return s.jsxs("details",{className:"ruleset-docs-index mb-3 small border rounded bg-light",children:[s.jsxs("summary",{className:"p-2 font-weight-bold user-select-none",children:["On this page (",r,")"]}),s.jsxs("nav",{className:"px-2 pb-2","aria-label":"Ruleset reference index",children:[e.length>0?s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"text-muted text-uppercase mb-1 mt-1",children:"Operations"}),s.jsx("ul",{className:"list-unstyled mb-2",children:e.map(l=>{const i=ks(l),o=sm(l,n);return s.jsx("li",{className:"mb-1",children:s.jsxs("button",{type:"button",className:"btn btn-link btn-sm p-0 text-left align-baseline",onClick:()=>Yu(`op-${l.rule}`),children:[i,o?s.jsxs("span",{className:"text-muted",children:[" (",l.rule,")"]}):null]})},l.rule)})})]}):null,t.length>0?s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"text-muted text-uppercase mb-1",children:"Queries"}),s.jsx("ul",{className:"list-unstyled mb-0",children:t.map(l=>{const i=im(l.args);return s.jsx("li",{className:"mb-1",children:s.jsxs("button",{type:"button",className:"btn btn-link btn-sm p-0 text-left text-mono align-baseline",onClick:()=>Yu(`q-${l.name}`),children:[l.name,"(",i,")"]})},l.name)})})]}):null]})]})}function wa({text:e,className:t}){const n=rv(e);return s.jsx("div",{className:"ruleset-docs-description mb-2",children:n.map((r,l)=>s.jsx("p",{className:[t,l>0?"mt-4":"","mb-0"].join(" ").trim(),children:r},l))})}function cv({op:e,dup:t,rid:n,picoEci:r}){const l=ks(e),i=(e.emits||[]).filter(sv);return s.jsxs("section",{className:"ruleset-docs-op mb-4",id:`op-${e.rule}`,children:[s.jsxs("h4",{className:"h5",children:[l,t?s.jsxs("span",{className:"text-muted font-weight-normal",children:[" (",e.rule,")"]}):null]}),e.description?s.jsx(wa,{text:e.description,className:"text-muted"}):null,s.jsxs("p",{className:"small mb-1",children:[s.jsx("strong",{children:"Send:"})," ",(e.entry||[]).filter(o=>o.domain&&o.name).map(o=>`${o.domain}:${o.name}`).join(", or ")||"—"]}),s.jsxs("p",{className:"small mb-2",children:[s.jsx("strong",{children:"Rule:"})," ",s.jsx("code",{children:e.rule})]}),s.jsx(om,{columns:"attrs",rows:e.attrs||[]}),e.examples&&e.examples.length>0?s.jsxs("div",{className:"mb-2",children:[s.jsx("strong",{className:"small",children:"Examples"}),s.jsx("ul",{className:"small mb-0",children:e.examples.map(o=>s.jsxs("li",{className:"mb-1",children:[s.jsxs("strong",{children:[o.name,":"]})," ",s.jsx("code",{children:JSON.stringify(o.attrs)}),s.jsx(ft,{to:`/pico/${r}/testing${rs({rid:n,rule:e.rule,example:o.name})}`,className:"btn btn-sm btn-outline-primary ml-2 py-0",children:"Try it"})]},o.name))})]}):(e.entry||[]).some(o=>o.domain&&o.name)?s.jsx("p",{className:"small mb-2",children:s.jsx(ft,{to:`/pico/${r}/testing${rs({rid:n,rule:e.rule})}`,className:"btn btn-sm btn-outline-primary py-0",children:"Try in Testing"})}):null,s.jsxs("p",{className:"small mb-1",children:[s.jsx("strong",{children:"Acts when:"})," ",e.condition?s.jsx("code",{children:e.condition}):"always, once selected"]}),e.prechecks&&e.prechecks.length>0?s.jsxs("p",{className:"small mb-2",children:[s.jsx("strong",{children:"Check first:"})," ",e.prechecks.map((o,a)=>s.jsxs(p.Fragment,{children:[a>0?", ":"",s.jsx("code",{children:lm(o)})]},a))]}):null,i.length>0?s.jsxs(s.Fragment,{children:[s.jsxs("p",{className:"small mb-1",children:[s.jsx("strong",{children:"May emit"})," (possible, not guaranteed):"]}),s.jsx("ul",{className:"small pl-3",children:i.map((o,a)=>s.jsx("li",{children:av(o,e.condition)},a))})]}):null,e.alsoSelectedBy&&e.alsoSelectedBy.length>0?s.jsxs(s.Fragment,{children:[s.jsx("p",{className:"small mb-1",children:s.jsx("strong",{children:"Other rules on the same event:"})}),s.jsx("ul",{className:"small pl-3 mb-2",children:e.alsoSelectedBy.map(o=>{const a=(o.select||"").replace(/\s+/g," ").replace(/^select when /,"");return s.jsxs("li",{children:[s.jsx("code",{children:o.rule}),o.condition?s.jsxs("span",{children:[" ","— acts when ",s.jsx("code",{children:o.condition})]}):/\bwhere\b|re#/.test(a)?s.jsxs("span",{children:[" ","— selects only ",s.jsx("code",{children:a})]}):null]},o.rule)})})]}):null,e.select?s.jsxs("details",{className:"small",children:[s.jsx("summary",{children:"Select expression"}),s.jsx("pre",{className:"small bg-light p-2 text-mono mb-0",children:e.select})]}):null]})}function dv({q:e,rid:t,picoEci:n}){const r=im(e.args),l=(e.args||[]).map(o=>`${o.name}=…`).join("&"),i=e.examples||[];return s.jsxs("section",{className:"ruleset-docs-query mb-4",id:`q-${e.name}`,children:[s.jsxs("h4",{className:"h5",children:[s.jsxs("code",{children:[e.name,"(",r,")"]}),e.returns?s.jsxs(s.Fragment,{children:[" → ",e.returns]}):null]}),e.description?s.jsx(wa,{text:e.description,className:"text-muted"}):null,s.jsx(om,{columns:"params",rows:e.args||[]}),i.length>0?s.jsxs("div",{className:"mb-2",children:[s.jsx("strong",{className:"small",children:"Examples"}),s.jsx("ul",{className:"small mb-0",children:i.map(o=>s.jsxs("li",{className:"mb-1",children:[s.jsxs("strong",{children:[o.name,":"]})," ",s.jsx("code",{children:JSON.stringify(o.args)}),s.jsx(ft,{to:`/pico/${n}/testing${rs({rid:t,query:e.name,example:o.name})}`,className:"btn btn-sm btn-outline-primary ml-2 py-0",children:"Try it"})]},o.name))})]}):null,s.jsx("p",{className:"small mb-2",children:s.jsx(ft,{to:`/pico/${n}/testing${rs({rid:t,query:e.name})}`,className:"btn btn-sm btn-outline-primary py-0",children:"Try in Testing"})}),s.jsxs("p",{className:"small text-muted mb-0",children:[s.jsx("strong",{children:"Call:"})," ",s.jsxs("code",{children:["GET /sky/query/<eci>/",t,"/",e.name,l?`?${l}`:""]})]})]})}const fv=({schema:e,picoEci:t})=>{const n=e.operations||[],r=e.queries||[],l=lv(n);return n.length===0&&r.length===0?s.jsxs("div",{className:"text-muted",children:[s.jsxs("p",{children:["This ruleset has no annotated operations or shared queries on this pico. Annotations are opt-in: add a ",s.jsx("code",{children:"/** … */"})," block immediately above a rule or shared function to make it discoverable."]}),s.jsxs("p",{className:"mb-0",children:["See"," ",s.jsx("a",{href:"/docs/#/krl/discovery",target:"_blank",rel:"noreferrer",children:"KRL discovery"})," ","in the engine docs."]})]}):s.jsxs("div",{className:"ruleset-docs-content",children:[e.description?s.jsx(wa,{text:e.description,className:"lead"}):null,s.jsxs("p",{className:"small text-muted mb-1",children:[s.jsx("strong",{children:"Version:"})," ",e.rid_version||"draft"]}),s.jsxs("p",{className:"small text-muted mb-2",children:["This reference is built from the ruleset as installed on this pico, not from a saved Markdown file. If you edit doc comments in the KRL source, use ",s.jsx("strong",{children:"Flush"})," on the Rulesets tab so this view picks up your changes."]}),s.jsx(uv,{ops:n,queries:r,dupCounts:l}),n.length>0?s.jsxs(s.Fragment,{children:[s.jsx("h3",{className:"mt-4 mb-2",children:"Operations"}),s.jsx("p",{className:"small text-muted",children:"Events you can send to a pico with this ruleset installed. Whether an operation acts, and what it emits, depends on its condition and the pico's state when the event arrives."}),n.map(i=>s.jsx(cv,{op:i,dup:sm(i,l),rid:e.rid,picoEci:t},i.rule))]}):null,r.length>0?s.jsxs(s.Fragment,{children:[s.jsx("h3",{className:"mt-4 mb-2",children:"Queries"}),r.map(i=>s.jsx(dv,{q:i,rid:e.rid,picoEci:t},i.name))]}):null]})},mv=({eci:e,rid:t,onClose:n})=>{const[r,l]=p.useState(null),[i,o]=p.useState(null),[a,u]=p.useState(!0);return p.useEffect(()=>{let c=!1;return u(!0),o(null),l(null),q(`/c/${e}/query/io.picolabs.wrangler/rulesetSchema`,{rid:t},!1).then(m=>{c||(l(m),u(!1))}).catch(m=>{c||(o(String(m)),u(!1))}),()=>{c=!0}},[e,t]),p.useEffect(()=>{function c(m){m.key==="Escape"&&n()}return window.addEventListener("keydown",c),()=>window.removeEventListener("keydown",c)},[n]),s.jsx("div",{className:"settings-modal-backdrop",onClick:n,children:s.jsxs("div",{className:"settings-modal card shadow ruleset-docs-modal",onClick:c=>c.stopPropagation(),role:"dialog","aria-modal":"true","aria-labelledby":"ruleset-docs-title",children:[s.jsxs("div",{className:"card-header d-flex align-items-center justify-content-between py-2",children:[s.jsx("h2",{id:"ruleset-docs-title",className:"h5 mb-0 text-mono",children:t}),s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-secondary",onClick:n,children:"Close"})]}),s.jsx("div",{className:"card-body",children:a?s.jsx("p",{className:"text-muted mb-0",children:"Loading schema…"}):i?s.jsx("div",{className:"alert alert-warning mb-0",children:i}):r?s.jsx(fv,{schema:r,picoEci:e}):s.jsxs("p",{className:"text-muted mb-0",children:["No discovery schema for ",s.jsx("code",{children:t})," on this pico (",s.jsx("code",{children:"rulesetSchema"})," returned null). Install the ruleset on this pico, or use Flush on the Rulesets tab if it was recently updated."]})})]})})},hv="io.picolabs.oauth",Xu="io.picolabs.oauth must be installed on the mesh root pico, not a child pico.";function fo(e){return e.parent==null}function am(e){return e.includes(hv)}function pv(e,t){return am(t)&&!fo(e)}const gv=({pico:e,isTabActive:t=!0})=>{var J,Y,Le,he;const[n]=ca(),r=n.get("rid")||"",[l,i]=p.useState(""),[o,a]=p.useState("{}"),[u,c]=p.useState(null),[m,d]=p.useState({}),[g,N]=p.useState(null),[x,v]=p.useState(null),[k,y]=p.useState(null),[f,h]=p.useState(e.eci),w=p.useRef(null);p.useEffect(()=>{h(e.eci)},[e.eci]);function b(){try{return JSON.parse(o),!0}catch{return!1}}function j(){return l.trim().length>0&&b()}async function E(C){if(C){v(null);try{const F=await C.text(),K=await q("/api/stage-krl",{content:F,filename:C.name});K!=null&&K.url&&i(K.url)}catch(F){v(F instanceof Error?F.message:String(F))}finally{w.current&&(w.current.value="")}}}const _=nn(null,()=>ye(`/c/${e.eci}/query/io.picolabs.pico-engine-ui/pico`)),O=nn(null,()=>ye("/api/krl-sources")),L=Rt(C=>q(`/c/${C.eci}/event-wait/engine_ui/install`,{url:C.url,config:C.config}).then(F=>{const K=Zy(F);if(K)throw new Error(K);return q(`/c/${C.eci}/query/io.picolabs.pico-engine-ui/pico`,{})}).then(F=>{_.setData(F),i(""),a("{}")})),U=Rt(({eci:C,rid:F})=>q(`/c/${C}/event/engine_ui/uninstall/query/io.picolabs.pico-engine-ui/pico`,{rid:F}).then(K=>{_.setData(K)})),te=Rt(({eci:C,url:F})=>q(`/c/${C}/event-wait/engine_ui/flush`,{url:F}).then(K=>{const Ne=ev(K);if(Ne)throw new Error(Ne);return q(`/c/${C}/query/io.picolabs.pico-engine-ui/pico`,{})}).then(K=>{_.setData(K)}));p.useEffect(()=>{_.load(),O.load()},[e.eci]);const I=(((J=_.data)==null?void 0:J.rulesets)||[]).map(C=>C.rid).join(`
|
|
76
|
+
`).map(n=>n.trim()).filter(Boolean).join(" ")).filter(Boolean)}function ks(e){const t=(e.entry||[]).filter(n=>n.domain&&n.name).map(n=>`${n.domain}:${n.name}`);return t.length?t.join(" · "):e.rule}function lv(e){const t=new Map;for(const n of e){const r=ks(n);t.set(r,(t.get(r)||0)+1)}return t}function sm(e,t){return(t.get(ks(e))||0)>1}function sv(e){if(e.via!=="action")return!0;const t=String(e.name||e.source||"");return t.includes(":")&&!/^ctx:/.test(t)}function im(e){return(e||[]).map(t=>t.required!==!1?t.name:`${t.name}?`).join(", ")}const iv={fired:"when it acts",else:"when it doesn't act",notfired:"when it doesn't act",always:"always",finally:"always, afterwards",action:"when it acts"};function ov(e,t){const n=[];for(const r of e||[])r.condition!==void 0?r.condition!==t&&n.push(`if ${String(r.condition)}`):r.branch!==void 0?n.push(iv[String(r.branch)]||String(r.branch)):r.if!==void 0?n.push(`if ${String(r.if)}`):r.choose!==void 0?n.push(`when ${String(r.choose)} is ${String(r.key)}`):r.sample?n.push("one chosen at random"):r.foreach!==void 0?n.push(`for each of ${String(r.foreach)}`):r.onFinal&&n.push("after the last iteration");return n.join(", ")}function av(e,t){const n=e.domain&&e.type?`${e.domain}:${e.type}`:String(e.source||e.name||"?");let r;e.via==="send"?(r=`sends ${n}`,e.replyTo?r+=` to the ${String(e.replyTo)} you supplied`:e.target&&(r+=` to ${String(e.target)}`)):e.via==="schedule"?r=`schedules ${n} on this pico`:e.via==="action"?r=`calls module action ${n}`:r=`raises ${n} on this pico`;const l=ov(e.when,t);return r+(l?` — ${l}`:"")}const om=({columns:e,rows:t})=>t.length?s.jsx("div",{className:"table-responsive mb-2",children:s.jsxs("table",{className:"table table-sm table-bordered mb-0",children:[s.jsx("thead",{className:"thead-light",children:s.jsxs("tr",{children:[s.jsx("th",{children:e==="attrs"?"Attribute":"Parameter"}),s.jsx("th",{children:"Type"}),s.jsx("th",{children:"Required"}),e==="params"?s.jsx("th",{children:"Default"}):null,s.jsx("th",{children:"Description"})]})}),s.jsx("tbody",{children:t.map(n=>s.jsxs("tr",{children:[s.jsx("td",{children:s.jsx("code",{children:n.name})}),s.jsx("td",{children:n.type||""}),s.jsx("td",{children:n.required!==!1?"yes":"no"}),e==="params"?s.jsx("td",{className:"text-mono small",children:n.default!==void 0?JSON.stringify(n.default):n.defaultExpr||""}):null,s.jsx("td",{className:"small",children:n.description||""})]},n.name))})]})}):s.jsx("p",{className:"text-muted small mb-2",children:"No attributes."});function Yu(e){var t;(t=document.getElementById(e))==null||t.scrollIntoView({behavior:"smooth",block:"start"})}function uv({ops:e,queries:t,dupCounts:n}){if(e.length===0&&t.length===0)return null;const r=e.length+t.length;return s.jsxs("details",{className:"ruleset-docs-index mb-3 small border rounded bg-light",children:[s.jsxs("summary",{className:"p-2 font-weight-bold user-select-none",children:["On this page (",r,")"]}),s.jsxs("nav",{className:"px-2 pb-2","aria-label":"Ruleset reference index",children:[e.length>0?s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"text-muted text-uppercase mb-1 mt-1",children:"Operations"}),s.jsx("ul",{className:"list-unstyled mb-2",children:e.map(l=>{const i=ks(l),o=sm(l,n);return s.jsx("li",{className:"mb-1",children:s.jsxs("button",{type:"button",className:"btn btn-link btn-sm p-0 text-left align-baseline",onClick:()=>Yu(`op-${l.rule}`),children:[i,o?s.jsxs("span",{className:"text-muted",children:[" (",l.rule,")"]}):null]})},l.rule)})})]}):null,t.length>0?s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"text-muted text-uppercase mb-1",children:"Queries"}),s.jsx("ul",{className:"list-unstyled mb-0",children:t.map(l=>{const i=im(l.args);return s.jsx("li",{className:"mb-1",children:s.jsxs("button",{type:"button",className:"btn btn-link btn-sm p-0 text-left text-mono align-baseline",onClick:()=>Yu(`q-${l.name}`),children:[l.name,"(",i,")"]})},l.name)})})]}):null]})]})}function wa({text:e,className:t}){const n=rv(e);return s.jsx("div",{className:"ruleset-docs-description mb-2",children:n.map((r,l)=>s.jsx("p",{className:[t,l>0?"mt-4":"","mb-0"].join(" ").trim(),children:r},l))})}function cv({op:e,dup:t,rid:n,picoEci:r}){const l=ks(e),i=(e.emits||[]).filter(sv);return s.jsxs("section",{className:"ruleset-docs-op mb-4",id:`op-${e.rule}`,children:[s.jsxs("h4",{className:"h5",children:[l,t?s.jsxs("span",{className:"text-muted font-weight-normal",children:[" (",e.rule,")"]}):null]}),e.description?s.jsx(wa,{text:e.description,className:"text-muted"}):null,s.jsxs("p",{className:"small mb-1",children:[s.jsx("strong",{children:"Send:"})," ",(e.entry||[]).filter(o=>o.domain&&o.name).map(o=>`${o.domain}:${o.name}`).join(", or ")||"—"]}),s.jsxs("p",{className:"small mb-2",children:[s.jsx("strong",{children:"Rule:"})," ",s.jsx("code",{children:e.rule})]}),s.jsx(om,{columns:"attrs",rows:e.attrs||[]}),e.examples&&e.examples.length>0?s.jsxs("div",{className:"mb-2",children:[s.jsx("strong",{className:"small",children:"Examples"}),s.jsx("ul",{className:"small mb-0",children:e.examples.map(o=>s.jsxs("li",{className:"mb-1",children:[s.jsxs("strong",{children:[o.name,":"]})," ",s.jsx("code",{children:JSON.stringify(o.attrs)}),s.jsx(ft,{to:`/pico/${r}/testing${rs({rid:n,rule:e.rule,example:o.name})}`,className:"btn btn-sm btn-outline-primary ml-2 py-0",children:"Try it"})]},o.name))})]}):(e.entry||[]).some(o=>o.domain&&o.name)?s.jsx("p",{className:"small mb-2",children:s.jsx(ft,{to:`/pico/${r}/testing${rs({rid:n,rule:e.rule})}`,className:"btn btn-sm btn-outline-primary py-0",children:"Try in Testing"})}):null,s.jsxs("p",{className:"small mb-1",children:[s.jsx("strong",{children:"Acts when:"})," ",e.condition?s.jsx("code",{children:e.condition}):"always, once selected"]}),e.prechecks&&e.prechecks.length>0?s.jsxs("p",{className:"small mb-2",children:[s.jsx("strong",{children:"Check first:"})," ",e.prechecks.map((o,a)=>s.jsxs(p.Fragment,{children:[a>0?", ":"",s.jsx("code",{children:lm(o)})]},a))]}):null,i.length>0?s.jsxs(s.Fragment,{children:[s.jsxs("p",{className:"small mb-1",children:[s.jsx("strong",{children:"May emit"})," (possible, not guaranteed):"]}),s.jsx("ul",{className:"small pl-3",children:i.map((o,a)=>s.jsx("li",{children:av(o,e.condition)},a))})]}):null,e.alsoSelectedBy&&e.alsoSelectedBy.length>0?s.jsxs(s.Fragment,{children:[s.jsx("p",{className:"small mb-1",children:s.jsx("strong",{children:"Other rules on the same event:"})}),s.jsx("ul",{className:"small pl-3 mb-2",children:e.alsoSelectedBy.map(o=>{const a=(o.select||"").replace(/\s+/g," ").replace(/^select when /,"");return s.jsxs("li",{children:[s.jsx("code",{children:o.rule}),o.condition?s.jsxs("span",{children:[" ","— acts when ",s.jsx("code",{children:o.condition})]}):/\bwhere\b|re#/.test(a)?s.jsxs("span",{children:[" ","— selects only ",s.jsx("code",{children:a})]}):null]},o.rule)})})]}):null,e.select?s.jsxs("details",{className:"small",children:[s.jsx("summary",{children:"Select expression"}),s.jsx("pre",{className:"small bg-light p-2 text-mono mb-0",children:e.select})]}):null]})}function dv({q:e,rid:t,picoEci:n}){const r=im(e.args),l=(e.args||[]).map(o=>`${o.name}=…`).join("&"),i=e.examples||[];return s.jsxs("section",{className:"ruleset-docs-query mb-4",id:`q-${e.name}`,children:[s.jsxs("h4",{className:"h5",children:[s.jsxs("code",{children:[e.name,"(",r,")"]}),e.returns?s.jsxs(s.Fragment,{children:[" → ",e.returns]}):null]}),e.description?s.jsx(wa,{text:e.description,className:"text-muted"}):null,s.jsx(om,{columns:"params",rows:e.args||[]}),i.length>0?s.jsxs("div",{className:"mb-2",children:[s.jsx("strong",{className:"small",children:"Examples"}),s.jsx("ul",{className:"small mb-0",children:i.map(o=>s.jsxs("li",{className:"mb-1",children:[s.jsxs("strong",{children:[o.name,":"]})," ",s.jsx("code",{children:JSON.stringify(o.args)}),s.jsx(ft,{to:`/pico/${n}/testing${rs({rid:t,query:e.name,example:o.name})}`,className:"btn btn-sm btn-outline-primary ml-2 py-0",children:"Try it"})]},o.name))})]}):null,s.jsx("p",{className:"small mb-2",children:s.jsx(ft,{to:`/pico/${n}/testing${rs({rid:t,query:e.name})}`,className:"btn btn-sm btn-outline-primary py-0",children:"Try in Testing"})}),s.jsxs("p",{className:"small text-muted mb-0",children:[s.jsx("strong",{children:"Call:"})," ",s.jsxs("code",{children:["GET /sky/query/<eci>/",t,"/",e.name,l?`?${l}`:""]})]})]})}const fv=({schema:e,picoEci:t})=>{const n=e.operations||[],r=e.queries||[],l=lv(n);return n.length===0&&r.length===0?s.jsxs("div",{className:"text-muted",children:[s.jsxs("p",{children:["This ruleset has no annotated operations or shared queries on this pico. Annotations are opt-in: add a ",s.jsx("code",{children:"/** … */"})," block immediately above a rule or shared function to make it discoverable."]}),s.jsxs("p",{className:"mb-0",children:["See"," ",s.jsx("a",{href:"https://docs.picolabs.io/#/krl/discovery",target:"_blank",rel:"noreferrer",children:"KRL discovery"})," ","in the engine docs."]})]}):s.jsxs("div",{className:"ruleset-docs-content",children:[e.description?s.jsx(wa,{text:e.description,className:"lead"}):null,s.jsxs("p",{className:"small text-muted mb-1",children:[s.jsx("strong",{children:"Version:"})," ",e.rid_version||"draft"]}),s.jsxs("p",{className:"small text-muted mb-2",children:["This reference is built from the ruleset as installed on this pico, not from a saved Markdown file. If you edit doc comments in the KRL source, use ",s.jsx("strong",{children:"Flush"})," on the Rulesets tab so this view picks up your changes."]}),s.jsx(uv,{ops:n,queries:r,dupCounts:l}),n.length>0?s.jsxs(s.Fragment,{children:[s.jsx("h3",{className:"mt-4 mb-2",children:"Operations"}),s.jsx("p",{className:"small text-muted",children:"Events you can send to a pico with this ruleset installed. Whether an operation acts, and what it emits, depends on its condition and the pico's state when the event arrives."}),n.map(i=>s.jsx(cv,{op:i,dup:sm(i,l),rid:e.rid,picoEci:t},i.rule))]}):null,r.length>0?s.jsxs(s.Fragment,{children:[s.jsx("h3",{className:"mt-4 mb-2",children:"Queries"}),r.map(i=>s.jsx(dv,{q:i,rid:e.rid,picoEci:t},i.name))]}):null]})},mv=({eci:e,rid:t,onClose:n})=>{const[r,l]=p.useState(null),[i,o]=p.useState(null),[a,u]=p.useState(!0);return p.useEffect(()=>{let c=!1;return u(!0),o(null),l(null),q(`/c/${e}/query/io.picolabs.wrangler/rulesetSchema`,{rid:t},!1).then(m=>{c||(l(m),u(!1))}).catch(m=>{c||(o(String(m)),u(!1))}),()=>{c=!0}},[e,t]),p.useEffect(()=>{function c(m){m.key==="Escape"&&n()}return window.addEventListener("keydown",c),()=>window.removeEventListener("keydown",c)},[n]),s.jsx("div",{className:"settings-modal-backdrop",onClick:n,children:s.jsxs("div",{className:"settings-modal card shadow ruleset-docs-modal",onClick:c=>c.stopPropagation(),role:"dialog","aria-modal":"true","aria-labelledby":"ruleset-docs-title",children:[s.jsxs("div",{className:"card-header d-flex align-items-center justify-content-between py-2",children:[s.jsx("h2",{id:"ruleset-docs-title",className:"h5 mb-0 text-mono",children:t}),s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-secondary",onClick:n,children:"Close"})]}),s.jsx("div",{className:"card-body",children:a?s.jsx("p",{className:"text-muted mb-0",children:"Loading schema…"}):i?s.jsx("div",{className:"alert alert-warning mb-0",children:i}):r?s.jsx(fv,{schema:r,picoEci:e}):s.jsxs("p",{className:"text-muted mb-0",children:["No discovery schema for ",s.jsx("code",{children:t})," on this pico (",s.jsx("code",{children:"rulesetSchema"})," returned null). Install the ruleset on this pico, or use Flush on the Rulesets tab if it was recently updated."]})})]})})},hv="io.picolabs.oauth",Xu="io.picolabs.oauth must be installed on the mesh root pico, not a child pico.";function fo(e){return e.parent==null}function am(e){return e.includes(hv)}function pv(e,t){return am(t)&&!fo(e)}const gv=({pico:e,isTabActive:t=!0})=>{var J,Y,Le,he;const[n]=ca(),r=n.get("rid")||"",[l,i]=p.useState(""),[o,a]=p.useState("{}"),[u,c]=p.useState(null),[m,d]=p.useState({}),[g,N]=p.useState(null),[x,v]=p.useState(null),[k,y]=p.useState(null),[f,h]=p.useState(e.eci),w=p.useRef(null);p.useEffect(()=>{h(e.eci)},[e.eci]);function b(){try{return JSON.parse(o),!0}catch{return!1}}function j(){return l.trim().length>0&&b()}async function E(C){if(C){v(null);try{const F=await C.text(),K=await q("/api/stage-krl",{content:F,filename:C.name});K!=null&&K.url&&i(K.url)}catch(F){v(F instanceof Error?F.message:String(F))}finally{w.current&&(w.current.value="")}}}const _=nn(null,()=>ye(`/c/${e.eci}/query/io.picolabs.pico-engine-ui/pico`)),O=nn(null,()=>ye("/api/krl-sources")),L=Rt(C=>q(`/c/${C.eci}/event-wait/engine_ui/install`,{url:C.url,config:C.config}).then(F=>{const K=Zy(F);if(K)throw new Error(K);return q(`/c/${C.eci}/query/io.picolabs.pico-engine-ui/pico`,{})}).then(F=>{_.setData(F),i(""),a("{}")})),U=Rt(({eci:C,rid:F})=>q(`/c/${C}/event/engine_ui/uninstall/query/io.picolabs.pico-engine-ui/pico`,{rid:F}).then(K=>{_.setData(K)})),te=Rt(({eci:C,url:F})=>q(`/c/${C}/event-wait/engine_ui/flush`,{url:F}).then(K=>{const Ne=ev(K);if(Ne)throw new Error(Ne);return q(`/c/${C}/query/io.picolabs.pico-engine-ui/pico`,{})}).then(K=>{_.setData(K)}));p.useEffect(()=>{_.load(),O.load()},[e.eci]);const I=(((J=_.data)==null?void 0:J.rulesets)||[]).map(C=>C.rid).join(`
|
|
77
77
|
`);p.useEffect(()=>{if(!r||!t||!I.split(`
|
|
78
|
-
`).includes(r))return;d(F=>F[r]?F:{...F,[r]:!0});const C=window.setTimeout(()=>{var F;(F=document.getElementById(`id-rid-${r}`))==null||F.scrollIntoView({block:"start"})},0);return()=>window.clearTimeout(C)},[r,t,I]);function ie(){_.load(),O.load()}async function Z(C){y({rid:C,rulesetSchema:null,loading:!0,error:null});try{const F=await q(`/c/${e.eci}/query/io.picolabs.wrangler/rulesetSchema`,{rid:C},!1),K=((F==null?void 0:F.queries)||[]).filter(Ne=>Ne.view).length;if(!F||K===0){y({rid:C,rulesetSchema:F,loading:!1,error:"This ruleset has no @view functions in its compiled schema. Add @view to shared functions, then flush or reinstall."});return}y({rid:C,rulesetSchema:F,loading:!1,error:null})}catch(F){y({rid:C,rulesetSchema:null,loading:!1,error:String(F)})}}const G=new Set((((Y=_.data)==null?void 0:Y.rulesets)||[]).map(C=>C.rid)),Ce=(((Le=O.data)==null?void 0:Le.sources)||[]).filter(C=>!G.has(C.rid));function D(C,F){if(pv(e,C)){c(Xu);return}c(null),L.act({eci:e.eci,url:C,config:F})}function z(C){b()&&D(C.url,JSON.parse(o))}const B=_.data&&_.data.channels.filter(C=>C.familyChannelPicoID===null)||[];return s.jsxs("div",{children:[s.jsx(nr,{title:"Rulesets",waiting:_.waiting||O.waiting,onRefresh:ie}),s.jsx(Ue,{error:_.error}),_.data&&_.data.rulesets.length>0?_.data.rulesets.map(C=>{var lt,S,$,R;const F=C.rid,K=`id-rid-${F}`,Ne=!!m[F];return s.jsxs("div",{children:[s.jsx("div",{children:s.jsxs("div",{className:"form-check",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:K,onChange:A=>{const W=Object.assign({},m);A.target.checked?W[F]=!0:delete W[F],d(W)},checked:Ne}),s.jsx("label",{className:"form-check-label",htmlFor:K,children:s.jsxs("span",{className:"text-mono",children:[C.rid,(S=(lt=C==null?void 0:C.meta)==null?void 0:lt.krlMeta)!=null&&S.version?"@"+((R=($=C==null?void 0:C.meta)==null?void 0:$.krlMeta)==null?void 0:R.version):""]})}),s.jsx("button",{type:"button",className:"btn btn-link btn-sm py-0 align-baseline ml-2",onClick:A=>{A.preventDefault(),N(C.rid)},children:"Docs"}),(C.viewCount??0)>0?s.jsx("button",{type:"button",className:"btn btn-link btn-sm py-0 align-baseline ml-1",onClick:A=>{A.preventDefault(),Z(C.rid)},disabled:_.waiting,children:"View"}):null]})}),Ne?s.jsxs("div",{className:"ml-3",children:[s.jsxs("div",{children:[s.jsx("button",{className:"btn btn-outline-primary btn-sm",type:"button",onClick:A=>{A.preventDefault(),te.act({eci:e.eci,url:C.url})},disabled:_.waiting||te.waiting,children:"flush"})," ",s.jsx("button",{className:"btn btn-outline-danger btn-sm",type:"button",onClick:A=>{A.preventDefault(),U.act({eci:e.eci,rid:C.rid})},disabled:_.waiting||U.waiting,children:"uninstall"}),s.jsx(Ue,{error:te.error}),s.jsx(Ue,{error:U.error})]}),s.jsx("div",{children:s.jsx("a",{href:C.url,target:"_blank",children:C.url})}),s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Config:"})," ",JSON.stringify(C.config)]}),C.meta&&s.jsxs("div",{children:[s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Last flushed:"})," ",C.meta.flushed+""]}),s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Hash:"})," ",C.meta.hash]}),s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Compiler version:"})," ",C.meta.compiler.version]}),s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Compiler warnings:"})," ",Array.isArray(C.meta.compiler.warnings)?s.jsx("ul",{children:C.meta.compiler.warnings.map(A=>{var ee;let W="";return A&&typeof A.message=="string"?W=`${(ee=A.loc)!=null&&ee.start?`${A.loc.start.line}:${A.loc.start.column}: `:""}${A.message}`:W=JSON.stringify(A),s.jsx("li",{children:W})})}):s.jsx("span",{})]})]})]}):""]},F)}):s.jsx("span",{className:"text-muted",children:"- no rulesets -"}),s.jsx("hr",{}),s.jsx("h4",{children:"Engine KRL Directory"}),s.jsxs("p",{className:"text-muted",children:["Rulesets shipped with this pico-engine install that are not yet on this pico. Flush always reloads from the engine's local ",s.jsx("code",{children:"krl/"})," ","directory."]}),s.jsx(Ue,{error:O.error}),O.waiting?s.jsx("span",{className:"text-muted",children:"Loading local rulesets…"}):Ce.length>0?Ce.map(C=>{const F=`krl:${C.rid}`,K=`id-krl-${C.rid}`,Ne=!!m[F],lt=!!C.rootOnly&&!fo(e);return s.jsxs("div",{children:[s.jsxs("div",{className:"form-check",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:K,onChange:S=>{const $=Object.assign({},m);S.target.checked?$[F]=!0:delete $[F],d($)},checked:Ne}),s.jsxs("label",{className:"form-check-label",htmlFor:K,children:[s.jsx("span",{className:"text-mono",children:C.rid}),C.name?s.jsxs("span",{className:"text-muted",children:[" — ",C.name]}):""]})]}),Ne?s.jsxs("div",{className:"ml-3 mb-2",children:[s.jsx("div",{className:"small text-muted",children:C.relativePath}),C.rootOnly?s.jsx("div",{className:"small text-warning",children:"Root pico only"}):"",C.description?s.jsx("div",{className:"small text-muted",children:C.description}):"",s.jsx("div",{children:s.jsx("a",{href:C.url,target:"_blank",children:C.url})}),s.jsxs("div",{className:"mt-1",children:[lt?s.jsx("span",{className:"badge badge-warning text-dark",children:"root only"}):s.jsx("button",{type:"button",className:"btn btn-outline-primary btn-sm",disabled:_.waiting||L.waiting||!b(),onClick:S=>{S.preventDefault(),z(C)},children:"install"})," ",s.jsx("button",{type:"button",className:"btn btn-outline-secondary btn-sm",disabled:_.waiting||L.waiting,onClick:S=>{S.preventDefault(),i(C.url)},children:"use URL"})]})]}):""]},C.relativePath)}):O.data?s.jsx("span",{className:"text-muted",children:"- all local rulesets installed -"}):s.jsx("span",{className:"text-muted",children:"- no local rulesets found -"}),s.jsx("hr",{}),s.jsx("h4",{children:"Install Ruleset by URL"}),!fo(e)&&am(l)?s.jsx("p",{className:"text-warning small",children:Xu}):"",s.jsxs("form",{onSubmit:C=>{C.preventDefault(),j()&&l&&D(l,JSON.parse(o))},children:[s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:"new-rs-url",children:"URL"}),s.jsxs("div",{className:"input-group",children:[s.jsx("input",{id:"new-rs-url",className:"form-control",value:l,onChange:C=>i(C.target.value),disabled:_.waiting||L.waiting,placeholder:"https://… or file://… or Browse for a local .krl file"}),s.jsx("div",{className:"input-group-append",children:s.jsx("button",{type:"button",className:"btn btn-outline-secondary",disabled:_.waiting||L.waiting,onClick:()=>{var C;return(C=w.current)==null?void 0:C.click()},children:"Browse…"})})]}),s.jsx("input",{ref:w,type:"file",accept:".krl",className:"d-none","aria-hidden":!0,tabIndex:-1,onChange:C=>{var F;E((F=C.target.files)==null?void 0:F[0])}}),s.jsx("small",{className:"form-text text-muted",children:"Browse reads the file in your browser and stages it on the engine host as a temporary file:// URL (for local development)."}),s.jsx(Ue,{error:x})]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:"new-rs-config",children:"Config"}),s.jsx("textarea",{id:"new-rs-config",rows:3,className:"form-control",value:o,onChange:C=>a(C.target.value),disabled:_.waiting||L.waiting})]}),s.jsx("button",{type:"submit",className:"btn btn-outline-primary",disabled:_.waiting||L.waiting||!j(),children:"Install"}),s.jsx(Ue,{error:u||L.error})]}),g&&t?s.jsx(mv,{eci:e.eci,rid:g,onClose:()=>N(null)}):null,k?s.jsx("div",{className:"modal show d-block",tabIndex:-1,role:"dialog",style:{backgroundColor:"rgba(0, 0, 0, 0.45)"},onClick:()=>y(null),children:s.jsx("div",{className:"modal-dialog modal-dialog-centered",role:"document",onClick:C=>C.stopPropagation(),children:s.jsxs("div",{className:"modal-content",children:[s.jsxs("div",{className:"modal-header",children:[s.jsxs("h5",{className:"modal-title text-mono",children:["Open view — ",k.rid]}),s.jsx("button",{type:"button",className:"close","aria-label":"Close",onClick:()=>y(null),children:s.jsx("span",{"aria-hidden":"true",children:"×"})})]}),s.jsxs("div",{className:"modal-body",children:[s.jsxs("p",{className:"small text-muted",children:["XMLUI apps load at"," ",s.jsxs("code",{children:["/ui/<eci>/",k.rid,"/"]}),". Choose a channel whose query policy allows the view queries you need."," ",s.jsx("a",{href:"/docs/pico-views-xmlui.md",target:"_blank",rel:"noreferrer",children:"Pico views (XMLUI)"})]}),k.loading?s.jsx("p",{className:"small text-muted mb-0",children:"Loading schema…"}):k.error?s.jsx("p",{className:"text-danger small mb-0",children:k.error}):s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{className:"small mb-1 d-block",children:"Channel (ECI)"}),s.jsx("select",{className:"form-control form-control-sm text-mono",value:f,onChange:C=>h(C.target.value),children:B.map(C=>{const F=C.id+" "+C.tags.map(K=>`#${K}`).join(" ");return s.jsx("option",{value:C.id,children:F},C.id)})})]}),s.jsxs("div",{className:"small text-muted mb-2",children:[s.jsx("strong",{children:"@view"})," pages:"," ",(((he=k.rulesetSchema)==null?void 0:he.queries)||[]).filter(C=>C.view).map(C=>C.name).join(", ")]}),s.jsx("a",{className:"btn btn-primary btn-sm",href:`/ui/${f}/${encodeURIComponent(k.rid)}/`,target:"_blank",rel:"noreferrer",children:"Open in new tab"})]})]})]})})}):null]})};function Pe(e){return e.current&&e.current.value}function um(e){return e.layer2===!0}function mo(e){const t=e.name||e.channel_name;if(typeof t!="string")return;const n=t.trim();return n.length>0?n:void 0}function kt({label:e,value:t,copyable:n}){const[r,l]=p.useState(!1);if(!t)return null;async function i(){if(t)try{await navigator.clipboard.writeText(t),l(!0),window.setTimeout(()=>l(!1),2e3)}catch{}}return s.jsxs("div",{className:"mb-1 d-flex align-items-start",children:[s.jsxs("div",{className:"flex-grow-1",children:[s.jsxs("b",{className:"text-muted",children:[e,":"]})," ",s.jsx("span",{className:"text-mono small",children:t})]}),n?s.jsx("button",{type:"button",className:"btn btn-sm btn-link p-0 ml-2 off-engine-copy-btn",title:r?"Copied":"Copy to clipboard","aria-label":r?"Copied":"Copy to clipboard",onClick:()=>{i()},children:r?s.jsx("span",{className:"text-success small",children:"Copied"}):s.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round","aria-hidden":"true",children:[s.jsx("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),s.jsx("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]})}):null]})}function yv({pico:e,rxEci:t,channel:n,onChannelUpdated:r}){const[l,i]=p.useState(!1),[o,a]=p.useState(""),[u,c]=p.useState(""),[m,d]=p.useState(!1),[g,N]=p.useState(null);p.useEffect(()=>{n&&(a(ro(n.eventPolicy)),c(lo(n.queryPolicy)))},[n]);async function x(v){if(v.preventDefault(),!!n){d(!0),N(null);try{await q(`/c/${e.eci}/event/engine_ui/update_channel/query/io.picolabs.pico-engine-ui/pico`,{eci:n.id,tags:n.tags,eventPolicy:ma(o),queryPolicy:ha(u)}),r(),i(!1)}catch(k){N(String(k))}finally{d(!1)}}}return s.jsxs("div",{className:"mt-2 p-2 border rounded bg-light",children:[s.jsxs("div",{className:"d-flex justify-content-between align-items-center mb-2",children:[s.jsx("b",{className:"small",children:"Relationship Rx channel policy"}),n&&!l?s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-secondary",onClick:()=>i(!0),children:"Edit policies"}):null,l?s.jsx("button",{type:"button",className:"btn btn-sm btn-link p-0",onClick:()=>{i(!1),N(null)},children:"Cancel"}):null]}),s.jsxs("div",{className:"small text-muted text-mono mb-1",children:["Rx ECI: ",t]}),n&&l?s.jsxs("form",{onSubmit:x,children:[s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{className:"small font-weight-bold",children:"Event policy"}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("textarea",{rows:3,className:"form-control form-control-sm",value:o,onChange:v=>a(v.target.value),disabled:m})}),s.jsx("div",{className:"col",children:s.jsx(pa,{src:o})})]})]}),s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{className:"small font-weight-bold",children:"Query policy"}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("textarea",{rows:3,className:"form-control form-control-sm",value:u,onChange:v=>c(v.target.value),disabled:m})}),s.jsx("div",{className:"col",children:s.jsx(ga,{src:u})})]})]}),s.jsx("button",{type:"submit",className:"btn btn-sm btn-outline-primary",disabled:m,children:"Save policies"})]}):n?s.jsxs("div",{className:"row",children:[s.jsxs("div",{className:"col",children:[s.jsx("div",{className:"small font-weight-bold",children:"Events"}),s.jsx(Kf,{policy:n.eventPolicy})]}),s.jsxs("div",{className:"col",children:[s.jsx("div",{className:"small font-weight-bold",children:"Queries"}),s.jsx(Qf,{policy:n.queryPolicy})]})]}):s.jsx("div",{className:"text-muted small",children:"Channel details not loaded — reload the tab or expand this relationship again."}),g?s.jsx("div",{className:"text-danger small mt-1",children:g}):null]})}function vv({pico:e,sub:t,rxChannel:n,onDelete:r,onRefresh:l,onChannelUpdated:i,showSend:o,deleteLabel:a="Cancel relationship"}){const u=um(t),c=p.useRef(null),m=p.useRef(null),[d,g]=p.useState(null),[N,x]=p.useState(!1);async function v(k){k.preventDefault(),g(null),x(!1);const y=Pe(c)||"wrangler",f=Pe(m)||"ping";try{await q(`/c/${e.eci}/event/wrangler/send_event_on_subs`,{subID:t.Id,domain:y,type:f,attrs:{uiTest:!0}}),x(!0)}catch(h){g(String(h))}}return s.jsxs("div",{className:"ml-3 mb-3",children:[u?s.jsx("span",{className:"badge badge-primary mb-2",children:"DID"}):s.jsx("span",{className:"badge badge-secondary mb-2",children:"ECI"}),s.jsx(kt,{label:"Remote Tx DID",value:t.Tx_did}),s.jsx(kt,{label:"Local Rx DID",value:t.Rx_did}),!u&&t.Tx?s.jsx(kt,{label:"Remote Tx ECI",value:t.Tx}):null,t.Tx_host?s.jsx(kt,{label:"Tx host",value:t.Tx_host}):null,t.Tx_role?s.jsx(kt,{label:"Tx role",value:t.Tx_role}):null,t.Rx_role?s.jsx(kt,{label:"Rx role",value:t.Rx_role}):null,mo(t)?s.jsx(kt,{label:"Name",value:mo(t)}):null,t.Rx?s.jsx(yv,{pico:e,rxEci:t.Rx,channel:n,onChannelUpdated:i}):null,o&&u?s.jsxs("form",{className:"form-inline mt-2",onSubmit:v,children:[s.jsx("input",{type:"text",className:"form-control form-control-sm mr-1",ref:c,placeholder:"domain",defaultValue:"wrangler"}),s.jsx("input",{type:"text",className:"form-control form-control-sm mr-1",ref:m,placeholder:"type",defaultValue:"ping"}),s.jsx("button",{type:"submit",className:"btn btn-sm btn-outline-primary",children:"Send test event"})]}):null,N?s.jsx("div",{className:"text-success small mt-1",children:"Event sent."}):null,d?s.jsx("div",{className:"text-danger small mt-1",children:d}):null,s.jsxs("details",{className:"mt-2",children:[s.jsx("summary",{className:"small text-muted",children:"Raw relationship record"}),s.jsx("pre",{className:"small mb-0",children:JSON.stringify(t,null,2)})]}),r?s.jsx("button",{className:"btn btn-outline-danger btn-sm mt-2",type:"button",onClick:r,children:a}):null]})}function xv({pico:e,myDid:t,publicIntro:n,wellKnownRx:r,onPublicIntroChange:l}){const[i,o]=p.useState(!1),[a,u]=p.useState(null);async function c(m){o(!0),u(null);try{await q(`/c/${e.eci}/event/wrangler/set_public_intro`,{enabled:m}),l(m)}catch(d){u(String(d))}finally{o(!1)}}return s.jsxs("div",{className:"mb-4 p-3 border rounded",children:[s.jsx("h5",{className:"h6 mb-3",children:"Identity"}),s.jsx(kt,{label:"did:webvh (myDid)",value:t||void 0,copyable:!0}),r?s.jsx(kt,{label:"wellKnown_Rx (ECI-based)",value:r}):null,s.jsxs("div",{className:"form-check mt-2",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:`public-intro-${e.eci}`,checked:n===!0,disabled:i||n===null,onChange:m=>c(m.target.checked)}),s.jsx("label",{className:"form-check-label",htmlFor:`public-intro-${e.eci}`,children:"Public intro (accept unsolicited SKY intro on did:webvh)"})]}),a?s.jsx("div",{className:"text-danger small mt-1",children:a}):null]})}const wv=({pico:e})=>{const[t,n]=p.useState([]),[r,l]=p.useState([]),[i,o]=p.useState([]),[a,u]=p.useState(""),[c,m]=p.useState(null),[d,g]=p.useState(null),[N,x]=p.useState({}),[v,k]=p.useState("did"),[y,f]=p.useState(null),h=p.useRef(null),w=p.useRef(null),b=p.useRef(null),j=p.useRef(null),E=p.useRef(null),_=p.useRef(null),O=p.useRef(null),L=p.useRef(null),U=p.useRef(null),te=p.useRef(null),I=p.useRef(null),ie=p.useRef(null),Z=p.useRef(null),G=nn(null,()=>ye(`/c/${e.eci}/query/io.picolabs.pico-engine-ui/pico`)),Ce=G.data&&G.data.channels||[];function D($){if($)return Ce.find(R=>R.id===$)}function z(){B(),J(),Y(),Le(),he(),G.load(),fe.refreshRelationships()}const B=()=>{ye(`/c/${e.eci}/query/io.picolabs.subscription/wellKnown_Rx`).then($=>{u(($==null?void 0:$.id)||"")}).catch(()=>u(""))},J=()=>{ye(`/c/${e.eci}/query/io.picolabs.wrangler/myDid`).then($=>m($?String($):null)).catch(()=>m(null)),ye(`/c/${e.eci}/query/io.picolabs.wrangler/publicIntro`).then($=>g(!!$)).catch(()=>g(null))},Y=()=>{ye(`/c/${e.eci}/query/io.picolabs.subscription/established`).then($=>{n($||[])})},Le=()=>{ye(`/c/${e.eci}/query/io.picolabs.subscription/inbound`).then($=>{o($||[])})},he=()=>{ye(`/c/${e.eci}/query/io.picolabs.subscription/outbound`).then($=>{l($||[])})};p.useEffect(()=>{z()},[e.eci]);const C=async $=>{await q(`/c/${e.eci}/event/wrangler/pending_relationship_approval`,{Id:$}),z()},F=async $=>{await q(`/c/${e.eci}/event/wrangler/outbound_relationship_cancellation`,{Id:$}),z()},K=async $=>{await q(`/c/${e.eci}/event/wrangler/relationship_cancellation`,{Id:$}),z()};async function Ne($){$.preventDefault(),f(null);const R=(Pe(h)||"").trim();if(!R.startsWith("did:")){f("target_did must start with did:");return}if(c&&R===c){f("Cannot form a relationship with yourself (target_did equals myDid).");return}try{await q(`/c/${e.eci}/event/wrangler/relationship`,{layer2:!0,target_did:R,name:Pe(w)||null,Rx_role:Pe(b)||null,Tx_role:Pe(j)||null,channel_type:Pe(E)||"Tx_Rx",password:Pe(_)||null}),he(),fe.refreshRelationships()}catch(A){f(String(A))}}async function lt($){$.preventDefault(),f(null);const R=(Pe(O)||"").trim();if(a&&R===a){f("Cannot form a relationship with yourself (wellKnown_Tx equals your wellKnown_Rx).");return}try{await q(`/c/${e.eci}/event/wrangler/relationship`,{wellKnown_Tx:Pe(O)||"",Rx_role:Pe(L)||null,Tx_role:Pe(U)||null,name:Pe(te)||null,channel_type:Pe(I)||null,Tx_host:Pe(ie)||null,password:Pe(Z)||null}),he()}catch(A){f(String(A))}}function S($,R){return $.map(A=>{const W=!!N[A.Id],ee=mo(A);return s.jsxs("div",{children:[s.jsxs("div",{className:"form-check",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:`${R.keyPrefix}-sub-${A.Id}`,checked:W,onChange:P=>{const V={...N};P.target.checked?V[A.Id]=!0:delete V[A.Id],x(V)}}),s.jsxs("label",{className:"form-check-label",htmlFor:`${R.keyPrefix}-sub-${A.Id}`,children:[ee?s.jsxs(s.Fragment,{children:[s.jsx("span",{className:"font-weight-bold",children:ee}),s.jsx("span",{className:"text-muted small ml-2 text-mono",children:A.Id})]}):s.jsx("span",{className:"text-mono",children:A.Id}),um(A)?s.jsx("span",{className:"badge badge-primary ml-1",children:"DID"}):null]})]}),W?s.jsxs(s.Fragment,{children:[s.jsx(vv,{pico:e,sub:A,rxChannel:D(A.Rx),showSend:R.showSend,onRefresh:z,onChannelUpdated:()=>{G.load()},deleteLabel:R.deleteLabel,onDelete:R.onDelete&&!R.onAccept?()=>{R.onDelete(A.Id)}:void 0}),R.onAccept?s.jsx("div",{className:"ml-3 mb-3",children:s.jsx("button",{className:"btn btn-outline-primary btn-sm",type:"button",onClick:()=>{R.onAccept(A.Id)},children:"Accept"})}):null]}):null]},`${R.keyPrefix}-${A.Id}`)})}return s.jsxs("div",{children:[s.jsx(nr,{title:"Relationships",waiting:G.waiting,onRefresh:z}),s.jsx(Ue,{error:G.error}),s.jsx(xv,{pico:e,myDid:c,publicIntro:d,wellKnownRx:a,onPublicIntroChange:g}),t.length>0?s.jsx("h5",{children:"Established"}):null,S(t,{keyPrefix:"est",onDelete:K,deleteLabel:"Delete relationship",showSend:!0}),i.length>0?s.jsx("h5",{className:"mt-3",children:"Inbound pending"}):null,S(i,{keyPrefix:"in",onAccept:C}),r.length>0?s.jsx("h5",{className:"mt-3",children:"Outbound pending"}):null,S(r,{keyPrefix:"out",onDelete:F,deleteLabel:"Cancel outbound"}),s.jsxs("div",{className:"mt-4",children:[s.jsx("h5",{children:"New relationship"}),s.jsxs("ul",{className:"nav nav-pills mb-2",children:[s.jsx("li",{className:"nav-item",children:s.jsx("button",{type:"button",className:"nav-link"+(v==="did"?" active":""),onClick:()=>k("did"),children:"DID-based"})}),s.jsx("li",{className:"nav-item",children:s.jsx("button",{type:"button",className:"nav-link"+(v==="legacy"?" active":""),onClick:()=>k("legacy"),children:"ECI-based"})})]}),v==="did"?s.jsxs("form",{onSubmit:Ne,children:[s.jsxs("p",{className:"text-muted small",children:["Introduce via SKY using the peer's ",s.jsx("code",{children:"did:webvh"})," ","(",s.jsx("b",{children:"myDid"})," on their Relationships tab). Share your"," ",s.jsx("b",{children:"myDid"})," above when they form a relationship with you."]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{children:"target_did"}),s.jsx("input",{type:"text",className:"form-control",ref:h,placeholder:"did:webvh:…",required:!0})]}),s.jsxs("div",{className:"form-row",children:[s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"name"}),s.jsx("input",{type:"text",className:"form-control",ref:w})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Rx_role"}),s.jsx("input",{type:"text",className:"form-control",ref:b})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Tx_role"}),s.jsx("input",{type:"text",className:"form-control",ref:j})]})]}),s.jsxs("div",{className:"form-row",children:[s.jsxs("div",{className:"form-group col-md-6",children:[s.jsx("label",{children:"channel_type"}),s.jsx("input",{type:"text",className:"form-control",ref:E,defaultValue:"Tx_Rx"})]}),s.jsxs("div",{className:"form-group col-md-6",children:[s.jsx("label",{children:"password (optional)"}),s.jsx("input",{type:"text",className:"form-control",ref:_})]})]}),s.jsx("button",{type:"submit",className:"btn btn-outline-primary",children:"Request relationship"})]}):s.jsxs("form",{onSubmit:lt,children:[s.jsx("p",{className:"text-muted small",children:"Classic introduction via the peer's wellKnown_Rx ECI."}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{children:"wellKnown_Tx"}),s.jsx("input",{type:"text",className:"form-control",ref:O,placeholder:"Remote wellKnown_Rx ECI"})]}),s.jsxs("div",{className:"form-row",children:[s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Rx_role"}),s.jsx("input",{type:"text",className:"form-control",ref:L})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Tx_role"}),s.jsx("input",{type:"text",className:"form-control",ref:U})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"name"}),s.jsx("input",{type:"text",className:"form-control",ref:te})]})]}),s.jsxs("div",{className:"form-row",children:[s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"channel_type"}),s.jsx("input",{type:"text",className:"form-control",ref:I})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Tx_host"}),s.jsx("input",{type:"text",className:"form-control",ref:ie})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"password"}),s.jsx("input",{type:"text",className:"form-control",ref:Z})]})]}),s.jsx("button",{type:"submit",className:"btn btn-outline-secondary",children:"Request legacy relationship"})]}),y?s.jsx("div",{className:"text-danger small mt-2",children:y}):null]})]})};function jv(e){if(!e)return null;const t=[],n=new Set;for(const l of e.operations||[]){const i=(l.attrs||[]).map(a=>a.name),o=l.examples||[];for(const a of l.entry||[]){if(!a.domain||!a.name)continue;const u=`${a.domain}:${a.name}`;if(o.length===0){if(n.has(u))continue;n.add(u),t.push({domain:a.domain,name:a.name,attrs:i.length?i:void 0});continue}for(const c of o){const m=`${u}:${c.name}`;if(n.has(m))continue;n.add(m);const d={};for(const[N,x]of Object.entries(c.attrs||{}))typeof x=="string"||typeof x=="number"||typeof x=="boolean"?d[N]=x:x!=null&&(d[N]=JSON.stringify(x));const g=Object.keys(d).length>0?Object.keys(d):i;t.push({domain:a.domain,name:a.name,attrs:g.length?g:void 0,exampleName:c.name,defaults:Object.keys(d).length?d:void 0})}}}const r=[];for(const l of e.queries||[]){if(l.view)continue;const i=(l.args||[]).map(a=>a.name),o=l.examples||[];if(o.length===0){r.push({name:l.name,args:i.length?i:void 0});continue}for(const a of o){const u={};for(const[c,m]of Object.entries(a.args||{}))typeof m=="string"||typeof m=="number"||typeof m=="boolean"?u[c]=m:m!=null&&(u[c]=JSON.stringify(m));r.push({name:l.name,args:Object.keys(u).length>0?Object.keys(u):i.length?i:void 0,exampleName:a.name,defaults:Object.keys(u).length?u:void 0})}}return t.length===0&&r.length===0?null:{events:t,queries:r}}function Nv(e){if(!e)return;const t={};for(const[n,r]of Object.entries(e))typeof r=="string"||typeof r=="number"||typeof r=="boolean"?t[n]=r:r!=null&&(t[n]=JSON.stringify(r));return Object.keys(t).length?t:void 0}function Sv(e){const t=[],n=(e.attrs||[]).map(l=>l.name),r=e.examples||[];for(const l of e.entry||[])if(!(!l.domain||!l.name)){if(r.length===0){t.push({domain:l.domain,name:l.name,attrs:n.length?n:void 0});continue}for(const i of r){const o=Nv(i.attrs);t.push({domain:l.domain,name:l.name,attrs:o&&Object.keys(o).length>0?Object.keys(o):n.length?n:void 0,exampleName:i.name,defaults:o})}}return{events:t}}function cm(e){const t=e.currentTarget,n={};for(let r=0;r<t.elements.length;r++){const l=t.elements[r];if(l&&l.name){let i=l.value;if(i==="")continue;if(l instanceof HTMLInputElement&&l.type==="number"){n[l.name]=Number(i);continue}try{i=JSON.parse(String(i))}catch{}n[l.name]=i}}return n}function kv(e){if(e!==void 0)return typeof e=="object"?JSON.stringify(e):String(e)}const dm=({name:e,meta:t,defaultValue:n,exampleAsPlaceholder:r=!1})=>{const l=(t==null?void 0:t.type)||"Any",i=[e,l!=="Any"?l:null,t!=null&&t.required?"required":null,t==null?void 0:t.description].filter(Boolean).join(" · ")||e,o=kv(n),a=r&&o!==void 0,u=a?`${i} · e.g. ${o}`:i,c=a?void 0:n!==void 0?String(n):void 0;if(l==="Boolean"){const m=a?"":c==="true"?"true":c==="false"?"false":"";return s.jsxs("select",{name:e,className:"form-control form-control-sm d-inline-block w-auto mr-1 mb-1",defaultValue:m,title:a&&o?`e.g. ${o}`:void 0,children:[s.jsx("option",{value:"",children:a&&o?`— (e.g. ${o})`:"—"}),s.jsx("option",{value:"true",children:"true"}),s.jsx("option",{value:"false",children:"false"})]})}return l==="Number"?s.jsx("input",{type:"number",name:e,className:"form-control form-control-sm d-inline-block w-auto mr-1 mb-1",placeholder:u,defaultValue:c}):l==="Map"||l==="Array"?s.jsx("textarea",{name:e,className:"form-control form-control-sm d-block mb-1 text-mono",rows:2,placeholder:u+" (JSON)",defaultValue:c!==void 0&&typeof n=="object"?JSON.stringify(n):c}):s.jsx("input",{type:"text",name:e,className:"form-control form-control-sm d-inline-block w-auto mr-1 mb-1",placeholder:u,defaultValue:c})},Gu=({domain:e,name:t,attrs:n,defaults:r,exampleName:l,attrMeta:i,testingEci:o,formId:a,className:u="border border-warning p-2 mt-1",showInlineResult:c=!1,exampleAsPlaceholder:m=!1,onResult:d})=>{const[g,N]=p.useState(!1),[x,v]=p.useState(null),[k,y]=p.useState(null);async function f(w){w.preventDefault();const b=cm(w);N(!0),v(null),y(null),d==null||d(null,null);try{const j=await q(`/c/${o}/event-wait/${e}/${t}`,b,!1),E=JSON.stringify(j,void 0,2);c?v(E):d==null||d(E,null,b)}catch(j){const E=String(j);c?y(E):d==null||d(null,E,b)}finally{N(!1)}}const h=`${e}:${t}`;return s.jsxs("form",{id:a,className:u,onSubmit:f,children:[s.jsx("div",{children:(n||[]).map(w=>{const b=r&&r[w],j=i==null?void 0:i.find(E=>E.name===w);return s.jsx(dm,{name:w,meta:j,defaultValue:b,exampleAsPlaceholder:m},w)})}),s.jsxs("button",{type:"submit",className:"btn btn-sm btn-warning",disabled:g,children:[g?"Sending…":h,l?` (${l})`:""]}),c&&k?s.jsx("pre",{className:"small text-danger mt-2 mb-0",children:k}):null,c&&x?s.jsx("pre",{className:"small bg-light p-2 mt-2 mb-0",children:x}):null]})},bv=({rid:e,name:t,args:n,defaults:r,exampleName:l,argMeta:i,testingEci:o,formId:a,className:u="border border-primary p-2 mb-2",showInlineResult:c=!1,exampleAsPlaceholder:m=!1,onResult:d})=>{const[g,N]=p.useState(!1),[x,v]=p.useState(null),[k,y]=p.useState(null);async function f(h){h.preventDefault();const w=cm(h);N(!0),v(null),y(null),d==null||d(null,null);try{const b=await q(`/c/${o}/query/${e}/${t}`,w,!1),j=JSON.stringify(b,void 0,2);c?v(j):d==null||d(j,null,w)}catch(b){const j=String(b);c?y(j):d==null||d(null,j,w)}finally{N(!1)}}return s.jsxs("form",{id:a,className:u,onSubmit:f,children:[s.jsx("div",{children:(n||[]).map(h=>{const w=r&&r[h],b=i==null?void 0:i.find(j=>j.name===h);return s.jsx(dm,{name:h,meta:b,defaultValue:w,exampleAsPlaceholder:m},h)})}),s.jsxs("button",{type:"submit",className:"btn btn-sm btn-primary",disabled:g,children:[g?"Running…":t,l?` (${l})`:""]}),c&&k?s.jsx("pre",{className:"small text-danger mt-2 mb-0",children:k}):null,c&&x?s.jsx("pre",{className:"small bg-light p-2 mt-2 mb-0",children:x}):null]})},ho=({text:e,className:t=""})=>{const[n,r]=p.useState(!1);async function l(){try{await navigator.clipboard.writeText(e),r(!0),window.setTimeout(()=>r(!1),2e3)}catch{window.prompt("Copy:",e)}}return s.jsx("button",{type:"button",className:"btn btn-sm btn-link p-0 align-self-start "+t,title:n?"Copied":"Copy to clipboard","aria-label":n?"Copied":"Copy to clipboard",onClick:()=>void l(),children:n?s.jsx("span",{className:"text-success small",children:"✓"}):s.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round","aria-hidden":"true",children:[s.jsx("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),s.jsx("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]})})},Ev=/password|secret|token|apikey|api_key|credential/i,Zu=8e3;function Cv(e){if(e)return e.length<=Zu?e:`${e.slice(0,Zu)}
|
|
78
|
+
`).includes(r))return;d(F=>F[r]?F:{...F,[r]:!0});const C=window.setTimeout(()=>{var F;(F=document.getElementById(`id-rid-${r}`))==null||F.scrollIntoView({block:"start"})},0);return()=>window.clearTimeout(C)},[r,t,I]);function ie(){_.load(),O.load()}async function Z(C){y({rid:C,rulesetSchema:null,loading:!0,error:null});try{const F=await q(`/c/${e.eci}/query/io.picolabs.wrangler/rulesetSchema`,{rid:C},!1),K=((F==null?void 0:F.queries)||[]).filter(Ne=>Ne.view).length;if(!F||K===0){y({rid:C,rulesetSchema:F,loading:!1,error:"This ruleset has no @view functions in its compiled schema. Add @view to shared functions, then flush or reinstall."});return}y({rid:C,rulesetSchema:F,loading:!1,error:null})}catch(F){y({rid:C,rulesetSchema:null,loading:!1,error:String(F)})}}const G=new Set((((Y=_.data)==null?void 0:Y.rulesets)||[]).map(C=>C.rid)),Ce=(((Le=O.data)==null?void 0:Le.sources)||[]).filter(C=>!G.has(C.rid));function D(C,F){if(pv(e,C)){c(Xu);return}c(null),L.act({eci:e.eci,url:C,config:F})}function z(C){b()&&D(C.url,JSON.parse(o))}const B=_.data&&_.data.channels.filter(C=>C.familyChannelPicoID===null)||[];return s.jsxs("div",{children:[s.jsx(nr,{title:"Rulesets",waiting:_.waiting||O.waiting,onRefresh:ie}),s.jsx(Ue,{error:_.error}),_.data&&_.data.rulesets.length>0?_.data.rulesets.map(C=>{var lt,S,$,R;const F=C.rid,K=`id-rid-${F}`,Ne=!!m[F];return s.jsxs("div",{children:[s.jsx("div",{children:s.jsxs("div",{className:"form-check",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:K,onChange:A=>{const W=Object.assign({},m);A.target.checked?W[F]=!0:delete W[F],d(W)},checked:Ne}),s.jsx("label",{className:"form-check-label",htmlFor:K,children:s.jsxs("span",{className:"text-mono",children:[C.rid,(S=(lt=C==null?void 0:C.meta)==null?void 0:lt.krlMeta)!=null&&S.version?"@"+((R=($=C==null?void 0:C.meta)==null?void 0:$.krlMeta)==null?void 0:R.version):""]})}),s.jsx("button",{type:"button",className:"btn btn-link btn-sm py-0 align-baseline ml-2",onClick:A=>{A.preventDefault(),N(C.rid)},children:"Docs"}),(C.viewCount??0)>0?s.jsx("button",{type:"button",className:"btn btn-link btn-sm py-0 align-baseline ml-1",onClick:A=>{A.preventDefault(),Z(C.rid)},disabled:_.waiting,children:"View"}):null]})}),Ne?s.jsxs("div",{className:"ml-3",children:[s.jsxs("div",{children:[s.jsx("button",{className:"btn btn-outline-primary btn-sm",type:"button",onClick:A=>{A.preventDefault(),te.act({eci:e.eci,url:C.url})},disabled:_.waiting||te.waiting,children:"flush"})," ",s.jsx("button",{className:"btn btn-outline-danger btn-sm",type:"button",onClick:A=>{A.preventDefault(),U.act({eci:e.eci,rid:C.rid})},disabled:_.waiting||U.waiting,children:"uninstall"}),s.jsx(Ue,{error:te.error}),s.jsx(Ue,{error:U.error})]}),s.jsx("div",{children:s.jsx("a",{href:C.url,target:"_blank",children:C.url})}),s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Config:"})," ",JSON.stringify(C.config)]}),C.meta&&s.jsxs("div",{children:[s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Last flushed:"})," ",C.meta.flushed+""]}),s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Hash:"})," ",C.meta.hash]}),s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Compiler version:"})," ",C.meta.compiler.version]}),s.jsxs("div",{children:[s.jsx("b",{className:"text-muted",children:"Compiler warnings:"})," ",Array.isArray(C.meta.compiler.warnings)?s.jsx("ul",{children:C.meta.compiler.warnings.map(A=>{var ee;let W="";return A&&typeof A.message=="string"?W=`${(ee=A.loc)!=null&&ee.start?`${A.loc.start.line}:${A.loc.start.column}: `:""}${A.message}`:W=JSON.stringify(A),s.jsx("li",{children:W})})}):s.jsx("span",{})]})]})]}):""]},F)}):s.jsx("span",{className:"text-muted",children:"- no rulesets -"}),s.jsx("hr",{}),s.jsx("h4",{children:"Engine KRL Directory"}),s.jsxs("p",{className:"text-muted",children:["Rulesets shipped with this pico-engine install that are not yet on this pico. Flush always reloads from the engine's local ",s.jsx("code",{children:"krl/"})," ","directory."]}),s.jsx(Ue,{error:O.error}),O.waiting?s.jsx("span",{className:"text-muted",children:"Loading local rulesets…"}):Ce.length>0?Ce.map(C=>{const F=`krl:${C.rid}`,K=`id-krl-${C.rid}`,Ne=!!m[F],lt=!!C.rootOnly&&!fo(e);return s.jsxs("div",{children:[s.jsxs("div",{className:"form-check",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:K,onChange:S=>{const $=Object.assign({},m);S.target.checked?$[F]=!0:delete $[F],d($)},checked:Ne}),s.jsxs("label",{className:"form-check-label",htmlFor:K,children:[s.jsx("span",{className:"text-mono",children:C.rid}),C.name?s.jsxs("span",{className:"text-muted",children:[" — ",C.name]}):""]})]}),Ne?s.jsxs("div",{className:"ml-3 mb-2",children:[s.jsx("div",{className:"small text-muted",children:C.relativePath}),C.rootOnly?s.jsx("div",{className:"small text-warning",children:"Root pico only"}):"",C.description?s.jsx("div",{className:"small text-muted",children:C.description}):"",s.jsx("div",{children:s.jsx("a",{href:C.url,target:"_blank",children:C.url})}),s.jsxs("div",{className:"mt-1",children:[lt?s.jsx("span",{className:"badge badge-warning text-dark",children:"root only"}):s.jsx("button",{type:"button",className:"btn btn-outline-primary btn-sm",disabled:_.waiting||L.waiting||!b(),onClick:S=>{S.preventDefault(),z(C)},children:"install"})," ",s.jsx("button",{type:"button",className:"btn btn-outline-secondary btn-sm",disabled:_.waiting||L.waiting,onClick:S=>{S.preventDefault(),i(C.url)},children:"use URL"})]})]}):""]},C.relativePath)}):O.data?s.jsx("span",{className:"text-muted",children:"- all local rulesets installed -"}):s.jsx("span",{className:"text-muted",children:"- no local rulesets found -"}),s.jsx("hr",{}),s.jsx("h4",{children:"Install Ruleset by URL"}),!fo(e)&&am(l)?s.jsx("p",{className:"text-warning small",children:Xu}):"",s.jsxs("form",{onSubmit:C=>{C.preventDefault(),j()&&l&&D(l,JSON.parse(o))},children:[s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:"new-rs-url",children:"URL"}),s.jsxs("div",{className:"input-group",children:[s.jsx("input",{id:"new-rs-url",className:"form-control",value:l,onChange:C=>i(C.target.value),disabled:_.waiting||L.waiting,placeholder:"https://… or file://… or Browse for a local .krl file"}),s.jsx("div",{className:"input-group-append",children:s.jsx("button",{type:"button",className:"btn btn-outline-secondary",disabled:_.waiting||L.waiting,onClick:()=>{var C;return(C=w.current)==null?void 0:C.click()},children:"Browse…"})})]}),s.jsx("input",{ref:w,type:"file",accept:".krl",className:"d-none","aria-hidden":!0,tabIndex:-1,onChange:C=>{var F;E((F=C.target.files)==null?void 0:F[0])}}),s.jsx("small",{className:"form-text text-muted",children:"Browse reads the file in your browser and stages it on the engine host as a temporary file:// URL (for local development)."}),s.jsx(Ue,{error:x})]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{htmlFor:"new-rs-config",children:"Config"}),s.jsx("textarea",{id:"new-rs-config",rows:3,className:"form-control",value:o,onChange:C=>a(C.target.value),disabled:_.waiting||L.waiting})]}),s.jsx("button",{type:"submit",className:"btn btn-outline-primary",disabled:_.waiting||L.waiting||!j(),children:"Install"}),s.jsx(Ue,{error:u||L.error})]}),g&&t?s.jsx(mv,{eci:e.eci,rid:g,onClose:()=>N(null)}):null,k?s.jsx("div",{className:"modal show d-block",tabIndex:-1,role:"dialog",style:{backgroundColor:"rgba(0, 0, 0, 0.45)"},onClick:()=>y(null),children:s.jsx("div",{className:"modal-dialog modal-dialog-centered",role:"document",onClick:C=>C.stopPropagation(),children:s.jsxs("div",{className:"modal-content",children:[s.jsxs("div",{className:"modal-header",children:[s.jsxs("h5",{className:"modal-title text-mono",children:["Open view — ",k.rid]}),s.jsx("button",{type:"button",className:"close","aria-label":"Close",onClick:()=>y(null),children:s.jsx("span",{"aria-hidden":"true",children:"×"})})]}),s.jsxs("div",{className:"modal-body",children:[s.jsxs("p",{className:"small text-muted",children:["XMLUI apps load at"," ",s.jsxs("code",{children:["/ui/<eci>/",k.rid,"/"]}),". Choose a channel whose query policy allows the view queries you need."," ",s.jsx("a",{href:"https://docs.picolabs.io/#/pico-views-xmlui",target:"_blank",rel:"noreferrer",children:"Pico views (XMLUI)"})]}),k.loading?s.jsx("p",{className:"small text-muted mb-0",children:"Loading schema…"}):k.error?s.jsx("p",{className:"text-danger small mb-0",children:k.error}):s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{className:"small mb-1 d-block",children:"Channel (ECI)"}),s.jsx("select",{className:"form-control form-control-sm text-mono",value:f,onChange:C=>h(C.target.value),children:B.map(C=>{const F=C.id+" "+C.tags.map(K=>`#${K}`).join(" ");return s.jsx("option",{value:C.id,children:F},C.id)})})]}),s.jsxs("div",{className:"small text-muted mb-2",children:[s.jsx("strong",{children:"@view"})," pages:"," ",(((he=k.rulesetSchema)==null?void 0:he.queries)||[]).filter(C=>C.view).map(C=>C.name).join(", ")]}),s.jsx("a",{className:"btn btn-primary btn-sm",href:`/ui/${f}/${encodeURIComponent(k.rid)}/`,target:"_blank",rel:"noreferrer",children:"Open in new tab"})]})]})]})})}):null]})};function Pe(e){return e.current&&e.current.value}function um(e){return e.layer2===!0}function mo(e){const t=e.name||e.channel_name;if(typeof t!="string")return;const n=t.trim();return n.length>0?n:void 0}function kt({label:e,value:t,copyable:n}){const[r,l]=p.useState(!1);if(!t)return null;async function i(){if(t)try{await navigator.clipboard.writeText(t),l(!0),window.setTimeout(()=>l(!1),2e3)}catch{}}return s.jsxs("div",{className:"mb-1 d-flex align-items-start",children:[s.jsxs("div",{className:"flex-grow-1",children:[s.jsxs("b",{className:"text-muted",children:[e,":"]})," ",s.jsx("span",{className:"text-mono small",children:t})]}),n?s.jsx("button",{type:"button",className:"btn btn-sm btn-link p-0 ml-2 off-engine-copy-btn",title:r?"Copied":"Copy to clipboard","aria-label":r?"Copied":"Copy to clipboard",onClick:()=>{i()},children:r?s.jsx("span",{className:"text-success small",children:"Copied"}):s.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round","aria-hidden":"true",children:[s.jsx("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),s.jsx("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]})}):null]})}function yv({pico:e,rxEci:t,channel:n,onChannelUpdated:r}){const[l,i]=p.useState(!1),[o,a]=p.useState(""),[u,c]=p.useState(""),[m,d]=p.useState(!1),[g,N]=p.useState(null);p.useEffect(()=>{n&&(a(ro(n.eventPolicy)),c(lo(n.queryPolicy)))},[n]);async function x(v){if(v.preventDefault(),!!n){d(!0),N(null);try{await q(`/c/${e.eci}/event/engine_ui/update_channel/query/io.picolabs.pico-engine-ui/pico`,{eci:n.id,tags:n.tags,eventPolicy:ma(o),queryPolicy:ha(u)}),r(),i(!1)}catch(k){N(String(k))}finally{d(!1)}}}return s.jsxs("div",{className:"mt-2 p-2 border rounded bg-light",children:[s.jsxs("div",{className:"d-flex justify-content-between align-items-center mb-2",children:[s.jsx("b",{className:"small",children:"Relationship Rx channel policy"}),n&&!l?s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-secondary",onClick:()=>i(!0),children:"Edit policies"}):null,l?s.jsx("button",{type:"button",className:"btn btn-sm btn-link p-0",onClick:()=>{i(!1),N(null)},children:"Cancel"}):null]}),s.jsxs("div",{className:"small text-muted text-mono mb-1",children:["Rx ECI: ",t]}),n&&l?s.jsxs("form",{onSubmit:x,children:[s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{className:"small font-weight-bold",children:"Event policy"}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("textarea",{rows:3,className:"form-control form-control-sm",value:o,onChange:v=>a(v.target.value),disabled:m})}),s.jsx("div",{className:"col",children:s.jsx(pa,{src:o})})]})]}),s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{className:"small font-weight-bold",children:"Query policy"}),s.jsxs("div",{className:"row",children:[s.jsx("div",{className:"col",children:s.jsx("textarea",{rows:3,className:"form-control form-control-sm",value:u,onChange:v=>c(v.target.value),disabled:m})}),s.jsx("div",{className:"col",children:s.jsx(ga,{src:u})})]})]}),s.jsx("button",{type:"submit",className:"btn btn-sm btn-outline-primary",disabled:m,children:"Save policies"})]}):n?s.jsxs("div",{className:"row",children:[s.jsxs("div",{className:"col",children:[s.jsx("div",{className:"small font-weight-bold",children:"Events"}),s.jsx(Kf,{policy:n.eventPolicy})]}),s.jsxs("div",{className:"col",children:[s.jsx("div",{className:"small font-weight-bold",children:"Queries"}),s.jsx(Qf,{policy:n.queryPolicy})]})]}):s.jsx("div",{className:"text-muted small",children:"Channel details not loaded — reload the tab or expand this relationship again."}),g?s.jsx("div",{className:"text-danger small mt-1",children:g}):null]})}function vv({pico:e,sub:t,rxChannel:n,onDelete:r,onRefresh:l,onChannelUpdated:i,showSend:o,deleteLabel:a="Cancel relationship"}){const u=um(t),c=p.useRef(null),m=p.useRef(null),[d,g]=p.useState(null),[N,x]=p.useState(!1);async function v(k){k.preventDefault(),g(null),x(!1);const y=Pe(c)||"wrangler",f=Pe(m)||"ping";try{await q(`/c/${e.eci}/event/wrangler/send_event_on_subs`,{subID:t.Id,domain:y,type:f,attrs:{uiTest:!0}}),x(!0)}catch(h){g(String(h))}}return s.jsxs("div",{className:"ml-3 mb-3",children:[u?s.jsx("span",{className:"badge badge-primary mb-2",children:"DID"}):s.jsx("span",{className:"badge badge-secondary mb-2",children:"ECI"}),s.jsx(kt,{label:"Remote Tx DID",value:t.Tx_did}),s.jsx(kt,{label:"Local Rx DID",value:t.Rx_did}),!u&&t.Tx?s.jsx(kt,{label:"Remote Tx ECI",value:t.Tx}):null,t.Tx_host?s.jsx(kt,{label:"Tx host",value:t.Tx_host}):null,t.Tx_role?s.jsx(kt,{label:"Tx role",value:t.Tx_role}):null,t.Rx_role?s.jsx(kt,{label:"Rx role",value:t.Rx_role}):null,mo(t)?s.jsx(kt,{label:"Name",value:mo(t)}):null,t.Rx?s.jsx(yv,{pico:e,rxEci:t.Rx,channel:n,onChannelUpdated:i}):null,o&&u?s.jsxs("form",{className:"form-inline mt-2",onSubmit:v,children:[s.jsx("input",{type:"text",className:"form-control form-control-sm mr-1",ref:c,placeholder:"domain",defaultValue:"wrangler"}),s.jsx("input",{type:"text",className:"form-control form-control-sm mr-1",ref:m,placeholder:"type",defaultValue:"ping"}),s.jsx("button",{type:"submit",className:"btn btn-sm btn-outline-primary",children:"Send test event"})]}):null,N?s.jsx("div",{className:"text-success small mt-1",children:"Event sent."}):null,d?s.jsx("div",{className:"text-danger small mt-1",children:d}):null,s.jsxs("details",{className:"mt-2",children:[s.jsx("summary",{className:"small text-muted",children:"Raw relationship record"}),s.jsx("pre",{className:"small mb-0",children:JSON.stringify(t,null,2)})]}),r?s.jsx("button",{className:"btn btn-outline-danger btn-sm mt-2",type:"button",onClick:r,children:a}):null]})}function xv({pico:e,myDid:t,publicIntro:n,wellKnownRx:r,onPublicIntroChange:l}){const[i,o]=p.useState(!1),[a,u]=p.useState(null);async function c(m){o(!0),u(null);try{await q(`/c/${e.eci}/event/wrangler/set_public_intro`,{enabled:m}),l(m)}catch(d){u(String(d))}finally{o(!1)}}return s.jsxs("div",{className:"mb-4 p-3 border rounded",children:[s.jsx("h5",{className:"h6 mb-3",children:"Identity"}),s.jsx(kt,{label:"did:webvh (myDid)",value:t||void 0,copyable:!0}),r?s.jsx(kt,{label:"wellKnown_Rx (ECI-based)",value:r}):null,s.jsxs("div",{className:"form-check mt-2",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:`public-intro-${e.eci}`,checked:n===!0,disabled:i||n===null,onChange:m=>c(m.target.checked)}),s.jsx("label",{className:"form-check-label",htmlFor:`public-intro-${e.eci}`,children:"Public intro (accept unsolicited SKY intro on did:webvh)"})]}),a?s.jsx("div",{className:"text-danger small mt-1",children:a}):null]})}const wv=({pico:e})=>{const[t,n]=p.useState([]),[r,l]=p.useState([]),[i,o]=p.useState([]),[a,u]=p.useState(""),[c,m]=p.useState(null),[d,g]=p.useState(null),[N,x]=p.useState({}),[v,k]=p.useState("did"),[y,f]=p.useState(null),h=p.useRef(null),w=p.useRef(null),b=p.useRef(null),j=p.useRef(null),E=p.useRef(null),_=p.useRef(null),O=p.useRef(null),L=p.useRef(null),U=p.useRef(null),te=p.useRef(null),I=p.useRef(null),ie=p.useRef(null),Z=p.useRef(null),G=nn(null,()=>ye(`/c/${e.eci}/query/io.picolabs.pico-engine-ui/pico`)),Ce=G.data&&G.data.channels||[];function D($){if($)return Ce.find(R=>R.id===$)}function z(){B(),J(),Y(),Le(),he(),G.load(),fe.refreshRelationships()}const B=()=>{ye(`/c/${e.eci}/query/io.picolabs.subscription/wellKnown_Rx`).then($=>{u(($==null?void 0:$.id)||"")}).catch(()=>u(""))},J=()=>{ye(`/c/${e.eci}/query/io.picolabs.wrangler/myDid`).then($=>m($?String($):null)).catch(()=>m(null)),ye(`/c/${e.eci}/query/io.picolabs.wrangler/publicIntro`).then($=>g(!!$)).catch(()=>g(null))},Y=()=>{ye(`/c/${e.eci}/query/io.picolabs.subscription/established`).then($=>{n($||[])})},Le=()=>{ye(`/c/${e.eci}/query/io.picolabs.subscription/inbound`).then($=>{o($||[])})},he=()=>{ye(`/c/${e.eci}/query/io.picolabs.subscription/outbound`).then($=>{l($||[])})};p.useEffect(()=>{z()},[e.eci]);const C=async $=>{await q(`/c/${e.eci}/event/wrangler/pending_relationship_approval`,{Id:$}),z()},F=async $=>{await q(`/c/${e.eci}/event/wrangler/outbound_relationship_cancellation`,{Id:$}),z()},K=async $=>{await q(`/c/${e.eci}/event/wrangler/relationship_cancellation`,{Id:$}),z()};async function Ne($){$.preventDefault(),f(null);const R=(Pe(h)||"").trim();if(!R.startsWith("did:")){f("target_did must start with did:");return}if(c&&R===c){f("Cannot form a relationship with yourself (target_did equals myDid).");return}try{await q(`/c/${e.eci}/event/wrangler/relationship`,{layer2:!0,target_did:R,name:Pe(w)||null,Rx_role:Pe(b)||null,Tx_role:Pe(j)||null,channel_type:Pe(E)||"Tx_Rx",password:Pe(_)||null}),he(),fe.refreshRelationships()}catch(A){f(String(A))}}async function lt($){$.preventDefault(),f(null);const R=(Pe(O)||"").trim();if(a&&R===a){f("Cannot form a relationship with yourself (wellKnown_Tx equals your wellKnown_Rx).");return}try{await q(`/c/${e.eci}/event/wrangler/relationship`,{wellKnown_Tx:Pe(O)||"",Rx_role:Pe(L)||null,Tx_role:Pe(U)||null,name:Pe(te)||null,channel_type:Pe(I)||null,Tx_host:Pe(ie)||null,password:Pe(Z)||null}),he()}catch(A){f(String(A))}}function S($,R){return $.map(A=>{const W=!!N[A.Id],ee=mo(A);return s.jsxs("div",{children:[s.jsxs("div",{className:"form-check",children:[s.jsx("input",{className:"form-check-input",type:"checkbox",id:`${R.keyPrefix}-sub-${A.Id}`,checked:W,onChange:P=>{const V={...N};P.target.checked?V[A.Id]=!0:delete V[A.Id],x(V)}}),s.jsxs("label",{className:"form-check-label",htmlFor:`${R.keyPrefix}-sub-${A.Id}`,children:[ee?s.jsxs(s.Fragment,{children:[s.jsx("span",{className:"font-weight-bold",children:ee}),s.jsx("span",{className:"text-muted small ml-2 text-mono",children:A.Id})]}):s.jsx("span",{className:"text-mono",children:A.Id}),um(A)?s.jsx("span",{className:"badge badge-primary ml-1",children:"DID"}):null]})]}),W?s.jsxs(s.Fragment,{children:[s.jsx(vv,{pico:e,sub:A,rxChannel:D(A.Rx),showSend:R.showSend,onRefresh:z,onChannelUpdated:()=>{G.load()},deleteLabel:R.deleteLabel,onDelete:R.onDelete&&!R.onAccept?()=>{R.onDelete(A.Id)}:void 0}),R.onAccept?s.jsx("div",{className:"ml-3 mb-3",children:s.jsx("button",{className:"btn btn-outline-primary btn-sm",type:"button",onClick:()=>{R.onAccept(A.Id)},children:"Accept"})}):null]}):null]},`${R.keyPrefix}-${A.Id}`)})}return s.jsxs("div",{children:[s.jsx(nr,{title:"Relationships",waiting:G.waiting,onRefresh:z}),s.jsx(Ue,{error:G.error}),s.jsx(xv,{pico:e,myDid:c,publicIntro:d,wellKnownRx:a,onPublicIntroChange:g}),t.length>0?s.jsx("h5",{children:"Established"}):null,S(t,{keyPrefix:"est",onDelete:K,deleteLabel:"Delete relationship",showSend:!0}),i.length>0?s.jsx("h5",{className:"mt-3",children:"Inbound pending"}):null,S(i,{keyPrefix:"in",onAccept:C}),r.length>0?s.jsx("h5",{className:"mt-3",children:"Outbound pending"}):null,S(r,{keyPrefix:"out",onDelete:F,deleteLabel:"Cancel outbound"}),s.jsxs("div",{className:"mt-4",children:[s.jsx("h5",{children:"New relationship"}),s.jsxs("ul",{className:"nav nav-pills mb-2",children:[s.jsx("li",{className:"nav-item",children:s.jsx("button",{type:"button",className:"nav-link"+(v==="did"?" active":""),onClick:()=>k("did"),children:"DID-based"})}),s.jsx("li",{className:"nav-item",children:s.jsx("button",{type:"button",className:"nav-link"+(v==="legacy"?" active":""),onClick:()=>k("legacy"),children:"ECI-based"})})]}),v==="did"?s.jsxs("form",{onSubmit:Ne,children:[s.jsxs("p",{className:"text-muted small",children:["Introduce via SKY using the peer's ",s.jsx("code",{children:"did:webvh"})," ","(",s.jsx("b",{children:"myDid"})," on their Relationships tab). Share your"," ",s.jsx("b",{children:"myDid"})," above when they form a relationship with you."]}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{children:"target_did"}),s.jsx("input",{type:"text",className:"form-control",ref:h,placeholder:"did:webvh:…",required:!0})]}),s.jsxs("div",{className:"form-row",children:[s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"name"}),s.jsx("input",{type:"text",className:"form-control",ref:w})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Rx_role"}),s.jsx("input",{type:"text",className:"form-control",ref:b})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Tx_role"}),s.jsx("input",{type:"text",className:"form-control",ref:j})]})]}),s.jsxs("div",{className:"form-row",children:[s.jsxs("div",{className:"form-group col-md-6",children:[s.jsx("label",{children:"channel_type"}),s.jsx("input",{type:"text",className:"form-control",ref:E,defaultValue:"Tx_Rx"})]}),s.jsxs("div",{className:"form-group col-md-6",children:[s.jsx("label",{children:"password (optional)"}),s.jsx("input",{type:"text",className:"form-control",ref:_})]})]}),s.jsx("button",{type:"submit",className:"btn btn-outline-primary",children:"Request relationship"})]}):s.jsxs("form",{onSubmit:lt,children:[s.jsx("p",{className:"text-muted small",children:"Classic introduction via the peer's wellKnown_Rx ECI."}),s.jsxs("div",{className:"form-group",children:[s.jsx("label",{children:"wellKnown_Tx"}),s.jsx("input",{type:"text",className:"form-control",ref:O,placeholder:"Remote wellKnown_Rx ECI"})]}),s.jsxs("div",{className:"form-row",children:[s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Rx_role"}),s.jsx("input",{type:"text",className:"form-control",ref:L})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Tx_role"}),s.jsx("input",{type:"text",className:"form-control",ref:U})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"name"}),s.jsx("input",{type:"text",className:"form-control",ref:te})]})]}),s.jsxs("div",{className:"form-row",children:[s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"channel_type"}),s.jsx("input",{type:"text",className:"form-control",ref:I})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"Tx_host"}),s.jsx("input",{type:"text",className:"form-control",ref:ie})]}),s.jsxs("div",{className:"form-group col-md-4",children:[s.jsx("label",{children:"password"}),s.jsx("input",{type:"text",className:"form-control",ref:Z})]})]}),s.jsx("button",{type:"submit",className:"btn btn-outline-secondary",children:"Request legacy relationship"})]}),y?s.jsx("div",{className:"text-danger small mt-2",children:y}):null]})]})};function jv(e){if(!e)return null;const t=[],n=new Set;for(const l of e.operations||[]){const i=(l.attrs||[]).map(a=>a.name),o=l.examples||[];for(const a of l.entry||[]){if(!a.domain||!a.name)continue;const u=`${a.domain}:${a.name}`;if(o.length===0){if(n.has(u))continue;n.add(u),t.push({domain:a.domain,name:a.name,attrs:i.length?i:void 0});continue}for(const c of o){const m=`${u}:${c.name}`;if(n.has(m))continue;n.add(m);const d={};for(const[N,x]of Object.entries(c.attrs||{}))typeof x=="string"||typeof x=="number"||typeof x=="boolean"?d[N]=x:x!=null&&(d[N]=JSON.stringify(x));const g=Object.keys(d).length>0?Object.keys(d):i;t.push({domain:a.domain,name:a.name,attrs:g.length?g:void 0,exampleName:c.name,defaults:Object.keys(d).length?d:void 0})}}}const r=[];for(const l of e.queries||[]){if(l.view)continue;const i=(l.args||[]).map(a=>a.name),o=l.examples||[];if(o.length===0){r.push({name:l.name,args:i.length?i:void 0});continue}for(const a of o){const u={};for(const[c,m]of Object.entries(a.args||{}))typeof m=="string"||typeof m=="number"||typeof m=="boolean"?u[c]=m:m!=null&&(u[c]=JSON.stringify(m));r.push({name:l.name,args:Object.keys(u).length>0?Object.keys(u):i.length?i:void 0,exampleName:a.name,defaults:Object.keys(u).length?u:void 0})}}return t.length===0&&r.length===0?null:{events:t,queries:r}}function Nv(e){if(!e)return;const t={};for(const[n,r]of Object.entries(e))typeof r=="string"||typeof r=="number"||typeof r=="boolean"?t[n]=r:r!=null&&(t[n]=JSON.stringify(r));return Object.keys(t).length?t:void 0}function Sv(e){const t=[],n=(e.attrs||[]).map(l=>l.name),r=e.examples||[];for(const l of e.entry||[])if(!(!l.domain||!l.name)){if(r.length===0){t.push({domain:l.domain,name:l.name,attrs:n.length?n:void 0});continue}for(const i of r){const o=Nv(i.attrs);t.push({domain:l.domain,name:l.name,attrs:o&&Object.keys(o).length>0?Object.keys(o):n.length?n:void 0,exampleName:i.name,defaults:o})}}return{events:t}}function cm(e){const t=e.currentTarget,n={};for(let r=0;r<t.elements.length;r++){const l=t.elements[r];if(l&&l.name){let i=l.value;if(i==="")continue;if(l instanceof HTMLInputElement&&l.type==="number"){n[l.name]=Number(i);continue}try{i=JSON.parse(String(i))}catch{}n[l.name]=i}}return n}function kv(e){if(e!==void 0)return typeof e=="object"?JSON.stringify(e):String(e)}const dm=({name:e,meta:t,defaultValue:n,exampleAsPlaceholder:r=!1})=>{const l=(t==null?void 0:t.type)||"Any",i=[e,l!=="Any"?l:null,t!=null&&t.required?"required":null,t==null?void 0:t.description].filter(Boolean).join(" · ")||e,o=kv(n),a=r&&o!==void 0,u=a?`${i} · e.g. ${o}`:i,c=a?void 0:n!==void 0?String(n):void 0;if(l==="Boolean"){const m=a?"":c==="true"?"true":c==="false"?"false":"";return s.jsxs("select",{name:e,className:"form-control form-control-sm d-inline-block w-auto mr-1 mb-1",defaultValue:m,title:a&&o?`e.g. ${o}`:void 0,children:[s.jsx("option",{value:"",children:a&&o?`— (e.g. ${o})`:"—"}),s.jsx("option",{value:"true",children:"true"}),s.jsx("option",{value:"false",children:"false"})]})}return l==="Number"?s.jsx("input",{type:"number",name:e,className:"form-control form-control-sm d-inline-block w-auto mr-1 mb-1",placeholder:u,defaultValue:c}):l==="Map"||l==="Array"?s.jsx("textarea",{name:e,className:"form-control form-control-sm d-block mb-1 text-mono",rows:2,placeholder:u+" (JSON)",defaultValue:c!==void 0&&typeof n=="object"?JSON.stringify(n):c}):s.jsx("input",{type:"text",name:e,className:"form-control form-control-sm d-inline-block w-auto mr-1 mb-1",placeholder:u,defaultValue:c})},Gu=({domain:e,name:t,attrs:n,defaults:r,exampleName:l,attrMeta:i,testingEci:o,formId:a,className:u="border border-warning p-2 mt-1",showInlineResult:c=!1,exampleAsPlaceholder:m=!1,onResult:d})=>{const[g,N]=p.useState(!1),[x,v]=p.useState(null),[k,y]=p.useState(null);async function f(w){w.preventDefault();const b=cm(w);N(!0),v(null),y(null),d==null||d(null,null);try{const j=await q(`/c/${o}/event-wait/${e}/${t}`,b,!1),E=JSON.stringify(j,void 0,2);c?v(E):d==null||d(E,null,b)}catch(j){const E=String(j);c?y(E):d==null||d(null,E,b)}finally{N(!1)}}const h=`${e}:${t}`;return s.jsxs("form",{id:a,className:u,onSubmit:f,children:[s.jsx("div",{children:(n||[]).map(w=>{const b=r&&r[w],j=i==null?void 0:i.find(E=>E.name===w);return s.jsx(dm,{name:w,meta:j,defaultValue:b,exampleAsPlaceholder:m},w)})}),s.jsxs("button",{type:"submit",className:"btn btn-sm btn-warning",disabled:g,children:[g?"Sending…":h,l?` (${l})`:""]}),c&&k?s.jsx("pre",{className:"small text-danger mt-2 mb-0",children:k}):null,c&&x?s.jsx("pre",{className:"small bg-light p-2 mt-2 mb-0",children:x}):null]})},bv=({rid:e,name:t,args:n,defaults:r,exampleName:l,argMeta:i,testingEci:o,formId:a,className:u="border border-primary p-2 mb-2",showInlineResult:c=!1,exampleAsPlaceholder:m=!1,onResult:d})=>{const[g,N]=p.useState(!1),[x,v]=p.useState(null),[k,y]=p.useState(null);async function f(h){h.preventDefault();const w=cm(h);N(!0),v(null),y(null),d==null||d(null,null);try{const b=await q(`/c/${o}/query/${e}/${t}`,w,!1),j=JSON.stringify(b,void 0,2);c?v(j):d==null||d(j,null,w)}catch(b){const j=String(b);c?y(j):d==null||d(null,j,w)}finally{N(!1)}}return s.jsxs("form",{id:a,className:u,onSubmit:f,children:[s.jsx("div",{children:(n||[]).map(h=>{const w=r&&r[h],b=i==null?void 0:i.find(j=>j.name===h);return s.jsx(dm,{name:h,meta:b,defaultValue:w,exampleAsPlaceholder:m},h)})}),s.jsxs("button",{type:"submit",className:"btn btn-sm btn-primary",disabled:g,children:[g?"Running…":t,l?` (${l})`:""]}),c&&k?s.jsx("pre",{className:"small text-danger mt-2 mb-0",children:k}):null,c&&x?s.jsx("pre",{className:"small bg-light p-2 mt-2 mb-0",children:x}):null]})},ho=({text:e,className:t=""})=>{const[n,r]=p.useState(!1);async function l(){try{await navigator.clipboard.writeText(e),r(!0),window.setTimeout(()=>r(!1),2e3)}catch{window.prompt("Copy:",e)}}return s.jsx("button",{type:"button",className:"btn btn-sm btn-link p-0 align-self-start "+t,title:n?"Copied":"Copy to clipboard","aria-label":n?"Copied":"Copy to clipboard",onClick:()=>void l(),children:n?s.jsx("span",{className:"text-success small",children:"✓"}):s.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round","aria-hidden":"true",children:[s.jsx("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),s.jsx("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]})})},Ev=/password|secret|token|apikey|api_key|credential/i,Zu=8e3;function Cv(e){if(e)return e.length<=Zu?e:`${e.slice(0,Zu)}
|
|
79
79
|
…`}function fm(e){if(!e)return e;const t={};for(const[n,r]of Object.entries(e))t[n]=Ev.test(n)?"••••••":r;return t}function _v(e){if(!e.ok)return e.error?`error: ${e.error}`:"error";const t=e.ruleOutcomes||[];if(t.length===0)return"ok";const n=t.filter(o=>o.status==="fired"),r=t.filter(o=>o.status==="not_fired"),l=[];n.length&&l.push(`fired: ${n.map(o=>`${o.rid}/${o.rule}`).join(", ")}`),r.length&&l.push(`not fired: ${r.map(o=>`${o.rid}/${o.rule}`).join(", ")}`);const i=t.filter(o=>o.status!=="fired"&&o.status!=="not_fired");return i.length&&l.push(`other: ${i.map(o=>`${o.rid}/${o.rule} (${o.status})`).join(", ")}`),l.join("; ")||"ok"}function mm(){return typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():`h-${Date.now()}-${Math.random().toString(36).slice(2,9)}`}async function Rv(e,t){const n=await q(`/c/${e}/query/io.picolabs.pico-engine-ui/testingHistory`,{},!1);if(Array.isArray(n))return n;if(n&&typeof n=="object"){const r=[];for(const l of Object.values(n))Array.isArray(l)&&r.push(...l);return r.sort((l,i)=>l.time<i.time?1:l.time>i.time?-1:0),r}return[]}async function Pv(e,t,n){await q(`/c/${e}/event/engine_ui/testing_history_record`,{rid:t,entry:n},!1)}async function Tv(e,t){await q(`/c/${e}/event/engine_ui/testing_history_clear`,t?{rid:t}:{},!1)}function ls(e){if(!e||typeof e!="object"||!("error"in e))return null;const t=e.error;return t==null||t===""?null:typeof t=="string"?t:JSON.stringify(t)}function ec(e){if(!e)return null;try{return ls(JSON.parse(e))}catch{return null}}function Ov(e){if(!e)return{};try{const t=JSON.parse(e);return{eid:t.eid,ruleOutcomes:t.ruleOutcomes}}catch{return{}}}function hm(e){if(e.kind==="query"&&!e.queryName&&e.label){const t=e.label.match(/^([^ (]+)/);if(t)return{...e,queryName:t[1]}}if(e.kind==="event"&&(!e.domain||!e.eventName)&&e.label.includes(":")){const t=e.label.match(/^([^:(]+):([^ (]+)/);if(t)return{...e,domain:t[1],eventName:t[2]}}return e}function pm(e){const t=hm(e);return t.kind==="console"?!0:t.kind==="query"?!!t.queryName:t.kind==="event"?!!(t.domain&&t.eventName):!1}async function Lv(e,t){const n=hm(t),r=n.payload||{};try{if(n.kind==="console"){const l=await q(`/c/${e}/query/${n.rid}/__consoleEval`,{expr:n.label},!1);return{result:JSON.stringify(l,void 0,2),error:ls(l)}}if(n.kind==="query"&&n.queryName){const l=await q(`/c/${e}/query/${n.rid}/${n.queryName}`,r,!1);return{result:JSON.stringify(l,void 0,2),error:ls(l)}}if(n.kind==="event"&&n.domain&&n.eventName){const l=await q(`/c/${e}/event-wait/${n.domain}/${n.eventName}`,r,!1);return{result:JSON.stringify(l,void 0,2),error:null}}return{result:null,error:"Cannot re-run: missing send details"}}catch(l){return{result:null,error:String(l)}}}const $v=/password|secret|token|apikey|api_key|credential/i;function gm(e){if(!e)return;const t={};for(const[n,r]of Object.entries(e))$v.test(n)||r!=="••••••"&&(t[n]=r);return Object.keys(t).length?t:void 0}function Iv(e,t){return{id:mm(),name:t.trim(),savedAt:new Date().toISOString(),kind:e.kind,rid:e.rid,label:e.label,domain:e.domain,eventName:e.eventName,queryName:e.queryName,exampleName:e.exampleName,payload:gm(e.payload)}}function tc(e){return{kind:e.kind,rid:e.rid,label:e.label,domain:e.domain,eventName:e.eventName,queryName:e.queryName,exampleName:e.exampleName,payload:e.payload}}function nc(e){return{kind:e.kind,rid:e.rid,label:e.label,domain:e.domain,eventName:e.eventName,queryName:e.queryName,exampleName:e.exampleName,payload:e.payload}}function Av(e,t){if(e.kind==="console")return null;const n=t.trim();if(!n)return null;const r=n.replace(/\\/g,"\\\\").replace(/"/g,'\\"'),l=gm(e.payload)||{};return` * @example "${r}" ${JSON.stringify(l)}`}async function Dv(e,t){const n=await q(`/c/${e}/query/io.picolabs.pico-engine-ui/testingSaved`,{},!1);if(Array.isArray(n))return n;if(n&&typeof n=="object"){const r=[];for(const l of Object.values(n))Array.isArray(l)&&r.push(...l);return r.sort((l,i)=>l.savedAt<i.savedAt?1:l.savedAt>i.savedAt?-1:0),r}return[]}async function Mv(e,t,n){await q(`/c/${e}/event/engine_ui/testing_saved_upsert`,{rid:t,entry:n},!1)}async function Fv(e,t,n){await q(`/c/${e}/event/engine_ui/testing_saved_delete`,{rid:t,id:n},!1)}const Uv=e=>e==="query"?s.jsx("span",{className:"badge badge-primary ml-1",children:"query"}):e==="event"?s.jsx("span",{className:"badge badge-warning ml-1",children:"event"}):s.jsx("span",{className:"badge badge-secondary ml-1",children:"console"}),zv=({entries:e,loading:t,filterRid:n,onSave:r,onRerun:l,onCopyExample:i,onClear:o})=>{const a=n?e.filter(u=>u.rid===n):e;return s.jsxs("details",{className:"mt-3 testing-panel-details",open:a.length>0,children:[s.jsxs("summary",{className:"h6 mb-0 testing-panel-summary",children:["History (",a.length,")",a.length>0?s.jsx("button",{type:"button",className:"btn btn-link btn-sm py-0 ml-2",onClick:u=>{u.preventDefault(),u.stopPropagation(),o(n)},children:"Clear"}):null]}),t?s.jsx("div",{className:"small text-muted mt-2",children:"Loading…"}):a.length===0?s.jsx("div",{className:"small text-muted mt-2",children:"Sends from this tab appear here (stored on this pico)."}):s.jsx("ul",{className:"list-unstyled small mt-2 mb-0",children:a.map(u=>{const c=fm(u.payload);return s.jsxs("li",{className:"border-bottom pb-2 mb-2",children:[s.jsxs("div",{className:"d-flex align-items-start",children:[s.jsxs("div",{className:"flex-grow-1 min-width-0",children:[s.jsx("code",{children:u.label}),Uv(u.kind)]}),u.kind==="console"?s.jsx(ho,{text:u.label,className:"ml-1 flex-shrink-0"}):null]}),s.jsx("div",{className:"text-muted small",children:u.time}),s.jsx("div",{className:"text-muted text-mono",children:u.rid}),s.jsx("div",{className:u.ok?"text-success":"text-danger",children:_v(u)}),s.jsxs("div",{className:"mt-1",children:[s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 mr-2",onClick:()=>r(u),children:"Save"}),s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 mr-2",disabled:!pm(u),onClick:()=>l(u),children:"Re-run"}),u.kind!=="console"?s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 mr-2",onClick:()=>i(u),children:"Copy @example"}):null]}),u.result?s.jsxs("div",{className:"d-flex align-items-start mt-1",children:[s.jsx("pre",{className:"small bg-light p-1 mb-0 flex-grow-1",children:u.result}),s.jsx(ho,{text:u.result,className:"ml-1 mt-1"})]}):null,c&&Object.keys(c).length>0?s.jsx("pre",{className:"small bg-light p-1 mb-0 mt-1",children:JSON.stringify(c,void 0,2)}):null]},u.id)})})]})},Bv=({entries:e,loading:t,filterRid:n,onRerun:r,onCopyExample:l,onDelete:i})=>{const o=n?e.filter(a=>a.rid===n):e;return s.jsxs("details",{className:"mt-3 testing-panel-details",open:o.length>0,children:[s.jsxs("summary",{className:"h6 mb-0 testing-panel-summary",children:["Saved (",o.length,")"]}),t?s.jsx("div",{className:"small text-muted mt-2",children:"Loading…"}):o.length===0?s.jsx("div",{className:"small text-muted mt-2",children:"Save a history entry to keep a named request on this pico."}):s.jsx("ul",{className:"list-unstyled small mt-2 mb-0",children:o.map(a=>s.jsxs("li",{className:"border-bottom pb-2 mb-2",children:[s.jsx("div",{children:s.jsx("strong",{children:a.name})}),s.jsx("div",{className:"text-muted small",children:a.savedAt}),s.jsx("div",{children:s.jsx("code",{children:a.label})}),s.jsx("div",{className:"text-muted text-mono",children:a.rid}),s.jsxs("div",{className:"mt-1",children:[s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 mr-2",disabled:!pm(a),onClick:()=>r(a),children:"Re-run"}),a.kind!=="console"?s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 mr-2",onClick:()=>l(a),children:"Copy @example"}):null,s.jsx("button",{type:"button",className:"btn btn-link btn-sm p-0 text-danger",onClick:()=>i(a),children:"Delete"})]}),a.payload&&Object.keys(a.payload).length>0?s.jsx("pre",{className:"small bg-light p-1 mb-0 mt-1",children:JSON.stringify(a.payload,void 0,2)}):null]},a.id))})]})};function Wv(e){const t=(e.entry||[]).filter(n=>n.domain&&n.name).map(n=>`${n.domain}:${n.name}`);return t.length?t.join(", "):e.rule}function gl(e,t){return`${e}:${t}`}function ym(e){return`pico-engine-ui-testing-rid:${e}`}function rc(e){try{const t=localStorage.getItem(ym(e));return t&&t.length?t:null}catch{return null}}function ni(e,t){try{localStorage.setItem(ym(e),t)}catch{}}const Vv=({pico:e})=>{var F,K,Ne,lt;const[t]=ca(),n=nv("?"+t.toString()),r=nn(null,()=>ye(`/c/${e.eci}/query/io.picolabs.pico-engine-ui/pico`)),l=nn(null,()=>ye(`/c/${e.eci}/query/io.picolabs.pico-engine-ui/testingECI`)),[i,o]=p.useState(e.eci),[a,u]=p.useState(null),[c,m]=p.useState(!1),[d,g]=p.useState(null),[N,x]=p.useState({}),[v,k]=p.useState(""),[y,f]=p.useState(""),[h,w]=p.useState(!1),[b,j]=p.useState([]),[E,_]=p.useState(!1),[O,L]=p.useState([]),[U,te]=p.useState(!1);async function I(){_(!0);try{j(await Rv(e.eci))}catch{j([])}finally{_(!1)}}async function ie(){te(!0);try{L(await Dv(e.eci))}catch{L([])}finally{te(!1)}}function Z(){r.load(),l.load(),I(),ie(),x({})}p.useEffect(()=>{r.load(),l.load(),x({}),k(rc(e.eci)||""),I(),ie()},[e.eci]),p.useEffect(()=>{var A;const S=((A=r.data)==null?void 0:A.rulesets)||[];if(!S.length)return;if(n!=null&&n.rid&&S.some(W=>W.rid===n.rid)){k(n.rid),ni(e.eci,n.rid);return}if(v&&S.some(W=>W.rid===v))return;const $=rc(e.eci);if($&&S.some(W=>W.rid===$)){k($);return}const R=S[0].rid;k(R),ni(e.eci,R)},[e.eci,r.data,n==null?void 0:n.rid,v]),p.useEffect(()=>{l.data&&o(l.data)},[l.data]),p.useEffect(()=>{if(!v)return;const S=gl(e.eci,v);N[S]===void 0&&Y(v)},[v,e.eci,N]),p.useEffect(()=>{var A,W,ee;if(!(n!=null&&n.rid)||n.rid!==v)return;const S=N[gl(e.eci,v)];if(!(S!=null&&S.rulesetSchema)&&!(S!=null&&S.schema))return;let $=null;if(n.query)$=Qu(n.rid,n.query,n.example);else if(n.rule){const P=(W=(A=S.rulesetSchema)==null?void 0:A.operations)==null?void 0:W.find(Me=>Me.rule===n.rule),V=(ee=P==null?void 0:P.entry)==null?void 0:ee.find(Me=>Me.domain&&Me.name);V!=null&&V.domain&&V.name&&($=n.example?Ju({rid:n.rid,rule:n.rule,domain:V.domain,name:V.name,exampleName:n.example}):Ku(n.rid,n.rule))}if(!$)return;const R=window.setTimeout(()=>{var P;(P=document.getElementById($))==null||P.scrollIntoView({behavior:"smooth",block:"nearest"})},100);return()=>window.clearTimeout(R)},[n,N,v]);async function G(S,$,R){const A=R||ec($)||null;u(A),g($),m(!1);const W=S.kind==="console"?void 0:fm(S.payload),ee=W!==void 0&&Object.keys(W).length>0;let P={id:mm(),time:new Date().toISOString(),kind:S.kind,rid:S.rid,label:S.label,domain:S.domain,eventName:S.eventName,queryName:S.queryName,exampleName:S.exampleName,eci:i,payload:ee?W:void 0,ok:!A,error:A||void 0};!A&&$&&(S.kind==="console"||S.kind==="query")&&(P.result=Cv($)),S.kind==="event"&&$&&!A&&Object.assign(P,Ov($));try{await Pv(e.eci,S.rid,P),j(V=>[P,...V].slice(0,50))}catch(V){console.error(V)}}async function Ce(S){S.rid!==v&&k(S.rid),S.kind==="console"&&f(S.label),m(!0),u(null),g(null);const{result:$,error:R}=await Lv(i,S);await G(S,$,R)}async function D(S){const $=S.exampleName||S.label.slice(0,48)||"Saved request",R=window.prompt("Name for saved request",$);if(!(R!=null&&R.trim()))return;const A=Iv(S,R);try{await Mv(e.eci,S.rid,A),L(W=>[A,...W.filter(ee=>ee.id!==A.id)])}catch(W){window.alert(String(W))}}async function z(S){const $=S.exampleName||"from testing",R=window.prompt("@example name",$);if(!(R!=null&&R.trim()))return;const A=Av(S,R);if(!A){window.alert("Console expressions are not @example annotations. Copy the expression from the label instead.");return}try{await navigator.clipboard.writeText(A)}catch{window.prompt("Copy this line into your ruleset doc comment:",A)}}async function B(S){const $=S?`history for ${S}`:"all testing history on this pico";if(window.confirm(`Clear ${$}? Saved requests are kept.`))try{await Tv(e.eci,S),j(S?R=>R.filter(A=>A.rid!==S):[])}catch(R){window.alert(String(R))}}async function J(S){if(window.confirm(`Delete saved request "${S.name}"?`))try{await Fv(e.eci,S.rid,S.id),L($=>$.filter(R=>R.id!==S.id))}catch($){window.alert(String($))}}async function Y(S){var R,A;const $=gl(e.eci,S);x(W=>({...W,[$]:{waiting:!0,error:null}}));try{let W=null,ee=null,P;try{const V=await q(`/c/${e.eci}/query/io.picolabs.wrangler/rulesetSchema`,{rid:S},!1);V===null?P="rulesetSchema returned null on this pico (ruleset not installed here, or flush/reinstall to refresh compiled schema).":(R=V.operations)!=null&&R.length||(A=V.queries)!=null&&A.length?(ee=V,W=jv(V)):P="This ruleset has no discovery annotations (no annotated operations or shared queries)."}catch(V){P=`rulesetSchema query failed: ${V}`}if(!W)try{W=await ye(`/c/${e.eci}/query/${S}/__testing`)}catch{}x(V=>({...V,[$]:{schema:W||void 0,rulesetSchema:ee||void 0,schemaNote:P}}))}catch(W){x(ee=>({...ee,[$]:{error:W+""}}))}}const Le=r.data?r.data.rulesets:[],he=r.data&&r.data.channels.filter(S=>S.familyChannelPicoID===null)||[],C=v&&N[gl(e.eci,v)]||null;return s.jsx("div",{children:s.jsx(Yf,{storageKey:"pico-engine-ui-testing-split",defaultLeftRatio:.55,left:s.jsxs(s.Fragment,{children:[s.jsx(nr,{title:"Testing",waiting:r.waiting||l.waiting||E||U,onRefresh:Z}),s.jsxs("div",{className:"mt-3 mb-3",children:[s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{className:"small mb-1 d-block",children:"ECI"}),s.jsx("select",{className:"form-control form-control-sm text-mono",value:i,onChange:S=>{o(S.target.value),q(`/c/${e.eci}/event/engine_ui/testing_eci`,{eci:S.target.value}).catch(console.error)},children:he.map(S=>{const $=S.id+" "+S.tags.map(R=>`#${R}`).join(" ");return s.jsx("option",{value:S.id,children:$},S.id)})})]}),s.jsxs("div",{className:"form-group mb-0",children:[s.jsx("label",{className:"small mb-1 d-block",children:"Ruleset"}),s.jsx("select",{className:"form-control form-control-sm text-mono",value:v,onChange:S=>{const $=S.target.value;k($),ni(e.eci,$)},children:Le.map(S=>s.jsx("option",{value:S.rid,children:S.rid},S.rid))})]})]}),s.jsxs("div",{className:"border rounded p-2 mb-3 bg-light",children:[s.jsx("h4",{className:"h6 mb-2",children:"KRL console"}),s.jsxs("p",{className:"small text-muted mb-2",children:["Evaluate a read-only expression in the selected ruleset's context (globals, modules, ",s.jsx("code",{children:"ent:"})," reads)."]}),s.jsxs("form",{className:"mb-0",onSubmit:async S=>{if(S.preventDefault(),!v.trim()||!y.trim())return;w(!0),m(!0),u(null),g(null);const $=y.trim();try{const R=await q(`/c/${i}/query/${v}/__consoleEval`,{expr:$},!1),A=JSON.stringify(R,void 0,2);await G({kind:"console",rid:v,label:$},A,ls(R))}catch(R){await G({kind:"console",rid:v,label:$},null,String(R))}finally{w(!1)}},children:[s.jsx("textarea",{className:"form-control form-control-sm text-mono mb-2",rows:3,placeholder:"e.g. ent:settings or wrangler:pico()",value:y,onChange:S=>f(S.target.value)}),s.jsx("button",{type:"submit",className:"btn btn-sm btn-secondary",disabled:h||!v||!y.trim(),children:h?"Evaluating…":"Evaluate"})]})]}),v?s.jsxs("div",{className:"mb-3",children:[s.jsx(Ue,{error:C==null?void 0:C.error}),C!=null&&C.schema||C!=null&&C.rulesetSchema?s.jsxs("div",{children:[(((F=C.schema)==null?void 0:F.queries)||[]).map((S,$)=>{var W,ee;const R=(ee=(W=C.rulesetSchema)==null?void 0:W.queries)==null?void 0:ee.find(P=>P.name===S.name),A=S.exampleName?`${S.name}:${S.exampleName}`:S.name;return s.jsx(bv,{rid:v,name:S.name,args:S.args,defaults:S.defaults,exampleName:S.exampleName,argMeta:R==null?void 0:R.args,testingEci:i,exampleAsPlaceholder:!0,formId:Qu(v,S.name,S.exampleName),onResult:(P,V,Me)=>{P!==null||V!==null?G({kind:"query",rid:v,label:S.exampleName?`${S.name} (${S.exampleName})`:S.name,queryName:S.name,exampleName:S.exampleName,payload:Me},P,V||(P?ec(P):null)):(m(!0),u(null),g(null))}},A+$)}),(Ne=(K=C.rulesetSchema)==null?void 0:K.operations)!=null&&Ne.length?C.rulesetSchema.operations.map(S=>{const $=Sv(S).events||[];return s.jsxs("div",{id:Ku(v,S.rule),className:"border border-secondary p-2 mb-2",children:[s.jsxs("div",{children:[s.jsx("strong",{children:Wv(S)}),s.jsxs("span",{className:"text-muted small",children:[" ","(",S.rule,")"]})]}),S.description?s.jsx("div",{className:"small text-muted mb-1",children:S.description}):null,S.prechecks&&S.prechecks.length>0?s.jsxs("div",{className:"small mb-1",children:["Check first:"," ",S.prechecks.map((R,A)=>s.jsxs("span",{children:[A>0?"; ":"",s.jsx("code",{children:lm(R)})]},A))]}):null,S.condition?s.jsxs("div",{className:"small mb-1",children:["Acts when:"," ",s.jsx("code",{className:"text-mono",children:S.condition})]}):null,S.select?s.jsxs("details",{className:"small mb-1",children:[s.jsx("summary",{children:"Select expression"}),s.jsx("pre",{className:"small mb-0 text-mono",children:S.select})]}):null,S.emits&&S.emits.length>0?s.jsxs("details",{className:"small mb-1",children:[s.jsx("summary",{children:"May emit (possible, not guaranteed)"}),s.jsx("ul",{className:"mb-0 pl-3",children:S.emits.map((R,A)=>s.jsx("li",{children:tv(R,S.condition)},A))})]}):null,S.alsoSelectedBy&&S.alsoSelectedBy.length>0?s.jsxs("details",{className:"small mb-1",children:[s.jsxs("summary",{children:["Also selected by ",S.alsoSelectedBy.length," ","rule(s)"]}),s.jsx("ul",{className:"mb-0 pl-3",children:S.alsoSelectedBy.map(R=>s.jsxs("li",{children:[s.jsx("code",{children:R.rule}),R.condition?s.jsxs("span",{children:[" ","— ",s.jsx("code",{children:R.condition})]}):null]},R.rule))})]}):null,$.map((R,A)=>{const W=`${R.domain}:${R.name}`,ee=R.exampleName?`${W}:${R.exampleName}`:W;return s.jsx(Gu,{domain:R.domain,name:R.name,attrs:R.attrs,defaults:R.defaults,exampleName:R.exampleName,attrMeta:S.attrs,testingEci:i,exampleAsPlaceholder:!0,formId:Ju({rid:v,rule:S.rule,domain:R.domain,name:R.name,exampleName:R.exampleName}),onResult:(P,V,Me)=>{P!==null||V!==null?G({kind:"event",rid:v,label:R.exampleName?`${W} (${R.exampleName})`:W,domain:R.domain,eventName:R.name,exampleName:R.exampleName,payload:Me},P,V):(m(!0),u(null),g(null))}},ee+A)})]},S.rule)}):(((lt=C.schema)==null?void 0:lt.events)||[]).map((S,$)=>{const R=`${S.domain}:${S.name}`,A=S.exampleName?`${R}:${S.exampleName}`:R;return s.jsx(Gu,{domain:S.domain,name:S.name,attrs:S.attrs,defaults:S.defaults,exampleName:S.exampleName,testingEci:i,exampleAsPlaceholder:!0,className:"border border-warning p-2 mb-2",onResult:(W,ee,P)=>{W!==null||ee!==null?G({kind:"event",rid:v,label:S.exampleName?`${R} (${S.exampleName})`:R,domain:S.domain,eventName:S.name,exampleName:S.exampleName,payload:P},W,ee):(m(!0),u(null),g(null))}},A+$)})]}):C!=null&&C.waiting?s.jsx("div",{className:"text-muted small",children:"Loading testing forms…"}):s.jsxs("div",{className:"text-muted small",children:[C!=null&&C.schemaNote?s.jsx("p",{className:"text-warning mb-2",children:C.schemaNote}):null,"No testing forms for this ruleset (no annotations and no legacy __testing query)."]})]}):null]}),right:s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"float-right",children:[s.jsx("span",{className:"text-muted",children:"Legend:"}),s.jsx("span",{className:"badge badge-primary ml-1",children:"query"}),s.jsx("span",{className:"badge badge-warning ml-1",children:"event"}),s.jsx("span",{className:"badge badge-secondary ml-1",children:"console"})]}),s.jsx("h3",{children:"Results"}),s.jsx(Ue,{error:a}),c?"Testing...":d?s.jsxs("div",{className:"d-flex align-items-start",children:[s.jsx("pre",{className:"flex-grow-1 mb-0",children:d}),s.jsx(ho,{text:d,className:"ml-2 mt-1"})]}):"",s.jsx(Bv,{entries:O,loading:U,filterRid:v||void 0,onRerun:S=>void Ce(tc(S)),onCopyExample:S=>void z(tc(S)),onDelete:S=>void J(S)}),s.jsx(zv,{entries:b,loading:E,filterRid:v||void 0,onSave:S=>void D(S),onRerun:S=>void Ce(nc(S)),onCopyExample:S=>void z(nc(S)),onClear:S=>void B(S)})]})})})};function bn(e,t,n){let r=`/pico/${e.eci}`;return n&&(r+="/"+n),s.jsx("li",{className:"nav-item",children:s.jsx(Jg,{className:"nav-link",to:r,end:!0,children:t})})}function Hv({pico:e,openTab:t}){const n=typeof t=="string"?t.toLowerCase().trim():"",r=n==="rulesets"||n==="channels"||n==="logging"||n==="testing"||n==="subscriptions"||n==="relationships"?n:"about";return s.jsxs(s.Fragment,{children:[s.jsx("div",{hidden:r!=="about",children:s.jsx(xy,{pico:e,isTabActive:r==="about"})}),s.jsx("div",{hidden:r!=="rulesets",children:s.jsx(gv,{pico:e,isTabActive:r==="rulesets"})}),s.jsx("div",{hidden:r!=="channels",children:s.jsx(My,{pico:e})}),s.jsx("div",{hidden:r!=="logging",children:s.jsx(Gy,{pico:e,isTabActive:r==="logging"})}),s.jsx("div",{hidden:r!=="testing",children:s.jsx(Vv,{pico:e})}),s.jsx("div",{hidden:r!=="relationships"&&r!=="subscriptions",children:s.jsx(wv,{pico:e})})]})}const qv=e=>{const{pico:t,openEci:n,openTab:r}=e,l=t.eci===n,i=fe.use(),o=i.picoMoving&&i.picoMoving.eci===t.eci,a=p.useRef(null);function u(m){const d=a.current;fe.setPicoMoving({eci:t.eci,action:"moving",relX:d?m.clientX-d.offsetLeft:0,relY:d?m.clientY-d.offsetTop:0})}function c(m){m.stopPropagation();const d=a.current;fe.setPicoMoving({eci:t.eci,action:"resizing",relX:d?m.clientX-d.offsetLeft:0,relY:d?m.clientY-d.offsetTop:0})}return s.jsx("div",{ref:a,className:"card pico"+(l?" open":"")+(o?"":" pico-not-moving-or-resizing"),style:{left:t.x,top:t.y,maxWidth:t.width,maxHeight:t.height,backgroundColor:t.backgroundColor},onMouseDown:l?void 0:u,children:l?s.jsxs(p.Fragment,{children:[s.jsx(ft,{to:"/",className:"btn btn-light position-absolute",style:{right:5,top:5},children:"Close"}),s.jsxs("div",{className:"card-header",children:[s.jsx("div",{className:"card-header-pico-name",style:{color:Uu(t.backgroundColor)},children:t.name}),s.jsxs("ul",{className:"nav nav-tabs card-header-tabs",children:[bn(t,"About"),bn(t,"Rulesets","rulesets"),bn(t,"Channels","channels"),bn(t,"Logging","logging"),bn(t,"Testing","testing"),bn(t,"Relationships","relationships")]})]}),s.jsx("div",{className:"card-body bg-white overflow-auto",children:s.jsx(Hv,{pico:t,openTab:r})})]}):s.jsxs(p.Fragment,{children:[s.jsx("div",{className:"card-body",children:s.jsx(ft,{to:"/pico/"+t.eci,className:"pico-name-open-btn",onMouseDown:m=>m.stopPropagation(),style:{color:Uu(t.backgroundColor)},children:t.name})}),s.jsx("div",{className:"pico-resize-handle",onMouseDown:c})]})})};function Kv(e){try{return new Date(e).toLocaleString()}catch{return e}}function lc(e){return e.length<=16?e:e.slice(0,8)+"…"+e.slice(-6)}function ri({id:e,title:t,summary:n,open:r,onToggle:l,children:i}){return s.jsxs("div",{className:"settings-section border rounded mb-3",children:[s.jsxs("button",{type:"button",id:`${e}-toggle`,className:"settings-section-toggle btn btn-link btn-block text-left d-flex align-items-center justify-content-between","aria-expanded":r,"aria-controls":`${e}-panel`,onClick:l,children:[s.jsx("span",{className:"h6 mb-0 text-dark",children:t}),s.jsx("span",{className:"settings-section-chevron text-muted","aria-hidden":"true",children:r?"▾":"▸"})]}),!r&&n?s.jsx("div",{className:"settings-section-summary px-3 pb-2 text-muted small",children:n}):"",r?s.jsx("div",{id:`${e}-panel`,className:"settings-section-body px-3 pb-3",children:i}):""]})}const Qv=({open:e,onClose:t,onLogout:n,allowSelfSignup:r=!1})=>{const[l,i]=p.useState(null),[o,a]=p.useState(!1),[u,c]=p.useState(!1),[m,d]=p.useState(null),[g,N]=p.useState(""),[x,v]=p.useState(""),[k,y]=p.useState(""),[f,h]=p.useState(null),[w,b]=p.useState(!1),[j,E]=p.useState(null),[_,O]=p.useState(!1),[L,U]=p.useState(null),[te,I]=p.useState(""),[ie,Z]=p.useState(""),[G,Ce]=p.useState(!0),[D,z]=p.useState(!1),[B,J]=p.useState(null),[Y,Le]=p.useState({passkeys:!0,invite:!1,oauth:!1});function he(P){Le(V=>({...V,[P]:!V[P]}))}const C=p.useCallback(async()=>{O(!0),U(null);try{E(await Cy())}catch(P){E(null),U(P+"")}finally{O(!1)}},[]),F=p.useCallback(async()=>{a(!0),d(null);try{const P=await ey();if(!P.authenticated)throw new Error("Not signed in");i(P)}catch(P){d(P+"")}finally{a(!1)}},[]);p.useEffect(()=>{e&&(N(""),v(""),h(null),I(""),Z(""),Ce(!0),J(null),Le({passkeys:!0,invite:!1,oauth:!1}),F(),C())},[e,F,C]),p.useEffect(()=>{if(!e)return;function P(V){V.key==="Escape"&&t()}return window.addEventListener("keydown",P),()=>window.removeEventListener("keydown",P)},[e,t]);async function K(){b(!0),d(null);try{const P=await ny(x.trim()||void 0,k.trim()||void 0);h(ly(P.token)),v(""),y("")}catch(P){d(P+"")}finally{b(!1)}}async function Ne(){if(f)try{await navigator.clipboard.writeText(f)}catch(P){d(P+"")}}async function lt(){z(!0),d(null),J(null);try{const P=ie.split(/\r?\n/).map(Me=>Me.trim()).filter(Boolean),V=await _y({name:te.trim(),redirect_uris:P,public_client:G});J(V),I(""),Z(""),Le(Me=>({...Me,oauth:!0})),await C()}catch(P){d(P+"")}finally{z(!1)}}async function S(P){if(window.confirm(`Remove OAuth app “${P.name}”?`)){z(!0),d(null);try{await Ry(P.client_id),await C()}catch(V){d(V+"")}finally{z(!1)}}}async function $(){c(!0),d(null);try{const P=await gt("/auth/credentials/options",{}),V=await no({optionsJSON:P});await gt("/auth/credentials/verify",{...V,label:g.trim()||"passkey"}),N(""),await F()}catch(P){d(P+"")}finally{c(!1)}}async function R(P){if(((l==null?void 0:l.credentials)||[]).length<=1){d("You must keep at least one passkey on your account.");return}if(window.confirm(`Remove passkey “${P.label||lc(P.credentialID)}”?`)){c(!0),d(null);try{await ty(`/auth/credentials/${encodeURIComponent(P.credentialID)}`),await F()}catch(Me){d(Me+"")}finally{c(!1)}}}if(!e)return null;const A=(l==null?void 0:l.credentials)||[],W=A.length===0?"No passkeys":A.length===1?"1 passkey":`${A.length} passkeys`,ee=_?"Loading…":L?"Unavailable":j&&j.length>0?j.length===1?"1 app":`${j.length} apps`:"No apps registered";return s.jsx("div",{className:"settings-modal-backdrop",onClick:t,children:s.jsxs("div",{className:"settings-modal card shadow",role:"dialog","aria-modal":"true","aria-labelledby":"settings-modal-title",onClick:P=>P.stopPropagation(),children:[s.jsxs("div",{className:"card-header d-flex align-items-center justify-content-between",children:[s.jsx("h2",{id:"settings-modal-title",className:"h5 mb-0",children:"Settings"}),s.jsx("button",{type:"button",className:"close","aria-label":"Close",onClick:t,children:s.jsx("span",{"aria-hidden":"true",children:"×"})})]}),s.jsxs("div",{className:"card-body",children:[o?s.jsx("p",{className:"text-muted mb-0",children:"Loading…"}):"",!o&&l?s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"mb-4",children:[s.jsx("div",{className:"text-muted small",children:"Signed into"}),s.jsx("div",{className:"font-weight-bold",children:l.displayName||"Account"})]}),s.jsxs(ri,{id:"settings-passkeys",title:"Passkeys",summary:W,open:Y.passkeys,onToggle:()=>he("passkeys"),children:[A.length===0?s.jsx("p",{className:"text-muted small mb-3",children:"No passkeys found."}):s.jsx("ul",{className:"list-group mb-3",children:A.map(P=>s.jsxs("li",{className:"list-group-item d-flex align-items-start justify-content-between",children:[s.jsxs("div",{className:"mr-3",children:[s.jsx("div",{className:"font-weight-bold",children:P.label||"passkey"}),s.jsx("div",{className:"text-muted small text-mono",children:lc(P.credentialID)}),s.jsxs("div",{className:"text-muted small",children:["Added ",Kv(P.createdAt)]})]}),s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-danger",disabled:u||A.length<=1,title:A.length<=1?"Cannot remove your only passkey":"Remove passkey",onClick:()=>R(P),children:"Remove"})]},P.credentialID))}),s.jsx("h4",{className:"h6",children:"Add passkey"}),s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{htmlFor:"passkeyLabel",className:"small mb-1",children:"Label (optional)"}),s.jsx("input",{id:"passkeyLabel",className:"form-control form-control-sm",value:g,onChange:P=>N(P.target.value),placeholder:"e.g. MacBook, YubiKey",disabled:u})]}),s.jsx("button",{type:"button",className:"btn btn-sm btn-primary",disabled:u,onClick:$,children:u?"Waiting for passkey…":"Add passkey"})]}),r?"":s.jsxs(ri,{id:"settings-invite",title:"Invite someone",summary:"Create a single-use registration link",open:Y.invite,onToggle:()=>he("invite"),children:[s.jsx("p",{className:"text-muted small mb-2",children:"Create a single-use link so someone can register when self-signup is disabled."}),s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{htmlFor:"inviteLabel",className:"small mb-1",children:"Label (optional)"}),s.jsx("input",{id:"inviteLabel",className:"form-control form-control-sm",value:x,onChange:P=>v(P.target.value),placeholder:"e.g. Alex",disabled:w||u})]}),s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{htmlFor:"inviteBootstrapUrl",className:"small mb-1",children:"Bootstrap ruleset URL (optional)"}),s.jsx("input",{id:"inviteBootstrapUrl",className:"form-control form-control-sm text-mono",value:k,onChange:P=>y(P.target.value),placeholder:"file://… or https://…/bootstrap.krl",disabled:w||u}),s.jsx("small",{className:"form-text text-muted",children:"Installed on the new root when the invite is accepted. Validated when you create the invite."})]}),s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-primary",disabled:w||u,onClick:K,children:w?"Creating…":"Create invite link"}),f?s.jsxs("div",{className:"mt-3",children:[s.jsx("label",{htmlFor:"inviteLink",className:"small mb-1",children:"Invite link"}),s.jsxs("div",{className:"input-group input-group-sm",children:[s.jsx("input",{id:"inviteLink",className:"form-control text-mono",value:f,readOnly:!0}),s.jsx("div",{className:"input-group-append",children:s.jsx("button",{type:"button",className:"btn btn-outline-secondary",onClick:Ne,children:"Copy"})})]})]}):""]}),s.jsxs(ri,{id:"settings-oauth",title:"OAuth apps",summary:ee,open:Y.oauth,onToggle:()=>he("oauth"),children:[s.jsxs("p",{className:"text-muted small mb-2",children:["Register apps like Home Assistant for authorization-code access across this mesh. Install ",s.jsx("code",{children:"io.picolabs.oauth"})," on the root pico first."]}),_?s.jsx("p",{className:"text-muted small mb-0",children:"Loading apps…"}):L?s.jsx("p",{className:"text-muted small mb-0",children:L}):j&&j.length>0?s.jsx("ul",{className:"list-group mb-3",children:j.map(P=>s.jsxs("li",{className:"list-group-item d-flex align-items-start justify-content-between",children:[s.jsxs("div",{className:"mr-3 flex-grow-1",children:[s.jsx("div",{className:"font-weight-bold",children:P.name}),s.jsx("label",{className:"small mb-0 text-muted",children:"Client ID"}),s.jsx("input",{className:"form-control form-control-sm text-mono mb-1",value:P.client_id,readOnly:!0,onFocus:V=>V.target.select()}),s.jsx("div",{className:"text-muted small",children:P.public_client?"Public (PKCE)":"Confidential"}),s.jsx("div",{className:"text-muted small",children:P.redirect_uris.join(", ")})]}),s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-danger",disabled:D||u,onClick:()=>S(P),children:"Remove"})]},P.client_id))}):j?s.jsx("p",{className:"text-muted small",children:"No OAuth apps registered."}):"",s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{htmlFor:"oauthAppName",className:"small mb-1",children:"App name"}),s.jsx("input",{id:"oauthAppName",className:"form-control form-control-sm",value:te,onChange:P=>I(P.target.value),placeholder:"Home Assistant",disabled:D||u})]}),s.jsxs("div",{className:"form-group mb-2",children:[s.jsx("label",{htmlFor:"oauthRedirectUris",className:"small mb-1",children:"Redirect URIs (one per line)"}),s.jsx("textarea",{id:"oauthRedirectUris",className:"form-control form-control-sm text-mono",rows:3,value:ie,onChange:P=>Z(P.target.value),placeholder:"https://homeassistant.local:8123/auth/external/callback",disabled:D||u})]}),s.jsxs("div",{className:"form-check mb-2",children:[s.jsx("input",{id:"oauthPublicClient",className:"form-check-input",type:"checkbox",checked:G,onChange:P=>Ce(P.target.checked),disabled:D||u}),s.jsx("label",{className:"form-check-label small",htmlFor:"oauthPublicClient",children:"Public client (PKCE; typical for Home Assistant)"})]}),s.jsx("button",{type:"button",className:"btn btn-sm btn-primary",disabled:D||u,onClick:lt,children:D?"Registering…":"Register app"}),B?s.jsxs("div",{className:"mt-3",children:[s.jsxs("div",{className:"alert alert-success py-2 small mb-2",children:["App registered. Copy the client ID",B.client_secret?" and client secret (shown once)":""," ","into your integrator."]}),s.jsx("label",{className:"small mb-1",children:"Client ID"}),s.jsx("input",{className:"form-control form-control-sm text-mono mb-2",value:B.client_id,readOnly:!0,onFocus:P=>P.target.select()}),B.client_secret?s.jsxs(s.Fragment,{children:[s.jsx("label",{className:"small mb-1",children:"Client secret"}),s.jsx("input",{className:"form-control form-control-sm text-mono",value:B.client_secret,readOnly:!0})]}):""]}):""]})]}):"",m?s.jsx("div",{className:"alert alert-danger py-2",children:m}):""]}),s.jsxs("div",{className:"card-footer d-flex justify-content-between",children:[s.jsx("button",{type:"button",className:"btn btn-outline-secondary",onClick:t,disabled:u,children:"Close"}),l?s.jsx("button",{type:"button",className:"btn btn-outline-danger",disabled:u,onClick:n,children:"Log out"}):""]})]})})},li=()=>{var u,c,m;let e=jg();const t=e.eci,n=e.tab,r=fe.use(),[l,i]=p.useState(!1);p.useEffect(()=>{fe.fetchAllWithBootstrapRetry()},[]);function o(d){if(r.picoMoving)switch(r.picoMoving.action){case"moving":{r.picoMoving.offEngine?fe.updateOffEnginePosition(d.clientX-(r.picoMoving.relX||0),d.clientY-(r.picoMoving.relY||0)):r.picoMoving.eci&&fe.updateBox(r.picoMoving.eci,{x:d.clientX-(r.picoMoving.relX||0),y:d.clientY-(r.picoMoving.relY||0)});break}case"resizing":{const g=r.picoMoving.eci;if(!g)break;const N=r.picoBoxes[g];N&&fe.updateBox(g,{width:Math.max(0,d.clientX-N.x),height:Math.max(0,d.clientY-N.y)});break}}}function a(d){if(r.picoMoving){if(r.picoMoving.offEngine)fe.persistOffEnginePosition();else if(r.picoMoving.eci){const g=r.picoBoxes[r.picoMoving.eci];g&&Bf(g.eci,{x:g.x,y:g.y,width:g.width,height:g.height})}fe.setPicoMoving(null)}}return s.jsxs("div",{id:"picos-page",onMouseMove:o,onMouseUp:a,children:[s.jsxs("footer",{className:"fixed-bottom-right p-5",children:[s.jsxs("div",{children:["Powered by"," ",s.jsx("a",{href:"https://github.com/Picolab/pico-engine",children:"pico-engine"})]}),s.jsx("div",{children:r.uiContext?`version: ${r.uiContext.version}`:""}),(c=(u=r.uiContext)==null?void 0:u.session)!=null&&c.displayName?s.jsxs("div",{children:["Signed into ",r.uiContext.session.displayName]}):"",s.jsxs("div",{className:"mt-2",children:[s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-secondary mr-2",disabled:r.loading,onClick:()=>fe.fetchAll(),children:r.loading?"Refreshing…":"Refresh"}),s.jsx("button",{type:"button",className:"btn btn-sm btn-outline-secondary",onClick:()=>i(!0),children:"Settings"})]}),!1]}),s.jsx(Qv,{open:l,onClose:()=>i(!1),onLogout:()=>fe.logout(),allowSelfSignup:((m=r.uiContext)==null?void 0:m.allowSelfSignup)===!0}),s.jsxs("div",{className:"container-fluid",children:[r.loading?"Loading...":"",r.error?s.jsx("div",{className:"alert alert-danger",children:r.error}):""]}),Object.values(r.picoBoxes).map(d=>s.jsx(qv,{pico:d,openEci:t,openTab:n},d.eci+d.x+d.y+d.width+d.height)),r.offEngineNode?s.jsx(gy,{node:r.offEngineNode}):null,s.jsx("svg",{id:"picos-svg",children:r.channelLines.map((d,g)=>s.jsx("line",{x1:d.from.x,y1:d.from.y,x2:d.to.x,y2:d.to.y},g))}),s.jsx("svg",{id:"relationships-svg",children:r.relationshipLines.map((d,g)=>s.jsx("line",{strokeDasharray:"4",x1:d.from.x,y1:d.from.y,x2:d.to.x,y2:d.to.y},g))})]})},Jv=()=>{var k,y;const[e,t]=p.useState(null),[n,r]=p.useState(!0),[l,i]=p.useState(null),[o,a]=p.useState(0),u=p.useMemo(()=>Af(),[o]),[c,m]=p.useState(!1),d=p.useCallback(()=>{a(f=>f+1)},[]),g=p.useCallback(async()=>{r(!0),i(null);try{t(await Zg())}catch(f){i(f+"")}finally{r(!1)}},[]),N=p.useCallback(async()=>{const f=ry();kr(),d(),await g(),f&&window.location.assign(f)},[g,d]);return p.useEffect(()=>{g()},[g]),p.useEffect(()=>{var h;if(!u||!((h=e==null?void 0:e.session)!=null&&h.authenticated)){m(!1);return}let f=!1;return m(!0),$f(u).then(w=>{if(!f){if(!w.valid){kr(),d(),m(!1);return}return Lf().then(()=>g())}}).finally(()=>{f||m(!1)}),()=>{f=!0}},[u,(k=e==null?void 0:e.session)==null?void 0:k.authenticated,g,d]),n||c?s.jsx("div",{className:"container py-5",children:"Loading…"}):l?s.jsx("div",{className:"container py-5 alert alert-danger",children:l}):e?((y=e.session)==null?void 0:y.authenticated)===!0?s.jsx(qg,{children:s.jsxs(Dg,{children:[s.jsx(Rl,{path:"/pico/:eci/:tab",element:s.jsx(li,{})}),s.jsx(Rl,{path:"/pico/:eci",element:s.jsx(li,{})}),s.jsx(Rl,{path:"*",element:s.jsx(li,{})})]})}):s.jsx(fy,{context:e,onAuthenticated:N}):s.jsx("div",{className:"container py-5",children:"Unable to load engine context."})},vm=document.createElement("DIV");document.body.append(vm);const Yv=vf(vm);Yv.render(s.jsx(Jv,{}));
|
package/public/.DS_Store
DELETED
|
Binary file
|
package/public/docs/README.md
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
# pico-engine NEXT
|
|
2
|
-
|
|
3
|
-
NEXT is a rewrite of pico-engine that introduces major improvements.
|
|
4
|
-
|
|
5
|
-
* Ruleset versioning and sharing to facilitate decentralized code sharing.
|
|
6
|
-
* A more pico-centric architecture that makes pico's easier to transfer between hosts.
|
|
7
|
-
* Better channel policies.
|
|
8
|
-
* A new UI written in React and only uses pico channels to render.
|
|
9
|
-
* Built on top of [pico-framework](https://github.com/Picolab/pico-framework) and [select-when](https://github.com/Picolab/select-when).
|
|
Binary file
|
package/public/docs/_sidebar.md
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# Contributing
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
To run the pico-engine in development mode do the following:
|
|
5
|
-
|
|
6
|
-
```sh
|
|
7
|
-
$ git clone https://github.com/Picolab/pico-engine.git
|
|
8
|
-
$ cd pico-engine
|
|
9
|
-
$ npm run setup
|
|
10
|
-
$ npm start
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
That will start the server and run the test. `npm start` is simply an alias for `cd packages/pico-engine && npm start`
|
|
14
|
-
|
|
15
|
-
**NOTE about dependencies:** generally don't use `npm i`, rather use `npm run setup` from the root. [lerna](https://github.com/lerna/lerna) will link up the packages so when you make changes in one package, it will be used in others.
|
|
16
|
-
|
|
17
|
-
## Improving documentation
|
|
18
|
-
|
|
19
|
-
This documentation site is found in the [docs](https://github.com/Picolab/pico-engine/tree/master/docs) folder at the repository root. The pages are simple markdown files. It uses [docsify](https://docsify.js.org/) to serve it.
|
|
20
|
-
|
|
21
|
-
To preview changes run the docsify local server using `npm run docs`. Then open your browser to the "Listening at" url.
|
|
22
|
-
|
|
23
|
-
## Making changes
|
|
24
|
-
|
|
25
|
-
Use a branch (or fork) to do your work. When you are ready, create a pull request. That way we can review it before merging it into master. (See the git cheatsheet below)
|
|
26
|
-
|
|
27
|
-
### git cheatsheet
|
|
28
|
-
|
|
29
|
-
```sh
|
|
30
|
-
# list your checked out branches
|
|
31
|
-
git branch
|
|
32
|
-
|
|
33
|
-
# list all branches including remote ones you don't have checked out
|
|
34
|
-
git branch -a
|
|
35
|
-
|
|
36
|
-
# checkout a remote branch
|
|
37
|
-
git checkout -t origin/<branch>
|
|
38
|
-
|
|
39
|
-
# switch branches
|
|
40
|
-
git checkout <branch>
|
|
41
|
-
git checkout master
|
|
42
|
-
|
|
43
|
-
# create a new branch off master
|
|
44
|
-
git checkout -b <branch> master
|
|
45
|
-
|
|
46
|
-
# push your local branch to github
|
|
47
|
-
git push -u origin <branch>
|
|
48
|
-
|
|
49
|
-
# to update your branch with the latest in master
|
|
50
|
-
git checkout master
|
|
51
|
-
git pull
|
|
52
|
-
git checkout <branch>
|
|
53
|
-
git merge master
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
# Use github to merge your branch into master using a pull request.
|
|
57
|
-
# On github use the "new pull request" button to make a PR for merging.
|
|
58
|
-
# Once the PR is merged you can delete the branch using the button on github.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
# Even though the branch is deleted on github, it won't delete it off your local machine.
|
|
62
|
-
# to delete a local branch
|
|
63
|
-
git branch -d <branch>
|
|
64
|
-
```
|
package/public/docs/favicon.ico
DELETED
|
Binary file
|