@keyou007/agent-bot 0.1.1 → 0.1.3
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/.env.example +3 -1
- package/CHANGELOG.md +15 -0
- package/README.md +23 -6
- package/README.zh.md +23 -6
- package/config.example.yaml +1 -2
- package/dist/cli/FeishuAppRegistration.js +12 -2
- package/dist/cli/FeishuAppRegistration.js.map +1 -1
- package/dist/cli/Initializer.js +164 -14
- package/dist/cli/Initializer.js.map +1 -1
- package/dist/cli/LocalControlClient.js +14 -1
- package/dist/cli/LocalControlClient.js.map +1 -1
- package/dist/cli/ServerStarter.js +69 -0
- package/dist/cli/ServerStarter.js.map +1 -0
- package/dist/cli/help.js +6 -2
- package/dist/cli/help.js.map +1 -1
- package/dist/cli/profile.js +40 -0
- package/dist/cli/profile.js.map +1 -0
- package/dist/cli.js +93 -89
- package/dist/cli.js.map +1 -1
- package/dist/config/loadConfig.js +7 -4
- package/dist/config/loadConfig.js.map +1 -1
- package/dist/config/paths.js +1 -0
- package/dist/config/paths.js.map +1 -1
- package/dist/config/schema.js +2 -1
- package/dist/config/schema.js.map +1 -1
- package/dist/feishu/CardRenderer.js +21 -3
- package/dist/feishu/CardRenderer.js.map +1 -1
- package/dist/feishu/FeishuConnector.js +8 -11
- package/dist/feishu/FeishuConnector.js.map +1 -1
- package/dist/feishu/transport.js +4 -11
- package/dist/feishu/transport.js.map +1 -1
- package/dist/index.js +20 -8
- package/dist/index.js.map +1 -1
- package/dist/proxy/ProxySessionController.js +18 -3
- package/dist/proxy/ProxySessionController.js.map +1 -1
- package/dist/startup/StartupNotifier.js +13 -2
- package/dist/startup/StartupNotifier.js.map +1 -1
- package/dist/startup/startFeishu.js +2 -1
- package/dist/startup/startFeishu.js.map +1 -1
- package/dist/supervision/SafeRestartNotifier.js +23 -7
- package/dist/supervision/SafeRestartNotifier.js.map +1 -1
- package/dist/supervision/SafeRestartScheduler.js +20 -2
- package/dist/supervision/SafeRestartScheduler.js.map +1 -1
- package/dist/supervision/SupervisorDiagnostics.js +209 -0
- package/dist/supervision/SupervisorDiagnostics.js.map +1 -0
- package/dist/supervisor.js +54 -16
- package/dist/supervisor.js.map +1 -1
- package/docs/technical-reference.md +28 -14
- package/docs/technical-reference.zh.md +28 -14
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/agent-bot/SKILL.md +24 -4
- package/src/cli/FeishuAppRegistration.ts +14 -1
- package/src/cli/Initializer.ts +174 -14
- package/src/cli/LocalControlClient.ts +12 -1
- package/src/cli/ServerStarter.ts +106 -0
- package/src/cli/help.ts +6 -2
- package/src/cli/profile.ts +59 -0
- package/src/cli.ts +114 -84
- package/src/config/loadConfig.ts +12 -4
- package/src/config/paths.ts +1 -0
- package/src/config/schema.ts +2 -1
- package/src/feishu/CardRenderer.ts +25 -3
- package/src/feishu/FeishuConnector.ts +8 -12
- package/src/feishu/transport.ts +7 -16
- package/src/index.ts +31 -8
- package/src/proxy/ProxySessionController.ts +18 -3
- package/src/startup/StartupNotifier.ts +21 -4
- package/src/startup/startFeishu.ts +2 -0
- package/src/supervision/SafeRestartNotifier.ts +22 -7
- package/src/supervision/SafeRestartScheduler.ts +27 -3
- package/src/supervision/SupervisorDiagnostics.ts +268 -0
- package/src/supervisor.ts +53 -15
package/src/supervisor.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { spawn, type ChildProcess } from "node:child_process";
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
|
-
import {
|
|
3
|
+
import { isServerReachable } from "./cli/LocalControlClient.js";
|
|
4
4
|
import { controlEndpoint } from "./cli/controlProtocol.js";
|
|
5
5
|
import { loadConfig } from "./config/loadConfig.js";
|
|
6
|
+
import { requireServerFeishuTransport } from "./feishu/transport.js";
|
|
6
7
|
import {
|
|
7
8
|
crashRestartDelayMs,
|
|
8
9
|
describeRestartReason,
|
|
@@ -11,10 +12,15 @@ import {
|
|
|
11
12
|
STABLE_UPTIME_MS,
|
|
12
13
|
STOP_EXIT_CODE,
|
|
13
14
|
} from "./supervision/restartPolicy.js";
|
|
15
|
+
import { SupervisorDiagnostics, nodeDiagnosticReportArguments } from "./supervision/SupervisorDiagnostics.js";
|
|
14
16
|
|
|
15
17
|
const childEntry = fileURLToPath(new URL("./index.js", import.meta.url));
|
|
16
|
-
const
|
|
18
|
+
const config = loadConfig();
|
|
19
|
+
requireServerFeishuTransport(config.feishu);
|
|
20
|
+
const sqlitePath = config.storage.sqlitePath;
|
|
17
21
|
const serverEndpoint = controlEndpoint(sqlitePath);
|
|
22
|
+
const diagnostics = new SupervisorDiagnostics(config);
|
|
23
|
+
diagnostics.initialize();
|
|
18
24
|
let child: ChildProcess | undefined;
|
|
19
25
|
let restartTimer: NodeJS.Timeout | undefined;
|
|
20
26
|
let stopping = false;
|
|
@@ -23,7 +29,7 @@ let nextStartReason = process.env.AGENT_BOT_RESTART_REASON?.trim() || "Superviso
|
|
|
23
29
|
|
|
24
30
|
async function startChild(): Promise<void> {
|
|
25
31
|
if (stopping || child) return;
|
|
26
|
-
if (await
|
|
32
|
+
if (await isServerReachable(serverEndpoint)) {
|
|
27
33
|
writeSupervisorLog("existing_server_detected", { endpoint: serverEndpoint });
|
|
28
34
|
process.exit(0);
|
|
29
35
|
return;
|
|
@@ -31,19 +37,33 @@ async function startChild(): Promise<void> {
|
|
|
31
37
|
const startedAt = Date.now();
|
|
32
38
|
const restartReason = nextStartReason;
|
|
33
39
|
nextStartReason = "Supervisor 重新拉起进程";
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
const workerStderr = diagnostics.openWorkerStderr();
|
|
41
|
+
try {
|
|
42
|
+
child = spawn(process.execPath, [
|
|
43
|
+
...nodeDiagnosticReportArguments(diagnostics.paths.crashReportDirectory),
|
|
44
|
+
childEntry,
|
|
45
|
+
], {
|
|
46
|
+
cwd: process.cwd(),
|
|
47
|
+
env: { ...process.env, AGENT_BOT_SUPERVISED: "1", AGENT_BOT_RESTART_REASON: restartReason },
|
|
48
|
+
stdio: ["ignore", "ignore", workerStderr],
|
|
49
|
+
windowsHide: true,
|
|
50
|
+
});
|
|
51
|
+
} finally {
|
|
52
|
+
diagnostics.closeWorkerStderr(workerStderr);
|
|
53
|
+
}
|
|
54
|
+
const workerPid = child.pid;
|
|
55
|
+
writeSupervisorLog("started", {
|
|
56
|
+
pid: child.pid,
|
|
57
|
+
restartReason,
|
|
58
|
+
workerStderrPath: diagnostics.paths.workerStderrPath,
|
|
59
|
+
crashReportDirectory: diagnostics.paths.crashReportDirectory,
|
|
39
60
|
});
|
|
40
|
-
writeSupervisorLog("started", { pid: child.pid, restartReason });
|
|
41
61
|
|
|
42
62
|
child.once("error", (error) => {
|
|
43
|
-
writeSupervisorLog("spawn_error", { error: error.message });
|
|
63
|
+
writeSupervisorLog("spawn_error", { pid: workerPid, error: error.message });
|
|
44
64
|
});
|
|
45
65
|
child.once("exit", (code, signal) => {
|
|
46
|
-
void handleChildExit(code, signal, startedAt);
|
|
66
|
+
void handleChildExit(code, signal, startedAt, workerPid);
|
|
47
67
|
});
|
|
48
68
|
}
|
|
49
69
|
|
|
@@ -51,10 +71,11 @@ async function handleChildExit(
|
|
|
51
71
|
code: number | null,
|
|
52
72
|
signal: NodeJS.Signals | null,
|
|
53
73
|
startedAt: number,
|
|
74
|
+
workerPid: number | undefined,
|
|
54
75
|
): Promise<void> {
|
|
55
76
|
child = undefined;
|
|
56
77
|
const uptimeMs = Date.now() - startedAt;
|
|
57
|
-
writeSupervisorLog("exited", { code, signal, uptimeMs });
|
|
78
|
+
writeSupervisorLog("exited", { pid: workerPid, code, signal, uptimeMs });
|
|
58
79
|
if (stopping) {
|
|
59
80
|
process.exit(0);
|
|
60
81
|
return;
|
|
@@ -64,7 +85,7 @@ async function handleChildExit(
|
|
|
64
85
|
process.exit(0);
|
|
65
86
|
return;
|
|
66
87
|
}
|
|
67
|
-
if (await
|
|
88
|
+
if (await isServerReachable(serverEndpoint)) {
|
|
68
89
|
writeSupervisorLog("duplicate_supervisor_stopped", { endpoint: serverEndpoint });
|
|
69
90
|
process.exit(0);
|
|
70
91
|
return;
|
|
@@ -77,7 +98,24 @@ async function handleChildExit(
|
|
|
77
98
|
const delayMs = intentional
|
|
78
99
|
? INTENTIONAL_RESTART_DELAY_MS
|
|
79
100
|
: crashRestartDelayMs(consecutiveFailures);
|
|
80
|
-
|
|
101
|
+
if (!intentional) {
|
|
102
|
+
diagnostics.recordCrash({
|
|
103
|
+
workerPid,
|
|
104
|
+
exitCode: code,
|
|
105
|
+
signal,
|
|
106
|
+
startedAt: new Date(startedAt).toISOString(),
|
|
107
|
+
exitedAt: new Date().toISOString(),
|
|
108
|
+
uptimeMs,
|
|
109
|
+
consecutiveFailures,
|
|
110
|
+
restartDelayMs: delayMs,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
writeSupervisorLog("restarting", {
|
|
114
|
+
previousPid: workerPid,
|
|
115
|
+
delayMs,
|
|
116
|
+
consecutiveFailures,
|
|
117
|
+
intentional,
|
|
118
|
+
});
|
|
81
119
|
restartTimer = setTimeout(() => void startChild(), delayMs);
|
|
82
120
|
}
|
|
83
121
|
|
|
@@ -93,7 +131,7 @@ function stop(signal: NodeJS.Signals): void {
|
|
|
93
131
|
}
|
|
94
132
|
|
|
95
133
|
function writeSupervisorLog(event: string, data: Record<string, unknown>): void {
|
|
96
|
-
|
|
134
|
+
diagnostics.writeEvent(event, data);
|
|
97
135
|
}
|
|
98
136
|
|
|
99
137
|
process.on("SIGINT", () => stop("SIGINT"));
|