summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/commands/ideCommand.ts
blob: 6fc4f50b93a47e1bdb07462c006efc28f1cbff8b (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
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { fileURLToPath } from 'url';
import {
  Config,
  getMCPDiscoveryState,
  getMCPServerStatus,
  IDE_SERVER_NAME,
  MCPDiscoveryState,
  MCPServerStatus,
} from '@google/gemini-cli-core';
import {
  CommandContext,
  SlashCommand,
  SlashCommandActionReturn,
  CommandKind,
} from './types.js';
import * as child_process from 'child_process';
import * as process from 'process';
import { glob } from 'glob';
import * as path from 'path';

const VSCODE_COMMAND = process.platform === 'win32' ? 'code.cmd' : 'code';
const VSCODE_COMPANION_EXTENSION_FOLDER = 'vscode-ide-companion';

function isVSCodeInstalled(): boolean {
  try {
    child_process.execSync(
      process.platform === 'win32'
        ? `where.exe ${VSCODE_COMMAND}`
        : `command -v ${VSCODE_COMMAND}`,
      { stdio: 'ignore' },
    );
    return true;
  } catch {
    return false;
  }
}

export const ideCommand = (config: Config | null): SlashCommand | null => {
  if (!config?.getIdeMode()) {
    return null;
  }

  return {
    name: 'ide',
    description: 'manage IDE integration',
    kind: CommandKind.BUILT_IN,
    subCommands: [
      {
        name: 'status',
        description: 'check status of IDE integration',
        kind: CommandKind.BUILT_IN,
        action: (_context: CommandContext): SlashCommandActionReturn => {
          const status = getMCPServerStatus(IDE_SERVER_NAME);
          const discoveryState = getMCPDiscoveryState();
          switch (status) {
            case MCPServerStatus.CONNECTED:
              return {
                type: 'message',
                messageType: 'info',
                content: `🟢 Connected`,
              };
            case MCPServerStatus.CONNECTING:
              return {
                type: 'message',
                messageType: 'info',
                content: `🔄 Initializing...`,
              };
            case MCPServerStatus.DISCONNECTED:
            default:
              if (discoveryState === MCPDiscoveryState.IN_PROGRESS) {
                return {
                  type: 'message',
                  messageType: 'info',
                  content: `🔄 Initializing...`,
                };
              } else {
                return {
                  type: 'message',
                  messageType: 'error',
                  content: `🔴 Disconnected`,
                };
              }
          }
        },
      },
      {
        name: 'install',
        description: 'install required VS Code companion extension',
        kind: CommandKind.BUILT_IN,
        action: async (context) => {
          if (!isVSCodeInstalled()) {
            context.ui.addItem(
              {
                type: 'error',
                text: `VS Code command-line tool "${VSCODE_COMMAND}" not found in your PATH.`,
              },
              Date.now(),
            );
            return;
          }

          const bundleDir = path.dirname(fileURLToPath(import.meta.url));
          // The VSIX file is copied to the bundle directory as part of the build.
          let vsixFiles = glob.sync(path.join(bundleDir, '*.vsix'));
          if (vsixFiles.length === 0) {
            // If the VSIX file is not in the bundle, it might be a dev
            // environment running with `npm start`. Look for it in the original
            // package location, relative to the bundle dir.
            const devPath = path.join(
              bundleDir,
              '..',
              '..',
              '..',
              '..',
              '..',
              VSCODE_COMPANION_EXTENSION_FOLDER,
              '*.vsix',
            );
            vsixFiles = glob.sync(devPath);
          }
          if (vsixFiles.length === 0) {
            context.ui.addItem(
              {
                type: 'error',
                text: 'Could not find the required VS Code companion extension. Please file a bug via /bug.',
              },
              Date.now(),
            );
            return;
          }

          const vsixPath = vsixFiles[0];
          const command = `${VSCODE_COMMAND} --install-extension ${vsixPath} --force`;
          context.ui.addItem(
            {
              type: 'info',
              text: `Installing VS Code companion extension...`,
            },
            Date.now(),
          );
          try {
            child_process.execSync(command, { stdio: 'pipe' });
            context.ui.addItem(
              {
                type: 'info',
                text: 'VS Code companion extension installed successfully. Restart gemini-cli in a fresh terminal window.',
              },
              Date.now(),
            );
          } catch (_error) {
            context.ui.addItem(
              {
                type: 'error',
                text: `Failed to install VS Code companion extension.`,
              },
              Date.now(),
            );
          }
        },
      },
    ],
  };
};