@popmelt.com/core 0.5.2 → 0.5.4

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/dist/cli.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- var Dt=Object.defineProperty,At=Object.defineProperties;var Jt=Object.getOwnPropertyDescriptors;var $e=Object.getOwnPropertySymbols;var et=Object.prototype.hasOwnProperty,tt=Object.prototype.propertyIsEnumerable;var Xe=(o,e)=>(e=Symbol[o])?e:Symbol.for("Symbol."+o);var Ze=(o,e,n)=>e in o?Dt(o,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):o[e]=n,V=(o,e)=>{for(var n in e||(e={}))et.call(e,n)&&Ze(o,n,e[n]);if($e)for(var n of $e(e))tt.call(e,n)&&Ze(o,n,e[n]);return o},oe=(o,e)=>At(o,Jt(e));var nt=(o,e)=>{var n={};for(var t in o)et.call(o,t)&&e.indexOf(t)<0&&(n[t]=o[t]);if(o!=null&&$e)for(var t of $e(o))e.indexOf(t)<0&&tt.call(o,t)&&(n[t]=o[t]);return n};var le=(o,e,n)=>(e=o[Xe("asyncIterator")])?e.call(o):(o=o[Xe("iterator")](),e={},n=(t,s)=>(s=o[t])&&(e[t]=a=>new Promise((i,c,p)=>(a=s.call(o,a),p=a.done,Promise.resolve(a.value).then(u=>i({value:u,done:p}),c)))),n("next"),n("return"),e);import{spawn as xn}from"child_process";import{execFileSync as an}from"child_process";import{randomUUID as he}from"crypto";import{mkdir as cn,readdir as ln,stat as dn,unlink as un,writeFile as ye}from"fs/promises";import{createServer as pn}from"http";import{tmpdir as hn}from"os";import{join as de}from"path";import{spawn as Nt}from"child_process";import{createInterface as _t}from"readline";function Re(o,e){let{prompt:n,projectRoot:t,maxTurns:s=10,maxBudgetUsd:a=1,allowedTools:i=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:p,model:u,onEvent:v}=e,W=[];p?W.push("--resume",p,"-p",n):W.push("-p",n),W.push("--output-format","stream-json","--verbose","--max-turns",String(s),"--max-budget-usd",String(a)),u&&W.push("--model",u);for(let S of i)W.push("--allowedTools",S);let y=Nt(c,W,{cwd:t,stdio:["ignore","pipe","pipe"],env:oe(V({},process.env),{ANTHROPIC_API_KEY:void 0})}),T=new Promise(S=>{var A;let F,b=[],j=[],K=!1,ee="",q=_t({input:y.stdout}),B=new Set;q.on("line",z=>{var P,Y,L,Z,Ie,ke,be,Pe,Te;if(z.trim())try{let Q=JSON.parse(z);Q.session_id&&!F&&(F=Q.session_id);let Ae=(Y=Q.type)!=null?Y:(P=Q.event)!=null&&P.type?`event.${Q.event.type}`:"unknown";if(B.add(Ae),Q.type==="result"&&Q.result&&b.length===0){let D=typeof Q.result=="string"?Q.result:"";D&&(b.push(D),v==null||v({type:"delta",jobId:o,text:D},o))}if(Q.type==="assistant"&&Array.isArray((L=Q.message)==null?void 0:L.content))for(let D of Q.message.content){if(D.type==="text"&&D.text&&(b.push(D.text),v==null||v({type:"delta",jobId:o,text:D.text},o)),D.type==="tool_use"&&D.name){let we=((Z=D.input)==null?void 0:Z.file_path)||((Ie=D.input)==null?void 0:Ie.path)||void 0;v==null||v(V({type:"tool_use",jobId:o,tool:D.name},we?{file:we}:{}),o),D.name==="Edit"&&((ke=D.input)!=null&&ke.file_path)?j.push({tool:"Edit",file_path:D.input.file_path,old_string:D.input.old_string,new_string:D.input.new_string,replace_all:D.input.replace_all}):D.name==="Write"&&((be=D.input)!=null&&be.file_path)&&j.push({tool:"Write",file_path:D.input.file_path,content:D.input.content})}D.type==="thinking"&&D.thinking&&(v==null||v({type:"thinking",jobId:o,text:D.thinking},o))}Q.type==="user"&&((Te=(Pe=Q.tool_use_result)==null?void 0:Pe.file)!=null&&Te.filePath)&&(v==null||v({type:"tool_use",jobId:o,tool:"Read",file:Q.tool_use_result.file.filePath},o))}catch(Q){}});let te=[];(A=y.stderr)==null||A.on("data",z=>{te.push(z.toString())}),y.on("close",z=>{q.close(),z!==0&&z!==null&&(K=!0,ee=te.join("")||`Claude process exited with code ${z}`),S({sessionId:F,text:b.join(""),success:!K,error:K?ee:void 0,fileEdits:j.length>0?j:void 0})}),y.on("error",z=>{K=!0,ee=z.message,S({sessionId:F,text:b.join(""),success:!1,error:ee,fileEdits:j.length>0?j:void 0})})});return{process:y,result:T}}import{spawn as Bt}from"child_process";import{createInterface as Ut}from"readline";function st(o,e){let{prompt:n,projectRoot:t,screenshotPath:s,resumeSessionId:a,model:i,onEvent:c}=e,p=[];a?(p.push("exec","resume",a),i&&p.push("-m",i),p.push("--json","--full-auto",n),s&&p.push("--image",s)):(p.push("exec","--json","--full-auto"),i&&p.push("-m",i),p.push(n),s&&p.push("--image",s));let u=Bt("codex",p,{cwd:t,stdio:["ignore","pipe","pipe"],env:V({},process.env)}),v=new Promise(W=>{var ee;let y,T=[],S=!1,F="",b=Ut({input:u.stdout}),j=new Set;b.on("line",q=>{var B,te,A,z;if(q.trim())try{let P=JSON.parse(q),Y=(B=P.type)!=null?B:"unknown";if(j.add(Y),Y==="thread.started"&&P.thread_id&&!y&&(y=P.thread_id),Y==="item/agentMessage/delta"&&((te=P.delta)!=null&&te.text)&&(T.push(P.delta.text),c==null||c({type:"delta",jobId:o,text:P.delta.text},o)),Y==="item/reasoning/delta"&&((A=P.delta)!=null&&A.text)&&(c==null||c({type:"thinking",jobId:o,text:P.delta.text},o)),Y==="item/started"&&P.item){let L=P.item.type;if(L==="command_execution")c==null||c({type:"tool_use",jobId:o,tool:"Bash"},o);else if(L==="file_change"){let Z=P.item.filename||P.item.path;c==null||c(V({type:"tool_use",jobId:o,tool:"Edit"},Z?{file:Z}:{}),o)}else if(L==="file_read"){let Z=P.item.filename||P.item.path;c==null||c(V({type:"tool_use",jobId:o,tool:"Read"},Z?{file:Z}:{}),o)}else if(L==="web_search")c==null||c({type:"tool_use",jobId:o,tool:"WebSearch"},o);else if(L==="mcp_tool_call"){let Z=P.item.tool_name||P.item.name||"MCP";c==null||c({type:"tool_use",jobId:o,tool:Z},o)}}if(Y==="item/completed"&&P.item){if(P.item.type==="agent_message"){let L=P.item.text;typeof L=="string"&&L&&T.push(L)}else if(P.item.type==="reasoning"){let L=P.item.text;typeof L=="string"&&L&&(c==null||c({type:"thinking",jobId:o,text:L},o))}}Y==="turn.failed"&&(S=!0,F=((z=P.error)==null?void 0:z.message)||P.message||"Turn failed")}catch(P){}});let K=[];(ee=u.stderr)==null||ee.on("data",q=>{K.push(q.toString())}),u.on("close",q=>{b.close(),q!==0&&q!==null&&(S=!0,F=K.join("")||`Codex process exited with code ${q}`),W({sessionId:y,text:T.join(""),success:!S,error:S?F:void 0})}),u.on("error",q=>{S=!0,F=q.message,W({sessionId:y,text:T.join(""),success:!1,error:F})})});return{process:u,result:v}}import{execFile as Ft}from"child_process";import{copyFile as ot,mkdir as rt,readdir as jt,readFile as zt,writeFile as Lt}from"fs/promises";import{join as pe}from"path";var Me=class{constructor(e){this.projectRoot=e;let n=pe(e,".popmelt");this.decisionsDir=pe(n,"decisions"),this.screenshotsDir=pe(n,"screenshots")}async persist(e,n,t){try{await rt(this.decisionsDir,{recursive:!0}),await rt(this.screenshotsDir,{recursive:!0});try{await ot(n,pe(this.screenshotsDir,`s-${e.id}.png`))}catch(s){}for(let s=0;s<t.length;s++)try{let a=e.pastedImagePaths[s];a&&await ot(t[s],pe(this.screenshotsDir,a.replace("screenshots/","")))}catch(a){}await Lt(pe(this.decisionsDir,`d-${e.id}.json`),JSON.stringify(e,null,2))}catch(s){console.error("[DecisionStore] Failed to persist decision record:",s)}}async listDecisionIds(){try{return(await jt(this.decisionsDir)).filter(n=>n.startsWith("d-")&&n.endsWith(".json")).map(n=>n.slice(2,-5))}catch(e){return[]}}async loadDecision(e){try{let n=await zt(pe(this.decisionsDir,`d-${e}.json`),"utf-8");return JSON.parse(n)}catch(n){return null}}async loadDecisions(e){return(await Promise.all(e.map(t=>this.loadDecision(t)))).filter(t=>t!==null)}captureGitDiff(e){return new Promise(n=>{Ft("git",["diff","HEAD"],{cwd:e,timeout:5e3,maxBuffer:1024*1024},(t,s)=>{if(t){n(null);return}n(s||null)})})}};import{readFile as it,writeFile as fe}from"fs/promises";import{join as Ne}from"path";var re="[Materializer]",Gt={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null},Oe=class{constructor(e,n,t={}){this.projectRoot=e;this.decisionStore=n;this.options=t;this.cachedIndex=null;this.running=!1;let s=Ne(e,".popmelt");this.indexPath=Ne(s,"materialized.json"),this.modelPath=Ne(s,"model.json")}get isRunning(){return this.running}async loadModel(){try{let e=await it(this.modelPath,"utf-8");return JSON.parse(e)}catch(e){return null}}async addComponent(e){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let t=n.components;return t[e]?{added:!1,alreadyExists:!0}:(t[e]={description:""},await fe(this.modelPath,JSON.stringify(n,null,2)),console.log(`${re} Added component "${e}" to model`),{added:!0,alreadyExists:!1})}async updateToken(e,n){let t=await this.loadModel();t||(t={tokens:{},components:{},rules:[]});let s=e.split("."),a=t;for(let p=0;p<s.length-1;p++){let u=s[p];(!a[u]||typeof a[u]!="object")&&(a[u]={}),a=a[u]}let i=s[s.length-1],c;try{c=JSON.parse(n)}catch(p){c=null}if(c&&typeof c=="object"&&c!==null&&"value"in c)a[i]=c;else{let p=a[i];p&&typeof p=="object"&&p!==null&&"value"in p?p.value=n:a[i]=n}return await fe(this.modelPath,JSON.stringify(t,null,2)),console.log(`${re} Updated token "${e}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(e){let n=await this.loadModel();if(!n)return{removed:!1};let t=e.split("."),s=n;for(let i=0;i<t.length-1;i++){let c=t[i];if(!s[c]||typeof s[c]!="object")return{removed:!1};s=s[c]}let a=t[t.length-1];return a in s?(delete s[a],await fe(this.modelPath,JSON.stringify(n,null,2)),console.log(`${re} Removed token "${e}" from model`),{removed:!0}):{removed:!1}}async removeComponent(e){let n=await this.loadModel();if(!n)return{removed:!1};let t=n.components;return!t||!t[e]?{removed:!1}:(delete t[e],await fe(this.modelPath,JSON.stringify(n,null,2)),console.log(`${re} Removed component "${e}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let e=await this.loadIndex(),n=new Set(e.materializedIds),s=(await this.decisionStore.listDecisionIds()).filter(i=>!n.has(i));return s.length===0?[]:(await this.decisionStore.loadDecisions(s)).filter(i=>i.resolutions.some(c=>{var u;let p=(u=c.finalScope)!=null?u:c.inferredScope;return(p==null?void 0:p.breadth)==="pattern"}))}async run(){var e,n,t,s,a,i,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 u=p.map(b=>b.id);console.log(`${re} Processing ${u.length} pattern-scoped decision(s): ${u.join(", ")}`),(n=(e=this.options).onEvent)==null||n.call(e,{type:"materialize_started",decisionIds:u});let v=await this.loadModel(),W=Wt(p,v),y=!0,T;try{let{result:b}=Re(`mat-${Date.now()}`,{prompt:W,projectRoot:this.projectRoot,maxTurns:(t=this.options.maxTurns)!=null?t:5,maxBudgetUsd:(s=this.options.maxBudgetUsd)!=null?s:.5,allowedTools:["Read"],claudePath:(a=this.options.claudePath)!=null?a:"claude"}),j=await b;if(!j.success)y=!1,T=j.error,console.error(`${re} Claude spawn error:`,T);else{let K=Ht(j.text);K?(await fe(this.modelPath,JSON.stringify(K,null,2)),console.log(`${re} Successfully materialized ${u.length} decision(s) \u2192 ${this.modelPath}`)):(y=!1,T="No <model> block found in response",console.error(`${re} ${T}`))}}catch(b){y=!1,T=b instanceof Error?b.message:String(b),console.error(`${re} Error:`,T)}let S=await this.loadIndex(),F=new Set(S.materializedIds);for(let b of u)F.add(b);return S.materializedIds=[...F],S.lastRunAt=Date.now(),S.lastRunDecisionIds=u,S.lastRunError=T!=null?T:null,await this.persistIndex(S),(c=(i=this.options).onEvent)==null||c.call(i,{type:"materialize_done",decisionIds:u,success:y,error:T}),{processedIds:u,success:y,error:T}}finally{this.running=!1}}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let e=await it(this.indexPath,"utf-8"),n=JSON.parse(e);return this.cachedIndex=n,n}catch(e){return this.cachedIndex=oe(V({},Gt),{materializedIds:[],lastRunDecisionIds:[]}),this.cachedIndex}}async persistIndex(e){this.cachedIndex=e;try{await fe(this.indexPath,JSON.stringify(e,null,2))}catch(n){console.error(`${re} Failed to write index:`,n)}}};function Ht(o){let e=o.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!(e!=null&&e[1]))return null;try{let n=JSON.parse(e[1]);return typeof n!="object"||n===null||Array.isArray(n)?null:n}catch(n){return null}}function Wt(o,e){let n=o.map(s=>{let i=s.resolutions.filter(u=>{var W;let v=(W=u.finalScope)!=null?W:u.inferredScope;return(v==null?void 0:v.breadth)==="pattern"}).map(u=>{var T,S,F,b;let v=(T=u.finalScope)!=null?T:u.inferredScope,W=(S=v==null?void 0:v.target)!=null?S:"unknown",y=(b=(F=u.filesModified)==null?void 0:F.join(", "))!=null?b:"none";return`- **${u.summary}** [scope: pattern/${W}]
3
- Files modified: ${y}`}).join(`
2
+ var Dt=Object.defineProperty,Jt=Object.defineProperties;var Nt=Object.getOwnPropertyDescriptors;var Re=Object.getOwnPropertySymbols;var tt=Object.prototype.hasOwnProperty,nt=Object.prototype.propertyIsEnumerable;var Ze=(o,e)=>(e=Symbol[o])?e:Symbol.for("Symbol."+o);var et=(o,e,n)=>e in o?Dt(o,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):o[e]=n,K=(o,e)=>{for(var n in e||(e={}))tt.call(e,n)&&et(o,n,e[n]);if(Re)for(var n of Re(e))nt.call(e,n)&&et(o,n,e[n]);return o},oe=(o,e)=>Jt(o,Nt(e));var st=(o,e)=>{var n={};for(var t in o)tt.call(o,t)&&e.indexOf(t)<0&&(n[t]=o[t]);if(o!=null&&Re)for(var t of Re(o))e.indexOf(t)<0&&nt.call(o,t)&&(n[t]=o[t]);return n};var le=(o,e,n)=>(e=o[Ze("asyncIterator")])?e.call(o):(o=o[Ze("iterator")](),e={},n=(t,s)=>(s=o[t])&&(e[t]=a=>new Promise((i,c,p)=>(a=s.call(o,a),p=a.done,Promise.resolve(a.value).then(u=>i({value:u,done:p}),c)))),n("next"),n("return"),e);import{spawn as In}from"child_process";import{execFileSync as cn}from"child_process";import{randomUUID as fe}from"crypto";import{mkdir as ln,readdir as dn,stat as un,unlink as pn,writeFile as we}from"fs/promises";import{createServer as hn}from"http";import{tmpdir as fn}from"os";import{join as de}from"path";import{spawn as _t}from"child_process";import{createInterface as Bt}from"readline";function Me(o,e){let{prompt:n,projectRoot:t,maxTurns:s=10,maxBudgetUsd:a=1,allowedTools:i=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:p,model:u,onEvent:x}=e,G=[];p?G.push("--resume",p,"-p",n):G.push("-p",n),G.push("--output-format","stream-json","--verbose","--max-turns",String(s),"--max-budget-usd",String(a)),u&&G.push("--model",u);for(let I of i)G.push("--allowedTools",I);let w=_t(c,G,{cwd:t,stdio:["ignore","pipe","pipe"],env:oe(K({},process.env),{ANTHROPIC_API_KEY:void 0})}),M=new Promise(I=>{var J;let F,P=[],j=[],Q=!1,ee="",W=Bt({input:w.stdout}),B=new Set;W.on("line",z=>{var $,q,L,X,ke,be,Pe,Te,$e;if(z.trim())try{let Y=JSON.parse(z);Y.session_id&&!F&&(F=Y.session_id);let Je=(q=Y.type)!=null?q:($=Y.event)!=null&&$.type?`event.${Y.event.type}`:"unknown";if(B.add(Je),Y.type==="result"&&Y.result&&P.length===0){let D=typeof Y.result=="string"?Y.result:"";D&&(P.push(D),x==null||x({type:"delta",jobId:o,text:D},o))}if(Y.type==="assistant"&&Array.isArray((L=Y.message)==null?void 0:L.content))for(let D of Y.message.content){if(D.type==="text"&&D.text&&(P.push(D.text),x==null||x({type:"delta",jobId:o,text:D.text},o)),D.type==="tool_use"&&D.name){let ve=((X=D.input)==null?void 0:X.file_path)||((ke=D.input)==null?void 0:ke.path)||void 0;x==null||x(K({type:"tool_use",jobId:o,tool:D.name},ve?{file:ve}:{}),o),D.name==="Edit"&&((be=D.input)!=null&&be.file_path)?j.push({tool:"Edit",file_path:D.input.file_path,old_string:D.input.old_string,new_string:D.input.new_string,replace_all:D.input.replace_all}):D.name==="Write"&&((Pe=D.input)!=null&&Pe.file_path)&&j.push({tool:"Write",file_path:D.input.file_path,content:D.input.content})}D.type==="thinking"&&D.thinking&&(x==null||x({type:"thinking",jobId:o,text:D.thinking},o))}Y.type==="user"&&(($e=(Te=Y.tool_use_result)==null?void 0:Te.file)!=null&&$e.filePath)&&(x==null||x({type:"tool_use",jobId:o,tool:"Read",file:Y.tool_use_result.file.filePath},o))}catch(Y){}});let te=[];(J=w.stderr)==null||J.on("data",z=>{te.push(z.toString())}),w.on("close",z=>{W.close(),z!==0&&z!==null&&(Q=!0,ee=te.join("")||`Claude process exited with code ${z}`),I({sessionId:F,text:P.join(""),success:!Q,error:Q?ee:void 0,fileEdits:j.length>0?j:void 0})}),w.on("error",z=>{Q=!0,ee=z.message,I({sessionId:F,text:P.join(""),success:!1,error:ee,fileEdits:j.length>0?j:void 0})})});return{process:w,result:M}}import{spawn as Ut}from"child_process";import{createInterface as Ft}from"readline";function ot(o,e){let{prompt:n,projectRoot:t,screenshotPath:s,resumeSessionId:a,model:i,onEvent:c}=e,p=[];a?(p.push("exec","resume",a),i&&p.push("-m",i),p.push("--json","--full-auto",n),s&&p.push("--image",s)):(p.push("exec","--json","--full-auto"),i&&p.push("-m",i),p.push(n),s&&p.push("--image",s));let u=Ut("codex",p,{cwd:t,stdio:["ignore","pipe","pipe"],env:K({},process.env)}),x=new Promise(G=>{var ee;let w,M=[],I=!1,F="",P=Ft({input:u.stdout}),j=new Set;P.on("line",W=>{var B,te,J,z;if(W.trim())try{let $=JSON.parse(W),q=(B=$.type)!=null?B:"unknown";if(j.add(q),q==="thread.started"&&$.thread_id&&!w&&(w=$.thread_id),q==="item/agentMessage/delta"&&((te=$.delta)!=null&&te.text)&&(M.push($.delta.text),c==null||c({type:"delta",jobId:o,text:$.delta.text},o)),q==="item/reasoning/delta"&&((J=$.delta)!=null&&J.text)&&(c==null||c({type:"thinking",jobId:o,text:$.delta.text},o)),q==="item/started"&&$.item){let L=$.item.type;if(L==="command_execution")c==null||c({type:"tool_use",jobId:o,tool:"Bash"},o);else if(L==="file_change"){let X=$.item.filename||$.item.path;c==null||c(K({type:"tool_use",jobId:o,tool:"Edit"},X?{file:X}:{}),o)}else if(L==="file_read"){let X=$.item.filename||$.item.path;c==null||c(K({type:"tool_use",jobId:o,tool:"Read"},X?{file:X}:{}),o)}else if(L==="web_search")c==null||c({type:"tool_use",jobId:o,tool:"WebSearch"},o);else if(L==="mcp_tool_call"){let X=$.item.tool_name||$.item.name||"MCP";c==null||c({type:"tool_use",jobId:o,tool:X},o)}}if(q==="item/completed"&&$.item){if($.item.type==="agent_message"){let L=$.item.text;typeof L=="string"&&L&&M.push(L)}else if($.item.type==="reasoning"){let L=$.item.text;typeof L=="string"&&L&&(c==null||c({type:"thinking",jobId:o,text:L},o))}}q==="turn.failed"&&(I=!0,F=((z=$.error)==null?void 0:z.message)||$.message||"Turn failed")}catch($){}});let Q=[];(ee=u.stderr)==null||ee.on("data",W=>{Q.push(W.toString())}),u.on("close",W=>{P.close(),W!==0&&W!==null&&(I=!0,F=Q.join("")||`Codex process exited with code ${W}`),G({sessionId:w,text:M.join(""),success:!I,error:I?F:void 0})}),u.on("error",W=>{I=!0,F=W.message,G({sessionId:w,text:M.join(""),success:!1,error:F})})});return{process:u,result:x}}import{execFile as jt}from"child_process";import{copyFile as rt,mkdir as it,readdir as zt,readFile as Lt,writeFile as Gt}from"fs/promises";import{join as he}from"path";var Oe=class{constructor(e){this.projectRoot=e;let n=he(e,".popmelt");this.decisionsDir=he(n,"decisions"),this.screenshotsDir=he(n,"screenshots")}async persist(e,n,t){try{await it(this.decisionsDir,{recursive:!0}),await it(this.screenshotsDir,{recursive:!0});try{await rt(n,he(this.screenshotsDir,`s-${e.id}.png`))}catch(s){}for(let s=0;s<t.length;s++)try{let a=e.pastedImagePaths[s];a&&await rt(t[s],he(this.screenshotsDir,a.replace("screenshots/","")))}catch(a){}await Gt(he(this.decisionsDir,`d-${e.id}.json`),JSON.stringify(e,null,2))}catch(s){console.error("[DecisionStore] Failed to persist decision record:",s)}}async listDecisionIds(){try{return(await zt(this.decisionsDir)).filter(n=>n.startsWith("d-")&&n.endsWith(".json")).map(n=>n.slice(2,-5))}catch(e){return[]}}async loadDecision(e){try{let n=await Lt(he(this.decisionsDir,`d-${e}.json`),"utf-8");return JSON.parse(n)}catch(n){return null}}async loadDecisions(e){return(await Promise.all(e.map(t=>this.loadDecision(t)))).filter(t=>t!==null)}captureGitDiff(e){return new Promise(n=>{jt("git",["diff","HEAD"],{cwd:e,timeout:5e3,maxBuffer:1024*1024},(t,s)=>{if(t){n(null);return}n(s||null)})})}};import{readFile as at,writeFile as me}from"fs/promises";import{join as _e}from"path";var re="[Materializer]",Ht={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null},Ee=class{constructor(e,n,t={}){this.projectRoot=e;this.decisionStore=n;this.options=t;this.cachedIndex=null;this.running=!1;let s=_e(e,".popmelt");this.indexPath=_e(s,"materialized.json"),this.modelPath=_e(s,"model.json")}get isRunning(){return this.running}async loadModel(){try{let e=await at(this.modelPath,"utf-8");return JSON.parse(e)}catch(e){return null}}async addComponent(e){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let t=n.components;return t[e]?{added:!1,alreadyExists:!0}:(t[e]={description:""},await me(this.modelPath,JSON.stringify(n,null,2)),console.log(`${re} Added component "${e}" to model`),{added:!0,alreadyExists:!1})}async updateToken(e,n){let t=await this.loadModel();t||(t={tokens:{},components:{},rules:[]});let s=e.split("."),a=t;for(let p=0;p<s.length-1;p++){let u=s[p];(!a[u]||typeof a[u]!="object")&&(a[u]={}),a=a[u]}let i=s[s.length-1],c;try{c=JSON.parse(n)}catch(p){c=null}if(c&&typeof c=="object"&&c!==null&&"value"in c)a[i]=c;else{let p=a[i];p&&typeof p=="object"&&p!==null&&"value"in p?p.value=n:a[i]=n}return await me(this.modelPath,JSON.stringify(t,null,2)),console.log(`${re} Updated token "${e}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(e){let n=await this.loadModel();if(!n)return{removed:!1};let t=e.split("."),s=n;for(let i=0;i<t.length-1;i++){let c=t[i];if(!s[c]||typeof s[c]!="object")return{removed:!1};s=s[c]}let a=t[t.length-1];return a in s?(delete s[a],await me(this.modelPath,JSON.stringify(n,null,2)),console.log(`${re} Removed token "${e}" from model`),{removed:!0}):{removed:!1}}async removeComponent(e){let n=await this.loadModel();if(!n)return{removed:!1};let t=n.components;return!t||!t[e]?{removed:!1}:(delete t[e],await me(this.modelPath,JSON.stringify(n,null,2)),console.log(`${re} Removed component "${e}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let e=await this.loadIndex(),n=new Set(e.materializedIds),s=(await this.decisionStore.listDecisionIds()).filter(i=>!n.has(i));return s.length===0?[]:(await this.decisionStore.loadDecisions(s)).filter(i=>i.resolutions.some(c=>{var u;let p=(u=c.finalScope)!=null?u:c.inferredScope;return(p==null?void 0:p.breadth)==="pattern"}))}async run(){var e,n,t,s,a,i,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 u=p.map(P=>P.id);console.log(`${re} Processing ${u.length} pattern-scoped decision(s): ${u.join(", ")}`),(n=(e=this.options).onEvent)==null||n.call(e,{type:"materialize_started",decisionIds:u});let x=await this.loadModel(),G=qt(p,x),w=!0,M;try{let{result:P}=Me(`mat-${Date.now()}`,{prompt:G,projectRoot:this.projectRoot,maxTurns:(t=this.options.maxTurns)!=null?t:5,maxBudgetUsd:(s=this.options.maxBudgetUsd)!=null?s:.5,allowedTools:["Read"],claudePath:(a=this.options.claudePath)!=null?a:"claude"}),j=await P;if(!j.success)w=!1,M=j.error,console.error(`${re} Claude spawn error:`,M);else{let Q=Wt(j.text);Q?(await me(this.modelPath,JSON.stringify(Q,null,2)),console.log(`${re} Successfully materialized ${u.length} decision(s) \u2192 ${this.modelPath}`)):(w=!1,M="No <model> block found in response",console.error(`${re} ${M}`))}}catch(P){w=!1,M=P instanceof Error?P.message:String(P),console.error(`${re} Error:`,M)}let I=await this.loadIndex(),F=new Set(I.materializedIds);for(let P of u)F.add(P);return I.materializedIds=[...F],I.lastRunAt=Date.now(),I.lastRunDecisionIds=u,I.lastRunError=M!=null?M:null,await this.persistIndex(I),(c=(i=this.options).onEvent)==null||c.call(i,{type:"materialize_done",decisionIds:u,success:w,error:M}),{processedIds:u,success:w,error:M}}finally{this.running=!1}}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let e=await at(this.indexPath,"utf-8"),n=JSON.parse(e);return this.cachedIndex=n,n}catch(e){return this.cachedIndex=oe(K({},Ht),{materializedIds:[],lastRunDecisionIds:[]}),this.cachedIndex}}async persistIndex(e){this.cachedIndex=e;try{await me(this.indexPath,JSON.stringify(e,null,2))}catch(n){console.error(`${re} Failed to write index:`,n)}}};function Wt(o){let e=o.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!(e!=null&&e[1]))return null;try{let n=JSON.parse(e[1]);return typeof n!="object"||n===null||Array.isArray(n)?null:n}catch(n){return null}}function qt(o,e){let n=o.map(s=>{let i=s.resolutions.filter(u=>{var G;let x=(G=u.finalScope)!=null?G:u.inferredScope;return(x==null?void 0:x.breadth)==="pattern"}).map(u=>{var M,I,F,P;let x=(M=u.finalScope)!=null?M:u.inferredScope,G=(I=x==null?void 0:x.target)!=null?I:"unknown",w=(P=(F=u.filesModified)==null?void 0:F.join(", "))!=null?P:"none";return`- **${u.summary}** [scope: pattern/${G}]
3
+ Files modified: ${w}`}).join(`
4
4
  `),c=s.annotations.map(u=>u.instruction).filter(Boolean).join(`
5
5
  `),p=s.gitDiff?`
6
6
  \`\`\`diff
@@ -67,37 +67,40 @@ Example:
67
67
  { "value": "8px", "property": "gap", "bindings": ["gap-2"] }
68
68
  - property: "gap", "padding", or "margin" \u2014 which CSS property this token controls
69
69
  - bindings: Tailwind class names (without responsive prefixes) that use this token
70
- Include property and bindings when the decision context has class-level evidence.`}import{readFile as lt}from"fs/promises";import{homedir as dt}from"os";import{join as me}from"path";var Be=/popmelt/i;function Ue(){return{found:!1,name:null,scope:null,disabled:!1}}function Se(o,e,n=!1){return{found:!0,name:o,scope:e,disabled:n}}function Ee(o){for(let e of Object.keys(o))if(Be.test(e))return e;return null}async function _e(o){try{let e=await lt(o,"utf-8");return JSON.parse(e)}catch(e){return null}}async function Fe(o){let e=dt(),n=await _e(me(e,".claude.json"));if(n&&typeof n=="object"){let s=n;if(s.mcpServers&&typeof s.mcpServers=="object"){let a=Ee(s.mcpServers);if(a)return Se(a,"user")}if(s.projects&&typeof s.projects=="object"){let i=s.projects[o];if(i&&typeof i=="object"){let c=i;if(c.mcpServers&&typeof c.mcpServers=="object"){let p=Ee(c.mcpServers);if(p){let u=Array.isArray(c.disabledMcpjsonServers)&&c.disabledMcpjsonServers.some(v=>Be.test(v));return Se(p,"project",u)}}}}}let t=await _e(me(o,".mcp.json"));if(t&&typeof t=="object"){let s=t;if(s.mcpServers&&typeof s.mcpServers=="object"){let i=Ee(s.mcpServers);if(i){let c=await at(o,i);return Se(i,"mcp.json",c)}}let a=Ee(s);if(a&&a!=="mcpServers"){let i=await at(o,a);return Se(a,"mcp.json",i)}}return Ue()}async function at(o,e){let n=me(o,".claude","settings.local.json"),t=await _e(n);if(t&&typeof t=="object"){let s=t;if(Array.isArray(s.disabledMcpjsonServers))return s.disabledMcpjsonServers.some(a=>a===e)}return!1}var qt=/^\[mcp_servers\.([^\]]+)\]/;function Yt(o){let e=o.split(`
71
- `),n=[],t=null;for(let s of e){let a=s.match(qt);a?(t&&n.push({name:t.name,body:t.bodyLines.join(`
70
+ Include property and bindings when the decision context has class-level evidence.`}import{readFile as dt}from"fs/promises";import{homedir as ut}from"os";import{join as ge}from"path";var Ue=/popmelt/i;function Fe(){return{found:!1,name:null,scope:null,disabled:!1}}function xe(o,e,n=!1){return{found:!0,name:o,scope:e,disabled:n}}function Ce(o){for(let e of Object.keys(o))if(Ue.test(e))return e;return null}async function Be(o){try{let e=await dt(o,"utf-8");return JSON.parse(e)}catch(e){return null}}async function je(o){let e=ut(),n=await Be(ge(e,".claude.json"));if(n&&typeof n=="object"){let s=n;if(s.mcpServers&&typeof s.mcpServers=="object"){let a=Ce(s.mcpServers);if(a)return xe(a,"user")}if(s.projects&&typeof s.projects=="object"){let i=s.projects[o];if(i&&typeof i=="object"){let c=i;if(c.mcpServers&&typeof c.mcpServers=="object"){let p=Ce(c.mcpServers);if(p){let u=Array.isArray(c.disabledMcpjsonServers)&&c.disabledMcpjsonServers.some(x=>Ue.test(x));return xe(p,"project",u)}}}}}let t=await Be(ge(o,".mcp.json"));if(t&&typeof t=="object"){let s=t;if(s.mcpServers&&typeof s.mcpServers=="object"){let i=Ce(s.mcpServers);if(i){let c=await ct(o,i);return xe(i,"mcp.json",c)}}let a=Ce(s);if(a&&a!=="mcpServers"){let i=await ct(o,a);return xe(a,"mcp.json",i)}}return Fe()}async function ct(o,e){let n=ge(o,".claude","settings.local.json"),t=await Be(n);if(t&&typeof t=="object"){let s=t;if(Array.isArray(s.disabledMcpjsonServers))return s.disabledMcpjsonServers.some(a=>a===e)}return!1}var Yt=/^\[mcp_servers\.([^\]]+)\]/;function Qt(o){let e=o.split(`
71
+ `),n=[],t=null;for(let s of e){let a=s.match(Yt);a?(t&&n.push({name:t.name,body:t.bodyLines.join(`
72
72
  `)}),t={name:a[1],bodyLines:[]}):s.startsWith("[")?t&&(n.push({name:t.name,body:t.bodyLines.join(`
73
73
  `)}),t=null):t&&t.bodyLines.push(s)}return t&&n.push({name:t.name,body:t.bodyLines.join(`
74
- `)}),n}function Qt(o){return/enabled\s*=\s*false/i.test(o)}async function je(o){let e=process.env.CODEX_HOME||me(dt(),".codex"),n=await ct(me(e,"config.toml"),"user");if(n.found)return n;let t=await ct(me(o,".codex","config.toml"),"project");return t.found?t:Ue()}async function ct(o,e){try{let n=await lt(o,"utf-8"),t=Yt(n);for(let s of t)if(Be.test(s.name)){let a=Qt(s.body);return Se(s.name,e,a)}}catch(n){}return Ue()}import{mkdir as Kt,readFile as ut,writeFile as pt}from"fs/promises";import{homedir as ht}from"os";import{dirname as Vt,join as ze}from"path";var ft="https://mcp.popmelt.com/mcp";async function mt(o=ft){let e=ze(ht(),".claude.json"),n;try{let s=await ut(e,"utf-8");n=JSON.parse(s)}catch(s){n={}}(!n.mcpServers||typeof n.mcpServers!="object")&&(n.mcpServers={});let t=n.mcpServers;for(let s of Object.keys(t))if(/popmelt/i.test(s))return{installed:!1,provider:"claude",scope:null,reason:"already_configured"};return t.popmelt={type:"http",url:o},await pt(e,JSON.stringify(n,null,2)+`
75
- `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function gt(o=ft){let e=process.env.CODEX_HOME||ze(ht(),".codex"),n=ze(e,"config.toml"),t;try{t=await ut(n,"utf-8")}catch(a){t=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(t))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await Kt(Vt(n),{recursive:!0});let s=`
74
+ `)}),n}function Kt(o){return/enabled\s*=\s*false/i.test(o)}async function ze(o){let e=process.env.CODEX_HOME||ge(ut(),".codex"),n=await lt(ge(e,"config.toml"),"user");if(n.found)return n;let t=await lt(ge(o,".codex","config.toml"),"project");return t.found?t:Fe()}async function lt(o,e){try{let n=await dt(o,"utf-8"),t=Qt(n);for(let s of t)if(Ue.test(s.name)){let a=Kt(s.body);return xe(s.name,e,a)}}catch(n){}return Fe()}import{mkdir as Vt,readFile as pt,writeFile as ht}from"fs/promises";import{homedir as ft}from"os";import{dirname as Xt,join as Le}from"path";var mt="https://mcp.popmelt.com/mcp";async function gt(o=mt){let e=Le(ft(),".claude.json"),n;try{let s=await pt(e,"utf-8");n=JSON.parse(s)}catch(s){n={}}(!n.mcpServers||typeof n.mcpServers!="object")&&(n.mcpServers={});let t=n.mcpServers;for(let s of Object.keys(t))if(/popmelt/i.test(s))return{installed:!1,provider:"claude",scope:null,reason:"already_configured"};return t.popmelt={type:"http",url:o},await ht(e,JSON.stringify(n,null,2)+`
75
+ `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function yt(o=mt){let e=process.env.CODEX_HOME||Le(ft(),".codex"),n=Le(e,"config.toml"),t;try{t=await pt(n,"utf-8")}catch(a){t=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(t))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await Vt(Xt(n),{recursive:!0});let s=`
76
76
  [mcp_servers.popmelt]
77
77
  url = "${o}"
78
- `;return await pt(n,t+s,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function ge(o){let n=(o.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!n)throw new Error("Missing multipart boundary");let t=n[1]||n[2],s=await Xt(o),a=Buffer.from(`--${t}`),i=Buffer.from(`--${t}--`),c,p,u,v,W,y,T,S,F,b,j,K=[],ee=0,q=[];for(;ee<s.length;){let B=s.indexOf(a,ee);if(B===-1)break;let te=B+a.length;if(s.slice(B,B+i.length).equals(i))break;let A=te;s[A]===13&&s[A+1]===10&&(A+=2);let z=s.indexOf(`\r
78
+ `;return await ht(n,t+s,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function ye(o){let n=(o.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!n)throw new Error("Missing multipart boundary");let t=n[1]||n[2],s=await Zt(o),a=Buffer.from(`--${t}`),i=Buffer.from(`--${t}--`),c,p,u,x,G,w,M,I,F,P,j,Q=[],ee=0,W=[];for(;ee<s.length;){let B=s.indexOf(a,ee);if(B===-1)break;let te=B+a.length;if(s.slice(B,B+i.length).equals(i))break;let J=te;s[J]===13&&s[J+1]===10&&(J+=2);let z=s.indexOf(`\r
79
79
  \r
80
- `,A);if(z===-1)break;let P=s.slice(A,z).toString("utf-8"),Y=z+4,L=s.indexOf(a,Y),Z=L!==-1?L-2:s.length;q.push({headers:P,body:s.slice(Y,Z)}),ee=L!==-1?L:s.length}for(let B of q){let te=B.headers.match(/name="([^"]+)"/);if(!te)continue;let A=te[1];if(A==="screenshot")c=B.body;else if(A==="feedback")p=B.body.toString("utf-8");else if(A==="color")u=B.body.toString("utf-8");else if(A==="provider")v=B.body.toString("utf-8");else if(A==="model")W=B.body.toString("utf-8");else if(A==="goal")y=B.body.toString("utf-8");else if(A==="pageUrl")T=B.body.toString("utf-8");else if(A==="viewport")S=B.body.toString("utf-8");else if(A==="planId")F=B.body.toString("utf-8");else if(A==="manifest")b=B.body.toString("utf-8");else if(A==="tasks")j=B.body.toString("utf-8");else if(A.startsWith("image-")){let z=A.split("-"),P=parseInt(z[z.length-1],10),Y=z.slice(1,-1).join("-");Y&&!isNaN(P)&&K.push({annotationId:Y,index:P,data:B.body})}}if(!c)throw new Error("Missing screenshot field");return p||(p=""),{screenshot:c,feedback:p,color:u,provider:v,model:W,goal:y,pageUrl:T,viewport:S,planId:F,manifest:b,tasks:j,pastedImages:K}}function Xt(o){return new Promise((e,n)=>{let t=[];o.on("data",s=>t.push(s)),o.on("end",()=>e(Buffer.concat(t))),o.on("error",n)})}function xe(o,e){var t,s;let n=[];if(o.annotations.length>0){n.push("## Annotations");for(let a of o.annotations){let i=a.elements.map(u=>{let v=[u.selector];return u.reactComponent&&v.push(`(${u.reactComponent})`),v.join(" ")}).join(", "),c=a.instruction||"No text";n.push(`- id=${a.id} [${a.type}] ${c} \u2192 Elements: ${i||"none"}`);let p=e==null?void 0:e[a.id];if(p&&p.length>0)for(let u of p)n.push(` Attached image: use the Read tool to view ${u}`)}}if(o.styleModifications.length>0){n.push(""),n.push("## Style Changes (make permanent in source)"),n.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 a of o.styleModifications){let i=(t=a.element)!=null&&t.reactComponent?`(${a.element.reactComponent})`:"";for(let c of a.changes)n.push(`- ${a.selector} ${i}: ${c.property} ${c.original} \u2192 ${c.modified}`)}}if((s=o.spacingTokenChanges)!=null&&s.length){n.push(""),n.push("## Spacing Token Changes"),n.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let a of o.spacingTokenChanges){n.push(`
80
+ `,J);if(z===-1)break;let $=s.slice(J,z).toString("utf-8"),q=z+4,L=s.indexOf(a,q),X=L!==-1?L-2:s.length;W.push({headers:$,body:s.slice(q,X)}),ee=L!==-1?L:s.length}for(let B of W){let te=B.headers.match(/name="([^"]+)"/);if(!te)continue;let J=te[1];if(J==="screenshot")c=B.body;else if(J==="feedback")p=B.body.toString("utf-8");else if(J==="color")u=B.body.toString("utf-8");else if(J==="provider")x=B.body.toString("utf-8");else if(J==="model")G=B.body.toString("utf-8");else if(J==="goal")w=B.body.toString("utf-8");else if(J==="pageUrl")M=B.body.toString("utf-8");else if(J==="viewport")I=B.body.toString("utf-8");else if(J==="planId")F=B.body.toString("utf-8");else if(J==="manifest")P=B.body.toString("utf-8");else if(J==="tasks")j=B.body.toString("utf-8");else if(J.startsWith("image-")){let z=J.split("-"),$=parseInt(z[z.length-1],10),q=z.slice(1,-1).join("-");q&&!isNaN($)&&Q.push({annotationId:q,index:$,data:B.body})}}if(!c)throw new Error("Missing screenshot field");return p||(p=""),{screenshot:c,feedback:p,color:u,provider:x,model:G,goal:w,pageUrl:M,viewport:I,planId:F,manifest:P,tasks:j,pastedImages:Q}}function Zt(o){return new Promise((e,n)=>{let t=[];o.on("data",s=>t.push(s)),o.on("end",()=>e(Buffer.concat(t))),o.on("error",n)})}function Ie(o,e){var t,s;let n=[];if(o.annotations.length>0){n.push("## Annotations");for(let a of o.annotations){let i=a.elements.map(u=>{let x=[u.selector];return u.reactComponent&&x.push(`(${u.reactComponent})`),x.join(" ")}).join(", "),c=a.instruction||"No text";n.push(`- id=${a.id} [${a.type}] ${c} \u2192 Elements: ${i||"none"}`);let p=e==null?void 0:e[a.id];if(p&&p.length>0)for(let u of p)n.push(` Attached image: use the Read tool to view ${u}`)}}if(o.styleModifications.length>0){n.push(""),n.push("## Style Changes (make permanent in source)"),n.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 a of o.styleModifications){let i=(t=a.element)!=null&&t.reactComponent?`(${a.element.reactComponent})`:"";for(let c of a.changes)n.push(`- ${a.selector} ${i}: ${c.property} ${c.original} \u2192 ${c.modified}`)}}if((s=o.spacingTokenChanges)!=null&&s.length){n.push(""),n.push("## Spacing Token Changes"),n.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let a of o.spacingTokenChanges){n.push(`
81
81
  ### ${a.tokenName}: ${a.originalPx}px \u2192 ${a.newPx}px`);for(let i of a.affectedElements){let c=i.reactComponent?` (${i.reactComponent})`:"";i.matchedClass&&i.suggestedClass?n.push(`- ${i.selector}${c}: \`${i.matchedClass}\` \u2192 \`${i.suggestedClass}\``):n.push(`- ${i.selector}${c}: ${i.property} ${a.originalPx}px \u2192 ${a.newPx}px`),n.push(` class="${i.className}"`)}}}if(o.inspectedElement){let a=o.inspectedElement;n.push(""),n.push("## Inspected Element"),n.push("The developer has this element selected in the inspector:");let i=[a.selector];a.reactComponent&&i.push(`(${a.reactComponent})`),a.context&&i.push(`in ${a.context}`),a.textContent&&i.push(`"${a.textContent.slice(0,80)}"`),n.push(`- ${i.join(" ")}`)}return n.join(`
82
- `)}function yt(o,e,n){var a;let t=[];if(t.push("You are reviewing a UI screenshot with developer annotations."),t.push(""),(n==null?void 0:n.provider)!=="codex"&&(t.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`),t.push("")),t.push(`The developer annotated their running app at ${e.url} (${e.viewport.width}x${e.viewport.height}).`),n!=null&&n.threadHistory&&n.threadHistory.length>0){t.push(""),t.push("## Previous Conversation");let i=0;for(let c of n.threadHistory)if(c.role==="human")i++,c.replyToQuestion?(t.push(`### Round ${i} (human) \u2014 reply`),t.push(`"${c.replyToQuestion}"`)):(t.push(`### Round ${i} (human)`),c.feedbackSummary&&t.push(`Annotations: ${c.feedbackSummary}`),c.annotationIds&&c.annotationIds.length>0&&t.push(`Annotation IDs: ${c.annotationIds.join(", ")}`));else if(c.question)t.push(`### Round ${i} (assistant) \u2014 question`),t.push(`"${c.question}"`);else{if(t.push(`### Round ${i} (assistant)`),c.responseText&&t.push(`Response: ${c.responseText}`),c.resolutions&&c.resolutions.length>0)for(let p of c.resolutions){let u=(a=p.finalScope)!=null?a:p.inferredScope,v=u?` [${u.breadth} ${u.target}]`:"";t.push(`- ${p.annotationId}: ${p.status}${v} \u2014 ${p.summary}`),p.filesModified&&p.filesModified.length>0&&t.push(` Files: ${p.filesModified.join(", ")}`)}c.toolsUsed&&c.toolsUsed.length>0&&t.push(`Tools used: ${c.toolsUsed.join(", ")}`)}t.push(""),t.push("The current round is shown in full below.")}if(n!=null&&n.designModel){t.push(""),t.push("## Established Design Policies"),t.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 i=n.designModel.rules;if(Array.isArray(i)&&i.length>0){t.push(""),t.push("Rules:");for(let u of i)typeof u=="string"&&t.push(`- ${u}`)}let c=n.designModel.tokens;c&&typeof c=="object"&&(t.push(""),t.push("Design tokens:"),t.push("```json"),t.push(JSON.stringify(c,null,2)),t.push("```"));let p=n.designModel.components;p&&typeof p=="object"&&(t.push(""),t.push("Component patterns:"),t.push("```json"),t.push(JSON.stringify(p,null,2)),t.push("```")),t.push(""),t.push("### Novel Pattern Detection"),t.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:"),t.push("<novel>"),t.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),t.push("</novel>"),t.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),t.push("- `element`: What you are styling or creating"),t.push("- `decision`: What you decided to do (specific values)"),t.push("- `reason`: Why this is novel (what is missing from the model)"),t.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}n!=null&&n.designModel||(t.push(""),t.push("## Design Context"),t.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."));let s=xe(e,n==null?void 0:n.imagePaths);return s&&(t.push(""),t.push(s)),t.push(""),t.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."),t.push(""),t.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),t.push(""),t.push("## Resolution"),t.push("After completing all work, output a resolution block listing what you did for each annotation:"),t.push("<resolution>"),t.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),t.push("</resolution>"),t.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),t.push(""),t.push("### Scope classification"),t.push("Each resolution MUST include scope fields:"),t.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),t.push("- `inferredScope`: What scope the change actually has, based on what you modified."),t.push("Scope has two dimensions:"),t.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),t.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),t.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),t.push("If you cannot confidently determine scope, set it to null."),t.push(""),t.push("## Questions"),t.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),t.push('<question>What do you mean by "..."?</question>'),t.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),t.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),t.join(`
83
- `)}function wt(o){var n;let e=o.match(/<question>\s*([\s\S]*?)\s*<\/question>/);return(n=e==null?void 0:e[1])!=null?n:null}function vt(o,e,n,t){let s=[];s.push("You are continuing work on a UI based on the developer's reply to your question."),s.push(""),n!=="codex"&&s.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`);let a=e.find(i=>i.role==="human"&&i.feedbackContext);if(a!=null&&a.feedbackContext&&(s.push(""),s.push(a.feedbackContext)),e.length>0){s.push(""),s.push("## Conversation History");let i=0;for(let c of e)c.role==="human"?(i++,c.replyToQuestion?(s.push(`### Round ${i} (human) \u2014 reply`),s.push(`"${c.replyToQuestion}"`)):(s.push(`### Round ${i} (human)`),c.feedbackSummary&&s.push(`Annotations: ${c.feedbackSummary}`))):c.question?(s.push(`### Round ${i} (assistant) \u2014 question`),s.push(`"${c.question}"`)):(s.push(`### Round ${i} (assistant)`),c.responseText&&s.push(`Response: ${c.responseText}`))}if(s.push(""),s.push("The developer answered your question. Continue working based on their reply."),s.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),s.push(""),s.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),t&&t.length>0){s.push(""),s.push("## Attached Images"),s.push("The developer attached reference images with their reply:");for(let i of t)s.push(`Attached image: use the Read tool to view the image at: ${i}`)}return s.push(""),s.push("## Resolution"),s.push("After completing all work, output a resolution block listing what you did for each annotation:"),s.push("<resolution>"),s.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),s.push("</resolution>"),s.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),s.push(""),s.push("### Scope classification"),s.push("Each resolution MUST include scope fields:"),s.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),s.push("- `inferredScope`: What scope the change actually has, based on what you modified."),s.push("Scope has two dimensions:"),s.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),s.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),s.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),s.push("If you cannot confidently determine scope, set it to null."),s.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.'),s.push(""),s.push("## Questions"),s.push("If you still need clarification, output:"),s.push("<question>Your question here</question>"),s.push("You may output BOTH a <resolution> and a <question> in the same response."),s.join(`
84
- `)}function Zt(o){if(typeof o!="object"||o===null)return!1;let e=o;return(e.breadth==="instance"||e.breadth==="pattern")&&(e.target==="element"||e.target==="component"||e.target==="token")}function St(o){if(typeof o!="object"||o===null||typeof o.annotationId!="string"||o.status!=="resolved"&&o.status!=="needs_review"||typeof o.summary!="string")return!1;let e=o;for(let n of["declaredScope","inferredScope","finalScope"])if(e[n]!==void 0&&e[n]!==null&&!Zt(e[n]))return!1;return!0}function xt(o){let e=o.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!e||!e[1])return[];try{let n=JSON.parse(e[1]);return Array.isArray(n)?n.filter(St):[]}catch(n){return[]}}function It(o){let e=[],n=/<resolution>\s*([\s\S]*?)\s*<\/resolution>/g,t;for(;(t=n.exec(o))!==null;)if(t[1])try{let s=JSON.parse(t[1]);Array.isArray(s)&&e.push(...s.filter(St))}catch(s){}return e}function kt(o){let e=o.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!(e!=null&&e[1]))return[];try{let n=JSON.parse(e[1]);return Array.isArray(n)?n.filter(t=>{if(typeof t!="object"||t===null)return!1;let s=t;return(s.category==="token"||s.category==="component"||s.category==="element")&&typeof s.element=="string"&&typeof s.decision=="string"&&typeof s.reason=="string"}):[]}catch(n){return[]}}function bt(o,e,n,t,s,a){let i=[];return i.push("You are a UI design planner. You are looking at a full-page screenshot of a web application."),i.push(""),i.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`),i.push(""),i.push(`Page: ${n}`),i.push(`Viewport: ${t.width}x${t.height}`),s&&(i.push(""),i.push("## Page Elements (ground truth)"),i.push("Below is a structured inventory of actual DOM elements on this page. Cross-reference"),i.push("against this list \u2014 do NOT reference elements that aren't listed here."),i.push(""),i.push("<manifest>"),i.push(s),i.push("</manifest>")),a&&(i.push(""),i.push("## Developer Context"),i.push("The developer has the following annotations and style changes on their canvas. Factor these into your plan:"),i.push(a)),i.push(""),i.push("## Goal"),i.push(e),i.push(""),i.push("## Your Task"),i.push("Analyze the screenshot and decompose the goal into specific, element-level tasks."),i.push("Each task targets a specific region of the page and gives a clear instruction for a worker agent."),i.push(""),i.push("Output your plan as a JSON array inside a <plan> tag. Each task has:"),i.push('- `id`: A short unique identifier (e.g., "t1", "t2")'),i.push("- `instruction`: Clear, specific instruction for a worker agent (what to change and how)"),i.push("- `region`: Bounding box in page coordinates `{x, y, width, height}` \u2014 where (x,y) is top-left corner"),i.push("- `priority`: Optional 1-5 (1=highest). Tasks with no dependency can share a priority level."),i.push(""),i.push("Example:"),i.push("<plan>"),i.push("["),i.push(' {"id":"t1","instruction":"Increase heading font-size to 48px and change font-weight to 700","region":{"x":100,"y":50,"width":600,"height":80},"priority":1},'),i.push(' {"id":"t2","instruction":"Add a subtle box-shadow to the card container","region":{"x":80,"y":200,"width":640,"height":300},"priority":2}'),i.push("]"),i.push("</plan>"),i.push(""),i.push("Guidelines:"),i.push("- CRITICAL: Cross-check all element references against the <manifest>. Only reference elements that actually exist. Use the manifest's text content, component names, and bounding rects for precise instructions."),i.push('- Be specific about values (colors, sizes, spacing) rather than vague ("make it look better")'),i.push("- Each task should be independently actionable by a worker that can only see its region"),i.push("- Regions should tightly bound the relevant UI element(s)"),i.push("- Keep tasks atomic \u2014 one change per task, not multiple unrelated changes"),i.push("- Order by priority: structural changes first, then visual polish"),i.push("- If the goal can be accomplished as a single change, return a plan with just one task. Only decompose when the goal genuinely requires multiple independent changes."),i.push("- If the goal is unclear or you need more context, output a question instead:"),i.push("<question>Your question here</question>"),i.push(""),i.push("Do NOT modify any files. You are a planner only \u2014 output a <plan> or <question>, nothing else."),i.join(`
85
- `)}function Pt(o){let e=o.match(/<plan>\s*([\s\S]*?)\s*<\/plan>/);if(!(e!=null&&e[1]))return null;try{let n=JSON.parse(e[1]);return Array.isArray(n)?n.filter(t=>{if(typeof t!="object"||t===null)return!1;let s=t;if(typeof s.id!="string"||typeof s.instruction!="string"||typeof s.region!="object"||s.region===null)return!1;let a=s.region;return typeof a.x=="number"&&typeof a.y=="number"&&typeof a.width=="number"&&typeof a.height=="number"}):null}catch(n){return null}}function Tt(o,e,n){let t=[];t.push("You are reviewing whether a series of UI changes achieved the original design goal."),t.push(""),t.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`),t.push(""),t.push("## Original Goal"),t.push(e),t.push(""),t.push("## Completed Tasks");for(let s of n)t.push(`- [${s.id}] ${s.instruction} \u2192 ${s.summary}`);return t.push(""),t.push("## Your Task"),t.push("Look at the current screenshot and determine if the goal has been achieved."),t.push("Output your verdict inside a <review> tag:"),t.push("<review>"),t.push('{"verdict":"pass","summary":"The changes look good..."}'),t.push("</review>"),t.push(""),t.push("Or if issues remain:"),t.push("<review>"),t.push('{"verdict":"fail","summary":"Some issues remain...","issues":["Issue 1","Issue 2"]}'),t.push("</review>"),t.push(""),t.push("Do NOT modify any files. Output only a <review> block."),t.join(`
86
- `)}function $t(o,e,n,t,s){let a=[];a.push("You are implementing a series of UI changes on a web application."),a.push(""),s!=="codex"&&(a.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`),a.push("")),a.push(`Page: ${n} (${t.width}x${t.height})`),a.push(""),a.push("## Tasks"),a.push("Each task targets a specific region of the page. Complete them in order."),a.push("");for(let i of e){if(a.push(`### Task ${i.planTaskId} (annotationId: ${i.annotationId})`),a.push(`Instruction: ${i.instruction}`),a.push(`Region: (${i.region.x}, ${i.region.y}) ${i.region.width}x${i.region.height}`),i.linkedSelector&&a.push(`Target element: ${i.linkedSelector}`),i.elements&&i.elements.length>0){let c=i.elements.map(p=>{let u=[p.selector];return p.reactComponent&&u.push(`(${p.reactComponent})`),u.join(" ")}).join(", ");a.push(`Elements: ${c}`)}a.push("")}return a.push("## Instructions"),a.push("- Apply each change to the source files \u2014 the dev server has HMR so changes appear immediately."),a.push("- IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source."),a.push("- You may use parallel subagents (Task tool) for independent changes, or work serially \u2014 use your judgment."),a.push(""),a.push("## Resolution"),a.push("CRITICAL: After completing EACH task, immediately output a <resolution> block for that task."),a.push("Do NOT wait until all tasks are done \u2014 output each resolution as soon as that task is finished."),a.push("<resolution>"),a.push('[{"annotationId":"<annotationId>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),a.push("</resolution>"),a.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),a.push(""),a.push("### Scope classification"),a.push("Each resolution MUST include scope fields:"),a.push("- `declaredScope`: What scope the task instruction implies. null if no signal."),a.push("- `inferredScope`: What scope the change actually has, based on what you modified."),a.push("Scope has two dimensions:"),a.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),a.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),a.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),a.push("If you cannot confidently determine scope, set it to null."),a.join(`
87
- `)}function Rt(o){let e=o.match(/<review>\s*([\s\S]*?)\s*<\/review>/);if(!(e!=null&&e[1]))return null;try{let n=JSON.parse(e[1]);return typeof n!="object"||n===null||n.verdict!=="pass"&&n.verdict!=="fail"||typeof n.summary!="string"?null:{verdict:n.verdict,summary:n.summary,issues:Array.isArray(n.issues)?n.issues.filter(t=>typeof t=="string"):void 0}}catch(n){return null}}var Ce=class{constructor(e=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;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,n){n?this.activeProcesses.set(e,n):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,n){for(let t of this.listeners)t(e,n)}cancelJob(e){let n=this.activeProcesses.get(e),t=this.activeJobs.get(e);return!n||!t?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(e),this.activeJobs.delete(e),t.status="error",t.error="Cancelled by user",this.broadcast({type:"error",jobId:t.id,message:"Cancelled by user"},t.id),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let e=Array.from(this.activeJobs.keys());for(let n of e)this.cancelJob(n);return!0}destroy(){for(let e of this.activeProcesses.values())e.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.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},e.id),this.processor(e).catch(n=>{e.status="error",e.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:e.id,message:e.error},e.id)}).finally(()=>{this.activeJobs.delete(e.id),this.activeProcesses.delete(e.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},e.id)})}}};import{mkdir as en,readFile as tn,writeFile as nn}from"fs/promises";import{dirname as sn,join as on}from"path";var rn={version:1,threads:{}},De=class{constructor(e){this.cache=null;this.writeChain=Promise.resolve();this.filePath=on(e,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let e=await tn(this.filePath,"utf-8"),n=JSON.parse(e);if(n&&n.version===1&&n.threads)return this.cache=n,this.cache}catch(e){}return this.cache=oe(V({},rn),{threads:{}}),this.cache}async getThread(e){var t;return(t=(await this.load()).threads[e])!=null?t:null}async findContinuationThread(e){if(e.length===0)return null;let n=await this.load(),t=new Set(e);for(let s of Object.values(n.threads))if(s.elementIdentifiers.some(i=>t.has(i)))return s;return null}async createThread(e,n){let t=await this.load(),s={id:e,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return t.threads[e]=s,await this.persist(),s}async appendMessage(e,n){let s=(await this.load()).threads[e];s&&(s.messages.push(n),s.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(e,n){let s=(await this.load()).threads[e];if(!s)return;let a=new Set(s.elementIdentifiers);for(let i of n)a.has(i)||s.elementIdentifiers.push(i);s.updatedAt=Date.now(),await this.persist()}async getThreadHistory(e,n=6){let t=await this.getThread(e);return!t||t.messages.length===0?[]:t.messages.length<=n?t.messages:[t.messages[0],...t.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await en(sn(this.filePath),{recursive:!0}),await nn(this.filePath,JSON.stringify(this.cache,null,2))}catch(e){console.error("[ThreadStore] Failed to persist:",e)}}),await this.writeChain}};var fn=1111,mn=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],gn=1800*1e3,yn=3600*1e3;function wn(o){if(!o)return!1;try{let e=new URL(o);return e.hostname==="localhost"||e.hostname==="127.0.0.1"}catch(e){return!1}}function vn(o,e){let n=o.headers.origin;wn(n)&&(e.setHeader("Access-Control-Allow-Origin",n),e.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),e.setHeader("Access-Control-Allow-Headers","Content-Type"))}function g(o,e,n){o.writeHead(e,{"Content-Type":"application/json"}),o.end(JSON.stringify(n))}function Sn(o,e){if(!o)return e;let n=o.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!n)return e;let[,t,s,a]=n;return`\x1B[38;2;${parseInt(t,16)};${parseInt(s,16)};${parseInt(a,16)}m${e}\x1B[0m`}function Mt(o,e){try{o.res.write(`event: ${e.type}
82
+ `)}function wt(o,e,n){var a;let t=[];if(t.push("You are reviewing a UI screenshot with developer annotations."),t.push(""),(n==null?void 0:n.provider)!=="codex"&&(t.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`),t.push("")),t.push(`The developer annotated their running app at ${e.url} (${e.viewport.width}x${e.viewport.height}).`),n!=null&&n.threadHistory&&n.threadHistory.length>0){t.push(""),t.push("## Previous Conversation");let i=0;for(let c of n.threadHistory)if(c.role==="human")i++,c.replyToQuestion?(t.push(`### Round ${i} (human) \u2014 reply`),t.push(`"${c.replyToQuestion}"`)):(t.push(`### Round ${i} (human)`),c.feedbackSummary&&t.push(`Annotations: ${c.feedbackSummary}`),c.annotationIds&&c.annotationIds.length>0&&t.push(`Annotation IDs: ${c.annotationIds.join(", ")}`));else if(c.question)t.push(`### Round ${i} (assistant) \u2014 question`),t.push(`"${c.question}"`);else{if(t.push(`### Round ${i} (assistant)`),c.responseText&&t.push(`Response: ${c.responseText}`),c.resolutions&&c.resolutions.length>0)for(let p of c.resolutions){let u=(a=p.finalScope)!=null?a:p.inferredScope,x=u?` [${u.breadth} ${u.target}]`:"";t.push(`- ${p.annotationId}: ${p.status}${x} \u2014 ${p.summary}`),p.filesModified&&p.filesModified.length>0&&t.push(` Files: ${p.filesModified.join(", ")}`)}c.toolsUsed&&c.toolsUsed.length>0&&t.push(`Tools used: ${c.toolsUsed.join(", ")}`)}t.push(""),t.push("The current round is shown in full below.")}if(n!=null&&n.designModel){t.push(""),t.push("## Established Design Policies"),t.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 i=n.designModel.rules;if(Array.isArray(i)&&i.length>0){t.push(""),t.push("Rules:");for(let u of i)typeof u=="string"&&t.push(`- ${u}`)}let c=n.designModel.tokens;c&&typeof c=="object"&&(t.push(""),t.push("Design tokens:"),t.push("```json"),t.push(JSON.stringify(c,null,2)),t.push("```"));let p=n.designModel.components;p&&typeof p=="object"&&(t.push(""),t.push("Component patterns:"),t.push("```json"),t.push(JSON.stringify(p,null,2)),t.push("```")),t.push(""),t.push("### Novel Pattern Detection"),t.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:"),t.push("<novel>"),t.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),t.push("</novel>"),t.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),t.push("- `element`: What you are styling or creating"),t.push("- `decision`: What you decided to do (specific values)"),t.push("- `reason`: Why this is novel (what is missing from the model)"),t.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}n!=null&&n.designModel||(t.push(""),t.push("## Design Context"),t.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."));let s=Ie(e,n==null?void 0:n.imagePaths);return s&&(t.push(""),t.push(s)),t.push(""),t.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."),t.push(""),t.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),t.push(""),t.push("## Resolution"),t.push("After completing all work, output a resolution block listing what you did for each annotation:"),t.push("<resolution>"),t.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),t.push("</resolution>"),t.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),t.push(""),t.push("### Scope classification"),t.push("Each resolution MUST include scope fields:"),t.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),t.push("- `inferredScope`: What scope the change actually has, based on what you modified."),t.push("Scope has two dimensions:"),t.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),t.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),t.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),t.push("If you cannot confidently determine scope, set it to null."),t.push(""),t.push("## Questions"),t.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),t.push('<question>What do you mean by "..."?</question>'),t.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),t.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),t.join(`
83
+ `)}function vt(o){var n;let e=o.match(/<question>\s*([\s\S]*?)\s*<\/question>/);return(n=e==null?void 0:e[1])!=null?n:null}function St(o,e,n,t){let s=[];s.push("You are continuing work on a UI based on the developer's reply to your question."),s.push(""),n!=="codex"&&s.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`);let a=e.find(i=>i.role==="human"&&i.feedbackContext);if(a!=null&&a.feedbackContext&&(s.push(""),s.push(a.feedbackContext)),e.length>0){s.push(""),s.push("## Conversation History");let i=0;for(let c of e)c.role==="human"?(i++,c.replyToQuestion?(s.push(`### Round ${i} (human) \u2014 reply`),s.push(`"${c.replyToQuestion}"`)):(s.push(`### Round ${i} (human)`),c.feedbackSummary&&s.push(`Annotations: ${c.feedbackSummary}`))):c.question?(s.push(`### Round ${i} (assistant) \u2014 question`),s.push(`"${c.question}"`)):(s.push(`### Round ${i} (assistant)`),c.responseText&&s.push(`Response: ${c.responseText}`))}if(s.push(""),s.push("The developer answered your question. Continue working based on their reply."),s.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),s.push(""),s.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),t&&t.length>0){s.push(""),s.push("## Attached Images"),s.push("The developer attached reference images with their reply:");for(let i of t)s.push(`Attached image: use the Read tool to view the image at: ${i}`)}return s.push(""),s.push("## Resolution"),s.push("After completing all work, output a resolution block listing what you did for each annotation:"),s.push("<resolution>"),s.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),s.push("</resolution>"),s.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),s.push(""),s.push("### Scope classification"),s.push("Each resolution MUST include scope fields:"),s.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),s.push("- `inferredScope`: What scope the change actually has, based on what you modified."),s.push("Scope has two dimensions:"),s.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),s.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),s.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),s.push("If you cannot confidently determine scope, set it to null."),s.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.'),s.push(""),s.push("## Questions"),s.push("If you still need clarification, output:"),s.push("<question>Your question here</question>"),s.push("You may output BOTH a <resolution> and a <question> in the same response."),s.join(`
84
+ `)}function en(o){if(typeof o!="object"||o===null)return!1;let e=o;return(e.breadth==="instance"||e.breadth==="pattern")&&(e.target==="element"||e.target==="component"||e.target==="token")}function xt(o){if(typeof o!="object"||o===null||typeof o.annotationId!="string"||o.status!=="resolved"&&o.status!=="needs_review"||typeof o.summary!="string")return!1;let e=o;for(let n of["declaredScope","inferredScope","finalScope"])if(e[n]!==void 0&&e[n]!==null&&!en(e[n]))return!1;return!0}function It(o){let e=o.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!e||!e[1])return[];try{let n=JSON.parse(e[1]);return Array.isArray(n)?n.filter(xt):[]}catch(n){return[]}}function kt(o){let e=[],n=/<resolution>\s*([\s\S]*?)\s*<\/resolution>/g,t;for(;(t=n.exec(o))!==null;)if(t[1])try{let s=JSON.parse(t[1]);Array.isArray(s)&&e.push(...s.filter(xt))}catch(s){}return e}function bt(o){let e=o.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!(e!=null&&e[1]))return[];try{let n=JSON.parse(e[1]);return Array.isArray(n)?n.filter(t=>{if(typeof t!="object"||t===null)return!1;let s=t;return(s.category==="token"||s.category==="component"||s.category==="element")&&typeof s.element=="string"&&typeof s.decision=="string"&&typeof s.reason=="string"}):[]}catch(n){return[]}}function Pt(o,e,n,t,s,a){let i=[];return i.push("You are a UI design planner. You are looking at a full-page screenshot of a web application."),i.push(""),i.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`),i.push(""),i.push(`Page: ${n}`),i.push(`Viewport: ${t.width}x${t.height}`),s&&(i.push(""),i.push("## Page Elements (ground truth)"),i.push("Below is a structured inventory of actual DOM elements on this page. Cross-reference"),i.push("against this list \u2014 do NOT reference elements that aren't listed here."),i.push(""),i.push("<manifest>"),i.push(s),i.push("</manifest>")),a&&(i.push(""),i.push("## Developer Context"),i.push("The developer has the following annotations and style changes on their canvas. Factor these into your plan:"),i.push(a)),i.push(""),i.push("## Goal"),i.push(e),i.push(""),i.push("## Your Task"),i.push("Analyze the screenshot and decompose the goal into specific, element-level tasks."),i.push("Each task targets a specific region of the page and gives a clear instruction for a worker agent."),i.push(""),i.push("Output your plan as a JSON array inside a <plan> tag. Each task has:"),i.push('- `id`: A short unique identifier (e.g., "t1", "t2")'),i.push("- `instruction`: Clear, specific instruction for a worker agent (what to change and how)"),i.push("- `region`: Bounding box in page coordinates `{x, y, width, height}` \u2014 where (x,y) is top-left corner"),i.push("- `priority`: Optional 1-5 (1=highest). Tasks with no dependency can share a priority level."),i.push(""),i.push("Example:"),i.push("<plan>"),i.push("["),i.push(' {"id":"t1","instruction":"Increase heading font-size to 48px and change font-weight to 700","region":{"x":100,"y":50,"width":600,"height":80},"priority":1},'),i.push(' {"id":"t2","instruction":"Add a subtle box-shadow to the card container","region":{"x":80,"y":200,"width":640,"height":300},"priority":2}'),i.push("]"),i.push("</plan>"),i.push(""),i.push("Guidelines:"),i.push("- CRITICAL: Cross-check all element references against the <manifest>. Only reference elements that actually exist. Use the manifest's text content, component names, and bounding rects for precise instructions."),i.push('- Be specific about values (colors, sizes, spacing) rather than vague ("make it look better")'),i.push("- Each task should be independently actionable by a worker that can only see its region"),i.push("- Regions should tightly bound the relevant UI element(s)"),i.push("- Keep tasks atomic \u2014 one change per task, not multiple unrelated changes"),i.push("- Order by priority: structural changes first, then visual polish"),i.push("- If the goal can be accomplished as a single change, return a plan with just one task. Only decompose when the goal genuinely requires multiple independent changes."),i.push("- If the goal is unclear or you need more context, output a question instead:"),i.push("<question>Your question here</question>"),i.push(""),i.push("Do NOT modify any files. You are a planner only \u2014 output a <plan> or <question>, nothing else."),i.join(`
85
+ `)}function Tt(o){let e=o.match(/<plan>\s*([\s\S]*?)\s*<\/plan>/);if(!(e!=null&&e[1]))return null;try{let n=JSON.parse(e[1]);return Array.isArray(n)?n.filter(t=>{if(typeof t!="object"||t===null)return!1;let s=t;if(typeof s.id!="string"||typeof s.instruction!="string"||typeof s.region!="object"||s.region===null)return!1;let a=s.region;return typeof a.x=="number"&&typeof a.y=="number"&&typeof a.width=="number"&&typeof a.height=="number"}):null}catch(n){return null}}function $t(o,e,n){let t=[];t.push("You are reviewing whether a series of UI changes achieved the original design goal."),t.push(""),t.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`),t.push(""),t.push("## Original Goal"),t.push(e),t.push(""),t.push("## Completed Tasks");for(let s of n)t.push(`- [${s.id}] ${s.instruction} \u2192 ${s.summary}`);return t.push(""),t.push("## Your Task"),t.push("Look at the current screenshot and determine if the goal has been achieved."),t.push("Output your verdict inside a <review> tag:"),t.push("<review>"),t.push('{"verdict":"pass","summary":"The changes look good..."}'),t.push("</review>"),t.push(""),t.push("Or if issues remain:"),t.push("<review>"),t.push('{"verdict":"fail","summary":"Some issues remain...","issues":["Issue 1","Issue 2"]}'),t.push("</review>"),t.push(""),t.push("Do NOT modify any files. Output only a <review> block."),t.join(`
86
+ `)}function Rt(o,e,n,t,s){let a=[];a.push("You are implementing a series of UI changes on a web application."),a.push(""),s!=="codex"&&(a.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${o}`),a.push("")),a.push(`Page: ${n} (${t.width}x${t.height})`),a.push(""),a.push("## Tasks"),a.push("Each task targets a specific region of the page. Complete them in order."),a.push("");for(let i of e){if(a.push(`### Task ${i.planTaskId} (annotationId: ${i.annotationId})`),a.push(`Instruction: ${i.instruction}`),a.push(`Region: (${i.region.x}, ${i.region.y}) ${i.region.width}x${i.region.height}`),i.linkedSelector&&a.push(`Target element: ${i.linkedSelector}`),i.elements&&i.elements.length>0){let c=i.elements.map(p=>{let u=[p.selector];return p.reactComponent&&u.push(`(${p.reactComponent})`),u.join(" ")}).join(", ");a.push(`Elements: ${c}`)}a.push("")}return a.push("## Instructions"),a.push("- Apply each change to the source files \u2014 the dev server has HMR so changes appear immediately."),a.push("- IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source."),a.push("- You may use parallel subagents (Task tool) for independent changes, or work serially \u2014 use your judgment."),a.push(""),a.push("## Resolution"),a.push("CRITICAL: After completing EACH task, immediately output a <resolution> block for that task."),a.push("Do NOT wait until all tasks are done \u2014 output each resolution as soon as that task is finished."),a.push("<resolution>"),a.push('[{"annotationId":"<annotationId>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),a.push("</resolution>"),a.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),a.push(""),a.push("### Scope classification"),a.push("Each resolution MUST include scope fields:"),a.push("- `declaredScope`: What scope the task instruction implies. null if no signal."),a.push("- `inferredScope`: What scope the change actually has, based on what you modified."),a.push("Scope has two dimensions:"),a.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),a.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),a.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),a.push("If you cannot confidently determine scope, set it to null."),a.join(`
87
+ `)}function Mt(o){let e=o.match(/<review>\s*([\s\S]*?)\s*<\/review>/);if(!(e!=null&&e[1]))return null;try{let n=JSON.parse(e[1]);return typeof n!="object"||n===null||n.verdict!=="pass"&&n.verdict!=="fail"||typeof n.summary!="string"?null:{verdict:n.verdict,summary:n.summary,issues:Array.isArray(n.issues)?n.issues.filter(t=>typeof t=="string"):void 0}}catch(n){return null}}var Ae=class{constructor(e=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;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,n){n?this.activeProcesses.set(e,n):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,n){for(let t of this.listeners)t(e,n)}cancelJob(e){let n=this.activeProcesses.get(e),t=this.activeJobs.get(e);return!n||!t?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(e),this.activeJobs.delete(e),t.status="error",t.error="Cancelled by user",this.broadcast({type:"error",jobId:t.id,message:"Cancelled by user"},t.id),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let e=Array.from(this.activeJobs.keys());for(let n of e)this.cancelJob(n);return!0}destroy(){for(let e of this.activeProcesses.values())e.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.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},e.id),this.processor(e).catch(n=>{e.status="error",e.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:e.id,message:e.error},e.id)}).finally(()=>{this.activeJobs.delete(e.id),this.activeProcesses.delete(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:{}},De=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"),n=JSON.parse(e);if(n&&n.version===1&&n.threads)return this.cache=n,this.cache}catch(e){}return this.cache=oe(K({},an),{threads:{}}),this.cache}async getThread(e){var t;return(t=(await this.load()).threads[e])!=null?t:null}async findContinuationThread(e){if(e.length===0)return null;let n=await this.load(),t=new Set(e);for(let s of Object.values(n.threads))if(s.elementIdentifiers.some(i=>t.has(i)))return s;return null}async createThread(e,n){let t=await this.load(),s={id:e,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return t.threads[e]=s,await this.persist(),s}async appendMessage(e,n){let s=(await this.load()).threads[e];s&&(s.messages.push(n),s.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(e,n){let s=(await this.load()).threads[e];if(!s)return;let a=new Set(s.elementIdentifiers);for(let i of n)a.has(i)||s.elementIdentifiers.push(i);s.updatedAt=Date.now(),await this.persist()}async getThreadHistory(e,n=6){let t=await this.getThread(e);return!t||t.messages.length===0?[]:t.messages.length<=n?t.messages:[t.messages[0],...t.messages.slice(-(n-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 mn=1111,gn=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],yn=1800*1e3,wn=3600*1e3;function vn(o){if(!o)return!1;try{let e=new URL(o);return e.hostname==="localhost"||e.hostname==="127.0.0.1"}catch(e){return!1}}function Sn(o,e){let n=o.headers.origin;vn(n)&&(e.setHeader("Access-Control-Allow-Origin",n),e.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),e.setHeader("Access-Control-Allow-Headers","Content-Type"))}function g(o,e,n){o.writeHead(e,{"Content-Type":"application/json"}),o.end(JSON.stringify(n))}function xn(o,e){if(!o)return e;let n=o.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!n)return e;let[,t,s,a]=n;return`\x1B[38;2;${parseInt(t,16)};${parseInt(s,16)};${parseInt(a,16)}m${e}\x1B[0m`}function Ot(o,e){try{o.res.write(`event: ${e.type}
88
88
  data: ${JSON.stringify(e)}
89
89
 
90
- `)}catch(n){}}async function Le(o={}){var we,Ge,He,We,qe,Ye,Qe,Ke;let e=(we=o.port)!=null?we:fn,n=(Ge=o.projectRoot)!=null?Ge:process.cwd(),t=(He=o.tempDir)!=null?He:de(hn(),"popmelt-bridge"),s=(We=o.maxTurns)!=null?We:10,a=(qe=o.maxBudgetUsd)!=null?qe:1,i=(Ye=o.allowedTools)!=null?Ye:mn,c=(Qe=o.claudePath)!=null?Qe:"claude",p=(Ke=o.provider)!=null?Ke:"claude",u={};for(let r of["claude","codex"])try{let l=an("which",[r],{encoding:"utf-8"}).trim();u[r]={available:!0,path:l}}catch(l){u[r]={available:!1,path:null}}let[v,W]=await Promise.all([Fe(n),je(n)]);u.claude&&(u.claude.mcp=v),u.codex&&(u.codex.mcp=W),await cn(t,{recursive:!0}),Ot(t).catch(()=>{});let y=new Ce(1),T=new Set,S=new De(n),F=new Me(n),b=new Oe(n,F,{claudePath:c,onEvent:r=>{for(let l of T)Mt(l,r)}}),j=new Map;y.addListener((r,l)=>{for(let h of T)Mt(h,r)}),y.setProcessor(async r=>{var H,X,se,E,ne,ce,ve;let l=r._replyPrompt,h=(H=r.provider)!=null?H:p,d;if(r.threadId){let k=await S.getThread(r.threadId);if(k){for(let C=k.messages.length-1;C>=0;C--)if(k.messages[C].sessionId){d=k.messages[C].sessionId;break}}}let m;if(d&&l){let k=(X=await S.getThread(r.threadId))==null?void 0:X.messages.filter(M=>M.role==="human").pop();m=((k==null?void 0:k.replyToQuestion)||(k==null?void 0:k.feedbackSummary)||"")+`
90
+ `)}catch(n){}}async function Ge(o={}){var ve,He,We,qe,Ye,Qe,Ke,Ve;let e=(ve=o.port)!=null?ve:mn,n=(He=o.projectRoot)!=null?He:process.cwd(),t=(We=o.tempDir)!=null?We:de(fn(),"popmelt-bridge"),s=(qe=o.maxTurns)!=null?qe:10,a=(Ye=o.maxBudgetUsd)!=null?Ye:1,i=(Qe=o.allowedTools)!=null?Qe:gn,c=(Ke=o.claudePath)!=null?Ke:"claude",p=(Ve=o.provider)!=null?Ve:"claude",u={};for(let r of["claude","codex"])try{let l=cn("which",[r],{encoding:"utf-8"}).trim();u[r]={available:!0,path:l}}catch(l){u[r]={available:!1,path:null}}let[x,G]=await Promise.all([je(n),ze(n)]);u.claude&&(u.claude.mcp=x),u.codex&&(u.codex.mcp=G),await ln(t,{recursive:!0}),Et(t).catch(()=>{});let w=new Ae(1),M=new Set,I=new De(n),F=new Oe(n),P=new Ee(n,F,{claudePath:c,onEvent:r=>{for(let l of M)Ot(l,r)}}),j=new Map;w.addListener((r,l)=>{for(let h of M)Ot(h,r)}),w.setProcessor(async r=>{var V,se,C,ne,ce,Se,ue;let l=r._replyPrompt,h=r._replyImagePaths,d=(V=r.provider)!=null?V:p,f;if(r.threadId){let R=await I.getThread(r.threadId);if(R){for(let m=R.messages.length-1;m>=0;m--)if(R.messages[m].sessionId){f=R.messages[m].sessionId;break}}}let b;if(f&&l){let R=(se=await I.getThread(r.threadId))==null?void 0:se.messages.filter(O=>O.role==="human").pop();if(b=(R==null?void 0:R.replyToQuestion)||(R==null?void 0:R.feedbackSummary)||"",h&&h.length>0){b+=`
91
91
 
92
- 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(d)m=xe(r.feedback,r.imagePaths)+`
92
+ The developer attached reference images with their reply:`;for(let O of h)b+=`
93
+ Attached image: use the Read tool to view the image at: ${O}`}b+=`
94
+
95
+ 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(f)b=Ie(r.feedback,r.imagePaths)+`
93
96
 
94
97
  Follow the developer's instructions. If they ask for changes, apply them to the source files.
95
98
 
96
- After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(h!=="codex"?`
99
+ After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(d!=="codex"?`
97
100
 
98
- IMPORTANT: First, use the Read tool to view the updated screenshot at: ${r.screenshotPath}`:"");else{let k=!l&&r.threadId?await S.getThreadHistory(r.threadId):void 0,C=l?null:await b.loadModel();m=l!=null?l:yt(r.screenshotPath,r.feedback,{threadHistory:k&&k.length>0?k:void 0,provider:h,imagePaths:r.imagePaths,designModel:C!=null?C:void 0})}let I=Sn(r.color,`[\u22B9 ${e}:${r.id}]`);console.log(`${I} Reviewing feedback ${r.screenshotPath} (provider: ${h})${r.threadId?` (thread: ${r.threadId})`:""}${d?` (resuming: ${d.slice(0,8)})`:""}`);let $=!!r._isPlanExecutor,x="",R=0,w=(k,C)=>{if(y.broadcast(k,C),$&&k.type==="delta"&&"text"in k){x+=k.text;let M=It(x);if(M.length>R){let N=M.slice(R);R=M.length,y.broadcast({type:"task_resolved",jobId:C,planId:r.planId,resolutions:N,threadId:r.threadId},C)}}},G=(se=r._allowedTools)!=null?se:i,{process:J,result:_}=h==="codex"?st(r.id,{prompt:m,projectRoot:n,screenshotPath:r.screenshotPath,resumeSessionId:d,model:r.model,onEvent:w}):Re(r.id,{prompt:m,projectRoot:n,maxTurns:s,maxBudgetUsd:a,allowedTools:G,claudePath:c,resumeSessionId:d,model:r.model,onEvent:w});y.setActiveProcess(r.id,J);let f=await _;if(r.result=f.text,f.success){console.log(`${I} Iteration complete`),f.fileEdits&&f.fileEdits.length>0&&console.log(`${I} Captured ${f.fileEdits.length} file edit(s): ${f.fileEdits.map(O=>`${O.tool} ${O.file_path}`).join(", ")}`),r.status="done";let k=wt(f.text),C=xt(f.text);if(C.length>0&&r.annotationIds&&r.annotationIds.length>0){let O=new Set(r.annotationIds);C.every(ie=>O.has(ie.annotationId))||(C=C.map((ie,ue)=>oe(V({},ie),{annotationId:r.annotationIds[ue%r.annotationIds.length]})))}let M=f.fileEdits&&f.fileEdits.length>0?f.fileEdits.map(O=>`${O.tool} ${O.file_path.split("/").pop()}`):void 0;if(r.threadId&&await S.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,responseText:f.text,resolutions:C.length>0?C:void 0,question:k!=null?k:void 0,sessionId:f.sessionId,toolsUsed:M}),F.captureGitDiff(n).then(async O=>{var Ve;let U=Date.now(),ie=r.imagePaths?Object.values(r.imagePaths).flat():[],ue=[];if(r.imagePaths)for(let[Et,Ct]of Object.entries(r.imagePaths))for(let Je=0;Je<Ct.length;Je++)ue.push(`screenshots/p-${r.id}-${Et}-${Je}.png`);await F.persist({version:1,id:r.id,createdAt:r.createdAt,completedAt:U,durationMs:U-r.createdAt,url:r.feedback.url,viewport:r.feedback.viewport,screenshotPath:`screenshots/s-${r.id}.png`,pastedImagePaths:ue,annotations:r.feedback.annotations,styleModifications:r.feedback.styleModifications,inspectedElement:r.feedback.inspectedElement,provider:r.provider,model:r.model,sessionId:f.sessionId,threadId:r.threadId,planId:r.planId,planTaskId:r.planTaskId,responseText:f.text,resolutions:C.length>0?C:[],question:k!=null?k:void 0,fileEdits:(Ve=f.fileEdits)!=null?Ve:[],toolsUsed:M,gitDiff:O},r.screenshotPath,ie)}).catch(()=>{}),C.length>0&&C.some(U=>{var ue;let ie=(ue=U.finalScope)!=null?ue:U.inferredScope;return(ie==null?void 0:ie.breadth)==="pattern"})&&b.run().catch(()=>{}),r.planId&&!r.planTaskId){let O=j.get(r.planId);if(O){let U=Pt(f.text);U&&U.length>0?(O.plan=U,O.status="awaiting_approval",O.plannerThreadId=r.threadId,console.log(`${I} Plan ready: ${U.length} tasks for group ${r.planId}`),y.broadcast({type:"plan_ready",jobId:r.id,planId:r.planId,tasks:U,threadId:r.threadId},r.id)):k||(O.status="error",console.error(`${I} Failed to parse plan from planner response`))}}if(r.planId&&r._isReview){let O=j.get(r.planId);if(O){let U=Rt(f.text);U&&(O.status=U.verdict==="pass"?"done":"executing",console.log(`${I} Review verdict: ${U.verdict} \u2014 ${U.summary}`),y.broadcast({type:"plan_review",planId:r.planId,verdict:U.verdict,summary:U.summary,issues:U.issues},r.id))}}k&&(console.log(`${I} \u{1F4AC} Question detected: "${k.slice(0,120)}" \u2192 broadcasting to ${T.size} SSE clients (threadId=${(E=r.threadId)!=null?E:r.id}, annotationIds=${(ce=(ne=r.annotationIds)==null?void 0:ne.join(","))!=null?ce:"none"})`),y.broadcast({type:"question",jobId:r.id,threadId:(ve=r.threadId)!=null?ve:r.id,question:k,annotationIds:r.annotationIds},r.id));let N=kt(f.text);N.length>0&&(console.log(`${I} Novel pattern(s): ${N.map(O=>`${O.category}/${O.element}`).join(", ")}`),y.broadcast({type:"novel_patterns",jobId:r.id,patterns:N,threadId:r.threadId},r.id)),y.broadcast({type:"done",jobId:r.id,success:!0,resolutions:C.length>0?C:void 0,responseText:f.text,threadId:r.threadId},r.id)}else console.error(`${I} Error: ${f.error}`),r.status="error",r.error=f.error,y.broadcast({type:"error",jobId:r.id,message:f.error||"Unknown error"},r.id)});let K=pn(async(r,l)=>{if(vn(r,l),r.method==="OPTIONS"){l.writeHead(204),l.end();return}let d=new URL(r.url||"/",`http://127.0.0.1:${e}`).pathname;try{if(r.method==="POST"&&d==="/send")await ee(r,l);else if(r.method==="GET"&&d==="/events")B(r,l);else if(r.method==="GET"&&d==="/status")te(l);else if(r.method==="GET"&&d==="/capabilities")g(l,200,{providers:u});else if(r.method==="POST"&&d==="/mcp/install")await ke(r,l);else if(r.method==="POST"&&d==="/reply")await q(r,l);else if(r.method==="POST"&&d==="/cancel")A(r,l);else if(r.method==="POST"&&d==="/materialize")await z(l);else if(r.method==="POST"&&d==="/plan")await P(r,l);else if(r.method==="POST"&&d==="/plan/approve")await Y(r,l);else if(r.method==="POST"&&d==="/plan/execute")await L(r,l);else if(r.method==="POST"&&d==="/plan/review")await Z(r,l);else if(r.method==="GET"&&d.startsWith("/plan/"))Ie(d.slice(6),l);else if(r.method==="POST"&&d==="/model/component")await be(r,l);else if(r.method==="DELETE"&&d==="/model/component")await Pe(r,l);else if(r.method==="PATCH"&&d==="/model/token")await Te(r,l);else if(r.method==="DELETE"&&d==="/model/token")await Q(r,l);else if(r.method==="GET"&&d==="/model"){let m=await b.loadModel();g(l,200,{model:m})}else if(r.method==="GET"&&d.startsWith("/thread/")){let m=d.slice(8);await Ae(m,l)}else g(l,404,{error:"Not found"})}catch(m){console.error("[Bridge] Request error:",m),g(l,500,{error:m instanceof Error?m.message:"Internal error"})}});async function ee(r,l){let{screenshot:h,feedback:d,color:m,provider:I,model:$,pastedImages:x}=await ge(r),R;try{R=JSON.parse(d)}catch(E){g(l,400,{error:"Invalid feedback JSON"});return}let w=he().slice(0,8),G=de(t,`screenshot-${w}.png`);await ye(G,h);let J={};if(x.length>0)for(let E of x){let ne=de(t,`pasted-${w}-${E.annotationId}-${E.index}.png`);await ye(ne,E.data),J[E.annotationId]||(J[E.annotationId]=[]),J[E.annotationId].push(ne)}let _=R.annotations.map(E=>E.linkedSelector).filter(E=>!!E),f;if(_.length>0){let E=await S.findContinuationThread(_);E?(f=E.id,await S.addElementIdentifiers(f,_)):f=(await S.createThread(w,_)).id}let H=R.annotations.map(E=>E.id),X=V({id:w,status:"queued",screenshotPath:G,feedback:R,createdAt:Date.now(),color:m,threadId:f,annotationIds:H,provider:I==="claude"||I==="codex"?I:void 0,model:$||void 0},Object.keys(J).length>0?{imagePaths:J}:{});if(f){let E=R.annotations.map(ce=>ce.instruction||`[${ce.type}]`).join("; "),ne=xe(R,Object.keys(J).length>0?J:void 0);await S.appendMessage(f,{role:"human",timestamp:Date.now(),jobId:w,screenshotPath:G,annotationIds:H,feedbackSummary:E,feedbackContext:ne||void 0})}let se=y.enqueue(X);g(l,200,{jobId:w,position:se,threadId:f})}async function q(r,l){let h=r.headers["content-type"]||"",d,m,I,$,x,R=[];if(h.includes("multipart/form-data")){let M=await ge(r),N=M.feedback?JSON.parse(M.feedback):{};d=N.threadId,m=N.reply,I=N.color,$=N.provider,x=N.model;for(let O of M.pastedImages)R.push(O.data)}else{let M=[];try{for(var ce=le(r),ve,k,C;ve=!(k=await ce.next()).done;ve=!1){let U=k.value;M.push(typeof U=="string"?Buffer.from(U):U)}}catch(k){C=[k]}finally{try{ve&&(k=ce.return)&&await k.call(ce)}finally{if(C)throw C[0]}}let N=Buffer.concat(M).toString("utf-8"),O;try{O=JSON.parse(N)}catch(U){g(l,400,{error:"Invalid JSON"});return}d=O.threadId,m=O.reply,I=O.color,$=O.provider,x=O.model}if(!d||!m){g(l,400,{error:"Missing threadId or reply"});return}if(!await S.getThread(d)){g(l,404,{error:"Thread not found"});return}let G=he().slice(0,8),J=[];for(let M=0;M<R.length;M++){let N=de(t,`reply-${G}-${M}.png`);await ye(N,R[M]),J.push(N)}let _="";{let M=await S.getThreadHistory(d);for(let N=M.length-1;N>=0;N--)if(M[N].screenshotPath){_=M[N].screenshotPath;break}}if(!_){g(l,400,{error:"No screenshot available"});return}await S.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:G,replyToQuestion:m,screenshotPath:_});let f=await S.getThreadHistory(d),H=[];for(let M of f)if(M.annotationIds)for(let N of M.annotationIds)H.includes(N)||H.push(N);let X=$==="claude"||$==="codex"?$:void 0,se=vt(_,f,X,J.length>0?J:void 0),E={id:G,status:"queued",screenshotPath:_,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:I,threadId:d,annotationIds:H.length>0?H:void 0,provider:X,model:x||void 0};E._replyPrompt=se;let ne=y.enqueue(E);g(l,200,{jobId:G,position:ne,threadId:d})}function B(r,l){l.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),l.write(`event: connected
101
+ IMPORTANT: First, use the Read tool to view the updated screenshot at: ${r.screenshotPath}`:"");else{let R=!l&&r.threadId?await I.getThreadHistory(r.threadId):void 0,m=l?null:await P.loadModel();b=l!=null?l:wt(r.screenshotPath,r.feedback,{threadHistory:R&&R.length>0?R:void 0,provider:d,imagePaths:r.imagePaths,designModel:m!=null?m:void 0})}let S=xn(r.color,`[\u22B9 ${e}:${r.id}]`);console.log(`${S} Reviewing feedback ${r.screenshotPath} (provider: ${d})${r.threadId?` (thread: ${r.threadId})`:""}${f?` (resuming: ${f.slice(0,8)})`:""}`);let k=!!r._isPlanExecutor,E="",v=0,U=(R,m)=>{if(w.broadcast(R,m),k&&R.type==="delta"&&"text"in R){E+=R.text;let O=kt(E);if(O.length>v){let Z=O.slice(v);v=O.length,w.broadcast({type:"task_resolved",jobId:m,planId:r.planId,resolutions:Z,threadId:r.threadId},m)}}},A=(C=r._allowedTools)!=null?C:i,{process:_,result:T}=d==="codex"?ot(r.id,{prompt:b,projectRoot:n,screenshotPath:r.screenshotPath,resumeSessionId:f,model:r.model,onEvent:U}):Me(r.id,{prompt:b,projectRoot:n,maxTurns:s,maxBudgetUsd:a,allowedTools:A,claudePath:c,resumeSessionId:f,model:r.model,onEvent:U});w.setActiveProcess(r.id,_);let y=await T;if(r.result=y.text,y.success){console.log(`${S} Iteration complete`),y.fileEdits&&y.fileEdits.length>0&&console.log(`${S} Captured ${y.fileEdits.length} file edit(s): ${y.fileEdits.map(N=>`${N.tool} ${N.file_path}`).join(", ")}`),r.status="done";let R=vt(y.text),m=It(y.text);if(m.length>0&&r.annotationIds&&r.annotationIds.length>0){let N=new Set(r.annotationIds);m.every(ie=>N.has(ie.annotationId))||(m=m.map((ie,pe)=>oe(K({},ie),{annotationId:r.annotationIds[pe%r.annotationIds.length]})))}let O=y.fileEdits&&y.fileEdits.length>0?y.fileEdits.map(N=>`${N.tool} ${N.file_path.split("/").pop()}`):void 0;if(r.threadId&&await I.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,responseText:y.text,resolutions:m.length>0?m:void 0,question:R!=null?R:void 0,sessionId:y.sessionId,toolsUsed:O}),F.captureGitDiff(n).then(async N=>{var Xe;let H=Date.now(),ie=r.imagePaths?Object.values(r.imagePaths).flat():[],pe=[];if(r.imagePaths)for(let[Ct,At]of Object.entries(r.imagePaths))for(let Ne=0;Ne<At.length;Ne++)pe.push(`screenshots/p-${r.id}-${Ct}-${Ne}.png`);await F.persist({version:1,id:r.id,createdAt:r.createdAt,completedAt:H,durationMs:H-r.createdAt,url:r.feedback.url,viewport:r.feedback.viewport,screenshotPath:`screenshots/s-${r.id}.png`,pastedImagePaths:pe,annotations:r.feedback.annotations,styleModifications:r.feedback.styleModifications,inspectedElement:r.feedback.inspectedElement,provider:r.provider,model:r.model,sessionId:y.sessionId,threadId:r.threadId,planId:r.planId,planTaskId:r.planTaskId,responseText:y.text,resolutions:m.length>0?m:[],question:R!=null?R:void 0,fileEdits:(Xe=y.fileEdits)!=null?Xe:[],toolsUsed:O,gitDiff:N},r.screenshotPath,ie)}).catch(()=>{}),m.length>0&&m.some(H=>{var pe;let ie=(pe=H.finalScope)!=null?pe:H.inferredScope;return(ie==null?void 0:ie.breadth)==="pattern"})&&P.run().catch(()=>{}),r.planId&&!r.planTaskId){let N=j.get(r.planId);if(N){let H=Tt(y.text);H&&H.length>0?(N.plan=H,N.status="awaiting_approval",N.plannerThreadId=r.threadId,console.log(`${S} Plan ready: ${H.length} tasks for group ${r.planId}`),w.broadcast({type:"plan_ready",jobId:r.id,planId:r.planId,tasks:H,threadId:r.threadId},r.id)):R||(N.status="error",console.error(`${S} Failed to parse plan from planner response`))}}if(r.planId&&r._isReview){let N=j.get(r.planId);if(N){let H=Mt(y.text);H&&(N.status=H.verdict==="pass"?"done":"executing",console.log(`${S} Review verdict: ${H.verdict} \u2014 ${H.summary}`),w.broadcast({type:"plan_review",planId:r.planId,verdict:H.verdict,summary:H.summary,issues:H.issues},r.id))}}R&&(console.log(`${S} \u{1F4AC} Question detected: "${R.slice(0,120)}" \u2192 broadcasting to ${M.size} SSE clients (threadId=${(ne=r.threadId)!=null?ne:r.id}, annotationIds=${(Se=(ce=r.annotationIds)==null?void 0:ce.join(","))!=null?Se:"none"})`),w.broadcast({type:"question",jobId:r.id,threadId:(ue=r.threadId)!=null?ue:r.id,question:R,annotationIds:r.annotationIds},r.id));let Z=bt(y.text);Z.length>0&&(console.log(`${S} Novel pattern(s): ${Z.map(N=>`${N.category}/${N.element}`).join(", ")}`),w.broadcast({type:"novel_patterns",jobId:r.id,patterns:Z,threadId:r.threadId},r.id)),w.broadcast({type:"done",jobId:r.id,success:!0,resolutions:m.length>0?m:void 0,responseText:y.text,threadId:r.threadId},r.id)}else console.error(`${S} Error: ${y.error}`),r.status="error",r.error=y.error,w.broadcast({type:"error",jobId:r.id,message:y.error||"Unknown error"},r.id)});let Q=hn(async(r,l)=>{if(Sn(r,l),r.method==="OPTIONS"){l.writeHead(204),l.end();return}let d=new URL(r.url||"/",`http://127.0.0.1:${e}`).pathname;try{if(r.method==="POST"&&d==="/send")await ee(r,l);else if(r.method==="GET"&&d==="/events")B(r,l);else if(r.method==="GET"&&d==="/status")te(l);else if(r.method==="GET"&&d==="/capabilities")g(l,200,{providers:u});else if(r.method==="POST"&&d==="/mcp/install")await be(r,l);else if(r.method==="POST"&&d==="/reply")await W(r,l);else if(r.method==="POST"&&d==="/cancel")J(r,l);else if(r.method==="POST"&&d==="/materialize")await z(l);else if(r.method==="POST"&&d==="/plan")await $(r,l);else if(r.method==="POST"&&d==="/plan/approve")await q(r,l);else if(r.method==="POST"&&d==="/plan/execute")await L(r,l);else if(r.method==="POST"&&d==="/plan/review")await X(r,l);else if(r.method==="GET"&&d.startsWith("/plan/"))ke(d.slice(6),l);else if(r.method==="POST"&&d==="/model/component")await Pe(r,l);else if(r.method==="DELETE"&&d==="/model/component")await Te(r,l);else if(r.method==="PATCH"&&d==="/model/token")await $e(r,l);else if(r.method==="DELETE"&&d==="/model/token")await Y(r,l);else if(r.method==="GET"&&d==="/model"){let f=await P.loadModel();g(l,200,{model:f})}else if(r.method==="GET"&&d.startsWith("/thread/")){let f=d.slice(8);await Je(f,l)}else g(l,404,{error:"Not found"})}catch(f){console.error("[Bridge] Request error:",f),g(l,500,{error:f instanceof Error?f.message:"Internal error"})}});async function ee(r,l){let{screenshot:h,feedback:d,color:f,provider:b,model:S,pastedImages:k}=await ye(r),E;try{E=JSON.parse(d)}catch(C){g(l,400,{error:"Invalid feedback JSON"});return}let v=fe().slice(0,8),U=de(t,`screenshot-${v}.png`);await we(U,h);let A={};if(k.length>0)for(let C of k){let ne=de(t,`pasted-${v}-${C.annotationId}-${C.index}.png`);await we(ne,C.data),A[C.annotationId]||(A[C.annotationId]=[]),A[C.annotationId].push(ne)}let _=E.annotations.map(C=>C.linkedSelector).filter(C=>!!C),T;if(_.length>0){let C=await I.findContinuationThread(_);C?(T=C.id,await I.addElementIdentifiers(T,_)):T=(await I.createThread(v,_)).id}let y=E.annotations.map(C=>C.id),V=K({id:v,status:"queued",screenshotPath:U,feedback:E,createdAt:Date.now(),color:f,threadId:T,annotationIds:y,provider:b==="claude"||b==="codex"?b:void 0,model:S||void 0},Object.keys(A).length>0?{imagePaths:A}:{});if(T){let C=E.annotations.map(ce=>ce.instruction||`[${ce.type}]`).join("; "),ne=Ie(E,Object.keys(A).length>0?A:void 0);await I.appendMessage(T,{role:"human",timestamp:Date.now(),jobId:v,screenshotPath:U,annotationIds:y,feedbackSummary:C,feedbackContext:ne||void 0})}let se=w.enqueue(V);g(l,200,{jobId:v,position:se,threadId:T})}async function W(r,l){let h=r.headers["content-type"]||"",d,f,b,S,k,E=[];if(h.includes("multipart/form-data")){let m=await ye(r),O=m.feedback?JSON.parse(m.feedback):{};d=O.threadId,f=O.reply,b=O.color,S=O.provider,k=O.model;for(let Z of m.pastedImages)E.push(Z.data)}else{let m=[];try{for(var ce=le(r),Se,ue,R;Se=!(ue=await ce.next()).done;Se=!1){let N=ue.value;m.push(typeof N=="string"?Buffer.from(N):N)}}catch(ue){R=[ue]}finally{try{Se&&(ue=ce.return)&&await ue.call(ce)}finally{if(R)throw R[0]}}let O=Buffer.concat(m).toString("utf-8"),Z;try{Z=JSON.parse(O)}catch(N){g(l,400,{error:"Invalid JSON"});return}d=Z.threadId,f=Z.reply,b=Z.color,S=Z.provider,k=Z.model}if(!d||!f){g(l,400,{error:"Missing threadId or reply"});return}if(!await I.getThread(d)){g(l,404,{error:"Thread not found"});return}let U=fe().slice(0,8),A=[];for(let m=0;m<E.length;m++){let O=de(t,`reply-${U}-${m}.png`);await we(O,E[m]),A.push(O)}let _="";{let m=await I.getThreadHistory(d);for(let O=m.length-1;O>=0;O--)if(m[O].screenshotPath){_=m[O].screenshotPath;break}}if(!_){g(l,400,{error:"No screenshot available"});return}await I.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:U,replyToQuestion:f,screenshotPath:_});let T=await I.getThreadHistory(d),y=[];for(let m of T)if(m.annotationIds)for(let O of m.annotationIds)y.includes(O)||y.push(O);let V=S==="claude"||S==="codex"?S:void 0,se=St(_,T,V,A.length>0?A:void 0),C={id:U,status:"queued",screenshotPath:_,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:b,threadId:d,annotationIds:y.length>0?y:void 0,provider:V,model:k||void 0};C._replyPrompt=se,A.length>0&&(C._replyImagePaths=A);let ne=w.enqueue(C);g(l,200,{jobId:U,position:ne,threadId:d})}function B(r,l){l.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),l.write(`event: connected
99
102
  data: {"status":"connected"}
100
103
 
101
- `);let h={id:he().slice(0,8),res:l};T.add(h),r.on("close",()=>{T.delete(h)})}function te(r){let l=y.allActive;g(r,200,{ok:!0,activeJob:l[0]?{id:l[0].id,status:l[0].status}:null,activeJobs:l.map(h=>({id:h.id,status:h.status})),queueDepth:y.depth})}function A(r,l){let d=new URL(r.url||"/",`http://127.0.0.1:${e}`).searchParams.get("jobId"),m=d?y.cancelJob(d):y.cancelActive();g(l,200,{cancelled:m})}async function z(r){if(b.isRunning){g(r,200,{skipped:!0,reason:"Already running"});return}let l=await b.getUnmaterializedPatternDecisions();if(l.length===0){g(r,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}b.run().catch(()=>{}),g(r,200,{started:!0,decisionCount:l.length,decisionIds:l.map(h=>h.id)})}async function P(r,l){let{screenshot:h,feedback:d,goal:m,pageUrl:I,viewport:$,provider:x,model:R,manifest:w}=await ge(r);if(!h||!m){g(l,400,{error:"Missing screenshot or goal"});return}let G=I||"",J={width:1440,height:900};try{$&&(J=JSON.parse($))}catch(M){}let _;if(d)try{let M=JSON.parse(d),N=xe(M);N&&(_=N)}catch(M){}let f=he().slice(0,8),H=he().slice(0,8),X=de(t,`screenshot-plan-${f}.png`);await ye(X,h);let E=(await S.createThread(H,[])).id,ne={id:f,goal:m,status:"planning",plannerJobId:H,plannerThreadId:E,workerJobIds:[],screenshotPath:X,pageUrl:G,viewport:J,createdAt:Date.now()};j.set(f,ne);let ce=bt(X,m,G,J,w,_);await S.appendMessage(E,{role:"human",timestamp:Date.now(),jobId:H,screenshotPath:X,feedbackSummary:`Plan: ${m}`,feedbackContext:`Goal: ${m}
102
- Page: ${G}`});let ve=x==="claude"||x==="codex"?x:p,k={id:H,status:"queued",screenshotPath:X,feedback:{timestamp:new Date().toISOString(),url:G,viewport:J,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:E,provider:ve,model:R||void 0,planId:f};k._replyPrompt=ce,k._allowedTools=["Read"];let C=y.enqueue(k);g(l,200,{planId:f,jobId:H,position:C,threadId:E})}async function Y(r,l){let h=[];try{for(var w=le(r),G,J,_;G=!(J=await w.next()).done;G=!1){let f=J.value;h.push(typeof f=="string"?Buffer.from(f):f)}}catch(J){_=[J]}finally{try{G&&(J=w.return)&&await J.call(w)}finally{if(_)throw _[0]}}let d=Buffer.concat(h).toString("utf-8"),m;try{m=JSON.parse(d)}catch(f){g(l,400,{error:"Invalid JSON"});return}let{planId:I,approvedTaskIds:$}=m;if(!I){g(l,400,{error:"Missing planId"});return}let x=j.get(I);if(!x){g(l,404,{error:"Plan not found"});return}if(!x.plan){g(l,400,{error:"Plan has no tasks"});return}let R=$?x.plan.filter(f=>$.includes(f.id)):x.plan;x.status="executing",g(l,200,{planId:I,tasks:R,status:"executing"})}async function L(r,l){let{screenshot:h,planId:d,tasks:m,provider:I,model:$}=await ge(r);if(!d||!m||!h){g(l,400,{error:"Missing planId, tasks, or screenshot"});return}let x=j.get(d);if(!x){g(l,404,{error:"Plan not found"});return}if(x.status!=="executing"){g(l,400,{error:`Plan status is ${x.status}, expected executing`});return}let R;try{R=JSON.parse(m)}catch(se){g(l,400,{error:"Invalid tasks JSON"});return}let w=he().slice(0,8),G=de(t,`screenshot-exec-${d}.png`);await ye(G,h);let J=I==="claude"||I==="codex"?I:p,_=$t(G,R,x.pageUrl,x.viewport,J),f=R.map(se=>se.annotationId),H={id:w,status:"queued",screenshotPath:G,feedback:{timestamp:new Date().toISOString(),url:x.pageUrl,viewport:x.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:J,model:$||void 0,planId:d,annotationIds:f};H._replyPrompt=_,H._isPlanExecutor=!0,x.executorJobId=w;let X=y.enqueue(H);g(l,200,{jobId:w,planId:d,position:X})}async function Z(r,l){let{screenshot:h,planId:d,provider:m,model:I}=await ge(r);if(!d){g(l,400,{error:"Missing planId"});return}let $=j.get(d);if(!$){g(l,404,{error:"Plan not found"});return}$.status="reviewing";let x=he().slice(0,8),R=$.screenshotPath;h&&(R=de(t,`screenshot-review-${d}.png`),await ye(R,h));let w=($.plan||[]).map(H=>({id:H.id,instruction:H.instruction,summary:"completed"})),G=Tt(R,$.goal,w),J=m==="claude"||m==="codex"?m:p,_={id:x,status:"queued",screenshotPath:R,feedback:{timestamp:new Date().toISOString(),url:$.pageUrl,viewport:$.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:J,model:I||void 0,planId:d};_._replyPrompt=G,_._isReview=!0,_._allowedTools=["Read"];let f=y.enqueue(_);g(l,200,{jobId:x,planId:d,position:f})}function Ie(r,l){let h=j.get(r);if(!h){g(l,404,{error:"Plan not found"});return}g(l,200,h)}async function ke(r,l){var J,_;let h=[];try{for(var x=le(r),R,w,G;R=!(w=await x.next()).done;R=!1){let f=w.value;h.push(typeof f=="string"?Buffer.from(f):f)}}catch(w){G=[w]}finally{try{R&&(w=x.return)&&await w.call(x)}finally{if(G)throw G[0]}}let d;if(h.length>0)try{d=JSON.parse(Buffer.concat(h).toString("utf-8")).serverUrl}catch(f){}let m=[];(J=u.claude)!=null&&J.available&&u.claude.mcp&&!u.claude.mcp.found&&m.push(await mt(d)),(_=u.codex)!=null&&_.available&&u.codex.mcp&&!u.codex.mcp.found&&m.push(await gt(d));let[I,$]=await Promise.all([Fe(n),je(n)]);u.claude&&(u.claude.mcp=I),u.codex&&(u.codex.mcp=$),g(l,200,{results:m,capabilities:{providers:u}})}async function be(r,l){let h=[];try{for(var I=le(r),$,x,R;$=!(x=await I.next()).done;$=!1){let w=x.value;h.push(typeof w=="string"?Buffer.from(w):w)}}catch(x){R=[x]}finally{try{$&&(x=I.return)&&await x.call(I)}finally{if(R)throw R[0]}}let d;try{d=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(w){g(l,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){g(l,400,{error:"Missing or invalid name"});return}let m=await b.addComponent(d.name);g(l,200,m)}async function Pe(r,l){let h=[];try{for(var I=le(r),$,x,R;$=!(x=await I.next()).done;$=!1){let w=x.value;h.push(typeof w=="string"?Buffer.from(w):w)}}catch(x){R=[x]}finally{try{$&&(x=I.return)&&await x.call(I)}finally{if(R)throw R[0]}}let d;try{d=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(w){g(l,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){g(l,400,{error:"Missing or invalid name"});return}let m=await b.removeComponent(d.name);g(l,200,m)}async function Te(r,l){let h=[];try{for(var I=le(r),$,x,R;$=!(x=await I.next()).done;$=!1){let w=x.value;h.push(typeof w=="string"?Buffer.from(w):w)}}catch(x){R=[x]}finally{try{$&&(x=I.return)&&await x.call(I)}finally{if(R)throw R[0]}}let d;try{d=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(w){g(l,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){g(l,400,{error:"Missing or invalid path/value"});return}let m=await b.updateToken(d.path,d.value);g(l,200,m)}async function Q(r,l){let h=[];try{for(var I=le(r),$,x,R;$=!(x=await I.next()).done;$=!1){let w=x.value;h.push(typeof w=="string"?Buffer.from(w):w)}}catch(x){R=[x]}finally{try{$&&(x=I.return)&&await x.call(I)}finally{if(R)throw R[0]}}let d;try{d=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(w){g(l,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){g(l,400,{error:"Missing or invalid path"});return}let m=await b.removeToken(d.path);g(l,200,m)}async function Ae(r,l){let h=await S.getThread(r);if(!h){g(l,404,{error:"Thread not found"});return}let d=h.messages.map($=>{var x=$,{screenshotPath:m}=x,I=nt(x,["screenshotPath"]);return I});g(l,200,{id:h.id,createdAt:h.createdAt,messages:d})}let D=setInterval(()=>{Ot(t).catch(()=>{})},gn);return new Promise((r,l)=>{K.on("error",h=>{if(h.code==="EADDRINUSE"){console.log(`[\u22B9 already watching :${e}]`),r({port:e,close:async()=>{}});return}l(h)}),K.listen(e,"127.0.0.1",()=>{console.log(`[\u22B9 is watching :${e}]`),r({port:e,close:async()=>{clearInterval(D),y.destroy();for(let h of T)try{h.res.end()}catch(d){}return T.clear(),new Promise(h=>{K.close(()=>h())})}})})})}async function Ot(o){try{let e=await ln(o),n=Date.now();for(let t of e){let s=de(o,t);try{let a=await dn(s);n-a.mtimeMs>yn&&await un(s)}catch(a){}}}catch(e){}}var ae="\x1B[35m[popmelt]\x1B[0m";async function In(){let o=process.argv.slice(2);if(o[0]==="wrap"){let e=o.indexOf("--");(e===-1||e===o.length-1)&&(console.error(`${ae} Usage: popmelt wrap -- <dev command>`),console.error(`${ae} Example: popmelt wrap -- next dev`),console.error(`${ae} Example: popmelt wrap -- astro dev`),process.exit(1));let n=o.slice(e+1);await bn(n);return}if(o[0]==="bridge"){await kn();return}console.log(`${ae} Popmelt \u2014 design collaboration for AI coding agents`),console.log(""),console.log(" popmelt wrap -- <command> Start bridge + dev server together"),console.log(" popmelt bridge Start the bridge server standalone"),console.log(""),console.log("Examples:"),console.log(" popmelt wrap -- next dev"),console.log(" popmelt wrap -- astro dev"),console.log(" popmelt wrap -- vite"),console.log(""),console.log("In package.json:"),console.log(' "scripts": { "dev": "popmelt wrap -- next dev" }')}async function kn(){let o=await Le({projectRoot:process.cwd()});console.log(`${ae} Bridge running on http://localhost:${o.port}`),await new Promise(e=>{let n=async()=>{console.log(`
103
- ${ae} Shutting down bridge...`),await o.close(),e()};process.on("SIGINT",n),process.on("SIGTERM",n)})}async function bn(o){let e=await Le({projectRoot:process.cwd()});console.log(`${ae} Bridge running on http://localhost:${e.port}`);let[n,...t]=o;console.log(`${ae} Starting: ${o.join(" ")}`);let s=xn(n,t,{stdio:"inherit",shell:!0,env:oe(V({},process.env),{POPMELT_BRIDGE_URL:`http://localhost:${e.port}`})}),a=i=>{s.kill(i)};process.on("SIGINT",()=>a("SIGINT")),process.on("SIGTERM",()=>a("SIGTERM")),s.on("exit",async(i,c)=>{await e.close(),c?process.kill(process.pid,c):process.exit(i!=null?i:0)})}In().catch(o=>{console.error(`${ae} Fatal:`,o),process.exit(1)});
104
+ `);let h={id:fe().slice(0,8),res:l};M.add(h),r.on("close",()=>{M.delete(h)})}function te(r){let l=w.allActive;g(r,200,{ok:!0,activeJob:l[0]?{id:l[0].id,status:l[0].status}:null,activeJobs:l.map(h=>({id:h.id,status:h.status})),queueDepth:w.depth})}function J(r,l){let d=new URL(r.url||"/",`http://127.0.0.1:${e}`).searchParams.get("jobId"),f=d?w.cancelJob(d):w.cancelActive();g(l,200,{cancelled:f})}async function z(r){if(P.isRunning){g(r,200,{skipped:!0,reason:"Already running"});return}let l=await P.getUnmaterializedPatternDecisions();if(l.length===0){g(r,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}P.run().catch(()=>{}),g(r,200,{started:!0,decisionCount:l.length,decisionIds:l.map(h=>h.id)})}async function $(r,l){let{screenshot:h,feedback:d,goal:f,pageUrl:b,viewport:S,provider:k,model:E,manifest:v}=await ye(r);if(!h||!f){g(l,400,{error:"Missing screenshot or goal"});return}let U=b||"",A={width:1440,height:900};try{S&&(A=JSON.parse(S))}catch(m){}let _;if(d)try{let m=JSON.parse(d),O=Ie(m);O&&(_=O)}catch(m){}let T=fe().slice(0,8),y=fe().slice(0,8),V=de(t,`screenshot-plan-${T}.png`);await we(V,h);let C=(await I.createThread(y,[])).id,ne={id:T,goal:f,status:"planning",plannerJobId:y,plannerThreadId:C,workerJobIds:[],screenshotPath:V,pageUrl:U,viewport:A,createdAt:Date.now()};j.set(T,ne);let ce=Pt(V,f,U,A,v,_);await I.appendMessage(C,{role:"human",timestamp:Date.now(),jobId:y,screenshotPath:V,feedbackSummary:`Plan: ${f}`,feedbackContext:`Goal: ${f}
105
+ Page: ${U}`});let Se=k==="claude"||k==="codex"?k:p,ue={id:y,status:"queued",screenshotPath:V,feedback:{timestamp:new Date().toISOString(),url:U,viewport:A,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:C,provider:Se,model:E||void 0,planId:T};ue._replyPrompt=ce,ue._allowedTools=["Read"];let R=w.enqueue(ue);g(l,200,{planId:T,jobId:y,position:R,threadId:C})}async function q(r,l){let h=[];try{for(var v=le(r),U,A,_;U=!(A=await v.next()).done;U=!1){let T=A.value;h.push(typeof T=="string"?Buffer.from(T):T)}}catch(A){_=[A]}finally{try{U&&(A=v.return)&&await A.call(v)}finally{if(_)throw _[0]}}let d=Buffer.concat(h).toString("utf-8"),f;try{f=JSON.parse(d)}catch(T){g(l,400,{error:"Invalid JSON"});return}let{planId:b,approvedTaskIds:S}=f;if(!b){g(l,400,{error:"Missing planId"});return}let k=j.get(b);if(!k){g(l,404,{error:"Plan not found"});return}if(!k.plan){g(l,400,{error:"Plan has no tasks"});return}let E=S?k.plan.filter(T=>S.includes(T.id)):k.plan;k.status="executing",g(l,200,{planId:b,tasks:E,status:"executing"})}async function L(r,l){let{screenshot:h,planId:d,tasks:f,provider:b,model:S}=await ye(r);if(!d||!f||!h){g(l,400,{error:"Missing planId, tasks, or screenshot"});return}let k=j.get(d);if(!k){g(l,404,{error:"Plan not found"});return}if(k.status!=="executing"){g(l,400,{error:`Plan status is ${k.status}, expected executing`});return}let E;try{E=JSON.parse(f)}catch(se){g(l,400,{error:"Invalid tasks JSON"});return}let v=fe().slice(0,8),U=de(t,`screenshot-exec-${d}.png`);await we(U,h);let A=b==="claude"||b==="codex"?b:p,_=Rt(U,E,k.pageUrl,k.viewport,A),T=E.map(se=>se.annotationId),y={id:v,status:"queued",screenshotPath:U,feedback:{timestamp:new Date().toISOString(),url:k.pageUrl,viewport:k.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:A,model:S||void 0,planId:d,annotationIds:T};y._replyPrompt=_,y._isPlanExecutor=!0,k.executorJobId=v;let V=w.enqueue(y);g(l,200,{jobId:v,planId:d,position:V})}async function X(r,l){let{screenshot:h,planId:d,provider:f,model:b}=await ye(r);if(!d){g(l,400,{error:"Missing planId"});return}let S=j.get(d);if(!S){g(l,404,{error:"Plan not found"});return}S.status="reviewing";let k=fe().slice(0,8),E=S.screenshotPath;h&&(E=de(t,`screenshot-review-${d}.png`),await we(E,h));let v=(S.plan||[]).map(y=>({id:y.id,instruction:y.instruction,summary:"completed"})),U=$t(E,S.goal,v),A=f==="claude"||f==="codex"?f:p,_={id:k,status:"queued",screenshotPath:E,feedback:{timestamp:new Date().toISOString(),url:S.pageUrl,viewport:S.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:A,model:b||void 0,planId:d};_._replyPrompt=U,_._isReview=!0,_._allowedTools=["Read"];let T=w.enqueue(_);g(l,200,{jobId:k,planId:d,position:T})}function ke(r,l){let h=j.get(r);if(!h){g(l,404,{error:"Plan not found"});return}g(l,200,h)}async function be(r,l){var A,_;let h=[];try{for(var k=le(r),E,v,U;E=!(v=await k.next()).done;E=!1){let T=v.value;h.push(typeof T=="string"?Buffer.from(T):T)}}catch(v){U=[v]}finally{try{E&&(v=k.return)&&await v.call(k)}finally{if(U)throw U[0]}}let d;if(h.length>0)try{d=JSON.parse(Buffer.concat(h).toString("utf-8")).serverUrl}catch(T){}let f=[];(A=u.claude)!=null&&A.available&&u.claude.mcp&&!u.claude.mcp.found&&f.push(await gt(d)),(_=u.codex)!=null&&_.available&&u.codex.mcp&&!u.codex.mcp.found&&f.push(await yt(d));let[b,S]=await Promise.all([je(n),ze(n)]);u.claude&&(u.claude.mcp=b),u.codex&&(u.codex.mcp=S),g(l,200,{results:f,capabilities:{providers:u}})}async function Pe(r,l){let h=[];try{for(var b=le(r),S,k,E;S=!(k=await b.next()).done;S=!1){let v=k.value;h.push(typeof v=="string"?Buffer.from(v):v)}}catch(k){E=[k]}finally{try{S&&(k=b.return)&&await k.call(b)}finally{if(E)throw E[0]}}let d;try{d=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(v){g(l,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){g(l,400,{error:"Missing or invalid name"});return}let f=await P.addComponent(d.name);g(l,200,f)}async function Te(r,l){let h=[];try{for(var b=le(r),S,k,E;S=!(k=await b.next()).done;S=!1){let v=k.value;h.push(typeof v=="string"?Buffer.from(v):v)}}catch(k){E=[k]}finally{try{S&&(k=b.return)&&await k.call(b)}finally{if(E)throw E[0]}}let d;try{d=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(v){g(l,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){g(l,400,{error:"Missing or invalid name"});return}let f=await P.removeComponent(d.name);g(l,200,f)}async function $e(r,l){let h=[];try{for(var b=le(r),S,k,E;S=!(k=await b.next()).done;S=!1){let v=k.value;h.push(typeof v=="string"?Buffer.from(v):v)}}catch(k){E=[k]}finally{try{S&&(k=b.return)&&await k.call(b)}finally{if(E)throw E[0]}}let d;try{d=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(v){g(l,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){g(l,400,{error:"Missing or invalid path/value"});return}let f=await P.updateToken(d.path,d.value);g(l,200,f)}async function Y(r,l){let h=[];try{for(var b=le(r),S,k,E;S=!(k=await b.next()).done;S=!1){let v=k.value;h.push(typeof v=="string"?Buffer.from(v):v)}}catch(k){E=[k]}finally{try{S&&(k=b.return)&&await k.call(b)}finally{if(E)throw E[0]}}let d;try{d=JSON.parse(Buffer.concat(h).toString("utf-8"))}catch(v){g(l,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){g(l,400,{error:"Missing or invalid path"});return}let f=await P.removeToken(d.path);g(l,200,f)}async function Je(r,l){let h=await I.getThread(r);if(!h){g(l,404,{error:"Thread not found"});return}let d=h.messages.map(S=>{var k=S,{screenshotPath:f}=k,b=st(k,["screenshotPath"]);return b});g(l,200,{id:h.id,createdAt:h.createdAt,messages:d})}let D=setInterval(()=>{Et(t).catch(()=>{})},yn);return new Promise((r,l)=>{Q.on("error",h=>{if(h.code==="EADDRINUSE"){console.log(`[\u22B9 already watching :${e}]`),r({port:e,close:async()=>{}});return}l(h)}),Q.listen(e,"127.0.0.1",()=>{console.log(`[\u22B9 is watching :${e}]`),r({port:e,close:async()=>{clearInterval(D),w.destroy();for(let h of M)try{h.res.end()}catch(d){}return M.clear(),new Promise(h=>{Q.close(()=>h())})}})})})}async function Et(o){try{let e=await dn(o),n=Date.now();for(let t of e){let s=de(o,t);try{let a=await un(s);n-a.mtimeMs>wn&&await pn(s)}catch(a){}}}catch(e){}}var ae="\x1B[35m[popmelt]\x1B[0m";async function kn(){let o=process.argv.slice(2);if(o[0]==="wrap"){let e=o.indexOf("--");(e===-1||e===o.length-1)&&(console.error(`${ae} Usage: popmelt wrap -- <dev command>`),console.error(`${ae} Example: popmelt wrap -- next dev`),console.error(`${ae} Example: popmelt wrap -- astro dev`),process.exit(1));let n=o.slice(e+1);await Pn(n);return}if(o[0]==="bridge"){await bn();return}console.log(`${ae} Popmelt \u2014 design collaboration for AI coding agents`),console.log(""),console.log(" popmelt wrap -- <command> Start bridge + dev server together"),console.log(" popmelt bridge Start the bridge server standalone"),console.log(""),console.log("Examples:"),console.log(" popmelt wrap -- next dev"),console.log(" popmelt wrap -- astro dev"),console.log(" popmelt wrap -- vite"),console.log(""),console.log("In package.json:"),console.log(' "scripts": { "dev": "popmelt wrap -- next dev" }')}async function bn(){let o=await Ge({projectRoot:process.cwd()});console.log(`${ae} Bridge running on http://localhost:${o.port}`),await new Promise(e=>{let n=async()=>{console.log(`
106
+ ${ae} Shutting down bridge...`),await o.close(),e()};process.on("SIGINT",n),process.on("SIGTERM",n)})}async function Pn(o){let e=await Ge({projectRoot:process.cwd()});console.log(`${ae} Bridge running on http://localhost:${e.port}`);let[n,...t]=o;console.log(`${ae} Starting: ${o.join(" ")}`);let s=In(n,t,{stdio:"inherit",shell:!0,env:oe(K({},process.env),{POPMELT_BRIDGE_URL:`http://localhost:${e.port}`})}),a=i=>{s.kill(i)};process.on("SIGINT",()=>a("SIGINT")),process.on("SIGTERM",()=>a("SIGTERM")),s.on("exit",async(i,c)=>{await e.close(),c?process.kill(process.pid,c):process.exit(i!=null?i:0)})}kn().catch(o=>{console.error(`${ae} Fatal:`,o),process.exit(1)});