Track tool part versions for SSE updates

This commit is contained in:
Shantur Rathore
2025-11-07 13:41:07 +00:00
parent e7957fd67d
commit 27da588d22
4 changed files with 239 additions and 201 deletions

View File

@@ -167,8 +167,10 @@ export default function MessageStream(props: MessageStreamProps) {
let messageItemCache = new Map<string, MessageCacheEntry>()
let toolItemCache = new Map<string, ToolCacheEntry>()
let scrollAnimationFrame: number | null = null
let lastKnownScrollTop = 0
const makeScrollKey = (instanceId: string, sessionId: string) => `${instanceId}:${sessionId}`
const scrollStateKey = () => makeScrollKey(props.instanceId, props.sessionId)
const connectionStatus = () => sseManager.getStatus(props.instanceId)
@@ -180,6 +182,12 @@ export default function MessageStream(props: MessageStreamProps) {
function createToolContentKey(toolPart: any, messageInfo?: any): string {
const state = toolPart?.state ?? {}
const version = typeof toolPart?.__version === "number" ? toolPart.__version : null
if (version !== null) {
const status = state?.status ?? "unknown"
return `${version}:${status}`
}
const metadata = state?.metadata ?? {}
const input = state?.input ?? {}
const output = state?.output ?? {}
@@ -261,18 +269,32 @@ export default function MessageStream(props: MessageStreamProps) {
})
}
function handleScroll() {
function handleScroll(event: Event) {
if (!containerRef) return
if (scrollAnimationFrame !== null) {
cancelAnimationFrame(scrollAnimationFrame)
}
const isUserScroll = event.isTrusted
scrollAnimationFrame = requestAnimationFrame(() => {
if (!containerRef) return
const currentScrollTop = containerRef.scrollTop
const movingUp = currentScrollTop < lastKnownScrollTop - 1
lastKnownScrollTop = currentScrollTop
const atBottom = isNearBottom(containerRef)
setAutoScroll(atBottom)
if (isUserScroll) {
if ((movingUp || !atBottom) && autoScroll()) {
setAutoScroll(false)
} else if (!movingUp && atBottom && !autoScroll()) {
setAutoScroll(true)
}
}
updateScrollIndicators(containerRef)
scrollAnimationFrame = null
})