@ai-matrx/records 0.44.1 → 0.45.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/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # Changelog — @ai-matrx/records
2
2
 
3
+ ## 0.45.0
4
+
5
+ **One coercion primitive: what a typed-in answer means to a Field, in one place.**
6
+
7
+ AGENT-BUILDS-2's closing walk, 2026-09-21, found Ironclad Mobile Mechanic's public booking
8
+ page **unbookable over any table with a number field**. A customer picked 2:00 PM, typed a
9
+ 2019 Honda CR-V, pressed "Book it" and read:
10
+
11
+ > Vehicle Year takes a number, and it was given a string
12
+
13
+ and got no appointment. A browser input hands over a STRING; a Field takes a value. The
14
+ grid's paste and the CSV import each carried their own coercion — the two doors a STRANGER
15
+ uses carried none.
16
+
17
+ New, headless, and now the only body that answers the question:
18
+
19
+ - **`fieldKindFor(field, kernel)`** — the ONE classifier (behaviour, modifiers, format, unit,
20
+ relation target, config). `@ai-matrx/records-ui`'s `editorKindFor` is this function with
21
+ the kernel File/Person ids bound, so the editor a person types into and the coercion
22
+ applied to what they typed cannot disagree about a column.
23
+ - **`coerceTypedAnswer(field, raw, opts)`** — one string → the Field's value, or a refusal
24
+ naming the field **in plain words**, never its key. It coerces and never guesses: `0` for
25
+ "about twelve" is how a table quietly becomes untrue.
26
+ - **`coerceTypedAnswers(fields, answers, opts)`** — a whole screen at once. Values already
27
+ of the right shape are left alone; keys the table has no Field for are REPORTED rather
28
+ than dropped, because naming an unknown key is the store's sentence to say.
29
+ - **`CoercibleField`** — the structural shape the primitive needs, so `CaptureField` and the
30
+ Field documents `custom.form_public` / `custom.booking_public` publish to a stranger all
31
+ satisfy it. Demanding a whole `Field` is how the capture sheet kept its own `Number(raw)`.
32
+ - **`FIELD_KINDS`** — the runtime list `FieldKind` is derived from, so a coverage test walks
33
+ the source of truth rather than a retyped copy.
34
+ - **`ChoiceOptions`** moved here from records-ui, which re-exports it.
35
+
36
+ *Consumer action:* none required — everything is additive. A host that coerces typed-in text
37
+ itself should delete that code and call `coerceTypedAnswer`.
38
+
3
39
  ## 0.43.0
4
40
 
5
41
  **The realtime port stops being half a contract, and a nudge re-reads only what moved.**
