@myassis/gateway 1.0.82 → 1.0.83
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/main.js +57 -36
- package/dist/services/ServiceManager.js +193 -63
- package/dist/services/session/Session.js +76 -1
- package/dist/services/tools/plan.js +1 -10
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -31,6 +31,21 @@ const WebSocketService_js_1 = require("./services/WebSocketService.js");
|
|
|
31
31
|
const TaskSchedulerService_js_1 = require("./services/TaskSchedulerService.js");
|
|
32
32
|
const ServiceManager_js_1 = require("./services/ServiceManager.js");
|
|
33
33
|
const logger = (0, shared_1.getLogger)('index');
|
|
34
|
+
// 内置放通的官方域名(含子域名),无需用户手动 addCors
|
|
35
|
+
const DEFAULT_CORS_DOMAINS = ['my-assis.com'];
|
|
36
|
+
// 判断来源主机是否命中某个域名(自身或其子域名)
|
|
37
|
+
function matchDomain(originHost, domain) {
|
|
38
|
+
// 允许配置成裸域名(app.example.com)、带协议的完整来源(https://app.example.com)或通配符(*.example.com)
|
|
39
|
+
const d = domain
|
|
40
|
+
.trim()
|
|
41
|
+
.toLowerCase()
|
|
42
|
+
.replace(/^https?:\/\//, '')
|
|
43
|
+
.replace(/\/.*$/, '')
|
|
44
|
+
.replace(/^\*\./, '');
|
|
45
|
+
if (!d)
|
|
46
|
+
return false;
|
|
47
|
+
return originHost === d || originHost.endsWith('.' + d);
|
|
48
|
+
}
|
|
34
49
|
// ─── CLI 模式 ─────────────────────────────────────────
|
|
35
50
|
// gateway install | start | stop | uninstall | status | update
|
|
36
51
|
const cliCommand = process.argv[2];
|
|
@@ -137,9 +152,11 @@ if (cliCommand) {
|
|
|
137
152
|
}
|
|
138
153
|
}
|
|
139
154
|
else if (cliCommand === 'listCors') {
|
|
155
|
+
console.log(`内置 CORS 域名 (${DEFAULT_CORS_DOMAINS.length}):`);
|
|
156
|
+
DEFAULT_CORS_DOMAINS.forEach(d => console.log(` - *.${d}`));
|
|
140
157
|
const domains = persistStore_js_1.persistStore.getCorsDomains();
|
|
141
158
|
if (domains.length === 0) {
|
|
142
|
-
console.log('暂无自定义 CORS
|
|
159
|
+
console.log('暂无自定义 CORS 域名(仅使用内置内网与内置域名规则)');
|
|
143
160
|
}
|
|
144
161
|
else {
|
|
145
162
|
console.log(`自定义 CORS 域名 (${domains.length}):`);
|
|
@@ -147,35 +164,27 @@ if (cliCommand) {
|
|
|
147
164
|
}
|
|
148
165
|
}
|
|
149
166
|
else if (cliCommand === '--help' || cliCommand === '-h') {
|
|
150
|
-
console.log(`我的助手 Gateway CLI
|
|
151
|
-
|
|
152
|
-
用法: gateway <命令>
|
|
153
|
-
|
|
154
|
-
服务管理命令:
|
|
155
|
-
install 安装 Gateway 服务(后台运行)
|
|
156
|
-
uninstall 卸载 Gateway 服务
|
|
157
|
-
start 启动 Gateway 服务
|
|
158
|
-
stop 停止 Gateway 服务
|
|
159
|
-
restart 重启 Gateway 服务
|
|
160
|
-
update 更新 Gateway(需重新安装)
|
|
161
|
-
status 查看服务状态
|
|
162
|
-
|
|
163
|
-
CORS 管理命令:
|
|
164
|
-
addCors <域名> 添加允许的跨域域名
|
|
165
|
-
removeCors <域名> 移除允许的跨域域名
|
|
166
|
-
listCors
|
|
167
|
-
|
|
168
|
-
其他:
|
|
169
|
-
--help, -h 显示本帮助信息
|
|
167
|
+
console.log(`我的助手 Gateway CLI
|
|
168
|
+
|
|
169
|
+
用法: gateway <命令>
|
|
170
|
+
|
|
171
|
+
服务管理命令:
|
|
172
|
+
install 安装 Gateway 服务(后台运行)
|
|
173
|
+
uninstall 卸载 Gateway 服务
|
|
174
|
+
start 启动 Gateway 服务
|
|
175
|
+
stop 停止 Gateway 服务
|
|
176
|
+
restart 重启 Gateway 服务
|
|
177
|
+
update 更新 Gateway(需重新安装)
|
|
178
|
+
status 查看服务状态
|
|
179
|
+
|
|
180
|
+
CORS 管理命令:
|
|
181
|
+
addCors <域名> 添加允许的跨域域名
|
|
182
|
+
removeCors <域名> 移除允许的跨域域名
|
|
183
|
+
listCors 列出所有跨域域名(含内置域名)
|
|
184
|
+
|
|
185
|
+
其他:
|
|
186
|
+
--help, -h 显示本帮助信息
|
|
170
187
|
`);
|
|
171
|
-
const domains = persistStore_js_1.persistStore.getCorsDomains();
|
|
172
|
-
if (domains.length === 0) {
|
|
173
|
-
console.log('暂无自定义 CORS 域名(仅使用内置内网规则)');
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
console.log(`自定义 CORS 域名 (${domains.length}):`);
|
|
177
|
-
domains.forEach(d => console.log(` - ${d}`));
|
|
178
|
-
}
|
|
179
188
|
}
|
|
180
189
|
else {
|
|
181
190
|
const fnMap = {
|
|
@@ -233,19 +242,31 @@ else {
|
|
|
233
242
|
if (lanPatterns.some(pattern => pattern.test(origin))) {
|
|
234
243
|
return callback(null, true);
|
|
235
244
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
245
|
+
let originHost = '';
|
|
246
|
+
try {
|
|
247
|
+
originHost = new URL(origin).hostname.toLowerCase();
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
originHost = origin.toLowerCase();
|
|
251
|
+
}
|
|
252
|
+
// 检查内置官方域名与用户自定义域名(均支持子域名匹配)
|
|
253
|
+
const allowedDomains = [...DEFAULT_CORS_DOMAINS, ...persistStore_js_1.persistStore.getCorsDomains()];
|
|
254
|
+
if (allowedDomains.some(d => matchDomain(originHost, d))) {
|
|
243
255
|
return callback(null, true);
|
|
244
256
|
}
|
|
245
257
|
callback(new Error('Not allowed by CORS'));
|
|
246
258
|
},
|
|
247
259
|
credentials: true,
|
|
248
260
|
};
|
|
261
|
+
// Chrome Private Network Access:公网页面(网页版)访问本机 Gateway 时,
|
|
262
|
+
// 预检请求会带 Access-Control-Request-Private-Network,必须显式放行。
|
|
263
|
+
// 必须注册在 cors() 之前,否则 cors 会先结束 OPTIONS 预检,响应头加不上。
|
|
264
|
+
app.use((req, res, next) => {
|
|
265
|
+
if (req.headers['access-control-request-private-network']) {
|
|
266
|
+
res.setHeader('Access-Control-Allow-Private-Network', 'true');
|
|
267
|
+
}
|
|
268
|
+
next();
|
|
269
|
+
});
|
|
249
270
|
app.use((0, cors_1.default)(corsOptions));
|
|
250
271
|
app.use(express_1.default.json({ limit: '10mb' }));
|
|
251
272
|
// Health check
|
|
@@ -112,65 +112,168 @@ function getStoredPid() {
|
|
|
112
112
|
* - 因此自杀场景改走 process.emit('SIGTERM'),复用 main.ts 已注册的优雅关闭逻辑,
|
|
113
113
|
* 并在响应返回后异步退出,避免 HTTP 调用方读不到结果。
|
|
114
114
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
/**
|
|
116
|
+
* 探测进程是否存活(signal 0 不会真的发信号)
|
|
117
|
+
*/
|
|
118
|
+
function isPidAlive(pid) {
|
|
119
|
+
try {
|
|
120
|
+
process.kill(pid, 0);
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 轮询等待指定进程退出,返回是否已退出
|
|
129
|
+
*/
|
|
130
|
+
async function waitForPidExit(pid, timeoutMs) {
|
|
131
|
+
const start = Date.now();
|
|
132
|
+
while (Date.now() - start < timeoutMs) {
|
|
133
|
+
if (!isPidAlive(pid))
|
|
134
|
+
return true;
|
|
135
|
+
await new Promise(r => setTimeout(r, 200));
|
|
136
|
+
}
|
|
137
|
+
return !isPidAlive(pid);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* 读取守护进程 PID
|
|
141
|
+
*/
|
|
142
|
+
function getDaemonPid() {
|
|
143
|
+
try {
|
|
144
|
+
if (!fs_1.default.existsSync(GATEWAY_DAEMON_PID_FILE))
|
|
145
|
+
return null;
|
|
146
|
+
// 去除 PowerShell Out-File -Encoding UTF8 写入的 BOM,否则 parseInt 会得到 NaN
|
|
147
|
+
const raw = fs_1.default.readFileSync(GATEWAY_DAEMON_PID_FILE, 'utf8').replace(/^\uFEFF/, '').trim();
|
|
148
|
+
const pid = parseInt(raw, 10);
|
|
149
|
+
return isNaN(pid) ? null : pid;
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 结束守护进程(运行 launcher.ps1 的那个 PowerShell)
|
|
157
|
+
*
|
|
158
|
+
* 必须在结束 gateway 之前调用:守护进程每 5s 轮询一次,若发现 gateway 已退出
|
|
159
|
+
* 且此刻停止标记不存在,就会立即重新拉起,表现为「stop 之后网关又活了」。
|
|
160
|
+
*/
|
|
161
|
+
async function stopDaemonProcess() {
|
|
162
|
+
if (process.platform !== 'win32')
|
|
118
163
|
return;
|
|
119
|
-
|
|
120
|
-
if (
|
|
164
|
+
const daemonPid = getDaemonPid();
|
|
165
|
+
if (daemonPid && daemonPid !== process.pid && isPidAlive(daemonPid)) {
|
|
121
166
|
try {
|
|
122
|
-
|
|
167
|
+
process.kill(daemonPid, 'SIGTERM');
|
|
123
168
|
}
|
|
124
169
|
catch { /* ignore */ }
|
|
125
|
-
|
|
126
|
-
setTimeout(() => {
|
|
170
|
+
if (!(await waitForPidExit(daemonPid, 3000))) {
|
|
127
171
|
try {
|
|
128
|
-
process.
|
|
172
|
+
process.kill(daemonPid, 'SIGKILL');
|
|
129
173
|
}
|
|
130
174
|
catch { /* ignore */ }
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}, 100).unref();
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
// —— 场景 2:停的是别人(孤儿进程 / 旧实例)—— 才真正发信号
|
|
137
|
-
try {
|
|
138
|
-
process.kill(pid, 'SIGTERM');
|
|
139
|
-
await new Promise(r => setTimeout(r, 2000));
|
|
175
|
+
await waitForPidExit(daemonPid, 2000);
|
|
176
|
+
}
|
|
140
177
|
}
|
|
141
|
-
catch { /* ignore */ }
|
|
142
|
-
// 强制终止(如果还没退出)
|
|
143
178
|
try {
|
|
144
|
-
|
|
179
|
+
fs_1.default.unlinkSync(GATEWAY_DAEMON_PID_FILE);
|
|
145
180
|
}
|
|
146
181
|
catch { /* ignore */ }
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* PID 文件丢失/过期时的兜底:按 exe 路径匹配并结束 gateway 进程
|
|
185
|
+
*
|
|
186
|
+
* PID 文件位于 %TEMP%,可能被系统清理策略删除,此时仅靠 PID 会漏杀。
|
|
187
|
+
* 仅在打包为 exe 时启用:开发模式下 execPath 是 node.exe,按路径匹配会误杀其它 Node 进程。
|
|
188
|
+
*/
|
|
189
|
+
async function stopGatewayByExePath() {
|
|
190
|
+
if (process.platform !== 'win32' || !isPackagedExe())
|
|
191
|
+
return 0;
|
|
192
|
+
const exe = process.execPath;
|
|
193
|
+
const name = path_1.default.basename(exe, path_1.default.extname(exe));
|
|
194
|
+
const q = (v) => v.replace(/'/g, "''");
|
|
147
195
|
try {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
catch { /* ignore */ }
|
|
161
|
-
await new Promise(r => setTimeout(r, 500));
|
|
196
|
+
const { stdout } = await execAsync(`powershell -NoProfile -Command "Get-Process -Name '${q(name)}' -ErrorAction SilentlyContinue | ` +
|
|
197
|
+
`Where-Object { $_.Id -ne ${process.pid} -and $_.Path -ieq '${q(exe)}' } | ` +
|
|
198
|
+
`ForEach-Object { $_.Id }"`, { timeout: 10000, windowsHide: true });
|
|
199
|
+
const pids = stdout.split(/\r?\n/)
|
|
200
|
+
.map(line => parseInt(line.trim(), 10))
|
|
201
|
+
.filter(n => !isNaN(n) && n !== process.pid);
|
|
202
|
+
for (const orphan of pids) {
|
|
203
|
+
try {
|
|
204
|
+
process.kill(orphan, 'SIGTERM');
|
|
205
|
+
}
|
|
206
|
+
catch { /* ignore */ }
|
|
207
|
+
if (!(await waitForPidExit(orphan, 3000))) {
|
|
162
208
|
try {
|
|
163
|
-
process.kill(
|
|
209
|
+
process.kill(orphan, 'SIGKILL');
|
|
164
210
|
}
|
|
165
211
|
catch { /* ignore */ }
|
|
166
212
|
}
|
|
167
213
|
}
|
|
214
|
+
return pids.length;
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return 0;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 停止 Gateway 用户进程(优雅终止)
|
|
222
|
+
*
|
|
223
|
+
* 注意:当 pid 指向当前进程自己时,绝不能使用 process.kill(pid)。
|
|
224
|
+
* - Windows 上 Node 的 process.kill 等同 TerminateProcess,无清理机会,
|
|
225
|
+
* server.close() / WebSocket / 文件句柄都不会被释放,会导致后续 exe 解锁超时。
|
|
226
|
+
* - 因此自杀场景改走 process.emit('SIGTERM'),复用 main.ts 已注册的优雅关闭逻辑,
|
|
227
|
+
* 并在响应返回后异步退出,避免 HTTP 调用方读不到结果。
|
|
228
|
+
*
|
|
229
|
+
* 返回:self 表示停的是当前进程(无法在本进程内校验最终结果),
|
|
230
|
+
* stopped 表示已确认目标进程退出。
|
|
231
|
+
*/
|
|
232
|
+
async function stopGatewayProcess() {
|
|
233
|
+
const pid = getStoredPid();
|
|
234
|
+
// 先断开守护进程,否则它会在 gateway 退出后立刻把它重新拉起
|
|
235
|
+
await stopDaemonProcess();
|
|
236
|
+
// —— 场景 1:要停的就是自己 —— 走优雅退出
|
|
237
|
+
if (pid && pid === process.pid) {
|
|
238
|
+
try {
|
|
239
|
+
fs_1.default.unlinkSync(GATEWAY_PID_FILE);
|
|
240
|
+
}
|
|
168
241
|
catch { /* ignore */ }
|
|
242
|
+
// 异步触发,确保当前调用栈(含 HTTP 响应)有机会先返回
|
|
243
|
+
setTimeout(() => {
|
|
244
|
+
try {
|
|
245
|
+
process.emit('SIGTERM');
|
|
246
|
+
}
|
|
247
|
+
catch { /* ignore */ }
|
|
248
|
+
// 兜底:5s 内 SIGTERM handler 没把进程关掉,再强退
|
|
249
|
+
setTimeout(() => process.exit(0), 5000).unref();
|
|
250
|
+
}, 100).unref();
|
|
251
|
+
return { stopped: true, self: true };
|
|
252
|
+
}
|
|
253
|
+
// —— 场景 2:停的是别人(CLI 停后台实例 / 孤儿进程)—— 才真正发信号
|
|
254
|
+
let stopped = true;
|
|
255
|
+
if (pid && isPidAlive(pid)) {
|
|
169
256
|
try {
|
|
170
|
-
|
|
257
|
+
process.kill(pid, 'SIGTERM');
|
|
171
258
|
}
|
|
172
259
|
catch { /* ignore */ }
|
|
260
|
+
stopped = await waitForPidExit(pid, 5000);
|
|
261
|
+
if (!stopped) {
|
|
262
|
+
// 优雅关闭超时,强制终止
|
|
263
|
+
try {
|
|
264
|
+
process.kill(pid, 'SIGKILL');
|
|
265
|
+
}
|
|
266
|
+
catch { /* ignore */ }
|
|
267
|
+
stopped = await waitForPidExit(pid, 3000);
|
|
268
|
+
}
|
|
173
269
|
}
|
|
270
|
+
try {
|
|
271
|
+
fs_1.default.unlinkSync(GATEWAY_PID_FILE);
|
|
272
|
+
}
|
|
273
|
+
catch { /* ignore */ }
|
|
274
|
+
// 兜底:清理没被 PID 文件记录到的残留进程
|
|
275
|
+
await stopGatewayByExePath();
|
|
276
|
+
return { stopped, self: false };
|
|
174
277
|
}
|
|
175
278
|
/**
|
|
176
279
|
* 写入 Gateway 启动脚本(用户级,不弹出窗口)
|
|
@@ -226,8 +329,14 @@ function writeGatewayLauncherScript(exePath, scriptPath, workDir) {
|
|
|
226
329
|
'# 守护循环:监控进程状态,异常退出则自动重启',
|
|
227
330
|
'while ($true) {',
|
|
228
331
|
' try {',
|
|
229
|
-
' # 每
|
|
230
|
-
' Start-Sleep -Seconds
|
|
332
|
+
' # 每 1 秒检查一次:轮询间隔过长会错过 stop 写入的停止标记,导致误重启',
|
|
333
|
+
' Start-Sleep -Seconds 1',
|
|
334
|
+
'',
|
|
335
|
+
' # 优先判定停止标记:无论 gateway 是否已退出,主动停止时守护都应该退出',
|
|
336
|
+
' if (Test-Path $stopFlag) {',
|
|
337
|
+
" Write-Log '检测到停止标记,守护进程退出'",
|
|
338
|
+
' break',
|
|
339
|
+
' }',
|
|
231
340
|
'',
|
|
232
341
|
' # 进程已退出',
|
|
233
342
|
' if ($proc.HasExited) {',
|
|
@@ -364,6 +473,11 @@ async function startServiceWindows() {
|
|
|
364
473
|
return { success: true, message: 'Gateway 已在运行' };
|
|
365
474
|
}
|
|
366
475
|
try {
|
|
476
|
+
// 清除上一次 stop 可能残留的停止标记,否则新守护会立即退出
|
|
477
|
+
try {
|
|
478
|
+
fs_1.default.unlinkSync(GATEWAY_STOP_FLAG_FILE);
|
|
479
|
+
}
|
|
480
|
+
catch { /* ignore */ }
|
|
367
481
|
const exe = getGatewayExePath();
|
|
368
482
|
const workDir = path_1.default.dirname(exe);
|
|
369
483
|
if (!fs_1.default.existsSync(GATEWAY_LAUNCHER_FILE)) {
|
|
@@ -388,9 +502,15 @@ async function startServiceWindows() {
|
|
|
388
502
|
}
|
|
389
503
|
async function restartServiceWindows() {
|
|
390
504
|
try {
|
|
391
|
-
await stopService();
|
|
505
|
+
const stopResult = await stopService();
|
|
506
|
+
if (!stopResult.success) {
|
|
507
|
+
return { success: false, message: `重启失败(停止阶段): ${stopResult.message}` };
|
|
508
|
+
}
|
|
392
509
|
await new Promise(r => setTimeout(r, 1000));
|
|
393
|
-
await startService();
|
|
510
|
+
const startResult = await startService();
|
|
511
|
+
if (!startResult.success) {
|
|
512
|
+
return { success: false, message: `重启失败(启动阶段): ${startResult.message}` };
|
|
513
|
+
}
|
|
394
514
|
return { success: true, message: 'Gateway 重启成功' };
|
|
395
515
|
}
|
|
396
516
|
catch (err) {
|
|
@@ -399,23 +519,33 @@ async function restartServiceWindows() {
|
|
|
399
519
|
}
|
|
400
520
|
async function stopServiceWindows() {
|
|
401
521
|
try {
|
|
522
|
+
if (!isGatewayRunning() && !getDaemonPid()) {
|
|
523
|
+
return { success: true, message: 'Gateway 未在运行' };
|
|
524
|
+
}
|
|
402
525
|
// 写入停止标记,告知守护进程这是主动停止,不要重启
|
|
403
526
|
try {
|
|
527
|
+
fs_1.default.mkdirSync(path_1.default.dirname(GATEWAY_STOP_FLAG_FILE), { recursive: true });
|
|
404
528
|
fs_1.default.writeFileSync(GATEWAY_STOP_FLAG_FILE, '1', 'utf8');
|
|
405
529
|
}
|
|
406
530
|
catch { /* ignore */ }
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
531
|
+
const { stopped, self } = await stopGatewayProcess();
|
|
532
|
+
// 自停场景(HTTP 接口调用)退出发生在本函数返回之后,无法在此校验,
|
|
533
|
+
// 也不能删停止标记(它要留给守护进程判定主动停止)
|
|
534
|
+
if (self) {
|
|
535
|
+
return { success: true, message: 'Gateway 正在退出' };
|
|
536
|
+
}
|
|
537
|
+
// 确认退出后才能清理停止标记,否则残留守护会把 gateway 重新拉起
|
|
538
|
+
if (!stopped) {
|
|
539
|
+
return { success: false, message: 'Gateway 进程未能终止,请手动结束进程后重试' };
|
|
540
|
+
}
|
|
411
541
|
try {
|
|
412
542
|
fs_1.default.unlinkSync(GATEWAY_STOP_FLAG_FILE);
|
|
413
543
|
}
|
|
414
544
|
catch { /* ignore */ }
|
|
415
|
-
|
|
416
|
-
|
|
545
|
+
// 最终校验:避免在没真正停掉时还报成功
|
|
546
|
+
if (isGatewayRunning()) {
|
|
547
|
+
return { success: false, message: 'Gateway 仍在运行,停止失败' };
|
|
417
548
|
}
|
|
418
|
-
catch { /* ignore */ }
|
|
419
549
|
return { success: true, message: 'Gateway 已停止' };
|
|
420
550
|
}
|
|
421
551
|
catch (err) {
|
|
@@ -451,21 +581,21 @@ async function installLinux() {
|
|
|
451
581
|
if (installed)
|
|
452
582
|
await stopService();
|
|
453
583
|
const execStart = isPkg ? exe : `${exe} ${script}`;
|
|
454
|
-
const unitContent = `[Unit]
|
|
455
|
-
Description=${exports.SERVICE_DISPLAY_NAME}
|
|
456
|
-
After=network.target
|
|
457
|
-
|
|
458
|
-
[Service]
|
|
459
|
-
Type=simple
|
|
460
|
-
User=${process.env.USER || 'root'}
|
|
461
|
-
WorkingDirectory=${workDir}
|
|
462
|
-
ExecStart=${execStart}
|
|
463
|
-
Restart=always
|
|
464
|
-
RestartSec=5
|
|
465
|
-
Environment=NODE_ENV=production
|
|
466
|
-
|
|
467
|
-
[Install]
|
|
468
|
-
WantedBy=multi-user.target
|
|
584
|
+
const unitContent = `[Unit]
|
|
585
|
+
Description=${exports.SERVICE_DISPLAY_NAME}
|
|
586
|
+
After=network.target
|
|
587
|
+
|
|
588
|
+
[Service]
|
|
589
|
+
Type=simple
|
|
590
|
+
User=${process.env.USER || 'root'}
|
|
591
|
+
WorkingDirectory=${workDir}
|
|
592
|
+
ExecStart=${execStart}
|
|
593
|
+
Restart=always
|
|
594
|
+
RestartSec=5
|
|
595
|
+
Environment=NODE_ENV=production
|
|
596
|
+
|
|
597
|
+
[Install]
|
|
598
|
+
WantedBy=multi-user.target
|
|
469
599
|
`;
|
|
470
600
|
await fs_1.default.promises.writeFile('/tmp/myassis-gateway.service', unitContent, 'utf8');
|
|
471
601
|
await execAsync('cp /tmp/myassis-gateway.service /etc/systemd/system/myassis-gateway.service', { timeout: 10000 });
|
|
@@ -495,6 +495,59 @@ class Session {
|
|
|
495
495
|
lines.push('调用时必须传入完整步骤列表,且同一时刻最多一个步骤为 in_progress。');
|
|
496
496
|
return lines.join('\n');
|
|
497
497
|
}
|
|
498
|
+
/**
|
|
499
|
+
* 把当前计划推送到 Desktop。
|
|
500
|
+
*
|
|
501
|
+
* 用动态 import 获取 webSocketService:WebSocketService 反向依赖本文件的
|
|
502
|
+
* handleApprovalResponse,静态引入会形成循环依赖。
|
|
503
|
+
*/
|
|
504
|
+
async notifyPlanUpdate() {
|
|
505
|
+
try {
|
|
506
|
+
const { webSocketService } = await Promise.resolve().then(() => __importStar(require('../WebSocketService.js')));
|
|
507
|
+
webSocketService.sendToUser(String(this.userId), {
|
|
508
|
+
type: 'plan_updated',
|
|
509
|
+
payload: {
|
|
510
|
+
sessionId: this.id,
|
|
511
|
+
agentId: this.agentId,
|
|
512
|
+
steps: this.plan,
|
|
513
|
+
updatedAt: this.planUpdatedAt,
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
catch (error) {
|
|
518
|
+
logger.error('推送计划更新失败:', error);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* 构造「收尾时计划未完成」的提醒。
|
|
523
|
+
*
|
|
524
|
+
* 模型准备结束回复时,如果计划里还有未完成的步骤,说明它要么漏了同步,
|
|
525
|
+
* 要么还有事情没做完。返回提醒文案要求它先处理;无需提醒时返回 null。
|
|
526
|
+
*/
|
|
527
|
+
buildPlanFinalizeReminder() {
|
|
528
|
+
if (this.plan.length === 0 || this.isPlanCompleted())
|
|
529
|
+
return null;
|
|
530
|
+
const unfinished = this.plan.filter(item => item.status !== 'completed');
|
|
531
|
+
const list = unfinished.map(item => `「${item.step}」`).join('、');
|
|
532
|
+
return `【计划未完成提醒】你准备结束本轮回复,但计划中仍有 ${unfinished.length} 个步骤未标记完成:${list}。
|
|
533
|
+
`
|
|
534
|
+
+ `请判断:如果这些步骤其实已经做完了,立即调用 updatePlan 把它们全部置为 completed;
|
|
535
|
+
`
|
|
536
|
+
+ `如果确实还没做,请继续完成剩余工作并同步计划状态。不要在计划未收尾的情况下结束回复。`;
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* 本轮结束时兜底处理未完成的计划。
|
|
540
|
+
*
|
|
541
|
+
* 即使提醒过模型,它仍可能不收尾。此时清空计划,避免 Desktop 的
|
|
542
|
+
* 进度圈永远停留在未完成状态。
|
|
543
|
+
*/
|
|
544
|
+
async finalizePlanOnComplete() {
|
|
545
|
+
if (this.plan.length === 0 || this.isPlanCompleted())
|
|
546
|
+
return;
|
|
547
|
+
logger.warn(`会话 ${this.id} 结束时计划仍未完成,已自动清除计划以释放进度圈`);
|
|
548
|
+
this.clearPlan();
|
|
549
|
+
await this.notifyPlanUpdate();
|
|
550
|
+
}
|
|
498
551
|
// 清除未读消息数并持久化
|
|
499
552
|
clearUnreadCount() {
|
|
500
553
|
this.unreadCount = 0;
|
|
@@ -645,6 +698,8 @@ class Session {
|
|
|
645
698
|
let toolRound = 0;
|
|
646
699
|
// 计划模式:距上次调用 updatePlan 已经过的工具轮次,用于在中间过程强制提醒模型同步进度
|
|
647
700
|
let roundsSincePlanUpdate = 0;
|
|
701
|
+
// 计划模式:收尾时提醒模型补完计划的次数,避免反复提醒导致死循环
|
|
702
|
+
let planFinalizeNudges = 0;
|
|
648
703
|
// 当前裁剪级别,遇到上下文超限时逐级加重
|
|
649
704
|
let trimLevel = ContextBuilder_js_1.TrimLevel.OldToolPayload;
|
|
650
705
|
/** 判断错误是否为上下文超限 */
|
|
@@ -999,7 +1054,25 @@ class Session {
|
|
|
999
1054
|
return await processModelResponse();
|
|
1000
1055
|
}
|
|
1001
1056
|
else {
|
|
1002
|
-
// ==========
|
|
1057
|
+
// ========== 没有工具调用,准备结束本轮 ==========
|
|
1058
|
+
// 计划模式:计划还没收尾时,先提醒模型补完状态(最多提醒一次,避免死循环)
|
|
1059
|
+
if (planFinalizeNudges < 1) {
|
|
1060
|
+
const finalizeReminder = this.buildPlanFinalizeReminder();
|
|
1061
|
+
if (finalizeReminder) {
|
|
1062
|
+
planFinalizeNudges++;
|
|
1063
|
+
messages.push({
|
|
1064
|
+
role: 'assistant',
|
|
1065
|
+
content: llmResult.content || llmResult.reasoningContent || '',
|
|
1066
|
+
attachments: []
|
|
1067
|
+
});
|
|
1068
|
+
messages.push({
|
|
1069
|
+
role: 'user',
|
|
1070
|
+
content: finalizeReminder,
|
|
1071
|
+
attachments: []
|
|
1072
|
+
});
|
|
1073
|
+
return await processModelResponse();
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1003
1076
|
if (llmResult.content || llmResult.reasoningContent) {
|
|
1004
1077
|
// 分段发送内容,每段约50个字符
|
|
1005
1078
|
const chunkSize = 1;
|
|
@@ -1056,6 +1129,8 @@ class Session {
|
|
|
1056
1129
|
logger.error('Error closing SSE connection:', e);
|
|
1057
1130
|
}
|
|
1058
1131
|
}
|
|
1132
|
+
// 计划模式:本轮已结束,若计划仍未完成则兜底清除,避免进度圈残留
|
|
1133
|
+
void this.finalizePlanOnComplete();
|
|
1059
1134
|
// 非当前查看的 session,助手回复完成后增加未读计数
|
|
1060
1135
|
(0, SessionManager_js_1.getSessionManager)(this.userId).onMessageComplete(this.id);
|
|
1061
1136
|
// 只在非 abort 的情况下保存(abort 由 stopGenerating 负责保存)
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.updatePlanTool = void 0;
|
|
4
4
|
const SessionManager_js_1 = require("../session/SessionManager.js");
|
|
5
|
-
const WebSocketService_js_1 = require("../WebSocketService.js");
|
|
6
5
|
const shared_1 = require("@myassis/shared");
|
|
7
6
|
const logger = (0, shared_1.getLogger)('PlanTool');
|
|
8
7
|
const VALID_STATUS = ['pending', 'in_progress', 'completed'];
|
|
@@ -84,15 +83,7 @@ exports.updatePlanTool = {
|
|
|
84
83
|
}
|
|
85
84
|
session.updatePlan(steps);
|
|
86
85
|
// 推送计划进度到 Desktop,驱动底部进度圈刷新
|
|
87
|
-
|
|
88
|
-
type: 'plan_updated',
|
|
89
|
-
payload: {
|
|
90
|
-
sessionId,
|
|
91
|
-
agentId: session.agentId,
|
|
92
|
-
steps: session.plan,
|
|
93
|
-
updatedAt: session.planUpdatedAt,
|
|
94
|
-
},
|
|
95
|
-
});
|
|
86
|
+
await session.notifyPlanUpdate();
|
|
96
87
|
const completed = steps.filter(s => s.status === 'completed').length;
|
|
97
88
|
return {
|
|
98
89
|
success: true,
|