@tickernelz/paperclip-pro-ui 2026.1027.0 → 2026.1104.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- var kt=Object.defineProperty;var s=(n,c)=>kt(n,"name",{value:c,configurable:!0});import{i as at}from"./rolldown-runtime-ZpX3ckPj.js";import{G as Pt,Qa as At,St as m,Za as Mt,_a as S,eo as Dt,ht as Tt,va as A}from"./boxes-Bpd3vwBa.js";import{Bn as qt,Vt as ze,Wt as Ge,d as Kt,fn as Et,ln as We,n as Ut,pt as Ot,r as F,st as J,zt as Ye}from"./BreadcrumbContext-DP0mgUIg.js";import{_ as He,v as Je,y as lt}from"./SearchableSelect-CyZJenSa.js";import{Ft as Lt,It as Qt,Mt as Bt,Nr as ee,Nt as Vt,Rr as $t,V as zt,jt as Gt,r as Wt,t as Yt,wt as Xe,x as he}from"./RoutineRunVariablesDialog-CrC88u8T.js";import{t as Ze}from"./AgentAvatar-D4YQ4myL.js";import{r as Ht,t as Jt}from"./dialog-aW5qVpwP.js";import{l as X,t as Xt}from"./MarkdownEditor-DHz7g42W.js";import{n as et,t as Zt}from"./tabs-D69nzsXS.js";import{h as es}from"./StatusIcon-C_7wHYAC.js";import{t as ts}from"./check-2BJzr5lo.js";import{t as ss}from"./chevron-down-C8ZdH7h9.js";import{a as ct}from"./clipboard-B6GwC8zN.js";import{a as Z,c as je,l as ye,n as ve,r as be}from"./select-CV4NIBut.js";import{t as ns}from"./heartbeats-CpU47ZZC.js";import{a as rs}from"./ToastContext-YqYQB596.js";import{t as os}from"./agents-BXUtBaur.js";import{t as is}from"./access-CspWWirX.js";import{i as as}from"./company-members-Duugb3uG.js";import{r as ls,t as cs}from"./card-B_2b5Riw.js";import{t as we}from"./EmptyState-BN5HM6-K.js";import{t as ds}from"./PageSkeleton-BiXcKvFW.js";import{$ as us,$t as ms,G as ps,J as fs,K as gs,Q as xs,X as hs,Xt as js,Y as ys,Z as vs,dt as bs,en as ws,et as Ns,ft as M,nt as Is,q as Rs,tt as _s,ut as Cs}from"./index-5wqemOVn.js";var l=at(Dt(),1),t=at(At(),1),Ss=["coalesce_if_active","always_enqueue","skip_if_active"],Fs=["skip_missed","enqueue_missed_with_cap"],ks={coalesce_if_active:"If a run is already active, keep just one follow-up run queued.",always_enqueue:"Queue every trigger occurrence, even if the routine is already running.",skip_if_active:"Drop new trigger occurrences while a run is still active."},Ps={skip_missed:"Ignore windows that were missed while the scheduler or routine was paused.",enqueue_missed_with_cap:"Catch up missed schedule windows after recovery; sub-hourly schedules are combined into one catch-up run, slower schedules replay each missed window up to a cap."};function tt(n){n&&(n.style.height="auto",n.style.height=`${n.scrollHeight}px`)}s(tt,"autoResizeTextarea");var As="__built_in_routines",st={sortField:"title",sortDir:"asc",groupBy:"folder",collapsedGroups:[]};function nt(n){try{const c=localStorage.getItem(n);if(c)return{...st,...JSON.parse(c)}}catch{}return{...st}}s(nt,"getRoutineViewState");function Ms(n,c){localStorage.setItem(n,JSON.stringify(c))}s(Ms,"saveRoutineViewState");function q(n){if(!n)return Number.NEGATIVE_INFINITY;const c=new Date(n).getTime();return Number.isFinite(c)?c:Number.NEGATIVE_INFINITY}s(q,"timestampValue");function rt(n,c){return(n??"").localeCompare(c??"",void 0,{sensitivity:"base"})}s(rt,"compareNullableText");function Ds(n){return{...n,description:n.description.trim()||null,projectId:n.projectId||null,folderId:n.folderId||null,assigneeAgentId:n.assigneeAgentId||null}}s(Ds,"buildRoutineMutationPayload");function Ts(n,c,d,b,g){if(c==="none")return[{key:"__all",label:null,items:n}];if(c==="folder"){const o=he(n,i=>i.folderId??"__unfiled");return Object.keys(o).sort((i,j)=>{if(i==="__unfiled"||j==="__unfiled")return i===j?0:i==="__unfiled"?1:-1;const w=g.get(i),I=g.get(j),O=(Number.isFinite(w?.position)?w.position:Number.POSITIVE_INFINITY)-(Number.isFinite(I?.position)?I.position:Number.POSITIVE_INFINITY);return O!==0?O:(w?.name??"Unknown folder").localeCompare(I?.name??"Unknown folder",void 0,{sensitivity:"base"})||i.localeCompare(j)}).map(i=>({key:i,label:i==="__unfiled"?"Unfiled":g.get(i)?.name??"Unknown folder",items:o[i]}))}if(c==="project"){const o=he(n,i=>i.projectId??"__no_project");return Object.keys(o).sort((i,j)=>{const w=i==="__no_project"?"No project":d.get(i)?.name??"Unknown project",I=j==="__no_project"?"No project":d.get(j)?.name??"Unknown project";return w.localeCompare(I)}).map(i=>({key:i,label:i==="__no_project"?"No project":d.get(i)?.name??"Unknown project",items:o[i]}))}const x=he(n,o=>o.assigneeAgentId??"__unassigned");return Object.keys(x).sort((o,i)=>{const j=o==="__unassigned"?"Unassigned":b.get(o)?.name??"Unknown agent",w=i==="__unassigned"?"Unassigned":b.get(i)?.name??"Unknown agent";return j.localeCompare(w)}).map(o=>({key:o,label:o==="__unassigned"?"Unassigned":b.get(o)?.name??"Unknown agent",items:x[o]}))}s(Ts,"buildRoutineGroups");function ot(n){return n.originKind==="built_in_agent_bundle"}s(ot,"isBuiltInRoutine");function qs(n,c,d,b,g){const x=n.filter(ot),o=Ts(n.filter(i=>!ot(i)),c,d,b,g).filter(i=>i.items.length>0).map(i=>x.length>0&&c==="none"&&i.key==="__all"?{...i,label:"Custom routines"}:i);return x.length===0?o:[...o,{key:As,label:"Built-in routines",items:x}]}s(qs,"buildRoutineSections");function Ks(n,c,d){const b=d==="asc"?1:-1;return[...n].sort((g,x)=>{let o=0;return c==="title"?o=rt(g.title,x.title):c==="created"?o=q(g.createdAt)-q(x.createdAt):c==="lastRun"?o=q(g.lastRun?.triggeredAt??g.lastTriggeredAt)-q(x.lastRun?.triggeredAt??x.lastTriggeredAt):o=q(g.updatedAt)-q(x.updatedAt),o!==0?o*b:rt(g.title,x.title)})}s(Ks,"sortRoutines");function it(n){return n==="runs"?"/routines?tab=runs":"/routines"}s(it,"buildRoutinesTabHref");function Es({label:n,count:c,isOpen:d}){return(0,t.jsxs)("div",{className:`flex items-center gap-2 rounded-lg border border-border px-3 py-2${d?" mb-1":""}`,children:[(0,t.jsxs)(lt,{className:"flex items-center gap-1.5",children:[(0,t.jsx)(ct,{className:"h-3.5 w-3.5 shrink-0 text-muted-foreground transition-transform [[data-state=open]>&]:rotate-90"}),(0,t.jsx)("span",{className:"text-sm font-semibold uppercase tracking-wide",children:n})]}),(0,t.jsx)("span",{className:"text-xs text-muted-foreground",children:c})]})}s(Es,"RoutineSectionHeader");function un(){const{selectedCompanyId:n}=Tt(),{setBreadcrumbs:c}=Ut(),d=Mt(),b=Kt(),[g,x]=qt(),{pushToast:o}=rs(),i=(0,l.useRef)(null),j=(0,l.useRef)(null),w=(0,l.useRef)(null),I=(0,l.useRef)(null),[O,K]=(0,l.useState)(!1),[te,L]=(0,l.useState)(null),[Q,se]=(0,l.useState)(null),[dt,Ne]=(0,l.useState)(!1),[ne,re]=(0,l.useState)(!1),[B,V]=(0,l.useState)([]),[Ie,Re]=(0,l.useState)([]),[ut,_e]=(0,l.useState)(null),[mt,Ce]=(0,l.useState)(null),[k,oe]=(0,l.useState)(null),[$,z]=(0,l.useState)(!1),[Se,ie]=(0,l.useState)(!1),P=g.get("tab")==="runs"?"runs":"routines",[p,R]=(0,l.useState)({title:"",description:"",projectId:"",folderId:null,assigneeAgentId:"",priority:"medium",concurrencyPolicy:"coalesce_if_active",catchUpPolicy:"skip_missed",variables:[]}),G=n?`paperclip:routines-view:${n}`:"paperclip:routines-view",[u,Fe]=(0,l.useState)(()=>nt(G)),f=_s(g.get("folder"));(0,l.useEffect)(()=>{c([{label:"Routines"}])},[c]),(0,l.useEffect)(()=>{Fe(nt(G))},[G]);const{data:ke,isLoading:pt,error:ae}=A({queryKey:m.routines.list(n),queryFn:s(()=>X.list(n),"queryFn"),enabled:!!n}),{data:h,isLoading:Pe}=A({queryKey:m.folders.list(n,"routine"),queryFn:s(()=>M.list(n,"routine"),"queryFn"),enabled:!!n&&P==="routines"}),{data:_}=A({queryKey:m.agents.list(n),queryFn:s(()=>os.list(n),"queryFn"),enabled:!!n}),{data:C}=A({queryKey:m.projects.list(n,{includeArchived:!0}),queryFn:s(()=>es.list(n,{includeArchived:!0}),"queryFn"),enabled:!!n}),{data:Ae}=A({queryKey:m.access.companyUserDirectory(n),queryFn:s(()=>is.listUserDirectory(n),"queryFn"),enabled:!!n}),{data:le,isLoading:ft,error:gt}=A({queryKey:[...m.issues.list(n),"routine-executions"],queryFn:s(()=>We.list(n,{originKind:"routine_execution"}),"queryFn"),enabled:!!n&&P==="runs"}),Me=m.liveRuns(n),ce=ws({companyId:n,resourceKey:"live-runs",queryKey:Me,enabled:!!n&&P==="runs",refetchInterval:!1,leaderOnly:!0}),{data:de,dataUpdatedAt:xt}=A({queryKey:Me,queryFn:s(()=>ns.liveRunsForCompany(n),"queryFn"),enabled:ce.enabled,refetchInterval:ce.refetchInterval});ms(ce,de,xt),(0,l.useEffect)(()=>{tt(j.current)},[p.title,$]);const ht=(0,l.useMemo)(()=>as({agents:_,projects:(C??[]).filter(e=>!e.archivedAt),members:Ae?.users}),[_,Ae?.users,C]),N=S({mutationFn:s(()=>X.create(n,Ds(p)),"mutationFn"),onSuccess:s(async e=>{R({title:"",description:"",projectId:"",folderId:null,assigneeAgentId:"",priority:"medium",concurrencyPolicy:"coalesce_if_active",catchUpPolicy:"skip_missed",variables:[]}),z(!1),ie(!1),await d.invalidateQueries({queryKey:m.routines.list(n)}),o({title:"Routine created",body:e.assigneeAgentId?"Add the first trigger to turn it into a live workflow.":"Draft saved. Add a default agent before enabling automation.",tone:"success"}),b(`/routines/${e.id}?tab=triggers`)},"onSuccess")}),De=S({mutationFn:s(e=>M.create(n,{kind:"routine",...e}),"mutationFn"),onSuccess:s(async e=>{if(K(!1),L(null),await d.invalidateQueries({queryKey:m.folders.list(n,"routine")}),Ie.length>0){const r=Ie;Re([]);try{await Promise.all(r.map(a=>M.moveItem(n,{kind:"routine",itemId:a,folderId:e.id}))),await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.folders.list(n,"routine")})])}catch(a){o({title:"Folder created, move failed",body:a instanceof Error?a.message:"Paperclip could not move the selected routines.",tone:"error"});return}}else Y(e.id);o({title:"Folder created",body:e.name,tone:"success"})},"onSuccess"),onError:s(e=>{o({title:"Failed to save folder",body:e instanceof Error?e.message:"Paperclip could not save the folder.",tone:"error"})},"onError")}),ue=S({mutationFn:s(({folderId:e,payload:r})=>M.update(n,e,r),"mutationFn"),onSuccess:s(async()=>{K(!1),L(null),await d.invalidateQueries({queryKey:m.folders.list(n,"routine")})},"onSuccess"),onError:s(e=>{o({title:"Folder save failed",body:e instanceof Error?e.message:"Paperclip could not update the folder.",tone:"error"})},"onError")}),Te=S({mutationFn:s(e=>M.delete(n,e),"mutationFn"),onSuccess:s(async(e,r)=>{f===r&&Y("all"),se(null),await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.folders.list(n,"routine")})]),o({title:"Folder deleted",body:"Items moved to Unfiled.",tone:"success"})},"onSuccess"),onError:s(e=>{o({title:"Folder delete failed",body:e instanceof Error?e.message:"Paperclip could not delete the folder.",tone:"error"})},"onError")}),qe=S({mutationFn:s(({itemId:e,folderId:r})=>M.moveItem(n,{kind:"routine",itemId:e,folderId:r}),"mutationFn"),onSuccess:s(async()=>{await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.folders.list(n,"routine")})])},"onSuccess"),onError:s(e=>{o({title:"Move failed",body:e instanceof Error?e.message:"Paperclip could not move the routine.",tone:"error"})},"onError")}),jt=S({mutationFn:s(({id:e,data:r})=>We.update(e,r),"mutationFn"),onSuccess:s(async()=>{await d.invalidateQueries({queryKey:[...m.issues.list(n),"routine-executions"]})},"onSuccess")}),Ke=S({mutationFn:s(({id:e,status:r})=>X.update(e,{status:r}),"mutationFn"),onMutate:s(({id:e})=>{Ce(e)},"onMutate"),onSuccess:s(async(e,r)=>{await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.routines.detail(r.id)})])},"onSuccess"),onSettled:s(()=>{Ce(null)},"onSettled"),onError:s(e=>{o({title:"Failed to update routine",body:e instanceof Error?e.message:"Paperclip could not update the routine.",tone:"error"})},"onError")}),Ee=S({mutationFn:s(({id:e,data:r})=>X.run(e,{...r?.variables&&Object.keys(r.variables).length>0?{variables:r.variables}:{},...r?.assigneeAgentId!==void 0?{assigneeAgentId:r.assigneeAgentId}:{},...r?.projectId!==void 0?{projectId:r.projectId}:{},...r?.executionWorkspaceId!==void 0?{executionWorkspaceId:r.executionWorkspaceId}:{},...r?.executionWorkspacePreference!==void 0?{executionWorkspacePreference:r.executionWorkspacePreference}:{},...r?.executionWorkspaceSettings!==void 0?{executionWorkspaceSettings:r.executionWorkspaceSettings}:{}}),"mutationFn"),onMutate:s(({id:e})=>{_e(e)},"onMutate"),onSuccess:s(async(e,{id:r})=>{oe(null),await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.routines.detail(r)})])},"onSuccess"),onSettled:s(()=>{_e(null)},"onSettled"),onError:s(e=>{o({title:"Routine run failed",body:e instanceof Error?e.message:"Paperclip could not start the routine run.",tone:"error"})},"onError")}),me=(0,l.useMemo)(()=>Vt(),[$]),yt=(0,l.useMemo)(()=>Gt(),[$]),vt=(0,l.useMemo)(()=>Lt((_??[]).filter(e=>e.status!=="terminated"),me).map(e=>({id:e.id,label:e.name,searchText:`${e.name} ${e.role} ${e.title??""}`})),[_,me]),bt=(0,l.useMemo)(()=>(C??[]).filter(e=>!e.archivedAt).map(e=>({id:e.id,label:e.name,searchText:e.description??""})),[C]),E=(0,l.useMemo)(()=>new Map((_??[]).map(e=>[e.id,e])),[_]),U=(0,l.useMemo)(()=>new Map((C??[]).map(e=>[e.id,e])),[C]),Ue=(0,l.useMemo)(()=>new Map((h?.folders??[]).map(e=>[e.id,e])),[h]),wt=(0,l.useMemo)(()=>zt(de,le),[de,le]),y=(0,l.useMemo)(()=>(ke??[]).filter(e=>e.status!=="archived"),[ke]),Oe=(0,l.useMemo)(()=>u.groupBy!=="folder"||f==="all"?y:f==="unfiled"?y.filter(e=>!e.folderId):y.filter(e=>e.folderId===f),[f,u.groupBy,y]),pe=(0,l.useMemo)(()=>{if(!h)return h;const e=new Map;let r=0;for(const a of y)a.folderId?e.set(a.folderId,(e.get(a.folderId)??0)+1):r+=1;return{...h,allCount:y.length,unfiledCount:r,folders:h.folders.map(a=>({...a,itemCount:e.get(a.id)??0}))}},[h,y]),D=(0,l.useMemo)(()=>Ks(Oe,u.sortField,u.sortDir),[Oe,u.sortDir,u.sortField]),Nt=(0,l.useMemo)(()=>qs(D,u.groupBy,U,E,Ue),[E,Ue,U,u.groupBy,D]),It=(0,l.useMemo)(()=>Et("Recent Runs",it("runs"),"issues"),[]),Le=p.assigneeAgentId?E.get(p.assigneeAgentId)??null:null,Qe=p.projectId?U.get(p.projectId)??null:null,Be=Is(h?.folders??[],f),W=(h?.folders.length??0)>0,fe=P==="routines"&&u.groupBy==="folder"&&W;function ge(e){Fe(r=>{const a={...r,...e};return Ms(G,a),a})}s(ge,"updateRoutineView");function Ve(e){const r=e==="runs"?"runs":"routines";(0,l.startTransition)(()=>{b(it(r))})}s(Ve,"handleTabChange");function Y(e){x(r=>{const a=new URLSearchParams(r),v=Ns(e);return v?a.set("folder",v):a.delete("folder"),a})}s(Y,"setFolderSelection");function T(e=[]){Re(e),L(null),K(!0)}s(T,"openCreateFolder");function $e(){R(e=>({...e,folderId:f==="all"||f==="unfiled"?null:f})),z(!0)}s($e,"openCreateRoutine");async function Rt(e){const r=B;if(r.length!==0)try{await Promise.all(r.map(a=>M.moveItem(n,{kind:"routine",itemId:a,folderId:e}))),V([]),re(!1),await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.folders.list(n,"routine")})]),o({title:"Routines moved",body:`${r.length} routine${r.length===1?"":"s"} filed.`,tone:"success"})}catch(a){o({title:"Failed to move routines",body:a instanceof Error?a.message:"Paperclip could not move the selected routines.",tone:"error"})}}s(Rt,"moveSelectedRoutines");function _t(e){oe(e)}s(_t,"handleRunNow");function Ct(e,r){if(!r&&!e.assigneeAgentId){o({title:"Default agent required",body:"Set a default agent before enabling routine automation.",tone:"warn"});return}Ke.mutate({id:e.id,status:bs(e.status,!r)})}s(Ct,"handleToggleEnabled");function St(e){Ke.mutate({id:e.id,status:e.status==="archived"?"active":"archived"})}return s(St,"handleToggleArchived"),n?pt?(0,t.jsx)(ds,{variant:"issues-list"}):(0,t.jsxs)("div",{className:"space-y-6",children:[(0,t.jsxs)("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between",children:[(0,t.jsxs)("div",{className:"space-y-1",children:[(0,t.jsx)("h1",{className:"text-2xl font-semibold tracking-tight",children:"Routines"}),(0,t.jsx)("p",{className:"text-sm text-muted-foreground",children:"Recurring work definitions that materialize into auditable execution tasks."})]}),(0,t.jsxs)(F,{onClick:$e,children:[(0,t.jsx)(J,{className:"mr-2 h-4 w-4"}),"Create routine"]})]}),(0,t.jsxs)(Zt,{value:P,onValueChange:Ve,children:[(0,t.jsx)(js,{align:"start",value:P,onValueChange:Ve,items:[{value:"routines",label:"Routines"},{value:"runs",label:"Recent Runs"}]}),(0,t.jsxs)(et,{value:"routines",className:"space-y-4",children:[(0,t.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,t.jsxs)("p",{className:"text-sm text-muted-foreground",children:[y.length," routine",y.length===1?"":"s"]}),(0,t.jsxs)("div",{className:"flex items-center gap-1",children:[(0,t.jsxs)(Ye,{children:[(0,t.jsx)(Ge,{asChild:!0,children:(0,t.jsxs)(F,{variant:"ghost",size:"sm",className:"text-xs",title:"Sort",children:[(0,t.jsx)($t,{className:"h-3.5 w-3.5 sm:h-3 sm:w-3 sm:mr-1"}),(0,t.jsx)("span",{className:"hidden sm:inline",children:"Sort"})]})}),(0,t.jsx)(ze,{align:"end",className:"w-44 p-0",children:(0,t.jsx)("div",{className:"p-2 space-y-0.5",children:[["updated","Updated"],["created","Created"],["lastRun","Last run"],["title","Title"]].map(([e,r])=>(0,t.jsxs)("button",{className:`flex w-full items-center justify-between rounded-sm px-2 py-1.5 text-sm ${u.sortField===e?"bg-accent/50 text-foreground":"text-muted-foreground hover:bg-accent/50"}`,onClick:s(()=>{ge(u.sortField===e?{sortDir:u.sortDir==="asc"?"desc":"asc"}:{sortField:e,sortDir:e==="title"?"asc":"desc"})},"onClick"),children:[(0,t.jsx)("span",{children:r}),u.sortField===e?(0,t.jsx)("span",{className:"text-xs text-muted-foreground",children:u.sortDir==="asc"?"Asc":"Desc"}):null]},e))})})]}),(0,t.jsxs)(Ye,{children:[(0,t.jsx)(Ge,{asChild:!0,children:(0,t.jsxs)(F,{variant:"ghost",size:"sm",className:"text-xs",title:"Group",children:[(0,t.jsx)(Ot,{className:"h-3.5 w-3.5 sm:h-3 sm:w-3 sm:mr-1"}),(0,t.jsx)("span",{className:"hidden sm:inline",children:"Group"})]})}),(0,t.jsx)(ze,{align:"end",className:"w-44 p-0",children:(0,t.jsx)("div",{className:"p-2 space-y-0.5",children:[["folder","Folder"],["project","Project"],["assignee","Agent"],["none","None"]].map(([e,r])=>(0,t.jsxs)("button",{className:`flex w-full items-center justify-between rounded-sm px-2 py-1.5 text-sm ${u.groupBy===e?"bg-accent/50 text-foreground":"text-muted-foreground hover:bg-accent/50"}`,onClick:s(()=>ge({groupBy:e,collapsedGroups:[]}),"onClick"),children:[(0,t.jsx)("span",{children:r}),u.groupBy===e?(0,t.jsx)(ts,{className:"h-3.5 w-3.5"}):null]},e))})})]}),u.groupBy==="folder"&&!W?(0,t.jsxs)(F,{variant:"outline",size:"sm",onClick:s(()=>T(),"onClick"),children:[(0,t.jsx)(J,{className:"mr-2 h-3.5 w-3.5"}),"New folder"]}):null,fe?(0,t.jsx)(F,{variant:"ghost",size:"sm",className:"text-xs",onClick:s(()=>re(e=>!e),"onClick"),children:ne?"Done":"Select"}):null]})]}),u.groupBy==="folder"?(0,t.jsx)("div",{className:"md:hidden",children:(0,t.jsx)(fs,{result:pe,selection:f,allLabel:"All routines",onClick:s(()=>Ne(!0),"onClick")})}):null]}),(0,t.jsx)(et,{value:"runs",children:(0,t.jsx)(Wt,{issues:le??[],isLoading:ft,error:gt,agents:_,projects:C,liveIssueIds:wt,viewStateKey:"paperclip:routine-recent-runs-view",issueLinkState:It,onUpdateIssue:s((e,r)=>jt.mutate({id:e,data:r}),"onUpdateIssue")})})]}),(0,t.jsx)(Jt,{open:$,onOpenChange:s(e=>{N.isPending||z(e)},"onOpenChange"),children:(0,t.jsxs)(Ht,{showCloseButton:!1,className:"flex max-h-(--sz-calc-18) max-w-3xl flex-col gap-0 overflow-hidden p-0",children:[(0,t.jsxs)("div",{className:"shrink-0 flex flex-wrap items-center justify-between gap-3 border-b border-border/60 px-5 py-3",children:[(0,t.jsxs)("div",{children:[(0,t.jsx)("p",{className:"text-xs font-medium uppercase tracking-(--tracking-caps) text-muted-foreground",children:"New routine"}),(0,t.jsx)("p",{className:"text-sm text-muted-foreground",children:"Define the recurring work first. Default project and agent are optional for draft routines."})]}),(0,t.jsx)(F,{variant:"ghost",size:"sm",onClick:s(()=>{z(!1),ie(!1)},"onClick"),disabled:N.isPending,children:"Cancel"})]}),(0,t.jsxs)("div",{className:"min-h-0 flex-1 overflow-y-auto",children:[(0,t.jsx)("div",{className:"px-5 pt-5 pb-3",children:(0,t.jsx)("textarea",{ref:j,className:"w-full resize-none overflow-hidden bg-transparent text-xl font-semibold outline-none placeholder:text-muted-foreground/50",placeholder:"Routine title",rows:1,value:p.title,onChange:s(e=>{R(r=>({...r,title:e.target.value})),tt(e.target)},"onChange"),onKeyDown:s(e=>{if(e.key==="Enter"&&!e.metaKey&&!e.ctrlKey&&!e.nativeEvent.isComposing){e.preventDefault(),i.current?.focus();return}e.key==="Tab"&&!e.shiftKey&&(e.preventDefault(),p.assigneeAgentId?p.projectId?i.current?.focus():I.current?.focus():w.current?.focus())},"onKeyDown"),autoFocus:!0})}),(0,t.jsx)("div",{className:"px-5 pb-3",children:(0,t.jsx)("div",{className:"overflow-x-auto overscroll-x-contain",children:(0,t.jsxs)("div",{className:"inline-flex min-w-full flex-wrap items-center gap-2 text-sm text-muted-foreground sm:min-w-max sm:flex-nowrap",children:[(0,t.jsx)("span",{children:"For"}),(0,t.jsx)(Xe,{ref:w,value:p.assigneeAgentId,options:vt,recentOptionIds:me,placeholder:"Responsible",noneLabel:"No responsible",searchPlaceholder:"Search responsible...",emptyMessage:"No responsible found.",onChange:s(e=>{e&&Qt(e),R(r=>({...r,assigneeAgentId:e}))},"onChange"),onConfirm:s(()=>{p.projectId?i.current?.focus():I.current?.focus()},"onConfirm"),renderTriggerValue:s(e=>e?Le?(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(Ze,{agent:Le,size:16,className:"h-3.5 w-3.5 shrink-0 text-muted-foreground"}),(0,t.jsx)("span",{className:"truncate",children:e.label})]}):(0,t.jsx)("span",{className:"truncate",children:e.label}):(0,t.jsx)("span",{className:"text-muted-foreground",children:"Responsible"}),"renderTriggerValue"),renderOption:s(e=>{if(!e.id)return(0,t.jsx)("span",{className:"truncate",children:e.label});const r=E.get(e.id);return(0,t.jsxs)(t.Fragment,{children:[r?(0,t.jsx)(Ze,{agent:r,size:16,className:"h-3.5 w-3.5 shrink-0 text-muted-foreground"}):null,(0,t.jsx)("span",{className:"truncate",children:e.label})]})},"renderOption")}),(0,t.jsx)("span",{children:"in"}),(0,t.jsx)(Xe,{ref:I,value:p.projectId,options:bt,recentOptionIds:yt,placeholder:"Project",noneLabel:"No project",searchPlaceholder:"Search projects...",emptyMessage:"No projects found.",onChange:s(e=>{e&&Bt(e),R(r=>({...r,projectId:e}))},"onChange"),onConfirm:s(()=>i.current?.focus(),"onConfirm"),renderTriggerValue:s(e=>e&&Qe?(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)("span",{className:"h-3.5 w-3.5 shrink-0 rounded-sm",style:{backgroundColor:Qe.color??"var(--project-none)"}}),(0,t.jsx)("span",{className:"truncate",children:e.label})]}):(0,t.jsx)("span",{className:"text-muted-foreground",children:"Project"}),"renderTriggerValue"),renderOption:s(e=>{if(!e.id)return(0,t.jsx)("span",{className:"truncate",children:e.label});const r=U.get(e.id);return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)("span",{className:"h-3.5 w-3.5 shrink-0 rounded-sm",style:{backgroundColor:r?.color??"var(--project-none)"}}),(0,t.jsx)("span",{className:"truncate",children:e.label})]})},"renderOption")}),(0,t.jsx)("span",{children:"filed in"}),(0,t.jsxs)(ve,{value:p.folderId??"__unfiled",onValueChange:s(e=>R(r=>({...r,folderId:e==="__unfiled"?null:e})),"onValueChange"),children:[(0,t.jsx)(je,{className:"h-8 w-auto min-w-32 border-0 bg-muted/50 px-2 shadow-none",children:(0,t.jsx)(ye,{})}),(0,t.jsxs)(be,{children:[(0,t.jsx)(Z,{value:"__unfiled",children:"Unfiled"}),(h?.folders??[]).map(e=>(0,t.jsx)(Z,{value:e.id,children:e.name},e.id))]})]})]})})}),(0,t.jsx)("div",{className:"border-t border-border/60 px-5 py-4",children:(0,t.jsx)(Xt,{ref:i,value:p.description,onChange:s(e=>R(r=>({...r,description:e})),"onChange"),placeholder:"Add instructions...",bordered:!1,contentClassName:"min-h-(--sz-160px) text-sm text-muted-foreground",mentions:ht,onSubmit:s(()=>{!N.isPending&&p.title.trim()&&p.projectId&&p.assigneeAgentId&&N.mutate()},"onSubmit")})}),(0,t.jsx)("div",{className:"border-t border-border/60 px-5 py-3",children:(0,t.jsxs)(He,{open:Se,onOpenChange:ie,children:[(0,t.jsxs)(lt,{className:"flex w-full items-center justify-between text-left",children:[(0,t.jsxs)("div",{children:[(0,t.jsx)("p",{className:"text-sm font-medium",children:"Advanced delivery settings"}),(0,t.jsx)("p",{className:"text-sm text-muted-foreground",children:"Keep policy controls secondary to the work definition."})]}),Se?(0,t.jsx)(ss,{className:"h-4 w-4 text-muted-foreground"}):(0,t.jsx)(ct,{className:"h-4 w-4 text-muted-foreground"})]}),(0,t.jsx)(Je,{className:"pt-3",children:(0,t.jsxs)("div",{className:"grid gap-4 md:grid-cols-2",children:[(0,t.jsxs)("div",{className:"space-y-2",children:[(0,t.jsx)("p",{className:"text-xs font-medium uppercase tracking-(--tracking-caps) text-muted-foreground",children:"Concurrency"}),(0,t.jsxs)(ve,{value:p.concurrencyPolicy,onValueChange:s(e=>R(r=>({...r,concurrencyPolicy:e})),"onValueChange"),children:[(0,t.jsx)(je,{children:(0,t.jsx)(ye,{})}),(0,t.jsx)(be,{children:Ss.map(e=>(0,t.jsx)(Z,{value:e,children:e.replaceAll("_"," ")},e))})]}),(0,t.jsx)("p",{className:"text-xs text-muted-foreground",children:ks[p.concurrencyPolicy]})]}),(0,t.jsxs)("div",{className:"space-y-2",children:[(0,t.jsx)("p",{className:"text-xs font-medium uppercase tracking-(--tracking-caps) text-muted-foreground",children:"Catch-up"}),(0,t.jsxs)(ve,{value:p.catchUpPolicy,onValueChange:s(e=>R(r=>({...r,catchUpPolicy:e})),"onValueChange"),children:[(0,t.jsx)(je,{children:(0,t.jsx)(ye,{})}),(0,t.jsx)(be,{children:Fs.map(e=>(0,t.jsx)(Z,{value:e,children:e.replaceAll("_"," ")},e))})]}),(0,t.jsx)("p",{className:"text-xs text-muted-foreground",children:Ps[p.catchUpPolicy]})]})]})})]})})]}),(0,t.jsxs)("div",{className:"shrink-0 flex flex-col gap-3 border-t border-border/60 px-5 py-4 sm:flex-row sm:items-center sm:justify-between",children:[(0,t.jsx)("div",{className:"text-sm text-muted-foreground",children:"After creation, Paperclip takes you straight to trigger setup. Draft routines stay paused until you add a default agent."}),(0,t.jsxs)("div",{className:"flex flex-col gap-2 sm:items-end",children:[(0,t.jsxs)(F,{onClick:s(()=>N.mutate(),"onClick"),disabled:N.isPending||!p.title.trim(),children:[(0,t.jsx)(J,{className:"mr-2 h-4 w-4"}),N.isPending?"Creating...":"Create routine"]}),N.isError?(0,t.jsx)("p",{className:"text-sm text-destructive",children:N.error instanceof Error?N.error.message:"Failed to create routine"}):null]})]})]})}),ae?(0,t.jsx)(cs,{children:(0,t.jsx)(ls,{className:"pt-6 text-sm text-destructive",children:ae instanceof Error?ae.message:"Failed to load routines"})}):null,P==="routines"?(0,t.jsxs)("div",{className:Pt(fe&&"flex gap-4"),children:[fe?(0,t.jsx)(hs,{result:pe,selection:f,allLabel:"All routines",itemLabelPlural:"routines",loading:Pe,onSelect:Y,onCreate:s(()=>T(),"onCreate"),onRename:s((e,r)=>ue.mutate({folderId:e.id,payload:{name:r}}),"onRename"),onEdit:s(e=>{L(e),K(!0)},"onEdit"),onDelete:se}):null,(0,t.jsxs)("div",{className:"min-w-0 flex-1",children:[u.groupBy==="folder"&&W?(0,t.jsx)("div",{className:"mb-3 flex flex-wrap items-center gap-2",children:f==="all"?(0,t.jsx)(Us,{label:"All routines",count:D.length}):(0,t.jsxs)("div",{className:"flex min-w-0 items-center gap-2 text-sm",children:[(0,t.jsx)(vs,{color:Be?.color}),(0,t.jsx)("span",{className:"truncate font-medium",children:f==="unfiled"?"Unfiled":Be?.name??"Folder"}),(0,t.jsxs)("span",{className:"text-muted-foreground",children:[D.length," routine",D.length===1?"":"s"]})]})}):null,u.groupBy==="folder"&&!W&&!Pe&&y.length>0?(0,t.jsx)(ps,{storageKey:`paperclip:routines-folder-nudge:${n??"none"}`,itemLabelPlural:"routines",onCreateFolder:s(()=>T(),"onCreateFolder")}):null,ne?(0,t.jsx)(gs,{selectedCount:B.length,folders:h?.folders??[],onMove:s(e=>{Rt(e)},"onMove"),onCreateAndMove:s(()=>T(B),"onCreateAndMove"),onClear:s(()=>V([]),"onClear"),onDone:s(()=>{re(!1),V([])},"onDone")}):null,y.length===0?(0,t.jsx)("div",{className:"py-12",children:(0,t.jsx)(we,{icon:ee,message:"No active routines. Use Create routine to define the first recurring workflow."})}):D.length===0?(0,t.jsxs)("div",{className:"py-12",children:[(0,t.jsx)(we,{icon:ee,message:f==="all"?"No routines match this view.":"This folder is empty."}),f!=="all"?(0,t.jsx)("div",{className:"mt-3 flex justify-center",children:(0,t.jsxs)(F,{size:"sm",onClick:$e,children:[(0,t.jsx)(J,{className:"mr-2 h-3.5 w-3.5"}),"New routine in this folder"]})}):null]}):(0,t.jsx)("div",{className:"flex flex-col gap-3",children:Nt.map(e=>{const r=!u.collapsedGroups.includes(e.key);return(0,t.jsxs)(He,{open:r,onOpenChange:s(a=>{ge({collapsedGroups:a?u.collapsedGroups.filter(v=>v!==e.key):[...u.collapsedGroups,e.key]})},"onOpenChange"),children:[e.label?(0,t.jsx)(Es,{label:e.label,count:e.items.length,isOpen:r}):null,(0,t.jsx)(Je,{children:e.items.map(a=>(0,t.jsx)(Cs,{routine:a,projectById:U,agentById:E,runningRoutineId:ut,statusMutationRoutineId:mt,href:`/routines/${a.id}`,runNowButton:!0,divider:!1,onRunNow:_t,onToggleEnabled:Ct,onToggleArchived:St,selectMode:ne,selected:B.includes(a.id),onSelectChange:s((v,xe)=>{V(H=>xe?Array.from(new Set([...H,v.id])):H.filter(Ft=>Ft!==v.id))},"onSelectChange"),extraMenuItems:(0,t.jsx)(us,{folders:h?.folders??[],currentFolderId:a.folderId??null,onMove:s(v=>{const xe=a.folderId??null;qe.mutate({itemId:a.id,folderId:v}),o({title:"Routine moved",body:v?`Moved "${a.title}" to ${h?.folders.find(H=>H.id===v)?.name??"folder"}.`:`Moved "${a.title}" to Unfiled.`,tone:"success",action:{label:"Undo",onClick:s(()=>qe.mutate({itemId:a.id,folderId:xe}),"onClick")}})},"onMove"),onCreateAndMove:s(()=>T([a.id]),"onCreateAndMove")})},a.id))})]},e.key)})})]})]}):null,(0,t.jsx)(ys,{open:O,kind:"routine",folder:te,pending:De.isPending||ue.isPending,onOpenChange:K,onSubmit:s(e=>{te?ue.mutate({folderId:te.id,payload:e}):De.mutate(e)},"onSubmit")}),(0,t.jsx)(Rs,{open:Q!==null,folder:Q,itemLabelPlural:"routines",pending:Te.isPending,onOpenChange:s(e=>{e||se(null)},"onOpenChange"),onConfirm:s(()=>{Q&&Te.mutate(Q.id)},"onConfirm")}),(0,t.jsx)(xs,{open:dt,onOpenChange:Ne,result:pe,selection:f,allLabel:"All routines",itemLabelPlural:"Routines",onSelect:Y,onCreate:s(()=>T(),"onCreate")}),(0,t.jsx)(Yt,{open:k!==null,onOpenChange:s(e=>{e||oe(null)},"onOpenChange"),companyId:n,routineName:k?.title??null,agents:_??[],projects:C??[],defaultProjectId:k?.projectId??null,defaultAssigneeAgentId:k?.assigneeAgentId??null,variables:k?.variables??[],isPending:Ee.isPending,onSubmit:s(e=>{k&&Ee.mutate({id:k.id,data:e})},"onSubmit")})]}):(0,t.jsx)(we,{icon:ee,message:"Select a company to view routines."})}s(un,"Routines");function Us({label:n,count:c}){return(0,t.jsxs)("div",{className:"flex min-w-0 items-center gap-2 text-sm",children:[(0,t.jsx)(ee,{className:"h-3.5 w-3.5 text-muted-foreground"}),(0,t.jsx)("span",{className:"truncate font-medium",children:n}),(0,t.jsxs)("span",{className:"text-muted-foreground",children:[c," routine",c===1?"":"s"]})]})}s(Us,"FolderIconHeader");export{un as Routines};
1
+ var kt=Object.defineProperty;var s=(n,c)=>kt(n,"name",{value:c,configurable:!0});import{i as at}from"./rolldown-runtime-ZpX3ckPj.js";import{G as Pt,Qa as At,St as m,Za as Mt,_a as S,eo as Dt,ht as Tt,va as A}from"./boxes-Bpd3vwBa.js";import{Bn as qt,Vt as ze,Wt as Ge,d as Kt,fn as Et,ln as We,n as Ut,pt as Ot,r as F,st as J,zt as Ye}from"./BreadcrumbContext-DP0mgUIg.js";import{_ as He,v as Je,y as lt}from"./SearchableSelect-CyZJenSa.js";import{Ft as Lt,It as Qt,Mt as Bt,Nr as ee,Nt as Vt,Rr as $t,V as zt,jt as Gt,r as Wt,t as Yt,wt as Xe,x as he}from"./RoutineRunVariablesDialog-CrC88u8T.js";import{t as Ze}from"./AgentAvatar-D4YQ4myL.js";import{r as Ht,t as Jt}from"./dialog-aW5qVpwP.js";import{l as X,t as Xt}from"./MarkdownEditor-DHz7g42W.js";import{n as et,t as Zt}from"./tabs-D69nzsXS.js";import{h as es}from"./StatusIcon-C_7wHYAC.js";import{t as ts}from"./check-2BJzr5lo.js";import{t as ss}from"./chevron-down-C8ZdH7h9.js";import{a as ct}from"./clipboard-B6GwC8zN.js";import{a as Z,c as je,l as ye,n as ve,r as be}from"./select-CV4NIBut.js";import{t as ns}from"./heartbeats-CpU47ZZC.js";import{a as rs}from"./ToastContext-YqYQB596.js";import{t as os}from"./agents-BXUtBaur.js";import{t as is}from"./access-CspWWirX.js";import{i as as}from"./company-members-Duugb3uG.js";import{r as ls,t as cs}from"./card-B_2b5Riw.js";import{t as we}from"./EmptyState-BN5HM6-K.js";import{t as ds}from"./PageSkeleton-BiXcKvFW.js";import{$ as us,$t as ms,G as ps,J as fs,K as gs,Q as xs,X as hs,Xt as js,Y as ys,Z as vs,dt as bs,en as ws,et as Ns,ft as M,nt as Is,q as Rs,tt as _s,ut as Cs}from"./index-Yr0PvgVh.js";var l=at(Dt(),1),t=at(At(),1),Ss=["coalesce_if_active","always_enqueue","skip_if_active"],Fs=["skip_missed","enqueue_missed_with_cap"],ks={coalesce_if_active:"If a run is already active, keep just one follow-up run queued.",always_enqueue:"Queue every trigger occurrence, even if the routine is already running.",skip_if_active:"Drop new trigger occurrences while a run is still active."},Ps={skip_missed:"Ignore windows that were missed while the scheduler or routine was paused.",enqueue_missed_with_cap:"Catch up missed schedule windows after recovery; sub-hourly schedules are combined into one catch-up run, slower schedules replay each missed window up to a cap."};function tt(n){n&&(n.style.height="auto",n.style.height=`${n.scrollHeight}px`)}s(tt,"autoResizeTextarea");var As="__built_in_routines",st={sortField:"title",sortDir:"asc",groupBy:"folder",collapsedGroups:[]};function nt(n){try{const c=localStorage.getItem(n);if(c)return{...st,...JSON.parse(c)}}catch{}return{...st}}s(nt,"getRoutineViewState");function Ms(n,c){localStorage.setItem(n,JSON.stringify(c))}s(Ms,"saveRoutineViewState");function q(n){if(!n)return Number.NEGATIVE_INFINITY;const c=new Date(n).getTime();return Number.isFinite(c)?c:Number.NEGATIVE_INFINITY}s(q,"timestampValue");function rt(n,c){return(n??"").localeCompare(c??"",void 0,{sensitivity:"base"})}s(rt,"compareNullableText");function Ds(n){return{...n,description:n.description.trim()||null,projectId:n.projectId||null,folderId:n.folderId||null,assigneeAgentId:n.assigneeAgentId||null}}s(Ds,"buildRoutineMutationPayload");function Ts(n,c,d,b,g){if(c==="none")return[{key:"__all",label:null,items:n}];if(c==="folder"){const o=he(n,i=>i.folderId??"__unfiled");return Object.keys(o).sort((i,j)=>{if(i==="__unfiled"||j==="__unfiled")return i===j?0:i==="__unfiled"?1:-1;const w=g.get(i),I=g.get(j),O=(Number.isFinite(w?.position)?w.position:Number.POSITIVE_INFINITY)-(Number.isFinite(I?.position)?I.position:Number.POSITIVE_INFINITY);return O!==0?O:(w?.name??"Unknown folder").localeCompare(I?.name??"Unknown folder",void 0,{sensitivity:"base"})||i.localeCompare(j)}).map(i=>({key:i,label:i==="__unfiled"?"Unfiled":g.get(i)?.name??"Unknown folder",items:o[i]}))}if(c==="project"){const o=he(n,i=>i.projectId??"__no_project");return Object.keys(o).sort((i,j)=>{const w=i==="__no_project"?"No project":d.get(i)?.name??"Unknown project",I=j==="__no_project"?"No project":d.get(j)?.name??"Unknown project";return w.localeCompare(I)}).map(i=>({key:i,label:i==="__no_project"?"No project":d.get(i)?.name??"Unknown project",items:o[i]}))}const x=he(n,o=>o.assigneeAgentId??"__unassigned");return Object.keys(x).sort((o,i)=>{const j=o==="__unassigned"?"Unassigned":b.get(o)?.name??"Unknown agent",w=i==="__unassigned"?"Unassigned":b.get(i)?.name??"Unknown agent";return j.localeCompare(w)}).map(o=>({key:o,label:o==="__unassigned"?"Unassigned":b.get(o)?.name??"Unknown agent",items:x[o]}))}s(Ts,"buildRoutineGroups");function ot(n){return n.originKind==="built_in_agent_bundle"}s(ot,"isBuiltInRoutine");function qs(n,c,d,b,g){const x=n.filter(ot),o=Ts(n.filter(i=>!ot(i)),c,d,b,g).filter(i=>i.items.length>0).map(i=>x.length>0&&c==="none"&&i.key==="__all"?{...i,label:"Custom routines"}:i);return x.length===0?o:[...o,{key:As,label:"Built-in routines",items:x}]}s(qs,"buildRoutineSections");function Ks(n,c,d){const b=d==="asc"?1:-1;return[...n].sort((g,x)=>{let o=0;return c==="title"?o=rt(g.title,x.title):c==="created"?o=q(g.createdAt)-q(x.createdAt):c==="lastRun"?o=q(g.lastRun?.triggeredAt??g.lastTriggeredAt)-q(x.lastRun?.triggeredAt??x.lastTriggeredAt):o=q(g.updatedAt)-q(x.updatedAt),o!==0?o*b:rt(g.title,x.title)})}s(Ks,"sortRoutines");function it(n){return n==="runs"?"/routines?tab=runs":"/routines"}s(it,"buildRoutinesTabHref");function Es({label:n,count:c,isOpen:d}){return(0,t.jsxs)("div",{className:`flex items-center gap-2 rounded-lg border border-border px-3 py-2${d?" mb-1":""}`,children:[(0,t.jsxs)(lt,{className:"flex items-center gap-1.5",children:[(0,t.jsx)(ct,{className:"h-3.5 w-3.5 shrink-0 text-muted-foreground transition-transform [[data-state=open]>&]:rotate-90"}),(0,t.jsx)("span",{className:"text-sm font-semibold uppercase tracking-wide",children:n})]}),(0,t.jsx)("span",{className:"text-xs text-muted-foreground",children:c})]})}s(Es,"RoutineSectionHeader");function un(){const{selectedCompanyId:n}=Tt(),{setBreadcrumbs:c}=Ut(),d=Mt(),b=Kt(),[g,x]=qt(),{pushToast:o}=rs(),i=(0,l.useRef)(null),j=(0,l.useRef)(null),w=(0,l.useRef)(null),I=(0,l.useRef)(null),[O,K]=(0,l.useState)(!1),[te,L]=(0,l.useState)(null),[Q,se]=(0,l.useState)(null),[dt,Ne]=(0,l.useState)(!1),[ne,re]=(0,l.useState)(!1),[B,V]=(0,l.useState)([]),[Ie,Re]=(0,l.useState)([]),[ut,_e]=(0,l.useState)(null),[mt,Ce]=(0,l.useState)(null),[k,oe]=(0,l.useState)(null),[$,z]=(0,l.useState)(!1),[Se,ie]=(0,l.useState)(!1),P=g.get("tab")==="runs"?"runs":"routines",[p,R]=(0,l.useState)({title:"",description:"",projectId:"",folderId:null,assigneeAgentId:"",priority:"medium",concurrencyPolicy:"coalesce_if_active",catchUpPolicy:"skip_missed",variables:[]}),G=n?`paperclip:routines-view:${n}`:"paperclip:routines-view",[u,Fe]=(0,l.useState)(()=>nt(G)),f=_s(g.get("folder"));(0,l.useEffect)(()=>{c([{label:"Routines"}])},[c]),(0,l.useEffect)(()=>{Fe(nt(G))},[G]);const{data:ke,isLoading:pt,error:ae}=A({queryKey:m.routines.list(n),queryFn:s(()=>X.list(n),"queryFn"),enabled:!!n}),{data:h,isLoading:Pe}=A({queryKey:m.folders.list(n,"routine"),queryFn:s(()=>M.list(n,"routine"),"queryFn"),enabled:!!n&&P==="routines"}),{data:_}=A({queryKey:m.agents.list(n),queryFn:s(()=>os.list(n),"queryFn"),enabled:!!n}),{data:C}=A({queryKey:m.projects.list(n,{includeArchived:!0}),queryFn:s(()=>es.list(n,{includeArchived:!0}),"queryFn"),enabled:!!n}),{data:Ae}=A({queryKey:m.access.companyUserDirectory(n),queryFn:s(()=>is.listUserDirectory(n),"queryFn"),enabled:!!n}),{data:le,isLoading:ft,error:gt}=A({queryKey:[...m.issues.list(n),"routine-executions"],queryFn:s(()=>We.list(n,{originKind:"routine_execution"}),"queryFn"),enabled:!!n&&P==="runs"}),Me=m.liveRuns(n),ce=ws({companyId:n,resourceKey:"live-runs",queryKey:Me,enabled:!!n&&P==="runs",refetchInterval:!1,leaderOnly:!0}),{data:de,dataUpdatedAt:xt}=A({queryKey:Me,queryFn:s(()=>ns.liveRunsForCompany(n),"queryFn"),enabled:ce.enabled,refetchInterval:ce.refetchInterval});ms(ce,de,xt),(0,l.useEffect)(()=>{tt(j.current)},[p.title,$]);const ht=(0,l.useMemo)(()=>as({agents:_,projects:(C??[]).filter(e=>!e.archivedAt),members:Ae?.users}),[_,Ae?.users,C]),N=S({mutationFn:s(()=>X.create(n,Ds(p)),"mutationFn"),onSuccess:s(async e=>{R({title:"",description:"",projectId:"",folderId:null,assigneeAgentId:"",priority:"medium",concurrencyPolicy:"coalesce_if_active",catchUpPolicy:"skip_missed",variables:[]}),z(!1),ie(!1),await d.invalidateQueries({queryKey:m.routines.list(n)}),o({title:"Routine created",body:e.assigneeAgentId?"Add the first trigger to turn it into a live workflow.":"Draft saved. Add a default agent before enabling automation.",tone:"success"}),b(`/routines/${e.id}?tab=triggers`)},"onSuccess")}),De=S({mutationFn:s(e=>M.create(n,{kind:"routine",...e}),"mutationFn"),onSuccess:s(async e=>{if(K(!1),L(null),await d.invalidateQueries({queryKey:m.folders.list(n,"routine")}),Ie.length>0){const r=Ie;Re([]);try{await Promise.all(r.map(a=>M.moveItem(n,{kind:"routine",itemId:a,folderId:e.id}))),await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.folders.list(n,"routine")})])}catch(a){o({title:"Folder created, move failed",body:a instanceof Error?a.message:"Paperclip could not move the selected routines.",tone:"error"});return}}else Y(e.id);o({title:"Folder created",body:e.name,tone:"success"})},"onSuccess"),onError:s(e=>{o({title:"Failed to save folder",body:e instanceof Error?e.message:"Paperclip could not save the folder.",tone:"error"})},"onError")}),ue=S({mutationFn:s(({folderId:e,payload:r})=>M.update(n,e,r),"mutationFn"),onSuccess:s(async()=>{K(!1),L(null),await d.invalidateQueries({queryKey:m.folders.list(n,"routine")})},"onSuccess"),onError:s(e=>{o({title:"Folder save failed",body:e instanceof Error?e.message:"Paperclip could not update the folder.",tone:"error"})},"onError")}),Te=S({mutationFn:s(e=>M.delete(n,e),"mutationFn"),onSuccess:s(async(e,r)=>{f===r&&Y("all"),se(null),await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.folders.list(n,"routine")})]),o({title:"Folder deleted",body:"Items moved to Unfiled.",tone:"success"})},"onSuccess"),onError:s(e=>{o({title:"Folder delete failed",body:e instanceof Error?e.message:"Paperclip could not delete the folder.",tone:"error"})},"onError")}),qe=S({mutationFn:s(({itemId:e,folderId:r})=>M.moveItem(n,{kind:"routine",itemId:e,folderId:r}),"mutationFn"),onSuccess:s(async()=>{await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.folders.list(n,"routine")})])},"onSuccess"),onError:s(e=>{o({title:"Move failed",body:e instanceof Error?e.message:"Paperclip could not move the routine.",tone:"error"})},"onError")}),jt=S({mutationFn:s(({id:e,data:r})=>We.update(e,r),"mutationFn"),onSuccess:s(async()=>{await d.invalidateQueries({queryKey:[...m.issues.list(n),"routine-executions"]})},"onSuccess")}),Ke=S({mutationFn:s(({id:e,status:r})=>X.update(e,{status:r}),"mutationFn"),onMutate:s(({id:e})=>{Ce(e)},"onMutate"),onSuccess:s(async(e,r)=>{await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.routines.detail(r.id)})])},"onSuccess"),onSettled:s(()=>{Ce(null)},"onSettled"),onError:s(e=>{o({title:"Failed to update routine",body:e instanceof Error?e.message:"Paperclip could not update the routine.",tone:"error"})},"onError")}),Ee=S({mutationFn:s(({id:e,data:r})=>X.run(e,{...r?.variables&&Object.keys(r.variables).length>0?{variables:r.variables}:{},...r?.assigneeAgentId!==void 0?{assigneeAgentId:r.assigneeAgentId}:{},...r?.projectId!==void 0?{projectId:r.projectId}:{},...r?.executionWorkspaceId!==void 0?{executionWorkspaceId:r.executionWorkspaceId}:{},...r?.executionWorkspacePreference!==void 0?{executionWorkspacePreference:r.executionWorkspacePreference}:{},...r?.executionWorkspaceSettings!==void 0?{executionWorkspaceSettings:r.executionWorkspaceSettings}:{}}),"mutationFn"),onMutate:s(({id:e})=>{_e(e)},"onMutate"),onSuccess:s(async(e,{id:r})=>{oe(null),await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.routines.detail(r)})])},"onSuccess"),onSettled:s(()=>{_e(null)},"onSettled"),onError:s(e=>{o({title:"Routine run failed",body:e instanceof Error?e.message:"Paperclip could not start the routine run.",tone:"error"})},"onError")}),me=(0,l.useMemo)(()=>Vt(),[$]),yt=(0,l.useMemo)(()=>Gt(),[$]),vt=(0,l.useMemo)(()=>Lt((_??[]).filter(e=>e.status!=="terminated"),me).map(e=>({id:e.id,label:e.name,searchText:`${e.name} ${e.role} ${e.title??""}`})),[_,me]),bt=(0,l.useMemo)(()=>(C??[]).filter(e=>!e.archivedAt).map(e=>({id:e.id,label:e.name,searchText:e.description??""})),[C]),E=(0,l.useMemo)(()=>new Map((_??[]).map(e=>[e.id,e])),[_]),U=(0,l.useMemo)(()=>new Map((C??[]).map(e=>[e.id,e])),[C]),Ue=(0,l.useMemo)(()=>new Map((h?.folders??[]).map(e=>[e.id,e])),[h]),wt=(0,l.useMemo)(()=>zt(de,le),[de,le]),y=(0,l.useMemo)(()=>(ke??[]).filter(e=>e.status!=="archived"),[ke]),Oe=(0,l.useMemo)(()=>u.groupBy!=="folder"||f==="all"?y:f==="unfiled"?y.filter(e=>!e.folderId):y.filter(e=>e.folderId===f),[f,u.groupBy,y]),pe=(0,l.useMemo)(()=>{if(!h)return h;const e=new Map;let r=0;for(const a of y)a.folderId?e.set(a.folderId,(e.get(a.folderId)??0)+1):r+=1;return{...h,allCount:y.length,unfiledCount:r,folders:h.folders.map(a=>({...a,itemCount:e.get(a.id)??0}))}},[h,y]),D=(0,l.useMemo)(()=>Ks(Oe,u.sortField,u.sortDir),[Oe,u.sortDir,u.sortField]),Nt=(0,l.useMemo)(()=>qs(D,u.groupBy,U,E,Ue),[E,Ue,U,u.groupBy,D]),It=(0,l.useMemo)(()=>Et("Recent Runs",it("runs"),"issues"),[]),Le=p.assigneeAgentId?E.get(p.assigneeAgentId)??null:null,Qe=p.projectId?U.get(p.projectId)??null:null,Be=Is(h?.folders??[],f),W=(h?.folders.length??0)>0,fe=P==="routines"&&u.groupBy==="folder"&&W;function ge(e){Fe(r=>{const a={...r,...e};return Ms(G,a),a})}s(ge,"updateRoutineView");function Ve(e){const r=e==="runs"?"runs":"routines";(0,l.startTransition)(()=>{b(it(r))})}s(Ve,"handleTabChange");function Y(e){x(r=>{const a=new URLSearchParams(r),v=Ns(e);return v?a.set("folder",v):a.delete("folder"),a})}s(Y,"setFolderSelection");function T(e=[]){Re(e),L(null),K(!0)}s(T,"openCreateFolder");function $e(){R(e=>({...e,folderId:f==="all"||f==="unfiled"?null:f})),z(!0)}s($e,"openCreateRoutine");async function Rt(e){const r=B;if(r.length!==0)try{await Promise.all(r.map(a=>M.moveItem(n,{kind:"routine",itemId:a,folderId:e}))),V([]),re(!1),await Promise.all([d.invalidateQueries({queryKey:m.routines.list(n)}),d.invalidateQueries({queryKey:m.folders.list(n,"routine")})]),o({title:"Routines moved",body:`${r.length} routine${r.length===1?"":"s"} filed.`,tone:"success"})}catch(a){o({title:"Failed to move routines",body:a instanceof Error?a.message:"Paperclip could not move the selected routines.",tone:"error"})}}s(Rt,"moveSelectedRoutines");function _t(e){oe(e)}s(_t,"handleRunNow");function Ct(e,r){if(!r&&!e.assigneeAgentId){o({title:"Default agent required",body:"Set a default agent before enabling routine automation.",tone:"warn"});return}Ke.mutate({id:e.id,status:bs(e.status,!r)})}s(Ct,"handleToggleEnabled");function St(e){Ke.mutate({id:e.id,status:e.status==="archived"?"active":"archived"})}return s(St,"handleToggleArchived"),n?pt?(0,t.jsx)(ds,{variant:"issues-list"}):(0,t.jsxs)("div",{className:"space-y-6",children:[(0,t.jsxs)("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between",children:[(0,t.jsxs)("div",{className:"space-y-1",children:[(0,t.jsx)("h1",{className:"text-2xl font-semibold tracking-tight",children:"Routines"}),(0,t.jsx)("p",{className:"text-sm text-muted-foreground",children:"Recurring work definitions that materialize into auditable execution tasks."})]}),(0,t.jsxs)(F,{onClick:$e,children:[(0,t.jsx)(J,{className:"mr-2 h-4 w-4"}),"Create routine"]})]}),(0,t.jsxs)(Zt,{value:P,onValueChange:Ve,children:[(0,t.jsx)(js,{align:"start",value:P,onValueChange:Ve,items:[{value:"routines",label:"Routines"},{value:"runs",label:"Recent Runs"}]}),(0,t.jsxs)(et,{value:"routines",className:"space-y-4",children:[(0,t.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,t.jsxs)("p",{className:"text-sm text-muted-foreground",children:[y.length," routine",y.length===1?"":"s"]}),(0,t.jsxs)("div",{className:"flex items-center gap-1",children:[(0,t.jsxs)(Ye,{children:[(0,t.jsx)(Ge,{asChild:!0,children:(0,t.jsxs)(F,{variant:"ghost",size:"sm",className:"text-xs",title:"Sort",children:[(0,t.jsx)($t,{className:"h-3.5 w-3.5 sm:h-3 sm:w-3 sm:mr-1"}),(0,t.jsx)("span",{className:"hidden sm:inline",children:"Sort"})]})}),(0,t.jsx)(ze,{align:"end",className:"w-44 p-0",children:(0,t.jsx)("div",{className:"p-2 space-y-0.5",children:[["updated","Updated"],["created","Created"],["lastRun","Last run"],["title","Title"]].map(([e,r])=>(0,t.jsxs)("button",{className:`flex w-full items-center justify-between rounded-sm px-2 py-1.5 text-sm ${u.sortField===e?"bg-accent/50 text-foreground":"text-muted-foreground hover:bg-accent/50"}`,onClick:s(()=>{ge(u.sortField===e?{sortDir:u.sortDir==="asc"?"desc":"asc"}:{sortField:e,sortDir:e==="title"?"asc":"desc"})},"onClick"),children:[(0,t.jsx)("span",{children:r}),u.sortField===e?(0,t.jsx)("span",{className:"text-xs text-muted-foreground",children:u.sortDir==="asc"?"Asc":"Desc"}):null]},e))})})]}),(0,t.jsxs)(Ye,{children:[(0,t.jsx)(Ge,{asChild:!0,children:(0,t.jsxs)(F,{variant:"ghost",size:"sm",className:"text-xs",title:"Group",children:[(0,t.jsx)(Ot,{className:"h-3.5 w-3.5 sm:h-3 sm:w-3 sm:mr-1"}),(0,t.jsx)("span",{className:"hidden sm:inline",children:"Group"})]})}),(0,t.jsx)(ze,{align:"end",className:"w-44 p-0",children:(0,t.jsx)("div",{className:"p-2 space-y-0.5",children:[["folder","Folder"],["project","Project"],["assignee","Agent"],["none","None"]].map(([e,r])=>(0,t.jsxs)("button",{className:`flex w-full items-center justify-between rounded-sm px-2 py-1.5 text-sm ${u.groupBy===e?"bg-accent/50 text-foreground":"text-muted-foreground hover:bg-accent/50"}`,onClick:s(()=>ge({groupBy:e,collapsedGroups:[]}),"onClick"),children:[(0,t.jsx)("span",{children:r}),u.groupBy===e?(0,t.jsx)(ts,{className:"h-3.5 w-3.5"}):null]},e))})})]}),u.groupBy==="folder"&&!W?(0,t.jsxs)(F,{variant:"outline",size:"sm",onClick:s(()=>T(),"onClick"),children:[(0,t.jsx)(J,{className:"mr-2 h-3.5 w-3.5"}),"New folder"]}):null,fe?(0,t.jsx)(F,{variant:"ghost",size:"sm",className:"text-xs",onClick:s(()=>re(e=>!e),"onClick"),children:ne?"Done":"Select"}):null]})]}),u.groupBy==="folder"?(0,t.jsx)("div",{className:"md:hidden",children:(0,t.jsx)(fs,{result:pe,selection:f,allLabel:"All routines",onClick:s(()=>Ne(!0),"onClick")})}):null]}),(0,t.jsx)(et,{value:"runs",children:(0,t.jsx)(Wt,{issues:le??[],isLoading:ft,error:gt,agents:_,projects:C,liveIssueIds:wt,viewStateKey:"paperclip:routine-recent-runs-view",issueLinkState:It,onUpdateIssue:s((e,r)=>jt.mutate({id:e,data:r}),"onUpdateIssue")})})]}),(0,t.jsx)(Jt,{open:$,onOpenChange:s(e=>{N.isPending||z(e)},"onOpenChange"),children:(0,t.jsxs)(Ht,{showCloseButton:!1,className:"flex max-h-(--sz-calc-18) max-w-3xl flex-col gap-0 overflow-hidden p-0",children:[(0,t.jsxs)("div",{className:"shrink-0 flex flex-wrap items-center justify-between gap-3 border-b border-border/60 px-5 py-3",children:[(0,t.jsxs)("div",{children:[(0,t.jsx)("p",{className:"text-xs font-medium uppercase tracking-(--tracking-caps) text-muted-foreground",children:"New routine"}),(0,t.jsx)("p",{className:"text-sm text-muted-foreground",children:"Define the recurring work first. Default project and agent are optional for draft routines."})]}),(0,t.jsx)(F,{variant:"ghost",size:"sm",onClick:s(()=>{z(!1),ie(!1)},"onClick"),disabled:N.isPending,children:"Cancel"})]}),(0,t.jsxs)("div",{className:"min-h-0 flex-1 overflow-y-auto",children:[(0,t.jsx)("div",{className:"px-5 pt-5 pb-3",children:(0,t.jsx)("textarea",{ref:j,className:"w-full resize-none overflow-hidden bg-transparent text-xl font-semibold outline-none placeholder:text-muted-foreground/50",placeholder:"Routine title",rows:1,value:p.title,onChange:s(e=>{R(r=>({...r,title:e.target.value})),tt(e.target)},"onChange"),onKeyDown:s(e=>{if(e.key==="Enter"&&!e.metaKey&&!e.ctrlKey&&!e.nativeEvent.isComposing){e.preventDefault(),i.current?.focus();return}e.key==="Tab"&&!e.shiftKey&&(e.preventDefault(),p.assigneeAgentId?p.projectId?i.current?.focus():I.current?.focus():w.current?.focus())},"onKeyDown"),autoFocus:!0})}),(0,t.jsx)("div",{className:"px-5 pb-3",children:(0,t.jsx)("div",{className:"overflow-x-auto overscroll-x-contain",children:(0,t.jsxs)("div",{className:"inline-flex min-w-full flex-wrap items-center gap-2 text-sm text-muted-foreground sm:min-w-max sm:flex-nowrap",children:[(0,t.jsx)("span",{children:"For"}),(0,t.jsx)(Xe,{ref:w,value:p.assigneeAgentId,options:vt,recentOptionIds:me,placeholder:"Responsible",noneLabel:"No responsible",searchPlaceholder:"Search responsible...",emptyMessage:"No responsible found.",onChange:s(e=>{e&&Qt(e),R(r=>({...r,assigneeAgentId:e}))},"onChange"),onConfirm:s(()=>{p.projectId?i.current?.focus():I.current?.focus()},"onConfirm"),renderTriggerValue:s(e=>e?Le?(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(Ze,{agent:Le,size:16,className:"h-3.5 w-3.5 shrink-0 text-muted-foreground"}),(0,t.jsx)("span",{className:"truncate",children:e.label})]}):(0,t.jsx)("span",{className:"truncate",children:e.label}):(0,t.jsx)("span",{className:"text-muted-foreground",children:"Responsible"}),"renderTriggerValue"),renderOption:s(e=>{if(!e.id)return(0,t.jsx)("span",{className:"truncate",children:e.label});const r=E.get(e.id);return(0,t.jsxs)(t.Fragment,{children:[r?(0,t.jsx)(Ze,{agent:r,size:16,className:"h-3.5 w-3.5 shrink-0 text-muted-foreground"}):null,(0,t.jsx)("span",{className:"truncate",children:e.label})]})},"renderOption")}),(0,t.jsx)("span",{children:"in"}),(0,t.jsx)(Xe,{ref:I,value:p.projectId,options:bt,recentOptionIds:yt,placeholder:"Project",noneLabel:"No project",searchPlaceholder:"Search projects...",emptyMessage:"No projects found.",onChange:s(e=>{e&&Bt(e),R(r=>({...r,projectId:e}))},"onChange"),onConfirm:s(()=>i.current?.focus(),"onConfirm"),renderTriggerValue:s(e=>e&&Qe?(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)("span",{className:"h-3.5 w-3.5 shrink-0 rounded-sm",style:{backgroundColor:Qe.color??"var(--project-none)"}}),(0,t.jsx)("span",{className:"truncate",children:e.label})]}):(0,t.jsx)("span",{className:"text-muted-foreground",children:"Project"}),"renderTriggerValue"),renderOption:s(e=>{if(!e.id)return(0,t.jsx)("span",{className:"truncate",children:e.label});const r=U.get(e.id);return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)("span",{className:"h-3.5 w-3.5 shrink-0 rounded-sm",style:{backgroundColor:r?.color??"var(--project-none)"}}),(0,t.jsx)("span",{className:"truncate",children:e.label})]})},"renderOption")}),(0,t.jsx)("span",{children:"filed in"}),(0,t.jsxs)(ve,{value:p.folderId??"__unfiled",onValueChange:s(e=>R(r=>({...r,folderId:e==="__unfiled"?null:e})),"onValueChange"),children:[(0,t.jsx)(je,{className:"h-8 w-auto min-w-32 border-0 bg-muted/50 px-2 shadow-none",children:(0,t.jsx)(ye,{})}),(0,t.jsxs)(be,{children:[(0,t.jsx)(Z,{value:"__unfiled",children:"Unfiled"}),(h?.folders??[]).map(e=>(0,t.jsx)(Z,{value:e.id,children:e.name},e.id))]})]})]})})}),(0,t.jsx)("div",{className:"border-t border-border/60 px-5 py-4",children:(0,t.jsx)(Xt,{ref:i,value:p.description,onChange:s(e=>R(r=>({...r,description:e})),"onChange"),placeholder:"Add instructions...",bordered:!1,contentClassName:"min-h-(--sz-160px) text-sm text-muted-foreground",mentions:ht,onSubmit:s(()=>{!N.isPending&&p.title.trim()&&p.projectId&&p.assigneeAgentId&&N.mutate()},"onSubmit")})}),(0,t.jsx)("div",{className:"border-t border-border/60 px-5 py-3",children:(0,t.jsxs)(He,{open:Se,onOpenChange:ie,children:[(0,t.jsxs)(lt,{className:"flex w-full items-center justify-between text-left",children:[(0,t.jsxs)("div",{children:[(0,t.jsx)("p",{className:"text-sm font-medium",children:"Advanced delivery settings"}),(0,t.jsx)("p",{className:"text-sm text-muted-foreground",children:"Keep policy controls secondary to the work definition."})]}),Se?(0,t.jsx)(ss,{className:"h-4 w-4 text-muted-foreground"}):(0,t.jsx)(ct,{className:"h-4 w-4 text-muted-foreground"})]}),(0,t.jsx)(Je,{className:"pt-3",children:(0,t.jsxs)("div",{className:"grid gap-4 md:grid-cols-2",children:[(0,t.jsxs)("div",{className:"space-y-2",children:[(0,t.jsx)("p",{className:"text-xs font-medium uppercase tracking-(--tracking-caps) text-muted-foreground",children:"Concurrency"}),(0,t.jsxs)(ve,{value:p.concurrencyPolicy,onValueChange:s(e=>R(r=>({...r,concurrencyPolicy:e})),"onValueChange"),children:[(0,t.jsx)(je,{children:(0,t.jsx)(ye,{})}),(0,t.jsx)(be,{children:Ss.map(e=>(0,t.jsx)(Z,{value:e,children:e.replaceAll("_"," ")},e))})]}),(0,t.jsx)("p",{className:"text-xs text-muted-foreground",children:ks[p.concurrencyPolicy]})]}),(0,t.jsxs)("div",{className:"space-y-2",children:[(0,t.jsx)("p",{className:"text-xs font-medium uppercase tracking-(--tracking-caps) text-muted-foreground",children:"Catch-up"}),(0,t.jsxs)(ve,{value:p.catchUpPolicy,onValueChange:s(e=>R(r=>({...r,catchUpPolicy:e})),"onValueChange"),children:[(0,t.jsx)(je,{children:(0,t.jsx)(ye,{})}),(0,t.jsx)(be,{children:Fs.map(e=>(0,t.jsx)(Z,{value:e,children:e.replaceAll("_"," ")},e))})]}),(0,t.jsx)("p",{className:"text-xs text-muted-foreground",children:Ps[p.catchUpPolicy]})]})]})})]})})]}),(0,t.jsxs)("div",{className:"shrink-0 flex flex-col gap-3 border-t border-border/60 px-5 py-4 sm:flex-row sm:items-center sm:justify-between",children:[(0,t.jsx)("div",{className:"text-sm text-muted-foreground",children:"After creation, Paperclip takes you straight to trigger setup. Draft routines stay paused until you add a default agent."}),(0,t.jsxs)("div",{className:"flex flex-col gap-2 sm:items-end",children:[(0,t.jsxs)(F,{onClick:s(()=>N.mutate(),"onClick"),disabled:N.isPending||!p.title.trim(),children:[(0,t.jsx)(J,{className:"mr-2 h-4 w-4"}),N.isPending?"Creating...":"Create routine"]}),N.isError?(0,t.jsx)("p",{className:"text-sm text-destructive",children:N.error instanceof Error?N.error.message:"Failed to create routine"}):null]})]})]})}),ae?(0,t.jsx)(cs,{children:(0,t.jsx)(ls,{className:"pt-6 text-sm text-destructive",children:ae instanceof Error?ae.message:"Failed to load routines"})}):null,P==="routines"?(0,t.jsxs)("div",{className:Pt(fe&&"flex gap-4"),children:[fe?(0,t.jsx)(hs,{result:pe,selection:f,allLabel:"All routines",itemLabelPlural:"routines",loading:Pe,onSelect:Y,onCreate:s(()=>T(),"onCreate"),onRename:s((e,r)=>ue.mutate({folderId:e.id,payload:{name:r}}),"onRename"),onEdit:s(e=>{L(e),K(!0)},"onEdit"),onDelete:se}):null,(0,t.jsxs)("div",{className:"min-w-0 flex-1",children:[u.groupBy==="folder"&&W?(0,t.jsx)("div",{className:"mb-3 flex flex-wrap items-center gap-2",children:f==="all"?(0,t.jsx)(Us,{label:"All routines",count:D.length}):(0,t.jsxs)("div",{className:"flex min-w-0 items-center gap-2 text-sm",children:[(0,t.jsx)(vs,{color:Be?.color}),(0,t.jsx)("span",{className:"truncate font-medium",children:f==="unfiled"?"Unfiled":Be?.name??"Folder"}),(0,t.jsxs)("span",{className:"text-muted-foreground",children:[D.length," routine",D.length===1?"":"s"]})]})}):null,u.groupBy==="folder"&&!W&&!Pe&&y.length>0?(0,t.jsx)(ps,{storageKey:`paperclip:routines-folder-nudge:${n??"none"}`,itemLabelPlural:"routines",onCreateFolder:s(()=>T(),"onCreateFolder")}):null,ne?(0,t.jsx)(gs,{selectedCount:B.length,folders:h?.folders??[],onMove:s(e=>{Rt(e)},"onMove"),onCreateAndMove:s(()=>T(B),"onCreateAndMove"),onClear:s(()=>V([]),"onClear"),onDone:s(()=>{re(!1),V([])},"onDone")}):null,y.length===0?(0,t.jsx)("div",{className:"py-12",children:(0,t.jsx)(we,{icon:ee,message:"No active routines. Use Create routine to define the first recurring workflow."})}):D.length===0?(0,t.jsxs)("div",{className:"py-12",children:[(0,t.jsx)(we,{icon:ee,message:f==="all"?"No routines match this view.":"This folder is empty."}),f!=="all"?(0,t.jsx)("div",{className:"mt-3 flex justify-center",children:(0,t.jsxs)(F,{size:"sm",onClick:$e,children:[(0,t.jsx)(J,{className:"mr-2 h-3.5 w-3.5"}),"New routine in this folder"]})}):null]}):(0,t.jsx)("div",{className:"flex flex-col gap-3",children:Nt.map(e=>{const r=!u.collapsedGroups.includes(e.key);return(0,t.jsxs)(He,{open:r,onOpenChange:s(a=>{ge({collapsedGroups:a?u.collapsedGroups.filter(v=>v!==e.key):[...u.collapsedGroups,e.key]})},"onOpenChange"),children:[e.label?(0,t.jsx)(Es,{label:e.label,count:e.items.length,isOpen:r}):null,(0,t.jsx)(Je,{children:e.items.map(a=>(0,t.jsx)(Cs,{routine:a,projectById:U,agentById:E,runningRoutineId:ut,statusMutationRoutineId:mt,href:`/routines/${a.id}`,runNowButton:!0,divider:!1,onRunNow:_t,onToggleEnabled:Ct,onToggleArchived:St,selectMode:ne,selected:B.includes(a.id),onSelectChange:s((v,xe)=>{V(H=>xe?Array.from(new Set([...H,v.id])):H.filter(Ft=>Ft!==v.id))},"onSelectChange"),extraMenuItems:(0,t.jsx)(us,{folders:h?.folders??[],currentFolderId:a.folderId??null,onMove:s(v=>{const xe=a.folderId??null;qe.mutate({itemId:a.id,folderId:v}),o({title:"Routine moved",body:v?`Moved "${a.title}" to ${h?.folders.find(H=>H.id===v)?.name??"folder"}.`:`Moved "${a.title}" to Unfiled.`,tone:"success",action:{label:"Undo",onClick:s(()=>qe.mutate({itemId:a.id,folderId:xe}),"onClick")}})},"onMove"),onCreateAndMove:s(()=>T([a.id]),"onCreateAndMove")})},a.id))})]},e.key)})})]})]}):null,(0,t.jsx)(ys,{open:O,kind:"routine",folder:te,pending:De.isPending||ue.isPending,onOpenChange:K,onSubmit:s(e=>{te?ue.mutate({folderId:te.id,payload:e}):De.mutate(e)},"onSubmit")}),(0,t.jsx)(Rs,{open:Q!==null,folder:Q,itemLabelPlural:"routines",pending:Te.isPending,onOpenChange:s(e=>{e||se(null)},"onOpenChange"),onConfirm:s(()=>{Q&&Te.mutate(Q.id)},"onConfirm")}),(0,t.jsx)(xs,{open:dt,onOpenChange:Ne,result:pe,selection:f,allLabel:"All routines",itemLabelPlural:"Routines",onSelect:Y,onCreate:s(()=>T(),"onCreate")}),(0,t.jsx)(Yt,{open:k!==null,onOpenChange:s(e=>{e||oe(null)},"onOpenChange"),companyId:n,routineName:k?.title??null,agents:_??[],projects:C??[],defaultProjectId:k?.projectId??null,defaultAssigneeAgentId:k?.assigneeAgentId??null,variables:k?.variables??[],isPending:Ee.isPending,onSubmit:s(e=>{k&&Ee.mutate({id:k.id,data:e})},"onSubmit")})]}):(0,t.jsx)(we,{icon:ee,message:"Select a company to view routines."})}s(un,"Routines");function Us({label:n,count:c}){return(0,t.jsxs)("div",{className:"flex min-w-0 items-center gap-2 text-sm",children:[(0,t.jsx)(ee,{className:"h-3.5 w-3.5 text-muted-foreground"}),(0,t.jsx)("span",{className:"truncate font-medium",children:n}),(0,t.jsxs)("span",{className:"text-muted-foreground",children:[c," routine",c===1?"":"s"]})]})}s(Us,"FolderIconHeader");export{un as Routines};
@@ -1 +1 @@
1
- var xe=Object.defineProperty;var s=(n,o)=>xe(n,"name",{value:o,configurable:!0});import{i as ae}from"./rolldown-runtime-ZpX3ckPj.js";import{G as ee,Qa as pe,St as te,Za as re,_a as Q,eo as fe,ht as ge,n as je,va as K}from"./boxes-Bpd3vwBa.js";import{n as be,o as ve,r as S,st as se}from"./BreadcrumbContext-DP0mgUIg.js";import{C as ye,S as Se,_ as Ne,a as Ce,c as we,f as ke,i as De,m as G,o as Me,p,r as Ee,t as He,u as ze}from"./dialog-aW5qVpwP.js";import{t as X}from"./label-CRFTGD7I.js";import{t as Fe}from"./x-BYvg7sTI.js";import{t as P}from"./input-CfcFr-x-.js";import{t as Le}from"./access-CspWWirX.js";import{t as _e}from"./environments-C7g5xojw.js";import{_t as k,c as Ae,gt as qe,ht as Z,mt as D,o as Te,pn as ne,pt as Ke,s as Pe,un as V}from"./index-5wqemOVn.js";var ie={name:"columns-2",size:24,node:[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}],["path",{d:"M12 3v18",key:"108xh3"}]],aliases:["columns"]};ie.node;var Qe=je(ie),d=ae(fe(),1),e=ae(pe(),1);function Oe({companyId:n,open:o,savedHosts:u,onOpenChange:r,onConnect:m}){const M=re(),[N,j]=(0,d.useState)(""),[v,E]=(0,d.useState)(""),[y,L]=(0,d.useState)(""),[H,C]=(0,d.useState)(null),f=y.trim(),g=s(()=>M.invalidateQueries({queryKey:Z.sshHosts(n)}),"refreshHosts"),w=Q({mutationFn:s(()=>D.createSshHost(n,{name:N.trim(),destination:v.trim(),...f?{port:Number.parseInt(f,10)}:{}}),"mutationFn"),onSuccess:s(l=>{C(null),j(""),E(""),L(""),g(),r(!1),m(l)},"onSuccess"),onError:s(l=>C(k(l,"Could not save the SSH host.")),"onError")}),b=Q({mutationFn:s(l=>D.removeSshHost(n,l),"mutationFn"),onSuccess:s(()=>{C(null),g()},"onSuccess"),onError:s(l=>C(k(l,"Could not remove the saved SSH host.")),"onError")}),z=f.length>0&&!/^[0-9]+$/.test(f),x=w.isPending||!N.trim()||!v.trim()||z;return(0,e.jsx)(He,{open:o,onOpenChange:r,children:(0,e.jsxs)(Ee,{className:"sm:max-w-md",children:[(0,e.jsxs)(Me,{children:[(0,e.jsx)(we,{children:"Connect to host"}),(0,e.jsx)(De,{children:"The session starts this server's ssh client with its own keys, agent and known hosts."})]}),(0,e.jsxs)("form",{className:"space-y-3",onSubmit:s(l=>{l.preventDefault(),x||w.mutate()},"onSubmit"),children:[(0,e.jsxs)("div",{className:"space-y-1",children:[(0,e.jsx)(X,{htmlFor:"ssh-host-name",children:"Name"}),(0,e.jsx)(P,{id:"ssh-host-name",value:N,placeholder:"prod-db",autoComplete:"off",onChange:s(l=>j(l.target.value),"onChange")})]}),(0,e.jsxs)("div",{className:"space-y-1",children:[(0,e.jsx)(X,{htmlFor:"ssh-host-destination",children:"Destination"}),(0,e.jsx)(P,{id:"ssh-host-destination",value:v,placeholder:"user@host",autoComplete:"off",onChange:s(l=>E(l.target.value),"onChange")})]}),(0,e.jsxs)("div",{className:"space-y-1",children:[(0,e.jsx)(X,{htmlFor:"ssh-host-port",children:"Port"}),(0,e.jsx)(P,{id:"ssh-host-port",value:y,placeholder:"22",inputMode:"numeric",autoComplete:"off","aria-invalid":z,onChange:s(l=>L(l.target.value),"onChange")})]}),H?(0,e.jsx)("p",{className:"text-xs text-destructive",role:"alert",children:H}):null,u.length>0?(0,e.jsxs)("div",{className:"space-y-1 border-t border-border pt-3",children:[(0,e.jsx)("p",{className:"text-xs font-medium text-foreground",children:"Saved hosts"}),(0,e.jsx)("ul",{className:"space-y-1",children:u.map(l=>(0,e.jsxs)("li",{className:"flex items-center gap-2 text-xs",children:[(0,e.jsxs)("button",{type:"button",className:"min-w-0 flex-1 truncate text-left hover:underline",onClick:s(()=>{r(!1),m(l)},"onClick"),children:[l.name,(0,e.jsxs)("span",{className:"ml-2 text-muted-foreground",children:[l.destination,l.port===null?"":`:${l.port}`]})]}),(0,e.jsx)(S,{type:"button",variant:"ghost",size:"icon-xs","aria-label":`Remove ${l.name}`,disabled:b.isPending&&b.variables===l.id,onClick:s(()=>b.mutate(l.id),"onClick"),children:(0,e.jsx)(ye,{"aria-hidden":!0})})]},l.id))})]}):null,(0,e.jsxs)(Ce,{children:[(0,e.jsx)(S,{type:"button",variant:"ghost",onClick:s(()=>r(!1),"onClick"),children:"Cancel"}),(0,e.jsx)(S,{type:"submit",disabled:x,children:"Save and connect"})]})]})]})})}s(Oe,"SshHostDialog");var F=2;function oe(n){return`paperclip:terminal-layout:v1:${n}`}s(oe,"layoutKey");function U(n){return`paperclip:terminal-titles:v1:${n}`}s(U,"titlesKey");function le(n){try{return JSON.parse(window.localStorage.getItem(n)??"null")}catch{return null}}s(le,"readJson");function W(n,o){try{window.localStorage.setItem(n,JSON.stringify(o))}catch{return}}s(W,"writeJson");function $e(n){const o=le(oe(n)),u=Array.isArray(o?.panes)?o.panes.filter(r=>typeof r=="string").slice(0,F):[];return{panes:u,focused:typeof o?.focused=="number"&&o.focused>=0&&o.focused<u.length?o.focused:0}}s($e,"readLayout");function Be(n){const o=le(U(n));return!o||typeof o!="object"||Array.isArray(o)?{}:Object.fromEntries(Object.entries(o).filter(u=>typeof u[1]=="string"))}s(Be,"readTitles");function Re(n,o){const u=new Set(o.map(m=>m.id)),r=n.panes.filter((m,M)=>u.has(m)&&n.panes.indexOf(m)===M);return r.length===0&&o.length>0&&r.push(o[0].id),{panes:r,focused:Math.min(n.focused,Math.max(0,r.length-1))}}s(Re,"reconcileLayout");function Je(n,o){return n.focused===o.focused&&n.panes.length===o.panes.length&&n.panes.every((u,r)=>u===o.panes[r])}s(Je,"sameLayout");function Ge({companyId:n}){const o=re(),u=Z.sessions(n),[r,m]=(0,d.useState)(()=>$e(n)),[M,N]=(0,d.useState)(()=>Be(n)),[j,v]=(0,d.useState)(null),[E,y]=(0,d.useState)(null),[L,H]=(0,d.useState)(!1),C=K({queryKey:te.access.currentBoardAccess,queryFn:s(()=>Le.getCurrentBoardAccess(),"queryFn")}),f=qe(C.data),g=K({queryKey:u,queryFn:s(()=>D.list(n),"queryFn")}),w=K({queryKey:te.environments.list(n),queryFn:s(()=>_e.list(n),"queryFn"),enabled:g.isSuccess}),b=(0,d.useMemo)(()=>g.data??[],[g.data]),z=(0,d.useMemo)(()=>(w.data??[]).filter(t=>t.driver==="ssh"),[w.data]),x=K({queryKey:Z.sshHosts(n),queryFn:s(()=>D.listSshHosts(n),"queryFn"),enabled:f}),l=(0,d.useMemo)(()=>(x.data?.hosts??[]).filter(t=>t.source==="config"),[x.data]),_=(0,d.useMemo)(()=>(x.data?.hosts??[]).filter(t=>t.source==="saved"),[x.data]);(0,d.useEffect)(()=>{g.isSuccess&&m(t=>{const a=Re(t,b);return Je(t,a)?t:a})},[b,g.isSuccess]),(0,d.useEffect)(()=>{W(oe(n),r)},[n,r]);const O=(0,d.useCallback)(t=>{o.setQueryData(u,(a=[])=>a.some(i=>i.id===t.id)?a.map(i=>i.id===t.id?t:i):[...a,t])},[o,u]),$=(0,d.useCallback)((t,a)=>{m(i=>{const c=i.panes.indexOf(t);if(c>=0)return{...i,focused:c};const h=a??i.focused,T=[...i.panes];return h>=T.length?T.push(t):T[h]=t,{panes:T.slice(0,F),focused:Math.min(h,1)}})},[]),A=Q({mutationFn:s(t=>D.create(n,t.body),"mutationFn"),onSuccess:s((t,a)=>{y(null),O(t),$(t.id,a.pane)},"onSuccess"),onError:s(t=>y(k(t,"Could not open a terminal session.")),"onError")}),B=Q({mutationFn:s(t=>D.remove(n,t),"mutationFn"),onSuccess:s((t,a)=>{y(null),o.setQueryData(u,(i=[])=>i.filter(c=>c.id!==a)),m(i=>{const c=i.panes.filter(h=>h!==a);return{panes:c,focused:Math.min(i.focused,Math.max(0,c.length-1))}}),N(i=>{if(!(a in i))return i;const{[a]:c,...h}=i;return W(U(n),h),h})},"onSuccess"),onError:s(t=>y(k(t,"Could not close the terminal session.")),"onError")}),R=s(t=>A.mutate({body:{kind:"local"},pane:t}),"createLocal"),de=s((t,a)=>A.mutate({body:{kind:"ssh",environmentId:t,title:a}}),"createSsh"),J=s(t=>A.mutate({body:{kind:"local",sshHostId:t.id,title:t.name}}),"createSshHost"),ce=s(()=>{if(r.panes.length>=F){const a=r.panes[r.focused]??r.panes[0];m({panes:[a],focused:0});return}const t=b.find(a=>!r.panes.includes(a.id));t?$(t.id,r.panes.length):f&&R(r.panes.length)},"toggleSplit"),ue=s(t=>M[t.id]??t.title,"titleOf"),Y=s(()=>{if(!j)return;const t=j.value.trim();N(a=>{const{[j.id]:i,...c}=a,h=t?{...c,[j.id]:t}:c;return W(U(n),h),h}),v(null)},"commitRename"),me=s(t=>{t.key==="Enter"?(t.preventDefault(),Y()):t.key==="Escape"&&(t.preventDefault(),v(null))},"onRenameKeyDown");if(g.isLoading)return(0,e.jsx)("p",{className:"p-4 text-sm text-muted-foreground",children:"Loading terminal sessions…"});if(g.isError)return(0,e.jsxs)("div",{className:"flex h-full min-h-(--sz-60vh) flex-col items-center justify-center gap-2 p-6 text-center","data-testid":"terminal-access-error",children:[(0,e.jsx)(V,{className:"size-6 text-muted-foreground","aria-hidden":!0}),(0,e.jsx)("p",{className:"text-sm font-medium text-foreground",children:"Terminal unavailable"}),(0,e.jsx)("p",{className:"max-w-md text-sm text-muted-foreground",children:k(g.error,"Terminal sessions could not be loaded.")})]});const he=r.panes[r.focused]??null,I=new Map(b.map(t=>[t.id,t])),q=A.isPending;return(0,e.jsxs)("div",{className:"flex h-full min-h-(--sz-60vh) flex-col overflow-hidden rounded-md border border-border","data-testid":"terminal-workspace",children:[(0,e.jsxs)("div",{className:"flex shrink-0 items-center gap-1 border-b border-border px-2 py-1",children:[(0,e.jsx)("div",{role:"tablist","aria-label":"Terminal sessions",className:"flex min-w-0 flex-1 items-center gap-1 overflow-x-auto",children:b.map(t=>{const a=t.id===he,i=r.panes.includes(t.id),c=ue(t);return(0,e.jsxs)("div",{className:ee("group flex shrink-0 items-center gap-1 rounded-md border px-2 py-1 text-xs",a?"border-ring bg-accent text-foreground":i?"border-border text-foreground":"border-transparent text-muted-foreground hover:bg-accent"),children:[t.kind==="ssh"||t.sshHost?(0,e.jsx)(ne,{className:"size-3","aria-hidden":!0}):(0,e.jsx)(V,{className:"size-3","aria-hidden":!0}),j?.id===t.id?(0,e.jsx)(P,{autoFocus:!0,value:j.value,"aria-label":"Session title",onChange:s(h=>v({id:t.id,value:h.target.value}),"onChange"),onKeyDown:me,onBlur:Y,className:"h-6 w-32 px-1 text-xs md:text-xs"}):(0,e.jsxs)("button",{type:"button",role:"tab","aria-selected":a,title:"Double-click to rename",className:"max-w-48 truncate outline-none focus-visible:underline",onClick:s(()=>$(t.id),"onClick"),onDoubleClick:s(()=>v({id:t.id,value:c}),"onDoubleClick"),children:[c,t.status==="exited"?(0,e.jsx)("span",{className:"ml-1 text-muted-foreground",children:"(ended)"}):null]}),(0,e.jsx)("button",{type:"button","aria-label":`Close ${c}`,className:"rounded-sm text-muted-foreground hover:text-foreground",disabled:B.isPending&&B.variables===t.id,onClick:s(()=>B.mutate(t.id),"onClick"),children:(0,e.jsx)(Fe,{className:"size-3","aria-hidden":!0})})]},t.id)})}),f?(0,e.jsxs)(S,{type:"button",variant:"ghost",size:"xs",onClick:s(()=>R(),"onClick"),disabled:q,children:[(0,e.jsx)(se,{"aria-hidden":!0}),"Local"]}):null,(0,e.jsxs)(ze,{children:[(0,e.jsx)(Se,{asChild:!0,children:(0,e.jsxs)(S,{type:"button",variant:"ghost",size:"xs",disabled:q,children:[(0,e.jsx)(ne,{"aria-hidden":!0}),"SSH"]})}),(0,e.jsxs)(ke,{align:"end",className:"w-72",children:[f?(0,e.jsxs)(e.Fragment,{children:[x.isLoading?(0,e.jsx)(p,{disabled:!0,children:"Loading…"}):x.isError?(0,e.jsx)(p,{disabled:!0,className:"text-destructive",children:k(x.error,"SSH hosts could not be loaded.")}):l.length===0&&_.length===0?(0,e.jsx)(p,{disabled:!0,children:"No SSH hosts"}):(0,e.jsxs)(e.Fragment,{children:[(0,e.jsx)(G,{children:"~/.ssh/config"}),l.length===0?(0,e.jsx)(p,{disabled:!0,children:"No hosts"}):l.map(t=>(0,e.jsxs)(p,{onSelect:s(()=>J(t),"onSelect"),children:[(0,e.jsx)("span",{className:"truncate",children:t.name}),(0,e.jsx)("span",{className:"ml-auto truncate pl-2 text-xs text-muted-foreground",children:t.destination})]},t.id)),(0,e.jsx)(G,{children:"Saved"}),_.length===0?(0,e.jsx)(p,{disabled:!0,children:"No saved hosts"}):_.map(t=>(0,e.jsxs)(p,{onSelect:s(()=>J(t),"onSelect"),children:[(0,e.jsx)("span",{className:"truncate",children:t.name}),(0,e.jsxs)("span",{className:"ml-auto truncate pl-2 text-xs text-muted-foreground",children:[t.destination,t.port===null?"":`:${t.port}`]})]},t.id))]}),x.data?.configError?(0,e.jsx)(p,{disabled:!0,className:"text-muted-foreground",children:x.data.configError}):null]}):null,(0,e.jsx)(G,{children:"Environments"}),z.length===0?(0,e.jsx)(p,{disabled:!0,children:w.isLoading?"Loading…":"No SSH environments"}):z.map(t=>(0,e.jsx)(p,{onSelect:s(()=>de(t.id,t.name),"onSelect"),children:t.name},t.id)),(0,e.jsx)(Ne,{}),f?(0,e.jsx)(p,{onSelect:s(()=>H(!0),"onSelect"),children:"Connect to host…"}):null,(0,e.jsx)(p,{asChild:!0,children:(0,e.jsx)(ve,{to:"/company/settings/instance/environments",children:"Manage SSH environments"})})]})]}),(0,e.jsx)(S,{type:"button",variant:"ghost",size:"icon-xs","aria-label":r.panes.length>=F?"Close split":"Split terminal","aria-pressed":r.panes.length>=F,onClick:ce,disabled:q||r.panes.length===0,children:(0,e.jsx)(Qe,{"aria-hidden":!0})})]}),(0,e.jsx)(Oe,{companyId:n,open:L,savedHosts:_,onOpenChange:H,onConnect:J}),E?(0,e.jsx)("p",{className:"shrink-0 border-b border-border px-3 py-1 text-xs text-destructive",role:"alert",children:E}):null,r.panes.length===0?(0,e.jsxs)("div",{className:"flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center",children:[(0,e.jsx)(V,{className:"size-6 text-muted-foreground","aria-hidden":!0}),(0,e.jsx)("p",{className:"text-sm text-muted-foreground",children:"No terminal sessions yet."}),f?(0,e.jsxs)(S,{type:"button",variant:"outline",size:"sm",onClick:s(()=>R(),"onClick"),disabled:q,children:[(0,e.jsx)(se,{"aria-hidden":!0}),"New local terminal"]}):(0,e.jsx)("p",{className:"max-w-md text-xs text-muted-foreground",children:"A shell on this host needs instance admin access. Open an SSH terminal to a registered environment instead."})]}):(0,e.jsx)(Ae,{className:"min-h-0 flex-1",children:r.panes.map((t,a)=>(0,e.jsxs)(d.Fragment,{children:[a>0?(0,e.jsx)(Te,{}):null,(0,e.jsx)(Pe,{id:`terminal-pane-${a}`,minSize:"20%",children:(0,e.jsx)("div",{className:ee("h-full min-h-0",r.panes.length>1&&a===r.focused&&"ring-1 ring-inset ring-ring"),onFocusCapture:s(()=>m(i=>i.focused===a?i:{...i,focused:a}),"onFocusCapture"),onMouseDown:s(()=>m(i=>i.focused===a?i:{...i,focused:a}),"onMouseDown"),"data-testid":`terminal-pane-${a}`,children:(0,e.jsx)(Ke,{companyId:n,sessionId:t,active:a===r.focused,onSessionChange:s(i=>{I.has(i.id)&&O(i)},"onSessionChange"),onExit:s(i=>{const c=I.get(t);c&&O({...c,status:"exited",exitCode:i.exitCode})},"onExit")})})})]},t))})]})}s(Ge,"TerminalWorkspace");function at(){const{selectedCompanyId:n}=ge(),{setBreadcrumbs:o}=be();return(0,d.useEffect)(()=>{o([{label:"Terminal"}])},[o]),n?(0,e.jsx)(Ge,{companyId:n},n):(0,e.jsx)("p",{className:"text-sm text-muted-foreground",children:"Select a company to open a terminal."})}s(at,"Terminal");export{at as Terminal};
1
+ var xe=Object.defineProperty;var s=(n,o)=>xe(n,"name",{value:o,configurable:!0});import{i as ae}from"./rolldown-runtime-ZpX3ckPj.js";import{G as ee,Qa as pe,St as te,Za as re,_a as Q,eo as fe,ht as ge,n as je,va as K}from"./boxes-Bpd3vwBa.js";import{n as be,o as ve,r as S,st as se}from"./BreadcrumbContext-DP0mgUIg.js";import{C as ye,S as Se,_ as Ne,a as Ce,c as we,f as ke,i as De,m as G,o as Me,p,r as Ee,t as He,u as ze}from"./dialog-aW5qVpwP.js";import{t as X}from"./label-CRFTGD7I.js";import{t as Fe}from"./x-BYvg7sTI.js";import{t as P}from"./input-CfcFr-x-.js";import{t as Le}from"./access-CspWWirX.js";import{t as _e}from"./environments-C7g5xojw.js";import{_t as k,c as Ae,gt as qe,ht as Z,mt as D,o as Te,pn as ne,pt as Ke,s as Pe,un as V}from"./index-Yr0PvgVh.js";var ie={name:"columns-2",size:24,node:[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}],["path",{d:"M12 3v18",key:"108xh3"}]],aliases:["columns"]};ie.node;var Qe=je(ie),d=ae(fe(),1),e=ae(pe(),1);function Oe({companyId:n,open:o,savedHosts:u,onOpenChange:r,onConnect:m}){const M=re(),[N,j]=(0,d.useState)(""),[v,E]=(0,d.useState)(""),[y,L]=(0,d.useState)(""),[H,C]=(0,d.useState)(null),f=y.trim(),g=s(()=>M.invalidateQueries({queryKey:Z.sshHosts(n)}),"refreshHosts"),w=Q({mutationFn:s(()=>D.createSshHost(n,{name:N.trim(),destination:v.trim(),...f?{port:Number.parseInt(f,10)}:{}}),"mutationFn"),onSuccess:s(l=>{C(null),j(""),E(""),L(""),g(),r(!1),m(l)},"onSuccess"),onError:s(l=>C(k(l,"Could not save the SSH host.")),"onError")}),b=Q({mutationFn:s(l=>D.removeSshHost(n,l),"mutationFn"),onSuccess:s(()=>{C(null),g()},"onSuccess"),onError:s(l=>C(k(l,"Could not remove the saved SSH host.")),"onError")}),z=f.length>0&&!/^[0-9]+$/.test(f),x=w.isPending||!N.trim()||!v.trim()||z;return(0,e.jsx)(He,{open:o,onOpenChange:r,children:(0,e.jsxs)(Ee,{className:"sm:max-w-md",children:[(0,e.jsxs)(Me,{children:[(0,e.jsx)(we,{children:"Connect to host"}),(0,e.jsx)(De,{children:"The session starts this server's ssh client with its own keys, agent and known hosts."})]}),(0,e.jsxs)("form",{className:"space-y-3",onSubmit:s(l=>{l.preventDefault(),x||w.mutate()},"onSubmit"),children:[(0,e.jsxs)("div",{className:"space-y-1",children:[(0,e.jsx)(X,{htmlFor:"ssh-host-name",children:"Name"}),(0,e.jsx)(P,{id:"ssh-host-name",value:N,placeholder:"prod-db",autoComplete:"off",onChange:s(l=>j(l.target.value),"onChange")})]}),(0,e.jsxs)("div",{className:"space-y-1",children:[(0,e.jsx)(X,{htmlFor:"ssh-host-destination",children:"Destination"}),(0,e.jsx)(P,{id:"ssh-host-destination",value:v,placeholder:"user@host",autoComplete:"off",onChange:s(l=>E(l.target.value),"onChange")})]}),(0,e.jsxs)("div",{className:"space-y-1",children:[(0,e.jsx)(X,{htmlFor:"ssh-host-port",children:"Port"}),(0,e.jsx)(P,{id:"ssh-host-port",value:y,placeholder:"22",inputMode:"numeric",autoComplete:"off","aria-invalid":z,onChange:s(l=>L(l.target.value),"onChange")})]}),H?(0,e.jsx)("p",{className:"text-xs text-destructive",role:"alert",children:H}):null,u.length>0?(0,e.jsxs)("div",{className:"space-y-1 border-t border-border pt-3",children:[(0,e.jsx)("p",{className:"text-xs font-medium text-foreground",children:"Saved hosts"}),(0,e.jsx)("ul",{className:"space-y-1",children:u.map(l=>(0,e.jsxs)("li",{className:"flex items-center gap-2 text-xs",children:[(0,e.jsxs)("button",{type:"button",className:"min-w-0 flex-1 truncate text-left hover:underline",onClick:s(()=>{r(!1),m(l)},"onClick"),children:[l.name,(0,e.jsxs)("span",{className:"ml-2 text-muted-foreground",children:[l.destination,l.port===null?"":`:${l.port}`]})]}),(0,e.jsx)(S,{type:"button",variant:"ghost",size:"icon-xs","aria-label":`Remove ${l.name}`,disabled:b.isPending&&b.variables===l.id,onClick:s(()=>b.mutate(l.id),"onClick"),children:(0,e.jsx)(ye,{"aria-hidden":!0})})]},l.id))})]}):null,(0,e.jsxs)(Ce,{children:[(0,e.jsx)(S,{type:"button",variant:"ghost",onClick:s(()=>r(!1),"onClick"),children:"Cancel"}),(0,e.jsx)(S,{type:"submit",disabled:x,children:"Save and connect"})]})]})]})})}s(Oe,"SshHostDialog");var F=2;function oe(n){return`paperclip:terminal-layout:v1:${n}`}s(oe,"layoutKey");function U(n){return`paperclip:terminal-titles:v1:${n}`}s(U,"titlesKey");function le(n){try{return JSON.parse(window.localStorage.getItem(n)??"null")}catch{return null}}s(le,"readJson");function W(n,o){try{window.localStorage.setItem(n,JSON.stringify(o))}catch{return}}s(W,"writeJson");function $e(n){const o=le(oe(n)),u=Array.isArray(o?.panes)?o.panes.filter(r=>typeof r=="string").slice(0,F):[];return{panes:u,focused:typeof o?.focused=="number"&&o.focused>=0&&o.focused<u.length?o.focused:0}}s($e,"readLayout");function Be(n){const o=le(U(n));return!o||typeof o!="object"||Array.isArray(o)?{}:Object.fromEntries(Object.entries(o).filter(u=>typeof u[1]=="string"))}s(Be,"readTitles");function Re(n,o){const u=new Set(o.map(m=>m.id)),r=n.panes.filter((m,M)=>u.has(m)&&n.panes.indexOf(m)===M);return r.length===0&&o.length>0&&r.push(o[0].id),{panes:r,focused:Math.min(n.focused,Math.max(0,r.length-1))}}s(Re,"reconcileLayout");function Je(n,o){return n.focused===o.focused&&n.panes.length===o.panes.length&&n.panes.every((u,r)=>u===o.panes[r])}s(Je,"sameLayout");function Ge({companyId:n}){const o=re(),u=Z.sessions(n),[r,m]=(0,d.useState)(()=>$e(n)),[M,N]=(0,d.useState)(()=>Be(n)),[j,v]=(0,d.useState)(null),[E,y]=(0,d.useState)(null),[L,H]=(0,d.useState)(!1),C=K({queryKey:te.access.currentBoardAccess,queryFn:s(()=>Le.getCurrentBoardAccess(),"queryFn")}),f=qe(C.data),g=K({queryKey:u,queryFn:s(()=>D.list(n),"queryFn")}),w=K({queryKey:te.environments.list(n),queryFn:s(()=>_e.list(n),"queryFn"),enabled:g.isSuccess}),b=(0,d.useMemo)(()=>g.data??[],[g.data]),z=(0,d.useMemo)(()=>(w.data??[]).filter(t=>t.driver==="ssh"),[w.data]),x=K({queryKey:Z.sshHosts(n),queryFn:s(()=>D.listSshHosts(n),"queryFn"),enabled:f}),l=(0,d.useMemo)(()=>(x.data?.hosts??[]).filter(t=>t.source==="config"),[x.data]),_=(0,d.useMemo)(()=>(x.data?.hosts??[]).filter(t=>t.source==="saved"),[x.data]);(0,d.useEffect)(()=>{g.isSuccess&&m(t=>{const a=Re(t,b);return Je(t,a)?t:a})},[b,g.isSuccess]),(0,d.useEffect)(()=>{W(oe(n),r)},[n,r]);const O=(0,d.useCallback)(t=>{o.setQueryData(u,(a=[])=>a.some(i=>i.id===t.id)?a.map(i=>i.id===t.id?t:i):[...a,t])},[o,u]),$=(0,d.useCallback)((t,a)=>{m(i=>{const c=i.panes.indexOf(t);if(c>=0)return{...i,focused:c};const h=a??i.focused,T=[...i.panes];return h>=T.length?T.push(t):T[h]=t,{panes:T.slice(0,F),focused:Math.min(h,1)}})},[]),A=Q({mutationFn:s(t=>D.create(n,t.body),"mutationFn"),onSuccess:s((t,a)=>{y(null),O(t),$(t.id,a.pane)},"onSuccess"),onError:s(t=>y(k(t,"Could not open a terminal session.")),"onError")}),B=Q({mutationFn:s(t=>D.remove(n,t),"mutationFn"),onSuccess:s((t,a)=>{y(null),o.setQueryData(u,(i=[])=>i.filter(c=>c.id!==a)),m(i=>{const c=i.panes.filter(h=>h!==a);return{panes:c,focused:Math.min(i.focused,Math.max(0,c.length-1))}}),N(i=>{if(!(a in i))return i;const{[a]:c,...h}=i;return W(U(n),h),h})},"onSuccess"),onError:s(t=>y(k(t,"Could not close the terminal session.")),"onError")}),R=s(t=>A.mutate({body:{kind:"local"},pane:t}),"createLocal"),de=s((t,a)=>A.mutate({body:{kind:"ssh",environmentId:t,title:a}}),"createSsh"),J=s(t=>A.mutate({body:{kind:"local",sshHostId:t.id,title:t.name}}),"createSshHost"),ce=s(()=>{if(r.panes.length>=F){const a=r.panes[r.focused]??r.panes[0];m({panes:[a],focused:0});return}const t=b.find(a=>!r.panes.includes(a.id));t?$(t.id,r.panes.length):f&&R(r.panes.length)},"toggleSplit"),ue=s(t=>M[t.id]??t.title,"titleOf"),Y=s(()=>{if(!j)return;const t=j.value.trim();N(a=>{const{[j.id]:i,...c}=a,h=t?{...c,[j.id]:t}:c;return W(U(n),h),h}),v(null)},"commitRename"),me=s(t=>{t.key==="Enter"?(t.preventDefault(),Y()):t.key==="Escape"&&(t.preventDefault(),v(null))},"onRenameKeyDown");if(g.isLoading)return(0,e.jsx)("p",{className:"p-4 text-sm text-muted-foreground",children:"Loading terminal sessions…"});if(g.isError)return(0,e.jsxs)("div",{className:"flex h-full min-h-(--sz-60vh) flex-col items-center justify-center gap-2 p-6 text-center","data-testid":"terminal-access-error",children:[(0,e.jsx)(V,{className:"size-6 text-muted-foreground","aria-hidden":!0}),(0,e.jsx)("p",{className:"text-sm font-medium text-foreground",children:"Terminal unavailable"}),(0,e.jsx)("p",{className:"max-w-md text-sm text-muted-foreground",children:k(g.error,"Terminal sessions could not be loaded.")})]});const he=r.panes[r.focused]??null,I=new Map(b.map(t=>[t.id,t])),q=A.isPending;return(0,e.jsxs)("div",{className:"flex h-full min-h-(--sz-60vh) flex-col overflow-hidden rounded-md border border-border","data-testid":"terminal-workspace",children:[(0,e.jsxs)("div",{className:"flex shrink-0 items-center gap-1 border-b border-border px-2 py-1",children:[(0,e.jsx)("div",{role:"tablist","aria-label":"Terminal sessions",className:"flex min-w-0 flex-1 items-center gap-1 overflow-x-auto",children:b.map(t=>{const a=t.id===he,i=r.panes.includes(t.id),c=ue(t);return(0,e.jsxs)("div",{className:ee("group flex shrink-0 items-center gap-1 rounded-md border px-2 py-1 text-xs",a?"border-ring bg-accent text-foreground":i?"border-border text-foreground":"border-transparent text-muted-foreground hover:bg-accent"),children:[t.kind==="ssh"||t.sshHost?(0,e.jsx)(ne,{className:"size-3","aria-hidden":!0}):(0,e.jsx)(V,{className:"size-3","aria-hidden":!0}),j?.id===t.id?(0,e.jsx)(P,{autoFocus:!0,value:j.value,"aria-label":"Session title",onChange:s(h=>v({id:t.id,value:h.target.value}),"onChange"),onKeyDown:me,onBlur:Y,className:"h-6 w-32 px-1 text-xs md:text-xs"}):(0,e.jsxs)("button",{type:"button",role:"tab","aria-selected":a,title:"Double-click to rename",className:"max-w-48 truncate outline-none focus-visible:underline",onClick:s(()=>$(t.id),"onClick"),onDoubleClick:s(()=>v({id:t.id,value:c}),"onDoubleClick"),children:[c,t.status==="exited"?(0,e.jsx)("span",{className:"ml-1 text-muted-foreground",children:"(ended)"}):null]}),(0,e.jsx)("button",{type:"button","aria-label":`Close ${c}`,className:"rounded-sm text-muted-foreground hover:text-foreground",disabled:B.isPending&&B.variables===t.id,onClick:s(()=>B.mutate(t.id),"onClick"),children:(0,e.jsx)(Fe,{className:"size-3","aria-hidden":!0})})]},t.id)})}),f?(0,e.jsxs)(S,{type:"button",variant:"ghost",size:"xs",onClick:s(()=>R(),"onClick"),disabled:q,children:[(0,e.jsx)(se,{"aria-hidden":!0}),"Local"]}):null,(0,e.jsxs)(ze,{children:[(0,e.jsx)(Se,{asChild:!0,children:(0,e.jsxs)(S,{type:"button",variant:"ghost",size:"xs",disabled:q,children:[(0,e.jsx)(ne,{"aria-hidden":!0}),"SSH"]})}),(0,e.jsxs)(ke,{align:"end",className:"w-72",children:[f?(0,e.jsxs)(e.Fragment,{children:[x.isLoading?(0,e.jsx)(p,{disabled:!0,children:"Loading…"}):x.isError?(0,e.jsx)(p,{disabled:!0,className:"text-destructive",children:k(x.error,"SSH hosts could not be loaded.")}):l.length===0&&_.length===0?(0,e.jsx)(p,{disabled:!0,children:"No SSH hosts"}):(0,e.jsxs)(e.Fragment,{children:[(0,e.jsx)(G,{children:"~/.ssh/config"}),l.length===0?(0,e.jsx)(p,{disabled:!0,children:"No hosts"}):l.map(t=>(0,e.jsxs)(p,{onSelect:s(()=>J(t),"onSelect"),children:[(0,e.jsx)("span",{className:"truncate",children:t.name}),(0,e.jsx)("span",{className:"ml-auto truncate pl-2 text-xs text-muted-foreground",children:t.destination})]},t.id)),(0,e.jsx)(G,{children:"Saved"}),_.length===0?(0,e.jsx)(p,{disabled:!0,children:"No saved hosts"}):_.map(t=>(0,e.jsxs)(p,{onSelect:s(()=>J(t),"onSelect"),children:[(0,e.jsx)("span",{className:"truncate",children:t.name}),(0,e.jsxs)("span",{className:"ml-auto truncate pl-2 text-xs text-muted-foreground",children:[t.destination,t.port===null?"":`:${t.port}`]})]},t.id))]}),x.data?.configError?(0,e.jsx)(p,{disabled:!0,className:"text-muted-foreground",children:x.data.configError}):null]}):null,(0,e.jsx)(G,{children:"Environments"}),z.length===0?(0,e.jsx)(p,{disabled:!0,children:w.isLoading?"Loading…":"No SSH environments"}):z.map(t=>(0,e.jsx)(p,{onSelect:s(()=>de(t.id,t.name),"onSelect"),children:t.name},t.id)),(0,e.jsx)(Ne,{}),f?(0,e.jsx)(p,{onSelect:s(()=>H(!0),"onSelect"),children:"Connect to host…"}):null,(0,e.jsx)(p,{asChild:!0,children:(0,e.jsx)(ve,{to:"/company/settings/instance/environments",children:"Manage SSH environments"})})]})]}),(0,e.jsx)(S,{type:"button",variant:"ghost",size:"icon-xs","aria-label":r.panes.length>=F?"Close split":"Split terminal","aria-pressed":r.panes.length>=F,onClick:ce,disabled:q||r.panes.length===0,children:(0,e.jsx)(Qe,{"aria-hidden":!0})})]}),(0,e.jsx)(Oe,{companyId:n,open:L,savedHosts:_,onOpenChange:H,onConnect:J}),E?(0,e.jsx)("p",{className:"shrink-0 border-b border-border px-3 py-1 text-xs text-destructive",role:"alert",children:E}):null,r.panes.length===0?(0,e.jsxs)("div",{className:"flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center",children:[(0,e.jsx)(V,{className:"size-6 text-muted-foreground","aria-hidden":!0}),(0,e.jsx)("p",{className:"text-sm text-muted-foreground",children:"No terminal sessions yet."}),f?(0,e.jsxs)(S,{type:"button",variant:"outline",size:"sm",onClick:s(()=>R(),"onClick"),disabled:q,children:[(0,e.jsx)(se,{"aria-hidden":!0}),"New local terminal"]}):(0,e.jsx)("p",{className:"max-w-md text-xs text-muted-foreground",children:"A shell on this host needs instance admin access. Open an SSH terminal to a registered environment instead."})]}):(0,e.jsx)(Ae,{className:"min-h-0 flex-1",children:r.panes.map((t,a)=>(0,e.jsxs)(d.Fragment,{children:[a>0?(0,e.jsx)(Te,{}):null,(0,e.jsx)(Pe,{id:`terminal-pane-${a}`,minSize:"20%",children:(0,e.jsx)("div",{className:ee("h-full min-h-0",r.panes.length>1&&a===r.focused&&"ring-1 ring-inset ring-ring"),onFocusCapture:s(()=>m(i=>i.focused===a?i:{...i,focused:a}),"onFocusCapture"),onMouseDown:s(()=>m(i=>i.focused===a?i:{...i,focused:a}),"onMouseDown"),"data-testid":`terminal-pane-${a}`,children:(0,e.jsx)(Ke,{companyId:n,sessionId:t,active:a===r.focused,onSessionChange:s(i=>{I.has(i.id)&&O(i)},"onSessionChange"),onExit:s(i=>{const c=I.get(t);c&&O({...c,status:"exited",exitCode:i.exitCode})},"onExit")})})})]},t))})]})}s(Ge,"TerminalWorkspace");function at(){const{selectedCompanyId:n}=ge(),{setBreadcrumbs:o}=be();return(0,d.useEffect)(()=>{o([{label:"Terminal"}])},[o]),n?(0,e.jsx)(Ge,{companyId:n},n):(0,e.jsx)("p",{className:"text-sm text-muted-foreground",children:"Select a company to open a terminal."})}s(at,"Terminal");export{at as Terminal};
@@ -1 +1 @@
1
- var c=Object.defineProperty;var n=(e,o)=>c(e,"name",{value:o,configurable:!0});import{Rt as p}from"./index-5wqemOVn.js";import{PAPERCLIP_CHARACTER as a}from"./character-CguPM7IG.js";import{o as l,r as f}from"./model-DL5biheO.js";function b(e,o=!1){const r=p[o?"muted-dream":e.paletteId],i={...a.character,id:"paperclip-cap-v1",name:"Agent",color:r.a,color2:r.b,followCursor:!0,followRotation:!0},s=f({version:1,name:"Paperclip",characters:[i],expressions:a.expressions,animations:a.animations},i);for(const t of s.animations)t.id==="happy"&&(t.loop=!1);return s}n(b,"characterDefinition");function d(e){return e==="success"?"happy":e==="rest"?"idle":e}n(d,"animationId");function C(e,o){if(o==="rest"){const r=e.expressions.find(i=>i.id==="idle")??e.expressions[0];return{pose:{...r.beats[0].pose},blink:0,bob:0,breathe:0,expressionId:r.id,beatIndex:0,stepIndex:0,faceLayers:void 0}}return l(e,d(o),.6)}n(C,"characterStill");export{d as animationId,b as characterDefinition,C as characterStill};
1
+ var c=Object.defineProperty;var n=(e,o)=>c(e,"name",{value:o,configurable:!0});import{Rt as p}from"./index-Yr0PvgVh.js";import{PAPERCLIP_CHARACTER as a}from"./character-CguPM7IG.js";import{o as l,r as f}from"./model-DL5biheO.js";function b(e,o=!1){const r=p[o?"muted-dream":e.paletteId],i={...a.character,id:"paperclip-cap-v1",name:"Agent",color:r.a,color2:r.b,followCursor:!0,followRotation:!0},s=f({version:1,name:"Paperclip",characters:[i],expressions:a.expressions,animations:a.animations},i);for(const t of s.animations)t.id==="happy"&&(t.loop=!1);return s}n(b,"characterDefinition");function d(e){return e==="success"?"happy":e==="rest"?"idle":e}n(d,"animationId");function C(e,o){if(o==="rest"){const r=e.expressions.find(i=>i.id==="idle")??e.expressions[0];return{pose:{...r.beats[0].pose},blink:0,bob:0,breathe:0,expressionId:r.id,beatIndex:0,stepIndex:0,faceLayers:void 0}}return l(e,d(o),.6)}n(C,"characterStill");export{d as animationId,b as characterDefinition,C as characterStill};