@popmelt.com/core 0.6.6 → 0.6.8

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