@umage.ai/redline-agent 0.0.0-stage → 0.1.0

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/panel.js ADDED
@@ -0,0 +1,83 @@
1
+ var ce="/__redline";var z=`${ce}/agent`,Ce={blocked:4,working:3,error:2,done:1,idle:0};function V(r){let n,s=-1;for(let a of r){if(!a.running&&a.status.state!=="done"&&a.status.state!=="error")continue;let w=Ce[a.status.state]??0;w>=s&&(n=a,s=w)}return n}var fe="#a3a3ad",Ee="#5c5c66",Te="\x1B",ge="\x1B[D",pe="\x1B[C";function be(r){let n=r.status;if(!r.running&&n.state!=="done"&&n.state!=="error")return null;let s=w=>`${r.command}: ${w}${n.progress!==void 0&&n.state==="working"?` ${n.progress}%`:""}${n.msg?` \xB7 ${n.msg}`:""}`,a={toolbar:"outline",launcher:"ring"};switch(n.state){case"working":return{...a,color:"#4f8cff",spin:!0,title:s(n.source==="activity"?"busy":"working")};case"blocked":return n.kind==="permission"?{...a,color:"#ff9f0a",pulse:!0,title:s("waiting for your approval")}:{...a,color:"#ffd60a",pulse:!0,title:s("waiting for your answer")};case"done":return{...a,color:"#3ecf8e",title:s("done")};case"error":return{...a,color:"#ff453a",title:s("failed")};default:return{color:fe,toolbar:!1,launcher:!1,title:s("ready")}}}function I(r){let n=r.status,s=n.msg||void 0;if(!r.running&&n.state!=="done"&&n.state!=="error")return{label:r.exitCode===void 0?"Not started":`Stopped${r.exitCode?` (exit ${r.exitCode})`:""}`,color:Ee};let a=be(r)?.color??fe;switch(n.state){case"working":return{label:`Working${n.progress!==void 0?` ${n.progress}%`:""}`,msg:s,color:a};case"blocked":return{label:n.kind==="permission"?"Needs your approval":"Needs your answer",msg:s,color:a};case"done":return{label:"Done",msg:s,color:a};case"error":return{label:"Failed",msg:s,color:a};default:return{label:"Ready",msg:s,color:a}}}function Ne(r,n,s=Y(n)){let a=s.join(", ");return r.replace(/\{(page|url|ids)\}/g,(w,c)=>c==="page"?n.page():c==="url"?location.href:a||"none")}var Y=r=>r.notes("page").filter(n=>!n.done).map(n=>n.id);function Le(r,n){let s=new Set(n.filter(a=>a.running).flatMap(a=>a.sent??[]));return Y(r).filter(a=>!s.has(a))}var ue=r=>r.prompt.includes("{ids}");function Re(r,n,s=3){let a=new Set(r.filter(c=>c.id===n||c.running&&c.status.state==="blocked").map(c=>c.id)),w=[...r].reverse();for(let c of[...w.filter(u=>u.running),...w.filter(u=>!u.running)])a.size<s&&a.add(c.id);return{shown:r.filter(c=>a.has(c.id)).map(c=>c.id),hidden:r.filter(c=>!a.has(c.id)).map(c=>c.id)}}function He(r,n){return n.has(r.id)?r.done?"done":"sent":null}function Pe(r,n,s){let a=[...n].reverse().find(c=>c.sent?.includes(r.id));if(!a)return null;if(r.done)return{cls:"done",text:`\u2713 done by ${s(a)}`};let w=a.running?a.status.state:"stopped";return w==="working"?{cls:"sent working",text:`${s(a)} is working on it`}:w==="blocked"?{cls:"sent asking",text:`${s(a)} needs your answer`}:{cls:"sent",text:`\u2933 sent to ${s(a)}`}}var Ae=`
2
+ /* A fixed width, so the toolbar doesn't change size with every message (the full text is the hover). */
3
+ .ag-chip { display: flex; align-items: center; gap: 7px; padding: 0 8px; flex: none; width: 290px; box-sizing: border-box; color: var(--fg); white-space: nowrap; overflow: hidden; }
4
+ .ag-chip b { flex: none; }
5
+ .ag-chip .ag-dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--c); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 25%, transparent); }
6
+ .ag-chip b { font-weight: 650; }
7
+ .ag-chip .ag-msg { color: var(--fg-2); overflow: hidden; text-overflow: ellipsis; min-width: 0; }
8
+ .bar button.ag-allow { background: #3ecf8e; color: #08130d; font-weight: 650; }
9
+ .bar button.ag-allow:hover { background: #5fdca3; color: #08130d; }
10
+ .bar button.ag-deny:hover { background: color-mix(in srgb, #ff453a 25%, var(--bg-3)); }
11
+ .ag-line { display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 10px; border-radius: 8px; background: var(--bg-2); color: var(--fg); text-align: left; cursor: pointer; }
12
+ .ag-line:hover { background: var(--bg-3); }
13
+ .ag-line .ag-dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--c); }
14
+ .ag-line span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
15
+ .ag-line kbd { flex: none; }
16
+ .ag-tag { display: inline-block; margin-top: 6px; padding: 1px 7px; border-radius: 9px; font-size: 11px; font-weight: 600; }
17
+ .ag-tag.sent { color: #8fb4ff; background: color-mix(in srgb, #4f8cff 18%, transparent); }
18
+ .ag-tag.done { color: #3ecf8e; background: color-mix(in srgb, #3ecf8e 16%, transparent); }
19
+ .ag-tag.working, .ag-tag.asking { cursor: pointer; }
20
+ .ag-tag.asking { color: #ffb340; background: color-mix(in srgb, #ff9f0a 18%, transparent); }
21
+ .ag-tag.working::before, .ag-tag.asking::before { content: ""; display: inline-block; width: 6px; height: 6px; margin-right: 6px; border-radius: 50%; background: currentColor; vertical-align: 1px; animation: ag-beat 1.2s ease-in-out infinite; }
22
+ @keyframes ag-beat { 50% { opacity: .45; transform: scale(.75); } }
23
+ .ag-chip[data-state="working"] .ag-dot, .ag-tab[data-state="working"] .ag-dot, .ag-switch[data-state="working"] .ag-dot, .ag-line[data-state="working"] .ag-dot { animation: ag-beat 1.2s ease-in-out infinite; }
24
+ @media (prefers-reduced-motion: reduce) { .ag-tag::before, .ag-dot { animation: none !important; } }
25
+ .ag-card { display: grid; gap: 10px; font-size: 13px; }
26
+ .ag-card .ag-who { display: flex; align-items: center; gap: 7px; color: var(--fg-2); font-size: 12px; }
27
+ .ag-card .ag-who .ag-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--c); }
28
+ .ag-steps { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
29
+ .ag-steps button { padding: 3px 9px; border-radius: 99px; border: 1px solid var(--line); color: var(--fg-2); font-size: 12px; }
30
+ .ag-steps button.cur { color: var(--fg); border-color: var(--c); background: color-mix(in srgb, var(--c) 18%, transparent); }
31
+ .ag-steps button.done { color: #3ecf8e; }
32
+ .ag-steps .nav { border: 0; padding: 3px 6px; }
33
+ .ag-ctx { margin: 0; padding: 8px 10px; border-radius: 8px; background: #0b0b0e; font: 12px/1.45 ui-monospace, SFMono-Regular, "Cascadia Mono", Menlo, Consolas, monospace; color: #c7c7cf; white-space: pre; overflow: auto; max-height: 260px; }
34
+ .ag-q { font-weight: 650; font-size: 14px; line-height: 1.35; }
35
+ .ag-opts { display: grid; gap: 4px; }
36
+ .ag-opt { display: grid; grid-template-columns: 22px 1fr; gap: 2px 8px; align-items: baseline; text-align: left; padding: 7px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg-2); color: var(--fg); cursor: pointer; }
37
+ .ag-opt:hover:not(:disabled) { background: var(--bg-3); border-color: color-mix(in srgb, var(--c) 50%, var(--line)); }
38
+ .ag-opt.sel { border-color: color-mix(in srgb, var(--c) 70%, var(--line)); }
39
+ .ag-opt:disabled { opacity: .5; cursor: default; }
40
+ .ag-opt .n, .ag-input .n { font: 600 11px ui-monospace, Consolas, monospace; color: var(--fg-2); background: var(--bg-3); border-radius: 5px; text-align: center; padding: 1px 0; }
41
+ .ag-opt .d { grid-column: 2; color: var(--fg-2); font-size: 12px; line-height: 1.35; }
42
+ .ag-input { display: grid; grid-template-columns: 22px 1fr auto; gap: 8px; align-items: center; padding: 5px 6px 5px 10px; border-radius: 8px; border: 1px dashed var(--line); }
43
+ .ag-input input { min-width: 0; background: transparent; border: 0; outline: 0; color: var(--fg); font: inherit; padding: 4px 0; }
44
+ .ag-input button, .ag-foot button { padding: 4px 10px; border-radius: 7px; background: var(--bg-3); color: var(--fg); }
45
+ .ag-foot { display: flex; align-items: center; gap: 8px; color: var(--fg-2); font-size: 11.5px; }
46
+ .ag-foot span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
47
+ .ag-tabs { display: flex; align-items: center; gap: 2px; }
48
+ .bar button.ag-tab { gap: 5px; padding: 0 7px; font-variant-numeric: tabular-nums; }
49
+ .ag-tab .ag-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--c); }
50
+ .ag-tab .ag-x { opacity: .55; padding-left: 2px; }
51
+ .ag-tab .ag-x:hover { opacity: 1; }
52
+ .bar button.ag-tab.cur { background: var(--bg-3); color: var(--fg); }
53
+ .bar button.ag-mode.on { background: color-mix(in srgb, #4f8cff 28%, var(--bg-3)); color: var(--fg); }
54
+ .bar button.ag-more { padding: 0 7px; font-variant-numeric: tabular-nums; }
55
+ .bar button.ag-more.lit { color: var(--fg); box-shadow: inset 0 0 0 1px var(--c); }
56
+ .ag-list { display: grid; gap: 4px; font-size: 13px; min-width: min(420px, calc(100vw - 48px)); }
57
+ .ag-list h4 { margin: 0 0 4px; font-size: 11px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em; color: var(--fg-2); }
58
+ .ag-row { display: grid; grid-template-columns: 8px 28px auto 1fr auto; gap: 8px; align-items: center; padding: 7px 10px; border-radius: 8px; background: var(--bg-2); color: var(--fg); text-align: left; cursor: pointer; border: 1px solid transparent; }
59
+ .ag-row:hover { background: var(--bg-3); }
60
+ .ag-row.cur { border-color: color-mix(in srgb, var(--c) 60%, var(--line)); }
61
+ .ag-row .ag-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--c); }
62
+ .ag-row .l { font-variant-numeric: tabular-nums; color: var(--fg-2); }
63
+ .ag-row .m { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--fg-2); }
64
+ .ag-row .ag-x { padding: 0 4px; color: var(--fg-2); }
65
+ .ag-row .ag-x:hover { color: var(--fg); }
66
+ .ag-row[data-state="working"] .ag-dot { animation: ag-beat 1.2s ease-in-out infinite; }
67
+ .ag-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 2px; }
68
+ .ag-head h4 { margin: 0; }
69
+ .ag-head .ag-mode { padding: 4px 10px; border-radius: 7px; background: var(--bg-3); color: var(--fg-2); }
70
+ .ag-head .ag-mode.on { background: color-mix(in srgb, #4f8cff 28%, var(--bg-3)); color: var(--fg); }
71
+ .bar .ag-phone { display: none; }
72
+ .bar button.ag-switch { gap: 5px; font-variant-numeric: tabular-nums; }
73
+ .ag-switch .ag-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--c); }
74
+ /* Phones: what fits one row. The outline says the state, the list has the words: the
75
+ session button (its dot the state) in place of the chip and tabs, Fresh and stop in its list, then the prompts and CLI. */
76
+ @media (max-width: 560px) {
77
+ .ag-chip, .ag-chip + .sep { display: none; }
78
+ .bar .ag-desk { display: none; }
79
+ .bar .ag-phone { display: flex; }
80
+ .bar button.ag-switch { padding: 6px 8px; }
81
+ .ag-row { grid-template-columns: 8px 24px auto 1fr auto; padding: 9px 8px; }
82
+ }`,q=()=>Object.assign(document.createElement("style"),{textContent:Ae});function o(r,n={},...s){let{class:a,data:w,...c}=n,u=Object.assign(document.createElement(r),c);a&&(u.className=a);for(let[M,S]of Object.entries(w??{}))u.dataset[M]=S;return u.append(...s.filter(M=>!!M)),u}async function Me(r){if(!(r.version>=5)||typeof r.addToolbar!="function")return;let n;try{let e=await fetch(`${z}/info`);if(!e.ok)return;n=await e.json()}catch{return}let s=n,a=()=>n.command,w=()=>({id:"",label:"",running:!1,command:a(),status:{state:"idle",source:"none"},sent:[]}),c=null,u=()=>s.sessions.find(e=>e.id===c)??s.sessions[s.sessions.length-1]??w(),M=()=>s.sessions.length>1,S=()=>s.mode==="fresh",v=e=>M()&&e.label?`${a()} ${e.label}`:a(),me=()=>new Set(s.sessions.flatMap(e=>e.sent??[])),Q=()=>n.hotkey?n.hotkey.split("+").map(e=>e.length===1?e.toUpperCase():e[0].toUpperCase()+e.slice(1)).join("+"):"",R=async(e,t={})=>{let i=await fetch(`${z}${e}`,{method:"POST",headers:{"content-type":"application/json","x-redline":"1","x-redline-agent":n.token},body:JSON.stringify(t)});if(!i.ok)throw new Error((await i.json().catch(()=>null))?.error??`HTTP ${i.status}`);return i.json()},P=(e,t={})=>R(e,{...t,...u().id?{id:u().id}:{}}),C=e=>r.toast(`Agent: ${e.message}`,!0),X=()=>Le(r,s.sessions),he=async e=>{let t=ue(e)?X():[];S()?r.toast(`Starting a new ${a()} session\u2026`):u().running||r.toast(`Starting ${a()}\u2026`);try{let i=await R("/send",{text:Ne(e.prompt,r,t),notes:t,...!S()&&u().id?{id:u().id}:{}});i?.id&&(c=i.id,T())}catch(i){C(i)}},Z=e=>{P("/answer",{choice:e}).catch(C)},H=!1,ee=async(e,t)=>{H=!0,k?.render();try{await P("/choose",t===void 0?{index:e}:{index:e,text:t})}catch(i){C(i)}finally{setTimeout(()=>{H=!1,k?.render()},400)}},F=e=>{P("/input",{data:e}).catch(C)},te=new Set,_={id:"",asking:!1},G=!1,N=!1,E=null,ne=e=>E?.contentWindow?.postMessage(e,location.origin),K=()=>`${z}/terminal${u().id?`?id=${encodeURIComponent(u().id)}`:""}`,O=null,k=null,U=null,J=new Map,se=e=>r.addButton({id:"agent",label:"Agent",key:n.key===!1?void 0:n.key||"a",title:e??`${a()}: agent toolbar${n.hotkey?` \xB7 ${Q()} from anywhere`:""}`,onClick:()=>k?.show(),active:()=>!!k?.isOpen()});se();function xe(e){let t=u(),i=I(t),h=t.status,m=t.running&&h.state==="blocked",d=m&&!!t.prompt?.options.length,g=m&&!d&&h.kind==="permission"&&n.approve,f=(p,x,L,A="",...Se)=>o("button",{class:A,title:x,onclick:L},p,...Se),l=o("div",{class:"ag-chip",title:`${v(t)}: ${i.label}${i.msg?` \xB7 ${i.msg}`:""}`,data:{state:t.running?h.state:"stopped"}},o("span",{class:"ag-dot"}),o("b",{},i.label),i.msg&&!d?o("span",{class:"ag-msg"},i.msg):null);l.style.setProperty("--c",i.color);let b=()=>o("div",{class:"sep"}),y=Re(s.sessions,t.id),$=s.sessions.filter(p=>y.hidden.includes(p.id)),D=null;if($.length){let p=V($.filter(x=>x.running&&(x.status.state==="working"||x.status.state==="blocked")));D=f(`+${$.length}`,`${$.length} more session${$.length===1?"":"s"}: show all`,()=>{N=!N,k?.render()},"ag-more"+(p?" lit":"")+(N?" on":"")),p&&D.style.setProperty("--c",I(p).color)}let W=M()||S()&&s.sessions.length?o("div",{class:"ag-tabs"},...s.sessions.filter(p=>y.shown.includes(p.id)).map(p=>{let x=I(p),L=o("button",{class:"ag-tab"+(p.id===t.id?" cur":""),title:`${v(p)}: ${x.label}${x.msg?` \xB7 ${x.msg}`:""}`,data:{id:p.id,state:p.running?p.status.state:"stopped"},onclick:()=>{c=p.id,T()}},o("span",{class:"ag-dot"}),p.label);return L.style.setProperty("--c",x.color),p.running||L.append(o("span",{class:"ag-x",title:"Close this session",onclick:A=>{A.stopPropagation(),R("/dismiss",{id:p.id}).catch(C)}},"\xD7")),L}),...D?[D]:[],b()):null,ye=g?[f("Allow",`Approve (${v(t)}'s prompt)`,()=>Z("allow"),"ag-allow"),f("Deny","Decline",()=>Z("deny"),"ag-deny")]:n.commands.map(p=>{let x=ue(p)?X().length:-1,L=x>=0?Y(r).length-x:0,A=f(p.label,(p.title??p.prompt)+(S()?" (in a new session)":"")+(L?` \xB7 ${L} note${L===1?" is":"s are"} already with a session`:""),()=>{he(p)},"",...x>=0?[o("span",{class:"count"},String(x))]:[]);return A.disabled=x===0||m&&!S(),A.dataset.cmd=p.label,A}),$e=oe("ag-desk"),le=f(m&&!g&&!d?"Answer in CLI":"CLI",O?.isOpen()?"Hide the terminal":`Show ${v(t)}'s terminal`,()=>O?.toggle(),O?.isOpen()?"on":"");le.dataset.cli="";let de=t.running?f("\u25A0",`Stop ${v(t)}`,()=>{P("/stop").catch(C)},"ag-stop ag-desk"):null,B=f(`${t.label||"Sessions"} \u25BE`,"All sessions, Fresh, stop",()=>{N=!N,k?.render()},"ag-switch ag-phone"+(N?" on":""));B.prepend(o("span",{class:"ag-dot"})),B.style.setProperty("--c",i.color),B.dataset.state=t.running?h.state:"stopped",W&&W.classList.add("ag-desk"),e.replaceChildren(q(),l,b(),...W?[W]:[],B,...ye,$e,b(),le,...de?[de]:[])}function oe(e=""){return o("button",{class:`ag-mode ${e}`+(S()?" on":""),title:S()?`On: every prompt starts a new session with just its notes (up to ${s.max} at once). Click for one shared session`:"Off: prompts go to one session that keeps its context. Click to start a new session for every prompt",onclick:()=>{R("/mode",{mode:S()?"shared":"fresh"}).then(i=>{s={...s,...i},T()}).catch(C)}},"Fresh")}function we(e){let t=u(),i=t.running?t.prompt:void 0;if(!i?.options.length){N&&ke(e);return}let h=I(t),m=o("div",{class:"ag-card",data:{kind:t.status.kind??"",session:t.id}});if(m.style.setProperty("--c",h.color),m.append(o("div",{class:"ag-who"},o("span",{class:"ag-dot"}),v(t)+" \xB7 "+h.label)),i.steps?.length){let g=i.steps.findIndex(l=>l.current),f=l=>{g>=0&&l!==g&&F((l>g?pe:ge).repeat(Math.abs(l-g)))};m.append(o("div",{class:"ag-steps"},o("button",{class:"nav",title:"Previous question",onclick:()=>F(ge)},"\u2190"),...i.steps.map((l,b)=>o("button",{class:(l.current?"cur ":"")+l.state,title:l.label,onclick:()=>f(b)},(l.state==="done"?"\u2612 ":l.state==="submit"?"\u2714 ":"\u2610 ")+l.label)),o("button",{class:"nav",title:"Next question",onclick:()=>F(pe)},"\u2192")))}i.lines.length&&m.append(o("pre",{class:"ag-ctx"},...i.lines.flatMap((g,f)=>[...f?[`
83
+ `]:[],...g.map(ve)]))),i.question&&m.append(o("div",{class:"ag-q"},i.question));let d=o("div",{class:"ag-opts"});i.options.forEach((g,f)=>{let l=o("span",{class:"n"},String(g.n??f+1));if(g.input){let b=document.createElement("input");b.placeholder=g.label.replace(/\.$/,"")+"\u2026",b.disabled=H;let y=()=>{let $=b.value.trim();$&&(b.blur(),ee(f,$))};b.addEventListener("keydown",$=>{$.key==="Enter"&&($.preventDefault(),y())}),b.addEventListener("focus",()=>{G=!0}),b.addEventListener("blur",()=>{G=!1}),d.append(o("div",{class:"ag-input"},l,b,o("button",{onclick:y,disabled:H},"Send")));return}d.append(o("button",{class:"ag-opt"+(g.selected?" sel":""),disabled:H,onclick:()=>{ee(f)},title:g.selected?"Selected in the CLI":""},l,o("span",{},g.label),g.description?o("span",{class:"d"},g.description):null))}),m.append(d,o("div",{class:"ag-foot"},o("span",{},i.hint??""),o("button",{title:"Esc in the CLI",onclick:()=>F(Te)},"Cancel"),o("button",{title:"Show "+v(t)+"'s terminal",onclick:()=>O?.show()},"CLI"))),e.append(q(),m)}function ke(e){let t=s.sessions.filter(d=>!d.running),i=[...s.sessions].reverse().map(d=>{let g=I(d),f=d.sent?.length??0,l=o("button",{class:"ag-row"+(d.id===u().id?" cur":""),title:`${v(d)}: ${g.label}${g.msg?` \xB7 ${g.msg}`:""}`,data:{id:d.id,state:d.running?d.status.state:"stopped"},onclick:()=>{c=d.id,N=!1,T()}},o("span",{class:"ag-dot"}),o("span",{class:"l"},d.label),o("b",{},g.label),o("span",{class:"m"},[f?`${f} note${f===1?"":"s"}`:"",g.msg??""].filter(Boolean).join(" \xB7 ")),d.running?o("span",{class:"ag-x",title:`Stop ${v(d)}`,onclick:b=>{b.stopPropagation(),R("/stop",{id:d.id}).catch(C)}},"\u25A0"):o("span",{class:"ag-x",title:"Close this session",onclick:b=>{b.stopPropagation(),R("/dismiss",{id:d.id}).catch(C)}},"\xD7"));return l.style.setProperty("--c",g.color),l}),h=async()=>{for(let d of t)await R("/dismiss",{id:d.id}).catch(C)},m=s.sessions.filter(d=>d.running).length;e.append(q(),o("div",{class:"ag-list"},o("div",{class:"ag-head"},o("h4",{},s.sessions.length?`Sessions \xB7 ${m} running`:"No sessions yet"),oe()),...i,o("div",{class:"ag-foot"},o("span",{},""),t.length?o("button",{onclick:()=>{h()}},`Close ended (${t.length})`):null,o("button",{onclick:()=>{N=!1,k?.render()}},"Done"))))}let ve=e=>{let t=o("span",{},e.t);return e.fg&&(t.style.color=e.fg),e.bg&&(t.style.background=e.bg),e.b&&(t.style.fontWeight="700"),e.dim&&(t.style.opacity=".6"),t};k=r.addToolbar({id:"agent",hotkey:n.hotkey||void 0,render:xe,attached:we,onShow(){u().id&&P("/ack").catch(()=>{}),T()},onHide(){T()}}),O=r.addWindow({id:"agent",title:a(),width:680,height:440,render(e){E=document.createElement("iframe"),E.src=K(),E.title=`${a()} terminal`,E.setAttribute("style","position:absolute;inset:0;width:100%;height:100%;border:0;background:#131316"),e.append(E)},onShow(){u().id&&P("/ack").catch(()=>{}),ne({type:"focus"}),k?.render()},onHide(){k?.render()},onResize(){ne({type:"fit"})}});function ae(){if(!U)return;let e=V(s.sessions)??u(),t=I(e),i=r.notes("all"),h=me(),m=i.filter(y=>h.has(y.id)&&!y.done).length,d=i.filter(y=>h.has(y.id)&&y.done).length,g=s.sessions.filter(y=>y.running).length,f=[g>1&&`${g} sessions`,m&&`${m} sent note${m===1?"":"s"} open`,d&&`${d} done`].filter(Boolean).join(" \xB7 "),l=e.running||e.status.state==="done"||e.status.state==="error"?`${v(e)}: ${t.label}${f?` \xB7 ${f}`:t.msg?` \xB7 ${t.msg}`:""}`:`Hand these notes to ${a()}`,b=o("button",{class:"ag-line",title:"Open the agent toolbar",onclick:()=>k?.show(),data:{state:e.running?e.status.state:"stopped"}},o("span",{class:"ag-dot"}),o("span",{},l),n.hotkey?o("kbd",{},Q()):n.key!==!1?o("kbd",{},(n.key||"a").toUpperCase()):null);b.style.setProperty("--c",t.color),U.replaceChildren(q(),b)}function re(e){let t=Pe(e.note,s.sessions,v),i=!!t&&/working|asking/.test(t.cls);e.el.replaceChildren(...t?[o("span",{class:`ag-tag ${t.cls}`,...i?{title:"Open the agent toolbar",onclick:()=>k?.show()}:{}},t.text)]:[])}r.addPanelSection({id:"agent",render(e){U=e,ae()}}),r.addCardDecorator({id:"agent",render(e,t){let i={el:t,note:e};J.set(e.id,i),re(i)}});function T(){c&&!s.sessions.some(l=>l.id===c)&&(c=null);let e=l=>l.running&&l.status.state==="blocked",t=s.sessions.filter(e).map(l=>l.id),i=t.find(l=>!te.has(l));te=new Set(t);let h=u(),m=_.id===h.id&&_.asking&&!e(h),d=i??(m?t[0]:void 0);d&&!G&&!e(h)&&(c=d),_={id:u().id,asking:e(u())};let g=V(s.sessions),f=g?be({...g,command:v(g)}):null;r.setStatus("agent",f?{...f,onClick:()=>k?.show()}:null),se(f?.title),E&&!E.src.endsWith(K())&&(E.src=K()),O?.setTitle(v(u())),k?.render(),ae();for(let l of J.values())l.el.isConnected?re(l):J.delete(l.note.id)}let j=null,ie=()=>{j?.close(),j=new EventSource(`${z}/stream?status=1&token=${encodeURIComponent(n.token)}`);for(let e of["hello","status"])j.addEventListener(e,t=>{s=JSON.parse(t.data),T()});j.onerror=async()=>{if(j?.readyState===EventSource.CLOSED)try{let e=await fetch(`${z}/info`);e.ok&&(n=await e.json(),s=n,T(),setTimeout(ie,1e3))}catch{}}};ie(),window.addEventListener("message",e=>{e.origin!==location.origin||e.source!==E?.contentWindow||(e.data?.type==="ack"&&P("/ack").catch(()=>{}),e.data?.type==="session"&&typeof e.data.id=="string"&&(c=e.data.id,T()))}),T()}export{Me as default,Ne as fill,Le as freeIds,be as indicatorFor,He as noteTag,V as overall,I as stateLabel,Pe as tagFor,Re as visibleTabs};