File size: 5,635 Bytes
52efc7b | 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as path from 'node:path';
import { getFolderStructure } from '../utils/getFolderStructure.js';
import type { MessageBus } from '../confirmation-bus/message-bus.js';
import {
BaseDeclarativeTool,
BaseToolInvocation,
Kind,
type ToolResult,
type ToolCallConfirmationDetails,
type ToolInvocation,
type ToolConfirmationOutcome,
type ExecuteOptions,
} from './tools.js';
import type { Config } from '../config/config.js';
import { ACTIVATE_SKILL_TOOL_NAME } from './tool-names.js';
import { ToolErrorType } from './tool-error.js';
import { getActivateSkillDefinition } from './definitions/coreTools.js';
import { resolveToolDeclaration } from './definitions/resolver.js';
/**
* Parameters for the ActivateSkill tool
*/
export interface ActivateSkillToolParams {
/**
* The name of the skill to activate
*/
name: string;
}
class ActivateSkillToolInvocation extends BaseToolInvocation<
ActivateSkillToolParams,
ToolResult
> {
private cachedFolderStructure: string | undefined;
constructor(
private config: Config,
params: ActivateSkillToolParams,
messageBus: MessageBus,
_toolName?: string,
_toolDisplayName?: string,
) {
super(params, messageBus, _toolName, _toolDisplayName);
}
getDescription(): string {
const skillName = this.params.name;
const skill = this.config.getSkillManager().getSkill(skillName);
if (skill) {
return `"${skillName}": ${skill.description}`;
}
return `"${skillName}" (?) unknown skill`;
}
private async getOrFetchFolderStructure(
skillLocation: string,
): Promise<string> {
if (this.cachedFolderStructure === undefined) {
this.cachedFolderStructure = await getFolderStructure(
path.dirname(skillLocation),
);
}
return this.cachedFolderStructure;
}
protected override async getConfirmationDetails(
_abortSignal: AbortSignal,
): Promise<ToolCallConfirmationDetails | false> {
if (!this.messageBus) {
return false;
}
const skillName = this.params.name;
const skill = this.config.getSkillManager().getSkill(skillName);
if (!skill) {
return false;
}
if (skill.isBuiltin) {
return false;
}
const folderStructure = await this.getOrFetchFolderStructure(
skill.location,
);
const confirmationDetails: ToolCallConfirmationDetails = {
type: 'info',
title: `Activate Skill: ${skillName}`,
prompt: `You are about to enable the specialized agent skill **${skillName}**.
**Description:**
${skill.description}
**Resources to be shared with the model:**
${folderStructure}`,
onConfirm: async (outcome: ToolConfirmationOutcome) => {
await this.publishPolicyUpdate(outcome);
},
};
return confirmationDetails;
}
async execute({ abortSignal: _signal }: ExecuteOptions): Promise<ToolResult> {
const skillName = this.params.name;
const skillManager = this.config.getSkillManager();
const skill = skillManager.getSkill(skillName);
if (!skill) {
const skills = skillManager.getSkills();
const availableSkills = skills.map((s) => s.name).join(', ');
const errorMessage = `Skill "${skillName}" not found. Available skills are: ${availableSkills}`;
return {
llmContent: `Error: ${errorMessage}`,
returnDisplay: `Error: ${errorMessage}`,
error: {
message: errorMessage,
type: ToolErrorType.INVALID_TOOL_PARAMS,
},
};
}
skillManager.activateSkill(skillName);
// Add the skill's directory to the workspace context so the agent has permission
// to read its bundled resources.
this.config
.getWorkspaceContext()
.addDirectory(path.dirname(skill.location));
const folderStructure = await this.getOrFetchFolderStructure(
skill.location,
);
return {
llmContent: `<activated_skill name="${skillName}">
<instructions>
${skill.body}
</instructions>
<available_resources>
${folderStructure}
</available_resources>
</activated_skill>`,
returnDisplay: `Skill **${skillName}** activated. Resources loaded from \`${path.dirname(skill.location)}\`:\n\n${folderStructure}`,
};
}
}
/**
* Implementation of the ActivateSkill tool logic
*/
export class ActivateSkillTool extends BaseDeclarativeTool<
ActivateSkillToolParams,
ToolResult
> {
static readonly Name = ACTIVATE_SKILL_TOOL_NAME;
constructor(
private config: Config,
messageBus: MessageBus,
) {
const skills = config.getSkillManager().getSkills();
const skillNames = skills.map((s) => s.name);
const definition = getActivateSkillDefinition(skillNames);
super(
ActivateSkillTool.Name,
'Activate Skill',
definition.base.description!,
Kind.Other,
definition.base.parametersJsonSchema,
messageBus,
true,
false,
);
}
protected createInvocation(
params: ActivateSkillToolParams,
messageBus: MessageBus,
_toolName?: string,
_toolDisplayName?: string,
): ToolInvocation<ActivateSkillToolParams, ToolResult> {
return new ActivateSkillToolInvocation(
this.config,
params,
messageBus,
_toolName,
_toolDisplayName ?? 'Activate Skill',
);
}
override getSchema(modelId?: string) {
const skills = this.config.getSkillManager().getSkills();
const skillNames = skills.map((s) => s.name);
return resolveToolDeclaration(
getActivateSkillDefinition(skillNames),
modelId,
);
}
}
|