@chatwarden/core 0.1.7 → 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.
Files changed (126) hide show
  1. package/README.md +124 -4
  2. package/dist/src/ai/case.d.ts +3 -3
  3. package/dist/src/ai/case.d.ts.map +1 -1
  4. package/dist/src/ai/case.js +1 -1
  5. package/dist/src/ai/case.js.map +1 -1
  6. package/dist/src/ai/hash.d.ts +3 -1
  7. package/dist/src/ai/hash.d.ts.map +1 -1
  8. package/dist/src/ai/hash.js +5 -2
  9. package/dist/src/ai/hash.js.map +1 -1
  10. package/dist/src/ai/prompt.d.ts +6 -2
  11. package/dist/src/ai/prompt.d.ts.map +1 -1
  12. package/dist/src/ai/prompt.js +11 -0
  13. package/dist/src/ai/prompt.js.map +1 -1
  14. package/dist/src/ai/service.d.ts +5 -1
  15. package/dist/src/ai/service.d.ts.map +1 -1
  16. package/dist/src/ai/service.js +2 -0
  17. package/dist/src/ai/service.js.map +1 -1
  18. package/dist/src/ai/types.d.ts +18 -2
  19. package/dist/src/ai/types.d.ts.map +1 -1
  20. package/dist/src/ai/types.js +2 -2
  21. package/dist/src/ai/types.js.map +1 -1
  22. package/dist/src/ai/worker.d.ts +3 -1
  23. package/dist/src/ai/worker.d.ts.map +1 -1
  24. package/dist/src/ai/worker.js +10 -3
  25. package/dist/src/ai/worker.js.map +1 -1
  26. package/dist/src/chat.d.ts +39 -1
  27. package/dist/src/chat.d.ts.map +1 -1
  28. package/dist/src/chat.js +85 -6
  29. package/dist/src/chat.js.map +1 -1
  30. package/dist/src/chatwarden.d.ts +68 -1
  31. package/dist/src/chatwarden.d.ts.map +1 -1
  32. package/dist/src/chatwarden.js +119 -5
  33. package/dist/src/chatwarden.js.map +1 -1
  34. package/dist/src/documents.d.ts +1 -1
  35. package/dist/src/documents.d.ts.map +1 -1
  36. package/dist/src/documents.js +3 -1
  37. package/dist/src/documents.js.map +1 -1
  38. package/dist/src/freeze.d.ts +3 -0
  39. package/dist/src/freeze.d.ts.map +1 -0
  40. package/dist/src/freeze.js +10 -0
  41. package/dist/src/freeze.js.map +1 -0
  42. package/dist/src/index.d.ts +13 -6
  43. package/dist/src/index.d.ts.map +1 -1
  44. package/dist/src/index.js +7 -2
  45. package/dist/src/index.js.map +1 -1
  46. package/dist/src/ladder.d.ts +28 -0
  47. package/dist/src/ladder.d.ts.map +1 -1
  48. package/dist/src/ladder.js +37 -1
  49. package/dist/src/ladder.js.map +1 -1
  50. package/dist/src/pace.d.ts +66 -0
  51. package/dist/src/pace.d.ts.map +1 -0
  52. package/dist/src/pace.js +111 -0
  53. package/dist/src/pace.js.map +1 -0
  54. package/dist/src/pacing.d.ts +63 -0
  55. package/dist/src/pacing.d.ts.map +1 -0
  56. package/dist/src/pacing.js +198 -0
  57. package/dist/src/pacing.js.map +1 -0
  58. package/dist/src/policy.d.ts +57 -1
  59. package/dist/src/policy.d.ts.map +1 -1
  60. package/dist/src/policy.js +67 -4
  61. package/dist/src/policy.js.map +1 -1
  62. package/dist/src/profile.d.ts +35 -8
  63. package/dist/src/profile.d.ts.map +1 -1
  64. package/dist/src/profile.js +57 -25
  65. package/dist/src/profile.js.map +1 -1
  66. package/dist/src/profileError.d.ts +10 -0
  67. package/dist/src/profileError.d.ts.map +1 -0
  68. package/dist/src/profileError.js +17 -0
  69. package/dist/src/profileError.js.map +1 -0
  70. package/dist/src/retention.d.ts +9 -0
  71. package/dist/src/retention.d.ts.map +1 -1
  72. package/dist/src/retention.js +10 -1
  73. package/dist/src/retention.js.map +1 -1
  74. package/dist/src/signalCounts.d.ts +37 -0
  75. package/dist/src/signalCounts.d.ts.map +1 -0
  76. package/dist/src/signalCounts.js +117 -0
  77. package/dist/src/signalCounts.js.map +1 -0
  78. package/dist/src/signalInert.d.ts +10 -0
  79. package/dist/src/signalInert.d.ts.map +1 -0
  80. package/dist/src/signalInert.js +28 -0
  81. package/dist/src/signalInert.js.map +1 -0
  82. package/dist/src/signalService.d.ts +101 -0
  83. package/dist/src/signalService.d.ts.map +1 -0
  84. package/dist/src/signalService.js +383 -0
  85. package/dist/src/signalService.js.map +1 -0
  86. package/dist/src/signals.d.ts +186 -0
  87. package/dist/src/signals.d.ts.map +1 -0
  88. package/dist/src/signals.js +361 -0
  89. package/dist/src/signals.js.map +1 -0
  90. package/dist/src/subjects.d.ts +2 -0
  91. package/dist/src/subjects.d.ts.map +1 -1
  92. package/dist/src/subjects.js +4 -0
  93. package/dist/src/subjects.js.map +1 -1
  94. package/dist/src/team.d.ts +27 -0
  95. package/dist/src/team.d.ts.map +1 -1
  96. package/dist/src/team.js +92 -4
  97. package/dist/src/team.js.map +1 -1
  98. package/dist/src/types.d.ts +56 -1
  99. package/dist/src/types.d.ts.map +1 -1
  100. package/dist/src/types.js.map +1 -1
  101. package/package.json +2 -2
  102. package/src/ai/case.ts +4 -4
  103. package/src/ai/hash.ts +5 -2
  104. package/src/ai/prompt.ts +16 -1
  105. package/src/ai/service.ts +8 -2
  106. package/src/ai/types.ts +19 -2
  107. package/src/ai/worker.ts +12 -4
  108. package/src/chat.ts +109 -8
  109. package/src/chatwarden.ts +184 -7
  110. package/src/documents.ts +5 -3
  111. package/src/freeze.ts +8 -0
  112. package/src/index.ts +41 -3
  113. package/src/ladder.ts +59 -1
  114. package/src/pace.ts +139 -0
  115. package/src/pacing.ts +242 -0
  116. package/src/policy.ts +107 -7
  117. package/src/profile.ts +83 -25
  118. package/src/profileError.ts +17 -0
  119. package/src/retention.ts +17 -1
  120. package/src/signalCounts.ts +133 -0
  121. package/src/signalInert.ts +31 -0
  122. package/src/signalService.ts +406 -0
  123. package/src/signals.ts +489 -0
  124. package/src/subjects.ts +5 -0
  125. package/src/team.ts +93 -5
  126. package/src/types.ts +65 -1
