@popmelt.com/core 0.6.6 → 0.6.7

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{spawn as yn}from"child_process";import{readFile as vn,unlink as wn}from"fs/promises";import{join as Sn}from"path";import{execFileSync as Xt,spawn as Yt}from"child_process";import{createHash as Vt,randomUUID as Me}from"crypto";import{mkdir as Kt,readFile as st,readdir as Zt,stat as en,unlink as tn,writeFile as xe}from"fs/promises";import{createServer as nn}from"http";import{tmpdir as sn}from"os";import{basename as on,dirname as rn,join as se}from"path";import{fileURLToPath as an}from"url";function De(i,t){return`<!DOCTYPE html>
2
+ import{spawn as Pn}from"child_process";import{readFile as kn,unlink as In}from"fs/promises";import{join as Tn}from"path";import{execFileSync as nn,spawn as it}from"child_process";import{createHash as sn,randomUUID as Pe}from"crypto";import{mkdir as on,readFile as at,readdir as rn,stat as an,unlink as cn,writeFile as ke}from"fs/promises";import{createServer as ln}from"http";import{tmpdir as dn}from"os";import{basename as un,dirname as pn,join as se}from"path";import{fileURLToPath as hn}from"url";function Be(r,t){return`<!DOCTYPE html>
3
3
  <html>
4
4
  <head>
5
5
  <meta charset="utf-8">
@@ -19,30 +19,30 @@ import{spawn as yn}from"child_process";import{readFile as vn,unlink as wn}from"f
19
19
  }}
20
20
  </script>
21
21
  <script type="module">
22
- import { mountCanvas } from 'http://localhost:${i}/canvas/app.mjs';
22
+ import { mountCanvas } from 'http://localhost:${r}/canvas/app.mjs';
23
23
  mountCanvas(document.getElementById('root'), {
24
24
  devOrigin: '${t}',
25
- bridgeOrigin: 'http://localhost:${i}',
25
+ bridgeOrigin: 'http://localhost:${r}',
26
26
  });
27
27
  </script>
28
28
  </body>
