Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(chat): unify reasoning/tool trace into a single streaming timeline

Akarshan Biswas071790b91671
diff --git a/web-app/src/components/PromptProgress.tsx b/web-app/src/components/PromptProgress.tsx
index 88a1b588d..c215d11bd 100644
--- a/web-app/src/components/PromptProgress.tsx
+++ b/web-app/src/components/PromptProgress.tsx
@@ -3,7 +3,7 @@ import { Loader } from 'lucide-react'
import { useParams } from '@tanstack/react-router'
import { Progress } from '@/components/ui/progress'
-export function PromptProgress() {
+export function PromptProgress({ hideIdle = false }: { hideIdle?: boolean }) {
const params = useParams({ strict: false })
const threadId = (params as { threadId?: string })?.threadId
const promptProgress = useAppState((state) =>
@@ -25,6 +25,13 @@ export function PromptProgress() {
promptProgress.total > 0 &&
percentage < 100
+ // Nothing concrete to report (no model load, no prompt-reading progress).
+ // Callers driving their own activity label (e.g. tool-call traces) pass
+ // hideIdle to suppress the redundant generic "Working…" fallback.
+ if (hideIdle && !loadingModel && !showReading) {
+ return null
+ }
+
const label = loadingModel
? 'Loading model…'
: showReading
diff --git a/web-app/src/components/ai-elements/chain-of-thought.tsx b/web-app/src/components/ai-elements/chain-of-thought.tsx
index e09bb9ef6..30c81db55 100644
--- a/web-app/src/components/ai-elements/chain-of-thought.tsx
+++ b/web-app/src/components/ai-elements/chain-of-thought.tsx
@@ -62,6 +62,8 @@ export type ChainOfThoughtProps = ComponentProps<typeof Collapsible> & {
isStreaming?: boolean
/** When true the collapsible auto-collapses (e.g. text content appeared after this CoT group). */
shouldCollapse?: boolean
+ /** When true the collapsible is forced open and overrides auto-collapse (e.g. a tool is awaiting approval). */
+ forceOpen?: boolean
open?: boolean
defaultOpen?: boolean
onOpenChange?: (open: boolean) => void
@@ -72,6 +74,7 @@ export const ChainOfThought = memo(
className,
isStreaming = false,
shouldCollapse = false,
+ forceOpen = false,
open,
defaultOpen = true,
onOpenChange,
@@ -84,12 +87,15 @@ export const ChainOfThought = memo(
onChange: onOpenChange,
})
- // Auto-collapse once text content appears after this CoT group
+ // Auto-collapse once text content appears after this CoT group, unless
+ // something inside still needs the user's attention (forceOpen).
useEffect(() => {
- if (shouldCollapse) {
+ if (forceOpen) {
+ setIsOpen(true)
+ } else if (shouldCollapse) {
setIsOpen(false)
}
- }, [shouldCollapse, setIsOpen])
+ }, [forceOpen, shouldCollapse, setIsOpen])
const handleOpenChange = (newOpen: boolean) => {
setIsOpen(newOpen)
@@ -135,10 +141,21 @@ export type ChainOfThoughtHeaderProps = ComponentProps<
typeof CollapsibleTrigger
> & {
title?: string
+ /** Label shown while streaming, e.g. "Working...". Defaults to "Reasoning...". */
+ streamingLabel?: string
+ /** Past-tense verb for the completed state, e.g. "Worked". Defaults to "Thought". */
+ completedVerb?: string
}
export const ChainOfThoughtHeader = memo(
- ({ className, title, children, ...props }: ChainOfThoughtHeaderProps) => {
+ ({
+ className,
+ title,
+ streamingLabel = 'Reasoning...',
+ completedVerb = 'Thought',
+ children,
+ ...props
+ }: ChainOfThoughtHeaderProps) => {
const { isStreaming, isOpen, duration } = useChainOfThought()
return (
@@ -153,13 +170,15 @@ export const ChainOfThoughtHeader = memo(
<>
<SparklesIcon className="size-4" />
{isStreaming || duration === 0 ? (
- <Shimmer duration={1}>Reasoning...</Shimmer>
+ <Shimmer duration={1}>{streamingLabel}</Shimmer>
) : title ? (
<p>{title}</p>
) : duration === undefined ? (
- <p>Thought for a few seconds</p>
+ <p>{completedVerb} for a few seconds</p>
) : (
- <p>Thought for {duration} seconds</p>
+ <p>
+ {completedVerb} for {duration} seconds
+ </p>
)}
<ChevronDownIcon
className={cn(
diff --git a/web-app/src/containers/MessageItem.tsx b/web-app/src/containers/MessageItem.tsx
index 34a484043..675e98649 100644
--- a/web-app/src/containers/MessageItem.tsx
+++ b/web-app/src/containers/MessageItem.tsx
@@ -449,15 +449,6 @@ export const MessageItem = memo(
)
}
- // Group consecutive reasoning + tool parts into a single CoT block.
- // Empty text parts and step-start markers (inserted by the AI SDK during
- // multi-step tool use) are absorbed so they don't split the group.
- const isCotPart = (part: any) =>
- part.type === CONTENT_TYPE.REASONING ||
- part.type.startsWith('tool-') ||
- part.type === 'step-start' ||
- (part.type === CONTENT_TYPE.TEXT && (!part.text || part.text.trim() === ''))
-
type PartEntry = { part: any; index: number }
const renderCotGroup = (
@@ -465,32 +456,53 @@ export const MessageItem = memo(
groupKey: string,
hasFollowingContent: boolean
) => {
- const hasReasoning = entries.some(
- (e) => e.part.type === CONTENT_TYPE.REASONING
- )
-
- // No reasoning in this group — render tool parts directly, no CoT wrapper
- if (!hasReasoning) {
- return entries.map(({ part, index: partIndex }) =>
- renderToolInline(part, partIndex)
- )
- }
+ const hasTools = entries.some((e) => e.part.type.startsWith('tool-'))
const lastEntryIndex = entries[entries.length - 1].index
const groupIsStreaming =
isStreaming && lastEntryIndex === message.parts.length - 1
+ // Keep the trace expanded while a tool is still running or awaiting the
+ // user's approval — collapsing it would hide the approval controls.
+ const keepOpen = hasPendingToolCall || awaitingApproval
+
+ // While streaming, surface only the latest step (current reasoning
+ // paragraph or tool call) so each step replaces the previous one rather
+ // than the whole trace scrolling by. The full trace renders once done.
+ const isMeaningfulEntry = ({ part }: PartEntry) => {
+ if (part.type === CONTENT_TYPE.REASONING || part.type === CONTENT_TYPE.TEXT) {
+ return Boolean(part.text && part.text.trim())
+ }
+ return part.type.startsWith('tool-') && 'state' in part
+ }
+ const meaningful = entries.filter(isMeaningfulEntry)
+ const visibleEntries =
+ groupIsStreaming && meaningful.length > 0
+ ? [meaningful[meaningful.length - 1]]
+ : entries
+
+ // Streaming label reflects the current step, not whether the whole trace
+ // ever used a tool — otherwise it sticks on "Using tools…" once the model
+ // resumes reasoning after a tool call.
+ const currentStepIsTool =
+ meaningful.length > 0 &&
+ meaningful[meaningful.length - 1].part.type.startsWith('tool-')
+
return (
<ChainOfThought
key={groupKey}
className="w-full text-muted-foreground"
isStreaming={groupIsStreaming}
- shouldCollapse={hasFollowingContent}
+ shouldCollapse={hasFollowingContent && !keepOpen}
+ forceOpen={keepOpen}
defaultOpen={true}
>
- <ChainOfThoughtHeader />
+ <ChainOfThoughtHeader
+ streamingLabel={currentStepIsTool ? 'Using tools...' : 'Reasoning...'}
+ completedVerb={hasTools ? 'Worked' : 'Thought'}
+ />
<ChainOfThoughtContent>
- {entries.map(({ part, index: partIndex }) => {
+ {visibleEntries.map(({ part, index: partIndex }) => {
if (part.type === CONTENT_TYPE.REASONING) {
const isLastMsgPart =
partIndex === message.parts.length - 1
@@ -538,6 +550,24 @@ export const MessageItem = memo(
)
}
+ // Interstitial narration emitted between steps — fold into the trace
+ if (part.type === CONTENT_TYPE.TEXT) {
+ if (!part.text || part.text.trim() === '') return null
+ return (
+ <div
+ key={`${message.id}-it-${partIndex}`}
+ dir="auto"
+ className="select-text whitespace-pre-wrap wrap-break-word text-sm text-main-view-fg/70"
+ >
+ {part.text}
+ </div>
+ )
+ }
+
+ if (part.type === CONTENT_TYPE.FILE) {
+ return renderFilePart(part, partIndex)
+ }
+
// Tool part inside CoT
return renderToolInline(part, partIndex)
})}
@@ -547,37 +577,50 @@ export const MessageItem = memo(
}
const renderedParts = useMemo(() => {
+ const parts = message.parts as any[]
const elements: React.ReactNode[] = []
- let cotBuffer: PartEntry[] = []
- const flushCot = (hasFollowing: boolean) => {
- if (cotBuffer.length === 0) return
- const key = `${message.id}-cot-${cotBuffer[0].index}`
- elements.push(renderCotGroup(cotBuffer, key, hasFollowing))
- cotBuffer = []
+ // Anchor the working trace at the last reasoning/tool part: everything up
+ // to it (reasoning, tools, step-start markers, interstitial narration)
+ // folds into a single collapsible CoT group; only the trailing answer
+ // text/files render in the main message body.
+ let lastCotAnchor = -1
+ for (let i = 0; i < parts.length; i++) {
+ const t = parts[i].type
+ if (t === CONTENT_TYPE.REASONING || t.startsWith('tool-')) {
+ lastCotAnchor = i
+ }
+ }
+
+ if (lastCotAnchor >= 0) {
+ const cotEntries: PartEntry[] = []
+ for (let i = 0; i <= lastCotAnchor; i++) {
+ cotEntries.push({ part: parts[i], index: i })
+ }
+ elements.push(
+ renderCotGroup(
+ cotEntries,
+ `${message.id}-cot`,
+ lastCotAnchor < parts.length - 1
+ )
+ )
}
- for (let i = 0; i < message.parts.length; i++) {
- const part = message.parts[i] as any
- if (isCotPart(part)) {
- cotBuffer.push({ part, index: i })
- } else {
- flushCot(true) // text/file follows → collapse the CoT
- switch (part.type) {
- case CONTENT_TYPE.TEXT:
- elements.push(
- renderTextPart(part as { type: 'text'; text: string }, i)
- )
- break
- case CONTENT_TYPE.FILE:
- elements.push(renderFilePart(part as any, i))
- break
- default:
- break
- }
+ for (let i = lastCotAnchor + 1; i < parts.length; i++) {
+ const part = parts[i]
+ switch (part.type) {
+ case CONTENT_TYPE.TEXT:
+ elements.push(
+ renderTextPart(part as { type: 'text'; text: string }, i)
+ )
+ break
+ case CONTENT_TYPE.FILE:
+ elements.push(renderFilePart(part as any, i))
+ break
+ default:
+ break
}
}
- flushCot(false) // end of message, no following content → keep open
return elements
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -625,7 +668,7 @@ export const MessageItem = memo(
message.role === 'assistant' &&
!awaitingApproval &&
(hasPendingToolCall || status === CHAT_STATUS.SUBMITTED) && (
- <PromptProgress />
+ <PromptProgress hideIdle={hasPendingToolCall} />
)}
{typeof messageError === 'string' && messageError.length > 0 && (
diff --git a/web-app/src/containers/__tests__/MessageItem.test.tsx b/web-app/src/containers/__tests__/MessageItem.test.tsx
index 793982a4d..288b0f09a 100644
--- a/web-app/src/containers/__tests__/MessageItem.test.tsx
+++ b/web-app/src/containers/__tests__/MessageItem.test.tsx
@@ -315,7 +315,7 @@ describe('MessageItem', () => {
expect(screen.getByText('thinking...')).toBeInTheDocument()
})
- it('renders inline tool part (no reasoning → no CoT wrapper)', () => {
+ it('folds a tool part into the CoT working trace', () => {
render(
<MessageItem
message={
@@ -333,7 +333,7 @@ describe('MessageItem', () => {
)
expect(screen.getByTestId('tool')).toBeInTheDocument()
expect(screen.getByTestId('tool-header')).toHaveTextContent('search')
- expect(screen.queryByTestId('cot')).not.toBeInTheDocument()
+ expect(screen.getByTestId('cot')).toBeInTheDocument()
})
it('renders tool error when state is output-error', () => {