mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-13 05:12:55 -07:00
Adds executeCommand endpoint with support for /extensions list (#11515)
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
|
|
||||||
|
describe('CommandRegistry', () => {
|
||||||
|
const mockListExtensionsCommandInstance = {
|
||||||
|
names: ['extensions', 'extensions list'],
|
||||||
|
execute: vi.fn(),
|
||||||
|
};
|
||||||
|
const mockListExtensionsCommand = vi.fn(
|
||||||
|
() => mockListExtensionsCommandInstance,
|
||||||
|
);
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
vi.resetModules();
|
||||||
|
vi.doMock('./list-extensions', () => ({
|
||||||
|
ListExtensionsCommand: mockListExtensionsCommand,
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should register ListExtensionsCommand on initialization', async () => {
|
||||||
|
const { commandRegistry } = await import('./command-registry.js');
|
||||||
|
expect(mockListExtensionsCommand).toHaveBeenCalled();
|
||||||
|
const command = commandRegistry.get('extensions');
|
||||||
|
expect(command).toBe(mockListExtensionsCommandInstance);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('get() should return undefined for a non-existent command', async () => {
|
||||||
|
const { commandRegistry } = await import('./command-registry.js');
|
||||||
|
const command = commandRegistry.get('non-existent');
|
||||||
|
expect(command).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('register() should register a new command', async () => {
|
||||||
|
const { commandRegistry } = await import('./command-registry.js');
|
||||||
|
const mockCommand = {
|
||||||
|
names: ['test-command'],
|
||||||
|
execute: vi.fn(),
|
||||||
|
};
|
||||||
|
commandRegistry.register(mockCommand);
|
||||||
|
const command = commandRegistry.get('test-command');
|
||||||
|
expect(command).toBe(mockCommand);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ListExtensionsCommand } from './list-extensions.js';
|
||||||
|
import type { Config } from '@google/gemini-cli-core';
|
||||||
|
|
||||||
|
export interface Command {
|
||||||
|
readonly names: string[];
|
||||||
|
execute(config: Config, args: string[]): Promise<unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CommandRegistry {
|
||||||
|
private readonly commands = new Map<string, Command>();
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.register(new ListExtensionsCommand());
|
||||||
|
}
|
||||||
|
|
||||||
|
register(command: Command) {
|
||||||
|
for (const name of command.names) {
|
||||||
|
this.commands.set(name, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get(commandName: string): Command | undefined {
|
||||||
|
return this.commands.get(commandName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const commandRegistry = new CommandRegistry();
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { describe, it, expect, vi } from 'vitest';
|
||||||
|
import { ListExtensionsCommand } from './list-extensions.js';
|
||||||
|
import type { Config } from '@google/gemini-cli-core';
|
||||||
|
|
||||||
|
const mockListExtensions = vi.hoisted(() => vi.fn());
|
||||||
|
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
|
||||||
|
const original =
|
||||||
|
await importOriginal<typeof import('@google/gemini-cli-core')>();
|
||||||
|
|
||||||
|
return {
|
||||||
|
...original,
|
||||||
|
listExtensions: mockListExtensions,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('ListExtensionsCommand', () => {
|
||||||
|
it('should have the correct names', () => {
|
||||||
|
const command = new ListExtensionsCommand();
|
||||||
|
expect(command.names).toEqual(['extensions', 'extensions list']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call listExtensions with the provided config', async () => {
|
||||||
|
const command = new ListExtensionsCommand();
|
||||||
|
const mockConfig = {} as Config;
|
||||||
|
const mockExtensions = [{ name: 'ext1' }];
|
||||||
|
mockListExtensions.mockReturnValue(mockExtensions);
|
||||||
|
|
||||||
|
const result = await command.execute(mockConfig, []);
|
||||||
|
|
||||||
|
expect(result).toEqual(mockExtensions);
|
||||||
|
expect(mockListExtensions).toHaveBeenCalledWith(mockConfig);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { listExtensions, type Config } from '@google/gemini-cli-core';
|
||||||
|
import type { Command } from './command-registry.js';
|
||||||
|
|
||||||
|
export class ListExtensionsCommand implements Command {
|
||||||
|
readonly names = ['extensions', 'extensions list'];
|
||||||
|
|
||||||
|
async execute(config: Config, _: string[]): Promise<unknown> {
|
||||||
|
return listExtensions(config);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,6 +71,7 @@ export async function loadConfig(
|
|||||||
},
|
},
|
||||||
ideMode: false,
|
ideMode: false,
|
||||||
folderTrust: settings.folderTrust === true,
|
folderTrust: settings.folderTrust === true,
|
||||||
|
extensions,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fileService = new FileDiscoveryService(workspaceDir);
|
const fileService = new FileDiscoveryService(workspaceDir);
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ let config: Config;
|
|||||||
const getToolRegistrySpy = vi.fn().mockReturnValue(ApprovalMode.DEFAULT);
|
const getToolRegistrySpy = vi.fn().mockReturnValue(ApprovalMode.DEFAULT);
|
||||||
const getApprovalModeSpy = vi.fn();
|
const getApprovalModeSpy = vi.fn();
|
||||||
const getShellExecutionConfigSpy = vi.fn();
|
const getShellExecutionConfigSpy = vi.fn();
|
||||||
|
const getExtensionsSpy = vi.fn();
|
||||||
|
|
||||||
vi.mock('../config/config.js', async () => {
|
vi.mock('../config/config.js', async () => {
|
||||||
const actual = await vi.importActual('../config/config.js');
|
const actual = await vi.importActual('../config/config.js');
|
||||||
return {
|
return {
|
||||||
@@ -74,6 +76,7 @@ vi.mock('../config/config.js', async () => {
|
|||||||
getToolRegistry: getToolRegistrySpy,
|
getToolRegistry: getToolRegistrySpy,
|
||||||
getApprovalMode: getApprovalModeSpy,
|
getApprovalMode: getApprovalModeSpy,
|
||||||
getShellExecutionConfig: getShellExecutionConfigSpy,
|
getShellExecutionConfig: getShellExecutionConfigSpy,
|
||||||
|
getExtensions: getExtensionsSpy,
|
||||||
});
|
});
|
||||||
config = mockConfig as Config;
|
config = mockConfig as Config;
|
||||||
return config;
|
return config;
|
||||||
@@ -652,4 +655,62 @@ describe('E2E Tests', () => {
|
|||||||
expect(thoughtEvent.kind).toBe('status-update');
|
expect(thoughtEvent.kind).toBe('status-update');
|
||||||
expect(thoughtEvent.metadata?.['traceId']).toBe(traceId);
|
expect(thoughtEvent.metadata?.['traceId']).toBe(traceId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('/executeCommand', () => {
|
||||||
|
const mockExtensions = [{ name: 'test-extension', version: '0.0.1' }];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
getExtensionsSpy.mockReturnValue(mockExtensions);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
getExtensionsSpy.mockClear();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return extensions for valid command', async () => {
|
||||||
|
const agent = request.agent(app);
|
||||||
|
const res = await agent
|
||||||
|
.post('/executeCommand')
|
||||||
|
.send({ command: 'extensions list', args: [] })
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
expect(res.body).toEqual(mockExtensions);
|
||||||
|
expect(getExtensionsSpy).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 404 for invalid command', async () => {
|
||||||
|
const agent = request.agent(app);
|
||||||
|
const res = await agent
|
||||||
|
.post('/executeCommand')
|
||||||
|
.send({ command: 'invalid command' })
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.expect(404);
|
||||||
|
|
||||||
|
expect(res.body.error).toBe('Command not found: invalid command');
|
||||||
|
expect(getExtensionsSpy).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 400 for missing command', async () => {
|
||||||
|
const agent = request.agent(app);
|
||||||
|
await agent
|
||||||
|
.post('/executeCommand')
|
||||||
|
.send({ args: [] })
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.expect(400);
|
||||||
|
expect(getExtensionsSpy).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 400 if args is not an array', async () => {
|
||||||
|
const agent = request.agent(app);
|
||||||
|
const res = await agent
|
||||||
|
.post('/executeCommand')
|
||||||
|
.send({ command: 'extensions.list', args: 'not-an-array' })
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(res.body.error).toBe('"args" field must be an array.');
|
||||||
|
expect(getExtensionsSpy).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ import type { AgentSettings } from '../types.js';
|
|||||||
import { GCSTaskStore, NoOpTaskStore } from '../persistence/gcs.js';
|
import { GCSTaskStore, NoOpTaskStore } from '../persistence/gcs.js';
|
||||||
import { CoderAgentExecutor } from '../agent/executor.js';
|
import { CoderAgentExecutor } from '../agent/executor.js';
|
||||||
import { requestStorage } from './requestStorage.js';
|
import { requestStorage } from './requestStorage.js';
|
||||||
|
import { loadConfig, loadEnvironment, setTargetDir } from '../config/config.js';
|
||||||
|
import { loadSettings } from '../config/settings.js';
|
||||||
|
import { loadExtensions } from '../config/extension.js';
|
||||||
|
import { commandRegistry } from '../commands/command-registry.js';
|
||||||
|
|
||||||
const coderAgentCard: AgentCard = {
|
const coderAgentCard: AgentCard = {
|
||||||
name: 'Gemini SDLC Agent',
|
name: 'Gemini SDLC Agent',
|
||||||
@@ -61,6 +65,13 @@ export function updateCoderAgentCardUrl(port: number) {
|
|||||||
|
|
||||||
export async function createApp() {
|
export async function createApp() {
|
||||||
try {
|
try {
|
||||||
|
// Load the server configuration once on startup.
|
||||||
|
const workspaceRoot = setTargetDir(undefined);
|
||||||
|
loadEnvironment();
|
||||||
|
const settings = loadSettings(workspaceRoot);
|
||||||
|
const extensions = loadExtensions(workspaceRoot);
|
||||||
|
const config = await loadConfig(settings, extensions, 'a2a-server');
|
||||||
|
|
||||||
// loadEnvironment() is called within getConfig now
|
// loadEnvironment() is called within getConfig now
|
||||||
const bucketName = process.env['GCS_BUCKET_NAME'];
|
const bucketName = process.env['GCS_BUCKET_NAME'];
|
||||||
let taskStoreForExecutor: TaskStore;
|
let taskStoreForExecutor: TaskStore;
|
||||||
@@ -119,6 +130,38 @@ export async function createApp() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expressApp.post('/executeCommand', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { command, args } = req.body;
|
||||||
|
|
||||||
|
if (typeof command !== 'string') {
|
||||||
|
return res.status(400).json({ error: 'Invalid "command" field.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args && !Array.isArray(args)) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ error: '"args" field must be an array.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const commandToExecute = commandRegistry.get(command);
|
||||||
|
|
||||||
|
if (!commandToExecute) {
|
||||||
|
return res
|
||||||
|
.status(404)
|
||||||
|
.json({ error: `Command not found: ${command}` });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await commandToExecute.execute(config, args ?? []);
|
||||||
|
return res.status(200).json(result);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('Error executing /executeCommand:', e);
|
||||||
|
const errorMessage =
|
||||||
|
e instanceof Error ? e.message : 'Unknown error executing command';
|
||||||
|
return res.status(500).json({ error: errorMessage });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
expressApp.get('/tasks/metadata', async (req, res) => {
|
expressApp.get('/tasks/metadata', async (req, res) => {
|
||||||
// This endpoint is only meaningful if the task store is in-memory.
|
// This endpoint is only meaningful if the task store is in-memory.
|
||||||
if (!(taskStoreForExecutor instanceof InMemoryTaskStore)) {
|
if (!(taskStoreForExecutor instanceof InMemoryTaskStore)) {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ describe('extensionsCommand', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.resetAllMocks();
|
vi.resetAllMocks();
|
||||||
|
mockGetExtensions.mockReturnValue([]);
|
||||||
mockContext = createMockCommandContext({
|
mockContext = createMockCommandContext({
|
||||||
services: {
|
services: {
|
||||||
config: {
|
config: {
|
||||||
@@ -46,6 +47,7 @@ describe('extensionsCommand', () => {
|
|||||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
{
|
{
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
|
extensions: expect.any(Array),
|
||||||
},
|
},
|
||||||
expect.any(Number),
|
expect.any(Number),
|
||||||
);
|
);
|
||||||
@@ -113,11 +115,13 @@ describe('extensionsCommand', () => {
|
|||||||
await updateAction(mockContext, '--all');
|
await updateAction(mockContext, '--all');
|
||||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
|
extensions: expect.any(Array),
|
||||||
});
|
});
|
||||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
||||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
{
|
{
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
|
extensions: expect.any(Array),
|
||||||
},
|
},
|
||||||
expect.any(Number),
|
expect.any(Number),
|
||||||
);
|
);
|
||||||
@@ -130,11 +134,13 @@ describe('extensionsCommand', () => {
|
|||||||
await updateAction(mockContext, '--all');
|
await updateAction(mockContext, '--all');
|
||||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
|
extensions: expect.any(Array),
|
||||||
});
|
});
|
||||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
||||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
{
|
{
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
|
extensions: expect.any(Array),
|
||||||
},
|
},
|
||||||
expect.any(Number),
|
expect.any(Number),
|
||||||
);
|
);
|
||||||
@@ -202,11 +208,13 @@ describe('extensionsCommand', () => {
|
|||||||
});
|
});
|
||||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith({
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
|
extensions: expect.any(Array),
|
||||||
});
|
});
|
||||||
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(null);
|
||||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
{
|
{
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
|
extensions: expect.any(Array),
|
||||||
},
|
},
|
||||||
expect.any(Number),
|
expect.any(Number),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -4,9 +4,10 @@
|
|||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { listExtensions } from '@google/gemini-cli-core';
|
||||||
import type { ExtensionUpdateInfo } from '../../config/extension.js';
|
import type { ExtensionUpdateInfo } from '../../config/extension.js';
|
||||||
import { getErrorMessage } from '../../utils/errors.js';
|
import { getErrorMessage } from '../../utils/errors.js';
|
||||||
import { MessageType } from '../types.js';
|
import { MessageType, type HistoryItemExtensionsList } from '../types.js';
|
||||||
import {
|
import {
|
||||||
type CommandContext,
|
type CommandContext,
|
||||||
type SlashCommand,
|
type SlashCommand,
|
||||||
@@ -14,12 +15,14 @@ import {
|
|||||||
} from './types.js';
|
} from './types.js';
|
||||||
|
|
||||||
async function listAction(context: CommandContext) {
|
async function listAction(context: CommandContext) {
|
||||||
context.ui.addItem(
|
const historyItem: HistoryItemExtensionsList = {
|
||||||
{
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
extensions: context.services.config
|
||||||
},
|
? listExtensions(context.services.config)
|
||||||
Date.now(),
|
: [],
|
||||||
);
|
};
|
||||||
|
|
||||||
|
context.ui.addItem(historyItem, Date.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateAction(context: CommandContext, args: string): Promise<void> {
|
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[]>(
|
const updateComplete = new Promise<ExtensionUpdateInfo[]>(
|
||||||
(resolve) => (resolveUpdateComplete = resolve),
|
(resolve) => (resolveUpdateComplete = resolve),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const historyItem: HistoryItemExtensionsList = {
|
||||||
|
type: MessageType.EXTENSIONS_LIST,
|
||||||
|
extensions: context.services.config
|
||||||
|
? listExtensions(context.services.config)
|
||||||
|
: [],
|
||||||
|
};
|
||||||
|
|
||||||
updateComplete.then((updateInfos) => {
|
updateComplete.then((updateInfos) => {
|
||||||
if (updateInfos.length === 0) {
|
if (updateInfos.length === 0) {
|
||||||
context.ui.addItem(
|
context.ui.addItem(
|
||||||
@@ -52,19 +63,13 @@ function updateAction(context: CommandContext, args: string): Promise<void> {
|
|||||||
Date.now(),
|
Date.now(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
context.ui.addItem(
|
|
||||||
{
|
context.ui.addItem(historyItem, Date.now());
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
|
||||||
},
|
|
||||||
Date.now(),
|
|
||||||
);
|
|
||||||
context.ui.setPendingItem(null);
|
context.ui.setPendingItem(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
context.ui.setPendingItem({
|
context.ui.setPendingItem(historyItem);
|
||||||
type: MessageType.EXTENSIONS_LIST,
|
|
||||||
});
|
|
||||||
|
|
||||||
context.ui.dispatchExtensionStateUpdate({
|
context.ui.dispatchExtensionStateUpdate({
|
||||||
type: 'SCHEDULE_UPDATE',
|
type: 'SCHEDULE_UPDATE',
|
||||||
@@ -77,7 +82,7 @@ function updateAction(context: CommandContext, args: string): Promise<void> {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (names?.length) {
|
if (names?.length) {
|
||||||
const extensions = context.services.config!.getExtensions();
|
const extensions = listExtensions(context.services.config!);
|
||||||
for (const name of names) {
|
for (const name of names) {
|
||||||
const extension = extensions.find(
|
const extension = extensions.find(
|
||||||
(extension) => extension.name === name,
|
(extension) => extension.name === name,
|
||||||
@@ -120,7 +125,9 @@ const updateExtensionsCommand: SlashCommand = {
|
|||||||
kind: CommandKind.BUILT_IN,
|
kind: CommandKind.BUILT_IN,
|
||||||
action: updateAction,
|
action: updateAction,
|
||||||
completion: async (context, partialArg) => {
|
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 extensionNames = extensions.map((ext) => ext.name);
|
||||||
const suggestions = extensionNames.filter((name) =>
|
const suggestions = extensionNames.filter((name) =>
|
||||||
name.startsWith(partialArg),
|
name.startsWith(partialArg),
|
||||||
|
|||||||
@@ -130,7 +130,9 @@ export const HistoryItemDisplay: React.FC<HistoryItemDisplayProps> = ({
|
|||||||
{itemForDisplay.type === 'compression' && (
|
{itemForDisplay.type === 'compression' && (
|
||||||
<CompressionMessage compression={itemForDisplay.compression} />
|
<CompressionMessage compression={itemForDisplay.compression} />
|
||||||
)}
|
)}
|
||||||
{itemForDisplay.type === 'extensions_list' && <ExtensionsList />}
|
{itemForDisplay.type === 'extensions_list' && (
|
||||||
|
<ExtensionsList extensions={itemForDisplay.extensions} />
|
||||||
|
)}
|
||||||
{itemForDisplay.type === 'tools_list' && (
|
{itemForDisplay.type === 'tools_list' && (
|
||||||
<ToolsList
|
<ToolsList
|
||||||
terminalWidth={terminalWidth}
|
terminalWidth={terminalWidth}
|
||||||
|
|||||||
@@ -5,20 +5,40 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { render } from 'ink-testing-library';
|
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 { useUIState } from '../../contexts/UIStateContext.js';
|
||||||
import { ExtensionUpdateState } from '../../state/extensions.js';
|
import { ExtensionUpdateState } from '../../state/extensions.js';
|
||||||
import { ExtensionsList } from './ExtensionsList.js';
|
import { ExtensionsList } from './ExtensionsList.js';
|
||||||
import { createMockCommandContext } from '../../../test-utils/mockCommandContext.js';
|
|
||||||
|
|
||||||
vi.mock('../../contexts/UIStateContext.js');
|
vi.mock('../../contexts/UIStateContext.js');
|
||||||
|
|
||||||
const mockUseUIState = vi.mocked(useUIState);
|
const mockUseUIState = vi.mocked(useUIState);
|
||||||
|
|
||||||
const mockExtensions = [
|
const mockExtensions = [
|
||||||
{ name: 'ext-one', version: '1.0.0', isActive: true },
|
{
|
||||||
{ name: 'ext-two', version: '2.1.0', isActive: true },
|
name: 'ext-one',
|
||||||
{ name: 'ext-disabled', version: '3.0.0', isActive: false },
|
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 />', () => {
|
describe('<ExtensionsList />', () => {
|
||||||
@@ -27,31 +47,25 @@ describe('<ExtensionsList />', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const mockUIState = (
|
const mockUIState = (
|
||||||
extensions: unknown[],
|
|
||||||
extensionsUpdateState: Map<string, ExtensionUpdateState>,
|
extensionsUpdateState: Map<string, ExtensionUpdateState>,
|
||||||
) => {
|
) => {
|
||||||
mockUseUIState.mockReturnValue({
|
mockUseUIState.mockReturnValue({
|
||||||
commandContext: createMockCommandContext({
|
|
||||||
services: {
|
|
||||||
config: {
|
|
||||||
getExtensions: () => extensions,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
extensionsUpdateState,
|
extensionsUpdateState,
|
||||||
// Add other required properties from UIState if needed by the component
|
// Add other required properties from UIState if needed by the component
|
||||||
} as never);
|
} as never);
|
||||||
};
|
};
|
||||||
|
|
||||||
it('should render "No extensions installed." if there are no extensions', () => {
|
it('should render "No extensions installed." if there are no extensions', () => {
|
||||||
mockUIState([], new Map());
|
mockUIState(new Map());
|
||||||
const { lastFrame } = render(<ExtensionsList />);
|
const { lastFrame } = render(<ExtensionsList extensions={[]} />);
|
||||||
expect(lastFrame()).toContain('No extensions installed.');
|
expect(lastFrame()).toContain('No extensions installed.');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should render a list of extensions with their version and status', () => {
|
it('should render a list of extensions with their version and status', () => {
|
||||||
mockUIState(mockExtensions, new Map());
|
mockUIState(new Map());
|
||||||
const { lastFrame } = render(<ExtensionsList />);
|
const { lastFrame } = render(
|
||||||
|
<ExtensionsList extensions={mockExtensions} />,
|
||||||
|
);
|
||||||
const output = lastFrame();
|
const output = lastFrame();
|
||||||
expect(output).toContain('ext-one (v1.0.0) - active');
|
expect(output).toContain('ext-one (v1.0.0) - active');
|
||||||
expect(output).toContain('ext-two (v2.1.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', () => {
|
it('should display "unknown state" if an extension has no update state', () => {
|
||||||
mockUIState([mockExtensions[0]], new Map());
|
mockUIState(new Map());
|
||||||
const { lastFrame } = render(<ExtensionsList />);
|
const { lastFrame } = render(
|
||||||
|
<ExtensionsList extensions={[mockExtensions[0]]} />,
|
||||||
|
);
|
||||||
expect(lastFrame()).toContain('(unknown state)');
|
expect(lastFrame()).toContain('(unknown state)');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -94,8 +110,10 @@ describe('<ExtensionsList />', () => {
|
|||||||
for (const { state, expectedText } of stateTestCases) {
|
for (const { state, expectedText } of stateTestCases) {
|
||||||
it(`should correctly display the state: ${state}`, () => {
|
it(`should correctly display the state: ${state}`, () => {
|
||||||
const updateState = new Map([[mockExtensions[0].name, state]]);
|
const updateState = new Map([[mockExtensions[0].name, state]]);
|
||||||
mockUIState([mockExtensions[0]], updateState);
|
mockUIState(updateState);
|
||||||
const { lastFrame } = render(<ExtensionsList />);
|
const { lastFrame } = render(
|
||||||
|
<ExtensionsList extensions={[mockExtensions[0]]} />,
|
||||||
|
);
|
||||||
expect(lastFrame()).toContain(expectedText);
|
expect(lastFrame()).toContain(expectedText);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,20 @@
|
|||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import type React from 'react';
|
||||||
import { Box, Text } from 'ink';
|
import { Box, Text } from 'ink';
|
||||||
import { useUIState } from '../../contexts/UIStateContext.js';
|
import { useUIState } from '../../contexts/UIStateContext.js';
|
||||||
import { ExtensionUpdateState } from '../../state/extensions.js';
|
import { ExtensionUpdateState } from '../../state/extensions.js';
|
||||||
|
import type { GeminiCLIExtension } from '@google/gemini-cli-core';
|
||||||
|
|
||||||
export const ExtensionsList = () => {
|
interface ExtensionsList {
|
||||||
const { commandContext, extensionsUpdateState } = useUIState();
|
extensions: readonly GeminiCLIExtension[];
|
||||||
const allExtensions = commandContext.services.config!.getExtensions();
|
}
|
||||||
|
|
||||||
if (allExtensions.length === 0) {
|
export const ExtensionsList: React.FC<ExtensionsList> = ({ extensions }) => {
|
||||||
|
const { extensionsUpdateState } = useUIState();
|
||||||
|
|
||||||
|
if (extensions.length === 0) {
|
||||||
return <Text>No extensions installed.</Text>;
|
return <Text>No extensions installed.</Text>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,7 +25,7 @@ export const ExtensionsList = () => {
|
|||||||
<Box flexDirection="column" marginTop={1} marginBottom={1}>
|
<Box flexDirection="column" marginTop={1} marginBottom={1}>
|
||||||
<Text>Installed extensions:</Text>
|
<Text>Installed extensions:</Text>
|
||||||
<Box flexDirection="column" paddingLeft={2}>
|
<Box flexDirection="column" paddingLeft={2}>
|
||||||
{allExtensions.map((ext) => {
|
{extensions.map((ext) => {
|
||||||
const state = extensionsUpdateState.get(ext.name);
|
const state = extensionsUpdateState.get(ext.name);
|
||||||
const isActive = ext.isActive;
|
const isActive = ext.isActive;
|
||||||
const activeString = isActive ? 'active' : 'disabled';
|
const activeString = isActive ? 'active' : 'disabled';
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
import type {
|
import type {
|
||||||
CompressionStatus,
|
CompressionStatus,
|
||||||
|
GeminiCLIExtension,
|
||||||
MCPServerConfig,
|
MCPServerConfig,
|
||||||
ThoughtSummary,
|
ThoughtSummary,
|
||||||
ToolCallConfirmationDetails,
|
ToolCallConfirmationDetails,
|
||||||
@@ -163,6 +164,7 @@ export type HistoryItemCompression = HistoryItemBase & {
|
|||||||
|
|
||||||
export type HistoryItemExtensionsList = HistoryItemBase & {
|
export type HistoryItemExtensionsList = HistoryItemBase & {
|
||||||
type: 'extensions_list';
|
type: 'extensions_list';
|
||||||
|
extensions: GeminiCLIExtension[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface ChatDetail {
|
export interface ChatDetail {
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { describe, it, expect, vi } from 'vitest';
|
||||||
|
import { listExtensions } from './extensions.js';
|
||||||
|
import type { Config } from '../config/config.js';
|
||||||
|
|
||||||
|
describe('listExtensions', () => {
|
||||||
|
it('should call config.getExtensions and return the result', () => {
|
||||||
|
const mockExtensions = [{ name: 'ext1' }, { name: 'ext2' }];
|
||||||
|
const mockConfig = {
|
||||||
|
getExtensions: vi.fn().mockReturnValue(mockExtensions),
|
||||||
|
} as unknown as Config;
|
||||||
|
|
||||||
|
const result = listExtensions(mockConfig);
|
||||||
|
|
||||||
|
expect(mockConfig.getExtensions).toHaveBeenCalledTimes(1);
|
||||||
|
expect(result).toEqual(mockExtensions);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { Config } from '../config/config.js';
|
||||||
|
|
||||||
|
export function listExtensions(config: Config) {
|
||||||
|
return config.getExtensions();
|
||||||
|
}
|
||||||
@@ -14,6 +14,9 @@ export * from './policy/policy-engine.js';
|
|||||||
export * from './confirmation-bus/types.js';
|
export * from './confirmation-bus/types.js';
|
||||||
export * from './confirmation-bus/message-bus.js';
|
export * from './confirmation-bus/message-bus.js';
|
||||||
|
|
||||||
|
// Export Commands logic
|
||||||
|
export * from './commands/extensions.js';
|
||||||
|
|
||||||
// Export Core Logic
|
// Export Core Logic
|
||||||
export * from './core/client.js';
|
export * from './core/client.js';
|
||||||
export * from './core/contentGenerator.js';
|
export * from './core/contentGenerator.js';
|
||||||
|
|||||||
Reference in New Issue
Block a user