feat(tools): add ToolRegistry, ToolSearchTool and ExpertTool to support on-demand tool visibility, discovery, activation, and expert/subagent tooling; extend DualAgentOrchestrator API and interfaces to manage tool lifecycle
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartagent',
|
||||
version: '1.7.0',
|
||||
version: '1.8.0',
|
||||
description: 'an agentic framework built on top of @push.rocks/smartai'
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ export { DualAgentOrchestrator } from './smartagent.classes.dualagent.js';
|
||||
export { DriverAgent } from './smartagent.classes.driveragent.js';
|
||||
export { GuardianAgent } from './smartagent.classes.guardianagent.js';
|
||||
|
||||
// Export tool registry and related classes
|
||||
export { ToolRegistry } from './smartagent.classes.toolregistry.js';
|
||||
|
||||
// Export base tool class for custom tool creation
|
||||
export { BaseToolWrapper } from './smartagent.tools.base.js';
|
||||
|
||||
@@ -18,6 +21,10 @@ export { BrowserTool } from './smartagent.tools.browser.js';
|
||||
export { DenoTool, type TDenoPermission } from './smartagent.tools.deno.js';
|
||||
export { JsonValidatorTool } from './smartagent.tools.json.js';
|
||||
|
||||
// Export tool search and expert tools
|
||||
export { ToolSearchTool } from './smartagent.tools.search.js';
|
||||
export { ExpertTool } from './smartagent.tools.expert.js';
|
||||
|
||||
// Export all interfaces
|
||||
export * from './smartagent.interfaces.js';
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@ import { HttpTool } from './smartagent.tools.http.js';
|
||||
import { ShellTool } from './smartagent.tools.shell.js';
|
||||
import { BrowserTool } from './smartagent.tools.browser.js';
|
||||
import { DenoTool } from './smartagent.tools.deno.js';
|
||||
import { ToolRegistry } from './smartagent.classes.toolregistry.js';
|
||||
import { ToolSearchTool } from './smartagent.tools.search.js';
|
||||
import { ExpertTool } from './smartagent.tools.expert.js';
|
||||
|
||||
/**
|
||||
* DualAgentOrchestrator - Coordinates Driver and Guardian agents
|
||||
@@ -20,7 +23,7 @@ export class DualAgentOrchestrator {
|
||||
private guardianProvider: plugins.smartai.MultiModalModel;
|
||||
private driver: DriverAgent;
|
||||
private guardian: GuardianAgent;
|
||||
private tools: Map<string, BaseToolWrapper> = new Map();
|
||||
private registry: ToolRegistry = new ToolRegistry();
|
||||
private isRunning = false;
|
||||
private conversationHistory: interfaces.IAgentMessage[] = [];
|
||||
private ownsSmartAi = true; // true if we created the SmartAi instance, false if it was provided
|
||||
@@ -125,19 +128,55 @@ export class DualAgentOrchestrator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom tool
|
||||
* Register a custom tool with optional visibility settings
|
||||
*/
|
||||
public registerTool(tool: BaseToolWrapper): void {
|
||||
this.tools.set(tool.name, tool);
|
||||
// Register with agents if they exist (they're created in start())
|
||||
if (this.driver) {
|
||||
this.driver.registerTool(tool);
|
||||
}
|
||||
if (this.guardian) {
|
||||
this.guardian.registerTool(tool);
|
||||
public registerTool(
|
||||
tool: BaseToolWrapper,
|
||||
options?: interfaces.IToolRegistrationOptions
|
||||
): void {
|
||||
this.registry.register(tool, options);
|
||||
|
||||
// If initial visibility and agents exist, register with them
|
||||
const visibility = options?.visibility ?? 'initial';
|
||||
if (visibility === 'initial') {
|
||||
if (this.driver) {
|
||||
this.driver.registerTool(tool);
|
||||
}
|
||||
if (this.guardian) {
|
||||
this.guardian.registerTool(tool);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an expert (subagent) as a tool
|
||||
*/
|
||||
public registerExpert(config: interfaces.IExpertConfig): void {
|
||||
const expert = new ExpertTool(config, this.smartai);
|
||||
this.registerTool(expert, {
|
||||
visibility: config.visibility,
|
||||
tags: config.tags,
|
||||
category: config.category ?? 'expert',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable tool search functionality
|
||||
* This adds a 'tools' tool that allows the Driver to discover and activate on-demand tools
|
||||
*/
|
||||
public enableToolSearch(): void {
|
||||
const searchTool = new ToolSearchTool(this.registry, (tool) => {
|
||||
// Callback when an on-demand tool is activated
|
||||
if (this.driver) {
|
||||
this.driver.registerTool(tool);
|
||||
}
|
||||
if (this.guardian) {
|
||||
this.guardian.registerTool(tool);
|
||||
}
|
||||
});
|
||||
this.registerTool(searchTool); // Always initial visibility
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all standard tools
|
||||
*/
|
||||
@@ -193,19 +232,14 @@ export class DualAgentOrchestrator {
|
||||
});
|
||||
this.guardian = new GuardianAgent(this.guardianProvider, this.options.guardianPolicyPrompt);
|
||||
|
||||
// Register any tools that were added before start() with the agents
|
||||
for (const tool of this.tools.values()) {
|
||||
// Register visible tools with agents
|
||||
for (const tool of this.registry.getVisibleTools()) {
|
||||
this.driver.registerTool(tool);
|
||||
this.guardian.registerTool(tool);
|
||||
}
|
||||
|
||||
// Initialize all tools
|
||||
const initPromises: Promise<void>[] = [];
|
||||
for (const tool of this.tools.values()) {
|
||||
initPromises.push(tool.initialize());
|
||||
}
|
||||
|
||||
await Promise.all(initPromises);
|
||||
// Initialize visible tools
|
||||
await this.registry.initializeVisibleTools();
|
||||
this.isRunning = true;
|
||||
}
|
||||
|
||||
@@ -213,13 +247,7 @@ export class DualAgentOrchestrator {
|
||||
* Cleanup all tools
|
||||
*/
|
||||
public async stop(): Promise<void> {
|
||||
const cleanupPromises: Promise<void>[] = [];
|
||||
|
||||
for (const tool of this.tools.values()) {
|
||||
cleanupPromises.push(tool.cleanup());
|
||||
}
|
||||
|
||||
await Promise.all(cleanupPromises);
|
||||
await this.registry.cleanup();
|
||||
|
||||
// Only stop smartai if we created it (don't stop external instances)
|
||||
if (this.ownsSmartAi) {
|
||||
@@ -432,7 +460,7 @@ Please output the exact XML format above.`
|
||||
});
|
||||
|
||||
// Execute the tool
|
||||
const tool = this.tools.get(proposal.toolName);
|
||||
const tool = this.registry.getTool(proposal.toolName);
|
||||
if (!tool) {
|
||||
const errorMessage = `Tool "${proposal.toolName}" not found.`;
|
||||
driverResponse = await this.driver.continueWithMessage(
|
||||
@@ -652,6 +680,13 @@ Please output the exact XML format above.`
|
||||
* Get registered tool names
|
||||
*/
|
||||
public getToolNames(): string[] {
|
||||
return Array.from(this.tools.keys());
|
||||
return this.registry.getAllMetadata().map((m) => m.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the tool registry for advanced operations
|
||||
*/
|
||||
public getRegistry(): ToolRegistry {
|
||||
return this.registry;
|
||||
}
|
||||
}
|
||||
|
||||
188
ts/smartagent.classes.toolregistry.ts
Normal file
188
ts/smartagent.classes.toolregistry.ts
Normal file
@@ -0,0 +1,188 @@
|
||||
import * as interfaces from './smartagent.interfaces.js';
|
||||
import { BaseToolWrapper } from './smartagent.tools.base.js';
|
||||
|
||||
/**
|
||||
* ToolRegistry - Manages tool registration, visibility, and lifecycle
|
||||
*
|
||||
* Responsibilities:
|
||||
* - Track all registered tools with their metadata
|
||||
* - Manage visibility (initial vs on-demand)
|
||||
* - Handle activation of on-demand tools
|
||||
* - Provide search functionality
|
||||
*/
|
||||
export class ToolRegistry {
|
||||
private tools: Map<string, BaseToolWrapper> = new Map();
|
||||
private metadata: Map<string, interfaces.IToolMetadata> = new Map();
|
||||
private activated: Set<string> = new Set();
|
||||
|
||||
/**
|
||||
* Register a tool with optional visibility settings
|
||||
*/
|
||||
register(tool: BaseToolWrapper, options: interfaces.IToolRegistrationOptions = {}): void {
|
||||
const visibility = options.visibility ?? 'initial';
|
||||
|
||||
this.tools.set(tool.name, tool);
|
||||
this.metadata.set(tool.name, {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
actions: tool.actions,
|
||||
visibility,
|
||||
isActivated: visibility === 'initial',
|
||||
isInitialized: false,
|
||||
tags: options.tags,
|
||||
category: options.category,
|
||||
});
|
||||
|
||||
if (visibility === 'initial') {
|
||||
this.activated.add(tool.name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tools visible to the Driver (initial + activated on-demand)
|
||||
*/
|
||||
getVisibleTools(): BaseToolWrapper[] {
|
||||
return Array.from(this.tools.entries())
|
||||
.filter(([name]) => this.activated.has(name))
|
||||
.map(([, tool]) => tool);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tools (for search results)
|
||||
*/
|
||||
getAllTools(): BaseToolWrapper[] {
|
||||
return Array.from(this.tools.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific tool by name
|
||||
*/
|
||||
getTool(name: string): BaseToolWrapper | undefined {
|
||||
return this.tools.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get metadata for a tool
|
||||
*/
|
||||
getMetadata(name: string): interfaces.IToolMetadata | undefined {
|
||||
return this.metadata.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all metadata
|
||||
*/
|
||||
getAllMetadata(): interfaces.IToolMetadata[] {
|
||||
return Array.from(this.metadata.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Search tools by query (matches name, description, tags, action names)
|
||||
*/
|
||||
search(query: string): interfaces.IToolMetadata[] {
|
||||
const q = query.toLowerCase();
|
||||
return this.getAllMetadata().filter((meta) => {
|
||||
if (meta.name.toLowerCase().includes(q)) return true;
|
||||
if (meta.description.toLowerCase().includes(q)) return true;
|
||||
if (meta.tags?.some((t) => t.toLowerCase().includes(q))) return true;
|
||||
if (meta.category?.toLowerCase().includes(q)) return true;
|
||||
if (
|
||||
meta.actions.some(
|
||||
(a) => a.name.toLowerCase().includes(q) || a.description.toLowerCase().includes(q)
|
||||
)
|
||||
)
|
||||
return true;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate an on-demand tool
|
||||
*/
|
||||
async activate(name: string): Promise<{ success: boolean; error?: string }> {
|
||||
const tool = this.tools.get(name);
|
||||
const meta = this.metadata.get(name);
|
||||
|
||||
if (!tool || !meta) {
|
||||
return { success: false, error: `Tool "${name}" not found` };
|
||||
}
|
||||
|
||||
if (this.activated.has(name)) {
|
||||
return { success: true }; // Already activated
|
||||
}
|
||||
|
||||
// Initialize if not already initialized
|
||||
if (!meta.isInitialized) {
|
||||
await tool.initialize();
|
||||
meta.isInitialized = true;
|
||||
}
|
||||
|
||||
this.activated.add(name);
|
||||
meta.isActivated = true;
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a tool is activated
|
||||
*/
|
||||
isActivated(name: string): boolean {
|
||||
return this.activated.has(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize all initial (visible) tools
|
||||
*/
|
||||
async initializeVisibleTools(): Promise<void> {
|
||||
const promises: Promise<void>[] = [];
|
||||
|
||||
for (const [name, tool] of this.tools) {
|
||||
const meta = this.metadata.get(name);
|
||||
if (meta && this.activated.has(name) && !meta.isInitialized) {
|
||||
promises.push(
|
||||
tool.initialize().then(() => {
|
||||
meta.isInitialized = true;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup all initialized tools
|
||||
*/
|
||||
async cleanup(): Promise<void> {
|
||||
const promises: Promise<void>[] = [];
|
||||
|
||||
for (const [name, tool] of this.tools) {
|
||||
const meta = this.metadata.get(name);
|
||||
if (meta?.isInitialized) {
|
||||
promises.push(tool.cleanup());
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a tool exists in the registry
|
||||
*/
|
||||
has(name: string): boolean {
|
||||
return this.tools.has(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of registered tools
|
||||
*/
|
||||
get size(): number {
|
||||
return this.tools.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of activated tools
|
||||
*/
|
||||
get activatedCount(): number {
|
||||
return this.activated.size;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,65 @@
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
// ================================
|
||||
// Tool Visibility & Registry Types
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* Tool visibility mode
|
||||
* - 'initial': Conveyed to model in system prompt AND discoverable via search
|
||||
* - 'on-demand': Only discoverable via search, must be activated before use
|
||||
*/
|
||||
export type TToolVisibility = 'initial' | 'on-demand';
|
||||
|
||||
/**
|
||||
* Tool metadata for discovery and management
|
||||
*/
|
||||
export interface IToolMetadata {
|
||||
name: string;
|
||||
description: string;
|
||||
actions: IToolAction[];
|
||||
visibility: TToolVisibility;
|
||||
isActivated: boolean;
|
||||
isInitialized: boolean;
|
||||
tags?: string[];
|
||||
category?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options when registering a tool
|
||||
*/
|
||||
export interface IToolRegistrationOptions {
|
||||
visibility?: TToolVisibility;
|
||||
tags?: string[];
|
||||
category?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for creating an Expert (SubAgent)
|
||||
*/
|
||||
export interface IExpertConfig {
|
||||
/** Unique name for the expert */
|
||||
name: string;
|
||||
/** Description of the expert's capabilities */
|
||||
description: string;
|
||||
/** System message defining expert behavior */
|
||||
systemMessage: string;
|
||||
/** Guardian policy for the expert's inner agent */
|
||||
guardianPolicy: string;
|
||||
/** AI provider (defaults to parent's provider) */
|
||||
provider?: plugins.smartai.TProvider;
|
||||
/** Tools available to this expert */
|
||||
tools?: IAgentToolWrapper[];
|
||||
/** Max iterations for expert tasks (default: 10) */
|
||||
maxIterations?: number;
|
||||
/** Visibility mode (default: 'initial') */
|
||||
visibility?: TToolVisibility;
|
||||
/** Searchable tags */
|
||||
tags?: string[];
|
||||
/** Category for grouping */
|
||||
category?: string;
|
||||
}
|
||||
|
||||
// ================================
|
||||
// Task Run Options
|
||||
// ================================
|
||||
|
||||
144
ts/smartagent.tools.expert.ts
Normal file
144
ts/smartagent.tools.expert.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as interfaces from './smartagent.interfaces.js';
|
||||
import { BaseToolWrapper } from './smartagent.tools.base.js';
|
||||
|
||||
// Forward declaration to avoid circular import at module load time
|
||||
// The actual import happens lazily in initialize()
|
||||
let DualAgentOrchestrator: typeof import('./smartagent.classes.dualagent.js').DualAgentOrchestrator;
|
||||
|
||||
/**
|
||||
* ExpertTool - A specialized agent wrapped as a tool
|
||||
*
|
||||
* Enables hierarchical agent architectures where the Driver can delegate
|
||||
* complex tasks to specialized experts with their own tools and policies.
|
||||
*/
|
||||
export class ExpertTool extends BaseToolWrapper {
|
||||
public name: string;
|
||||
public description: string;
|
||||
public actions: interfaces.IToolAction[] = [
|
||||
{
|
||||
name: 'consult',
|
||||
description: 'Delegate a task or question to this expert',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
task: { type: 'string', description: 'The task or question for the expert' },
|
||||
context: { type: 'string', description: 'Additional context to help the expert' },
|
||||
},
|
||||
required: ['task'],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
private config: interfaces.IExpertConfig;
|
||||
private smartAi: plugins.smartai.SmartAi;
|
||||
private inner?: InstanceType<typeof DualAgentOrchestrator>;
|
||||
|
||||
constructor(config: interfaces.IExpertConfig, smartAi: plugins.smartai.SmartAi) {
|
||||
super();
|
||||
this.config = config;
|
||||
this.smartAi = smartAi;
|
||||
this.name = config.name;
|
||||
this.description = config.description;
|
||||
}
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
// Lazy import to avoid circular dependency
|
||||
if (!DualAgentOrchestrator) {
|
||||
const module = await import('./smartagent.classes.dualagent.js');
|
||||
DualAgentOrchestrator = module.DualAgentOrchestrator;
|
||||
}
|
||||
|
||||
this.inner = new DualAgentOrchestrator({
|
||||
smartAiInstance: this.smartAi, // Share SmartAi instance
|
||||
defaultProvider: this.config.provider,
|
||||
driverSystemMessage: this.config.systemMessage,
|
||||
guardianPolicyPrompt: this.config.guardianPolicy,
|
||||
maxIterations: this.config.maxIterations ?? 10,
|
||||
});
|
||||
|
||||
// Register expert's tools
|
||||
if (this.config.tools) {
|
||||
for (const tool of this.config.tools) {
|
||||
// Tools in the config are IAgentToolWrapper, but we need BaseToolWrapper
|
||||
// Since all our tools extend BaseToolWrapper, this cast is safe
|
||||
this.inner.registerTool(tool as BaseToolWrapper);
|
||||
}
|
||||
}
|
||||
|
||||
await this.inner.start();
|
||||
this.isInitialized = true;
|
||||
}
|
||||
|
||||
async cleanup(): Promise<void> {
|
||||
if (this.inner) {
|
||||
await this.inner.stop();
|
||||
this.inner = undefined;
|
||||
}
|
||||
this.isInitialized = false;
|
||||
}
|
||||
|
||||
async execute(
|
||||
action: string,
|
||||
params: Record<string, unknown>
|
||||
): Promise<interfaces.IToolExecutionResult> {
|
||||
this.validateAction(action);
|
||||
this.ensureInitialized();
|
||||
|
||||
const task = params.task as string;
|
||||
const context = params.context as string | undefined;
|
||||
|
||||
const fullTask = context ? `Context: ${context}\n\nTask: ${task}` : task;
|
||||
|
||||
try {
|
||||
const result = await this.inner!.run(fullTask);
|
||||
|
||||
return {
|
||||
success: result.success,
|
||||
result: {
|
||||
response: result.result,
|
||||
iterations: result.iterations,
|
||||
status: result.status,
|
||||
},
|
||||
summary: result.success
|
||||
? `Expert "${this.name}" completed (${result.iterations} iterations)`
|
||||
: `Expert "${this.name}" failed: ${result.status}`,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Expert error: ${error instanceof Error ? error.message : String(error)}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
getCallSummary(action: string, params: Record<string, unknown>): string {
|
||||
const task = params.task as string;
|
||||
const preview = task.length > 60 ? task.substring(0, 60) + '...' : task;
|
||||
return `Consult ${this.name}: "${preview}"`;
|
||||
}
|
||||
|
||||
getToolExplanation(): string {
|
||||
return `## Expert: ${this.name}
|
||||
${this.description}
|
||||
|
||||
### Usage:
|
||||
Delegate tasks to this expert when you need specialized help.
|
||||
|
||||
\`\`\`
|
||||
<tool_call>
|
||||
<tool>${this.name}</tool>
|
||||
<action>consult</action>
|
||||
<params>{"task": "Your question or task", "context": "Optional background"}</params>
|
||||
</tool_call>
|
||||
\`\`\`
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the expert's configuration
|
||||
*/
|
||||
getConfig(): interfaces.IExpertConfig {
|
||||
return this.config;
|
||||
}
|
||||
}
|
||||
237
ts/smartagent.tools.search.ts
Normal file
237
ts/smartagent.tools.search.ts
Normal file
@@ -0,0 +1,237 @@
|
||||
import * as interfaces from './smartagent.interfaces.js';
|
||||
import { BaseToolWrapper } from './smartagent.tools.base.js';
|
||||
import { ToolRegistry } from './smartagent.classes.toolregistry.js';
|
||||
|
||||
/**
|
||||
* ToolSearchTool - AI-facing interface for discovering and activating tools
|
||||
*
|
||||
* This tool enables the Driver to:
|
||||
* - Search for tools by capability
|
||||
* - List all available tools
|
||||
* - Activate on-demand tools
|
||||
* - Get detailed information about specific tools
|
||||
*/
|
||||
export class ToolSearchTool extends BaseToolWrapper {
|
||||
public name = 'tools';
|
||||
public description =
|
||||
'Search for and activate available tools and experts. Use this to discover specialized capabilities.';
|
||||
|
||||
public actions: interfaces.IToolAction[] = [
|
||||
{
|
||||
name: 'search',
|
||||
description: 'Search for tools by name, description, tags, or capabilities',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
query: { type: 'string', description: 'Search query' },
|
||||
},
|
||||
required: ['query'],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'list',
|
||||
description: 'List all available tools grouped by visibility',
|
||||
parameters: { type: 'object', properties: {} },
|
||||
},
|
||||
{
|
||||
name: 'activate',
|
||||
description: 'Activate an on-demand tool to make it available for use',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name of the tool to activate' },
|
||||
},
|
||||
required: ['name'],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'details',
|
||||
description: 'Get detailed information about a specific tool',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name of the tool' },
|
||||
},
|
||||
required: ['name'],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
private registry: ToolRegistry;
|
||||
private onToolActivated?: (tool: BaseToolWrapper) => void;
|
||||
|
||||
constructor(registry: ToolRegistry, onToolActivated?: (tool: BaseToolWrapper) => void) {
|
||||
super();
|
||||
this.registry = registry;
|
||||
this.onToolActivated = onToolActivated;
|
||||
}
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
this.isInitialized = true;
|
||||
}
|
||||
|
||||
async cleanup(): Promise<void> {
|
||||
this.isInitialized = false;
|
||||
}
|
||||
|
||||
async execute(
|
||||
action: string,
|
||||
params: Record<string, unknown>
|
||||
): Promise<interfaces.IToolExecutionResult> {
|
||||
this.validateAction(action);
|
||||
|
||||
switch (action) {
|
||||
case 'search':
|
||||
return this.handleSearch(params.query as string);
|
||||
case 'list':
|
||||
return this.handleList();
|
||||
case 'activate':
|
||||
return this.handleActivate(params.name as string);
|
||||
case 'details':
|
||||
return this.handleDetails(params.name as string);
|
||||
default:
|
||||
return { success: false, error: `Unknown action: ${action}` };
|
||||
}
|
||||
}
|
||||
|
||||
private handleSearch(query: string): interfaces.IToolExecutionResult {
|
||||
const results = this.registry.search(query);
|
||||
return {
|
||||
success: true,
|
||||
result: results.map((m) => ({
|
||||
name: m.name,
|
||||
description: m.description,
|
||||
visibility: m.visibility,
|
||||
isActivated: m.isActivated,
|
||||
category: m.category,
|
||||
tags: m.tags,
|
||||
actionCount: m.actions.length,
|
||||
})),
|
||||
summary: `Found ${results.length} tools matching "${query}"`,
|
||||
};
|
||||
}
|
||||
|
||||
private handleList(): interfaces.IToolExecutionResult {
|
||||
const all = this.registry.getAllMetadata();
|
||||
const initial = all.filter((m) => m.visibility === 'initial');
|
||||
const onDemand = all.filter((m) => m.visibility === 'on-demand');
|
||||
|
||||
return {
|
||||
success: true,
|
||||
result: {
|
||||
initial: initial.map((m) => ({
|
||||
name: m.name,
|
||||
description: m.description,
|
||||
category: m.category,
|
||||
})),
|
||||
onDemand: onDemand.map((m) => ({
|
||||
name: m.name,
|
||||
description: m.description,
|
||||
category: m.category,
|
||||
isActivated: m.isActivated,
|
||||
})),
|
||||
summary: `${initial.length} initial, ${onDemand.length} on-demand`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private async handleActivate(name: string): Promise<interfaces.IToolExecutionResult> {
|
||||
const result = await this.registry.activate(name);
|
||||
|
||||
if (result.success && this.onToolActivated) {
|
||||
const tool = this.registry.getTool(name);
|
||||
if (tool) {
|
||||
this.onToolActivated(tool);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: result.success,
|
||||
result: result.success ? { name, message: `Tool "${name}" is now available` } : undefined,
|
||||
error: result.error,
|
||||
summary: result.success ? `Activated: ${name}` : result.error,
|
||||
};
|
||||
}
|
||||
|
||||
private handleDetails(name: string): interfaces.IToolExecutionResult {
|
||||
const tool = this.registry.getTool(name);
|
||||
const meta = this.registry.getMetadata(name);
|
||||
|
||||
if (!tool || !meta) {
|
||||
return { success: false, error: `Tool "${name}" not found` };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
result: {
|
||||
name: meta.name,
|
||||
description: meta.description,
|
||||
visibility: meta.visibility,
|
||||
isActivated: meta.isActivated,
|
||||
category: meta.category,
|
||||
tags: meta.tags,
|
||||
actions: meta.actions,
|
||||
fullExplanation: tool.getToolExplanation(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
getCallSummary(action: string, params: Record<string, unknown>): string {
|
||||
switch (action) {
|
||||
case 'search':
|
||||
return `Search tools: "${params.query}"`;
|
||||
case 'list':
|
||||
return 'List all tools';
|
||||
case 'activate':
|
||||
return `Activate tool: ${params.name}`;
|
||||
case 'details':
|
||||
return `Get details: ${params.name}`;
|
||||
default:
|
||||
return `tools.${action}`;
|
||||
}
|
||||
}
|
||||
|
||||
getToolExplanation(): string {
|
||||
return `## Tool: tools
|
||||
Search for and manage available tools and experts.
|
||||
|
||||
### Actions:
|
||||
|
||||
**search** - Find tools by capability
|
||||
\`\`\`
|
||||
<tool_call>
|
||||
<tool>tools</tool>
|
||||
<action>search</action>
|
||||
<params>{"query": "database"}</params>
|
||||
</tool_call>
|
||||
\`\`\`
|
||||
|
||||
**list** - List all tools grouped by visibility
|
||||
\`\`\`
|
||||
<tool_call>
|
||||
<tool>tools</tool>
|
||||
<action>list</action>
|
||||
<params>{}</params>
|
||||
</tool_call>
|
||||
\`\`\`
|
||||
|
||||
**activate** - Activate an on-demand tool
|
||||
\`\`\`
|
||||
<tool_call>
|
||||
<tool>tools</tool>
|
||||
<action>activate</action>
|
||||
<params>{"name": "database_expert"}</params>
|
||||
</tool_call>
|
||||
\`\`\`
|
||||
|
||||
**details** - Get full information about a tool
|
||||
\`\`\`
|
||||
<tool_call>
|
||||
<tool>tools</tool>
|
||||
<action>details</action>
|
||||
<params>{"name": "filesystem"}</params>
|
||||
</tool_call>
|
||||
\`\`\`
|
||||
`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user