@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/dist/index.cjs CHANGED
@@ -26,6 +26,7 @@ __export(src_exports, {
26
26
  CONTEXT_POLICIES: () => CONTEXT_POLICIES,
27
27
  DELIVERIES: () => DELIVERIES,
28
28
  ENTITY_TOKENS: () => ENTITY_TOKENS,
29
+ FIELD_KINDS: () => FIELD_KINDS,
29
30
  FIELD_SENSITIVITIES: () => FIELD_SENSITIVITIES,
30
31
  FIELD_SOURCES: () => FIELD_SOURCES,
31
32
  FRESHNESS: () => FRESHNESS,
@@ -63,7 +64,11 @@ __export(src_exports, {
63
64
  choiceSlug: () => choiceSlug,
64
65
  choiceValuesOf: () => choiceValuesOf,
65
66
  choicesOf: () => choicesOf,
67
+ coerceTypedAnswer: () => coerceTypedAnswer,
68
+ coerceTypedAnswers: () => coerceTypedAnswers,
69
+ coercionLabel: () => coercionLabel,
66
70
  err: () => err,
71
+ fieldKindFor: () => fieldKindFor,
67
72
  forgetAllOps: () => forgetAllOps,
68
73
  highestLevel: () => highestLevel,
69
74
  holdsARetiredChoice: () => holdsARetiredChoice,
@@ -215,6 +220,209 @@ function looksLikeAnId(value) {
215
220
  return typeof value === "string" && UUID.test(value);
216
221
  }
217
222
 
223
+ // src/coerce.ts
224
+ var FIELD_KINDS = [
225
+ "attachment",
226
+ "checkbox",
227
+ "currency",
228
+ "datetime",
229
+ "email",
230
+ "formula",
231
+ "json",
232
+ "long_text",
233
+ "lookup",
234
+ "member",
235
+ "multi_select",
236
+ "number",
237
+ "percent",
238
+ "phone",
239
+ "relation",
240
+ "rollup",
241
+ "select",
242
+ "text",
243
+ "url"
244
+ ];
245
+ function fieldKindFor(field, kernel) {
246
+ const format = (field.format ?? "").toLowerCase();
247
+ const config = field.config ?? {};
248
+ switch (field.type) {
249
+ case "boolean":
250
+ return "checkbox";
251
+ case "text":
252
+ if (format === "email") return "email";
253
+ if (format === "phone") return "phone";
254
+ if (format === "url") return "url";
255
+ if (format === "json" || config["json"] === true) return "json";
256
+ if (format === "long" || config["multiline"] === true) return "long_text";
257
+ return "text";
258
+ case "range": {
259
+ const kind = String(config["kind"] ?? "number");
260
+ if (kind === "date" || kind === "datetime") return "datetime";
261
+ if (format === "currency") return "currency";
262
+ if (format === "percent" || field.unit === "%") return "percent";
263
+ return "number";
264
+ }
265
+ case "list":
266
+ return field.multi ? "multi_select" : "select";
267
+ case "relation": {
268
+ const parity = String(config["parity"] ?? "");
269
+ if (parity === "attachment" || parity === "member") return parity;
270
+ if (kernel?.file && field.relation_target === kernel.file) return "attachment";
271
+ if (kernel?.person && field.relation_target === kernel.person) return "member";
272
+ return "relation";
273
+ }
274
+ case "formula": {
275
+ const via = config["via"];
276
+ if (via && config["agg"]) return "rollup";
277
+ if (via) return "lookup";
278
+ return "formula";
279
+ }
280
+ default:
281
+ return "text";
282
+ }
283
+ }
284
+ var TRUE_WORDS = /* @__PURE__ */ new Set(["true", "yes", "y", "1", "\u2713", "x", "checked", "on"]);
285
+ var FALSE_WORDS = /* @__PURE__ */ new Set(["false", "no", "n", "0", "", "unchecked", "off"]);
286
+ function coercionLabel(field) {
287
+ const label = (field.label ?? "").trim();
288
+ if (label !== "") return label;
289
+ const name = (field.name ?? "").trim();
290
+ if (name !== "") return name;
291
+ return field.key.replace(/[_-]+/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()).trim();
292
+ }
293
+ function coerceTypedAnswer(field, raw, opts = {}) {
294
+ const label = opts.label ?? coercionLabel(field);
295
+ const text = raw.trim();
296
+ const kind = fieldKindFor(field, opts.kernel);
297
+ if (text === "" && kind !== "checkbox") return { value: null };
298
+ switch (kind) {
299
+ case "checkbox": {
300
+ const word = text.toLowerCase();
301
+ if (TRUE_WORDS.has(word)) return { value: true };
302
+ if (FALSE_WORDS.has(word)) return { value: word === "" ? null : false };
303
+ return {
304
+ 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.`
305
+ };
306
+ }
307
+ case "number":
308
+ case "currency":
309
+ case "percent": {
310
+ const negative = /^\(.*\)$/.test(text);
311
+ const bare = text.replace(/^\(|\)$/g, "").replace(/[\s,]/g, "").replace(/^[^\d.\-+]+/, "").replace(/[^\d.\-+eE]+$/, "");
312
+ const n = Number(bare);
313
+ if (bare === "" || Number.isNaN(n)) {
314
+ return {
315
+ 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.`
316
+ };
317
+ }
318
+ return { value: negative ? -n : n };
319
+ }
320
+ case "datetime": {
321
+ const wantsTime = String(field.config?.["kind"] ?? "date") === "datetime";
322
+ const plainDay = /^\s*(\d{4})-(\d{2})-(\d{2})\s*$/.exec(raw);
323
+ if (!wantsTime && plainDay) return { value: `${plainDay[1]}-${plainDay[2]}-${plainDay[3]}` };
324
+ const at = Date.parse(text);
325
+ if (Number.isNaN(at)) {
326
+ return {
327
+ 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.`
328
+ };
329
+ }
330
+ const parsed = new Date(at);
331
+ if (wantsTime) return { value: parsed.toISOString() };
332
+ const y = parsed.getFullYear();
333
+ const m = String(parsed.getMonth() + 1).padStart(2, "0");
334
+ const d = String(parsed.getDate()).padStart(2, "0");
335
+ return { value: `${y}-${m}-${d}` };
336
+ }
337
+ case "json": {
338
+ try {
339
+ return { value: JSON.parse(text) };
340
+ } catch {
341
+ return {
342
+ refusal: `${label} holds a document, and \u201C${raw}\u201D is not valid JSON. Write the whole object, braces and all.`
343
+ };
344
+ }
345
+ }
346
+ case "select":
347
+ case "multi_select": {
348
+ if (!opts.options) {
349
+ return {
350
+ 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.`
351
+ };
352
+ }
353
+ const wanted = kind === "multi_select" ? text.split(/[,;]/).map((w) => w.trim()).filter((w) => w.length > 0) : [text];
354
+ if (wanted.length === 0) return { value: kind === "multi_select" ? [] : null };
355
+ const matched = [];
356
+ const unmatched = [];
357
+ for (const word of wanted) {
358
+ const hit = opts.options.byWord.get(word.toLowerCase());
359
+ if (hit === void 0) unmatched.push(word);
360
+ else matched.push(hit);
361
+ }
362
+ if (unmatched.length > 0) {
363
+ const offered = opts.options.labels.length > 0 ? opts.options.labels.join(", ") : "it has none yet";
364
+ return {
365
+ refusal: `${label} does not have ${unmatched.map((w) => `\u201C${w}\u201D`).join(" or ")} among its options. It offers: ${offered}.`,
366
+ unmatched
367
+ };
368
+ }
369
+ return { value: kind === "multi_select" ? matched : matched[0] };
370
+ }
371
+ // AN IDENTITY IS NOT A WORD. Writing the word would put a string where an
372
+ // id belongs, and every filter, colour and rollup built on that column
373
+ // would stop agreeing with it. So it is refused and the person is told
374
+ // which control does hold it.
375
+ case "member":
376
+ return {
377
+ refusal: `${label} names a person in this organization, so it is chosen rather than typed. Open it and pick them.`
378
+ };
379
+ case "attachment":
380
+ return {
381
+ refusal: `${label} holds a file, which cannot arrive as typed-in text. Open it and attach the file.`
382
+ };
383
+ case "relation":
384
+ return {
385
+ refusal: `${label} points at another record, so it is chosen rather than typed. Open it and pick the record.`
386
+ };
387
+ case "formula":
388
+ case "lookup":
389
+ case "rollup":
390
+ return {
391
+ refusal: `${label} is worked out by the system, so nothing can be written into it.`
392
+ };
393
+ default:
394
+ return { value: raw };
395
+ }
396
+ }
397
+ function coerceTypedAnswers(fields, answers, opts = {}) {
398
+ const byKey = new Map(fields.map((f) => [f.key, f]));
399
+ const values = {};
400
+ const refusals = {};
401
+ const unknownKeys = [];
402
+ for (const [key, given] of Object.entries(answers)) {
403
+ const field = byKey.get(key);
404
+ if (!field) {
405
+ unknownKeys.push(key);
406
+ values[key] = given;
407
+ continue;
408
+ }
409
+ if (typeof given !== "string") {
410
+ values[key] = given;
411
+ continue;
412
+ }
413
+ const opt = {};
414
+ const choices = opts.options?.get(key);
415
+ if (choices) opt.options = choices;
416
+ if (opts.kernel) opt.kernel = opts.kernel;
417
+ const label = opts.labels?.[key];
418
+ if (label) opt.label = label;
419
+ const answered = coerceTypedAnswer(field, given, opt);
420
+ if ("refusal" in answered) refusals[key] = answered.refusal;
421
+ else values[key] = answered.value;
422
+ }
423
+ return { values, refusals, unknownKeys };
424
+ }
425
+
218
426
  // src/pipeline.ts
219
427
  var PIPELINE_RULE_KINDS = ["moves", "requires", "who", "limit", "entry"];
220
428
  var STAGE_RULE_ON_FAIL = ["refuse", "require_approval"];