@popmelt.com/core 0.7.2 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import{mkdir as jo,unlink as Uo,writeFile as zo}from"fs/promises";import{join as En}from"path";import{execFileSync as Ze,spawn as vn}from"child_process";import{createHash as xn,randomUUID as we}from"crypto";import{mkdir as ko,readFile as yn,readdir as Po,stat as Eo,unlink as Ro,writeFile as Oe}from"fs/promises";import{createServer as To}from"http";import{tmpdir as Io}from"os";import{basename as wn,dirname as Mo,join as he}from"path";import{fileURLToPath as Co}from"url";function yt(s,t){return`<!DOCTYPE html>
1
+ import{mkdir as Uo,unlink as zo,writeFile as Lo}from"fs/promises";import{join as En}from"path";import{execFileSync as Ze,spawn as yn}from"child_process";import{createHash as xn,randomUUID as we}from"crypto";import{mkdir as ko,readFile as wn,readdir as Po,stat as Eo,unlink as Ro,writeFile as Oe}from"fs/promises";import{createServer as Io}from"http";import{tmpdir as To}from"os";import{basename as mt,dirname as Mo,join as he}from"path";import{fileURLToPath as Co}from"url";function wt(s,t){return`<!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
4
  <meta charset="utf-8">
@@ -25,12 +25,12 @@ import{mkdir as jo,unlink as Uo,writeFile as zo}from"fs/promises";import{join as
25
25
  });
26
26
  </script>
27
27
  </body>
