Skip to content

Commit

Permalink
chore: fix chat flickering (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
geclos authored Sep 26, 2024
1 parent 69ba466 commit 3f6e720
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export default function Chat({
if (data.type === ChainEventTypes.Step) {
if (data.isLastStep) setChainLength(messagesCount + 1)
} else if (data.type === ChainEventTypes.Complete) {
setResponseStream(undefined)
setTokens(data.response.usage.totalTokens)
setEndTime(performance.now())
} else if (data.type === ChainEventTypes.Error) {
Expand All @@ -123,9 +124,6 @@ export default function Chat({
if (data.type === 'text-delta') {
response += data.textDelta
setResponseStream(response)
} else if (data.type === 'finish') {
setResponseStream(undefined)
response = ''
}
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export default function Chat({
const [error, setError] = useState<Error | undefined>()
const [tokens, setTokens] = useState<number>(0)
const [isScrolledToBottom, setIsScrolledToBottom] = useState(false)
const [startTime, _] = useState(performance.now())
const [endTime, setEndTime] = useState<number>()
const [time, setTime] = useState<number>()
const containerRef = useRef<HTMLDivElement>(null)
useAutoScroll(containerRef, {
startAtBottom: true,
Expand Down Expand Up @@ -73,6 +72,7 @@ export default function Chat({
)

const runDocument = useCallback(async () => {
const start = performance.now()
setError(undefined)
setResponseStream(undefined)

Expand Down Expand Up @@ -105,8 +105,9 @@ export default function Chat({
if (data.type === ChainEventTypes.Step) {
if (data.isLastStep) setChainLength(messagesCount + 1)
} else if (data.type === ChainEventTypes.Complete) {
setResponseStream(undefined)
setTokens(data.response.usage.totalTokens)
setEndTime(performance.now())
setTime(performance.now() - start)
} else if (data.type === ChainEventTypes.Error) {
setError(new Error(data.error.message))
}
Expand All @@ -118,9 +119,6 @@ export default function Chat({
if (data.type === 'text-delta') {
response += data.textDelta
setResponseStream(response)
} else if (data.type === 'finish') {
setResponseStream(undefined)
response = ''
}
break
}
Expand Down Expand Up @@ -175,6 +173,7 @@ export default function Chat({
role: MessageRole.assistant,
content: data.response.text,
} as AssistantMessage)
setResponseStream(undefined)
}

break
Expand All @@ -184,9 +183,6 @@ export default function Chat({
if (data.type === 'text-delta') {
response += data.textDelta
setResponseStream(response)
} else if (data.type === 'finish') {
setResponseStream(undefined)
response = ''
}
break
}
Expand Down Expand Up @@ -216,7 +212,7 @@ export default function Chat({
}
variant='accent'
/>
{endTime && <Timer timeMs={endTime - startTime} />}
{time && <Timer timeMs={time} />}
</>
)}
{(conversation?.messages.length ?? 0) > chainLength && (
Expand Down

0 comments on commit 3f6e720

Please sign in to comment.