Elffuss BraidHub

kikocis/continue public

8cf316c34504 · 4.6 KiBRawPermalink
1// src/components/ThinkingBlockPeek.tsx
2import { ChevronDownIcon } from "@heroicons/react/24/outline";
3import { ChevronUpIcon } from "@heroicons/react/24/solid";
4import { ChatHistoryItem } from "core";
5import { useEffect, useState } from "react";
6import styled from "styled-components";
7import { defaultBorderRadius, lightGray, vscBackground } from "../..";
8import { getFontSize } from "../../../util";
9import StyledMarkdownPreview from "../../StyledMarkdownPreview";
10
11const SpoilerButton = styled.div`
12 background-color: ${vscBackground};
13 width: fit-content;
14 margin: 8px 6px 0px 2px;
15 font-size: ${getFontSize() - 2}px;
16 border: 0.5px solid ${lightGray};
17 border-radius: ${defaultBorderRadius};
18 padding: 4px 8px;
19 color: ${lightGray};
20 cursor: pointer;
21 box-shadow:
22 0 4px 6px rgba(0, 0, 0, 0.1),
23 0 1px 3px rgba(0, 0, 0, 0.08);
24 transition: box-shadow 0.3s ease;
25
26 &:hover {
27 box-shadow:
28 0 6px 8px rgba(0, 0, 0, 0.15),
29 0 3px 6px rgba(0, 0, 0, 0.1);
30 }
31`;
32
33const ButtonContent = styled.div`
34 display: flex;
35 align-items: center;
36 gap: 6px;
37`;
38
39export const ThinkingText = styled.span`
40 position: relative;
41 padding-right: 12px;
42
43 &:after {
44 content: "...";
45 position: absolute;
46 animation: ellipsis 1s steps(4, end) infinite;
47 width: 0px;
48 display: inline-block;
49 overflow: hidden;
50 }
51
52 @keyframes ellipsis {
53 0%,
54 100% {
55 width: 0px;
56 }
57 33% {
58 width: 8px;
59 }
60 66% {
61 width: 16px;
62 }
63 90% {
64 width: 24px;
65 }
66 }
67`;
68
69const MarkdownWrapper = styled.div`
70 & > div > *:first-child {
71 margin-top: 0 !important;
72 }
73`;
74
75interface ThinkingBlockPeekProps {
76 content: string;
77 redactedThinking?: string;
78 index: number;
79 prevItem: ChatHistoryItem | null;
80 inProgress?: boolean;
81 signature?: string;
82 tokens?: number;
83}
84
85function ThinkingBlockPeek({
86 content,
87 redactedThinking,
88 index,
89 prevItem,
90 inProgress,
91 signature,
92 tokens,
93}: ThinkingBlockPeekProps) {
94 const [open, setOpen] = useState(false);
95 const [startTime, setStartTime] = useState<number | null>(null);
96 const [elapsedTime, setElapsedTime] = useState<string>("");
97
98 const duplicateRedactedThinkingBlock =
99 prevItem &&
100 prevItem.message.role === "thinking" &&
101 redactedThinking &&
102 prevItem.message.redactedThinking;
103
104 useEffect(() => {
105 if (inProgress) {
106 setStartTime(Date.now());
107 setElapsedTime("");
108 } else if (startTime) {
109 const endTime = Date.now();
110 const diff = endTime - startTime;
111 const diffString = `${(diff / 1000).toFixed(1)}s`;
112 setElapsedTime(diffString);
113 }
114 }, [inProgress]);
115
116 return duplicateRedactedThinkingBlock ? null : (
117 <div className="thread-message">
118 <div className="" style={{ backgroundColor: vscBackground }}>
119 <div
120 className="flex items-center justify-start pl-2 text-xs text-gray-300"
121 data-testid="thinking-block-peek"
122 >
123 <SpoilerButton onClick={() => setOpen((prev) => !prev)}>
124 <ButtonContent>
125 {inProgress ? (
126 <ThinkingText>
127 {redactedThinking ? "Redacted Thinking" : "Thinking"}
128 </ThinkingText>
129 ) : redactedThinking ? (
130 "Redacted Thinking"
131 ) : (
132 "Thought" +
133 (elapsedTime ? ` for ${elapsedTime}` : "") +
134 (tokens ? ` (${tokens} tokens)` : "")
135 )}
136 {open ? (
137 <ChevronUpIcon className="h-3 w-3" />
138 ) : (
139 <ChevronDownIcon className="h-3 w-3" />
140 )}
141 </ButtonContent>
142 </SpoilerButton>
143 </div>
144
145 <div
146 className={`ml-2 mt-2 overflow-y-auto transition-none duration-300 ease-in-out ${
147 open ? "mb-2 mt-5 opacity-100" : "max-h-0 border-0 opacity-0"
148 }`}
149 style={{
150 borderLeft:
151 open && !redactedThinking
152 ? "2px solid var(--vscode-input-border, #606060)"
153 : "none",
154 }}
155 >
156 {redactedThinking ? (
157 <div className="text-description-muted pl-4 text-xs">
158 Thinking content redacted due to safety reasons.
159 </div>
160 ) : (
161 <>
162 <MarkdownWrapper className="-mt-1 px-0 pl-1">
163 <StyledMarkdownPreview
164 isRenderingInStepContainer
165 source={content}
166 itemIndex={index}
167 />
168 </MarkdownWrapper>
169 </>
170 )}
171 </div>
172 </div>
173 </div>
174 );
175}
176
177export default ThinkingBlockPeek;