28
- </html>`}import{spawn as Vn}from"child_process";import{createInterface as Zn}from"readline";var es=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"]),wt=1e5;function ts(s){let t=s.input?.file_path||s.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!es.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>wt?e.slice(0,wt)+`
29
- \u2026[truncated]`:e}function Je(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:c=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:a="claude",resumeSessionId:m,model:P,timeoutMs:g=3e5,onEvent:F}=t,z=[];m?z.push("--resume",m,"-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 c)z.push("--allowedTools",S);let O=Vn(a,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=[],K=[],Z=[],Q=!1,V="",D,_,R=!1,k=setTimeout(()=>{R=!0,O.kill("SIGTERM"),setTimeout(()=>{try{O.kill("SIGKILL")}catch{}},5e3)},g),y=Zn({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),F?.({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),F?.({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=ts(E);if(F?.({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};K.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&&(K.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&&F?.({type:"thinking",jobId:s,text:E.thinking},s)}}b.type==="result"&&b.is_error===!0&&(Q=!0,typeof b.error=="string"&&(D=b.error),typeof b.result=="string"&&(_=b.result)),b.type==="user"&&b.tool_use_result?.file?.filePath&&F?.({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(", ")}`),R)Q=!0,V=`Timed out after ${Math.round(g/6e4)} minutes`;else if(x!==0&&x!==null){Q=!0;let H=N.join("").trim(),E=W.join("").trim(),ie=W.length===0&&C.size>0?` (no text captured, event types: ${[...C].join(", ")})`:"";V=H||_||E||`Claude process exited with code ${x}${ie}`}Q&&!V&&_&&(V=_);let b=ns(V,D);S({sessionId:X,text:W.join(""),success:!Q,error:Q?b.message:void 0,errorCode:Q?b.code:void 0,recoverableError:Q?b.recoverable:void 0,fileEdits:K.length>0?K:void 0,editEvents:Z.length>0?Z:void 0})}),O.on("error",x=>{clearTimeout(k),Q=!0,V=x.message,S({sessionId:X,text:W.join(""),success:!1,error:V,errorCode:"claude_spawn_error",recoverableError:!1,fileEdits:K.length>0?K:void 0,editEvents:Z.length>0?Z:void 0})})});return{process:O,result:L}}function ns(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 ss,spawnSync as Et}from"child_process";import{readFileSync as os,statSync as rs}from"fs";import{basename as is,extname as as,isAbsolute as cs,relative as Rt,resolve as ls}from"path";import{createInterface as ds}from"readline";function St(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}var bt=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"]),nt=2e5,us=1e5,Tt=2e5;function It(s){let t=is(s).toLowerCase(),n=as(s).toLowerCase();return bt.has(n)||bt.has(t)}function Mt(s,t){let n=cs(t)?t:ls(s,t),e=Rt(s,n);return e===""||e.startsWith("..")?null:n}function Fe(s,t){if(!It(t))return;let n=Mt(s,t);if(n)try{let e=rs(n);return e.isFile()?e.size>nt?{exists:!0,truncated:!0}:{exists:!0,content:os(n,"utf8")}:{exists:!1}}catch{return{exists:!1}}}function ps(s,t){for(let n of t){let e=s[n];if(typeof e=="string")return e}}function fs(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 xt(s){return ps(s,["patch","diff","unifiedDiff","unified_diff"])}function st(s,t){return s.length<=t?{text:s}:{text:s.slice(0,t),truncated:!0}}function kt(s,t){let n=Et("git",t,{cwd:s,encoding:"utf8",timeout:5e3,maxBuffer:Tt+65536});return n.status!==0?null:n.stdout??""}function hs(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 Pt(s){let t=kt(s,["status","--porcelain=v1","-uall"]);if(t===null)return;let n=kt(s,["diff","--no-ext-diff","HEAD","--"])??"",e=st(n,Tt);return{status:t,changedFiles:hs(t),...e.text?{diff:e.text}:{},...e.truncated?{diffTruncated:!0}:{}}}function ms(s,t){return!s||!t?!1:s.status!==t.status||(s.diff??"")!==(t.diff??"")}function gs(s,t){if(!It(t))return;let n=Mt(s,t);if(!n)return;let e=Rt(s,n);if(e===""||e.startsWith(".."))return;let o=Et("git",["show",`HEAD:${e}`],{cwd:s,encoding:"utf8",timeout:5e3,maxBuffer:nt+65536});if(o.status!==0)return{exists:!1};let i=o.stdout??"",c=st(i,nt);return{exists:!0,content:c.text,...c.truncated?{truncated:!0}:{}}}function vs(s,t,n){return t.map(e=>{let o=n.has(e)?n.get(e):gs(s,e),i=Fe(s,e);return{filePath:e,...o?{before:o}:{},...i?{after:i}:{}}})}function Be(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:c,onEvent:a,sandboxMode:m="workspace-write",configOverrides:P=[]}=t,g=[];if(i){g.push("exec","resume","--skip-git-repo-check","--json","-c",`sandbox_mode="${m}"`);for(let O of P)g.push("-c",O);c&&g.push("-m",c),g.push(i),g.push(n),o&&g.push("--image",o)}else{g.push("exec","--skip-git-repo-check","--json","--sandbox",m);for(let O of P)g.push("-c",O);c&&g.push("-m",c),g.push(n),o&&g.push("--image",o)}let F=ss("codex",g,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),z=new Promise(O=>{let L,S=[],X=[],W=[],K=new Map,Z=new Map,Q=!1,V="",D=ds({input:F.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),a?.({type:"delta",jobId:s,text:y.delta.text},s)),(C==="item.reasoning.delta"||C==="item/reasoning/delta")&&y.delta?.text&&a?.({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?St(x):void 0,H=Pt(e),E=new Map;for(let re of H?.changedFiles??[])E.set(re,Fe(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),a?.({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&&!K.has(x)&&K.set(x,Fe(e,x)),X.push(x?`Edit ${x.split("/").pop()}`:"Edit"),a?.({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"),a?.({type:"tool_use",jobId:s,tool:"Read",...x?{file:x}:{}},s)}else if(N==="web_search")X.push("WebSearch"),a?.({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),a?.({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),a?.({type:"delta",jobId:s,text:N},s))}else if(y.item.type==="reasoning"){let N=y.item.text;typeof N=="string"&&N&&a?.({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()}`),a?.({type:"tool_use",jobId:s,tool:b,file:x},s);let H=N,E=typeof H.kind=="string"?H.kind:void 0,ie=fs(E),re=K.get(x),ne=Fe(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}:{},...xt(H)?{patch:xt(H)}:{},...E?{rawKind:E}:{}}),K.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?St(b):x?.command,E=Pt(e);if(ms(x?.beforeWorktree,E)){let ie=[...new Set([...x?.beforeWorktree?.changedFiles??[],...E?.changedFiles??[]])],re=typeof y.item.aggregated_output=="string"?st(y.item.aggregated_output,us):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:vs(e,ie,x?.beforeSnapshots??new Map)}:{}})}N&&Z.delete(N)}}C==="turn.failed"&&(Q=!0,V=y.error?.message||y.message||"Turn failed")}catch{}});let R=[];F.stderr?.on("data",k=>{R.push(k.toString())}),F.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&&(Q=!0,V=R.join("")||`Codex process exited with code ${k}`),O({sessionId:L,text:S.join(""),success:!Q,error:Q?V:void 0,editEvents:W.length>0?W:void 0,toolsUsed:X.length>0?X:void 0})}),F.on("error",k=>{Q=!0,V=k.message,O({sessionId:L,text:S.join(""),success:!1,error:V,editEvents:W.length>0?W:void 0,toolsUsed:X.length>0?X:void 0})})});return{process:F,result:z}}import{spawn as ys}from"child_process";import{createInterface as ws}from"readline";function Ct(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 $t(s,t){let{prompt:n,projectRoot:e,resumeSessionId:o,model:i,timeoutMs:c=3e5,onEvent:a,copilotPath:m="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=ys(m,P,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),F=new Promise(z=>{let O,L=[],S=[],X=!1,W="",K=!1,Z=setTimeout(()=>{K=!0,g.kill("SIGTERM"),setTimeout(()=>{try{g.kill("SIGKILL")}catch{}},5e3)},c),Q=ws({input:g.stdout}),V=new Set,D=[];Q.on("line",R=>{if(R.trim())try{let k=JSON.parse(R),y=ke(k);V.add(y),D.length<5&&bs(y)&&D.push(R.slice(0,800));let C=xe(k,["sessionId","session_id","id"],["session","conversation","thread"]);C&&!O&&(O=C);let N=Ss(k,L.length===0);N&&(L.push(N),a?.({type:"delta",jobId:s,text:N},s));let x=xs(k);x&&a?.({type:"thinking",jobId:s,text:x},s);let b=ks(k);b&&(S.push(b.label),a?.({type:"tool_use",jobId:s,tool:b.tool,...b.file?{file:b.file}:{},...b.content?{content:b.content}:{}},s));let H=Ps(k);H&&(X=!0,W=H)}catch{}});let _=[];g.stderr?.on("data",R=>{_.push(R.toString())}),g.on("close",R=>{clearTimeout(Z),Q.close(),L.length===0&&V.size>0&&(console.warn(`[Copilot:${s}] No text captured. Event types seen: ${[...V].join(", ")}`),D.length>0&&console.warn(`[Copilot:${s}] Sample text-bearing candidates: ${D.join(`
32
- `)}`)),K?(X=!0,W=`Timed out after ${Math.round(c/6e4)} minutes`):R!==0&&R!==null&&(X=!0,W=_.join("").trim()||W||`Copilot process exited with code ${R}`);let k=X?Ct(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",R=>{clearTimeout(Z);let k=Ct(R.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:F}}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 Ss(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"&&ot(s))return s.text;if(typeof s.message=="string"&&ot(s))return s.message;if(typeof s.delta=="string"&&ot(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 bs(s){let t=s.toLowerCase();return t.includes("assistant.message")||t==="result"}function xs(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 ks(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 Ps(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(c=>e.toLowerCase().includes(c)))continue;let i=xe(o,t);if(i)return i}}function ot(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 Es}from"child_process";import{copyFile as _t,mkdir as Ot,readdir as Rs,readFile as Ts,writeFile as Is}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 Ot(this.decisionsDir,{recursive:!0}),await Ot(this.screenshotsDir,{recursive:!0});try{await _t(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 _t(e[o],ve(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await Is(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 Rs(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 Ts(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=>{Es("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 At,writeFile as Ee}from"fs/promises";import{join as Re}from"path";var Ms=80,Cs=24,Dt=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 At(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 At(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(c=>[c.id,c])),o=new Set;for(let c of t){let a=e.get(c.id);a&&(a.title!==c.title||a.prompt!==c.prompt||JSON.stringify(a.assertions)!==JSON.stringify(c.assertions))&&o.add(c.id),e.set(c.id,c)}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(a=>a.id===t?{...a,...n.status?{status:n.status}:{},...n.title?{title:n.title}:{},...n.assertions?{assertions:n.assertions}:{}}:a),c=i.find(a=>a.id===t)??null;return c?(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])),c):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 Nt(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?Bt(e):null}catch{return null}}function Jt(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?Bt(e,"pending"):null}catch{return null}}function Ft(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=Ns(e.assertionResults,t),i=Js(o);return{status:i,summary:fe(e.summary)||zt(i),assertionResults:o,tooling:Fs(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 Bt(s,t="approved"){let n=[];return s.forEach((e,o)=>{if(!e||typeof e!="object")return;let i=e,c=fe(i.title),a=fe(i.prompt),m=fe(i.rationale),P=Se(i.assertions);if(!c||!a||P.length===0)return;let g=Os(i.category),F=Bs(fe(i.id))||`eval-${Lt(c).slice(0,32)||o+1}`,z=As(i.status)??t;n.push({id:F,title:c,status:z,category:g,prompt:a,assertions:P,rationale:m||"Inferred from Popmelt design history.",sourceDecisionIds:Se(i.sourceDecisionIds),sourceThreadIds:Se(i.sourceThreadIds)})}),n}function jt(s){let t=[...s.decisions].sort((e,o)=>o.createdAt-e.createdAt).slice(0,Ms),n=Object.values(s.threads.threads).sort((e,o)=>o.updatedAt-e.updatedAt).slice(0,Cs);return`You are helping a designer turn organic Popmelt annotation history into a project eval suite.
28
+ </html>`}import{spawn as Vn}from"child_process";import{createInterface as Zn}from"readline";var es=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"]),St=1e5;function ts(s){let t=s.input?.file_path||s.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!es.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>St?e.slice(0,St)+`
29
+ \u2026[truncated]`:e}function Je(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:c=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:a="claude",resumeSessionId:m,model:P,timeoutMs:v=3e5,onEvent:F}=t,z=[];m?z.push("--resume",m,"-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 c)z.push("--allowedTools",S);let A=Vn(a,z,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),W=new Promise(S=>{let X,H=[],K=[],Z=[],Q=!1,V="",N,_,R=!1,k=setTimeout(()=>{R=!0,A.kill("SIGTERM"),setTimeout(()=>{try{A.kill("SIGKILL")}catch{}},5e3)},v),w=Zn({input:A.stdout}),C=new Set;w.on("line",x=>{if(x.trim())try{let b=JSON.parse(x);b.session_id&&!X&&(X=b.session_id);let G=b.type??(b.event?.type?`event.${b.event.type}`:"unknown");if(C.add(G),b.type==="result"&&b.result&&H.length===0){let E=typeof b.result=="string"?b.result:"";E&&(H.push(E),F?.({type:"delta",jobId:s,text:E},s))}if(b.type==="assistant"&&Array.isArray(b.message?.content)){typeof b.error=="string"&&(N=b.error);for(let E of b.message.content){if(E.type==="text"&&E.text&&(H.push(E.text),F?.({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=ts(E);if(F?.({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};K.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&&(K.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&&F?.({type:"thinking",jobId:s,text:E.thinking},s)}}b.type==="result"&&b.is_error===!0&&(Q=!0,typeof b.error=="string"&&(N=b.error),typeof b.result=="string"&&(_=b.result)),b.type==="user"&&b.tool_use_result?.file?.filePath&&F?.({type:"tool_use",jobId:s,tool:"Read",file:b.tool_use_result.file.filePath},s)}catch{}});let J=[];A.stderr?.on("data",x=>{J.push(x.toString())}),A.on("close",x=>{if(clearTimeout(k),w.close(),H.length===0&&C.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...C].join(", ")}`),R)Q=!0,V=`Timed out after ${Math.round(v/6e4)} minutes`;else if(x!==0&&x!==null){Q=!0;let G=J.join("").trim(),E=H.join("").trim(),ie=H.length===0&&C.size>0?` (no text captured, event types: ${[...C].join(", ")})`:"";V=G||_||E||`Claude process exited with code ${x}${ie}`}Q&&!V&&_&&(V=_);let b=ns(V,N);S({sessionId:X,text:H.join(""),success:!Q,error:Q?b.message:void 0,errorCode:Q?b.code:void 0,recoverableError:Q?b.recoverable:void 0,fileEdits:K.length>0?K:void 0,editEvents:Z.length>0?Z:void 0})}),A.on("error",x=>{clearTimeout(k),Q=!0,V=x.message,S({sessionId:X,text:H.join(""),success:!1,error:V,errorCode:"claude_spawn_error",recoverableError:!1,fileEdits:K.length>0?K:void 0,editEvents:Z.length>0?Z:void 0})})});return{process:A,result:W}}function ns(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 ss,spawnSync as Rt}from"child_process";import{readFileSync as os,statSync as rs}from"fs";import{basename as is,extname as as,isAbsolute as cs,relative as It,resolve as ls}from"path";import{createInterface as ds}from"readline";function bt(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}var xt=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"]),nt=2e5,us=1e5,Tt=2e5;function Mt(s){let t=is(s).toLowerCase(),n=as(s).toLowerCase();return xt.has(n)||xt.has(t)}function Ct(s,t){let n=cs(t)?t:ls(s,t),e=It(s,n);return e===""||e.startsWith("..")?null:n}function Fe(s,t){if(!Mt(t))return;let n=Ct(s,t);if(n)try{let e=rs(n);return e.isFile()?e.size>nt?{exists:!0,truncated:!0}:{exists:!0,content:os(n,"utf8")}:{exists:!1}}catch{return{exists:!1}}}function ps(s,t){for(let n of t){let e=s[n];if(typeof e=="string")return e}}function fs(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 kt(s){return ps(s,["patch","diff","unifiedDiff","unified_diff"])}function st(s,t){return s.length<=t?{text:s}:{text:s.slice(0,t),truncated:!0}}function Pt(s,t){let n=Rt("git",t,{cwd:s,encoding:"utf8",timeout:5e3,maxBuffer:Tt+65536});return n.status!==0?null:n.stdout??""}function hs(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 Et(s){let t=Pt(s,["status","--porcelain=v1","-uall"]);if(t===null)return;let n=Pt(s,["diff","--no-ext-diff","HEAD","--"])??"",e=st(n,Tt);return{status:t,changedFiles:hs(t),...e.text?{diff:e.text}:{},...e.truncated?{diffTruncated:!0}:{}}}function ms(s,t){return!s||!t?!1:s.status!==t.status||(s.diff??"")!==(t.diff??"")}function gs(s,t){if(!Mt(t))return;let n=Ct(s,t);if(!n)return;let e=It(s,n);if(e===""||e.startsWith(".."))return;let o=Rt("git",["show",`HEAD:${e}`],{cwd:s,encoding:"utf8",timeout:5e3,maxBuffer:nt+65536});if(o.status!==0)return{exists:!1};let i=o.stdout??"",c=st(i,nt);return{exists:!0,content:c.text,...c.truncated?{truncated:!0}:{}}}function vs(s,t,n){return t.map(e=>{let o=n.has(e)?n.get(e):gs(s,e),i=Fe(s,e);return{filePath:e,...o?{before:o}:{},...i?{after:i}:{}}})}function Be(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:c,onEvent:a,sandboxMode:m="workspace-write",configOverrides:P=[]}=t,v=[];if(i){v.push("exec","resume","--skip-git-repo-check","--json","-c",`sandbox_mode="${m}"`);for(let A of P)v.push("-c",A);c&&v.push("-m",c),v.push(i),v.push(n),o&&v.push("--image",o)}else{v.push("exec","--skip-git-repo-check","--json","--sandbox",m);for(let A of P)v.push("-c",A);c&&v.push("-m",c),v.push(n),o&&v.push("--image",o)}let F=ss("codex",v,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),z=new Promise(A=>{let W,S=[],X=[],H=[],K=new Map,Z=new Map,Q=!1,V="",N=ds({input:F.stdout}),_=new Set;N.on("line",k=>{if(k.trim())try{let w=JSON.parse(k),C=w.type??"unknown";if(_.add(C),C==="thread.started"&&w.thread_id&&!W&&(W=w.thread_id),(C==="item.agentMessage.delta"||C==="item/agentMessage/delta")&&w.delta?.text&&(S.push(w.delta.text),a?.({type:"delta",jobId:s,text:w.delta.text},s)),(C==="item.reasoning.delta"||C==="item/reasoning/delta")&&w.delta?.text&&a?.({type:"thinking",jobId:s,text:w.delta.text},s),(C==="item.started"||C==="item/started")&&w.item){let J=w.item.type;if(J==="command_execution"){let x=w.item.command,b=x?bt(x):void 0,G=Et(e),E=new Map;for(let re of G?.changedFiles??[])E.set(re,Fe(e,re));typeof w.item.id=="string"&&Z.set(w.item.id,{...x?{rawCommand:x}:{},...b?{command:b}:{},...G?{beforeWorktree:G}:{},beforeSnapshots:E});let ie=b?`Bash: ${b.split(`
31
+ `)[0].slice(0,80)}`:"Bash";X.push(ie),a?.({type:"tool_use",jobId:s,tool:"Bash",...b?{content:b}:{}},s)}else if(J==="file_change"){let x=w.item.filename||w.item.path;x&&!K.has(x)&&K.set(x,Fe(e,x)),X.push(x?`Edit ${x.split("/").pop()}`:"Edit"),a?.({type:"tool_use",jobId:s,tool:"Edit",...x?{file:x}:{}},s)}else if(J==="file_read"){let x=w.item.filename||w.item.path;X.push(x?`Read ${x.split("/").pop()}`:"Read"),a?.({type:"tool_use",jobId:s,tool:"Read",...x?{file:x}:{}},s)}else if(J==="web_search")X.push("WebSearch"),a?.({type:"tool_use",jobId:s,tool:"WebSearch"},s);else if(J==="mcp_tool_call"){let x=w.item.tool_name||w.item.name||"MCP";X.push(x),a?.({type:"tool_use",jobId:s,tool:x},s)}}if((C==="item.completed"||C==="item/completed")&&w.item){if(w.item.type==="agent_message"){let J=w.item.text;typeof J=="string"&&J&&(S.push(J),a?.({type:"delta",jobId:s,text:J},s))}else if(w.item.type==="reasoning"){let J=w.item.text;typeof J=="string"&&J&&a?.({type:"thinking",jobId:s,text:J},s)}else if(w.item.type==="file_change"&&Array.isArray(w.item.changes))for(let J of w.item.changes){let x=J.path||J.filename,b=J.kind==="add"?"Write":"Edit";if(x){X.push(`${b} ${x.split("/").pop()}`),a?.({type:"tool_use",jobId:s,tool:b,file:x},s);let G=J,E=typeof G.kind=="string"?G.kind:void 0,ie=fs(E),re=K.get(x),ne=Fe(e,x);H.push({sequence:H.length,occurredAt:Date.now(),provider:"codex",source:"codex_file_change",tool:"FileChange",operation:ie,filePath:x,...re?{before:re}:{},...ne?{after:ne}:{},...kt(G)?{patch:kt(G)}:{},...E?{rawKind:E}:{}}),K.set(x,ne)}}else if(w.item.type==="command_execution"){let J=typeof w.item.id=="string"?w.item.id:void 0,x=J?Z.get(J):void 0,b=typeof w.item.command=="string"?w.item.command:x?.rawCommand,G=b?bt(b):x?.command,E=Et(e);if(ms(x?.beforeWorktree,E)){let ie=[...new Set([...x?.beforeWorktree?.changedFiles??[],...E?.changedFiles??[]])],re=typeof w.item.aggregated_output=="string"?st(w.item.aggregated_output,us):void 0,ne=typeof w.item.exit_code=="number"?w.item.exit_code:w.item.exit_code===null?null:void 0,De=typeof w.item.status=="string"?w.item.status:void 0;H.push({sequence:H.length,occurredAt:Date.now(),provider:"codex",source:"codex_command_execution",tool:"Bash",operation:"command",...b?{rawCommand:b}:{},...G?{command:G}:{},...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:vs(e,ie,x?.beforeSnapshots??new Map)}:{}})}J&&Z.delete(J)}}C==="turn.failed"&&(Q=!0,V=w.error?.message||w.message||"Turn failed")}catch{}});let R=[];F.stderr?.on("data",k=>{R.push(k.toString())}),F.on("close",k=>{N.close(),S.length===0&&_.size>0&&console.warn(`[Codex:${s}] No text captured. Event types seen: ${[..._].join(", ")}`),k!==0&&k!==null&&(Q=!0,V=R.join("")||`Codex process exited with code ${k}`),A({sessionId:W,text:S.join(""),success:!Q,error:Q?V:void 0,editEvents:H.length>0?H:void 0,toolsUsed:X.length>0?X:void 0})}),F.on("error",k=>{Q=!0,V=k.message,A({sessionId:W,text:S.join(""),success:!1,error:V,editEvents:H.length>0?H:void 0,toolsUsed:X.length>0?X:void 0})})});return{process:F,result:z}}import{spawn as ys}from"child_process";import{createInterface as ws}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 _t(s,t){let{prompt:n,projectRoot:e,resumeSessionId:o,model:i,timeoutMs:c=3e5,onEvent:a,copilotPath:m="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 v=ys(m,P,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),F=new Promise(z=>{let A,W=[],S=[],X=!1,H="",K=!1,Z=setTimeout(()=>{K=!0,v.kill("SIGTERM"),setTimeout(()=>{try{v.kill("SIGKILL")}catch{}},5e3)},c),Q=ws({input:v.stdout}),V=new Set,N=[];Q.on("line",R=>{if(R.trim())try{let k=JSON.parse(R),w=ke(k);V.add(w),N.length<5&&bs(w)&&N.push(R.slice(0,800));let C=xe(k,["sessionId","session_id","id"],["session","conversation","thread"]);C&&!A&&(A=C);let J=Ss(k,W.length===0);J&&(W.push(J),a?.({type:"delta",jobId:s,text:J},s));let x=xs(k);x&&a?.({type:"thinking",jobId:s,text:x},s);let b=ks(k);b&&(S.push(b.label),a?.({type:"tool_use",jobId:s,tool:b.tool,...b.file?{file:b.file}:{},...b.content?{content:b.content}:{}},s));let G=Ps(k);G&&(X=!0,H=G)}catch{}});let _=[];v.stderr?.on("data",R=>{_.push(R.toString())}),v.on("close",R=>{clearTimeout(Z),Q.close(),W.length===0&&V.size>0&&(console.warn(`[Copilot:${s}] No text captured. Event types seen: ${[...V].join(", ")}`),N.length>0&&console.warn(`[Copilot:${s}] Sample text-bearing candidates: ${N.join(`
32
+ `)}`)),K?(X=!0,H=`Timed out after ${Math.round(c/6e4)} minutes`):R!==0&&R!==null&&(X=!0,H=_.join("").trim()||H||`Copilot process exited with code ${R}`);let k=X?$t(H,i):null;z({sessionId:A,text:W.join(""),success:!X,error:k?.message,errorCode:k?.code,recoverableError:k?.recoverable,provider:"copilot",toolsUsed:S.length>0?S:void 0})}),v.on("error",R=>{clearTimeout(Z);let k=$t(R.message,i);z({sessionId:A,text:W.join(""),success:!1,error:k.message,errorCode:k.code,recoverableError:k.recoverable,provider:"copilot",toolsUsed:S.length>0?S:void 0})})});return{process:v,result:F}}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 Ss(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"&&ot(s))return s.text;if(typeof s.message=="string"&&ot(s))return s.message;if(typeof s.delta=="string"&&ot(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 bs(s){let t=s.toLowerCase();return t.includes("assistant.message")||t==="result"}function xs(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 ks(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 Ps(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(c=>e.toLowerCase().includes(c)))continue;let i=xe(o,t);if(i)return i}}function ot(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 Es}from"child_process";import{copyFile as Ot,mkdir as At,readdir as Rs,readFile as Is,writeFile as Ts}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 At(this.decisionsDir,{recursive:!0}),await At(this.screenshotsDir,{recursive:!0});try{await Ot(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 Ot(e[o],ve(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await Ts(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 Rs(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 Is(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=>{Es("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 Dt,writeFile as Ee}from"fs/promises";import{join as Re}from"path";var Ms=80,Cs=24,Nt=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 Dt(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 Dt(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(c=>[c.id,c])),o=new Set;for(let c of t){let a=e.get(c.id);a&&(a.title!==c.title||a.prompt!==c.prompt||JSON.stringify(a.assertions)!==JSON.stringify(c.assertions))&&o.add(c.id),e.set(c.id,c)}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(a=>a.id===t?{...a,...n.status?{status:n.status}:{},...n.title?{title:n.title}:{},...n.assertions?{assertions:n.assertions}:{}}:a),c=i.find(a=>a.id===t)??null;return c?(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])),c):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 Jt(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?jt(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?jt(e,"pending"):null}catch{return null}}function Bt(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=Ns(e.assertionResults,t),i=Js(o);return{status:i,summary:fe(e.summary)||Lt(i),assertionResults:o,tooling:Fs(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 jt(s,t="approved"){let n=[];return s.forEach((e,o)=>{if(!e||typeof e!="object")return;let i=e,c=fe(i.title),a=fe(i.prompt),m=fe(i.rationale),P=Se(i.assertions);if(!c||!a||P.length===0)return;let v=Os(i.category),F=Bs(fe(i.id))||`eval-${Wt(c).slice(0,32)||o+1}`,z=As(i.status)??t;n.push({id:F,title:c,status:z,category:v,prompt:a,assertions:P,rationale:m||"Inferred from Popmelt design history.",sourceDecisionIds:Se(i.sourceDecisionIds),sourceThreadIds:Se(i.sourceThreadIds)})}),n}function Ut(s){let t=[...s.decisions].sort((e,o)=>o.createdAt-e.createdAt).slice(0,Ms),n=Object.values(s.threads.threads).sort((e,o)=>o.updatedAt-e.updatedAt).slice(0,Cs);return`You are helping a designer turn organic Popmelt annotation history into a project eval suite.
34
34
 
35
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
36
 
@@ -86,7 +86,7 @@ Allowed categories: component, layout, copy, interaction, visual, workflow.
86
86
  Return only the <evals> block after approval.
87
87
 
88
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 Ut(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.
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 zt(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
90
 
91
91
  ## Eval
92
92
  \`\`\`json
@@ -147,16 +147,16 @@ Thread: ${s.threadId??"none"}
147
147
  Created: ${new Date(s.createdAt).toISOString()}
148
148
  URL: ${s.url||"unknown"}
149
149
  Instructions:
150
- ${ze(o||"(none)",Dt)}
150
+ ${ze(o||"(none)",Nt)}
151
151
  Resolutions:
152
- ${ze(i||"(none)",Dt)}
152
+ ${ze(i||"(none)",Nt)}
153
153
  Files: ${c.length>0?c.join(", "):"none"}`}function _s(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(c=>`${c.status}: ${c.summary}`).join("; ")??"";return`- ${e.role}: ${ze(o||i||"(no text)",500)}`}).join(`
154
154
  `);return`### Thread ${s.id}
155
155
  Messages: ${s.messages.length}
156
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 Os(s){let t=fe(s);return t==="component"||t==="layout"||t==="copy"||t==="interaction"||t==="visual"||t==="workflow"?t:"visual"}function As(s){let t=fe(s);return t==="pending"||t==="approved"||t==="rejected"?t:null}function Ds(s){let t=fe(s);return t==="pass"||t==="fail"||t==="needs_review"?t:null}function Ns(s,t){let e=(Array.isArray(s)?s:[]).map(o=>{if(!o||typeof o!="object")return null;let i=o,c=fe(i.assertion);return c?{assertion:c,status:Ds(i.status)??"needs_review",summary:fe(i.summary)||zt("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 Js(s){return s.some(t=>t.status==="fail")?"fail":s.some(t=>t.status==="needs_review")?"needs_review":"pass"}function Fs(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 zt(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 Bs(s){return Lt(s).slice(0,48)}function Lt(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 Wt,writeFile as ge}from"fs/promises";import{join as rt}from"path";var te="[Materializer]",js={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function Us(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=rt(t,".popmelt");this.indexPath=rt(o,"materialized.json"),this.modelPath=rt(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await Wt(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=Us(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 m=0;m<o.length-1;m++){let P=o[m];(!i[P]||typeof i[P]!="object")&&(i[P]={}),i=i[P]}let c=o[o.length-1],a;try{a=JSON.parse(n)}catch{a=null}if(a&&typeof a=="object"&&a!==null&&"value"in a)i[c]=a;else{let m=i[c];m&&typeof m=="object"&&m!==null&&"value"in m?m.value=n:i[c]=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 c=0;c<e.length-1;c++){let a=e[c];if(!o[a]||typeof o[a]!="object")return{removed:!1};o=o[a]}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(c=>!n.has(c));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(c=>c.resolutions.some(a=>(a.finalScope??a.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=zs(n,o),c=!0,a;try{let g=`mat-${Date.now()}`,{provider:F,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)c=!1,a=O.error,console.error(`${te} ${F} spawn error:`,a);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 ${F} \u2192 ${this.modelPath}`)):(c=!1,a="No <model> block found in response",console.error(`${te} ${a}`))}}catch(g){c=!1,a=g instanceof Error?g.message:String(g),console.error(`${te} Error:`,a)}let m=await this.loadIndex(),P=new Set(m.materializedIds);for(let g of e)P.add(g);return m.materializedIds=[...P],m.lastRunAt=Date.now(),m.lastRunDecisionIds=e,m.lastRunError=a??null,await this.persistIndex(m),this.options.onEvent?.({type:"materialize_done",decisionIds:e,success:c,error:a}),{processedIds:e,success:c,error:a}}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=Ls(n),{provider:o,result:i}=this.spawnModelAgent(`consolidate-${Date.now()}`,e,t,{maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[]}),c=await i;if(!c.success)return console.error(`${te} ${o} consolidation spawn error:`,c.error),{success:!1,error:c.error};let a=We(c.text);return a?(Array.isArray(a.rules)&&(a.rules=Te(a.rules)),!a.tokens&&n.tokens&&(a.tokens=n.tokens),!a.components&&n.components&&(a.components=n.components),await ge(this.modelPath,JSON.stringify(a,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 Wt(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...js,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",c=this.options.onEvent?(a,m)=>this.options.onEvent?.(a):void 0;return i==="codex"?{provider:"codex",...Be(t,{prompt:n,projectRoot:this.projectRoot,model:e.model??this.options.model,timeoutMs:this.options.timeoutMs,sandboxMode:"read-only",onEvent:c})}:(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:c})})}};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 zs(s,t){let n=s.map(i=>{let a=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
- `),m=i.annotations.map(g=>g.instruction).filter(Boolean).join(`
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 Os(s){let t=fe(s);return t==="component"||t==="layout"||t==="copy"||t==="interaction"||t==="visual"||t==="workflow"?t:"visual"}function As(s){let t=fe(s);return t==="pending"||t==="approved"||t==="rejected"?t:null}function Ds(s){let t=fe(s);return t==="pass"||t==="fail"||t==="needs_review"?t:null}function Ns(s,t){let e=(Array.isArray(s)?s:[]).map(o=>{if(!o||typeof o!="object")return null;let i=o,c=fe(i.assertion);return c?{assertion:c,status:Ds(i.status)??"needs_review",summary:fe(i.summary)||Lt("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 Js(s){return s.some(t=>t.status==="fail")?"fail":s.some(t=>t.status==="needs_review")?"needs_review":"pass"}function Fs(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 Lt(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 Bs(s){return Wt(s).slice(0,48)}function Wt(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 Ht,writeFile as ge}from"fs/promises";import{join as rt}from"path";var te="[Materializer]",js={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function Us(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 Ie(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=rt(t,".popmelt");this.indexPath=rt(o,"materialized.json"),this.modelPath=rt(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await Ht(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=Us(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 m=0;m<o.length-1;m++){let P=o[m];(!i[P]||typeof i[P]!="object")&&(i[P]={}),i=i[P]}let c=o[o.length-1],a;try{a=JSON.parse(n)}catch{a=null}if(a&&typeof a=="object"&&a!==null&&"value"in a)i[c]=a;else{let m=i[c];m&&typeof m=="object"&&m!==null&&"value"in m?m.value=n:i[c]=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 c=0;c<e.length-1;c++){let a=e[c];if(!o[a]||typeof o[a]!="object")return{removed:!1};o=o[a]}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(c=>!n.has(c));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(c=>c.resolutions.some(a=>(a.finalScope??a.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(v=>v.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=zs(n,o),c=!0,a;try{let v=`mat-${Date.now()}`,{provider:F,result:z}=this.spawnModelAgent(v,i,t,{maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"]}),A=await z;if(!A.success)c=!1,a=A.error,console.error(`${te} ${F} spawn error:`,a);else{let W=We(A.text);W?(Array.isArray(W.rules)&&(W.rules=Ie(W.rules)),await ge(this.modelPath,JSON.stringify(W,null,2)),console.log(`${te} Successfully materialized ${e.length} decision(s) with ${F} \u2192 ${this.modelPath}`)):(c=!1,a="No <model> block found in response",console.error(`${te} ${a}`))}}catch(v){c=!1,a=v instanceof Error?v.message:String(v),console.error(`${te} Error:`,a)}let m=await this.loadIndex(),P=new Set(m.materializedIds);for(let v of e)P.add(v);return m.materializedIds=[...P],m.lastRunAt=Date.now(),m.lastRunDecisionIds=e,m.lastRunError=a??null,await this.persistIndex(m),this.options.onEvent?.({type:"materialize_done",decisionIds:e,success:c,error:a}),{processedIds:e,success:c,error:a}}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=Ls(n),{provider:o,result:i}=this.spawnModelAgent(`consolidate-${Date.now()}`,e,t,{maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[]}),c=await i;if(!c.success)return console.error(`${te} ${o} consolidation spawn error:`,c.error),{success:!1,error:c.error};let a=We(c.text);return a?(Array.isArray(a.rules)&&(a.rules=Ie(a.rules)),!a.tokens&&n.tokens&&(a.tokens=n.tokens),!a.components&&n.components&&(a.components=n.components),await ge(this.modelPath,JSON.stringify(a,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=Ie(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 Ht(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...js,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",c=this.options.onEvent?(a,m)=>this.options.onEvent?.(a):void 0;return i==="codex"?{provider:"codex",...Be(t,{prompt:n,projectRoot:this.projectRoot,model:e.model??this.options.model,timeoutMs:this.options.timeoutMs,sandboxMode:"read-only",onEvent:c})}:(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:c})})}};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 zs(s,t){let n=s.map(i=>{let a=i.resolutions.filter(v=>(v.finalScope??v.inferredScope)?.breadth==="pattern").map(v=>{let z=(v.finalScope??v.inferredScope)?.target??"unknown",A=v.filesModified?.join(", ")??"none";return`- **${v.summary}** [scope: pattern/${z}]
158
+ Files modified: ${A}`}).join(`
159
+ `),m=i.annotations.map(v=>v.instruction).filter(Boolean).join(`
160
160
  `),P=i.gitDiff?`
161
161
  \`\`\`diff
162
162
  ${i.gitDiff.slice(0,2e3)}
@@ -279,7 +279,7 @@ Output the complete model inside <model> tags. Preserve tokens and components as
279
279
 
280
280
  <model>
281
281
  { "tokens": { ... }, "components": { ... }, "rules": [ ... ] }
282
- </model>`}function Ht(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 Gt(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.
283
283
 
284
284
  ## Current Model
285
285
  \`\`\`json
@@ -312,47 +312,47 @@ These are my proposed rule changes. Would you like to approve all of them, adjus
312
312
  </question>
313
313
 
314
314
  ## After approval
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 Ws}from"child_process";import{readFile as at}from"fs/promises";import{homedir as Ge}from"os";import{join as ue}from"path";var ct=/popmelt/i,Hs=/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(ct.test(t))return t;return null}async function He(s){try{let t=await at(s,"utf-8");return JSON.parse(t)}catch{return null}}async function lt(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 c=o.projects[s];if(c&&typeof c=="object"){let a=c;if(a.mcpServers&&typeof a.mcpServers=="object"){let m=Ie(a.mcpServers);if(m){let P=Array.isArray(a.disabledMcpjsonServers)&&a.disabledMcpjsonServers.some(g=>ct.test(g));return be(m,"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 c=Ie(o.mcpServers);if(c){let a=await Gt(s,c);return be(c,"mcp.json",a)}}let i=Ie(o);if(i&&i!=="mcpServers"){let c=await Gt(s,i);return be(i,"mcp.json",c)}}return ye()}async function Gt(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 Gs=/^\[mcp_servers\.([^\]]+)\]/;function Xs(s){return s.replace(/^"(.+)"$/,"$1")}function qs(s){return/\.tools\./.test(s)||/"\.tools\./.test(s)}function qt(s){let t=s.split(`
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 Ws}from"child_process";import{readFile as at}from"fs/promises";import{homedir as Ge}from"os";import{join as ue}from"path";var ct=/popmelt/i,Hs=/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 Te(s){for(let t of Object.keys(s))if(ct.test(t))return t;return null}async function He(s){try{let t=await at(s,"utf-8");return JSON.parse(t)}catch{return null}}async function lt(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=Te(o.mcpServers);if(i)return be(i,"user")}if(o.projects&&typeof o.projects=="object"){let c=o.projects[s];if(c&&typeof c=="object"){let a=c;if(a.mcpServers&&typeof a.mcpServers=="object"){let m=Te(a.mcpServers);if(m){let P=Array.isArray(a.disabledMcpjsonServers)&&a.disabledMcpjsonServers.some(v=>ct.test(v));return be(m,"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 c=Te(o.mcpServers);if(c){let a=await Xt(s,c);return be(c,"mcp.json",a)}}let i=Te(o);if(i&&i!=="mcpServers"){let c=await Xt(s,i);return be(i,"mcp.json",c)}}return ye()}async function Xt(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 Gs=/^\[mcp_servers\.([^\]]+)\]/;function Xs(s){return s.replace(/^"(.+)"$/,"$1")}function qs(s){return/\.tools\./.test(s)||/"\.tools\./.test(s)}function Yt(s){let t=s.split(`
316
316
  `),n=[],e=null;for(let o of t){let i=o.match(Gs);i?(e&&n.push({name:e.name,body:e.bodyLines.join(`
317
317
  `)}),e={name:i[1],bodyLines:[]}):o.startsWith("[")?e&&(n.push({name:e.name,body:e.bodyLines.join(`
318
318
  `)}),e=null):e&&e.bodyLines.push(o)}return e&&n.push({name:e.name,body:e.bodyLines.join(`
319
- `)}),n}function Yt(s){return/enabled\s*=\s*false/i.test(s)}async function dt(s){let t=process.env.CODEX_HOME||ue(Ge(),".codex"),n=await Xt(ue(t,"config.toml"),"user");if(n.found)return n;let e=await Xt(ue(s,".codex","config.toml"),"project");return e.found?e:ye()}async function Ys(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 at(o,"utf-8");for(let c of qt(i)){if(qs(c.name)||Yt(c.body))continue;let a=Xs(c.name);t.test(a)&&e.add(a)}}catch{}return[...e]}async function Kt(s){return Ys(s,Hs)}async function ut(s,t="copilot"){let n=await Ks(t);if(n.found)return n;let e=process.env.COPILOT_HOME||ue(Ge(),".copilot"),o=await it(ue(e,"mcp-config.json"),"user");if(o.found)return o;let i=await it(ue(s,".mcp.json"),"mcp.json");if(i.found)return i;let c=await it(ue(s,".github","mcp.json"),"mcp.json");return c.found?c:ye()}async function Ks(s){try{let t=await new Promise((n,e)=>{Ws(s,["mcp","list","--json"],{encoding:"utf-8",timeout:5e3},(o,i)=>{if(o){e(o);return}n(i)})});return Qt(JSON.parse(t),"user")}catch{return ye()}}async function it(s,t){let n=await He(s);return Qt(n,t)}function Qt(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 Xt(s,t){try{let n=await at(s,"utf-8"),e=qt(n);for(let o of e)if(ct.test(o.name)){let i=Yt(o.body);return be(o.name,t,i)}}catch{}return ye()}import{execFile as Vt}from"child_process";import{mkdir as Qs,readFile as Zt,writeFile as en}from"fs/promises";import{homedir as tn}from"os";import{dirname as Vs,join as pt}from"path";var ft="https://mcp.popmelt.com/mcp";async function nn(s=ft){let t=pt(tn(),".claude.json"),n;try{let o=await Zt(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 en(t,JSON.stringify(n,null,2)+`
320
- `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function sn(s=ft){let t=process.env.CODEX_HOME||pt(tn(),".codex"),n=pt(t,"config.toml"),e;try{e=await Zt(n,"utf-8")}catch{e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await Qs(Vs(n),{recursive:!0});let o=`
319
+ `)}),n}function Kt(s){return/enabled\s*=\s*false/i.test(s)}async function dt(s){let t=process.env.CODEX_HOME||ue(Ge(),".codex"),n=await qt(ue(t,"config.toml"),"user");if(n.found)return n;let e=await qt(ue(s,".codex","config.toml"),"project");return e.found?e:ye()}async function Ys(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 at(o,"utf-8");for(let c of Yt(i)){if(qs(c.name)||Kt(c.body))continue;let a=Xs(c.name);t.test(a)&&e.add(a)}}catch{}return[...e]}async function Qt(s){return Ys(s,Hs)}async function ut(s,t="copilot"){let n=await Ks(t);if(n.found)return n;let e=process.env.COPILOT_HOME||ue(Ge(),".copilot"),o=await it(ue(e,"mcp-config.json"),"user");if(o.found)return o;let i=await it(ue(s,".mcp.json"),"mcp.json");if(i.found)return i;let c=await it(ue(s,".github","mcp.json"),"mcp.json");return c.found?c:ye()}async function Ks(s){try{let t=await new Promise((n,e)=>{Ws(s,["mcp","list","--json"],{encoding:"utf-8",timeout:5e3},(o,i)=>{if(o){e(o);return}n(i)})});return Vt(JSON.parse(t),"user")}catch{return ye()}}async function it(s,t){let n=await He(s);return Vt(n,t)}function Vt(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=Te(e);return o?be(o,t):ye()}async function qt(s,t){try{let n=await at(s,"utf-8"),e=Yt(n);for(let o of e)if(ct.test(o.name)){let i=Kt(o.body);return be(o.name,t,i)}}catch{}return ye()}import{execFile as Zt}from"child_process";import{mkdir as Qs,readFile as en,writeFile as tn}from"fs/promises";import{homedir as nn}from"os";import{dirname as Vs,join as pt}from"path";var ft="https://mcp.popmelt.com/mcp";async function sn(s=ft){let t=pt(nn(),".claude.json"),n;try{let o=await en(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 tn(t,JSON.stringify(n,null,2)+`
320
+ `,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function on(s=ft){let t=process.env.CODEX_HOME||pt(nn(),".codex"),n=pt(t,"config.toml"),e;try{e=await en(n,"utf-8")}catch{e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await Qs(Vs(n),{recursive:!0});let o=`
321
321
  [mcp_servers.popmelt]
322
322
  url = "${s}"
323
- `;return await en(n,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function on(s=ft,t="copilot"){let n=await Zs(t);if(eo(n))return{installed:!1,provider:"copilot",scope:null,reason:"already_configured"};try{return await new Promise((e,o)=>{Vt(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 Zs(s){try{let t=await new Promise((n,e)=>{Vt(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 eo(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 to(s),i=Buffer.from(`--${e}`),c=Buffer.from(`--${e}--`),a,m,P,g,F,z,O,L,S,X,W,K,Z=[],Q=[],V=0,D=[];for(;V<o.length;){let _=o.indexOf(i,V);if(_===-1)break;let R=_+i.length;if(o.slice(_,_+c.length).equals(c))break;let k=R;o[k]===13&&o[k+1]===10&&(k+=2);let y=o.indexOf(`\r
323
+ `;return await tn(n,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function rn(s=ft,t="copilot"){let n=await Zs(t);if(eo(n))return{installed:!1,provider:"copilot",scope:null,reason:"already_configured"};try{return await new Promise((e,o)=>{Zt(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 Zs(s){try{let t=await new Promise((n,e)=>{Zt(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 eo(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 to(s),i=Buffer.from(`--${e}`),c=Buffer.from(`--${e}--`),a,m,P,v,F,z,A,W,S,X,H,K,Z=[],Q=[],V=0,N=[];for(;V<o.length;){let _=o.indexOf(i,V);if(_===-1)break;let R=_+i.length;if(o.slice(_,_+c.length).equals(c))break;let k=R;o[k]===13&&o[k+1]===10&&(k+=2);let w=o.indexOf(`\r
324
324
  \r
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)}),V=x!==-1?x:o.length}for(let _ of D){let R=_.headers.match(/name="([^"]+)"/);if(!R)continue;let k=R[1];if(k==="screenshot")a=_.body;else if(k==="feedback")m=_.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")F=_.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")K=_.body.toString("utf-8");else if(k.startsWith("screenshot-")){let y=k.slice(11);try{let C=decodeURIComponent(y);Q.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(!a)throw new Error("Missing screenshot field");return m||(m=""),{screenshot:a,feedback:m,color:P,provider:g,model:F,goal:z,pageUrl:O,viewport:L,planId:S,manifest:X,tasks:W,sourceId:K,pastedImages:Z,pageScreenshots:Q}}function to(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(a=>{let m=[a.selector];return a.reactComponent&&m.push(`(${a.reactComponent})`),m.join(" ")}).join(", "),i=e.instruction||"No text";n.push(`- id=${e.id} [${e.type}] ${i} \u2192 Elements: ${o||"none"}`);let c=t?.[e.id];if(c&&c.length>0)for(let a of c)n.push(` Attached image: use the Read tool to view ${a}`)}}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(w===-1)break;let C=o.slice(k,w).toString("utf-8"),J=w+4,x=o.indexOf(i,J),b=x!==-1?x-2:o.length;N.push({headers:C,body:o.slice(J,b)}),V=x!==-1?x:o.length}for(let _ of N){let R=_.headers.match(/name="([^"]+)"/);if(!R)continue;let k=R[1];if(k==="screenshot")a=_.body;else if(k==="feedback")m=_.body.toString("utf-8");else if(k==="color")P=_.body.toString("utf-8");else if(k==="provider")v=_.body.toString("utf-8");else if(k==="model")F=_.body.toString("utf-8");else if(k==="goal")z=_.body.toString("utf-8");else if(k==="pageUrl")A=_.body.toString("utf-8");else if(k==="viewport")W=_.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")H=_.body.toString("utf-8");else if(k==="sourceId")K=_.body.toString("utf-8");else if(k.startsWith("screenshot-")){let w=k.slice(11);try{let C=decodeURIComponent(w);Q.push({pathname:C,data:_.body})}catch{}}else if(k.startsWith("image-")){let w=k.split("-"),C=parseInt(w[w.length-1],10),J=w.slice(1,-1).join("-");J&&!isNaN(C)&&Z.push({annotationId:J,index:C,data:_.body})}}if(!a)throw new Error("Missing screenshot field");return m||(m=""),{screenshot:a,feedback:m,color:P,provider:v,model:F,goal:z,pageUrl:A,viewport:W,planId:S,manifest:X,tasks:H,sourceId:K,pastedImages:Z,pageScreenshots:Q}}function to(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(a=>{let m=[a.selector];return a.reactComponent&&m.push(`(${a.reactComponent})`),m.join(" ")}).join(", "),i=e.instruction||"No text";n.push(`- id=${e.id} [${e.type}] ${i} \u2192 Elements: ${o||"none"}`);let c=t?.[e.id];if(c&&c.length>0)for(let a of c)n.push(` Attached image: use the Read tool to view ${a}`)}}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(`
326
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(`
327
- `)}function rn(s,t,n){let e=[],i=new Set(t.annotations.map(c=>c.pathname).filter(Boolean)).size>1;if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),!i&&n?.provider!=="codex"&&(e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`),e.push("")),e.push(`The developer annotated their running app at ${t.url} (${t.viewport.width}x${t.viewport.height}).`),n?.threadHistory&&n.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let c=0;for(let a of n.threadHistory)if(a.role==="human")c++,a.replyToQuestion?(e.push(`### Round ${c} (human) \u2014 reply`),e.push(`"${a.replyToQuestion}"`)):(e.push(`### Round ${c} (human)`),a.feedbackSummary&&e.push(`Annotations: ${a.feedbackSummary}`),a.annotationIds&&a.annotationIds.length>0&&e.push(`Annotation IDs: ${a.annotationIds.join(", ")}`));else if(a.question)e.push(`### Round ${c} (assistant) \u2014 question`),e.push(`"${a.question}"`);else{if(e.push(`### Round ${c} (assistant)`),a.responseText&&e.push(`Response: ${a.responseText}`),a.resolutions&&a.resolutions.length>0)for(let m of a.resolutions){let P=m.finalScope??m.inferredScope,g=P?` [${P.breadth} ${P.target}]`:"";e.push(`- ${m.annotationId}: ${m.status}${g} \u2014 ${m.summary}`),m.filesModified&&m.filesModified.length>0&&e.push(` Files: ${m.filesModified.join(", ")}`)}a.toolsUsed&&a.toolsUsed.length>0&&e.push(`Tools used: ${a.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(n?.designModel){e.push(""),e.push("## Established Design Policies"),e.push("This project has an established design model (stored in .popmelt/model.json), extracted from the developer's previous design decisions. When making changes, follow these patterns unless the developer explicitly overrides them. When asked about design tokens, component patterns, or design decisions, reference this model as the authoritative source.");let c=n.designModel.rules;if(Array.isArray(c)&&c.length>0)if(e.push(""),e.push("Rules:"),c.length>0&&typeof c[0]=="object"&&c[0]!==null&&"scope"in c[0]){let g=new Map;for(let F of c)if(typeof F=="object"&&F!==null&&"text"in F){let z=F,O=z.scope||"general";g.has(O)||g.set(O,[]),g.get(O).push(z.text)}for(let[F,z]of g){e.push(`**${F.charAt(0).toUpperCase()+F.slice(1)}**`);for(let O of z)e.push(`- ${O}`)}}else for(let g of c)typeof g=="string"&&e.push(`- ${g}`);let a=n.designModel.tokens;a&&typeof a=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(a,null,2)),e.push("```"));let m=n.designModel.components;m&&typeof m=="object"&&(e.push(""),e.push("Component patterns:"),e.push("```json"),e.push(JSON.stringify(m,null,2)),e.push("```")),e.push(""),e.push("### Novel Pattern Detection"),e.push("When you make a design decision that has no matching policy in the model above (e.g., styling a component type not yet in the model, choosing a color with no token, picking spacing with no rule), flag it:"),e.push("<novel>"),e.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),e.push("</novel>"),e.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),e.push("- `element`: What you are styling or creating"),e.push("- `decision`: What you decided to do (specific values)"),e.push("- `reason`: Why this is novel (what is missing from the model)"),e.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}if(n?.designModel||(e.push(""),e.push("## Design Context"),e.push("This project uses Popmelt for design governance. Design decisions are stored in .popmelt/decisions/ (JSON files). A materialized design model may exist at .popmelt/model.json. When the developer asks about design tokens, patterns, or past decisions, check these files first before searching source code.")),i){let c=n?.screenshotPaths??{},a=new Map;for(let m of t.annotations){let P=m.pathname||new URL(t.url).pathname;a.has(P)||a.set(P,[]),a.get(P).push(m)}e.push(""),e.push("The developer annotated multiple pages. Each page section includes its own screenshot where available.");for(let[m,P]of a){e.push(""),e.push(`## Page: ${m}`);let g=c[m];g&&n?.provider!=="codex"?e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${g}`):!g&&n?.provider!=="codex"&&(Object.keys(c).length===0?(e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${s}`),e.push("(This screenshot shows the page the developer was on when submitting \u2014 not this page)")):e.push("(No screenshot available for this page \u2014 work from the annotation text and selectors)"));let F={...t,annotations:P,styleModifications:t.styleModifications},z=Me(F,n?.imagePaths);z&&(e.push(""),e.push(z))}}else{let c=Me(t,n?.imagePaths);c&&(e.push(""),e.push(c))}return e.push(""),e.push("Follow the developer's instructions. If they ask for changes, apply them to the source files \u2014 the dev server has HMR so changes appear immediately. If they ask a question or request analysis, respond in text without modifying code."),e.push(""),e.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e.push(""),e.push("## Resolution"),e.push("After completing all work, output a resolution block listing what you did for each annotation:"),e.push("<resolution>"),e.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),e.push("</resolution>"),e.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),e.push(""),e.push("### Scope classification"),e.push("Each resolution MUST include scope fields:"),e.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),e.push("- `inferredScope`: What scope the change actually has, based on what you modified."),e.push("Scope has two dimensions:"),e.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),e.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),e.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),e.push("If you cannot confidently determine scope, set it to null."),e.push(""),e.push("## Questions"),e.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),e.push('<question>What do you mean by "..."?</question>'),e.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),e.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),e.join(`
328
- `)}function an(s){return s.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function cn(s,t,n,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),n!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`);let i=t.find(c=>c.role==="human"&&c.feedbackContext);if(i?.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),t.length>0){o.push(""),o.push("## Conversation History");let c=0;for(let a of t)a.role==="human"?(c++,a.replyToQuestion?(o.push(`### Round ${c} (human) \u2014 reply`),o.push(`"${a.replyToQuestion}"`)):(o.push(`### Round ${c} (human)`),a.feedbackSummary&&o.push(`Annotations: ${a.feedbackSummary}`))):a.question?(o.push(`### Round ${c} (assistant) \u2014 question`),o.push(`"${a.question}"`)):(o.push(`### Round ${c} (assistant)`),a.responseText&&o.push(`Response: ${a.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let c of e)o.push(`Attached image: use the Read tool to view the image at: ${c}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
329
- `)}function no(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 so(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&&!no(t[n]))return!1;return!0}function ln(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(so):[]}catch{return[]}}function dn(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 oo}from"child_process";import{readdir as ro,readFile as io,stat as ao}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"},co=/^\s*-\s+"([^"]+)"/;function lo(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(co);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:ht(i),source:"cli"})}return Ke([_e,...t])}function uo(s){let t=JSON.parse(s);return(Array.isArray(t.models)?t.models:[]).filter(e=>e.visibility===void 0||e.visibility==="list").sort((e,o)=>{let i=typeof e.priority=="number"?e.priority:Number.MAX_SAFE_INTEGER,c=typeof o.priority=="number"?o.priority:Number.MAX_SAFE_INTEGER;return i-c}).flatMap(e=>{let o=typeof e.slug=="string"?e.slug:typeof e.id=="string"?e.id:"";if(!o)return[];let i=typeof e.display_name=="string"&&e.display_name.trim()?e.display_name.trim():ht(o);return[{id:o,label:i,source:"cli"}]})}function po(s){let t=s.match(/\bclaude-[a-z0-9]+(?:[-.][a-z0-9]+)*\b/gi)??[];return Ke(t.map(n=>({id:n,label:ht(n),source:"cli"})))}function ht(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":go(e[1]),c=e[2]&&e[3]?` ${e[2]}.${e[3]}`:"";return`${i}${c}${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 un(s="copilot"){let t=await qe(s,["help","config"]),n=lo(t);return n.length>1?n:[_e]}async function pn(s="codex"){let t=await qe(s,["debug","models"]).catch(async()=>{let e=process.env.CODEX_HOME||me(Ce(),".codex");return hn(me(e,"models_cache.json"))}),n=uo(t);return n.length>0?Ke([...n,...$e]):$e}async function fn(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(()=>""),fo(t).catch(()=>[])]),c=mo([...n,...i]),a=await Promise.all(c.map(P=>hn(P).catch(()=>""))),m=po([e,o,...a].join(`
331
- `));return Ke([...Ye,...m])}function qe(s,t,n=5e3){return new Promise((e,o)=>{oo(s,t,{encoding:"utf-8",timeout:n},(i,c)=>{if(i){o(i);return}e(c)})})}async function hn(s){return io(s,"utf-8")}async function fo(s){let t=[me(s,"projects"),me(s,"telemetry")],n=[];for(let e of t)await mn(e,n,4);return n.sort((e,o)=>o.mtimeMs-e.mtimeMs).slice(0,80).map(e=>e.path)}async function mn(s,t,n){if(n<0)return;let e;try{e=await ro(s,{withFileTypes:!0})}catch{return}await Promise.all(e.map(async o=>{let i=me(s,o.name);if(o.isDirectory()){await mn(i,t,n-1);return}if(!(!o.isFile()||!ho(o.name)))try{let c=await ao(i);if(c.size>2e6)return;t.push({path:i,mtimeMs:c.mtimeMs})}catch{}}))}function ho(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 mo(s){return[...new Set(s)]}function go(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 c=n<0?e.events:e.events.filter(a=>a.seq>n);return{jobId:t,events:c,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,c=()=>{i||(i=!0,o())};e.on("exit",c),e.on("error",c),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(c,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as vo,readFile as yo,writeFile as wo}from"fs/promises";import{dirname as So,join as bo}from"path";var xo={version:1,threads:{}},Ve=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=bo(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await yo(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={...xo,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(c=>e.has(c)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let c of n)i.has(c)||o.elementIdentifiers.push(c);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let c=o.messages.find(a=>a.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:c,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await vo(So(this.filePath),{recursive:!0}),await wo(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var gn="0.7.2";var $o=1111,_o=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],Oo=1800*1e3,Ao=3600*1e3;function kn(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 Do(s){let t=[];for(let n of s)if(n.type==="delta"){let e=n.text;if(!e)continue;let o=t[t.length-1];o&&o.kind==="text"?o.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",o=n.file??void 0,i=n.content??void 0,c=o?o.split("/").pop()??o:void 0,a;switch(e){case"Read":a=c?`Reading ${c}`:"Reading file";break;case"Edit":a=c?`Editing ${c}`:"Editing file";break;case"Write":a=c?`Writing ${c}`:"Writing file";break;case"Bash":a=i?i.split(`
332
- `)[0].trim().slice(0,60):"Running command";break;case"Glob":a="Searching files";break;case"Grep":a="Searching code";break;case"WebFetch":a="Fetching page";break;case"WebSearch":a="Searching web";break;default:a=e?`Using ${e}`:"tool";break}let m=o??i??void 0,P=t[t.length-1];P&&P.kind==="tool_group"&&P.tool===e?P.items.push({label:a,detail:m}):t.push({kind:"tool_group",tool:e,items:[{label:a,detail:m}]})}return t}function No(s,t){let n=s.headers.origin;kn(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 Jo(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 mt(s,t){try{s.res.write(`event: ${t.type}
327
+ `)}function an(s,t,n){let e=[],i=new Set(t.annotations.map(c=>c.pathname).filter(Boolean)).size>1;if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),!i&&n?.provider!=="codex"&&(e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`),e.push("")),e.push(`The developer annotated their running app at ${t.url} (${t.viewport.width}x${t.viewport.height}).`),n?.threadHistory&&n.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let c=0;for(let a of n.threadHistory)if(a.role==="human")c++,a.replyToQuestion?(e.push(`### Round ${c} (human) \u2014 reply`),e.push(`"${a.replyToQuestion}"`)):(e.push(`### Round ${c} (human)`),a.feedbackSummary&&e.push(`Annotations: ${a.feedbackSummary}`),a.annotationIds&&a.annotationIds.length>0&&e.push(`Annotation IDs: ${a.annotationIds.join(", ")}`));else if(a.question)e.push(`### Round ${c} (assistant) \u2014 question`),e.push(`"${a.question}"`);else{if(e.push(`### Round ${c} (assistant)`),a.responseText&&e.push(`Response: ${a.responseText}`),a.resolutions&&a.resolutions.length>0)for(let m of a.resolutions){let P=m.finalScope??m.inferredScope,v=P?` [${P.breadth} ${P.target}]`:"";e.push(`- ${m.annotationId}: ${m.status}${v} \u2014 ${m.summary}`),m.filesModified&&m.filesModified.length>0&&e.push(` Files: ${m.filesModified.join(", ")}`)}a.toolsUsed&&a.toolsUsed.length>0&&e.push(`Tools used: ${a.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(n?.designModel){e.push(""),e.push("## Established Design Policies"),e.push("This project has an established design model (stored in .popmelt/model.json), extracted from the developer's previous design decisions. When making changes, follow these patterns unless the developer explicitly overrides them. When asked about design tokens, component patterns, or design decisions, reference this model as the authoritative source.");let c=n.designModel.rules;if(Array.isArray(c)&&c.length>0)if(e.push(""),e.push("Rules:"),c.length>0&&typeof c[0]=="object"&&c[0]!==null&&"scope"in c[0]){let v=new Map;for(let F of c)if(typeof F=="object"&&F!==null&&"text"in F){let z=F,A=z.scope||"general";v.has(A)||v.set(A,[]),v.get(A).push(z.text)}for(let[F,z]of v){e.push(`**${F.charAt(0).toUpperCase()+F.slice(1)}**`);for(let A of z)e.push(`- ${A}`)}}else for(let v of c)typeof v=="string"&&e.push(`- ${v}`);let a=n.designModel.tokens;a&&typeof a=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(a,null,2)),e.push("```"));let m=n.designModel.components;m&&typeof m=="object"&&(e.push(""),e.push("Component patterns:"),e.push("```json"),e.push(JSON.stringify(m,null,2)),e.push("```")),e.push(""),e.push("### Novel Pattern Detection"),e.push("When you make a design decision that has no matching policy in the model above (e.g., styling a component type not yet in the model, choosing a color with no token, picking spacing with no rule), flag it:"),e.push("<novel>"),e.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),e.push("</novel>"),e.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),e.push("- `element`: What you are styling or creating"),e.push("- `decision`: What you decided to do (specific values)"),e.push("- `reason`: Why this is novel (what is missing from the model)"),e.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}if(n?.designModel||(e.push(""),e.push("## Design Context"),e.push("This project uses Popmelt for design governance. Design decisions are stored in .popmelt/decisions/ (JSON files). A materialized design model may exist at .popmelt/model.json. When the developer asks about design tokens, patterns, or past decisions, check these files first before searching source code.")),i){let c=n?.screenshotPaths??{},a=new Map;for(let m of t.annotations){let P=m.pathname||new URL(t.url).pathname;a.has(P)||a.set(P,[]),a.get(P).push(m)}e.push(""),e.push("The developer annotated multiple pages. Each page section includes its own screenshot where available.");for(let[m,P]of a){e.push(""),e.push(`## Page: ${m}`);let v=c[m];v&&n?.provider!=="codex"?e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${v}`):!v&&n?.provider!=="codex"&&(Object.keys(c).length===0?(e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${s}`),e.push("(This screenshot shows the page the developer was on when submitting \u2014 not this page)")):e.push("(No screenshot available for this page \u2014 work from the annotation text and selectors)"));let F={...t,annotations:P,styleModifications:t.styleModifications},z=Me(F,n?.imagePaths);z&&(e.push(""),e.push(z))}}else{let c=Me(t,n?.imagePaths);c&&(e.push(""),e.push(c))}return e.push(""),e.push("Follow the developer's instructions. If they ask for changes, apply them to the source files \u2014 the dev server has HMR so changes appear immediately. If they ask a question or request analysis, respond in text without modifying code."),e.push(""),e.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e.push(""),e.push("## Resolution"),e.push("After completing all work, output a resolution block listing what you did for each annotation:"),e.push("<resolution>"),e.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),e.push("</resolution>"),e.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),e.push(""),e.push("### Scope classification"),e.push("Each resolution MUST include scope fields:"),e.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),e.push("- `inferredScope`: What scope the change actually has, based on what you modified."),e.push("Scope has two dimensions:"),e.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),e.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),e.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),e.push("If you cannot confidently determine scope, set it to null."),e.push(""),e.push("## Questions"),e.push("If the annotation text is unclear, ambiguous, gibberish, or you are unsure what the developer wants, output a question:"),e.push('<question>What do you mean by "..."?</question>'),e.push("Do NOT guess what unclear instructions mean \u2014 ask instead."),e.push("You may output BOTH a <resolution> for clear annotations AND a <question> for unclear ones in the same response."),e.join(`
328
+ `)}function cn(s){return s.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function ln(s,t,n,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),n!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`);let i=t.find(c=>c.role==="human"&&c.feedbackContext);if(i?.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),t.length>0){o.push(""),o.push("## Conversation History");let c=0;for(let a of t)a.role==="human"?(c++,a.replyToQuestion?(o.push(`### Round ${c} (human) \u2014 reply`),o.push(`"${a.replyToQuestion}"`)):(o.push(`### Round ${c} (human)`),a.feedbackSummary&&o.push(`Annotations: ${a.feedbackSummary}`))):a.question?(o.push(`### Round ${c} (assistant) \u2014 question`),o.push(`"${a.question}"`)):(o.push(`### Round ${c} (assistant)`),a.responseText&&o.push(`Response: ${a.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let c of e)o.push(`Attached image: use the Read tool to view the image at: ${c}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
329
+ `)}function no(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 so(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&&!no(t[n]))return!1;return!0}function dn(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(so):[]}catch{return[]}}function un(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 oo}from"child_process";import{readdir as ro,readFile as io,stat as ao}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"},co=/^\s*-\s+"([^"]+)"/;function lo(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(co);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:ht(i),source:"cli"})}return Ke([_e,...t])}function uo(s){let t=JSON.parse(s);return(Array.isArray(t.models)?t.models:[]).filter(e=>e.visibility===void 0||e.visibility==="list").sort((e,o)=>{let i=typeof e.priority=="number"?e.priority:Number.MAX_SAFE_INTEGER,c=typeof o.priority=="number"?o.priority:Number.MAX_SAFE_INTEGER;return i-c}).flatMap(e=>{let o=typeof e.slug=="string"?e.slug:typeof e.id=="string"?e.id:"";if(!o)return[];let i=typeof e.display_name=="string"&&e.display_name.trim()?e.display_name.trim():ht(o);return[{id:o,label:i,source:"cli"}]})}function po(s){let t=s.match(/\bclaude-[a-z0-9]+(?:[-.][a-z0-9]+)*\b/gi)??[];return Ke(t.map(n=>({id:n,label:ht(n),source:"cli"})))}function ht(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":go(e[1]),c=e[2]&&e[3]?` ${e[2]}.${e[3]}`:"";return`${i}${c}${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 pn(s="copilot"){let t=await qe(s,["help","config"]),n=lo(t);return n.length>1?n:[_e]}async function fn(s="codex"){let t=await qe(s,["debug","models"]).catch(async()=>{let e=process.env.CODEX_HOME||me(Ce(),".codex");return mn(me(e,"models_cache.json"))}),n=uo(t);return n.length>0?Ke([...n,...$e]):$e}async function hn(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(()=>""),fo(t).catch(()=>[])]),c=mo([...n,...i]),a=await Promise.all(c.map(P=>mn(P).catch(()=>""))),m=po([e,o,...a].join(`
331
+ `));return Ke([...Ye,...m])}function qe(s,t,n=5e3){return new Promise((e,o)=>{oo(s,t,{encoding:"utf-8",timeout:n},(i,c)=>{if(i){o(i);return}e(c)})})}async function mn(s){return io(s,"utf-8")}async function fo(s){let t=[me(s,"projects"),me(s,"telemetry")],n=[];for(let e of t)await gn(e,n,4);return n.sort((e,o)=>o.mtimeMs-e.mtimeMs).slice(0,80).map(e=>e.path)}async function gn(s,t,n){if(n<0)return;let e;try{e=await ro(s,{withFileTypes:!0})}catch{return}await Promise.all(e.map(async o=>{let i=me(s,o.name);if(o.isDirectory()){await gn(i,t,n-1);return}if(!(!o.isFile()||!ho(o.name)))try{let c=await ao(i);if(c.size>2e6)return;t.push({path:i,mtimeMs:c.mtimeMs})}catch{}}))}function ho(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 mo(s){return[...new Set(s)]}function go(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 c=n<0?e.events:e.events.filter(a=>a.seq>n);return{jobId:t,events:c,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,c=()=>{i||(i=!0,o())};e.on("exit",c),e.on("error",c),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(c,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as vo,readFile as yo,writeFile as wo}from"fs/promises";import{dirname as So,join as bo}from"path";var xo={version:1,threads:{}},Ve=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=bo(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await yo(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={...xo,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(c=>e.has(c)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let c of n)i.has(c)||o.elementIdentifiers.push(c);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let c=o.messages.find(a=>a.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:c,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await vo(So(this.filePath),{recursive:!0}),await wo(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var vn="0.7.4";var $o=1111,_o=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],Oo=1800*1e3,Ao=3600*1e3;function kn(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 Do(s){let t=[];for(let n of s)if(n.type==="delta"){let e=n.text;if(!e)continue;let o=t[t.length-1];o&&o.kind==="text"?o.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",o=n.file??void 0,i=n.content??void 0,c=o?o.split("/").pop()??o:void 0,a;switch(e){case"Read":a=c?`Reading ${c}`:"Reading file";break;case"Edit":a=c?`Editing ${c}`:"Editing file";break;case"Write":a=c?`Writing ${c}`:"Writing file";break;case"Bash":a=i?i.split(`
332
+ `)[0].trim().slice(0,60):"Running command";break;case"Glob":a="Searching files";break;case"Grep":a="Searching code";break;case"WebFetch":a="Fetching page";break;case"WebSearch":a="Searching web";break;default:a=e?`Using ${e}`:"tool";break}let m=o??i??void 0,P=t[t.length-1];P&&P.kind==="tool_group"&&P.tool===e?P.items.push({label:a,detail:m}):t.push({kind:"tool_group",tool:e,items:[{label:a,detail:m}]})}return t}function No(s,t){let n=s.headers.origin;kn(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, X-Popmelt-Project-Id"))}function y(s,t,n){s.writeHead(t,{"Content-Type":"application/json"}),s.end(JSON.stringify(n))}function Jo(s,t){let n=s.headers["x-popmelt-project-id"];return(Array.isArray(n)?n[0]:n)||t.searchParams.get("projectId")||null}function Fo(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 gt(s,t){try{s.res.write(`event: ${t.type}
333
333
  data: ${JSON.stringify(t)}
334
334
 
335
- `)}catch{}}function Ae(s){return s==="claude"||s==="codex"||s==="copilot"?s:void 0}async function Fo(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 Bo(s,t){return new Promise((n,e)=>{let o=c=>{s.removeListener("listening",i),e(c)},i=()=>{s.removeListener("error",o),n()};s.once("error",o),s.once("listening",i),s.listen(t,"127.0.0.1")})}function Sn(s){try{let t=Ze("git",["rev-parse","HEAD"],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3}).trim(),n=["--",".",":(exclude).popmelt/**",":(exclude)**/.popmelt/**"],e=Ze("git",["status","--porcelain","--untracked-files=all",...n],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3,maxBuffer:1024*1024}),o=Ze("git",["diff","HEAD",...n],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3,maxBuffer:2*1024*1024});return xn("sha256").update(`${t}
335
+ `)}catch{}}function Ae(s){return s==="claude"||s==="codex"||s==="copilot"?s:void 0}async function Bo(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 jo(s,t){return new Promise((n,e)=>{let o=c=>{s.removeListener("listening",i),e(c)},i=()=>{s.removeListener("error",o),n()};s.once("error",o),s.once("listening",i),s.listen(t,"127.0.0.1")})}function Sn(s){try{let t=Ze("git",["rev-parse","HEAD"],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3}).trim(),n=["--",".",":(exclude).popmelt/**",":(exclude)**/.popmelt/**"],e=Ze("git",["status","--porcelain","--untracked-files=all",...n],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3,maxBuffer:1024*1024}),o=Ze("git",["diff","HEAD",...n],{cwd:s,encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:5e3,maxBuffer:2*1024*1024});return xn("sha256").update(`${t}
336
336
  ${e}
337
- ${o}`).digest("hex").slice(0,16)}catch{return}}async function Pn(s={}){let t=s.port??$o,n=s.projectRoot??process.cwd(),e=xn("sha256").update(n).digest("hex").slice(0,12),o=s.devOrigin??(process.env.PORT?`http://localhost:${process.env.PORT}`:null),i=s.tempDir??he(Io(),"popmelt-bridge"),c=s.maxTurns??40,a=s.maxBudgetUsd??1,m=[...s.allowedTools??_o],P=s.claudePath??"claude",g=s.copilotPath??"copilot",F=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:Ze("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=vn(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 fn(S.claude.path).catch(()=>Ye):Ye)})(),(async()=>{S.codex&&(S.codex.models=S.codex.available&&S.codex.path?await pn(S.codex.path).catch(()=>$e):$e)})(),(async()=>{S.copilot&&(S.copilot.models=S.copilot.available&&S.copilot.path?await un(S.copilot.path).catch(()=>[_e]):[_e])})()]);let[K,Z,Q]=await Promise.all([lt(n),dt(n),ut(n,S.copilot?.path??g)]),V=await Kt(n);S.claude&&(S.claude.mcp=K),S.codex&&(S.codex.mcp=Z),S.copilot&&(S.copilot.mcp=Q),K.found&&K.name&&m.push(`mcp__${K.name}__*`),await ko(i,{recursive:!0}),bn(i).catch(()=>{});let D=new Qe(1),_=new Set,R=new Ve(n),k=new je(n),y=new Ue(n),C=new Le(n,k,{claudePath:P,provider:F,timeoutMs:z,onEvent:r=>{for(let l of _)mt(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)&&mt(u,r)}),D.setProcessor(async r=>{let l=r._replyPrompt,p=r._replyImagePaths,u=r.provider??F,f;if(r.threadId){let T=await R.getThread(r.threadId);if(T){for(let I=T.messages.length-1;I>=0;I--)if(T.messages[I].sessionId){f=T.messages[I].sessionId;break}}}let d;if(f&&l){let T=(await R.getThread(r.threadId))?.messages.filter(se=>se.role==="human").pop();if(d=T?.replyToQuestion||T?.feedbackSummary||"",p&&p.length>0){d+=`
337
+ ${o}`).digest("hex").slice(0,16)}catch{return}}async function Pn(s={}){let t=s.port??$o,n=s.projectRoot??process.cwd(),e=xn("sha256").update(n).digest("hex").slice(0,12),o=s.devOrigin??(process.env.PORT?`http://localhost:${process.env.PORT}`:null),i=s.tempDir??he(To(),"popmelt-bridge"),c=s.maxTurns??40,a=s.maxBudgetUsd??1,m=[...s.allowedTools??_o],P=s.claudePath??"claude",v=s.copilotPath??"copilot",F=s.provider??"claude",z=s.timeoutMs,A=s.restartMode??"embedded",W=t,S={},X={claude:P,codex:"codex",copilot:v};for(let[r,l]of Object.entries(X))try{let p=l.includes("/")?l:Ze("which",[l],{encoding:"utf-8"}).trim();S[r]={available:!0,path:p}}catch{S[r]={available:!1,path:null}}function H(r,l){return new Promise(p=>{let d=yn(l,["--version"],{stdio:["ignore","ignore","ignore"]}),f=!1,u=O=>{f||(f=!0,p(O))},g=setTimeout(()=>{d.kill("SIGTERM"),u(!0)},5e3);d.on("error",()=>{clearTimeout(g),u(!1)}),d.on("close",O=>{clearTimeout(g),u(O===0)})})}await Promise.all([(async()=>{S.claude&&(S.claude.models=S.claude.available&&S.claude.path?await hn(S.claude.path).catch(()=>Ye):Ye)})(),(async()=>{S.codex&&(S.codex.models=S.codex.available&&S.codex.path?await fn(S.codex.path).catch(()=>$e):$e)})(),(async()=>{S.copilot&&(S.copilot.models=S.copilot.available&&S.copilot.path?await pn(S.copilot.path).catch(()=>[_e]):[_e])})()]);let[K,Z,Q]=await Promise.all([lt(n),dt(n),ut(n,S.copilot?.path??v)]),V=await Qt(n);S.claude&&(S.claude.mcp=K),S.codex&&(S.codex.mcp=Z),S.copilot&&(S.copilot.mcp=Q),K.found&&K.name&&m.push(`mcp__${K.name}__*`),await ko(i,{recursive:!0}),bn(i).catch(()=>{});let N=new Qe(1),_=new Set,R=new Ve(n),k=new je(n),w=new Ue(n),C=new Le(n,k,{claudePath:P,provider:F,timeoutMs:z,onEvent:r=>{for(let l of _)gt(l,r)}}),J=20,x=300*1e3,b=[],G=new Map,E=null,ie;N.addListener((r,l,p)=>{for(let d of _)(!p||!d.sourceId||d.sourceId===p)&&gt(d,r)}),N.setProcessor(async r=>{let l=r._replyPrompt,p=r._replyImagePaths,d=r.provider??F,f;if(r.threadId){let I=await R.getThread(r.threadId);if(I){for(let T=I.messages.length-1;T>=0;T--)if(I.messages[T].sessionId){f=I.messages[T].sessionId;break}}}let u;if(f&&l){let I=(await R.getThread(r.threadId))?.messages.filter(se=>se.role==="human").pop();if(u=I?.replyToQuestion||I?.feedbackSummary||"",p&&p.length>0){u+=`
338
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+=`
339
+ The developer attached reference images with their reply:`;for(let se of p)u+=`
340
+ Attached image: use the Read tool to view the image at: ${se}`}r.kind==="synthesize"||r.kind==="synthesize_evals"||r.kind==="run_eval"?u+=`
341
341
 
342
- Continue the project workflow from the previous instructions. If the developer approved the proposals, emit the final structured block requested earlier.`:d+=`
342
+ Continue the project workflow from the previous instructions. If the developer approved the proposals, emit the final structured block requested earlier.`:u+=`
343
343
 
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)+`
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)u=Me(r.feedback,r.imagePaths)+`
345
345
 
346
346
  Follow the developer's instructions. If they ask for changes, apply them to the source files.
347
347
 
348
- After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(u!=="codex"?`
348
+ After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(d!=="codex"?`
349
349
 
350
- IMPORTANT: First, use the Read tool to view the updated screenshot at: ${r.screenshotPath}`:"");else{let T=!l&&r.threadId?await R.getThreadHistory(r.threadId):void 0,I=l?null:await C.loadModel();d=l??rn(r.screenshotPath,r.feedback,{threadHistory:T&&T.length>0?T:void 0,provider:u,imagePaths:r.imagePaths,designModel:I??void 0,screenshotPaths:r.screenshotPaths})}let w=Jo(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=(T,I)=>{T.type==="delta"&&"text"in T?D.accumulateText(I,"response",T.text):T.type==="thinking"&&"text"in T&&D.accumulateText(I,"thinking",T.text),D.broadcast(T,I,r.sourceId)},{process:q,result:B}=u==="codex"?Be(r.id,{prompt:d,projectRoot:n,screenshotPath:r.screenshotPath,resumeSessionId:f,model:r.model,onEvent:j}):u==="copilot"?$t(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:c,maxBudgetUsd:a,allowedTools:m,claudePath:P,resumeSessionId:f,model:r.model,timeoutMs:z,onEvent:j});D.setActiveProcess(r.id,q);let h=await B;if(r.result=h.text,h.success){console.log(`${w} Iteration complete`),h.fileEdits&&h.fileEdits.length>0&&console.log(`${w} Captured ${h.fileEdits.length} file edit(s): ${h.fileEdits.map($=>`${$.tool} ${$.file_path}`).join(", ")}`),h.editEvents&&h.editEvents.length>0&&console.log(`${w} Captured ${h.editEvents.length} edit event(s): ${h.editEvents.map($=>`${$.operation} ${$.filePath}`).join(", ")}`),r.status="done";let T=an(h.text),I=ln(h.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=h.fileEdits&&h.fileEdits.length>0?h.fileEdits.map($=>`${$.tool} ${$.file_path.split("/").pop()}`):h.toolsUsed,ee=D.getBufferedEvents(r.id),ae=ee?Do(ee.events):void 0;if(r.threadId&&await R.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,responseText:h.text,resolutions:I.length>0?I:void 0,question:T??void 0,sessionId:h.sessionId,toolsUsed:se,editEvents:h.editEvents&&h.editEvents.length>0?h.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 Y=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:Y,durationMs:Y-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:h.sessionId,threadId:r.threadId,responseText:h.text,resolutions:I.length>0?I:[],question:T??void 0,fileEdits:h.fileEdits??[],editEvents:h.editEvents??[],toolsUsed:se,gitDiff:$},r.screenshotPath,A)}).catch(()=>{}),I.length>0){let $=I.some(Y=>(Y.finalScope??Y.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"&&h.text){let $=We(h.text);if($){Array.isArray($.rules)&&($.rules=Te($.rules));let Y=await C.loadModel();Y&&(!$.tokens&&Y.tokens&&($.tokens=Y.tokens),!$.components&&Y.components&&($.components=Y.components)),await C.writeModel($),console.log(`${w} Synthesize: model.json updated`)}}else if(r.kind==="synthesize_evals"&&h.text){let $=Nt(h.text),Y=Jt(h.text),A=$??Y;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"&&h.text&&r.evalId&&r.evalRunId){let $=await y.findCase(r.evalId),Y=r._evalRunScope;if($&&Y){let A=Ft(h.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:Y,provider:u,model:r.model,screenshotPath:r._evalScreenshotPath,codeFingerprint:Sn(n),rawResponse:h.text,...A}),console.log(`${w} Eval run: ${$.title} \u2192 ${A.status}`)}}T&&(console.log(`${w} \u{1F4AC} Question detected: "${T.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:T,annotationIds:r.annotationIds},r.id,r.sourceId));let ce=dn(h.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:h.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: ${h.error}`),r.status="error",r.error=h.error,r.threadId&&await R.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,error:h.error||"Unknown error",model:r.model,provider:u}),D.broadcast({type:"error",jobId:r.id,threadId:r.threadId,message:h.error||"Unknown error",provider:u,model:r.model,recoverable:h.recoverableError,code:h.errorCode},r.id,r.sourceId),b.push({id:r.id,status:"error",completedAt:Date.now(),error:h.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=To(async(r,l)=>{if(No(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")Cn(r,l);else if(r.method==="GET"&&d==="/status")$n(l);else if(r.method==="PATCH"&&d==="/config")await _n(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(()=>{vn(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 jn(r,l);else if(r.method==="POST"&&d==="/reply")await De(r,l);else if(r.method==="POST"&&d==="/cancel")On(r,l);else if(r.method==="POST"&&d==="/materialize")await An(r,l);else if(r.method==="POST"&&d==="/model/component")await Un(r,l);else if(r.method==="DELETE"&&d==="/model/component")await zn(r,l);else if(r.method==="PATCH"&&d==="/model/token")await Ln(r,l);else if(r.method==="DELETE"&&d==="/model/token")await Wn(r,l);else if(r.method==="POST"&&d==="/model/consolidate"){let{provider:w,model:J}=await et(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 Dn(r,l);else if(r.method==="POST"&&d==="/evals/synthesize")await Nn(r,l);else if(r.method==="GET"&&d==="/evals"){let w=await y.loadSuiteWithCurrentRuns(Sn(n));v(l,200,w)}else if(r.method==="POST"&&d.startsWith("/evals/")&&d.endsWith("/run")){let w=d.slice(7,-4);await Jn(w,r,l)}else if(r.method==="PATCH"&&d.startsWith("/evals/"))await Fn(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 qn(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 R.listRecent(w);v(l,200,J)}else if(r.method==="GET"&&d.startsWith("/thread/")){let w=d.slice(8);await Yn(w,l)}else r.method==="GET"&&(d==="/canvas"||d==="/canvas/")?Hn(r,l):r.method==="GET"&&d==="/canvas/manifest"?await Gn(l):r.method==="GET"&&d==="/canvas/app.mjs"?await Xn(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:q}=await Xe(r),B;try{B=JSON.parse(u)}catch{v(l,400,{error:"Invalid feedback JSON"});return}let h=we().slice(0,8),G={};if(q.length>0)for(let M of q){let oe=encodeURIComponent(M.pathname),de=he(i,`screenshot-${h}-${oe}.webp`);await Oe(de,M.data),G[M.pathname]=de}let T=he(i,`screenshot-${h}.webp`);await Oe(T,p);let I={};if(j.length>0)for(let M of j){let oe=he(i,`pasted-${h}-${M.annotationId}-${M.index}.webp`);await Oe(oe,M.data),I[M.annotationId]||(I[M.annotationId]=[]),I[M.annotationId].push(oe)}let se=B.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 R.findContinuationThread(se);M?(ee=M.id,await R.addElementIdentifiers(ee,se)):ee=(await R.createThread(h,se)).id}else ee=(await R.createThread(h,[])).id;let ae=B.annotations.map(M=>M.id),ce=Object.keys(G).length>0,$={id:h,status:"queued",screenshotPath:T,feedback:B,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}:{}},Y=new Set(B.annotations.map(M=>M.pathname).filter(Boolean)),A;if(Y.size>1){let M=new Map;for(let oe of B.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}\`
350
+ IMPORTANT: First, use the Read tool to view the updated screenshot at: ${r.screenshotPath}`:"");else{let I=!l&&r.threadId?await R.getThreadHistory(r.threadId):void 0,T=l?null:await C.loadModel();u=l??an(r.screenshotPath,r.feedback,{threadHistory:I&&I.length>0?I:void 0,provider:d,imagePaths:r.imagePaths,designModel:T??void 0,screenshotPaths:r.screenshotPaths})}let g=Fo(r.color,`[\u22B9 ${W}:${r.id}]`),O=r.screenshotPaths&&Object.keys(r.screenshotPaths).length>0?`${Object.keys(r.screenshotPaths).length} pages [${Object.keys(r.screenshotPaths).join(", ")}]`:r.screenshotPath;console.log(`${g} Reviewing ${O} (provider: ${d})${r.threadId?` (thread: ${r.threadId})`:""}${f?` (resuming: ${f.slice(0,8)})`:""}`);let B=(I,T)=>{I.type==="delta"&&"text"in I?N.accumulateText(T,"response",I.text):I.type==="thinking"&&"text"in I&&N.accumulateText(T,"thinking",I.text),N.broadcast(I,T,r.sourceId)},{process:q,result:j}=d==="codex"?Be(r.id,{prompt:u,projectRoot:n,screenshotPath:r.screenshotPath,resumeSessionId:f,model:r.model,onEvent:B}):d==="copilot"?_t(r.id,{prompt:u,projectRoot:n,resumeSessionId:f,model:r.model,timeoutMs:z,copilotPath:S.copilot?.path??v,onEvent:B}):Je(r.id,{prompt:u,projectRoot:n,maxTurns:c,maxBudgetUsd:a,allowedTools:m,claudePath:P,resumeSessionId:f,model:r.model,timeoutMs:z,onEvent:B});N.setActiveProcess(r.id,q);let h=await j;if(r.result=h.text,h.success){console.log(`${g} Iteration complete`),h.fileEdits&&h.fileEdits.length>0&&console.log(`${g} Captured ${h.fileEdits.length} file edit(s): ${h.fileEdits.map($=>`${$.tool} ${$.file_path}`).join(", ")}`),h.editEvents&&h.editEvents.length>0&&console.log(`${g} Captured ${h.editEvents.length} edit event(s): ${h.editEvents.map($=>`${$.operation} ${$.filePath}`).join(", ")}`),r.status="done";let I=cn(h.text),T=dn(h.text);if(T.length>0&&r.annotationIds&&r.annotationIds.length>0){let $=new Set(r.annotationIds);T.every(D=>$.has(D.annotationId))||(T=T.map((D,U)=>({...D,annotationId:r.annotationIds[U%r.annotationIds.length]})))}let se=h.fileEdits&&h.fileEdits.length>0?h.fileEdits.map($=>`${$.tool} ${$.file_path.split("/").pop()}`):h.toolsUsed,ee=N.getBufferedEvents(r.id),ae=ee?Do(ee.events):void 0;if(r.threadId&&await R.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,responseText:h.text,resolutions:T.length>0?T:void 0,question:I??void 0,sessionId:h.sessionId,toolsUsed:se,editEvents:h.editEvents&&h.editEvents.length>0?h.editEvents:void 0,segments:ae&&ae.length>0?ae:void 0,model:r.model,provider:d}),r.kind!=="run_eval"&&k.captureGitDiff(n).then(async $=>{let Y=Date.now(),D=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:Y,durationMs:Y-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:d,model:r.model,sessionId:h.sessionId,threadId:r.threadId,responseText:h.text,resolutions:T.length>0?T:[],question:I??void 0,fileEdits:h.fileEdits??[],editEvents:h.editEvents??[],toolsUsed:se,gitDiff:$},r.screenshotPath,D)}).catch(()=>{}),T.length>0){let $=T.some(Y=>(Y.finalScope??Y.inferredScope)?.breadth==="pattern");$&&d!=="copilot"?C.run({provider:d,model:r.model}).catch(()=>{}):$&&console.log(`${g} Skipping materialization for Copilot provider`)}if(r.kind==="synthesize"&&h.text){let $=We(h.text);if($){Array.isArray($.rules)&&($.rules=Ie($.rules));let Y=await C.loadModel();Y&&(!$.tokens&&Y.tokens&&($.tokens=Y.tokens),!$.components&&Y.components&&($.components=Y.components)),await C.writeModel($),console.log(`${g} Synthesize: model.json updated`)}}else if(r.kind==="synthesize_evals"&&h.text){let $=Jt(h.text),Y=Ft(h.text),D=$??Y;if(D&&D.length>0){let U=await w.writeSynthesized(D);console.log(`${g} Eval synthesis: wrote ${D.length} ${$?"approved":"pending"} eval(s) \u2192 .popmelt/evals/evals.json (${U.cases.length} total)`)}}else if(r.kind==="run_eval"&&h.text&&r.evalId&&r.evalRunId){let $=await w.findCase(r.evalId),Y=r._evalRunScope;if($&&Y){let D=Bt(h.text,$.assertions),U=Date.now();await w.writeRun({id:r.evalRunId,evalId:r.evalId,threadId:r.threadId,createdAt:r.createdAt,completedAt:U,durationMs:U-r.createdAt,scope:Y,provider:d,model:r.model,screenshotPath:r._evalScreenshotPath,codeFingerprint:Sn(n),rawResponse:h.text,...D}),console.log(`${g} Eval run: ${$.title} \u2192 ${D.status}`)}}I&&(console.log(`${g} \u{1F4AC} Question detected: "${I.slice(0,120)}" \u2192 broadcasting to ${_.size} SSE clients (threadId=${r.threadId??r.id}, annotationIds=${r.annotationIds?.join(",")??"none"})`),N.broadcast({type:"question",jobId:r.id,threadId:r.threadId??r.id,question:I,annotationIds:r.annotationIds},r.id,r.sourceId));let ce=un(h.text);ce.length>0&&(console.log(`${g} Novel pattern(s): ${ce.map($=>`${$.category}/${$.element}`).join(", ")}`),N.broadcast({type:"novel_patterns",jobId:r.id,patterns:ce,threadId:r.threadId},r.id,r.sourceId)),N.broadcast({type:"done",jobId:r.id,success:!0,resolutions:T.length>0?T:void 0,responseText:h.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(`${g} Error: ${h.error}`),r.status="error",r.error=h.error,r.threadId&&await R.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,error:h.error||"Unknown error",model:r.model,provider:d}),N.broadcast({type:"error",jobId:r.id,threadId:r.threadId,message:h.error||"Unknown error",provider:d,model:r.model,recoverable:h.recoverableError,code:h.errorCode},r.id,r.sourceId),b.push({id:r.id,status:"error",completedAt:Date.now(),error:h.error,threadId:r.threadId,annotationIds:r.annotationIds});let L=Date.now()-x;for(;b.length>0&&(b[0].completedAt<L||b.length>J);)b.shift()});let re=Io(async(r,l)=>{if(No(r,l),r.method==="OPTIONS"){l.writeHead(204),l.end();return}let p=r.url||"/",d=p.split("?")[0],f=new URL(p,`http://127.0.0.1:${W}`),u=f.pathname;if(r.headers.origin){if(!kn(r.headers.origin)){y(l,403,{error:"Popmelt bridge only accepts browser requests from localhost origins."});return}let g=Jo(r,f);if(g!==e){y(l,409,{error:"Popmelt bridge/project mismatch",bridgeProjectId:e,requestProjectId:g,projectName:mt(n)});return}}try{if(r.method==="POST"&&u==="/send")await ne(r,l);else if(r.method==="GET"&&u==="/events")Cn(r,l);else if(r.method==="GET"&&u==="/status")$n(l);else if(r.method==="PATCH"&&u==="/config")await _n(r,l);else if(r.method==="POST"&&u==="/shutdown"){if(A!=="detached"){y(l,409,{ok:!1,error:"This Popmelt bridge is embedded in the host dev server. Restart the dev server to reload bridge code.",restartMode:A});return}y(l,200,{ok:!0}),setTimeout(()=>process.exit(0),100)}else if(r.method==="POST"&&u==="/restart"){if(A!=="detached"){y(l,409,{ok:!1,error:"This Popmelt bridge is embedded in the host dev server. Restart the dev server to reload bridge code.",restartMode:A});return}y(l,200,{ok:!0,restarting:!0}),setTimeout(()=>{for(let g of _)try{g.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"&&u==="/capabilities")y(l,200,{providers:S});else if(r.method==="POST"&&u==="/mcp/install")await jn(r,l);else if(r.method==="POST"&&u==="/reply")await De(r,l);else if(r.method==="POST"&&u==="/cancel")On(r,l);else if(r.method==="POST"&&u==="/materialize")await An(r,l);else if(r.method==="POST"&&u==="/model/component")await Un(r,l);else if(r.method==="DELETE"&&u==="/model/component")await zn(r,l);else if(r.method==="PATCH"&&u==="/model/token")await Ln(r,l);else if(r.method==="DELETE"&&u==="/model/token")await Wn(r,l);else if(r.method==="POST"&&u==="/model/consolidate"){let{provider:g,model:O}=await et(r);if(g==="copilot"){y(l,400,{error:"Model consolidation is not available for Copilot yet."});return}C.consolidate({provider:g,model:O}).catch(B=>console.error("[Bridge] Consolidation error:",B)),y(l,200,{started:!0})}else if(r.method==="POST"&&u==="/model/synthesize")await Dn(r,l);else if(r.method==="POST"&&u==="/evals/synthesize")await Nn(r,l);else if(r.method==="GET"&&u==="/evals"){let g=await w.loadSuiteWithCurrentRuns(Sn(n));y(l,200,g)}else if(r.method==="POST"&&u.startsWith("/evals/")&&u.endsWith("/run")){let g=u.slice(7,-4);await Jn(g,r,l)}else if(r.method==="PATCH"&&u.startsWith("/evals/"))await Fn(u.slice(7),r,l);else if(r.method==="GET"&&u==="/model"){let g=await C.loadModel();y(l,200,{model:g})}else if(r.method==="GET"&&u.startsWith("/jobs/")&&u.endsWith("/events")){let g=u.slice(6,u.length-7),O=parseInt(f.searchParams.get("afterSeq")??"-1",10),B=N.getBufferedEvents(g,isNaN(O)?-1:O);B?y(l,200,B):y(l,404,{error:"Unknown job"})}else if(r.method==="GET"&&d.startsWith("/files/"))await qn(d.slice(7),l);else if(r.method==="GET"&&u==="/threads/recent"){let g=Math.min(Math.max(parseInt(f.searchParams.get("limit")??"5",10)||5,1),20),O=await R.listRecent(g);y(l,200,O)}else if(r.method==="GET"&&u.startsWith("/thread/")){let g=u.slice(8);await Yn(g,l)}else r.method==="GET"&&(u==="/canvas"||u==="/canvas/")?Hn(r,l):r.method==="GET"&&u==="/canvas/manifest"?await Gn(l):r.method==="GET"&&u==="/canvas/app.mjs"?await Xn(l):y(l,404,{error:"Not found"})}catch(g){console.error("[Bridge] Request error:",g),y(l,500,{error:g instanceof Error?g.message:"Internal error"})}});async function ne(r,l){let{screenshot:p,feedback:d,color:f,provider:u,model:g,sourceId:O,pastedImages:B,pageScreenshots:q}=await Xe(r),j;try{j=JSON.parse(d)}catch{y(l,400,{error:"Invalid feedback JSON"});return}let h=we().slice(0,8),L={};if(q.length>0)for(let M of q){let oe=encodeURIComponent(M.pathname),de=he(i,`screenshot-${h}-${oe}.webp`);await Oe(de,M.data),L[M.pathname]=de}let I=he(i,`screenshot-${h}.webp`);await Oe(I,p);let T={};if(B.length>0)for(let M of B){let oe=he(i,`pasted-${h}-${M.annotationId}-${M.index}.webp`);await Oe(oe,M.data),T[M.annotationId]||(T[M.annotationId]=[]),T[M.annotationId].push(oe)}let se=j.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 R.findContinuationThread(se);M?(ee=M.id,await R.addElementIdentifiers(ee,se)):ee=(await R.createThread(h,se)).id}else ee=(await R.createThread(h,[])).id;let ae=j.annotations.map(M=>M.id),ce=Object.keys(L).length>0,$={id:h,status:"queued",screenshotPath:I,feedback:j,createdAt:Date.now(),color:f,threadId:ee,annotationIds:ae,provider:Ae(u),model:g||void 0,...Object.keys(T).length>0?{imagePaths:T}:{},sourceId:O||void 0,...ce?{screenshotPaths:L}:{}},Y=new Set(j.annotations.map(M=>M.pathname).filter(Boolean)),D;if(Y.size>1){let M=new Map;for(let oe of j.annotations){let de=oe.pathname||"(unknown)";M.has(de)||M.set(de,[]),M.get(de).push(oe.instruction||`[${oe.type}]`)}D=[...M.entries()].map(([oe,de])=>`\`${oe}\`
351
351
  ${de.map(tt=>`- ${tt}`).join(`
352
352
  `)}`).join(`
353
- `)}else A=B.annotations.map(M=>M.instruction||`[${M.type}]`).join("; ");let U=Me(B,Object.keys(I).length>0?I:void 0);await R.appendMessage(ee,{role:"human",timestamp:Date.now(),jobId:h,screenshotPath:T,...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:h,position:le,threadId:ee})}async function De(r,l){let p=r.headers["content-type"]||"",u,f,d,w,J,j,q=[];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)q.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 R.getThread(u)){v(l,404,{error:"Thread not found"});return}let h=we().slice(0,8),G=[];for(let A=0;A<q.length;A++){let U=he(i,`reply-${h}-${A}.webp`);await Oe(U,q[A]),G.push(U)}let T="";{let A=await R.getThreadHistory(u);for(let U=A.length-1;U>=0;U--)if(A[U].screenshotPath){T=A[U].screenshotPath;break}}let I=await Bn(u);if(!T&&!I){v(l,400,{error:"No screenshot available"});return}await R.appendMessage(u,{role:"human",timestamp:Date.now(),jobId:h,replyToQuestion:f,screenshotPath:T,...G.length>0?{replyImagePaths:G}:{}});let se=await R.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=cn(T,se,ae,G.length>0?G:void 0),$={id:h,status:"queued",screenshotPath:T,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color: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 Y=D.enqueue($);v(l,200,{jobId:h,position:Y,threadId:u})}function Cn(r,l){l.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),l.write(`event: connected
353
+ `)}else D=j.annotations.map(M=>M.instruction||`[${M.type}]`).join("; ");let U=Me(j,Object.keys(T).length>0?T:void 0);await R.appendMessage(ee,{role:"human",timestamp:Date.now(),jobId:h,screenshotPath:I,...ce?{screenshotPaths:L}:{},...Object.keys(T).length>0?{imagePaths:T}:{},annotationIds:ae,feedbackSummary:D,feedbackContext:U||void 0});let le=N.enqueue($);y(l,200,{jobId:h,position:le,threadId:ee})}async function De(r,l){let p=r.headers["content-type"]||"",d,f,u,g,O,B,q=[];if(p.includes("multipart/form-data")){let D=await Xe(r),U=D.feedback?JSON.parse(D.feedback):{};d=U.threadId,f=U.reply,u=U.color,g=U.provider,O=U.model,B=U.sourceId||D.sourceId;for(let le of D.pastedImages)q.push(le.data)}else{let D=[];for await(let M of r)D.push(typeof M=="string"?Buffer.from(M):M);let U=Buffer.concat(D).toString("utf-8"),le;try{le=JSON.parse(U)}catch{y(l,400,{error:"Invalid JSON"});return}d=le.threadId,f=le.reply,u=le.color,g=le.provider,O=le.model,B=le.sourceId}if(!d||!f){y(l,400,{error:"Missing threadId or reply"});return}if(!await R.getThread(d)){y(l,404,{error:"Thread not found"});return}let h=we().slice(0,8),L=[];for(let D=0;D<q.length;D++){let U=he(i,`reply-${h}-${D}.webp`);await Oe(U,q[D]),L.push(U)}let I="";{let D=await R.getThreadHistory(d);for(let U=D.length-1;U>=0;U--)if(D[U].screenshotPath){I=D[U].screenshotPath;break}}let T=await Bn(d);if(!I&&!T){y(l,400,{error:"No screenshot available"});return}await R.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:h,replyToQuestion:f,screenshotPath:I,...L.length>0?{replyImagePaths:L}:{}});let se=await R.getThreadHistory(d),ee=[];for(let D of se)if(D.annotationIds)for(let U of D.annotationIds)ee.includes(U)||ee.push(U);let ae=Ae(g),ce=ln(I,se,ae,L.length>0?L:void 0),$={id:h,status:"queued",screenshotPath:I,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:u,threadId:d,annotationIds:ee.length>0?ee:void 0,provider:ae,model:O||void 0,sourceId:B||void 0};T&&($.kind=T),$._replyPrompt=ce,L.length>0&&($._replyImagePaths=L);let Y=N.enqueue($);y(l,200,{jobId:h,position:Y,threadId:d})}function Cn(r,l){l.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),l.write(`event: connected
354
354
  data: {"status":"connected"}
355
355
 
356
- `),r.headers.origin&&kn(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 $n(r){let l=D.allActive;v(r,200,{ok:!0,version:gn,restartMode:O,projectId:e,projectName:wn(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 _n(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 On(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 R.appendMessage(J,{role:"assistant",timestamp:Date.now(),jobId:u||"",cancelled:!0});v(l,200,{cancelled:w})}async function An(r,l){if(C.isRunning){v(l,200,{skipped:!0,reason:"Already running"});return}let{provider:p,model:u}=await et(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 et(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 Dn(r,l){let p=await C.loadModel();if(!p){v(l,400,{error:"No model exists yet"});return}let u,f;try{let h=[];for await(let T of r)h.push(typeof T=="string"?Buffer.from(T):T);let G=JSON.parse(Buffer.concat(h).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=Ht(p),w=we().slice(0,8),j=(await R.createThread(w,[])).id;H.set(j,"synthesize"),await R.appendMessage(j,{role:"human",timestamp:Date.now(),jobId:w,feedbackSummary:"Synthesize rules \u2014 review and propose improvements"});let q={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};q._replyPrompt=d;let B=D.enqueue(q);v(l,200,{jobId:w,position:B,threadId:j})}async function Nn(r,l){let{provider:p,model:u}=await et(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 R.load(),J=await C.loadModel(),j=await y.loadSuite(),q=jt({decisions:d,threads:w,model:J,existingSuite:j}),B=we().slice(0,8),G=(await R.createThread(B,[])).id;H.set(G,"synthesize_evals"),await R.appendMessage(G,{role:"human",timestamp:Date.now(),jobId:B,feedbackSummary:"Synthesize evals \u2014 infer evals from design history"});let T={id:B,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};T._replyPrompt=q;let I=D.enqueue(T);v(l,200,{jobId:B,position:I,threadId:G})}async function Jn(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:q}=await Xe(l),B=Ae(J)??F;if(B==="copilot"){v(p,400,{error:"Eval runs are not available for Copilot yet."});return}let h={};try{h=w?JSON.parse(w):{}}catch{v(p,400,{error:"Invalid eval run context JSON"});return}let G=we().slice(0,8),T=we().slice(0,8),I=he(i,`eval-run-${G}.webp`);await Oe(I,d);let se=await y.writeRunScreenshot(G,d),ee=h.viewport,ae=ee&&typeof ee=="object"?ee:{},ce={type:"current_page",url:typeof h.url=="string"?h.url:"",viewport:{width:typeof ae.width=="number"?ae.width:0,height:typeof ae.height=="number"?ae.height:0}},$=await C.loadModel(),Y=B==="claude"?K.name:B==="codex"?Z.name:null,A=B==="codex"?V.length>0:typeof Y=="string"&&/chrome|devtools/i.test(Y),U=Ut({evalCase:f,scope:ce,model:$,screenshotPath:I,chromeDevtoolsAvailable:A}),M=(await R.createThread(T,[])).id;H.set(M,"run_eval");let oe={timestamp:new Date().toISOString(),url:ce.url,viewport:ce.viewport,scrollPosition:{x:typeof h.scrollX=="number"?h.scrollX:0,y:typeof h.scrollY=="number"?h.scrollY:0},annotations:[],styleModifications:[]};await R.appendMessage(M,{role:"human",timestamp:Date.now(),jobId:T,screenshotPath:I,feedbackSummary:`Run eval \u2014 ${f.title}`,feedbackContext:[`Eval: ${f.title}`,"Scope: current page",`URL: ${ce.url||"unknown"}`,"Assertions:",...f.assertions.map(Qn=>`- ${Qn}`)].join(`
357
- `)});let de={id:T,status:"queued",screenshotPath:I,feedback:oe,createdAt:Date.now(),color:"#111827",threadId:M,kind:"run_eval",evalId:u,evalRunId:G,provider:B,model:j||void 0,sourceId:q||void 0};de._replyPrompt=U,de._evalRunScope=ce,de._evalScreenshotPath=se;let tt=D.enqueue(de);v(p,200,{jobId:T,runId:G,position:tt,threadId:M,screenshotPath:se})}async function Fn(r,l,p){let u=decodeURIComponent(r),f={},d,w,J;try{let q=[];for await(let h of l)q.push(typeof h=="string"?Buffer.from(h):h);let B=JSON.parse(Buffer.concat(q).toString());d=B.status,w=B.title,J=B.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 q=J.filter(B=>typeof B=="string").map(B=>B.trim()).filter(Boolean);if(q.length===0){v(p,400,{error:"Invalid assertions"});return}f.assertions=q}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 Bn(r){let l=H.get(r);if(l)return l;let f=(await R.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 jn(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 nn(u)),S.codex?.available&&S.codex.mcp&&!S.codex.mcp.found&&f.push(await sn(u)),S.copilot?.available&&S.copilot.mcp&&!S.copilot.mcp.found&&f.push(await on(u,S.copilot.path??g));let[d,w,J]=await Promise.all([lt(n),dt(n),ut(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 Un(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 zn(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 Ln(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 Wn(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 Hn(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=yt(L,p);l.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),l.end(u)}async function Gn(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 Xn(r){let l=[he(n,"node_modules","@popmelt.com","core","dist","canvas.mjs"),he(n,"packages","popmelt","dist","canvas.mjs")];try{let p=Co(import.meta.url);l.unshift(he(Mo(p),"canvas.mjs"))}catch{}for(let p of l)try{let u=await yn(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 qn(r,l){if(!r||r.includes("/")||r.includes("\\")||r.includes("..")){v(l,400,{error:"Invalid filename"});return}try{let p=await yn(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/${wn(r)}`}async function Yn(r,l){let p=await R.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,...q})=>({...q,...q.role==="assistant"&&u?{evalRun:u}:{},...d?{screenshotUrl:Ne(d)}:{},...w?{screenshotUrls:Object.fromEntries(Object.entries(w).map(([B,h])=>[B,Ne(h)]))}:{},...J?{imageUrls:Object.fromEntries(Object.entries(J).map(([B,h])=>[B,h.map(Ne)]))}:{},...j?{replyImageUrls:j.map(Ne)}:{}}));v(l,200,{id:p.id,createdAt:p.createdAt,messages:f})}let gt=9,vt=!1;for(let r=t;r<t+gt;r++)try{await Bo(re,r),L=r,vt=!0,console.log(`[\u22B9 is watching :${L}]`);break}catch(l){if(l.code==="EADDRINUSE"){let p=await Fo(r);if(p&&p.projectId===e)return console.log(`[\u22B9 already watching :${r}]`),{port:r,projectId:e,close:async()=>{}};continue}throw l}if(!vt)throw new Error(`[Bridge] All ports ${t}\u2013${t+gt-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 _)mt(u,{type:"capabilities_changed",data:{}})}});let Kn=setInterval(()=>{bn(i).catch(()=>{})},Oo);return{port:L,projectId:e,close:async()=>{clearInterval(Kn),await D.destroyAsync();for(let r of _)try{r.res.end()}catch{}return _.clear(),new Promise(r=>{re.close(()=>r())})}}}async function bn(s){try{let t=await Po(s),n=Date.now();for(let e of t){let o=he(s,e);try{let i=await Eo(o);n-i.mtimeMs>Ao&&await Ro(o)}catch{}}}catch{}}var Lo=parseInt(process.env.POPMELT_PORT||"1111",10),Rn=process.env.POPMELT_PROJECT_ROOT||process.cwd(),Wo=process.env.POPMELT_DEV_ORIGIN||void 0,Ho=process.env.POPMELT_COPILOT_PATH||void 0,Tn=En(Rn,".popmelt"),In=En(Tn,"bridge.lock");async function Go(s){await jo(Tn,{recursive:!0}),await zo(In,JSON.stringify({pid:process.pid,port:s,startedAt:Date.now()})+`
358
- `)}async function Mn(){try{await Uo(In)}catch{}}async function Xo(){let s=await Pn({port:Lo,projectRoot:Rn,devOrigin:Wo,copilotPath:Ho,restartMode:"detached"});await Go(s.port),await new Promise(t=>{let n=async()=>{await s.close(),await Mn(),t()};process.on("SIGTERM",n),process.on("SIGINT",n)})}Xo().catch(s=>{console.error("[popmelt bridge-entry] Fatal:",s),Mn().finally(()=>process.exit(1))});
356
+ `);let d=new URL(r.url||"/",`http://127.0.0.1:${W}`).searchParams.get("sourceId")||void 0,f={id:we().slice(0,8),res:l,sourceId:d};_.add(f),r.on("close",()=>{_.delete(f)})}function $n(r){let l=N.allActive;y(r,200,{ok:!0,version:vn,restartMode:A,projectId:e,projectName:mt(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:N.depth,recentJobs:b})}async function _n(r,l){let p=await new Promise(d=>{let f="";r.on("data",u=>{f+=u.toString()}),r.on("end",()=>d(f))});try{let d=JSON.parse(p);typeof d.devOrigin=="string"&&(o=d.devOrigin||null),y(l,200,{ok:!0,devOrigin:o})}catch{y(l,400,{error:"Invalid JSON"})}}async function On(r,l){let d=new URL(r.url||"/",`http://127.0.0.1:${W}`).searchParams.get("jobId"),u=(d?N.allActive.filter(O=>O.id===d):N.allActive).map(O=>O.threadId).filter(Boolean),g=d?N.cancelJob(d):N.cancelActive();for(let O of u)await R.appendMessage(O,{role:"assistant",timestamp:Date.now(),jobId:d||"",cancelled:!0});y(l,200,{cancelled:g})}async function An(r,l){if(C.isRunning){y(l,200,{skipped:!0,reason:"Already running"});return}let{provider:p,model:d}=await et(r);if(p==="copilot"){y(l,400,{error:"Materialization is not available for Copilot yet."});return}let f=await C.getUnmaterializedPatternDecisions();if(f.length===0){y(l,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}C.run({provider:p,model:d}).catch(()=>{}),y(l,200,{started:!0,decisionCount:f.length,decisionIds:f.map(u=>u.id)})}async function et(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 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 Dn(r,l){let p=await C.loadModel();if(!p){y(l,400,{error:"No model exists yet"});return}let d,f;try{let h=[];for await(let I of r)h.push(typeof I=="string"?Buffer.from(I):I);let L=JSON.parse(Buffer.concat(h).toString());d=L.provider,f=L.model}catch{}if(d==="copilot"){y(l,400,{error:"Rule synthesis is not available for Copilot yet."});return}let u=Gt(p),g=we().slice(0,8),B=(await R.createThread(g,[])).id;G.set(B,"synthesize"),await R.appendMessage(B,{role:"human",timestamp:Date.now(),jobId:g,feedbackSummary:"Synthesize rules \u2014 review and propose improvements"});let q={id:g,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:B,kind:"synthesize",provider:Ae(d),model:f||void 0};q._replyPrompt=u;let j=N.enqueue(q);y(l,200,{jobId:g,position:j,threadId:B})}async function Nn(r,l){let{provider:p,model:d}=await et(r);if(p==="copilot"){y(l,400,{error:"Eval synthesis is not available for Copilot yet."});return}let f=await k.listDecisionIds(),u=await k.loadDecisions(f),g=await R.load(),O=await C.loadModel(),B=await w.loadSuite(),q=Ut({decisions:u,threads:g,model:O,existingSuite:B}),j=we().slice(0,8),L=(await R.createThread(j,[])).id;G.set(L,"synthesize_evals"),await R.appendMessage(L,{role:"human",timestamp:Date.now(),jobId:j,feedbackSummary:"Synthesize evals \u2014 infer evals from design history"});let I={id:j,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:L,kind:"synthesize_evals",provider:p,model:d||void 0};I._replyPrompt=q;let T=N.enqueue(I);y(l,200,{jobId:j,position:T,threadId:L})}async function Jn(r,l,p){let d=decodeURIComponent(r),f=await w.findCase(d);if(!f){y(p,404,{error:"Eval not found"});return}if(f.status!=="approved"){y(p,400,{error:"Only approved evals can be run"});return}let{screenshot:u,feedback:g,provider:O,model:B,sourceId:q}=await Xe(l),j=Ae(O)??F;if(j==="copilot"){y(p,400,{error:"Eval runs are not available for Copilot yet."});return}let h={};try{h=g?JSON.parse(g):{}}catch{y(p,400,{error:"Invalid eval run context JSON"});return}let L=we().slice(0,8),I=we().slice(0,8),T=he(i,`eval-run-${L}.webp`);await Oe(T,u);let se=await w.writeRunScreenshot(L,u),ee=h.viewport,ae=ee&&typeof ee=="object"?ee:{},ce={type:"current_page",url:typeof h.url=="string"?h.url:"",viewport:{width:typeof ae.width=="number"?ae.width:0,height:typeof ae.height=="number"?ae.height:0}},$=await C.loadModel(),Y=j==="claude"?K.name:j==="codex"?Z.name:null,D=j==="codex"?V.length>0:typeof Y=="string"&&/chrome|devtools/i.test(Y),U=zt({evalCase:f,scope:ce,model:$,screenshotPath:T,chromeDevtoolsAvailable:D}),M=(await R.createThread(I,[])).id;G.set(M,"run_eval");let oe={timestamp:new Date().toISOString(),url:ce.url,viewport:ce.viewport,scrollPosition:{x:typeof h.scrollX=="number"?h.scrollX:0,y:typeof h.scrollY=="number"?h.scrollY:0},annotations:[],styleModifications:[]};await R.appendMessage(M,{role:"human",timestamp:Date.now(),jobId:I,screenshotPath:T,feedbackSummary:`Run eval \u2014 ${f.title}`,feedbackContext:[`Eval: ${f.title}`,"Scope: current page",`URL: ${ce.url||"unknown"}`,"Assertions:",...f.assertions.map(Qn=>`- ${Qn}`)].join(`
357
+ `)});let de={id:I,status:"queued",screenshotPath:T,feedback:oe,createdAt:Date.now(),color:"#111827",threadId:M,kind:"run_eval",evalId:d,evalRunId:L,provider:j,model:B||void 0,sourceId:q||void 0};de._replyPrompt=U,de._evalRunScope=ce,de._evalScreenshotPath=se;let tt=N.enqueue(de);y(p,200,{jobId:I,runId:L,position:tt,threadId:M,screenshotPath:se})}async function Fn(r,l,p){let d=decodeURIComponent(r),f={},u,g,O;try{let q=[];for await(let h of l)q.push(typeof h=="string"?Buffer.from(h):h);let j=JSON.parse(Buffer.concat(q).toString());u=j.status,g=j.title,O=j.assertions}catch{y(p,400,{error:"Invalid JSON"});return}if(u!==void 0&&u!=="pending"&&u!=="approved"&&u!=="rejected"){y(p,400,{error:"Invalid status"});return}if(u&&(f.status=u),g!==void 0){if(typeof g!="string"||g.trim().length===0){y(p,400,{error:"Invalid title"});return}f.title=g.trim()}if(O!==void 0){if(!Array.isArray(O)){y(p,400,{error:"Invalid assertions"});return}let q=O.filter(j=>typeof j=="string").map(j=>j.trim()).filter(Boolean);if(q.length===0){y(p,400,{error:"Invalid assertions"});return}f.assertions=q}if(!f.status&&!f.title&&!f.assertions){y(p,400,{error:"No eval changes provided"});return}let B=await w.updateCase(d,f);if(!B){y(p,404,{error:"Eval not found"});return}y(p,200,{eval:B})}async function Bn(r){let l=G.get(r);if(l)return l;let f=(await R.getThread(r))?.messages.find(u=>u.role==="human")?.feedbackSummary??"";if(f.startsWith("Synthesize evals"))return G.set(r,"synthesize_evals"),"synthesize_evals";if(f.startsWith("Synthesize rules"))return G.set(r,"synthesize"),"synthesize";if(f.startsWith("Run eval"))return G.set(r,"run_eval"),"run_eval"}async function jn(r,l){let p=[];for await(let B of r)p.push(typeof B=="string"?Buffer.from(B):B);let d;if(p.length>0)try{d=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 sn(d)),S.codex?.available&&S.codex.mcp&&!S.codex.mcp.found&&f.push(await on(d)),S.copilot?.available&&S.copilot.mcp&&!S.copilot.mcp.found&&f.push(await rn(d,S.copilot.path??v));let[u,g,O]=await Promise.all([lt(n),dt(n),ut(n,S.copilot?.path??v)]);S.claude&&(S.claude.mcp=u),S.codex&&(S.codex.mcp=g),S.copilot&&(S.copilot.mcp=O),y(l,200,{results:f,capabilities:{providers:S}})}async function Un(r,l){let p=[];for await(let u of r)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{y(l,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){y(l,400,{error:"Missing or invalid name"});return}let f=await C.addComponent(d.name);y(l,200,f)}async function zn(r,l){let p=[];for await(let u of r)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{y(l,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){y(l,400,{error:"Missing or invalid name"});return}let f=await C.removeComponent(d.name);y(l,200,f)}async function Ln(r,l){let p=[];for await(let u of r)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{y(l,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){y(l,400,{error:"Missing or invalid path/value"});return}let f=await C.updateToken(d.path,d.value);y(l,200,f)}async function Wn(r,l){let p=[];for await(let u of r)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{y(l,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){y(l,400,{error:"Missing or invalid path"});return}let f=await C.removeToken(d.path);y(l,200,f)}function Hn(r,l){let p=o??"http://localhost:3000";if(!o){let f=r.headers.referer||r.headers.origin;if(f)try{let u=new URL(typeof f=="string"?f:f[0]||"");(u.hostname==="localhost"||u.hostname==="127.0.0.1")&&(p=u.origin)}catch{}}let d=wt(W,p);l.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),l.end(d)}async function Gn(r){let l=Date.now();if(E&&l<E.expires){y(r,200,E.data);return}try{let{scanForComponents:p}=await import("./react-scanner-P3VD53VT.mjs"),{generateRenderFiles:d}=await import("./render-generator-HWFLZYC3.mjs"),f=await p(n);E={data:f,expires:l+5e3},d(f,n,ie).then(u=>{ie=u}).catch(u=>console.warn("[Bridge] Render generation failed:",u)),y(r,200,f)}catch(p){console.error("[Bridge] Scanner error:",p),y(r,500,{error:"Failed to scan components"})}}async function Xn(r){let l=[he(n,"node_modules","@popmelt.com","core","dist","canvas.mjs"),he(n,"packages","popmelt","dist","canvas.mjs")];try{let p=Co(import.meta.url);l.unshift(he(Mo(p),"canvas.mjs"))}catch{}for(let p of l)try{let d=await wn(p,"utf-8");r.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),r.end(d);return}catch{}console.error("[Bridge] Canvas bundle not found in:",l),y(r,404,{error:"Canvas bundle not found"})}async function qn(r,l){if(!r||r.includes("/")||r.includes("\\")||r.includes("..")){y(l,400,{error:"Invalid filename"});return}try{let p=await wn(he(i,r)),d=r.split(".").pop()?.toLowerCase(),f=d==="png"?"image/png":d==="webp"?"image/webp":d==="jpg"||d==="jpeg"?"image/jpeg":"application/octet-stream";l.writeHead(200,{"Content-Type":f,"Cache-Control":"public, max-age=3600"}),l.end(p)}catch{y(l,404,{error:"File not found"})}}function Ne(r){return`/files/${mt(r)}`}async function Yn(r,l){let p=await R.getThread(r);if(!p){y(l,404,{error:"Thread not found"});return}let d=(await w.loadRuns()).runs.filter(g=>g.threadId===r).sort((g,O)=>O.completedAt-g.completedAt)[0],f=d?await w.findCase(d.evalId):null,u=p.messages.map(({screenshotPath:g,screenshotPaths:O,imagePaths:B,replyImagePaths:q,...j})=>({...j,...j.role==="assistant"&&d?{evalRun:{...d,title:f?.title}}:{},...g?{screenshotUrl:Ne(g)}:{},...O?{screenshotUrls:Object.fromEntries(Object.entries(O).map(([h,L])=>[h,Ne(L)]))}:{},...B?{imageUrls:Object.fromEntries(Object.entries(B).map(([h,L])=>[h,L.map(Ne)]))}:{},...q?{replyImageUrls:q.map(Ne)}:{}}));y(l,200,{id:p.id,createdAt:p.createdAt,messages:u})}let vt=9,yt=!1;for(let r=t;r<t+vt;r++)try{await jo(re,r),W=r,yt=!0,console.log(`[\u22B9 is watching :${W}]`);break}catch(l){if(l.code==="EADDRINUSE"){let p=await Bo(r);if(p&&p.projectId===e)return console.log(`[\u22B9 already watching :${r}]`),{port:r,projectId:e,close:async()=>{}};continue}throw l}if(!yt)throw new Error(`[Bridge] All ports ${t}\u2013${t+vt-1} in use`);for(let[r,l]of Object.entries(S))!l.available||!l.path||H(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 d of _)gt(d,{type:"capabilities_changed",data:{}})}});let Kn=setInterval(()=>{bn(i).catch(()=>{})},Oo);return{port:W,projectId:e,close:async()=>{clearInterval(Kn),await N.destroyAsync();for(let r of _)try{r.res.end()}catch{}return _.clear(),new Promise(r=>{re.close(()=>r())})}}}async function bn(s){try{let t=await Po(s),n=Date.now();for(let e of t){let o=he(s,e);try{let i=await Eo(o);n-i.mtimeMs>Ao&&await Ro(o)}catch{}}}catch{}}var Wo=parseInt(process.env.POPMELT_PORT||"1111",10),Rn=process.env.POPMELT_PROJECT_ROOT||process.cwd(),Ho=process.env.POPMELT_DEV_ORIGIN||void 0,Go=process.env.POPMELT_COPILOT_PATH||void 0,In=En(Rn,".popmelt"),Tn=En(In,"bridge.lock");async function Xo(s){await Uo(In,{recursive:!0}),await Lo(Tn,JSON.stringify({pid:process.pid,port:s,startedAt:Date.now()})+`
358
+ `)}async function Mn(){try{await zo(Tn)}catch{}}async function qo(){let s=await Pn({port:Wo,projectRoot:Rn,devOrigin:Ho,copilotPath:Go,restartMode:"detached"});await Xo(s.port),await new Promise(t=>{let n=async()=>{await s.close(),await Mn(),t()};process.on("SIGTERM",n),process.on("SIGINT",n)})}qo().catch(s=>{console.error("[popmelt bridge-entry] Fatal:",s),Mn().finally(()=>process.exit(1))});