@chatwarden/core 0.1.5 → 0.1.7
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 +51 -1
- 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 +13 -0
- package/dist/src/ai/hash.d.ts.map +1 -0
- package/dist/src/ai/hash.js +19 -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 +44 -0
- package/dist/src/ai/prompt.d.ts.map +1 -0
- package/dist/src/ai/prompt.js +185 -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 +328 -0
- package/dist/src/ai/service.d.ts.map +1 -0
- package/dist/src/ai/service.js +749 -0
- package/dist/src/ai/service.js.map +1 -0
- package/dist/src/ai/types.d.ts +180 -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 +74 -0
- package/dist/src/ai/worker.d.ts.map +1 -0
- package/dist/src/ai/worker.js +286 -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/chatwarden.d.ts +28 -0
- package/dist/src/chatwarden.d.ts.map +1 -1
- package/dist/src/chatwarden.js +60 -1
- 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 +4 -1
- package/dist/src/documents.js.map +1 -1
- package/dist/src/index.d.ts +27 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +17 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/profile.d.ts +30 -1
- package/dist/src/profile.d.ts.map +1 -1
- package/dist/src/profile.js +43 -4
- package/dist/src/profile.js.map +1 -1
- package/dist/src/retention.d.ts +19 -0
- package/dist/src/retention.d.ts.map +1 -1
- package/dist/src/retention.js +33 -10
- package/dist/src/retention.js.map +1 -1
- package/dist/src/types.d.ts +269 -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 +20 -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 +208 -0
- package/src/ai/redact.ts +174 -0
- package/src/ai/schema.ts +51 -0
- package/src/ai/service.ts +955 -0
- package/src/ai/types.ts +165 -0
- package/src/ai/validate.ts +173 -0
- package/src/ai/worker.ts +364 -0
- package/src/audit.ts +31 -5
- package/src/chatwarden.ts +82 -1
- package/src/decide.ts +97 -4
- package/src/documents.ts +5 -2
- package/src/index.ts +52 -1
- package/src/profile.ts +68 -4
- package/src/retention.ts +47 -10
- package/src/types.ts +294 -2
package/README.md
CHANGED
|
@@ -111,7 +111,7 @@ For the moderators' screens and the player's notices, mount the routes in
|
|
|
111
111
|
| Part | What it is for |
|
|
112
112
|
| --- | --- |
|
|
113
113
|
| `cw.rooms` | `seat`, `seatConnection`, `unseat`, `close`: who is in which seat of which room. |
|
|
114
|
-
| `cw.chat.accept(room, seat, text)` | The answer for one line: `{ deliver }` or `{ refuse: { reason } }`. `visibleTo` applies a viewer's mutes. `restricted(who)` says whether a person is chat-restricted, for typing indicators. |
|
|
114
|
+
| `cw.chat.accept(room, seat, text)` | The answer for one line: `{ deliver }` or `{ refuse: { reason } }`. `visibleTo` applies a viewer's mutes. `restricted(who)` says whether a person is chat-restricted, for typing indicators; `restrictedBySubject(subject)` (`@experimental`) says the same for a subject id from `cw.rooms.subjectAt`, so a server that keeps a connection open need not keep the player's key to ask again. |
|
|
115
115
|
| `cw.mutes` | A player mutes another player for themselves only. |
|
|
116
116
|
| `cw.reports` | `submit` and `addDetail` (players; see "Reporting settings"), `list`, `get` and `reporterRecord` (staff). `reporterRecord(person)` is how the reports a person filed stand: `{ reports, open, actioned, dismissed, badFaith, suspendedUntil, reasonMatched, reasonDecided, accuracy }`. `reporterAccuracy(people)` is how often each person's recent reports turned out to be unfounded (see "Reporter accuracy"). |
|
|
117
117
|
| `cw.decide(input)` | A moderator closes a report and writes the sanctions and notices in one transaction. Safe to send twice. `reason` is required, except for a `no_action` with no actions (a one-click dismissal): leave it out and the standard reason `NO_ACTION_DEFAULT_REASON` (`No action needed`) is stored. |
|
|
@@ -276,6 +276,55 @@ const res = await cw.reports.addDetail(reportId, 'seat-1', { reason: 'cheating',
|
|
|
276
276
|
- **Your own store.** `addReportDetail` and `loadReporterDecisions` are optional on `Store`. Without the first, `addDetail` answers `off`; without the
|
|
277
277
|
second, the accuracy counts are 0. `insertReport` must keep `reportedReason`, and `listReports` must apply the `reason` filter (`null` means none).
|
|
278
278
|
|
|
279
|
+
## The AI (levels 1 and 2)
|
|
280
|
+
|
|
281
|
+
Optional help for the moderator, `@experimental` until 0.2 and new in 0.1.7. With an adapter, the kit reads each new report after the player's
|
|
282
|
+
ack, in the background, and the hub shows what it found (level 1, Read); at level 2 (Recommend) the decision form opens pre-filled with a mild
|
|
283
|
+
suggestion that the moderator accepts or edits. It is off until you give it an adapter, and the level is then the owner's to set in the hub
|
|
284
|
+
(Settings > AI): 0 by default, held to the `maxLevel` you pass.
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
import { createChatwarden, createFakeModerationAI } from '@chatwarden/core';
|
|
288
|
+
import { openStore } from '@chatwarden/store-sqlite';
|
|
289
|
+
|
|
290
|
+
// A scripted fake for a first look: it reads a report by simple rules, with no key and no network.
|
|
291
|
+
// A real adapter is any object with an `id` and a `read` function (the `ModerationAI` type).
|
|
292
|
+
const adapter = createFakeModerationAI([], { id: 'demo-fake' });
|
|
293
|
+
|
|
294
|
+
const cw = await createChatwarden({
|
|
295
|
+
store: openStore({ path: './chatwarden.db' }),
|
|
296
|
+
secret: process.env.CHATWARDEN_SECRET!,
|
|
297
|
+
platform,
|
|
298
|
+
ai: { adapter, maxLevel: 2, dailyBudgetMicros: 1_000_000 },
|
|
299
|
+
});
|
|
300
|
+
console.log(cw.ai.status()); // { available: true, level: 0, maxLevel: 2, state: 'ok', ... }
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
- **The adapter only sends.** The kit chooses and redacts the lines, renders the prompt, checks the model's answer against the rules and
|
|
304
|
+
the ceilings, and stores it. Your `read(input, { signal })` sends `input.prompt` and returns `{ output, usage }`; anything it throws must
|
|
305
|
+
be an `AIError` (`'declined'`, `'fatal'`, `'timeout'`, `'retryable'`). `examples/ai-anthropic` is a copy-and-own adapter for Anthropic, and
|
|
306
|
+
`docs/adoption/moxgate.md` has the steps for turning it on.
|
|
307
|
+
- **It advises; it never acts.** The AI never writes a sanction, a strike, a mute or a notice: nothing under `core/src/ai` imports the modules
|
|
308
|
+
that do (a lint rule and a test hold that). The one-key accept never fills in a ban, an area ban or anything longer than 24 hours
|
|
309
|
+
(`AI_MAX_PREFILL_MS`), and a suggestion to uphold or note needs a cited line by the reported person.
|
|
310
|
+
- **What leaves your server.** Only the chosen lines of the report with the speakers replaced by `P1`, `P2`, ..., the reporter's reason and
|
|
311
|
+
note (redacted), your Community Rules and the standard. Flags, mutes and report counts are never sent, nor any id, handle, IP, hash, room or
|
|
312
|
+
tag. A line the word filter blocked is sent as typed unless you set `blockedLines: 'mask'`: the model needs the words to judge intent.
|
|
313
|
+
- **What it costs.** `dailyBudgetMicros` (default $1.00 a UTC day) is set in code and a hub cannot raise it. The owner also sets a ceiling on
|
|
314
|
+
calls a day (`profile.ai.dailyCalls`, default 5 000), so an adapter that reports no cost still cannot run unbounded. There are at most 30 calls
|
|
315
|
+
a minute and 2 at once, a hard deadline per call (`timeoutMs`) and a breaker that opens after repeated failures. At a limit, reads pause. A call that a shutdown aborts books nothing, so a restart loop around a slow provider can under-count the day's money; the call ceiling still bounds it.
|
|
316
|
+
- **Learning.** With `profile.ai.learn` off no example is sent and no new example text is kept, but text already stored stays until its 180 days are up or
|
|
317
|
+
a senior marks the case "don't teach".
|
|
318
|
+
- **It cannot get in the way.** A failing, slow, refusing or garbage-returning provider changes nothing else: the player's ack does not wait for
|
|
319
|
+
the provider, the queue and the case page answer exactly as with the AI off, and a read that fails shows nothing.
|
|
320
|
+
- **Your store needs the AI methods.** The SQLite store has them (it adds three tables in migration 20, which is one-way: back up the database before upgrading); the in-memory store has them only
|
|
321
|
+
through `withMemoryAi(store)`. A store without them leaves the AI inert: `createChatwarden` logs `chatwarden.ai_store_unsupported` once and
|
|
322
|
+
carries on (a bad `ai` option still throws).
|
|
323
|
+
- **`cw.ai`** is always there: `level()`, `status()`, `read(reportId)`, `expected(reportId)` (whether a read of that report is on its way now: the level is 1 or 2, an adapter is set up, the breaker is closed, neither daily limit is reached and its read has not ended without an answer), `readAgain(reportId, actor)`, `stats()` (the casebook counts) and `idle()`.
|
|
324
|
+
Without an adapter it answers as if the AI were off. The kit loads the AI code only when you pass `ai`, so a bundler can leave it out of a
|
|
325
|
+
browser build that never does.
|
|
326
|
+
- **To try it with no key**, run `examples/tiny-chat` with `CW_DEMO_AI=1`; tests use `createFakeModerationAI` and never need a network.
|
|
327
|
+
|
|
279
328
|
## Typing indicators
|
|
280
329
|
|
|
281
330
|
Do not show a "typing..." indicator for a player who cannot chat. `cw.chat.restricted(who, { roomId, seatId })` runs the
|
|
@@ -347,6 +396,7 @@ in a browser.
|
|
|
347
396
|
| `logger` | `{ warn(event, detail?) }`. Defaults to `console.warn`. It only ever receives an event name and an error class. |
|
|
348
397
|
| `rules` | The numbered Community Rules, from `defineRules`. Defaults to the preset's starter set. |
|
|
349
398
|
| `autoRestrictRule` | The rule number the automatic slur filter cites (default 1). Must be an active rule. |
|
|
399
|
+
| `ai` | `{ adapter, maxLevel?, dailyBudgetMicros?, ... }`: turns on AI reads (levels 1 and 2). Off without it. See "The AI (levels 1 and 2)" below. |
|
|
350
400
|
|
|
351
401
|
Bad options throw at creation (`ProfileError`, `RangeError`), before the store is touched.
|
|
352
402
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { AiOutcomeDetail, AiVerdict, SanctionKind } from '../types.js';
|
|
2
|
+
import type { AIRead } from './types.js';
|
|
3
|
+
/** A step as a person would take it: a kind and a length (null for a warning, a note or a permanent one). */
|
|
4
|
+
export interface StepLike {
|
|
5
|
+
kind: SanctionKind;
|
|
6
|
+
durationMs: number | null;
|
|
7
|
+
}
|
|
8
|
+
/** What the human decided about the reported person, as the server sees it. */
|
|
9
|
+
export interface HumanDecision {
|
|
10
|
+
verdict: 'upheld' | 'noted' | 'no_action' | 'bad_faith';
|
|
11
|
+
/** The Community Rule the human cited against the reported person, or null (a dismissal). */
|
|
12
|
+
rule: number | 'uncovered' | null;
|
|
13
|
+
/** The ids of the reported person's lines the human cited. */
|
|
14
|
+
lineIds: readonly string[];
|
|
15
|
+
/** The strongest step the human took against the reported person, or null. */
|
|
16
|
+
step: StepLike | null;
|
|
17
|
+
/** The step the ladder gives for the AI's conduct level: what the pre-fill would have been; null when there was none. */
|
|
18
|
+
prefill: StepLike | null;
|
|
19
|
+
/** How many actions the decision took on people other than the reported person (the reporter, a bystander); absent means none. */
|
|
20
|
+
others?: number;
|
|
21
|
+
/** The client says the moderator accepted the pre-fill in one click. A claim: the server counts it only when the decision is the pre-fill (see {@link agreementOf}). */
|
|
22
|
+
oneClick: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** The result: the class of agreement, the AI's verdict in the casebook's three classes, and the finer facts. */
|
|
25
|
+
export interface Agreement {
|
|
26
|
+
agreement: 'agree' | 'override' | 'none';
|
|
27
|
+
/** What the AI recommended, or null when no recommendation was shown (level 1, `unclear`, or no read). */
|
|
28
|
+
aiVerdict: AiVerdict | null;
|
|
29
|
+
detail: AiOutcomeDetail;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Order two steps by how heavy they are: none, a note, a warning, a chat restriction, a ban from public rooms, a full ban; within a
|
|
33
|
+
* kind, by length (permanent last). Negative when `a` is milder than `b`, zero when equal, positive when `a` is harsher.
|
|
34
|
+
*/
|
|
35
|
+
export declare function compareSteps(a: StepLike | null, b: StepLike | null): number;
|
|
36
|
+
/**
|
|
37
|
+
* Compare the AI's recommendation in `read` with `decision`. `level` is the AI level in force: below 2 the recommendation was not
|
|
38
|
+
* shown, so there is nothing to agree with (`none`). `upheld` and `noted` are one class (the AI wanted to act, or to record);
|
|
39
|
+
* `no_action` is the other; a `bad_faith` verdict is always an override of a recommendation (the AI can never call a report bad faith).
|
|
40
|
+
* A rule that differs is not a disagreement: it is `rule: false` in the detail (and for a `no_action` the rule means nothing: `true`).
|
|
41
|
+
* `detail.oneClick` is the server's verdict, not the client's claim: true only when the client said so AND the decision is an
|
|
42
|
+
* agreement that equals the pre-fill (the same verdict, the same rule, the same kind of step and no longer) and the decision takes no
|
|
43
|
+
* action on anyone else: a decision that also sanctions the reporter or a bystander is the moderator's own, not the AI's one key.
|
|
44
|
+
*/
|
|
45
|
+
export declare function agreementOf(read: AIRead | null, decision: HumanDecision, level?: 0 | 1 | 2): Agreement;
|
|
46
|
+
//# sourceMappingURL=agreement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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C,GAAG,CAAK,GAAG,SAAS,CAwBzG"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const KIND_RANK = { note: 1, warning: 2, chat: 3, area: 4, ban: 5 };
|
|
2
|
+
/** How long a step lasts for ordering: no length for a note or warning, and a permanent restriction or ban beyond any length. */
|
|
3
|
+
function lengthOf(s) {
|
|
4
|
+
if (s.kind === 'note' || s.kind === 'warning')
|
|
5
|
+
return 0;
|
|
6
|
+
return s.durationMs === null ? Number.POSITIVE_INFINITY : s.durationMs;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Order two steps by how heavy they are: none, a note, a warning, a chat restriction, a ban from public rooms, a full ban; within a
|
|
10
|
+
* kind, by length (permanent last). Negative when `a` is milder than `b`, zero when equal, positive when `a` is harsher.
|
|
11
|
+
*/
|
|
12
|
+
export function compareSteps(a, b) {
|
|
13
|
+
const kindA = a === null ? 0 : (KIND_RANK[a.kind] ?? 0);
|
|
14
|
+
const kindB = b === null ? 0 : (KIND_RANK[b.kind] ?? 0);
|
|
15
|
+
if (kindA !== kindB)
|
|
16
|
+
return kindA < kindB ? -1 : 1;
|
|
17
|
+
if (a === null || b === null)
|
|
18
|
+
return 0;
|
|
19
|
+
const x = lengthOf(a);
|
|
20
|
+
const y = lengthOf(b);
|
|
21
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
22
|
+
}
|
|
23
|
+
const NO_DETAIL = { verdict: false, rule: false, lines: 0, outcome: 'none', oneClick: false };
|
|
24
|
+
/**
|
|
25
|
+
* The decision is the pre-fill as it stood: the same kind of step, no longer than the pre-fill (a moderator may trim a length and it is
|
|
26
|
+
* still their one click), or both without a step (a dismissal accepted as one).
|
|
27
|
+
*/
|
|
28
|
+
function stepAccepted(step, prefill) {
|
|
29
|
+
if (step === null || prefill === null)
|
|
30
|
+
return step === null && prefill === null;
|
|
31
|
+
if (step.kind !== prefill.kind)
|
|
32
|
+
return false;
|
|
33
|
+
if (step.durationMs === prefill.durationMs)
|
|
34
|
+
return true;
|
|
35
|
+
return step.durationMs !== null && prefill.durationMs !== null && step.durationMs <= prefill.durationMs;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Compare the AI's recommendation in `read` with `decision`. `level` is the AI level in force: below 2 the recommendation was not
|
|
39
|
+
* shown, so there is nothing to agree with (`none`). `upheld` and `noted` are one class (the AI wanted to act, or to record);
|
|
40
|
+
* `no_action` is the other; a `bad_faith` verdict is always an override of a recommendation (the AI can never call a report bad faith).
|
|
41
|
+
* A rule that differs is not a disagreement: it is `rule: false` in the detail (and for a `no_action` the rule means nothing: `true`).
|
|
42
|
+
* `detail.oneClick` is the server's verdict, not the client's claim: true only when the client said so AND the decision is an
|
|
43
|
+
* agreement that equals the pre-fill (the same verdict, the same rule, the same kind of step and no longer) and the decision takes no
|
|
44
|
+
* action on anyone else: a decision that also sanctions the reporter or a bystander is the moderator's own, not the AI's one key.
|
|
45
|
+
*/
|
|
46
|
+
export function agreementOf(read, decision, level = 2) {
|
|
47
|
+
const rec = read?.recommendation ?? null;
|
|
48
|
+
if (level < 2 || rec === null || rec.verdict === 'unclear') {
|
|
49
|
+
return { agreement: 'none', aiVerdict: null, detail: { ...NO_DETAIL } };
|
|
50
|
+
}
|
|
51
|
+
const aiVerdict = rec.verdict;
|
|
52
|
+
const aiActs = aiVerdict === 'upheld' || aiVerdict === 'noted';
|
|
53
|
+
const humanActs = decision.verdict === 'upheld' || decision.verdict === 'noted';
|
|
54
|
+
const same = decision.verdict !== 'bad_faith' && aiActs === humanActs;
|
|
55
|
+
const cited = new Set(rec.evidence);
|
|
56
|
+
const lines = new Set(decision.lineIds.filter((id) => cited.has(id))).size;
|
|
57
|
+
const cmp = compareSteps(decision.step, decision.prefill);
|
|
58
|
+
const ruleMatches = aiVerdict === 'no_action' || rec.rule === decision.rule;
|
|
59
|
+
return {
|
|
60
|
+
agreement: same ? 'agree' : 'override',
|
|
61
|
+
aiVerdict,
|
|
62
|
+
detail: {
|
|
63
|
+
verdict: same,
|
|
64
|
+
rule: ruleMatches,
|
|
65
|
+
lines,
|
|
66
|
+
outcome: cmp === 0 ? 'same' : cmp < 0 ? 'milder' : 'harsher',
|
|
67
|
+
oneClick: decision.oneClick && same && rec.verdict === decision.verdict && ruleMatches && (decision.others ?? 0) === 0 && stepAccepted(decision.step, decision.prefill),
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=agreement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agreement.js","sourceRoot":"","sources":["../../../src/ai/agreement.ts"],"names":[],"mappings":"AAqCA,MAAM,SAAS,GAAiC,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;AAElG,iIAAiI;AACjI,SAAS,QAAQ,CAAC,CAAW;IAC3B,IAAI,CAAC,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,CAAC,IAAI,KAAK,SAAS;QAAE,OAAO,CAAC,CAAC;IACxD,OAAO,CAAC,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;AACzE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,CAAkB,EAAE,CAAkB;IACjE,MAAM,KAAK,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACxD,MAAM,KAAK,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACxD,IAAI,KAAK,KAAK,KAAK;QAAE,OAA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C,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;YAC5D,QAAQ,EAAE,QAAQ,CAAC,QAAQ,IAAI,IAAI,IAAI,GAAG,CAAC,OAAO,KAAK,QAAQ,CAAC,OAAO,IAAI,WAAW,IAAI,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC;SACxK;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Consecutive failures that open the breaker. */
|
|
2
|
+
export declare const BREAKER_THRESHOLD = 5;
|
|
3
|
+
/** How long an open breaker refuses calls before one probe is let through. */
|
|
4
|
+
export declare const BREAKER_OPEN_MS: number;
|
|
5
|
+
/** Whether a call may be made now: freely, as the one probe, or not at all. */
|
|
6
|
+
export type BreakerGate = 'go' | 'probe' | 'no';
|
|
7
|
+
/** What the hub shows: `closed` is working, `open` and `half_open` are "unavailable". */
|
|
8
|
+
export type BreakerState = 'closed' | 'open' | 'half_open';
|
|
9
|
+
export declare class Breaker {
|
|
10
|
+
private failures;
|
|
11
|
+
private openUntil;
|
|
12
|
+
private probing;
|
|
13
|
+
private readonly clock;
|
|
14
|
+
constructor(clock: {
|
|
15
|
+
now(): number;
|
|
16
|
+
});
|
|
17
|
+
/** Ask to make a call. A `probe` is handed out once; report its end with {@link success}, {@link failure} or {@link release}. */
|
|
18
|
+
gate(): BreakerGate;
|
|
19
|
+
/** Whether a call could go out now: closed, or half-open with the probe free. Takes nothing (see {@link gate}). */
|
|
20
|
+
available(): boolean;
|
|
21
|
+
state(): BreakerState;
|
|
22
|
+
/** When an open breaker lets the probe through (epoch ms), or null when closed. */
|
|
23
|
+
reopensAt(): number | null;
|
|
24
|
+
/** A call produced an answer (even a refusal or an unusable one). Closes the breaker. */
|
|
25
|
+
success(): void;
|
|
26
|
+
/** A call failed. `fatal` opens at once; so does a failed probe and the fifth failure in a row. */
|
|
27
|
+
failure(fatal?: boolean): void;
|
|
28
|
+
/** A probe was handed out but no call came of it (for example the budget stopped it): let the next ask be the probe. */
|
|
29
|
+
release(): void;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=breaker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"breaker.d.ts","sourceRoot":"","sources":["../../../src/ai/breaker.ts"],"names":[],"mappings":"AAIA,kDAAkD;AAClD,eAAO,MAAM,iBAAiB,IAAI,CAAC;AACnC,8EAA8E;AAC9E,eAAO,MAAM,eAAe,QAAa,CAAC;AAE1C,+EAA+E;AAC/E,MAAM,MAAM,WAAW,GAAG,IAAI,GAAG,OAAO,GAAG,IAAI,CAAC;AAEhD,yFAAyF;AACzF,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAE3D,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,OAAO,CAAS;IAExB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAoB;gBAE9B,KAAK,EAAE;QAAE,GAAG,IAAI,MAAM,CAAA;KAAE;IAIpC,iIAAiI;IACjI,IAAI,IAAI,WAAW;IAQnB,mHAAmH;IACnH,SAAS,IAAI,OAAO;IAKpB,KAAK,IAAI,YAAY;IAKrB,mFAAmF;IACnF,SAAS,IAAI,MAAM,GAAG,IAAI;IAI1B,yFAAyF;IACzF,OAAO,IAAI,IAAI;IAMf,mGAAmG;IACnG,OAAO,CAAC,KAAK,UAAQ,GAAG,IAAI;IAS5B,wHAAwH;IACxH,OAAO,IAAI,IAAI;CAGhB"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// The breaker (design section 4, "A breaker"). Five consecutive failures, or any `fatal`, stop new calls for five minutes; then
|
|
2
|
+
// ONE probe call decides: its success closes the breaker, its failure opens it for another five minutes. While it is open nothing is
|
|
3
|
+
// called. A refusal (`declined`) or an unusable answer (`invalid`) is an answer, so it counts as a success here: the provider is up.
|
|
4
|
+
/** Consecutive failures that open the breaker. */
|
|
5
|
+
export const BREAKER_THRESHOLD = 5;
|
|
6
|
+
/** How long an open breaker refuses calls before one probe is let through. */
|
|
7
|
+
export const BREAKER_OPEN_MS = 5 * 60_000;
|
|
8
|
+
export class Breaker {
|
|
9
|
+
failures = 0;
|
|
10
|
+
openUntil = null;
|
|
11
|
+
probing = false;
|
|
12
|
+
clock;
|
|
13
|
+
constructor(clock) {
|
|
14
|
+
this.clock = clock;
|
|
15
|
+
}
|
|
16
|
+
/** Ask to make a call. A `probe` is handed out once; report its end with {@link success}, {@link failure} or {@link release}. */
|
|
17
|
+
gate() {
|
|
18
|
+
if (this.openUntil === null)
|
|
19
|
+
return 'go';
|
|
20
|
+
if (this.clock.now() < this.openUntil)
|
|
21
|
+
return 'no';
|
|
22
|
+
if (this.probing)
|
|
23
|
+
return 'no';
|
|
24
|
+
this.probing = true;
|
|
25
|
+
return 'probe';
|
|
26
|
+
}
|
|
27
|
+
/** Whether a call could go out now: closed, or half-open with the probe free. Takes nothing (see {@link gate}). */
|
|
28
|
+
available() {
|
|
29
|
+
if (this.openUntil === null)
|
|
30
|
+
return true;
|
|
31
|
+
return this.clock.now() >= this.openUntil && !this.probing;
|
|
32
|
+
}
|
|
33
|
+
state() {
|
|
34
|
+
if (this.openUntil === null)
|
|
35
|
+
return 'closed';
|
|
36
|
+
return this.clock.now() < this.openUntil ? 'open' : 'half_open';
|
|
37
|
+
}
|
|
38
|
+
/** When an open breaker lets the probe through (epoch ms), or null when closed. */
|
|
39
|
+
reopensAt() {
|
|
40
|
+
return this.openUntil;
|
|
41
|
+
}
|
|
42
|
+
/** A call produced an answer (even a refusal or an unusable one). Closes the breaker. */
|
|
43
|
+
success() {
|
|
44
|
+
this.failures = 0;
|
|
45
|
+
this.openUntil = null;
|
|
46
|
+
this.probing = false;
|
|
47
|
+
}
|
|
48
|
+
/** A call failed. `fatal` opens at once; so does a failed probe and the fifth failure in a row. */
|
|
49
|
+
failure(fatal = false) {
|
|
50
|
+
this.failures += 1;
|
|
51
|
+
if (fatal || this.probing || this.failures >= BREAKER_THRESHOLD) {
|
|
52
|
+
this.openUntil = this.clock.now() + BREAKER_OPEN_MS;
|
|
53
|
+
this.failures = 0;
|
|
54
|
+
}
|
|
55
|
+
this.probing = false;
|
|
56
|
+
}
|
|
57
|
+
/** A probe was handed out but no call came of it (for example the budget stopped it): let the next ask be the probe. */
|
|
58
|
+
release() {
|
|
59
|
+
this.probing = false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=breaker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"breaker.js","sourceRoot":"","sources":["../../../src/ai/breaker.ts"],"names":[],"mappings":"AAAA,gIAAgI;AAChI,qIAAqI;AACrI,qIAAqI;AAErI,kDAAkD;AAClD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC;AACnC,8EAA8E;AAC9E,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,GAAG,MAAM,CAAC;AAQ1C,MAAM,OAAO,OAAO;IACV,QAAQ,GAAG,CAAC,CAAC;IACb,SAAS,GAAkB,IAAI,CAAC;IAChC,OAAO,GAAG,KAAK,CAAC;IAEP,KAAK,CAAoB;IAE1C,YAAY,KAAwB;QAClC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,iIAAiI;IACjI,IAAI;QACF,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC;QACzC,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,SAAS;YAAE,OAAO,IAAI,CAAC;QACnD,IAAI,IAAI,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC;QAC9B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,OAAO,OAAO,CAAC;IACj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UtcDay(ms: number): number;
|
|
3
|
+
/** Epoch ms of the next 00:00 UTC after `ms`: when a day's cap resets. */
|
|
4
|
+
export declare function nextUtcMidnight(ms: number): number;
|
|
5
|
+
/** The UTC day as `YYYY-MM-DD`. */
|
|
6
|
+
export declare function utcDayLabel(ms: number): string;
|
|
7
|
+
/**
|
|
8
|
+
* At most `limit` calls in any `windowMs` (the per-minute cap). In-memory: it guards a burst in this process; the daily spend cap is
|
|
9
|
+
* what the store shares.
|
|
10
|
+
*/
|
|
11
|
+
export declare class CallWindow {
|
|
12
|
+
private readonly at;
|
|
13
|
+
private readonly limit;
|
|
14
|
+
private readonly windowMs;
|
|
15
|
+
constructor(limit: number, windowMs?: number);
|
|
16
|
+
/** Take a slot at `now`; false (nothing taken) when the window is full. */
|
|
17
|
+
tryTake(now: number): boolean;
|
|
18
|
+
/** Whether a slot is free at `now`, taking none. */
|
|
19
|
+
peek(now: number): boolean;
|
|
20
|
+
/** When a slot is free: `now` if one is, else the moment the oldest call leaves the window. */
|
|
21
|
+
nextFree(now: number): number;
|
|
22
|
+
/** Give back the slot taken at `now` (the call was not made after all). */
|
|
23
|
+
giveBack(now: number): void;
|
|
24
|
+
private trim;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=budget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.d.ts","sourceRoot":"","sources":["../../../src/ai/budget.ts"],"names":[],"mappings":"AAKA,wDAAwD;AACxD,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,0EAA0E;AAC1E,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,mCAAmC;AACnC,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED;;;GAGG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAgB;IAEnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;gBAEtB,KAAK,EAAE,MAAM,EAAE,QAAQ,SAAS;IAK5C,2EAA2E;IAC3E,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAO7B,oDAAoD;IACpD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAK1B,+FAA+F;IAC/F,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAK7B,2EAA2E;IAC3E,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAK3B,OAAO,CAAC,IAAI;CAGb"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// The spend cap and the call-rate cap (design section 4). The cap is over the UTC day and computed from the stored spend, so it
|
|
2
|
+
// survives a restart and is shared by every process on the store; the helpers here only name the day.
|
|
3
|
+
const DAY_MS = 24 * 3_600_000;
|
|
4
|
+
/** Epoch ms of 00:00 UTC on the day that holds `ms`. */
|
|
5
|
+
export function startOfUtcDay(ms) {
|
|
6
|
+
return Math.floor(ms / DAY_MS) * DAY_MS;
|
|
7
|
+
}
|
|
8
|
+
/** Epoch ms of the next 00:00 UTC after `ms`: when a day's cap resets. */
|
|
9
|
+
export function nextUtcMidnight(ms) {
|
|
10
|
+
return startOfUtcDay(ms) + DAY_MS;
|
|
11
|
+
}
|
|
12
|
+
/** The UTC day as `YYYY-MM-DD`. */
|
|
13
|
+
export function utcDayLabel(ms) {
|
|
14
|
+
return new Date(startOfUtcDay(ms)).toISOString().slice(0, 10);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* At most `limit` calls in any `windowMs` (the per-minute cap). In-memory: it guards a burst in this process; the daily spend cap is
|
|
18
|
+
* what the store shares.
|
|
19
|
+
*/
|
|
20
|
+
export class CallWindow {
|
|
21
|
+
at = [];
|
|
22
|
+
limit;
|
|
23
|
+
windowMs;
|
|
24
|
+
constructor(limit, windowMs = 60_000) {
|
|
25
|
+
this.limit = limit;
|
|
26
|
+
this.windowMs = windowMs;
|
|
27
|
+
}
|
|
28
|
+
/** Take a slot at `now`; false (nothing taken) when the window is full. */
|
|
29
|
+
tryTake(now) {
|
|
30
|
+
this.trim(now);
|
|
31
|
+
if (this.at.length >= this.limit)
|
|
32
|
+
return false;
|
|
33
|
+
this.at.push(now);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
/** Whether a slot is free at `now`, taking none. */
|
|
37
|
+
peek(now) {
|
|
38
|
+
this.trim(now);
|
|
39
|
+
return this.at.length < this.limit;
|
|
40
|
+
}
|
|
41
|
+
/** When a slot is free: `now` if one is, else the moment the oldest call leaves the window. */
|
|
42
|
+
nextFree(now) {
|
|
43
|
+
this.trim(now);
|
|
44
|
+
return this.at.length < this.limit ? now : this.at[0] + this.windowMs;
|
|
45
|
+
}
|
|
46
|
+
/** Give back the slot taken at `now` (the call was not made after all). */
|
|
47
|
+
giveBack(now) {
|
|
48
|
+
const i = this.at.lastIndexOf(now);
|
|
49
|
+
if (i >= 0)
|
|
50
|
+
this.at.splice(i, 1);
|
|
51
|
+
}
|
|
52
|
+
trim(now) {
|
|
53
|
+
while (this.at.length > 0 && this.at[0] <= now - this.windowMs)
|
|
54
|
+
this.at.shift();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.js","sourceRoot":"","sources":["../../../src/ai/budget.ts"],"names":[],"mappings":"AAAA,gIAAgI;AAChI,sGAAsG;AAEtG,MAAM,MAAM,GAAG,EAAE,GAAG,SAAS,CAAC;AAE9B,wDAAwD;AACxD,MAAM,UAAU,aAAa,CAAC,EAAU;IACtC,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC;AAC1C,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,eAAe,CAAC,EAAU;IACxC,OAAO,a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C,GAAW;QAClB,MAAM,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC;YAAE,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACnC,CAAC;IAEO,IAAI,CAAC,GAAW;QACtB,OAAO,IAAI,CAAC,EAAE,CAAC,MAAM,GAAG,CAAC,IAAK,IAAI,CAAC,EAAE,CAAC,CAAC,CAAY,IAAI,GAAG,GAAG,IAAI,CAAC,QAAQ;YAAE,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IAC9F,CAAC;CACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { DocumentPolicy } from '../documents.js';
|
|
2
|
+
import type { ReportReason } from '../reporting.js';
|
|
3
|
+
import type { ReportRecord } from '../types.js';
|
|
4
|
+
import type { ChosenLines } from './lines.js';
|
|
5
|
+
import type { AIExampleInput, AIReadInput } from './types.js';
|
|
6
|
+
/** A report ready to be read (or to be compared with a stored read). */
|
|
7
|
+
export interface Prepared {
|
|
8
|
+
/** The input without casebook examples: what the hash is of. */
|
|
9
|
+
input: AIReadInput;
|
|
10
|
+
chosen: ChosenLines;
|
|
11
|
+
/** `inputHash(input, adapterId)`. */
|
|
12
|
+
hash: string;
|
|
13
|
+
/** A line or the note looks like an instruction to the AI (the tripwire). */
|
|
14
|
+
injection: boolean;
|
|
15
|
+
/** Every line ref that is in the prompt. */
|
|
16
|
+
refs: ReadonlySet<string>;
|
|
17
|
+
/** The numbers of the rules that were active when the prompt was built. */
|
|
18
|
+
activeRules: ReadonlySet<number>;
|
|
19
|
+
/** The refs of the lines the reported person wrote. */
|
|
20
|
+
ownRefs: ReadonlySet<string>;
|
|
21
|
+
/** The real line id behind each ref. Never handed to an adapter. */
|
|
22
|
+
idOf: ReadonlyMap<string, string>;
|
|
23
|
+
/** The real ids of the lines the reported person wrote (among the chosen). */
|
|
24
|
+
ownLineIds: ReadonlySet<string>;
|
|
25
|
+
/** The same input with casebook examples in it (at most two are used). The hash does not change. */
|
|
26
|
+
build(examples: readonly AIExampleInput[]): AIReadInput;
|
|
27
|
+
}
|
|
28
|
+
export interface PrepareArgs {
|
|
29
|
+
report: ReportRecord;
|
|
30
|
+
policy: DocumentPolicy;
|
|
31
|
+
reasons: readonly ReportReason[];
|
|
32
|
+
adapterId: string;
|
|
33
|
+
/** `blockedLines: 'mask'`: send a line the filter touched masked, not raw. */
|
|
34
|
+
mask: boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare function prepareCase(args: PrepareArgs): Prepared;
|
|
37
|
+
//# sourceMappingURL=case.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"case.d.ts","sourceRoot":"","sources":["../../../src/ai/case.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAIhD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAM9D,wEAAwE;AACxE,MAAM,WAAW,QAAQ;IACvB,gEAAgE;IAChE,KAAK,EAAE,WAAW,CAAC;IACnB,MAAM,EAAE,WAAW,CAAC;IACpB,qCAAqC;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,6EAA6E;IAC7E,SAAS,EAAE,OAAO,CAAC;IACnB,4CAA4C;IAC5C,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC1B,2EAA2E;IAC3E,WAAW,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACjC,uDAAuD;IACvD,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC7B,oEAAoE;IACpE,IAAI,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,8EAA8E;IAC9E,UAAU,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAChC,oGAAoG;IACpG,KAAK,CAAC,QAAQ,EAAE,SAAS,cAAc,EAAE,GAAG,WAAW,CAAC;CACzD;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,YAAY,CAAC;IACrB,MAAM,EAAE,cAAc,CAAC;IACvB,OAAO,EAAE,SAAS,YAAY,EAAE,CAAC;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,8EAA8E;IAC9E,IAAI,EAAE,OAAO,CAAC;CACf;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,WAAW,GAAG,QAAQ,CA0BvD"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { reasonLabel } from '../reporting.js';
|
|
2
|
+
import { inputHash } from './hash.js';
|
|
3
|
+
import { looksLikeInjection } from './injection.js';
|
|
4
|
+
import { chooseLines } from './lines.js';
|
|
5
|
+
import { buildAIInput } from './prompt.js';
|
|
6
|
+
export function prepareCase(args) {
|
|
7
|
+
const { report, policy } = args;
|
|
8
|
+
const chosen = chooseLines(report, { mask: args.mask });
|
|
9
|
+
const reasonId = report.reportedReason ?? null;
|
|
10
|
+
const reason = reasonId === null ? null : { id: reasonId, label: reasonLabel(args.reasons, reasonId) ?? reasonId };
|
|
11
|
+
const build = (examples) => buildAIInput({ policy, reason, chosen, examples });
|
|
12
|
+
const input = build([]);
|
|
13
|
+
// The tripwire reads what the players typed (the raw text), whatever is sent.
|
|
14
|
+
const rawOf = new Map(report.excerpt.map((l) => [l.lineId, l.raw]));
|
|
15
|
+
const injection = (report.note !== null && looksLikeInjection(report.note)) ||
|
|
16
|
+
chosen.lines.some((l) => looksLikeInjection(rawOf.get(l.lineId) ?? l.text));
|
|
17
|
+
return {
|
|
18
|
+
input,
|
|
19
|
+
chosen,
|
|
20
|
+
hash: inputHash(input, args.adapterId),
|
|
21
|
+
injection,
|
|
22
|
+
refs: new Set(chosen.lines.map((l) => l.ref)),
|
|
23
|
+
activeRules: new Set(input.rules.map((r) => r.number)),
|
|
24
|
+
ownRefs: new Set(chosen.lines.filter((l) => l.byReported).map((l) => l.ref)),
|
|
25
|
+
idOf: new Map(chosen.lines.map((l) => [l.ref, l.lineId])),
|
|
26
|
+
ownLineIds: new Set(chosen.lines.filter((l) => l.byReported).map((l) => l.lineId)),
|
|
27
|
+
build,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=case.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"case.js","sourceRoot":"","sources":["../../../src/ai/case.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG9C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAuC3C,MAAM,UAAU,WAAW,CAAC,IAAiB;IAC3C,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAChC,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IACxD,MAAM,QAAQ,GAAG,MAAM,CAAC,cAAc,IAAI,IAAI,CAAC;IAC/C,MAAM,MAAM,GAAG,QAAQ,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,WAAW,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,QAAQ,EAAE,CAAC;IACnH,MAAM,KAAK,GAAG,CAAC,QAAmC,EAAe,EA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GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;QACzD,UAAU,EAAE,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QAClF,KAAK;KACN,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { RuleRef } from '../rules.js';
|
|
2
|
+
import type { AiOutcomeInsert, ConductLevel, ReportRecord, SanctionKind, SubjectId } from '../types.js';
|
|
3
|
+
import type { StepLike } from './agreement.js';
|
|
4
|
+
import type { ChosenLines } from './lines.js';
|
|
5
|
+
/** How many teachable decisions must exist before any example is shown to the model. */
|
|
6
|
+
export declare const AI_EXAMPLES_MIN = 30;
|
|
7
|
+
/** The most lines in one example. */
|
|
8
|
+
export declare const EXAMPLE_LINES_MAX = 6;
|
|
9
|
+
/**
|
|
10
|
+
* Up to six lines of the case, as aliases and redacted text: first the lines the human or the AI cited, then the rest of what the
|
|
11
|
+
* reported person wrote, then the other lines, the newest first within each group; shown in time order. The text goes through the
|
|
12
|
+
* same redaction as a read (it is idempotent on text that already went through it). No handle, seat, id or line id is returned.
|
|
13
|
+
*/
|
|
14
|
+
export declare function pickExampleLines(chosen: ChosenLines, cited: ReadonlySet<string>): Array<{
|
|
15
|
+
who: string;
|
|
16
|
+
text: string;
|
|
17
|
+
}>;
|
|
18
|
+
/** One action the decision took, as the casebook needs it. */
|
|
19
|
+
export interface TakenAction {
|
|
20
|
+
subject: string;
|
|
21
|
+
kind: SanctionKind;
|
|
22
|
+
durationMs: number | null;
|
|
23
|
+
rule: number | 'uncovered';
|
|
24
|
+
lineIds: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* What the human did to the reported person: their strongest step, the rule it cited and the lines they showed. A decision that
|
|
28
|
+
* acted on nobody but others (the reporter, a bystander) did nothing to the reported person; `others` counts the actions on those others.
|
|
29
|
+
*/
|
|
30
|
+
export declare function actionsOnTarget(actions: readonly TakenAction[], target: string): {
|
|
31
|
+
step: StepLike | null;
|
|
32
|
+
rule: number | 'uncovered' | null;
|
|
33
|
+
lineIds: string[];
|
|
34
|
+
others: number;
|
|
35
|
+
};
|
|
36
|
+
/** What the casebook is told about a decision that is about to commit (see `DecisionServiceDeps.aiOutcome`). */
|
|
37
|
+
export interface AiOutcomeArgs {
|
|
38
|
+
report: ReportRecord;
|
|
39
|
+
/** The canonical subject the report is about. */
|
|
40
|
+
target: SubjectId;
|
|
41
|
+
verdict: 'upheld' | 'noted' | 'no_action' | 'bad_faith';
|
|
42
|
+
actions: ReadonlyArray<{
|
|
43
|
+
subject: SubjectId;
|
|
44
|
+
kind: SanctionKind;
|
|
45
|
+
durationMs: number | null;
|
|
46
|
+
rule: RuleRef;
|
|
47
|
+
lineIds: readonly string[];
|
|
48
|
+
}>;
|
|
49
|
+
/** The echo of the decision, or all-empty (`readId` null, not accepted, no severity) when it carried none. */
|
|
50
|
+
ai: {
|
|
51
|
+
readId: string | null;
|
|
52
|
+
accepted: boolean;
|
|
53
|
+
severity: ConductLevel | null;
|
|
54
|
+
};
|
|
55
|
+
now: number;
|
|
56
|
+
}
|
|
57
|
+
/** Computes the casebook row for a decision, from the stored read and the decision. Returns null for none. */
|
|
58
|
+
export type AiOutcomeHook = (args: AiOutcomeArgs) => AiOutcomeInsert | null;
|
|
59
|
+
//# sourceMappingURL=casebook.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"casebook.d.ts","sourceRoot":"","sources":["../../../src/ai/casebook.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAA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AE,aAAa,CAAC;QAAE,OAAO,EAAE,SAAS,CAAC;QAAC,IAAI,EAAE,YAAY,CAAC;QAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,IAAI,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;IACzI,8GAA8G;IAC9G,EAAE,EAAE;QAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAC;QAAC,QAAQ,EAAE,YAAY,GAAG,IAAI,CAAA;KAAE,CAAC;IAChF,GAAG,EAAE,MAAM,CAAC;CACb;AAED,8GAA8G;AAC9G,MAAM,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,aAAa,KAAK,eAAe,GAAG,IAAI,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { compareSteps } from './agreement.js';
|
|
2
|
+
import { redactText } from './redact.js';
|
|
3
|
+
// The casebook's pure parts (design section 11): which lines of a decided case become a pseudonymised example, and what the human
|
|
4
|
+
// did to the reported person. The service puts them together with the store; nothing here reads a store or acts on anyone.
|
|
5
|
+
/** How many teachable decisions must exist before any example is shown to the model. */
|
|
6
|
+
export const AI_EXAMPLES_MIN = 30;
|
|
7
|
+
/** The most lines in one example. */
|
|
8
|
+
export const EXAMPLE_LINES_MAX = 6;
|
|
9
|
+
/**
|
|
10
|
+
* Up to six lines of the case, as aliases and redacted text: first the lines the human or the AI cited, then the rest of what the
|
|
11
|
+
* reported person wrote, then the other lines, the newest first within each group; shown in time order. The text goes through the
|
|
12
|
+
* same redaction as a read (it is idempotent on text that already went through it). No handle, seat, id or line id is returned.
|
|
13
|
+
*/
|
|
14
|
+
export function pickExampleLines(chosen, cited) {
|
|
15
|
+
const lines = chosen.lines;
|
|
16
|
+
const ranked = lines
|
|
17
|
+
.map((l, i) => ({ i, rank: (cited.has(l.lineId) ? 0 : l.byReported ? 1 : 2) * 1000 + (lines.length - i) }))
|
|
18
|
+
.sort((a, b) => a.rank - b.rank)
|
|
19
|
+
.slice(0, EXAMPLE_LINES_MAX)
|
|
20
|
+
.map((e) => e.i)
|
|
21
|
+
.sort((a, b) => a - b);
|
|
22
|
+
return ranked.map((i) => {
|
|
23
|
+
const line = lines[i];
|
|
24
|
+
return { who: line.who, text: redactText(line.text, [], () => null) };
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* What the human did to the reported person: their strongest step, the rule it cited and the lines they showed. A decision that
|
|
29
|
+
* acted on nobody but others (the reporter, a bystander) did nothing to the reported person; `others` counts the actions on those others.
|
|
30
|
+
*/
|
|
31
|
+
export function actionsOnTarget(actions, target) {
|
|
32
|
+
const own = actions.filter((a) => a.subject === target);
|
|
33
|
+
const others = actions.length - own.length;
|
|
34
|
+
if (own.length === 0)
|
|
35
|
+
return { step: null, rule: null, lineIds: [], others };
|
|
36
|
+
let strongest = own[0];
|
|
37
|
+
for (const a of own)
|
|
38
|
+
if (compareSteps(a, strongest) > 0)
|
|
39
|
+
strongest = a;
|
|
40
|
+
return {
|
|
41
|
+
step: { kind: strongest.kind, durationMs: strongest.durationMs },
|
|
42
|
+
rule: strongest.rule,
|
|
43
|
+
lineIds: [...new Set(own.flatMap((a) => a.lineIds))],
|
|
44
|
+
others,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=casebook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"casebook.js","sourceRoot":"","sources":["../../../src/ai/casebook.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,kIAAkI;AAClI,2HAA2H;AAE3H,wFAAwF;AACxF,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAC;AAClC,qCAAqC;AACrC,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,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