@open-wa/api 5.1.0 → 5.3.0
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/dashboard-neo/dist/assets/{AgentScalarDrawer.vue-Bq0juj-E.js → AgentScalarDrawer.vue-Cly13x9l.js} +3 -3
- package/dashboard-neo/dist/assets/index-CP58_PDI.css +1 -0
- package/dashboard-neo/dist/assets/index-Dl0sJuu6.js +2162 -0
- package/dashboard-neo/dist/assets/{modal-74dUeivw.js → modal-BTlJ4pQT.js} +1 -1
- package/dashboard-neo/dist/index.html +2 -2
- package/dist/index.cjs +165 -6
- package/dist/index.d.cts +1 -0
- package/dist/index.d.cts.map +1 -1
- package/package.json +4 -4
- package/src/createApiServer.ts +7 -1
- package/src/diagnostics/DiagnosticStore.ts +99 -0
- package/src/docs/openapi.ts +57 -2
- package/src/routes/debug.ts +17 -2
- package/src/version.ts +4 -0
- package/dashboard-neo/dist/assets/index-DM5k7cbF.css +0 -1
- package/dashboard-neo/dist/assets/index-DO9OELB4.js +0 -2149
package/dist/index.cjs
CHANGED
|
@@ -194,6 +194,10 @@ function createApiMiddleware(clientSource, options) {
|
|
|
194
194
|
return app;
|
|
195
195
|
}
|
|
196
196
|
//#endregion
|
|
197
|
+
//#region src/version.ts
|
|
198
|
+
/** Version of the API package running the Easy API server. */
|
|
199
|
+
const API_VERSION = "5.3.0";
|
|
200
|
+
//#endregion
|
|
197
201
|
//#region src/docs/openapi.ts
|
|
198
202
|
function schemaToOpenApi(schema) {
|
|
199
203
|
if (typeof schema?.toJSONSchema === "function") return schema.toJSONSchema({ target: "openapi-3.0" });
|
|
@@ -226,14 +230,76 @@ function createOpenApiDocument(methodDefinitions, options) {
|
|
|
226
230
|
"x-openwa-aliases": def.routeSignatures.filter((signature) => signature !== `${def.httpMethod} ${def.path}`),
|
|
227
231
|
responses: { "200": {
|
|
228
232
|
description: "Successful response",
|
|
229
|
-
content: { "application/json": { schema:
|
|
230
|
-
} }
|
|
233
|
+
content: { "application/json": { schema: (0, _open_wa_schema.successResponseSchema)(def.outputSchema) } }
|
|
234
|
+
} },
|
|
235
|
+
"400": {
|
|
236
|
+
description: "Validation error",
|
|
237
|
+
content: { "application/json": {
|
|
238
|
+
schema: {
|
|
239
|
+
type: "object",
|
|
240
|
+
required: ["error", "details"],
|
|
241
|
+
properties: {
|
|
242
|
+
error: {
|
|
243
|
+
type: "string",
|
|
244
|
+
enum: ["Validation Error"]
|
|
245
|
+
},
|
|
246
|
+
details: {
|
|
247
|
+
type: "array",
|
|
248
|
+
items: {
|
|
249
|
+
type: "object",
|
|
250
|
+
required: [
|
|
251
|
+
"code",
|
|
252
|
+
"path",
|
|
253
|
+
"message"
|
|
254
|
+
],
|
|
255
|
+
properties: {
|
|
256
|
+
code: { type: "string" },
|
|
257
|
+
path: {
|
|
258
|
+
type: "array",
|
|
259
|
+
items: { oneOf: [{ type: "string" }, { type: "integer" }] }
|
|
260
|
+
},
|
|
261
|
+
message: { type: "string" }
|
|
262
|
+
},
|
|
263
|
+
additionalProperties: true
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
additionalProperties: false
|
|
268
|
+
},
|
|
269
|
+
examples: { validationError: {
|
|
270
|
+
summary: "Input validation failed",
|
|
271
|
+
value: {
|
|
272
|
+
error: "Validation Error",
|
|
273
|
+
details: [{
|
|
274
|
+
code: "invalid_type",
|
|
275
|
+
path: ["to"],
|
|
276
|
+
message: "Invalid input"
|
|
277
|
+
}]
|
|
278
|
+
}
|
|
279
|
+
} }
|
|
280
|
+
} }
|
|
281
|
+
},
|
|
282
|
+
"500": {
|
|
283
|
+
description: "Internal server error",
|
|
284
|
+
content: { "application/json": {
|
|
285
|
+
schema: {
|
|
286
|
+
type: "object",
|
|
287
|
+
required: ["error"],
|
|
288
|
+
properties: { error: { type: "string" } },
|
|
289
|
+
additionalProperties: false
|
|
290
|
+
},
|
|
291
|
+
examples: { internalError: {
|
|
292
|
+
summary: "Execution failed",
|
|
293
|
+
value: { error: "Internal Server Error" }
|
|
294
|
+
} }
|
|
295
|
+
} }
|
|
296
|
+
}
|
|
231
297
|
} }]));
|
|
232
298
|
return {
|
|
233
299
|
openapi: "3.0.3",
|
|
234
300
|
info: {
|
|
235
301
|
title: options.title || "open-wa Easy API",
|
|
236
|
-
version: options.version ||
|
|
302
|
+
version: options.version || API_VERSION
|
|
237
303
|
},
|
|
238
304
|
servers: [{ url: options.origin }],
|
|
239
305
|
paths
|
|
@@ -723,6 +789,14 @@ function registerMetaRoutes(app, options) {
|
|
|
723
789
|
//#endregion
|
|
724
790
|
//#region src/routes/debug.ts
|
|
725
791
|
function registerDebugRoutes(app, options) {
|
|
792
|
+
app.get("/meta/debug/diagnostics", (c) => {
|
|
793
|
+
const apiKey = options.config.apiKey;
|
|
794
|
+
if (!apiKey || c.req.header("X-API-Key") !== apiKey) return c.json({
|
|
795
|
+
error: "Unauthorized",
|
|
796
|
+
details: "Invalid or missing API key"
|
|
797
|
+
}, 401);
|
|
798
|
+
return c.json(options.getDiagnostics());
|
|
799
|
+
});
|
|
726
800
|
app.get("/meta/debug/memory", (c) => {
|
|
727
801
|
const mem = process.memoryUsage();
|
|
728
802
|
return c.json({
|
|
@@ -740,7 +814,9 @@ function registerDebugRoutes(app, options) {
|
|
|
740
814
|
"apiKey",
|
|
741
815
|
"licenseKey",
|
|
742
816
|
"elasticPassword",
|
|
743
|
-
"elasticUsername"
|
|
817
|
+
"elasticUsername",
|
|
818
|
+
"sessionData",
|
|
819
|
+
"sessionDataBucketAuth"
|
|
744
820
|
]) if (redacted[key]) redacted[key] = "***REDACTED***";
|
|
745
821
|
if (redacted.proxyServerCredentials) redacted.proxyServerCredentials = {
|
|
746
822
|
...redacted.proxyServerCredentials,
|
|
@@ -748,7 +824,10 @@ function registerDebugRoutes(app, options) {
|
|
|
748
824
|
};
|
|
749
825
|
if (redacted.s3Sync) redacted.s3Sync = {
|
|
750
826
|
...redacted.s3Sync,
|
|
751
|
-
|
|
827
|
+
accessKeyId: "***REDACTED***",
|
|
828
|
+
secretAccessKey: "***REDACTED***",
|
|
829
|
+
sessionToken: "***REDACTED***",
|
|
830
|
+
headers: "***REDACTED***"
|
|
752
831
|
};
|
|
753
832
|
return c.json(redacted);
|
|
754
833
|
});
|
|
@@ -1099,6 +1178,82 @@ var HealthStore = class {
|
|
|
1099
1178
|
}
|
|
1100
1179
|
};
|
|
1101
1180
|
//#endregion
|
|
1181
|
+
//#region src/diagnostics/DiagnosticStore.ts
|
|
1182
|
+
const MAX_RECORDS = 100;
|
|
1183
|
+
const MAX_TEXT = 8e3;
|
|
1184
|
+
function text(value, limit = 500) {
|
|
1185
|
+
if (typeof value !== "string" || !value.trim()) return null;
|
|
1186
|
+
const clean = value.trim();
|
|
1187
|
+
if (clean.length <= limit) return clean;
|
|
1188
|
+
const notice = `\n[truncated at ${limit} characters]`;
|
|
1189
|
+
return `${clean.slice(0, limit - notice.length)}${notice}`;
|
|
1190
|
+
}
|
|
1191
|
+
function unwrap(payload) {
|
|
1192
|
+
if (!payload || typeof payload !== "object") return {};
|
|
1193
|
+
const record = payload;
|
|
1194
|
+
const nested = record.details ?? record.ctx ?? record.payload;
|
|
1195
|
+
return nested && typeof nested === "object" ? nested : record;
|
|
1196
|
+
}
|
|
1197
|
+
function isErrorEvent(event, payload) {
|
|
1198
|
+
const outer = payload && typeof payload === "object" ? payload : {};
|
|
1199
|
+
const nested = unwrap(payload);
|
|
1200
|
+
const records = outer === nested ? [outer] : [outer, nested];
|
|
1201
|
+
const levels = records.map((record) => String(record.level ?? record.severity ?? record.type ?? record.kind ?? "").toLowerCase());
|
|
1202
|
+
if (records.some((record) => record.warning === true) || levels.some((level) => [
|
|
1203
|
+
"warn",
|
|
1204
|
+
"warning",
|
|
1205
|
+
"info",
|
|
1206
|
+
"debug",
|
|
1207
|
+
"trace",
|
|
1208
|
+
"verbose"
|
|
1209
|
+
].includes(level))) return false;
|
|
1210
|
+
if (event === "error") return true;
|
|
1211
|
+
const nestedError = nested.error && typeof nested.error === "object" ? nested.error : null;
|
|
1212
|
+
return event === "debug:log" && (levels.some((level) => level === "error" || level === "fatal") || payload instanceof Error || records.some((record) => record.error instanceof Error) || Boolean(nestedError && (typeof nestedError.message === "string" || typeof nestedError.stack === "string")) || records.some((record) => Boolean(record.stack) && record.fatal !== false));
|
|
1213
|
+
}
|
|
1214
|
+
/** Holds structured runtime errors observed on this API server's event bridge. */
|
|
1215
|
+
var DiagnosticStore = class {
|
|
1216
|
+
constructor() {
|
|
1217
|
+
this.nextId = 0;
|
|
1218
|
+
this.records = [];
|
|
1219
|
+
this.attached = false;
|
|
1220
|
+
}
|
|
1221
|
+
attach() {
|
|
1222
|
+
this.attached = true;
|
|
1223
|
+
}
|
|
1224
|
+
capture(event, payload) {
|
|
1225
|
+
if (!isErrorEvent(event, payload)) return;
|
|
1226
|
+
const outer = payload && typeof payload === "object" ? payload : {};
|
|
1227
|
+
const record = unwrap(payload);
|
|
1228
|
+
const error = record.error instanceof Error ? record.error : outer.error instanceof Error ? outer.error : null;
|
|
1229
|
+
const errorRecord = record.error && typeof record.error === "object" ? record.error : {};
|
|
1230
|
+
const stackValue = error?.stack ?? errorRecord.stack ?? record.stack ?? record.trace;
|
|
1231
|
+
const message = text(error?.message ?? errorRecord.message ?? record.message ?? record.msg ?? (typeof record.error === "string" ? record.error : void 0) ?? (event === "error" && typeof payload === "string" ? payload : void 0), 1500) ?? text(typeof stackValue === "string" ? stackValue.split(/\r?\n/, 1)[0] : null, 1500);
|
|
1232
|
+
if (!message) return;
|
|
1233
|
+
const stack = text(stackValue, MAX_TEXT);
|
|
1234
|
+
this.records.push({
|
|
1235
|
+
id: ++this.nextId,
|
|
1236
|
+
event,
|
|
1237
|
+
timestamp: Date.now(),
|
|
1238
|
+
message,
|
|
1239
|
+
stack,
|
|
1240
|
+
scope: text(record.scope ?? record.name, 240),
|
|
1241
|
+
method: text(record.method ?? record.operation, 240),
|
|
1242
|
+
route: text(record.route ?? record.path ?? record.endpoint, 240),
|
|
1243
|
+
component: text(record.component ?? record.source ?? record.module, 240)
|
|
1244
|
+
});
|
|
1245
|
+
if (this.records.length > MAX_RECORDS) this.records.splice(0, this.records.length - MAX_RECORDS);
|
|
1246
|
+
}
|
|
1247
|
+
getSnapshot() {
|
|
1248
|
+
return {
|
|
1249
|
+
available: this.attached,
|
|
1250
|
+
captureSource: "session event bridge",
|
|
1251
|
+
captureLimit: MAX_RECORDS,
|
|
1252
|
+
records: this.records.slice()
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
//#endregion
|
|
1102
1257
|
//#region src/events/EventBroadcaster.ts
|
|
1103
1258
|
const DEFAULT_KEEPALIVE_MS = 2e4;
|
|
1104
1259
|
function normalizeTopics(topics) {
|
|
@@ -1295,6 +1450,7 @@ var ApiServer = class {
|
|
|
1295
1450
|
this.latestQR = null;
|
|
1296
1451
|
this.isDashboardActive = false;
|
|
1297
1452
|
this.healthStore = new HealthStore();
|
|
1453
|
+
this.diagnosticStore = new DiagnosticStore();
|
|
1298
1454
|
this.config = options.config;
|
|
1299
1455
|
if (this.config.mcp?.enabled && !this.config.apiKey) throw new Error("MCP requires Easy API apiKey. Refusing to start with MCP enabled and no apiKey configured.");
|
|
1300
1456
|
this.app = new hono.Hono();
|
|
@@ -1320,9 +1476,11 @@ var ApiServer = class {
|
|
|
1320
1476
|
this.eventBridge = bridge;
|
|
1321
1477
|
this.eventBridgeListener = (event, payload) => {
|
|
1322
1478
|
this.healthStore.processEvent(event, payload);
|
|
1479
|
+
this.diagnosticStore.capture(event, payload);
|
|
1323
1480
|
this.eventBroadcaster.broadcast(event, payload);
|
|
1324
1481
|
};
|
|
1325
1482
|
bridge.onAny(this.eventBridgeListener);
|
|
1483
|
+
this.diagnosticStore.attach();
|
|
1326
1484
|
}
|
|
1327
1485
|
setReadinessProvider(provider) {
|
|
1328
1486
|
this.readinessProvider = provider;
|
|
@@ -1442,7 +1600,7 @@ var ApiServer = class {
|
|
|
1442
1600
|
const healthSnapshot = this.healthStore.getSnapshot();
|
|
1443
1601
|
return c.json({
|
|
1444
1602
|
status: "ok",
|
|
1445
|
-
version:
|
|
1603
|
+
version: API_VERSION,
|
|
1446
1604
|
host: {
|
|
1447
1605
|
available: true,
|
|
1448
1606
|
api: true,
|
|
@@ -1519,6 +1677,7 @@ var ApiServer = class {
|
|
|
1519
1677
|
registerDebugRoutes(this.app, {
|
|
1520
1678
|
config: this.config,
|
|
1521
1679
|
getConfig: () => this.config,
|
|
1680
|
+
getDiagnostics: () => this.diagnosticStore.getSnapshot(),
|
|
1522
1681
|
setIntegration: (id, data) => {
|
|
1523
1682
|
if (!this.config.integrations) this.config.integrations = {};
|
|
1524
1683
|
this.config.integrations[id] = data;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/events/EventBridge.ts","../src/events/EventBroadcaster.ts","../src/monitoring/elastic.ts","../src/types.ts","../src/createApiServer.ts","../src/createApiMiddleware.ts","../src/auth/api-key.ts","../src/middleware/rate-limit.ts"],"mappings":";;;;;;;UAAiB;EACf,MAAM,WAAW,eAAe;EAChC,OAAO,WAAW,eAAe;;;;UCFlB;EACf;EACA;EACA;EACA;EACA;;UAQe;EACf;EACA,SAAS;EACT;;qBAiBW;mBACM;mBACA;UACT;UACA;UACA;EAER,YAAY;EAIL,UAAU,eAAe,mBAAmB;EAsB5C,aAAa,UAAS,qBAA0B,eAAe;EAwD/D;;;;UCxHQ;EACf;EACA;EACA;EACA;EACA;EACA;;UAGe;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;GACC;;qBAGU;UACH;UACA;UACA;UACA;EAER,YAAY,QAAQ;EAsBP,SAAS;EAOf,IAAI,UAAU,KAAK;EAYb,SAAS;EAmBT,QAAQ;;;;KC1FX,kBAAkB,mBAAmB,gBAAgB;KACrD,eAAe,qCAAqC;UAE/C;EACf,QAAQ;EACR;EACA;EACA,oBAAoB;EACpB,iBAAiB;EACjB;;UASe;EACf,QAAQ;;;;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/events/EventBridge.ts","../src/events/EventBroadcaster.ts","../src/monitoring/elastic.ts","../src/types.ts","../src/createApiServer.ts","../src/createApiMiddleware.ts","../src/auth/api-key.ts","../src/middleware/rate-limit.ts"],"mappings":";;;;;;;UAAiB;EACf,MAAM,WAAW,eAAe;EAChC,OAAO,WAAW,eAAe;;;;UCFlB;EACf;EACA;EACA;EACA;EACA;;UAQe;EACf;EACA,SAAS;EACT;;qBAiBW;mBACM;mBACA;UACT;UACA;UACA;EAER,YAAY;EAIL,UAAU,eAAe,mBAAmB;EAsB5C,aAAa,UAAS,qBAA0B,eAAe;EAwD/D;;;;UCxHQ;EACf;EACA;EACA;EACA;EACA;EACA;;UAGe;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;GACC;;qBAGU;UACH;UACA;UACA;UACA;EAER,YAAY,QAAQ;EAsBP,SAAS;EAOf,IAAI,UAAU,KAAK;EAYb,SAAS;EAmBT,QAAQ;;;;KC1FX,kBAAkB,mBAAmB,gBAAgB;KACrD,eAAe,qCAAqC;UAE/C;EACf,QAAQ;EACR;EACA;EACA,oBAAoB;EACpB,iBAAiB;EACjB;;UASe;EACf,QAAQ;;;;qBCuBG;UACH;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;EAWR,YAAY,SAAS;EAmCd,UAAU,QAAQ;EAIlB,eAAe,QAAQ;EAiBvB,qBACL;IAGU;IACA;IACA;IACA;IACA;IACA;;EAYC,QAAQ,MAAM,kBAAkB;EAOhC,aAAa;EAInB,MAAM;EAIN,UAAM,0BAAA,+BAAA;EAIN,uBAAmB;EAQnB,aAAa,YAAY;EASnB,SAAK;EAiDL,QAAI;EAcV;UAIC;UAeA;UAwCA;;wBAiKM,gBAAgB,QAAQ,SAAM;;;wBCpZ9B,oBAAoB,cAAc,cAAc,SAAS,uBAAoB,0BAAA,+BAAA;;;wBC9D7E,iBAAiB,kBAAc,GAC5B,SAAO,MAAQ,SAAI,gBAAA,0BAAA;;;;;;wBCUtB,oBAAoB,sBAAmB,qBAAgB,GACpD,SAAO,MAAQ,SAAI,gBAAA,0BAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-wa/api",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "Reusable Easy API transport and runtime explorer layer for open-wa",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"types": "./dist/index.d.cts",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@elastic/elasticsearch": "^9.5.1",
|
|
21
21
|
"@hono/node-server": "^2.1.1",
|
|
22
|
-
"@open-wa/mcp": "5.
|
|
23
|
-
"@open-wa/schema": "5.
|
|
24
|
-
"@open-wa/screencaster": "5.
|
|
22
|
+
"@open-wa/mcp": "5.3.0",
|
|
23
|
+
"@open-wa/schema": "5.3.0",
|
|
24
|
+
"@open-wa/screencaster": "5.3.0",
|
|
25
25
|
"hono": "^4.13.9",
|
|
26
26
|
"tree-kill": "^1.2.2",
|
|
27
27
|
"ws": "^8.22.0"
|
package/src/createApiServer.ts
CHANGED
|
@@ -17,6 +17,8 @@ import { registerDebugRoutes } from './routes/debug';
|
|
|
17
17
|
import { registerAgentDiscoveryRoutes } from './routes/agent-discovery';
|
|
18
18
|
import { type EventBridge } from './events/EventBridge';
|
|
19
19
|
import { HealthStore } from './health/HealthStore';
|
|
20
|
+
import { DiagnosticStore } from './diagnostics/DiagnosticStore';
|
|
21
|
+
import { API_VERSION } from './version';
|
|
20
22
|
import { EventBroadcaster } from './events/EventBroadcaster';
|
|
21
23
|
import { ElasticEmitter } from './monitoring/elastic';
|
|
22
24
|
import {
|
|
@@ -54,6 +56,7 @@ export class ApiServer {
|
|
|
54
56
|
private isDashboardActive: boolean = false;
|
|
55
57
|
private pluginHost?: PluginHost;
|
|
56
58
|
private healthStore: HealthStore = new HealthStore();
|
|
59
|
+
private diagnosticStore: DiagnosticStore = new DiagnosticStore();
|
|
57
60
|
private eventBroadcaster: EventBroadcaster;
|
|
58
61
|
private eventBridge?: EventBridge;
|
|
59
62
|
private eventBridgeListener?: (event: string, payload: any) => void;
|
|
@@ -115,11 +118,13 @@ export class ApiServer {
|
|
|
115
118
|
this.eventBridge = bridge;
|
|
116
119
|
this.eventBridgeListener = (event: string, payload: any) => {
|
|
117
120
|
this.healthStore.processEvent(event, payload);
|
|
121
|
+
this.diagnosticStore.capture(event, payload);
|
|
118
122
|
this.eventBroadcaster.broadcast(event, payload);
|
|
119
123
|
};
|
|
120
124
|
|
|
121
125
|
// Also wire events into the HealthStore so /health returns accumulated data
|
|
122
126
|
bridge.onAny(this.eventBridgeListener);
|
|
127
|
+
this.diagnosticStore.attach();
|
|
123
128
|
}
|
|
124
129
|
|
|
125
130
|
public setReadinessProvider(
|
|
@@ -316,7 +321,7 @@ export class ApiServer {
|
|
|
316
321
|
const healthSnapshot = this.healthStore.getSnapshot();
|
|
317
322
|
return c.json({
|
|
318
323
|
status: 'ok',
|
|
319
|
-
version:
|
|
324
|
+
version: API_VERSION,
|
|
320
325
|
host: {
|
|
321
326
|
available: true,
|
|
322
327
|
api: true,
|
|
@@ -431,6 +436,7 @@ export class ApiServer {
|
|
|
431
436
|
registerDebugRoutes(this.app, {
|
|
432
437
|
config: this.config,
|
|
433
438
|
getConfig: () => this.config,
|
|
439
|
+
getDiagnostics: () => this.diagnosticStore.getSnapshot(),
|
|
434
440
|
setIntegration: (id, data) => {
|
|
435
441
|
if (!this.config.integrations) {
|
|
436
442
|
this.config.integrations = {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export type ServerDiagnosticRecord = {
|
|
2
|
+
id: number;
|
|
3
|
+
event: string;
|
|
4
|
+
timestamp: number;
|
|
5
|
+
message: string;
|
|
6
|
+
stack: string | null;
|
|
7
|
+
scope: string | null;
|
|
8
|
+
method: string | null;
|
|
9
|
+
route: string | null;
|
|
10
|
+
component: string | null;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const MAX_RECORDS = 100;
|
|
14
|
+
const MAX_TEXT = 8_000;
|
|
15
|
+
|
|
16
|
+
function text(value: unknown, limit = 500): string | null {
|
|
17
|
+
if (typeof value !== 'string' || !value.trim()) return null;
|
|
18
|
+
const clean = value.trim();
|
|
19
|
+
if (clean.length <= limit) return clean;
|
|
20
|
+
const notice = `\n[truncated at ${limit} characters]`;
|
|
21
|
+
return `${clean.slice(0, limit - notice.length)}${notice}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function unwrap(payload: unknown): Record<string, unknown> {
|
|
25
|
+
if (!payload || typeof payload !== 'object') return {};
|
|
26
|
+
const record = payload as Record<string, unknown>;
|
|
27
|
+
const nested = record.details ?? record.ctx ?? record.payload;
|
|
28
|
+
return nested && typeof nested === 'object' ? nested as Record<string, unknown> : record;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isErrorEvent(event: string, payload: unknown) {
|
|
32
|
+
const outer = payload && typeof payload === 'object' ? payload as Record<string, unknown> : {};
|
|
33
|
+
const nested = unwrap(payload);
|
|
34
|
+
const records = outer === nested ? [outer] : [outer, nested];
|
|
35
|
+
const levels = records.map((record) => String(record.level ?? record.severity ?? record.type ?? record.kind ?? '').toLowerCase());
|
|
36
|
+
if (records.some((record) => record.warning === true) || levels.some((level) => ['warn', 'warning', 'info', 'debug', 'trace', 'verbose'].includes(level))) return false;
|
|
37
|
+
if (event === 'error') return true;
|
|
38
|
+
const nestedError = nested.error && typeof nested.error === 'object'
|
|
39
|
+
? nested.error as Record<string, unknown>
|
|
40
|
+
: null;
|
|
41
|
+
return event === 'debug:log' && (
|
|
42
|
+
levels.some((level) => level === 'error' || level === 'fatal')
|
|
43
|
+
|| payload instanceof Error
|
|
44
|
+
|| records.some((record) => record.error instanceof Error)
|
|
45
|
+
|| Boolean(nestedError && (typeof nestedError.message === 'string' || typeof nestedError.stack === 'string'))
|
|
46
|
+
|| records.some((record) => Boolean(record.stack) && record.fatal !== false)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Holds structured runtime errors observed on this API server's event bridge. */
|
|
51
|
+
export class DiagnosticStore {
|
|
52
|
+
private nextId = 0;
|
|
53
|
+
private records: ServerDiagnosticRecord[] = [];
|
|
54
|
+
private attached = false;
|
|
55
|
+
|
|
56
|
+
attach() {
|
|
57
|
+
this.attached = true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
capture(event: string, payload: unknown) {
|
|
61
|
+
if (!isErrorEvent(event, payload)) return;
|
|
62
|
+
const outer = payload && typeof payload === 'object' ? payload as Record<string, unknown> : {};
|
|
63
|
+
const record = unwrap(payload);
|
|
64
|
+
const error = record.error instanceof Error
|
|
65
|
+
? record.error
|
|
66
|
+
: outer.error instanceof Error
|
|
67
|
+
? outer.error
|
|
68
|
+
: null;
|
|
69
|
+
const errorRecord = record.error && typeof record.error === 'object'
|
|
70
|
+
? record.error as Record<string, unknown>
|
|
71
|
+
: {};
|
|
72
|
+
const stackValue = error?.stack ?? errorRecord.stack ?? record.stack ?? record.trace;
|
|
73
|
+
const message = text(error?.message ?? errorRecord.message ?? record.message ?? record.msg ?? (typeof record.error === 'string' ? record.error : undefined) ?? (event === 'error' && typeof payload === 'string' ? payload : undefined), 1_500)
|
|
74
|
+
?? text(typeof stackValue === 'string' ? stackValue.split(/\r?\n/, 1)[0] : null, 1_500);
|
|
75
|
+
if (!message) return;
|
|
76
|
+
const stack = text(stackValue, MAX_TEXT);
|
|
77
|
+
this.records.push({
|
|
78
|
+
id: ++this.nextId,
|
|
79
|
+
event,
|
|
80
|
+
timestamp: Date.now(),
|
|
81
|
+
message,
|
|
82
|
+
stack,
|
|
83
|
+
scope: text(record.scope ?? record.name, 240),
|
|
84
|
+
method: text(record.method ?? record.operation, 240),
|
|
85
|
+
route: text(record.route ?? record.path ?? record.endpoint, 240),
|
|
86
|
+
component: text(record.component ?? record.source ?? record.module, 240),
|
|
87
|
+
});
|
|
88
|
+
if (this.records.length > MAX_RECORDS) this.records.splice(0, this.records.length - MAX_RECORDS);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
getSnapshot() {
|
|
92
|
+
return {
|
|
93
|
+
available: this.attached,
|
|
94
|
+
captureSource: 'session event bridge' as const,
|
|
95
|
+
captureLimit: MAX_RECORDS,
|
|
96
|
+
records: this.records.slice(),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
package/src/docs/openapi.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { successResponseSchema } from '@open-wa/schema';
|
|
1
2
|
import type { HttpMethodDefinition } from '@open-wa/schema';
|
|
3
|
+
import { API_VERSION } from '../version';
|
|
2
4
|
|
|
3
5
|
function schemaToOpenApi(schema: { toJSONSchema?: (params?: unknown) => unknown }) {
|
|
4
6
|
if (typeof schema?.toJSONSchema === 'function') {
|
|
@@ -55,7 +57,60 @@ export function createOpenApiDocument(
|
|
|
55
57
|
description: 'Successful response',
|
|
56
58
|
content: {
|
|
57
59
|
'application/json': {
|
|
58
|
-
schema:
|
|
60
|
+
schema: successResponseSchema(def.outputSchema),
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
'400': {
|
|
66
|
+
description: 'Validation error',
|
|
67
|
+
content: {
|
|
68
|
+
'application/json': {
|
|
69
|
+
schema: {
|
|
70
|
+
type: 'object',
|
|
71
|
+
required: ['error', 'details'],
|
|
72
|
+
properties: {
|
|
73
|
+
error: { type: 'string', enum: ['Validation Error'] },
|
|
74
|
+
details: {
|
|
75
|
+
type: 'array',
|
|
76
|
+
items: {
|
|
77
|
+
type: 'object',
|
|
78
|
+
required: ['code', 'path', 'message'],
|
|
79
|
+
properties: {
|
|
80
|
+
code: { type: 'string' },
|
|
81
|
+
path: { type: 'array', items: { oneOf: [{ type: 'string' }, { type: 'integer' }] } },
|
|
82
|
+
message: { type: 'string' },
|
|
83
|
+
},
|
|
84
|
+
additionalProperties: true,
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
additionalProperties: false,
|
|
89
|
+
},
|
|
90
|
+
examples: {
|
|
91
|
+
validationError: {
|
|
92
|
+
summary: 'Input validation failed',
|
|
93
|
+
value: { error: 'Validation Error', details: [{ code: 'invalid_type', path: ['to'], message: 'Invalid input' }] },
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
'500': {
|
|
100
|
+
description: 'Internal server error',
|
|
101
|
+
content: {
|
|
102
|
+
'application/json': {
|
|
103
|
+
schema: {
|
|
104
|
+
type: 'object',
|
|
105
|
+
required: ['error'],
|
|
106
|
+
properties: { error: { type: 'string' } },
|
|
107
|
+
additionalProperties: false,
|
|
108
|
+
},
|
|
109
|
+
examples: {
|
|
110
|
+
internalError: {
|
|
111
|
+
summary: 'Execution failed',
|
|
112
|
+
value: { error: 'Internal Server Error' },
|
|
113
|
+
},
|
|
59
114
|
},
|
|
60
115
|
},
|
|
61
116
|
},
|
|
@@ -69,7 +124,7 @@ export function createOpenApiDocument(
|
|
|
69
124
|
openapi: '3.0.3',
|
|
70
125
|
info: {
|
|
71
126
|
title: options.title || 'open-wa Easy API',
|
|
72
|
-
version: options.version ||
|
|
127
|
+
version: options.version || API_VERSION,
|
|
73
128
|
},
|
|
74
129
|
servers: [{ url: options.origin }],
|
|
75
130
|
paths,
|
package/src/routes/debug.ts
CHANGED
|
@@ -11,9 +11,24 @@ export function registerDebugRoutes(
|
|
|
11
11
|
options: {
|
|
12
12
|
config: Config;
|
|
13
13
|
getConfig: () => Config;
|
|
14
|
+
getDiagnostics: () => {
|
|
15
|
+
available: boolean;
|
|
16
|
+
captureSource: 'session event bridge';
|
|
17
|
+
captureLimit: number;
|
|
18
|
+
records: unknown[];
|
|
19
|
+
};
|
|
14
20
|
setIntegration?: (id: string, data: { enabled: boolean; config: Record<string, string> }) => void;
|
|
15
21
|
}
|
|
16
22
|
) {
|
|
23
|
+
// Error records can contain runtime details, so require the configured Easy API key.
|
|
24
|
+
app.get('/meta/debug/diagnostics', (c: any) => {
|
|
25
|
+
const apiKey = options.config.apiKey;
|
|
26
|
+
if (!apiKey || c.req.header('X-API-Key') !== apiKey) {
|
|
27
|
+
return c.json({ error: 'Unauthorized', details: 'Invalid or missing API key' }, 401);
|
|
28
|
+
}
|
|
29
|
+
return c.json(options.getDiagnostics());
|
|
30
|
+
});
|
|
31
|
+
|
|
17
32
|
// ─── Memory Metrics ──────────────────────────────────────────────
|
|
18
33
|
app.get('/meta/debug/memory', (c: any) => {
|
|
19
34
|
const mem = process.memoryUsage();
|
|
@@ -32,7 +47,7 @@ export function registerDebugRoutes(
|
|
|
32
47
|
const config = options.getConfig();
|
|
33
48
|
// Redact sensitive fields
|
|
34
49
|
const redacted = { ...config } as Record<string, unknown>;
|
|
35
|
-
const sensitiveKeys = ['apiKey', 'licenseKey', 'elasticPassword', 'elasticUsername'];
|
|
50
|
+
const sensitiveKeys = ['apiKey', 'licenseKey', 'elasticPassword', 'elasticUsername', 'sessionData', 'sessionDataBucketAuth'];
|
|
36
51
|
for (const key of sensitiveKeys) {
|
|
37
52
|
if (redacted[key]) {
|
|
38
53
|
redacted[key] = '***REDACTED***';
|
|
@@ -43,7 +58,7 @@ export function registerDebugRoutes(
|
|
|
43
58
|
redacted.proxyServerCredentials = { ...redacted.proxyServerCredentials as Record<string, unknown>, password: '***REDACTED***' };
|
|
44
59
|
}
|
|
45
60
|
if (redacted.s3Sync) {
|
|
46
|
-
redacted.s3Sync = { ...redacted.s3Sync as Record<string, unknown>, secretAccessKey: '***REDACTED***' };
|
|
61
|
+
redacted.s3Sync = { ...redacted.s3Sync as Record<string, unknown>, accessKeyId: '***REDACTED***', secretAccessKey: '***REDACTED***', sessionToken: '***REDACTED***', headers: '***REDACTED***' };
|
|
47
62
|
}
|
|
48
63
|
return c.json(redacted);
|
|
49
64
|
});
|
package/src/version.ts
ADDED