@fefeding/aicmd 0.1.2 → 0.1.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.
package/README.md CHANGED
@@ -195,9 +195,31 @@ All data is stored locally on the server:
195
195
  | AI Config | `~/.aicmd/ai-config.json` |
196
196
  | Chat History | `~/.aicmd/ai-history/` |
197
197
  | User Skills | `~/.aicmd/skills/*.md` |
198
+ | Trash Bin | `~/.aicmd/.trash/` |
198
199
 
199
200
  Use `AICMD_DATA_DIR` environment variable to override the data directory.
200
201
 
202
+ ## Safety Mechanisms
203
+
204
+ The AI agent includes built-in command-level safety protections to reduce the risk of destructive operations:
205
+
206
+ ### Delete Protection (Trash Bin)
207
+ - All `rm` commands (Linux/macOS) are automatically rewritten to `mv` into `~/.aicmd/.trash/` instead of permanent deletion.
208
+ - Windows `Remove-Item`/`del`/`rd` commands are similarly rewritten to `Move-Item` into `%USERPROFILE%\.aicmd\.trash\`.
209
+ - Trashed files are timestamped (e.g. `_del_1716000000_filename`) to avoid naming conflicts.
210
+ - To recover a file, simply browse the trash directory and move it back.
211
+
212
+ ### Dangerous Operation Blocking
213
+ The following irreversible, destructive operations are blocked entirely:
214
+
215
+ | Platform | Blocked Operations |
216
+ |----------|-------------------|
217
+ | Linux/macOS | `rm -rf /`, `mkfs.*` (disk formatting), `dd of=/dev/sd*` (disk overwrite), fork bombs |
218
+ | Windows | `format C:`, `Clear-Disk`, `Remove-Item C:\`, `rd /s C:\` |
219
+
220
+ ### Platform Awareness
221
+ The safety layer automatically detects the target session's OS (via systemContext) and applies the appropriate Unix or Windows rules — no manual configuration required.
222
+
201
223
  ## Cross-Platform Support
202
224
 
203
225
  The terminal and AI agent work on:
package/README.zh.md CHANGED
@@ -195,9 +195,31 @@ tags: [deploy, ops]
195
195
  | AI 配置 | `~/.aicmd/ai-config.json` |
196
196
  | 对话历史 | `~/.aicmd/ai-history/` |
197
197
  | 用户 Skills | `~/.aicmd/skills/*.md` |
198
+ | 回收站 | `~/.aicmd/.trash/` |
198
199
 
199
200
  可通过 `AICMD_DATA_DIR` 环境变量自定义数据目录。
200
201
 
202
+ ## 安全机制
203
+
204
+ AI Agent 内置了命令级安全防护,降低误操作风险:
205
+
206
+ ### 删除保护(回收站)
207
+ - 所有 `rm` 命令(Linux/macOS)自动改写为 `mv` 到 `~/.aicmd/.trash/`,而非永久删除
208
+ - Windows 的 `Remove-Item`/`del`/`rd` 同样改写为 `Move-Item` 到 `%USERPROFILE%\.aicmd\.trash\`
209
+ - 回收站文件带时间戳命名(如 `_del_1716000000_filename`),不会冲突
210
+ - 需要恢复时,直接到回收站目录找回即可
211
+
212
+ ### 危险操作阻止
213
+ 以下不可恢复的破坏性操作会被直接阻止执行:
214
+
215
+ | 平台 | 被阻止的操作 |
216
+ |------|-------------|
217
+ | Linux/macOS | `rm -rf /`、`mkfs.*` 格式化磁盘、`dd of=/dev/sd*` 覆写磁盘、Fork bomb |
218
+ | Windows | `format C:`、`Clear-Disk`、`Remove-Item C:\`、`rd /s C:\` |
219
+
220
+ ### 平台感知
221
+ 安全防护自动检测目标会话的操作系统(通过 systemContext),分别应用 Unix 或 Windows 规则,无需手动配置。
222
+
201
223
  ## 跨平台支持
202
224
 
203
225
  终端和 AI Agent 支持以下平台:
package/bin/aicmd.js CHANGED
@@ -8,11 +8,53 @@ const os = require('os');
8
8
 
9
9
  const projectRoot = path.resolve(__dirname, '..');
10
10
 
11
- function getPidFilePath() {
11
+ /**
12
+ * 递归修复目录及所有文件权限:目录 755,文件 644
13
+ */
14
+ function fixPermissions(dir) {
15
+ try {
16
+ fs.chmodSync(dir, 0o755);
17
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
18
+ for (const entry of entries) {
19
+ const fullPath = path.join(dir, entry.name);
20
+ try {
21
+ if (entry.isDirectory()) {
22
+ fixPermissions(fullPath);
23
+ } else {
24
+ fs.chmodSync(fullPath, 0o644);
25
+ }
26
+ } catch { /* 单个文件失败不影响其他 */ }
27
+ }
28
+ } catch { /* ignore */ }
29
+ }
30
+
31
+ function ensureDataDir() {
12
32
  const dataDir = process.env.AICMD_DATA_DIR || path.join(os.homedir(), '.aicmd');
13
- if (!fs.existsSync(dataDir)) {
14
- fs.mkdirSync(dataDir, { recursive: true });
33
+ try {
34
+ if (!fs.existsSync(dataDir)) {
35
+ fs.mkdirSync(dataDir, { recursive: true });
36
+ } else {
37
+ // 确保整个数据目录可写,递归修复权限
38
+ try {
39
+ fs.accessSync(dataDir, fs.constants.W_OK);
40
+ } catch {
41
+ fixPermissions(dataDir);
42
+ // 再次检查
43
+ try { fs.accessSync(dataDir, fs.constants.W_OK); } catch {
44
+ throw new Error(`Permission denied even after chmod: ${dataDir}`);
45
+ }
46
+ }
47
+ }
48
+ } catch (e) {
49
+ console.error(`Cannot access data directory ${dataDir}: ${e.message}`);
50
+ console.error('Try: sudo chown -R $(whoami) ' + dataDir);
51
+ process.exit(1);
15
52
  }
53
+ return dataDir;
54
+ }
55
+
56
+ function getPidFilePath() {
57
+ const dataDir = ensureDataDir();
16
58
  return path.join(dataDir, 'aicmd.server.pid');
17
59
  }
18
60
 
@@ -26,7 +68,19 @@ function readPid() {
26
68
 
27
69
  function writePid(pid) {
28
70
  const pidFilePath = getPidFilePath();
29
- fs.writeFileSync(pidFilePath, pid.toString());
71
+ try {
72
+ fs.writeFileSync(pidFilePath, pid.toString());
73
+ } catch (e) {
74
+ // 尝试修复权限后重试
75
+ try { fs.chmodSync(pidFilePath, 0o644); } catch { /* ignore */ }
76
+ try {
77
+ fs.writeFileSync(pidFilePath, pid.toString());
78
+ } catch (e2) {
79
+ console.error(`Cannot write PID file ${pidFilePath}: ${e2.message}`);
80
+ console.error('Try: sudo chown -R $(whoami) ' + path.dirname(pidFilePath));
81
+ process.exit(1);
82
+ }
83
+ }
30
84
  }
31
85
 
32
86
  function deletePid() {
@@ -114,6 +168,7 @@ async function startProject() {
114
168
 
115
169
  console.log('Server started with PID:', child.pid);
116
170
  console.log(`aicmd is running at http://localhost:${port}`);
171
+ openBrowser(`http://localhost:${port}`);
117
172
  }
118
173
 
119
174
  function stopProject() {
@@ -145,6 +200,14 @@ function restartProject() {
145
200
  startProject();
146
201
  }
147
202
 
203
+ function openBrowser(url) {
204
+ const platform = process.platform;
205
+ const cmd = platform === 'darwin' ? 'open' : platform === 'win32' ? 'start' : 'xdg-open';
206
+ const args = platform === 'win32' ? ['', url] : [url];
207
+ const proc = spawn(cmd, args, { detached: true, stdio: 'ignore', shell: platform === 'win32' });
208
+ proc.unref();
209
+ }
210
+
148
211
  function showVersion() {
149
212
  const pkg = JSON.parse(fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf8'));
150
213
  console.log(pkg.version);
@@ -1,5 +1,5 @@
1
1
  {
2
- "lastValidatedTimestamp": 1780110581479,
2
+ "lastValidatedTimestamp": 1780154509148,
3
3
  "projects": {},
4
4
  "pnpmfiles": [],
5
5
  "settings": {
package/dist/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
- "name": "@fefeding/fterm",
3
- "version": "0.1.4",
2
+ "name": "@fefeding/aicmd",
3
+ "version": "0.1.2",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
- "description": "Web SSH 远程终端工具 - 支持 rz/sz 文件传输",
6
+ "description": "AI-powered web SSH terminal with autonomous agent, skills, and file transfer",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "https://cnb.cool/yc_null/fshell"
9
+ "url": "https://github.com/fefeding/ai-cmd"
10
10
  },
11
11
  "main": "view/index.html",
12
12
  "files": [
@@ -14,14 +14,15 @@
14
14
  "public/",
15
15
  "view/",
16
16
  "dist/",
17
- "scripts/",
17
+ "data/skills",
18
+ "patches",
18
19
  "server.js",
19
20
  "package.json",
20
21
  "README.md"
21
22
  ],
22
23
  "icon": "public/favicon.ico",
23
24
  "window": {
24
- "title": "fTerm - 远程终端",
25
+ "title": "AICmd - AI Terminal",
25
26
  "width": 1200,
26
27
  "height": 800,
27
28
  "min_width": 800,
@@ -32,9 +33,9 @@
32
33
  "icon": "public/favicon.ico"
33
34
  },
34
35
  "scripts": {
35
- "start": "pm2 start server.js --name fterm-server --disable-logs --",
36
- "restart": "pm2 restart fterm-server --disable-logs --",
37
- "stop": "pm2 stop fterm-server --",
36
+ "start": "pm2 start server.js --name aicmd-server --disable-logs --",
37
+ "restart": "pm2 restart aicmd-server --disable-logs --",
38
+ "stop": "pm2 stop aicmd-server --",
38
39
  "dev": "vite",
39
40
  "build": "npm run build-only",
40
41
  "preview": "vite preview",
@@ -104,7 +105,7 @@
104
105
  "zmodem.js": "^0.1.10"
105
106
  },
106
107
  "bin": {
107
- "fterm": "bin/fterm.js"
108
+ "aicmd": "bin/aicmd.js"
108
109
  },
109
110
  "pnpm": {
110
111
  "onlyBuiltDependencies": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fefeding/aicmd",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "description": "AI-powered web SSH terminal with autonomous agent, skills, and file transfer",
package/server.js CHANGED
@@ -306,10 +306,19 @@ for (let i = 0; i < process.argv.length; i++) {
306
306
  }
307
307
  }
308
308
 
309
+ // PID 文件路径
310
+ const pidDir = process.env.AICMD_DATA_DIR || path.join(require('os').homedir(), '.aicmd');
311
+ try { fs.mkdirSync(pidDir, { recursive: true }); } catch {}
312
+ const pidFilePath = path.join(pidDir, 'aicmd.server.pid');
313
+
314
+ // 进程退出时清理 PID 文件
315
+ function cleanupPid() {
316
+ try { if (fs.existsSync(pidFilePath)) fs.unlinkSync(pidFilePath); } catch {}
317
+ }
318
+
309
319
  // 启动服务器
310
320
  const PORT = portFromArgs || process.env.PORT || 9802;
311
321
  server.listen(PORT, () => {
312
- const pidFilePath = path.join(__dirname, 'aicmd.server.pid');
313
322
  fs.writeFileSync(pidFilePath, process.pid.toString());
314
323
  console.log(`PID ${process.pid} written to ${pidFilePath}`);
315
324
  console.log(`aicmd Server is running on port ${PORT}`);
@@ -317,14 +326,18 @@ server.listen(PORT, () => {
317
326
  });
318
327
 
319
328
  // 进程退出处理
320
- process.on('exit', () => { logStream.end(); });
329
+ process.on('exit', () => { cleanupPid(); logStream.end(); });
330
+ process.on('SIGTERM', () => { cleanupPid(); process.exit(0); });
331
+ process.on('SIGINT', () => { cleanupPid(); process.exit(0); });
321
332
  process.on('uncaughtException', (err) => {
322
333
  console.error('Uncaught exception:', err);
334
+ cleanupPid();
323
335
  logStream.end();
324
336
  process.exit(1);
325
337
  });
326
338
  process.on('unhandledRejection', (err) => {
327
339
  console.error('Unhandled rejection:', err);
340
+ cleanupPid();
328
341
  logStream.end();
329
342
  process.exit(1);
330
343
  });