@alwith/module-chat 0.1.11 → 0.1.12

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