@gakwaya/app-agent-tools
v1.3.2
Published
Advanced tool system with composition, discovery, and enhanced execution
Readme
@gakwaya/app-agent-tools
Advanced tool system with composition, discovery, caching, and enhanced execution capabilities for AI agents.
Features
- Tool Composition - Combine multiple tools into complex workflows
- Tool Discovery - Smart tool search and recommendation
- Result Caching - Cache tool results for performance
- Batch Execution - Execute multiple tools efficiently
- Parameter Validation - Zod schema validation for all tools
- Metrics Collection - Track tool usage and performance
- Error Recovery - Retry logic and error handling
- Timeout Control - Configurable timeouts per tool
Installation
pnpm add @gakwaya/app-agent-toolsUsage
Basic Tool Registration
import { ToolRegistry } from '@gakwaya/app-agent-tools';
import { z } from 'zod';
const registry = new ToolRegistry({
enableCaching: true,
enableMetrics: true,
defaultTimeout: 30000,
});
// Define a tool
const navigateTool = {
id: 'navigate',
name: 'Navigate',
description: 'Navigate to a specific path in the application',
category: 'navigation' as const,
inputSchema: z.object({
path: z.string().describe('The path to navigate to'),
}),
execute: async (params, context) => {
// Navigation logic
window.location.href = params.path;
return { navigated: true, path: params.path };
},
metadata: {
tags: ['navigation', 'routing'],
examples: [
{
description: 'Navigate to home',
parameters: { path: '/' },
result: { navigated: true, path: '/' },
},
],
capabilities: ['client-side-navigation', 'url-manipulation'],
riskLevel: 'low' as const,
},
options: {
timeout: 5000,
caching: {
enabled: true,
ttl: 60000,
maxSize: 50,
},
},
};
// Register the tool
registry.registerTool(navigateTool);
// Execute the tool
const result = await registry.executeTool('navigate', { path: '/dashboard' }, context);Tool Composition
// Register a composition that combines multiple tools
const checkoutComposition = {
id: 'checkout-flow',
name: 'Checkout Flow',
description: 'Complete checkout process with multiple steps',
tools: [
{
toolId: 'navigate-to-cart',
parameters: { path: '/cart' },
},
{
toolId: 'verify-items',
parameters: {},
},
{
toolId: 'proceed-checkout',
parameters: {},
},
{
toolId: 'complete-payment',
parameters: {},
},
],
strategy: 'sequential' as const,
errorHandling: 'stop' as const,
};
registry.registerComposition(checkoutComposition);
// Execute the composition
const results = await registry.executeComposition('checkout-flow', {}, context);Batch Execution
// Execute multiple tools at once
const batch = {
batchId: 'batch-1',
executions: [
{ toolId: 'extract-price', parameters: { selector: '.price' } },
{ toolId: 'extract-title', parameters: { selector: '.title' } },
{ toolId: 'extract-availability', parameters: { selector: '.stock' } },
],
mode: 'parallel' as const,
errorHandling: 'continue' as const,
};
const batchResult = await registry.executeBatch(batch);
console.log('Extracted data:', batchResult.results);Tool Discovery
// Discover tools based on capabilities
const query = {
terms: ['form', 'fill'],
categories: ['interaction'] as const,
capabilities: ['input-text', 'form-submission'],
maxRiskLevel: 'medium' as const,
};
const discovery = registry.discoverTools(query);
console.log('Relevant tools:', discovery.tools);
console.log('Relevance scores:', discovery.scores);Tool Metrics
// Get metrics for all tools
const allMetrics = registry.getMetrics();
console.log('Tool performance:', allMetrics);
// Get metrics for specific tool
const toolMetrics = registry.getMetrics('navigate');
console.log('Navigate tool stats:', {
totalCalls: toolMetrics.totalCalls,
successRate: toolMetrics.successfulCalls / toolMetrics.totalCalls,
avgDuration: toolMetrics.avgDuration,
cacheHitRate: toolMetrics.cacheHitRate,
});Tool Categories
Navigation
- Page/view navigation
- URL manipulation
- Back/forward navigation
Interaction
- Element clicking
- Form filling
- Text input
- Dropdown selection
Extraction
- Text extraction
- Data parsing
- Element information
- Attribute extraction
Manipulation
- DOM modification
- Data transformation
- Variable updates
Verification
- Result validation
- Condition checking
- State verification
Utility
- Wait/delay
- Logging
- Helper functions
Composite
- Multi-step workflows
- Tool chains
- Complex operations
Advanced Features
Result Caching
Tools can cache their results for improved performance:
const cachedTool = {
id: 'get-user-info',
name: 'Get User Info',
// ... other properties
options: {
caching: {
enabled: true,
ttl: 300000, // 5 minutes
maxSize: 100,
},
},
};
// First call executes the tool
const result1 = await registry.executeTool('get-user-info', { userId: '123' }, context);
// Second call returns cached result (within TTL)
const result2 = await registry.executeTool('get-user-info', { userId: '123' }, context);Error Recovery
Configure retry logic for flaky tools:
const resilientTool = {
id: 'api-call',
name: 'API Call',
// ... other properties
options: {
retry: {
maxAttempts: 3,
retryDelay: 1000,
backoffMultiplier: 2,
retryableErrors: ['ETIMEDOUT', 'ECONNREFUSED'],
},
},
};Tool Discovery with Smart Matching
The registry can recommend tools based on:
- Search terms (name, description)
- Category matching
- Capability matching
- Tag matching
- Risk level filtering
// Find tools for form interaction
const formTools = registry.discoverTools({
terms: ['form', 'input', 'submit'],
capabilities: ['form-filling', 'form-submission'],
categories: ['interaction'],
maxRiskLevel: 'low',
});Batch Execution Modes
Sequential Mode
Execute tools one after another:
const batch = {
mode: 'sequential' as const,
executions: [...],
errorHandling: 'stop' as const,
};Parallel Mode
Execute tools simultaneously:
const batch = {
mode: 'parallel' as const,
executions: [...],
errorHandling: 'collect' as const,
};API
ToolRegistry
Constructor
new ToolRegistry(config?: ToolRegistryConfig)Methods
registerTool(tool)- Register a toolunregisterTool(toolId)- Unregister a toolgetTool(toolId)- Get tool by IDgetAllTools()- Get all registered toolsgetToolsByCategory(category)- Get tools by categorydiscoverTools(query)- Discover tools based on queryexecuteTool(toolId, params, context)- Execute a toolexecuteBatch(batch)- Execute batch of toolsregisterComposition(composition)- Register tool compositionexecuteComposition(compositionId, params, context)- Execute compositiongetMetrics(toolId?)- Get tool metricsclearCache(toolId?)- Clear tool cachedispose()- Dispose of registry
Events
tool_registered- Tool registeredtool_unregistered- Tool unregisteredtool_executed- Tool execution completedcomposition_registered- Composition registered
Tool Definition Structure
interface Tool<TParams, TResult> {
id: string; // Unique identifier
name: string; // Display name
description: string; // What the tool does
category: ToolCategory; // Tool category
inputSchema: z.ZodType<TParams>; // Parameter validation
outputSchema?: z.ZodType<TResult>; // Result validation
execute: (params: TParams, context: ToolContext) => Promise<TResult>;
metadata: {
tags: string[];
examples: ToolExample[];
capabilities: string[];
riskLevel: 'low' | 'medium' | 'high';
};
options?: {
timeout?: number;
retry?: RetryConfig;
caching?: CachingConfig;
};
}Configuration
ToolRegistryConfig
interface ToolRegistryConfig {
autoDiscover?: boolean; // Auto-discover tools (default: true)
enableCaching?: boolean; // Enable result caching (default: true)
defaultTimeout?: number; // Default timeout in ms (default: 30000)
enableMetrics?: boolean; // Enable metrics collection (default: true)
maxCacheSize?: number; // Max cache entries (default: 100)
cacheTTL?: number; // Cache TTL in ms (default: 300000)
}Best Practices
Tool Design
- Clear Descriptions - Help discovery system match tools
- Proper Categories - Enable better organization
- Schema Validation - Use Zod for type safety
- Error Handling - Return meaningful error messages
- Examples - Provide usage examples
Performance
- Enable Caching - For expensive operations
- Batch Operations - Combine related calls
- Appropriate Timeouts - Balance responsiveness and reliability
- Monitor Metrics - Track performance and optimize
Security
- Risk Levels - Mark tools appropriately
- Input Validation - Strict schema validation
- Rate Limiting - Prevent abuse
- Permissions - Require specific permissions
License
MIT
