jsfe 1.82.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 +14 -0
- package/dist/helpers/numbers.d.ts +6 -12
- package/dist/helpers/numbers.d.ts.map +1 -1
- package/dist/helpers/numbers.js +151 -24
- package/dist/runtime-identity.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -519,6 +519,20 @@ const engine = new WorkflowEngine(logger, aiCallback, flows, tools,
|
|
|
519
519
|
`isSpelledFragment`, `isAmountAnswer`, `amountInAnswer`, `readEmail`, `readPhone`,
|
|
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
|
+
- 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, 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
|
|
532
|
+
`{zip, street, unit, said}`.
|
|
533
|
+
- The conversation: `chatTranscript(cargo.chatTurns)` (`role: text` lines) and `conversationCopy(cargo.chatTurns, lang)`
|
|
534
|
+
(localized You / Assistant lines); `cargo.chatTurns` is the host's masked turns before the current one
|
|
535
|
+
([Sendables](docs/sendables.md)).
|
|
522
536
|
- Saying values: `amountToSpeech`, `dateToWords`, `last4`, `maskEmail`, `urlToSpeech`,
|
|
523
537
|
`textWithUrlToSpeech`, `anythingElse`, `goodbye`, `channelVerb(lang, cargo, capital)` ("say" on a call, "type"
|
|
524
538
|
elsewhere, or the host's `cargo.verb`), and `yesNoKeys(lang, cargo, yes, no)` ("Press 1 or say YES, or press 2 or say
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Every number in the answer, in order: [{value, said, start, end, restarts}]. value is the complete number's digits; said
|
|
3
|
-
* text it was read from (start/end index the answer); restarts the readings of the number once when the caller repeated it
|
|
4
|
-
* (restartsOf), else []. Number words
|
|
2
|
+
* Every number in the answer, in order: [{value, said, start, end, restarts, groups}]. value is the complete number's digits; said
|
|
3
|
+
* the text it was read from (start/end index the answer); restarts the readings of the number once when the caller repeated it
|
|
4
|
+
* (restartsOf), else []; groups its digits as the caller separated them ("07 10 1972" → ['07', '10', '1972']). Number words
|
|
5
5
|
* are numbers ("one thousand and 3" → 1003, "cinco cinco cinco" → 555); every other word separates two numbers.
|
|
6
6
|
*/
|
|
7
7
|
export function findNumbers(answer: any): {
|
|
@@ -10,16 +10,9 @@ export function findNumbers(answer: any): {
|
|
|
10
10
|
start: any;
|
|
11
11
|
end: any;
|
|
12
12
|
restarts: any[];
|
|
13
|
+
groups: string[];
|
|
13
14
|
}[];
|
|
14
|
-
|
|
15
|
-
* Which numbers are which, by the caller's tests: kinds = {name: (digits) => false | true | normalized value}. One value per kind.
|
|
16
|
-
* A number is read by every test its complete digits pass and, for a restart, every test the number once passes. A number
|
|
17
|
-
* read more than one way is ambiguous, and so is a kind given two different values: those kinds take no value, so the caller
|
|
18
|
-
* asks (the longest match wins only when it is the only match: "5603 5603" is an 8-digit account or SSN 5603 said twice).
|
|
19
|
-
* A number no test reads is ignored here; the caller decides whether that means asking again.
|
|
20
|
-
* → {values: {name: value}, ambiguous: [names], unread: [numbers]}
|
|
21
|
-
*/
|
|
22
|
-
export function assignNumbers(numbers: any, kinds: any): {
|
|
15
|
+
export function assignNumbers(numbers: any, specs: any, options: any): {
|
|
23
16
|
values: {};
|
|
24
17
|
ambiguous: any[];
|
|
25
18
|
unread: any[];
|
|
@@ -32,4 +25,5 @@ export function joinSpokenNumbers(answer: any, options?: {}): string;
|
|
|
32
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
|
|
33
26
|
* comma after a longer number separates two numbers ("92610, 56035214"). */
|
|
34
27
|
export const DIGIT_RUN: RegExp;
|
|
28
|
+
export const NUMBER_KINDS: string[];
|
|
35
29
|
//# sourceMappingURL=numbers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/helpers/numbers.js"],"names":[],"mappings":"
|
|
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"}
|
package/dist/helpers/numbers.js
CHANGED
|
@@ -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
|
|
8
|
-
* proprietary identifier is one more
|
|
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
|
|
@@ -113,9 +114,9 @@ function restartsOf(said) {
|
|
|
113
114
|
return [...found];
|
|
114
115
|
}
|
|
115
116
|
/**
|
|
116
|
-
* Every number in the answer, in order: [{value, said, start, end, restarts}]. value is the complete number's digits; said
|
|
117
|
-
* text it was read from (start/end index the answer); restarts the readings of the number once when the caller repeated it
|
|
118
|
-
* (restartsOf), else []. Number words
|
|
117
|
+
* Every number in the answer, in order: [{value, said, start, end, restarts, groups}]. value is the complete number's digits; said
|
|
118
|
+
* the text it was read from (start/end index the answer); restarts the readings of the number once when the caller repeated it
|
|
119
|
+
* (restartsOf), else []; groups its digits as the caller separated them ("07 10 1972" → ['07', '10', '1972']). Number words
|
|
119
120
|
* are numbers ("one thousand and 3" → 1003, "cinco cinco cinco" → 555); every other word separates two numbers.
|
|
120
121
|
*/
|
|
121
122
|
export function findNumbers(answer) {
|
|
@@ -149,31 +150,147 @@ export function findNumbers(answer) {
|
|
|
149
150
|
return [...text.matchAll(DIGIT_RUN)].map((m) => {
|
|
150
151
|
const said = m[0].replace(/[\s.,-]+$/, '');
|
|
151
152
|
const start = firstOf[m.index], end = lastOf[m.index + said.length - 1] + 1;
|
|
152
|
-
return { value: said.replace(/\D/g, ''), said: raw.slice(start, end), start, end, restarts: restartsOf(said)
|
|
153
|
+
return { value: said.replace(/\D/g, ''), said: raw.slice(start, end), start, end, restarts: restartsOf(said),
|
|
154
|
+
groups: said.split(/\D+/).filter(Boolean) };
|
|
153
155
|
});
|
|
154
156
|
}
|
|
155
157
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
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.
|
|
226
|
+
*
|
|
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.)
|
|
161
232
|
* → {values: {name: value}, ambiguous: [names], unread: [numbers]}
|
|
162
233
|
*/
|
|
163
|
-
|
|
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) {
|
|
238
|
+
const found = new Map();
|
|
239
|
+
const parts = (from) => {
|
|
240
|
+
if (from === groups.length)
|
|
241
|
+
return [[]];
|
|
242
|
+
const out = [];
|
|
243
|
+
for (let to = from + 1; to <= groups.length; to++) {
|
|
244
|
+
for (const rest of parts(to))
|
|
245
|
+
out.push([groups.slice(from, to).join(''), ...rest]);
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
};
|
|
249
|
+
for (const cut of parts(0)) {
|
|
250
|
+
if (cut.length < 2) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
else { /* the whole number was already tried */ }
|
|
254
|
+
const options = cut.map((digits) => readers.map(({ name, read }) => [name, read(digits)]).filter(([, v]) => v));
|
|
255
|
+
const assign = (i, used, chosen) => {
|
|
256
|
+
if (i === cut.length) {
|
|
257
|
+
found.set(JSON.stringify(chosen), chosen);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
else { /* next part */ }
|
|
261
|
+
for (const [name, value] of options[i]) {
|
|
262
|
+
if (used.has(name)) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
else { /* a spec not filled yet */ }
|
|
266
|
+
assign(i + 1, new Set(used).add(name), [...chosen, [name, value]]);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
if (options.every((o) => o.length))
|
|
270
|
+
assign(0, new Set(), []);
|
|
271
|
+
else { /* a part no spec reads: not this cut */ }
|
|
272
|
+
}
|
|
273
|
+
if (found.size === 1)
|
|
274
|
+
return [...found.values()][0];
|
|
275
|
+
if (found.size > 1)
|
|
276
|
+
return { ambiguous: [...new Set([...found.values()].flat().map(([name]) => name))] };
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
export function assignNumbers(numbers, specs, options) {
|
|
280
|
+
const readers = readersOf(specs);
|
|
281
|
+
const multiple = options?.multiple === true || options?.split === true;
|
|
164
282
|
const seen = {}, ambiguous = new Set(), unread = [];
|
|
165
|
-
const read = (test, digits) => { const r = test(digits); return r === true ? digits : r ? String(r) : ''; };
|
|
166
283
|
for (const n of numbers || []) {
|
|
167
284
|
const readings = [];
|
|
168
|
-
for (const
|
|
169
|
-
const whole = read(
|
|
285
|
+
for (const { name, read } of readers) {
|
|
286
|
+
const whole = read(n.value);
|
|
170
287
|
if (whole)
|
|
171
|
-
readings.push([
|
|
288
|
+
readings.push([name, whole]);
|
|
172
289
|
else { /* the complete number is not this kind */ }
|
|
173
290
|
for (const restart of n.restarts || []) {
|
|
174
|
-
const once = read(
|
|
291
|
+
const once = read(restart);
|
|
175
292
|
if (once)
|
|
176
|
-
readings.push([
|
|
293
|
+
readings.push([name, once]);
|
|
177
294
|
else { /* not this kind */ }
|
|
178
295
|
}
|
|
179
296
|
}
|
|
@@ -181,18 +298,28 @@ export function assignNumbers(numbers, kinds) {
|
|
|
181
298
|
(seen[readings[0][0]] ||= new Set()).add(readings[0][1]);
|
|
182
299
|
}
|
|
183
300
|
else if (readings.length > 1) {
|
|
184
|
-
readings.forEach(([
|
|
301
|
+
readings.forEach(([name]) => ambiguous.add(name));
|
|
185
302
|
}
|
|
186
303
|
else {
|
|
187
|
-
|
|
304
|
+
const groups = n.groups || [];
|
|
305
|
+
const split = multiple && groups.length > 1 && groups.length <= MAX_SPLIT_GROUPS ? splitReading(groups, readers) : null;
|
|
306
|
+
if (Array.isArray(split)) {
|
|
307
|
+
split.forEach(([name, value]) => (seen[name] ||= new Set()).add(value));
|
|
308
|
+
}
|
|
309
|
+
else if (split) {
|
|
310
|
+
split.ambiguous.forEach((name) => ambiguous.add(name));
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
unread.push(n);
|
|
314
|
+
}
|
|
188
315
|
}
|
|
189
316
|
}
|
|
190
317
|
const values = {};
|
|
191
|
-
for (const [
|
|
192
|
-
if (set.size === 1 && !ambiguous.has(
|
|
193
|
-
values[
|
|
318
|
+
for (const [name, set] of Object.entries(seen)) {
|
|
319
|
+
if (set.size === 1 && !ambiguous.has(name))
|
|
320
|
+
values[name] = [...set][0];
|
|
194
321
|
else
|
|
195
|
-
ambiguous.add(
|
|
322
|
+
ambiguous.add(name);
|
|
196
323
|
}
|
|
197
324
|
return { values, ambiguous: [...ambiguous], unread };
|
|
198
325
|
}
|
package/dist/runtime-identity.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const JSFE_RUNTIME_FINGERPRINT = "sha256:
|
|
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.
|
|
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",
|