summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/components/messages/ToolMessage.tsx
blob: 9c1dd36bfe7be3a4fc5cfaea65fd4f2b91e1a071 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import React from 'react';
import { Box, Text } from 'ink';
import Spinner from 'ink-spinner';
import {
  IndividualToolCallDisplay,
  ToolCallStatus,
  ToolCallConfirmationDetails,
  ToolEditConfirmationDetails,
  ToolExecuteConfirmationDetails,
} from '../../types.js';
import { DiffRenderer } from './DiffRenderer.js';
import { FileDiff, ToolResultDisplay } from '../../../tools/tools.js';

export const ToolMessage: React.FC<IndividualToolCallDisplay> = ({
  callId,
  name,
  description,
  resultDisplay,
  status,
  confirmationDetails,
}) => {
  // Explicitly type the props to help the type checker
  const typedConfirmationDetails = confirmationDetails as
    | ToolCallConfirmationDetails
    | undefined;
  const typedResultDisplay = resultDisplay as ToolResultDisplay | undefined;

  let color = 'gray';
  let prefix = '';
  switch (status) {
    case ToolCallStatus.Pending:
      prefix = 'Pending:';
      break;
    case ToolCallStatus.Invoked:
      prefix = 'Executing:';
      break;
    case ToolCallStatus.Confirming:
      color = 'yellow';
      prefix = 'Confirm:';
      break;
    case ToolCallStatus.Success:
      color = 'green';
      prefix = 'Success:';
      break;
    case ToolCallStatus.Error:
      color = 'red';
      prefix = 'Error:';
      break;
    default:
      // Handle unexpected status if necessary, or just break
      break;
  }

  const title = `${prefix} ${name}`;

  return (
    <Box key={callId} borderStyle="round" paddingX={1} flexDirection="column">
      <Box>
        {status === ToolCallStatus.Invoked && (
          <Box marginRight={1}>
            <Text color="blue">
              <Spinner type="dots" />
            </Text>
          </Box>
        )}
        <Text bold color={color}>
          {title}
        </Text>
        <Text color={color}>
          {status === ToolCallStatus.Error && typedResultDisplay
            ? `: ${typedResultDisplay}`
            : ` - ${description}`}
        </Text>
      </Box>
      {status === ToolCallStatus.Confirming && typedConfirmationDetails && (
        <Box flexDirection="column" marginLeft={2}>
          {/* Display diff for edit/write */}
          {'fileDiff' in typedConfirmationDetails && (
            <DiffRenderer
              diffContent={
                (typedConfirmationDetails as ToolEditConfirmationDetails)
                  .fileDiff
              }
            />
          )}
          {/* Display command for execute */}
          {'command' in typedConfirmationDetails && (
            <Text color="yellow">
              Command:{' '}
              {
                (typedConfirmationDetails as ToolExecuteConfirmationDetails)
                  .command
              }
            </Text>
          )}
          {/* <ConfirmInput onConfirm={handleConfirm} isFocused={isFocused} /> */}
        </Box>
      )}
      {status === ToolCallStatus.Success && typedResultDisplay && (
        <Box flexDirection="column" marginLeft={2}>
          {typeof typedResultDisplay === 'string' ? (
            <Text>{typedResultDisplay}</Text>
          ) : (
            <DiffRenderer
              diffContent={(typedResultDisplay as FileDiff).fileDiff}
            />
          )}
        </Box>
      )}
    </Box>
  );
};