-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(ui): add thinking ui to mothership #4254
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
+520
−41
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e9b5ce
feat(ui): Add thinking ui
TheodoreSpeaks d57d5b4
fix tests
TheodoreSpeaks 9d35b0f
Remove duplicate helper for block timing
TheodoreSpeaks c88cb83
fix lint
TheodoreSpeaks 03d40db
fix endedAt timestamp bug
TheodoreSpeaks 52034d0
fix stuck subagent thinking
TheodoreSpeaks 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
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
1 change: 1 addition & 0 deletions
1
...orkspace/[workspaceId]/home/components/message-content/components/thinking-block/index.ts
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { ThinkingBlock } from './thinking-block' |
123 changes: 123 additions & 0 deletions
123
...workspaceId]/home/components/message-content/components/thinking-block/thinking-block.tsx
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 |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| 'use client' | ||
|
|
||
| import { useEffect, useLayoutEffect, useRef, useState } from 'react' | ||
| import { ChevronDown, Expandable, ExpandableContent } from '@/components/emcn' | ||
| import { BrainIcon } from '@/components/icons' | ||
| import { cn } from '@/lib/core/utils/cn' | ||
|
|
||
| interface ThinkingBlockProps { | ||
| content: string | ||
| isActive: boolean | ||
| isStreaming?: boolean | ||
| startedAt?: number | ||
| endedAt?: number | ||
| } | ||
|
|
||
| const MIN_VISIBLE_THINKING_MS = 3000 | ||
|
|
||
| export function ThinkingBlock({ | ||
| content, | ||
| isActive, | ||
| isStreaming = false, | ||
| startedAt, | ||
| endedAt, | ||
| }: ThinkingBlockProps) { | ||
| // Start collapsed so the `Expandable` plays its height-open animation | ||
| // when `expanded` flips to true below — otherwise the panel mounts | ||
| // already-open and jumps up with its full content in one frame. | ||
| const [expanded, setExpanded] = useState(false) | ||
| const panelRef = useRef<HTMLDivElement>(null) | ||
| const wasActiveRef = useRef<boolean | null>(null) | ||
| // Suppress active thinking until it exceeds MIN_VISIBLE_THINKING_MS. | ||
| // Completed-<=threshold is filtered upstream in message-content, so if | ||
| // we're mounted with isActive=false we've already passed that gate. | ||
| const [thresholdReached, setThresholdReached] = useState(() => { | ||
| if (!isActive || startedAt === undefined) return true | ||
| return Date.now() - startedAt > MIN_VISIBLE_THINKING_MS | ||
| }) | ||
|
|
||
| useEffect(() => { | ||
| if (thresholdReached) return | ||
| if (!isActive || startedAt === undefined) { | ||
| setThresholdReached(true) | ||
| return | ||
| } | ||
| const remainingMs = Math.max(0, MIN_VISIBLE_THINKING_MS - (Date.now() - startedAt)) | ||
| const id = window.setTimeout(() => setThresholdReached(true), remainingMs + 50) | ||
| return () => window.clearTimeout(id) | ||
| }, [isActive, startedAt, thresholdReached]) | ||
|
|
||
| useEffect(() => { | ||
| // Wait until the threshold has actually been reached — otherwise this | ||
| // effect fires during the 3-second hidden period (while the component | ||
| // returns null) and sets `expanded` to true before the panel is even | ||
| // rendered, so the Collapsible mounts already-open with no animation. | ||
| if (!thresholdReached) return | ||
| if (wasActiveRef.current === isActive) return | ||
| // On first run (wasActiveRef === null): open if the stream is live — | ||
| // even when thinking itself has already ended — so a mid-stream refresh | ||
| // shows the thinking panel open while the rest of the response is still | ||
| // being generated. Subsequent runs only react to the isActive transition | ||
| // (auto-collapse when thinking ends). | ||
| const isFirstRun = wasActiveRef.current === null | ||
| wasActiveRef.current = isActive | ||
| const target = isFirstRun ? isActive || isStreaming : isActive | ||
| // Defer to the next frame so Radix Collapsible paints the closed state | ||
| // first, then sees the transition to open. Without this, React can batch | ||
| // the mount + flip into a single commit and the animation never plays. | ||
| const id = window.requestAnimationFrame(() => setExpanded(target)) | ||
| return () => window.cancelAnimationFrame(id) | ||
| }, [isActive, isStreaming, thresholdReached]) | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (!isActive || !expanded) return | ||
| const el = panelRef.current | ||
| if (!el) return | ||
| el.scrollTop = el.scrollHeight | ||
| }, [content, isActive, expanded]) | ||
|
|
||
| if (!thresholdReached) return null | ||
|
|
||
| const elapsedMs = | ||
| startedAt !== undefined && endedAt !== undefined && endedAt >= startedAt | ||
| ? endedAt - startedAt | ||
| : undefined | ||
| const elapsedSeconds = | ||
| elapsedMs !== undefined ? Math.max(1, Math.round(elapsedMs / 1000)) : undefined | ||
| const label = isActive | ||
| ? 'Thinking' | ||
| : elapsedSeconds !== undefined | ||
| ? `Thought for ${elapsedSeconds}s` | ||
| : 'Thought' | ||
|
|
||
| return ( | ||
| <div className='flex flex-col gap-1.5'> | ||
| <button | ||
| type='button' | ||
| onClick={() => setExpanded((prev) => !prev)} | ||
| className='flex cursor-pointer items-center gap-2' | ||
| > | ||
| <div className='flex h-[16px] w-[16px] flex-shrink-0 items-center justify-center'> | ||
| <BrainIcon className='h-[14px] w-[14px] text-[var(--text-icon)]' /> | ||
| </div> | ||
| <span className='font-base text-[var(--text-body)] text-sm'>{label}</span> | ||
| <ChevronDown | ||
| className={cn( | ||
| 'h-[7px] w-[9px] text-[var(--text-icon)] transition-transform duration-150', | ||
| !expanded && '-rotate-90' | ||
| )} | ||
| /> | ||
| </button> | ||
|
|
||
| <Expandable expanded={expanded}> | ||
| <ExpandableContent> | ||
| <div ref={panelRef} className='max-h-[110px] overflow-y-scroll pt-0.5 pr-2 pl-6'> | ||
| <div className='whitespace-pre-wrap break-words font-base text-[13px] text-[var(--text-secondary)] leading-[18px] opacity-60'> | ||
| {content} | ||
|
TheodoreSpeaks marked this conversation as resolved.
|
||
| </div> | ||
| </div> | ||
| </ExpandableContent> | ||
| </Expandable> | ||
| </div> | ||
| ) | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.