@popmelt.com/core 0.6.8 → 0.6.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bridge-entry.mjs
CHANGED
|
@@ -26,7 +26,7 @@ import{mkdir as ws,unlink as vs,writeFile as Ss}from"fs/promises";import{join as
|
|
|
26
26
|
</script>
|
|
27
27
|
</body>
|
|
28
28
|
</html>`}import{spawn as on}from"child_process";import{createInterface as rn}from"readline";var an=new Set([".md",".txt",".json",".ts",".tsx",".js",".jsx",".css",".scss",".html",".xml",".yaml",".yml",".toml",".ini",".cfg",".conf",".sh",".bash",".zsh",".py",".rb",".go",".rs",".java",".c",".h",".cpp",".hpp",".swift",".kt",".sql",".graphql",".svg",".env",".gitignore",".prettierrc",".eslintrc"]),Ve=1e5;function cn(s){let t=s.input?.file_path||s.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!an.has(n))return;let e;if(s.name==="Write"&&typeof s.input?.content=="string"?e=s.input.content:s.name==="Edit"&&typeof s.input?.new_string=="string"&&(e=s.input.new_string),!!e)return e.length>Ve?e.slice(0,Ve)+`
|
|
29
|
-
\u2026[truncated]`:e}function fe(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:c=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:l="claude",resumeSessionId:h,model:y,timeoutMs:w=3e5,onEvent:A}=t,D=[];h?D.push("--resume",h,"-p",n):D.push("-p",n),D.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),y&&D.push("--model",y);for(let L of c)D.push("--allowedTools",L);let E=on(l,D,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),f=new Promise(L=>{let z,j=[],H=[],Y=!1,k="",x=!1,T=setTimeout(()=>{x=!0,E.kill("SIGTERM"),setTimeout(()=>{try{E.kill("SIGKILL")}catch{}},5e3)},w),S=rn({input:E.stdout}),g=new Set;S.on("line",U=>{if(U.trim())try{let $=JSON.parse(U);$.session_id&&!z&&(z=$.session_id);let G=$.type??($.event?.type?`event.${$.event.type}`:"unknown");if(g.add(G),$.type==="result"&&$.result&&j.length===0){let R=typeof $.result=="string"?$.result:"";R&&(j.push(R),A?.({type:"delta",jobId:s,text:R},s))}if($.type==="assistant"&&Array.isArray($.message?.content))for(let R of $.message.content){if(R.type==="text"&&R.text&&(j.push(R.text),A?.({type:"delta",jobId:s,text:R.text},s)),R.type==="tool_use"&&R.name){let V=R.input?.file_path||R.input?.path||void 0,ie=cn(R);A?.({type:"tool_use",jobId:s,tool:R.name,...V?{file:V}:{},...ie?{content:ie}:{}},s),R.name==="Edit"&&R.input?.file_path?H.push({tool:"Edit",file_path:R.input.file_path,old_string:R.input.old_string,new_string:R.input.new_string,replace_all:R.input.replace_all}):R.name==="Write"&&R.input?.file_path&&H.push({tool:"Write",file_path:R.input.file_path,content:R.input.content})}R.type==="thinking"&&R.thinking&&A?.({type:"thinking",jobId:s,text:R.thinking},s)}$.type==="user"&&$.tool_use_result?.file?.filePath&&A?.({type:"tool_use",jobId:s,tool:"Read",file:$.tool_use_result.file.filePath},s)}catch{}});let v=[];E.stderr?.on("data",U=>{v.push(U.toString())}),E.on("close",U=>{if(clearTimeout(T),S.close(),j.length===0&&g.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...g].join(", ")}`),x)Y=!0,k=`Timed out after ${Math.round(w/6e4)} minutes`;else if(U!==0&&U!==null){Y=!0;let $=v.join("").trim(),G=j.length===0&&g.size>0?` (no text captured, event types: ${[...g].join(", ")})`:"";k=$||`Claude process exited with code ${U}${G}`}L({sessionId:z,text:j.join(""),success:!Y,error:Y?k:void 0,fileEdits:H.length>0?H:void 0})}),E.on("error",U=>{clearTimeout(T),Y=!0,k=U.message,L({sessionId:z,text:j.join(""),success:!1,error:k,fileEdits:H.length>0?H:void 0})})});return{process:E,result:f}}import{spawn as ln}from"child_process";import{createInterface as dn}from"readline";function un(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}function Ze(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:c,onEvent:l}=t,h=[];i?(h.push("exec","resume",
|
|
29
|
+
\u2026[truncated]`:e}function fe(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:c=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:l="claude",resumeSessionId:h,model:y,timeoutMs:w=3e5,onEvent:A}=t,D=[];h?D.push("--resume",h,"-p",n):D.push("-p",n),D.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),y&&D.push("--model",y);for(let L of c)D.push("--allowedTools",L);let E=on(l,D,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),f=new Promise(L=>{let z,j=[],H=[],Y=!1,k="",x=!1,T=setTimeout(()=>{x=!0,E.kill("SIGTERM"),setTimeout(()=>{try{E.kill("SIGKILL")}catch{}},5e3)},w),S=rn({input:E.stdout}),g=new Set;S.on("line",U=>{if(U.trim())try{let $=JSON.parse(U);$.session_id&&!z&&(z=$.session_id);let G=$.type??($.event?.type?`event.${$.event.type}`:"unknown");if(g.add(G),$.type==="result"&&$.result&&j.length===0){let R=typeof $.result=="string"?$.result:"";R&&(j.push(R),A?.({type:"delta",jobId:s,text:R},s))}if($.type==="assistant"&&Array.isArray($.message?.content))for(let R of $.message.content){if(R.type==="text"&&R.text&&(j.push(R.text),A?.({type:"delta",jobId:s,text:R.text},s)),R.type==="tool_use"&&R.name){let V=R.input?.file_path||R.input?.path||void 0,ie=cn(R);A?.({type:"tool_use",jobId:s,tool:R.name,...V?{file:V}:{},...ie?{content:ie}:{}},s),R.name==="Edit"&&R.input?.file_path?H.push({tool:"Edit",file_path:R.input.file_path,old_string:R.input.old_string,new_string:R.input.new_string,replace_all:R.input.replace_all}):R.name==="Write"&&R.input?.file_path&&H.push({tool:"Write",file_path:R.input.file_path,content:R.input.content})}R.type==="thinking"&&R.thinking&&A?.({type:"thinking",jobId:s,text:R.thinking},s)}$.type==="user"&&$.tool_use_result?.file?.filePath&&A?.({type:"tool_use",jobId:s,tool:"Read",file:$.tool_use_result.file.filePath},s)}catch{}});let v=[];E.stderr?.on("data",U=>{v.push(U.toString())}),E.on("close",U=>{if(clearTimeout(T),S.close(),j.length===0&&g.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...g].join(", ")}`),x)Y=!0,k=`Timed out after ${Math.round(w/6e4)} minutes`;else if(U!==0&&U!==null){Y=!0;let $=v.join("").trim(),G=j.length===0&&g.size>0?` (no text captured, event types: ${[...g].join(", ")})`:"";k=$||`Claude process exited with code ${U}${G}`}L({sessionId:z,text:j.join(""),success:!Y,error:Y?k:void 0,fileEdits:H.length>0?H:void 0})}),E.on("error",U=>{clearTimeout(T),Y=!0,k=U.message,L({sessionId:z,text:j.join(""),success:!1,error:k,fileEdits:H.length>0?H:void 0})})});return{process:E,result:f}}import{spawn as ln}from"child_process";import{createInterface as dn}from"readline";function un(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}function Ze(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:c,onEvent:l}=t,h=[];i?(h.push("exec","resume","--skip-git-repo-check","--json","-c",'sandbox_mode="workspace-write"'),c&&h.push("-m",c),h.push(i),h.push(n),o&&h.push("--image",o)):(h.push("exec","--skip-git-repo-check","--json","--sandbox","workspace-write"),c&&h.push("-m",c),h.push(n),o&&h.push("--image",o));let y=ln("codex",h,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),w=new Promise(A=>{let D,E=[],f=[],L=!1,z="",j=dn({input:y.stdout}),H=new Set;j.on("line",k=>{if(k.trim())try{let x=JSON.parse(k),T=x.type??"unknown";if(H.add(T),T==="thread.started"&&x.thread_id&&!D&&(D=x.thread_id),(T==="item.agentMessage.delta"||T==="item/agentMessage/delta")&&x.delta?.text&&(E.push(x.delta.text),l?.({type:"delta",jobId:s,text:x.delta.text},s)),(T==="item.reasoning.delta"||T==="item/reasoning/delta")&&x.delta?.text&&l?.({type:"thinking",jobId:s,text:x.delta.text},s),(T==="item.started"||T==="item/started")&&x.item){let S=x.item.type;if(S==="command_execution"){let g=x.item.command,v=g?un(g):void 0,U=v?`Bash: ${v.split(`
|
|
30
30
|
`)[0].slice(0,80)}`:"Bash";f.push(U),l?.({type:"tool_use",jobId:s,tool:"Bash",...v?{content:v}:{}},s)}else if(S==="file_change"){let g=x.item.filename||x.item.path;f.push(g?`Edit ${g.split("/").pop()}`:"Edit"),l?.({type:"tool_use",jobId:s,tool:"Edit",...g?{file:g}:{}},s)}else if(S==="file_read"){let g=x.item.filename||x.item.path;f.push(g?`Read ${g.split("/").pop()}`:"Read"),l?.({type:"tool_use",jobId:s,tool:"Read",...g?{file:g}:{}},s)}else if(S==="web_search")f.push("WebSearch"),l?.({type:"tool_use",jobId:s,tool:"WebSearch"},s);else if(S==="mcp_tool_call"){let g=x.item.tool_name||x.item.name||"MCP";f.push(g),l?.({type:"tool_use",jobId:s,tool:g},s)}}if((T==="item.completed"||T==="item/completed")&&x.item){if(x.item.type==="agent_message"){let S=x.item.text;typeof S=="string"&&S&&(E.push(S),l?.({type:"delta",jobId:s,text:S},s))}else if(x.item.type==="reasoning"){let S=x.item.text;typeof S=="string"&&S&&l?.({type:"thinking",jobId:s,text:S},s)}else if(x.item.type==="file_change"&&Array.isArray(x.item.changes))for(let S of x.item.changes){let g=S.path||S.filename,v=S.kind==="add"?"Write":"Edit";g&&(f.push(`${v} ${g.split("/").pop()}`),l?.({type:"tool_use",jobId:s,tool:v,file:g},s))}}T==="turn.failed"&&(L=!0,z=x.error?.message||x.message||"Turn failed")}catch{}});let Y=[];y.stderr?.on("data",k=>{Y.push(k.toString())}),y.on("close",k=>{j.close(),E.length===0&&H.size>0&&console.warn(`[Codex:${s}] No text captured. Event types seen: ${[...H].join(", ")}`),k!==0&&k!==null&&(L=!0,z=Y.join("")||`Codex process exited with code ${k}`),A({sessionId:D,text:E.join(""),success:!L,error:L?z:void 0,toolsUsed:f.length>0?f:void 0})}),y.on("error",k=>{L=!0,z=k.message,A({sessionId:D,text:E.join(""),success:!1,error:z,toolsUsed:f.length>0?f:void 0})})});return{process:y,result:w}}import{spawn as pn}from"child_process";import{createInterface as hn}from"readline";function et(s,t){let n=s.trim()||"Copilot CLI failed",e=n.toLowerCase();return e.includes("no authentication information")||e.includes("not authenticated")||e.includes("/login")?{code:"copilot_auth_required",recoverable:!0,message:"Copilot is not authenticated. Run `copilot login`, set `COPILOT_GITHUB_TOKEN`, or refresh `gh auth login`."}:e.includes("model")&&(e.includes("not available")||e.includes("not found")||e.includes("disabled")||e.includes("policy"))?{code:"copilot_model_unavailable",recoverable:!0,message:t?`Copilot rejected ${t}. Choose another model or ask your administrator to enable it.`:n}:e.includes("mcp")&&(e.includes("blocked")||e.includes("allowlist"))?{code:"copilot_mcp_blocked",recoverable:!0,message:"Copilot blocked the Popmelt MCP server. Ask your administrator to add it to the enterprise allowlist."}:e.includes("permission")||e.includes("denied")||e.includes("not allowed")?{code:"copilot_permission_denied",recoverable:!0,message:n}:{code:"copilot_error",recoverable:!1,message:n}}function tt(s,t){let{prompt:n,projectRoot:e,resumeSessionId:o,model:i,timeoutMs:c=3e5,onEvent:l,copilotPath:h="copilot"}=t,y=["--no-color","--no-auto-update","--output-format","json","--stream","on","--available-tools=read,write,shell","--allow-all-tools","--deny-tool=shell(git push)","--deny-tool=shell(rm)","--deny-tool=shell(sudo)","--no-ask-user"];o&&y.push(`--resume=${o}`),i&&y.push("--model",i),y.push("-p",n);let w=pn(h,y,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),A=new Promise(D=>{let E,f=[],L=[],z=!1,j="",H=!1,Y=setTimeout(()=>{H=!0,w.kill("SIGTERM"),setTimeout(()=>{try{w.kill("SIGKILL")}catch{}},5e3)},c),k=hn({input:w.stdout}),x=new Set,T=[];k.on("line",g=>{if(g.trim())try{let v=JSON.parse(g),U=ge(v);x.add(U),T.length<5&&mn(U)&&T.push(g.slice(0,800));let $=me(v,["sessionId","session_id","id"],["session","conversation","thread"]);$&&!E&&(E=$);let G=fn(v,f.length===0);G&&(f.push(G),l?.({type:"delta",jobId:s,text:G},s));let R=gn(v);R&&l?.({type:"thinking",jobId:s,text:R},s);let V=yn(v);V&&(L.push(V.label),l?.({type:"tool_use",jobId:s,tool:V.tool,...V.file?{file:V.file}:{},...V.content?{content:V.content}:{}},s));let ie=wn(v);ie&&(z=!0,j=ie)}catch{}});let S=[];w.stderr?.on("data",g=>{S.push(g.toString())}),w.on("close",g=>{clearTimeout(Y),k.close(),f.length===0&&x.size>0&&(console.warn(`[Copilot:${s}] No text captured. Event types seen: ${[...x].join(", ")}`),T.length>0&&console.warn(`[Copilot:${s}] Sample text-bearing candidates: ${T.join(`
|
|
31
31
|
`)}`)),H?(z=!0,j=`Timed out after ${Math.round(c/6e4)} minutes`):g!==0&&g!==null&&(z=!0,j=S.join("").trim()||j||`Copilot process exited with code ${g}`);let v=z?et(j,i):null;D({sessionId:E,text:f.join(""),success:!z,error:v?.message,errorCode:v?.code,recoverableError:v?.recoverable,provider:"copilot",toolsUsed:L.length>0?L:void 0})}),w.on("error",g=>{clearTimeout(Y);let v=et(g.message,i);D({sessionId:E,text:f.join(""),success:!1,error:v.message,errorCode:v.code,recoverableError:v.recoverable,provider:"copilot",toolsUsed:L.length>0?L:void 0})})});return{process:w,result:A}}function ge(s){if(typeof s.type=="string")return s.type;let t=s.update;return t&&typeof t.sessionUpdate=="string"?t.sessionUpdate:typeof s.event=="string"?s.event:"unknown"}function fn(s,t=!1){let n=ge(s).toLowerCase(),e=s.update,o=e?.content;if(e?.sessionUpdate==="agent_message_chunk"&&o?.type==="text"&&typeof o.text=="string")return o.text;if(n==="assistant.message_delta"||n.includes("agent_message.delta"))return ne(s.delta)??ne(s.content)??ne(s.message);if(n==="assistant.message"&&t)return ne(s.message)??ne(s.content)??ne(s);if(n==="result"&&t)return ne(s.result)??ne(s.output)??ne(s.message)??ne(s);let i=s.content;if(i?.type==="text"&&typeof i.text=="string")return i.text;if(typeof s.text=="string"&&De(s))return s.text;if(typeof s.message=="string"&&De(s))return s.message;if(typeof s.delta=="string"&&De(s))return s.delta}function ne(s){if(typeof s=="string")return s;if(!s)return;if(Array.isArray(s)){let n=s.map(ne).filter(e=>!!e);return n.length>0?n.join(""):void 0}if(typeof s!="object")return;let t=s;if(typeof t.text=="string")return t.text;if(typeof t.content=="string")return t.content;for(let n of["delta","content","message","messages","parts","items","output","result","data","value","body","markdown","text_delta","textDelta"]){let e=ne(t[n]);if(e)return e}}function mn(s){let t=s.toLowerCase();return t.includes("assistant.message")||t==="result"}function gn(s){if(typeof s.thinking=="string")return s.thinking;if(typeof s.reasoning=="string")return s.reasoning;let t=s.update;if(t?.sessionUpdate==="reasoning_chunk"){let n=t.content;if(typeof n?.text=="string")return n.text}}function yn(s){let t=me(s,["tool","toolName","tool_name","name"]),n=me(s,["command","cmd"]),e=me(s,["file","path","filename","filePath"]),o=ge(s);if(n||o.includes("shell"))return{tool:"Bash",label:n?`Bash: ${n.split(`
|
|
32
32
|
`)[0].slice(0,80)}`:"Bash",...n?{content:n}:{}};if(t)return{tool:t,label:e?`${t} ${e.split("/").pop()}`:t,...e?{file:e}:{}};if(e&&(o.includes("file")||o.includes("write")||o.includes("edit"))){let i=o.includes("write")?"Write":o.includes("read")?"Read":"Edit";return{tool:i,label:`${i} ${e.split("/").pop()}`,file:e}}}function wn(s){if(typeof s.error=="string")return s.error;if(s.error&&typeof s.error=="object"){let t=s.error;if(typeof t.message=="string")return t.message}if(typeof s.message=="string"&&ge(s).toLowerCase().includes("error"))return s.message}function me(s,t,n=[]){for(let e of t){let o=s[e];if(typeof o=="string")return o}for(let[e,o]of Object.entries(s)){if(!o||typeof o!="object"||n.length>0&&!n.some(c=>e.toLowerCase().includes(c)))continue;let i=me(o,t);if(i)return i}}function De(s){let t=ge(s).toLowerCase();return(typeof s.role=="string"?s.role.toLowerCase():"")==="assistant"||t.includes("assistant")||t.includes("agent_message")}import{execFile as vn}from"child_process";import{copyFile as nt,mkdir as st,readdir as Sn,readFile as xn,writeFile as bn}from"fs/promises";import{join as ue}from"path";var ke=class{constructor(t){this.projectRoot=t;let n=ue(t,".popmelt");this.decisionsDir=ue(n,"decisions"),this.screenshotsDir=ue(n,"screenshots")}async persist(t,n,e){try{await st(this.decisionsDir,{recursive:!0}),await st(this.screenshotsDir,{recursive:!0});try{await nt(n,ue(this.screenshotsDir,`s-${t.id}.webp`))}catch{}for(let o=0;o<e.length;o++)try{let i=t.pastedImagePaths[o];i&&await nt(e[o],ue(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await bn(ue(this.decisionsDir,`d-${t.id}.json`),JSON.stringify(t,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await Sn(this.decisionsDir)).filter(n=>n.startsWith("d-")&&n.endsWith(".json")).map(n=>n.slice(2,-5))}catch{return[]}}async loadDecision(t){try{let n=await xn(ue(this.decisionsDir,`d-${t}.json`),"utf-8");return JSON.parse(n)}catch{return null}}async loadDecisions(t){return(await Promise.all(t.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(t){return new Promise(n=>{vn("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){n(null);return}n(o||null)})})}};import{readFile as ot,writeFile as le}from"fs/promises";import{join as Ne}from"path";var X="[Materializer]",Pn={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function kn(s){return Array.isArray(s)?s.map((t,n)=>typeof t=="string"?{id:Math.random().toString(16).slice(2,10),scope:"general",text:t,sources:[]}:t&&typeof t=="object"&&typeof t.text=="string"?t:{id:Math.random().toString(16).slice(2,10),scope:"general",text:String(t),sources:[]}):[]}function ye(s){let t=[];for(let n of s){if(!n||typeof n!="object")continue;let e=n;if(typeof e.id!="string"||typeof e.text!="string"){console.warn(`${X} Dropping rule missing id or text:`,JSON.stringify(n).slice(0,120));continue}t.push({id:e.id,scope:typeof e.scope=="string"?e.scope:"general",text:e.text,sources:Array.isArray(e.sources)?e.sources.filter(o=>typeof o=="string"):[]})}return t.length>30&&console.warn(`${X} Rule count ${t.length} exceeds cap of 30`),t}var Ie=class{constructor(t,n,e={}){this.projectRoot=t;this.decisionStore=n;this.options=e;this.cachedIndex=null;this.running=!1;let o=Ne(t,".popmelt");this.indexPath=Ne(o,"materialized.json"),this.modelPath=Ne(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await ot(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=kn(n.rules)),n}catch{return null}}async addComponent(t){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let e=n.components;return e[t]?{added:!1,alreadyExists:!0}:(e[t]={description:""},await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,n){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=t.split("."),i=e;for(let h=0;h<o.length-1;h++){let y=o[h];(!i[y]||typeof i[y]!="object")&&(i[y]={}),i=i[y]}let c=o[o.length-1],l;try{l=JSON.parse(n)}catch{l=null}if(l&&typeof l=="object"&&l!==null&&"value"in l)i[c]=l;else{let h=i[c];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=n:i[c]=n}return await le(this.modelPath,JSON.stringify(e,null,2)),console.log(`${X} Updated token "${t}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=t.split("."),o=n;for(let c=0;c<e.length-1;c++){let l=e[c];if(!o[l]||typeof o[l]!="object")return{removed:!1};o=o[l]}let i=e[e.length-1];return i in o?(delete o[i],await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=n.components;return!e||!e[t]?{removed:!1}:(delete e[t],await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),n=new Set(t.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(c=>!n.has(c));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(c=>c.resolutions.some(l=>(l.finalScope??l.inferredScope)?.breadth==="pattern"))}async run(){if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let t=await this.getUnmaterializedPatternDecisions();if(t.length===0)return{processedIds:[],success:!0};let n=t.map(y=>y.id);console.log(`${X} Processing ${n.length} pattern-scoped decision(s): ${n.join(", ")}`),this.options.onEvent?.({type:"materialize_started",decisionIds:n});let e=await this.loadModel(),o=In(t,e),i=!0,c;try{let{result:y}=fe(`mat-${Date.now()}`,{prompt:o,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"],claudePath:this.options.claudePath??"claude"}),w=await y;if(!w.success)i=!1,c=w.error,console.error(`${X} Claude spawn error:`,c);else{let A=Te(w.text);A?(Array.isArray(A.rules)&&(A.rules=ye(A.rules)),await le(this.modelPath,JSON.stringify(A,null,2)),console.log(`${X} Successfully materialized ${n.length} decision(s) \u2192 ${this.modelPath}`)):(i=!1,c="No <model> block found in response",console.error(`${X} ${c}`))}}catch(y){i=!1,c=y instanceof Error?y.message:String(y),console.error(`${X} Error:`,c)}let l=await this.loadIndex(),h=new Set(l.materializedIds);for(let y of n)h.add(y);return l.materializedIds=[...h],l.lastRunAt=Date.now(),l.lastRunDecisionIds=n,l.lastRunError=c??null,await this.persistIndex(l),this.options.onEvent?.({type:"materialize_done",decisionIds:n,success:i,error:c}),{processedIds:n,success:i,error:c}}finally{this.running=!1}}async consolidate(){if(this.running)return{success:!1,error:"Already running"};this.running=!0;try{let t=await this.loadModel();if(!t)return{success:!1,error:"No model exists"};let n=Tn(t),{result:e}=fe(`consolidate-${Date.now()}`,{prompt:n,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[],claudePath:this.options.claudePath??"claude"}),o=await e;if(!o.success)return console.error(`${X} Consolidation spawn error:`,o.error),{success:!1,error:o.error};let i=Te(o.text);return i?(Array.isArray(i.rules)&&(i.rules=ye(i.rules)),!i.tokens&&t.tokens&&(i.tokens=t.tokens),!i.components&&t.components&&(i.components=t.components),await le(this.modelPath,JSON.stringify(i,null,2)),console.log(`${X} Consolidation complete \u2192 ${this.modelPath}`),{success:!0}):(console.error(`${X} No <model> block in consolidation response`),{success:!1,error:"No <model> block found"})}catch(t){let n=t instanceof Error?t.message:String(t);return console.error(`${X} Consolidation error:`,n),{success:!1,error:n}}finally{this.running=!1}}async writeModel(t){Array.isArray(t.rules)&&(t.rules=ye(t.rules)),await le(this.modelPath,JSON.stringify(t,null,2)),console.log(`${X} Model written \u2192 ${this.modelPath}`)}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await ot(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...Pn,materializedIds:[],lastRunDecisionIds:[]},this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await le(this.indexPath,JSON.stringify(t,null,2))}catch(n){console.error(`${X} Failed to write index:`,n)}}};function Te(s){let t=s.match(/<model>\s*([\s\S]*?)\s*<\/model>/);if(!t?.[1])return null;try{let n=JSON.parse(t[1]);return typeof n!="object"||n===null||Array.isArray(n)?null:n}catch{return null}}function In(s,t){let n=s.map(i=>{let l=i.resolutions.filter(w=>(w.finalScope??w.inferredScope)?.breadth==="pattern").map(w=>{let D=(w.finalScope??w.inferredScope)?.target??"unknown",E=w.filesModified?.join(", ")??"none";return`- **${w.summary}** [scope: pattern/${D}]
|
|
@@ -201,7 +201,7 @@ url = "${s}"
|
|
|
201
201
|
`)}function wt(s){return s.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function vt(s,t,n,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),n!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`);let i=t.find(c=>c.role==="human"&&c.feedbackContext);if(i?.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),t.length>0){o.push(""),o.push("## Conversation History");let c=0;for(let l of t)l.role==="human"?(c++,l.replyToQuestion?(o.push(`### Round ${c} (human) \u2014 reply`),o.push(`"${l.replyToQuestion}"`)):(o.push(`### Round ${c} (human)`),l.feedbackSummary&&o.push(`Annotations: ${l.feedbackSummary}`))):l.question?(o.push(`### Round ${c} (assistant) \u2014 question`),o.push(`"${l.question}"`)):(o.push(`### Round ${c} (assistant)`),l.responseText&&o.push(`Response: ${l.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let c of e)o.push(`Attached image: use the Read tool to view the image at: ${c}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
|
|
202
202
|
`)}function Bn(s){if(typeof s!="object"||s===null)return!1;let t=s;return(t.breadth==="instance"||t.breadth==="pattern")&&(t.target==="element"||t.target==="component"||t.target==="token")}function Jn(s){if(typeof s!="object"||s===null||typeof s.annotationId!="string"||s.status!=="resolved"&&s.status!=="needs_review"||typeof s.summary!="string")return!1;let t=s;for(let n of["declaredScope","inferredScope","finalScope"])if(t[n]!==void 0&&t[n]!==null&&!Bn(t[n]))return!1;return!0}function St(s){let t=s.match(/<resolution>\s*([\s\S]*?)\s*<\/resolution>/);if(!t||!t[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(Jn):[]}catch{return[]}}function xt(s){let t=s.match(/<novel>\s*([\s\S]*?)\s*<\/novel>/);if(!t?.[1])return[];try{let n=JSON.parse(t[1]);return Array.isArray(n)?n.filter(e=>{if(typeof e!="object"||e===null)return!1;let o=e;return(o.category==="token"||o.category==="component"||o.category==="element")&&typeof o.element=="string"&&typeof o.decision=="string"&&typeof o.reason=="string"}):[]}catch{return[]}}import{execFile as Un}from"child_process";import{readFile as Fn}from"fs/promises";import{homedir as Me}from"os";import{join as Se}from"path";var Ee=[{id:"claude-opus-4-6",label:"Opus 4.6",source:"static"},{id:"claude-sonnet-4-6",label:"Sonn 4.6",source:"static"}],xe=[{id:"gpt-5.4",label:"GPT 5.4",source:"static"},{id:"gpt-5.3-codex",label:"Codex 5.3",source:"static"},{id:"gpt-5.3-codex-spark",label:"Spark 5.3",source:"static"}],be={id:"",label:"Default",source:"cli"},jn=/^\s*-\s+"([^"]+)"/;function Ln(s){let t=[],n=!1;for(let e of s.split(`
|
|
203
203
|
`)){if(/^\s*`model`:/.test(e)){n=!0;continue}if(n&&/^\s*`[^`]+`:/.test(e))break;if(!n)continue;let o=e.match(jn);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:We(i),source:"cli"})}return Ce([be,...t])}function zn(s){let t=JSON.parse(s);return(Array.isArray(t.models)?t.models:[]).filter(e=>e.visibility===void 0||e.visibility==="list").sort((e,o)=>{let i=typeof e.priority=="number"?e.priority:Number.MAX_SAFE_INTEGER,c=typeof o.priority=="number"?o.priority:Number.MAX_SAFE_INTEGER;return i-c}).flatMap(e=>{let o=typeof e.slug=="string"?e.slug:typeof e.id=="string"?e.id:"";if(!o)return[];let i=typeof e.display_name=="string"&&e.display_name.trim()?e.display_name.trim():We(o);return[{id:o,label:i,source:"cli"}]})}function Hn(s){let t=s.match(/\bclaude-[a-z0-9]+(?:[-.][a-z0-9]+)*\b/gi)??[];return Ce(t.map(n=>({id:n,label:We(n),source:"cli"})))}function We(s){if(!s)return"Default";let t={"gpt-5.5":"GPT 5.5","gpt-5.4":"GPT 5.4","gpt-5.4-mini":"Mini 5.4","gpt-5.3-codex":"Codex 5.3","gpt-5.3-codex-spark":"Spark 5.3","gpt-5.2-codex":"Codex 5.2","gpt-5.2":"GPT 5.2","gpt-5.1":"GPT 5.1","gpt-5-mini":"Mini 5","gpt-4.1":"GPT 4.1","claude-opus-4.7":"Opus 4.7","claude-opus-4.6":"Opus 4.6","claude-opus-4.6-fast":"Opus 4.6 Fast","claude-opus-4.5":"Opus 4.5","claude-sonnet-4.6":"Sonn 4.6","claude-sonnet-4.5":"Sonn 4.5","claude-sonnet-4":"Sonn 4","claude-haiku-4.5":"Haiku 4.5"},n=s.replace(/-\d{8,}$/,"");if(t[s])return t[s];if(t[n])return t[n];let e=n.match(/^claude-(opus|sonnet|haiku)-(\d+)[-.](\d+)(?:-(fast))?$/);if(e)return`${e[1]==="sonnet"?"Sonn":Gn(e[1])} ${e[2]}.${e[3]}${e[4]?" Fast":""}`;let o=n.match(/^gpt-(\d+)(?:[.-](\d+))?(?:-(mini|codex|spark))?$/);if(o){let i=o[2]?`${o[1]}.${o[2]}`:o[1];return o[3]==="mini"?`Mini ${i}`:o[3]==="codex"?`Codex ${i}`:o[3]==="spark"?`Spark ${i}`:`GPT ${i}`}return n}async function bt(s="copilot"){let t=await It(s,["help","config"]),n=Ln(t);return n.length>1?n:[be]}async function Pt(s="codex"){let t=await It(s,["debug","models"]).catch(async()=>{let e=process.env.CODEX_HOME||Se(Me(),".codex");return Tt(Se(e,"models_cache.json"))}),n=zn(t);return n.length>0?Ce([...n,...xe]):xe}async function kt(){let s=[Se(Me(),".claude","stats-cache.json"),Se(Me(),".claude","settings.json"),Se(Me(),".claude.json")],t=await Promise.all(s.map(e=>Tt(e).catch(()=>""))),n=Hn(t.join(`
|
|
204
|
-
`));return Ce([...Ee,...n])}function It(s,t,n=5e3){return new Promise((e,o)=>{Un(s,t,{encoding:"utf-8",timeout:n},(i,c)=>{if(i){o(i);return}e(c)})})}async function Tt(s){return Fn(s,"utf-8")}function Ce(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Gn(s){return s.charAt(0).toUpperCase()+s.slice(1)}var $e=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let c=n<0?e.events:e.events.filter(l=>l.seq>n);return{jobId:t,events:c,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,c=()=>{i||(i=!0,o())};e.on("exit",c),e.on("error",c),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(c,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Wn,readFile as qn,writeFile as Xn}from"fs/promises";import{dirname as Yn,join as Qn}from"path";var Kn={version:1,threads:{}},Oe=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Qn(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await qn(this.filePath,"utf-8"),n=JSON.parse(t);if(n&&n.version===1&&n.threads)return this.cache=n,this.cache}catch{}return this.cache={...Kn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(c=>e.has(c)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let c of n)i.has(c)||o.elementIdentifiers.push(c);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let c=o.messages.find(l=>l.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:c,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Wn(Yn(this.filePath),{recursive:!0}),await Xn(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var Rt="0.6.
|
|
204
|
+
`));return Ce([...Ee,...n])}function It(s,t,n=5e3){return new Promise((e,o)=>{Un(s,t,{encoding:"utf-8",timeout:n},(i,c)=>{if(i){o(i);return}e(c)})})}async function Tt(s){return Fn(s,"utf-8")}function Ce(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Gn(s){return s.charAt(0).toUpperCase()+s.slice(1)}var $e=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let c=n<0?e.events:e.events.filter(l=>l.seq>n);return{jobId:t,events:c,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,c=()=>{i||(i=!0,o())};e.on("exit",c),e.on("error",c),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(c,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Wn,readFile as qn,writeFile as Xn}from"fs/promises";import{dirname as Yn,join as Qn}from"path";var Kn={version:1,threads:{}},Oe=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Qn(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await qn(this.filePath,"utf-8"),n=JSON.parse(t);if(n&&n.version===1&&n.threads)return this.cache=n,this.cache}catch{}return this.cache={...Kn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(c=>e.has(c)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let c of n)i.has(c)||o.elementIdentifiers.push(c);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let c=o.messages.find(l=>l.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:c,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Wn(Yn(this.filePath),{recursive:!0}),await Xn(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var Rt="0.6.9";var ls=1111,ds=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],us=1800*1e3,ps=3600*1e3;function $t(s){if(!s)return!1;try{let t=new URL(s);return t.hostname==="localhost"||t.hostname==="127.0.0.1"}catch{return!1}}function hs(s){let t=[];for(let n of s)if(n.type==="delta"){let e=n.text;if(!e)continue;let o=t[t.length-1];o&&o.kind==="text"?o.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",o=n.file??void 0,i=n.content??void 0,c=o?o.split("/").pop()??o:void 0,l;switch(e){case"Read":l=c?`Reading ${c}`:"Reading file";break;case"Edit":l=c?`Editing ${c}`:"Editing file";break;case"Write":l=c?`Writing ${c}`:"Writing file";break;case"Bash":l=i?i.split(`
|
|
205
205
|
`)[0].trim().slice(0,60):"Running command";break;case"Glob":l="Searching files";break;case"Grep":l="Searching code";break;case"WebFetch":l="Fetching page";break;case"WebSearch":l="Searching web";break;default:l=e?`Using ${e}`:"tool";break}let h=o??i??void 0,y=t[t.length-1];y&&y.kind==="tool_group"&&y.tool===e?y.items.push({label:l,detail:h}):t.push({kind:"tool_group",tool:e,items:[{label:l,detail:h}]})}return t}function fs(s,t){let n=s.headers.origin;$t(n)&&(t.setHeader("Access-Control-Allow-Origin",n),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function b(s,t,n){s.writeHead(t,{"Content-Type":"application/json"}),s.end(JSON.stringify(n))}function ms(s,t){if(!s)return t;let n=s.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(!n)return t;let[,e,o,i]=n;return`\x1B[38;2;${parseInt(e,16)};${parseInt(o,16)};${parseInt(i,16)}m${t}\x1B[0m`}function qe(s,t){try{s.res.write(`event: ${t.type}
|
|
206
206
|
data: ${JSON.stringify(t)}
|
|
207
207
|
|
package/dist/cli.mjs
CHANGED
|
@@ -27,8 +27,8 @@ import{spawn as fs}from"child_process";import{readFile as ms,unlink as gs}from"f
|
|
|
27
27
|
</script>
|
|
28
28
|
</body>
|
|
29
29
|
</html>`}import{spawn as en}from"child_process";import{createInterface as tn}from"readline";var nn=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"]),et=1e5;function sn(s){let t=s.input?.file_path||s.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!nn.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>et?e.slice(0,et)+`
|
|
30
|
-
\u2026[truncated]`:e}function me(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:c=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:l="claude",resumeSessionId:h,model:y,timeoutMs:w=3e5,onEvent:A}=t,D=[];h?D.push("--resume",h,"-p",n):D.push("-p",n),D.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),y&&D.push("--model",y);for(let L of c)D.push("--allowedTools",L);let M=en(l,D,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),f=new Promise(L=>{let z,F=[],G=[],Y=!1,
|
|
31
|
-
`)[0].slice(0,80)}`:"Bash";f.push(U),l?.({type:"tool_use",jobId:s,tool:"Bash",...v?{content:v}:{}},s)}else if(S==="file_change"){let g=x.item.filename||x.item.path;f.push(g?`Edit ${g.split("/").pop()}`:"Edit"),l?.({type:"tool_use",jobId:s,tool:"Edit",...g?{file:g}:{}},s)}else if(S==="file_read"){let g=x.item.filename||x.item.path;f.push(g?`Read ${g.split("/").pop()}`:"Read"),l?.({type:"tool_use",jobId:s,tool:"Read",...g?{file:g}:{}},s)}else if(S==="web_search")f.push("WebSearch"),l?.({type:"tool_use",jobId:s,tool:"WebSearch"},s);else if(S==="mcp_tool_call"){let g=x.item.tool_name||x.item.name||"MCP";f.push(g),l?.({type:"tool_use",jobId:s,tool:g},s)}}if((T==="item.completed"||T==="item/completed")&&x.item){if(x.item.type==="agent_message"){let S=x.item.text;typeof S=="string"&&S&&(M.push(S),l?.({type:"delta",jobId:s,text:S},s))}else if(x.item.type==="reasoning"){let S=x.item.text;typeof S=="string"&&S&&l?.({type:"thinking",jobId:s,text:S},s)}else if(x.item.type==="file_change"&&Array.isArray(x.item.changes))for(let S of x.item.changes){let g=S.path||S.filename,v=S.kind==="add"?"Write":"Edit";g&&(f.push(`${v} ${g.split("/").pop()}`),l?.({type:"tool_use",jobId:s,tool:v,file:g},s))}}T==="turn.failed"&&(L=!0,z=x.error?.message||x.message||"Turn failed")}catch{}});let Y=[];y.stderr?.on("data",
|
|
30
|
+
\u2026[truncated]`:e}function me(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:c=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:l="claude",resumeSessionId:h,model:y,timeoutMs:w=3e5,onEvent:A}=t,D=[];h?D.push("--resume",h,"-p",n):D.push("-p",n),D.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),y&&D.push("--model",y);for(let L of c)D.push("--allowedTools",L);let M=en(l,D,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),f=new Promise(L=>{let z,F=[],G=[],Y=!1,P="",x=!1,T=setTimeout(()=>{x=!0,M.kill("SIGTERM"),setTimeout(()=>{try{M.kill("SIGKILL")}catch{}},5e3)},w),S=tn({input:M.stdout}),g=new Set;S.on("line",U=>{if(U.trim())try{let C=JSON.parse(U);C.session_id&&!z&&(z=C.session_id);let H=C.type??(C.event?.type?`event.${C.event.type}`:"unknown");if(g.add(H),C.type==="result"&&C.result&&F.length===0){let R=typeof C.result=="string"?C.result:"";R&&(F.push(R),A?.({type:"delta",jobId:s,text:R},s))}if(C.type==="assistant"&&Array.isArray(C.message?.content))for(let R of C.message.content){if(R.type==="text"&&R.text&&(F.push(R.text),A?.({type:"delta",jobId:s,text:R.text},s)),R.type==="tool_use"&&R.name){let V=R.input?.file_path||R.input?.path||void 0,ae=sn(R);A?.({type:"tool_use",jobId:s,tool:R.name,...V?{file:V}:{},...ae?{content:ae}:{}},s),R.name==="Edit"&&R.input?.file_path?G.push({tool:"Edit",file_path:R.input.file_path,old_string:R.input.old_string,new_string:R.input.new_string,replace_all:R.input.replace_all}):R.name==="Write"&&R.input?.file_path&&G.push({tool:"Write",file_path:R.input.file_path,content:R.input.content})}R.type==="thinking"&&R.thinking&&A?.({type:"thinking",jobId:s,text:R.thinking},s)}C.type==="user"&&C.tool_use_result?.file?.filePath&&A?.({type:"tool_use",jobId:s,tool:"Read",file:C.tool_use_result.file.filePath},s)}catch{}});let v=[];M.stderr?.on("data",U=>{v.push(U.toString())}),M.on("close",U=>{if(clearTimeout(T),S.close(),F.length===0&&g.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...g].join(", ")}`),x)Y=!0,P=`Timed out after ${Math.round(w/6e4)} minutes`;else if(U!==0&&U!==null){Y=!0;let C=v.join("").trim(),H=F.length===0&&g.size>0?` (no text captured, event types: ${[...g].join(", ")})`:"";P=C||`Claude process exited with code ${U}${H}`}L({sessionId:z,text:F.join(""),success:!Y,error:Y?P:void 0,fileEdits:G.length>0?G:void 0})}),M.on("error",U=>{clearTimeout(T),Y=!0,P=U.message,L({sessionId:z,text:F.join(""),success:!1,error:P,fileEdits:G.length>0?G:void 0})})});return{process:M,result:f}}import{spawn as on}from"child_process";import{createInterface as rn}from"readline";function an(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}function tt(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:c,onEvent:l}=t,h=[];i?(h.push("exec","resume","--skip-git-repo-check","--json","-c",'sandbox_mode="workspace-write"'),c&&h.push("-m",c),h.push(i),h.push(n),o&&h.push("--image",o)):(h.push("exec","--skip-git-repo-check","--json","--sandbox","workspace-write"),c&&h.push("-m",c),h.push(n),o&&h.push("--image",o));let y=on("codex",h,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),w=new Promise(A=>{let D,M=[],f=[],L=!1,z="",F=rn({input:y.stdout}),G=new Set;F.on("line",P=>{if(P.trim())try{let x=JSON.parse(P),T=x.type??"unknown";if(G.add(T),T==="thread.started"&&x.thread_id&&!D&&(D=x.thread_id),(T==="item.agentMessage.delta"||T==="item/agentMessage/delta")&&x.delta?.text&&(M.push(x.delta.text),l?.({type:"delta",jobId:s,text:x.delta.text},s)),(T==="item.reasoning.delta"||T==="item/reasoning/delta")&&x.delta?.text&&l?.({type:"thinking",jobId:s,text:x.delta.text},s),(T==="item.started"||T==="item/started")&&x.item){let S=x.item.type;if(S==="command_execution"){let g=x.item.command,v=g?an(g):void 0,U=v?`Bash: ${v.split(`
|
|
31
|
+
`)[0].slice(0,80)}`:"Bash";f.push(U),l?.({type:"tool_use",jobId:s,tool:"Bash",...v?{content:v}:{}},s)}else if(S==="file_change"){let g=x.item.filename||x.item.path;f.push(g?`Edit ${g.split("/").pop()}`:"Edit"),l?.({type:"tool_use",jobId:s,tool:"Edit",...g?{file:g}:{}},s)}else if(S==="file_read"){let g=x.item.filename||x.item.path;f.push(g?`Read ${g.split("/").pop()}`:"Read"),l?.({type:"tool_use",jobId:s,tool:"Read",...g?{file:g}:{}},s)}else if(S==="web_search")f.push("WebSearch"),l?.({type:"tool_use",jobId:s,tool:"WebSearch"},s);else if(S==="mcp_tool_call"){let g=x.item.tool_name||x.item.name||"MCP";f.push(g),l?.({type:"tool_use",jobId:s,tool:g},s)}}if((T==="item.completed"||T==="item/completed")&&x.item){if(x.item.type==="agent_message"){let S=x.item.text;typeof S=="string"&&S&&(M.push(S),l?.({type:"delta",jobId:s,text:S},s))}else if(x.item.type==="reasoning"){let S=x.item.text;typeof S=="string"&&S&&l?.({type:"thinking",jobId:s,text:S},s)}else if(x.item.type==="file_change"&&Array.isArray(x.item.changes))for(let S of x.item.changes){let g=S.path||S.filename,v=S.kind==="add"?"Write":"Edit";g&&(f.push(`${v} ${g.split("/").pop()}`),l?.({type:"tool_use",jobId:s,tool:v,file:g},s))}}T==="turn.failed"&&(L=!0,z=x.error?.message||x.message||"Turn failed")}catch{}});let Y=[];y.stderr?.on("data",P=>{Y.push(P.toString())}),y.on("close",P=>{F.close(),M.length===0&&G.size>0&&console.warn(`[Codex:${s}] No text captured. Event types seen: ${[...G].join(", ")}`),P!==0&&P!==null&&(L=!0,z=Y.join("")||`Codex process exited with code ${P}`),A({sessionId:D,text:M.join(""),success:!L,error:L?z:void 0,toolsUsed:f.length>0?f:void 0})}),y.on("error",P=>{L=!0,z=P.message,A({sessionId:D,text:M.join(""),success:!1,error:z,toolsUsed:f.length>0?f:void 0})})});return{process:y,result:w}}import{spawn as cn}from"child_process";import{createInterface as ln}from"readline";function nt(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 st(s,t){let{prompt:n,projectRoot:e,resumeSessionId:o,model:i,timeoutMs:c=3e5,onEvent:l,copilotPath:h="copilot"}=t,y=["--no-color","--no-auto-update","--output-format","json","--stream","on","--available-tools=read,write,shell","--allow-all-tools","--deny-tool=shell(git push)","--deny-tool=shell(rm)","--deny-tool=shell(sudo)","--no-ask-user"];o&&y.push(`--resume=${o}`),i&&y.push("--model",i),y.push("-p",n);let w=cn(h,y,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),A=new Promise(D=>{let M,f=[],L=[],z=!1,F="",G=!1,Y=setTimeout(()=>{G=!0,w.kill("SIGTERM"),setTimeout(()=>{try{w.kill("SIGKILL")}catch{}},5e3)},c),P=ln({input:w.stdout}),x=new Set,T=[];P.on("line",g=>{if(g.trim())try{let v=JSON.parse(g),U=ye(v);x.add(U),T.length<5&&un(U)&&T.push(g.slice(0,800));let C=ge(v,["sessionId","session_id","id"],["session","conversation","thread"]);C&&!M&&(M=C);let H=dn(v,f.length===0);H&&(f.push(H),l?.({type:"delta",jobId:s,text:H},s));let R=pn(v);R&&l?.({type:"thinking",jobId:s,text:R},s);let V=hn(v);V&&(L.push(V.label),l?.({type:"tool_use",jobId:s,tool:V.tool,...V.file?{file:V.file}:{},...V.content?{content:V.content}:{}},s));let ae=fn(v);ae&&(z=!0,F=ae)}catch{}});let S=[];w.stderr?.on("data",g=>{S.push(g.toString())}),w.on("close",g=>{clearTimeout(Y),P.close(),f.length===0&&x.size>0&&(console.warn(`[Copilot:${s}] No text captured. Event types seen: ${[...x].join(", ")}`),T.length>0&&console.warn(`[Copilot:${s}] Sample text-bearing candidates: ${T.join(`
|
|
32
32
|
`)}`)),G?(z=!0,F=`Timed out after ${Math.round(c/6e4)} minutes`):g!==0&&g!==null&&(z=!0,F=S.join("").trim()||F||`Copilot process exited with code ${g}`);let v=z?nt(F,i):null;D({sessionId:M,text:f.join(""),success:!z,error:v?.message,errorCode:v?.code,recoverableError:v?.recoverable,provider:"copilot",toolsUsed:L.length>0?L:void 0})}),w.on("error",g=>{clearTimeout(Y);let v=nt(g.message,i);D({sessionId:M,text:f.join(""),success:!1,error:v.message,errorCode:v.code,recoverableError:v.recoverable,provider:"copilot",toolsUsed:L.length>0?L:void 0})})});return{process:w,result:A}}function ye(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 dn(s,t=!1){let n=ye(s).toLowerCase(),e=s.update,o=e?.content;if(e?.sessionUpdate==="agent_message_chunk"&&o?.type==="text"&&typeof o.text=="string")return o.text;if(n==="assistant.message_delta"||n.includes("agent_message.delta"))return ne(s.delta)??ne(s.content)??ne(s.message);if(n==="assistant.message"&&t)return ne(s.message)??ne(s.content)??ne(s);if(n==="result"&&t)return ne(s.result)??ne(s.output)??ne(s.message)??ne(s);let i=s.content;if(i?.type==="text"&&typeof i.text=="string")return i.text;if(typeof s.text=="string"&&Ne(s))return s.text;if(typeof s.message=="string"&&Ne(s))return s.message;if(typeof s.delta=="string"&&Ne(s))return s.delta}function ne(s){if(typeof s=="string")return s;if(!s)return;if(Array.isArray(s)){let n=s.map(ne).filter(e=>!!e);return n.length>0?n.join(""):void 0}if(typeof s!="object")return;let t=s;if(typeof t.text=="string")return t.text;if(typeof t.content=="string")return t.content;for(let n of["delta","content","message","messages","parts","items","output","result","data","value","body","markdown","text_delta","textDelta"]){let e=ne(t[n]);if(e)return e}}function un(s){let t=s.toLowerCase();return t.includes("assistant.message")||t==="result"}function pn(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 hn(s){let t=ge(s,["tool","toolName","tool_name","name"]),n=ge(s,["command","cmd"]),e=ge(s,["file","path","filename","filePath"]),o=ye(s);if(n||o.includes("shell"))return{tool:"Bash",label:n?`Bash: ${n.split(`
|
|
33
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 fn(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"&&ye(s).toLowerCase().includes("error"))return s.message}function ge(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=ge(o,t);if(i)return i}}function Ne(s){let t=ye(s).toLowerCase();return(typeof s.role=="string"?s.role.toLowerCase():"")==="assistant"||t.includes("assistant")||t.includes("agent_message")}import{execFile as mn}from"child_process";import{copyFile as ot,mkdir as rt,readdir as gn,readFile as yn,writeFile as wn}from"fs/promises";import{join as pe}from"path";var Ie=class{constructor(t){this.projectRoot=t;let n=pe(t,".popmelt");this.decisionsDir=pe(n,"decisions"),this.screenshotsDir=pe(n,"screenshots")}async persist(t,n,e){try{await rt(this.decisionsDir,{recursive:!0}),await rt(this.screenshotsDir,{recursive:!0});try{await ot(n,pe(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],pe(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await wn(pe(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 gn(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 yn(pe(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=>{mn("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){n(null);return}n(o||null)})})}};import{readFile as it,writeFile as de}from"fs/promises";import{join as Be}from"path";var X="[Materializer]",vn={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function Sn(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 we(s){let t=[];for(let n of s){if(!n||typeof n!="object")continue;let e=n;if(typeof e.id!="string"||typeof e.text!="string"){console.warn(`${X} Dropping rule missing id or text:`,JSON.stringify(n).slice(0,120));continue}t.push({id:e.id,scope:typeof e.scope=="string"?e.scope:"general",text:e.text,sources:Array.isArray(e.sources)?e.sources.filter(o=>typeof o=="string"):[]})}return t.length>30&&console.warn(`${X} Rule count ${t.length} exceeds cap of 30`),t}var Te=class{constructor(t,n,e={}){this.projectRoot=t;this.decisionStore=n;this.options=e;this.cachedIndex=null;this.running=!1;let o=Be(t,".popmelt");this.indexPath=Be(o,"materialized.json"),this.modelPath=Be(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await it(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=Sn(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 de(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,n){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=t.split("."),i=e;for(let h=0;h<o.length-1;h++){let y=o[h];(!i[y]||typeof i[y]!="object")&&(i[y]={}),i=i[y]}let c=o[o.length-1],l;try{l=JSON.parse(n)}catch{l=null}if(l&&typeof l=="object"&&l!==null&&"value"in l)i[c]=l;else{let h=i[c];h&&typeof h=="object"&&h!==null&&"value"in h?h.value=n:i[c]=n}return await de(this.modelPath,JSON.stringify(e,null,2)),console.log(`${X} Updated token "${t}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=t.split("."),o=n;for(let c=0;c<e.length-1;c++){let l=e[c];if(!o[l]||typeof o[l]!="object")return{removed:!1};o=o[l]}let i=e[e.length-1];return i in o?(delete o[i],await de(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=n.components;return!e||!e[t]?{removed:!1}:(delete e[t],await de(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),n=new Set(t.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(c=>!n.has(c));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(c=>c.resolutions.some(l=>(l.finalScope??l.inferredScope)?.breadth==="pattern"))}async run(){if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let t=await this.getUnmaterializedPatternDecisions();if(t.length===0)return{processedIds:[],success:!0};let n=t.map(y=>y.id);console.log(`${X} Processing ${n.length} pattern-scoped decision(s): ${n.join(", ")}`),this.options.onEvent?.({type:"materialize_started",decisionIds:n});let e=await this.loadModel(),o=xn(t,e),i=!0,c;try{let{result:y}=me(`mat-${Date.now()}`,{prompt:o,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"],claudePath:this.options.claudePath??"claude"}),w=await y;if(!w.success)i=!1,c=w.error,console.error(`${X} Claude spawn error:`,c);else{let A=Re(w.text);A?(Array.isArray(A.rules)&&(A.rules=we(A.rules)),await de(this.modelPath,JSON.stringify(A,null,2)),console.log(`${X} Successfully materialized ${n.length} decision(s) \u2192 ${this.modelPath}`)):(i=!1,c="No <model> block found in response",console.error(`${X} ${c}`))}}catch(y){i=!1,c=y instanceof Error?y.message:String(y),console.error(`${X} Error:`,c)}let l=await this.loadIndex(),h=new Set(l.materializedIds);for(let y of n)h.add(y);return l.materializedIds=[...h],l.lastRunAt=Date.now(),l.lastRunDecisionIds=n,l.lastRunError=c??null,await this.persistIndex(l),this.options.onEvent?.({type:"materialize_done",decisionIds:n,success:i,error:c}),{processedIds:n,success:i,error:c}}finally{this.running=!1}}async consolidate(){if(this.running)return{success:!1,error:"Already running"};this.running=!0;try{let t=await this.loadModel();if(!t)return{success:!1,error:"No model exists"};let n=bn(t),{result:e}=me(`consolidate-${Date.now()}`,{prompt:n,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[],claudePath:this.options.claudePath??"claude"}),o=await e;if(!o.success)return console.error(`${X} Consolidation spawn error:`,o.error),{success:!1,error:o.error};let i=Re(o.text);return i?(Array.isArray(i.rules)&&(i.rules=we(i.rules)),!i.tokens&&t.tokens&&(i.tokens=t.tokens),!i.components&&t.components&&(i.components=t.components),await de(this.modelPath,JSON.stringify(i,null,2)),console.log(`${X} Consolidation complete \u2192 ${this.modelPath}`),{success:!0}):(console.error(`${X} No <model> block in consolidation response`),{success:!1,error:"No <model> block found"})}catch(t){let n=t instanceof Error?t.message:String(t);return console.error(`${X} Consolidation error:`,n),{success:!1,error:n}}finally{this.running=!1}}async writeModel(t){Array.isArray(t.rules)&&(t.rules=we(t.rules)),await de(this.modelPath,JSON.stringify(t,null,2)),console.log(`${X} Model written \u2192 ${this.modelPath}`)}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await it(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...vn,materializedIds:[],lastRunDecisionIds:[]},this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await de(this.indexPath,JSON.stringify(t,null,2))}catch(n){console.error(`${X} Failed to write index:`,n)}}};function Re(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 xn(s,t){let n=s.map(i=>{let l=i.resolutions.filter(w=>(w.finalScope??w.inferredScope)?.breadth==="pattern").map(w=>{let D=(w.finalScope??w.inferredScope)?.target??"unknown",M=w.filesModified?.join(", ")??"none";return`- **${w.summary}** [scope: pattern/${D}]
|
|
34
34
|
Files modified: ${M}`}).join(`
|
|
@@ -186,27 +186,27 @@ These are my proposed rule changes. Would you like to approve all of them, adjus
|
|
|
186
186
|
</question>
|
|
187
187
|
|
|
188
188
|
## After approval
|
|
189
|
-
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
|
|
190
|
-
`),n=[],e=null;for(let o of t){let i=o.match(
|
|
189
|
+
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 kn}from"child_process";import{readFile as dt}from"fs/promises";import{homedir as Ue}from"os";import{join as re}from"path";var je=/popmelt/i;function he(){return{found:!1,name:null,scope:null,disabled:!1}}function fe(s,t,n=!1){return{found:!0,name:s,scope:t,disabled:n}}function ve(s){for(let t of Object.keys(s))if(je.test(t))return t;return null}async function Ee(s){try{let t=await dt(s,"utf-8");return JSON.parse(t)}catch{return null}}async function Fe(s){let t=Ue(),n=await Ee(re(t,".claude.json"));if(n&&typeof n=="object"){let o=n;if(o.mcpServers&&typeof o.mcpServers=="object"){let i=ve(o.mcpServers);if(i)return fe(i,"user")}if(o.projects&&typeof o.projects=="object"){let c=o.projects[s];if(c&&typeof c=="object"){let l=c;if(l.mcpServers&&typeof l.mcpServers=="object"){let h=ve(l.mcpServers);if(h){let y=Array.isArray(l.disabledMcpjsonServers)&&l.disabledMcpjsonServers.some(w=>je.test(w));return fe(h,"project",y)}}}}}let e=await Ee(re(s,".mcp.json"));if(e&&typeof e=="object"){let o=e;if(o.mcpServers&&typeof o.mcpServers=="object"){let c=ve(o.mcpServers);if(c){let l=await ct(s,c);return fe(c,"mcp.json",l)}}let i=ve(o);if(i&&i!=="mcpServers"){let c=await ct(s,i);return fe(i,"mcp.json",c)}}return he()}async function ct(s,t){let n=re(s,".claude","settings.local.json"),e=await Ee(n);if(e&&typeof e=="object"){let o=e;if(Array.isArray(o.disabledMcpjsonServers))return o.disabledMcpjsonServers.some(i=>i===t)}return!1}var Pn=/^\[mcp_servers\.([^\]]+)\]/;function In(s){let t=s.split(`
|
|
190
|
+
`),n=[],e=null;for(let o of t){let i=o.match(Pn);i?(e&&n.push({name:e.name,body:e.bodyLines.join(`
|
|
191
191
|
`)}),e={name:i[1],bodyLines:[]}):o.startsWith("[")?e&&(n.push({name:e.name,body:e.bodyLines.join(`
|
|
192
192
|
`)}),e=null):e&&e.bodyLines.push(o)}return e&&n.push({name:e.name,body:e.bodyLines.join(`
|
|
193
|
-
`)}),n}function Tn(s){return/enabled\s*=\s*false/i.test(s)}async function Le(s){let t=process.env.CODEX_HOME||re(Ue(),".codex"),n=await lt(re(t,"config.toml"),"user");if(n.found)return n;let e=await lt(re(s,".codex","config.toml"),"project");return e.found?e:he()}async function ze(s,t="copilot"){let n=await Rn(t);if(n.found)return n;let e=process.env.COPILOT_HOME||re(Ue(),".copilot"),o=await Je(re(e,"mcp-config.json"),"user");if(o.found)return o;let i=await Je(re(s,".mcp.json"),"mcp.json");if(i.found)return i;let c=await Je(re(s,".github","mcp.json"),"mcp.json");return c.found?c:he()}async function Rn(s){try{let t=await new Promise((n,e)=>{
|
|
193
|
+
`)}),n}function Tn(s){return/enabled\s*=\s*false/i.test(s)}async function Le(s){let t=process.env.CODEX_HOME||re(Ue(),".codex"),n=await lt(re(t,"config.toml"),"user");if(n.found)return n;let e=await lt(re(s,".codex","config.toml"),"project");return e.found?e:he()}async function ze(s,t="copilot"){let n=await Rn(t);if(n.found)return n;let e=process.env.COPILOT_HOME||re(Ue(),".copilot"),o=await Je(re(e,"mcp-config.json"),"user");if(o.found)return o;let i=await Je(re(s,".mcp.json"),"mcp.json");if(i.found)return i;let c=await Je(re(s,".github","mcp.json"),"mcp.json");return c.found?c:he()}async function Rn(s){try{let t=await new Promise((n,e)=>{kn(s,["mcp","list","--json"],{encoding:"utf-8",timeout:5e3},(o,i)=>{if(o){e(o);return}n(i)})});return ut(JSON.parse(t),"user")}catch{return he()}}async function Je(s,t){let n=await Ee(s);return ut(n,t)}function ut(s,t){if(!s||typeof s!="object")return he();let n=s,e=n.mcpServers&&typeof n.mcpServers=="object"?n.mcpServers:n.servers&&typeof n.servers=="object"?n.servers:n,o=ve(e);return o?fe(o,t):he()}async function lt(s,t){try{let n=await dt(s,"utf-8"),e=In(n);for(let o of e)if(je.test(o.name)){let i=Tn(o.body);return fe(o.name,t,i)}}catch{}return he()}import{execFile as pt}from"child_process";import{mkdir as En,readFile as ht,writeFile as ft}from"fs/promises";import{homedir as mt}from"os";import{dirname as Mn,join as Ge}from"path";var He="https://mcp.popmelt.com/mcp";async function gt(s=He){let t=Ge(mt(),".claude.json"),n;try{let o=await ht(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 ft(t,JSON.stringify(n,null,2)+`
|
|
194
194
|
`,"utf-8"),{installed:!0,provider:"claude",scope:"user"}}async function yt(s=He){let t=process.env.CODEX_HOME||Ge(mt(),".codex"),n=Ge(t,"config.toml"),e;try{e=await ht(n,"utf-8")}catch{e=""}if(/\[mcp_servers\.[^\]]*popmelt[^\]]*\]/i.test(e))return{installed:!1,provider:"codex",scope:null,reason:"already_configured"};await En(Mn(n),{recursive:!0});let o=`
|
|
195
195
|
[mcp_servers.popmelt]
|
|
196
196
|
url = "${s}"
|
|
197
|
-
`;return await ft(n,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function wt(s=He,t="copilot"){let n=await $n(t);if(Cn(n))return{installed:!1,provider:"copilot",scope:null,reason:"already_configured"};try{return await new Promise((e,o)=>{pt(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 $n(s){try{let t=await new Promise((n,e)=>{pt(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 Cn(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 We(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 On(s),i=Buffer.from(`--${e}`),c=Buffer.from(`--${e}--`),l,h,y,w,A,D,M,f,L,z,F,G,Y=[],
|
|
197
|
+
`;return await ft(n,e+o,"utf-8"),{installed:!0,provider:"codex",scope:"user"}}async function wt(s=He,t="copilot"){let n=await $n(t);if(Cn(n))return{installed:!1,provider:"copilot",scope:null,reason:"already_configured"};try{return await new Promise((e,o)=>{pt(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 $n(s){try{let t=await new Promise((n,e)=>{pt(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 Cn(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 We(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 On(s),i=Buffer.from(`--${e}`),c=Buffer.from(`--${e}--`),l,h,y,w,A,D,M,f,L,z,F,G,Y=[],P=[],x=0,T=[];for(;x<o.length;){let S=o.indexOf(i,x);if(S===-1)break;let g=S+i.length;if(o.slice(S,S+c.length).equals(c))break;let v=g;o[v]===13&&o[v+1]===10&&(v+=2);let U=o.indexOf(`\r
|
|
198
198
|
\r
|
|
199
|
-
`,v);if(U===-1)break;let C=o.slice(v,U).toString("utf-8"),H=U+4,R=o.indexOf(i,H),V=R!==-1?R-2:o.length;T.push({headers:C,body:o.slice(H,V)}),x=R!==-1?R:o.length}for(let S of T){let g=S.headers.match(/name="([^"]+)"/);if(!g)continue;let v=g[1];if(v==="screenshot")l=S.body;else if(v==="feedback")h=S.body.toString("utf-8");else if(v==="color")y=S.body.toString("utf-8");else if(v==="provider")w=S.body.toString("utf-8");else if(v==="model")A=S.body.toString("utf-8");else if(v==="goal")D=S.body.toString("utf-8");else if(v==="pageUrl")M=S.body.toString("utf-8");else if(v==="viewport")f=S.body.toString("utf-8");else if(v==="planId")L=S.body.toString("utf-8");else if(v==="manifest")z=S.body.toString("utf-8");else if(v==="tasks")F=S.body.toString("utf-8");else if(v==="sourceId")G=S.body.toString("utf-8");else if(v.startsWith("screenshot-")){let U=v.slice(11);try{let C=decodeURIComponent(U);
|
|
199
|
+
`,v);if(U===-1)break;let C=o.slice(v,U).toString("utf-8"),H=U+4,R=o.indexOf(i,H),V=R!==-1?R-2:o.length;T.push({headers:C,body:o.slice(H,V)}),x=R!==-1?R:o.length}for(let S of T){let g=S.headers.match(/name="([^"]+)"/);if(!g)continue;let v=g[1];if(v==="screenshot")l=S.body;else if(v==="feedback")h=S.body.toString("utf-8");else if(v==="color")y=S.body.toString("utf-8");else if(v==="provider")w=S.body.toString("utf-8");else if(v==="model")A=S.body.toString("utf-8");else if(v==="goal")D=S.body.toString("utf-8");else if(v==="pageUrl")M=S.body.toString("utf-8");else if(v==="viewport")f=S.body.toString("utf-8");else if(v==="planId")L=S.body.toString("utf-8");else if(v==="manifest")z=S.body.toString("utf-8");else if(v==="tasks")F=S.body.toString("utf-8");else if(v==="sourceId")G=S.body.toString("utf-8");else if(v.startsWith("screenshot-")){let U=v.slice(11);try{let C=decodeURIComponent(U);P.push({pathname:C,data:S.body})}catch{}}else if(v.startsWith("image-")){let U=v.split("-"),C=parseInt(U[U.length-1],10),H=U.slice(1,-1).join("-");H&&!isNaN(C)&&Y.push({annotationId:H,index:C,data:S.body})}}if(!l)throw new Error("Missing screenshot field");return h||(h=""),{screenshot:l,feedback:h,color:y,provider:w,model:A,goal:D,pageUrl:M,viewport:f,planId:L,manifest:z,tasks:F,sourceId:G,pastedImages:Y,pageScreenshots:P}}function On(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 Se(s,t){let n=[];if(s.annotations.length>0){n.push("## Annotations");for(let e of s.annotations){let o=e.elements.map(l=>{let h=[l.selector];return l.reactComponent&&h.push(`(${l.reactComponent})`),h.join(" ")}).join(", "),i=e.instruction||"No text";n.push(`- id=${e.id} [${e.type}] ${i} \u2192 Elements: ${o||"none"}`);let c=t?.[e.id];if(c&&c.length>0)for(let l of c)n.push(` Attached image: use the Read tool to view ${l}`)}}if(s.styleModifications.length>0){n.push(""),n.push("## Style Changes (make permanent in source)"),n.push("The developer previewed these CSS changes via inline style overrides. Find the corresponding styles in the source files and update them so the changes persist:");for(let e of s.styleModifications){let o=e.element?.reactComponent?`(${e.element.reactComponent})`:"";for(let i of e.changes)n.push(`- ${e.selector} ${o}: ${i.property} ${i.original} \u2192 ${i.modified}`)}}if(s.spacingTokenChanges?.length){n.push(""),n.push("## Spacing Token Changes"),n.push("The developer adjusted these spacing tokens. Apply each change to the source code:");for(let e of s.spacingTokenChanges){n.push(`
|
|
200
200
|
### ${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(`
|
|
201
201
|
`)}function vt(s,t,n){let e=[],i=new Set(t.annotations.map(c=>c.pathname).filter(Boolean)).size>1;if(e.push("You are reviewing a UI screenshot with developer annotations."),e.push(""),!i&&n?.provider!=="codex"&&(e.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`),e.push("")),e.push(`The developer annotated their running app at ${t.url} (${t.viewport.width}x${t.viewport.height}).`),n?.threadHistory&&n.threadHistory.length>0){e.push(""),e.push("## Previous Conversation");let c=0;for(let l of n.threadHistory)if(l.role==="human")c++,l.replyToQuestion?(e.push(`### Round ${c} (human) \u2014 reply`),e.push(`"${l.replyToQuestion}"`)):(e.push(`### Round ${c} (human)`),l.feedbackSummary&&e.push(`Annotations: ${l.feedbackSummary}`),l.annotationIds&&l.annotationIds.length>0&&e.push(`Annotation IDs: ${l.annotationIds.join(", ")}`));else if(l.question)e.push(`### Round ${c} (assistant) \u2014 question`),e.push(`"${l.question}"`);else{if(e.push(`### Round ${c} (assistant)`),l.responseText&&e.push(`Response: ${l.responseText}`),l.resolutions&&l.resolutions.length>0)for(let h of l.resolutions){let y=h.finalScope??h.inferredScope,w=y?` [${y.breadth} ${y.target}]`:"";e.push(`- ${h.annotationId}: ${h.status}${w} \u2014 ${h.summary}`),h.filesModified&&h.filesModified.length>0&&e.push(` Files: ${h.filesModified.join(", ")}`)}l.toolsUsed&&l.toolsUsed.length>0&&e.push(`Tools used: ${l.toolsUsed.join(", ")}`)}e.push(""),e.push("The current round is shown in full below.")}if(n?.designModel){e.push(""),e.push("## Established Design Policies"),e.push("This project has an established design model (stored in .popmelt/model.json), extracted from the developer's previous design decisions. When making changes, follow these patterns unless the developer explicitly overrides them. When asked about design tokens, component patterns, or design decisions, reference this model as the authoritative source.");let c=n.designModel.rules;if(Array.isArray(c)&&c.length>0)if(e.push(""),e.push("Rules:"),c.length>0&&typeof c[0]=="object"&&c[0]!==null&&"scope"in c[0]){let w=new Map;for(let A of c)if(typeof A=="object"&&A!==null&&"text"in A){let D=A,M=D.scope||"general";w.has(M)||w.set(M,[]),w.get(M).push(D.text)}for(let[A,D]of w){e.push(`**${A.charAt(0).toUpperCase()+A.slice(1)}**`);for(let M of D)e.push(`- ${M}`)}}else for(let w of c)typeof w=="string"&&e.push(`- ${w}`);let l=n.designModel.tokens;l&&typeof l=="object"&&(e.push(""),e.push("Design tokens:"),e.push("```json"),e.push(JSON.stringify(l,null,2)),e.push("```"));let h=n.designModel.components;h&&typeof h=="object"&&(e.push(""),e.push("Component patterns:"),e.push("```json"),e.push(JSON.stringify(h,null,2)),e.push("```")),e.push(""),e.push("### Novel Pattern Detection"),e.push("When you make a design decision that has no matching policy in the model above (e.g., styling a component type not yet in the model, choosing a color with no token, picking spacing with no rule), flag it:"),e.push("<novel>"),e.push('[{"category":"component","element":"button","decision":"Used 8px border-radius, 12px 24px padding","reason":"No button pattern in design model"}]'),e.push("</novel>"),e.push('- `category`: "token" (color, spacing, typography), "component" (UI component pattern), or "element" (specific element style)'),e.push("- `element`: What you are styling or creating"),e.push("- `decision`: What you decided to do (specific values)"),e.push("- `reason`: Why this is novel (what is missing from the model)"),e.push("Still do the work \u2014 just flag it so the developer can review and set policy.")}if(n?.designModel||(e.push(""),e.push("## Design Context"),e.push("This project uses Popmelt for design governance. Design decisions are stored in .popmelt/decisions/ (JSON files). A materialized design model may exist at .popmelt/model.json. When the developer asks about design tokens, patterns, or past decisions, check these files first before searching source code.")),i){let c=n?.screenshotPaths??{},l=new Map;for(let h of t.annotations){let y=h.pathname||new URL(t.url).pathname;l.has(y)||l.set(y,[]),l.get(y).push(h)}e.push(""),e.push("The developer annotated multiple pages. Each page section includes its own screenshot where available.");for(let[h,y]of l){e.push(""),e.push(`## Page: ${h}`);let w=c[h];w&&n?.provider!=="codex"?e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${w}`):!w&&n?.provider!=="codex"&&(Object.keys(c).length===0?(e.push(`IMPORTANT: Use the Read tool to view the screenshot at: ${s}`),e.push("(This screenshot shows the page the developer was on when submitting \u2014 not this page)")):e.push("(No screenshot available for this page \u2014 work from the annotation text and selectors)"));let A={...t,annotations:y,styleModifications:t.styleModifications},D=Se(A,n?.imagePaths);D&&(e.push(""),e.push(D))}}else{let c=Se(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(`
|
|
202
202
|
`)}function St(s){return s.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function xt(s,t,n,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),n!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`);let i=t.find(c=>c.role==="human"&&c.feedbackContext);if(i?.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),t.length>0){o.push(""),o.push("## Conversation History");let c=0;for(let l of t)l.role==="human"?(c++,l.replyToQuestion?(o.push(`### Round ${c} (human) \u2014 reply`),o.push(`"${l.replyToQuestion}"`)):(o.push(`### Round ${c} (human)`),l.feedbackSummary&&o.push(`Annotations: ${l.feedbackSummary}`))):l.question?(o.push(`### Round ${c} (assistant) \u2014 question`),o.push(`"${l.question}"`)):(o.push(`### Round ${c} (assistant)`),l.responseText&&o.push(`Response: ${l.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let c of e)o.push(`Attached image: use the Read tool to view the image at: ${c}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
|
|
203
|
-
`)}function _n(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 An(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&&!_n(t[n]))return!1;return!0}function bt(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(An):[]}catch{return[]}}function
|
|
204
|
-
`)){if(/^\s*`model`:/.test(e)){n=!0;continue}if(n&&/^\s*`[^`]+`:/.test(e))break;if(!n)continue;let o=e.match(Bn);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:qe(i),source:"cli"})}return Ce([
|
|
205
|
-
`));return Ce([...$e,...n])}function Rt(s,t,n=5e3){return new Promise((e,o)=>{Dn(s,t,{encoding:"utf-8",timeout:n},(i,c)=>{if(i){o(i);return}e(c)})})}async function Et(s){return Nn(s,"utf-8")}function Ce(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Fn(s){return s.charAt(0).toUpperCase()+s.slice(1)}var Oe=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let c=n<0?e.events:e.events.filter(l=>l.seq>n);return{jobId:t,events:c,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,c=()=>{i||(i=!0,o())};e.on("exit",c),e.on("error",c),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(c,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Ln,readFile as zn,writeFile as Gn}from"fs/promises";import{dirname as Hn,join as Wn}from"path";var qn={version:1,threads:{}},_e=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Wn(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await zn(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={...qn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(c=>e.has(c)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let c of n)i.has(c)||o.elementIdentifiers.push(c);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let c=o.messages.find(l=>l.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:c,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Ln(Hn(this.filePath),{recursive:!0}),await Gn(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var Mt="0.6.
|
|
203
|
+
`)}function _n(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 An(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&&!_n(t[n]))return!1;return!0}function bt(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(An):[]}catch{return[]}}function kt(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 Dn}from"child_process";import{readFile as Nn}from"fs/promises";import{homedir as Me}from"os";import{join as xe}from"path";var $e=[{id:"claude-opus-4-6",label:"Opus 4.6",source:"static"},{id:"claude-sonnet-4-6",label:"Sonn 4.6",source:"static"}],be=[{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"}],ke={id:"",label:"Default",source:"cli"},Bn=/^\s*-\s+"([^"]+)"/;function Jn(s){let t=[],n=!1;for(let e of s.split(`
|
|
204
|
+
`)){if(/^\s*`model`:/.test(e)){n=!0;continue}if(n&&/^\s*`[^`]+`:/.test(e))break;if(!n)continue;let o=e.match(Bn);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:qe(i),source:"cli"})}return Ce([ke,...t])}function Un(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():qe(o);return[{id:o,label:i,source:"cli"}]})}function jn(s){let t=s.match(/\bclaude-[a-z0-9]+(?:[-.][a-z0-9]+)*\b/gi)??[];return Ce(t.map(n=>({id:n,label:qe(n),source:"cli"})))}function qe(s){if(!s)return"Default";let t={"gpt-5.5":"GPT 5.5","gpt-5.4":"GPT 5.4","gpt-5.4-mini":"Mini 5.4","gpt-5.3-codex":"Codex 5.3","gpt-5.3-codex-spark":"Spark 5.3","gpt-5.2-codex":"Codex 5.2","gpt-5.2":"GPT 5.2","gpt-5.1":"GPT 5.1","gpt-5-mini":"Mini 5","gpt-4.1":"GPT 4.1","claude-opus-4.7":"Opus 4.7","claude-opus-4.6":"Opus 4.6","claude-opus-4.6-fast":"Opus 4.6 Fast","claude-opus-4.5":"Opus 4.5","claude-sonnet-4.6":"Sonn 4.6","claude-sonnet-4.5":"Sonn 4.5","claude-sonnet-4":"Sonn 4","claude-haiku-4.5":"Haiku 4.5"},n=s.replace(/-\d{8,}$/,"");if(t[s])return t[s];if(t[n])return t[n];let e=n.match(/^claude-(opus|sonnet|haiku)-(\d+)[-.](\d+)(?:-(fast))?$/);if(e)return`${e[1]==="sonnet"?"Sonn":Fn(e[1])} ${e[2]}.${e[3]}${e[4]?" Fast":""}`;let o=n.match(/^gpt-(\d+)(?:[.-](\d+))?(?:-(mini|codex|spark))?$/);if(o){let i=o[2]?`${o[1]}.${o[2]}`:o[1];return o[3]==="mini"?`Mini ${i}`:o[3]==="codex"?`Codex ${i}`:o[3]==="spark"?`Spark ${i}`:`GPT ${i}`}return n}async function Pt(s="copilot"){let t=await Rt(s,["help","config"]),n=Jn(t);return n.length>1?n:[ke]}async function It(s="codex"){let t=await Rt(s,["debug","models"]).catch(async()=>{let e=process.env.CODEX_HOME||xe(Me(),".codex");return Et(xe(e,"models_cache.json"))}),n=Un(t);return n.length>0?Ce([...n,...be]):be}async function Tt(){let s=[xe(Me(),".claude","stats-cache.json"),xe(Me(),".claude","settings.json"),xe(Me(),".claude.json")],t=await Promise.all(s.map(e=>Et(e).catch(()=>""))),n=jn(t.join(`
|
|
205
|
+
`));return Ce([...$e,...n])}function Rt(s,t,n=5e3){return new Promise((e,o)=>{Dn(s,t,{encoding:"utf-8",timeout:n},(i,c)=>{if(i){o(i);return}e(c)})})}async function Et(s){return Nn(s,"utf-8")}function Ce(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Fn(s){return s.charAt(0).toUpperCase()+s.slice(1)}var Oe=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let c=n<0?e.events:e.events.filter(l=>l.seq>n);return{jobId:t,events:c,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,c=()=>{i||(i=!0,o())};e.on("exit",c),e.on("error",c),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(c,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Ln,readFile as zn,writeFile as Gn}from"fs/promises";import{dirname as Hn,join as Wn}from"path";var qn={version:1,threads:{}},_e=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Wn(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await zn(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={...qn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(c=>e.has(c)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let c of n)i.has(c)||o.elementIdentifiers.push(c);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let c=o.messages.find(l=>l.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:c,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Ln(Hn(this.filePath),{recursive:!0}),await Gn(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var Mt="0.6.9";var rs=1111,is=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],as=1800*1e3,cs=3600*1e3;function _t(s){if(!s)return!1;try{let t=new URL(s);return t.hostname==="localhost"||t.hostname==="127.0.0.1"}catch{return!1}}function ls(s){let t=[];for(let n of s)if(n.type==="delta"){let e=n.text;if(!e)continue;let o=t[t.length-1];o&&o.kind==="text"?o.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",o=n.file??void 0,i=n.content??void 0,c=o?o.split("/").pop()??o:void 0,l;switch(e){case"Read":l=c?`Reading ${c}`:"Reading file";break;case"Edit":l=c?`Editing ${c}`:"Editing file";break;case"Write":l=c?`Writing ${c}`:"Writing file";break;case"Bash":l=i?i.split(`
|
|
206
206
|
`)[0].trim().slice(0,60):"Running command";break;case"Glob":l="Searching files";break;case"Grep":l="Searching code";break;case"WebFetch":l="Fetching page";break;case"WebSearch":l="Searching web";break;default:l=e?`Using ${e}`:"tool";break}let h=o??i??void 0,y=t[t.length-1];y&&y.kind==="tool_group"&&y.tool===e?y.items.push({label:l,detail:h}):t.push({kind:"tool_group",tool:e,items:[{label:l,detail:h}]})}return t}function ds(s,t){let n=s.headers.origin;_t(n)&&(t.setHeader("Access-Control-Allow-Origin",n),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function b(s,t,n){s.writeHead(t,{"Content-Type":"application/json"}),s.end(JSON.stringify(n))}function us(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 Xe(s,t){try{s.res.write(`event: ${t.type}
|
|
207
207
|
data: ${JSON.stringify(t)}
|
|
208
208
|
|
|
209
|
-
`)}catch{}}function Ye(s){return s==="claude"||s==="codex"||s==="copilot"?s:void 0}async function ps(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 hs(s,t){return new Promise((n,e)=>{let o=c=>{s.removeListener("listening",i),e(c)},i=()=>{s.removeListener("error",o),n()};s.once("error",o),s.once("listening",i),s.listen(t,"127.0.0.1")})}async function Qe(s={}){let t=s.port??rs,n=s.projectRoot??process.cwd(),e=Yn("sha256").update(n).digest("hex").slice(0,12),o=s.devOrigin??(process.env.PORT?`http://localhost:${process.env.PORT}`:null),i=s.tempDir??ie(ts(),"popmelt-bridge"),c=s.maxTurns??40,l=s.maxBudgetUsd??1,h=[...s.allowedTools??is],y=s.claudePath??"claude",w=s.copilotPath??"copilot",A=s.provider??"claude",D=s.timeoutMs,M=t,f={},L={claude:y,codex:"codex",copilot:w};for(let[r,a]of Object.entries(L))try{let p=a.includes("/")?a:Xn("which",[a],{encoding:"utf-8"}).trim();f[r]={available:!0,path:p}}catch{f[r]={available:!1,path:null}}function z(r,a){return new Promise(p=>{let d=$t(a,["--version"],{stdio:["ignore","ignore","ignore"]}),m=!1,u=J=>{m||(m=!0,p(J))},
|
|
209
|
+
`)}catch{}}function Ye(s){return s==="claude"||s==="codex"||s==="copilot"?s:void 0}async function ps(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 hs(s,t){return new Promise((n,e)=>{let o=c=>{s.removeListener("listening",i),e(c)},i=()=>{s.removeListener("error",o),n()};s.once("error",o),s.once("listening",i),s.listen(t,"127.0.0.1")})}async function Qe(s={}){let t=s.port??rs,n=s.projectRoot??process.cwd(),e=Yn("sha256").update(n).digest("hex").slice(0,12),o=s.devOrigin??(process.env.PORT?`http://localhost:${process.env.PORT}`:null),i=s.tempDir??ie(ts(),"popmelt-bridge"),c=s.maxTurns??40,l=s.maxBudgetUsd??1,h=[...s.allowedTools??is],y=s.claudePath??"claude",w=s.copilotPath??"copilot",A=s.provider??"claude",D=s.timeoutMs,M=t,f={},L={claude:y,codex:"codex",copilot:w};for(let[r,a]of Object.entries(L))try{let p=a.includes("/")?a:Xn("which",[a],{encoding:"utf-8"}).trim();f[r]={available:!0,path:p}}catch{f[r]={available:!1,path:null}}function z(r,a){return new Promise(p=>{let d=$t(a,["--version"],{stdio:["ignore","ignore","ignore"]}),m=!1,u=J=>{m||(m=!0,p(J))},k=setTimeout(()=>{d.kill("SIGTERM"),u(!0)},5e3);d.on("error",()=>{clearTimeout(k),u(!1)}),d.on("close",J=>{clearTimeout(k),u(J===0)})})}await Promise.all([(async()=>{f.claude&&(f.claude.models=f.claude.available?await Tt().catch(()=>$e):$e)})(),(async()=>{f.codex&&(f.codex.models=f.codex.available&&f.codex.path?await It(f.codex.path).catch(()=>be):be)})(),(async()=>{f.copilot&&(f.copilot.models=f.copilot.available&&f.copilot.path?await Pt(f.copilot.path).catch(()=>[ke]):[ke])})()]);let[F,G,Y]=await Promise.all([Fe(n),Le(n),ze(n,f.copilot?.path??w)]);f.claude&&(f.claude.mcp=F),f.codex&&(f.codex.mcp=G),f.copilot&&(f.copilot.mcp=Y),F.found&&F.name&&h.push(`mcp__${F.name}__*`),await Qn(i,{recursive:!0}),Ot(i).catch(()=>{});let P=new Oe(1),x=new Set,T=new _e(n),S=new Ie(n),g=new Te(n,S,{claudePath:y,onEvent:r=>{for(let a of x)Xe(a,r)}}),v=20,U=300*1e3,C=[],H=new Set,R=null,V;P.addListener((r,a,p)=>{for(let d of x)(!p||!d.sourceId||d.sourceId===p)&&Xe(d,r)}),P.setProcessor(async r=>{let a=r._replyPrompt,p=r._replyImagePaths,d=r.provider??A,m;if(r.threadId){let E=await T.getThread(r.threadId);if(E){for(let $=E.messages.length-1;$>=0;$--)if(E.messages[$].sessionId){m=E.messages[$].sessionId;break}}}let u;if(m&&a){let E=(await T.getThread(r.threadId))?.messages.filter(Q=>Q.role==="human").pop();if(u=E?.replyToQuestion||E?.feedbackSummary||"",p&&p.length>0){u+=`
|
|
210
210
|
|
|
211
211
|
The developer attached reference images with their reply:`;for(let Q of p)u+=`
|
|
212
212
|
Attached image: use the Read tool to view the image at: ${Q}`}u+=`
|
|
@@ -217,11 +217,11 @@ Follow the developer's instructions. If they ask for changes, apply them to the
|
|
|
217
217
|
|
|
218
218
|
After completing work, output a <resolution> block with declaredScope and inferredScope. If unclear, output a <question> block.`+(d!=="codex"?`
|
|
219
219
|
|
|
220
|
-
IMPORTANT: First, use the Read tool to view the updated screenshot at: ${r.screenshotPath}`:"");else{let E=!a&&r.threadId?await T.getThreadHistory(r.threadId):void 0,$=a?null:await g.loadModel();u=a??vt(r.screenshotPath,r.feedback,{threadHistory:E&&E.length>0?E:void 0,provider:d,imagePaths:r.imagePaths,designModel:$??void 0,screenshotPaths:r.screenshotPaths})}let
|
|
220
|
+
IMPORTANT: First, use the Read tool to view the updated screenshot at: ${r.screenshotPath}`:"");else{let E=!a&&r.threadId?await T.getThreadHistory(r.threadId):void 0,$=a?null:await g.loadModel();u=a??vt(r.screenshotPath,r.feedback,{threadHistory:E&&E.length>0?E:void 0,provider:d,imagePaths:r.imagePaths,designModel:$??void 0,screenshotPaths:r.screenshotPaths})}let k=us(r.color,`[\u22B9 ${M}:${r.id}]`),J=r.screenshotPaths&&Object.keys(r.screenshotPaths).length>0?`${Object.keys(r.screenshotPaths).length} pages [${Object.keys(r.screenshotPaths).join(", ")}]`:r.screenshotPath;console.log(`${k} Reviewing ${J} (provider: ${d})${r.threadId?` (thread: ${r.threadId})`:""}${m?` (resuming: ${m.slice(0,8)})`:""}`);let j=(E,$)=>{E.type==="delta"&&"text"in E?P.accumulateText($,"response",E.text):E.type==="thinking"&&"text"in E&&P.accumulateText($,"thinking",E.text),P.broadcast(E,$,r.sourceId)},{process:Z,result:W}=d==="codex"?tt(r.id,{prompt:u,projectRoot:n,screenshotPath:r.screenshotPath,resumeSessionId:m,model:r.model,onEvent:j}):d==="copilot"?st(r.id,{prompt:u,projectRoot:n,resumeSessionId:m,model:r.model,timeoutMs:D,copilotPath:f.copilot?.path??w,onEvent:j}):me(r.id,{prompt:u,projectRoot:n,maxTurns:c,maxBudgetUsd:l,allowedTools:h,claudePath:y,resumeSessionId:m,model:r.model,timeoutMs:D,onEvent:j});P.setActiveProcess(r.id,Z);let I=await W;if(r.result=I.text,I.success){console.log(`${k} Iteration complete`),I.fileEdits&&I.fileEdits.length>0&&console.log(`${k} Captured ${I.fileEdits.length} file edit(s): ${I.fileEdits.map(B=>`${B.tool} ${B.file_path}`).join(", ")}`),r.status="done";let E=St(I.text),$=bt(I.text);if($.length>0&&r.annotationIds&&r.annotationIds.length>0){let B=new Set(r.annotationIds);$.every(N=>B.has(N.annotationId))||($=$.map((N,K)=>({...N,annotationId:r.annotationIds[K%r.annotationIds.length]})))}let Q=I.fileEdits&&I.fileEdits.length>0?I.fileEdits.map(B=>`${B.tool} ${B.file_path.split("/").pop()}`):I.toolsUsed,te=P.getBufferedEvents(r.id),ce=te?ls(te.events):void 0;if(r.threadId&&await T.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,responseText:I.text,resolutions:$.length>0?$:void 0,question:E??void 0,sessionId:I.sessionId,toolsUsed:Q,segments:ce&&ce.length>0?ce:void 0,model:r.model,provider:d}),S.captureGitDiff(n).then(async B=>{let _=Date.now(),N=r.imagePaths?Object.values(r.imagePaths).flat():[],K=[];if(r.imagePaths)for(let[ue,O]of Object.entries(r.imagePaths))for(let ee=0;ee<O.length;ee++)K.push(`screenshots/p-${r.id}-${ue}-${ee}.webp`);await S.persist({version:1,id:r.id,createdAt:r.createdAt,completedAt:_,durationMs:_-r.createdAt,url:r.feedback.url,viewport:r.feedback.viewport,screenshotPath:`screenshots/s-${r.id}.webp`,pastedImagePaths:K,annotations:r.feedback.annotations,styleModifications:r.feedback.styleModifications,inspectedElement:r.feedback.inspectedElement,provider:d,model:r.model,sessionId:I.sessionId,threadId:r.threadId,responseText:I.text,resolutions:$.length>0?$:[],question:E??void 0,fileEdits:I.fileEdits??[],toolsUsed:Q,gitDiff:B},r.screenshotPath,N)}).catch(()=>{}),$.length>0){let B=$.some(_=>(_.finalScope??_.inferredScope)?.breadth==="pattern");B&&d!=="copilot"?g.run().catch(()=>{}):B&&console.log(`${k} Skipping materialization for Copilot provider`)}if(r.kind==="synthesize"&&I.text){let B=Re(I.text);if(B){Array.isArray(B.rules)&&(B.rules=we(B.rules));let _=await g.loadModel();_&&(!B.tokens&&_.tokens&&(B.tokens=_.tokens),!B.components&&_.components&&(B.components=_.components)),await g.writeModel(B),console.log(`${k} Synthesize: model.json updated`)}}E&&(console.log(`${k} \u{1F4AC} Question detected: "${E.slice(0,120)}" \u2192 broadcasting to ${x.size} SSE clients (threadId=${r.threadId??r.id}, annotationIds=${r.annotationIds?.join(",")??"none"})`),P.broadcast({type:"question",jobId:r.id,threadId:r.threadId??r.id,question:E,annotationIds:r.annotationIds},r.id,r.sourceId));let oe=kt(I.text);oe.length>0&&(console.log(`${k} Novel pattern(s): ${oe.map(B=>`${B.category}/${B.element}`).join(", ")}`),P.broadcast({type:"novel_patterns",jobId:r.id,patterns:oe,threadId:r.threadId},r.id,r.sourceId)),P.broadcast({type:"done",jobId:r.id,success:!0,resolutions:$.length>0?$:void 0,responseText:I.text,threadId:r.threadId},r.id,r.sourceId),C.push({id:r.id,status:"done",completedAt:Date.now(),threadId:r.threadId,annotationIds:r.annotationIds})}else console.error(`${k} Error: ${I.error}`),r.status="error",r.error=I.error,r.threadId&&await T.appendMessage(r.threadId,{role:"assistant",timestamp:Date.now(),jobId:r.id,error:I.error||"Unknown error",model:r.model,provider:d}),P.broadcast({type:"error",jobId:r.id,threadId:r.threadId,message:I.error||"Unknown error",provider:d,model:r.model,recoverable:I.recoverableError,code:I.errorCode},r.id,r.sourceId),C.push({id:r.id,status:"error",completedAt:Date.now(),error:I.error,threadId:r.threadId,annotationIds:r.annotationIds});let q=Date.now()-U;for(;C.length>0&&(C[0].completedAt<q||C.length>v);)C.shift()});let ae=es(async(r,a)=>{if(ds(r,a),r.method==="OPTIONS"){a.writeHead(204),a.end();return}let p=r.url||"/",d=p.split("?")[0],m=new URL(p,`http://127.0.0.1:${M}`),u=m.pathname;try{if(r.method==="POST"&&u==="/send")await At(r,a);else if(r.method==="GET"&&u==="/events")Nt(r,a);else if(r.method==="GET"&&u==="/status")Bt(a);else if(r.method==="PATCH"&&u==="/config")await Jt(r,a);else if(r.method==="POST"&&u==="/shutdown")b(a,200,{ok:!0}),setTimeout(()=>process.exit(0),100);else if(r.method==="POST"&&u==="/restart")b(a,200,{ok:!0,restarting:!0}),setTimeout(()=>{for(let k of x)try{k.res.end()}catch{}ae.close(()=>{$t(process.execPath,process.argv.slice(1),{detached:!0,stdio:"ignore",cwd:process.cwd(),env:process.env}).unref(),setTimeout(()=>process.exit(0),200)})},100);else if(r.method==="GET"&&u==="/capabilities")b(a,200,{providers:f});else if(r.method==="POST"&&u==="/mcp/install")await Lt(r,a);else if(r.method==="POST"&&u==="/reply")await Dt(r,a);else if(r.method==="POST"&&u==="/cancel")Ut(r,a);else if(r.method==="POST"&&u==="/materialize")await jt(a);else if(r.method==="POST"&&u==="/model/component")await zt(r,a);else if(r.method==="DELETE"&&u==="/model/component")await Gt(r,a);else if(r.method==="PATCH"&&u==="/model/token")await Ht(r,a);else if(r.method==="DELETE"&&u==="/model/token")await Wt(r,a);else if(r.method==="POST"&&u==="/model/consolidate")g.consolidate().catch(k=>console.error("[Bridge] Consolidation error:",k)),b(a,200,{started:!0});else if(r.method==="POST"&&u==="/model/synthesize")await Ft(r,a);else if(r.method==="GET"&&u==="/model"){let k=await g.loadModel();b(a,200,{model:k})}else if(r.method==="GET"&&u.startsWith("/jobs/")&&u.endsWith("/events")){let k=u.slice(6,u.length-7),J=parseInt(m.searchParams.get("afterSeq")??"-1",10),j=P.getBufferedEvents(k,isNaN(J)?-1:J);j?b(a,200,j):b(a,404,{error:"Unknown job"})}else if(r.method==="GET"&&d.startsWith("/files/"))await Qt(d.slice(7),a);else if(r.method==="GET"&&u==="/threads/recent"){let k=Math.min(Math.max(parseInt(m.searchParams.get("limit")??"5",10)||5,1),20),J=await T.listRecent(k);b(a,200,J)}else if(r.method==="GET"&&u.startsWith("/thread/")){let k=u.slice(8);await Kt(k,a)}else r.method==="GET"&&(u==="/canvas"||u==="/canvas/")?qt(r,a):r.method==="GET"&&u==="/canvas/manifest"?await Xt(a):r.method==="GET"&&u==="/canvas/app.mjs"?await Yt(a):b(a,404,{error:"Not found"})}catch(k){console.error("[Bridge] Request error:",k),b(a,500,{error:k instanceof Error?k.message:"Internal error"})}});async function At(r,a){let{screenshot:p,feedback:d,color:m,provider:u,model:k,sourceId:J,pastedImages:j,pageScreenshots:Z}=await We(r),W;try{W=JSON.parse(d)}catch{b(a,400,{error:"Invalid feedback JSON"});return}let I=Ae().slice(0,8),q={};if(Z.length>0)for(let O of Z){let ee=encodeURIComponent(O.pathname),le=ie(i,`screenshot-${I}-${ee}.webp`);await De(le,O.data),q[O.pathname]=le}let E=ie(i,`screenshot-${I}.webp`);await De(E,p);let $={};if(j.length>0)for(let O of j){let ee=ie(i,`pasted-${I}-${O.annotationId}-${O.index}.webp`);await De(ee,O.data),$[O.annotationId]||($[O.annotationId]=[]),$[O.annotationId].push(ee)}let Q=W.annotations.map(O=>O.linkedSelector?O.pathname?`${O.pathname}:${O.linkedSelector}`:O.linkedSelector:null).filter(O=>!!O),te;if(Q.length>0){let O=await T.findContinuationThread(Q);O?(te=O.id,await T.addElementIdentifiers(te,Q)):te=(await T.createThread(I,Q)).id}else te=(await T.createThread(I,[])).id;let ce=W.annotations.map(O=>O.id),oe=Object.keys(q).length>0,B={id:I,status:"queued",screenshotPath:E,feedback:W,createdAt:Date.now(),color:m,threadId:te,annotationIds:ce,provider:Ye(u),model:k||void 0,...Object.keys($).length>0?{imagePaths:$}:{},sourceId:J||void 0,...oe?{screenshotPaths:q}:{}},_=new Set(W.annotations.map(O=>O.pathname).filter(Boolean)),N;if(_.size>1){let O=new Map;for(let ee of W.annotations){let le=ee.pathname||"(unknown)";O.has(le)||O.set(le,[]),O.get(le).push(ee.instruction||`[${ee.type}]`)}N=[...O.entries()].map(([ee,le])=>`\`${ee}\`
|
|
221
221
|
${le.map(Zt=>`- ${Zt}`).join(`
|
|
222
222
|
`)}`).join(`
|
|
223
|
-
`)}else N=W.annotations.map(O=>O.instruction||`[${O.type}]`).join("; ");let K=Se(W,Object.keys($).length>0?$:void 0);await T.appendMessage(te,{role:"human",timestamp:Date.now(),jobId:I,screenshotPath:E,...oe?{screenshotPaths:q}:{},...Object.keys($).length>0?{imagePaths:$}:{},annotationIds:ce,feedbackSummary:N,feedbackContext:K||void 0});let ue=
|
|
223
|
+
`)}else N=W.annotations.map(O=>O.instruction||`[${O.type}]`).join("; ");let K=Se(W,Object.keys($).length>0?$:void 0);await T.appendMessage(te,{role:"human",timestamp:Date.now(),jobId:I,screenshotPath:E,...oe?{screenshotPaths:q}:{},...Object.keys($).length>0?{imagePaths:$}:{},annotationIds:ce,feedbackSummary:N,feedbackContext:K||void 0});let ue=P.enqueue(B);b(a,200,{jobId:I,position:ue,threadId:te})}async function Dt(r,a){let p=r.headers["content-type"]||"",d,m,u,k,J,j,Z=[];if(p.includes("multipart/form-data")){let _=await We(r),N=_.feedback?JSON.parse(_.feedback):{};d=N.threadId,m=N.reply,u=N.color,k=N.provider,J=N.model,j=N.sourceId||_.sourceId;for(let K of _.pastedImages)Z.push(K.data)}else{let _=[];for await(let ue of r)_.push(typeof ue=="string"?Buffer.from(ue):ue);let N=Buffer.concat(_).toString("utf-8"),K;try{K=JSON.parse(N)}catch{b(a,400,{error:"Invalid JSON"});return}d=K.threadId,m=K.reply,u=K.color,k=K.provider,J=K.model,j=K.sourceId}if(!d||!m){b(a,400,{error:"Missing threadId or reply"});return}if(!await T.getThread(d)){b(a,404,{error:"Thread not found"});return}let I=Ae().slice(0,8),q=[];for(let _=0;_<Z.length;_++){let N=ie(i,`reply-${I}-${_}.webp`);await De(N,Z[_]),q.push(N)}let E="";{let _=await T.getThreadHistory(d);for(let N=_.length-1;N>=0;N--)if(_[N].screenshotPath){E=_[N].screenshotPath;break}}if(!E&&!H.has(d)){b(a,400,{error:"No screenshot available"});return}await T.appendMessage(d,{role:"human",timestamp:Date.now(),jobId:I,replyToQuestion:m,screenshotPath:E,...q.length>0?{replyImagePaths:q}:{}});let $=await T.getThreadHistory(d),Q=[];for(let _ of $)if(_.annotationIds)for(let N of _.annotationIds)Q.includes(N)||Q.push(N);let te=Ye(k),ce=xt(E,$,te,q.length>0?q:void 0),oe={id:I,status:"queued",screenshotPath:E,feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),color:u,threadId:d,annotationIds:Q.length>0?Q:void 0,provider:te,model:J||void 0,sourceId:j||void 0};H.has(d)&&(oe.kind="synthesize"),oe._replyPrompt=ce,q.length>0&&(oe._replyImagePaths=q);let B=P.enqueue(oe);b(a,200,{jobId:I,position:B,threadId:d})}function Nt(r,a){a.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),a.write(`event: connected
|
|
224
224
|
data: {"status":"connected"}
|
|
225
225
|
|
|
226
|
-
`),r.headers.origin&&_t(r.headers.origin)&&o!==r.headers.origin&&(o=r.headers.origin);let d=new URL(r.url||"/",`http://127.0.0.1:${M}`).searchParams.get("sourceId")||void 0,m={id:Ae().slice(0,8),res:a,sourceId:d};x.add(m),r.on("close",()=>{x.delete(m)})}function Bt(r){let a=
|
|
226
|
+
`),r.headers.origin&&_t(r.headers.origin)&&o!==r.headers.origin&&(o=r.headers.origin);let d=new URL(r.url||"/",`http://127.0.0.1:${M}`).searchParams.get("sourceId")||void 0,m={id:Ae().slice(0,8),res:a,sourceId:d};x.add(m),r.on("close",()=>{x.delete(m)})}function Bt(r){let a=P.allActive;b(r,200,{ok:!0,version:Mt,projectId:e,devOrigin:o,activeJob:a[0]?{id:a[0].id,status:a[0].status}:null,activeJobs:a.map(p=>({id:p.id,status:p.status,threadId:p.threadId,annotationIds:p.annotationIds,color:p.color})),queueDepth:P.depth,recentJobs:C})}async function Jt(r,a){let p=await new Promise(d=>{let m="";r.on("data",u=>{m+=u.toString()}),r.on("end",()=>d(m))});try{let d=JSON.parse(p);typeof d.devOrigin=="string"&&(o=d.devOrigin||null),b(a,200,{ok:!0,devOrigin:o})}catch{b(a,400,{error:"Invalid JSON"})}}async function Ut(r,a){let d=new URL(r.url||"/",`http://127.0.0.1:${M}`).searchParams.get("jobId"),u=(d?P.allActive.filter(J=>J.id===d):P.allActive).map(J=>J.threadId).filter(Boolean),k=d?P.cancelJob(d):P.cancelActive();for(let J of u)await T.appendMessage(J,{role:"assistant",timestamp:Date.now(),jobId:d||"",cancelled:!0});b(a,200,{cancelled:k})}async function jt(r){if(g.isRunning){b(r,200,{skipped:!0,reason:"Already running"});return}let a=await g.getUnmaterializedPatternDecisions();if(a.length===0){b(r,200,{skipped:!0,reason:"No unmaterialized pattern decisions"});return}g.run().catch(()=>{}),b(r,200,{started:!0,decisionCount:a.length,decisionIds:a.map(p=>p.id)})}async function Ft(r,a){let p=await g.loadModel();if(!p){b(a,400,{error:"No model exists yet"});return}let d,m;try{let I=[];for await(let E of r)I.push(typeof E=="string"?Buffer.from(E):E);let q=JSON.parse(Buffer.concat(I).toString());d=q.provider,m=q.model}catch{}if(d==="copilot"){b(a,400,{error:"Rule synthesis is not available for Copilot yet."});return}let u=at(p),k=Ae().slice(0,8),j=(await T.createThread(k,[])).id;H.add(j),await T.appendMessage(j,{role:"human",timestamp:Date.now(),jobId:k,feedbackSummary:"Synthesize rules \u2014 review and propose improvements"});let Z={id:k,status:"queued",screenshotPath:"",feedback:{timestamp:new Date().toISOString(),url:"",viewport:{width:0,height:0},scrollPosition:{x:0,y:0},annotations:[],styleModifications:[]},createdAt:Date.now(),threadId:j,kind:"synthesize",provider:Ye(d),model:m||void 0};Z._replyPrompt=u;let W=P.enqueue(Z);b(a,200,{jobId:k,position:W,threadId:j})}async function Lt(r,a){let p=[];for await(let j of r)p.push(typeof j=="string"?Buffer.from(j):j);let d;if(p.length>0)try{d=JSON.parse(Buffer.concat(p).toString("utf-8")).serverUrl}catch{}let m=[];f.claude?.available&&f.claude.mcp&&!f.claude.mcp.found&&m.push(await gt(d)),f.codex?.available&&f.codex.mcp&&!f.codex.mcp.found&&m.push(await yt(d)),f.copilot?.available&&f.copilot.mcp&&!f.copilot.mcp.found&&m.push(await wt(d,f.copilot.path??w));let[u,k,J]=await Promise.all([Fe(n),Le(n),ze(n,f.copilot?.path??w)]);f.claude&&(f.claude.mcp=u),f.codex&&(f.codex.mcp=k),f.copilot&&(f.copilot.mcp=J),b(a,200,{results:m,capabilities:{providers:f}})}async function zt(r,a){let p=[];for await(let u of r)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{b(a,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){b(a,400,{error:"Missing or invalid name"});return}let m=await g.addComponent(d.name);b(a,200,m)}async function Gt(r,a){let p=[];for await(let u of r)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{b(a,400,{error:"Invalid JSON"});return}if(!d.name||typeof d.name!="string"){b(a,400,{error:"Missing or invalid name"});return}let m=await g.removeComponent(d.name);b(a,200,m)}async function Ht(r,a){let p=[];for await(let u of r)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{b(a,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"||typeof d.value!="string"){b(a,400,{error:"Missing or invalid path/value"});return}let m=await g.updateToken(d.path,d.value);b(a,200,m)}async function Wt(r,a){let p=[];for await(let u of r)p.push(typeof u=="string"?Buffer.from(u):u);let d;try{d=JSON.parse(Buffer.concat(p).toString("utf-8"))}catch{b(a,400,{error:"Invalid JSON"});return}if(!d.path||typeof d.path!="string"){b(a,400,{error:"Missing or invalid path"});return}let m=await g.removeToken(d.path);b(a,200,m)}function qt(r,a){let p=o??"http://localhost:3000";if(!o){let m=r.headers.referer||r.headers.origin;if(m)try{let u=new URL(typeof m=="string"?m:m[0]||"");(u.hostname==="localhost"||u.hostname==="127.0.0.1")&&(p=u.origin)}catch{}}let d=Ze(M,p);a.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),a.end(d)}async function Xt(r){let a=Date.now();if(R&&a<R.expires){b(r,200,R.data);return}try{let{scanForComponents:p}=await import("./react-scanner-ZXYS5M3Y.mjs"),{generateRenderFiles:d}=await import("./render-generator-EANIDD2E.mjs"),m=await p(n);R={data:m,expires:a+5e3},d(m,n,V).then(u=>{V=u}).catch(u=>console.warn("[Bridge] Render generation failed:",u)),b(r,200,m)}catch(p){console.error("[Bridge] Scanner error:",p),b(r,500,{error:"Failed to scan components"})}}async function Yt(r){let a=[ie(n,"node_modules","@popmelt.com","core","dist","canvas.mjs"),ie(n,"packages","popmelt","dist","canvas.mjs")];try{let p=os(import.meta.url);a.unshift(ie(ss(p),"canvas.mjs"))}catch{}for(let p of a)try{let d=await Ct(p,"utf-8");r.writeHead(200,{"Content-Type":"application/javascript; charset=utf-8","Access-Control-Allow-Origin":"*"}),r.end(d);return}catch{}console.error("[Bridge] Canvas bundle not found in:",a),b(r,404,{error:"Canvas bundle not found"})}async function Qt(r,a){if(!r||r.includes("/")||r.includes("\\")||r.includes("..")){b(a,400,{error:"Invalid filename"});return}try{let p=await Ct(ie(i,r)),d=r.split(".").pop()?.toLowerCase(),m=d==="png"?"image/png":d==="webp"?"image/webp":d==="jpg"||d==="jpeg"?"image/jpeg":"application/octet-stream";a.writeHead(200,{"Content-Type":m,"Cache-Control":"public, max-age=3600"}),a.end(p)}catch{b(a,404,{error:"File not found"})}}function Pe(r){return`/files/${ns(r)}`}async function Kt(r,a){let p=await T.getThread(r);if(!p){b(a,404,{error:"Thread not found"});return}let d=p.messages.map(({screenshotPath:m,screenshotPaths:u,imagePaths:k,replyImagePaths:J,...j})=>({...j,...m?{screenshotUrl:Pe(m)}:{},...u?{screenshotUrls:Object.fromEntries(Object.entries(u).map(([Z,W])=>[Z,Pe(W)]))}:{},...k?{imageUrls:Object.fromEntries(Object.entries(k).map(([Z,W])=>[Z,W.map(Pe)]))}:{},...J?{replyImageUrls:J.map(Pe)}:{}}));b(a,200,{id:p.id,createdAt:p.createdAt,messages:d})}let Ke=9,Ve=!1;for(let r=t;r<t+Ke;r++)try{await hs(ae,r),M=r,Ve=!0,console.log(`[\u22B9 is watching :${M}]`);break}catch(a){if(a.code==="EADDRINUSE"){let p=await ps(r);if(p&&p.projectId===e)return console.log(`[\u22B9 already watching :${r}]`),{port:r,projectId:e,close:async()=>{}};continue}throw a}if(!Ve)throw new Error(`[Bridge] All ports ${t}\u2013${t+Ke-1} in use`);for(let[r,a]of Object.entries(f))!a.available||!a.path||z(r,a.path).then(p=>{if(p)console.log(`[Bridge] ${r} warmed up`);else{console.warn(`[Bridge] ${r} warm-up failed \u2014 marking unavailable`),a.available=!1,a.path=null;for(let d of x)Xe(d,{type:"capabilities_changed",data:{}})}});let Vt=setInterval(()=>{Ot(i).catch(()=>{})},as);return{port:M,projectId:e,close:async()=>{clearInterval(Vt),await P.destroyAsync();for(let r of x)try{r.res.end()}catch{}return x.clear(),new Promise(r=>{ae.close(()=>r())})}}}async function Ot(s){try{let t=await Kn(s),n=Date.now();for(let e of t){let o=ie(s,e);try{let i=await Vn(o);n-i.mtimeMs>cs&&await Zn(o)}catch{}}}catch{}}var se="\x1B[35m[popmelt]\x1B[0m";async function ws(){let s=process.argv.slice(2);if(s[0]==="wrap"){let t=s.indexOf("--");(t===-1||t===s.length-1)&&(console.error(`${se} Usage: popmelt wrap -- <dev command>`),console.error(`${se} Example: popmelt wrap -- next dev`),console.error(`${se} Example: popmelt wrap -- astro dev`),process.exit(1));let n=s.slice(t+1);await xs(n);return}if(s[0]==="bridge"){await vs();return}if(s[0]==="stop"){await Ss();return}console.log(`${se} Popmelt \u2014 design collaboration for AI coding agents`),console.log(""),console.log(" popmelt wrap -- <command> Start bridge + dev server together"),console.log(" popmelt bridge Start the bridge server standalone"),console.log(" popmelt stop Stop a detached bridge"),console.log(""),console.log("Examples:"),console.log(" popmelt wrap -- next dev"),console.log(" popmelt wrap -- astro dev"),console.log(" popmelt wrap -- vite"),console.log(""),console.log("In package.json:"),console.log(' "scripts": { "dev": "popmelt wrap -- next dev" }')}async function vs(){let s=await Qe({projectRoot:process.cwd()});console.log(`${se} Bridge running on http://localhost:${s.port}`),await new Promise(t=>{let n=async()=>{console.log(`
|
|
227
227
|
${se} Shutting down bridge...`),await s.close(),t()};process.on("SIGINT",n),process.on("SIGTERM",n)})}async function Ss(){let s=ys(process.cwd(),".popmelt","bridge.lock"),t;try{t=JSON.parse(await ms(s,"utf8"))}catch{console.log(`${se} No bridge running (no .popmelt/bridge.lock found)`);return}try{process.kill(t.pid,"SIGTERM"),console.log(`${se} Sent SIGTERM to bridge (pid ${t.pid}, port ${t.port})`)}catch(n){if(n.code==="ESRCH")console.log(`${se} Bridge process ${t.pid} already dead`);else throw n}try{await gs(s)}catch{}}async function xs(s){let t=await Qe({projectRoot:process.cwd()});console.log(`${se} Bridge running on http://localhost:${t.port}`);let[n,...e]=s;console.log(`${se} Starting: ${s.join(" ")}`);let o=fs(n,e,{stdio:"inherit",shell:!0,env:{...process.env,POPMELT_BRIDGE_URL:`http://localhost:${t.port}`}}),i=c=>{o.kill(c)};process.on("SIGINT",()=>i("SIGINT")),process.on("SIGTERM",()=>i("SIGTERM")),o.on("exit",async(c,l)=>{await t.close(),l?process.kill(process.pid,l):process.exit(c??0)})}ws().catch(s=>{console.error(`${se} Fatal:`,s),process.exit(1)});
|
package/dist/plugin-astro.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{request as P}from"http";function y(c){let d=c?.basePath??"/popmelt",h=c?.bridge!==!1,n=null,u=null;return{name:"popmelt",hooks:{async"astro:server:setup"({server:g}){if(!h||process.env.POPMELT_BRIDGE_URL){if(process.env.POPMELT_BRIDGE_URL)try{n=new URL(process.env.POPMELT_BRIDGE_URL).port?parseInt(new URL(process.env.POPMELT_BRIDGE_URL).port,10):null}catch{}}else try{let{startPopmelt:o}=await import("./server-
|
|
1
|
+
import{request as P}from"http";function y(c){let d=c?.basePath??"/popmelt",h=c?.bridge!==!1,n=null,u=null;return{name:"popmelt",hooks:{async"astro:server:setup"({server:g}){if(!h||process.env.POPMELT_BRIDGE_URL){if(process.env.POPMELT_BRIDGE_URL)try{n=new URL(process.env.POPMELT_BRIDGE_URL).port?parseInt(new URL(process.env.POPMELT_BRIDGE_URL).port,10):null}catch{}}else try{let{startPopmelt:o}=await import("./server-55K3KMLN.mjs"),t=g.config,i=t?.server?.host===!0?"0.0.0.0":t?.server?.host||"localhost",s=t?.server?.port??4321,l=`http://${i==="0.0.0.0"?"localhost":i}:${s}`,a=await o({port:c?.port,projectRoot:c?.projectRoot,devOrigin:l,force:!0,detached:!0});n=a.port,u=a.close,console.log(`[popmelt] bridge ready at http://localhost:${n}`)}catch(o){console.warn("[popmelt] bridge failed to start:",o.message??o)}g.middlewares.use((o,t,i)=>{let s=o.url||"";if(s.startsWith(d)){if(n===null)return i();let l=`http://localhost:${n}`,a="/canvas"+s.slice(d.length),r=new URL(a,l),e=P(r,{method:o.method,headers:{...o.headers,host:r.host}},p=>{t.writeHead(p.statusCode||502,p.headers),p.pipe(t)});e.on("error",()=>{t.writeHead(502,{"Content-Type":"text/plain"}),t.end(`Popmelt bridge not running on port ${n}`)}),o.pipe(e);return}if(n!==null&&(o.headers.accept?.includes("text/html")||s==="/"||s.endsWith(".html"))){let l=t.write,a=t.end,r="";t.write=function(e,...p){return r+=typeof e=="string"?e:e.toString("utf8"),!0},t.end=function(e,...p){e&&(r+=typeof e=="string"?e:e.toString("utf8"));let m=`<script type="text/javascript">window.__POPMELT_BRIDGE_URL__="http://localhost:${n}";</script>`;r=r.replace("<head>",`<head>${m}`),t.getHeader("content-length")&&t.setHeader("content-length",Buffer.byteLength(r)),l.call(t,r,"utf8"),a.call(t)}}i()})}}}}export{y as popmelt};
|
package/dist/plugin-vite.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{request as m}from"http";function f(n){let c=n?.basePath??"/popmelt",u=n?.bridge!==!1,e=null,h=null;return{name:"popmelt",async configureServer(p){if(!u||process.env.POPMELT_BRIDGE_URL){if(process.env.POPMELT_BRIDGE_URL)try{e=new URL(process.env.POPMELT_BRIDGE_URL).port?parseInt(new URL(process.env.POPMELT_BRIDGE_URL).port,10):null}catch{}}else try{let{startPopmelt:t}=await import("./server-
|
|
1
|
+
import{request as m}from"http";function f(n){let c=n?.basePath??"/popmelt",u=n?.bridge!==!1,e=null,h=null;return{name:"popmelt",async configureServer(p){if(!u||process.env.POPMELT_BRIDGE_URL){if(process.env.POPMELT_BRIDGE_URL)try{e=new URL(process.env.POPMELT_BRIDGE_URL).port?parseInt(new URL(process.env.POPMELT_BRIDGE_URL).port,10):null}catch{}}else try{let{startPopmelt:t}=await import("./server-55K3KMLN.mjs"),r=p.config,o=r?.server?.host===!0?"0.0.0.0":r?.server?.host||"localhost",s=r?.server?.port??5173,i=`http://${o==="0.0.0.0"?"localhost":o}:${s}`,a=await t({port:n?.port,projectRoot:n?.projectRoot,devOrigin:i,force:!0,detached:!0});e=a.port,h=a.close,console.log(`[popmelt] bridge ready at http://localhost:${e}`)}catch(t){console.warn("[popmelt] bridge failed to start:",t.message??t)}p.middlewares.use((t,r,o)=>{let s=t.url||"";if(!s.startsWith(c)||e===null)return o();let i=`http://localhost:${e}`,a="/canvas"+s.slice(c.length),d=new URL(a,i),g=m(d,{method:t.method,headers:{...t.headers,host:d.host}},l=>{r.writeHead(l.statusCode||502,l.headers),l.pipe(r)});g.on("error",()=>{r.writeHead(502,{"Content-Type":"text/plain"}),r.end(`Popmelt bridge not running on port ${e}`)}),t.pipe(g)})},transformIndexHtml(){if(e!==null)return[{tag:"script",attrs:{type:"text/javascript"},children:`window.__POPMELT_BRIDGE_URL__="http://localhost:${e}";`,injectTo:"head-prepend"}]}}}export{f as popmelt};
|
|
@@ -26,7 +26,7 @@ import{spawn as hs}from"child_process";import{createHash as fs}from"crypto";impo
|
|
|
26
26
|
</script>
|
|
27
27
|
</body>
|
|
28
28
|
</html>`}import{spawn as en}from"child_process";import{createInterface as tn}from"readline";var nn=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"]),et=1e5;function sn(s){let t=s.input?.file_path||s.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!nn.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>et?e.slice(0,et)+`
|
|
29
|
-
\u2026[truncated]`:e}function fe(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:a=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:p,model:g,timeoutMs:w=3e5,onEvent:A}=t,D=[];p?D.push("--resume",p,"-p",n):D.push("-p",n),D.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),g&&D.push("--model",g);for(let F of a)D.push("--allowedTools",F);let M=en(c,D,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),f=new Promise(F=>{let z,L=[],H=[],Y=!1,k="",x=!1,I=setTimeout(()=>{x=!0,M.kill("SIGTERM"),setTimeout(()=>{try{M.kill("SIGKILL")}catch{}},5e3)},w),S=tn({input:M.stdout}),y=new Set;S.on("line",U=>{if(U.trim())try{let O=JSON.parse(U);O.session_id&&!z&&(z=O.session_id);let G=O.type??(O.event?.type?`event.${O.event.type}`:"unknown");if(y.add(G),O.type==="result"&&O.result&&L.length===0){let R=typeof O.result=="string"?O.result:"";R&&(L.push(R),A?.({type:"delta",jobId:s,text:R},s))}if(O.type==="assistant"&&Array.isArray(O.message?.content))for(let R of O.message.content){if(R.type==="text"&&R.text&&(L.push(R.text),A?.({type:"delta",jobId:s,text:R.text},s)),R.type==="tool_use"&&R.name){let K=R.input?.file_path||R.input?.path||void 0,ie=sn(R);A?.({type:"tool_use",jobId:s,tool:R.name,...K?{file:K}:{},...ie?{content:ie}:{}},s),R.name==="Edit"&&R.input?.file_path?H.push({tool:"Edit",file_path:R.input.file_path,old_string:R.input.old_string,new_string:R.input.new_string,replace_all:R.input.replace_all}):R.name==="Write"&&R.input?.file_path&&H.push({tool:"Write",file_path:R.input.file_path,content:R.input.content})}R.type==="thinking"&&R.thinking&&A?.({type:"thinking",jobId:s,text:R.thinking},s)}O.type==="user"&&O.tool_use_result?.file?.filePath&&A?.({type:"tool_use",jobId:s,tool:"Read",file:O.tool_use_result.file.filePath},s)}catch{}});let v=[];M.stderr?.on("data",U=>{v.push(U.toString())}),M.on("close",U=>{if(clearTimeout(I),S.close(),L.length===0&&y.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...y].join(", ")}`),x)Y=!0,k=`Timed out after ${Math.round(w/6e4)} minutes`;else if(U!==0&&U!==null){Y=!0;let O=v.join("").trim(),G=L.length===0&&y.size>0?` (no text captured, event types: ${[...y].join(", ")})`:"";k=O||`Claude process exited with code ${U}${G}`}F({sessionId:z,text:L.join(""),success:!Y,error:Y?k:void 0,fileEdits:H.length>0?H:void 0})}),M.on("error",U=>{clearTimeout(I),Y=!0,k=U.message,F({sessionId:z,text:L.join(""),success:!1,error:k,fileEdits:H.length>0?H:void 0})})});return{process:M,result:f}}import{spawn as on}from"child_process";import{createInterface as rn}from"readline";function an(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}function tt(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:a,onEvent:c}=t,p=[];i?(p.push("exec","resume",
|
|
29
|
+
\u2026[truncated]`:e}function fe(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:a=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:p,model:g,timeoutMs:w=3e5,onEvent:A}=t,D=[];p?D.push("--resume",p,"-p",n):D.push("-p",n),D.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),g&&D.push("--model",g);for(let F of a)D.push("--allowedTools",F);let M=en(c,D,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),f=new Promise(F=>{let z,L=[],H=[],Y=!1,k="",x=!1,I=setTimeout(()=>{x=!0,M.kill("SIGTERM"),setTimeout(()=>{try{M.kill("SIGKILL")}catch{}},5e3)},w),S=tn({input:M.stdout}),y=new Set;S.on("line",U=>{if(U.trim())try{let O=JSON.parse(U);O.session_id&&!z&&(z=O.session_id);let G=O.type??(O.event?.type?`event.${O.event.type}`:"unknown");if(y.add(G),O.type==="result"&&O.result&&L.length===0){let R=typeof O.result=="string"?O.result:"";R&&(L.push(R),A?.({type:"delta",jobId:s,text:R},s))}if(O.type==="assistant"&&Array.isArray(O.message?.content))for(let R of O.message.content){if(R.type==="text"&&R.text&&(L.push(R.text),A?.({type:"delta",jobId:s,text:R.text},s)),R.type==="tool_use"&&R.name){let K=R.input?.file_path||R.input?.path||void 0,ie=sn(R);A?.({type:"tool_use",jobId:s,tool:R.name,...K?{file:K}:{},...ie?{content:ie}:{}},s),R.name==="Edit"&&R.input?.file_path?H.push({tool:"Edit",file_path:R.input.file_path,old_string:R.input.old_string,new_string:R.input.new_string,replace_all:R.input.replace_all}):R.name==="Write"&&R.input?.file_path&&H.push({tool:"Write",file_path:R.input.file_path,content:R.input.content})}R.type==="thinking"&&R.thinking&&A?.({type:"thinking",jobId:s,text:R.thinking},s)}O.type==="user"&&O.tool_use_result?.file?.filePath&&A?.({type:"tool_use",jobId:s,tool:"Read",file:O.tool_use_result.file.filePath},s)}catch{}});let v=[];M.stderr?.on("data",U=>{v.push(U.toString())}),M.on("close",U=>{if(clearTimeout(I),S.close(),L.length===0&&y.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...y].join(", ")}`),x)Y=!0,k=`Timed out after ${Math.round(w/6e4)} minutes`;else if(U!==0&&U!==null){Y=!0;let O=v.join("").trim(),G=L.length===0&&y.size>0?` (no text captured, event types: ${[...y].join(", ")})`:"";k=O||`Claude process exited with code ${U}${G}`}F({sessionId:z,text:L.join(""),success:!Y,error:Y?k:void 0,fileEdits:H.length>0?H:void 0})}),M.on("error",U=>{clearTimeout(I),Y=!0,k=U.message,F({sessionId:z,text:L.join(""),success:!1,error:k,fileEdits:H.length>0?H:void 0})})});return{process:M,result:f}}import{spawn as on}from"child_process";import{createInterface as rn}from"readline";function an(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}function tt(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:a,onEvent:c}=t,p=[];i?(p.push("exec","resume","--skip-git-repo-check","--json","-c",'sandbox_mode="workspace-write"'),a&&p.push("-m",a),p.push(i),p.push(n),o&&p.push("--image",o)):(p.push("exec","--skip-git-repo-check","--json","--sandbox","workspace-write"),a&&p.push("-m",a),p.push(n),o&&p.push("--image",o));let g=on("codex",p,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),w=new Promise(A=>{let D,M=[],f=[],F=!1,z="",L=rn({input:g.stdout}),H=new Set;L.on("line",k=>{if(k.trim())try{let x=JSON.parse(k),I=x.type??"unknown";if(H.add(I),I==="thread.started"&&x.thread_id&&!D&&(D=x.thread_id),(I==="item.agentMessage.delta"||I==="item/agentMessage/delta")&&x.delta?.text&&(M.push(x.delta.text),c?.({type:"delta",jobId:s,text:x.delta.text},s)),(I==="item.reasoning.delta"||I==="item/reasoning/delta")&&x.delta?.text&&c?.({type:"thinking",jobId:s,text:x.delta.text},s),(I==="item.started"||I==="item/started")&&x.item){let S=x.item.type;if(S==="command_execution"){let y=x.item.command,v=y?an(y):void 0,U=v?`Bash: ${v.split(`
|
|
30
30
|
`)[0].slice(0,80)}`:"Bash";f.push(U),c?.({type:"tool_use",jobId:s,tool:"Bash",...v?{content:v}:{}},s)}else if(S==="file_change"){let y=x.item.filename||x.item.path;f.push(y?`Edit ${y.split("/").pop()}`:"Edit"),c?.({type:"tool_use",jobId:s,tool:"Edit",...y?{file:y}:{}},s)}else if(S==="file_read"){let y=x.item.filename||x.item.path;f.push(y?`Read ${y.split("/").pop()}`:"Read"),c?.({type:"tool_use",jobId:s,tool:"Read",...y?{file:y}:{}},s)}else if(S==="web_search")f.push("WebSearch"),c?.({type:"tool_use",jobId:s,tool:"WebSearch"},s);else if(S==="mcp_tool_call"){let y=x.item.tool_name||x.item.name||"MCP";f.push(y),c?.({type:"tool_use",jobId:s,tool:y},s)}}if((I==="item.completed"||I==="item/completed")&&x.item){if(x.item.type==="agent_message"){let S=x.item.text;typeof S=="string"&&S&&(M.push(S),c?.({type:"delta",jobId:s,text:S},s))}else if(x.item.type==="reasoning"){let S=x.item.text;typeof S=="string"&&S&&c?.({type:"thinking",jobId:s,text:S},s)}else if(x.item.type==="file_change"&&Array.isArray(x.item.changes))for(let S of x.item.changes){let y=S.path||S.filename,v=S.kind==="add"?"Write":"Edit";y&&(f.push(`${v} ${y.split("/").pop()}`),c?.({type:"tool_use",jobId:s,tool:v,file:y},s))}}I==="turn.failed"&&(F=!0,z=x.error?.message||x.message||"Turn failed")}catch{}});let Y=[];g.stderr?.on("data",k=>{Y.push(k.toString())}),g.on("close",k=>{L.close(),M.length===0&&H.size>0&&console.warn(`[Codex:${s}] No text captured. Event types seen: ${[...H].join(", ")}`),k!==0&&k!==null&&(F=!0,z=Y.join("")||`Codex process exited with code ${k}`),A({sessionId:D,text:M.join(""),success:!F,error:F?z:void 0,toolsUsed:f.length>0?f:void 0})}),g.on("error",k=>{F=!0,z=k.message,A({sessionId:D,text:M.join(""),success:!1,error:z,toolsUsed:f.length>0?f:void 0})})});return{process:g,result:w}}import{spawn as cn}from"child_process";import{createInterface as ln}from"readline";function nt(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 st(s,t){let{prompt:n,projectRoot:e,resumeSessionId:o,model:i,timeoutMs:a=3e5,onEvent:c,copilotPath:p="copilot"}=t,g=["--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&&g.push(`--resume=${o}`),i&&g.push("--model",i),g.push("-p",n);let w=cn(p,g,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),A=new Promise(D=>{let M,f=[],F=[],z=!1,L="",H=!1,Y=setTimeout(()=>{H=!0,w.kill("SIGTERM"),setTimeout(()=>{try{w.kill("SIGKILL")}catch{}},5e3)},a),k=ln({input:w.stdout}),x=new Set,I=[];k.on("line",y=>{if(y.trim())try{let v=JSON.parse(y),U=ge(v);x.add(U),I.length<5&&un(U)&&I.push(y.slice(0,800));let O=me(v,["sessionId","session_id","id"],["session","conversation","thread"]);O&&!M&&(M=O);let G=dn(v,f.length===0);G&&(f.push(G),c?.({type:"delta",jobId:s,text:G},s));let R=pn(v);R&&c?.({type:"thinking",jobId:s,text:R},s);let K=hn(v);K&&(F.push(K.label),c?.({type:"tool_use",jobId:s,tool:K.tool,...K.file?{file:K.file}:{},...K.content?{content:K.content}:{}},s));let ie=fn(v);ie&&(z=!0,L=ie)}catch{}});let S=[];w.stderr?.on("data",y=>{S.push(y.toString())}),w.on("close",y=>{clearTimeout(Y),k.close(),f.length===0&&x.size>0&&(console.warn(`[Copilot:${s}] No text captured. Event types seen: ${[...x].join(", ")}`),I.length>0&&console.warn(`[Copilot:${s}] Sample text-bearing candidates: ${I.join(`
|
|
31
31
|
`)}`)),H?(z=!0,L=`Timed out after ${Math.round(a/6e4)} minutes`):y!==0&&y!==null&&(z=!0,L=S.join("").trim()||L||`Copilot process exited with code ${y}`);let v=z?nt(L,i):null;D({sessionId:M,text:f.join(""),success:!z,error:v?.message,errorCode:v?.code,recoverableError:v?.recoverable,provider:"copilot",toolsUsed:F.length>0?F:void 0})}),w.on("error",y=>{clearTimeout(Y);let v=nt(y.message,i);D({sessionId:M,text:f.join(""),success:!1,error:v.message,errorCode:v.code,recoverableError:v.recoverable,provider:"copilot",toolsUsed:F.length>0?F:void 0})})});return{process:w,result:A}}function ge(s){if(typeof s.type=="string")return s.type;let t=s.update;return t&&typeof t.sessionUpdate=="string"?t.sessionUpdate:typeof s.event=="string"?s.event:"unknown"}function dn(s,t=!1){let n=ge(s).toLowerCase(),e=s.update,o=e?.content;if(e?.sessionUpdate==="agent_message_chunk"&&o?.type==="text"&&typeof o.text=="string")return o.text;if(n==="assistant.message_delta"||n.includes("agent_message.delta"))return ne(s.delta)??ne(s.content)??ne(s.message);if(n==="assistant.message"&&t)return ne(s.message)??ne(s.content)??ne(s);if(n==="result"&&t)return ne(s.result)??ne(s.output)??ne(s.message)??ne(s);let i=s.content;if(i?.type==="text"&&typeof i.text=="string")return i.text;if(typeof s.text=="string"&&Be(s))return s.text;if(typeof s.message=="string"&&Be(s))return s.message;if(typeof s.delta=="string"&&Be(s))return s.delta}function ne(s){if(typeof s=="string")return s;if(!s)return;if(Array.isArray(s)){let n=s.map(ne).filter(e=>!!e);return n.length>0?n.join(""):void 0}if(typeof s!="object")return;let t=s;if(typeof t.text=="string")return t.text;if(typeof t.content=="string")return t.content;for(let n of["delta","content","message","messages","parts","items","output","result","data","value","body","markdown","text_delta","textDelta"]){let e=ne(t[n]);if(e)return e}}function un(s){let t=s.toLowerCase();return t.includes("assistant.message")||t==="result"}function pn(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 hn(s){let t=me(s,["tool","toolName","tool_name","name"]),n=me(s,["command","cmd"]),e=me(s,["file","path","filename","filePath"]),o=ge(s);if(n||o.includes("shell"))return{tool:"Bash",label:n?`Bash: ${n.split(`
|
|
32
32
|
`)[0].slice(0,80)}`:"Bash",...n?{content:n}:{}};if(t)return{tool:t,label:e?`${t} ${e.split("/").pop()}`:t,...e?{file:e}:{}};if(e&&(o.includes("file")||o.includes("write")||o.includes("edit"))){let i=o.includes("write")?"Write":o.includes("read")?"Read":"Edit";return{tool:i,label:`${i} ${e.split("/").pop()}`,file:e}}}function fn(s){if(typeof s.error=="string")return s.error;if(s.error&&typeof s.error=="object"){let t=s.error;if(typeof t.message=="string")return t.message}if(typeof s.message=="string"&&ge(s).toLowerCase().includes("error"))return s.message}function me(s,t,n=[]){for(let e of t){let o=s[e];if(typeof o=="string")return o}for(let[e,o]of Object.entries(s)){if(!o||typeof o!="object"||n.length>0&&!n.some(a=>e.toLowerCase().includes(a)))continue;let i=me(o,t);if(i)return i}}function Be(s){let t=ge(s).toLowerCase();return(typeof s.role=="string"?s.role.toLowerCase():"")==="assistant"||t.includes("assistant")||t.includes("agent_message")}import{execFile as mn}from"child_process";import{copyFile as ot,mkdir as rt,readdir as gn,readFile as yn,writeFile as wn}from"fs/promises";import{join as ue}from"path";var ke=class{constructor(t){this.projectRoot=t;let n=ue(t,".popmelt");this.decisionsDir=ue(n,"decisions"),this.screenshotsDir=ue(n,"screenshots")}async persist(t,n,e){try{await rt(this.decisionsDir,{recursive:!0}),await rt(this.screenshotsDir,{recursive:!0});try{await ot(n,ue(this.screenshotsDir,`s-${t.id}.webp`))}catch{}for(let o=0;o<e.length;o++)try{let i=t.pastedImagePaths[o];i&&await ot(e[o],ue(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await wn(ue(this.decisionsDir,`d-${t.id}.json`),JSON.stringify(t,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await gn(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 yn(ue(this.decisionsDir,`d-${t}.json`),"utf-8");return JSON.parse(n)}catch{return null}}async loadDecisions(t){return(await Promise.all(t.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(t){return new Promise(n=>{mn("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){n(null);return}n(o||null)})})}};import{readFile as it,writeFile as le}from"fs/promises";import{join as Je}from"path";var X="[Materializer]",vn={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function Sn(s){return Array.isArray(s)?s.map((t,n)=>typeof t=="string"?{id:Math.random().toString(16).slice(2,10),scope:"general",text:t,sources:[]}:t&&typeof t=="object"&&typeof t.text=="string"?t:{id:Math.random().toString(16).slice(2,10),scope:"general",text:String(t),sources:[]}):[]}function ye(s){let t=[];for(let n of s){if(!n||typeof n!="object")continue;let e=n;if(typeof e.id!="string"||typeof e.text!="string"){console.warn(`${X} Dropping rule missing id or text:`,JSON.stringify(n).slice(0,120));continue}t.push({id:e.id,scope:typeof e.scope=="string"?e.scope:"general",text:e.text,sources:Array.isArray(e.sources)?e.sources.filter(o=>typeof o=="string"):[]})}return t.length>30&&console.warn(`${X} Rule count ${t.length} exceeds cap of 30`),t}var Te=class{constructor(t,n,e={}){this.projectRoot=t;this.decisionStore=n;this.options=e;this.cachedIndex=null;this.running=!1;let o=Je(t,".popmelt");this.indexPath=Je(o,"materialized.json"),this.modelPath=Je(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await it(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=Sn(n.rules)),n}catch{return null}}async addComponent(t){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let e=n.components;return e[t]?{added:!1,alreadyExists:!0}:(e[t]={description:""},await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,n){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=t.split("."),i=e;for(let p=0;p<o.length-1;p++){let g=o[p];(!i[g]||typeof i[g]!="object")&&(i[g]={}),i=i[g]}let a=o[o.length-1],c;try{c=JSON.parse(n)}catch{c=null}if(c&&typeof c=="object"&&c!==null&&"value"in c)i[a]=c;else{let p=i[a];p&&typeof p=="object"&&p!==null&&"value"in p?p.value=n:i[a]=n}return await le(this.modelPath,JSON.stringify(e,null,2)),console.log(`${X} Updated token "${t}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=t.split("."),o=n;for(let a=0;a<e.length-1;a++){let c=e[a];if(!o[c]||typeof o[c]!="object")return{removed:!1};o=o[c]}let i=e[e.length-1];return i in o?(delete o[i],await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=n.components;return!e||!e[t]?{removed:!1}:(delete e[t],await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),n=new Set(t.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(a=>!n.has(a));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(a=>a.resolutions.some(c=>(c.finalScope??c.inferredScope)?.breadth==="pattern"))}async run(){if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let t=await this.getUnmaterializedPatternDecisions();if(t.length===0)return{processedIds:[],success:!0};let n=t.map(g=>g.id);console.log(`${X} Processing ${n.length} pattern-scoped decision(s): ${n.join(", ")}`),this.options.onEvent?.({type:"materialize_started",decisionIds:n});let e=await this.loadModel(),o=xn(t,e),i=!0,a;try{let{result:g}=fe(`mat-${Date.now()}`,{prompt:o,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"],claudePath:this.options.claudePath??"claude"}),w=await g;if(!w.success)i=!1,a=w.error,console.error(`${X} Claude spawn error:`,a);else{let A=Ie(w.text);A?(Array.isArray(A.rules)&&(A.rules=ye(A.rules)),await le(this.modelPath,JSON.stringify(A,null,2)),console.log(`${X} Successfully materialized ${n.length} decision(s) \u2192 ${this.modelPath}`)):(i=!1,a="No <model> block found in response",console.error(`${X} ${a}`))}}catch(g){i=!1,a=g instanceof Error?g.message:String(g),console.error(`${X} Error:`,a)}let c=await this.loadIndex(),p=new Set(c.materializedIds);for(let g of n)p.add(g);return c.materializedIds=[...p],c.lastRunAt=Date.now(),c.lastRunDecisionIds=n,c.lastRunError=a??null,await this.persistIndex(c),this.options.onEvent?.({type:"materialize_done",decisionIds:n,success:i,error:a}),{processedIds:n,success:i,error:a}}finally{this.running=!1}}async consolidate(){if(this.running)return{success:!1,error:"Already running"};this.running=!0;try{let t=await this.loadModel();if(!t)return{success:!1,error:"No model exists"};let n=Pn(t),{result:e}=fe(`consolidate-${Date.now()}`,{prompt:n,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[],claudePath:this.options.claudePath??"claude"}),o=await e;if(!o.success)return console.error(`${X} Consolidation spawn error:`,o.error),{success:!1,error:o.error};let i=Ie(o.text);return i?(Array.isArray(i.rules)&&(i.rules=ye(i.rules)),!i.tokens&&t.tokens&&(i.tokens=t.tokens),!i.components&&t.components&&(i.components=t.components),await le(this.modelPath,JSON.stringify(i,null,2)),console.log(`${X} Consolidation complete \u2192 ${this.modelPath}`),{success:!0}):(console.error(`${X} No <model> block in consolidation response`),{success:!1,error:"No <model> block found"})}catch(t){let n=t instanceof Error?t.message:String(t);return console.error(`${X} Consolidation error:`,n),{success:!1,error:n}}finally{this.running=!1}}async writeModel(t){Array.isArray(t.rules)&&(t.rules=ye(t.rules)),await le(this.modelPath,JSON.stringify(t,null,2)),console.log(`${X} Model written \u2192 ${this.modelPath}`)}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await it(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...vn,materializedIds:[],lastRunDecisionIds:[]},this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await le(this.indexPath,JSON.stringify(t,null,2))}catch(n){console.error(`${X} Failed to write index:`,n)}}};function Ie(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 xn(s,t){let n=s.map(i=>{let c=i.resolutions.filter(w=>(w.finalScope??w.inferredScope)?.breadth==="pattern").map(w=>{let D=(w.finalScope??w.inferredScope)?.target??"unknown",M=w.filesModified?.join(", ")??"none";return`- **${w.summary}** [scope: pattern/${D}]
|
|
@@ -201,7 +201,7 @@ url = "${s}"
|
|
|
201
201
|
`)}function St(s){return s.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function xt(s,t,n,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),n!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`);let i=t.find(a=>a.role==="human"&&a.feedbackContext);if(i?.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),t.length>0){o.push(""),o.push("## Conversation History");let a=0;for(let c of t)c.role==="human"?(a++,c.replyToQuestion?(o.push(`### Round ${a} (human) \u2014 reply`),o.push(`"${c.replyToQuestion}"`)):(o.push(`### Round ${a} (human)`),c.feedbackSummary&&o.push(`Annotations: ${c.feedbackSummary}`))):c.question?(o.push(`### Round ${a} (assistant) \u2014 question`),o.push(`"${c.question}"`)):(o.push(`### Round ${a} (assistant)`),c.responseText&&o.push(`Response: ${c.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let a of e)o.push(`Attached image: use the Read tool to view the image at: ${a}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
|
|
202
202
|
`)}function _n(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 An(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&&!_n(t[n]))return!1;return!0}function Pt(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(An):[]}catch{return[]}}function bt(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 Dn}from"child_process";import{readFile as Nn}from"fs/promises";import{homedir as Ee}from"os";import{join as Se}from"path";var Me=[{id:"claude-opus-4-6",label:"Opus 4.6",source:"static"},{id:"claude-sonnet-4-6",label:"Sonn 4.6",source:"static"}],xe=[{id:"gpt-5.4",label:"GPT 5.4",source:"static"},{id:"gpt-5.3-codex",label:"Codex 5.3",source:"static"},{id:"gpt-5.3-codex-spark",label:"Spark 5.3",source:"static"}],Pe={id:"",label:"Default",source:"cli"},Bn=/^\s*-\s+"([^"]+)"/;function Jn(s){let t=[],n=!1;for(let e of s.split(`
|
|
203
203
|
`)){if(/^\s*`model`:/.test(e)){n=!0;continue}if(n&&/^\s*`[^`]+`:/.test(e))break;if(!n)continue;let o=e.match(Bn);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:Xe(i),source:"cli"})}return Ce([Pe,...t])}function Un(s){let t=JSON.parse(s);return(Array.isArray(t.models)?t.models:[]).filter(e=>e.visibility===void 0||e.visibility==="list").sort((e,o)=>{let i=typeof e.priority=="number"?e.priority:Number.MAX_SAFE_INTEGER,a=typeof o.priority=="number"?o.priority:Number.MAX_SAFE_INTEGER;return i-a}).flatMap(e=>{let o=typeof e.slug=="string"?e.slug:typeof e.id=="string"?e.id:"";if(!o)return[];let i=typeof e.display_name=="string"&&e.display_name.trim()?e.display_name.trim():Xe(o);return[{id:o,label:i,source:"cli"}]})}function jn(s){let t=s.match(/\bclaude-[a-z0-9]+(?:[-.][a-z0-9]+)*\b/gi)??[];return Ce(t.map(n=>({id:n,label:Xe(n),source:"cli"})))}function Xe(s){if(!s)return"Default";let t={"gpt-5.5":"GPT 5.5","gpt-5.4":"GPT 5.4","gpt-5.4-mini":"Mini 5.4","gpt-5.3-codex":"Codex 5.3","gpt-5.3-codex-spark":"Spark 5.3","gpt-5.2-codex":"Codex 5.2","gpt-5.2":"GPT 5.2","gpt-5.1":"GPT 5.1","gpt-5-mini":"Mini 5","gpt-4.1":"GPT 4.1","claude-opus-4.7":"Opus 4.7","claude-opus-4.6":"Opus 4.6","claude-opus-4.6-fast":"Opus 4.6 Fast","claude-opus-4.5":"Opus 4.5","claude-sonnet-4.6":"Sonn 4.6","claude-sonnet-4.5":"Sonn 4.5","claude-sonnet-4":"Sonn 4","claude-haiku-4.5":"Haiku 4.5"},n=s.replace(/-\d{8,}$/,"");if(t[s])return t[s];if(t[n])return t[n];let e=n.match(/^claude-(opus|sonnet|haiku)-(\d+)[-.](\d+)(?:-(fast))?$/);if(e)return`${e[1]==="sonnet"?"Sonn":Ln(e[1])} ${e[2]}.${e[3]}${e[4]?" Fast":""}`;let o=n.match(/^gpt-(\d+)(?:[.-](\d+))?(?:-(mini|codex|spark))?$/);if(o){let i=o[2]?`${o[1]}.${o[2]}`:o[1];return o[3]==="mini"?`Mini ${i}`:o[3]==="codex"?`Codex ${i}`:o[3]==="spark"?`Spark ${i}`:`GPT ${i}`}return n}async function kt(s="copilot"){let t=await Rt(s,["help","config"]),n=Jn(t);return n.length>1?n:[Pe]}async function Tt(s="codex"){let t=await Rt(s,["debug","models"]).catch(async()=>{let e=process.env.CODEX_HOME||Se(Ee(),".codex");return Et(Se(e,"models_cache.json"))}),n=Un(t);return n.length>0?Ce([...n,...xe]):xe}async function It(){let s=[Se(Ee(),".claude","stats-cache.json"),Se(Ee(),".claude","settings.json"),Se(Ee(),".claude.json")],t=await Promise.all(s.map(e=>Et(e).catch(()=>""))),n=jn(t.join(`
|
|
204
|
-
`));return Ce([...Me,...n])}function Rt(s,t,n=5e3){return new Promise((e,o)=>{Dn(s,t,{encoding:"utf-8",timeout:n},(i,a)=>{if(i){o(i);return}e(a)})})}async function Et(s){return Nn(s,"utf-8")}function Ce(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Ln(s){return s.charAt(0).toUpperCase()+s.slice(1)}var Oe=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let a=n<0?e.events:e.events.filter(c=>c.seq>n);return{jobId:t,events:a,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,a=()=>{i||(i=!0,o())};e.on("exit",a),e.on("error",a),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(a,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Fn,readFile as zn,writeFile as Hn}from"fs/promises";import{dirname as Gn,join as Wn}from"path";var qn={version:1,threads:{}},$e=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Wn(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await zn(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={...qn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(a=>e.has(a)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let a of n)i.has(a)||o.elementIdentifiers.push(a);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let a=o.messages.find(c=>c.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:a,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Fn(Gn(this.filePath),{recursive:!0}),await Hn(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var _e="0.6.
|
|
204
|
+
`));return Ce([...Me,...n])}function Rt(s,t,n=5e3){return new Promise((e,o)=>{Dn(s,t,{encoding:"utf-8",timeout:n},(i,a)=>{if(i){o(i);return}e(a)})})}async function Et(s){return Nn(s,"utf-8")}function Ce(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Ln(s){return s.charAt(0).toUpperCase()+s.slice(1)}var Oe=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let a=n<0?e.events:e.events.filter(c=>c.seq>n);return{jobId:t,events:a,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,a=()=>{i||(i=!0,o())};e.on("exit",a),e.on("error",a),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(a,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Fn,readFile as zn,writeFile as Hn}from"fs/promises";import{dirname as Gn,join as Wn}from"path";var qn={version:1,threads:{}},$e=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Wn(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await zn(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={...qn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(a=>e.has(a)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let a of n)i.has(a)||o.elementIdentifiers.push(a);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let a=o.messages.find(c=>c.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:a,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Fn(Gn(this.filePath),{recursive:!0}),await Hn(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var _e="0.6.9";var rs=1111,is=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],as=1800*1e3,cs=3600*1e3;function $t(s){if(!s)return!1;try{let t=new URL(s);return t.hostname==="localhost"||t.hostname==="127.0.0.1"}catch{return!1}}function ls(s){let t=[];for(let n of s)if(n.type==="delta"){let e=n.text;if(!e)continue;let o=t[t.length-1];o&&o.kind==="text"?o.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",o=n.file??void 0,i=n.content??void 0,a=o?o.split("/").pop()??o:void 0,c;switch(e){case"Read":c=a?`Reading ${a}`:"Reading file";break;case"Edit":c=a?`Editing ${a}`:"Editing file";break;case"Write":c=a?`Writing ${a}`:"Writing file";break;case"Bash":c=i?i.split(`
|
|
205
205
|
`)[0].trim().slice(0,60):"Running command";break;case"Glob":c="Searching files";break;case"Grep":c="Searching code";break;case"WebFetch":c="Fetching page";break;case"WebSearch":c="Searching web";break;default:c=e?`Using ${e}`:"tool";break}let p=o??i??void 0,g=t[t.length-1];g&&g.kind==="tool_group"&&g.tool===e?g.items.push({label:c,detail:p}):t.push({kind:"tool_group",tool:e,items:[{label:c,detail:p}]})}return t}function ds(s,t){let n=s.headers.origin;$t(n)&&(t.setHeader("Access-Control-Allow-Origin",n),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function P(s,t,n){s.writeHead(t,{"Content-Type":"application/json"}),s.end(JSON.stringify(n))}function us(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 Ye(s,t){try{s.res.write(`event: ${t.type}
|
|
206
206
|
data: ${JSON.stringify(t)}
|
|
207
207
|
|
package/dist/server.mjs
CHANGED
|
@@ -26,7 +26,7 @@ import{spawn as hs}from"child_process";import{createHash as fs}from"crypto";impo
|
|
|
26
26
|
</script>
|
|
27
27
|
</body>
|
|
28
28
|
</html>`}import{spawn as en}from"child_process";import{createInterface as tn}from"readline";var nn=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"]),et=1e5;function sn(s){let t=s.input?.file_path||s.input?.path;if(!t)return;let n=t.includes(".")?`.${t.split(".").pop().toLowerCase()}`:"";if(!nn.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>et?e.slice(0,et)+`
|
|
29
|
-
\u2026[truncated]`:e}function fe(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:a=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:p,model:g,timeoutMs:w=3e5,onEvent:A}=t,D=[];p?D.push("--resume",p,"-p",n):D.push("-p",n),D.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),g&&D.push("--model",g);for(let F of a)D.push("--allowedTools",F);let M=en(c,D,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),f=new Promise(F=>{let z,L=[],H=[],Y=!1,k="",x=!1,I=setTimeout(()=>{x=!0,M.kill("SIGTERM"),setTimeout(()=>{try{M.kill("SIGKILL")}catch{}},5e3)},w),S=tn({input:M.stdout}),y=new Set;S.on("line",U=>{if(U.trim())try{let O=JSON.parse(U);O.session_id&&!z&&(z=O.session_id);let G=O.type??(O.event?.type?`event.${O.event.type}`:"unknown");if(y.add(G),O.type==="result"&&O.result&&L.length===0){let R=typeof O.result=="string"?O.result:"";R&&(L.push(R),A?.({type:"delta",jobId:s,text:R},s))}if(O.type==="assistant"&&Array.isArray(O.message?.content))for(let R of O.message.content){if(R.type==="text"&&R.text&&(L.push(R.text),A?.({type:"delta",jobId:s,text:R.text},s)),R.type==="tool_use"&&R.name){let K=R.input?.file_path||R.input?.path||void 0,ie=sn(R);A?.({type:"tool_use",jobId:s,tool:R.name,...K?{file:K}:{},...ie?{content:ie}:{}},s),R.name==="Edit"&&R.input?.file_path?H.push({tool:"Edit",file_path:R.input.file_path,old_string:R.input.old_string,new_string:R.input.new_string,replace_all:R.input.replace_all}):R.name==="Write"&&R.input?.file_path&&H.push({tool:"Write",file_path:R.input.file_path,content:R.input.content})}R.type==="thinking"&&R.thinking&&A?.({type:"thinking",jobId:s,text:R.thinking},s)}O.type==="user"&&O.tool_use_result?.file?.filePath&&A?.({type:"tool_use",jobId:s,tool:"Read",file:O.tool_use_result.file.filePath},s)}catch{}});let v=[];M.stderr?.on("data",U=>{v.push(U.toString())}),M.on("close",U=>{if(clearTimeout(I),S.close(),L.length===0&&y.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...y].join(", ")}`),x)Y=!0,k=`Timed out after ${Math.round(w/6e4)} minutes`;else if(U!==0&&U!==null){Y=!0;let O=v.join("").trim(),G=L.length===0&&y.size>0?` (no text captured, event types: ${[...y].join(", ")})`:"";k=O||`Claude process exited with code ${U}${G}`}F({sessionId:z,text:L.join(""),success:!Y,error:Y?k:void 0,fileEdits:H.length>0?H:void 0})}),M.on("error",U=>{clearTimeout(I),Y=!0,k=U.message,F({sessionId:z,text:L.join(""),success:!1,error:k,fileEdits:H.length>0?H:void 0})})});return{process:M,result:f}}import{spawn as on}from"child_process";import{createInterface as rn}from"readline";function an(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}function tt(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:a,onEvent:c}=t,p=[];i?(p.push("exec","resume",
|
|
29
|
+
\u2026[truncated]`:e}function fe(s,t){let{prompt:n,projectRoot:e,maxTurns:o=40,maxBudgetUsd:i=1,allowedTools:a=["Read","Edit","Write","Glob","Grep","Bash"],claudePath:c="claude",resumeSessionId:p,model:g,timeoutMs:w=3e5,onEvent:A}=t,D=[];p?D.push("--resume",p,"-p",n):D.push("-p",n),D.push("--output-format","stream-json","--verbose","--max-turns",String(o),"--max-budget-usd",String(i)),g&&D.push("--model",g);for(let F of a)D.push("--allowedTools",F);let M=en(c,D,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),f=new Promise(F=>{let z,L=[],H=[],Y=!1,k="",x=!1,I=setTimeout(()=>{x=!0,M.kill("SIGTERM"),setTimeout(()=>{try{M.kill("SIGKILL")}catch{}},5e3)},w),S=tn({input:M.stdout}),y=new Set;S.on("line",U=>{if(U.trim())try{let O=JSON.parse(U);O.session_id&&!z&&(z=O.session_id);let G=O.type??(O.event?.type?`event.${O.event.type}`:"unknown");if(y.add(G),O.type==="result"&&O.result&&L.length===0){let R=typeof O.result=="string"?O.result:"";R&&(L.push(R),A?.({type:"delta",jobId:s,text:R},s))}if(O.type==="assistant"&&Array.isArray(O.message?.content))for(let R of O.message.content){if(R.type==="text"&&R.text&&(L.push(R.text),A?.({type:"delta",jobId:s,text:R.text},s)),R.type==="tool_use"&&R.name){let K=R.input?.file_path||R.input?.path||void 0,ie=sn(R);A?.({type:"tool_use",jobId:s,tool:R.name,...K?{file:K}:{},...ie?{content:ie}:{}},s),R.name==="Edit"&&R.input?.file_path?H.push({tool:"Edit",file_path:R.input.file_path,old_string:R.input.old_string,new_string:R.input.new_string,replace_all:R.input.replace_all}):R.name==="Write"&&R.input?.file_path&&H.push({tool:"Write",file_path:R.input.file_path,content:R.input.content})}R.type==="thinking"&&R.thinking&&A?.({type:"thinking",jobId:s,text:R.thinking},s)}O.type==="user"&&O.tool_use_result?.file?.filePath&&A?.({type:"tool_use",jobId:s,tool:"Read",file:O.tool_use_result.file.filePath},s)}catch{}});let v=[];M.stderr?.on("data",U=>{v.push(U.toString())}),M.on("close",U=>{if(clearTimeout(I),S.close(),L.length===0&&y.size>0&&console.warn(`[Claude:${s}] No text captured. Event types seen: ${[...y].join(", ")}`),x)Y=!0,k=`Timed out after ${Math.round(w/6e4)} minutes`;else if(U!==0&&U!==null){Y=!0;let O=v.join("").trim(),G=L.length===0&&y.size>0?` (no text captured, event types: ${[...y].join(", ")})`:"";k=O||`Claude process exited with code ${U}${G}`}F({sessionId:z,text:L.join(""),success:!Y,error:Y?k:void 0,fileEdits:H.length>0?H:void 0})}),M.on("error",U=>{clearTimeout(I),Y=!0,k=U.message,F({sessionId:z,text:L.join(""),success:!1,error:k,fileEdits:H.length>0?H:void 0})})});return{process:M,result:f}}import{spawn as on}from"child_process";import{createInterface as rn}from"readline";function an(s){let t=s.match(/^\/bin\/(?:ba)?sh\s+-\w+\s+"(.*)"$/s);return t?t[1].replace(/\\"/g,'"').replace(/\\\\/g,"\\"):s}function tt(s,t){let{prompt:n,projectRoot:e,screenshotPath:o,resumeSessionId:i,model:a,onEvent:c}=t,p=[];i?(p.push("exec","resume","--skip-git-repo-check","--json","-c",'sandbox_mode="workspace-write"'),a&&p.push("-m",a),p.push(i),p.push(n),o&&p.push("--image",o)):(p.push("exec","--skip-git-repo-check","--json","--sandbox","workspace-write"),a&&p.push("-m",a),p.push(n),o&&p.push("--image",o));let g=on("codex",p,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,CLAUDECODE:void 0}}),w=new Promise(A=>{let D,M=[],f=[],F=!1,z="",L=rn({input:g.stdout}),H=new Set;L.on("line",k=>{if(k.trim())try{let x=JSON.parse(k),I=x.type??"unknown";if(H.add(I),I==="thread.started"&&x.thread_id&&!D&&(D=x.thread_id),(I==="item.agentMessage.delta"||I==="item/agentMessage/delta")&&x.delta?.text&&(M.push(x.delta.text),c?.({type:"delta",jobId:s,text:x.delta.text},s)),(I==="item.reasoning.delta"||I==="item/reasoning/delta")&&x.delta?.text&&c?.({type:"thinking",jobId:s,text:x.delta.text},s),(I==="item.started"||I==="item/started")&&x.item){let S=x.item.type;if(S==="command_execution"){let y=x.item.command,v=y?an(y):void 0,U=v?`Bash: ${v.split(`
|
|
30
30
|
`)[0].slice(0,80)}`:"Bash";f.push(U),c?.({type:"tool_use",jobId:s,tool:"Bash",...v?{content:v}:{}},s)}else if(S==="file_change"){let y=x.item.filename||x.item.path;f.push(y?`Edit ${y.split("/").pop()}`:"Edit"),c?.({type:"tool_use",jobId:s,tool:"Edit",...y?{file:y}:{}},s)}else if(S==="file_read"){let y=x.item.filename||x.item.path;f.push(y?`Read ${y.split("/").pop()}`:"Read"),c?.({type:"tool_use",jobId:s,tool:"Read",...y?{file:y}:{}},s)}else if(S==="web_search")f.push("WebSearch"),c?.({type:"tool_use",jobId:s,tool:"WebSearch"},s);else if(S==="mcp_tool_call"){let y=x.item.tool_name||x.item.name||"MCP";f.push(y),c?.({type:"tool_use",jobId:s,tool:y},s)}}if((I==="item.completed"||I==="item/completed")&&x.item){if(x.item.type==="agent_message"){let S=x.item.text;typeof S=="string"&&S&&(M.push(S),c?.({type:"delta",jobId:s,text:S},s))}else if(x.item.type==="reasoning"){let S=x.item.text;typeof S=="string"&&S&&c?.({type:"thinking",jobId:s,text:S},s)}else if(x.item.type==="file_change"&&Array.isArray(x.item.changes))for(let S of x.item.changes){let y=S.path||S.filename,v=S.kind==="add"?"Write":"Edit";y&&(f.push(`${v} ${y.split("/").pop()}`),c?.({type:"tool_use",jobId:s,tool:v,file:y},s))}}I==="turn.failed"&&(F=!0,z=x.error?.message||x.message||"Turn failed")}catch{}});let Y=[];g.stderr?.on("data",k=>{Y.push(k.toString())}),g.on("close",k=>{L.close(),M.length===0&&H.size>0&&console.warn(`[Codex:${s}] No text captured. Event types seen: ${[...H].join(", ")}`),k!==0&&k!==null&&(F=!0,z=Y.join("")||`Codex process exited with code ${k}`),A({sessionId:D,text:M.join(""),success:!F,error:F?z:void 0,toolsUsed:f.length>0?f:void 0})}),g.on("error",k=>{F=!0,z=k.message,A({sessionId:D,text:M.join(""),success:!1,error:z,toolsUsed:f.length>0?f:void 0})})});return{process:g,result:w}}import{spawn as cn}from"child_process";import{createInterface as ln}from"readline";function nt(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 st(s,t){let{prompt:n,projectRoot:e,resumeSessionId:o,model:i,timeoutMs:a=3e5,onEvent:c,copilotPath:p="copilot"}=t,g=["--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&&g.push(`--resume=${o}`),i&&g.push("--model",i),g.push("-p",n);let w=cn(p,g,{cwd:e,stdio:["ignore","pipe","pipe"],env:{...process.env,ANTHROPIC_API_KEY:void 0,CLAUDECODE:void 0}}),A=new Promise(D=>{let M,f=[],F=[],z=!1,L="",H=!1,Y=setTimeout(()=>{H=!0,w.kill("SIGTERM"),setTimeout(()=>{try{w.kill("SIGKILL")}catch{}},5e3)},a),k=ln({input:w.stdout}),x=new Set,I=[];k.on("line",y=>{if(y.trim())try{let v=JSON.parse(y),U=ge(v);x.add(U),I.length<5&&un(U)&&I.push(y.slice(0,800));let O=me(v,["sessionId","session_id","id"],["session","conversation","thread"]);O&&!M&&(M=O);let G=dn(v,f.length===0);G&&(f.push(G),c?.({type:"delta",jobId:s,text:G},s));let R=pn(v);R&&c?.({type:"thinking",jobId:s,text:R},s);let K=hn(v);K&&(F.push(K.label),c?.({type:"tool_use",jobId:s,tool:K.tool,...K.file?{file:K.file}:{},...K.content?{content:K.content}:{}},s));let ie=fn(v);ie&&(z=!0,L=ie)}catch{}});let S=[];w.stderr?.on("data",y=>{S.push(y.toString())}),w.on("close",y=>{clearTimeout(Y),k.close(),f.length===0&&x.size>0&&(console.warn(`[Copilot:${s}] No text captured. Event types seen: ${[...x].join(", ")}`),I.length>0&&console.warn(`[Copilot:${s}] Sample text-bearing candidates: ${I.join(`
|
|
31
31
|
`)}`)),H?(z=!0,L=`Timed out after ${Math.round(a/6e4)} minutes`):y!==0&&y!==null&&(z=!0,L=S.join("").trim()||L||`Copilot process exited with code ${y}`);let v=z?nt(L,i):null;D({sessionId:M,text:f.join(""),success:!z,error:v?.message,errorCode:v?.code,recoverableError:v?.recoverable,provider:"copilot",toolsUsed:F.length>0?F:void 0})}),w.on("error",y=>{clearTimeout(Y);let v=nt(y.message,i);D({sessionId:M,text:f.join(""),success:!1,error:v.message,errorCode:v.code,recoverableError:v.recoverable,provider:"copilot",toolsUsed:F.length>0?F:void 0})})});return{process:w,result:A}}function ge(s){if(typeof s.type=="string")return s.type;let t=s.update;return t&&typeof t.sessionUpdate=="string"?t.sessionUpdate:typeof s.event=="string"?s.event:"unknown"}function dn(s,t=!1){let n=ge(s).toLowerCase(),e=s.update,o=e?.content;if(e?.sessionUpdate==="agent_message_chunk"&&o?.type==="text"&&typeof o.text=="string")return o.text;if(n==="assistant.message_delta"||n.includes("agent_message.delta"))return ne(s.delta)??ne(s.content)??ne(s.message);if(n==="assistant.message"&&t)return ne(s.message)??ne(s.content)??ne(s);if(n==="result"&&t)return ne(s.result)??ne(s.output)??ne(s.message)??ne(s);let i=s.content;if(i?.type==="text"&&typeof i.text=="string")return i.text;if(typeof s.text=="string"&&Be(s))return s.text;if(typeof s.message=="string"&&Be(s))return s.message;if(typeof s.delta=="string"&&Be(s))return s.delta}function ne(s){if(typeof s=="string")return s;if(!s)return;if(Array.isArray(s)){let n=s.map(ne).filter(e=>!!e);return n.length>0?n.join(""):void 0}if(typeof s!="object")return;let t=s;if(typeof t.text=="string")return t.text;if(typeof t.content=="string")return t.content;for(let n of["delta","content","message","messages","parts","items","output","result","data","value","body","markdown","text_delta","textDelta"]){let e=ne(t[n]);if(e)return e}}function un(s){let t=s.toLowerCase();return t.includes("assistant.message")||t==="result"}function pn(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 hn(s){let t=me(s,["tool","toolName","tool_name","name"]),n=me(s,["command","cmd"]),e=me(s,["file","path","filename","filePath"]),o=ge(s);if(n||o.includes("shell"))return{tool:"Bash",label:n?`Bash: ${n.split(`
|
|
32
32
|
`)[0].slice(0,80)}`:"Bash",...n?{content:n}:{}};if(t)return{tool:t,label:e?`${t} ${e.split("/").pop()}`:t,...e?{file:e}:{}};if(e&&(o.includes("file")||o.includes("write")||o.includes("edit"))){let i=o.includes("write")?"Write":o.includes("read")?"Read":"Edit";return{tool:i,label:`${i} ${e.split("/").pop()}`,file:e}}}function fn(s){if(typeof s.error=="string")return s.error;if(s.error&&typeof s.error=="object"){let t=s.error;if(typeof t.message=="string")return t.message}if(typeof s.message=="string"&&ge(s).toLowerCase().includes("error"))return s.message}function me(s,t,n=[]){for(let e of t){let o=s[e];if(typeof o=="string")return o}for(let[e,o]of Object.entries(s)){if(!o||typeof o!="object"||n.length>0&&!n.some(a=>e.toLowerCase().includes(a)))continue;let i=me(o,t);if(i)return i}}function Be(s){let t=ge(s).toLowerCase();return(typeof s.role=="string"?s.role.toLowerCase():"")==="assistant"||t.includes("assistant")||t.includes("agent_message")}import{execFile as mn}from"child_process";import{copyFile as ot,mkdir as rt,readdir as gn,readFile as yn,writeFile as wn}from"fs/promises";import{join as ue}from"path";var ke=class{constructor(t){this.projectRoot=t;let n=ue(t,".popmelt");this.decisionsDir=ue(n,"decisions"),this.screenshotsDir=ue(n,"screenshots")}async persist(t,n,e){try{await rt(this.decisionsDir,{recursive:!0}),await rt(this.screenshotsDir,{recursive:!0});try{await ot(n,ue(this.screenshotsDir,`s-${t.id}.webp`))}catch{}for(let o=0;o<e.length;o++)try{let i=t.pastedImagePaths[o];i&&await ot(e[o],ue(this.screenshotsDir,i.replace("screenshots/","")))}catch{}await wn(ue(this.decisionsDir,`d-${t.id}.json`),JSON.stringify(t,null,2))}catch(o){console.error("[DecisionStore] Failed to persist decision record:",o)}}async listDecisionIds(){try{return(await gn(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 yn(ue(this.decisionsDir,`d-${t}.json`),"utf-8");return JSON.parse(n)}catch{return null}}async loadDecisions(t){return(await Promise.all(t.map(e=>this.loadDecision(e)))).filter(e=>e!==null)}captureGitDiff(t){return new Promise(n=>{mn("git",["diff","HEAD"],{cwd:t,timeout:5e3,maxBuffer:1024*1024},(e,o)=>{if(e){n(null);return}n(o||null)})})}};import{readFile as it,writeFile as le}from"fs/promises";import{join as Je}from"path";var X="[Materializer]",vn={version:1,materializedIds:[],lastRunAt:null,lastRunDecisionIds:[],lastRunError:null};function Sn(s){return Array.isArray(s)?s.map((t,n)=>typeof t=="string"?{id:Math.random().toString(16).slice(2,10),scope:"general",text:t,sources:[]}:t&&typeof t=="object"&&typeof t.text=="string"?t:{id:Math.random().toString(16).slice(2,10),scope:"general",text:String(t),sources:[]}):[]}function ye(s){let t=[];for(let n of s){if(!n||typeof n!="object")continue;let e=n;if(typeof e.id!="string"||typeof e.text!="string"){console.warn(`${X} Dropping rule missing id or text:`,JSON.stringify(n).slice(0,120));continue}t.push({id:e.id,scope:typeof e.scope=="string"?e.scope:"general",text:e.text,sources:Array.isArray(e.sources)?e.sources.filter(o=>typeof o=="string"):[]})}return t.length>30&&console.warn(`${X} Rule count ${t.length} exceeds cap of 30`),t}var Te=class{constructor(t,n,e={}){this.projectRoot=t;this.decisionStore=n;this.options=e;this.cachedIndex=null;this.running=!1;let o=Je(t,".popmelt");this.indexPath=Je(o,"materialized.json"),this.modelPath=Je(o,"model.json")}get isRunning(){return this.running}async loadModel(){try{let t=await it(this.modelPath,"utf-8"),n=JSON.parse(t);return Array.isArray(n.rules)&&(n.rules=Sn(n.rules)),n}catch{return null}}async addComponent(t){let n=await this.loadModel();n||(n={tokens:{},components:{},rules:[]}),(!n.components||typeof n.components!="object")&&(n.components={});let e=n.components;return e[t]?{added:!1,alreadyExists:!0}:(e[t]={description:""},await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Added component "${t}" to model`),{added:!0,alreadyExists:!1})}async updateToken(t,n){let e=await this.loadModel();e||(e={tokens:{},components:{},rules:[]});let o=t.split("."),i=e;for(let p=0;p<o.length-1;p++){let g=o[p];(!i[g]||typeof i[g]!="object")&&(i[g]={}),i=i[g]}let a=o[o.length-1],c;try{c=JSON.parse(n)}catch{c=null}if(c&&typeof c=="object"&&c!==null&&"value"in c)i[a]=c;else{let p=i[a];p&&typeof p=="object"&&p!==null&&"value"in p?p.value=n:i[a]=n}return await le(this.modelPath,JSON.stringify(e,null,2)),console.log(`${X} Updated token "${t}" \u2192 "${n.slice(0,80)}"`),{updated:!0}}async removeToken(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=t.split("."),o=n;for(let a=0;a<e.length-1;a++){let c=e[a];if(!o[c]||typeof o[c]!="object")return{removed:!1};o=o[c]}let i=e[e.length-1];return i in o?(delete o[i],await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Removed token "${t}" from model`),{removed:!0}):{removed:!1}}async removeComponent(t){let n=await this.loadModel();if(!n)return{removed:!1};let e=n.components;return!e||!e[t]?{removed:!1}:(delete e[t],await le(this.modelPath,JSON.stringify(n,null,2)),console.log(`${X} Removed component "${t}" from model`),{removed:!0})}async getUnmaterializedPatternDecisions(){let t=await this.loadIndex(),n=new Set(t.materializedIds),o=(await this.decisionStore.listDecisionIds()).filter(a=>!n.has(a));return o.length===0?[]:(await this.decisionStore.loadDecisions(o)).filter(a=>a.resolutions.some(c=>(c.finalScope??c.inferredScope)?.breadth==="pattern"))}async run(){if(this.running)return{processedIds:[],success:!0,error:"Already running"};this.running=!0;try{let t=await this.getUnmaterializedPatternDecisions();if(t.length===0)return{processedIds:[],success:!0};let n=t.map(g=>g.id);console.log(`${X} Processing ${n.length} pattern-scoped decision(s): ${n.join(", ")}`),this.options.onEvent?.({type:"materialize_started",decisionIds:n});let e=await this.loadModel(),o=xn(t,e),i=!0,a;try{let{result:g}=fe(`mat-${Date.now()}`,{prompt:o,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??5,maxBudgetUsd:this.options.maxBudgetUsd??.5,allowedTools:["Read"],claudePath:this.options.claudePath??"claude"}),w=await g;if(!w.success)i=!1,a=w.error,console.error(`${X} Claude spawn error:`,a);else{let A=Ie(w.text);A?(Array.isArray(A.rules)&&(A.rules=ye(A.rules)),await le(this.modelPath,JSON.stringify(A,null,2)),console.log(`${X} Successfully materialized ${n.length} decision(s) \u2192 ${this.modelPath}`)):(i=!1,a="No <model> block found in response",console.error(`${X} ${a}`))}}catch(g){i=!1,a=g instanceof Error?g.message:String(g),console.error(`${X} Error:`,a)}let c=await this.loadIndex(),p=new Set(c.materializedIds);for(let g of n)p.add(g);return c.materializedIds=[...p],c.lastRunAt=Date.now(),c.lastRunDecisionIds=n,c.lastRunError=a??null,await this.persistIndex(c),this.options.onEvent?.({type:"materialize_done",decisionIds:n,success:i,error:a}),{processedIds:n,success:i,error:a}}finally{this.running=!1}}async consolidate(){if(this.running)return{success:!1,error:"Already running"};this.running=!0;try{let t=await this.loadModel();if(!t)return{success:!1,error:"No model exists"};let n=Pn(t),{result:e}=fe(`consolidate-${Date.now()}`,{prompt:n,projectRoot:this.projectRoot,maxTurns:this.options.maxTurns??3,maxBudgetUsd:this.options.maxBudgetUsd??.3,allowedTools:[],claudePath:this.options.claudePath??"claude"}),o=await e;if(!o.success)return console.error(`${X} Consolidation spawn error:`,o.error),{success:!1,error:o.error};let i=Ie(o.text);return i?(Array.isArray(i.rules)&&(i.rules=ye(i.rules)),!i.tokens&&t.tokens&&(i.tokens=t.tokens),!i.components&&t.components&&(i.components=t.components),await le(this.modelPath,JSON.stringify(i,null,2)),console.log(`${X} Consolidation complete \u2192 ${this.modelPath}`),{success:!0}):(console.error(`${X} No <model> block in consolidation response`),{success:!1,error:"No <model> block found"})}catch(t){let n=t instanceof Error?t.message:String(t);return console.error(`${X} Consolidation error:`,n),{success:!1,error:n}}finally{this.running=!1}}async writeModel(t){Array.isArray(t.rules)&&(t.rules=ye(t.rules)),await le(this.modelPath,JSON.stringify(t,null,2)),console.log(`${X} Model written \u2192 ${this.modelPath}`)}async loadIndex(){if(this.cachedIndex)return this.cachedIndex;try{let t=await it(this.indexPath,"utf-8"),n=JSON.parse(t);return this.cachedIndex=n,n}catch{return this.cachedIndex={...vn,materializedIds:[],lastRunDecisionIds:[]},this.cachedIndex}}async persistIndex(t){this.cachedIndex=t;try{await le(this.indexPath,JSON.stringify(t,null,2))}catch(n){console.error(`${X} Failed to write index:`,n)}}};function Ie(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 xn(s,t){let n=s.map(i=>{let c=i.resolutions.filter(w=>(w.finalScope??w.inferredScope)?.breadth==="pattern").map(w=>{let D=(w.finalScope??w.inferredScope)?.target??"unknown",M=w.filesModified?.join(", ")??"none";return`- **${w.summary}** [scope: pattern/${D}]
|
|
@@ -201,7 +201,7 @@ url = "${s}"
|
|
|
201
201
|
`)}function St(s){return s.match(/<question>\s*([\s\S]*?)\s*<\/question>/)?.[1]??null}function xt(s,t,n,e){let o=[];o.push("You are continuing work on a UI based on the developer's reply to your question."),o.push(""),n!=="codex"&&o.push(`IMPORTANT: First, use the Read tool to view the screenshot at: ${s}`);let i=t.find(a=>a.role==="human"&&a.feedbackContext);if(i?.feedbackContext&&(o.push(""),o.push(i.feedbackContext)),t.length>0){o.push(""),o.push("## Conversation History");let a=0;for(let c of t)c.role==="human"?(a++,c.replyToQuestion?(o.push(`### Round ${a} (human) \u2014 reply`),o.push(`"${c.replyToQuestion}"`)):(o.push(`### Round ${a} (human)`),c.feedbackSummary&&o.push(`Annotations: ${c.feedbackSummary}`))):c.question?(o.push(`### Round ${a} (assistant) \u2014 question`),o.push(`"${c.question}"`)):(o.push(`### Round ${a} (assistant)`),c.responseText&&o.push(`Response: ${c.responseText}`))}if(o.push(""),o.push("The developer answered your question. Continue working based on their reply."),o.push("Follow their instructions \u2014 apply code changes only if requested. The dev server has HMR so changes appear immediately."),o.push(""),o.push("IMPORTANT: If any elements you modify have a `data-pm` attribute, preserve it in the source. This attribute tracks annotation positions."),e&&e.length>0){o.push(""),o.push("## Attached Images"),o.push("The developer attached reference images with their reply:");for(let a of e)o.push(`Attached image: use the Read tool to view the image at: ${a}`)}return o.push(""),o.push("## Resolution"),o.push("After completing all work, output a resolution block listing what you did for each annotation:"),o.push("<resolution>"),o.push('[{"annotationId":"<id>","status":"resolved","summary":"<what you did>","filesModified":["<file>"],"declaredScope":{"breadth":"...","target":"..."},"inferredScope":{"breadth":"...","target":"..."}}]'),o.push("</resolution>"),o.push(`Use status "resolved" when the change is complete, or "needs_review" if you're unsure about the result.`),o.push(""),o.push("### Scope classification"),o.push("Each resolution MUST include scope fields:"),o.push("- `declaredScope`: What scope the user's instruction text implies. null if no signal."),o.push("- `inferredScope`: What scope the change actually has, based on what you modified."),o.push("Scope has two dimensions:"),o.push('- `breadth`: "instance" (just this occurrence) or "pattern" (all similar occurrences)'),o.push('- `target`: "element" (a specific DOM element), "component" (a React/UI component), or "token" (a design token \u2014 color, spacing, typography)'),o.push('Note: "instance" + "token" is invalid \u2014 tokens are inherently patterns.'),o.push("If you cannot confidently determine scope, set it to null."),o.push('If the developer\'s reply corrects a prior scope classification (e.g., "this should apply everywhere" or "only fix this one"), set `finalScope` on your resolution to reflect their correction and apply the change at the corrected scope.'),o.push(""),o.push("## Questions"),o.push("If you still need clarification, output:"),o.push("<question>Your question here</question>"),o.push("You may output BOTH a <resolution> and a <question> in the same response."),o.join(`
|
|
202
202
|
`)}function _n(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 An(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&&!_n(t[n]))return!1;return!0}function Pt(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(An):[]}catch{return[]}}function bt(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 Dn}from"child_process";import{readFile as Nn}from"fs/promises";import{homedir as Ee}from"os";import{join as Se}from"path";var Me=[{id:"claude-opus-4-6",label:"Opus 4.6",source:"static"},{id:"claude-sonnet-4-6",label:"Sonn 4.6",source:"static"}],xe=[{id:"gpt-5.4",label:"GPT 5.4",source:"static"},{id:"gpt-5.3-codex",label:"Codex 5.3",source:"static"},{id:"gpt-5.3-codex-spark",label:"Spark 5.3",source:"static"}],Pe={id:"",label:"Default",source:"cli"},Bn=/^\s*-\s+"([^"]+)"/;function Jn(s){let t=[],n=!1;for(let e of s.split(`
|
|
203
203
|
`)){if(/^\s*`model`:/.test(e)){n=!0;continue}if(n&&/^\s*`[^`]+`:/.test(e))break;if(!n)continue;let o=e.match(Bn);if(!o?.[1])continue;let i=o[1];t.push({id:i,label:Xe(i),source:"cli"})}return Ce([Pe,...t])}function Un(s){let t=JSON.parse(s);return(Array.isArray(t.models)?t.models:[]).filter(e=>e.visibility===void 0||e.visibility==="list").sort((e,o)=>{let i=typeof e.priority=="number"?e.priority:Number.MAX_SAFE_INTEGER,a=typeof o.priority=="number"?o.priority:Number.MAX_SAFE_INTEGER;return i-a}).flatMap(e=>{let o=typeof e.slug=="string"?e.slug:typeof e.id=="string"?e.id:"";if(!o)return[];let i=typeof e.display_name=="string"&&e.display_name.trim()?e.display_name.trim():Xe(o);return[{id:o,label:i,source:"cli"}]})}function jn(s){let t=s.match(/\bclaude-[a-z0-9]+(?:[-.][a-z0-9]+)*\b/gi)??[];return Ce(t.map(n=>({id:n,label:Xe(n),source:"cli"})))}function Xe(s){if(!s)return"Default";let t={"gpt-5.5":"GPT 5.5","gpt-5.4":"GPT 5.4","gpt-5.4-mini":"Mini 5.4","gpt-5.3-codex":"Codex 5.3","gpt-5.3-codex-spark":"Spark 5.3","gpt-5.2-codex":"Codex 5.2","gpt-5.2":"GPT 5.2","gpt-5.1":"GPT 5.1","gpt-5-mini":"Mini 5","gpt-4.1":"GPT 4.1","claude-opus-4.7":"Opus 4.7","claude-opus-4.6":"Opus 4.6","claude-opus-4.6-fast":"Opus 4.6 Fast","claude-opus-4.5":"Opus 4.5","claude-sonnet-4.6":"Sonn 4.6","claude-sonnet-4.5":"Sonn 4.5","claude-sonnet-4":"Sonn 4","claude-haiku-4.5":"Haiku 4.5"},n=s.replace(/-\d{8,}$/,"");if(t[s])return t[s];if(t[n])return t[n];let e=n.match(/^claude-(opus|sonnet|haiku)-(\d+)[-.](\d+)(?:-(fast))?$/);if(e)return`${e[1]==="sonnet"?"Sonn":Ln(e[1])} ${e[2]}.${e[3]}${e[4]?" Fast":""}`;let o=n.match(/^gpt-(\d+)(?:[.-](\d+))?(?:-(mini|codex|spark))?$/);if(o){let i=o[2]?`${o[1]}.${o[2]}`:o[1];return o[3]==="mini"?`Mini ${i}`:o[3]==="codex"?`Codex ${i}`:o[3]==="spark"?`Spark ${i}`:`GPT ${i}`}return n}async function kt(s="copilot"){let t=await Rt(s,["help","config"]),n=Jn(t);return n.length>1?n:[Pe]}async function Tt(s="codex"){let t=await Rt(s,["debug","models"]).catch(async()=>{let e=process.env.CODEX_HOME||Se(Ee(),".codex");return Et(Se(e,"models_cache.json"))}),n=Un(t);return n.length>0?Ce([...n,...xe]):xe}async function It(){let s=[Se(Ee(),".claude","stats-cache.json"),Se(Ee(),".claude","settings.json"),Se(Ee(),".claude.json")],t=await Promise.all(s.map(e=>Et(e).catch(()=>""))),n=jn(t.join(`
|
|
204
|
-
`));return Ce([...Me,...n])}function Rt(s,t,n=5e3){return new Promise((e,o)=>{Dn(s,t,{encoding:"utf-8",timeout:n},(i,a)=>{if(i){o(i);return}e(a)})})}async function Et(s){return Nn(s,"utf-8")}function Ce(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Ln(s){return s.charAt(0).toUpperCase()+s.slice(1)}var Oe=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let a=n<0?e.events:e.events.filter(c=>c.seq>n);return{jobId:t,events:a,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,a=()=>{i||(i=!0,o())};e.on("exit",a),e.on("error",a),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(a,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Fn,readFile as zn,writeFile as Hn}from"fs/promises";import{dirname as Gn,join as Wn}from"path";var qn={version:1,threads:{}},$e=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Wn(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await zn(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={...qn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(a=>e.has(a)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let a of n)i.has(a)||o.elementIdentifiers.push(a);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let a=o.messages.find(c=>c.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:a,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Fn(Gn(this.filePath),{recursive:!0}),await Hn(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var _e="0.6.
|
|
204
|
+
`));return Ce([...Me,...n])}function Rt(s,t,n=5e3){return new Promise((e,o)=>{Dn(s,t,{encoding:"utf-8",timeout:n},(i,a)=>{if(i){o(i);return}e(a)})})}async function Et(s){return Nn(s,"utf-8")}function Ce(s){let t=new Set,n=[];for(let e of s)t.has(e.id)||(t.add(e.id),n.push(e));return n}function Ln(s){return s.charAt(0).toUpperCase()+s.slice(1)}var Oe=class{constructor(t=5){this.queue=[];this.activeJobs=new Map;this.activeProcesses=new Map;this.listeners=new Set;this.processor=null;this.eventBuffers=new Map;this.accumulators=new Map;this.cleanupTimers=new Map;this.maxConcurrency=t}setProcessor(t){this.processor=t}get active(){let t=this.activeJobs.values().next();return t.done?null:t.value}get allActive(){return Array.from(this.activeJobs.values())}get activeCount(){return this.activeJobs.size}get depth(){return this.queue.length}get isRunning(){return this.activeJobs.size>0}setActiveProcess(t,n){n?this.activeProcesses.set(t,n):this.activeProcesses.delete(t)}enqueue(t){return this.queue.push(t),this.processNext(),this.queue.length+this.activeJobs.size}addListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}broadcast(t,n,e){let o=this.bufferEvent(n,t);for(let i of this.listeners)i(o,n,e)}bufferEvent(t,n){let e=this.eventBuffers.get(t);e||(e={events:[],nextSeq:0},this.eventBuffers.set(t,e));let o={...n,seq:e.nextSeq++};return e.events.push(o),e.events.length>1e4&&e.events.splice(0,e.events.length-1e4),o}getBufferedEvents(t,n=-1){let e=this.eventBuffers.get(t),o=this.accumulators.get(t)??{response:"",thinking:""},i=this.activeJobs.has(t);if(!e)return null;let a=n<0?e.events:e.events.filter(c=>c.seq>n);return{jobId:t,events:a,currentSeq:e.nextSeq-1,accumulated:{...o},jobActive:i}}accumulateText(t,n,e){let o=this.accumulators.get(t);o||(o={response:"",thinking:""},this.accumulators.set(t,o)),o[n]+=e}getAccumulated(t){return this.accumulators.get(t)??null}scheduleBufferCleanup(t){let n=this.cleanupTimers.get(t);n&&clearTimeout(n);let e=setTimeout(()=>{this.eventBuffers.delete(t),this.accumulators.delete(t),this.cleanupTimers.delete(t)},6e4);this.cleanupTimers.set(t,e)}cancelJob(t){let n=this.activeProcesses.get(t),e=this.activeJobs.get(t);return!n||!e?!1:(n.kill("SIGTERM"),this.activeProcesses.delete(t),this.activeJobs.delete(t),e.status="error",e.error="Cancelled by user",this.broadcast({type:"error",jobId:e.id,threadId:e.threadId,message:"Cancelled by user",cancelled:!0,provider:e.provider,model:e.model},e.id,e.sourceId),this.processNext(),!0)}cancelActive(){if(this.activeJobs.size===0)return!1;let t=Array.from(this.activeJobs.keys());for(let n of t)this.cancelJob(n);return!0}destroy(){for(let t of this.activeProcesses.values())t.kill("SIGTERM");this.activeProcesses.clear(),this.activeJobs.clear(),this.queue=[],this.listeners.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let t of this.cleanupTimers.values())clearTimeout(t);this.cleanupTimers.clear()}async destroyAsync(t=1e4){let n=Array.from(this.activeProcesses.values());if(this.queue=[],this.listeners.clear(),n.length===0){this.activeProcesses.clear(),this.activeJobs.clear();return}for(let e of n)try{e.kill("SIGTERM")}catch{}await Promise.all(n.map(e=>new Promise(o=>{let i=!1,a=()=>{i||(i=!0,o())};e.on("exit",a),e.on("error",a),setTimeout(()=>{if(!i){try{e.kill("SIGKILL")}catch{}setTimeout(a,500)}},t)}))),this.activeProcesses.clear(),this.activeJobs.clear(),this.eventBuffers.clear(),this.accumulators.clear();for(let e of this.cleanupTimers.values())clearTimeout(e);this.cleanupTimers.clear()}processNext(){for(;this.activeJobs.size<this.maxConcurrency&&this.queue.length>0&&this.processor;){let t=this.queue.shift();this.activeJobs.set(t.id,t),t.status="running",this.broadcast({type:"job_started",jobId:t.id,position:0,threadId:t.threadId},t.id,t.sourceId),this.processor(t).catch(n=>{t.status="error",t.error=n instanceof Error?n.message:String(n),this.broadcast({type:"error",jobId:t.id,threadId:t.threadId,message:t.error,provider:t.provider,model:t.model},t.id,t.sourceId)}).finally(()=>{this.activeJobs.delete(t.id),this.activeProcesses.delete(t.id),this.scheduleBufferCleanup(t.id),this.processNext(),this.activeJobs.size===0&&this.queue.length===0&&this.broadcast({type:"queue_drained"},t.id)})}}};import{mkdir as Fn,readFile as zn,writeFile as Hn}from"fs/promises";import{dirname as Gn,join as Wn}from"path";var qn={version:1,threads:{}},$e=class{constructor(t){this.cache=null;this.writeChain=Promise.resolve();this.filePath=Wn(t,".popmelt","threads.json")}async load(){if(this.cache)return this.cache;try{let t=await zn(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={...qn,threads:{}},this.cache}async getThread(t){return(await this.load()).threads[t]??null}async findContinuationThread(t){if(t.length===0)return null;let n=await this.load(),e=new Set(t);for(let o of Object.values(n.threads))if(o.elementIdentifiers.some(a=>e.has(a)))return o;return null}async createThread(t,n){let e=await this.load(),o={id:t,createdAt:Date.now(),updatedAt:Date.now(),elementIdentifiers:n,messages:[]};return e.threads[t]=o,await this.persist(),o}async appendMessage(t,n){let o=(await this.load()).threads[t];o&&(o.messages.push(n),o.updatedAt=Date.now(),await this.persist())}async addElementIdentifiers(t,n){let o=(await this.load()).threads[t];if(!o)return;let i=new Set(o.elementIdentifiers);for(let a of n)i.has(a)||o.elementIdentifiers.push(a);o.updatedAt=Date.now(),await this.persist()}async listRecent(t=5){let n=await this.load(),e=Object.values(n.threads);return e.sort((o,i)=>i.updatedAt-o.updatedAt),e.slice(0,t).map(o=>{let a=o.messages.find(c=>c.role==="human")?.feedbackSummary||"[thread]";return{id:o.id,createdAt:o.createdAt,updatedAt:o.updatedAt,preview:a,messageCount:o.messages.length,elementIdentifiers:o.elementIdentifiers}})}async getThreadHistory(t,n=6){let e=await this.getThread(t);return!e||e.messages.length===0?[]:e.messages.length<=n?e.messages:[e.messages[0],...e.messages.slice(-(n-1))]}async persist(){this.writeChain=this.writeChain.then(async()=>{if(this.cache)try{await Fn(Gn(this.filePath),{recursive:!0}),await Hn(this.filePath,JSON.stringify(this.cache,null,2))}catch(t){console.error("[ThreadStore] Failed to persist:",t)}}),await this.writeChain}};var _e="0.6.9";var rs=1111,is=["Read","Edit","Write","Glob","Grep","Bash","WebFetch","WebSearch","Bash(curl:*)"],as=1800*1e3,cs=3600*1e3;function $t(s){if(!s)return!1;try{let t=new URL(s);return t.hostname==="localhost"||t.hostname==="127.0.0.1"}catch{return!1}}function ls(s){let t=[];for(let n of s)if(n.type==="delta"){let e=n.text;if(!e)continue;let o=t[t.length-1];o&&o.kind==="text"?o.text+=e:t.push({kind:"text",text:e})}else if(n.type==="tool_use"){let e=n.tool||"",o=n.file??void 0,i=n.content??void 0,a=o?o.split("/").pop()??o:void 0,c;switch(e){case"Read":c=a?`Reading ${a}`:"Reading file";break;case"Edit":c=a?`Editing ${a}`:"Editing file";break;case"Write":c=a?`Writing ${a}`:"Writing file";break;case"Bash":c=i?i.split(`
|
|
205
205
|
`)[0].trim().slice(0,60):"Running command";break;case"Glob":c="Searching files";break;case"Grep":c="Searching code";break;case"WebFetch":c="Fetching page";break;case"WebSearch":c="Searching web";break;default:c=e?`Using ${e}`:"tool";break}let p=o??i??void 0,g=t[t.length-1];g&&g.kind==="tool_group"&&g.tool===e?g.items.push({label:c,detail:p}):t.push({kind:"tool_group",tool:e,items:[{label:c,detail:p}]})}return t}function ds(s,t){let n=s.headers.origin;$t(n)&&(t.setHeader("Access-Control-Allow-Origin",n),t.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type"))}function P(s,t,n){s.writeHead(t,{"Content-Type":"application/json"}),s.end(JSON.stringify(n))}function us(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 Ye(s,t){try{s.res.write(`event: ${t.type}
|
|
206
206
|
data: ${JSON.stringify(t)}
|
|
207
207
|
|
package/package.json
CHANGED