specrails-hub 1.61.4 → 1.62.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.
Files changed (32) hide show
  1. package/README.md +40 -7
  2. package/client/dist/assets/{ActivityFeedPage-B_E9mK4R.js → ActivityFeedPage-EBZPstfo.js} +1 -1
  3. package/client/dist/assets/{AgentsPage-DcI2u8_X.js → AgentsPage-BZZVEFCY.js} +3 -3
  4. package/client/dist/assets/AnalyticsPage-KZmfI07R.js +1 -0
  5. package/client/dist/assets/{BarChart-DzsFGC0T.js → BarChart-B9252cvV.js} +2 -2
  6. package/client/dist/assets/CodePage-CbN_zM1g.js +3 -0
  7. package/client/dist/assets/DocsDialog-v9hTkX8F.js +11 -0
  8. package/client/dist/assets/{DocsPage-C7OMQVSQ.js → DocsPage-CjOO0dbM.js} +2 -2
  9. package/client/dist/assets/{ExportDropdown-23I2FamX.js → ExportDropdown-Dzxx0V6g.js} +1 -1
  10. package/client/dist/assets/{HubAnalyticsPage-C1FeRTOx.js → HubAnalyticsPage-DZjlXZtY.js} +1 -1
  11. package/client/dist/assets/{IntegrationsPage-D2FMRRT3.js → IntegrationsPage-RhQ6cpUY.js} +2 -2
  12. package/client/dist/assets/JobDetailPage-BbjSYik-.js +16 -0
  13. package/client/dist/assets/JobsPage-pVQ_7GtV.js +1 -0
  14. package/client/dist/assets/{dist-js-B-iYrpW3.js → dist-js-BKKNuWzk.js} +1 -1
  15. package/client/dist/assets/{dist-js-C6NUWouW.js → dist-js-DZQQtn9U.js} +1 -1
  16. package/client/dist/assets/index-C59a1tUC.css +2 -0
  17. package/client/dist/assets/{index-DH6oVfkR.js → index-CdTQ6ble.js} +49 -47
  18. package/client/dist/assets/{lib-BGjYU-cr.js → lib-BRPL9fm2.js} +1 -1
  19. package/client/dist/assets/{useProjectCache-BPHHM-II.js → useProjectCache-C1g09Sfr.js} +1 -1
  20. package/client/dist/index.html +2 -2
  21. package/package.json +1 -1
  22. package/server/dist/browser-capture-manager.js +162 -1
  23. package/server/dist/browser-network.js +149 -0
  24. package/server/dist/browser-playwright.js +329 -4
  25. package/server/dist/index.js +1 -1
  26. package/server/dist/project-router.js +91 -1
  27. package/client/dist/assets/AnalyticsPage-wZzvK4jf.js +0 -1
  28. package/client/dist/assets/CodePage-DKS1CJNS.js +0 -3
  29. package/client/dist/assets/DocsDialog-qICmfuDa.js +0 -11
  30. package/client/dist/assets/JobDetailPage-Cu2RMX8t.js +0 -16
  31. package/client/dist/assets/JobsPage-CsmfHKg1.js +0 -1
  32. package/client/dist/assets/index-bjvfBl-B.css +0 -2
package/README.md CHANGED
@@ -12,6 +12,7 @@ It's a local-first companion to [specrails-core](https://github.com/fjpulidop/sp
12
12
 
13
13
  - **Draft a spec by talking to Claude or Codex** — open Explore, describe what you want; the live draft updates each turn. Save it as a draft and come back later, or commit when it looks right.
14
14
  - **Generate a spec in one shot** — Quick mode for when you already know what you want. Optionally enrich it with a "Contract Layer" of names, shapes, invariants, and a file touch list.
15
+ - **Turn a live website into a spec** — *Add Spec → From a website* opens an embedded browser. Navigate to a page, hover-select an element or drag a rectangle, and the screenshot + rich DOM + applied CSS become attachments that feed Quick or Explore. The desktop app ships its own Chromium, so it works offline.
15
16
  - **Drag specs onto execution rails** — each rail is an independent lane. Run multiple specs in parallel, with different agent profiles per rail.
16
17
  - **Compare two specs side by side** — drag any spec modal to the edge of the screen; a picker of your todo specs appears on the other side. Pick one and they live next to each other. Tablet-style.
17
18
  - **Split a big epic** — SMASH a parent spec into a family of sub-specs in one click; the children carry short summaries on their cards.
@@ -71,8 +72,8 @@ If the project doesn't have specrails-core yet, the setup wizard walks you throu
71
72
  The provider is chosen per-project at install time and is immutable after
72
73
  creation. On macOS, the desktop app handles Homebrew/Volta/nvm paths
73
74
  automatically — see [docs/platforms/macos.md](docs/platforms/macos.md).
74
- **Windows users:** see [docs/windows.md](docs/windows.md) for Windows
75
- 10/11 specifics. For Codex-specific topics — auth, sandbox config,
75
+ **Windows users:** see [docs/platforms/windows.md](docs/platforms/windows.md)
76
+ for Windows 10/11 specifics. For Codex-specific topics — auth, sandbox config,
76
77
  estimated cost caveats, plugin support, and emergency rollback — see
77
78
  [docs/codex.md](docs/codex.md).
78
79
 
@@ -111,12 +112,44 @@ npm run dev # server (4200) + client (4201)
111
112
 
112
113
  | Script | Description |
113
114
  |--------|-------------|
114
- | `npm run dev` | Server + client with hot reload |
115
- | `npm run build` | Production build |
116
- | `npm test` | vitest |
117
- | `npm run tauri dev` | Run desktop app in dev mode |
115
+ | `npm run dev` | Server (4200) + client (4201) with hot reload |
116
+ | `npm run dev:desktop` | Desktop app (Tauri) in dev mode |
117
+ | `npm run build` | Production build (client + server + CLI) |
118
+ | `npm test` | vitest (server + CLI) |
119
+ | `npm run test:coverage` | Server coverage (mirrors the CI gate) |
120
+ | `npm run build:desktop` | Desktop installers — ships an **empty** `runtimes/` |
121
+ | `npm run build:desktop:local` | Self-contained desktop app (macOS arm64) |
118
122
 
119
- CI gates coverage hard: 70 % global, 80 % server, 80 % client. Local runs must clear the same bars.
123
+ CI gates coverage hard: 70 % global, 80 % server, 80 % client. Local runs must clear the same bars (`npm run test:coverage`, and `cd client && npm run test:coverage`).
124
+
125
+ ### Building the desktop app locally
126
+
127
+ `npm run build:desktop` builds the `.app`/`.dmg`/`.exe` but does **not** assemble
128
+ the bundled Node/Git/Chromium runtimes — the resulting app falls back to your
129
+ system PATH, and the embedded browser downloads a Playwright-managed Chromium on
130
+ first use. To build a self-contained app the way CI does (**macOS arm64 only**):
131
+
132
+ ```bash
133
+ # Bundle Node 22 + a relocatable Git into src-tauri/runtimes/, then build.
134
+ npm run build:desktop:local
135
+
136
+ # …and bundle Chromium too, so "Add Spec from a website" works fully offline.
137
+ # Adds a one-time ~150 MB Playwright Chromium download.
138
+ BUNDLE_CHROMIUM=true npm run build:desktop:local
139
+ ```
140
+
141
+ Notes:
142
+
143
+ - **Local builds are unsigned.** Gatekeeper will warn ("unidentified developer") —
144
+ right-click → Open, or run `xattr -dr com.apple.quarantine <App>.app`.
145
+ - Locally, Chromium is bundled **unpacked**; the app launches it straight from
146
+ `Contents/Resources/runtimes/chromium`.
147
+ - The signed + notarized installers are produced only by the `desktop-release` CI
148
+ workflow. There, Chromium is shipped as an obfuscated `chromium.pak` blob — the
149
+ notarization service recurses into plain archives and would reject Chromium's
150
+ ad-hoc-signed binaries, so the magic bytes are XOR-broken to make it opaque. The
151
+ app reverses the XOR and extracts Chromium to `~/.specrails/runtimes/chromium`
152
+ on first use, where Google's ad-hoc signature is enough to run it.
120
153
 
121
154
  ## Security model
122
155
 
