protoagent 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,342 @@
1
+ import { spawn } from 'child_process';
2
+ import inquirer from 'inquirer';
3
+ // Current working directory for file operations
4
+ const workingDirectory = process.cwd();
5
+ // Global flags and session state
6
+ let globalConfig = null;
7
+ let dangerouslyAcceptAll = false;
8
+ let approvedCommandsForSession = new Set();
9
+ // Whitelisted safe commands that don't require confirmation
10
+ const SAFE_COMMANDS = [
11
+ 'ls', 'dir', 'pwd', 'whoami', 'date', 'echo', 'cat', 'head', 'tail',
12
+ 'grep', 'find', 'wc', 'sort', 'uniq', 'cut', 'awk', 'sed',
13
+ 'git status', 'git log', 'git diff', 'git branch', 'git show',
14
+ 'npm list', 'npm ls', 'yarn list', 'node --version', 'npm --version',
15
+ 'python --version', 'python3 --version', 'which', 'type', 'file'
16
+ ];
17
+ export function setShellConfig(config) {
18
+ globalConfig = config;
19
+ }
20
+ export function setDangerouslyAcceptAll(accept) {
21
+ dangerouslyAcceptAll = accept;
22
+ }
23
+ export async function runShellCommand(command, args = [], timeoutMs = 30000) {
24
+ return await runShellCommandWithRetry(command, args, timeoutMs, 0);
25
+ }
26
+ async function runShellCommandWithRetry(command, args = [], timeoutMs = 30000, retryCount = 0) {
27
+ const maxRetries = 2;
28
+ try {
29
+ // Security: Basic validation to prevent obviously dangerous commands
30
+ const dangerousCommands = ['rm -rf', 'sudo', 'su', 'chmod 777', 'dd if=', 'mkfs', 'fdisk', 'format'];
31
+ const fullCommand = `${command} ${args.join(' ')}`.toLowerCase();
32
+ for (const dangerous of dangerousCommands) {
33
+ if (fullCommand.includes(dangerous)) {
34
+ throw new Error(`Command contains potentially dangerous operation: ${dangerous}`);
35
+ }
36
+ }
37
+ const commandString = `${command} ${args.join(' ')}`;
38
+ const baseCommand = command.toLowerCase();
39
+ // Check if we should auto-approve (only show approval prompt on first attempt)
40
+ if (retryCount === 0) {
41
+ if (dangerouslyAcceptAll) {
42
+ console.log(`šŸš€ Auto-executing (--dangerously-accept-all): ${commandString}`);
43
+ }
44
+ else if (approvedCommandsForSession.has(baseCommand)) {
45
+ console.log(`šŸš€ Auto-executing (${baseCommand} approved for session): ${commandString}`);
46
+ }
47
+ else {
48
+ // Check if it's a safe command
49
+ const isSafeCommand = SAFE_COMMANDS.some(safe => {
50
+ const normalizedSafe = safe.toLowerCase();
51
+ const normalizedFull = fullCommand.toLowerCase();
52
+ return normalizedFull === normalizedSafe ||
53
+ (normalizedSafe.includes(' ') && normalizedFull.startsWith(normalizedSafe)) ||
54
+ (!normalizedSafe.includes(' ') && normalizedFull.split(' ')[0] === normalizedSafe);
55
+ });
56
+ if (isSafeCommand) {
57
+ console.log(`🟢 Executing safe command: ${commandString}`);
58
+ }
59
+ else {
60
+ // Require user confirmation with enhanced options
61
+ console.log(`\nšŸ” Shell Command Requested: ${commandString}`);
62
+ console.log(`šŸ“ Working Directory: ${workingDirectory}`);
63
+ const { choice } = await inquirer.prompt([
64
+ {
65
+ type: 'list',
66
+ name: 'choice',
67
+ message: 'Choose your action:',
68
+ choices: [
69
+ { name: '1. āœ… Execute this command now', value: 'execute' },
70
+ { name: `2. āœ… Execute and approve all "${baseCommand}" commands for this session`, value: 'approve_session' },
71
+ { name: '3. āŒ Cancel and suggest alternative', value: 'cancel' }
72
+ ],
73
+ default: 'execute'
74
+ }
75
+ ]);
76
+ switch (choice) {
77
+ case 'execute':
78
+ console.log(`šŸš€ Executing: ${commandString}`);
79
+ break;
80
+ case 'approve_session':
81
+ approvedCommandsForSession.add(baseCommand);
82
+ console.log(`šŸ”“ "${baseCommand}" approved for session - all future ${baseCommand} commands will auto-execute`);
83
+ console.log(`šŸš€ Executing: ${commandString}`);
84
+ break;
85
+ case 'cancel':
86
+ return getSuggestion(commandString);
87
+ default:
88
+ return `Command execution cancelled by user: ${commandString}`;
89
+ }
90
+ }
91
+ }
92
+ }
93
+ else {
94
+ console.log(`šŸ”„ Retry attempt ${retryCount}/${maxRetries}: ${commandString}`);
95
+ }
96
+ return new Promise((resolve, reject) => {
97
+ const child = spawn(command, args, {
98
+ cwd: workingDirectory,
99
+ stdio: ['pipe', 'pipe', 'pipe'], // Capture all input/output for AI processing
100
+ shell: true,
101
+ env: { ...process.env, PATH: process.env.PATH }
102
+ });
103
+ let stdout = '';
104
+ let stderr = '';
105
+ let completed = false;
106
+ // Set up timeout
107
+ const timeout = setTimeout(() => {
108
+ if (!completed) {
109
+ completed = true;
110
+ child.kill('SIGTERM');
111
+ reject(new Error(`TIMEOUT_ERROR:${timeoutMs}`));
112
+ }
113
+ }, timeoutMs);
114
+ child.stdout?.on('data', (data) => {
115
+ stdout += data.toString();
116
+ });
117
+ child.stderr?.on('data', (data) => {
118
+ stderr += data.toString();
119
+ });
120
+ child.on('close', (code) => {
121
+ if (!completed) {
122
+ completed = true;
123
+ clearTimeout(timeout);
124
+ const output = stdout + (stderr ? `\nSTDERR:\n${stderr}` : '');
125
+ if (code === 0) {
126
+ resolve(`Command executed successfully (exit code: ${code})\n\nOutput:\n${output || '(no output)'}`);
127
+ }
128
+ else {
129
+ // For non-zero exit codes, still return the output but indicate the failure
130
+ resolve(`Command exited with code ${code}\n\nOutput:\n${output || '(no output)'}`);
131
+ }
132
+ }
133
+ });
134
+ child.on('error', (error) => {
135
+ if (!completed) {
136
+ completed = true;
137
+ clearTimeout(timeout);
138
+ reject(new Error(`Failed to execute command: ${error.message}`));
139
+ }
140
+ });
141
+ });
142
+ }
143
+ catch (error) {
144
+ if (error instanceof Error) {
145
+ // Check if this is a timeout error and we haven't exceeded max retries
146
+ if (error.message.startsWith('TIMEOUT_ERROR:') && retryCount < maxRetries) {
147
+ const timeoutDuration = parseInt(error.message.split(':')[1]);
148
+ return await analyzeTimeoutAndRetry(command, args, timeoutDuration, retryCount);
149
+ }
150
+ throw new Error(`Shell command error: ${error.message.replace('TIMEOUT_ERROR:', 'Command timed out after ').replace(/:\d+/, 'ms')}`);
151
+ }
152
+ throw new Error('Shell command error: Unknown error');
153
+ }
154
+ }
155
+ async function analyzeTimeoutAndRetry(command, args = [], originalTimeout, retryCount) {
156
+ const commandString = `${command} ${args.join(' ')}`;
157
+ console.log(`ā±ļø Command timed out after ${originalTimeout}ms: ${commandString}`);
158
+ console.log(`šŸ” Analyzing timeout cause and preparing retry...`);
159
+ // Analyze the command to understand why it might have timed out
160
+ const analysis = analyzeCommandForTimeout(command, args);
161
+ console.log(`šŸ’” Timeout analysis: ${analysis.reason}`);
162
+ if (analysis.suggestedArgs.length > 0) {
163
+ console.log(`šŸ”§ Suggested fix: ${command} ${analysis.suggestedArgs.join(' ')}`);
164
+ // Try with suggested arguments
165
+ return await runShellCommandWithRetry(command, analysis.suggestedArgs, analysis.suggestedTimeout, retryCount + 1);
166
+ }
167
+ else if (analysis.suggestedTimeout > originalTimeout) {
168
+ console.log(`ā° Increasing timeout to ${analysis.suggestedTimeout}ms for long-running operation`);
169
+ // Try with longer timeout
170
+ return await runShellCommandWithRetry(command, args, analysis.suggestedTimeout, retryCount + 1);
171
+ }
172
+ else {
173
+ // No specific fix found, provide analysis
174
+ return `Command timed out after ${originalTimeout}ms: ${commandString}\n\nTimeout Analysis: ${analysis.reason}\n\nSuggestion: ${analysis.suggestion}`;
175
+ }
176
+ }
177
+ function analyzeCommandForTimeout(command, args) {
178
+ const fullCommand = `${command} ${args.join(' ')}`.toLowerCase();
179
+ const baseCommand = command.toLowerCase();
180
+ // Interactive command detection
181
+ if (baseCommand === 'npm' && args.length > 0) {
182
+ const npmSubcommand = args[0].toLowerCase();
183
+ if (npmSubcommand === 'create' || npmSubcommand === 'init') {
184
+ // Check if template is specified for npm create
185
+ if (npmSubcommand === 'create' && !args.some(arg => arg.includes('--template'))) {
186
+ return {
187
+ reason: "npm create command likely waiting for interactive template selection",
188
+ suggestion: "Add --template flag to avoid interactive prompts",
189
+ suggestedArgs: [...args, '--template', 'vanilla'],
190
+ suggestedTimeout: 60000
191
+ };
192
+ }
193
+ // Check for other interactive npm commands
194
+ if (!args.some(arg => arg.includes('-y') || arg.includes('--yes'))) {
195
+ return {
196
+ reason: "npm command likely waiting for interactive confirmation",
197
+ suggestion: "Add -y flag to auto-confirm prompts",
198
+ suggestedArgs: [...args, '-y'],
199
+ suggestedTimeout: 60000
200
+ };
201
+ }
202
+ }
203
+ if (npmSubcommand === 'install' || npmSubcommand === 'i') {
204
+ return {
205
+ reason: "npm install operations can take a long time",
206
+ suggestion: "Increase timeout for package installation",
207
+ suggestedArgs: args,
208
+ suggestedTimeout: 120000 // 2 minutes
209
+ };
210
+ }
211
+ }
212
+ // Git operations
213
+ if (baseCommand === 'git') {
214
+ const gitSubcommand = args[0]?.toLowerCase();
215
+ if (['clone', 'pull', 'push', 'fetch'].includes(gitSubcommand)) {
216
+ return {
217
+ reason: "Git network operations can be slow",
218
+ suggestion: "Increase timeout for network operations",
219
+ suggestedArgs: args,
220
+ suggestedTimeout: 90000 // 1.5 minutes
221
+ };
222
+ }
223
+ if (gitSubcommand === 'commit' && !args.some(arg => arg.includes('-m'))) {
224
+ return {
225
+ reason: "Git commit without -m flag opens interactive editor",
226
+ suggestion: "Add commit message with -m flag",
227
+ suggestedArgs: [...args, '-m', '"Auto-commit"'],
228
+ suggestedTimeout: 30000
229
+ };
230
+ }
231
+ }
232
+ // Yarn operations
233
+ if (baseCommand === 'yarn') {
234
+ if (args.length === 0 || args[0] === 'install') {
235
+ return {
236
+ reason: "Yarn install operations can take a long time",
237
+ suggestion: "Increase timeout for package installation",
238
+ suggestedArgs: args,
239
+ suggestedTimeout: 120000 // 2 minutes
240
+ };
241
+ }
242
+ if (args[0] === 'create' && !args.some(arg => arg.includes('--template'))) {
243
+ return {
244
+ reason: "yarn create command likely waiting for interactive input",
245
+ suggestion: "Add template specification to avoid prompts",
246
+ suggestedArgs: [...args, '--template', 'default'],
247
+ suggestedTimeout: 60000
248
+ };
249
+ }
250
+ }
251
+ // Build operations
252
+ if (['build', 'compile', 'webpack', 'rollup', 'vite'].includes(baseCommand)) {
253
+ return {
254
+ reason: "Build operations often require more time",
255
+ suggestion: "Increase timeout for build processes",
256
+ suggestedArgs: args,
257
+ suggestedTimeout: 120000 // 2 minutes
258
+ };
259
+ }
260
+ // Testing operations
261
+ if (['test', 'jest', 'mocha', 'karma'].includes(baseCommand) ||
262
+ (baseCommand === 'npm' && args[0] === 'test')) {
263
+ return {
264
+ reason: "Test operations can take significant time",
265
+ suggestion: "Increase timeout for test execution",
266
+ suggestedArgs: args,
267
+ suggestedTimeout: 90000 // 1.5 minutes
268
+ };
269
+ }
270
+ // Python operations
271
+ if (['python', 'python3', 'pip', 'pip3'].includes(baseCommand)) {
272
+ if (args.some(arg => arg.includes('install'))) {
273
+ return {
274
+ reason: "Python package installation can be slow",
275
+ suggestion: "Increase timeout for package installation",
276
+ suggestedArgs: args,
277
+ suggestedTimeout: 120000 // 2 minutes
278
+ };
279
+ }
280
+ }
281
+ // Default analysis for unknown timeouts
282
+ return {
283
+ reason: "Command exceeded default timeout, possibly due to network delays, large operations, or interactive prompts",
284
+ suggestion: "Try adding flags to make the command non-interactive (like -y, --yes, --no-interactive) or check if the command is waiting for user input",
285
+ suggestedArgs: [],
286
+ suggestedTimeout: 60000 // 1 minute default increase
287
+ };
288
+ }
289
+ // Helper function to provide suggestions for cancelled commands
290
+ function getSuggestion(commandString) {
291
+ const suggestions = {
292
+ 'rm': 'Consider using file tools like write_file or edit_file to manage files safely',
293
+ 'sudo': 'ProtoAgent runs with your user permissions only for security',
294
+ 'npm install': 'Try: Use the file tools to examine package.json first, then I can suggest safer alternatives',
295
+ 'npm create': 'Add flags to avoid interactive prompts: npm create vite@latest my-app --template react',
296
+ 'git push': 'Consider: First check git status, then review changes before pushing',
297
+ 'chmod': 'Consider: Use file tools to check permissions first, specific chmod may not be needed'
298
+ };
299
+ const cmd = commandString.split(' ')[0].toLowerCase();
300
+ const fullCmd = commandString.toLowerCase();
301
+ // Check for interactive command patterns
302
+ if (fullCmd.includes('npm create') && !fullCmd.includes('--template')) {
303
+ return `Command cancelled: ${commandString}\n\nšŸ’” Suggestion: npm create commands require template specification to avoid interactive prompts. Try: npm create vite@latest my-app --template react\n\nAvailable templates: vanilla, vue, react, preact, lit, svelte, solid, qwik, angular`;
304
+ }
305
+ if (fullCmd.includes('git commit') && !fullCmd.includes('-m')) {
306
+ return `Command cancelled: ${commandString}\n\nšŸ’” Suggestion: git commit requires a message flag to avoid opening an interactive editor. Try: git commit -m "Your commit message"`;
307
+ }
308
+ const suggestion = suggestions[cmd] || suggestions[fullCmd.split(' ').slice(0, 2).join(' ')] ||
309
+ 'Consider using the available file system tools (read_file, write_file, list_directory) for safer operations, or add flags to make commands non-interactive';
310
+ return `Command cancelled: ${commandString}\n\nšŸ’” Suggestion: ${suggestion}\n\nYou can:\n- Use 'protoagent --dangerously-accept-all' to auto-approve all commands\n- Choose option 2 next time to approve commands for the session\n- Add flags to make commands non-interactive (e.g., --template, --yes, --no-interactive, -m for git)\n- ProtoAgent can automatically retry timed-out commands with better parameters\n- Ask me to break down the task into safer operations`;
311
+ }
312
+ // Tool definitions
313
+ export const shellTools = [
314
+ {
315
+ type: 'function',
316
+ function: {
317
+ name: 'run_shell_command',
318
+ description: 'Execute a shell command in the current working directory. Commands run non-interactively and output is captured for analysis. For tools that normally prompt for input (like npm create), provide all necessary flags to avoid interactive prompts. Safe commands (ls, find, grep, git status, etc.) run automatically. Other commands may require user confirmation unless running with --dangerously-accept-all flag. Examples: find . -name "*.js", grep -r "TODO" ., npm create vite@latest my-app --template react --no-interactive',
319
+ parameters: {
320
+ type: 'object',
321
+ properties: {
322
+ command: {
323
+ type: 'string',
324
+ description: 'The command to execute. Examples: "find", "grep", "ls", "git", "npm", "python", "node", "yarn"'
325
+ },
326
+ args: {
327
+ type: 'array',
328
+ items: {
329
+ type: 'string'
330
+ },
331
+ description: 'Arguments to pass to the command. Examples: [".", "-name", "*.js"] for find, ["-r", "TODO", "."] for grep, ["-la"] for ls'
332
+ },
333
+ timeout_ms: {
334
+ type: 'integer',
335
+ description: 'Timeout in milliseconds for the command execution. Default is 30000 (30 seconds). Use higher values for long-running operations.'
336
+ }
337
+ },
338
+ required: ['command']
339
+ }
340
+ }
341
+ }
342
+ ];
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Todo management tool for ProtoAgent
3
+ */
4
+ import { z } from 'zod';
5
+ // Define the todo item schema
6
+ const TodoItemSchema = z.object({
7
+ id: z.string().describe('Unique identifier for the todo item'),
8
+ content: z.string().describe('Brief description of the task'),
9
+ status: z.enum(['pending', 'in_progress', 'completed', 'cancelled']).describe('Current status of the task'),
10
+ priority: z.enum(['high', 'medium', 'low']).describe('Priority level of the task'),
11
+ created: z.string().optional().describe('Creation timestamp'),
12
+ updated: z.string().optional().describe('Last update timestamp')
13
+ });
14
+ // In-memory storage for todo items (could be persisted to file later)
15
+ const todoStorage = {};
16
+ /**
17
+ * Get current session ID (simplified - could use actual session management)
18
+ */
19
+ function getCurrentSessionId() {
20
+ return 'default_session';
21
+ }
22
+ /**
23
+ * Generate a unique ID for todo items
24
+ */
25
+ function generateTodoId() {
26
+ return `todo_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
27
+ }
28
+ /**
29
+ * Tool for reading the current todo list
30
+ */
31
+ export const todoReadTool = {
32
+ type: 'function',
33
+ function: {
34
+ name: 'todo_read',
35
+ description: 'Read the current todo list to see all tasks and their status',
36
+ parameters: {
37
+ type: 'object',
38
+ properties: {},
39
+ required: []
40
+ }
41
+ }
42
+ };
43
+ /**
44
+ * Tool for writing/updating the todo list
45
+ */
46
+ export const todoWriteTool = {
47
+ type: 'function',
48
+ function: {
49
+ name: 'todo_write',
50
+ description: 'Update the todo list by providing a complete list of todos with their current status. Use this to add new todos, update existing ones, or mark items as completed.',
51
+ parameters: {
52
+ type: 'object',
53
+ properties: {
54
+ todos: {
55
+ type: 'array',
56
+ description: 'The complete updated todo list',
57
+ items: {
58
+ type: 'object',
59
+ properties: {
60
+ id: {
61
+ type: 'string',
62
+ description: 'Unique identifier for the todo item. Use existing ID to update, or provide new ID for new items.'
63
+ },
64
+ content: {
65
+ type: 'string',
66
+ description: 'Brief description of the task'
67
+ },
68
+ status: {
69
+ type: 'string',
70
+ enum: ['pending', 'in_progress', 'completed', 'cancelled'],
71
+ description: 'Current status of the task'
72
+ },
73
+ priority: {
74
+ type: 'string',
75
+ enum: ['high', 'medium', 'low'],
76
+ description: 'Priority level of the task'
77
+ }
78
+ },
79
+ required: ['id', 'content', 'status', 'priority']
80
+ }
81
+ }
82
+ },
83
+ required: ['todos']
84
+ }
85
+ }
86
+ };
87
+ /**
88
+ * Handle todo read operation
89
+ */
90
+ export async function handleTodoRead() {
91
+ const sessionId = getCurrentSessionId();
92
+ const todos = todoStorage[sessionId] || [];
93
+ if (todos.length === 0) {
94
+ return 'Todo list is empty. No tasks found.';
95
+ }
96
+ const activeTodos = todos.filter(t => t.status !== 'completed');
97
+ const completedTodos = todos.filter(t => t.status === 'completed');
98
+ let result = `Todo List (${activeTodos.length} active, ${completedTodos.length} completed):\n\n`;
99
+ // Show active todos first
100
+ if (activeTodos.length > 0) {
101
+ result += 'šŸ“‹ ACTIVE TASKS:\n';
102
+ activeTodos.forEach((todo, index) => {
103
+ const priorityIcon = todo.priority === 'high' ? 'šŸ”“' : todo.priority === 'medium' ? '🟔' : '🟢';
104
+ const statusIcon = todo.status === 'in_progress' ? 'šŸ”„' : 'ā³';
105
+ result += `${index + 1}. ${statusIcon} ${priorityIcon} ${todo.content} [${todo.status.toUpperCase()}]\n`;
106
+ result += ` ID: ${todo.id}\n\n`;
107
+ });
108
+ }
109
+ // Show completed todos
110
+ if (completedTodos.length > 0) {
111
+ result += 'āœ… COMPLETED TASKS:\n';
112
+ completedTodos.forEach((todo, index) => {
113
+ result += `${index + 1}. āœ… ${todo.content}\n`;
114
+ result += ` ID: ${todo.id}\n\n`;
115
+ });
116
+ }
117
+ return result.trim();
118
+ }
119
+ /**
120
+ * Handle todo write operation
121
+ */
122
+ export async function handleTodoWrite(todos) {
123
+ const sessionId = getCurrentSessionId();
124
+ try {
125
+ // Validate and process todos
126
+ const validatedTodos = [];
127
+ const timestamp = new Date().toISOString();
128
+ for (const todo of todos) {
129
+ // Parse and validate each todo item
130
+ const parsed = TodoItemSchema.parse({
131
+ ...todo,
132
+ updated: timestamp,
133
+ created: todo.created || timestamp
134
+ });
135
+ validatedTodos.push(parsed);
136
+ }
137
+ // Update storage
138
+ todoStorage[sessionId] = validatedTodos;
139
+ // Generate summary
140
+ const activeTodos = validatedTodos.filter(t => t.status !== 'completed');
141
+ const completedTodos = validatedTodos.filter(t => t.status === 'completed');
142
+ const highPriorityTodos = activeTodos.filter(t => t.priority === 'high');
143
+ let result = `āœ… Todo list updated successfully!\n\n`;
144
+ result += `šŸ“Š Summary:\n`;
145
+ result += `- Total tasks: ${validatedTodos.length}\n`;
146
+ result += `- Active tasks: ${activeTodos.length}\n`;
147
+ result += `- Completed tasks: ${completedTodos.length}\n`;
148
+ result += `- High priority tasks: ${highPriorityTodos.length}\n\n`;
149
+ if (highPriorityTodos.length > 0) {
150
+ result += `šŸ”“ HIGH PRIORITY TASKS:\n`;
151
+ highPriorityTodos.forEach((todo, index) => {
152
+ const statusIcon = todo.status === 'in_progress' ? 'šŸ”„' : 'ā³';
153
+ result += `${index + 1}. ${statusIcon} ${todo.content}\n`;
154
+ });
155
+ }
156
+ return result.trim();
157
+ }
158
+ catch (error) {
159
+ return `āŒ Error updating todo list: ${error instanceof Error ? error.message : 'Unknown error'}`;
160
+ }
161
+ }
162
+ /**
163
+ * Main todo tool handler
164
+ */
165
+ export async function handleTodoTool(toolName, args) {
166
+ switch (toolName) {
167
+ case 'todo_read':
168
+ return await handleTodoRead();
169
+ case 'todo_write':
170
+ if (!args.todos || !Array.isArray(args.todos)) {
171
+ return 'āŒ Error: todos parameter must be an array';
172
+ }
173
+ return await handleTodoWrite(args.todos);
174
+ default:
175
+ return `āŒ Unknown todo tool: ${toolName}`;
176
+ }
177
+ }
@@ -0,0 +1,125 @@
1
+ import fs from 'fs/promises';
2
+ import path from 'path';
3
+ // Current working directory for file operations
4
+ const workingDirectory = process.cwd();
5
+ // Security utilities
6
+ function normalizePath(p) {
7
+ return path.normalize(p);
8
+ }
9
+ async function validatePath(requestedPath) {
10
+ const absolute = path.isAbsolute(requestedPath)
11
+ ? path.resolve(requestedPath)
12
+ : path.resolve(workingDirectory, requestedPath);
13
+ const normalizedRequested = normalizePath(absolute);
14
+ // Check if path is within working directory
15
+ if (!normalizedRequested.startsWith(workingDirectory)) {
16
+ throw new Error(`Access denied - path outside working directory: ${absolute}`);
17
+ }
18
+ // Handle symlinks by checking their real path
19
+ try {
20
+ const realPath = await fs.realpath(absolute);
21
+ const normalizedReal = normalizePath(realPath);
22
+ if (!normalizedReal.startsWith(workingDirectory)) {
23
+ throw new Error(`Access denied - symlink target outside working directory: ${realPath}`);
24
+ }
25
+ return realPath;
26
+ }
27
+ catch (error) {
28
+ // For new files that don't exist yet, verify parent directory
29
+ if (error.code === 'ENOENT') {
30
+ const parentDir = path.dirname(absolute);
31
+ try {
32
+ const realParentPath = await fs.realpath(parentDir);
33
+ const normalizedParent = normalizePath(realParentPath);
34
+ if (!normalizedParent.startsWith(workingDirectory)) {
35
+ throw new Error(`Access denied - parent directory outside working directory: ${realParentPath}`);
36
+ }
37
+ return absolute;
38
+ }
39
+ catch {
40
+ throw new Error(`Parent directory does not exist: ${parentDir}`);
41
+ }
42
+ }
43
+ throw error;
44
+ }
45
+ }
46
+ // Directory tree utility function
47
+ async function buildDirectoryTree(dirPath, prefix = '', isLast = true, maxDepth = 5, currentDepth = 0) {
48
+ if (currentDepth >= maxDepth) {
49
+ return '';
50
+ }
51
+ const validPath = await validatePath(dirPath);
52
+ const stats = await fs.stat(validPath);
53
+ if (!stats.isDirectory()) {
54
+ return '';
55
+ }
56
+ let result = '';
57
+ const entries = await fs.readdir(validPath, { withFileTypes: true });
58
+ // Sort entries: directories first, then files, alphabetically
59
+ entries.sort((a, b) => {
60
+ if (a.isDirectory() && !b.isDirectory())
61
+ return -1;
62
+ if (!a.isDirectory() && b.isDirectory())
63
+ return 1;
64
+ return a.name.localeCompare(b.name);
65
+ });
66
+ for (let i = 0; i < entries.length; i++) {
67
+ const entry = entries[i];
68
+ const isLastEntry = i === entries.length - 1;
69
+ const currentPrefix = isLastEntry ? '└── ' : 'ā”œā”€ā”€ ';
70
+ const nextPrefix = prefix + (isLastEntry ? ' ' : '│ ');
71
+ result += `${prefix}${currentPrefix}${entry.name}${entry.isDirectory() ? '/' : ''}\n`;
72
+ if (entry.isDirectory()) {
73
+ const subPath = path.join(dirPath, entry.name);
74
+ try {
75
+ result += await buildDirectoryTree(subPath, nextPrefix, isLastEntry, maxDepth, currentDepth + 1);
76
+ }
77
+ catch (error) {
78
+ // Skip directories we can't read (permissions, etc.)
79
+ result += `${nextPrefix}└── [Permission Denied]\n`;
80
+ }
81
+ }
82
+ }
83
+ return result;
84
+ }
85
+ export async function viewDirectoryTree(dirPath, maxDepth = 5) {
86
+ try {
87
+ const validPath = await validatePath(dirPath);
88
+ // Check if path exists and is a directory
89
+ const stats = await fs.stat(validPath);
90
+ if (!stats.isDirectory()) {
91
+ throw new Error('Path is not a directory');
92
+ }
93
+ const relativePath = path.relative(workingDirectory, validPath) || '.';
94
+ const tree = await buildDirectoryTree(validPath, '', true, maxDepth);
95
+ return `Directory tree for ${relativePath}:\n${relativePath}/\n${tree}`;
96
+ }
97
+ catch (error) {
98
+ if (error instanceof Error) {
99
+ throw new Error(`Failed to view directory tree: ${error.message}`);
100
+ }
101
+ throw new Error('Failed to view directory tree: Unknown error');
102
+ }
103
+ }
104
+ // Tool definition
105
+ export const viewDirectoryTreeTool = {
106
+ type: 'function',
107
+ function: {
108
+ name: 'view_directory_tree',
109
+ description: 'Display the directory tree of a specified directory up to a certain depth. This provides a visual overview of the directory structure, showing nested folders and files. Useful for understanding project layout or finding files.',
110
+ parameters: {
111
+ type: 'object',
112
+ properties: {
113
+ directory_path: {
114
+ type: 'string',
115
+ description: 'The path to the directory to display, relative to the current working directory. Examples: "src", "docs"'
116
+ },
117
+ max_depth: {
118
+ type: 'integer',
119
+ description: 'The maximum depth to display in the directory tree. Use a higher number to show more levels of the tree.'
120
+ }
121
+ },
122
+ required: ['directory_path']
123
+ }
124
+ }
125
+ };