| 1 | // src/components/ThinkingBlockPeek.tsx |
| 2 | import { ChevronDownIcon } from "@heroicons/react/24/outline"; |
| 3 | import { ChevronUpIcon } from "@heroicons/react/24/solid"; |
| 4 | import { ChatHistoryItem } from "core"; |
| 5 | import { useEffect, useState } from "react"; |
| 6 | import styled from "styled-components"; |
| 7 | |
| 8 | import { vscBackground } from "../.."; |
| 9 | import { AnimatedEllipsis } from "../../AnimatedEllipsis"; |
| 10 | import StyledMarkdownPreview from "../../StyledMarkdownPreview"; |
| 11 | import { ButtonContent, SpoilerButton } from "../../ui/SpoilerButton"; |
| 12 | |
| 13 | const ThinkingTextContainer = styled.span` |
| 14 | display: inline-block; |
| 15 | min-width: fit-content; |
| 16 | |
| 17 | padding-right: 1em; /* Reserve space for the ellipsis animation */ |
| 18 | `; |
| 19 | |
| 20 | const MarkdownWrapper = styled.div` |
| 21 | & > div > *:first-child { |
| 22 | margin-top: 0 !important; |
| 23 | } |
| 24 | `; |
| 25 | |
| 26 | interface ThinkingBlockPeekProps { |
| 27 | content: string; |
| 28 | redactedThinking?: string; |
| 29 | index: number; |
| 30 | prevItem: ChatHistoryItem | null; |
| 31 | inProgress?: boolean; |
| 32 | signature?: string; |
| 33 | tokens?: number; |
| 34 | } |
| 35 | |
| 36 | function ThinkingBlockPeek({ |
| 37 | content, |
| 38 | redactedThinking, |
| 39 | index, |
| 40 | prevItem, |
| 41 | inProgress, |
| 42 | signature, |
| 43 | tokens, |
| 44 | }: ThinkingBlockPeekProps) { |
| 45 | const [open, setOpen] = useState(false); |
| 46 | const [startTime, setStartTime] = useState<number | null>(null); |
| 47 | const [elapsedTime, setElapsedTime] = useState<string>(""); |
| 48 | |
| 49 | const duplicateRedactedThinkingBlock = |
| 50 | prevItem && |
| 51 | prevItem.message.role === "thinking" && |
| 52 | redactedThinking && |
| 53 | prevItem.message.redactedThinking; |
| 54 | |
| 55 | useEffect(() => { |
| 56 | if (inProgress) { |
| 57 | setStartTime(Date.now()); |
| 58 | setElapsedTime(""); |
| 59 | } else if (startTime) { |
| 60 | const endTime = Date.now(); |
| 61 | const diff = endTime - startTime; |
| 62 | const diffString = `${(diff / 1000).toFixed(1)}s`; |
| 63 | setElapsedTime(diffString); |
| 64 | } |
| 65 | }, [inProgress]); |
| 66 | |
| 67 | return duplicateRedactedThinkingBlock ? null : ( |
| 68 | <div className="thread-message"> |
| 69 | <div className="" style={{ backgroundColor: vscBackground }}> |
| 70 | <div |
| 71 | className="flex items-center justify-start pl-2 text-xs text-gray-300" |
| 72 | data-testid="thinking-block-peek" |
| 73 | > |
| 74 | <SpoilerButton onClick={() => setOpen(!open)}> |
| 75 | <ButtonContent> |
| 76 | {inProgress ? ( |
| 77 | <span> |
| 78 | {redactedThinking ? "Redacted Thinking" : "Thinking"} |
| 79 | <AnimatedEllipsis /> |
| 80 | </span> |
| 81 | ) : redactedThinking ? ( |
| 82 | "Redacted Thinking" |
| 83 | ) : ( |
| 84 | "Thought" + |
| 85 | (elapsedTime ? ` for ${elapsedTime}` : "") + |
| 86 | (tokens ? ` (${tokens} tokens)` : "") |
| 87 | )} |
| 88 | {open ? ( |
| 89 | <ChevronUpIcon className="h-3 w-3" /> |
| 90 | ) : ( |
| 91 | <ChevronDownIcon className="h-3 w-3" /> |
| 92 | )} |
| 93 | </ButtonContent> |
| 94 | </SpoilerButton> |
| 95 | </div> |
| 96 | <div |
| 97 | className={`ml-2 mt-2 overflow-y-auto transition-none duration-300 ease-in-out ${ |
| 98 | open ? "mb-2 mt-5 opacity-100" : "max-h-0 border-0 opacity-0" |
| 99 | }`} |
| 100 | style={{ |
| 101 | borderLeft: |
| 102 | open && !redactedThinking |
| 103 | ? "2px solid var(--vscode-input-border, #606060)" |
| 104 | : "none", |
| 105 | }} |
| 106 | > |
| 107 | {redactedThinking ? ( |
| 108 | <div className="text-description-muted pl-4 text-xs"> |
| 109 | Thinking content redacted due to safety reasons. |
| 110 | </div> |
| 111 | ) : ( |
| 112 | <> |
| 113 | <MarkdownWrapper className="-mt-1 px-0 pl-1"> |
| 114 | <StyledMarkdownPreview |
| 115 | isRenderingInStepContainer |
| 116 | source={content} |
| 117 | itemIndex={index} |
| 118 | /> |
| 119 | </MarkdownWrapper> |
| 120 | </> |
| 121 | )} |
| 122 | </div> |
| 123 | </div> |
| 124 | </div> |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | export default ThinkingBlockPeek; |