@popmelt.com/core 0.5.10 → 0.5.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import{a as te,b as ce,c as ot,d as de}from"./chunk-3HWT3PC2.mjs";import{execFileSync as yn}from"child_process";import{createHash as wn,randomUUID as fe}from"crypto";import{mkdir as vn,readFile as Sn,readdir as In,stat as xn,unlink as Pn,writeFile as ve}from"fs/promises";import{createServer as kn}from"http";import{tmpdir as bn}from"os";import{dirname as Tn,join as oe}from"path";import{fileURLToPath as Rn}from"url";function rt(r,n){return`<!DOCTYPE html>
1
+ import{a as q,b as ne,c as tt,d as ce}from"./chunk-3HWT3PC2.mjs";import{execFileSync as en,spawn as tn}from"child_process";import{createHash as nn,randomUUID as Le}from"crypto";import{mkdir as sn,readFile as on,readdir as rn,stat as an,unlink as cn,writeFile as je}from"fs/promises";import{createServer as ln}from"http";import{tmpdir as dn}from"os";import{dirname as un,join as re}from"path";import{fileURLToPath as pn}from"url";function nt(r,t){return`<!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
4
  <meta charset="utf-8">
@@ -20,24 +20,24 @@ import{a as te,b as ce,c as ot,d as de}from"./chunk-3HWT3PC2.mjs";import{execFil
20
20
  <script type="module">
21
21
  import { mountCanvas } from 'http://localhost:${r}/canvas/app.mjs';
22
22
  mountCanvas(document.getElementById('root'), {
23
- devOrigin: '${n}',
23
+ devOrigin: '${t}',
24
24
  bridgeOrigin: 'http://localhost:${r}',
25
25
  });
26
26
  </script>
27
27
  </body>
28
- </html>`}import{spawn as Wt}from"child_process";import{createInterface as Yt}from"readline";function Ce(r,n){let{prompt:t,projectRoot:e,maxTurns:o=40,maxBudgetUsd:a=1,allowedTools:i=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:l="claude",resumeSessionId:h,model:f,timeoutMs:J=3e5,onEvent:A}=n,O=[];h?O.push("--resume",h,"-p",t):O.push("-p",t),O.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(a)),f&&O.push("--model",f);for(let W of i)O.push("--allowedTools",W);let g=Wt(l,O,{cwd:e,stdio:["ignore","pipe","pipe"],env:ce(te({},process.env),{ANTHROPIC_API_KEY:void 0})}),H=new Promise(W=>{var G;let v,L=[],C=[],V=!1,E="",X=!1,U=setTimeout(()=>{X=!0,g.kill("SIGTERM"),setTimeout(()=>{try{g.kill("SIGKILL")}catch(k){}},5e3)},J),ne=Yt({input:g.stdout}),P=new Set;ne.on("line",k=>{var K,me,Pe,ke,be,Te,Re,$e,Me;if(k.trim())try{let q=JSON.parse(k);q.session_id&&!v&&(v=q.session_id);let Ne=(me=q.type)!=null?me:(K=q.event)!=null&&K.type?`event.${q.event.type}`:"unknown";if(P.add(Ne),q.type==="result"&&q.result&&L.length===0){let B=typeof q.result=="string"?q.result:"";B&&(L.push(B),A==null||A({type:"delta",jobId:r,text:B},r))}if(q.type==="assistant"&&Array.isArray((Pe=q.message)==null?void 0:Pe.content))for(let B of q.message.content){if(B.type==="text"&&B.text&&(L.push(B.text),A==null||A({type:"delta",jobId:r,text:B.text},r)),B.type==="tool_use"&&B.name){let Oe=((ke=B.input)==null?void 0:ke.file_path)||((be=B.input)==null?void 0:be.path)||void 0;A==null||A(te({type:"tool_use",jobId:r,tool:B.name},Oe?{file:Oe}:{}),r),B.name==="Edit"&&((Te=B.input)!=null&&Te.file_path)?C.push({tool:"Edit",file_path:B.input.file_path,old_string:B.input.old_string,new_string:B.input.new_string,replace_all:B.input.replace_all}):B.name==="Write"&&((Re=B.input)!=null&&Re.file_path)&&C.push({tool:"Write",file_path:B.input.file_path,content:B.input.content})}B.type==="thinking"&&B.thinking&&(A==null||A({type:"thinking",jobId:r,text:B.thinking},r))}q.type==="user"&&((Me=($e=q.tool_use_result)==null?void 0:$e.file)!=null&&Me.filePath)&&(A==null||A({type:"tool_use",jobId:r,tool:"Read",file:q.tool_use_result.file.filePath},r))}catch(q){}});let x=[];(G=g.stderr)==null||G.on("data",k=>{x.push(k.toString())}),g.on("close",k=>{clearTimeout(U),ne.close(),L.length===0&&P.size>0&&console.warn(`[Claude:${r}] No text captured. Event types seen: ${[...P].join(", ")}`),X?(V=!0,E=`Timed out after ${Math.round(J/6e4)} minutes`):k!==0&&k!==null&&(V=!0,E=x.join("")||`Claude process exited with code ${k}`),W({sessionId:v,text:L.join(""),success:!V,error:V?E:void 0,fileEdits:C.length>0?C:void 0})}),g.on("error",k=>{clearTimeout(U),V=!0,E=k.message,W({sessionId:v,text:L.join(""),success:!1,error:E,fileEdits:C.length>0?C:void 0})})});return{process:g,result:H}}import{spawn as Qt}from"child_process";import{createInterface as qt}from"readline";function it(r,n){let{prompt:t,projectRoot:e,screenshotPath:o,resumeSessionId:a,model:i,onEvent:l}=n,h=[];a?(h.push("exec","resume",a),i&&h.push("-m",i),h.push("--json","--full-auto",t),o&&h.push("--image",o)):(h.push("exec","--json","--full-auto"),i&&h.push("-m",i),h.push(t),o&&h.push("--image",o));let f=Qt("codex",h,{cwd:e,stdio:["ignore","pipe","pipe"],env:te({},process.env)}),J=new Promise(A=>{var V;let O,g=[],H=!1,W="",v=qt({input:f.stdout}),L=new Set;v.on("line",E=>{var X,U,ne,P;if(E.trim())try{let x=JSON.parse(E),G=(X=x.type)!=null?X:"unknown";if(L.add(G),G==="thread.started"&&x.thread_id&&!O&&(O=x.thread_id),(G==="item.agentMessage.delta"||G==="item/agentMessage/delta")&&((U=x.delta)!=null&&U.text)&&(g.push(x.delta.text),l==null||l({type:"delta",jobId:r,text:x.delta.text},r)),(G==="item.reasoning.delta"||G==="item/reasoning/delta")&&((ne=x.delta)!=null&&ne.text)&&(l==null||l({type:"thinking",jobId:r,text:x.delta.text},r)),(G==="item.started"||G==="item/started")&&x.item){let k=x.item.type;if(k==="command_execution")l==null||l({type:"tool_use",jobId:r,tool:"Bash"},r);else if(k==="file_change"){let K=x.item.filename||x.item.path;l==null||l(te({type:"tool_use",jobId:r,tool:"Edit"},K?{file:K}:{}),r)}else if(k==="file_read"){let K=x.item.filename||x.item.path;l==null||l(te({type:"tool_use",jobId:r,tool:"Read"},K?{file:K}:{}),r)}else if(k==="web_search")l==null||l({type:"tool_use",jobId:r,tool:"WebSearch"},r);else if(k==="mcp_tool_call"){let K=x.item.tool_name||x.item.name||"MCP";l==null||l({type:"tool_use",jobId:r,tool:K},r)}}if((G==="item.completed"||G==="item/completed")&&x.item){if(x.item.type==="agent_message"){let k=x.item.text;typeof k=="string"&&k&&(g.push(k),l==null||l({type:"delta",jobId:r,text:k},r))}else if(x.item.type==="reasoning"){let k=x.item.text;typeof k=="string"&&k&&(l==null||l({type:"thinking",jobId:r,text:k},r))}}G==="turn.failed"&&(H=!0,W=((P=x.error)==null?void 0:P.message)||x.message||"Turn failed")}catch(x){}});let C=[];(V=f.stderr)==null||V.on("data",E=>{C.push(E.toString())}),f.on("close",E=>{v.close(),g.length===0&&L.size>0&&console.warn(`[Codex:${r}] No text captured. Event types seen: ${[...L].join(", ")}`),E!==0&&E!==null&&(H=!0,W=C.join("")||`Codex process exited with code ${E}`),A({sessionId:O,text:g.join(""),success:!H,error:H?W:void 0})}),f.on("error",E=>{H=!0,W=E.message,A({sessionId:O,text:g.join(""),success:!1,error:W})})});return{process:f,result:J}}import{execFile as Kt}from"child_process";import{copyFile as at,mkdir as ct,readdir as Xt,readFile as Vt,writeFile as Zt}from"fs/promises";import{join as he}from"path";var Ee=class{constructor(n){this.projectRoot=n;let t=he(n,".popmelt");this.decisionsDir=he(t,"decisions"),this.screenshotsDir=he(t,"screenshots")}async persist(n,t,e){try{await ct(this.decisionsDir,{recursive:!0}),await ct(this.screenshotsDir,{recursive:!0});try{await at(t,he(this.screenshotsDir,`s-${n.id}.png`))}catch(o){}for(let o=0;o<e.length;o++)try{let a=n.pastedImagePaths[o];a&&await at(e[o],he(this.screenshotsDir,a.replace("screenshots/","")))}catch(a){}await Zt(he(this.decisionsDir,`d-${n.id}.json`),JSON.stringify(n,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await Xt(this.decisionsDir)).filter(t=>t.startsWith("d-")&&t.endsWith(".json")).map(t=>t.slice(2,-5))}catch(n){return[]}}async loadDecision(n){try{let t=await Vt(he(this.decisionsDir,`d-${n}.json`),"utf-8");return JSON.parse(t)}catch(t){return null}}async loadDecisions(n){return(await Promise.all(n.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(n){return new Promise(t=>{Kt("git",["diff","HEAD"],{cwd:n,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){t(null);return}t(o||null)})})}};import{readFile as lt,writeFile as ge}from"fs/promises";import{join as Ue}from"path";var ie="[Materializer]",en={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null},Ae=class{constructor(n,t,e={}){this.projectRoot=n;this.decisionStore=t;this.options=e;this.cachedIndex=null;this.running=!1;let o=Ue(n,".popmelt");this.indexPath=Ue(o,"materialized.json"),this.modelPath=Ue(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let n=await lt(this.modelPath,"utf-8");return JSON.parse(n)}catch(n){return null}}async addComponent(n){let t=await this.loadModel();t||(t={tokens:{},components:{},rules:[]}),(!t.components||typeof t.components!="object")&&(t.components={});let e=t.components;return e[n]?{added:!1,alreadyExists:!0}:(e[n]={description:""},await ge(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ie} Added component "${n}" to model`),{added:!0,alreadyExists:!1})}async updateToken(n,t){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=n.split("."),a=e;for(let h=0;h<o.length-1;h++){let f=o[h];(!a[f]||typeof a[f]!="object")&&(a[f]={}),a=a[f]}let i=o[o.length-1],l;try{l=JSON.parse(t)}catch(h){l=null}if(l&&typeof l=="object"&&l!==null&&"value"in l)a[i]=l;else{let h=a[i];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=t:a[i]=t}return await ge(this.modelPath,JSON.stringify(e,null,2)),console.log(`${ie} Updated token "${n}" \u2192 "${t.slice(0,80)}"`),{updated:!0}}async removeToken(n){let t=await this.loadModel();if(!t)return{removed:!1};let e=n.split("."),o=t;for(let i=0;i<e.length-1;i++){let l=e[i];if(!o[l]||typeof o[l]!="object")return{removed:!1};o=o[l]}let a=e[e.length-1];return a in o?(delete o[a],await ge(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ie} Removed token "${n}" from model`),{removed:!0}):{removed:!1}}async removeComponent(n){let t=await this.loadModel();if(!t)return{removed:!1};let e=t.components;return!e||!e[n]?{removed:!1}:(delete e[n],await ge(this.modelPath,JSON.stringify(t,null,2)),console.log(`${ie} Removed component "${n}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let n=await this.loadIndex(),t=new Set(n.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(i=>!t.has(i));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(i=>i.resolutions.some(l=>{var f;let h=(f=l.finalScope)!=null?f:l.inferredScope;return(h==null?void 0:h.breadth)==="pattern"}))}async run(){var n,t,e,o,a,i,l;if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let h=await this.getUnmaterializedPatternDecisions();if(h.length===0)return{processedIds:[],success:!0};let f=h.map(v=>v.id);console.log(`${ie} Processing ${f.length} pattern-scoped decision(s): ${f.join(", ")}`),(t=(n=this.options).onEvent)==null||t.call(n,{type:"materialize_started",decisionIds:f});let J=await this.loadModel(),A=nn(h,J),O=!0,g;try{let{result:v}=Ce(`mat-${Date.now()}`,{prompt:A,projectRoot:this.projectRoot,maxTurns:(e=this.options.maxTurns)!=null?e:5,maxBudgetUsd:(o=this.options.maxBudgetUsd)!=null?o:.5,allowedTools:["Read"],claudePath:(a=this.options.claudePath)!=null?a:"claude"}),L=await v;if(!L.success)O=!1,g=L.error,console.error(`${ie} Claude spawn error:`,g);else{let C=tn(L.text);C?(await ge(this.modelPath,JSON.stringify(C,null,2)),console.log(`${ie} Successfully materialized ${f.length} decision(s) \u2192 ${this.modelPath}`)):(O=!1,g="No <model> block found in response",console.error(`${ie} ${g}`))}}catch(v){O=!1,g=v instanceof Error?v.message:String(v),console.error(`${ie} Error:`,g)}let H=await this.loadIndex(),W=new Set(H.materializedIds);for(let v of f)W.add(v);return H.materializedIds=[...W],H.lastRunAt=Date.now(),H.lastRunDecisionIds=f,H.lastRunError=g!=null?g:null,await this.persistIndex(H),(l=(i=this.options).onEvent)==null||l.call(i,{type:"materialize_done",decisionIds:f,success:O,error:g}),{processedIds:f,success:O,error:g}}finally{this.running=!1}}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let n=await lt(this.indexPath,"utf-8"),t=JSON.parse(n);return this.cachedIndex=t,t}catch(n){return this.cachedIndex=ce(te({},en),{materializedIds:[],lastRunDecisionIds:[]}),this.cachedIndex}}async persistIndex(n){this.cachedIndex=n;try{await ge(this.indexPath,JSON.stringify(n,null,2))}catch(t){console.error(`${ie} Failed to write index:`,t)}}};function tn(r){let n=r.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return typeof t!="object"||t===null||Array.isArray(t)?null:t}catch(t){return null}}function nn(r,n){let t=r.map(o=>{let i=o.resolutions.filter(f=>{var A;let J=(A=f.finalScope)!=null?A:f.inferredScope;return(J==null?void 0:J.breadth)==="pattern"}).map(f=>{var g,H,W,v;let J=(g=f.finalScope)!=null?g:f.inferredScope,A=(H=J==null?void 0:J.target)!=null?H:"unknown",O=(v=(W=f.filesModified)==null?void 0:W.join(", "))!=null?v:"none";return`- **${f.summary}** [scope: pattern/${A}]
29
- Files modified: ${O}`}).join(`
30
- `),l=o.annotations.map(f=>f.instruction).filter(Boolean).join(`
31
- `),h=o.gitDiff?`
28
+ </html>`}import{spawn as Mt}from"child_process";import{createInterface as Ct}from"readline";function be(r,t){let{prompt:e,projectRoot:n,maxTurns:s=40,maxBudgetUsd:a=1,allowedTools:l=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:i="claude",resumeSessionId:u,model:h,timeoutMs:A=3e5,onEvent:T}=t,k=[];u?k.push("--resume",u,"-p",e):k.push("-p",e),k.push("--output-format","stream-json","--verbose","--max-turns",String(s),"--max-budget-usd",String(a)),h&&k.push("--model",h);for(let j of l)k.push("--allowedTools",j);let f=Mt(i,k,{cwd:n,stdio:["ignore","pipe","pipe"],env:ne(q({},process.env),{ANTHROPIC_API_KEY:void 0})}),F=new Promise(j=>{var L;let J,v=[],B=[],E=!1,U="",z=!1,O=setTimeout(()=>{z=!0,f.kill("SIGTERM"),setTimeout(()=>{try{f.kill("SIGKILL")}catch(x){}},5e3)},A),Z=Ct({input:f.stdout}),S=new Set;Z.on("line",x=>{var Q,de,ge,ye,we,ve,Se,xe,Pe;if(x.trim())try{let W=JSON.parse(x);W.session_id&&!J&&(J=W.session_id);let Ee=(de=W.type)!=null?de:(Q=W.event)!=null&&Q.type?`event.${W.event.type}`:"unknown";if(S.add(Ee),W.type==="result"&&W.result&&v.length===0){let $=typeof W.result=="string"?W.result:"";$&&(v.push($),T==null||T({type:"delta",jobId:r,text:$},r))}if(W.type==="assistant"&&Array.isArray((ge=W.message)==null?void 0:ge.content))for(let $ of W.message.content){if($.type==="text"&&$.text&&(v.push($.text),T==null||T({type:"delta",jobId:r,text:$.text},r)),$.type==="tool_use"&&$.name){let Ie=((ye=$.input)==null?void 0:ye.file_path)||((we=$.input)==null?void 0:we.path)||void 0;T==null||T(q({type:"tool_use",jobId:r,tool:$.name},Ie?{file:Ie}:{}),r),$.name==="Edit"&&((ve=$.input)!=null&&ve.file_path)?B.push({tool:"Edit",file_path:$.input.file_path,old_string:$.input.old_string,new_string:$.input.new_string,replace_all:$.input.replace_all}):$.name==="Write"&&((Se=$.input)!=null&&Se.file_path)&&B.push({tool:"Write",file_path:$.input.file_path,content:$.input.content})}$.type==="thinking"&&$.thinking&&(T==null||T({type:"thinking",jobId:r,text:$.thinking},r))}W.type==="user"&&((Pe=(xe=W.tool_use_result)==null?void 0:xe.file)!=null&&Pe.filePath)&&(T==null||T({type:"tool_use",jobId:r,tool:"Read",file:W.tool_use_result.file.filePath},r))}catch(W){}});let g=[];(L=f.stderr)==null||L.on("data",x=>{g.push(x.toString())}),f.on("close",x=>{clearTimeout(O),Z.close(),v.length===0&&S.size>0&&console.warn(`[Claude:${r}] No text captured. Event types seen: ${[...S].join(", ")}`),z?(E=!0,U=`Timed out after ${Math.round(A/6e4)} minutes`):x!==0&&x!==null&&(E=!0,U=g.join("")||`Claude process exited with code ${x}`),j({sessionId:J,text:v.join(""),success:!E,error:E?U:void 0,fileEdits:B.length>0?B:void 0})}),f.on("error",x=>{clearTimeout(O),E=!0,U=x.message,j({sessionId:J,text:v.join(""),success:!1,error:U,fileEdits:B.length>0?B:void 0})})});return{process:f,result:F}}import{spawn as Et}from"child_process";import{createInterface as Ot}from"readline";function st(r,t){let{prompt:e,projectRoot:n,screenshotPath:s,resumeSessionId:a,model:l,onEvent:i}=t,u=[];a?(u.push("exec","resume",a),l&&u.push("-m",l),u.push("--json","--full-auto",e),s&&u.push("--image",s)):(u.push("exec","--json","--full-auto"),l&&u.push("-m",l),u.push(e),s&&u.push("--image",s));let h=Et("codex",u,{cwd:n,stdio:["ignore","pipe","pipe"],env:q({},process.env)}),A=new Promise(T=>{var E;let k,f=[],F=!1,j="",J=Ot({input:h.stdout}),v=new Set;J.on("line",U=>{var z,O,Z,S;if(U.trim())try{let g=JSON.parse(U),L=(z=g.type)!=null?z:"unknown";if(v.add(L),L==="thread.started"&&g.thread_id&&!k&&(k=g.thread_id),(L==="item.agentMessage.delta"||L==="item/agentMessage/delta")&&((O=g.delta)!=null&&O.text)&&(f.push(g.delta.text),i==null||i({type:"delta",jobId:r,text:g.delta.text},r)),(L==="item.reasoning.delta"||L==="item/reasoning/delta")&&((Z=g.delta)!=null&&Z.text)&&(i==null||i({type:"thinking",jobId:r,text:g.delta.text},r)),(L==="item.started"||L==="item/started")&&g.item){let x=g.item.type;if(x==="command_execution")i==null||i({type:"tool_use",jobId:r,tool:"Bash"},r);else if(x==="file_change"){let Q=g.item.filename||g.item.path;i==null||i(q({type:"tool_use",jobId:r,tool:"Edit"},Q?{file:Q}:{}),r)}else if(x==="file_read"){let Q=g.item.filename||g.item.path;i==null||i(q({type:"tool_use",jobId:r,tool:"Read"},Q?{file:Q}:{}),r)}else if(x==="web_search")i==null||i({type:"tool_use",jobId:r,tool:"WebSearch"},r);else if(x==="mcp_tool_call"){let Q=g.item.tool_name||g.item.name||"MCP";i==null||i({type:"tool_use",jobId:r,tool:Q},r)}}if((L==="item.completed"||L==="item/completed")&&g.item){if(g.item.type==="agent_message"){let x=g.item.text;typeof x=="string"&&x&&(f.push(x),i==null||i({type:"delta",jobId:r,text:x},r))}else if(g.item.type==="reasoning"){let x=g.item.text;typeof x=="string"&&x&&(i==null||i({type:"thinking",jobId:r,text:x},r))}}L==="turn.failed"&&(F=!0,j=((S=g.error)==null?void 0:S.message)||g.message||"Turn failed")}catch(g){}});let B=[];(E=h.stderr)==null||E.on("data",U=>{B.push(U.toString())}),h.on("close",U=>{J.close(),f.length===0&&v.size>0&&console.warn(`[Codex:${r}] No text captured. Event types seen: ${[...v].join(", ")}`),U!==0&&U!==null&&(F=!0,j=B.join("")||`Codex process exited with code ${U}`),T({sessionId:k,text:f.join(""),success:!F,error:F?j:void 0})}),h.on("error",U=>{F=!0,j=U.message,T({sessionId:k,text:f.join(""),success:!1,error:j})})});return{process:h,result:A}}import{execFile as Dt}from"child_process";import{copyFile as ot,mkdir as rt,readdir as At,readFile as _t,writeFile as Nt}from"fs/promises";import{join as le}from"path";var ke=class{constructor(t){this.projectRoot=t;let e=le(t,".popmelt");this.decisionsDir=le(e,"decisions"),this.screenshotsDir=le(e,"screenshots")}async persist(t,e,n){try{await rt(this.decisionsDir,{recursive:!0}),await rt(this.screenshotsDir,{recursive:!0});try{await ot(e,le(this.screenshotsDir,`s-${t.id}.png`))}catch(s){}for(let s=0;s<n.length;s++)try{let a=t.pastedImagePaths[s];a&&await ot(n[s],le(this.screenshotsDir,a.replace("screenshots/","")))}catch(a){}await Nt(le(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(e=>e.startsWith("d-")&&e.endsWith(".json")).map(e=>e.slice(2,-5))}catch(t){return[]}}async loadDecision(t){try{let e=await _t(le(this.decisionsDir,`d-${t}.json`),"utf-8");return JSON.parse(e)}catch(e){return null}}async loadDecisions(t){return(await Promise.all(t.map(n=>this.loadDecision(n)))).filter(n=>n!==null)}captureGitDiff(t){return new Promise(e=>{Dt("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(n,s)=>{if(n){e(null);return}e(s||null)})})}};import{readFile as it,writeFile as he}from"fs/promises";import{join as De}from"path";var se="[Materializer]",Jt={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null},Te=class{constructor(t,e,n={}){this.projectRoot=t;this.decisionStore=e;this.options=n;this.cachedIndex=null;this.running=!1;let s=De(t,".popmelt");this.indexPath=De(s,"materialized.json"),this.modelPath=De(s,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await it(this.modelPath,"utf-8");return JSON.parse(t)}catch(t){return null}}async addComponent(t){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]}),(!e.components||typeof e.components!="object")&&(e.components={});let n=e.components;return n[t]?{added:!1,alreadyExists:!0}:(n[t]={description:""},await he(this.modelPath,JSON.stringify(e,null,2)),console.log(`${se} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,e){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]});let s=t.split("."),a=n;for(let u=0;u<s.length-1;u++){let h=s[u];(!a[h]||typeof a[h]!="object")&&(a[h]={}),a=a[h]}let l=s[s.length-1],i;try{i=JSON.parse(e)}catch(u){i=null}if(i&&typeof i=="object"&&i!==null&&"value"in i)a[l]=i;else{let u=a[l];u&&typeof u=="object"&&u!==null&&"value"in u?u.value=e:a[l]=e}return await he(this.modelPath,JSON.stringify(n,null,2)),console.log(`${se} Updated token "${t}" \u2192 "${e.slice(0,80)}"`),{updated:!0}}async removeToken(t){let e=await this.loadModel();if(!e)return{removed:!1};let n=t.split("."),s=e;for(let l=0;l<n.length-1;l++){let i=n[l];if(!s[i]||typeof s[i]!="object")return{removed:!1};s=s[i]}let a=n[n.length-1];return a in s?(delete s[a],await he(this.modelPath,JSON.stringify(e,null,2)),console.log(`${se} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let e=await this.loadModel();if(!e)return{removed:!1};let n=e.components;return!n||!n[t]?{removed:!1}:(delete n[t],await he(this.modelPath,JSON.stringify(e,null,2)),console.log(`${se} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),e=new Set(t.materializedIds),s=(await this.decisionStore.listDecisionIds()).filter(l=>!e.has(l));return s.length===0?[]:(await this.decisionStore.loadDecisions(s)).filter(l=>l.resolutions.some(i=>{var h;let u=(h=i.finalScope)!=null?h:i.inferredScope;return(u==null?void 0:u.breadth)==="pattern"}))}async run(){var t,e,n,s,a,l,i;if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let u=await this.getUnmaterializedPatternDecisions();if(u.length===0)return{processedIds:[],success:!0};let h=u.map(J=>J.id);console.log(`${se} Processing ${h.length} pattern-scoped decision(s): ${h.join(", ")}`),(e=(t=this.options).onEvent)==null||e.call(t,{type:"materialize_started",decisionIds:h});let A=await this.loadModel(),T=Ut(u,A),k=!0,f;try{let{result:J}=be(`mat-${Date.now()}`,{prompt:T,projectRoot:this.projectRoot,maxTurns:(n=this.options.maxTurns)!=null?n:5,maxBudgetUsd:(s=this.options.maxBudgetUsd)!=null?s:.5,allowedTools:["Read"],claudePath:(a=this.options.claudePath)!=null?a:"claude"}),v=await J;if(!v.success)k=!1,f=v.error,console.error(`${se} Claude spawn error:`,f);else{let B=Bt(v.text);B?(await he(this.modelPath,JSON.stringify(B,null,2)),console.log(`${se} Successfully materialized ${h.length} decision(s) \u2192 ${this.modelPath}`)):(k=!1,f="No <model> block found in response",console.error(`${se} ${f}`))}}catch(J){k=!1,f=J instanceof Error?J.message:String(J),console.error(`${se} Error:`,f)}let F=await this.loadIndex(),j=new Set(F.materializedIds);for(let J of h)j.add(J);return F.materializedIds=[...j],F.lastRunAt=Date.now(),F.lastRunDecisionIds=h,F.lastRunError=f!=null?f:null,await this.persistIndex(F),(i=(l=this.options).onEvent)==null||i.call(l,{type:"materialize_done",decisionIds:h,success:k,error:f}),{processedIds:h,success:k,error:f}}finally{this.running=!1}}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await it(this.indexPath,"utf-8"),e=JSON.parse(t);return this.cachedIndex=e,e}catch(t){return this.cachedIndex=ne(q({},Jt),{materializedIds:[],lastRunDecisionIds:[]}),this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await he(this.indexPath,JSON.stringify(t,null,2))}catch(e){console.error(`${se} Failed to write index:`,e)}}};function Bt(r){let t=r.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!(t!=null&&t[1]))return null;try{let e=JSON.parse(t[1]);return typeof e!="object"||e===null||Array.isArray(e)?null:e}catch(e){return null}}function Ut(r,t){let e=r.map(s=>{let l=s.resolutions.filter(h=>{var T;let A=(T=h.finalScope)!=null?T:h.inferredScope;return(A==null?void 0:A.breadth)==="pattern"}).map(h=>{var f,F,j,J;let A=(f=h.finalScope)!=null?f:h.inferredScope,T=(F=A==null?void 0:A.target)!=null?F:"unknown",k=(J=(j=h.filesModified)==null?void 0:j.join(", "))!=null?J:"none";return`- **${h.summary}** [scope: pattern/${T}]
29
+ Files modified: ${k}`}).join(`
30
+ `),i=s.annotations.map(h=>h.instruction).filter(Boolean).join(`
31
+ `),u=s.gitDiff?`
32
32
  \`\`\`diff
33
- ${o.gitDiff.slice(0,2e3)}
34
- \`\`\``:"";return`### Decision ${o.id} (${new Date(o.createdAt).toISOString()})
35
- Page: ${o.url}
36
- ${i}
37
- ${h}
38
- ${l?`
33
+ ${s.gitDiff.slice(0,2e3)}
34
+ \`\`\``:"";return`### Decision ${s.id} (${new Date(s.createdAt).toISOString()})
35
+ Page: ${s.url}
36
+ ${l}
37
+ ${u}
38
+ ${i?`
39
39
  Original instructions:
40
- ${l}`:""}`}).join(`
40
+ ${i}`:""}`}).join(`
41
41
 
42
42
  `);return`You are extracting a local design model from accumulated design decisions.
43
43
 
@@ -46,15 +46,15 @@ ${l}`:""}`}).join(`
46
46
  2. Determine what design tokens, component patterns, and rules to add or update.
47
47
  3. Output the complete updated model as a JSON object inside <model> tags.
48
48
 
49
- ${n?`## Current Model
49
+ ${t?`## Current Model
50
50
  \`\`\`json
51
- ${JSON.stringify(n,null,2)}
51
+ ${JSON.stringify(t,null,2)}
52
52
  \`\`\`
53
53
 
54
54
  Merge the new decisions into the existing model. Preserve existing entries that are not contradicted by new decisions.`:"No model exists yet. Create one from scratch based on the decisions below."}
55
55
 
56
56
  ## Design Decisions to Materialize
57
- ${t}
57
+ ${e}
58
58
 
59
59
  ## Output Format
60
60
  Output the full model inside <model> tags. The model is a JSON object with these sections:
@@ -93,39 +93,35 @@ Example:
93
93
  { "value": "8px", "property": "gap", "bindings": ["gap-2"] }
94
94
  - property: "gap", "padding", or "margin" \u2014 which CSS property this token controls
95
95
  - bindings: Tailwind class names (without responsive prefixes) that use this token
96
- Include property and bindings when the decision context has class-level evidence.`}import{readFile as pt}from"fs/promises";import{homedir as ht}from"os";import{join as ye}from"path";var Le=/popmelt/i;function ze(){return{found:!1,name:null,scope:null,disabled:!1}}function Ie(r,n,t=!1){return{found:!0,name:r,scope:n,disabled:t}}function De(r){for(let n of Object.keys(r))if(Le.test(n))return n;return null}async function Fe(r){try{let n=await pt(r,"utf-8");return JSON.parse(n)}catch(n){return null}}async function He(r){let n=ht(),t=await Fe(ye(n,".claude.json"));if(t&&typeof t=="object"){let o=t;if(o.mcpServers&&typeof o.mcpServers=="object"){let a=De(o.mcpServers);if(a)return Ie(a,"user")}if(o.projects&&typeof o.projects=="object"){let i=o.projects[r];if(i&&typeof i=="object"){let l=i;if(l.mcpServers&&typeof l.mcpServers=="object"){let h=De(l.mcpServers);if(h){let f=Array.isArray(l.disabledMcpjsonServers)&&l.disabledMcpjsonServers.some(J=>Le.test(J));return Ie(h,"project",f)}}}}}let e=await Fe(ye(r,".mcp.json"));if(e&&typeof e=="object"){let o=e;if(o.mcpServers&&typeof o.mcpServers=="object"){let i=De(o.mcpServers);if(i){let l=await dt(r,i);return Ie(i,"mcp.json",l)}}let a=De(o);if(a&&a!=="mcpServers"){let i=await dt(r,a);return Ie(a,"mcp.json",i)}}return ze()}async function dt(r,n){let t=ye(r,".claude","settings.local.json"),e=await Fe(t);if(e&&typeof e=="object"){let o=e;if(Array.isArray(o.disabledMcpjsonServers))return o.disabledMcpjsonServers.some(a=>a===n)}return!1}var sn=/^\[mcp_servers\.([^\]]+)\]/;function on(r){let n=r.split(`
97
- `),t=[],e=null;for(let o of n){let a=o.match(sn);a?(e&&t.push({name:e.name,body:e.bodyLines.join(`
98
- `)}),e={name:a[1],bodyLines:[]}):o.startsWith("[")?e&&(t.push({name:e.name,body:e.bodyLines.join(`
99
- `)}),e=null):e&&e.bodyLines.push(o)}return e&&t.push({name:e.name,body:e.bodyLines.join(`
100
- `)}),t}function rn(r){return/enabled\s*=\s*false/i.test(r)}async function Ge(r){let n=process.env.CODEX_HOME||ye(ht(),".codex"),t=await ut(ye(n,"config.toml"),"user");if(t.found)return t;let e=await ut(ye(r,".codex","config.toml"),"project");return e.found?e:ze()}async function ut(r,n){try{let t=await pt(r,"utf-8"),e=on(t);for(let o of e)if(Le.test(o.name)){let a=rn(o.body);return Ie(o.name,n,a)}}catch(t){}return ze()}import{mkdir as an,readFile as ft,writeFile as mt}from"fs/promises";import{homedir as gt}from"os";import{dirname as cn,join as je}from"path";var yt="https://mcp.popmelt.com/mcp";async function wt(r=yt){let n=je(gt(),".claude.json"),t;try{let o=await ft(n,"utf-8");t=JSON.parse(o)}catch(o){t={}}(!t.mcpServers||typeof t.mcpServers!="object")&&(t.mcpServers={});let e=t.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:r},await mt(n,JSON.stringify(t,null,2)+`
101
- `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function vt(r=yt){let n=process.env.CODEX_HOME||je(gt(),".codex"),t=je(n,"config.toml"),e;try{e=await ft(t,"utf-8")}catch(a){e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await an(cn(t),{recursive:!0});let o=`
96
+ Include property and bindings when the decision context has class-level evidence.`}import{readFile as lt}from"fs/promises";import{homedir as dt}from"os";import{join as fe}from"path";var _e=/popmelt/i;function Ne(){return{found:!1,name:null,scope:null,disabled:!1}}function me(r,t,e=!1){return{found:!0,name:r,scope:t,disabled:e}}function Re(r){for(let t of Object.keys(r))if(_e.test(t))return t;return null}async function Ae(r){try{let t=await lt(r,"utf-8");return JSON.parse(t)}catch(t){return null}}async function Je(r){let t=dt(),e=await Ae(fe(t,".claude.json"));if(e&&typeof e=="object"){let s=e;if(s.mcpServers&&typeof s.mcpServers=="object"){let a=Re(s.mcpServers);if(a)return me(a,"user")}if(s.projects&&typeof s.projects=="object"){let l=s.projects[r];if(l&&typeof l=="object"){let i=l;if(i.mcpServers&&typeof i.mcpServers=="object"){let u=Re(i.mcpServers);if(u){let h=Array.isArray(i.disabledMcpjsonServers)&&i.disabledMcpjsonServers.some(A=>_e.test(A));return me(u,"project",h)}}}}}let n=await Ae(fe(r,".mcp.json"));if(n&&typeof n=="object"){let s=n;if(s.mcpServers&&typeof s.mcpServers=="object"){let l=Re(s.mcpServers);if(l){let i=await at(r,l);return me(l,"mcp.json",i)}}let a=Re(s);if(a&&a!=="mcpServers"){let l=await at(r,a);return me(a,"mcp.json",l)}}return Ne()}async function at(r,t){let e=fe(r,".claude","settings.local.json"),n=await Ae(e);if(n&&typeof n=="object"){let s=n;if(Array.isArray(s.disabledMcpjsonServers))return s.disabledMcpjsonServers.some(a=>a===t)}return!1}var Ft=/^\[mcp_servers\.([^\]]+)\]/;function Lt(r){let t=r.split(`
97
+ `),e=[],n=null;for(let s of t){let a=s.match(Ft);a?(n&&e.push({name:n.name,body:n.bodyLines.join(`
98
+ `)}),n={name:a[1],bodyLines:[]}):s.startsWith("[")?n&&(e.push({name:n.name,body:n.bodyLines.join(`
99
+ `)}),n=null):n&&n.bodyLines.push(s)}return n&&e.push({name:n.name,body:n.bodyLines.join(`
100
+ `)}),e}function jt(r){return/enabled\s*=\s*false/i.test(r)}async function Be(r){let t=process.env.CODEX_HOME||fe(dt(),".codex"),e=await ct(fe(t,"config.toml"),"user");if(e.found)return e;let n=await ct(fe(r,".codex","config.toml"),"project");return n.found?n:Ne()}async function ct(r,t){try{let e=await lt(r,"utf-8"),n=Lt(e);for(let s of n)if(_e.test(s.name)){let a=jt(s.body);return me(s.name,t,a)}}catch(e){}return Ne()}import{mkdir as zt,readFile as ut,writeFile as pt}from"fs/promises";import{homedir as ht}from"os";import{dirname as Ht,join as Ue}from"path";var ft="https://mcp.popmelt.com/mcp";async function mt(r=ft){let t=Ue(ht(),".claude.json"),e;try{let s=await ut(t,"utf-8");e=JSON.parse(s)}catch(s){e={}}(!e.mcpServers||typeof e.mcpServers!="object")&&(e.mcpServers={});let n=e.mcpServers;for(let s of Object.keys(n))if(/popmelt/i.test(s))return{installed:!1,provider:"claude",scope:null,reason:"already_configured"};return n.popmelt={type:"http",url:r},await pt(t,JSON.stringify(e,null,2)+`
101
+ `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function gt(r=ft){let t=process.env.CODEX_HOME||Ue(ht(),".codex"),e=Ue(t,"config.toml"),n;try{n=await ut(e,"utf-8")}catch(a){n=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(n))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await zt(Ht(e),{recursive:!0});let s=`
102
102
  [mcp_servers.popmelt]
103
103
  url = "${r}"
104
- `;return await mt(t,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function we(r){let t=(r.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!t)throw new Error("Missing multipart boundary");let e=t[1]||t[2],o=await ln(r),a=Buffer.from(`--${e}`),i=Buffer.from(`--${e}--`),l,h,f,J,A,O,g,H,W,v,L,C,V=[],E=0,X=[];for(;E<o.length;){let U=o.indexOf(a,E);if(U===-1)break;let ne=U+a.length;if(o.slice(U,U+i.length).equals(i))break;let P=ne;o[P]===13&&o[P+1]===10&&(P+=2);let x=o.indexOf(`\r
104
+ `;return await pt(e,n+s,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function Fe(r){let e=(r.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!e)throw new Error("Missing multipart boundary");let n=e[1]||e[2],s=await Gt(r),a=Buffer.from(`--${n}`),l=Buffer.from(`--${n}--`),i,u,h,A,T,k,f,F,j,J,v,B,E=[],U=0,z=[];for(;U<s.length;){let O=s.indexOf(a,U);if(O===-1)break;let Z=O+a.length;if(s.slice(O,O+l.length).equals(l))break;let S=Z;s[S]===13&&s[S+1]===10&&(S+=2);let g=s.indexOf(`\r
105
105
  \r
106
- `,P);if(x===-1)break;let G=o.slice(P,x).toString("utf-8"),k=x+4,K=o.indexOf(a,k),me=K!==-1?K-2:o.length;X.push({headers:G,body:o.slice(k,me)}),E=K!==-1?K:o.length}for(let U of X){let ne=U.headers.match(/name="([^"]+)"/);if(!ne)continue;let P=ne[1];if(P==="screenshot")l=U.body;else if(P==="feedback")h=U.body.toString("utf-8");else if(P==="color")f=U.body.toString("utf-8");else if(P==="provider")J=U.body.toString("utf-8");else if(P==="model")A=U.body.toString("utf-8");else if(P==="goal")O=U.body.toString("utf-8");else if(P==="pageUrl")g=U.body.toString("utf-8");else if(P==="viewport")H=U.body.toString("utf-8");else if(P==="planId")W=U.body.toString("utf-8");else if(P==="manifest")v=U.body.toString("utf-8");else if(P==="tasks")L=U.body.toString("utf-8");else if(P==="sourceId")C=U.body.toString("utf-8");else if(P.startsWith("image-")){let x=P.split("-"),G=parseInt(x[x.length-1],10),k=x.slice(1,-1).join("-");k&&!isNaN(G)&&V.push({annotationId:k,index:G,data:U.body})}}if(!l)throw new Error("Missing screenshot field");return h||(h=""),{screenshot:l,feedback:h,color:f,provider:J,model:A,goal:O,pageUrl:g,viewport:H,planId:W,manifest:v,tasks:L,sourceId:C,pastedImages:V}}function ln(r){return new Promise((n,t)=>{let e=[];r.on("data",o=>e.push(o)),r.on("end",()=>n(Buffer.concat(e))),r.on("error",t)})}function xe(r,n){var e,o;let t=[];if(r.annotations.length>0){t.push("## Annotations");for(let a of r.annotations){let i=a.elements.map(f=>{let J=[f.selector];return f.reactComponent&&J.push(`(${f.reactComponent})`),J.join(" ")}).join(", "),l=a.instruction||"No text";t.push(`- id=${a.id} [${a.type}] ${l} \u2192 Elements: ${i||"none"}`);let h=n==null?void 0:n[a.id];if(h&&h.length>0)for(let f of h)t.push(` Attached image: use the Read tool to view ${f}`)}}if(r.styleModifications.length>0){t.push(""),t.push("## Style Changes (make permanent in source)"),t.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let a of r.styleModifications){let i=(e=a.element)!=null&&e.reactComponent?`(${a.element.reactComponent})`:"";for(let l of a.changes)t.push(`- ${a.selector} ${i}: ${l.property} ${l.original} \u2192 ${l.modified}`)}}if((o=r.spacingTokenChanges)!=null&&o.length){t.push(""),t.push("## Spacing Token Changes"),t.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let a of r.spacingTokenChanges){t.push(`
107
- ### ${a.tokenName}: ${a.originalPx}px \u2192 ${a.newPx}px`);for(let i of a.affectedElements){let l=i.reactComponent?` (${i.reactComponent})`:"";i.matchedClass&&i.suggestedClass?t.push(`- ${i.selector}${l}: \`${i.matchedClass}\` \u2192 \`${i.suggestedClass}\``):t.push(`- ${i.selector}${l}: ${i.property} ${a.originalPx}px \u2192 ${a.newPx}px`),t.push(` class="${i.className}"`)}}}if(r.inspectedElement){let a=r.inspectedElement;t.push(""),t.push("## Inspected Element"),t.push("The developer has this element selected in the inspector:");let i=[a.selector];a.reactComponent&&i.push(`(${a.reactComponent})`),a.context&&i.push(`in ${a.context}`),a.textContent&&i.push(`"${a.textContent.slice(0,80)}"`),t.push(`- ${i.join(" ")}`)}return t.join(`
108
- `)}function St(r,n,t){var a;let e=[];if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),(t==null?void 0:t.provider)!=="codex"&&(e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),e.push("")),e.push(`The developer annotated their running app at ${n.url} (${n.viewport.width}x${n.viewport.height}).`),t!=null&&t.threadHistory&&t.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let i=0;for(let l of t.threadHistory)if(l.role==="human")i++,l.replyToQuestion?(e.push(`### Round ${i} (human) \u2014 reply`),e.push(`"${l.replyToQuestion}"`)):(e.push(`### Round ${i} (human)`),l.feedbackSummary&&e.push(`Annotations: ${l.feedbackSummary}`),l.annotationIds&&l.annotationIds.length>0&&e.push(`Annotation IDs: ${l.annotationIds.join(", ")}`));else if(l.question)e.push(`### Round ${i} (assistant) \u2014 question`),e.push(`"${l.question}"`);else{if(e.push(`### Round ${i} (assistant)`),l.responseText&&e.push(`Response: ${l.responseText}`),l.resolutions&&l.resolutions.length>0)for(let h of l.resolutions){let f=(a=h.finalScope)!=null?a:h.inferredScope,J=f?` [${f.breadth} ${f.target}]`:"";e.push(`- ${h.annotationId}: ${h.status}${J} \u2014 ${h.summary}`),h.filesModified&&h.filesModified.length>0&&e.push(` Files: ${h.filesModified.join(", ")}`)}l.toolsUsed&&l.toolsUsed.length>0&&e.push(`Tools used: ${l.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(t!=null&&t.designModel){e.push(""),e.push("## Established Design Policies"),e.push("This project has an established design model (stored in .popmelt/model.json), extracted from the developer's previous design decisions. When making changes, follow these patterns unless the developer explicitly overrides them. When asked about design tokens, component patterns, or design decisions, reference this model as the authoritative source.");let i=t.designModel.rules;if(Array.isArray(i)&&i.length>0){e.push(""),e.push("Rules:");for(let f of i)typeof f=="string"&&e.push(`- ${f}`)}let l=t.designModel.tokens;l&&typeof l=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(l,null,2)),e.push("```"));let h=t.designModel.components;h&&typeof h=="object"&&(e.push(""),e.push("Component patterns:"),e.push("```json"),e.push(JSON.stringify(h,null,2)),e.push("```")),e.push(""),e.push("### Novel Pattern Detection"),e.push("When you make a design decision that has no matching policy in the model above (e.g., styling a component type not yet in the model, choosing a color with no token, picking spacing with no rule), flag it:"),e.push("<novel>"),e.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),e.push("</novel>"),e.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),e.push("- `element`: What you are styling or creating"),e.push("- `decision`: What you decided to do (specific values)"),e.push("- `reason`: Why this is novel (what is missing from the model)"),e.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}t!=null&&t.designModel||(e.push(""),e.push("## Design Context"),e.push("This project uses Popmelt for design governance. Design decisions are stored in .popmelt/decisions/ (JSON files). A materialized design model may exist at .popmelt/model.json. When the developer asks about design tokens, patterns, or past decisions, check these files first before searching source code."));let o=xe(n,t==null?void 0:t.imagePaths);return o&&(e.push(""),e.push(o)),e.push(""),e.push("Follow the developer's instructions. If they ask for changes, apply them to the source files \u2014 the dev server has HMR so changes appear immediately. If they ask a question or request analysis, respond in text without modifying code."),e.push(""),e.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e.push(""),e.push("## Resolution"),e.push("After completing all work, output a resolution block listing what you did for each annotation:"),e.push("<resolution>"),e.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),e.push("</resolution>"),e.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),e.push(""),e.push("### Scope classification"),e.push("Each resolution MUST include scope fields:"),e.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),e.push("- `inferredScope`: What scope the change actually has, based on what you modified."),e.push("Scope has two dimensions:"),e.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),e.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),e.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),e.push("If you cannot confidently determine scope, set it to null."),e.push(""),e.push("## Questions"),e.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),e.push('<question>What do you mean by "..."?</question>'),e.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),e.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),e.join(`
109
- `)}function It(r){var t;let n=r.match(/<question>\s*([\s\S]*?)\s*<\/question>/);return(t=n==null?void 0:n[1])!=null?t:null}function xt(r,n,t,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),t!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`);let a=n.find(i=>i.role==="human"&&i.feedbackContext);if(a!=null&&a.feedbackContext&&(o.push(""),o.push(a.feedbackContext)),n.length>0){o.push(""),o.push("## Conversation History");let i=0;for(let l of n)l.role==="human"?(i++,l.replyToQuestion?(o.push(`### Round ${i} (human) \u2014 reply`),o.push(`"${l.replyToQuestion}"`)):(o.push(`### Round ${i} (human)`),l.feedbackSummary&&o.push(`Annotations: ${l.feedbackSummary}`))):l.question?(o.push(`### Round ${i} (assistant) \u2014 question`),o.push(`"${l.question}"`)):(o.push(`### Round ${i} (assistant)`),l.responseText&&o.push(`Response: ${l.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let i of e)o.push(`Attached image: use the Read tool to view the image at: ${i}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
110
- `)}function dn(r){if(typeof r!="object"||r===null)return!1;let n=r;return(n.breadth==="instance"||n.breadth==="pattern")&&(n.target==="element"||n.target==="component"||n.target==="token")}function Pt(r){if(typeof r!="object"||r===null||typeof r.annotationId!="string"||r.status!=="resolved"&&r.status!=="needs_review"||typeof r.summary!="string")return!1;let n=r;for(let t of["declaredScope","inferredScope","finalScope"])if(n[t]!==void 0&&n[t]!==null&&!dn(n[t]))return!1;return!0}function kt(r){let n=r.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!n||!n[1])return[];try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(Pt):[]}catch(t){return[]}}function bt(r){let n=[],t=/<resolution>\s*([\s\S]*?)\s*<\/resolution>/g,e;for(;(e=t.exec(r))!==null;)if(e[1])try{let o=JSON.parse(e[1]);Array.isArray(o)&&n.push(...o.filter(Pt))}catch(o){}return n}function Tt(r){let n=r.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!(n!=null&&n[1]))return[];try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;return(o.category==="token"||o.category==="component"||o.category==="element")&&typeof o.element=="string"&&typeof o.decision=="string"&&typeof o.reason=="string"}):[]}catch(t){return[]}}function Rt(r,n,t,e,o,a){let i=[];return i.push("You are a UI design planner. You are looking at a full-page screenshot of a web application."),i.push(""),i.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),i.push(""),i.push(`Page: ${t}`),i.push(`Viewport: ${e.width}x${e.height}`),o&&(i.push(""),i.push("## Page Elements (ground truth)"),i.push("Below is a structured inventory of actual DOM elements on this page. Cross-reference"),i.push("against this list \u2014 do NOT reference elements that aren't listed here."),i.push(""),i.push("<manifest>"),i.push(o),i.push("</manifest>")),a&&(i.push(""),i.push("## Developer Context"),i.push("The developer has the following annotations and style changes on their canvas. Factor these into your plan:"),i.push(a)),i.push(""),i.push("## Goal"),i.push(n),i.push(""),i.push("## Your Task"),i.push("Analyze the screenshot and decompose the goal into specific, element-level tasks."),i.push("Each task targets a specific region of the page and gives a clear instruction for a worker agent."),i.push(""),i.push("Output your plan as a JSON array inside a <plan> tag. Each task has:"),i.push('- `id`: A short unique identifier (e.g., "t1", "t2")'),i.push("- `instruction`: Clear, specific instruction for a worker agent (what to change and how)"),i.push("- `region`: Bounding box in page coordinates `{x, y, width, height}` \u2014 where (x,y) is top-left corner"),i.push("- `priority`: Optional 1-5 (1=highest). Tasks with no dependency can share a priority level."),i.push(""),i.push("Example:"),i.push("<plan>"),i.push("["),i.push(' {"id":"t1","instruction":"Increase heading font-size to 48px and change font-weight to 700","region":{"x":100,"y":50,"width":600,"height":80},"priority":1},'),i.push(' {"id":"t2","instruction":"Add a subtle box-shadow to the card container","region":{"x":80,"y":200,"width":640,"height":300},"priority":2}'),i.push("]"),i.push("</plan>"),i.push(""),i.push("Guidelines:"),i.push("- CRITICAL: Cross-check all element references against the <manifest>. Only reference elements that actually exist. Use the manifest's text content, component names, and bounding rects for precise instructions."),i.push('- Be specific about values (colors, sizes, spacing) rather than vague ("make it look better")'),i.push("- Each task should be independently actionable by a worker that can only see its region"),i.push("- Regions should tightly bound the relevant UI element(s)"),i.push("- Keep tasks atomic \u2014 one change per task, not multiple unrelated changes"),i.push("- Order by priority: structural changes first, then visual polish"),i.push("- If the goal can be accomplished as a single change, return a plan with just one task. Only decompose when the goal genuinely requires multiple independent changes."),i.push("- If the goal is unclear or you need more context, output a question instead:"),i.push("<question>Your question here</question>"),i.push(""),i.push("Do NOT modify any files. You are a planner only \u2014 output a <plan> or <question>, nothing else."),i.join(`
111
- `)}function $t(r){let n=r.match(/<plan>\s*([\s\S]*?)\s*<\/plan>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return Array.isArray(t)?t.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;if(typeof o.id!="string"||typeof o.instruction!="string"||typeof o.region!="object"||o.region===null)return!1;let a=o.region;return typeof a.x=="number"&&typeof a.y=="number"&&typeof a.width=="number"&&typeof a.height=="number"}):null}catch(t){return null}}function Mt(r,n,t){let e=[];e.push("You are reviewing whether a series of UI changes achieved the original design goal."),e.push(""),e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),e.push(""),e.push("## Original Goal"),e.push(n),e.push(""),e.push("## Completed Tasks");for(let o of t)e.push(`- [${o.id}] ${o.instruction} \u2192 ${o.summary}`);return e.push(""),e.push("## Your Task"),e.push("Look at the current screenshot and determine if the goal has been achieved."),e.push("Output your verdict inside a <review> tag:"),e.push("<review>"),e.push('{"verdict":"pass","summary":"The changes look good..."}'),e.push("</review>"),e.push(""),e.push("Or if issues remain:"),e.push("<review>"),e.push('{"verdict":"fail","summary":"Some issues remain...","issues":["Issue 1","Issue 2"]}'),e.push("</review>"),e.push(""),e.push("Do NOT modify any files. Output only a <review> block."),e.join(`
112
- `)}function Ot(r,n,t,e,o){let a=[];a.push("You are implementing a series of UI changes on a web application."),a.push(""),o!=="codex"&&(a.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),a.push("")),a.push(`Page: ${t} (${e.width}x${e.height})`),a.push(""),a.push("## Tasks"),a.push("Each task targets a specific region of the page. Complete them in order."),a.push("");for(let i of n){if(a.push(`### Task ${i.planTaskId} (annotationId: ${i.annotationId})`),a.push(`Instruction: ${i.instruction}`),a.push(`Region: (${i.region.x}, ${i.region.y}) ${i.region.width}x${i.region.height}`),i.linkedSelector&&a.push(`Target element: ${i.linkedSelector}`),i.elements&&i.elements.length>0){let l=i.elements.map(h=>{let f=[h.selector];return h.reactComponent&&f.push(`(${h.reactComponent})`),f.join(" ")}).join(", ");a.push(`Elements: ${l}`)}a.push("")}return a.push("## Instructions"),a.push("- Apply each change to the source files \u2014 the dev server has HMR so changes appear immediately."),a.push("- IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source."),a.push("- You may use parallel subagents (Task tool) for independent changes, or work serially \u2014 use your judgment."),a.push(""),a.push("## Resolution"),a.push("CRITICAL: After completing EACH task, immediately output a <resolution> block for that task."),a.push("Do NOT wait until all tasks are done \u2014 output each resolution as soon as that task is finished."),a.push("<resolution>"),a.push('[{"annotationId":"<annotationId>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),a.push("</resolution>"),a.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),a.push(""),a.push("### Scope classification"),a.push("Each resolution MUST include scope fields:"),a.push("- `declaredScope`: What scope the task instruction implies. null if no signal."),a.push("- `inferredScope`: What scope the change actually has, based on what you modified."),a.push("Scope has two dimensions:"),a.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),a.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),a.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),a.push("If you cannot confidently determine scope, set it to null."),a.join(`
113
- `)}function Ct(r){let n=r.match(/<review>\s*([\s\S]*?)\s*<\/review>/);if(!(n!=null&&n[1]))return null;try{let t=JSON.parse(n[1]);return typeof t!="object"||t===null||t.verdict!=="pass"&&t.verdict!=="fail"||typeof t.summary!="string"?null:{verdict:t.verdict,summary:t.summary,issues:Array.isArray(t.issues)?t.issues.filter(e=>typeof e=="string"):void 0}}catch(t){return null}}var _e=class{constructor(n=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.maxConcurrency=n}setProcessor(n){this.processor=n}get active(){let n=this.activeJobs.values().next();return n.done?null:n.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(n,t){t?this.activeProcesses.set(n,t):this.activeProcesses.delete(n)}enqueue(n){return this.queue.push(n),this.processNext(),this.queue.length+this.activeJobs.size}addListener(n){return this.listeners.add(n),()=>this.listeners.delete(n)}broadcast(n,t,e){for(let o of this.listeners)o(n,t,e)}cancelJob(n){let t=this.activeProcesses.get(n),e=this.activeJobs.get(n);return!t||!e?!1:(t.kill("SIGTERM"),this.activeProcesses.delete(n),this.activeJobs.delete(n),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,message:"Cancelled by user",cancelled:!0},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let n=Array.from(this.activeJobs.keys());for(let t of n)this.cancelJob(t);return!0}destroy(){for(let n of this.activeProcesses.values())n.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear()}async destroyAsync(n=1e4){let t=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),t.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of t)try{e.kill("SIGTERM")}catch(o){}await Promise.all(t.map(e=>new Promise(o=>{let a=!1,i=()=>{a||(a=!0,o())};e.on("exit",i),e.on("error",i),setTimeout(()=>{if(!a){try{e.kill("SIGKILL")}catch(l){}setTimeout(i,500)}},n)}))),this.activeProcesses.clear(),this.activeJobs.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let n=this.queue.shift();this.activeJobs.set(n.id,n),n.status="running",this.broadcast({type:"job_started",jobId:n.id,position:0},n.id,n.sourceId),this.processor(n).catch(t=>{n.status="error",n.error=t instanceof Error?t.message:String(t),this.broadcast({type:"error",jobId:n.id,message:n.error},n.id,n.sourceId)}).finally(()=>{this.activeJobs.delete(n.id),this.activeProcesses.delete(n.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},n.id)})}}};import{mkdir as un,readFile as pn,writeFile as hn}from"fs/promises";import{dirname as fn,join as mn}from"path";var gn={version:1,threads:{}},Je=class{constructor(n){this.cache=null;this.writeChain=Promise.resolve();this.filePath=mn(n,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let n=await pn(this.filePath,"utf-8"),t=JSON.parse(n);if(t&&t.version===1&&t.threads)return this.cache=t,this.cache}catch(n){}return this.cache=ce(te({},gn),{threads:{}}),this.cache}async getThread(n){var e;return(e=(await this.load()).threads[n])!=null?e:null}async findContinuationThread(n){if(n.length===0)return null;let t=await this.load(),e=new Set(n);for(let o of Object.values(t.threads))if(o.elementIdentifiers.some(i=>e.has(i)))return o;return null}async createThread(n,t){let e=await this.load(),o={id:n,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:t,messages:[]};return e.threads[n]=o,await this.persist(),o}async appendMessage(n,t){let o=(await this.load()).threads[n];o&&(o.messages.push(t),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(n,t){let o=(await this.load()).threads[n];if(!o)return;let a=new Set(o.elementIdentifiers);for(let i of t)a.has(i)||o.elementIdentifiers.push(i);o.updatedAt=Date.now(),await this.persist()}async getThreadHistory(n,t=6){let e=await this.getThread(n);return!e||e.messages.length===0?[]:e.messages.length<=t?e.messages:[e.messages[0],...e.messages.slice(-(t-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await un(fn(this.filePath),{recursive:!0}),await hn(this.filePath,JSON.stringify(this.cache,null,2))}catch(n){console.error("[ThreadStore] Failed to persist:",n)}}),await this.writeChain}};var Jn={};var $n=1111,Mn=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],On=1800*1e3,Cn=3600*1e3;function Dt(r){if(!r)return!1;try{let n=new URL(r);return n.hostname==="localhost"||n.hostname==="127.0.0.1"}catch(n){return!1}}function En(r,n){let t=r.headers.origin;Dt(t)&&(n.setHeader("Access-Control-Allow-Origin",t),n.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),n.setHeader("Access-Control-Allow-Headers","Content-Type"))}function m(r,n,t){r.writeHead(n,{"Content-Type":"application/json"}),r.end(JSON.stringify(t))}function An(r,n){if(!r)return n;let t=r.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!t)return n;let[,e,o,a]=t;return`\x1B[38;2;${parseInt(e,16)};${parseInt(o,16)};${parseInt(a,16)}m${n}\x1B[0m`}function Et(r,n){try{r.res.write(`event: ${n.type}
114
- data: ${JSON.stringify(n)}
106
+ `,S);if(g===-1)break;let L=s.slice(S,g).toString("utf-8"),x=g+4,Q=s.indexOf(a,x),de=Q!==-1?Q-2:s.length;z.push({headers:L,body:s.slice(x,de)}),U=Q!==-1?Q:s.length}for(let O of z){let Z=O.headers.match(/name="([^"]+)"/);if(!Z)continue;let S=Z[1];if(S==="screenshot")i=O.body;else if(S==="feedback")u=O.body.toString("utf-8");else if(S==="color")h=O.body.toString("utf-8");else if(S==="provider")A=O.body.toString("utf-8");else if(S==="model")T=O.body.toString("utf-8");else if(S==="goal")k=O.body.toString("utf-8");else if(S==="pageUrl")f=O.body.toString("utf-8");else if(S==="viewport")F=O.body.toString("utf-8");else if(S==="planId")j=O.body.toString("utf-8");else if(S==="manifest")J=O.body.toString("utf-8");else if(S==="tasks")v=O.body.toString("utf-8");else if(S==="sourceId")B=O.body.toString("utf-8");else if(S.startsWith("image-")){let g=S.split("-"),L=parseInt(g[g.length-1],10),x=g.slice(1,-1).join("-");x&&!isNaN(L)&&E.push({annotationId:x,index:L,data:O.body})}}if(!i)throw new Error("Missing screenshot field");return u||(u=""),{screenshot:i,feedback:u,color:h,provider:A,model:T,goal:k,pageUrl:f,viewport:F,planId:j,manifest:J,tasks:v,sourceId:B,pastedImages:E}}function Gt(r){return new Promise((t,e)=>{let n=[];r.on("data",s=>n.push(s)),r.on("end",()=>t(Buffer.concat(n))),r.on("error",e)})}function $e(r,t){var n,s;let e=[];if(r.annotations.length>0){e.push("## Annotations");for(let a of r.annotations){let l=a.elements.map(h=>{let A=[h.selector];return h.reactComponent&&A.push(`(${h.reactComponent})`),A.join(" ")}).join(", "),i=a.instruction||"No text";e.push(`- id=${a.id} [${a.type}] ${i} \u2192 Elements: ${l||"none"}`);let u=t==null?void 0:t[a.id];if(u&&u.length>0)for(let h of u)e.push(` Attached image: use the Read tool to view ${h}`)}}if(r.styleModifications.length>0){e.push(""),e.push("## Style Changes (make permanent in source)"),e.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let a of r.styleModifications){let l=(n=a.element)!=null&&n.reactComponent?`(${a.element.reactComponent})`:"";for(let i of a.changes)e.push(`- ${a.selector} ${l}: ${i.property} ${i.original} \u2192 ${i.modified}`)}}if((s=r.spacingTokenChanges)!=null&&s.length){e.push(""),e.push("## Spacing Token Changes"),e.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let a of r.spacingTokenChanges){e.push(`
107
+ ### ${a.tokenName}: ${a.originalPx}px \u2192 ${a.newPx}px`);for(let l of a.affectedElements){let i=l.reactComponent?` (${l.reactComponent})`:"";l.matchedClass&&l.suggestedClass?e.push(`- ${l.selector}${i}: \`${l.matchedClass}\` \u2192 \`${l.suggestedClass}\``):e.push(`- ${l.selector}${i}: ${l.property} ${a.originalPx}px \u2192 ${a.newPx}px`),e.push(` class="${l.className}"`)}}}if(r.inspectedElement){let a=r.inspectedElement;e.push(""),e.push("## Inspected Element"),e.push("The developer has this element selected in the inspector:");let l=[a.selector];a.reactComponent&&l.push(`(${a.reactComponent})`),a.context&&l.push(`in ${a.context}`),a.textContent&&l.push(`"${a.textContent.slice(0,80)}"`),e.push(`- ${l.join(" ")}`)}return e.join(`
108
+ `)}function yt(r,t,e){var a;let n=[];if(n.push("You are reviewing a UI screenshot with developer annotations."),n.push(""),(e==null?void 0:e.provider)!=="codex"&&(n.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`),n.push("")),n.push(`The developer annotated their running app at ${t.url} (${t.viewport.width}x${t.viewport.height}).`),e!=null&&e.threadHistory&&e.threadHistory.length>0){n.push(""),n.push("## Previous Conversation");let l=0;for(let i of e.threadHistory)if(i.role==="human")l++,i.replyToQuestion?(n.push(`### Round ${l} (human) \u2014 reply`),n.push(`"${i.replyToQuestion}"`)):(n.push(`### Round ${l} (human)`),i.feedbackSummary&&n.push(`Annotations: ${i.feedbackSummary}`),i.annotationIds&&i.annotationIds.length>0&&n.push(`Annotation IDs: ${i.annotationIds.join(", ")}`));else if(i.question)n.push(`### Round ${l} (assistant) \u2014 question`),n.push(`"${i.question}"`);else{if(n.push(`### Round ${l} (assistant)`),i.responseText&&n.push(`Response: ${i.responseText}`),i.resolutions&&i.resolutions.length>0)for(let u of i.resolutions){let h=(a=u.finalScope)!=null?a:u.inferredScope,A=h?` [${h.breadth} ${h.target}]`:"";n.push(`- ${u.annotationId}: ${u.status}${A} \u2014 ${u.summary}`),u.filesModified&&u.filesModified.length>0&&n.push(` Files: ${u.filesModified.join(", ")}`)}i.toolsUsed&&i.toolsUsed.length>0&&n.push(`Tools used: ${i.toolsUsed.join(", ")}`)}n.push(""),n.push("The current round is shown in full below.")}if(e!=null&&e.designModel){n.push(""),n.push("## Established Design Policies"),n.push("This project has an established design model (stored in .popmelt/model.json), extracted from the developer's previous design decisions. When making changes, follow these patterns unless the developer explicitly overrides them. When asked about design tokens, component patterns, or design decisions, reference this model as the authoritative source.");let l=e.designModel.rules;if(Array.isArray(l)&&l.length>0){n.push(""),n.push("Rules:");for(let h of l)typeof h=="string"&&n.push(`- ${h}`)}let i=e.designModel.tokens;i&&typeof i=="object"&&(n.push(""),n.push("Design tokens:"),n.push("```json"),n.push(JSON.stringify(i,null,2)),n.push("```"));let u=e.designModel.components;u&&typeof u=="object"&&(n.push(""),n.push("Component patterns:"),n.push("```json"),n.push(JSON.stringify(u,null,2)),n.push("```")),n.push(""),n.push("### Novel Pattern Detection"),n.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:"),n.push("<novel>"),n.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),n.push("</novel>"),n.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),n.push("- `element`: What you are styling or creating"),n.push("- `decision`: What you decided to do (specific values)"),n.push("- `reason`: Why this is novel (what is missing from the model)"),n.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}e!=null&&e.designModel||(n.push(""),n.push("## Design Context"),n.push("This project uses Popmelt for design governance. Design decisions are stored in .popmelt/decisions/ (JSON files). A materialized design model may exist at .popmelt/model.json. When the developer asks about design tokens, patterns, or past decisions, check these files first before searching source code."));let s=$e(t,e==null?void 0:e.imagePaths);return s&&(n.push(""),n.push(s)),n.push(""),n.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."),n.push(""),n.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),n.push(""),n.push("## Resolution"),n.push("After completing all work, output a resolution block listing what you did for each annotation:"),n.push("<resolution>"),n.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),n.push("</resolution>"),n.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),n.push(""),n.push("### Scope classification"),n.push("Each resolution MUST include scope fields:"),n.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),n.push("- `inferredScope`: What scope the change actually has, based on what you modified."),n.push("Scope has two dimensions:"),n.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),n.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),n.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),n.push("If you cannot confidently determine scope, set it to null."),n.push(""),n.push("## Questions"),n.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),n.push('<question>What do you mean by "..."?</question>'),n.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),n.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),n.join(`
109
+ `)}function wt(r){var e;let t=r.match(/<question>\s*([\s\S]*?)\s*<\/question>/);return(e=t==null?void 0:t[1])!=null?e:null}function vt(r,t,e,n){let s=[];s.push("You are continuing work on a UI based on the developer's reply to your question."),s.push(""),e!=="codex"&&s.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${r}`);let a=t.find(l=>l.role==="human"&&l.feedbackContext);if(a!=null&&a.feedbackContext&&(s.push(""),s.push(a.feedbackContext)),t.length>0){s.push(""),s.push("## Conversation History");let l=0;for(let i of t)i.role==="human"?(l++,i.replyToQuestion?(s.push(`### Round ${l} (human) \u2014 reply`),s.push(`"${i.replyToQuestion}"`)):(s.push(`### Round ${l} (human)`),i.feedbackSummary&&s.push(`Annotations: ${i.feedbackSummary}`))):i.question?(s.push(`### Round ${l} (assistant) \u2014 question`),s.push(`"${i.question}"`)):(s.push(`### Round ${l} (assistant)`),i.responseText&&s.push(`Response: ${i.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."),n&&n.length>0){s.push(""),s.push("## Attached Images"),s.push("The developer attached reference images with their reply:");for(let l of n)s.push(`Attached image: use the Read tool to view the image at: ${l}`)}return s.push(""),s.push("## Resolution"),s.push("After completing all work, output a resolution block listing what you did for each annotation:"),s.push("<resolution>"),s.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),s.push("</resolution>"),s.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),s.push(""),s.push("### Scope classification"),s.push("Each resolution MUST include scope fields:"),s.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),s.push("- `inferredScope`: What scope the change actually has, based on what you modified."),s.push("Scope has two dimensions:"),s.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),s.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),s.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),s.push("If you cannot confidently determine scope, set it to null."),s.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),s.push(""),s.push("## Questions"),s.push("If you still need clarification, output:"),s.push("<question>Your question here</question>"),s.push("You may output BOTH a <resolution> and a <question> in the same response."),s.join(`
110
+ `)}function Wt(r){if(typeof r!="object"||r===null)return!1;let t=r;return(t.breadth==="instance"||t.breadth==="pattern")&&(t.target==="element"||t.target==="component"||t.target==="token")}function Qt(r){if(typeof r!="object"||r===null||typeof r.annotationId!="string"||r.status!=="resolved"&&r.status!=="needs_review"||typeof r.summary!="string")return!1;let t=r;for(let e of["declaredScope","inferredScope","finalScope"])if(t[e]!==void 0&&t[e]!==null&&!Wt(t[e]))return!1;return!0}function St(r){let t=r.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!t||!t[1])return[];try{let e=JSON.parse(t[1]);return Array.isArray(e)?e.filter(Qt):[]}catch(e){return[]}}function xt(r){let t=r.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!(t!=null&&t[1]))return[];try{let e=JSON.parse(t[1]);return Array.isArray(e)?e.filter(n=>{if(typeof n!="object"||n===null)return!1;let s=n;return(s.category==="token"||s.category==="component"||s.category==="element")&&typeof s.element=="string"&&typeof s.decision=="string"&&typeof s.reason=="string"}):[]}catch(e){return[]}}var Me=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;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,e){e?this.activeProcesses.set(t,e):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,e,n){for(let s of this.listeners)s(t,e,n)}cancelJob(t){let e=this.activeProcesses.get(t),n=this.activeJobs.get(t);return!e||!n?!1:(e.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),n.status="error",n.error="Cancelled by user",this.broadcast({type:"error",jobId:n.id,message:"Cancelled by user",cancelled:!0},n.id,n.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let e of t)this.cancelJob(e);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear()}async destroyAsync(t=1e4){let e=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),e.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let n of e)try{n.kill("SIGTERM")}catch(s){}await Promise.all(e.map(n=>new Promise(s=>{let a=!1,l=()=>{a||(a=!0,s())};n.on("exit",l),n.on("error",l),setTimeout(()=>{if(!a){try{n.kill("SIGKILL")}catch(i){}setTimeout(l,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.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(e=>{t.status="error",t.error=e instanceof Error?e.message:String(e),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.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Yt,readFile as qt,writeFile as Kt}from"fs/promises";import{dirname as Xt,join as Vt}from"path";var Zt={version:1,threads:{}},Ce=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"),e=JSON.parse(t);if(e&&e.version===1&&e.threads)return this.cache=e,this.cache}catch(t){}return this.cache=ne(q({},Zt),{threads:{}}),this.cache}async getThread(t){var n;return(n=(await this.load()).threads[t])!=null?n:null}async findContinuationThread(t){if(t.length===0)return null;let e=await this.load(),n=new Set(t);for(let s of Object.values(e.threads))if(s.elementIdentifiers.some(l=>n.has(l)))return s;return null}async createThread(t,e){let n=await this.load(),s={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:e,messages:[]};return n.threads[t]=s,await this.persist(),s}async appendMessage(t,e){let s=(await this.load()).threads[t];s&&(s.messages.push(e),s.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,e){let s=(await this.load()).threads[t];if(!s)return;let a=new Set(s.elementIdentifiers);for(let l of e)a.has(l)||s.elementIdentifiers.push(l);s.updatedAt=Date.now(),await this.persist()}async getThreadHistory(t,e=6){let n=await this.getThread(t);return!n||n.messages.length===0?[]:n.messages.length<=e?n.messages:[n.messages[0],...n.messages.slice(-(e-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Yt(Xt(this.filePath),{recursive:!0}),await Kt(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var xn={};var hn=1111,fn=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],mn=1800*1e3,gn=3600*1e3;function It(r){if(!r)return!1;try{let t=new URL(r);return t.hostname==="localhost"||t.hostname==="127.0.0.1"}catch(t){return!1}}function yn(r,t){let e=r.headers.origin;It(e)&&(t.setHeader("Access-Control-Allow-Origin",e),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function P(r,t,e){r.writeHead(t,{"Content-Type":"application/json"}),r.end(JSON.stringify(e))}function wn(r,t){if(!r)return t;let e=r.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!e)return t;let[,n,s,a]=e;return`\x1B[38;2;${parseInt(n,16)};${parseInt(s,16)};${parseInt(a,16)}m${t}\x1B[0m`}function ze(r,t){try{r.res.write(`event: ${t.type}
111
+ data: ${JSON.stringify(t)}
115
112
 
116
- `)}catch(t){}}async function Dn(r){try{let n=new AbortController,t=setTimeout(()=>n.abort(),500),e=await fetch(`http://127.0.0.1:${r}/status`,{signal:n.signal});return clearTimeout(t),e.ok?await e.json():null}catch(n){return null}}function _n(r,n){return new Promise((t,e)=>{let o=i=>{r.removeListener("listening",a),e(i)},a=()=>{r.removeListener("error",o),t()};r.once("error",o),r.once("listening",a),r.listen(n,"127.0.0.1")})}async function _t(r={}){var Qe,qe,Ke,Xe,Ve,Ze,et,tt,nt;let n=(Qe=r.port)!=null?Qe:$n,t=(qe=r.projectRoot)!=null?qe:process.cwd(),e=wn("sha256").update(t).digest("hex").slice(0,12),o=(Ke=r.devOrigin)!=null?Ke:process.env.PORT?`http://localhost:${process.env.PORT}`:null,a=(Xe=r.tempDir)!=null?Xe:oe(bn(),"popmelt-bridge"),i=(Ve=r.maxTurns)!=null?Ve:40,l=(Ze=r.maxBudgetUsd)!=null?Ze:1,h=(et=r.allowedTools)!=null?et:Mn,f=(tt=r.claudePath)!=null?tt:"claude",J=(nt=r.provider)!=null?nt:"claude",A=r.timeoutMs,O=n,g={};for(let s of["claude","codex"])try{let c=yn("which",[s],{encoding:"utf-8"}).trim();g[s]={available:!0,path:c}}catch(c){g[s]={available:!1,path:null}}let[H,W]=await Promise.all([He(t),Ge(t)]);g.claude&&(g.claude.mcp=H),g.codex&&(g.codex.mcp=W),await vn(a,{recursive:!0}),At(a).catch(()=>{});let v=new _e(1),L=new Set,C=new Je(t),V=new Ee(t),E=new Ae(t,V,{claudePath:f,onEvent:s=>{for(let c of L)Et(c,s)}}),X=new Map,U=20,ne=300*1e3,P=[],x=null,G;v.addListener((s,c,u)=>{for(let d of L)(!u||!d.sourceId||d.sourceId===u)&&Et(d,s)}),v.setProcessor(async s=>{var Z,re,_,se,le,Se,ue;let c=s._replyPrompt,u=s._replyImagePaths,d=(Z=s.provider)!=null?Z:J,p;if(s.threadId){let $=await C.getThread(s.threadId);if($){for(let y=$.messages.length-1;y>=0;y--)if($.messages[y].sessionId){p=$.messages[y].sessionId;break}}}let S;if(p&&c){let $=(re=await C.getThread(s.threadId))==null?void 0:re.messages.filter(M=>M.role==="human").pop();if(S=($==null?void 0:$.replyToQuestion)||($==null?void 0:$.feedbackSummary)||"",u&&u.length>0){S+=`
113
+ `)}catch(e){}}async function vn(r){try{let t=new AbortController,e=setTimeout(()=>t.abort(),500),n=await fetch(`http://127.0.0.1:${r}/status`,{signal:t.signal});return clearTimeout(e),n.ok?await n.json():null}catch(t){return null}}function Sn(r,t){return new Promise((e,n)=>{let s=l=>{r.removeListener("listening",a),n(l)},a=()=>{r.removeListener("error",s),e()};r.once("error",s),r.once("listening",a),r.listen(t,"127.0.0.1")})}async function bt(r={}){var We,Qe,Ye,qe,Ke,Xe,Ve,Ze,et;let t=(We=r.port)!=null?We:hn,e=(Qe=r.projectRoot)!=null?Qe:process.cwd(),n=nn("sha256").update(e).digest("hex").slice(0,12),s=(Ye=r.devOrigin)!=null?Ye:process.env.PORT?`http://localhost:${process.env.PORT}`:null,a=(qe=r.tempDir)!=null?qe:re(dn(),"popmelt-bridge"),l=(Ke=r.maxTurns)!=null?Ke:40,i=(Xe=r.maxBudgetUsd)!=null?Xe:1,u=(Ve=r.allowedTools)!=null?Ve:fn,h=(Ze=r.claudePath)!=null?Ze:"claude",A=(et=r.provider)!=null?et:"claude",T=r.timeoutMs,k=t,f={};for(let o of["claude","codex"])try{let c=en("which",[o],{encoding:"utf-8"}).trim();f[o]={available:!0,path:c}}catch(c){f[o]={available:!1,path:null}}function F(o,c,p){return new Promise(d=>{let y=tn(c,o==="codex"?["exec","--json","--full-auto","ok"]:["-p","ok","--output-format","stream-json","--max-turns","1","--verbose"],{cwd:p,stdio:["ignore","ignore","pipe"],env:ne(q({},process.env),{ANTHROPIC_API_KEY:void 0})}),_=!1,N=w=>{_||(_=!0,d(w))},Y=setTimeout(()=>{y.kill("SIGTERM"),setTimeout(()=>{try{y.kill("SIGKILL")}catch(w){}},5e3),N(!0)},3e4);y.on("error",()=>{clearTimeout(Y),N(!1)}),y.on("close",w=>{clearTimeout(Y),N(w===0)})})}let[j,J]=await Promise.all([Je(e),Be(e)]);f.claude&&(f.claude.mcp=j),f.codex&&(f.codex.mcp=J),await sn(a,{recursive:!0}),Pt(a).catch(()=>{});let v=new Me(1),B=new Set,E=new Ce(e),U=new ke(e),z=new Te(e,U,{claudePath:h,onEvent:o=>{for(let c of B)ze(c,o)}}),O=20,Z=300*1e3,S=[],g=null,L;v.addListener((o,c,p)=>{for(let d of B)(!p||!d.sourceId||d.sourceId===p)&&ze(d,o)}),v.setProcessor(async o=>{var H,G,V,te,ie,ue;let c=o._replyPrompt,p=o._replyImagePaths,d=(H=o.provider)!=null?H:A,m;if(o.threadId){let I=await E.getThread(o.threadId);if(I){for(let M=I.messages.length-1;M>=0;M--)if(I.messages[M].sessionId){m=I.messages[M].sessionId;break}}}let y;if(m&&c){let I=(G=await E.getThread(o.threadId))==null?void 0:G.messages.filter(C=>C.role==="human").pop();if(y=(I==null?void 0:I.replyToQuestion)||(I==null?void 0:I.feedbackSummary)||"",p&&p.length>0){y+=`
117
114
 
118
- The developer attached reference images with their reply:`;for(let M of u)S+=`
119
- Attached image: use the Read tool to view the image at: ${M}`}S+=`
115
+ The developer attached reference images with their reply:`;for(let C of p)y+=`
116
+ Attached image: use the Read tool to view the image at: ${C}`}y+=`
120
117
 
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(p)S=xe(s.feedback,s.imagePaths)+`
118
+ 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)y=$e(o.feedback,o.imagePaths)+`
122
119
 
123
120
  Follow the developer's instructions. If they ask for changes, apply them to the source files.
124
121
 
125
122
  After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(d!=="codex"?`
126
123
 
127
- IMPORTANT: First, use the Read tool to view the updated screenshot at: ${s.screenshotPath}`:"");else{let $=!c&&s.threadId?await C.getThreadHistory(s.threadId):void 0,y=c?null:await E.loadModel();S=c!=null?c:St(s.screenshotPath,s.feedback,{threadHistory:$&&$.length>0?$:void 0,provider:d,imagePaths:s.imagePaths,designModel:y!=null?y:void 0})}let R=An(s.color,`[\u22B9 ${O}:${s.id}]`);console.log(`${R} Reviewing feedback ${s.screenshotPath} (provider: ${d})${s.threadId?` (thread: ${s.threadId})`:""}${p?` (resuming: ${p.slice(0,8)})`:""}`);let b=!!s._isPlanExecutor,D="",w=0,Y=($,y)=>{if(v.broadcast($,y,s.sourceId),b&&$.type==="delta"&&"text"in $){D+=$.text;let M=bt(D);if(M.length>w){let ee=M.slice(w);w=M.length,v.broadcast({type:"task_resolved",jobId:y,planId:s.planId,resolutions:ee,threadId:s.threadId},y,s.sourceId)}}},z=(_=s._allowedTools)!=null?_:h,{process:N,result:T}=d==="codex"?it(s.id,{prompt:S,projectRoot:t,screenshotPath:s.screenshotPath,resumeSessionId:p,model:s.model,onEvent:Y}):Ce(s.id,{prompt:S,projectRoot:t,maxTurns:i,maxBudgetUsd:l,allowedTools:z,claudePath:f,resumeSessionId:p,model:s.model,timeoutMs:A,onEvent:Y});v.setActiveProcess(s.id,N);let I=await T;if(s.result=I.text,I.success){console.log(`${R} Iteration complete`),I.fileEdits&&I.fileEdits.length>0&&console.log(`${R} Captured ${I.fileEdits.length} file edit(s): ${I.fileEdits.map(F=>`${F.tool} ${F.file_path}`).join(", ")}`),s.status="done";let $=It(I.text),y=kt(I.text);if(y.length>0&&s.annotationIds&&s.annotationIds.length>0){let F=new Set(s.annotationIds);y.every(ae=>F.has(ae.annotationId))||(y=y.map((ae,pe)=>ce(te({},ae),{annotationId:s.annotationIds[pe%s.annotationIds.length]})))}let M=I.fileEdits&&I.fileEdits.length>0?I.fileEdits.map(F=>`${F.tool} ${F.file_path.split("/").pop()}`):void 0;if(s.threadId&&await C.appendMessage(s.threadId,{role:"assistant",timestamp:Date.now(),jobId:s.id,responseText:I.text,resolutions:y.length>0?y:void 0,question:$!=null?$:void 0,sessionId:I.sessionId,toolsUsed:M}),V.captureGitDiff(t).then(async F=>{var st;let Q=Date.now(),ae=s.imagePaths?Object.values(s.imagePaths).flat():[],pe=[];if(s.imagePaths)for(let[Gt,jt]of Object.entries(s.imagePaths))for(let Be=0;Be<jt.length;Be++)pe.push(`screenshots/p-${s.id}-${Gt}-${Be}.png`);await V.persist({version:1,id:s.id,createdAt:s.createdAt,completedAt:Q,durationMs:Q-s.createdAt,url:s.feedback.url,viewport:s.feedback.viewport,screenshotPath:`screenshots/s-${s.id}.png`,pastedImagePaths:pe,annotations:s.feedback.annotations,styleModifications:s.feedback.styleModifications,inspectedElement:s.feedback.inspectedElement,provider:s.provider,model:s.model,sessionId:I.sessionId,threadId:s.threadId,planId:s.planId,planTaskId:s.planTaskId,responseText:I.text,resolutions:y.length>0?y:[],question:$!=null?$:void 0,fileEdits:(st=I.fileEdits)!=null?st:[],toolsUsed:M,gitDiff:F},s.screenshotPath,ae)}).catch(()=>{}),y.length>0&&y.some(Q=>{var pe;let ae=(pe=Q.finalScope)!=null?pe:Q.inferredScope;return(ae==null?void 0:ae.breadth)==="pattern"})&&E.run().catch(()=>{}),s.planId&&!s.planTaskId){let F=X.get(s.planId);if(F){let Q=$t(I.text);Q&&Q.length>0?(F.plan=Q,F.status="awaiting_approval",F.plannerThreadId=s.threadId,console.log(`${R} Plan ready: ${Q.length} tasks for group ${s.planId}`),v.broadcast({type:"plan_ready",jobId:s.id,planId:s.planId,tasks:Q,threadId:s.threadId},s.id,s.sourceId)):$||(F.status="error",console.error(`${R} Failed to parse plan from planner response`))}}if(s.planId&&s._isReview){let F=X.get(s.planId);if(F){let Q=Ct(I.text);Q&&(F.status=Q.verdict==="pass"?"done":"executing",console.log(`${R} Review verdict: ${Q.verdict} \u2014 ${Q.summary}`),v.broadcast({type:"plan_review",planId:s.planId,verdict:Q.verdict,summary:Q.summary,issues:Q.issues},s.id,s.sourceId))}}$&&(console.log(`${R} \u{1F4AC} Question detected: "${$.slice(0,120)}" \u2192 broadcasting to ${L.size} SSE clients (threadId=${(se=s.threadId)!=null?se:s.id}, annotationIds=${(Se=(le=s.annotationIds)==null?void 0:le.join(","))!=null?Se:"none"})`),v.broadcast({type:"question",jobId:s.id,threadId:(ue=s.threadId)!=null?ue:s.id,question:$,annotationIds:s.annotationIds},s.id,s.sourceId));let ee=Tt(I.text);ee.length>0&&(console.log(`${R} Novel pattern(s): ${ee.map(F=>`${F.category}/${F.element}`).join(", ")}`),v.broadcast({type:"novel_patterns",jobId:s.id,patterns:ee,threadId:s.threadId},s.id,s.sourceId)),v.broadcast({type:"done",jobId:s.id,success:!0,resolutions:y.length>0?y:void 0,responseText:I.text,threadId:s.threadId},s.id,s.sourceId),P.push({id:s.id,status:"done",completedAt:Date.now(),threadId:s.threadId})}else console.error(`${R} Error: ${I.error}`),s.status="error",s.error=I.error,v.broadcast({type:"error",jobId:s.id,message:I.error||"Unknown error"},s.id,s.sourceId),P.push({id:s.id,status:"error",completedAt:Date.now(),error:I.error,threadId:s.threadId});let j=Date.now()-ne;for(;P.length>0&&(P[0].completedAt<j||P.length>U);)P.shift()});let k=kn(async(s,c)=>{if(En(s,c),s.method==="OPTIONS"){c.writeHead(204),c.end();return}let d=new URL(s.url||"/",`http://127.0.0.1:${O}`).pathname;try{if(s.method==="POST"&&d==="/send")await K(s,c);else if(s.method==="GET"&&d==="/events")Pe(s,c);else if(s.method==="GET"&&d==="/status")ke(c);else if(s.method==="GET"&&d==="/capabilities")m(c,200,{providers:g});else if(s.method==="POST"&&d==="/mcp/install")await B(s,c);else if(s.method==="POST"&&d==="/reply")await me(s,c);else if(s.method==="POST"&&d==="/cancel")be(s,c);else if(s.method==="POST"&&d==="/materialize")await Te(c);else if(s.method==="POST"&&d==="/plan")await Re(s,c);else if(s.method==="POST"&&d==="/plan/approve")await $e(s,c);else if(s.method==="POST"&&d==="/plan/execute")await Me(s,c);else if(s.method==="POST"&&d==="/plan/review")await q(s,c);else if(s.method==="GET"&&d.startsWith("/plan/"))Ne(d.slice(6),c);else if(s.method==="POST"&&d==="/model/component")await Oe(s,c);else if(s.method==="DELETE"&&d==="/model/component")await Jt(s,c);else if(s.method==="PATCH"&&d==="/model/token")await Nt(s,c);else if(s.method==="DELETE"&&d==="/model/token")await Bt(s,c);else if(s.method==="GET"&&d==="/model"){let p=await E.loadModel();m(c,200,{model:p})}else if(s.method==="GET"&&d.startsWith("/thread/")){let p=d.slice(8);await zt(p,c)}else s.method==="GET"&&(d==="/canvas"||d==="/canvas/")?Ut(s,c):s.method==="GET"&&d==="/canvas/manifest"?await Ft(c):s.method==="GET"&&d==="/canvas/app.mjs"?await Lt(c):m(c,404,{error:"Not found"})}catch(p){console.error("[Bridge] Request error:",p),m(c,500,{error:p instanceof Error?p.message:"Internal error"})}});async function K(s,c){let{screenshot:u,feedback:d,color:p,provider:S,model:R,sourceId:b,pastedImages:D}=await we(s),w;try{w=JSON.parse(d)}catch(_){m(c,400,{error:"Invalid feedback JSON"});return}let Y=fe().slice(0,8),z=oe(a,`screenshot-${Y}.png`);await ve(z,u);let N={};if(D.length>0)for(let _ of D){let se=oe(a,`pasted-${Y}-${_.annotationId}-${_.index}.png`);await ve(se,_.data),N[_.annotationId]||(N[_.annotationId]=[]),N[_.annotationId].push(se)}let T=w.annotations.map(_=>_.linkedSelector).filter(_=>!!_),I;if(T.length>0){let _=await C.findContinuationThread(T);_?(I=_.id,await C.addElementIdentifiers(I,T)):I=(await C.createThread(Y,T)).id}let j=w.annotations.map(_=>_.id),Z=ce(te({id:Y,status:"queued",screenshotPath:z,feedback:w,createdAt:Date.now(),color:p,threadId:I,annotationIds:j,provider:S==="claude"||S==="codex"?S:void 0,model:R||void 0},Object.keys(N).length>0?{imagePaths:N}:{}),{sourceId:b||void 0});if(I){let _=w.annotations.map(le=>le.instruction||`[${le.type}]`).join("; "),se=xe(w,Object.keys(N).length>0?N:void 0);await C.appendMessage(I,{role:"human",timestamp:Date.now(),jobId:Y,screenshotPath:z,annotationIds:j,feedbackSummary:_,feedbackContext:se||void 0})}let re=v.enqueue(Z);m(c,200,{jobId:Y,position:re,threadId:I})}async function me(s,c){let u=s.headers["content-type"]||"",d,p,S,R,b,D,w=[];if(u.includes("multipart/form-data")){let y=await we(s),M=y.feedback?JSON.parse(y.feedback):{};d=M.threadId,p=M.reply,S=M.color,R=M.provider,b=M.model,D=M.sourceId||y.sourceId;for(let ee of y.pastedImages)w.push(ee.data)}else{let y=[];try{for(var le=de(s),Se,ue,$;Se=!(ue=await le.next()).done;Se=!1){let F=ue.value;y.push(typeof F=="string"?Buffer.from(F):F)}}catch(ue){$=[ue]}finally{try{Se&&(ue=le.return)&&await ue.call(le)}finally{if($)throw $[0]}}let M=Buffer.concat(y).toString("utf-8"),ee;try{ee=JSON.parse(M)}catch(F){m(c,400,{error:"Invalid JSON"});return}d=ee.threadId,p=ee.reply,S=ee.color,R=ee.provider,b=ee.model,D=ee.sourceId}if(!d||!p){m(c,400,{error:"Missing threadId or reply"});return}if(!await C.getThread(d)){m(c,404,{error:"Thread not found"});return}let z=fe().slice(0,8),N=[];for(let y=0;y<w.length;y++){let M=oe(a,`reply-${z}-${y}.png`);await ve(M,w[y]),N.push(M)}let T="";{let y=await C.getThreadHistory(d);for(let M=y.length-1;M>=0;M--)if(y[M].screenshotPath){T=y[M].screenshotPath;break}}if(!T){m(c,400,{error:"No screenshot available"});return}await C.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:z,replyToQuestion:p,screenshotPath:T});let I=await C.getThreadHistory(d),j=[];for(let y of I)if(y.annotationIds)for(let M of y.annotationIds)j.includes(M)||j.push(M);let Z=R==="claude"||R==="codex"?R:void 0,re=xt(T,I,Z,N.length>0?N:void 0),_={id:z,status:"queued",screenshotPath:T,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:S,threadId:d,annotationIds:j.length>0?j:void 0,provider:Z,model:b||void 0,sourceId:D||void 0};_._replyPrompt=re,N.length>0&&(_._replyImagePaths=N);let se=v.enqueue(_);m(c,200,{jobId:z,position:se,threadId:d})}function Pe(s,c){c.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),c.write(`event: connected
124
+ IMPORTANT: First, use the Read tool to view the updated screenshot at: ${o.screenshotPath}`:"");else{let I=!c&&o.threadId?await E.getThreadHistory(o.threadId):void 0,M=c?null:await z.loadModel();y=c!=null?c:yt(o.screenshotPath,o.feedback,{threadHistory:I&&I.length>0?I:void 0,provider:d,imagePaths:o.imagePaths,designModel:M!=null?M:void 0})}let _=wn(o.color,`[\u22B9 ${k}:${o.id}]`);console.log(`${_} Reviewing feedback ${o.screenshotPath} (provider: ${d})${o.threadId?` (thread: ${o.threadId})`:""}${m?` (resuming: ${m.slice(0,8)})`:""}`);let N=(I,M)=>{v.broadcast(I,M,o.sourceId)},{process:Y,result:w}=d==="codex"?st(o.id,{prompt:y,projectRoot:e,screenshotPath:o.screenshotPath,resumeSessionId:m,model:o.model,onEvent:N}):be(o.id,{prompt:y,projectRoot:e,maxTurns:l,maxBudgetUsd:i,allowedTools:u,claudePath:h,resumeSessionId:m,model:o.model,timeoutMs:T,onEvent:N});v.setActiveProcess(o.id,Y);let b=await w;if(o.result=b.text,b.success){console.log(`${_} Iteration complete`),b.fileEdits&&b.fileEdits.length>0&&console.log(`${_} Captured ${b.fileEdits.length} file edit(s): ${b.fileEdits.map(K=>`${K.tool} ${K.file_path}`).join(", ")}`),o.status="done";let I=wt(b.text),M=St(b.text);if(M.length>0&&o.annotationIds&&o.annotationIds.length>0){let K=new Set(o.annotationIds);M.every(R=>K.has(R.annotationId))||(M=M.map((R,D)=>ne(q({},R),{annotationId:o.annotationIds[D%o.annotationIds.length]})))}let C=b.fileEdits&&b.fileEdits.length>0?b.fileEdits.map(K=>`${K.tool} ${K.file_path.split("/").pop()}`):void 0;o.threadId&&await E.appendMessage(o.threadId,{role:"assistant",timestamp:Date.now(),jobId:o.id,responseText:b.text,resolutions:M.length>0?M:void 0,question:I!=null?I:void 0,sessionId:b.sessionId,toolsUsed:C}),U.captureGitDiff(e).then(async K=>{var ee;let ae=Date.now(),R=o.imagePaths?Object.values(o.imagePaths).flat():[],D=[];if(o.imagePaths)for(let[pe,$t]of Object.entries(o.imagePaths))for(let Oe=0;Oe<$t.length;Oe++)D.push(`screenshots/p-${o.id}-${pe}-${Oe}.png`);await U.persist({version:1,id:o.id,createdAt:o.createdAt,completedAt:ae,durationMs:ae-o.createdAt,url:o.feedback.url,viewport:o.feedback.viewport,screenshotPath:`screenshots/s-${o.id}.png`,pastedImagePaths:D,annotations:o.feedback.annotations,styleModifications:o.feedback.styleModifications,inspectedElement:o.feedback.inspectedElement,provider:o.provider,model:o.model,sessionId:b.sessionId,threadId:o.threadId,responseText:b.text,resolutions:M.length>0?M:[],question:I!=null?I:void 0,fileEdits:(ee=b.fileEdits)!=null?ee:[],toolsUsed:C,gitDiff:K},o.screenshotPath,R)}).catch(()=>{}),M.length>0&&M.some(ae=>{var D;let R=(D=ae.finalScope)!=null?D:ae.inferredScope;return(R==null?void 0:R.breadth)==="pattern"})&&z.run().catch(()=>{}),I&&(console.log(`${_} \u{1F4AC} Question detected: "${I.slice(0,120)}" \u2192 broadcasting to ${B.size} SSE clients (threadId=${(V=o.threadId)!=null?V:o.id}, annotationIds=${(ie=(te=o.annotationIds)==null?void 0:te.join(","))!=null?ie:"none"})`),v.broadcast({type:"question",jobId:o.id,threadId:(ue=o.threadId)!=null?ue:o.id,question:I,annotationIds:o.annotationIds},o.id,o.sourceId));let oe=xt(b.text);oe.length>0&&(console.log(`${_} Novel pattern(s): ${oe.map(K=>`${K.category}/${K.element}`).join(", ")}`),v.broadcast({type:"novel_patterns",jobId:o.id,patterns:oe,threadId:o.threadId},o.id,o.sourceId)),v.broadcast({type:"done",jobId:o.id,success:!0,resolutions:M.length>0?M:void 0,responseText:b.text,threadId:o.threadId},o.id,o.sourceId),S.push({id:o.id,status:"done",completedAt:Date.now(),threadId:o.threadId})}else console.error(`${_} Error: ${b.error}`),o.status="error",o.error=b.error,v.broadcast({type:"error",jobId:o.id,message:b.error||"Unknown error"},o.id,o.sourceId),S.push({id:o.id,status:"error",completedAt:Date.now(),error:b.error,threadId:o.threadId});let X=Date.now()-Z;for(;S.length>0&&(S[0].completedAt<X||S.length>O);)S.shift()});let x=ln(async(o,c)=>{if(yn(o,c),o.method==="OPTIONS"){c.writeHead(204),c.end();return}let d=new URL(o.url||"/",`http://127.0.0.1:${k}`).pathname;try{if(o.method==="POST"&&d==="/send")await Q(o,c);else if(o.method==="GET"&&d==="/events")ge(o,c);else if(o.method==="GET"&&d==="/status")ye(c);else if(o.method==="GET"&&d==="/capabilities")P(c,200,{providers:f});else if(o.method==="POST"&&d==="/mcp/install")await Se(o,c);else if(o.method==="POST"&&d==="/reply")await de(o,c);else if(o.method==="POST"&&d==="/cancel")we(o,c);else if(o.method==="POST"&&d==="/materialize")await ve(c);else if(o.method==="POST"&&d==="/model/component")await xe(o,c);else if(o.method==="DELETE"&&d==="/model/component")await Pe(o,c);else if(o.method==="PATCH"&&d==="/model/token")await W(o,c);else if(o.method==="DELETE"&&d==="/model/token")await Ee(o,c);else if(o.method==="GET"&&d==="/model"){let m=await z.loadModel();P(c,200,{model:m})}else if(o.method==="GET"&&d.startsWith("/thread/")){let m=d.slice(8);await Tt(m,c)}else o.method==="GET"&&(d==="/canvas"||d==="/canvas/")?$(o,c):o.method==="GET"&&d==="/canvas/manifest"?await Ie(c):o.method==="GET"&&d==="/canvas/app.mjs"?await kt(c):P(c,404,{error:"Not found"})}catch(m){console.error("[Bridge] Request error:",m),P(c,500,{error:m instanceof Error?m.message:"Internal error"})}});async function Q(o,c){let{screenshot:p,feedback:d,color:m,provider:y,model:_,sourceId:N,pastedImages:Y}=await Fe(o),w;try{w=JSON.parse(d)}catch(C){P(c,400,{error:"Invalid feedback JSON"});return}let b=Le().slice(0,8),X=re(a,`screenshot-${b}.png`);await je(X,p);let H={};if(Y.length>0)for(let C of Y){let oe=re(a,`pasted-${b}-${C.annotationId}-${C.index}.png`);await je(oe,C.data),H[C.annotationId]||(H[C.annotationId]=[]),H[C.annotationId].push(oe)}let G=w.annotations.map(C=>C.linkedSelector).filter(C=>!!C),V;if(G.length>0){let C=await E.findContinuationThread(G);C?(V=C.id,await E.addElementIdentifiers(V,G)):V=(await E.createThread(b,G)).id}else V=(await E.createThread(b,[])).id;let te=w.annotations.map(C=>C.id),ie=ne(q({id:b,status:"queued",screenshotPath:X,feedback:w,createdAt:Date.now(),color:m,threadId:V,annotationIds:te,provider:y==="claude"||y==="codex"?y:void 0,model:_||void 0},Object.keys(H).length>0?{imagePaths:H}:{}),{sourceId:N||void 0}),ue=w.annotations.map(C=>C.instruction||`[${C.type}]`).join("; "),I=$e(w,Object.keys(H).length>0?H:void 0);await E.appendMessage(V,{role:"human",timestamp:Date.now(),jobId:b,screenshotPath:X,annotationIds:te,feedbackSummary:ue,feedbackContext:I||void 0});let M=v.enqueue(ie);P(c,200,{jobId:b,position:M,threadId:V})}async function de(o,c){let p=o.headers["content-type"]||"",d,m,y,_,N,Y,w=[];if(p.includes("multipart/form-data")){let R=await Fe(o),D=R.feedback?JSON.parse(R.feedback):{};d=D.threadId,m=D.reply,y=D.color,_=D.provider,N=D.model,Y=D.sourceId||R.sourceId;for(let ee of R.pastedImages)w.push(ee.data)}else{let R=[];try{for(var C=ce(o),oe,K,ae;oe=!(K=await C.next()).done;oe=!1){let pe=K.value;R.push(typeof pe=="string"?Buffer.from(pe):pe)}}catch(K){ae=[K]}finally{try{oe&&(K=C.return)&&await K.call(C)}finally{if(ae)throw ae[0]}}let D=Buffer.concat(R).toString("utf-8"),ee;try{ee=JSON.parse(D)}catch(pe){P(c,400,{error:"Invalid JSON"});return}d=ee.threadId,m=ee.reply,y=ee.color,_=ee.provider,N=ee.model,Y=ee.sourceId}if(!d||!m){P(c,400,{error:"Missing threadId or reply"});return}if(!await E.getThread(d)){P(c,404,{error:"Thread not found"});return}let X=Le().slice(0,8),H=[];for(let R=0;R<w.length;R++){let D=re(a,`reply-${X}-${R}.png`);await je(D,w[R]),H.push(D)}let G="";{let R=await E.getThreadHistory(d);for(let D=R.length-1;D>=0;D--)if(R[D].screenshotPath){G=R[D].screenshotPath;break}}if(!G){P(c,400,{error:"No screenshot available"});return}await E.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:X,replyToQuestion:m,screenshotPath:G});let V=await E.getThreadHistory(d),te=[];for(let R of V)if(R.annotationIds)for(let D of R.annotationIds)te.includes(D)||te.push(D);let ie=_==="claude"||_==="codex"?_:void 0,ue=vt(G,V,ie,H.length>0?H:void 0),I={id:X,status:"queued",screenshotPath:G,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:y,threadId:d,annotationIds:te.length>0?te:void 0,provider:ie,model:N||void 0,sourceId:Y||void 0};I._replyPrompt=ue,H.length>0&&(I._replyImagePaths=H);let M=v.enqueue(I);P(c,200,{jobId:X,position:M,threadId:d})}function ge(o,c){c.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),c.write(`event: connected
128
125
  data: {"status":"connected"}
129
126
 
130
- `),!o&&s.headers.origin&&Dt(s.headers.origin)&&(o=s.headers.origin);let d=new URL(s.url||"/",`http://127.0.0.1:${O}`).searchParams.get("sourceId")||void 0,p={id:fe().slice(0,8),res:c,sourceId:d};L.add(p),s.on("close",()=>{L.delete(p)})}function ke(s){let c=v.allActive;m(s,200,{ok:!0,projectId:e,devOrigin:o,activeJob:c[0]?{id:c[0].id,status:c[0].status}:null,activeJobs:c.map(u=>({id:u.id,status:u.status})),queueDepth:v.depth,recentJobs:P})}function be(s,c){let d=new URL(s.url||"/",`http://127.0.0.1:${O}`).searchParams.get("jobId"),p=d?v.cancelJob(d):v.cancelActive();m(c,200,{cancelled:p})}async function Te(s){if(E.isRunning){m(s,200,{skipped:!0,reason:"Already running"});return}let c=await E.getUnmaterializedPatternDecisions();if(c.length===0){m(s,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}E.run().catch(()=>{}),m(s,200,{started:!0,decisionCount:c.length,decisionIds:c.map(u=>u.id)})}async function Re(s,c){let{screenshot:u,feedback:d,goal:p,pageUrl:S,viewport:R,provider:b,model:D,manifest:w,sourceId:Y}=await we(s);if(!u||!p){m(c,400,{error:"Missing screenshot or goal"});return}let z=S||"",N={width:1440,height:900};try{R&&(N=JSON.parse(R))}catch(y){}let T;if(d)try{let y=JSON.parse(d),M=xe(y);M&&(T=M)}catch(y){}let I=fe().slice(0,8),j=fe().slice(0,8),Z=oe(a,`screenshot-plan-${I}.png`);await ve(Z,u);let _=(await C.createThread(j,[])).id,se={id:I,goal:p,status:"planning",plannerJobId:j,plannerThreadId:_,workerJobIds:[],screenshotPath:Z,pageUrl:z,viewport:N,createdAt:Date.now()};X.set(I,se);let le=Rt(Z,p,z,N,w,T);await C.appendMessage(_,{role:"human",timestamp:Date.now(),jobId:j,screenshotPath:Z,feedbackSummary:`Plan: ${p}`,feedbackContext:`Goal: ${p}
131
- Page: ${z}`});let Se=b==="claude"||b==="codex"?b:J,ue={id:j,status:"queued",screenshotPath:Z,feedback:{timestamp:new Date().toISOString(),url:z,viewport:N,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:_,provider:Se,model:D||void 0,planId:I,sourceId:Y||void 0};ue._replyPrompt=le,ue._allowedTools=["Read"];let $=v.enqueue(ue);m(c,200,{planId:I,jobId:j,position:$,threadId:_})}async function $e(s,c){let u=[];try{for(var w=de(s),Y,z,N;Y=!(z=await w.next()).done;Y=!1){let T=z.value;u.push(typeof T=="string"?Buffer.from(T):T)}}catch(z){N=[z]}finally{try{Y&&(z=w.return)&&await z.call(w)}finally{if(N)throw N[0]}}let d=Buffer.concat(u).toString("utf-8"),p;try{p=JSON.parse(d)}catch(T){m(c,400,{error:"Invalid JSON"});return}let{planId:S,approvedTaskIds:R}=p;if(!S){m(c,400,{error:"Missing planId"});return}let b=X.get(S);if(!b){m(c,404,{error:"Plan not found"});return}if(!b.plan){m(c,400,{error:"Plan has no tasks"});return}let D=R?b.plan.filter(T=>R.includes(T.id)):b.plan;b.status="executing",m(c,200,{planId:S,tasks:D,status:"executing"})}async function Me(s,c){let{screenshot:u,planId:d,tasks:p,provider:S,model:R,sourceId:b}=await we(s);if(!d||!p||!u){m(c,400,{error:"Missing planId, tasks, or screenshot"});return}let D=X.get(d);if(!D){m(c,404,{error:"Plan not found"});return}if(D.status!=="executing"){m(c,400,{error:`Plan status is ${D.status}, expected executing`});return}let w;try{w=JSON.parse(p)}catch(re){m(c,400,{error:"Invalid tasks JSON"});return}let Y=fe().slice(0,8),z=oe(a,`screenshot-exec-${d}.png`);await ve(z,u);let N=S==="claude"||S==="codex"?S:J,T=Ot(z,w,D.pageUrl,D.viewport,N),I=w.map(re=>re.annotationId),j={id:Y,status:"queued",screenshotPath:z,feedback:{timestamp:new Date().toISOString(),url:D.pageUrl,viewport:D.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:N,model:R||void 0,planId:d,annotationIds:I,sourceId:b||void 0};j._replyPrompt=T,j._isPlanExecutor=!0,D.executorJobId=Y;let Z=v.enqueue(j);m(c,200,{jobId:Y,planId:d,position:Z})}async function q(s,c){let{screenshot:u,planId:d,provider:p,model:S,sourceId:R}=await we(s);if(!d){m(c,400,{error:"Missing planId"});return}let b=X.get(d);if(!b){m(c,404,{error:"Plan not found"});return}b.status="reviewing";let D=fe().slice(0,8),w=b.screenshotPath;u&&(w=oe(a,`screenshot-review-${d}.png`),await ve(w,u));let Y=(b.plan||[]).map(j=>({id:j.id,instruction:j.instruction,summary:"completed"})),z=Mt(w,b.goal,Y),N=p==="claude"||p==="codex"?p:J,T={id:D,status:"queued",screenshotPath:w,feedback:{timestamp:new Date().toISOString(),url:b.pageUrl,viewport:b.viewport,scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),provider:N,model:S||void 0,planId:d,sourceId:R||void 0};T._replyPrompt=z,T._isReview=!0,T._allowedTools=["Read"];let I=v.enqueue(T);m(c,200,{jobId:D,planId:d,position:I})}function Ne(s,c){let u=X.get(s);if(!u){m(c,404,{error:"Plan not found"});return}m(c,200,u)}async function B(s,c){var z,N;let u=[];try{for(var b=de(s),D,w,Y;D=!(w=await b.next()).done;D=!1){let T=w.value;u.push(typeof T=="string"?Buffer.from(T):T)}}catch(w){Y=[w]}finally{try{D&&(w=b.return)&&await w.call(b)}finally{if(Y)throw Y[0]}}let d;if(u.length>0)try{d=JSON.parse(Buffer.concat(u).toString("utf-8")).serverUrl}catch(T){}let p=[];(z=g.claude)!=null&&z.available&&g.claude.mcp&&!g.claude.mcp.found&&p.push(await wt(d)),(N=g.codex)!=null&&N.available&&g.codex.mcp&&!g.codex.mcp.found&&p.push(await vt(d));let[S,R]=await Promise.all([He(t),Ge(t)]);g.claude&&(g.claude.mcp=S),g.codex&&(g.codex.mcp=R),m(c,200,{results:p,capabilities:{providers:g}})}async function Oe(s,c){let u=[];try{for(var S=de(s),R,b,D;R=!(b=await S.next()).done;R=!1){let w=b.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(b){D=[b]}finally{try{R&&(b=S.return)&&await b.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){m(c,400,{error:"Missing or invalid name"});return}let p=await E.addComponent(d.name);m(c,200,p)}async function Jt(s,c){let u=[];try{for(var S=de(s),R,b,D;R=!(b=await S.next()).done;R=!1){let w=b.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(b){D=[b]}finally{try{R&&(b=S.return)&&await b.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){m(c,400,{error:"Missing or invalid name"});return}let p=await E.removeComponent(d.name);m(c,200,p)}async function Nt(s,c){let u=[];try{for(var S=de(s),R,b,D;R=!(b=await S.next()).done;R=!1){let w=b.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(b){D=[b]}finally{try{R&&(b=S.return)&&await b.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){m(c,400,{error:"Missing or invalid path/value"});return}let p=await E.updateToken(d.path,d.value);m(c,200,p)}async function Bt(s,c){let u=[];try{for(var S=de(s),R,b,D;R=!(b=await S.next()).done;R=!1){let w=b.value;u.push(typeof w=="string"?Buffer.from(w):w)}}catch(b){D=[b]}finally{try{R&&(b=S.return)&&await b.call(S)}finally{if(D)throw D[0]}}let d;try{d=JSON.parse(Buffer.concat(u).toString("utf-8"))}catch(w){m(c,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){m(c,400,{error:"Missing or invalid path"});return}let p=await E.removeToken(d.path);m(c,200,p)}function Ut(s,c){let u=o!=null?o:"http://localhost:3000";if(!o){let p=s.headers.referer||s.headers.origin;if(p)try{let S=new URL(typeof p=="string"?p:p[0]||"");(S.hostname==="localhost"||S.hostname==="127.0.0.1")&&(u=S.origin)}catch(S){}}let d=rt(O,u);c.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),c.end(d)}async function Ft(s){let c=Date.now();if(x&&c<x.expires){m(s,200,x.data);return}try{let{scanForComponents:u}=await import("./react-scanner-5NIJ6ZPL.mjs"),{generateRenderFiles:d}=await import("./render-generator-ZNV3RDU7.mjs"),p=await u(t);x={data:p,expires:c+5e3},d(p,t,G).then(S=>{G=S}).catch(S=>console.warn("[Bridge] Render generation failed:",S)),m(s,200,p)}catch(u){console.error("[Bridge] Scanner error:",u),m(s,500,{error:"Failed to scan components"})}}async function Lt(s){let c=[oe(t,"node_modules","@popmelt.com","core","dist","canvas.mjs"),oe(t,"packages","popmelt","dist","canvas.mjs")];try{let u=Rn(Jn.url);c.unshift(oe(Tn(u),"canvas.mjs"))}catch(u){}for(let u of c)try{let d=await Sn(u,"utf-8");s.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),s.end(d);return}catch(d){}console.error("[Bridge] Canvas bundle not found in:",c),m(s,404,{error:"Canvas bundle not found"})}async function zt(s,c){let u=await C.getThread(s);if(!u){m(c,404,{error:"Thread not found"});return}let d=u.messages.map(R=>{var b=R,{screenshotPath:p}=b,S=ot(b,["screenshotPath"]);return S});m(c,200,{id:u.id,createdAt:u.createdAt,messages:d})}let We=9,Ye=!1;for(let s=n;s<n+We;s++)try{await _n(k,s),O=s,Ye=!0,console.log(`[\u22B9 is watching :${O}]`);break}catch(c){if(c.code==="EADDRINUSE"){let u=await Dn(s);if(u&&u.projectId===e)return console.log(`[\u22B9 already watching :${s}]`),{port:s,projectId:e,close:async()=>{}};continue}throw c}if(!Ye)throw new Error(`[Bridge] All ports ${n}\u2013${n+We-1} in use`);let Ht=setInterval(()=>{At(a).catch(()=>{})},On);return{port:O,projectId:e,close:async()=>{clearInterval(Ht),await v.destroyAsync();for(let s of L)try{s.res.end()}catch(c){}return L.clear(),new Promise(s=>{k.close(()=>s())})}}}async function At(r){try{let n=await In(r),t=Date.now();for(let e of n){let o=oe(r,e);try{let a=await xn(o);t-a.mtimeMs>Cn&&await Pn(o)}catch(a){}}}catch(n){}}async function Ns(r){if(process.env.NODE_ENV==="production"&&!(r!=null&&r.force))throw new Error("[Bridge] Refusing to start in production. Pass { force: true } to override.");let n=await _t(r);return process.env.POPMELT_BRIDGE_PORT=String(n.port),n}export{Ns as startPopmelt};
127
+ `),!s&&o.headers.origin&&It(o.headers.origin)&&(s=o.headers.origin);let d=new URL(o.url||"/",`http://127.0.0.1:${k}`).searchParams.get("sourceId")||void 0,m={id:Le().slice(0,8),res:c,sourceId:d};B.add(m),o.on("close",()=>{B.delete(m)})}function ye(o){let c=v.allActive;P(o,200,{ok:!0,projectId:n,devOrigin:s,activeJob:c[0]?{id:c[0].id,status:c[0].status}:null,activeJobs:c.map(p=>({id:p.id,status:p.status})),queueDepth:v.depth,recentJobs:S})}async function we(o,c){let d=new URL(o.url||"/",`http://127.0.0.1:${k}`).searchParams.get("jobId"),y=(d?v.allActive.filter(N=>N.id===d):v.allActive).map(N=>N.threadId).filter(Boolean),_=d?v.cancelJob(d):v.cancelActive();for(let N of y)await E.appendMessage(N,{role:"assistant",timestamp:Date.now(),jobId:d||"",cancelled:!0});P(c,200,{cancelled:_})}async function ve(o){if(z.isRunning){P(o,200,{skipped:!0,reason:"Already running"});return}let c=await z.getUnmaterializedPatternDecisions();if(c.length===0){P(o,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}z.run().catch(()=>{}),P(o,200,{started:!0,decisionCount:c.length,decisionIds:c.map(p=>p.id)})}async function Se(o,c){var X,H;let p=[];try{for(var N=ce(o),Y,w,b;Y=!(w=await N.next()).done;Y=!1){let G=w.value;p.push(typeof G=="string"?Buffer.from(G):G)}}catch(w){b=[w]}finally{try{Y&&(w=N.return)&&await w.call(N)}finally{if(b)throw b[0]}}let d;if(p.length>0)try{d=JSON.parse(Buffer.concat(p).toString("utf-8")).serverUrl}catch(G){}let m=[];(X=f.claude)!=null&&X.available&&f.claude.mcp&&!f.claude.mcp.found&&m.push(await mt(d)),(H=f.codex)!=null&&H.available&&f.codex.mcp&&!f.codex.mcp.found&&m.push(await gt(d));let[y,_]=await Promise.all([Je(e),Be(e)]);f.claude&&(f.claude.mcp=y),f.codex&&(f.codex.mcp=_),P(c,200,{results:m,capabilities:{providers:f}})}async function xe(o,c){let p=[];try{for(var y=ce(o),_,N,Y;_=!(N=await y.next()).done;_=!1){let w=N.value;p.push(typeof w=="string"?Buffer.from(w):w)}}catch(N){Y=[N]}finally{try{_&&(N=y.return)&&await N.call(y)}finally{if(Y)throw Y[0]}}let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch(w){P(c,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){P(c,400,{error:"Missing or invalid name"});return}let m=await z.addComponent(d.name);P(c,200,m)}async function Pe(o,c){let p=[];try{for(var y=ce(o),_,N,Y;_=!(N=await y.next()).done;_=!1){let w=N.value;p.push(typeof w=="string"?Buffer.from(w):w)}}catch(N){Y=[N]}finally{try{_&&(N=y.return)&&await N.call(y)}finally{if(Y)throw Y[0]}}let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch(w){P(c,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){P(c,400,{error:"Missing or invalid name"});return}let m=await z.removeComponent(d.name);P(c,200,m)}async function W(o,c){let p=[];try{for(var y=ce(o),_,N,Y;_=!(N=await y.next()).done;_=!1){let w=N.value;p.push(typeof w=="string"?Buffer.from(w):w)}}catch(N){Y=[N]}finally{try{_&&(N=y.return)&&await N.call(y)}finally{if(Y)throw Y[0]}}let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch(w){P(c,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){P(c,400,{error:"Missing or invalid path/value"});return}let m=await z.updateToken(d.path,d.value);P(c,200,m)}async function Ee(o,c){let p=[];try{for(var y=ce(o),_,N,Y;_=!(N=await y.next()).done;_=!1){let w=N.value;p.push(typeof w=="string"?Buffer.from(w):w)}}catch(N){Y=[N]}finally{try{_&&(N=y.return)&&await N.call(y)}finally{if(Y)throw Y[0]}}let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch(w){P(c,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){P(c,400,{error:"Missing or invalid path"});return}let m=await z.removeToken(d.path);P(c,200,m)}function $(o,c){let p=s!=null?s:"http://localhost:3000";if(!s){let m=o.headers.referer||o.headers.origin;if(m)try{let y=new URL(typeof m=="string"?m:m[0]||"");(y.hostname==="localhost"||y.hostname==="127.0.0.1")&&(p=y.origin)}catch(y){}}let d=nt(k,p);c.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),c.end(d)}async function Ie(o){let c=Date.now();if(g&&c<g.expires){P(o,200,g.data);return}try{let{scanForComponents:p}=await import("./react-scanner-5NIJ6ZPL.mjs"),{generateRenderFiles:d}=await import("./render-generator-ZNV3RDU7.mjs"),m=await p(e);g={data:m,expires:c+5e3},d(m,e,L).then(y=>{L=y}).catch(y=>console.warn("[Bridge] Render generation failed:",y)),P(o,200,m)}catch(p){console.error("[Bridge] Scanner error:",p),P(o,500,{error:"Failed to scan components"})}}async function kt(o){let c=[re(e,"node_modules","@popmelt.com","core","dist","canvas.mjs"),re(e,"packages","popmelt","dist","canvas.mjs")];try{let p=pn(xn.url);c.unshift(re(un(p),"canvas.mjs"))}catch(p){}for(let p of c)try{let d=await on(p,"utf-8");o.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),o.end(d);return}catch(d){}console.error("[Bridge] Canvas bundle not found in:",c),P(o,404,{error:"Canvas bundle not found"})}async function Tt(o,c){let p=await E.getThread(o);if(!p){P(c,404,{error:"Thread not found"});return}let d=p.messages.map(_=>{var N=_,{screenshotPath:m}=N,y=tt(N,["screenshotPath"]);return y});P(c,200,{id:p.id,createdAt:p.createdAt,messages:d})}let He=9,Ge=!1;for(let o=t;o<t+He;o++)try{await Sn(x,o),k=o,Ge=!0,console.log(`[\u22B9 is watching :${k}]`);break}catch(c){if(c.code==="EADDRINUSE"){let p=await vn(o);if(p&&p.projectId===n)return console.log(`[\u22B9 already watching :${o}]`),{port:o,projectId:n,close:async()=>{}};continue}throw c}if(!Ge)throw new Error(`[Bridge] All ports ${t}\u2013${t+He-1} in use`);for(let[o,c]of Object.entries(f))!c.available||!c.path||F(o,c.path,e).then(p=>{if(p)console.log(`[Bridge] ${o} warmed up`);else{console.warn(`[Bridge] ${o} warm-up failed \u2014 marking unavailable`),c.available=!1,c.path=null;for(let d of B)ze(d,{type:"capabilities_changed",data:{}})}});let Rt=setInterval(()=>{Pt(a).catch(()=>{})},mn);return{port:k,projectId:n,close:async()=>{clearInterval(Rt),await v.destroyAsync();for(let o of B)try{o.res.end()}catch(c){}return B.clear(),new Promise(o=>{x.close(()=>o())})}}}async function Pt(r){try{let t=await rn(r),e=Date.now();for(let n of t){let s=re(r,n);try{let a=await an(s);e-a.mtimeMs>gn&&await cn(s)}catch(a){}}}catch(t){}}async function Ps(r){if(process.env.NODE_ENV==="production"&&!(r!=null&&r.force))throw new Error("[Bridge] Refusing to start in production. Pass { force: true } to override.");let t=await bt(r);return process.env.POPMELT_BRIDGE_PORT=String(t.port),t}export{Ps as startPopmelt};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@popmelt.com/core",
3
- "version": "0.5.10",
3
+ "version": "0.5.11",
4
4
  "description": "The design collaboration layer for AI coding agents",
5
5
  "license": "PolyForm-Shield-1.0.0",
6
6
  "author": "Popmelt <hello@popmelt.com> (https://popmelt.com)",