@alwith/module-chat 0.1.8 → 0.1.9
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
|
-
import{jsx as
|
|
1
|
+
import{jsx as K,jsxs as lt}from"react/jsx-runtime";import{forwardRef as gt,useCallback as B,useEffect as ot,useImperativeHandle as xt,useLayoutEffect as P,useMemo as A,useRef as c,useState as H}from"react";import{calculateCodexThreadVirtualRange as S,calculateCodexTurnCenterDistanceFromBottom as ht,calculateCodexTurnTargetDistanceFromBottom as ft,createCodexThreadVirtualLayout as mt,preserveCodexThreadDistanceFromBottom as yt,preserveCodexThreadRenderedWindow as it}from"./thread-virtualizer.js";var st=12,V=2,L=800,at=new Map;function ut({turnKey:k,heightPx:x,observe:I,onReady:r,children:p}){let D=c(null),[g,T]=H(!1),b=B((e)=>{D.current=e,I(k,e)},[I,k]);return P(()=>{let e=D.current;if(e===null)throw Error("Codex turn element is missing");let N=()=>{r(e),T(!0)};if(e.querySelector("[data-markdown-pending]")===null){N();return}let d=new MutationObserver(()=>{if(e.querySelector("[data-markdown-pending]")!==null)return;d.disconnect(),N()});return d.observe(e,{childList:!0,subtree:!0}),()=>d.disconnect()},[r]),K("div",{style:g?void 0:{height:x,overflow:"hidden"},children:K("div",{ref:b,className:"[&_[data-virtualized-turn-content]]:[content-visibility:visible]","data-turn-key":k,children:p})})}var wt=gt(function({entries:x,renderTurn:I,scrollElement:r,sessionKey:p},D){let g=A(()=>at.get(p),[p]),[T,b]=H(()=>g?.turnHeightsByKey??{}),e=A(()=>mt({entries:x,gapPx:st,measuredHeightsByKey:T}),[x,T]),N=A(()=>{let t=S({distanceFromBottomPx:g?.distanceFromBottomPx??0,layout:e,overscanCount:V,viewportHeightPx:L}),n=g?.renderedWindow;if(n===void 0||n===null)return t;return it({anchorKey:n.anchorKey,layout:e,previousRange:{startIndex:0,endIndex:Math.min(n.count,t.endIndex-t.startIndex)}})??t},[g,e]),[d,E]=H(N),[h,G]=H(L),[W,U]=H(g?.distanceFromBottomPx??0),[u,X]=H(null),R=c(W),F=c(d),J=c(e.turnKeys),l=c(T),q=c(e),Q=c(e),C=c(new Map),f=c(null),w=c(null),Y=c(!1),Z=c(()=>{}),m=B((t,n)=>{let o=Math.min(t,e.totalHeightPx),s=S({distanceFromBottomPx:o,layout:e,overscanCount:V,viewportHeightPx:n}),a=F.current,i=a.startIndex<=s.startIndex&&a.endIndex>=s.endIndex?a:s;R.current=o,F.current=i,J.current=e.turnKeys,U(o),G(n),E(i)},[e]),O=B((t)=>{let n=Math.max(0,t.scrollHeight-e.totalHeightPx);return Math.max(0,t.scrollHeight-t.clientHeight-t.scrollTop-n)},[e.totalHeightPx]),tt=B((t)=>{let n=t.dataset.turnKey;if(n===void 0)throw Error("Codex measured turn is missing its key");let o=Math.max(1,t.getBoundingClientRect().height);if(l.current[n]===o)return;let s={...l.current,[n]:o};l.current=s,b(s)},[]),j=B(()=>{if(w.current!==null)return w.current;return w.current=new ResizeObserver((t)=>{let n={...l.current},o=!1;for(let s of t){let a=s.target;if(a.querySelector("[data-markdown-pending]")!==null)continue;let i=a.dataset.turnKey;if(i===void 0)throw Error("Codex measured turn is missing its key");let v=Math.max(1,s.borderBoxSize[0]?.blockSize??s.contentRect.height);if(n[i]===v)continue;n[i]=v,o=!0}if(!o)return;l.current=n,b(n)}),w.current},[]),et=B((t,n)=>{let o=C.current.get(t);if(o!==void 0)j().unobserve(o),C.current.delete(t);if(n===null)return;if(n.dataset.turnKey!==t)throw Error(`Codex turn key does not match its element: ${t}`);C.current.set(t,n),j().observe(n)},[j]);ot(()=>()=>{w.current?.disconnect(),w.current=null,C.current.clear()},[]),P(()=>{Z.current=()=>{if(r===null)return;let t=O(r);if(f.current!==null){R.current=t,U(t),G(r.clientHeight||L);return}m(t,r.clientHeight||L)}},[O,r,m]),P(()=>{if(r===null)return;if(!Y.current){Y.current=!0;let o=g?.distanceFromBottomPx??0;r.scrollTo({behavior:"instant",top:Math.max(0,o>0?q.current.totalHeightPx-r.clientHeight-o:r.scrollHeight-r.clientHeight)})}let t=()=>Z.current();t(),r.addEventListener("scroll",t,{passive:!0});let n=new ResizeObserver(t);return n.observe(r),()=>{r.removeEventListener("scroll",t),n.disconnect()}},[g,r]),P(()=>{if(r===null||u===null)return;let t=C.current.get(u.turnKey);if(t===void 0||t.querySelector("[data-markdown-pending]")!==null)return;let n=t.offsetHeight;if(n<=0)return;if(l.current[u.turnKey]!==n){let v={...l.current,[u.turnKey]:n};l.current=v,b(v);return}let o=u.getTargetElement(t)??t,s=t.getBoundingClientRect(),a=o.getBoundingClientRect(),i=ft({align:u.align,layout:e,targetHeightPx:a.height,targetOffsetPx:a.top-s.top,turnKey:u.turnKey,viewportHeightPx:r.clientHeight});if(i===null)throw Error(`Codex virtual turn is missing: ${u.turnKey}`);r.scrollTo({behavior:"instant",top:Math.max(0,e.totalHeightPx-r.clientHeight-i)}),m(i,r.clientHeight||L),queueMicrotask(()=>{if(f.current!==u)return;u.complete(),f.current=null,X(null)})},[e,u,r,m]),P(()=>{let t=Q.current;if(Q.current=e,q.current=e,r===null||u!==null)return;let n=R.current;if(n>24&&t!==e){let o=O(r);if(o<=96){m(o,h);return}let s=S({distanceFromBottomPx:n,layout:t,overscanCount:0,viewportHeightPx:h}),a=t.turnKeys.slice(s.startIndex,s.endIndex).find((i)=>l.current[i]!==void 0&&e.turnIndexByKey.has(i));if(a!==void 0){let i=yt({anchorKey:a,distanceFromBottomPx:n,nextLayout:e,previousLayout:t});if(i===null)throw Error(`Codex viewport anchor is missing: ${a}`);n=i,r.scrollTo({behavior:"instant",top:Math.max(0,e.totalHeightPx-r.clientHeight-n)})}}m(n,h)},[e,u,O,r,m,h]),P(()=>{F.current=d},[d]),P(()=>{R.current=W},[W]),ot(()=>()=>{f.current?.complete(),f.current=null;let t=F.current,o=q.current.turnKeys[t.startIndex];at.set(p,{distanceFromBottomPx:R.current,renderedWindow:o===void 0?null:{anchorKey:o,count:t.endIndex-t.startIndex},turnHeightsByKey:l.current})},[p]),xt(D,()=>({scrollToKey(t,n=(s)=>s,o={align:"center"}){if(f.current?.complete(),e.turnIndexByKey.get(t)===void 0)throw Error(`Codex virtual turn is missing: ${t}`);let a=ht({layout:e,turnKey:t,viewportHeightPx:h});if(a===null)throw Error(`Codex virtual turn is missing: ${t}`);let i=S({distanceFromBottomPx:a,layout:e,overscanCount:V,viewportHeightPx:h});return F.current=i,E(i),new Promise((v)=>{let rt={align:o.align,complete:v,getTargetElement:n,turnKey:t};f.current=rt,X(rt)})}}),[e,h]);let y=d,_=J.current;if(!(_===e.turnKeys||_.length===e.turnKeys.length&&_.every((t,n)=>t===e.turnKeys[n]))){let t=_[d.startIndex];y=(t===void 0?null:it({anchorKey:t,layout:e,previousRange:d}))??S({distanceFromBottomPx:R.current,layout:e,overscanCount:V,viewportHeightPx:h})}let M=x.length-1,z=x[M],ct=e.topOffsetsPx[M]??0,nt=x.slice(y.startIndex,Math.min(y.endIndex,M)),dt=e.topOffsetsPx[y.startIndex]??0;return lt("div",{"data-codex-thread":!0,"data-codex-virtualized-pending-key":u?.turnKey,"data-codex-virtualized-range":`${y.startIndex}:${y.endIndex}`,"data-codex-virtualized-total-count":x.length,"data-codex-virtualized-turn-count":nt.length+(z===void 0?0:1),className:"relative shrink-0",style:{height:e.totalHeightPx},children:[K("div",{className:"flex flex-col",style:{gap:st,marginTop:dt},children:nt.map((t,n)=>{let o=y.startIndex+n;return K(ut,{turnKey:t.turnKey,heightPx:e.heightsPx[o],observe:et,onReady:tt,children:I(t,o)},t.turnKey)})}),z!==void 0&&K("div",{className:"absolute inset-x-0",style:{top:ct},children:K(ut,{turnKey:z.turnKey,heightPx:e.heightsPx[M],observe:et,onReady:tt,children:I(z,M)},z.turnKey)})]})});export{wt as VirtualizedTurnList};
|