File size: 7,828 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import type React from 'react';
import { Box, Text } from 'ink';
import { CoreToolCallStatus } from '@google/gemini-cli-core';
import type {
  HistoryItem,
  HistoryItemWithoutId,
  HistoryItemToolDisplayGroup,
  ToolDisplayItem,
} from '../../types.js';
import { theme } from '../../semantic-colors.js';
import { ToolStatusIndicator } from './ToolShared.js';
import { useSettings } from '../../contexts/SettingsContext.js';

interface ToolGroupDisplayProps {
  item: HistoryItem | HistoryItemWithoutId;
  isToolGroupBoundary?: boolean;
}

export const ToolGroupDisplay: React.FC<ToolGroupDisplayProps> = ({
  item,
  isToolGroupBoundary,
}) => {
  const settings = useSettings();
  const isCompactModeEnabled = settings.merged.ui?.compactToolOutput === true;

  if (item.type !== 'tool_display_group') {
    return null;
  }

  const { tools, borderColor, borderDimColor, borderTop, borderBottom } =
    item as HistoryItemToolDisplayGroup;

  const visibleTools = tools.filter(
    (t) => t.status !== CoreToolCallStatus.AwaitingApproval,
  );

  const noticeTools = visibleTools.filter((t) => t.format === 'notice');
  const otherTools = visibleTools.filter(
    (t) => t.format !== 'notice' && t.format !== 'hidden',
  );

  const hasOtherTools = otherTools.length > 0;
  const isClosingSlice = tools.length === 0 && borderBottom;

  // If no tools are visible and it's not an explicit closing slice, hide the group
  if (visibleTools.length === 0 && !isClosingSlice) {
    return null;
  }

  // Standard view behavior: If compact mode is enabled, non-notice tools
  // are typically rendered without an outer box.
  const shouldShowBox =
    (hasOtherTools || isClosingSlice) && !isCompactModeEnabled;

  const boxBorderTop = borderTop || noticeTools.length > 0;

  return (
    <Box flexDirection="column">
      {noticeTools.map((tool, index) => {
        const isFirstInGroup = index === 0 && borderTop;
        const isLastElementInGroup =
          index === noticeTools.length - 1 && !shouldShowBox && borderBottom;

        return (
          <Box
            key={`notice-${index}`}
            marginTop={isFirstInGroup ? 1 : index > 0 ? 1 : 0}
            marginBottom={isLastElementInGroup ? 1 : 0}
          >
            <ToolDisplayMessage tool={tool} />
          </Box>
        );
      })}
      {shouldShowBox ? (
        <Box
          flexDirection="column"
          borderStyle="round"
          borderColor={borderColor}
          borderDimColor={borderDimColor}
          borderTop={boxBorderTop}
          borderBottom={borderBottom}
          borderLeft={!isToolGroupBoundary}
          borderRight={!isToolGroupBoundary}
          marginTop={boxBorderTop ? 1 : 0}
          marginBottom={borderBottom ? 1 : 0}
          paddingX={1}
        >
          {otherTools.map((tool, index) => (
            <ToolDisplayMessage key={`tool-${index}`} tool={tool} />
          ))}
        </Box>
      ) : otherTools.length > 0 ? (
        // Compact mode or no tools to box
        <Box
          flexDirection="column"
          marginTop={noticeTools.length > 0 ? 1 : 0}
          marginBottom={borderBottom ? 1 : 0}
        >
          {otherTools.map((tool, index) => (
            <ToolDisplayMessage key={`tool-${index}`} tool={tool} />
          ))}
        </Box>
      ) : null}
    </Box>
  );
};

interface ToolDisplayMessageProps {
  tool: ToolDisplayItem;
}

