@popmelt.com/core 0.5.9 → 0.5.10

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,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{a as V,b as oe,c as it,d as ue}from"./chunk-4LDJX6BW.mjs";import{spawn as Bn}from"child_process";import{execFileSync as wn}from"child_process";import{createHash as vn,randomUUID as me}from"crypto";import{mkdir as Sn,readFile as In,readdir as xn,stat as Pn,unlink as bn,writeFile as Se}from"fs/promises";import{createServer as kn}from"http";import{tmpdir as Tn}from"os";import{dirname as $n,join as re}from"path";import{fileURLToPath as Rn}from"url";function at(r,n){return`<!DOCTYPE html>
2
+ import{a as V,b as oe,c as it,d as ue}from"./chunk-4LDJX6BW.mjs";import{spawn as Bn}from"child_process";import{execFileSync as wn}from"child_process";import{createHash as vn,randomUUID as me}from"crypto";import{mkdir as Sn,readFile as In,readdir as xn,stat as Pn,unlink as bn,writeFile as Se}from"fs/promises";import{createServer as kn}from"http";import{tmpdir as Tn}from"os";import{dirname as Rn,join as re}from"path";import{fileURLToPath as $n}from"url";function at(r,n){return`<!DOCTYPE html>
3
3
  <html>
4
4
  <head>
5
5
  <meta charset="utf-8">
@@ -26,7 +26,7 @@ import{a as V,b as oe,c as it,d as ue}from"./chunk-4LDJX6BW.mjs";import{spawn as
26
26
  });
27
27
  </script>
28
28
  </body>
