Agent Skills: Implement Core Skill Infrastructure & Tiered Discovery (#15698)

This commit is contained in:
N. Taylor Mullen
2025-12-30 13:35:52 -08:00
committed by GitHub
parent ec11b8afbf
commit de1233b8ca
19 changed files with 1209 additions and 3 deletions
@@ -0,0 +1,227 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { skillsCommand } from './skillsCommand.js';
import { MessageType } from '../types.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import type { CommandContext } from './types.js';
import type { Config } from '@google/gemini-cli-core';
import { SettingScope, type LoadedSettings } from '../../config/settings.js';
describe('skillsCommand', () => {
let context: CommandContext;
beforeEach(() => {
const skills = [
{
name: 'skill1',
description: 'desc1',
location: '/loc1',
body: 'body1',
},
{
name: 'skill2',
description: 'desc2',
location: '/loc2',
body: 'body2',
},
];
context = createMockCommandContext({
services: {
config: {
getSkillManager: vi.fn().mockReturnValue({
getAllSkills: vi.fn().mockReturnValue(skills),
getSkill: vi
.fn()
.mockImplementation(
(name: string) => skills.find((s) => s.name === name) ?? null,
),
}),
} as unknown as Config,
settings: {
merged: { skills: { disabled: [] } },
workspace: { path: '/workspace' },
setValue: vi.fn(),
} as unknown as LoadedSettings,
},
});
});
it('should add a SKILLS_LIST item to UI with descriptions by default', async () => {
await skillsCommand.action!(context, '');
expect(context.ui.addItem).toHaveBeenCalledWith(
expect.objectContaining({
type: MessageType.SKILLS_LIST,
skills: [
{ name: 'skill1', description: 'desc1' },
{ name: 'skill2', description: 'desc2' },
],
showDescriptions: true,
}),
expect.any(Number),
);
});
it('should list skills when "list" subcommand is used', async () => {
const listCmd = skillsCommand.subCommands!.find((s) => s.name === 'list')!;
await listCmd.action!(context, '');
expect(context.ui.addItem).toHaveBeenCalledWith(
expect.objectContaining({
type: MessageType.SKILLS_LIST,
skills: [
{ name: 'skill1', description: 'desc1' },
{ name: 'skill2', description: 'desc2' },
],
showDescriptions: true,
}),
expect.any(Number),
);
});
it('should disable descriptions if "nodesc" arg is provided to list', async () => {
const listCmd = skillsCommand.subCommands!.find((s) => s.name === 'list')!;
await listCmd.action!(context, 'nodesc');
expect(context.ui.addItem).toHaveBeenCalledWith(
expect.objectContaining({
showDescriptions: false,
}),
expect.any(Number),
);
});
describe('disable/enable', () => {
beforeEach(() => {
context.services.settings.merged.skills = { disabled: [] };
(
context.services.settings as unknown as { workspace: { path: string } }
).workspace = {
path: '/workspace',
};
});
it('should disable a skill', async () => {
const disableCmd = skillsCommand.subCommands!.find(
(s) => s.name === 'disable',
)!;
await disableCmd.action!(context, 'skill1');
expect(context.services.settings.setValue).toHaveBeenCalledWith(
SettingScope.Workspace,
'skills.disabled',
['skill1'],
);
expect(context.ui.addItem).toHaveBeenCalledWith(
expect.objectContaining({
type: MessageType.INFO,
text: expect.stringContaining('Skill "skill1" disabled'),
}),
expect.any(Number),
);
});
it('should enable a skill', async () => {
const enableCmd = skillsCommand.subCommands!.find(
(s) => s.name === 'enable',
)!;
context.services.settings.merged.skills = { disabled: ['skill1'] };
await enableCmd.action!(context, 'skill1');
expect(context.services.settings.setValue).toHaveBeenCalledWith(
SettingScope.Workspace,
'skills.disabled',
[],
);
expect(context.ui.addItem).toHaveBeenCalledWith(
expect.objectContaining({
type: MessageType.INFO,
text: expect.stringContaining('Skill "skill1" enabled'),
}),
expect.any(Number),
);
});
it('should show error if skill not found during disable', async () => {
const disableCmd = skillsCommand.subCommands!.find(
(s) => s.name === 'disable',
)!;
await disableCmd.action!(context, 'non-existent');
expect(context.ui.addItem).toHaveBeenCalledWith(
expect.objectContaining({
type: MessageType.ERROR,
text: 'Skill "non-existent" not found.',
}),
expect.any(Number),
);
});
});
describe('completions', () => {
it('should provide completions for disable (only enabled skills)', async () => {
const disableCmd = skillsCommand.subCommands!.find(
(s) => s.name === 'disable',
)!;
const skillManager = context.services.config!.getSkillManager();
const mockSkills = [
{
name: 'skill1',
description: 'desc1',
disabled: false,
location: '/loc1',
body: 'body1',
},
{
name: 'skill2',
description: 'desc2',
disabled: true,
location: '/loc2',
body: 'body2',
},
];
vi.mocked(skillManager.getAllSkills).mockReturnValue(mockSkills);
vi.mocked(skillManager.getSkill).mockImplementation(
(name: string) => mockSkills.find((s) => s.name === name) ?? null,
);
const completions = await disableCmd.completion!(context, 'sk');
expect(completions).toEqual(['skill1']);
});
it('should provide completions for enable (only disabled skills)', async () => {
const enableCmd = skillsCommand.subCommands!.find(
(s) => s.name === 'enable',
)!;
const skillManager = context.services.config!.getSkillManager();
const mockSkills = [
{
name: 'skill1',
description: 'desc1',
disabled: false,
location: '/loc1',
body: 'body1',
},
{
name: 'skill2',
description: 'desc2',
disabled: true,
location: '/loc2',
body: 'body2',
},
];
vi.mocked(skillManager.getAllSkills).mockReturnValue(mockSkills);
vi.mocked(skillManager.getSkill).mockImplementation(
(name: string) => mockSkills.find((s) => s.name === name) ?? null,
);
const completions = await enableCmd.completion!(context, 'sk');
expect(completions).toEqual(['skill2']);
});
});
});
@@ -0,0 +1,213 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
type CommandContext,
type SlashCommand,
type SlashCommandActionReturn,
CommandKind,
} from './types.js';
import { MessageType, type HistoryItemSkillsList } from '../types.js';
import { SettingScope } from '../../config/settings.js';
async function listAction(
context: CommandContext,
args: string,
): Promise<void | SlashCommandActionReturn> {
const subCommand = args.trim();
// Default to SHOWING descriptions. The user can hide them with 'nodesc'.
let useShowDescriptions = true;
if (subCommand === 'nodesc') {
useShowDescriptions = false;
}
const skillManager = context.services.config?.getSkillManager();
if (!skillManager) {
context.ui.addItem(
{
type: MessageType.ERROR,
text: 'Could not retrieve skill manager.',
},
Date.now(),
);
return;
}
const skills = skillManager.getAllSkills();
const skillsListItem: HistoryItemSkillsList = {
type: MessageType.SKILLS_LIST,
skills: skills.map((skill) => ({
name: skill.name,
description: skill.description,
disabled: skill.disabled,
})),
showDescriptions: useShowDescriptions,
};
context.ui.addItem(skillsListItem, Date.now());
}
async function disableAction(
context: CommandContext,
args: string,
): Promise<void | SlashCommandActionReturn> {
const skillName = args.trim();
if (!skillName) {
context.ui.addItem(
{
type: MessageType.ERROR,
text: 'Please provide a skill name to disable.',
},
Date.now(),
);
return;
}
const skillManager = context.services.config?.getSkillManager();
const skill = skillManager?.getSkill(skillName);
if (!skill) {
context.ui.addItem(
{
type: MessageType.ERROR,
text: `Skill "${skillName}" not found.`,
},
Date.now(),
);
return;
}
const currentDisabled =
context.services.settings.merged.skills?.disabled ?? [];
if (currentDisabled.includes(skillName)) {
context.ui.addItem(
{
type: MessageType.INFO,
text: `Skill "${skillName}" is already disabled.`,
},
Date.now(),
);
return;
}
const newDisabled = [...currentDisabled, skillName];
const scope = context.services.settings.workspace.path
? SettingScope.Workspace
: SettingScope.User;
context.services.settings.setValue(scope, 'skills.disabled', newDisabled);
context.ui.addItem(
{
type: MessageType.INFO,
text: `Skill "${skillName}" disabled in ${scope} settings. Restart required to take effect.`,
},
Date.now(),
);
}
async function enableAction(
context: CommandContext,
args: string,
): Promise<void | SlashCommandActionReturn> {
const skillName = args.trim();
if (!skillName) {
context.ui.addItem(
{
type: MessageType.ERROR,
text: 'Please provide a skill name to enable.',
},
Date.now(),
);
return;
}
const currentDisabled =
context.services.settings.merged.skills?.disabled ?? [];
if (!currentDisabled.includes(skillName)) {
context.ui.addItem(
{
type: MessageType.INFO,
text: `Skill "${skillName}" is not disabled.`,
},
Date.now(),
);
return;
}
const newDisabled = currentDisabled.filter((name) => name !== skillName);
const scope = context.services.settings.workspace.path
? SettingScope.Workspace
: SettingScope.User;
context.services.settings.setValue(scope, 'skills.disabled', newDisabled);
context.ui.addItem(
{
type: MessageType.INFO,
text: `Skill "${skillName}" enabled in ${scope} settings. Restart required to take effect.`,
},
Date.now(),
);
}
function disableCompletion(
context: CommandContext,
partialArg: string,
): string[] {
const skillManager = context.services.config?.getSkillManager();
if (!skillManager) {
return [];
}
return skillManager
.getAllSkills()
.filter((s) => !s.disabled && s.name.startsWith(partialArg))
.map((s) => s.name);
}
function enableCompletion(
context: CommandContext,
partialArg: string,
): string[] {
const skillManager = context.services.config?.getSkillManager();
if (!skillManager) {
return [];
}
return skillManager
.getAllSkills()
.filter((s) => s.disabled && s.name.startsWith(partialArg))
.map((s) => s.name);
}
export const skillsCommand: SlashCommand = {
name: 'skills',
description:
'List, enable, or disable Gemini CLI agent skills. Usage: /skills [list | disable <name> | enable <name>]',
kind: CommandKind.BUILT_IN,
autoExecute: false,
subCommands: [
{
name: 'list',
description: 'List available agent skills. Usage: /skills list [nodesc]',
kind: CommandKind.BUILT_IN,
action: listAction,
},
{
name: 'disable',
description: 'Disable a skill by name. Usage: /skills disable <name>',
kind: CommandKind.BUILT_IN,
action: disableAction,
completion: disableCompletion,
},
{
name: 'enable',
description:
'Enable a disabled skill by name. Usage: /skills enable <name>',
kind: CommandKind.BUILT_IN,
action: enableAction,
completion: enableCompletion,
},
],
action: listAction,
};