@popmelt.com/core 0.6.1 → 0.6.2

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.
@@ -1 +1 @@
1
- import{a as _,b as w}from"./chunk-3HWT3PC2.mjs";import{request as O}from"http";function I(e){var m;let u=(m=e==null?void 0:e.basePath)!=null?m:"/popmelt",E=(e==null?void 0:e.bridge)!==!1,o=null,p=null;return{name:"popmelt",hooks:{async"astro:server:setup"({server:d}){var P,y,f,R,b,L;if(!E||process.env.POPMELT_BRIDGE_URL){if(process.env.POPMELT_BRIDGE_URL)try{o=new URL(process.env.POPMELT_BRIDGE_URL).port?parseInt(new URL(process.env.POPMELT_BRIDGE_URL).port,10):null}catch(r){}}else try{let{startPopmelt:r}=await import("./server-RSWDAN3X.mjs"),t=d.config,c=((P=t==null?void 0:t.server)==null?void 0:P.host)===!0?"0.0.0.0":((y=t==null?void 0:t.server)==null?void 0:y.host)||"localhost",l=(R=(f=t==null?void 0:t.server)==null?void 0:f.port)!=null?R:4321,h=`http://${c==="0.0.0.0"?"localhost":c}:${l}`,s=await r({port:e==null?void 0:e.port,projectRoot:e==null?void 0:e.projectRoot,devOrigin:h,force:!0});o=s.port,p=s.close,console.log(`[popmelt] bridge ready at http://localhost:${o}`),(b=d.httpServer)==null||b.on("close",()=>{p&&(p().catch(()=>{}),p=null)})}catch(r){console.warn("[popmelt] bridge failed to start:",(L=r.message)!=null?L:r)}d.middlewares.use((r,t,c)=>{var h;let l=r.url||"";if(l.startsWith(u)){if(o===null)return c();let s=`http://localhost:${o}`,g="/canvas"+l.slice(u.length),a=new URL(g,s),n=O(a,{method:r.method,headers:w(_({},r.headers),{host:a.host})},i=>{t.writeHead(i.statusCode||502,i.headers),i.pipe(t)});n.on("error",()=>{t.writeHead(502,{"Content-Type":"text/plain"}),t.end(`Popmelt bridge not running on port ${o}`)}),r.pipe(n);return}if(o!==null&&((h=r.headers.accept)!=null&&h.includes("text/html")||l==="/"||l.endsWith(".html"))){let s=t.write,g=t.end,a="";t.write=function(n,...i){return a+=typeof n=="string"?n:n.toString("utf8"),!0},t.end=function(n,...i){n&&(a+=typeof n=="string"?n:n.toString("utf8"));let v=`<script type="text/javascript">window.__POPMELT_BRIDGE_URL__="http://localhost:${o}";</script>`;a=a.replace("<head>",`<head>${v}`),t.getHeader("content-length")&&t.setHeader("content-length",Buffer.byteLength(a)),s.call(t,a,"utf8"),g.call(t)}}c()})}}}}export{I as popmelt};
1
+ import{a as _,b as w}from"./chunk-3HWT3PC2.mjs";import{request as O}from"http";function I(e){var m;let u=(m=e==null?void 0:e.basePath)!=null?m:"/popmelt",E=(e==null?void 0:e.bridge)!==!1,o=null,p=null;return{name:"popmelt",hooks:{async"astro:server:setup"({server:d}){var P,y,f,R,b,L;if(!E||process.env.POPMELT_BRIDGE_URL){if(process.env.POPMELT_BRIDGE_URL)try{o=new URL(process.env.POPMELT_BRIDGE_URL).port?parseInt(new URL(process.env.POPMELT_BRIDGE_URL).port,10):null}catch(r){}}else try{let{startPopmelt:r}=await import("./server-56YLMPXH.mjs"),t=d.config,c=((P=t==null?void 0:t.server)==null?void 0:P.host)===!0?"0.0.0.0":((y=t==null?void 0:t.server)==null?void 0:y.host)||"localhost",l=(R=(f=t==null?void 0:t.server)==null?void 0:f.port)!=null?R:4321,h=`http://${c==="0.0.0.0"?"localhost":c}:${l}`,s=await r({port:e==null?void 0:e.port,projectRoot:e==null?void 0:e.projectRoot,devOrigin:h,force:!0});o=s.port,p=s.close,console.log(`[popmelt] bridge ready at http://localhost:${o}`),(b=d.httpServer)==null||b.on("close",()=>{p&&(p().catch(()=>{}),p=null)})}catch(r){console.warn("[popmelt] bridge failed to start:",(L=r.message)!=null?L:r)}d.middlewares.use((r,t,c)=>{var h;let l=r.url||"";if(l.startsWith(u)){if(o===null)return c();let s=`http://localhost:${o}`,g="/canvas"+l.slice(u.length),a=new URL(g,s),n=O(a,{method:r.method,headers:w(_({},r.headers),{host:a.host})},i=>{t.writeHead(i.statusCode||502,i.headers),i.pipe(t)});n.on("error",()=>{t.writeHead(502,{"Content-Type":"text/plain"}),t.end(`Popmelt bridge not running on port ${o}`)}),r.pipe(n);return}if(o!==null&&((h=r.headers.accept)!=null&&h.includes("text/html")||l==="/"||l.endsWith(".html"))){let s=t.write,g=t.end,a="";t.write=function(n,...i){return a+=typeof n=="string"?n:n.toString("utf8"),!0},t.end=function(n,...i){n&&(a+=typeof n=="string"?n:n.toString("utf8"));let v=`<script type="text/javascript">window.__POPMELT_BRIDGE_URL__="http://localhost:${o}";</script>`;a=a.replace("<head>",`<head>${v}`),t.getHeader("content-length")&&t.setHeader("content-length",Buffer.byteLength(a)),s.call(t,a,"utf8"),g.call(t)}}c()})}}}}export{I as popmelt};
@@ -1 +1 @@
1
- import{a as L,b as y}from"./chunk-3HWT3PC2.mjs";import{request as E}from"http";function O(e){var u;let p=(u=e==null?void 0:e.basePath)!=null?u:"/popmelt",w=(e==null?void 0:e.bridge)!==!1,n=null,l=null;return{name:"popmelt",async configureServer(c){var d,g,m,P,R,b;if(!w||process.env.POPMELT_BRIDGE_URL){if(process.env.POPMELT_BRIDGE_URL)try{n=new URL(process.env.POPMELT_BRIDGE_URL).port?parseInt(new URL(process.env.POPMELT_BRIDGE_URL).port,10):null}catch(r){}}else try{let{startPopmelt:r}=await import("./server-RSWDAN3X.mjs"),t=c.config,a=((d=t==null?void 0:t.server)==null?void 0:d.host)===!0?"0.0.0.0":((g=t==null?void 0:t.server)==null?void 0:g.host)||"localhost",s=(P=(m=t==null?void 0:t.server)==null?void 0:m.port)!=null?P:5173,i=`http://${a==="0.0.0.0"?"localhost":a}:${s}`,o=await r({port:e==null?void 0:e.port,projectRoot:e==null?void 0:e.projectRoot,devOrigin:i,force:!0});n=o.port,l=o.close,console.log(`[popmelt] bridge ready at http://localhost:${n}`),(R=c.httpServer)==null||R.on("close",()=>{l&&(l().catch(()=>{}),l=null)})}catch(r){console.warn("[popmelt] bridge failed to start:",(b=r.message)!=null?b:r)}c.middlewares.use((r,t,a)=>{let s=r.url||"";if(!s.startsWith(p)||n===null)return a();let i=`http://localhost:${n}`,o="/canvas"+s.slice(p.length),_=new URL(o,i),f=E(_,{method:r.method,headers:y(L({},r.headers),{host:_.host})},h=>{t.writeHead(h.statusCode||502,h.headers),h.pipe(t)});f.on("error",()=>{t.writeHead(502,{"Content-Type":"text/plain"}),t.end(`Popmelt bridge not running on port ${n}`)}),r.pipe(f)})},transformIndexHtml(){if(n!==null)return[{tag:"script",attrs:{type:"text/javascript"},children:`window.__POPMELT_BRIDGE_URL__="http://localhost:${n}";`,injectTo:"head-prepend"}]}}}export{O as popmelt};
1
+ import{a as L,b as y}from"./chunk-3HWT3PC2.mjs";import{request as E}from"http";function O(e){var u;let p=(u=e==null?void 0:e.basePath)!=null?u:"/popmelt",w=(e==null?void 0:e.bridge)!==!1,n=null,l=null;return{name:"popmelt",async configureServer(c){var d,g,m,P,R,b;if(!w||process.env.POPMELT_BRIDGE_URL){if(process.env.POPMELT_BRIDGE_URL)try{n=new URL(process.env.POPMELT_BRIDGE_URL).port?parseInt(new URL(process.env.POPMELT_BRIDGE_URL).port,10):null}catch(r){}}else try{let{startPopmelt:r}=await import("./server-56YLMPXH.mjs"),t=c.config,a=((d=t==null?void 0:t.server)==null?void 0:d.host)===!0?"0.0.0.0":((g=t==null?void 0:t.server)==null?void 0:g.host)||"localhost",s=(P=(m=t==null?void 0:t.server)==null?void 0:m.port)!=null?P:5173,i=`http://${a==="0.0.0.0"?"localhost":a}:${s}`,o=await r({port:e==null?void 0:e.port,projectRoot:e==null?void 0:e.projectRoot,devOrigin:i,force:!0});n=o.port,l=o.close,console.log(`[popmelt] bridge ready at http://localhost:${n}`),(R=c.httpServer)==null||R.on("close",()=>{l&&(l().catch(()=>{}),l=null)})}catch(r){console.warn("[popmelt] bridge failed to start:",(b=r.message)!=null?b:r)}c.middlewares.use((r,t,a)=>{let s=r.url||"";if(!s.startsWith(p)||n===null)return a();let i=`http://localhost:${n}`,o="/canvas"+s.slice(p.length),_=new URL(o,i),f=E(_,{method:r.method,headers:y(L({},r.headers),{host:_.host})},h=>{t.writeHead(h.statusCode||502,h.headers),h.pipe(t)});f.on("error",()=>{t.writeHead(502,{"Content-Type":"text/plain"}),t.end(`Popmelt bridge not running on port ${n}`)}),r.pipe(f)})},transformIndexHtml(){if(n!==null)return[{tag:"script",attrs:{type:"text/javascript"},children:`window.__POPMELT_BRIDGE_URL__="http://localhost:${n}";`,injectTo:"head-prepend"}]}}}export{O as popmelt};
@@ -0,0 +1,131 @@
1
+ import{a as _,b as Z,c as st,d as de}from"./chunk-3HWT3PC2.mjs";import{execFileSync as cn,spawn as ln}from"child_process";import{createHash as dn,randomUUID as He}from"crypto";import{mkdir as un,readFile as kt,readdir as pn,stat as hn,unlink as fn,writeFile as _e}from"fs/promises";import{createServer as mn}from"http";import{tmpdir as gn}from"os";import{basename as yn,dirname as wn,join as ie}from"path";import{fileURLToPath as vn}from"url";function ot(r,e){return`<!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Popmelt Canvas</title>
6
+ <link rel="preconnect" href="https://fonts.googleapis.com">
7
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
8
+ <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600;700&display=swap" rel="stylesheet">
9
+ <style>*{margin:0;padding:0;box-sizing:border-box}html,body,#root{height:100%;overflow:hidden}</style>
10
+ </head>
11
+ <body>
12
+ <div id="root"></div>
13
+ <script type="importmap">
14
+ {"imports":{
15
+ "react":"https://esm.sh/react@19?dev",
16
+ "react-dom/client":"https://esm.sh/react-dom@19/client?dev",
17
+ "react/jsx-runtime":"https://esm.sh/react@19/jsx-runtime?dev"
18
+ }}
19
+ </script>
20
+ <script type="module">
21
+ import { mountCanvas } from 'http://localhost:${r}/canvas/app.mjs';
22
+ mountCanvas(document.getElementById('root'), {
23
+ devOrigin: '${e}',
24
+ bridgeOrigin: 'http://localhost:${r}',
25
+ });
26
+ </script>
27
+ </body>
28
+ </html>`}import{spawn as Dt}from"child_process";import{createInterface as At}from"readline";var Nt=new Set([".md",".txt",".json",".ts",".tsx",".js",".jsx",".css",".scss",".html",".xml",".yaml",".yml",".toml",".ini",".cfg",".conf",".sh",".bash",".zsh",".py",".rb",".go",".rs",".java",".c",".h",".cpp",".hpp",".swift",".kt",".sql",".graphql",".svg",".env",".gitignore",".prettierrc",".eslintrc"]),rt=1e5;function Bt(r){var o,i,l,c;let e=((o=r.input)==null?void 0:o.file_path)||((i=r.input)==null?void 0:i.path);if(!e)return;let t=e.includes(".")?`.${e.split(".").pop().toLowerCase()}`:"";if(!Nt.has(t))return;let n;if(r.name==="Write"&&typeof((l=r.input)==null?void 0:l.content)=="string"?n=r.input.content:r.name==="Edit"&&typeof((c=r.input)==null?void 0:c.new_string)=="string"&&(n=r.input.new_string),!!n)return n.length>rt?n.slice(0,rt)+`
29
+ \u2026[truncated]`:n}function Re(r,e){let{prompt:t,projectRoot:n,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:l=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:p,model:d,timeoutMs:S=3e5,onEvent:x}=e,k=[];p?k.push("--resume",p,"-p",t):k.push("-p",t),k.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),d&&k.push("--model",d);for(let F of l)k.push("--allowedTools",F);let y=Dt(c,k,{cwd:n,stdio:["ignore","pipe","pipe"],env:Z(_({},process.env),{ANTHROPIC_API_KEY:void 0})}),J=new Promise(F=>{var N;let U,I=[],L=[],D=!1,z="",G=!1,oe=setTimeout(()=>{G=!0,y.kill("SIGTERM"),setTimeout(()=>{try{y.kill("SIGKILL")}catch(b){}},5e3)},S),A=At({input:y.stdout}),j=new Set;A.on("line",b=>{var H,ne,pe,we,ve,Se,xe,Pe,Ie;if(b.trim())try{let Q=JSON.parse(b);Q.session_id&&!U&&(U=Q.session_id);let De=(ne=Q.type)!=null?ne:(H=Q.event)!=null&&H.type?`event.${Q.event.type}`:"unknown";if(j.add(De),Q.type==="result"&&Q.result&&I.length===0){let C=typeof Q.result=="string"?Q.result:"";C&&(I.push(C),x==null||x({type:"delta",jobId:r,text:C},r))}if(Q.type==="assistant"&&Array.isArray((pe=Q.message)==null?void 0:pe.content))for(let C of Q.message.content){if(C.type==="text"&&C.text&&(I.push(C.text),x==null||x({type:"delta",jobId:r,text:C.text},r)),C.type==="tool_use"&&C.name){let Te=((we=C.input)==null?void 0:we.file_path)||((ve=C.input)==null?void 0:ve.path)||void 0,ke=Bt(C);x==null||x(_(_({type:"tool_use",jobId:r,tool:C.name},Te?{file:Te}:{}),ke?{content:ke}:{}),r),C.name==="Edit"&&((Se=C.input)!=null&&Se.file_path)?L.push({tool:"Edit",file_path:C.input.file_path,old_string:C.input.old_string,new_string:C.input.new_string,replace_all:C.input.replace_all}):C.name==="Write"&&((xe=C.input)!=null&&xe.file_path)&&L.push({tool:"Write",file_path:C.input.file_path,content:C.input.content})}C.type==="thinking"&&C.thinking&&(x==null||x({type:"thinking",jobId:r,text:C.thinking},r))}Q.type==="user"&&((Ie=(Pe=Q.tool_use_result)==null?void 0:Pe.file)!=null&&Ie.filePath)&&(x==null||x({type:"tool_use",jobId:r,tool:"Read",file:Q.tool_use_result.file.filePath},r))}catch(Q){}});let g=[];(N=y.stderr)==null||N.on("data",b=>{g.push(b.toString())}),y.on("close",b=>{if(clearTimeout(oe),A.close(),I.length===0&&j.size>0&&console.warn(`[Claude:${r}] No text captured. Event types seen: ${[...j].join(", ")}`),G)D=!0,z=`Timed out after ${Math.round(S/6e4)} minutes`;else if(b!==0&&b!==null){D=!0;let H=g.join("").trim(),ne=I.length===0&&j.size>0?` (no text captured, event types: ${[...j].join(", ")})`:"";z=H||`Claude process exited with code ${b}${ne}`}F({sessionId:U,text:I.join(""),success:!D,error:D?z:void 0,fileEdits:L.length>0?L:void 0})}),y.on("error",b=>{clearTimeout(oe),D=!0,z=b.message,F({sessionId:U,text:I.join(""),success:!1,error:z,fileEdits:L.length>0?L:void 0})})});return{process:y,result:J}}import{spawn as Jt}from"child_process";import{createInterface as Ut}from"readline";function it(r,e){let{prompt:t,projectRoot:n,screenshotPath:o,resumeSessionId:i,model:l,onEvent:c}=e,p=[];i?(p.push("exec","resume",i),l&&p.push("-m",l),p.push("--json","--full-auto",t),o&&p.push("--image",o)):(p.push("exec","--json","--full-auto"),l&&p.push("-m",l),p.push(t),o&&p.push("--image",o));let d=Jt("codex",p,{cwd:n,stdio:["ignore","pipe","pipe"],env:_({},process.env)}),S=new Promise(x=>{var D;let k,y=[],J=!1,F="",U=Ut({input:d.stdout}),I=new Set;U.on("line",z=>{var G,oe,A,j;if(z.trim())try{let g=JSON.parse(z),N=(G=g.type)!=null?G:"unknown";if(I.add(N),N==="thread.started"&&g.thread_id&&!k&&(k=g.thread_id),(N==="item.agentMessage.delta"||N==="item/agentMessage/delta")&&((oe=g.delta)!=null&&oe.text)&&(y.push(g.delta.text),c==null||c({type:"delta",jobId:r,text:g.delta.text},r)),(N==="item.reasoning.delta"||N==="item/reasoning/delta")&&((A=g.delta)!=null&&A.text)&&(c==null||c({type:"thinking",jobId:r,text:g.delta.text},r)),(N==="item.started"||N==="item/started")&&g.item){let b=g.item.type;if(b==="command_execution")c==null||c({type:"tool_use",jobId:r,tool:"Bash"},r);else if(b==="file_change"){let H=g.item.filename||g.item.path;c==null||c(_({type:"tool_use",jobId:r,tool:"Edit"},H?{file:H}:{}),r)}else if(b==="file_read"){let H=g.item.filename||g.item.path;c==null||c(_({type:"tool_use",jobId:r,tool:"Read"},H?{file:H}:{}),r)}else if(b==="web_search")c==null||c({type:"tool_use",jobId:r,tool:"WebSearch"},r);else if(b==="mcp_tool_call"){let H=g.item.tool_name||g.item.name||"MCP";c==null||c({type:"tool_use",jobId:r,tool:H},r)}}if((N==="item.completed"||N==="item/completed")&&g.item){if(g.item.type==="agent_message"){let b=g.item.text;typeof b=="string"&&b&&(y.push(b),c==null||c({type:"delta",jobId:r,text:b},r))}else if(g.item.type==="reasoning"){let b=g.item.text;typeof b=="string"&&b&&(c==null||c({type:"thinking",jobId:r,text:b},r))}}N==="turn.failed"&&(J=!0,F=((j=g.error)==null?void 0:j.message)||g.message||"Turn failed")}catch(g){}});let L=[];(D=d.stderr)==null||D.on("data",z=>{L.push(z.toString())}),d.on("close",z=>{U.close(),y.length===0&&I.size>0&&console.warn(`[Codex:${r}] No text captured. Event types seen: ${[...I].join(", ")}`),z!==0&&z!==null&&(J=!0,F=L.join("")||`Codex process exited with code ${z}`),x({sessionId:k,text:y.join(""),success:!J,error:J?F:void 0})}),d.on("error",z=>{J=!0,F=z.message,x({sessionId:k,text:y.join(""),success:!1,error:F})})});return{process:d,result:S}}import{execFile as Ft}from"child_process";import{copyFile as at,mkdir as ct,readdir as Lt,readFile as zt,writeFile as jt}from"fs/promises";import{join as ue}from"path";var $e=class{constructor(e){this.projectRoot=e;let t=ue(e,".popmelt");this.decisionsDir=ue(t,"decisions"),this.screenshotsDir=ue(t,"screenshots")}async persist(e,t,n){try{await ct(this.decisionsDir,{recursive:!0}),await ct(this.screenshotsDir,{recursive:!0});try{await at(t,ue(this.screenshotsDir,`s-${e.id}.png`))}catch(o){}for(let o=0;o<n.length;o++)try{let i=e.pastedImagePaths[o];i&&await at(n[o],ue(this.screenshotsDir,i.replace("screenshots/","")))}catch(i){}await jt(ue(this.decisionsDir,`d-${e.id}.json`),JSON.stringify(e,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await Lt(this.decisionsDir)).filter(t=>t.startsWith("d-")&&t.endsWith(".json")).map(t=>t.slice(2,-5))}catch(e){return[]}}async loadDecision(e){try{let t=await zt(ue(this.decisionsDir,`d-${e}.json`),"utf-8");return JSON.parse(t)}catch(t){return null}}async loadDecisions(e){return(await Promise.all(e.map(n=>this.loadDecision(n)))).filter(n=>n!==null)}captureGitDiff(e){return new Promise(t=>{Ft("git",["diff","HEAD"],{cwd:e,timeout:5e3,maxBuffer:1024*1024},(n,o)=>{if(n){t(null);return}t(o||null)})})}};import{readFile as lt,writeFile as fe}from"fs/promises";import{join as Ne}from"path";var re="[Materializer]",Ht={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null},Me=class{constructor(e,t,n={}){this.projectRoot=e;this.decisionStore=t;this.options=n;this.cachedIndex=null;this.running=!1;let o=Ne(e,".popmelt");this.indexPath=Ne(o,"materialized.json"),this.modelPath=Ne(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let e=await lt(this.modelPath,"utf-8");return JSON.parse(e)}catch(e){return null}}async addComponent(e){let t=await this.loadModel();t||(t={tokens:{},components:{},rules:[]}),(!t.components||typeof t.components!="object")&&(t.components={});let n=t.components;return n[e]?{added:!1,alreadyExists:!0}:(n[e]={description:""},await fe(this.modelPath,JSON.stringify(t,null,2)),console.log(`${re} Added component "${e}" to model`),{added:!0,alreadyExists:!1})}async updateToken(e,t){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]});let o=e.split("."),i=n;for(let p=0;p<o.length-1;p++){let d=o[p];(!i[d]||typeof i[d]!="object")&&(i[d]={}),i=i[d]}let l=o[o.length-1],c;try{c=JSON.parse(t)}catch(p){c=null}if(c&&typeof c=="object"&&c!==null&&"value"in c)i[l]=c;else{let p=i[l];p&&typeof p=="object"&&p!==null&&"value"in p?p.value=t:i[l]=t}return await fe(this.modelPath,JSON.stringify(n,null,2)),console.log(`${re} Updated token "${e}" \u2192 "${t.slice(0,80)}"`),{updated:!0}}async removeToken(e){let t=await this.loadModel();if(!t)return{removed:!1};let n=e.split("."),o=t;for(let l=0;l<n.length-1;l++){let c=n[l];if(!o[c]||typeof o[c]!="object")return{removed:!1};o=o[c]}let i=n[n.length-1];return i in o?(delete o[i],await fe(this.modelPath,JSON.stringify(t,null,2)),console.log(`${re} Removed token "${e}" from model`),{removed:!0}):{removed:!1}}async removeComponent(e){let t=await this.loadModel();if(!t)return{removed:!1};let n=t.components;return!n||!n[e]?{removed:!1}:(delete n[e],await fe(this.modelPath,JSON.stringify(t,null,2)),console.log(`${re} Removed component "${e}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let e=await this.loadIndex(),t=new Set(e.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(l=>!t.has(l));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(l=>l.resolutions.some(c=>{var d;let p=(d=c.finalScope)!=null?d:c.inferredScope;return(p==null?void 0:p.breadth)==="pattern"}))}async run(){var e,t,n,o,i,l,c;if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let p=await this.getUnmaterializedPatternDecisions();if(p.length===0)return{processedIds:[],success:!0};let d=p.map(U=>U.id);console.log(`${re} Processing ${d.length} pattern-scoped decision(s): ${d.join(", ")}`),(t=(e=this.options).onEvent)==null||t.call(e,{type:"materialize_started",decisionIds:d});let S=await this.loadModel(),x=Gt(p,S),k=!0,y;try{let{result:U}=Re(`mat-${Date.now()}`,{prompt:x,projectRoot:this.projectRoot,maxTurns:(n=this.options.maxTurns)!=null?n:5,maxBudgetUsd:(o=this.options.maxBudgetUsd)!=null?o:.5,allowedTools:["Read"],claudePath:(i=this.options.claudePath)!=null?i:"claude"}),I=await U;if(!I.success)k=!1,y=I.error,console.error(`${re} Claude spawn error:`,y);else{let L=Wt(I.text);L?(await fe(this.modelPath,JSON.stringify(L,null,2)),console.log(`${re} Successfully materialized ${d.length} decision(s) \u2192 ${this.modelPath}`)):(k=!1,y="No <model> block found in response",console.error(`${re} ${y}`))}}catch(U){k=!1,y=U instanceof Error?U.message:String(U),console.error(`${re} Error:`,y)}let J=await this.loadIndex(),F=new Set(J.materializedIds);for(let U of d)F.add(U);return J.materializedIds=[...F],J.lastRunAt=Date.now(),J.lastRunDecisionIds=d,J.lastRunError=y!=null?y:null,await this.persistIndex(J),(c=(l=this.options).onEvent)==null||c.call(l,{type:"materialize_done",decisionIds:d,success:k,error:y}),{processedIds:d,success:k,error:y}}finally{this.running=!1}}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let e=await lt(this.indexPath,"utf-8"),t=JSON.parse(e);return this.cachedIndex=t,t}catch(e){return this.cachedIndex=Z(_({},Ht),{materializedIds:[],lastRunDecisionIds:[]}),this.cachedIndex}}async persistIndex(e){this.cachedIndex=e;try{await fe(this.indexPath,JSON.stringify(e,null,2))}catch(t){console.error(`${re} Failed to write index:`,t)}}};function Wt(r){let e=r.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!(e!=null&&e[1]))return null;try{let t=JSON.parse(e[1]);return typeof t!="object"||t===null||Array.isArray(t)?null:t}catch(t){return null}}function Gt(r,e){let t=r.map(o=>{let l=o.resolutions.filter(d=>{var x;let S=(x=d.finalScope)!=null?x:d.inferredScope;return(S==null?void 0:S.breadth)==="pattern"}).map(d=>{var y,J,F,U;let S=(y=d.finalScope)!=null?y:d.inferredScope,x=(J=S==null?void 0:S.target)!=null?J:"unknown",k=(U=(F=d.filesModified)==null?void 0:F.join(", "))!=null?U:"none";return`- **${d.summary}** [scope: pattern/${x}]
30
+ Files modified: ${k}`}).join(`
31
+ `),c=o.annotations.map(d=>d.instruction).filter(Boolean).join(`
32
+ `),p=o.gitDiff?`
33
+ \`\`\`diff
34
+ ${o.gitDiff.slice(0,2e3)}
35
+ \`\`\``:"";return`### Decision ${o.id} (${new Date(o.createdAt).toISOString()})
36
+ Page: ${o.url}
37
+ ${l}
38
+ ${p}
39
+ ${c?`
40
+ Original instructions:
41
+ ${c}`:""}`}).join(`
42
+
43
+ `);return`You are extracting a local design model from accumulated design decisions.
44
+
45
+ ## Instructions
46
+ 1. Review the current model (if any) and the new decisions below.
47
+ 2. Determine what design tokens, component patterns, and rules to add or update.
48
+ 3. Output the complete updated model as a JSON object inside <model> tags.
49
+
50
+ ${e?`## Current Model
51
+ \`\`\`json
52
+ ${JSON.stringify(e,null,2)}
53
+ \`\`\`
54
+
55
+ Merge the new decisions into the existing model. Preserve existing entries that are not contradicted by new decisions.`:"No model exists yet. Create one from scratch based on the decisions below."}
56
+
57
+ ## Design Decisions to Materialize
58
+ ${t}
59
+
60
+ ## Output Format
61
+ Output the full model inside <model> tags. The model is a JSON object with these sections:
62
+ - \`tokens\`: Design tokens (colors, spacing, typography, etc.)
63
+ - \`components\`: Component-level patterns (e.g., button styles, card layouts)
64
+ - \`rules\`: Array of plain-language rules extracted from decisions
65
+
66
+ Example:
67
+ <model>
68
+ {
69
+ "tokens": {
70
+ "colors": { "primary": "#3b82f6" },
71
+ "spacing": {
72
+ "sm": "4px",
73
+ "md": "8px",
74
+ "section-gap": { "value": "32px", "property": "gap", "bindings": ["gap-8"] }
75
+ }
76
+ },
77
+ "components": {
78
+ "button": { "padding": "12px 24px", "borderRadius": "8px" }
79
+ },
80
+ "rules": [
81
+ "Buttons use 12px vertical padding and 24px horizontal padding",
82
+ "Primary actions use the primary color"
83
+ ]
84
+ }
85
+ </model>
86
+
87
+ ## Guidelines
88
+ - Map token-scoped decisions to \`tokens\`, component-scoped to \`components\`.
89
+ - Extract clear, enforceable rules into the \`rules\` array.
90
+ - When merging, new decisions override conflicting older values.
91
+ - Keep the model concise \u2014 only include patterns with clear evidence from decisions.
92
+ - Do NOT output resolution or question blocks. Just output the <model> block.
93
+ - Spacing tokens can be plain strings ("8px") or objects with code bindings:
94
+ { "value": "8px", "property": "gap", "bindings": ["gap-2"] }
95
+ - property: "gap", "padding", or "margin" \u2014 which CSS property this token controls
96
+ - bindings: Tailwind class names (without responsive prefixes) that use this token
97
+ Include property and bindings when the decision context has class-level evidence.`}import{readFile as pt}from"fs/promises";import{homedir as ht}from"os";import{join as me}from"path";var Je=/popmelt/i;function Ue(){return{found:!1,name:null,scope:null,disabled:!1}}function ge(r,e,t=!1){return{found:!0,name:r,scope:e,disabled:t}}function Ee(r){for(let e of Object.keys(r))if(Je.test(e))return e;return null}async function Be(r){try{let e=await pt(r,"utf-8");return JSON.parse(e)}catch(e){return null}}async function Fe(r){let e=ht(),t=await Be(me(e,".claude.json"));if(t&&typeof t=="object"){let o=t;if(o.mcpServers&&typeof o.mcpServers=="object"){let i=Ee(o.mcpServers);if(i)return ge(i,"user")}if(o.projects&&typeof o.projects=="object"){let l=o.projects[r];if(l&&typeof l=="object"){let c=l;if(c.mcpServers&&typeof c.mcpServers=="object"){let p=Ee(c.mcpServers);if(p){let d=Array.isArray(c.disabledMcpjsonServers)&&c.disabledMcpjsonServers.some(S=>Je.test(S));return ge(p,"project",d)}}}}}let n=await Be(me(r,".mcp.json"));if(n&&typeof n=="object"){let o=n;if(o.mcpServers&&typeof o.mcpServers=="object"){let l=Ee(o.mcpServers);if(l){let c=await dt(r,l);return ge(l,"mcp.json",c)}}let i=Ee(o);if(i&&i!=="mcpServers"){let l=await dt(r,i);return ge(i,"mcp.json",l)}}return Ue()}async function dt(r,e){let t=me(r,".claude","settings.local.json"),n=await Be(t);if(n&&typeof n=="object"){let o=n;if(Array.isArray(o.disabledMcpjsonServers))return o.disabledMcpjsonServers.some(i=>i===e)}return!1}var qt=/^\[mcp_servers\.([^\]]+)\]/;function Qt(r){let e=r.split(`
98
+ `),t=[],n=null;for(let o of e){let i=o.match(qt);i?(n&&t.push({name:n.name,body:n.bodyLines.join(`
99
+ `)}),n={name:i[1],bodyLines:[]}):o.startsWith("[")?n&&(t.push({name:n.name,body:n.bodyLines.join(`
100
+ `)}),n=null):n&&n.bodyLines.push(o)}return n&&t.push({name:n.name,body:n.bodyLines.join(`
101
+ `)}),t}function Xt(r){return/enabled\s*=\s*false/i.test(r)}async function Le(r){let e=process.env.CODEX_HOME||me(ht(),".codex"),t=await ut(me(e,"config.toml"),"user");if(t.found)return t;let n=await ut(me(r,".codex","config.toml"),"project");return n.found?n:Ue()}async function ut(r,e){try{let t=await pt(r,"utf-8"),n=Qt(t);for(let o of n)if(Je.test(o.name)){let i=Xt(o.body);return ge(o.name,e,i)}}catch(t){}return Ue()}import{mkdir as Yt,readFile as ft,writeFile as mt}from"fs/promises";import{homedir as gt}from"os";import{dirname as Vt,join as ze}from"path";var yt="https://mcp.popmelt.com/mcp";async function wt(r=yt){let e=ze(gt(),".claude.json"),t;try{let o=await ft(e,"utf-8");t=JSON.parse(o)}catch(o){t={}}(!t.mcpServers||typeof t.mcpServers!="object")&&(t.mcpServers={});let n=t.mcpServers;for(let o of Object.keys(n))if(/popmelt/i.test(o))return{installed:!1,provider:"claude",scope:null,reason:"already_configured"};return n.popmelt={type:"http",url:r},await mt(e,JSON.stringify(t,null,2)+`
102
+ `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function vt(r=yt){let e=process.env.CODEX_HOME||ze(gt(),".codex"),t=ze(e,"config.toml"),n;try{n=await ft(t,"utf-8")}catch(i){n=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(n))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await Yt(Vt(t),{recursive:!0});let o=`
103
+ [mcp_servers.popmelt]
104
+ url = "${r}"
105
+ `;return await mt(t,n+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function je(r){let t=(r.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!t)throw new Error("Missing multipart boundary");let n=t[1]||t[2],o=await Kt(r),i=Buffer.from(`--${n}`),l=Buffer.from(`--${n}--`),c,p,d,S,x,k,y,J,F,U,I,L,D=[],z=[],G=0,oe=[];for(;G<o.length;){let A=o.indexOf(i,G);if(A===-1)break;let j=A+i.length;if(o.slice(A,A+l.length).equals(l))break;let g=j;o[g]===13&&o[g+1]===10&&(g+=2);let N=o.indexOf(`\r
106
+ \r
107
+ `,g);if(N===-1)break;let b=o.slice(g,N).toString("utf-8"),H=N+4,ne=o.indexOf(i,H),pe=ne!==-1?ne-2:o.length;oe.push({headers:b,body:o.slice(H,pe)}),G=ne!==-1?ne:o.length}for(let A of oe){let j=A.headers.match(/name="([^"]+)"/);if(!j)continue;let g=j[1];if(g==="screenshot")c=A.body;else if(g==="feedback")p=A.body.toString("utf-8");else if(g==="color")d=A.body.toString("utf-8");else if(g==="provider")S=A.body.toString("utf-8");else if(g==="model")x=A.body.toString("utf-8");else if(g==="goal")k=A.body.toString("utf-8");else if(g==="pageUrl")y=A.body.toString("utf-8");else if(g==="viewport")J=A.body.toString("utf-8");else if(g==="planId")F=A.body.toString("utf-8");else if(g==="manifest")U=A.body.toString("utf-8");else if(g==="tasks")I=A.body.toString("utf-8");else if(g==="sourceId")L=A.body.toString("utf-8");else if(g.startsWith("screenshot-")){let N=g.slice(11);try{let b=decodeURIComponent(N);z.push({pathname:b,data:A.body})}catch(b){}}else if(g.startsWith("image-")){let N=g.split("-"),b=parseInt(N[N.length-1],10),H=N.slice(1,-1).join("-");H&&!isNaN(b)&&D.push({annotationId:H,index:b,data:A.body})}}if(!c)throw new Error("Missing screenshot field");return p||(p=""),{screenshot:c,feedback:p,color:d,provider:S,model:x,goal:k,pageUrl:y,viewport:J,planId:F,manifest:U,tasks:I,sourceId:L,pastedImages:D,pageScreenshots:z}}function Kt(r){return new Promise((e,t)=>{let n=[];r.on("data",o=>n.push(o)),r.on("end",()=>e(Buffer.concat(n))),r.on("error",t)})}function ye(r,e){var n,o;let t=[];if(r.annotations.length>0){t.push("## Annotations");for(let i of r.annotations){let l=i.elements.map(d=>{let S=[d.selector];return d.reactComponent&&S.push(`(${d.reactComponent})`),S.join(" ")}).join(", "),c=i.instruction||"No text";t.push(`- id=${i.id} [${i.type}] ${c} \u2192 Elements: ${l||"none"}`);let p=e==null?void 0:e[i.id];if(p&&p.length>0)for(let d of p)t.push(` Attached image: use the Read tool to view ${d}`)}}if(r.styleModifications.length>0){t.push(""),t.push("## Style Changes (make permanent in source)"),t.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let i of r.styleModifications){let l=(n=i.element)!=null&&n.reactComponent?`(${i.element.reactComponent})`:"";for(let c of i.changes)t.push(`- ${i.selector} ${l}: ${c.property} ${c.original} \u2192 ${c.modified}`)}}if((o=r.spacingTokenChanges)!=null&&o.length){t.push(""),t.push("## Spacing Token Changes"),t.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let i of r.spacingTokenChanges){t.push(`
108
+ ### ${i.tokenName}: ${i.originalPx}px \u2192 ${i.newPx}px`);for(let l of i.affectedElements){let c=l.reactComponent?` (${l.reactComponent})`:"";l.matchedClass&&l.suggestedClass?t.push(`- ${l.selector}${c}: \`${l.matchedClass}\` \u2192 \`${l.suggestedClass}\``):t.push(`- ${l.selector}${c}: ${l.property} ${i.originalPx}px \u2192 ${i.newPx}px`),t.push(` class="${l.className}"`)}}}if(r.inspectedElement){let i=r.inspectedElement;t.push(""),t.push("## Inspected Element"),t.push("The developer has this element selected in the inspector:");let l=[i.selector];i.reactComponent&&l.push(`(${i.reactComponent})`),i.context&&l.push(`in ${i.context}`),i.textContent&&l.push(`"${i.textContent.slice(0,80)}"`),t.push(`- ${l.join(" ")}`)}return t.join(`
109
+ `)}function St(r,e,t){var l,c;let n=[],i=new Set(e.annotations.map(p=>p.pathname).filter(Boolean)).size>1;if(n.push("You are reviewing a UI screenshot with developer annotations."),n.push(""),!i&&(t==null?void 0:t.provider)!=="codex"&&(n.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),n.push("")),n.push(`The developer annotated their running app at ${e.url} (${e.viewport.width}x${e.viewport.height}).`),t!=null&&t.threadHistory&&t.threadHistory.length>0){n.push(""),n.push("## Previous Conversation");let p=0;for(let d of t.threadHistory)if(d.role==="human")p++,d.replyToQuestion?(n.push(`### Round ${p} (human) \u2014 reply`),n.push(`"${d.replyToQuestion}"`)):(n.push(`### Round ${p} (human)`),d.feedbackSummary&&n.push(`Annotations: ${d.feedbackSummary}`),d.annotationIds&&d.annotationIds.length>0&&n.push(`Annotation IDs: ${d.annotationIds.join(", ")}`));else if(d.question)n.push(`### Round ${p} (assistant) \u2014 question`),n.push(`"${d.question}"`);else{if(n.push(`### Round ${p} (assistant)`),d.responseText&&n.push(`Response: ${d.responseText}`),d.resolutions&&d.resolutions.length>0)for(let S of d.resolutions){let x=(l=S.finalScope)!=null?l:S.inferredScope,k=x?` [${x.breadth} ${x.target}]`:"";n.push(`- ${S.annotationId}: ${S.status}${k} \u2014 ${S.summary}`),S.filesModified&&S.filesModified.length>0&&n.push(` Files: ${S.filesModified.join(", ")}`)}d.toolsUsed&&d.toolsUsed.length>0&&n.push(`Tools used: ${d.toolsUsed.join(", ")}`)}n.push(""),n.push("The current round is shown in full below.")}if(t!=null&&t.designModel){n.push(""),n.push("## Established Design Policies"),n.push("This project has an established design model (stored in .popmelt/model.json), extracted from the developer's previous design decisions. When making changes, follow these patterns unless the developer explicitly overrides them. When asked about design tokens, component patterns, or design decisions, reference this model as the authoritative source.");let p=t.designModel.rules;if(Array.isArray(p)&&p.length>0){n.push(""),n.push("Rules:");for(let x of p)typeof x=="string"&&n.push(`- ${x}`)}let d=t.designModel.tokens;d&&typeof d=="object"&&(n.push(""),n.push("Design tokens:"),n.push("```json"),n.push(JSON.stringify(d,null,2)),n.push("```"));let S=t.designModel.components;S&&typeof S=="object"&&(n.push(""),n.push("Component patterns:"),n.push("```json"),n.push(JSON.stringify(S,null,2)),n.push("```")),n.push(""),n.push("### Novel Pattern Detection"),n.push("When you make a design decision that has no matching policy in the model above (e.g., styling a component type not yet in the model, choosing a color with no token, picking spacing with no rule), flag it:"),n.push("<novel>"),n.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),n.push("</novel>"),n.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),n.push("- `element`: What you are styling or creating"),n.push("- `decision`: What you decided to do (specific values)"),n.push("- `reason`: Why this is novel (what is missing from the model)"),n.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}if(t!=null&&t.designModel||(n.push(""),n.push("## Design Context"),n.push("This project uses Popmelt for design governance. Design decisions are stored in .popmelt/decisions/ (JSON files). A materialized design model may exist at .popmelt/model.json. When the developer asks about design tokens, patterns, or past decisions, check these files first before searching source code.")),i){let p=(c=t==null?void 0:t.screenshotPaths)!=null?c:{},d=new Map;for(let S of e.annotations){let x=S.pathname||new URL(e.url).pathname;d.has(x)||d.set(x,[]),d.get(x).push(S)}n.push(""),n.push("The developer annotated multiple pages. Each page section includes its own screenshot where available.");for(let[S,x]of d){n.push(""),n.push(`## Page: ${S}`);let k=p[S];k&&(t==null?void 0:t.provider)!=="codex"?n.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${k}`):!k&&(t==null?void 0:t.provider)!=="codex"&&(Object.keys(p).length===0?(n.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${r}`),n.push("(This screenshot shows the page the developer was on when submitting \u2014 not this page)")):n.push("(No screenshot available for this page \u2014 work from the annotation text and selectors)"));let y=Z(_({},e),{annotations:x,styleModifications:e.styleModifications}),J=ye(y,t==null?void 0:t.imagePaths);J&&(n.push(""),n.push(J))}}else{let p=ye(e,t==null?void 0:t.imagePaths);p&&(n.push(""),n.push(p))}return n.push(""),n.push("Follow the developer's instructions. If they ask for changes, apply them to the source files \u2014 the dev server has HMR so changes appear immediately. If they ask a question or request analysis, respond in text without modifying code."),n.push(""),n.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),n.push(""),n.push("## Resolution"),n.push("After completing all work, output a resolution block listing what you did for each annotation:"),n.push("<resolution>"),n.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),n.push("</resolution>"),n.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),n.push(""),n.push("### Scope classification"),n.push("Each resolution MUST include scope fields:"),n.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),n.push("- `inferredScope`: What scope the change actually has, based on what you modified."),n.push("Scope has two dimensions:"),n.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),n.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),n.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),n.push("If you cannot confidently determine scope, set it to null."),n.push(""),n.push("## Questions"),n.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),n.push('<question>What do you mean by "..."?</question>'),n.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),n.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),n.join(`
110
+ `)}function xt(r){var t;let e=r.match(/<question>\s*([\s\S]*?)\s*<\/question>/);return(t=e==null?void 0:e[1])!=null?t:null}function Pt(r,e,t,n){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),t!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`);let i=e.find(l=>l.role==="human"&&l.feedbackContext);if(i!=null&&i.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),e.length>0){o.push(""),o.push("## Conversation History");let l=0;for(let c of e)c.role==="human"?(l++,c.replyToQuestion?(o.push(`### Round ${l} (human) \u2014 reply`),o.push(`"${c.replyToQuestion}"`)):(o.push(`### Round ${l} (human)`),c.feedbackSummary&&o.push(`Annotations: ${c.feedbackSummary}`))):c.question?(o.push(`### Round ${l} (assistant) \u2014 question`),o.push(`"${c.question}"`)):(o.push(`### Round ${l} (assistant)`),c.responseText&&o.push(`Response: ${c.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),n&&n.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let l of n)o.push(`Attached image: use the Read tool to view the image at: ${l}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
111
+ `)}function Zt(r){if(typeof r!="object"||r===null)return!1;let e=r;return(e.breadth==="instance"||e.breadth==="pattern")&&(e.target==="element"||e.target==="component"||e.target==="token")}function en(r){if(typeof r!="object"||r===null||typeof r.annotationId!="string"||r.status!=="resolved"&&r.status!=="needs_review"||typeof r.summary!="string")return!1;let e=r;for(let t of["declaredScope","inferredScope","finalScope"])if(e[t]!==void 0&&e[t]!==null&&!Zt(e[t]))return!1;return!0}function It(r){let e=r.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!e||!e[1])return[];try{let t=JSON.parse(e[1]);return Array.isArray(t)?t.filter(en):[]}catch(t){return[]}}function Tt(r){let e=r.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!(e!=null&&e[1]))return[];try{let t=JSON.parse(e[1]);return Array.isArray(t)?t.filter(n=>{if(typeof n!="object"||n===null)return!1;let o=n;return(o.category==="token"||o.category==="component"||o.category==="element")&&typeof o.element=="string"&&typeof o.decision=="string"&&typeof o.reason=="string"}):[]}catch(t){return[]}}var Ce=class{constructor(e=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=e}setProcessor(e){this.processor=e}get active(){let e=this.activeJobs.values().next();return e.done?null:e.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(e,t){t?this.activeProcesses.set(e,t):this.activeProcesses.delete(e)}enqueue(e){return this.queue.push(e),this.processNext(),this.queue.length+this.activeJobs.size}addListener(e){return this.listeners.add(e),()=>this.listeners.delete(e)}broadcast(e,t,n){let o=this.bufferEvent(t,e);for(let i of this.listeners)i(o,t,n)}bufferEvent(e,t){let n=this.eventBuffers.get(e);n||(n={events:[],nextSeq:0},this.eventBuffers.set(e,n));let o=Z(_({},t),{seq:n.nextSeq++});return n.events.push(o),n.events.length>1e4&&n.events.splice(0,n.events.length-1e4),o}getBufferedEvents(e,t=-1){var c;let n=this.eventBuffers.get(e),o=(c=this.accumulators.get(e))!=null?c:{response:"",thinking:""},i=this.activeJobs.has(e);if(!n)return null;let l=t<0?n.events:n.events.filter(p=>p.seq>t);return{jobId:e,events:l,currentSeq:n.nextSeq-1,accumulated:_({},o),jobActive:i}}accumulateText(e,t,n){let o=this.accumulators.get(e);o||(o={response:"",thinking:""},this.accumulators.set(e,o)),o[t]+=n}getAccumulated(e){var t;return(t=this.accumulators.get(e))!=null?t:null}scheduleBufferCleanup(e){let t=this.cleanupTimers.get(e);t&&clearTimeout(t);let n=setTimeout(()=>{this.eventBuffers.delete(e),this.accumulators.delete(e),this.cleanupTimers.delete(e)},6e4);this.cleanupTimers.set(e,n)}cancelJob(e){let t=this.activeProcesses.get(e),n=this.activeJobs.get(e);return!t||!n?!1:(t.kill("SIGTERM"),this.activeProcesses.delete(e),this.activeJobs.delete(e),n.status="error",n.error="Cancelled by user",this.broadcast({type:"error",jobId:n.id,message:"Cancelled by user",cancelled:!0},n.id,n.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let e=Array.from(this.activeJobs.keys());for(let t of e)this.cancelJob(t);return!0}destroy(){for(let e of this.activeProcesses.values())e.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}async destroyAsync(e=1e4){let t=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),t.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let n of t)try{n.kill("SIGTERM")}catch(o){}await Promise.all(t.map(n=>new Promise(o=>{let i=!1,l=()=>{i||(i=!0,o())};n.on("exit",l),n.on("error",l),setTimeout(()=>{if(!i){try{n.kill("SIGKILL")}catch(c){}setTimeout(l,500)}},e)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let n of this.cleanupTimers.values())clearTimeout(n);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let e=this.queue.shift();this.activeJobs.set(e.id,e),e.status="running",this.broadcast({type:"job_started",jobId:e.id,position:0,threadId:e.threadId},e.id,e.sourceId),this.processor(e).catch(t=>{e.status="error",e.error=t instanceof Error?t.message:String(t),this.broadcast({type:"error",jobId:e.id,message:e.error},e.id,e.sourceId)}).finally(()=>{this.activeJobs.delete(e.id),this.activeProcesses.delete(e.id),this.scheduleBufferCleanup(e.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},e.id)})}}};import{mkdir as tn,readFile as nn,writeFile as sn}from"fs/promises";import{dirname as on,join as rn}from"path";var an={version:1,threads:{}},Oe=class{constructor(e){this.cache=null;this.writeChain=Promise.resolve();this.filePath=rn(e,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let e=await nn(this.filePath,"utf-8"),t=JSON.parse(e);if(t&&t.version===1&&t.threads)return this.cache=t,this.cache}catch(e){}return this.cache=Z(_({},an),{threads:{}}),this.cache}async getThread(e){var n;return(n=(await this.load()).threads[e])!=null?n:null}async findContinuationThread(e){if(e.length===0)return null;let t=await this.load(),n=new Set(e);for(let o of Object.values(t.threads))if(o.elementIdentifiers.some(l=>n.has(l)))return o;return null}async createThread(e,t){let n=await this.load(),o={id:e,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:t,messages:[]};return n.threads[e]=o,await this.persist(),o}async appendMessage(e,t){let o=(await this.load()).threads[e];o&&(o.messages.push(t),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(e,t){let o=(await this.load()).threads[e];if(!o)return;let i=new Set(o.elementIdentifiers);for(let l of t)i.has(l)||o.elementIdentifiers.push(l);o.updatedAt=Date.now(),await this.persist()}async getThreadHistory(e,t=6){let n=await this.getThread(e);return!n||n.messages.length===0?[]:n.messages.length<=t?n.messages:[n.messages[0],...n.messages.slice(-(t-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await tn(on(this.filePath),{recursive:!0}),await sn(this.filePath,JSON.stringify(this.cache,null,2))}catch(e){console.error("[ThreadStore] Failed to persist:",e)}}),await this.writeChain}};var $n={};var Sn=1111,xn=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],Pn=1800*1e3,In=3600*1e3;function Rt(r){if(!r)return!1;try{let e=new URL(r);return e.hostname==="localhost"||e.hostname==="127.0.0.1"}catch(e){return!1}}function Tn(r,e){let t=r.headers.origin;Rt(t)&&(e.setHeader("Access-Control-Allow-Origin",t),e.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),e.setHeader("Access-Control-Allow-Headers","Content-Type"))}function T(r,e,t){r.writeHead(e,{"Content-Type":"application/json"}),r.end(JSON.stringify(t))}function kn(r,e){if(!r)return e;let t=r.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!t)return e;let[,n,o,i]=t;return`\x1B[38;2;${parseInt(n,16)};${parseInt(o,16)};${parseInt(i,16)}m${e}\x1B[0m`}function We(r,e){try{r.res.write(`event: ${e.type}
112
+ data: ${JSON.stringify(e)}
113
+
114
+ `)}catch(t){}}async function bn(r){try{let e=new AbortController,t=setTimeout(()=>e.abort(),500),n=await fetch(`http://127.0.0.1:${r}/status`,{signal:e.signal});return clearTimeout(t),n.ok?await n.json():null}catch(e){return null}}function Rn(r,e){return new Promise((t,n)=>{let o=l=>{r.removeListener("listening",i),n(l)},i=()=>{r.removeListener("error",o),t()};r.once("error",o),r.once("listening",i),r.listen(e,"127.0.0.1")})}async function $t(r={}){var Qe,Xe,Ye,Ve,Ke,Ze,et,tt,nt;let e=(Qe=r.port)!=null?Qe:Sn,t=(Xe=r.projectRoot)!=null?Xe:process.cwd(),n=dn("sha256").update(t).digest("hex").slice(0,12),o=(Ye=r.devOrigin)!=null?Ye:process.env.PORT?`http://localhost:${process.env.PORT}`:null,i=(Ve=r.tempDir)!=null?Ve:ie(gn(),"popmelt-bridge"),l=(Ke=r.maxTurns)!=null?Ke:40,c=(Ze=r.maxBudgetUsd)!=null?Ze:1,p=[...(et=r.allowedTools)!=null?et:xn],d=(tt=r.claudePath)!=null?tt:"claude",S=(nt=r.provider)!=null?nt:"claude",x=r.timeoutMs,k=e,y={};for(let s of["claude","codex"])try{let a=cn("which",[s],{encoding:"utf-8"}).trim();y[s]={available:!0,path:a}}catch(a){y[s]={available:!1,path:null}}function J(s,a){return new Promise(h=>{let u=ln(a,["--version"],{stdio:["ignore","ignore","ignore"]}),w=!1,f=$=>{w||(w=!0,h($))},E=setTimeout(()=>{u.kill("SIGTERM"),f(!0)},5e3);u.on("error",()=>{clearTimeout(E),f(!1)}),u.on("close",$=>{clearTimeout(E),f($===0)})})}let[F,U]=await Promise.all([Fe(t),Le(t)]);y.claude&&(y.claude.mcp=F),y.codex&&(y.codex.mcp=U),F.found&&F.name&&p.push(`mcp__${F.name}__*`),await un(i,{recursive:!0}),bt(i).catch(()=>{});let I=new Ce(1),L=new Set,D=new Oe(t),z=new $e(t),G=new Me(t,z,{claudePath:d,onEvent:s=>{for(let a of L)We(a,s)}}),oe=20,A=300*1e3,j=[],g=null,N;I.addListener((s,a,h)=>{for(let u of L)(!h||!u.sourceId||u.sourceId===h)&&We(u,s)}),I.setProcessor(async s=>{var X,K,te,ee,le,ae;let a=s._replyPrompt,h=s._replyImagePaths,u=(X=s.provider)!=null?X:S,w;if(s.threadId){let R=await D.getThread(s.threadId);if(R){for(let O=R.messages.length-1;O>=0;O--)if(R.messages[O].sessionId){w=R.messages[O].sessionId;break}}}let f;if(w&&a){let R=(K=await D.getThread(s.threadId))==null?void 0:K.messages.filter(se=>se.role==="human").pop();if(f=(R==null?void 0:R.replyToQuestion)||(R==null?void 0:R.feedbackSummary)||"",h&&h.length>0){f+=`
115
+
116
+ The developer attached reference images with their reply:`;for(let se of h)f+=`
117
+ Attached image: use the Read tool to view the image at: ${se}`}f+=`
118
+
119
+ After completing work, output a <resolution> block with declaredScope and inferredScope. If the developer corrected scope, set finalScope. If unclear, output a <question> block.`}else if(w)f=ye(s.feedback,s.imagePaths)+`
120
+
121
+ Follow the developer's instructions. If they ask for changes, apply them to the source files.
122
+
123
+ After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(u!=="codex"?`
124
+
125
+ IMPORTANT: First, use the Read tool to view the updated screenshot at: ${s.screenshotPath}`:"");else{let R=!a&&s.threadId?await D.getThreadHistory(s.threadId):void 0,O=a?null:await G.loadModel();f=a!=null?a:St(s.screenshotPath,s.feedback,{threadHistory:R&&R.length>0?R:void 0,provider:u,imagePaths:s.imagePaths,designModel:O!=null?O:void 0,screenshotPaths:s.screenshotPaths})}let E=kn(s.color,`[\u22B9 ${k}:${s.id}]`),$=s.screenshotPaths&&Object.keys(s.screenshotPaths).length>0?`${Object.keys(s.screenshotPaths).length} pages [${Object.keys(s.screenshotPaths).join(", ")}]`:s.screenshotPath;console.log(`${E} Reviewing ${$} (provider: ${u})${s.threadId?` (thread: ${s.threadId})`:""}${w?` (resuming: ${w.slice(0,8)})`:""}`);let q=(R,O)=>{R.type==="delta"&&"text"in R?I.accumulateText(O,"response",R.text):R.type==="thinking"&&"text"in R&&I.accumulateText(O,"thinking",R.text),I.broadcast(R,O,s.sourceId)},{process:M,result:V}=u==="codex"?it(s.id,{prompt:f,projectRoot:t,screenshotPath:s.screenshotPath,resumeSessionId:w,model:s.model,onEvent:q}):Re(s.id,{prompt:f,projectRoot:t,maxTurns:l,maxBudgetUsd:c,allowedTools:p,claudePath:d,resumeSessionId:w,model:s.model,timeoutMs:x,onEvent:q});I.setActiveProcess(s.id,M);let v=await V;if(s.result=v.text,v.success){console.log(`${E} Iteration complete`),v.fileEdits&&v.fileEdits.length>0&&console.log(`${E} Captured ${v.fileEdits.length} file edit(s): ${v.fileEdits.map(Y=>`${Y.tool} ${Y.file_path}`).join(", ")}`),s.status="done";let R=xt(v.text),O=It(v.text);if(O.length>0&&s.annotationIds&&s.annotationIds.length>0){let Y=new Set(s.annotationIds);O.every(P=>Y.has(P.annotationId))||(O=O.map((P,B)=>Z(_({},P),{annotationId:s.annotationIds[B%s.annotationIds.length]})))}let se=v.fileEdits&&v.fileEdits.length>0?v.fileEdits.map(Y=>`${Y.tool} ${Y.file_path.split("/").pop()}`):void 0;s.threadId&&await D.appendMessage(s.threadId,{role:"assistant",timestamp:Date.now(),jobId:s.id,responseText:v.text,resolutions:O.length>0?O:void 0,question:R!=null?R:void 0,sessionId:v.sessionId,toolsUsed:se,model:s.model,provider:s.provider}),z.captureGitDiff(t).then(async Y=>{var ce;let m=Date.now(),P=s.imagePaths?Object.values(s.imagePaths).flat():[],B=[];if(s.imagePaths)for(let[Ot,_t]of Object.entries(s.imagePaths))for(let Ae=0;Ae<_t.length;Ae++)B.push(`screenshots/p-${s.id}-${Ot}-${Ae}.png`);await z.persist({version:1,id:s.id,createdAt:s.createdAt,completedAt:m,durationMs:m-s.createdAt,url:s.feedback.url,viewport:s.feedback.viewport,screenshotPath:`screenshots/s-${s.id}.png`,pastedImagePaths:B,annotations:s.feedback.annotations,styleModifications:s.feedback.styleModifications,inspectedElement:s.feedback.inspectedElement,provider:s.provider,model:s.model,sessionId:v.sessionId,threadId:s.threadId,responseText:v.text,resolutions:O.length>0?O:[],question:R!=null?R:void 0,fileEdits:(ce=v.fileEdits)!=null?ce:[],toolsUsed:se,gitDiff:Y},s.screenshotPath,P)}).catch(()=>{}),O.length>0&&O.some(m=>{var B;let P=(B=m.finalScope)!=null?B:m.inferredScope;return(P==null?void 0:P.breadth)==="pattern"})&&G.run().catch(()=>{}),R&&(console.log(`${E} \u{1F4AC} Question detected: "${R.slice(0,120)}" \u2192 broadcasting to ${L.size} SSE clients (threadId=${(te=s.threadId)!=null?te:s.id}, annotationIds=${(le=(ee=s.annotationIds)==null?void 0:ee.join(","))!=null?le:"none"})`),I.broadcast({type:"question",jobId:s.id,threadId:(ae=s.threadId)!=null?ae:s.id,question:R,annotationIds:s.annotationIds},s.id,s.sourceId));let he=Tt(v.text);he.length>0&&(console.log(`${E} Novel pattern(s): ${he.map(Y=>`${Y.category}/${Y.element}`).join(", ")}`),I.broadcast({type:"novel_patterns",jobId:s.id,patterns:he,threadId:s.threadId},s.id,s.sourceId)),I.broadcast({type:"done",jobId:s.id,success:!0,resolutions:O.length>0?O:void 0,responseText:v.text,threadId:s.threadId},s.id,s.sourceId),j.push({id:s.id,status:"done",completedAt:Date.now(),threadId:s.threadId,annotationIds:s.annotationIds})}else console.error(`${E} Error: ${v.error}`),s.status="error",s.error=v.error,s.threadId&&await D.appendMessage(s.threadId,{role:"assistant",timestamp:Date.now(),jobId:s.id,error:v.error||"Unknown error",model:s.model,provider:s.provider}),I.broadcast({type:"error",jobId:s.id,threadId:s.threadId,message:v.error||"Unknown error"},s.id,s.sourceId),j.push({id:s.id,status:"error",completedAt:Date.now(),error:v.error,threadId:s.threadId,annotationIds:s.annotationIds});let W=Date.now()-A;for(;j.length>0&&(j[0].completedAt<W||j.length>oe);)j.shift()});let b=mn(async(s,a)=>{var E;if(Tn(s,a),s.method==="OPTIONS"){a.writeHead(204),a.end();return}let h=s.url||"/",u=h.split("?")[0],w=new URL(h,`http://127.0.0.1:${k}`),f=w.pathname;try{if(s.method==="POST"&&f==="/send")await H(s,a);else if(s.method==="GET"&&f==="/events")pe(s,a);else if(s.method==="GET"&&f==="/status")we(a);else if(s.method==="GET"&&f==="/capabilities")T(a,200,{providers:y});else if(s.method==="POST"&&f==="/mcp/install")await xe(s,a);else if(s.method==="POST"&&f==="/reply")await ne(s,a);else if(s.method==="POST"&&f==="/cancel")ve(s,a);else if(s.method==="POST"&&f==="/materialize")await Se(a);else if(s.method==="POST"&&f==="/model/component")await Pe(s,a);else if(s.method==="DELETE"&&f==="/model/component")await Ie(s,a);else if(s.method==="PATCH"&&f==="/model/token")await Q(s,a);else if(s.method==="DELETE"&&f==="/model/token")await De(s,a);else if(s.method==="GET"&&f==="/model"){let $=await G.loadModel();T(a,200,{model:$})}else if(s.method==="GET"&&f.startsWith("/jobs/")&&f.endsWith("/events")){let $=f.slice(6,f.length-7),q=parseInt((E=w.searchParams.get("afterSeq"))!=null?E:"-1",10),M=I.getBufferedEvents($,isNaN(q)?-1:q);M?T(a,200,M):T(a,404,{error:"Unknown job"})}else if(s.method==="GET"&&u.startsWith("/files/"))await Mt(u.slice(7),a);else if(s.method==="GET"&&f.startsWith("/thread/")){let $=f.slice(8);await Et($,a)}else s.method==="GET"&&(f==="/canvas"||f==="/canvas/")?C(s,a):s.method==="GET"&&f==="/canvas/manifest"?await Te(a):s.method==="GET"&&f==="/canvas/app.mjs"?await ke(a):T(a,404,{error:"Not found"})}catch($){console.error("[Bridge] Request error:",$),T(a,500,{error:$ instanceof Error?$.message:"Internal error"})}});async function H(s,a){let{screenshot:h,feedback:u,color:w,provider:f,model:E,sourceId:$,pastedImages:q,pageScreenshots:M}=await je(s),V;try{V=JSON.parse(u)}catch(m){T(a,400,{error:"Invalid feedback JSON"});return}let v=He().slice(0,8),W={};if(M.length>0)for(let m of M){let P=encodeURIComponent(m.pathname),B=ie(i,`screenshot-${v}-${P}.png`);await _e(B,m.data),W[m.pathname]=B}let X=ie(i,`screenshot-${v}.png`);await _e(X,h);let K={};if(q.length>0)for(let m of q){let P=ie(i,`pasted-${v}-${m.annotationId}-${m.index}.png`);await _e(P,m.data),K[m.annotationId]||(K[m.annotationId]=[]),K[m.annotationId].push(P)}let te=V.annotations.map(m=>m.linkedSelector?m.pathname?`${m.pathname}:${m.linkedSelector}`:m.linkedSelector:null).filter(m=>!!m),ee;if(te.length>0){let m=await D.findContinuationThread(te);m?(ee=m.id,await D.addElementIdentifiers(ee,te)):ee=(await D.createThread(v,te)).id}else ee=(await D.createThread(v,[])).id;let le=V.annotations.map(m=>m.id),ae=Object.keys(W).length>0,R=_(Z(_({id:v,status:"queued",screenshotPath:X,feedback:V,createdAt:Date.now(),color:w,threadId:ee,annotationIds:le,provider:f==="claude"||f==="codex"?f:void 0,model:E||void 0},Object.keys(K).length>0?{imagePaths:K}:{}),{sourceId:$||void 0}),ae?{screenshotPaths:W}:{}),O=new Set(V.annotations.map(m=>m.pathname).filter(Boolean)),se;if(O.size>1){let m=new Map;for(let P of V.annotations){let B=P.pathname||"(unknown)";m.has(B)||m.set(B,[]),m.get(B).push(P.instruction||`[${P.type}]`)}se=[...m.entries()].map(([P,B])=>`\`${P}\`
126
+ ${B.map(ce=>`- ${ce}`).join(`
127
+ `)}`).join(`
128
+ `)}else se=V.annotations.map(m=>m.instruction||`[${m.type}]`).join("; ");let he=ye(V,Object.keys(K).length>0?K:void 0);await D.appendMessage(ee,Z(_(_({role:"human",timestamp:Date.now(),jobId:v,screenshotPath:X},ae?{screenshotPaths:W}:{}),Object.keys(K).length>0?{imagePaths:K}:{}),{annotationIds:le,feedbackSummary:se,feedbackContext:he||void 0}));let Y=I.enqueue(R);T(a,200,{jobId:v,position:Y,threadId:ee})}async function ne(s,a){let h=s.headers["content-type"]||"",u,w,f,E,$,q,M=[];if(h.includes("multipart/form-data")){let m=await je(s),P=m.feedback?JSON.parse(m.feedback):{};u=P.threadId,w=P.reply,f=P.color,E=P.provider,$=P.model,q=P.sourceId||m.sourceId;for(let B of m.pastedImages)M.push(B.data)}else{let m=[];try{for(var O=de(s),se,he,Y;se=!(he=await O.next()).done;se=!1){let ce=he.value;m.push(typeof ce=="string"?Buffer.from(ce):ce)}}catch(he){Y=[he]}finally{try{se&&(he=O.return)&&await he.call(O)}finally{if(Y)throw Y[0]}}let P=Buffer.concat(m).toString("utf-8"),B;try{B=JSON.parse(P)}catch(ce){T(a,400,{error:"Invalid JSON"});return}u=B.threadId,w=B.reply,f=B.color,E=B.provider,$=B.model,q=B.sourceId}if(!u||!w){T(a,400,{error:"Missing threadId or reply"});return}if(!await D.getThread(u)){T(a,404,{error:"Thread not found"});return}let v=He().slice(0,8),W=[];for(let m=0;m<M.length;m++){let P=ie(i,`reply-${v}-${m}.png`);await _e(P,M[m]),W.push(P)}let X="";{let m=await D.getThreadHistory(u);for(let P=m.length-1;P>=0;P--)if(m[P].screenshotPath){X=m[P].screenshotPath;break}}if(!X){T(a,400,{error:"No screenshot available"});return}await D.appendMessage(u,_({role:"human",timestamp:Date.now(),jobId:v,replyToQuestion:w,screenshotPath:X},W.length>0?{replyImagePaths:W}:{}));let K=await D.getThreadHistory(u),te=[];for(let m of K)if(m.annotationIds)for(let P of m.annotationIds)te.includes(P)||te.push(P);let ee=E==="claude"||E==="codex"?E:void 0,le=Pt(X,K,ee,W.length>0?W:void 0),ae={id:v,status:"queued",screenshotPath:X,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:f,threadId:u,annotationIds:te.length>0?te:void 0,provider:ee,model:$||void 0,sourceId:q||void 0};ae._replyPrompt=le,W.length>0&&(ae._replyImagePaths=W);let R=I.enqueue(ae);T(a,200,{jobId:v,position:R,threadId:u})}function pe(s,a){a.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),a.write(`event: connected
129
+ data: {"status":"connected"}
130
+
131
+ `),!o&&s.headers.origin&&Rt(s.headers.origin)&&(o=s.headers.origin);let u=new URL(s.url||"/",`http://127.0.0.1:${k}`).searchParams.get("sourceId")||void 0,w={id:He().slice(0,8),res:a,sourceId:u};L.add(w),s.on("close",()=>{L.delete(w)})}function we(s){let a=I.allActive;T(s,200,{ok:!0,projectId:n,devOrigin:o,activeJob:a[0]?{id:a[0].id,status:a[0].status}:null,activeJobs:a.map(h=>({id:h.id,status:h.status,threadId:h.threadId,annotationIds:h.annotationIds,color:h.color})),queueDepth:I.depth,recentJobs:j})}async function ve(s,a){let u=new URL(s.url||"/",`http://127.0.0.1:${k}`).searchParams.get("jobId"),f=(u?I.allActive.filter($=>$.id===u):I.allActive).map($=>$.threadId).filter(Boolean),E=u?I.cancelJob(u):I.cancelActive();for(let $ of f)await D.appendMessage($,{role:"assistant",timestamp:Date.now(),jobId:u||"",cancelled:!0});T(a,200,{cancelled:E})}async function Se(s){if(G.isRunning){T(s,200,{skipped:!0,reason:"Already running"});return}let a=await G.getUnmaterializedPatternDecisions();if(a.length===0){T(s,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}G.run().catch(()=>{}),T(s,200,{started:!0,decisionCount:a.length,decisionIds:a.map(h=>h.id)})}async function xe(s,a){var v,W;let h=[];try{for(var $=de(s),q,M,V;q=!(M=await $.next()).done;q=!1){let X=M.value;h.push(typeof X=="string"?Buffer.from(X):X)}}catch(M){V=[M]}finally{try{q&&(M=$.return)&&await M.call($)}finally{if(V)throw V[0]}}let u;if(h.length>0)try{u=JSON.parse(Buffer.concat(h).toString("utf-8")).serverUrl}catch(X){}let w=[];(v=y.claude)!=null&&v.available&&y.claude.mcp&&!y.claude.mcp.found&&w.push(await wt(u)),(W=y.codex)!=null&&W.available&&y.codex.mcp&&!y.codex.mcp.found&&w.push(await vt(u));let[f,E]=await Promise.all([Fe(t),Le(t)]);y.claude&&(y.claude.mcp=f),y.codex&&(y.codex.mcp=E),T(a,200,{results:w,capabilities:{providers:y}})}async function Pe(s,a){let h=[];try{for(var f=de(s),E,$,q;E=!($=await f.next()).done;E=!1){let M=$.value;h.push(typeof M=="string"?Buffer.from(M):M)}}catch($){q=[$]}finally{try{E&&($=f.return)&&await $.call(f)}finally{if(q)throw q[0]}}let u;try{u=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(M){T(a,400,{error:"Invalid JSON"});return}if(!u.name||typeof u.name!="string"){T(a,400,{error:"Missing or invalid name"});return}let w=await G.addComponent(u.name);T(a,200,w)}async function Ie(s,a){let h=[];try{for(var f=de(s),E,$,q;E=!($=await f.next()).done;E=!1){let M=$.value;h.push(typeof M=="string"?Buffer.from(M):M)}}catch($){q=[$]}finally{try{E&&($=f.return)&&await $.call(f)}finally{if(q)throw q[0]}}let u;try{u=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(M){T(a,400,{error:"Invalid JSON"});return}if(!u.name||typeof u.name!="string"){T(a,400,{error:"Missing or invalid name"});return}let w=await G.removeComponent(u.name);T(a,200,w)}async function Q(s,a){let h=[];try{for(var f=de(s),E,$,q;E=!($=await f.next()).done;E=!1){let M=$.value;h.push(typeof M=="string"?Buffer.from(M):M)}}catch($){q=[$]}finally{try{E&&($=f.return)&&await $.call(f)}finally{if(q)throw q[0]}}let u;try{u=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(M){T(a,400,{error:"Invalid JSON"});return}if(!u.path||typeof u.path!="string"||typeof u.value!="string"){T(a,400,{error:"Missing or invalid path/value"});return}let w=await G.updateToken(u.path,u.value);T(a,200,w)}async function De(s,a){let h=[];try{for(var f=de(s),E,$,q;E=!($=await f.next()).done;E=!1){let M=$.value;h.push(typeof M=="string"?Buffer.from(M):M)}}catch($){q=[$]}finally{try{E&&($=f.return)&&await $.call(f)}finally{if(q)throw q[0]}}let u;try{u=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(M){T(a,400,{error:"Invalid JSON"});return}if(!u.path||typeof u.path!="string"){T(a,400,{error:"Missing or invalid path"});return}let w=await G.removeToken(u.path);T(a,200,w)}function C(s,a){let h=o!=null?o:"http://localhost:3000";if(!o){let w=s.headers.referer||s.headers.origin;if(w)try{let f=new URL(typeof w=="string"?w:w[0]||"");(f.hostname==="localhost"||f.hostname==="127.0.0.1")&&(h=f.origin)}catch(f){}}let u=ot(k,h);a.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),a.end(u)}async function Te(s){let a=Date.now();if(g&&a<g.expires){T(s,200,g.data);return}try{let{scanForComponents:h}=await import("./react-scanner-5NIJ6ZPL.mjs"),{generateRenderFiles:u}=await import("./render-generator-ZNV3RDU7.mjs"),w=await h(t);g={data:w,expires:a+5e3},u(w,t,N).then(f=>{N=f}).catch(f=>console.warn("[Bridge] Render generation failed:",f)),T(s,200,w)}catch(h){console.error("[Bridge] Scanner error:",h),T(s,500,{error:"Failed to scan components"})}}async function ke(s){let a=[ie(t,"node_modules","@popmelt.com","core","dist","canvas.mjs"),ie(t,"packages","popmelt","dist","canvas.mjs")];try{let h=vn($n.url);a.unshift(ie(wn(h),"canvas.mjs"))}catch(h){}for(let h of a)try{let u=await kt(h,"utf-8");s.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),s.end(u);return}catch(u){}console.error("[Bridge] Canvas bundle not found in:",a),T(s,404,{error:"Canvas bundle not found"})}async function Mt(s,a){var h;if(!s||s.includes("/")||s.includes("\\")||s.includes("..")){T(a,400,{error:"Invalid filename"});return}try{let u=await kt(ie(i,s)),w=(h=s.split(".").pop())==null?void 0:h.toLowerCase(),f=w==="png"?"image/png":w==="jpg"||w==="jpeg"?"image/jpeg":"application/octet-stream";a.writeHead(200,{"Content-Type":f,"Cache-Control":"public, max-age=3600"}),a.end(u)}catch(u){T(a,404,{error:"File not found"})}}function be(s){return`/files/${yn(s)}`}async function Et(s,a){let h=await D.getThread(s);if(!h){T(a,404,{error:"Thread not found"});return}let u=h.messages.map(M=>{var V=M,{screenshotPath:w,screenshotPaths:f,imagePaths:E,replyImagePaths:$}=V,q=st(V,["screenshotPath","screenshotPaths","imagePaths","replyImagePaths"]);return _(_(_(_(_({},q),w?{screenshotUrl:be(w)}:{}),f?{screenshotUrls:Object.fromEntries(Object.entries(f).map(([v,W])=>[v,be(W)]))}:{}),E?{imageUrls:Object.fromEntries(Object.entries(E).map(([v,W])=>[v,W.map(be)]))}:{}),$?{replyImageUrls:$.map(be)}:{})});T(a,200,{id:h.id,createdAt:h.createdAt,messages:u})}let Ge=9,qe=!1;for(let s=e;s<e+Ge;s++)try{await Rn(b,s),k=s,qe=!0,console.log(`[\u22B9 is watching :${k}]`);break}catch(a){if(a.code==="EADDRINUSE"){let h=await bn(s);if(h&&h.projectId===n)return console.log(`[\u22B9 already watching :${s}]`),{port:s,projectId:n,close:async()=>{}};continue}throw a}if(!qe)throw new Error(`[Bridge] All ports ${e}\u2013${e+Ge-1} in use`);for(let[s,a]of Object.entries(y))!a.available||!a.path||J(s,a.path).then(h=>{if(h)console.log(`[Bridge] ${s} warmed up`);else{console.warn(`[Bridge] ${s} warm-up failed \u2014 marking unavailable`),a.available=!1,a.path=null;for(let u of L)We(u,{type:"capabilities_changed",data:{}})}});let Ct=setInterval(()=>{bt(i).catch(()=>{})},Pn);return{port:k,projectId:n,close:async()=>{clearInterval(Ct),await I.destroyAsync();for(let s of L)try{s.res.end()}catch(a){}return L.clear(),new Promise(s=>{b.close(()=>s())})}}}async function bt(r){try{let e=await pn(r),t=Date.now();for(let n of e){let o=ie(r,n);try{let i=await hn(o);t-i.mtimeMs>In&&await fn(o)}catch(i){}}}catch(e){}}async function Cs(r){if(process.env.NODE_ENV==="production"&&!(r!=null&&r.force))throw new Error("[Bridge] Refusing to start in production. Pass { force: true } to override.");let e=await $t(r);return process.env.POPMELT_BRIDGE_PORT=String(e.port),e}export{Cs as startPopmelt};