29
- </html>`}import{spawn as Yt}from"child_process";import{createInterface as Qt}from"readline";function Oe(r,n){let{prompt:t,projectRoot:e,maxTurns:o=40,maxBudgetUsd:a=1,allowedTools:i=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:l="claude",resumeSessionId:h,model:f,timeoutMs:_=3e5,onEvent:A}=n,C=[];h?C.push("--resume",h,"-p",t):C.push("-p",t),C.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(a)),f&&C.push("--model",f);for(let W of i)C.push("--allowedTools",W);let g=Yt(l,C,{cwd:e,stdio:["ignore","pipe","pipe"],env:oe(V({},process.env),{ANTHROPIC_API_KEY:void 0})}),G=new Promise(W=>{var j;let v,L=[],E=[],Z=!1,O="",X=!1,U=setTimeout(()=>{X=!0,g.kill("SIGTERM"),setTimeout(()=>{try{g.kill("SIGKILL")}catch(b){}},5e3)},_),ne=Qt({input:g.stdout}),P=new Set;ne.on("line",b=>{var K,ge,be,ke,Te,$e,Re,Me,Ce;if(b.trim())try{let q=JSON.parse(b);q.session_id&&!v&&(v=q.session_id);let Be=(ge=q.type)!=null?ge:(K=q.event)!=null&&K.type?`event.${q.event.type}`:"unknown";if(P.add(Be),q.type==="result"&&q.result&&L.length===0){let B=typeof q.result=="string"?q.result:"";B&&(L.push(B),A==null||A({type:"delta",jobId:r,text:B},r))}if(q.type==="assistant"&&Array.isArray((be=q.message)==null?void 0:be.content))for(let B of q.message.content){if(B.type==="text"&&B.text&&(L.push(B.text),A==null||A({type:"delta",jobId:r,text:B.text},r)),B.type==="tool_use"&&B.name){let Ee=((ke=B.input)==null?void 0:ke.file_path)||((Te=B.input)==null?void 0:Te.path)||void 0;A==null||A(V({type:"tool_use",jobId:r,tool:B.name},Ee?{file:Ee}:{}),r),B.name==="Edit"&&(($e=B.input)!=null&&$e.file_path)?E.push({tool:"Edit",file_path:B.input.file_path,old_string:B.input.old_string,new_string:B.input.new_string,replace_all:B.input.replace_all}):B.name==="Write"&&((Re=B.input)!=null&&Re.file_path)&&E.push({tool:"Write",file_path:B.input.file_path,content:B.input.content})}B.type==="thinking"&&B.thinking&&(A==null||A({type:"thinking",jobId:r,text:B.thinking},r))}q.type==="user"&&((Ce=(Me=q.tool_use_result)==null?void 0:Me.file)!=null&&Ce.filePath)&&(A==null||A({type:"tool_use",jobId:r,tool:"Read",file:q.tool_use_result.file.filePath},r))}catch(q){}});let x=[];(j=g.stderr)==null||j.on("data",b=>{x.push(b.toString())}),g.on("close",b=>{clearTimeout(U),ne.close(),L.length===0&&P.size>0&&console.warn(`[Claude:${r}] No text captured. Event types seen: ${[...P].join(", ")}`),X?(Z=!0,O=`Timed out after ${Math.round(_/6e4)} minutes`):b!==0&&b!==null&&(Z=!0,O=x.join("")||`Claude process exited with code ${b}`),W({sessionId:v,text:L.join(""),success:!Z,error:Z?O:void 0,fileEdits:E.length>0?E:void 0})}),g.on("error",b=>{clearTimeout(U),Z=!0,O=b.message,W({sessionId:v,text:L.join(""),success:!1,error:O,fileEdits:E.length>0?E:void 0})})});return{process:g,result:G}}import{spawn as qt}from"child_process";import{createInterface as Kt}from"readline";function ct(r,n){let{prompt:t,projectRoot:e,screenshotPath:o,resumeSessionId:a,model:i,onEvent:l}=n,h=[];a?(h.push("exec","resume",a),i&&h.push("-m",i),h.push("--json","--full-auto",t),o&&h.push("--image",o)):(h.push("exec","--json","--full-auto"),i&&h.push("-m",i),h.push(t),o&&h.push("--image",o));let f=qt("codex",h,{cwd:e,stdio:["ignore","pipe","pipe"],env:V({},process.env)}),_=new Promise(A=>{var Z;let C,g=[],G=!1,W="",v=Kt({input:f.stdout}),L=new Set;v.on("line",O=>{var X,U,ne,P;if(O.trim())try{let x=JSON.parse(O),j=(X=x.type)!=null?X:"unknown";if(L.add(j),j==="thread.started"&&x.thread_id&&!C&&(C=x.thread_id),(j==="item.agentMessage.delta"||j==="item/agentMessage/delta")&&((U=x.delta)!=null&&U.text)&&(g.push(x.delta.text),l==null||l({type:"delta",jobId:r,text:x.delta.text},r)),(j==="item.reasoning.delta"||j==="item/reasoning/delta")&&((ne=x.delta)!=null&&ne.text)&&(l==null||l({type:"thinking",jobId:r,text:x.delta.text},r)),(j==="item.started"||j==="item/started")&&x.item){let b=x.item.type;if(b==="command_execution")l==null||l({type:"tool_use",jobId:r,tool:"Bash"},r);else if(b==="file_change"){let K=x.item.filename||x.item.path;l==null||l(V({type:"tool_use",jobId:r,tool:"Edit"},K?{file:K}:{}),r)}else if(b==="file_read"){let K=x.item.filename||x.item.path;l==null||l(V({type:"tool_use",jobId:r,tool:"Read"},K?{file:K}:{}),r)}else if(b==="web_search")l==null||l({type:"tool_use",jobId:r,tool:"WebSearch"},r);else if(b==="mcp_tool_call"){let K=x.item.tool_name||x.item.name||"MCP";l==null||l({type:"tool_use",jobId:r,tool:K},r)}}if((j==="item.completed"||j==="item/completed")&&x.item){if(x.item.type==="agent_message"){let b=x.item.text;typeof b=="string"&&b&&(g.push(b),l==null||l({type:"delta",jobId:r,text:b},r))}else if(x.item.type==="reasoning"){let b=x.item.text;typeof b=="string"&&b&&(l==null||l({type:"thinking",jobId:r,text:b},r))}}j==="turn.failed"&&(G=!0,W=((P=x.error)==null?void 0:P.message)||x.message||"Turn failed")}catch(x){}});let E=[];(Z=f.stderr)==null||Z.on("data",O=>{E.push(O.toString())}),f.on("close",O=>{v.close(),g.length===0&&L.size>0&&console.warn(`[Codex:${r}] No text captured. Event types seen: ${[...L].join(", ")}`),O!==0&&O!==null&&(G=!0,W=E.join("")||`Codex process exited with code ${O}`),A({sessionId:C,text:g.join(""),success:!G,error:G?W:void 0})}),f.on("error",O=>{G=!0,W=O.message,A({sessionId:C,text:g.join(""),success:!1,error:W})})});return{process:f,result:_}}import{execFile as Xt}from"child_process";import{copyFile as lt,mkdir as dt,readdir as Vt,readFile as Zt,writeFile as en}from"fs/promises";import{join as fe}from"path";var Ae=class{constructor(n){this.projectRoot=n;let t=fe(n,".popmelt");this.decisionsDir=fe(t,"decisions"),this.screenshotsDir=fe(t,"screenshots")}async persist(n,t,e){try{await dt(this.decisionsDir,{recursive:!0}),await dt(this.screenshotsDir,{recursive:!0});try{await lt(t,fe(this.screenshotsDir,`s-${n.id}.png`))}catch(o){}for(let o=0;o<e.length;o++)try{let a=n.pastedImagePaths[o];a&&await lt(e[o],fe(this.screenshotsDir,a.replace("screenshots/","")))}catch(a){}await en(fe(this.decisionsDir,`d-${n.id}.json`),JSON.stringify(n,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await Vt(this.decisionsDir)).filter(t=>t.startsWith("d-")&&t.endsWith(".json")).map(t=>t.slice(2,-5))}catch(n){return[]}}async loadDecision(n){try{let t=await Zt(fe(this.decisionsDir,`d-${n}.json`),"utf-8");return JSON.parse(t)}catch(t){return null}}async loadDecisions(n){return(await Promise.all(n.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(n){return new Promise(t=>{Xt("git",["diff","HEAD"],{cwd:n,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){t(null);return}t(o||null)})})}};import{readFile as ut,writeFile as ye}from"fs/promises";import{join as Fe}from"path";var ae="[Materializer]",tn={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null},De=class{constructor(n,t,e={}){this.projectRoot=n;this.decisionStore=t;this.options=e;this.cachedIndex=null;this.running=!1;let o=Fe(n,".popmelt");this.indexPath=Fe(o,"materialized.json"),this.modelPath=Fe(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let n=await ut(this.modelPath,"utf-8");return JSON.parse(n)}catch(n){return null}}async addComponent(n){let t=await this.loadModel();t||(t={tokens:{},components:{},rules:[]}),(!t.components||typeof t.components!="object")&&(t.components={});let e=t.components;return e[n]?{added:!1,alreadyExists:!0}:(e[n]={description:""},await ye(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ae} Added component "${n}" to model`),{added:!0,alreadyExists:!1})}async updateToken(n,t){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=n.split("."),a=e;for(let h=0;h<o.length-1;h++){let f=o[h];(!a[f]||typeof a[f]!="object")&&(a[f]={}),a=a[f]}let i=o[o.length-1],l;try{l=JSON.parse(t)}catch(h){l=null}if(l&&typeof l=="object"&&l!==null&&"value"in l)a[i]=l;else{let h=a[i];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=t:a[i]=t}return await ye(this.modelPath,JSON.stringify(e,null,2)),console.log(`${ae} Updated token "${n}" \u2192 "${t.slice(0,80)}"`),{updated:!0}}async removeToken(n){let t=await this.loadModel();if(!t)return{removed:!1};let e=n.split("."),o=t;for(let i=0;i<e.length-1;i++){let l=e[i];if(!o[l]||typeof o[l]!="object")return{removed:!1};o=o[l]}let a=e[e.length-1];return a in o?(delete o[a],await ye(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ae} Removed token "${n}" from model`),{removed:!0}):{removed:!1}}async removeComponent(n){let t=await this.loadModel();if(!t)return{removed:!1};let e=t.components;return!e||!e[n]?{removed:!1}:(delete e[n],await ye(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ae} Removed component "${n}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let n=await this.loadIndex(),t=new Set(n.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(i=>!t.has(i));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(i=>i.resolutions.some(l=>{var f;let h=(f=l.finalScope)!=null?f:l.inferredScope;return(h==null?void 0:h.breadth)==="pattern"}))}async run(){var n,t,e,o,a,i,l;if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let h=await this.getUnmaterializedPatternDecisions();if(h.length===0)return{processedIds:[],success:!0};let f=h.map(v=>v.id);console.log(`${ae} Processing ${f.length} pattern-scoped decision(s): ${f.join(", ")}`),(t=(n=this.options).onEvent)==null||t.call(n,{type:"materialize_started",decisionIds:f});let _=await this.loadModel(),A=sn(h,_),C=!0,g;try{let{result:v}=Oe(`mat-${Date.now()}`,{prompt:A,projectRoot:this.projectRoot,maxTurns:(e=this.options.maxTurns)!=null?e:5,maxBudgetUsd:(o=this.options.maxBudgetUsd)!=null?o:.5,allowedTools:["Read"],claudePath:(a=this.options.claudePath)!=null?a:"claude"}),L=await v;if(!L.success)C=!1,g=L.error,console.error(`${ae} Claude spawn error:`,g);else{let E=nn(L.text);E?(await ye(this.modelPath,JSON.stringify(E,null,2)),console.log(`${ae} Successfully materialized ${f.length} decision(s) \u2192 ${this.modelPath}`)):(C=!1,g="No <model> block found in response",console.error(`${ae} ${g}`))}}catch(v){C=!1,g=v instanceof Error?v.message:String(v),console.error(`${ae} Error:`,g)}let G=await this.loadIndex(),W=new Set(G.materializedIds);for(let v of f)W.add(v);return G.materializedIds=[...W],G.lastRunAt=Date.now(),G.lastRunDecisionIds=f,G.lastRunError=g!=null?g:null,await this.persistIndex(G),(l=(i=this.options).onEvent)==null||l.call(i,{type:"materialize_done",decisionIds:f,success:C,error:g}),{processedIds:f,success:C,error:g}}finally{this.running=!1}}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let n=await ut(this.indexPath,"utf-8"),t=JSON.parse(n);return this.cachedIndex=t,t}catch(n){return this.cachedIndex=oe(V({},tn),{materializedIds:[],lastRunDecisionIds:[]}),this.cachedIndex}}async persistIndex(n){this.cachedIndex=n;try{await ye(this.indexPath,JSON.stringify(n,null,2))}catch(t){console.error(`${ae} Failed to write index:`,t)}}};function nn(r){let n=r.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return typeof t!="object"||t===null||Array.isArray(t)?null:t}catch(t){return null}}function sn(r,n){let t=r.map(o=>{let i=o.resolutions.filter(f=>{var A;let _=(A=f.finalScope)!=null?A:f.inferredScope;return(_==null?void 0:_.breadth)==="pattern"}).map(f=>{var g,G,W,v;let _=(g=f.finalScope)!=null?g:f.inferredScope,A=(G=_==null?void 0:_.target)!=null?G:"unknown",C=(v=(W=f.filesModified)==null?void 0:W.join(", "))!=null?v:"none";return`- **${f.summary}** [scope: pattern/${A}]
29
+ </html>`}import{spawn as Yt}from"child_process";import{createInterface as Qt}from"readline";function Ee(r,n){let{prompt:t,projectRoot:e,maxTurns:o=40,maxBudgetUsd:a=1,allowedTools:i=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:l="claude",resumeSessionId:h,model:f,timeoutMs:_=3e5,onEvent:A}=n,C=[];h?C.push("--resume",h,"-p",t):C.push("-p",t),C.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(a)),f&&C.push("--model",f);for(let W of i)C.push("--allowedTools",W);let g=Yt(l,C,{cwd:e,stdio:["ignore","pipe","pipe"],env:oe(V({},process.env),{ANTHROPIC_API_KEY:void 0})}),G=new Promise(W=>{var j;let v,L=[],O=[],Z=!1,E="",X=!1,U=setTimeout(()=>{X=!0,g.kill("SIGTERM"),setTimeout(()=>{try{g.kill("SIGKILL")}catch(b){}},5e3)},_),ne=Qt({input:g.stdout}),P=new Set;ne.on("line",b=>{var K,ge,be,ke,Te,Re,$e,Me,Ce;if(b.trim())try{let q=JSON.parse(b);q.session_id&&!v&&(v=q.session_id);let Be=(ge=q.type)!=null?ge:(K=q.event)!=null&&K.type?`event.${q.event.type}`:"unknown";if(P.add(Be),q.type==="result"&&q.result&&L.length===0){let B=typeof q.result=="string"?q.result:"";B&&(L.push(B),A==null||A({type:"delta",jobId:r,text:B},r))}if(q.type==="assistant"&&Array.isArray((be=q.message)==null?void 0:be.content))for(let B of q.message.content){if(B.type==="text"&&B.text&&(L.push(B.text),A==null||A({type:"delta",jobId:r,text:B.text},r)),B.type==="tool_use"&&B.name){let Oe=((ke=B.input)==null?void 0:ke.file_path)||((Te=B.input)==null?void 0:Te.path)||void 0;A==null||A(V({type:"tool_use",jobId:r,tool:B.name},Oe?{file:Oe}:{}),r),B.name==="Edit"&&((Re=B.input)!=null&&Re.file_path)?O.push({tool:"Edit",file_path:B.input.file_path,old_string:B.input.old_string,new_string:B.input.new_string,replace_all:B.input.replace_all}):B.name==="Write"&&(($e=B.input)!=null&&$e.file_path)&&O.push({tool:"Write",file_path:B.input.file_path,content:B.input.content})}B.type==="thinking"&&B.thinking&&(A==null||A({type:"thinking",jobId:r,text:B.thinking},r))}q.type==="user"&&((Ce=(Me=q.tool_use_result)==null?void 0:Me.file)!=null&&Ce.filePath)&&(A==null||A({type:"tool_use",jobId:r,tool:"Read",file:q.tool_use_result.file.filePath},r))}catch(q){}});let x=[];(j=g.stderr)==null||j.on("data",b=>{x.push(b.toString())}),g.on("close",b=>{clearTimeout(U),ne.close(),L.length===0&&P.size>0&&console.warn(`[Claude:${r}] No text captured. Event types seen: ${[...P].join(", ")}`),X?(Z=!0,E=`Timed out after ${Math.round(_/6e4)} minutes`):b!==0&&b!==null&&(Z=!0,E=x.join("")||`Claude process exited with code ${b}`),W({sessionId:v,text:L.join(""),success:!Z,error:Z?E:void 0,fileEdits:O.length>0?O:void 0})}),g.on("error",b=>{clearTimeout(U),Z=!0,E=b.message,W({sessionId:v,text:L.join(""),success:!1,error:E,fileEdits:O.length>0?O:void 0})})});return{process:g,result:G}}import{spawn as qt}from"child_process";import{createInterface as Kt}from"readline";function ct(r,n){let{prompt:t,projectRoot:e,screenshotPath:o,resumeSessionId:a,model:i,onEvent:l}=n,h=[];a?(h.push("exec","resume",a),i&&h.push("-m",i),h.push("--json","--full-auto",t),o&&h.push("--image",o)):(h.push("exec","--json","--full-auto"),i&&h.push("-m",i),h.push(t),o&&h.push("--image",o));let f=qt("codex",h,{cwd:e,stdio:["ignore","pipe","pipe"],env:V({},process.env)}),_=new Promise(A=>{var Z;let C,g=[],G=!1,W="",v=Kt({input:f.stdout}),L=new Set;v.on("line",E=>{var X,U,ne,P;if(E.trim())try{let x=JSON.parse(E),j=(X=x.type)!=null?X:"unknown";if(L.add(j),j==="thread.started"&&x.thread_id&&!C&&(C=x.thread_id),(j==="item.agentMessage.delta"||j==="item/agentMessage/delta")&&((U=x.delta)!=null&&U.text)&&(g.push(x.delta.text),l==null||l({type:"delta",jobId:r,text:x.delta.text},r)),(j==="item.reasoning.delta"||j==="item/reasoning/delta")&&((ne=x.delta)!=null&&ne.text)&&(l==null||l({type:"thinking",jobId:r,text:x.delta.text},r)),(j==="item.started"||j==="item/started")&&x.item){let b=x.item.type;if(b==="command_execution")l==null||l({type:"tool_use",jobId:r,tool:"Bash"},r);else if(b==="file_change"){let K=x.item.filename||x.item.path;l==null||l(V({type:"tool_use",jobId:r,tool:"Edit"},K?{file:K}:{}),r)}else if(b==="file_read"){let K=x.item.filename||x.item.path;l==null||l(V({type:"tool_use",jobId:r,tool:"Read"},K?{file:K}:{}),r)}else if(b==="web_search")l==null||l({type:"tool_use",jobId:r,tool:"WebSearch"},r);else if(b==="mcp_tool_call"){let K=x.item.tool_name||x.item.name||"MCP";l==null||l({type:"tool_use",jobId:r,tool:K},r)}}if((j==="item.completed"||j==="item/completed")&&x.item){if(x.item.type==="agent_message"){let b=x.item.text;typeof b=="string"&&b&&(g.push(b),l==null||l({type:"delta",jobId:r,text:b},r))}else if(x.item.type==="reasoning"){let b=x.item.text;typeof b=="string"&&b&&(l==null||l({type:"thinking",jobId:r,text:b},r))}}j==="turn.failed"&&(G=!0,W=((P=x.error)==null?void 0:P.message)||x.message||"Turn failed")}catch(x){}});let O=[];(Z=f.stderr)==null||Z.on("data",E=>{O.push(E.toString())}),f.on("close",E=>{v.close(),g.length===0&&L.size>0&&console.warn(`[Codex:${r}] No text captured. Event types seen: ${[...L].join(", ")}`),E!==0&&E!==null&&(G=!0,W=O.join("")||`Codex process exited with code ${E}`),A({sessionId:C,text:g.join(""),success:!G,error:G?W:void 0})}),f.on("error",E=>{G=!0,W=E.message,A({sessionId:C,text:g.join(""),success:!1,error:W})})});return{process:f,result:_}}import{execFile as Xt}from"child_process";import{copyFile as lt,mkdir as dt,readdir as Vt,readFile as Zt,writeFile as en}from"fs/promises";import{join as fe}from"path";var Ae=class{constructor(n){this.projectRoot=n;let t=fe(n,".popmelt");this.decisionsDir=fe(t,"decisions"),this.screenshotsDir=fe(t,"screenshots")}async persist(n,t,e){try{await dt(this.decisionsDir,{recursive:!0}),await dt(this.screenshotsDir,{recursive:!0});try{await lt(t,fe(this.screenshotsDir,`s-${n.id}.png`))}catch(o){}for(let o=0;o<e.length;o++)try{let a=n.pastedImagePaths[o];a&&await lt(e[o],fe(this.screenshotsDir,a.replace("screenshots/","")))}catch(a){}await en(fe(this.decisionsDir,`d-${n.id}.json`),JSON.stringify(n,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await Vt(this.decisionsDir)).filter(t=>t.startsWith("d-")&&t.endsWith(".json")).map(t=>t.slice(2,-5))}catch(n){return[]}}async loadDecision(n){try{let t=await Zt(fe(this.decisionsDir,`d-${n}.json`),"utf-8");return JSON.parse(t)}catch(t){return null}}async loadDecisions(n){return(await Promise.all(n.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(n){return new Promise(t=>{Xt("git",["diff","HEAD"],{cwd:n,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){t(null);return}t(o||null)})})}};import{readFile as ut,writeFile as ye}from"fs/promises";import{join as Fe}from"path";var ae="[Materializer]",tn={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null},De=class{constructor(n,t,e={}){this.projectRoot=n;this.decisionStore=t;this.options=e;this.cachedIndex=null;this.running=!1;let o=Fe(n,".popmelt");this.indexPath=Fe(o,"materialized.json"),this.modelPath=Fe(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let n=await ut(this.modelPath,"utf-8");return JSON.parse(n)}catch(n){return null}}async addComponent(n){let t=await this.loadModel();t||(t={tokens:{},components:{},rules:[]}),(!t.components||typeof t.components!="object")&&(t.components={});let e=t.components;return e[n]?{added:!1,alreadyExists:!0}:(e[n]={description:""},await ye(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ae} Added component "${n}" to model`),{added:!0,alreadyExists:!1})}async updateToken(n,t){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=n.split("."),a=e;for(let h=0;h<o.length-1;h++){let f=o[h];(!a[f]||typeof a[f]!="object")&&(a[f]={}),a=a[f]}let i=o[o.length-1],l;try{l=JSON.parse(t)}catch(h){l=null}if(l&&typeof l=="object"&&l!==null&&"value"in l)a[i]=l;else{let h=a[i];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=t:a[i]=t}return await ye(this.modelPath,JSON.stringify(e,null,2)),console.log(`${ae} Updated token "${n}" \u2192 "${t.slice(0,80)}"`),{updated:!0}}async removeToken(n){let t=await this.loadModel();if(!t)return{removed:!1};let e=n.split("."),o=t;for(let i=0;i<e.length-1;i++){let l=e[i];if(!o[l]||typeof o[l]!="object")return{removed:!1};o=o[l]}let a=e[e.length-1];return a in o?(delete o[a],await ye(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ae} Removed token "${n}" from model`),{removed:!0}):{removed:!1}}async removeComponent(n){let t=await this.loadModel();if(!t)return{removed:!1};let e=t.components;return!e||!e[n]?{removed:!1}:(delete e[n],await ye(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ae} Removed component "${n}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let n=await this.loadIndex(),t=new Set(n.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(i=>!t.has(i));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(i=>i.resolutions.some(l=>{var f;let h=(f=l.finalScope)!=null?f:l.inferredScope;return(h==null?void 0:h.breadth)==="pattern"}))}async run(){var n,t,e,o,a,i,l;if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let h=await this.getUnmaterializedPatternDecisions();if(h.length===0)return{processedIds:[],success:!0};let f=h.map(v=>v.id);console.log(`${ae} Processing ${f.length} pattern-scoped decision(s): ${f.join(", ")}`),(t=(n=this.options).onEvent)==null||t.call(n,{type:"materialize_started",decisionIds:f});let _=await this.loadModel(),A=sn(h,_),C=!0,g;try{let{result:v}=Ee(`mat-${Date.now()}`,{prompt:A,projectRoot:this.projectRoot,maxTurns:(e=this.options.maxTurns)!=null?e:5,maxBudgetUsd:(o=this.options.maxBudgetUsd)!=null?o:.5,allowedTools:["Read"],claudePath:(a=this.options.claudePath)!=null?a:"claude"}),L=await v;if(!L.success)C=!1,g=L.error,console.error(`${ae} Claude spawn error:`,g);else{let O=nn(L.text);O?(await ye(this.modelPath,JSON.stringify(O,null,2)),console.log(`${ae} Successfully materialized ${f.length} decision(s) \u2192 ${this.modelPath}`)):(C=!1,g="No <model> block found in response",console.error(`${ae} ${g}`))}}catch(v){C=!1,g=v instanceof Error?v.message:String(v),console.error(`${ae} Error:`,g)}let G=await this.loadIndex(),W=new Set(G.materializedIds);for(let v of f)W.add(v);return G.materializedIds=[...W],G.lastRunAt=Date.now(),G.lastRunDecisionIds=f,G.lastRunError=g!=null?g:null,await this.persistIndex(G),(l=(i=this.options).onEvent)==null||l.call(i,{type:"materialize_done",decisionIds:f,success:C,error:g}),{processedIds:f,success:C,error:g}}finally{this.running=!1}}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let n=await ut(this.indexPath,"utf-8"),t=JSON.parse(n);return this.cachedIndex=t,t}catch(n){return this.cachedIndex=oe(V({},tn),{materializedIds:[],lastRunDecisionIds:[]}),this.cachedIndex}}async persistIndex(n){this.cachedIndex=n;try{await ye(this.indexPath,JSON.stringify(n,null,2))}catch(t){console.error(`${ae} Failed to write index:`,t)}}};function nn(r){let n=r.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return typeof t!="object"||t===null||Array.isArray(t)?null:t}catch(t){return null}}function sn(r,n){let t=r.map(o=>{let i=o.resolutions.filter(f=>{var A;let _=(A=f.finalScope)!=null?A:f.inferredScope;return(_==null?void 0:_.breadth)==="pattern"}).map(f=>{var g,G,W,v;let _=(g=f.finalScope)!=null?g:f.inferredScope,A=(G=_==null?void 0:_.target)!=null?G:"unknown",C=(v=(W=f.filesModified)==null?void 0:W.join(", "))!=null?v:"none";return`- **${f.summary}** [scope: pattern/${A}]
30
30
  Files modified: ${C}`}).join(`
31
31
  `),l=o.annotations.map(f=>f.instruction).filter(Boolean).join(`
32
32
  `),h=o.gitDiff?`
@@ -102,19 +102,19 @@ Example:
102
102
  `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function It(r=vt){let n=process.env.CODEX_HOME||We(wt(),".codex"),t=We(n,"config.toml"),e;try{e=await gt(t,"utf-8")}catch(a){e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await cn(ln(t),{recursive:!0});let o=`
103
103
  [mcp_servers.popmelt]
104
104
  url = "${r}"
105
- `;return await yt(t,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function ve(r){let t=(r.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!t)throw new Error("Missing multipart boundary");let e=t[1]||t[2],o=await dn(r),a=Buffer.from(`--${e}`),i=Buffer.from(`--${e}--`),l,h,f,_,A,C,g,G,W,v,L,E,Z=[],O=0,X=[];for(;O<o.length;){let U=o.indexOf(a,O);if(U===-1)break;let ne=U+a.length;if(o.slice(U,U+i.length).equals(i))break;let P=ne;o[P]===13&&o[P+1]===10&&(P+=2);let x=o.indexOf(`\r
105
+ `;return await yt(t,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function ve(r){let t=(r.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!t)throw new Error("Missing multipart boundary");let e=t[1]||t[2],o=await dn(r),a=Buffer.from(`--${e}`),i=Buffer.from(`--${e}--`),l,h,f,_,A,C,g,G,W,v,L,O,Z=[],E=0,X=[];for(;E<o.length;){let U=o.indexOf(a,E);if(U===-1)break;let ne=U+a.length;if(o.slice(U,U+i.length).equals(i))break;let P=ne;o[P]===13&&o[P+1]===10&&(P+=2);let x=o.indexOf(`\r
106
106
  \r
107
- `,P);if(x===-1)break;let j=o.slice(P,x).toString("utf-8"),b=x+4,K=o.indexOf(a,b),ge=K!==-1?K-2:o.length;X.push({headers:j,body:o.slice(b,ge)}),O=K!==-1?K:o.length}for(let U of X){let ne=U.headers.match(/name="([^"]+)"/);if(!ne)continue;let P=ne[1];if(P==="screenshot")l=U.body;else if(P==="feedback")h=U.body.toString("utf-8");else if(P==="color")f=U.body.toString("utf-8");else if(P==="provider")_=U.body.toString("utf-8");else if(P==="model")A=U.body.toString("utf-8");else if(P==="goal")C=U.body.toString("utf-8");else if(P==="pageUrl")g=U.body.toString("utf-8");else if(P==="viewport")G=U.body.toString("utf-8");else if(P==="planId")W=U.body.toString("utf-8");else if(P==="manifest")v=U.body.toString("utf-8");else if(P==="tasks")L=U.body.toString("utf-8");else if(P==="sourceId")E=U.body.toString("utf-8");else if(P.startsWith("image-")){let x=P.split("-"),j=parseInt(x[x.length-1],10),b=x.slice(1,-1).join("-");b&&!isNaN(j)&&Z.push({annotationId:b,index:j,data:U.body})}}if(!l)throw new Error("Missing screenshot field");return h||(h=""),{screenshot:l,feedback:h,color:f,provider:_,model:A,goal:C,pageUrl:g,viewport:G,planId:W,manifest:v,tasks:L,sourceId:E,pastedImages:Z}}function dn(r){return new Promise((n,t)=>{let e=[];r.on("data",o=>e.push(o)),r.on("end",()=>n(Buffer.concat(e))),r.on("error",t)})}function Pe(r,n){var e,o;let t=[];if(r.annotations.length>0){t.push("## Annotations");for(let a of r.annotations){let i=a.elements.map(f=>{let _=[f.selector];return f.reactComponent&&_.push(`(${f.reactComponent})`),_.join(" ")}).join(", "),l=a.instruction||"No text";t.push(`- id=${a.id} [${a.type}] ${l} \u2192 Elements: ${i||"none"}`);let h=n==null?void 0:n[a.id];if(h&&h.length>0)for(let f of h)t.push(` Attached image: use the Read tool to view ${f}`)}}if(r.styleModifications.length>0){t.push(""),t.push("## Style Changes (make permanent in source)"),t.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let a of r.styleModifications){let i=(e=a.element)!=null&&e.reactComponent?`(${a.element.reactComponent})`:"";for(let l of a.changes)t.push(`- ${a.selector} ${i}: ${l.property} ${l.original} \u2192 ${l.modified}`)}}if((o=r.spacingTokenChanges)!=null&&o.length){t.push(""),t.push("## Spacing Token Changes"),t.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let a of r.spacingTokenChanges){t.push(`
107
+ `,P);if(x===-1)break;let j=o.slice(P,x).toString("utf-8"),b=x+4,K=o.indexOf(a,b),ge=K!==-1?K-2:o.length;X.push({headers:j,body:o.slice(b,ge)}),E=K!==-1?K:o.length}for(let U of X){let ne=U.headers.match(/name="([^"]+)"/);if(!ne)continue;let P=ne[1];if(P==="screenshot")l=U.body;else if(P==="feedback")h=U.body.toString("utf-8");else if(P==="color")f=U.body.toString("utf-8");else if(P==="provider")_=U.body.toString("utf-8");else if(P==="model")A=U.body.toString("utf-8");else if(P==="goal")C=U.body.toString("utf-8");else if(P==="pageUrl")g=U.body.toString("utf-8");else if(P==="viewport")G=U.body.toString("utf-8");else if(P==="planId")W=U.body.toString("utf-8");else if(P==="manifest")v=U.body.toString("utf-8");else if(P==="tasks")L=U.body.toString("utf-8");else if(P==="sourceId")O=U.body.toString("utf-8");else if(P.startsWith("image-")){let x=P.split("-"),j=parseInt(x[x.length-1],10),b=x.slice(1,-1).join("-");b&&!isNaN(j)&&Z.push({annotationId:b,index:j,data:U.body})}}if(!l)throw new Error("Missing screenshot field");return h||(h=""),{screenshot:l,feedback:h,color:f,provider:_,model:A,goal:C,pageUrl:g,viewport:G,planId:W,manifest:v,tasks:L,sourceId:O,pastedImages:Z}}function dn(r){return new Promise((n,t)=>{let e=[];r.on("data",o=>e.push(o)),r.on("end",()=>n(Buffer.concat(e))),r.on("error",t)})}function Pe(r,n){var e,o;let t=[];if(r.annotations.length>0){t.push("## Annotations");for(let a of r.annotations){let i=a.elements.map(f=>{let _=[f.selector];return f.reactComponent&&_.push(`(${f.reactComponent})`),_.join(" ")}).join(", "),l=a.instruction||"No text";t.push(`- id=${a.id} [${a.type}] ${l} \u2192 Elements: ${i||"none"}`);let h=n==null?void 0:n[a.id];if(h&&h.length>0)for(let f of h)t.push(` Attached image: use the Read tool to view ${f}`)}}if(r.styleModifications.length>0){t.push(""),t.push("## Style Changes (make permanent in source)"),t.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let a of r.styleModifications){let i=(e=a.element)!=null&&e.reactComponent?`(${a.element.reactComponent})`:"";for(let l of a.changes)t.push(`- ${a.selector} ${i}: ${l.property} ${l.original} \u2192 ${l.modified}`)}}if((o=r.spacingTokenChanges)!=null&&o.length){t.push(""),t.push("## Spacing Token Changes"),t.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let a of r.spacingTokenChanges){t.push(`
108
108
  ### ${a.tokenName}: ${a.originalPx}px \u2192 ${a.newPx}px`);for(let i of a.affectedElements){let l=i.reactComponent?` (${i.reactComponent})`:"";i.matchedClass&&i.suggestedClass?t.push(`- ${i.selector}${l}: \`${i.matchedClass}\` \u2192 \`${i.suggestedClass}\``):t.push(`- ${i.selector}${l}: ${i.property} ${a.originalPx}px \u2192 ${a.newPx}px`),t.push(` class="${i.className}"`)}}}if(r.inspectedElement){let a=r.inspectedElement;t.push(""),t.push("## Inspected Element"),t.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)}"`),t.push(`- ${i.join(" ")}`)}return t.join(`
109
109
  `)}function xt(r,n,t){var a;let e=[];if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),(t==null?void 0:t.provider)!=="codex"&&(e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),e.push("")),e.push(`The developer annotated their running app at ${n.url} (${n.viewport.width}x${n.viewport.height}).`),t!=null&&t.threadHistory&&t.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let i=0;for(let l of t.threadHistory)if(l.role==="human")i++,l.replyToQuestion?(e.push(`### Round ${i} (human) \u2014 reply`),e.push(`"${l.replyToQuestion}"`)):(e.push(`### Round ${i} (human)`),l.feedbackSummary&&e.push(`Annotations: ${l.feedbackSummary}`),l.annotationIds&&l.annotationIds.length>0&&e.push(`Annotation IDs: ${l.annotationIds.join(", ")}`));else if(l.question)e.push(`### Round ${i} (assistant) \u2014 question`),e.push(`"${l.question}"`);else{if(e.push(`### Round ${i} (assistant)`),l.responseText&&e.push(`Response: ${l.responseText}`),l.resolutions&&l.resolutions.length>0)for(let h of l.resolutions){let f=(a=h.finalScope)!=null?a:h.inferredScope,_=f?` [${f.breadth} ${f.target}]`:"";e.push(`- ${h.annotationId}: ${h.status}${_} \u2014 ${h.summary}`),h.filesModified&&h.filesModified.length>0&&e.push(` Files: ${h.filesModified.join(", ")}`)}l.toolsUsed&&l.toolsUsed.length>0&&e.push(`Tools used: ${l.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(t!=null&&t.designModel){e.push(""),e.push("## Established Design Policies"),e.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=t.designModel.rules;if(Array.isArray(i)&&i.length>0){e.push(""),e.push("Rules:");for(let f of i)typeof f=="string"&&e.push(`- ${f}`)}let l=t.designModel.tokens;l&&typeof l=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(l,null,2)),e.push("```"));let h=t.designModel.components;h&&typeof h=="object"&&(e.push(""),e.push("Component patterns:"),e.push("```json"),e.push(JSON.stringify(h,null,2)),e.push("```")),e.push(""),e.push("### Novel Pattern Detection"),e.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:"),e.push("<novel>"),e.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),e.push("</novel>"),e.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),e.push("- `element`: What you are styling or creating"),e.push("- `decision`: What you decided to do (specific values)"),e.push("- `reason`: Why this is novel (what is missing from the model)"),e.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}t!=null&&t.designModel||(e.push(""),e.push("## Design Context"),e.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 o=Pe(n,t==null?void 0:t.imagePaths);return o&&(e.push(""),e.push(o)),e.push(""),e.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."),e.push(""),e.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e.push(""),e.push("## Resolution"),e.push("After completing all work, output a resolution block listing what you did for each annotation:"),e.push("<resolution>"),e.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),e.push("</resolution>"),e.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),e.push(""),e.push("### Scope classification"),e.push("Each resolution MUST include scope fields:"),e.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),e.push("- `inferredScope`: What scope the change actually has, based on what you modified."),e.push("Scope has two dimensions:"),e.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),e.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),e.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),e.push("If you cannot confidently determine scope, set it to null."),e.push(""),e.push("## Questions"),e.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),e.push('<question>What do you mean by "..."?</question>'),e.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),e.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),e.join(`
110
110
  `)}function Pt(r){var t;let n=r.match(/<question>\s*([\s\S]*?)\s*<\/question>/);return(t=n==null?void 0:n[1])!=null?t:null}function bt(r,n,t,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),t!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`);let a=n.find(i=>i.role==="human"&&i.feedbackContext);if(a!=null&&a.feedbackContext&&(o.push(""),o.push(a.feedbackContext)),n.length>0){o.push(""),o.push("## Conversation History");let i=0;for(let l of n)l.role==="human"?(i++,l.replyToQuestion?(o.push(`### Round ${i} (human) \u2014 reply`),o.push(`"${l.replyToQuestion}"`)):(o.push(`### Round ${i} (human)`),l.feedbackSummary&&o.push(`Annotations: ${l.feedbackSummary}`))):l.question?(o.push(`### Round ${i} (assistant) \u2014 question`),o.push(`"${l.question}"`)):(o.push(`### Round ${i} (assistant)`),l.responseText&&o.push(`Response: ${l.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let i of e)o.push(`Attached image: use the Read tool to view the image at: ${i}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
111
- `)}function un(r){if(typeof r!="object"||r===null)return!1;let n=r;return(n.breadth==="instance"||n.breadth==="pattern")&&(n.target==="element"||n.target==="component"||n.target==="token")}function kt(r){if(typeof r!="object"||r===null||typeof r.annotationId!="string"||r.status!=="resolved"&&r.status!=="needs_review"||typeof r.summary!="string")return!1;let n=r;for(let t of["declaredScope","inferredScope","finalScope"])if(n[t]!==void 0&&n[t]!==null&&!un(n[t]))return!1;return!0}function Tt(r){let n=r.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!n||!n[1])return[];try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(kt):[]}catch(t){return[]}}function $t(r){let n=[],t=/<resolution>\s*([\s\S]*?)\s*<\/resolution>/g,e;for(;(e=t.exec(r))!==null;)if(e[1])try{let o=JSON.parse(e[1]);Array.isArray(o)&&n.push(...o.filter(kt))}catch(o){}return n}function Rt(r){let n=r.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!(n!=null&&n[1]))return[];try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;return(o.category==="token"||o.category==="component"||o.category==="element")&&typeof o.element=="string"&&typeof o.decision=="string"&&typeof o.reason=="string"}):[]}catch(t){return[]}}function Mt(r,n,t,e,o,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: ${r}`),i.push(""),i.push(`Page: ${t}`),i.push(`Viewport: ${e.width}x${e.height}`),o&&(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(o),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(n),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(`
112
- `)}function Ct(r){let n=r.match(/<plan>\s*([\s\S]*?)\s*<\/plan>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;if(typeof o.id!="string"||typeof o.instruction!="string"||typeof o.region!="object"||o.region===null)return!1;let a=o.region;return typeof a.x=="number"&&typeof a.y=="number"&&typeof a.width=="number"&&typeof a.height=="number"}):null}catch(t){return null}}function Et(r,n,t){let e=[];e.push("You are reviewing whether a series of UI changes achieved the original design goal."),e.push(""),e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),e.push(""),e.push("## Original Goal"),e.push(n),e.push(""),e.push("## Completed Tasks");for(let o of t)e.push(`- [${o.id}] ${o.instruction} \u2192 ${o.summary}`);return e.push(""),e.push("## Your Task"),e.push("Look at the current screenshot and determine if the goal has been achieved."),e.push("Output your verdict inside a <review> tag:"),e.push("<review>"),e.push('{"verdict":"pass","summary":"The changes look good..."}'),e.push("</review>"),e.push(""),e.push("Or if issues remain:"),e.push("<review>"),e.push('{"verdict":"fail","summary":"Some issues remain...","issues":["Issue 1","Issue 2"]}'),e.push("</review>"),e.push(""),e.push("Do NOT modify any files. Output only a <review> block."),e.join(`
113
- `)}function Ot(r,n,t,e,o){let a=[];a.push("You are implementing a series of UI changes on a web application."),a.push(""),o!=="codex"&&(a.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),a.push("")),a.push(`Page: ${t} (${e.width}x${e.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 n){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 l=i.elements.map(h=>{let f=[h.selector];return h.reactComponent&&f.push(`(${h.reactComponent})`),f.join(" ")}).join(", ");a.push(`Elements: ${l}`)}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(`
114
- `)}function At(r){let n=r.match(/<review>\s*([\s\S]*?)\s*<\/review>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return typeof t!="object"||t===null||t.verdict!=="pass"&&t.verdict!=="fail"||typeof t.summary!="string"?null:{verdict:t.verdict,summary:t.summary,issues:Array.isArray(t.issues)?t.issues.filter(e=>typeof e=="string"):void 0}}catch(t){return null}}var _e=class{constructor(n=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.maxConcurrency=n}setProcessor(n){this.processor=n}get active(){let n=this.activeJobs.values().next();return n.done?null:n.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(n,t){t?this.activeProcesses.set(n,t):this.activeProcesses.delete(n)}enqueue(n){return this.queue.push(n),this.processNext(),this.queue.length+this.activeJobs.size}addListener(n){return this.listeners.add(n),()=>this.listeners.delete(n)}broadcast(n,t,e){for(let o of this.listeners)o(n,t,e)}cancelJob(n){let t=this.activeProcesses.get(n),e=this.activeJobs.get(n);return!t||!e?!1:(t.kill("SIGTERM"),this.activeProcesses.delete(n),this.activeJobs.delete(n),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,message:"Cancelled by user",cancelled:!0},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let n=Array.from(this.activeJobs.keys());for(let t of n)this.cancelJob(t);return!0}destroy(){for(let n of this.activeProcesses.values())n.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear()}async destroyAsync(n=1e4){let t=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),t.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of t)try{e.kill("SIGTERM")}catch(o){}await Promise.all(t.map(e=>new Promise(o=>{let a=!1,i=()=>{a||(a=!0,o())};e.on("exit",i),e.on("error",i),setTimeout(()=>{if(!a){try{e.kill("SIGKILL")}catch(l){}setTimeout(i,500)}},n)}))),this.activeProcesses.clear(),this.activeJobs.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let n=this.queue.shift();this.activeJobs.set(n.id,n),n.status="running",this.broadcast({type:"job_started",jobId:n.id,position:0},n.id,n.sourceId),this.processor(n).catch(t=>{n.status="error",n.error=t instanceof Error?t.message:String(t),this.broadcast({type:"error",jobId:n.id,message:n.error},n.id,n.sourceId)}).finally(()=>{this.activeJobs.delete(n.id),this.activeProcesses.delete(n.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},n.id)})}}};import{mkdir as pn,readFile as hn,writeFile as fn}from"fs/promises";import{dirname as mn,join as gn}from"path";var yn={version:1,threads:{}},Ne=class{constructor(n){this.cache=null;this.writeChain=Promise.resolve();this.filePath=gn(n,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let n=await hn(this.filePath,"utf-8"),t=JSON.parse(n);if(t&&t.version===1&&t.threads)return this.cache=t,this.cache}catch(n){}return this.cache=oe(V({},yn),{threads:{}}),this.cache}async getThread(n){var e;return(e=(await this.load()).threads[n])!=null?e:null}async findContinuationThread(n){if(n.length===0)return null;let t=await this.load(),e=new Set(n);for(let o of Object.values(t.threads))if(o.elementIdentifiers.some(i=>e.has(i)))return o;return null}async createThread(n,t){let e=await this.load(),o={id:n,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:t,messages:[]};return e.threads[n]=o,await this.persist(),o}async appendMessage(n,t){let o=(await this.load()).threads[n];o&&(o.messages.push(t),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(n,t){let o=(await this.load()).threads[n];if(!o)return;let a=new Set(o.elementIdentifiers);for(let i of t)a.has(i)||o.elementIdentifiers.push(i);o.updatedAt=Date.now(),await this.persist()}async getThreadHistory(n,t=6){let e=await this.getThread(n);return!e||e.messages.length===0?[]:e.messages.length<=t?e.messages:[e.messages[0],...e.messages.slice(-(t-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await pn(mn(this.filePath),{recursive:!0}),await fn(this.filePath,JSON.stringify(this.cache,null,2))}catch(n){console.error("[ThreadStore] Failed to persist:",n)}}),await this.writeChain}};var Nn={};var Mn=1111,Cn=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],En=1800*1e3,On=3600*1e3;function _t(r){if(!r)return!1;try{let n=new URL(r);return n.hostname==="localhost"||n.hostname==="127.0.0.1"}catch(n){return!1}}function An(r,n){let t=r.headers.origin;_t(t)&&(n.setHeader("Access-Control-Allow-Origin",t),n.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),n.setHeader("Access-Control-Allow-Headers","Content-Type"))}function m(r,n,t){r.writeHead(n,{"Content-Type":"application/json"}),r.end(JSON.stringify(t))}function Dn(r,n){if(!r)return n;let t=r.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!t)return n;let[,e,o,a]=t;return`\x1B[38;2;${parseInt(e,16)};${parseInt(o,16)};${parseInt(a,16)}m${n}\x1B[0m`}function Dt(r,n){try{r.res.write(`event: ${n.type}
111
+ `)}function un(r){if(typeof r!="object"||r===null)return!1;let n=r;return(n.breadth==="instance"||n.breadth==="pattern")&&(n.target==="element"||n.target==="component"||n.target==="token")}function kt(r){if(typeof r!="object"||r===null||typeof r.annotationId!="string"||r.status!=="resolved"&&r.status!=="needs_review"||typeof r.summary!="string")return!1;let n=r;for(let t of["declaredScope","inferredScope","finalScope"])if(n[t]!==void 0&&n[t]!==null&&!un(n[t]))return!1;return!0}function Tt(r){let n=r.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!n||!n[1])return[];try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(kt):[]}catch(t){return[]}}function Rt(r){let n=[],t=/<resolution>\s*([\s\S]*?)\s*<\/resolution>/g,e;for(;(e=t.exec(r))!==null;)if(e[1])try{let o=JSON.parse(e[1]);Array.isArray(o)&&n.push(...o.filter(kt))}catch(o){}return n}function $t(r){let n=r.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!(n!=null&&n[1]))return[];try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;return(o.category==="token"||o.category==="component"||o.category==="element")&&typeof o.element=="string"&&typeof o.decision=="string"&&typeof o.reason=="string"}):[]}catch(t){return[]}}function Mt(r,n,t,e,o,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: ${r}`),i.push(""),i.push(`Page: ${t}`),i.push(`Viewport: ${e.width}x${e.height}`),o&&(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(o),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(n),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(`
112
+ `)}function Ct(r){let n=r.match(/<plan>\s*([\s\S]*?)\s*<\/plan>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;if(typeof o.id!="string"||typeof o.instruction!="string"||typeof o.region!="object"||o.region===null)return!1;let a=o.region;return typeof a.x=="number"&&typeof a.y=="number"&&typeof a.width=="number"&&typeof a.height=="number"}):null}catch(t){return null}}function Ot(r,n,t){let e=[];e.push("You are reviewing whether a series of UI changes achieved the original design goal."),e.push(""),e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),e.push(""),e.push("## Original Goal"),e.push(n),e.push(""),e.push("## Completed Tasks");for(let o of t)e.push(`- [${o.id}] ${o.instruction} \u2192 ${o.summary}`);return e.push(""),e.push("## Your Task"),e.push("Look at the current screenshot and determine if the goal has been achieved."),e.push("Output your verdict inside a <review> tag:"),e.push("<review>"),e.push('{"verdict":"pass","summary":"The changes look good..."}'),e.push("</review>"),e.push(""),e.push("Or if issues remain:"),e.push("<review>"),e.push('{"verdict":"fail","summary":"Some issues remain...","issues":["Issue 1","Issue 2"]}'),e.push("</review>"),e.push(""),e.push("Do NOT modify any files. Output only a <review> block."),e.join(`
113
+ `)}function Et(r,n,t,e,o){let a=[];a.push("You are implementing a series of UI changes on a web application."),a.push(""),o!=="codex"&&(a.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),a.push("")),a.push(`Page: ${t} (${e.width}x${e.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 n){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 l=i.elements.map(h=>{let f=[h.selector];return h.reactComponent&&f.push(`(${h.reactComponent})`),f.join(" ")}).join(", ");a.push(`Elements: ${l}`)}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(`
114
+ `)}function At(r){let n=r.match(/<review>\s*([\s\S]*?)\s*<\/review>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return typeof t!="object"||t===null||t.verdict!=="pass"&&t.verdict!=="fail"||typeof t.summary!="string"?null:{verdict:t.verdict,summary:t.summary,issues:Array.isArray(t.issues)?t.issues.filter(e=>typeof e=="string"):void 0}}catch(t){return null}}var _e=class{constructor(n=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.maxConcurrency=n}setProcessor(n){this.processor=n}get active(){let n=this.activeJobs.values().next();return n.done?null:n.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(n,t){t?this.activeProcesses.set(n,t):this.activeProcesses.delete(n)}enqueue(n){return this.queue.push(n),this.processNext(),this.queue.length+this.activeJobs.size}addListener(n){return this.listeners.add(n),()=>this.listeners.delete(n)}broadcast(n,t,e){for(let o of this.listeners)o(n,t,e)}cancelJob(n){let t=this.activeProcesses.get(n),e=this.activeJobs.get(n);return!t||!e?!1:(t.kill("SIGTERM"),this.activeProcesses.delete(n),this.activeJobs.delete(n),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,message:"Cancelled by user",cancelled:!0},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let n=Array.from(this.activeJobs.keys());for(let t of n)this.cancelJob(t);return!0}destroy(){for(let n of this.activeProcesses.values())n.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear()}async destroyAsync(n=1e4){let t=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),t.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of t)try{e.kill("SIGTERM")}catch(o){}await Promise.all(t.map(e=>new Promise(o=>{let a=!1,i=()=>{a||(a=!0,o())};e.on("exit",i),e.on("error",i),setTimeout(()=>{if(!a){try{e.kill("SIGKILL")}catch(l){}setTimeout(i,500)}},n)}))),this.activeProcesses.clear(),this.activeJobs.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let n=this.queue.shift();this.activeJobs.set(n.id,n),n.status="running",this.broadcast({type:"job_started",jobId:n.id,position:0},n.id,n.sourceId),this.processor(n).catch(t=>{n.status="error",n.error=t instanceof Error?t.message:String(t),this.broadcast({type:"error",jobId:n.id,message:n.error},n.id,n.sourceId)}).finally(()=>{this.activeJobs.delete(n.id),this.activeProcesses.delete(n.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},n.id)})}}};import{mkdir as pn,readFile as hn,writeFile as fn}from"fs/promises";import{dirname as mn,join as gn}from"path";var yn={version:1,threads:{}},Ne=class{constructor(n){this.cache=null;this.writeChain=Promise.resolve();this.filePath=gn(n,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let n=await hn(this.filePath,"utf-8"),t=JSON.parse(n);if(t&&t.version===1&&t.threads)return this.cache=t,this.cache}catch(n){}return this.cache=oe(V({},yn),{threads:{}}),this.cache}async getThread(n){var e;return(e=(await this.load()).threads[n])!=null?e:null}async findContinuationThread(n){if(n.length===0)return null;let t=await this.load(),e=new Set(n);for(let o of Object.values(t.threads))if(o.elementIdentifiers.some(i=>e.has(i)))return o;return null}async createThread(n,t){let e=await this.load(),o={id:n,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:t,messages:[]};return e.threads[n]=o,await this.persist(),o}async appendMessage(n,t){let o=(await this.load()).threads[n];o&&(o.messages.push(t),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(n,t){let o=(await this.load()).threads[n];if(!o)return;let a=new Set(o.elementIdentifiers);for(let i of t)a.has(i)||o.elementIdentifiers.push(i);o.updatedAt=Date.now(),await this.persist()}async getThreadHistory(n,t=6){let e=await this.getThread(n);return!e||e.messages.length===0?[]:e.messages.length<=t?e.messages:[e.messages[0],...e.messages.slice(-(t-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await pn(mn(this.filePath),{recursive:!0}),await fn(this.filePath,JSON.stringify(this.cache,null,2))}catch(n){console.error("[ThreadStore] Failed to persist:",n)}}),await this.writeChain}};var Nn={};var Mn=1111,Cn=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],On=1800*1e3,En=3600*1e3;function _t(r){if(!r)return!1;try{let n=new URL(r);return n.hostname==="localhost"||n.hostname==="127.0.0.1"}catch(n){return!1}}function An(r,n){let t=r.headers.origin;_t(t)&&(n.setHeader("Access-Control-Allow-Origin",t),n.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),n.setHeader("Access-Control-Allow-Headers","Content-Type"))}function m(r,n,t){r.writeHead(n,{"Content-Type":"application/json"}),r.end(JSON.stringify(t))}function Dn(r,n){if(!r)return n;let t=r.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!t)return n;let[,e,o,a]=t;return`\x1B[38;2;${parseInt(e,16)};${parseInt(o,16)};${parseInt(a,16)}m${n}\x1B[0m`}function Dt(r,n){try{r.res.write(`event: ${n.type}
115
115
  data: ${JSON.stringify(n)}
116
116
 
117
- `)}catch(t){}}async function Jn(r){try{let n=new AbortController,t=setTimeout(()=>n.abort(),500),e=await fetch(`http://127.0.0.1:${r}/status`,{signal:n.signal});return clearTimeout(t),e.ok?await e.json():null}catch(n){return null}}function _n(r,n){return new Promise((t,e)=>{let o=i=>{r.removeListener("listening",a),e(i)},a=()=>{r.removeListener("error",o),t()};r.once("error",o),r.once("listening",a),r.listen(n,"127.0.0.1")})}async function Ye(r={}){var Ke,Xe,Ve,Ze,et,tt,nt,st,ot;let n=(Ke=r.port)!=null?Ke:Mn,t=(Xe=r.projectRoot)!=null?Xe:process.cwd(),e=vn("sha256").update(t).digest("hex").slice(0,12),o=(Ve=r.devOrigin)!=null?Ve:null,a=(Ze=r.tempDir)!=null?Ze:re(Tn(),"popmelt-bridge"),i=(et=r.maxTurns)!=null?et:40,l=(tt=r.maxBudgetUsd)!=null?tt:1,h=(nt=r.allowedTools)!=null?nt:Cn,f=(st=r.claudePath)!=null?st:"claude",_=(ot=r.provider)!=null?ot:"claude",A=r.timeoutMs,C=n,g={};for(let s of["claude","codex"])try{let c=wn("which",[s],{encoding:"utf-8"}).trim();g[s]={available:!0,path:c}}catch(c){g[s]={available:!1,path:null}}let[G,W]=await Promise.all([je(t),He(t)]);g.claude&&(g.claude.mcp=G),g.codex&&(g.codex.mcp=W),await Sn(a,{recursive:!0}),Jt(a).catch(()=>{});let v=new _e(1),L=new Set,E=new Ne(t),Z=new Ae(t),O=new De(t,Z,{claudePath:f,onEvent:s=>{for(let c of L)Dt(c,s)}}),X=new Map,U=20,ne=300*1e3,P=[],x=null,j;v.addListener((s,c,u)=>{for(let d of L)(!u||!d.sourceId||d.sourceId===u)&&Dt(d,s)}),v.setProcessor(async s=>{var ee,ie,J,se,de,Ie,pe;let c=s._replyPrompt,u=s._replyImagePaths,d=(ee=s.provider)!=null?ee:_,p;if(s.threadId){let R=await E.getThread(s.threadId);if(R){for(let y=R.messages.length-1;y>=0;y--)if(R.messages[y].sessionId){p=R.messages[y].sessionId;break}}}let S;if(p&&c){let R=(ie=await E.getThread(s.threadId))==null?void 0:ie.messages.filter(M=>M.role==="human").pop();if(S=(R==null?void 0:R.replyToQuestion)||(R==null?void 0:R.feedbackSummary)||"",u&&u.length>0){S+=`
117
+ `)}catch(t){}}async function Jn(r){try{let n=new AbortController,t=setTimeout(()=>n.abort(),500),e=await fetch(`http://127.0.0.1:${r}/status`,{signal:n.signal});return clearTimeout(t),e.ok?await e.json():null}catch(n){return null}}function _n(r,n){return new Promise((t,e)=>{let o=i=>{r.removeListener("listening",a),e(i)},a=()=>{r.removeListener("error",o),t()};r.once("error",o),r.once("listening",a),r.listen(n,"127.0.0.1")})}async function Ye(r={}){var Ke,Xe,Ve,Ze,et,tt,nt,st,ot;let n=(Ke=r.port)!=null?Ke:Mn,t=(Xe=r.projectRoot)!=null?Xe:process.cwd(),e=vn("sha256").update(t).digest("hex").slice(0,12),o=(Ve=r.devOrigin)!=null?Ve:process.env.PORT?`http://localhost:${process.env.PORT}`:null,a=(Ze=r.tempDir)!=null?Ze:re(Tn(),"popmelt-bridge"),i=(et=r.maxTurns)!=null?et:40,l=(tt=r.maxBudgetUsd)!=null?tt:1,h=(nt=r.allowedTools)!=null?nt:Cn,f=(st=r.claudePath)!=null?st:"claude",_=(ot=r.provider)!=null?ot:"claude",A=r.timeoutMs,C=n,g={};for(let s of["claude","codex"])try{let c=wn("which",[s],{encoding:"utf-8"}).trim();g[s]={available:!0,path:c}}catch(c){g[s]={available:!1,path:null}}let[G,W]=await Promise.all([je(t),He(t)]);g.claude&&(g.claude.mcp=G),g.codex&&(g.codex.mcp=W),await Sn(a,{recursive:!0}),Jt(a).catch(()=>{});let v=new _e(1),L=new Set,O=new Ne(t),Z=new Ae(t),E=new De(t,Z,{claudePath:f,onEvent:s=>{for(let c of L)Dt(c,s)}}),X=new Map,U=20,ne=300*1e3,P=[],x=null,j;v.addListener((s,c,u)=>{for(let d of L)(!u||!d.sourceId||d.sourceId===u)&&Dt(d,s)}),v.setProcessor(async s=>{var ee,ie,J,se,de,Ie,pe;let c=s._replyPrompt,u=s._replyImagePaths,d=(ee=s.provider)!=null?ee:_,p;if(s.threadId){let $=await O.getThread(s.threadId);if($){for(let y=$.messages.length-1;y>=0;y--)if($.messages[y].sessionId){p=$.messages[y].sessionId;break}}}let S;if(p&&c){let $=(ie=await O.getThread(s.threadId))==null?void 0:ie.messages.filter(M=>M.role==="human").pop();if(S=($==null?void 0:$.replyToQuestion)||($==null?void 0:$.feedbackSummary)||"",u&&u.length>0){S+=`
118
118
 
119
119
  The developer attached reference images with their reply:`;for(let M of u)S+=`
120
120
  Attached image: use the Read tool to view the image at: ${M}`}S+=`
@@ -125,9 +125,9 @@ Follow the developer's instructions. If they ask for changes, apply them to the
125
125
 
126
126
  After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(d!=="codex"?`
127
127
 
128
- IMPORTANT: First, use the Read tool to view the updated screenshot at: ${s.screenshotPath}`:"");else{let R=!c&&s.threadId?await E.getThreadHistory(s.threadId):void 0,y=c?null:await O.loadModel();S=c!=null?c:xt(s.screenshotPath,s.feedback,{threadHistory:R&&R.length>0?R:void 0,provider:d,imagePaths:s.imagePaths,designModel:y!=null?y:void 0})}let $=Dn(s.color,`[\u22B9 ${C}:${s.id}]`);console.log(`${$} Reviewing feedback ${s.screenshotPath} (provider: ${d})${s.threadId?` (thread: ${s.threadId})`:""}${p?` (resuming: ${p.slice(0,8)})`:""}`);let k=!!s._isPlanExecutor,D="",w=0,Y=(R,y)=>{if(v.broadcast(R,y,s.sourceId),k&&R.type==="delta"&&"text"in R){D+=R.text;let M=$t(D);if(M.length>w){let te=M.slice(w);w=M.length,v.broadcast({type:"task_resolved",jobId:y,planId:s.planId,resolutions:te,threadId:s.threadId},y,s.sourceId)}}},z=(J=s._allowedTools)!=null?J:h,{process:N,result:T}=d==="codex"?ct(s.id,{prompt:S,projectRoot:t,screenshotPath:s.screenshotPath,resumeSessionId:p,model:s.model,onEvent:Y}):Oe(s.id,{prompt:S,projectRoot:t,maxTurns:i,maxBudgetUsd:l,allowedTools:z,claudePath:f,resumeSessionId:p,model:s.model,timeoutMs:A,onEvent:Y});v.setActiveProcess(s.id,N);let I=await T;if(s.result=I.text,I.success){console.log(`${$} Iteration complete`),I.fileEdits&&I.fileEdits.length>0&&console.log(`${$} Captured ${I.fileEdits.length} file edit(s): ${I.fileEdits.map(F=>`${F.tool} ${F.file_path}`).join(", ")}`),s.status="done";let R=Pt(I.text),y=Tt(I.text);if(y.length>0&&s.annotationIds&&s.annotationIds.length>0){let F=new Set(s.annotationIds);y.every(ce=>F.has(ce.annotationId))||(y=y.map((ce,he)=>oe(V({},ce),{annotationId:s.annotationIds[he%s.annotationIds.length]})))}let M=I.fileEdits&&I.fileEdits.length>0?I.fileEdits.map(F=>`${F.tool} ${F.file_path.split("/").pop()}`):void 0;if(s.threadId&&await E.appendMessage(s.threadId,{role:"assistant",timestamp:Date.now(),jobId:s.id,responseText:I.text,resolutions:y.length>0?y:void 0,question:R!=null?R:void 0,sessionId:I.sessionId,toolsUsed:M}),Z.captureGitDiff(t).then(async F=>{var rt;let Q=Date.now(),ce=s.imagePaths?Object.values(s.imagePaths).flat():[],he=[];if(s.imagePaths)for(let[Ht,Wt]of Object.entries(s.imagePaths))for(let Ue=0;Ue<Wt.length;Ue++)he.push(`screenshots/p-${s.id}-${Ht}-${Ue}.png`);await Z.persist({version:1,id:s.id,createdAt:s.createdAt,completedAt:Q,durationMs:Q-s.createdAt,url:s.feedback.url,viewport:s.feedback.viewport,screenshotPath:`screenshots/s-${s.id}.png`,pastedImagePaths:he,annotations:s.feedback.annotations,styleModifications:s.feedback.styleModifications,inspectedElement:s.feedback.inspectedElement,provider:s.provider,model:s.model,sessionId:I.sessionId,threadId:s.threadId,planId:s.planId,planTaskId:s.planTaskId,responseText:I.text,resolutions:y.length>0?y:[],question:R!=null?R:void 0,fileEdits:(rt=I.fileEdits)!=null?rt:[],toolsUsed:M,gitDiff:F},s.screenshotPath,ce)}).catch(()=>{}),y.length>0&&y.some(Q=>{var he;let ce=(he=Q.finalScope)!=null?he:Q.inferredScope;return(ce==null?void 0:ce.breadth)==="pattern"})&&O.run().catch(()=>{}),s.planId&&!s.planTaskId){let F=X.get(s.planId);if(F){let Q=Ct(I.text);Q&&Q.length>0?(F.plan=Q,F.status="awaiting_approval",F.plannerThreadId=s.threadId,console.log(`${$} Plan ready: ${Q.length} tasks for group ${s.planId}`),v.broadcast({type:"plan_ready",jobId:s.id,planId:s.planId,tasks:Q,threadId:s.threadId},s.id,s.sourceId)):R||(F.status="error",console.error(`${$} Failed to parse plan from planner response`))}}if(s.planId&&s._isReview){let F=X.get(s.planId);if(F){let Q=At(I.text);Q&&(F.status=Q.verdict==="pass"?"done":"executing",console.log(`${$} Review verdict: ${Q.verdict} \u2014 ${Q.summary}`),v.broadcast({type:"plan_review",planId:s.planId,verdict:Q.verdict,summary:Q.summary,issues:Q.issues},s.id,s.sourceId))}}R&&(console.log(`${$} \u{1F4AC} Question detected: "${R.slice(0,120)}" \u2192 broadcasting to ${L.size} SSE clients (threadId=${(se=s.threadId)!=null?se:s.id}, annotationIds=${(Ie=(de=s.annotationIds)==null?void 0:de.join(","))!=null?Ie:"none"})`),v.broadcast({type:"question",jobId:s.id,threadId:(pe=s.threadId)!=null?pe:s.id,question:R,annotationIds:s.annotationIds},s.id,s.sourceId));let te=Rt(I.text);te.length>0&&(console.log(`${$} Novel pattern(s): ${te.map(F=>`${F.category}/${F.element}`).join(", ")}`),v.broadcast({type:"novel_patterns",jobId:s.id,patterns:te,threadId:s.threadId},s.id,s.sourceId)),v.broadcast({type:"done",jobId:s.id,success:!0,resolutions:y.length>0?y:void 0,responseText:I.text,threadId:s.threadId},s.id,s.sourceId),P.push({id:s.id,status:"done",completedAt:Date.now(),threadId:s.threadId})}else console.error(`${$} Error: ${I.error}`),s.status="error",s.error=I.error,v.broadcast({type:"error",jobId:s.id,message:I.error||"Unknown error"},s.id,s.sourceId),P.push({id:s.id,status:"error",completedAt:Date.now(),error:I.error,threadId:s.threadId});let H=Date.now()-ne;for(;P.length>0&&(P[0].completedAt<H||P.length>U);)P.shift()});let b=kn(async(s,c)=>{if(An(s,c),s.method==="OPTIONS"){c.writeHead(204),c.end();return}let d=new URL(s.url||"/",`http://127.0.0.1:${C}`).pathname;try{if(s.method==="POST"&&d==="/send")await K(s,c);else if(s.method==="GET"&&d==="/events")be(s,c);else if(s.method==="GET"&&d==="/status")ke(c);else if(s.method==="GET"&&d==="/capabilities")m(c,200,{providers:g});else if(s.method==="POST"&&d==="/mcp/install")await B(s,c);else if(s.method==="POST"&&d==="/reply")await ge(s,c);else if(s.method==="POST"&&d==="/cancel")Te(s,c);else if(s.method==="POST"&&d==="/materialize")await $e(c);else if(s.method==="POST"&&d==="/plan")await Re(s,c);else if(s.method==="POST"&&d==="/plan/approve")await Me(s,c);else if(s.method==="POST"&&d==="/plan/execute")await Ce(s,c);else if(s.method==="POST"&&d==="/plan/review")await q(s,c);else if(s.method==="GET"&&d.startsWith("/plan/"))Be(d.slice(6),c);else if(s.method==="POST"&&d==="/model/component")await Ee(s,c);else if(s.method==="DELETE"&&d==="/model/component")await Nt(s,c);else if(s.method==="PATCH"&&d==="/model/token")await Bt(s,c);else if(s.method==="DELETE"&&d==="/model/token")await Ut(s,c);else if(s.method==="GET"&&d==="/model"){let p=await O.loadModel();m(c,200,{model:p})}else if(s.method==="GET"&&d.startsWith("/thread/")){let p=d.slice(8);await Gt(p,c)}else s.method==="GET"&&(d==="/canvas"||d==="/canvas/")?Ft(s,c):s.method==="GET"&&d==="/canvas/manifest"?await Lt(c):s.method==="GET"&&d==="/canvas/app.mjs"?await zt(c):m(c,404,{error:"Not found"})}catch(p){console.error("[Bridge] Request error:",p),m(c,500,{error:p instanceof Error?p.message:"Internal error"})}});async function K(s,c){let{screenshot:u,feedback:d,color:p,provider:S,model:$,sourceId:k,pastedImages:D}=await ve(s),w;try{w=JSON.parse(d)}catch(J){m(c,400,{error:"Invalid feedback JSON"});return}let Y=me().slice(0,8),z=re(a,`screenshot-${Y}.png`);await Se(z,u);let N={};if(D.length>0)for(let J of D){let se=re(a,`pasted-${Y}-${J.annotationId}-${J.index}.png`);await Se(se,J.data),N[J.annotationId]||(N[J.annotationId]=[]),N[J.annotationId].push(se)}let T=w.annotations.map(J=>J.linkedSelector).filter(J=>!!J),I;if(T.length>0){let J=await E.findContinuationThread(T);J?(I=J.id,await E.addElementIdentifiers(I,T)):I=(await E.createThread(Y,T)).id}let H=w.annotations.map(J=>J.id),ee=oe(V({id:Y,status:"queued",screenshotPath:z,feedback:w,createdAt:Date.now(),color:p,threadId:I,annotationIds:H,provider:S==="claude"||S==="codex"?S:void 0,model:$||void 0},Object.keys(N).length>0?{imagePaths:N}:{}),{sourceId:k||void 0});if(I){let J=w.annotations.map(de=>de.instruction||`[${de.type}]`).join("; "),se=Pe(w,Object.keys(N).length>0?N:void 0);await E.appendMessage(I,{role:"human",timestamp:Date.now(),jobId:Y,screenshotPath:z,annotationIds:H,feedbackSummary:J,feedbackContext:se||void 0})}let ie=v.enqueue(ee);m(c,200,{jobId:Y,position:ie,threadId:I})}async function ge(s,c){let u=s.headers["content-type"]||"",d,p,S,$,k,D,w=[];if(u.includes("multipart/form-data")){let y=await ve(s),M=y.feedback?JSON.parse(y.feedback):{};d=M.threadId,p=M.reply,S=M.color,$=M.provider,k=M.model,D=M.sourceId||y.sourceId;for(let te of y.pastedImages)w.push(te.data)}else{let y=[];try{for(var de=ue(s),Ie,pe,R;Ie=!(pe=await de.next()).done;Ie=!1){let F=pe.value;y.push(typeof F=="string"?Buffer.from(F):F)}}catch(pe){R=[pe]}finally{try{Ie&&(pe=de.return)&&await pe.call(de)}finally{if(R)throw R[0]}}let M=Buffer.concat(y).toString("utf-8"),te;try{te=JSON.parse(M)}catch(F){m(c,400,{error:"Invalid JSON"});return}d=te.threadId,p=te.reply,S=te.color,$=te.provider,k=te.model,D=te.sourceId}if(!d||!p){m(c,400,{error:"Missing threadId or reply"});return}if(!await E.getThread(d)){m(c,404,{error:"Thread not found"});return}let z=me().slice(0,8),N=[];for(let y=0;y<w.length;y++){let M=re(a,`reply-${z}-${y}.png`);await Se(M,w[y]),N.push(M)}let T="";{let y=await E.getThreadHistory(d);for(let M=y.length-1;M>=0;M--)if(y[M].screenshotPath){T=y[M].screenshotPath;break}}if(!T){m(c,400,{error:"No screenshot available"});return}await E.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:z,replyToQuestion:p,screenshotPath:T});let I=await E.getThreadHistory(d),H=[];for(let y of I)if(y.annotationIds)for(let M of y.annotationIds)H.includes(M)||H.push(M);let ee=$==="claude"||$==="codex"?$:void 0,ie=bt(T,I,ee,N.length>0?N:void 0),J={id:z,status:"queued",screenshotPath:T,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:S,threadId:d,annotationIds:H.length>0?H:void 0,provider:ee,model:k||void 0,sourceId:D||void 0};J._replyPrompt=ie,N.length>0&&(J._replyImagePaths=N);let se=v.enqueue(J);m(c,200,{jobId:z,position:se,threadId:d})}function be(s,c){c.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),c.write(`event: connected
128
+ IMPORTANT: First, use the Read tool to view the updated screenshot at: ${s.screenshotPath}`:"");else{let $=!c&&s.threadId?await O.getThreadHistory(s.threadId):void 0,y=c?null:await E.loadModel();S=c!=null?c:xt(s.screenshotPath,s.feedback,{threadHistory:$&&$.length>0?$:void 0,provider:d,imagePaths:s.imagePaths,designModel:y!=null?y:void 0})}let R=Dn(s.color,`[\u22B9 ${C}:${s.id}]`);console.log(`${R} Reviewing feedback ${s.screenshotPath} (provider: ${d})${s.threadId?` (thread: ${s.threadId})`:""}${p?` (resuming: ${p.slice(0,8)})`:""}`);let k=!!s._isPlanExecutor,D="",w=0,Y=($,y)=>{if(v.broadcast($,y,s.sourceId),k&&$.type==="delta"&&"text"in $){D+=$.text;let M=Rt(D);if(M.length>w){let te=M.slice(w);w=M.length,v.broadcast({type:"task_resolved",jobId:y,planId:s.planId,resolutions:te,threadId:s.threadId},y,s.sourceId)}}},z=(J=s._allowedTools)!=null?J:h,{process:N,result:T}=d==="codex"?ct(s.id,{prompt:S,projectRoot:t,screenshotPath:s.screenshotPath,resumeSessionId:p,model:s.model,onEvent:Y}):Ee(s.id,{prompt:S,projectRoot:t,maxTurns:i,maxBudgetUsd:l,allowedTools:z,claudePath:f,resumeSessionId:p,model:s.model,timeoutMs:A,onEvent:Y});v.setActiveProcess(s.id,N);let I=await T;if(s.result=I.text,I.success){console.log(`${R} Iteration complete`),I.fileEdits&&I.fileEdits.length>0&&console.log(`${R} Captured ${I.fileEdits.length} file edit(s): ${I.fileEdits.map(F=>`${F.tool} ${F.file_path}`).join(", ")}`),s.status="done";let $=Pt(I.text),y=Tt(I.text);if(y.length>0&&s.annotationIds&&s.annotationIds.length>0){let F=new Set(s.annotationIds);y.every(ce=>F.has(ce.annotationId))||(y=y.map((ce,he)=>oe(V({},ce),{annotationId:s.annotationIds[he%s.annotationIds.length]})))}let M=I.fileEdits&&I.fileEdits.length>0?I.fileEdits.map(F=>`${F.tool} ${F.file_path.split("/").pop()}`):void 0;if(s.threadId&&await O.appendMessage(s.threadId,{role:"assistant",timestamp:Date.now(),jobId:s.id,responseText:I.text,resolutions:y.length>0?y:void 0,question:$!=null?$:void 0,sessionId:I.sessionId,toolsUsed:M}),Z.captureGitDiff(t).then(async F=>{var rt;let Q=Date.now(),ce=s.imagePaths?Object.values(s.imagePaths).flat():[],he=[];if(s.imagePaths)for(let[Ht,Wt]of Object.entries(s.imagePaths))for(let Ue=0;Ue<Wt.length;Ue++)he.push(`screenshots/p-${s.id}-${Ht}-${Ue}.png`);await Z.persist({version:1,id:s.id,createdAt:s.createdAt,completedAt:Q,durationMs:Q-s.createdAt,url:s.feedback.url,viewport:s.feedback.viewport,screenshotPath:`screenshots/s-${s.id}.png`,pastedImagePaths:he,annotations:s.feedback.annotations,styleModifications:s.feedback.styleModifications,inspectedElement:s.feedback.inspectedElement,provider:s.provider,model:s.model,sessionId:I.sessionId,threadId:s.threadId,planId:s.planId,planTaskId:s.planTaskId,responseText:I.text,resolutions:y.length>0?y:[],question:$!=null?$:void 0,fileEdits:(rt=I.fileEdits)!=null?rt:[],toolsUsed:M,gitDiff:F},s.screenshotPath,ce)}).catch(()=>{}),y.length>0&&y.some(Q=>{var he;let ce=(he=Q.finalScope)!=null?he:Q.inferredScope;return(ce==null?void 0:ce.breadth)==="pattern"})&&E.run().catch(()=>{}),s.planId&&!s.planTaskId){let F=X.get(s.planId);if(F){let Q=Ct(I.text);Q&&Q.length>0?(F.plan=Q,F.status="awaiting_approval",F.plannerThreadId=s.threadId,console.log(`${R} Plan ready: ${Q.length} tasks for group ${s.planId}`),v.broadcast({type:"plan_ready",jobId:s.id,planId:s.planId,tasks:Q,threadId:s.threadId},s.id,s.sourceId)):$||(F.status="error",console.error(`${R} Failed to parse plan from planner response`))}}if(s.planId&&s._isReview){let F=X.get(s.planId);if(F){let Q=At(I.text);Q&&(F.status=Q.verdict==="pass"?"done":"executing",console.log(`${R} Review verdict: ${Q.verdict} \u2014 ${Q.summary}`),v.broadcast({type:"plan_review",planId:s.planId,verdict:Q.verdict,summary:Q.summary,issues:Q.issues},s.id,s.sourceId))}}$&&(console.log(`${R} \u{1F4AC} Question detected: "${$.slice(0,120)}" \u2192 broadcasting to ${L.size} SSE clients (threadId=${(se=s.threadId)!=null?se:s.id}, annotationIds=${(Ie=(de=s.annotationIds)==null?void 0:de.join(","))!=null?Ie:"none"})`),v.broadcast({type:"question",jobId:s.id,threadId:(pe=s.threadId)!=null?pe:s.id,question:$,annotationIds:s.annotationIds},s.id,s.sourceId));let te=$t(I.text);te.length>0&&(console.log(`${R} Novel pattern(s): ${te.map(F=>`${F.category}/${F.element}`).join(", ")}`),v.broadcast({type:"novel_patterns",jobId:s.id,patterns:te,threadId:s.threadId},s.id,s.sourceId)),v.broadcast({type:"done",jobId:s.id,success:!0,resolutions:y.length>0?y:void 0,responseText:I.text,threadId:s.threadId},s.id,s.sourceId),P.push({id:s.id,status:"done",completedAt:Date.now(),threadId:s.threadId})}else console.error(`${R} Error: ${I.error}`),s.status="error",s.error=I.error,v.broadcast({type:"error",jobId:s.id,message:I.error||"Unknown error"},s.id,s.sourceId),P.push({id:s.id,status:"error",completedAt:Date.now(),error:I.error,threadId:s.threadId});let H=Date.now()-ne;for(;P.length>0&&(P[0].completedAt<H||P.length>U);)P.shift()});let b=kn(async(s,c)=>{if(An(s,c),s.method==="OPTIONS"){c.writeHead(204),c.end();return}let d=new URL(s.url||"/",`http://127.0.0.1:${C}`).pathname;try{if(s.method==="POST"&&d==="/send")await K(s,c);else if(s.method==="GET"&&d==="/events")be(s,c);else if(s.method==="GET"&&d==="/status")ke(c);else if(s.method==="GET"&&d==="/capabilities")m(c,200,{providers:g});else if(s.method==="POST"&&d==="/mcp/install")await B(s,c);else if(s.method==="POST"&&d==="/reply")await ge(s,c);else if(s.method==="POST"&&d==="/cancel")Te(s,c);else if(s.method==="POST"&&d==="/materialize")await Re(c);else if(s.method==="POST"&&d==="/plan")await $e(s,c);else if(s.method==="POST"&&d==="/plan/approve")await Me(s,c);else if(s.method==="POST"&&d==="/plan/execute")await Ce(s,c);else if(s.method==="POST"&&d==="/plan/review")await q(s,c);else if(s.method==="GET"&&d.startsWith("/plan/"))Be(d.slice(6),c);else if(s.method==="POST"&&d==="/model/component")await Oe(s,c);else if(s.method==="DELETE"&&d==="/model/component")await Nt(s,c);else if(s.method==="PATCH"&&d==="/model/token")await Bt(s,c);else if(s.method==="DELETE"&&d==="/model/token")await Ut(s,c);else if(s.method==="GET"&&d==="/model"){let p=await E.loadModel();m(c,200,{model:p})}else if(s.method==="GET"&&d.startsWith("/thread/")){let p=d.slice(8);await Gt(p,c)}else s.method==="GET"&&(d==="/canvas"||d==="/canvas/")?Ft(s,c):s.method==="GET"&&d==="/canvas/manifest"?await Lt(c):s.method==="GET"&&d==="/canvas/app.mjs"?await zt(c):m(c,404,{error:"Not found"})}catch(p){console.error("[Bridge] Request error:",p),m(c,500,{error:p instanceof Error?p.message:"Internal error"})}});async function K(s,c){let{screenshot:u,feedback:d,color:p,provider:S,model:R,sourceId:k,pastedImages:D}=await ve(s),w;try{w=JSON.parse(d)}catch(J){m(c,400,{error:"Invalid feedback JSON"});return}let Y=me().slice(0,8),z=re(a,`screenshot-${Y}.png`);await Se(z,u);let N={};if(D.length>0)for(let J of D){let se=re(a,`pasted-${Y}-${J.annotationId}-${J.index}.png`);await Se(se,J.data),N[J.annotationId]||(N[J.annotationId]=[]),N[J.annotationId].push(se)}let T=w.annotations.map(J=>J.linkedSelector).filter(J=>!!J),I;if(T.length>0){let J=await O.findContinuationThread(T);J?(I=J.id,await O.addElementIdentifiers(I,T)):I=(await O.createThread(Y,T)).id}let H=w.annotations.map(J=>J.id),ee=oe(V({id:Y,status:"queued",screenshotPath:z,feedback:w,createdAt:Date.now(),color:p,threadId:I,annotationIds:H,provider:S==="claude"||S==="codex"?S:void 0,model:R||void 0},Object.keys(N).length>0?{imagePaths:N}:{}),{sourceId:k||void 0});if(I){let J=w.annotations.map(de=>de.instruction||`[${de.type}]`).join("; "),se=Pe(w,Object.keys(N).length>0?N:void 0);await O.appendMessage(I,{role:"human",timestamp:Date.now(),jobId:Y,screenshotPath:z,annotationIds:H,feedbackSummary:J,feedbackContext:se||void 0})}let ie=v.enqueue(ee);m(c,200,{jobId:Y,position:ie,threadId:I})}async function ge(s,c){let u=s.headers["content-type"]||"",d,p,S,R,k,D,w=[];if(u.includes("multipart/form-data")){let y=await ve(s),M=y.feedback?JSON.parse(y.feedback):{};d=M.threadId,p=M.reply,S=M.color,R=M.provider,k=M.model,D=M.sourceId||y.sourceId;for(let te of y.pastedImages)w.push(te.data)}else{let y=[];try{for(var de=ue(s),Ie,pe,$;Ie=!(pe=await de.next()).done;Ie=!1){let F=pe.value;y.push(typeof F=="string"?Buffer.from(F):F)}}catch(pe){$=[pe]}finally{try{Ie&&(pe=de.return)&&await pe.call(de)}finally{if($)throw $[0]}}let M=Buffer.concat(y).toString("utf-8"),te;try{te=JSON.parse(M)}catch(F){m(c,400,{error:"Invalid JSON"});return}d=te.threadId,p=te.reply,S=te.color,R=te.provider,k=te.model,D=te.sourceId}if(!d||!p){m(c,400,{error:"Missing threadId or reply"});return}if(!await O.getThread(d)){m(c,404,{error:"Thread not found"});return}let z=me().slice(0,8),N=[];for(let y=0;y<w.length;y++){let M=re(a,`reply-${z}-${y}.png`);await Se(M,w[y]),N.push(M)}let T="";{let y=await O.getThreadHistory(d);for(let M=y.length-1;M>=0;M--)if(y[M].screenshotPath){T=y[M].screenshotPath;break}}if(!T){m(c,400,{error:"No screenshot available"});return}await O.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:z,replyToQuestion:p,screenshotPath:T});let I=await O.getThreadHistory(d),H=[];for(let y of I)if(y.annotationIds)for(let M of y.annotationIds)H.includes(M)||H.push(M);let ee=R==="claude"||R==="codex"?R:void 0,ie=bt(T,I,ee,N.length>0?N:void 0),J={id:z,status:"queued",screenshotPath:T,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:S,threadId:d,annotationIds:H.length>0?H:void 0,provider:ee,model:k||void 0,sourceId:D||void 0};J._replyPrompt=ie,N.length>0&&(J._replyImagePaths=N);let se=v.enqueue(J);m(c,200,{jobId:z,position:se,threadId:d})}function be(s,c){c.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),c.write(`event: connected
129
129
  data: {"status":"connected"}
130
130
 
131
- `),!o&&s.headers.origin&&_t(s.headers.origin)&&(o=s.headers.origin);let d=new URL(s.url||"/",`http://127.0.0.1:${C}`).searchParams.get("sourceId")||void 0,p={id:me().slice(0,8),res:c,sourceId:d};L.add(p),s.on("close",()=>{L.delete(p)})}function ke(s){let c=v.allActive;m(s,200,{ok:!0,projectId:e,devOrigin:o,activeJob:c[0]?{id:c[0].id,status:c[0].status}:null,activeJobs:c.map(u=>({id:u.id,status:u.status})),queueDepth:v.depth,recentJobs:P})}function Te(s,c){let d=new URL(s.url||"/",`http://127.0.0.1:${C}`).searchParams.get("jobId"),p=d?v.cancelJob(d):v.cancelActive();m(c,200,{cancelled:p})}async function $e(s){if(O.isRunning){m(s,200,{skipped:!0,reason:"Already running"});return}let c=await O.getUnmaterializedPatternDecisions();if(c.length===0){m(s,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}O.run().catch(()=>{}),m(s,200,{started:!0,decisionCount:c.length,decisionIds:c.map(u=>u.id)})}async function Re(s,c){let{screenshot:u,feedback:d,goal:p,pageUrl:S,viewport:$,provider:k,model:D,manifest:w,sourceId:Y}=await ve(s);if(!u||!p){m(c,400,{error:"Missing screenshot or goal"});return}let z=S||"",N={width:1440,height:900};try{$&&(N=JSON.parse($))}catch(y){}let T;if(d)try{let y=JSON.parse(d),M=Pe(y);M&&(T=M)}catch(y){}let I=me().slice(0,8),H=me().slice(0,8),ee=re(a,`screenshot-plan-${I}.png`);await Se(ee,u);let J=(await E.createThread(H,[])).id,se={id:I,goal:p,status:"planning",plannerJobId:H,plannerThreadId:J,workerJobIds:[],screenshotPath:ee,pageUrl:z,viewport:N,createdAt:Date.now()};X.set(I,se);let de=Mt(ee,p,z,N,w,T);await E.appendMessage(J,{role:"human",timestamp:Date.now(),jobId:H,screenshotPath:ee,feedbackSummary:`Plan: ${p}`,feedbackContext:`Goal: ${p}
132
- Page: ${z}`});let Ie=k==="claude"||k==="codex"?k:_,pe={id:H,status:"queued",screenshotPath:ee,feedback:{timestamp:new Date().toISOString(),url:z,viewport:N,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:J,provider:Ie,model:D||void 0,planId:I,sourceId:Y||void 0};pe._replyPrompt=de,pe._allowedTools=["Read"];let R=v.enqueue(pe);m(c,200,{planId:I,jobId:H,position:R,threadId:J})}async function Me(s,c){let u=[];try{for(var w=ue(s),Y,z,N;Y=!(z=await w.next()).done;Y=!1){let T=z.value;u.push(typeof T=="string"?Buffer.from(T):T)}}catch(z){N=[z]}finally{try{Y&&(z=w.return)&&await z.call(w)}finally{if(N)throw N[0]}}let d=Buffer.concat(u).toString("utf-8"),p;try{p=JSON.parse(d)}catch(T){m(c,400,{error:"Invalid JSON"});return}let{planId:S,approvedTaskIds:$}=p;if(!S){m(c,400,{error:"Missing planId"});return}let k=X.get(S);if(!k){m(c,404,{error:"Plan not found"});return}if(!k.plan){m(c,400,{error:"Plan has no tasks"});return}let D=$?k.plan.filter(T=>$.includes(T.id)):k.plan;k.status="executing",m(c,200,{planId:S,tasks:D,status:"executing"})}async function Ce(s,c){let{screenshot:u,planId:d,tasks:p,provider:S,model:$,sourceId:k}=await ve(s);if(!d||!p||!u){m(c,400,{error:"Missing planId, tasks, or screenshot"});return}let D=X.get(d);if(!D){m(c,404,{error:"Plan not found"});return}if(D.status!=="executing"){m(c,400,{error:`Plan status is ${D.status}, expected executing`});return}let w;try{w=JSON.parse(p)}catch(ie){m(c,400,{error:"Invalid tasks JSON"});return}let Y=me().slice(0,8),z=re(a,`screenshot-exec-${d}.png`);await Se(z,u);let N=S==="claude"||S==="codex"?S:_,T=Ot(z,w,D.pageUrl,D.viewport,N),I=w.map(ie=>ie.annotationId),H={id:Y,status:"queued",screenshotPath:z,feedback:{timestamp:new Date().toISOString(),url:D.pageUrl,viewport:D.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:N,model:$||void 0,planId:d,annotationIds:I,sourceId:k||void 0};H._replyPrompt=T,H._isPlanExecutor=!0,D.executorJobId=Y;let ee=v.enqueue(H);m(c,200,{jobId:Y,planId:d,position:ee})}async function q(s,c){let{screenshot:u,planId:d,provider:p,model:S,sourceId:$}=await ve(s);if(!d){m(c,400,{error:"Missing planId"});return}let k=X.get(d);if(!k){m(c,404,{error:"Plan not found"});return}k.status="reviewing";let D=me().slice(0,8),w=k.screenshotPath;u&&(w=re(a,`screenshot-review-${d}.png`),await Se(w,u));let Y=(k.plan||[]).map(H=>({id:H.id,instruction:H.instruction,summary:"completed"})),z=Et(w,k.goal,Y),N=p==="claude"||p==="codex"?p:_,T={id:D,status:"queued",screenshotPath:w,feedback:{timestamp:new Date().toISOString(),url:k.pageUrl,viewport:k.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:N,model:S||void 0,planId:d,sourceId:$||void 0};T._replyPrompt=z,T._isReview=!0,T._allowedTools=["Read"];let I=v.enqueue(T);m(c,200,{jobId:D,planId:d,position:I})}function Be(s,c){let u=X.get(s);if(!u){m(c,404,{error:"Plan not found"});return}m(c,200,u)}async function B(s,c){var z,N;let u=[];try{for(var k=ue(s),D,w,Y;D=!(w=await k.next()).done;D=!1){let T=w.value;u.push(typeof T=="string"?Buffer.from(T):T)}}catch(w){Y=[w]}finally{try{D&&(w=k.return)&&await w.call(k)}finally{if(Y)throw Y[0]}}let d;if(u.length>0)try{d=JSON.parse(Buffer.concat(u).toString("utf-8")).serverUrl}catch(T){}let p=[];(z=g.claude)!=null&&z.available&&g.claude.mcp&&!g.claude.mcp.found&&p.push(await St(d)),(N=g.codex)!=null&&N.available&&g.codex.mcp&&!g.codex.mcp.found&&p.push(await It(d));let[S,$]=await Promise.all([je(t),He(t)]);g.claude&&(g.claude.mcp=S),g.codex&&(g.codex.mcp=$),m(c,200,{results:p,capabilities:{providers:g}})}async function Ee(s,c){let u=[];try{for(var S=ue(s),$,k,D;$=!(k=await S.next()).done;$=!1){let w=k.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(k){D=[k]}finally{try{$&&(k=S.return)&&await k.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){m(c,400,{error:"Missing or invalid name"});return}let p=await O.addComponent(d.name);m(c,200,p)}async function Nt(s,c){let u=[];try{for(var S=ue(s),$,k,D;$=!(k=await S.next()).done;$=!1){let w=k.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(k){D=[k]}finally{try{$&&(k=S.return)&&await k.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){m(c,400,{error:"Missing or invalid name"});return}let p=await O.removeComponent(d.name);m(c,200,p)}async function Bt(s,c){let u=[];try{for(var S=ue(s),$,k,D;$=!(k=await S.next()).done;$=!1){let w=k.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(k){D=[k]}finally{try{$&&(k=S.return)&&await k.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){m(c,400,{error:"Missing or invalid path/value"});return}let p=await O.updateToken(d.path,d.value);m(c,200,p)}async function Ut(s,c){let u=[];try{for(var S=ue(s),$,k,D;$=!(k=await S.next()).done;$=!1){let w=k.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(k){D=[k]}finally{try{$&&(k=S.return)&&await k.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){m(c,400,{error:"Missing or invalid path"});return}let p=await O.removeToken(d.path);m(c,200,p)}function Ft(s,c){let u=o!=null?o:"http://localhost:3000";if(!o){let p=s.headers.referer||s.headers.origin;if(p)try{let S=new URL(typeof p=="string"?p:p[0]||"");(S.hostname==="localhost"||S.hostname==="127.0.0.1")&&(u=S.origin)}catch(S){}}let d=at(C,u);c.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),c.end(d)}async function Lt(s){let c=Date.now();if(x&&c<x.expires){m(s,200,x.data);return}try{let{scanForComponents:u}=await import("./react-scanner-MSMGKCIV.mjs"),{generateRenderFiles:d}=await import("./render-generator-QV5BYGPF.mjs"),p=await u(t);x={data:p,expires:c+5e3},d(p,t,j).then(S=>{j=S}).catch(S=>console.warn("[Bridge] Render generation failed:",S)),m(s,200,p)}catch(u){console.error("[Bridge] Scanner error:",u),m(s,500,{error:"Failed to scan components"})}}async function zt(s){let c=[re(t,"node_modules","@popmelt.com","core","dist","canvas.mjs"),re(t,"packages","popmelt","dist","canvas.mjs")];try{let u=Rn(Nn.url);c.unshift(re($n(u),"canvas.mjs"))}catch(u){}for(let u of c)try{let d=await In(u,"utf-8");s.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),s.end(d);return}catch(d){}console.error("[Bridge] Canvas bundle not found in:",c),m(s,404,{error:"Canvas bundle not found"})}async function Gt(s,c){let u=await E.getThread(s);if(!u){m(c,404,{error:"Thread not found"});return}let d=u.messages.map($=>{var k=$,{screenshotPath:p}=k,S=it(k,["screenshotPath"]);return S});m(c,200,{id:u.id,createdAt:u.createdAt,messages:d})}let Qe=9,qe=!1;for(let s=n;s<n+Qe;s++)try{await _n(b,s),C=s,qe=!0,console.log(`[\u22B9 is watching :${C}]`);break}catch(c){if(c.code==="EADDRINUSE"){let u=await Jn(s);if(u&&u.projectId===e)return console.log(`[\u22B9 already watching :${s}]`),{port:s,projectId:e,close:async()=>{}};continue}throw c}if(!qe)throw new Error(`[Bridge] All ports ${n}\u2013${n+Qe-1} in use`);let jt=setInterval(()=>{Jt(a).catch(()=>{})},En);return{port:C,projectId:e,close:async()=>{clearInterval(jt),await v.destroyAsync();for(let s of L)try{s.res.end()}catch(c){}return L.clear(),new Promise(s=>{b.close(()=>s())})}}}async function Jt(r){try{let n=await xn(r),t=Date.now();for(let e of n){let o=re(r,e);try{let a=await Pn(o);t-a.mtimeMs>On&&await bn(o)}catch(a){}}}catch(n){}}var le="\x1B[35m[popmelt]\x1B[0m";async function Un(){let r=process.argv.slice(2);if(r[0]==="wrap"){let n=r.indexOf("--");(n===-1||n===r.length-1)&&(console.error(`${le} Usage: popmelt wrap -- <dev command>`),console.error(`${le} Example: popmelt wrap -- next dev`),console.error(`${le} Example: popmelt wrap -- astro dev`),process.exit(1));let t=r.slice(n+1);await Ln(t);return}if(r[0]==="bridge"){await Fn();return}console.log(`${le} 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 Fn(){let r=await Ye({projectRoot:process.cwd()});console.log(`${le} Bridge running on http://localhost:${r.port}`),await new Promise(n=>{let t=async()=>{console.log(`
131
+ `),!o&&s.headers.origin&&_t(s.headers.origin)&&(o=s.headers.origin);let d=new URL(s.url||"/",`http://127.0.0.1:${C}`).searchParams.get("sourceId")||void 0,p={id:me().slice(0,8),res:c,sourceId:d};L.add(p),s.on("close",()=>{L.delete(p)})}function ke(s){let c=v.allActive;m(s,200,{ok:!0,projectId:e,devOrigin:o,activeJob:c[0]?{id:c[0].id,status:c[0].status}:null,activeJobs:c.map(u=>({id:u.id,status:u.status})),queueDepth:v.depth,recentJobs:P})}function Te(s,c){let d=new URL(s.url||"/",`http://127.0.0.1:${C}`).searchParams.get("jobId"),p=d?v.cancelJob(d):v.cancelActive();m(c,200,{cancelled:p})}async function Re(s){if(E.isRunning){m(s,200,{skipped:!0,reason:"Already running"});return}let c=await E.getUnmaterializedPatternDecisions();if(c.length===0){m(s,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}E.run().catch(()=>{}),m(s,200,{started:!0,decisionCount:c.length,decisionIds:c.map(u=>u.id)})}async function $e(s,c){let{screenshot:u,feedback:d,goal:p,pageUrl:S,viewport:R,provider:k,model:D,manifest:w,sourceId:Y}=await ve(s);if(!u||!p){m(c,400,{error:"Missing screenshot or goal"});return}let z=S||"",N={width:1440,height:900};try{R&&(N=JSON.parse(R))}catch(y){}let T;if(d)try{let y=JSON.parse(d),M=Pe(y);M&&(T=M)}catch(y){}let I=me().slice(0,8),H=me().slice(0,8),ee=re(a,`screenshot-plan-${I}.png`);await Se(ee,u);let J=(await O.createThread(H,[])).id,se={id:I,goal:p,status:"planning",plannerJobId:H,plannerThreadId:J,workerJobIds:[],screenshotPath:ee,pageUrl:z,viewport:N,createdAt:Date.now()};X.set(I,se);let de=Mt(ee,p,z,N,w,T);await O.appendMessage(J,{role:"human",timestamp:Date.now(),jobId:H,screenshotPath:ee,feedbackSummary:`Plan: ${p}`,feedbackContext:`Goal: ${p}
132
+ Page: ${z}`});let Ie=k==="claude"||k==="codex"?k:_,pe={id:H,status:"queued",screenshotPath:ee,feedback:{timestamp:new Date().toISOString(),url:z,viewport:N,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:J,provider:Ie,model:D||void 0,planId:I,sourceId:Y||void 0};pe._replyPrompt=de,pe._allowedTools=["Read"];let $=v.enqueue(pe);m(c,200,{planId:I,jobId:H,position:$,threadId:J})}async function Me(s,c){let u=[];try{for(var w=ue(s),Y,z,N;Y=!(z=await w.next()).done;Y=!1){let T=z.value;u.push(typeof T=="string"?Buffer.from(T):T)}}catch(z){N=[z]}finally{try{Y&&(z=w.return)&&await z.call(w)}finally{if(N)throw N[0]}}let d=Buffer.concat(u).toString("utf-8"),p;try{p=JSON.parse(d)}catch(T){m(c,400,{error:"Invalid JSON"});return}let{planId:S,approvedTaskIds:R}=p;if(!S){m(c,400,{error:"Missing planId"});return}let k=X.get(S);if(!k){m(c,404,{error:"Plan not found"});return}if(!k.plan){m(c,400,{error:"Plan has no tasks"});return}let D=R?k.plan.filter(T=>R.includes(T.id)):k.plan;k.status="executing",m(c,200,{planId:S,tasks:D,status:"executing"})}async function Ce(s,c){let{screenshot:u,planId:d,tasks:p,provider:S,model:R,sourceId:k}=await ve(s);if(!d||!p||!u){m(c,400,{error:"Missing planId, tasks, or screenshot"});return}let D=X.get(d);if(!D){m(c,404,{error:"Plan not found"});return}if(D.status!=="executing"){m(c,400,{error:`Plan status is ${D.status}, expected executing`});return}let w;try{w=JSON.parse(p)}catch(ie){m(c,400,{error:"Invalid tasks JSON"});return}let Y=me().slice(0,8),z=re(a,`screenshot-exec-${d}.png`);await Se(z,u);let N=S==="claude"||S==="codex"?S:_,T=Et(z,w,D.pageUrl,D.viewport,N),I=w.map(ie=>ie.annotationId),H={id:Y,status:"queued",screenshotPath:z,feedback:{timestamp:new Date().toISOString(),url:D.pageUrl,viewport:D.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:N,model:R||void 0,planId:d,annotationIds:I,sourceId:k||void 0};H._replyPrompt=T,H._isPlanExecutor=!0,D.executorJobId=Y;let ee=v.enqueue(H);m(c,200,{jobId:Y,planId:d,position:ee})}async function q(s,c){let{screenshot:u,planId:d,provider:p,model:S,sourceId:R}=await ve(s);if(!d){m(c,400,{error:"Missing planId"});return}let k=X.get(d);if(!k){m(c,404,{error:"Plan not found"});return}k.status="reviewing";let D=me().slice(0,8),w=k.screenshotPath;u&&(w=re(a,`screenshot-review-${d}.png`),await Se(w,u));let Y=(k.plan||[]).map(H=>({id:H.id,instruction:H.instruction,summary:"completed"})),z=Ot(w,k.goal,Y),N=p==="claude"||p==="codex"?p:_,T={id:D,status:"queued",screenshotPath:w,feedback:{timestamp:new Date().toISOString(),url:k.pageUrl,viewport:k.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:N,model:S||void 0,planId:d,sourceId:R||void 0};T._replyPrompt=z,T._isReview=!0,T._allowedTools=["Read"];let I=v.enqueue(T);m(c,200,{jobId:D,planId:d,position:I})}function Be(s,c){let u=X.get(s);if(!u){m(c,404,{error:"Plan not found"});return}m(c,200,u)}async function B(s,c){var z,N;let u=[];try{for(var k=ue(s),D,w,Y;D=!(w=await k.next()).done;D=!1){let T=w.value;u.push(typeof T=="string"?Buffer.from(T):T)}}catch(w){Y=[w]}finally{try{D&&(w=k.return)&&await w.call(k)}finally{if(Y)throw Y[0]}}let d;if(u.length>0)try{d=JSON.parse(Buffer.concat(u).toString("utf-8")).serverUrl}catch(T){}let p=[];(z=g.claude)!=null&&z.available&&g.claude.mcp&&!g.claude.mcp.found&&p.push(await St(d)),(N=g.codex)!=null&&N.available&&g.codex.mcp&&!g.codex.mcp.found&&p.push(await It(d));let[S,R]=await Promise.all([je(t),He(t)]);g.claude&&(g.claude.mcp=S),g.codex&&(g.codex.mcp=R),m(c,200,{results:p,capabilities:{providers:g}})}async function Oe(s,c){let u=[];try{for(var S=ue(s),R,k,D;R=!(k=await S.next()).done;R=!1){let w=k.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(k){D=[k]}finally{try{R&&(k=S.return)&&await k.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){m(c,400,{error:"Missing or invalid name"});return}let p=await E.addComponent(d.name);m(c,200,p)}async function Nt(s,c){let u=[];try{for(var S=ue(s),R,k,D;R=!(k=await S.next()).done;R=!1){let w=k.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(k){D=[k]}finally{try{R&&(k=S.return)&&await k.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){m(c,400,{error:"Missing or invalid name"});return}let p=await E.removeComponent(d.name);m(c,200,p)}async function Bt(s,c){let u=[];try{for(var S=ue(s),R,k,D;R=!(k=await S.next()).done;R=!1){let w=k.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(k){D=[k]}finally{try{R&&(k=S.return)&&await k.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){m(c,400,{error:"Missing or invalid path/value"});return}let p=await E.updateToken(d.path,d.value);m(c,200,p)}async function Ut(s,c){let u=[];try{for(var S=ue(s),R,k,D;R=!(k=await S.next()).done;R=!1){let w=k.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(k){D=[k]}finally{try{R&&(k=S.return)&&await k.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){m(c,400,{error:"Missing or invalid path"});return}let p=await E.removeToken(d.path);m(c,200,p)}function Ft(s,c){let u=o!=null?o:"http://localhost:3000";if(!o){let p=s.headers.referer||s.headers.origin;if(p)try{let S=new URL(typeof p=="string"?p:p[0]||"");(S.hostname==="localhost"||S.hostname==="127.0.0.1")&&(u=S.origin)}catch(S){}}let d=at(C,u);c.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),c.end(d)}async function Lt(s){let c=Date.now();if(x&&c<x.expires){m(s,200,x.data);return}try{let{scanForComponents:u}=await import("./react-scanner-MSMGKCIV.mjs"),{generateRenderFiles:d}=await import("./render-generator-QV5BYGPF.mjs"),p=await u(t);x={data:p,expires:c+5e3},d(p,t,j).then(S=>{j=S}).catch(S=>console.warn("[Bridge] Render generation failed:",S)),m(s,200,p)}catch(u){console.error("[Bridge] Scanner error:",u),m(s,500,{error:"Failed to scan components"})}}async function zt(s){let c=[re(t,"node_modules","@popmelt.com","core","dist","canvas.mjs"),re(t,"packages","popmelt","dist","canvas.mjs")];try{let u=$n(Nn.url);c.unshift(re(Rn(u),"canvas.mjs"))}catch(u){}for(let u of c)try{let d=await In(u,"utf-8");s.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),s.end(d);return}catch(d){}console.error("[Bridge] Canvas bundle not found in:",c),m(s,404,{error:"Canvas bundle not found"})}async function Gt(s,c){let u=await O.getThread(s);if(!u){m(c,404,{error:"Thread not found"});return}let d=u.messages.map(R=>{var k=R,{screenshotPath:p}=k,S=it(k,["screenshotPath"]);return S});m(c,200,{id:u.id,createdAt:u.createdAt,messages:d})}let Qe=9,qe=!1;for(let s=n;s<n+Qe;s++)try{await _n(b,s),C=s,qe=!0,console.log(`[\u22B9 is watching :${C}]`);break}catch(c){if(c.code==="EADDRINUSE"){let u=await Jn(s);if(u&&u.projectId===e)return console.log(`[\u22B9 already watching :${s}]`),{port:s,projectId:e,close:async()=>{}};continue}throw c}if(!qe)throw new Error(`[Bridge] All ports ${n}\u2013${n+Qe-1} in use`);let jt=setInterval(()=>{Jt(a).catch(()=>{})},On);return{port:C,projectId:e,close:async()=>{clearInterval(jt),await v.destroyAsync();for(let s of L)try{s.res.end()}catch(c){}return L.clear(),new Promise(s=>{b.close(()=>s())})}}}async function Jt(r){try{let n=await xn(r),t=Date.now();for(let e of n){let o=re(r,e);try{let a=await Pn(o);t-a.mtimeMs>En&&await bn(o)}catch(a){}}}catch(n){}}var le="\x1B[35m[popmelt]\x1B[0m";async function Un(){let r=process.argv.slice(2);if(r[0]==="wrap"){let n=r.indexOf("--");(n===-1||n===r.length-1)&&(console.error(`${le} Usage: popmelt wrap -- <dev command>`),console.error(`${le} Example: popmelt wrap -- next dev`),console.error(`${le} Example: popmelt wrap -- astro dev`),process.exit(1));let t=r.slice(n+1);await Ln(t);return}if(r[0]==="bridge"){await Fn();return}console.log(`${le} 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 Fn(){let r=await Ye({projectRoot:process.cwd()});console.log(`${le} Bridge running on http://localhost:${r.port}`),await new Promise(n=>{let t=async()=>{console.log(`
133
133
  ${le} Shutting down bridge...`),await r.close(),n()};process.on("SIGINT",t),process.on("SIGTERM",t)})}async function Ln(r){let n=await Ye({projectRoot:process.cwd()});console.log(`${le} Bridge running on http://localhost:${n.port}`);let[t,...e]=r;console.log(`${le} Starting: ${r.join(" ")}`);let o=Bn(t,e,{stdio:"inherit",shell:!0,env:oe(V({},process.env),{POPMELT_BRIDGE_URL:`http://localhost:${n.port}`})}),a=i=>{o.kill(i)};process.on("SIGINT",()=>a("SIGINT")),process.on("SIGTERM",()=>a("SIGTERM")),o.on("exit",async(i,l)=>{await n.close(),l?process.kill(process.pid,l):process.exit(i!=null?i:0)})}Un().catch(r=>{console.error(`${le} Fatal:`,r),process.exit(1)});