@kb-labs/rest-api-app 2.89.0 → 2.94.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/dist/index.js CHANGED
@@ -868,7 +868,7 @@ async function registerEventRoutes(server, basePath, registry, readiness, eventH
868
868
  const ready = isReady(readiness);
869
869
  const reason = resolveReadinessReason(readiness);
870
870
  const pluginSnapshot = metricsCollector.getLastPluginMountSnapshot();
871
- const redisStatus = registry.getRedisStatus?.();
871
+ const redisStatus = "getRedisStatus" in registry && typeof registry.getRedisStatus === "function" ? registry.getRedisStatus() : void 0;
872
872
  send({
873
873
  type: "health",
874
874
  status: health.status,
@@ -886,7 +886,11 @@ async function registerEventRoutes(server, basePath, registry, readiness, eventH
886
886
  pluginRoutesLastDurationMs: readiness.pluginRoutesLastDurationMs ?? null,
887
887
  redisEnabled: redisStatus?.enabled ?? false,
888
888
  redisHealthy: redisStatus?.healthy ?? true,
889
- redisStates: redisStatus?.roles
889
+ redisStates: redisStatus ? {
890
+ publisher: redisStatus.roles.publisher ?? null,
891
+ subscriber: redisStatus.roles.subscriber ?? null,
892
+ cache: redisStatus.roles.cache ?? null
893
+ } : void 0
890
894
  });
891
895
  }).catch((error) => {
892
896
  if (request.kbLogger) {
@@ -2261,12 +2265,15 @@ async function registerPluginSnapshotRoutes(server, config, registry) {
2261
2265
  },
2262
2266
  registryErrors: {
2263
2267
  total: registryErrors.length,
2264
- items: registryErrors.map((err) => ({
2265
- pluginPath: err.pluginPath,
2266
- pluginId: err.pluginId,
2267
- error: err.error,
2268
- code: err.code
2269
- }))
2268
+ items: registryErrors.map((err) => {
2269
+ const e = err;
2270
+ return {
2271
+ pluginPath: e.pluginPath,
2272
+ pluginId: e.pluginId,
2273
+ error: e.error,
2274
+ code: e.code
2275
+ };
2276
+ })
2270
2277
  },
2271
2278
  diagnostics: {
2272
2279
  total: discoveryDiagnostics.length,
@@ -2982,8 +2989,9 @@ async function registerObservabilityRoutes(fastify, config, repoRoot, historical
2982
2989
  fastify.log.debug("Fetching system metrics from all instances");
2983
2990
  const allMetrics = [];
2984
2991
  try {
2985
- if ("scan" in platform17.cache && typeof platform17.cache.scan === "function") {
2986
- const keys = await platform17.cache.scan("system-metrics:*");
2992
+ const cacheWithScan = platform17.cache;
2993
+ if ("scan" in platform17.cache && typeof cacheWithScan.scan === "function") {
2994
+ const keys = await cacheWithScan.scan("system-metrics:*");
2987
2995
  for (const key of keys) {
2988
2996
  const metrics = await platform17.cache.get(key);
2989
2997
  if (metrics) {
@@ -3639,14 +3647,15 @@ function getEventData(event) {
3639
3647
  if (event.schema === "kb.v1" && event.payload) {
3640
3648
  return event.payload;
3641
3649
  }
3642
- return event.properties || {};
3650
+ return event.properties ?? {};
3643
3651
  }
3644
3652
  function extractDateRange(query) {
3645
- if (!query) {
3653
+ if (!query || typeof query !== "object") {
3646
3654
  return { from: void 0, to: void 0 };
3647
3655
  }
3648
- const from = query.from;
3649
- const to = query.to;
3656
+ const q = query;
3657
+ const from = q.from;
3658
+ const to = q.to;
3650
3659
  if (from && !isValidISODate(from)) {
3651
3660
  throw new Error('Invalid "from" date format. Expected ISO 8601 datetime (e.g., 2026-01-01T00:00:00Z)');
3652
3661
  }
@@ -3656,22 +3665,27 @@ function extractDateRange(query) {
3656
3665
  return { from, to };
3657
3666
  }
3658
3667
  function extractModelFilter(query) {
3659
- if (!query || !query.models) {
3668
+ if (!query || typeof query !== "object") {
3660
3669
  return void 0;
3661
3670
  }
3662
- const modelsParam = query.models;
3671
+ const q = query;
3672
+ if (!q.models) {
3673
+ return void 0;
3674
+ }
3675
+ const modelsParam = q.models;
3663
3676
  const models = modelsParam.split(",").map((m) => m.trim()).filter((m) => m.length > 0);
3664
3677
  return models.length > 0 ? models : void 0;
3665
3678
  }
3666
3679
  function extractStatsOptions(query) {
3667
- if (!query) {
3680
+ if (!query || typeof query !== "object") {
3668
3681
  return {};
3669
3682
  }
3670
- const groupBy = query.groupBy;
3683
+ const q = query;
3684
+ const groupBy = q.groupBy;
3671
3685
  const validGroupBy = ["hour", "day", "week", "month"];
3672
3686
  const resolvedGroupBy = groupBy && validGroupBy.includes(groupBy) ? groupBy : void 0;
3673
- const breakdownBy = query.breakdownBy;
3674
- const metricsParam = query.metrics;
3687
+ const breakdownBy = q.breakdownBy;
3688
+ const metricsParam = q.metrics;
3675
3689
  const metrics = metricsParam ? metricsParam.split(",").map((m) => m.trim()).filter((m) => m.length > 0) : void 0;
3676
3690
  return {
3677
3691
  groupBy: resolvedGroupBy,
@@ -4618,8 +4632,9 @@ Log Entries (${Math.min(relevantLogs.length, 100)} most recent):
4618
4632
  `;
4619
4633
  }
4620
4634
  }
4621
- if (includeContext.stackTraces && log.err?.stack) {
4622
- prompt += ` stack: ${log.err.stack.split("\n").slice(0, 5).join("\n ")}
4635
+ const logErr = log.err;
4636
+ if (includeContext.stackTraces && logErr?.stack) {
4637
+ prompt += ` stack: ${logErr.stack.split("\n").slice(0, 5).join("\n ")}
4623
4638
  `;
4624
4639
  }
4625
4640
  });
@@ -4778,7 +4793,7 @@ async function registerLogRoutes(server, config, eventHub) {
4778
4793
  return reply.code(503).send({
4779
4794
  ok: false,
4780
4795
  error: "Log query failed",
4781
- message: error?.message ?? "Unknown error"
4796
+ message: error instanceof Error ? error.message : "Unknown error"
4782
4797
  });
4783
4798
  }
4784
4799
  });
@@ -4811,7 +4826,7 @@ async function registerLogRoutes(server, config, eventHub) {
4811
4826
  return reply.code(500).send({
4812
4827
  ok: false,
4813
4828
  error: "Failed to fetch log",
4814
- message: error?.message ?? "Unknown error"
4829
+ message: error instanceof Error ? error.message : "Unknown error"
4815
4830
  });
4816
4831
  }
4817
4832
  }
@@ -4842,7 +4857,7 @@ async function registerLogRoutes(server, config, eventHub) {
4842
4857
  return reply.code(500).send({
4843
4858
  ok: false,
4844
4859
  error: "Failed to fetch related logs",
4845
- message: error?.message ?? "Unknown error"
4860
+ message: error instanceof Error ? error.message : "Unknown error"
4846
4861
  });
4847
4862
  }
4848
4863
  }
@@ -4931,7 +4946,7 @@ async function registerLogRoutes(server, config, eventHub) {
4931
4946
  return reply.code(500).send({
4932
4947
  ok: false,
4933
4948
  error: "Failed to fetch stats",
4934
- message: error?.message ?? "Unknown error"
4949
+ message: error instanceof Error ? error.message : "Unknown error"
4935
4950
  });
4936
4951
  }
4937
4952
  });
@@ -5039,7 +5054,7 @@ async function registerLogRoutes(server, config, eventHub) {
5039
5054
  return reply.code(500).send({
5040
5055
  ok: false,
5041
5056
  error: "Log summarization failed",
5042
- message: error?.message ?? "Unknown error"
5057
+ message: error instanceof Error ? error.message : "Unknown error"
5043
5058
  });
5044
5059
  }
5045
5060
  });
@@ -5081,10 +5096,11 @@ async function loadPlatformConfig(repoRoot) {
5081
5096
  };
5082
5097
  }
5083
5098
  const config = configResult.data;
5099
+ const configPlatform = typeof config.platform === "object" && config.platform !== null ? config.platform : {};
5084
5100
  return {
5085
- adapters: config.platform?.adapters ?? {},
5086
- adapterOptions: config.platform?.adapterOptions ?? {},
5087
- execution: config.platform?.execution ?? { mode: "in-process" }
5101
+ adapters: configPlatform.adapters ?? {},
5102
+ adapterOptions: configPlatform.adapterOptions ?? {},
5103
+ execution: configPlatform.execution ?? { mode: "in-process" }
5088
5104
  };
5089
5105
  } catch (error) {
5090
5106
  return {
@@ -5657,11 +5673,11 @@ function normalizeBasePath2(basePath) {
5657
5673
  async function registerRoutes(server, config, repoRoot, registry) {
5658
5674
  registerRouteCollector(server);
5659
5675
  const initialSnapshot = registry.snapshot();
5660
- const initialRedisStatus = registry.getRedisStatus?.();
5661
- const initialRedisStates = initialRedisStatus?.roles ?? {
5662
- publisher: null,
5663
- subscriber: null,
5664
- cache: null
5676
+ const initialRedisStatus = "getRedisStatus" in registry && typeof registry.getRedisStatus === "function" ? registry.getRedisStatus() : void 0;
5677
+ const initialRedisStates = {
5678
+ publisher: initialRedisStatus?.roles.publisher ?? null,
5679
+ subscriber: initialRedisStatus?.roles.subscriber ?? null,
5680
+ cache: initialRedisStatus?.roles.cache ?? null
5665
5681
  };
5666
5682
  const registryLoaded = initialSnapshot.plugins.length > 0 && !initialSnapshot.partial && !initialSnapshot.stale;
5667
5683
  const readiness = {
@@ -5706,7 +5722,7 @@ async function registerRoutes(server, config, repoRoot, registry) {
5706
5722
  server.kbEventHub = eventHub;
5707
5723
  const broadcastState = async () => {
5708
5724
  const pluginsSnapshot = metricsCollector.getLastPluginMountSnapshot();
5709
- const redisStatus = registry.getRedisStatus?.();
5725
+ const redisStatus = "getRedisStatus" in registry && typeof registry.getRedisStatus === "function" ? registry.getRedisStatus() : void 0;
5710
5726
  if (redisStatus) {
5711
5727
  handleRedisUpdate(redisStatus);
5712
5728
  readiness.redisEnabled = redisStatus.enabled;
@@ -6088,15 +6104,17 @@ function registerErrorGuard(server) {
6088
6104
  return;
6089
6105
  }
6090
6106
  const requestId = request.id || "unknown";
6107
+ const err = error instanceof Error ? error : new Error(String(error));
6108
+ const errWithStatus = error;
6091
6109
  const envelope = {
6092
6110
  status: "error",
6093
- http: error.statusCode || 500,
6111
+ http: errWithStatus.statusCode || 500,
6094
6112
  code: ErrorCode.INTERNAL,
6095
- message: error.message || "Internal server error",
6113
+ message: err.message || "Internal server error",
6096
6114
  details: {
6097
- error: error.message || String(error)
6115
+ error: err.message || String(error)
6098
6116
  },
6099
- trace: error.stack,
6117
+ trace: err.stack,
6100
6118
  meta: {
6101
6119
  requestId,
6102
6120
  pluginId: "system",
@@ -6320,17 +6338,19 @@ async function createServer(config, repoRoot, registry) {
6320
6338
  const customLogger = {
6321
6339
  [kDisableRequestLogging]: true,
6322
6340
  // Disable Fastify's built-in request logging
6323
- debug: (obj, msg) => {
6341
+ debug: (_obj, _msg) => {
6342
+ },
6343
+ info: (_obj, _msg) => {
6324
6344
  },
6325
- info: (obj, msg) => {
6345
+ warn: (_obj, _msg) => {
6326
6346
  },
6327
- warn: (obj, msg) => {
6347
+ error: (_obj, _msg) => {
6328
6348
  },
6329
- error: (obj, msg) => {
6349
+ fatal: (_obj, _msg) => {
6330
6350
  },
6331
- fatal: (obj, msg) => {
6351
+ trace: (_obj, _msg) => {
6332
6352
  },
6333
- trace: (obj, msg) => {
6353
+ silent: (_obj, _msg) => {
6334
6354
  },
6335
6355
  child: () => customLogger,
6336
6356
  level: "silent"
@@ -6478,7 +6498,7 @@ async function bootstrap(cwd = process.cwd()) {
6478
6498
  await metricsCollector2.start(1e4, 6e4);
6479
6499
  const address = await server.listen({
6480
6500
  port: config.port,
6481
- host: "0.0.0.0"
6501
+ host: process.env.REST_API_HOST ?? "0.0.0.0"
6482
6502
  });
6483
6503
  bootstrapLogger.info("REST API server listening", { address });
6484
6504
  const shutdown = async (signal) => {