@popmelt.com/core 0.6.4 → 0.6.6

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