1import{c as Ce,j as e,bo as pe,c6 as fe,c7 as je,c8 as Ne,c5 as be,dk as _e,cl as ve,aB as f,L as d,u as Q,aN as X,m as x,s as c,aP as de,ca as q,B as u,cI as oe,b4 as me,bN as xe,S as Fe,M as Me,N as Ee,O as Te,P as he,Z as Ae,A as Re,aW as Ie,e as De,f as Pe,g as Be,h as Ge,i as Le,k as $e,l as ze,d1 as Je,d2 as Ue,ck as Oe,d3 as qe,an as He,ba as We,bd as Ke}from"./index-CqN5fG2-.js";import{r as h}from"./vendor-charts-DSwqWjoP.js";import{T as Ye,a as Ve,b as ee,c as P,d as Ze,e as C}from"./table-CUHG34y3.js";import{A as F,b as M}from"./alert-CluhtmL8.js";import{g as Qe}from"./vendor-dates-C70Beoa9.js";import{C as ue}from"./circle-check-big-Dl7Rc5Ae.js";import{C as H}from"./circle-x-43HiFYSr.js";import{S as Xe}from"./skip-forward-CFamFV_S.js";import{T as se}from"./trending-up-VAuv1u-e.js";import{R as ge}from"./rotate-ccw-wRTDJoiU.js";import{P as es}from"./play-Cqe7Whqa.js";import"./vendor-qr-CJWhnf2P.js";(function(){try{var i=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{};i.SENTRY_RELEASE={id:"ac030688110ba1ddc2b6cf0f37b3796303083cdc"};var N=new i.Error().stack;N&&(i._sentryDebugIds=i._sentryDebugIds||{},i._sentryDebugIds[N]="08989164-2be8-40fe-9540-fd62afa32d37",i._sentryDebugIdIdentifier="sentry-dbid-08989164-2be8-40fe-9540-fd62afa32d37")}catch{}})();/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const ss=Ce("CircleStop",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["rect",{x:"9",y:"9",width:"6",height:"6",rx:"1",key:"1ssd4o"}]]);function as({status:i,mode:N,totalProfiles:r,processed:g,successCount:B,failedCount:G,skippedCount:L,currentBatch:$,batchSize:o,startedAt:E,heartbeatAt:y,createdAt:w}){const W=()=>{switch(i){case"pending":return e.jsxs(f,{variant:"secondary",className:"gap-1.5",children:[e.jsx(d,{className:"h-3 w-3 animate-spin"})," Starting..."]});case"processing":return e.jsxs(f,{variant:"default",className:"gap-1.5",children:[e.jsx(d,{className:"h-3 w-3 animate-spin"})," Processing"]});case"cancelling":return e.jsxs(f,{variant:"destructive",className:"gap-1.5",children:[e.jsx(d,{className:"h-3 w-3 animate-spin"})," Cancelling..."]});case"completed":return e.jsxs(f,{variant:"outline",className:"gap-1.5 bg-green-500/10 text-green-700 dark:text-green-400 border-green-500/20",children:[e.jsx(ue,{className:"h-3 w-3"})," Completed"]});case"cancelled":return e.jsxs(f,{variant:"outline",className:"gap-1.5",children:[e.jsx(H,{className:"h-3 w-3"})," Cancelled"]});case"failed":return e.jsxs(f,{variant:"destructive",className:"gap-1.5",children:[e.jsx(H,{className:"h-3 w-3"})," Failed"]});default:return e.jsx(f,{variant:"outline",children:i})}},j=()=>{const R={fast_initial:"Fast Initial",background_maintenance:"Background Maintenance"};return e.jsx(f,{variant:"secondary",children:R[N]||N})},k=r&&g?Math.round(g/r*100):0,z=E||w,T=Date.now()-new Date(z).getTime(),A=Math.floor(T/1e3/60),K=Math.floor(T/1e3%60);let b=0,_=0;if(E&&g&&g>0){const R=Date.now()-new Date(E).getTime();b=g/(R/1e3/60);const Y=(r||0)-g;_=b>0?Math.ceil(Y/b):0}const J=y?Qe(new Date(y),{addSuffix:!0}):"just now";return e.jsxs(pe,{className:"border-primary/20",children:[e.jsxs(fe,{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(je,{className:"text-lg",children:"Embedding Generation Progress"}),e.jsxs("div",{className:"flex gap-2",children:[W(),j()]})]}),e.jsxs(Ne,{children:[i==="pending"&&"Initializing generation process...",i==="processing"&&`Processing batch ${$||0} of ${Math.ceil((r||0)/(o||50))}`,i==="completed"&&"Generation completed successfully",i==="cancelled"&&"Generation was cancelled",i==="failed"&&"Generation encountered an error"]})]}),e.jsxs(be,{className:"space-y-4",children:[r&&r>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex justify-between text-sm",children:[e.jsxs("span",{className:"text-muted-foreground",children:[g||0," / ",r," profiles"]}),e.jsxs("span",{className:"font-medium",children:[k,"%"]})]}),e.jsx(_e,{value:k,className:"h-2",indicatorClassName:i==="completed"?"bg-green-500":i==="failed"||i==="cancelled"?"bg-destructive":"bg-primary"})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(ue,{className:"h-4 w-4 text-green-600 dark:text-green-400"}),e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:B||0}),e.jsx("div",{className:"text-muted-foreground text-xs",children:"Success"})]})]}),e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(H,{className:"h-4 w-4 text-destructive"}),e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:G||0}),e.jsx("div",{className:"text-muted-foreground text-xs",children:"Failed"})]})]}),e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(Xe,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:L||0}),e.jsx("div",{className:"text-muted-foreground text-xs",children:"Skipped"})]})]})]}),(i==="processing"||i==="completed")&&e.jsxs("div",{className:"grid grid-cols-2 gap-4 pt-2 border-t",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(ve,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("div",{children:[e.jsxs("div",{className:"font-medium",children:[A,"m ",K,"s"]}),e.jsx("div",{className:"text-muted-foreground text-xs",children:"Elapsed"})]})]}),i==="processing"&&b>0&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(se,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("div",{children:[e.jsxs("div",{className:"font-medium",children:["~",Math.round(b)," /min"]}),e.jsx("div",{className:"text-muted-foreground text-xs",children:"Rate"})]})]})]}),i==="processing"&&b>0&&_>0&&e.jsxs("div",{className:"flex items-center justify-between text-sm p-3 bg-muted/50 rounded-md",children:[e.jsx("span",{className:"text-muted-foreground",children:"Estimated time remaining:"}),e.jsx("span",{className:"font-medium",children:_<60?`~${_} minutes`:`~${Math.floor(_/60)}h ${_%60}m`})]}),e.jsxs("div",{className:"text-xs text-muted-foreground text-center pt-2 border-t",children:["Last updated ",J]})]})]})}function ts(){var ne,re,ie,le;const[i,N]=h.useState(null),[r,g]=h.useState({total:0,processed:0,success:0,failed:0,skipped:0,currentBatch:0,totalBatches:0,isRunning:!1,errors:[]}),[B,G]=h.useState(!1),[L,$]=h.useState(!1),[o,E]=h.useState(null),{data:y}=Q({queryKey:["communities-for-embedding-selector"],queryFn:async()=>{const{data:s,error:n}=await c.rpc("list_communities_for_admin");if(n)throw n;return(s??[]).map(t=>({id:t.id,name:t.name}))}}),[w,W]=h.useState(""),[j,k]=h.useState(null),[z,T]=h.useState(!1),[A,K]=h.useState(null),[b,_]=h.useState(!1),[J,R]=h.useState(!1),Y=async()=>{const s=w.split(` 7`).map(n=>
7n.trim()).filter(Boolean);if(s.length!==0){T(!0),k(null);try{const{data:n,error:t}=await c.functions.invoke("generate-embeddings",{body:{userIds:s,community_id:o||null}});if(t)throw t;k(n)}catch(n){k({error:n instanceof Error?n.message:"Unknown error"})}finally{T(!1)}}},ye=async()=>{const s=w.split(` 8`).map(m=>m.trim()).filter(Boolean);if(s.length===0)return;_(!0);const{data:n}=await c.from("profiles").select("id, user_id").in("user_id",s),t=(n||[]).map(m=>m.id),D=Object.fromEntries((n||[]).map(m=>[m.id,m.user_id])),{data:Z}=await c.from("profile_embeddings").select("profile_id, scope, stale, updated_at").in("profile_id",t),O=(Z||[]).map(m=>({...m,user_id:D[m.profile_id]}));K(O),_(!1)},[we,ae]=h.useState(null),{data:l,refetch:U}=Q({queryKey:["active-embedding-job"],queryFn:async()=>{const{data:s,error:n}=await c.from("embedding_generation_jobs").select("*").in("status",["pending","processing"]).order("created_at",{ascending:!1}).limit(1).maybeSingle();if(n&&n.code!=="PGRST116"&&console.error("Error fetching active job:",n),!s){const{data:t}=await c.from("embedding_generation_jobs").select("*").in("status",["completed","cancelled","failed"]).order("completed_at",{ascending:!1}).limit(1).maybeSingle();return t&&we==="processing"&&t.id===i&&(g({total:t.total_profiles,processed:t.profiles_processed,success:t.success_count,failed:t.failed_count,skipped:t.skipped_count,currentBatch:t.current_batch,totalBatches:Math.ceil(t.total_profiles/(t.batch_size||50)),isRunning:!1,errors:t.errors||[]}),t.status==="completed"?(x.success(`â Embedding generation completed! Processed ${t.profiles_processed} profiles (${t.success_count} success, ${t.failed_count} failed)`),I()):t.status==="cancelled"?(x.warning("â ï¸ Embedding generation was cancelled"),I()):t.status==="failed"&&(x.error("â Embedding generation failed"),I())),N(null),ae(null),r.isRunning&&g(D=>({...D,isRunning:!1})),null}return s&&(N(s.id),ae(s.status),g({total:s.total_profiles,processed:s.profiles_processed,success:s.success_count,failed:s.failed_count,skipped:s.skipped_count,currentBatch:s.current_batch,totalBatches:Math.ceil(s.total_profiles/s.batch_size),isRunning:s.status==="processing",errors:[]})),s},refetchInterval:i?2e3:!1}),{data:a,isLoading:v,isFetching:S,refetch:I}=Q({queryKey:["embedding-stats",o],queryFn:async()=>{var O,m,ce;const{data:s}=await c.rpc("get_embedding_stats",{p_community_id:o||null}),n=Number(((O=s==null?void 0:s[0])==null?void 0:O.total_profiles)||0),t=Number(((m=s==null?void 0:s[0])==null?void 0:m.profiles_with_embeddings)||0),D=Number(((ce=s==null?void 0:s[0])==null?void 0:ce.stale_embeddings)||0),{data:Z}=await c.rpc("get_embedding_analytics");return{total_profiles:n||0,profiles_with_embeddings:t,profiles_without_embeddings:(n||0)-t,stale_embeddings:D,analytics:Z||{currently_stale:0,total_stale_events:0,avg_hours_to_regeneration:0,max_hours_stale:0,regenerated_last_24h:0,became_stale_last_24h:0}}}}),p=X({mutationFn:async s=>{const{data:n,error:t}=await c.functions.invoke("start-embedding-generation",{body:{mode:s.mode,force_regenerate_all:s.force_regenerate_all||!1,community_id:s.community_id||null}});if(t)throw t;return n},onSuccess:s=>{const n=s.mode==="fast_initial"?"â¡ Fast generation started! This will process all profiles in a few hours.":s.mode==="force_regenerate_all"?"ð Force regeneration started! This will regenerate ALL embeddings.":s.mode==="fast_stale"?"â¡ Fast stale regeneration started! This will update stale embeddings in a few hours.":"Background generation started";x.success(n),N(s.job_id),U(),I()},onError:s=>{x.error(`Failed to start generation: ${s.message}`)}}),V=X({mutationFn:async s=>{const{data:n,error:t}=await c.functions.invoke("stop-embedding-generation",{body:{job_id:s}});if(t)throw t;return n},onSuccess:()=>{x.success("Stopping generation after current batch..."),U()},onError:s=>{x.error(`Failed to stop generation: ${s.message}`)}});X({mutationFn:async s=>{const{data:n,error:t}=await c.functions.invoke("generate-embeddings",{body:{userIds:s,community_id:o||null}});if(t)throw t;return n}});const Se=async()=>{G(!0);try{const{data:s,error:n}=await c.functions.invoke("start-embedding-generation",{body:{mode:"fast_initial",community_id:o||null}});if(n)throw n;if(s.success)x.success(`Started generating embeddings for ${s.total_profiles} profiles. You can safely leave this page - the job runs on the server.`),U();else throw new Error(s.error||"Failed to start generation")}catch(s){console.error("Error starting embedding generation:",s),x.error(s instanceof Error?s.message:"Failed to start generation")}finally{G(!1)}},te=r.total>0?Math.round(r.processed/r.total*100):0,ke=a?Math.ceil((a.total_profiles||0)/1e3):0;return e.jsxs(pe,{children:[e.jsxs(fe,{children:[e.jsxs(je,{className:"flex items-center gap-2",children:[e.jsx(de,{className:"h-5 w-5"}),"Semantic Search Embeddings"]}),e.jsx(Ne,{children:"Generate AI embeddings for profiles to enable semantic search capabilities"})]}),e.jsxs(be,{className:"space-y-6",children:[l&&e.jsxs(e.Fragment,{children:[l.heartbeat_at&&new Date().getTime()-new Date(l.heartbeat_at).getTime()>12e4&&e.jsxs(F,{variant:"destructive",children:[e.jsx(q,{className:"h-4 w-4"}),e.jsxs(M,{className:"flex items-center justify-between",children:[e.jsxs("span",{children:["â ï¸ Job heartbeat is stale (last update: ",new Date(l.heartbeat_at).toLocaleStr
8ing(),"). The generation may be stuck."]}),e.jsxs(u,{size:"sm",variant:"outline",disabled:L,className:"ml-4 shrink-0",onClick:async()=>{$(!0);try{const{error:s}=await c.functions.invoke("detect-stuck-jobs",{body:{force_job_id:l.id}});if(s)throw s;x.success("Resume triggered â job should restart shortly"),U()}catch(s){x.error("Failed to trigger resume",{description:s.message})}finally{$(!1)}},children:[L?e.jsx(d,{className:"h-3 w-3 animate-spin mr-1"}):e.jsx(ge,{className:"h-3 w-3 mr-1"}),"Resume"]})]})]}),e.jsx(as,{status:l.status,mode:l.mode,totalProfiles:l.total_profiles,processed:l.profiles_processed,successCount:l.success_count,failedCount:l.failed_count,skippedCount:l.skipped_count,currentBatch:l.current_batch,batchSize:l.batch_size,startedAt:l.started_at,heartbeatAt:l.heartbeat_at,createdAt:l.created_at})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-5 gap-4",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Total Profiles"}),e.jsx("p",{className:"text-2xl font-bold",children:v||S?"...":a==null?void 0:a.total_profiles.toLocaleString()})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"With Embeddings"}),e.jsx("p",{className:"text-2xl font-bold text-green-600",children:v||S?"...":a==null?void 0:a.profiles_with_embeddings.toLocaleString()})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Without Embeddings"}),e.jsx("p",{className:"text-2xl font-bold text-orange-600",children:v||S?"...":a==null?void 0:a.profiles_without_embeddings.toLocaleString()})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Stale (Need Update)"}),e.jsx("p",{className:"text-2xl font-bold text-yellow-600",children:v||S?"...":a==null?void 0:a.stale_embeddings.toLocaleString()})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Coverage"}),e.jsx("p",{className:"text-2xl font-bold",children:v||S?"...":a&&a.total_profiles>0?`${Math.round(a.profiles_with_embeddings/a.total_profiles*100)}%`:"0%"})]})]}),a&&a.profiles_without_embeddings>0&&!l&&e.jsxs(F,{className:"border-orange-500/50 bg-orange-500/10",children:[e.jsx(q,{className:"h-4 w-4 text-orange-600"}),e.jsxs(M,{className:"flex flex-col gap-2",children:[e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsxs("span",{children:[e.jsxs("strong",{children:[a.profiles_without_embeddings," profile",a.profiles_without_embeddings!==1?"s":""]})," missing embeddings"]}),e.jsx(u,{size:"sm",onClick:Se,disabled:B,className:"gap-1.5",children:B?e.jsxs(e.Fragment,{children:[e.jsx(d,{className:"h-3.5 w-3.5 animate-spin"}),"Starting..."]}):e.jsxs(e.Fragment,{children:[e.jsx(oe,{className:"h-3.5 w-3.5"}),"Generate Now"]})})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"The job runs on the server - you can safely leave this page. Progress will show in the Active Job card above."})]})]}),r.isRunning&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsxs("span",{className:"text-muted-foreground",children:["Batch ",r.currentBatch," of ",r.totalBatches]}),e.jsxs("span",{className:"font-medium",children:[te,"%"]})]}),e.jsx(_e,{value:te,className:"h-2"})]}),e.jsxs("div",{className:"flex gap-4 text-sm",children:[e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(me,{className:"h-4 w-4 text-green-600"}),e.jsxs("span",{children:[r.success," success"]})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(H,{className:"h-4 w-4 text-red-600"}),e.jsxs("span",{children:[r.failed," failed"]})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(q,{className:"h-4 w-4 text-yellow-600"}),e.jsxs("span",{children:[r.skipped," skipped"]})]})]})]}),!r.isRunning&&r.processed>0&&e.jsxs(F,{children:[e.jsx(me,{className:"h-4 w-4"}),e.jsxs(M,{children:["Last run: Processed ",r.processed," profiles (",r.success," ","success, ",r.failed," failed, ",r.skipped," skipped)"]})]}),r.errors.length>0&&e.jsxs(F,{variant:"destructive",children:[e.jsx(q,{className:"h-4 w-4"}),e.jsx(M,{children:e.jsxs("div",{className:"space-y-1",children:[e.jsxs("p",{className:"font-medium",children:[r.errors.length," errors occurred:"]}),e.jsxs("div",{className:"max-h-32 overflow-y-auto text-xs space-y-1",children:[r.errors.slice(0,10).map((s,n)=>e.jsxs("div",{className:"font-mono",children:[s.userId.slice(0,8),"...: ",s.error]},n)),r.errors.length>10&&e.jsxs("p",{className:"text-muted-foreground",children:["+ ",r.errors.length-10," more errors"]})]})]})})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs(F,{children:[e.jsx(xe,{className:"h-4 w-4"}),e.jsx(M,{children:e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"font-medium",children:"Choose a generation mode:"}),e.jsxs("ul",{className:"text-sm space-y-1 ml-4 list-disc",children:[e.jsxs("li",{children:[e.jsx("strong",{children:"Fast Initial Generation"}),": Processes all profiles without embeddings in 4-8 hours using batch processing (50 profiles per batch, up to 100 OpenAI API calls at once)"]}
8),e.jsxs("li",{children:[e.jsx("strong",{children:"Background Maintenance"}),": Runs every 15 minutes to process stale embeddings and new profiles (20 profiles per batch)"]})]})]})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium text-muted-foreground whitespace-nowrap",children:"Target community:"}),e.jsxs(Fe,{value:o||"all",onValueChange:s=>E(s==="all"?null:s),children:[e.jsx(Me,{className:"w-[220px]",children:e.jsx(Ee,{placeholder:"All communities"})}),e.jsxs(Te,{children:[e.jsx(he,{value:"all",children:"All communities"}),y==null?void 0:y.map(s=>e.jsx(he,{value:s.id,children:Ae(y,s)},s.id))]})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[l&&l.status!=="completed"&&l.status!=="cancelled"?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex items-center gap-2 px-3 py-2 bg-primary/10 rounded-md",children:e.jsxs(f,{variant:"outline",className:"gap-1",children:[e.jsx(d,{className:"h-3 w-3 animate-spin"}),l.mode==="fast_initial"?"Fast Generation":l.mode==="fast_stale"?"Fast Stale":"Background"," Running"]})}),e.jsx(u,{onClick:()=>i&&V.mutate(i),disabled:V.isPending,variant:"destructive",className:"gap-2",children:V.isPending?e.jsxs(e.Fragment,{children:[e.jsx(d,{className:"h-4 w-4 animate-spin"}),"Stopping..."]}):e.jsxs(e.Fragment,{children:[e.jsx(ss,{className:"h-4 w-4"}),"Stop Generation"]})})]}):e.jsxs(e.Fragment,{children:[e.jsx(u,{onClick:()=>p.mutate({mode:"fast_initial",community_id:o}),disabled:p.isPending||v||(a==null?void 0:a.profiles_without_embeddings)===0,className:"gap-2",variant:"default",children:p.isPending?e.jsxs(e.Fragment,{children:[e.jsx(d,{className:"h-4 w-4 animate-spin"}),"Starting..."]}):e.jsxs(e.Fragment,{children:[e.jsx(de,{className:"h-4 w-4"}),"Fast Initial Generation"]})}),e.jsx(u,{onClick:()=>p.mutate({mode:"background_maintenance",community_id:o}),disabled:p.isPending||v,className:"gap-2",variant:"outline",children:p.isPending?e.jsxs(e.Fragment,{children:[e.jsx(d,{className:"h-4 w-4 animate-spin"}),"Starting..."]}):e.jsxs(e.Fragment,{children:[e.jsx(es,{className:"h-4 w-4"}),"Background Maintenance"]})}),e.jsx(u,{onClick:()=>p.mutate({mode:"fast_stale",community_id:o}),disabled:p.isPending||v||(a==null?void 0:a.stale_embeddings)===0,className:"gap-2",variant:"outline",children:p.isPending?e.jsxs(e.Fragment,{children:[e.jsx(d,{className:"h-4 w-4 animate-spin"}),"Starting..."]}):e.jsxs(e.Fragment,{children:[e.jsx(oe,{className:"h-4 w-4"}),"Fast Stale (",(a==null?void 0:a.stale_embeddings)||0,")"]})}),e.jsxs(Re,{children:[e.jsx(Ie,{asChild:!0,children:e.jsxs(u,{disabled:p.isPending||v,className:"gap-2",variant:"destructive",children:[e.jsx(ge,{className:"h-4 w-4"}),"Regenerate All"]})}),e.jsxs(De,{children:[e.jsxs(Pe,{children:[e.jsx(Be,{children:"Regenerate All Embeddings?"}),e.jsxs(Ge,{className:"space-y-2",children:[e.jsxs("p",{children:["This will regenerate embeddings for ",e.jsxs("strong",{children:["ALL ",(a==null?void 0:a.total_profiles)||0," profiles"]}),", regardless of whether they already have embeddings."]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Estimated time:"})," ~",ke," minutes"]}),e.jsx("p",{className:"text-sm",children:"â ï¸ This is useful when you've changed the embedding structure and need to rebuild everything from scratch."})]})]}),e.jsxs(Le,{children:[e.jsx($e,{children:"Cancel"}),e.jsx(ze,{onClick:()=>p.mutate({mode:"force_regenerate_all",force_regenerate_all:!0,community_id:o}),className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:"Regenerate All"})]})]})]})]}),e.jsx(u,{onClick:async()=>{await I(),x.success("Stats refreshed!")},variant:"outline",disabled:S,className:"gap-2",children:S?e.jsxs(e.Fragment,{children:[e.jsx(d,{className:"h-4 w-4 animate-spin"}),"Refreshing..."]}):e.jsxs(e.Fragment,{children:[e.jsx(se,{className:"h-4 w-4"}),"Refresh Stats"]})})]})]}),(a==null?void 0:a.analytics)&&e.jsxs("div",{className:"border-t pt-6",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[e.jsx(se,{className:"h-5 w-5 text-muted-foreground"}),e.jsx("h3",{className:"font-semibold",children:"Staleness Analytics"})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-3 gap-4",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsxs("p",{className:"text-sm text-muted-foreground flex items-center gap-1.5",children:[e.jsx(ve,{className:"h-3.5 w-3.5"}),"Avg. Time to Regeneration"]}),e.jsx("p",{className:"text-xl font-bold",children:a.analytics.avg_hours_to_regeneration>0?`${a.analytics.avg_hours_to_regeneration.toFixed(1)}h`:"N/A"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Max Hours Stale"}),e.jsx("p",{className:"text-xl font-bold text-orange-600",children:a.analytics.max_hours_stale>0?`${a.analytics.max_hours_stale.toFixed(1)}h`:"N/A"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Total Stale Events"}),e.jsx("p",{className:"text-xl font-bold",children:a.analytics.total_stale_events.toLocaleStr
8ing()})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Became Stale ",e.jsx(f,{variant:"outline",className:"ml-1 text-xs",children:"24h"})]}),e.jsx("p",{className:"text-xl font-bold text-yellow-600",children:a.analytics.became_stale_last_24h.toLocaleString()})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Regenerated ",e.jsx(f,{variant:"outline",className:"ml-1 text-xs",children:"24h"})]}),e.jsx("p",{className:"text-xl font-bold text-green-600",children:a.analytics.regenerated_last_24h.toLocaleString()})]})]})]}),e.jsxs(F,{children:[e.jsx(xe,{className:"h-4 w-4"}),e.jsxs(M,{className:"space-y-2",children:[e.jsx("p",{className:"font-medium",children:"How Background Embedding Generation Works:"}),e.jsxs("ul",{className:"list-disc list-inside space-y-1 text-sm",children:[e.jsxs("li",{children:[e.jsx("strong",{children:"Server-Side Processing:"})," Once started, generation runs on the server and continues even if you close your browser or computer."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Automatic Regeneration:"}),' When you update a profile, its embedding is marked as "stale" and will be automatically regenerated.']}),e.jsxs("li",{children:[e.jsx("strong",{children:"Manual Control:"}),' Use "Start Background Generation" to begin processing all profiles. Click "Stop Generation" to halt after the current batch.']}),e.jsxs("li",{children:[e.jsx("strong",{children:"Priority System:"})," Stale embeddings (from profile updates) are regenerated first, then new profiles are processed."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Batch Processing:"})," Profiles are processed in batches of 20 every 15 minutes to optimize performance."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Real-Time Tracking:"})," Progress updates every 5 seconds showing how many profiles have been processed successfully."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Email Notifications:"})," All admins receive email alerts when generation completes, is cancelled, or encounters errors."]})]})]})]}),e.jsxs(Je,{open:J,onOpenChange:R,children:[e.jsx(Ue,{asChild:!0,children:e.jsxs(u,{variant:"outline",className:"w-full justify-between",children:["Manual Generation (Debug)",e.jsx(Oe,{className:`h-4 w-4 transition-transform ${J?"rotate-180":""}`})]})}),e.jsxs(qe,{className:"mt-4 space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium",children:"User IDs (one per line)"}),e.jsx(He,{value:w,onChange:s=>W(s.target.value),placeholder:"paste user_id values here, one per line",rows:5,className:"font-mono text-xs"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(u,{onClick:Y,disabled:z||!w.trim(),size:"sm",children:[z&&e.jsx(d,{className:"h-4 w-4 animate-spin"}),"Generate Embeddings"]}),e.jsxs(u,{onClick:ye,disabled:b||!w.trim(),size:"sm",variant:"outline",children:[b&&e.jsx(d,{className:"h-4 w-4 animate-spin"}),"Check Results"]})]}),j&&e.jsx("div",{className:"rounded-md border p-3 text-sm space-y-1",children:j.error?e.jsxs("p",{className:"text-destructive",children:["Error: ",j.error]}):e.jsxs(e.Fragment,{children:[e.jsxs("p",{children:["â
Success: ",((ne=j.processed)==null?void 0:ne.success)??0]}),e.jsxs("p",{children:["âï¸ Skipped: ",((re=j.processed)==null?void 0:re.skipped)??0]}),e.jsxs("p",{children:["â Failed: ",((ie=j.processed)==null?void 0:ie.failed)??0]}),((le=j.errors)==null?void 0:le.length)>0&&e.jsx("ul",{className:"mt-1 list-disc list-inside text-destructive",children:j.errors.map((s,n)=>e.jsxs("li",{className:"truncate",children:[s.userId,": ",s.error]},n))})]})}),A&&e.jsx("div",{className:"rounded-md border overflow-auto",children:e.jsxs(Ye,{children:[e.jsx(Ve,{children:e.jsxs(ee,{children:[e.jsx(P,{className:"text-xs",children:"user_id"}),e.jsx(P,{className:"text-xs",children:"profile_id"}),e.jsx(P,{className:"text-xs",children:"scope"}),e.jsx(P,{className:"text-xs",children:"stale"}),e.jsx(P,{className:"text-xs",children:"updated_at"})]})}),e.jsx(Ze,{children:A.length===0?e.jsx(ee,{children:e.jsx(C,{colSpan:5,className:"text-center text-muted-foreground text-sm",children:"No rows found"})}):A.map((s,n)=>e.jsxs(ee,{children:[e.jsx(C,{className:"font-mono text-xs truncate max-w-[120px]",children:s.user_id}),e.jsx(C,{className:"font-mono text-xs truncate max-w-[120px]",children:s.profile_id}),e.jsx(C,{className:"text-xs",children:s.scope}),e.jsx(C,{className:"text-xs",children:s.stale?"Yes":"No"}),e.jsx(C,{className:"text-xs",children:s.updated_at?new Date(s.updated_at).toLocaleStr
8ing():"â"})]},n))})]})})]})]})]})]})}function ps(){const i=We();return e.jsxs("div",{className:"container mx-auto p-6 space-y-6",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(u,{variant:"ghost",size:"icon",onClick:()=>i("/admin"),children:e.jsx(Ke,{className:"h-4 w-4"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold",children:"Embedding Generation"}),e.jsx("p",{className:"text-muted-foreground",children:"Generate and manage profile embeddings for semantic search"})]})]}),e.jsx("div",{className:"max-w-2xl space-y-6",children:e.jsx(ts,{})})]})}export{ps as default};
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.