n2n-nexus 0.4.2 → 0.7.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +114 -0
  2. package/README.md +77 -117
  3. package/assets/n2n-nexus-banner.jpg +0 -0
  4. package/assets/n2n-nexus-icon.png +0 -0
  5. package/assets/n2n-nexus-logo.png +0 -0
  6. package/build/client/nexus-client.js +44 -21
  7. package/build/config/cli.js +0 -3
  8. package/build/config/index.js +0 -10
  9. package/build/constants.js +7 -11
  10. package/build/daemon/index.js +35 -19
  11. package/build/daemon/rate-limit.js +62 -0
  12. package/build/daemon/server.js +422 -545
  13. package/build/daemon/tool-outputs.js +177 -0
  14. package/build/daemon/tools.js +321 -0
  15. package/build/index.js +1 -1
  16. package/build/server/instance-id.js +19 -0
  17. package/build/server/nexus.js +243 -47
  18. package/build/storage/direct-messages.js +89 -0
  19. package/build/storage/docs.js +14 -7
  20. package/build/storage/events.js +31 -0
  21. package/build/storage/fs-utils.js +42 -0
  22. package/build/storage/global-messages.js +44 -0
  23. package/build/storage/index.js +47 -41
  24. package/build/storage/migrations.js +86 -0
  25. package/build/storage/paths.js +0 -3
  26. package/build/storage/projects.js +4 -5
  27. package/build/storage/registry.js +67 -25
  28. package/build/storage/sessions.js +77 -0
  29. package/build/storage/sqlite-meeting.js +35 -23
  30. package/build/storage/sqlite.js +73 -24
  31. package/build/storage/tasks.js +29 -103
  32. package/build/types.js +1 -1
  33. package/docs/ARCHITECTURE.md +36 -25
  34. package/docs/ARCHITECTURE_zh.md +37 -26
  35. package/docs/ASSISTANT_GUIDE.md +77 -65
  36. package/docs/README_zh.md +34 -19
  37. package/llms.txt +12 -2
  38. package/package.json +24 -14
  39. package/build/storage/logs.js +0 -60
  40. package/build/storage/meetings.js +0 -276
  41. package/build/storage/store.js +0 -153
@@ -1,41 +1,57 @@
1
1
  import { createDaemonServer } from "./server.js";
