loop-canvas 0.1.1 → 0.1.2
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/README.md +1 -1
- package/cli.mjs +9 -8
- package/package.json +1 -1
- package/public/app.css +1 -1
- package/public/app.js +80 -35
- package/public/icons.LICENSE.txt +21 -0
- package/public/index.html +38 -21
- package/server.mjs +29 -19
- package/skills/loop-canvas/SKILL.md +13 -8
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Hugeicons
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/public/index.html
CHANGED
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
8
8
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
|
|
9
9
|
<link rel="stylesheet" href="/__loop/app/app.css">
|
|
10
|
-
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect x='3' y='3' width='26' height='26' rx='7' fill='%2317171A'/%3E%3Ccircle cx='16' cy='16' r='6.5' stroke='%
|
|
10
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect x='3' y='3' width='26' height='26' rx='7' fill='%2317171A'/%3E%3Ccircle cx='16' cy='16' r='6.5' stroke='%23F4F4F5' stroke-width='2.6' fill='none'/%3E%3C/svg%3E">
|
|
11
11
|
</head>
|
|
12
12
|
<body>
|
|
13
13
|
<div id="viewport" data-tool="select">
|
|
14
14
|
<div id="world">
|
|
15
|
+
<div id="sections"></div>
|
|
15
16
|
<div id="frames"></div>
|
|
16
17
|
<svg id="ink" width="1" height="1"></svg>
|
|
17
18
|
<div id="cursors"></div>
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
<div class="empty-mark"></div>
|
|
28
29
|
<h1>An empty canvas</h1>
|
|
29
30
|
<p>Ask your agent to fill it:</p>
|
|
30
|
-
<code>Use the Loop skill to put this project
|
|
31
|
+
<code>Use the Loop skill to put this project on a canvas.</code>
|
|
31
32
|
<p class="muted">Or press <kbd>N</kbd> to describe a frame and let the agent build it.</p>
|
|
32
33
|
</div>
|
|
33
34
|
</div>
|
|
@@ -35,17 +36,17 @@
|
|
|
35
36
|
|
|
36
37
|
<header id="topbar">
|
|
37
38
|
<div class="brand">
|
|
38
|
-
<span class="logo" aria-hidden="true"><svg viewBox="0 0 32 32"><rect x="3" y="3" width="26" height="26" rx="7"/><circle cx="16" cy="16" r="6.5"/></svg></span>
|
|
39
|
-
<span class="wordmark">Loop</span>
|
|
40
|
-
<span class="sep">/</span>
|
|
39
|
+
<span class="logo" aria-hidden="true" title="Loop"><svg viewBox="0 0 32 32"><rect x="3" y="3" width="26" height="26" rx="7"/><circle cx="16" cy="16" r="6.5"/></svg></span>
|
|
41
40
|
<span id="canvas-name" class="canvas-name" title="Rename canvas"></span>
|
|
42
41
|
</div>
|
|
43
42
|
<div class="top-right">
|
|
44
43
|
<div id="presence" class="presence"></div>
|
|
44
|
+
<button id="comments-btn" class="pill comments-btn" title="All comments"><i data-i="comments"></i><span id="comments-count"></span></button>
|
|
45
45
|
<button id="agent-pill" class="pill" title="Agent connection">
|
|
46
46
|
<span class="agent-dot"></span><span id="agent-label">Agent</span>
|
|
47
47
|
</button>
|
|
48
|
-
<button id="
|
|
48
|
+
<button id="update-pill" class="pill update-pill" hidden></button>
|
|
49
|
+
<button id="share-btn" class="pill share-btn">Share</button>
|
|
49
50
|
</div>
|
|
50
51
|
</header>
|
|
51
52
|
|
|
@@ -61,8 +62,9 @@
|
|
|
61
62
|
</button>
|
|
62
63
|
<button class="menu-item" data-mode="inbox">
|
|
63
64
|
<span class="radio"></span>
|
|
64
|
-
<span><b>The chat that launched Loop</b><small>Comments
|
|
65
|
+
<span><b>The chat that launched Loop</b><small>Comments go to that chat, with everything it already knows, and it replies here. If the chat isn't listening, comments wait for it.</small></span>
|
|
65
66
|
</button>
|
|
67
|
+
<div id="agent-allowed" class="agent-allowed" hidden></div>
|
|
66
68
|
</div>
|
|
67
69
|
|
|
68
70
|
<div id="share-menu" class="menu" hidden>
|
|
@@ -93,14 +95,15 @@
|
|
|
93
95
|
</div>
|
|
94
96
|
|
|
95
97
|
<nav id="toolbar" aria-label="Tools">
|
|
96
|
-
<button data-tool="select" title="Select
|
|
97
|
-
<button data-tool="
|
|
98
|
-
<button data-tool="pen" title="Draw (P)"><
|
|
99
|
-
<button data-tool="erase" title="Erase marks (E)"><
|
|
100
|
-
<button data-tool="
|
|
101
|
-
<button data-tool="comment" title="Point & comment (C)"><svg viewBox="0 0 24 24"><path d="M4 18.5V6.5A2.5 2.5 0 016.5 4h11A2.5 2.5 0 0120 6.5v8a2.5 2.5 0 01-2.5 2.5H8.5z"/></svg></button>
|
|
98
|
+
<div class="tb-group"><button id="move-btn" data-tool="select" title="Select (V)"><i data-i="select"></i></button><button class="tb-caret" id="move-caret" title="Select or Hand"><i data-i="down"></i></button></div>
|
|
99
|
+
<button data-tool="use" title="Interact (I)"><i data-i="point"></i></button>
|
|
100
|
+
<button data-tool="pen" title="Draw (P)"><i data-i="pen"></i></button>
|
|
101
|
+
<button data-tool="erase" title="Erase marks (E)"><i data-i="erase"></i></button>
|
|
102
|
+
<button data-tool="comment" title="Point & comment (C)"><i data-i="comment"></i></button>
|
|
102
103
|
<span class="tb-sep"></span>
|
|
103
|
-
<button id="
|
|
104
|
+
<button id="section-btn" data-tool="section" title="Section: drag to make a region that holds frames (⇧S)"><svg class="hi" viewBox="0 0 24 24"><rect x="3.5" y="8" width="17" height="12.5" rx="3"/><path d="M4.5 3.75h6.5"/></svg></button>
|
|
105
|
+
<button id="new-frame-btn" data-tool="frame" class="tb-new" title="Frame (F)"><i data-i="frame"></i><span>Frame</span></button>
|
|
106
|
+
<div id="frame-tray" class="frame-tray" aria-label="Frame size"></div>
|
|
104
107
|
</nav>
|
|
105
108
|
|
|
106
109
|
<aside id="frame-panel" class="panel" hidden>
|
|
@@ -111,25 +114,34 @@
|
|
|
111
114
|
<div id="align-bar" hidden></div>
|
|
112
115
|
|
|
113
116
|
<div id="zoom">
|
|
117
|
+
<button id="theme-btn" title="Light or dark (⇧D)"><i data-i="sun"></i></button>
|
|
114
118
|
<button id="zoom-out" title="Zoom out (−)">−</button>
|
|
115
119
|
<button id="zoom-level" title="Fit all (Shift+1)">100%</button>
|
|
116
120
|
<button id="zoom-in" title="Zoom in (+)">+</button>
|
|
117
121
|
</div>
|
|
118
122
|
|
|
119
123
|
<div id="composer" class="pop" hidden>
|
|
120
|
-
<div class="prompt-head"><
|
|
124
|
+
<div class="prompt-head"><b id="prompt-title">What should this screen be?</b></div>
|
|
121
125
|
<div class="prompt-ideas" id="prompt-ideas"></div>
|
|
122
|
-
<div class="
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
<
|
|
126
|
-
<span
|
|
127
|
-
<span class="hint"><kbd>↵</kbd> send · <kbd>⇧↵</kbd> new line · <kbd>esc</kbd></span>
|
|
126
|
+
<div class="cbox">
|
|
127
|
+
<div class="c-chips"><span id="composer-area" class="chip chip-area" hidden></span><span id="composer-ink" class="chip" hidden></span></div>
|
|
128
|
+
<div class="c-files" hidden></div>
|
|
129
|
+
<textarea id="composer-input" rows="1" placeholder="Ask for a change"></textarea>
|
|
130
|
+
<span class="hint" hidden></span>
|
|
128
131
|
</div>
|
|
129
132
|
</div>
|
|
130
133
|
|
|
131
134
|
<div id="thread" class="pop" hidden></div>
|
|
132
135
|
|
|
136
|
+
<aside id="comments-panel" hidden>
|
|
137
|
+
<div class="cp-head">
|
|
138
|
+
<b>Comments</b>
|
|
139
|
+
<div class="cp-tabs"><button data-tab="open" class="on">Open <span></span></button><button data-tab="resolved">Resolved <span></span></button></div>
|
|
140
|
+
<button class="cp-x" title="Close"><i data-i="x"></i></button>
|
|
141
|
+
</div>
|
|
142
|
+
<div class="cp-list"></div>
|
|
143
|
+
</aside>
|
|
144
|
+
|
|
133
145
|
<div id="ink-chip" hidden><button>Comment on marks</button></div>
|
|
134
146
|
|
|
135
147
|
<div id="new-frame" class="modal" hidden>
|
|
@@ -155,6 +167,11 @@
|
|
|
155
167
|
</div>
|
|
156
168
|
</div>
|
|
157
169
|
|
|
170
|
+
<div id="preset-menu" class="menu preset-menu" hidden></div>
|
|
171
|
+
<div id="tool-menu" class="menu tool-menu" hidden>
|
|
172
|
+
<button class="tm-item" data-pick-tool="select"><span class="tm-check"></span><i data-i="select"></i><span>Select</span><kbd>V</kbd></button>
|
|
173
|
+
<button class="tm-item" data-pick-tool="hand"><span class="tm-check"></span><i data-i="hand"></i><span>Hand</span><kbd>H</kbd></button>
|
|
174
|
+
</div>
|
|
158
175
|
<div id="toast" hidden></div>
|
|
159
176
|
<script src="/__loop/app/app.js" type="module"></script>
|
|
160
177
|
</body>
|
package/server.mjs
CHANGED
|
@@ -1,22 +1,32 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
2
|
+
import xt from"node:http";import $ from"node:fs";import _ from"node:fs/promises";import m from"node:path";import se from"node:os";import zt from"node:crypto";import{spawn as Te,execFile as Ht,execFileSync as Ut}from"node:child_process";import Bt from"node:net";import{fileURLToPath as Kt}from"node:url";var Jt=!1,ye=m.dirname(Kt(import.meta.url)),Ne=m.join(ye,"public"),K=m.join(ye,"cli.mjs"),$t=Wn(process.argv.slice(2)),O=m.resolve($t.dir||".loop"),de=m.dirname(O),We=m.join(O,"canvas.json"),vt=m.join(O,"server.json");$.mkdirSync(m.join(O,"frames"),{recursive:!0});var Oe=process.env.LOOP_CLAUDE_BIN||me("claude"),et=process.env.LOOP_CODEX_BIN||me("codex")||["/Applications/ChatGPT.app/Contents/Resources/codex-cli/bin/codex","/Applications/Codex.app/Contents/Resources/codex",m.join(se.homedir(),"Applications/ChatGPT.app/Contents/Resources/codex-cli/bin/codex")].find(e=>{try{return $.accessSync(e,$.constants.X_OK),!0}catch{return!1}}),fe={claude:{label:"Claude Code",bin:Oe},codex:{label:"Codex",bin:et}},Yt=["fable","opus","sonnet","haiku"],Xt=["low","medium","high","xhigh","max"],dt=0,lt=null;function Zt(){return Date.now()-dt<3e4?lt:(dt=Date.now(),lt=Vt())}function Vt(){let e=m.join(se.homedir(),".codex"),t=[],o={},i=null;try{let r=JSON.parse($.readFileSync(m.join(e,"models_cache.json"),"utf8"));for(let s of Array.isArray(r)?r:r.models||[]){let a=s.slug||s.id;!a||(s.visibility||"list")!=="list"||(t.push(a),o[a]=(s.supported_reasoning_levels||s.supported_reasoning_efforts||[]).map(l=>l?.effort||l).filter(l=>typeof l=="string"))}}catch{}try{i=/^\s*model_reasoning_effort\s*=\s*"([^"]+)"/m.exec($.readFileSync(m.join(e,"config.toml"),"utf8"))?.[1]||null}catch{}return{models:t,efforts:o,effort:i}}function $e(e=be()){let t=d.agent.pick?.[e]||{},o,i,r;if(e==="codex"){let a=Zt(),l=Qt();o={model:l,effort:a.effort},i=a.models,r=a.efforts[t.model||l]||["low","medium","high"]}else{let a=d.agent.chat?.runner==="claude"?d.agent.chat:{};o={model:process.env.LOOP_MODEL||a.model||null,effort:process.env.LOOP_EFFORT||a.effort||null},i=Yt,r=Xt}let s=t.effort||o.effort||null;return{runner:e,models:i,efforts:r,chat:o,pick:{model:t.model||null,effort:t.effort||null},model:t.model||o.model||null,effort:s&&r.includes(s)?s:e==="codex"?null:s}}function Qt(){if(process.env.LOOP_CODEX_MODEL)return process.env.LOOP_CODEX_MODEL;let e=m.join(se.homedir(),".codex"),t=null,o=[];try{t=/^\s*model\s*=\s*"([^"]+)"/m.exec($.readFileSync(m.join(e,"config.toml"),"utf8"))?.[1]||null}catch{}try{let i=JSON.parse($.readFileSync(m.join(e,"models_cache.json"),"utf8"));o=(Array.isArray(i)?i:i.models||[]).filter(r=>(r.visibility||"list")==="list").map(r=>r.slug||r.id).filter(Boolean)}catch{}return t&&(!o.length||o.includes(t))?t:o[0]||t||null}var R=(e=8)=>zt.randomBytes(e).toString("base64url").slice(0,e),d=qt()||{version:1,name:m.basename(de),token:R(16),agent:{mode:"headless"},frames:[],strokes:[],comments:[]},x=new Map,ne=null,le=null;function qt(){try{return JSON.parse($.readFileSync(We,"utf8"))}catch{return null}}function tt(){let e=We+".tmp";$.writeFileSync(e,JSON.stringify(d)),$.renameSync(e,We)}var Ge=null;function en(){clearTimeout(Ge),Ge=setTimeout(tt,120)}d.sections||=[];for(let e of d.frames)delete e.building;for(let e of d.comments)e.status==="working"&&(e.status="error",e.reply="Interrupted \u2014 the server restarted.");for(let e of d.strokes||[]){if(!e.frameId||e.fx!=null)continue;let t=d.frames.find(b=>b.id===e.frameId);if(!t)continue;e.fx=t.x,e.fy=t.y;let o=e.commentId&&d.comments.find(b=>b.id===e.commentId),i=o?.anchor;if(!i||i.scope==="area"||i.scope==="elements"||!Number.isFinite(i.x)||!Number.isFinite(i.y))continue;let r=d.strokes.filter(b=>b.commentId===o.id&&b.frameId===t.id),s=r.flatMap(b=>b.points.filter((j,I)=>I%2===0)),a=r.flatMap(b=>b.points.filter((j,I)=>I%2===1));if(!s.length)continue;let l=t.x+i.x-(Math.min(...s)+Math.max(...s))/2,p=t.y+i.y-(Math.min(...a)+Math.max(...a))/2;if(Math.hypot(l,p)<4)continue;let w=(b,j)=>s.filter((I,A)=>I+b>=t.x&&I+b<=t.x+t.w&&a[A]+j>=t.y&&a[A]+j<=t.y+t.h).length;if(!(w(l,p)<=w(0,0)))for(let b of r)b.fx=t.x-l,b.fy=t.y-p}var tn=(()=>{let e=(o,i)=>{try{return Ut(o,i,{encoding:"utf8",stdio:["ignore","pipe","ignore"],timeout:1500,cwd:m.dirname(O)}).trim()}catch{return""}},t=process.env.LOOP_NAME||(process.platform==="darwin"?e("id",["-F"]):"")||e("git",["config","user.name"]);if(!t)try{t=se.userInfo().username}catch{}return String(t||"").trim().slice(0,32)||"Host"})();function It(e){let t={};for(let[a,l]of x)t[a]={commentId:l.commentId,step:l.step,startedAt:l.startedAt};let{token:o,cloud:i,...r}=d,s=be();return{...r,hostName:d.hostName||tn,live:t,role:e,version:Ve,update:e==="owner"?V:void 0,emailSharing:!!G,chatListening:Le(),agentBin:s,agentLabel:fe[s].label,models:$e(s),runners:Object.fromEntries(Object.entries(fe).map(([a,l])=>[a,!!l.bin])),share:e==="owner"?Ze():void 0}}var ue="/__loop";function nn(e,t,o){let i=new URL(o),r={...e.headers,host:i.host},s=xt.request({hostname:i.hostname,port:i.port||80,method:e.method,path:e.url,headers:r},a=>{let l={...a.headers};typeof l.location=="string"&&l.location.startsWith(i.origin)&&(l.location=l.location.slice(i.origin.length)||"/"),delete l["x-frame-options"],l["content-security-policy"]&&(l["content-security-policy"]=String(l["content-security-policy"]).replace(/frame-ancestors[^;]*;?/i,"")),t.writeHead(a.statusCode||502,l),a.pipe(t)});s.on("error",a=>{if(t.headersSent)return t.end();t.writeHead(502,{"content-type":"text/html; charset=utf-8"}),t.end(`<body style="font:14px system-ui;color:#8b8880;display:grid;place-items:center;height:100vh;margin:0">The dev server at ${ve(o)} isn't responding (${ve(a.code||a.message)}). Start it, or run <code>loop scan</code>.</body>`)}),e.pipe(s)}var ze=new Set;function He(e,t){e.write(`data: ${JSON.stringify(t)}
|
|
3
3
|
|
|
4
|
-
`)}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
<
|
|
8
|
-
<body
|
|
9
|
-
|
|
10
|
-
`)}let
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
`))
|
|
15
|
-
`)
|
|
16
|
-
`))
|
|
17
|
-
`).
|
|
4
|
+
`)}var Tt=["frames","comments","strokes","sections"];function on(e,t){let o={};for(let[i,r]of Object.entries(t))o[i]=Tt.includes(i)&&Array.isArray(r)?new Map(r.map(s=>[s.id,JSON.stringify(s)])):JSON.stringify(r??null);e.last=o}function Ot(e){let t=It(e.role),o=e.last;if(!o)return on(e,t),{type:"state",state:t};let i={type:"patch"},r=!1;for(let[s,a]of Object.entries(t))if(Tt.includes(s)&&Array.isArray(a)){let l=o[s]instanceof Map?o[s]:new Map,p=new Map,w=[];for(let I of a){let A=JSON.stringify(I);p.set(I.id,A),l.get(I.id)!==A&&w.push(I)}let b=[...p.keys()],j=b.length!==l.size||b.join(`
|
|
5
|
+
`)!==[...l.keys()].join(`
|
|
6
|
+
`);(w.length||j)&&((i.lists||={})[s]={up:w,...j?{order:b}:{}},r=!0),o[s]=p}else{let l=JSON.stringify(a??null);o[s]!==l&&((i.set||={})[s]=a??null,o[s]=l,r=!0)}return r?i:null}function Z(e){for(let t of ze){if(e.type!=="state"){He(t.res,e);continue}let o=Ot(t);o&&He(t.res,o)}}var ft=null,G=null,Ue=new Set;function k(e){en(),clearTimeout(ft),ft=setTimeout(()=>Z({type:"state"}),16),e||G?.schedulePush();for(let t of[...Ue])t()}var Ce=new Map;function nt(e){if(e.kind!=="folder"||Ce.has(e.id))return;let t=null;try{let o=$.watch(e.src,{recursive:!0},(i,r)=>{if(!(r&&(r.startsWith(".")||r.includes("node_modules")))){if(clearTimeout(t),Me.add(e.id),x.get(e.id)?.inbox&&r&&B(e.id,`Editing ${m.basename(String(r))}`),e.building&&e.optionOf&&Date.now()-e.createdAt>1500){let s=d.comments.find(a=>a.id===e.optionOf.cid)?.options?.find(a=>a.frameId===e.id);s&&!s.touched&&(s.touched=!0,e.building="filling",k(),x.has(e.optionOf.from)&&B(e.optionOf.from,`Making option ${s.id}${s.placeholder?"":` \xB7 ${s.name}`}`))}t=setTimeout(()=>{Z({type:"frame-changed",frameId:e.id,live:x.has(e.id)}),x.has(e.id)||G?.frameChanged(e)},x.has(e.id)?450:200)}});Ce.set(e.id,o)}catch{}}function _e(e){Ce.get(e)?.close(),Ce.delete(e)}d.frames.forEach(nt);function Be(e=1280,t=800,o){let i=o&&d.frames.find(a=>a.id===o);if(i){let a=(w,b)=>d.frames.some(j=>w<j.x+j.w+60&&w+e+60>j.x&&b<j.y+j.h+60&&b+t+60>j.y),l=i.x+i.w+120,p=i.y;for(let w=0;w<50&&a(l,p);w++)l+=160;return{x:l,y:p}}if(!d.frames.length)return{x:0,y:0};let r=Math.max(...d.frames.map(a=>a.x+a.w)),s=Math.min(...d.frames.map(a=>a.y));return{x:r+120,y:s}}function rn(e){return(e||"frame").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"").slice(0,40)||"frame"}function sn(e){let t=e.split(/[?#]/)[0];return t==="/"||!t?"Home":t.split("/").filter(Boolean).map(o=>decodeURIComponent(o).replace(/\.html?$/,"").replace(/[-_]+/g," ").replace(/^\w/,i=>i.toUpperCase())).join(" / ").slice(0,60)}function q({src:e,route:t,name:o,w:i,h:r,x:s,y:a,near:l,dup:p}){if(i=+i||we.Desktop[0],r=+r||we.Desktop[1],t!=null){t="/"+String(t).replace(/^\/+/,"");let M=!p&&d.frames.find(C=>C.kind==="route"&&C.src===t&&C.w===i);if(M)return M;let L=s!=null&&a!=null?{x:+s,y:+a}:Be(i,r,l),E={id:R(6),name:o||t,kind:"route",src:t,entry:"",w:i,h:r,...L,createdAt:Date.now()};return d.frames.push(E),k(),E}let w=/^https?:\/\//.test(e),b=w?"url":"folder",j="index.html";w||(e=m.resolve(de,e),$.existsSync(e)&&$.statSync(e).isFile()&&(j=m.basename(e),e=m.dirname(e)));let I=s!=null&&a!=null?{x:+s,y:+a}:Be(i,r,l),A={id:R(6),name:o||(w?new URL(e).pathname:m.basename(e)),kind:b,src:e,entry:j,w:i,h:r,...I,createdAt:Date.now()};return d.frames.push(A),nt(A),k(),A}var ve=e=>String(e).replace(/[&<>"]/g,t=>({"&":"&","<":"<",">":">",'"':"""})[t]),an=e=>`<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
7
|
+
<title>${ve(e)}</title><style>html,body{height:100%;margin:0}body{display:grid;place-items:center;font:14px/1.4 ui-sans-serif,system-ui;color:#9a978f;background:#fbfaf7}</style></head>
|
|
8
|
+
<body><div>${ve(e)} \u2014 waiting for the agent\u2026</div></body></html>`,cn=e=>`<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
9
|
+
<meta name="loop-empty" content="1"><title>${ve(e)}</title><style>html,body{height:100%;margin:0;background:#fff}</style></head>
|
|
10
|
+
<body></body></html>`;function ge(e){let t=rn(e),o=m.join(O,"frames",t),i=2;for(;$.existsSync(o);)o=m.join(O,"frames",`${t}-${i++}`);return o}function he(e){let t=new Set(d.frames.map(o=>o.name));if(!t.has(e))return e;for(let o=2;;o++)if(!t.has(`${e} ${o}`))return`${e} ${o}`}function ut(e,t=!1){let o=ge(e);return $.mkdirSync(o,{recursive:!0}),$.writeFileSync(m.join(o,"index.html"),t?cn(e):an(e)),o}var jt=new Set(["node_modules",".git",".loop",".next","dist","build"]);async function re(e,t,o={bytes:3e7}){await _.mkdir(t,{recursive:!0});for(let i of await _.readdir(e,{withFileTypes:!0})){if(i.name.startsWith(".")||jt.has(i.name))continue;let r=m.join(e,i.name),s=m.join(t,i.name);if(i.isDirectory())await re(r,s,o);else if(i.isFile()){let{size:a}=await _.stat(r);if((o.bytes-=a)<0)throw new Error("frame too large to snapshot");await _.copyFile(r,s)}}}async function De(e,t=e,o=new Set){for(let i of await _.readdir(e,{withFileTypes:!0})){if(i.name.startsWith(".")||jt.has(i.name))continue;let r=m.join(e,i.name);i.isDirectory()?await De(r,t,o):o.add(m.relative(t,r))}return o}function ot(e,t,o){for(let i of $.readdirSync(e,{withFileTypes:!0})){if(i.name.startsWith("."))continue;let r=m.join(e,i.name);i.isDirectory()?ot(r,t,o):o.add(m.relative(t,r))}}var ht=new Map;async function it(e,t){if(e.kind!=="folder")return null;let o=m.join(O,"history",t);try{return await re(e.src,o),o}catch{return null}}async function je(e,t){if(e.kind!=="folder")return null;let o=m.join(O,"history",t);try{return await _.rm(o,{recursive:!0,force:!0}),await re(e.src,o),o}catch{return null}}async function Ae(e,t){let o=await De(e.src),i=await De(t);for(let r of o)i.has(r)||await _.rm(m.join(e.src,r),{force:!0});await re(t,e.src)}function mt(e,t={}){let o=t.file_path||t.path||t.notebook_path,i=o?m.basename(o):"",r=String(t.command||"");switch(e){case"Read":return o&&/\.(png|jpe?g|webp|gif)$/i.test(o)?o.includes(`${m.sep}shots${m.sep}`)?"Looking at the screen":o.includes(`${m.sep}annotations${m.sep}`)?"Looking at your marks":"Looking at your reference":i?`Reading ${i}`:"Reading the code";case"Edit":case"MultiEdit":return i?`Editing ${i}`:"Editing";case"Write":return i?`Writing ${i}`:"Writing";case"Glob":case"Grep":return"Searching the code";case"Bash":return/cli\.mjs"? shot\b|\bloop shot\b/.test(r)?"Taking a screenshot":/cli\.mjs"? (new|add-route)\b/.test(r)?"Adding a frame":/cli\.mjs"? option\b/.test(r)?"Setting up an option":r?"Running a command":"Working\u2026";case"TodoWrite":return"Planning";case"WebFetch":case"WebSearch":return"Looking something up";default:return e}}var we={Mobile:[393,852],Tablet:[834,1194],Desktop:[1440,1024]},rt=e=>e.w<=480?"Mobile":e.w>=1100?"Desktop":e.h>=e.w*1.15?"Tablet":"Custom-size";function Je(e){let t=d.frames.filter(o=>o.id!==e?.id);return["","How Loop canvases work (follow these conventions):",`- A frame is one screen at one device size. Standard sizes: Mobile ${we.Mobile.join("\xD7")}, Tablet ${we.Tablet.join("\xD7")}, Desktop ${we.Desktop.join("\xD7")}. When someone asks for a "mobile", "phone", "tablet", "iPad", "desktop" or "web" version, use the matching size.`,"- Change only the project (or this frame's folder). Never edit Loop itself: its canvas, toolbar, menus or .loop/ files are not yours to change.","- A frame IS the device screen: design edge to edge at that size. Never draw a phone or laptop mockup, notch, status bar, home indicator or bezel inside a frame, and never letterbox a small layout in the middle of a big frame.",'- "A mobile version of this" means the same screen, content and copy, re-laid-out for that width: single column, stacked sections, a mobile navigation pattern (top bar, tab bar or menu), tap targets of at least 44px, readable type. Keep the same colours, fonts and components.','- Name frames "<Screen> \u2014 <Device>", e.g. "Settings \u2014 Mobile". New frames go next to the frame they came from.',"- Keep frames consistent with each other: reuse the visual style already on the canvas.",...t.length?["- Other frames on this canvas: "+t.map(o=>`"${o.name}" (${rt(o)} ${o.w}\xD7${o.h}${o.kind==="route"?`, app route ${o.src}`:""})`).join("; ")+"."]:[]]}function st(e,t){let o=[];if(t.kind==="create")return o.push(`On my Loop canvas, design a new screen called "${e.name}" (${rt(e)}, ${e.w}\xD7${e.h}):`,"",t.text,"","Write it as a complete index.html in the current working directory (self-contained HTML/CSS/JS; CDN imports and web fonts are fine, no build step), real content and working interactions where they make sense.","","Design it the way you would if I had asked you directly in a chat: make real design decisions (layout, hierarchy, type, colour, detail) so it looks finished, not like a wireframe.",`Look at it as you go: \`node ${K} shot ${e.id} --dir ${O}\` saves a screenshot of the frame and prints its path. Check it after your first pass and iterate until it looks right at ${e.w}\xD7${e.h}.`,...Je(e),"","When you're done, tell me briefly what you made."),o.join(`
|
|
11
|
+
`);if(e.kind==="canvas"){o.push("You are the design agent behind Loop, a canvas of live frames for the project in the current working directory. The user left a general comment on the canvas itself (not on a single frame).","","Frames on the canvas:",...d.frames.map(r=>`- "${r.name}": ${r.kind==="folder"?`static files in ${r.src}`:r.kind==="frozen"?`a frozen snapshot of ${r.from||"an app screen"} (read-only)`:r.kind==="route"?`the app's ${r.src} route (dev server ${d.proxy})`:r.src}`));let i=t.anchor?.near;return t.anchor?.area&&i?.length?o.push("",`They drew a box on the canvas over: ${i.map(r=>`"${r}"`).join(", ")}. Those frames are the focus; the box is approximate.`):i?.length&&o.push("",`The comment was placed near: ${i.map(r=>`"${r}"`).join(", ")}.`),o.push(...Je(null)),o.push("","Comment:",'"""',t.text,'"""',"",`To add a frame: \`node ${K} new "<Screen> \u2014 <Device>" --w <w> --h <h> --dir ${O}\` prints a folder; write a self-contained index.html there.`,At.slice(2),"Decide which frames or source files it concerns and make the change. If it is a question or a note that needs no change, just answer it. When done, reply to the user the way you would in a chat: a short, plain summary of what you changed (one to three sentences). If they asked a question, answer it properly. If something was ambiguous, say what you assumed."),o.join(`
|
|
12
|
+
`)}return mn(_t(e,t))}var dn=(e,t)=>{let o=`\`node ${K} shot ${e.id} --dir ${O}\``,i=fn(t)||Ie.has(t.id)&&t.shotSoon;return["Work on this the way you would if I had asked you directly in a chat:",i?`- This is the screen as I see it right now: ${i} (look at it first, along with the relevant code). No need to take that screenshot yourself.`:`- Understand it first: read the relevant code, and look at the screen as I see it (${o} saves a screenshot and prints its path).`,`- Match the effort to what I asked. A small, specific fix (wording, a colour, spacing, one element): just make it, precisely, and don't spend time on more screenshots. Design work, a layout change, or anything vague ("make it nicer", "this feels off"): make real design decisions (hierarchy, spacing, type, colour, detail), then check the result with a screenshot (`+o+") and iterate until it looks right.","- Keep it consistent with the rest of the screen and canvas unless I'm asking you to change the style.","- When you're done, reply like you would in a chat: what you changed, briefly, and anything I should know. Answer questions properly.",At]},At="- If a command or tool is refused, don't look for another way to do the same thing. Finish what you can and say in your reply what you needed and why: I'll see it with an Allow button.",ae=new Map,Ie=new Map;function ln(e,t){if(!Fe||!X||t.kind==="canvas"||t.id===ke)return;let o=ae.get(t.id)||0;e.shotSoon=m.join(O,"shots",`${t.id}-${e.id}.png`);let i=ce(t,void 0,m.basename(e.shotSoon)).then(r=>{e.shot=r,e.shotRev=o}).catch(()=>{e.shotSoon=null}).finally(()=>Ie.delete(e.id));Ie.set(e.id,i)}var fn=e=>e.shot&&e.shotRev===(ae.get(e.frameId)||0)&&$.existsSync(e.shot)?e.shot:null,un=e=>Ie.has(e.id)?Promise.race([Ie.get(e.id),new Promise(t=>setTimeout(t,3e3))]):null;function _t(e,t){let o=[],i=t.parentId?d.comments.filter(l=>(l.id===t.parentId||l.parentId===t.parentId)&&l.id!==t.id&&l.createdAt<t.createdAt):[];if(o.push(`On my Loop canvas I'm looking at "${e.name}", a live ${rt(e)} screen (${e.w}\xD7${e.h}).`),e.kind==="folder"?o.push(`It's a static page: its files are in the current working directory (entry: ${e.entry}).`):e.kind==="frozen"?o.push(`It's a frozen snapshot of ${e.from?`the app's "${e.from}" screen`:"an app screen"}, kept as the "before". Leave the snapshot alone and change the app's source for that screen; its live frames show the result.`):e.kind==="route"?o.push(`It's the app's "${e.src}" route, running live from the project in the current working directory (dev server ${d.proxy}, hot reload; don't restart it).`):o.push(`It's a live page at ${e.src}, served from the project in the current working directory.`),e.kind==="folder")try{$.readFileSync(m.join(e.src,e.entry),"utf8").includes('name="loop-empty"')&&o.push(`It's empty right now: design the whole screen from scratch at exactly ${e.w}\xD7${e.h} in index.html, and remove the loop-empty meta tag.`)}catch{}if(i.length){o.push("","What we said so far about this:");for(let l of i)o.push(l.kind==="note"?`- You: ${l.text}`:`- Me: ${l.text}${l.reply&&l.status!=="interrupted"?`
|
|
13
|
+
- You: ${l.reply}`:l.status==="interrupted"?`
|
|
14
|
+
- (you were working on that when I interrupted)`:""}`)}t.steeredFrom&&o.push("","I interrupted you to add this. The files may already have part of that work in them: carry on from where things are and do what both messages ask (this one wins where they conflict)."),o.push("",t.text);let r=t.anchor,s=[];if(r?.scope==="area")s.push(`I highlighted an area: x=${Math.round(r.x)}, y=${Math.round(r.y)}, ${Math.round(r.w)}\xD7${Math.round(r.h)}px in the ${e.w}\xD7${e.h} viewport. It's roughly where to look, not a strict boundary.`),r.elements?.length&&s.push("Inside it: "+r.elements.map(l=>`<${l.tag}> \`${l.selector}\`${l.text?` ("${l.text}")`:""}`).join("; ")+"."),r.container&&s.push(`Smallest element wrapping it: <${r.container.tag}> \`${r.container.selector}\`.`);else if(r?.scope==="elements"&&r.elements?.length){s.push(`I picked ${r.elements.length===1?"this exact element":`these ${r.elements.length} exact elements`}:`);for(let l of r.elements.slice(0,10))s.push(`- <${l.tag}> \`${l.selector}\`${l.text?` ("${l.text}")`:""}`),l.html&&s.push(" ```html"," "+l.html.replace(/\n/g,`
|
|
15
|
+
`)," ```")}else r?.scope==="frame"?s.push("I mean the whole screen."):r&&r.selector?(s.push(`My comment pin is on <${r.tag}> \`${r.selector}\`${r.text?` ("${r.text}")`:""}. It may be about that element or the area around it.`),r.html&&s.push("```html",r.html,"```")):r&&s.push(`I pointed at x=${Math.round(r.x)}, y=${Math.round(r.y)} in the viewport.`);t.ink?.length&&s.push(`I drew marks over: ${t.ink.map(l=>`\`${l.selector}\`${l.text?` ("${l.text}")`:""}`).join(", ")}.`),t.annotation&&s.push(`A picture of my marks over the screen: ${t.annotation} (look at it).`),t.attachments?.length&&s.push(`I attached ${t.attachments.length===1?"a reference":`${t.attachments.length} references`}; look at ${t.attachments.length===1?"it":"them"} first: ${t.attachments.map(l=>`${l.path} (${l.type})`).join(", ")}.`);let a=hn(e,r);return a&&s.push(a),s.length&&o.push("",...s),o.push("",...dn(e,t),"",`(This message's id, for Loop commands: ${t.id})`),{message:o.join(`
|
|
16
|
+
`),reference:Pt(e)}}function hn(e,t){if(e.kind!=="folder"||!t)return"";let o;try{o=$.readFileSync(m.join(e.src,e.entry||"index.html"),"utf8")}catch{return""}let i=[...(t.elements||[]).map(s=>s.html),t.html,t.container?.html].filter(Boolean),r=[];for(let s of i){let a=/^<[^>]+>/.exec(s)?.[0];if(!a)continue;let l=o.indexOf(a);l<0||o.indexOf(a,l+1)>=0||r.push(o.slice(0,l).split(`
|
|
17
|
+
`).length)}return r.length?`It is in ${e.entry||"index.html"} at line ${[...new Set(r)].sort((s,a)=>s-a).join(", ")}: open it there, no need to search for it.`:""}function Pt(e){let t=[],[o,i]=we.Mobile;t.push("You are working inside Loop, a canvas of live frames the user reviews and comments on. Their comments reach you as messages."),t.push("","New frames: if they ask for a new frame (a mobile/desktop version, or a separate copy to keep side by side), leave this frame as it is and add one next to it:"),e.kind==="route"?t.push(`- The same app screen at another size: \`node ${K} add-route ${e.src} --name "<Screen> \u2014 Mobile" --w ${o} --h ${i} --near ${e.id} --dir ${O}\`. It's the real app, so if it doesn't fit that width, fix the responsive styles in the source.`,`- A brand-new static design: \`node ${K} new "<Screen> \u2014 <Device>" --w <w> --h <h> --near ${e.id} --dir ${O}\` prints a folder; write a complete self-contained index.html there.`):t.push(`- \`node ${K} new "<Screen> \u2014 <Device>" --w <w> --h <h> --near ${e.id} --dir ${O}\` (e.g. --w ${o} --h ${i} for mobile) prints a folder; write a complete, self-contained index.html there, based on this frame and adapted to that size.`);let r=e.kind==="folder"?e.src:de;return e.kind==="folder"?t.push("",`Options: if they ask for options to choose from (options, ideas, alternatives, variations), leave this frame's files alone. Make 2 to 4 genuinely different options (as many as they asked for). Each message ends with its id; for each option run \`node ${K} option <message id> "<short name>" --dir ${O}\`. It prints a folder that is a full copy of this frame and is already on the canvas as its own frame beside this one: make that option's changes there, editing its files in place. They watch each one fill in as you work, so start the first option early and finish one before starting the next. \`node ${K} shot ${e.id} --option <message id>:A --dir ${O}\` shows how option A looks. Reply with a line per option on how it differs.`):t.push("",`Options: if they ask for options to choose from (options, ideas, alternatives, variations), don't change the real files. Make 2 to 4 genuinely different options. Each message ends with its id; for each option, \`node ${K} option <message id> "<short name>" --dir ${O}\` prints an empty folder: write the complete new version of every file that option changes there, at the same path relative to ${r}. They preview the options live in the frame and pick one; reply with a line per option on how it differs.`),e.kind==="folder"&&t.push("","This frame is self-contained: keep its files in its folder (CDN imports are fine, no build step)."),t.push(...Je(e)),t.join(`
|
|
18
|
+
`)}function mn(e){return e.reference?`${e.message}
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
`),o?.length&&m.write(o),m.pipe(t),t.pipe(m)});m.on("error",()=>t.destroy()),t.on("error",()=>m.destroy())});Ne&&(T={createCloud:()=>null}.createCloud({state:c,changed:k,pump:N,broadcast:A,log:e=>console.log(e)}));var $e=!1;setInterval(()=>{let e=Z();if(e!==$e&&($e=e,k(!0)),c.agent.mode==="inbox"&&!e){for(let[t,o]of y)if(o.inbox){y.delete(t);let i=c.comments.find(r=>r.id===o.commentId);i&&i.status==="working"&&(i.status="queued")}ge()}},5e3);function Me(e,t=20){K.once("error",o=>{o.code==="EADDRINUSE"&&t>0?Me(e+1,t-1):(console.error(o),process.exit(1))}),K.listen(e,"0.0.0.0",()=>{G=K.address().port,v.writeFileSync(Ee,JSON.stringify({port:G,pid:process.pid})),console.log(`Loop \u25B8 http://localhost:${G} (${I})`),ge()})}function ye(){try{v.rmSync(Ee,{force:!0})}catch{}_?.kill();for(let e of y.values())e.child?.kill("SIGTERM");clearTimeout(oe),Oe(),process.exit(0)}process.on("SIGINT",ye);process.on("SIGTERM",ye);Me(+je.port||4848);function nt(e){let t={};for(let o=0;o<e.length;o++)if(e[o].startsWith("--")){let i=e[o].slice(2);t[i]=e[o+1]&&!e[o+1].startsWith("--")?e[++o]:!0}return t}function q(e){let t=[...(process.env.PATH||"").split(":"),f.join(Q.homedir(),".local/bin"),"/opt/homebrew/bin","/usr/local/bin"];for(let o of t){if(!o)continue;let i=f.join(o,e);try{return v.accessSync(i,v.constants.X_OK),i}catch{}}return null}export{W as PRESETS};
|
|
20
|
+
---
|
|
21
|
+
How Loop works (reference):
|
|
22
|
+
${e.reference}`:e.message}var ke="__canvas";function z(e){return e!==ke?d.frames.find(t=>t.id===e):(d.canvasFrame||={id:ke,name:"Canvas",kind:"canvas",src:de,entry:"",w:0,h:0},d.canvasFrame)}var Ke=0,Le=()=>Date.now()-Ke<45e3;function pn(e){let t=x.get(e.frameId),o=t&&d.comments.find(s=>s.id===t.commentId),i=e.parentId||e.id;if(e.status!=="queued"||!o||o.id!==i&&o.parentId!==i)return!1;e.steeredFrom=o.id,d.comments.splice(d.comments.indexOf(e),1);let r=d.comments.findIndex(s=>s.frameId===e.frameId&&s.status==="queued");return d.comments.splice(r<0?d.comments.length:r,0,e),t.steered=!0,t.canceled=!0,t.child?t.child.kill("SIGTERM"):Y(o,"canceled","Stopped."),k(),!0}function Et(e){return e.kind==="folder"?e.src:de}async function pt(e){let t=new Set;try{await De(e,e,t)}catch{}return[...t]}async function gt(e,t){let o=z(e.frameId);if(!o)throw new Error("no frame");if(e.optionChosen)throw new Error("an option was already chosen");let i=Et(o),r=m.join(O,"options",e.id,"_original"),s=new Set;for(let p of e.options||[])for(let w of await pt(p.dir))s.add(w);if(!$.existsSync(r)){await _.mkdir(r,{recursive:!0});let p=[];for(let w of s){let b=m.join(i,w);oe(i,b)&&($.existsSync(b)?(await _.mkdir(m.dirname(m.join(r,w)),{recursive:!0}),await _.copyFile(b,m.join(r,w))):p.push(w))}await _.writeFile(m.join(r,".missing.json"),JSON.stringify(p))}let a=JSON.parse(await _.readFile(m.join(r,".missing.json"),"utf8").catch(()=>"[]"));for(let p of s){let w=m.join(i,p);oe(i,w)&&(a.includes(p)?await _.rm(w,{force:!0}):$.existsSync(m.join(r,p))&&await _.copyFile(m.join(r,p),w))}let l=t&&(e.options||[]).find(p=>p.id===t);if(t&&!l)throw new Error("no such option");if(l)for(let p of await pt(l.dir)){let w=m.join(i,p);oe(i,w)&&(await _.mkdir(m.dirname(w),{recursive:!0}),await _.copyFile(m.join(l.dir,p),w))}e.optionApplied=l?l.id:null,k(),Z({type:"frame-changed",frameId:o.id})}var Fe=process.env.LOOP_CHROME||["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome","/Applications/Chromium.app/Contents/MacOS/Chromium","/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge","/Applications/Brave Browser.app/Contents/MacOS/Brave Browser",m.join(se.homedir(),"Applications/Google Chrome.app/Contents/MacOS/Google Chrome")].find(e=>$.existsSync(e))||me("google-chrome")||me("chromium")||me("chromium-browser");function gn(e){return e.kind==="folder"||e.kind==="frozen"?`http://127.0.0.1:${X}/__loop/f/${e.id}/${encodeURIComponent(e.entry||"index.html")}`:e.kind==="route"?`http://127.0.0.1:${X}${e.src}`:e.src}var W=null;function Ye(){let e=W;if(W=null,!!e){clearTimeout(e.idle);try{e.child.kill("SIGKILL")}catch{}$.rm(e.profile,{recursive:!0,force:!0},()=>{})}}async function Ct(){if(W)return clearTimeout((await W).idle),W;W=(async()=>{let t=$.mkdtempSync(m.join(se.tmpdir(),"loop-shot-")),o=Te(Fe,["--headless=new","--use-angle=swiftshader","--enable-unsafe-swiftshader","--ignore-gpu-blocklist","--hide-scrollbars","--no-first-run","--no-default-browser-check","--mute-audio",`--user-data-dir=${t}`,"--remote-debugging-port=0","--window-size=1280,900","about:blank"],{stdio:"ignore"}),i={child:o,profile:t,port:null,idle:null};o.on("exit",()=>{W&&(W===e||W===i)&&(W=null,$.rm(t,{recursive:!0,force:!0},()=>{}))});for(let r=0;r<100&&!i.port;r++)try{i.port=$.readFileSync(m.join(t,"DevToolsActivePort"),"utf8").split(`
|
|
23
|
+
`)[0]}catch{await new Promise(s=>setTimeout(s,50))}if(!i.port){try{o.kill("SIGKILL")}catch{}throw new Error("Chrome did not start")}return i})();let e=W;try{let t=await e;return W===e&&(W=t),t}catch(t){throw W===e&&(W=null),t}}var xe=new Map,Me=new Set;async function wn(e){let t=`${e.w}x${e.h}`,o=xe.get(e.id);if(o&&!(o instanceof Promise)&&o.size===t&&!Me.has(e.id)&&$.existsSync(o.file))return o.file;if(o instanceof Promise)return(await o).file;Me.delete(e.id);let i=ce(e).then(r=>{let s=xe.get(e.id),a={file:r,size:t};return xe.set(e.id,a),a}).catch(r=>{throw xe.delete(e.id),r});return xe.set(e.id,i),(await i).file}var wt=Promise.resolve();function ce(e,t,o){let i=wt.then(()=>yn(e,t,o));return wt=i.catch(()=>{}),i}async function yn(e,t,o){if(!Fe)throw new Error("No Chrome, Edge, Brave or Chromium found for screenshots (set LOOP_CHROME to one).");let i=globalThis.WebSocket;if(!i)throw new Error("Screenshots need Node 22 or newer");let r=m.join(O,"shots");$.mkdirSync(r,{recursive:!0});let s=m.join(r,o||`${e.id}-${Date.now()}${R(3)}.png`),a=Math.round(e.w),l=Math.round(e.h),p=await Ct();p.busy=(p.busy||0)+1;let w=null,b=null;try{for(let C=0;C<40&&!w;C++)try{w=await(await fetch(`http://127.0.0.1:${p.port}/json/new?about:blank`,{method:"PUT"})).json()}catch{await new Promise(F=>setTimeout(F,50))}if(!w?.webSocketDebuggerUrl)throw new Error("Chrome tab not found");b=new i(w.webSocketDebuggerUrl),await new Promise((C,F)=>{b.onopen=C,b.onerror=()=>F(new Error("DevTools connection failed"))});let j=0,I=new Map,A=null;b.onmessage=C=>{let F=JSON.parse(C.data);F.id&&I.has(F.id)?(I.get(F.id)(F),I.delete(F.id)):F.method==="Page.loadEventFired"&&A?.()};let M=(C,F={})=>new Promise((N,J)=>{let Se=++j;I.set(Se,N),setTimeout(()=>J(new Error("Screenshot timed out")),3e4),b.send(JSON.stringify({id:Se,method:C,params:F}))});await M("Page.enable"),await M("Page.bringToFront"),await M("Emulation.setDeviceMetricsOverride",{width:a,height:l,deviceScaleFactor:2,mobile:!1});let L=new Promise(C=>{A=C,setTimeout(C,12e3)});await M("Page.navigate",{url:t||gn(e)}),await L,await Promise.race([M("Runtime.evaluate",{awaitPromise:!0,expression:"(async () => { try { await document.fonts.ready; } catch {} await Promise.all([...document.images].filter(i => !i.complete).map(i => new Promise(r => { i.onload = i.onerror = r; }))); await new Promise(r => setTimeout(r, 250)); })()"}),new Promise(C=>setTimeout(C,2500))]);let E=await M("Page.captureScreenshot",{format:"png",clip:{x:0,y:0,width:a,height:l,scale:1}});if(!E.result?.data)throw new Error("Screenshot failed");return $.writeFileSync(s,Buffer.from(E.result.data,"base64")),s}catch(j){throw w||Ye(),j}finally{try{b?.close()}catch{}w?.id&&fetch(`http://127.0.0.1:${p.port}/json/close/${w.id}`).catch(()=>{}),p.busy--,clearTimeout(p.idle),p.busy||(p.idle=setTimeout(()=>{W===p&&!p.busy&&Ye()},4*6e4))}}function ie(e){if(x.has(e))return;let t=d.comments.find(o=>o.frameId===e&&o.status==="queued"&&(d.agent.mode!=="inbox"||o.background));t&&Lt(t)}function at(){for(let e of d.frames)ie(e.id);ie(ke)}var kn=new Set(["Reading the code","Editing","Writing","Working\u2026","Setting up an option"]);function B(e,t,o){let i=x.get(e);if(!i)return;i.step=t;let r=o==null&&t&&d.comments.find(s=>s.id===i.commentId);if(r&&!/^(Writing…|Reading your comment…|Your chat picked this up|Working…)$/.test(t)){let s=r.steps||=[],a=s[s.length-1];!kn.has(t)&&(!a||a.s!==t)&&(s.push({t:Date.now(),s:t}),s.length>60&&s.splice(0,s.length-60))}Z({type:"activity",frameId:e,commentId:i.commentId,step:t,...o!=null?{partial:o}:{}}),G?.liveChanged(x)}async function Dt(e){let t=z(e.frameId);if(!(!t||t.kind!=="folder"||!e.options?.length||!X)){if(!e.origThumb&&!e.optionApplied)try{e.origThumb=`/__loop/shots/${m.basename(await ce(t))}`,k()}catch{}for(let o of e.options)try{let i=await ce(t,`http://127.0.0.1:${X}/__loop/optview/${e.id}/${o.id}/${encodeURIComponent(t.entry||"index.html")}`);o.thumb=`/__loop/shots/${m.basename(i)}`,k()}catch{}}}var H={GAP:60,PAD:48,TOP:72},Pe=(e,t)=>t*e.w+(t-1)*H.GAP+H.PAD*2;function bn(e){let t=String(e||""),o=/\b(\d|two|three|four|five|six|a couple of|couple of|a few|few|some|several|more|other|different)\s+(?:[\w-]+\s+){0,3}?(options?|variations?|variants?|alternatives?|versions|directions|takes|ideas|explorations?)\b/i.exec(t);if(o){let i=o[1].toLowerCase(),r={two:2,three:3,four:4,five:5,six:6,"a couple of":2,"couple of":2,"a few":3,few:3,some:2,several:3,more:2,other:2,different:2}[i]??+i;return Math.max(1,Math.min(4,r))}return/\b(options|variations|variants|alternatives|explorations)\b/i.test(t)&&/\b(give|make|create|show|try|explore|generate|come up|want|need|can (we|you)|let'?s|what about)\b/i.test(t)?2:/\b(another|a new|one more|a different|an alternative|a second)\s+(version|variation|variant|option|take|direction|design|layout)\b|\bduplicate (this|it)\b|\b(make|create) a copy\b/i.test(t)?1:0}function yt(e,t,o=new Set){let i=a=>a.y<e.y+e.h&&a.y+a.h>e.y&&a.x+a.w/2>t.x+t.w,r=[...d.frames.filter(a=>a.id!==t.id&&!o.has(a.id)&&i(a)),...d.sections.filter(a=>!o.has(a.id)&&i(a))];if(!r.length)return;let s=e.x+e.w+H.GAP-Math.min(...r.map(a=>a.x));if(s>0)for(let a of r)a.x+=s}function ct(e,t,o){let i=d.sections.find(r=>r.id===e.optionSection);if(i){if(o>(i.slots||0)){let r=new Set([i.id,...(e.options||[]).map(s=>s.frameId)]);yt({x:i.x,y:i.y,w:Pe(t,o),h:i.h},t,r),i.w=Pe(t,o),i.slots=o}}else{let r={x:t.x+t.w+H.GAP+20,y:t.y-H.TOP,w:Pe(t,o),h:t.h+H.TOP+H.PAD};yt(r,t),i={id:R(6),name:`Options \xB7 ${t.name}`,...r,optionsFor:e.id,slots:o,createdAt:Date.now()},d.sections.push(i),e.optionSection=i.id}return i}function Sn(){let e=[...x.entries()].filter(([s])=>s!==ke);if(e.length!==1)return null;let[t,o]=e[0],i=z(t),r=d.comments.find(s=>s.id===o.commentId);return i&&r&&i.kind==="folder"&&!i.optionOf?{c:r,f:i}:null}function xn(e,t,o,i){e.options||=[];let r=String.fromCharCode(65+e.options.length),s=e.options.length,a={id:r,name:i||`Option ${r}`};e.options.push(a);let l=ct(e,t,Math.max(s+1,d.sections.find(w=>w.id===e.optionSection)?.slots||0)),p=q({src:o,name:he(i||`Option ${r}`),w:t.w,h:t.h,x:l.x+H.PAD+s*(t.w+H.GAP),y:t.y});return p.optionOf={cid:e.id,oid:r,from:t.id},p.building=!0,a.dir=o,a.frameId=p.id,a.madeAt=Date.now(),a.touched=!0,k(),p}async function kt(e,t,o){if(e.options||=[],e.options.length>=6)throw new Error("up to 6 options");let i=String.fromCharCode(65+e.options.length),r=e.options.length,s={id:i,name:o||`Option ${i}`,placeholder:!o};e.options.push(s);let a=ct(e,t,Math.max(r+1,d.sections.find(w=>w.id===e.optionSection)?.slots||0)),l=ge(`${t.name} option ${i} ${R(3)}`);await re(t.src,l);let p=q({src:m.join(l,t.entry||"index.html"),name:he(o?`${i} \xB7 ${o}`:`Option ${i}`),w:t.w,h:t.h,x:a.x+H.PAD+r*(t.w+H.GAP),y:t.y});return p.optionOf={cid:e.id,oid:i,from:t.id},p.building=!0,s.dir=l,s.frameId=p.id,s.madeAt=Date.now(),k(),s}function $n(e){if(e.touched)return!0;try{let t=new Set;return ot(e.dir,e.dir,t),[...t].some(o=>$.statSync(m.join(e.dir,o)).mtimeMs>(e.madeAt||0)+800)}catch{return!1}}function vn(e){let t=(e.options||[]).filter(a=>a.frameId);if(!t.length)return;let o=z(e.frameId),i=t.filter(a=>!a.placeholder&&$n(a)),r=new Set(t.filter(a=>!i.includes(a)).map(a=>a.frameId));for(let a of r)_e(a);d.frames=d.frames.filter(a=>!r.has(a.id)),e.options=e.options.filter(a=>!a.frameId||i.includes(a));let s=d.sections.find(a=>a.id===e.optionSection);i.forEach((a,l)=>{let p=d.frames.find(w=>w.id===a.frameId);p&&(delete p.building,s&&o&&(p.x=s.x+H.PAD+l*(o.w+H.GAP)))}),s&&!i.length?(d.sections=d.sections.filter(a=>a!==s),delete e.optionSection):s&&o&&(s.w=Pe(o,i.length),s.slots=i.length)}async function In(e){let t=z(e.frameId),o=(e.options||[]).filter(s=>s.files?.length&&!s.frameId);if(!t||t.kind!=="folder"||!o.length)return!1;let i=ct(e,t,o.length),r=0;for(let s of o){let a=ge(`${t.name} ${s.id} ${s.name}`);try{await re(t.src,a),await re(s.dir,a)}catch{continue}let l=q({src:m.join(a,t.entry||"index.html"),name:he(`${s.id} \xB7 ${s.name}`),w:t.w,h:t.h,x:i.x+H.PAD+r++*(t.w+H.GAP),y:t.y});l.optionOf={cid:e.id,oid:s.id,from:t.id},s.frameId=l.id}return k(),r>0}function Y(e,t,o){if(vn(e),e.options?.length)for(let s of e.options)s.files=(()=>{try{let a=new Set;return ot(s.dir,s.dir,a),[...a]}catch{return[]}})();e.options?.length&&(e.options=e.options.filter(s=>s.files.length)),e.options?.some(s=>!s.frameId)&&t==="done"&&setTimeout(async()=>{await In(e).catch(()=>!1)||Dt(e)},50);let i=x.get(e.frameId);x.delete(e.frameId),e.status=i?.steered?"interrupted":i?.canceled?"canceled":t,e.reply=i?.steered?"Interrupted to take your next message.":i?.canceled?"Stopped.":o,e.finishedAt=Date.now(),ae.set(e.frameId,(ae.get(e.frameId)||0)+1),Me.add(e.frameId),k(),Z({type:"frame-changed",frameId:e.frameId,done:!0,commentId:e.id,status:e.status});let r=d.frames.find(s=>s.id===e.frameId);r&&G?.frameChanged(r),G?.liveChanged(x),setTimeout(()=>ie(e.frameId),50)}function be(){let e=d.agent.runner;return e&&fe[e]?.bin?e:Oe?"claude":et?"codex":"claude"}var U=new Map,Tn=3,On=10*6e4;function pe(e){clearTimeout(e.idle),e.dead=!0;try{e.child.kill("SIGTERM")}catch{}U.get(e.frameId)===e&&U.delete(e.frameId)}function Mt(e){clearTimeout(e.idle),e.idle=setTimeout(()=>{e.turn||pe(e)},On)}function Xe(e,t=!1){let o=U.get(e.id),i=$e("claude"),r=`${i.model}|${i.effort}`;if(o&&!o.dead){if(o.turn||o.cfgKey===r)return o;!o.turns&&!o.resuming&&delete e.sessionId,pe(o)}let s=["-p","--input-format","stream-json","--output-format","stream-json","--verbose","--include-partial-messages","--permission-mode","acceptEdits","--allowedTools",[`Read,Edit,MultiEdit,Write,Glob,Grep,TodoWrite,WebFetch,WebSearch,Bash(node ${K} new:*),Bash(node ${K} add-route:*),Bash(node ${K} option:*),Bash(node ${K} shot:*)`,...jn()].join(","),"--add-dir",O,...An().flatMap(I=>["--add-dir",I])],a=!!(e.sessionId&&!t);a&&s.push("--resume",e.sessionId),i.model&&s.push("--model",i.model),i.effort&&s.push("--effort",i.effort),e.kind!=="canvas"&&s.push("--append-system-prompt",Pt(e));let l=e.kind==="folder"?e.src:de,p=Te(Oe,s,{cwd:l,env:process.env,stdio:["pipe","pipe","pipe"]}),w={frameId:e.id,child:p,turn:null,turns:0,resuming:a,idle:null,dead:!1,stderr:"",cfgKey:r};U.set(e.id,w),p.stdin.on("error",()=>{});let b="";p.stdout.on("data",I=>{b+=I;let A;for(;(A=b.indexOf(`
|
|
24
|
+
`))>=0;){let M=b.slice(0,A);if(b=b.slice(A+1),!M.trim())continue;let L;try{L=JSON.parse(M)}catch{continue}process.env.LOOP_DEBUG&&console.log(Date.now()%1e6,L.type,L.subtype||L.event?.type||"",L.event?.content_block?.type||L.event?.delta?.type||"");try{Cn(w,e,L)}catch(E){console.error(E)}}}),p.stderr.on("data",I=>{w.stderr=(w.stderr+I).slice(-2e3)}),p.on("error",I=>{w.stderr+=I.message}),p.on("close",I=>{w.dead=!0,clearTimeout(w.idle),U.get(e.id)===w&&U.delete(e.id);let A=w.turn;w.turn=null;let M=w.resuming&&!w.turns;if(!A){M&&(e.sessionId=null);return}let L=A.c,E=x.get(e.id);if(E?.commentId===L.id){if(!E.canceled&&M)return e.sessionId=null,x.delete(e.id),Lt(L,!0);E.canceled&&!E.steered&&!w.turns&&!w.resuming&&(e.sessionId=null),Y(L,"error",String(A.lastText&&E.canceled?A.lastText:w.stderr.trim()||`Agent exited with code ${I}`).slice(-400))}}),Mt(w);let j=[...U.values()].filter(I=>!I.turn&&I!==w);for(;U.size>Tn&&j.length;)pe(j.shift());return w}var jn=()=>(d.agent.allow||[]).filter(e=>!e.startsWith("dir:")),An=()=>(d.agent.allow||[]).filter(e=>e.startsWith("dir:")).map(e=>e.slice(4)),_n=/^(cd|tail|head|grep|sort|uniq|wc|cat|echo|true|sleep|tee|xargs)$/;function Pn(e){let t=String(e.tool_name||""),o=e.tool_input||{};if(t==="Bash"){let r=String(o.command||"").split(/&&|\|\||;|\|/).map(p=>p.trim()).filter(Boolean),s=r.find(p=>!_n.test(p.split(/\s+/)[0]))||r[0];if(!s)return null;let a=[];for(let p of s.split(/\s+/)){if(a.length===3||/^\d*[<>]|[<>&|$`'"*(){};]/.test(p))break;a.push(p)}if(!a.length)return null;let l=a.join(" ");return{tool:t,label:l,rule:`Bash(${l}:*)`,cmd:String(o.command).slice(0,300)}}let i=o.file_path||o.notebook_path||o.path;if(/^(Read|Edit|MultiEdit|Write|NotebookEdit|Glob|Grep)$/.test(t)&&i){let r=m.resolve(String(i)),s=/^(Glob|Grep)$/.test(t)?r:m.dirname(r);return{tool:t,label:`${/^(Read|Glob|Grep)$/.test(t)?"Read":"Edit"} files in ${s.replace(se.homedir(),"~")}`,rule:`dir:${s}`,cmd:String(i)}}return t?{tool:t,label:t.replace(/^mcp__/,"").replace(/__/g," \xB7 "),rule:t}:null}function En(e){let t=[],o=new Set(d.agent.allow||[]);for(let i of e||[]){let r=Pn(i);r&&!o.has(r.rule)&&!t.some(s=>s.rule===r.rule)&&t.push(r)}return t}function Cn(e,t,o){o.session_id&&(t.sessionId=o.session_id);let i=e.turn;if(!i)return;let r=i.c,s=o.type==="system"&&o.model||o.type==="assistant"&&o.message?.model;if(s&&r.model!==s&&(r.model=s,d.agent.lastModel=s,k()),r.t&&r.t.first==null&&o.type==="stream_event"&&(r.t.first=Date.now()-r.createdAt),r.t&&r.t.tool==null&&o.type==="stream_event"&&o.event?.content_block?.type==="tool_use"&&(r.t.tool=Date.now()-r.createdAt),o.type==="stream_event"){let a=o.event||{};if(a.type==="message_start"&&x.get(t.id)?.step==="Reading your comment\u2026"&&B(t.id,"Thinking\u2026"),a.type==="content_block_start"){let l=a.content_block||{};l.type==="thinking"?B(t.id,"Thinking\u2026"):l.type==="tool_use"?(i.partial="",B(t.id,mt(l.name,{}))):l.type==="text"&&(i.partial="")}else a.type==="content_block_delta"&&a.delta?.type==="text_delta"&&(i.partial+=a.delta.text,i.flush||(i.flush=setTimeout(()=>{i.flush=null,e.turn===i&&B(t.id,"Writing\u2026",i.partial.slice(-4e3))},90)))}else if(o.type==="assistant"){for(let a of o.message?.content||[])if(a.type==="tool_use")B(t.id,mt(a.name,a.input));else if(a.type==="text"&&a.text.trim()){i.lastText=a.text.trim();let l=r.steps||=[];l.push({t:Date.now(),s:i.lastText.slice(0,600),say:!0}),l.length>60&&l.splice(0,l.length-60),i.partial="",k()}}else if(o.type==="result"){clearTimeout(i.flush),e.turn=null,e.turns++,Mt(e);let a=o.result??i.lastText,l=r.steps?.[r.steps.length-1];l?.say&&String(a||"").trim().startsWith(l.s.slice(0,80))&&r.steps.pop();let p=!!o.is_error||(o.permission_denials||[]).some(b=>/^(Edit|MultiEdit|Write)$/.test(b.tool_name)),w=En(o.permission_denials);if(w.length?r.blocked=w:delete r.blocked,e.restart&&pe(e),x.get(t.id)?.commentId!==r.id)return;a!=null&&!p?Y(r,"done",String(a).trim().slice(0,4e3)||"Done."):Y(r,"error",String(a||e.stderr.trim()||"Something went wrong.").slice(-400))}}function Dn(e){if(!Oe||be()!=="claude"||x.has(e.id)||d.agent.mode==="inbox"&&Le())return!1;try{return Xe(e),!0}catch{return!1}}async function Lt(e,t=!1){let o=z(e.frameId);if(!o)return;if(be()==="codex")return Ft(e,o,t);if(!Oe)return x.set(o.id,{commentId:e.id,step:"",startedAt:Date.now()}),Y(e,"error",'No agent found to run this. Install Claude Code or Codex, or switch to "Your agent session" in the top-right menu.');let i={commentId:e.id,step:"Reading your comment\u2026",startedAt:e.startedAt&&t?e.startedAt:Date.now()};x.set(o.id,i),e.startedAt=i.startedAt,e.status="working",k();let r,s=!!U.get(o.id)&&!U.get(o.id).dead;try{r=Xe(o,t)}catch(p){return Y(e,"error",`Couldn't start the agent: ${p.message}`)}r.warmed=s,i.child=r.child;let a=!t&&!e.snapshot?await it(o,e.id):e.snapshot;if(e.snapshot=a,x.get(o.id)!==i)return;if(i.canceled)return Y(e,"canceled","Stopped.");if(r.dead){try{r=Xe(o,!0)}catch(p){return Y(e,"error",`Couldn't start the agent: ${p.message}`)}i.child=r.child}let l=e.kind==="create"||o.kind==="canvas"?st(o,e):_t(o,e).message;r.turn={c:e,lastText:"",partial:"",flush:null},clearTimeout(r.idle),e.t={sent:Date.now()-e.createdAt,warm:!!r.warmed},r.child.stdin.write(JSON.stringify({type:"user",message:{role:"user",content:[{type:"text",text:l}]}})+`
|
|
25
|
+
`)}async function Ft(e,t,o=!1){x.set(t.id,{commentId:e.id,step:"Starting Codex\u2026",startedAt:Date.now()}),e.startedAt=Date.now(),e.status="working",k(),!o&&!e.snapshot&&(e.snapshot=await it(t,e.id));let i=st(t,e),r=t.kind==="folder"?t.src:de,{model:s,effort:a}=$e("codex"),l=["--json","-c","sandbox_workspace_write.network_access=true",...s?["-m",s]:[],...a?["-c",`model_reasoning_effort="${a}"`]:[]],p=!!(t.codexSession&&!o&&e.kind!=="create"),w=p?["exec","resume",...l,"--skip-git-repo-check","-c",'sandbox_mode="workspace-write"',"-c",`sandbox_workspace_write.writable_roots=${JSON.stringify([r,O])}`,t.codexSession,"-"]:["exec",...l,"-s","workspace-write","--skip-git-repo-check","-C",r,"--add-dir",O,"-"];s&&(e.model=s,d.agent.lastModel=s,k());let b;try{b=Te(et,w,{cwd:r,env:process.env,stdio:["pipe","pipe","pipe"]})}catch(E){return Y(e,"error",`Couldn't start Codex: ${E.message}`)}x.get(t.id).child=b,b.stdin.on("error",()=>{}),b.stdin.end(i);let j="",I="",A=null,M=!1,L="";b.stdout.on("data",E=>{j+=E;let C;for(;(C=j.indexOf(`
|
|
26
|
+
`))>=0;){let F=j.slice(0,C);j=j.slice(C+1);let N;try{N=JSON.parse(F)}catch{continue}let J=N.item||{};N.type==="thread.started"&&N.thread_id&&e.kind!=="create"?t.codexSession=N.thread_id:J.type==="command_execution"&&N.type==="item.started"?B(t.id,/\bcat |sed -n|rg |ls\b|grep /.test(J.command||"")?"Reading the code":"Running a command"):J.type==="file_change"?B(t.id,`Editing ${m.basename(J.changes?.[0]?.path||"")}`.trim()):J.type==="agent_message"&&J.text?.trim()?(I=J.text.trim(),B(t.id,"Thinking\u2026")):J.type==="reasoning"?B(t.id,"Thinking\u2026"):N.type==="turn.completed"?M=!0:(N.type==="turn.failed"||N.type==="error")&&(A=N.error?.message||N.message||"Codex failed")}}),b.stderr.on("data",E=>{L=(L+E).slice(-2e3)}),b.on("error",E=>{L+=E.message}),b.on("close",E=>{if(!x.get(t.id)?.canceled&&!M&&p)return t.codexSession=null,x.delete(t.id),Ft(e,t,!0);M&&!A?Y(e,"done",I.slice(0,4e3)||"Done."):Y(e,"error",String(A||L.trim()||`Codex exited with code ${E}`).slice(-400))})}function Mn(){for(let e of Object.values(se.networkInterfaces()))for(let t of e||[])if(t.family==="IPv4"&&!t.internal)return t.address;return null}var X=0;function Ze(){let e=Mn();return{lan:e?`http://${e}:${X}${ue}/?t=${d.token}`:null,public:ne?`${ne}${ue}/?t=${d.token}`:null,tunnelAvailable:!!me("cloudflared")}}function Ln(){return new Promise((e,t)=>{if(ne)return e(ne);let o=me("cloudflared");if(!o)return t(new Error("Public links need cloudflared. Install it with: brew install cloudflared"));le=Te(o,["tunnel","--no-autoupdate","--url",`http://localhost:${X}`],{stdio:["ignore","pipe","pipe"]});let i=r=>{let s=String(r).match(/https:\/\/[a-z0-9-]+\.trycloudflare\.com/);s&&!ne&&(ne=s[0],k(),e(ne))};le.stdout.on("data",i),le.stderr.on("data",i),le.on("close",()=>{ne=null,le=null,k()}),setTimeout(()=>t(new Error("The tunnel took too long to start")),2e4)})}var Fn={".html":"text/html; charset=utf-8",".js":"text/javascript; charset=utf-8",".mjs":"text/javascript; charset=utf-8",".css":"text/css; charset=utf-8",".json":"application/json",".svg":"image/svg+xml",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".webp":"image/webp",".woff2":"font/woff2",".woff":"font/woff",".ttf":"font/ttf",".ico":"image/x-icon",".mp4":"video/mp4",".txt":"text/plain; charset=utf-8"};function Rt(e,t){let o=e.socket.remoteAddress||"",i=o==="127.0.0.1"||o==="::1"||o==="::ffff:127.0.0.1",r=e.headers["x-forwarded-for"]||e.headers["cf-connecting-ip"];if(i&&!r)return"owner";let s=/(?:^|;\s*)loop_t=([^;]+)/.exec(e.headers.cookie||"")?.[1];return t.searchParams.get("t")===d.token||s===d.token?"guest":null}async function D(e){let t=[],o=0;for await(let r of e){if(o+=r.length,o>15e6)throw new Error("too large");t.push(r)}let i=Buffer.concat(t).toString("utf8");return i?JSON.parse(i):{}}function u(e,t,o){e.writeHead(t,{"content-type":"application/json","cache-control":"no-store"}),e.end(JSON.stringify(o))}async function te(e,t,o={}){try{if((await _.stat(t)).isDirectory())return te(e,m.join(t,"index.html"),o);e.writeHead(200,{"content-type":Fn[m.extname(t).toLowerCase()]||"application/octet-stream","cache-control":"no-store",...o}),$.createReadStream(t).pipe(e)}catch{e.writeHead(404),e.end("Not found")}}var oe=(e,t)=>t===e||t.startsWith(e+m.sep),Ee=xt.createServer(async(e,t)=>{let o=new URL(e.url,"http://x"),i;try{i=decodeURIComponent(o.pathname)}catch{return t.writeHead(400),t.end()}let r=Rt(e,o);if(!r)return t.writeHead(403,{"content-type":"text/plain"}),t.end("This Loop canvas is private. Ask the host for a share link.");let s=r==="owner";if(i!==ue&&!i.startsWith(ue+"/"))return d.proxy?nn(e,t,d.proxy):i==="/"?(t.writeHead(302,{location:`${ue}/${o.search}`}),t.end()):(t.writeHead(404),t.end("Not found"));i=i.slice(ue.length)||"/";try{if(i==="/"||i==="/index.html"){let n=o.searchParams.get("t")?{"set-cookie":`loop_t=${d.token}; Path=/; HttpOnly; SameSite=Lax`}:{};return te(t,m.join(Ne,"index.html"),n)}if(i.startsWith("/app/")){let n=m.join(Ne,m.normalize(i.slice(5)));return oe(Ne,n)?te(t,n):(t.writeHead(400),t.end())}let a=/^\/optview\/([A-Za-z0-9_-]+)\/([A-F])\/(.*)$/.exec(i);if(a){let n=d.comments.find(v=>v.id===a[1]),c=n&&z(n.frameId),f=n?.options?.find(v=>v.id===a[2]);if(!c||!f||c.kind!=="folder")return t.writeHead(404),t.end();let h=m.normalize(decodeURIComponent(a[3]||c.entry)),g=m.join(f.dir,h),y=m.join(c.src,h);return!oe(f.dir,g)||!oe(c.src,y)?(t.writeHead(400),t.end()):te(t,$.existsSync(g)?g:y)}let l=/^\/hist\/([A-Za-z0-9_-]+)\/(.*)$/.exec(i);if(l){let n=m.join(O,"history",l[1]),c=m.join(n,m.normalize(l[2]||"index.html"));return oe(n,c)?te(t,c):(t.writeHead(400),t.end())}let p=/^\/poster\/([A-Za-z0-9_-]+)$/.exec(i);if(p){let n=d.frames.find(c=>c.id===p[1]);if(!n)return t.writeHead(404),t.end();try{return te(t,await wn(n),{"cache-control":"private, max-age=3600"})}catch{return t.writeHead(404),t.end()}}let w=/^\/shots\/([\w.-]+\.png)$/.exec(i);if(w)return te(t,m.join(O,"shots",w[1]));let b=/^\/attachments\/([A-Za-z0-9_-]+)\/([^/]+)$/.exec(i);if(b){let n=m.join(O,"attachments",b[1]),c=m.join(n,decodeURIComponent(b[2]));return oe(n,c)?te(t,c):(t.writeHead(400),t.end())}let j=/^\/f\/([^/]+)\/(.*)$/.exec(i);if(j){let n=d.frames.find(f=>f.id===j[1]);if(!n||n.kind!=="folder"&&n.kind!=="frozen")return t.writeHead(404),t.end();let c=m.join(n.src,m.normalize(j[2]||n.entry));return oe(n.src,c)?te(t,c):(t.writeHead(400),t.end())}if(i==="/api/ping")return u(t,200,{ok:!0,name:d.name,pid:process.pid});if(i==="/api/state")return u(t,200,It(r));if(i==="/api/events"){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-store",connection:"keep-alive"});let n={res:t,role:r};ze.add(n),He(t,Ot(n));let c=setInterval(()=>t.write(`: ka
|
|
27
|
+
|
|
28
|
+
`),2e4);e.on("close",()=>{clearInterval(c),ze.delete(n)});return}if(e.method==="POST"&&i==="/api/presence"){let n=await D(e);return Z({type:"presence",id:String(n.id).slice(0,32),name:String(n.name||"Guest").slice(0,32),color:String(n.color||"#888").slice(0,16),x:+n.x,y:+n.y,leave:!!n.leave}),u(t,200,{ok:!0})}if(e.method==="POST"&&i==="/api/comments"){let n=await D(e),c=z(n.frameId);if(!c||!String(n.text||"").trim())return u(t,400,{error:"frame and text required"});let f=typeof n.id=="string"&&/^[A-Za-z0-9_-]{8}$/.test(n.id)&&!d.comments.some(S=>S.id===n.id)?n.id:R(8),h=null;typeof n.annotationPng=="string"&&n.annotationPng.startsWith("data:image/png;base64,")&&(await _.mkdir(m.join(O,"annotations"),{recursive:!0}),h=m.join(O,"annotations",`${f}.png`),await _.writeFile(h,Buffer.from(n.annotationPng.split(",")[1],"base64")));let g=[];for(let S of(Array.isArray(n.attachments)?n.attachments:[]).slice(0,6)){let P=/^data:([\w.+-]+\/[\w.+-]+)?(;[^,]*)?;base64,(.*)$/s.exec(String(S?.data||""));if(!P)continue;let Q=String(S.name||"file").replace(/[^\w.\- ]+/g,"_").replace(/^\.+/,"").slice(-80)||"file",T=m.join(O,"attachments",f);await _.mkdir(T,{recursive:!0});let ee=Q,Wt=1;for(;g.some(Gt=>Gt.name===ee);)ee=`${Wt++}-${Q}`;await _.writeFile(m.join(T,ee),Buffer.from(P[3],"base64")),g.push({name:ee,type:P[1]||"application/octet-stream",path:m.join(T,ee),url:`/__loop/attachments/${f}/${encodeURIComponent(ee)}`})}let y=n.parentId?d.comments.find(S=>S.id===n.parentId):null,v={id:f,frameId:c.id,parentId:y?y.parentId||y.id:null,text:String(n.text).slice(0,4e3),anchor:n.anchor||y?.anchor||null,ink:Array.isArray(n.ink)?n.ink.slice(0,20):[],inkStrokes:Array.isArray(n.inkStrokes)?n.inkStrokes.map(String):[],annotation:h,author:{name:String(n.author?.name||(s?"You":"Guest")).slice(0,32),color:String(n.author?.color||"#2F5BFF").slice(0,16),guest:!s},status:s?"queued":"waiting",reply:null,createdAt:Date.now(),...g.length?{attachments:g}:{}};d.comments.push(v);for(let S of d.strokes)v.inkStrokes.includes(S.id)&&(S.commentId=v.id);if(s&&ln(v,c),k(),s&&c.kind==="folder"&&v.kind!=="create"){let S=bn(v.text);for(let P=0;P<S;P++)try{await kt(v,c)}catch{break}}return ie(c.id),u(t,200,v)}let I=/^\/api\/comments\/([^/]+)(?:\/(\w+))?$/.exec(i);if(I){let n=d.comments.find(f=>f.id===I[1]);if(!n)return u(t,404,{error:"no such comment"});let c=I[2];if(e.method==="DELETE"&&!c){if(!s&&!n.author?.guest)return u(t,403,{});let f=new Set([n.id,...d.comments.filter(g=>g.parentId===n.id).map(g=>g.id)]),h=x.get(n.frameId);return h&&f.has(h.commentId)&&(h.canceled=!0,h.child?h.child.kill("SIGTERM"):x.delete(n.frameId)),d.comments=d.comments.filter(g=>!f.has(g.id)),d.strokes=d.strokes.filter(g=>!f.has(g.commentId)),k(),u(t,200,{ok:!0})}if(!s)return u(t,403,{error:"host only"});if(c==="send")return["waiting","error","canceled"].includes(n.status)&&(n.status="queued",n.snapshot=null,n.undone=!1,n.redoSnap=null,k(),ie(n.frameId)),u(t,200,n);if(c==="background")return s?n.status!=="queued"?u(t,409,{error:"It already started"}):(n.background=!0,k(),ie(n.frameId),u(t,200,{ok:!0})):u(t,403,{});if(c==="steer")return s?pn(n)?u(t,200,{ok:!0}):u(t,409,{error:"Nothing to steer into: the run already finished"}):u(t,403,{});if(c==="cancel"){let f=x.get(n.frameId);return f?.commentId===n.id?(f.canceled=!0,f.child?f.child.kill("SIGTERM"):Y(n,"canceled","Stopped.")):n.status==="queued"&&(n.status="canceled",k()),u(t,200,{ok:!0})}if(c==="undo"){let f=z(n.frameId);if(!n.snapshot||!f||f.kind!=="folder")return u(t,400,{error:"nothing to undo"});if(x.has(f.id))return u(t,409,{error:"The agent is still working on this frame \u2014 wait for it to finish (or stop it) first."});let h=d.comments.filter(y=>y.id!==n.id&&y.frameId===n.frameId&&y.snapshot&&y.status==="done"&&!y.undone&&(y.startedAt||y.createdAt)>(n.startedAt||n.createdAt)),g=await D(e);if(h.length&&!g.force)return u(t,409,{error:"newer",newer:h.length});n.redoSnap=await je(f,`${n.id}-after`),await Ae(f,n.snapshot),n.undone=!0;for(let y of h)y.undone=!0;return k(),Z({type:"frame-changed",frameId:f.id}),u(t,200,{ok:!0,reverted:1+h.length})}if(c==="redo"){let f=z(n.frameId);return!n.redoSnap||!n.undone||!f?u(t,400,{error:"nothing to redo"}):x.has(f.id)?u(t,409,{error:"The agent is still working on this frame \u2014 wait for it to finish first."}):(await Ae(f,n.redoSnap),n.undone=!1,k(),Z({type:"frame-changed",frameId:f.id}),u(t,200,{ok:!0}))}if(c==="resolve")return n.resolved=!n.resolved,k(),u(t,200,n);if(c==="allow"&&e.method==="POST"){let f=await D(e),h=n.blocked?.[+f.i];if(!h||h.allowed)return u(t,400,{error:"Nothing to allow"});let g=d.agent.allow||=[];g.includes(h.rule)||g.push(h.rule),h.allowed=!0;let y=U.get(n.frameId);y&&!y.turn?pe(y):y&&(y.restart=!0);let v=d.comments.find(P=>P.id===(n.parentId||n.id)),S=h.tool==="Bash"?`\`${h.label}\``:h.label.charAt(0).toLowerCase()+h.label.slice(1);return d.comments.push({id:R(8),frameId:n.frameId,parentId:v?.id||n.id,anchor:n.anchor,ink:[],inkStrokes:[],text:`Allowed ${S}. Go ahead and use it, then carry on with what you were doing.`,author:v?.author||{name:"You",color:"#2F5BFF"},status:"queued",reply:null,createdAt:Date.now()}),k(),ie(n.frameId),u(t,200,{ok:!0})}if(c==="note"){let f=await D(e),h=String(f.text||"").trim().slice(0,2e3);if(!h)return u(t,400,{error:"text required"});let g={id:R(8),kind:"note",frameId:n.frameId,parentId:n.parentId||n.id,text:h,anchor:n.anchor,author:{name:fe[be()].label,agent:!0,color:"#17171A"},model:f.model?String(f.model).slice(0,60):void 0,status:"done",createdAt:Date.now()};return d.comments.push(g),k(),Z({type:"frame-changed",frameId:n.frameId}),u(t,200,g)}if(c==="step"){let f=await D(e);return B(n.frameId,String(f.text||"").slice(0,120)),u(t,200,{ok:!0})}if(c==="option"&&e.method==="POST"){let f=await D(e),h=z(n.frameId);if(!h)return u(t,404,{error:"no frame"});if(n.options=n.options||[],h.kind==="folder"){let v=String(f.name||"").trim().slice(0,60),S=n.options.find(P=>P.placeholder&&P.frameId);try{if(S){S.placeholder=!1,S.name=v||S.name;let P=d.frames.find(Q=>Q.id===S.frameId);P&&v&&(P.name=he(`${S.id} \xB7 ${v}`)),S.madeAt=Date.now(),k()}else S=await kt(n,h,v||`Option ${String.fromCharCode(65+n.options.length)}`)}catch(P){return u(t,400,{error:P.message})}return u(t,200,{id:S.id,dir:S.dir,root:h.src,frame:!0})}if(n.options.length>=6)return u(t,400,{error:"up to 6 options"});let g=String.fromCharCode(65+n.options.length),y=m.join(O,"options",n.id,g);return await _.rm(y,{recursive:!0,force:!0}),await _.mkdir(y,{recursive:!0}),n.options.push({id:g,name:String(f.name||`Option ${g}`).slice(0,60),dir:y}),k(),u(t,200,{id:g,dir:y,root:Et(h)})}if(c==="thumbs"&&e.method==="POST")return Dt(n),u(t,200,{ok:!0});if(c==="preview"&&e.method==="POST"){if(!s)return u(t,403,{});let f=await D(e);try{await gt(n,f.option||null)}catch(h){return u(t,400,{error:h.message})}return u(t,200,{ok:!0})}if(c==="clearoptions"&&e.method==="POST"){let f=new Set((n.options||[]).map(h=>h.frameId).filter(Boolean));for(let h of f){let g=x.get(h);g&&(g.canceled=!0,g.child?.kill("SIGTERM"),x.delete(h)),_e(h)}d.frames=d.frames.filter(h=>!f.has(h.id)),d.comments=d.comments.filter(h=>!f.has(h.frameId)),d.sections=d.sections.filter(h=>h.id!==n.optionSection);for(let h of n.options||[])delete h.frameId;return n.optionsCleared=!0,delete n.optionSection,k(),u(t,200,{ok:!0})}if(c==="choose"&&e.method==="POST"){if(!s)return u(t,403,{});let f=await D(e),h=n.options?.find(y=>y.id===f.option&&y.frameId),g=h&&d.frames.find(y=>y.id===h.frameId);if(g){let y=z(n.frameId);if(!y||y.kind!=="folder")return u(t,404,{error:"The original frame is gone."});if(x.has(y.id))return u(t,409,{error:"The agent is still working on the original frame. Wait for it to finish (or stop it) first."});let v=`r${R(7)}`;return await je(y,v)?((y.restores||=[]).push({id:v,at:Date.now()}),await Ae(y,g.src),n.optionChosen=h.id,ae.set(y.id,(ae.get(y.id)||0)+1),k(),Z({type:"frame-changed",frameId:y.id}),u(t,200,{ok:!0})):u(t,500,{error:"Could not keep the current version, so nothing was changed."})}try{await gt(n,f.option)}catch(y){return u(t,400,{error:y.message})}return n.optionChosen=f.option,k(),await _.rm(m.join(O,"options",n.id,"_original"),{recursive:!0,force:!0}).catch(()=>{}),u(t,200,{ok:!0})}if(c==="reply"){let f=await D(e);return x.has(n.frameId)||x.set(n.frameId,{commentId:n.id,step:"",startedAt:Date.now()}),f.model&&(n.model=String(f.model).slice(0,60),d.agent.lastModel=n.model),n.startedAt||(n.startedAt=x.get(n.frameId)?.startedAt),Y(n,f.error?"error":"done",String(f.text||"Done.").slice(0,4e3)),u(t,200,n)}}if(!s)return u(t,403,{error:"host only"});if(i==="/api/inbox"){let n=Date.now()+(o.searchParams.get("wait")?25e3:0),c=Le(),f=!1;for(e.on("close",()=>{f=!0}),Ke=Date.now(),c||k(!0);;){if(f)return;Ke=Date.now();let h=d.comments.find(g=>g.status==="queued"&&!g.background&&!x.has(g.frameId));if(h){let g=z(h.frameId),y={commentId:h.id,step:"Your chat picked this up",startedAt:Date.now(),inbox:!0};return x.set(g.id,y),setTimeout(()=>{x.get(g.id)===y&&y.step==="Your chat picked this up"&&B(g.id,"Working\u2026")},3e3),h.status="working",k(),[h.snapshot]=await Promise.all([h.snapshot||it(g,h.id),un(h)]),u(t,200,{comment:{id:h.id,kind:h.kind||"comment",text:h.text,anchor:h.anchor,ink:h.ink,annotation:h.annotation,attachments:h.attachments},frame:{id:g.id,name:g.name,kind:g.kind,src:g.src,entry:g.entry,w:g.w,h:g.h},prompt:st(g,h)})}if(Date.now()>n)return u(t,200,{comment:null});await new Promise(g=>{let y=()=>{clearTimeout(v),Ue.delete(y),g()},v=setTimeout(y,2e3);Ue.add(y)})}}if(e.method==="POST"&&i==="/api/frames"){let n=await D(e);if(n.prompt){let c=n.name||n.prompt.split(/[.\n]/)[0].slice(0,40),f=ut(c),h=q({src:f,name:c,w:n.w,h:n.h,x:n.x,y:n.y});return d.comments.push({id:R(8),kind:"create",frameId:h.id,text:n.prompt,author:{name:"You",color:"#2F5BFF"},status:"queued",createdAt:Date.now()}),k(),ie(h.id),u(t,200,{...h,dir:f})}if(n.new){let c=ut(n.name||"Untitled",!!n.blank);typeof n.html=="string"&&n.html.length<4e6&&$.writeFileSync(m.join(c,"index.html"),n.html);let f=n.x==null&&Sn();if(f)return u(t,200,{...xn(f.c,f.f,c,n.name),dir:c});let h=q({...n,src:c});return n.parent&&d.frames.some(g=>g.id===n.parent&&g.id!==h.id)&&(h.parent=n.parent,k()),(n.fill==="none"||/^#[0-9a-f]{6}$/i.test(n.fill||""))&&(h.fill=n.fill.toLowerCase(),k()),u(t,200,{...h,dir:c})}return n.route!=null?u(t,200,q(n)):n.src?!/^https?:\/\//.test(n.src)&&!$.existsSync(m.resolve(de,n.src))?u(t,400,{error:`No such file or folder: ${n.src}`}):u(t,200,q(n)):u(t,400,{error:"src, route, new or prompt required"})}let A=/^\/api\/frames\/([^/]+)\/versions(\/restore|\/duplicate)?$/.exec(i);if(A&&e.method==="POST"){let n=d.frames.find(S=>S.id===A[1]);if(!n||n.kind!=="folder")return u(t,400,{error:"Only static frames keep versions."});let c=encodeURIComponent(n.entry||"index.html");if(!A[2]){let S=[...d.comments.filter(T=>T.frameId===n.id&&T.snapshot&&!T.snapThumb&&$.existsSync(T.snapshot)),...(n.restores||[]).filter(T=>!T.thumb)].slice(-14),P=async T=>{try{let ee=await ce(n,`http://127.0.0.1:${X}/__loop/hist/${T.id}/${c}`);T["frameId"in T?"snapThumb":"thumb"]=`/__loop/shots/${m.basename(ee)}`,k()}catch{}},[Q]=await Promise.all([ce(n).catch(()=>null),...S.map(P)]);return u(t,200,{now:Q?`/__loop/shots/${m.basename(Q)}`:null})}let f=await D(e),h=String(f.id||"");if(!/^[A-Za-z0-9_-]+$/.test(h))return u(t,400,{error:"no such version"});let g=d.comments.some(S=>S.id===h&&S.frameId===n.id&&S.snapshot)||(n.restores||[]).some(S=>S.id===h),y=m.join(O,"history",h);if(!g||!$.existsSync(y))return u(t,404,{error:"That version is no longer kept."});if(A[2]==="/duplicate"){let S=he(String(f.name||`${n.name} \xB7 earlier version`).slice(0,80)),P=ge(S);return await re(y,P),u(t,200,q({src:m.join(P,n.entry||"index.html"),name:S,w:n.w,h:n.h,near:n.id}))}if(x.has(n.id))return u(t,409,{error:"The agent is still working on this frame. Wait for it to finish (or stop it) first."});let v=`r${R(7)}`;return await je(n,v)?((n.restores||=[]).push({id:v,at:Date.now()}),await Ae(n,y),ae.set(n.id,(ae.get(n.id)||0)+1),k(),Z({type:"frame-changed",frameId:n.id}),u(t,200,{ok:!0})):u(t,500,{error:"Could not keep the current version, so nothing was changed."})}let M=/^\/api\/frames\/([^/]+)\/(source|edit)$/.exec(i);if(M){let n=d.frames.find(h=>h.id===M[1]);if(!n||!s)return u(t,404,{error:"no such frame"});if(n.kind!=="folder")return u(t,400,{error:"Only static frames can be edited directly."});let c=m.join(n.src,n.entry||"index.html"),f;try{f=await _.readFile(c,"utf8")}catch{return u(t,404,{error:"The frame has no page to edit."})}if(M[2]==="source"&&e.method==="GET")return u(t,200,{text:f});if(M[2]==="edit"&&e.method==="POST"){if(x.has(n.id))return u(t,409,{error:"The agent is working on this frame. Try again when it is done."});let h=await D(e),g=(Array.isArray(h.edits)?h.edits:[]).map(T=>({a:T.a|0,b:T.b|0,before:String(T.before??""),text:String(T.text??"")})).sort((T,ee)=>T.a-ee.a);if(!g.length||g.length>40)return u(t,400,{error:"nothing to change"});let y=0;for(let T of g){if(T.a<y||T.b<T.a||T.b>f.length||f.slice(T.a,T.b)!==T.before)return u(t,409,{error:"The page changed since you started editing. Try again."});y=T.b}let v=ht.get(n.id)||0,S=Date.now();if(S-v>9e4){let T=`e${R(7)}`;await je(n,T)&&(n.restores||=[]).push({id:T,at:S,what:String(h.what||"Edited by hand").slice(0,80)})}ht.set(n.id,S);let P="",Q=0;for(let T of g)P+=f.slice(Q,T.a)+T.text,Q=T.b;return P+=f.slice(Q),await _.writeFile(c,P),k(),u(t,200,{ok:!0})}}let L=/^\/api\/frames\/([^/]+)\/warm$/.exec(i);if(L&&e.method==="POST"){let n=z(L[1]);return Fe&&n?.kind!=="canvas"&&Ct().catch(()=>{}),u(t,200,{warm:!!(n&&Dn(n))})}let E=/^\/api\/frames\/([^/]+)\/shot$/.exec(i);if(E&&e.method==="POST"){let n=d.frames.find(h=>h.id===E[1]);if(!n||!s)return u(t,404,{error:"no such frame"});let c=await D(e),f;if(c.option){let[h,g]=String(c.option).split(":"),y=d.comments.find(S=>S.id===h);if(!y?.options?.some(S=>S.id===g))return u(t,400,{error:"no such option"});let v=d.frames.find(S=>S.id===y.options.find(P=>P.id===g)?.frameId);if(v)try{return u(t,200,{path:await ce(v)})}catch(S){return u(t,500,{error:S.message})}if(n.kind!=="folder")return u(t,400,{error:"Options on app screens can't be screenshotted before they're applied. Describe them clearly instead."});f=`http://127.0.0.1:${X}/__loop/optview/${h}/${g}/${encodeURIComponent(n.entry||"index.html")}`}x.get(n.id)?.inbox&&B(n.id,"Looking at the screen");try{return u(t,200,{path:await ce(n,f)})}catch(h){return u(t,500,{error:h.message})}}let C=/^\/api\/frames\/([^/]+)\/freeze$/.exec(i);if(C&&e.method==="POST"){let n=d.frames.find(v=>v.id===C[1]);if(!n)return u(t,404,{error:"no such frame"});let c=await D(e);if(typeof c.html!="string"||!c.html)return u(t,400,{error:"nothing captured"});let f=he(`${n.name.replace(/ · frozen( \d+)?$/,"")} \xB7 frozen`),h=ge(f);await _.mkdir(h,{recursive:!0}),await _.writeFile(m.join(h,"index.html"),c.html);let g=typeof c.from=="string"&&c.from.startsWith("/")&&!c.from.startsWith("/__loop/")?c.from.slice(0,2e3):n.kind==="frozen"?n.from:n.kind==="route"?n.src:void 0,y={id:R(6),name:f,kind:"frozen",src:h,entry:"index.html",from:g,frozenAt:Date.now(),w:n.w,h:n.h,keyboard:n.keyboard,...c.x!=null&&c.y!=null?{x:+c.x,y:+c.y}:Be(n.w,n.h,n.id),createdAt:Date.now()};return d.frames.push(y),k(),u(t,200,y)}let F=/^\/api\/frames\/([^/]+)\/duplicate$/.exec(i);if(F&&e.method==="POST"){let n=d.frames.find(S=>S.id===F[1]);if(!n)return u(t,404,{error:"no such frame"});let c=await D(e),f=n.kind==="route"&&typeof c.route=="string"&&c.route.startsWith("/")&&!c.route.startsWith("/__loop/")?c.route.slice(0,2e3):null,h=f&&f!==n.src,g=he(h?sn(f):`${n.name.replace(/ copy( \d+)?$/,"")} copy`),y=c.x!=null&&c.y!=null?{x:c.x,y:c.y}:{near:n.id},v;if(n.kind==="folder"||n.kind==="frozen"){let S=ge(g);await re(n.src,S),v=q({src:m.join(S,n.entry),name:g,w:n.w,h:n.h,...y}),n.kind==="frozen"&&(_e(v.id),Object.assign(v,{kind:"frozen",from:n.from,frozenAt:n.frozenAt}),k())}else n.kind==="route"?v=q({route:f||n.src,name:g,w:n.w,h:n.h,dup:!0,...y}):v=q({src:n.src,name:g,w:n.w,h:n.h,...y});return u(t,200,v)}let N=/^\/api\/frames\/([^/]+)$/.exec(i);if(N){let n=d.frames.find(c=>c.id===N[1]);if(!n)return u(t,404,{});if(e.method==="PATCH"){let c=await D(e);for(let f of["x","y","w","h"])c[f]!=null&&isFinite(c[f])&&(n[f]=Math.round(+c[f]));return c.name&&(n.name=String(c.name).slice(0,80)),typeof c.keyboard=="boolean"&&(n.keyboard=c.keyboard||void 0),"fill"in c&&(n.fill=c.fill==="none"?"none":typeof c.fill=="string"&&/^#[0-9a-f]{6}$/i.test(c.fill)?c.fill.toLowerCase():void 0),"parent"in c&&(n.parent=c.parent&&c.parent!==n.id&&d.frames.some(f=>f.id===c.parent)?c.parent:void 0),k(),u(t,200,n)}if(e.method==="DELETE"){let c=x.get(n.id);return c&&(c.canceled=!0,c.child?.kill("SIGTERM"),x.delete(n.id)),d.frames=d.frames.filter(f=>f.id!==n.id),d.comments=d.comments.filter(f=>f.frameId!==n.id),_e(n.id),k(),u(t,200,{ok:!0})}}if(e.method==="POST"&&i==="/api/sections"){let n=await D(e);if(![n.x,n.y,n.w,n.h].every(Number.isFinite))return u(t,400,{error:"x, y, w and h required"});let c=d.sections.length+1;for(;d.sections.some(h=>h.name===`Section ${c}`);)c++;let f={id:R(6),name:String(n.name||`Section ${c}`).slice(0,80),x:Math.round(n.x),y:Math.round(n.y),w:Math.max(120,Math.round(n.w)),h:Math.max(120,Math.round(n.h)),createdAt:Date.now()};return d.sections.push(f),k(),u(t,200,f)}let J=/^\/api\/sections\/([^/]+)$/.exec(i);if(J){let n=d.sections.find(c=>c.id===J[1]);if(!n)return u(t,404,{});if(e.method==="PATCH"){let c=await D(e);for(let f of["x","y","w","h"])c[f]!=null&&isFinite(c[f])&&(n[f]=Math.round(+c[f]));return c.name&&(n.name=String(c.name).slice(0,80)),k(),u(t,200,n)}if(e.method==="DELETE")return d.sections=d.sections.filter(c=>c.id!==n.id),k(),u(t,200,{ok:!0})}if(e.method==="POST"&&i==="/api/restore"){let n=await D(e);for(let c of n.sections||[])c?.id&&!d.sections.find(f=>f.id===c.id)&&d.sections.push(c);for(let c of n.frames||[])c?.id&&!d.frames.find(f=>f.id===c.id)&&(d.frames.push(c),nt(c));for(let c of n.strokes||[])c?.id&&!d.strokes.find(f=>f.id===c.id)&&d.strokes.push(c);for(let c of n.comments||[])c?.id&&!d.comments.find(f=>f.id===c.id)&&d.comments.push({...c,status:["working","queued"].includes(c.status)?"canceled":c.status});return k(),u(t,200,{ok:!0})}if(e.method==="POST"&&i==="/api/strokes"){let n=await D(e),c={id:R(8),points:(n.points||[]).slice(0,6e3).map(h=>Math.round(h*10)/10),color:String(n.color||"#E5484D").slice(0,16),width:+n.width||3,frameId:n.frameId||null,createdAt:Date.now()},f=c.frameId&&d.frames.find(h=>h.id===c.frameId);return f&&(c.fx=Number.isFinite(+n.fx)?+n.fx:f.x,c.fy=Number.isFinite(+n.fy)?+n.fy:f.y),d.strokes.push(c),k(),u(t,200,c)}let Se=/^\/api\/strokes\/([^/]+)$/.exec(i);if(Se&&e.method==="DELETE")return d.strokes=d.strokes.filter(n=>n.id!==Se[1]),k(),u(t,200,{ok:!0});if(e.method==="POST"&&i==="/api/update")return!V||V.how!=="restart"?u(t,400,{error:"No update to apply here"}):V.state==="updating"?u(t,200,{ok:!0}):x.size?u(t,409,{error:"The agent is working. Update when it has finished."}):(Nn(),u(t,200,{ok:!0}));if(e.method==="POST"&&i==="/api/settings"){let n=await D(e);if((n.mode==="headless"||n.mode==="inbox")&&(d.agent.mode=n.mode),n.runner&&fe[n.runner]){if(!fe[n.runner].bin)return u(t,400,{error:`${fe[n.runner].label} isn't installed on this computer`});d.agent.runner=n.runner}if(n.proxy!==void 0){if(n.proxy&&!/^http:\/\/(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/.test(n.proxy))return u(t,400,{error:"proxy must be a local dev server origin, e.g. http://localhost:3000"});d.proxy=n.proxy||null}if(n.chat&&typeof n.chat=="object"){let c=f=>typeof f=="string"&&/^[\w.:\-\[\]]{1,60}$/.test(f)?f:null;d.agent.chat={runner:n.chat.runner==="codex"?"codex":"claude",model:c(n.chat.model),effort:c(n.chat.effort)}}if(n.pick&&typeof n.pick=="object"){let c=be(),f=$e(c),h=(d.agent.pick||={})[c]||={};if("model"in n.pick){if(n.pick.model&&!f.models.includes(n.pick.model))return u(t,400,{error:"No such model"});h.model=n.pick.model||void 0}if("effort"in n.pick){if(n.pick.effort&&!$e(c).efforts.includes(n.pick.effort))return u(t,400,{error:"No such effort level"});h.effort=n.pick.effort||void 0}}if(n.disallow){d.agent.allow=(d.agent.allow||[]).filter(c=>c!==n.disallow);for(let c of[...U.values()])c.turn?c.restart=!0:pe(c)}return n.name&&(d.name=String(n.name).slice(0,80)),n.hostName!==void 0&&(d.hostName=String(n.hostName).trim().slice(0,32)||void 0),k(),at(),u(t,200,{ok:!0})}if(e.method==="POST"&&i==="/api/share/public")try{return await Ln(),u(t,200,Ze())}catch(n){return u(t,400,{error:n.message})}if(e.method==="POST"&&i==="/api/share/stop")return le?.kill(),ne=null,k(),u(t,200,{ok:!0});if(e.method==="POST"&&i==="/api/share/rotate")return d.token=R(16),k(),u(t,200,Ze());if(i.startsWith("/api/cloud")&&!G)return u(t,404,{error:"Email sharing is not available in this version of Loop."});if(i==="/api/cloud"&&e.method==="GET")return u(t,200,await G.status());if(e.method==="POST"&&i==="/api/cloud/session"){let n=await D(e);return u(t,200,{user:await G.setSession(n)})}if(e.method==="POST"&&i==="/api/cloud/signout")return G.signOut(),u(t,200,{ok:!0});if(e.method==="POST"&&i==="/api/cloud/publish")return u(t,200,{link:await G.publish()});if(e.method==="POST"&&i==="/api/cloud/snapshot"){let n=await D(e),c=d.frames.find(h=>h.id===n.frameId);if(!c||c.kind!=="route"||typeof n.html!="string")return u(t,400,{error:"route frame and html required"});let f=await G.publishSnapshot(c,n.html);return G.schedulePush(200),u(t,200,f)}if(e.method==="POST"&&i==="/api/cloud/invite"){let n=await D(e);return u(t,200,await G.invite(n.emails||[],n.role))}if(e.method==="DELETE"&&i.startsWith("/api/cloud/members/"))return await G.removeMember(decodeURIComponent(i.slice(19))),u(t,200,{ok:!0});if(e.method==="POST"&&i==="/api/shutdown")return u(t,200,{ok:!0}),setTimeout(Re,50);u(t,404,{error:"not found"})}catch(a){u(t,500,{error:a.message})}});Ee.on("upgrade",(e,t,o)=>{let i=new URL(e.url,"http://x");if(!d.proxy||i.pathname.startsWith(ue)||!Rt(e,i))return t.destroy();let r=new URL(d.proxy),s=Bt.connect(+(r.port||80),r.hostname,()=>{let a=`${e.method} ${e.url} HTTP/1.1\r
|
|
29
|
+
`;for(let l=0;l<e.rawHeaders.length;l+=2){let p=e.rawHeaders[l],w=e.rawHeaders[l+1];p.toLowerCase()==="host"&&(w=r.host),a+=`${p}: ${w}\r
|
|
30
|
+
`}s.write(a+`\r
|
|
31
|
+
`),o?.length&&s.write(o),s.pipe(t),t.pipe(s)});s.on("error",()=>t.destroy()),t.on("error",()=>s.destroy())});Jt&&(G={createCloud:()=>null}.createCloud({state:d,changed:k,pump:ie,broadcast:Z,log:e=>console.log(e)}));var bt=!1;setInterval(()=>{let e=Le();e!==bt&&(bt=e,k(!0));for(let t of[...U.values()])!t.turn&&t.frameId!==ke&&!d.frames.some(o=>o.id===t.frameId)&&pe(t);if(d.agent.mode==="inbox"&&!e){for(let[t,o]of x)if(o.inbox){x.delete(t);let i=d.comments.find(r=>r.id===o.commentId);i&&i.status==="working"&&(i.status="queued",k())}if(d.agent.mode==="inbox"&&St>=0){let t=!1;for(let o of d.comments)o.status==="queued"&&!o.background&&Date.now()-(o.queuedAt||o.createdAt)>St&&z(o.frameId)&&(o.background=!0,t=!0);t&&(k(),at())}}},5e3);var St=Number(process.env.LOOP_CHAT_WAIT??15e3),Ve=(()=>{try{return JSON.parse($.readFileSync(m.join(ye,"package.json"),"utf8")).version||null}catch{return null}})(),Qe=process.env.LOOP_UPDATE_TEST?"test":/[\\/]_npx[\\/]/.test(ye)?"npx":/[\\/]node_modules[\\/]/.test(ye)?"global":null,V=null,Rn=(e,t)=>{let o=String(e).split(".").map(Number),i=String(t).split(".").map(Number);for(let r=0;r<3;r++)if((o[r]||0)!==(i[r]||0))return(o[r]||0)>(i[r]||0);return!1};async function Nt(){if(!(!Qe||!Ve||V?.state==="updating"))try{let e=process.env.LOOP_UPDATE_TEST||(await(await fetch("https://registry.npmjs.org/loop-canvas/latest",{signal:AbortSignal.timeout(8e3),headers:{accept:"application/json"}})).json()).version;e&&Rn(e,Ve)&&V?.latest!==e&&(V={latest:e,how:Qe==="npx"?"next-start":"restart",state:"available"},k())}catch{}}setTimeout(Nt,4e3).unref?.();setInterval(Nt,3*36e5).unref?.();async function Nn(){if(V.state="updating",delete V.error,k(),Qe==="global")try{await new Promise((e,t)=>Ht("npm",["install","-g",`loop-canvas@${V.latest}`],{timeout:18e4},(o,i,r)=>o?t(new Error(String(r||o.message).trim().split(`
|
|
32
|
+
`).slice(-3).join(" "))):e()))}catch(e){V.state="failed",V.error=e.message.slice(0,300),k();return}tt(),Te(process.execPath,[m.join(ye,"server.mjs"),"--dir",O,"--port",String(X)],{detached:!0,stdio:"ignore",env:{...process.env,LOOP_SAME_PORT:"1",LOOP_UPDATE_TEST:""}}).unref(),setTimeout(Re,150)}function qe(e,t=20){Ee.once("error",o=>{if(o.code==="EADDRINUSE"&&process.env.LOOP_SAME_PORT&&t>0)return setTimeout(()=>qe(e,t-1),500);o.code==="EADDRINUSE"&&t>0?qe(e+1,t-1):(console.error(o),process.exit(1))}),Ee.listen(e,"0.0.0.0",()=>{X=Ee.address().port,$.writeFileSync(vt,JSON.stringify({port:X,pid:process.pid})),console.log(`Loop \u25B8 http://localhost:${X} (${O})`),at()})}function Re(){try{$.rmSync(vt,{force:!0})}catch{}le?.kill(),Ye();for(let e of U.values())try{e.child.kill("SIGTERM")}catch{}for(let e of x.values())e.child?.kill("SIGTERM");clearTimeout(Ge),tt(),process.exit(0)}process.on("SIGINT",Re);process.on("SIGTERM",Re);qe(+$t.port||4848);function Wn(e){let t={};for(let o=0;o<e.length;o++)if(e[o].startsWith("--")){let i=e[o].slice(2);t[i]=e[o+1]&&!e[o+1].startsWith("--")?e[++o]:!0}return t}function me(e){let t=[...(process.env.PATH||"").split(":"),m.join(se.homedir(),".local/bin"),"/opt/homebrew/bin","/usr/local/bin"];for(let o of t){if(!o)continue;let i=m.join(o,e);try{return $.accessSync(i,$.constants.X_OK),i}catch{}}return null}export{we as PRESETS};
|