n2n-nexus 0.4.2 → 0.6.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/CHANGELOG.md +87 -0
- package/README.md +74 -117
- package/assets/n2n-nexus-banner.jpg +0 -0
- package/assets/n2n-nexus-icon.png +0 -0
- package/assets/n2n-nexus-logo.png +0 -0
- package/build/client/nexus-client.js +30 -14
- package/build/config/cli.js +0 -3
- package/build/config/index.js +0 -10
- package/build/constants.js +7 -11
- package/build/daemon/index.js +35 -19
- package/build/daemon/rate-limit.js +62 -0
- package/build/daemon/server.js +349 -542
- package/build/daemon/tool-outputs.js +155 -0
- package/build/daemon/tools.js +271 -0
- package/build/index.js +1 -1
- package/build/server/instance-id.js +19 -0
- package/build/server/nexus.js +152 -43
- package/build/storage/docs.js +14 -7
- package/build/storage/events.js +31 -0
- package/build/storage/fs-utils.js +42 -0
- package/build/storage/global-messages.js +44 -0
- package/build/storage/index.js +44 -41
- package/build/storage/migrations.js +79 -0
- package/build/storage/paths.js +0 -3
- package/build/storage/projects.js +4 -5
- package/build/storage/registry.js +67 -25
- package/build/storage/sqlite-meeting.js +35 -23
- package/build/storage/sqlite.js +41 -24
- package/build/storage/tasks.js +29 -103
- package/build/types.js +1 -1
- package/docs/ARCHITECTURE.md +29 -20
- package/docs/ARCHITECTURE_zh.md +29 -20
- package/docs/ASSISTANT_GUIDE.md +70 -67
- package/docs/README_zh.md +25 -15
- package/llms.txt +11 -1
- package/package.json +24 -14
- package/build/storage/logs.js +0 -60
- package/build/storage/meetings.js +0 -276
- package/build/storage/store.js +0 -153
|
@@ -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;
|