File size: 6,329 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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import type React from 'react';
import { Box, Text } from 'ink';
import { theme } from '../../semantic-colors.js';
import Spinner from 'ink-spinner';
import { MarkdownDisplay } from '../../utils/MarkdownDisplay.js';
import {
  type SubagentProgress,
  type SubagentActivityItem,
  SubagentState,
} from '@google/gemini-cli-core';
import { TOOL_STATUS } from '../../constants.js';
import { STATUS_INDICATOR_WIDTH } from './ToolShared.js';
import { safeJsonToMarkdown } from '@google/gemini-cli-core';

export interface SubagentProgressDisplayProps {
  progress: SubagentProgress;
  terminalWidth: number;
  historyOverrides?: SubagentActivityItem[];
}

export const formatToolArgs = (args?: string): string => {
  if (!args) return '';
  try {
    const parsed: unknown = JSON.parse(args);
    if (typeof parsed !== 'object' || parsed === null) {
      return args;
    }

    if (
      'description' in parsed &&
      typeof parsed.description === 'string' &&
      parsed.description
    ) {
      return parsed.description;
    }
    if ('command' in parsed && typeof parsed.command === 'string')
      return parsed.command;
    if ('file_path' in parsed && typeof parsed.file_path === 'string')
      return parsed.file_path;
    if ('dir_path' in parsed && typeof parsed.dir_path === 'string')
      return parsed.dir_path;
    if ('query' in parsed && typeof parsed.query === 'string')
      return parsed.query;
    if ('url' in parsed && typeof parsed.url === 'string') return parsed.url;
    if ('target' in parsed && typeof parsed.target === 'string')
      return parsed.target;

    return args;
  } catch {
    return args;
  }
};

export const SubagentProgressDisplay: React.FC<
  SubagentProgressDisplayProps
> = ({ progress, terminalWidth, historyOverrides }) => {
  let headerText: string | undefined;
  let headerColor = theme.text.secondary;

  if (progress.state === SubagentState.CANCELLED) {
    headerText = `Subagent ${progress.agentName} was cancelled.`;
    headerColor = theme.status.warning;
  } else if (progress.state === SubagentState.ERROR) {
    headerText = `Subagent ${progress.agentName} failed.`;
    headerColor = theme.status.error;
  } else if (progress.state === SubagentState.COMPLETED) {
    headerText = `Subagent ${progress.agentName} completed.`;
    headerColor = theme.status.success;
  } else {
    headerText = `Running subagent ${progress.agentName}...`;
    headerColor = theme.text.primary;
  }

  return (
    <Box flexDirection="column" paddingY={0}>
      {headerText && (
        <Box marginBottom={1}>
          <Text color={headerColor} italic>
            {headerText}
          </Text>
        </Box>
      )}
      <Box flexDirection="column" marginLeft={0} gap={0}>
        {(historyOverrides ?? progress.recentActivity).map(
          (item: SubagentActivityItem) => {
            if (item.type === 'thought') {
              const isCancellation = item.content === 'Request cancelled.';
              const icon = isCancellation ? 'ℹ ' : '💭';
              const color = isCancellation
                ? theme.status.warning
                : theme.text.secondary;

              return (
                <Box key={item.id} flexDirection="row">
                  <Box minWidth={STATUS_INDICATOR_WIDTH}>
                    <Text color={color}>{icon}</Text>
                  </Box>
                  <Box flexGrow={1}>
                    <Text color={color}>{item.content}</Text>
                  </Box>
                </Box>
              );
            } else if (item.type === 'tool_call') {
              const statusSymbol =
                item.status === SubagentState.RUNNING ? (
                  <Spinner type="dots" />
                ) : item.status === SubagentState.COMPLETED ? (
                  <Text color={theme.status.success}>
                    {TOOL_STATUS.SUCCESS}
                  </Text>
                ) : item.status === SubagentState.CANCELLED ? (
                  <Text color={theme.status.warning} bold>
                    {TOOL_STATUS.CANCELED}
                  </Text>
                ) : (
                  <Text color={theme.status.error}>{TOOL_STATUS.ERROR}</Text>
                );

              const formattedArgs =
                item.description || formatToolArgs(item.args);
              const displayArgs =
                formattedArgs.length > 60
                  ? formattedArgs.slice(0, 60) + '...'
                  : formattedArgs;

              return (
                <Box key={item.id} flexDirection="row">
                  <Box minWidth={STATUS_INDICATOR_WIDTH}>{statusSymbol}</Box>
                  <Box flexDirection="row" flexGrow={1} flexWrap="wrap">
                    <Text
                      bold
                      color={theme.text.primary}
                      strikethrough={item.status === SubagentState.CANCELLED}
                    >
                      {item.displayName || item.content}
                    </Text>
                    {displayArgs && (
                      <Box marginLeft={1}>
                        <Text
                          color={theme.text.secondary}
                          wrap="truncate"
                          strikethrough={
                            item.status === SubagentState.CANCELLED
                          }
                        >
                          {displayArgs}
                        </Text>
                      </Box>
                    )}
                  </Box>
                </Box>
              );
            }
            return null;
          },
        )}
      </Box>

      {progress.result && (
        <Box flexDirection="column" marginTop={1}>
          {progress.terminateReason && progress.terminateReason !== 'GOAL' && (
            <Box marginBottom={1}>
              <Text color={theme.status.warning} bold>
                Agent Finished Early ({progress.terminateReason})
              </Text>
            </Box>
          )}
          <MarkdownDisplay
            text={safeJsonToMarkdown(progress.result)}
            isPending={progress.state !== SubagentState.COMPLETED}
            terminalWidth={terminalWidth}
          />
        </Box>
      )}
    </Box>
  );
};