ai-lab-bridge 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/index.js +24 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -136,6 +136,7 @@ const BRIDGE_VERSION = (() => {
|
|
|
136
136
|
// 连接参数由各子命令解析填充(connect/daemon 需要,status/stop 不需要)
|
|
137
137
|
let SERVER = "";
|
|
138
138
|
let TOKEN = "";
|
|
139
|
+
let sessionCwd = ""; // 当前任务的实际工作目录(emitSession 回传用)
|
|
139
140
|
|
|
140
141
|
// ---------------------------------------------------------------------------
|
|
141
142
|
// CLI 能力探测
|
|
@@ -234,7 +235,11 @@ class Session {
|
|
|
234
235
|
emitSession(cliSessionId) {
|
|
235
236
|
if (!cliSessionId || this.cliSessionId) return;
|
|
236
237
|
this.cliSessionId = cliSessionId;
|
|
237
|
-
this.emitEvent("session", {
|
|
238
|
+
this.emitEvent("session", {
|
|
239
|
+
cli_session_id: cliSessionId,
|
|
240
|
+
// 设备实际执行的工作目录(CLI cwd 或 bridge 进程启动目录),平台前端展示用
|
|
241
|
+
working_dir: sessionCwd || null,
|
|
242
|
+
});
|
|
238
243
|
}
|
|
239
244
|
|
|
240
245
|
/**
|
|
@@ -720,6 +725,8 @@ function startSession(message) {
|
|
|
720
725
|
|
|
721
726
|
const timeoutSeconds = Number(options.timeout_seconds) > 0 ? Number(options.timeout_seconds) : 0;
|
|
722
727
|
const cwd = typeof options.cwd === "string" && options.cwd ? options.cwd : process.cwd();
|
|
728
|
+
// 供 emitSession 回传实际工作目录(Session 构造在 cwd 解析之前,用模块级变量传递)
|
|
729
|
+
sessionCwd = cwd;
|
|
723
730
|
let argv;
|
|
724
731
|
|
|
725
732
|
let promptViaStdin = true;
|
|
@@ -942,7 +949,8 @@ async function connect() {
|
|
|
942
949
|
for (const cap of capabilities) {
|
|
943
950
|
console.log(`[bridge] ${cap.cli}: ${cap.available ? `可用 (${cap.version})` : "未安装"}`);
|
|
944
951
|
}
|
|
945
|
-
|
|
952
|
+
// working_dir:bridge 进程启动目录,作为未配置 cwd 时任务的默认工作目录(平台前端展示)
|
|
953
|
+
send({ type: "register", capabilities, working_dir: process.cwd() });
|
|
946
954
|
startHeartbeat();
|
|
947
955
|
} else if (message.type === "registered") {
|
|
948
956
|
console.log("[bridge] 能力注册完成,等待任务下发");
|
|
@@ -1168,6 +1176,14 @@ async function cmdConnect() {
|
|
|
1168
1176
|
persistConfig(SERVER, TOKEN);
|
|
1169
1177
|
|
|
1170
1178
|
if (args.foreground) {
|
|
1179
|
+
// 前台运行前先停掉已有后台 daemon:否则旧进程仍在连接平台发心跳,
|
|
1180
|
+
// 且控制面文件被本进程覆盖后,stop/status 将无法再管理它(成为孤儿进程)
|
|
1181
|
+
const ctl = readCtlState();
|
|
1182
|
+
if (ctl && isPidAlive(ctl.pid)) {
|
|
1183
|
+
console.log(`[bridge] 检测到后台进程正在运行(pid=${ctl.pid}),先停止...`);
|
|
1184
|
+
await stopDaemonByCtl(ctl);
|
|
1185
|
+
console.log(`[bridge] 后台进程已停止 (pid=${ctl.pid})`);
|
|
1186
|
+
}
|
|
1171
1187
|
await daemonMain();
|
|
1172
1188
|
return;
|
|
1173
1189
|
}
|
|
@@ -1283,6 +1299,12 @@ async function cmdStop() {
|
|
|
1283
1299
|
console.log("[bridge] 后台进程未运行。");
|
|
1284
1300
|
return;
|
|
1285
1301
|
}
|
|
1302
|
+
await stopDaemonByCtl(ctl);
|
|
1303
|
+
console.log(`[bridge] 后台进程已停止 (pid=${ctl.pid})`);
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
/** 按 ctl 记录停止指定 daemon:控制面 /stop → 等待退出 → 强杀兜底 → 清理 ctl 文件。 */
|
|
1307
|
+
async function stopDaemonByCtl(ctl) {
|
|
1286
1308
|
try {
|
|
1287
1309
|
await fetch(`http://127.0.0.1:${ctl.port}/stop`, {
|
|
1288
1310
|
method: "POST",
|
|
@@ -1304,7 +1326,6 @@ async function cmdStop() {
|
|
|
1304
1326
|
} catch {}
|
|
1305
1327
|
}
|
|
1306
1328
|
removeCtlFiles();
|
|
1307
|
-
console.log(`[bridge] 后台进程已停止 (pid=${ctl.pid})`);
|
|
1308
1329
|
}
|
|
1309
1330
|
|
|
1310
1331
|
// ---------------------------------------------------------------------------
|