| 1 | import { ChevronDownIcon, ChevronUpIcon } from "@heroicons/react/24/solid"; |
| 2 | import { ChatHistoryItem } from "core"; |
| 3 | import { stripImages } from "core/util/messageContent"; |
| 4 | import { useEffect, useState } from "react"; |
| 5 | import styled from "styled-components"; |
| 6 | import { |
| 7 | defaultBorderRadius, |
| 8 | lightGray, |
| 9 | vscBackground, |
| 10 | vscQuickInputBackground, |
| 11 | } from ".."; |
| 12 | import { getFontSize } from "../../util"; |
| 13 | import StyledMarkdownPreview from "../StyledMarkdownPreview"; |
| 14 | |
| 15 | interface ReasoningProps { |
| 16 | item: ChatHistoryItem; |
| 17 | index: number; |
| 18 | isLast: boolean; |
| 19 | } |
| 20 | |
| 21 | const SpoilerButton = styled.div` |
| 22 | margin: 8px 6px 16px 6px; |
| 23 | background-color: ${vscBackground}; |
| 24 | width: fit-content; |
| 25 | font-size: ${getFontSize() - 2}px; |
| 26 | border: 0.5px solid ${lightGray}; |
| 27 | border-radius: ${defaultBorderRadius}; |
| 28 | padding: 4px 8px; |
| 29 | color: ${lightGray}; |
| 30 | cursor: pointer; |
| 31 | box-shadow: |
| 32 | 0 4px 6px rgba(0, 0, 0, 0.1), |
| 33 | 0 1px 3px rgba(0, 0, 0, 0.08); |
| 34 | transition: box-shadow 0.3s ease; |
| 35 | |
| 36 | &:hover { |
| 37 | box-shadow: |
| 38 | 0 6px 8px rgba(0, 0, 0, 0.15), |
| 39 | 0 3px 6px rgba(0, 0, 0, 0.1); |
| 40 | } |
| 41 | `; |
| 42 | |
| 43 | const ButtonContent = styled.div` |
| 44 | display: flex; |
| 45 | align-items: center; |
| 46 | gap: 6px; |
| 47 | `; |
| 48 | |
| 49 | const ContentDiv = styled.div<{ fontSize?: number }>` |
| 50 | margin: 4px; |
| 51 | padding: 4px; |
| 52 | padding-bottom: 8px; |
| 53 | font-size: ${getFontSize()}px; |
| 54 | overflow: hidden; |
| 55 | border-left: 4px solid ${lightGray}; |
| 56 | border-radius: ${defaultBorderRadius}; |
| 57 | background-color: ${vscQuickInputBackground}; |
| 58 | `; |
| 59 | |
| 60 | const ThinkingText = styled.span` |
| 61 | position: relative; |
| 62 | padding-right: 12px; |
| 63 | |
| 64 | &:after { |
| 65 | content: "..."; |
| 66 | position: absolute; |
| 67 | animation: ellipsis 1s steps(4, end) infinite; |
| 68 | width: 0px; |
| 69 | display: inline-block; |
| 70 | overflow: hidden; |
| 71 | } |
| 72 | |
| 73 | @keyframes ellipsis { |
| 74 | 0%, |
| 75 | 100% { |
| 76 | width: 0px; |
| 77 | } |
| 78 | 33% { |
| 79 | width: 8px; |
| 80 | } |
| 81 | 66% { |
| 82 | width: 16px; |
| 83 | } |
| 84 | 90% { |
| 85 | width: 24px; |
| 86 | } |
| 87 | } |
| 88 | `; |
| 89 | |
| 90 | export default function Reasoning(props: ReasoningProps) { |
| 91 | const [open, setOpen] = useState(false); |
| 92 | const [reasoningTime, setReasoningTime] = useState(""); |
| 93 | const isThinking = !props.item.reasoning?.endAt; |
| 94 | |
| 95 | useEffect(() => { |
| 96 | if (!props.item.reasoning) return; |
| 97 | if (!props.item.reasoning?.endAt) return; |
| 98 | |
| 99 | const startAt = props.item.reasoning?.startAt || Date.now(); |
| 100 | const endAt = props.item.reasoning?.endAt || Date.now(); |
| 101 | const diff = endAt - startAt; |
| 102 | const diffString = `${(diff / 1000).toFixed(1)}s`; |
| 103 | setReasoningTime(diffString); |
| 104 | }, [props.item.reasoning?.startAt, props.item.reasoning?.endAt]); |
| 105 | |
| 106 | if (!props.item.reasoning?.text) { |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | return ( |
| 111 | <> |
| 112 | <SpoilerButton onClick={() => setOpen(!open)}> |
| 113 | <ButtonContent> |
| 114 | {isThinking ? ( |
| 115 | <ThinkingText>Thinking</ThinkingText> |
| 116 | ) : ( |
| 117 | `Thought for ${reasoningTime}` |
| 118 | )} |
| 119 | {open ? ( |
| 120 | <ChevronUpIcon className="h-3 w-3" /> |
| 121 | ) : ( |
| 122 | <ChevronDownIcon className="h-3 w-3" /> |
| 123 | )} |
| 124 | </ButtonContent> |
| 125 | </SpoilerButton> |
| 126 | {open && ( |
| 127 | <ContentDiv> |
| 128 | <StyledMarkdownPreview |
| 129 | isRenderingInStepContainer |
| 130 | source={stripImages(props.item.reasoning.text)} |
| 131 | itemIndex={props.index} |
| 132 | useParentBackgroundColor |
| 133 | /> |
| 134 | </ContentDiv> |
| 135 | )} |
| 136 | </> |
| 137 | ); |
| 138 | } |