2
- import { pkg, getRootPath } from "../config/index.js";
3
- import { NEXUS_HOST } from "../constants.js";
2
+ import { closeDatabase } from "../storage/sqlite.js";
3
+ import { runTaskMaintenance } from "../storage/tasks.js";
4
+ import { TASK_MAINTENANCE_INTERVAL_MS } from "../constants.js";
5
+ import { pkg } from "../config/index.js";
6
+ import { getArg } from "../config/cli.js";
7
+ import { getRootPath } from "../config/paths.js";
8
+ import { NEXUS_HOST, NEXUS_PORT } from "../constants.js";
4
9
  function parsePort() {
5
- const argIndex = process.argv.indexOf("--port");
6
- if (argIndex !== -1) {
7
- const parsed = parseInt(process.argv[argIndex + 1] || "", 10);
10
+ for (const candidate of [getArg("--port"), process.env.NEXUS_DAEMON_PORT]) {
11
+ const parsed = parseInt(candidate || "", 10);
8
12
  if (!isNaN(parsed) && parsed > 0)
9
13
  return parsed;
10
14
  }
11
- const envPort = parseInt(process.env.NEXUS_DAEMON_PORT || process.env.NEXUS_PORT || "", 10);
12
- if (!isNaN(envPort) && envPort > 0)
13
- return envPort;
14
- return 5688;
15
+ return NEXUS_PORT;
15
16
  }
16
17
  function parseHost() {
17
- const argIndex = process.argv.indexOf("--host");
18
- if (argIndex !== -1 && process.argv[argIndex + 1]) {
19
- return process.argv[argIndex + 1];
20
- }
21
- return process.env.NEXUS_HOST || NEXUS_HOST;
18
+ return getArg("--host") || process.env.NEXUS_HOST || NEXUS_HOST;
22
19
  }
23
20
  export async function runDaemon() {
24
21
  const port = parsePort();
25
22
  const host = parseHost();
26
23
  const root = getRootPath();
27
- // Inject root path into CONFIG via env so StorageManager picks it up
24
+ // Publish the resolved root so every storage module (which reads NEXUS_ROOT) agrees with the log line below
28
25
  process.env.NEXUS_ROOT = root;
29
- const { server, storageInfo } = await createDaemonServer({ port, host, version: pkg.version });
26
+ const { server } = await createDaemonServer({ port, host, version: pkg.version });
30
27
  await new Promise((resolve, reject) => {
31
28
  server.once("error", reject);
32
29
  server.listen(port, host, () => {
33
30
  console.error(`[n2n-nexus] Daemon v${pkg.version} listening on http://${host}:${port}`);
34
- console.error(`[n2n-nexus] Storage: ${root} (${storageInfo.storageMode}${storageInfo.isDegraded ? ", degraded" : ""})`);
31
+ console.error(`[n2n-nexus] Storage: ${root}`);
35
32
  resolve();
36
33
  });
37
34
  });
38
- process.once("SIGINT", () => { console.error("[n2n-nexus] Shutting down."); server.close(() => process.exit(0)); });
39
- process.once("SIGTERM", () => { server.close(() => process.exit(0)); });
35
+ // Expire overdue tasks and purge old finished ones at startup and then periodically
36
+ const maintain = () => {
37
+ const { expired, removed } = runTaskMaintenance();
38
+ if (expired || removed)
39
+ console.error(`[n2n-nexus] Task maintenance: ${expired} expired, ${removed} removed.`);
40
+ };
41
+ maintain();
42
+ setInterval(maintain, TASK_MAINTENANCE_INTERVAL_MS).unref();
43
+ // Graceful shutdown: stop accepting connections, let in-flight requests
44
+ // finish, then close SQLite. A second signal exits immediately.
45
+ const shutdown = (signal) => {
46
+ console.error(`[n2n-nexus] ${signal} received, shutting down.`);
47
+ server.close(() => {
48
+ closeDatabase();
49
+ process.exit(0);
50
+ });
51
+ process.once(signal, () => process.exit(1));
52
+ };
53
+ process.once("SIGINT", () => shutdown("SIGINT"));
54
+ process.once("SIGTERM", () => shutdown("SIGTERM"));
55
+ process.on("exit", () => closeDatabase());
40
56
  console.error("[n2n-nexus] Ready. Press Ctrl+C to stop.");
41
57
  }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Token-bucket rate limiter keyed by caller. Each key may make `burst`
3
+ * calls at once and refills at `perSecond`. Only `perSecond` = 0 disables
4
+ * it; `burst` is at least 1 (a bucket smaller than one call would reject
5
+ * every call forever).
6
+ */
7
+ export class RateLimiter {
8
+ perSecond;
9
+ now;
10
+ buckets = new Map();
11
+ lastSweep = 0;
12
+ burst;
13
+ constructor(perSecond, burst, now = Date.now) {
14
+ this.perSecond = perSecond;
15
+ this.now = now;
16
+ this.burst = Math.max(1, burst);
17
+ }
18
+ get enabled() {
19
+ return this.perSecond > 0;
20
+ }
21
+ /** Take one token for `key`. Returns 0 when allowed, otherwise seconds until a token is available. */
22
+ take(key) {
23
+ if (!this.enabled)
24
+ return 0;
25
+ const t = this.now();
26
+ this.sweep(t);
27
+ const b = this.buckets.get(key) ?? { tokens: this.burst, updated: t };
28
+ b.tokens = Math.min(this.burst, b.tokens + ((t - b.updated) / 1000) * this.perSecond);
29
+ b.updated = t;
30
+ this.buckets.set(key, b);
31
+ if (b.tokens >= 1) {
32
+ b.tokens -= 1;
33
+ return 0;
34
+ }
35
+ return Math.max(1, Math.ceil((1 - b.tokens) / this.perSecond));
36
+ }
37
+ /** Drop buckets that have refilled completely, so ids that stop calling do not accumulate. */
38
+ sweep(t) {
39
+ if (t - this.lastSweep < 60_000)
40
+ return;
41
+ this.lastSweep = t;
42
+ const fullAfterMs = (this.burst / this.perSecond) * 1000;
43
+ for (const [key, b] of this.buckets) {
44
+ if (t - b.updated >= fullAfterMs)
45
+ this.buckets.delete(key);
46
+ }
47
+ }
48
+ }
49
+ /** Per-instance limit from NEXUS_RATE_LIMIT (calls/s, 0 disables) and NEXUS_RATE_BURST. */
50
+ export function rateLimitFromEnv(env = process.env) {
51
+ const num = (v, d) => {
52
+ const n = Number(v);
53
+ return v !== undefined && v !== "" && Number.isFinite(n) && n >= 0 ? n : d;
54
+ };
55
+ return { perSecond: num(env.NEXUS_RATE_LIMIT, 20), burst: num(env.NEXUS_RATE_BURST, 60) };
56
+ }
57
+ /**
58
+ * Instance ids are self-declared, so a client could rotate them to dodge the
59
+ * per-instance limit; every request is also counted per client address at
60
+ * this multiple of the per-instance limit.
61
+ */
62
+ export const ADDRESS_LIMIT_FACTOR = 5;