jsfe 1.83.0 → 1.84.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -520,12 +520,15 @@ const engine = new WorkflowEngine(logger, aiCallback, flows, tools,
520
520
  `readInternationalPhone`, `readIdentifier`, `normalizeAndFindCapture`, and `matchInputs` (which of a
521
521
  form's inputs an answer fills; ties between inputs of the same form are returned for the flow to ask).
522
522
  - Reading numbers: `findNumbers(answer)` (every number in an answer, in order, number words included: `[{value, said,
523
- start, end, restarts, groups}]`; any other word separates two numbers) and `assignNumbers(numbers, kinds, options)`
524
- (which number is which, by the flow's tests `{name: (digits) => false | true | value}`; a number two tests read, or a
525
- kind given two values, is `ambiguous` and takes no value, so the flow asks). Options: `split: true` reads a number no
526
- test reads as values typed one after another ("07 10 1972 9238": a date, then a last four), cut only where the caller
527
- separated it and only when every part passes a test, and only when that reading is the single one; `multiple: [names]`
528
- lets those kinds take several values (a list). `addressDraft(raw)` splits a spoken or typed address into
523
+ start, end, restarts, groups}]`; any other word separates two numbers) and `assignNumbers(numbers, specs, options)`
524
+ (which number is which: `specs` is a list `[{name, kind, ...}]`, one value per spec. A known kind is a field preset of
525
+ digits (`zip`, `card_zip`, `ssn_last4`, `account_number`, `phone`, `card_number` with its Luhn check, `card_expiration`
526
+ MMYY, `card_cvv`) or `date` (MMDDYYYY or MMDDYY, valued `YYYY-MM-DD`; `past: true` for a date of birth); a custom kind is
527
+ described, `{kind: 'pattern', regex, flags?}` or `{kind: 'digits', min, max}`. An amount is read by the money reader, not
528
+ here. A number two specs read, or a spec given two values, is `ambiguous` and takes no value, so the flow asks.
529
+ `{multiple: true}` lets one answer fill several specs with a number typed in parts ("07 10 1972 9238": a date, then a
530
+ last four), cut only where the caller separated it, every digit consumed, and only when that reading is the single one.
531
+ An unknown kind or a malformed spec throws.) `addressDraft(raw)` splits a spoken or typed address into
529
532
  `{zip, street, unit, said}`.
530
533
  - The conversation: `chatTranscript(cargo.chatTurns)` (`role: text` lines) and `conversationCopy(cargo.chatTurns, lang)`
531
534
  (localized You / Assistant lines); `cargo.chatTurns` is the host's masked turns before the current one
@@ -12,7 +12,7 @@ export function findNumbers(answer: any): {
12
12
  restarts: any[];
13
13
  groups: string[];
14
14
  }[];
15
- export function assignNumbers(numbers: any, kinds: any, options: any): {
15
+ export function assignNumbers(numbers: any, specs: any, options: any): {
16
16
  values: {};
17
17
  ambiguous: any[];
18
18
  unread: any[];
@@ -25,4 +25,5 @@ export function joinSpokenNumbers(answer: any, options?: {}): string;
25
25
  * pauses between spoken groups that way (prod 2026-10-31: the ZIP 92610 said as "926, 1 0.", the code as "7, 9, 984. 5."). A
26
26
  * comma after a longer number separates two numbers ("92610, 56035214"). */
27
27
  export const DIGIT_RUN: RegExp;
28
+ export const NUMBER_KINDS: string[];
28
29
  //# sourceMappingURL=numbers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/helpers/numbers.js"],"names":[],"mappings":"AAmGA;;;;;GAKG;AACH;;;;;;;IAsBC;AAiDD;;;;EAqCC;AAED;mFACmF;AACnF,qEASC;AAvND;;;4EAG4E;AAC5E,+BAAqF"}
1
+ {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/helpers/numbers.js"],"names":[],"mappings":"AAoGA;;;;;GAKG;AACH;;;;;;;IAsBC;AAwGD;;;;EAmCC;AAED;mFACmF;AACnF,qEASC;AA5QD;;;4EAG4E;AAC5E,+BAAqF;AAsJrF,oCAAqD"}
@@ -4,10 +4,11 @@
4
4
  * that took pieces read a phone typed "323 5713211" as account 5713211 (prod 2026-11-17) and the year of a spoken date as an
5
5
  * SSN's last four (11-18).
6
6
  *
7
- * findNumbers finds; it never decides what a number is. The caller's own tests decide that (assignNumbers), so a tenant's
8
- * proprietary identifier is one more test, not a matcher here. Shape reading only; part of jsfe/helpers, imports a sibling.
7
+ * findNumbers finds; it never decides what a number is. The caller's specs decide that (assignNumbers: known kinds, or a
8
+ * described pattern), so a tenant's proprietary identifier is one more spec, not a matcher here. Shape reading only; part of jsfe/helpers, imports siblings.
9
9
  */
10
10
  import { cardinalWords, isNumberWord, plainWord } from './spoken-money.js';
11
+ import { validateCardNumber, validateExpiration } from './card-checks.js';
11
12
  /** A run of digits as said: spoken digits come apart ("4 3 2 1", "555-123-4567"), so a single space, dash or dot inside a run
12
13
  * joins it, and so does a comma (or a period and a space) after a group of one to three digits: speech-to-text writes the
13
14
  * pauses between spoken groups that way (prod 2026-10-31: the ZIP 92610 said as "926, 1 0.", the code as "7, 9, 984. 5."). A
@@ -154,25 +155,86 @@ export function findNumbers(answer) {
154
155
  });
155
156
  }
156
157
  /**
157
- * Which numbers are which, by the caller's tests: kinds = {name: (digits) => false | true | normalized value}. One value per kind.
158
- * A number is read by every test its complete digits pass and, for a restart, every test the number once passes. A number
159
- * read more than one way is ambiguous, and so is a kind given two different values: those kinds take no value, so the caller
160
- * asks (the longest match wins only when it is the only match: "5603 5603" is an 8-digit account or SSN 5603 said twice).
161
- * A number no test reads is ignored here; the caller decides whether that means asking again.
158
+ * The kinds a number can be (owner 2026-11-20): a known kind is a jsfe field preset of digits, or a date; a custom kind is
159
+ * described, never coded: {kind: 'pattern', regex, flags?} or {kind: 'digits', min, max}. Each reads a number's complete
160
+ * digits and gives its value (a date as YYYY-MM-DD, everything else its digits) or '' when it is not that kind. The preset
161
+ * lengths are pinned to FIELD_PRESETS by tests/join-spoken-numbers.test.mjs. An amount is not here: it is read by the
162
+ * money reader, never out of a run of digits.
163
+ */
164
+ const length = (min, max = min) => (digits) => (digits.length >= min && digits.length <= max ? digits : '');
165
+ /** A date typed as digits: MMDDYYYY or MMDDYY, day first ("17101981") only when it cannot be month first. past: no later
166
+ * year than this one, and a two-digit year is the most recent one (a date of birth). */
167
+ function dateDigits(digits, spec) {
168
+ if (digits.length !== 6 && digits.length !== 8)
169
+ return '';
170
+ const now = new Date().getFullYear();
171
+ const take = (m, d, y) => {
172
+ let year = Number(y);
173
+ if (y.length === 2)
174
+ year += spec.past === true ? (year > now % 100 ? 1900 : 2000) : 2000;
175
+ else { /* four digits, as typed */ }
176
+ const month = Number(m), day = Number(d), at = new Date(Date.UTC(year, month - 1, day));
177
+ const valid = year >= 1900 && year <= (spec.past === true ? now : 2099) && at.getUTCFullYear() === year && at.getUTCMonth() + 1 === month && at.getUTCDate() === day;
178
+ return valid ? `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}` : '';
179
+ };
180
+ const a = digits.slice(0, 2), b = digits.slice(2, 4), y = digits.slice(4);
181
+ return take(a, b, y) || (Number(a) > 12 ? take(b, a, y) : '');
182
+ }
183
+ const KNOWN_KINDS = {
184
+ date: (spec) => (digits) => dateDigits(digits, spec),
185
+ zip: () => length(5),
186
+ card_zip: () => length(5),
187
+ ssn_last4: () => length(4),
188
+ account_number: () => length(6, 12),
189
+ phone: () => length(10, 11),
190
+ card_cvv: () => length(3, 4),
191
+ card_number: () => (digits) => (digits.length >= 13 && digits.length <= 19 && validateCardNumber(digits) ? digits : ''),
192
+ card_expiration: () => (digits) => (digits.length === 4 && validateExpiration(digits.slice(0, 2), digits.slice(2)) ? digits : ''),
193
+ pattern: (spec) => { const rx = new RegExp(spec.regex, spec.flags || ''); return (digits) => (rx.test(digits) ? digits : ''); },
194
+ digits: (spec) => length(Number(spec.min), Number(spec.max ?? spec.min))
195
+ };
196
+ export const NUMBER_KINDS = Object.keys(KNOWN_KINDS);
197
+ /** specs → [{name, read}]. A spec list is the documented form; a {name: (digits) => false | true | value} object (jsfe
198
+ * 1.81-1.83) is still read the same way. An unknown kind, a pattern without its regex, digits without min, or a repeated
199
+ * name throws: a reader declared wrong fails where it is declared, never by reading nothing. */
200
+ function readersOf(specs) {
201
+ if (!Array.isArray(specs)) {
202
+ return Object.entries(specs || {}).map(([name, test]) => ({ name, read: (digits) => { const r = test(digits); return r === true ? digits : r ? String(r) : ''; } }));
203
+ }
204
+ else { /* a spec list */ }
205
+ const names = new Set();
206
+ return specs.map((spec) => {
207
+ const name = spec && spec.name, kind = spec && spec.kind;
208
+ if (!name || typeof name !== 'string' || names.has(name))
209
+ throw new Error(`assignNumbers: spec name ${JSON.stringify(name)} is missing or repeated`);
210
+ if (!Object.hasOwn(KNOWN_KINDS, kind))
211
+ throw new Error(`assignNumbers: ${name} has kind ${JSON.stringify(kind)}; the kinds are ${NUMBER_KINDS.join(', ')}`);
212
+ if (kind === 'pattern' && typeof spec.regex !== 'string')
213
+ throw new Error(`assignNumbers: ${name} is a pattern without its regex`);
214
+ if (kind === 'digits' && !(Number(spec.min) > 0))
215
+ throw new Error(`assignNumbers: ${name} is digits without min`);
216
+ names.add(name);
217
+ return { name, read: KNOWN_KINDS[kind](spec) };
218
+ });
219
+ }
220
+ /**
221
+ * Which numbers are which: specs = [{name, kind, ...}] (NUMBER_KINDS above), one value per spec. A number is read by every
222
+ * spec its complete digits fit and, for a restart, every spec the number once fits. A number read more than one way is
223
+ * ambiguous, and so is a spec given two different values: those take no value, so the caller asks (the longest match wins
224
+ * only when it is the only match: "5603 5603" is an 8-digit account or SSN 5603 said twice). A number no spec reads is
225
+ * returned in unread; the caller decides whether that means asking again.
162
226
  *
163
- * options (optional; without them nothing above changes):
164
- * - split: true — a number no test reads, whole or as a restart, may be several values the caller typed one after another
165
- * (owner 2026-11-20; prod WhatsApp 11-20 08:23 "07 10 1972 9238" was a date of birth and an SSN's last four, asked for
166
- * "2 digits for the month, 2 for the day and 4 for the year"). It is split only where the caller separated it, never inside
167
- * a group, and only into parts that EVERY one passes a test, so every digit is consumed. Exactly one such reading is taken;
168
- * two or more make their kinds ambiguous; none leaves the number unread. A kind takes one part unless it is in multiple.
169
- * - multiple: [names] — those kinds may take several values: their value is the list of distinct values in order.
227
+ * options.multiple: true — one answer may fill several specs with one number typed in parts (owner 2026-11-20; prod WhatsApp
228
+ * 11-20 08:23 "07 10 1972 9238" was a date of birth and an SSN's last four). A number no spec reads whole is cut only where
229
+ * the caller separated it, never inside a group, into parts that each fill a different spec, so every digit is consumed;
230
+ * exactly one such reading is taken, two or more make their specs ambiguous, none leaves the number unread. (split: true is
231
+ * the jsfe 1.83.0 name for it.)
170
232
  * → {values: {name: value}, ambiguous: [names], unread: [numbers]}
171
233
  */
172
- const MAX_SPLIT_GROUPS = 12; // 2^11 ways to split; a longer run is digits said one by one, never several values
173
- /** The single reading of a number as consecutive parts, each read by a test: [[kind, value], ...]; null for none; 'ambiguous'
174
- * (with the kinds) for several. */
175
- function splitReading(groups, kinds, read, multiple) {
234
+ const MAX_SPLIT_GROUPS = 12; // 2^11 ways to cut; a longer run is digits said one by one, never several values
235
+ /** The single reading of a number as consecutive parts, each filling a different spec: [[name, value], ...]; null for none;
236
+ * {ambiguous: [names]} for several. */
237
+ function splitReading(groups, readers) {
176
238
  const found = new Map();
177
239
  const parts = (from) => {
178
240
  if (from === groups.length)
@@ -189,46 +251,46 @@ function splitReading(groups, kinds, read, multiple) {
189
251
  continue;
190
252
  }
191
253
  else { /* the whole number was already tried */ }
192
- const options = cut.map((digits) => Object.entries(kinds).map(([kind, test]) => [kind, read(test, digits)]).filter(([, v]) => v));
254
+ const options = cut.map((digits) => readers.map(({ name, read }) => [name, read(digits)]).filter(([, v]) => v));
193
255
  const assign = (i, used, chosen) => {
194
256
  if (i === cut.length) {
195
257
  found.set(JSON.stringify(chosen), chosen);
196
258
  return;
197
259
  }
198
260
  else { /* next part */ }
199
- for (const [kind, value] of options[i]) {
200
- if (used.has(kind) && !multiple.has(kind)) {
261
+ for (const [name, value] of options[i]) {
262
+ if (used.has(name)) {
201
263
  continue;
202
264
  }
203
- else { /* this kind may take this part */ }
204
- assign(i + 1, new Set(used).add(kind), [...chosen, [kind, value]]);
265
+ else { /* a spec not filled yet */ }
266
+ assign(i + 1, new Set(used).add(name), [...chosen, [name, value]]);
205
267
  }
206
268
  };
207
269
  if (options.every((o) => o.length))
208
270
  assign(0, new Set(), []);
209
- else { /* a part no test reads: not this cut */ }
271
+ else { /* a part no spec reads: not this cut */ }
210
272
  }
211
273
  if (found.size === 1)
212
274
  return [...found.values()][0];
213
275
  if (found.size > 1)
214
- return { ambiguous: [...new Set([...found.values()].flat().map(([kind]) => kind))] };
276
+ return { ambiguous: [...new Set([...found.values()].flat().map(([name]) => name))] };
215
277
  return null;
216
278
  }
217
- export function assignNumbers(numbers, kinds, options) {
279
+ export function assignNumbers(numbers, specs, options) {
280
+ const readers = readersOf(specs);
281
+ const multiple = options?.multiple === true || options?.split === true;
218
282
  const seen = {}, ambiguous = new Set(), unread = [];
219
- const multiple = new Set(Array.isArray(options?.multiple) ? options.multiple : []);
220
- const read = (test, digits) => { const r = test(digits); return r === true ? digits : r ? String(r) : ''; };
221
283
  for (const n of numbers || []) {
222
284
  const readings = [];
223
- for (const [kind, test] of Object.entries(kinds || {})) {
224
- const whole = read(test, n.value);
285
+ for (const { name, read } of readers) {
286
+ const whole = read(n.value);
225
287
  if (whole)
226
- readings.push([kind, whole]);
288
+ readings.push([name, whole]);
227
289
  else { /* the complete number is not this kind */ }
228
290
  for (const restart of n.restarts || []) {
229
- const once = read(test, restart);
291
+ const once = read(restart);
230
292
  if (once)
231
- readings.push([kind, once]);
293
+ readings.push([name, once]);
232
294
  else { /* not this kind */ }
233
295
  }
234
296
  }
@@ -236,17 +298,16 @@ export function assignNumbers(numbers, kinds, options) {
236
298
  (seen[readings[0][0]] ||= new Set()).add(readings[0][1]);
237
299
  }
238
300
  else if (readings.length > 1) {
239
- readings.forEach(([kind]) => ambiguous.add(kind));
301
+ readings.forEach(([name]) => ambiguous.add(name));
240
302
  }
241
303
  else {
242
304
  const groups = n.groups || [];
243
- const split = options?.split === true && groups.length > 1 && groups.length <= MAX_SPLIT_GROUPS
244
- ? splitReading(groups, kinds || {}, read, multiple) : null;
305
+ const split = multiple && groups.length > 1 && groups.length <= MAX_SPLIT_GROUPS ? splitReading(groups, readers) : null;
245
306
  if (Array.isArray(split)) {
246
- split.forEach(([kind, value]) => (seen[kind] ||= new Set()).add(value));
307
+ split.forEach(([name, value]) => (seen[name] ||= new Set()).add(value));
247
308
  }
248
309
  else if (split) {
249
- split.ambiguous.forEach((kind) => ambiguous.add(kind));
310
+ split.ambiguous.forEach((name) => ambiguous.add(name));
250
311
  }
251
312
  else {
252
313
  unread.push(n);
@@ -254,17 +315,11 @@ export function assignNumbers(numbers, kinds, options) {
254
315
  }
255
316
  }
256
317
  const values = {};
257
- for (const [kind, set] of Object.entries(seen)) {
258
- if (ambiguous.has(kind)) {
259
- continue;
260
- }
261
- else { /* read */ }
262
- if (multiple.has(kind))
263
- values[kind] = [...set];
264
- else if (set.size === 1)
265
- values[kind] = [...set][0];
318
+ for (const [name, set] of Object.entries(seen)) {
319
+ if (set.size === 1 && !ambiguous.has(name))
320
+ values[name] = [...set][0];
266
321
  else
267
- ambiguous.add(kind);
322
+ ambiguous.add(name);
268
323
  }
269
324
  return { values, ambiguous: [...ambiguous], unread };
270
325
  }
@@ -1 +1 @@
1
- export const JSFE_RUNTIME_FINGERPRINT = "sha256:23bed0ce253ee5a0982df144b00f92c28511340e9fa6653f6122f60b62412fdd";
1
+ export const JSFE_RUNTIME_FINGERPRINT = "sha256:b83c69872c78bee0affda14a3693b2ef736e8f26f35909008e83c9ea9e2676fc";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsfe",
3
- "version": "1.83.0",
3
+ "version": "1.84.0",
4
4
  "description": "TypeScript workflow engine for conversational/automation apps: multi-flow orchestration, interrupt/resume, stack-based control, robust errors, and flexible tool/REST integration.",
5
5
  "license": "MIT",
6
6
  "author": "Ron Pinkas",