File size: 4,923 Bytes
84aa3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  coreEvents,
  CoreEvent,
  type SlashCommandConflictsPayload,
  type SlashCommandConflict,
} from '@google/gemini-cli-core';
import { CommandKind } from '../ui/commands/types.js';

/**
 * Handles slash command conflict events and provides user feedback.
 *
 * This handler batches multiple conflict events into a single notification
 * block per command name to avoid UI clutter during startup or incremental loading.
 */
export class SlashCommandConflictHandler {
  private notifiedConflicts = new Set<string>();
  private pendingConflicts: SlashCommandConflict[] = [];
  private flushTimeout: ReturnType<typeof setTimeout> | null = null;

  constructor() {
    this.handleConflicts = this.handleConflicts.bind(this);
  }

  start() {
    coreEvents.on(CoreEvent.SlashCommandConflicts, this.handleConflicts);
  }

  stop() {
    coreEvents.off(CoreEvent.SlashCommandConflicts, this.handleConflicts);
    if (this.flushTimeout) {
      clearTimeout(this.flushTimeout);
      this.flushTimeout = null;
    }
  }

  private handleConflicts(payload: SlashCommandConflictsPayload) {
    const newConflicts = payload.conflicts.filter((c) => {
      // Use a unique key to prevent duplicate notifications for the same conflict
      const sourceId =
        c.loserExtensionName || c.loserMcpServerName || c.loserKind;
      const key = `${c.name}:${sourceId}:${c.renamedTo}`;
      if (this.notifiedConflicts.has(key)) {
        return false;
      }
      this.notifiedConflicts.add(key);
      return true;
    });

    if (newConflicts.length > 0) {
      this.pendingConflicts.push(...newConflicts);
      this.scheduleFlush();
    }
  }

  private scheduleFlush() {
    if (this.flushTimeout) {
      clearTimeout(this.flushTimeout);
    }
    // Use a trailing debounce to capture staggered reloads during startup
    this.flushTimeout = setTimeout(() => this.flush(), 500);
  }

  private flush() {
    this.flushTimeout = null;
    const conflicts = [...this.pendingConflicts];
    this.pendingConflicts = [];

    if (conflicts.length === 0) {
      return;
    }

    // Group conflicts by their original command name
    const grouped = new Map<string, SlashCommandConflict[]>();
    for (const c of conflicts) {
      const list = grouped.get(c.name) ?? [];
      list.push(c);
      grouped.set(c.name, list);
    }

    for (const [name, commandConflicts] of grouped) {
      if (commandConflicts.length > 1) {
        this.emitGroupedFeedback(name, commandConflicts);
      } else {
        this.emitSingleFeedback(commandConflicts[0]);
      }
    }
  }

  /**
   * Emits a grouped notification for multiple conflicts sharing the same name.
   */
  private emitGroupedFeedback(
    name: string,
    conflicts: SlashCommandConflict[],
  ): void {
    const messages = conflicts
      .map((c) => {
        const source = this.getSourceDescription(
          c.loserExtensionName,
          c.loserKind,
          c.loserMcpServerName,
        );
        return `- ${this.capitalize(source)} '/${c.name}' was renamed to '/${c.renamedTo}'`;
      })
      .join('\n');

    coreEvents.emitFeedback(
      'info',
      `Conflicts detected for command '/${name}':\n${messages}`,
    );
  }

  /**
   * Emits a descriptive notification for a single command conflict.
   */
  private emitSingleFeedback(c: SlashCommandConflict): void {
    const loserSource = this.getSourceDescription(
      c.loserExtensionName,
      c.loserKind,
      c.loserMcpServerName,
    );
    const winnerSource = this.getSourceDescription(
      c.winnerExtensionName,
      c.winnerKind,
      c.winnerMcpServerName,
    );

    coreEvents.emitFeedback(
      'info',
      `${this.capitalize(loserSource)} '/${c.name}' was renamed to '/${c.renamedTo}' because it conflicts with ${winnerSource}.`,
    );
  }

  private capitalize(s: string): string {
    return s.charAt(0).toUpperCase() + s.slice(1);
  }

  /**
   * Returns a human-readable description of a command's source.
   */
  private getSourceDescription(
    extensionName?: string,
    kind?: string,
    mcpServerName?: string,
  ): string {
    switch (kind) {
      case CommandKind.EXTENSION_FILE:
        return extensionName
          ? `extension '${extensionName}' command`
          : 'extension command';
      case CommandKind.SKILL:
        return extensionName
          ? `extension '${extensionName}' skill`
          : 'skill command';
      case CommandKind.MCP_PROMPT:
        return mcpServerName
          ? `MCP server '${mcpServerName}' command`
          : 'MCP server command';
      case CommandKind.USER_FILE:
        return 'user command';
      case CommandKind.WORKSPACE_FILE:
        return 'workspace command';
      case CommandKind.BUILT_IN:
        return 'built-in command';
      default:
        return 'existing command';
    }
  }
}