const ToolDisplayMessage: React.FC<ToolDisplayMessageProps> = ({ tool }) => {
  const settings = useSettings();
  const isCompactModeEnabled = settings.merged.ui?.compactToolOutput === true;

  // Since ToolDisplayItem is ToolDisplay & { status, ... }, we check for identifying properties
  // of ToolDisplay. If name or description is missing and there's no result, it might be "empty".
  if (!tool.name && !tool.description && !tool.result && !tool.resultSummary) {
    return (
      <Box paddingLeft={2}>
        <ToolStatusIndicator
          status={tool.status}
          name={tool.originalRequestName || 'unknown'}
        />
        <Text color={theme.status.error}> Error: Tool display missing</Text>
      </Box>
    );
  }

  const {
    status,
    format: preferredFormat,
    name,
    description,
    resultSummary,
    result,
  } = tool;
  const format = preferredFormat || 'auto';

  if (format === 'hidden') {
    return null;
  }

  if (format === 'notice') {
    // If the name is part of the description (typical for topic updates),
    // suppress the bold name to avoid redundancy and match legacy UI.
    const isRedundant = !!(name && description?.includes(`"${name}"`));

    return (
      <Box paddingLeft={2} flexDirection="column">
        {name && !isRedundant && (
          <Text color={theme.text.primary} bold wrap="truncate-end">
            {name}:
          </Text>
        )}
        {description && (
          <Text color={theme.text.secondary} wrap="wrap">
            {description}
          </Text>
        )}
      </Box>
    );
  }

  const isCompact =
    format === 'compact' || (format === 'auto' && isCompactModeEnabled);

  if (isCompact) {
    return (
      <Box paddingLeft={2} flexDirection="row" flexWrap="wrap">
        <ToolStatusIndicator
          status={status}
          name={name || tool.originalRequestName || ''}
        />
        <Text bold color={theme.text.primary}>
          {' '}
          {name || tool.originalRequestName}{' '}
        </Text>
        {description && <Text color={theme.text.secondary}>{description}</Text>}
        {resultSummary && (
          <Text color={theme.text.accent}>
            {' '}
            → {resultSummary.replace(/\n/g, ' ')}
          </Text>
        )}
      </Box>
    );
  }

  // Box format (full)
  return (
    <Box flexDirection="column" paddingLeft={0}>
      <Box flexDirection="row" paddingLeft={1}>
        <ToolStatusIndicator
          status={status}
          name={name || tool.originalRequestName || ''}
        />
        <Text bold color={theme.text.primary}>
          {' '}
          {name || tool.originalRequestName}{' '}
        </Text>
        {description && <Text color={theme.text.secondary}>{description}</Text>}
      </Box>
      {resultSummary && !result && (
        <Box paddingLeft={1} marginTop={1}>
          <Text color={theme.text.primary}>{resultSummary}</Text>
        </Box>
      )}
      {result && (
        <Box paddingLeft={1} marginTop={1}>
          <ToolResultDisplayContent content={result} summary={resultSummary} />
        </Box>
      )}
    </Box>
  );
};

interface ToolResultDisplayContentProps {
  content: ToolDisplayItem['result'];
  summary?: string | null;
}

const ToolResultDisplayContent: React.FC<ToolResultDisplayContentProps> = ({
  content,
  summary,
}) => {
  if (!content) return null;

  switch (content.type) {
    case 'text':
      return (
        <Box flexDirection="column">
          <Text color={theme.text.primary}>{content.text}</Text>
          {summary && (
            <Box marginTop={0}>
              <Text color={theme.text.primary}>{summary}</Text>
            </Box>
          )}
        </Box>
      );
    case 'diff':
      // Simplified diff display for now
      return (
        <Box flexDirection="column">
          {summary && <Text color={theme.text.primary}>{summary}</Text>}
          <Text color={theme.text.secondary}>
            {`[Diff Display: ${content.beforeText.length} -> ${content.afterText.length} chars]`}
          </Text>
        </Box>
      );
    case 'terminal':
      return <Text color={theme.text.secondary}>[Terminal Output]</Text>;
    case 'agent':
      return (
        <Text color={theme.text.secondary}>[Subagent: {content.threadId}]</Text>
      );
    default:
      return null;
  }
};