shoud-cli 1.0.3 → 1.0.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/tools/index.js +24 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shoud-cli",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "SHOUD Terminal Agent: Give your computer a job.",
5
5
  "main": "bin/shoud.js",
6
6
  "bin": {
@@ -3,23 +3,16 @@ const fs = require('fs');
3
3
  const path = require('path');
4
4
  const os = require('os');
5
5
 
6
- // Maximum output size (1 MB)
7
6
  const MAX_OUTPUT_SIZE = 1024 * 1024;
8
-
9
- // Get the project root (current working directory where CLI was started)
10
7
  const PROJECT_ROOT = process.cwd();
11
8
 
12
- // Determine the appropriate shell
13
9
  function getShell() {
14
10
  if (os.platform() === 'win32') {
15
- return 'powershell.exe'; // Use PowerShell on Windows
11
+ return 'powershell.exe';
16
12
  }
17
- return '/bin/bash'; // Use bash on Unix (Linux/macOS)
13
+ return '/bin/bash';
18
14
  }
19
15
 
20
- /**
21
- * Sanitize a file path to ensure it stays within the project root.
22
- */
23
16
  function sanitizePath(inputPath) {
24
17
  const resolved = path.resolve(PROJECT_ROOT, inputPath);
25
18
  if (!resolved.startsWith(PROJECT_ROOT)) {
@@ -28,44 +21,54 @@ function sanitizePath(inputPath) {
28
21
  return resolved;
29
22
  }
30
23
 
31
- /**
32
- * Execute a shell command securely.
33
- * No command whitelist – safety is enforced by the permission engine.
34
- */
35
24
  function executeShell(commandString) {
36
25
  if (!commandString || commandString.trim().length === 0) {
37
26
  throw new Error('Empty command.');
38
27
  }
39
28
 
29
+ // ✅ On Windows, ensure PowerShell outputs text (not objects)
30
+ let finalCommand = commandString;
31
+ if (os.platform() === 'win32') {
32
+ // Convert simple commands to PowerShell-friendly format
33
+ if (commandString.trim() === 'Get-ChildItem') {
34
+ finalCommand = 'Get-ChildItem | Out-String -Width 4096';
35
+ } else if (commandString.trim() === 'dir') {
36
+ finalCommand = 'cmd.exe /c dir';
37
+ } else if (commandString.trim() === 'ls') {
38
+ // ls is not a native PowerShell command – use Get-ChildItem
39
+ finalCommand = 'Get-ChildItem | Out-String -Width 4096';
40
+ }
41
+ }
42
+
40
43
  try {
41
- const output = execSync(commandString, {
44
+ const output = execSync(finalCommand, {
42
45
  cwd: PROJECT_ROOT,
43
46
  encoding: 'utf-8',
44
47
  shell: getShell(),
45
- timeout: 30000, // 30 seconds
48
+ timeout: 30000,
46
49
  maxBuffer: MAX_OUTPUT_SIZE,
47
50
  stdio: 'pipe',
48
51
  });
49
- return output.trim() || 'Command executed successfully with no output.';
52
+ const trimmed = output.trim();
53
+ return trimmed || 'Command executed successfully with no output.';
50
54
  } catch (err) {
51
- // If command fails, return the error message (safe to show to AI)
52
55
  if (err.stderr) {
53
56
  return `Error: ${err.stderr.trim()}`;
54
57
  }
58
+ // If the command fails but there's output, return it
59
+ if (err.stdout) {
60
+ return err.stdout.trim();
61
+ }
55
62
  throw new Error(`Command execution failed: ${err.message}`);
56
63
  }
57
64
  }
58
65
 
59
- /**
60
- * Read a file securely, ensuring it's within the project root.
61
- */
62
66
  function readFile(filePath) {
63
67
  const safePath = sanitizePath(filePath);
64
68
  try {
65
69
  const stats = fs.statSync(safePath);
66
70
  if (!stats.isFile()) throw new Error('Path is not a file.');
67
71
  if (stats.size > MAX_OUTPUT_SIZE) {
68
- // Read only first 1 MB
69
72
  const buffer = Buffer.alloc(MAX_OUTPUT_SIZE);
70
73
  const fd = fs.openSync(safePath, 'r');
71
74
  fs.readSync(fd, buffer, 0, MAX_OUTPUT_SIZE, 0);
@@ -78,9 +81,6 @@ function readFile(filePath) {
78
81
  }
79
82
  }
80
83
 
81
- /**
82
- * Write content to a file securely, ensuring it's within the project root.
83
- */
84
84
  function writeFile(filePath, content) {
85
85
  const safePath = sanitizePath(filePath);
86
86
  try {
@@ -95,10 +95,6 @@ function writeFile(filePath, content) {
95
95
  }
96
96
  }
97
97
 
98
- /**
99
- * Main tool dispatcher.
100
- * Returns the result as a string (to be sent back to the AI).
101
- */
102
98
  function executeTool(toolName, input) {
103
99
  try {
104
100
  switch (toolName) {
@@ -112,7 +108,6 @@ function executeTool(toolName, input) {
112
108
  throw new Error(`Tool "${toolName}" is not supported.`);
113
109
  }
114
110
  } catch (error) {
115
- // Return a generic error message to the AI (don't leak internal details)
116
111
  return `Tool execution failed: ${error.message}`;
117
112
  }
118
113
  }