Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import * as vscode from 'vscode';
import { EventEmitter } from 'events';
import { IToolManager } from '../interfaces/IToolManager';
import { Tool, ToolResult, ToolInvocation } from '../types/Tool';
import { PermissionLevel } from '../types/Permission';
export class ToolManager extends EventEmitter implements IToolManager {
private tools: Map<string, Tool> = new Map();
private invocationHistory: ToolInvocation[] = [];
private permissionLevel: PermissionLevel = PermissionLevel.Normal;
constructor(private context: vscode.ExtensionContext) {
super();
}
registerTool(name: string, tool: Tool): void {
if (this.tools.has(name)) {
throw new Error(`Tool ${name} already registered`);
}
this.tools.set(name, tool);
this.emit('tool-registered', name);
}
async registerDefaultTools(): Promise<void> {
// Register file system tool
this.registerTool('filesystem', {
name: 'filesystem',
description: 'File system operations',
parameters: [
{
name: 'action',
type: 'string',
description: 'Action to perform (read, write, delete)',
required: true
},
{
name: 'path',
type: 'string',
description: 'File path',
required: true
}
],
execute: async (args) => {
// Simplified implementation for testing
return {
success: true,
output: `Executed ${args.action} on ${args.path}`,
duration: 10
};
}
});
// Register terminal tool
this.registerTool('terminal', {
name: 'terminal',
description: 'Terminal command execution',
parameters: [
{
name: 'command',
type: 'string',
description: 'Command to execute',
required: true
}
],
execute: async (args) => {
return {
success: true,
output: `Executed: ${args.command}`,
duration: 100
};
}
});
}
getRegisteredTools(): string[] {
return Array.from(this.tools.keys());
}
async invokeTool(name: string, args: any): Promise<ToolResult> {
const tool = this.tools.get(name);
if (!tool) {
throw new Error(`Tool ${name} not found`);
}
const invocation: ToolInvocation = {
id: Math.random().toString(),
tool: name,
action: args.action || 'execute',
args,
timestamp: Date.now(),
permitted: false
};
// Check permission
if (!this.checkPermission(name, args.action || 'execute')) {
invocation.result = {
success: false,
error: `Permission denied for ${name}.${args.action || 'execute'}`,
duration: 0
};
this.invocationHistory.push(invocation);
this.emit('tool-invocation-denied', invocation);
throw new Error(invocation.result.error);
}
invocation.permitted = true;
const startTime = Date.now();
try {
const result = await tool.execute(args);
invocation.result = result;
invocation.duration = Date.now() - startTime;
this.invocationHistory.push(invocation);
this.emit('tool-invoked', invocation);
return result;
} catch (error) {
const errorResult: ToolResult = {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
duration: Date.now() - startTime
};
invocation.result = errorResult;
invocation.duration = Date.now() - startTime;
this.invocationHistory.push(invocation);
this.emit('tool-error', invocation);
throw error;
}
}
checkPermission(tool: string, action: string): boolean {
// Destructive actions
const destructiveActions = ['delete', 'remove', 'uninstall', 'format'];
const isDestructive = destructiveActions.includes(action.toLowerCase());
switch (this.permissionLevel) {
case PermissionLevel.Restricted:
// Always require permission
return false;
case PermissionLevel.Normal:
// Require permission for destructive actions
return !isDestructive;
case PermissionLevel.Autonomous:
// No permission required
return true;
default:
return false;
}
}
setPermissionLevel(level: PermissionLevel): void {
this.permissionLevel = level;
this.emit('permission-level-changed', level);
}
getInvocationHistory(): ToolInvocation[] {
return [...this.invocationHistory];
}
clearHistory(): void {
this.invocationHistory = [];
this.emit('history-cleared');
}
getTool(name: string): Tool | undefined {
return this.tools.get(name);
}
dispose(): void {
this.removeAllListeners();
this.tools.clear();
this.invocationHistory = [];
}
} |