@popmelt.com/core 0.7.0 → 0.7.1

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{spawn as qs}from"child_process";import{createHash as Xs}from"crypto";import{dirname as Ys,join as Qs}from"path";import{fileURLToPath as Ks}from"url";import{execFileSync as Ms,spawn as Xt}from"child_process";import{createHash as Rs,randomUUID as je}from"crypto";import{mkdir as Cs,readFile as Yt,readdir as $s,stat as Os,unlink as _s,writeFile as Le}from"fs/promises";import{createServer as As}from"http";import{tmpdir as Ds}from"os";import{basename as Ns,dirname as Fs,join as ue}from"path";import{fileURLToPath as Bs}from"url";function lt(s,t){return`<!DOCTYPE html>
1
+ import{spawn as No}from"child_process";import{createHash as Jo}from"crypto";import{dirname as Bo,join as Fo}from"path";import{fileURLToPath as jo}from"url";import{execFileSync as et,spawn as yn}from"child_process";import{createHash as kn,randomUUID as we}from"crypto";import{mkdir as wo,readFile as wn,readdir as So,stat as bo,unlink as xo,writeFile as Oe}from"fs/promises";import{createServer as ko}from"http";import{tmpdir as Po}from"os";import{basename as Sn,dirname as Eo,join as he}from"path";import{fileURLToPath as Ro}from"url";function St(s,t){return`<!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
4
  <meta charset="utf-8">
@@ -25,21 +25,145 @@ import{spawn as qs}from"child_process";import{createHash as Xs}from"crypto";impo
25
25
  });
26
26
  </script>
27
27
  </body>
