forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
UI: Render reasoning as plain italic (match <thinking>) #7752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ab958bf
feat(ui): render reasoning as plain italic text to match <thinking> s…
roomote-agent c557a2f
feat(chat): add Reasoning heading and persistent timer; persist timin…
hannesrudolph 0acab3b
ui(chat): show reasoning timer as (⟲ Ns) beside Thinking heading
hannesrudolph 8d8111f
ui(chat): refine reasoning timer — remove brackets, match heading fon…
hannesrudolph ea322fd
fix(ui/chat): align 'Thinking' header with Task Completed; restore bo…
hannesrudolph a84d41b
refactor(chat): reasoning UI tidy — utility classes, mb-2.5, safer ef…
hannesrudolph a34f392
fix: prevent memory leak in ReasoningBlock timer cleanup
daniel-lxs 1be489b
refactor: simplify ReasoningBlock by removing timer persistence
daniel-lxs d377e45
fix: add right padding to ReasoningBlock header for proper alignment
daniel-lxs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,57 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react" | ||
import { CaretDownIcon, CaretUpIcon, CounterClockwiseClockIcon } from "@radix-ui/react-icons" | ||
import React, { useEffect, useRef, useState } from "react" | ||
import { useTranslation } from "react-i18next" | ||
|
||
import MarkdownBlock from "../common/MarkdownBlock" | ||
import { useMount } from "react-use" | ||
import { Clock, Lightbulb } from "lucide-react" | ||
|
||
interface ReasoningBlockProps { | ||
content: string | ||
elapsed?: number | ||
isCollapsed?: boolean | ||
onToggleCollapse?: () => void | ||
ts: number | ||
isStreaming: boolean | ||
isLast: boolean | ||
metadata?: any | ||
} | ||
|
||
export const ReasoningBlock = ({ content, elapsed, isCollapsed = false, onToggleCollapse }: ReasoningBlockProps) => { | ||
const contentRef = useRef<HTMLDivElement>(null) | ||
const elapsedRef = useRef<number>(0) | ||
const { t } = useTranslation("chat") | ||
const [thought, setThought] = useState<string>() | ||
const [prevThought, setPrevThought] = useState<string>(t("chat:reasoning.thinking")) | ||
const [isTransitioning, setIsTransitioning] = useState<boolean>(false) | ||
const cursorRef = useRef<number>(0) | ||
const queueRef = useRef<string[]>([]) | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice documentation! Could we expand it slightly to explain the design rationale? For example: |
||
* Render reasoning with a heading and a simple timer. | ||
* - Heading uses i18n key chat:reasoning.thinking | ||
* - Timer runs while reasoning is active (no persistence) | ||
*/ | ||
export const ReasoningBlock = ({ content, isStreaming, isLast }: ReasoningBlockProps) => { | ||
const { t } = useTranslation() | ||
|
||
useEffect(() => { | ||
if (contentRef.current && !isCollapsed) { | ||
contentRef.current.scrollTop = contentRef.current.scrollHeight | ||
} | ||
}, [content, isCollapsed]) | ||
|
||
useEffect(() => { | ||
if (elapsed) { | ||
elapsedRef.current = elapsed | ||
} | ||
}, [elapsed]) | ||
|
||
// Process the transition queue. | ||
const processNextTransition = useCallback(() => { | ||
const nextThought = queueRef.current.pop() | ||
queueRef.current = [] | ||
|
||
if (nextThought) { | ||
setIsTransitioning(true) | ||
} | ||
|
||
setTimeout(() => { | ||
if (nextThought) { | ||
setPrevThought(nextThought) | ||
setIsTransitioning(false) | ||
} | ||
|
||
setTimeout(() => processNextTransition(), 500) | ||
}, 200) | ||
}, []) | ||
|
||
useMount(() => { | ||
processNextTransition() | ||
}) | ||
const startTimeRef = useRef<number>(Date.now()) | ||
const [elapsed, setElapsed] = useState<number>(0) | ||
|
||
// Simple timer that runs while streaming | ||
useEffect(() => { | ||
if (content.length - cursorRef.current > 160) { | ||
setThought("... " + content.slice(cursorRef.current)) | ||
cursorRef.current = content.length | ||
if (isLast && isStreaming) { | ||
const tick = () => setElapsed(Date.now() - startTimeRef.current) | ||
tick() | ||
const id = setInterval(tick, 1000) | ||
return () => clearInterval(id) | ||
} | ||
}, [content]) | ||
}, [isLast, isStreaming]) | ||
|
||
useEffect(() => { | ||
if (thought && thought !== prevThought) { | ||
queueRef.current.push(thought) | ||
} | ||
}, [thought, prevThought]) | ||
const seconds = Math.floor(elapsed / 1000) | ||
const secondsLabel = t("chat:reasoning.seconds", { count: seconds }) | ||
|
||
return ( | ||
<div className="bg-vscode-editor-background border border-vscode-border rounded-xs overflow-hidden"> | ||
<div | ||
className="flex items-center justify-between gap-1 px-3 py-2 cursor-pointer text-muted-foreground" | ||
onClick={onToggleCollapse}> | ||
<div | ||
className={`truncate flex-1 transition-opacity duration-200 ${isTransitioning ? "opacity-0" : "opacity-100"}`}> | ||
{prevThought} | ||
</div> | ||
<div className="flex flex-row items-center gap-1"> | ||
{elapsedRef.current > 1000 && ( | ||
<> | ||
<CounterClockwiseClockIcon className="scale-80" /> | ||
<div>{t("reasoning.seconds", { count: Math.round(elapsedRef.current / 1000) })}</div> | ||
</> | ||
)} | ||
{isCollapsed ? <CaretDownIcon /> : <CaretUpIcon />} | ||
<div className="py-1"> | ||
<div className="flex items-center justify-between mb-2.5"> | ||
<div className="flex items-center gap-2"> | ||
<Lightbulb className="w-4" /> | ||
<span className="font-bold text-vscode-foreground">{t("chat:reasoning.thinking")}</span> | ||
</div> | ||
{elapsed > 0 && ( | ||
<span className="text-vscode-foreground tabular-nums flex items-center gap-1"> | ||
<Clock className="w-4" /> | ||
{secondsLabel} | ||
</span> | ||
)} | ||
</div> | ||
{!isCollapsed && ( | ||
<div ref={contentRef} className="px-3 max-h-[160px] overflow-y-auto"> | ||
{(content?.trim()?.length ?? 0) > 0 && ( | ||
<div className="px-3 italic text-vscode-descriptionForeground"> | ||
<MarkdownBlock markdown={content} /> | ||
</div> | ||
)} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The state and are no longer used after the simplification. Should we remove this unused import and state declaration?