| 1 | import { useEffect, useRef, useState } from "react"; |
| 2 | import { LLMInteraction } from "../../hooks/useLLMLog"; |
| 3 | import useLLMSummary from "../../hooks/useLLMSummary"; |
| 4 | import End from "./End"; |
| 5 | import ResultGroup from "./ResultGroup"; |
| 6 | import Start from "./Start"; |
| 7 | import StatusIcon from "./StatusIcon"; |
| 8 | |
| 9 | export interface DetailsProps { |
| 10 | interaction: LLMInteraction; |
| 11 | } |
| 12 | |
| 13 | function renderCell(children: React.ReactNode) { |
| 14 | return ( |
| 15 | <div className="border-0 border-r-2 border-solid border-[color:var(--vscode-panel-border)] pl-2 pr-2 text-sm"> |
| 16 | {children} |
| 17 | </div> |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * A cell of statistics at the top of the details view |
| 23 | */ |
| 24 | function Cell({ |
| 25 | label, |
| 26 | value, |
| 27 | format, |
| 28 | }: { |
| 29 | label: string; |
| 30 | value: any; |
| 31 | format?: (value: any) => string; |
| 32 | }) { |
| 33 | return renderCell( |
| 34 | value != undefined ? `${label}: ${format ? format(value) : value}` : "", |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * A cell of statistics at the top of the details view with custom content |
| 40 | */ |
| 41 | function CustomCell({ children }: { children: React.ReactNode }) { |
| 42 | return renderCell(children); |
| 43 | } |
| 44 | |
| 45 | function formatSeconds(milliseconds: number) { |
| 46 | return (milliseconds / 1000).toFixed(2) + "s"; |
| 47 | } |
| 48 | |
| 49 | export default function Details({ interaction }: DetailsProps) { |
| 50 | const scrollTop = useRef<HTMLDivElement>(null); |
| 51 | const lastResult = useRef<any>(null); |
| 52 | const summary = useLLMSummary(interaction); |
| 53 | const [isAtBottom, setIsAtBottom] = useState(true); |
| 54 | |
| 55 | // Handler to check if user is at the bottom |
| 56 | function handleScroll() { |
| 57 | const elt = scrollTop.current; |
| 58 | if (!elt) { |
| 59 | return; |
| 60 | } |
| 61 | // Allow a small threshold for floating point errors |
| 62 | const threshold = 5; |
| 63 | setIsAtBottom( |
| 64 | elt.scrollHeight - elt.scrollTop - elt.clientHeight < threshold, |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | useEffect(() => { |
| 69 | if (interaction.end) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | const last = interaction.results[interaction.results.length - 1]; |
| 74 | if (last != lastResult.current) { |
| 75 | lastResult.current = last; |
| 76 | if (isAtBottom) { |
| 77 | const lastChild = scrollTop.current?.lastChild; |
| 78 | if (lastChild) { |
| 79 | (lastChild as HTMLElement).scrollIntoView({ |
| 80 | behavior: "auto", |
| 81 | block: "end", |
| 82 | }); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | }, [interaction, isAtBottom]); |
| 87 | |
| 88 | return ( |
| 89 | <div className="m-0 flex min-w-0 flex-1 shrink grow flex-col"> |
| 90 | <div className="shrink-0 text-base"> |
| 91 | <div className="columns-3 gap-0 border-0 border-b-2 border-solid border-[color:var(--vscode-panel-border)] p-0"> |
| 92 | <Cell label="Type" value={summary.type}></Cell> |
| 93 | <CustomCell> |
| 94 | Result: <StatusIcon interaction={interaction}></StatusIcon> |
| 95 | {summary.result} |
| 96 | </CustomCell> |
| 97 | </div> |
| 98 | <div className="columns-3 gap-0 border-0 border-b-2 border-solid border-[color:var(--vscode-panel-border)] p-0"> |
| 99 | <Cell label="Prompt Tokens" value={summary.promptTokens}></Cell> |
| 100 | <Cell label="Generated Tokens" value={summary.generatedTokens}></Cell> |
| 101 | <Cell label="ThinkingTokens" value={summary.thinkingTokens}></Cell> |
| 102 | </div> |
| 103 | <div className="columns-3 gap-0 border-0 border-b-2 border-solid border-[color:var(--vscode-panel-border)] p-0"> |
| 104 | <Cell |
| 105 | label="Total Time" |
| 106 | value={summary.totalTime} |
| 107 | format={formatSeconds} |
| 108 | ></Cell> |
| 109 | <Cell |
| 110 | label="To First Token" |
| 111 | value={summary.toFirstToken} |
| 112 | format={formatSeconds} |
| 113 | ></Cell> |
| 114 | <Cell |
| 115 | label="Tokens/s" |
| 116 | value={summary.tokensPerSecond} |
| 117 | format={(v: number) => v.toFixed(1)} |
| 118 | ></Cell> |
| 119 | </div> |
| 120 | {/* {summary.costBreakdown && ( |
| 121 | <pre className="whitespace-pre-wrap px-2 text-sm"> |
| 122 | {summary.costBreakdown.breakdown} |
| 123 | </pre> |
| 124 | )} */} |
| 125 | </div> |
| 126 | <div |
| 127 | ref={scrollTop} |
| 128 | className="grow overflow-auto" |
| 129 | onScroll={handleScroll} |
| 130 | > |
| 131 | {interaction.start ? <Start item={interaction.start}></Start> : ""} |
| 132 | <div className="whitespace-pre-wrap p-2"> |
| 133 | {interaction.results.map((group, i) => { |
| 134 | return <ResultGroup key={i} group={group}></ResultGroup>; |
| 135 | })} |
| 136 | </div> |
| 137 | {interaction.end ? <End item={interaction.end}></End> : ""} |
| 138 | </div> |
| 139 | </div> |
| 140 | ); |
| 141 | } |