@@ -1 +1 @@
1
- import{r as e}from"./chunk-CilyBKbf.js";import{A as t,Bt as n,G as r,at as i,bt as a,gt as o,ht as s,it as c,vt as l,xt as u,yt as d}from"./index-DH6oVfkR.js";var f=o(`ban`,[[`circle`,{cx:`12`,cy:`12`,r:`10`,key:`1mglay`}],[`path`,{d:`M4.929 4.929 19.07 19.071`,key:`196cmz`}]]),p=e(n(),1);function m(e){let t=new Set;return e.filter(e=>{let n=`${e.type}:${e.jobId}`;return t.has(n)?!1:(t.add(n),!0)})}function h({activeProjectId:e,limit:t=50}){let[n,r]=(0,p.useState)([]),[i,a]=(0,p.useState)(!1),[o,s]=(0,p.useState)(!0),c=(0,p.useRef)(e);(0,p.useEffect)(()=>{c.current=e},[e]);let{registerHandler:l,unregisterHandler:f}=d();(0,p.useEffect)(()=>{if(!e){r([]),s(!0);return}let n=!1;a(!0),r([]),s(!0);async function i(){let e=u();try{let i=await fetch(`${e}/activity?limit=${t}`);if(!i.ok||n)return;let a=await i.json();if(n)return;r(a),s(a.length===t)}catch{}finally{n||a(!1)}}return i(),()=>{n=!0}},[e,t]);let h=(0,p.useCallback)(async()=>{if(i||!o)return;a(!0);let e=u();try{r(n=>{let i=n[n.length-1];if(!i)return n;let o=encodeURIComponent(i.timestamp);return fetch(`${e}/activity?limit=${t}&before=${o}`).then(e=>e.json()).then(e=>{r(t=>m([...t,...e])),s(e.length===t),a(!1)}).catch(()=>a(!1)),n})}catch{a(!1)}},[i,o,t]),g=(0,p.useCallback)(e=>{let t=e,n=c.current;if(!(!t||typeof t.type!=`string`)&&!(t.projectId&&t.projectId!==n)){if(t.type===`queue`&&Array.isArray(t.jobs)){let e=[];for(let n of t.jobs){let t=n.status===`completed`?`job_completed`:n.status===`failed`?`job_failed`:n.status===`canceled`?`job_canceled`:`job_started`;e.push({id:`${t}:${n.id}`,type:t,jobId:n.id,jobCommand:n.command??``,timestamp:n.startedAt??new Date().toISOString(),summary:`${t.replace(`_`,` `)}: ${n.command??``}`,costUsd:null})}e.length>0&&r(t=>m([...e,...t]))}if(t.type===`phase`&&t.phase&&t.state&&t.timestamp){let e={id:`phase:${t.phase}:${t.state}:${t.timestamp}`,type:`job_started`,jobId:`phase-${t.phase}`,jobCommand:`Phase: ${t.phase} → ${t.state}`,timestamp:t.timestamp,summary:`Phase ${t.phase} is ${t.state}`,costUsd:null};r(t=>m([e,...t]))}}},[]);return(0,p.useLayoutEffect)(()=>(l(`activity`,g),()=>f(`activity`)),[g,l,f]),{items:n,loading:i,hasMore:o,loadMore:h}}var g=a();function _(e){let t=Date.now()-new Date(e).getTime(),n=Math.floor(t/1e3);if(n<60)return`${n}s ago`;let r=Math.floor(n/60);if(r<60)return`${r}m ago`;let i=Math.floor(r/60);return i<24?`${i}h ago`:`${Math.floor(i/24)}d ago`}function v({type:e}){switch(e){case`job_completed`:return(0,g.jsx)(i,{className:`w-4 h-4 text-green-500 flex-shrink-0`});case`job_failed`:return(0,g.jsx)(c,{className:`w-4 h-4 text-red-500 flex-shrink-0`});case`job_canceled`:return(0,g.jsx)(f,{className:`w-4 h-4 text-muted-foreground flex-shrink-0`});default:return(0,g.jsx)(t,{className:`w-4 h-4 text-blue-500 flex-shrink-0`})}}function y(e){switch(e){case`job_completed`:return`Completed`;case`job_failed`:return`Failed`;case`job_canceled`:return`Canceled`;default:return`Started`}}function b(e){switch(e){case`job_completed`:return`text-green-500`;case`job_failed`:return`text-red-500`;case`job_canceled`:return`text-muted-foreground`;default:return`text-blue-500`}}function x(){let{activeProjectId:e}=l(),{items:t,loading:n,hasMore:i,loadMore:a}=h({activeProjectId:e}),o=(0,p.useRef)(null);return(0,p.useEffect)(()=>{let e=o.current;if(!e)return;let t=new IntersectionObserver(e=>{e[0].isIntersecting&&i&&!n&&a()},{threshold:.1});return t.observe(e),()=>t.disconnect()},[i,n,a]),(0,g.jsxs)(`div`,{className:`flex flex-col h-full overflow-hidden`,children:[(0,g.jsxs)(`div`,{className:`flex items-center gap-2 px-4 py-3 border-b border-border bg-background/50`,children:[(0,g.jsx)(s,{className:`w-4 h-4 text-muted-foreground`}),(0,g.jsx)(`h1`,{className:`text-sm font-medium`,children:`Activity`})]}),(0,g.jsxs)(`div`,{className:`flex-1 overflow-y-auto`,children:[n&&t.length===0?(0,g.jsx)(`div`,{className:`flex items-center justify-center h-32`,children:(0,g.jsx)(r,{className:`w-4 h-4 animate-spin text-muted-foreground`})}):t.length===0?(0,g.jsxs)(`div`,{className:`flex flex-col items-center justify-center h-48 gap-2 text-muted-foreground`,children:[(0,g.jsx)(s,{className:`w-8 h-8 opacity-40`}),(0,g.jsx)(`p`,{className:`text-sm`,children:`No activity yet`}),(0,g.jsx)(`p`,{className:`text-xs opacity-70`,children:`Job events will appear here when jobs run`})]}):(0,g.jsx)(`ul`,{className:`divide-y divide-border/50`,children:t.map(e=>(0,g.jsxs)(`li`,{className:`flex items-center gap-3 px-4 py-2.5 hover:bg-accent/30 transition-colors`,children:[(0,g.jsx)(v,{type:e.type}),(0,g.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,g.jsx)(`p`,{className:`text-xs text-foreground truncate`,title:e.jobCommand,children:e.jobCommand}),(0,g.jsxs)(`div`,{className:`flex items-center gap-2 mt-0.5`,children:[(0,g.jsx)(`span`,{className:`text-xs font-medium ${b(e.type)}`,children:y(e.type)}),e.costUsd!=null&&(0,g.jsxs)(`span`,{className:`text-xs text-muted-foreground`,children:[`$`,e.costUsd.toFixed(4)]})]})]}),(0,g.jsx)(`span`,{className:`text-xs text-muted-foreground flex-shrink-0 tabular-nums`,children:_(e.timestamp)})]},`${e.type}:${e.jobId}`))}),(0,g.jsx)(`div`,{ref:o,className:`h-1`}),n&&t.length>0&&(0,g.jsx)(`div`,{className:`flex justify-center py-3`,children:(0,g.jsx)(r,{className:`w-4 h-4 animate-spin text-muted-foreground`})}),!i&&t.length>0&&(0,g.jsx)(`p`,{className:`text-center text-xs text-muted-foreground py-3`,children:`All activity loaded`})]})]})}export{x as default};
1
+ import{r as e}from"./chunk-CilyBKbf.js";import{A as t,G as n,St as r,Vt as i,_t as a,at as o,bt as s,gt as c,it as l,xt as u,yt as d}from"./index-CdTQ6ble.js";var f=a(`ban`,[[`circle`,{cx:`12`,cy:`12`,r:`10`,key:`1mglay`}],[`path`,{d:`M4.929 4.929 19.07 19.071`,key:`196cmz`}]]),p=e(i(),1);function m(e){let t=new Set;return e.filter(e=>{let n=`${e.type}:${e.jobId}`;return t.has(n)?!1:(t.add(n),!0)})}function h({activeProjectId:e,limit:t=50}){let[n,i]=(0,p.useState)([]),[a,o]=(0,p.useState)(!1),[c,l]=(0,p.useState)(!0),u=(0,p.useRef)(e);(0,p.useEffect)(()=>{u.current=e},[e]);let{registerHandler:d,unregisterHandler:f}=s();(0,p.useEffect)(()=>{if(!e){i([]),l(!0);return}let n=!1;o(!0),i([]),l(!0);async function a(){let e=r();try{let r=await fetch(`${e}/activity?limit=${t}`);if(!r.ok||n)return;let a=await r.json();if(n)return;i(a),l(a.length===t)}catch{}finally{n||o(!1)}}return a(),()=>{n=!0}},[e,t]);let h=(0,p.useCallback)(async()=>{if(a||!c)return;o(!0);let e=r();try{i(n=>{let r=n[n.length-1];if(!r)return n;let a=encodeURIComponent(r.timestamp);return fetch(`${e}/activity?limit=${t}&before=${a}`).then(e=>e.json()).then(e=>{i(t=>m([...t,...e])),l(e.length===t),o(!1)}).catch(()=>o(!1)),n})}catch{o(!1)}},[a,c,t]),g=(0,p.useCallback)(e=>{let t=e,n=u.current;if(!(!t||typeof t.type!=`string`)&&!(t.projectId&&t.projectId!==n)){if(t.type===`queue`&&Array.isArray(t.jobs)){let e=[];for(let n of t.jobs){let t=n.status===`completed`?`job_completed`:n.status===`failed`?`job_failed`:n.status===`canceled`?`job_canceled`:`job_started`;e.push({id:`${t}:${n.id}`,type:t,jobId:n.id,jobCommand:n.command??``,timestamp:n.startedAt??new Date().toISOString(),summary:`${t.replace(`_`,` `)}: ${n.command??``}`,costUsd:null})}e.length>0&&i(t=>m([...e,...t]))}if(t.type===`phase`&&t.phase&&t.state&&t.timestamp){let e={id:`phase:${t.phase}:${t.state}:${t.timestamp}`,type:`job_started`,jobId:`phase-${t.phase}`,jobCommand:`Phase: ${t.phase} → ${t.state}`,timestamp:t.timestamp,summary:`Phase ${t.phase} is ${t.state}`,costUsd:null};i(t=>m([e,...t]))}}},[]);return(0,p.useLayoutEffect)(()=>(d(`activity`,g),()=>f(`activity`)),[g,d,f]),{items:n,loading:a,hasMore:c,loadMore:h}}var g=u();function _(e){let t=Date.now()-new Date(e).getTime(),n=Math.floor(t/1e3);if(n<60)return`${n}s ago`;let r=Math.floor(n/60);if(r<60)return`${r}m ago`;let i=Math.floor(r/60);return i<24?`${i}h ago`:`${Math.floor(i/24)}d ago`}function v({type:e}){switch(e){case`job_completed`:return(0,g.jsx)(o,{className:`w-4 h-4 text-green-500 flex-shrink-0`});case`job_failed`:return(0,g.jsx)(l,{className:`w-4 h-4 text-red-500 flex-shrink-0`});case`job_canceled`:return(0,g.jsx)(f,{className:`w-4 h-4 text-muted-foreground flex-shrink-0`});default:return(0,g.jsx)(t,{className:`w-4 h-4 text-blue-500 flex-shrink-0`})}}function y(e){switch(e){case`job_completed`:return`Completed`;case`job_failed`:return`Failed`;case`job_canceled`:return`Canceled`;default:return`Started`}}function b(e){switch(e){case`job_completed`:return`text-green-500`;case`job_failed`:return`text-red-500`;case`job_canceled`:return`text-muted-foreground`;default:return`text-blue-500`}}function x(){let{activeProjectId:e}=d(),{items:t,loading:r,hasMore:i,loadMore:a}=h({activeProjectId:e}),o=(0,p.useRef)(null);return(0,p.useEffect)(()=>{let e=o.current;if(!e)return;let t=new IntersectionObserver(e=>{e[0].isIntersecting&&i&&!r&&a()},{threshold:.1});return t.observe(e),()=>t.disconnect()},[i,r,a]),(0,g.jsxs)(`div`,{className:`flex flex-col h-full overflow-hidden`,children:[(0,g.jsxs)(`div`,{className:`flex items-center gap-2 px-4 py-3 border-b border-border bg-background/50`,children:[(0,g.jsx)(c,{className:`w-4 h-4 text-muted-foreground`}),(0,g.jsx)(`h1`,{className:`text-sm font-medium`,children:`Activity`})]}),(0,g.jsxs)(`div`,{className:`flex-1 overflow-y-auto`,children:[r&&t.length===0?(0,g.jsx)(`div`,{className:`flex items-center justify-center h-32`,children:(0,g.jsx)(n,{className:`w-4 h-4 animate-spin text-muted-foreground`})}):t.length===0?(0,g.jsxs)(`div`,{className:`flex flex-col items-center justify-center h-48 gap-2 text-muted-foreground`,children:[(0,g.jsx)(c,{className:`w-8 h-8 opacity-40`}),(0,g.jsx)(`p`,{className:`text-sm`,children:`No activity yet`}),(0,g.jsx)(`p`,{className:`text-xs opacity-70`,children:`Job events will appear here when jobs run`})]}):(0,g.jsx)(`ul`,{className:`divide-y divide-border/50`,children:t.map(e=>(0,g.jsxs)(`li`,{className:`flex items-center gap-3 px-4 py-2.5 hover:bg-accent/30 transition-colors`,children:[(0,g.jsx)(v,{type:e.type}),(0,g.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,g.jsx)(`p`,{className:`text-xs text-foreground truncate`,title:e.jobCommand,children:e.jobCommand}),(0,g.jsxs)(`div`,{className:`flex items-center gap-2 mt-0.5`,children:[(0,g.jsx)(`span`,{className:`text-xs font-medium ${b(e.type)}`,children:y(e.type)}),e.costUsd!=null&&(0,g.jsxs)(`span`,{className:`text-xs text-muted-foreground`,children:[`$`,e.costUsd.toFixed(4)]})]})]}),(0,g.jsx)(`span`,{className:`text-xs text-muted-foreground flex-shrink-0 tabular-nums`,children:_(e.timestamp)})]},`${e.type}:${e.jobId}`))}),(0,g.jsx)(`div`,{ref:o,className:`h-1`}),r&&t.length>0&&(0,g.jsx)(`div`,{className:`flex justify-center py-3`,children:(0,g.jsx)(n,{className:`w-4 h-4 animate-spin text-muted-foreground`})}),!i&&t.length>0&&(0,g.jsx)(`p`,{className:`text-center text-xs text-muted-foreground py-3`,children:`All activity loaded`})]})]})}export{x as default};
@@ -1,4 +1,4 @@
1
- import{r as e}from"./chunk-CilyBKbf.js";import{$ as t,At as n,B as r,Bt as i,C as a,Ct as o,Dt as s,E as c,Et as l,F as u,G as d,H as f,I as p,L as m,M as h,Mt as g,N as _,Nt as v,O as y,Ot as b,P as x,Pt as S,S as C,St as w,T,Tt as E,U as D,W as O,X as k,a as A,b as j,bt as M,ct as N,ft as P,gt as F,j as I,jt as L,kt as R,lt as ee,mt as z,ot as B,pt as te,q as V,tt as ne,ut as re,vt as ie,w as H,wt as U,x as ae,xt as W,y as G,yt as oe}from"./index-DH6oVfkR.js";var K=F(`flask-conical`,[[`path`,{d:`M14 2v6a2 2 0 0 0 .245.96l5.51 10.08A2 2 0 0 1 18 22H6a2 2 0 0 1-1.755-2.96l5.51-10.08A2 2 0 0 0 10 8V2`,key:`18mbvz`}],[`path`,{d:`M6.453 15h11.094`,key:`3shlmq`}],[`path`,{d:`M8.5 2h7`,key:`csnxdl`}]]),q=F(`history`,[[`path`,{d:`M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8`,key:`1357e3`}],[`path`,{d:`M3 3v5h5`,key:`1xhq8a`}],[`path`,{d:`M12 7v5l4 2`,key:`1fdv2h`}]]),se=F(`pin`,[[`path`,{d:`M12 17v5`,key:`bb1du9`}],[`path`,{d:`M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z`,key:`1nkz8b`}]]),ce=F(`wand-sparkles`,[[`path`,{d:`m21.64 3.64-1.28-1.28a1.21 1.21 0 0 0-1.72 0L2.36 18.64a1.21 1.21 0 0 0 0 1.72l1.28 1.28a1.2 1.2 0 0 0 1.72 0L21.64 5.36a1.2 1.2 0 0 0 0-1.72`,key:`ul74o6`}],[`path`,{d:`m14 7 3 3`,key:`1r5n42`}],[`path`,{d:`M5 6v4`,key:`ilb8ba`}],[`path`,{d:`M19 14v4`,key:`blhpug`}],[`path`,{d:`M10 2v2`,key:`7u0qdc`}],[`path`,{d:`M7 8H3`,key:`zfb6yr`}],[`path`,{d:`M21 16h-4`,key:`1cnmox`}],[`path`,{d:`M11 3H9`,key:`1obp7u`}]]),J=e(i(),1),Y=M(),X=/^[a-z0-9][a-z0-9-]*$/;function le({open:e,mode:t=`add`,initial:n,chainAgents:r,onConfirm:i,onCancel:o}){let[s,c]=(0,J.useState)(``),[l,u]=(0,J.useState)(r[0]??``);(0,J.useEffect)(()=>{e&&(t===`edit`&&n?(c(n.tags.join(`, `)),u(r.includes(n.agent)?n.agent:r[0]??``)):(c(``),u(r[0]??``)))},[e,t,n,r]);let d=s.split(`,`).map(e=>e.trim()).filter(Boolean),f=d.filter(e=>!X.test(e)),p=d.length>0&&f.length===0&&r.includes(l);return(0,Y.jsx)(G,{open:e,onOpenChange:e=>{e||o()},children:(0,Y.jsxs)(j,{className:`max-w-md`,children:[(0,Y.jsxs)(a,{children:[(0,Y.jsx)(H,{children:t===`edit`?`Edit routing rule`:`Add routing rule`}),(0,Y.jsx)(ae,{children:t===`edit`?`Update the tags or target agent for this rule.`:`Add a tag-matched routing rule using lowercase kebab-case tags.`})]}),(0,Y.jsxs)(`div`,{className:`py-2 space-y-3`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Tags`}),(0,Y.jsx)(A,{autoFocus:!0,value:s,onChange:e=>c(e.target.value),placeholder:`frontend, ui`,"aria-label":`Tags`,className:`text-sm font-mono`}),(0,Y.jsxs)(`p`,{className:`text-[11px] text-muted-foreground mt-1`,children:[`Comma-separated. Use lowercase kebab-case like `,(0,Y.jsx)(`code`,{children:`frontend`}),` or `,(0,Y.jsx)(`code`,{children:`api-design`}),`.`]}),f.length>0&&(0,Y.jsxs)(`p`,{className:`text-[11px] text-red-400 mt-1`,children:[`Invalid tag`,f.length===1?``:`s`,`: `,f.join(`, `),`. Tags must use lowercase letters, digits, and hyphens only.`]})]}),(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Route to`}),(0,Y.jsx)(`select`,{value:l,onChange:e=>u(e.target.value),"aria-label":`Route to`,className:`w-full h-9 px-2 text-sm rounded-md border border-border bg-background`,children:r.map(e=>(0,Y.jsx)(`option`,{value:e,children:e},e))}),(0,Y.jsx)(`p`,{className:`text-[11px] text-muted-foreground mt-1`,children:`Only agents in this profile's chain can be routing targets.`})]})]}),(0,Y.jsxs)(C,{children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:o,children:`Cancel`}),(0,Y.jsx)(y,{size:`sm`,onClick:()=>i(d,l),disabled:!p,children:t===`edit`?`Save changes`:`Add rule`})]})]})})}var Z=new Set([`sr-architect`,`sr-developer`,`sr-reviewer`,`sr-merge-resolver`]),ue=[`sonnet`,`opus`,`haiku`],de=/^[a-z0-9][a-z0-9-]*$/;function fe({profile:e,onChange:t,footer:r,onValidityChange:i,onSoftWarningsChange:a}){let[c,u]=(0,J.useState)([]),[d,p]=(0,J.useState)(!1),[m,h]=(0,J.useState)(!1),[_,v]=(0,J.useState)(null);(0,J.useEffect)(()=>{let e=!1;return fetch(`${W()}/profiles/catalog`).then(e=>e.ok?e.json():{agents:[]}).then(t=>{e||u(t.agents)}).catch(()=>{e||u([])}),()=>{e=!0}},[]);let x=n=>{let r=JSON.parse(JSON.stringify(e));n(r),t(r)},S=new Set(e.agents.map(e=>e.id)),C=c.filter(e=>!S.has(e.id)),T=(0,J.useMemo)(()=>{let t=[];for(let n of Z)e.agents.some(e=>e.id===n)||t.push(`Missing required baseline agent: ${n}`);let n=e.routing.filter(e=>`default`in e&&e.default===!0);if(n.length>1&&t.push(`Routing may have at most one default rule (found ${n.length})`),n.length===1){let n=e.routing[e.routing.length-1];`default`in n&&n.default===!0||t.push(`The default routing rule must be the last entry`)}for(let n of e.routing)if(e.agents.some(e=>e.id===n.agent)||t.push(`Routing references agent not in the chain: ${n.agent}`),`tags`in n){let e=n.tags.filter(e=>!de.test(e));e.length>0&&t.push(`Routing rule ${n.agent} has invalid tags: ${e.join(`, `)} (use lowercase kebab-case)`)}return t},[e]),E=(0,J.useMemo)(()=>{if(e.routing.length===0)return[];let t=new Set(e.routing.map(e=>e.agent));return e.agents.filter(e=>!Z.has(e.id)&&!t.has(e.id)).map(e=>e.id)},[e]),D=(0,J.useMemo)(()=>e.routing.some(e=>`default`in e&&e.default===!0),[e.routing]);(0,J.useEffect)(()=>{i&&i(T)},[T,i]),(0,J.useEffect)(()=>{a&&a({agentsMissingRouting:E})},[E,a]);let O=e=>{x(t=>{let n={id:e,model:`sonnet`},r=t.agents.findIndex(e=>e.id===`sr-merge-resolver`);r>=0?t.agents.splice(r,0,n):t.agents.push(n)}),p(!1)},k=t=>{let n=e.agents[t];Z.has(n.id)||x(e=>{e.agents.splice(t,1),e.routing=e.routing.filter(e=>e.agent!==n.id)})},j=(t,n)=>{if(t===n)return;let r=e.agents.findIndex(e=>e.id===t),i=e.agents.findIndex(e=>e.id===n);if(r<0||i<0)return;let a=o(e.agents,r,i),s=a.findIndex(e=>e.id===`sr-architect`);if(s>0){let[e]=a.splice(s,1);a.unshift(e)}let c=a.findIndex(e=>e.id===`sr-merge-resolver`);if(c>=0&&c!==a.length-1){let[e]=a.splice(c,1);a.push(e)}x(e=>{e.agents=a})},M=g(L(R,{activationConstraint:{distance:4}}),L(b,{coordinateGetter:U})),N=e=>{let{active:t,over:n}=e;n&&j(String(t.id),String(n.id))},P=(e,t)=>{x(n=>{n.agents[e].model=t})},F=(e,t)=>{x(n=>{let r={tags:e,agent:t},i=n.routing.findIndex(e=>`default`in e&&e.default===!0);i>=0?n.routing.splice(i,0,r):n.routing.push(r)}),h(!1)},ee=()=>{x(e=>{e.routing.some(e=>`default`in e&&e.default===!0)||e.agents.some(e=>e.id===`sr-developer`)&&e.routing.push({default:!0,agent:`sr-developer`})})},z=e=>`default`in e&&e.default===!0,B=(e,t)=>{x(n=>{let r=n.routing[e];r&&(z(r)||(r.agent=t))})},te=(e,t,n)=>{x(r=>{let i=r.routing[e];i&&(z(i)||(i.tags=t,i.agent=n))})},V=e=>{x(t=>{let n=t.routing[e];n&&(z(n)||t.routing.splice(e,1))})},ne=(t,n)=>{let r=t+n;if(r<0||r>=e.routing.length)return;let i=e.routing[t],a=e.routing[r];z(i)||z(a)||x(e=>{let[n]=e.routing.splice(t,1);e.routing.splice(r,0,n)})};return(0,Y.jsxs)(`div`,{className:`p-6 space-y-6 max-w-3xl`,children:[(0,Y.jsx)(le,{open:m,mode:`add`,chainAgents:e.agents.map(e=>e.id),onConfirm:F,onCancel:()=>h(!1)}),(0,Y.jsx)(le,{open:_!==null,mode:`edit`,initial:_!==null&&e.routing[_]&&!(`default`in e.routing[_])?{tags:e.routing[_].tags,agent:e.routing[_].agent}:void 0,chainAgents:e.agents.map(e=>e.id),onConfirm:(e,t)=>{_!==null&&(te(_,e,t),v(null))},onCancel:()=>v(null)}),T.length>0&&(0,Y.jsxs)(`div`,{className:`px-3 py-2 text-xs rounded-md border border-yellow-500/30 bg-yellow-500/10 text-yellow-500`,children:[(0,Y.jsxs)(`div`,{className:`font-medium mb-1`,children:[T.length,` validation `,T.length===1?`issue`:`issues`]}),(0,Y.jsx)(`ul`,{className:`list-disc list-inside space-y-0.5`,children:T.map((e,t)=>(0,Y.jsx)(`li`,{children:e},t))})]}),(0,Y.jsxs)(`section`,{className:`space-y-3`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Name`}),(0,Y.jsx)(A,{value:e.name,disabled:!0,className:`text-sm`})]}),(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Description`}),(0,Y.jsx)(A,{value:e.description??``,onChange:e=>x(t=>{t.description=e.target.value}),className:`text-sm`,placeholder:`What is this profile for?`})]})]}),(0,Y.jsxs)(`section`,{children:[(0,Y.jsx)(`h2`,{className:`text-xs font-medium text-muted-foreground uppercase tracking-wide mb-2`,children:`Orchestrator`}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-3 p-3 rounded-md border border-border`,children:[(0,Y.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-mono text-foreground truncate`,children:`/specrails:implement · /specrails:batch-implement`}),(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground mt-0.5`,children:`Top-level model for both commands. batch-implement delegates to implement per feature, so every rail it spawns inherits this profile's agent chain.`})]}),(0,Y.jsx)(he,{value:e.orchestrator.model,onChange:e=>x(t=>{t.orchestrator.model=e})})]})]}),(0,Y.jsxs)(`section`,{children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between mb-2`,children:[(0,Y.jsxs)(`h2`,{className:`text-xs font-medium text-muted-foreground uppercase tracking-wide`,children:[`Agent chain (`,e.agents.length,`)`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>p(e=>!e),disabled:C.length===0,title:C.length===0?`All catalog agents are already in the chain`:`Add an agent from the catalog`,children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1`}),` Add`]})]}),d&&(0,Y.jsxs)(`div`,{className:`mb-2 p-2 rounded-md border border-border bg-muted/30`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between mb-1.5 px-1`,children:[(0,Y.jsxs)(`span`,{className:`text-[11px] text-muted-foreground`,children:[`Pick from catalog (`,C.length,` available)`]}),(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,onClick:()=>p(!1),title:`Close`,children:(0,Y.jsx)(I,{className:`w-3 h-3`})})]}),C.length===0?(0,Y.jsx)(`div`,{className:`px-2 py-3 text-xs text-muted-foreground text-center`,children:`No more agents in the catalog. Add custom agents from the Agents Catalog tab.`}):(0,Y.jsx)(`div`,{className:`space-y-0.5 max-h-64 overflow-auto`,children:C.map(e=>(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>O(e.id),className:`w-full flex items-center justify-between gap-2 px-2 py-1.5 text-left rounded hover:bg-accent transition-colors`,children:[(0,Y.jsx)(`span`,{className:`text-sm font-mono`,children:e.id}),(0,Y.jsx)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded `+(e.kind===`custom`?`bg-purple-500/15 text-purple-400`:`bg-muted text-muted-foreground`),children:e.kind})]},e.id))})]}),(0,Y.jsx)(s,{sensors:M,collisionDetection:n,onDragEnd:N,children:(0,Y.jsx)(w,{items:e.agents.map(e=>e.id),strategy:l,children:(0,Y.jsx)(`div`,{className:`space-y-1.5`,children:e.agents.map((e,t)=>(0,Y.jsx)(pe,{agent:e,canRemove:!Z.has(e.id),onModel:e=>P(t,e),onRemove:()=>k(t)},e.id))})})})]}),(0,Y.jsxs)(`section`,{children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between mb-2`,children:[(0,Y.jsxs)(`h2`,{className:`text-xs font-medium text-muted-foreground uppercase tracking-wide`,children:[`Routing (`,e.routing.length,`)`]}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-1`,children:[!D&&(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:ee,disabled:e.agents.length===0,title:e.agents.length===0?`Add at least one agent before creating routing rules`:void 0,children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1`}),` Add default`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>h(!0),disabled:e.agents.length===0,title:e.agents.length===0?`Add at least one agent before creating routing rules`:void 0,children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1`}),` Add rule`]})]})]}),(0,Y.jsx)(`p`,{className:`text-[11px] text-muted-foreground mb-2`,children:`First matching rule wins. Rules are editable and removable, and the default catch-all stays last when present. If you leave routing empty, the pipeline falls back to the first developer-shaped agent in the chain.`}),E.length>0&&(0,Y.jsxs)(`div`,{className:`mb-2 px-3 py-2 text-xs rounded-md border border-yellow-500/30 bg-yellow-500/10 text-yellow-500`,children:[(0,Y.jsx)(`div`,{className:`font-medium mb-1`,children:`Untargeted agents in the chain`}),(0,Y.jsxs)(`div`,{children:[`No routing rule points to: `,(0,Y.jsx)(`span`,{className:`font-mono`,children:E.join(`, `)}),`. Add a tag rule or retarget the default rule if you want them to run.`]})]}),(0,Y.jsx)(`div`,{className:`space-y-1.5`,children:e.routing.map((t,n)=>{let r=`default`in t&&t.default===!0;return(0,Y.jsx)(me,{rule:t,ordinal:n+1,isLast:n===e.routing.length-1,canMove:!r,canRemove:!r,canEdit:!r,chainAgents:e.agents.map(e=>e.id),onAgentChange:e=>B(n,e),onEdit:()=>v(n),onUp:()=>ne(n,-1),onDown:()=>ne(n,1),onRemove:()=>V(n)},n)})})]}),r&&(0,Y.jsx)(`div`,{className:`pt-3 border-t border-border`,children:r})]})}function pe({agent:e,canRemove:t,onModel:n,onRemove:r}){let i=Z.has(e.id),a=e.id===`sr-architect`,o=e.id===`sr-merge-resolver`,{attributes:s,listeners:c,setNodeRef:l,transform:u,transition:d,isDragging:f}=E({id:e.id});return(0,Y.jsxs)(`div`,{ref:l,style:{transform:v.Transform.toString(u),transition:d,opacity:f?.5:void 0,zIndex:f?10:void 0},className:`flex items-center gap-2 px-2 py-1.5 rounded-md border border-border group hover:bg-accent/30 transition-colors bg-background`,children:[(0,Y.jsx)(`button`,{type:`button`,className:`flex-shrink-0 p-0.5 rounded text-muted-foreground cursor-grab active:cursor-grabbing hover:text-foreground`,title:`Drag to reorder`,"aria-label":`Drag handle`,...s,...c,children:(0,Y.jsx)(V,{className:`w-3.5 h-3.5`})}),(0,Y.jsx)(`span`,{className:`text-sm font-mono flex-1 truncate`,children:e.id}),a&&(0,Y.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-[10px] px-1.5 py-0.5 rounded bg-accent-primary/15 text-accent-primary`,title:`Pinned to first position — pipeline always starts with sr-architect`,children:[(0,Y.jsx)(se,{className:`w-2.5 h-2.5 rotate-[135deg]`}),` first`]}),o&&(0,Y.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-[10px] px-1.5 py-0.5 rounded bg-accent-primary/15 text-accent-primary`,title:`Pinned to last position — merge phase always runs last`,children:[(0,Y.jsx)(se,{className:`w-2.5 h-2.5 rotate-45`}),` last`]}),i&&!a&&!o&&(0,Y.jsx)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground`,children:`required`}),(0,Y.jsx)(he,{value:e.model??`sonnet`,onChange:n}),(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-red-500/20 text-red-400 rounded disabled:opacity-30 disabled:cursor-not-allowed opacity-0 group-hover:opacity-100 transition-opacity`,onClick:r,disabled:!t,title:t?`Remove`:`Required baseline agent — the pipeline depends on this row`,children:(0,Y.jsx)(I,{className:`w-3 h-3`})})]})}function me({rule:e,ordinal:t,isLast:n,canMove:r,canRemove:i,canEdit:a,chainAgents:o,onAgentChange:s,onEdit:c,onUp:l,onDown:u,onRemove:d}){let f=`default`in e&&e.default===!0;return(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 px-2 py-1.5 rounded-md border border-border group hover:bg-accent/30 transition-colors`,children:[(0,Y.jsxs)(`span`,{className:`text-[10px] font-mono text-muted-foreground w-5 text-center flex-shrink-0`,children:[t,`.`]}),f?(0,Y.jsx)(`span`,{className:`text-xs text-muted-foreground flex-1`,children:`everything else`}):(0,Y.jsx)(`span`,{className:`text-xs flex-1 flex gap-1 flex-wrap items-center`,children:e.tags.map(e=>(0,Y.jsx)(`span`,{className:`px-1.5 py-0.5 rounded bg-muted font-mono text-[11px]`,children:e},e))}),(0,Y.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:`→`}),f?(0,Y.jsx)(`span`,{className:`h-7 max-w-[15rem] px-2 text-xs font-mono rounded border border-border bg-muted/40 text-muted-foreground inline-flex items-center`,"aria-label":`Default routing target (core, read-only)`,title:`Core fallback — not editable`,children:e.agent}):(0,Y.jsx)(`select`,{value:e.agent,onChange:e=>s(e.target.value),"aria-label":`Routing target for rule ${t}`,className:`h-7 max-w-[15rem] px-2 text-xs font-mono rounded border border-border bg-background`,children:o.map(e=>(0,Y.jsx)(`option`,{value:e,children:e},e))}),(0,Y.jsxs)(`div`,{className:`flex gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity`,children:[a&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,onClick:c,title:`Edit rule`,"aria-label":`Edit rule ${t}`,children:(0,Y.jsx)(D,{className:`w-3 h-3`})}),r&&!n&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,onClick:u,title:`Move down`,children:(0,Y.jsx)(z,{className:`w-3 h-3`})}),r&&t>1&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,onClick:l,title:`Move up`,children:(0,Y.jsx)(P,{className:`w-3 h-3`})}),i&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-red-500/20 text-red-400 rounded`,onClick:d,title:`Remove`,children:(0,Y.jsx)(I,{className:`w-3 h-3`})})]}),f&&(0,Y.jsx)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground`,title:`Core fallback — the pipeline's last-resort rule, pinned to sr-developer`,children:`core · default`})]})}function he({value:e,onChange:t}){return(0,Y.jsx)(`select`,{value:e,onChange:e=>t(e.target.value),className:`h-7 px-2 text-xs rounded border border-border bg-background`,children:ue.map(e=>(0,Y.jsx)(`option`,{value:e,children:e},e))})}function ge({open:e,title:t,description:n,placeholder:r,initialValue:i=``,confirmLabel:o=`Confirm`,inputPattern:s,inputInvalidHint:c,onConfirm:l,onCancel:u}){let[d,f]=(0,J.useState)(i);(0,J.useEffect)(()=>{e&&f(i)},[e,i]);let p=d.trim(),m=!s||s.test(p),h=p.length>0&&m;return(0,Y.jsx)(G,{open:e,onOpenChange:e=>{e||u()},children:(0,Y.jsxs)(j,{className:`max-w-md`,children:[(0,Y.jsx)(a,{children:(0,Y.jsx)(H,{children:t})}),(0,Y.jsxs)(`div`,{className:`py-2 space-y-2`,children:[n&&(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:n}),(0,Y.jsx)(A,{autoFocus:!0,value:d,placeholder:r,onChange:e=>f(e.target.value),onKeyDown:e=>{e.key===`Enter`&&h&&l(p)},className:`text-sm font-mono`}),p.length>0&&!m&&c&&(0,Y.jsx)(`p`,{className:`text-[11px] text-yellow-500`,children:c})]}),(0,Y.jsxs)(C,{children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:u,children:`Cancel`}),(0,Y.jsx)(y,{size:`sm`,onClick:()=>l(p),disabled:!h,children:o})]})]})})}function _e({open:e,title:t,description:n,confirmLabel:r=`Confirm`,destructive:i=!1,onConfirm:o,onCancel:s}){return(0,Y.jsx)(G,{open:e,onOpenChange:e=>{e||s()},children:(0,Y.jsxs)(j,{className:`max-w-md`,children:[(0,Y.jsx)(a,{children:(0,Y.jsx)(H,{children:t})}),n&&(0,Y.jsx)(`div`,{className:`py-2`,children:(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:n})}),(0,Y.jsxs)(C,{children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:s,children:`Cancel`}),(0,Y.jsx)(y,{size:`sm`,onClick:o,className:i?`bg-red-500 hover:bg-red-600 text-white`:void 0,children:r})]})]})})}function ve(){let[e,t]=(0,J.useState)([]),[n,r]=(0,J.useState)(null),[i,a]=(0,J.useState)(null),[o,s]=(0,J.useState)(!0),[c,l]=(0,J.useState)(null),[u,d]=(0,J.useState)(!1),[p,h]=(0,J.useState)([]),[g,v]=(0,J.useState)([]),[b,x]=(0,J.useState)(!1),[C,w]=(0,J.useState)(null),[T,E]=(0,J.useState)(null),D=(0,J.useCallback)(async()=>{s(!0),l(null);try{let e=await fetch(`${W()}/profiles`);if(!e.ok)throw Error(`List failed: ${e.status}`);let i=await e.json();t(i.profiles),i.profiles.length>0&&!n&&r(i.profiles[0].name)}catch(e){l(e.message)}finally{s(!1)}},[n]);(0,J.useEffect)(()=>{D()},[D]),(0,J.useEffect)(()=>{if(!n){a(null);return}let e=!1;return fetch(`${W()}/profiles/${encodeURIComponent(n)}`).then(e=>{if(!e.ok)throw Error(`Load failed: ${e.status}`);return e.json()}).then(t=>{e||a(t.profile)}).catch(t=>{e||l(t.message)}),()=>{e=!0}},[n]);let O=(0,J.useCallback)(async()=>{d(!0),l(null);try{let e=await fetch(`${W()}/profiles/migrate-from-settings`,{method:`POST`});if(!e.ok){let t=await e.json().catch(()=>({}));throw Error(t.error??`Migration failed: ${e.status}`)}await D(),r(`default`),S.success(`Profile migrated`,{description:`default profile created from your current agents`})}catch(e){let t=e.message;l(t),S.error(`Migration failed`,{description:t})}finally{d(!1)}},[D]),k=(0,J.useCallback)(async e=>{x(!1),d(!0),l(null);try{let t={schemaVersion:1,name:e,description:``,orchestrator:{model:`sonnet`},agents:[{id:`sr-architect`,model:`sonnet`,required:!0},{id:`sr-developer`,model:`sonnet`,required:!0},{id:`sr-reviewer`,model:`sonnet`,required:!0},{id:`sr-merge-resolver`,model:`sonnet`,required:!0}],routing:[{default:!0,agent:`sr-developer`}]},n=await fetch(`${W()}/profiles`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t)});if(!n.ok){let e=await n.json().catch(()=>({}));throw Error(e.error??`Create failed: ${n.status}`)}await D(),r(e),S.success(`Profile created`,{description:e})}catch(e){let t=e.message;l(t),S.error(`Failed to create profile`,{description:t})}finally{d(!1)}},[D]),A=(0,J.useCallback)(async(e,t)=>{w(null),d(!0),l(null);try{let n=await fetch(`${W()}/profiles/${encodeURIComponent(e)}/duplicate`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({name:t})});if(!n.ok){let e=await n.json().catch(()=>({}));throw Error(e.error??`Duplicate failed: ${n.status}`)}await D(),r(t),S.success(`Profile duplicated`,{description:`${e} → ${t}`})}catch(e){let t=e.message;l(t),S.error(`Failed to duplicate profile`,{description:t})}finally{d(!1)}},[D]),j=(0,J.useCallback)(async e=>{E(null),d(!0),l(null);try{let t=await fetch(`${W()}/profiles/${encodeURIComponent(e)}`,{method:`DELETE`});if(!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error??`Delete failed: ${t.status}`)}r(t=>t===e?null:t),await D(),S.success(`Profile deleted`,{description:e})}catch(e){let t=e.message;l(t),S.error(`Failed to delete profile`,{description:t})}finally{d(!1)}},[D]),M=(0,J.useCallback)(()=>x(!0),[]),N=(0,J.useCallback)(e=>w({from:e}),[]),P=(0,J.useCallback)(e=>E({name:e}),[]),F=(0,J.useCallback)(async(e,t)=>{d(!0),l(null);try{let n=await fetch(`${W()}/profiles/${encodeURIComponent(e.name)}`,{method:`PATCH`,headers:{"Content-Type":`application/json`},body:JSON.stringify(e)});if(!n.ok){let e=await n.json().catch(()=>({}));throw Error(e.error??`Save failed: ${n.status}`)}a(e),t.length>0?S.warning(`Profile saved with untargeted agents`,{description:`No routing rule points to: ${t.join(`, `)}. Add a tag rule or retarget the default rule if you want them to run.`,duration:6e3}):S.success(`Profile saved`,{description:e.name})}catch(e){let t=e.message;l(t),S.error(`Failed to save profile`,{description:t})}finally{d(!1)}},[]),I=(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(ge,{open:b,title:`New profile`,description:`Pick a lowercase kebab-case name (letters, digits, and hyphens).`,placeholder:`my-profile`,confirmLabel:`Create`,inputPattern:/^[a-z0-9][a-z0-9-]*$/,inputInvalidHint:`Must start with a letter or digit and contain only lowercase letters, digits, and hyphens.`,onConfirm:e=>void k(e),onCancel:()=>x(!1)}),C&&(0,Y.jsx)(ge,{open:!0,title:`Duplicate "${C.from}"`,description:`Name for the new profile.`,placeholder:`${C.from}-copy`,initialValue:`${C.from}-copy`,confirmLabel:`Duplicate`,inputPattern:/^[a-z0-9][a-z0-9-]*$/,inputInvalidHint:`Lowercase kebab-case only.`,onConfirm:e=>void A(C.from,e),onCancel:()=>w(null)}),T&&(0,Y.jsx)(_e,{open:!0,title:`Delete profile "${T.name}"?`,description:`This cannot be undone. Jobs already launched with this profile keep their snapshot; future launches will fall back to the resolution order.`,confirmLabel:`Delete`,destructive:!0,onConfirm:()=>void j(T.name),onCancel:()=>E(null)})]});return o?(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Loading profiles…`})}):e.length===0?(0,Y.jsxs)(Y.Fragment,{children:[I,(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsxs)(`div`,{className:`text-center max-w-md`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-medium text-foreground`,children:`No profiles yet`}),(0,Y.jsx)(`div`,{className:`text-xs text-muted-foreground mt-1 mb-4`,children:`Profiles let you save orchestrator + agent + routing combinations and pick one per rail.`}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 justify-center`,children:[(0,Y.jsxs)(y,{size:`sm`,onClick:O,disabled:u,children:[(0,Y.jsx)(ce,{className:`w-3.5 h-3.5 mr-1.5`}),` Migrate from current agents`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:M,disabled:u,children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1.5`}),` Blank profile`]})]}),(0,Y.jsxs)(`div`,{className:`text-[11px] text-muted-foreground/70 mt-3`,children:[`"Migrate" reads your existing `,(0,Y.jsx)(`code`,{className:`text-foreground`,children:`.claude/agents/`}),` `,`frontmatter models and creates a `,(0,Y.jsx)(`code`,{className:`text-foreground`,children:`default`}),` profile mirroring today's behavior — zero-loss.`]}),c&&(0,Y.jsx)(`div`,{className:`mt-3 text-xs text-red-400`,children:c})]})})]}):(0,Y.jsxs)(`div`,{className:`flex flex-col h-full`,children:[I,(0,Y.jsxs)(`div`,{className:`flex flex-1 min-h-0`,children:[(0,Y.jsxs)(`aside`,{className:`w-64 flex-shrink-0 border-r border-border flex flex-col`,children:[(0,Y.jsxs)(`div`,{className:`p-3 flex items-center justify-between`,children:[(0,Y.jsx)(`div`,{className:`text-xs font-medium text-muted-foreground uppercase tracking-wide`,children:`Profiles`}),(0,Y.jsx)(y,{size:`sm`,variant:`ghost`,onClick:M,disabled:u,children:(0,Y.jsx)(f,{className:`w-3.5 h-3.5`})})]}),(0,Y.jsx)(`div`,{className:`flex-1 overflow-auto px-2 pb-3`,children:e.map(e=>(0,Y.jsxs)(`div`,{className:`group mb-1 rounded-md px-2 py-1.5 text-xs cursor-pointer transition-colors flex items-center justify-between `+(e.name===n?`bg-accent text-foreground`:`text-muted-foreground hover:bg-accent/50 hover:text-foreground`),onClick:()=>r(e.name),role:`button`,tabIndex:0,onKeyDown:t=>{(t.key===`Enter`||t.key===` `)&&r(e.name)},children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-1.5 min-w-0`,children:[(0,Y.jsx)(`span`,{className:`truncate font-medium`,children:e.name}),e.isDefault&&(0,Y.jsx)(`span`,{className:`text-[10px] text-muted-foreground`,children:`(team default)`})]}),(0,Y.jsxs)(`div`,{className:`flex gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity`,children:[(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,title:`Duplicate`,onClick:t=>{t.stopPropagation(),N(e.name)},children:(0,Y.jsx)(ne,{className:`w-3 h-3`})}),!e.isDefault&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-red-500/20 text-red-400 rounded`,title:`Delete`,onClick:t=>{t.stopPropagation(),P(e.name)},children:(0,Y.jsx)(_,{className:`w-3 h-3`})})]})]},e.name))})]}),(0,Y.jsxs)(`main`,{className:`flex-1 overflow-auto`,children:[c&&(0,Y.jsx)(`div`,{className:`mx-4 mt-4 px-3 py-2 text-xs rounded border border-red-500/30 bg-red-500/10 text-red-400`,children:c}),i?(0,Y.jsx)(fe,{profile:i,onChange:a,onValidityChange:h,onSoftWarningsChange:e=>v(e.agentsMissingRouting),footer:(0,Y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Y.jsxs)(y,{size:`sm`,onClick:()=>void F(i,g),disabled:u||p.length>0,title:p.length>0?`Fix validation issues before saving`:void 0,children:[(0,Y.jsx)(m,{className:`w-3.5 h-3.5 mr-1.5`}),`Save`]}),u&&(0,Y.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:`Saving…`}),p.length>0&&(0,Y.jsxs)(`span`,{className:`text-xs text-yellow-500`,children:[p.length,` `,p.length===1?`issue`:`issues`,` to resolve`]})]})},i.name):(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Select a profile to edit`})})]})]})]})}function Q(e){let t=e.workflow.map((e,t)=>`${t+1}. ${e}`).join(`
1
+ import{r as e}from"./chunk-CilyBKbf.js";import{$ as t,At as n,B as r,C as i,Ct as a,Dt as o,E as s,Et as c,F as l,Ft as u,G as d,H as f,I as p,L as m,M as h,Mt as g,N as _,Nt as v,O as y,Ot as b,P as x,Pt as S,S as C,St as w,T,Tt as E,U as D,Vt as O,W as k,X as A,_t as j,a as M,b as N,bt as P,ct as F,ft as I,ht as L,j as R,jt as z,kt as ee,lt as B,mt as V,ot as te,q as H,tt as ne,ut as U,w as W,wt as G,x as re,xt as ie,y as K,yt as ae}from"./index-CdTQ6ble.js";var oe=j(`flask-conical`,[[`path`,{d:`M14 2v6a2 2 0 0 0 .245.96l5.51 10.08A2 2 0 0 1 18 22H6a2 2 0 0 1-1.755-2.96l5.51-10.08A2 2 0 0 0 10 8V2`,key:`18mbvz`}],[`path`,{d:`M6.453 15h11.094`,key:`3shlmq`}],[`path`,{d:`M8.5 2h7`,key:`csnxdl`}]]),q=j(`history`,[[`path`,{d:`M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8`,key:`1357e3`}],[`path`,{d:`M3 3v5h5`,key:`1xhq8a`}],[`path`,{d:`M12 7v5l4 2`,key:`1fdv2h`}]]),se=j(`pin`,[[`path`,{d:`M12 17v5`,key:`bb1du9`}],[`path`,{d:`M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z`,key:`1nkz8b`}]]),ce=j(`wand-sparkles`,[[`path`,{d:`m21.64 3.64-1.28-1.28a1.21 1.21 0 0 0-1.72 0L2.36 18.64a1.21 1.21 0 0 0 0 1.72l1.28 1.28a1.2 1.2 0 0 0 1.72 0L21.64 5.36a1.2 1.2 0 0 0 0-1.72`,key:`ul74o6`}],[`path`,{d:`m14 7 3 3`,key:`1r5n42`}],[`path`,{d:`M5 6v4`,key:`ilb8ba`}],[`path`,{d:`M19 14v4`,key:`blhpug`}],[`path`,{d:`M10 2v2`,key:`7u0qdc`}],[`path`,{d:`M7 8H3`,key:`zfb6yr`}],[`path`,{d:`M21 16h-4`,key:`1cnmox`}],[`path`,{d:`M11 3H9`,key:`1obp7u`}]]),J=e(O(),1),Y=ie(),X=/^[a-z0-9][a-z0-9-]*$/;function le({open:e,mode:t=`add`,initial:n,chainAgents:r,onConfirm:a,onCancel:o}){let[s,c]=(0,J.useState)(``),[l,u]=(0,J.useState)(r[0]??``);(0,J.useEffect)(()=>{e&&(t===`edit`&&n?(c(n.tags.join(`, `)),u(r.includes(n.agent)?n.agent:r[0]??``)):(c(``),u(r[0]??``)))},[e,t,n,r]);let d=s.split(`,`).map(e=>e.trim()).filter(Boolean),f=d.filter(e=>!X.test(e)),p=d.length>0&&f.length===0&&r.includes(l);return(0,Y.jsx)(K,{open:e,onOpenChange:e=>{e||o()},children:(0,Y.jsxs)(N,{className:`max-w-md`,children:[(0,Y.jsxs)(i,{children:[(0,Y.jsx)(W,{children:t===`edit`?`Edit routing rule`:`Add routing rule`}),(0,Y.jsx)(re,{children:t===`edit`?`Update the tags or target agent for this rule.`:`Add a tag-matched routing rule using lowercase kebab-case tags.`})]}),(0,Y.jsxs)(`div`,{className:`py-2 space-y-3`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Tags`}),(0,Y.jsx)(M,{autoFocus:!0,value:s,onChange:e=>c(e.target.value),placeholder:`frontend, ui`,"aria-label":`Tags`,className:`text-sm font-mono`}),(0,Y.jsxs)(`p`,{className:`text-[11px] text-muted-foreground mt-1`,children:[`Comma-separated. Use lowercase kebab-case like `,(0,Y.jsx)(`code`,{children:`frontend`}),` or `,(0,Y.jsx)(`code`,{children:`api-design`}),`.`]}),f.length>0&&(0,Y.jsxs)(`p`,{className:`text-[11px] text-red-400 mt-1`,children:[`Invalid tag`,f.length===1?``:`s`,`: `,f.join(`, `),`. Tags must use lowercase letters, digits, and hyphens only.`]})]}),(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Route to`}),(0,Y.jsx)(`select`,{value:l,onChange:e=>u(e.target.value),"aria-label":`Route to`,className:`w-full h-9 px-2 text-sm rounded-md border border-border bg-background`,children:r.map(e=>(0,Y.jsx)(`option`,{value:e,children:e},e))}),(0,Y.jsx)(`p`,{className:`text-[11px] text-muted-foreground mt-1`,children:`Only agents in this profile's chain can be routing targets.`})]})]}),(0,Y.jsxs)(C,{children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:o,children:`Cancel`}),(0,Y.jsx)(y,{size:`sm`,onClick:()=>a(d,l),disabled:!p,children:t===`edit`?`Save changes`:`Add rule`})]})]})})}var Z=new Set([`sr-architect`,`sr-developer`,`sr-reviewer`,`sr-merge-resolver`]),ue=[`sonnet`,`opus`,`haiku`],de=/^[a-z0-9][a-z0-9-]*$/;function fe({profile:e,onChange:t,footer:r,onValidityChange:i,onSoftWarningsChange:s}){let[c,l]=(0,J.useState)([]),[u,d]=(0,J.useState)(!1),[p,m]=(0,J.useState)(!1),[h,_]=(0,J.useState)(null);(0,J.useEffect)(()=>{let e=!1;return fetch(`${w()}/profiles/catalog`).then(e=>e.ok?e.json():{agents:[]}).then(t=>{e||l(t.agents)}).catch(()=>{e||l([])}),()=>{e=!0}},[]);let x=n=>{let r=JSON.parse(JSON.stringify(e));n(r),t(r)},S=new Set(e.agents.map(e=>e.id)),C=c.filter(e=>!S.has(e.id)),T=(0,J.useMemo)(()=>{let t=[];for(let n of Z)e.agents.some(e=>e.id===n)||t.push(`Missing required baseline agent: ${n}`);let n=e.routing.filter(e=>`default`in e&&e.default===!0);if(n.length>1&&t.push(`Routing may have at most one default rule (found ${n.length})`),n.length===1){let n=e.routing[e.routing.length-1];`default`in n&&n.default===!0||t.push(`The default routing rule must be the last entry`)}for(let n of e.routing)if(e.agents.some(e=>e.id===n.agent)||t.push(`Routing references agent not in the chain: ${n.agent}`),`tags`in n){let e=n.tags.filter(e=>!de.test(e));e.length>0&&t.push(`Routing rule ${n.agent} has invalid tags: ${e.join(`, `)} (use lowercase kebab-case)`)}return t},[e]),D=(0,J.useMemo)(()=>{if(e.routing.length===0)return[];let t=new Set(e.routing.map(e=>e.agent));return e.agents.filter(e=>!Z.has(e.id)&&!t.has(e.id)).map(e=>e.id)},[e]),O=(0,J.useMemo)(()=>e.routing.some(e=>`default`in e&&e.default===!0),[e.routing]);(0,J.useEffect)(()=>{i&&i(T)},[T,i]),(0,J.useEffect)(()=>{s&&s({agentsMissingRouting:D})},[D,s]);let k=e=>{x(t=>{let n={id:e,model:`sonnet`},r=t.agents.findIndex(e=>e.id===`sr-merge-resolver`);r>=0?t.agents.splice(r,0,n):t.agents.push(n)}),d(!1)},A=t=>{let n=e.agents[t];Z.has(n.id)||x(e=>{e.agents.splice(t,1),e.routing=e.routing.filter(e=>e.agent!==n.id)})},j=(t,n)=>{if(t===n)return;let r=e.agents.findIndex(e=>e.id===t),i=e.agents.findIndex(e=>e.id===n);if(r<0||i<0)return;let a=G(e.agents,r,i),o=a.findIndex(e=>e.id===`sr-architect`);if(o>0){let[e]=a.splice(o,1);a.unshift(e)}let s=a.findIndex(e=>e.id===`sr-merge-resolver`);if(s>=0&&s!==a.length-1){let[e]=a.splice(s,1);a.push(e)}x(e=>{e.agents=a})},N=v(g(n,{activationConstraint:{distance:4}}),g(ee,{coordinateGetter:E})),P=e=>{let{active:t,over:n}=e;n&&j(String(t.id),String(n.id))},F=(e,t)=>{x(n=>{n.agents[e].model=t})},I=(e,t)=>{x(n=>{let r={tags:e,agent:t},i=n.routing.findIndex(e=>`default`in e&&e.default===!0);i>=0?n.routing.splice(i,0,r):n.routing.push(r)}),m(!1)},L=()=>{x(e=>{e.routing.some(e=>`default`in e&&e.default===!0)||e.agents.some(e=>e.id===`sr-developer`)&&e.routing.push({default:!0,agent:`sr-developer`})})},B=e=>`default`in e&&e.default===!0,V=(e,t)=>{x(n=>{let r=n.routing[e];r&&(B(r)||(r.agent=t))})},te=(e,t,n)=>{x(r=>{let i=r.routing[e];i&&(B(i)||(i.tags=t,i.agent=n))})},H=e=>{x(t=>{let n=t.routing[e];n&&(B(n)||t.routing.splice(e,1))})},ne=(t,n)=>{let r=t+n;if(r<0||r>=e.routing.length)return;let i=e.routing[t],a=e.routing[r];B(i)||B(a)||x(e=>{let[n]=e.routing.splice(t,1);e.routing.splice(r,0,n)})};return(0,Y.jsxs)(`div`,{className:`p-6 space-y-6 max-w-3xl`,children:[(0,Y.jsx)(le,{open:p,mode:`add`,chainAgents:e.agents.map(e=>e.id),onConfirm:I,onCancel:()=>m(!1)}),(0,Y.jsx)(le,{open:h!==null,mode:`edit`,initial:h!==null&&e.routing[h]&&!(`default`in e.routing[h])?{tags:e.routing[h].tags,agent:e.routing[h].agent}:void 0,chainAgents:e.agents.map(e=>e.id),onConfirm:(e,t)=>{h!==null&&(te(h,e,t),_(null))},onCancel:()=>_(null)}),T.length>0&&(0,Y.jsxs)(`div`,{className:`px-3 py-2 text-xs rounded-md border border-yellow-500/30 bg-yellow-500/10 text-yellow-500`,children:[(0,Y.jsxs)(`div`,{className:`font-medium mb-1`,children:[T.length,` validation `,T.length===1?`issue`:`issues`]}),(0,Y.jsx)(`ul`,{className:`list-disc list-inside space-y-0.5`,children:T.map((e,t)=>(0,Y.jsx)(`li`,{children:e},t))})]}),(0,Y.jsxs)(`section`,{className:`space-y-3`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Name`}),(0,Y.jsx)(M,{value:e.name,disabled:!0,className:`text-sm`})]}),(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Description`}),(0,Y.jsx)(M,{value:e.description??``,onChange:e=>x(t=>{t.description=e.target.value}),className:`text-sm`,placeholder:`What is this profile for?`})]})]}),(0,Y.jsxs)(`section`,{children:[(0,Y.jsx)(`h2`,{className:`text-xs font-medium text-muted-foreground uppercase tracking-wide mb-2`,children:`Orchestrator`}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-3 p-3 rounded-md border border-border`,children:[(0,Y.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-mono text-foreground truncate`,children:`/specrails:implement · /specrails:batch-implement`}),(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground mt-0.5`,children:`Top-level model for both commands. batch-implement delegates to implement per feature, so every rail it spawns inherits this profile's agent chain.`})]}),(0,Y.jsx)(he,{value:e.orchestrator.model,onChange:e=>x(t=>{t.orchestrator.model=e})})]})]}),(0,Y.jsxs)(`section`,{children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between mb-2`,children:[(0,Y.jsxs)(`h2`,{className:`text-xs font-medium text-muted-foreground uppercase tracking-wide`,children:[`Agent chain (`,e.agents.length,`)`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>d(e=>!e),disabled:C.length===0,title:C.length===0?`All catalog agents are already in the chain`:`Add an agent from the catalog`,children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1`}),` Add`]})]}),u&&(0,Y.jsxs)(`div`,{className:`mb-2 p-2 rounded-md border border-border bg-muted/30`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between mb-1.5 px-1`,children:[(0,Y.jsxs)(`span`,{className:`text-[11px] text-muted-foreground`,children:[`Pick from catalog (`,C.length,` available)`]}),(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,onClick:()=>d(!1),title:`Close`,children:(0,Y.jsx)(R,{className:`w-3 h-3`})})]}),C.length===0?(0,Y.jsx)(`div`,{className:`px-2 py-3 text-xs text-muted-foreground text-center`,children:`No more agents in the catalog. Add custom agents from the Agents Catalog tab.`}):(0,Y.jsx)(`div`,{className:`space-y-0.5 max-h-64 overflow-auto`,children:C.map(e=>(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>k(e.id),className:`w-full flex items-center justify-between gap-2 px-2 py-1.5 text-left rounded hover:bg-accent transition-colors`,children:[(0,Y.jsx)(`span`,{className:`text-sm font-mono`,children:e.id}),(0,Y.jsx)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded `+(e.kind===`custom`?`bg-purple-500/15 text-purple-400`:`bg-muted text-muted-foreground`),children:e.kind})]},e.id))})]}),(0,Y.jsx)(b,{sensors:N,collisionDetection:z,onDragEnd:P,children:(0,Y.jsx)(a,{items:e.agents.map(e=>e.id),strategy:o,children:(0,Y.jsx)(`div`,{className:`space-y-1.5`,children:e.agents.map((e,t)=>(0,Y.jsx)(pe,{agent:e,canRemove:!Z.has(e.id),onModel:e=>F(t,e),onRemove:()=>A(t)},e.id))})})})]}),(0,Y.jsxs)(`section`,{children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between mb-2`,children:[(0,Y.jsxs)(`h2`,{className:`text-xs font-medium text-muted-foreground uppercase tracking-wide`,children:[`Routing (`,e.routing.length,`)`]}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-1`,children:[!O&&(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:L,disabled:e.agents.length===0,title:e.agents.length===0?`Add at least one agent before creating routing rules`:void 0,children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1`}),` Add default`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>m(!0),disabled:e.agents.length===0,title:e.agents.length===0?`Add at least one agent before creating routing rules`:void 0,children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1`}),` Add rule`]})]})]}),(0,Y.jsx)(`p`,{className:`text-[11px] text-muted-foreground mb-2`,children:`First matching rule wins. Rules are editable and removable, and the default catch-all stays last when present. If you leave routing empty, the pipeline falls back to the first developer-shaped agent in the chain.`}),D.length>0&&(0,Y.jsxs)(`div`,{className:`mb-2 px-3 py-2 text-xs rounded-md border border-yellow-500/30 bg-yellow-500/10 text-yellow-500`,children:[(0,Y.jsx)(`div`,{className:`font-medium mb-1`,children:`Untargeted agents in the chain`}),(0,Y.jsxs)(`div`,{children:[`No routing rule points to: `,(0,Y.jsx)(`span`,{className:`font-mono`,children:D.join(`, `)}),`. Add a tag rule or retarget the default rule if you want them to run.`]})]}),(0,Y.jsx)(`div`,{className:`space-y-1.5`,children:e.routing.map((t,n)=>{let r=`default`in t&&t.default===!0;return(0,Y.jsx)(me,{rule:t,ordinal:n+1,isLast:n===e.routing.length-1,canMove:!r,canRemove:!r,canEdit:!r,chainAgents:e.agents.map(e=>e.id),onAgentChange:e=>V(n,e),onEdit:()=>_(n),onUp:()=>ne(n,-1),onDown:()=>ne(n,1),onRemove:()=>H(n)},n)})})]}),r&&(0,Y.jsx)(`div`,{className:`pt-3 border-t border-border`,children:r})]})}function pe({agent:e,canRemove:t,onModel:n,onRemove:r}){let i=Z.has(e.id),a=e.id===`sr-architect`,o=e.id===`sr-merge-resolver`,{attributes:s,listeners:l,setNodeRef:u,transform:d,transition:f,isDragging:p}=c({id:e.id});return(0,Y.jsxs)(`div`,{ref:u,style:{transform:S.Transform.toString(d),transition:f,opacity:p?.5:void 0,zIndex:p?10:void 0},className:`flex items-center gap-2 px-2 py-1.5 rounded-md border border-border group hover:bg-accent/30 transition-colors bg-background`,children:[(0,Y.jsx)(`button`,{type:`button`,className:`flex-shrink-0 p-0.5 rounded text-muted-foreground cursor-grab active:cursor-grabbing hover:text-foreground`,title:`Drag to reorder`,"aria-label":`Drag handle`,...s,...l,children:(0,Y.jsx)(H,{className:`w-3.5 h-3.5`})}),(0,Y.jsx)(`span`,{className:`text-sm font-mono flex-1 truncate`,children:e.id}),a&&(0,Y.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-[10px] px-1.5 py-0.5 rounded bg-accent-primary/15 text-accent-primary`,title:`Pinned to first position — pipeline always starts with sr-architect`,children:[(0,Y.jsx)(se,{className:`w-2.5 h-2.5 rotate-[135deg]`}),` first`]}),o&&(0,Y.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-[10px] px-1.5 py-0.5 rounded bg-accent-primary/15 text-accent-primary`,title:`Pinned to last position — merge phase always runs last`,children:[(0,Y.jsx)(se,{className:`w-2.5 h-2.5 rotate-45`}),` last`]}),i&&!a&&!o&&(0,Y.jsx)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground`,children:`required`}),(0,Y.jsx)(he,{value:e.model??`sonnet`,onChange:n}),(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-red-500/20 text-red-400 rounded disabled:opacity-30 disabled:cursor-not-allowed opacity-0 group-hover:opacity-100 transition-opacity`,onClick:r,disabled:!t,title:t?`Remove`:`Required baseline agent — the pipeline depends on this row`,children:(0,Y.jsx)(R,{className:`w-3 h-3`})})]})}function me({rule:e,ordinal:t,isLast:n,canMove:r,canRemove:i,canEdit:a,chainAgents:o,onAgentChange:s,onEdit:c,onUp:l,onDown:u,onRemove:d}){let f=`default`in e&&e.default===!0;return(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 px-2 py-1.5 rounded-md border border-border group hover:bg-accent/30 transition-colors`,children:[(0,Y.jsxs)(`span`,{className:`text-[10px] font-mono text-muted-foreground w-5 text-center flex-shrink-0`,children:[t,`.`]}),f?(0,Y.jsx)(`span`,{className:`text-xs text-muted-foreground flex-1`,children:`everything else`}):(0,Y.jsx)(`span`,{className:`text-xs flex-1 flex gap-1 flex-wrap items-center`,children:e.tags.map(e=>(0,Y.jsx)(`span`,{className:`px-1.5 py-0.5 rounded bg-muted font-mono text-[11px]`,children:e},e))}),(0,Y.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:`→`}),f?(0,Y.jsx)(`span`,{className:`h-7 max-w-[15rem] px-2 text-xs font-mono rounded border border-border bg-muted/40 text-muted-foreground inline-flex items-center`,"aria-label":`Default routing target (core, read-only)`,title:`Core fallback — not editable`,children:e.agent}):(0,Y.jsx)(`select`,{value:e.agent,onChange:e=>s(e.target.value),"aria-label":`Routing target for rule ${t}`,className:`h-7 max-w-[15rem] px-2 text-xs font-mono rounded border border-border bg-background`,children:o.map(e=>(0,Y.jsx)(`option`,{value:e,children:e},e))}),(0,Y.jsxs)(`div`,{className:`flex gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity`,children:[a&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,onClick:c,title:`Edit rule`,"aria-label":`Edit rule ${t}`,children:(0,Y.jsx)(D,{className:`w-3 h-3`})}),r&&!n&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,onClick:u,title:`Move down`,children:(0,Y.jsx)(L,{className:`w-3 h-3`})}),r&&t>1&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,onClick:l,title:`Move up`,children:(0,Y.jsx)(I,{className:`w-3 h-3`})}),i&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-red-500/20 text-red-400 rounded`,onClick:d,title:`Remove`,children:(0,Y.jsx)(R,{className:`w-3 h-3`})})]}),f&&(0,Y.jsx)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground`,title:`Core fallback — the pipeline's last-resort rule, pinned to sr-developer`,children:`core · default`})]})}function he({value:e,onChange:t}){return(0,Y.jsx)(`select`,{value:e,onChange:e=>t(e.target.value),className:`h-7 px-2 text-xs rounded border border-border bg-background`,children:ue.map(e=>(0,Y.jsx)(`option`,{value:e,children:e},e))})}function ge({open:e,title:t,description:n,placeholder:r,initialValue:a=``,confirmLabel:o=`Confirm`,inputPattern:s,inputInvalidHint:c,onConfirm:l,onCancel:u}){let[d,f]=(0,J.useState)(a);(0,J.useEffect)(()=>{e&&f(a)},[e,a]);let p=d.trim(),m=!s||s.test(p),h=p.length>0&&m;return(0,Y.jsx)(K,{open:e,onOpenChange:e=>{e||u()},children:(0,Y.jsxs)(N,{className:`max-w-md`,children:[(0,Y.jsx)(i,{children:(0,Y.jsx)(W,{children:t})}),(0,Y.jsxs)(`div`,{className:`py-2 space-y-2`,children:[n&&(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:n}),(0,Y.jsx)(M,{autoFocus:!0,value:d,placeholder:r,onChange:e=>f(e.target.value),onKeyDown:e=>{e.key===`Enter`&&h&&l(p)},className:`text-sm font-mono`}),p.length>0&&!m&&c&&(0,Y.jsx)(`p`,{className:`text-[11px] text-yellow-500`,children:c})]}),(0,Y.jsxs)(C,{children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:u,children:`Cancel`}),(0,Y.jsx)(y,{size:`sm`,onClick:()=>l(p),disabled:!h,children:o})]})]})})}function _e({open:e,title:t,description:n,confirmLabel:r=`Confirm`,destructive:a=!1,onConfirm:o,onCancel:s}){return(0,Y.jsx)(K,{open:e,onOpenChange:e=>{e||s()},children:(0,Y.jsxs)(N,{className:`max-w-md`,children:[(0,Y.jsx)(i,{children:(0,Y.jsx)(W,{children:t})}),n&&(0,Y.jsx)(`div`,{className:`py-2`,children:(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:n})}),(0,Y.jsxs)(C,{children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:s,children:`Cancel`}),(0,Y.jsx)(y,{size:`sm`,onClick:o,className:a?`bg-red-500 hover:bg-red-600 text-white`:void 0,children:r})]})]})})}function ve(){let[e,t]=(0,J.useState)([]),[n,r]=(0,J.useState)(null),[i,a]=(0,J.useState)(null),[o,s]=(0,J.useState)(!0),[c,l]=(0,J.useState)(null),[d,p]=(0,J.useState)(!1),[h,g]=(0,J.useState)([]),[v,b]=(0,J.useState)([]),[x,S]=(0,J.useState)(!1),[C,T]=(0,J.useState)(null),[E,D]=(0,J.useState)(null),O=(0,J.useCallback)(async()=>{s(!0),l(null);try{let e=await fetch(`${w()}/profiles`);if(!e.ok)throw Error(`List failed: ${e.status}`);let i=await e.json();t(i.profiles),i.profiles.length>0&&!n&&r(i.profiles[0].name)}catch(e){l(e.message)}finally{s(!1)}},[n]);(0,J.useEffect)(()=>{O()},[O]),(0,J.useEffect)(()=>{if(!n){a(null);return}let e=!1;return fetch(`${w()}/profiles/${encodeURIComponent(n)}`).then(e=>{if(!e.ok)throw Error(`Load failed: ${e.status}`);return e.json()}).then(t=>{e||a(t.profile)}).catch(t=>{e||l(t.message)}),()=>{e=!0}},[n]);let k=(0,J.useCallback)(async()=>{p(!0),l(null);try{let e=await fetch(`${w()}/profiles/migrate-from-settings`,{method:`POST`});if(!e.ok){let t=await e.json().catch(()=>({}));throw Error(t.error??`Migration failed: ${e.status}`)}await O(),r(`default`),u.success(`Profile migrated`,{description:`default profile created from your current agents`})}catch(e){let t=e.message;l(t),u.error(`Migration failed`,{description:t})}finally{p(!1)}},[O]),A=(0,J.useCallback)(async e=>{S(!1),p(!0),l(null);try{let t={schemaVersion:1,name:e,description:``,orchestrator:{model:`sonnet`},agents:[{id:`sr-architect`,model:`sonnet`,required:!0},{id:`sr-developer`,model:`sonnet`,required:!0},{id:`sr-reviewer`,model:`sonnet`,required:!0},{id:`sr-merge-resolver`,model:`sonnet`,required:!0}],routing:[{default:!0,agent:`sr-developer`}]},n=await fetch(`${w()}/profiles`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t)});if(!n.ok){let e=await n.json().catch(()=>({}));throw Error(e.error??`Create failed: ${n.status}`)}await O(),r(e),u.success(`Profile created`,{description:e})}catch(e){let t=e.message;l(t),u.error(`Failed to create profile`,{description:t})}finally{p(!1)}},[O]),j=(0,J.useCallback)(async(e,t)=>{T(null),p(!0),l(null);try{let n=await fetch(`${w()}/profiles/${encodeURIComponent(e)}/duplicate`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({name:t})});if(!n.ok){let e=await n.json().catch(()=>({}));throw Error(e.error??`Duplicate failed: ${n.status}`)}await O(),r(t),u.success(`Profile duplicated`,{description:`${e} → ${t}`})}catch(e){let t=e.message;l(t),u.error(`Failed to duplicate profile`,{description:t})}finally{p(!1)}},[O]),M=(0,J.useCallback)(async e=>{D(null),p(!0),l(null);try{let t=await fetch(`${w()}/profiles/${encodeURIComponent(e)}`,{method:`DELETE`});if(!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error??`Delete failed: ${t.status}`)}r(t=>t===e?null:t),await O(),u.success(`Profile deleted`,{description:e})}catch(e){let t=e.message;l(t),u.error(`Failed to delete profile`,{description:t})}finally{p(!1)}},[O]),N=(0,J.useCallback)(()=>S(!0),[]),P=(0,J.useCallback)(e=>T({from:e}),[]),F=(0,J.useCallback)(e=>D({name:e}),[]),I=(0,J.useCallback)(async(e,t)=>{p(!0),l(null);try{let n=await fetch(`${w()}/profiles/${encodeURIComponent(e.name)}`,{method:`PATCH`,headers:{"Content-Type":`application/json`},body:JSON.stringify(e)});if(!n.ok){let e=await n.json().catch(()=>({}));throw Error(e.error??`Save failed: ${n.status}`)}a(e),t.length>0?u.warning(`Profile saved with untargeted agents`,{description:`No routing rule points to: ${t.join(`, `)}. Add a tag rule or retarget the default rule if you want them to run.`,duration:6e3}):u.success(`Profile saved`,{description:e.name})}catch(e){let t=e.message;l(t),u.error(`Failed to save profile`,{description:t})}finally{p(!1)}},[]),L=(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(ge,{open:x,title:`New profile`,description:`Pick a lowercase kebab-case name (letters, digits, and hyphens).`,placeholder:`my-profile`,confirmLabel:`Create`,inputPattern:/^[a-z0-9][a-z0-9-]*$/,inputInvalidHint:`Must start with a letter or digit and contain only lowercase letters, digits, and hyphens.`,onConfirm:e=>void A(e),onCancel:()=>S(!1)}),C&&(0,Y.jsx)(ge,{open:!0,title:`Duplicate "${C.from}"`,description:`Name for the new profile.`,placeholder:`${C.from}-copy`,initialValue:`${C.from}-copy`,confirmLabel:`Duplicate`,inputPattern:/^[a-z0-9][a-z0-9-]*$/,inputInvalidHint:`Lowercase kebab-case only.`,onConfirm:e=>void j(C.from,e),onCancel:()=>T(null)}),E&&(0,Y.jsx)(_e,{open:!0,title:`Delete profile "${E.name}"?`,description:`This cannot be undone. Jobs already launched with this profile keep their snapshot; future launches will fall back to the resolution order.`,confirmLabel:`Delete`,destructive:!0,onConfirm:()=>void M(E.name),onCancel:()=>D(null)})]});return o?(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Loading profiles…`})}):e.length===0?(0,Y.jsxs)(Y.Fragment,{children:[L,(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsxs)(`div`,{className:`text-center max-w-md`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-medium text-foreground`,children:`No profiles yet`}),(0,Y.jsx)(`div`,{className:`text-xs text-muted-foreground mt-1 mb-4`,children:`Profiles let you save orchestrator + agent + routing combinations and pick one per rail.`}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 justify-center`,children:[(0,Y.jsxs)(y,{size:`sm`,onClick:k,disabled:d,children:[(0,Y.jsx)(ce,{className:`w-3.5 h-3.5 mr-1.5`}),` Migrate from current agents`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:N,disabled:d,children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1.5`}),` Blank profile`]})]}),(0,Y.jsxs)(`div`,{className:`text-[11px] text-muted-foreground/70 mt-3`,children:[`"Migrate" reads your existing `,(0,Y.jsx)(`code`,{className:`text-foreground`,children:`.claude/agents/`}),` `,`frontmatter models and creates a `,(0,Y.jsx)(`code`,{className:`text-foreground`,children:`default`}),` profile mirroring today's behavior — zero-loss.`]}),c&&(0,Y.jsx)(`div`,{className:`mt-3 text-xs text-red-400`,children:c})]})})]}):(0,Y.jsxs)(`div`,{className:`flex flex-col h-full`,children:[L,(0,Y.jsxs)(`div`,{className:`flex flex-1 min-h-0`,children:[(0,Y.jsxs)(`aside`,{className:`w-64 flex-shrink-0 border-r border-border flex flex-col`,children:[(0,Y.jsxs)(`div`,{className:`p-3 flex items-center justify-between`,children:[(0,Y.jsx)(`div`,{className:`text-xs font-medium text-muted-foreground uppercase tracking-wide`,children:`Profiles`}),(0,Y.jsx)(y,{size:`sm`,variant:`ghost`,onClick:N,disabled:d,children:(0,Y.jsx)(f,{className:`w-3.5 h-3.5`})})]}),(0,Y.jsx)(`div`,{className:`flex-1 overflow-auto px-2 pb-3`,children:e.map(e=>(0,Y.jsxs)(`div`,{className:`group mb-1 rounded-md px-2 py-1.5 text-xs cursor-pointer transition-colors flex items-center justify-between `+(e.name===n?`bg-accent text-foreground`:`text-muted-foreground hover:bg-accent/50 hover:text-foreground`),onClick:()=>r(e.name),role:`button`,tabIndex:0,onKeyDown:t=>{(t.key===`Enter`||t.key===` `)&&r(e.name)},children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-1.5 min-w-0`,children:[(0,Y.jsx)(`span`,{className:`truncate font-medium`,children:e.name}),e.isDefault&&(0,Y.jsx)(`span`,{className:`text-[10px] text-muted-foreground`,children:`(team default)`})]}),(0,Y.jsxs)(`div`,{className:`flex gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity`,children:[(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-accent rounded`,title:`Duplicate`,onClick:t=>{t.stopPropagation(),P(e.name)},children:(0,Y.jsx)(ne,{className:`w-3 h-3`})}),!e.isDefault&&(0,Y.jsx)(`button`,{type:`button`,className:`p-1 hover:bg-red-500/20 text-red-400 rounded`,title:`Delete`,onClick:t=>{t.stopPropagation(),F(e.name)},children:(0,Y.jsx)(_,{className:`w-3 h-3`})})]})]},e.name))})]}),(0,Y.jsxs)(`main`,{className:`flex-1 overflow-auto`,children:[c&&(0,Y.jsx)(`div`,{className:`mx-4 mt-4 px-3 py-2 text-xs rounded border border-red-500/30 bg-red-500/10 text-red-400`,children:c}),i?(0,Y.jsx)(fe,{profile:i,onChange:a,onValidityChange:g,onSoftWarningsChange:e=>b(e.agentsMissingRouting),footer:(0,Y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Y.jsxs)(y,{size:`sm`,onClick:()=>void I(i,v),disabled:d||h.length>0,title:h.length>0?`Fix validation issues before saving`:void 0,children:[(0,Y.jsx)(m,{className:`w-3.5 h-3.5 mr-1.5`}),`Save`]}),d&&(0,Y.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:`Saving…`}),h.length>0&&(0,Y.jsxs)(`span`,{className:`text-xs text-yellow-500`,children:[h.length,` `,h.length===1?`issue`:`issues`,` to resolve`]})]})},i.name):(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Select a profile to edit`})})]})]})]})}function Q(e){let t=e.workflow.map((e,t)=>`${t+1}. ${e}`).join(`
2
2
  `);return`---
3
3
  name: ${e.id}
4
4
  description: "${e.descriptionOneLine}"
@@ -76,11 +76,11 @@ You are ...
76
76
  - **risk_tolerance**: conservative
77
77
  - **detail_level**: full
78
78
  - **focus_areas**: ...
79
- `;function Se({agentId:e,initialBody:t,initialName:n,draftFromRefine:r,onClose:i,onSaved:a,onResumeRefine:o}){let s=!e,[c,l]=(0,J.useState)(e??n??``),[u,f]=(0,J.useState)(t??$),[p,h]=(0,J.useState)(!s&&!t&&!r),[g,v]=(0,J.useState)(!!r&&!s),[b,x]=(0,J.useState)(!1),[C,w]=(0,J.useState)(null),[T,E]=(0,J.useState)([]),[D,O]=(0,J.useState)(!1),[k,j]=(0,J.useState)(!!t&&s),[M,N]=(0,J.useState)(!1),[P,F]=(0,J.useState)(``),[I,L]=(0,J.useState)(!1),[R,ee]=(0,J.useState)(null),[z,V]=(0,J.useState)(null),[ne,re]=(0,J.useState)(!1);(0,J.useEffect)(()=>{if(s||t!==void 0)return;if(r){let t=!1;return fetch(`${W()}/profiles/catalog/${encodeURIComponent(e)}/refine/${r}`).then(e=>{if(!e.ok)throw Error(`Load draft failed: ${e.status}`);return e.json()}).then(e=>{!t&&e.draftBody&&(f(e.draftBody),j(!0))}).catch(e=>{t||w(e.message)}).finally(()=>{t||v(!1)}),()=>{t=!0}}let n=!1;return fetch(`${W()}/profiles/catalog/${encodeURIComponent(e)}`).then(e=>{if(!e.ok)throw Error(`Load failed: ${e.status}`);return e.json()}).then(e=>{n||f(e.body)}).catch(e=>{n||w(e.message)}).finally(()=>{n||h(!1)}),()=>{n=!0}},[e,s,t,r]);let ie=(0,J.useCallback)(()=>{s||fetch(`${W()}/profiles/catalog/${encodeURIComponent(e)}/versions`).then(e=>e.ok?e.json():{versions:[]}).then(e=>E(e.versions)).catch(()=>E([]))},[e,s]);(0,J.useEffect)(()=>{D&&ie()},[D,ie]);let H=/^custom-[a-z0-9][a-z0-9-]*$/.test(c),U=u.trim().length>0,ae=/^---\s*\n[\s\S]*?\n---/.test(u),G=U&&ae&&(s?H:!0)&&k,oe=(0,J.useCallback)(async()=>{x(!0),w(null);try{let t;if(t=s?await fetch(`${W()}/profiles/catalog`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({id:c,body:u})}):await fetch(`${W()}/profiles/catalog/${encodeURIComponent(e)}`,{method:`PATCH`,headers:{"Content-Type":`application/json`},body:JSON.stringify({body:u})}),!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error??`Save failed: ${t.status}`)}j(!1),S.success(s?`Agent created`:`Agent saved`,{description:s?c:e}),a&&a(s?c:e)}catch(e){let t=e.message;w(t),S.error(s?`Failed to create agent`:`Failed to save agent`,{description:t})}finally{x(!1)}},[e,u,c,s,a]),se=(0,J.useCallback)(async()=>{if(!s){re(!1),x(!0),w(null);try{let t=await fetch(`${W()}/profiles/catalog/${encodeURIComponent(e)}`,{method:`DELETE`});if(!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error??`Delete failed: ${t.status}`)}S.success(`Agent deleted`,{description:e}),a&&a(e),i()}catch(e){let t=e.message;w(t),S.error(`Failed to delete agent`,{description:t})}finally{x(!1)}}},[e,s,i,a]),X=(0,J.useCallback)(e=>{f(e.body),j(!0),O(!1)},[]),le=(0,J.useCallback)(async()=>{L(!0),V(null),ee(null);try{let t=await fetch(`${W()}/profiles/catalog/test`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({agentId:s?c||`draft`:e,draftBody:u,sampleTask:P})});if(!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error??`Test failed: ${t.status}`)}ee(await t.json())}catch(e){V(e.message)}finally{L(!1)}},[e,u,c,s,P]);return p?(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Loading agent…`})}):(0,Y.jsxs)(`div`,{className:`flex flex-col h-full`,children:[!s&&(0,Y.jsx)(_e,{open:ne,title:`Delete agent "${e}"?`,description:`This removes the .md from disk. Version history stays in the DB — use a fresh custom agent if you want to recover the body later.`,confirmLabel:`Delete`,destructive:!0,onConfirm:se,onCancel:()=>re(!1)}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-3 px-4 py-2 border-b border-border`,children:[(0,Y.jsx)(`button`,{type:`button`,onClick:i,className:`p-1 rounded hover:bg-accent text-muted-foreground hover:text-foreground`,title:`Back`,children:(0,Y.jsx)(te,{className:`w-4 h-4`})}),(0,Y.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Y.jsx)(`div`,{className:`text-xs text-muted-foreground uppercase tracking-wide`,children:s?`New custom agent`:`Edit custom agent`}),s?(0,Y.jsx)(A,{value:c,onChange:e=>{l(e.target.value),j(!0)},placeholder:`custom-my-agent`,className:`text-sm font-mono mt-1 max-w-sm`}):(0,Y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-mono`,children:e}),r&&(0,Y.jsxs)(`button`,{type:`button`,onClick:async()=>{if(!(!o||!e))try{let t=await fetch(`${W()}/profiles/catalog/${encodeURIComponent(e)}`);if(!t.ok)return;o(r,e,(await t.json()).body)}catch{}},className:`inline-flex items-center gap-1 text-[11px] px-2 py-0.5 rounded-full border border-accent-primary/40 bg-accent-primary/15 text-accent-primary hover:bg-accent-primary/25 focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none`,title:`Hand this draft back to the AI Edit overlay`,children:[(0,Y.jsx)(ce,{className:`w-3 h-3`}),` Resume AI Edit`]}),g&&(0,Y.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-[10px] text-muted-foreground`,children:[(0,Y.jsx)(d,{className:`w-3 h-3 animate-spin`}),` loading AI draft…`]})]})]}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>{N(e=>!e),M||O(!1)},title:`Test this agent against a sample task`,children:[(0,Y.jsx)(K,{className:`w-3.5 h-3.5 mr-1`}),`Test`]}),!s&&(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>{O(e=>!e),D||N(!1)},title:`Version history`,children:[(0,Y.jsx)(q,{className:`w-3.5 h-3.5 mr-1`}),`History`]}),!s&&(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>re(!0),disabled:b,className:`text-red-400 hover:text-red-300`,children:[(0,Y.jsx)(_,{className:`w-3.5 h-3.5 mr-1`}),`Delete`]}),(0,Y.jsxs)(y,{size:`sm`,onClick:oe,disabled:!G||b,children:[(0,Y.jsx)(m,{className:`w-3.5 h-3.5 mr-1`}),b?`Saving…`:s?`Create`:`Save`]})]})]}),C&&(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 px-4 py-2 text-xs border-b border-red-500/30 bg-red-500/10 text-red-400`,children:[(0,Y.jsx)(B,{className:`w-3.5 h-3.5 flex-shrink-0`}),C]}),s&&c.length>0&&!H&&(0,Y.jsxs)(`div`,{className:`px-4 py-1.5 text-[11px] border-b border-yellow-500/30 bg-yellow-500/10 text-yellow-500`,children:[`Name must start with `,(0,Y.jsx)(`code`,{children:`custom-`}),` and contain only lowercase letters, digits, and hyphens.`]}),!ae&&(0,Y.jsxs)(`div`,{className:`px-4 py-1.5 text-[11px] border-b border-yellow-500/30 bg-yellow-500/10 text-yellow-500`,children:[`Missing YAML frontmatter (needs opening `,(0,Y.jsx)(`code`,{children:`---`}),` on the first line).`]}),(0,Y.jsxs)(`div`,{className:`flex flex-1 min-h-0`,children:[(0,Y.jsxs)(`div`,{className:`flex-1 flex flex-col min-h-0`,children:[(0,Y.jsxs)(`div`,{className:`px-4 py-1.5 border-b border-border text-[11px] font-mono text-muted-foreground flex items-center justify-between`,children:[(0,Y.jsxs)(`span`,{children:[`.claude/agents/`,s?c||`custom-…`:e,`.md`]}),k&&(0,Y.jsx)(`span`,{className:`text-yellow-500`,children:`● unsaved`})]}),(0,Y.jsx)(`textarea`,{value:u,onChange:e=>{f(e.target.value),j(!0)},spellCheck:!1,className:`flex-1 w-full p-4 text-xs font-mono bg-background text-foreground outline-none resize-none leading-relaxed`})]}),M&&(0,Y.jsxs)(`aside`,{className:`w-96 flex-shrink-0 border-l border-border flex flex-col min-h-0`,children:[(0,Y.jsxs)(`div`,{className:`px-3 py-2 border-b border-border text-xs font-medium text-muted-foreground uppercase tracking-wide flex items-center gap-2`,children:[(0,Y.jsx)(K,{className:`w-3.5 h-3.5`}),` Test agent`]}),(0,Y.jsxs)(`div`,{className:`p-3 border-b border-border`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between mb-1`,children:[(0,Y.jsx)(`label`,{className:`text-[11px] text-muted-foreground`,children:`Sample task`}),(0,Y.jsx)(`select`,{className:`h-6 text-[11px] rounded border border-border bg-background px-1`,value:``,onChange:e=>{let t=parseInt(e.target.value,10);!isNaN(t)&&t>0&&F(xe[t].prompt)},disabled:I,children:xe.map((e,t)=>(0,Y.jsx)(`option`,{value:t,children:e.label},t))})]}),(0,Y.jsx)(`textarea`,{value:P,onChange:e=>F(e.target.value),placeholder:`Describe what the agent should do, or pick a sample above.`,className:`w-full text-xs p-2 rounded border border-border bg-background min-h-[80px] resize-y font-mono`,disabled:I}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 mt-2`,children:[(0,Y.jsx)(y,{size:`sm`,onClick:le,disabled:I||!P.trim()||!u.trim(),children:I?(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(d,{className:`w-3.5 h-3.5 mr-1.5 animate-spin`}),` Running…`]}):(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(K,{className:`w-3.5 h-3.5 mr-1.5`}),` Run`]})}),(0,Y.jsx)(`span`,{className:`text-[11px] text-muted-foreground`,children:`Sandboxed; no files written.`})]})]}),(0,Y.jsxs)(`div`,{className:`flex-1 overflow-auto p-3 min-h-0`,children:[z&&(0,Y.jsx)(`div`,{className:`px-3 py-2 text-xs rounded border border-red-500/30 bg-red-500/10 text-red-400 mb-2`,children:z}),R&&(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-3 text-[11px] text-muted-foreground mb-2`,children:[(0,Y.jsxs)(`span`,{children:[(0,Y.jsx)(`span`,{className:`text-foreground font-mono`,children:R.tokens}),` tokens`]}),(0,Y.jsx)(`span`,{children:(0,Y.jsxs)(`span`,{className:`text-foreground font-mono`,children:[(R.durationMs/1e3).toFixed(1),`s`]})})]}),(0,Y.jsx)(`pre`,{className:`text-xs font-mono whitespace-pre-wrap leading-relaxed rounded border border-border bg-background p-3`,children:R.output})]}),!I&&!R&&!z&&(0,Y.jsx)(`p`,{className:`text-[11px] text-muted-foreground italic`,children:`Run a sample task against the current draft. Output appears here.`})]})]}),D&&!s&&(0,Y.jsxs)(`aside`,{className:`w-72 flex-shrink-0 border-l border-border flex flex-col`,children:[(0,Y.jsx)(`div`,{className:`px-3 py-2 border-b border-border text-xs font-medium text-muted-foreground uppercase tracking-wide`,children:`Version history`}),(0,Y.jsx)(`div`,{className:`flex-1 overflow-auto p-2 space-y-1`,children:T.length===0?(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground italic px-2 py-2`,children:`No prior versions recorded for this agent.`}):T.map(e=>(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>X(e),className:`w-full text-left px-2 py-1.5 rounded hover:bg-accent/50 transition-colors`,children:[(0,Y.jsxs)(`div`,{className:`text-xs font-mono text-foreground`,children:[`v`,e.version]}),(0,Y.jsx)(`div`,{className:`text-[10px] text-muted-foreground`,children:new Date(e.createdAt).toLocaleString()})]},e.version))}),(0,Y.jsx)(`div`,{className:`px-3 py-2 border-t border-border text-[11px] text-muted-foreground`,children:`Click a version to restore its body into the editor (you still need to Save).`})]})]})]})}var Ce={refineId:null,agentId:null,uiState:`closed`,phase:`idle`,history:[],streamingText:``,draftBody:null,baseBody:null,autoTest:!1,testResult:null,appliedVersion:null,errorMessage:null,applyConflict:null};function we(e){return e.replace(/<!--tool:[^>]+-->/g,``)}function Te(e,t){switch(t.type){case`OPEN`:return{...Ce,agentId:t.agentId,baseBody:t.baseBody,uiState:`composing`};case`START_TURN`:return{...e,refineId:t.refineId,uiState:`streaming`,phase:`reading`,streamingText:``,errorMessage:null,applyConflict:null,history:[...e.history,t.userTurn]};case`STREAM_DELTA`:return e.refineId===t.refineId?{...e,streamingText:e.streamingText+t.delta}:e;case`PHASE`:return e.refineId===t.refineId?{...e,phase:t.phase}:e;case`READY`:return e.refineId===t.refineId?{...e,uiState:`reviewing`,phase:`done`,draftBody:t.draftBody,streamingText:``,history:[...e.history,{role:`assistant`,content:we(t.draftBody),timestamp:Date.now()}]}:e;case`TEST_RESULT`:return e.refineId===t.refineId?{...e,testResult:t.result,history:[...e.history,{role:`system`,kind:`test_result`,content:t.result.output,timestamp:Date.now()}]}:e;case`APPLIED`:return e.refineId===t.refineId?{...e,uiState:`applied`,appliedVersion:t.version}:e;case`CANCELLED`:return{...e,uiState:`cancelled`};case`ERROR`:return t.refineId!==null&&e.refineId!==t.refineId?e:{...e,uiState:`error`,errorMessage:t.message};case`CONFLICT`:return e.refineId===t.refineId?{...e,applyConflict:t.reason}:e;case`TOGGLE_AUTO_TEST`:return{...e,autoTest:t.enabled};case`REHYDRATE`:return{...e,...t.payload};case`CLOSE`:return Ce;default:return e}}function Ee(e){switch(e.status){case`streaming`:return`streaming`;case`ready`:return`reviewing`;case`applied`:return`applied`;case`cancelled`:return`cancelled`;case`error`:return`error`;default:return e.draftBody?`reviewing`:`composing`}}function De(e){let[t,n]=(0,J.useReducer)(Te,Ce),{registerHandler:r,unregisterHandler:i}=oe(),a=(0,J.useRef)(null);a.current=t.refineId;let o=(0,J.useRef)(null);o.current=t.agentId;let s=(0,J.useRef)(e);s.current=e;let c=(0,J.useRef)(e);(0,J.useEffect)(()=>{e!==c.current&&(c.current=e,n({type:`CLOSE`}))},[e]),(0,J.useEffect)(()=>{let e=`agent-refine-${Math.random().toString(36).slice(2,9)}`;return r(e,e=>{let t=e;if(typeof t.type!=`string`||t.projectId!==s.current||t.refineId!==a.current)return;let r=t.refineId;switch(t.type){case`agent_refine_stream`:n({type:`STREAM_DELTA`,refineId:r,delta:t.delta});break;case`agent_refine_phase`:n({type:`PHASE`,refineId:r,phase:t.phase});break;case`agent_refine_ready`:n({type:`READY`,refineId:r,draftBody:t.draftBody});break;case`agent_refine_test`:n({type:`TEST_RESULT`,refineId:r,result:t.result});break;case`agent_refine_applied`:n({type:`APPLIED`,refineId:r,version:t.version});break;case`agent_refine_cancelled`:n({type:`CANCELLED`,refineId:r});break;case`agent_refine_error`:n({type:`ERROR`,refineId:r,message:t.error});break}}),()=>i(e)},[r,i]);let l=(0,J.useCallback)((e,t)=>{n({type:`OPEN`,agentId:e,baseBody:t})},[]),u=(0,J.useCallback)(()=>{n({type:`CLOSE`})},[]);return{state:t,open:l,start:(0,J.useCallback)(async e=>{let r=o.current;if(r)try{let i=await fetch(`${W()}/profiles/catalog/${encodeURIComponent(r)}/refine`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({instruction:e,autoTest:t.autoTest})});if(!i.ok){n({type:`ERROR`,refineId:null,message:(await i.json().catch(()=>({}))).error??`Server error (${i.status})`});return}let o=await i.json();a.current=o.refineId,n({type:`START_TURN`,refineId:o.refineId,userTurn:{role:`user`,content:e,timestamp:Date.now()}})}catch(e){n({type:`ERROR`,refineId:null,message:`Connection failed: ${e.message}`})}},[t.autoTest]),sendTurn:(0,J.useCallback)(async e=>{let t=a.current,r=o.current;if(!(!t||!r)){n({type:`START_TURN`,refineId:t,userTurn:{role:`user`,content:e,timestamp:Date.now()}});try{let i=await fetch(`${W()}/profiles/catalog/${encodeURIComponent(r)}/refine/${t}/turn`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({instruction:e})});i.ok||n({type:`ERROR`,refineId:t,message:(await i.json().catch(()=>({}))).error??`Server error (${i.status})`})}catch(e){n({type:`ERROR`,refineId:t,message:`Connection failed: ${e.message}`})}}},[]),cancel:(0,J.useCallback)(async()=>{let e=a.current,t=o.current;if(!(!e||!t))try{await fetch(`${W()}/profiles/catalog/${encodeURIComponent(t)}/refine/${e}`,{method:`DELETE`})}catch{}},[]),apply:(0,J.useCallback)(async e=>{let t=a.current,r=o.current;if(!(!t||!r))try{let i=await fetch(`${W()}/profiles/catalog/${encodeURIComponent(r)}/refine/${t}/apply`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({force:!!e})});if(i.status===409){let e=await i.json().catch(()=>({}));if(e.reason===`disk_changed`||e.reason===`name_changed`){n({type:`CONFLICT`,refineId:t,reason:e.reason});return}}if(!i.ok){n({type:`ERROR`,refineId:t,message:(await i.json().catch(()=>({}))).error??`Apply failed (${i.status})`});return}n({type:`APPLIED`,refineId:t,version:(await i.json()).version})}catch(e){n({type:`ERROR`,refineId:t,message:`Connection failed: ${e.message}`})}},[]),toggleAutoTest:(0,J.useCallback)(async e=>{let t=a.current,r=o.current;if(n({type:`TOGGLE_AUTO_TEST`,enabled:e}),!(!t||!r))try{await fetch(`${W()}/profiles/catalog/${encodeURIComponent(r)}/refine/${t}`,{method:`PATCH`,headers:{"Content-Type":`application/json`},body:JSON.stringify({autoTest:e})})}catch{}},[]),rehydrate:(0,J.useCallback)(async(e,t)=>{try{let r=await fetch(`${W()}/profiles/catalog/${encodeURIComponent(t)}`);if(!r.ok)throw Error(`load agent failed: ${r.status}`);let i=await r.json(),s=await fetch(`${W()}/profiles/catalog/${encodeURIComponent(t)}/refine/${e}`);if(!s.ok)throw Error(`load session failed: ${s.status}`);let c=await s.json();a.current=e,o.current=t,n({type:`REHYDRATE`,payload:{refineId:e,agentId:t,baseBody:i.body,draftBody:c.draftBody,history:c.history,autoTest:c.autoTest,phase:c.phase,uiState:Ee(c),streamingText:``}})}catch(e){n({type:`ERROR`,refineId:null,message:`Rehydrate failed: ${e.message}`})}},[]),close:u}}function Oe(){return typeof window>`u`||!(`__TAURI_INTERNALS__`in window)?!1:/mac/i.test(navigator.platform)}function ke(e){let[t,n]=(0,J.useState)(!1),r=(0,J.useRef)(null),i=(0,J.useRef)(null);(0,J.useEffect)(()=>(r.current=document.activeElement,()=>{let e=r.current;e&&e instanceof HTMLElement&&e.focus()}),[]);let a=(0,J.useCallback)(()=>{e.uiPhase===`streaming`||e.uiPhase===`reviewing`?n(!0):e.onClose()},[e]),o=(0,J.useCallback)(()=>{n(!1),e.onDiscard()},[e]);(0,J.useEffect)(()=>{let t=t=>{if(t.key===`Escape`){t.preventDefault(),a();return}if((t.metaKey||t.ctrlKey)&&t.key===`Enter`&&e.uiPhase===`reviewing`&&e.canApply&&!e.applyConflict){t.preventDefault(),e.onApply();return}e.onKeyDown?.(t)};return window.addEventListener(`keydown`,t),()=>window.removeEventListener(`keydown`,t)},[e,a]);let s=e.uiPhase===`composing`||e.uiPhase===`streaming`||e.uiPhase===`error`,c=e.uiPhase===`reviewing`||e.uiPhase===`applied`;return(0,Y.jsx)(`div`,{className:`fixed inset-0 z-50 flex p-3 pt-10 sm:p-6 sm:pt-12 bg-black/40 backdrop-blur-sm animate-in fade-in duration-150`,"data-testid":`ai-edit-backdrop`,children:(0,Y.jsxs)(`div`,{ref:i,role:`dialog`,"aria-modal":`true`,"aria-label":`${e.eyebrow} · ${e.targetLabel}`,className:`m-auto w-full h-full max-w-[1600px] flex flex-col bg-background rounded-xl border border-border/40 shadow-2xl overflow-hidden`,children:[(0,Y.jsx)(Ae,{eyebrow:e.eyebrow,targetLabel:e.targetLabel,targetLabelMono:e.targetLabelMono??!0,showApply:e.uiPhase===`reviewing`&&e.canApply&&!e.applyConflict,showDiscard:e.uiPhase!==`applied`,onApply:e.onApply,onDiscard:a,onMinimize:e.onMinimize}),s&&(0,Y.jsx)(je,{uiPhase:e.uiPhase,headline:e.uiPhase===`streaming`?e.streamingHeadline:e.headline,description:e.description,chips:e.chips,onChipSubmit:e.onChipSubmit,streamingText:e.streamingText,composer:e.composer,composerAccessory:e.composerAccessory,history:e.history??[],baseBody:e.baseBody,baseBodyDisclosureLabel:e.baseBodyDisclosureLabel,errorMessage:e.errorMessage}),c&&(0,Y.jsx)(Ie,{state:e,appliedNotice:e.appliedNotice}),t&&(0,Y.jsx)(He,{onCancel:()=>n(!1),onConfirm:o})]})})}function Ae({eyebrow:e,targetLabel:t,targetLabelMono:n,showApply:r,showDiscard:i,onApply:a,onDiscard:o,onMinimize:s}){return(0,Y.jsxs)(`div`,{className:`flex-shrink-0 flex items-center justify-between ${Oe()?`pl-[88px]`:`pl-4`} pr-4 h-14 border-b border-border bg-card/60 backdrop-blur-sm`,children:[(0,Y.jsxs)(`button`,{type:`button`,onClick:o,className:`flex items-center gap-2 group p-1 -ml-1 rounded hover:bg-accent focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none max-w-[60vw]`,"aria-label":`Back (Esc)`,children:[(0,Y.jsx)(te,{className:`w-4 h-4 text-muted-foreground group-hover:text-foreground transition-colors flex-shrink-0`}),(0,Y.jsxs)(`div`,{className:`text-left min-w-0`,children:[(0,Y.jsx)(`div`,{className:`text-[10px] uppercase tracking-wider text-muted-foreground/70 leading-none`,children:e}),n?(0,Y.jsx)(`code`,{className:`text-sm font-mono font-medium text-foreground truncate block`,children:t}):(0,Y.jsx)(`span`,{className:`text-sm font-medium text-foreground truncate block`,children:t})]})]}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[s&&(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:s,"aria-label":`Minimize`,"data-testid":`ai-edit-minimize`,children:(0,Y.jsx)(O,{className:`w-4 h-4`})}),i&&(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:o,children:`Discard`}),r&&(0,Y.jsxs)(y,{size:`sm`,onClick:a,className:`gap-1.5`,children:[(0,Y.jsx)(re,{className:`w-3.5 h-3.5`}),` Apply`,(0,Y.jsx)(`span`,{className:`text-[10px] opacity-70 ml-1`,children:`⌘⏎`})]})]})]})}function je({uiPhase:e,headline:t,description:n,chips:r,onChipSubmit:i,streamingText:a,composer:o,composerAccessory:s,history:c,baseBody:l,baseBodyDisclosureLabel:d,errorMessage:f}){let p=e===`composing`&&(r?.length??0)>0&&c.length===0,m=e===`error`,g=c.filter(e=>e.role===`user`||e.role===`system`&&e.kind===`test_result`);return(0,Y.jsx)(`div`,{className:`flex-1 min-h-0 overflow-y-auto`,children:(0,Y.jsxs)(`div`,{className:`mx-auto max-w-2xl px-6 py-12 pb-32 flex flex-col gap-6`,children:[(0,Y.jsxs)(`header`,{className:`text-center space-y-2`,children:[(0,Y.jsx)(`div`,{className:`inline-flex items-center justify-center w-10 h-10 rounded-full ring-1 mx-auto `+(m?`bg-red-500/10 ring-red-500/30`:`bg-accent-primary/15 ring-accent-primary/30`),children:m?(0,Y.jsx)(h,{className:`w-5 h-5 text-red-400`}):(0,Y.jsx)(u,{className:`w-5 h-5 text-accent-primary`})}),(0,Y.jsx)(`h1`,{className:`text-xl font-semibold tracking-tight`,children:m?`Something went wrong`:t}),m&&f&&(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground max-w-md mx-auto font-mono break-words`,children:f}),!m&&n&&(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground max-w-md mx-auto leading-relaxed`,children:n})]}),e===`streaming`&&(0,Y.jsx)(Ne,{streamingText:a}),p&&r&&i&&(0,Y.jsx)(Me,{chips:r,onPick:i}),(0,Y.jsxs)(`div`,{className:`space-y-2`,children:[o,s&&(0,Y.jsx)(`div`,{children:s})]}),g.length>0&&(0,Y.jsx)(Pe,{history:g}),l&&(0,Y.jsx)(Fe,{body:l,label:d??`View current content`})]})})}function Me({chips:e,onPick:t}){return(0,Y.jsxs)(`div`,{className:`space-y-2`,children:[(0,Y.jsx)(`div`,{className:`text-[11px] uppercase tracking-wider text-muted-foreground/70`,children:`Quick prompts`}),(0,Y.jsx)(`div`,{className:`flex flex-wrap gap-1.5`,children:e.map(e=>(0,Y.jsx)(`button`,{type:`button`,onClick:()=>t(e),className:`text-xs px-3 py-1.5 rounded-full border border-border bg-card/40 hover:border-accent-primary/50 hover:bg-accent-primary/10 hover:text-accent-primary text-foreground/80 transition-colors focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none`,children:e},e))})]})}function Ne({streamingText:e}){return(0,Y.jsx)(`div`,{className:`rounded-xl border border-accent-primary/30 bg-accent-primary/5 p-4`,children:e?(0,Y.jsxs)(`div`,{className:`text-xs font-mono whitespace-pre-wrap break-words text-foreground/80 max-h-64 overflow-y-auto leading-relaxed`,children:[We(e),(0,Y.jsx)(`span`,{className:`inline-block w-1.5 h-3 bg-accent-primary animate-pulse align-middle ml-0.5`})]}):(0,Y.jsxs)(`div`,{className:`text-xs text-muted-foreground italic flex items-center gap-1.5`,children:[(0,Y.jsx)(d,{className:`w-3 h-3 animate-spin`}),`Working on your refinement…`]})})}function Pe({history:e}){return e.length===0?null:(0,Y.jsxs)(`div`,{className:`space-y-2`,children:[(0,Y.jsx)(`div`,{className:`text-[11px] uppercase tracking-wider text-muted-foreground/70`,children:`History`}),(0,Y.jsx)(`div`,{className:`space-y-2`,children:e.map((e,t)=>(0,Y.jsx)(ze,{turn:e},t))})]})}function Fe({body:e,label:t}){let[n,r]=(0,J.useState)(!1);return(0,Y.jsxs)(`div`,{className:`border-t border-border/60 pt-4`,children:[(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>r(e=>!e),className:`flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none rounded px-1 -ml-1`,children:[n?(0,Y.jsx)(ee,{className:`w-3.5 h-3.5`}):(0,Y.jsx)(N,{className:`w-3.5 h-3.5`}),t]}),n&&(0,Y.jsx)(`pre`,{className:`mt-2 p-3 rounded-md bg-muted/40 border border-border text-[11px] font-mono whitespace-pre-wrap break-words max-h-72 overflow-y-auto text-foreground/80`,children:e})]})}function Ie({state:e,appliedNotice:t}){return(0,Y.jsxs)(`div`,{className:`flex-1 min-h-0 grid grid-cols-1 md:grid-cols-[minmax(0,1fr)_minmax(0,1.4fr)]`,children:[(0,Y.jsx)(Le,{history:e.history??[],composer:e.composer,composerAccessory:e.composerAccessory,secondaryAction:e.secondaryAction,appliedNotice:t,uiPhase:e.uiPhase}),(0,Y.jsx)(Be,{diff:e.diff,label:e.diffHeaderLabel,applyConflict:e.applyConflict??null,onForceApply:e.onForceApply})]})}function Le({history:e,composer:n,composerAccessory:r,secondaryAction:i,appliedNotice:a,uiPhase:o}){return(0,Y.jsxs)(`section`,{"aria-label":`Conversation`,className:`flex flex-col min-h-0 border-r border-border bg-card/20`,children:[(0,Y.jsxs)(`div`,{className:`flex-1 overflow-y-auto p-4 pb-32 space-y-3`,"aria-live":`polite`,children:[e.map((e,t)=>(0,Y.jsx)(ze,{turn:e},t)),o===`applied`&&(a??(0,Y.jsx)(Re,{}))]}),(0,Y.jsxs)(`div`,{className:`flex-shrink-0 border-t border-border p-3 space-y-2`,children:[n,r,i&&(0,Y.jsxs)(`button`,{type:`button`,onClick:i.onClick,className:`w-full inline-flex items-center justify-center gap-1.5 text-[11px] text-muted-foreground hover:text-foreground py-1.5 rounded hover:bg-accent/50 focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none`,children:[i.icon??(0,Y.jsx)(t,{className:`w-3 h-3`}),i.label]})]})]})}function Re(){return(0,Y.jsxs)(`div`,{className:`rounded-md border border-green-500/40 bg-green-500/10 p-3 text-xs text-green-300 flex items-center gap-2`,children:[(0,Y.jsx)(re,{className:`w-3.5 h-3.5`}),`Applied.`]})}function ze({turn:e}){return e.role===`user`?(0,Y.jsxs)(`div`,{className:`flex flex-col items-end`,children:[(0,Y.jsx)(`div`,{className:`text-[10px] text-muted-foreground mb-1`,children:`You`}),(0,Y.jsx)(`div`,{className:`max-w-[90%] rounded-2xl rounded-br-sm bg-accent-primary/15 text-foreground text-sm px-3 py-2 whitespace-pre-wrap break-words`,children:e.content})]}):e.role===`system`&&e.kind===`test_result`?(0,Y.jsxs)(`div`,{className:`rounded-md border border-accent-info/30 bg-accent-info/5 p-3 text-xs`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-1.5 font-medium mb-1 text-accent-info`,children:[(0,Y.jsx)(u,{className:`w-3 h-3`}),` Auto-test result`]}),(0,Y.jsx)(`pre`,{className:`text-[11px] font-mono whitespace-pre-wrap break-words text-foreground/80 max-h-48 overflow-y-auto`,children:e.content})]}):(0,Y.jsxs)(`div`,{className:`flex flex-col items-start`,children:[(0,Y.jsxs)(`div`,{className:`text-[10px] text-muted-foreground mb-1 inline-flex items-center gap-1`,children:[(0,Y.jsx)(u,{className:`w-3 h-3 text-accent-primary`}),` AI`]}),(0,Y.jsx)(`div`,{className:`max-w-[90%] rounded-2xl rounded-bl-sm border border-border bg-muted/30 text-xs px-3 py-2 text-muted-foreground italic`,children:`Refinement ready — review the diff on the right.`})]})}function Be({diff:e,label:t,applyConflict:n,onForceApply:i}){return(0,Y.jsxs)(`section`,{"aria-label":`Diff preview`,className:`flex flex-col min-h-0`,children:[(0,Y.jsxs)(`div`,{className:`flex-shrink-0 flex items-center justify-between px-4 h-9 border-b border-border bg-card/40`,children:[(0,Y.jsx)(`span`,{className:`text-xs font-mono text-muted-foreground truncate`,children:t??`Proposed changes`}),(0,Y.jsx)(`span`,{className:`text-[10px] text-muted-foreground`,children:`word-level diff`})]}),n===`disk_changed`&&(0,Y.jsx)(Ve,{kind:`disk_changed`,action:i&&(0,Y.jsxs)(y,{variant:`ghost`,size:`sm`,onClick:i,children:[(0,Y.jsx)(r,{className:`w-3 h-3 mr-1`}),` Force apply`]})}),n===`name_changed`&&(0,Y.jsx)(Ve,{kind:`name_changed`}),(0,Y.jsx)(`div`,{className:`flex-1 overflow-auto p-4 pb-32`,children:e})]})}function Ve({kind:e,action:t}){return(0,Y.jsxs)(`div`,{role:`alert`,className:`flex items-start justify-between gap-3 px-4 py-2 border-b border-yellow-500/40 bg-yellow-500/10 text-yellow-200 text-xs`,children:[(0,Y.jsxs)(`div`,{className:`flex items-start gap-2 min-w-0`,children:[(0,Y.jsx)(h,{className:`w-3.5 h-3.5 mt-0.5 flex-shrink-0`}),(0,Y.jsx)(`span`,{children:e===`disk_changed`?`The file changed on disk while you were editing. Apply was blocked.`:`The AI changed the agent name. Renaming is a separate explicit action — adjust the draft or rename in Studio.`})]}),t]})}function He({onCancel:e,onConfirm:t}){return(0,Y.jsx)(`div`,{role:`alertdialog`,"aria-modal":`true`,"aria-label":`Discard changes?`,className:`absolute inset-0 z-10 flex items-center justify-center bg-black/40`,children:(0,Y.jsxs)(`div`,{className:`rounded-lg border border-border bg-card p-5 max-w-sm shadow-xl`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 mb-2`,children:[(0,Y.jsx)(h,{className:`w-4 h-4 text-yellow-400`}),(0,Y.jsx)(`h2`,{className:`text-sm font-medium`,children:`Discard refinement?`})]}),(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground mb-4`,children:`The current draft will be cancelled and not applied.`}),(0,Y.jsxs)(`div`,{className:`flex justify-end gap-2`,children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:e,children:`Keep editing`}),(0,Y.jsx)(y,{size:`sm`,onClick:t,children:`Discard`})]})]})})}function Ue({hunks:e}){return(0,Y.jsx)(`pre`,{className:`font-mono text-xs leading-5 whitespace-pre-wrap break-words`,children:e.map((e,t)=>e.type===`eq`?(0,Y.jsx)(`span`,{className:`text-foreground/70`,children:e.text},t):e.type===`add`?(0,Y.jsxs)(`span`,{"data-hunk":`add`,className:`bg-green-500/20 text-green-200 rounded px-0.5 ring-1 ring-green-500/30`,children:[(0,Y.jsx)(`span`,{"aria-hidden":`true`,children:`+ `}),e.text]},t):(0,Y.jsxs)(`span`,{"data-hunk":`del`,className:`bg-red-500/20 text-red-200 line-through rounded px-0.5 ring-1 ring-red-500/30`,children:[(0,Y.jsx)(`span`,{"aria-hidden":`true`,children:`− `}),e.text]},t))})}function We(e){return e.replace(/<!--tool:[^>]+-->/g,``)}function Ge(e,t){let n=/(\s+)/,r=e.split(n),i=t.split(n);if(r.length>2e4||i.length>2e4)return Ke(e,t);let a=r.length,o=i.length,s=new Uint16Array((a+1)*(o+1)),c=o+1;for(let e=a-1;e>=0;e--)for(let t=o-1;t>=0;t--)r[e]===i[t]?s[e*c+t]=s[(e+1)*c+(t+1)]+1:s[e*c+t]=Math.max(s[(e+1)*c+t],s[e*c+(t+1)]);let l=[],u=0,d=0,f=null,p=``,m=()=>{f!==null&&p&&l.push({type:f,text:p}),f=null,p=``},h=(e,t)=>{f===e?p+=t:(m(),f=e,p=t)};for(;u<a&&d<o;)r[u]===i[d]?(h(`eq`,r[u]),u++,d++):s[(u+1)*c+d]>=s[u*c+(d+1)]?(h(`del`,r[u]),u++):(h(`add`,i[d]),d++);for(;u<a;)h(`del`,r[u++]);for(;d<o;)h(`add`,i[d++]);return m(),l}function Ke(e,t){let n=e.split(`
79
+ `;function Se({agentId:e,initialBody:t,initialName:n,draftFromRefine:r,onClose:i,onSaved:a,onResumeRefine:o}){let s=!e,[c,l]=(0,J.useState)(e??n??``),[f,p]=(0,J.useState)(t??$),[h,g]=(0,J.useState)(!s&&!t&&!r),[v,b]=(0,J.useState)(!!r&&!s),[x,S]=(0,J.useState)(!1),[C,T]=(0,J.useState)(null),[E,D]=(0,J.useState)([]),[O,k]=(0,J.useState)(!1),[A,j]=(0,J.useState)(!!t&&s),[N,P]=(0,J.useState)(!1),[F,I]=(0,J.useState)(``),[L,R]=(0,J.useState)(!1),[z,ee]=(0,J.useState)(null),[B,H]=(0,J.useState)(null),[ne,U]=(0,J.useState)(!1);(0,J.useEffect)(()=>{if(s||t!==void 0)return;if(r){let t=!1;return fetch(`${w()}/profiles/catalog/${encodeURIComponent(e)}/refine/${r}`).then(e=>{if(!e.ok)throw Error(`Load draft failed: ${e.status}`);return e.json()}).then(e=>{!t&&e.draftBody&&(p(e.draftBody),j(!0))}).catch(e=>{t||T(e.message)}).finally(()=>{t||b(!1)}),()=>{t=!0}}let n=!1;return fetch(`${w()}/profiles/catalog/${encodeURIComponent(e)}`).then(e=>{if(!e.ok)throw Error(`Load failed: ${e.status}`);return e.json()}).then(e=>{n||p(e.body)}).catch(e=>{n||T(e.message)}).finally(()=>{n||g(!1)}),()=>{n=!0}},[e,s,t,r]);let W=(0,J.useCallback)(()=>{s||fetch(`${w()}/profiles/catalog/${encodeURIComponent(e)}/versions`).then(e=>e.ok?e.json():{versions:[]}).then(e=>D(e.versions)).catch(()=>D([]))},[e,s]);(0,J.useEffect)(()=>{O&&W()},[O,W]);let G=/^custom-[a-z0-9][a-z0-9-]*$/.test(c),re=f.trim().length>0,ie=/^---\s*\n[\s\S]*?\n---/.test(f),K=re&&ie&&(s?G:!0)&&A,ae=(0,J.useCallback)(async()=>{S(!0),T(null);try{let t;if(t=s?await fetch(`${w()}/profiles/catalog`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({id:c,body:f})}):await fetch(`${w()}/profiles/catalog/${encodeURIComponent(e)}`,{method:`PATCH`,headers:{"Content-Type":`application/json`},body:JSON.stringify({body:f})}),!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error??`Save failed: ${t.status}`)}j(!1),u.success(s?`Agent created`:`Agent saved`,{description:s?c:e}),a&&a(s?c:e)}catch(e){let t=e.message;T(t),u.error(s?`Failed to create agent`:`Failed to save agent`,{description:t})}finally{S(!1)}},[e,f,c,s,a]),se=(0,J.useCallback)(async()=>{if(!s){U(!1),S(!0),T(null);try{let t=await fetch(`${w()}/profiles/catalog/${encodeURIComponent(e)}`,{method:`DELETE`});if(!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error??`Delete failed: ${t.status}`)}u.success(`Agent deleted`,{description:e}),a&&a(e),i()}catch(e){let t=e.message;T(t),u.error(`Failed to delete agent`,{description:t})}finally{S(!1)}}},[e,s,i,a]),X=(0,J.useCallback)(e=>{p(e.body),j(!0),k(!1)},[]),le=(0,J.useCallback)(async()=>{R(!0),H(null),ee(null);try{let t=await fetch(`${w()}/profiles/catalog/test`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({agentId:s?c||`draft`:e,draftBody:f,sampleTask:F})});if(!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error??`Test failed: ${t.status}`)}ee(await t.json())}catch(e){H(e.message)}finally{R(!1)}},[e,f,c,s,F]);return h?(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Loading agent…`})}):(0,Y.jsxs)(`div`,{className:`flex flex-col h-full`,children:[!s&&(0,Y.jsx)(_e,{open:ne,title:`Delete agent "${e}"?`,description:`This removes the .md from disk. Version history stays in the DB — use a fresh custom agent if you want to recover the body later.`,confirmLabel:`Delete`,destructive:!0,onConfirm:se,onCancel:()=>U(!1)}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-3 px-4 py-2 border-b border-border`,children:[(0,Y.jsx)(`button`,{type:`button`,onClick:i,className:`p-1 rounded hover:bg-accent text-muted-foreground hover:text-foreground`,title:`Back`,children:(0,Y.jsx)(V,{className:`w-4 h-4`})}),(0,Y.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Y.jsx)(`div`,{className:`text-xs text-muted-foreground uppercase tracking-wide`,children:s?`New custom agent`:`Edit custom agent`}),s?(0,Y.jsx)(M,{value:c,onChange:e=>{l(e.target.value),j(!0)},placeholder:`custom-my-agent`,className:`text-sm font-mono mt-1 max-w-sm`}):(0,Y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-mono`,children:e}),r&&(0,Y.jsxs)(`button`,{type:`button`,onClick:async()=>{if(!(!o||!e))try{let t=await fetch(`${w()}/profiles/catalog/${encodeURIComponent(e)}`);if(!t.ok)return;o(r,e,(await t.json()).body)}catch{}},className:`inline-flex items-center gap-1 text-[11px] px-2 py-0.5 rounded-full border border-accent-primary/40 bg-accent-primary/15 text-accent-primary hover:bg-accent-primary/25 focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none`,title:`Hand this draft back to the AI Edit overlay`,children:[(0,Y.jsx)(ce,{className:`w-3 h-3`}),` Resume AI Edit`]}),v&&(0,Y.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-[10px] text-muted-foreground`,children:[(0,Y.jsx)(d,{className:`w-3 h-3 animate-spin`}),` loading AI draft…`]})]})]}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>{P(e=>!e),N||k(!1)},title:`Test this agent against a sample task`,children:[(0,Y.jsx)(oe,{className:`w-3.5 h-3.5 mr-1`}),`Test`]}),!s&&(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>{k(e=>!e),O||P(!1)},title:`Version history`,children:[(0,Y.jsx)(q,{className:`w-3.5 h-3.5 mr-1`}),`History`]}),!s&&(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>U(!0),disabled:x,className:`text-red-400 hover:text-red-300`,children:[(0,Y.jsx)(_,{className:`w-3.5 h-3.5 mr-1`}),`Delete`]}),(0,Y.jsxs)(y,{size:`sm`,onClick:ae,disabled:!K||x,children:[(0,Y.jsx)(m,{className:`w-3.5 h-3.5 mr-1`}),x?`Saving…`:s?`Create`:`Save`]})]})]}),C&&(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 px-4 py-2 text-xs border-b border-red-500/30 bg-red-500/10 text-red-400`,children:[(0,Y.jsx)(te,{className:`w-3.5 h-3.5 flex-shrink-0`}),C]}),s&&c.length>0&&!G&&(0,Y.jsxs)(`div`,{className:`px-4 py-1.5 text-[11px] border-b border-yellow-500/30 bg-yellow-500/10 text-yellow-500`,children:[`Name must start with `,(0,Y.jsx)(`code`,{children:`custom-`}),` and contain only lowercase letters, digits, and hyphens.`]}),!ie&&(0,Y.jsxs)(`div`,{className:`px-4 py-1.5 text-[11px] border-b border-yellow-500/30 bg-yellow-500/10 text-yellow-500`,children:[`Missing YAML frontmatter (needs opening `,(0,Y.jsx)(`code`,{children:`---`}),` on the first line).`]}),(0,Y.jsxs)(`div`,{className:`flex flex-1 min-h-0`,children:[(0,Y.jsxs)(`div`,{className:`flex-1 flex flex-col min-h-0`,children:[(0,Y.jsxs)(`div`,{className:`px-4 py-1.5 border-b border-border text-[11px] font-mono text-muted-foreground flex items-center justify-between`,children:[(0,Y.jsxs)(`span`,{children:[`.claude/agents/`,s?c||`custom-…`:e,`.md`]}),A&&(0,Y.jsx)(`span`,{className:`text-yellow-500`,children:`● unsaved`})]}),(0,Y.jsx)(`textarea`,{value:f,onChange:e=>{p(e.target.value),j(!0)},spellCheck:!1,className:`flex-1 w-full p-4 text-xs font-mono bg-background text-foreground outline-none resize-none leading-relaxed`})]}),N&&(0,Y.jsxs)(`aside`,{className:`w-96 flex-shrink-0 border-l border-border flex flex-col min-h-0`,children:[(0,Y.jsxs)(`div`,{className:`px-3 py-2 border-b border-border text-xs font-medium text-muted-foreground uppercase tracking-wide flex items-center gap-2`,children:[(0,Y.jsx)(oe,{className:`w-3.5 h-3.5`}),` Test agent`]}),(0,Y.jsxs)(`div`,{className:`p-3 border-b border-border`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between mb-1`,children:[(0,Y.jsx)(`label`,{className:`text-[11px] text-muted-foreground`,children:`Sample task`}),(0,Y.jsx)(`select`,{className:`h-6 text-[11px] rounded border border-border bg-background px-1`,value:``,onChange:e=>{let t=parseInt(e.target.value,10);!isNaN(t)&&t>0&&I(xe[t].prompt)},disabled:L,children:xe.map((e,t)=>(0,Y.jsx)(`option`,{value:t,children:e.label},t))})]}),(0,Y.jsx)(`textarea`,{value:F,onChange:e=>I(e.target.value),placeholder:`Describe what the agent should do, or pick a sample above.`,className:`w-full text-xs p-2 rounded border border-border bg-background min-h-[80px] resize-y font-mono`,disabled:L}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 mt-2`,children:[(0,Y.jsx)(y,{size:`sm`,onClick:le,disabled:L||!F.trim()||!f.trim(),children:L?(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(d,{className:`w-3.5 h-3.5 mr-1.5 animate-spin`}),` Running…`]}):(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(oe,{className:`w-3.5 h-3.5 mr-1.5`}),` Run`]})}),(0,Y.jsx)(`span`,{className:`text-[11px] text-muted-foreground`,children:`Sandboxed; no files written.`})]})]}),(0,Y.jsxs)(`div`,{className:`flex-1 overflow-auto p-3 min-h-0`,children:[B&&(0,Y.jsx)(`div`,{className:`px-3 py-2 text-xs rounded border border-red-500/30 bg-red-500/10 text-red-400 mb-2`,children:B}),z&&(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-3 text-[11px] text-muted-foreground mb-2`,children:[(0,Y.jsxs)(`span`,{children:[(0,Y.jsx)(`span`,{className:`text-foreground font-mono`,children:z.tokens}),` tokens`]}),(0,Y.jsx)(`span`,{children:(0,Y.jsxs)(`span`,{className:`text-foreground font-mono`,children:[(z.durationMs/1e3).toFixed(1),`s`]})})]}),(0,Y.jsx)(`pre`,{className:`text-xs font-mono whitespace-pre-wrap leading-relaxed rounded border border-border bg-background p-3`,children:z.output})]}),!L&&!z&&!B&&(0,Y.jsx)(`p`,{className:`text-[11px] text-muted-foreground italic`,children:`Run a sample task against the current draft. Output appears here.`})]})]}),O&&!s&&(0,Y.jsxs)(`aside`,{className:`w-72 flex-shrink-0 border-l border-border flex flex-col`,children:[(0,Y.jsx)(`div`,{className:`px-3 py-2 border-b border-border text-xs font-medium text-muted-foreground uppercase tracking-wide`,children:`Version history`}),(0,Y.jsx)(`div`,{className:`flex-1 overflow-auto p-2 space-y-1`,children:E.length===0?(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground italic px-2 py-2`,children:`No prior versions recorded for this agent.`}):E.map(e=>(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>X(e),className:`w-full text-left px-2 py-1.5 rounded hover:bg-accent/50 transition-colors`,children:[(0,Y.jsxs)(`div`,{className:`text-xs font-mono text-foreground`,children:[`v`,e.version]}),(0,Y.jsx)(`div`,{className:`text-[10px] text-muted-foreground`,children:new Date(e.createdAt).toLocaleString()})]},e.version))}),(0,Y.jsx)(`div`,{className:`px-3 py-2 border-t border-border text-[11px] text-muted-foreground`,children:`Click a version to restore its body into the editor (you still need to Save).`})]})]})]})}var Ce={refineId:null,agentId:null,uiState:`closed`,phase:`idle`,history:[],streamingText:``,draftBody:null,baseBody:null,autoTest:!1,testResult:null,appliedVersion:null,errorMessage:null,applyConflict:null};function we(e){return e.replace(/<!--tool:[^>]+-->/g,``)}function Te(e,t){switch(t.type){case`OPEN`:return{...Ce,agentId:t.agentId,baseBody:t.baseBody,uiState:`composing`};case`START_TURN`:return{...e,refineId:t.refineId,uiState:`streaming`,phase:`reading`,streamingText:``,errorMessage:null,applyConflict:null,history:[...e.history,t.userTurn]};case`STREAM_DELTA`:return e.refineId===t.refineId?{...e,streamingText:e.streamingText+t.delta}:e;case`PHASE`:return e.refineId===t.refineId?{...e,phase:t.phase}:e;case`READY`:return e.refineId===t.refineId?{...e,uiState:`reviewing`,phase:`done`,draftBody:t.draftBody,streamingText:``,history:[...e.history,{role:`assistant`,content:we(t.draftBody),timestamp:Date.now()}]}:e;case`TEST_RESULT`:return e.refineId===t.refineId?{...e,testResult:t.result,history:[...e.history,{role:`system`,kind:`test_result`,content:t.result.output,timestamp:Date.now()}]}:e;case`APPLIED`:return e.refineId===t.refineId?{...e,uiState:`applied`,appliedVersion:t.version}:e;case`CANCELLED`:return{...e,uiState:`cancelled`};case`ERROR`:return t.refineId!==null&&e.refineId!==t.refineId?e:{...e,uiState:`error`,errorMessage:t.message};case`CONFLICT`:return e.refineId===t.refineId?{...e,applyConflict:t.reason}:e;case`TOGGLE_AUTO_TEST`:return{...e,autoTest:t.enabled};case`REHYDRATE`:return{...e,...t.payload};case`CLOSE`:return Ce;default:return e}}function Ee(e){switch(e.status){case`streaming`:return`streaming`;case`ready`:return`reviewing`;case`applied`:return`applied`;case`cancelled`:return`cancelled`;case`error`:return`error`;default:return e.draftBody?`reviewing`:`composing`}}function De(e){let[t,n]=(0,J.useReducer)(Te,Ce),{registerHandler:r,unregisterHandler:i}=P(),a=(0,J.useRef)(null);a.current=t.refineId;let o=(0,J.useRef)(null);o.current=t.agentId;let s=(0,J.useRef)(e);s.current=e;let c=(0,J.useRef)(e);(0,J.useEffect)(()=>{e!==c.current&&(c.current=e,n({type:`CLOSE`}))},[e]),(0,J.useEffect)(()=>{let e=`agent-refine-${Math.random().toString(36).slice(2,9)}`;return r(e,e=>{let t=e;if(typeof t.type!=`string`||t.projectId!==s.current||t.refineId!==a.current)return;let r=t.refineId;switch(t.type){case`agent_refine_stream`:n({type:`STREAM_DELTA`,refineId:r,delta:t.delta});break;case`agent_refine_phase`:n({type:`PHASE`,refineId:r,phase:t.phase});break;case`agent_refine_ready`:n({type:`READY`,refineId:r,draftBody:t.draftBody});break;case`agent_refine_test`:n({type:`TEST_RESULT`,refineId:r,result:t.result});break;case`agent_refine_applied`:n({type:`APPLIED`,refineId:r,version:t.version});break;case`agent_refine_cancelled`:n({type:`CANCELLED`,refineId:r});break;case`agent_refine_error`:n({type:`ERROR`,refineId:r,message:t.error});break}}),()=>i(e)},[r,i]);let l=(0,J.useCallback)((e,t)=>{n({type:`OPEN`,agentId:e,baseBody:t})},[]),u=(0,J.useCallback)(()=>{n({type:`CLOSE`})},[]);return{state:t,open:l,start:(0,J.useCallback)(async e=>{let r=o.current;if(r)try{let i=await fetch(`${w()}/profiles/catalog/${encodeURIComponent(r)}/refine`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({instruction:e,autoTest:t.autoTest})});if(!i.ok){n({type:`ERROR`,refineId:null,message:(await i.json().catch(()=>({}))).error??`Server error (${i.status})`});return}let o=await i.json();a.current=o.refineId,n({type:`START_TURN`,refineId:o.refineId,userTurn:{role:`user`,content:e,timestamp:Date.now()}})}catch(e){n({type:`ERROR`,refineId:null,message:`Connection failed: ${e.message}`})}},[t.autoTest]),sendTurn:(0,J.useCallback)(async e=>{let t=a.current,r=o.current;if(!(!t||!r)){n({type:`START_TURN`,refineId:t,userTurn:{role:`user`,content:e,timestamp:Date.now()}});try{let i=await fetch(`${w()}/profiles/catalog/${encodeURIComponent(r)}/refine/${t}/turn`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({instruction:e})});i.ok||n({type:`ERROR`,refineId:t,message:(await i.json().catch(()=>({}))).error??`Server error (${i.status})`})}catch(e){n({type:`ERROR`,refineId:t,message:`Connection failed: ${e.message}`})}}},[]),cancel:(0,J.useCallback)(async()=>{let e=a.current,t=o.current;if(!(!e||!t))try{await fetch(`${w()}/profiles/catalog/${encodeURIComponent(t)}/refine/${e}`,{method:`DELETE`})}catch{}},[]),apply:(0,J.useCallback)(async e=>{let t=a.current,r=o.current;if(!(!t||!r))try{let i=await fetch(`${w()}/profiles/catalog/${encodeURIComponent(r)}/refine/${t}/apply`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({force:!!e})});if(i.status===409){let e=await i.json().catch(()=>({}));if(e.reason===`disk_changed`||e.reason===`name_changed`){n({type:`CONFLICT`,refineId:t,reason:e.reason});return}}if(!i.ok){n({type:`ERROR`,refineId:t,message:(await i.json().catch(()=>({}))).error??`Apply failed (${i.status})`});return}n({type:`APPLIED`,refineId:t,version:(await i.json()).version})}catch(e){n({type:`ERROR`,refineId:t,message:`Connection failed: ${e.message}`})}},[]),toggleAutoTest:(0,J.useCallback)(async e=>{let t=a.current,r=o.current;if(n({type:`TOGGLE_AUTO_TEST`,enabled:e}),!(!t||!r))try{await fetch(`${w()}/profiles/catalog/${encodeURIComponent(r)}/refine/${t}`,{method:`PATCH`,headers:{"Content-Type":`application/json`},body:JSON.stringify({autoTest:e})})}catch{}},[]),rehydrate:(0,J.useCallback)(async(e,t)=>{try{let r=await fetch(`${w()}/profiles/catalog/${encodeURIComponent(t)}`);if(!r.ok)throw Error(`load agent failed: ${r.status}`);let i=await r.json(),s=await fetch(`${w()}/profiles/catalog/${encodeURIComponent(t)}/refine/${e}`);if(!s.ok)throw Error(`load session failed: ${s.status}`);let c=await s.json();a.current=e,o.current=t,n({type:`REHYDRATE`,payload:{refineId:e,agentId:t,baseBody:i.body,draftBody:c.draftBody,history:c.history,autoTest:c.autoTest,phase:c.phase,uiState:Ee(c),streamingText:``}})}catch(e){n({type:`ERROR`,refineId:null,message:`Rehydrate failed: ${e.message}`})}},[]),close:u}}function Oe(){return typeof window>`u`||!(`__TAURI_INTERNALS__`in window)?!1:/mac/i.test(navigator.platform)}function ke(e){let[t,n]=(0,J.useState)(!1),r=(0,J.useRef)(null),i=(0,J.useRef)(null);(0,J.useEffect)(()=>(r.current=document.activeElement,()=>{let e=r.current;e&&e instanceof HTMLElement&&e.focus()}),[]);let a=(0,J.useCallback)(()=>{e.uiPhase===`streaming`||e.uiPhase===`reviewing`?n(!0):e.onClose()},[e]),o=(0,J.useCallback)(()=>{n(!1),e.onDiscard()},[e]);(0,J.useEffect)(()=>{let t=t=>{if(t.key===`Escape`){t.preventDefault(),a();return}if((t.metaKey||t.ctrlKey)&&t.key===`Enter`&&e.uiPhase===`reviewing`&&e.canApply&&!e.applyConflict){t.preventDefault(),e.onApply();return}e.onKeyDown?.(t)};return window.addEventListener(`keydown`,t),()=>window.removeEventListener(`keydown`,t)},[e,a]);let s=e.uiPhase===`composing`||e.uiPhase===`streaming`||e.uiPhase===`error`,c=e.uiPhase===`reviewing`||e.uiPhase===`applied`;return(0,Y.jsx)(`div`,{className:`fixed inset-0 z-50 flex p-3 pt-10 sm:p-6 sm:pt-12 bg-black/40 backdrop-blur-sm animate-in fade-in duration-150`,"data-testid":`ai-edit-backdrop`,children:(0,Y.jsxs)(`div`,{ref:i,role:`dialog`,"aria-modal":`true`,"aria-label":`${e.eyebrow} · ${e.targetLabel}`,className:`m-auto w-full h-full max-w-[1600px] flex flex-col bg-background rounded-xl border border-border/40 shadow-2xl overflow-hidden`,children:[(0,Y.jsx)(Ae,{eyebrow:e.eyebrow,targetLabel:e.targetLabel,targetLabelMono:e.targetLabelMono??!0,showApply:e.uiPhase===`reviewing`&&e.canApply&&!e.applyConflict,showDiscard:e.uiPhase!==`applied`,onApply:e.onApply,onDiscard:a,onMinimize:e.onMinimize}),s&&(0,Y.jsx)(je,{uiPhase:e.uiPhase,headline:e.uiPhase===`streaming`?e.streamingHeadline:e.headline,description:e.description,chips:e.chips,onChipSubmit:e.onChipSubmit,streamingText:e.streamingText,composer:e.composer,composerAccessory:e.composerAccessory,history:e.history??[],baseBody:e.baseBody,baseBodyDisclosureLabel:e.baseBodyDisclosureLabel,errorMessage:e.errorMessage}),c&&(0,Y.jsx)(Ie,{state:e,appliedNotice:e.appliedNotice}),t&&(0,Y.jsx)(He,{onCancel:()=>n(!1),onConfirm:o})]})})}function Ae({eyebrow:e,targetLabel:t,targetLabelMono:n,showApply:r,showDiscard:i,onApply:a,onDiscard:o,onMinimize:s}){return(0,Y.jsxs)(`div`,{className:`flex-shrink-0 flex items-center justify-between ${Oe()?`pl-[88px]`:`pl-4`} pr-4 h-14 border-b border-border bg-card/60 backdrop-blur-sm`,children:[(0,Y.jsxs)(`button`,{type:`button`,onClick:o,className:`flex items-center gap-2 group p-1 -ml-1 rounded hover:bg-accent focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none max-w-[60vw]`,"aria-label":`Back (Esc)`,children:[(0,Y.jsx)(V,{className:`w-4 h-4 text-muted-foreground group-hover:text-foreground transition-colors flex-shrink-0`}),(0,Y.jsxs)(`div`,{className:`text-left min-w-0`,children:[(0,Y.jsx)(`div`,{className:`text-[10px] uppercase tracking-wider text-muted-foreground/70 leading-none`,children:e}),n?(0,Y.jsx)(`code`,{className:`text-sm font-mono font-medium text-foreground truncate block`,children:t}):(0,Y.jsx)(`span`,{className:`text-sm font-medium text-foreground truncate block`,children:t})]})]}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-2`,children:[s&&(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:s,"aria-label":`Minimize`,"data-testid":`ai-edit-minimize`,children:(0,Y.jsx)(k,{className:`w-4 h-4`})}),i&&(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:o,children:`Discard`}),r&&(0,Y.jsxs)(y,{size:`sm`,onClick:a,className:`gap-1.5`,children:[(0,Y.jsx)(U,{className:`w-3.5 h-3.5`}),` Apply`,(0,Y.jsx)(`span`,{className:`text-[10px] opacity-70 ml-1`,children:`⌘⏎`})]})]})]})}function je({uiPhase:e,headline:t,description:n,chips:r,onChipSubmit:i,streamingText:a,composer:o,composerAccessory:s,history:c,baseBody:u,baseBodyDisclosureLabel:d,errorMessage:f}){let p=e===`composing`&&(r?.length??0)>0&&c.length===0,m=e===`error`,g=c.filter(e=>e.role===`user`||e.role===`system`&&e.kind===`test_result`);return(0,Y.jsx)(`div`,{className:`flex-1 min-h-0 overflow-y-auto`,children:(0,Y.jsxs)(`div`,{className:`mx-auto max-w-2xl px-6 py-12 pb-32 flex flex-col gap-6`,children:[(0,Y.jsxs)(`header`,{className:`text-center space-y-2`,children:[(0,Y.jsx)(`div`,{className:`inline-flex items-center justify-center w-10 h-10 rounded-full ring-1 mx-auto `+(m?`bg-red-500/10 ring-red-500/30`:`bg-accent-primary/15 ring-accent-primary/30`),children:m?(0,Y.jsx)(h,{className:`w-5 h-5 text-red-400`}):(0,Y.jsx)(l,{className:`w-5 h-5 text-accent-primary`})}),(0,Y.jsx)(`h1`,{className:`text-xl font-semibold tracking-tight`,children:m?`Something went wrong`:t}),m&&f&&(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground max-w-md mx-auto font-mono break-words`,children:f}),!m&&n&&(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground max-w-md mx-auto leading-relaxed`,children:n})]}),e===`streaming`&&(0,Y.jsx)(Ne,{streamingText:a}),p&&r&&i&&(0,Y.jsx)(Me,{chips:r,onPick:i}),(0,Y.jsxs)(`div`,{className:`space-y-2`,children:[o,s&&(0,Y.jsx)(`div`,{children:s})]}),g.length>0&&(0,Y.jsx)(Pe,{history:g}),u&&(0,Y.jsx)(Fe,{body:u,label:d??`View current content`})]})})}function Me({chips:e,onPick:t}){return(0,Y.jsxs)(`div`,{className:`space-y-2`,children:[(0,Y.jsx)(`div`,{className:`text-[11px] uppercase tracking-wider text-muted-foreground/70`,children:`Quick prompts`}),(0,Y.jsx)(`div`,{className:`flex flex-wrap gap-1.5`,children:e.map(e=>(0,Y.jsx)(`button`,{type:`button`,onClick:()=>t(e),className:`text-xs px-3 py-1.5 rounded-full border border-border bg-card/40 hover:border-accent-primary/50 hover:bg-accent-primary/10 hover:text-accent-primary text-foreground/80 transition-colors focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none`,children:e},e))})]})}function Ne({streamingText:e}){return(0,Y.jsx)(`div`,{className:`rounded-xl border border-accent-primary/30 bg-accent-primary/5 p-4`,children:e?(0,Y.jsxs)(`div`,{className:`text-xs font-mono whitespace-pre-wrap break-words text-foreground/80 max-h-64 overflow-y-auto leading-relaxed`,children:[We(e),(0,Y.jsx)(`span`,{className:`inline-block w-1.5 h-3 bg-accent-primary animate-pulse align-middle ml-0.5`})]}):(0,Y.jsxs)(`div`,{className:`text-xs text-muted-foreground italic flex items-center gap-1.5`,children:[(0,Y.jsx)(d,{className:`w-3 h-3 animate-spin`}),`Working on your refinement…`]})})}function Pe({history:e}){return e.length===0?null:(0,Y.jsxs)(`div`,{className:`space-y-2`,children:[(0,Y.jsx)(`div`,{className:`text-[11px] uppercase tracking-wider text-muted-foreground/70`,children:`History`}),(0,Y.jsx)(`div`,{className:`space-y-2`,children:e.map((e,t)=>(0,Y.jsx)(ze,{turn:e},t))})]})}function Fe({body:e,label:t}){let[n,r]=(0,J.useState)(!1);return(0,Y.jsxs)(`div`,{className:`border-t border-border/60 pt-4`,children:[(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>r(e=>!e),className:`flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none rounded px-1 -ml-1`,children:[n?(0,Y.jsx)(B,{className:`w-3.5 h-3.5`}):(0,Y.jsx)(F,{className:`w-3.5 h-3.5`}),t]}),n&&(0,Y.jsx)(`pre`,{className:`mt-2 p-3 rounded-md bg-muted/40 border border-border text-[11px] font-mono whitespace-pre-wrap break-words max-h-72 overflow-y-auto text-foreground/80`,children:e})]})}function Ie({state:e,appliedNotice:t}){return(0,Y.jsxs)(`div`,{className:`flex-1 min-h-0 grid grid-cols-1 md:grid-cols-[minmax(0,1fr)_minmax(0,1.4fr)]`,children:[(0,Y.jsx)(Le,{history:e.history??[],composer:e.composer,composerAccessory:e.composerAccessory,secondaryAction:e.secondaryAction,appliedNotice:t,uiPhase:e.uiPhase}),(0,Y.jsx)(Be,{diff:e.diff,label:e.diffHeaderLabel,applyConflict:e.applyConflict??null,onForceApply:e.onForceApply})]})}function Le({history:e,composer:n,composerAccessory:r,secondaryAction:i,appliedNotice:a,uiPhase:o}){return(0,Y.jsxs)(`section`,{"aria-label":`Conversation`,className:`flex flex-col min-h-0 border-r border-border bg-card/20`,children:[(0,Y.jsxs)(`div`,{className:`flex-1 overflow-y-auto p-4 pb-32 space-y-3`,"aria-live":`polite`,children:[e.map((e,t)=>(0,Y.jsx)(ze,{turn:e},t)),o===`applied`&&(a??(0,Y.jsx)(Re,{}))]}),(0,Y.jsxs)(`div`,{className:`flex-shrink-0 border-t border-border p-3 space-y-2`,children:[n,r,i&&(0,Y.jsxs)(`button`,{type:`button`,onClick:i.onClick,className:`w-full inline-flex items-center justify-center gap-1.5 text-[11px] text-muted-foreground hover:text-foreground py-1.5 rounded hover:bg-accent/50 focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none`,children:[i.icon??(0,Y.jsx)(t,{className:`w-3 h-3`}),i.label]})]})]})}function Re(){return(0,Y.jsxs)(`div`,{className:`rounded-md border border-green-500/40 bg-green-500/10 p-3 text-xs text-green-300 flex items-center gap-2`,children:[(0,Y.jsx)(U,{className:`w-3.5 h-3.5`}),`Applied.`]})}function ze({turn:e}){return e.role===`user`?(0,Y.jsxs)(`div`,{className:`flex flex-col items-end`,children:[(0,Y.jsx)(`div`,{className:`text-[10px] text-muted-foreground mb-1`,children:`You`}),(0,Y.jsx)(`div`,{className:`max-w-[90%] rounded-2xl rounded-br-sm bg-accent-primary/15 text-foreground text-sm px-3 py-2 whitespace-pre-wrap break-words`,children:e.content})]}):e.role===`system`&&e.kind===`test_result`?(0,Y.jsxs)(`div`,{className:`rounded-md border border-accent-info/30 bg-accent-info/5 p-3 text-xs`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-1.5 font-medium mb-1 text-accent-info`,children:[(0,Y.jsx)(l,{className:`w-3 h-3`}),` Auto-test result`]}),(0,Y.jsx)(`pre`,{className:`text-[11px] font-mono whitespace-pre-wrap break-words text-foreground/80 max-h-48 overflow-y-auto`,children:e.content})]}):(0,Y.jsxs)(`div`,{className:`flex flex-col items-start`,children:[(0,Y.jsxs)(`div`,{className:`text-[10px] text-muted-foreground mb-1 inline-flex items-center gap-1`,children:[(0,Y.jsx)(l,{className:`w-3 h-3 text-accent-primary`}),` AI`]}),(0,Y.jsx)(`div`,{className:`max-w-[90%] rounded-2xl rounded-bl-sm border border-border bg-muted/30 text-xs px-3 py-2 text-muted-foreground italic`,children:`Refinement ready — review the diff on the right.`})]})}function Be({diff:e,label:t,applyConflict:n,onForceApply:i}){return(0,Y.jsxs)(`section`,{"aria-label":`Diff preview`,className:`flex flex-col min-h-0`,children:[(0,Y.jsxs)(`div`,{className:`flex-shrink-0 flex items-center justify-between px-4 h-9 border-b border-border bg-card/40`,children:[(0,Y.jsx)(`span`,{className:`text-xs font-mono text-muted-foreground truncate`,children:t??`Proposed changes`}),(0,Y.jsx)(`span`,{className:`text-[10px] text-muted-foreground`,children:`word-level diff`})]}),n===`disk_changed`&&(0,Y.jsx)(Ve,{kind:`disk_changed`,action:i&&(0,Y.jsxs)(y,{variant:`ghost`,size:`sm`,onClick:i,children:[(0,Y.jsx)(r,{className:`w-3 h-3 mr-1`}),` Force apply`]})}),n===`name_changed`&&(0,Y.jsx)(Ve,{kind:`name_changed`}),(0,Y.jsx)(`div`,{className:`flex-1 overflow-auto p-4 pb-32`,children:e})]})}function Ve({kind:e,action:t}){return(0,Y.jsxs)(`div`,{role:`alert`,className:`flex items-start justify-between gap-3 px-4 py-2 border-b border-yellow-500/40 bg-yellow-500/10 text-yellow-200 text-xs`,children:[(0,Y.jsxs)(`div`,{className:`flex items-start gap-2 min-w-0`,children:[(0,Y.jsx)(h,{className:`w-3.5 h-3.5 mt-0.5 flex-shrink-0`}),(0,Y.jsx)(`span`,{children:e===`disk_changed`?`The file changed on disk while you were editing. Apply was blocked.`:`The AI changed the agent name. Renaming is a separate explicit action — adjust the draft or rename in Studio.`})]}),t]})}function He({onCancel:e,onConfirm:t}){return(0,Y.jsx)(`div`,{role:`alertdialog`,"aria-modal":`true`,"aria-label":`Discard changes?`,className:`absolute inset-0 z-10 flex items-center justify-center bg-black/40`,children:(0,Y.jsxs)(`div`,{className:`rounded-lg border border-border bg-card p-5 max-w-sm shadow-xl`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 mb-2`,children:[(0,Y.jsx)(h,{className:`w-4 h-4 text-yellow-400`}),(0,Y.jsx)(`h2`,{className:`text-sm font-medium`,children:`Discard refinement?`})]}),(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground mb-4`,children:`The current draft will be cancelled and not applied.`}),(0,Y.jsxs)(`div`,{className:`flex justify-end gap-2`,children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:e,children:`Keep editing`}),(0,Y.jsx)(y,{size:`sm`,onClick:t,children:`Discard`})]})]})})}function Ue({hunks:e}){return(0,Y.jsx)(`pre`,{className:`font-mono text-xs leading-5 whitespace-pre-wrap break-words`,children:e.map((e,t)=>e.type===`eq`?(0,Y.jsx)(`span`,{className:`text-foreground/70`,children:e.text},t):e.type===`add`?(0,Y.jsxs)(`span`,{"data-hunk":`add`,className:`bg-green-500/20 text-green-200 rounded px-0.5 ring-1 ring-green-500/30`,children:[(0,Y.jsx)(`span`,{"aria-hidden":`true`,children:`+ `}),e.text]},t):(0,Y.jsxs)(`span`,{"data-hunk":`del`,className:`bg-red-500/20 text-red-200 line-through rounded px-0.5 ring-1 ring-red-500/30`,children:[(0,Y.jsx)(`span`,{"aria-hidden":`true`,children:`− `}),e.text]},t))})}function We(e){return e.replace(/<!--tool:[^>]+-->/g,``)}function Ge(e,t){let n=/(\s+)/,r=e.split(n),i=t.split(n);if(r.length>2e4||i.length>2e4)return Ke(e,t);let a=r.length,o=i.length,s=new Uint16Array((a+1)*(o+1)),c=o+1;for(let e=a-1;e>=0;e--)for(let t=o-1;t>=0;t--)r[e]===i[t]?s[e*c+t]=s[(e+1)*c+(t+1)]+1:s[e*c+t]=Math.max(s[(e+1)*c+t],s[e*c+(t+1)]);let l=[],u=0,d=0,f=null,p=``,m=()=>{f!==null&&p&&l.push({type:f,text:p}),f=null,p=``},h=(e,t)=>{f===e?p+=t:(m(),f=e,p=t)};for(;u<a&&d<o;)r[u]===i[d]?(h(`eq`,r[u]),u++,d++):s[(u+1)*c+d]>=s[u*c+(d+1)]?(h(`del`,r[u]),u++):(h(`add`,i[d]),d++);for(;u<a;)h(`del`,r[u++]);for(;d<o;)h(`add`,i[d++]);return m(),l}function Ke(e,t){let n=e.split(`
80
80
  `),r=t.split(`
81
81
  `),i=[],a=0,o=0;for(;a<n.length&&o<r.length;)if(n[a]===r[o])i.push({type:`eq`,text:n[a]+`
82
82
  `}),a++,o++;else{let e=r.indexOf(n[a],o),t=n.indexOf(r[o],a);e!==-1&&(t===-1||e-o<=t-a)?(i.push({type:`add`,text:r[o]+`
83
83
  `}),o++):(i.push({type:`del`,text:n[a]+`
84
84
  `}),a++)}for(;a<n.length;)i.push({type:`del`,text:n[a++]+`
85
85
  `});for(;o<r.length;)i.push({type:`add`,text:r[o++]+`
86
- `});return i}function qe({value:e,onChange:t,onSubmit:n,disabled:r,placeholder:i,autoFocus:a,inputRef:o}){return(0,Y.jsxs)(`div`,{className:`space-y-2`,children:[(0,Y.jsxs)(`div`,{className:`relative rounded-xl border border-border bg-card/40 focus-within:border-accent-primary/40 focus-within:ring-2 focus-within:ring-accent-primary/20 transition-all`,children:[(0,Y.jsx)(`textarea`,{ref:o,value:e,onChange:e=>t(e.target.value),placeholder:i,rows:3,disabled:r,autoFocus:a,className:`w-full text-sm p-3 pr-14 bg-transparent resize-none focus:outline-none disabled:opacity-50 placeholder:text-muted-foreground/60`,onKeyDown:e=>{e.key===`Enter`&&(e.metaKey||e.ctrlKey)&&(e.preventDefault(),n())}}),(0,Y.jsx)(`button`,{type:`button`,onClick:n,disabled:!e.trim()||r,"aria-label":`Send (⌘⏎)`,className:`absolute bottom-2.5 right-2.5 p-2 rounded-md bg-accent-primary text-white hover:bg-accent-primary/90 disabled:opacity-30 disabled:cursor-not-allowed focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none transition-opacity`,children:r?(0,Y.jsx)(d,{className:`w-3.5 h-3.5 animate-spin`}):(0,Y.jsx)(Je,{})})]}),(0,Y.jsx)(`div`,{className:`flex items-center justify-end text-[11px] text-muted-foreground/70`,children:(0,Y.jsxs)(`span`,{children:[(0,Y.jsx)(`kbd`,{className:`px-1.5 py-0.5 rounded bg-muted/60 text-[10px] font-mono`,children:`⌘⏎`}),` submit`,(0,Y.jsx)(`span`,{className:`mx-1.5`,children:`·`}),(0,Y.jsx)(`kbd`,{className:`px-1.5 py-0.5 rounded bg-muted/60 text-[10px] font-mono`,children:`Esc`}),` cancel`]})})]})}function Je(){return(0,Y.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,strokeLinecap:`round`,strokeLinejoin:`round`,className:`w-3.5 h-3.5`,children:[(0,Y.jsx)(`path`,{d:`M22 2 11 13`}),(0,Y.jsx)(`path`,{d:`M22 2 15 22l-4-9-9-4Z`})]})}var Ye=[`Tighten the tool list`,`Make the personality stricter`,`Add a Workflow protocol section`,`Shorten and sharpen the description`,`Match the sr-developer style`];function Xe({agentId:e,baseBody:n,resumeRefineId:r,onClose:i,onMinimize:a,onStateChange:o,onOpenInStudio:s,onApplied:c}){let{activeProjectId:l}=ie(),u=De(l),[d,f]=(0,J.useState)(``),p=(0,J.useRef)(null),m=(0,J.useRef)(!1);(0,J.useEffect)(()=>{m.current||(m.current=!0,r?u.rehydrate(r,e):u.open(e,n))},[e,n,r,u]),(0,J.useEffect)(()=>{let e=setTimeout(()=>p.current?.focus(),50);return()=>clearTimeout(e)},[]);let h=(0,J.useRef)(o);h.current=o,(0,J.useEffect)(()=>{h.current?.({refineId:u.state.refineId??null})},[u.state.refineId]);let g=(0,J.useRef)(null);(0,J.useEffect)(()=>{u.state.uiState===`applied`&&u.state.appliedVersion!==null&&u.state.appliedVersion!==g.current&&(g.current=u.state.appliedVersion,c&&u.state.agentId&&c(u.state.agentId,u.state.appliedVersion))},[u.state.uiState,u.state.appliedVersion,u.state.agentId,c]);let _=(0,J.useCallback)(async e=>{let t=e.trim();t&&(f(``),u.state.refineId?await u.sendTurn(t):await u.start(t))},[u]),v=(0,J.useCallback)(()=>{_(d)},[d,_]),y=(0,J.useCallback)(async()=>{u.state.refineId&&(u.state.uiState===`streaming`||u.state.uiState===`reviewing`)&&await u.cancel(),i()},[u,i]),b=Ze(u.state),x=(0,J.useMemo)(()=>!u.state.draftBody||!u.state.baseBody?null:(0,Y.jsx)(Ue,{hunks:Ge(u.state.baseBody,u.state.draftBody)}),[u.state.baseBody,u.state.draftBody]);return(0,Y.jsx)(ke,{uiPhase:b,errorMessage:u.state.errorMessage,applyConflict:u.state.applyConflict,eyebrow:`AI Edit`,targetLabel:e,targetLabelMono:!0,headline:`Refine your agent`,streamingHeadline:`Refining your agent…`,description:Qe(n),chips:Ye,onChipSubmit:e=>void _(e),composer:(0,Y.jsx)(qe,{value:d,onChange:f,onSubmit:v,disabled:b===`streaming`||b===`applied`,placeholder:u.state.refineId?`Send a follow-up refinement…`:`Describe how to refine this agent…`,inputRef:p}),streamingText:u.state.streamingText,history:u.state.history,diff:x,diffHeaderLabel:`.claude/agents/${e}.md`,baseBody:n,baseBodyDisclosureLabel:`View current agent body`,appliedNotice:u.state.appliedVersion===null?void 0:(0,Y.jsxs)(`div`,{className:`rounded-md border border-green-500/40 bg-green-500/10 p-3 text-xs text-green-300`,children:[`Applied as version `,u.state.appliedVersion,`.`]}),canApply:u.state.uiState===`reviewing`&&!!u.state.draftBody&&!u.state.applyConflict,onApply:()=>void u.apply(),onForceApply:()=>void u.apply(!0),onDiscard:()=>void y(),onClose:()=>void y(),onMinimize:a?()=>a(u.state.refineId??null):void 0,secondaryAction:s&&u.state.draftBody&&u.state.refineId?{label:`Open draft in Studio for manual editing`,icon:(0,Y.jsx)(t,{className:`w-3 h-3`}),onClick:()=>{u.state.refineId&&u.state.draftBody&&s&&s(u.state.refineId,u.state.draftBody)}}:void 0})}function Ze(e){switch(e.uiState){case`streaming`:return`streaming`;case`reviewing`:return`reviewing`;case`applied`:return`applied`;case`error`:return`error`;default:return`composing`}}function Qe(e){let t=e.match(/^---\r?\n([\s\S]*?)\r?\n---/);if(!t)return null;let n=t[1].match(/^description:\s*([\s\S]*?)(?=^[a-z_]+:\s|\Z)/m);if(!n)return null;let r=n[1].trim();return(r.startsWith(`"`)&&r.endsWith(`"`)||r.startsWith(`'`)&&r.endsWith(`'`))&&(r=r.slice(1,-1)),r=r.replace(/\\n/g,` `).replace(/\\t/g,` `).replace(/\\"/g,`"`).replace(/\\'/g,`'`).replace(/\\\\/g,`\\`).replace(/\s+/g,` `).trim(),r.length>240&&(r=r.slice(0,237)+`…`),r||null}function $e(){let[e,t]=(0,J.useState)([]),[n,r]=(0,J.useState)(null),[i,o]=(0,J.useState)(null),[s,l]=(0,J.useState)(!0),[m,h]=(0,J.useState)(!1),[g,_]=(0,J.useState)(null),[v,b]=(0,J.useState)({kind:`closed`}),[S,w]=(0,J.useState)({kind:`closed`}),[E,O]=(0,J.useState)(!1),[M,N]=(0,J.useState)(``),[P,F]=(0,J.useState)(`all`),[L,R]=(0,J.useState)(null),[ee,z]=(0,J.useState)(!1),[B,te]=(0,J.useState)(``),[V,re]=(0,J.useState)(``),[U,ae]=(0,J.useState)(!1),[oe,K]=(0,J.useState)(null),{activeProjectId:q}=ie(),{minimize:se}=T(),X=(0,J.useRef)({refineId:null}),le=(0,J.useRef)(S);le.current=S;let Z=(0,J.useCallback)(()=>{let e=le.current;e.kind!==`open`||!e.projectId||se({kind:`ai-edit`,projectId:e.projectId,label:`AI Edit · ${e.agentId}`,restoreRoute:`/agents`,params:{agentId:e.agentId,baseBody:e.baseBody,resumeRefineId:X.current.refineId??e.resumeRefineId}})},[se]);c(`ai-edit`,q,e=>{e.kind===`ai-edit`&&(Z(),X.current={refineId:e.params.resumeRefineId??null},w({kind:`open`,projectId:e.projectId,agentId:e.params.agentId,baseBody:e.params.baseBody,resumeRefineId:e.params.resumeRefineId}))});let ue=(0,J.useCallback)(()=>{let e=!1;return l(!0),fetch(`${W()}/profiles/catalog`).then(e=>{if(!e.ok)throw Error(`Catalog load failed: ${e.status}`);return e.json()}).then(i=>{e||(t(i.agents),i.agents.length>0&&!n&&r(i.agents[0].id))}).catch(t=>{e||_(t.message)}).finally(()=>{e||l(!1)}),()=>{e=!0}},[n]);(0,J.useEffect)(()=>ue(),[ue]);let de=(0,J.useCallback)(e=>{let t=!1;return h(!0),o(null),fetch(`${W()}/profiles/catalog/${encodeURIComponent(e)}`).then(e=>{if(!e.ok)throw Error(`Agent body load failed: ${e.status}`);return e.json()}).then(e=>{t||o(e.body)}).catch(e=>{t||_(e.message)}).finally(()=>{t||h(!1)}),()=>{t=!0}},[]);if((0,J.useEffect)(()=>{if(n)return de(n)},[n,de]),s)return(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Loading catalog…`})});if(v.kind!==`closed`)return(0,Y.jsx)(Se,{agentId:v.kind===`edit`?v.agentId:void 0,initialBody:v.kind===`duplicate`||v.kind===`create`?v.initialBody:void 0,initialName:v.kind===`create`?v.initialName:void 0,draftFromRefine:v.kind===`edit`?v.draftFromRefine:void 0,onClose:()=>b({kind:`closed`}),onSaved:e=>{r(e),b({kind:`closed`}),ue()},onResumeRefine:(e,t,n)=>{b({kind:`closed`}),q&&(Z(),X.current={refineId:e},w({kind:`open`,projectId:q,agentId:t,baseBody:n,resumeRefineId:e}))}});if(S.kind===`open`)return(0,Y.jsx)(Xe,{agentId:S.agentId,baseBody:S.baseBody,resumeRefineId:S.resumeRefineId,onStateChange:e=>{X.current=e},onClose:()=>w({kind:`closed`}),onMinimize:e=>{let t=S.kind===`open`?S.projectId:q;if(!t){w({kind:`closed`});return}let n=S.kind===`open`?S.agentId:``,r=S.kind===`open`?S.baseBody:``;se({kind:`ai-edit`,projectId:t,label:`AI Edit · ${n}`,restoreRoute:`/agents`,params:{agentId:n,baseBody:r,resumeRefineId:e??void 0}}),w({kind:`closed`})},onApplied:e=>{w({kind:`closed`}),r(e),ue()},onOpenInStudio:(e,t)=>{w({kind:`closed`}),b({kind:`edit`,agentId:S.agentId,draftFromRefine:e})}},`${S.agentId}:${S.resumeRefineId??`fresh`}`);let fe=(()=>{let e=M.trim().toLowerCase();return ye.filter(t=>!(P!==`all`&&t.category!==P||L&&!t.tags.includes(L)||e&&!(t.label+` `+t.blurb+` `+t.tags.join(` `)+` `+t.category).toLowerCase().includes(e)))})(),pe=(()=>{let e=new Map;for(let t of ye)e.set(t.category,(e.get(t.category)??0)+1);return e})(),me=()=>{O(!1),N(``),F(`all`),R(null)},he=()=>(0,Y.jsx)(G,{open:E,onOpenChange:e=>{e||me()},children:(0,Y.jsxs)(j,{className:`max-w-4xl p-0 h-[85vh] flex flex-col overflow-hidden`,children:[(0,Y.jsxs)(`div`,{className:`flex-shrink-0 px-6 pt-5 pb-3 border-b border-border`,children:[(0,Y.jsxs)(a,{children:[(0,Y.jsxs)(H,{className:`flex items-center gap-2 text-base`,children:[(0,Y.jsx)(k,{className:`w-4 h-4 text-accent-primary`}),` Agent template library`,(0,Y.jsxs)(`span`,{className:`text-[11px] font-normal text-muted-foreground ml-1`,children:[fe.length,` of `,ye.length]})]}),(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground mt-1`,children:`Start from a curated template across engineering, product, science, health, legal, and more. Pick one and open it in the Studio for review and editing before saving.`})]}),(0,Y.jsxs)(`div`,{className:`mt-3 relative`,children:[(0,Y.jsx)(p,{className:`w-3.5 h-3.5 absolute left-2.5 top-1/2 -translate-y-1/2 text-muted-foreground pointer-events-none`}),(0,Y.jsx)(`input`,{value:M,onChange:e=>N(e.target.value),placeholder:`Search by name, description, tag, or category…`,className:`w-full h-9 pl-8 pr-8 text-sm rounded-md border border-border bg-background focus:outline-none focus:ring-1 focus:ring-primary/40`,autoFocus:!0}),M&&(0,Y.jsx)(`button`,{type:`button`,onClick:()=>N(``),className:`absolute right-2 top-1/2 -translate-y-1/2 p-0.5 rounded hover:bg-accent text-muted-foreground`,children:(0,Y.jsx)(I,{className:`w-3.5 h-3.5`})})]}),(0,Y.jsxs)(`div`,{className:`flex gap-1.5 overflow-x-auto mt-3 pb-1 scrollbar-thin`,children:[(0,Y.jsx)(et,{label:`All`,count:ye.length,active:P===`all`,onClick:()=>F(`all`)}),be.map(e=>(0,Y.jsx)(et,{label:e,count:pe.get(e)??0,active:P===e,onClick:()=>F(e)},e))]}),L&&(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 mt-2`,children:[(0,Y.jsx)(`span`,{className:`text-[11px] text-muted-foreground`,children:`Tag filter:`}),(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>R(null),className:`inline-flex items-center gap-1 text-[11px] px-2 py-0.5 rounded-full bg-accent-primary/20 text-accent-primary hover:bg-accent-primary/30`,children:[(0,Y.jsx)(x,{className:`w-3 h-3`}),` `,L,(0,Y.jsx)(I,{className:`w-3 h-3`})]})]})]}),(0,Y.jsx)(`div`,{className:`flex-1 overflow-y-auto px-6 py-4`,children:fe.length===0?(0,Y.jsx)(`div`,{className:`h-full flex items-center justify-center text-center`,children:(0,Y.jsxs)(`div`,{className:`max-w-sm`,children:[(0,Y.jsx)(`div`,{className:`text-sm text-muted-foreground`,children:`No templates match your filters.`}),(0,Y.jsx)(`button`,{type:`button`,onClick:()=>{N(``),F(`all`),R(null)},className:`text-xs text-accent-primary hover:underline mt-2`,children:`Clear filters`})]})}):(0,Y.jsx)(`div`,{className:`grid grid-cols-1 md:grid-cols-2 gap-3`,children:fe.map(e=>(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>{me(),b({kind:`create`,initialBody:e.body,initialName:e.nameHint})},className:`group text-left p-4 rounded-lg border border-border bg-card/40 hover:border-accent-primary/50 hover:bg-accent/40 hover:shadow-sm transition-all`,children:[(0,Y.jsxs)(`div`,{className:`flex items-start gap-3 mb-2`,children:[(0,Y.jsx)(`span`,{className:`text-2xl leading-none mt-0.5`,children:e.emoji}),(0,Y.jsxs)(`div`,{className:`min-w-0 flex-1`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-semibold text-foreground group-hover:text-accent-primary truncate`,children:e.label}),(0,Y.jsx)(`div`,{className:`text-[10px] text-muted-foreground mt-0.5`,children:e.category})]})]}),(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground leading-relaxed line-clamp-2 min-h-[2lh]`,children:e.blurb}),(0,Y.jsx)(`div`,{className:`flex flex-wrap gap-1 mt-3`,children:e.tags.slice(0,5).map(e=>(0,Y.jsx)(`span`,{role:`button`,tabIndex:0,onClick:t=>{t.stopPropagation(),R(e)},onKeyDown:t=>{(t.key===`Enter`||t.key===` `)&&(t.preventDefault(),t.stopPropagation(),R(e))},className:`text-[10px] px-1.5 py-0.5 rounded bg-muted/50 text-muted-foreground hover:bg-accent-primary/20 hover:text-accent-primary cursor-pointer transition-colors `+(L===e?`bg-accent-primary/20 text-accent-primary`:``),children:e},e))}),(0,Y.jsxs)(`div`,{className:`mt-3 pt-2 border-t border-border/40 flex items-center justify-between`,children:[(0,Y.jsx)(`code`,{className:`text-[10px] font-mono text-muted-foreground/70 truncate`,children:e.nameHint}),(0,Y.jsx)(`span`,{className:`text-[10px] text-accent-primary opacity-0 group-hover:opacity-100 transition-opacity`,children:`Open in Studio →`})]})]},e.id))})}),(0,Y.jsxs)(`div`,{className:`flex-shrink-0 px-6 py-3 border-t border-border flex items-center justify-between`,children:[(0,Y.jsx)(`span`,{className:`text-[11px] text-muted-foreground`,children:`Tip: click a tag pill on any card to filter by that tag.`}),(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:me,children:`Cancel`})]})]})}),ge=()=>(0,Y.jsx)(G,{open:ee,onOpenChange:e=>{!e&&!U&&(z(!1),K(null))},children:(0,Y.jsxs)(j,{className:`max-w-lg`,children:[(0,Y.jsx)(a,{children:(0,Y.jsxs)(H,{className:`flex items-center gap-2`,children:[(0,Y.jsx)(u,{className:`w-4 h-4 text-accent-primary`}),` Generate a custom agent`]})}),(0,Y.jsxs)(`div`,{className:`space-y-3 py-2`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Agent id`}),(0,Y.jsx)(A,{value:B,onChange:e=>te(e.target.value),placeholder:`custom-my-agent`,className:`text-sm font-mono`,disabled:U}),(0,Y.jsxs)(`p`,{className:`text-[11px] text-muted-foreground mt-1`,children:[`Must start with `,(0,Y.jsx)(`code`,{children:`custom-`}),`.`]})]}),(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Describe what this agent should do`}),(0,Y.jsx)(`textarea`,{value:V,onChange:e=>re(e.target.value),placeholder:`e.g. Review Terraform/IaC changes and flag security misconfigurations, excessive IAM permissions, and public S3 buckets before merging. Conservative and terse.`,className:`w-full text-sm p-2 rounded border border-border bg-background min-h-[120px] resize-y`,disabled:U})]}),oe&&(0,Y.jsx)(`div`,{className:`px-3 py-2 text-xs rounded border border-red-500/30 bg-red-500/10 text-red-400`,children:oe}),(0,Y.jsxs)(`p`,{className:`text-[11px] text-muted-foreground`,children:[`Claude will draft a full `,(0,Y.jsx)(`code`,{children:`.md`}),` body. You'll review it in the Studio and can edit before saving. This spawns a one-shot claude invocation and can take up to 90 seconds.`]})]}),(0,Y.jsxs)(C,{children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:()=>z(!1),disabled:U,children:`Cancel`}),(0,Y.jsx)(y,{size:`sm`,onClick:_e,disabled:U||!B.trim()||!V.trim(),children:U?(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(d,{className:`w-3.5 h-3.5 mr-1.5 animate-spin`}),` Generating…`]}):(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(u,{className:`w-3.5 h-3.5 mr-1.5`}),` Generate`]})})]})]})}),_e=async()=>{ae(!0),K(null);try{let e=await fetch(`${W()}/profiles/catalog/generate`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({name:B.trim(),description:V.trim()})});if(!e.ok){let t=await e.json().catch(()=>({}));throw Error(t.error??`Generate failed: ${e.status}`)}let t=await e.json();z(!1),b({kind:`create`,initialBody:t.draft,initialName:B.trim()})}catch(e){K(e.message)}finally{ae(!1)}},ve=async e=>{try{let t=await fetch(`${W()}/profiles/catalog/${encodeURIComponent(e)}`);if(!t.ok)throw Error(`Load failed: ${t.status}`);b({kind:`duplicate`,from:e,initialBody:(await t.json()).body})}catch(e){_(e.message)}};if(e.length===0)return(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsxs)(`div`,{className:`text-center max-w-sm`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-medium text-foreground`,children:`No agents installed`}),(0,Y.jsxs)(`div`,{className:`text-xs text-muted-foreground mt-1 mb-4`,children:[`Run `,(0,Y.jsx)(`code`,{className:`text-foreground`,children:`npx specrails-core@latest update`}),` in this project to install the upstream agents, or create a custom agent now.`]}),(0,Y.jsxs)(`div`,{className:`flex gap-2 justify-center flex-wrap`,children:[(0,Y.jsxs)(y,{size:`sm`,onClick:()=>z(!0),children:[(0,Y.jsx)(u,{className:`w-3.5 h-3.5 mr-1.5`}),` Generate with Claude`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>O(!0),children:[(0,Y.jsx)(k,{className:`w-3.5 h-3.5 mr-1.5`}),` Template`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>b({kind:`create`}),children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1.5`}),` Blank`]})]})]})}),he(),ge()]});let Q=e.filter(e=>e.kind===`upstream`),xe=e.filter(e=>e.kind===`custom`),$=e.find(e=>e.id===n);return(0,Y.jsxs)(Y.Fragment,{children:[he(),ge(),(0,Y.jsxs)(`div`,{className:`flex h-full`,children:[(0,Y.jsxs)(`aside`,{className:`w-72 flex-shrink-0 border-r border-border flex flex-col`,children:[(0,Y.jsxs)(`div`,{className:`px-3 py-2 border-b border-border`,children:[(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground uppercase tracking-wide mb-1.5`,children:`Catalog`}),(0,Y.jsx)(`div`,{className:`text-[10px] text-muted-foreground/70 mb-2`,children:`Create a custom agent:`}),(0,Y.jsxs)(`div`,{className:`flex gap-1`,children:[(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,className:`flex-1 h-7 text-[11px] px-2`,onClick:()=>O(!0),title:`Start from a template`,children:[(0,Y.jsx)(k,{className:`w-3 h-3 mr-1`}),` Template`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,className:`flex-1 h-7 text-[11px] px-2`,onClick:()=>z(!0),title:`Generate with Claude`,children:[(0,Y.jsx)(u,{className:`w-3 h-3 mr-1`}),` Generate`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,className:`flex-1 h-7 text-[11px] px-2`,onClick:()=>b({kind:`create`}),title:`Start from a blank template`,children:[(0,Y.jsx)(f,{className:`w-3 h-3 mr-1`}),` Blank`]})]})]}),(0,Y.jsxs)(`div`,{className:`flex-1 overflow-auto p-2`,children:[(0,Y.jsx)(tt,{title:`Upstream (${Q.length})`,description:`Shipped by specrails-core; read-only.`,children:Q.map(e=>(0,Y.jsx)(nt,{agent:e,selected:e.id===n,onSelect:()=>r(e.id)},e.id))}),(0,Y.jsx)(tt,{title:`Custom (${xe.length})`,description:`Your project's custom-*.md files. Use Template / Generate / Blank above to create one.`,children:xe.length===0?(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground px-2 py-2 italic`,children:`No custom agents yet.`}):xe.map(e=>(0,Y.jsx)(nt,{agent:e,selected:e.id===n,onSelect:()=>r(e.id)},e.id))})]})]}),(0,Y.jsxs)(`main`,{className:`flex-1 min-w-0 overflow-auto`,children:[g&&(0,Y.jsx)(`div`,{className:`m-4 px-3 py-2 text-xs rounded border border-red-500/30 bg-red-500/10 text-red-400`,children:g}),$&&(0,Y.jsxs)(`div`,{className:`p-6 min-w-0`,children:[(0,Y.jsxs)(`div`,{className:`flex items-start justify-between gap-4 mb-1`,children:[(0,Y.jsxs)(`div`,{className:`min-w-0 flex-1`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 flex-wrap`,children:[(0,Y.jsx)(`h2`,{className:`text-base font-mono font-semibold`,children:$.id}),(0,Y.jsx)(rt,{kind:$.kind}),$.model&&(0,Y.jsxs)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground`,children:[`default model: `,$.model]})]}),$.description&&(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground mt-1 break-words`,children:$.description})]}),(0,Y.jsxs)(`div`,{className:`flex gap-2 flex-shrink-0`,children:[(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>void ve($.id),children:[(0,Y.jsx)(ne,{className:`w-3.5 h-3.5 mr-1`}),` Duplicate`]}),$.kind===`custom`&&(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>b({kind:`edit`,agentId:$.id}),children:[(0,Y.jsx)(D,{className:`w-3.5 h-3.5 mr-1`}),` Edit`]}),(0,Y.jsxs)(y,{size:`sm`,onClick:async()=>{if(q)try{let e=await fetch(`${W()}/profiles/catalog/${encodeURIComponent($.id)}`);if(!e.ok)throw Error(`Load failed: ${e.status}`);let t=await e.json();Z(),X.current={refineId:null},w({kind:`open`,projectId:q,agentId:$.id,baseBody:t.body})}catch(e){_(e.message)}},title:`Iteratively refine this agent with AI`,children:[(0,Y.jsx)(ce,{className:`w-3.5 h-3.5 mr-1`}),` AI Edit`]})]})]})]}),(0,Y.jsxs)(`div`,{className:`rounded-md border border-border bg-muted/30 mt-4 min-w-0 overflow-hidden`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between px-3 py-1.5 border-b border-border`,children:[(0,Y.jsxs)(`span`,{className:`text-[11px] font-mono text-muted-foreground truncate`,children:[`.claude/agents/`,$.id,`.md`]}),(0,Y.jsx)(`span`,{className:`text-[10px] text-muted-foreground flex-shrink-0 ml-2`,children:$.kind===`upstream`?`read-only`:`editable — use Edit above`})]}),(0,Y.jsx)(`pre`,{className:`p-4 text-xs font-mono overflow-auto max-h-[60vh] whitespace-pre-wrap break-all`,children:m?`Loading…`:i??``})]})]})]})]})]})}function et({label:e,count:t,active:n,onClick:r}){return(0,Y.jsxs)(`button`,{type:`button`,onClick:r,className:`flex-shrink-0 inline-flex items-center gap-1 h-7 px-2.5 text-[11px] rounded-full border transition-colors whitespace-nowrap `+(n?`bg-accent-primary/20 border-accent-primary/50 text-accent-primary`:`bg-transparent border-border text-muted-foreground hover:bg-accent/50 hover:text-foreground`),children:[e,(0,Y.jsx)(`span`,{className:`text-[9px] px-1 rounded `+(n?`bg-accent-primary/30`:`bg-muted/60`),children:t})]})}function tt({title:e,description:t,children:n}){return(0,Y.jsxs)(`div`,{className:`mb-4`,children:[(0,Y.jsxs)(`div`,{className:`px-2 pb-1`,children:[(0,Y.jsx)(`div`,{className:`text-[11px] font-medium text-muted-foreground uppercase tracking-wide`,children:e}),t&&(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground/70 mt-0.5`,children:t})]}),(0,Y.jsx)(`div`,{className:`space-y-0.5`,children:n})]})}function nt({agent:e,selected:t,onSelect:n}){return(0,Y.jsxs)(`button`,{type:`button`,onClick:n,className:`w-full flex items-center justify-between gap-2 px-2 py-1.5 text-left rounded transition-colors `+(t?`bg-accent text-foreground`:`text-muted-foreground hover:bg-accent/50 hover:text-foreground`),children:[(0,Y.jsx)(`span`,{className:`text-sm font-mono truncate`,children:e.id}),(0,Y.jsx)(rt,{kind:e.kind})]})}function rt({kind:e}){return(0,Y.jsx)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded flex-shrink-0 `+(e===`custom`?`bg-purple-500/15 text-purple-400`:`bg-muted text-muted-foreground`),children:e})}var it=[{label:`7d`,days:7},{label:`30d`,days:30},{label:`90d`,days:90}];function at(){let[e,t]=(0,J.useState)(null),[n,r]=(0,J.useState)(30),[i,a]=(0,J.useState)(!0);if((0,J.useEffect)(()=>{let e=!1;return a(!0),fetch(`${W()}/profiles/analytics?windowDays=${n}`).then(e=>e.ok?e.json():null).then(n=>{!e&&n&&t(n)}).catch(()=>{}).finally(()=>{e||a(!1)}),()=>{e=!0}},[n]),i&&!e)return null;if(!e||e.rows.length===0)return(0,Y.jsxs)(`div`,{className:`mx-6 my-4 rounded-md border border-border bg-muted/20`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-2 border-b border-border`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`div`,{className:`text-xs font-medium text-foreground`,children:`Profile usage`}),(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground`,children:`Jobs launched per profile in the selected window.`})]}),(0,Y.jsx)(`div`,{className:`flex gap-0.5`,children:it.map(e=>(0,Y.jsx)(`button`,{type:`button`,onClick:()=>r(e.days),className:`text-[10px] px-2 py-1 rounded transition-colors `+(n===e.days?`bg-accent text-foreground`:`text-muted-foreground hover:bg-accent/50 hover:text-foreground`),children:e.label},e.days))})]}),(0,Y.jsx)(`div`,{className:`px-4 py-8 text-xs text-muted-foreground`,children:`No profile-scoped jobs yet.`})]});let o=Math.max(...e.rows.map(e=>e.jobs),1);return(0,Y.jsxs)(`div`,{className:`mx-6 my-4 rounded-md border border-border bg-muted/20`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-2 border-b border-border`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`div`,{className:`text-xs font-medium text-foreground`,children:`Profile usage`}),(0,Y.jsxs)(`div`,{className:`text-[11px] text-muted-foreground`,children:[`Jobs launched per profile in the last `,e.windowDays,` days.`]})]}),(0,Y.jsx)(`div`,{className:`flex gap-0.5`,children:it.map(e=>(0,Y.jsx)(`button`,{type:`button`,onClick:()=>r(e.days),className:`text-[10px] px-2 py-1 rounded transition-colors `+(n===e.days?`bg-accent text-foreground`:`text-muted-foreground hover:bg-accent/50 hover:text-foreground`),children:e.label},e.days))})]}),(0,Y.jsx)(`div`,{className:`p-4 space-y-2`,children:e.rows.map(e=>(0,Y.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Y.jsx)(`div`,{className:`w-32 text-xs font-mono truncate`,children:e.profileName}),(0,Y.jsx)(`div`,{className:`flex-1 h-4 bg-muted/50 rounded overflow-hidden`,children:(0,Y.jsx)(`div`,{className:`h-full bg-accent-primary/70`,style:{width:`${Math.max(6,Math.round(e.jobs/o*100))}%`}})}),(0,Y.jsxs)(`div`,{className:`flex gap-4 text-[11px] text-muted-foreground min-w-0 flex-shrink-0`,children:[(0,Y.jsxs)(`span`,{title:`jobs`,children:[(0,Y.jsx)(`span`,{className:`text-foreground font-mono`,children:e.jobs}),` jobs`]}),(0,Y.jsxs)(`span`,{title:`success rate`,children:[(0,Y.jsxs)(`span`,{className:`text-foreground font-mono`,children:[Math.round(e.successRate*100),`%`]}),` `,`ok`]}),e.avgDurationMs!=null&&(0,Y.jsx)(`span`,{title:`avg duration`,children:(0,Y.jsx)(`span`,{className:`text-foreground font-mono`,children:ot(e.avgDurationMs)})}),e.avgTokens!=null&&(0,Y.jsxs)(`span`,{title:`avg tokens`,children:[(0,Y.jsx)(`span`,{className:`text-foreground font-mono`,children:st(e.avgTokens)}),` `,`tok`]})]})]},e.profileName))})]})}function ot(e){let t=Math.round(e/1e3);if(t<60)return`${t}s`;let n=Math.floor(t/60);return n<60?`${n}m`:`${Math.floor(n/60)}h${n%60}m`}function st(e){return e<1e3?Math.round(e).toString():e<1e6?`${(e/1e3).toFixed(1)}k`:`${(e/1e6).toFixed(1)}M`}var ct=`specrails-hub:agents-tab`;function lt(){try{let e=localStorage.getItem(ct);if(e===`profiles`||e===`usage`||e===`catalog`)return e}catch{}return`profiles`}function ut(){let[e,t]=(0,J.useState)(()=>lt()),n=e=>{t(e);try{localStorage.setItem(ct,e)}catch{}},[r,i]=(0,J.useState)(null),{activeProjectId:a}=ie(),{pendingRestores:o}=T(),s=(0,J.useRef)(!1);return(0,J.useEffect)(()=>{if(!a)return;let t=o.some(e=>e.kind===`ai-edit`&&e.projectId===a);t&&!s.current&&e!==`catalog`&&n(`catalog`),s.current=t},[o,a,e]),(0,J.useEffect)(()=>{let e=!1;return fetch(`${W()}/profiles/core-version`).then(e=>e.ok?e.json():null).then(t=>{!e&&t&&i(t)}).catch(()=>{}),()=>{e=!0}},[]),(0,Y.jsxs)(`div`,{className:`flex flex-col h-full`,children:[r!==null&&!r.profileAware&&(0,Y.jsxs)(`div`,{className:`flex-shrink-0 flex items-start gap-3 px-6 py-3 border-b border-yellow-500/30 bg-yellow-500/10`,children:[(0,Y.jsx)(h,{className:`w-4 h-4 text-yellow-500 mt-0.5 flex-shrink-0`}),(0,Y.jsxs)(`div`,{className:`text-xs`,children:[(0,Y.jsxs)(`div`,{className:`font-medium text-yellow-500`,children:[`Profile-aware pipeline requires specrails-core `,r.required,`+`]}),(0,Y.jsxs)(`div`,{className:`text-yellow-500/80 mt-0.5`,children:[`This project has`,` `,(0,Y.jsx)(`code`,{className:`px-1 rounded bg-yellow-500/20`,children:r.version??`unknown`}),`. Run`,` `,(0,Y.jsx)(`code`,{className:`px-1 rounded bg-yellow-500/20`,children:`npx specrails-core@latest update`}),` `,`inside the project to unlock profile mode. Profiles you create here will still save; they just won't affect the pipeline until core is updated.`]})]})]}),(0,Y.jsxs)(`div`,{className:`flex-shrink-0 border-b border-border`,children:[(0,Y.jsxs)(`div`,{className:`px-6 pt-4`,children:[(0,Y.jsx)(`h1`,{className:`text-lg font-semibold`,children:`Agents`}),(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground mt-0.5`,children:`Manage agent profiles and the catalog (upstream + custom) for this project.`})]}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-1 px-4 pt-3`,children:[(0,Y.jsx)(ft,{active:e===`profiles`,onClick:()=>n(`profiles`),children:`Profiles`}),(0,Y.jsx)(ft,{active:e===`usage`,onClick:()=>n(`usage`),children:`Usage`}),(0,Y.jsx)(ft,{active:e===`catalog`,onClick:()=>n(`catalog`),children:`Catalog`})]})]}),(0,Y.jsxs)(`div`,{className:`flex-1 min-w-0 overflow-auto`,children:[e===`profiles`&&(0,Y.jsx)(ve,{}),e===`usage`&&(0,Y.jsx)(dt,{}),e===`catalog`&&(0,Y.jsx)($e,{})]})]})}function dt(){return(0,Y.jsx)(`div`,{className:`min-h-full`,children:(0,Y.jsx)(at,{})})}function ft({active:e,children:t,onClick:n}){return(0,Y.jsx)(`button`,{type:`button`,onClick:n,className:`h-8 px-3 text-xs font-medium rounded-t-md border-b-2 transition-colors `+(e?`text-foreground border-foreground`:`text-muted-foreground border-transparent hover:text-foreground hover:border-border`),children:t})}export{ut as default};
86
+ `});return i}function qe({value:e,onChange:t,onSubmit:n,disabled:r,placeholder:i,autoFocus:a,inputRef:o}){return(0,Y.jsxs)(`div`,{className:`space-y-2`,children:[(0,Y.jsxs)(`div`,{className:`relative rounded-xl border border-border bg-card/40 focus-within:border-accent-primary/40 focus-within:ring-2 focus-within:ring-accent-primary/20 transition-all`,children:[(0,Y.jsx)(`textarea`,{ref:o,value:e,onChange:e=>t(e.target.value),placeholder:i,rows:3,disabled:r,autoFocus:a,className:`w-full text-sm p-3 pr-14 bg-transparent resize-none focus:outline-none disabled:opacity-50 placeholder:text-muted-foreground/60`,onKeyDown:e=>{e.key===`Enter`&&(e.metaKey||e.ctrlKey)&&(e.preventDefault(),n())}}),(0,Y.jsx)(`button`,{type:`button`,onClick:n,disabled:!e.trim()||r,"aria-label":`Send (⌘⏎)`,className:`absolute bottom-2.5 right-2.5 p-2 rounded-md bg-accent-primary text-white hover:bg-accent-primary/90 disabled:opacity-30 disabled:cursor-not-allowed focus-visible:ring-2 focus-visible:ring-primary/40 focus-visible:outline-none transition-opacity`,children:r?(0,Y.jsx)(d,{className:`w-3.5 h-3.5 animate-spin`}):(0,Y.jsx)(Je,{})})]}),(0,Y.jsx)(`div`,{className:`flex items-center justify-end text-[11px] text-muted-foreground/70`,children:(0,Y.jsxs)(`span`,{children:[(0,Y.jsx)(`kbd`,{className:`px-1.5 py-0.5 rounded bg-muted/60 text-[10px] font-mono`,children:`⌘⏎`}),` submit`,(0,Y.jsx)(`span`,{className:`mx-1.5`,children:`·`}),(0,Y.jsx)(`kbd`,{className:`px-1.5 py-0.5 rounded bg-muted/60 text-[10px] font-mono`,children:`Esc`}),` cancel`]})})]})}function Je(){return(0,Y.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,strokeLinecap:`round`,strokeLinejoin:`round`,className:`w-3.5 h-3.5`,children:[(0,Y.jsx)(`path`,{d:`M22 2 11 13`}),(0,Y.jsx)(`path`,{d:`M22 2 15 22l-4-9-9-4Z`})]})}var Ye=[`Tighten the tool list`,`Make the personality stricter`,`Add a Workflow protocol section`,`Shorten and sharpen the description`,`Match the sr-developer style`];function Xe({agentId:e,baseBody:n,resumeRefineId:r,onClose:i,onMinimize:a,onStateChange:o,onOpenInStudio:s,onApplied:c}){let{activeProjectId:l}=ae(),u=De(l),[d,f]=(0,J.useState)(``),p=(0,J.useRef)(null),m=(0,J.useRef)(!1);(0,J.useEffect)(()=>{m.current||(m.current=!0,r?u.rehydrate(r,e):u.open(e,n))},[e,n,r,u]),(0,J.useEffect)(()=>{let e=setTimeout(()=>p.current?.focus(),50);return()=>clearTimeout(e)},[]);let h=(0,J.useRef)(o);h.current=o,(0,J.useEffect)(()=>{h.current?.({refineId:u.state.refineId??null})},[u.state.refineId]);let g=(0,J.useRef)(null);(0,J.useEffect)(()=>{u.state.uiState===`applied`&&u.state.appliedVersion!==null&&u.state.appliedVersion!==g.current&&(g.current=u.state.appliedVersion,c&&u.state.agentId&&c(u.state.agentId,u.state.appliedVersion))},[u.state.uiState,u.state.appliedVersion,u.state.agentId,c]);let _=(0,J.useCallback)(async e=>{let t=e.trim();t&&(f(``),u.state.refineId?await u.sendTurn(t):await u.start(t))},[u]),v=(0,J.useCallback)(()=>{_(d)},[d,_]),y=(0,J.useCallback)(async()=>{u.state.refineId&&(u.state.uiState===`streaming`||u.state.uiState===`reviewing`)&&await u.cancel(),i()},[u,i]),b=Ze(u.state),x=(0,J.useMemo)(()=>!u.state.draftBody||!u.state.baseBody?null:(0,Y.jsx)(Ue,{hunks:Ge(u.state.baseBody,u.state.draftBody)}),[u.state.baseBody,u.state.draftBody]);return(0,Y.jsx)(ke,{uiPhase:b,errorMessage:u.state.errorMessage,applyConflict:u.state.applyConflict,eyebrow:`AI Edit`,targetLabel:e,targetLabelMono:!0,headline:`Refine your agent`,streamingHeadline:`Refining your agent…`,description:Qe(n),chips:Ye,onChipSubmit:e=>void _(e),composer:(0,Y.jsx)(qe,{value:d,onChange:f,onSubmit:v,disabled:b===`streaming`||b===`applied`,placeholder:u.state.refineId?`Send a follow-up refinement…`:`Describe how to refine this agent…`,inputRef:p}),streamingText:u.state.streamingText,history:u.state.history,diff:x,diffHeaderLabel:`.claude/agents/${e}.md`,baseBody:n,baseBodyDisclosureLabel:`View current agent body`,appliedNotice:u.state.appliedVersion===null?void 0:(0,Y.jsxs)(`div`,{className:`rounded-md border border-green-500/40 bg-green-500/10 p-3 text-xs text-green-300`,children:[`Applied as version `,u.state.appliedVersion,`.`]}),canApply:u.state.uiState===`reviewing`&&!!u.state.draftBody&&!u.state.applyConflict,onApply:()=>void u.apply(),onForceApply:()=>void u.apply(!0),onDiscard:()=>void y(),onClose:()=>void y(),onMinimize:a?()=>a(u.state.refineId??null):void 0,secondaryAction:s&&u.state.draftBody&&u.state.refineId?{label:`Open draft in Studio for manual editing`,icon:(0,Y.jsx)(t,{className:`w-3 h-3`}),onClick:()=>{u.state.refineId&&u.state.draftBody&&s&&s(u.state.refineId,u.state.draftBody)}}:void 0})}function Ze(e){switch(e.uiState){case`streaming`:return`streaming`;case`reviewing`:return`reviewing`;case`applied`:return`applied`;case`error`:return`error`;default:return`composing`}}function Qe(e){let t=e.match(/^---\r?\n([\s\S]*?)\r?\n---/);if(!t)return null;let n=t[1].match(/^description:\s*([\s\S]*?)(?=^[a-z_]+:\s|\Z)/m);if(!n)return null;let r=n[1].trim();return(r.startsWith(`"`)&&r.endsWith(`"`)||r.startsWith(`'`)&&r.endsWith(`'`))&&(r=r.slice(1,-1)),r=r.replace(/\\n/g,` `).replace(/\\t/g,` `).replace(/\\"/g,`"`).replace(/\\'/g,`'`).replace(/\\\\/g,`\\`).replace(/\s+/g,` `).trim(),r.length>240&&(r=r.slice(0,237)+`…`),r||null}function $e(){let[e,t]=(0,J.useState)([]),[n,r]=(0,J.useState)(null),[a,o]=(0,J.useState)(null),[c,u]=(0,J.useState)(!0),[m,h]=(0,J.useState)(!1),[g,_]=(0,J.useState)(null),[v,b]=(0,J.useState)({kind:`closed`}),[S,E]=(0,J.useState)({kind:`closed`}),[O,k]=(0,J.useState)(!1),[j,P]=(0,J.useState)(``),[F,I]=(0,J.useState)(`all`),[L,z]=(0,J.useState)(null),[ee,B]=(0,J.useState)(!1),[V,te]=(0,J.useState)(``),[H,U]=(0,J.useState)(``),[G,re]=(0,J.useState)(!1),[ie,oe]=(0,J.useState)(null),{activeProjectId:q}=ae(),{minimize:se}=T(),X=(0,J.useRef)({refineId:null}),le=(0,J.useRef)(S);le.current=S;let Z=(0,J.useCallback)(()=>{let e=le.current;e.kind!==`open`||!e.projectId||se({kind:`ai-edit`,projectId:e.projectId,label:`AI Edit · ${e.agentId}`,restoreRoute:`/agents`,params:{agentId:e.agentId,baseBody:e.baseBody,resumeRefineId:X.current.refineId??e.resumeRefineId}})},[se]);s(`ai-edit`,q,e=>{e.kind===`ai-edit`&&(Z(),X.current={refineId:e.params.resumeRefineId??null},E({kind:`open`,projectId:e.projectId,agentId:e.params.agentId,baseBody:e.params.baseBody,resumeRefineId:e.params.resumeRefineId}))});let ue=(0,J.useCallback)(()=>{let e=!1;return u(!0),fetch(`${w()}/profiles/catalog`).then(e=>{if(!e.ok)throw Error(`Catalog load failed: ${e.status}`);return e.json()}).then(i=>{e||(t(i.agents),i.agents.length>0&&!n&&r(i.agents[0].id))}).catch(t=>{e||_(t.message)}).finally(()=>{e||u(!1)}),()=>{e=!0}},[n]);(0,J.useEffect)(()=>ue(),[ue]);let de=(0,J.useCallback)(e=>{let t=!1;return h(!0),o(null),fetch(`${w()}/profiles/catalog/${encodeURIComponent(e)}`).then(e=>{if(!e.ok)throw Error(`Agent body load failed: ${e.status}`);return e.json()}).then(e=>{t||o(e.body)}).catch(e=>{t||_(e.message)}).finally(()=>{t||h(!1)}),()=>{t=!0}},[]);if((0,J.useEffect)(()=>{if(n)return de(n)},[n,de]),c)return(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Loading catalog…`})});if(v.kind!==`closed`)return(0,Y.jsx)(Se,{agentId:v.kind===`edit`?v.agentId:void 0,initialBody:v.kind===`duplicate`||v.kind===`create`?v.initialBody:void 0,initialName:v.kind===`create`?v.initialName:void 0,draftFromRefine:v.kind===`edit`?v.draftFromRefine:void 0,onClose:()=>b({kind:`closed`}),onSaved:e=>{r(e),b({kind:`closed`}),ue()},onResumeRefine:(e,t,n)=>{b({kind:`closed`}),q&&(Z(),X.current={refineId:e},E({kind:`open`,projectId:q,agentId:t,baseBody:n,resumeRefineId:e}))}});if(S.kind===`open`)return(0,Y.jsx)(Xe,{agentId:S.agentId,baseBody:S.baseBody,resumeRefineId:S.resumeRefineId,onStateChange:e=>{X.current=e},onClose:()=>E({kind:`closed`}),onMinimize:e=>{let t=S.kind===`open`?S.projectId:q;if(!t){E({kind:`closed`});return}let n=S.kind===`open`?S.agentId:``,r=S.kind===`open`?S.baseBody:``;se({kind:`ai-edit`,projectId:t,label:`AI Edit · ${n}`,restoreRoute:`/agents`,params:{agentId:n,baseBody:r,resumeRefineId:e??void 0}}),E({kind:`closed`})},onApplied:e=>{E({kind:`closed`}),r(e),ue()},onOpenInStudio:(e,t)=>{E({kind:`closed`}),b({kind:`edit`,agentId:S.agentId,draftFromRefine:e})}},`${S.agentId}:${S.resumeRefineId??`fresh`}`);let fe=(()=>{let e=j.trim().toLowerCase();return ye.filter(t=>!(F!==`all`&&t.category!==F||L&&!t.tags.includes(L)||e&&!(t.label+` `+t.blurb+` `+t.tags.join(` `)+` `+t.category).toLowerCase().includes(e)))})(),pe=(()=>{let e=new Map;for(let t of ye)e.set(t.category,(e.get(t.category)??0)+1);return e})(),me=()=>{k(!1),P(``),I(`all`),z(null)},he=()=>(0,Y.jsx)(K,{open:O,onOpenChange:e=>{e||me()},children:(0,Y.jsxs)(N,{className:`max-w-4xl p-0 h-[85vh] flex flex-col overflow-hidden`,children:[(0,Y.jsxs)(`div`,{className:`flex-shrink-0 px-6 pt-5 pb-3 border-b border-border`,children:[(0,Y.jsxs)(i,{children:[(0,Y.jsxs)(W,{className:`flex items-center gap-2 text-base`,children:[(0,Y.jsx)(A,{className:`w-4 h-4 text-accent-primary`}),` Agent template library`,(0,Y.jsxs)(`span`,{className:`text-[11px] font-normal text-muted-foreground ml-1`,children:[fe.length,` of `,ye.length]})]}),(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground mt-1`,children:`Start from a curated template across engineering, product, science, health, legal, and more. Pick one and open it in the Studio for review and editing before saving.`})]}),(0,Y.jsxs)(`div`,{className:`mt-3 relative`,children:[(0,Y.jsx)(p,{className:`w-3.5 h-3.5 absolute left-2.5 top-1/2 -translate-y-1/2 text-muted-foreground pointer-events-none`}),(0,Y.jsx)(`input`,{value:j,onChange:e=>P(e.target.value),placeholder:`Search by name, description, tag, or category…`,className:`w-full h-9 pl-8 pr-8 text-sm rounded-md border border-border bg-background focus:outline-none focus:ring-1 focus:ring-primary/40`,autoFocus:!0}),j&&(0,Y.jsx)(`button`,{type:`button`,onClick:()=>P(``),className:`absolute right-2 top-1/2 -translate-y-1/2 p-0.5 rounded hover:bg-accent text-muted-foreground`,children:(0,Y.jsx)(R,{className:`w-3.5 h-3.5`})})]}),(0,Y.jsxs)(`div`,{className:`flex gap-1.5 overflow-x-auto mt-3 pb-1 scrollbar-thin`,children:[(0,Y.jsx)(et,{label:`All`,count:ye.length,active:F===`all`,onClick:()=>I(`all`)}),be.map(e=>(0,Y.jsx)(et,{label:e,count:pe.get(e)??0,active:F===e,onClick:()=>I(e)},e))]}),L&&(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 mt-2`,children:[(0,Y.jsx)(`span`,{className:`text-[11px] text-muted-foreground`,children:`Tag filter:`}),(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>z(null),className:`inline-flex items-center gap-1 text-[11px] px-2 py-0.5 rounded-full bg-accent-primary/20 text-accent-primary hover:bg-accent-primary/30`,children:[(0,Y.jsx)(x,{className:`w-3 h-3`}),` `,L,(0,Y.jsx)(R,{className:`w-3 h-3`})]})]})]}),(0,Y.jsx)(`div`,{className:`flex-1 overflow-y-auto px-6 py-4`,children:fe.length===0?(0,Y.jsx)(`div`,{className:`h-full flex items-center justify-center text-center`,children:(0,Y.jsxs)(`div`,{className:`max-w-sm`,children:[(0,Y.jsx)(`div`,{className:`text-sm text-muted-foreground`,children:`No templates match your filters.`}),(0,Y.jsx)(`button`,{type:`button`,onClick:()=>{P(``),I(`all`),z(null)},className:`text-xs text-accent-primary hover:underline mt-2`,children:`Clear filters`})]})}):(0,Y.jsx)(`div`,{className:`grid grid-cols-1 md:grid-cols-2 gap-3`,children:fe.map(e=>(0,Y.jsxs)(`button`,{type:`button`,onClick:()=>{me(),b({kind:`create`,initialBody:e.body,initialName:e.nameHint})},className:`group text-left p-4 rounded-lg border border-border bg-card/40 hover:border-accent-primary/50 hover:bg-accent/40 hover:shadow-sm transition-all`,children:[(0,Y.jsxs)(`div`,{className:`flex items-start gap-3 mb-2`,children:[(0,Y.jsx)(`span`,{className:`text-2xl leading-none mt-0.5`,children:e.emoji}),(0,Y.jsxs)(`div`,{className:`min-w-0 flex-1`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-semibold text-foreground group-hover:text-accent-primary truncate`,children:e.label}),(0,Y.jsx)(`div`,{className:`text-[10px] text-muted-foreground mt-0.5`,children:e.category})]})]}),(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground leading-relaxed line-clamp-2 min-h-[2lh]`,children:e.blurb}),(0,Y.jsx)(`div`,{className:`flex flex-wrap gap-1 mt-3`,children:e.tags.slice(0,5).map(e=>(0,Y.jsx)(`span`,{role:`button`,tabIndex:0,onClick:t=>{t.stopPropagation(),z(e)},onKeyDown:t=>{(t.key===`Enter`||t.key===` `)&&(t.preventDefault(),t.stopPropagation(),z(e))},className:`text-[10px] px-1.5 py-0.5 rounded bg-muted/50 text-muted-foreground hover:bg-accent-primary/20 hover:text-accent-primary cursor-pointer transition-colors `+(L===e?`bg-accent-primary/20 text-accent-primary`:``),children:e},e))}),(0,Y.jsxs)(`div`,{className:`mt-3 pt-2 border-t border-border/40 flex items-center justify-between`,children:[(0,Y.jsx)(`code`,{className:`text-[10px] font-mono text-muted-foreground/70 truncate`,children:e.nameHint}),(0,Y.jsx)(`span`,{className:`text-[10px] text-accent-primary opacity-0 group-hover:opacity-100 transition-opacity`,children:`Open in Studio →`})]})]},e.id))})}),(0,Y.jsxs)(`div`,{className:`flex-shrink-0 px-6 py-3 border-t border-border flex items-center justify-between`,children:[(0,Y.jsx)(`span`,{className:`text-[11px] text-muted-foreground`,children:`Tip: click a tag pill on any card to filter by that tag.`}),(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:me,children:`Cancel`})]})]})}),ge=()=>(0,Y.jsx)(K,{open:ee,onOpenChange:e=>{!e&&!G&&(B(!1),oe(null))},children:(0,Y.jsxs)(N,{className:`max-w-lg`,children:[(0,Y.jsx)(i,{children:(0,Y.jsxs)(W,{className:`flex items-center gap-2`,children:[(0,Y.jsx)(l,{className:`w-4 h-4 text-accent-primary`}),` Generate a custom agent`]})}),(0,Y.jsxs)(`div`,{className:`space-y-3 py-2`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Agent id`}),(0,Y.jsx)(M,{value:V,onChange:e=>te(e.target.value),placeholder:`custom-my-agent`,className:`text-sm font-mono`,disabled:G}),(0,Y.jsxs)(`p`,{className:`text-[11px] text-muted-foreground mt-1`,children:[`Must start with `,(0,Y.jsx)(`code`,{children:`custom-`}),`.`]})]}),(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`label`,{className:`block text-xs font-medium text-muted-foreground mb-1`,children:`Describe what this agent should do`}),(0,Y.jsx)(`textarea`,{value:H,onChange:e=>U(e.target.value),placeholder:`e.g. Review Terraform/IaC changes and flag security misconfigurations, excessive IAM permissions, and public S3 buckets before merging. Conservative and terse.`,className:`w-full text-sm p-2 rounded border border-border bg-background min-h-[120px] resize-y`,disabled:G})]}),ie&&(0,Y.jsx)(`div`,{className:`px-3 py-2 text-xs rounded border border-red-500/30 bg-red-500/10 text-red-400`,children:ie}),(0,Y.jsxs)(`p`,{className:`text-[11px] text-muted-foreground`,children:[`Claude will draft a full `,(0,Y.jsx)(`code`,{children:`.md`}),` body. You'll review it in the Studio and can edit before saving. This spawns a one-shot claude invocation and can take up to 90 seconds.`]})]}),(0,Y.jsxs)(C,{children:[(0,Y.jsx)(y,{variant:`ghost`,size:`sm`,onClick:()=>B(!1),disabled:G,children:`Cancel`}),(0,Y.jsx)(y,{size:`sm`,onClick:_e,disabled:G||!V.trim()||!H.trim(),children:G?(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(d,{className:`w-3.5 h-3.5 mr-1.5 animate-spin`}),` Generating…`]}):(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(l,{className:`w-3.5 h-3.5 mr-1.5`}),` Generate`]})})]})]})}),_e=async()=>{re(!0),oe(null);try{let e=await fetch(`${w()}/profiles/catalog/generate`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({name:V.trim(),description:H.trim()})});if(!e.ok){let t=await e.json().catch(()=>({}));throw Error(t.error??`Generate failed: ${e.status}`)}let t=await e.json();B(!1),b({kind:`create`,initialBody:t.draft,initialName:V.trim()})}catch(e){oe(e.message)}finally{re(!1)}},ve=async e=>{try{let t=await fetch(`${w()}/profiles/catalog/${encodeURIComponent(e)}`);if(!t.ok)throw Error(`Load failed: ${t.status}`);b({kind:`duplicate`,from:e,initialBody:(await t.json()).body})}catch(e){_(e.message)}};if(e.length===0)return(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,Y.jsxs)(`div`,{className:`text-center max-w-sm`,children:[(0,Y.jsx)(`div`,{className:`text-sm font-medium text-foreground`,children:`No agents installed`}),(0,Y.jsxs)(`div`,{className:`text-xs text-muted-foreground mt-1 mb-4`,children:[`Run `,(0,Y.jsx)(`code`,{className:`text-foreground`,children:`npx specrails-core@latest update`}),` in this project to install the upstream agents, or create a custom agent now.`]}),(0,Y.jsxs)(`div`,{className:`flex gap-2 justify-center flex-wrap`,children:[(0,Y.jsxs)(y,{size:`sm`,onClick:()=>B(!0),children:[(0,Y.jsx)(l,{className:`w-3.5 h-3.5 mr-1.5`}),` Generate with Claude`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>k(!0),children:[(0,Y.jsx)(A,{className:`w-3.5 h-3.5 mr-1.5`}),` Template`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>b({kind:`create`}),children:[(0,Y.jsx)(f,{className:`w-3.5 h-3.5 mr-1.5`}),` Blank`]})]})]})}),he(),ge()]});let Q=e.filter(e=>e.kind===`upstream`),xe=e.filter(e=>e.kind===`custom`),$=e.find(e=>e.id===n);return(0,Y.jsxs)(Y.Fragment,{children:[he(),ge(),(0,Y.jsxs)(`div`,{className:`flex h-full`,children:[(0,Y.jsxs)(`aside`,{className:`w-72 flex-shrink-0 border-r border-border flex flex-col`,children:[(0,Y.jsxs)(`div`,{className:`px-3 py-2 border-b border-border`,children:[(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground uppercase tracking-wide mb-1.5`,children:`Catalog`}),(0,Y.jsx)(`div`,{className:`text-[10px] text-muted-foreground/70 mb-2`,children:`Create a custom agent:`}),(0,Y.jsxs)(`div`,{className:`flex gap-1`,children:[(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,className:`flex-1 h-7 text-[11px] px-2`,onClick:()=>k(!0),title:`Start from a template`,children:[(0,Y.jsx)(A,{className:`w-3 h-3 mr-1`}),` Template`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,className:`flex-1 h-7 text-[11px] px-2`,onClick:()=>B(!0),title:`Generate with Claude`,children:[(0,Y.jsx)(l,{className:`w-3 h-3 mr-1`}),` Generate`]}),(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,className:`flex-1 h-7 text-[11px] px-2`,onClick:()=>b({kind:`create`}),title:`Start from a blank template`,children:[(0,Y.jsx)(f,{className:`w-3 h-3 mr-1`}),` Blank`]})]})]}),(0,Y.jsxs)(`div`,{className:`flex-1 overflow-auto p-2`,children:[(0,Y.jsx)(tt,{title:`Upstream (${Q.length})`,description:`Shipped by specrails-core; read-only.`,children:Q.map(e=>(0,Y.jsx)(nt,{agent:e,selected:e.id===n,onSelect:()=>r(e.id)},e.id))}),(0,Y.jsx)(tt,{title:`Custom (${xe.length})`,description:`Your project's custom-*.md files. Use Template / Generate / Blank above to create one.`,children:xe.length===0?(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground px-2 py-2 italic`,children:`No custom agents yet.`}):xe.map(e=>(0,Y.jsx)(nt,{agent:e,selected:e.id===n,onSelect:()=>r(e.id)},e.id))})]})]}),(0,Y.jsxs)(`main`,{className:`flex-1 min-w-0 overflow-auto`,children:[g&&(0,Y.jsx)(`div`,{className:`m-4 px-3 py-2 text-xs rounded border border-red-500/30 bg-red-500/10 text-red-400`,children:g}),$&&(0,Y.jsxs)(`div`,{className:`p-6 min-w-0`,children:[(0,Y.jsxs)(`div`,{className:`flex items-start justify-between gap-4 mb-1`,children:[(0,Y.jsxs)(`div`,{className:`min-w-0 flex-1`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center gap-2 flex-wrap`,children:[(0,Y.jsx)(`h2`,{className:`text-base font-mono font-semibold`,children:$.id}),(0,Y.jsx)(rt,{kind:$.kind}),$.model&&(0,Y.jsxs)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground`,children:[`default model: `,$.model]})]}),$.description&&(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground mt-1 break-words`,children:$.description})]}),(0,Y.jsxs)(`div`,{className:`flex gap-2 flex-shrink-0`,children:[(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>void ve($.id),children:[(0,Y.jsx)(ne,{className:`w-3.5 h-3.5 mr-1`}),` Duplicate`]}),$.kind===`custom`&&(0,Y.jsxs)(Y.Fragment,{children:[(0,Y.jsxs)(y,{size:`sm`,variant:`ghost`,onClick:()=>b({kind:`edit`,agentId:$.id}),children:[(0,Y.jsx)(D,{className:`w-3.5 h-3.5 mr-1`}),` Edit`]}),(0,Y.jsxs)(y,{size:`sm`,onClick:async()=>{if(q)try{let e=await fetch(`${w()}/profiles/catalog/${encodeURIComponent($.id)}`);if(!e.ok)throw Error(`Load failed: ${e.status}`);let t=await e.json();Z(),X.current={refineId:null},E({kind:`open`,projectId:q,agentId:$.id,baseBody:t.body})}catch(e){_(e.message)}},title:`Iteratively refine this agent with AI`,children:[(0,Y.jsx)(ce,{className:`w-3.5 h-3.5 mr-1`}),` AI Edit`]})]})]})]}),(0,Y.jsxs)(`div`,{className:`rounded-md border border-border bg-muted/30 mt-4 min-w-0 overflow-hidden`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between px-3 py-1.5 border-b border-border`,children:[(0,Y.jsxs)(`span`,{className:`text-[11px] font-mono text-muted-foreground truncate`,children:[`.claude/agents/`,$.id,`.md`]}),(0,Y.jsx)(`span`,{className:`text-[10px] text-muted-foreground flex-shrink-0 ml-2`,children:$.kind===`upstream`?`read-only`:`editable — use Edit above`})]}),(0,Y.jsx)(`pre`,{className:`p-4 text-xs font-mono overflow-auto max-h-[60vh] whitespace-pre-wrap break-all`,children:m?`Loading…`:a??``})]})]})]})]})]})}function et({label:e,count:t,active:n,onClick:r}){return(0,Y.jsxs)(`button`,{type:`button`,onClick:r,className:`flex-shrink-0 inline-flex items-center gap-1 h-7 px-2.5 text-[11px] rounded-full border transition-colors whitespace-nowrap `+(n?`bg-accent-primary/20 border-accent-primary/50 text-accent-primary`:`bg-transparent border-border text-muted-foreground hover:bg-accent/50 hover:text-foreground`),children:[e,(0,Y.jsx)(`span`,{className:`text-[9px] px-1 rounded `+(n?`bg-accent-primary/30`:`bg-muted/60`),children:t})]})}function tt({title:e,description:t,children:n}){return(0,Y.jsxs)(`div`,{className:`mb-4`,children:[(0,Y.jsxs)(`div`,{className:`px-2 pb-1`,children:[(0,Y.jsx)(`div`,{className:`text-[11px] font-medium text-muted-foreground uppercase tracking-wide`,children:e}),t&&(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground/70 mt-0.5`,children:t})]}),(0,Y.jsx)(`div`,{className:`space-y-0.5`,children:n})]})}function nt({agent:e,selected:t,onSelect:n}){return(0,Y.jsxs)(`button`,{type:`button`,onClick:n,className:`w-full flex items-center justify-between gap-2 px-2 py-1.5 text-left rounded transition-colors `+(t?`bg-accent text-foreground`:`text-muted-foreground hover:bg-accent/50 hover:text-foreground`),children:[(0,Y.jsx)(`span`,{className:`text-sm font-mono truncate`,children:e.id}),(0,Y.jsx)(rt,{kind:e.kind})]})}function rt({kind:e}){return(0,Y.jsx)(`span`,{className:`text-[10px] px-1.5 py-0.5 rounded flex-shrink-0 `+(e===`custom`?`bg-purple-500/15 text-purple-400`:`bg-muted text-muted-foreground`),children:e})}var it=[{label:`7d`,days:7},{label:`30d`,days:30},{label:`90d`,days:90}];function at(){let[e,t]=(0,J.useState)(null),[n,r]=(0,J.useState)(30),[i,a]=(0,J.useState)(!0);if((0,J.useEffect)(()=>{let e=!1;return a(!0),fetch(`${w()}/profiles/analytics?windowDays=${n}`).then(e=>e.ok?e.json():null).then(n=>{!e&&n&&t(n)}).catch(()=>{}).finally(()=>{e||a(!1)}),()=>{e=!0}},[n]),i&&!e)return null;if(!e||e.rows.length===0)return(0,Y.jsxs)(`div`,{className:`mx-6 my-4 rounded-md border border-border bg-muted/20`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-2 border-b border-border`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`div`,{className:`text-xs font-medium text-foreground`,children:`Profile usage`}),(0,Y.jsx)(`div`,{className:`text-[11px] text-muted-foreground`,children:`Jobs launched per profile in the selected window.`})]}),(0,Y.jsx)(`div`,{className:`flex gap-0.5`,children:it.map(e=>(0,Y.jsx)(`button`,{type:`button`,onClick:()=>r(e.days),className:`text-[10px] px-2 py-1 rounded transition-colors `+(n===e.days?`bg-accent text-foreground`:`text-muted-foreground hover:bg-accent/50 hover:text-foreground`),children:e.label},e.days))})]}),(0,Y.jsx)(`div`,{className:`px-4 py-8 text-xs text-muted-foreground`,children:`No profile-scoped jobs yet.`})]});let o=Math.max(...e.rows.map(e=>e.jobs),1);return(0,Y.jsxs)(`div`,{className:`mx-6 my-4 rounded-md border border-border bg-muted/20`,children:[(0,Y.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-2 border-b border-border`,children:[(0,Y.jsxs)(`div`,{children:[(0,Y.jsx)(`div`,{className:`text-xs font-medium text-foreground`,children:`Profile usage`}),(0,Y.jsxs)(`div`,{className:`text-[11px] text-muted-foreground`,children:[`Jobs launched per profile in the last `,e.windowDays,` days.`]})]}),(0,Y.jsx)(`div`,{className:`flex gap-0.5`,children:it.map(e=>(0,Y.jsx)(`button`,{type:`button`,onClick:()=>r(e.days),className:`text-[10px] px-2 py-1 rounded transition-colors `+(n===e.days?`bg-accent text-foreground`:`text-muted-foreground hover:bg-accent/50 hover:text-foreground`),children:e.label},e.days))})]}),(0,Y.jsx)(`div`,{className:`p-4 space-y-2`,children:e.rows.map(e=>(0,Y.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Y.jsx)(`div`,{className:`w-32 text-xs font-mono truncate`,children:e.profileName}),(0,Y.jsx)(`div`,{className:`flex-1 h-4 bg-muted/50 rounded overflow-hidden`,children:(0,Y.jsx)(`div`,{className:`h-full bg-accent-primary/70`,style:{width:`${Math.max(6,Math.round(e.jobs/o*100))}%`}})}),(0,Y.jsxs)(`div`,{className:`flex gap-4 text-[11px] text-muted-foreground min-w-0 flex-shrink-0`,children:[(0,Y.jsxs)(`span`,{title:`jobs`,children:[(0,Y.jsx)(`span`,{className:`text-foreground font-mono`,children:e.jobs}),` jobs`]}),(0,Y.jsxs)(`span`,{title:`success rate`,children:[(0,Y.jsxs)(`span`,{className:`text-foreground font-mono`,children:[Math.round(e.successRate*100),`%`]}),` `,`ok`]}),e.avgDurationMs!=null&&(0,Y.jsx)(`span`,{title:`avg duration`,children:(0,Y.jsx)(`span`,{className:`text-foreground font-mono`,children:ot(e.avgDurationMs)})}),e.avgTokens!=null&&(0,Y.jsxs)(`span`,{title:`avg tokens`,children:[(0,Y.jsx)(`span`,{className:`text-foreground font-mono`,children:st(e.avgTokens)}),` `,`tok`]})]})]},e.profileName))})]})}function ot(e){let t=Math.round(e/1e3);if(t<60)return`${t}s`;let n=Math.floor(t/60);return n<60?`${n}m`:`${Math.floor(n/60)}h${n%60}m`}function st(e){return e<1e3?Math.round(e).toString():e<1e6?`${(e/1e3).toFixed(1)}k`:`${(e/1e6).toFixed(1)}M`}var ct=`specrails-hub:agents-tab`;function lt(){try{let e=localStorage.getItem(ct);if(e===`profiles`||e===`usage`||e===`catalog`)return e}catch{}return`profiles`}function ut(){let[e,t]=(0,J.useState)(()=>lt()),n=e=>{t(e);try{localStorage.setItem(ct,e)}catch{}},[r,i]=(0,J.useState)(null),{activeProjectId:a}=ae(),{pendingRestores:o}=T(),s=(0,J.useRef)(!1);return(0,J.useEffect)(()=>{if(!a)return;let t=o.some(e=>e.kind===`ai-edit`&&e.projectId===a);t&&!s.current&&e!==`catalog`&&n(`catalog`),s.current=t},[o,a,e]),(0,J.useEffect)(()=>{let e=!1;return fetch(`${w()}/profiles/core-version`).then(e=>e.ok?e.json():null).then(t=>{!e&&t&&i(t)}).catch(()=>{}),()=>{e=!0}},[]),(0,Y.jsxs)(`div`,{className:`flex flex-col h-full`,children:[r!==null&&!r.profileAware&&(0,Y.jsxs)(`div`,{className:`flex-shrink-0 flex items-start gap-3 px-6 py-3 border-b border-yellow-500/30 bg-yellow-500/10`,children:[(0,Y.jsx)(h,{className:`w-4 h-4 text-yellow-500 mt-0.5 flex-shrink-0`}),(0,Y.jsxs)(`div`,{className:`text-xs`,children:[(0,Y.jsxs)(`div`,{className:`font-medium text-yellow-500`,children:[`Profile-aware pipeline requires specrails-core `,r.required,`+`]}),(0,Y.jsxs)(`div`,{className:`text-yellow-500/80 mt-0.5`,children:[`This project has`,` `,(0,Y.jsx)(`code`,{className:`px-1 rounded bg-yellow-500/20`,children:r.version??`unknown`}),`. Run`,` `,(0,Y.jsx)(`code`,{className:`px-1 rounded bg-yellow-500/20`,children:`npx specrails-core@latest update`}),` `,`inside the project to unlock profile mode. Profiles you create here will still save; they just won't affect the pipeline until core is updated.`]})]})]}),(0,Y.jsxs)(`div`,{className:`flex-shrink-0 border-b border-border`,children:[(0,Y.jsxs)(`div`,{className:`px-6 pt-4`,children:[(0,Y.jsx)(`h1`,{className:`text-lg font-semibold`,children:`Agents`}),(0,Y.jsx)(`p`,{className:`text-xs text-muted-foreground mt-0.5`,children:`Manage agent profiles and the catalog (upstream + custom) for this project.`})]}),(0,Y.jsxs)(`div`,{className:`flex items-center gap-1 px-4 pt-3`,children:[(0,Y.jsx)(ft,{active:e===`profiles`,onClick:()=>n(`profiles`),children:`Profiles`}),(0,Y.jsx)(ft,{active:e===`usage`,onClick:()=>n(`usage`),children:`Usage`}),(0,Y.jsx)(ft,{active:e===`catalog`,onClick:()=>n(`catalog`),children:`Catalog`})]})]}),(0,Y.jsxs)(`div`,{className:`flex-1 min-w-0 overflow-auto`,children:[e===`profiles`&&(0,Y.jsx)(ve,{}),e===`usage`&&(0,Y.jsx)(dt,{}),e===`catalog`&&(0,Y.jsx)($e,{})]})]})}function dt(){return(0,Y.jsx)(`div`,{className:`min-h-full`,children:(0,Y.jsx)(at,{})})}function ft({active:e,children:t,onClick:n}){return(0,Y.jsx)(`button`,{type:`button`,onClick:n,className:`h-8 px-3 text-xs font-medium rounded-t-md border-b-2 transition-colors `+(e?`text-foreground border-foreground`:`text-muted-foreground border-transparent hover:text-foreground hover:border-border`),children:t})}export{ut as default};