@deliverd/sdk 0.38.0 → 0.39.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deliverd/sdk",
3
- "version": "0.38.0",
3
+ "version": "0.39.1",
4
4
  "description": "Add human approval to any AI agent. Ask a person, wait for the answer, act on it. Start free at deliverd.dev.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -69,6 +69,22 @@ export type {
69
69
  MuseToolMessage,
70
70
  MuseToolRunner,
71
71
  } from "./muse-spark.js";
72
+ export {
73
+ answerPerplexityApprovals,
74
+ runPerplexityFunctionCalls,
75
+ runPerplexityWithDeliverd,
76
+ } from "./perplexity.js";
77
+ export type {
78
+ PerplexityApprovalOptions,
79
+ PerplexityApprovalRequest,
80
+ PerplexityApprovalResponse,
81
+ PerplexityApprovals,
82
+ PerplexityClient,
83
+ PerplexityFunctionCall,
84
+ PerplexityFunctionCallOutput,
85
+ PerplexityResponse,
86
+ PerplexityRunOptions,
87
+ } from "./perplexity.js";
72
88
  export { deliverdAllow, deliverdWebMcpAllow, describeStep, uiSdkApprovalRoute, UI_SDK_DEFAULT_TIMEOUT } from "./ui-sdk.js";
73
89
  export type { DeliverdAllowOptions, UiSdkApprovalAnswer, UiSdkApprovalRouteOptions, UiSdkAskContext, UiSdkStep } from "./ui-sdk.js";
74
90
  export { defaultDescription, DEFAULT_TOOL_EXPIRY } from "./shared.js";
@@ -109,12 +109,18 @@ export async function runMuseToolCalls(
109
109
  return results;
110
110
  }
111
111
 
