mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-12 06:10:42 -07:00
Adds executeCommand endpoint with support for /extensions list (#11515)
This commit is contained in:
@@ -25,6 +25,7 @@ describe('extensionsCommand', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
mockGetExtensions.mockReturnValue([]);
|
||||
mockContext = createMockCommandContext({
|
||||
services: {
|
||||
config: {
|
||||
@@ -46,6 +47,7 @@ describe('extensionsCommand', () => {
|
||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||
{
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: expect.any(Array),
|
||||
},
|
||||
expect.any(Number),
|
||||
);
|
||||
@@ -113,11 +115,13 @@ describe('extensionsCommand', () => {
|
||||
await updateAction(mockContext, '--all');
|
||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: expect.any(Array),
|
||||
});
|
||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||
{
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: expect.any(Array),
|
||||
},
|
||||
expect.any(Number),
|
||||
);
|
||||
@@ -130,11 +134,13 @@ describe('extensionsCommand', () => {
|
||||
await updateAction(mockContext, '--all');
|
||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: expect.any(Array),
|
||||
});
|
||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||
{
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: expect.any(Array),
|
||||
},
|
||||
expect.any(Number),
|
||||
);
|
||||
@@ -202,11 +208,13 @@ describe('extensionsCommand', () => {
|
||||
});
|
||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: expect.any(Array),
|
||||
});
|
||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||
{
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: expect.any(Array),
|
||||
},
|
||||
expect.any(Number),
|
||||
);
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { listExtensions } from '@google/gemini-cli-core';
|
||||
import type { ExtensionUpdateInfo } from '../../config/extension.js';
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
import { MessageType } from '../types.js';
|
||||
import { MessageType, type HistoryItemExtensionsList } from '../types.js';
|
||||
import {
|
||||
type CommandContext,
|
||||
type SlashCommand,
|
||||
@@ -14,12 +15,14 @@ import {
|
||||
} from './types.js';
|
||||
|
||||
async function listAction(context: CommandContext) {
|
||||
context.ui.addItem(
|
||||
{
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
},
|
||||
Date.now(),
|
||||
);
|
||||
const historyItem: HistoryItemExtensionsList = {
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: context.services.config
|
||||
? listExtensions(context.services.config)
|
||||
: [],
|
||||
};
|
||||
|
||||
context.ui.addItem(historyItem, Date.now());
|
||||
}
|
||||
|
||||
function updateAction(context: CommandContext, args: string): Promise<void> {
|
||||
@@ -42,6 +45,14 @@ function updateAction(context: CommandContext, args: string): Promise<void> {
|
||||
const updateComplete = new Promise<ExtensionUpdateInfo[]>(
|
||||
(resolve) => (resolveUpdateComplete = resolve),
|
||||
);
|
||||
|
||||
const historyItem: HistoryItemExtensionsList = {
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
extensions: context.services.config
|
||||
? listExtensions(context.services.config)
|
||||
: [],
|
||||
};
|
||||
|
||||
updateComplete.then((updateInfos) => {
|
||||
if (updateInfos.length === 0) {
|
||||
context.ui.addItem(
|
||||
@@ -52,19 +63,13 @@ function updateAction(context: CommandContext, args: string): Promise<void> {
|
||||
Date.now(),
|
||||
);
|
||||
}
|
||||
context.ui.addItem(
|
||||
{
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
},
|
||||
Date.now(),
|
||||
);
|
||||
|
||||
context.ui.addItem(historyItem, Date.now());
|
||||
context.ui.setPendingItem(null);
|
||||
});
|
||||
|
||||
try {
|
||||
context.ui.setPendingItem({
|
||||
type: MessageType.EXTENSIONS_LIST,
|
||||
});
|
||||
context.ui.setPendingItem(historyItem);
|
||||
|
||||
context.ui.dispatchExtensionStateUpdate({
|
||||
type: 'SCHEDULE_UPDATE',
|
||||
@@ -77,7 +82,7 @@ function updateAction(context: CommandContext, args: string): Promise<void> {
|
||||
},
|
||||
});
|
||||
if (names?.length) {
|
||||
const extensions = context.services.config!.getExtensions();
|
||||
const extensions = listExtensions(context.services.config!);
|
||||
for (const name of names) {
|
||||
const extension = extensions.find(
|
||||
(extension) => extension.name === name,
|
||||
@@ -120,7 +125,9 @@ const updateExtensionsCommand: SlashCommand = {
|
||||
kind: CommandKind.BUILT_IN,
|
||||
action: updateAction,
|
||||
completion: async (context, partialArg) => {
|
||||
const extensions = context.services.config?.getExtensions() ?? [];
|
||||
const extensions = context.services.config
|
||||
? listExtensions(context.services.config)
|
||||
: [];
|
||||
const extensionNames = extensions.map((ext) => ext.name);
|
||||
const suggestions = extensionNames.filter((name) =>
|
||||
name.startsWith(partialArg),
|
||||
|
||||
@@ -130,7 +130,9 @@ export const HistoryItemDisplay: React.FC<HistoryItemDisplayProps> = ({
|
||||
{itemForDisplay.type === 'compression' && (
|
||||
<CompressionMessage compression={itemForDisplay.compression} />
|
||||
)}
|
||||
{itemForDisplay.type === 'extensions_list' && <ExtensionsList />}
|
||||
{itemForDisplay.type === 'extensions_list' && (
|
||||
<ExtensionsList extensions={itemForDisplay.extensions} />
|
||||
)}
|
||||
{itemForDisplay.type === 'tools_list' && (
|
||||
<ToolsList
|
||||
terminalWidth={terminalWidth}
|
||||
|
||||
@@ -5,20 +5,40 @@
|
||||
*/
|
||||
|
||||
import { render } from 'ink-testing-library';
|
||||
import { vi } from 'vitest';
|
||||
import { vi, describe, beforeEach, it, expect } from 'vitest';
|
||||
import { useUIState } from '../../contexts/UIStateContext.js';
|
||||
import { ExtensionUpdateState } from '../../state/extensions.js';
|
||||
import { ExtensionsList } from './ExtensionsList.js';
|
||||
import { createMockCommandContext } from '../../../test-utils/mockCommandContext.js';
|
||||
|
||||
vi.mock('../../contexts/UIStateContext.js');
|
||||
|
||||
const mockUseUIState = vi.mocked(useUIState);
|
||||
|
||||
const mockExtensions = [
|
||||
{ name: 'ext-one', version: '1.0.0', isActive: true },
|
||||
{ name: 'ext-two', version: '2.1.0', isActive: true },
|
||||
{ name: 'ext-disabled', version: '3.0.0', isActive: false },
|
||||
{
|
||||
name: 'ext-one',
|
||||
version: '1.0.0',
|
||||
isActive: true,
|
||||
path: '/path/to/ext-one',
|
||||
contextFiles: [],
|
||||
id: '',
|
||||
},
|
||||
{
|
||||
name: 'ext-two',
|
||||
version: '2.1.0',
|
||||
isActive: true,
|
||||
path: '/path/to/ext-two',
|
||||
contextFiles: [],
|
||||
id: '',
|
||||
},
|
||||
{
|
||||
name: 'ext-disabled',
|
||||
version: '3.0.0',
|
||||
isActive: false,
|
||||
path: '/path/to/ext-disabled',
|
||||
contextFiles: [],
|
||||
id: '',
|
||||
},
|
||||
];
|
||||
|
||||
describe('<ExtensionsList />', () => {
|
||||
@@ -27,31 +47,25 @@ describe('<ExtensionsList />', () => {
|
||||
});
|
||||
|
||||
const mockUIState = (
|
||||
extensions: unknown[],
|
||||
extensionsUpdateState: Map<string, ExtensionUpdateState>,
|
||||
) => {
|
||||
mockUseUIState.mockReturnValue({
|
||||
commandContext: createMockCommandContext({
|
||||
services: {
|
||||
config: {
|
||||
getExtensions: () => extensions,
|
||||
},
|
||||
},
|
||||
}),
|
||||
extensionsUpdateState,
|
||||
// Add other required properties from UIState if needed by the component
|
||||
} as never);
|
||||
};
|
||||
|
||||
it('should render "No extensions installed." if there are no extensions', () => {
|
||||
mockUIState([], new Map());
|
||||
const { lastFrame } = render(<ExtensionsList />);
|
||||
mockUIState(new Map());
|
||||
const { lastFrame } = render(<ExtensionsList extensions={[]} />);
|
||||
expect(lastFrame()).toContain('No extensions installed.');
|
||||
});
|
||||
|
||||
it('should render a list of extensions with their version and status', () => {
|
||||
mockUIState(mockExtensions, new Map());
|
||||
const { lastFrame } = render(<ExtensionsList />);
|
||||
mockUIState(new Map());
|
||||
const { lastFrame } = render(
|
||||
<ExtensionsList extensions={mockExtensions} />,
|
||||
);
|
||||
const output = lastFrame();
|
||||
expect(output).toContain('ext-one (v1.0.0) - active');
|
||||
expect(output).toContain('ext-two (v2.1.0) - active');
|
||||
@@ -59,8 +73,10 @@ describe('<ExtensionsList />', () => {
|
||||
});
|
||||
|
||||
it('should display "unknown state" if an extension has no update state', () => {
|
||||
mockUIState([mockExtensions[0]], new Map());
|
||||
const { lastFrame } = render(<ExtensionsList />);
|
||||
mockUIState(new Map());
|
||||
const { lastFrame } = render(
|
||||
<ExtensionsList extensions={[mockExtensions[0]]} />,
|
||||
);
|
||||
expect(lastFrame()).toContain('(unknown state)');
|
||||
});
|
||||
|
||||
@@ -94,8 +110,10 @@ describe('<ExtensionsList />', () => {
|
||||
for (const { state, expectedText } of stateTestCases) {
|
||||
it(`should correctly display the state: ${state}`, () => {
|
||||
const updateState = new Map([[mockExtensions[0].name, state]]);
|
||||
mockUIState([mockExtensions[0]], updateState);
|
||||
const { lastFrame } = render(<ExtensionsList />);
|
||||
mockUIState(updateState);
|
||||
const { lastFrame } = render(
|
||||
<ExtensionsList extensions={[mockExtensions[0]]} />,
|
||||
);
|
||||
expect(lastFrame()).toContain(expectedText);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,15 +4,20 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type React from 'react';
|
||||
import { Box, Text } from 'ink';
|
||||
import { useUIState } from '../../contexts/UIStateContext.js';
|
||||
import { ExtensionUpdateState } from '../../state/extensions.js';
|
||||
import type { GeminiCLIExtension } from '@google/gemini-cli-core';
|
||||
|
||||
export const ExtensionsList = () => {
|
||||
const { commandContext, extensionsUpdateState } = useUIState();
|
||||
const allExtensions = commandContext.services.config!.getExtensions();
|
||||
interface ExtensionsList {
|
||||
extensions: readonly GeminiCLIExtension[];
|
||||
}
|
||||
|
||||
if (allExtensions.length === 0) {
|
||||
export const ExtensionsList: React.FC<ExtensionsList> = ({ extensions }) => {
|
||||
const { extensionsUpdateState } = useUIState();
|
||||
|
||||
if (extensions.length === 0) {
|
||||
return <Text>No extensions installed.</Text>;
|
||||
}
|
||||
|
||||
@@ -20,7 +25,7 @@ export const ExtensionsList = () => {
|
||||
<Box flexDirection="column" marginTop={1} marginBottom={1}>
|
||||
<Text>Installed extensions:</Text>
|
||||
<Box flexDirection="column" paddingLeft={2}>
|
||||
{allExtensions.map((ext) => {
|
||||
{extensions.map((ext) => {
|
||||
const state = extensionsUpdateState.get(ext.name);
|
||||
const isActive = ext.isActive;
|
||||
const activeString = isActive ? 'active' : 'disabled';
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
import type {
|
||||
CompressionStatus,
|
||||
GeminiCLIExtension,
|
||||
MCPServerConfig,
|
||||
ThoughtSummary,
|
||||
ToolCallConfirmationDetails,
|
||||
@@ -163,6 +164,7 @@ export type HistoryItemCompression = HistoryItemBase & {
|
||||
|
||||
export type HistoryItemExtensionsList = HistoryItemBase & {
|
||||
type: 'extensions_list';
|
||||
extensions: GeminiCLIExtension[];
|
||||
};
|
||||
|
||||
export interface ChatDetail {
|
||||
|
||||
Reference in New Issue
Block a user