@steadwing/openalerts 0.2.4 → 0.2.6
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/README.md +198 -137
- package/dist/channels/console.d.ts +6 -0
- package/dist/channels/console.d.ts.map +1 -0
- package/dist/channels/console.js +10 -0
- package/dist/channels/console.js.map +1 -0
- package/dist/channels/telegram.d.ts +12 -0
- package/dist/channels/telegram.d.ts.map +1 -0
- package/dist/channels/telegram.js +28 -0
- package/dist/channels/telegram.js.map +1 -0
- package/dist/channels/webhook.d.ts +8 -0
- package/dist/channels/webhook.d.ts.map +1 -0
- package/dist/channels/webhook.js +15 -0
- package/dist/channels/webhook.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +234 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +51 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +86 -0
- package/dist/config.js.map +1 -0
- package/dist/core/alert-channel.d.ts +3 -10
- package/dist/core/alert-channel.d.ts.map +1 -0
- package/dist/core/alert-channel.js +9 -30
- package/dist/core/alert-channel.js.map +1 -0
- package/dist/core/bounded-map.d.ts +1 -0
- package/dist/core/bounded-map.d.ts.map +1 -0
- package/dist/core/bounded-map.js +1 -0
- package/dist/core/bounded-map.js.map +1 -0
- package/dist/core/engine.d.ts +6 -18
- package/dist/core/engine.d.ts.map +1 -0
- package/dist/core/engine.js +49 -98
- package/dist/core/engine.js.map +1 -0
- package/dist/core/evaluator.d.ts +1 -0
- package/dist/core/evaluator.d.ts.map +1 -0
- package/dist/core/evaluator.js +1 -0
- package/dist/core/evaluator.js.map +1 -0
- package/dist/core/event-bus.d.ts +1 -0
- package/dist/core/event-bus.d.ts.map +1 -0
- package/dist/core/event-bus.js +1 -0
- package/dist/core/event-bus.js.map +1 -0
- package/dist/core/formatter.d.ts +1 -0
- package/dist/core/formatter.d.ts.map +1 -0
- package/dist/core/formatter.js +1 -0
- package/dist/core/formatter.js.map +1 -0
- package/dist/core/rules.d.ts +1 -0
- package/dist/core/rules.d.ts.map +1 -0
- package/dist/core/rules.js +98 -0
- package/dist/core/rules.js.map +1 -0
- package/dist/core/store.d.ts +6 -9
- package/dist/core/store.d.ts.map +1 -0
- package/dist/core/store.js +43 -96
- package/dist/core/store.js.map +1 -0
- package/dist/core/types.d.ts +2 -1
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +1 -0
- package/dist/core/types.js.map +1 -0
- package/dist/db/index.d.ts +6 -0
- package/dist/db/index.d.ts.map +1 -0
- package/dist/db/index.js +31 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/queries.d.ts +157 -0
- package/dist/db/queries.d.ts.map +1 -0
- package/dist/db/queries.js +221 -0
- package/dist/db/queries.js.map +1 -0
- package/dist/db/schema.d.ts +5 -0
- package/dist/db/schema.d.ts.map +1 -0
- package/dist/db/schema.js +177 -0
- package/dist/db/schema.js.map +1 -0
- package/dist/readers/openclaw.d.ts +11 -0
- package/dist/readers/openclaw.d.ts.map +1 -0
- package/dist/readers/openclaw.js +267 -0
- package/dist/readers/openclaw.js.map +1 -0
- package/dist/server/dashboard.d.ts +2 -0
- package/dist/server/dashboard.d.ts.map +1 -0
- package/dist/server/dashboard.js +765 -0
- package/dist/server/dashboard.js.map +1 -0
- package/dist/server/index.d.ts +10 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +28 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/routes.d.ts +6 -0
- package/dist/server/routes.d.ts.map +1 -0
- package/dist/server/routes.js +146 -0
- package/dist/server/routes.js.map +1 -0
- package/dist/server/sse.d.ts +21 -0
- package/dist/server/sse.d.ts.map +1 -0
- package/dist/server/sse.js +53 -0
- package/dist/server/sse.js.map +1 -0
- package/dist/watchers/files.d.ts +19 -0
- package/dist/watchers/files.d.ts.map +1 -0
- package/dist/watchers/files.js +105 -0
- package/dist/watchers/files.js.map +1 -0
- package/dist/watchers/gateway-adapter.d.ts +18 -0
- package/dist/watchers/gateway-adapter.d.ts.map +1 -0
- package/dist/watchers/gateway-adapter.js +273 -0
- package/dist/watchers/gateway-adapter.js.map +1 -0
- package/dist/watchers/gateway.d.ts +27 -0
- package/dist/watchers/gateway.d.ts.map +1 -0
- package/dist/watchers/gateway.js +131 -0
- package/dist/watchers/gateway.js.map +1 -0
- package/package.json +29 -43
- package/LICENSE +0 -201
- package/dist/core/index.d.ts +0 -12
- package/dist/core/index.js +0 -23
- package/dist/core/llm-enrichment.d.ts +0 -21
- package/dist/core/llm-enrichment.js +0 -180
- package/dist/core/platform.d.ts +0 -17
- package/dist/core/platform.js +0 -93
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -213
- package/dist/plugin/adapter.d.ts +0 -150
- package/dist/plugin/adapter.js +0 -530
- package/dist/plugin/commands.d.ts +0 -18
- package/dist/plugin/commands.js +0 -103
- package/dist/plugin/dashboard-html.d.ts +0 -7
- package/dist/plugin/dashboard-html.js +0 -938
- package/dist/plugin/dashboard-routes.d.ts +0 -7
- package/dist/plugin/dashboard-routes.js +0 -336
- package/dist/plugin/gateway-client.d.ts +0 -39
- package/dist/plugin/gateway-client.js +0 -193
- package/dist/plugin/log-bridge.d.ts +0 -22
- package/dist/plugin/log-bridge.js +0 -363
- package/openclaw.plugin.json +0 -57
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import type { OpenAlertsEngine } from "../core/index.js";
|
|
3
|
-
type HttpHandler = (req: IncomingMessage, res: ServerResponse) => Promise<boolean> | boolean;
|
|
4
|
-
/** Close all active SSE connections. Call on engine stop. */
|
|
5
|
-
export declare function closeDashboardConnections(): void;
|
|
6
|
-
export declare function createDashboardHandler(getEngine: () => OpenAlertsEngine | null): HttpHandler;
|
|
7
|
-
export {};
|
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
import { readFileSync, existsSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { DEFAULTS } from "../core/index.js";
|
|
4
|
-
import { getDashboardHtml } from "./dashboard-html.js";
|
|
5
|
-
// ─── SSE connection tracking ─────────────────────────────────────────────────
|
|
6
|
-
const sseConnections = new Set();
|
|
7
|
-
/** Close all active SSE connections. Call on engine stop. */
|
|
8
|
-
export function closeDashboardConnections() {
|
|
9
|
-
for (const conn of sseConnections) {
|
|
10
|
-
clearInterval(conn.heartbeat);
|
|
11
|
-
clearInterval(conn.logTailer);
|
|
12
|
-
conn.unsub();
|
|
13
|
-
try {
|
|
14
|
-
conn.res.end();
|
|
15
|
-
}
|
|
16
|
-
catch {
|
|
17
|
-
/* already closed */
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
sseConnections.clear();
|
|
21
|
-
}
|
|
22
|
-
// ─── Rule status helper ──────────────────────────────────────────────────────
|
|
23
|
-
const RULE_IDS = [
|
|
24
|
-
"infra-errors",
|
|
25
|
-
"llm-errors",
|
|
26
|
-
"session-stuck",
|
|
27
|
-
"heartbeat-fail",
|
|
28
|
-
"queue-depth",
|
|
29
|
-
"high-error-rate",
|
|
30
|
-
"tool-errors",
|
|
31
|
-
"gateway-down",
|
|
32
|
-
];
|
|
33
|
-
function getRuleStatuses(engine) {
|
|
34
|
-
const state = engine.state;
|
|
35
|
-
const now = Date.now();
|
|
36
|
-
const cooldownWindow = 15 * 60 * 1000;
|
|
37
|
-
return RULE_IDS.map((id) => {
|
|
38
|
-
// For gateway-down, reflect current condition: if heartbeats have resumed,
|
|
39
|
-
// show OK even if the rule fired recently.
|
|
40
|
-
if (id === "gateway-down") {
|
|
41
|
-
const silenceMs = state.lastHeartbeatTs > 0
|
|
42
|
-
? now - state.lastHeartbeatTs
|
|
43
|
-
: 0;
|
|
44
|
-
const isCurrentlyDown = state.lastHeartbeatTs > 0 &&
|
|
45
|
-
silenceMs >= DEFAULTS.gatewayDownThresholdMs;
|
|
46
|
-
return { id, status: isCurrentlyDown ? "fired" : "ok" };
|
|
47
|
-
}
|
|
48
|
-
// Cooldown keys are fingerprints like "llm-errors:unknown", not bare rule IDs.
|
|
49
|
-
// Check if ANY cooldown key starting with this rule ID has fired recently.
|
|
50
|
-
let fired = false;
|
|
51
|
-
for (const [key, ts] of state.cooldowns) {
|
|
52
|
-
if (key === id || key.startsWith(id + ":")) {
|
|
53
|
-
if (ts > now - cooldownWindow) {
|
|
54
|
-
fired = true;
|
|
55
|
-
break;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return { id, status: fired ? "fired" : "ok" };
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
// ─── OpenClaw log file ──────────────────────────────────────────────────────
|
|
63
|
-
function getOpenClawLogDir() {
|
|
64
|
-
// Use platform-appropriate default: C:\tmp\openclaw on Windows, /tmp/openclaw elsewhere
|
|
65
|
-
if (process.platform === "win32") {
|
|
66
|
-
return join("C:", "tmp", "openclaw");
|
|
67
|
-
}
|
|
68
|
-
return join("/tmp", "openclaw");
|
|
69
|
-
}
|
|
70
|
-
function getOpenClawLogPath() {
|
|
71
|
-
const d = new Date();
|
|
72
|
-
const y = d.getFullYear();
|
|
73
|
-
const m = String(d.getMonth() + 1).padStart(2, "0");
|
|
74
|
-
const day = String(d.getDate()).padStart(2, "0");
|
|
75
|
-
return join(getOpenClawLogDir(), `openclaw-${y}-${m}-${day}.log`);
|
|
76
|
-
}
|
|
77
|
-
/** Parse key=value pairs from a log message string. */
|
|
78
|
-
function parseLogKVs(msg) {
|
|
79
|
-
const kvs = {};
|
|
80
|
-
// Match key=value or key="quoted value"
|
|
81
|
-
const re = /(\w+)=(?:"([^"]*)"|(\S+))/g;
|
|
82
|
-
let m;
|
|
83
|
-
while ((m = re.exec(msg)) !== null) {
|
|
84
|
-
kvs[m[1]] = m[2] ?? m[3];
|
|
85
|
-
}
|
|
86
|
-
return kvs;
|
|
87
|
-
}
|
|
88
|
-
function parseLogLine(line) {
|
|
89
|
-
try {
|
|
90
|
-
const obj = JSON.parse(line);
|
|
91
|
-
const ts = obj.time || obj._meta?.date || "";
|
|
92
|
-
const tsMs = ts ? new Date(ts).getTime() : 0;
|
|
93
|
-
let subsystem = "";
|
|
94
|
-
try {
|
|
95
|
-
const nameObj = JSON.parse(obj["0"] || "{}");
|
|
96
|
-
subsystem = nameObj.subsystem || "";
|
|
97
|
-
}
|
|
98
|
-
catch {
|
|
99
|
-
subsystem = obj["0"] || "";
|
|
100
|
-
}
|
|
101
|
-
const message = obj["1"] || "";
|
|
102
|
-
const extra = parseLogKVs(message);
|
|
103
|
-
return {
|
|
104
|
-
ts,
|
|
105
|
-
tsMs,
|
|
106
|
-
level: obj._meta?.logLevelName || "DEBUG",
|
|
107
|
-
subsystem,
|
|
108
|
-
message,
|
|
109
|
-
sessionId: extra.sessionId,
|
|
110
|
-
runId: extra.runId,
|
|
111
|
-
durationMs: extra.durationMs ? parseInt(extra.durationMs, 10) : undefined,
|
|
112
|
-
filePath: obj._meta?.path?.fileNameWithLine,
|
|
113
|
-
method: obj._meta?.path?.method,
|
|
114
|
-
hostname: obj._meta?.hostname,
|
|
115
|
-
extra,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
catch {
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
function readOpenClawLogs(maxEntries, afterTs) {
|
|
123
|
-
const logPath = getOpenClawLogPath();
|
|
124
|
-
if (!existsSync(logPath))
|
|
125
|
-
return { entries: [], truncated: false, subsystems: [] };
|
|
126
|
-
try {
|
|
127
|
-
const content = readFileSync(logPath, "utf-8");
|
|
128
|
-
const lines = content.trim().split("\n").filter(Boolean);
|
|
129
|
-
const entries = [];
|
|
130
|
-
const subsystemSet = new Set();
|
|
131
|
-
for (const line of lines) {
|
|
132
|
-
const parsed = parseLogLine(line);
|
|
133
|
-
if (!parsed)
|
|
134
|
-
continue;
|
|
135
|
-
if (afterTs && parsed.ts <= afterTs)
|
|
136
|
-
continue;
|
|
137
|
-
entries.push(parsed);
|
|
138
|
-
subsystemSet.add(parsed.subsystem);
|
|
139
|
-
}
|
|
140
|
-
const truncated = maxEntries > 0 && entries.length > maxEntries;
|
|
141
|
-
const sliced = maxEntries > 0 ? entries.slice(-maxEntries) : entries;
|
|
142
|
-
const subsystems = Array.from(subsystemSet).sort();
|
|
143
|
-
return { entries: sliced, truncated, subsystems };
|
|
144
|
-
}
|
|
145
|
-
catch {
|
|
146
|
-
return { entries: [], truncated: false, subsystems: [] };
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Create a log tailer for an SSE connection.
|
|
151
|
-
* Polls the log file every 2 seconds, sends new lines as `event: oclog`.
|
|
152
|
-
*/
|
|
153
|
-
function createLogTailer(res) {
|
|
154
|
-
let lastLineCount = 0;
|
|
155
|
-
let lastPath = "";
|
|
156
|
-
// Initialize: count existing lines so we only send new ones
|
|
157
|
-
try {
|
|
158
|
-
const path = getOpenClawLogPath();
|
|
159
|
-
if (existsSync(path)) {
|
|
160
|
-
const content = readFileSync(path, "utf-8");
|
|
161
|
-
lastLineCount = content.trim().split("\n").length;
|
|
162
|
-
lastPath = path;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
catch {
|
|
166
|
-
/* ignore */
|
|
167
|
-
}
|
|
168
|
-
return setInterval(() => {
|
|
169
|
-
try {
|
|
170
|
-
const path = getOpenClawLogPath();
|
|
171
|
-
if (!existsSync(path))
|
|
172
|
-
return;
|
|
173
|
-
// If date changed (new log file), reset counter
|
|
174
|
-
if (path !== lastPath) {
|
|
175
|
-
lastLineCount = 0;
|
|
176
|
-
lastPath = path;
|
|
177
|
-
}
|
|
178
|
-
const content = readFileSync(path, "utf-8");
|
|
179
|
-
const lines = content.trim().split("\n");
|
|
180
|
-
if (lines.length <= lastLineCount)
|
|
181
|
-
return;
|
|
182
|
-
// Send only new lines
|
|
183
|
-
const newLines = lines.slice(lastLineCount);
|
|
184
|
-
lastLineCount = lines.length;
|
|
185
|
-
for (const line of newLines) {
|
|
186
|
-
const parsed = parseLogLine(line);
|
|
187
|
-
if (!parsed)
|
|
188
|
-
continue;
|
|
189
|
-
res.write(`event: oclog\ndata: ${JSON.stringify(parsed)}\n\n`);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
catch {
|
|
193
|
-
/* ignore read errors */
|
|
194
|
-
}
|
|
195
|
-
}, 2000);
|
|
196
|
-
}
|
|
197
|
-
// ─── HTTP Handler ────────────────────────────────────────────────────────────
|
|
198
|
-
export function createDashboardHandler(getEngine) {
|
|
199
|
-
return async (req, res) => {
|
|
200
|
-
const url = req.url ?? "";
|
|
201
|
-
if (!url.startsWith("/openalerts"))
|
|
202
|
-
return false;
|
|
203
|
-
const engine = getEngine();
|
|
204
|
-
// ── GET /openalerts → Dashboard HTML ──────────────────────
|
|
205
|
-
if ((url === "/openalerts" || url === "/openalerts/") &&
|
|
206
|
-
req.method === "GET") {
|
|
207
|
-
if (!engine) {
|
|
208
|
-
res.writeHead(503, { "Content-Type": "text/plain" });
|
|
209
|
-
res.end("OpenAlerts engine not running.");
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
res.writeHead(200, {
|
|
213
|
-
"Content-Type": "text/html; charset=utf-8",
|
|
214
|
-
"Cache-Control": "no-cache",
|
|
215
|
-
});
|
|
216
|
-
res.end(getDashboardHtml());
|
|
217
|
-
return true;
|
|
218
|
-
}
|
|
219
|
-
// ── GET /openalerts/events → SSE stream (engine events + log tailing) ──
|
|
220
|
-
if (url === "/openalerts/events" && req.method === "GET") {
|
|
221
|
-
if (!engine) {
|
|
222
|
-
res.writeHead(503, { "Content-Type": "text/plain" });
|
|
223
|
-
res.end("Engine not running.");
|
|
224
|
-
return true;
|
|
225
|
-
}
|
|
226
|
-
res.writeHead(200, {
|
|
227
|
-
"Content-Type": "text/event-stream",
|
|
228
|
-
"Cache-Control": "no-cache",
|
|
229
|
-
Connection: "keep-alive",
|
|
230
|
-
"Access-Control-Allow-Origin": "*",
|
|
231
|
-
});
|
|
232
|
-
res.flushHeaders();
|
|
233
|
-
// Send initial connection event so the browser knows the stream is live
|
|
234
|
-
res.write(`:ok\n\n`);
|
|
235
|
-
// Send current state snapshot as initial event
|
|
236
|
-
const state = engine.state;
|
|
237
|
-
res.write(`event: state\ndata: ${JSON.stringify({
|
|
238
|
-
uptimeMs: Date.now() - state.startedAt,
|
|
239
|
-
stats: state.stats,
|
|
240
|
-
rules: getRuleStatuses(engine),
|
|
241
|
-
})}\n\n`);
|
|
242
|
-
// Send event history so dashboard survives refreshes
|
|
243
|
-
const history = engine.getRecentLiveEvents(200);
|
|
244
|
-
if (history.length > 0) {
|
|
245
|
-
res.write(`event: history\ndata: ${JSON.stringify(history)}\n\n`);
|
|
246
|
-
}
|
|
247
|
-
// Subscribe to engine events
|
|
248
|
-
const unsub = engine.bus.on((event) => {
|
|
249
|
-
try {
|
|
250
|
-
res.write(`event: openalerts\ndata: ${JSON.stringify(event)}\n\n`);
|
|
251
|
-
}
|
|
252
|
-
catch {
|
|
253
|
-
/* closed */
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
// Start log file tailer — streams OpenClaw internal logs as `event: oclog`
|
|
257
|
-
const logTailer = createLogTailer(res);
|
|
258
|
-
// Heartbeat every 15s
|
|
259
|
-
const heartbeat = setInterval(() => {
|
|
260
|
-
try {
|
|
261
|
-
res.write(":heartbeat\n\n");
|
|
262
|
-
}
|
|
263
|
-
catch {
|
|
264
|
-
/* closed */
|
|
265
|
-
}
|
|
266
|
-
}, 15_000);
|
|
267
|
-
const conn = { res, unsub, heartbeat, logTailer };
|
|
268
|
-
sseConnections.add(conn);
|
|
269
|
-
req.on("close", () => {
|
|
270
|
-
clearInterval(heartbeat);
|
|
271
|
-
clearInterval(logTailer);
|
|
272
|
-
unsub();
|
|
273
|
-
sseConnections.delete(conn);
|
|
274
|
-
});
|
|
275
|
-
return true;
|
|
276
|
-
}
|
|
277
|
-
// ── GET /openalerts/state → JSON snapshot ─────────────────
|
|
278
|
-
if (url === "/openalerts/state" && req.method === "GET") {
|
|
279
|
-
if (!engine) {
|
|
280
|
-
res.writeHead(503, { "Content-Type": "application/json" });
|
|
281
|
-
res.end(JSON.stringify({ error: "Engine not running" }));
|
|
282
|
-
return true;
|
|
283
|
-
}
|
|
284
|
-
const state = engine.state;
|
|
285
|
-
const recentAlerts = engine
|
|
286
|
-
.getRecentEvents(50)
|
|
287
|
-
.filter((e) => e.type === "alert");
|
|
288
|
-
const body = JSON.stringify({
|
|
289
|
-
uptimeMs: Date.now() - state.startedAt,
|
|
290
|
-
startedAt: state.startedAt,
|
|
291
|
-
lastHeartbeatTs: state.lastHeartbeatTs,
|
|
292
|
-
hourlyAlerts: state.hourlyAlerts,
|
|
293
|
-
stuckSessions: state.stats.stuckSessions,
|
|
294
|
-
lastResetTs: state.stats.lastResetTs,
|
|
295
|
-
stats: state.stats,
|
|
296
|
-
busListeners: engine.bus.size,
|
|
297
|
-
platformConnected: engine.platformConnected,
|
|
298
|
-
recentAlerts: recentAlerts.slice(0, 20),
|
|
299
|
-
rules: getRuleStatuses(engine),
|
|
300
|
-
cooldowns: Object.fromEntries(state.cooldowns),
|
|
301
|
-
});
|
|
302
|
-
res.writeHead(200, {
|
|
303
|
-
"Content-Type": "application/json",
|
|
304
|
-
"Cache-Control": "no-cache",
|
|
305
|
-
"Access-Control-Allow-Origin": "*",
|
|
306
|
-
});
|
|
307
|
-
res.end(body);
|
|
308
|
-
return true;
|
|
309
|
-
}
|
|
310
|
-
// ── GET /openalerts/logs → OpenClaw log entries (for Logs tab) ────
|
|
311
|
-
if (url.startsWith("/openalerts/logs") && req.method === "GET") {
|
|
312
|
-
const urlObj = new URL(url, "http://localhost");
|
|
313
|
-
const rawLimit = urlObj.searchParams.get("limit") || "200";
|
|
314
|
-
// limit=0 means "no limit" — return all log entries
|
|
315
|
-
const limit = rawLimit === "0" ? 0 : Math.min(parseInt(rawLimit, 10), 50000);
|
|
316
|
-
const afterTs = urlObj.searchParams.get("after") || undefined;
|
|
317
|
-
const result = readOpenClawLogs(limit, afterTs);
|
|
318
|
-
res.writeHead(200, {
|
|
319
|
-
"Content-Type": "application/json",
|
|
320
|
-
"Cache-Control": "no-cache",
|
|
321
|
-
"Access-Control-Allow-Origin": "*",
|
|
322
|
-
});
|
|
323
|
-
res.end(JSON.stringify({
|
|
324
|
-
entries: result.entries,
|
|
325
|
-
truncated: result.truncated,
|
|
326
|
-
subsystems: result.subsystems,
|
|
327
|
-
logFile: getOpenClawLogPath(),
|
|
328
|
-
}));
|
|
329
|
-
return true;
|
|
330
|
-
}
|
|
331
|
-
// Unknown /openalerts sub-route
|
|
332
|
-
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
333
|
-
res.end("Not found");
|
|
334
|
-
return true;
|
|
335
|
-
};
|
|
336
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "eventemitter3";
|
|
2
|
-
interface GatewayClientConfig {
|
|
3
|
-
url: string;
|
|
4
|
-
token: string;
|
|
5
|
-
reconnectInterval: number;
|
|
6
|
-
maxRetries: number;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* WebSocket client for OpenClaw Gateway
|
|
10
|
-
* - Connects to ws://127.0.0.1:18789
|
|
11
|
-
* - Handles JSON-RPC requests/responses
|
|
12
|
-
* - Emits events: agent, health, cron, chat
|
|
13
|
-
* - Auto-reconnects on disconnect
|
|
14
|
-
*/
|
|
15
|
-
export declare class GatewayClient extends EventEmitter {
|
|
16
|
-
private ws;
|
|
17
|
-
private config;
|
|
18
|
-
private pending;
|
|
19
|
-
private backoffMs;
|
|
20
|
-
private closed;
|
|
21
|
-
private connectTimer;
|
|
22
|
-
private ready;
|
|
23
|
-
constructor(config?: Partial<GatewayClientConfig>);
|
|
24
|
-
start(): void;
|
|
25
|
-
stop(): void;
|
|
26
|
-
isReady(): boolean;
|
|
27
|
-
private doConnect;
|
|
28
|
-
private sendConnectHandshake;
|
|
29
|
-
private handleMessage;
|
|
30
|
-
/**
|
|
31
|
-
* Send RPC request to gateway
|
|
32
|
-
* @example
|
|
33
|
-
* const cost = await client.request("usage.cost", { period: "day" });
|
|
34
|
-
* const sessions = await client.request("sessions.list");
|
|
35
|
-
*/
|
|
36
|
-
request<T = unknown>(method: string, params?: unknown): Promise<T>;
|
|
37
|
-
private scheduleReconnect;
|
|
38
|
-
}
|
|
39
|
-
export {};
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { WebSocket } from "ws";
|
|
3
|
-
import EventEmitter from "eventemitter3";
|
|
4
|
-
/**
|
|
5
|
-
* WebSocket client for OpenClaw Gateway
|
|
6
|
-
* - Connects to ws://127.0.0.1:18789
|
|
7
|
-
* - Handles JSON-RPC requests/responses
|
|
8
|
-
* - Emits events: agent, health, cron, chat
|
|
9
|
-
* - Auto-reconnects on disconnect
|
|
10
|
-
*/
|
|
11
|
-
export class GatewayClient extends EventEmitter {
|
|
12
|
-
ws = null;
|
|
13
|
-
config;
|
|
14
|
-
pending = new Map();
|
|
15
|
-
backoffMs = 1000;
|
|
16
|
-
closed = false;
|
|
17
|
-
connectTimer = null;
|
|
18
|
-
ready = false;
|
|
19
|
-
constructor(config) {
|
|
20
|
-
super();
|
|
21
|
-
this.config = {
|
|
22
|
-
url: config?.url ?? "ws://127.0.0.1:18789",
|
|
23
|
-
token: config?.token ?? "",
|
|
24
|
-
reconnectInterval: config?.reconnectInterval ?? 1000,
|
|
25
|
-
maxRetries: config?.maxRetries ?? 60,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
start() {
|
|
29
|
-
if (this.closed) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
this.doConnect();
|
|
33
|
-
}
|
|
34
|
-
stop() {
|
|
35
|
-
this.closed = true;
|
|
36
|
-
if (this.connectTimer) {
|
|
37
|
-
clearTimeout(this.connectTimer);
|
|
38
|
-
this.connectTimer = null;
|
|
39
|
-
}
|
|
40
|
-
if (this.ws) {
|
|
41
|
-
this.ws.close();
|
|
42
|
-
this.ws = null;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
isReady() {
|
|
46
|
-
return this.ready;
|
|
47
|
-
}
|
|
48
|
-
doConnect() {
|
|
49
|
-
if (this.closed || this.ws) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
this.ws = new WebSocket(this.config.url, {
|
|
53
|
-
maxPayload: 25 * 1024 * 1024,
|
|
54
|
-
});
|
|
55
|
-
this.ws.on("open", () => {
|
|
56
|
-
this.backoffMs = 1000;
|
|
57
|
-
// Delay connect handshake slightly to allow gateway to send challenge
|
|
58
|
-
setTimeout(() => this.sendConnectHandshake(), 800);
|
|
59
|
-
});
|
|
60
|
-
this.ws.on("message", (data) => {
|
|
61
|
-
this.handleMessage(data.toString());
|
|
62
|
-
});
|
|
63
|
-
this.ws.on("error", (err) => {
|
|
64
|
-
this.emit("error", err);
|
|
65
|
-
});
|
|
66
|
-
this.ws.on("close", () => {
|
|
67
|
-
this.ws = null;
|
|
68
|
-
this.ready = false;
|
|
69
|
-
this.emit("disconnected");
|
|
70
|
-
if (!this.closed) {
|
|
71
|
-
this.scheduleReconnect();
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
sendConnectHandshake() {
|
|
76
|
-
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const id = randomUUID();
|
|
80
|
-
const frame = {
|
|
81
|
-
type: "req",
|
|
82
|
-
id,
|
|
83
|
-
method: "connect",
|
|
84
|
-
params: {
|
|
85
|
-
minProtocol: 3,
|
|
86
|
-
maxProtocol: 3,
|
|
87
|
-
client: {
|
|
88
|
-
id: "gateway-client",
|
|
89
|
-
displayName: "OpenAlerts Monitor",
|
|
90
|
-
version: "0.1.0",
|
|
91
|
-
platform: process.platform,
|
|
92
|
-
mode: "backend",
|
|
93
|
-
},
|
|
94
|
-
role: "operator",
|
|
95
|
-
scopes: ["operator.admin"],
|
|
96
|
-
auth: {
|
|
97
|
-
token: this.config.token,
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
this.pending.set(id, {
|
|
102
|
-
resolve: (result) => {
|
|
103
|
-
this.ready = true;
|
|
104
|
-
this.emit("ready", result);
|
|
105
|
-
},
|
|
106
|
-
reject: (err) => {
|
|
107
|
-
this.emit("error", new Error(`Connect handshake failed: ${err.message}`));
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
this.ws.send(JSON.stringify(frame));
|
|
111
|
-
}
|
|
112
|
-
handleMessage(raw) {
|
|
113
|
-
try {
|
|
114
|
-
const frame = JSON.parse(raw);
|
|
115
|
-
if (frame.type === "res") {
|
|
116
|
-
const pending = this.pending.get(frame.id);
|
|
117
|
-
if (pending) {
|
|
118
|
-
if (frame.error || frame.ok === false) {
|
|
119
|
-
const errMsg = typeof frame.error === "string"
|
|
120
|
-
? frame.error
|
|
121
|
-
: typeof frame.payload === "object" &&
|
|
122
|
-
frame.payload &&
|
|
123
|
-
"message" in frame.payload
|
|
124
|
-
? String(frame.payload.message)
|
|
125
|
-
: JSON.stringify(frame.error ?? frame.payload);
|
|
126
|
-
pending.reject(new Error(errMsg));
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
pending.resolve(frame.payload ?? frame.result);
|
|
130
|
-
}
|
|
131
|
-
this.pending.delete(frame.id);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
else if (frame.type === "event") {
|
|
135
|
-
// Emit the event for subscribers: agent, health, cron, chat
|
|
136
|
-
this.emit(frame.event, frame.payload);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
catch (err) {
|
|
140
|
-
this.emit("error", new Error(`Failed to parse frame: ${err}`));
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Send RPC request to gateway
|
|
145
|
-
* @example
|
|
146
|
-
* const cost = await client.request("usage.cost", { period: "day" });
|
|
147
|
-
* const sessions = await client.request("sessions.list");
|
|
148
|
-
*/
|
|
149
|
-
request(method, params) {
|
|
150
|
-
if (!this.ready || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
151
|
-
return Promise.reject(new Error("Gateway not ready"));
|
|
152
|
-
}
|
|
153
|
-
const id = randomUUID();
|
|
154
|
-
return new Promise((resolve, reject) => {
|
|
155
|
-
const timeout = setTimeout(() => {
|
|
156
|
-
this.pending.delete(id);
|
|
157
|
-
reject(new Error(`Request timeout: ${method}`));
|
|
158
|
-
}, 10000);
|
|
159
|
-
this.pending.set(id, {
|
|
160
|
-
resolve: (value) => {
|
|
161
|
-
clearTimeout(timeout);
|
|
162
|
-
resolve(value);
|
|
163
|
-
},
|
|
164
|
-
reject: (err) => {
|
|
165
|
-
clearTimeout(timeout);
|
|
166
|
-
reject(err);
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
const frame = {
|
|
170
|
-
type: "req",
|
|
171
|
-
id,
|
|
172
|
-
method,
|
|
173
|
-
params,
|
|
174
|
-
};
|
|
175
|
-
if (!this.ws) {
|
|
176
|
-
this.pending.delete(id);
|
|
177
|
-
clearTimeout(timeout);
|
|
178
|
-
reject(new Error("WebSocket not connected"));
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
this.ws.send(JSON.stringify(frame));
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
scheduleReconnect() {
|
|
185
|
-
if (this.connectTimer) {
|
|
186
|
-
clearTimeout(this.connectTimer);
|
|
187
|
-
}
|
|
188
|
-
this.connectTimer = setTimeout(() => {
|
|
189
|
-
this.backoffMs = Math.min(this.backoffMs * 2, 60000);
|
|
190
|
-
this.doConnect();
|
|
191
|
-
}, this.backoffMs);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { OpenAlertsEngine } from "../core/index.js";
|
|
2
|
-
import type { LogTransportRecord } from "openclaw/plugin-sdk";
|
|
3
|
-
/**
|
|
4
|
-
* Create a log-transport bridge that parses structured log records and
|
|
5
|
-
* synthesizes OpenAlertsEvents to fill gaps left by non-firing plugin hooks.
|
|
6
|
-
*
|
|
7
|
-
* Covers:
|
|
8
|
-
* - Tool calls (tool start → tool end, with duration) — fills `after_tool_call` gap
|
|
9
|
-
* - Session lifecycle (idle ↔ processing transitions) — fills `session_start/end` gap
|
|
10
|
-
* - Run prompt duration — enriches session.end with durationMs
|
|
11
|
-
* - Agent run lifecycle — agent start/end at run level
|
|
12
|
-
* - Compaction events — detects costly context compaction
|
|
13
|
-
* - Message delivery — "Committed messaging text" fills `message_sent` gap
|
|
14
|
-
* - Exec commands — attaches elevated commands to tool events
|
|
15
|
-
*
|
|
16
|
-
* Returns the transport function to pass to registerLogTransport().
|
|
17
|
-
* Call the returned cleanup function to release internal state.
|
|
18
|
-
*/
|
|
19
|
-
export declare function createLogBridge(engine: OpenAlertsEngine): {
|
|
20
|
-
transport: (logObj: LogTransportRecord) => void;
|
|
21
|
-
cleanup: () => void;
|
|
22
|
-
};
|