Refactor project structure and remove unused modules
- Removed performance monitoring, tools service, and polyfills modules - Moved formatToolCall helper function to test file - Simplified imports in index.ts by removing polyfills import - Cleaned up unnecessary files and consolidated code structure
This commit is contained in:
@@ -1,17 +1,15 @@
|
||||
import { z } from 'zod';
|
||||
import { get_hass } from '../hass/index.js';
|
||||
import { Tool } from '../types/index.js';
|
||||
import { listDevicesTool } from './list-devices.tool.js';
|
||||
import { controlTool } from './control.tool.js';
|
||||
import { historyTool } from './history.tool.js';
|
||||
import { sceneTool } from './scene.tool.js';
|
||||
import { notifyTool } from './notify.tool.js';
|
||||
import { automationTool } from './automation.tool.js';
|
||||
import { addonTool } from './addon.tool.js';
|
||||
import { packageTool } from './package.tool.js';
|
||||
import { automationConfigTool } from './automation-config.tool.js';
|
||||
import { subscribeEventsTool } from './subscribe-events.tool.js';
|
||||
import { getSSEStatsTool } from './sse-stats.tool.js';
|
||||
import { Tool } from '../types/index';
|
||||
import { listDevicesTool } from './list-devices.tool';
|
||||
import { controlTool } from './control.tool';
|
||||
import { historyTool } from './history.tool';
|
||||
import { sceneTool } from './scene.tool';
|
||||
import { notifyTool } from './notify.tool';
|
||||
import { automationTool } from './automation.tool';
|
||||
import { addonTool } from './addon.tool';
|
||||
import { packageTool } from './package.tool';
|
||||
import { automationConfigTool } from './automation-config.tool';
|
||||
import { subscribeEventsTool } from './subscribe-events.tool';
|
||||
import { getSSEStatsTool } from './sse-stats.tool';
|
||||
|
||||
// Tool category types
|
||||
export enum ToolCategory {
|
||||
@@ -27,16 +25,6 @@ export enum ToolPriority {
|
||||
LOW = 'low'
|
||||
}
|
||||
|
||||
interface ToolParameters {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
interface Tool<Params extends ToolParameters = ToolParameters> {
|
||||
name: string;
|
||||
description: string;
|
||||
execute(params: Params): Promise<any>;
|
||||
}
|
||||
|
||||
interface ToolMetadata {
|
||||
category: ToolCategory;
|
||||
platform: string;
|
||||
@@ -47,161 +35,6 @@ interface ToolMetadata {
|
||||
};
|
||||
}
|
||||
|
||||
// Enhanced tool interface
|
||||
export interface EnhancedTool extends Tool {
|
||||
metadata: ToolMetadata;
|
||||
validate?: (params: any) => Promise<boolean>;
|
||||
preExecute?: (params: any) => Promise<void>;
|
||||
postExecute?: (result: any) => Promise<void>;
|
||||
}
|
||||
|
||||
// Tool registry for managing and organizing tools
|
||||
export class ToolRegistry {
|
||||
private tools: Map<string, EnhancedTool> = new Map();
|
||||
private categories: Map<ToolCategory, Set<string>> = new Map();
|
||||
private cache: Map<string, { data: any; timestamp: number }> = new Map();
|
||||
|
||||
constructor() {
|
||||
// Initialize categories
|
||||
Object.values(ToolCategory).forEach(category => {
|
||||
this.categories.set(category, new Set());
|
||||
});
|
||||
}
|
||||
|
||||
// Register a new tool
|
||||
public registerTool(tool: EnhancedTool): void {
|
||||
this.tools.set(tool.name, tool);
|
||||
this.categories.get(tool.metadata.category)?.add(tool.name);
|
||||
}
|
||||
|
||||
// Get tool by name
|
||||
public getTool(name: string): EnhancedTool | undefined {
|
||||
return this.tools.get(name);
|
||||
}
|
||||
|
||||
// Get all tools in a category
|
||||
public getToolsByCategory(category: ToolCategory): EnhancedTool[] {
|
||||
const toolNames = this.categories.get(category);
|
||||
if (!toolNames) return [];
|
||||
return Array.from(toolNames).map(name => this.tools.get(name)!);
|
||||
}
|
||||
|
||||
// Execute a tool with validation and hooks
|
||||
public async executeTool(name: string, params: any): Promise<any> {
|
||||
const tool = this.tools.get(name);
|
||||
if (!tool) {
|
||||
throw new Error(`Tool ${name} not found`);
|
||||
}
|
||||
|
||||
// Check cache if enabled
|
||||
if (tool.metadata.caching?.enabled) {
|
||||
const cacheKey = `${name}:${JSON.stringify(params)}`;
|
||||
const cached = this.cache.get(cacheKey);
|
||||
if (cached && Date.now() - cached.timestamp < tool.metadata.caching.ttl) {
|
||||
return cached.data;
|
||||
}
|
||||
}
|
||||
|
||||
// Validate parameters
|
||||
if (tool.validate) {
|
||||
const isValid = await tool.validate(params);
|
||||
if (!isValid) {
|
||||
throw new Error('Invalid parameters');
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-execution hook
|
||||
if (tool.preExecute) {
|
||||
await tool.preExecute(params);
|
||||
}
|
||||
|
||||
// Execute tool
|
||||
const result = await tool.execute(params);
|
||||
|
||||
// Post-execution hook
|
||||
if (tool.postExecute) {
|
||||
await tool.postExecute(result);
|
||||
}
|
||||
|
||||
// Update cache if enabled
|
||||
if (tool.metadata.caching?.enabled) {
|
||||
const cacheKey = `${name}:${JSON.stringify(params)}`;
|
||||
this.cache.set(cacheKey, {
|
||||
data: result,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Clean up expired cache entries
|
||||
public cleanCache(): void {
|
||||
const now = Date.now();
|
||||
for (const [key, value] of this.cache.entries()) {
|
||||
const tool = this.tools.get(key.split(':')[0]);
|
||||
if (tool?.metadata.caching?.ttl && now - value.timestamp > tool.metadata.caching.ttl) {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create and export the global tool registry
|
||||
export const toolRegistry = new ToolRegistry();
|
||||
|
||||
// Tool decorator for easy registration
|
||||
function registerTool(metadata: Partial<ToolMetadata>) {
|
||||
return function (constructor: any) {
|
||||
return constructor;
|
||||
};
|
||||
}
|
||||
|
||||
// Example usage:
|
||||
@registerTool({
|
||||
category: ToolCategory.DEVICE,
|
||||
platform: 'hass',
|
||||
version: '1.0.0',
|
||||
caching: {
|
||||
enabled: true,
|
||||
ttl: 60000
|
||||
}
|
||||
})
|
||||
export class LightControlTool implements EnhancedTool {
|
||||
name = 'light_control';
|
||||
description = 'Control light devices';
|
||||
metadata: ToolMetadata = {
|
||||
category: ToolCategory.DEVICE,
|
||||
platform: 'hass',
|
||||
version: '1.0.0',
|
||||
caching: {
|
||||
enabled: true,
|
||||
ttl: 60000
|
||||
}
|
||||
};
|
||||
parameters = z.object({
|
||||
command: z.enum(['turn_on', 'turn_off', 'toggle']),
|
||||
entity_id: z.string(),
|
||||
brightness: z.number().min(0).max(255).optional(),
|
||||
color_temp: z.number().optional(),
|
||||
rgb_color: z.tuple([z.number(), z.number(), z.number()]).optional()
|
||||
});
|
||||
|
||||
async validate(params: any): Promise<boolean> {
|
||||
try {
|
||||
this.parameters.parse(params);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async execute(params: any): Promise<any> {
|
||||
// Implementation here
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
// Array to track all tools
|
||||
const tools: Tool[] = [
|
||||
listDevicesTool,
|
||||
|
||||
Reference in New Issue
Block a user