File size: 3,782 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { CoreToolCallStatus } from '@google/gemini-cli-core';
import { isShellTool } from '../components/messages/ToolShared.js';
import { theme } from '../semantic-colors.js';
import type {
  HistoryItem,
  HistoryItemWithoutId,
  HistoryItemToolGroup,
  IndividualToolCallDisplay,
} from '../types.js';
import type { BackgroundTask } from '../hooks/shellReducer.js';
import type { TrackedToolCall } from '../hooks/useToolScheduler.js';

function isTrackedToolCall(
  tool: IndividualToolCallDisplay | TrackedToolCall,
): tool is TrackedToolCall {
  return 'request' in tool;
}

/**
 * Calculates the border color and dimming state for a tool group message.
 */
export function getToolGroupBorderAppearance(
  item:
    | HistoryItem
    | HistoryItemWithoutId
    | {
        type: 'tool_group';
        tools: Array<IndividualToolCallDisplay | TrackedToolCall>;
      },
  activeShellPtyId: number | null | undefined,
  embeddedShellFocused: boolean | undefined,
  allPendingItems: HistoryItemWithoutId[] = [],
  backgroundTasks: Map<number, BackgroundTask> = new Map(),
): { borderColor: string; borderDimColor: boolean } {
  if (item.type !== 'tool_group') {
    return { borderColor: '', borderDimColor: false };
  }

  // If this item has no tools, it's a closing slice for the current batch.
  // We need to look at the last pending item to determine the batch's appearance.
  const toolsToInspect =
    item.tools.length > 0
      ? item.tools
      : allPendingItems
          .filter(
            (i): i is HistoryItemToolGroup =>
              i !== null &&
              i !== undefined &&
              i.type === 'tool_group' &&
              i.tools.length > 0,
          )
          .slice(-1)
          .flatMap((i) => i.tools);

  const hasPending = toolsToInspect.some((t) => {
    if (isTrackedToolCall(t)) {
      return (
        t.status !== 'success' &&
        t.status !== 'error' &&
        t.status !== 'cancelled'
      );
    } else {
      return (
        t.status !== CoreToolCallStatus.Success &&
        t.status !== CoreToolCallStatus.Error &&
        t.status !== CoreToolCallStatus.Cancelled
      );
    }
  });

  const isEmbeddedShellFocused = toolsToInspect.some((t) => {
    if (isTrackedToolCall(t)) {
      return (
        isShellTool(t.request.name) &&
        t.status === 'executing' &&
        t.pid === activeShellPtyId &&
        !!embeddedShellFocused
      );
    } else {
      return (
        isShellTool(t.name) &&
        t.status === CoreToolCallStatus.Executing &&
        t.ptyId === activeShellPtyId &&
        !!embeddedShellFocused
      );
    }
  });

  const isShellCommand = toolsToInspect.some((t) => {
    if (isTrackedToolCall(t)) {
      return isShellTool(t.request.name);
    } else {
      return isShellTool(t.name);
    }
  });

  // If we have an active PTY that isn't a background shell, then the current
  // pending batch is definitely a shell batch.
  const isCurrentlyInShellTurn =
    !!activeShellPtyId && !backgroundTasks.has(activeShellPtyId);

  const isShell =
    isShellCommand || (item.tools.length === 0 && isCurrentlyInShellTurn);
  const isPending =
    hasPending || (item.tools.length === 0 && isCurrentlyInShellTurn);

  const isEffectivelyFocused =
    isEmbeddedShellFocused ||
    (item.tools.length === 0 &&
      isCurrentlyInShellTurn &&
      !!embeddedShellFocused);

  const borderColor = isEffectivelyFocused
    ? theme.ui.focus
    : isShell && isPending
      ? theme.ui.active
      : isPending
        ? theme.status.warning
        : theme.border.default;

  const borderDimColor = isPending && (!isShell || !isEffectivelyFocused);

  return { borderColor, borderDimColor };
}