@@ -40,6 +40,7 @@ __export(core_exports, {
40
40
  DELIVERIES: () => DELIVERIES,
41
41
  DOORS: () => DOORS,
42
42
  ENTITY_TOKENS: () => ENTITY_TOKENS,
43
+ FIELD_KINDS: () => FIELD_KINDS,
43
44
  FIELD_SENSITIVITIES: () => FIELD_SENSITIVITIES,
44
45
  FIELD_SOURCES: () => FIELD_SOURCES,
45
46
  FRESHNESS: () => FRESHNESS,
@@ -80,6 +81,9 @@ __export(core_exports, {
80
81
  choiceSlug: () => choiceSlug,
81
82
  choiceValuesOf: () => choiceValuesOf,
82
83
  choicesOf: () => choicesOf,
84
+ coerceTypedAnswer: () => coerceTypedAnswer,
85
+ coerceTypedAnswers: () => coerceTypedAnswers,
86
+ coercionLabel: () => coercionLabel,
83
87
  createRecordsClient: () => createRecordsClient,
84
88
  doorAbsent: () => doorAbsent,
85
89
  doorCensus: () => doorCensus,
@@ -87,6 +91,7 @@ __export(core_exports, {
87
91
  err: () => err,
88
92
  exportCsv: () => exportCsv,
89
93
  exportXlsx: () => exportXlsx,
94
+ fieldKindFor: () => fieldKindFor,
90
95
  forgetAllOps: () => forgetAllOps,
91
96
  highestLevel: () => highestLevel,
92
97
  holdsARetiredChoice: () => holdsARetiredChoice,
@@ -251,6 +256,209 @@ function looksLikeAnId(value) {
251
256
  return typeof value === "string" && UUID.test(value);
252
257
  }
253
258
 
259
+ // src/coerce.ts
260
+ var FIELD_KINDS = [
261
+ "attachment",
262
+ "checkbox",
263
+ "currency",
264
+ "datetime",
265
+ "email",
266
+ "formula",
267
+ "json",
268
+ "long_text",
269
+ "lookup",
270
+ "member",
271
+ "multi_select",
272
+ "number",
273
+ "percent",
274
+ "phone",
275
+ "relation",
276
+ "rollup",
277
+ "select",
278
+ "text",
279
+ "url"
280
+ ];
281
+ function fieldKindFor(field, kernel) {
282
+ const format = (field.format ?? "").toLowerCase();
283
+ const config = field.config ?? {};
284
+ switch (field.type) {
285
+ case "boolean":
286
+ return "checkbox";
287
+ case "text":
288
+ if (format === "email") return "email";
289
+ if (format === "phone") return "phone";
290
+ if (format === "url") return "url";
291
+ if (format === "json" || config["json"] === true) return "json";
292
+ if (format === "long" || config["multiline"] === true) return "long_text";
293
+ return "text";
294
+ case "range": {
295
+ const kind = String(config["kind"] ?? "number");
296
+ if (kind === "date" || kind === "datetime") return "datetime";
297
+ if (format === "currency") return "currency";
298
+ if (format === "percent" || field.unit === "%") return "percent";
299
+ return "number";
300
+ }
301
+ case "list":
302
+ return field.multi ? "multi_select" : "select";
303
+ case "relation": {
304
+ const parity = String(config["parity"] ?? "");
305
+ if (parity === "attachment" || parity === "member") return parity;
306
+ if (kernel?.file && field.relation_target === kernel.file) return "attachment";
307
+ if (kernel?.person && field.relation_target === kernel.person) return "member";
308
+ return "relation";
309
+ }
310
+ case "formula": {
311
+ const via = config["via"];
312
+ if (via && config["agg"]) return "rollup";
313
+ if (via) return "lookup";
314
+ return "formula";
315
+ }
316
+ default:
317
+ return "text";
318
+ }
319
+ }
320
+ var TRUE_WORDS = /* @__PURE__ */ new Set(["true", "yes", "y", "1", "\u2713", "x", "checked", "on"]);
321
+ var FALSE_WORDS = /* @__PURE__ */ new Set(["false", "no", "n", "0", "", "unchecked", "off"]);
322
+ function coercionLabel(field) {
323
+ const label = (field.label ?? "").trim();
324
+ if (label !== "") return label;
325
+ const name = (field.name ?? "").trim();
326
+ if (name !== "") return name;
327
+ return field.key.replace(/[_-]+/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()).trim();
328
+ }
329
+ function coerceTypedAnswer(field, raw, opts = {}) {
330
+ const label = opts.label ?? coercionLabel(field);
331
+ const text = raw.trim();
332
+ const kind = fieldKindFor(field, opts.kernel);
333
+ if (text === "" && kind !== "checkbox") return { value: null };
334
+ switch (kind) {
335
+ case "checkbox": {
336
+ const word = text.toLowerCase();
337
+ if (TRUE_WORDS.has(word)) return { value: true };
338
+ if (FALSE_WORDS.has(word)) return { value: word === "" ? null : false };
339
+ return {
340
+ refusal: `${label} is a tick box, so it takes yes or no \u2014 \u201C${raw}\u201D is neither. Write TRUE/FALSE, yes/no or 1/0.`
341
+ };
342
+ }
343
+ case "number":
344
+ case "currency":
345
+ case "percent": {
346
+ const negative = /^\(.*\)$/.test(text);
347
+ const bare = text.replace(/^\(|\)$/g, "").replace(/[\s,]/g, "").replace(/^[^\d.\-+]+/, "").replace(/[^\d.\-+eE]+$/, "");
348
+ const n = Number(bare);
349
+ if (bare === "" || Number.isNaN(n)) {
350
+ return {
351
+ refusal: `${label} holds a number, and \u201C${raw}\u201D is not one. Write digits \u2014 a currency symbol, a percent sign, commas and brackets for a negative are all fine.`
352
+ };
353
+ }
354
+ return { value: negative ? -n : n };
355
+ }
356
+ case "datetime": {
357
+ const wantsTime = String(field.config?.["kind"] ?? "date") === "datetime";
358
+ const plainDay = /^\s*(\d{4})-(\d{2})-(\d{2})\s*$/.exec(raw);
359
+ if (!wantsTime && plainDay) return { value: `${plainDay[1]}-${plainDay[2]}-${plainDay[3]}` };
360
+ const at = Date.parse(text);
361
+ if (Number.isNaN(at)) {
362
+ return {
363
+ refusal: `${label} holds a date, and \u201C${raw}\u201D is not one this can read. Write it as 2026-09-21, or the way your spreadsheet writes dates.`
364
+ };
365
+ }
366
+ const parsed = new Date(at);
367
+ if (wantsTime) return { value: parsed.toISOString() };
368
+ const y = parsed.getFullYear();
369
+ const m = String(parsed.getMonth() + 1).padStart(2, "0");
370
+ const d = String(parsed.getDate()).padStart(2, "0");
371
+ return { value: `${y}-${m}-${d}` };
372
+ }
373
+ case "json": {
374
+ try {
375
+ return { value: JSON.parse(text) };
376
+ } catch {
377
+ return {
378
+ refusal: `${label} holds a document, and \u201C${raw}\u201D is not valid JSON. Write the whole object, braces and all.`
379
+ };
380
+ }
381
+ }
382
+ case "select":
383
+ case "multi_select": {
384
+ if (!opts.options) {
385
+ return {
386
+ refusal: `${label} is a list and its options have not been read yet, so nothing can be matched to \u201C${raw}\u201D. Try again once the column's choices have loaded.`
387
+ };
388
+ }
389
+ const wanted = kind === "multi_select" ? text.split(/[,;]/).map((w) => w.trim()).filter((w) => w.length > 0) : [text];
390
+ if (wanted.length === 0) return { value: kind === "multi_select" ? [] : null };
391
+ const matched = [];
392
+ const unmatched = [];
393
+ for (const word of wanted) {
394
+ const hit = opts.options.byWord.get(word.toLowerCase());
395
+ if (hit === void 0) unmatched.push(word);
396
+ else matched.push(hit);
397
+ }
398
+ if (unmatched.length > 0) {
399
+ const offered = opts.options.labels.length > 0 ? opts.options.labels.join(", ") : "it has none yet";
400
+ return {
401
+ refusal: `${label} does not have ${unmatched.map((w) => `\u201C${w}\u201D`).join(" or ")} among its options. It offers: ${offered}.`,
402
+ unmatched
403
+ };
404
+ }
405
+ return { value: kind === "multi_select" ? matched : matched[0] };
406
+ }
407
+ // AN IDENTITY IS NOT A WORD. Writing the word would put a string where an
408
+ // id belongs, and every filter, colour and rollup built on that column
409
+ // would stop agreeing with it. So it is refused and the person is told
410
+ // which control does hold it.
411
+ case "member":
412
+ return {
413
+ refusal: `${label} names a person in this organization, so it is chosen rather than typed. Open it and pick them.`
414
+ };
415
+ case "attachment":
416
+ return {
417
+ refusal: `${label} holds a file, which cannot arrive as typed-in text. Open it and attach the file.`
418
+ };
419
+ case "relation":
420
+ return {
421
+ refusal: `${label} points at another record, so it is chosen rather than typed. Open it and pick the record.`
422
+ };
423
+ case "formula":
424
+ case "lookup":
425
+ case "rollup":
426
+ return {
427
+ refusal: `${label} is worked out by the system, so nothing can be written into it.`
428
+ };
429
+ default:
430
+ return { value: raw };
431
+ }
432
+ }
433
+ function coerceTypedAnswers(fields, answers, opts = {}) {
434
+ const byKey = new Map(fields.map((f) => [f.key, f]));
435
+ const values = {};
436
+ const refusals = {};
437
+ const unknownKeys = [];
438
+ for (const [key, given] of Object.entries(answers)) {
439
+ const field = byKey.get(key);
440
+ if (!field) {
441
+ unknownKeys.push(key);
442
+ values[key] = given;
443
+ continue;
444
+ }
445
+ if (typeof given !== "string") {
446
+ values[key] = given;
447
+ continue;
448
+ }
449
+ const opt = {};
450
+ const choices = opts.options?.get(key);
451
+ if (choices) opt.options = choices;
452
+ if (opts.kernel) opt.kernel = opts.kernel;
453
+ const label = opts.labels?.[key];
454
+ if (label) opt.label = label;
455
+ const answered = coerceTypedAnswer(field, given, opt);
456
+ if ("refusal" in answered) refusals[key] = answered.refusal;
457
+ else values[key] = answered.value;
458
+ }
459
+ return { values, refusals, unknownKeys };
460
+ }
461
+
254
462
  // src/pipeline.ts
255
463
  var PIPELINE_RULE_KINDS = ["moves", "requires", "who", "limit", "entry"];
256
464
  var STAGE_RULE_ON_FAIL = ["refuse", "require_approval"];