Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix: restore pre-CoT reasoning duration + prevent action flicker during tool calls (#8133)
Akarshan Biswas136fee8aad13
diff --git a/web-app/src/components/ai-elements/__tests__/chain-of-thought.test.tsx b/web-app/src/components/ai-elements/__tests__/chain-of-thought.test.tsxindex 02026863e..e14dae2e5 100644--- a/web-app/src/components/ai-elements/__tests__/chain-of-thought.test.tsx+++ b/web-app/src/components/ai-elements/__tests__/chain-of-thought.test.tsx@@ -129,7 +129,7 @@ describe('ChainOfThoughtHeader', () => {</ChainOfThought>)expect(- screen.getByText('Reasoned through the problem')+ screen.getByText('Thought for a few seconds')).toBeInTheDocument()})@@ -143,7 +143,7 @@ describe('ChainOfThoughtHeader', () => {)expect(screen.getByText('Custom header')).toBeInTheDocument()expect(- screen.queryByText('Reasoned through the problem')+ screen.queryByText(/Thought for/)).not.toBeInTheDocument()})})diff --git a/web-app/src/components/ai-elements/chain-of-thought.tsx b/web-app/src/components/ai-elements/chain-of-thought.tsxindex 05e4cebaa..329f41d6a 100644--- a/web-app/src/components/ai-elements/chain-of-thought.tsx+++ b/web-app/src/components/ai-elements/chain-of-thought.tsx@@ -22,6 +22,7 @@ import {useContext,useEffect,useMemo,+ useState,} from 'react'import { Streamdown } from 'streamdown'import { Shimmer } from './shimmer'@@ -36,8 +37,11 @@ type ChainOfThoughtContextValue = {isOpen: booleansetIsOpen: (open: boolean) => voidisStreaming: boolean+ duration: number | undefined}+const MS_IN_S = 1000+const ChainOfThoughtContext = createContext<ChainOfThoughtContextValue | null>(null)@@ -91,9 +95,23 @@ export const ChainOfThought = memo(setIsOpen(newOpen)}+ const [startTime, setStartTime] = useState<number | null>(null)+ const [duration, setDuration] = useState<number | undefined>(undefined)++ useEffect(() => {+ if (isStreaming) {+ if (startTime === null) {+ setStartTime(Date.now())+ }+ } else if (startTime !== null) {+ setDuration(Math.ceil((Date.now() - startTime) / MS_IN_S))+ setStartTime(null)+ }+ }, [isStreaming, startTime])+const contextValue = useMemo(- () => ({ isStreaming, isOpen, setIsOpen }),- [isStreaming, isOpen, setIsOpen]+ () => ({ isStreaming, isOpen, setIsOpen, duration }),+ [isStreaming, isOpen, setIsOpen, duration])return (@@ -121,7 +139,7 @@ export type ChainOfThoughtHeaderProps = ComponentProps<export const ChainOfThoughtHeader = memo(({ className, title, children, ...props }: ChainOfThoughtHeaderProps) => {- const { isStreaming, isOpen } = useChainOfThought()+ const { isStreaming, isOpen, duration } = useChainOfThought()return (<CollapsibleTrigger@@ -134,10 +152,14 @@ export const ChainOfThoughtHeader = memo({children ?? (<><SparklesIcon className="size-4" />- {isStreaming ? (+ {isStreaming || duration === 0 ? (<Shimmer duration={1}>Reasoning...</Shimmer>+ ) : title ? (+ <p>{title}</p>+ ) : duration === undefined ? (+ <p>Thought for a few seconds</p>) : (- <p>{title ?? 'Reasoned through the problem'}</p>+ <p>Thought for {duration} seconds</p>)}<ChevronDownIconclassName={cn(diff --git a/web-app/src/containers/MessageItem.tsx b/web-app/src/containers/MessageItem.tsxindex 1639d6710..2d1c23012 100644--- a/web-app/src/containers/MessageItem.tsx+++ b/web-app/src/containers/MessageItem.tsx@@ -107,7 +107,25 @@ export const MessageItem = memo(.map((part) => (part as { url: string }).url)}, [message.parts])- const isStreaming = isLastMessage && status === CHAT_STATUS.STREAMING+ // A tool part is "pending" until it reaches a terminal state. While any+ // tool on the last assistant message is still pending the turn isn't+ // done — the model will resume once the tool result arrives, even if the+ // SDK briefly reports status as 'ready' between the tool-call stream and+ // the follow-up request.+ const hasPendingToolCall = useMemo(() => {+ if (!isLastMessage || message.role !== 'assistant') return false+ return message.parts.some((part) => {+ if (!part.type?.startsWith('tool-')) return false+ const state = (part as { state?: string }).state+ return state !== 'output-available' && state !== 'output-error'+ })+ }, [isLastMessage, message.role, message.parts])++ const isStreaming =+ (isLastMessage &&+ (status === CHAT_STATUS.STREAMING ||+ status === CHAT_STATUS.SUBMITTED)) ||+ hasPendingToolCall// Extract file metadata from message text (for user messages with attachments)const attachedFiles = useMemo(() => {@@ -460,7 +478,8 @@ export const MessageItem = memo(</span><CopyButton text={getFullTextContent()} />- {onEdit && status !== CHAT_STATUS.STREAMING && (+ {onEdit && status !== CHAT_STATUS.STREAMING &&+ status !== CHAT_STATUS.SUBMITTED && (<EditMessageDialogmessage={getFullTextContent()}imageUrls={imageUrls.length > 0 ? imageUrls : undefined}@@ -468,7 +487,8 @@ export const MessageItem = memo(/>)}- {onDelete && status !== CHAT_STATUS.STREAMING && (+ {onDelete && status !== CHAT_STATUS.STREAMING &&+ status !== CHAT_STATUS.SUBMITTED && (<DeleteMessageDialog onDelete={handleDelete} />)}</div>@@ -538,8 +558,12 @@ export const MessageItem = memo()},(prevProps, nextProps) => {- // Always re-render if streaming and this is the last message- if (nextProps.isLastMessage && nextProps.status === CHAT_STATUS.STREAMING) {+ // Always re-render if the last message is in-flight (streaming or submitted)+ if (+ nextProps.isLastMessage &&+ (nextProps.status === CHAT_STATUS.STREAMING ||+ nextProps.status === CHAT_STATUS.SUBMITTED)+ ) {return false}