forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskHeader.tsx
More file actions
555 lines (518 loc) · 17.8 KB
/
TaskHeader.tsx
File metadata and controls
555 lines (518 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import React, { memo, useEffect, useMemo, useRef, useState } from "react"
import { useWindowSize } from "react-use"
import { VSCodeButton } from "@vscode/webview-ui-toolkit/react"
import prettyBytes from "pretty-bytes"
import { useTranslation } from "react-i18next"
import { vscode } from "@/utils/vscode"
import { formatLargeNumber } from "@/utils/format"
import { calculateTokenDistribution, getMaxTokensForModel } from "@/utils/model-utils"
import { Button } from "@/components/ui"
import { ClineMessage } from "../../../../src/shared/ExtensionMessage"
import { mentionRegexGlobal } from "../../../../src/shared/context-mentions"
import { HistoryItem } from "../../../../src/shared/HistoryItem"
import { useExtensionState } from "../../context/ExtensionStateContext"
import Thumbnails from "../common/Thumbnails"
import { normalizeApiConfiguration } from "../settings/ApiOptions"
import { DeleteTaskDialog } from "../history/DeleteTaskDialog"
import { vscBadgeBackground, vscEditorBackground, vscInputBackground } from "../ui"
import { usePearAiModels } from "@/hooks/usePearAiModels"
interface TaskHeaderProps {
task: ClineMessage
tokensIn: number
tokensOut: number
doesModelSupportPromptCache: boolean
cacheWrites?: number
cacheReads?: number
totalCost: number
contextTokens: number
onClose: () => void
}
const TaskHeader: React.FC<TaskHeaderProps> = ({
task,
tokensIn,
tokensOut,
doesModelSupportPromptCache,
cacheWrites,
cacheReads,
totalCost,
contextTokens,
onClose,
}) => {
const { t } = useTranslation()
const { apiConfiguration, currentTaskItem } = useExtensionState()
const pearAiModels = usePearAiModels(apiConfiguration)
const { selectedModelInfo } = useMemo(() => {
return normalizeApiConfiguration(apiConfiguration, pearAiModels)
}, [apiConfiguration, pearAiModels])
const [isTaskExpanded, setIsTaskExpanded] = useState(true)
const [isTextExpanded, setIsTextExpanded] = useState(false)
const [showSeeMore, setShowSeeMore] = useState(false)
const textContainerRef = useRef<HTMLDivElement>(null)
const textRef = useRef<HTMLDivElement>(null)
const contextWindow = selectedModelInfo?.contextWindow || 1
/*
When dealing with event listeners in React components that depend on state
variables, we face a challenge. We want our listener to always use the most
up-to-date version of a callback function that relies on current state, but
we don't want to constantly add and remove event listeners as that function
updates. This scenario often arises with resize listeners or other window
events. Simply adding the listener in a useEffect with an empty dependency
array risks using stale state, while including the callback in the
dependencies can lead to unnecessary re-registrations of the listener. There
are react hook libraries that provide a elegant solution to this problem by
utilizing the useRef hook to maintain a reference to the latest callback
function without triggering re-renders or effect re-runs. This approach
ensures that our event listener always has access to the most current state
while minimizing performance overhead and potential memory leaks from
multiple listener registrations.
Sources
- https://usehooks-ts.com/react-hook/use-event-listener
- https://streamich.github.io/react-use/?path=/story/sensors-useevent--docs
- https://github.com/streamich/react-use/blob/master/src/useEvent.ts
- https://stackoverflow.com/questions/55565444/how-to-register-event-with-useeffect-hooks
Before:
const updateMaxHeight = useCallback(() => {
if (isExpanded && textContainerRef.current) {
const maxHeight = window.innerHeight * (3 / 5)
textContainerRef.current.style.maxHeight = `${maxHeight}px`
}
}, [isExpanded])
useEffect(() => {
updateMaxHeight()
}, [isExpanded, updateMaxHeight])
useEffect(() => {
window.removeEventListener("resize", updateMaxHeight)
window.addEventListener("resize", updateMaxHeight)
return () => {
window.removeEventListener("resize", updateMaxHeight)
}
}, [updateMaxHeight])
After:
*/
const { height: windowHeight, width: windowWidth } = useWindowSize()
useEffect(() => {
if (isTextExpanded && textContainerRef.current) {
const maxHeight = windowHeight * (1 / 2)
textContainerRef.current.style.maxHeight = `${maxHeight}px`
}
}, [isTextExpanded, windowHeight])
useEffect(() => {
if (textRef.current && textContainerRef.current) {
let textContainerHeight = textContainerRef.current.clientHeight
if (!textContainerHeight) {
textContainerHeight = textContainerRef.current.getBoundingClientRect().height
}
const isOverflowing = textRef.current.scrollHeight > textContainerHeight
// necessary to show see more button again if user resizes window to expand and then back to collapse
if (!isOverflowing) {
setIsTextExpanded(false)
}
setShowSeeMore(isOverflowing)
}
}, [task.text, windowWidth])
const isCostAvailable = useMemo(() => {
return (
apiConfiguration?.apiProvider !== "openai" &&
apiConfiguration?.apiProvider !== "ollama" &&
apiConfiguration?.apiProvider !== "lmstudio" &&
apiConfiguration?.apiProvider !== "gemini"
)
}, [apiConfiguration?.apiProvider])
const shouldShowPromptCacheInfo = doesModelSupportPromptCache && apiConfiguration?.apiProvider !== "openrouter"
return (
<div style={{ padding: "10px 13px 10px 13px" }}>
<div
style={{
backgroundColor: vscEditorBackground,
color: "var(--vscode-badge-foreground)",
borderRadius: "12px",
padding: "12px",
display: "flex",
flexDirection: "column",
gap: 6,
position: "relative",
zIndex: 1,
}}>
<div
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
}}>
<div
style={{
display: "flex",
alignItems: "center",
cursor: "pointer",
marginLeft: -2,
userSelect: "none",
WebkitUserSelect: "none",
MozUserSelect: "none",
msUserSelect: "none",
flexGrow: 1,
minWidth: 0, // This allows the div to shrink below its content size
}}
onClick={() => setIsTaskExpanded(!isTaskExpanded)}>
<div style={{ display: "flex", alignItems: "center", flexShrink: 0 }}>
<span className={`codicon codicon-chevron-${isTaskExpanded ? "down" : "right"}`}></span>
</div>
<div
style={{
marginLeft: 6,
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
flexGrow: 1,
minWidth: 0, // This allows the div to shrink below its content size
}}>
<span style={{ fontWeight: "bold" }}>
{t("chat:task.title")}
{!isTaskExpanded && ":"}
</span>
{!isTaskExpanded && (
<span style={{ marginLeft: 4 }}>{highlightMentions(task.text, false)}</span>
)}
</div>
</div>
{!isTaskExpanded && isCostAvailable && (
<div
style={{
marginLeft: 10,
backgroundColor: "color-mix(in srgb, var(--vscode-badge-foreground) 70%, transparent)",
color: "var(--vscode-badge-background)",
padding: "2px 4px",
borderRadius: "500px",
fontSize: "11px",
fontWeight: 500,
display: "inline-block",
flexShrink: 0,
}}>
${totalCost?.toFixed(4)}
</div>
)}
<VSCodeButton
appearance="icon"
onClick={onClose}
style={{ marginLeft: 6, flexShrink: 0, color: "var(--vscode-badge-foreground)" }}
title={t("chat:task.closeAndStart")}>
<span className="codicon codicon-close"></span>
</VSCodeButton>
</div>
{isTaskExpanded && (
<>
<div
ref={textContainerRef}
style={{
marginTop: -2,
fontSize: "var(--vscode-font-size)",
overflowY: isTextExpanded ? "auto" : "hidden",
wordBreak: "break-word",
overflowWrap: "anywhere",
position: "relative",
}}>
<div
ref={textRef}
style={{
display: "-webkit-box",
WebkitLineClamp: isTextExpanded ? "unset" : 3,
WebkitBoxOrient: "vertical",
overflow: "hidden",
whiteSpace: "pre-wrap",
wordBreak: "break-word",
overflowWrap: "anywhere",
}}>
{highlightMentions(task.text, false)}
</div>
{!isTextExpanded && showSeeMore && (
<div
style={{
position: "absolute",
right: 0,
bottom: 0,
display: "flex",
alignItems: "center",
}}>
<div
style={{
cursor: "pointer",
color: "var(--vscode-foreground)",
fontSize: "11px",
padding: "2px 8px",
backgroundColor: "var(--vscode-badge-background)",
borderRadius: "3px",
border: "1px solid var(--vscode-button-secondaryBackground)",
display: "flex",
alignItems: "center",
height: "18px",
}}
onClick={() => setIsTextExpanded(!isTextExpanded)}>
{t("chat:task.seeMore")}
</div>
</div>
)}
</div>
{isTextExpanded && showSeeMore && (
<div
style={{
cursor: "pointer",
color: "var(--vscode-foreground)",
fontSize: "11px",
marginLeft: "auto",
padding: "2px 8px",
backgroundColor: "var(--vscode-badge-background)",
borderRadius: "3px",
border: "1px solid var(--vscode-button-secondaryBackground)",
display: "inline-flex",
alignItems: "center",
height: "18px",
width: "fit-content",
}}
onClick={() => setIsTextExpanded(!isTextExpanded)}>
{t("chat:task.seeLess")}
</div>
)}
{task.images && task.images.length > 0 && <Thumbnails images={task.images} />}
<div style={{ display: "flex", flexDirection: "column", gap: "4px" }}>
<div className="flex justify-between items-center h-[20px]">
<div style={{ display: "flex", alignItems: "center", gap: "4px", flexWrap: "wrap" }}>
<span style={{ fontWeight: "bold" }}>{t("chat:task.tokens")}</span>
<span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
<i
className="codicon codicon-arrow-up"
style={{ fontSize: "12px", fontWeight: "bold", marginBottom: "-2px" }}
/>
{formatLargeNumber(tokensIn || 0)}
</span>
<span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
<i
className="codicon codicon-arrow-down"
style={{ fontSize: "12px", fontWeight: "bold", marginBottom: "-2px" }}
/>
{formatLargeNumber(tokensOut || 0)}
</span>
</div>
{!isCostAvailable && <TaskActions item={currentTaskItem} />}
</div>
{isTaskExpanded && contextWindow > 0 && (
<div
className={`w-full flex ${windowWidth < 400 ? "flex-col" : "flex-row"} gap-1 h-auto`}>
<ContextWindowProgress
contextWindow={contextWindow}
contextTokens={contextTokens || 0}
maxTokens={getMaxTokensForModel(selectedModelInfo, apiConfiguration)}
/>
</div>
)}
{shouldShowPromptCacheInfo && (cacheReads !== undefined || cacheWrites !== undefined) && (
<div className="flex items-center gap-1 flex-wrap h-[20px]">
<span style={{ fontWeight: "bold" }}>{t("chat:task.cache")}</span>
<span className="flex items-center gap-1">
<i
className="codicon codicon-database"
style={{ fontSize: "12px", fontWeight: "bold" }}
/>
+{formatLargeNumber(cacheWrites || 0)}
</span>
<span className="flex items-center gap-1">
<i
className="codicon codicon-arrow-right"
style={{ fontSize: "12px", fontWeight: "bold" }}
/>
{formatLargeNumber(cacheReads || 0)}
</span>
</div>
)}
{isCostAvailable && (
<div className="flex justify-between items-center h-[20px]">
<div className="flex items-center gap-1">
<span className="font-bold">{t("chat:task.apiCost")}</span>
<span>${totalCost?.toFixed(4)}</span>
</div>
<TaskActions item={currentTaskItem} />
</div>
)}
</div>
</>
)}
</div>
</div>
)
}
export const highlightMentions = (text?: string, withShadow = true) => {
if (!text) return text
const parts = text.split(mentionRegexGlobal)
return parts.map((part, index) => {
if (index % 2 === 0) {
// This is regular text
return part
} else {
// This is a mention
return (
<span
key={index}
className={withShadow ? "mention-context-highlight-with-shadow" : "mention-context-highlight"}
style={{ cursor: "pointer" }}
onClick={() => vscode.postMessage({ type: "openMention", text: part })}>
@{part}
</span>
)
}
})
}
const TaskActions = ({ item }: { item: HistoryItem | undefined }) => {
const [deleteTaskId, setDeleteTaskId] = useState<string | null>(null)
const { t } = useTranslation()
return (
<div className="flex flex-row gap-1">
<Button
variant="ghost"
size="sm"
title={t("chat:task.export")}
onClick={() => vscode.postMessage({ type: "exportCurrentTask" })}>
<span className="codicon codicon-cloud-download" />
</Button>
{!!item?.size && item.size > 0 && (
<>
<Button
variant="ghost"
size="sm"
title={t("chat:task.delete")}
onClick={(e) => {
e.stopPropagation()
if (e.shiftKey) {
vscode.postMessage({ type: "deleteTaskWithId", text: item.id })
} else {
setDeleteTaskId(item.id)
}
}}>
<span className="codicon codicon-trash" />
{prettyBytes(item.size)}
</Button>
{deleteTaskId && (
<DeleteTaskDialog
taskId={deleteTaskId}
onOpenChange={(open) => !open && setDeleteTaskId(null)}
open
/>
)}
</>
)}
</div>
)
}
interface ContextWindowProgressProps {
contextWindow: number
contextTokens: number
maxTokens?: number
}
const ContextWindowProgress = ({ contextWindow, contextTokens, maxTokens }: ContextWindowProgressProps) => {
const { t } = useTranslation()
// Use the shared utility function to calculate all token distribution values
const tokenDistribution = useMemo(
() => calculateTokenDistribution(contextWindow, contextTokens, maxTokens),
[contextWindow, contextTokens, maxTokens],
)
// Destructure the values we need
const { currentPercent, reservedPercent, availableSize, reservedForOutput, availablePercent } = tokenDistribution
// For display purposes
const safeContextWindow = Math.max(0, contextWindow)
const safeContextTokens = Math.max(0, contextTokens)
return (
<>
<div className="flex items-center gap-1 flex-shrink-0">
<span className="font-bold" data-testid="context-window-label">
{t("chat:task.contextWindow")}
</span>
</div>
<div className="flex items-center gap-2 flex-1 whitespace-nowrap px-2">
<div data-testid="context-tokens-count">{formatLargeNumber(safeContextTokens)}</div>
<div className="flex-1 relative">
{/* Invisible overlay for hover area */}
<div
className="absolute w-full cursor-pointer"
style={{
height: "16px",
top: "-7px",
zIndex: 5,
}}
title={t("chat:tokenProgress.availableSpace", { amount: formatLargeNumber(availableSize) })}
data-testid="context-available-space"
/>
{/* Main progress bar container */}
<div className="flex items-center h-1 rounded-[2px] overflow-hidden w-full bg-[color-mix(in_srgb,var(--vscode-badge-foreground)_20%,transparent)]">
{/* Current tokens container */}
<div className="relative h-full" style={{ width: `${currentPercent}%` }}>
{/* Invisible overlay for current tokens section */}
<div
className="absolute cursor-pointer"
style={{
height: "16px",
top: "-7px",
width: "100%",
zIndex: 6,
}}
title={t("chat:tokenProgress.tokensUsed", {
used: formatLargeNumber(safeContextTokens),
total: formatLargeNumber(safeContextWindow),
})}
data-testid="context-tokens-used"
/>
{/* Current tokens used - darkest */}
<div
className="h-full w-full bg-[var(--vscode-badge-foreground)]"
style={{
transition: "width 0.3s ease-out",
}}
/>
</div>
{/* Container for reserved tokens */}
<div className="relative h-full" style={{ width: `${reservedPercent}%` }}>
{/* Invisible overlay for reserved section */}
<div
className="absolute cursor-pointer"
style={{
height: "16px",
top: "-7px",
width: "100%",
zIndex: 6,
}}
title={t("chat:tokenProgress.reservedForResponse", {
amount: formatLargeNumber(reservedForOutput),
})}
data-testid="context-reserved-tokens"
/>
{/* Reserved for output section - medium gray */}
<div
className="h-full w-full bg-[color-mix(in_srgb,var(--vscode-badge-foreground)_30%,transparent)]"
style={{
transition: "width 0.3s ease-out",
}}
/>
</div>
{/* Empty section (if any) */}
{availablePercent > 0 && (
<div className="relative h-full" style={{ width: `${availablePercent}%` }}>
{/* Invisible overlay for available space */}
<div
className="absolute cursor-pointer"
style={{
height: "16px",
top: "-7px",
width: "100%",
zIndex: 6,
}}
title={t("chat:tokenProgress.availableSpace", {
amount: formatLargeNumber(availableSize),
})}
data-testid="context-available-space-section"
/>
</div>
)}
</div>
</div>
<div data-testid="context-window-size">{formatLargeNumber(safeContextWindow)}</div>
</div>
</>
)
}
export default memo(TaskHeader)