1import{r as l,v as E,j as e}from"./entry-BCe1jibU.js";import{u as K,a as H}from"./pathPrefix-DU8I2oCO.js";import{ad as ae,ae as ne}from"./admin-CNoMlLPj.js";import{E as ie,Y as le,y as oe}from"./icons-B6i7AQrN.js";import{s as O}from"./toast-Blm9fZ-n.js";import{D as ce,N as S,a as P,e as B,f as de,c as $,C as ue,g as me}from"./primitives-PzVjDGvc.js";import{c as A}from"./adminView-DkbsldBq.js";import"./types-CjJu7saM.js";import"./Identity-QN8NXWkM.js";import"./billing-XWNTP5pu.js";import"./EvalCloneMarker-DeoTUGFR.js";import"./speechToText-BOPKEWM6.js";import"./types-CLw4uLQj.js";import"./models-DiMb92RR.js";import"./types-CcG5mtI4.js";import"./authStep-Vc6mlF9x.js";import"./types-9eUqxxwr.js";import"./parsePhoneNumber-DJPI2TOO.js";import"./normalizeArguments-DLwglSfj.js";import"./types-BLuqObM8.js";import"./lindyTool-CidTJeMk.js";import"./hostedMcpProviders-R1ropXKE.js";import"./NativeButton-DgYgvC9L.js";import"./buttonStyles-DgzPUjES.js";import"./bundle-mjs-CSqCgoPn.js";import"./index-pNUg2pyP.js";import"./Scrollable-BvCr7Sb1.js";import"./XMarkIcon-CqPeZFAz.js";import"./InformationCircleIcon-DCmSqGbl.js";import"./adminLayout-OU-AekOj.js";import"./auth-CgkXDIO7.js";import"./graphql-4Y3Cu4kt.js";import"./operations-Dn0xY-u2.js";import"./session-C75oaowB.js";import"./identity-X0DTFeA9.js";import"./shellCache-Br8NUPQk.js";function V(s){const t=s?.progress?.stats;return!!(t&&(t.alreadyRunningUsers>0||t.skippedNoAgentDefinitionUsers>0))}function _(s){const t=s?.progress?.stats;return!!(t&&(t.failedUsers>0||t.failedAgentDefinitionIds.length>0))}function pe(s){return V(s)||_(s)}function W(s){return!!(s&&!["COMPLETED","NOT_FOUND","RUNNING"].includes(s.status))}function ge(s,t){const n=s?.progress,o=!!(n?.ceiling&&t?.progress?.ceiling===n.ceiling),c=t?.progress?.cursor?.agentDefinitionId??null,g=!!(t?.status==="COMPLETED"&&t?.progress?.ceiling&&c),a=o||g?t:null,d=s?.status==="COMPLETED"&&!!n,x=d&&!!n?.ceiling,f=pe(s)||W(s)||V(t)||W(t)&&(!t?.progress?.ceiling||o),u=_(t),m=a?.progress?.cursor?.agentDefinitionId??null,b=m?a?.progress?.ceiling??null:n?.ceiling??null,y=!!(x&&!f&&u&&a?.status==="COMPLETED"&&m),F=f||u&&!y,N=f||u,p=!!(d&&!N&&n?.stats.eligibleUsers===0),j=!!(d&&!N&&a?.status==="COMPLETED"&&!a.progress?.cursor),C=!a||a.status==="COMPLETED";return{canContinueWithDeferredFailures:y,canStartLive:!!(x&&C&&t?.status!=="RUNNING"&&!N&&!j&&!p),ceiling:b,complete:j,cursor:m,deferredFailedUsers:Math.max(a?.progress?.stats.failedUsers??0,a?.progress?.stats.failedAgentDefinitionIds.length??0),hasBlockingFailures:F,hasFailures:N,liveMatchesDryRun:o,nothingToRepair:p}}const fe=3e3,he=[10,50,100,500,1e3],xe=[1,2,3,5],be=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,ye={scannedAgents:"Agents scanned",eligibleUsers:"Eligible users",usersMigrated:"Users repaired",memoriesMigrated:"Mongo memories accounted for",referencesWritten:"References written",skippedNoActiveMemoryUsers:"No active Mongo memory",skippedAlreadySeededUsers:"Already repaired",skippedNotSeededUsers:"Not seeded yet",alreadyRunningUsers:"Already running",skippedNoAgentDefinitionUsers:"Agent unavailable",failedUsers:"Failed users"},Ne=Object.entries(ye);function je(s){return[...new Set(s.split(/[\s,;]+/).map(t=>t.trim().toLowerCase()))].filter(Boolean).sort()}async function ve(s,t){return await K(H.call(ne,{body:{dryRun:s,emails:t}}))}function we(s){return s instanceof Error?s.message:"Unknown error"}function G({dryRun:s,emails:t,enabled:n,refreshKey:o}){const[c,g]=l.useState(),[a,d]=l.useState(!1),[x,f]=l.useState(!1);return l.useEffect(()=>{if(!n){g(void 0),d(!1),f(!1);return}let u=!1,m;g(void 0),d(!0),f(!1);const b=async()=>{try{const y=await ve(s,t);if(u)return;g(y),d(!1),y.status==="RUNNING"&&(m=window.setTimeout(()=>E(b()),fe))}catch{if(u)return;d(!1),f(!0)}};return E(b()),()=>{u=!0,m!==void 0&&window.clearTimeout(m)}},[s,t,n,o]),{error:x,loading:a,value:c}}function z({label:s,loading:t,value:n}){const o=n?.progress?.stats,g=!!(o&&(o.alreadyRunningUsers>0||o.failedUsers>0||o.failedAgentDefinitionIds.length>0||o.skippedNoAgentDefinitionUsers>0))?"text-red-11":n?.status==="COMPLETED"?"text-green-11":n?.status==="RUNNING"?"text-blue-11":"text-slate-11";return e.jsxs("section",{className:"border-t border-edge py-5 first:border-t-0 first:pt-0",children:[e.jsxs("div",{className:"mb-4 flex flex-wrap items-baseline justify-between gap-3",children:[e.jsx("h2",{className:"text-base font-semibold text-slate-12",children:s}),e.jsx("span",{className:`text-sm font-medium ${g}`,children:t?"LOADING":n?.status??"NOT FOUND"})]}),n?.progress&&o?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-4 grid gap-2 text-xs text-slate-11 md:grid-cols-2",children:[e.jsxs("div",{children:["Ceiling: ",e.jsx("code",{children:n.progress.ceiling??"none"})]}),e.jsxs("div",{children:["Cursor:"," ",e.jsx("code",{children:n.progress.cursor?.agentDefinitionId??"complete"})]})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full max-w-3xl text-left text-sm",children:[e.jsx("thead",{className:"text-xs text-slate-11",children:e.jsxs("tr",{className:"border-b border-edge",children:[e.jsx("th",{className:"py-2 pr-4",children:"Metric"}),e.jsx("th",{className:"py-2 text-right",children:"Count"})]})}),e.jsx("tbody",{children:Ne.map(([a,d])=>e.jsxs("tr",{className:"border-b border-edge",children:[e.jsx("td",{className:"py-2 pr-4 text-slate-11",children:d}),e.jsx("td",{className:`py-2 text-right font-mono ${(a==="alreadyRunningUsers"||a==="failedUsers"||a==="skippedNoAgentDefinitionUsers")&&o[a]>0?"text-red-11":"text-slate-12"}`,children:o[a]})]},a))})]})}),o.failedAgentDefinitionIds.length>0?e.jsxs("div",{className:"mt-3 text-xs text-red-11",children:["Failed agents: ",e.jsx("code",{children:o.failedAgentDefinitionIds.join(", ")})]}):null]}):e.jsx("div",{className:"text-sm text-slate-11",children:n?.progressUnavailable?"Progress unavailable":"No run found"})]})}function ls(){const[s,t]=l.useState("email"),[n,o]=l.useState(""),[c,g]=l.useState(null),[a,d]=l.useState("10"),[x,f]=l.useState("1"),[u,m]=l.useState(null),[b,y]=l.useState(0),[F,N]=l.useState(null),p=l.useMemo(()=>je(n),[n]),j=p.length>0&&p.length<=100&&p.every(i=>be.test(i)),C=c?.join(",")===p.join(",")&&j,I=s==="email"?c:null,T=s==="batch"||c!==null,w=G({dryRun:!0,emails:I,enabled:T,refreshKey:b}),v=G({dryRun:!1,emails:I,enabled:T,refreshKey:b}),r=ge(w.value??null,v.value??null),h=w.value?.status==="RUNNING"||v.value?.status==="RUNNING",U=r.canContinueWithDeferredFailures?[v.value?.workflowId,r.ceiling,r.cursor,r.deferredFailedUsers,...v.value?.progress?.stats.failedAgentDefinitionIds??[]].join(":"):null,D=U!==null&&U===F,R=(r.canStartLive||r.canContinueWithDeferredFailures&&D)&&(s==="batch"||C),q=s==="email"&&!!c&&!C,k=l.useCallback(async i=>{m(i.dryRun?"dry":"live");try{const M=await K(H.call(ae,{body:i}));i.dryRun&&i.emails&&g(i.emails),i.dryRun||N(null),y(re=>re+1),O({durationMs:5e3,label:`${i.dryRun?"Dry run":"Live repair"} started: ${M.workflowId}`,level:"success"})}catch(M){O({durationMs:7e3,label:`Failed to start memory repair: ${we(M)}`,level:"danger"})}finally{m(null)}},[]),Y=l.useCallback(()=>{s==="email"&&!j||E(k({ceiling:null,concurrency:3,cursor:null,dryRun:!0,emails:s==="email"?p:null,maxTargets:null,pageSize:25}))},[j,s,p,k]),J=l.useCallback(()=>
1{if(!r.ceiling||!R)return;const i=s==="email"?1e3:Number(a);E(k({ceiling:r.ceiling,concurrency:Number(x),cursor:r.cursor,dryRun:!1,emails:s==="email"?c:null,maxTargets:i,pageSize:Math.min(i,100)}))},[c,a,R,x,s,r,k]),Q=l.useCallback(()=>{h||t("email")},[h]),X=l.useCallback(()=>{h||t("batch")},[h]),Z=l.useCallback(i=>N(i?U:null),[U]),ee=r.hasBlockingFailures?"Blocked by unresolved users":r.canContinueWithDeferredFailures&&!D?`Acknowledge ${r.deferredFailedUsers} failed users to continue`:r.canContinueWithDeferredFailures?`Ready to continue with ${r.deferredFailedUsers} failed users deferred`:q?"Dry run required for this email set":r.nothingToRepair?"Nothing to repair":r.complete?"Repair complete":R?r.cursor?"Ready to continue":"Ready to repair":h?"Workflow running":"Dry run required",se=s==="email"?1e3:Number(a),L=s==="email"?c:null,te=`Repair memory files for ${L?`${L.length} selected email(s)`:`the next ${se} users`}? This writes to production.${D?` ${r.deferredFailedUsers} failed users from the prior batch will be deferred.`:""}`;return e.jsxs(ce,{title:"Memory repair backfill",description:"Repair active Mongo memories into the filesystem-backed memory store.",width:"max-w-5xl",children:[e.jsx(S,{tone:"amber",children:"Always complete a clean dry run for the current scope before starting a live repair."}),e.jsxs(P,{title:"Scope",children:[e.jsxs("div",{className:"mb-5 flex flex-wrap gap-2",children:[e.jsxs(A,{"aria-pressed":s==="email",disabled:h,level:s==="email"?"primary":"outline",onClick:Q,children:[e.jsx(ie,{className:"size-4"}),"By email"]}),e.jsxs(A,{"aria-pressed":s==="batch",disabled:h,level:s==="batch"?"primary":"outline",onClick:X,children:[e.jsx(le,{className:"size-4"}),"Batches"]})]}),s==="email"?e.jsx(B,{label:"Email addresses",hint:`${p.length} selected; separate with spaces, commas, or semicolons.`,children:e.jsx(de,{className:"max-w-3xl",placeholder:"[email protected]",rows:3,value:n,onChange:o})}):e.jsx(B,{label:"Batch size",children:e.jsx($,{className:"max-w-48",value:a,onChange:d,children:he.map(i=>e.jsx("option",{value:i,children:i},i))})}),e.jsxs("div",{className:"mt-5 flex flex-wrap items-end gap-4",children:[e.jsx(B,{label:"Concurrency",children:e.jsx($,{className:"w-40",value:x,onChange:f,children:xe.map(i=>e.jsx("option",{value:i,children:i},i))})}),e.jsxs(A,{disabled:h||u!==null||s==="email"&&!j,level:"outline",loading:u==="dry",onClick:Y,children:[e.jsx(oe,{className:"size-4"}),"Dry run"]}),e.jsx(ue,{confirmation:te,disabled:!R||h||u!==null,loading:u==="live",onConfirm:J,children:s==="email"?"Backfill emails":"Run next batch"})]}),r.canContinueWithDeferredFailures?e.jsx(S,{tone:"amber",children:e.jsx(me,{checked:D,onChange:Z,children:e.jsxs("span",{children:[e.jsxs("span",{className:"block font-medium",children:["I saved these ",r.deferredFailedUsers," failed agent IDs; defer them and continue"]}),e.jsx("span",{className:"block text-xs",children:"The next batch replaces this status view. Retry the saved IDs after the remaining batches finish."})]})})}):null]}),e.jsx(S,{tone:r.hasBlockingFailures?"red":r.canContinueWithDeferredFailures&&!D?"amber":r.complete?"green":R?"amber":"slate",children:ee}),w.error||v.error?e.jsx(S,{tone:"red",children:"Could not load workflow status."}):null,e.jsxs(P,{children:[e.jsx(z,{label:"Dry run",loading:w.loading,value:w.value}),e.jsx(z,{label:"Live repair",loading:v.loading,value:v.value})]})]})}export{ls as MemoryRepairBackfillView,je as parseMemoryRepairEmails};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.