Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
import{D as e,E as t,N as n,O as r,S as i,a,j as o,k as s}from"./served-from-8DzM0VAM.js";import{t as c}from"./local-source-emarjVnA.js";import{$ as l}from"./parse-sse-frame-DXl4zDFf.js";import{Xn as u,Zn as d,cr as f,dr as p,i as m,it as h,ot as g,pi as _,r as v,rt as y,st as b,tt as x}from"./index-Dknlc6p6.js";import{a as S,i as C,n as w,o as T,r as E,s as D,t as O}from"./review-stack-client-DzMErSoo.js";var ee={name:`bookmark`,size:24,node:[[`path`,{d:`M17 3a2 2 0 0 1 2 2v15a1 1 0 0 1-1.496.868l-4.512-2.578a2 2 0 0 0-1.984 0l-4.512 2.578A1 1 0 0 1 5 20V5a2 2 0 0 1 2-2z`,key:`oz39mx`}]]};ee.node;var te=r(ee),k={name:`expand`,size:24,node:[[`path`,{d:`m15 15 6 6`,key:`1s409w`}],[`path`,{d:`m15 9 6-6`,key:`ko1vev`}],[`path`,{d:`M21 16v5h-5`,key:`1ck2sf`}],[`path`,{d:`M21 8V3h-5`,key:`1qoq8a`}],[`path`,{d:`M3 16v5h5`,key:`1t08am`}],[`path`,{d:`m3 21 6-6`,key:`wwnumi`}],[`path`,{d:`M3 8V3h5`,key:`1ln10m`}],[`path`,{d:`M9 9 3 3`,key:`v551iv`}]]};k.node;var A=r(k),j={name:`message-square`,size:24,node:[[`path`,{d:`M22 17a2 2 0 0 1-2 2H6.828a2 2 0 0 0-1.414.586l-2.202 2.202A.71.71 0 0 1 2 21.286V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2z`,key:`18887p`}]]};j.node;var ne=r(j),M={name:`signpost`,size:24,node:[[`path`,{d:`M12 13v8`,key:`1l5pq0`}],[`path`,{d:`M12 3v3`,key:`1n5kay`}],[`path`,{d:`M2.354 10.354a1.207 1.207 0 0 1 0-1.708l2.06-2.06A2 2 0 0 1 5.828 6h12.344a2 2 0 0 1 1.414.586l2.06 2.06a1.207 1.207 0 0 1 0 1.708l-2.06 2.06a2 2 0 0 1-1.414.586H5.828a2 2 0 0 1-1.414-.586z`,key:`1tm261`}]]};M.node;var N=r(M),P={name:`skip-forward`,size:24,node:[[`path`,{d:`M21 4v16`,key:`7j8fe9`}],[`path`,{d:`M6.029 4.285A2 2 0 0 0 3 6v12a2 2 0 0 0 3.029 1.715l9.997-5.998a2 2 0 0 0 .003-3.432z`,key:`zs4d6`}]]};P.node;var re=r(P),F={name:`upload`,size:24,node:[[`path`,{d:`M12 3v12`,key:`1x0j5s`}],[`path`,{d:`m17 8-5-5-5 5`,key:`7q97r8`}],[`path`,{d:`M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4`,key:`ih7n3h`}]]};F.node;var ie=r(F),I=n(o(),1),ae=[`browse`,`work`],oe={browse:`Browse`,work:`Work`},L=[`seen`,`skip`],se={seen:`Seen`,skip:`Skipped`};[...L];var R=e=>e.normalize(`NFKC`).toLowerCase(),z=3e3,B=5;function ce(e,t){return t>e&&Math.floor(t/B)>Math.floor(e/B)}var V=e=>e.focusId??R(e.focus);function le(e){let t=new Map(e.map(e=>[V(e),e.focus])),n=new Map;for(let[e,r]of t){let t=R(r),i=n.get(t);i?i.push(e):n.set(t,[e])}for(let e of n.values())e.length>1&&e.sort().forEach((e,n)=>t.set(e,t.get(e)+` (`+(n+1)+`)`));return t}function ue(e,t,n){let r=R(t),i=e.cards.filter(e=>!e.historical&&(!t||V(e)===r)),a=new Map(e.reviews.map(e=>[e.cardId,e])),o=new Map(e.bookmarks.map(e=>[e.cardId,e])),s=[],c=[],l=0;for(let e of i){let t=a.get(e.id);t?t.action===`seen`?l++:(c.push(t.id),n.has(t.id)&&s.push(e)):s.push(e)}return{cards:i,pending:s,skipped:c,seen:l,reviews:a,bookmarks:o}}function H(e,t,n){return Math.abs(e)>Math.abs(t)*1.15&&Math.abs(e)>=Math.max(44,Math.min(64,n*.12))}var U=s(),W=8388608;function de(e,t){let n=e.preview??e.imageData,r=e.localPreview===e.localSource?w(e.localPreview):void 0,i=m(n),a=C(e.id,n)??(n?void 0:r),[o,s]=(0,I.useState)();if((0,I.useEffect)(()=>{if(!a)return;let e=new AbortController,t;return fetch(a,{signal:e.signal,cache:`no-store`,credentials:`same-origin`,redirect:`error`}).then(async n=>{if(!n.ok)throw Error(`Preview unavailable`);if(a===r){let t=Number(n.headers.get(`Content-Length`));if(!Number.isSafeInteger(t)||t<1||t>W){n.body?.cancel().catch(()=>{}),e.signal.aborted||s({path:a});return}}if(!/^image\/(?:png|jpeg|webp|gif)$/i.test(n.headers.get(`Content-Type`)?.split(`;`)[0]??``))throw Error(`Preview unavailable`);let i=await n.blob();e.signal.aborted||(t=URL.createObjectURL(i),s({path:a,url:t}))}).catch(()=>{e.signal.aborted||s({path:a,failed:!0})}),()=>{e.abort(),t&&URL.revokeObjectURL(t)}},[a,r,t]),a)return o?.path===a?o:{url:void 0,failed:!1};let c=n&&/^data:image\/(?:png|jpeg|webp);base64,/i.test(n);return{url:i?v.thumbnailUrl(i):c?n:E(n),failed:!1}}function G({card:e,onInteraction:t}){let{t:n}=i(),[r,o]=(0,I.useState)(!1);(0,I.useLayoutEffect)(()=>{if(r)return t(!0),()=>t(!1)},[r,t]);let[s,c]=(0,I.useState)(0),[u,d]=(0,I.useState)(!1),f=de(e,s),m=w(e.localSource),h=m?_(m):E(e.source),v=m&&h!==m;return(0,U.jsxs)(U.Fragment,{children:[f.url&&!u&&(0,U.jsxs)(a,{variant:`bare`,type:`button`,className:`stack-preview`,"aria-label":l(n(`Enlarge {title}`),{title:e.title}),onClick:()=>o(!0),children:[(0,U.jsx)(`img`,{src:f.url,alt:e.title,decoding:`async`,draggable:!1,referrerPolicy:`no-referrer`,onError:()=>d(!0)}),(0,U.jsx)(A,{"aria-hidden":`true`,className:`stack-expand`})]}),(f.failed||u)&&(0,U.jsxs)(`p`,{className:`flex items-center gap-2 text-sm`,children:[n(`Preview unavailable.`),` `,(0,U.jsx)(a,{size:`sm`,variant:`outline`,onClick:()=>{d(!1),c(e=>e+1)},children:n(`Retry preview`)})]}),e.excerpt&&(0,U.jsx)(`blockquote`,{className:`stack-excerpt`,dir:`auto`,children:e.excerpt}),!h&&e.key.startsWith(`auto-file:`)&&(0,U.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:n(`The original opens on the computer that created it.`)}),(0,U.jsxs)(`div`,{className:`flex flex-wrap gap-x-4`,children:[h&&(0,U.jsxs)(`a`,{className:`stack-source`,href:h,target:`_blank`,rel:`noreferrer`,children:[n(v?`Download original`:`Open original`),(0,U.jsx)(p,{size:16,"aria-hidden":`true`})]}),e.requestId&&(0,U.jsx)(`a`,{className:`stack-source`,href:`?view=requests&request=${encodeURIComponent(e.requestId)}`,children:n(`Open request`)})]}),r&&f.url&&(0,U.jsx)(x,{open:!0,onOpenChange:e=>{e||o(!1)},children:(0,U.jsxs)(y,{className:`max-h-[92svh] overflow-auto sm:max-w-4xl`,"aria-describedby":void 0,children:[(0,U.jsx)(g,{children:(0,U.jsx)(b,{children:e.title})}),(0,U.jsx)(`img`,{className:`mx-auto max-h-[72svh] max-w-full object-contain`,alt:e.title,src:f.url,referrerPolicy:`no-referrer`})]})})]})}function fe({card:e,mode:t,disabled:n,onSeen:r,onGroup:a,onBrowse:o,onInteraction:s,children:c}){let{t:u}=i(),d=(0,I.useRef)(null),f=(0,I.useRef)(void 0),p=(0,I.useRef)(!1),[m,h]=(0,I.useState)(!1),[g,_]=(0,I.useState)(0),v=(0,I.useRef)({onSeen:r,onGroup:a,onBrowse:o});(0,I.useLayoutEffect)(()=>{v.current={onSeen:r,onGroup:a,onBrowse:o}},[r,a,o]);function y(){clearTimeout(f.current),f.current=void 0,h(!1)}function b(){y(),_(0),d.current&&(d.current=null,s(!1))}function x(){d.current&&(p.current=!0),b()}function S(){f.current===void 0&&(h(!0),f.current=setTimeout(()=>{d.current&&(x(),document.hidden||v.current.onGroup())},z))}(0,I.useLayoutEffect)(()=>{x()},[e.id,n,t]),(0,I.useEffect)(()=>{let e=()=>{document.hidden&&x()},t=e=>{e.key===`Escape`&&x()},n=e=>{d.current&&e.pointerId!==d.current.id&&x()};return window.addEventListener(`blur`,x),document.addEventListener(`visibilitychange`,e),document.addEventListener(`keydown`,t),document.addEventListener(`pointerdown`,n,!0),()=>{clearTimeout(f.current),d.current&&s(!1),window.removeEventListener(`blur`,x),document.removeEventListener(`visibilitychange`,e),document.removeEventListener(`keydown`,t),document.removeEventListener(`pointerdown`,n,!0)}},[]);function C(e){if(n||!e.isPrimary||e.button!==0)return;p.current=!1;let t=e.target,r=!!t.closest(`[data-review-seen]`);(r||!t.closest(`a,input,textarea,select,button:not(.stack-preview)`))&&(d.current={id:e.pointerId,startX:e.clientX,startY:e.clientY,startedAt:e.timeStamp,width:e.currentTarget.clientWidth,button:r},s(!0),r&&(t.setPointerCapture(e.pointerId),S()))}function w(e){let n=d.current;if(n?.id!==e.pointerId)return;let r=e.clientX-n.startX,i=e.clientY-n.startY;n.button?Math.hypot(r,i)>15&&x():(Math.abs(r)>10&&Math.abs(r)>Math.abs(i)*1.15&&(e.target.hasPointerCapture(e.pointerId)||e.currentTarget.setPointerCapture(e.pointerId),_(Math.max(-44,Math.min(44,r*.3)))),t===`work`&&r>0&&H(r,i,n.width)?S():f.current&&y())}function T(e){let n=d.current;if(n?.id!==e.pointerId)return;let r=e.clientX-n.startX,i=e.clientY-n.startY;b(),n.button?Math.hypot(r,i)>15&&(p.current=!0):(Math.abs(r)>=26&&Math.abs(r)>Math.abs(i)*1.15&&Math.abs(r)/Math.max(1,e.timeStamp-n.startedAt)>=.5||H(r,i,n.width))&&(p.current=!0,t===`browse`?v.current.onBrowse(r<0?1:-1):r>0&&v.current.onSeen())}function E(e){if(!(n||e.metaKey||e.altKey||e.ctrlKey)){if(e.target.closest(`[data-review-seen]`)&&(e.key===`Enter`||e.key===` `)){if(e.preventDefault(),e.repeat||d.current)return;p.current=!1,d.current={id:`keyboard`,startX:0,startY:0,startedAt:e.timeStamp,width:0,button:!0},s(!0),S()}else e.target===e.currentTarget&&!e.repeat&&(t===`browse`&&[`ArrowLeft`,`ArrowRight`].includes(e.key)&&(e.preventDefault(),o(e.key===`ArrowRight`?1:-1)),t===`work`&&e.key===`ArrowRight`&&(e.preventDefault(),r()))}}return(0,U.jsxs)(`article`,{className:`stack-card`,tabIndex:0,"aria-label":e.title,style:g?{transform:`translateX(${g}px)`}:void 0,onPointerDown:C,onPointerMove:w,onPointerUp:T,onPointerCancel:e=>{d.current?.id===e.pointerId&&x()},onLostPointerCapture:e=>{d.current?.id===e.pointerId&&!e.currentTarget.hasPointerCapture(e.pointerId)&&x()},onPointerLeave:e=>{d.current?.id===e.pointerId&&!d.current.button&&!e.currentTarget.hasPointerCapture(e.pointerId)&&x()},onClickCapture:e=>{p.current&&(p.current=!1,e.preventDefault(),e.stopPropagation())},onKeyDown:E,onKeyUp:e=>{d.current?.id===`keyboard`&&(e.key===`Enter`||e.key===` `)&&(e.preventDefault(),b(),r())},onBlur:()=>{d.current?.id===`keyboard`&&x()},children:[(0,U.jsx)(G,{card:e,onInteraction:s},e.id),(0,U.jsxs)(`div`,{className:`stack-copy`,children:[(0,U.jsx)(`span`,{className:`text-sm font-medium text-muted-foreground`,dir:`auto`,children:e.focus}),(0,U.jsx)(`h2`,{className:`text-xl font-semibold leading-tight`,dir:`auto`,children:e.title}),e.category===`decision`?(0,U.jsxs)(`div`,{className:`stack-decision`,children:[(0,U.jsxs)(`span`,{className:`flex items-center gap-2 text-sm font-semibold`,children:[(0,U.jsx)(N,{"aria-hidden":`true`,size:17}),u(`Your move`)]}),(0,U.jsx)(`p`,{dir:`auto`,children:e.what})]}):(0,U.jsx)(`p`,{dir:`auto`,children:e.what}),e.context&&(0,U.jsx)(`p`,{className:`text-sm text-muted-foreground`,dir:`auto`,children:e.context})]}),m&&(0,U.jsxs)(`p`,{className:`stack-hold`,role:`status`,children:[l(u(`Hold to mark {focus} seen`),{focus:e.focus}),(0,U.jsx)(`i`,{"aria-hidden":`true`})]}),c]})}function pe({snapshot:e,focus:t,disabled:n,onAction:r,onNote:o,onOpen:s}){let{t:c}=i(),[u,d]=(0,I.useState)(!1),[f,p]=(0,I.useState)(32),m=(0,I.useMemo)(()=>new Map(e.reviews.map(e=>[e.cardId,e])),[e.reviews]),h=(0,I.useMemo)(()=>new Map(e.bookmarks.map(e=>[e.cardId,e])),[e.bookmarks]),g=(0,I.useMemo)(()=>{let t=new Map;for(let n of e.followUps){let e=t.get(n.cardId)??[];e.push(n),t.set(n.cardId,e)}return t},[e.followUps]),_=e=>!t||V(e)===t,v=e.cards.filter(e=>_(e)&&(u?h.get(e.id)?.saved:m.has(e.id)||h.get(e.id)?.saved||g.has(e.id))),y=e.groups.filter(e=>_(e)&&e.activeCount>0);return(0,U.jsxs)(`section`,{className:`space-y-3 rounded-xl border bg-card p-4`,"aria-label":c(`Review history`),children:[(0,U.jsxs)(`div`,{className:`flex flex-wrap items-center justify-between gap-2`,children:[(0,U.jsx)(`h2`,{className:`text-lg font-semibold`,children:c(`Review history`)}),(0,U.jsxs)(`label`,{className:`flex min-h-11 items-center gap-2 text-sm`,children:[(0,U.jsx)(`input`,{type:`checkbox`,checked:u,onChange:e=>d(e.target.checked)}),c(`Saved only`)]})]}),!u&&y.map(e=>(0,U.jsxs)(`div`,{className:`flex flex-wrap items-center justify-between gap-2 rounded-lg border p-2`,children:[(0,U.jsx)(`span`,{children:l(c(`{focus} · {count} marked seen together`),{focus:e.focus,count:e.activeCount})}),(0,U.jsx)(a,{size:`sm`,variant:`outline`,disabled:n,onClick:()=>r({action:`undo-group`,groupId:e.id}),children:c(`Undo group`)})]},e.id)),!v.length&&(0,U.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:c(`No reviews here yet.`)}),(0,U.jsx)(`ul`,{className:`divide-y`,children:v.slice(0,f).map(e=>{let t=m.get(e.id),i=h.get(e.id);return(0,U.jsxs)(`li`,{className:`space-y-2 py-3`,children:[(0,U.jsxs)(`div`,{className:`flex flex-wrap items-baseline justify-between gap-2`,children:[e.historical?(0,U.jsx)(`span`,{className:`font-medium`,dir:`auto`,children:e.title}):(0,U.jsx)(a,{variant:`link`,className:`h-auto whitespace-normal p-0 text-left`,onClick:()=>s(e),children:e.title}),(0,U.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:[t&&c(se[t.action]),i?.saved&&c(`Saved`)].filter(Boolean).join(` · `)})]}),e.historical&&(0,U.jsxs)(`div`,{className:`space-y-1 text-sm`,children:[(0,U.jsx)(`p`,{children:c(e.what)}),e.requestId&&(0,U.jsx)(`a`,{className:`stack-source`,href:`?view=requests&request=${encodeURIComponent(e.requestId)}`,children:c(`Open request`)})]}),g.get(e.id)?.map(t=>(0,U.jsxs)(`div`,{className:`space-y-1 border-l-2 pl-3`,children:[(0,U.jsx)(`p`,{className:`whitespace-pre-wrap break-words`,dir:`auto`,children:t.body}),(0,U.jsx)(a,{size:`sm`,variant:`outline`,disabled:n,onClick:()=>r({action:`set-follow-up`,cardId:e.id,followUpId:t.id,closed:t.closedAt===null,revision:t.revision}),children:t.closedAt===null?c(`Complete follow-up`):c(`Reopen follow-up`)})]},t.id)),(0,U.jsxs)(`div`,{className:`flex flex-wrap gap-2`,children:[(0,U.jsx)(a,{variant:`outline`,size:`sm`,disabled:n,onClick:()=>r({action:`bookmark`,cardId:e.id,revision:i?.revision??0,saved:!i?.saved}),children:i?.saved?c(`Unsave`):c(`Save`)}),(0,U.jsx)(a,{variant:`outline`,size:`sm`,disabled:n,onClick:()=>o(e),children:c(`Follow up`)}),t&&(0,U.jsx)(a,{variant:`outline`,size:`sm`,disabled:n,onClick:()=>r({action:`undo`,cardId:e.id,reviewId:t.id}),children:e.historical?c(`Undo review`):c(`Return to deck`)})]})]},e.id)})}),v.length>f&&(0,U.jsx)(a,{variant:`outline`,onClick:()=>p(e=>e+32),children:c(`Show more`)})]})}async function K(e){if(/^image\/(png|jpeg|webp)$/i.test(e.type)&&e.size<=163840)return new Promise((t,n)=>{let r=new FileReader;r.onload=()=>t(String(r.result)),r.onerror=()=>n(Error(`Could not read ${e.name}.`)),r.readAsDataURL(e)});let t=await createImageBitmap(e).catch(()=>{throw Error(`Could not open the image ${e.name}.`)});try{let n=document.createElement(`canvas`),r=Math.min(1,960/Math.max(t.width,t.height));for(;;){n.width=Math.max(1,Math.round(t.width*r)),n.height=Math.max(1,Math.round(t.height*r));let i=n.getContext(`2d`);if(!i)throw Error(`Could not make a preview for ${e.name}.`);i.drawImage(t,0,0,n.width,n.height);let a=n.toDataURL(`image/webp`,.82);if(a.startsWith(`data:image/`)&&(a.length-a.indexOf(`,`)-1)*.75<=163840)return a;if(Math.max(n.width,n.height)<=32)throw Error(`Choose a smaller thumbnail for ${e.name}.`);r*=.7}}finally{t.close()}}async function q(e){let t=e.filter(e=>/\.json$/i.test(e.name));if(t.length!==1)throw Error(`Choose one cards.json file and the images it references.`);let n;try{n=JSON.parse(await t[0].text())}catch{throw Error(`This file could not be read as JSON.`)}if(!Array.isArray(n?.cards)||!n.cards.length)throw Error(`The manifest needs a cards array with at least one original.`);let r=new Set;return{cards:n.cards.map((t,n)=>{if(!t||typeof t!=`object`)throw Error(`Card ${n+1} could not be read.`);for(let e of[`key`,`focus`,`title`,`what`,`context`])if(typeof t[e]!=`string`||!t[e].trim())throw Error(`Card ${n+1} needs ${e}.`);let{key:i,focus:a,title:o,what:s,context:c}=t,l={key:i.trim(),focus:a,title:o,what:s,context:c};if(r.has(l.key))throw Error(`Duplicate card key: ${l.key}`);r.add(l.key);for(let e of[`source`,`preview`,`imageData`,`excerpt`,`imageFile`])if(t[e]!=null&&t[e]!==``){if(typeof t[e]!=`string`)throw Error(`Card ${n+1}: ${e} must be text.`);l[e]=t[e]}return l.imageFile&&J(l.imageFile,e),l}),files:e,offset:0,added:0,existing:0,batchKey:crypto.randomUUID()}}function J(e,t){if(typeof e!=`string`||/(^[\\/]|^[A-Za-z]:|(^|[\\/])\.\.([\\/]|$))/.test(e))throw Error(`Choose images alongside cards.json using relative imageFile names.`);let n=e.replace(/\\/g,`/`),r=n.split(`/`).at(-1),i=t.filter(e=>e.webkitRelativePath===n||e.webkitRelativePath?.endsWith(`/`+n)),a=i.length?i:t.filter(e=>e.name===r);if(a.length!==1)throw Error(`Select the image ${n} with cards.json. Each image name must identify one file.`);return a[0]}async function Y({imageFile:e,...t},n,r){return e&&(t.imageData=await r(J(e,n))),t}async function X(e,t,n,r=D){for(;e.offset<e.cards.length;){let i=e.cards.slice(e.offset,e.offset+12),a=[];for(let t of i)a.push(await Y(t,e.files,n));let o=await r(a,e.batchKey);e.added+=o.added,e.existing+=o.existing,e.offset+=i.length,e.batchKey=crypto.randomUUID(),t()}}function me({close:e,onImported:t}){let{t:n}=i(),[r,o]=(0,I.useState)([]),[s,c]=(0,I.useState)(``),[u,d]=(0,I.useState)(``),[f,p]=(0,I.useState)(!1),[m,_]=(0,I.useState)(!1),v=(0,I.useRef)(void 0),S=(0,I.useRef)(!1);async function C(){if(!S.current){S.current=!0,p(!0),d(``);try{let e=v.current??=await q(r),t=()=>c(l(n(`{processed} of {total} processed · {added} added · {existing} already in your stack`),{processed:e.offset,total:e.cards.length,added:e.added,existing:e.existing}));t(),await X(e,t,K),_(!0)}catch(e){d(e instanceof Error?e.message:n(`Import failed. Try again.`))}finally{v.current?.offset&&t(),S.current=!1,p(!1)}}}return(0,U.jsx)(x,{open:!0,onOpenChange:t=>{!t&&!f&&e()},children:(0,U.jsxs)(y,{showCloseButton:!f,className:`sm:max-w-md`,children:[(0,U.jsxs)(g,{children:[(0,U.jsx)(b,{children:n(`Import review cards`)}),(0,U.jsx)(h,{children:n(`Choose cards.json from your deck. Select any images it names alongside it.`)})]}),!m&&(0,U.jsxs)(`label`,{className:`space-y-1`,children:[(0,U.jsx)(`span`,{children:n(`Manifest and images`)}),(0,U.jsx)(`input`,{type:`file`,multiple:!0,disabled:f,accept:`.json,.png,.jpg,.jpeg,.webp,.gif`,className:`block w-full text-sm`,onChange:e=>{o(Array.from(e.target.files??[])),v.current=void 0,c(``),d(``)}})]}),s&&(0,U.jsx)(`p`,{role:`status`,children:s}),u&&(0,U.jsx)(`p`,{role:`alert`,className:`text-destructive`,children:u}),m?(0,U.jsx)(a,{onClick:e,children:n(`View cards`)}):(0,U.jsx)(a,{disabled:!r.length||f,onClick:()=>void C(),children:f?n(`Importing…`):u&&v.current?n(`Retry remaining cards`):n(`Import cards`)})]})})}var Z=e=>e instanceof Error?e.message:`Could not reach Review Stack.`;function he(e){let[t,n]=(0,I.useState)(null),[r,i]=(0,I.useState)(``),[a,o]=(0,I.useState)(!1),[s,l]=(0,I.useState)(!1),u=(0,I.useRef)(null),d=(0,I.useRef)(!1),f=(0,I.useRef)(!1),p=(0,I.useRef)(e),m=(0,I.useRef)(!1),h=(0,I.useRef)(()=>{}),g=(0,I.useRef)(new Map),_=(0,I.useCallback)(async(e=!1,t=!1)=>{u.current?.abort();let r=new AbortController;u.current=r;let a=setTimeout(()=>r.abort(Error(`Loading Review Stack timed out.`)),45e3);try{let e=await S(r.signal);return d.current&&u.current===r&&(t&&(p.current||document.hidden)?m.current=!0:(n(t=>t?.revision===e.revision?t:e),l(!1),i(``))),e}catch(t){return d.current&&u.current===r&&(l(!0),i((e?`Saved. Reload to see the latest cards. `:``)+Z(t))),null}finally{clearTimeout(a),u.current===r&&(u.current=null,h.current())}},[]);(0,I.useLayoutEffect)(()=>{p.current=e,e||h.current()},[e]),(0,I.useEffect)(()=>{d.current=!0,h.current=()=>{!m.current||!d.current||u.current||f.current||p.current||document.hidden||(m.current=!1,_(!1,!0))};let e=()=>{m.current=!0,h.current()},t=()=>{document.hidden||e()},n=c.subscribeToReviewStackChanges?.(e);return e(),window.addEventListener(`focus`,t),window.addEventListener(`online`,t),document.addEventListener(`visibilitychange`,t),()=>{d.current=!1,n?.(),h.current=()=>{},u.current?.abort(),u.current=null,window.removeEventListener(`focus`,t),window.removeEventListener(`online`,t),document.removeEventListener(`visibilitychange`,t)}},[_]);async function v(e){if(f.current||s)return{saved:!1,next:null};f.current=!0,o(!0),i(``),u.current?.abort(),u.current=null;let t=JSON.stringify(e),n=g.current.get(t)??crypto.randomUUID();g.current.set(t,n);try{return await T(e,n),g.current.delete(t),m.current=!1,{saved:!0,next:d.current?await _(!0):null,eventId:n}}catch(e){return e instanceof O&&e.status>=400&&e.status<500&&(g.current.delete(t),d.current&&e.status===409&&await _()),d.current&&i(Z(e)),{saved:!1,next:null}}finally{f.current=!1,d.current&&(o(!1),h.current())}}return{snapshot:t,error:r,saving:a,stale:s,refresh:_,write:v}}function Q(){let{t:n}=i(),[r,o]=(0,I.useState)(`work`),[s,c]=(0,I.useState)(``),[p,m]=(0,I.useState)(``),[h,_]=(0,I.useState)(new Set),[v,S]=(0,I.useState)(!1),C=(0,I.useRef)(void 0),[w,T]=(0,I.useState)(``),[E,D]=(0,I.useState)(),[O,ee]=(0,I.useState)(0),[k,A]=(0,I.useState)(!1),[j,M]=(0,I.useState)(!1),[N,P]=(0,I.useState)(null),[F,L]=(0,I.useState)(``),[se,R]=(0,I.useState)(!1),{snapshot:z,saving:B,stale:V,error:H,refresh:W,write:de}=he(se||j||!!N);if((0,I.useEffect)(()=>()=>clearTimeout(C.current),[]),!z)return(0,U.jsxs)(`section`,{className:`rounded-xl border bg-card p-5`,"aria-busy":!H,children:[(0,U.jsx)(`p`,{role:H?`alert`:`status`,children:H||n(`Loading review cards…`)}),H&&(0,U.jsx)(a,{className:`mt-3`,variant:`outline`,onClick:()=>void W(),children:n(`Try again`)})]});let G=le(z.cards),K=G.has(s)?s:``,q=ue(z,K,h),J=Math.max(0,q.cards.findIndex(e=>e.id===p)),Y=r===`work`?q.pending[0]:q.cards[J],X=Y&&q.bookmarks.get(Y.id),Z=z.reviews.filter(e=>e.action===`seen`).length,Q=B||V;function ge(e){e!==r&&(e===`browse`&&Y&&m(Y.id),o(e),S(!0),T(``),clearTimeout(C.current),C.current=setTimeout(()=>S(!1),2100))}function _e(e){c(e),m(``),_(new Set),D(void 0),T(``)}function ve(e){let t=q.cards[Math.max(0,Math.min(q.cards.length-1,J+e))];t&&m(t.id)}function ye(e){P(e),L(``)}async function $(e){T(``);let t=await de(e);if(!t.saved)return!1;D(void 0);let r=t.next;if(e.action===`seen`||e.action===`skip`)e.expectedReviewId&&_(t=>{let n=new Set(t);return n.delete(e.expectedReviewId),n}),T(e.action===`seen`?n(`Marked as seen.`):n(`Skipped for this pass.`));else if(e.action===`seen-group`){let e=r?.groups.find(e=>e.id===t.eventId&&e.activeCount>0);e&&(D(e.id),T(l(n(`{count} marked seen in {focus}.`),{count:e.count,focus:e.focus})))}else e.action===`bookmark`?T(e.saved?n(`Saved to review history.`):n(`Bookmark removed.`)):e.action===`follow-up`?T(n(`Follow-up saved.`)):e.action===`undo`||e.action===`undo-group`?T(n(`Review undone.`)):e.action===`set-follow-up`&&T(e.closed?n(`Follow-up completed.`):n(`Follow-up reopened.`));if(r&&(e.action===`seen`||e.action===`seen-group`)){let t=r.reviews.filter(e=>e.action===`seen`).length;t>Z&&(e.action===`seen-group`||ce(Z,t))&&ee(e=>e+1)}return!0}function be(e){Y&&$({action:e,cardId:Y.id,expectedReviewId:q.reviews.get(Y.id)?.id??null})}function xe(){Y&&$({action:`seen-group`,focus:Y.focus,...Y.focusId&&{cardId:Y.id}})}function Se(){Y&&$({action:`bookmark`,cardId:Y.id,saved:!X?.saved,revision:X?.revision??0})}return(0,U.jsxs)(`section`,{className:`review-stack space-y-4`,"aria-label":n(`Review Stack`),children:[(0,U.jsxs)(`div`,{className:`stack-toolbar`,children:[(0,U.jsx)(`div`,{className:`stack-modes`,role:`group`,"aria-label":n(`Review mode`),children:ae.map(e=>(0,U.jsx)(a,{variant:`bare`,type:`button`,"aria-pressed":r===e,disabled:B,onClick:()=>ge(e),children:n(oe[e])},e))}),(0,U.jsxs)(`div`,{className:`flex flex-wrap gap-2`,children:[(0,U.jsxs)(a,{size:`sm`,variant:`outline`,"aria-expanded":k,onClick:()=>A(e=>!e),children:[(0,U.jsx)(u,{"aria-hidden":`true`}),n(`History`)]}),(0,U.jsxs)(a,{size:`sm`,variant:`outline`,disabled:Q,onClick:()=>M(!0),children:[(0,U.jsx)(ie,{"aria-hidden":`true`}),n(`Import`)]}),(0,U.jsx)(a,{size:`icon-sm`,variant:`outline`,disabled:B,"aria-label":n(`Refresh review cards`),title:n(`Refresh review cards`),onClick:()=>void W(),children:(0,U.jsx)(d,{"aria-hidden":`true`})})]})]}),v&&(0,U.jsx)(`p`,{className:`stack-guide`,role:`status`,children:n(r===`browse`?`Swipe left for the next card, right for the previous card.`:`Swipe right for Seen. Hold for three seconds to mark the whole focus area seen.`)}),H&&(0,U.jsxs)(`div`,{className:`flex flex-wrap items-center gap-2 rounded-lg border p-3`,children:[(0,U.jsx)(`p`,{role:`alert`,children:H}),(0,U.jsx)(a,{size:`sm`,variant:`outline`,onClick:()=>void W(),children:n(`Reload cards`)})]}),z.cards.length>0&&(0,U.jsxs)(U.Fragment,{children:[(0,U.jsxs)(`div`,{className:`stack-toolbar`,children:[(0,U.jsxs)(`label`,{className:`flex min-w-0 flex-1 flex-col gap-1 text-xs font-medium text-muted-foreground`,children:[n(`Focus area`),(0,U.jsxs)(`select`,{className:`stack-select`,value:K,disabled:B,onChange:e=>_e(e.target.value),children:[(0,U.jsx)(`option`,{value:``,children:n(`All focus areas`)}),Array.from(G,([e,t])=>(0,U.jsx)(`option`,{value:e,children:t},e))]})]}),(0,U.jsxs)(`div`,{className:`text-right text-sm`,children:[(0,U.jsx)(`p`,{children:l(n(`{seen} of {total} seen`),{seen:q.seen,total:q.cards.length})}),(0,U.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:l(n(`{points} XP`),{points:Z*10})})]})]}),(0,U.jsx)(`progress`,{className:`stack-progress`,max:q.cards.length||1,value:q.seen,"aria-label":n(`Cards seen in this focus area`)})]}),(0,U.jsxs)(`div`,{className:`stack-feedback`,role:`status`,"aria-live":`polite`,children:[w,E&&(0,U.jsx)(a,{size:`sm`,variant:`outline`,className:`ml-2`,disabled:Q,onClick:()=>void $({action:`undo-group`,groupId:E}),children:n(`Undo group`)}),O>0&&(0,U.jsx)(`span`,{className:`stack-confetti`,"aria-hidden":`true`,children:Array.from({length:12},(e,t)=>(0,U.jsx)(`i`,{},t))},O)]}),Y?(0,U.jsxs)(U.Fragment,{children:[r===`browse`&&(0,U.jsxs)(`div`,{className:`stack-toolbar`,children:[(0,U.jsxs)(a,{size:`sm`,variant:`outline`,disabled:!J,onClick:()=>ve(-1),children:[(0,U.jsx)(f,{"aria-hidden":`true`}),n(`Previous`)]}),(0,U.jsx)(`span`,{className:`text-sm`,children:l(n(`{position} of {total}`),{position:J+1,total:q.cards.length})}),(0,U.jsxs)(a,{size:`sm`,variant:`outline`,disabled:J>=q.cards.length-1,onClick:()=>ve(1),children:[n(`Next`),(0,U.jsx)(t,{"aria-hidden":`true`})]})]}),(0,U.jsx)(fe,{card:Y,mode:r,disabled:Q||j||!!N,onInteraction:R,onSeen:()=>be(`seen`),onGroup:xe,onBrowse:ve,children:r===`work`&&(0,U.jsxs)(`div`,{className:`stack-actions`,children:[(0,U.jsxs)(a,{"data-review-seen":!0,disabled:Q,"aria-describedby":`review-stack-hold-hint`,onClick:()=>be(`seen`),children:[(0,U.jsx)(e,{"aria-hidden":`true`}),n(`Seen`)]}),(0,U.jsxs)(a,{variant:`outline`,disabled:Q,"aria-pressed":!!X?.saved,onClick:Se,children:[(0,U.jsx)(te,{"aria-hidden":`true`,fill:X?.saved?`currentColor`:`none`}),X?.saved?n(`Saved`):n(`Save`)]}),(0,U.jsxs)(a,{variant:`outline`,disabled:Q,onClick:()=>ye(Y),children:[(0,U.jsx)(ne,{"aria-hidden":`true`}),n(`Follow up`)]}),(0,U.jsxs)(a,{variant:`ghost`,disabled:Q,onClick:()=>be(`skip`),children:[(0,U.jsx)(re,{"aria-hidden":`true`}),n(`Skip for now`)]}),(0,U.jsx)(`p`,{id:`review-stack-hold-hint`,className:`col-span-full text-center text-xs text-muted-foreground`,children:l(n(`Swipe right for Seen. Hold for 3s to mark {focus} seen.`),{focus:Y.focus})})]})})]}):(0,U.jsxs)(`div`,{className:`stack-empty`,children:[(0,U.jsx)(`h2`,{className:`text-lg font-semibold`,children:z.cards.length?n(`You’ve reached the end of this stack.`):n(`Your Review Stack is empty.`)}),q.cards.length>0&&(0,U.jsxs)(`div`,{className:`flex flex-wrap justify-center gap-2`,children:[q.skipped.length>0&&(0,U.jsx)(a,{variant:`outline`,onClick:()=>_(new Set(q.skipped)),children:l(n(`Revisit {count} skipped`),{count:q.skipped.length})}),(0,U.jsx)(a,{variant:`outline`,onClick:()=>ge(`browse`),children:n(`Browse all cards`)})]}),!z.cards.length&&(0,U.jsxs)(U.Fragment,{children:[(0,U.jsx)(`p`,{children:n(`Deliverables and decisions that need your attention appear here automatically.`)}),(0,U.jsx)(a,{onClick:()=>M(!0),children:n(`Import cards`)})]})]}),k&&(0,U.jsx)(pe,{snapshot:z,focus:K,disabled:Q,onAction:e=>void $(e),onNote:ye,onOpen:e=>{ge(`browse`),m(e.id),A(!1)}}),j&&(0,U.jsx)(me,{close:()=>M(!1),onImported:()=>void W()}),N&&(0,U.jsx)(x,{open:!0,onOpenChange:e=>{!e&&!B&&P(null)},children:(0,U.jsxs)(y,{hasUnsavedText:!!F.trim(),showCloseButton:!B,"aria-describedby":void 0,children:[(0,U.jsx)(g,{children:(0,U.jsx)(b,{children:n(`Follow up`)})}),(0,U.jsx)(`p`,{className:`font-medium`,dir:`auto`,children:N.title}),(0,U.jsxs)(`form`,{className:`space-y-3`,onSubmit:async e=>{e.preventDefault();let t=F.trim();t&&await $({action:`follow-up`,cardId:N.id,body:t})&&P(null)},children:[(0,U.jsxs)(`label`,{className:`block space-y-1`,children:[(0,U.jsx)(`span`,{children:n(`What should happen next?`)}),(0,U.jsx)(`textarea`,{autoFocus:!0,required:!0,maxLength:4e3,disabled:B,value:F,onChange:e=>L(e.target.value),rows:4,className:`block w-full rounded-lg border bg-background p-3`})]}),H&&(0,U.jsx)(`p`,{role:`alert`,className:`text-destructive`,children:H}),(0,U.jsx)(a,{disabled:Q||!F.trim(),type:`submit`,children:n(`Save follow-up`)})]})]})})]})}export{Q as default};