summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/components/SessionSummaryDisplay.tsx
blob: a009f3d8821e185e8e381df0c285fc8728e813f7 (plain)
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import React from 'react';
import { Box, Text } from 'ink';
import Gradient from 'ink-gradient';
import { Colors } from '../colors.js';
import { formatDuration } from '../utils/formatters.js';
import { useSessionStats } from '../contexts/SessionContext.js';
import { computeSessionStats } from '../utils/computeStats.js';
import { FormattedStats, StatRow, StatsColumn } from './Stats.js';

// --- Prop and Data Structures ---

interface SessionSummaryDisplayProps {
  duration: string;
}

// --- Main Component ---

export const SessionSummaryDisplay: React.FC<SessionSummaryDisplayProps> = ({
  duration,
}) => {
  const { stats } = useSessionStats();
  const { metrics } = stats;
  const computed = computeSessionStats(metrics);

  const cumulativeFormatted: FormattedStats = {
    inputTokens: Object.values(metrics.models).reduce(
      (acc, model) => acc + model.tokens.prompt,
      0,
    ),
    outputTokens: Object.values(metrics.models).reduce(
      (acc, model) => acc + model.tokens.candidates,
      0,
    ),
    toolUseTokens: Object.values(metrics.models).reduce(
      (acc, model) => acc + model.tokens.tool,
      0,
    ),
    thoughtsTokens: Object.values(metrics.models).reduce(
      (acc, model) => acc + model.tokens.thoughts,
      0,
    ),
    cachedTokens: Object.values(metrics.models).reduce(
      (acc, model) => acc + model.tokens.cached,
      0,
    ),
    totalTokens: Object.values(metrics.models).reduce(
      (acc, model) => acc + model.tokens.total,
      0,
    ),
  };

  const totalRequests = Object.values(metrics.models).reduce(
    (acc, model) => acc + model.api.totalRequests,
    0,
  );

  const title = 'Agent powering down. Goodbye!';

  return (
    <Box
      borderStyle="round"
      borderColor="gray"
      flexDirection="column"
      paddingY={1}
      paddingX={2}
      alignSelf="flex-start"
    >
      <Box marginBottom={1} flexDirection="column">
        {Colors.GradientColors ? (
          <Gradient colors={Colors.GradientColors}>
            <Text bold>{title}</Text>
          </Gradient>
        ) : (
          <Text bold>{title}</Text>
        )}
      </Box>

      <Box marginTop={1}>
        <StatsColumn
          title={`Cumulative Stats (${totalRequests} API calls)`}
          stats={cumulativeFormatted}
          isCumulative={true}
        >
          <Box marginTop={1} flexDirection="column">
            <StatRow
              label="Total duration (API)"
              value={formatDuration(computed.totalApiTime)}
            />
            <StatRow
              label="Total duration (Tools)"
              value={formatDuration(computed.totalToolTime)}
            />
            <StatRow label="Total duration (wall)" value={duration} />
          </Box>
        </StatsColumn>
      </Box>
    </Box>
  );
};