pixie-mcp 0.5.5 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/pixie-mcp.mjs +38 -97
package/package.json
CHANGED
package/pixie-mcp.mjs
CHANGED
|
@@ -1,101 +1,42 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var P=(r,e)=>()=>(r&&(e=r(r=0)),e);function $e(r){let e="";for(let t=0;t<r.length;t+=32768)e+=String.fromCharCode.apply(null,r.subarray(t,t+32768));return btoa(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}var _e=P(()=>{"use strict"});var O,wt,yt,jn,je=P(()=>{"use strict";O="[A-Za-z0-9_-]{1,64}",wt="token|members|link|slow|suggest|project",yt=[{owner:"accounts",re:new RegExp("^/auth/")},{owner:"accounts",re:new RegExp("^/a/[A-Za-z0-9_-]{1,64}\\.png$")},{owner:"accounts",re:new RegExp("^/projects(/|$)")},{owner:"accounts",re:new RegExp(`^/documents/${O}/(${wt})$`)},{owner:"library",re:new RegExp("^/documents$")},{owner:"library",re:new RegExp("^/documents/(new|register|move|order|folders|folders/delete|trash|trash/empty)$")},{owner:"library",re:new RegExp(`^/documents/${O}(/(thumbnail\\.png|export|duplicate|rename|restore))?$`)},{owner:"library",re:new RegExp(`^/documents/${O}/history/at/\\d+/thumbnail\\.png$`)},{owner:"library",re:new RegExp("^/wall\\.png$")},{owner:"document",re:new RegExp(`^/documents/${O}/history(/at/\\d+|/\\d+)?$`)},{owner:"document",re:new RegExp(`^/documents/${O}/comments(/[A-Za-z0-9]{1,40})?$`)},{owner:"document",re:new RegExp(`^/documents/${O}/snapshots(/enabled|/[A-Za-z0-9_-]{1,128})?$`)},{owner:"document",re:new RegExp(`^/favourites(/order|/(document|project)/${O})?$`)}],jn=yt.map(({owner:r,re:e})=>({owner:r,source:e.source}))});var oe,se=P(()=>{"use strict";je();oe=["read","draw","create","duplicate","organize"]});async function Re(r){let e=new TextEncoder().encode(String(r));return $e(new Uint8Array(await crypto.subtle.digest("SHA-256",e)))}var Mn,Ae=P(()=>{"use strict";_e();se();Mn=120*1e3});function $(r=Date.now()){let e="0123456789ABCDEFGHJKMNPQRSTVWXYZ",t=(BigInt(r)&0xffffffffffffn)<<80n,n=crypto.getRandomValues(new Uint8Array(10)),o=0n;for(let i of n)o=o<<8n|BigInt(i);t|=o;let s="";for(let i=0;i<26;i++)s=e[Number(t&31n)]+s,t>>=5n;return s}function T(r){return r==null?{at:0}:typeof r=="number"?{at:r}:{frame:r}}var vt,$t,_t,H,M,ie,z=P(()=>{"use strict";vt=new TextEncoder,$t=new TextDecoder,_t=1;H=class r{#e;#n;constructor(e){this.#e=e,this.#n=e.exports}static async load(e){let t;if(e instanceof WebAssembly.Module)t=await WebAssembly.instantiate(e,{});else if(e instanceof ArrayBuffer||ArrayBuffer.isView(e))({instance:t}=await WebAssembly.instantiate(e,{}));else{let n=e instanceof Response||typeof e?.then=="function"?e:fetch(e);({instance:t}=await WebAssembly.instantiateStreaming(n,{}))}return new r(t)}get#i(){return new Uint8Array(this.#n.memory.buffer)}call(e,t={}){let n=vt.encode(JSON.stringify({m:e,a:t})),o=this.#n.sprite_alloc(n.length);if(!o)throw new Error("sprite-core: allocation failed");let s=0;try{if(this.#i.set(n,o),s=this.#n.sprite_dispatch(o,n.length),!s)throw new Error("sprite-core: dispatch returned nothing");let i=this.#i,a=i[s]|i[s+1]<<8|i[s+2]<<16|i[s+3]<<24,c=i[s+4],l=i.slice(s+5,s+5+a);if(c===_t)return l;let h=JSON.parse($t.decode(l));if(h&&h.error)throw new Error(h.error);return h}finally{s?this.#n.sprite_free_result(s):this.#n.sprite_dealloc(o,n.length)}}reset(){this.#n.sprite_reset()}newDocument(e={}){return this.call("doc.new",{w:32,h:32,name:"untitled",...e})}decodePng(e){let t=this.call("image.decode",{png:[...e]}),n=new DataView(t.buffer,t.byteOffset,t.byteLength),o=n.getUint32(0,!0),s=n.getUint32(4,!0);return{w:o,h:s,pixels:t.subarray(8)}}openSession(e,t,n=1){let{h:o}=this.call("session.new",{doc:e,author:t,seed:n});return new M(this,o)}openRoom(e,{seq:t=0}={}){let{h:n}=this.call("room.new",{doc:e,...t>0?{seq:t}:{}});return new ie(this,n)}},M=class{constructor(e,t){this.core=e,this.h=t}#e(e,t={}){return this.core.call(e,{h:this.h,...t})}state(){return this.#e("session.state")}apply(e,{now:t=Date.now(),label:n="Edit",job:o=null}={}){let s={now:t,label:n,bodies:Array.isArray(e)?e:[e]};return o&&(s.job=o),this.#e("session.apply",s).ops}remote(e){return this.#e("session.remote",{sop:e})}reject(e){return this.#e("session.reject",{op:e})}undo(e=Date.now()){return this.#e("session.undo",{now:e}).ops}redo(e=Date.now()){return this.#e("session.redo",{now:e}).ops}pending(){return this.#e("session.pending")}checkpoint({doc:e=!0}={}){return this.#e("session.checkpoint",{doc:e})}save(){return this.#e("session.save")}drawArt(e,t,n={}){return this.#e("session.drawArt",{rows:e,colors:t,...n}).bodies}drawAnimation(e,t,n={}){return this.#e("session.drawAnimation",{frames:e,colors:t,...n}).bodies}readArt(e,t,n={}){return this.#e("session.readArt",{layer:e,...T(t),...n})}cel(e,t){return this.#e("session.cel",{layer:e,...T(t)})}layerImage(e,t){return this.#e("session.layerImage",{layer:e,...T(t)})}tileCel(e,t){return this.#e("session.tileCel",{layer:e,...T(t)})}tile(e,t){return this.#e("session.tile",{tileset:e,index:t})}composite(e,{include:t="export"}={}){return this.#e("session.composite",{...T(e),include:t})}png(e,{scale:t=1,rect:n=null}={}){return this.#e("session.png",{...T(e),scale:Math.max(1,Math.round(t)||1),...n?{rect:n}:{}})}contactSheet({columns:e=4,scale:t=2}={}){return this.#e("session.sheet",{columns:e,scale:t})}thumbnail(e=48){return this.#e("session.thumbnail",{size:e})}card({frame:e=null,width:t=1200,height:n=630,framing:o=null,bleed:s=0,raw:i=!1}={}){return this.#e("session.card",{width:t,height:n,...o??{},...T(e),...s?{bleed:s}:{},...i?{raw:i}:{}})}cardLayout({frame:e=null,width:t=1200,height:n=630,framing:o=null}={}){return this.#e("session.cardLayout",{width:t,height:n,...o??{},...T(e)})}fit({width:e,height:t,margin:n=0,inscribed:o=!1,dx:s=0,dy:i=0,frame:a=null}){return this.#e("session.fit",{width:e,height:t,margin:n,inscribed:o,dx:s,dy:i,...T(a)})}close(){this.#e("session.free")}},ie=class{constructor(e,t){this.core=e,this.h=t}#e(e,t={}){return this.core.call(e,{h:this.h,...t})}state(){return this.#e("room.state")}submit(e,t=Date.now()){return this.#e("room.submit",{op:e,now:t})}since(e){return this.#e("room.since",{seq:e})}compact(){return this.#e("room.compact")}save(){return this.#e("room.save")}lease(e,t){return this.#e("room.lease",{action:e,...t})}close(){this.#e("room.free")}}});function Le(r){return JSON.stringify(r)}function Ue(r){let e=r.rle instanceof Uint8Array?r.rle:null,t=new TextEncoder().encode(JSON.stringify({...r,rle:void 0}));if(t.length>jt)return null;let n=new Uint8Array(3+t.length+(e?.length??0));return n[0]=De,n[1]=t.length&255,n[2]=t.length>>8&255,n.set(t,3),e&&n.set(e,3+t.length),n}function Rt(r){return r instanceof Uint8Array?r:r instanceof ArrayBuffer?new Uint8Array(r):ArrayBuffer.isView(r)?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):null}function ze(r){if(typeof r!="string"){let e=Rt(r);if(!e||e.length<3||e[0]!==De)return null;let t=e[1]|e[2]<<8;if(e.length<3+t)return null;try{let n=JSON.parse(new TextDecoder().decode(e.subarray(3,3+t)));if(!n||typeof n.t!="string")return null;let o=e.subarray(3+t);return n.rle=o.length?Uint8Array.from(o):null,n}catch{return null}}try{let e=JSON.parse(r);return e&&typeof e.t=="string"?e:null}catch{return null}}var x,Me,S,Ce,De,jt,Fe=P(()=>{"use strict";x={HELLO:"hello",SUBMIT:"submit",PRESENCE:"presence",STROKE:"stroke",LEASE:"lease",REVIEW:"review",PING:"ping",PROMPT:"prompt",ANSWER:"answer",STOP:"stop"},Me={DOCUMENT:"document",LIBRARY:"library"},S={WELCOME:"welcome",REHELLO:"rehello",OPS:"ops",REJECT:"reject",PRESENCE:"presence",STROKE:"stroke",LEASE:"lease",INK:"ink",SUGGESTIONS:"suggestions",SUGGESTION:"suggestion",ERROR:"error",PONG:"pong",PROMPT:"prompt",ANSWER:"answer",STOP:"stop"},Ce={OPS:"ops",SNAPSHOT:"snapshot"};De=157,jt=65535});function Be(r){return $().toLowerCase().slice(0,16)}var We=P(()=>{"use strict";z()});function Tt(r){let e=[];if(!r.length)return new Uint8Array;let t=r[0],n=r[1],o=r[2],s=r[3],i=0,a=()=>{let c=i;do{let l=c&127;c>>>=7,e.push(c?l|128:l)}while(c);e.push(t,n,o,s)};for(let c=0;c<r.length;c+=4)r[c]===t&&r[c+1]===n&&r[c+2]===o&&r[c+3]===s?i++:(a(),t=r[c],n=r[c+1],o=r[c+2],s=r[c+3],i=1);return a(),new Uint8Array(e)}function Pt(r){let e="";for(let n=0;n<r.length;n+=32768)e+=String.fromCharCode.apply(null,r.subarray(n,n+32768));return btoa(e)}function Ot(r){return Pt(Tt(r))}function He(r,e,t,n){return{t:"paintRegion",layer:r,frame:e,rect:{x:t.x,y:t.y,w:t.w,h:t.h},rle:Ot(n)}}var qe=P(()=>{"use strict"});import{readFile as ln}from"node:fs/promises";import{readFile as at,writeFile as ct,mkdir as lt,chmod as ht}from"node:fs/promises";import{readFileSync as ut}from"node:fs";import{homedir as dt,hostname as pt}from"node:os";import{join as ne}from"node:path";import{execFile as mt}from"node:child_process";var b=r=>String(r??"").replace(/\/+$/,"").replace(/^ws/,"http"),ue=r=>String(r??"").replace(/\/+$/,"").replace(/^http/,"ws");async function de(r,e){if(!r?.ok||!(r.headers.get("content-type")??"").includes("json"))return null;let t=await r.json().catch(()=>null);return!t||typeof t!="object"?null:e.some(n=>n in t)?t:null}async function pe(r,{timeoutMs:e=3e3,fetchImpl:t=fetch}={}){try{let n=await t(`${b(r)}/setup`,{signal:AbortSignal.timeout(e)});return await de(n,["accounts","expectedPort","install"])}catch{return null}}var te=new Map;function me(r,{fetchImpl:e=fetch,timeoutMs:t=3e3}={}){let n=b(r);return te.has(n)||te.set(n,it(r,{fetchImpl:e,timeoutMs:t})),te.get(n)}async function it(r,{fetchImpl:e,timeoutMs:t}){try{let n=await e(`${b(r)}/config.json`,{signal:AbortSignal.timeout(t)}),s=(await de(n,["rooms","accounts"]))?.rooms;if(typeof s=="string"&&/^(wss?|https?):\/\/./.test(s))return ue(s)}catch{}return ue(r)}var D=process.env.SPRITE_PUBLIC_SERVER||ft()||null;function ft(){return"wss://drawpixie.com"}var fe=ne(process.env.XDG_CONFIG_HOME||ne(dt(),".config"),"sprite"),re=ne(fe,"agent.json"),we=r=>String(r??"").replace(/\/+$/,"");function ye(){return`${pt().replace(/\.local$/,"")}`.slice(0,60)||"a computer"}async function ge(){try{let r=JSON.parse(await at(re,"utf8"));return r&&typeof r=="object"&&r.servers?r:{servers:{}}}catch{return{servers:{}}}}async function U(r){return process.env.SPRITE_TOKEN?{token:process.env.SPRITE_TOKEN,from:"the environment"}:(await ge()).servers[we(r)]??null}async function be(r,e){await lt(fe,{recursive:!0,mode:448});let t=await ge();t.servers[we(r)]={...e,at:Date.now()};let n=`${re}.tmp`;await ct(n,JSON.stringify(t,null,2),{mode:384}),await ht(n,384);let{rename:o}=await import("node:fs/promises");await o(n,re)}async function xe(r,{timeoutMs:e=3e3}={}){try{let t=await fetch(`${b(r)}/setup`,{signal:AbortSignal.timeout(e)});return t.ok?!!(await t.json()).accounts:!1}catch{return!1}}async function Se(r,e,{session:t=null}={}){let n=t??await U(r);if(!n?.token)return;let o=await fetch(`${b(r)}/documents/${encodeURIComponent(e)}/token`,{headers:{Authorization:`Bearer ${n.token}`}});if(o.status===401)throw new Error("this machine is signed out \u2014 pair it again");if(o.status===403)throw new Error(`no access to ${e}`);if(!o.ok)throw new Error(`could not get access to ${e}`);return(await o.json()).token}async function Ee(r,{name:e,w:t,h:n},{session:o=null}={}){let s=o??await U(r);if(!s?.token)return;let i=await fetch(`${b(r)}/documents/new`,{method:"POST",headers:{Authorization:`Bearer ${s.token}`,"Content-Type":"application/json"},body:JSON.stringify({name:e,w:t,h:n})});if(i.status===401)throw new Error("this machine is signed out \u2014 pair it again");if(!i.ok)throw new Error(`could not make a document: the server said ${i.status}`);let{id:a}=await i.json();if(typeof a!="string"||!a)throw new Error("could not make a document: the server named none");return a}async function ke(r,e,{method:t="GET",body:n=null,session:o=null}={}){let s=o??await U(r);if(!s?.token)return;let i=await fetch(`${b(r)}${e}`,{method:t,headers:{Authorization:`Bearer ${s.token}`,...n!==null?{"Content-Type":"application/json"}:{}},body:n!==null?JSON.stringify(n):void 0});if(i.status===401)throw new Error("this machine is signed out \u2014 pair it again");let a=await i.json().catch(()=>({}));if(!i.ok)throw new Error(a?.error?String(a.error):`the server said ${i.status}`);return a}function ve(r){let e=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";return new Promise(t=>mt(e,[r],()=>t()))}Ae();se();import{createServer as gt}from"node:http";import{randomBytes as Pe}from"node:crypto";var bt=300*1e3,xt=()=>Pe(32).toString("base64url"),St=()=>Pe(16).toString("base64url");function Te(r,e){return`<!doctype html><meta charset="utf-8"><title>Pixie</title><body style="background:#101014;color:#d8d8e0;font:14px/1.6 system-ui,sans-serif;display:grid;place-items:center;height:100vh;margin:0;text-align:center"><div><p style="font-size:16px">${r}</p><p style="color:#8a8a99;font-size:12px">${e}</p></div>`}async function Et({state:r,timeoutMs:e}){let t,n,o=new Promise((c,l)=>{t=c,n=l}),s=gt((c,l)=>{let h=new URL(c.url,"http://127.0.0.1");if(h.pathname!=="/callback"){l.writeHead(404).end();return}if(h.searchParams.get("state")!==r){l.writeHead(404).end();return}let u=h.searchParams.get("error");l.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}).end(u?Te("Nothing was granted.","You can close this window."):Te("This computer can now draw in your sprites.","You can close this window and go back to what you were doing.")),t(h.searchParams)});await new Promise((c,l)=>{s.once("error",l),s.listen(0,"127.0.0.1",c)});let i=setTimeout(()=>n(new Error("nobody answered the browser in time")),e);i.unref?.();let{port:a}=s.address();return{redirect:`http://127.0.0.1:${a}/callback`,wait:o,close:()=>{clearTimeout(i),s.close(),s.closeAllConnections?.()}}}async function Oe(r,{onUrl:e=()=>{},open:t=ve,label:n="the Pixie agent",scope:o=oe,timeoutMs:s=bt}={}){let i=xt(),a=await Re(i),c=St(),l=await Et({state:c,timeoutMs:s});try{let h=new URL(`${b(r)}/authorize`);h.searchParams.set("challenge",a),h.searchParams.set("state",c),h.searchParams.set("redirect",l.redirect),h.searchParams.set("scope",o.join(",")),h.searchParams.set("name",n),e(h.toString()),await t(h.toString());let u=await l.wait,d=u.get("error");if(d)throw new Error(d==="access_denied"?"you said no on that screen, so nothing was granted":`that request was refused: ${d}`);let p=u.get("code");if(!p)throw new Error("the browser came back without an authorization");let w=await fetch(`${b(r)}/auth/authorize/exchange`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({code:p,verifier:i,name:ye()})}),m=await w.json().catch(()=>({}));if(!w.ok||!m.token)throw new Error(m.error||"that authorization could not be exchanged");return await be(r,{token:m.token,name:m.name,email:m.email,scope:m.scope??null}),m}finally{l.close()}}import{realpathSync as Ne}from"node:fs";import{fileURLToPath as kt}from"node:url";function Ie(r){let e=process.argv[1];if(!e)return!1;try{return Ne(kt(r))===Ne(e)}catch{return!1}}z();import{execFile as Wt}from"node:child_process";Fe();z();var At=5e3,q=class{#e=null;constructor(e,t,n){if(!n||!n.author)throw new Error("DocumentClient needs an author");if(this.core=e,this.author=n.author,this.name=n.name||"anonymous",this.agent=n.agent===!0,this.answers=n.answers===!0,this.seed=n.seed??1,this.epoch=0,this.resumed=null,n.resume){let{h:o,dropped:s}=e.call("session.resume",{doc:t,author:this.author,seed:this.seed,seq:n.resume.seq??0,pending:n.resume.pending??[]});this.session=new M(e,o),this.resumed={seq:n.resume.seq??0,dropped:s??[]}}else this.session=e.openSession(t,this.author,this.seed);this.getToken=n.getToken??null,this.transport=null,this.connected=!1,this.welcomed=!1,this.ready=new Promise(o=>{this.#e=o}),this.participants=[],this.leases=[],this.presence=null,this.you=null,this.onChange=()=>{},this.onPresence=()=>{},this.onStroke=()=>{},this.strokePreview=!0,this.onLeases=()=>{},this.ink=null,this.onInk=()=>{},this.proposals=new Map,this.suggesting=!1,this.onSuggestions=()=>{},this.onLost=()=>{},this.waiters=new Set,this.onPrompt=()=>{},this.onAnswer=()=>{},this.onStop=()=>{},this.onStatus=()=>{}}apply(e,t={}){let n=this.session.apply(e,t);return this.#r(n),this.onChange(),n}acked(e,{timeoutMs:t=3e4}={}){let n=new Set((Array.isArray(e)?e:[e]).map(o=>o?.opId??o).filter(Boolean));return new Promise((o,s)=>{let i={ids:n,resolve:o,reject:s,timer:null};i.timer=setTimeout(()=>{this.waiters.delete(i);let a=[...n].filter(c=>this.session.pending().some(l=>l.opId===c)).length;s(new Error(`${a} operation${a===1?"":"s"} not confirmed by the room after ${Math.round(t/1e3)}s \u2014 `+(this.connected?"it is answering slowly":`the connection dropped; nothing since sequence ${this.session.state().seq} has been saved`)))},t),this.waiters.add(i),this.#n()})}#n(){if(!this.waiters.size)return;let e=new Set(this.session.pending().map(t=>t.opId));for(let t of this.waiters)[...t.ids].some(n=>e.has(n))||(this.waiters.delete(t),clearTimeout(t.timer),t.resolve())}#i(e,t){for(let n of this.waiters)n.ids.has(e)&&(this.waiters.delete(n),clearTimeout(n.timer),n.reject(new Error(t)))}undo(e=Date.now()){let t=this.session.undo(e);return this.#r(t),this.onChange(),t}redo(e=Date.now()){let t=this.session.redo(e);return this.#r(t),this.onChange(),t}setPresence(e){this.presence=e,this.connected&&this.#o({t:x.PRESENCE,...e})}sendStroke({layer:e,frame:t,rect:n,rle:o,done:s=!1}){if(!this.connected||!this.welcomed||!this.strokePreview||this.suggesting)return;this._strokeSentAt=Date.now();let i=Ue({t:x.STROKE,layer:e,frame:t,rect:n,rle:o,done:s});i&&this.#u(i)}suggest(e){if(!e.length||!this.connected||!this.welcomed)return[];let t=this.session.state().seq,n=e.map(o=>({opId:$(),author:this.author,parentSeq:t,...o}));return this.#o({t:x.SUBMIT,ops:n}),n}review(e,t){!this.connected||!this.welcomed||this.#o({t:x.REVIEW,author:e,verdict:t})}lease(e,t={}){this.#o({t:x.LEASE,action:e,...t})}prompt(e,{where:t=Me.DOCUMENT}={}){if(!this.connected)return null;let n=String(e??"").trim().slice(0,2e3);if(!n)return null;let o=$();return this.#o({t:x.PROMPT,id:o,text:n,where:t}),o}answer(e,t="",{reply:n=null,made:o=null,model:s=null}={}){return this.connected?(this.#o({t:x.ANSWER,state:e,text:String(t??"").slice(0,2e3),reply:n,made:o,model:s}),!0):!1}stopAnswer(e){return!this.connected||!e?!1:(this.#o({t:x.STOP,id:e}),!0)}get state(){return this.session.state()}checkpoint({doc:e=!0}={}){return{epoch:this.epoch,author:this.author,...this.session.checkpoint({doc:e})}}composite(e){return this.session.composite(e)}connect(e){return this.transport=e,this.pendingToken=this.getToken?this.getToken():void 0,this.pendingToken?.catch?.(()=>{}),e.onopen=async()=>{this.connected=!0,this.welcomed=!1,clearInterval(this.heartbeat),this.heartbeat=setInterval(()=>{this.connected&&this.#o({t:x.PING})},At),await this.#s()},e.onmessage=t=>this.#l(t),e.onclose=()=>{this.connected=!1,this.welcomed=!1,clearInterval(this.heartbeat),this.onStatus({connected:!1})},e.readyState===1&&e.onopen(),this}disconnect(){if(this.transport){this.connected=!1,this.welcomed=!1,clearInterval(this.heartbeat);try{this.transport.close()}catch{}this.transport=null,this.onStatus({connected:!1})}}async#s(){let e;if(this.pendingToken!==void 0){let n=this.pendingToken;this.pendingToken=void 0,e=await n}else e=this.getToken?await this.getToken():void 0;let t=this.session.state();this.#o({t:x.HELLO,author:this.author,name:this.name,token:e,since:t.seq,...this.agent?{agent:!0}:{},...this.answers?{answers:!0}:{},docId:t.doc.id})}#l(e){let t=ze(e);if(t)switch(t.t){case S.WELCOME:return this.#t(t);case S.REHELLO:return this.welcomed=!1,void this.#s();case S.OPS:return this.#c(t);case S.REJECT:return this.#h(t);case S.PRESENCE:return this.participants=t.participants||[],this.onPresence(this.participants);case S.STROKE:return this.onStroke(t);case S.LEASE:return this.leases=t.leases||[],this.onLeases(this.leases);case S.INK:return this.ink={left:t.left,cap:t.cap,refillMs:t.refillMs,nextInMs:t.nextInMs??0,at:Date.now()},this.onInk(this.ink);case S.SUGGESTIONS:return this.proposals=new Map((t.proposals??[]).map(n=>[n.author,{...n,ops:[...n.ops??[]]}])),this.onSuggestions(this.proposals);case S.SUGGESTION:{if(t.gone)return this.proposals.delete(t.author),this.onSuggestions(this.proposals,{author:t.author,name:t.name??null,gone:t.gone,by:t.by??null,applied:t.applied??0});let n=this.proposals.get(t.author)??{author:t.author,name:t.name??null,cc:t.cc??null,ops:[],at:t.at};if(n.name=t.name??n.name,n.at=t.at??n.at,t.replaced?.length){let o=new Set(t.replaced);n.ops=n.ops.filter(s=>!(s.rect?.w===1&&s.rect?.h===1&&o.has(`${s.rect.x},${s.rect.y}`)))}return n.ops.push(...t.ops??[]),this.proposals.set(t.author,n),this.onSuggestions(this.proposals,{author:t.author,added:t.ops?.length??0})}case S.PROMPT:return this.onPrompt(t);case S.ANSWER:return this.onAnswer(t);case S.STOP:return this.onStop(t);case S.ERROR:if(this.#d(t.message)){this.strokePreview=!1;return}return this.onLost({kind:t.fatal?"fatal":"protocol",message:t.message});default:return}}#t(e){this.you=e.you,this.suggesting=e.you?.suggest===!0,this.welcomed=!0;let t=this.session.pending();if(e.catchup.kind===Ce.SNAPSHOT){let{h:n,dropped:o}=this.core.call("session.resume",{doc:e.catchup.doc,author:this.author,seed:this.seed,seq:e.catchup.seq,pending:t});if(this.session.close(),this.session=new M(this.core,n),this.epoch++,o&&o.length){for(let s of o)this.#i(s,"could not be replayed onto the current document");this.onLost({kind:"rebased",ops:o,message:`${o.length} edit${o.length>1?"s":""} could not be replayed onto the current document`})}}else for(let n of e.catchup.ops||[])this.#a(n);if(this.#n(),this.participants=e.participants||[],this.leases=e.leases||[],t.length&&this.suggesting)for(let n of t)this.session.reject(n.opId);else t.length&&this.#o({t:x.SUBMIT,ops:t});this.presence&&this.#o({t:x.PRESENCE,...this.presence}),this.onStatus({connected:!0,resent:t.length}),this.onPresence(this.participants),this.onLeases(this.leases),this.onChange(),this.#e?.(),this.#e=null}#c(e){let t=!1;for(let n of e.ops||[])this.#a(n),t=!0;t&&(this.#n(),this.onChange())}#a(e){let t=this.session.remote(e);t.dropped&&t.dropped.length&&this.onLost({kind:"obsolete",ops:t.dropped,message:`${t.dropped.length} of your edits no longer apply and were dropped`})}#h(e){e.opId&&this.session.reject(e.opId),e.opId&&this.#i(e.opId,e.message),this.onLost({kind:e.kind,ops:e.opId?[e.opId]:[],message:e.message}),this.onChange()}#r(e){!e.length||!this.connected||!this.welcomed||this.#o({t:x.SUBMIT,ops:e})}#o(e){this.#u(Le(e))}#d(e){return typeof e!="string"?!1:e===`unknown message: ${x.STROKE}`?!0:e==="unreadable message"&&Date.now()-(this._strokeSentAt??0)<2e3}#u(e){if(this.transport)try{this.transport.send(e)}catch{this.connected=!1}}close(){this.disconnect(),this.session.close()}};z();We();var A=r=>{let e=String(r).replace("#","");return[parseInt(e.slice(0,2),16),parseInt(e.slice(2,4),16),parseInt(e.slice(4,6),16)]};var C=([r,e,t])=>.3*r+.59*e+.11*t;function J([r,e,t]){let n=Math.max(r,e,t),o=Math.min(r,e,t),s=n-o,i=0;return s&&(n===r?i=(e-t)/s%6:n===e?i=(t-r)/s+2:i=(r-e)/s+4,i*=60,i<0&&(i+=360)),[i,n?s/n:0,n/255]}var Ge={luminance:(r,e)=>C(A(r))-C(A(e)),hue:(r,e)=>{let[t,n]=J(A(r)),[o,s]=J(A(e));return n<.08||s<.08?n<.08&&s<.08?C(A(r))-C(A(e)):n<.08?-1:1:Math.round(t/20)!==Math.round(o/20)?t-o:C(A(r))-C(A(e))},saturation:(r,e)=>J(A(r))[1]-J(A(e))[1]};function L(r,e="hue"){let t=Ge[e]??Ge.hue;return[...r].sort(t)}qe();import{readFile as Gt}from"node:fs/promises";function Nt(r){try{return/^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(new URL(b(r)).hostname)}catch{return!1}}function Je(r,{publicServer:e=D}={}){let t=b(r);return e&&r===e?`nothing is answering at ${t} \u2014 this machine is offline, or the site is down`:Nt(r)?`nothing is answering at ${t} \u2014 start the editor with \`npm start\` in the Pixie repository, then try again`:`nothing is answering at ${t} \u2014 check the address and that whatever serves it is up`}import{readFile as It,writeFile as Ve}from"node:fs/promises";var V={type:"string",description:"#rrggbb"};async function Mt(r,e){let t=await It(r).catch(s=>{throw new Error(`could not read ${r}: ${s.code==="ENOENT"?"no such file":s.message}`)});if(t.length>8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71){if(!e.core?.decodePng)throw new Error("this server cannot decode a PNG \u2014 send rows and colors");return Ct(e.core.decodePng(t))}let o;try{o=JSON.parse(t.toString("utf8"))}catch{throw new Error(`${r} is neither a PNG nor JSON`)}if(!o||typeof o!="object"||!Array.isArray(o.rows)||typeof o.colors!="object")throw new Error(`${r} needs "rows" (an array of strings) and "colors" (a map of character to #rrggbb)`);return o}function Ct({w:r,h:e,pixels:t}){let n={},o=new Map,s=33,i=l=>{let h=o.get(l);if(h)return h;for(;;)if(s===34||s===46||s===92||s===127)s+=1;else if(s>=128&&s<161)s=161;else if(s>=55296&&s<57344)s=57344;else break;return h=String.fromCodePoint(s),s+=1,o.set(l,h),n[h]=l,h},a=l=>l.toString(16).padStart(2,"0"),c=[];for(let l=0;l<e;l++){let h="";for(let u=0;u<r;u++){let d=(l*r+u)*4,p=t[d+3];if(p===0){h+=".";continue}let w=p===255?`#${a(t[d])}${a(t[d+1])}${a(t[d+2])}`:`#${a(t[d])}${a(t[d+1])}${a(t[d+2])}${a(p)}`;h+=i(w)}c.push(h)}return{rows:c,colors:n}}var k={type:"string",description:"The job this call belongs to. May be omitted when exactly one job is open."},F={type:"string",description:"Which document. Omit to use the one your job owns. The id \u2014 the one new_sprite, get_info and list_documents hand back \u2014 or the exact name when only one document has it. In a local editor a 0-based tab index works too."},Lt=r=>({isError:!0,content:[{type:"text",text:r}]}),Ze=r=>({content:[{type:"text",text:typeof r=="string"?r:JSON.stringify(r)}]}),Ke=(r,e)=>({content:[{type:"image",data:Buffer.from(r).toString("base64"),mimeType:"image/png"},{type:"text",text:e}]}),R={w:80,h:42},Dt="40\xD721, 80\xD742, 120\xD763, 160\xD784, 240\xD7126",Ut="40\xD740, 50\xD750, 80\xD780, 100\xD7100",zt="60\xD720, 75\xD725, 150\xD750, 300\xD7100";function ae(r,e){if(!r||!e)return null;let t=r/e,n=1200/630;if(Math.abs(t-n)<.15){let o=Math.round(r/n);return`${r}\xD7${e} is ${t.toFixed(2)}:1; the link-card shape at this width is ${r}\xD7${o} \u2014 resize_canvas if it is meant to be posted.`}return Math.abs(t-1)<.1?`${r}\xD7${e} is nearly square; ${Math.max(r,e)}\xD7${Math.max(r,e)} would be a profile picture.`:Math.abs(t-3)<.25?`${r}\xD7${e} is nearly 3:1; ${r}\xD7${Math.round(r/3)} would be a profile banner.`:null}function K(r,e){return!r||!e?null:r===e?"This is square, the shape of a profile picture: in the editor, file \u25B8 Save as profile picture (400\xD7400) fits it inside the circle it will be cropped to. Tell the user.":r===3*e?"This is 3:1, the shape of a profile banner: in the editor, file \u25B8 Save as profile banner (1500\xD7500) exports it at the size X wants. Tell the user.":Math.abs(r/e-1200/630)<.02?"This is the link-card shape: the share button posts it edge to edge.":null}function Ft(r){let e=String(r??"").trim();if(!/^[A-Za-z0-9]+([-_][A-Za-z0-9]+)+$/.test(e))return e;let t=e.split(/[-_]+/).join(" ");return t[0].toUpperCase()+t.slice(1)}var Bt=[{name:"new_sprite",description:"Create a sprite and claim it. Call this FIRST \u2014 before writing a script, computing a grid, or thinking about pixels. It costs milliseconds and puts a labeled document in front of the user immediately, so they can see what you are making and where. The tab opens in the background and never steals their focus.",inputSchema:{type:"object",properties:{name:{type:"string",description:'The title, the way a person would write it: "Blue cake", not "blue-cake". Shown on the tab and in the library; the URL is minted from it. A hyphenated lowercase name is turned into words.'},w:{type:"integer",default:R.w,description:`Width in pixels. WHAT THE USER ASKED FOR WINS: a stated size or shape is used as stated, and a named artist, piece or style is matched in scale and detail. The rest is defaults for when nothing was said: ${R.w}\xD7${R.h} \u2014 1.91:1, the ratio link cards crop to, so a piece drawn on it posts edge to edge \u2014 at any size (${Dt}, 480\xD7252, 600\xD7315: examples, not limits). A PROFILE PICTURE is square and cropped to a circle \u2014 keep the subject inside it: ${Ut}. A BANNER is 3:1: ${zt}. Use a game asset's own size (8\xD78, 16\xD716, 32\xD732) only when the user asks for a tile, icon, or sprite for a game.`},h:{type:"integer",default:R.h,description:"Height in pixels. See w."},palette:{type:"array",items:V,description:"Optional starting palette."},intent:{type:"string",description:"One line on what you are making. Shown while you work."},folder:{type:"string",description:'Optional folder to file it in, by path (`food/sushi`). Made if it does not exist. Use this when the user asks for sprites "in a folder" rather than moving them afterwards.'},challenge:{type:"string",enum:["daily","weekly"],description:'"daily" when the user asks for the sprite of the day / today\'s theme / the Pixel Dailies; "weekly" for the sprite of the week / this week\'s word / Weekly Pixels. The name comes from the theme itself (pass `name` only to override), and finish_job will say how to post it as an entry. Call `challenges` first if you want to see the theme before drawing.'}},required:["intent"]},run:async(r,e)=>{let t=r.name?Ft(r.name):null,n=r.w??null,o=r.h??null;if(r.challenge){let s=await e.challenge(r.challenge);if(t??=s.theme,n===null&&o===null&&s.sizes?.length){let i=s.sizes[Math.floor((s.sizes.length-1)/2)],[a,c]=i.split(/[x×]/).map(Number);a&&c&&([n,o]=[a,c])}}if(!t)throw new Error("new_sprite needs a name, or a challenge to take one from");return e.newSprite({name:t,w:n??R.w,h:o??R.h,intent:r.intent,palette:r.palette,folder:r.folder??null,challenge:r.challenge??null})}},{name:"challenges",description:`The sprite of the day and the sprite of the week: Pixel Dailies sets a theme every day ("Today's theme is #Gateway") and Weekly Pixels a word every week, with sizes. When the user says "the sprite of the day", "today's theme", "the daily", "the weekly", "this week's word" or names either community, this is what they mean \u2014 read it here rather than asking them. The server reads the accounts and keeps the answer; nothing is fetched from Bluesky by you.`,inputSchema:{type:"object",properties:{}},run:(r,e)=>e.challenges()},{name:"begin_job",description:"Claim an existing document for one piece of work \u2014 the other way to start, where new_sprite is the one for new art. Refused by name if another job holds it, which is what keeps parallel requests from colliding.",inputSchema:{type:"object",properties:{document:F,intent:{type:"string"},layer:{type:"string",description:"Restrict the claim to one layer, leaving the rest free. Rooms only."}},required:["document","intent"]},run:(r,e)=>e.beginJob({document:r.document,intent:r.intent,layer:r.layer??null})},{name:"finish_job",description:"Release the document. Call this when the sprite is done, so the user knows to look. The result says what to do next for a profile picture, a banner or a card \u2014 pass that on to the user.",inputSchema:{type:"object",properties:{job:k}},run:(r,e)=>e.finishJob({job:r.job})},{name:"draw_art",description:'Returns when the room has confirmed the drawing \u2014 `seq` is the room\'s number for it and `committed` is true \u2014 or fails saying why; if it says the connection dropped, wait a moment and check get_info before drawing more. Draw a character grid. Each string in `rows` is one row of pixels and each character maps to a color via `colors`. "." and " " leave existing pixels alone; a color mapped to null erases. Every character is resolved before a pixel moves, so a typo fails rather than half-drawing. With no style reference, keep the palette small \u2014 four to eight colors, reused for shading \u2014 because that is what makes a small sprite read as pixel art rather than as a downscaled photograph. When matching an artist or a piece, use as many colors as their work does. A canvas past ~300 wide goes in bands: several calls with `y`, a few dozen rows each. A LARGE OR GENERATED GRID SHOULD NOT PASS THROUGH YOU: have the script write it to a file and pass `file` instead of `rows` \u2014 a JSON file of {rows, colors, x?, y?, \u2026}, or a PNG, which is the whole canvas or, with `rect`, one region of it. The reply says how many pixels changed and which colors joined the palette, so growth is never silent. read here on the same machine. Then the call is fifty bytes, whatever the canvas.',inputSchema:{type:"object",properties:{file:{type:"string",description:"Path to a grid on this machine, instead of rows and colors: a JSON file of draw_art arguments, or a PNG. Anything given in the call as well wins."},rows:{type:"array",items:{type:"string"}},colors:{type:"object",additionalProperties:{oneOf:[V,{type:"null"}]}},x:{type:"integer"},y:{type:"integer"},center:{type:"boolean"},clear:{type:"boolean",description:"Erase the cel first."},rect:{type:"object",description:"With `file`: replace only this region `{x, y, w, h}` of the canvas from a full-canvas file \u2014 a one-object change is not a whole-canvas redraw. With `clear`, only the region is emptied first.",properties:{x:{type:"integer"},y:{type:"integer"},w:{type:"integer"},h:{type:"integer"}}},palette:{type:"string",enum:["add","replace"],description:"What this drawing's colors do to the document palette: `add` (default) joins them; `replace` makes them the whole palette \u2014 for a generated piece redrawn several times, so the palette does not pile up."},layer:{type:"string",description:"Layer name or index. Defaults to the top one."},frame:{type:"integer",description:"Frame index. Defaults to the first."},label:{type:"string",description:"What this shows as in the undo history."},job:k}},run:async(r,e)=>{let t=r.file?{...await Mt(r.file,e),...r}:r;if(!Array.isArray(t.rows)||!t.colors)throw new Error("draw_art needs rows and colors, or a file that has them");return e.drawArt({job:t.job,rows:t.rows,colors:t.colors,x:t.x??0,y:t.y??0,center:t.center??!1,clear:t.clear??!1,layer:t.layer,frame:t.frame??0,label:t.label,palette:t.palette??"add",rect:t.rect??null})}},{name:"draw_animation",description:"Write every frame in ONE call, as a single undo step. Use copyPrevious so a frame carries the last one forward and sends only the pixels that moved \u2014 that is what takes a six-frame loop from four thousand tokens of restated grid to under a thousand. Never loop draw_art.",inputSchema:{type:"object",properties:{frames:{type:"array",minItems:1,items:{type:"object",properties:{rows:{type:"array",items:{type:"string"}},copyPrevious:{type:"boolean"},x:{type:"integer"},y:{type:"integer"},center:{type:"boolean"}},required:["rows"]}},colors:{type:"object",additionalProperties:{oneOf:[V,{type:"null"}]}},fps:{type:"number"},layer:{type:"string"},label:{type:"string"},job:k},required:["frames","colors"]},run:(r,e)=>e.drawAnimation({job:r.job,frames:r.frames,colors:r.colors,fps:r.fps,layer:r.layer,label:r.label})},{name:"read_art",description:"Read a cel back as a grid \u2014 the exact inverse of draw_art, so you can extend art a human drew rather than only replacing it. Reads never need a job and never wait.",inputSchema:{type:"object",properties:{document:F,layer:{type:"string"},frame:{type:"integer"},trim:{type:"boolean",description:"Return only the drawn area. Rooms only."},file:{type:"string",description:"Write the grid to this path on this machine as JSON \u2014 the shape draw_art takes as `file` \u2014 instead of returning it, so a script can round-trip art without it passing through you. The reply says where it went and how big it is."},png:{type:"string",description:"Write the document to this path as a PNG, one image pixel per sprite pixel \u2014 the composite, or one `layer` \u2014 so an image tool, `references/compare.py` or `references/scene.py guides` can open the room's truth directly. Rooms only."},job:k}},run:async(r,e)=>{if(r.png){if(!e.readPng)throw new Error("a PNG of the document is a room thing \u2014 in a local editor, read_art({ file }) and render it");let i=await e.readPng({document:r.document,job:r.job,layer:r.layer??null,frame:r.frame??0});return await Ve(r.png,i.png).catch(a=>{throw new Error(`could not write ${r.png}: ${a.message}`)}),{document:i.document,png:r.png,size:`${i.w}\xD7${i.h}`,layer:r.layer??"composite"}}let t=await e.readArt({document:r.document,job:r.job,layer:r.layer,frame:r.frame??0,trim:r.trim??!1});if(!r.file)return t;let{rows:n,colors:o,...s}=t;return await Ve(r.file,JSON.stringify({rows:n,colors:o},null,0)).catch(i=>{throw new Error(`could not write ${r.file}: ${i.message}`)}),{...s,file:r.file,size:`${n[0]?.length??0}\xD7${n.length}`,colors:Object.keys(o??{}).length}}},{name:"render_preview",description:"See one frame as an image, so you can check your own work rather than working blind. Rendered at a readable size by default (about a thousand pixels on the long side); `scale` is screen pixels per sprite pixel if you want a particular one. To look closely at one part of a big canvas \u2014 a cell of a sprite sheet \u2014 pass `rect` `{x, y, w, h}` in sprite pixels. The text beside the image says exactly what was rendered and at what scale. WHEN MATCHING A REFERENCE, pass `reference` (a PNG path on this machine): the reference is brought to the canvas grid and shown on the LEFT of the render, same rect, same scale \u2014 the only comparison that catches an object built nothing like the original. Judge per object from that, never from the whole image or from memory.",inputSchema:{type:"object",properties:{document:F,frame:{type:"integer"},scale:{type:"integer",description:"Screen pixels per sprite pixel. Default: whatever makes the long side about 1000px."},rect:{type:"object",description:"A window onto the canvas in sprite pixels, cropped before scaling. Omit w/h to run to the edge.",properties:{x:{type:"integer"},y:{type:"integer"},w:{type:"integer"},h:{type:"integer"}}},reference:{type:"string",description:"Path to a reference PNG on this machine. Shown beside the render at the same crop and scale, resampled to the canvas grid by width."},job:k}},async run(r,e){let t=await e.preview({document:r.document,job:r.job,frame:r.frame??0,scale:r.scale??null,rect:r.rect??null,reference:r.reference??null});return Ke(t.png,t.caption)}},{name:"render_animation",description:"Every frame as one numbered contact sheet. Stills inspected one call at a time, minutes apart, cannot tell you whether motion reads; a contact sheet can, in one call. Check any animation with this before calling it done.",inputSchema:{type:"object",properties:{document:F,columns:{type:"integer",default:4},scale:{type:"integer",default:2},job:k}},async run(r,e){let t=await e.sheet({document:r.document,job:r.job,columns:r.columns??4,scale:r.scale??2});return Ke(t.png,t.caption)}},{name:"add_layer",description:"Add a layer above the others. Anything you will revise while the rest stays \u2014 a figure over a background, a caption over a figure \u2014 goes on its own layer, so a revision is one draw_art with `clear: true` on that layer and the base is never redrawn or at risk.",inputSchema:{type:"object",properties:{name:{type:"string"},job:k}},run:(r,e)=>e.addLayer({job:r.job,name:r.name})},{name:"add_frame",description:"Add an animation frame after the last one.",inputSchema:{type:"object",properties:{copy:{type:"boolean",description:"Start from the previous frame."},job:k}},run:(r,e)=>e.addFrame({job:r.job,copy:r.copy??!1})},{name:"set_palette",description:"Replace the palette. Constraining yourself to a small fixed palette is the single biggest quality lever in pixel art \u2014 choose it before you draw, because every draw_art adds its colors. With `prune: true` and no `colors`, drop every palette entry no pixel uses instead: the way to clean up after a session that did not. Never done for you on finish_job.",inputSchema:{type:"object",properties:{colors:{type:"array",items:V,minItems:1},prune:{type:"boolean",description:"Keep only the colors some pixel uses. `colors` is ignored."},job:k}},run:(r,e)=>!r.prune&&!r.colors?.length?Lt("set_palette wants `colors`, or `prune: true`"):e.setPalette({job:r.job,colors:r.colors??[],prune:r.prune===!0})},{name:"set_fps",description:"Set playback speed for every frame. Eight to twelve suits character animation.",inputSchema:{type:"object",properties:{fps:{type:"integer",minimum:1,maximum:60},job:k},required:["fps"]},run:(r,e)=>e.setFps({job:r.job,fps:r.fps})},{name:"clear",description:"Erase a cel. Destructive, so it goes through a job like every other write.",inputSchema:{type:"object",properties:{layer:{type:"string"},frame:{type:"integer"},job:k}},run:(r,e)=>e.clearCel({job:r.job,layer:r.layer,frame:r.frame??0})},{name:"trim_canvas",description:"Crop the canvas to the drawn pixels. Call this before finish_job on every GAME ASSET \u2014 a tile, an icon, a sprite for a sheet: you cannot know its extent before you have drawn it, so overshoot at new_sprite and trim afterwards, or empty space is inherited by every export and sheet. Do NOT trim a piece made to be posted: its canvas is the card, and the space around the subject is the composition.",inputSchema:{type:"object",properties:{margin:{type:"integer",default:0},job:k}},run:(r,e)=>e.trim({job:r.job,margin:r.margin??0})},{name:"resize_canvas",description:"Change the canvas size, keeping the art \u2014 the fix for a sprite made at the wrong size, in place, on the same document. `anchor` says where the old canvas sits on the new one: `center` (default), `top-left`, `top`, `top-right`, `left`, `right`, `bottom-left`, `bottom`, `bottom-right`. Never make a second document for a size change.",inputSchema:{type:"object",properties:{w:{type:"integer",minimum:1,maximum:1024},h:{type:"integer",minimum:1,maximum:1024},anchor:{type:"string"},job:k},required:["w","h"]},run:(r,e)=>e.resizeCanvas({job:r.job,w:r.w,h:r.h,anchor:r.anchor??"center"})},{name:"get_info",description:"What is in a document \u2014 size, layers, frames, palette \u2014 and who else is looking at it; `connected` says whether the room is on the other end and `pending` how many operations it has not confirmed. Both should be true and zero before you trust a read.",inputSchema:{type:"object",properties:{document:F,job:k}},run:(r,e)=>e.info({document:r.document,job:r.job})},{name:"list_documents",description:'Every document you can reach, and every job currently open. Call this when the user names a sprite ("add a frame to the hamster") so you can pass it to begin_job. Never guess a name.',inputSchema:{type:"object",properties:{}},run:(r,e)=>e.documents()},{name:"create_folder",description:"Make a folder in the library, by path \u2014 `food/sushi` nests. Already existing is fine. Prefer passing `folder` to new_sprite when the sprites are new; use this for an empty folder or before move_documents.",inputSchema:{type:"object",properties:{name:{type:"string",description:"The folder path."}},required:["name"]},run:(r,e)=>e.createFolder({name:r.name})},{name:"move_documents",description:"File documents into a folder, or back to the top level with folder null. Takes document ids from list_documents. The folder is made if it does not exist.",inputSchema:{type:"object",properties:{documents:{type:"array",items:{type:"string"},description:"Document ids."},folder:{type:["string","null"],description:"Folder path, or null for the top level."}},required:["documents"]},run:(r,e)=>e.moveDocuments({documents:r.documents,folder:r.folder??null,make:!0})},{name:"take_requests",description:"Requests people have typed into the editor and not yet been answered. Call this when the user asks you to check, or after joining a room they are in. Each one is somebody asking for a sprite in words \u2014 treat it exactly as if they had said it to you directly. Each carries what it wants: a `room` to answer in, or `room: null` for a request typed at the library, which wants a brand new sprite rather than a change to any existing one.",inputSchema:{type:"object",properties:{}},run:(r,e)=>e.takeRequests()},{name:"open_editor",description:"Open the Pixie editor in the default browser and wait for it to connect. Run this first if nothing is connected.",inputSchema:{type:"object",properties:{}},async run(r,e){let t=await e.openEditor();return Ze(t.message)}},{name:"select_document",description:"Bring a document to the front for the USER to look at. This does not give you the right to draw in it \u2014 begin_job does that.",inputSchema:{type:"object",properties:{document:{type:["string","integer"]}},required:["document"]},run:(r,e)=>e.select({document:r.document})},{name:"close_document",description:"Close a document: in a local editor its tab, in a room the connection and any job you hold on it. The document stays in the library, so this tidies the workspace rather than deleting anything.",inputSchema:{type:"object",properties:{document:{type:["string","integer"]}}},run:(r,e)=>e.closeDocument({document:r.document??null})},{name:"set_frame",description:"Move the user to a frame, by 0-based index.",inputSchema:{type:"object",properties:{index:{type:"integer"}},required:["index"]},run:(r,e)=>e.setFrame({index:r.index})},{name:"set_layer",description:"Move the user to a layer, by 0-based index counting from the bottom.",inputSchema:{type:"object",properties:{index:{type:"integer"}},required:["index"]},run:(r,e)=>e.setLayer({index:r.index})},{name:"undo",description:"Undo the last change, so the user can always reject generated art.",inputSchema:{type:"object",properties:{}},run:(r,e)=>e.undo()},{name:"redo",description:"Redo the last undone change.",inputSchema:{type:"object",properties:{}},run:(r,e)=>e.redo()}];function Xe(r){return Bt.filter(e=>r.supports(e.name)).map(e=>({name:e.name,description:e.description,inputSchema:e.inputSchema,run:async t=>{let n=await e.run(t??{},r);return n&&typeof n=="object"&&Array.isArray(n.content)?n:Ze(n)}}))}var Ye=/^[A-Za-z0-9_-]{1,64}$/,Ht=6e3;function qt(r,e){return Math.max(1,Math.min(32,Math.round(1e3/Math.max(r,e,1))))}function Jt(r,e,t){let n=new Uint8Array(e*t*4),o=r.w/e,s=r.h/t;for(let i=0;i<t;i++){let a=Math.floor(i*s),c=Math.max(a+1,Math.floor((i+1)*s));for(let l=0;l<e;l++){let h=Math.floor(l*o),u=Math.max(h+1,Math.floor((l+1)*o)),d=0,p=0,w=0,m=0,f=0;for(let E=a;E<c&&E<r.h;E++)for(let _=h;_<u&&_<r.w;_++){let j=(E*r.w+_)*4;d+=r.pixels[j],p+=r.pixels[j+1],w+=r.pixels[j+2],m+=r.pixels[j+3],f++}let y=(i*e+l)*4;f&&n.set([Math.round(d/f),Math.round(p/f),Math.round(w/f),Math.round(m/f)],y)}}return{w:e,h:t,pixels:n}}function Qe(r,e,t){let n=(c,l)=>Number.isFinite(+c)?Math.max(0,Math.round(+c)):l,o=Math.min(n(r?.x,0),e),s=Math.min(n(r?.y,0),t),i=Math.min(n(r?.w,e-o),e-o),a=Math.min(n(r?.h,t-s),t-s);return{x:o,y:s,w:i,h:a}}var Z=class{constructor(e,t){this.core=e,this.author=t.author,this.name=t.name||"agent",this.connectTransport=t.connect,this.getToken=t.getToken??null,this.createDocument=t.createDocument??null,this.control=t.control??null,this.probe=t.probe??null,this.rooms=new Map,this.asked=[],this.onAsked=null,this.jobs=new Map,this.log=[]}async open(e,{w:t=32,h:n=32,name:o=e}={}){let s=this.rooms.get(e);if(s)return s.client;let i=this.core.newDocument({w:t,h:n,name:o,seed:tt(),now:Date.now()}),a=new q(this.core,i,{author:this.author,name:this.name,seed:tt(),agent:!0,getToken:this.getToken?()=>this.getToken(e):null});a.onLost=u=>{this.lastRefusal=u},a.onPrompt=u=>{if(u.author===this.author)return;let d=u.where==="library";this.asked.push({text:u.text,from:u.name,at:u.at,wants:d?"a new sprite":"a change to this one",room:d?null:e,askedIn:e}),this.asked.length>32&&this.asked.shift(),this.onAsked?.()};let c=this.connectTransport(e,{w:t,h:n,name:o}),l=!1;return await new Promise((u,d)=>{let p=setTimeout(()=>d(new Error(`timed out joining ${e}`)),1e4);a.onStatus=m=>{m.connected&&(l=!0,clearTimeout(p),u())},a.connect(c);let w=c.onclose;c.onclose=()=>{if(w?.(),l)return this.#n(e,{w:t,h:n,name:o});clearTimeout(p),this.#e(e).then(m=>d(new Error(m)))}}),this.rooms.set(e,{client:a,attempts:0}),a}async#e(e){return this.serverUrl?(this.probe?await this.probe(this.serverUrl).catch(()=>!1):!1)?Ye.test(e)?`the room "${e}" would not open, and the server is up: try once more \u2014 a room can be away for a moment \u2014 and if it repeats, there is no such document or it is not this account's; list_documents names what it can`:`the room server refused "${e}": that is a name, not a document id \u2014 list_documents gives the id, and passing the exact name works when it is unique`:Je(this.serverUrl):"nothing is answering at the room server"}#n(e,t){let n=this.rooms.get(e);if(!n||n.closing)return;let o=Math.min(15e3,1e3*2**Math.min(n.attempts,4));n.attempts+=1,n.retry=setTimeout(()=>{if(!this.rooms.get(e)||n.closing)return;let s=this.connectTransport(e,t),i=!1,a=s.onclose;n.client.onStatus=l=>{l.connected&&(i=!0,n.attempts=0)},n.client.connect(s);let c=s.onclose;s.onclose=()=>{c?.(),a?.(),this.#n(e,t)}},o),n.retry.unref?.()}client(e){let t=this.rooms.get(e);if(!t)throw new Error(`not in room ${e}`);return t.client}async peek(e){return this.rooms.has(e)?this.client(e):this.open(e)}async begin(e,t,{layer:n=null,open:o={}}={}){let s=await this.open(e,o),i=$(),a=Xt(s,i);s.lease("acquire",{job:i,intent:t,...n?{layers:[n]}:{}});let c=await a;if(c!=="held"){let h=s.leases.find(u=>u.job!==i);throw new Error(c==="refused"&&this.lastRefusal?.message?`could not claim "${e}": ${this.lastRefusal.message}`:h?`"${e}" is already held by another job: ${h.intent}`:c==="silent"?`could not claim "${e}": the room did not answer in time \u2014 check get_info and try again`:`could not claim "${e}"`)}let l={room:e,layer:n,intent:t,cancelled:!1,timer:setInterval(()=>{this.jobs.has(i)&&s.lease("renew",{job:i})},Ht)};return l.timer.unref&&l.timer.unref(),this.jobs.set(i,l),this.note("begin_job",e,t),i}resolve(e){if(e){let o=this.jobs.get(e);if(!o)throw new Error(`unknown job ${e} \u2014 it may have finished`);if(o.cancelled)throw new Error("the user stopped you \u2014 do not retry; say what you were about to do and ask");return{job:e,...o}}if(this.jobs.size===0)throw new Error(this.lastRoom?`no job is open \u2014 begin_job({ document: "${this.lastRoom}" }) picks ${this.lastRoom} up again, or new_sprite starts another`:"no job is open \u2014 call new_sprite or begin_job first");if(this.jobs.size>1)throw new Error(`${this.jobs.size} jobs are open \u2014 pass the job argument`);let[t,n]=[...this.jobs.entries()][0];return{job:t,...n}}finish(e){let t=this.jobs.get(e);t&&(clearInterval(t.timer),this.jobs.delete(e),this.lastRoom=t.room,this.client(t.room).lease("release",{job:e}),this.note("finish_job",t.room,t.intent))}async submit(e,t,n){let o=this.resolve(e),s=this.client(o.room);if(!n.length)return{ops:0};if(!s.connected||!s.welcomed)throw new Error(`the room connection dropped \u2014 nothing since sequence ${s.state.seq} has been saved. Reconnecting; try again in a moment, and check get_info before drawing more.`);this.lastRefusal=null;let i=s.state.seq,a=s.apply(n,{label:t,job:o.job,now:Date.now()});try{await s.acked(a,{timeoutMs:3e4})}catch(l){throw this.lastRefusal?.kind==="revoked"&&(o.cancelled=!0),this.lastRefusal=null,l}if(this.lastRefusal){let l=this.lastRefusal.message;throw this.lastRefusal.kind==="revoked"&&(o.cancelled=!0),this.lastRefusal=null,new Error(l)}this.note(t,o.room,`${n.length} op${n.length>1?"s":""}`);let c=s.state.seq;return{ops:n.length,seq:c,committed:c>i}}cancel(e){let t=this.jobs.get(e);t&&(t.cancelled=!0)}note(e,t,n){this.log.unshift({at:Date.now(),tool:e,room:t,detail:n}),this.log.length>200&&this.log.pop()}announce(){if(!this.serverUrl)return;let e=`${this.serverUrl.replace(/^ws/,"http").replace(/\/$/,"")}/agent`,t=()=>{fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:this.name})}).catch(()=>{})};t(),clearInterval(this.heartbeat),this.heartbeat=setInterval(t,15e3),this.heartbeat.unref?.()}close(){for(let e of this.rooms.values())e.closing=!0,clearTimeout(e.retry);clearInterval(this.heartbeat);for(let[e]of this.jobs)this.finish(e);for(let{client:e}of this.rooms.values())e.close();this.rooms.clear()}get kind(){return"room"}get where(){return this.serverUrl??"a room server"}supports(e){return e==="open_editor"?!!this.editorUrl:Vt.has(e)}async openEditor({document:e,job:t}={}){if(!this.editorUrl)return{ok:!1,message:"there is no editor to open from here"};let n=this.editorUrl;try{n=`${this.editorUrl}/d/${await this.#s(e,t)}`}catch{n=`${this.editorUrl}/library`}return await Zt(n),{ok:!0,message:`Opened ${n}`}}async newSprite({name:e,w:t=R.w,h:n=R.h,intent:o,palette:s,folder:i=null,challenge:a=null}){let c=await this.createDocument?.({name:e,w:t,h:n})??Be(e),l=await this.begin(c,o,{open:{w:t,h:n,name:e}});a&&(this.jobs.get(l).challenge=a),s?.length&&await this.submit(l,"set_palette",[{t:"setPalette",colors:L(s,"hue")}]),i&&await this.moveDocuments({documents:[c],folder:i,make:!0}),this.#i(c,o);let h=K(t,n)??ae(t,n);return{job:l,...this.#t(c),...h?{shape:h}:{}}}async#i(e,t){let n=process.env.SPRITE_ASKED_IN,o=process.env.SPRITE_REPLY_TO??null;if(n)try{(await this.open(n)).answer("thinking",t?String(t).slice(0,200):"making a new sprite",{reply:o,made:e})}catch{}}async beginJob({document:e,intent:t,layer:n=null}){let o=await this.#s(e,null);return{job:await this.begin(o,t,{layer:n}),...this.#t(o)}}finishJob({job:e}){let t=this.resolve(e);this.finish(t.job);let n=this.client(t.room).state,o=K(n.doc.w,n.doc.h),i=[t.challenge?`This is ${t.challenge==="weekly"?"this week's Weekly Pixels":"today's Pixel Dailies"} entry: tell the user to post it from the share button with the ${t.challenge==="weekly"?"Weekly Pixels":"Pixel Dailies"} switch on, which puts the theme and the tags on the post.`:null,o].filter(Boolean).join(" ");return{finished:t.intent,document:t.room,...i?{next:i}:{}}}async challenges(){if(!this.newsUrl)throw new Error("no server to ask: the themes come from the Pixie server, and this session has none");if(!(this.newsAt&&Date.now()-this.newsAt<300*1e3)){let n=await fetch(this.newsUrl);if(!n.ok)throw new Error(`the server said ${n.status} for the themes`);this.news=await n.json(),this.newsAt=Date.now()}let t=(n,o)=>n?{theme:n.theme,tags:n.tags,detail:n.detail??null,sizes:n.sizes??[],postedAt:new Date(n.at).toISOString(),post:n.url}:{theme:null,note:`no ${o} theme is known right now`};return{daily:t(this.news?.dailies,"daily"),weekly:t(this.news?.weekly,"weekly"),how:'new_sprite({ challenge: "daily" | "weekly", intent }) names the sprite after the theme; finish_job says how to post it as an entry.'}}async challenge(e){let t=await this.challenges(),n=e==="weekly"?t.weekly:t.daily;if(!n?.theme)throw new Error(`no ${e} theme is known right now \u2014 ask the user what to draw`);return n}async#s(e,t){if(!e)return this.resolve(t).room;let n=String(e).trim();if(this.rooms.has(n))return n;let o=await this.#l();if(o===null||o.some(i=>i.id===n))return n;let s=o.filter(i=>(i.name??"").trim().toLowerCase()===n.toLowerCase());if(s.length===1)return s[0].id;if(s.length>1)throw new Error(`${s.length} documents are called "${n}" \u2014 pass the id: ${s.map(i=>i.id).join(", ")}`);if(Ye.test(n))return n;throw new Error(`no document is called "${n}" \u2014 list_documents names every one with its id; pass the id`)}async#l(){let e=this.control?await this.control("/documents",{}).catch(()=>null):null;if(e?.documents)return e.documents.map(n=>({id:n.id,name:n.name}));if(!this.serverUrl)return null;let t=this.serverUrl.replace(/^ws/,"http").replace(/\/$/,"");return fetch(`${t}/documents`).then(n=>n.ok?n.json():null).then(n=>n?.documents?.map(o=>({id:o.id,name:o.name}))??null).catch(()=>null)}async info({document:e,job:t}={}){let n=await this.#s(e,t);return await this.peek(n),this.#t(n)}#t(e){let t=this.client(e),n=t.state;return{document:e,name:n.doc.name,w:n.doc.w,h:n.doc.h,layers:n.doc.layers.map(o=>o.name),frames:n.doc.frames.length,palette:n.doc.palette,tags:n.doc.tags.map(o=>o.name),seq:n.seq,connected:!!(t.connected&&t.welcomed),pending:n.pending,participants:t.participants.map(o=>o.name)}}#c(e){let t=this.client(e),n=t.state;return n.pending===0&&t.connected?{}:{warning:n.pending?`includes ${n.pending} operation${n.pending===1?"":"s"} the room has not confirmed${t.connected?"":" \u2014 the connection dropped, reconnecting"}`:"the room connection dropped, reconnecting"}}async readArt({document:e,job:t,layer:n,frame:o=0,trim:s=!1}){let i=await this.#s(e,t),a=await this.peek(i);return{document:i,...a.session.readArt(this.#r(i,n),o,{trim:s}),...this.#c(i)}}async readPng({document:e,job:t,layer:n=null,frame:o=0}){let s=await this.#s(e,t),i=await this.peek(s),a=i.state.doc;if(n==null)return{document:s,png:i.session.png(o,{scale:1}),w:a.w,h:a.h};let c=i.session.cel(this.#r(s,n),o);return{document:s,png:this.#a(a.w,a.h,c),w:a.w,h:a.h}}#a(e,t,n,o=1){let s=this.core.newDocument({w:e,h:t,name:"scratch",seed:1,now:Date.now()}),i=this.core.openSession(s,$(),1);try{let a=i.state();return i.apply([He(a.doc.layers[0].id,a.doc.frames[0].id,{x:0,y:0,w:e,h:t},n)],{now:Date.now()}),i.png(0,{scale:o})}finally{i.close()}}async#h(e,t,n,o,s){let i=await Gt(t).catch(f=>{throw new Error(`could not read the reference ${t}: ${f.code==="ENOENT"?"no such file":f.message}`)}),a=this.core.decodePng(i),c=e.state.doc,l=Jt(a,c.w,Math.max(1,Math.round(a.h*c.w/a.w))),h=e.session.composite(o),u=n.w,d=n.h,p=new Uint8Array((u*2+1)*d*4),w=(u*2+1)*4;for(let f=0;f<d;f++){for(let y=0;y<u;y++){let E=n.x+y,_=n.y+f,j=(_*l.w+E)*4,N=_<l.h&&E<l.w;p.set(N?l.pixels.subarray(j,j+4):[0,0,0,255],f*w+y*4);let G=(_*c.w+E)*4;p.set(h.subarray(G,G+4),f*w+(u+1+y)*4)}p.set([255,0,255,255],f*w+u*4)}let m=l.h!==c.h?` \u2014 the reference is ${a.w}\xD7${a.h}, brought to ${l.w}\xD7${l.h} by width; the canvas is ${c.w}\xD7${c.h}`:"";return{png:this.#a(u*2+1,d,p,s),note:m}}async preview({document:e,job:t,frame:n=0,scale:o=null,rect:s=null,reference:i=null}){let a=await this.#s(e,t),c=await this.peek(a),{warning:l}=this.#c(a),h=c.state.doc,u=s?Qe(s,h.w,h.h):null,d=u?u.w:h.w,p=u?u.h:h.h,w=Number.isInteger(o)&&o>0?o:qt(i?d*2:d,p),m=Math.max(1,Math.min(w,Math.floor(8192/Math.max(i?d*2:d,p,1)))),f=u?`${u.w}\xD7${u.h} at (${u.x},${u.y}) of ${h.w}\xD7${h.h}`:`${h.w}\xD7${h.h}`,y=m<w?` \u2014 asked for \xD7${w}, held to \xD7${m} so a side stays under 8192px; render a smaller rect to look closer`:"";if(i){let{png:_,note:j}=await this.#h(c,String(i),u??{x:0,y:0,w:h.w,h:h.h},n,m);return{png:_,caption:`${a}, frame ${n+1}: reference (left) beside the render (right), ${f} at \xD7${m}${j}${y}${l?` \u2014 ${l}`:""}. Name what differs, per object.`}}return{png:c.session.png(n,{scale:m,...u?{rect:u}:{}}),caption:`${a}, frame ${n+1}: ${f} at \xD7${m} (${d*m}\xD7${p*m}px)${y}${l?` \u2014 ${l}`:""}`}}async sheet({document:e,job:t,columns:n=4,scale:o=2}){let s=await this.#s(e,t),i=await this.peek(s);return{png:i.session.contactSheet({columns:n,scale:o}),caption:`${s}, ${i.state.doc.frames.length} frames, numbered left to right`}}async documents(){let e=[...this.jobs.entries()].map(([c,l])=>({job:c,document:l.room,intent:l.intent})),t=[...this.rooms.keys()],n=null,o=null,s=null,i=this.control?await this.control("/documents",{}).catch(()=>null):null;if(i)n=i.documents?.map(c=>c.id)??null,o=Array.isArray(i.folders)?i.folders.map(c=>typeof c=="string"?c:c?.path??c?.name).filter(Boolean):null,s=i.documents?Object.fromEntries(i.documents.filter(c=>c.folder).map(c=>[c.id,c.folder])):null;else if(this.serverUrl){let c=this.serverUrl.replace(/^ws/,"http").replace(/\/$/,"");n=await fetch(`${c}/documents`).then(l=>l.ok?l.json():null).then(l=>l?.documents?.map(h=>h.id)).catch(()=>null)}return{documents:n?[...new Set([...t,...n])]:t,jobs:e,open:t,...o?{folders:o}:{},...s?{filed:s}:{}}}async createFolder({name:e}){if(!this.control)throw new Error("no library here: this Sprite has no accounts, so there are no folders");let t=String(e??"").trim();if(!t)throw new Error("a folder needs a name");let n=await this.folders();if(n.includes(t))return{folder:t,created:!1,folders:n};let o=t.split("/");for(let s=1;s<=o.length;s++){let i=o.slice(0,s).join("/");n.includes(i)||(await this.control("/documents/folders",{method:"POST",body:{name:i}}),n.push(i))}return{folder:t,created:!0,folders:await this.folders()}}async moveDocuments({documents:e,folder:t=null,make:n=!1}){if(!this.control)throw new Error("no library here: this Sprite has no accounts, so there are no folders");let o=(Array.isArray(e)?e:[e]).map(String).filter(Boolean);if(!o.length)throw new Error("say which documents");let s=t===null||t===""?null:String(t).trim();s&&n&&await this.createFolder({name:s});let i=await this.control("/documents/move",{method:"POST",body:{ids:o,folder:s}});return{moved:o,folder:s,ok:i?.ok!==!1}}async folders(){let e=await this.control("/documents",{});return Array.isArray(e?.folders)?e.folders.map(t=>typeof t=="string"?t:t?.path??t?.name).filter(Boolean):[]}takeRequests(){let e=this.asked.splice(0,this.asked.length);return{requests:e,note:e.length?"Each says what it wants. Where `room` is set, answer in that document. Where it is null the request came from the library and wants a new sprite \u2014 call new_sprite and work there, and do not touch `askedIn`, which is only the room the words travelled through. The person is watching.":"Nobody has asked for anything."}}#r(e,t){let n=this.client(e).state;if(t!=null&&t!==""){let o=typeof t=="number"?n.doc.layers[t]:n.doc.layers.find(s=>s.name===t||s.id===t);if(!o)throw new Error(`no layer ${JSON.stringify(t)} in ${e}`);return o.id}return n.doc.layers[n.doc.layers.length-1].id}async drawArt({job:e,rows:t,colors:n,x:o=0,y:s=0,center:i=!1,clear:a=!1,layer:c,frame:l=0,label:h,palette:u="add",rect:d=null}){let p=this.resolve(e),w=this.client(p.room),m=t,f={x:o,y:s},y=a;if(d){let g=Qe(d,Math.max(...t.map(v=>v.length),1),t.length);if(m=t.slice(g.y,g.y+g.h).map(v=>v.slice(g.x,g.x+g.w).padEnd(g.w,".")),f={x:g.x,y:g.y},a){let v=[..."~^`|"].find(I=>!(I in n))??"\xA1";m=m.map(I=>I.replace(/[. ]/g,v)),n={...n,[v]:null},y=!1}}let E=new Set((w.state.doc.palette??[]).map(g=>String(g).toLowerCase())),_=w.session.drawArt(m,n,{layer:c!=null?this.#r(p.room,c):p.layer??this.#r(p.room,null),at:l,x:f.x,y:f.y,center:d?!1:i,clear:y}),{committed:j}=await this.submit(p.job,h||"draw_art",_),N=(this.client(p.room).state.doc.palette??[]).map(g=>String(g).toLowerCase()).filter(g=>!E.has(g)),G=m.reduce((g,v)=>g+[...v].filter(I=>I!=="."&&I!==" ").length,0);if(u==="replace"&&j){let g=[...new Set(Object.values(n).filter(v=>typeof v=="string"&&/^#[0-9a-f]{6}$/i.test(v)).map(v=>v.toLowerCase()))];g.length&&await this.submit(p.job,"set_palette",[{t:"setPalette",colors:L(g,"hue")}])}return{...this.#t(p.room),drew:d?`${m[0]?.length??0}\xD7${m.length} at (${f.x},${f.y})`:`${t[0]?.length??0}\xD7${t.length}`,changed:G,committed:j,...N.length?{added:L(N,"hue"),note:`${N.length} color${N.length===1?"":"s"} joined the palette \u2014 draw from a chosen palette, or set_palette({ prune: true }) later`}:{}}}async resizeCanvas({job:e,w:t,h:n,anchor:o="center"}){let s=this.resolve(e),i=this.client(s.room).state,a=Math.max(1,Math.min(1024,Math.round(Number(t)||i.doc.w))),c=Math.max(1,Math.min(1024,Math.round(Number(n)||i.doc.h)));if(a===i.doc.w&&c===i.doc.h)return{...this.#t(s.room),resized:"already that size"};let l=String(o??"center").toLowerCase(),h=l.includes("left")?0:l.includes("right")?2:1,u=l.includes("top")?0:l.includes("bottom")?2:1,d=Math.round((a-i.doc.w)*h/2),p=Math.round((c-i.doc.h)*u/2);await this.submit(s.job,"resize_canvas",[{t:"resize",w:a,h:c,dx:d,dy:p}]);let w=K(a,c)??ae(a,c);return{...this.#t(s.room),resized:`${i.doc.w}\xD7${i.doc.h} \u2192 ${a}\xD7${c}, the art kept at ${d},${p}`,...w?{shape:w}:{}}}async closeDocument({document:e}){let t=await this.#s(e,null),n=this.rooms.get(t);if(!n)return{closed:t,open:!1};for(let[o,s]of this.jobs)s.room===t&&this.finish(o);return n.closing=!0,clearTimeout(n.retry),n.client.close(),this.rooms.delete(t),{closed:t,open:!1}}async drawAnimation({job:e,frames:t,colors:n,fps:o,layer:s,label:i}){let a=this.resolve(e),c=this.client(a.room),l=s!=null?this.#r(a.room,s):a.layer??this.#r(a.room,null),h=c.state.doc.frames.length,u=t.length-h;if(u>0){let p=[],w=c.state.doc.frames[h-1]?.index??"V";for(let m=0;m<u;m++)w=w+"V",p.push({t:"addFrame",id:$(),index:w,durMs:ce(o)});await this.submit(a.job,"add frames",p)}if(o){let p=ce(o);await this.submit(a.job,"set fps",c.state.doc.frames.map(w=>({t:"setFrameDur",id:w.id,durMs:p})))}let d=c.session.drawAnimation(t,n,{layer:l});return await this.submit(a.job,i||"draw_animation",d),{...this.#t(a.room),wrote:`${t.length} frames`}}async addLayer({job:e,name:t}){let n=this.resolve(e),o=this.client(n.room).state,s=o.doc.layers[o.doc.layers.length-1];return await this.submit(n.job,"add_layer",[{t:"addLayer",id:$(),index:(s?.index??"V")+"V",name:t||`Layer ${o.doc.layers.length+1}`,visible:!0,locked:!1,opacity:1}]),this.#t(n.room)}async addFrame({job:e,copy:t=!1}){let n=this.resolve(e),o=this.client(n.room),s=o.state,i=s.doc.frames[s.doc.frames.length-1],a=$(),c=[{t:"addFrame",id:a,index:(i?.index??"V")+"V",durMs:i?.durMs??83}];if(t&&i)for(let l of s.doc.layers){let h=o.session.cel(l.id,i.id);h.every(u=>u===0)||c.push(et(l.id,a,s.doc,h))}return await this.submit(n.job,"add_frame",c),this.#t(n.room)}async setPalette({job:e,colors:t,prune:n=!1}){let o=this.resolve(e),s;if(n){let i=this.client(o.room),a=i.state,c=new Set;for(let h of a.doc.layers)for(let u of a.doc.frames){let d=i.session.cel(h.id,u.id);for(let p=0;p<d.length;p+=4)d[p+3]!==0&&c.add(`#${[d[p],d[p+1],d[p+2]].map(w=>w.toString(16).padStart(2,"0")).join("")}`)}let l=a.doc.palette??[];s=l.filter(h=>c.has(String(h).toLowerCase()));for(let h of c)s.some(u=>String(u).toLowerCase()===h)||s.push(h);return s=L(s,"hue"),await this.submit(o.job,"set_palette",[{t:"setPalette",colors:s}]),{...this.#t(o.room),palette:s.length,pruned:l.length-s.length}}return s=L(t,"hue"),await this.submit(o.job,"set_palette",[{t:"setPalette",colors:s}]),{...this.#t(o.room),palette:s.length}}async setFps({job:e,fps:t}){let n=this.resolve(e),o=this.client(n.room).state,s=ce(t);return await this.submit(n.job,"set_fps",o.doc.frames.map(i=>({t:"setFrameDur",id:i.id,durMs:s}))),{...this.#t(n.room),fps:t}}async clearCel({job:e,layer:t,frame:n=0}){let o=this.resolve(e),i=this.client(o.room).state,a=this.#r(o.room,t??o.layer),c=i.doc.frames[n];if(!c)throw new Error(`no frame ${n} in ${o.room}`);let l=new Uint8Array(i.doc.w*i.doc.h*4);return await this.submit(o.job,"clear",[et(a,c.id,i.doc,l)]),this.#t(o.room)}async trim({job:e,margin:t=0}){let n=this.resolve(e),o=this.client(n.room),s=o.state,i=this.#r(n.room,n.layer),a=o.session.readArt(i,0,{trim:!0}),c=a.rect.w+t*2,l=a.rect.h+t*2;return c>=s.doc.w&&l>=s.doc.h?{...this.#t(n.room),trimmed:"already tight"}:(await this.submit(n.job,"trim_canvas",[{t:"resize",w:c,h:l,dx:-a.rect.x+t,dy:-a.rect.y+t}]),{...this.#t(n.room),trimmed:`${c}\xD7${l}`})}},Vt=new Set(["new_sprite","begin_job","finish_job","draw_art","draw_animation","read_art","render_preview","render_animation","add_layer","add_frame","set_palette","set_fps","clear","trim_canvas","resize_canvas","close_document","get_info","list_documents","challenges","take_requests","create_folder","move_documents"]);function et(r,e,t,n){return{t:"paintRegion",layer:r,frame:e,rect:{x:0,y:0,w:t.w,h:t.h},rle:Kt(n)}}function Kt(r){let e=[];if(r.length){let t=[r[0],r[1],r[2],r[3]],n=0,o=()=>{let s=n;do{let i=s&127;s>>=7,e.push(s?i|128:i)}while(s);e.push(...t)};for(let s=0;s<r.length;s+=4)r[s]===t[0]&&r[s+1]===t[1]&&r[s+2]===t[2]&&r[s+3]===t[3]?n++:(o(),t=[r[s],r[s+1],r[s+2],r[s+3]],n=1);o()}return Buffer.from(Uint8Array.from(e)).toString("base64")}function Zt(r){let e=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";return new Promise(t=>Wt(e,[r],()=>t()))}function ce(r){return!r||r<=0?83:Math.max(10,Math.round(1e3/r))}function Xt(r,e,t=8e3){return new Promise(n=>{let o=r.onLeases,s=r.onLost,i=!1,a=l=>{i||(i=!0,clearTimeout(c),r.onLeases=o,r.onLost=s,n(l))};r.onLeases=l=>{o?.(l),l.some(h=>h.job===e)&&a("held")},r.onLost=l=>{s?.(l),l?.ops?.length||a("refused")};let c=setTimeout(()=>a("silent"),t);r.leases.some(l=>l.job===e)&&a("held")})}function tt(){return(Date.now()^Math.floor(Math.random()*4294967295))>>>0}import{execFile as Yt}from"node:child_process";var Qt=2e4,en=5e3,B=class r{constructor({base:e,name:t="agent"}={}){this.base=String(e).replace(/\/$/,""),this.name=t,this.log=[],this.beat=setInterval(()=>void this.announce(),en),this.beat.unref&&this.beat.unref()}get kind(){return"local"}get where(){return this.base}supports(e){return tn.has(e)}static async probe(e,{timeoutMs:t=700}={}){try{let n=await fetch(`${e.replace(/\/$/,"")}/bridge/status`,{signal:AbortSignal.timeout(t)});if(!n.ok)return null;let o=await n.json();return o?.server==="sprite"?o:null}catch{return null}}async announce(){try{await fetch(`${this.base}/bridge/agent`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:this.name,version:null}),signal:AbortSignal.timeout(2e3)})}catch{}}async call(e,t=[],n=Qt){let o;try{o=await(await fetch(`${this.base}/bridge/call`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({method:e,args:t,timeoutMs:n}),signal:AbortSignal.timeout(n+2e3)})).json()}catch(s){throw new Error(`lost contact with the Pixie editor at ${this.base}: ${s.message}`)}if(!o.ok)throw new Error(o.error);return this.note(e),o.result}note(e,t=""){this.log.unshift({at:Date.now(),tool:e,room:"local",detail:t}),this.log.length>200&&this.log.pop()}newSprite({name:e,w:t=R.w,h:n=R.h,intent:o,palette:s}){return this.call("newSprite",[{w:t,h:n,name:e,palette:s,intent:o,replace:!1}])}async challenges(){let e=await fetch(`${this.base}/news.json`).catch(()=>null);if(!e?.ok)throw new Error("this editor does not serve the themes \u2014 ask the user what to draw");let t=await e.json(),n=(o,s)=>o?{theme:o.theme,tags:o.tags,detail:o.detail??null,sizes:o.sizes??[],postedAt:new Date(o.at).toISOString(),post:o.url}:{theme:null,note:`no ${s} theme is known right now`};return{daily:n(t?.dailies,"daily"),weekly:n(t?.weekly,"weekly")}}async challenge(e){let t=await this.challenges(),n=e==="weekly"?t.weekly:t.daily;if(!n?.theme)throw new Error(`no ${e} theme is known right now \u2014 ask the user what to draw`);return n}beginJob({document:e,intent:t}){return this.call("beginJob",[{document:e,intent:t}])}finishJob({job:e}){return this.call("finishJob",[{job:e}])}async info({document:e}={}){let t=await this.call("info",[]);return{editor:this.base,...e?{asked:e}:{},...t}}readArt({document:e,layer:t,frame:n}){return this.call("readArt",[{document:e,layer:t,frame:n}])}async preview({document:e,frame:t,scale:n=null,rect:o=null,reference:s=null}){if(o)throw new Error("the local editor cannot render a region \u2014 render the whole frame");if(s)throw new Error("a reference beside the render is a room thing \u2014 in a local editor, read_art({ file }) and compare with references/compare.py");let i=await this.call("toDataURL",[t??null,Number.isInteger(n)&&n>0?n:8,e??null]);return{png:nt(i.url),caption:i.caption}}async sheet({document:e,columns:t=0,scale:n=0}){let o=await this.call("toStripDataURL",[{document:e??null,columns:t,scale:n}]);return{png:nt(o.url),caption:o.caption}}documents(){return this.call("listDocuments",[])}drawArt({job:e,rows:t,colors:n,x:o,y:s,center:i,clear:a,layer:c,frame:l,label:h,rect:u=null}){if(u){let d=Math.max(0,u.x|0),p=Math.max(0,u.y|0);t=t.slice(p,p+(u.h|0)).map(w=>w.slice(d,d+(u.w|0))),o=d,s=p,i=!1}return this.call("drawArt",[t,n,{job:e,x:o,y:s,center:i,clear:a,layer:c,frame:l,label:h}])}drawAnimation({job:e,frames:t,colors:n,fps:o,layer:s,label:i}){return this.call("drawAnimation",[{frames:t,colors:n,fps:o,job:e,layer:s,label:i}])}addLayer({name:e}){return this.call("addLayer",[e])}addFrame({copy:e=!1}){return this.call("addFrame",[{copy:!!e}])}setPalette({colors:e,prune:t=!1}){if(t)throw new Error("prune works in a room \u2014 in a local editor, pass the colors to keep");return this.call("setPalette",[e])}setFps({fps:e}){return this.call("setFps",[e])}clearCel({job:e,layer:t,frame:n}){return this.call("clear",[{job:e,layer:t,frame:n}])}trim({job:e,margin:t=0}){return this.call("trimCanvas",[{job:e,margin:t}])}setFrame({index:e}){return this.call("setFrame",[e])}setLayer({index:e}){return this.call("setLayer",[e])}select({document:e}){return this.call("selectDocument",[e])}closeDocument({document:e}){return this.call("closeDocument",[e??null])}undo(){return this.call("undo",[])}redo(){return this.call("redo",[])}async openEditor(){if((await r.probe(this.base))?.connected)return{ok:!0,message:`Editor already connected at ${this.base}`};await nn(this.base);let t=Date.now()+1e4;for(;Date.now()<t;){if((await r.probe(this.base))?.connected)return{ok:!0,message:`Editor connected at ${this.base}`};await new Promise(o=>setTimeout(o,200))}return{ok:!1,message:`Opened ${this.base} but nothing connected \u2014 check the browser window.`}}close(){clearInterval(this.beat)}},tn=new Set(["new_sprite","begin_job","finish_job","draw_art","draw_animation","read_art","render_preview","render_animation","add_layer","add_frame","set_palette","set_fps","clear","trim_canvas","get_info","list_documents","challenges","open_editor","select_document","close_document","set_frame","set_layer","undo","redo"]);function nt(r){let e=String(r??"").split(",")[1];if(!e)throw new Error("the editor returned something that was not an image");return Buffer.from(e,"base64")}function nn(r){let e=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";return new Promise(t=>Yt(e,[r],()=>t()))}import{readFileSync as rn}from"node:fs";var on="0.5.5";function sn(){let r=new URL("./version.json",import.meta.url);try{let e=JSON.parse(rn(r,"utf8"))?.version;if(typeof e=="string"&&e)return e;throw new Error("it names no version")}catch(e){throw new Error(`${r.pathname} could not be read as a version: ${e.message}`)}}var rt=on??sn();function le(r="room"){return`Pixel art editor with live collaboration. ${r==="local"?`Documents are tabs in a browser window on this machine. The user is
|
|
3
|
-
|
|
4
|
-
moment.`:`Documents are rooms; other people and other agents may be in one with
|
|
2
|
+
var O=(r,e)=>()=>(r&&(e=r(r=0)),e);function Te(r){let e="";for(let t=0;t<r.length;t+=32768)e+=String.fromCharCode.apply(null,r.subarray(t,t+32768));return btoa(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}var Pe=O(()=>{"use strict"});var I,St,Et,Nn,Oe=O(()=>{"use strict";I="[A-Za-z0-9_-]{1,64}",St="token|members|link|slow|suggest|project",Et=[{owner:"accounts",re:new RegExp("^/auth/")},{owner:"accounts",re:new RegExp("^/a/[A-Za-z0-9_-]{1,64}\\.png$")},{owner:"accounts",re:new RegExp("^/projects(/|$)")},{owner:"accounts",re:new RegExp(`^/documents/${I}/(${St})$`)},{owner:"library",re:new RegExp("^/documents$")},{owner:"library",re:new RegExp("^/documents/(new|register|move|order|folders|folders/delete|trash|trash/empty)$")},{owner:"library",re:new RegExp(`^/documents/${I}(/(thumbnail\\.png|export|duplicate|rename|restore))?$`)},{owner:"library",re:new RegExp(`^/documents/${I}/history/at/\\d+/thumbnail\\.png$`)},{owner:"library",re:new RegExp("^/wall\\.png$")},{owner:"document",re:new RegExp(`^/documents/${I}/history(/at/\\d+|/\\d+)?$`)},{owner:"document",re:new RegExp(`^/documents/${I}/comments(/[A-Za-z0-9]{1,40})?$`)},{owner:"document",re:new RegExp(`^/documents/${I}/snapshots(/enabled|/[A-Za-z0-9_-]{1,128})?$`)},{owner:"document",re:new RegExp(`^/favourites(/order|/(document|project)/${I})?$`)}],Nn=Et.map(({owner:r,re:e})=>({owner:r,source:e.source}))});var ae,ce=O(()=>{"use strict";Oe();ae=["read","draw","create","duplicate","organize"]});async function Ne(r){let e=new TextEncoder().encode(String(r));return Te(new Uint8Array(await crypto.subtle.digest("SHA-256",e)))}var Fn,Ie=O(()=>{"use strict";Pe();ce();Fn=120*1e3});function j(r=Date.now()){let e="0123456789ABCDEFGHJKMNPQRSTVWXYZ",t=(BigInt(r)&0xffffffffffffn)<<80n,n=crypto.getRandomValues(new Uint8Array(10)),o=0n;for(let i of n)o=o<<8n|BigInt(i);t|=o;let s="";for(let i=0;i<26;i++)s=e[Number(t&31n)]+s,t>>=5n;return s}function P(r){return r==null?{at:0}:typeof r=="number"?{at:r}:{frame:r}}var At,Tt,Pt,q,C,le,B=O(()=>{"use strict";At=new TextEncoder,Tt=new TextDecoder,Pt=1;q=class r{#e;#n;constructor(e){this.#e=e,this.#n=e.exports}static async load(e){let t;if(e instanceof WebAssembly.Module)t=await WebAssembly.instantiate(e,{});else if(e instanceof ArrayBuffer||ArrayBuffer.isView(e))({instance:t}=await WebAssembly.instantiate(e,{}));else{let n=e instanceof Response||typeof e?.then=="function"?e:fetch(e);({instance:t}=await WebAssembly.instantiateStreaming(n,{}))}return new r(t)}get#i(){return new Uint8Array(this.#n.memory.buffer)}call(e,t={}){let n=At.encode(JSON.stringify({m:e,a:t})),o=this.#n.sprite_alloc(n.length);if(!o)throw new Error("sprite-core: allocation failed");let s=0;try{if(this.#i.set(n,o),s=this.#n.sprite_dispatch(o,n.length),!s)throw new Error("sprite-core: dispatch returned nothing");let i=this.#i,a=i[s]|i[s+1]<<8|i[s+2]<<16|i[s+3]<<24,c=i[s+4],l=i.slice(s+5,s+5+a);if(c===Pt)return l;let h=JSON.parse(Tt.decode(l));if(h&&h.error)throw new Error(h.error);return h}finally{s?this.#n.sprite_free_result(s):this.#n.sprite_dealloc(o,n.length)}}reset(){this.#n.sprite_reset()}newDocument(e={}){return this.call("doc.new",{w:32,h:32,name:"untitled",...e})}decodePng(e){let t=this.call("image.decode",{png:[...e]}),n=new DataView(t.buffer,t.byteOffset,t.byteLength),o=n.getUint32(0,!0),s=n.getUint32(4,!0);return{w:o,h:s,pixels:t.subarray(8)}}openSession(e,t,n=1){let{h:o}=this.call("session.new",{doc:e,author:t,seed:n});return new C(this,o)}openRoom(e,{seq:t=0}={}){let{h:n}=this.call("room.new",{doc:e,...t>0?{seq:t}:{}});return new le(this,n)}},C=class{constructor(e,t){this.core=e,this.h=t}#e(e,t={}){return this.core.call(e,{h:this.h,...t})}state(){return this.#e("session.state")}apply(e,{now:t=Date.now(),label:n="Edit",job:o=null}={}){let s={now:t,label:n,bodies:Array.isArray(e)?e:[e]};return o&&(s.job=o),this.#e("session.apply",s).ops}remote(e){return this.#e("session.remote",{sop:e})}reject(e){return this.#e("session.reject",{op:e})}undo(e=Date.now()){return this.#e("session.undo",{now:e}).ops}redo(e=Date.now()){return this.#e("session.redo",{now:e}).ops}pending(){return this.#e("session.pending")}checkpoint({doc:e=!0}={}){return this.#e("session.checkpoint",{doc:e})}save(){return this.#e("session.save")}drawArt(e,t,n={}){return this.#e("session.drawArt",{rows:e,colors:t,...n}).bodies}drawAnimation(e,t,n={}){return this.#e("session.drawAnimation",{frames:e,colors:t,...n}).bodies}readArt(e,t,n={}){return this.#e("session.readArt",{layer:e,...P(t),...n})}cel(e,t){return this.#e("session.cel",{layer:e,...P(t)})}layerImage(e,t){return this.#e("session.layerImage",{layer:e,...P(t)})}tileCel(e,t){return this.#e("session.tileCel",{layer:e,...P(t)})}tile(e,t){return this.#e("session.tile",{tileset:e,index:t})}composite(e,{include:t="export"}={}){return this.#e("session.composite",{...P(e),include:t})}png(e,{scale:t=1,rect:n=null}={}){return this.#e("session.png",{...P(e),scale:Math.max(1,Math.round(t)||1),...n?{rect:n}:{}})}contactSheet({columns:e=4,scale:t=2}={}){return this.#e("session.sheet",{columns:e,scale:t})}thumbnail(e=48){return this.#e("session.thumbnail",{size:e})}card({frame:e=null,width:t=1200,height:n=630,framing:o=null,bleed:s=0,raw:i=!1}={}){return this.#e("session.card",{width:t,height:n,...o??{},...P(e),...s?{bleed:s}:{},...i?{raw:i}:{}})}cardLayout({frame:e=null,width:t=1200,height:n=630,framing:o=null}={}){return this.#e("session.cardLayout",{width:t,height:n,...o??{},...P(e)})}fit({width:e,height:t,margin:n=0,inscribed:o=!1,dx:s=0,dy:i=0,frame:a=null}){return this.#e("session.fit",{width:e,height:t,margin:n,inscribed:o,dx:s,dy:i,...P(a)})}close(){this.#e("session.free")}},le=class{constructor(e,t){this.core=e,this.h=t}#e(e,t={}){return this.core.call(e,{h:this.h,...t})}state(){return this.#e("room.state")}submit(e,t=Date.now()){return this.#e("room.submit",{op:e,now:t})}since(e){return this.#e("room.since",{seq:e})}compact(){return this.#e("room.compact")}save(){return this.#e("room.save")}lease(e,t){return this.#e("room.lease",{action:e,...t})}close(){this.#e("room.free")}}});function Be(r){return JSON.stringify(r)}function Ge(r){let e=r.rle instanceof Uint8Array?r.rle:null,t=new TextEncoder().encode(JSON.stringify({...r,rle:void 0}));if(t.length>Ot)return null;let n=new Uint8Array(3+t.length+(e?.length??0));return n[0]=We,n[1]=t.length&255,n[2]=t.length>>8&255,n.set(t,3),e&&n.set(e,3+t.length),n}function Nt(r){return r instanceof Uint8Array?r:r instanceof ArrayBuffer?new Uint8Array(r):ArrayBuffer.isView(r)?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):null}function qe(r){if(typeof r!="string"){let e=Nt(r);if(!e||e.length<3||e[0]!==We)return null;let t=e[1]|e[2]<<8;if(e.length<3+t)return null;try{let n=JSON.parse(new TextDecoder().decode(e.subarray(3,3+t)));if(!n||typeof n.t!="string")return null;let o=e.subarray(3+t);return n.rle=o.length?Uint8Array.from(o):null,n}catch{return null}}try{let e=JSON.parse(r);return e&&typeof e.t=="string"?e:null}catch{return null}}var S,ze,E,Fe,We,Ot,He=O(()=>{"use strict";S={HELLO:"hello",SUBMIT:"submit",PRESENCE:"presence",STROKE:"stroke",LEASE:"lease",REVIEW:"review",PING:"ping",PROMPT:"prompt",ANSWER:"answer",STOP:"stop"},ze={DOCUMENT:"document",LIBRARY:"library"},E={WELCOME:"welcome",REHELLO:"rehello",OPS:"ops",REJECT:"reject",PRESENCE:"presence",STROKE:"stroke",LEASE:"lease",INK:"ink",SUGGESTIONS:"suggestions",SUGGESTION:"suggestion",ERROR:"error",PONG:"pong",PROMPT:"prompt",ANSWER:"answer",STOP:"stop"},Fe={OPS:"ops",SNAPSHOT:"snapshot"};We=157,Ot=65535});function Je(r){return j().toLowerCase().slice(0,16)}var Ve=O(()=>{"use strict";B()});function Mt(r){let e=[];if(!r.length)return new Uint8Array;let t=r[0],n=r[1],o=r[2],s=r[3],i=0,a=()=>{let c=i;do{let l=c&127;c>>>=7,e.push(c?l|128:l)}while(c);e.push(t,n,o,s)};for(let c=0;c<r.length;c+=4)r[c]===t&&r[c+1]===n&&r[c+2]===o&&r[c+3]===s?i++:(a(),t=r[c],n=r[c+1],o=r[c+2],s=r[c+3],i=1);return a(),new Uint8Array(e)}function Ct(r){let e="";for(let n=0;n<r.length;n+=32768)e+=String.fromCharCode.apply(null,r.subarray(n,n+32768));return btoa(e)}function Ut(r){return Ct(Mt(r))}function Xe(r,e,t,n){return{t:"paintRegion",layer:r,frame:e,rect:{x:t.x,y:t.y,w:t.w,h:t.h},rle:Ut(n)}}var Ze=O(()=>{"use strict"});import{readFile as fn}from"node:fs/promises";import{readFile as dt,writeFile as pt,mkdir as mt,chmod as ft}from"node:fs/promises";import{readFileSync as wt}from"node:fs";import{homedir as gt,hostname as yt}from"node:os";import{join as se}from"node:path";import{execFile as bt}from"node:child_process";var b=r=>String(r??"").replace(/\/+$/,"").replace(/^ws/,"http"),we=r=>String(r??"").replace(/\/+$/,"").replace(/^http/,"ws");async function ge(r,e){if(!r?.ok||!(r.headers.get("content-type")??"").includes("json"))return null;let t=await r.json().catch(()=>null);return!t||typeof t!="object"?null:e.some(n=>n in t)?t:null}async function ye(r,{timeoutMs:e=3e3,fetchImpl:t=fetch}={}){try{let n=await t(`${b(r)}/setup`,{signal:AbortSignal.timeout(e)});return await ge(n,["accounts","expectedPort","install"])}catch{return null}}var oe=new Map;function be(r,{fetchImpl:e=fetch,timeoutMs:t=3e3}={}){let n=b(r);return oe.has(n)||oe.set(n,ut(r,{fetchImpl:e,timeoutMs:t})),oe.get(n)}async function ut(r,{fetchImpl:e,timeoutMs:t}){try{let n=await e(`${b(r)}/config.json`,{signal:AbortSignal.timeout(t)}),s=(await ge(n,["rooms","accounts"]))?.rooms;if(typeof s=="string"&&/^(wss?|https?):\/\/./.test(s))return we(s)}catch{}return we(r)}var z=process.env.SPRITE_PUBLIC_SERVER||xt()||null;function xt(){return"wss://drawpixie.com"}var xe=se(process.env.XDG_CONFIG_HOME||se(gt(),".config"),"sprite"),ie=se(xe,"agent.json"),Se=r=>String(r??"").replace(/\/+$/,"");function Ee(){return`${yt().replace(/\.local$/,"")}`.slice(0,60)||"a computer"}async function ke(){try{let r=JSON.parse(await dt(ie,"utf8"));return r&&typeof r=="object"&&r.servers?r:{servers:{}}}catch{return{servers:{}}}}async function F(r){return process.env.SPRITE_TOKEN?{token:process.env.SPRITE_TOKEN,from:"the environment"}:(await ke()).servers[Se(r)]??null}async function $e(r,e){await mt(xe,{recursive:!0,mode:448});let t=await ke();t.servers[Se(r)]={...e,at:Date.now()};let n=`${ie}.tmp`;await pt(n,JSON.stringify(t,null,2),{mode:384}),await ft(n,384);let{rename:o}=await import("node:fs/promises");await o(n,ie)}async function ve(r,{timeoutMs:e=3e3}={}){try{let t=await fetch(`${b(r)}/setup`,{signal:AbortSignal.timeout(e)});return t.ok?!!(await t.json()).accounts:!1}catch{return!1}}async function je(r,e,{session:t=null}={}){let n=t??await F(r);if(!n?.token)return;let o=await fetch(`${b(r)}/documents/${encodeURIComponent(e)}/token`,{headers:{Authorization:`Bearer ${n.token}`}});if(o.status===401)throw new Error("this machine is signed out \u2014 pair it again");if(o.status===403)throw new Error(`no access to ${e}`);if(!o.ok)throw new Error(`could not get access to ${e}`);return(await o.json()).token}async function _e(r,{name:e,w:t,h:n},{session:o=null}={}){let s=o??await F(r);if(!s?.token)return;let i=await fetch(`${b(r)}/documents/new`,{method:"POST",headers:{Authorization:`Bearer ${s.token}`,"Content-Type":"application/json"},body:JSON.stringify({name:e,w:t,h:n})});if(i.status===401)throw new Error("this machine is signed out \u2014 pair it again");if(!i.ok)throw new Error(`could not make a document: the server said ${i.status}`);let{id:a}=await i.json();if(typeof a!="string"||!a)throw new Error("could not make a document: the server named none");return a}async function Re(r,e,{method:t="GET",body:n=null,session:o=null}={}){let s=o??await F(r);if(!s?.token)return;let i=await fetch(`${b(r)}${e}`,{method:t,headers:{Authorization:`Bearer ${s.token}`,...n!==null?{"Content-Type":"application/json"}:{}},body:n!==null?JSON.stringify(n):void 0});if(i.status===401)throw new Error("this machine is signed out \u2014 pair it again");let a=await i.json().catch(()=>({}));if(!i.ok)throw new Error(a?.error?String(a.error):`the server said ${i.status}`);return a}function Ae(r){let e=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";return new Promise(t=>bt(e,[r],()=>t()))}Ie();ce();import{createServer as kt}from"node:http";import{randomBytes as Ce}from"node:crypto";var $t=300*1e3,vt=()=>Ce(32).toString("base64url"),jt=()=>Ce(16).toString("base64url");function Me(r,e){return`<!doctype html><meta charset="utf-8"><title>Pixie</title><body style="background:#101014;color:#d8d8e0;font:14px/1.6 system-ui,sans-serif;display:grid;place-items:center;height:100vh;margin:0;text-align:center"><div><p style="font-size:16px">${r}</p><p style="color:#8a8a99;font-size:12px">${e}</p></div>`}async function _t({state:r,timeoutMs:e}){let t,n,o=new Promise((c,l)=>{t=c,n=l}),s=kt((c,l)=>{let h=new URL(c.url,"http://127.0.0.1");if(h.pathname!=="/callback"){l.writeHead(404).end();return}if(h.searchParams.get("state")!==r){l.writeHead(404).end();return}let d=h.searchParams.get("error");l.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}).end(d?Me("Nothing was granted.","You can close this window."):Me("This computer can now draw in your sprites.","You can close this window and go back to what you were doing.")),t(h.searchParams)});await new Promise((c,l)=>{s.once("error",l),s.listen(0,"127.0.0.1",c)});let i=setTimeout(()=>n(new Error("nobody answered the browser in time")),e);i.unref?.();let{port:a}=s.address();return{redirect:`http://127.0.0.1:${a}/callback`,wait:o,close:()=>{clearTimeout(i),s.close(),s.closeAllConnections?.()}}}async function Ue(r,{onUrl:e=()=>{},open:t=Ae,label:n="the Pixie agent",scope:o=ae,timeoutMs:s=$t}={}){let i=vt(),a=await Ne(i),c=jt(),l=await _t({state:c,timeoutMs:s});try{let h=new URL(`${b(r)}/authorize`);h.searchParams.set("challenge",a),h.searchParams.set("state",c),h.searchParams.set("redirect",l.redirect),h.searchParams.set("scope",o.join(",")),h.searchParams.set("name",n),e(h.toString()),await t(h.toString());let d=await l.wait,u=d.get("error");if(u)throw new Error(u==="access_denied"?"you said no on that screen, so nothing was granted":`that request was refused: ${u}`);let p=d.get("code");if(!p)throw new Error("the browser came back without an authorization");let w=await fetch(`${b(r)}/auth/authorize/exchange`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({code:p,verifier:i,name:Ee()})}),m=await w.json().catch(()=>({}));if(!w.ok||!m.token)throw new Error(m.error||"that authorization could not be exchanged");return await $e(r,{token:m.token,name:m.name,email:m.email,scope:m.scope??null}),m}finally{l.close()}}import{realpathSync as Le}from"node:fs";import{fileURLToPath as Rt}from"node:url";function De(r){let e=process.argv[1];if(!e)return!1;try{return Le(Rt(r))===Le(e)}catch{return!1}}B();import{execFile as Kt}from"node:child_process";He();B();var It=5e3,H=class{#e=null;constructor(e,t,n){if(!n||!n.author)throw new Error("DocumentClient needs an author");if(this.core=e,this.author=n.author,this.name=n.name||"anonymous",this.agent=n.agent===!0,this.answers=n.answers===!0,this.seed=n.seed??1,this.epoch=0,this.resumed=null,n.resume){let{h:o,dropped:s}=e.call("session.resume",{doc:t,author:this.author,seed:this.seed,seq:n.resume.seq??0,pending:n.resume.pending??[]});this.session=new C(e,o),this.resumed={seq:n.resume.seq??0,dropped:s??[]}}else this.session=e.openSession(t,this.author,this.seed);this.getToken=n.getToken??null,this.transport=null,this.connected=!1,this.welcomed=!1,this.ready=new Promise(o=>{this.#e=o}),this.participants=[],this.leases=[],this.presence=null,this.you=null,this.onChange=()=>{},this.onPresence=()=>{},this.onStroke=()=>{},this.strokePreview=!0,this.onLeases=()=>{},this.ink=null,this.onInk=()=>{},this.proposals=new Map,this.suggesting=!1,this.onSuggestions=()=>{},this.onLost=()=>{},this.waiters=new Set,this.onPrompt=()=>{},this.onAnswer=()=>{},this.onStop=()=>{},this.onStatus=()=>{}}apply(e,t={}){let n=this.session.apply(e,t);return this.#r(n),this.onChange(),n}acked(e,{timeoutMs:t=3e4}={}){let n=new Set((Array.isArray(e)?e:[e]).map(o=>o?.opId??o).filter(Boolean));return new Promise((o,s)=>{let i={ids:n,resolve:o,reject:s,timer:null};i.timer=setTimeout(()=>{this.waiters.delete(i);let a=[...n].filter(c=>this.session.pending().some(l=>l.opId===c)).length;s(new Error(`${a} operation${a===1?"":"s"} not confirmed by the room after ${Math.round(t/1e3)}s \u2014 `+(this.connected?"it is answering slowly":`the connection dropped; nothing since sequence ${this.session.state().seq} has been saved`)))},t),this.waiters.add(i),this.#n()})}#n(){if(!this.waiters.size)return;let e=new Set(this.session.pending().map(t=>t.opId));for(let t of this.waiters)[...t.ids].some(n=>e.has(n))||(this.waiters.delete(t),clearTimeout(t.timer),t.resolve())}#i(e,t){for(let n of this.waiters)n.ids.has(e)&&(this.waiters.delete(n),clearTimeout(n.timer),n.reject(new Error(t)))}undo(e=Date.now()){let t=this.session.undo(e);return this.#r(t),this.onChange(),t}redo(e=Date.now()){let t=this.session.redo(e);return this.#r(t),this.onChange(),t}setPresence(e){this.presence=e,this.connected&&this.#o({t:S.PRESENCE,...e})}sendStroke({layer:e,frame:t,rect:n,rle:o,done:s=!1}){if(!this.connected||!this.welcomed||!this.strokePreview||this.suggesting)return;this._strokeSentAt=Date.now();let i=Ge({t:S.STROKE,layer:e,frame:t,rect:n,rle:o,done:s});i&&this.#u(i)}suggest(e){if(!e.length||!this.connected||!this.welcomed)return[];let t=this.session.state().seq,n=e.map(o=>({opId:j(),author:this.author,parentSeq:t,...o}));return this.#o({t:S.SUBMIT,ops:n}),n}review(e,t){!this.connected||!this.welcomed||this.#o({t:S.REVIEW,author:e,verdict:t})}lease(e,t={}){this.#o({t:S.LEASE,action:e,...t})}prompt(e,{where:t=ze.DOCUMENT}={}){if(!this.connected)return null;let n=String(e??"").trim().slice(0,2e3);if(!n)return null;let o=j();return this.#o({t:S.PROMPT,id:o,text:n,where:t}),o}answer(e,t="",{reply:n=null,made:o=null,model:s=null}={}){return this.connected?(this.#o({t:S.ANSWER,state:e,text:String(t??"").slice(0,2e3),reply:n,made:o,model:s}),!0):!1}stopAnswer(e){return!this.connected||!e?!1:(this.#o({t:S.STOP,id:e}),!0)}get state(){return this.session.state()}checkpoint({doc:e=!0}={}){return{epoch:this.epoch,author:this.author,...this.session.checkpoint({doc:e})}}composite(e){return this.session.composite(e)}connect(e){return this.transport=e,this.pendingToken=this.getToken?this.getToken():void 0,this.pendingToken?.catch?.(()=>{}),e.onopen=async()=>{this.connected=!0,this.welcomed=!1,clearInterval(this.heartbeat),this.heartbeat=setInterval(()=>{this.connected&&this.#o({t:S.PING})},It),await this.#s()},e.onmessage=t=>this.#l(t),e.onclose=()=>{this.connected=!1,this.welcomed=!1,clearInterval(this.heartbeat),this.onStatus({connected:!1})},e.readyState===1&&e.onopen(),this}disconnect(){if(this.transport){this.connected=!1,this.welcomed=!1,clearInterval(this.heartbeat);try{this.transport.close()}catch{}this.transport=null,this.onStatus({connected:!1})}}async#s(){let e;if(this.pendingToken!==void 0){let n=this.pendingToken;this.pendingToken=void 0,e=await n}else e=this.getToken?await this.getToken():void 0;let t=this.session.state();this.#o({t:S.HELLO,author:this.author,name:this.name,token:e,since:t.seq,...this.agent?{agent:!0}:{},...this.answers?{answers:!0}:{},...this.presence?.active===!1?{active:!1}:{},docId:t.doc.id})}#l(e){let t=qe(e);if(t)switch(t.t){case E.WELCOME:return this.#t(t);case E.REHELLO:return this.welcomed=!1,void this.#s();case E.OPS:return this.#c(t);case E.REJECT:return this.#h(t);case E.PRESENCE:return this.participants=t.participants||[],this.onPresence(this.participants);case E.STROKE:return this.onStroke(t);case E.LEASE:return this.leases=t.leases||[],this.onLeases(this.leases);case E.INK:return this.ink={left:t.left,cap:t.cap,refillMs:t.refillMs,nextInMs:t.nextInMs??0,at:Date.now()},this.onInk(this.ink);case E.SUGGESTIONS:return this.proposals=new Map((t.proposals??[]).map(n=>[n.author,{...n,ops:[...n.ops??[]]}])),this.onSuggestions(this.proposals);case E.SUGGESTION:{if(t.gone)return this.proposals.delete(t.author),this.onSuggestions(this.proposals,{author:t.author,name:t.name??null,gone:t.gone,by:t.by??null,applied:t.applied??0});let n=this.proposals.get(t.author)??{author:t.author,name:t.name??null,cc:t.cc??null,ops:[],at:t.at};if(n.name=t.name??n.name,n.at=t.at??n.at,t.replaced?.length){let o=new Set(t.replaced);n.ops=n.ops.filter(s=>!(s.rect?.w===1&&s.rect?.h===1&&o.has(`${s.rect.x},${s.rect.y}`)))}return n.ops.push(...t.ops??[]),this.proposals.set(t.author,n),this.onSuggestions(this.proposals,{author:t.author,added:t.ops?.length??0})}case E.PROMPT:return this.onPrompt(t);case E.ANSWER:return this.onAnswer(t);case E.STOP:return this.onStop(t);case E.ERROR:if(this.#d(t.message)){this.strokePreview=!1;return}return this.onLost({kind:t.fatal?"fatal":"protocol",message:t.message});default:return}}#t(e){this.you=e.you,this.suggesting=e.you?.suggest===!0,this.welcomed=!0;let t=this.session.pending();if(e.catchup.kind===Fe.SNAPSHOT){let{h:n,dropped:o}=this.core.call("session.resume",{doc:e.catchup.doc,author:this.author,seed:this.seed,seq:e.catchup.seq,pending:t});if(this.session.close(),this.session=new C(this.core,n),this.epoch++,o&&o.length){for(let s of o)this.#i(s,"could not be replayed onto the current document");this.onLost({kind:"rebased",ops:o,message:`${o.length} edit${o.length>1?"s":""} could not be replayed onto the current document`})}}else for(let n of e.catchup.ops||[])this.#a(n);if(this.#n(),this.participants=e.participants||[],this.leases=e.leases||[],t.length&&this.suggesting)for(let n of t)this.session.reject(n.opId);else t.length&&this.#o({t:S.SUBMIT,ops:t});this.presence&&this.#o({t:S.PRESENCE,...this.presence}),this.onStatus({connected:!0,resent:t.length}),this.onPresence(this.participants),this.onLeases(this.leases),this.onChange(),this.#e?.(),this.#e=null}#c(e){let t=!1;for(let n of e.ops||[])this.#a(n),t=!0;t&&(this.#n(),this.onChange())}#a(e){let t=this.session.remote(e);t.dropped&&t.dropped.length&&this.onLost({kind:"obsolete",ops:t.dropped,message:`${t.dropped.length} of your edits no longer apply and were dropped`})}#h(e){e.opId&&this.session.reject(e.opId),e.opId&&this.#i(e.opId,e.message),this.onLost({kind:e.kind,ops:e.opId?[e.opId]:[],message:e.message}),this.onChange()}#r(e){!e.length||!this.connected||!this.welcomed||this.#o({t:S.SUBMIT,ops:e})}#o(e){this.#u(Be(e))}#d(e){return typeof e!="string"?!1:e===`unknown message: ${S.STROKE}`?!0:e==="unreadable message"&&Date.now()-(this._strokeSentAt??0)<2e3}#u(e){if(this.transport)try{this.transport.send(e)}catch{this.connected=!1}}close(){this.disconnect(),this.session.close()}};B();Ve();var A=r=>{let e=String(r).replace("#","");return[parseInt(e.slice(0,2),16),parseInt(e.slice(2,4),16),parseInt(e.slice(4,6),16)]};var U=([r,e,t])=>.3*r+.59*e+.11*t;function J([r,e,t]){let n=Math.max(r,e,t),o=Math.min(r,e,t),s=n-o,i=0;return s&&(n===r?i=(e-t)/s%6:n===e?i=(t-r)/s+2:i=(r-e)/s+4,i*=60,i<0&&(i+=360)),[i,n?s/n:0,n/255]}var Ke={luminance:(r,e)=>U(A(r))-U(A(e)),hue:(r,e)=>{let[t,n]=J(A(r)),[o,s]=J(A(e));return n<.08||s<.08?n<.08&&s<.08?U(A(r))-U(A(e)):n<.08?-1:1:Math.round(t/20)!==Math.round(o/20)?t-o:U(A(r))-U(A(e))},saturation:(r,e)=>J(A(r))[1]-J(A(e))[1]};function L(r,e="hue"){let t=Ke[e]??Ke.hue;return[...r].sort(t)}Ze();import{readFile as Xt}from"node:fs/promises";function Lt(r){try{return/^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(new URL(b(r)).hostname)}catch{return!1}}function Ye(r,{publicServer:e=z}={}){let t=b(r);return e&&r===e?`nothing is answering at ${t} \u2014 this machine is offline, or the site is down`:Lt(r)?`nothing is answering at ${t} \u2014 start the editor with \`npm start\` in the Pixie repository, then try again`:`nothing is answering at ${t} \u2014 check the address and that whatever serves it is up`}import{readFile as zt,writeFile as Qe}from"node:fs/promises";function N(r,e=0){if(r==null||typeof r=="number"||typeof r=="boolean")return r;if(typeof r=="string")return r.length>120?`${r.slice(0,119)}\u2026`:r;if(Array.isArray(r)){if(r.length&&r.every(t=>typeof t=="string")){let t=new Set(r.map(n=>n.length));return`${r.length}\xD7${t.size===1?r[0].length:`${Math.min(...t)}\u2013${Math.max(...t)}`}`}return`[${r.length}]`}if(typeof r=="object"){let t=Object.keys(r),n=t.length<=8&&t.every(s=>r[s]==null||typeof r[s]!="object");if(t.length>32||e>0&&!n)return`{${t.length}}`;let o={};for(let s of t)o[s]=N(r[s],e+1);return o}return String(r)}function Dt(r){if(r&&typeof r=="object"&&Array.isArray(r.content)){let e=r.content.find(n=>n.type==="image"),t=r.content.find(n=>n.type==="text")?.text??"";if(r.isError)return{ok:!1,error:N(t)};if(e)return{ok:!0,result:`image \u2014 ${N(t)}`};try{return{ok:!0,result:N(JSON.parse(t))}}catch{return{ok:!0,result:N(t)}}}return{ok:!0,result:N(r)}}var V=class{constructor(){this.entries=[],this.next=1,this.open=new Map,this.starts=new Map}async record(e,t,n){let o=Date.now(),s={i:this.next++,t:new Date(o).toISOString(),tool:e,args:N(t??{})};try{let i=await n();return Object.assign(s,Dt(i),{ms:Date.now()-o}),this.#e(s),i}catch(i){throw Object.assign(s,{ok:!1,error:N(i?.message??String(i)),ms:Date.now()-o}),this.#e(s),i}}#e(e){this.entries.push(e),this.entries.length>4e3&&this.entries.splice(0,this.entries.length-4e3)}began(e){if(!e)return;let t=this.entries.at(-1)?.i??this.next;this.open.set(e,t),this.starts.set(t,e)}finished(e){let t=e??(this.open.size===1?[...this.open.keys()][0]:null);if(!t||!this.open.has(t))return[];let n=this.open.get(t);return this.open.delete(t),this.entries.filter(o=>o.i>=n&&(o.args?.job==null||o.args.job===t)&&(this.starts.get(o.i)??t)===t)}};var K={type:"string",description:"#rrggbb"};async function Ft(r,e){let t=await zt(r).catch(s=>{throw new Error(`could not read ${r}: ${s.code==="ENOENT"?"no such file":s.message}`)});if(t.length>8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71){if(!e.core?.decodePng)throw new Error("this server cannot decode a PNG \u2014 send rows and colors");return Bt(e.core.decodePng(t))}let o;try{o=JSON.parse(t.toString("utf8"))}catch{throw new Error(`${r} is neither a PNG nor JSON`)}if(!o||typeof o!="object"||!Array.isArray(o.rows)||typeof o.colors!="object")throw new Error(`${r} needs "rows" (an array of strings) and "colors" (a map of character to #rrggbb)`);return o}function Bt({w:r,h:e,pixels:t}){let n={},o=new Map,s=33,i=l=>{let h=o.get(l);if(h)return h;for(;;)if(s===34||s===46||s===92||s===127)s+=1;else if(s>=128&&s<161)s=161;else if(s>=55296&&s<57344)s=57344;else break;return h=String.fromCodePoint(s),s+=1,o.set(l,h),n[h]=l,h},a=l=>l.toString(16).padStart(2,"0"),c=[];for(let l=0;l<e;l++){let h="";for(let d=0;d<r;d++){let u=(l*r+d)*4,p=t[u+3];if(p===0){h+=".";continue}let w=p===255?`#${a(t[u])}${a(t[u+1])}${a(t[u+2])}`:`#${a(t[u])}${a(t[u+1])}${a(t[u+2])}${a(p)}`;h+=i(w)}c.push(h)}return{rows:c,colors:n}}var $={type:"string",description:"The job this call belongs to. May be omitted when exactly one job is open."},X={type:"string",description:"Which document. Omit to use the one your job owns. The id \u2014 the one new_sprite, get_info and list_documents hand back \u2014 or the exact name when only one document has it. In a local editor a 0-based tab index works too."},Wt=r=>({isError:!0,content:[{type:"text",text:r}]}),he=r=>({content:[{type:"text",text:typeof r=="string"?r:JSON.stringify(r)}]}),et=(r,e)=>({content:[{type:"image",data:Buffer.from(r).toString("base64"),mimeType:"image/png"},{type:"text",text:e}]}),_={w:80,h:42},Gt="40\xD721, 80\xD742, 120\xD763, 160\xD784, 240\xD7126",qt="40\xD740, 50\xD750, 80\xD780, 100\xD7100",Ht="60\xD720, 75\xD725, 150\xD750, 300\xD7100";function ue(r,e){if(!r||!e)return null;let t=r/e,n=1200/630;if(Math.abs(t-n)<.15){let o=Math.round(r/n);return`${r}\xD7${e} is ${t.toFixed(2)}:1; the link-card shape at this width is ${r}\xD7${o} \u2014 resize_canvas if it is meant to be posted.`}return Math.abs(t-1)<.1?`${r}\xD7${e} is nearly square; ${Math.max(r,e)}\xD7${Math.max(r,e)} would be a profile picture.`:Math.abs(t-3)<.25?`${r}\xD7${e} is nearly 3:1; ${r}\xD7${Math.round(r/3)} would be a profile banner.`:null}function Z(r,e){return!r||!e?null:r===e?"This is square, the shape of a profile picture: in the editor, file \u25B8 Save as profile picture (400\xD7400) fits it inside the circle it will be cropped to. Tell the user.":r===3*e?"This is 3:1, the shape of a profile banner: in the editor, file \u25B8 Save as profile banner (1500\xD7500) exports it at the size X wants. Tell the user.":Math.abs(r/e-1200/630)<.02?"This is the link-card shape: the share button posts it edge to edge.":null}function Jt(r){let e=String(r??"").trim();if(!/^[A-Za-z0-9]+([-_][A-Za-z0-9]+)+$/.test(e))return e;let t=e.split(/[-_]+/).join(" ");return t[0].toUpperCase()+t.slice(1)}var Vt=[{name:"new_sprite",description:"Create a sprite and claim it. Call this FIRST \u2014 before writing a script, computing a grid, or thinking about pixels. It costs milliseconds and puts a labeled document in front of the user immediately, so they can see what you are making and where. The tab opens in the background and never steals their focus.",inputSchema:{type:"object",properties:{name:{type:"string",description:'The title, the way a person would write it: "Blue cake", not "blue-cake". Shown on the tab and in the library; the URL is minted from it. A hyphenated lowercase name is turned into words.'},w:{type:"integer",default:_.w,description:`Width in pixels. WHAT THE USER ASKED FOR WINS: a stated size or shape is used as stated, and a named artist, piece or style is matched in scale and detail. The rest is defaults for when nothing was said: ${_.w}\xD7${_.h} \u2014 1.91:1, the ratio link cards crop to, so a piece drawn on it posts edge to edge \u2014 at any size (${Gt}, 480\xD7252, 600\xD7315: examples, not limits). A PROFILE PICTURE is square and cropped to a circle \u2014 keep the subject inside it: ${qt}. A BANNER is 3:1: ${Ht}. Use a game asset's own size (8\xD78, 16\xD716, 32\xD732) only when the user asks for a tile, icon, or sprite for a game.`},h:{type:"integer",default:_.h,description:"Height in pixels. See w."},palette:{type:"array",items:K,description:"Optional starting palette."},intent:{type:"string",description:"One line on what you are making. Shown while you work."},folder:{type:"string",description:'Optional folder to file it in, by path (`food/sushi`). Made if it does not exist. Use this when the user asks for sprites "in a folder" rather than moving them afterwards.'},challenge:{type:"string",enum:["daily","weekly"],description:'"daily" when the user asks for the sprite of the day / today\'s theme / the Pixel Dailies; "weekly" for the sprite of the week / this week\'s word / Weekly Pixels. The name comes from the theme itself (pass `name` only to override), and finish_job will say how to post it as an entry. Call `challenges` first if you want to see the theme before drawing.'}},required:["intent"]},run:async(r,e)=>{let t=r.name?Jt(r.name):null,n=r.w??null,o=r.h??null;if(r.challenge){let s=await e.challenge(r.challenge);if(t??=s.theme,n===null&&o===null&&s.sizes?.length){let i=s.sizes[Math.floor((s.sizes.length-1)/2)],[a,c]=i.split(/[x×]/).map(Number);a&&c&&([n,o]=[a,c])}}if(!t)throw new Error("new_sprite needs a name, or a challenge to take one from");return e.newSprite({name:t,w:n??_.w,h:o??_.h,intent:r.intent,palette:r.palette,folder:r.folder??null,challenge:r.challenge??null})}},{name:"challenges",description:`The sprite of the day and the sprite of the week: Pixel Dailies sets a theme every day ("Today's theme is #Gateway") and Weekly Pixels a word every week, with sizes. When the user says "the sprite of the day", "today's theme", "the daily", "the weekly", "this week's word" or names either community, this is what they mean \u2014 read it here rather than asking them. The server reads the accounts and keeps the answer; nothing is fetched from Bluesky by you.`,inputSchema:{type:"object",properties:{}},run:(r,e)=>e.challenges()},{name:"begin_job",description:"Claim an existing document for one piece of work \u2014 the other way to start, where new_sprite is the one for new art. Refused by name if another job holds it, which is what keeps parallel requests from colliding. With no `document` and no job open, it picks up the document you last finished \u2014 one more pass on the same sprite is begin_job({ intent }).",inputSchema:{type:"object",properties:{document:{type:"string",description:"Which document: the id, or the exact name when only one document has it. Omit, with no job open, for the one you last finished."},intent:{type:"string"},layer:{type:"string",description:"Restrict the claim to one layer, leaving the rest free. Rooms only."}},required:["intent"]},run:(r,e)=>e.beginJob({document:r.document,intent:r.intent,layer:r.layer??null})},{name:"finish_job",description:"Release the document. Call this when the sprite is done, so the user knows to look. The result says what to do next for a profile picture, a banner or a card \u2014 pass that on to the user \u2014 and carries `trace`: every call of this job, as the server saw it. When the task asked for a report back, save it as trace.jsonl (one entry per line) beside report.md.",inputSchema:{type:"object",properties:{job:$}},run:(r,e)=>e.finishJob({job:r.job})},{name:"draw_art",description:'Returns when the room has confirmed the drawing \u2014 `seq` is the room\'s number for it and `committed` is true \u2014 or fails saying why; if it says the connection dropped, wait a moment and check get_info before drawing more. Draw a character grid. Each string in `rows` is one row of pixels and each character maps to a color via `colors`. "." and " " leave existing pixels alone; a color mapped to null erases. Every character is resolved before a pixel moves, so a typo fails rather than half-drawing. With no style reference, keep the palette small \u2014 four to eight colors, reused for shading \u2014 because that is what makes a small sprite read as pixel art rather than as a downscaled photograph. When matching an artist or a piece, use as many colors as their work does. A canvas past ~300 wide goes in bands: several calls with `y`, a few dozen rows each. A LARGE OR GENERATED GRID SHOULD NOT PASS THROUGH YOU: have the script write it to a file and pass `file` instead of `rows` \u2014 a JSON file of {rows, colors, x?, y?, \u2026}, or a PNG, which is the whole canvas or, with `rect`, one region of it. The reply says how many pixels changed and which colors joined the palette, so growth is never silent (with `palette: "replace"` nothing joins: the palette is this drawing\'s colors, and the reply says so). read here on the same machine. Then the call is fifty bytes, whatever the canvas.',inputSchema:{type:"object",properties:{file:{type:"string",description:"Path to a grid on this machine, instead of rows and colors: a JSON file of draw_art arguments, or a PNG. Anything given in the call as well wins."},rows:{type:"array",items:{type:"string"}},colors:{type:"object",additionalProperties:{oneOf:[K,{type:"null"}]}},x:{type:"integer"},y:{type:"integer"},center:{type:"boolean"},clear:{type:"boolean",description:"Erase the cel first."},rect:{type:"object",description:"With `file`: replace only this region `{x, y, w, h}` of the canvas from a full-canvas file \u2014 a one-object change is not a whole-canvas redraw. With `clear`, only the region is emptied first.",properties:{x:{type:"integer"},y:{type:"integer"},w:{type:"integer"},h:{type:"integer"}}},palette:{type:"string",enum:["add","replace"],description:"What this drawing's colors do to the document palette: `add` (default) joins them; `replace` makes them the whole palette \u2014 for a generated piece redrawn several times, so the palette does not pile up."},layer:{type:"string",description:"Layer name or index. Defaults to the top one."},frame:{type:"integer",description:"Frame index. Defaults to the first."},label:{type:"string",description:"What this shows as in the undo history."},job:$}},run:async(r,e)=>{let t=r.file?{...await Ft(r.file,e),...r}:r;if(!Array.isArray(t.rows)||!t.colors)throw new Error("draw_art needs rows and colors, or a file that has them");return e.drawArt({job:t.job,rows:t.rows,colors:t.colors,x:t.x??0,y:t.y??0,center:t.center??!1,clear:t.clear??!1,layer:t.layer,frame:t.frame??0,label:t.label,palette:t.palette??"add",rect:t.rect??null})}},{name:"draw_animation",description:"Write every frame in ONE call, as a single undo step. Use copyPrevious so a frame carries the last one forward and sends only the pixels that moved \u2014 that is what takes a six-frame loop from four thousand tokens of restated grid to under a thousand. Never loop draw_art. SILHOUETTE FIRST: at 32px a front-on animal is any animal; when the subject could go several ways, draw 2-3 rough silhouettes as frames, look with render_animation, keep the one that reads, then add detail \u2014 three candidates cost barely more than one.",inputSchema:{type:"object",properties:{frames:{type:"array",minItems:1,items:{type:"object",properties:{rows:{type:"array",items:{type:"string"}},copyPrevious:{type:"boolean"},x:{type:"integer"},y:{type:"integer"},center:{type:"boolean"}},required:["rows"]}},colors:{type:"object",additionalProperties:{oneOf:[K,{type:"null"}]}},fps:{type:"number"},layer:{type:"string"},label:{type:"string"},job:$},required:["frames","colors"]},run:(r,e)=>e.drawAnimation({job:r.job,frames:r.frames,colors:r.colors,fps:r.fps,layer:r.layer,label:r.label})},{name:"read_art",description:"Read a cel back as a grid \u2014 the exact inverse of draw_art, so you can extend art a human drew rather than only replacing it. Reads never need a job and never wait.",inputSchema:{type:"object",properties:{document:X,layer:{type:"string"},frame:{type:"integer"},trim:{type:"boolean",description:"Return only the drawn area. Rooms only."},file:{type:"string",description:"Write the grid to this path on this machine as JSON \u2014 the shape draw_art takes as `file` \u2014 instead of returning it, so a script can round-trip art without it passing through you. The reply says where it went and how big it is."},png:{type:"string",description:"Write the document to this path as a PNG, one image pixel per sprite pixel \u2014 the composite, or one `layer` \u2014 so an image tool, `references/compare.py` or `references/scene.py guides` can open the room's truth directly. Rooms only."},job:$}},run:async(r,e)=>{if(r.png){if(!e.readPng)throw new Error("a PNG of the document is a room thing \u2014 in a local editor, read_art({ file }) and render it");let i=await e.readPng({document:r.document,job:r.job,layer:r.layer??null,frame:r.frame??0});return await Qe(r.png,i.png).catch(a=>{throw new Error(`could not write ${r.png}: ${a.message}`)}),{document:i.document,png:r.png,size:`${i.w}\xD7${i.h}`,layer:r.layer??"composite"}}let t=await e.readArt({document:r.document,job:r.job,layer:r.layer,frame:r.frame??0,trim:r.trim??!1});if(!r.file)return t;let{rows:n,colors:o,...s}=t;return await Qe(r.file,JSON.stringify({rows:n,colors:o},null,0)).catch(i=>{throw new Error(`could not write ${r.file}: ${i.message}`)}),{...s,file:r.file,size:`${n[0]?.length??0}\xD7${n.length}`,colors:Object.keys(o??{}).length}}},{name:"render_preview",description:"See one frame as an image, so you can check your own work rather than working blind. Rendered at a readable size by default (about a thousand pixels on the long side); `scale` is screen pixels per sprite pixel if you want a particular one. To look closely at one part of a big canvas \u2014 a cell of a sprite sheet \u2014 pass `rect` `{x, y, w, h}` in sprite pixels. The text beside the image says exactly what was rendered and at what scale. WHEN MATCHING A REFERENCE, pass `reference` (a PNG path on this machine): the reference is brought to the canvas grid and shown on the LEFT of the render, same rect, same scale \u2014 the only comparison that catches an object built nothing like the original. Judge per object from that, never from the whole image or from memory \u2014 and judge construction, not resemblance: count the faces, name the light, say where the shadow falls, on theirs and on yours. `values` renders both as grays \u2014 the squint test for a value plan and a focal point.",inputSchema:{type:"object",properties:{document:X,frame:{type:"integer"},scale:{type:"integer",description:"Screen pixels per sprite pixel. Default: whatever makes the long side about 1000px."},rect:{type:"object",description:"A window onto the canvas in sprite pixels, cropped before scaling. Omit w/h to run to the edge.",properties:{x:{type:"integer"},y:{type:"integer"},w:{type:"integer"},h:{type:"integer"}}},reference:{type:"string",description:"Path to a reference PNG on this machine. Shown beside the render at the same crop and scale, resampled to the canvas grid by width."},values:{type:"boolean",description:"Render as values only (grayscale) \u2014 the render, and the reference when one is given. A picture that is mid-tone everywhere shows it here and nowhere else."},job:$}},async run(r,e){let t=await e.preview({document:r.document,job:r.job,frame:r.frame??0,scale:r.scale??null,rect:r.rect??null,reference:r.reference??null,values:r.values===!0});return et(t.png,t.caption)}},{name:"render_animation",description:"Every frame as one numbered contact sheet. Stills inspected one call at a time, minutes apart, cannot tell you whether motion reads; a contact sheet can, in one call. Check any animation with this before calling it done.",inputSchema:{type:"object",properties:{document:X,columns:{type:"integer",default:4},scale:{type:"integer",default:2},job:$}},async run(r,e){let t=await e.sheet({document:r.document,job:r.job,columns:r.columns??4,scale:r.scale??2});return et(t.png,t.caption)}},{name:"add_layer",description:"Add a layer above the others. Anything you will revise while the rest stays \u2014 a figure over a background, a caption over a figure \u2014 goes on its own layer, so a revision is one draw_art with `clear: true` on that layer and the base is never redrawn or at risk.",inputSchema:{type:"object",properties:{name:{type:"string"},job:$}},run:(r,e)=>e.addLayer({job:r.job,name:r.name})},{name:"add_frame",description:"Add an animation frame after the last one.",inputSchema:{type:"object",properties:{copy:{type:"boolean",description:"Start from the previous frame."},job:$}},run:(r,e)=>e.addFrame({job:r.job,copy:r.copy??!1})},{name:"set_palette",description:"Replace the palette. Constraining yourself to a small fixed palette is the single biggest quality lever in pixel art \u2014 choose it before you draw, because every draw_art adds its colors. With `prune: true` and no `colors`, drop every palette entry no pixel uses instead: the way to clean up after a session that did not. Never done for you on finish_job.",inputSchema:{type:"object",properties:{colors:{type:"array",items:K,minItems:1},prune:{type:"boolean",description:"Keep only the colors some pixel uses. `colors` is ignored."},job:$}},run:(r,e)=>!r.prune&&!r.colors?.length?Wt("set_palette wants `colors`, or `prune: true`"):e.setPalette({job:r.job,colors:r.colors??[],prune:r.prune===!0})},{name:"set_fps",description:"Set playback speed for every frame. Eight to twelve suits character animation.",inputSchema:{type:"object",properties:{fps:{type:"integer",minimum:1,maximum:60},job:$},required:["fps"]},run:(r,e)=>e.setFps({job:r.job,fps:r.fps})},{name:"clear",description:"Erase a cel. Destructive, so it goes through a job like every other write.",inputSchema:{type:"object",properties:{layer:{type:"string"},frame:{type:"integer"},job:$}},run:(r,e)=>e.clearCel({job:r.job,layer:r.layer,frame:r.frame??0})},{name:"trim_canvas",description:"Crop the canvas to the drawn pixels. Call this before finish_job on every GAME ASSET \u2014 a tile, an icon, a sprite for a sheet: you cannot know its extent before you have drawn it, so overshoot at new_sprite and trim afterwards, or empty space is inherited by every export and sheet. Do NOT trim a piece made to be posted: its canvas is the card, and the space around the subject is the composition.",inputSchema:{type:"object",properties:{margin:{type:"integer",default:0},job:$}},run:(r,e)=>e.trim({job:r.job,margin:r.margin??0})},{name:"resize_canvas",description:"Change the canvas size, keeping the art \u2014 the fix for a sprite made at the wrong size, in place, on the same document. `anchor` says where the old canvas sits on the new one: `center` (default), `top-left`, `top`, `top-right`, `left`, `right`, `bottom-left`, `bottom`, `bottom-right`. Never make a second document for a size change.",inputSchema:{type:"object",properties:{w:{type:"integer",minimum:1,maximum:1024},h:{type:"integer",minimum:1,maximum:1024},anchor:{type:"string"},job:$},required:["w","h"]},run:(r,e)=>e.resizeCanvas({job:r.job,w:r.w,h:r.h,anchor:r.anchor??"center"})},{name:"get_info",description:"What is in a document \u2014 size, layers, frames, palette \u2014 and who else is looking at it; `connected` says whether the room is on the other end and `pending` how many operations it has not confirmed. Both should be true and zero before you trust a read.",inputSchema:{type:"object",properties:{document:X,job:$}},run:(r,e)=>e.info({document:r.document,job:r.job})},{name:"list_documents",description:'Every document you can reach, and every job currently open. Call this when the user names a sprite ("add a frame to the hamster") so you can pass it to begin_job. Never guess a name.',inputSchema:{type:"object",properties:{}},run:(r,e)=>e.documents()},{name:"create_folder",description:"Make a folder in the library, by path \u2014 `food/sushi` nests. Already existing is fine. Prefer passing `folder` to new_sprite when the sprites are new; use this for an empty folder or before move_documents.",inputSchema:{type:"object",properties:{name:{type:"string",description:"The folder path."}},required:["name"]},run:(r,e)=>e.createFolder({name:r.name})},{name:"move_documents",description:"File documents into a folder, or back to the top level with folder null. Takes document ids from list_documents. The folder is made if it does not exist.",inputSchema:{type:"object",properties:{documents:{type:"array",items:{type:"string"},description:"Document ids."},folder:{type:["string","null"],description:"Folder path, or null for the top level."}},required:["documents"]},run:(r,e)=>e.moveDocuments({documents:r.documents,folder:r.folder??null,make:!0})},{name:"take_requests",description:"Requests people have typed into the editor and not yet been answered. Call this when the user asks you to check, or after joining a room they are in. Each one is somebody asking for a sprite in words \u2014 treat it exactly as if they had said it to you directly. Each carries what it wants: a `room` to answer in, or `room: null` for a request typed at the library, which wants a brand new sprite rather than a change to any existing one.",inputSchema:{type:"object",properties:{}},run:(r,e)=>e.takeRequests()},{name:"open_editor",description:"Open the Pixie editor in the default browser and wait for it to connect. Run this first if nothing is connected.",inputSchema:{type:"object",properties:{}},async run(r,e){let t=await e.openEditor();return he(t.message)}},{name:"select_document",description:"Bring a document to the front for the USER to look at. This does not give you the right to draw in it \u2014 begin_job does that.",inputSchema:{type:"object",properties:{document:{type:["string","integer"]}},required:["document"]},run:(r,e)=>e.select({document:r.document})},{name:"close_document",description:"Close a document: in a local editor its tab, in a room the connection and any job you hold on it. The document stays in the library, so this tidies the workspace rather than deleting anything.",inputSchema:{type:"object",properties:{document:{type:["string","integer"]}}},run:(r,e)=>e.closeDocument({document:r.document??null})},{name:"set_frame",description:"Move the user to a frame, by 0-based index.",inputSchema:{type:"object",properties:{index:{type:"integer"}},required:["index"]},run:(r,e)=>e.setFrame({index:r.index})},{name:"set_layer",description:"Move the user to a layer, by 0-based index counting from the bottom.",inputSchema:{type:"object",properties:{index:{type:"integer"}},required:["index"]},run:(r,e)=>e.setLayer({index:r.index})},{name:"undo",description:"Undo the last change, so the user can always reject generated art.",inputSchema:{type:"object",properties:{}},run:(r,e)=>e.undo()},{name:"redo",description:"Redo the last undone change.",inputSchema:{type:"object",properties:{}},run:(r,e)=>e.redo()}];function tt(r){let e=r.trace??=new V,t=n=>n&&typeof n=="object"&&Array.isArray(n.content);return Vt.filter(n=>r.supports(n.name)).map(n=>({name:n.name,description:n.description,inputSchema:n.inputSchema,run:async o=>{let s=await e.record(n.name,o,()=>n.run(o??{},r));return(n.name==="new_sprite"||n.name==="begin_job")&&s?.job&&e.began(s.job),n.name==="finish_job"&&s&&typeof s=="object"&&!t(s)?he({...s,trace:e.finished(s.job??o?.job)}):t(s)?s:he(s)}}))}var nt=/^[A-Za-z0-9_-]{1,64}$/,Zt=6e3;function Yt(r,e){return Math.max(1,Math.min(32,Math.round(1e3/Math.max(r,e,1))))}function Qt(r,e,t){let n=new Uint8Array(e*t*4),o=r.w/e,s=r.h/t;for(let i=0;i<t;i++){let a=Math.floor(i*s),c=Math.max(a+1,Math.floor((i+1)*s));for(let l=0;l<e;l++){let h=Math.floor(l*o),d=Math.max(h+1,Math.floor((l+1)*o)),u=0,p=0,w=0,m=0,f=0;for(let x=a;x<c&&x<r.h;x++)for(let R=h;R<d&&R<r.w;R++){let k=(x*r.w+R)*4;u+=r.pixels[k],p+=r.pixels[k+1],w+=r.pixels[k+2],m+=r.pixels[k+3],f++}let g=(i*e+l)*4;f&&n.set([Math.round(u/f),Math.round(p/f),Math.round(w/f),Math.round(m/f)],g)}}return{w:e,h:t,pixels:n}}function de(r,e=new Uint8Array(r.length)){for(let t=0;t<r.length;t+=4){let n=Math.round(.299*r[t]+.587*r[t+1]+.114*r[t+2]);e[t]=e[t+1]=e[t+2]=n,e[t+3]=r[t+3]}return e}function en(r,e,t){let n=new Uint8Array(t.w*t.h*4);for(let o=0;o<t.h;o++){let s=((t.y+o)*e+t.x)*4;n.set(r.subarray(s,s+t.w*4),o*t.w*4)}return n}function rt(r,e,t){let n=(c,l)=>Number.isFinite(+c)?Math.max(0,Math.round(+c)):l,o=Math.min(n(r?.x,0),e),s=Math.min(n(r?.y,0),t),i=Math.min(n(r?.w,e-o),e-o),a=Math.min(n(r?.h,t-s),t-s);return{x:o,y:s,w:i,h:a}}var Y=class{constructor(e,t){this.core=e,this.author=t.author,this.name=t.name||"agent",this.connectTransport=t.connect,this.getToken=t.getToken??null,this.createDocument=t.createDocument??null,this.control=t.control??null,this.probe=t.probe??null,this.rooms=new Map,this.asked=[],this.onAsked=null,this.jobs=new Map,this.log=[]}async open(e,{w:t=32,h:n=32,name:o=e}={}){let s=this.rooms.get(e);if(s)return s.client;let i=this.core.newDocument({w:t,h:n,name:o,seed:st(),now:Date.now()}),a=new H(this.core,i,{author:this.author,name:this.name,seed:st(),agent:!0,getToken:this.getToken?()=>this.getToken(e):null});a.onLost=d=>{this.lastRefusal=d},a.onPrompt=d=>{if(d.author===this.author)return;let u=d.where==="library";this.asked.push({text:d.text,from:d.name,at:d.at,wants:u?"a new sprite":"a change to this one",room:u?null:e,askedIn:e}),this.asked.length>32&&this.asked.shift(),this.onAsked?.()};let c=this.connectTransport(e,{w:t,h:n,name:o}),l=!1;return await new Promise((d,u)=>{let p=setTimeout(()=>u(new Error(`timed out joining ${e}`)),1e4);a.onStatus=m=>{m.connected&&(l=!0,clearTimeout(p),d())},a.connect(c);let w=c.onclose;c.onclose=()=>{if(w?.(),l)return this.#n(e,{w:t,h:n,name:o});clearTimeout(p),this.#e(e).then(m=>u(new Error(m)))}}),this.rooms.set(e,{client:a,attempts:0}),a}async#e(e){return this.serverUrl?(this.probe?await this.probe(this.serverUrl).catch(()=>!1):!1)?nt.test(e)?`the room "${e}" would not open, and the server is up: try once more \u2014 a room can be away for a moment \u2014 and if it repeats, there is no such document or it is not this account's; list_documents names what it can`:`the room server refused "${e}": that is a name, not a document id \u2014 list_documents gives the id, and passing the exact name works when it is unique`:Ye(this.serverUrl):"nothing is answering at the room server"}#n(e,t){let n=this.rooms.get(e);if(!n||n.closing)return;let o=Math.min(15e3,1e3*2**Math.min(n.attempts,4));n.attempts+=1,n.retry=setTimeout(()=>{if(!this.rooms.get(e)||n.closing)return;let s=this.connectTransport(e,t),i=!1,a=s.onclose;n.client.onStatus=l=>{l.connected&&(i=!0,n.attempts=0)},n.client.connect(s);let c=s.onclose;s.onclose=()=>{c?.(),a?.(),this.#n(e,t)}},o),n.retry.unref?.()}client(e){let t=this.rooms.get(e);if(!t)throw new Error(`not in room ${e}`);return t.client}async peek(e){return this.rooms.has(e)?this.client(e):this.open(e)}async begin(e,t,{layer:n=null,open:o={}}={}){let s=await this.open(e,o),i=j(),a=on(s,i);s.lease("acquire",{job:i,intent:t,...n?{layers:[n]}:{}});let c=await a;if(c!=="held"){let h=s.leases.find(d=>d.job!==i);throw new Error(c==="refused"&&this.lastRefusal?.message?`could not claim "${e}": ${this.lastRefusal.message}`:h?`"${e}" is already held by another job: ${h.intent}`:c==="silent"?`could not claim "${e}": the room did not answer in time \u2014 check get_info and try again`:`could not claim "${e}"`)}let l={room:e,layer:n,intent:t,cancelled:!1,timer:setInterval(()=>{this.jobs.has(i)&&s.lease("renew",{job:i})},Zt)};return l.timer.unref&&l.timer.unref(),this.jobs.set(i,l),this.note("begin_job",e,t),i}resolve(e){if(e){let o=this.jobs.get(e);if(!o)throw new Error(`unknown job ${e} \u2014 it may have finished`);if(o.cancelled)throw new Error("the user stopped you \u2014 do not retry; say what you were about to do and ask");return{job:e,...o}}if(this.jobs.size===0)throw new Error(this.lastRoom?`no job is open \u2014 begin_job({ document: "${this.lastRoom}" }) picks ${this.lastRoom} up again, or new_sprite starts another`:"no job is open \u2014 call new_sprite or begin_job first");if(this.jobs.size>1)throw new Error(`${this.jobs.size} jobs are open \u2014 pass the job argument`);let[t,n]=[...this.jobs.entries()][0];return{job:t,...n}}finish(e){let t=this.jobs.get(e);t&&(clearInterval(t.timer),this.jobs.delete(e),this.lastRoom=t.room,this.client(t.room).lease("release",{job:e}),this.note("finish_job",t.room,t.intent))}async submit(e,t,n){let o=this.resolve(e),s=this.client(o.room);if(!n.length)return{ops:0};if(!s.connected||!s.welcomed)throw new Error(`the room connection dropped \u2014 nothing since sequence ${s.state.seq} has been saved. Reconnecting; try again in a moment, and check get_info before drawing more.`);this.lastRefusal=null;let i=s.state.seq,a=s.apply(n,{label:t,job:o.job,now:Date.now()});try{await s.acked(a,{timeoutMs:3e4})}catch(l){throw this.lastRefusal?.kind==="revoked"&&(o.cancelled=!0),this.lastRefusal=null,l}if(this.lastRefusal){let l=this.lastRefusal.message;throw this.lastRefusal.kind==="revoked"&&(o.cancelled=!0),this.lastRefusal=null,new Error(l)}this.note(t,o.room,`${n.length} op${n.length>1?"s":""}`);let c=s.state.seq;return{ops:n.length,seq:c,committed:c>i}}cancel(e){let t=this.jobs.get(e);t&&(t.cancelled=!0)}note(e,t,n){this.log.unshift({at:Date.now(),tool:e,room:t,detail:n}),this.log.length>200&&this.log.pop()}announce(){if(!this.serverUrl)return;let e=`${this.serverUrl.replace(/^ws/,"http").replace(/\/$/,"")}/agent`,t=()=>{fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:this.name})}).catch(()=>{})};t(),clearInterval(this.heartbeat),this.heartbeat=setInterval(t,15e3),this.heartbeat.unref?.()}close(){for(let e of this.rooms.values())e.closing=!0,clearTimeout(e.retry);clearInterval(this.heartbeat);for(let[e]of this.jobs)this.finish(e);for(let{client:e}of this.rooms.values())e.close();this.rooms.clear()}get kind(){return"room"}get where(){return this.serverUrl??"a room server"}supports(e){return e==="open_editor"?!!this.editorUrl:tn.has(e)}async openEditor({document:e,job:t}={}){if(!this.editorUrl)return{ok:!1,message:"there is no editor to open from here"};let n=this.editorUrl;try{n=`${this.editorUrl}/d/${await this.#s(e,t)}`}catch{n=`${this.editorUrl}/library`}return await rn(n),{ok:!0,message:`Opened ${n}`}}async newSprite({name:e,w:t=_.w,h:n=_.h,intent:o,palette:s,folder:i=null,challenge:a=null}){let c=await this.createDocument?.({name:e,w:t,h:n})??Je(e),l=await this.begin(c,o,{open:{w:t,h:n,name:e}});a&&(this.jobs.get(l).challenge=a),s?.length&&await this.submit(l,"set_palette",[{t:"setPalette",colors:L(s,"hue")}]),i&&await this.moveDocuments({documents:[c],folder:i,make:!0}),this.#i(c,o);let h=Z(t,n)??ue(t,n);return{job:l,...this.#t(c),...h?{shape:h}:{}}}async#i(e,t){let n=process.env.SPRITE_ASKED_IN,o=process.env.SPRITE_REPLY_TO??null;if(n)try{(await this.open(n)).answer("thinking",t?String(t).slice(0,200):"making a new sprite",{reply:o,made:e})}catch{}}async beginJob({document:e,intent:t,layer:n=null}){let o=!e&&this.jobs.size===0&&this.lastRoom?this.lastRoom:await this.#s(e,null);return{job:await this.begin(o,t,{layer:n}),...this.#t(o)}}finishJob({job:e}){let t=this.resolve(e);this.finish(t.job);let n=this.client(t.room).state,o=Z(n.doc.w,n.doc.h),i=[t.challenge?`This is ${t.challenge==="weekly"?"this week's Weekly Pixels":"today's Pixel Dailies"} entry: tell the user to post it from the share button with the ${t.challenge==="weekly"?"Weekly Pixels":"Pixel Dailies"} switch on, which puts the theme and the tags on the post.`:null,o].filter(Boolean).join(" ");return{finished:t.intent,job:t.job,document:t.room,...i?{next:i}:{}}}async challenges(){if(!this.newsUrl)throw new Error("no server to ask: the themes come from the Pixie server, and this session has none");if(!(this.newsAt&&Date.now()-this.newsAt<300*1e3)){let n=await fetch(this.newsUrl);if(!n.ok)throw new Error(`the server said ${n.status} for the themes`);this.news=await n.json(),this.newsAt=Date.now()}let t=(n,o)=>n?{theme:n.theme,tags:n.tags,detail:n.detail??null,sizes:n.sizes??[],postedAt:new Date(n.at).toISOString(),post:n.url}:{theme:null,note:`no ${o} theme is known right now`};return{daily:t(this.news?.dailies,"daily"),weekly:t(this.news?.weekly,"weekly"),how:'new_sprite({ challenge: "daily" | "weekly", intent }) names the sprite after the theme; finish_job says how to post it as an entry.'}}async challenge(e){let t=await this.challenges(),n=e==="weekly"?t.weekly:t.daily;if(!n?.theme)throw new Error(`no ${e} theme is known right now \u2014 ask the user what to draw`);return n}async#s(e,t){if(!e)return this.resolve(t).room;let n=String(e).trim();if(this.rooms.has(n))return n;let o=await this.#l();if(o===null||o.some(i=>i.id===n))return n;let s=o.filter(i=>(i.name??"").trim().toLowerCase()===n.toLowerCase());if(s.length===1)return s[0].id;if(s.length>1)throw new Error(`${s.length} documents are called "${n}" \u2014 pass the id: ${s.map(i=>i.id).join(", ")}`);if(nt.test(n))return n;throw new Error(`no document is called "${n}" \u2014 list_documents names every one with its id; pass the id`)}async#l(){let e=this.control?await this.control("/documents",{}).catch(()=>null):null;if(e?.documents)return e.documents.map(n=>({id:n.id,name:n.name}));if(!this.serverUrl)return null;let t=this.serverUrl.replace(/^ws/,"http").replace(/\/$/,"");return fetch(`${t}/documents`).then(n=>n.ok?n.json():null).then(n=>n?.documents?.map(o=>({id:o.id,name:o.name}))??null).catch(()=>null)}async info({document:e,job:t}={}){let n=await this.#s(e,t);return await this.peek(n),this.#t(n)}#t(e){let t=this.client(e),n=t.state;return{document:e,name:n.doc.name,w:n.doc.w,h:n.doc.h,layers:n.doc.layers.map(o=>o.name),frames:n.doc.frames.length,palette:n.doc.palette,tags:n.doc.tags.map(o=>o.name),seq:n.seq,connected:!!(t.connected&&t.welcomed),pending:n.pending,participants:t.participants.map(o=>o.name)}}#c(e){let t=this.client(e),n=t.state;return n.pending===0&&t.connected?{}:{warning:n.pending?`includes ${n.pending} operation${n.pending===1?"":"s"} the room has not confirmed${t.connected?"":" \u2014 the connection dropped, reconnecting"}`:"the room connection dropped, reconnecting"}}async readArt({document:e,job:t,layer:n,frame:o=0,trim:s=!1}){let i=await this.#s(e,t),a=await this.peek(i);return{document:i,...a.session.readArt(this.#r(i,n),o,{trim:s}),...this.#c(i)}}async readPng({document:e,job:t,layer:n=null,frame:o=0}){let s=await this.#s(e,t),i=await this.peek(s),a=i.state.doc;if(n==null)return{document:s,png:i.session.png(o,{scale:1}),w:a.w,h:a.h};let c=i.session.cel(this.#r(s,n),o);return{document:s,png:this.#a(a.w,a.h,c),w:a.w,h:a.h}}#a(e,t,n,o=1){let s=this.core.newDocument({w:e,h:t,name:"scratch",seed:1,now:Date.now()}),i=this.core.openSession(s,j(),1);try{let a=i.state();return i.apply([Xe(a.doc.layers[0].id,a.doc.frames[0].id,{x:0,y:0,w:e,h:t},n)],{now:Date.now()}),i.png(0,{scale:o})}finally{i.close()}}async#h(e,t,n,o,s,i=!1){let a=await Xt(t).catch(g=>{throw new Error(`could not read the reference ${t}: ${g.code==="ENOENT"?"no such file":g.message}`)}),c=this.core.decodePng(a),l=e.state.doc,h=Qt(c,l.w,Math.max(1,Math.round(c.h*l.w/c.w))),d=i?de(e.session.composite(o)):e.session.composite(o);i&&de(h.pixels,h.pixels);let u=n.w,p=n.h,w=new Uint8Array((u*2+1)*p*4),m=(u*2+1)*4;for(let g=0;g<p;g++){for(let x=0;x<u;x++){let R=n.x+x,k=n.y+g,T=(k*h.w+R)*4,re=k<h.h&&R<h.w;w.set(re?h.pixels.subarray(T,T+4):[0,0,0,255],g*m+x*4);let D=(k*l.w+R)*4;w.set(d.subarray(D,D+4),g*m+(u+1+x)*4)}w.set([255,0,255,255],g*m+u*4)}let f=h.h!==l.h?` \u2014 the reference is ${c.w}\xD7${c.h}, brought to ${h.w}\xD7${h.h} by width; the canvas is ${l.w}\xD7${l.h}`:"";return{png:this.#a(u*2+1,p,w,s),note:f}}async preview({document:e,job:t,frame:n=0,scale:o=null,rect:s=null,reference:i=null,values:a=!1}){let c=await this.#s(e,t),l=await this.peek(c),{warning:h}=this.#c(c),d=l.state.doc,u=s?rt(s,d.w,d.h):null,p=u?u.w:d.w,w=u?u.h:d.h,m=Number.isInteger(o)&&o>0?o:Yt(i?p*2:p,w),f=Math.max(1,Math.min(m,Math.floor(8192/Math.max(i?p*2:p,w,1)))),g=u?`${u.w}\xD7${u.h} at (${u.x},${u.y}) of ${d.w}\xD7${d.h}`:`${d.w}\xD7${d.h}`,x=f<m?` \u2014 asked for \xD7${m}, held to \xD7${f} so a side stays under 8192px; render a smaller rect to look closer`:"";if(i){let{png:k,note:T}=await this.#h(l,String(i),u??{x:0,y:0,w:d.w,h:d.h},n,f,a);return{png:k,caption:`${c}, frame ${n+1}: reference (left) beside the render (right), ${g} at \xD7${f}${a?", values only":""}${T}${x}${h?` \u2014 ${h}`:""}. Per object, say how each is built \u2014 count the faces, name the light, say where the shadow falls \u2014 and what differs; "reads as" is not a verdict.`}}return{png:a?this.#a(p,w,en(de(l.session.composite(n)),d.w,u??{x:0,y:0,w:d.w,h:d.h}),f):l.session.png(n,{scale:f,...u?{rect:u}:{}}),caption:`${c}, frame ${n+1}: ${g} at \xD7${f} (${p*f}\xD7${w*f}px)${a?", values only \u2014 squint: is there a focal point, and where is the darkest dark":""}${x}${h?` \u2014 ${h}`:""}`}}async sheet({document:e,job:t,columns:n=4,scale:o=2}){let s=await this.#s(e,t),i=await this.peek(s);return{png:i.session.contactSheet({columns:n,scale:o}),caption:`${s}, ${i.state.doc.frames.length} frames, numbered left to right`}}async documents(){let e=[...this.jobs.entries()].map(([c,l])=>({job:c,document:l.room,intent:l.intent})),t=[...this.rooms.keys()],n=null,o=null,s=null,i=this.control?await this.control("/documents",{}).catch(()=>null):null;if(i)n=i.documents?.map(c=>c.id)??null,o=Array.isArray(i.folders)?i.folders.map(c=>typeof c=="string"?c:c?.path??c?.name).filter(Boolean):null,s=i.documents?Object.fromEntries(i.documents.filter(c=>c.folder).map(c=>[c.id,c.folder])):null;else if(this.serverUrl){let c=this.serverUrl.replace(/^ws/,"http").replace(/\/$/,"");n=await fetch(`${c}/documents`).then(l=>l.ok?l.json():null).then(l=>l?.documents?.map(h=>h.id)).catch(()=>null)}return{documents:n?[...new Set([...t,...n])]:t,jobs:e,open:t,...o?{folders:o}:{},...s?{filed:s}:{}}}async createFolder({name:e}){if(!this.control)throw new Error("no library here: this Sprite has no accounts, so there are no folders");let t=String(e??"").trim();if(!t)throw new Error("a folder needs a name");let n=await this.folders();if(n.includes(t))return{folder:t,created:!1,folders:n};let o=t.split("/");for(let s=1;s<=o.length;s++){let i=o.slice(0,s).join("/");n.includes(i)||(await this.control("/documents/folders",{method:"POST",body:{name:i}}),n.push(i))}return{folder:t,created:!0,folders:await this.folders()}}async moveDocuments({documents:e,folder:t=null,make:n=!1}){if(!this.control)throw new Error("no library here: this Sprite has no accounts, so there are no folders");let o=(Array.isArray(e)?e:[e]).map(String).filter(Boolean);if(!o.length)throw new Error("say which documents");let s=t===null||t===""?null:String(t).trim();s&&n&&await this.createFolder({name:s});let i=await this.control("/documents/move",{method:"POST",body:{ids:o,folder:s}});return{moved:o,folder:s,ok:i?.ok!==!1}}async folders(){let e=await this.control("/documents",{});return Array.isArray(e?.folders)?e.folders.map(t=>typeof t=="string"?t:t?.path??t?.name).filter(Boolean):[]}takeRequests(){let e=this.asked.splice(0,this.asked.length);return{requests:e,note:e.length?"Each says what it wants. Where `room` is set, answer in that document. Where it is null the request came from the library and wants a new sprite \u2014 call new_sprite and work there, and do not touch `askedIn`, which is only the room the words travelled through. The person is watching.":"Nobody has asked for anything."}}#r(e,t){let n=this.client(e).state;if(t!=null&&t!==""){let o=typeof t=="number"?n.doc.layers[t]:n.doc.layers.find(s=>s.name===t||s.id===t);if(!o)throw new Error(`no layer ${JSON.stringify(t)} in ${e}`);return o.id}return n.doc.layers[n.doc.layers.length-1].id}async drawArt({job:e,rows:t,colors:n,x:o=0,y:s=0,center:i=!1,clear:a=!1,layer:c,frame:l=0,label:h,palette:d="add",rect:u=null}){let p=this.resolve(e),w=this.client(p.room),m=t,f={x:o,y:s},g=a;if(u){let y=rt(u,Math.max(...t.map(v=>v.length),1),t.length);if(m=t.slice(y.y,y.y+y.h).map(v=>v.slice(y.x,y.x+y.w).padEnd(y.w,".")),f={x:y.x,y:y.y},a){let v=[..."~^`|"].find(M=>!(M in n))??"\xA1";m=m.map(M=>M.replace(/[. ]/g,v)),n={...n,[v]:null},g=!1}}let x=new Set((w.state.doc.palette??[]).map(y=>String(y).toLowerCase())),R=w.session.drawArt(m,n,{layer:c!=null?this.#r(p.room,c):p.layer??this.#r(p.room,null),at:l,x:f.x,y:f.y,center:u?!1:i,clear:g}),{committed:k}=await this.submit(p.job,h||"draw_art",R),T=(this.client(p.room).state.doc.palette??[]).map(y=>String(y).toLowerCase()).filter(y=>!x.has(y)),re=m.reduce((y,v)=>y+[...v].filter(M=>M!=="."&&M!==" ").length,0);if(d==="replace"&&k){let y=[...new Set(Object.values(n).filter(v=>typeof v=="string"&&/^#[0-9a-f]{6}$/i.test(v)).map(v=>v.toLowerCase()))];y.length&&await this.submit(p.job,"set_palette",[{t:"setPalette",colors:L(y,"hue")}])}let D=(this.client(p.room).state.doc.palette??[]).length,ht=d==="replace"&&k?{note:`palette replaced: ${D} color${D===1?"":"s"}, this drawing's`}:T.length?{added:L(T,"hue"),note:`${T.length} color${T.length===1?"":"s"} joined the palette \u2014 draw from a chosen palette, or set_palette({ prune: true }) later`}:{};return{...this.#t(p.room),drew:u?`${m[0]?.length??0}\xD7${m.length} at (${f.x},${f.y})`:`${t[0]?.length??0}\xD7${t.length}`,changed:re,committed:k,...ht}}async resizeCanvas({job:e,w:t,h:n,anchor:o="center"}){let s=this.resolve(e),i=this.client(s.room).state,a=Math.max(1,Math.min(1024,Math.round(Number(t)||i.doc.w))),c=Math.max(1,Math.min(1024,Math.round(Number(n)||i.doc.h)));if(a===i.doc.w&&c===i.doc.h)return{...this.#t(s.room),resized:"already that size"};let l=String(o??"center").toLowerCase(),h=l.includes("left")?0:l.includes("right")?2:1,d=l.includes("top")?0:l.includes("bottom")?2:1,u=Math.round((a-i.doc.w)*h/2),p=Math.round((c-i.doc.h)*d/2);await this.submit(s.job,"resize_canvas",[{t:"resize",w:a,h:c,dx:u,dy:p}]);let w=Z(a,c)??ue(a,c);return{...this.#t(s.room),resized:`${i.doc.w}\xD7${i.doc.h} \u2192 ${a}\xD7${c}, the art kept at ${u},${p}`,...w?{shape:w}:{}}}async closeDocument({document:e}){let t=await this.#s(e,null),n=this.rooms.get(t);if(!n)return{closed:t,open:!1};for(let[o,s]of this.jobs)s.room===t&&this.finish(o);return n.closing=!0,clearTimeout(n.retry),n.client.close(),this.rooms.delete(t),{closed:t,open:!1}}async drawAnimation({job:e,frames:t,colors:n,fps:o,layer:s,label:i}){let a=this.resolve(e),c=this.client(a.room),l=s!=null?this.#r(a.room,s):a.layer??this.#r(a.room,null),h=c.state.doc.frames.length,d=t.length-h;if(d>0){let p=[],w=c.state.doc.frames[h-1]?.index??"V";for(let m=0;m<d;m++)w=w+"V",p.push({t:"addFrame",id:j(),index:w,durMs:pe(o)});await this.submit(a.job,"add frames",p)}if(o){let p=pe(o);await this.submit(a.job,"set fps",c.state.doc.frames.map(w=>({t:"setFrameDur",id:w.id,durMs:p})))}let u=c.session.drawAnimation(t,n,{layer:l});return await this.submit(a.job,i||"draw_animation",u),{...this.#t(a.room),wrote:`${t.length} frames`}}async addLayer({job:e,name:t}){let n=this.resolve(e),o=this.client(n.room).state,s=o.doc.layers[o.doc.layers.length-1];return await this.submit(n.job,"add_layer",[{t:"addLayer",id:j(),index:(s?.index??"V")+"V",name:t||`Layer ${o.doc.layers.length+1}`,visible:!0,locked:!1,opacity:1}]),this.#t(n.room)}async addFrame({job:e,copy:t=!1}){let n=this.resolve(e),o=this.client(n.room),s=o.state,i=s.doc.frames[s.doc.frames.length-1],a=j(),c=[{t:"addFrame",id:a,index:(i?.index??"V")+"V",durMs:i?.durMs??83}];if(t&&i)for(let l of s.doc.layers){let h=o.session.cel(l.id,i.id);h.every(d=>d===0)||c.push(ot(l.id,a,s.doc,h))}return await this.submit(n.job,"add_frame",c),this.#t(n.room)}async setPalette({job:e,colors:t,prune:n=!1}){let o=this.resolve(e),s;if(n){let i=this.client(o.room),a=i.state,c=new Set;for(let h of a.doc.layers)for(let d of a.doc.frames){let u=i.session.cel(h.id,d.id);for(let p=0;p<u.length;p+=4)u[p+3]!==0&&c.add(`#${[u[p],u[p+1],u[p+2]].map(w=>w.toString(16).padStart(2,"0")).join("")}`)}let l=a.doc.palette??[];s=l.filter(h=>c.has(String(h).toLowerCase()));for(let h of c)s.some(d=>String(d).toLowerCase()===h)||s.push(h);return s=L(s,"hue"),await this.submit(o.job,"set_palette",[{t:"setPalette",colors:s}]),{...this.#t(o.room),palette:s.length,pruned:l.length-s.length}}return s=L(t,"hue"),await this.submit(o.job,"set_palette",[{t:"setPalette",colors:s}]),{...this.#t(o.room),palette:s.length}}async setFps({job:e,fps:t}){let n=this.resolve(e),o=this.client(n.room).state,s=pe(t);return await this.submit(n.job,"set_fps",o.doc.frames.map(i=>({t:"setFrameDur",id:i.id,durMs:s}))),{...this.#t(n.room),fps:t}}async clearCel({job:e,layer:t,frame:n=0}){let o=this.resolve(e),i=this.client(o.room).state,a=this.#r(o.room,t??o.layer),c=i.doc.frames[n];if(!c)throw new Error(`no frame ${n} in ${o.room}`);let l=new Uint8Array(i.doc.w*i.doc.h*4);return await this.submit(o.job,"clear",[ot(a,c.id,i.doc,l)]),this.#t(o.room)}async trim({job:e,margin:t=0}){let n=this.resolve(e),o=this.client(n.room),s=o.state,i=this.#r(n.room,n.layer),a=o.session.readArt(i,0,{trim:!0}),c=a.rect.w+t*2,l=a.rect.h+t*2;return c>=s.doc.w&&l>=s.doc.h?{...this.#t(n.room),trimmed:"already tight"}:(await this.submit(n.job,"trim_canvas",[{t:"resize",w:c,h:l,dx:-a.rect.x+t,dy:-a.rect.y+t}]),{...this.#t(n.room),trimmed:`${c}\xD7${l}`})}},tn=new Set(["new_sprite","begin_job","finish_job","draw_art","draw_animation","read_art","render_preview","render_animation","add_layer","add_frame","set_palette","set_fps","clear","trim_canvas","resize_canvas","close_document","get_info","list_documents","challenges","take_requests","create_folder","move_documents"]);function ot(r,e,t,n){return{t:"paintRegion",layer:r,frame:e,rect:{x:0,y:0,w:t.w,h:t.h},rle:nn(n)}}function nn(r){let e=[];if(r.length){let t=[r[0],r[1],r[2],r[3]],n=0,o=()=>{let s=n;do{let i=s&127;s>>=7,e.push(s?i|128:i)}while(s);e.push(...t)};for(let s=0;s<r.length;s+=4)r[s]===t[0]&&r[s+1]===t[1]&&r[s+2]===t[2]&&r[s+3]===t[3]?n++:(o(),t=[r[s],r[s+1],r[s+2],r[s+3]],n=1);o()}return Buffer.from(Uint8Array.from(e)).toString("base64")}function rn(r){let e=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";return new Promise(t=>Kt(e,[r],()=>t()))}function pe(r){return!r||r<=0?83:Math.max(10,Math.round(1e3/r))}function on(r,e,t=8e3){return new Promise(n=>{let o=r.onLeases,s=r.onLost,i=!1,a=l=>{i||(i=!0,clearTimeout(c),r.onLeases=o,r.onLost=s,n(l))};r.onLeases=l=>{o?.(l),l.some(h=>h.job===e)&&a("held")},r.onLost=l=>{s?.(l),l?.ops?.length||a("refused")};let c=setTimeout(()=>a("silent"),t);r.leases.some(l=>l.job===e)&&a("held")})}function st(){return(Date.now()^Math.floor(Math.random()*4294967295))>>>0}import{execFile as sn}from"node:child_process";var an=2e4,cn=5e3,W=class r{constructor({base:e,name:t="agent"}={}){this.base=String(e).replace(/\/$/,""),this.name=t,this.log=[],this.beat=setInterval(()=>void this.announce(),cn),this.beat.unref&&this.beat.unref()}get kind(){return"local"}get where(){return this.base}supports(e){return ln.has(e)}static async probe(e,{timeoutMs:t=700}={}){try{let n=await fetch(`${e.replace(/\/$/,"")}/bridge/status`,{signal:AbortSignal.timeout(t)});if(!n.ok)return null;let o=await n.json();return o?.server==="sprite"?o:null}catch{return null}}async announce(){try{await fetch(`${this.base}/bridge/agent`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:this.name,version:null}),signal:AbortSignal.timeout(2e3)})}catch{}}async call(e,t=[],n=an){let o;try{o=await(await fetch(`${this.base}/bridge/call`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({method:e,args:t,timeoutMs:n}),signal:AbortSignal.timeout(n+2e3)})).json()}catch(s){throw new Error(`lost contact with the Pixie editor at ${this.base}: ${s.message}`)}if(!o.ok)throw new Error(o.error);return this.note(e),o.result}note(e,t=""){this.log.unshift({at:Date.now(),tool:e,room:"local",detail:t}),this.log.length>200&&this.log.pop()}newSprite({name:e,w:t=_.w,h:n=_.h,intent:o,palette:s}){return this.call("newSprite",[{w:t,h:n,name:e,palette:s,intent:o,replace:!1}])}async challenges(){let e=await fetch(`${this.base}/news.json`).catch(()=>null);if(!e?.ok)throw new Error("this editor does not serve the themes \u2014 ask the user what to draw");let t=await e.json(),n=(o,s)=>o?{theme:o.theme,tags:o.tags,detail:o.detail??null,sizes:o.sizes??[],postedAt:new Date(o.at).toISOString(),post:o.url}:{theme:null,note:`no ${s} theme is known right now`};return{daily:n(t?.dailies,"daily"),weekly:n(t?.weekly,"weekly")}}async challenge(e){let t=await this.challenges(),n=e==="weekly"?t.weekly:t.daily;if(!n?.theme)throw new Error(`no ${e} theme is known right now \u2014 ask the user what to draw`);return n}beginJob({document:e,intent:t}){return this.call("beginJob",[{document:e,intent:t}])}finishJob({job:e}){return this.call("finishJob",[{job:e}])}async info({document:e}={}){let t=await this.call("info",[]);return{editor:this.base,...e?{asked:e}:{},...t}}readArt({document:e,layer:t,frame:n}){return this.call("readArt",[{document:e,layer:t,frame:n}])}async preview({document:e,frame:t,scale:n=null,rect:o=null,reference:s=null}){if(o)throw new Error("the local editor cannot render a region \u2014 render the whole frame");if(s)throw new Error("a reference beside the render is a room thing \u2014 in a local editor, read_art({ file }) and compare with references/compare.py");let i=await this.call("toDataURL",[t??null,Number.isInteger(n)&&n>0?n:8,e??null]);return{png:it(i.url),caption:i.caption}}async sheet({document:e,columns:t=0,scale:n=0}){let o=await this.call("toStripDataURL",[{document:e??null,columns:t,scale:n}]);return{png:it(o.url),caption:o.caption}}documents(){return this.call("listDocuments",[])}drawArt({job:e,rows:t,colors:n,x:o,y:s,center:i,clear:a,layer:c,frame:l,label:h,rect:d=null}){if(d){let u=Math.max(0,d.x|0),p=Math.max(0,d.y|0);t=t.slice(p,p+(d.h|0)).map(w=>w.slice(u,u+(d.w|0))),o=u,s=p,i=!1}return this.call("drawArt",[t,n,{job:e,x:o,y:s,center:i,clear:a,layer:c,frame:l,label:h}])}drawAnimation({job:e,frames:t,colors:n,fps:o,layer:s,label:i}){return this.call("drawAnimation",[{frames:t,colors:n,fps:o,job:e,layer:s,label:i}])}addLayer({name:e}){return this.call("addLayer",[e])}addFrame({copy:e=!1}){return this.call("addFrame",[{copy:!!e}])}setPalette({colors:e,prune:t=!1}){if(t)throw new Error("prune works in a room \u2014 in a local editor, pass the colors to keep");return this.call("setPalette",[e])}setFps({fps:e}){return this.call("setFps",[e])}clearCel({job:e,layer:t,frame:n}){return this.call("clear",[{job:e,layer:t,frame:n}])}trim({job:e,margin:t=0}){return this.call("trimCanvas",[{job:e,margin:t}])}setFrame({index:e}){return this.call("setFrame",[e])}setLayer({index:e}){return this.call("setLayer",[e])}select({document:e}){return this.call("selectDocument",[e])}closeDocument({document:e}){return this.call("closeDocument",[e??null])}undo(){return this.call("undo",[])}redo(){return this.call("redo",[])}async openEditor(){if((await r.probe(this.base))?.connected)return{ok:!0,message:`Editor already connected at ${this.base}`};await hn(this.base);let t=Date.now()+1e4;for(;Date.now()<t;){if((await r.probe(this.base))?.connected)return{ok:!0,message:`Editor connected at ${this.base}`};await new Promise(o=>setTimeout(o,200))}return{ok:!1,message:`Opened ${this.base} but nothing connected \u2014 check the browser window.`}}close(){clearInterval(this.beat)}},ln=new Set(["new_sprite","begin_job","finish_job","draw_art","draw_animation","read_art","render_preview","render_animation","add_layer","add_frame","set_palette","set_fps","clear","trim_canvas","get_info","list_documents","challenges","open_editor","select_document","close_document","set_frame","set_layer","undo","redo"]);function it(r){let e=String(r??"").split(",")[1];if(!e)throw new Error("the editor returned something that was not an image");return Buffer.from(e,"base64")}function hn(r){let e=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";return new Promise(t=>sn(e,[r],()=>t()))}import{readFileSync as un}from"node:fs";var dn="0.6.0";function pn(){let r=new URL("./version.json",import.meta.url);try{let e=JSON.parse(un(r,"utf8"))?.version;if(typeof e=="string"&&e)return e;throw new Error("it names no version")}catch(e){throw new Error(`${r.pathname} could not be read as a version: ${e.message}`)}}var at=dn??pn();function me(r="room"){return`Pixel art editor with live collaboration. ${r==="local"?`Documents are tabs in a browser window on this machine; the user is looking
|
|
3
|
+
at it while you work, and can take the canvas back at any moment.`:`Documents are rooms; other people and other agents may be in one with
|
|
5
4
|
you, and they see what you do as you do it.`}
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
JOBS.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
WHAT THE USER SAYS BEATS EVERY DEFAULT
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
THE SPRITE OF THE DAY. Two communities run a standing prompt: Pixel
|
|
40
|
-
Dailies sets a theme every day and Weekly Pixels a word every week. "Make
|
|
41
|
-
the sprite of the day", "today's theme", "the daily", "the weekly", "this
|
|
42
|
-
week's word" all mean those \u2014 call \`challenges\` to read them (the server
|
|
43
|
-
serves them; do not fetch Bluesky yourself), or go straight to
|
|
44
|
-
new_sprite({ challenge: "daily" }) / ({ challenge: "weekly" }), which names
|
|
45
|
-
the sprite after the theme. Draw the theme, not the word: "#Gateway" is a
|
|
46
|
-
gateway. When it is finished, tell the user to post it from the share
|
|
47
|
-
button with that challenge's switch on.
|
|
48
|
-
|
|
49
|
-
SIZE. The default canvas is 80\xD742 \u2014 1.91:1, the ratio link cards crop to \u2014
|
|
50
|
-
so what you make posts edge to edge on Bluesky, Facebook, LinkedIn and X.
|
|
51
|
-
Compose for it: a subject and its surroundings, not a lone figure on empty
|
|
52
|
-
paper. Keep the ratio at any size: 40\xD721, 80\xD742, 120\xD763, 240\xD7126, 480\xD7252,
|
|
53
|
-
600\xD7315 \u2014 examples, not limits, and any multiple of 40\xD721 fills the card.
|
|
54
|
-
A reference sets the size; without one, pick the smallest at which the
|
|
55
|
-
composition reads. A BIG OR GENERATED GRID DOES NOT PASS THROUGH YOU: have
|
|
56
|
-
the script write it to a file \u2014 JSON of draw_art's arguments, or a PNG \u2014
|
|
57
|
-
and call draw_art with \`file\`; the server reads it on the same machine and
|
|
58
|
-
the call is fifty bytes. Iterate against your own render of the file and
|
|
59
|
-
send once. Only a grid you typed by hand goes in \`rows\`, and past ~300
|
|
60
|
-
wide even that goes in bands: several draw_art calls with \`y\`.
|
|
61
|
-
A PROFILE PICTURE is square (40\xD740, 50\xD750, 80\xD780, 100\xD7100) and is cropped to
|
|
62
|
-
a circle wherever it goes, so keep the subject inside one. A BANNER is 3:1
|
|
63
|
-
(60\xD720, 75\xD725, 150\xD750, 300\xD7100). When either is finished, tell the user the
|
|
64
|
-
editor has the export for it: file \u25B8 Save as profile picture (400\xD7400), or
|
|
65
|
-
file \u25B8 Save as profile banner (1500\xD7500) \u2014 both scale by a whole number and
|
|
66
|
-
center on white, with a preview and a nudge. finish_job says so too.
|
|
67
|
-
A game asset \u2014 a tile, an icon, a character for a sheet \u2014 is the size the
|
|
68
|
-
game needs instead, and is the one kind of sprite you trim.
|
|
69
|
-
|
|
70
|
-
NAMES. Name a sprite the way a person would write it on a label \u2014 "Blue cake",
|
|
71
|
-
"Salmon nigiri" \u2014 not blue-cake or salmon_nigiri. The URL is minted from the
|
|
72
|
-
name; the name itself is what people read.
|
|
73
|
-
|
|
74
|
-
THE LIBRARY. Sprites live in folders. "Make five sushi sprites in a folder" is
|
|
75
|
-
five new_sprite calls with the same \`folder\` \u2014 the folder is made on the way.
|
|
76
|
-
Existing sprites are edited by beginning a job on them (list_documents names
|
|
77
|
-
them) and filed with move_documents. If a call is refused as "may not
|
|
78
|
-
organize", this machine was paired before folders were part of the default:
|
|
79
|
-
tell the user to run \`pixie-mcp login\` again.
|
|
80
|
-
|
|
81
|
-
${an}`}var an=`SPEED. Wall clock is round trips times payload, so:
|
|
82
|
-
- Silhouette before detail. At 32px a front-on animal is indistinguishable from
|
|
83
|
-
any other animal; profile is what reads. When the subject could go several
|
|
84
|
-
ways, draw 2-3 rough silhouettes as frames, look at them with
|
|
85
|
-
render_animation, keep the one that reads, and only then add detail. This
|
|
86
|
-
turns a serial mistake into a parallel one.
|
|
87
|
-
- One call per animation. Never loop draw_art plus add_frame.
|
|
88
|
-
- Send changes, not sprites: copyPrevious plus the pixels that moved.
|
|
89
|
-
- Look once, properly. render_animation, not N previews.
|
|
90
|
-
- trim_canvas before you finish a game asset. Overshoot at new_sprite, then
|
|
91
|
-
trim. A piece made to be posted keeps its canvas: the space is the picture.
|
|
92
|
-
- finish_job when the sprite is done, so everyone knows you are gone.
|
|
93
|
-
|
|
94
|
-
Do not ask the user abstract clarifying questions. A human round trip costs
|
|
95
|
-
minutes; a tool round trip costs seconds. Commit to a good default and show
|
|
96
|
-
something.`,cn=le("room"),X=class{constructor({name:e="pixie",version:t=rt,tools:n=[],instructions:o=cn}={}){this.name=e,this.version=t,this.tools=n,this.instructions=o,this.client=null,this.onCall=null}async handle(e){if(!e||e.jsonrpc!=="2.0")return null;let t=e.id===void 0||e.id===null;try{let n=await this.#e(e);return t?null:{jsonrpc:"2.0",id:e.id,result:n}}catch(n){return t?null:{jsonrpc:"2.0",id:e.id,error:{code:-32603,message:n?.message??String(n)}}}}async#e(e){switch(e.method){case"initialize":return this.client=e.params?.clientInfo??null,{protocolVersion:e.params?.protocolVersion??"2024-11-05",capabilities:{tools:{}},serverInfo:{name:this.name,version:this.version},instructions:this.instructions};case"notifications/initialized":case"notifications/cancelled":return{};case"ping":return{};case"tools/list":return{tools:this.tools.map(t=>({name:t.name,description:t.description,inputSchema:t.inputSchema}))};case"tools/call":{let t=this.tools.find(n=>n.name===e.params?.name);if(!t)throw new Error(`unknown tool: ${e.params?.name}`);this.onCall?.(t.name,e.params?.arguments??{});try{return await t.run(e.params?.arguments??{})}catch(n){return{isError:!0,content:[{type:"text",text:n?.message??String(n)}]}}}case"resources/list":return{resources:[]};case"prompts/list":return{prompts:[]};default:throw new Error(`unknown method: ${e.method}`)}}serve(e=process.stdin,t=process.stdout){let n="";return e.setEncoding("utf8"),e.on("data",async o=>{n+=o;let s;for(;(s=n.indexOf(`
|
|
6
|
+
CALL new_sprite FIRST \u2014 before a script, a grid, or a thought about pixels.
|
|
7
|
+
It needs a size, a name and a line of intent, and it puts a labeled
|
|
8
|
+
document in front of the user at once; nothing else should happen while
|
|
9
|
+
they look at an unchanged canvas. A wrong size is resize_canvas on the
|
|
10
|
+
same document, never a second new_sprite.
|
|
11
|
+
|
|
12
|
+
WITH A REFERENCE, MEASURE FIRST. Pixel art that was upscaled or re-encoded
|
|
13
|
+
has a native scale (a whole number of screen pixels per sprite pixel); the
|
|
14
|
+
sprite is made at that size. A photo or a painting has none: choose the
|
|
15
|
+
smallest grid at which the smallest object that matters still reads.
|
|
16
|
+
|
|
17
|
+
AUDIT CONSTRUCTION, NOT RESEMBLANCE, BEFORE YOU FINISH. Per object, crop
|
|
18
|
+
the reference beside yours at one zoom (render_preview with reference and
|
|
19
|
+
rect) and say how each is built: count the faces, name the light, say where
|
|
20
|
+
the shadow falls. "Reads as a house" passed one flat wall three times;
|
|
21
|
+
"theirs shows two walls, mine one" is a verdict. Nothing is done until
|
|
22
|
+
every object has had one.
|
|
23
|
+
|
|
24
|
+
JOBS. A job claims one document; a call that draws is refused without one.
|
|
25
|
+
With one job open omit the job argument; with several pass it. begin_job
|
|
26
|
+
with no document picks up the one you last finished. The user can revoke
|
|
27
|
+
your job \u2014 if a call fails saying the user stopped you, do NOT retry: say
|
|
28
|
+
what you were about to do and ask.
|
|
29
|
+
|
|
30
|
+
WHAT THE USER SAYS BEATS EVERY DEFAULT \u2014 size, shape, palette, ratio,
|
|
31
|
+
artist. Only when nobody said what it should look like do the defaults in
|
|
32
|
+
the tool descriptions apply (new_sprite has the sizes and the naming,
|
|
33
|
+
challenges the daily and weekly themes).
|
|
34
|
+
|
|
35
|
+
A BIG OR GENERATED GRID DOES NOT PASS THROUGH YOU: write it to a file and
|
|
36
|
+
call draw_art with file. Do not ask the user abstract clarifying questions:
|
|
37
|
+
commit to a good default and show something. finish_job when it is done.`}var mn=me("room"),Q=class{constructor({name:e="pixie",version:t=at,tools:n=[],instructions:o=mn}={}){this.name=e,this.version=t,this.tools=n,this.instructions=o,this.client=null,this.onCall=null}async handle(e){if(!e||e.jsonrpc!=="2.0")return null;let t=e.id===void 0||e.id===null;try{let n=await this.#e(e);return t?null:{jsonrpc:"2.0",id:e.id,result:n}}catch(n){return t?null:{jsonrpc:"2.0",id:e.id,error:{code:-32603,message:n?.message??String(n)}}}}async#e(e){switch(e.method){case"initialize":return this.client=e.params?.clientInfo??null,{protocolVersion:e.params?.protocolVersion??"2024-11-05",capabilities:{tools:{}},serverInfo:{name:this.name,version:this.version},instructions:this.instructions};case"notifications/initialized":case"notifications/cancelled":return{};case"ping":return{};case"tools/list":return{tools:this.tools.map(t=>({name:t.name,description:t.description,inputSchema:t.inputSchema}))};case"tools/call":{let t=this.tools.find(n=>n.name===e.params?.name);if(!t)throw new Error(`unknown tool: ${e.params?.name}`);this.onCall?.(t.name,e.params?.arguments??{});try{return await t.run(e.params?.arguments??{})}catch(n){return{isError:!0,content:[{type:"text",text:n?.message??String(n)}]}}}case"resources/list":return{resources:[]};case"prompts/list":return{prompts:[]};default:throw new Error(`unknown method: ${e.method}`)}}serve(e=process.stdin,t=process.stdout){let n="";return e.setEncoding("utf8"),e.on("data",async o=>{n+=o;let s;for(;(s=n.indexOf(`
|
|
97
38
|
`))!==-1;){let i=n.slice(0,s).trim();if(n=n.slice(s+1),!i)continue;let a;try{a=JSON.parse(i)}catch{continue}let c=await this.handle(a);c&&t.write(JSON.stringify(c)+`
|
|
98
|
-
`)}}),this}};var
|
|
39
|
+
`)}}),this}};var G=(r,e)=>{let t=process.argv.indexOf(`--${r}`);return t>-1?process.argv[t+1]:e},fe=process.env.SPRITE_EDITOR||"http://127.0.0.1:4242",ne=process.env.SPRITE_SERVER||"ws://127.0.0.1:4243",wn=new URL("./sprite_core.wasm",import.meta.url);function gn(r){let e=String(r??"").trim();return!e||!/^[a-z0-9]+([-_][a-z0-9]+)*$/.test(e)?e||"an agent":e.split(/[-_]/).map(t=>yn.has(t)?t.toUpperCase():t[0].toUpperCase()+t.slice(1)).join(" ")}var yn=new Set(["cli","ai","api","ide","mcp","sdk","gpt","llm"]);function bn(r){let e=new WebSocket(r),t={readyState:0,onopen:null,onmessage:null,onclose:null,send:n=>e.send(n),close:()=>e.close()};return e.addEventListener("open",()=>{t.readyState=1,t.onopen?.()}),e.addEventListener("message",n=>t.onmessage?.(n.data)),e.addEventListener("close",()=>{t.readyState=3,t.onclose?.()}),e.addEventListener("error",()=>{}),t}async function lt(r,{timeoutMs:e=700}={}){if(await ye(r,{timeoutMs:e}))return!0;try{let t=await fetch(`${b(r)}/health`,{signal:AbortSignal.timeout(e)});return!t.ok||!(t.headers.get("content-type")??"").includes("json")?!1:(await t.json().catch(()=>null))?.ok===!0}catch{return!1}}async function xn({editor:r=fe,server:e=ne,allowHost:t=!0,publicServer:n=z}={}){if(n)return{kind:"room",url:n,why:`the Pixie this agent belongs to, at ${b(n)}`};let o=await W.probe(r);return o?{kind:"local",url:r,why:o.connected?`a Pixie editor is open at ${r}`:`a Pixie server is running at ${r}, with no editor open yet`}:await lt(e)?{kind:"room",url:e,why:`a room server is running at ${e}`}:t?{kind:"room",url:e,start:!0,why:"nothing was running, so this process is the server"}:{kind:"room",url:e,why:`nothing is running yet; waiting for one at ${e}`}}async function Sn({kind:r=null,editor:e=fe,server:t=ne,wasm:n=wn,name:o="agent",allowHost:s=!0}={}){let i=r?{kind:r,url:r==="local"?e:t,why:"you said so"}:await xn({editor:e,server:t,allowHost:s}),a,c=null,l=null;if(i.kind==="local")a=new W({base:i.url,name:o}),a.announce();else{i.start;let u=be(i.url),p=n instanceof Uint8Array?n:await fn(n);c=await q.load(p);let w=await u;a=new Y(c,{author:j(),name:o,connect:(m,f={})=>{let g=new URLSearchParams;f.w&&g.set("w",String(f.w)),f.h&&g.set("h",String(f.h)),f.name&&g.set("name",f.name);let x=g.toString();return bn(`${w}/rooms/${m}${x?`?${x}`:""}`)},probe:()=>lt(i.url)}),a.serverUrl=w,a.getToken=async m=>{let f=await je(i.url,m);if(f!==void 0)return f;if(await ee(i.url))throw new Error(te(i.url))},a.createDocument=async m=>{let f=await _e(i.url,m);if(f!==void 0)return f;if(await ee(i.url))throw new Error(te(i.url))},a.control=async(m,f)=>{let g=await Re(i.url,m,f);if(g!==void 0)return g;throw new Error(await ee(i.url)?te(i.url):"no library here: this Sprite has no accounts, so there are no folders")},a.editorUrl=b(i.url),a.newsUrl=`${b(i.url)}/news.json`,a.hosted=l}let h=new Q({tools:tt(a),instructions:me(a.kind)}),d=h.handle.bind(h);return h.handle=async u=>{let p=await d(u);return u?.method==="initialize"&&h.client?.name&&(a.name=gn(h.client.name),a.announce()),p},{core:c,backend:a,mcp:h,chosen:i}}var ct=null;function ee(r){return ct??=ve(r).catch(()=>!1),ct}function En(r=process.argv){let e=r[1]??"";return/[\\/]pixie-mcp$/.test(e)?"pixie-mcp":/[\\/]sprite-mcp$/.test(e)?"sprite-mcp":"npx pixie-mcp"}function te(r,e=En()){return`this machine has not been given a credential for ${b(r)} yet. Run \`${e} login\` once, in a terminal \u2014 it opens a browser, and one button in it is the whole of it.`}if(De(import.meta.url)){let r=G("server",null)?"room":G("editor",null)?"local":null;if(process.argv.includes("login")){let e=G("server",z||ne);try{let t=await Ue(e,{onUrl:n=>process.stderr.write(`
|
|
99
40
|
${n}
|
|
100
41
|
`),label:"the Pixie MCP server"});process.stderr.write(`
|
|
101
42
|
This machine can now draw as ${t.email??t.name??"you"}.
|
|
@@ -103,6 +44,6 @@ something.`,cn=le("room"),X=class{constructor({name:e="pixie",version:t=rt,tools
|
|
|
103
44
|
`)}catch(t){process.stderr.write(`
|
|
104
45
|
${t.message}
|
|
105
46
|
|
|
106
|
-
`),process.exit(1)}}else{let{mcp:e,chosen:t}=await
|
|
107
|
-
`),e.serve(),t.kind==="room"&&(async()=>(await
|
|
108
|
-
`))()}}export{
|
|
47
|
+
`),process.exit(1)}}else{let{mcp:e,chosen:t}=await Sn({kind:r,editor:G("editor",fe),server:G("server",ne),allowHost:!process.argv.includes("--no-host")});process.stderr.write(`sprite-mcp \u2192 ${t.kind} (${t.why})
|
|
48
|
+
`),e.serve(),t.kind==="room"&&(async()=>(await F(t.url))?.token||await ee(t.url)&&process.stderr.write(`sprite-mcp \u2192 ${te(t.url)}
|
|
49
|
+
`))()}}export{fe as DEFAULT_EDITOR,ne as DEFAULT_SERVER,En as commandName,Sn as createServer,xn as detect,gn as presentable,lt as probeRooms,te as signInFirst,bn as websocketTransport};
|