@fefeding/aicmd 0.1.7 → 0.1.14
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/dist/public/.vite/manifest.json +17 -17
- package/dist/public/favicon.ico +0 -0
- package/dist/public/index.css +80 -0
- package/dist/public/index.js +149 -23
- package/dist/public/views.css +309 -221
- package/dist/public/views.js +1743 -1605
- package/dist/public/vue.js +5120 -5120
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +13 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.ts +12 -1
- package/dist/server/model/connection.entity.d.ts +4 -0
- package/dist/server/model/connection.entity.d.ts.map +1 -1
- package/dist/server/model/connection.entity.js +4 -0
- package/dist/server/model/connection.entity.js.map +1 -1
- package/dist/server/model/connection.entity.ts +6 -0
- package/dist/server/service/ai.service.d.ts +17 -1
- package/dist/server/service/ai.service.d.ts.map +1 -1
- package/dist/server/service/ai.service.js +189 -106
- package/dist/server/service/ai.service.js.map +1 -1
- package/dist/server/service/ai.service.ts +198 -114
- package/dist/server/service/connection.service.d.ts +13 -2
- package/dist/server/service/connection.service.d.ts.map +1 -1
- package/dist/server/service/connection.service.js +153 -19
- package/dist/server/service/connection.service.js.map +1 -1
- package/dist/server/service/connection.service.ts +163 -22
- package/dist/server/service/ssh.service.d.ts +39 -0
- package/dist/server/service/ssh.service.d.ts.map +1 -1
- package/dist/server/service/ssh.service.js +419 -9
- package/dist/server/service/ssh.service.js.map +1 -1
- package/dist/server/service/ssh.service.ts +425 -9
- package/dist/server/utils/data-dir.d.ts +7 -0
- package/dist/server/utils/data-dir.d.ts.map +1 -1
- package/dist/server/utils/data-dir.js +38 -2
- package/dist/server/utils/data-dir.js.map +1 -1
- package/dist/server/utils/data-dir.ts +33 -2
- package/dist/view/index.html +19 -21
- package/electron/main.js +73 -11
- package/electron/preload.js +34 -0
- package/electron/updater.js +181 -0
- package/package.json +6 -4
- package/server.js +128 -3
- package/view/index.html +0 -9
- package/view/modules/header.tpl +0 -14
- package/view/modules/initial_state.tpl +0 -20
|
@@ -9,7 +9,9 @@ import * as fs from 'fs';
|
|
|
9
9
|
* 优先级:环境变量 AICMD_DATA_DIR > ~/.aicmd
|
|
10
10
|
*/
|
|
11
11
|
export function getDataDir(): string {
|
|
12
|
-
|
|
12
|
+
const dir = process.env.AICMD_DATA_DIR || path.join(os.homedir(), '.aicmd');
|
|
13
|
+
console.log(`[data-dir] getDataDir: ${dir}, home: ${os.homedir()}`);
|
|
14
|
+
return dir;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
/**
|
|
@@ -18,11 +20,16 @@ export function getDataDir(): string {
|
|
|
18
20
|
export function ensureDataDir(): string {
|
|
19
21
|
const dir = getDataDir();
|
|
20
22
|
try {
|
|
23
|
+
console.log(`[data-dir] ensureDataDir: ${dir}`);
|
|
21
24
|
if (!fs.existsSync(dir)) {
|
|
22
25
|
fs.mkdirSync(dir, { recursive: true });
|
|
26
|
+
console.log(`[data-dir] Created directory: ${dir}`);
|
|
23
27
|
}
|
|
28
|
+
fs.accessSync(dir, fs.constants.W_OK);
|
|
29
|
+
console.log(`[data-dir] Directory is writable: ${dir}`);
|
|
24
30
|
} catch (e: any) {
|
|
25
|
-
console.error(`Cannot create data directory ${dir}: ${e.message}`);
|
|
31
|
+
console.error(`[data-dir] Cannot create/access data directory ${dir}: ${e.message}`);
|
|
32
|
+
console.error(`[data-dir] dir exists: ${fs.existsSync(dir)}`);
|
|
26
33
|
throw e;
|
|
27
34
|
}
|
|
28
35
|
return dir;
|
|
@@ -36,6 +43,30 @@ export function getDataPath(...segments: string[]): string {
|
|
|
36
43
|
return path.join(getDataDir(), ...segments);
|
|
37
44
|
}
|
|
38
45
|
|
|
46
|
+
/**
|
|
47
|
+
* 确保指定目录存在且可写
|
|
48
|
+
* - 目录不存在时递归创建
|
|
49
|
+
* - 权限不足时尝试修复为 0o755
|
|
50
|
+
* - 全部失败返回 false,不抛异常
|
|
51
|
+
*/
|
|
52
|
+
export function ensureDir(dir: string): boolean {
|
|
53
|
+
try {
|
|
54
|
+
if (!fs.existsSync(dir)) {
|
|
55
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
56
|
+
}
|
|
57
|
+
// 检查是否可写
|
|
58
|
+
try {
|
|
59
|
+
fs.accessSync(dir, fs.constants.W_OK);
|
|
60
|
+
} catch {
|
|
61
|
+
try { fs.chmodSync(dir, 0o755); } catch (_) { /* 无权限修复则忽略 */ }
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
} catch (e: any) {
|
|
65
|
+
console.warn(`[data-dir] Cannot create/fix directory (${dir}): ${e.message}`);
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
39
70
|
/**
|
|
40
71
|
* 递归修复目录及文件权限:目录 755,文件 644
|
|
41
72
|
*/
|
package/dist/view/index.html
CHANGED
|
@@ -4,26 +4,25 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<base href="" />
|
|
6
6
|
<script type="text/template" id="__INITIAL_STATE__">
|
|
7
|
-
|
|
8
|
-
</script>
|
|
9
|
-
<script type="text/template" id="__DEFAULTINITIAL_STATE__">
|
|
10
|
-
|
|
11
|
-
</script>
|
|
12
|
-
<script>
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
</script>
|
|
26
|
-
|
|
7
|
+
{{ data | dump | safe }}
|
|
8
|
+
</script>
|
|
9
|
+
<script type="text/template" id="__DEFAULTINITIAL_STATE__">
|
|
10
|
+
{"config":{"prefix":"","apiUrl":""},"title":"aicmd Terminal"}
|
|
11
|
+
</script>
|
|
12
|
+
<script>
|
|
13
|
+
function __get_templateJson(id) {
|
|
14
|
+
try {
|
|
15
|
+
var tag = document.getElementById(id);
|
|
16
|
+
var obj = JSON.parse(tag.innerHTML);
|
|
17
|
+
return obj;
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
var __INITIAL_STATE__ = __get_templateJson('__INITIAL_STATE__');
|
|
24
|
+
if(!__INITIAL_STATE__) __INITIAL_STATE__ = __get_templateJson('__DEFAULTINITIAL_STATE__');
|
|
25
|
+
</script>
|
|
27
26
|
<link rel="icon" href="/public/favicon.ico" />
|
|
28
27
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
29
28
|
<title>AICmd - AI Terminal</title>
|
|
@@ -40,7 +39,6 @@
|
|
|
40
39
|
<link rel="stylesheet" crossorigin href="/public/bootstrap.css">
|
|
41
40
|
<link rel="stylesheet" crossorigin href="/public/index.css">
|
|
42
41
|
</head>
|
|
43
|
-
|
|
44
42
|
<body>
|
|
45
43
|
<div id="app" class="min-vh-100"></div>
|
|
46
44
|
</body>
|
package/electron/main.js
CHANGED
|
@@ -8,6 +8,38 @@
|
|
|
8
8
|
|
|
9
9
|
const { app, BrowserWindow, shell, ipcMain, protocol, net, Menu } = require('electron');
|
|
10
10
|
const path = require('path');
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const os = require('os');
|
|
13
|
+
const { initAutoUpdater } = require('./updater');
|
|
14
|
+
|
|
15
|
+
// ========== 日志输出到文件(生产模式调试用) ==========
|
|
16
|
+
const logDir = path.join(os.homedir(), '.aicmd');
|
|
17
|
+
try { if (!fs.existsSync(logDir)) fs.mkdirSync(logDir, { recursive: true }); } catch (e) { /* ignore */ }
|
|
18
|
+
const logFile = path.join(logDir, 'electron.log');
|
|
19
|
+
const logStream = (() => {
|
|
20
|
+
try {
|
|
21
|
+
return fs.createWriteStream(logFile, { flags: 'w' });
|
|
22
|
+
} catch (e) { return null; }
|
|
23
|
+
})();
|
|
24
|
+
const origLog = console.log;
|
|
25
|
+
const origError = console.error;
|
|
26
|
+
const origWarn = console.warn;
|
|
27
|
+
function ts() { return new Date().toISOString(); }
|
|
28
|
+
console.log = function(...args) {
|
|
29
|
+
origLog.apply(console, args);
|
|
30
|
+
if (logStream) logStream.write(`[${ts()}] LOG ${args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ')}\n`);
|
|
31
|
+
};
|
|
32
|
+
console.error = function(...args) {
|
|
33
|
+
origError.apply(console, args);
|
|
34
|
+
if (logStream) logStream.write(`[${ts()}] ERR ${args.map(a => typeof a === 'string' ? a : (a instanceof Error ? a.message + '\n' + a.stack : JSON.stringify(a))).join(' ')}\n`);
|
|
35
|
+
};
|
|
36
|
+
console.warn = function(...args) {
|
|
37
|
+
origWarn.apply(console, args);
|
|
38
|
+
if (logStream) logStream.write(`[${ts()}] WRN ${args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ')}\n`);
|
|
39
|
+
};
|
|
40
|
+
console.log(`[Electron] Log file: ${logFile}`);
|
|
41
|
+
console.log(`[Electron] App starting, packaged: ${app.isPackaged}, platform: ${process.platform}, arch: ${process.arch}`);
|
|
42
|
+
console.log(`[Electron] __dirname: ${__dirname}, cwd: ${process.cwd()}`);
|
|
11
43
|
|
|
12
44
|
// ========== 注册自定义协议(必须在 app.whenReady 之前) ==========
|
|
13
45
|
// 解决 file:// 协议下绝对路径(/public/xxx.js)无法加载的问题
|
|
@@ -28,19 +60,32 @@ let _services = null;
|
|
|
28
60
|
function getServices() {
|
|
29
61
|
if (_services) return _services;
|
|
30
62
|
if (isDev) {
|
|
31
|
-
// 开发模式下服务端由 Vite 开发服务器提供,主进程不加载
|
|
32
63
|
console.warn('[Electron] Dev mode: services are provided by Vite dev server, IPC disabled');
|
|
33
64
|
return null;
|
|
34
65
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
66
|
+
try {
|
|
67
|
+
const serverPath = path.join(__dirname, '..', 'dist', 'server', 'index.js');
|
|
68
|
+
console.log(`[Electron] Loading server module from: ${serverPath}`);
|
|
69
|
+
console.log(`[Electron] __dirname: ${__dirname}`);
|
|
70
|
+
console.log(`[Electron] File exists: ${require('fs').existsSync(serverPath)}`);
|
|
71
|
+
const serverModule = require(serverPath);
|
|
72
|
+
console.log(`[Electron] Server module loaded, keys: ${Object.keys(serverModule).join(', ')}`);
|
|
73
|
+
_services = {
|
|
74
|
+
sshService: serverModule.sshService,
|
|
75
|
+
connectionService: serverModule.connectionService,
|
|
76
|
+
aiService: serverModule.aiService,
|
|
77
|
+
monitorService: serverModule.monitorService,
|
|
78
|
+
};
|
|
79
|
+
// 检查各服务是否正常
|
|
80
|
+
for (const [name, svc] of Object.entries(_services)) {
|
|
81
|
+
console.log(`[Electron] Service '${name}': ${svc ? 'OK' : 'NULL'}`);
|
|
82
|
+
}
|
|
83
|
+
return _services;
|
|
84
|
+
} catch (e) {
|
|
85
|
+
console.error(`[Electron] Failed to load server module: ${e.message}`);
|
|
86
|
+
console.error(e.stack);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
44
89
|
}
|
|
45
90
|
|
|
46
91
|
// ========== 终端 IPC 处理(替代 WebSocket) ==========
|
|
@@ -188,7 +233,7 @@ function setupTerminalIPC() {
|
|
|
188
233
|
|
|
189
234
|
case 'close': {
|
|
190
235
|
if (sid) {
|
|
191
|
-
sshService.
|
|
236
|
+
sshService.deleteSession(sid);
|
|
192
237
|
const sessions = windowSessions.get(win.id);
|
|
193
238
|
if (sessions) sessions.delete(sid);
|
|
194
239
|
}
|
|
@@ -251,6 +296,7 @@ function cleanupWindow(win) {
|
|
|
251
296
|
sessions.forEach(sid => {
|
|
252
297
|
try {
|
|
253
298
|
services.monitorService.stopSessionMonitors(sid);
|
|
299
|
+
// 仅关闭连接,不删除元数据(保留 sessions.json 供下次启动恢复)
|
|
254
300
|
services.sshService.closeSession(sid);
|
|
255
301
|
} catch (e) { /* ignore */ }
|
|
256
302
|
});
|
|
@@ -305,6 +351,13 @@ function createWindow(loadTarget) {
|
|
|
305
351
|
if (isDev) {
|
|
306
352
|
mainWindow.webContents.openDevTools({ mode: 'detach' });
|
|
307
353
|
}
|
|
354
|
+
|
|
355
|
+
// 生产模式下可通过快捷键打开 DevTools 调试
|
|
356
|
+
mainWindow.webContents.on('before-input-event', (event, input) => {
|
|
357
|
+
if ((input.meta || input.control) && input.shift && input.key.toLowerCase() === 'i') {
|
|
358
|
+
mainWindow.webContents.toggleDevTools();
|
|
359
|
+
}
|
|
360
|
+
});
|
|
308
361
|
}
|
|
309
362
|
|
|
310
363
|
// ========== 应用菜单 ==========
|
|
@@ -330,6 +383,12 @@ function setupMenu() {
|
|
|
330
383
|
{ label: 'Report Issue', click: () => shell.openExternal('https://github.com/fefeding/ai-cmd/issues') },
|
|
331
384
|
{ type: 'separator' },
|
|
332
385
|
{ label: 'About', click: () => shell.openExternal('https://aigcwhere.com/opensource/aicmd') },
|
|
386
|
+
{ type: 'separator' },
|
|
387
|
+
{ label: 'Check for Updates...', click: () => {
|
|
388
|
+
if (mainWindow && !mainWindow.isDestroyed()) {
|
|
389
|
+
mainWindow.webContents.send('menu:action', 'check-update');
|
|
390
|
+
}
|
|
391
|
+
}},
|
|
333
392
|
],
|
|
334
393
|
},
|
|
335
394
|
];
|
|
@@ -394,6 +453,9 @@ app.whenReady().then(() => {
|
|
|
394
453
|
|
|
395
454
|
createWindow(target);
|
|
396
455
|
|
|
456
|
+
// 初始化自动更新(生产模式启用,开发模式自动禁用)
|
|
457
|
+
initAutoUpdater({ isDev });
|
|
458
|
+
|
|
397
459
|
app.on('activate', () => {
|
|
398
460
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
399
461
|
createWindow(target);
|
package/electron/preload.js
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
const { ipcRenderer } = require('electron');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
|
|
12
|
+
// 服务端模块绝对路径(供渲染进程 require 使用)
|
|
13
|
+
const serverModulePath = path.join(process.resourcesPath, 'app.asar', 'dist', 'server', 'index.js');
|
|
10
14
|
|
|
11
15
|
// 终端 IPC 通道(替代 WebSocket)
|
|
12
16
|
const terminalIPC = {
|
|
@@ -22,9 +26,39 @@ const terminalIPC = {
|
|
|
22
26
|
},
|
|
23
27
|
};
|
|
24
28
|
|
|
29
|
+
// 自动更新 API
|
|
30
|
+
const updater = {
|
|
31
|
+
/** 手动检查更新 */
|
|
32
|
+
checkForUpdates() {
|
|
33
|
+
return ipcRenderer.invoke('update:check');
|
|
34
|
+
},
|
|
35
|
+
/** 获取当前更新状态 */
|
|
36
|
+
getStatus() {
|
|
37
|
+
return ipcRenderer.invoke('update:status');
|
|
38
|
+
},
|
|
39
|
+
/** 安装更新并重启 */
|
|
40
|
+
install() {
|
|
41
|
+
return ipcRenderer.invoke('update:install');
|
|
42
|
+
},
|
|
43
|
+
/** 监听更新事件(available, progress, downloaded, error) */
|
|
44
|
+
onEvent(callback) {
|
|
45
|
+
const handler = (_event, msg) => callback(msg);
|
|
46
|
+
ipcRenderer.on('update:event', handler);
|
|
47
|
+
return () => ipcRenderer.removeListener('update:event', handler);
|
|
48
|
+
},
|
|
49
|
+
/** 监听菜单操作(如 check-update) */
|
|
50
|
+
onMenuAction(callback) {
|
|
51
|
+
const handler = (_event, action) => callback(action);
|
|
52
|
+
ipcRenderer.on('menu:action', handler);
|
|
53
|
+
return () => ipcRenderer.removeListener('menu:action', handler);
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
25
57
|
window.electronAPI = {
|
|
26
58
|
isElectron: true,
|
|
27
59
|
isPackaged: !process.env.ELECTRON_DEV,
|
|
28
60
|
platform: process.platform,
|
|
61
|
+
serverModulePath,
|
|
29
62
|
terminalIPC,
|
|
63
|
+
updater,
|
|
30
64
|
};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Electron 自动更新模块
|
|
3
|
+
* @description 使用 electron-updater 检测、下载和安装应用更新
|
|
4
|
+
*
|
|
5
|
+
* 更新流程:
|
|
6
|
+
* 1. 应用启动时自动检查更新
|
|
7
|
+
* 2. 发现新版本 → 通知渲染进程显示提示
|
|
8
|
+
* 3. 后台下载更新(利用 blockmap 增量下载)
|
|
9
|
+
* 4. 下载完成 → 通知渲染进程显示"重启安装"按钮
|
|
10
|
+
* 5. 用户确认后重启并安装
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { autoUpdater } = require('electron-updater');
|
|
14
|
+
const { BrowserWindow, ipcMain, app } = require('electron');
|
|
15
|
+
|
|
16
|
+
// 更新状态
|
|
17
|
+
let updateAvailable = null;
|
|
18
|
+
let updateDownloaded = false;
|
|
19
|
+
let downloadProgress = null;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 向所有窗口广播更新事件
|
|
23
|
+
*/
|
|
24
|
+
function broadcastUpdate(event, data) {
|
|
25
|
+
BrowserWindow.getAllWindows().forEach((win) => {
|
|
26
|
+
if (!win.isDestroyed()) {
|
|
27
|
+
win.webContents.send('update:event', { event, data });
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 初始化自动更新
|
|
34
|
+
* @param {object} options - 配置选项
|
|
35
|
+
* @param {boolean} options.isDev - 是否开发模式
|
|
36
|
+
*/
|
|
37
|
+
function initAutoUpdater(options = {}) {
|
|
38
|
+
// 开发模式下禁用自动更新
|
|
39
|
+
if (options.isDev) {
|
|
40
|
+
console.log('[Updater] Disabled in dev mode');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 未打包时禁用(如通过 npm start 运行的 Web 模式)
|
|
45
|
+
if (!app.isPackaged) {
|
|
46
|
+
console.log('[Updater] Disabled: app is not packaged');
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 配置 autoUpdater
|
|
51
|
+
autoUpdater.autoDownload = true;
|
|
52
|
+
autoUpdater.autoInstallOnAppQuit = true;
|
|
53
|
+
|
|
54
|
+
// 事件监听
|
|
55
|
+
autoUpdater.on('checking-for-update', () => {
|
|
56
|
+
console.log('[Updater] Checking for updates...');
|
|
57
|
+
broadcastUpdate('checking', null);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
autoUpdater.on('update-available', (info) => {
|
|
61
|
+
console.log(`[Updater] Update available: v${info.version}`);
|
|
62
|
+
updateAvailable = info;
|
|
63
|
+
updateDownloaded = false;
|
|
64
|
+
broadcastUpdate('available', {
|
|
65
|
+
version: info.version,
|
|
66
|
+
releaseDate: info.releaseDate,
|
|
67
|
+
releaseNotes: info.releaseNotes,
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
autoUpdater.on('update-not-available', (info) => {
|
|
72
|
+
console.log(`[Updater] Already up to date: v${info.version}`);
|
|
73
|
+
updateAvailable = null;
|
|
74
|
+
updateDownloaded = false;
|
|
75
|
+
broadcastUpdate('not-available', { version: info.version });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
autoUpdater.on('download-progress', (progress) => {
|
|
79
|
+
downloadProgress = {
|
|
80
|
+
percent: Math.round(progress.percent),
|
|
81
|
+
bytesPerSecond: progress.bytesPerSecond,
|
|
82
|
+
transferred: progress.transferred,
|
|
83
|
+
total: progress.total,
|
|
84
|
+
};
|
|
85
|
+
// 每 10% 广播一次,避免过于频繁
|
|
86
|
+
if (downloadProgress.percent % 10 === 0) {
|
|
87
|
+
broadcastUpdate('progress', downloadProgress);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
autoUpdater.on('update-downloaded', (info) => {
|
|
92
|
+
console.log(`[Updater] Update downloaded: v${info.version}`);
|
|
93
|
+
updateDownloaded = true;
|
|
94
|
+
broadcastUpdate('downloaded', {
|
|
95
|
+
version: info.version,
|
|
96
|
+
releaseDate: info.releaseDate,
|
|
97
|
+
releaseNotes: info.releaseNotes,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
autoUpdater.on('error', (error) => {
|
|
102
|
+
console.error('[Updater] Error:', error.message);
|
|
103
|
+
updateAvailable = null;
|
|
104
|
+
updateDownloaded = false;
|
|
105
|
+
downloadProgress = null;
|
|
106
|
+
// 静默处理网络错误,不打扰用户
|
|
107
|
+
if (!error.message.includes('net::') && !error.message.includes('ENOTFOUND')) {
|
|
108
|
+
broadcastUpdate('error', { message: error.message });
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// 注册 IPC 处理器
|
|
113
|
+
setupUpdateIPC();
|
|
114
|
+
|
|
115
|
+
// 延迟 3 秒后检查更新(避免启动时太卡)
|
|
116
|
+
setTimeout(() => {
|
|
117
|
+
checkForUpdates();
|
|
118
|
+
}, 3000);
|
|
119
|
+
|
|
120
|
+
// 每小时检查一次更新
|
|
121
|
+
setInterval(() => {
|
|
122
|
+
checkForUpdates();
|
|
123
|
+
}, 60 * 60 * 1000);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 检查更新
|
|
128
|
+
*/
|
|
129
|
+
async function checkForUpdates() {
|
|
130
|
+
try {
|
|
131
|
+
await autoUpdater.checkForUpdates();
|
|
132
|
+
} catch (err) {
|
|
133
|
+
console.error('[Updater] Check failed:', err.message);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 安装更新并重启
|
|
139
|
+
*/
|
|
140
|
+
function quitAndInstall() {
|
|
141
|
+
if (updateDownloaded) {
|
|
142
|
+
autoUpdater.quitAndInstall();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 注册更新相关的 IPC 通道
|
|
148
|
+
*/
|
|
149
|
+
function setupUpdateIPC() {
|
|
150
|
+
// 渲染进程请求检查更新
|
|
151
|
+
ipcMain.handle('update:check', async () => {
|
|
152
|
+
await checkForUpdates();
|
|
153
|
+
return { checking: true };
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// 渲染进程请求获取当前更新状态
|
|
157
|
+
ipcMain.handle('update:status', () => {
|
|
158
|
+
return {
|
|
159
|
+
updateAvailable: updateAvailable
|
|
160
|
+
? {
|
|
161
|
+
version: updateAvailable.version,
|
|
162
|
+
releaseDate: updateAvailable.releaseDate,
|
|
163
|
+
}
|
|
164
|
+
: null,
|
|
165
|
+
updateDownloaded,
|
|
166
|
+
downloadProgress,
|
|
167
|
+
};
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// 渲染进程请求安装更新
|
|
171
|
+
ipcMain.handle('update:install', () => {
|
|
172
|
+
quitAndInstall();
|
|
173
|
+
return { installing: true };
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
module.exports = {
|
|
178
|
+
initAutoUpdater,
|
|
179
|
+
checkForUpdates,
|
|
180
|
+
quitAndInstall,
|
|
181
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fefeding/aicmd",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"description": "AI-powered web SSH terminal with autonomous agent, skills, and file transfer",
|
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
"files": [
|
|
13
13
|
"bin/",
|
|
14
14
|
"public/",
|
|
15
|
-
"view/",
|
|
16
15
|
"dist/",
|
|
17
16
|
"electron/",
|
|
18
17
|
"data/skills",
|
|
@@ -42,6 +41,7 @@
|
|
|
42
41
|
"axios": "^1.16.1",
|
|
43
42
|
"dayjs": "^1.11.21",
|
|
44
43
|
"dotenv": "^17.4.2",
|
|
44
|
+
"electron-updater": "^6.8.3",
|
|
45
45
|
"express": "^5.2.1",
|
|
46
46
|
"node-pty": "1.2.0-beta.13",
|
|
47
47
|
"ssh2": "^1.17.0",
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"@codemirror/state": "^6.6.0",
|
|
56
56
|
"@codemirror/theme-one-dark": "^6.1.3",
|
|
57
57
|
"@codemirror/view": "^6.43.0",
|
|
58
|
+
"@electron/rebuild": "^4.0.4",
|
|
58
59
|
"@fefeding/eventemitter": "^1.0.5",
|
|
59
60
|
"@fefeding/vite-nunjucks-plugin": "^1.0.2",
|
|
60
61
|
"@popperjs/core": "^2.11.8",
|
|
@@ -74,11 +75,12 @@
|
|
|
74
75
|
"autoprefixer": "^10.5.0",
|
|
75
76
|
"bootstrap": "^5.3.8",
|
|
76
77
|
"bootstrap-icons": "^1.13.1",
|
|
78
|
+
"electron": "^35.5.1",
|
|
79
|
+
"electron-builder": "^26.0.12",
|
|
80
|
+
"electron-updater": "^6.8.3",
|
|
77
81
|
"jimp": "^1.6.1",
|
|
78
82
|
"js-cookie": "^3.0.7",
|
|
79
83
|
"jsdom": "^26.1.0",
|
|
80
|
-
"electron": "^35.5.1",
|
|
81
|
-
"electron-builder": "^26.0.12",
|
|
82
84
|
"pinia": "^3.0.4",
|
|
83
85
|
"pinia-plugin-persistedstate": "^4.7.1",
|
|
84
86
|
"postcss": "^8.5.15",
|
package/server.js
CHANGED
|
@@ -56,10 +56,39 @@ if (!fs.existsSync(serverIndexPath)) {
|
|
|
56
56
|
app.use((req, res, next) => {
|
|
57
57
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
58
58
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
|
59
|
-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
59
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Session-Id, X-File-Name');
|
|
60
|
+
if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
|
|
60
61
|
next();
|
|
61
62
|
});
|
|
62
63
|
|
|
64
|
+
// HTTP 文件上传端点(绕过 WebSocket,支持大文件)
|
|
65
|
+
app.post('/api/file-upload', async (req, res) => {
|
|
66
|
+
const sessionId = req.headers['x-session-id'];
|
|
67
|
+
const fileName = req.headers['x-file-name'];
|
|
68
|
+
if (!sessionId || !fileName) {
|
|
69
|
+
res.status(400).json({ success: false, error: 'Missing X-Session-Id or X-File-Name header' });
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const serverModule = require('./dist/server/index.js');
|
|
74
|
+
const { sshService } = serverModule;
|
|
75
|
+
// 收集原始请求体(文件二进制数据)
|
|
76
|
+
console.log(`[HTTP-Upload] Receiving file: session=${sessionId}, file=${fileName}`);
|
|
77
|
+
const chunks = [];
|
|
78
|
+
for await (const chunk of req) {
|
|
79
|
+
chunks.push(chunk);
|
|
80
|
+
}
|
|
81
|
+
const fileBuffer = Buffer.concat(chunks);
|
|
82
|
+
console.log(`[HTTP-Upload] Received ${fileBuffer.length} bytes, calling SFTP...`);
|
|
83
|
+
const bytes = await sshService.uploadFileViaSftp(sessionId, fileName, fileBuffer);
|
|
84
|
+
console.log(`[HTTP-Upload] SFTP done: ${fileName}, ${bytes} bytes`);
|
|
85
|
+
res.json({ success: true, bytes, fileName });
|
|
86
|
+
} catch (err) {
|
|
87
|
+
console.error(`[HTTP-Upload] Error: ${err.message}`);
|
|
88
|
+
res.status(500).json({ success: false, error: err.message, fileName });
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
63
92
|
// 处理 API 请求
|
|
64
93
|
app.use('/api/', async (req, res, next) => {
|
|
65
94
|
if (req.method === 'POST') {
|
|
@@ -90,8 +119,12 @@ app.use((req, res) => {
|
|
|
90
119
|
});
|
|
91
120
|
|
|
92
121
|
// ========== WebSocket 服务 ==========
|
|
93
|
-
|
|
94
|
-
|
|
122
|
+
// maxPayload 设为 500MB,支持较大文件的 base64 传输(base64 比原文件大 ~33%)
|
|
123
|
+
const wss = new WebSocket.Server({ server, path: '/ws/terminal', maxPayload: 500 * 1024 * 1024 });
|
|
124
|
+
console.log(`[WS] WebSocket server created on path /ws/terminal (maxPayload: 500MB)`);
|
|
125
|
+
|
|
126
|
+
// 分块上传缓冲区: uploadId -> { chunks: string[], totalChunks: number, fileName: string, sessionId: string }
|
|
127
|
+
const pendingUploads = new Map();
|
|
95
128
|
|
|
96
129
|
// 跟踪 sessionId -> WebSocket 映射(用于推送监控事件)
|
|
97
130
|
const wsBySessionId = new Map();
|
|
@@ -280,6 +313,98 @@ wss.on('connection', async (ws, req) => {
|
|
|
280
313
|
break;
|
|
281
314
|
}
|
|
282
315
|
|
|
316
|
+
case 'file-upload': {
|
|
317
|
+
// SFTP 文件上传 - 单消息模式(小文件,<50MB base64)
|
|
318
|
+
const { fileName, data: fileData } = data || {};
|
|
319
|
+
if (!sid || !fileName || !fileData) {
|
|
320
|
+
console.error(`[WS] file-upload missing params: sid=${sid}, fileName=${fileName}, hasData=${!!fileData}`);
|
|
321
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: false, error: 'Missing params', fileName }));
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
console.log(`[WS] SFTP upload START: session=${sid}, file=${fileName}, b64len=${fileData.length}, wsReady=${ws.readyState}`);
|
|
326
|
+
const bytes = await sshService.uploadFileViaSftp(sid, fileName, Buffer.from(fileData, 'base64'));
|
|
327
|
+
console.log(`[WS] SFTP upload DONE: ${fileName}, ${bytes} bytes, wsReady=${ws.readyState}`);
|
|
328
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
329
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: true, bytes, fileName }));
|
|
330
|
+
} else {
|
|
331
|
+
console.error(`[WS] Cannot send result, WS not open: state=${ws.readyState}`);
|
|
332
|
+
}
|
|
333
|
+
} catch (err) {
|
|
334
|
+
console.error(`[WS] SFTP upload ERROR: ${err.message}`, err.stack);
|
|
335
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
336
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: false, error: err.message, fileName }));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
case 'file-upload-start': {
|
|
343
|
+
// 分块上传 - 开始(大文件)
|
|
344
|
+
const { uploadId, fileName: startFileName, totalChunks } = data || {};
|
|
345
|
+
if (!uploadId || !startFileName || !totalChunks) {
|
|
346
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: false, error: 'Missing params', fileName: startFileName }));
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
console.log(`[WS] Chunked upload start: id=${uploadId}, file=${startFileName}, chunks=${totalChunks}`);
|
|
350
|
+
pendingUploads.set(uploadId, { chunks: new Array(totalChunks), totalChunks, fileName: startFileName, sessionId: sid, receivedChunks: 0 });
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
case 'file-upload-chunk': {
|
|
355
|
+
// 分块上传 - 数据块
|
|
356
|
+
const { uploadId: chunkId, chunkIndex, data: chunkData } = data || {};
|
|
357
|
+
const upload = chunkId ? pendingUploads.get(chunkId) : null;
|
|
358
|
+
if (!upload) {
|
|
359
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: false, error: 'Upload session not found' }));
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
upload.chunks[chunkIndex] = chunkData;
|
|
363
|
+
upload.receivedChunks++;
|
|
364
|
+
// 每 10 个 chunk 反馈一次进度
|
|
365
|
+
if (upload.receivedChunks % 10 === 0 || upload.receivedChunks === upload.totalChunks) {
|
|
366
|
+
ws.send(JSON.stringify({ type: 'file-upload-progress', uploadId: chunkId, received: upload.receivedChunks, total: upload.totalChunks }));
|
|
367
|
+
}
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
case 'file-upload-end': {
|
|
372
|
+
// 分块上传 - 结束,拼接并 SFTP 写入
|
|
373
|
+
const { uploadId: endId } = data || {};
|
|
374
|
+
const endUpload = endId ? pendingUploads.get(endId) : null;
|
|
375
|
+
if (!endUpload) {
|
|
376
|
+
console.error(`[WS] file-upload-end: upload session not found: ${endId}`);
|
|
377
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: false, error: 'Upload session not found' }));
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
try {
|
|
381
|
+
console.log(`[WS] Chunked upload end: id=${endId}, file=${endUpload.fileName}, received=${endUpload.receivedChunks}/${endUpload.totalChunks}`);
|
|
382
|
+
// 检查是否收到所有 chunk
|
|
383
|
+
const missingChunks = endUpload.chunks.reduce((acc, c, i) => c === undefined ? [...acc, i] : acc, []);
|
|
384
|
+
if (missingChunks.length > 0) {
|
|
385
|
+
console.error(`[WS] Missing chunks: ${missingChunks.join(',')}`);
|
|
386
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: false, error: `Missing ${missingChunks.length} chunks`, fileName: endUpload.fileName }));
|
|
387
|
+
pendingUploads.delete(endId);
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
const fullB64 = endUpload.chunks.join('');
|
|
391
|
+
console.log(`[WS] Joined ${endUpload.totalChunks} chunks -> b64len=${fullB64.length}, calling SFTP upload...`);
|
|
392
|
+
const bytes = await sshService.uploadFileViaSftp(endUpload.sessionId, endUpload.fileName, Buffer.from(fullB64, 'base64'));
|
|
393
|
+
console.log(`[WS] Chunked upload SFTP DONE: ${endUpload.fileName}, ${bytes} bytes, wsReady=${ws.readyState}`);
|
|
394
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
395
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: true, bytes, fileName: endUpload.fileName }));
|
|
396
|
+
}
|
|
397
|
+
} catch (err) {
|
|
398
|
+
console.error(`[WS] Chunked upload ERROR: ${err.message}`, err.stack);
|
|
399
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
400
|
+
ws.send(JSON.stringify({ type: 'file-upload-result', success: false, error: err.message, fileName: endUpload.fileName }));
|
|
401
|
+
}
|
|
402
|
+
} finally {
|
|
403
|
+
pendingUploads.delete(endId);
|
|
404
|
+
}
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
|
|
283
408
|
case 'ai-agent-stop': {
|
|
284
409
|
// 停止 AI Agent
|
|
285
410
|
const { aiSessionId: stopSid } = data || {};
|