112
- async function approveAndRun(
112
+ /**
113
+ * Ask about one call when `when` selects it, and run it only on a yes. Shared
114
+ * with the other Responses-API-shaped adapters (Perplexity's Agent API), each
115
+ * keying its requests with its own prefix. Not exported from the adapters entry.
116
+ */
117
+ export async function approveAndRun(
113
118
  toolName: string,
114
119
  rawArguments: string,
115
120
  callId: string,
116
121
  run: MuseToolRunner,
117
- options: MuseToolApprovalOptions
122
+ options: MuseToolApprovalOptions,
123
+ prefix = "muse-spark:"
118
124
  ): Promise<string> {
119
125
  const input = parseArguments(rawArguments);
120
126
  if (!options.when || (await options.when(toolName, input))) {
@@ -124,7 +130,7 @@ async function approveAndRun(
124
130
  : undefined;
125
131
  // The call id makes the ask idempotent: a loop that is retried, or a
126
132
  // process restarted mid-wait, finds the request it already made.
127
- const outcome = await askAboutCall(toolName, input, `muse-spark:${callId}`, options, undefined, editable);
133
+ const outcome = await askAboutCall(toolName, input, `${prefix}${callId}`, options, undefined, editable);
128
134
  if (!outcome.allowed) return outcome.reason;
129
135
  // What was approved runs, which is the proposal unless a person corrected it.
130
136
  return render(await run(toolName, outcome.input ?? input));
@@ -0,0 +1,236 @@
1
+ import { approveAndRun, type MuseToolApprovalOptions, type MuseToolRunner } from "./muse-spark.js";
2
+ import { askAboutCall } from "./shared.js";
3
+
4
+ /**
5
+ * Perplexity's Agent API: a person decides the tool calls a run asks to make.
6
+ *
7
+ * The Agent API (`POST https://api.perplexity.ai/v1/agent`, also served at
8
+ * `/v1/responses` for the OpenAI SDK) runs the agent loop itself: search,
9
+ * fetch, any model through one key, remote MCP servers and the connectors a
10
+ * Project administrator set up. It stops for a person in two places, and this
11
+ * answers both:
12
+ *
13
+ * - **MCP servers and connectors with `require_approval`.** The call does not
14
+ * run; the response's `output` carries an `mcp_approval_request` instead,
15
+ * and the next request answers it with an `mcp_approval_response`. Perplexity
16
+ * runs MCP and connector calls without asking unless `require_approval` is
17
+ * set, unlike OpenAI's default, so set it on every server that can change
18
+ * something.
19
+ * - **Your own functions.** A `function_call` in `output` is a call your code
20
+ * makes, so your code is where a person comes in: only approved calls run,
21
+ * and a declined one answers the model with the person's reason.
22
+ *
23
+ * ```ts
24
+ * import Perplexity from "@perplexity-ai/perplexity_ai";
25
+ * import { Deliverd } from "@deliverd/sdk";
26
+ * import { runPerplexityWithDeliverd } from "@deliverd/sdk/adapters";
27
+ *
28
+ * const response = await runPerplexityWithDeliverd(new Perplexity(), {
29
+ * model: "openai/gpt-5.6-sol",
30
+ * input: "Close the stale issues in our tracker",
31
+ * tools: [{ type: "connector", id: "connector_linear", server_label: "linear", require_approval: "always" }],
32
+ * }, { deliverd: new Deliverd(), approvers: ["platform-team"] });
33
+ * ```
34
+ *
35
+ * Every call is keyed by its id (`perplexity:<id>`), so a retried round, or a
36
+ * process restarted mid-wait, finds the request it already made. Shapes are
37
+ * from Perplexity's Agent API reference (MCP tool, connectors, custom
38
+ * functions); nothing from `@perplexity-ai/perplexity_ai` is imported.
39
+ */
40
+
41
+ const PREFIX = "perplexity:";
42
+
43
+ /** An MCP or connector call waiting for an answer. It has not run. */
44
+ export interface PerplexityApprovalRequest {
45
+ type: "mcp_approval_request";
46
+ id: string;
47
+ name: string;
48
+ /** JSON-encoded on the wire. */
49
+ arguments?: string;
50
+ server_label?: string;
51
+ /** Set when the call came from a connector set up in the API Console. */
52
+ connector_id?: string | null;
53
+ }
54
+
55
+ /** The answer to one `mcp_approval_request`, sent as an `input` item of the next request. */
56
+ export interface PerplexityApprovalResponse {
57
+ type: "mcp_approval_response";
58
+ approval_request_id: string;
59
+ approve: boolean;
60
+ /** Read by the model when a call was declined. */
61
+ reason?: string;
62
+ }
63
+
64
+ export interface PerplexityApprovalOptions extends MuseToolApprovalOptions {
65
+ /** Put the person's reason on a declined answer, where the model reads it. Default true. */
66
+ sendReason?: boolean;
67
+ }
68
+
69
+ export interface PerplexityApprovals {
70
+ /** One answer per request, in the order they were asked: the next request's `input`. */
71
+ responses: PerplexityApprovalResponse[];
72
+ /** Why each declined call was declined, by request id, for your logs. */
73
+ reasons: Record<string, string>;
74
+ }
75
+
76
+ /**
77
+ * Ask about every `mcp_approval_request` in a response's `output`, and build
78
+ * the answers. A call `when` does not select is approved without asking.
79
+ * Other items are ignored.
80
+ */
81
+ export async function answerPerplexityApprovals(
82
+ output: readonly { type?: string }[] | null | undefined,
83
+ options: PerplexityApprovalOptions
84
+ ): Promise<PerplexityApprovals> {
85
+ const result: PerplexityApprovals = { responses: [], reasons: {} };
86
+ for (const request of approvalRequests(output)) {
87
+ const input = parseArguments(request.arguments);
88
+ let approve = true;
89
+ let reason: string | undefined;
90
+ if (!options.when || (await options.when(request.name, input))) {
91
+ const outcome = await askAboutCall(request.name, input, `${PREFIX}${request.id}`, {
92
+ ...options,
93
+ metadata: {
94
+ ...options.metadata,
95
+ ...(request.server_label ? { server: request.server_label } : {}),
96
+ ...(request.connector_id ? { connector: request.connector_id } : {}),
97
+ },
98
+ });
99
+ approve = outcome.allowed;
100
+ if (!outcome.allowed) reason = outcome.reason;
101
+ }
102
+ const answer: PerplexityApprovalResponse = { type: "mcp_approval_response", approval_request_id: request.id, approve };
103
+ if (reason) {
104
+ result.reasons[request.id] = reason;
105
+ if (options.sendReason !== false) answer.reason = reason;
106
+ }
107
+ result.responses.push(answer);
108
+ }
109
+ return result;
110
+ }
111
+
112
+ /** A function call the model asked for in an Agent API response. */
113
+ export interface PerplexityFunctionCall {
114
+ type: "function_call";
115
+ call_id: string;
116
+ name: string;
117
+ arguments: string;
118
+ }
119
+
120
+ /** The result of one function call, sent as an `input` item of the next request. */
121
+ export interface PerplexityFunctionCallOutput {
122
+ type: "function_call_output";
123
+ call_id: string;
124
+ output: string;
125
+ }
126
+
127
+ /**
128
+ * Ask about, and run, every `function_call` in a response's `output`. Only
129
+ * approved calls run; a declined one's output tells the model a person said
130
+ * no, and why. Every call gets an output, because the run cannot continue
131
+ * without one.
132
+ */
133
+ export async function runPerplexityFunctionCalls(
134
+ output: readonly { type?: string }[] | null | undefined,
135
+ run: MuseToolRunner,
136
+ options: PerplexityApprovalOptions
137
+ ): Promise<PerplexityFunctionCallOutput[]> {
138
+ const results: PerplexityFunctionCallOutput[] = [];
139
+ for (const item of output ?? []) {
140
+ if (item.type !== "function_call") continue;
141
+ const call = item as PerplexityFunctionCall;
142
+ results.push({
143
+ type: "function_call_output",
144
+ call_id: call.call_id,
145
+ output: await approveAndRun(call.name, call.arguments, call.call_id, run, options, PREFIX),
146
+ });
147
+ }
148
+ return results;
149
+ }
150
+
151
+ /** Perplexity's client, or the OpenAI SDK pointed at `https://api.perplexity.ai/v1`. */
152
+ export interface PerplexityClient {
153
+ responses: { create(params: Record<string, unknown>): Promise<PerplexityResponse> | PromiseLike<PerplexityResponse> };
154
+ }
155
+
156
+ /** The parts of an Agent API response this reads. */
157
+ export interface PerplexityResponse {
158
+ id: string;
159
+ status?: string;
160
+ output?: { type?: string }[] | null;
161
+ }
162
+
163
+ export interface PerplexityRunOptions extends PerplexityApprovalOptions {
164
+ /** Makes your own function calls. Without it, a response asking for one is returned to you as it is. */
165
+ run?: MuseToolRunner;
166
+ /**
167
+ * Resend the conversation in `input` instead of chaining with
168
+ * `previous_response_id`. For accounts under zero data retention, which
169
+ * cannot use `previous_response_id`; each response's output, and the
170
+ * answers, are sent back unchanged.
171
+ */
172
+ replay?: boolean;
173
+ /** Stop after this many rounds of answers. Default 10. */
174
+ maxRounds?: number;
175
+ }
176
+
177
+ /**
178
+ * Create a response, and keep answering its approval requests and running its
179
+ * function calls until it asks for nothing more.
180
+ *
181
+ * Each follow-up carries everything in `params` (`model`, `tools`,
182
+ * `instructions`) except `input`, which becomes the answers and outputs, with
183
+ * the last response's id as `previous_response_id`; or, with `replay`, the
184
+ * whole conversation so far. The `tools` array goes unchanged, as Perplexity
185
+ * requires. Non-streaming, foreground responses only.
186
+ */
187
+ export async function runPerplexityWithDeliverd(
188
+ client: PerplexityClient,
189
+ params: Record<string, unknown>,
190
+ options: PerplexityRunOptions
191
+ ): Promise<PerplexityResponse> {
192
+ const { input: firstInput, previous_response_id: _chain, ...carried } = params;
193
+ void _chain;
194
+ let response = await client.responses.create(params);
195
+ let history: unknown[] = toItems(firstInput);
196
+ const maxRounds = options.maxRounds ?? 10;
197
+ for (let round = 0; ; round++) {
198
+ const output = response.output ?? [];
199
+ const asking = approvalRequests(output).length > 0;
200
+ const calling = !!options.run && output.some((item) => item.type === "function_call");
201
+ if (!asking && !calling) return response;
202
+ if (round >= maxRounds) {
203
+ throw new Error(`The run was still asking for decisions after ${maxRounds} rounds. Raise maxRounds if that is expected.`);
204
+ }
205
+ const { responses } = await answerPerplexityApprovals(output, options);
206
+ const results = options.run ? await runPerplexityFunctionCalls(output, options.run, options) : [];
207
+ const answers = [...responses, ...results];
208
+ if (options.replay) {
209
+ history = [...history, ...output, ...answers];
210
+ response = await client.responses.create({ ...carried, input: history });
211
+ } else {
212
+ response = await client.responses.create({ ...carried, input: answers, previous_response_id: response.id });
213
+ }
214
+ }
215
+ }
216
+
217
+ function approvalRequests(output: readonly { type?: string }[] | null | undefined): PerplexityApprovalRequest[] {
218
+ return (output ?? []).filter(
219
+ (item): item is PerplexityApprovalRequest => item.type === "mcp_approval_request" && !!(item as PerplexityApprovalRequest).id
220
+ );
221
+ }
222
+
223
+ function toItems(input: unknown): unknown[] {
224
+ if (input === undefined || input === null) return [];
225
+ if (typeof input === "string") return [{ type: "message", role: "user", content: input }];
226
+ return Array.isArray(input) ? [...input] : [input];
227
+ }
228
+
229
+ function parseArguments(raw: string | undefined): unknown {
230
+ if (!raw) return {};
231
+ try {
232
+ return JSON.parse(raw);
233
+ } catch {
234
+ return raw;
235
+ }
236
+ }
@@ -58,6 +58,17 @@ export const DATA_KINDS = {
58
58
  uk_national_insurance: { label: "UK National Insurance number", category: "personal", placeholder: "NINO", redactable: true },
59
59
  ie_pps: { label: "Irish PPS number", category: "personal", placeholder: "PPS", redactable: true },
60
60
  us_ssn: { label: "US Social Security number", category: "personal", placeholder: "SSN", redactable: true },
61
+ us_itin: { label: "US ITIN", category: "personal", placeholder: "ITIN", redactable: true },
62
+ ca_sin: { label: "Canadian Social Insurance number", category: "personal", placeholder: "SIN", redactable: true },
63
+ fr_nir: { label: "French social security number", category: "personal", placeholder: "NIR", redactable: true },
64
+ es_dni: { label: "Spanish DNI or NIE", category: "personal", placeholder: "DNI", redactable: true },
65
+ it_fiscal_code: { label: "Italian fiscal code", category: "personal", placeholder: "CF", redactable: true },
66
+ nl_bsn: { label: "Dutch BSN", category: "personal", placeholder: "BSN", redactable: true },
67
+ pl_pesel: { label: "Polish PESEL number", category: "personal", placeholder: "PESEL", redactable: true },
68
+ de_tax_id: { label: "German tax ID", category: "personal", placeholder: "STEUERID", redactable: true },
69
+ passport: { label: "Passport number", category: "personal", placeholder: "PASSPORT", redactable: true },
70
+ vat_number: { label: "VAT number", category: "financial", placeholder: "VAT", redactable: true },
71
+ bank_account: { label: "Bank account number", category: "financial", placeholder: "ACCOUNT", redactable: true },
61
72
  ip_address: { label: "IP address", category: "personal", placeholder: "IP", redactable: true },
62
73
  date_of_birth: { label: "Date of birth", category: "personal", placeholder: "DOB", redactable: true },
63
74
  /** Never found by a pattern here: only by a model detector the caller plugs in (`spans`). */
@@ -175,6 +186,100 @@ function ipv6Valid(raw: string): boolean {
175
186
  return compressed === 0 ? groups === 8 : compressed === 1 && groups >= 3 && groups <= 7;
176
187
  }
177
188
 
189
+ /** An ITIN: 9XX-XX-XXXX with a middle group the IRS issues (50-65, 70-88, 90-92, 94-99). */
190
+ function itinValid(raw: string): boolean {
191
+ const g = Number(raw.slice(4, 6));
192
+ return (g >= 50 && g <= 65) || (g >= 70 && g <= 88) || (g >= 90 && g <= 92) || (g >= 94 && g <= 99);
193
+ }
194
+
195
+ /** A Canadian SIN: nine digits, Luhn-checked; none is issued starting with 0 or 8. */
196
+ function sinValid(raw: string): boolean {
197
+ const d = digitsOf(raw);
198
+ return d.length === 9 && d[0] !== "0" && d[0] !== "8" && luhn(d);
199
+ }
200
+
201
+ /** An ABA routing number: weights 3, 7, 1 repeating, a multiple of 10. */
202
+ function abaValid(raw: string): boolean {
203
+ const d = digitsOf(raw);
204
+ if (d.length !== 9 || /^0+$/.test(d)) return false;
205
+ let sum = 0;
206
+ for (let i = 0; i < 9; i++) sum += (d.charCodeAt(i) - 48) * [3, 7, 1][i % 3]!;
207
+ return sum % 10 === 0;
208
+ }
209
+
210
+ /** A French NIR: thirteen digits (2A and 2B for Corsica) and a key, 97 minus the number mod 97. */
211
+ function nirValid(raw: string): boolean {
212
+ const s = raw.replace(/ /g, "").toUpperCase();
213
+ if (s.length !== 15) return false;
214
+ const body = s.slice(0, 13).replace("2A", "19").replace("2B", "18");
215
+ if (!/^\d{13}$/.test(body)) return false;
216
+ let mod = 0;
217
+ for (const ch of body) mod = (mod * 10 + (ch.charCodeAt(0) - 48)) % 97;
218
+ return 97 - mod === Number(s.slice(13));
219
+ }
220
+
221
+ /** A Spanish DNI (eight digits) or NIE (X, Y or Z and seven), and its check letter. */
222
+ function dniValid(raw: string): boolean {
223
+ const s = raw.replace("-", "").toUpperCase();
224
+ const n = Number(s.slice(0, -1).replace(/^X/, "0").replace(/^Y/, "1").replace(/^Z/, "2"));
225
+ return "TRWAGMYFPDXBNJZSQVHLCKE"[n % 23] === s.at(-1);
226
+ }
227
+
228
+ const CF_ODD = [1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23];
229
+
230
+ /** An Italian codice fiscale and its check letter. */
231
+ function fiscalCodeValid(raw: string): boolean {
232
+ const s = raw.toUpperCase();
233
+ let sum = 0;
234
+ for (let i = 0; i < 15; i++) {
235
+ const c = s.charCodeAt(i);
236
+ const v = c >= 65 ? c - 65 : c - 48;
237
+ sum += i % 2 === 0 ? CF_ODD[v]! : v;
238
+ }
239
+ return String.fromCharCode(65 + (sum % 26)) === s[15];
240
+ }
241
+
242
+ /** A Dutch BSN: the eleven test, weights 9 to 2 and -1 on the last digit. */
243
+ function bsnValid(raw: string): boolean {
244
+ const d = digitsOf(raw);
245
+ if (d.length !== 9 || /^0+$/.test(d)) return false;
246
+ let sum = 0;
247
+ for (let i = 0; i < 8; i++) sum += (d.charCodeAt(i) - 48) * (9 - i);
248
+ return (sum - (d.charCodeAt(8) - 48)) % 11 === 0;
249
+ }
250
+
251
+ /** A Polish PESEL: weights 1, 3, 7, 9 repeating, and a check digit. */
252
+ function peselValid(raw: string): boolean {
253
+ if (!/^\d{11}$/.test(raw)) return false;
254
+ let sum = 0;
255
+ for (let i = 0; i < 10; i++) sum += (raw.charCodeAt(i) - 48) * [1, 3, 7, 9][i % 4]!;
256
+ return (10 - (sum % 10)) % 10 === raw.charCodeAt(10) - 48;
257
+ }
258
+
259
+ /** A German Steuer-ID: eleven digits, not starting with 0, ISO 7064 MOD 11,10. */
260
+ function steuerIdValid(raw: string): boolean {
261
+ const d = digitsOf(raw);
262
+ if (d.length !== 11 || d[0] === "0") return false;
263
+ let product = 10;
264
+ for (let i = 0; i < 10; i++) {
265
+ let sum = (d.charCodeAt(i) - 48 + product) % 10;
266
+ if (sum === 0) sum = 10;
267
+ product = (2 * sum) % 11;
268
+ }
269
+ const check = 11 - product;
270
+ return (check === 10 ? 0 : check) === d.charCodeAt(10) - 48;
271
+ }
272
+
273
+ /** A passport number after the word: some digits in it, so "passport holder" is not one. */
274
+ function passportValid(raw: string): boolean {
275
+ return digitsOf(raw).length >= 4;
276
+ }
277
+
278
+ /** A VAT number after the word: written in capitals, as issued, with digits in it. */
279
+ function vatValid(raw: string): boolean {
280
+ return raw === raw.toUpperCase() && digitsOf(raw).length >= 6;
281
+ }
282
+
178
283
  /** A value in a .env line that is a placeholder, not a credential. */
179
284
  function envSecretValid(value: string): boolean {
180
285
  return !/^[<{%(]/.test(value) && !/your|example|placeholder|changeme|xxxx/i.test(value);
@@ -234,6 +339,33 @@ const DETECTORS: Detector[] = [
234
339
  pattern: /\b(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?\d{2} ?\d{2} ?\d{2} ?[A-D]\b/g,
235
340
  },
236
341
  { kind: "ie_pps", pattern: /\b\d{7}[A-W][A-IW]?\b/g, valid: ppsValid },
342
+ { kind: "us_itin", pattern: /\b9\d{2}-\d{2}-\d{4}\b/g, valid: itinValid },
343
+ // Written in groups of three, or as nine digits after the words.
344
+ { kind: "ca_sin", pattern: /\b\d{3}[ -]\d{3}[ -]\d{3}\b/g, valid: sinValid },
345
+ { kind: "ca_sin", pattern: /\b(?:SIN|social insurance (?:number|no\.?))[ \t:#]*(\d{9})\b/gi, valid: sinValid, group: 1 },
346
+ { kind: "fr_nir", pattern: /\b[12] ?\d{2} ?\d{2} ?(?:\d{2}|2[AB]) ?\d{3} ?\d{3} ?\d{2}\b/g, valid: nirValid },
347
+ { kind: "es_dni", pattern: /\b(?:\d{8}|[XYZ]\d{7})-?[A-Z]\b/g, valid: dniValid },
348
+ { kind: "it_fiscal_code", pattern: /\b[A-Z]{6}\d{2}[A-EHLMPRST]\d{2}[A-Z]\d{3}[A-Z]\b/g, valid: fiscalCodeValid },
349
+ // Nine or eleven bare digits are too often something else: these need the word before them.
350
+ { kind: "nl_bsn", pattern: /\b(?:BSN|burgerservicenummer)(?:-?nummer)?[ \t:#.]*(\d{9}|\d{4}\.\d{2}\.\d{3}|\d{3}[ .]\d{3}[ .]\d{3})\b/gi, valid: bsnValid, group: 1 },
351
+ { kind: "pl_pesel", pattern: /\bPESEL[ \t:#.]*(\d{11})\b/gi, valid: peselValid, group: 1 },
352
+ {
353
+ kind: "de_tax_id",
354
+ pattern: /\b(?:steuer-?id(?:entifikationsnummer)?|steuerliche identifikationsnummer|idnr\.?|tax id)[ \t:#.]*(\d{2} ?\d{3} ?\d{3} ?\d{3})\b/gi,
355
+ valid: steuerIdValid,
356
+ group: 1,
357
+ },
358
+ { kind: "passport", pattern: /\bpassport(?: (?:no\.?|number|#))?[ \t:#]*([A-Z0-9]{6,9})\b/gi, valid: passportValid, group: 1 },
359
+ {
360
+ kind: "vat_number",
361
+ pattern: /\b(?:VAT|BTW|TVA|IVA|USt-?IdNr\.?)(?: (?:no\.?|number|reg(?:istration)?(?: no\.?| number)?|id))?[ \t:#.]*((?:AT|BE|BG|CY|CZ|DE|DK|EE|EL|ES|FI|FR|GB|HR|HU|IE|IT|LT|LU|LV|MT|NL|PL|PT|RO|SE|SI|SK|XI) ?[0-9A-Z]{8,12})\b/gi,
362
+ valid: vatValid,
363
+ group: 1,
364
+ },
365
+ // Bank details after the words: a US routing number, a UK sort code, an account number.
366
+ { kind: "bank_account", pattern: /\b(?:routing|ABA|RTN)(?: (?:no\.?|number|#))?[ \t:#]*(\d{9})\b/gi, valid: abaValid, group: 1 },
367
+ { kind: "bank_account", pattern: /\bsort code[ \t:#]*(\d{2}[- ]?\d{2}[- ]?\d{2})\b/gi, group: 1 },
368
+ { kind: "bank_account", pattern: /\b(?:account|acct|a\/c)(?: (?:no\.?|number|#))?[ \t:#]*(\d{8,17})\b/gi, group: 1 },
237
369
  { kind: "email", pattern: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b/g },
238
370
  {
239
371
  kind: "date_of_birth",
@@ -262,7 +394,7 @@ const DETECTORS: Detector[] = [
262
394
 
263
395
  /** Earlier wins an overlap: a card number is not also a phone number. */
264
396
  const PRIORITY: Record<DataKind, number> = Object.fromEntries(
265
- ["secret", "iban", "payment_card", "us_ssn", "uk_national_insurance", "ie_pps", "email", "date_of_birth", "phone", "ip_address", "term", "person_name", "special_category"].map(
397
+ ["secret", "iban", "payment_card", "us_ssn", "uk_national_insurance", "ie_pps", "us_itin", "ca_sin", "fr_nir", "es_dni", "it_fiscal_code", "nl_bsn", "pl_pesel", "de_tax_id", "passport", "vat_number", "bank_account", "email", "date_of_birth", "phone", "ip_address", "term", "person_name", "special_category"].map(
266
398
  (k, i) => [k, i]
267
399
  )
268
400
  ) as Record<DataKind, number>;
@@ -466,6 +598,9 @@ export function readScreenPolicy(raw: unknown): ScreenPolicy {
466
598
  };
467
599
  }
468
600
 
601
+ /** Labels that start with a proper name, so they keep their capitals in a sentence. */
602
+ const KEEP_CASE: ReadonlySet<DataKind> = new Set(["ca_sin", "fr_nir", "it_fiscal_code", "nl_bsn", "pl_pesel", "de_tax_id"]);
603
+
469
604
  const plural = (n: number, label: string) => `${n} ${label.toLowerCase()}${n === 1 ? "" : "s"}`;
470
605
 
471
606
  /** "2 email addresses and 1 IBAN", for a reason or an approval's title. */
@@ -481,6 +616,8 @@ export function describeCounts(counts: ScreenCounts): string {
481
616
  if (k === "ip_address") return `${n} IP address${n === 1 ? "" : "es"}`;
482
617
  if (k === "date_of_birth") return `${n} date${n === 1 ? "" : "s"} of birth`;
483
618
  if (k === "ie_pps") return `${n} Irish PPS number${n === 1 ? "" : "s"}`;
619
+ if (k === "es_dni") return `${n} Spanish ${n === 1 ? "DNI or NIE" : "DNIs or NIEs"}`;
620
+ if (KEEP_CASE.has(k)) return `${n} ${label}${n === 1 ? "" : "s"}`;
484
621
  return /^[A-Z]{2,}/.test(label) ? `${n} ${label}${n === 1 ? "" : "s"}` : plural(n, label);
485
622
  });
486
623
  if (parts.length <= 1) return parts[0] ?? "nothing";