@assistant-ui/react 0.12.28 → 0.14.0
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.
- package/dist/client/ExternalThread.d.ts.map +1 -1
- package/dist/client/ExternalThread.js +0 -2
- package/dist/client/ExternalThread.js.map +1 -1
- package/dist/client/InMemoryThreadList.d.ts.map +1 -1
- package/dist/client/InMemoryThreadList.js +3 -0
- package/dist/client/InMemoryThreadList.js.map +1 -1
- package/dist/client/SingleThreadList.d.ts.map +1 -1
- package/dist/client/SingleThreadList.js +3 -0
- package/dist/client/SingleThreadList.js.map +1 -1
- package/dist/context/providers/ThreadViewportProvider.d.ts.map +1 -1
- package/dist/context/providers/ThreadViewportProvider.js +2 -10
- package/dist/context/providers/ThreadViewportProvider.js.map +1 -1
- package/dist/context/stores/ThreadViewport.d.ts +46 -4
- package/dist/context/stores/ThreadViewport.d.ts.map +1 -1
- package/dist/context/stores/ThreadViewport.js +51 -7
- package/dist/context/stores/ThreadViewport.js.map +1 -1
- package/dist/index.d.ts +1 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -28
- package/dist/index.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.d.ts +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.js +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.js.map +1 -1
- package/dist/primitives/composer/ComposerInput.d.ts.map +1 -1
- package/dist/primitives/composer/ComposerInput.js +9 -4
- package/dist/primitives/composer/ComposerInput.js.map +1 -1
- package/dist/primitives/message/MessageRoot.d.ts +6 -30
- package/dist/primitives/message/MessageRoot.d.ts.map +1 -1
- package/dist/primitives/message/MessageRoot.js +68 -25
- package/dist/primitives/message/MessageRoot.js.map +1 -1
- package/dist/primitives/thread/ThreadViewport.d.ts +38 -0
- package/dist/primitives/thread/ThreadViewport.d.ts.map +1 -1
- package/dist/primitives/thread/ThreadViewport.js +53 -5
- package/dist/primitives/thread/ThreadViewport.js.map +1 -1
- package/dist/primitives/thread/ThreadViewportFooter.d.ts +2 -1
- package/dist/primitives/thread/ThreadViewportFooter.d.ts.map +1 -1
- package/dist/primitives/thread/ThreadViewportFooter.js +2 -1
- package/dist/primitives/thread/ThreadViewportFooter.js.map +1 -1
- package/dist/primitives/thread/topAnchor/computeTopAnchorSlack.d.ts +22 -0
- package/dist/primitives/thread/topAnchor/computeTopAnchorSlack.d.ts.map +1 -0
- package/dist/primitives/thread/topAnchor/computeTopAnchorSlack.js +53 -0
- package/dist/primitives/thread/topAnchor/computeTopAnchorSlack.js.map +1 -0
- package/dist/primitives/thread/topAnchor/createReserveObservers.d.ts +5 -0
- package/dist/primitives/thread/topAnchor/createReserveObservers.d.ts.map +1 -0
- package/dist/primitives/thread/topAnchor/createReserveObservers.js +38 -0
- package/dist/primitives/thread/topAnchor/createReserveObservers.js.map +1 -0
- package/dist/primitives/thread/topAnchor/mountTopAnchorReserve.d.ts +22 -0
- package/dist/primitives/thread/topAnchor/mountTopAnchorReserve.d.ts.map +1 -0
- package/dist/primitives/thread/topAnchor/mountTopAnchorReserve.js +75 -0
- package/dist/primitives/thread/topAnchor/mountTopAnchorReserve.js.map +1 -0
- package/dist/primitives/thread/topAnchor/topAnchorTurn.d.ts +15 -0
- package/dist/primitives/thread/topAnchor/topAnchorTurn.d.ts.map +1 -0
- package/dist/primitives/thread/topAnchor/topAnchorTurn.js +13 -0
- package/dist/primitives/thread/topAnchor/topAnchorTurn.js.map +1 -0
- package/dist/primitives/thread/topAnchor/topAnchorUtils.d.ts +15 -0
- package/dist/primitives/thread/topAnchor/topAnchorUtils.d.ts.map +1 -0
- package/dist/primitives/thread/topAnchor/topAnchorUtils.js +51 -0
- package/dist/primitives/thread/topAnchor/topAnchorUtils.js.map +1 -0
- package/dist/primitives/thread/topAnchor/useTopAnchorReserve.d.ts +7 -0
- package/dist/primitives/thread/topAnchor/useTopAnchorReserve.d.ts.map +1 -0
- package/dist/primitives/thread/topAnchor/useTopAnchorReserve.js +18 -0
- package/dist/primitives/thread/topAnchor/useTopAnchorReserve.js.map +1 -0
- package/dist/primitives/thread/useThreadViewportAutoScroll.d.ts.map +1 -1
- package/dist/primitives/thread/useThreadViewportAutoScroll.js +13 -1
- package/dist/primitives/thread/useThreadViewportAutoScroll.js.map +1 -1
- package/dist/primitives/thread.d.ts +0 -1
- package/dist/primitives/thread.d.ts.map +1 -1
- package/dist/primitives/thread.js +0 -1
- package/dist/primitives/thread.js.map +1 -1
- package/dist/primitives/threadList/ThreadListLoadMore.d.ts +13 -0
- package/dist/primitives/threadList/ThreadListLoadMore.d.ts.map +1 -0
- package/dist/primitives/threadList/ThreadListLoadMore.js +11 -0
- package/dist/primitives/threadList/ThreadListLoadMore.js.map +1 -0
- package/dist/primitives/threadList.d.ts +1 -0
- package/dist/primitives/threadList.d.ts.map +1 -1
- package/dist/primitives/threadList.js +1 -0
- package/dist/primitives/threadList.js.map +1 -1
- package/dist/utils/hooks/useManagedRef.d.ts.map +1 -1
- package/dist/utils/hooks/useManagedRef.js +1 -0
- package/dist/utils/hooks/useManagedRef.js.map +1 -1
- package/dist/utils/hooks/useOnResizeContent.d.ts.map +1 -1
- package/dist/utils/hooks/useOnResizeContent.js +1 -2
- package/dist/utils/hooks/useOnResizeContent.js.map +1 -1
- package/package.json +10 -10
- package/src/client/ExternalThread.ts +0 -2
- package/src/client/InMemoryThreadList.ts +3 -0
- package/src/client/SingleThreadList.ts +3 -0
- package/src/context/providers/ThreadViewportProvider.tsx +2 -12
- package/src/context/stores/ThreadViewport.ts +111 -11
- package/src/index.ts +0 -35
- package/src/legacy-runtime/runtime-cores/assistant-transport/utils.ts +1 -5
- package/src/primitives/composer/ComposerInput.test.tsx +232 -0
- package/src/primitives/composer/ComposerInput.tsx +9 -4
- package/src/primitives/message/MessageRoot.tsx +135 -57
- package/src/primitives/thread/ThreadViewport.tsx +95 -4
- package/src/primitives/thread/ThreadViewportFooter.tsx +2 -1
- package/src/primitives/thread/topAnchor/computeTopAnchorSlack.test.ts +131 -0
- package/src/primitives/thread/topAnchor/computeTopAnchorSlack.ts +94 -0
- package/src/primitives/thread/topAnchor/createReserveObservers.ts +50 -0
- package/src/primitives/thread/topAnchor/mountTopAnchorReserve.test.ts +131 -0
- package/src/primitives/thread/topAnchor/mountTopAnchorReserve.ts +127 -0
- package/src/primitives/thread/topAnchor/topAnchorTurn.test.ts +46 -0
- package/src/primitives/thread/topAnchor/topAnchorTurn.ts +30 -0
- package/src/primitives/thread/topAnchor/topAnchorUtils.ts +58 -0
- package/src/primitives/thread/topAnchor/useTopAnchorReserve.ts +19 -0
- package/src/primitives/thread/useThreadViewportAutoScroll.ts +15 -1
- package/src/primitives/thread.ts +0 -1
- package/src/primitives/threadList/ThreadListLoadMore.tsx +24 -0
- package/src/primitives/threadList.ts +1 -0
- package/src/tests/RemoteThreadListRuntime.adapterProvider.test.tsx +138 -0
- package/src/tests/RemoteThreadListRuntime.deferredProvider.test.tsx +28 -17
- package/src/utils/hooks/useManagedRef.ts +1 -0
- package/src/utils/hooks/useOnResizeContent.ts +1 -2
- package/dist/legacy-runtime/runtime-cores/external-store/getExternalStoreMessage.d.ts +0 -3
- package/dist/legacy-runtime/runtime-cores/external-store/getExternalStoreMessage.d.ts.map +0 -1
- package/dist/legacy-runtime/runtime-cores/external-store/getExternalStoreMessage.js +0 -3
- package/dist/legacy-runtime/runtime-cores/external-store/getExternalStoreMessage.js.map +0 -1
- package/dist/primitives/thread/ThreadViewportSlack.d.ts +0 -20
- package/dist/primitives/thread/ThreadViewportSlack.d.ts.map +0 -1
- package/dist/primitives/thread/ThreadViewportSlack.js +0 -80
- package/dist/primitives/thread/ThreadViewportSlack.js.map +0 -1
- package/src/legacy-runtime/runtime-cores/external-store/getExternalStoreMessage.ts +0 -6
- package/src/primitives/thread/ThreadViewportSlack.tsx +0 -116
|
@@ -5,14 +5,21 @@ import {
|
|
|
5
5
|
type ComponentRef,
|
|
6
6
|
forwardRef,
|
|
7
7
|
type ComponentPropsWithoutRef,
|
|
8
|
+
type ForwardedRef,
|
|
8
9
|
useCallback,
|
|
9
10
|
} from "react";
|
|
10
11
|
import { useAui, useAuiState } from "@assistant-ui/store";
|
|
11
12
|
import { useManagedRef } from "../../utils/hooks/useManagedRef";
|
|
12
|
-
import { useSizeHandle } from "../../utils/hooks/useSizeHandle";
|
|
13
13
|
import { useComposedRefs } from "@radix-ui/react-compose-refs";
|
|
14
|
-
import {
|
|
15
|
-
|
|
14
|
+
import {
|
|
15
|
+
useThreadViewport,
|
|
16
|
+
useThreadViewportStore,
|
|
17
|
+
} from "../../context/react/ThreadViewportContext";
|
|
18
|
+
import { parseCssLength } from "../thread/topAnchor/topAnchorUtils";
|
|
19
|
+
|
|
20
|
+
type ThreadViewportStore = NonNullable<
|
|
21
|
+
ReturnType<typeof useThreadViewportStore>
|
|
22
|
+
>;
|
|
16
23
|
|
|
17
24
|
const useIsHoveringRef = () => {
|
|
18
25
|
const aui = useAui();
|
|
@@ -47,54 +54,126 @@ const useIsHoveringRef = () => {
|
|
|
47
54
|
return useManagedRef(callbackRef);
|
|
48
55
|
};
|
|
49
56
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*/
|
|
54
|
-
const useMessageViewportRef = () => {
|
|
55
|
-
const turnAnchor = useThreadViewport((s) => s.turnAnchor);
|
|
56
|
-
const registerUserHeight = useThreadViewport(
|
|
57
|
-
(s) => s.registerUserMessageHeight,
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
// inset rules:
|
|
61
|
-
// - the previous user message before the last assistant message registers its full height
|
|
62
|
-
const shouldRegisterAsInset = useAuiState(
|
|
57
|
+
const useIsTopAnchorUser = () => {
|
|
58
|
+
const activeAnchorId = useThreadViewport((s) => s.topAnchorTurn?.anchorId);
|
|
59
|
+
return useAuiState(
|
|
63
60
|
(s) =>
|
|
64
|
-
turnAnchor === "top" &&
|
|
65
61
|
s.message.role === "user" &&
|
|
62
|
+
s.message.index > 0 &&
|
|
66
63
|
s.message.index === s.thread.messages.length - 2 &&
|
|
67
|
-
s.thread.messages.at(-1)?.role === "assistant"
|
|
64
|
+
s.thread.messages.at(-1)?.role === "assistant" &&
|
|
65
|
+
(s.message.id === activeAnchorId || s.thread.isRunning),
|
|
68
66
|
);
|
|
67
|
+
};
|
|
69
68
|
|
|
70
|
-
|
|
69
|
+
const useIsTopAnchorTarget = () => {
|
|
70
|
+
const activeTargetId = useThreadViewport((s) => s.topAnchorTurn?.targetId);
|
|
71
|
+
return useAuiState(
|
|
72
|
+
(s) =>
|
|
73
|
+
s.message.isLast &&
|
|
74
|
+
s.message.role === "assistant" &&
|
|
75
|
+
s.message.index >= 1 &&
|
|
76
|
+
s.thread.messages.at(s.message.index - 1)?.role === "user" &&
|
|
77
|
+
(s.message.id === activeTargetId || s.thread.isRunning),
|
|
78
|
+
);
|
|
79
|
+
};
|
|
71
80
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
const useTopAnchorUserRef = (
|
|
82
|
+
active: boolean,
|
|
83
|
+
threadViewportStore: ThreadViewportStore,
|
|
84
|
+
) => {
|
|
85
|
+
const callback = useCallback(
|
|
86
|
+
(el: HTMLElement) => {
|
|
87
|
+
if (!active) return;
|
|
88
|
+
return threadViewportStore.getState().registerAnchorElement(el);
|
|
89
|
+
},
|
|
90
|
+
[active, threadViewportStore],
|
|
75
91
|
);
|
|
92
|
+
|
|
93
|
+
return useManagedRef<HTMLElement>(callback);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const useTopAnchorTargetRef = ({
|
|
97
|
+
active,
|
|
98
|
+
threadViewportStore,
|
|
99
|
+
}: {
|
|
100
|
+
active: boolean;
|
|
101
|
+
threadViewportStore: ThreadViewportStore;
|
|
102
|
+
}) => {
|
|
103
|
+
const targetRefCallback = useCallback(
|
|
104
|
+
(el: HTMLElement) => {
|
|
105
|
+
if (!active) return;
|
|
106
|
+
const state = threadViewportStore.getState();
|
|
107
|
+
const clamp = state.topAnchorMessageClamp;
|
|
108
|
+
|
|
109
|
+
return state.registerAnchorTargetElement(el, {
|
|
110
|
+
tallerThan: parseCssLength(clamp.tallerThan, el),
|
|
111
|
+
visibleHeight: parseCssLength(clamp.visibleHeight, el),
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
[active, threadViewportStore],
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
return useManagedRef<HTMLElement>(targetRefCallback);
|
|
76
118
|
};
|
|
77
119
|
|
|
78
120
|
export namespace MessagePrimitiveRoot {
|
|
79
121
|
export type Element = ComponentRef<typeof Primitive.div>;
|
|
80
|
-
|
|
81
|
-
* Props for the MessagePrimitive.Root component.
|
|
82
|
-
* Accepts all standard div element props plus optional viewport slack tuning.
|
|
83
|
-
*/
|
|
84
|
-
export type Props = ComponentPropsWithoutRef<typeof Primitive.div> & {
|
|
85
|
-
/**
|
|
86
|
-
* Threshold at which the user message height clamps to the offset.
|
|
87
|
-
* @default "10em"
|
|
88
|
-
*/
|
|
89
|
-
fillClampThreshold?: string | undefined;
|
|
90
|
-
/**
|
|
91
|
-
* Offset used when clamping large user messages.
|
|
92
|
-
* @default "6em"
|
|
93
|
-
*/
|
|
94
|
-
fillClampOffset?: string | undefined;
|
|
95
|
-
};
|
|
122
|
+
export type Props = ComponentPropsWithoutRef<typeof Primitive.div>;
|
|
96
123
|
}
|
|
97
124
|
|
|
125
|
+
type MessagePrimitiveRootInternalProps = MessagePrimitiveRoot.Props & {
|
|
126
|
+
forwardedRef: ForwardedRef<MessagePrimitiveRoot.Element>;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const MessagePrimitiveRootDefault = ({
|
|
130
|
+
forwardedRef,
|
|
131
|
+
...props
|
|
132
|
+
}: MessagePrimitiveRootInternalProps) => {
|
|
133
|
+
const isHoveringRef = useIsHoveringRef();
|
|
134
|
+
const ref = useComposedRefs<HTMLDivElement>(forwardedRef, isHoveringRef);
|
|
135
|
+
const messageId = useAuiState((s) => s.message.id);
|
|
136
|
+
|
|
137
|
+
return <Primitive.div {...props} ref={ref} data-message-id={messageId} />;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const MessagePrimitiveRootTopAnchor = ({
|
|
141
|
+
forwardedRef,
|
|
142
|
+
threadViewportStore,
|
|
143
|
+
...props
|
|
144
|
+
}: MessagePrimitiveRootInternalProps & {
|
|
145
|
+
threadViewportStore: ThreadViewportStore;
|
|
146
|
+
}) => {
|
|
147
|
+
const isHoveringRef = useIsHoveringRef();
|
|
148
|
+
const isTopAnchorUser = useIsTopAnchorUser();
|
|
149
|
+
const isTopAnchorTarget = useIsTopAnchorTarget();
|
|
150
|
+
const topAnchorUserRef = useTopAnchorUserRef(
|
|
151
|
+
isTopAnchorUser,
|
|
152
|
+
threadViewportStore,
|
|
153
|
+
);
|
|
154
|
+
const topAnchorTargetRef = useTopAnchorTargetRef({
|
|
155
|
+
active: isTopAnchorTarget,
|
|
156
|
+
threadViewportStore,
|
|
157
|
+
});
|
|
158
|
+
const ref = useComposedRefs<HTMLDivElement>(
|
|
159
|
+
forwardedRef,
|
|
160
|
+
isHoveringRef,
|
|
161
|
+
topAnchorUserRef,
|
|
162
|
+
topAnchorTargetRef,
|
|
163
|
+
);
|
|
164
|
+
const messageId = useAuiState((s) => s.message.id);
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<Primitive.div
|
|
168
|
+
{...props}
|
|
169
|
+
ref={ref}
|
|
170
|
+
data-message-id={messageId}
|
|
171
|
+
data-aui-top-anchor-user={isTopAnchorUser ? "" : undefined}
|
|
172
|
+
data-aui-top-anchor-target={isTopAnchorTarget ? "" : undefined}
|
|
173
|
+
/>
|
|
174
|
+
);
|
|
175
|
+
};
|
|
176
|
+
|
|
98
177
|
/**
|
|
99
178
|
* The root container component for a message.
|
|
100
179
|
*
|
|
@@ -102,8 +181,10 @@ export namespace MessagePrimitiveRoot {
|
|
|
102
181
|
* hover state management for the message. It automatically tracks when the user
|
|
103
182
|
* is hovering over the message, which can be used by child components like action bars.
|
|
104
183
|
*
|
|
105
|
-
* When `turnAnchor="top"` is set on the viewport, this component
|
|
106
|
-
* registers itself as the
|
|
184
|
+
* When `turnAnchor="top"` is set on the viewport, this component automatically
|
|
185
|
+
* registers itself as the top-anchor user message (when it's the previous user
|
|
186
|
+
* message) or as the top-anchor target (when it's the streaming assistant
|
|
187
|
+
* response). No additional component is required.
|
|
107
188
|
*
|
|
108
189
|
* @example
|
|
109
190
|
* ```tsx
|
|
@@ -119,24 +200,21 @@ export namespace MessagePrimitiveRoot {
|
|
|
119
200
|
export const MessagePrimitiveRoot = forwardRef<
|
|
120
201
|
MessagePrimitiveRoot.Element,
|
|
121
202
|
MessagePrimitiveRoot.Props
|
|
122
|
-
>((
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
forwardRef,
|
|
127
|
-
isHoveringRef,
|
|
128
|
-
anchorUserMessageRef,
|
|
129
|
-
);
|
|
130
|
-
const messageId = useAuiState((s) => s.message.id);
|
|
203
|
+
>((props, forwardedRef) => {
|
|
204
|
+
const threadViewportStore = useThreadViewportStore();
|
|
205
|
+
// turnAnchor is initial-only viewport config (see ThreadViewportProvider).
|
|
206
|
+
const turnAnchor = threadViewportStore.getState().turnAnchor;
|
|
131
207
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
208
|
+
if (turnAnchor === "top") {
|
|
209
|
+
return (
|
|
210
|
+
<MessagePrimitiveRootTopAnchor
|
|
211
|
+
{...props}
|
|
212
|
+
forwardedRef={forwardedRef}
|
|
213
|
+
threadViewportStore={threadViewportStore}
|
|
214
|
+
/>
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
return <MessagePrimitiveRootDefault {...props} forwardedRef={forwardedRef} />;
|
|
140
218
|
});
|
|
141
219
|
|
|
142
220
|
MessagePrimitiveRoot.displayName = "MessagePrimitive.Root";
|
|
@@ -7,11 +7,23 @@ import {
|
|
|
7
7
|
forwardRef,
|
|
8
8
|
type ComponentPropsWithoutRef,
|
|
9
9
|
useCallback,
|
|
10
|
+
useLayoutEffect,
|
|
11
|
+
useMemo,
|
|
10
12
|
} from "react";
|
|
13
|
+
import { useAuiEvent, useAuiState } from "@assistant-ui/store";
|
|
14
|
+
import { useManagedRef } from "../../utils/hooks/useManagedRef";
|
|
11
15
|
import { useThreadViewportAutoScroll } from "./useThreadViewportAutoScroll";
|
|
12
16
|
import { ThreadPrimitiveViewportProvider } from "../../context/providers/ThreadViewportProvider";
|
|
13
17
|
import { useSizeHandle } from "../../utils/hooks/useSizeHandle";
|
|
14
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
useThreadViewport,
|
|
20
|
+
useThreadViewportStore,
|
|
21
|
+
} from "../../context/react/ThreadViewportContext";
|
|
22
|
+
import { useTopAnchorReserve } from "./topAnchor/useTopAnchorReserve";
|
|
23
|
+
import {
|
|
24
|
+
getActiveTopAnchorAnchorId,
|
|
25
|
+
getActiveTopAnchorTargetId,
|
|
26
|
+
} from "./topAnchor/topAnchorTurn";
|
|
15
27
|
|
|
16
28
|
export namespace ThreadPrimitiveViewport {
|
|
17
29
|
export type Element = ComponentRef<typeof Primitive.div>;
|
|
@@ -31,6 +43,26 @@ export namespace ThreadPrimitiveViewport {
|
|
|
31
43
|
*/
|
|
32
44
|
turnAnchor?: "top" | "bottom" | undefined;
|
|
33
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Clamps tall user messages so the assistant response stays in view.
|
|
48
|
+
*
|
|
49
|
+
* @default { tallerThan: "10em", visibleHeight: "6em" }
|
|
50
|
+
*/
|
|
51
|
+
topAnchorMessageClamp?: {
|
|
52
|
+
/**
|
|
53
|
+
* Clamp messages taller than this. Supports `px`, `em`, and `rem`.
|
|
54
|
+
*
|
|
55
|
+
* @default "10em"
|
|
56
|
+
*/
|
|
57
|
+
tallerThan?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Visible portion of clamped messages. Supports `px`, `em`, and `rem`.
|
|
60
|
+
*
|
|
61
|
+
* @default "6em"
|
|
62
|
+
*/
|
|
63
|
+
visibleHeight?: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
34
66
|
/**
|
|
35
67
|
* Whether to scroll to bottom when a new run starts.
|
|
36
68
|
*
|
|
@@ -60,6 +92,52 @@ const useViewportSizeRef = () => {
|
|
|
60
92
|
return useSizeHandle(register, getHeight);
|
|
61
93
|
};
|
|
62
94
|
|
|
95
|
+
const useViewportElementRef = () => {
|
|
96
|
+
const registerViewportElement = useThreadViewport(
|
|
97
|
+
(s) => s.registerViewportElement,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return useManagedRef(registerViewportElement);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const useTopAnchorTurn = (enabled: boolean) => {
|
|
104
|
+
const threadViewportStore = useThreadViewportStore();
|
|
105
|
+
const activeAnchorId = useAuiState((s) => {
|
|
106
|
+
if (!enabled) return undefined;
|
|
107
|
+
return getActiveTopAnchorAnchorId(s.thread);
|
|
108
|
+
});
|
|
109
|
+
const activeTargetId = useAuiState((s) => {
|
|
110
|
+
if (!enabled) return undefined;
|
|
111
|
+
return getActiveTopAnchorTargetId(s.thread);
|
|
112
|
+
});
|
|
113
|
+
const activeTurn = useMemo(() => {
|
|
114
|
+
if (!activeAnchorId || !activeTargetId) return null;
|
|
115
|
+
return { anchorId: activeAnchorId, targetId: activeTargetId };
|
|
116
|
+
}, [activeAnchorId, activeTargetId]);
|
|
117
|
+
|
|
118
|
+
useLayoutEffect(() => {
|
|
119
|
+
if (!activeTurn) return;
|
|
120
|
+
|
|
121
|
+
const state = threadViewportStore.getState();
|
|
122
|
+
const current = state.topAnchorTurn;
|
|
123
|
+
if (
|
|
124
|
+
current?.anchorId === activeTurn.anchorId &&
|
|
125
|
+
current.targetId === activeTurn.targetId
|
|
126
|
+
) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
state.setTopAnchorTurn(activeTurn);
|
|
131
|
+
}, [activeTurn, threadViewportStore]);
|
|
132
|
+
|
|
133
|
+
const clearTopAnchorTurn = useCallback(() => {
|
|
134
|
+
threadViewportStore.getState().setTopAnchorTurn(null);
|
|
135
|
+
}, [threadViewportStore]);
|
|
136
|
+
|
|
137
|
+
useAuiEvent("thread.initialize", clearTopAnchorTurn);
|
|
138
|
+
useAuiEvent("threadListItem.switchedTo", clearTopAnchorTurn);
|
|
139
|
+
};
|
|
140
|
+
|
|
63
141
|
const ThreadPrimitiveViewportScrollable = forwardRef<
|
|
64
142
|
ThreadPrimitiveViewport.Element,
|
|
65
143
|
ThreadPrimitiveViewport.Props
|
|
@@ -82,7 +160,18 @@ const ThreadPrimitiveViewportScrollable = forwardRef<
|
|
|
82
160
|
scrollToBottomOnThreadSwitch,
|
|
83
161
|
});
|
|
84
162
|
const viewportSizeRef = useViewportSizeRef();
|
|
85
|
-
const
|
|
163
|
+
const viewportElementRef = useViewportElementRef();
|
|
164
|
+
const threadViewportStore = useThreadViewportStore();
|
|
165
|
+
const turnAnchor = threadViewportStore.getState().turnAnchor;
|
|
166
|
+
const topAnchorEnabled = turnAnchor === "top";
|
|
167
|
+
useTopAnchorTurn(topAnchorEnabled);
|
|
168
|
+
useTopAnchorReserve(topAnchorEnabled);
|
|
169
|
+
const ref = useComposedRefs(
|
|
170
|
+
forwardedRef,
|
|
171
|
+
autoScrollRef,
|
|
172
|
+
viewportSizeRef,
|
|
173
|
+
viewportElementRef,
|
|
174
|
+
);
|
|
86
175
|
|
|
87
176
|
return (
|
|
88
177
|
<Primitive.div {...rest} ref={ref}>
|
|
@@ -114,9 +203,11 @@ ThreadPrimitiveViewportScrollable.displayName =
|
|
|
114
203
|
export const ThreadPrimitiveViewport = forwardRef<
|
|
115
204
|
ThreadPrimitiveViewport.Element,
|
|
116
205
|
ThreadPrimitiveViewport.Props
|
|
117
|
-
>(({ turnAnchor, ...props }, ref) => {
|
|
206
|
+
>(({ turnAnchor, topAnchorMessageClamp, ...props }, ref) => {
|
|
118
207
|
return (
|
|
119
|
-
<ThreadPrimitiveViewportProvider
|
|
208
|
+
<ThreadPrimitiveViewportProvider
|
|
209
|
+
options={{ turnAnchor, topAnchorMessageClamp }}
|
|
210
|
+
>
|
|
120
211
|
<ThreadPrimitiveViewportScrollable {...props} ref={ref} />
|
|
121
212
|
</ThreadPrimitiveViewportProvider>
|
|
122
213
|
);
|
|
@@ -20,7 +20,8 @@ export namespace ThreadPrimitiveViewportFooter {
|
|
|
20
20
|
* A footer container that measures its height for scroll calculations.
|
|
21
21
|
*
|
|
22
22
|
* This component measures its height and provides it to the viewport context
|
|
23
|
-
*
|
|
23
|
+
* so the auto-scroll system can account for any sticky footer overlapping the
|
|
24
|
+
* message list.
|
|
24
25
|
*
|
|
25
26
|
* Multiple ViewportFooter components can be used - their heights are summed.
|
|
26
27
|
*
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
computeTopAnchorReserve,
|
|
4
|
+
computeTopAnchorTargetScrollTop,
|
|
5
|
+
} from "./computeTopAnchorSlack";
|
|
6
|
+
|
|
7
|
+
const makeElement = ({
|
|
8
|
+
top = 0,
|
|
9
|
+
height = 0,
|
|
10
|
+
scrollTop = 0,
|
|
11
|
+
scrollHeight = 0,
|
|
12
|
+
clientHeight = 0,
|
|
13
|
+
offsetHeight = height,
|
|
14
|
+
offsetTop = top,
|
|
15
|
+
offsetParent = null,
|
|
16
|
+
}: {
|
|
17
|
+
top?: number;
|
|
18
|
+
height?: number;
|
|
19
|
+
scrollTop?: number;
|
|
20
|
+
scrollHeight?: number;
|
|
21
|
+
clientHeight?: number;
|
|
22
|
+
offsetHeight?: number;
|
|
23
|
+
offsetTop?: number;
|
|
24
|
+
offsetParent?: HTMLElement | null;
|
|
25
|
+
}) =>
|
|
26
|
+
({
|
|
27
|
+
scrollTop,
|
|
28
|
+
scrollHeight,
|
|
29
|
+
clientHeight,
|
|
30
|
+
offsetHeight,
|
|
31
|
+
offsetTop,
|
|
32
|
+
offsetParent,
|
|
33
|
+
getBoundingClientRect: () => ({
|
|
34
|
+
top,
|
|
35
|
+
height,
|
|
36
|
+
}),
|
|
37
|
+
}) as HTMLElement;
|
|
38
|
+
|
|
39
|
+
describe("computeTopAnchorTargetScrollTop", () => {
|
|
40
|
+
it("uses layout offset geometry instead of the anchor's transformed visual position", () => {
|
|
41
|
+
const viewport = makeElement({ offsetTop: 0 });
|
|
42
|
+
const anchor = makeElement({
|
|
43
|
+
top: 160,
|
|
44
|
+
height: 60,
|
|
45
|
+
offsetTop: 156,
|
|
46
|
+
offsetParent: viewport,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(
|
|
50
|
+
computeTopAnchorTargetScrollTop({
|
|
51
|
+
viewport,
|
|
52
|
+
anchor,
|
|
53
|
+
tallerThan: 160,
|
|
54
|
+
visibleHeight: 96,
|
|
55
|
+
}),
|
|
56
|
+
).toBe(156);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("over-scrolls tall anchors so only visibleHeight is visible", () => {
|
|
60
|
+
const viewport = makeElement({ offsetTop: 0 });
|
|
61
|
+
const anchor = makeElement({ height: 240, offsetTop: 200 });
|
|
62
|
+
|
|
63
|
+
// 240 > 160 threshold => keep 96 visible => over-scroll by 240 - 96 = 144
|
|
64
|
+
expect(
|
|
65
|
+
computeTopAnchorTargetScrollTop({
|
|
66
|
+
viewport,
|
|
67
|
+
anchor,
|
|
68
|
+
tallerThan: 160,
|
|
69
|
+
visibleHeight: 96,
|
|
70
|
+
}),
|
|
71
|
+
).toBe(200 + 144);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("computeTopAnchorReserve", () => {
|
|
76
|
+
it("reserves only the extra height needed to make the anchor target reachable", () => {
|
|
77
|
+
const viewport = makeElement({
|
|
78
|
+
offsetTop: 0,
|
|
79
|
+
scrollTop: 0,
|
|
80
|
+
scrollHeight: 560,
|
|
81
|
+
clientHeight: 400,
|
|
82
|
+
});
|
|
83
|
+
const anchor = makeElement({ height: 64, offsetTop: 220 });
|
|
84
|
+
const reserve = makeElement({ offsetHeight: 0 });
|
|
85
|
+
|
|
86
|
+
expect(
|
|
87
|
+
computeTopAnchorReserve({
|
|
88
|
+
viewport,
|
|
89
|
+
anchor,
|
|
90
|
+
reserve,
|
|
91
|
+
tallerThan: 160,
|
|
92
|
+
visibleHeight: 96,
|
|
93
|
+
}),
|
|
94
|
+
).toBe(60);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("shrinks as the response content grows", () => {
|
|
98
|
+
const anchor = makeElement({ height: 64, offsetTop: 220 });
|
|
99
|
+
const reserve = makeElement({ offsetHeight: 60 });
|
|
100
|
+
|
|
101
|
+
expect(
|
|
102
|
+
computeTopAnchorReserve({
|
|
103
|
+
viewport: makeElement({
|
|
104
|
+
offsetTop: 0,
|
|
105
|
+
scrollTop: 0,
|
|
106
|
+
scrollHeight: 620,
|
|
107
|
+
clientHeight: 400,
|
|
108
|
+
}),
|
|
109
|
+
anchor,
|
|
110
|
+
reserve,
|
|
111
|
+
tallerThan: 160,
|
|
112
|
+
visibleHeight: 96,
|
|
113
|
+
}),
|
|
114
|
+
).toBe(60);
|
|
115
|
+
|
|
116
|
+
expect(
|
|
117
|
+
computeTopAnchorReserve({
|
|
118
|
+
viewport: makeElement({
|
|
119
|
+
offsetTop: 0,
|
|
120
|
+
scrollTop: 0,
|
|
121
|
+
scrollHeight: 680,
|
|
122
|
+
clientHeight: 400,
|
|
123
|
+
}),
|
|
124
|
+
anchor,
|
|
125
|
+
reserve,
|
|
126
|
+
tallerThan: 160,
|
|
127
|
+
visibleHeight: 96,
|
|
128
|
+
}),
|
|
129
|
+
).toBe(0);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
export type ComputeTopAnchorTargetOptions = {
|
|
4
|
+
viewport: HTMLElement;
|
|
5
|
+
anchor: HTMLElement;
|
|
6
|
+
tallerThan: number;
|
|
7
|
+
visibleHeight: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type ComputeTopAnchorReserveOptions = ComputeTopAnchorTargetOptions & {
|
|
11
|
+
reserve: HTMLElement;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type ComputeTopAnchorSlackOptions = ComputeTopAnchorTargetOptions & {
|
|
15
|
+
scrollHeight: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const getDocumentOffsetTop = (element: HTMLElement): number => {
|
|
19
|
+
let top = 0;
|
|
20
|
+
let current: HTMLElement | null = element;
|
|
21
|
+
|
|
22
|
+
while (current) {
|
|
23
|
+
top += current.offsetTop;
|
|
24
|
+
current = current.offsetParent as HTMLElement | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return top;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const getLayoutOffsetTop = (
|
|
31
|
+
element: HTMLElement,
|
|
32
|
+
ancestor: HTMLElement,
|
|
33
|
+
): number => {
|
|
34
|
+
// Use layout geometry, not visual rects, so entrance transforms/animations
|
|
35
|
+
// on the anchor do not shift the scroll target while they settle.
|
|
36
|
+
let top = 0;
|
|
37
|
+
let current: HTMLElement | null = element;
|
|
38
|
+
|
|
39
|
+
while (current && current !== ancestor) {
|
|
40
|
+
top += current.offsetTop;
|
|
41
|
+
current = current.offsetParent as HTMLElement | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (current === ancestor) return top;
|
|
45
|
+
|
|
46
|
+
return getDocumentOffsetTop(element) - getDocumentOffsetTop(ancestor);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Compute the scroll position that pins the anchor (last user message) to the
|
|
51
|
+
* top of the viewport. For tall user messages the anchor is intentionally
|
|
52
|
+
* over-scrolled so only `visibleHeight` of it remains visible, leaving room
|
|
53
|
+
* for the assistant message below.
|
|
54
|
+
*
|
|
55
|
+
* Depends only on the anchor's offset within the scroll content; never reads
|
|
56
|
+
* `viewport.scrollHeight` (which is volatile while the assistant message
|
|
57
|
+
* streams in).
|
|
58
|
+
*/
|
|
59
|
+
export const computeTopAnchorTargetScrollTop = ({
|
|
60
|
+
viewport,
|
|
61
|
+
anchor,
|
|
62
|
+
tallerThan,
|
|
63
|
+
visibleHeight,
|
|
64
|
+
}: ComputeTopAnchorTargetOptions): number => {
|
|
65
|
+
const anchorTop = getLayoutOffsetTop(anchor, viewport);
|
|
66
|
+
const anchorHeight = anchor.offsetHeight;
|
|
67
|
+
const visibleAnchorHeight =
|
|
68
|
+
anchorHeight <= tallerThan ? anchorHeight : visibleHeight;
|
|
69
|
+
|
|
70
|
+
return anchorTop + Math.max(0, anchorHeight - visibleAnchorHeight);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const computeTopAnchorSlack = ({
|
|
74
|
+
scrollHeight,
|
|
75
|
+
...targetOptions
|
|
76
|
+
}: ComputeTopAnchorSlackOptions): number => {
|
|
77
|
+
const { viewport } = targetOptions;
|
|
78
|
+
const targetScrollTop = computeTopAnchorTargetScrollTop(targetOptions);
|
|
79
|
+
const targetScrollHeight = targetScrollTop + viewport.clientHeight;
|
|
80
|
+
|
|
81
|
+
return Math.max(0, targetScrollHeight - scrollHeight);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const computeTopAnchorReserve = ({
|
|
85
|
+
viewport,
|
|
86
|
+
reserve,
|
|
87
|
+
...targetOptions
|
|
88
|
+
}: ComputeTopAnchorReserveOptions): number => {
|
|
89
|
+
return computeTopAnchorSlack({
|
|
90
|
+
viewport,
|
|
91
|
+
...targetOptions,
|
|
92
|
+
scrollHeight: viewport.scrollHeight - reserve.offsetHeight,
|
|
93
|
+
});
|
|
94
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
export const createReserveObservers = (onChange: () => void) => {
|
|
4
|
+
const resizeObserver = new ResizeObserver(onChange);
|
|
5
|
+
const mutationObserver = new MutationObserver(onChange);
|
|
6
|
+
|
|
7
|
+
let observedViewport: HTMLElement | null = null;
|
|
8
|
+
let observedAnchor: HTMLElement | null = null;
|
|
9
|
+
let observedTarget: HTMLElement | null = null;
|
|
10
|
+
|
|
11
|
+
const disconnect = () => {
|
|
12
|
+
resizeObserver.disconnect();
|
|
13
|
+
mutationObserver.disconnect();
|
|
14
|
+
observedViewport = null;
|
|
15
|
+
observedAnchor = null;
|
|
16
|
+
observedTarget = null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
target: (
|
|
21
|
+
viewport: HTMLElement,
|
|
22
|
+
anchor: HTMLElement,
|
|
23
|
+
target: HTMLElement,
|
|
24
|
+
) => {
|
|
25
|
+
if (
|
|
26
|
+
observedViewport === viewport &&
|
|
27
|
+
observedAnchor === anchor &&
|
|
28
|
+
observedTarget === target
|
|
29
|
+
) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
disconnect();
|
|
34
|
+
|
|
35
|
+
resizeObserver.observe(viewport);
|
|
36
|
+
resizeObserver.observe(anchor);
|
|
37
|
+
resizeObserver.observe(target);
|
|
38
|
+
mutationObserver.observe(target, {
|
|
39
|
+
childList: true,
|
|
40
|
+
subtree: true,
|
|
41
|
+
characterData: true,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
observedViewport = viewport;
|
|
45
|
+
observedAnchor = anchor;
|
|
46
|
+
observedTarget = target;
|
|
47
|
+
},
|
|
48
|
+
disconnect,
|
|
49
|
+
};
|
|
50
|
+
};
|