28
- </html>`}import{spawn as yn}from"child_process";import{createInterface as wn}from"readline";var vn=new Set([".md",".txt",".json",".ts",".tsx",".js",".jsx",".css",".scss",".html",".xml",".yaml",".yml",".toml",".ini",".cfg",".conf",".sh",".bash",".zsh",".py",".rb",".go",".rs",".java",".c",".h",".cpp",".hpp",".swift",".kt",".sql",".graphql",".svg",".env",".gitignore",".prettierrc",".eslintrc"]),dt=1e5;function Sn(s){let t=s.input?.file_path||s.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!vn.has(n))return;let e;if(s.name==="Write"&&typeof s.input?.content=="string"?e=s.input.content:s.name==="Edit"&&typeof s.input?.new_string=="string"&&(e=s.input.new_string),!!e)return e.length>dt?e.slice(0,dt)+`
29
- \u2026[truncated]`:e}function Me(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:a=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:h,model:v,timeoutMs:y=3e5,onEvent:B}=t,N=[];h?N.push("--resume",h,"-p",n):N.push("-p",n),N.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),v&&N.push("--model",v);for(let m of a)N.push("--allowedTools",m);let R=yn(c,N,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),F=new Promise(m=>{let W,H=[],Y=[],q=[],G=!1,I="",z,b,_=!1,u=setTimeout(()=>{_=!0,R.kill("SIGTERM"),setTimeout(()=>{try{R.kill("SIGKILL")}catch{}},5e3)},y),j=wn({input:R.stdout}),E=new Set;j.on("line",C=>{if(C.trim())try{let S=JSON.parse(C);S.session_id&&!W&&(W=S.session_id);let X=S.type??(S.event?.type?`event.${S.event.type}`:"unknown");if(E.add(X),S.type==="result"&&S.result&&H.length===0){let T=typeof S.result=="string"?S.result:"";T&&(H.push(T),B?.({type:"delta",jobId:s,text:T},s))}if(S.type==="assistant"&&Array.isArray(S.message?.content)){typeof S.error=="string"&&(z=S.error);for(let T of S.message.content){if(T.type==="text"&&T.text&&(H.push(T.text),B?.({type:"delta",jobId:s,text:T.text},s)),T.type==="tool_use"&&T.name){let Z=T.input?.file_path||T.input?.path||void 0,ae=Sn(T);if(B?.({type:"tool_use",jobId:s,tool:T.name,...Z?{file:Z}:{},...ae?{content:ae}:{}},s),T.name==="Edit"&&T.input?.file_path){let oe={tool:"Edit",file_path:T.input.file_path,old_string:T.input.old_string,new_string:T.input.new_string,replace_all:T.input.replace_all};Y.push(oe),q.push({sequence:q.length,occurredAt:Date.now(),provider:"claude",source:"claude_tool_use",tool:"Edit",operation:"modify",filePath:oe.file_path,oldString:typeof oe.old_string=="string"?oe.old_string:void 0,newString:typeof oe.new_string=="string"?oe.new_string:void 0,replaceAll:typeof oe.replace_all=="boolean"?oe.replace_all:void 0})}else T.name==="Write"&&T.input?.file_path&&(Y.push({tool:"Write",file_path:T.input.file_path,content:T.input.content}),q.push({sequence:q.length,occurredAt:Date.now(),provider:"claude",source:"claude_tool_use",tool:"Write",operation:"create",filePath:T.input.file_path,content:typeof T.input.content=="string"?T.input.content:void 0}))}T.type==="thinking"&&T.thinking&&B?.({type:"thinking",jobId:s,text:T.thinking},s)}}S.type==="result"&&S.is_error===!0&&(G=!0,typeof S.error=="string"&&(z=S.error),typeof S.result=="string"&&(b=S.result)),S.type==="user"&&S.tool_use_result?.file?.filePath&&B?.({type:"tool_use",jobId:s,tool:"Read",file:S.tool_use_result.file.filePath},s)}catch{}});let w=[];R.stderr?.on("data",C=>{w.push(C.toString())}),R.on("close",C=>{if(clearTimeout(u),j.close(),H.length===0&&E.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...E].join(", ")}`),_)G=!0,I=`Timed out after ${Math.round(y/6e4)} minutes`;else if(C!==0&&C!==null){G=!0;let X=w.join("").trim(),T=H.join("").trim(),Z=H.length===0&&E.size>0?` (no text captured, event types: ${[...E].join(", ")})`:"";I=X||b||T||`Claude process exited with code ${C}${Z}`}G&&!I&&b&&(I=b);let S=xn(I,z);m({sessionId:W,text:H.join(""),success:!G,error:G?S.message:void 0,errorCode:G?S.code:void 0,recoverableError:G?S.recoverable:void 0,fileEdits:Y.length>0?Y:void 0,editEvents:q.length>0?q:void 0})}),R.on("error",C=>{clearTimeout(u),G=!0,I=C.message,m({sessionId:W,text:H.join(""),success:!1,error:I,errorCode:"claude_spawn_error",recoverableError:!1,fileEdits:Y.length>0?Y:void 0,editEvents:q.length>0?q:void 0})})});return{process:R,result:F}}function xn(s,t){let n=s.trim()||"Claude CLI failed",e=`${t??""} ${n}`.toLowerCase();return e.includes("authentication_failed")||e.includes("not logged in")||e.includes("please run /login")||e.includes("api key")?{message:n,code:"claude_auth_required",recoverable:!0}:e.includes("model")&&(e.includes("not found")||e.includes("unknown")||e.includes("unavailable"))?{message:n,code:"claude_model_unavailable",recoverable:!0}:{message:n,code:t||"claude_error",recoverable:!1}}import{spawn as bn,spawnSync as gt}from"child_process";import{readFileSync as Pn,statSync as kn}from"fs";import{basename as Tn,extname as En,isAbsolute as In,relative as yt,resolve as Mn}from"path";import{createInterface as Rn}from"readline";function ut(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}var pt=new Set([".md",".txt",".json",".ts",".tsx",".js",".jsx",".css",".scss",".html",".xml",".yaml",".yml",".toml",".ini",".cfg",".conf",".sh",".bash",".zsh",".py",".rb",".go",".rs",".java",".c",".h",".cpp",".hpp",".swift",".kt",".sql",".graphql",".svg",".env",".gitignore",".prettierrc",".eslintrc"]),He=2e5,Cn=1e5,wt=2e5;function vt(s){let t=Tn(s).toLowerCase(),n=En(s).toLowerCase();return pt.has(n)||pt.has(t)}function St(s,t){let n=In(t)?t:Mn(s,t),e=yt(s,n);return e===""||e.startsWith("..")?null:n}function Re(s,t){if(!vt(t))return;let n=St(s,t);if(n)try{let e=kn(n);return e.isFile()?e.size>He?{exists:!0,truncated:!0}:{exists:!0,content:Pn(n,"utf8")}:{exists:!1}}catch{return{exists:!1}}}function $n(s,t){for(let n of t){let e=s[n];if(typeof e=="string")return e}}function On(s){let t=s?.toLowerCase()??"";return t.includes("add")||t.includes("create")?"create":t.includes("delete")||t.includes("remove")?"delete":t.includes("rename")||t.includes("move")?"rename":t.includes("modify")||t.includes("update")||t.includes("edit")?"modify":"unknown"}function ft(s){return $n(s,["patch","diff","unifiedDiff","unified_diff"])}function Ge(s,t){return s.length<=t?{text:s}:{text:s.slice(0,t),truncated:!0}}function ht(s,t){let n=gt("git",t,{cwd:s,encoding:"utf8",timeout:5e3,maxBuffer:wt+65536});return n.status!==0?null:n.stdout??""}function _n(s){let t=new Set;for(let n of s.split(`
30
- `)){if(n.length<4)continue;let e=n.slice(3).trim();if(!e)continue;e.startsWith('"')&&e.endsWith('"')&&(e=e.slice(1,-1));let o=e.split(" -> ");for(let i of o)i&&t.add(i)}return[...t]}function mt(s){let t=ht(s,["status","--porcelain=v1","-uall"]);if(t===null)return;let n=ht(s,["diff","--no-ext-diff","HEAD","--"])??"",e=Ge(n,wt);return{status:t,changedFiles:_n(t),...e.text?{diff:e.text}:{},...e.truncated?{diffTruncated:!0}:{}}}function An(s,t){return!s||!t?!1:s.status!==t.status||(s.diff??"")!==(t.diff??"")}function Dn(s,t){if(!vt(t))return;let n=St(s,t);if(!n)return;let e=yt(s,n);if(e===""||e.startsWith(".."))return;let o=gt("git",["show",`HEAD:${e}`],{cwd:s,encoding:"utf8",timeout:5e3,maxBuffer:He+65536});if(o.status!==0)return{exists:!1};let i=o.stdout??"",a=Ge(i,He);return{exists:!0,content:a.text,...a.truncated?{truncated:!0}:{}}}function Nn(s,t,n){return t.map(e=>{let o=n.has(e)?n.get(e):Dn(s,e),i=Re(s,e);return{filePath:e,...o?{before:o}:{},...i?{after:i}:{}}})}function Ce(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:a,onEvent:c,sandboxMode:h="workspace-write"}=t,v=[];i?(v.push("exec","resume","--skip-git-repo-check","--json","-c",`sandbox_mode="${h}"`),a&&v.push("-m",a),v.push(i),v.push(n),o&&v.push("--image",o)):(v.push("exec","--skip-git-repo-check","--json","--sandbox",h),a&&v.push("-m",a),v.push(n),o&&v.push("--image",o));let y=bn("codex",v,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),B=new Promise(N=>{let R,F=[],m=[],W=[],H=new Map,Y=new Map,q=!1,G="",I=Rn({input:y.stdout}),z=new Set;I.on("line",_=>{if(_.trim())try{let u=JSON.parse(_),j=u.type??"unknown";if(z.add(j),j==="thread.started"&&u.thread_id&&!R&&(R=u.thread_id),(j==="item.agentMessage.delta"||j==="item/agentMessage/delta")&&u.delta?.text&&(F.push(u.delta.text),c?.({type:"delta",jobId:s,text:u.delta.text},s)),(j==="item.reasoning.delta"||j==="item/reasoning/delta")&&u.delta?.text&&c?.({type:"thinking",jobId:s,text:u.delta.text},s),(j==="item.started"||j==="item/started")&&u.item){let E=u.item.type;if(E==="command_execution"){let w=u.item.command,C=w?ut(w):void 0,S=mt(e),X=new Map;for(let Z of S?.changedFiles??[])X.set(Z,Re(e,Z));typeof u.item.id=="string"&&Y.set(u.item.id,{...w?{rawCommand:w}:{},...C?{command:C}:{},...S?{beforeWorktree:S}:{},beforeSnapshots:X});let T=C?`Bash: ${C.split(`
31
- `)[0].slice(0,80)}`:"Bash";m.push(T),c?.({type:"tool_use",jobId:s,tool:"Bash",...C?{content:C}:{}},s)}else if(E==="file_change"){let w=u.item.filename||u.item.path;w&&!H.has(w)&&H.set(w,Re(e,w)),m.push(w?`Edit ${w.split("/").pop()}`:"Edit"),c?.({type:"tool_use",jobId:s,tool:"Edit",...w?{file:w}:{}},s)}else if(E==="file_read"){let w=u.item.filename||u.item.path;m.push(w?`Read ${w.split("/").pop()}`:"Read"),c?.({type:"tool_use",jobId:s,tool:"Read",...w?{file:w}:{}},s)}else if(E==="web_search")m.push("WebSearch"),c?.({type:"tool_use",jobId:s,tool:"WebSearch"},s);else if(E==="mcp_tool_call"){let w=u.item.tool_name||u.item.name||"MCP";m.push(w),c?.({type:"tool_use",jobId:s,tool:w},s)}}if((j==="item.completed"||j==="item/completed")&&u.item){if(u.item.type==="agent_message"){let E=u.item.text;typeof E=="string"&&E&&(F.push(E),c?.({type:"delta",jobId:s,text:E},s))}else if(u.item.type==="reasoning"){let E=u.item.text;typeof E=="string"&&E&&c?.({type:"thinking",jobId:s,text:E},s)}else if(u.item.type==="file_change"&&Array.isArray(u.item.changes))for(let E of u.item.changes){let w=E.path||E.filename,C=E.kind==="add"?"Write":"Edit";if(w){m.push(`${C} ${w.split("/").pop()}`),c?.({type:"tool_use",jobId:s,tool:C,file:w},s);let S=E,X=typeof S.kind=="string"?S.kind:void 0,T=On(X),Z=H.get(w),ae=Re(e,w);W.push({sequence:W.length,occurredAt:Date.now(),provider:"codex",source:"codex_file_change",tool:"FileChange",operation:T,filePath:w,...Z?{before:Z}:{},...ae?{after:ae}:{},...ft(S)?{patch:ft(S)}:{},...X?{rawKind:X}:{}}),H.set(w,ae)}}else if(u.item.type==="command_execution"){let E=typeof u.item.id=="string"?u.item.id:void 0,w=E?Y.get(E):void 0,C=typeof u.item.command=="string"?u.item.command:w?.rawCommand,S=C?ut(C):w?.command,X=mt(e);if(An(w?.beforeWorktree,X)){let T=[...new Set([...w?.beforeWorktree?.changedFiles??[],...X?.changedFiles??[]])],Z=typeof u.item.aggregated_output=="string"?Ge(u.item.aggregated_output,Cn):void 0,ae=typeof u.item.exit_code=="number"?u.item.exit_code:u.item.exit_code===null?null:void 0,oe=typeof u.item.status=="string"?u.item.status:void 0;W.push({sequence:W.length,occurredAt:Date.now(),provider:"codex",source:"codex_command_execution",tool:"Bash",operation:"command",...C?{rawCommand:C}:{},...S?{command:S}:{},...ae!==void 0?{exitCode:ae}:{},...oe?{status:oe}:{},...Z?{output:Z.text}:{},...Z?.truncated?{outputTruncated:!0}:{},...w?.beforeWorktree?{beforeWorktree:w.beforeWorktree}:{},...X?{afterWorktree:X}:{},...T.length>0?{touchedFiles:Nn(e,T,w?.beforeSnapshots??new Map)}:{}})}E&&Y.delete(E)}}j==="turn.failed"&&(q=!0,G=u.error?.message||u.message||"Turn failed")}catch{}});let b=[];y.stderr?.on("data",_=>{b.push(_.toString())}),y.on("close",_=>{I.close(),F.length===0&&z.size>0&&console.warn(`[Codex:${s}] No text captured. Event types seen: ${[...z].join(", ")}`),_!==0&&_!==null&&(q=!0,G=b.join("")||`Codex process exited with code ${_}`),N({sessionId:R,text:F.join(""),success:!q,error:q?G:void 0,editEvents:W.length>0?W:void 0,toolsUsed:m.length>0?m:void 0})}),y.on("error",_=>{q=!0,G=_.message,N({sessionId:R,text:F.join(""),success:!1,error:G,editEvents:W.length>0?W:void 0,toolsUsed:m.length>0?m:void 0})})});return{process:y,result:B}}import{spawn as Fn}from"child_process";import{createInterface as Bn}from"readline";function xt(s,t){let n=s.trim()||"Copilot CLI failed",e=n.toLowerCase();return e.includes("no authentication information")||e.includes("not authenticated")||e.includes("/login")?{code:"copilot_auth_required",recoverable:!0,message:"Copilot is not authenticated. Run `copilot login`, set `COPILOT_GITHUB_TOKEN`, or refresh `gh auth login`."}:e.includes("model")&&(e.includes("not available")||e.includes("not found")||e.includes("disabled")||e.includes("policy"))?{code:"copilot_model_unavailable",recoverable:!0,message:t?`Copilot rejected ${t}. Choose another model or ask your administrator to enable it.`:n}:e.includes("mcp")&&(e.includes("blocked")||e.includes("allowlist"))?{code:"copilot_mcp_blocked",recoverable:!0,message:"Copilot blocked the Popmelt MCP server. Ask your administrator to add it to the enterprise allowlist."}:e.includes("permission")||e.includes("denied")||e.includes("not allowed")?{code:"copilot_permission_denied",recoverable:!0,message:n}:{code:"copilot_error",recoverable:!1,message:n}}function bt(s,t){let{prompt:n,projectRoot:e,resumeSessionId:o,model:i,timeoutMs:a=3e5,onEvent:c,copilotPath:h="copilot"}=t,v=["--no-color","--no-auto-update","--output-format","json","--stream","on","--available-tools=read,write,shell","--allow-all-tools","--deny-tool=shell(git push)","--deny-tool=shell(rm)","--deny-tool=shell(sudo)","--no-ask-user"];o&&v.push(`--resume=${o}`),i&&v.push("--model",i),v.push("-p",n);let y=Fn(h,v,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),B=new Promise(N=>{let R,F=[],m=[],W=!1,H="",Y=!1,q=setTimeout(()=>{Y=!0,y.kill("SIGTERM"),setTimeout(()=>{try{y.kill("SIGKILL")}catch{}},5e3)},a),G=Bn({input:y.stdout}),I=new Set,z=[];G.on("line",_=>{if(_.trim())try{let u=JSON.parse(_),j=Se(u);I.add(j),z.length<5&&Un(j)&&z.push(_.slice(0,800));let E=ve(u,["sessionId","session_id","id"],["session","conversation","thread"]);E&&!R&&(R=E);let w=Jn(u,F.length===0);w&&(F.push(w),c?.({type:"delta",jobId:s,text:w},s));let C=jn(u);C&&c?.({type:"thinking",jobId:s,text:C},s);let S=Ln(u);S&&(m.push(S.label),c?.({type:"tool_use",jobId:s,tool:S.tool,...S.file?{file:S.file}:{},...S.content?{content:S.content}:{}},s));let X=zn(u);X&&(W=!0,H=X)}catch{}});let b=[];y.stderr?.on("data",_=>{b.push(_.toString())}),y.on("close",_=>{clearTimeout(q),G.close(),F.length===0&&I.size>0&&(console.warn(`[Copilot:${s}] No text captured. Event types seen: ${[...I].join(", ")}`),z.length>0&&console.warn(`[Copilot:${s}] Sample text-bearing candidates: ${z.join(`
32
- `)}`)),Y?(W=!0,H=`Timed out after ${Math.round(a/6e4)} minutes`):_!==0&&_!==null&&(W=!0,H=b.join("").trim()||H||`Copilot process exited with code ${_}`);let u=W?xt(H,i):null;N({sessionId:R,text:F.join(""),success:!W,error:u?.message,errorCode:u?.code,recoverableError:u?.recoverable,provider:"copilot",toolsUsed:m.length>0?m:void 0})}),y.on("error",_=>{clearTimeout(q);let u=xt(_.message,i);N({sessionId:R,text:F.join(""),success:!1,error:u.message,errorCode:u.code,recoverableError:u.recoverable,provider:"copilot",toolsUsed:m.length>0?m:void 0})})});return{process:y,result:B}}function Se(s){if(typeof s.type=="string")return s.type;let t=s.update;return t&&typeof t.sessionUpdate=="string"?t.sessionUpdate:typeof s.event=="string"?s.event:"unknown"}function Jn(s,t=!1){let n=Se(s).toLowerCase(),e=s.update,o=e?.content;if(e?.sessionUpdate==="agent_message_chunk"&&o?.type==="text"&&typeof o.text=="string")return o.text;if(n==="assistant.message_delta"||n.includes("agent_message.delta"))return ie(s.delta)??ie(s.content)??ie(s.message);if(n==="assistant.message"&&t)return ie(s.message)??ie(s.content)??ie(s);if(n==="result"&&t)return ie(s.result)??ie(s.output)??ie(s.message)??ie(s);let i=s.content;if(i?.type==="text"&&typeof i.text=="string")return i.text;if(typeof s.text=="string"&&qe(s))return s.text;if(typeof s.message=="string"&&qe(s))return s.message;if(typeof s.delta=="string"&&qe(s))return s.delta}function ie(s){if(typeof s=="string")return s;if(!s)return;if(Array.isArray(s)){let n=s.map(ie).filter(e=>!!e);return n.length>0?n.join(""):void 0}if(typeof s!="object")return;let t=s;if(typeof t.text=="string")return t.text;if(typeof t.content=="string")return t.content;for(let n of["delta","content","message","messages","parts","items","output","result","data","value","body","markdown","text_delta","textDelta"]){let e=ie(t[n]);if(e)return e}}function Un(s){let t=s.toLowerCase();return t.includes("assistant.message")||t==="result"}function jn(s){if(typeof s.thinking=="string")return s.thinking;if(typeof s.reasoning=="string")return s.reasoning;let t=s.update;if(t?.sessionUpdate==="reasoning_chunk"){let n=t.content;if(typeof n?.text=="string")return n.text}}function Ln(s){let t=ve(s,["tool","toolName","tool_name","name"]),n=ve(s,["command","cmd"]),e=ve(s,["file","path","filename","filePath"]),o=Se(s);if(n||o.includes("shell"))return{tool:"Bash",label:n?`Bash: ${n.split(`
33
- `)[0].slice(0,80)}`:"Bash",...n?{content:n}:{}};if(t)return{tool:t,label:e?`${t} ${e.split("/").pop()}`:t,...e?{file:e}:{}};if(e&&(o.includes("file")||o.includes("write")||o.includes("edit"))){let i=o.includes("write")?"Write":o.includes("read")?"Read":"Edit";return{tool:i,label:`${i} ${e.split("/").pop()}`,file:e}}}function zn(s){if(typeof s.error=="string")return s.error;if(s.error&&typeof s.error=="object"){let t=s.error;if(typeof t.message=="string")return t.message}if(typeof s.message=="string"&&Se(s).toLowerCase().includes("error"))return s.message}function ve(s,t,n=[]){for(let e of t){let o=s[e];if(typeof o=="string")return o}for(let[e,o]of Object.entries(s)){if(!o||typeof o!="object"||n.length>0&&!n.some(a=>e.toLowerCase().includes(a)))continue;let i=ve(o,t);if(i)return i}}function qe(s){let t=Se(s).toLowerCase();return(typeof s.role=="string"?s.role.toLowerCase():"")==="assistant"||t.includes("assistant")||t.includes("agent_message")}import{execFile as Wn}from"child_process";import{copyFile as Pt,mkdir as kt,readdir as Hn,readFile as Gn,writeFile as qn}from"fs/promises";import{join as ge}from"path";var $e=class{constructor(t){this.projectRoot=t;let n=ge(t,".popmelt");this.decisionsDir=ge(n,"decisions"),this.screenshotsDir=ge(n,"screenshots")}async persist(t,n,e){try{await kt(this.decisionsDir,{recursive:!0}),await kt(this.screenshotsDir,{recursive:!0});try{await Pt(n,ge(this.screenshotsDir,`s-${t.id}.webp`))}catch{}for(let o=0;o<e.length;o++)try{let i=t.pastedImagePaths[o];i&&await Pt(e[o],ge(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await qn(ge(this.decisionsDir,`d-${t.id}.json`),JSON.stringify(t,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await Hn(this.decisionsDir)).filter(n=>n.startsWith("d-")&&n.endsWith(".json")).map(n=>n.slice(2,-5))}catch{return[]}}async loadDecision(t){try{let n=await Gn(ge(this.decisionsDir,`d-${t}.json`),"utf-8");return JSON.parse(n)}catch{return null}}async loadDecisions(t){return(await Promise.all(t.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(t){return new Promise(n=>{Wn("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){n(null);return}n(o||null)})})}};import{readFile as Tt,writeFile as he}from"fs/promises";import{join as Xe}from"path";var Q="[Materializer]",Xn={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function Yn(s){return Array.isArray(s)?s.map((t,n)=>typeof t=="string"?{id:Math.random().toString(16).slice(2,10),scope:"general",text:t,sources:[]}:t&&typeof t=="object"&&typeof t.text=="string"?t:{id:Math.random().toString(16).slice(2,10),scope:"general",text:String(t),sources:[]}):[]}function xe(s){let t=[];for(let n of s){if(!n||typeof n!="object")continue;let e=n;if(typeof e.id!="string"||typeof e.text!="string"){console.warn(`${Q} Dropping rule missing id or text:`,JSON.stringify(n).slice(0,120));continue}t.push({id:e.id,scope:typeof e.scope=="string"?e.scope:"general",text:e.text,sources:Array.isArray(e.sources)?e.sources.filter(o=>typeof o=="string"):[]})}return t.length>30&&console.warn(`${Q} Rule count ${t.length} exceeds cap of 30`),t}var Oe=class{constructor(t,n,e={}){this.projectRoot=t;this.decisionStore=n;this.options=e;this.cachedIndex=null;this.running=!1;let o=Xe(t,".popmelt");this.indexPath=Xe(o,"materialized.json"),this.modelPath=Xe(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await Tt(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=Yn(n.rules)),n}catch{return null}}async addComponent(t){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let e=n.components;return e[t]?{added:!1,alreadyExists:!0}:(e[t]={description:""},await he(this.modelPath,JSON.stringify(n,null,2)),console.log(`${Q} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,n){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=t.split("."),i=e;for(let h=0;h<o.length-1;h++){let v=o[h];(!i[v]||typeof i[v]!="object")&&(i[v]={}),i=i[v]}let a=o[o.length-1],c;try{c=JSON.parse(n)}catch{c=null}if(c&&typeof c=="object"&&c!==null&&"value"in c)i[a]=c;else{let h=i[a];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=n:i[a]=n}return await he(this.modelPath,JSON.stringify(e,null,2)),console.log(`${Q} Updated token "${t}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=t.split("."),o=n;for(let a=0;a<e.length-1;a++){let c=e[a];if(!o[c]||typeof o[c]!="object")return{removed:!1};o=o[c]}let i=e[e.length-1];return i in o?(delete o[i],await he(this.modelPath,JSON.stringify(n,null,2)),console.log(`${Q} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=n.components;return!e||!e[t]?{removed:!1}:(delete e[t],await he(this.modelPath,JSON.stringify(n,null,2)),console.log(`${Q} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),n=new Set(t.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(a=>!n.has(a));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(a=>a.resolutions.some(c=>(c.finalScope??c.inferredScope)?.breadth==="pattern"))}async run(t={}){if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let n=await this.getUnmaterializedPatternDecisions();if(n.length===0)return{processedIds:[],success:!0};let e=n.map(y=>y.id);console.log(`${Q} Processing ${e.length} pattern-scoped decision(s): ${e.join(", ")}`),this.options.onEvent?.({type:"materialize_started",decisionIds:e});let o=await this.loadModel(),i=Qn(n,o),a=!0,c;try{let y=`mat-${Date.now()}`,{provider:B,result:N}=this.spawnModelAgent(y,i,t,{maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"]}),R=await N;if(!R.success)a=!1,c=R.error,console.error(`${Q} ${B} spawn error:`,c);else{let F=_e(R.text);F?(Array.isArray(F.rules)&&(F.rules=xe(F.rules)),await he(this.modelPath,JSON.stringify(F,null,2)),console.log(`${Q} Successfully materialized ${e.length} decision(s) with ${B} \u2192 ${this.modelPath}`)):(a=!1,c="No <model> block found in response",console.error(`${Q} ${c}`))}}catch(y){a=!1,c=y instanceof Error?y.message:String(y),console.error(`${Q} Error:`,c)}let h=await this.loadIndex(),v=new Set(h.materializedIds);for(let y of e)v.add(y);return h.materializedIds=[...v],h.lastRunAt=Date.now(),h.lastRunDecisionIds=e,h.lastRunError=c??null,await this.persistIndex(h),this.options.onEvent?.({type:"materialize_done",decisionIds:e,success:a,error:c}),{processedIds:e,success:a,error:c}}finally{this.running=!1}}async consolidate(t={}){if(this.running)return{success:!1,error:"Already running"};this.running=!0;try{let n=await this.loadModel();if(!n)return{success:!1,error:"No model exists"};let e=Kn(n),{provider:o,result:i}=this.spawnModelAgent(`consolidate-${Date.now()}`,e,t,{maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[]}),a=await i;if(!a.success)return console.error(`${Q} ${o} consolidation spawn error:`,a.error),{success:!1,error:a.error};let c=_e(a.text);return c?(Array.isArray(c.rules)&&(c.rules=xe(c.rules)),!c.tokens&&n.tokens&&(c.tokens=n.tokens),!c.components&&n.components&&(c.components=n.components),await he(this.modelPath,JSON.stringify(c,null,2)),console.log(`${Q} Consolidation complete with ${o} \u2192 ${this.modelPath}`),{success:!0}):(console.error(`${Q} No <model> block in consolidation response`),{success:!1,error:"No <model> block found"})}catch(n){let e=n instanceof Error?n.message:String(n);return console.error(`${Q} Consolidation error:`,e),{success:!1,error:e}}finally{this.running=!1}}async writeModel(t){Array.isArray(t.rules)&&(t.rules=xe(t.rules)),await he(this.modelPath,JSON.stringify(t,null,2)),console.log(`${Q} Model written \u2192 ${this.modelPath}`)}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await Tt(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...Xn,materializedIds:[],lastRunDecisionIds:[]},this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await he(this.indexPath,JSON.stringify(t,null,2))}catch(n){console.error(`${Q} Failed to write index:`,n)}}spawnModelAgent(t,n,e,o){let i=e.provider??this.options.provider??"claude",a=this.options.onEvent?(c,h)=>this.options.onEvent?.(c):void 0;return i==="codex"?{provider:"codex",...Ce(t,{prompt:n,projectRoot:this.projectRoot,model:e.model??this.options.model,timeoutMs:this.options.timeoutMs,sandboxMode:"read-only",onEvent:a})}:(i==="copilot"&&console.warn(`${Q} Copilot materialization is not available yet; falling back to Claude.`),{provider:"claude",...Me(t,{prompt:n,projectRoot:this.projectRoot,maxTurns:o.maxTurns,maxBudgetUsd:o.maxBudgetUsd,allowedTools:o.allowedTools,claudePath:this.options.claudePath??"claude",model:i==="claude"?e.model??this.options.model:void 0,timeoutMs:this.options.timeoutMs,onEvent:a})})}};function _e(s){let t=s.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!t?.[1])return null;try{let n=JSON.parse(t[1]);return typeof n!="object"||n===null||Array.isArray(n)?null:n}catch{return null}}function Qn(s,t){let n=s.map(i=>{let c=i.resolutions.filter(y=>(y.finalScope??y.inferredScope)?.breadth==="pattern").map(y=>{let N=(y.finalScope??y.inferredScope)?.target??"unknown",R=y.filesModified?.join(", ")??"none";return`- **${y.summary}** [scope: pattern/${N}]
34
- Files modified: ${R}`}).join(`
35
- `),h=i.annotations.map(y=>y.instruction).filter(Boolean).join(`
36
- `),v=i.gitDiff?`
28
+ </html>`}import{spawn as qn}from"child_process";import{createInterface as Yn}from"readline";var Kn=new Set([".md",".txt",".json",".ts",".tsx",".js",".jsx",".css",".scss",".html",".xml",".yaml",".yml",".toml",".ini",".cfg",".conf",".sh",".bash",".zsh",".py",".rb",".go",".rs",".java",".c",".h",".cpp",".hpp",".swift",".kt",".sql",".graphql",".svg",".env",".gitignore",".prettierrc",".eslintrc"]),bt=1e5;function Qn(s){let t=s.input?.file_path||s.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!Kn.has(n))return;let e;if(s.name==="Write"&&typeof s.input?.content=="string"?e=s.input.content:s.name==="Edit"&&typeof s.input?.new_string=="string"&&(e=s.input.new_string),!!e)return e.length>bt?e.slice(0,bt)+`
29
+ \u2026[truncated]`:e}function Je(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:a=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:h,model:P,timeoutMs:g=3e5,onEvent:B}=t,z=[];h?z.push("--resume",h,"-p",n):z.push("-p",n),z.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),P&&z.push("--model",P);for(let S of a)z.push("--allowedTools",S);let O=qn(c,z,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),L=new Promise(S=>{let X,W=[],Q=[],Z=[],V=!1,q="",D,_,T=!1,k=setTimeout(()=>{T=!0,O.kill("SIGTERM"),setTimeout(()=>{try{O.kill("SIGKILL")}catch{}},5e3)},g),y=Yn({input:O.stdout}),C=new Set;y.on("line",x=>{if(x.trim())try{let b=JSON.parse(x);b.session_id&&!X&&(X=b.session_id);let H=b.type??(b.event?.type?`event.${b.event.type}`:"unknown");if(C.add(H),b.type==="result"&&b.result&&W.length===0){let E=typeof b.result=="string"?b.result:"";E&&(W.push(E),B?.({type:"delta",jobId:s,text:E},s))}if(b.type==="assistant"&&Array.isArray(b.message?.content)){typeof b.error=="string"&&(D=b.error);for(let E of b.message.content){if(E.type==="text"&&E.text&&(W.push(E.text),B?.({type:"delta",jobId:s,text:E.text},s)),E.type==="tool_use"&&E.name){let ie=E.input?.file_path||E.input?.path||void 0,re=Qn(E);if(B?.({type:"tool_use",jobId:s,tool:E.name,...ie?{file:ie}:{},...re?{content:re}:{}},s),E.name==="Edit"&&E.input?.file_path){let ne={tool:"Edit",file_path:E.input.file_path,old_string:E.input.old_string,new_string:E.input.new_string,replace_all:E.input.replace_all};Q.push(ne),Z.push({sequence:Z.length,occurredAt:Date.now(),provider:"claude",source:"claude_tool_use",tool:"Edit",operation:"modify",filePath:ne.file_path,oldString:typeof ne.old_string=="string"?ne.old_string:void 0,newString:typeof ne.new_string=="string"?ne.new_string:void 0,replaceAll:typeof ne.replace_all=="boolean"?ne.replace_all:void 0})}else E.name==="Write"&&E.input?.file_path&&(Q.push({tool:"Write",file_path:E.input.file_path,content:E.input.content}),Z.push({sequence:Z.length,occurredAt:Date.now(),provider:"claude",source:"claude_tool_use",tool:"Write",operation:"create",filePath:E.input.file_path,content:typeof E.input.content=="string"?E.input.content:void 0}))}E.type==="thinking"&&E.thinking&&B?.({type:"thinking",jobId:s,text:E.thinking},s)}}b.type==="result"&&b.is_error===!0&&(V=!0,typeof b.error=="string"&&(D=b.error),typeof b.result=="string"&&(_=b.result)),b.type==="user"&&b.tool_use_result?.file?.filePath&&B?.({type:"tool_use",jobId:s,tool:"Read",file:b.tool_use_result.file.filePath},s)}catch{}});let N=[];O.stderr?.on("data",x=>{N.push(x.toString())}),O.on("close",x=>{if(clearTimeout(k),y.close(),W.length===0&&C.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...C].join(", ")}`),T)V=!0,q=`Timed out after ${Math.round(g/6e4)} minutes`;else if(x!==0&&x!==null){V=!0;let H=N.join("").trim(),E=W.join("").trim(),ie=W.length===0&&C.size>0?` (no text captured, event types: ${[...C].join(", ")})`:"";q=H||_||E||`Claude process exited with code ${x}${ie}`}V&&!q&&_&&(q=_);let b=Vn(q,D);S({sessionId:X,text:W.join(""),success:!V,error:V?b.message:void 0,errorCode:V?b.code:void 0,recoverableError:V?b.recoverable:void 0,fileEdits:Q.length>0?Q:void 0,editEvents:Z.length>0?Z:void 0})}),O.on("error",x=>{clearTimeout(k),V=!0,q=x.message,S({sessionId:X,text:W.join(""),success:!1,error:q,errorCode:"claude_spawn_error",recoverableError:!1,fileEdits:Q.length>0?Q:void 0,editEvents:Z.length>0?Z:void 0})})});return{process:O,result:L}}function Vn(s,t){let n=s.trim()||"Claude CLI failed",e=`${t??""} ${n}`.toLowerCase();return e.includes("authentication_failed")||e.includes("not logged in")||e.includes("please run /login")||e.includes("api key")?{message:n,code:"claude_auth_required",recoverable:!0}:e.includes("model")&&(e.includes("not found")||e.includes("unknown")||e.includes("unavailable"))?{message:n,code:"claude_model_unavailable",recoverable:!0}:{message:n,code:t||"claude_error",recoverable:!1}}import{spawn as Zn,spawnSync as Tt}from"child_process";import{readFileSync as es,statSync as ts}from"fs";import{basename as ns,extname as ss,isAbsolute as os,relative as It,resolve as rs}from"path";import{createInterface as is}from"readline";function xt(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}var kt=new Set([".md",".txt",".json",".ts",".tsx",".js",".jsx",".css",".scss",".html",".xml",".yaml",".yml",".toml",".ini",".cfg",".conf",".sh",".bash",".zsh",".py",".rb",".go",".rs",".java",".c",".h",".cpp",".hpp",".swift",".kt",".sql",".graphql",".svg",".env",".gitignore",".prettierrc",".eslintrc"]),ot=2e5,as=1e5,Mt=2e5;function Ct(s){let t=ns(s).toLowerCase(),n=ss(s).toLowerCase();return kt.has(n)||kt.has(t)}function $t(s,t){let n=os(t)?t:rs(s,t),e=It(s,n);return e===""||e.startsWith("..")?null:n}function Be(s,t){if(!Ct(t))return;let n=$t(s,t);if(n)try{let e=ts(n);return e.isFile()?e.size>ot?{exists:!0,truncated:!0}:{exists:!0,content:es(n,"utf8")}:{exists:!1}}catch{return{exists:!1}}}function cs(s,t){for(let n of t){let e=s[n];if(typeof e=="string")return e}}function ls(s){let t=s?.toLowerCase()??"";return t.includes("add")||t.includes("create")?"create":t.includes("delete")||t.includes("remove")?"delete":t.includes("rename")||t.includes("move")?"rename":t.includes("modify")||t.includes("update")||t.includes("edit")?"modify":"unknown"}function Pt(s){return cs(s,["patch","diff","unifiedDiff","unified_diff"])}function rt(s,t){return s.length<=t?{text:s}:{text:s.slice(0,t),truncated:!0}}function Et(s,t){let n=Tt("git",t,{cwd:s,encoding:"utf8",timeout:5e3,maxBuffer:Mt+65536});return n.status!==0?null:n.stdout??""}function ds(s){let t=new Set;for(let n of s.split(`
30
+ `)){if(n.length<4)continue;let e=n.slice(3).trim();if(!e)continue;e.startsWith('"')&&e.endsWith('"')&&(e=e.slice(1,-1));let o=e.split(" -> ");for(let i of o)i&&t.add(i)}return[...t]}function Rt(s){let t=Et(s,["status","--porcelain=v1","-uall"]);if(t===null)return;let n=Et(s,["diff","--no-ext-diff","HEAD","--"])??"",e=rt(n,Mt);return{status:t,changedFiles:ds(t),...e.text?{diff:e.text}:{},...e.truncated?{diffTruncated:!0}:{}}}function us(s,t){return!s||!t?!1:s.status!==t.status||(s.diff??"")!==(t.diff??"")}function ps(s,t){if(!Ct(t))return;let n=$t(s,t);if(!n)return;let e=It(s,n);if(e===""||e.startsWith(".."))return;let o=Tt("git",["show",`HEAD:${e}`],{cwd:s,encoding:"utf8",timeout:5e3,maxBuffer:ot+65536});if(o.status!==0)return{exists:!1};let i=o.stdout??"",a=rt(i,ot);return{exists:!0,content:a.text,...a.truncated?{truncated:!0}:{}}}function fs(s,t,n){return t.map(e=>{let o=n.has(e)?n.get(e):ps(s,e),i=Be(s,e);return{filePath:e,...o?{before:o}:{},...i?{after:i}:{}}})}function Fe(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:a,onEvent:c,sandboxMode:h="workspace-write",configOverrides:P=[]}=t,g=[];if(i){g.push("exec","resume","--skip-git-repo-check","--json","-c",`sandbox_mode="${h}"`);for(let O of P)g.push("-c",O);a&&g.push("-m",a),g.push(i),g.push(n),o&&g.push("--image",o)}else{g.push("exec","--skip-git-repo-check","--json","--sandbox",h);for(let O of P)g.push("-c",O);a&&g.push("-m",a),g.push(n),o&&g.push("--image",o)}let B=Zn("codex",g,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),z=new Promise(O=>{let L,S=[],X=[],W=[],Q=new Map,Z=new Map,V=!1,q="",D=is({input:B.stdout}),_=new Set;D.on("line",k=>{if(k.trim())try{let y=JSON.parse(k),C=y.type??"unknown";if(_.add(C),C==="thread.started"&&y.thread_id&&!L&&(L=y.thread_id),(C==="item.agentMessage.delta"||C==="item/agentMessage/delta")&&y.delta?.text&&(S.push(y.delta.text),c?.({type:"delta",jobId:s,text:y.delta.text},s)),(C==="item.reasoning.delta"||C==="item/reasoning/delta")&&y.delta?.text&&c?.({type:"thinking",jobId:s,text:y.delta.text},s),(C==="item.started"||C==="item/started")&&y.item){let N=y.item.type;if(N==="command_execution"){let x=y.item.command,b=x?xt(x):void 0,H=Rt(e),E=new Map;for(let re of H?.changedFiles??[])E.set(re,Be(e,re));typeof y.item.id=="string"&&Z.set(y.item.id,{...x?{rawCommand:x}:{},...b?{command:b}:{},...H?{beforeWorktree:H}:{},beforeSnapshots:E});let ie=b?`Bash: ${b.split(`
31
+ `)[0].slice(0,80)}`:"Bash";X.push(ie),c?.({type:"tool_use",jobId:s,tool:"Bash",...b?{content:b}:{}},s)}else if(N==="file_change"){let x=y.item.filename||y.item.path;x&&!Q.has(x)&&Q.set(x,Be(e,x)),X.push(x?`Edit ${x.split("/").pop()}`:"Edit"),c?.({type:"tool_use",jobId:s,tool:"Edit",...x?{file:x}:{}},s)}else if(N==="file_read"){let x=y.item.filename||y.item.path;X.push(x?`Read ${x.split("/").pop()}`:"Read"),c?.({type:"tool_use",jobId:s,tool:"Read",...x?{file:x}:{}},s)}else if(N==="web_search")X.push("WebSearch"),c?.({type:"tool_use",jobId:s,tool:"WebSearch"},s);else if(N==="mcp_tool_call"){let x=y.item.tool_name||y.item.name||"MCP";X.push(x),c?.({type:"tool_use",jobId:s,tool:x},s)}}if((C==="item.completed"||C==="item/completed")&&y.item){if(y.item.type==="agent_message"){let N=y.item.text;typeof N=="string"&&N&&(S.push(N),c?.({type:"delta",jobId:s,text:N},s))}else if(y.item.type==="reasoning"){let N=y.item.text;typeof N=="string"&&N&&c?.({type:"thinking",jobId:s,text:N},s)}else if(y.item.type==="file_change"&&Array.isArray(y.item.changes))for(let N of y.item.changes){let x=N.path||N.filename,b=N.kind==="add"?"Write":"Edit";if(x){X.push(`${b} ${x.split("/").pop()}`),c?.({type:"tool_use",jobId:s,tool:b,file:x},s);let H=N,E=typeof H.kind=="string"?H.kind:void 0,ie=ls(E),re=Q.get(x),ne=Be(e,x);W.push({sequence:W.length,occurredAt:Date.now(),provider:"codex",source:"codex_file_change",tool:"FileChange",operation:ie,filePath:x,...re?{before:re}:{},...ne?{after:ne}:{},...Pt(H)?{patch:Pt(H)}:{},...E?{rawKind:E}:{}}),Q.set(x,ne)}}else if(y.item.type==="command_execution"){let N=typeof y.item.id=="string"?y.item.id:void 0,x=N?Z.get(N):void 0,b=typeof y.item.command=="string"?y.item.command:x?.rawCommand,H=b?xt(b):x?.command,E=Rt(e);if(us(x?.beforeWorktree,E)){let ie=[...new Set([...x?.beforeWorktree?.changedFiles??[],...E?.changedFiles??[]])],re=typeof y.item.aggregated_output=="string"?rt(y.item.aggregated_output,as):void 0,ne=typeof y.item.exit_code=="number"?y.item.exit_code:y.item.exit_code===null?null:void 0,De=typeof y.item.status=="string"?y.item.status:void 0;W.push({sequence:W.length,occurredAt:Date.now(),provider:"codex",source:"codex_command_execution",tool:"Bash",operation:"command",...b?{rawCommand:b}:{},...H?{command:H}:{},...ne!==void 0?{exitCode:ne}:{},...De?{status:De}:{},...re?{output:re.text}:{},...re?.truncated?{outputTruncated:!0}:{},...x?.beforeWorktree?{beforeWorktree:x.beforeWorktree}:{},...E?{afterWorktree:E}:{},...ie.length>0?{touchedFiles:fs(e,ie,x?.beforeSnapshots??new Map)}:{}})}N&&Z.delete(N)}}C==="turn.failed"&&(V=!0,q=y.error?.message||y.message||"Turn failed")}catch{}});let T=[];B.stderr?.on("data",k=>{T.push(k.toString())}),B.on("close",k=>{D.close(),S.length===0&&_.size>0&&console.warn(`[Codex:${s}] No text captured. Event types seen: ${[..._].join(", ")}`),k!==0&&k!==null&&(V=!0,q=T.join("")||`Codex process exited with code ${k}`),O({sessionId:L,text:S.join(""),success:!V,error:V?q:void 0,editEvents:W.length>0?W:void 0,toolsUsed:X.length>0?X:void 0})}),B.on("error",k=>{V=!0,q=k.message,O({sessionId:L,text:S.join(""),success:!1,error:q,editEvents:W.length>0?W:void 0,toolsUsed:X.length>0?X:void 0})})});return{process:B,result:z}}import{spawn as hs}from"child_process";import{createInterface as ms}from"readline";function _t(s,t){let n=s.trim()||"Copilot CLI failed",e=n.toLowerCase();return e.includes("no authentication information")||e.includes("not authenticated")||e.includes("/login")?{code:"copilot_auth_required",recoverable:!0,message:"Copilot is not authenticated. Run `copilot login`, set `COPILOT_GITHUB_TOKEN`, or refresh `gh auth login`."}:e.includes("model")&&(e.includes("not available")||e.includes("not found")||e.includes("disabled")||e.includes("policy"))?{code:"copilot_model_unavailable",recoverable:!0,message:t?`Copilot rejected ${t}. Choose another model or ask your administrator to enable it.`:n}:e.includes("mcp")&&(e.includes("blocked")||e.includes("allowlist"))?{code:"copilot_mcp_blocked",recoverable:!0,message:"Copilot blocked the Popmelt MCP server. Ask your administrator to add it to the enterprise allowlist."}:e.includes("permission")||e.includes("denied")||e.includes("not allowed")?{code:"copilot_permission_denied",recoverable:!0,message:n}:{code:"copilot_error",recoverable:!1,message:n}}function Ot(s,t){let{prompt:n,projectRoot:e,resumeSessionId:o,model:i,timeoutMs:a=3e5,onEvent:c,copilotPath:h="copilot"}=t,P=["--no-color","--no-auto-update","--output-format","json","--stream","on","--available-tools=read,write,shell","--allow-all-tools","--deny-tool=shell(git push)","--deny-tool=shell(rm)","--deny-tool=shell(sudo)","--no-ask-user"];o&&P.push(`--resume=${o}`),i&&P.push("--model",i),P.push("-p",n);let g=hs(h,P,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),B=new Promise(z=>{let O,L=[],S=[],X=!1,W="",Q=!1,Z=setTimeout(()=>{Q=!0,g.kill("SIGTERM"),setTimeout(()=>{try{g.kill("SIGKILL")}catch{}},5e3)},a),V=ms({input:g.stdout}),q=new Set,D=[];V.on("line",T=>{if(T.trim())try{let k=JSON.parse(T),y=ke(k);q.add(y),D.length<5&&vs(y)&&D.push(T.slice(0,800));let C=xe(k,["sessionId","session_id","id"],["session","conversation","thread"]);C&&!O&&(O=C);let N=gs(k,L.length===0);N&&(L.push(N),c?.({type:"delta",jobId:s,text:N},s));let x=ys(k);x&&c?.({type:"thinking",jobId:s,text:x},s);let b=ws(k);b&&(S.push(b.label),c?.({type:"tool_use",jobId:s,tool:b.tool,...b.file?{file:b.file}:{},...b.content?{content:b.content}:{}},s));let H=Ss(k);H&&(X=!0,W=H)}catch{}});let _=[];g.stderr?.on("data",T=>{_.push(T.toString())}),g.on("close",T=>{clearTimeout(Z),V.close(),L.length===0&&q.size>0&&(console.warn(`[Copilot:${s}] No text captured. Event types seen: ${[...q].join(", ")}`),D.length>0&&console.warn(`[Copilot:${s}] Sample text-bearing candidates: ${D.join(`
32
+ `)}`)),Q?(X=!0,W=`Timed out after ${Math.round(a/6e4)} minutes`):T!==0&&T!==null&&(X=!0,W=_.join("").trim()||W||`Copilot process exited with code ${T}`);let k=X?_t(W,i):null;z({sessionId:O,text:L.join(""),success:!X,error:k?.message,errorCode:k?.code,recoverableError:k?.recoverable,provider:"copilot",toolsUsed:S.length>0?S:void 0})}),g.on("error",T=>{clearTimeout(Z);let k=_t(T.message,i);z({sessionId:O,text:L.join(""),success:!1,error:k.message,errorCode:k.code,recoverableError:k.recoverable,provider:"copilot",toolsUsed:S.length>0?S:void 0})})});return{process:g,result:B}}function ke(s){if(typeof s.type=="string")return s.type;let t=s.update;return t&&typeof t.sessionUpdate=="string"?t.sessionUpdate:typeof s.event=="string"?s.event:"unknown"}function gs(s,t=!1){let n=ke(s).toLowerCase(),e=s.update,o=e?.content;if(e?.sessionUpdate==="agent_message_chunk"&&o?.type==="text"&&typeof o.text=="string")return o.text;if(n==="assistant.message_delta"||n.includes("agent_message.delta"))return pe(s.delta)??pe(s.content)??pe(s.message);if(n==="assistant.message"&&t)return pe(s.message)??pe(s.content)??pe(s);if(n==="result"&&t)return pe(s.result)??pe(s.output)??pe(s.message)??pe(s);let i=s.content;if(i?.type==="text"&&typeof i.text=="string")return i.text;if(typeof s.text=="string"&&it(s))return s.text;if(typeof s.message=="string"&&it(s))return s.message;if(typeof s.delta=="string"&&it(s))return s.delta}function pe(s){if(typeof s=="string")return s;if(!s)return;if(Array.isArray(s)){let n=s.map(pe).filter(e=>!!e);return n.length>0?n.join(""):void 0}if(typeof s!="object")return;let t=s;if(typeof t.text=="string")return t.text;if(typeof t.content=="string")return t.content;for(let n of["delta","content","message","messages","parts","items","output","result","data","value","body","markdown","text_delta","textDelta"]){let e=pe(t[n]);if(e)return e}}function vs(s){let t=s.toLowerCase();return t.includes("assistant.message")||t==="result"}function ys(s){if(typeof s.thinking=="string")return s.thinking;if(typeof s.reasoning=="string")return s.reasoning;let t=s.update;if(t?.sessionUpdate==="reasoning_chunk"){let n=t.content;if(typeof n?.text=="string")return n.text}}function ws(s){let t=xe(s,["tool","toolName","tool_name","name"]),n=xe(s,["command","cmd"]),e=xe(s,["file","path","filename","filePath"]),o=ke(s);if(n||o.includes("shell"))return{tool:"Bash",label:n?`Bash: ${n.split(`
33
+ `)[0].slice(0,80)}`:"Bash",...n?{content:n}:{}};if(t)return{tool:t,label:e?`${t} ${e.split("/").pop()}`:t,...e?{file:e}:{}};if(e&&(o.includes("file")||o.includes("write")||o.includes("edit"))){let i=o.includes("write")?"Write":o.includes("read")?"Read":"Edit";return{tool:i,label:`${i} ${e.split("/").pop()}`,file:e}}}function Ss(s){if(typeof s.error=="string")return s.error;if(s.error&&typeof s.error=="object"){let t=s.error;if(typeof t.message=="string")return t.message}if(typeof s.message=="string"&&ke(s).toLowerCase().includes("error"))return s.message}function xe(s,t,n=[]){for(let e of t){let o=s[e];if(typeof o=="string")return o}for(let[e,o]of Object.entries(s)){if(!o||typeof o!="object"||n.length>0&&!n.some(a=>e.toLowerCase().includes(a)))continue;let i=xe(o,t);if(i)return i}}function it(s){let t=ke(s).toLowerCase();return(typeof s.role=="string"?s.role.toLowerCase():"")==="assistant"||t.includes("assistant")||t.includes("agent_message")}import{execFile as bs}from"child_process";import{copyFile as At,mkdir as Dt,readdir as xs,readFile as ks,writeFile as Ps}from"fs/promises";import{join as ve}from"path";var je=class{constructor(t){this.projectRoot=t;let n=ve(t,".popmelt");this.decisionsDir=ve(n,"decisions"),this.screenshotsDir=ve(n,"screenshots")}async persist(t,n,e){try{await Dt(this.decisionsDir,{recursive:!0}),await Dt(this.screenshotsDir,{recursive:!0});try{await At(n,ve(this.screenshotsDir,`s-${t.id}.webp`))}catch{}for(let o=0;o<e.length;o++)try{let i=t.pastedImagePaths[o];i&&await At(e[o],ve(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await Ps(ve(this.decisionsDir,`d-${t.id}.json`),JSON.stringify(t,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await xs(this.decisionsDir)).filter(n=>n.startsWith("d-")&&n.endsWith(".json")).map(n=>n.slice(2,-5))}catch{return[]}}async loadDecision(t){try{let n=await ks(ve(this.decisionsDir,`d-${t}.json`),"utf-8");return JSON.parse(n)}catch{return null}}async loadDecisions(t){return(await Promise.all(t.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(t){return new Promise(n=>{bs("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){n(null);return}n(o||null)})})}};import{mkdir as Pe,readFile as Nt,writeFile as Ee}from"fs/promises";import{join as Re}from"path";var Es=80,Rs=24,Jt=1e3,Ue=class{constructor(t){this.evalsDir=Re(t,".popmelt","evals"),this.suitePath=Re(this.evalsDir,"evals.json"),this.runsPath=Re(this.evalsDir,"runs.json"),this.screenshotsDir=Re(this.evalsDir,"screenshots")}async loadSuite(){try{let t=await Nt(this.suitePath,"utf-8"),n=JSON.parse(t);if(n?.version===1&&Array.isArray(n.cases))return n}catch{}return{version:1,updatedAt:0,cases:[]}}async loadRuns(){try{let t=await Nt(this.runsPath,"utf-8"),n=JSON.parse(t);if(n?.version===1&&Array.isArray(n.runs))return n}catch{}return{version:1,updatedAt:0,runs:[]}}async loadSuiteWithRuns(){let[t,n]=await Promise.all([this.loadSuite(),this.loadRuns()]);return{...t,runs:n.runs}}async loadSuiteWithCurrentRuns(t){let n=await this.loadSuiteWithRuns();return t?{...n,runs:n.runs.filter(e=>e.codeFingerprint===t)}:n}async writeSynthesized(t){let n=await this.loadSuite(),e=new Map(n.cases.map(a=>[a.id,a])),o=new Set;for(let a of t){let c=e.get(a.id);c&&(c.title!==a.title||c.prompt!==a.prompt||JSON.stringify(c.assertions)!==JSON.stringify(a.assertions))&&o.add(a.id),e.set(a.id,a)}let i={version:1,updatedAt:Date.now(),cases:[...e.values()]};return await Pe(this.evalsDir,{recursive:!0}),await Ee(this.suitePath,JSON.stringify(i,null,2)),o.size>0&&await this.invalidateRunsForEvals(o),i}async updateCase(t,n){let e=await this.loadSuite(),o=n.title!==void 0||n.assertions!==void 0,i=e.cases.map(c=>c.id===t?{...c,...n.status?{status:n.status}:{},...n.title?{title:n.title}:{},...n.assertions?{assertions:n.assertions}:{}}:c),a=i.find(c=>c.id===t)??null;return a?(await Pe(this.evalsDir,{recursive:!0}),await Ee(this.suitePath,JSON.stringify({version:1,updatedAt:Date.now(),cases:i},null,2)),o&&await this.invalidateRunsForEvals(new Set([t])),a):null}async invalidateRunsForEvals(t){let e=(await this.loadRuns()).runs.filter(i=>!t.has(i.evalId)),o={version:1,updatedAt:Date.now(),runs:e};return await Pe(this.evalsDir,{recursive:!0}),await Ee(this.runsPath,JSON.stringify(o,null,2)),o}async writeRun(t){let n=await this.loadRuns(),e=[t,...n.runs.filter(i=>i.id!==t.id)].slice(0,200),o={version:1,updatedAt:Date.now(),runs:e};return await Pe(this.evalsDir,{recursive:!0}),await Ee(this.runsPath,JSON.stringify(o,null,2)),o}async writeRunScreenshot(t,n){return await Pe(this.screenshotsDir,{recursive:!0}),await Ee(Re(this.screenshotsDir,`eval-${t}.webp`),n),`evals/screenshots/eval-${t}.webp`}async findCase(t){return(await this.loadSuite()).cases.find(e=>e.id===t)??null}};function Bt(s){let t=s.match(/<evals>\s*([\s\S]*?)\s*<\/evals>/);if(!t?.[1])return null;try{let n=JSON.parse(t[1]),e=Array.isArray(n)?n:n&&typeof n=="object"&&Array.isArray(n.evals)?n.evals:null;return e?Ut(e):null}catch{return null}}function Ft(s){let t=s.match(/<eval_proposals>\s*([\s\S]*?)\s*<\/eval_proposals>/);if(!t?.[1])return null;try{let n=JSON.parse(t[1]),e=Array.isArray(n)?n:n&&typeof n=="object"&&Array.isArray(n.evals)?n.evals:null;return e?Ut(e,"pending"):null}catch{return null}}function jt(s,t){let n=s.match(/<eval_run_result>\s*([\s\S]*?)\s*<\/eval_run_result>/);if(n?.[1])try{let e=JSON.parse(n[1]),o=_s(e.assertionResults,t),i=Os(o);return{status:i,summary:fe(e.summary)||Wt(i),assertionResults:o,tooling:As(e.tooling)}}catch{}return{status:"needs_review",summary:ze(s.trim()||"The evaluator did not return a structured result.",1e3),assertionResults:t.map(e=>({assertion:e,status:"needs_review",summary:"No structured assertion result was returned.",evidence:[]})),tooling:{chromeDevtools:"unknown",evidenceTypes:["screenshot"]}}}function Ut(s,t="approved"){let n=[];return s.forEach((e,o)=>{if(!e||typeof e!="object")return;let i=e,a=fe(i.title),c=fe(i.prompt),h=fe(i.rationale),P=Se(i.assertions);if(!a||!c||P.length===0)return;let g=Ms(i.category),B=Ds(fe(i.id))||`eval-${Ht(a).slice(0,32)||o+1}`,z=Cs(i.status)??t;n.push({id:B,title:a,status:z,category:g,prompt:c,assertions:P,rationale:h||"Inferred from Popmelt design history.",sourceDecisionIds:Se(i.sourceDecisionIds),sourceThreadIds:Se(i.sourceThreadIds)})}),n}function zt(s){let t=[...s.decisions].sort((e,o)=>o.createdAt-e.createdAt).slice(0,Es),n=Object.values(s.threads.threads).sort((e,o)=>o.updatedAt-e.updatedAt).slice(0,Rs);return`You are helping a designer turn organic Popmelt annotation history into a project eval suite.
34
+
35
+ The designer does NOT work eval-first. They use annotations and conversation threads to zhuzh the UI over time. Your job is to infer useful eval proposals from that trail. The developer will reject, refine, or approve proposals in this thread. Do not write files or run commands.
36
+
37
+ ## Current Design Model
38
+ \`\`\`json
39
+ ${JSON.stringify(s.model??null,null,2)}
40
+ \`\`\`
41
+
42
+ ## Existing Eval Suite
43
+ \`\`\`json
44
+ ${JSON.stringify(s.existingSuite,null,2)}
45
+ \`\`\`
46
+
47
+ ## Recent Decisions
48
+ ${t.map(Ts).join(`
49
+
50
+ `)||"No decisions found."}
51
+
52
+ ## Recent Threads
53
+ ${n.map(Is).join(`
54
+
55
+ `)||"No threads found."}
56
+
57
+ ## Instructions
58
+ 1. Infer evals that encode reusable taste, design judgment, product behavior, or interaction expectations.
59
+ 2. Prefer proposals backed by multiple decisions, rich thread context, or explicit approved/resolved outcomes.
60
+ 3. Avoid one-off implementation chores, temporary bug fixes, vague preferences, or evals that cannot be checked later.
61
+ 4. Cite source decision ids and thread ids for each proposal.
62
+ 5. Present proposals as clear text first. Group them by Component, Layout, Copy, Interaction, Visual, and Workflow.
63
+ 6. Ask the developer what to approve, reject, or adjust.
64
+ 7. Include a machine-readable <eval_proposals> block containing the same proposals as pending eval JSON. These will be saved as draft proposals for panel review.
65
+ 8. Tell the developer that the proposals have been saved to the Eval panel for accept/reject review.
66
+ 9. Do NOT output an <evals> block yet. First get the developer's approval.
67
+
68
+ ## After Approval
69
+ When the developer approves or asks you to save the final set, output a complete JSON array inside <evals> tags. Each object must use this shape:
70
+
71
+ \`\`\`json
72
+ {
73
+ "id": "eval-short-stable-id",
74
+ "title": "Short human-readable name",
75
+ "status": "approved",
76
+ "category": "component",
77
+ "prompt": "The eval instruction an agent should satisfy.",
78
+ "assertions": ["Observable expectation 1", "Observable expectation 2"],
79
+ "rationale": "Why this eval represents the designer's taste.",
80
+ "sourceDecisionIds": ["decision-id"],
81
+ "sourceThreadIds": ["thread-id"]
82
+ }
83
+ \`\`\`
84
+
85
+ Allowed categories: component, layout, copy, interaction, visual, workflow.
86
+ Return only the <evals> block after approval.
87
+
88
+ ## First Response Structured Block
89
+ At the end of your first proposal response, output a JSON array inside <eval_proposals> tags using the same schema, but with "status": "pending".`}function Lt(s){return`You are running a Popmelt design eval against the current page. This is an evaluation-only task: do not edit files, do not write code, and do not create annotations.
90
+
91
+ ## Eval
92
+ \`\`\`json
93
+ ${JSON.stringify(s.evalCase,null,2)}
94
+ \`\`\`
95
+
96
+ ## Scope
97
+ \`\`\`json
98
+ ${JSON.stringify(s.scope,null,2)}
99
+ \`\`\`
100
+
101
+ ## Current Design Model
102
+ \`\`\`json
103
+ ${JSON.stringify(s.model??null,null,2)}
104
+ \`\`\`
105
+
106
+ ## Evidence
107
+ - Current page screenshot: ${s.screenshotPath}
108
+ - Chrome DevTools MCP is ${s.chromeDevtoolsAvailable?"configured for this provider and approved by the bridge when the runtime exposes its tools":"not detected by the bridge"}.
109
+
110
+ ## Instructions
111
+ 1. Evaluate only the current page scope.
112
+ 2. Use the screenshot as baseline evidence.
113
+ 3. If Chrome DevTools MCP tools are available, prefer them for DOM, computed style, accessibility, console, and layout evidence.
114
+ 4. Judge each assertion independently as "pass", "fail", or "needs_review".
115
+ 5. Keep failures actionable and grounded in observed evidence.
116
+ 6. Do not propose visual markers yet.
117
+ 7. End with exactly one machine-readable <eval_run_result> block.
118
+ 8. Do not ask the user for additional Chrome DevTools permission. If DevTools is unavailable, denied, or not useful, continue immediately from the screenshot, source, and model evidence.
119
+ 9. Before the structured block, keep the visible response brief. Use one checklist row per assertion with these markers: \u2611 pass, \u2612 fail, \u25A1 needs_review. Do not include long reasoning paragraphs.
120
+
121
+ The block must be valid JSON with this shape:
122
+
123
+ \`\`\`json
124
+ {
125
+ "status": "pass",
126
+ "summary": "Short run summary.",
127
+ "assertionResults": [
128
+ {
129
+ "assertion": "Original assertion text",
130
+ "status": "pass",
131
+ "summary": "What was observed.",
132
+ "evidence": ["Screenshot shows ...", "DOM selector ..."]
133
+ }
134
+ ],
135
+ "tooling": {
136
+ "chromeDevtools": "available",
137
+ "evidenceTypes": ["screenshot", "dom", "computed_styles"]
138
+ }
139
+ }
140
+ \`\`\`
141
+
142
+ Allowed statuses: pass, fail, needs_review.
143
+ Allowed chromeDevtools values: available, unavailable, unknown.`}function Ts(s){let t=s.annotations??[],n=s.resolutions??[],e=s.editEvents??[],o=t.map(c=>c.instruction).filter(Boolean).join(`
144
+ `),i=n.map(c=>{let h=c.finalScope??c.inferredScope??c.declaredScope;return`- ${c.status}: ${c.summary}${h?` [${h.breadth}/${h.target}]`:""}`}).join(`
145
+ `),a=[...new Set([...n.flatMap(c=>c.filesModified??[]),...e.map(c=>c.filePath).filter(c=>!!c)])].slice(0,12);return`### Decision ${s.id}
146
+ Thread: ${s.threadId??"none"}
147
+ Created: ${new Date(s.createdAt).toISOString()}
148
+ URL: ${s.url||"unknown"}
149
+ Instructions:
150
+ ${ze(o||"(none)",Jt)}
151
+ Resolutions:
152
+ ${ze(i||"(none)",Jt)}
153
+ Files: ${a.length>0?a.join(", "):"none"}`}function Is(s){let n=s.messages.slice(0,2).concat(s.messages.length>4?s.messages.slice(-2):s.messages.slice(2)).map(e=>{let o=e.feedbackSummary??e.replyToQuestion??e.question??e.responseText??"",i=e.resolutions?.map(a=>`${a.status}: ${a.summary}`).join("; ")??"";return`- ${e.role}: ${ze(o||i||"(no text)",500)}`}).join(`
154
+ `);return`### Thread ${s.id}
155
+ Messages: ${s.messages.length}
156
+ Updated: ${new Date(s.updatedAt).toISOString()}
157
+ ${n}`}function fe(s){return typeof s=="string"?s.trim():""}function Se(s){return Array.isArray(s)?s.filter(t=>typeof t=="string"&&t.trim().length>0).map(t=>t.trim()):[]}function Ms(s){let t=fe(s);return t==="component"||t==="layout"||t==="copy"||t==="interaction"||t==="visual"||t==="workflow"?t:"visual"}function Cs(s){let t=fe(s);return t==="pending"||t==="approved"||t==="rejected"?t:null}function $s(s){let t=fe(s);return t==="pass"||t==="fail"||t==="needs_review"?t:null}function _s(s,t){let e=(Array.isArray(s)?s:[]).map(o=>{if(!o||typeof o!="object")return null;let i=o,a=fe(i.assertion);return a?{assertion:a,status:$s(i.status)??"needs_review",summary:fe(i.summary)||Wt("needs_review"),evidence:Se(i.evidence)}:null}).filter(o=>o!==null);return e.length>0?e:t.map(o=>({assertion:o,status:"needs_review",summary:"No structured assertion result was returned.",evidence:[]}))}function Os(s){return s.some(t=>t.status==="fail")?"fail":s.some(t=>t.status==="needs_review")?"needs_review":"pass"}function As(s){if(!s||typeof s!="object")return{chromeDevtools:"unknown",evidenceTypes:["screenshot"]};let t=s,n=fe(t.chromeDevtools);return{chromeDevtools:n==="available"||n==="unavailable"||n==="unknown"?n:"unknown",evidenceTypes:Se(t.evidenceTypes).length>0?Se(t.evidenceTypes):["screenshot"]}}function Wt(s){return s==="pass"?"The current page appears to satisfy this eval.":s==="fail"?"The current page appears to violate this eval.":"The current page needs human review for this eval."}function Ds(s){return Ht(s).slice(0,48)}function Ht(s){return s.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")}function ze(s,t){return s.length>t?`${s.slice(0,t)}...`:s}import{readFile as Gt,writeFile as ge}from"fs/promises";import{join as at}from"path";var te="[Materializer]",Ns={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function Js(s){return Array.isArray(s)?s.map((t,n)=>typeof t=="string"?{id:Math.random().toString(16).slice(2,10),scope:"general",text:t,sources:[]}:t&&typeof t=="object"&&typeof t.text=="string"?t:{id:Math.random().toString(16).slice(2,10),scope:"general",text:String(t),sources:[]}):[]}function Te(s){let t=[];for(let n of s){if(!n||typeof n!="object")continue;let e=n;if(typeof e.id!="string"||typeof e.text!="string"){console.warn(`${te} Dropping rule missing id or text:`,JSON.stringify(n).slice(0,120));continue}t.push({id:e.id,scope:typeof e.scope=="string"?e.scope:"general",text:e.text,sources:Array.isArray(e.sources)?e.sources.filter(o=>typeof o=="string"):[]})}return t.length>30&&console.warn(`${te} Rule count ${t.length} exceeds cap of 30`),t}var Le=class{constructor(t,n,e={}){this.projectRoot=t;this.decisionStore=n;this.options=e;this.cachedIndex=null;this.running=!1;let o=at(t,".popmelt");this.indexPath=at(o,"materialized.json"),this.modelPath=at(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await Gt(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=Js(n.rules)),n}catch{return null}}async addComponent(t){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let e=n.components;return e[t]?{added:!1,alreadyExists:!0}:(e[t]={description:""},await ge(this.modelPath,JSON.stringify(n,null,2)),console.log(`${te} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,n){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=t.split("."),i=e;for(let h=0;h<o.length-1;h++){let P=o[h];(!i[P]||typeof i[P]!="object")&&(i[P]={}),i=i[P]}let a=o[o.length-1],c;try{c=JSON.parse(n)}catch{c=null}if(c&&typeof c=="object"&&c!==null&&"value"in c)i[a]=c;else{let h=i[a];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=n:i[a]=n}return await ge(this.modelPath,JSON.stringify(e,null,2)),console.log(`${te} Updated token "${t}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=t.split("."),o=n;for(let a=0;a<e.length-1;a++){let c=e[a];if(!o[c]||typeof o[c]!="object")return{removed:!1};o=o[c]}let i=e[e.length-1];return i in o?(delete o[i],await ge(this.modelPath,JSON.stringify(n,null,2)),console.log(`${te} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=n.components;return!e||!e[t]?{removed:!1}:(delete e[t],await ge(this.modelPath,JSON.stringify(n,null,2)),console.log(`${te} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),n=new Set(t.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(a=>!n.has(a));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(a=>a.resolutions.some(c=>(c.finalScope??c.inferredScope)?.breadth==="pattern"))}async run(t={}){if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let n=await this.getUnmaterializedPatternDecisions();if(n.length===0)return{processedIds:[],success:!0};let e=n.map(g=>g.id);console.log(`${te} Processing ${e.length} pattern-scoped decision(s): ${e.join(", ")}`),this.options.onEvent?.({type:"materialize_started",decisionIds:e});let o=await this.loadModel(),i=Bs(n,o),a=!0,c;try{let g=`mat-${Date.now()}`,{provider:B,result:z}=this.spawnModelAgent(g,i,t,{maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"]}),O=await z;if(!O.success)a=!1,c=O.error,console.error(`${te} ${B} spawn error:`,c);else{let L=We(O.text);L?(Array.isArray(L.rules)&&(L.rules=Te(L.rules)),await ge(this.modelPath,JSON.stringify(L,null,2)),console.log(`${te} Successfully materialized ${e.length} decision(s) with ${B} \u2192 ${this.modelPath}`)):(a=!1,c="No <model> block found in response",console.error(`${te} ${c}`))}}catch(g){a=!1,c=g instanceof Error?g.message:String(g),console.error(`${te} Error:`,c)}let h=await this.loadIndex(),P=new Set(h.materializedIds);for(let g of e)P.add(g);return h.materializedIds=[...P],h.lastRunAt=Date.now(),h.lastRunDecisionIds=e,h.lastRunError=c??null,await this.persistIndex(h),this.options.onEvent?.({type:"materialize_done",decisionIds:e,success:a,error:c}),{processedIds:e,success:a,error:c}}finally{this.running=!1}}async consolidate(t={}){if(this.running)return{success:!1,error:"Already running"};this.running=!0;try{let n=await this.loadModel();if(!n)return{success:!1,error:"No model exists"};let e=Fs(n),{provider:o,result:i}=this.spawnModelAgent(`consolidate-${Date.now()}`,e,t,{maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[]}),a=await i;if(!a.success)return console.error(`${te} ${o} consolidation spawn error:`,a.error),{success:!1,error:a.error};let c=We(a.text);return c?(Array.isArray(c.rules)&&(c.rules=Te(c.rules)),!c.tokens&&n.tokens&&(c.tokens=n.tokens),!c.components&&n.components&&(c.components=n.components),await ge(this.modelPath,JSON.stringify(c,null,2)),console.log(`${te} Consolidation complete with ${o} \u2192 ${this.modelPath}`),{success:!0}):(console.error(`${te} No <model> block in consolidation response`),{success:!1,error:"No <model> block found"})}catch(n){let e=n instanceof Error?n.message:String(n);return console.error(`${te} Consolidation error:`,e),{success:!1,error:e}}finally{this.running=!1}}async writeModel(t){Array.isArray(t.rules)&&(t.rules=Te(t.rules)),await ge(this.modelPath,JSON.stringify(t,null,2)),console.log(`${te} Model written \u2192 ${this.modelPath}`)}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await Gt(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...Ns,materializedIds:[],lastRunDecisionIds:[]},this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await ge(this.indexPath,JSON.stringify(t,null,2))}catch(n){console.error(`${te} Failed to write index:`,n)}}spawnModelAgent(t,n,e,o){let i=e.provider??this.options.provider??"claude",a=this.options.onEvent?(c,h)=>this.options.onEvent?.(c):void 0;return i==="codex"?{provider:"codex",...Fe(t,{prompt:n,projectRoot:this.projectRoot,model:e.model??this.options.model,timeoutMs:this.options.timeoutMs,sandboxMode:"read-only",onEvent:a})}:(i==="copilot"&&console.warn(`${te} Copilot materialization is not available yet; falling back to Claude.`),{provider:"claude",...Je(t,{prompt:n,projectRoot:this.projectRoot,maxTurns:o.maxTurns,maxBudgetUsd:o.maxBudgetUsd,allowedTools:o.allowedTools,claudePath:this.options.claudePath??"claude",model:i==="claude"?e.model??this.options.model:void 0,timeoutMs:this.options.timeoutMs,onEvent:a})})}};function We(s){let t=s.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!t?.[1])return null;try{let n=JSON.parse(t[1]);return typeof n!="object"||n===null||Array.isArray(n)?null:n}catch{return null}}function Bs(s,t){let n=s.map(i=>{let c=i.resolutions.filter(g=>(g.finalScope??g.inferredScope)?.breadth==="pattern").map(g=>{let z=(g.finalScope??g.inferredScope)?.target??"unknown",O=g.filesModified?.join(", ")??"none";return`- **${g.summary}** [scope: pattern/${z}]
158
+ Files modified: ${O}`}).join(`
159
+ `),h=i.annotations.map(g=>g.instruction).filter(Boolean).join(`
160
+ `),P=i.gitDiff?`
37
161
  \`\`\`diff
38
162
  ${i.gitDiff.slice(0,2e3)}
39
163
  \`\`\``:"";return`### Decision ${i.id} (${new Date(i.createdAt).toISOString()})
40
164
  Page: ${i.url}
41
165
  ${c}
42
- ${v}
166
+ ${P}
43
167
  ${h?`
44
168
  Original instructions:
45
169
  ${h}`:""}`}).join(`
@@ -116,7 +240,7 @@ Example:
116
240
  { "value": "8px", "property": "gap", "bindings": ["gap-2"] }
117
241
  - property: "gap", "padding", or "margin" \u2014 which CSS property this token controls
118
242
  - bindings: Tailwind class names (without responsive prefixes) that use this token
119
- Include property and bindings when the decision context has class-level evidence.`}function Kn(s){return`You are consolidating a design model's rules. The model has accumulated too many rules and needs cleanup.
243
+ Include property and bindings when the decision context has class-level evidence.`}function Fs(s){return`You are consolidating a design model's rules. The model has accumulated too many rules and needs cleanup.
120
244
 
121
245
  ## Current Model
122
246
  \`\`\`json
@@ -155,7 +279,7 @@ Output the complete model inside <model> tags. Preserve tokens and components as
155
279
 
156
280
  <model>
157
281
  { "tokens": { ... }, "components": { ... }, "rules": [ ... ] }
158
- </model>`}function Et(s){return`You are a design system curator reviewing a project's design model. Your job is to propose improvements to the rules \u2014 merging duplicates, filling gaps, removing noise.
282
+ </model>`}function Xt(s){return`You are a design system curator reviewing a project's design model. Your job is to propose improvements to the rules \u2014 merging duplicates, filling gaps, removing noise.
159
283
 
160
284
  ## Current Model
161
285
  \`\`\`json
@@ -188,41 +312,46 @@ These are my proposed rule changes. Would you like to approve all of them, adjus
188
312
  </question>
189
313
 
190
314
  ## After approval
191
- When the developer approves (says "yes", "looks good", "go ahead", etc.), output the complete updated model inside <model> tags \u2014 the full JSON with tokens, components, and the revised rules array. Do NOT output partial models. Preserve tokens and components as-is unless the developer asks to change them.`}import{execFile as Vn}from"child_process";import{readFile as Rt}from"fs/promises";import{homedir as Qe}from"os";import{join as le}from"path";var Ke=/popmelt/i;function ye(){return{found:!1,name:null,scope:null,disabled:!1}}function we(s,t,n=!1){return{found:!0,name:s,scope:t,disabled:n}}function be(s){for(let t of Object.keys(s))if(Ke.test(t))return t;return null}async function Ae(s){try{let t=await Rt(s,"utf-8");return JSON.parse(t)}catch{return null}}async function Ve(s){let t=Qe(),n=await Ae(le(t,".claude.json"));if(n&&typeof n=="object"){let o=n;if(o.mcpServers&&typeof o.mcpServers=="object"){let i=be(o.mcpServers);if(i)return we(i,"user")}if(o.projects&&typeof o.projects=="object"){let a=o.projects[s];if(a&&typeof a=="object"){let c=a;if(c.mcpServers&&typeof c.mcpServers=="object"){let h=be(c.mcpServers);if(h){let v=Array.isArray(c.disabledMcpjsonServers)&&c.disabledMcpjsonServers.some(y=>Ke.test(y));return we(h,"project",v)}}}}}let e=await Ae(le(s,".mcp.json"));if(e&&typeof e=="object"){let o=e;if(o.mcpServers&&typeof o.mcpServers=="object"){let a=be(o.mcpServers);if(a){let c=await It(s,a);return we(a,"mcp.json",c)}}let i=be(o);if(i&&i!=="mcpServers"){let a=await It(s,i);return we(i,"mcp.json",a)}}return ye()}async function It(s,t){let n=le(s,".claude","settings.local.json"),e=await Ae(n);if(e&&typeof e=="object"){let o=e;if(Array.isArray(o.disabledMcpjsonServers))return o.disabledMcpjsonServers.some(i=>i===t)}return!1}var Zn=/^\[mcp_servers\.([^\]]+)\]/;function es(s){let t=s.split(`
192
- `),n=[],e=null;for(let o of t){let i=o.match(Zn);i?(e&&n.push({name:e.name,body:e.bodyLines.join(`
315
+ When the developer approves (says "yes", "looks good", "go ahead", etc.), output the complete updated model inside <model> tags \u2014 the full JSON with tokens, components, and the revised rules array. Do NOT output partial models. Preserve tokens and components as-is unless the developer asks to change them.`}import{execFile as js}from"child_process";import{readFile as lt}from"fs/promises";import{homedir as Ge}from"os";import{join as ue}from"path";var dt=/popmelt/i,Us=/chrome|devtools/i;function ye(){return{found:!1,name:null,scope:null,disabled:!1}}function be(s,t,n=!1){return{found:!0,name:s,scope:t,disabled:n}}function Ie(s){for(let t of Object.keys(s))if(dt.test(t))return t;return null}async function He(s){try{let t=await lt(s,"utf-8");return JSON.parse(t)}catch{return null}}async function ut(s){let t=Ge(),n=await He(ue(t,".claude.json"));if(n&&typeof n=="object"){let o=n;if(o.mcpServers&&typeof o.mcpServers=="object"){let i=Ie(o.mcpServers);if(i)return be(i,"user")}if(o.projects&&typeof o.projects=="object"){let a=o.projects[s];if(a&&typeof a=="object"){let c=a;if(c.mcpServers&&typeof c.mcpServers=="object"){let h=Ie(c.mcpServers);if(h){let P=Array.isArray(c.disabledMcpjsonServers)&&c.disabledMcpjsonServers.some(g=>dt.test(g));return be(h,"project",P)}}}}}let e=await He(ue(s,".mcp.json"));if(e&&typeof e=="object"){let o=e;if(o.mcpServers&&typeof o.mcpServers=="object"){let a=Ie(o.mcpServers);if(a){let c=await qt(s,a);return be(a,"mcp.json",c)}}let i=Ie(o);if(i&&i!=="mcpServers"){let a=await qt(s,i);return be(i,"mcp.json",a)}}return ye()}async function qt(s,t){let n=ue(s,".claude","settings.local.json"),e=await He(n);if(e&&typeof e=="object"){let o=e;if(Array.isArray(o.disabledMcpjsonServers))return o.disabledMcpjsonServers.some(i=>i===t)}return!1}var zs=/^\[mcp_servers\.([^\]]+)\]/;function Ls(s){return s.replace(/^"(.+)"$/,"$1")}function Ws(s){return/\.tools\./.test(s)||/"\.tools\./.test(s)}function Kt(s){let t=s.split(`
316
+ `),n=[],e=null;for(let o of t){let i=o.match(zs);i?(e&&n.push({name:e.name,body:e.bodyLines.join(`
193
317
  `)}),e={name:i[1],bodyLines:[]}):o.startsWith("[")?e&&(n.push({name:e.name,body:e.bodyLines.join(`
194
318
  `)}),e=null):e&&e.bodyLines.push(o)}return e&&n.push({name:e.name,body:e.bodyLines.join(`
195
- `)}),n}function ts(s){return/enabled\s*=\s*false/i.test(s)}async function Ze(s){let t=process.env.CODEX_HOME||le(Qe(),".codex"),n=await Mt(le(t,"config.toml"),"user");if(n.found)return n;let e=await Mt(le(s,".codex","config.toml"),"project");return e.found?e:ye()}async function et(s,t="copilot"){let n=await ns(t);if(n.found)return n;let e=process.env.COPILOT_HOME||le(Qe(),".copilot"),o=await Ye(le(e,"mcp-config.json"),"user");if(o.found)return o;let i=await Ye(le(s,".mcp.json"),"mcp.json");if(i.found)return i;let a=await Ye(le(s,".github","mcp.json"),"mcp.json");return a.found?a:ye()}async function ns(s){try{let t=await new Promise((n,e)=>{Vn(s,["mcp","list","--json"],{encoding:"utf-8",timeout:5e3},(o,i)=>{if(o){e(o);return}n(i)})});return Ct(JSON.parse(t),"user")}catch{return ye()}}async function Ye(s,t){let n=await Ae(s);return Ct(n,t)}function Ct(s,t){if(!s||typeof s!="object")return ye();let n=s,e=n.mcpServers&&typeof n.mcpServers=="object"?n.mcpServers:n.servers&&typeof n.servers=="object"?n.servers:n,o=be(e);return o?we(o,t):ye()}async function Mt(s,t){try{let n=await Rt(s,"utf-8"),e=es(n);for(let o of e)if(Ke.test(o.name)){let i=ts(o.body);return we(o.name,t,i)}}catch{}return ye()}import{execFile as $t}from"child_process";import{mkdir as ss,readFile as Ot,writeFile as _t}from"fs/promises";import{homedir as At}from"os";import{dirname as os,join as tt}from"path";var nt="https://mcp.popmelt.com/mcp";async function Dt(s=nt){let t=tt(At(),".claude.json"),n;try{let o=await Ot(t,"utf-8");n=JSON.parse(o)}catch{n={}}(!n.mcpServers||typeof n.mcpServers!="object")&&(n.mcpServers={});let e=n.mcpServers;for(let o of Object.keys(e))if(/popmelt/i.test(o))return{installed:!1,provider:"claude",scope:null,reason:"already_configured"};return e.popmelt={type:"http",url:s},await _t(t,JSON.stringify(n,null,2)+`
196
- `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function Nt(s=nt){let t=process.env.CODEX_HOME||tt(At(),".codex"),n=tt(t,"config.toml"),e;try{e=await Ot(n,"utf-8")}catch{e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await ss(os(n),{recursive:!0});let o=`
319
+ `)}),n}function Qt(s){return/enabled\s*=\s*false/i.test(s)}async function pt(s){let t=process.env.CODEX_HOME||ue(Ge(),".codex"),n=await Yt(ue(t,"config.toml"),"user");if(n.found)return n;let e=await Yt(ue(s,".codex","config.toml"),"project");return e.found?e:ye()}async function Hs(s,t){let n=process.env.CODEX_HOME||ue(Ge(),".codex"),e=new Set;for(let o of[ue(n,"config.toml"),ue(s,".codex","config.toml")])try{let i=await lt(o,"utf-8");for(let a of Kt(i)){if(Ws(a.name)||Qt(a.body))continue;let c=Ls(a.name);t.test(c)&&e.add(c)}}catch{}return[...e]}async function Vt(s){return Hs(s,Us)}async function ft(s,t="copilot"){let n=await Gs(t);if(n.found)return n;let e=process.env.COPILOT_HOME||ue(Ge(),".copilot"),o=await ct(ue(e,"mcp-config.json"),"user");if(o.found)return o;let i=await ct(ue(s,".mcp.json"),"mcp.json");if(i.found)return i;let a=await ct(ue(s,".github","mcp.json"),"mcp.json");return a.found?a:ye()}async function Gs(s){try{let t=await new Promise((n,e)=>{js(s,["mcp","list","--json"],{encoding:"utf-8",timeout:5e3},(o,i)=>{if(o){e(o);return}n(i)})});return Zt(JSON.parse(t),"user")}catch{return ye()}}async function ct(s,t){let n=await He(s);return Zt(n,t)}function Zt(s,t){if(!s||typeof s!="object")return ye();let n=s,e=n.mcpServers&&typeof n.mcpServers=="object"?n.mcpServers:n.servers&&typeof n.servers=="object"?n.servers:n,o=Ie(e);return o?be(o,t):ye()}async function Yt(s,t){try{let n=await lt(s,"utf-8"),e=Kt(n);for(let o of e)if(dt.test(o.name)){let i=Qt(o.body);return be(o.name,t,i)}}catch{}return ye()}import{execFile as en}from"child_process";import{mkdir as Xs,readFile as tn,writeFile as nn}from"fs/promises";import{homedir as sn}from"os";import{dirname as qs,join as ht}from"path";var mt="https://mcp.popmelt.com/mcp";async function on(s=mt){let t=ht(sn(),".claude.json"),n;try{let o=await tn(t,"utf-8");n=JSON.parse(o)}catch{n={}}(!n.mcpServers||typeof n.mcpServers!="object")&&(n.mcpServers={});let e=n.mcpServers;for(let o of Object.keys(e))if(/popmelt/i.test(o))return{installed:!1,provider:"claude",scope:null,reason:"already_configured"};return e.popmelt={type:"http",url:s},await nn(t,JSON.stringify(n,null,2)+`
320
+ `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function rn(s=mt){let t=process.env.CODEX_HOME||ht(sn(),".codex"),n=ht(t,"config.toml"),e;try{e=await tn(n,"utf-8")}catch{e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await Xs(qs(n),{recursive:!0});let o=`
197
321
  [mcp_servers.popmelt]
198
322
  url = "${s}"
199
- `;return await _t(n,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function Ft(s=nt,t="copilot"){let n=await rs(t);if(is(n))return{installed:!1,provider:"copilot",scope:null,reason:"already_configured"};try{return await new Promise((e,o)=>{$t(t,["mcp","add","--transport","http","--json","popmelt",s],{encoding:"utf-8",timeout:1e4},i=>{if(i){o(i);return}e()})}),{installed:!0,provider:"copilot",scope:"user"}}catch(e){return{installed:!1,provider:"copilot",scope:null,reason:e instanceof Error?e.message:"install_failed"}}}async function rs(s){try{let t=await new Promise((n,e)=>{$t(s,["mcp","list","--json"],{encoding:"utf-8",timeout:5e3},(o,i)=>{if(o){e(o);return}n(i)})});return JSON.parse(t)}catch{return null}}function is(s){if(!s||typeof s!="object")return!1;let t=s,n=t.mcpServers&&typeof t.mcpServers=="object"?t.mcpServers:t;return Object.keys(n).some(e=>/popmelt/i.test(e))}async function st(s){let n=(s.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!n)throw new Error("Missing multipart boundary");let e=n[1]||n[2],o=await as(s),i=Buffer.from(`--${e}`),a=Buffer.from(`--${e}--`),c,h,v,y,B,N,R,F,m,W,H,Y,q=[],G=[],I=0,z=[];for(;I<o.length;){let b=o.indexOf(i,I);if(b===-1)break;let _=b+i.length;if(o.slice(b,b+a.length).equals(a))break;let u=_;o[u]===13&&o[u+1]===10&&(u+=2);let j=o.indexOf(`\r
323
+ `;return await nn(n,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function an(s=mt,t="copilot"){let n=await Ys(t);if(Ks(n))return{installed:!1,provider:"copilot",scope:null,reason:"already_configured"};try{return await new Promise((e,o)=>{en(t,["mcp","add","--transport","http","--json","popmelt",s],{encoding:"utf-8",timeout:1e4},i=>{if(i){o(i);return}e()})}),{installed:!0,provider:"copilot",scope:"user"}}catch(e){return{installed:!1,provider:"copilot",scope:null,reason:e instanceof Error?e.message:"install_failed"}}}async function Ys(s){try{let t=await new Promise((n,e)=>{en(s,["mcp","list","--json"],{encoding:"utf-8",timeout:5e3},(o,i)=>{if(o){e(o);return}n(i)})});return JSON.parse(t)}catch{return null}}function Ks(s){if(!s||typeof s!="object")return!1;let t=s,n=t.mcpServers&&typeof t.mcpServers=="object"?t.mcpServers:t;return Object.keys(n).some(e=>/popmelt/i.test(e))}async function Xe(s){let n=(s.headers["content-type"]||"").match(/boundary=(?:"([^"]+)"|([^\s;]+))/);if(!n)throw new Error("Missing multipart boundary");let e=n[1]||n[2],o=await Qs(s),i=Buffer.from(`--${e}`),a=Buffer.from(`--${e}--`),c,h,P,g,B,z,O,L,S,X,W,Q,Z=[],V=[],q=0,D=[];for(;q<o.length;){let _=o.indexOf(i,q);if(_===-1)break;let T=_+i.length;if(o.slice(_,_+a.length).equals(a))break;let k=T;o[k]===13&&o[k+1]===10&&(k+=2);let y=o.indexOf(`\r
200
324
  \r
201
- `,u);if(j===-1)break;let E=o.slice(u,j).toString("utf-8"),w=j+4,C=o.indexOf(i,w),S=C!==-1?C-2:o.length;z.push({headers:E,body:o.slice(w,S)}),I=C!==-1?C:o.length}for(let b of z){let _=b.headers.match(/name="([^"]+)"/);if(!_)continue;let u=_[1];if(u==="screenshot")c=b.body;else if(u==="feedback")h=b.body.toString("utf-8");else if(u==="color")v=b.body.toString("utf-8");else if(u==="provider")y=b.body.toString("utf-8");else if(u==="model")B=b.body.toString("utf-8");else if(u==="goal")N=b.body.toString("utf-8");else if(u==="pageUrl")R=b.body.toString("utf-8");else if(u==="viewport")F=b.body.toString("utf-8");else if(u==="planId")m=b.body.toString("utf-8");else if(u==="manifest")W=b.body.toString("utf-8");else if(u==="tasks")H=b.body.toString("utf-8");else if(u==="sourceId")Y=b.body.toString("utf-8");else if(u.startsWith("screenshot-")){let j=u.slice(11);try{let E=decodeURIComponent(j);G.push({pathname:E,data:b.body})}catch{}}else if(u.startsWith("image-")){let j=u.split("-"),E=parseInt(j[j.length-1],10),w=j.slice(1,-1).join("-");w&&!isNaN(E)&&q.push({annotationId:w,index:E,data:b.body})}}if(!c)throw new Error("Missing screenshot field");return h||(h=""),{screenshot:c,feedback:h,color:v,provider:y,model:B,goal:N,pageUrl:R,viewport:F,planId:m,manifest:W,tasks:H,sourceId:Y,pastedImages:q,pageScreenshots:G}}function as(s){return new Promise((t,n)=>{let e=[];s.on("data",o=>e.push(o)),s.on("end",()=>t(Buffer.concat(e))),s.on("error",n)})}function Pe(s,t){let n=[];if(s.annotations.length>0){n.push("## Annotations");for(let e of s.annotations){let o=e.elements.map(c=>{let h=[c.selector];return c.reactComponent&&h.push(`(${c.reactComponent})`),h.join(" ")}).join(", "),i=e.instruction||"No text";n.push(`- id=${e.id} [${e.type}] ${i} \u2192 Elements: ${o||"none"}`);let a=t?.[e.id];if(a&&a.length>0)for(let c of a)n.push(` Attached image: use the Read tool to view ${c}`)}}if(s.styleModifications.length>0){n.push(""),n.push("## Style Changes (make permanent in source)"),n.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let e of s.styleModifications){let o=e.element?.reactComponent?`(${e.element.reactComponent})`:"";for(let i of e.changes)n.push(`- ${e.selector} ${o}: ${i.property} ${i.original} \u2192 ${i.modified}`)}}if(s.spacingTokenChanges?.length){n.push(""),n.push("## Spacing Token Changes"),n.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let e of s.spacingTokenChanges){n.push(`
325
+ `,k);if(y===-1)break;let C=o.slice(k,y).toString("utf-8"),N=y+4,x=o.indexOf(i,N),b=x!==-1?x-2:o.length;D.push({headers:C,body:o.slice(N,b)}),q=x!==-1?x:o.length}for(let _ of D){let T=_.headers.match(/name="([^"]+)"/);if(!T)continue;let k=T[1];if(k==="screenshot")c=_.body;else if(k==="feedback")h=_.body.toString("utf-8");else if(k==="color")P=_.body.toString("utf-8");else if(k==="provider")g=_.body.toString("utf-8");else if(k==="model")B=_.body.toString("utf-8");else if(k==="goal")z=_.body.toString("utf-8");else if(k==="pageUrl")O=_.body.toString("utf-8");else if(k==="viewport")L=_.body.toString("utf-8");else if(k==="planId")S=_.body.toString("utf-8");else if(k==="manifest")X=_.body.toString("utf-8");else if(k==="tasks")W=_.body.toString("utf-8");else if(k==="sourceId")Q=_.body.toString("utf-8");else if(k.startsWith("screenshot-")){let y=k.slice(11);try{let C=decodeURIComponent(y);V.push({pathname:C,data:_.body})}catch{}}else if(k.startsWith("image-")){let y=k.split("-"),C=parseInt(y[y.length-1],10),N=y.slice(1,-1).join("-");N&&!isNaN(C)&&Z.push({annotationId:N,index:C,data:_.body})}}if(!c)throw new Error("Missing screenshot field");return h||(h=""),{screenshot:c,feedback:h,color:P,provider:g,model:B,goal:z,pageUrl:O,viewport:L,planId:S,manifest:X,tasks:W,sourceId:Q,pastedImages:Z,pageScreenshots:V}}function Qs(s){return new Promise((t,n)=>{let e=[];s.on("data",o=>e.push(o)),s.on("end",()=>t(Buffer.concat(e))),s.on("error",n)})}function Me(s,t){let n=[];if(s.annotations.length>0){n.push("## Annotations");for(let e of s.annotations){let o=e.elements.map(c=>{let h=[c.selector];return c.reactComponent&&h.push(`(${c.reactComponent})`),h.join(" ")}).join(", "),i=e.instruction||"No text";n.push(`- id=${e.id} [${e.type}] ${i} \u2192 Elements: ${o||"none"}`);let a=t?.[e.id];if(a&&a.length>0)for(let c of a)n.push(` Attached image: use the Read tool to view ${c}`)}}if(s.styleModifications.length>0){n.push(""),n.push("## Style Changes (make permanent in source)"),n.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let e of s.styleModifications){let o=e.element?.reactComponent?`(${e.element.reactComponent})`:"";for(let i of e.changes)n.push(`- ${e.selector} ${o}: ${i.property} ${i.original} \u2192 ${i.modified}`)}}if(s.spacingTokenChanges?.length){n.push(""),n.push("## Spacing Token Changes"),n.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let e of s.spacingTokenChanges){n.push(`
202
326
  ### ${e.tokenName}: ${e.originalPx}px \u2192 ${e.newPx}px`);for(let o of e.affectedElements){let i=o.reactComponent?` (${o.reactComponent})`:"";o.matchedClass&&o.suggestedClass?n.push(`- ${o.selector}${i}: \`${o.matchedClass}\` \u2192 \`${o.suggestedClass}\``):n.push(`- ${o.selector}${i}: ${o.property} ${e.originalPx}px \u2192 ${e.newPx}px`),n.push(` class="${o.className}"`)}}}if(s.inspectedElement){let e=s.inspectedElement;n.push(""),n.push("## Inspected Element"),n.push("The developer has this element selected in the inspector:");let o=[e.selector];e.reactComponent&&o.push(`(${e.reactComponent})`),e.context&&o.push(`in ${e.context}`),e.textContent&&o.push(`"${e.textContent.slice(0,80)}"`),n.push(`- ${o.join(" ")}`)}return n.join(`
203
- `)}function Bt(s,t,n){let e=[],i=new Set(t.annotations.map(a=>a.pathname).filter(Boolean)).size>1;if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),!i&&n?.provider!=="codex"&&(e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`),e.push("")),e.push(`The developer annotated their running app at ${t.url} (${t.viewport.width}x${t.viewport.height}).`),n?.threadHistory&&n.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let a=0;for(let c of n.threadHistory)if(c.role==="human")a++,c.replyToQuestion?(e.push(`### Round ${a} (human) \u2014 reply`),e.push(`"${c.replyToQuestion}"`)):(e.push(`### Round ${a} (human)`),c.feedbackSummary&&e.push(`Annotations: ${c.feedbackSummary}`),c.annotationIds&&c.annotationIds.length>0&&e.push(`Annotation IDs: ${c.annotationIds.join(", ")}`));else if(c.question)e.push(`### Round ${a} (assistant) \u2014 question`),e.push(`"${c.question}"`);else{if(e.push(`### Round ${a} (assistant)`),c.responseText&&e.push(`Response: ${c.responseText}`),c.resolutions&&c.resolutions.length>0)for(let h of c.resolutions){let v=h.finalScope??h.inferredScope,y=v?` [${v.breadth} ${v.target}]`:"";e.push(`- ${h.annotationId}: ${h.status}${y} \u2014 ${h.summary}`),h.filesModified&&h.filesModified.length>0&&e.push(` Files: ${h.filesModified.join(", ")}`)}c.toolsUsed&&c.toolsUsed.length>0&&e.push(`Tools used: ${c.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(n?.designModel){e.push(""),e.push("## Established Design Policies"),e.push("This project has an established design model (stored in .popmelt/model.json), extracted from the developer's previous design decisions. When making changes, follow these patterns unless the developer explicitly overrides them. When asked about design tokens, component patterns, or design decisions, reference this model as the authoritative source.");let a=n.designModel.rules;if(Array.isArray(a)&&a.length>0)if(e.push(""),e.push("Rules:"),a.length>0&&typeof a[0]=="object"&&a[0]!==null&&"scope"in a[0]){let y=new Map;for(let B of a)if(typeof B=="object"&&B!==null&&"text"in B){let N=B,R=N.scope||"general";y.has(R)||y.set(R,[]),y.get(R).push(N.text)}for(let[B,N]of y){e.push(`**${B.charAt(0).toUpperCase()+B.slice(1)}**`);for(let R of N)e.push(`- ${R}`)}}else for(let y of a)typeof y=="string"&&e.push(`- ${y}`);let c=n.designModel.tokens;c&&typeof c=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(c,null,2)),e.push("```"));let h=n.designModel.components;h&&typeof h=="object"&&(e.push(""),e.push("Component patterns:"),e.push("```json"),e.push(JSON.stringify(h,null,2)),e.push("```")),e.push(""),e.push("### Novel Pattern Detection"),e.push("When you make a design decision that has no matching policy in the model above (e.g., styling a component type not yet in the model, choosing a color with no token, picking spacing with no rule), flag it:"),e.push("<novel>"),e.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),e.push("</novel>"),e.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),e.push("- `element`: What you are styling or creating"),e.push("- `decision`: What you decided to do (specific values)"),e.push("- `reason`: Why this is novel (what is missing from the model)"),e.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}if(n?.designModel||(e.push(""),e.push("## Design Context"),e.push("This project uses Popmelt for design governance. Design decisions are stored in .popmelt/decisions/ (JSON files). A materialized design model may exist at .popmelt/model.json. When the developer asks about design tokens, patterns, or past decisions, check these files first before searching source code.")),i){let a=n?.screenshotPaths??{},c=new Map;for(let h of t.annotations){let v=h.pathname||new URL(t.url).pathname;c.has(v)||c.set(v,[]),c.get(v).push(h)}e.push(""),e.push("The developer annotated multiple pages. Each page section includes its own screenshot where available.");for(let[h,v]of c){e.push(""),e.push(`## Page: ${h}`);let y=a[h];y&&n?.provider!=="codex"?e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${y}`):!y&&n?.provider!=="codex"&&(Object.keys(a).length===0?(e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${s}`),e.push("(This screenshot shows the page the developer was on when submitting \u2014 not this page)")):e.push("(No screenshot available for this page \u2014 work from the annotation text and selectors)"));let B={...t,annotations:v,styleModifications:t.styleModifications},N=Pe(B,n?.imagePaths);N&&(e.push(""),e.push(N))}}else{let a=Pe(t,n?.imagePaths);a&&(e.push(""),e.push(a))}return e.push(""),e.push("Follow the developer's instructions. If they ask for changes, apply them to the source files \u2014 the dev server has HMR so changes appear immediately. If they ask a question or request analysis, respond in text without modifying code."),e.push(""),e.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e.push(""),e.push("## Resolution"),e.push("After completing all work, output a resolution block listing what you did for each annotation:"),e.push("<resolution>"),e.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),e.push("</resolution>"),e.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),e.push(""),e.push("### Scope classification"),e.push("Each resolution MUST include scope fields:"),e.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),e.push("- `inferredScope`: What scope the change actually has, based on what you modified."),e.push("Scope has two dimensions:"),e.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),e.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),e.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),e.push("If you cannot confidently determine scope, set it to null."),e.push(""),e.push("## Questions"),e.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),e.push('<question>What do you mean by "..."?</question>'),e.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),e.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),e.join(`
204
- `)}function Jt(s){return s.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function Ut(s,t,n,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),n!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`);let i=t.find(a=>a.role==="human"&&a.feedbackContext);if(i?.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),t.length>0){o.push(""),o.push("## Conversation History");let a=0;for(let c of t)c.role==="human"?(a++,c.replyToQuestion?(o.push(`### Round ${a} (human) \u2014 reply`),o.push(`"${c.replyToQuestion}"`)):(o.push(`### Round ${a} (human)`),c.feedbackSummary&&o.push(`Annotations: ${c.feedbackSummary}`))):c.question?(o.push(`### Round ${a} (assistant) \u2014 question`),o.push(`"${c.question}"`)):(o.push(`### Round ${a} (assistant)`),c.responseText&&o.push(`Response: ${c.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 a of e)o.push(`Attached image: use the Read tool to view the image at: ${a}`)}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(`
205
- `)}function cs(s){if(typeof s!="object"||s===null)return!1;let t=s;return(t.breadth==="instance"||t.breadth==="pattern")&&(t.target==="element"||t.target==="component"||t.target==="token")}function ls(s){if(typeof s!="object"||s===null||typeof s.annotationId!="string"||s.status!=="resolved"&&s.status!=="needs_review"||typeof s.summary!="string")return!1;let t=s;for(let n of["declaredScope","inferredScope","finalScope"])if(t[n]!==void 0&&t[n]!==null&&!cs(t[n]))return!1;return!0}function jt(s){let t=s.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!t||!t[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(ls):[]}catch{return[]}}function Lt(s){let t=s.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!t?.[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;return(o.category==="token"||o.category==="component"||o.category==="element")&&typeof o.element=="string"&&typeof o.decision=="string"&&typeof o.reason=="string"}):[]}catch{return[]}}import{execFile as ds}from"child_process";import{readdir as us,readFile as ps,stat as fs}from"fs/promises";import{homedir as ke}from"os";import{join as de}from"path";var Ne=[{id:"claude-opus-4-6",label:"Opus 4.6",source:"static"},{id:"claude-sonnet-4-6",label:"Sonn 4.6",source:"static"}],Te=[{id:"gpt-5.4",label:"GPT 5.4",source:"static"},{id:"gpt-5.3-codex",label:"Codex 5.3",source:"static"},{id:"gpt-5.3-codex-spark",label:"Spark 5.3",source:"static"}],Ee={id:"",label:"Default",source:"cli"},hs=/^\s*-\s+"([^"]+)"/;function ms(s){let t=[],n=!1;for(let e of s.split(`
206
- `)){if(/^\s*`model`:/.test(e)){n=!0;continue}if(n&&/^\s*`[^`]+`:/.test(e))break;if(!n)continue;let o=e.match(hs);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:ot(i),source:"cli"})}return Fe([Ee,...t])}function gs(s){let t=JSON.parse(s);return(Array.isArray(t.models)?t.models:[]).filter(e=>e.visibility===void 0||e.visibility==="list").sort((e,o)=>{let i=typeof e.priority=="number"?e.priority:Number.MAX_SAFE_INTEGER,a=typeof o.priority=="number"?o.priority:Number.MAX_SAFE_INTEGER;return i-a}).flatMap(e=>{let o=typeof e.slug=="string"?e.slug:typeof e.id=="string"?e.id:"";if(!o)return[];let i=typeof e.display_name=="string"&&e.display_name.trim()?e.display_name.trim():ot(o);return[{id:o,label:i,source:"cli"}]})}function ys(s){let t=s.match(/\bclaude-[a-z0-9]+(?:[-.][a-z0-9]+)*\b/gi)??[];return Fe(t.map(n=>({id:n,label:ot(n),source:"cli"})))}function ot(s){if(!s)return"Default";let t={"gpt-5.5":"GPT 5.5","gpt-5.4":"GPT 5.4","gpt-5.4-mini":"Mini 5.4","gpt-5.3-codex":"Codex 5.3","gpt-5.3-codex-spark":"Spark 5.3","gpt-5.2-codex":"Codex 5.2","gpt-5.2":"GPT 5.2","gpt-5.1":"GPT 5.1","gpt-5-mini":"Mini 5","gpt-4.1":"GPT 4.1","claude-opus-4.7":"Opus 4.7","claude-opus-4.6":"Opus 4.6","claude-opus-4.6-fast":"Opus 4.6 Fast","claude-opus-4.5":"Opus 4.5","claude-sonnet-4.6":"Sonn 4.6","claude-sonnet-4.5":"Sonn 4.5","claude-sonnet-4":"Sonn 4","claude-haiku-4.5":"Haiku 4.5","claude-fable":"Fable"},n=s.replace(/-\d{8,}$/,"");if(t[s])return t[s];if(t[n])return t[n];let e=n.match(/^claude-(opus|sonnet|haiku|fable)(?:-(\d+)[-.](\d+))?(?:-(fast))?$/);if(e){let i=e[1]==="sonnet"?"Sonn":xs(e[1]),a=e[2]&&e[3]?` ${e[2]}.${e[3]}`:"";return`${i}${a}${e[4]?" Fast":""}`}let o=n.match(/^gpt-(\d+)(?:[.-](\d+))?(?:-(mini|codex|spark))?$/);if(o){let i=o[2]?`${o[1]}.${o[2]}`:o[1];return o[3]==="mini"?`Mini ${i}`:o[3]==="codex"?`Codex ${i}`:o[3]==="spark"?`Spark ${i}`:`GPT ${i}`}return n}async function zt(s="copilot"){let t=await De(s,["help","config"]),n=ms(t);return n.length>1?n:[Ee]}async function Wt(s="codex"){let t=await De(s,["debug","models"]).catch(async()=>{let e=process.env.CODEX_HOME||de(ke(),".codex");return Gt(de(e,"models_cache.json"))}),n=gs(t);return n.length>0?Fe([...n,...Te]):Te}async function Ht(s="claude"){let t=de(ke(),".claude"),n=[de(ke(),".claude","stats-cache.json"),de(ke(),".claude","settings.json"),de(ke(),".claude.json"),de(t,"cache","changelog.md")],[e,o,i]=await Promise.all([De(s,["--help"]).catch(()=>""),De(s,["--version"]).catch(()=>""),ws(t).catch(()=>[])]),a=Ss([...n,...i]),c=await Promise.all(a.map(v=>Gt(v).catch(()=>""))),h=ys([e,o,...c].join(`
207
- `));return Fe([...Ne,...h])}function De(s,t,n=5e3){return new Promise((e,o)=>{ds(s,t,{encoding:"utf-8",timeout:n},(i,a)=>{if(i){o(i);return}e(a)})})}async function Gt(s){return ps(s,"utf-8")}async function ws(s){let t=[de(s,"projects"),de(s,"telemetry")],n=[];for(let e of t)await qt(e,n,4);return n.sort((e,o)=>o.mtimeMs-e.mtimeMs).slice(0,80).map(e=>e.path)}async function qt(s,t,n){if(n<0)return;let e;try{e=await us(s,{withFileTypes:!0})}catch{return}await Promise.all(e.map(async o=>{let i=de(s,o.name);if(o.isDirectory()){await qt(i,t,n-1);return}if(!(!o.isFile()||!vs(o.name)))try{let a=await fs(i);if(a.size>2e6)return;t.push({path:i,mtimeMs:a.mtimeMs})}catch{}}))}function vs(s){return s.endsWith(".json")||s.endsWith(".jsonl")||s.endsWith(".md")||s.endsWith(".txt")}function Fe(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Ss(s){return[...new Set(s)]}function xs(s){return s.charAt(0).toUpperCase()+s.slice(1)}var Be=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let a=n<0?e.events:e.events.filter(c=>c.seq>n);return{jobId:t,events:a,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,a=()=>{i||(i=!0,o())};e.on("exit",a),e.on("error",a),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(a,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as bs,readFile as Ps,writeFile as ks}from"fs/promises";import{dirname as Ts,join as Es}from"path";var Is={version:1,threads:{}},Je=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Es(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await Ps(this.filePath,"utf-8"),n=JSON.parse(t);if(n&&n.version===1&&n.threads)return this.cache=n,this.cache}catch{}return this.cache={...Is,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(a=>e.has(a)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let a of n)i.has(a)||o.elementIdentifiers.push(a);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let a=o.messages.find(c=>c.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:a,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await bs(Ts(this.filePath),{recursive:!0}),await ks(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var Ue="0.7.0";var Js=1111,Us=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],js=1800*1e3,Ls=3600*1e3;function Kt(s){if(!s)return!1;try{let t=new URL(s);return t.hostname==="localhost"||t.hostname==="127.0.0.1"}catch{return!1}}function zs(s){let t=[];for(let n of s)if(n.type==="delta"){let e=n.text;if(!e)continue;let o=t[t.length-1];o&&o.kind==="text"?o.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",o=n.file??void 0,i=n.content??void 0,a=o?o.split("/").pop()??o:void 0,c;switch(e){case"Read":c=a?`Reading ${a}`:"Reading file";break;case"Edit":c=a?`Editing ${a}`:"Editing file";break;case"Write":c=a?`Writing ${a}`:"Writing file";break;case"Bash":c=i?i.split(`
208
- `)[0].trim().slice(0,60):"Running command";break;case"Glob":c="Searching files";break;case"Grep":c="Searching code";break;case"WebFetch":c="Fetching page";break;case"WebSearch":c="Searching web";break;default:c=e?`Using ${e}`:"tool";break}let h=o??i??void 0,v=t[t.length-1];v&&v.kind==="tool_group"&&v.tool===e?v.items.push({label:c,detail:h}):t.push({kind:"tool_group",tool:e,items:[{label:c,detail:h}]})}return t}function Ws(s,t){let n=s.headers.origin;Kt(n)&&(t.setHeader("Access-Control-Allow-Origin",n),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function P(s,t,n){s.writeHead(t,{"Content-Type":"application/json"}),s.end(JSON.stringify(n))}function Hs(s,t){if(!s)return t;let n=s.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!n)return t;let[,e,o,i]=n;return`\x1B[38;2;${parseInt(e,16)};${parseInt(o,16)};${parseInt(i,16)}m${t}\x1B[0m`}function rt(s,t){try{s.res.write(`event: ${t.type}
327
+ `)}function cn(s,t,n){let e=[],i=new Set(t.annotations.map(a=>a.pathname).filter(Boolean)).size>1;if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),!i&&n?.provider!=="codex"&&(e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`),e.push("")),e.push(`The developer annotated their running app at ${t.url} (${t.viewport.width}x${t.viewport.height}).`),n?.threadHistory&&n.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let a=0;for(let c of n.threadHistory)if(c.role==="human")a++,c.replyToQuestion?(e.push(`### Round ${a} (human) \u2014 reply`),e.push(`"${c.replyToQuestion}"`)):(e.push(`### Round ${a} (human)`),c.feedbackSummary&&e.push(`Annotations: ${c.feedbackSummary}`),c.annotationIds&&c.annotationIds.length>0&&e.push(`Annotation IDs: ${c.annotationIds.join(", ")}`));else if(c.question)e.push(`### Round ${a} (assistant) \u2014 question`),e.push(`"${c.question}"`);else{if(e.push(`### Round ${a} (assistant)`),c.responseText&&e.push(`Response: ${c.responseText}`),c.resolutions&&c.resolutions.length>0)for(let h of c.resolutions){let P=h.finalScope??h.inferredScope,g=P?` [${P.breadth} ${P.target}]`:"";e.push(`- ${h.annotationId}: ${h.status}${g} \u2014 ${h.summary}`),h.filesModified&&h.filesModified.length>0&&e.push(` Files: ${h.filesModified.join(", ")}`)}c.toolsUsed&&c.toolsUsed.length>0&&e.push(`Tools used: ${c.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(n?.designModel){e.push(""),e.push("## Established Design Policies"),e.push("This project has an established design model (stored in .popmelt/model.json), extracted from the developer's previous design decisions. When making changes, follow these patterns unless the developer explicitly overrides them. When asked about design tokens, component patterns, or design decisions, reference this model as the authoritative source.");let a=n.designModel.rules;if(Array.isArray(a)&&a.length>0)if(e.push(""),e.push("Rules:"),a.length>0&&typeof a[0]=="object"&&a[0]!==null&&"scope"in a[0]){let g=new Map;for(let B of a)if(typeof B=="object"&&B!==null&&"text"in B){let z=B,O=z.scope||"general";g.has(O)||g.set(O,[]),g.get(O).push(z.text)}for(let[B,z]of g){e.push(`**${B.charAt(0).toUpperCase()+B.slice(1)}**`);for(let O of z)e.push(`- ${O}`)}}else for(let g of a)typeof g=="string"&&e.push(`- ${g}`);let c=n.designModel.tokens;c&&typeof c=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(c,null,2)),e.push("```"));let h=n.designModel.components;h&&typeof h=="object"&&(e.push(""),e.push("Component patterns:"),e.push("```json"),e.push(JSON.stringify(h,null,2)),e.push("```")),e.push(""),e.push("### Novel Pattern Detection"),e.push("When you make a design decision that has no matching policy in the model above (e.g., styling a component type not yet in the model, choosing a color with no token, picking spacing with no rule), flag it:"),e.push("<novel>"),e.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),e.push("</novel>"),e.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),e.push("- `element`: What you are styling or creating"),e.push("- `decision`: What you decided to do (specific values)"),e.push("- `reason`: Why this is novel (what is missing from the model)"),e.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}if(n?.designModel||(e.push(""),e.push("## Design Context"),e.push("This project uses Popmelt for design governance. Design decisions are stored in .popmelt/decisions/ (JSON files). A materialized design model may exist at .popmelt/model.json. When the developer asks about design tokens, patterns, or past decisions, check these files first before searching source code.")),i){let a=n?.screenshotPaths??{},c=new Map;for(let h of t.annotations){let P=h.pathname||new URL(t.url).pathname;c.has(P)||c.set(P,[]),c.get(P).push(h)}e.push(""),e.push("The developer annotated multiple pages. Each page section includes its own screenshot where available.");for(let[h,P]of c){e.push(""),e.push(`## Page: ${h}`);let g=a[h];g&&n?.provider!=="codex"?e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${g}`):!g&&n?.provider!=="codex"&&(Object.keys(a).length===0?(e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${s}`),e.push("(This screenshot shows the page the developer was on when submitting \u2014 not this page)")):e.push("(No screenshot available for this page \u2014 work from the annotation text and selectors)"));let B={...t,annotations:P,styleModifications:t.styleModifications},z=Me(B,n?.imagePaths);z&&(e.push(""),e.push(z))}}else{let a=Me(t,n?.imagePaths);a&&(e.push(""),e.push(a))}return e.push(""),e.push("Follow the developer's instructions. If they ask for changes, apply them to the source files \u2014 the dev server has HMR so changes appear immediately. If they ask a question or request analysis, respond in text without modifying code."),e.push(""),e.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e.push(""),e.push("## Resolution"),e.push("After completing all work, output a resolution block listing what you did for each annotation:"),e.push("<resolution>"),e.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),e.push("</resolution>"),e.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),e.push(""),e.push("### Scope classification"),e.push("Each resolution MUST include scope fields:"),e.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),e.push("- `inferredScope`: What scope the change actually has, based on what you modified."),e.push("Scope has two dimensions:"),e.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),e.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),e.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),e.push("If you cannot confidently determine scope, set it to null."),e.push(""),e.push("## Questions"),e.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),e.push('<question>What do you mean by "..."?</question>'),e.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),e.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),e.join(`
328
+ `)}function ln(s){return s.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function dn(s,t,n,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),n!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`);let i=t.find(a=>a.role==="human"&&a.feedbackContext);if(i?.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),t.length>0){o.push(""),o.push("## Conversation History");let a=0;for(let c of t)c.role==="human"?(a++,c.replyToQuestion?(o.push(`### Round ${a} (human) \u2014 reply`),o.push(`"${c.replyToQuestion}"`)):(o.push(`### Round ${a} (human)`),c.feedbackSummary&&o.push(`Annotations: ${c.feedbackSummary}`))):c.question?(o.push(`### Round ${a} (assistant) \u2014 question`),o.push(`"${c.question}"`)):(o.push(`### Round ${a} (assistant)`),c.responseText&&o.push(`Response: ${c.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 a of e)o.push(`Attached image: use the Read tool to view the image at: ${a}`)}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(`
329
+ `)}function Vs(s){if(typeof s!="object"||s===null)return!1;let t=s;return(t.breadth==="instance"||t.breadth==="pattern")&&(t.target==="element"||t.target==="component"||t.target==="token")}function Zs(s){if(typeof s!="object"||s===null||typeof s.annotationId!="string"||s.status!=="resolved"&&s.status!=="needs_review"||typeof s.summary!="string")return!1;let t=s;for(let n of["declaredScope","inferredScope","finalScope"])if(t[n]!==void 0&&t[n]!==null&&!Vs(t[n]))return!1;return!0}function un(s){let t=s.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!t||!t[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(Zs):[]}catch{return[]}}function pn(s){let t=s.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!t?.[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;return(o.category==="token"||o.category==="component"||o.category==="element")&&typeof o.element=="string"&&typeof o.decision=="string"&&typeof o.reason=="string"}):[]}catch{return[]}}import{execFile as eo}from"child_process";import{readdir as to,readFile as no,stat as so}from"fs/promises";import{homedir as Ce}from"os";import{join as me}from"path";var Ye=[{id:"claude-opus-4-6",label:"Opus 4.6",source:"static"},{id:"claude-sonnet-4-6",label:"Sonn 4.6",source:"static"}],$e=[{id:"gpt-5.4",label:"GPT 5.4",source:"static"},{id:"gpt-5.3-codex",label:"Codex 5.3",source:"static"},{id:"gpt-5.3-codex-spark",label:"Spark 5.3",source:"static"}],_e={id:"",label:"Default",source:"cli"},oo=/^\s*-\s+"([^"]+)"/;function ro(s){let t=[],n=!1;for(let e of s.split(`
330
+ `)){if(/^\s*`model`:/.test(e)){n=!0;continue}if(n&&/^\s*`[^`]+`:/.test(e))break;if(!n)continue;let o=e.match(oo);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:gt(i),source:"cli"})}return Ke([_e,...t])}function io(s){let t=JSON.parse(s);return(Array.isArray(t.models)?t.models:[]).filter(e=>e.visibility===void 0||e.visibility==="list").sort((e,o)=>{let i=typeof e.priority=="number"?e.priority:Number.MAX_SAFE_INTEGER,a=typeof o.priority=="number"?o.priority:Number.MAX_SAFE_INTEGER;return i-a}).flatMap(e=>{let o=typeof e.slug=="string"?e.slug:typeof e.id=="string"?e.id:"";if(!o)return[];let i=typeof e.display_name=="string"&&e.display_name.trim()?e.display_name.trim():gt(o);return[{id:o,label:i,source:"cli"}]})}function ao(s){let t=s.match(/\bclaude-[a-z0-9]+(?:[-.][a-z0-9]+)*\b/gi)??[];return Ke(t.map(n=>({id:n,label:gt(n),source:"cli"})))}function gt(s){if(!s)return"Default";let t={"gpt-5.5":"GPT 5.5","gpt-5.4":"GPT 5.4","gpt-5.4-mini":"Mini 5.4","gpt-5.3-codex":"Codex 5.3","gpt-5.3-codex-spark":"Spark 5.3","gpt-5.2-codex":"Codex 5.2","gpt-5.2":"GPT 5.2","gpt-5.1":"GPT 5.1","gpt-5-mini":"Mini 5","gpt-4.1":"GPT 4.1","claude-opus-4.7":"Opus 4.7","claude-opus-4.6":"Opus 4.6","claude-opus-4.6-fast":"Opus 4.6 Fast","claude-opus-4.5":"Opus 4.5","claude-sonnet-4.6":"Sonn 4.6","claude-sonnet-4.5":"Sonn 4.5","claude-sonnet-4":"Sonn 4","claude-haiku-4.5":"Haiku 4.5","claude-fable":"Fable"},n=s.replace(/-\d{8,}$/,"");if(t[s])return t[s];if(t[n])return t[n];let e=n.match(/^claude-(opus|sonnet|haiku|fable)(?:-(\d+)[-.](\d+))?(?:-(fast))?$/);if(e){let i=e[1]==="sonnet"?"Sonn":po(e[1]),a=e[2]&&e[3]?` ${e[2]}.${e[3]}`:"";return`${i}${a}${e[4]?" Fast":""}`}let o=n.match(/^gpt-(\d+)(?:[.-](\d+))?(?:-(mini|codex|spark))?$/);if(o){let i=o[2]?`${o[1]}.${o[2]}`:o[1];return o[3]==="mini"?`Mini ${i}`:o[3]==="codex"?`Codex ${i}`:o[3]==="spark"?`Spark ${i}`:`GPT ${i}`}return n}async function fn(s="copilot"){let t=await qe(s,["help","config"]),n=ro(t);return n.length>1?n:[_e]}async function hn(s="codex"){let t=await qe(s,["debug","models"]).catch(async()=>{let e=process.env.CODEX_HOME||me(Ce(),".codex");return gn(me(e,"models_cache.json"))}),n=io(t);return n.length>0?Ke([...n,...$e]):$e}async function mn(s="claude"){let t=me(Ce(),".claude"),n=[me(Ce(),".claude","stats-cache.json"),me(Ce(),".claude","settings.json"),me(Ce(),".claude.json"),me(t,"cache","changelog.md")],[e,o,i]=await Promise.all([qe(s,["--help"]).catch(()=>""),qe(s,["--version"]).catch(()=>""),co(t).catch(()=>[])]),a=uo([...n,...i]),c=await Promise.all(a.map(P=>gn(P).catch(()=>""))),h=ao([e,o,...c].join(`
331
+ `));return Ke([...Ye,...h])}function qe(s,t,n=5e3){return new Promise((e,o)=>{eo(s,t,{encoding:"utf-8",timeout:n},(i,a)=>{if(i){o(i);return}e(a)})})}async function gn(s){return no(s,"utf-8")}async function co(s){let t=[me(s,"projects"),me(s,"telemetry")],n=[];for(let e of t)await vn(e,n,4);return n.sort((e,o)=>o.mtimeMs-e.mtimeMs).slice(0,80).map(e=>e.path)}async function vn(s,t,n){if(n<0)return;let e;try{e=await to(s,{withFileTypes:!0})}catch{return}await Promise.all(e.map(async o=>{let i=me(s,o.name);if(o.isDirectory()){await vn(i,t,n-1);return}if(!(!o.isFile()||!lo(o.name)))try{let a=await so(i);if(a.size>2e6)return;t.push({path:i,mtimeMs:a.mtimeMs})}catch{}}))}function lo(s){return s.endsWith(".json")||s.endsWith(".jsonl")||s.endsWith(".md")||s.endsWith(".txt")}function Ke(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function uo(s){return[...new Set(s)]}function po(s){return s.charAt(0).toUpperCase()+s.slice(1)}var Qe=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let a=n<0?e.events:e.events.filter(c=>c.seq>n);return{jobId:t,events:a,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,a=()=>{i||(i=!0,o())};e.on("exit",a),e.on("error",a),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(a,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as fo,readFile as ho,writeFile as mo}from"fs/promises";import{dirname as go,join as vo}from"path";var yo={version:1,threads:{}},Ve=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=vo(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await ho(this.filePath,"utf-8"),n=JSON.parse(t);if(n&&n.version===1&&n.threads)return this.cache=n,this.cache}catch{}return this.cache={...yo,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(a=>e.has(a)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let a of n)i.has(a)||o.elementIdentifiers.push(a);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let a=o.messages.find(c=>c.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:a,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await fo(go(this.filePath),{recursive:!0}),await mo(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var Ze="0.7.1";var To=1111,Io=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],Mo=1800*1e3,Co=3600*1e3;function Pn(s){if(!s)return!1;try{let t=new URL(s);return t.hostname==="localhost"||t.hostname.endsWith(".localhost")||t.hostname==="127.0.0.1"||t.hostname==="::1"||t.hostname==="[::1]"}catch{return!1}}function $o(s){let t=[];for(let n of s)if(n.type==="delta"){let e=n.text;if(!e)continue;let o=t[t.length-1];o&&o.kind==="text"?o.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",o=n.file??void 0,i=n.content??void 0,a=o?o.split("/").pop()??o:void 0,c;switch(e){case"Read":c=a?`Reading ${a}`:"Reading file";break;case"Edit":c=a?`Editing ${a}`:"Editing file";break;case"Write":c=a?`Writing ${a}`:"Writing file";break;case"Bash":c=i?i.split(`
332
+ `)[0].trim().slice(0,60):"Running command";break;case"Glob":c="Searching files";break;case"Grep":c="Searching code";break;case"WebFetch":c="Fetching page";break;case"WebSearch":c="Searching web";break;default:c=e?`Using ${e}`:"tool";break}let h=o??i??void 0,P=t[t.length-1];P&&P.kind==="tool_group"&&P.tool===e?P.items.push({label:c,detail:h}):t.push({kind:"tool_group",tool:e,items:[{label:c,detail:h}]})}return t}function _o(s,t){let n=s.headers.origin;Pn(n)&&(t.setHeader("Access-Control-Allow-Origin",n),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function v(s,t,n){s.writeHead(t,{"Content-Type":"application/json"}),s.end(JSON.stringify(n))}function Oo(s,t){if(!s)return t;let n=s.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!n)return t;let[,e,o,i]=n;return`\x1B[38;2;${parseInt(e,16)};${parseInt(o,16)};${parseInt(i,16)}m${t}\x1B[0m`}function vt(s,t){try{s.res.write(`event: ${t.type}
209
333
  data: ${JSON.stringify(t)}
210
334
 
211
- `)}catch{}}function ze(s){return s==="claude"||s==="codex"||s==="copilot"?s:void 0}async function We(s){try{let t=new AbortController,n=setTimeout(()=>t.abort(),500),e=await fetch(`http://127.0.0.1:${s}/status`,{signal:t.signal});return clearTimeout(n),e.ok?await e.json():null}catch{return null}}function Gs(s,t){return new Promise((n,e)=>{let o=a=>{s.removeListener("listening",i),e(a)},i=()=>{s.removeListener("error",o),n()};s.once("error",o),s.once("listening",i),s.listen(t,"127.0.0.1")})}async function Vt(s={}){let t=s.port??Js,n=s.projectRoot??process.cwd(),e=Rs("sha256").update(n).digest("hex").slice(0,12),o=s.devOrigin??(process.env.PORT?`http://localhost:${process.env.PORT}`:null),i=s.tempDir??ue(Ds(),"popmelt-bridge"),a=s.maxTurns??40,c=s.maxBudgetUsd??1,h=[...s.allowedTools??Us],v=s.claudePath??"claude",y=s.copilotPath??"copilot",B=s.provider??"claude",N=s.timeoutMs,R=s.restartMode??"embedded",F=t,m={},W={claude:v,codex:"codex",copilot:y};for(let[r,l]of Object.entries(W))try{let f=l.includes("/")?l:Ms("which",[l],{encoding:"utf-8"}).trim();m[r]={available:!0,path:f}}catch{m[r]={available:!1,path:null}}function H(r,l){return new Promise(f=>{let d=Xt(l,["--version"],{stdio:["ignore","ignore","ignore"]}),g=!1,p=J=>{g||(g=!0,f(J))},k=setTimeout(()=>{d.kill("SIGTERM"),p(!0)},5e3);d.on("error",()=>{clearTimeout(k),p(!1)}),d.on("close",J=>{clearTimeout(k),p(J===0)})})}await Promise.all([(async()=>{m.claude&&(m.claude.models=m.claude.available&&m.claude.path?await Ht(m.claude.path).catch(()=>Ne):Ne)})(),(async()=>{m.codex&&(m.codex.models=m.codex.available&&m.codex.path?await Wt(m.codex.path).catch(()=>Te):Te)})(),(async()=>{m.copilot&&(m.copilot.models=m.copilot.available&&m.copilot.path?await zt(m.copilot.path).catch(()=>[Ee]):[Ee])})()]);let[Y,q,G]=await Promise.all([Ve(n),Ze(n),et(n,m.copilot?.path??y)]);m.claude&&(m.claude.mcp=Y),m.codex&&(m.codex.mcp=q),m.copilot&&(m.copilot.mcp=G),Y.found&&Y.name&&h.push(`mcp__${Y.name}__*`),await Cs(i,{recursive:!0}),Qt(i).catch(()=>{});let I=new Be(1),z=new Set,b=new Je(n),_=new $e(n),u=new Oe(n,_,{claudePath:v,provider:B,timeoutMs:N,onEvent:r=>{for(let l of z)rt(l,r)}}),j=20,E=300*1e3,w=[],C=new Set,S=null,X;I.addListener((r,l,f)=>{for(let d of z)(!f||!d.sourceId||d.sourceId===f)&&rt(d,r)}),I.setProcessor(async r=>{let l=r._replyPrompt,f=r._replyImagePaths,d=r.provider??B,g;if(r.threadId){let M=await b.getThread(r.threadId);if(M){for(let $=M.messages.length-1;$>=0;$--)if(M.messages[$].sessionId){g=M.messages[$].sessionId;break}}}let p;if(g&&l){let M=(await b.getThread(r.threadId))?.messages.filter(ee=>ee.role==="human").pop();if(p=M?.replyToQuestion||M?.feedbackSummary||"",f&&f.length>0){p+=`
335
+ `)}catch{}}function Ae(s){return s==="claude"||s==="codex"||s==="copilot"?s:void 0}async function tt(s){try{let t=new AbortController,n=setTimeout(()=>t.abort(),500),e=await fetch(`http://127.0.0.1:${s}/status`,{signal:t.signal});return clearTimeout(n),e.ok?await e.json():null}catch{return null}}function Ao(s,t){return new Promise((n,e)=>{let o=a=>{s.removeListener("listening",i),e(a)},i=()=>{s.removeListener("error",o),n()};s.once("error",o),s.once("listening",i),s.listen(t,"127.0.0.1")})}function Do(s){return/^[A-Za-z0-9_-]+$/.test(s)?`mcp_servers.${/^[A-Za-z0-9_]+$/.test(s)?s:`"${s}"`}.default_tools_approval_mode="approve"`:null}function bn(s){try{let t=et("git",["rev-parse","HEAD"],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3}).trim(),n=["--",".",":(exclude).popmelt/**",":(exclude)**/.popmelt/**"],e=et("git",["status","--porcelain","--untracked-files=all",...n],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3,maxBuffer:1024*1024}),o=et("git",["diff","HEAD",...n],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3,maxBuffer:2*1024*1024});return kn("sha256").update(`${t}
336
+ ${e}
337
+ ${o}`).digest("hex").slice(0,16)}catch{return}}async function En(s={}){let t=s.port??To,n=s.projectRoot??process.cwd(),e=kn("sha256").update(n).digest("hex").slice(0,12),o=s.devOrigin??(process.env.PORT?`http://localhost:${process.env.PORT}`:null),i=s.tempDir??he(Po(),"popmelt-bridge"),a=s.maxTurns??40,c=s.maxBudgetUsd??1,h=[...s.allowedTools??Io],P=s.claudePath??"claude",g=s.copilotPath??"copilot",B=s.provider??"claude",z=s.timeoutMs,O=s.restartMode??"embedded",L=t,S={},X={claude:P,codex:"codex",copilot:g};for(let[r,l]of Object.entries(X))try{let p=l.includes("/")?l:et("which",[l],{encoding:"utf-8"}).trim();S[r]={available:!0,path:p}}catch{S[r]={available:!1,path:null}}function W(r,l){return new Promise(p=>{let u=yn(l,["--version"],{stdio:["ignore","ignore","ignore"]}),f=!1,d=J=>{f||(f=!0,p(J))},w=setTimeout(()=>{u.kill("SIGTERM"),d(!0)},5e3);u.on("error",()=>{clearTimeout(w),d(!1)}),u.on("close",J=>{clearTimeout(w),d(J===0)})})}await Promise.all([(async()=>{S.claude&&(S.claude.models=S.claude.available&&S.claude.path?await mn(S.claude.path).catch(()=>Ye):Ye)})(),(async()=>{S.codex&&(S.codex.models=S.codex.available&&S.codex.path?await hn(S.codex.path).catch(()=>$e):$e)})(),(async()=>{S.copilot&&(S.copilot.models=S.copilot.available&&S.copilot.path?await fn(S.copilot.path).catch(()=>[_e]):[_e])})()]);let[Q,Z,V]=await Promise.all([ut(n),pt(n),ft(n,S.copilot?.path??g)]),q=await Vt(n);S.claude&&(S.claude.mcp=Q),S.codex&&(S.codex.mcp=Z),S.copilot&&(S.copilot.mcp=V),Q.found&&Q.name&&h.push(`mcp__${Q.name}__*`),await wo(i,{recursive:!0}),xn(i).catch(()=>{});let D=new Qe(1),_=new Set,T=new Ve(n),k=new je(n),y=new Ue(n),C=new Le(n,k,{claudePath:P,provider:B,timeoutMs:z,onEvent:r=>{for(let l of _)vt(l,r)}}),N=20,x=300*1e3,b=[],H=new Map,E=null,ie;D.addListener((r,l,p)=>{for(let u of _)(!p||!u.sourceId||u.sourceId===p)&&vt(u,r)}),D.setProcessor(async r=>{let l=r._replyPrompt,p=r._replyImagePaths,u=r.provider??B,f;if(r.threadId){let R=await T.getThread(r.threadId);if(R){for(let I=R.messages.length-1;I>=0;I--)if(R.messages[I].sessionId){f=R.messages[I].sessionId;break}}}let d;if(f&&l){let R=(await T.getThread(r.threadId))?.messages.filter(se=>se.role==="human").pop();if(d=R?.replyToQuestion||R?.feedbackSummary||"",p&&p.length>0){d+=`
338
+
339
+ The developer attached reference images with their reply:`;for(let se of p)d+=`
340
+ Attached image: use the Read tool to view the image at: ${se}`}r.kind==="synthesize"||r.kind==="synthesize_evals"||r.kind==="run_eval"?d+=`
212
341
 
213
- The developer attached reference images with their reply:`;for(let ee of f)p+=`
214
- Attached image: use the Read tool to view the image at: ${ee}`}p+=`
342
+ Continue the project workflow from the previous instructions. If the developer approved the proposals, emit the final structured block requested earlier.`:d+=`
215
343
 
216
- 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(g)p=Pe(r.feedback,r.imagePaths)+`
344
+ After completing work, output a <resolution> block with declaredScope and inferredScope. If the developer corrected scope, set finalScope. If unclear, output a <question> block.`}else if(f)d=Me(r.feedback,r.imagePaths)+`
217
345
 
218
346
  Follow the developer's instructions. If they ask for changes, apply them to the source files.
219
347
 
220
- After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(d!=="codex"?`
348
+ After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(u!=="codex"?`
221
349
 
222
- IMPORTANT: First, use the Read tool to view the updated screenshot at: ${r.screenshotPath}`:"");else{let M=!l&&r.threadId?await b.getThreadHistory(r.threadId):void 0,$=l?null:await u.loadModel();p=l??Bt(r.screenshotPath,r.feedback,{threadHistory:M&&M.length>0?M:void 0,provider:d,imagePaths:r.imagePaths,designModel:$??void 0,screenshotPaths:r.screenshotPaths})}let k=Hs(r.color,`[\u22B9 ${F}:${r.id}]`),J=r.screenshotPaths&&Object.keys(r.screenshotPaths).length>0?`${Object.keys(r.screenshotPaths).length} pages [${Object.keys(r.screenshotPaths).join(", ")}]`:r.screenshotPath;console.log(`${k} Reviewing ${J} (provider: ${d})${r.threadId?` (thread: ${r.threadId})`:""}${g?` (resuming: ${g.slice(0,8)})`:""}`);let L=(M,$)=>{M.type==="delta"&&"text"in M?I.accumulateText($,"response",M.text):M.type==="thinking"&&"text"in M&&I.accumulateText($,"thinking",M.text),I.broadcast(M,$,r.sourceId)},{process:ne,result:K}=d==="codex"?Ce(r.id,{prompt:p,projectRoot:n,screenshotPath:r.screenshotPath,resumeSessionId:g,model:r.model,onEvent:L}):d==="copilot"?bt(r.id,{prompt:p,projectRoot:n,resumeSessionId:g,model:r.model,timeoutMs:N,copilotPath:m.copilot?.path??y,onEvent:L}):Me(r.id,{prompt:p,projectRoot:n,maxTurns:a,maxBudgetUsd:c,allowedTools:h,claudePath:v,resumeSessionId:g,model:r.model,timeoutMs:N,onEvent:L});I.setActiveProcess(r.id,ne);let x=await K;if(r.result=x.text,x.success){console.log(`${k} Iteration complete`),x.fileEdits&&x.fileEdits.length>0&&console.log(`${k} Captured ${x.fileEdits.length} file edit(s): ${x.fileEdits.map(D=>`${D.tool} ${D.file_path}`).join(", ")}`),x.editEvents&&x.editEvents.length>0&&console.log(`${k} Captured ${x.editEvents.length} edit event(s): ${x.editEvents.map(D=>`${D.operation} ${D.filePath}`).join(", ")}`),r.status="done";let M=Jt(x.text),$=jt(x.text);if($.length>0&&r.annotationIds&&r.annotationIds.length>0){let D=new Set(r.annotationIds);$.every(U=>D.has(U.annotationId))||($=$.map((U,te)=>({...U,annotationId:r.annotationIds[te%r.annotationIds.length]})))}let ee=x.fileEdits&&x.fileEdits.length>0?x.fileEdits.map(D=>`${D.tool} ${D.file_path.split("/").pop()}`):x.toolsUsed,re=I.getBufferedEvents(r.id),pe=re?zs(re.events):void 0;if(r.threadId&&await b.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,responseText:x.text,resolutions:$.length>0?$:void 0,question:M??void 0,sessionId:x.sessionId,toolsUsed:ee,editEvents:x.editEvents&&x.editEvents.length>0?x.editEvents:void 0,segments:pe&&pe.length>0?pe:void 0,model:r.model,provider:d}),_.captureGitDiff(n).then(async D=>{let A=Date.now(),U=r.imagePaths?Object.values(r.imagePaths).flat():[],te=[];if(r.imagePaths)for(let[me,O]of Object.entries(r.imagePaths))for(let se=0;se<O.length;se++)te.push(`screenshots/p-${r.id}-${me}-${se}.webp`);await _.persist({version:1,id:r.id,createdAt:r.createdAt,completedAt:A,durationMs:A-r.createdAt,url:r.feedback.url,viewport:r.feedback.viewport,screenshotPath:`screenshots/s-${r.id}.webp`,pastedImagePaths:te,annotations:r.feedback.annotations,styleModifications:r.feedback.styleModifications,inspectedElement:r.feedback.inspectedElement,provider:d,model:r.model,sessionId:x.sessionId,threadId:r.threadId,responseText:x.text,resolutions:$.length>0?$:[],question:M??void 0,fileEdits:x.fileEdits??[],editEvents:x.editEvents??[],toolsUsed:ee,gitDiff:D},r.screenshotPath,U)}).catch(()=>{}),$.length>0){let D=$.some(A=>(A.finalScope??A.inferredScope)?.breadth==="pattern");D&&d!=="copilot"?u.run({provider:d,model:r.model}).catch(()=>{}):D&&console.log(`${k} Skipping materialization for Copilot provider`)}if(r.kind==="synthesize"&&x.text){let D=_e(x.text);if(D){Array.isArray(D.rules)&&(D.rules=xe(D.rules));let A=await u.loadModel();A&&(!D.tokens&&A.tokens&&(D.tokens=A.tokens),!D.components&&A.components&&(D.components=A.components)),await u.writeModel(D),console.log(`${k} Synthesize: model.json updated`)}}M&&(console.log(`${k} \u{1F4AC} Question detected: "${M.slice(0,120)}" \u2192 broadcasting to ${z.size} SSE clients (threadId=${r.threadId??r.id}, annotationIds=${r.annotationIds?.join(",")??"none"})`),I.broadcast({type:"question",jobId:r.id,threadId:r.threadId??r.id,question:M,annotationIds:r.annotationIds},r.id,r.sourceId));let ce=Lt(x.text);ce.length>0&&(console.log(`${k} Novel pattern(s): ${ce.map(D=>`${D.category}/${D.element}`).join(", ")}`),I.broadcast({type:"novel_patterns",jobId:r.id,patterns:ce,threadId:r.threadId},r.id,r.sourceId)),I.broadcast({type:"done",jobId:r.id,success:!0,resolutions:$.length>0?$:void 0,responseText:x.text,threadId:r.threadId},r.id,r.sourceId),w.push({id:r.id,status:"done",completedAt:Date.now(),threadId:r.threadId,annotationIds:r.annotationIds})}else console.error(`${k} Error: ${x.error}`),r.status="error",r.error=x.error,r.threadId&&await b.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,error:x.error||"Unknown error",model:r.model,provider:d}),I.broadcast({type:"error",jobId:r.id,threadId:r.threadId,message:x.error||"Unknown error",provider:d,model:r.model,recoverable:x.recoverableError,code:x.errorCode},r.id,r.sourceId),w.push({id:r.id,status:"error",completedAt:Date.now(),error:x.error,threadId:r.threadId,annotationIds:r.annotationIds});let V=Date.now()-E;for(;w.length>0&&(w[0].completedAt<V||w.length>j);)w.shift()});let T=As(async(r,l)=>{if(Ws(r,l),r.method==="OPTIONS"){l.writeHead(204),l.end();return}let f=r.url||"/",d=f.split("?")[0],g=new URL(f,`http://127.0.0.1:${F}`),p=g.pathname;try{if(r.method==="POST"&&p==="/send")await Z(r,l);else if(r.method==="GET"&&p==="/events")oe(r,l);else if(r.method==="GET"&&p==="/status")Zt(l);else if(r.method==="PATCH"&&p==="/config")await en(r,l);else if(r.method==="POST"&&p==="/shutdown"){if(R!=="detached"){P(l,409,{ok:!1,error:"This Popmelt bridge is embedded in the host dev server. Restart the dev server to reload bridge code.",restartMode:R});return}P(l,200,{ok:!0}),setTimeout(()=>process.exit(0),100)}else if(r.method==="POST"&&p==="/restart"){if(R!=="detached"){P(l,409,{ok:!1,error:"This Popmelt bridge is embedded in the host dev server. Restart the dev server to reload bridge code.",restartMode:R});return}P(l,200,{ok:!0,restarting:!0}),setTimeout(()=>{for(let k of z)try{k.res.end()}catch{}T.close(()=>{Xt(process.execPath,process.argv.slice(1),{detached:!0,stdio:"ignore",cwd:process.cwd(),env:process.env}).unref(),setTimeout(()=>process.exit(0),200)})},100)}else if(r.method==="GET"&&p==="/capabilities")P(l,200,{providers:m});else if(r.method==="POST"&&p==="/mcp/install")await on(r,l);else if(r.method==="POST"&&p==="/reply")await ae(r,l);else if(r.method==="POST"&&p==="/cancel")tn(r,l);else if(r.method==="POST"&&p==="/materialize")await nn(r,l);else if(r.method==="POST"&&p==="/model/component")await rn(r,l);else if(r.method==="DELETE"&&p==="/model/component")await an(r,l);else if(r.method==="PATCH"&&p==="/model/token")await cn(r,l);else if(r.method==="DELETE"&&p==="/model/token")await ln(r,l);else if(r.method==="POST"&&p==="/model/consolidate"){let{provider:k,model:J}=await it(r);if(k==="copilot"){P(l,400,{error:"Model consolidation is not available for Copilot yet."});return}u.consolidate({provider:k,model:J}).catch(L=>console.error("[Bridge] Consolidation error:",L)),P(l,200,{started:!0})}else if(r.method==="POST"&&p==="/model/synthesize")await sn(r,l);else if(r.method==="GET"&&p==="/model"){let k=await u.loadModel();P(l,200,{model:k})}else if(r.method==="GET"&&p.startsWith("/jobs/")&&p.endsWith("/events")){let k=p.slice(6,p.length-7),J=parseInt(g.searchParams.get("afterSeq")??"-1",10),L=I.getBufferedEvents(k,isNaN(J)?-1:J);L?P(l,200,L):P(l,404,{error:"Unknown job"})}else if(r.method==="GET"&&d.startsWith("/files/"))await fn(d.slice(7),l);else if(r.method==="GET"&&p==="/threads/recent"){let k=Math.min(Math.max(parseInt(g.searchParams.get("limit")??"5",10)||5,1),20),J=await b.listRecent(k);P(l,200,J)}else if(r.method==="GET"&&p.startsWith("/thread/")){let k=p.slice(8);await hn(k,l)}else r.method==="GET"&&(p==="/canvas"||p==="/canvas/")?dn(r,l):r.method==="GET"&&p==="/canvas/manifest"?await un(l):r.method==="GET"&&p==="/canvas/app.mjs"?await pn(l):P(l,404,{error:"Not found"})}catch(k){console.error("[Bridge] Request error:",k),P(l,500,{error:k instanceof Error?k.message:"Internal error"})}});async function Z(r,l){let{screenshot:f,feedback:d,color:g,provider:p,model:k,sourceId:J,pastedImages:L,pageScreenshots:ne}=await st(r),K;try{K=JSON.parse(d)}catch{P(l,400,{error:"Invalid feedback JSON"});return}let x=je().slice(0,8),V={};if(ne.length>0)for(let O of ne){let se=encodeURIComponent(O.pathname),fe=ue(i,`screenshot-${x}-${se}.webp`);await Le(fe,O.data),V[O.pathname]=fe}let M=ue(i,`screenshot-${x}.webp`);await Le(M,f);let $={};if(L.length>0)for(let O of L){let se=ue(i,`pasted-${x}-${O.annotationId}-${O.index}.webp`);await Le(se,O.data),$[O.annotationId]||($[O.annotationId]=[]),$[O.annotationId].push(se)}let ee=K.annotations.map(O=>O.linkedSelector?O.pathname?`${O.pathname}:${O.linkedSelector}`:O.linkedSelector:null).filter(O=>!!O),re;if(ee.length>0){let O=await b.findContinuationThread(ee);O?(re=O.id,await b.addElementIdentifiers(re,ee)):re=(await b.createThread(x,ee)).id}else re=(await b.createThread(x,[])).id;let pe=K.annotations.map(O=>O.id),ce=Object.keys(V).length>0,D={id:x,status:"queued",screenshotPath:M,feedback:K,createdAt:Date.now(),color:g,threadId:re,annotationIds:pe,provider:ze(p),model:k||void 0,...Object.keys($).length>0?{imagePaths:$}:{},sourceId:J||void 0,...ce?{screenshotPaths:V}:{}},A=new Set(K.annotations.map(O=>O.pathname).filter(Boolean)),U;if(A.size>1){let O=new Map;for(let se of K.annotations){let fe=se.pathname||"(unknown)";O.has(fe)||O.set(fe,[]),O.get(fe).push(se.instruction||`[${se.type}]`)}U=[...O.entries()].map(([se,fe])=>`\`${se}\`
223
- ${fe.map(gn=>`- ${gn}`).join(`
350
+ IMPORTANT: First, use the Read tool to view the updated screenshot at: ${r.screenshotPath}`:"");else{let R=!l&&r.threadId?await T.getThreadHistory(r.threadId):void 0,I=l?null:await C.loadModel();d=l??cn(r.screenshotPath,r.feedback,{threadHistory:R&&R.length>0?R:void 0,provider:u,imagePaths:r.imagePaths,designModel:I??void 0,screenshotPaths:r.screenshotPaths})}let w=Oo(r.color,`[\u22B9 ${L}:${r.id}]`),J=r.screenshotPaths&&Object.keys(r.screenshotPaths).length>0?`${Object.keys(r.screenshotPaths).length} pages [${Object.keys(r.screenshotPaths).join(", ")}]`:r.screenshotPath;console.log(`${w} Reviewing ${J} (provider: ${u})${r.threadId?` (thread: ${r.threadId})`:""}${f?` (resuming: ${f.slice(0,8)})`:""}`);let j=(R,I)=>{R.type==="delta"&&"text"in R?D.accumulateText(I,"response",R.text):R.type==="thinking"&&"text"in R&&D.accumulateText(I,"thinking",R.text),D.broadcast(R,I,r.sourceId)},{process:Y,result:F}=u==="codex"?Fe(r.id,{prompt:d,projectRoot:n,screenshotPath:r.screenshotPath,resumeSessionId:f,model:r.model,configOverrides:r.kind==="run_eval"?q.map(Do).filter(R=>typeof R=="string"):[],onEvent:j}):u==="copilot"?Ot(r.id,{prompt:d,projectRoot:n,resumeSessionId:f,model:r.model,timeoutMs:z,copilotPath:S.copilot?.path??g,onEvent:j}):Je(r.id,{prompt:d,projectRoot:n,maxTurns:a,maxBudgetUsd:c,allowedTools:h,claudePath:P,resumeSessionId:f,model:r.model,timeoutMs:z,onEvent:j});D.setActiveProcess(r.id,Y);let m=await F;if(r.result=m.text,m.success){console.log(`${w} Iteration complete`),m.fileEdits&&m.fileEdits.length>0&&console.log(`${w} Captured ${m.fileEdits.length} file edit(s): ${m.fileEdits.map($=>`${$.tool} ${$.file_path}`).join(", ")}`),m.editEvents&&m.editEvents.length>0&&console.log(`${w} Captured ${m.editEvents.length} edit event(s): ${m.editEvents.map($=>`${$.operation} ${$.filePath}`).join(", ")}`),r.status="done";let R=ln(m.text),I=un(m.text);if(I.length>0&&r.annotationIds&&r.annotationIds.length>0){let $=new Set(r.annotationIds);I.every(A=>$.has(A.annotationId))||(I=I.map((A,U)=>({...A,annotationId:r.annotationIds[U%r.annotationIds.length]})))}let se=m.fileEdits&&m.fileEdits.length>0?m.fileEdits.map($=>`${$.tool} ${$.file_path.split("/").pop()}`):m.toolsUsed,ee=D.getBufferedEvents(r.id),ae=ee?$o(ee.events):void 0;if(r.threadId&&await T.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,responseText:m.text,resolutions:I.length>0?I:void 0,question:R??void 0,sessionId:m.sessionId,toolsUsed:se,editEvents:m.editEvents&&m.editEvents.length>0?m.editEvents:void 0,segments:ae&&ae.length>0?ae:void 0,model:r.model,provider:u}),r.kind!=="run_eval"&&k.captureGitDiff(n).then(async $=>{let K=Date.now(),A=r.imagePaths?Object.values(r.imagePaths).flat():[],U=[];if(r.imagePaths)for(let[le,M]of Object.entries(r.imagePaths))for(let oe=0;oe<M.length;oe++)U.push(`screenshots/p-${r.id}-${le}-${oe}.webp`);await k.persist({version:1,id:r.id,createdAt:r.createdAt,completedAt:K,durationMs:K-r.createdAt,url:r.feedback.url,viewport:r.feedback.viewport,screenshotPath:`screenshots/s-${r.id}.webp`,pastedImagePaths:U,annotations:r.feedback.annotations,styleModifications:r.feedback.styleModifications,inspectedElement:r.feedback.inspectedElement,provider:u,model:r.model,sessionId:m.sessionId,threadId:r.threadId,responseText:m.text,resolutions:I.length>0?I:[],question:R??void 0,fileEdits:m.fileEdits??[],editEvents:m.editEvents??[],toolsUsed:se,gitDiff:$},r.screenshotPath,A)}).catch(()=>{}),I.length>0){let $=I.some(K=>(K.finalScope??K.inferredScope)?.breadth==="pattern");$&&u!=="copilot"?C.run({provider:u,model:r.model}).catch(()=>{}):$&&console.log(`${w} Skipping materialization for Copilot provider`)}if(r.kind==="synthesize"&&m.text){let $=We(m.text);if($){Array.isArray($.rules)&&($.rules=Te($.rules));let K=await C.loadModel();K&&(!$.tokens&&K.tokens&&($.tokens=K.tokens),!$.components&&K.components&&($.components=K.components)),await C.writeModel($),console.log(`${w} Synthesize: model.json updated`)}}else if(r.kind==="synthesize_evals"&&m.text){let $=Bt(m.text),K=Ft(m.text),A=$??K;if(A&&A.length>0){let U=await y.writeSynthesized(A);console.log(`${w} Eval synthesis: wrote ${A.length} ${$?"approved":"pending"} eval(s) \u2192 .popmelt/evals/evals.json (${U.cases.length} total)`)}}else if(r.kind==="run_eval"&&m.text&&r.evalId&&r.evalRunId){let $=await y.findCase(r.evalId),K=r._evalRunScope;if($&&K){let A=jt(m.text,$.assertions),U=Date.now();await y.writeRun({id:r.evalRunId,evalId:r.evalId,threadId:r.threadId,createdAt:r.createdAt,completedAt:U,durationMs:U-r.createdAt,scope:K,provider:u,model:r.model,screenshotPath:r._evalScreenshotPath,codeFingerprint:bn(n),rawResponse:m.text,...A}),console.log(`${w} Eval run: ${$.title} \u2192 ${A.status}`)}}R&&(console.log(`${w} \u{1F4AC} Question detected: "${R.slice(0,120)}" \u2192 broadcasting to ${_.size} SSE clients (threadId=${r.threadId??r.id}, annotationIds=${r.annotationIds?.join(",")??"none"})`),D.broadcast({type:"question",jobId:r.id,threadId:r.threadId??r.id,question:R,annotationIds:r.annotationIds},r.id,r.sourceId));let ce=pn(m.text);ce.length>0&&(console.log(`${w} Novel pattern(s): ${ce.map($=>`${$.category}/${$.element}`).join(", ")}`),D.broadcast({type:"novel_patterns",jobId:r.id,patterns:ce,threadId:r.threadId},r.id,r.sourceId)),D.broadcast({type:"done",jobId:r.id,success:!0,resolutions:I.length>0?I:void 0,responseText:m.text,threadId:r.threadId},r.id,r.sourceId),b.push({id:r.id,status:"done",completedAt:Date.now(),threadId:r.threadId,annotationIds:r.annotationIds})}else console.error(`${w} Error: ${m.error}`),r.status="error",r.error=m.error,r.threadId&&await T.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,error:m.error||"Unknown error",model:r.model,provider:u}),D.broadcast({type:"error",jobId:r.id,threadId:r.threadId,message:m.error||"Unknown error",provider:u,model:r.model,recoverable:m.recoverableError,code:m.errorCode},r.id,r.sourceId),b.push({id:r.id,status:"error",completedAt:Date.now(),error:m.error,threadId:r.threadId,annotationIds:r.annotationIds});let G=Date.now()-x;for(;b.length>0&&(b[0].completedAt<G||b.length>N);)b.shift()});let re=ko(async(r,l)=>{if(_o(r,l),r.method==="OPTIONS"){l.writeHead(204),l.end();return}let p=r.url||"/",u=p.split("?")[0],f=new URL(p,`http://127.0.0.1:${L}`),d=f.pathname;try{if(r.method==="POST"&&d==="/send")await ne(r,l);else if(r.method==="GET"&&d==="/events")Rn(r,l);else if(r.method==="GET"&&d==="/status")Tn(l);else if(r.method==="PATCH"&&d==="/config")await In(r,l);else if(r.method==="POST"&&d==="/shutdown"){if(O!=="detached"){v(l,409,{ok:!1,error:"This Popmelt bridge is embedded in the host dev server. Restart the dev server to reload bridge code.",restartMode:O});return}v(l,200,{ok:!0}),setTimeout(()=>process.exit(0),100)}else if(r.method==="POST"&&d==="/restart"){if(O!=="detached"){v(l,409,{ok:!1,error:"This Popmelt bridge is embedded in the host dev server. Restart the dev server to reload bridge code.",restartMode:O});return}v(l,200,{ok:!0,restarting:!0}),setTimeout(()=>{for(let w of _)try{w.res.end()}catch{}re.close(()=>{yn(process.execPath,process.argv.slice(1),{detached:!0,stdio:"ignore",cwd:process.cwd(),env:process.env}).unref(),setTimeout(()=>process.exit(0),200)})},100)}else if(r.method==="GET"&&d==="/capabilities")v(l,200,{providers:S});else if(r.method==="POST"&&d==="/mcp/install")await Nn(r,l);else if(r.method==="POST"&&d==="/reply")await De(r,l);else if(r.method==="POST"&&d==="/cancel")Mn(r,l);else if(r.method==="POST"&&d==="/materialize")await Cn(r,l);else if(r.method==="POST"&&d==="/model/component")await Jn(r,l);else if(r.method==="DELETE"&&d==="/model/component")await Bn(r,l);else if(r.method==="PATCH"&&d==="/model/token")await Fn(r,l);else if(r.method==="DELETE"&&d==="/model/token")await jn(r,l);else if(r.method==="POST"&&d==="/model/consolidate"){let{provider:w,model:J}=await nt(r);if(w==="copilot"){v(l,400,{error:"Model consolidation is not available for Copilot yet."});return}C.consolidate({provider:w,model:J}).catch(j=>console.error("[Bridge] Consolidation error:",j)),v(l,200,{started:!0})}else if(r.method==="POST"&&d==="/model/synthesize")await $n(r,l);else if(r.method==="POST"&&d==="/evals/synthesize")await _n(r,l);else if(r.method==="GET"&&d==="/evals"){let w=await y.loadSuiteWithCurrentRuns(bn(n));v(l,200,w)}else if(r.method==="POST"&&d.startsWith("/evals/")&&d.endsWith("/run")){let w=d.slice(7,-4);await On(w,r,l)}else if(r.method==="PATCH"&&d.startsWith("/evals/"))await An(d.slice(7),r,l);else if(r.method==="GET"&&d==="/model"){let w=await C.loadModel();v(l,200,{model:w})}else if(r.method==="GET"&&d.startsWith("/jobs/")&&d.endsWith("/events")){let w=d.slice(6,d.length-7),J=parseInt(f.searchParams.get("afterSeq")??"-1",10),j=D.getBufferedEvents(w,isNaN(J)?-1:J);j?v(l,200,j):v(l,404,{error:"Unknown job"})}else if(r.method==="GET"&&u.startsWith("/files/"))await Wn(u.slice(7),l);else if(r.method==="GET"&&d==="/threads/recent"){let w=Math.min(Math.max(parseInt(f.searchParams.get("limit")??"5",10)||5,1),20),J=await T.listRecent(w);v(l,200,J)}else if(r.method==="GET"&&d.startsWith("/thread/")){let w=d.slice(8);await Hn(w,l)}else r.method==="GET"&&(d==="/canvas"||d==="/canvas/")?Un(r,l):r.method==="GET"&&d==="/canvas/manifest"?await zn(l):r.method==="GET"&&d==="/canvas/app.mjs"?await Ln(l):v(l,404,{error:"Not found"})}catch(w){console.error("[Bridge] Request error:",w),v(l,500,{error:w instanceof Error?w.message:"Internal error"})}});async function ne(r,l){let{screenshot:p,feedback:u,color:f,provider:d,model:w,sourceId:J,pastedImages:j,pageScreenshots:Y}=await Xe(r),F;try{F=JSON.parse(u)}catch{v(l,400,{error:"Invalid feedback JSON"});return}let m=we().slice(0,8),G={};if(Y.length>0)for(let M of Y){let oe=encodeURIComponent(M.pathname),de=he(i,`screenshot-${m}-${oe}.webp`);await Oe(de,M.data),G[M.pathname]=de}let R=he(i,`screenshot-${m}.webp`);await Oe(R,p);let I={};if(j.length>0)for(let M of j){let oe=he(i,`pasted-${m}-${M.annotationId}-${M.index}.webp`);await Oe(oe,M.data),I[M.annotationId]||(I[M.annotationId]=[]),I[M.annotationId].push(oe)}let se=F.annotations.map(M=>M.linkedSelector?M.pathname?`${M.pathname}:${M.linkedSelector}`:M.linkedSelector:null).filter(M=>!!M),ee;if(se.length>0){let M=await T.findContinuationThread(se);M?(ee=M.id,await T.addElementIdentifiers(ee,se)):ee=(await T.createThread(m,se)).id}else ee=(await T.createThread(m,[])).id;let ae=F.annotations.map(M=>M.id),ce=Object.keys(G).length>0,$={id:m,status:"queued",screenshotPath:R,feedback:F,createdAt:Date.now(),color:f,threadId:ee,annotationIds:ae,provider:Ae(d),model:w||void 0,...Object.keys(I).length>0?{imagePaths:I}:{},sourceId:J||void 0,...ce?{screenshotPaths:G}:{}},K=new Set(F.annotations.map(M=>M.pathname).filter(Boolean)),A;if(K.size>1){let M=new Map;for(let oe of F.annotations){let de=oe.pathname||"(unknown)";M.has(de)||M.set(de,[]),M.get(de).push(oe.instruction||`[${oe.type}]`)}A=[...M.entries()].map(([oe,de])=>`\`${oe}\`
351
+ ${de.map(st=>`- ${st}`).join(`
224
352
  `)}`).join(`
225
- `)}else U=K.annotations.map(O=>O.instruction||`[${O.type}]`).join("; ");let te=Pe(K,Object.keys($).length>0?$:void 0);await b.appendMessage(re,{role:"human",timestamp:Date.now(),jobId:x,screenshotPath:M,...ce?{screenshotPaths:V}:{},...Object.keys($).length>0?{imagePaths:$}:{},annotationIds:pe,feedbackSummary:U,feedbackContext:te||void 0});let me=I.enqueue(D);P(l,200,{jobId:x,position:me,threadId:re})}async function ae(r,l){let f=r.headers["content-type"]||"",d,g,p,k,J,L,ne=[];if(f.includes("multipart/form-data")){let A=await st(r),U=A.feedback?JSON.parse(A.feedback):{};d=U.threadId,g=U.reply,p=U.color,k=U.provider,J=U.model,L=U.sourceId||A.sourceId;for(let te of A.pastedImages)ne.push(te.data)}else{let A=[];for await(let me of r)A.push(typeof me=="string"?Buffer.from(me):me);let U=Buffer.concat(A).toString("utf-8"),te;try{te=JSON.parse(U)}catch{P(l,400,{error:"Invalid JSON"});return}d=te.threadId,g=te.reply,p=te.color,k=te.provider,J=te.model,L=te.sourceId}if(!d||!g){P(l,400,{error:"Missing threadId or reply"});return}if(!await b.getThread(d)){P(l,404,{error:"Thread not found"});return}let x=je().slice(0,8),V=[];for(let A=0;A<ne.length;A++){let U=ue(i,`reply-${x}-${A}.webp`);await Le(U,ne[A]),V.push(U)}let M="";{let A=await b.getThreadHistory(d);for(let U=A.length-1;U>=0;U--)if(A[U].screenshotPath){M=A[U].screenshotPath;break}}if(!M&&!C.has(d)){P(l,400,{error:"No screenshot available"});return}await b.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:x,replyToQuestion:g,screenshotPath:M,...V.length>0?{replyImagePaths:V}:{}});let $=await b.getThreadHistory(d),ee=[];for(let A of $)if(A.annotationIds)for(let U of A.annotationIds)ee.includes(U)||ee.push(U);let re=ze(k),pe=Ut(M,$,re,V.length>0?V:void 0),ce={id:x,status:"queued",screenshotPath:M,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:p,threadId:d,annotationIds:ee.length>0?ee:void 0,provider:re,model:J||void 0,sourceId:L||void 0};C.has(d)&&(ce.kind="synthesize"),ce._replyPrompt=pe,V.length>0&&(ce._replyImagePaths=V);let D=I.enqueue(ce);P(l,200,{jobId:x,position:D,threadId:d})}function oe(r,l){l.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),l.write(`event: connected
353
+ `)}else A=F.annotations.map(M=>M.instruction||`[${M.type}]`).join("; ");let U=Me(F,Object.keys(I).length>0?I:void 0);await T.appendMessage(ee,{role:"human",timestamp:Date.now(),jobId:m,screenshotPath:R,...ce?{screenshotPaths:G}:{},...Object.keys(I).length>0?{imagePaths:I}:{},annotationIds:ae,feedbackSummary:A,feedbackContext:U||void 0});let le=D.enqueue($);v(l,200,{jobId:m,position:le,threadId:ee})}async function De(r,l){let p=r.headers["content-type"]||"",u,f,d,w,J,j,Y=[];if(p.includes("multipart/form-data")){let A=await Xe(r),U=A.feedback?JSON.parse(A.feedback):{};u=U.threadId,f=U.reply,d=U.color,w=U.provider,J=U.model,j=U.sourceId||A.sourceId;for(let le of A.pastedImages)Y.push(le.data)}else{let A=[];for await(let M of r)A.push(typeof M=="string"?Buffer.from(M):M);let U=Buffer.concat(A).toString("utf-8"),le;try{le=JSON.parse(U)}catch{v(l,400,{error:"Invalid JSON"});return}u=le.threadId,f=le.reply,d=le.color,w=le.provider,J=le.model,j=le.sourceId}if(!u||!f){v(l,400,{error:"Missing threadId or reply"});return}if(!await T.getThread(u)){v(l,404,{error:"Thread not found"});return}let m=we().slice(0,8),G=[];for(let A=0;A<Y.length;A++){let U=he(i,`reply-${m}-${A}.webp`);await Oe(U,Y[A]),G.push(U)}let R="";{let A=await T.getThreadHistory(u);for(let U=A.length-1;U>=0;U--)if(A[U].screenshotPath){R=A[U].screenshotPath;break}}let I=await Dn(u);if(!R&&!I){v(l,400,{error:"No screenshot available"});return}await T.appendMessage(u,{role:"human",timestamp:Date.now(),jobId:m,replyToQuestion:f,screenshotPath:R,...G.length>0?{replyImagePaths:G}:{}});let se=await T.getThreadHistory(u),ee=[];for(let A of se)if(A.annotationIds)for(let U of A.annotationIds)ee.includes(U)||ee.push(U);let ae=Ae(w),ce=dn(R,se,ae,G.length>0?G:void 0),$={id:m,status:"queued",screenshotPath:R,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:d,threadId:u,annotationIds:ee.length>0?ee:void 0,provider:ae,model:J||void 0,sourceId:j||void 0};I&&($.kind=I),$._replyPrompt=ce,G.length>0&&($._replyImagePaths=G);let K=D.enqueue($);v(l,200,{jobId:m,position:K,threadId:u})}function Rn(r,l){l.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),l.write(`event: connected
226
354
  data: {"status":"connected"}
227
355
 
228
- `),r.headers.origin&&Kt(r.headers.origin)&&o!==r.headers.origin&&(o=r.headers.origin);let d=new URL(r.url||"/",`http://127.0.0.1:${F}`).searchParams.get("sourceId")||void 0,g={id:je().slice(0,8),res:l,sourceId:d};z.add(g),r.on("close",()=>{z.delete(g)})}function Zt(r){let l=I.allActive;P(r,200,{ok:!0,version:Ue,restartMode:R,projectId:e,devOrigin:o,activeJob:l[0]?{id:l[0].id,status:l[0].status}:null,activeJobs:l.map(f=>({id:f.id,status:f.status,threadId:f.threadId,annotationIds:f.annotationIds,color:f.color})),queueDepth:I.depth,recentJobs:w})}async function en(r,l){let f=await new Promise(d=>{let g="";r.on("data",p=>{g+=p.toString()}),r.on("end",()=>d(g))});try{let d=JSON.parse(f);typeof d.devOrigin=="string"&&(o=d.devOrigin||null),P(l,200,{ok:!0,devOrigin:o})}catch{P(l,400,{error:"Invalid JSON"})}}async function tn(r,l){let d=new URL(r.url||"/",`http://127.0.0.1:${F}`).searchParams.get("jobId"),p=(d?I.allActive.filter(J=>J.id===d):I.allActive).map(J=>J.threadId).filter(Boolean),k=d?I.cancelJob(d):I.cancelActive();for(let J of p)await b.appendMessage(J,{role:"assistant",timestamp:Date.now(),jobId:d||"",cancelled:!0});P(l,200,{cancelled:k})}async function nn(r,l){if(u.isRunning){P(l,200,{skipped:!0,reason:"Already running"});return}let{provider:f,model:d}=await it(r);if(f==="copilot"){P(l,400,{error:"Materialization is not available for Copilot yet."});return}let g=await u.getUnmaterializedPatternDecisions();if(g.length===0){P(l,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}u.run({provider:f,model:d}).catch(()=>{}),P(l,200,{started:!0,decisionCount:g.length,decisionIds:g.map(p=>p.id)})}async function it(r){try{let l=[];for await(let d of r)l.push(typeof d=="string"?Buffer.from(d):d);if(l.length===0)return{};let f=JSON.parse(Buffer.concat(l).toString());return{provider:ze(typeof f.provider=="string"?f.provider:void 0),model:typeof f.model=="string"&&f.model?f.model:void 0}}catch{return{}}}async function sn(r,l){let f=await u.loadModel();if(!f){P(l,400,{error:"No model exists yet"});return}let d,g;try{let x=[];for await(let M of r)x.push(typeof M=="string"?Buffer.from(M):M);let V=JSON.parse(Buffer.concat(x).toString());d=V.provider,g=V.model}catch{}if(d==="copilot"){P(l,400,{error:"Rule synthesis is not available for Copilot yet."});return}let p=Et(f),k=je().slice(0,8),L=(await b.createThread(k,[])).id;C.add(L),await b.appendMessage(L,{role:"human",timestamp:Date.now(),jobId:k,feedbackSummary:"Synthesize rules \u2014 review and propose improvements"});let ne={id:k,status:"queued",screenshotPath:"",feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:L,kind:"synthesize",provider:ze(d),model:g||void 0};ne._replyPrompt=p;let K=I.enqueue(ne);P(l,200,{jobId:k,position:K,threadId:L})}async function on(r,l){let f=[];for await(let L of r)f.push(typeof L=="string"?Buffer.from(L):L);let d;if(f.length>0)try{d=JSON.parse(Buffer.concat(f).toString("utf-8")).serverUrl}catch{}let g=[];m.claude?.available&&m.claude.mcp&&!m.claude.mcp.found&&g.push(await Dt(d)),m.codex?.available&&m.codex.mcp&&!m.codex.mcp.found&&g.push(await Nt(d)),m.copilot?.available&&m.copilot.mcp&&!m.copilot.mcp.found&&g.push(await Ft(d,m.copilot.path??y));let[p,k,J]=await Promise.all([Ve(n),Ze(n),et(n,m.copilot?.path??y)]);m.claude&&(m.claude.mcp=p),m.codex&&(m.codex.mcp=k),m.copilot&&(m.copilot.mcp=J),P(l,200,{results:g,capabilities:{providers:m}})}async function rn(r,l){let f=[];for await(let p of r)f.push(typeof p=="string"?Buffer.from(p):p);let d;try{d=JSON.parse(Buffer.concat(f).toString("utf-8"))}catch{P(l,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){P(l,400,{error:"Missing or invalid name"});return}let g=await u.addComponent(d.name);P(l,200,g)}async function an(r,l){let f=[];for await(let p of r)f.push(typeof p=="string"?Buffer.from(p):p);let d;try{d=JSON.parse(Buffer.concat(f).toString("utf-8"))}catch{P(l,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){P(l,400,{error:"Missing or invalid name"});return}let g=await u.removeComponent(d.name);P(l,200,g)}async function cn(r,l){let f=[];for await(let p of r)f.push(typeof p=="string"?Buffer.from(p):p);let d;try{d=JSON.parse(Buffer.concat(f).toString("utf-8"))}catch{P(l,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){P(l,400,{error:"Missing or invalid path/value"});return}let g=await u.updateToken(d.path,d.value);P(l,200,g)}async function ln(r,l){let f=[];for await(let p of r)f.push(typeof p=="string"?Buffer.from(p):p);let d;try{d=JSON.parse(Buffer.concat(f).toString("utf-8"))}catch{P(l,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){P(l,400,{error:"Missing or invalid path"});return}let g=await u.removeToken(d.path);P(l,200,g)}function dn(r,l){let f=o??"http://localhost:3000";if(!o){let g=r.headers.referer||r.headers.origin;if(g)try{let p=new URL(typeof g=="string"?g:g[0]||"");(p.hostname==="localhost"||p.hostname==="127.0.0.1")&&(f=p.origin)}catch{}}let d=lt(F,f);l.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),l.end(d)}async function un(r){let l=Date.now();if(S&&l<S.expires){P(r,200,S.data);return}try{let{scanForComponents:f}=await import("./react-scanner-P3VD53VT.mjs"),{generateRenderFiles:d}=await import("./render-generator-HWFLZYC3.mjs"),g=await f(n);S={data:g,expires:l+5e3},d(g,n,X).then(p=>{X=p}).catch(p=>console.warn("[Bridge] Render generation failed:",p)),P(r,200,g)}catch(f){console.error("[Bridge] Scanner error:",f),P(r,500,{error:"Failed to scan components"})}}async function pn(r){let l=[ue(n,"node_modules","@popmelt.com","core","dist","canvas.mjs"),ue(n,"packages","popmelt","dist","canvas.mjs")];try{let f=Bs(import.meta.url);l.unshift(ue(Fs(f),"canvas.mjs"))}catch{}for(let f of l)try{let d=await Yt(f,"utf-8");r.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),r.end(d);return}catch{}console.error("[Bridge] Canvas bundle not found in:",l),P(r,404,{error:"Canvas bundle not found"})}async function fn(r,l){if(!r||r.includes("/")||r.includes("\\")||r.includes("..")){P(l,400,{error:"Invalid filename"});return}try{let f=await Yt(ue(i,r)),d=r.split(".").pop()?.toLowerCase(),g=d==="png"?"image/png":d==="webp"?"image/webp":d==="jpg"||d==="jpeg"?"image/jpeg":"application/octet-stream";l.writeHead(200,{"Content-Type":g,"Cache-Control":"public, max-age=3600"}),l.end(f)}catch{P(l,404,{error:"File not found"})}}function Ie(r){return`/files/${Ns(r)}`}async function hn(r,l){let f=await b.getThread(r);if(!f){P(l,404,{error:"Thread not found"});return}let d=f.messages.map(({screenshotPath:g,screenshotPaths:p,imagePaths:k,replyImagePaths:J,...L})=>({...L,...g?{screenshotUrl:Ie(g)}:{},...p?{screenshotUrls:Object.fromEntries(Object.entries(p).map(([ne,K])=>[ne,Ie(K)]))}:{},...k?{imageUrls:Object.fromEntries(Object.entries(k).map(([ne,K])=>[ne,K.map(Ie)]))}:{},...J?{replyImageUrls:J.map(Ie)}:{}}));P(l,200,{id:f.id,createdAt:f.createdAt,messages:d})}let at=9,ct=!1;for(let r=t;r<t+at;r++)try{await Gs(T,r),F=r,ct=!0,console.log(`[\u22B9 is watching :${F}]`);break}catch(l){if(l.code==="EADDRINUSE"){let f=await We(r);if(f&&f.projectId===e)return console.log(`[\u22B9 already watching :${r}]`),{port:r,projectId:e,close:async()=>{}};continue}throw l}if(!ct)throw new Error(`[Bridge] All ports ${t}\u2013${t+at-1} in use`);for(let[r,l]of Object.entries(m))!l.available||!l.path||H(r,l.path).then(f=>{if(f)console.log(`[Bridge] ${r} warmed up`);else{console.warn(`[Bridge] ${r} warm-up failed \u2014 marking unavailable`),l.available=!1,l.path=null;for(let d of z)rt(d,{type:"capabilities_changed",data:{}})}});let mn=setInterval(()=>{Qt(i).catch(()=>{})},js);return{port:F,projectId:e,close:async()=>{clearInterval(mn),await I.destroyAsync();for(let r of z)try{r.res.end()}catch{}return z.clear(),new Promise(r=>{T.close(()=>r())})}}}async function Qt(s){try{let t=await $s(s),n=Date.now();for(let e of t){let o=ue(s,e);try{let i=await Os(o);n-i.mtimeMs>Ls&&await _s(o)}catch{}}}catch{}}var Vs=1111;async function wr(s){if(process.env.NODE_ENV==="production"&&!s?.force)throw new Error("[Bridge] Refusing to start in production. Pass { force: true } to override.");if(s?.detached)return Zs(s);let t=await Vt(s);return process.env.POPMELT_BRIDGE_PORT=String(t.port),t}async function Zs(s){let t=s.port??Vs,n=s.projectRoot??process.cwd(),e=Xs("sha256").update(n).digest("hex").slice(0,12);for(let a=t;a<t+10;a++){let c=await We(a);if(c&&c.projectId===e){if(c.version&&c.version!==Ue){try{await fetch(`http://127.0.0.1:${a}/shutdown`,{method:"POST"})}catch{}await new Promise(v=>setTimeout(v,500));break}let h=s.devOrigin;if(h&&c.devOrigin!==h)try{await fetch(`http://127.0.0.1:${a}/config`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({devOrigin:h})})}catch{}return process.env.POPMELT_BRIDGE_PORT=String(a),{port:a,projectId:e,close:async()=>{}}}}let o=Qs(Ys(Ks(import.meta.url)),"bridge-entry.mjs");qs(process.execPath,[o],{detached:!0,stdio:"ignore",env:{...process.env,POPMELT_PORT:String(t),POPMELT_PROJECT_ROOT:n,POPMELT_DEV_ORIGIN:s.devOrigin??"",POPMELT_COPILOT_PATH:s.copilotPath??""}}).unref();let i=Date.now()+3e3;for(;Date.now()<i;){await new Promise(a=>setTimeout(a,100));for(let a=t;a<t+10;a++){let c=await We(a);if(c&&c.projectId===e)return process.env.POPMELT_BRIDGE_PORT=String(a),{port:a,projectId:e,close:async()=>{}}}}throw new Error(`[Bridge] Detached bridge failed to start within 3s (port ${t})`)}export{wr as startPopmelt};
356
+ `),r.headers.origin&&Pn(r.headers.origin)&&o!==r.headers.origin&&(o=r.headers.origin);let u=new URL(r.url||"/",`http://127.0.0.1:${L}`).searchParams.get("sourceId")||void 0,f={id:we().slice(0,8),res:l,sourceId:u};_.add(f),r.on("close",()=>{_.delete(f)})}function Tn(r){let l=D.allActive;v(r,200,{ok:!0,version:Ze,restartMode:O,projectId:e,projectName:Sn(n),devOrigin:o,activeJob:l[0]?{id:l[0].id,status:l[0].status}:null,activeJobs:l.map(p=>({id:p.id,status:p.status,threadId:p.threadId,annotationIds:p.annotationIds,color:p.color,kind:p.kind})),queueDepth:D.depth,recentJobs:b})}async function In(r,l){let p=await new Promise(u=>{let f="";r.on("data",d=>{f+=d.toString()}),r.on("end",()=>u(f))});try{let u=JSON.parse(p);typeof u.devOrigin=="string"&&(o=u.devOrigin||null),v(l,200,{ok:!0,devOrigin:o})}catch{v(l,400,{error:"Invalid JSON"})}}async function Mn(r,l){let u=new URL(r.url||"/",`http://127.0.0.1:${L}`).searchParams.get("jobId"),d=(u?D.allActive.filter(J=>J.id===u):D.allActive).map(J=>J.threadId).filter(Boolean),w=u?D.cancelJob(u):D.cancelActive();for(let J of d)await T.appendMessage(J,{role:"assistant",timestamp:Date.now(),jobId:u||"",cancelled:!0});v(l,200,{cancelled:w})}async function Cn(r,l){if(C.isRunning){v(l,200,{skipped:!0,reason:"Already running"});return}let{provider:p,model:u}=await nt(r);if(p==="copilot"){v(l,400,{error:"Materialization is not available for Copilot yet."});return}let f=await C.getUnmaterializedPatternDecisions();if(f.length===0){v(l,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}C.run({provider:p,model:u}).catch(()=>{}),v(l,200,{started:!0,decisionCount:f.length,decisionIds:f.map(d=>d.id)})}async function nt(r){try{let l=[];for await(let u of r)l.push(typeof u=="string"?Buffer.from(u):u);if(l.length===0)return{};let p=JSON.parse(Buffer.concat(l).toString());return{provider:Ae(typeof p.provider=="string"?p.provider:void 0),model:typeof p.model=="string"&&p.model?p.model:void 0}}catch{return{}}}async function $n(r,l){let p=await C.loadModel();if(!p){v(l,400,{error:"No model exists yet"});return}let u,f;try{let m=[];for await(let R of r)m.push(typeof R=="string"?Buffer.from(R):R);let G=JSON.parse(Buffer.concat(m).toString());u=G.provider,f=G.model}catch{}if(u==="copilot"){v(l,400,{error:"Rule synthesis is not available for Copilot yet."});return}let d=Xt(p),w=we().slice(0,8),j=(await T.createThread(w,[])).id;H.set(j,"synthesize"),await T.appendMessage(j,{role:"human",timestamp:Date.now(),jobId:w,feedbackSummary:"Synthesize rules \u2014 review and propose improvements"});let Y={id:w,status:"queued",screenshotPath:"",feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:j,kind:"synthesize",provider:Ae(u),model:f||void 0};Y._replyPrompt=d;let F=D.enqueue(Y);v(l,200,{jobId:w,position:F,threadId:j})}async function _n(r,l){let{provider:p,model:u}=await nt(r);if(p==="copilot"){v(l,400,{error:"Eval synthesis is not available for Copilot yet."});return}let f=await k.listDecisionIds(),d=await k.loadDecisions(f),w=await T.load(),J=await C.loadModel(),j=await y.loadSuite(),Y=zt({decisions:d,threads:w,model:J,existingSuite:j}),F=we().slice(0,8),G=(await T.createThread(F,[])).id;H.set(G,"synthesize_evals"),await T.appendMessage(G,{role:"human",timestamp:Date.now(),jobId:F,feedbackSummary:"Synthesize evals \u2014 infer evals from design history"});let R={id:F,status:"queued",screenshotPath:"",feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:"#6b7280",threadId:G,kind:"synthesize_evals",provider:p,model:u||void 0};R._replyPrompt=Y;let I=D.enqueue(R);v(l,200,{jobId:F,position:I,threadId:G})}async function On(r,l,p){let u=decodeURIComponent(r),f=await y.findCase(u);if(!f){v(p,404,{error:"Eval not found"});return}if(f.status!=="approved"){v(p,400,{error:"Only approved evals can be run"});return}let{screenshot:d,feedback:w,provider:J,model:j,sourceId:Y}=await Xe(l),F=Ae(J)??B;if(F==="copilot"){v(p,400,{error:"Eval runs are not available for Copilot yet."});return}let m={};try{m=w?JSON.parse(w):{}}catch{v(p,400,{error:"Invalid eval run context JSON"});return}let G=we().slice(0,8),R=we().slice(0,8),I=he(i,`eval-run-${G}.webp`);await Oe(I,d);let se=await y.writeRunScreenshot(G,d),ee=m.viewport,ae=ee&&typeof ee=="object"?ee:{},ce={type:"current_page",url:typeof m.url=="string"?m.url:"",viewport:{width:typeof ae.width=="number"?ae.width:0,height:typeof ae.height=="number"?ae.height:0}},$=await C.loadModel(),K=F==="claude"?Q.name:F==="codex"?Z.name:null,A=F==="codex"?q.length>0:typeof K=="string"&&/chrome|devtools/i.test(K),U=Lt({evalCase:f,scope:ce,model:$,screenshotPath:I,chromeDevtoolsAvailable:A}),M=(await T.createThread(R,[])).id;H.set(M,"run_eval");let oe={timestamp:new Date().toISOString(),url:ce.url,viewport:ce.viewport,scrollPosition:{x:typeof m.scrollX=="number"?m.scrollX:0,y:typeof m.scrollY=="number"?m.scrollY:0},annotations:[],styleModifications:[]};await T.appendMessage(M,{role:"human",timestamp:Date.now(),jobId:R,screenshotPath:I,feedbackSummary:`Run eval \u2014 ${f.title}`,feedbackContext:[`Eval: ${f.title}`,"Scope: current page",`URL: ${ce.url||"unknown"}`,"Assertions:",...f.assertions.map(Xn=>`- ${Xn}`)].join(`
357
+ `)});let de={id:R,status:"queued",screenshotPath:I,feedback:oe,createdAt:Date.now(),color:"#111827",threadId:M,kind:"run_eval",evalId:u,evalRunId:G,provider:F,model:j||void 0,sourceId:Y||void 0};de._replyPrompt=U,de._evalRunScope=ce,de._evalScreenshotPath=se;let st=D.enqueue(de);v(p,200,{jobId:R,runId:G,position:st,threadId:M,screenshotPath:se})}async function An(r,l,p){let u=decodeURIComponent(r),f={},d,w,J;try{let Y=[];for await(let m of l)Y.push(typeof m=="string"?Buffer.from(m):m);let F=JSON.parse(Buffer.concat(Y).toString());d=F.status,w=F.title,J=F.assertions}catch{v(p,400,{error:"Invalid JSON"});return}if(d!==void 0&&d!=="pending"&&d!=="approved"&&d!=="rejected"){v(p,400,{error:"Invalid status"});return}if(d&&(f.status=d),w!==void 0){if(typeof w!="string"||w.trim().length===0){v(p,400,{error:"Invalid title"});return}f.title=w.trim()}if(J!==void 0){if(!Array.isArray(J)){v(p,400,{error:"Invalid assertions"});return}let Y=J.filter(F=>typeof F=="string").map(F=>F.trim()).filter(Boolean);if(Y.length===0){v(p,400,{error:"Invalid assertions"});return}f.assertions=Y}if(!f.status&&!f.title&&!f.assertions){v(p,400,{error:"No eval changes provided"});return}let j=await y.updateCase(u,f);if(!j){v(p,404,{error:"Eval not found"});return}v(p,200,{eval:j})}async function Dn(r){let l=H.get(r);if(l)return l;let f=(await T.getThread(r))?.messages.find(d=>d.role==="human")?.feedbackSummary??"";if(f.startsWith("Synthesize evals"))return H.set(r,"synthesize_evals"),"synthesize_evals";if(f.startsWith("Synthesize rules"))return H.set(r,"synthesize"),"synthesize";if(f.startsWith("Run eval"))return H.set(r,"run_eval"),"run_eval"}async function Nn(r,l){let p=[];for await(let j of r)p.push(typeof j=="string"?Buffer.from(j):j);let u;if(p.length>0)try{u=JSON.parse(Buffer.concat(p).toString("utf-8")).serverUrl}catch{}let f=[];S.claude?.available&&S.claude.mcp&&!S.claude.mcp.found&&f.push(await on(u)),S.codex?.available&&S.codex.mcp&&!S.codex.mcp.found&&f.push(await rn(u)),S.copilot?.available&&S.copilot.mcp&&!S.copilot.mcp.found&&f.push(await an(u,S.copilot.path??g));let[d,w,J]=await Promise.all([ut(n),pt(n),ft(n,S.copilot?.path??g)]);S.claude&&(S.claude.mcp=d),S.codex&&(S.codex.mcp=w),S.copilot&&(S.copilot.mcp=J),v(l,200,{results:f,capabilities:{providers:S}})}async function Jn(r,l){let p=[];for await(let d of r)p.push(typeof d=="string"?Buffer.from(d):d);let u;try{u=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{v(l,400,{error:"Invalid JSON"});return}if(!u.name||typeof u.name!="string"){v(l,400,{error:"Missing or invalid name"});return}let f=await C.addComponent(u.name);v(l,200,f)}async function Bn(r,l){let p=[];for await(let d of r)p.push(typeof d=="string"?Buffer.from(d):d);let u;try{u=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{v(l,400,{error:"Invalid JSON"});return}if(!u.name||typeof u.name!="string"){v(l,400,{error:"Missing or invalid name"});return}let f=await C.removeComponent(u.name);v(l,200,f)}async function Fn(r,l){let p=[];for await(let d of r)p.push(typeof d=="string"?Buffer.from(d):d);let u;try{u=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{v(l,400,{error:"Invalid JSON"});return}if(!u.path||typeof u.path!="string"||typeof u.value!="string"){v(l,400,{error:"Missing or invalid path/value"});return}let f=await C.updateToken(u.path,u.value);v(l,200,f)}async function jn(r,l){let p=[];for await(let d of r)p.push(typeof d=="string"?Buffer.from(d):d);let u;try{u=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{v(l,400,{error:"Invalid JSON"});return}if(!u.path||typeof u.path!="string"){v(l,400,{error:"Missing or invalid path"});return}let f=await C.removeToken(u.path);v(l,200,f)}function Un(r,l){let p=o??"http://localhost:3000";if(!o){let f=r.headers.referer||r.headers.origin;if(f)try{let d=new URL(typeof f=="string"?f:f[0]||"");(d.hostname==="localhost"||d.hostname==="127.0.0.1")&&(p=d.origin)}catch{}}let u=St(L,p);l.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),l.end(u)}async function zn(r){let l=Date.now();if(E&&l<E.expires){v(r,200,E.data);return}try{let{scanForComponents:p}=await import("./react-scanner-P3VD53VT.mjs"),{generateRenderFiles:u}=await import("./render-generator-HWFLZYC3.mjs"),f=await p(n);E={data:f,expires:l+5e3},u(f,n,ie).then(d=>{ie=d}).catch(d=>console.warn("[Bridge] Render generation failed:",d)),v(r,200,f)}catch(p){console.error("[Bridge] Scanner error:",p),v(r,500,{error:"Failed to scan components"})}}async function Ln(r){let l=[he(n,"node_modules","@popmelt.com","core","dist","canvas.mjs"),he(n,"packages","popmelt","dist","canvas.mjs")];try{let p=Ro(import.meta.url);l.unshift(he(Eo(p),"canvas.mjs"))}catch{}for(let p of l)try{let u=await wn(p,"utf-8");r.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),r.end(u);return}catch{}console.error("[Bridge] Canvas bundle not found in:",l),v(r,404,{error:"Canvas bundle not found"})}async function Wn(r,l){if(!r||r.includes("/")||r.includes("\\")||r.includes("..")){v(l,400,{error:"Invalid filename"});return}try{let p=await wn(he(i,r)),u=r.split(".").pop()?.toLowerCase(),f=u==="png"?"image/png":u==="webp"?"image/webp":u==="jpg"||u==="jpeg"?"image/jpeg":"application/octet-stream";l.writeHead(200,{"Content-Type":f,"Cache-Control":"public, max-age=3600"}),l.end(p)}catch{v(l,404,{error:"File not found"})}}function Ne(r){return`/files/${Sn(r)}`}async function Hn(r,l){let p=await T.getThread(r);if(!p){v(l,404,{error:"Thread not found"});return}let u=(await y.loadRuns()).runs.filter(d=>d.threadId===r).sort((d,w)=>w.completedAt-d.completedAt)[0],f=p.messages.map(({screenshotPath:d,screenshotPaths:w,imagePaths:J,replyImagePaths:j,...Y})=>({...Y,...Y.role==="assistant"&&u?{evalRun:u}:{},...d?{screenshotUrl:Ne(d)}:{},...w?{screenshotUrls:Object.fromEntries(Object.entries(w).map(([F,m])=>[F,Ne(m)]))}:{},...J?{imageUrls:Object.fromEntries(Object.entries(J).map(([F,m])=>[F,m.map(Ne)]))}:{},...j?{replyImageUrls:j.map(Ne)}:{}}));v(l,200,{id:p.id,createdAt:p.createdAt,messages:f})}let yt=9,wt=!1;for(let r=t;r<t+yt;r++)try{await Ao(re,r),L=r,wt=!0,console.log(`[\u22B9 is watching :${L}]`);break}catch(l){if(l.code==="EADDRINUSE"){let p=await tt(r);if(p&&p.projectId===e)return console.log(`[\u22B9 already watching :${r}]`),{port:r,projectId:e,close:async()=>{}};continue}throw l}if(!wt)throw new Error(`[Bridge] All ports ${t}\u2013${t+yt-1} in use`);for(let[r,l]of Object.entries(S))!l.available||!l.path||W(r,l.path).then(p=>{if(p)console.log(`[Bridge] ${r} warmed up`);else{console.warn(`[Bridge] ${r} warm-up failed \u2014 marking unavailable`),l.available=!1,l.path=null;for(let u of _)vt(u,{type:"capabilities_changed",data:{}})}});let Gn=setInterval(()=>{xn(i).catch(()=>{})},Mo);return{port:L,projectId:e,close:async()=>{clearInterval(Gn),await D.destroyAsync();for(let r of _)try{r.res.end()}catch{}return _.clear(),new Promise(r=>{re.close(()=>r())})}}}async function xn(s){try{let t=await So(s),n=Date.now();for(let e of t){let o=he(s,e);try{let i=await bo(o);n-i.mtimeMs>Co&&await xo(o)}catch{}}}catch{}}var Uo=1111;async function di(s){if(process.env.NODE_ENV==="production"&&!s?.force)throw new Error("[Bridge] Refusing to start in production. Pass { force: true } to override.");if(s?.detached)return zo(s);let t=await En(s);return process.env.POPMELT_BRIDGE_PORT=String(t.port),t}async function zo(s){let t=s.port??Uo,n=s.projectRoot??process.cwd(),e=Jo("sha256").update(n).digest("hex").slice(0,12);for(let a=t;a<t+10;a++){let c=await tt(a);if(c&&c.projectId===e){if(c.version&&c.version!==Ze){try{await fetch(`http://127.0.0.1:${a}/shutdown`,{method:"POST"})}catch{}await new Promise(P=>setTimeout(P,500));break}let h=s.devOrigin;if(h&&c.devOrigin!==h)try{await fetch(`http://127.0.0.1:${a}/config`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({devOrigin:h})})}catch{}return process.env.POPMELT_BRIDGE_PORT=String(a),{port:a,projectId:e,close:async()=>{}}}}let o=Fo(Bo(jo(import.meta.url)),"bridge-entry.mjs");No(process.execPath,[o],{detached:!0,stdio:"ignore",env:{...process.env,POPMELT_PORT:String(t),POPMELT_PROJECT_ROOT:n,POPMELT_DEV_ORIGIN:s.devOrigin??"",POPMELT_COPILOT_PATH:s.copilotPath??""}}).unref();let i=Date.now()+3e3;for(;Date.now()<i;){await new Promise(a=>setTimeout(a,100));for(let a=t;a<t+10;a++){let c=await tt(a);if(c&&c.projectId===e)return process.env.POPMELT_BRIDGE_PORT=String(a),{port:a,projectId:e,close:async()=>{}}}}throw new Error(`[Bridge] Detached bridge failed to start within 3s (port ${t})`)}export{di as startPopmelt};