@chatwarden/core 0.1.6 → 0.1.8
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 +173 -3
- package/dist/src/ai/agreement.d.ts +46 -0
- package/dist/src/ai/agreement.d.ts.map +1 -0
- package/dist/src/ai/agreement.js +71 -0
- package/dist/src/ai/agreement.js.map +1 -0
- package/dist/src/ai/breaker.d.ts +31 -0
- package/dist/src/ai/breaker.d.ts.map +1 -0
- package/dist/src/ai/breaker.js +62 -0
- package/dist/src/ai/breaker.js.map +1 -0
- package/dist/src/ai/budget.d.ts +26 -0
- package/dist/src/ai/budget.d.ts.map +1 -0
- package/dist/src/ai/budget.js +57 -0
- package/dist/src/ai/budget.js.map +1 -0
- package/dist/src/ai/case.d.ts +37 -0
- package/dist/src/ai/case.d.ts.map +1 -0
- package/dist/src/ai/case.js +30 -0
- package/dist/src/ai/case.js.map +1 -0
- package/dist/src/ai/casebook.d.ts +59 -0
- package/dist/src/ai/casebook.d.ts.map +1 -0
- package/dist/src/ai/casebook.js +47 -0
- package/dist/src/ai/casebook.js.map +1 -0
- package/dist/src/ai/ceilings.d.ts +42 -0
- package/dist/src/ai/ceilings.d.ts.map +1 -0
- package/dist/src/ai/ceilings.js +59 -0
- package/dist/src/ai/ceilings.js.map +1 -0
- package/dist/src/ai/fake.d.ts +41 -0
- package/dist/src/ai/fake.d.ts.map +1 -0
- package/dist/src/ai/fake.js +75 -0
- package/dist/src/ai/fake.js.map +1 -0
- package/dist/src/ai/hash.d.ts +15 -0
- package/dist/src/ai/hash.d.ts.map +1 -0
- package/dist/src/ai/hash.js +22 -0
- package/dist/src/ai/hash.js.map +1 -0
- package/dist/src/ai/inert.d.ts +5 -0
- package/dist/src/ai/inert.d.ts.map +1 -0
- package/dist/src/ai/inert.js +22 -0
- package/dist/src/ai/inert.js.map +1 -0
- package/dist/src/ai/injection.d.ts +8 -0
- package/dist/src/ai/injection.d.ts.map +1 -0
- package/dist/src/ai/injection.js +30 -0
- package/dist/src/ai/injection.js.map +1 -0
- package/dist/src/ai/lines.d.ts +64 -0
- package/dist/src/ai/lines.d.ts.map +1 -0
- package/dist/src/ai/lines.js +172 -0
- package/dist/src/ai/lines.js.map +1 -0
- package/dist/src/ai/prompt.d.ts +48 -0
- package/dist/src/ai/prompt.d.ts.map +1 -0
- package/dist/src/ai/prompt.js +196 -0
- package/dist/src/ai/prompt.js.map +1 -0
- package/dist/src/ai/redact.d.ts +29 -0
- package/dist/src/ai/redact.d.ts.map +1 -0
- package/dist/src/ai/redact.js +160 -0
- package/dist/src/ai/redact.js.map +1 -0
- package/dist/src/ai/schema.d.ts +6 -0
- package/dist/src/ai/schema.d.ts.map +1 -0
- package/dist/src/ai/schema.js +50 -0
- package/dist/src/ai/schema.js.map +1 -0
- package/dist/src/ai/service.d.ts +332 -0
- package/dist/src/ai/service.d.ts.map +1 -0
- package/dist/src/ai/service.js +751 -0
- package/dist/src/ai/service.js.map +1 -0
- package/dist/src/ai/types.d.ts +196 -0
- package/dist/src/ai/types.d.ts.map +1 -0
- package/dist/src/ai/types.js +23 -0
- package/dist/src/ai/types.js.map +1 -0
- package/dist/src/ai/validate.d.ts +30 -0
- package/dist/src/ai/validate.d.ts.map +1 -0
- package/dist/src/ai/validate.js +160 -0
- package/dist/src/ai/validate.js.map +1 -0
- package/dist/src/ai/worker.d.ts +76 -0
- package/dist/src/ai/worker.d.ts.map +1 -0
- package/dist/src/ai/worker.js +293 -0
- package/dist/src/ai/worker.js.map +1 -0
- package/dist/src/audit.d.ts +12 -3
- package/dist/src/audit.d.ts.map +1 -1
- package/dist/src/audit.js +30 -4
- package/dist/src/audit.js.map +1 -1
- package/dist/src/chat.d.ts +39 -1
- package/dist/src/chat.d.ts.map +1 -1
- package/dist/src/chat.js +85 -6
- package/dist/src/chat.js.map +1 -1
- package/dist/src/chatwarden.d.ts +84 -1
- package/dist/src/chatwarden.d.ts.map +1 -1
- package/dist/src/chatwarden.js +168 -6
- package/dist/src/chatwarden.js.map +1 -1
- package/dist/src/decide.d.ts +36 -1
- package/dist/src/decide.d.ts.map +1 -1
- package/dist/src/decide.js +62 -2
- package/dist/src/decide.js.map +1 -1
- package/dist/src/documents.d.ts +1 -1
- package/dist/src/documents.d.ts.map +1 -1
- package/dist/src/documents.js +6 -1
- package/dist/src/documents.js.map +1 -1
- package/dist/src/freeze.d.ts +3 -0
- package/dist/src/freeze.d.ts.map +1 -0
- package/dist/src/freeze.js +10 -0
- package/dist/src/freeze.js.map +1 -0
- package/dist/src/index.d.ts +37 -8
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +23 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/ladder.d.ts +28 -0
- package/dist/src/ladder.d.ts.map +1 -1
- package/dist/src/ladder.js +37 -1
- package/dist/src/ladder.js.map +1 -1
- package/dist/src/pace.d.ts +66 -0
- package/dist/src/pace.d.ts.map +1 -0
- package/dist/src/pace.js +111 -0
- package/dist/src/pace.js.map +1 -0
- package/dist/src/pacing.d.ts +63 -0
- package/dist/src/pacing.d.ts.map +1 -0
- package/dist/src/pacing.js +198 -0
- package/dist/src/pacing.js.map +1 -0
- package/dist/src/policy.d.ts +57 -1
- package/dist/src/policy.d.ts.map +1 -1
- package/dist/src/policy.js +67 -4
- package/dist/src/policy.js.map +1 -1
- package/dist/src/profile.d.ts +64 -8
- package/dist/src/profile.d.ts.map +1 -1
- package/dist/src/profile.js +98 -27
- package/dist/src/profile.js.map +1 -1
- package/dist/src/profileError.d.ts +10 -0
- package/dist/src/profileError.d.ts.map +1 -0
- package/dist/src/profileError.js +17 -0
- package/dist/src/profileError.js.map +1 -0
- package/dist/src/retention.d.ts +28 -0
- package/dist/src/retention.d.ts.map +1 -1
- package/dist/src/retention.js +42 -10
- package/dist/src/retention.js.map +1 -1
- package/dist/src/signalCounts.d.ts +37 -0
- package/dist/src/signalCounts.d.ts.map +1 -0
- package/dist/src/signalCounts.js +117 -0
- package/dist/src/signalCounts.js.map +1 -0
- package/dist/src/signalInert.d.ts +10 -0
- package/dist/src/signalInert.d.ts.map +1 -0
- package/dist/src/signalInert.js +28 -0
- package/dist/src/signalInert.js.map +1 -0
- package/dist/src/signalService.d.ts +101 -0
- package/dist/src/signalService.d.ts.map +1 -0
- package/dist/src/signalService.js +383 -0
- package/dist/src/signalService.js.map +1 -0
- package/dist/src/signals.d.ts +186 -0
- package/dist/src/signals.d.ts.map +1 -0
- package/dist/src/signals.js +361 -0
- package/dist/src/signals.js.map +1 -0
- package/dist/src/subjects.d.ts +2 -0
- package/dist/src/subjects.d.ts.map +1 -1
- package/dist/src/subjects.js +4 -0
- package/dist/src/subjects.js.map +1 -1
- package/dist/src/team.d.ts +27 -0
- package/dist/src/team.d.ts.map +1 -1
- package/dist/src/team.js +92 -4
- package/dist/src/team.js.map +1 -1
- package/dist/src/types.d.ts +324 -2
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js.map +1 -1
- package/package.json +2 -2
- package/src/ai/agreement.ts +106 -0
- package/src/ai/breaker.ts +73 -0
- package/src/ai/budget.ts +65 -0
- package/src/ai/case.ts +74 -0
- package/src/ai/casebook.ts +75 -0
- package/src/ai/ceilings.ts +68 -0
- package/src/ai/fake.ts +105 -0
- package/src/ai/hash.ts +23 -0
- package/src/ai/inert.ts +26 -0
- package/src/ai/injection.ts +32 -0
- package/src/ai/lines.ts +216 -0
- package/src/ai/prompt.ts +223 -0
- package/src/ai/redact.ts +174 -0
- package/src/ai/schema.ts +51 -0
- package/src/ai/service.ts +961 -0
- package/src/ai/types.ts +182 -0
- package/src/ai/validate.ts +173 -0
- package/src/ai/worker.ts +372 -0
- package/src/audit.ts +31 -5
- package/src/chat.ts +109 -8
- package/src/chatwarden.ts +248 -8
- package/src/decide.ts +97 -4
- package/src/documents.ts +7 -2
- package/src/freeze.ts +8 -0
- package/src/index.ts +91 -2
- package/src/ladder.ts +59 -1
- package/src/pace.ts +139 -0
- package/src/pacing.ts +242 -0
- package/src/policy.ts +107 -7
- package/src/profile.ts +149 -27
- package/src/profileError.ts +17 -0
- package/src/retention.ts +63 -10
- package/src/signalCounts.ts +133 -0
- package/src/signalInert.ts +31 -0
- package/src/signalService.ts +406 -0
- package/src/signals.ts +489 -0
- package/src/subjects.ts +5 -0
- package/src/team.ts +93 -5
- package/src/types.ts +358 -2
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** The reasons a line can be refused for pacing, in the order they are checked. */
|
|
2
|
+
export declare const PACE_REASONS: readonly ["slow_mode", "flood", "duplicate"];
|
|
3
|
+
export type PaceReason = (typeof PACE_REASONS)[number];
|
|
4
|
+
/**
|
|
5
|
+
* How a room paces a player's lines. Every field optional on input; `null` or absent is off.
|
|
6
|
+
* @experimental
|
|
7
|
+
*/
|
|
8
|
+
export interface PaceSettings {
|
|
9
|
+
/** The least time between one player's lines in a room, ms. 500 to 600 000. */
|
|
10
|
+
slowModeMs?: number | null;
|
|
11
|
+
/** At most `lines` lines in any `windowMs`. `lines` 2 to 100, `windowMs` 1 000 to 600 000. */
|
|
12
|
+
burst?: {
|
|
13
|
+
lines: number;
|
|
14
|
+
windowMs: number;
|
|
15
|
+
} | null;
|
|
16
|
+
/** The same normalized line again inside this many ms is refused. 1 000 to 3 600 000. */
|
|
17
|
+
duplicateWindowMs?: number | null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* What the kit reads: every field filled, `null` meaning off.
|
|
21
|
+
* @experimental
|
|
22
|
+
*/
|
|
23
|
+
export interface ResolvedPace {
|
|
24
|
+
slowModeMs: number | null;
|
|
25
|
+
burst: {
|
|
26
|
+
lines: number;
|
|
27
|
+
windowMs: number;
|
|
28
|
+
} | null;
|
|
29
|
+
duplicateWindowMs: number | null;
|
|
30
|
+
}
|
|
31
|
+
/** A normalized line shorter than this (in characters) is never a duplicate, so "gg" and "ok" stay free. */
|
|
32
|
+
export declare const DUPLICATE_MIN_CHARS = 3;
|
|
33
|
+
/** How many of a player's most recent line hashes a repeat is compared with. */
|
|
34
|
+
export declare const DUPLICATE_RECENT = 8;
|
|
35
|
+
/**
|
|
36
|
+
* The most (room, player) states the limiter holds; the oldest go. A state is small (a time, a digest or two) but a burst of 100 lines
|
|
37
|
+
* keeps up to 100 times and 8 digests per player, roughly 1.5 KB: at the cap with the largest settings that is about 70 MB, so a game
|
|
38
|
+
* that sets `burst.lines` high and has very many players in one process should budget for it.
|
|
39
|
+
*/
|
|
40
|
+
export declare const PACE_KEYS_MAX = 50000;
|
|
41
|
+
/**
|
|
42
|
+
* Values the hub offers behind "Use suggested values". Never applied by themselves.
|
|
43
|
+
* @experimental
|
|
44
|
+
*/
|
|
45
|
+
export declare const PACE_SUGGESTIONS: Readonly<Record<'relaxed' | 'standard' | 'strict', ResolvedPace>>;
|
|
46
|
+
/**
|
|
47
|
+
* Check the shape and ranges of `input` and return what is set, as a new object that shares nothing with it: a key is present only
|
|
48
|
+
* when it was given a value (`null` included), and each value was read from `input` exactly once, so what was validated is what is
|
|
49
|
+
* returned even for an object with getters. Throws ProfileError like {@link resolvePace}. For a caller that must keep the override a
|
|
50
|
+
* room was given (`cw.rooms.setPace`).
|
|
51
|
+
* @experimental
|
|
52
|
+
*/
|
|
53
|
+
export declare function checkPace(input: PaceSettings, path?: string): PaceSettings;
|
|
54
|
+
/**
|
|
55
|
+
* Validate and fill. Throws ProfileError naming the field (`pace.burst.lines`), never echoing the value. `path` names the settings in
|
|
56
|
+
* the error (default `pace`). The result is a new object that shares nothing with the input.
|
|
57
|
+
* @experimental
|
|
58
|
+
*/
|
|
59
|
+
export declare function resolvePace(input: PaceSettings | undefined, path?: string): ResolvedPace;
|
|
60
|
+
/**
|
|
61
|
+
* `over` on top of `base`: a field left out inherits, `null` turns it off, a value sets it. `burst` is replaced whole, never merged by
|
|
62
|
+
* member. `over` is validated (a ProfileError with the path `pace...`). Neither argument is changed or shared with the result.
|
|
63
|
+
* @experimental
|
|
64
|
+
*/
|
|
65
|
+
export declare function mergePace(base: ResolvedPace, over: PaceSettings): ResolvedPace;
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AC,KAAK,EAAE,YAAY,GAAG,SAAS,EAAE,IAAI,SAAS,GAAG,YAAY,CAIxF;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,GAAG,YAAY,CAO9E"}
|
package/dist/src/pace.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { deepFreeze } from './freeze.js';
|
|
2
|
+
import { ProfileError } from './profileError.js';
|
|
3
|
+
// The pace settings: how a room paces a player's lines (a gap, a burst limit, no repeats). Pure data and validation, no state and no
|
|
4
|
+
// clock; the limiter that applies them is pacing.ts. Every setting is off until set, so a kit that sets none behaves as before.
|
|
5
|
+
/** The reasons a line can be refused for pacing, in the order they are checked. */
|
|
6
|
+
export const PACE_REASONS = ['slow_mode', 'flood', 'duplicate'];
|
|
7
|
+
/** A normalized line shorter than this (in characters) is never a duplicate, so "gg" and "ok" stay free. */
|
|
8
|
+
export const DUPLICATE_MIN_CHARS = 3;
|
|
9
|
+
/** How many of a player's most recent line hashes a repeat is compared with. */
|
|
10
|
+
export const DUPLICATE_RECENT = 8;
|
|
11
|
+
/**
|
|
12
|
+
* The most (room, player) states the limiter holds; the oldest go. A state is small (a time, a digest or two) but a burst of 100 lines
|
|
13
|
+
* keeps up to 100 times and 8 digests per player, roughly 1.5 KB: at the cap with the largest settings that is about 70 MB, so a game
|
|
14
|
+
* that sets `burst.lines` high and has very many players in one process should budget for it.
|
|
15
|
+
*/
|
|
16
|
+
export const PACE_KEYS_MAX = 50_000;
|
|
17
|
+
/** The allowed whole-number range of each number setting. */
|
|
18
|
+
const RANGES = {
|
|
19
|
+
slowModeMs: [500, 600_000],
|
|
20
|
+
lines: [2, 100],
|
|
21
|
+
windowMs: [1000, 600_000],
|
|
22
|
+
duplicateWindowMs: [1000, 3_600_000],
|
|
23
|
+
};
|
|
24
|
+
const BURST_KEYS = ['lines', 'windowMs'];
|
|
25
|
+
const PACE_KEYS = ['slowModeMs', 'burst', 'duplicateWindowMs'];
|
|
26
|
+
/**
|
|
27
|
+
* Values the hub offers behind "Use suggested values". Never applied by themselves.
|
|
28
|
+
* @experimental
|
|
29
|
+
*/
|
|
30
|
+
export const PACE_SUGGESTIONS = deepFreeze({
|
|
31
|
+
relaxed: { slowModeMs: null, burst: { lines: 10, windowMs: 10_000 }, duplicateWindowMs: 10_000 },
|
|
32
|
+
standard: { slowModeMs: null, burst: { lines: 6, windowMs: 10_000 }, duplicateWindowMs: 15_000 },
|
|
33
|
+
strict: { slowModeMs: 2000, burst: { lines: 5, windowMs: 10_000 }, duplicateWindowMs: 30_000 },
|
|
34
|
+
});
|
|
35
|
+
function isPlain(v) {
|
|
36
|
+
if (typeof v !== 'object' || v === null || Array.isArray(v))
|
|
37
|
+
return false;
|
|
38
|
+
const proto = Object.getPrototypeOf(v);
|
|
39
|
+
return proto === Object.prototype || proto === null;
|
|
40
|
+
}
|
|
41
|
+
/** An own, defined value: a key left out, `undefined` and an inherited property all read as "not given". */
|
|
42
|
+
function given(o, key) {
|
|
43
|
+
return Object.hasOwn(o, key) ? o[key] : undefined;
|
|
44
|
+
}
|
|
45
|
+
/** `o` must be a plain object with no key outside `allowed`; a refusal names the key. */
|
|
46
|
+
function plain(o, allowed, path, what) {
|
|
47
|
+
if (!isPlain(o))
|
|
48
|
+
throw new ProfileError(path, what);
|
|
49
|
+
for (const k of Object.keys(o))
|
|
50
|
+
if (!allowed.includes(k))
|
|
51
|
+
throw new ProfileError(`${path}.${k}`, 'is not a known setting');
|
|
52
|
+
return o;
|
|
53
|
+
}
|
|
54
|
+
function whole(v, key, path) {
|
|
55
|
+
const [lo, hi] = RANGES[key];
|
|
56
|
+
if (typeof v !== 'number' || !Number.isSafeInteger(v) || v < lo || v > hi)
|
|
57
|
+
throw new ProfileError(path, `must be a whole number from ${lo} to ${hi}`);
|
|
58
|
+
return v;
|
|
59
|
+
}
|
|
60
|
+
function checkBurst(v, path) {
|
|
61
|
+
const o = plain(v, BURST_KEYS, path, 'must be an object with lines and windowMs, or null');
|
|
62
|
+
for (const k of BURST_KEYS)
|
|
63
|
+
if (given(o, k) === undefined)
|
|
64
|
+
throw new ProfileError(`${path}.${k}`, 'is required');
|
|
65
|
+
return { lines: whole(o['lines'], 'lines', `${path}.lines`), windowMs: whole(o['windowMs'], 'windowMs', `${path}.windowMs`) };
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Check the shape and ranges of `input` and return what is set, as a new object that shares nothing with it: a key is present only
|
|
69
|
+
* when it was given a value (`null` included), and each value was read from `input` exactly once, so what was validated is what is
|
|
70
|
+
* returned even for an object with getters. Throws ProfileError like {@link resolvePace}. For a caller that must keep the override a
|
|
71
|
+
* room was given (`cw.rooms.setPace`).
|
|
72
|
+
* @experimental
|
|
73
|
+
*/
|
|
74
|
+
export function checkPace(input, path = 'pace') {
|
|
75
|
+
return checked(input, path);
|
|
76
|
+
}
|
|
77
|
+
function checked(input, path) {
|
|
78
|
+
const o = plain(input, PACE_KEYS, path, 'must be an object');
|
|
79
|
+
const out = {};
|
|
80
|
+
for (const k of PACE_KEYS) {
|
|
81
|
+
const v = given(o, k);
|
|
82
|
+
if (v !== undefined)
|
|
83
|
+
out[k] = v === null ? null : k === 'burst' ? checkBurst(v, `${path}.${k}`) : whole(v, k, `${path}.${k}`);
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Validate and fill. Throws ProfileError naming the field (`pace.burst.lines`), never echoing the value. `path` names the settings in
|
|
89
|
+
* the error (default `pace`). The result is a new object that shares nothing with the input.
|
|
90
|
+
* @experimental
|
|
91
|
+
*/
|
|
92
|
+
export function resolvePace(input, path = 'pace') {
|
|
93
|
+
if (input === undefined)
|
|
94
|
+
return { slowModeMs: null, burst: null, duplicateWindowMs: null };
|
|
95
|
+
const set = checked(input, path);
|
|
96
|
+
return { slowModeMs: set.slowModeMs ?? null, burst: set.burst ?? null, duplicateWindowMs: set.duplicateWindowMs ?? null };
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* `over` on top of `base`: a field left out inherits, `null` turns it off, a value sets it. `burst` is replaced whole, never merged by
|
|
100
|
+
* member. `over` is validated (a ProfileError with the path `pace...`). Neither argument is changed or shared with the result.
|
|
101
|
+
* @experimental
|
|
102
|
+
*/
|
|
103
|
+
export function mergePace(base, over) {
|
|
104
|
+
const set = checked(over, 'pace');
|
|
105
|
+
return {
|
|
106
|
+
slowModeMs: set.slowModeMs !== undefined ? set.slowModeMs : base.slowModeMs,
|
|
107
|
+
burst: set.burst !== undefined ? set.burst : base.burst === null ? null : { lines: base.burst.lines, windowMs: base.burst.windowMs },
|
|
108
|
+
duplicateWindowMs: set.duplicateWindowMs !== undefined ? set.duplicateWindowMs : base.duplicateWindowMs,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=pace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pace.js","sourceRoot":"","sources":["../../src/pace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,qIAAqI;AACrI,gIAAgI;AAEhI,mFAAmF;AACnF,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,WAAW,EAAE,OAAO,EAAE,WAAW,CAAU,CAAC;AA0BzE,4GAA4G;AAC5G,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AACrC,gFAAgF;AAChF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAClC;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC;AAEpC,6DAA6D;AAC7D,MAAM,MAAM,GAAwD;IAClE,UAAU,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC;IAC1B,KAAK,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC;IACf,QAAQ,EAAE,CA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,CAAC;IACjC,OAAO,EAAE,UAAU,EAAE,GAAG,CAAC,UAAU,IAAI,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,IAAI,IAAI,EAAE,iBAAiB,EAAE,GAAG,CAAC,iBAAiB,IAAI,IAAI,EAAE,CAAC;AAC5H,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,IAAkB,EAAE,IAAkB;IAC9D,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAClC,OAAO;QACL,UAAU,EAAE,GAAG,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU;QAC3E,KAAK,EAAE,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE;QACpI,iBAAiB,EAAE,GAAG,CAAC,iBAAiB,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,IAAI,CAAC,iBAAiB;KACxG,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { PaceReason, ResolvedPace } from './pace.js';
|
|
2
|
+
import type { Clock } from './types.js';
|
|
3
|
+
/** Who a line is paced as: the room, and the canonical subject (or `seat:<seatId>` for a guest). */
|
|
4
|
+
export interface PaceKey {
|
|
5
|
+
roomId: string;
|
|
6
|
+
who: string;
|
|
7
|
+
}
|
|
8
|
+
/** The answer to one line. `retryAfterMs` is a whole number from 1 up to the window of the check that refused. */
|
|
9
|
+
export type PaceVerdict = {
|
|
10
|
+
ok: true;
|
|
11
|
+
} | {
|
|
12
|
+
ok: false;
|
|
13
|
+
reason: PaceReason;
|
|
14
|
+
retryAfterMs: number;
|
|
15
|
+
signalFlood: boolean;
|
|
16
|
+
};
|
|
17
|
+
export interface Pacer {
|
|
18
|
+
/** Should this line be refused? Reads state, changes no timing state. Order: `slow_mode`, `flood`, `duplicate`. */
|
|
19
|
+
check(key: PaceKey, line: string): PaceVerdict;
|
|
20
|
+
/** Record that a line was accepted: it now counts toward the next check. A no-op while every check is off. */
|
|
21
|
+
commit(key: PaceKey, line: string): void;
|
|
22
|
+
/** Is any check on for this room right now? When not, `check` and `commit` do nothing and the caller need not build a key. */
|
|
23
|
+
active(roomId: string): boolean;
|
|
24
|
+
/** The line `check` accepted will not be committed (the fixed rate refused it): drop the digest memo that holds its text. */
|
|
25
|
+
discard(): void;
|
|
26
|
+
/** Is a line's text held in the digest memo (between a `check` that passed and its `commit` or `discard`)? A diagnostic. */
|
|
27
|
+
holdsText(): boolean;
|
|
28
|
+
/** Forget every key of a room in one step. */
|
|
29
|
+
dropRoom(roomId: string): void;
|
|
30
|
+
/** Keys held. A diagnostic: it stays at or under {@link PACE_KEYS_MAX}. */
|
|
31
|
+
size(): number;
|
|
32
|
+
}
|
|
33
|
+
/** What the pacer keeps for one player in one room. @internal */
|
|
34
|
+
export interface KeyState {
|
|
35
|
+
/** When the last committed line was accepted. */
|
|
36
|
+
lastAt: number;
|
|
37
|
+
/** The times of the last committed lines, oldest first (at most the burst's `lines`). */
|
|
38
|
+
stamps: number[];
|
|
39
|
+
/** Digests of the last {@link DUPLICATE_RECENT} committed lines of enough length, oldest first. */
|
|
40
|
+
recent: Array<{
|
|
41
|
+
h: string;
|
|
42
|
+
at: number;
|
|
43
|
+
}>;
|
|
44
|
+
/** When `check` last reported a flood for this key, or null. */
|
|
45
|
+
floodSignalAt: number | null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* NFKC, lower-case, then only letters, digits and the combining marks that belong to them: what "the same line again" means. Marks stay
|
|
49
|
+
* because in many scripts (Devanagari, Thai) the vowel signs are marks, and without them different words look alike. Variation
|
|
50
|
+
* selectors (U+FE00 to U+FE0F, which pick text or emoji presentation) are marks too but carry no meaning of their own, so they go
|
|
51
|
+
* first: `1\uFE0F\u20E3` and `1\u20E3` are one line, and an emoji line leaves nothing to count. Never stored.
|
|
52
|
+
*/
|
|
53
|
+
export declare function normalizeForRepeat(text: string): string;
|
|
54
|
+
/**
|
|
55
|
+
* `tap` is a test seam: it is handed the state (rooms by key) once, so a test can look at what a pacer holds. Nothing in the kit passes it.
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
export declare function createPacer(opts: {
|
|
59
|
+
clock: Clock;
|
|
60
|
+
settings: (roomId: string) => ResolvedPace;
|
|
61
|
+
tap?: (rooms: ReadonlyMap<string, ReadonlyMap<string, Readonly<KeyState>>>) => void;
|
|
62
|
+
}): Pacer;
|
|
63
|
+
//# sourceMappingURL=pacing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pacing.d.ts","sourceRoot":"","sources":["../../src/pacing.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC1D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAiBxC,oGAAoG;AACpG,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;CACb;AAED,kHAAkH;AAClH,MAAM,MAAM,WAAW,GAAG;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,UAAU,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,OAAO,CAAA;CAAE,CAAC;AAEvH,MAAM,WAAW,KAAK;IACpB,mHAAmH;IACnH,KAAK,CAAC,GAAG,EAAE,OAAO,E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HARS, DUPLICATE_RECENT, PACE_KEYS_MAX } from './pace.js';
|
|
3
|
+
// The pacer: the in-memory limiter behind slow mode, the burst limit and repeat refusal. Pure and clocked: it reads the settings
|
|
4
|
+
// through a function on every call, holds no line text (a repeat is remembered as a short digest of the normalized line), and its
|
|
5
|
+
// `check` is timing-pure: a refusal changes nothing the next check reads. The one write `check` makes is the `floodSignalAt`
|
|
6
|
+
// bookkeeping, set when it returns `signalFlood: true`, so a flood is reported at most once per burst window.
|
|
7
|
+
//
|
|
8
|
+
// State is per process, a map of maps by room (so a room goes in one delete), swept every SWEEP_EVERY checks against the longest
|
|
9
|
+
// window the room has configured, and capped at PACE_KEYS_MAX keys (the oldest go).
|
|
10
|
+
/** Sweep quiet keys after this many `check` calls (the same cadence as `slidingWindow`). */
|
|
11
|
+
const SWEEP_EVERY = 256;
|
|
12
|
+
/** When the cap is hit, this many of the oldest keys go at once, so the scan is paid for once per batch and not once per new key. */
|
|
13
|
+
const EVICT_BATCH = Math.ceil(PACE_KEYS_MAX / 8);
|
|
14
|
+
/** Hex characters of the digest kept for a repeat: enough that two different lines do not meet, far too few to recover a line. */
|
|
15
|
+
const DIGEST_CHARS = 16;
|
|
16
|
+
/**
|
|
17
|
+
* NFKC, lower-case, then only letters, digits and the combining marks that belong to them: what "the same line again" means. Marks stay
|
|
18
|
+
* because in many scripts (Devanagari, Thai) the vowel signs are marks, and without them different words look alike. Variation
|
|
19
|
+
* selectors (U+FE00 to U+FE0F, which pick text or emoji presentation) are marks too but carry no meaning of their own, so they go
|
|
20
|
+
* first: `1\uFE0F\u20E3` and `1\u20E3` are one line, and an emoji line leaves nothing to count. Never stored.
|
|
21
|
+
*/
|
|
22
|
+
export function normalizeForRepeat(text) {
|
|
23
|
+
return text.normalize('NFKC').toLowerCase().replace(/[\uFE00-\uFE0F]/g, '').replace(/[^\p{L}\p{N}\p{M}]/gu, '');
|
|
24
|
+
}
|
|
25
|
+
/** Letters and digits in a normalized line: marks do not count toward the minimum length, so a line of marks alone is never a repeat. */
|
|
26
|
+
const baseChars = (normalized) => Array.from(normalized.replace(/\p{M}/gu, '')).length;
|
|
27
|
+
const OK = Object.freeze({ ok: true });
|
|
28
|
+
const allOff = (p) => p.slowModeMs === null && p.burst === null && p.duplicateWindowMs === null;
|
|
29
|
+
/** A refusal with a whole-number wait of at least 1 and at most the refusing check's own window (a clock that stepped back cannot exceed it). */
|
|
30
|
+
function refusal(reason, waitMs, windowMs, signalFlood) {
|
|
31
|
+
const wait = Number.isFinite(waitMs) ? Math.min(windowMs, Math.max(1, Math.ceil(waitMs))) : windowMs;
|
|
32
|
+
return { ok: false, reason, retryAfterMs: wait, signalFlood };
|
|
33
|
+
}
|
|
34
|
+
/** The longest window a room has configured, or 0 when every check is off. */
|
|
35
|
+
function longestWindow(p) {
|
|
36
|
+
return Math.max(p.slowModeMs ?? 0, p.burst?.windowMs ?? 0, p.duplicateWindowMs ?? 0);
|
|
37
|
+
}
|
|
38
|
+
const touchedAt = (s) => (s.floodSignalAt !== null && s.floodSignalAt > s.lastAt ? s.floodSignalAt : s.lastAt);
|
|
39
|
+
/**
|
|
40
|
+
* `tap` is a test seam: it is handed the state (rooms by key) once, so a test can look at what a pacer holds. Nothing in the kit passes it.
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export function createPacer(opts) {
|
|
44
|
+
const { clock, settings } = opts;
|
|
45
|
+
const rooms = new Map();
|
|
46
|
+
opts.tap?.(rooms);
|
|
47
|
+
let keys = 0;
|
|
48
|
+
let calls = 0;
|
|
49
|
+
const sweep = (now) => {
|
|
50
|
+
for (const [roomId, room] of rooms) {
|
|
51
|
+
let longest;
|
|
52
|
+
try {
|
|
53
|
+
longest = longestWindow(settings(roomId));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (longest === 0) {
|
|
59
|
+
keys -= room.size;
|
|
60
|
+
rooms.delete(roomId);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
for (const [who, state] of room) {
|
|
64
|
+
if (now - touchedAt(state) >= longest) {
|
|
65
|
+
room.delete(who);
|
|
66
|
+
keys -= 1;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (room.size === 0)
|
|
70
|
+
rooms.delete(roomId);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/** Over the cap: drop the oldest keys, a batch at a time. */
|
|
74
|
+
const evict = () => {
|
|
75
|
+
const all = [];
|
|
76
|
+
for (const [roomId, room] of rooms)
|
|
77
|
+
for (const [who, state] of room)
|
|
78
|
+
all.push({ roomId, who, at: touchedAt(state) });
|
|
79
|
+
all.sort((a, b) => a.at - b.at);
|
|
80
|
+
const drop = Math.min(all.length, keys - PACE_KEYS_MAX + EVICT_BATCH);
|
|
81
|
+
for (let i = 0; i < drop; i += 1) {
|
|
82
|
+
const { roomId, who } = all[i];
|
|
83
|
+
const room = rooms.get(roomId);
|
|
84
|
+
if (room?.delete(who) === true)
|
|
85
|
+
keys -= 1;
|
|
86
|
+
if (room !== undefined && room.size === 0)
|
|
87
|
+
rooms.delete(roomId);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const digest = (normalized) => sha256Hex(normalized).slice(0, DIGEST_CHARS);
|
|
91
|
+
/**
|
|
92
|
+
* The digest `check` made of the line it just accepted, so `commit` of the same line does not hash it again. It holds that one line
|
|
93
|
+
* only from the check to the commit that follows it in the same call (and is replaced or cleared by the next call): the keyed state
|
|
94
|
+
* below never holds text.
|
|
95
|
+
*/
|
|
96
|
+
let memo = null;
|
|
97
|
+
/** A normalized line long enough to be called a repeat, as its digest; null for one that is not. */
|
|
98
|
+
const repeatHash = (line) => {
|
|
99
|
+
if (memo !== null && memo.line === line)
|
|
100
|
+
return memo.h;
|
|
101
|
+
const normalized = normalizeForRepeat(line);
|
|
102
|
+
return baseChars(normalized) < DUPLICATE_MIN_CHARS ? null : digest(normalized);
|
|
103
|
+
};
|
|
104
|
+
const pacer = {
|
|
105
|
+
active: (roomId) => !allOff(settings(roomId)),
|
|
106
|
+
check(key, line) {
|
|
107
|
+
const now = clock.now();
|
|
108
|
+
memo = null;
|
|
109
|
+
if (++calls % SWEEP_EVERY === 0)
|
|
110
|
+
sweep(now);
|
|
111
|
+
const pace = settings(key.roomId);
|
|
112
|
+
if (allOff(pace))
|
|
113
|
+
return OK;
|
|
114
|
+
const state = rooms.get(key.roomId)?.get(key.who);
|
|
115
|
+
if (state === undefined)
|
|
116
|
+
return OK;
|
|
117
|
+
const { slowModeMs, burst, duplicateWindowMs } = pace;
|
|
118
|
+
// State stamped after `now` (the clock stepped back) is stale, not "in the future": it never refuses, so a step back cannot lock
|
|
119
|
+
// a player out. The refusal clamp below is then a guard only.
|
|
120
|
+
if (slowModeMs !== null && state.lastAt <= now && now - state.lastAt < slowModeMs) {
|
|
121
|
+
return refusal('slow_mode', state.lastAt + slowModeMs - now, slowModeMs, false);
|
|
122
|
+
}
|
|
123
|
+
if (burst !== null) {
|
|
124
|
+
const live = state.stamps.filter((t) => t <= now && now - t < burst.windowMs).sort((a, b) => a - b);
|
|
125
|
+
if (live.length >= burst.lines) {
|
|
126
|
+
// Enough must age out to leave room for one more: with exactly `lines` live that is the oldest, with more (a burst
|
|
127
|
+
// narrowed after the lines were sent) it is the one that leaves `lines - 1`.
|
|
128
|
+
const oldest = live[live.length - burst.lines];
|
|
129
|
+
const signal = state.floodSignalAt === null || now < state.floodSignalAt || now - state.floodSignalAt >= burst.windowMs;
|
|
130
|
+
if (signal)
|
|
131
|
+
state.floodSignalAt = now;
|
|
132
|
+
return refusal('flood', oldest + burst.windowMs - now, burst.windowMs, signal);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (duplicateWindowMs !== null && state.recent.length > 0) {
|
|
136
|
+
const h = repeatHash(line);
|
|
137
|
+
if (h !== null) {
|
|
138
|
+
const seen = state.recent.find((e) => e.h === h && e.at <= now && now - e.at < duplicateWindowMs);
|
|
139
|
+
if (seen !== undefined)
|
|
140
|
+
return refusal('duplicate', seen.at + duplicateWindowMs - now, duplicateWindowMs, false);
|
|
141
|
+
memo = { line, h };
|
|
142
|
+
}
|
|
143
|
+
else
|
|
144
|
+
memo = { line, h: null };
|
|
145
|
+
}
|
|
146
|
+
return OK;
|
|
147
|
+
},
|
|
148
|
+
commit(key, line) {
|
|
149
|
+
const now = clock.now();
|
|
150
|
+
const pace = settings(key.roomId);
|
|
151
|
+
if (allOff(pace))
|
|
152
|
+
return;
|
|
153
|
+
let room = rooms.get(key.roomId);
|
|
154
|
+
let state = room?.get(key.who);
|
|
155
|
+
if (state === undefined) {
|
|
156
|
+
if (keys >= PACE_KEYS_MAX)
|
|
157
|
+
evict();
|
|
158
|
+
if (room === undefined || !rooms.has(key.roomId)) {
|
|
159
|
+
room = new Map();
|
|
160
|
+
rooms.set(key.roomId, room);
|
|
161
|
+
}
|
|
162
|
+
state = { lastAt: now, stamps: [], recent: [], floodSignalAt: null };
|
|
163
|
+
room.set(key.who, state);
|
|
164
|
+
keys += 1;
|
|
165
|
+
}
|
|
166
|
+
state.lastAt = now;
|
|
167
|
+
const { burst, duplicateWindowMs } = pace;
|
|
168
|
+
if (burst === null)
|
|
169
|
+
state.stamps = [];
|
|
170
|
+
else
|
|
171
|
+
state.stamps = [...state.stamps.filter((t) => t <= now && now - t < burst.windowMs), now].slice(-burst.lines);
|
|
172
|
+
if (duplicateWindowMs === null)
|
|
173
|
+
state.recent = [];
|
|
174
|
+
else {
|
|
175
|
+
const h = repeatHash(line);
|
|
176
|
+
const kept = state.recent.filter((e) => e.h !== h && e.at <= now && now - e.at < duplicateWindowMs);
|
|
177
|
+
if (h !== null)
|
|
178
|
+
kept.push({ h, at: now });
|
|
179
|
+
state.recent = kept.slice(-DUPLICATE_RECENT);
|
|
180
|
+
}
|
|
181
|
+
memo = null;
|
|
182
|
+
},
|
|
183
|
+
discard() {
|
|
184
|
+
memo = null;
|
|
185
|
+
},
|
|
186
|
+
holdsText: () => memo !== null,
|
|
187
|
+
dropRoom(roomId) {
|
|
188
|
+
const room = rooms.get(roomId);
|
|
189
|
+
if (room === undefined)
|
|
190
|
+
return;
|
|
191
|
+
keys -= room.size;
|
|
192
|
+
rooms.delete(roomId);
|
|
193
|
+
},
|
|
194
|
+
size: () => keys,
|
|
195
|
+
};
|
|
196
|
+
return pacer;
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=pacing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pacing.js","sourceRoot":"","sources":["../../src/pacing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAIjF,iIAAiI;AACjI,kIAAkI;AAClI,6HAA6H;AAC7H,8GAA8G;AAC9G,EAAE;AACF,iIAAiI;AACjI,oFAAoF;AAEpF,4FAA4F;AAC5F,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,qIAAqI;AACrI,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC,CAAC;AACjD,kIAAkI;AAClI,MAAM,YAAY,GAAG,EAAE,CAAC;AAwCxB;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,sBAAsB,EAAE,EAAE,CAAC,CAAC;AAClH,CAAC;AAED,yIAAyI;AACzI,MAAM,SAAS,GAAG,CAAC,UAAkB,EAAU,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;AAEvG,MAAM,EAAE,GAAgB,MAAM,CAAC,MAAM,CAAC,EAAE,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,IAAI,CAAC,IAAI,CAAC;YAClB,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACvB,CAAC;QAED,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI;KACjB,CAAC;IAEF,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/src/policy.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PolicyLayer, PolicySection, ResolvedProfile } from './profile.js';
|
|
2
2
|
import type { PlayerReporting } from './reporting.js';
|
|
3
3
|
import type { RuleRef } from './rules.js';
|
|
4
|
+
import type { SignalDropReason } from './signals.js';
|
|
4
5
|
import type { Clock, ConductLevel, Logger, Store } from './types.js';
|
|
5
6
|
/**
|
|
6
7
|
* The editable moderation profile at runtime. The profile given in code (a preset plus `overrides`) is the BASE. A moderator's
|
|
@@ -11,13 +12,39 @@ import type { Clock, ConductLevel, Logger, Store } from './types.js';
|
|
|
11
12
|
*/
|
|
12
13
|
/** How often a read may look at the store for a newer version: 10 seconds. */
|
|
13
14
|
export declare const POLICY_REFRESH_MS = 10000;
|
|
15
|
+
/**
|
|
16
|
+
* A stored signal feed that was dropped when the layer was read: the feed (`record`, `queue`, `ladder`, `ai`, or `unknown` for a name the kit
|
|
17
|
+
* does not have) and why. A code only: never a kind id, a rule id, a label or a number from the stored layer.
|
|
18
|
+
* @experimental
|
|
19
|
+
*/
|
|
20
|
+
export interface PolicyDropped {
|
|
21
|
+
feed: string;
|
|
22
|
+
reason: SignalDropReason;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The kit's own numbers the settings screens need, read only: they come from `createChatwarden` (`limits.chatRate`, `retention.signalsMs`), are
|
|
26
|
+
* never part of the stored layer, and a save cannot change them.
|
|
27
|
+
* @experimental
|
|
28
|
+
*/
|
|
29
|
+
export interface PolicyLimits {
|
|
30
|
+
/** The chat rate the kit enforces per room and seat: at most `count` lines in any `windowMs` (default 3 per 5 000 ms). Slow mode and burst sit beside it. */
|
|
31
|
+
chatRate: {
|
|
32
|
+
count: number;
|
|
33
|
+
windowMs: number;
|
|
34
|
+
};
|
|
35
|
+
/** How long signal rows are kept, ms: the longest window a signal feed may look back (default 90 days, never less than 14). */
|
|
36
|
+
signalsMs: number;
|
|
37
|
+
}
|
|
14
38
|
/** What {@link PolicyService.get} answers. */
|
|
15
39
|
export interface PolicyState {
|
|
16
40
|
/** The profile in force: base, then the stored layer. Deep-frozen. */
|
|
17
41
|
effective: ResolvedProfile;
|
|
18
42
|
/** The profile from code (preset and overrides), without the stored layer. Deep-frozen. */
|
|
19
43
|
base: ResolvedProfile;
|
|
20
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* The layer in force, as last read (`{}` when none), deep-frozen: the stored layer with any signal feed that could no longer be used
|
|
46
|
+
* taken out (see `dropped`). Send it back to `save` and the save is accepted, and repairs the stored row.
|
|
47
|
+
*/
|
|
21
48
|
layer: PolicyLayer;
|
|
22
49
|
/** The stored layer's version; 0 when nothing was ever saved. Pass it back to `save`. */
|
|
23
50
|
version: number;
|
|
@@ -33,6 +60,19 @@ export interface PolicyState {
|
|
|
33
60
|
path: string;
|
|
34
61
|
kind: 'unreadable' | 'invalid';
|
|
35
62
|
};
|
|
63
|
+
/**
|
|
64
|
+
* Present only when the stored layer held signal feeds that could no longer be used (the code stopped declaring their kind, or a
|
|
65
|
+
* window is longer than the retention now allows) and were left out of `layer` and of what is in force. One entry per feed, with the
|
|
66
|
+
* reason of the first thing lost from it. The stored row itself is rewritten by the next save.
|
|
67
|
+
* @experimental
|
|
68
|
+
*/
|
|
69
|
+
dropped?: readonly PolicyDropped[];
|
|
70
|
+
/**
|
|
71
|
+
* The chat rate limit actually in force and the signals retention window, so a settings screen can say what slow mode sits beside and how
|
|
72
|
+
* far back a feed may look. Read only, deep-frozen.
|
|
73
|
+
* @experimental
|
|
74
|
+
*/
|
|
75
|
+
limits: PolicyLimits;
|
|
36
76
|
}
|
|
37
77
|
/** What {@link PolicyService.save} and {@link PolicyService.reset} answer. */
|
|
38
78
|
export type PolicySaveResult = {
|
|
@@ -81,17 +121,33 @@ export declare class PolicyService {
|
|
|
81
121
|
private state;
|
|
82
122
|
/** When the store was last looked at (epoch ms), or null before the first read. */
|
|
83
123
|
private lastCheck;
|
|
124
|
+
/** How long signal rows are kept, ms: a feed window longer than this could never be met. */
|
|
125
|
+
private readonly signalsMs;
|
|
126
|
+
/** The chat rate in force (`limits.chatRate`), for `get().limits`. */
|
|
127
|
+
private readonly chatRate;
|
|
84
128
|
constructor(deps: {
|
|
85
129
|
store: Store;
|
|
86
130
|
clock: Clock;
|
|
87
131
|
logger: Logger | undefined;
|
|
88
132
|
base: ResolvedProfile;
|
|
133
|
+
signalsMs?: () => number;
|
|
134
|
+
chatRate?: {
|
|
135
|
+
count: number;
|
|
136
|
+
windowMs: number;
|
|
137
|
+
};
|
|
89
138
|
});
|
|
90
139
|
private get supported();
|
|
91
140
|
/** Read the stored layer once, at creation. A store failure here is the caller's (like every other load). */
|
|
92
141
|
load(): void;
|
|
93
142
|
/** Make `stored` the layer in force: the effective profile is rebuilt, or the base kept when the layer cannot be used. */
|
|
94
143
|
private adopt;
|
|
144
|
+
/**
|
|
145
|
+
* The stored layer with any signal feed that can no longer be used taken out (it names a kind the code stopped declaring, or a window
|
|
146
|
+
* longer than the retention now allows), and what was taken out. Without this one stale feed would make the whole layer unusable and
|
|
147
|
+
* silently drop the owner's other edits too. Logged by feed name only. Anything else wrong with the layer is left for
|
|
148
|
+
* `applyPolicyLayer` to refuse; a layer with nothing to drop comes back as it is.
|
|
149
|
+
*/
|
|
150
|
+
private withoutStaleFeeds;
|
|
95
151
|
/** Look at the store now and adopt a strictly newer version (an older one, say a restored backup, never regresses what is in force). A store failure throws. */
|
|
96
152
|
private reload;
|
|
97
153
|
/** The cheap check every read does: at most one store look per {@link POLICY_REFRESH_MS}; a failure keeps what is in force. */
|
package/dist/src/policy.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../../src/policy.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEhF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAI1C,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAErE;;;;;;GAMG;AAEH,8EAA8E;AAC9E,eAAO,MAAM,iBAAiB,QAAS,CAAC;AAExC,8CAA8C;AAC9C,MAAM,WAAW,WAAW;IAC1B,sEAAsE;IACtE,SAAS,EAAE,eAAe,CAAC;IAC3B,2FAA2F;IAC3F,IAAI,EAAE,eAAe,CAAC;IACtB
|
|
1
|
+
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../../src/policy.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEhF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAI1C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAIrD,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAErE;;;;;;GAMG;AAEH,8EAA8E;AAC9E,eAAO,MAAM,iBAAiB,QAAS,CAAC;AAExC;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,gBAAgB,CAAC;CAC1B;AAED;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,6JAA6J;IAC7J,QAAQ,EAAE;QAAE,KAAK,EAAE,MAAM,C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