Elffuss BraidHub

kikocis/continue public

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