Files
gemini-cli/packages/cli/src/ui/commands/extensionsCommand.ts
T

153 lines
4.0 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { listExtensions } from '@google/gemini-cli-core';
2025-10-03 21:06:26 -07:00
import type { ExtensionUpdateInfo } from '../../config/extension.js';
2025-09-10 08:09:09 -07:00
import { getErrorMessage } from '../../utils/errors.js';
import { MessageType, type HistoryItemExtensionsList } from '../types.js';
import {
type CommandContext,
type SlashCommand,
CommandKind,
} from './types.js';
2025-09-10 08:09:09 -07:00
async function listAction(context: CommandContext) {
const historyItem: HistoryItemExtensionsList = {
type: MessageType.EXTENSIONS_LIST,
extensions: context.services.config
? listExtensions(context.services.config)
: [],
};
context.ui.addItem(historyItem, Date.now());
2025-09-10 08:09:09 -07:00
}
2025-10-03 21:06:26 -07:00
function updateAction(context: CommandContext, args: string): Promise<void> {
2025-09-10 08:09:09 -07:00
const updateArgs = args.split(' ').filter((value) => value.length > 0);
const all = updateArgs.length === 1 && updateArgs[0] === '--all';
2025-10-03 21:06:26 -07:00
const names = all ? null : updateArgs;
if (!all && names?.length === 0) {
context.ui.addItem(
{
type: MessageType.ERROR,
text: 'Usage: /extensions update <extension-names>|--all',
},
Date.now(),
);
2025-10-03 21:06:26 -07:00
return Promise.resolve();
}
2025-10-03 21:06:26 -07:00
let resolveUpdateComplete: (updateInfo: ExtensionUpdateInfo[]) => void;
const updateComplete = new Promise<ExtensionUpdateInfo[]>(
(resolve) => (resolveUpdateComplete = resolve),
);
const historyItem: HistoryItemExtensionsList = {
type: MessageType.EXTENSIONS_LIST,
extensions: context.services.config
? listExtensions(context.services.config)
: [],
};
2025-10-03 21:06:26 -07:00
updateComplete.then((updateInfos) => {
if (updateInfos.length === 0) {
context.ui.addItem(
{
type: MessageType.INFO,
text: 'No extensions to update.',
},
Date.now(),
);
}
context.ui.addItem(historyItem, Date.now());
2025-10-03 21:06:26 -07:00
context.ui.setPendingItem(null);
});
try {
context.ui.setPendingItem(historyItem);
2025-10-03 21:06:26 -07:00
context.ui.dispatchExtensionStateUpdate({
type: 'SCHEDULE_UPDATE',
payload: {
all,
names,
onComplete: (updateInfos) => {
resolveUpdateComplete(updateInfos);
},
},
});
if (names?.length) {
const extensions = listExtensions(context.services.config!);
2025-09-10 08:09:09 -07:00
for (const name of names) {
const extension = extensions.find(
(extension) => extension.name === name,
);
if (!extension) {
context.ui.addItem(
{
type: MessageType.ERROR,
text: `Extension ${name} not found.`,
},
Date.now(),
);
continue;
}
2025-09-10 08:09:09 -07:00
}
}
} catch (error) {
2025-10-03 21:06:26 -07:00
resolveUpdateComplete!([]);
context.ui.addItem(
{
type: MessageType.ERROR,
text: getErrorMessage(error),
2025-09-10 08:09:09 -07:00
},
Date.now(),
);
}
2025-10-03 21:06:26 -07:00
return updateComplete.then((_) => {});
2025-09-10 08:09:09 -07:00
}
const listExtensionsCommand: SlashCommand = {
name: 'list',
description: 'List active extensions',
kind: CommandKind.BUILT_IN,
action: listAction,
};
const updateExtensionsCommand: SlashCommand = {
name: 'update',
description: 'Update extensions. Usage: update <extension-names>|--all',
kind: CommandKind.BUILT_IN,
action: updateAction,
completion: async (context, partialArg) => {
const extensions = context.services.config
? listExtensions(context.services.config)
: [];
const extensionNames = extensions.map((ext) => ext.name);
const suggestions = extensionNames.filter((name) =>
name.startsWith(partialArg),
);
if ('--all'.startsWith(partialArg) || 'all'.startsWith(partialArg)) {
suggestions.unshift('--all');
}
return suggestions;
},
2025-09-10 08:09:09 -07:00
};
export const extensionsCommand: SlashCommand = {
name: 'extensions',
description: 'Manage extensions',
kind: CommandKind.BUILT_IN,
subCommands: [listExtensionsCommand, updateExtensionsCommand],
action: (context, args) =>
// Default to list if no subcommand is provided
listExtensionsCommand.action!(context, args),
};