-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdoctor.js
More file actions
128 lines (114 loc) · 3.67 KB
/
doctor.js
File metadata and controls
128 lines (114 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 健康检查模块(借鉴 cc-connect DoctorChecker)
// /doctor 命令:全面诊断 bridge 运行状态
import { existsSync } from "fs";
export async function runHealthCheck(ctx) {
const {
adapters = {},
activeBackends = [],
sessions = null,
cronManager = null,
rateLimiter = null,
idleMonitor = null,
dirManager = null,
a2aBus = null,
sharedContextConfig = null,
cwd = "",
chatId = 0,
} = ctx;
const lines = ["🩺 *Bridge Health Check*\n"];
// 1. Backend 连通性
for (const name of activeBackends) {
const adapter = adapters[name];
if (adapter) {
try {
const info = adapter.statusInfo?.();
lines.push(`✅ ${adapter.label || name}: ready (${info?.model || "default"})`);
} catch (e) {
lines.push(`❌ ${adapter.label || name}: ${e.message}`);
}
} else {
lines.push(`❌ ${name}: adapter not loaded`);
}
}
// 2. 工作目录
if (cwd && existsSync(cwd)) {
lines.push(`✅ 工作目录: ${cwd}`);
} else {
lines.push(`❌ 工作目录: ${cwd || "(未设置)"} ${cwd ? "不存在" : ""}`);
}
// 3. Session DB
const sessionsDbPath = process.env.SESSIONS_DB;
if (sessionsDbPath && existsSync(sessionsDbPath)) {
try {
if (sessions?.getSession) {
lines.push(`✅ Session DB: ${sessionsDbPath}`);
} else {
lines.push(`✅ Session DB: ${sessionsDbPath} (file exists)`);
}
} catch {
lines.push(`❌ Session DB: 读取失败`);
}
} else {
lines.push(`⚠️ Session DB: ${sessionsDbPath || "(未配置)"}`);
}
// 4. Tasks DB
const tasksDbPath = process.env.TASKS_DB;
if (tasksDbPath && existsSync(tasksDbPath)) {
lines.push(`✅ Tasks DB: ${tasksDbPath}`);
} else {
lines.push(`⚠️ Tasks DB: ${tasksDbPath || "(未配置)"}`);
}
// 5. A2A Bus
if (a2aBus) {
try {
const stats = a2aBus.getStats?.();
lines.push(`✅ A2A Bus: port=${process.env.A2A_PORT || "?"}, received=${stats?.received || 0}`);
} catch {
lines.push(`❌ A2A Bus: 状态获取失败`);
}
} else {
lines.push(`⏭️ A2A: disabled`);
}
// 6. Shared Context
if (sharedContextConfig) {
const backend = sharedContextConfig.sharedContextBackend || "sqlite";
lines.push(`✅ Shared Context: ${backend}`);
} else {
lines.push(`⏭️ Shared Context: disabled`);
}
// 7. Cron
if (cronManager) {
const jobList = cronManager.list(chatId);
const activeCount = jobList.filter((j) => j.status === "active").length;
const nextJob = jobList.find((j) => j.nextRun);
const nextStr = nextJob
? `, next: ${new Date(nextJob.nextRun).toLocaleTimeString("zh-CN")}`
: "";
lines.push(`✅ Cron: ${activeCount} active / ${jobList.length} total${nextStr}`);
} else {
lines.push(`⏭️ Cron: disabled`);
}
// 8. Rate Limiter
if (rateLimiter) {
const stats = rateLimiter.stats(chatId);
lines.push(`📊 限流: ${stats.used}/${stats.max} used (window ${Math.round(stats.windowMs / 1000)}s)`);
}
// 9. Idle Monitor
if (idleMonitor) {
const info = idleMonitor.statusInfo();
const timeoutStr = info.idleTimeoutMs > 0
? `${Math.round(info.idleTimeoutMs / 60000)}min`
: "off";
const resetStr = info.resetOnIdleMs > 0
? `${Math.round(info.resetOnIdleMs / 60000)}min`
: "off";
lines.push(`📊 Idle: timeout=${timeoutStr}, reset=${resetStr}, sessions=${info.activeSessions}`);
}
// 10. Dir Manager
if (dirManager) {
const currentDir = dirManager.current(chatId);
const hist = dirManager.history(chatId);
lines.push(`📊 目录: ${currentDir} (history: ${hist.length})`);
}
return lines.join("\n");
}