@@ -11,10 +11,11 @@ export const RETENTION_DEFAULTS = {
11
11
  auditMs: 365 * DAY_MS,
12
12
  aiOutcomesMs: 365 * DAY_MS,
13
13
  aiExamplesMs: 180 * DAY_MS,
14
+ signalsMs: 90 * DAY_MS,
14
15
  };
15
16
  /** The ladder looks back 90 days and "My record" shows 90 days, so the rows behind them must outlive that. */
16
17
  const RECORD_FLOOR_MS = 90 * DAY_MS;
17
- const FIELDS = ['reportsMs', 'sanctionAfterEndMs', 'strikesMs', 'noticesMs', 'badFaithMs', 'auditMs', 'aiOutcomesMs', 'aiExamplesMs'];
18
+ const FIELDS = ['reportsMs', 'sanctionAfterEndMs', 'strikesMs', 'noticesMs', 'badFaithMs', 'auditMs', 'aiOutcomesMs', 'aiExamplesMs', 'signalsMs'];
18
19
  /**
19
20
  * Refuse a retention policy that would delete rows the kit still reads. Only the fields present are checked (an
20
21
  * absent one takes a default, and the defaults satisfy every floor):
@@ -24,6 +25,7 @@ const FIELDS = ['reportsMs', 'sanctionAfterEndMs', 'strikesMs', 'noticesMs', 'ba
24
25
  * - `auditMs` at least 90 days: the staff trail is how a community answers "who did that"
25
26
  * - `aiOutcomesMs` at least 90 days: the agree-or-override record is the data the AI's later gates read
26
27
  * - `aiExamplesMs` at least 0 (0 keeps no example text at all)
28
+ * - `signalsMs` at least 14 days: a counting window needs at least two weeks of rows
27
29
  *
28
30
  * Every field present must also be a finite number (`reportsMs` and `strikesMs` have no floor, only this).
29
31
  *
@@ -55,6 +57,9 @@ export function checkRetentionFloors(policy, suspendWindowMs) {
55
57
  if (below(policy.aiExamplesMs, 0)) {
56
58
  throw new ProfileError('retention.aiExamplesMs', 'must be 0 or more: 0 keeps no example text at all');
57
59
  }
60
+ if (below(policy.signalsMs, 14 * DAY_MS)) {
61
+ throw new ProfileError('retention.signalsMs', 'must be at least 14 days: a counting window needs at least two weeks of rows');
62
+ }
58
63
  if (below(policy.badFaithMs, suspendWindowMs)) {
59
64
  throw new ProfileError('retention.badFaithMs', `must be at least limits.reports.suspendWindowMs (${Math.round((suspendWindowMs / DAY_MS) * 100) / 100} days): ` +
60
65
  "a report dismissed as bad faith counts toward its reporter's suspension for that long");
@@ -81,6 +86,7 @@ function checkWindow(name, ms) {
81
86
  * - AI reads: `createdAt < now - reportsMs` (a read also goes with its report, in `purgeReports`); AI outcomes (agree-or-override
82
87
  * record, no chat text): `decidedAt < now - aiOutcomesMs`; the pseudonymised example kept with an outcome:
83
88
  * `decidedAt < now - aiExamplesMs`, the row stays with its counts. A store without the AI methods has none of these.
89
+ * - signals: `at < now - signalsMs` (90 days by default, never less than 14); a store without the signal methods has none
84
90
  * - notices: withdrawn, or a seen warning / report outcome, with `createdAt < now - noticesMs`; or
85
91
  * `until < now - noticesMs`. A notice for a permanent or running sanction is kept while it applies (agrees with
86
92
  * `NoticeService.record`), and an unseen warning is kept until it is seen or withdrawn.
@@ -98,6 +104,7 @@ export async function purge(store, now, policy = {}) {
98
104
  auditMs: policy.auditMs ?? RETENTION_DEFAULTS.auditMs,
99
105
  aiOutcomesMs: policy.aiOutcomesMs ?? RETENTION_DEFAULTS.aiOutcomesMs,
100
106
  aiExamplesMs: policy.aiExamplesMs ?? RETENTION_DEFAULTS.aiExamplesMs,
107
+ signalsMs: policy.signalsMs ?? RETENTION_DEFAULTS.signalsMs,
101
108
  };
102
109
  if (!Number.isFinite(now))
103
110
  throw new Error('purge: invalid now');
@@ -109,6 +116,7 @@ export async function purge(store, now, policy = {}) {
109
116
  checkWindow('auditMs', p.auditMs);
110
117
  checkWindow('aiOutcomesMs', p.aiOutcomesMs);
111
118
  checkWindow('aiExamplesMs', p.aiExamplesMs);
119
+ checkWindow('signalsMs', p.signalsMs);
112
120
  return store.transaction(() => {
113
121
  const reports = store.purgeReports(now - p.reportsMs, now - p.badFaithMs);
114
122
  const ai = store.purgeAi?.(now - p.reportsMs, now - p.aiOutcomesMs, now - p.aiExamplesMs) ?? { reads: 0, outcomes: 0 };
@@ -122,6 +130,7 @@ export async function purge(store, now, policy = {}) {
122
130
  webhookKeys: store.purgeWebhookKeys(now - WEBHOOK_KEY_TTL_MS),
123
131
  aiReads: ai.reads,
124
132
  aiOutcomes: ai.outcomes,
133
+ signals: store.purgeSignals?.(now - p.signalsMs) ?? 0,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GAAG,CAAC,CAAC,SAAS,CAAC;YAC9C,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC;YACxC,WAAW,EAAE,KAAK,CAAC,gBAAgB,CAAC,GAAG,GAAG,kBAAkB,CAAC;YAC7D,OAAO,EAAE,EAAE,CAAC,KAAK;YACjB,UAAU,EAAE,EAAE,CAAC,QAAQ;YACvB,OAAO,EAAE,KAAK,CAAC,YAAY,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC;SACtD,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,37 @@
1
+ import type { SignalCount, SignalPolarity, SignalThreshold } from './signals.js';
2
+ import type { SignalSourceRow, SubjectId } from './types.js';
3
+ /** What counting needs to know. `kinds` is the request: only these kinds are counted, and only if they are own keys. */
4
+ export interface CountContext {
5
+ kinds: Readonly<Record<string, {
6
+ label: string;
7
+ polarity: SignalPolarity;
8
+ }>>;
9
+ /** Folds a merged subject into the person it became (`SubjectIndex.canonical`). */
10
+ canonical: (subject: SubjectId) => SubjectId;
11
+ /** The window the rows were read for, reported on each count. */
12
+ windowDays: number;
13
+ }
14
+ /** What one threshold evaluation answers. `firstAt` and `lastAt` are null when no source counted. */
15
+ export interface ThresholdResult {
16
+ met: boolean;
17
+ distinct: number;
18
+ weight: number;
19
+ firstAt: number | null;
20
+ lastAt: number | null;
21
+ }
22
+ /**
23
+ * The counts for a person: one entry per requested kind that has at least one counted source, in the order of the request. `rows` are
24
+ * the store's grouped rows for the person and all their aliases, already limited to the window; aliases fold here. For staff tooling
25
+ * and your own game: never send the result to a player.
26
+ */
27
+ export declare function countSignals(rows: readonly SignalSourceRow[], ctx: CountContext): SignalCount[];
28
+ /**
29
+ * Does a person meet a threshold? Sources are counted per kind and the numbers added across the kinds, so one player who both muted and
30
+ * blocked counts twice for a two-kind threshold (use one kind per threshold when that matters). Met when `distinct >= minDistinct` and,
31
+ * if `minWeight` is given, `weight >= minWeight`. A kind that is not in `ctx.kinds` is ignored. The window is the caller's: `rows` are
32
+ * already limited to `t.windowDays`.
33
+ */
34
+ export declare function evaluateThreshold(rows: readonly SignalSourceRow[], ctx: Omit<CountContext, 'windowDays'> & {
35
+ windowDays?: number;
36
+ }, t: SignalThreshold): ThresholdResult;
37
+ //# sourceMappingURL=signalCounts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signalCounts.d.ts","sourceRoot":"","sources":["../../src/signalCounts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAK7D,wHAAwH;AACxH,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,cAAc,CAAA;KAAE,CAAC,CAAC,CAAC;IAC7E,mFAAmF;IACnF,SAAS,EAAE,CAAC,OAAO,EAAE,SAAS,KAAK,SAAS,CAAC;IAC7C,iEAAiE;IACjE,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,qGAAqG;AACrG,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,OAAO,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB;AAmED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,eAAe,EAAE,EAAE,GAAG,EAAE,YAAY,GAAG,WAAW,EAAE,CAY/F;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,SAAS,eAAe,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,YAAY,EAAE,YAAY,CAAC,GAAG;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,EAAE,CAAC,EAAE,eAAe,GAAG,eAAe,CAkBxK"}
@@ -0,0 +1,117 @@
1
+ /**
2
+ * The source of a row, with a player folded through `canonical` so two aliases of one person are one source: `b:<canonical by>` when
3
+ * the row has a `by` (the row's `bySubject`, or the id inside the key when the row has none), and the key as stored for a room
4
+ * (`r:`) or a day (`d:`).
5
+ */
6
+ function sourceOf(row, canonical) {
7
+ if (!row.sourceKey.startsWith('b:'))
8
+ return row.sourceKey;
9
+ const by = row.bySubject ?? row.sourceKey.slice(2);
10
+ return `b:${canonical(by)}`;
11
+ }
12
+ /**
13
+ * Count one kind: group by source, take the highest weight in a source, drop sources whose weight is not above 0, then the number of
14
+ * sources left and the sum of their weights. The times span the sources that counted.
15
+ */
16
+ function countKind(rows, canonical) {
17
+ const sources = new Map();
18
+ for (const row of rows) {
19
+ const w = row.weight;
20
+ if (!(typeof w === 'number' && w > 0 && Number.isFinite(w)))
21
+ continue;
22
+ const key = sourceOf(row, canonical);
23
+ // A source that is the subject itself (an alt account linked to the main one after it signalled) is not a second opinion.
24
+ if (key === `b:${canonical(row.subject)}`)
25
+ continue;
26
+ const s = sources.get(key);
27
+ if (s === undefined)
28
+ sources.set(key, { distinct: 1, weight: w, firstAt: row.firstAt, lastAt: row.lastAt });
29
+ else {
30
+ if (w > s.weight)
31
+ s.weight = w;
32
+ if (row.firstAt < s.firstAt)
33
+ s.firstAt = row.firstAt;
34
+ if (row.lastAt > s.lastAt)
35
+ s.lastAt = row.lastAt;
36
+ }
37
+ }
38
+ if (sources.size === 0)
39
+ return null;
40
+ let weight = 0;
41
+ let firstAt = Infinity;
42
+ let lastAt = -Infinity;
43
+ for (const s of sources.values()) {
44
+ weight += s.weight;
45
+ if (s.firstAt < firstAt)
46
+ firstAt = s.firstAt;
47
+ if (s.lastAt > lastAt)
48
+ lastAt = s.lastAt;
49
+ }
50
+ return { distinct: sources.size, weight: round(weight), firstAt, lastAt };
51
+ }
52
+ /** Weights are floats: ten sources of 0.1 sum to 0.9999999999999999. Sums are rounded to 6 places and compared with a tolerance of 1e-9. */
53
+ const round = (n) => Math.round(n * 1e6) / 1e6;
54
+ /** Rows by kind, for the kinds in the request only (own keys: a row of an undeclared or prototype-named kind is ignored). */
55
+ function byKind(rows, kinds, only) {
56
+ const out = new Map();
57
+ for (const row of rows) {
58
+ if (!Object.hasOwn(kinds, row.kind) || (only !== undefined && !only.includes(row.kind)))
59
+ continue;
60
+ const list = out.get(row.kind);
61
+ if (list === undefined)
62
+ out.set(row.kind, [row]);
63
+ else
64
+ list.push(row);
65
+ }
66
+ return out;
67
+ }
68
+ /**
69
+ * The counts for a person: one entry per requested kind that has at least one counted source, in the order of the request. `rows` are
70
+ * the store's grouped rows for the person and all their aliases, already limited to the window; aliases fold here. For staff tooling
71
+ * and your own game: never send the result to a player.
72
+ */
73
+ export function countSignals(rows, ctx) {
74
+ const grouped = byKind(rows, ctx.kinds);
75
+ const out = [];
76
+ for (const kind of Object.keys(ctx.kinds)) {
77
+ const list = grouped.get(kind);
78
+ if (list === undefined)
79
+ continue;
80
+ const counted = countKind(list, ctx.canonical);
81
+ if (counted === null)
82
+ continue;
83
+ const spec = ctx.kinds[kind];
84
+ out.push({ kind, label: spec.label, polarity: spec.polarity, windowDays: ctx.windowDays, ...counted });
85
+ }
86
+ return out;
87
+ }
88
+ /**
89
+ * Does a person meet a threshold? Sources are counted per kind and the numbers added across the kinds, so one player who both muted and
90
+ * blocked counts twice for a two-kind threshold (use one kind per threshold when that matters). Met when `distinct >= minDistinct` and,
91
+ * if `minWeight` is given, `weight >= minWeight`. A kind that is not in `ctx.kinds` is ignored. The window is the caller's: `rows` are
92
+ * already limited to `t.windowDays`.
93
+ */
94
+ export function evaluateThreshold(rows, ctx, t) {
95
+ const grouped = byKind(rows, ctx.kinds, t.kinds);
96
+ let distinct = 0;
97
+ let weight = 0;
98
+ let firstAt = null;
99
+ let lastAt = null;
100
+ for (const kind of t.kinds) {
101
+ const list = grouped.get(kind);
102
+ if (list === undefined)
103
+ continue;
104
+ const counted = countKind(list, ctx.canonical);
105
+ if (counted === null)
106
+ continue;
107
+ distinct += counted.distinct;
108
+ weight = round(weight + counted.weight);
109
+ if (firstAt === null || counted.firstAt < firstAt)
110
+ firstAt = counted.firstAt;
111
+ if (lastAt === null || counted.lastAt > lastAt)
112
+ lastAt = counted.lastAt;
113
+ }
114
+ const met = distinct >= t.minDistinct && (t.minWeight === undefined || weight >= t.minWeight - 1e-9);
115
+ return { met, distinct, weight, firstAt, lastAt };
116
+ }
117
+ //# sourceMappingURL=signalCounts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signalCounts.js","sourceRoot":"","sources":["../../src/signalCounts.ts"],"names":[],"mappings":"AA+BA;;;;GAIG;AACH,SAAS,QAAQ,CAAC,GAAoB,EAAE,SAAsC;IAC5E,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,GAAG,CAAC,SAAS,CAAC;IAC1D,MAAM,EAAE,GAAG,GAAG,CAAC,SAAS,IAAI,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACnD,OAAO,KAAK,SAAS,CAAC,EAAE,CAAC,EAAE,CAAC;AAC9B,CAAC;AAED;;;GAGG;AACH,SAAS,SAAS,CAAC,IAAgC,EAAE,SAAsC;IACzF,MAAM,OAAO,GAAG,IAAI,GAAG,EAAmB,CAAC;IAC3C,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC;QACrB,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;YAAE,SAAS;QACtE,MAAM,GAAG,GAAG,QAAQ,CAAC,GAAG,EAAE,S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AO,KAAK,IAAI,IAAI,OAAO,CAAC,OAAO,GAAG,OAAO;YAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC7E,IAAI,MAAM,KAAK,IAAI,IAAI,OAAO,CAAC,MAAM,GAAG,MAAM;YAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC1E,CAAC;IACD,MAAM,GAAG,GAAG,QAAQ,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,IAAI,MAAM,IAAI,CAAC,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;IACrG,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACpD,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { ResolvedSignals } from './signals.js';
2
+ import type { SignalRuntime } from './signalService.js';
3
+ import type { Logger } from './types.js';
4
+ /**
5
+ * What `cw.signals` is when the store cannot keep signals (it lacks the three signal methods): `record` is false and said once, every reader
6
+ * is empty, and `kinds` still lists what the profile declares. The real service (`./signalService.js`) is loaded only for a store that can keep
7
+ * them, so a page that never records a signal does not carry it.
8
+ */
9
+ export declare function createInertSignals(logger: Logger | undefined, settings: () => ResolvedSignals): SignalRuntime;
10
+ //# sourceMappingURL=signalInert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signalInert.d.ts","sourceRoot":"","sources":["../../src/signalInert.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,GAAG,aAAa,CAmB7G"}
@@ -0,0 +1,28 @@
1
+ import { safeWarn } from './log.js';
2
+ import { BUILTIN_SIGNAL_KINDS } from './signals.js';
3
+ /**
4
+ * What `cw.signals` is when the store cannot keep signals (it lacks the three signal methods): `record` is false and said once, every reader
5
+ * is empty, and `kinds` still lists what the profile declares. The real service (`./signalService.js`) is loaded only for a store that can keep
6
+ * them, so a page that never records a signal does not carry it.
7
+ */
8
+ export function createInertSignals(logger, settings) {
9
+ let said = false;
10
+ const record = () => {
11
+ if (!said) {
12
+ said = true;
13
+ safeWarn(logger, 'chatwarden.signals_store_unsupported');
14
+ }
15
+ return false;
16
+ };
17
+ return {
18
+ record,
19
+ flood: record,
20
+ counts: () => [],
21
+ kinds: () => Object.entries(settings().kinds).map(([kind, k]) => ({ kind, label: k.label, polarity: k.polarity, weight: k.weight, builtin: Object.hasOwn(BUILTIN_SIGNAL_KINDS, kind) })),
22
+ boosted: () => new Set(),
23
+ forRecord: () => [],
24
+ forAi: () => [],
25
+ ladderHits: () => [],
26
+ };
27
+ }
28
+ //# sourceMappingURL=signalInert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signalInert.js","sourceRoot":"","sources":["../../src/signalInert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAKpD;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAA0B,EAAE,QAA+B;IAC5F,IAAI,IAAI,GAAG,KAAK,CAAC;IACjB,MAAM,MAAM,GAAG,GAAY,EAAE;QAC3B,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,IAAI,GAAG,IAAI,CAAC;YACZ,QAAQ,CAAC,MAAM,EAAE,sCAAsC,CAAC,CAAC;QAC3D,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IACF,OAAO;QACL,MAAM;QACN,KAAK,EAAE,MAAM;QACb,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE;QAChB,KAAK,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,oBAAoB,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QACxL,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI,GAAG,EAAE;QACxB,SAAS,EAAE,GAAG,EAAE,CAAC,EAAE;QACnB,KAAK,EAAE,GAAG,EAAE,CAAC,EAAE;QACf,UAAU,EAAE,GAAG,EAAE,CAAC,EAAE;KACrB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,101 @@
1
+ import type { SignalHit } from './ladder.js';
2
+ import type { ResolvedSignals, SignalCount, SignalInput, SignalKindView } from './signals.js';
3
+ import type { SubjectIndex } from './subjects.js';
4
+ import type { Clock, Logger, RawWho, Store, SubjectId } from './types.js';
5
+ /** Is this a room id a signal may carry (1 to 64 of A-Z a-z 0-9 _ . : -)? */
6
+ export declare const isSignalRoomId: (v: unknown) => v is string;
7
+ /**
8
+ * The room id a flood row carries: the id itself when the signal rules accept it, else a stable `h` plus 16 hex digits of its SHA-256 (so a
9
+ * game whose room ids hold a space or run long still gets one source per room, and only the digest is stored). Undefined for no room.
10
+ */
11
+ export declare const floodRoomId: (v: unknown) => string | undefined;
12
+ export type SignalRefusal = 'undeclared' | 'malformed' | 'unknown_subject' | 'self' | 'time' | 'capped' | 'no_store' | 'store_failed';
13
+ export interface SignalServiceDeps {
14
+ store: Store;
15
+ clock: Clock;
16
+ logger: Logger | undefined;
17
+ subjects: SubjectIndex;
18
+ /** The person behind a player's connection details: FOUND and never created. */
19
+ personOf: (who: RawWho) => SubjectId | null;
20
+ /** The signal settings in force right now (kinds, guards, feeds). Read on every call. */
21
+ settings: () => ResolvedSignals;
22
+ /** How long signal rows are kept, ms. */
23
+ signalsMs: () => number;
24
+ /** How old is this player's account, in days? See `ChatwardenOptions.signals`. */
25
+ accountAgeDays?: (accountIds: readonly string[]) => number | null | undefined;
26
+ }
27
+ /** What the kit asks of the signal service: the real one, or the inert one (`./signalInert.js`) for a store that cannot keep signals. */
28
+ export interface SignalRuntime {
29
+ /** Record one signal; false for any refusal. Never throws. */
30
+ record(input: SignalInput): boolean;
31
+ /** The built-in `flood_triggered` for a person, with the room only when the signal rules accept its id. Never throws. */
32
+ flood(subject: SubjectId, roomId: string): void;
33
+ counts(subject: SubjectId, opts?: {
34
+ windowDays?: number;
35
+ kinds?: readonly string[];
36
+ }): SignalCount[];
37
+ kinds(): SignalKindView[];
38
+ boosted(people: readonly SubjectId[]): Set<SubjectId>;
39
+ forRecord(subject: SubjectId): SignalCount[];
40
+ /** The counts the AI read may be shown (`feeds.ai`): the feed's kinds and window. `[]` when the feed is off. Never throws. */
41
+ forAi(subject: SubjectId): SignalCount[];
42
+ ladderHits(subject: SubjectId): SignalHit[];
43
+ }
44
+ export declare class SignalService implements SignalRuntime {
45
+ private readonly deps;
46
+ /** One throttle per refusal reason, so a burst of `malformed` cannot hide a `store_failed` for a minute. */
47
+ private readonly refusedWarn;
48
+ private readonly readWarn;
49
+ private readonly ageWarn;
50
+ private unsupportedLogged;
51
+ /** The account age the game's hook gave for a player, and when (see `accountFactor`). */
52
+ private readonly ageMemo;
53
+ constructor(deps: SignalServiceDeps);
54
+ /** Does the store keep signals (the three methods)? */
55
+ private get supported();
56
+ /** The store cannot keep signals: said once per kit, as the AI does for its own optional methods. */
57
+ private unsupported;
58
+ /** Record one signal. Synchronous, fenced, never throws: false for any refusal (logged by reason code, at most once a minute). */
59
+ record(input: SignalInput): boolean;
60
+ /** `record`'s body. `quietCapped`: the cap refusing is the designed answer (a second flood trip the same day), so it is not logged. */
61
+ private guarded;
62
+ flood(subject: SubjectId, roomId: string): void;
63
+ private recordRow;
64
+ private insert;
65
+ /** The canonical person a party names: a subject id the index knows, or a connection details object FOUND (never created). */
66
+ private party;
67
+ /**
68
+ * The new-account factor for `by`: the guard's factor only when the game supplied the hook, it returned an age for the player's
69
+ * account ids, and the age is below `belowDays`; else 1. A hook that throws is unknown (logged by class). Computed once, at record time.
70
+ */
71
+ private accountFactor;
72
+ /** A read that failed (a store fault) answers empty; it is said once a minute, by the error's class only. Never throws. */
73
+ private readFailed;
74
+ /** The declared kinds, built-ins included. */
75
+ kinds(): SignalKindView[];
76
+ /** The counts for a person (aliases folded): one entry per requested kind with a counted source. Never throws; a store failure reads []. */
77
+ counts(subject: SubjectId, opts?: {
78
+ windowDays?: number;
79
+ kinds?: readonly string[];
80
+ }): SignalCount[];
81
+ private windowOf;
82
+ /** The declared kinds (all, or those named, in declared order) as the counting context wants them. */
83
+ private kindsFor;
84
+ private countFor;
85
+ /**
86
+ * Who of these people meets `feeds.queue`, as canonical ids: one grouped store read for all of them. Empty (and no store read) when the
87
+ * feed is off. Never throws.
88
+ */
89
+ boosted(people: readonly SubjectId[]): Set<SubjectId>;
90
+ /**
91
+ * The ladder rules (`feeds.ladder`) this person meets, for `suggest`: each as its id, effect and the sentence ("Muted by other players:
92
+ * 5 players in 14 days"). Rows are read once per distinct window. `[]` (and no store read) when the feed is off. Never throws.
93
+ */
94
+ ladderHits(subject: SubjectId): SignalHit[];
95
+ /** The lines the Person page record shows: the `feeds.record` kinds and window. `[]` (and no store read) when the feed is off. Never throws. */
96
+ forRecord(subject: SubjectId): SignalCount[];
97
+ /** The counts the AI read may be shown: the `feeds.ai` kinds and window. `[]` (and no store read) when the feed is off. Never throws. */
98
+ forAi(subject: SubjectId): SignalCount[];
99
+ private forFeed;
100
+ }
101
+ //# sourceMappingURL=signalService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signalService.d.ts","sourceRoot":"","sources":["../../src/signal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