Skip to content

Commit

Permalink
Fix autoscroll in various situations (retrieval actions, citations...) (
Browse files Browse the repository at this point in the history
#2273)

* listen to action changes for scroll

* listen to citations and adapt to message length

* explain it
  • Loading branch information
philipperolet authored Oct 26, 2023
1 parent 55ed009 commit 229d5f1
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions front/components/assistant/conversation/AgentMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export function AgentMessage({
const [isRetryHandlerProcessing, setIsRetryHandlerProcessing] =
useState<boolean>(false);

const [references, setReferences] = useState<{
[key: string]: RetrievalDocumentType;
}>({});

const [activeReferences, setActiveReferences] = useState<
{ index: number; document: RetrievalDocumentType }[]
>([]);

const shouldStream = (() => {
if (message.status !== "created") {
return false;
Expand Down Expand Up @@ -185,17 +193,40 @@ export function AgentMessage({
}
})();

// Autoscroll is performed when a message is generating and the page is
// already scrolled down; but if the user has scrolled the page up after the
// start of the message, we do not want to scroll it back down.
//
// Checking the conversation is already at the bottom of the screen is done
// modulo a small margin (50px). This value is small because if large, it
// prevents user from scrolling up when the message continues generating
// (forces it back down), but it cannot be zero otherwise the scroll does not
// happen.
//
// Keeping a ref on the message's height, and accounting to the height
// difference when new parts of the message are generated, allows for the
// scroll to happen even if the newly generated parts are larger than 50px
// (e.g. citations)
const messageRef = useRef<HTMLDivElement>(null);
const messageHeight = useRef<number | null>(null);
useEffect(() => {
const previousHeight = messageHeight.current || 0;
messageHeight.current = messageRef.current?.scrollHeight || previousHeight;
const mainTag = document.getElementById("main-content");
if (mainTag && agentMessageToRender.status === "created") {
if (
mainTag.offsetHeight + mainTag.scrollTop >=
mainTag.scrollHeight - 50
mainTag.scrollHeight - (50 + messageHeight.current - previousHeight)
) {
mainTag.scrollTo(0, mainTag.scrollHeight);
}
}
}, [agentMessageToRender.content, agentMessageToRender.status]);
}, [
agentMessageToRender.content,
agentMessageToRender.status,
agentMessageToRender.action,
activeReferences.length,
]);

// GenerationContext: to know if we are generating or not
const generationContext = useContext(GenerationContext);
Expand Down Expand Up @@ -240,12 +271,6 @@ export function AgentMessage({
},
];

const [references, setReferences] = useState<{
[key: string]: RetrievalDocumentType;
}>({});
const [activeReferences, setActiveReferences] = useState<
{ index: number; document: RetrievalDocumentType }[]
>([]);
function updateActiveReferences(
document: RetrievalDocumentType,
index: number
Expand Down Expand Up @@ -286,7 +311,9 @@ export function AgentMessage({
reactions={reactions}
enableEmojis={true}
>
{renderMessage(agentMessageToRender, references, shouldStream)}
<div ref={messageRef}>
{renderMessage(agentMessageToRender, references, shouldStream)}
</div>
</ConversationMessage>
);

Expand Down

0 comments on commit 229d5f1

Please sign in to comment.