29
- </html>`}import{spawn as xt}from"child_process";import{createInterface as Pt}from"readline";var It=new Set([".md",".txt",".json",".ts",".tsx",".js",".jsx",".css",".scss",".html",".xml",".yaml",".yml",".toml",".ini",".cfg",".conf",".sh",".bash",".zsh",".py",".rb",".go",".rs",".java",".c",".h",".cpp",".hpp",".swift",".kt",".sql",".graphql",".svg",".env",".gitignore",".prettierrc",".eslintrc"]),Ne=1e5;function bt(i){let t=i.input?.file_path||i.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!It.has(n))return;let e;if(i.name==="Write"&&typeof i.input?.content=="string"?e=i.input.content:i.name==="Edit"&&typeof i.input?.new_string=="string"&&(e=i.input.new_string),!!e)return e.length>Ne?e.slice(0,Ne)+`
30
- \u2026[truncated]`:e}function me(i,t){let{prompt:n,projectRoot:e,maxTurns:s=40,maxBudgetUsd:l=1,allowedTools:c=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:r="claude",resumeSessionId:h,model:f,timeoutMs:N=3e5,onEvent:U}=t,O=[];h?O.push("--resume",h,"-p",n):O.push("-p",n),O.push("--output-format","stream-json","--verbose","--max-turns",String(s),"--max-budget-usd",String(l)),f&&O.push("--model",f);for(let L of c)O.push("--allowedTools",L);let I=xt(r,O,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0}}),z=new Promise(L=>{let W,T=[],J=[],C=!1,B="",g=!1,F=setTimeout(()=>{g=!0,I.kill("SIGTERM"),setTimeout(()=>{try{I.kill("SIGKILL")}catch{}},5e3)},N),y=Pt({input:I.stdout}),w=new Set;y.on("line",D=>{if(D.trim())try{let A=JSON.parse(D);A.session_id&&!W&&(W=A.session_id);let ee=A.type??(A.event?.type?`event.${A.event.type}`:"unknown");if(w.add(ee),A.type==="result"&&A.result&&T.length===0){let $=typeof A.result=="string"?A.result:"";$&&(T.push($),U?.({type:"delta",jobId:i,text:$},i))}if(A.type==="assistant"&&Array.isArray(A.message?.content))for(let $ of A.message.content){if($.type==="text"&&$.text&&(T.push($.text),U?.({type:"delta",jobId:i,text:$.text},i)),$.type==="tool_use"&&$.name){let ce=$.input?.file_path||$.input?.path||void 0,he=bt($);U?.({type:"tool_use",jobId:i,tool:$.name,...ce?{file:ce}:{},...he?{content:he}:{}},i),$.name==="Edit"&&$.input?.file_path?J.push({tool:"Edit",file_path:$.input.file_path,old_string:$.input.old_string,new_string:$.input.new_string,replace_all:$.input.replace_all}):$.name==="Write"&&$.input?.file_path&&J.push({tool:"Write",file_path:$.input.file_path,content:$.input.content})}$.type==="thinking"&&$.thinking&&U?.({type:"thinking",jobId:i,text:$.thinking},i)}A.type==="user"&&A.tool_use_result?.file?.filePath&&U?.({type:"tool_use",jobId:i,tool:"Read",file:A.tool_use_result.file.filePath},i)}catch{}});let S=[];I.stderr?.on("data",D=>{S.push(D.toString())}),I.on("close",D=>{if(clearTimeout(F),y.close(),T.length===0&&w.size>0&&console.warn(`[Claude:${i}] No text captured. Event types seen: ${[...w].join(", ")}`),g)C=!0,B=`Timed out after ${Math.round(N/6e4)} minutes`;else if(D!==0&&D!==null){C=!0;let A=S.join("").trim(),ee=T.length===0&&w.size>0?` (no text captured, event types: ${[...w].join(", ")})`:"";B=A||`Claude process exited with code ${D}${ee}`}L({sessionId:W,text:T.join(""),success:!C,error:C?B:void 0,fileEdits:J.length>0?J:void 0})}),I.on("error",D=>{clearTimeout(F),C=!0,B=D.message,L({sessionId:W,text:T.join(""),success:!1,error:B,fileEdits:J.length>0?J:void 0})})});return{process:I,result:z}}import{spawn as kt}from"child_process";import{createInterface as Tt}from"readline";function Et(i){let t=i.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):i}function Be(i,t){let{prompt:n,projectRoot:e,screenshotPath:s,resumeSessionId:l,model:c,onEvent:r}=t,h=[];l?(h.push("exec","resume",l),c&&h.push("-m",c),h.push("--json","--full-auto",n),s&&h.push("--image",s)):(h.push("exec","--json","--full-auto"),c&&h.push("-m",c),h.push(n),s&&h.push("--image",s));let f=kt("codex",h,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env}}),N=new Promise(U=>{let O,I=[],z=[],L=!1,W="",T=Tt({input:f.stdout}),J=new Set;T.on("line",B=>{if(B.trim())try{let g=JSON.parse(B),F=g.type??"unknown";if(J.add(F),F==="thread.started"&&g.thread_id&&!O&&(O=g.thread_id),(F==="item.agentMessage.delta"||F==="item/agentMessage/delta")&&g.delta?.text&&(I.push(g.delta.text),r?.({type:"delta",jobId:i,text:g.delta.text},i)),(F==="item.reasoning.delta"||F==="item/reasoning/delta")&&g.delta?.text&&r?.({type:"thinking",jobId:i,text:g.delta.text},i),(F==="item.started"||F==="item/started")&&g.item){let y=g.item.type;if(y==="command_execution"){let w=g.item.command,S=w?Et(w):void 0,D=S?`Bash: ${S.split(`
31
- `)[0].slice(0,80)}`:"Bash";z.push(D),r?.({type:"tool_use",jobId:i,tool:"Bash",...S?{content:S}:{}},i)}else if(y==="file_change"){let w=g.item.filename||g.item.path;z.push(w?`Edit ${w.split("/").pop()}`:"Edit"),r?.({type:"tool_use",jobId:i,tool:"Edit",...w?{file:w}:{}},i)}else if(y==="file_read"){let w=g.item.filename||g.item.path;z.push(w?`Read ${w.split("/").pop()}`:"Read"),r?.({type:"tool_use",jobId:i,tool:"Read",...w?{file:w}:{}},i)}else if(y==="web_search")z.push("WebSearch"),r?.({type:"tool_use",jobId:i,tool:"WebSearch"},i);else if(y==="mcp_tool_call"){let w=g.item.tool_name||g.item.name||"MCP";z.push(w),r?.({type:"tool_use",jobId:i,tool:w},i)}}if((F==="item.completed"||F==="item/completed")&&g.item){if(g.item.type==="agent_message"){let y=g.item.text;typeof y=="string"&&y&&(I.push(y),r?.({type:"delta",jobId:i,text:y},i))}else if(g.item.type==="reasoning"){let y=g.item.text;typeof y=="string"&&y&&r?.({type:"thinking",jobId:i,text:y},i)}else if(g.item.type==="file_change"&&Array.isArray(g.item.changes))for(let y of g.item.changes){let w=y.path||y.filename,S=y.kind==="add"?"Write":"Edit";w&&(z.push(`${S} ${w.split("/").pop()}`),r?.({type:"tool_use",jobId:i,tool:S,file:w},i))}}F==="turn.failed"&&(L=!0,W=g.error?.message||g.message||"Turn failed")}catch{}});let C=[];f.stderr?.on("data",B=>{C.push(B.toString())}),f.on("close",B=>{T.close(),I.length===0&&J.size>0&&console.warn(`[Codex:${i}] No text captured. Event types seen: ${[...J].join(", ")}`),B!==0&&B!==null&&(L=!0,W=C.join("")||`Codex process exited with code ${B}`),U({sessionId:O,text:I.join(""),success:!L,error:L?W:void 0,toolsUsed:z.length>0?z:void 0})}),f.on("error",B=>{L=!0,W=B.message,U({sessionId:O,text:I.join(""),success:!1,error:W,toolsUsed:z.length>0?z:void 0})})});return{process:f,result:N}}import{execFile as $t}from"child_process";import{copyFile as Je,mkdir as Ue,readdir as Rt,readFile as Mt,writeFile as Ct}from"fs/promises";import{join as ae}from"path";var ge=class{constructor(t){this.projectRoot=t;let n=ae(t,".popmelt");this.decisionsDir=ae(n,"decisions"),this.screenshotsDir=ae(n,"screenshots")}async persist(t,n,e){try{await Ue(this.decisionsDir,{recursive:!0}),await Ue(this.screenshotsDir,{recursive:!0});try{await Je(n,ae(this.screenshotsDir,`s-${t.id}.png`))}catch{}for(let s=0;s<e.length;s++)try{let l=t.pastedImagePaths[s];l&&await Je(e[s],ae(this.screenshotsDir,l.replace("screenshots/","")))}catch{}await Ct(ae(this.decisionsDir,`d-${t.id}.json`),JSON.stringify(t,null,2))}catch(s){console.error("[DecisionStore] Failed to persist decision record:",s)}}async listDecisionIds(){try{return(await Rt(this.decisionsDir)).filter(n=>n.startsWith("d-")&&n.endsWith(".json")).map(n=>n.slice(2,-5))}catch{return[]}}async loadDecision(t){try{let n=await Mt(ae(this.decisionsDir,`d-${t}.json`),"utf-8");return JSON.parse(n)}catch{return null}}async loadDecisions(t){return(await Promise.all(t.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(t){return new Promise(n=>{$t("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(e,s)=>{if(e){n(null);return}n(s||null)})})}};import{readFile as Fe,writeFile as le}from"fs/promises";import{join as Pe}from"path";var ne="[Materializer]",Ot={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null},ye=class{constructor(t,n,e={}){this.projectRoot=t;this.decisionStore=n;this.options=e;this.cachedIndex=null;this.running=!1;let s=Pe(t,".popmelt");this.indexPath=Pe(s,"materialized.json"),this.modelPath=Pe(s,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await Fe(this.modelPath,"utf-8");return JSON.parse(t)}catch{return null}}async addComponent(t){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let e=n.components;return e[t]?{added:!1,alreadyExists:!0}:(e[t]={description:""},await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${ne} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,n){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let s=t.split("."),l=e;for(let h=0;h<s.length-1;h++){let f=s[h];(!l[f]||typeof l[f]!="object")&&(l[f]={}),l=l[f]}let c=s[s.length-1],r;try{r=JSON.parse(n)}catch{r=null}if(r&&typeof r=="object"&&r!==null&&"value"in r)l[c]=r;else{let h=l[c];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=n:l[c]=n}return await le(this.modelPath,JSON.stringify(e,null,2)),console.log(`${ne} Updated token "${t}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=t.split("."),s=n;for(let c=0;c<e.length-1;c++){let r=e[c];if(!s[r]||typeof s[r]!="object")return{removed:!1};s=s[r]}let l=e[e.length-1];return l in s?(delete s[l],await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${ne} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=n.components;return!e||!e[t]?{removed:!1}:(delete e[t],await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${ne} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),n=new Set(t.materializedIds),s=(await this.decisionStore.listDecisionIds()).filter(c=>!n.has(c));return s.length===0?[]:(await this.decisionStore.loadDecisions(s)).filter(c=>c.resolutions.some(r=>(r.finalScope??r.inferredScope)?.breadth==="pattern"))}async run(){if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let t=await this.getUnmaterializedPatternDecisions();if(t.length===0)return{processedIds:[],success:!0};let n=t.map(f=>f.id);console.log(`${ne} Processing ${n.length} pattern-scoped decision(s): ${n.join(", ")}`),this.options.onEvent?.({type:"materialize_started",decisionIds:n});let e=await this.loadModel(),s=_t(t,e),l=!0,c;try{let{result:f}=me(`mat-${Date.now()}`,{prompt:s,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"],claudePath:this.options.claudePath??"claude"}),N=await f;if(!N.success)l=!1,c=N.error,console.error(`${ne} Claude spawn error:`,c);else{let U=At(N.text);U?(await le(this.modelPath,JSON.stringify(U,null,2)),console.log(`${ne} Successfully materialized ${n.length} decision(s) \u2192 ${this.modelPath}`)):(l=!1,c="No <model> block found in response",console.error(`${ne} ${c}`))}}catch(f){l=!1,c=f instanceof Error?f.message:String(f),console.error(`${ne} Error:`,c)}let r=await this.loadIndex(),h=new Set(r.materializedIds);for(let f of n)h.add(f);return r.materializedIds=[...h],r.lastRunAt=Date.now(),r.lastRunDecisionIds=n,r.lastRunError=c??null,await this.persistIndex(r),this.options.onEvent?.({type:"materialize_done",decisionIds:n,success:l,error:c}),{processedIds:n,success:l,error:c}}finally{this.running=!1}}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await Fe(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...Ot,materializedIds:[],lastRunDecisionIds:[]},this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await le(this.indexPath,JSON.stringify(t,null,2))}catch(n){console.error(`${ne} Failed to write index:`,n)}}};function At(i){let t=i.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!t?.[1])return null;try{let n=JSON.parse(t[1]);return typeof n!="object"||n===null||Array.isArray(n)?null:n}catch{return null}}function _t(i,t){let n=i.map(s=>{let c=s.resolutions.filter(f=>(f.finalScope??f.inferredScope)?.breadth==="pattern").map(f=>{let U=(f.finalScope??f.inferredScope)?.target??"unknown",O=f.filesModified?.join(", ")??"none";return`- **${f.summary}** [scope: pattern/${U}]
32
- Files modified: ${O}`}).join(`
33
- `),r=s.annotations.map(f=>f.instruction).filter(Boolean).join(`
34
- `),h=s.gitDiff?`
29
+ </html>`}import{spawn as Rt}from"child_process";import{createInterface as $t}from"readline";var Et=new Set([".md",".txt",".json",".ts",".tsx",".js",".jsx",".css",".scss",".html",".xml",".yaml",".yml",".toml",".ini",".cfg",".conf",".sh",".bash",".zsh",".py",".rb",".go",".rs",".java",".c",".h",".cpp",".hpp",".swift",".kt",".sql",".graphql",".svg",".env",".gitignore",".prettierrc",".eslintrc"]),Je=1e5;function Mt(r){let t=r.input?.file_path||r.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!Et.has(n))return;let e;if(r.name==="Write"&&typeof r.input?.content=="string"?e=r.input.content:r.name==="Edit"&&typeof r.input?.new_string=="string"&&(e=r.input.new_string),!!e)return e.length>Je?e.slice(0,Je)+`
30
+ \u2026[truncated]`:e}function ue(r,t){let{prompt:n,projectRoot:e,maxTurns:s=40,maxBudgetUsd:i=1,allowedTools:l=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:h,model:m,timeoutMs:b=3e5,onEvent:O}=t,C=[];h?C.push("--resume",h,"-p",n):C.push("-p",n),C.push("--output-format","stream-json","--verbose","--max-turns",String(s),"--max-budget-usd",String(i)),m&&C.push("--model",m);for(let L of l)C.push("--allowedTools",L);let v=Rt(c,C,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),H=new Promise(L=>{let X,R=[],U=[],A=!1,F="",g=!1,j=setTimeout(()=>{g=!0,v.kill("SIGTERM"),setTimeout(()=>{try{v.kill("SIGKILL")}catch{}},5e3)},b),S=$t({input:v.stdout}),k=new Set;S.on("line",B=>{if(B.trim())try{let _=JSON.parse(B);_.session_id&&!X&&(X=_.session_id);let Z=_.type??(_.event?.type?`event.${_.event.type}`:"unknown");if(k.add(Z),_.type==="result"&&_.result&&R.length===0){let E=typeof _.result=="string"?_.result:"";E&&(R.push(E),O?.({type:"delta",jobId:r,text:E},r))}if(_.type==="assistant"&&Array.isArray(_.message?.content))for(let E of _.message.content){if(E.type==="text"&&E.text&&(R.push(E.text),O?.({type:"delta",jobId:r,text:E.text},r)),E.type==="tool_use"&&E.name){let le=E.input?.file_path||E.input?.path||void 0,me=Mt(E);O?.({type:"tool_use",jobId:r,tool:E.name,...le?{file:le}:{},...me?{content:me}:{}},r),E.name==="Edit"&&E.input?.file_path?U.push({tool:"Edit",file_path:E.input.file_path,old_string:E.input.old_string,new_string:E.input.new_string,replace_all:E.input.replace_all}):E.name==="Write"&&E.input?.file_path&&U.push({tool:"Write",file_path:E.input.file_path,content:E.input.content})}E.type==="thinking"&&E.thinking&&O?.({type:"thinking",jobId:r,text:E.thinking},r)}_.type==="user"&&_.tool_use_result?.file?.filePath&&O?.({type:"tool_use",jobId:r,tool:"Read",file:_.tool_use_result.file.filePath},r)}catch{}});let I=[];v.stderr?.on("data",B=>{I.push(B.toString())}),v.on("close",B=>{if(clearTimeout(j),S.close(),R.length===0&&k.size>0&&console.warn(`[Claude:${r}] No text captured. Event types seen: ${[...k].join(", ")}`),g)A=!0,F=`Timed out after ${Math.round(b/6e4)} minutes`;else if(B!==0&&B!==null){A=!0;let _=I.join("").trim(),Z=R.length===0&&k.size>0?` (no text captured, event types: ${[...k].join(", ")})`:"";F=_||`Claude process exited with code ${B}${Z}`}L({sessionId:X,text:R.join(""),success:!A,error:A?F:void 0,fileEdits:U.length>0?U:void 0})}),v.on("error",B=>{clearTimeout(j),A=!0,F=B.message,L({sessionId:X,text:R.join(""),success:!1,error:F,fileEdits:U.length>0?U:void 0})})});return{process:v,result:H}}import{spawn as Ct}from"child_process";import{createInterface as Ot}from"readline";function At(r){let t=r.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):r}function Ue(r,t){let{prompt:n,projectRoot:e,screenshotPath:s,resumeSessionId:i,model:l,onEvent:c}=t,h=[];i?(h.push("exec","resume",i),l&&h.push("-m",l),h.push("--json","--full-auto",n),s&&h.push("--image",s)):(h.push("exec","--json","--full-auto"),l&&h.push("-m",l),h.push(n),s&&h.push("--image",s));let m=Ct("codex",h,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),b=new Promise(O=>{let C,v=[],H=[],L=!1,X="",R=Ot({input:m.stdout}),U=new Set;R.on("line",F=>{if(F.trim())try{let g=JSON.parse(F),j=g.type??"unknown";if(U.add(j),j==="thread.started"&&g.thread_id&&!C&&(C=g.thread_id),(j==="item.agentMessage.delta"||j==="item/agentMessage/delta")&&g.delta?.text&&(v.push(g.delta.text),c?.({type:"delta",jobId:r,text:g.delta.text},r)),(j==="item.reasoning.delta"||j==="item/reasoning/delta")&&g.delta?.text&&c?.({type:"thinking",jobId:r,text:g.delta.text},r),(j==="item.started"||j==="item/started")&&g.item){let S=g.item.type;if(S==="command_execution"){let k=g.item.command,I=k?At(k):void 0,B=I?`Bash: ${I.split(`
31
+ `)[0].slice(0,80)}`:"Bash";H.push(B),c?.({type:"tool_use",jobId:r,tool:"Bash",...I?{content:I}:{}},r)}else if(S==="file_change"){let k=g.item.filename||g.item.path;H.push(k?`Edit ${k.split("/").pop()}`:"Edit"),c?.({type:"tool_use",jobId:r,tool:"Edit",...k?{file:k}:{}},r)}else if(S==="file_read"){let k=g.item.filename||g.item.path;H.push(k?`Read ${k.split("/").pop()}`:"Read"),c?.({type:"tool_use",jobId:r,tool:"Read",...k?{file:k}:{}},r)}else if(S==="web_search")H.push("WebSearch"),c?.({type:"tool_use",jobId:r,tool:"WebSearch"},r);else if(S==="mcp_tool_call"){let k=g.item.tool_name||g.item.name||"MCP";H.push(k),c?.({type:"tool_use",jobId:r,tool:k},r)}}if((j==="item.completed"||j==="item/completed")&&g.item){if(g.item.type==="agent_message"){let S=g.item.text;typeof S=="string"&&S&&(v.push(S),c?.({type:"delta",jobId:r,text:S},r))}else if(g.item.type==="reasoning"){let S=g.item.text;typeof S=="string"&&S&&c?.({type:"thinking",jobId:r,text:S},r)}else if(g.item.type==="file_change"&&Array.isArray(g.item.changes))for(let S of g.item.changes){let k=S.path||S.filename,I=S.kind==="add"?"Write":"Edit";k&&(H.push(`${I} ${k.split("/").pop()}`),c?.({type:"tool_use",jobId:r,tool:I,file:k},r))}}j==="turn.failed"&&(L=!0,X=g.error?.message||g.message||"Turn failed")}catch{}});let A=[];m.stderr?.on("data",F=>{A.push(F.toString())}),m.on("close",F=>{R.close(),v.length===0&&U.size>0&&console.warn(`[Codex:${r}] No text captured. Event types seen: ${[...U].join(", ")}`),F!==0&&F!==null&&(L=!0,X=A.join("")||`Codex process exited with code ${F}`),O({sessionId:C,text:v.join(""),success:!L,error:L?X:void 0,toolsUsed:H.length>0?H:void 0})}),m.on("error",F=>{L=!0,X=F.message,O({sessionId:C,text:v.join(""),success:!1,error:X,toolsUsed:H.length>0?H:void 0})})});return{process:m,result:b}}import{execFile as Dt}from"child_process";import{copyFile as Fe,mkdir as je,readdir as Nt,readFile as _t,writeFile as Bt}from"fs/promises";import{join as ce}from"path";var ye=class{constructor(t){this.projectRoot=t;let n=ce(t,".popmelt");this.decisionsDir=ce(n,"decisions"),this.screenshotsDir=ce(n,"screenshots")}async persist(t,n,e){try{await je(this.decisionsDir,{recursive:!0}),await je(this.screenshotsDir,{recursive:!0});try{await Fe(n,ce(this.screenshotsDir,`s-${t.id}.webp`))}catch{}for(let s=0;s<e.length;s++)try{let i=t.pastedImagePaths[s];i&&await Fe(e[s],ce(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await Bt(ce(this.decisionsDir,`d-${t.id}.json`),JSON.stringify(t,null,2))}catch(s){console.error("[DecisionStore] Failed to persist decision record:",s)}}async listDecisionIds(){try{return(await Nt(this.decisionsDir)).filter(n=>n.startsWith("d-")&&n.endsWith(".json")).map(n=>n.slice(2,-5))}catch{return[]}}async loadDecision(t){try{let n=await _t(ce(this.decisionsDir,`d-${t}.json`),"utf-8");return JSON.parse(n)}catch{return null}}async loadDecisions(t){return(await Promise.all(t.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(t){return new Promise(n=>{Dt("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(e,s)=>{if(e){n(null);return}n(s||null)})})}};import{readFile as ze,writeFile as ie}from"fs/promises";import{join as Ie}from"path";var q="[Materializer]",Jt={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function Ut(r){return Array.isArray(r)?r.map((t,n)=>typeof t=="string"?{id:Math.random().toString(16).slice(2,10),scope:"general",text:t,sources:[]}:t&&typeof t=="object"&&typeof t.text=="string"?t:{id:Math.random().toString(16).slice(2,10),scope:"general",text:String(t),sources:[]}):[]}function pe(r){let t=[];for(let n of r){if(!n||typeof n!="object")continue;let e=n;if(typeof e.id!="string"||typeof e.text!="string"){console.warn(`${q} Dropping rule missing id or text:`,JSON.stringify(n).slice(0,120));continue}t.push({id:e.id,scope:typeof e.scope=="string"?e.scope:"general",text:e.text,sources:Array.isArray(e.sources)?e.sources.filter(s=>typeof s=="string"):[]})}return t.length>30&&console.warn(`${q} Rule count ${t.length} exceeds cap of 30`),t}var we=class{constructor(t,n,e={}){this.projectRoot=t;this.decisionStore=n;this.options=e;this.cachedIndex=null;this.running=!1;let s=Ie(t,".popmelt");this.indexPath=Ie(s,"materialized.json"),this.modelPath=Ie(s,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await ze(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=Ut(n.rules)),n}catch{return null}}async addComponent(t){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let e=n.components;return e[t]?{added:!1,alreadyExists:!0}:(e[t]={description:""},await ie(this.modelPath,JSON.stringify(n,null,2)),console.log(`${q} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,n){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let s=t.split("."),i=e;for(let h=0;h<s.length-1;h++){let m=s[h];(!i[m]||typeof i[m]!="object")&&(i[m]={}),i=i[m]}let l=s[s.length-1],c;try{c=JSON.parse(n)}catch{c=null}if(c&&typeof c=="object"&&c!==null&&"value"in c)i[l]=c;else{let h=i[l];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=n:i[l]=n}return await ie(this.modelPath,JSON.stringify(e,null,2)),console.log(`${q} Updated token "${t}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=t.split("."),s=n;for(let l=0;l<e.length-1;l++){let c=e[l];if(!s[c]||typeof s[c]!="object")return{removed:!1};s=s[c]}let i=e[e.length-1];return i in s?(delete s[i],await ie(this.modelPath,JSON.stringify(n,null,2)),console.log(`${q} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=n.components;return!e||!e[t]?{removed:!1}:(delete e[t],await ie(this.modelPath,JSON.stringify(n,null,2)),console.log(`${q} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),n=new Set(t.materializedIds),s=(await this.decisionStore.listDecisionIds()).filter(l=>!n.has(l));return s.length===0?[]:(await this.decisionStore.loadDecisions(s)).filter(l=>l.resolutions.some(c=>(c.finalScope??c.inferredScope)?.breadth==="pattern"))}async run(){if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let t=await this.getUnmaterializedPatternDecisions();if(t.length===0)return{processedIds:[],success:!0};let n=t.map(m=>m.id);console.log(`${q} Processing ${n.length} pattern-scoped decision(s): ${n.join(", ")}`),this.options.onEvent?.({type:"materialize_started",decisionIds:n});let e=await this.loadModel(),s=Ft(t,e),i=!0,l;try{let{result:m}=ue(`mat-${Date.now()}`,{prompt:s,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"],claudePath:this.options.claudePath??"claude"}),b=await m;if(!b.success)i=!1,l=b.error,console.error(`${q} Claude spawn error:`,l);else{let O=ve(b.text);O?(Array.isArray(O.rules)&&(O.rules=pe(O.rules)),await ie(this.modelPath,JSON.stringify(O,null,2)),console.log(`${q} Successfully materialized ${n.length} decision(s) \u2192 ${this.modelPath}`)):(i=!1,l="No <model> block found in response",console.error(`${q} ${l}`))}}catch(m){i=!1,l=m instanceof Error?m.message:String(m),console.error(`${q} Error:`,l)}let c=await this.loadIndex(),h=new Set(c.materializedIds);for(let m of n)h.add(m);return c.materializedIds=[...h],c.lastRunAt=Date.now(),c.lastRunDecisionIds=n,c.lastRunError=l??null,await this.persistIndex(c),this.options.onEvent?.({type:"materialize_done",decisionIds:n,success:i,error:l}),{processedIds:n,success:i,error:l}}finally{this.running=!1}}async consolidate(){if(this.running)return{success:!1,error:"Already running"};this.running=!0;try{let t=await this.loadModel();if(!t)return{success:!1,error:"No model exists"};let n=jt(t),{result:e}=ue(`consolidate-${Date.now()}`,{prompt:n,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[],claudePath:this.options.claudePath??"claude"}),s=await e;if(!s.success)return console.error(`${q} Consolidation spawn error:`,s.error),{success:!1,error:s.error};let i=ve(s.text);return i?(Array.isArray(i.rules)&&(i.rules=pe(i.rules)),!i.tokens&&t.tokens&&(i.tokens=t.tokens),!i.components&&t.components&&(i.components=t.components),await ie(this.modelPath,JSON.stringify(i,null,2)),console.log(`${q} Consolidation complete \u2192 ${this.modelPath}`),{success:!0}):(console.error(`${q} No <model> block in consolidation response`),{success:!1,error:"No <model> block found"})}catch(t){let n=t instanceof Error?t.message:String(t);return console.error(`${q} Consolidation error:`,n),{success:!1,error:n}}finally{this.running=!1}}async writeModel(t){Array.isArray(t.rules)&&(t.rules=pe(t.rules)),await ie(this.modelPath,JSON.stringify(t,null,2)),console.log(`${q} Model written \u2192 ${this.modelPath}`)}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await ze(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...Jt,materializedIds:[],lastRunDecisionIds:[]},this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await ie(this.indexPath,JSON.stringify(t,null,2))}catch(n){console.error(`${q} Failed to write index:`,n)}}};function ve(r){let t=r.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!t?.[1])return null;try{let n=JSON.parse(t[1]);return typeof n!="object"||n===null||Array.isArray(n)?null:n}catch{return null}}function Ft(r,t){let n=r.map(i=>{let c=i.resolutions.filter(b=>(b.finalScope??b.inferredScope)?.breadth==="pattern").map(b=>{let C=(b.finalScope??b.inferredScope)?.target??"unknown",v=b.filesModified?.join(", ")??"none";return`- **${b.summary}** [scope: pattern/${C}]
32
+ Files modified: ${v}`}).join(`
33
+ `),h=i.annotations.map(b=>b.instruction).filter(Boolean).join(`
34
+ `),m=i.gitDiff?`
35
35
  \`\`\`diff
36
- ${s.gitDiff.slice(0,2e3)}
37
- \`\`\``:"";return`### Decision ${s.id} (${new Date(s.createdAt).toISOString()})
38
- Page: ${s.url}
36
+ ${i.gitDiff.slice(0,2e3)}
37
+ \`\`\``:"";return`### Decision ${i.id} (${new Date(i.createdAt).toISOString()})
38
+ Page: ${i.url}
39
39
  ${c}
40
- ${h}
41
- ${r?`
40
+ ${m}
41
+ ${h?`
42
42
  Original instructions:
43
- ${r}`:""}`}).join(`
43
+ ${h}`:""}`}).join(`
44
44
 
45
- `);return`You are extracting a local design model from accumulated design decisions.
45
+ `),e=r.map(i=>i.id);return`You are extracting a local design model from accumulated design decisions.
46
46
 
47
47
  ## Instructions
48
48
  1. Review the current model (if any) and the new decisions below.
@@ -63,7 +63,24 @@ ${n}
63
63
  Output the full model inside <model> tags. The model is a JSON object with these sections:
64
64
  - \`tokens\`: Design tokens (colors, spacing, typography, etc.)
65
65
  - \`components\`: Component-level patterns (e.g., button styles, card layouts)
66
- - \`rules\`: Array of plain-language rules extracted from decisions
66
+ - \`rules\`: Array of structured rule objects
67
+
68
+ ### Rule format
69
+ Each rule is a JSON object:
70
+ \`\`\`json
71
+ { "id": "a1b2c3d4", "scope": "typography", "text": "Body text uses Inter at 16px/1.5", "sources": ["d-abc123"] }
72
+ \`\`\`
73
+
74
+ Fields:
75
+ - \`id\`: 8-character hex string. Preserve IDs for unchanged rules. Generate new IDs for new or merged rules.
76
+ - \`scope\`: One of: typography, color, spacing, border, component, layout, copy, ssr, accessibility, structure
77
+ - \`text\`: A clear, enforceable design rule
78
+ - \`sources\`: Array of decision IDs that informed this rule. For new rules from these decisions, use: ${JSON.stringify(e)}
79
+
80
+ ### Rule guidelines
81
+ - **Hard cap: 30 rules maximum.** Merge rules covering the same concern into one.
82
+ - **Reject:** workflow advice ("After fixing X, verify Y"), instance-level observations ("The hero section uses 32px gap"), rules that merely restate a token or component already in the model.
83
+ - **Keep:** Enforceable patterns, constraints, relationships between elements, accessibility requirements.
67
84
 
68
85
  Example:
69
86
  <model>
@@ -80,15 +97,15 @@ Example:
80
97
  "button": { "padding": "12px 24px", "borderRadius": "8px" }
81
98
  },
82
99
  "rules": [
83
- "Buttons use 12px vertical padding and 24px horizontal padding",
84
- "Primary actions use the primary color"
100
+ { "id": "a1b2c3d4", "scope": "typography", "text": "Body text uses Inter at 16px/1.5", "sources": ["d-abc123"] },
101
+ { "id": "e5f6a7b8", "scope": "color", "text": "Primary actions use the primary color token", "sources": ["d-def456"] }
85
102
  ]
86
103
  }
87
104
  </model>
88
105
 
89
106
  ## Guidelines
90
107
  - Map token-scoped decisions to \`tokens\`, component-scoped to \`components\`.
91
- - Extract clear, enforceable rules into the \`rules\` array.
108
+ - Extract clear, enforceable rules into the \`rules\` array as structured objects.
92
109
  - When merging, new decisions override conflicting older values.
93
110
  - Keep the model concise \u2014 only include patterns with clear evidence from decisions.
94
111
  - Do NOT output resolution or question blocks. Just output the <model> block.
@@ -96,40 +113,111 @@ Example:
96
113
  { "value": "8px", "property": "gap", "bindings": ["gap-2"] }
97
114
  - property: "gap", "padding", or "margin" \u2014 which CSS property this token controls
98
115
  - bindings: Tailwind class names (without responsive prefixes) that use this token
99
- Include property and bindings when the decision context has class-level evidence.`}import{readFile as je}from"fs/promises";import{homedir as He}from"os";import{join as de}from"path";var be=/popmelt/i;function ke(){return{found:!1,name:null,scope:null,disabled:!1}}function ue(i,t,n=!1){return{found:!0,name:i,scope:t,disabled:n}}function ve(i){for(let t of Object.keys(i))if(be.test(t))return t;return null}async function Ie(i){try{let t=await je(i,"utf-8");return JSON.parse(t)}catch{return null}}async function Te(i){let t=He(),n=await Ie(de(t,".claude.json"));if(n&&typeof n=="object"){let s=n;if(s.mcpServers&&typeof s.mcpServers=="object"){let l=ve(s.mcpServers);if(l)return ue(l,"user")}if(s.projects&&typeof s.projects=="object"){let c=s.projects[i];if(c&&typeof c=="object"){let r=c;if(r.mcpServers&&typeof r.mcpServers=="object"){let h=ve(r.mcpServers);if(h){let f=Array.isArray(r.disabledMcpjsonServers)&&r.disabledMcpjsonServers.some(N=>be.test(N));return ue(h,"project",f)}}}}}let e=await Ie(de(i,".mcp.json"));if(e&&typeof e=="object"){let s=e;if(s.mcpServers&&typeof s.mcpServers=="object"){let c=ve(s.mcpServers);if(c){let r=await Le(i,c);return ue(c,"mcp.json",r)}}let l=ve(s);if(l&&l!=="mcpServers"){let c=await Le(i,l);return ue(l,"mcp.json",c)}}return ke()}async function Le(i,t){let n=de(i,".claude","settings.local.json"),e=await Ie(n);if(e&&typeof e=="object"){let s=e;if(Array.isArray(s.disabledMcpjsonServers))return s.disabledMcpjsonServers.some(l=>l===t)}return!1}var Dt=/^\[mcp_servers\.([^\]]+)\]/;function Nt(i){let t=i.split(`
100
- `),n=[],e=null;for(let s of t){let l=s.match(Dt);l?(e&&n.push({name:e.name,body:e.bodyLines.join(`
101
- `)}),e={name:l[1],bodyLines:[]}):s.startsWith("[")?e&&(n.push({name:e.name,body:e.bodyLines.join(`
116
+ Include property and bindings when the decision context has class-level evidence.`}function jt(r){return`You are consolidating a design model's rules. The model has accumulated too many rules and needs cleanup.
117
+
118
+ ## Current Model
119
+ \`\`\`json
120
+ ${JSON.stringify(r,null,2)}
121
+ \`\`\`
122
+
123
+ ## Instructions
124
+ 1. Review all current rules.
125
+ 2. Merge rules that cover the same concern into single, clear rules.
126
+ 3. Remove rules that are:
127
+ - Workflow advice ("After SSR fixes, re-verify headless output")
128
+ - Instance-level observations ("Footer tagline is 'Design everything.'")
129
+ - Restating tokens or components already defined elsewhere in the model
130
+ - Procedural instructions rather than design constraints
131
+ 4. Output the full model with consolidated rules.
132
+
133
+ ## Rule format
134
+ Each rule MUST be a JSON object:
135
+ { "id": "a1b2c3d4", "scope": "<scope>", "text": "<rule text>", "sources": ["<decision-id>", ...] }
136
+
137
+ - \`id\`: 8-char hex. Preserve the ID of the most representative source rule when merging. Generate new 8-char hex IDs for genuinely new rules.
138
+ - \`scope\`: One of: typography, color, spacing, border, component, layout, copy, ssr, accessibility, structure
139
+ - \`text\`: Clear, enforceable design constraint
140
+ - \`sources\`: Merge source arrays when combining rules
141
+
142
+ ## Hard cap: 30 rules maximum.
143
+
144
+ Prioritize rules that are:
145
+ - Enforceable constraints (not preferences)
146
+ - Cross-cutting (affect many elements)
147
+ - Not already captured by tokens or component definitions in the model
148
+
149
+ ## Output
150
+ Output the complete model inside <model> tags. Preserve tokens and components as-is \u2014 only rules change.
151
+
152
+ <model>
153
+ { "tokens": { ... }, "components": { ... }, "rules": [ ... ] }
154
+ </model>`}function Le(r){return`You are a design system curator reviewing a project's design model. Your job is to propose improvements to the rules \u2014 merging duplicates, filling gaps, removing noise.
155
+
156
+ ## Current Model
157
+ \`\`\`json
158
+ ${JSON.stringify(r,null,2)}
159
+ \`\`\`
160
+
161
+ ## Instructions
162
+ 1. Review all current rules, tokens, and components.
163
+ 2. Propose specific changes \u2014 additions, merges, removals \u2014 with brief reasoning for each.
164
+ 3. Present your proposals as clear text so the developer can review them.
165
+ 4. Do NOT output a <model> block yet. First get the developer's approval.
166
+
167
+ ## Output format
168
+ - List each proposed change as a numbered item with a short rationale.
169
+ - Group by action: **Merge**, **Add**, **Remove**, **Reword**.
170
+ - After your proposals, output a <question> block asking the developer to approve, adjust, or reject.
171
+
172
+ Example:
173
+ **Merge**
174
+ 1. Rules "Body text uses Inter 16px" and "Paragraph text is Inter 16/1.5" \u2192 "Body/paragraph text uses Inter at 16px/1.5" (they say the same thing)
175
+
176
+ **Remove**
177
+ 2. "After fixing SSR, re-verify headless output" \u2014 workflow advice, not a design constraint
178
+
179
+ **Add**
180
+ 3. "Primary actions use the primary color token; secondary actions use gray-500" \u2014 implied by token usage but not codified
181
+
182
+ <question>
183
+ These are my proposed rule changes. Would you like to approve all of them, adjust specific items, or add more? Once you're happy I'll output the final model.
184
+ </question>
185
+
186
+ ## After approval
187
+ When the developer approves (says "yes", "looks good", "go ahead", etc.), output the complete updated model inside <model> tags \u2014 the full JSON with tokens, components, and the revised rules array. Do NOT output partial models. Preserve tokens and components as-is unless the developer asks to change them.`}import{readFile as We}from"fs/promises";import{homedir as qe}from"os";import{join as de}from"path";var Re=/popmelt/i;function $e(){return{found:!1,name:null,scope:null,disabled:!1}}function he(r,t,n=!1){return{found:!0,name:r,scope:t,disabled:n}}function Se(r){for(let t of Object.keys(r))if(Re.test(t))return t;return null}async function Te(r){try{let t=await We(r,"utf-8");return JSON.parse(t)}catch{return null}}async function Ee(r){let t=qe(),n=await Te(de(t,".claude.json"));if(n&&typeof n=="object"){let s=n;if(s.mcpServers&&typeof s.mcpServers=="object"){let i=Se(s.mcpServers);if(i)return he(i,"user")}if(s.projects&&typeof s.projects=="object"){let l=s.projects[r];if(l&&typeof l=="object"){let c=l;if(c.mcpServers&&typeof c.mcpServers=="object"){let h=Se(c.mcpServers);if(h){let m=Array.isArray(c.disabledMcpjsonServers)&&c.disabledMcpjsonServers.some(b=>Re.test(b));return he(h,"project",m)}}}}}let e=await Te(de(r,".mcp.json"));if(e&&typeof e=="object"){let s=e;if(s.mcpServers&&typeof s.mcpServers=="object"){let l=Se(s.mcpServers);if(l){let c=await He(r,l);return he(l,"mcp.json",c)}}let i=Se(s);if(i&&i!=="mcpServers"){let l=await He(r,i);return he(i,"mcp.json",l)}}return $e()}async function He(r,t){let n=de(r,".claude","settings.local.json"),e=await Te(n);if(e&&typeof e=="object"){let s=e;if(Array.isArray(s.disabledMcpjsonServers))return s.disabledMcpjsonServers.some(i=>i===t)}return!1}var zt=/^\[mcp_servers\.([^\]]+)\]/;function Lt(r){let t=r.split(`
188
+ `),n=[],e=null;for(let s of t){let i=s.match(zt);i?(e&&n.push({name:e.name,body:e.bodyLines.join(`
189
+ `)}),e={name:i[1],bodyLines:[]}):s.startsWith("[")?e&&(n.push({name:e.name,body:e.bodyLines.join(`
102
190
  `)}),e=null):e&&e.bodyLines.push(s)}return e&&n.push({name:e.name,body:e.bodyLines.join(`
103
- `)}),n}function Bt(i){return/enabled\s*=\s*false/i.test(i)}async function Ee(i){let t=process.env.CODEX_HOME||de(He(),".codex"),n=await ze(de(t,"config.toml"),"user");if(n.found)return n;let e=await ze(de(i,".codex","config.toml"),"project");return e.found?e:ke()}async function ze(i,t){try{let n=await je(i,"utf-8"),e=Nt(n);for(let s of e)if(be.test(s.name)){let l=Bt(s.body);return ue(s.name,t,l)}}catch{}return ke()}import{mkdir as Jt,readFile as Ge,writeFile as We}from"fs/promises";import{homedir as qe}from"os";import{dirname as Ut,join as $e}from"path";var Qe="https://mcp.popmelt.com/mcp";async function Xe(i=Qe){let t=$e(qe(),".claude.json"),n;try{let s=await Ge(t,"utf-8");n=JSON.parse(s)}catch{n={}}(!n.mcpServers||typeof n.mcpServers!="object")&&(n.mcpServers={});let e=n.mcpServers;for(let s of Object.keys(e))if(/popmelt/i.test(s))return{installed:!1,provider:"claude",scope:null,reason:"already_configured"};return e.popmelt={type:"http",url:i},await We(t,JSON.stringify(n,null,2)+`
104
- `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function Ye(i=Qe){let t=process.env.CODEX_HOME||$e(qe(),".codex"),n=$e(t,"config.toml"),e;try{e=await Ge(n,"utf-8")}catch{e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await Jt(Ut(n),{recursive:!0});let s=`
191
+ `)}),n}function Ht(r){return/enabled\s*=\s*false/i.test(r)}async function Me(r){let t=process.env.CODEX_HOME||de(qe(),".codex"),n=await Ge(de(t,"config.toml"),"user");if(n.found)return n;let e=await Ge(de(r,".codex","config.toml"),"project");return e.found?e:$e()}async function Ge(r,t){try{let n=await We(r,"utf-8"),e=Lt(n);for(let s of e)if(Re.test(s.name)){let i=Ht(s.body);return he(s.name,t,i)}}catch{}return $e()}import{mkdir as Gt,readFile as Ye,writeFile as Qe}from"fs/promises";import{homedir as Xe}from"os";import{dirname as Wt,join as Ce}from"path";var Ve="https://mcp.popmelt.com/mcp";async function Ke(r=Ve){let t=Ce(Xe(),".claude.json"),n;try{let s=await Ye(t,"utf-8");n=JSON.parse(s)}catch{n={}}(!n.mcpServers||typeof n.mcpServers!="object")&&(n.mcpServers={});let e=n.mcpServers;for(let s of Object.keys(e))if(/popmelt/i.test(s))return{installed:!1,provider:"claude",scope:null,reason:"already_configured"};return e.popmelt={type:"http",url:r},await Qe(t,JSON.stringify(n,null,2)+`
192
+ `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function Ze(r=Ve){let t=process.env.CODEX_HOME||Ce(Xe(),".codex"),n=Ce(t,"config.toml"),e;try{e=await Ye(n,"utf-8")}catch{e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await Gt(Wt(n),{recursive:!0});let s=`
105
193
  [mcp_servers.popmelt]
106
- url = "${i}"
107
- `;return await We(n,e+s,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function Re(i){let n=(i.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!n)throw new Error("Missing multipart boundary");let e=n[1]||n[2],s=await Ft(i),l=Buffer.from(`--${e}`),c=Buffer.from(`--${e}--`),r,h,f,N,U,O,I,z,L,W,T,J,C=[],B=[],g=0,F=[];for(;g<s.length;){let y=s.indexOf(l,g);if(y===-1)break;let w=y+l.length;if(s.slice(y,y+c.length).equals(c))break;let S=w;s[S]===13&&s[S+1]===10&&(S+=2);let D=s.indexOf(`\r
194
+ url = "${r}"
195
+ `;return await Qe(n,e+s,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function Oe(r){let n=(r.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!n)throw new Error("Missing multipart boundary");let e=n[1]||n[2],s=await qt(r),i=Buffer.from(`--${e}`),l=Buffer.from(`--${e}--`),c,h,m,b,O,C,v,H,L,X,R,U,A=[],F=[],g=0,j=[];for(;g<s.length;){let S=s.indexOf(i,g);if(S===-1)break;let k=S+i.length;if(s.slice(S,S+l.length).equals(l))break;let I=k;s[I]===13&&s[I+1]===10&&(I+=2);let B=s.indexOf(`\r
108
196
  \r
109
- `,S);if(D===-1)break;let A=s.slice(S,D).toString("utf-8"),ee=D+4,$=s.indexOf(l,ee),ce=$!==-1?$-2:s.length;F.push({headers:A,body:s.slice(ee,ce)}),g=$!==-1?$:s.length}for(let y of F){let w=y.headers.match(/name="([^"]+)"/);if(!w)continue;let S=w[1];if(S==="screenshot")r=y.body;else if(S==="feedback")h=y.body.toString("utf-8");else if(S==="color")f=y.body.toString("utf-8");else if(S==="provider")N=y.body.toString("utf-8");else if(S==="model")U=y.body.toString("utf-8");else if(S==="goal")O=y.body.toString("utf-8");else if(S==="pageUrl")I=y.body.toString("utf-8");else if(S==="viewport")z=y.body.toString("utf-8");else if(S==="planId")L=y.body.toString("utf-8");else if(S==="manifest")W=y.body.toString("utf-8");else if(S==="tasks")T=y.body.toString("utf-8");else if(S==="sourceId")J=y.body.toString("utf-8");else if(S.startsWith("screenshot-")){let D=S.slice(11);try{let A=decodeURIComponent(D);B.push({pathname:A,data:y.body})}catch{}}else if(S.startsWith("image-")){let D=S.split("-"),A=parseInt(D[D.length-1],10),ee=D.slice(1,-1).join("-");ee&&!isNaN(A)&&C.push({annotationId:ee,index:A,data:y.body})}}if(!r)throw new Error("Missing screenshot field");return h||(h=""),{screenshot:r,feedback:h,color:f,provider:N,model:U,goal:O,pageUrl:I,viewport:z,planId:L,manifest:W,tasks:T,sourceId:J,pastedImages:C,pageScreenshots:B}}function Ft(i){return new Promise((t,n)=>{let e=[];i.on("data",s=>e.push(s)),i.on("end",()=>t(Buffer.concat(e))),i.on("error",n)})}function pe(i,t){let n=[];if(i.annotations.length>0){n.push("## Annotations");for(let e of i.annotations){let s=e.elements.map(r=>{let h=[r.selector];return r.reactComponent&&h.push(`(${r.reactComponent})`),h.join(" ")}).join(", "),l=e.instruction||"No text";n.push(`- id=${e.id} [${e.type}] ${l} \u2192 Elements: ${s||"none"}`);let c=t?.[e.id];if(c&&c.length>0)for(let r of c)n.push(` Attached image: use the Read tool to view ${r}`)}}if(i.styleModifications.length>0){n.push(""),n.push("## Style Changes (make permanent in source)"),n.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let e of i.styleModifications){let s=e.element?.reactComponent?`(${e.element.reactComponent})`:"";for(let l of e.changes)n.push(`- ${e.selector} ${s}: ${l.property} ${l.original} \u2192 ${l.modified}`)}}if(i.spacingTokenChanges?.length){n.push(""),n.push("## Spacing Token Changes"),n.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let e of i.spacingTokenChanges){n.push(`
110
- ### ${e.tokenName}: ${e.originalPx}px \u2192 ${e.newPx}px`);for(let s of e.affectedElements){let l=s.reactComponent?` (${s.reactComponent})`:"";s.matchedClass&&s.suggestedClass?n.push(`- ${s.selector}${l}: \`${s.matchedClass}\` \u2192 \`${s.suggestedClass}\``):n.push(`- ${s.selector}${l}: ${s.property} ${e.originalPx}px \u2192 ${e.newPx}px`),n.push(` class="${s.className}"`)}}}if(i.inspectedElement){let e=i.inspectedElement;n.push(""),n.push("## Inspected Element"),n.push("The developer has this element selected in the inspector:");let s=[e.selector];e.reactComponent&&s.push(`(${e.reactComponent})`),e.context&&s.push(`in ${e.context}`),e.textContent&&s.push(`"${e.textContent.slice(0,80)}"`),n.push(`- ${s.join(" ")}`)}return n.join(`
111
- `)}function Ve(i,t,n){let e=[],l=new Set(t.annotations.map(c=>c.pathname).filter(Boolean)).size>1;if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),!l&&n?.provider!=="codex"&&(e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${i}`),e.push("")),e.push(`The developer annotated their running app at ${t.url} (${t.viewport.width}x${t.viewport.height}).`),n?.threadHistory&&n.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let c=0;for(let r of n.threadHistory)if(r.role==="human")c++,r.replyToQuestion?(e.push(`### Round ${c} (human) \u2014 reply`),e.push(`"${r.replyToQuestion}"`)):(e.push(`### Round ${c} (human)`),r.feedbackSummary&&e.push(`Annotations: ${r.feedbackSummary}`),r.annotationIds&&r.annotationIds.length>0&&e.push(`Annotation IDs: ${r.annotationIds.join(", ")}`));else if(r.question)e.push(`### Round ${c} (assistant) \u2014 question`),e.push(`"${r.question}"`);else{if(e.push(`### Round ${c} (assistant)`),r.responseText&&e.push(`Response: ${r.responseText}`),r.resolutions&&r.resolutions.length>0)for(let h of r.resolutions){let f=h.finalScope??h.inferredScope,N=f?` [${f.breadth} ${f.target}]`:"";e.push(`- ${h.annotationId}: ${h.status}${N} \u2014 ${h.summary}`),h.filesModified&&h.filesModified.length>0&&e.push(` Files: ${h.filesModified.join(", ")}`)}r.toolsUsed&&r.toolsUsed.length>0&&e.push(`Tools used: ${r.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(n?.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 c=n.designModel.rules;if(Array.isArray(c)&&c.length>0){e.push(""),e.push("Rules:");for(let f of c)typeof f=="string"&&e.push(`- ${f}`)}let r=n.designModel.tokens;r&&typeof r=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(r,null,2)),e.push("```"));let h=n.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.")}if(n?.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.")),l){let c=n?.screenshotPaths??{},r=new Map;for(let h of t.annotations){let f=h.pathname||new URL(t.url).pathname;r.has(f)||r.set(f,[]),r.get(f).push(h)}e.push(""),e.push("The developer annotated multiple pages. Each page section includes its own screenshot where available.");for(let[h,f]of r){e.push(""),e.push(`## Page: ${h}`);let N=c[h];N&&n?.provider!=="codex"?e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${N}`):!N&&n?.provider!=="codex"&&(Object.keys(c).length===0?(e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${i}`),e.push("(This screenshot shows the page the developer was on when submitting \u2014 not this page)")):e.push("(No screenshot available for this page \u2014 work from the annotation text and selectors)"));let U={...t,annotations:f,styleModifications:t.styleModifications},O=pe(U,n?.imagePaths);O&&(e.push(""),e.push(O))}}else{let c=pe(t,n?.imagePaths);c&&(e.push(""),e.push(c))}return 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(`
112
- `)}function Ke(i){return i.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function Ze(i,t,n,e){let s=[];s.push("You are continuing work on a UI based on the developer's reply to your question."),s.push(""),n!=="codex"&&s.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${i}`);let l=t.find(c=>c.role==="human"&&c.feedbackContext);if(l?.feedbackContext&&(s.push(""),s.push(l.feedbackContext)),t.length>0){s.push(""),s.push("## Conversation History");let c=0;for(let r of t)r.role==="human"?(c++,r.replyToQuestion?(s.push(`### Round ${c} (human) \u2014 reply`),s.push(`"${r.replyToQuestion}"`)):(s.push(`### Round ${c} (human)`),r.feedbackSummary&&s.push(`Annotations: ${r.feedbackSummary}`))):r.question?(s.push(`### Round ${c} (assistant) \u2014 question`),s.push(`"${r.question}"`)):(s.push(`### Round ${c} (assistant)`),r.responseText&&s.push(`Response: ${r.responseText}`))}if(s.push(""),s.push("The developer answered your question. Continue working based on their reply."),s.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),s.push(""),s.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){s.push(""),s.push("## Attached Images"),s.push("The developer attached reference images with their reply:");for(let c of e)s.push(`Attached image: use the Read tool to view the image at: ${c}`)}return s.push(""),s.push("## Resolution"),s.push("After completing all work, output a resolution block listing what you did for each annotation:"),s.push("<resolution>"),s.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),s.push("</resolution>"),s.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),s.push(""),s.push("### Scope classification"),s.push("Each resolution MUST include scope fields:"),s.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),s.push("- `inferredScope`: What scope the change actually has, based on what you modified."),s.push("Scope has two dimensions:"),s.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),s.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),s.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),s.push("If you cannot confidently determine scope, set it to null."),s.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),s.push(""),s.push("## Questions"),s.push("If you still need clarification, output:"),s.push("<question>Your question here</question>"),s.push("You may output BOTH a <resolution> and a <question> in the same response."),s.join(`
113
- `)}function Lt(i){if(typeof i!="object"||i===null)return!1;let t=i;return(t.breadth==="instance"||t.breadth==="pattern")&&(t.target==="element"||t.target==="component"||t.target==="token")}function zt(i){if(typeof i!="object"||i===null||typeof i.annotationId!="string"||i.status!=="resolved"&&i.status!=="needs_review"||typeof i.summary!="string")return!1;let t=i;for(let n of["declaredScope","inferredScope","finalScope"])if(t[n]!==void 0&&t[n]!==null&&!Lt(t[n]))return!1;return!0}function et(i){let t=i.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!t||!t[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(zt):[]}catch{return[]}}function tt(i){let t=i.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!t?.[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(e=>{if(typeof e!="object"||e===null)return!1;let s=e;return(s.category==="token"||s.category==="component"||s.category==="element")&&typeof s.element=="string"&&typeof s.decision=="string"&&typeof s.reason=="string"}):[]}catch{return[]}}var we=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.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(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let s=this.bufferEvent(n,t);for(let l of this.listeners)l(s,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let s={...n,seq:e.nextSeq++};return e.events.push(s),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),s}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),s=this.accumulators.get(t)??{response:"",thinking:""},l=this.activeJobs.has(t);if(!e)return null;let c=n<0?e.events:e.events.filter(r=>r.seq>n);return{jobId:t,events:c,currentSeq:e.nextSeq-1,accumulated:{...s},jobActive:l}}accumulateText(t,n,e){let s=this.accumulators.get(t);s||(s={response:"",thinking:""},this.accumulators.set(t,s)),s[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),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 t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(s=>{let l=!1,c=()=>{l||(l=!0,s())};e.on("exit",c),e.on("error",c),setTimeout(()=>{if(!l){try{e.kill("SIGKILL")}catch{}setTimeout(c,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,message:t.error},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as jt,readFile as Ht,writeFile as Gt}from"fs/promises";import{dirname as Wt,join as qt}from"path";var Qt={version:1,threads:{}},Se=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=qt(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await Ht(this.filePath,"utf-8"),n=JSON.parse(t);if(n&&n.version===1&&n.threads)return this.cache=n,this.cache}catch{}return this.cache={...Qt,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let s of Object.values(n.threads))if(s.elementIdentifiers.some(c=>e.has(c)))return s;return null}async createThread(t,n){let e=await this.load(),s={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=s,await this.persist(),s}async appendMessage(t,n){let s=(await this.load()).threads[t];s&&(s.messages.push(n),s.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let s=(await this.load()).threads[t];if(!s)return;let l=new Set(s.elementIdentifiers);for(let c of n)l.has(c)||s.elementIdentifiers.push(c);s.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((s,l)=>l.updatedAt-s.updatedAt),e.slice(0,t).map(s=>{let c=s.messages.find(r=>r.role==="human")?.feedbackSummary||"[thread]";return{id:s.id,createdAt:s.createdAt,updatedAt:s.updatedAt,preview:c,messageCount:s.messages.length,elementIdentifiers:s.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await jt(Wt(this.filePath),{recursive:!0}),await Gt(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var nt="0.6.6";var cn=1111,ln=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],dn=1800*1e3,un=3600*1e3;function it(i){if(!i)return!1;try{let t=new URL(i);return t.hostname==="localhost"||t.hostname==="127.0.0.1"}catch{return!1}}function pn(i){let t=[];for(let n of i)if(n.type==="delta"){let e=n.text;if(!e)continue;let s=t[t.length-1];s&&s.kind==="text"?s.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",s=n.file??void 0,l=n.content??void 0,c=s?s.split("/").pop()??s:void 0,r;switch(e){case"Read":r=c?`Reading ${c}`:"Reading file";break;case"Edit":r=c?`Editing ${c}`:"Editing file";break;case"Write":r=c?`Writing ${c}`:"Writing file";break;case"Bash":r=l?l.split(`
114
- `)[0].trim().slice(0,60):"Running command";break;case"Glob":r="Searching files";break;case"Grep":r="Searching code";break;case"WebFetch":r="Fetching page";break;case"WebSearch":r="Searching web";break;default:r=e?`Using ${e}`:"tool";break}let h=s??l??void 0,f=t[t.length-1];f&&f.kind==="tool_group"&&f.tool===e?f.items.push({label:r,detail:h}):t.push({kind:"tool_group",tool:e,items:[{label:r,detail:h}]})}return t}function hn(i,t){let n=i.headers.origin;it(n)&&(t.setHeader("Access-Control-Allow-Origin",n),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function v(i,t,n){i.writeHead(t,{"Content-Type":"application/json"}),i.end(JSON.stringify(n))}function fn(i,t){if(!i)return t;let n=i.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!n)return t;let[,e,s,l]=n;return`\x1B[38;2;${parseInt(e,16)};${parseInt(s,16)};${parseInt(l,16)}m${t}\x1B[0m`}function Ce(i,t){try{i.res.write(`event: ${t.type}
197
+ `,I);if(B===-1)break;let _=s.slice(I,B).toString("utf-8"),Z=B+4,E=s.indexOf(i,Z),le=E!==-1?E-2:s.length;j.push({headers:_,body:s.slice(Z,le)}),g=E!==-1?E:s.length}for(let S of j){let k=S.headers.match(/name="([^"]+)"/);if(!k)continue;let I=k[1];if(I==="screenshot")c=S.body;else if(I==="feedback")h=S.body.toString("utf-8");else if(I==="color")m=S.body.toString("utf-8");else if(I==="provider")b=S.body.toString("utf-8");else if(I==="model")O=S.body.toString("utf-8");else if(I==="goal")C=S.body.toString("utf-8");else if(I==="pageUrl")v=S.body.toString("utf-8");else if(I==="viewport")H=S.body.toString("utf-8");else if(I==="planId")L=S.body.toString("utf-8");else if(I==="manifest")X=S.body.toString("utf-8");else if(I==="tasks")R=S.body.toString("utf-8");else if(I==="sourceId")U=S.body.toString("utf-8");else if(I.startsWith("screenshot-")){let B=I.slice(11);try{let _=decodeURIComponent(B);F.push({pathname:_,data:S.body})}catch{}}else if(I.startsWith("image-")){let B=I.split("-"),_=parseInt(B[B.length-1],10),Z=B.slice(1,-1).join("-");Z&&!isNaN(_)&&A.push({annotationId:Z,index:_,data:S.body})}}if(!c)throw new Error("Missing screenshot field");return h||(h=""),{screenshot:c,feedback:h,color:m,provider:b,model:O,goal:C,pageUrl:v,viewport:H,planId:L,manifest:X,tasks:R,sourceId:U,pastedImages:A,pageScreenshots:F}}function qt(r){return new Promise((t,n)=>{let e=[];r.on("data",s=>e.push(s)),r.on("end",()=>t(Buffer.concat(e))),r.on("error",n)})}function fe(r,t){let n=[];if(r.annotations.length>0){n.push("## Annotations");for(let e of r.annotations){let s=e.elements.map(c=>{let h=[c.selector];return c.reactComponent&&h.push(`(${c.reactComponent})`),h.join(" ")}).join(", "),i=e.instruction||"No text";n.push(`- id=${e.id} [${e.type}] ${i} \u2192 Elements: ${s||"none"}`);let l=t?.[e.id];if(l&&l.length>0)for(let c of l)n.push(` Attached image: use the Read tool to view ${c}`)}}if(r.styleModifications.length>0){n.push(""),n.push("## Style Changes (make permanent in source)"),n.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let e of r.styleModifications){let s=e.element?.reactComponent?`(${e.element.reactComponent})`:"";for(let i of e.changes)n.push(`- ${e.selector} ${s}: ${i.property} ${i.original} \u2192 ${i.modified}`)}}if(r.spacingTokenChanges?.length){n.push(""),n.push("## Spacing Token Changes"),n.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let e of r.spacingTokenChanges){n.push(`
198
+ ### ${e.tokenName}: ${e.originalPx}px \u2192 ${e.newPx}px`);for(let s of e.affectedElements){let i=s.reactComponent?` (${s.reactComponent})`:"";s.matchedClass&&s.suggestedClass?n.push(`- ${s.selector}${i}: \`${s.matchedClass}\` \u2192 \`${s.suggestedClass}\``):n.push(`- ${s.selector}${i}: ${s.property} ${e.originalPx}px \u2192 ${e.newPx}px`),n.push(` class="${s.className}"`)}}}if(r.inspectedElement){let e=r.inspectedElement;n.push(""),n.push("## Inspected Element"),n.push("The developer has this element selected in the inspector:");let s=[e.selector];e.reactComponent&&s.push(`(${e.reactComponent})`),e.context&&s.push(`in ${e.context}`),e.textContent&&s.push(`"${e.textContent.slice(0,80)}"`),n.push(`- ${s.join(" ")}`)}return n.join(`
199
+ `)}function et(r,t,n){let e=[],i=new Set(t.annotations.map(l=>l.pathname).filter(Boolean)).size>1;if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),!i&&n?.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 ${t.url} (${t.viewport.width}x${t.viewport.height}).`),n?.threadHistory&&n.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let l=0;for(let c of n.threadHistory)if(c.role==="human")l++,c.replyToQuestion?(e.push(`### Round ${l} (human) \u2014 reply`),e.push(`"${c.replyToQuestion}"`)):(e.push(`### Round ${l} (human)`),c.feedbackSummary&&e.push(`Annotations: ${c.feedbackSummary}`),c.annotationIds&&c.annotationIds.length>0&&e.push(`Annotation IDs: ${c.annotationIds.join(", ")}`));else if(c.question)e.push(`### Round ${l} (assistant) \u2014 question`),e.push(`"${c.question}"`);else{if(e.push(`### Round ${l} (assistant)`),c.responseText&&e.push(`Response: ${c.responseText}`),c.resolutions&&c.resolutions.length>0)for(let h of c.resolutions){let m=h.finalScope??h.inferredScope,b=m?` [${m.breadth} ${m.target}]`:"";e.push(`- ${h.annotationId}: ${h.status}${b} \u2014 ${h.summary}`),h.filesModified&&h.filesModified.length>0&&e.push(` Files: ${h.filesModified.join(", ")}`)}c.toolsUsed&&c.toolsUsed.length>0&&e.push(`Tools used: ${c.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(n?.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 l=n.designModel.rules;if(Array.isArray(l)&&l.length>0)if(e.push(""),e.push("Rules:"),l.length>0&&typeof l[0]=="object"&&l[0]!==null&&"scope"in l[0]){let b=new Map;for(let O of l)if(typeof O=="object"&&O!==null&&"text"in O){let C=O,v=C.scope||"general";b.has(v)||b.set(v,[]),b.get(v).push(C.text)}for(let[O,C]of b){e.push(`**${O.charAt(0).toUpperCase()+O.slice(1)}**`);for(let v of C)e.push(`- ${v}`)}}else for(let b of l)typeof b=="string"&&e.push(`- ${b}`);let c=n.designModel.tokens;c&&typeof c=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(c,null,2)),e.push("```"));let h=n.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.")}if(n?.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.")),i){let l=n?.screenshotPaths??{},c=new Map;for(let h of t.annotations){let m=h.pathname||new URL(t.url).pathname;c.has(m)||c.set(m,[]),c.get(m).push(h)}e.push(""),e.push("The developer annotated multiple pages. Each page section includes its own screenshot where available.");for(let[h,m]of c){e.push(""),e.push(`## Page: ${h}`);let b=l[h];b&&n?.provider!=="codex"?e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${b}`):!b&&n?.provider!=="codex"&&(Object.keys(l).length===0?(e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${r}`),e.push("(This screenshot shows the page the developer was on when submitting \u2014 not this page)")):e.push("(No screenshot available for this page \u2014 work from the annotation text and selectors)"));let O={...t,annotations:m,styleModifications:t.styleModifications},C=fe(O,n?.imagePaths);C&&(e.push(""),e.push(C))}}else{let l=fe(t,n?.imagePaths);l&&(e.push(""),e.push(l))}return 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(`
200
+ `)}function tt(r){return r.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function nt(r,t,n,e){let s=[];s.push("You are continuing work on a UI based on the developer's reply to your question."),s.push(""),n!=="codex"&&s.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`);let i=t.find(l=>l.role==="human"&&l.feedbackContext);if(i?.feedbackContext&&(s.push(""),s.push(i.feedbackContext)),t.length>0){s.push(""),s.push("## Conversation History");let l=0;for(let c of t)c.role==="human"?(l++,c.replyToQuestion?(s.push(`### Round ${l} (human) \u2014 reply`),s.push(`"${c.replyToQuestion}"`)):(s.push(`### Round ${l} (human)`),c.feedbackSummary&&s.push(`Annotations: ${c.feedbackSummary}`))):c.question?(s.push(`### Round ${l} (assistant) \u2014 question`),s.push(`"${c.question}"`)):(s.push(`### Round ${l} (assistant)`),c.responseText&&s.push(`Response: ${c.responseText}`))}if(s.push(""),s.push("The developer answered your question. Continue working based on their reply."),s.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),s.push(""),s.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){s.push(""),s.push("## Attached Images"),s.push("The developer attached reference images with their reply:");for(let l of e)s.push(`Attached image: use the Read tool to view the image at: ${l}`)}return s.push(""),s.push("## Resolution"),s.push("After completing all work, output a resolution block listing what you did for each annotation:"),s.push("<resolution>"),s.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),s.push("</resolution>"),s.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),s.push(""),s.push("### Scope classification"),s.push("Each resolution MUST include scope fields:"),s.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),s.push("- `inferredScope`: What scope the change actually has, based on what you modified."),s.push("Scope has two dimensions:"),s.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),s.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),s.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),s.push("If you cannot confidently determine scope, set it to null."),s.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),s.push(""),s.push("## Questions"),s.push("If you still need clarification, output:"),s.push("<question>Your question here</question>"),s.push("You may output BOTH a <resolution> and a <question> in the same response."),s.join(`
201
+ `)}function Yt(r){if(typeof r!="object"||r===null)return!1;let t=r;return(t.breadth==="instance"||t.breadth==="pattern")&&(t.target==="element"||t.target==="component"||t.target==="token")}function Qt(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 t=r;for(let n of["declaredScope","inferredScope","finalScope"])if(t[n]!==void 0&&t[n]!==null&&!Yt(t[n]))return!1;return!0}function st(r){let t=r.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!t||!t[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(Qt):[]}catch{return[]}}function ot(r){let t=r.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!t?.[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(e=>{if(typeof e!="object"||e===null)return!1;let s=e;return(s.category==="token"||s.category==="component"||s.category==="element")&&typeof s.element=="string"&&typeof s.decision=="string"&&typeof s.reason=="string"}):[]}catch{return[]}}var xe=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.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(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let s=this.bufferEvent(n,t);for(let i of this.listeners)i(s,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let s={...n,seq:e.nextSeq++};return e.events.push(s),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),s}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),s=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let l=n<0?e.events:e.events.filter(c=>c.seq>n);return{jobId:t,events:l,currentSeq:e.nextSeq-1,accumulated:{...s},jobActive:i}}accumulateText(t,n,e){let s=this.accumulators.get(t);s||(s={response:"",thinking:""},this.accumulators.set(t,s)),s[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),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 t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(s=>{let i=!1,l=()=>{i||(i=!0,s())};e.on("exit",l),e.on("error",l),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(l,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,message:t.error},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Xt,readFile as Vt,writeFile as Kt}from"fs/promises";import{dirname as Zt,join as en}from"path";var tn={version:1,threads:{}},be=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=en(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await Vt(this.filePath,"utf-8"),n=JSON.parse(t);if(n&&n.version===1&&n.threads)return this.cache=n,this.cache}catch{}return this.cache={...tn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let s of Object.values(n.threads))if(s.elementIdentifiers.some(l=>e.has(l)))return s;return null}async createThread(t,n){let e=await this.load(),s={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=s,await this.persist(),s}async appendMessage(t,n){let s=(await this.load()).threads[t];s&&(s.messages.push(n),s.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let s=(await this.load()).threads[t];if(!s)return;let i=new Set(s.elementIdentifiers);for(let l of n)i.has(l)||s.elementIdentifiers.push(l);s.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((s,i)=>i.updatedAt-s.updatedAt),e.slice(0,t).map(s=>{let l=s.messages.find(c=>c.role==="human")?.feedbackSummary||"[thread]";return{id:s.id,createdAt:s.createdAt,updatedAt:s.updatedAt,preview:l,messageCount:s.messages.length,elementIdentifiers:s.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Xt(Zt(this.filePath),{recursive:!0}),await Kt(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var rt="0.6.6";var fn=1111,mn=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],gn=1800*1e3,yn=3600*1e3;function lt(r){if(!r)return!1;try{let t=new URL(r);return t.hostname==="localhost"||t.hostname==="127.0.0.1"}catch{return!1}}function wn(r){let t=[];for(let n of r)if(n.type==="delta"){let e=n.text;if(!e)continue;let s=t[t.length-1];s&&s.kind==="text"?s.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",s=n.file??void 0,i=n.content??void 0,l=s?s.split("/").pop()??s:void 0,c;switch(e){case"Read":c=l?`Reading ${l}`:"Reading file";break;case"Edit":c=l?`Editing ${l}`:"Editing file";break;case"Write":c=l?`Writing ${l}`:"Writing file";break;case"Bash":c=i?i.split(`
202
+ `)[0].trim().slice(0,60):"Running command";break;case"Glob":c="Searching files";break;case"Grep":c="Searching code";break;case"WebFetch":c="Fetching page";break;case"WebSearch":c="Searching web";break;default:c=e?`Using ${e}`:"tool";break}let h=s??i??void 0,m=t[t.length-1];m&&m.kind==="tool_group"&&m.tool===e?m.items.push({label:c,detail:h}):t.push({kind:"tool_group",tool:e,items:[{label:c,detail:h}]})}return t}function vn(r,t){let n=r.headers.origin;lt(n)&&(t.setHeader("Access-Control-Allow-Origin",n),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function w(r,t,n){r.writeHead(t,{"Content-Type":"application/json"}),r.end(JSON.stringify(n))}function Sn(r,t){if(!r)return t;let n=r.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!n)return t;let[,e,s,i]=n;return`\x1B[38;2;${parseInt(e,16)};${parseInt(s,16)};${parseInt(i,16)}m${t}\x1B[0m`}function Ae(r,t){try{r.res.write(`event: ${t.type}
115
203
  data: ${JSON.stringify(t)}
116
204
 
117
- `)}catch{}}async function mn(i){try{let t=new AbortController,n=setTimeout(()=>t.abort(),500),e=await fetch(`http://127.0.0.1:${i}/status`,{signal:t.signal});return clearTimeout(n),e.ok?await e.json():null}catch{return null}}function gn(i,t){return new Promise((n,e)=>{let s=c=>{i.removeListener("listening",l),e(c)},l=()=>{i.removeListener("error",s),n()};i.once("error",s),i.once("listening",l),i.listen(t,"127.0.0.1")})}async function Oe(i={}){let t=i.port??cn,n=i.projectRoot??process.cwd(),e=Vt("sha256").update(n).digest("hex").slice(0,12),s=i.devOrigin??(process.env.PORT?`http://localhost:${process.env.PORT}`:null),l=i.tempDir??se(sn(),"popmelt-bridge"),c=i.maxTurns??40,r=i.maxBudgetUsd??1,h=[...i.allowedTools??ln],f=i.claudePath??"claude",N=i.provider??"claude",U=i.timeoutMs,O=t,I={};for(let o of["claude","codex"])try{let a=Xt("which",[o],{encoding:"utf-8"}).trim();I[o]={available:!0,path:a}}catch{I[o]={available:!1,path:null}}function z(o,a){return new Promise(p=>{let d=Yt(a,["--version"],{stdio:["ignore","ignore","ignore"]}),m=!1,u=R=>{m||(m=!0,p(R))},b=setTimeout(()=>{d.kill("SIGTERM"),u(!0)},5e3);d.on("error",()=>{clearTimeout(b),u(!1)}),d.on("close",R=>{clearTimeout(b),u(R===0)})})}let[L,W]=await Promise.all([Te(n),Ee(n)]);I.claude&&(I.claude.mcp=L),I.codex&&(I.codex.mcp=W),L.found&&L.name&&h.push(`mcp__${L.name}__*`),await Kt(l,{recursive:!0}),ot(l).catch(()=>{});let T=new we(1),J=new Set,C=new Se(n),B=new ge(n),g=new ye(n,B,{claudePath:f,onEvent:o=>{for(let a of J)Ce(a,o)}}),F=20,y=300*1e3,w=[],S=null,D;T.addListener((o,a,p)=>{for(let d of J)(!p||!d.sourceId||d.sourceId===p)&&Ce(d,o)}),T.setProcessor(async o=>{let a=o._replyPrompt,p=o._replyImagePaths,d=o.provider??N,m;if(o.threadId){let E=await C.getThread(o.threadId);if(E){for(let P=E.messages.length-1;P>=0;P--)if(E.messages[P].sessionId){m=E.messages[P].sessionId;break}}}let u;if(m&&a){let E=(await C.getThread(o.threadId))?.messages.filter(H=>H.role==="human").pop();if(u=E?.replyToQuestion||E?.feedbackSummary||"",p&&p.length>0){u+=`
205
+ `)}catch{}}async function xn(r){try{let t=new AbortController,n=setTimeout(()=>t.abort(),500),e=await fetch(`http://127.0.0.1:${r}/status`,{signal:t.signal});return clearTimeout(n),e.ok?await e.json():null}catch{return null}}function bn(r,t){return new Promise((n,e)=>{let s=l=>{r.removeListener("listening",i),e(l)},i=()=>{r.removeListener("error",s),n()};r.once("error",s),r.once("listening",i),r.listen(t,"127.0.0.1")})}async function De(r={}){let t=r.port??fn,n=r.projectRoot??process.cwd(),e=sn("sha256").update(n).digest("hex").slice(0,12),s=r.devOrigin??(process.env.PORT?`http://localhost:${process.env.PORT}`:null),i=r.tempDir??se(dn(),"popmelt-bridge"),l=r.maxTurns??40,c=r.maxBudgetUsd??1,h=[...r.allowedTools??mn],m=r.claudePath??"claude",b=r.provider??"claude",O=r.timeoutMs,C=t,v={};for(let o of["claude","codex"])try{let a=nn("which",[o],{encoding:"utf-8"}).trim();v[o]={available:!0,path:a}}catch{v[o]={available:!1,path:null}}function H(o,a){return new Promise(p=>{let d=it(a,["--version"],{stdio:["ignore","ignore","ignore"]}),f=!1,u=D=>{f||(f=!0,p(D))},y=setTimeout(()=>{d.kill("SIGTERM"),u(!0)},5e3);d.on("error",()=>{clearTimeout(y),u(!1)}),d.on("close",D=>{clearTimeout(y),u(D===0)})})}let[L,X]=await Promise.all([Ee(n),Me(n)]);v.claude&&(v.claude.mcp=L),v.codex&&(v.codex.mcp=X),L.found&&L.name&&h.push(`mcp__${L.name}__*`),await on(i,{recursive:!0}),ct(i).catch(()=>{});let R=new xe(1),U=new Set,A=new be(n),F=new ye(n),g=new we(n,F,{claudePath:m,onEvent:o=>{for(let a of U)Ae(a,o)}}),j=20,S=300*1e3,k=[],I=new Set,B=null,_;R.addListener((o,a,p)=>{for(let d of U)(!p||!d.sourceId||d.sourceId===p)&&Ae(d,o)}),R.setProcessor(async o=>{let a=o._replyPrompt,p=o._replyImagePaths,d=o.provider??b,f;if(o.threadId){let P=await A.getThread(o.threadId);if(P){for(let T=P.messages.length-1;T>=0;T--)if(P.messages[T].sessionId){f=P.messages[T].sessionId;break}}}let u;if(f&&a){let P=(await A.getThread(o.threadId))?.messages.filter(Y=>Y.role==="human").pop();if(u=P?.replyToQuestion||P?.feedbackSummary||"",p&&p.length>0){u+=`
118
206
 
119
- The developer attached reference images with their reply:`;for(let H of p)u+=`
120
- Attached image: use the Read tool to view the image at: ${H}`}u+=`
207
+ The developer attached reference images with their reply:`;for(let Y of p)u+=`
208
+ Attached image: use the Read tool to view the image at: ${Y}`}u+=`
121
209
 
122
- After completing work, output a <resolution> block with declaredScope and inferredScope. If the developer corrected scope, set finalScope. If unclear, output a <question> block.`}else if(m)u=pe(o.feedback,o.imagePaths)+`
210
+ After completing work, output a <resolution> block with declaredScope and inferredScope. If the developer corrected scope, set finalScope. If unclear, output a <question> block.`}else if(f)u=fe(o.feedback,o.imagePaths)+`
123
211
 
124
212
  Follow the developer's instructions. If they ask for changes, apply them to the source files.
125
213
 
126
214
  After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(d!=="codex"?`
127
215
 
128
- IMPORTANT: First, use the Read tool to view the updated screenshot at: ${o.screenshotPath}`:"");else{let E=!a&&o.threadId?await C.getThreadHistory(o.threadId):void 0,P=a?null:await g.loadModel();u=a??Ve(o.screenshotPath,o.feedback,{threadHistory:E&&E.length>0?E:void 0,provider:d,imagePaths:o.imagePaths,designModel:P??void 0,screenshotPaths:o.screenshotPaths})}let b=fn(o.color,`[\u22B9 ${O}:${o.id}]`),R=o.screenshotPaths&&Object.keys(o.screenshotPaths).length>0?`${Object.keys(o.screenshotPaths).length} pages [${Object.keys(o.screenshotPaths).join(", ")}]`:o.screenshotPath;console.log(`${b} Reviewing ${R} (provider: ${d})${o.threadId?` (thread: ${o.threadId})`:""}${m?` (resuming: ${m.slice(0,8)})`:""}`);let X=(E,P)=>{E.type==="delta"&&"text"in E?T.accumulateText(P,"response",E.text):E.type==="thinking"&&"text"in E&&T.accumulateText(P,"thinking",E.text),T.broadcast(E,P,o.sourceId)},{process:K,result:q}=d==="codex"?Be(o.id,{prompt:u,projectRoot:n,screenshotPath:o.screenshotPath,resumeSessionId:m,model:o.model,onEvent:X}):me(o.id,{prompt:u,projectRoot:n,maxTurns:c,maxBudgetUsd:r,allowedTools:h,claudePath:f,resumeSessionId:m,model:o.model,timeoutMs:U,onEvent:X});T.setActiveProcess(o.id,K);let x=await q;if(o.result=x.text,x.success){console.log(`${b} Iteration complete`),x.fileEdits&&x.fileEdits.length>0&&console.log(`${b} Captured ${x.fileEdits.length} file edit(s): ${x.fileEdits.map(j=>`${j.tool} ${j.file_path}`).join(", ")}`),o.status="done";let E=Ke(x.text),P=et(x.text);if(P.length>0&&o.annotationIds&&o.annotationIds.length>0){let j=new Set(o.annotationIds);P.every(M=>j.has(M.annotationId))||(P=P.map((M,G)=>({...M,annotationId:o.annotationIds[G%o.annotationIds.length]})))}let H=x.fileEdits&&x.fileEdits.length>0?x.fileEdits.map(j=>`${j.tool} ${j.file_path.split("/").pop()}`):x.toolsUsed,V=T.getBufferedEvents(o.id),oe=V?pn(V.events):void 0;o.threadId&&await C.appendMessage(o.threadId,{role:"assistant",timestamp:Date.now(),jobId:o.id,responseText:x.text,resolutions:P.length>0?P:void 0,question:E??void 0,sessionId:x.sessionId,toolsUsed:H,segments:oe&&oe.length>0?oe:void 0,model:o.model,provider:o.provider}),B.captureGitDiff(n).then(async j=>{let _=Date.now(),M=o.imagePaths?Object.values(o.imagePaths).flat():[],G=[];if(o.imagePaths)for(let[re,k]of Object.entries(o.imagePaths))for(let Q=0;Q<k.length;Q++)G.push(`screenshots/p-${o.id}-${re}-${Q}.png`);await B.persist({version:1,id:o.id,createdAt:o.createdAt,completedAt:_,durationMs:_-o.createdAt,url:o.feedback.url,viewport:o.feedback.viewport,screenshotPath:`screenshots/s-${o.id}.png`,pastedImagePaths:G,annotations:o.feedback.annotations,styleModifications:o.feedback.styleModifications,inspectedElement:o.feedback.inspectedElement,provider:o.provider,model:o.model,sessionId:x.sessionId,threadId:o.threadId,responseText:x.text,resolutions:P.length>0?P:[],question:E??void 0,fileEdits:x.fileEdits??[],toolsUsed:H,gitDiff:j},o.screenshotPath,M)}).catch(()=>{}),P.length>0&&P.some(_=>(_.finalScope??_.inferredScope)?.breadth==="pattern")&&g.run().catch(()=>{}),E&&(console.log(`${b} \u{1F4AC} Question detected: "${E.slice(0,120)}" \u2192 broadcasting to ${J.size} SSE clients (threadId=${o.threadId??o.id}, annotationIds=${o.annotationIds?.join(",")??"none"})`),T.broadcast({type:"question",jobId:o.id,threadId:o.threadId??o.id,question:E,annotationIds:o.annotationIds},o.id,o.sourceId));let te=tt(x.text);te.length>0&&(console.log(`${b} Novel pattern(s): ${te.map(j=>`${j.category}/${j.element}`).join(", ")}`),T.broadcast({type:"novel_patterns",jobId:o.id,patterns:te,threadId:o.threadId},o.id,o.sourceId)),T.broadcast({type:"done",jobId:o.id,success:!0,resolutions:P.length>0?P:void 0,responseText:x.text,threadId:o.threadId},o.id,o.sourceId),w.push({id:o.id,status:"done",completedAt:Date.now(),threadId:o.threadId,annotationIds:o.annotationIds})}else console.error(`${b} Error: ${x.error}`),o.status="error",o.error=x.error,o.threadId&&await C.appendMessage(o.threadId,{role:"assistant",timestamp:Date.now(),jobId:o.id,error:x.error||"Unknown error",model:o.model,provider:o.provider}),T.broadcast({type:"error",jobId:o.id,threadId:o.threadId,message:x.error||"Unknown error"},o.id,o.sourceId),w.push({id:o.id,status:"error",completedAt:Date.now(),error:x.error,threadId:o.threadId,annotationIds:o.annotationIds});let Y=Date.now()-y;for(;w.length>0&&(w[0].completedAt<Y||w.length>F);)w.shift()});let A=nn(async(o,a)=>{if(hn(o,a),o.method==="OPTIONS"){a.writeHead(204),a.end();return}let p=o.url||"/",d=p.split("?")[0],m=new URL(p,`http://127.0.0.1:${O}`),u=m.pathname;try{if(o.method==="POST"&&u==="/send")await ee(o,a);else if(o.method==="GET"&&u==="/events")ce(o,a);else if(o.method==="GET"&&u==="/status")he(a);else if(o.method==="PATCH"&&u==="/config")await rt(o,a);else if(o.method==="POST"&&u==="/shutdown")v(a,200,{ok:!0}),setTimeout(()=>process.exit(0),100);else if(o.method==="GET"&&u==="/capabilities")v(a,200,{providers:I});else if(o.method==="POST"&&u==="/mcp/install")await lt(o,a);else if(o.method==="POST"&&u==="/reply")await $(o,a);else if(o.method==="POST"&&u==="/cancel")at(o,a);else if(o.method==="POST"&&u==="/materialize")await ct(a);else if(o.method==="POST"&&u==="/model/component")await dt(o,a);else if(o.method==="DELETE"&&u==="/model/component")await ut(o,a);else if(o.method==="PATCH"&&u==="/model/token")await pt(o,a);else if(o.method==="DELETE"&&u==="/model/token")await ht(o,a);else if(o.method==="GET"&&u==="/model"){let b=await g.loadModel();v(a,200,{model:b})}else if(o.method==="GET"&&u.startsWith("/jobs/")&&u.endsWith("/events")){let b=u.slice(6,u.length-7),R=parseInt(m.searchParams.get("afterSeq")??"-1",10),X=T.getBufferedEvents(b,isNaN(R)?-1:R);X?v(a,200,X):v(a,404,{error:"Unknown job"})}else if(o.method==="GET"&&d.startsWith("/files/"))await yt(d.slice(7),a);else if(o.method==="GET"&&u==="/threads/recent"){let b=Math.min(Math.max(parseInt(m.searchParams.get("limit")??"5",10)||5,1),20),R=await C.listRecent(b);v(a,200,R)}else if(o.method==="GET"&&u.startsWith("/thread/")){let b=u.slice(8);await vt(b,a)}else o.method==="GET"&&(u==="/canvas"||u==="/canvas/")?ft(o,a):o.method==="GET"&&u==="/canvas/manifest"?await mt(a):o.method==="GET"&&u==="/canvas/app.mjs"?await gt(a):v(a,404,{error:"Not found"})}catch(b){console.error("[Bridge] Request error:",b),v(a,500,{error:b instanceof Error?b.message:"Internal error"})}});async function ee(o,a){let{screenshot:p,feedback:d,color:m,provider:u,model:b,sourceId:R,pastedImages:X,pageScreenshots:K}=await Re(o),q;try{q=JSON.parse(d)}catch{v(a,400,{error:"Invalid feedback JSON"});return}let x=Me().slice(0,8),Y={};if(K.length>0)for(let k of K){let Q=encodeURIComponent(k.pathname),ie=se(l,`screenshot-${x}-${Q}.png`);await xe(ie,k.data),Y[k.pathname]=ie}let E=se(l,`screenshot-${x}.png`);await xe(E,p);let P={};if(X.length>0)for(let k of X){let Q=se(l,`pasted-${x}-${k.annotationId}-${k.index}.png`);await xe(Q,k.data),P[k.annotationId]||(P[k.annotationId]=[]),P[k.annotationId].push(Q)}let H=q.annotations.map(k=>k.linkedSelector?k.pathname?`${k.pathname}:${k.linkedSelector}`:k.linkedSelector:null).filter(k=>!!k),V;if(H.length>0){let k=await C.findContinuationThread(H);k?(V=k.id,await C.addElementIdentifiers(V,H)):V=(await C.createThread(x,H)).id}else V=(await C.createThread(x,[])).id;let oe=q.annotations.map(k=>k.id),te=Object.keys(Y).length>0,j={id:x,status:"queued",screenshotPath:E,feedback:q,createdAt:Date.now(),color:m,threadId:V,annotationIds:oe,provider:u==="claude"||u==="codex"?u:void 0,model:b||void 0,...Object.keys(P).length>0?{imagePaths:P}:{},sourceId:R||void 0,...te?{screenshotPaths:Y}:{}},_=new Set(q.annotations.map(k=>k.pathname).filter(Boolean)),M;if(_.size>1){let k=new Map;for(let Q of q.annotations){let ie=Q.pathname||"(unknown)";k.has(ie)||k.set(ie,[]),k.get(ie).push(Q.instruction||`[${Q.type}]`)}M=[...k.entries()].map(([Q,ie])=>`\`${Q}\`
129
- ${ie.map(St=>`- ${St}`).join(`
216
+ IMPORTANT: First, use the Read tool to view the updated screenshot at: ${o.screenshotPath}`:"");else{let P=!a&&o.threadId?await A.getThreadHistory(o.threadId):void 0,T=a?null:await g.loadModel();u=a??et(o.screenshotPath,o.feedback,{threadHistory:P&&P.length>0?P:void 0,provider:d,imagePaths:o.imagePaths,designModel:T??void 0,screenshotPaths:o.screenshotPaths})}let y=Sn(o.color,`[\u22B9 ${C}:${o.id}]`),D=o.screenshotPaths&&Object.keys(o.screenshotPaths).length>0?`${Object.keys(o.screenshotPaths).length} pages [${Object.keys(o.screenshotPaths).join(", ")}]`:o.screenshotPath;console.log(`${y} Reviewing ${D} (provider: ${d})${o.threadId?` (thread: ${o.threadId})`:""}${f?` (resuming: ${f.slice(0,8)})`:""}`);let z=(P,T)=>{P.type==="delta"&&"text"in P?R.accumulateText(T,"response",P.text):P.type==="thinking"&&"text"in P&&R.accumulateText(T,"thinking",P.text),R.broadcast(P,T,o.sourceId)},{process:V,result:G}=d==="codex"?Ue(o.id,{prompt:u,projectRoot:n,screenshotPath:o.screenshotPath,resumeSessionId:f,model:o.model,onEvent:z}):ue(o.id,{prompt:u,projectRoot:n,maxTurns:l,maxBudgetUsd:c,allowedTools:h,claudePath:m,resumeSessionId:f,model:o.model,timeoutMs:O,onEvent:z});R.setActiveProcess(o.id,V);let x=await G;if(o.result=x.text,x.success){console.log(`${y} Iteration complete`),x.fileEdits&&x.fileEdits.length>0&&console.log(`${y} Captured ${x.fileEdits.length} file edit(s): ${x.fileEdits.map(J=>`${J.tool} ${J.file_path}`).join(", ")}`),o.status="done";let P=tt(x.text),T=st(x.text);if(T.length>0&&o.annotationIds&&o.annotationIds.length>0){let J=new Set(o.annotationIds);T.every(N=>J.has(N.annotationId))||(T=T.map((N,Q)=>({...N,annotationId:o.annotationIds[Q%o.annotationIds.length]})))}let Y=x.fileEdits&&x.fileEdits.length>0?x.fileEdits.map(J=>`${J.tool} ${J.file_path.split("/").pop()}`):x.toolsUsed,ee=R.getBufferedEvents(o.id),oe=ee?wn(ee.events):void 0;if(o.threadId&&await A.appendMessage(o.threadId,{role:"assistant",timestamp:Date.now(),jobId:o.id,responseText:x.text,resolutions:T.length>0?T:void 0,question:P??void 0,sessionId:x.sessionId,toolsUsed:Y,segments:oe&&oe.length>0?oe:void 0,model:o.model,provider:o.provider}),F.captureGitDiff(n).then(async J=>{let M=Date.now(),N=o.imagePaths?Object.values(o.imagePaths).flat():[],Q=[];if(o.imagePaths)for(let[ae,$]of Object.entries(o.imagePaths))for(let K=0;K<$.length;K++)Q.push(`screenshots/p-${o.id}-${ae}-${K}.webp`);await F.persist({version:1,id:o.id,createdAt:o.createdAt,completedAt:M,durationMs:M-o.createdAt,url:o.feedback.url,viewport:o.feedback.viewport,screenshotPath:`screenshots/s-${o.id}.webp`,pastedImagePaths:Q,annotations:o.feedback.annotations,styleModifications:o.feedback.styleModifications,inspectedElement:o.feedback.inspectedElement,provider:o.provider,model:o.model,sessionId:x.sessionId,threadId:o.threadId,responseText:x.text,resolutions:T.length>0?T:[],question:P??void 0,fileEdits:x.fileEdits??[],toolsUsed:Y,gitDiff:J},o.screenshotPath,N)}).catch(()=>{}),T.length>0&&T.some(M=>(M.finalScope??M.inferredScope)?.breadth==="pattern")&&g.run().catch(()=>{}),o.kind==="synthesize"&&x.text){let J=ve(x.text);if(J){Array.isArray(J.rules)&&(J.rules=pe(J.rules));let M=await g.loadModel();M&&(!J.tokens&&M.tokens&&(J.tokens=M.tokens),!J.components&&M.components&&(J.components=M.components)),await g.writeModel(J),console.log(`${y} Synthesize: model.json updated`)}}P&&(console.log(`${y} \u{1F4AC} Question detected: "${P.slice(0,120)}" \u2192 broadcasting to ${U.size} SSE clients (threadId=${o.threadId??o.id}, annotationIds=${o.annotationIds?.join(",")??"none"})`),R.broadcast({type:"question",jobId:o.id,threadId:o.threadId??o.id,question:P,annotationIds:o.annotationIds},o.id,o.sourceId));let ne=ot(x.text);ne.length>0&&(console.log(`${y} Novel pattern(s): ${ne.map(J=>`${J.category}/${J.element}`).join(", ")}`),R.broadcast({type:"novel_patterns",jobId:o.id,patterns:ne,threadId:o.threadId},o.id,o.sourceId)),R.broadcast({type:"done",jobId:o.id,success:!0,resolutions:T.length>0?T:void 0,responseText:x.text,threadId:o.threadId},o.id,o.sourceId),k.push({id:o.id,status:"done",completedAt:Date.now(),threadId:o.threadId,annotationIds:o.annotationIds})}else console.error(`${y} Error: ${x.error}`),o.status="error",o.error=x.error,o.threadId&&await A.appendMessage(o.threadId,{role:"assistant",timestamp:Date.now(),jobId:o.id,error:x.error||"Unknown error",model:o.model,provider:o.provider}),R.broadcast({type:"error",jobId:o.id,threadId:o.threadId,message:x.error||"Unknown error"},o.id,o.sourceId),k.push({id:o.id,status:"error",completedAt:Date.now(),error:x.error,threadId:o.threadId,annotationIds:o.annotationIds});let W=Date.now()-S;for(;k.length>0&&(k[0].completedAt<W||k.length>j);)k.shift()});let Z=ln(async(o,a)=>{if(vn(o,a),o.method==="OPTIONS"){a.writeHead(204),a.end();return}let p=o.url||"/",d=p.split("?")[0],f=new URL(p,`http://127.0.0.1:${C}`),u=f.pathname;try{if(o.method==="POST"&&u==="/send")await E(o,a);else if(o.method==="GET"&&u==="/events")me(o,a);else if(o.method==="GET"&&u==="/status")dt(a);else if(o.method==="PATCH"&&u==="/config")await ut(o,a);else if(o.method==="POST"&&u==="/shutdown")w(a,200,{ok:!0}),setTimeout(()=>process.exit(0),100);else if(o.method==="POST"&&u==="/restart")w(a,200,{ok:!0,restarting:!0}),setTimeout(()=>{for(let y of U)try{y.res.end()}catch{}Z.close(()=>{it(process.execPath,process.argv.slice(1),{detached:!0,stdio:"ignore",cwd:process.cwd(),env:process.env}).unref(),setTimeout(()=>process.exit(0),200)})},100);else if(o.method==="GET"&&u==="/capabilities")w(a,200,{providers:v});else if(o.method==="POST"&&u==="/mcp/install")await mt(o,a);else if(o.method==="POST"&&u==="/reply")await le(o,a);else if(o.method==="POST"&&u==="/cancel")pt(o,a);else if(o.method==="POST"&&u==="/materialize")await ht(a);else if(o.method==="POST"&&u==="/model/component")await gt(o,a);else if(o.method==="DELETE"&&u==="/model/component")await yt(o,a);else if(o.method==="PATCH"&&u==="/model/token")await wt(o,a);else if(o.method==="DELETE"&&u==="/model/token")await vt(o,a);else if(o.method==="POST"&&u==="/model/consolidate")g.consolidate().catch(y=>console.error("[Bridge] Consolidation error:",y)),w(a,200,{started:!0});else if(o.method==="POST"&&u==="/model/synthesize")await ft(o,a);else if(o.method==="GET"&&u==="/model"){let y=await g.loadModel();w(a,200,{model:y})}else if(o.method==="GET"&&u.startsWith("/jobs/")&&u.endsWith("/events")){let y=u.slice(6,u.length-7),D=parseInt(f.searchParams.get("afterSeq")??"-1",10),z=R.getBufferedEvents(y,isNaN(D)?-1:D);z?w(a,200,z):w(a,404,{error:"Unknown job"})}else if(o.method==="GET"&&d.startsWith("/files/"))await Pt(d.slice(7),a);else if(o.method==="GET"&&u==="/threads/recent"){let y=Math.min(Math.max(parseInt(f.searchParams.get("limit")??"5",10)||5,1),20),D=await A.listRecent(y);w(a,200,D)}else if(o.method==="GET"&&u.startsWith("/thread/")){let y=u.slice(8);await kt(y,a)}else o.method==="GET"&&(u==="/canvas"||u==="/canvas/")?St(o,a):o.method==="GET"&&u==="/canvas/manifest"?await xt(a):o.method==="GET"&&u==="/canvas/app.mjs"?await bt(a):w(a,404,{error:"Not found"})}catch(y){console.error("[Bridge] Request error:",y),w(a,500,{error:y instanceof Error?y.message:"Internal error"})}});async function E(o,a){let{screenshot:p,feedback:d,color:f,provider:u,model:y,sourceId:D,pastedImages:z,pageScreenshots:V}=await Oe(o),G;try{G=JSON.parse(d)}catch{w(a,400,{error:"Invalid feedback JSON"});return}let x=Pe().slice(0,8),W={};if(V.length>0)for(let $ of V){let K=encodeURIComponent($.pathname),re=se(i,`screenshot-${x}-${K}.webp`);await ke(re,$.data),W[$.pathname]=re}let P=se(i,`screenshot-${x}.webp`);await ke(P,p);let T={};if(z.length>0)for(let $ of z){let K=se(i,`pasted-${x}-${$.annotationId}-${$.index}.webp`);await ke(K,$.data),T[$.annotationId]||(T[$.annotationId]=[]),T[$.annotationId].push(K)}let Y=G.annotations.map($=>$.linkedSelector?$.pathname?`${$.pathname}:${$.linkedSelector}`:$.linkedSelector:null).filter($=>!!$),ee;if(Y.length>0){let $=await A.findContinuationThread(Y);$?(ee=$.id,await A.addElementIdentifiers(ee,Y)):ee=(await A.createThread(x,Y)).id}else ee=(await A.createThread(x,[])).id;let oe=G.annotations.map($=>$.id),ne=Object.keys(W).length>0,J={id:x,status:"queued",screenshotPath:P,feedback:G,createdAt:Date.now(),color:f,threadId:ee,annotationIds:oe,provider:u==="claude"||u==="codex"?u:void 0,model:y||void 0,...Object.keys(T).length>0?{imagePaths:T}:{},sourceId:D||void 0,...ne?{screenshotPaths:W}:{}},M=new Set(G.annotations.map($=>$.pathname).filter(Boolean)),N;if(M.size>1){let $=new Map;for(let K of G.annotations){let re=K.pathname||"(unknown)";$.has(re)||$.set(re,[]),$.get(re).push(K.instruction||`[${K.type}]`)}N=[...$.entries()].map(([K,re])=>`\`${K}\`
217
+ ${re.map(Tt=>`- ${Tt}`).join(`
130
218
  `)}`).join(`
131
- `)}else M=q.annotations.map(k=>k.instruction||`[${k.type}]`).join("; ");let G=pe(q,Object.keys(P).length>0?P:void 0);await C.appendMessage(V,{role:"human",timestamp:Date.now(),jobId:x,screenshotPath:E,...te?{screenshotPaths:Y}:{},...Object.keys(P).length>0?{imagePaths:P}:{},annotationIds:oe,feedbackSummary:M,feedbackContext:G||void 0});let re=T.enqueue(j);v(a,200,{jobId:x,position:re,threadId:V})}async function $(o,a){let p=o.headers["content-type"]||"",d,m,u,b,R,X,K=[];if(p.includes("multipart/form-data")){let _=await Re(o),M=_.feedback?JSON.parse(_.feedback):{};d=M.threadId,m=M.reply,u=M.color,b=M.provider,R=M.model,X=M.sourceId||_.sourceId;for(let G of _.pastedImages)K.push(G.data)}else{let _=[];for await(let re of o)_.push(typeof re=="string"?Buffer.from(re):re);let M=Buffer.concat(_).toString("utf-8"),G;try{G=JSON.parse(M)}catch{v(a,400,{error:"Invalid JSON"});return}d=G.threadId,m=G.reply,u=G.color,b=G.provider,R=G.model,X=G.sourceId}if(!d||!m){v(a,400,{error:"Missing threadId or reply"});return}if(!await C.getThread(d)){v(a,404,{error:"Thread not found"});return}let x=Me().slice(0,8),Y=[];for(let _=0;_<K.length;_++){let M=se(l,`reply-${x}-${_}.png`);await xe(M,K[_]),Y.push(M)}let E="";{let _=await C.getThreadHistory(d);for(let M=_.length-1;M>=0;M--)if(_[M].screenshotPath){E=_[M].screenshotPath;break}}if(!E){v(a,400,{error:"No screenshot available"});return}await C.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:x,replyToQuestion:m,screenshotPath:E,...Y.length>0?{replyImagePaths:Y}:{}});let P=await C.getThreadHistory(d),H=[];for(let _ of P)if(_.annotationIds)for(let M of _.annotationIds)H.includes(M)||H.push(M);let V=b==="claude"||b==="codex"?b:void 0,oe=Ze(E,P,V,Y.length>0?Y:void 0),te={id:x,status:"queued",screenshotPath:E,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:u,threadId:d,annotationIds:H.length>0?H:void 0,provider:V,model:R||void 0,sourceId:X||void 0};te._replyPrompt=oe,Y.length>0&&(te._replyImagePaths=Y);let j=T.enqueue(te);v(a,200,{jobId:x,position:j,threadId:d})}function ce(o,a){a.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),a.write(`event: connected
219
+ `)}else N=G.annotations.map($=>$.instruction||`[${$.type}]`).join("; ");let Q=fe(G,Object.keys(T).length>0?T:void 0);await A.appendMessage(ee,{role:"human",timestamp:Date.now(),jobId:x,screenshotPath:P,...ne?{screenshotPaths:W}:{},...Object.keys(T).length>0?{imagePaths:T}:{},annotationIds:oe,feedbackSummary:N,feedbackContext:Q||void 0});let ae=R.enqueue(J);w(a,200,{jobId:x,position:ae,threadId:ee})}async function le(o,a){let p=o.headers["content-type"]||"",d,f,u,y,D,z,V=[];if(p.includes("multipart/form-data")){let M=await Oe(o),N=M.feedback?JSON.parse(M.feedback):{};d=N.threadId,f=N.reply,u=N.color,y=N.provider,D=N.model,z=N.sourceId||M.sourceId;for(let Q of M.pastedImages)V.push(Q.data)}else{let M=[];for await(let ae of o)M.push(typeof ae=="string"?Buffer.from(ae):ae);let N=Buffer.concat(M).toString("utf-8"),Q;try{Q=JSON.parse(N)}catch{w(a,400,{error:"Invalid JSON"});return}d=Q.threadId,f=Q.reply,u=Q.color,y=Q.provider,D=Q.model,z=Q.sourceId}if(!d||!f){w(a,400,{error:"Missing threadId or reply"});return}if(!await A.getThread(d)){w(a,404,{error:"Thread not found"});return}let x=Pe().slice(0,8),W=[];for(let M=0;M<V.length;M++){let N=se(i,`reply-${x}-${M}.webp`);await ke(N,V[M]),W.push(N)}let P="";{let M=await A.getThreadHistory(d);for(let N=M.length-1;N>=0;N--)if(M[N].screenshotPath){P=M[N].screenshotPath;break}}if(!P&&!I.has(d)){w(a,400,{error:"No screenshot available"});return}await A.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:x,replyToQuestion:f,screenshotPath:P,...W.length>0?{replyImagePaths:W}:{}});let T=await A.getThreadHistory(d),Y=[];for(let M of T)if(M.annotationIds)for(let N of M.annotationIds)Y.includes(N)||Y.push(N);let ee=y==="claude"||y==="codex"?y:void 0,oe=nt(P,T,ee,W.length>0?W:void 0),ne={id:x,status:"queued",screenshotPath:P,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:u,threadId:d,annotationIds:Y.length>0?Y:void 0,provider:ee,model:D||void 0,sourceId:z||void 0};I.has(d)&&(ne.kind="synthesize"),ne._replyPrompt=oe,W.length>0&&(ne._replyImagePaths=W);let J=R.enqueue(ne);w(a,200,{jobId:x,position:J,threadId:d})}function me(o,a){a.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),a.write(`event: connected
132
220
  data: {"status":"connected"}
133
221
 
134
- `),o.headers.origin&&it(o.headers.origin)&&s!==o.headers.origin&&(s=o.headers.origin);let d=new URL(o.url||"/",`http://127.0.0.1:${O}`).searchParams.get("sourceId")||void 0,m={id:Me().slice(0,8),res:a,sourceId:d};J.add(m),o.on("close",()=>{J.delete(m)})}function he(o){let a=T.allActive;v(o,200,{ok:!0,version:nt,projectId:e,devOrigin:s,activeJob:a[0]?{id:a[0].id,status:a[0].status}:null,activeJobs:a.map(p=>({id:p.id,status:p.status,threadId:p.threadId,annotationIds:p.annotationIds,color:p.color})),queueDepth:T.depth,recentJobs:w})}async function rt(o,a){let p=await new Promise(d=>{let m="";o.on("data",u=>{m+=u.toString()}),o.on("end",()=>d(m))});try{let d=JSON.parse(p);typeof d.devOrigin=="string"&&(s=d.devOrigin||null),v(a,200,{ok:!0,devOrigin:s})}catch{v(a,400,{error:"Invalid JSON"})}}async function at(o,a){let d=new URL(o.url||"/",`http://127.0.0.1:${O}`).searchParams.get("jobId"),u=(d?T.allActive.filter(R=>R.id===d):T.allActive).map(R=>R.threadId).filter(Boolean),b=d?T.cancelJob(d):T.cancelActive();for(let R of u)await C.appendMessage(R,{role:"assistant",timestamp:Date.now(),jobId:d||"",cancelled:!0});v(a,200,{cancelled:b})}async function ct(o){if(g.isRunning){v(o,200,{skipped:!0,reason:"Already running"});return}let a=await g.getUnmaterializedPatternDecisions();if(a.length===0){v(o,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}g.run().catch(()=>{}),v(o,200,{started:!0,decisionCount:a.length,decisionIds:a.map(p=>p.id)})}async function lt(o,a){let p=[];for await(let R of o)p.push(typeof R=="string"?Buffer.from(R):R);let d;if(p.length>0)try{d=JSON.parse(Buffer.concat(p).toString("utf-8")).serverUrl}catch{}let m=[];I.claude?.available&&I.claude.mcp&&!I.claude.mcp.found&&m.push(await Xe(d)),I.codex?.available&&I.codex.mcp&&!I.codex.mcp.found&&m.push(await Ye(d));let[u,b]=await Promise.all([Te(n),Ee(n)]);I.claude&&(I.claude.mcp=u),I.codex&&(I.codex.mcp=b),v(a,200,{results:m,capabilities:{providers:I}})}async function dt(o,a){let p=[];for await(let u of o)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{v(a,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){v(a,400,{error:"Missing or invalid name"});return}let m=await g.addComponent(d.name);v(a,200,m)}async function ut(o,a){let p=[];for await(let u of o)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{v(a,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){v(a,400,{error:"Missing or invalid name"});return}let m=await g.removeComponent(d.name);v(a,200,m)}async function pt(o,a){let p=[];for await(let u of o)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{v(a,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){v(a,400,{error:"Missing or invalid path/value"});return}let m=await g.updateToken(d.path,d.value);v(a,200,m)}async function ht(o,a){let p=[];for await(let u of o)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{v(a,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){v(a,400,{error:"Missing or invalid path"});return}let m=await g.removeToken(d.path);v(a,200,m)}function ft(o,a){let p=s??"http://localhost:3000";if(!s){let m=o.headers.referer||o.headers.origin;if(m)try{let u=new URL(typeof m=="string"?m:m[0]||"");(u.hostname==="localhost"||u.hostname==="127.0.0.1")&&(p=u.origin)}catch{}}let d=De(O,p);a.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),a.end(d)}async function mt(o){let a=Date.now();if(S&&a<S.expires){v(o,200,S.data);return}try{let{scanForComponents:p}=await import("./react-scanner-ZXYS5M3Y.mjs"),{generateRenderFiles:d}=await import("./render-generator-EANIDD2E.mjs"),m=await p(n);S={data:m,expires:a+5e3},d(m,n,D).then(u=>{D=u}).catch(u=>console.warn("[Bridge] Render generation failed:",u)),v(o,200,m)}catch(p){console.error("[Bridge] Scanner error:",p),v(o,500,{error:"Failed to scan components"})}}async function gt(o){let a=[se(n,"node_modules","@popmelt.com","core","dist","canvas.mjs"),se(n,"packages","popmelt","dist","canvas.mjs")];try{let p=an(import.meta.url);a.unshift(se(rn(p),"canvas.mjs"))}catch{}for(let p of a)try{let d=await st(p,"utf-8");o.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),o.end(d);return}catch{}console.error("[Bridge] Canvas bundle not found in:",a),v(o,404,{error:"Canvas bundle not found"})}async function yt(o,a){if(!o||o.includes("/")||o.includes("\\")||o.includes("..")){v(a,400,{error:"Invalid filename"});return}try{let p=await st(se(l,o)),d=o.split(".").pop()?.toLowerCase(),m=d==="png"?"image/png":d==="jpg"||d==="jpeg"?"image/jpeg":"application/octet-stream";a.writeHead(200,{"Content-Type":m,"Cache-Control":"public, max-age=3600"}),a.end(p)}catch{v(a,404,{error:"File not found"})}}function fe(o){return`/files/${on(o)}`}async function vt(o,a){let p=await C.getThread(o);if(!p){v(a,404,{error:"Thread not found"});return}let d=p.messages.map(({screenshotPath:m,screenshotPaths:u,imagePaths:b,replyImagePaths:R,...X})=>({...X,...m?{screenshotUrl:fe(m)}:{},...u?{screenshotUrls:Object.fromEntries(Object.entries(u).map(([K,q])=>[K,fe(q)]))}:{},...b?{imageUrls:Object.fromEntries(Object.entries(b).map(([K,q])=>[K,q.map(fe)]))}:{},...R?{replyImageUrls:R.map(fe)}:{}}));v(a,200,{id:p.id,createdAt:p.createdAt,messages:d})}let Ae=9,_e=!1;for(let o=t;o<t+Ae;o++)try{await gn(A,o),O=o,_e=!0,console.log(`[\u22B9 is watching :${O}]`);break}catch(a){if(a.code==="EADDRINUSE"){let p=await mn(o);if(p&&p.projectId===e)return console.log(`[\u22B9 already watching :${o}]`),{port:o,projectId:e,close:async()=>{}};continue}throw a}if(!_e)throw new Error(`[Bridge] All ports ${t}\u2013${t+Ae-1} in use`);for(let[o,a]of Object.entries(I))!a.available||!a.path||z(o,a.path).then(p=>{if(p)console.log(`[Bridge] ${o} warmed up`);else{console.warn(`[Bridge] ${o} warm-up failed \u2014 marking unavailable`),a.available=!1,a.path=null;for(let d of J)Ce(d,{type:"capabilities_changed",data:{}})}});let wt=setInterval(()=>{ot(l).catch(()=>{})},dn);return{port:O,projectId:e,close:async()=>{clearInterval(wt),await T.destroyAsync();for(let o of J)try{o.res.end()}catch{}return J.clear(),new Promise(o=>{A.close(()=>o())})}}}async function ot(i){try{let t=await Zt(i),n=Date.now();for(let e of t){let s=se(i,e);try{let l=await en(s);n-l.mtimeMs>un&&await tn(s)}catch{}}}catch{}}var Z="\x1B[35m[popmelt]\x1B[0m";async function xn(){let i=process.argv.slice(2);if(i[0]==="wrap"){let t=i.indexOf("--");(t===-1||t===i.length-1)&&(console.error(`${Z} Usage: popmelt wrap -- <dev command>`),console.error(`${Z} Example: popmelt wrap -- next dev`),console.error(`${Z} Example: popmelt wrap -- astro dev`),process.exit(1));let n=i.slice(t+1);await bn(n);return}if(i[0]==="bridge"){await Pn();return}if(i[0]==="stop"){await In();return}console.log(`${Z} 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(" popmelt stop Stop a detached bridge"),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 Pn(){let i=await Oe({projectRoot:process.cwd()});console.log(`${Z} Bridge running on http://localhost:${i.port}`),await new Promise(t=>{let n=async()=>{console.log(`
135
- ${Z} Shutting down bridge...`),await i.close(),t()};process.on("SIGINT",n),process.on("SIGTERM",n)})}async function In(){let i=Sn(process.cwd(),".popmelt","bridge.lock"),t;try{t=JSON.parse(await vn(i,"utf8"))}catch{console.log(`${Z} No bridge running (no .popmelt/bridge.lock found)`);return}try{process.kill(t.pid,"SIGTERM"),console.log(`${Z} Sent SIGTERM to bridge (pid ${t.pid}, port ${t.port})`)}catch(n){if(n.code==="ESRCH")console.log(`${Z} Bridge process ${t.pid} already dead`);else throw n}try{await wn(i)}catch{}}async function bn(i){let t=await Oe({projectRoot:process.cwd()});console.log(`${Z} Bridge running on http://localhost:${t.port}`);let[n,...e]=i;console.log(`${Z} Starting: ${i.join(" ")}`);let s=yn(n,e,{stdio:"inherit",shell:!0,env:{...process.env,POPMELT_BRIDGE_URL:`http://localhost:${t.port}`}}),l=c=>{s.kill(c)};process.on("SIGINT",()=>l("SIGINT")),process.on("SIGTERM",()=>l("SIGTERM")),s.on("exit",async(c,r)=>{await t.close(),r?process.kill(process.pid,r):process.exit(c??0)})}xn().catch(i=>{console.error(`${Z} Fatal:`,i),process.exit(1)});
222
+ `),o.headers.origin&&lt(o.headers.origin)&&s!==o.headers.origin&&(s=o.headers.origin);let d=new URL(o.url||"/",`http://127.0.0.1:${C}`).searchParams.get("sourceId")||void 0,f={id:Pe().slice(0,8),res:a,sourceId:d};U.add(f),o.on("close",()=>{U.delete(f)})}function dt(o){let a=R.allActive;w(o,200,{ok:!0,version:rt,projectId:e,devOrigin:s,activeJob:a[0]?{id:a[0].id,status:a[0].status}:null,activeJobs:a.map(p=>({id:p.id,status:p.status,threadId:p.threadId,annotationIds:p.annotationIds,color:p.color})),queueDepth:R.depth,recentJobs:k})}async function ut(o,a){let p=await new Promise(d=>{let f="";o.on("data",u=>{f+=u.toString()}),o.on("end",()=>d(f))});try{let d=JSON.parse(p);typeof d.devOrigin=="string"&&(s=d.devOrigin||null),w(a,200,{ok:!0,devOrigin:s})}catch{w(a,400,{error:"Invalid JSON"})}}async function pt(o,a){let d=new URL(o.url||"/",`http://127.0.0.1:${C}`).searchParams.get("jobId"),u=(d?R.allActive.filter(D=>D.id===d):R.allActive).map(D=>D.threadId).filter(Boolean),y=d?R.cancelJob(d):R.cancelActive();for(let D of u)await A.appendMessage(D,{role:"assistant",timestamp:Date.now(),jobId:d||"",cancelled:!0});w(a,200,{cancelled:y})}async function ht(o){if(g.isRunning){w(o,200,{skipped:!0,reason:"Already running"});return}let a=await g.getUnmaterializedPatternDecisions();if(a.length===0){w(o,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}g.run().catch(()=>{}),w(o,200,{started:!0,decisionCount:a.length,decisionIds:a.map(p=>p.id)})}async function ft(o,a){let p=await g.loadModel();if(!p){w(a,400,{error:"No model exists yet"});return}let d,f;try{let x=[];for await(let P of o)x.push(typeof P=="string"?Buffer.from(P):P);let W=JSON.parse(Buffer.concat(x).toString());d=W.provider,f=W.model}catch{}let u=Le(p),y=Pe().slice(0,8),z=(await A.createThread(y,[])).id;I.add(z),await A.appendMessage(z,{role:"human",timestamp:Date.now(),jobId:y,feedbackSummary:"Synthesize rules \u2014 review and propose improvements"});let V={id:y,status:"queued",screenshotPath:"",feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:z,kind:"synthesize",provider:d==="claude"||d==="codex"?d:void 0,model:f||void 0};V._replyPrompt=u;let G=R.enqueue(V);w(a,200,{jobId:y,position:G,threadId:z})}async function mt(o,a){let p=[];for await(let D of o)p.push(typeof D=="string"?Buffer.from(D):D);let d;if(p.length>0)try{d=JSON.parse(Buffer.concat(p).toString("utf-8")).serverUrl}catch{}let f=[];v.claude?.available&&v.claude.mcp&&!v.claude.mcp.found&&f.push(await Ke(d)),v.codex?.available&&v.codex.mcp&&!v.codex.mcp.found&&f.push(await Ze(d));let[u,y]=await Promise.all([Ee(n),Me(n)]);v.claude&&(v.claude.mcp=u),v.codex&&(v.codex.mcp=y),w(a,200,{results:f,capabilities:{providers:v}})}async function gt(o,a){let p=[];for await(let u of o)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{w(a,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){w(a,400,{error:"Missing or invalid name"});return}let f=await g.addComponent(d.name);w(a,200,f)}async function yt(o,a){let p=[];for await(let u of o)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{w(a,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){w(a,400,{error:"Missing or invalid name"});return}let f=await g.removeComponent(d.name);w(a,200,f)}async function wt(o,a){let p=[];for await(let u of o)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{w(a,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){w(a,400,{error:"Missing or invalid path/value"});return}let f=await g.updateToken(d.path,d.value);w(a,200,f)}async function vt(o,a){let p=[];for await(let u of o)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{w(a,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){w(a,400,{error:"Missing or invalid path"});return}let f=await g.removeToken(d.path);w(a,200,f)}function St(o,a){let p=s??"http://localhost:3000";if(!s){let f=o.headers.referer||o.headers.origin;if(f)try{let u=new URL(typeof f=="string"?f:f[0]||"");(u.hostname==="localhost"||u.hostname==="127.0.0.1")&&(p=u.origin)}catch{}}let d=Be(C,p);a.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),a.end(d)}async function xt(o){let a=Date.now();if(B&&a<B.expires){w(o,200,B.data);return}try{let{scanForComponents:p}=await import("./react-scanner-ZXYS5M3Y.mjs"),{generateRenderFiles:d}=await import("./render-generator-EANIDD2E.mjs"),f=await p(n);B={data:f,expires:a+5e3},d(f,n,_).then(u=>{_=u}).catch(u=>console.warn("[Bridge] Render generation failed:",u)),w(o,200,f)}catch(p){console.error("[Bridge] Scanner error:",p),w(o,500,{error:"Failed to scan components"})}}async function bt(o){let a=[se(n,"node_modules","@popmelt.com","core","dist","canvas.mjs"),se(n,"packages","popmelt","dist","canvas.mjs")];try{let p=hn(import.meta.url);a.unshift(se(pn(p),"canvas.mjs"))}catch{}for(let p of a)try{let d=await at(p,"utf-8");o.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),o.end(d);return}catch{}console.error("[Bridge] Canvas bundle not found in:",a),w(o,404,{error:"Canvas bundle not found"})}async function Pt(o,a){if(!o||o.includes("/")||o.includes("\\")||o.includes("..")){w(a,400,{error:"Invalid filename"});return}try{let p=await at(se(i,o)),d=o.split(".").pop()?.toLowerCase(),f=d==="png"?"image/png":d==="webp"?"image/webp":d==="jpg"||d==="jpeg"?"image/jpeg":"application/octet-stream";a.writeHead(200,{"Content-Type":f,"Cache-Control":"public, max-age=3600"}),a.end(p)}catch{w(a,404,{error:"File not found"})}}function ge(o){return`/files/${un(o)}`}async function kt(o,a){let p=await A.getThread(o);if(!p){w(a,404,{error:"Thread not found"});return}let d=p.messages.map(({screenshotPath:f,screenshotPaths:u,imagePaths:y,replyImagePaths:D,...z})=>({...z,...f?{screenshotUrl:ge(f)}:{},...u?{screenshotUrls:Object.fromEntries(Object.entries(u).map(([V,G])=>[V,ge(G)]))}:{},...y?{imageUrls:Object.fromEntries(Object.entries(y).map(([V,G])=>[V,G.map(ge)]))}:{},...D?{replyImageUrls:D.map(ge)}:{}}));w(a,200,{id:p.id,createdAt:p.createdAt,messages:d})}let Ne=9,_e=!1;for(let o=t;o<t+Ne;o++)try{await bn(Z,o),C=o,_e=!0,console.log(`[\u22B9 is watching :${C}]`);break}catch(a){if(a.code==="EADDRINUSE"){let p=await xn(o);if(p&&p.projectId===e)return console.log(`[\u22B9 already watching :${o}]`),{port:o,projectId:e,close:async()=>{}};continue}throw a}if(!_e)throw new Error(`[Bridge] All ports ${t}\u2013${t+Ne-1} in use`);for(let[o,a]of Object.entries(v))!a.available||!a.path||H(o,a.path).then(p=>{if(p)console.log(`[Bridge] ${o} warmed up`);else{console.warn(`[Bridge] ${o} warm-up failed \u2014 marking unavailable`),a.available=!1,a.path=null;for(let d of U)Ae(d,{type:"capabilities_changed",data:{}})}});let It=setInterval(()=>{ct(i).catch(()=>{})},gn);return{port:C,projectId:e,close:async()=>{clearInterval(It),await R.destroyAsync();for(let o of U)try{o.res.end()}catch{}return U.clear(),new Promise(o=>{Z.close(()=>o())})}}}async function ct(r){try{let t=await rn(r),n=Date.now();for(let e of t){let s=se(r,e);try{let i=await an(s);n-i.mtimeMs>yn&&await cn(s)}catch{}}}catch{}}var te="\x1B[35m[popmelt]\x1B[0m";async function Rn(){let r=process.argv.slice(2);if(r[0]==="wrap"){let t=r.indexOf("--");(t===-1||t===r.length-1)&&(console.error(`${te} Usage: popmelt wrap -- <dev command>`),console.error(`${te} Example: popmelt wrap -- next dev`),console.error(`${te} Example: popmelt wrap -- astro dev`),process.exit(1));let n=r.slice(t+1);await Mn(n);return}if(r[0]==="bridge"){await $n();return}if(r[0]==="stop"){await En();return}console.log(`${te} 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(" popmelt stop Stop a detached bridge"),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 $n(){let r=await De({projectRoot:process.cwd()});console.log(`${te} Bridge running on http://localhost:${r.port}`),await new Promise(t=>{let n=async()=>{console.log(`
223
+ ${te} Shutting down bridge...`),await r.close(),t()};process.on("SIGINT",n),process.on("SIGTERM",n)})}async function En(){let r=Tn(process.cwd(),".popmelt","bridge.lock"),t;try{t=JSON.parse(await kn(r,"utf8"))}catch{console.log(`${te} No bridge running (no .popmelt/bridge.lock found)`);return}try{process.kill(t.pid,"SIGTERM"),console.log(`${te} Sent SIGTERM to bridge (pid ${t.pid}, port ${t.port})`)}catch(n){if(n.code==="ESRCH")console.log(`${te} Bridge process ${t.pid} already dead`);else throw n}try{await In(r)}catch{}}async function Mn(r){let t=await De({projectRoot:process.cwd()});console.log(`${te} Bridge running on http://localhost:${t.port}`);let[n,...e]=r;console.log(`${te} Starting: ${r.join(" ")}`);let s=Pn(n,e,{stdio:"inherit",shell:!0,env:{...process.env,POPMELT_BRIDGE_URL:`http://localhost:${t.port}`}}),i=l=>{s.kill(l)};process.on("SIGINT",()=>i("SIGINT")),process.on("SIGTERM",()=>i("SIGTERM")),s.on("exit",async(l,c)=>{await t.close(),c?process.kill(process.pid,c):process.exit(l??0)})}Rn().catch(r=>{console.error(`${te} Fatal:`,r),process.exit(1)});